1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <assert.h>
12 #include <stdlib.h> // qsort()
13
14 #include "./vp9_rtcd.h"
15 #include "./vpx_dsp_rtcd.h"
16 #include "./vpx_scale_rtcd.h"
17
18 #include "vpx_dsp/bitreader_buffer.h"
19 #include "vpx_dsp/bitreader.h"
20 #include "vpx_dsp/vpx_dsp_common.h"
21 #include "vpx_mem/vpx_mem.h"
22 #include "vpx_ports/mem.h"
23 #include "vpx_ports/mem_ops.h"
24 #include "vpx_scale/vpx_scale.h"
25 #include "vpx_util/vpx_thread.h"
26
27 #include "vp9/common/vp9_alloccommon.h"
28 #include "vp9/common/vp9_common.h"
29 #include "vp9/common/vp9_entropy.h"
30 #include "vp9/common/vp9_entropymode.h"
31 #include "vp9/common/vp9_idct.h"
32 #include "vp9/common/vp9_thread_common.h"
33 #include "vp9/common/vp9_pred_common.h"
34 #include "vp9/common/vp9_quant_common.h"
35 #include "vp9/common/vp9_reconintra.h"
36 #include "vp9/common/vp9_reconinter.h"
37 #include "vp9/common/vp9_seg_common.h"
38 #include "vp9/common/vp9_tile_common.h"
39
40 #include "vp9/decoder/vp9_decodeframe.h"
41 #include "vp9/decoder/vp9_detokenize.h"
42 #include "vp9/decoder/vp9_decodemv.h"
43 #include "vp9/decoder/vp9_decoder.h"
44 #include "vp9/decoder/vp9_dsubexp.h"
45
46 #define MAX_VP9_HEADER_SIZE 80
47
48 typedef int (*predict_recon_func)(TileWorkerData *twd, MODE_INFO *const mi,
49 int plane, int row, int col, TX_SIZE tx_size);
50
51 typedef void (*intra_recon_func)(TileWorkerData *twd, MODE_INFO *const mi,
52 int plane, int row, int col, TX_SIZE tx_size);
53
read_is_valid(const uint8_t * start,size_t len,const uint8_t * end)54 static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
55 return len != 0 && len <= (size_t)(end - start);
56 }
57
decode_unsigned_max(struct vpx_read_bit_buffer * rb,int max)58 static int decode_unsigned_max(struct vpx_read_bit_buffer *rb, int max) {
59 const int data = vpx_rb_read_literal(rb, get_unsigned_bits(max));
60 return data > max ? max : data;
61 }
62
read_tx_mode(vpx_reader * r)63 static TX_MODE read_tx_mode(vpx_reader *r) {
64 TX_MODE tx_mode = vpx_read_literal(r, 2);
65 if (tx_mode == ALLOW_32X32) tx_mode += vpx_read_bit(r);
66 return tx_mode;
67 }
68
read_tx_mode_probs(struct tx_probs * tx_probs,vpx_reader * r)69 static void read_tx_mode_probs(struct tx_probs *tx_probs, vpx_reader *r) {
70 int i, j;
71
72 for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
73 for (j = 0; j < TX_SIZES - 3; ++j)
74 vp9_diff_update_prob(r, &tx_probs->p8x8[i][j]);
75
76 for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
77 for (j = 0; j < TX_SIZES - 2; ++j)
78 vp9_diff_update_prob(r, &tx_probs->p16x16[i][j]);
79
80 for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
81 for (j = 0; j < TX_SIZES - 1; ++j)
82 vp9_diff_update_prob(r, &tx_probs->p32x32[i][j]);
83 }
84
read_switchable_interp_probs(FRAME_CONTEXT * fc,vpx_reader * r)85 static void read_switchable_interp_probs(FRAME_CONTEXT *fc, vpx_reader *r) {
86 int i, j;
87 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
88 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
89 vp9_diff_update_prob(r, &fc->switchable_interp_prob[j][i]);
90 }
91
read_inter_mode_probs(FRAME_CONTEXT * fc,vpx_reader * r)92 static void read_inter_mode_probs(FRAME_CONTEXT *fc, vpx_reader *r) {
93 int i, j;
94 for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
95 for (j = 0; j < INTER_MODES - 1; ++j)
96 vp9_diff_update_prob(r, &fc->inter_mode_probs[i][j]);
97 }
98
read_frame_reference_mode(const VP9_COMMON * cm,vpx_reader * r)99 static REFERENCE_MODE read_frame_reference_mode(const VP9_COMMON *cm,
100 vpx_reader *r) {
101 if (vp9_compound_reference_allowed(cm)) {
102 return vpx_read_bit(r)
103 ? (vpx_read_bit(r) ? REFERENCE_MODE_SELECT : COMPOUND_REFERENCE)
104 : SINGLE_REFERENCE;
105 } else {
106 return SINGLE_REFERENCE;
107 }
108 }
109
read_frame_reference_mode_probs(VP9_COMMON * cm,vpx_reader * r)110 static void read_frame_reference_mode_probs(VP9_COMMON *cm, vpx_reader *r) {
111 FRAME_CONTEXT *const fc = cm->fc;
112 int i;
113
114 if (cm->reference_mode == REFERENCE_MODE_SELECT)
115 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
116 vp9_diff_update_prob(r, &fc->comp_inter_prob[i]);
117
118 if (cm->reference_mode != COMPOUND_REFERENCE)
119 for (i = 0; i < REF_CONTEXTS; ++i) {
120 vp9_diff_update_prob(r, &fc->single_ref_prob[i][0]);
121 vp9_diff_update_prob(r, &fc->single_ref_prob[i][1]);
122 }
123
124 if (cm->reference_mode != SINGLE_REFERENCE)
125 for (i = 0; i < REF_CONTEXTS; ++i)
126 vp9_diff_update_prob(r, &fc->comp_ref_prob[i]);
127 }
128
update_mv_probs(vpx_prob * p,int n,vpx_reader * r)129 static void update_mv_probs(vpx_prob *p, int n, vpx_reader *r) {
130 int i;
131 for (i = 0; i < n; ++i)
132 if (vpx_read(r, MV_UPDATE_PROB)) p[i] = (vpx_read_literal(r, 7) << 1) | 1;
133 }
134
read_mv_probs(nmv_context * ctx,int allow_hp,vpx_reader * r)135 static void read_mv_probs(nmv_context *ctx, int allow_hp, vpx_reader *r) {
136 int i, j;
137
138 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
139
140 for (i = 0; i < 2; ++i) {
141 nmv_component *const comp_ctx = &ctx->comps[i];
142 update_mv_probs(&comp_ctx->sign, 1, r);
143 update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
144 update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
145 update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
146 }
147
148 for (i = 0; i < 2; ++i) {
149 nmv_component *const comp_ctx = &ctx->comps[i];
150 for (j = 0; j < CLASS0_SIZE; ++j)
151 update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
152 update_mv_probs(comp_ctx->fp, 3, r);
153 }
154
155 if (allow_hp) {
156 for (i = 0; i < 2; ++i) {
157 nmv_component *const comp_ctx = &ctx->comps[i];
158 update_mv_probs(&comp_ctx->class0_hp, 1, r);
159 update_mv_probs(&comp_ctx->hp, 1, r);
160 }
161 }
162 }
163
inverse_transform_block_inter(MACROBLOCKD * xd,int plane,const TX_SIZE tx_size,uint8_t * dst,int stride,int eob)164 static void inverse_transform_block_inter(MACROBLOCKD *xd, int plane,
165 const TX_SIZE tx_size, uint8_t *dst,
166 int stride, int eob) {
167 struct macroblockd_plane *const pd = &xd->plane[plane];
168 tran_low_t *const dqcoeff = pd->dqcoeff;
169 assert(eob > 0);
170 #if CONFIG_VP9_HIGHBITDEPTH
171 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
172 uint16_t *const dst16 = CONVERT_TO_SHORTPTR(dst);
173 if (xd->lossless) {
174 vp9_highbd_iwht4x4_add(dqcoeff, dst16, stride, eob, xd->bd);
175 } else {
176 switch (tx_size) {
177 case TX_4X4:
178 vp9_highbd_idct4x4_add(dqcoeff, dst16, stride, eob, xd->bd);
179 break;
180 case TX_8X8:
181 vp9_highbd_idct8x8_add(dqcoeff, dst16, stride, eob, xd->bd);
182 break;
183 case TX_16X16:
184 vp9_highbd_idct16x16_add(dqcoeff, dst16, stride, eob, xd->bd);
185 break;
186 case TX_32X32:
187 vp9_highbd_idct32x32_add(dqcoeff, dst16, stride, eob, xd->bd);
188 break;
189 default: assert(0 && "Invalid transform size");
190 }
191 }
192 } else {
193 if (xd->lossless) {
194 vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
195 } else {
196 switch (tx_size) {
197 case TX_4X4: vp9_idct4x4_add(dqcoeff, dst, stride, eob); break;
198 case TX_8X8: vp9_idct8x8_add(dqcoeff, dst, stride, eob); break;
199 case TX_16X16: vp9_idct16x16_add(dqcoeff, dst, stride, eob); break;
200 case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
201 default: assert(0 && "Invalid transform size"); return;
202 }
203 }
204 }
205 #else
206 if (xd->lossless) {
207 vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
208 } else {
209 switch (tx_size) {
210 case TX_4X4: vp9_idct4x4_add(dqcoeff, dst, stride, eob); break;
211 case TX_8X8: vp9_idct8x8_add(dqcoeff, dst, stride, eob); break;
212 case TX_16X16: vp9_idct16x16_add(dqcoeff, dst, stride, eob); break;
213 case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
214 default: assert(0 && "Invalid transform size"); return;
215 }
216 }
217 #endif // CONFIG_VP9_HIGHBITDEPTH
218
219 if (eob == 1) {
220 dqcoeff[0] = 0;
221 } else {
222 if (tx_size <= TX_16X16 && eob <= 10)
223 memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
224 else if (tx_size == TX_32X32 && eob <= 34)
225 memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
226 else
227 memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
228 }
229 }
230
inverse_transform_block_intra(MACROBLOCKD * xd,int plane,const TX_TYPE tx_type,const TX_SIZE tx_size,uint8_t * dst,int stride,int eob)231 static void inverse_transform_block_intra(MACROBLOCKD *xd, int plane,
232 const TX_TYPE tx_type,
233 const TX_SIZE tx_size, uint8_t *dst,
234 int stride, int eob) {
235 struct macroblockd_plane *const pd = &xd->plane[plane];
236 tran_low_t *const dqcoeff = pd->dqcoeff;
237 assert(eob > 0);
238 #if CONFIG_VP9_HIGHBITDEPTH
239 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
240 uint16_t *const dst16 = CONVERT_TO_SHORTPTR(dst);
241 if (xd->lossless) {
242 vp9_highbd_iwht4x4_add(dqcoeff, dst16, stride, eob, xd->bd);
243 } else {
244 switch (tx_size) {
245 case TX_4X4:
246 vp9_highbd_iht4x4_add(tx_type, dqcoeff, dst16, stride, eob, xd->bd);
247 break;
248 case TX_8X8:
249 vp9_highbd_iht8x8_add(tx_type, dqcoeff, dst16, stride, eob, xd->bd);
250 break;
251 case TX_16X16:
252 vp9_highbd_iht16x16_add(tx_type, dqcoeff, dst16, stride, eob, xd->bd);
253 break;
254 case TX_32X32:
255 vp9_highbd_idct32x32_add(dqcoeff, dst16, stride, eob, xd->bd);
256 break;
257 default: assert(0 && "Invalid transform size");
258 }
259 }
260 } else {
261 if (xd->lossless) {
262 vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
263 } else {
264 switch (tx_size) {
265 case TX_4X4: vp9_iht4x4_add(tx_type, dqcoeff, dst, stride, eob); break;
266 case TX_8X8: vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob); break;
267 case TX_16X16:
268 vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob);
269 break;
270 case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
271 default: assert(0 && "Invalid transform size"); return;
272 }
273 }
274 }
275 #else
276 if (xd->lossless) {
277 vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
278 } else {
279 switch (tx_size) {
280 case TX_4X4: vp9_iht4x4_add(tx_type, dqcoeff, dst, stride, eob); break;
281 case TX_8X8: vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob); break;
282 case TX_16X16:
283 vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob);
284 break;
285 case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
286 default: assert(0 && "Invalid transform size"); return;
287 }
288 }
289 #endif // CONFIG_VP9_HIGHBITDEPTH
290
291 if (eob == 1) {
292 dqcoeff[0] = 0;
293 } else {
294 if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
295 memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
296 else if (tx_size == TX_32X32 && eob <= 34)
297 memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
298 else
299 memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
300 }
301 }
302
predict_and_reconstruct_intra_block(TileWorkerData * twd,MODE_INFO * const mi,int plane,int row,int col,TX_SIZE tx_size)303 static void predict_and_reconstruct_intra_block(TileWorkerData *twd,
304 MODE_INFO *const mi, int plane,
305 int row, int col,
306 TX_SIZE tx_size) {
307 MACROBLOCKD *const xd = &twd->xd;
308 struct macroblockd_plane *const pd = &xd->plane[plane];
309 PREDICTION_MODE mode = (plane == 0) ? mi->mode : mi->uv_mode;
310 uint8_t *dst;
311 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
312
313 if (mi->sb_type < BLOCK_8X8)
314 if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
315
316 vp9_predict_intra_block(xd, pd->n4_wl, tx_size, mode, dst, pd->dst.stride,
317 dst, pd->dst.stride, col, row, plane);
318
319 if (!mi->skip) {
320 const TX_TYPE tx_type =
321 (plane || xd->lossless) ? DCT_DCT : intra_mode_to_tx_type_lookup[mode];
322 const scan_order *sc = (plane || xd->lossless)
323 ? &vp9_default_scan_orders[tx_size]
324 : &vp9_scan_orders[tx_size][tx_type];
325 const int eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size,
326 mi->segment_id);
327 if (eob > 0) {
328 inverse_transform_block_intra(xd, plane, tx_type, tx_size, dst,
329 pd->dst.stride, eob);
330 }
331 }
332 }
333
parse_intra_block_row_mt(TileWorkerData * twd,MODE_INFO * const mi,int plane,int row,int col,TX_SIZE tx_size)334 static void parse_intra_block_row_mt(TileWorkerData *twd, MODE_INFO *const mi,
335 int plane, int row, int col,
336 TX_SIZE tx_size) {
337 MACROBLOCKD *const xd = &twd->xd;
338 PREDICTION_MODE mode = (plane == 0) ? mi->mode : mi->uv_mode;
339
340 if (mi->sb_type < BLOCK_8X8)
341 if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
342
343 if (!mi->skip) {
344 struct macroblockd_plane *const pd = &xd->plane[plane];
345 const TX_TYPE tx_type =
346 (plane || xd->lossless) ? DCT_DCT : intra_mode_to_tx_type_lookup[mode];
347 const scan_order *sc = (plane || xd->lossless)
348 ? &vp9_default_scan_orders[tx_size]
349 : &vp9_scan_orders[tx_size][tx_type];
350 *pd->eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size,
351 mi->segment_id);
352 /* Keep the alignment to 16 */
353 pd->dqcoeff += (16 << (tx_size << 1));
354 pd->eob++;
355 }
356 }
357
predict_and_reconstruct_intra_block_row_mt(TileWorkerData * twd,MODE_INFO * const mi,int plane,int row,int col,TX_SIZE tx_size)358 static void predict_and_reconstruct_intra_block_row_mt(TileWorkerData *twd,
359 MODE_INFO *const mi,
360 int plane, int row,
361 int col,
362 TX_SIZE tx_size) {
363 MACROBLOCKD *const xd = &twd->xd;
364 struct macroblockd_plane *const pd = &xd->plane[plane];
365 PREDICTION_MODE mode = (plane == 0) ? mi->mode : mi->uv_mode;
366 uint8_t *dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
367
368 if (mi->sb_type < BLOCK_8X8)
369 if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
370
371 vp9_predict_intra_block(xd, pd->n4_wl, tx_size, mode, dst, pd->dst.stride,
372 dst, pd->dst.stride, col, row, plane);
373
374 if (!mi->skip) {
375 const TX_TYPE tx_type =
376 (plane || xd->lossless) ? DCT_DCT : intra_mode_to_tx_type_lookup[mode];
377 if (*pd->eob > 0) {
378 inverse_transform_block_intra(xd, plane, tx_type, tx_size, dst,
379 pd->dst.stride, *pd->eob);
380 }
381 /* Keep the alignment to 16 */
382 pd->dqcoeff += (16 << (tx_size << 1));
383 pd->eob++;
384 }
385 }
386
reconstruct_inter_block(TileWorkerData * twd,MODE_INFO * const mi,int plane,int row,int col,TX_SIZE tx_size)387 static int reconstruct_inter_block(TileWorkerData *twd, MODE_INFO *const mi,
388 int plane, int row, int col,
389 TX_SIZE tx_size) {
390 MACROBLOCKD *const xd = &twd->xd;
391 struct macroblockd_plane *const pd = &xd->plane[plane];
392 const scan_order *sc = &vp9_default_scan_orders[tx_size];
393 const int eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size,
394 mi->segment_id);
395
396 if (eob > 0) {
397 inverse_transform_block_inter(
398 xd, plane, tx_size, &pd->dst.buf[4 * row * pd->dst.stride + 4 * col],
399 pd->dst.stride, eob);
400 }
401 return eob;
402 }
403
parse_inter_block_row_mt(TileWorkerData * twd,MODE_INFO * const mi,int plane,int row,int col,TX_SIZE tx_size)404 static int parse_inter_block_row_mt(TileWorkerData *twd, MODE_INFO *const mi,
405 int plane, int row, int col,
406 TX_SIZE tx_size) {
407 MACROBLOCKD *const xd = &twd->xd;
408 struct macroblockd_plane *const pd = &xd->plane[plane];
409 const scan_order *sc = &vp9_default_scan_orders[tx_size];
410 const int eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size,
411 mi->segment_id);
412
413 *pd->eob = eob;
414 pd->dqcoeff += (16 << (tx_size << 1));
415 pd->eob++;
416
417 return eob;
418 }
419
reconstruct_inter_block_row_mt(TileWorkerData * twd,MODE_INFO * const mi,int plane,int row,int col,TX_SIZE tx_size)420 static int reconstruct_inter_block_row_mt(TileWorkerData *twd,
421 MODE_INFO *const mi, int plane,
422 int row, int col, TX_SIZE tx_size) {
423 MACROBLOCKD *const xd = &twd->xd;
424 struct macroblockd_plane *const pd = &xd->plane[plane];
425 const int eob = *pd->eob;
426
427 (void)mi;
428 if (eob > 0) {
429 inverse_transform_block_inter(
430 xd, plane, tx_size, &pd->dst.buf[4 * row * pd->dst.stride + 4 * col],
431 pd->dst.stride, eob);
432 }
433 pd->dqcoeff += (16 << (tx_size << 1));
434 pd->eob++;
435
436 return eob;
437 }
438
build_mc_border(const uint8_t * src,int src_stride,uint8_t * dst,int dst_stride,int x,int y,int b_w,int b_h,int w,int h)439 static void build_mc_border(const uint8_t *src, int src_stride, uint8_t *dst,
440 int dst_stride, int x, int y, int b_w, int b_h,
441 int w, int h) {
442 // Get a pointer to the start of the real data for this row.
443 const uint8_t *ref_row = src - x - y * src_stride;
444
445 if (y >= h)
446 ref_row += (h - 1) * src_stride;
447 else if (y > 0)
448 ref_row += y * src_stride;
449
450 do {
451 int right = 0, copy;
452 int left = x < 0 ? -x : 0;
453
454 if (left > b_w) left = b_w;
455
456 if (x + b_w > w) right = x + b_w - w;
457
458 if (right > b_w) right = b_w;
459
460 copy = b_w - left - right;
461
462 if (left) memset(dst, ref_row[0], left);
463
464 if (copy) memcpy(dst + left, ref_row + x + left, copy);
465
466 if (right) memset(dst + left + copy, ref_row[w - 1], right);
467
468 dst += dst_stride;
469 ++y;
470
471 if (y > 0 && y < h) ref_row += src_stride;
472 } while (--b_h);
473 }
474
475 #if CONFIG_VP9_HIGHBITDEPTH
high_build_mc_border(const uint8_t * src8,int src_stride,uint16_t * dst,int dst_stride,int x,int y,int b_w,int b_h,int w,int h)476 static void high_build_mc_border(const uint8_t *src8, int src_stride,
477 uint16_t *dst, int dst_stride, int x, int y,
478 int b_w, int b_h, int w, int h) {
479 // Get a pointer to the start of the real data for this row.
480 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
481 const uint16_t *ref_row = src - x - y * src_stride;
482
483 if (y >= h)
484 ref_row += (h - 1) * src_stride;
485 else if (y > 0)
486 ref_row += y * src_stride;
487
488 do {
489 int right = 0, copy;
490 int left = x < 0 ? -x : 0;
491
492 if (left > b_w) left = b_w;
493
494 if (x + b_w > w) right = x + b_w - w;
495
496 if (right > b_w) right = b_w;
497
498 copy = b_w - left - right;
499
500 if (left) vpx_memset16(dst, ref_row[0], left);
501
502 if (copy) memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t));
503
504 if (right) vpx_memset16(dst + left + copy, ref_row[w - 1], right);
505
506 dst += dst_stride;
507 ++y;
508
509 if (y > 0 && y < h) ref_row += src_stride;
510 } while (--b_h);
511 }
512 #endif // CONFIG_VP9_HIGHBITDEPTH
513
514 #if CONFIG_VP9_HIGHBITDEPTH
extend_and_predict(const uint8_t * buf_ptr1,int pre_buf_stride,int x0,int y0,int b_w,int b_h,int frame_width,int frame_height,int border_offset,uint8_t * const dst,int dst_buf_stride,int subpel_x,int subpel_y,const InterpKernel * kernel,const struct scale_factors * sf,MACROBLOCKD * xd,int w,int h,int ref,int xs,int ys)515 static void extend_and_predict(const uint8_t *buf_ptr1, int pre_buf_stride,
516 int x0, int y0, int b_w, int b_h,
517 int frame_width, int frame_height,
518 int border_offset, uint8_t *const dst,
519 int dst_buf_stride, int subpel_x, int subpel_y,
520 const InterpKernel *kernel,
521 const struct scale_factors *sf, MACROBLOCKD *xd,
522 int w, int h, int ref, int xs, int ys) {
523 DECLARE_ALIGNED(16, uint16_t, mc_buf_high[80 * 2 * 80 * 2]);
524
525 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
526 high_build_mc_border(buf_ptr1, pre_buf_stride, mc_buf_high, b_w, x0, y0,
527 b_w, b_h, frame_width, frame_height);
528 highbd_inter_predictor(mc_buf_high + border_offset, b_w,
529 CONVERT_TO_SHORTPTR(dst), dst_buf_stride, subpel_x,
530 subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
531 } else {
532 build_mc_border(buf_ptr1, pre_buf_stride, (uint8_t *)mc_buf_high, b_w, x0,
533 y0, b_w, b_h, frame_width, frame_height);
534 inter_predictor(((uint8_t *)mc_buf_high) + border_offset, b_w, dst,
535 dst_buf_stride, subpel_x, subpel_y, sf, w, h, ref, kernel,
536 xs, ys);
537 }
538 }
539 #else
extend_and_predict(const uint8_t * buf_ptr1,int pre_buf_stride,int x0,int y0,int b_w,int b_h,int frame_width,int frame_height,int border_offset,uint8_t * const dst,int dst_buf_stride,int subpel_x,int subpel_y,const InterpKernel * kernel,const struct scale_factors * sf,int w,int h,int ref,int xs,int ys)540 static void extend_and_predict(const uint8_t *buf_ptr1, int pre_buf_stride,
541 int x0, int y0, int b_w, int b_h,
542 int frame_width, int frame_height,
543 int border_offset, uint8_t *const dst,
544 int dst_buf_stride, int subpel_x, int subpel_y,
545 const InterpKernel *kernel,
546 const struct scale_factors *sf, int w, int h,
547 int ref, int xs, int ys) {
548 DECLARE_ALIGNED(16, uint8_t, mc_buf[80 * 2 * 80 * 2]);
549 const uint8_t *buf_ptr;
550
551 build_mc_border(buf_ptr1, pre_buf_stride, mc_buf, b_w, x0, y0, b_w, b_h,
552 frame_width, frame_height);
553 buf_ptr = mc_buf + border_offset;
554
555 inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x, subpel_y, sf, w,
556 h, ref, kernel, xs, ys);
557 }
558 #endif // CONFIG_VP9_HIGHBITDEPTH
559
dec_build_inter_predictors(MACROBLOCKD * xd,int plane,int bw,int bh,int x,int y,int w,int h,int mi_x,int mi_y,const InterpKernel * kernel,const struct scale_factors * sf,struct buf_2d * pre_buf,struct buf_2d * dst_buf,const MV * mv,RefCntBuffer * ref_frame_buf,int is_scaled,int ref)560 static void dec_build_inter_predictors(
561 MACROBLOCKD *xd, int plane, int bw, int bh, int x, int y, int w, int h,
562 int mi_x, int mi_y, const InterpKernel *kernel,
563 const struct scale_factors *sf, struct buf_2d *pre_buf,
564 struct buf_2d *dst_buf, const MV *mv, RefCntBuffer *ref_frame_buf,
565 int is_scaled, int ref) {
566 struct macroblockd_plane *const pd = &xd->plane[plane];
567 uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
568 MV32 scaled_mv;
569 int xs, ys, x0, y0, x0_16, y0_16, frame_width, frame_height, buf_stride,
570 subpel_x, subpel_y;
571 uint8_t *ref_frame, *buf_ptr;
572
573 // Get reference frame pointer, width and height.
574 if (plane == 0) {
575 frame_width = ref_frame_buf->buf.y_crop_width;
576 frame_height = ref_frame_buf->buf.y_crop_height;
577 ref_frame = ref_frame_buf->buf.y_buffer;
578 } else {
579 frame_width = ref_frame_buf->buf.uv_crop_width;
580 frame_height = ref_frame_buf->buf.uv_crop_height;
581 ref_frame =
582 plane == 1 ? ref_frame_buf->buf.u_buffer : ref_frame_buf->buf.v_buffer;
583 }
584
585 if (is_scaled) {
586 const MV mv_q4 = clamp_mv_to_umv_border_sb(
587 xd, mv, bw, bh, pd->subsampling_x, pd->subsampling_y);
588 // Co-ordinate of containing block to pixel precision.
589 int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
590 int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y));
591 #if 0 // CONFIG_BETTER_HW_COMPATIBILITY
592 assert(xd->mi[0]->sb_type != BLOCK_4X8 &&
593 xd->mi[0]->sb_type != BLOCK_8X4);
594 assert(mv_q4.row == mv->row * (1 << (1 - pd->subsampling_y)) &&
595 mv_q4.col == mv->col * (1 << (1 - pd->subsampling_x)));
596 #endif
597 // Co-ordinate of the block to 1/16th pixel precision.
598 x0_16 = (x_start + x) << SUBPEL_BITS;
599 y0_16 = (y_start + y) << SUBPEL_BITS;
600
601 // Co-ordinate of current block in reference frame
602 // to 1/16th pixel precision.
603 x0_16 = sf->scale_value_x(x0_16, sf);
604 y0_16 = sf->scale_value_y(y0_16, sf);
605
606 // Map the top left corner of the block into the reference frame.
607 x0 = sf->scale_value_x(x_start + x, sf);
608 y0 = sf->scale_value_y(y_start + y, sf);
609
610 // Scale the MV and incorporate the sub-pixel offset of the block
611 // in the reference frame.
612 scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
613 xs = sf->x_step_q4;
614 ys = sf->y_step_q4;
615 } else {
616 // Co-ordinate of containing block to pixel precision.
617 x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
618 y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
619
620 // Co-ordinate of the block to 1/16th pixel precision.
621 x0_16 = x0 << SUBPEL_BITS;
622 y0_16 = y0 << SUBPEL_BITS;
623
624 scaled_mv.row = mv->row * (1 << (1 - pd->subsampling_y));
625 scaled_mv.col = mv->col * (1 << (1 - pd->subsampling_x));
626 xs = ys = 16;
627 }
628 subpel_x = scaled_mv.col & SUBPEL_MASK;
629 subpel_y = scaled_mv.row & SUBPEL_MASK;
630
631 // Calculate the top left corner of the best matching block in the
632 // reference frame.
633 x0 += scaled_mv.col >> SUBPEL_BITS;
634 y0 += scaled_mv.row >> SUBPEL_BITS;
635 x0_16 += scaled_mv.col;
636 y0_16 += scaled_mv.row;
637
638 // Get reference block pointer.
639 buf_ptr = ref_frame + y0 * pre_buf->stride + x0;
640 buf_stride = pre_buf->stride;
641
642 // Do border extension if there is motion or the
643 // width/height is not a multiple of 8 pixels.
644 if (is_scaled || scaled_mv.col || scaled_mv.row || (frame_width & 0x7) ||
645 (frame_height & 0x7)) {
646 int y1 = ((y0_16 + (h - 1) * ys) >> SUBPEL_BITS) + 1;
647
648 // Get reference block bottom right horizontal coordinate.
649 int x1 = ((x0_16 + (w - 1) * xs) >> SUBPEL_BITS) + 1;
650 int x_pad = 0, y_pad = 0;
651
652 if (subpel_x || (sf->x_step_q4 != SUBPEL_SHIFTS)) {
653 x0 -= VP9_INTERP_EXTEND - 1;
654 x1 += VP9_INTERP_EXTEND;
655 x_pad = 1;
656 }
657
658 if (subpel_y || (sf->y_step_q4 != SUBPEL_SHIFTS)) {
659 y0 -= VP9_INTERP_EXTEND - 1;
660 y1 += VP9_INTERP_EXTEND;
661 y_pad = 1;
662 }
663
664 // Skip border extension if block is inside the frame.
665 if (x0 < 0 || x0 > frame_width - 1 || x1 < 0 || x1 > frame_width - 1 ||
666 y0 < 0 || y0 > frame_height - 1 || y1 < 0 || y1 > frame_height - 1) {
667 // Extend the border.
668 const uint8_t *const buf_ptr1 = ref_frame + y0 * buf_stride + x0;
669 const int b_w = x1 - x0 + 1;
670 const int b_h = y1 - y0 + 1;
671 const int border_offset = y_pad * 3 * b_w + x_pad * 3;
672
673 extend_and_predict(buf_ptr1, buf_stride, x0, y0, b_w, b_h, frame_width,
674 frame_height, border_offset, dst, dst_buf->stride,
675 subpel_x, subpel_y, kernel, sf,
676 #if CONFIG_VP9_HIGHBITDEPTH
677 xd,
678 #endif
679 w, h, ref, xs, ys);
680 return;
681 }
682 }
683 #if CONFIG_VP9_HIGHBITDEPTH
684 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
685 highbd_inter_predictor(CONVERT_TO_SHORTPTR(buf_ptr), buf_stride,
686 CONVERT_TO_SHORTPTR(dst), dst_buf->stride, subpel_x,
687 subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
688 } else {
689 inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
690 subpel_y, sf, w, h, ref, kernel, xs, ys);
691 }
692 #else
693 inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x, subpel_y,
694 sf, w, h, ref, kernel, xs, ys);
695 #endif // CONFIG_VP9_HIGHBITDEPTH
696 }
697
dec_build_inter_predictors_sb(VP9Decoder * const pbi,MACROBLOCKD * xd,int mi_row,int mi_col)698 static void dec_build_inter_predictors_sb(VP9Decoder *const pbi,
699 MACROBLOCKD *xd, int mi_row,
700 int mi_col) {
701 int plane;
702 const int mi_x = mi_col * MI_SIZE;
703 const int mi_y = mi_row * MI_SIZE;
704 const MODE_INFO *mi = xd->mi[0];
705 const InterpKernel *kernel = vp9_filter_kernels[mi->interp_filter];
706 const BLOCK_SIZE sb_type = mi->sb_type;
707 const int is_compound = has_second_ref(mi);
708 int ref;
709 int is_scaled;
710
711 for (ref = 0; ref < 1 + is_compound; ++ref) {
712 const MV_REFERENCE_FRAME frame = mi->ref_frame[ref];
713 RefBuffer *ref_buf = &pbi->common.frame_refs[frame - LAST_FRAME];
714 const struct scale_factors *const sf = &ref_buf->sf;
715 const int idx = ref_buf->idx;
716 BufferPool *const pool = pbi->common.buffer_pool;
717 RefCntBuffer *const ref_frame_buf = &pool->frame_bufs[idx];
718
719 if (!vp9_is_valid_scale(sf))
720 vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
721 "Reference frame has invalid dimensions");
722
723 is_scaled = vp9_is_scaled(sf);
724 vp9_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
725 is_scaled ? sf : NULL);
726 xd->block_refs[ref] = ref_buf;
727
728 if (sb_type < BLOCK_8X8) {
729 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
730 struct macroblockd_plane *const pd = &xd->plane[plane];
731 struct buf_2d *const dst_buf = &pd->dst;
732 const int num_4x4_w = pd->n4_w;
733 const int num_4x4_h = pd->n4_h;
734 const int n4w_x4 = 4 * num_4x4_w;
735 const int n4h_x4 = 4 * num_4x4_h;
736 struct buf_2d *const pre_buf = &pd->pre[ref];
737 int i = 0, x, y;
738 for (y = 0; y < num_4x4_h; ++y) {
739 for (x = 0; x < num_4x4_w; ++x) {
740 const MV mv = average_split_mvs(pd, mi, ref, i++);
741 dec_build_inter_predictors(xd, plane, n4w_x4, n4h_x4, 4 * x, 4 * y,
742 4, 4, mi_x, mi_y, kernel, sf, pre_buf,
743 dst_buf, &mv, ref_frame_buf, is_scaled,
744 ref);
745 }
746 }
747 }
748 } else {
749 const MV mv = mi->mv[ref].as_mv;
750 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
751 struct macroblockd_plane *const pd = &xd->plane[plane];
752 struct buf_2d *const dst_buf = &pd->dst;
753 const int num_4x4_w = pd->n4_w;
754 const int num_4x4_h = pd->n4_h;
755 const int n4w_x4 = 4 * num_4x4_w;
756 const int n4h_x4 = 4 * num_4x4_h;
757 struct buf_2d *const pre_buf = &pd->pre[ref];
758 dec_build_inter_predictors(xd, plane, n4w_x4, n4h_x4, 0, 0, n4w_x4,
759 n4h_x4, mi_x, mi_y, kernel, sf, pre_buf,
760 dst_buf, &mv, ref_frame_buf, is_scaled, ref);
761 }
762 }
763 }
764 }
765
dec_reset_skip_context(MACROBLOCKD * xd)766 static INLINE void dec_reset_skip_context(MACROBLOCKD *xd) {
767 int i;
768 for (i = 0; i < MAX_MB_PLANE; i++) {
769 struct macroblockd_plane *const pd = &xd->plane[i];
770 memset(pd->above_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_w);
771 memset(pd->left_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_h);
772 }
773 }
774
set_plane_n4(MACROBLOCKD * const xd,int bw,int bh,int bwl,int bhl)775 static void set_plane_n4(MACROBLOCKD *const xd, int bw, int bh, int bwl,
776 int bhl) {
777 int i;
778 for (i = 0; i < MAX_MB_PLANE; i++) {
779 xd->plane[i].n4_w = (bw << 1) >> xd->plane[i].subsampling_x;
780 xd->plane[i].n4_h = (bh << 1) >> xd->plane[i].subsampling_y;
781 xd->plane[i].n4_wl = bwl - xd->plane[i].subsampling_x;
782 xd->plane[i].n4_hl = bhl - xd->plane[i].subsampling_y;
783 }
784 }
785
set_offsets_recon(VP9_COMMON * const cm,MACROBLOCKD * const xd,int mi_row,int mi_col,int bw,int bh,int bwl,int bhl)786 static MODE_INFO *set_offsets_recon(VP9_COMMON *const cm, MACROBLOCKD *const xd,
787 int mi_row, int mi_col, int bw, int bh,
788 int bwl, int bhl) {
789 const int offset = mi_row * cm->mi_stride + mi_col;
790 const TileInfo *const tile = &xd->tile;
791 xd->mi = cm->mi_grid_visible + offset;
792
793 set_plane_n4(xd, bw, bh, bwl, bhl);
794
795 set_skip_context(xd, mi_row, mi_col);
796
797 // Distance of Mb to the various image edges. These are specified to 8th pel
798 // as they are always compared to values that are in 1/8th pel units
799 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
800
801 vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
802 return xd->mi[0];
803 }
804
set_offsets(VP9_COMMON * const cm,MACROBLOCKD * const xd,BLOCK_SIZE bsize,int mi_row,int mi_col,int bw,int bh,int x_mis,int y_mis,int bwl,int bhl)805 static MODE_INFO *set_offsets(VP9_COMMON *const cm, MACROBLOCKD *const xd,
806 BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
807 int bh, int x_mis, int y_mis, int bwl, int bhl) {
808 const int offset = mi_row * cm->mi_stride + mi_col;
809 int x, y;
810 const TileInfo *const tile = &xd->tile;
811
812 xd->mi = cm->mi_grid_visible + offset;
813 xd->mi[0] = &cm->mi[offset];
814 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
815 // passing bsize from decode_partition().
816 xd->mi[0]->sb_type = bsize;
817 for (y = 0; y < y_mis; ++y)
818 for (x = !y; x < x_mis; ++x) {
819 xd->mi[y * cm->mi_stride + x] = xd->mi[0];
820 }
821
822 set_plane_n4(xd, bw, bh, bwl, bhl);
823
824 set_skip_context(xd, mi_row, mi_col);
825
826 // Distance of Mb to the various image edges. These are specified to 8th pel
827 // as they are always compared to values that are in 1/8th pel units
828 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
829
830 vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
831 return xd->mi[0];
832 }
833
predict_recon_inter(MACROBLOCKD * xd,MODE_INFO * mi,TileWorkerData * twd,predict_recon_func func)834 static INLINE int predict_recon_inter(MACROBLOCKD *xd, MODE_INFO *mi,
835 TileWorkerData *twd,
836 predict_recon_func func) {
837 int eobtotal = 0;
838 int plane;
839 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
840 const struct macroblockd_plane *const pd = &xd->plane[plane];
841 const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
842 const int num_4x4_w = pd->n4_w;
843 const int num_4x4_h = pd->n4_h;
844 const int step = (1 << tx_size);
845 int row, col;
846 const int max_blocks_wide =
847 num_4x4_w + (xd->mb_to_right_edge >= 0
848 ? 0
849 : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
850 const int max_blocks_high =
851 num_4x4_h + (xd->mb_to_bottom_edge >= 0
852 ? 0
853 : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
854
855 xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
856 xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
857
858 for (row = 0; row < max_blocks_high; row += step)
859 for (col = 0; col < max_blocks_wide; col += step)
860 eobtotal += func(twd, mi, plane, row, col, tx_size);
861 }
862 return eobtotal;
863 }
864
predict_recon_intra(MACROBLOCKD * xd,MODE_INFO * mi,TileWorkerData * twd,intra_recon_func func)865 static INLINE void predict_recon_intra(MACROBLOCKD *xd, MODE_INFO *mi,
866 TileWorkerData *twd,
867 intra_recon_func func) {
868 int plane;
869 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
870 const struct macroblockd_plane *const pd = &xd->plane[plane];
871 const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
872 const int num_4x4_w = pd->n4_w;
873 const int num_4x4_h = pd->n4_h;
874 const int step = (1 << tx_size);
875 int row, col;
876 const int max_blocks_wide =
877 num_4x4_w + (xd->mb_to_right_edge >= 0
878 ? 0
879 : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
880 const int max_blocks_high =
881 num_4x4_h + (xd->mb_to_bottom_edge >= 0
882 ? 0
883 : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
884
885 xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
886 xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
887
888 for (row = 0; row < max_blocks_high; row += step)
889 for (col = 0; col < max_blocks_wide; col += step)
890 func(twd, mi, plane, row, col, tx_size);
891 }
892 }
893
decode_block(TileWorkerData * twd,VP9Decoder * const pbi,int mi_row,int mi_col,BLOCK_SIZE bsize,int bwl,int bhl)894 static void decode_block(TileWorkerData *twd, VP9Decoder *const pbi, int mi_row,
895 int mi_col, BLOCK_SIZE bsize, int bwl, int bhl) {
896 VP9_COMMON *const cm = &pbi->common;
897 const int less8x8 = bsize < BLOCK_8X8;
898 const int bw = 1 << (bwl - 1);
899 const int bh = 1 << (bhl - 1);
900 const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
901 const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
902 vpx_reader *r = &twd->bit_reader;
903 MACROBLOCKD *const xd = &twd->xd;
904
905 MODE_INFO *mi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis,
906 y_mis, bwl, bhl);
907
908 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
909 const BLOCK_SIZE uv_subsize =
910 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
911 if (uv_subsize == BLOCK_INVALID)
912 vpx_internal_error(xd->error_info, VPX_CODEC_CORRUPT_FRAME,
913 "Invalid block size.");
914 }
915
916 vp9_read_mode_info(twd, pbi, mi_row, mi_col, x_mis, y_mis);
917
918 if (mi->skip) {
919 dec_reset_skip_context(xd);
920 }
921
922 if (!is_inter_block(mi)) {
923 int plane;
924 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
925 const struct macroblockd_plane *const pd = &xd->plane[plane];
926 const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
927 const int num_4x4_w = pd->n4_w;
928 const int num_4x4_h = pd->n4_h;
929 const int step = (1 << tx_size);
930 int row, col;
931 const int max_blocks_wide =
932 num_4x4_w + (xd->mb_to_right_edge >= 0
933 ? 0
934 : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
935 const int max_blocks_high =
936 num_4x4_h + (xd->mb_to_bottom_edge >= 0
937 ? 0
938 : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
939
940 xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
941 xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
942
943 for (row = 0; row < max_blocks_high; row += step)
944 for (col = 0; col < max_blocks_wide; col += step)
945 predict_and_reconstruct_intra_block(twd, mi, plane, row, col,
946 tx_size);
947 }
948 } else {
949 // Prediction
950 dec_build_inter_predictors_sb(pbi, xd, mi_row, mi_col);
951
952 // Reconstruction
953 if (!mi->skip) {
954 int eobtotal = 0;
955 int plane;
956
957 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
958 const struct macroblockd_plane *const pd = &xd->plane[plane];
959 const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
960 const int num_4x4_w = pd->n4_w;
961 const int num_4x4_h = pd->n4_h;
962 const int step = (1 << tx_size);
963 int row, col;
964 const int max_blocks_wide =
965 num_4x4_w + (xd->mb_to_right_edge >= 0
966 ? 0
967 : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
968 const int max_blocks_high =
969 num_4x4_h +
970 (xd->mb_to_bottom_edge >= 0
971 ? 0
972 : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
973
974 xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
975 xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
976
977 for (row = 0; row < max_blocks_high; row += step)
978 for (col = 0; col < max_blocks_wide; col += step)
979 eobtotal +=
980 reconstruct_inter_block(twd, mi, plane, row, col, tx_size);
981 }
982
983 if (!less8x8 && eobtotal == 0) mi->skip = 1; // skip loopfilter
984 }
985 }
986
987 xd->corrupted |= vpx_reader_has_error(r);
988
989 if (cm->lf.filter_level) {
990 vp9_build_mask(cm, mi, mi_row, mi_col, bw, bh);
991 }
992 }
993
recon_block(TileWorkerData * twd,VP9Decoder * const pbi,int mi_row,int mi_col,BLOCK_SIZE bsize,int bwl,int bhl)994 static void recon_block(TileWorkerData *twd, VP9Decoder *const pbi, int mi_row,
995 int mi_col, BLOCK_SIZE bsize, int bwl, int bhl) {
996 VP9_COMMON *const cm = &pbi->common;
997 const int bw = 1 << (bwl - 1);
998 const int bh = 1 << (bhl - 1);
999 MACROBLOCKD *const xd = &twd->xd;
1000
1001 MODE_INFO *mi = set_offsets_recon(cm, xd, mi_row, mi_col, bw, bh, bwl, bhl);
1002
1003 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1004 const BLOCK_SIZE uv_subsize =
1005 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1006 if (uv_subsize == BLOCK_INVALID)
1007 vpx_internal_error(xd->error_info, VPX_CODEC_CORRUPT_FRAME,
1008 "Invalid block size.");
1009 }
1010
1011 if (!is_inter_block(mi)) {
1012 predict_recon_intra(xd, mi, twd,
1013 predict_and_reconstruct_intra_block_row_mt);
1014 } else {
1015 // Prediction
1016 dec_build_inter_predictors_sb(pbi, xd, mi_row, mi_col);
1017
1018 // Reconstruction
1019 if (!mi->skip) {
1020 predict_recon_inter(xd, mi, twd, reconstruct_inter_block_row_mt);
1021 }
1022 }
1023
1024 vp9_build_mask(cm, mi, mi_row, mi_col, bw, bh);
1025 }
1026
parse_block(TileWorkerData * twd,VP9Decoder * const pbi,int mi_row,int mi_col,BLOCK_SIZE bsize,int bwl,int bhl)1027 static void parse_block(TileWorkerData *twd, VP9Decoder *const pbi, int mi_row,
1028 int mi_col, BLOCK_SIZE bsize, int bwl, int bhl) {
1029 VP9_COMMON *const cm = &pbi->common;
1030 const int less8x8 = bsize < BLOCK_8X8;
1031 const int bw = 1 << (bwl - 1);
1032 const int bh = 1 << (bhl - 1);
1033 const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
1034 const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
1035 vpx_reader *r = &twd->bit_reader;
1036 MACROBLOCKD *const xd = &twd->xd;
1037
1038 MODE_INFO *mi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis,
1039 y_mis, bwl, bhl);
1040
1041 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1042 const BLOCK_SIZE uv_subsize =
1043 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1044 if (uv_subsize == BLOCK_INVALID)
1045 vpx_internal_error(xd->error_info, VPX_CODEC_CORRUPT_FRAME,
1046 "Invalid block size.");
1047 }
1048
1049 vp9_read_mode_info(twd, pbi, mi_row, mi_col, x_mis, y_mis);
1050
1051 if (mi->skip) {
1052 dec_reset_skip_context(xd);
1053 }
1054
1055 if (!is_inter_block(mi)) {
1056 predict_recon_intra(xd, mi, twd, parse_intra_block_row_mt);
1057 } else {
1058 if (!mi->skip) {
1059 const int eobtotal =
1060 predict_recon_inter(xd, mi, twd, parse_inter_block_row_mt);
1061
1062 if (!less8x8 && eobtotal == 0) mi->skip = 1; // skip loopfilter
1063 }
1064 }
1065
1066 xd->corrupted |= vpx_reader_has_error(r);
1067 }
1068
dec_partition_plane_context(TileWorkerData * twd,int mi_row,int mi_col,int bsl)1069 static INLINE int dec_partition_plane_context(TileWorkerData *twd, int mi_row,
1070 int mi_col, int bsl) {
1071 const PARTITION_CONTEXT *above_ctx = twd->xd.above_seg_context + mi_col;
1072 const PARTITION_CONTEXT *left_ctx =
1073 twd->xd.left_seg_context + (mi_row & MI_MASK);
1074 int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1;
1075
1076 // assert(bsl >= 0);
1077
1078 return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
1079 }
1080
dec_update_partition_context(TileWorkerData * twd,int mi_row,int mi_col,BLOCK_SIZE subsize,int bw)1081 static INLINE void dec_update_partition_context(TileWorkerData *twd, int mi_row,
1082 int mi_col, BLOCK_SIZE subsize,
1083 int bw) {
1084 PARTITION_CONTEXT *const above_ctx = twd->xd.above_seg_context + mi_col;
1085 PARTITION_CONTEXT *const left_ctx =
1086 twd->xd.left_seg_context + (mi_row & MI_MASK);
1087
1088 // update the partition context at the end notes. set partition bits
1089 // of block sizes larger than the current one to be one, and partition
1090 // bits of smaller block sizes to be zero.
1091 memset(above_ctx, partition_context_lookup[subsize].above, bw);
1092 memset(left_ctx, partition_context_lookup[subsize].left, bw);
1093 }
1094
read_partition(TileWorkerData * twd,int mi_row,int mi_col,int has_rows,int has_cols,int bsl)1095 static PARTITION_TYPE read_partition(TileWorkerData *twd, int mi_row,
1096 int mi_col, int has_rows, int has_cols,
1097 int bsl) {
1098 const int ctx = dec_partition_plane_context(twd, mi_row, mi_col, bsl);
1099 const vpx_prob *const probs = twd->xd.partition_probs[ctx];
1100 FRAME_COUNTS *counts = twd->xd.counts;
1101 PARTITION_TYPE p;
1102 vpx_reader *r = &twd->bit_reader;
1103
1104 if (has_rows && has_cols)
1105 p = (PARTITION_TYPE)vpx_read_tree(r, vp9_partition_tree, probs);
1106 else if (!has_rows && has_cols)
1107 p = vpx_read(r, probs[1]) ? PARTITION_SPLIT : PARTITION_HORZ;
1108 else if (has_rows && !has_cols)
1109 p = vpx_read(r, probs[2]) ? PARTITION_SPLIT : PARTITION_VERT;
1110 else
1111 p = PARTITION_SPLIT;
1112
1113 if (counts) ++counts->partition[ctx][p];
1114
1115 return p;
1116 }
1117
1118 // TODO(slavarnway): eliminate bsize and subsize in future commits
decode_partition(TileWorkerData * twd,VP9Decoder * const pbi,int mi_row,int mi_col,BLOCK_SIZE bsize,int n4x4_l2)1119 static void decode_partition(TileWorkerData *twd, VP9Decoder *const pbi,
1120 int mi_row, int mi_col, BLOCK_SIZE bsize,
1121 int n4x4_l2) {
1122 VP9_COMMON *const cm = &pbi->common;
1123 const int n8x8_l2 = n4x4_l2 - 1;
1124 const int num_8x8_wh = 1 << n8x8_l2;
1125 const int hbs = num_8x8_wh >> 1;
1126 PARTITION_TYPE partition;
1127 BLOCK_SIZE subsize;
1128 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1129 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1130 MACROBLOCKD *const xd = &twd->xd;
1131
1132 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1133
1134 partition = read_partition(twd, mi_row, mi_col, has_rows, has_cols, n8x8_l2);
1135 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
1136 if (!hbs) {
1137 // calculate bmode block dimensions (log 2)
1138 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1139 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1140 decode_block(twd, pbi, mi_row, mi_col, subsize, 1, 1);
1141 } else {
1142 switch (partition) {
1143 case PARTITION_NONE:
1144 decode_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n4x4_l2);
1145 break;
1146 case PARTITION_HORZ:
1147 decode_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n8x8_l2);
1148 if (has_rows)
1149 decode_block(twd, pbi, mi_row + hbs, mi_col, subsize, n4x4_l2,
1150 n8x8_l2);
1151 break;
1152 case PARTITION_VERT:
1153 decode_block(twd, pbi, mi_row, mi_col, subsize, n8x8_l2, n4x4_l2);
1154 if (has_cols)
1155 decode_block(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2,
1156 n4x4_l2);
1157 break;
1158 case PARTITION_SPLIT:
1159 decode_partition(twd, pbi, mi_row, mi_col, subsize, n8x8_l2);
1160 decode_partition(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2);
1161 decode_partition(twd, pbi, mi_row + hbs, mi_col, subsize, n8x8_l2);
1162 decode_partition(twd, pbi, mi_row + hbs, mi_col + hbs, subsize,
1163 n8x8_l2);
1164 break;
1165 default: assert(0 && "Invalid partition type");
1166 }
1167 }
1168
1169 // update partition context
1170 if (bsize >= BLOCK_8X8 &&
1171 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
1172 dec_update_partition_context(twd, mi_row, mi_col, subsize, num_8x8_wh);
1173 }
1174
recon_partition(TileWorkerData * twd,VP9Decoder * const pbi,int mi_row,int mi_col,BLOCK_SIZE bsize,int n4x4_l2)1175 static void recon_partition(TileWorkerData *twd, VP9Decoder *const pbi,
1176 int mi_row, int mi_col, BLOCK_SIZE bsize,
1177 int n4x4_l2) {
1178 VP9_COMMON *const cm = &pbi->common;
1179 const int n8x8_l2 = n4x4_l2 - 1;
1180 const int num_8x8_wh = 1 << n8x8_l2;
1181 const int hbs = num_8x8_wh >> 1;
1182 PARTITION_TYPE partition;
1183 BLOCK_SIZE subsize;
1184 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1185 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1186 MACROBLOCKD *const xd = &twd->xd;
1187
1188 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1189
1190 partition = *xd->partition;
1191 xd->partition++;
1192
1193 subsize = get_subsize(bsize, partition);
1194 if (!hbs) {
1195 // calculate bmode block dimensions (log 2)
1196 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1197 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1198 recon_block(twd, pbi, mi_row, mi_col, subsize, 1, 1);
1199 } else {
1200 switch (partition) {
1201 case PARTITION_NONE:
1202 recon_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n4x4_l2);
1203 break;
1204 case PARTITION_HORZ:
1205 recon_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n8x8_l2);
1206 if (has_rows)
1207 recon_block(twd, pbi, mi_row + hbs, mi_col, subsize, n4x4_l2,
1208 n8x8_l2);
1209 break;
1210 case PARTITION_VERT:
1211 recon_block(twd, pbi, mi_row, mi_col, subsize, n8x8_l2, n4x4_l2);
1212 if (has_cols)
1213 recon_block(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2,
1214 n4x4_l2);
1215 break;
1216 case PARTITION_SPLIT:
1217 recon_partition(twd, pbi, mi_row, mi_col, subsize, n8x8_l2);
1218 recon_partition(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2);
1219 recon_partition(twd, pbi, mi_row + hbs, mi_col, subsize, n8x8_l2);
1220 recon_partition(twd, pbi, mi_row + hbs, mi_col + hbs, subsize, n8x8_l2);
1221 break;
1222 default: assert(0 && "Invalid partition type");
1223 }
1224 }
1225 }
1226
parse_partition(TileWorkerData * twd,VP9Decoder * const pbi,int mi_row,int mi_col,BLOCK_SIZE bsize,int n4x4_l2)1227 static void parse_partition(TileWorkerData *twd, VP9Decoder *const pbi,
1228 int mi_row, int mi_col, BLOCK_SIZE bsize,
1229 int n4x4_l2) {
1230 VP9_COMMON *const cm = &pbi->common;
1231 const int n8x8_l2 = n4x4_l2 - 1;
1232 const int num_8x8_wh = 1 << n8x8_l2;
1233 const int hbs = num_8x8_wh >> 1;
1234 PARTITION_TYPE partition;
1235 BLOCK_SIZE subsize;
1236 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1237 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1238 MACROBLOCKD *const xd = &twd->xd;
1239
1240 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1241
1242 *xd->partition =
1243 read_partition(twd, mi_row, mi_col, has_rows, has_cols, n8x8_l2);
1244
1245 partition = *xd->partition;
1246 xd->partition++;
1247
1248 subsize = get_subsize(bsize, partition);
1249 if (!hbs) {
1250 // calculate bmode block dimensions (log 2)
1251 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1252 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1253 parse_block(twd, pbi, mi_row, mi_col, subsize, 1, 1);
1254 } else {
1255 switch (partition) {
1256 case PARTITION_NONE:
1257 parse_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n4x4_l2);
1258 break;
1259 case PARTITION_HORZ:
1260 parse_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n8x8_l2);
1261 if (has_rows)
1262 parse_block(twd, pbi, mi_row + hbs, mi_col, subsize, n4x4_l2,
1263 n8x8_l2);
1264 break;
1265 case PARTITION_VERT:
1266 parse_block(twd, pbi, mi_row, mi_col, subsize, n8x8_l2, n4x4_l2);
1267 if (has_cols)
1268 parse_block(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2,
1269 n4x4_l2);
1270 break;
1271 case PARTITION_SPLIT:
1272 parse_partition(twd, pbi, mi_row, mi_col, subsize, n8x8_l2);
1273 parse_partition(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2);
1274 parse_partition(twd, pbi, mi_row + hbs, mi_col, subsize, n8x8_l2);
1275 parse_partition(twd, pbi, mi_row + hbs, mi_col + hbs, subsize, n8x8_l2);
1276 break;
1277 default: assert(0 && "Invalid partition type");
1278 }
1279 }
1280
1281 // update partition context
1282 if (bsize >= BLOCK_8X8 &&
1283 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
1284 dec_update_partition_context(twd, mi_row, mi_col, subsize, num_8x8_wh);
1285 }
1286
setup_token_decoder(const uint8_t * data,const uint8_t * data_end,size_t read_size,struct vpx_internal_error_info * error_info,vpx_reader * r,vpx_decrypt_cb decrypt_cb,void * decrypt_state)1287 static void setup_token_decoder(const uint8_t *data, const uint8_t *data_end,
1288 size_t read_size,
1289 struct vpx_internal_error_info *error_info,
1290 vpx_reader *r, vpx_decrypt_cb decrypt_cb,
1291 void *decrypt_state) {
1292 // Validate the calculated partition length. If the buffer
1293 // described by the partition can't be fully read, then restrict
1294 // it to the portion that can be (for EC mode) or throw an error.
1295 if (!read_is_valid(data, read_size, data_end))
1296 vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
1297 "Truncated packet or corrupt tile length");
1298
1299 if (vpx_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
1300 vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR,
1301 "Failed to allocate bool decoder %d", 1);
1302 }
1303
read_coef_probs_common(vp9_coeff_probs_model * coef_probs,vpx_reader * r)1304 static void read_coef_probs_common(vp9_coeff_probs_model *coef_probs,
1305 vpx_reader *r) {
1306 int i, j, k, l, m;
1307
1308 if (vpx_read_bit(r))
1309 for (i = 0; i < PLANE_TYPES; ++i)
1310 for (j = 0; j < REF_TYPES; ++j)
1311 for (k = 0; k < COEF_BANDS; ++k)
1312 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
1313 for (m = 0; m < UNCONSTRAINED_NODES; ++m)
1314 vp9_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
1315 }
1316
read_coef_probs(FRAME_CONTEXT * fc,TX_MODE tx_mode,vpx_reader * r)1317 static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, vpx_reader *r) {
1318 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
1319 TX_SIZE tx_size;
1320 for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size)
1321 read_coef_probs_common(fc->coef_probs[tx_size], r);
1322 }
1323
setup_segmentation(struct segmentation * seg,struct vpx_read_bit_buffer * rb)1324 static void setup_segmentation(struct segmentation *seg,
1325 struct vpx_read_bit_buffer *rb) {
1326 int i, j;
1327
1328 seg->update_map = 0;
1329 seg->update_data = 0;
1330
1331 seg->enabled = vpx_rb_read_bit(rb);
1332 if (!seg->enabled) return;
1333
1334 // Segmentation map update
1335 seg->update_map = vpx_rb_read_bit(rb);
1336 if (seg->update_map) {
1337 for (i = 0; i < SEG_TREE_PROBS; i++)
1338 seg->tree_probs[i] =
1339 vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8) : MAX_PROB;
1340
1341 seg->temporal_update = vpx_rb_read_bit(rb);
1342 if (seg->temporal_update) {
1343 for (i = 0; i < PREDICTION_PROBS; i++)
1344 seg->pred_probs[i] =
1345 vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8) : MAX_PROB;
1346 } else {
1347 for (i = 0; i < PREDICTION_PROBS; i++) seg->pred_probs[i] = MAX_PROB;
1348 }
1349 }
1350
1351 // Segmentation data update
1352 seg->update_data = vpx_rb_read_bit(rb);
1353 if (seg->update_data) {
1354 seg->abs_delta = vpx_rb_read_bit(rb);
1355
1356 vp9_clearall_segfeatures(seg);
1357
1358 for (i = 0; i < MAX_SEGMENTS; i++) {
1359 for (j = 0; j < SEG_LVL_MAX; j++) {
1360 int data = 0;
1361 const int feature_enabled = vpx_rb_read_bit(rb);
1362 if (feature_enabled) {
1363 vp9_enable_segfeature(seg, i, j);
1364 data = decode_unsigned_max(rb, vp9_seg_feature_data_max(j));
1365 if (vp9_is_segfeature_signed(j))
1366 data = vpx_rb_read_bit(rb) ? -data : data;
1367 }
1368 vp9_set_segdata(seg, i, j, data);
1369 }
1370 }
1371 }
1372 }
1373
setup_loopfilter(struct loopfilter * lf,struct vpx_read_bit_buffer * rb)1374 static void setup_loopfilter(struct loopfilter *lf,
1375 struct vpx_read_bit_buffer *rb) {
1376 lf->filter_level = vpx_rb_read_literal(rb, 6);
1377 lf->sharpness_level = vpx_rb_read_literal(rb, 3);
1378
1379 // Read in loop filter deltas applied at the MB level based on mode or ref
1380 // frame.
1381 lf->mode_ref_delta_update = 0;
1382
1383 lf->mode_ref_delta_enabled = vpx_rb_read_bit(rb);
1384 if (lf->mode_ref_delta_enabled) {
1385 lf->mode_ref_delta_update = vpx_rb_read_bit(rb);
1386 if (lf->mode_ref_delta_update) {
1387 int i;
1388
1389 for (i = 0; i < MAX_REF_LF_DELTAS; i++)
1390 if (vpx_rb_read_bit(rb))
1391 lf->ref_deltas[i] = vpx_rb_read_signed_literal(rb, 6);
1392
1393 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
1394 if (vpx_rb_read_bit(rb))
1395 lf->mode_deltas[i] = vpx_rb_read_signed_literal(rb, 6);
1396 }
1397 }
1398 }
1399
read_delta_q(struct vpx_read_bit_buffer * rb)1400 static INLINE int read_delta_q(struct vpx_read_bit_buffer *rb) {
1401 return vpx_rb_read_bit(rb) ? vpx_rb_read_signed_literal(rb, 4) : 0;
1402 }
1403
setup_quantization(VP9_COMMON * const cm,MACROBLOCKD * const xd,struct vpx_read_bit_buffer * rb)1404 static void setup_quantization(VP9_COMMON *const cm, MACROBLOCKD *const xd,
1405 struct vpx_read_bit_buffer *rb) {
1406 cm->base_qindex = vpx_rb_read_literal(rb, QINDEX_BITS);
1407 cm->y_dc_delta_q = read_delta_q(rb);
1408 cm->uv_dc_delta_q = read_delta_q(rb);
1409 cm->uv_ac_delta_q = read_delta_q(rb);
1410 cm->dequant_bit_depth = cm->bit_depth;
1411 xd->lossless = cm->base_qindex == 0 && cm->y_dc_delta_q == 0 &&
1412 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
1413
1414 #if CONFIG_VP9_HIGHBITDEPTH
1415 xd->bd = (int)cm->bit_depth;
1416 #endif
1417 }
1418
setup_segmentation_dequant(VP9_COMMON * const cm)1419 static void setup_segmentation_dequant(VP9_COMMON *const cm) {
1420 // Build y/uv dequant values based on segmentation.
1421 if (cm->seg.enabled) {
1422 int i;
1423 for (i = 0; i < MAX_SEGMENTS; ++i) {
1424 const int qindex = vp9_get_qindex(&cm->seg, i, cm->base_qindex);
1425 cm->y_dequant[i][0] =
1426 vp9_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
1427 cm->y_dequant[i][1] = vp9_ac_quant(qindex, 0, cm->bit_depth);
1428 cm->uv_dequant[i][0] =
1429 vp9_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
1430 cm->uv_dequant[i][1] =
1431 vp9_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
1432 }
1433 } else {
1434 const int qindex = cm->base_qindex;
1435 // When segmentation is disabled, only the first value is used. The
1436 // remaining are don't cares.
1437 cm->y_dequant[0][0] = vp9_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
1438 cm->y_dequant[0][1] = vp9_ac_quant(qindex, 0, cm->bit_depth);
1439 cm->uv_dequant[0][0] =
1440 vp9_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
1441 cm->uv_dequant[0][1] =
1442 vp9_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
1443 }
1444 }
1445
read_interp_filter(struct vpx_read_bit_buffer * rb)1446 static INTERP_FILTER read_interp_filter(struct vpx_read_bit_buffer *rb) {
1447 const INTERP_FILTER literal_to_filter[] = { EIGHTTAP_SMOOTH, EIGHTTAP,
1448 EIGHTTAP_SHARP, BILINEAR };
1449 return vpx_rb_read_bit(rb) ? SWITCHABLE
1450 : literal_to_filter[vpx_rb_read_literal(rb, 2)];
1451 }
1452
setup_render_size(VP9_COMMON * cm,struct vpx_read_bit_buffer * rb)1453 static void setup_render_size(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1454 cm->render_width = cm->width;
1455 cm->render_height = cm->height;
1456 if (vpx_rb_read_bit(rb))
1457 vp9_read_frame_size(rb, &cm->render_width, &cm->render_height);
1458 }
1459
resize_mv_buffer(VP9_COMMON * cm)1460 static void resize_mv_buffer(VP9_COMMON *cm) {
1461 vpx_free(cm->cur_frame->mvs);
1462 cm->cur_frame->mi_rows = cm->mi_rows;
1463 cm->cur_frame->mi_cols = cm->mi_cols;
1464 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
1465 (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
1466 sizeof(*cm->cur_frame->mvs)));
1467 }
1468
resize_context_buffers(VP9_COMMON * cm,int width,int height)1469 static void resize_context_buffers(VP9_COMMON *cm, int width, int height) {
1470 #if CONFIG_SIZE_LIMIT
1471 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
1472 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1473 "Dimensions of %dx%d beyond allowed size of %dx%d.",
1474 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
1475 #endif
1476 if (cm->width != width || cm->height != height) {
1477 const int new_mi_rows =
1478 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
1479 const int new_mi_cols =
1480 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
1481
1482 // Allocations in vp9_alloc_context_buffers() depend on individual
1483 // dimensions as well as the overall size.
1484 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
1485 if (vp9_alloc_context_buffers(cm, width, height)) {
1486 // The cm->mi_* values have been cleared and any existing context
1487 // buffers have been freed. Clear cm->width and cm->height to be
1488 // consistent and to force a realloc next time.
1489 cm->width = 0;
1490 cm->height = 0;
1491 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1492 "Failed to allocate context buffers");
1493 }
1494 } else {
1495 vp9_set_mb_mi(cm, width, height);
1496 }
1497 vp9_init_context_buffers(cm);
1498 cm->width = width;
1499 cm->height = height;
1500 }
1501 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
1502 cm->mi_cols > cm->cur_frame->mi_cols) {
1503 resize_mv_buffer(cm);
1504 }
1505 }
1506
setup_frame_size(VP9_COMMON * cm,struct vpx_read_bit_buffer * rb)1507 static void setup_frame_size(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1508 int width, height;
1509 BufferPool *const pool = cm->buffer_pool;
1510 vp9_read_frame_size(rb, &width, &height);
1511 resize_context_buffers(cm, width, height);
1512 setup_render_size(cm, rb);
1513
1514 if (vpx_realloc_frame_buffer(
1515 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
1516 cm->subsampling_y,
1517 #if CONFIG_VP9_HIGHBITDEPTH
1518 cm->use_highbitdepth,
1519 #endif
1520 VP9_DEC_BORDER_IN_PIXELS, cm->byte_alignment,
1521 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
1522 pool->cb_priv)) {
1523 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1524 "Failed to allocate frame buffer");
1525 }
1526
1527 pool->frame_bufs[cm->new_fb_idx].released = 0;
1528 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
1529 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
1530 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
1531 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
1532 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
1533 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
1534 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
1535 }
1536
valid_ref_frame_img_fmt(vpx_bit_depth_t ref_bit_depth,int ref_xss,int ref_yss,vpx_bit_depth_t this_bit_depth,int this_xss,int this_yss)1537 static INLINE int valid_ref_frame_img_fmt(vpx_bit_depth_t ref_bit_depth,
1538 int ref_xss, int ref_yss,
1539 vpx_bit_depth_t this_bit_depth,
1540 int this_xss, int this_yss) {
1541 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
1542 ref_yss == this_yss;
1543 }
1544
setup_frame_size_with_refs(VP9_COMMON * cm,struct vpx_read_bit_buffer * rb)1545 static void setup_frame_size_with_refs(VP9_COMMON *cm,
1546 struct vpx_read_bit_buffer *rb) {
1547 int width, height;
1548 int found = 0, i;
1549 int has_valid_ref_frame = 0;
1550 BufferPool *const pool = cm->buffer_pool;
1551 for (i = 0; i < REFS_PER_FRAME; ++i) {
1552 if (vpx_rb_read_bit(rb)) {
1553 if (cm->frame_refs[i].idx != INVALID_IDX) {
1554 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
1555 width = buf->y_crop_width;
1556 height = buf->y_crop_height;
1557 found = 1;
1558 break;
1559 } else {
1560 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1561 "Failed to decode frame size");
1562 }
1563 }
1564 }
1565
1566 if (!found) vp9_read_frame_size(rb, &width, &height);
1567
1568 if (width <= 0 || height <= 0)
1569 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1570 "Invalid frame size");
1571
1572 // Check to make sure at least one of frames that this frame references
1573 // has valid dimensions.
1574 for (i = 0; i < REFS_PER_FRAME; ++i) {
1575 RefBuffer *const ref_frame = &cm->frame_refs[i];
1576 has_valid_ref_frame |=
1577 (ref_frame->idx != INVALID_IDX &&
1578 valid_ref_frame_size(ref_frame->buf->y_crop_width,
1579 ref_frame->buf->y_crop_height, width, height));
1580 }
1581 if (!has_valid_ref_frame)
1582 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1583 "Referenced frame has invalid size");
1584 for (i = 0; i < REFS_PER_FRAME; ++i) {
1585 RefBuffer *const ref_frame = &cm->frame_refs[i];
1586 if (ref_frame->idx == INVALID_IDX ||
1587 !valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
1588 ref_frame->buf->subsampling_x,
1589 ref_frame->buf->subsampling_y, cm->bit_depth,
1590 cm->subsampling_x, cm->subsampling_y))
1591 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1592 "Referenced frame has incompatible color format");
1593 }
1594
1595 resize_context_buffers(cm, width, height);
1596 setup_render_size(cm, rb);
1597
1598 if (vpx_realloc_frame_buffer(
1599 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
1600 cm->subsampling_y,
1601 #if CONFIG_VP9_HIGHBITDEPTH
1602 cm->use_highbitdepth,
1603 #endif
1604 VP9_DEC_BORDER_IN_PIXELS, cm->byte_alignment,
1605 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
1606 pool->cb_priv)) {
1607 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1608 "Failed to allocate frame buffer");
1609 }
1610
1611 pool->frame_bufs[cm->new_fb_idx].released = 0;
1612 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
1613 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
1614 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
1615 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
1616 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
1617 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
1618 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
1619 }
1620
setup_tile_info(VP9_COMMON * cm,struct vpx_read_bit_buffer * rb)1621 static void setup_tile_info(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1622 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
1623 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
1624
1625 // columns
1626 max_ones = max_log2_tile_cols - min_log2_tile_cols;
1627 cm->log2_tile_cols = min_log2_tile_cols;
1628 while (max_ones-- && vpx_rb_read_bit(rb)) cm->log2_tile_cols++;
1629
1630 if (cm->log2_tile_cols > 6)
1631 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1632 "Invalid number of tile columns");
1633
1634 // rows
1635 cm->log2_tile_rows = vpx_rb_read_bit(rb);
1636 if (cm->log2_tile_rows) cm->log2_tile_rows += vpx_rb_read_bit(rb);
1637 }
1638
1639 // Reads the next tile returning its size and adjusting '*data' accordingly
1640 // based on 'is_last'.
get_tile_buffer(const uint8_t * const data_end,int is_last,struct vpx_internal_error_info * error_info,const uint8_t ** data,vpx_decrypt_cb decrypt_cb,void * decrypt_state,TileBuffer * buf)1641 static void get_tile_buffer(const uint8_t *const data_end, int is_last,
1642 struct vpx_internal_error_info *error_info,
1643 const uint8_t **data, vpx_decrypt_cb decrypt_cb,
1644 void *decrypt_state, TileBuffer *buf) {
1645 size_t size;
1646
1647 if (!is_last) {
1648 if (!read_is_valid(*data, 4, data_end))
1649 vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
1650 "Truncated packet or corrupt tile length");
1651
1652 if (decrypt_cb) {
1653 uint8_t be_data[4];
1654 decrypt_cb(decrypt_state, *data, be_data, 4);
1655 size = mem_get_be32(be_data);
1656 } else {
1657 size = mem_get_be32(*data);
1658 }
1659 *data += 4;
1660
1661 if (size > (size_t)(data_end - *data))
1662 vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
1663 "Truncated packet or corrupt tile size");
1664 } else {
1665 size = data_end - *data;
1666 }
1667
1668 buf->data = *data;
1669 buf->size = size;
1670
1671 *data += size;
1672 }
1673
get_tile_buffers(VP9Decoder * pbi,const uint8_t * data,const uint8_t * data_end,int tile_cols,int tile_rows,TileBuffer (* tile_buffers)[1<<6])1674 static void get_tile_buffers(VP9Decoder *pbi, const uint8_t *data,
1675 const uint8_t *data_end, int tile_cols,
1676 int tile_rows,
1677 TileBuffer (*tile_buffers)[1 << 6]) {
1678 int r, c;
1679
1680 for (r = 0; r < tile_rows; ++r) {
1681 for (c = 0; c < tile_cols; ++c) {
1682 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
1683 TileBuffer *const buf = &tile_buffers[r][c];
1684 buf->col = c;
1685 get_tile_buffer(data_end, is_last, &pbi->common.error, &data,
1686 pbi->decrypt_cb, pbi->decrypt_state, buf);
1687 }
1688 }
1689 }
1690
decode_tiles(VP9Decoder * pbi,const uint8_t * data,const uint8_t * data_end)1691 static const uint8_t *decode_tiles(VP9Decoder *pbi, const uint8_t *data,
1692 const uint8_t *data_end) {
1693 VP9_COMMON *const cm = &pbi->common;
1694 const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
1695 const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
1696 const int tile_cols = 1 << cm->log2_tile_cols;
1697 const int tile_rows = 1 << cm->log2_tile_rows;
1698 TileBuffer tile_buffers[4][1 << 6];
1699 int tile_row, tile_col;
1700 int mi_row, mi_col;
1701 TileWorkerData *tile_data = NULL;
1702
1703 if (cm->lf.filter_level && !cm->skip_loop_filter &&
1704 pbi->lf_worker.data1 == NULL) {
1705 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
1706 vpx_memalign(32, sizeof(LFWorkerData)));
1707 pbi->lf_worker.hook = vp9_loop_filter_worker;
1708 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
1709 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
1710 "Loop filter thread creation failed");
1711 }
1712 }
1713
1714 if (cm->lf.filter_level && !cm->skip_loop_filter) {
1715 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
1716 // Be sure to sync as we might be resuming after a failed frame decode.
1717 winterface->sync(&pbi->lf_worker);
1718 vp9_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
1719 pbi->mb.plane);
1720 }
1721
1722 assert(tile_rows <= 4);
1723 assert(tile_cols <= (1 << 6));
1724
1725 // Note: this memset assumes above_context[0], [1] and [2]
1726 // are allocated as part of the same buffer.
1727 memset(cm->above_context, 0,
1728 sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_cols);
1729
1730 memset(cm->above_seg_context, 0,
1731 sizeof(*cm->above_seg_context) * aligned_cols);
1732
1733 vp9_reset_lfm(cm);
1734
1735 get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows, tile_buffers);
1736
1737 // Load all tile information into tile_data.
1738 for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
1739 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
1740 const TileBuffer *const buf = &tile_buffers[tile_row][tile_col];
1741 tile_data = pbi->tile_worker_data + tile_cols * tile_row + tile_col;
1742 tile_data->xd = pbi->mb;
1743 tile_data->xd.corrupted = 0;
1744 tile_data->xd.counts =
1745 cm->frame_parallel_decoding_mode ? NULL : &cm->counts;
1746 vp9_zero(tile_data->dqcoeff);
1747 vp9_tile_init(&tile_data->xd.tile, cm, tile_row, tile_col);
1748 setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
1749 &tile_data->bit_reader, pbi->decrypt_cb,
1750 pbi->decrypt_state);
1751 vp9_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff);
1752 }
1753 }
1754
1755 for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
1756 TileInfo tile;
1757 vp9_tile_set_row(&tile, cm, tile_row);
1758 for (mi_row = tile.mi_row_start; mi_row < tile.mi_row_end;
1759 mi_row += MI_BLOCK_SIZE) {
1760 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
1761 const int col =
1762 pbi->inv_tile_order ? tile_cols - tile_col - 1 : tile_col;
1763 tile_data = pbi->tile_worker_data + tile_cols * tile_row + col;
1764 vp9_tile_set_col(&tile, cm, col);
1765 vp9_zero(tile_data->xd.left_context);
1766 vp9_zero(tile_data->xd.left_seg_context);
1767 for (mi_col = tile.mi_col_start; mi_col < tile.mi_col_end;
1768 mi_col += MI_BLOCK_SIZE) {
1769 if (pbi->row_mt == 1) {
1770 int plane;
1771 RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data;
1772 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1773 tile_data->xd.plane[plane].eob = row_mt_worker_data->eob[plane];
1774 tile_data->xd.plane[plane].dqcoeff =
1775 row_mt_worker_data->dqcoeff[plane];
1776 }
1777 tile_data->xd.partition = row_mt_worker_data->partition;
1778 parse_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4);
1779
1780 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1781 tile_data->xd.plane[plane].eob = row_mt_worker_data->eob[plane];
1782 tile_data->xd.plane[plane].dqcoeff =
1783 row_mt_worker_data->dqcoeff[plane];
1784 }
1785 tile_data->xd.partition = row_mt_worker_data->partition;
1786 recon_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4);
1787 } else {
1788 decode_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4);
1789 }
1790 }
1791 pbi->mb.corrupted |= tile_data->xd.corrupted;
1792 if (pbi->mb.corrupted)
1793 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1794 "Failed to decode tile data");
1795 }
1796 // Loopfilter one row.
1797 if (cm->lf.filter_level && !cm->skip_loop_filter) {
1798 const int lf_start = mi_row - MI_BLOCK_SIZE;
1799 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
1800
1801 // delay the loopfilter by 1 macroblock row.
1802 if (lf_start < 0) continue;
1803
1804 // decoding has completed: finish up the loop filter in this thread.
1805 if (mi_row + MI_BLOCK_SIZE >= cm->mi_rows) continue;
1806
1807 winterface->sync(&pbi->lf_worker);
1808 lf_data->start = lf_start;
1809 lf_data->stop = mi_row;
1810 if (pbi->max_threads > 1) {
1811 winterface->launch(&pbi->lf_worker);
1812 } else {
1813 winterface->execute(&pbi->lf_worker);
1814 }
1815 }
1816 }
1817 }
1818
1819 // Loopfilter remaining rows in the frame.
1820 if (cm->lf.filter_level && !cm->skip_loop_filter) {
1821 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
1822 winterface->sync(&pbi->lf_worker);
1823 lf_data->start = lf_data->stop;
1824 lf_data->stop = cm->mi_rows;
1825 winterface->execute(&pbi->lf_worker);
1826 }
1827
1828 // Get last tile data.
1829 tile_data = pbi->tile_worker_data + tile_cols * tile_rows - 1;
1830
1831 return vpx_reader_find_end(&tile_data->bit_reader);
1832 }
1833
set_rows_after_error(VP9LfSync * lf_sync,int start_row,int mi_rows,int num_tiles_left,int total_num_tiles)1834 static void set_rows_after_error(VP9LfSync *lf_sync, int start_row, int mi_rows,
1835 int num_tiles_left, int total_num_tiles) {
1836 do {
1837 int mi_row;
1838 const int aligned_rows = mi_cols_aligned_to_sb(mi_rows);
1839 const int sb_rows = (aligned_rows >> MI_BLOCK_SIZE_LOG2);
1840 const int corrupted = 1;
1841 for (mi_row = start_row; mi_row < mi_rows; mi_row += MI_BLOCK_SIZE) {
1842 const int is_last_row = (sb_rows - 1 == mi_row >> MI_BLOCK_SIZE_LOG2);
1843 vp9_set_row(lf_sync, total_num_tiles, mi_row >> MI_BLOCK_SIZE_LOG2,
1844 is_last_row, corrupted);
1845 }
1846 /* If there are multiple tiles, the second tile should start marking row
1847 * progress from row 0.
1848 */
1849 start_row = 0;
1850 } while (num_tiles_left--);
1851 }
1852
1853 // On entry 'tile_data->data_end' points to the end of the input frame, on exit
1854 // it is updated to reflect the bitreader position of the final tile column if
1855 // present in the tile buffer group or NULL otherwise.
tile_worker_hook(void * arg1,void * arg2)1856 static int tile_worker_hook(void *arg1, void *arg2) {
1857 TileWorkerData *const tile_data = (TileWorkerData *)arg1;
1858 VP9Decoder *const pbi = (VP9Decoder *)arg2;
1859
1860 TileInfo *volatile tile = &tile_data->xd.tile;
1861 const int final_col = (1 << pbi->common.log2_tile_cols) - 1;
1862 const uint8_t *volatile bit_reader_end = NULL;
1863 VP9_COMMON *cm = &pbi->common;
1864
1865 LFWorkerData *lf_data = tile_data->lf_data;
1866 VP9LfSync *lf_sync = tile_data->lf_sync;
1867
1868 volatile int mi_row = 0;
1869 volatile int n = tile_data->buf_start;
1870 tile_data->error_info.setjmp = 1;
1871
1872 if (setjmp(tile_data->error_info.jmp)) {
1873 tile_data->error_info.setjmp = 0;
1874 tile_data->xd.corrupted = 1;
1875 tile_data->data_end = NULL;
1876 if (pbi->lpf_mt_opt && cm->lf.filter_level && !cm->skip_loop_filter) {
1877 const int num_tiles_left = tile_data->buf_end - n;
1878 const int mi_row_start = mi_row;
1879 set_rows_after_error(lf_sync, mi_row_start, cm->mi_rows, num_tiles_left,
1880 1 << cm->log2_tile_cols);
1881 }
1882 return 0;
1883 }
1884
1885 tile_data->xd.corrupted = 0;
1886
1887 do {
1888 int mi_col;
1889 const TileBuffer *const buf = pbi->tile_buffers + n;
1890
1891 /* Initialize to 0 is safe since we do not deal with streams that have
1892 * more than one row of tiles. (So tile->mi_row_start will be 0)
1893 */
1894 assert(cm->log2_tile_rows == 0);
1895 mi_row = 0;
1896 vp9_zero(tile_data->dqcoeff);
1897 vp9_tile_init(tile, &pbi->common, 0, buf->col);
1898 setup_token_decoder(buf->data, tile_data->data_end, buf->size,
1899 &tile_data->error_info, &tile_data->bit_reader,
1900 pbi->decrypt_cb, pbi->decrypt_state);
1901 vp9_init_macroblockd(&pbi->common, &tile_data->xd, tile_data->dqcoeff);
1902 // init resets xd.error_info
1903 tile_data->xd.error_info = &tile_data->error_info;
1904
1905 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
1906 mi_row += MI_BLOCK_SIZE) {
1907 vp9_zero(tile_data->xd.left_context);
1908 vp9_zero(tile_data->xd.left_seg_context);
1909 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
1910 mi_col += MI_BLOCK_SIZE) {
1911 decode_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4);
1912 }
1913 if (pbi->lpf_mt_opt && cm->lf.filter_level && !cm->skip_loop_filter) {
1914 const int aligned_rows = mi_cols_aligned_to_sb(cm->mi_rows);
1915 const int sb_rows = (aligned_rows >> MI_BLOCK_SIZE_LOG2);
1916 const int is_last_row = (sb_rows - 1 == mi_row >> MI_BLOCK_SIZE_LOG2);
1917 vp9_set_row(lf_sync, 1 << cm->log2_tile_cols,
1918 mi_row >> MI_BLOCK_SIZE_LOG2, is_last_row,
1919 tile_data->xd.corrupted);
1920 }
1921 }
1922
1923 if (buf->col == final_col) {
1924 bit_reader_end = vpx_reader_find_end(&tile_data->bit_reader);
1925 }
1926 } while (!tile_data->xd.corrupted && ++n <= tile_data->buf_end);
1927
1928 if (pbi->lpf_mt_opt && n < tile_data->buf_end && cm->lf.filter_level &&
1929 !cm->skip_loop_filter) {
1930 /* This was not incremented in the tile loop, so increment before tiles left
1931 * calculation
1932 */
1933 ++n;
1934 set_rows_after_error(lf_sync, 0, cm->mi_rows, tile_data->buf_end - n,
1935 1 << cm->log2_tile_cols);
1936 }
1937
1938 if (pbi->lpf_mt_opt && !tile_data->xd.corrupted && cm->lf.filter_level &&
1939 !cm->skip_loop_filter) {
1940 vp9_loopfilter_rows(lf_data, lf_sync);
1941 }
1942
1943 tile_data->data_end = bit_reader_end;
1944 return !tile_data->xd.corrupted;
1945 }
1946
1947 // sorts in descending order
compare_tile_buffers(const void * a,const void * b)1948 static int compare_tile_buffers(const void *a, const void *b) {
1949 const TileBuffer *const buf_a = (const TileBuffer *)a;
1950 const TileBuffer *const buf_b = (const TileBuffer *)b;
1951 return (buf_a->size < buf_b->size) - (buf_a->size > buf_b->size);
1952 }
1953
decode_tiles_mt(VP9Decoder * pbi,const uint8_t * data,const uint8_t * data_end)1954 static const uint8_t *decode_tiles_mt(VP9Decoder *pbi, const uint8_t *data,
1955 const uint8_t *data_end) {
1956 VP9_COMMON *const cm = &pbi->common;
1957 const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
1958 const uint8_t *bit_reader_end = NULL;
1959 VP9LfSync *lf_row_sync = &pbi->lf_row_sync;
1960 YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm);
1961 const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
1962 const int tile_cols = 1 << cm->log2_tile_cols;
1963 const int tile_rows = 1 << cm->log2_tile_rows;
1964 const int num_workers = VPXMIN(pbi->max_threads, tile_cols);
1965 int n;
1966
1967 assert(tile_cols <= (1 << 6));
1968 assert(tile_rows == 1);
1969 (void)tile_rows;
1970
1971 if (pbi->num_tile_workers == 0) {
1972 const int num_threads = pbi->max_threads;
1973 CHECK_MEM_ERROR(cm, pbi->tile_workers,
1974 vpx_malloc(num_threads * sizeof(*pbi->tile_workers)));
1975 for (n = 0; n < num_threads; ++n) {
1976 VPxWorker *const worker = &pbi->tile_workers[n];
1977 ++pbi->num_tile_workers;
1978
1979 winterface->init(worker);
1980 if (n < num_threads - 1 && !winterface->reset(worker)) {
1981 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
1982 "Tile decoder thread creation failed");
1983 }
1984 }
1985 }
1986
1987 // Initialize LPF
1988 if (pbi->lpf_mt_opt && cm->lf.filter_level && !cm->skip_loop_filter) {
1989 vp9_lpf_mt_init(lf_row_sync, cm, cm->lf.filter_level,
1990 pbi->num_tile_workers);
1991 }
1992
1993 // Reset tile decoding hook
1994 for (n = 0; n < num_workers; ++n) {
1995 VPxWorker *const worker = &pbi->tile_workers[n];
1996 TileWorkerData *const tile_data =
1997 &pbi->tile_worker_data[n + pbi->total_tiles];
1998 winterface->sync(worker);
1999
2000 if (pbi->lpf_mt_opt && cm->lf.filter_level && !cm->skip_loop_filter) {
2001 tile_data->lf_sync = lf_row_sync;
2002 tile_data->lf_data = &tile_data->lf_sync->lfdata[n];
2003 vp9_loop_filter_data_reset(tile_data->lf_data, new_fb, cm, pbi->mb.plane);
2004 tile_data->lf_data->y_only = 0;
2005 }
2006
2007 tile_data->xd = pbi->mb;
2008 tile_data->xd.counts =
2009 cm->frame_parallel_decoding_mode ? NULL : &tile_data->counts;
2010 worker->hook = tile_worker_hook;
2011 worker->data1 = tile_data;
2012 worker->data2 = pbi;
2013 }
2014
2015 // Note: this memset assumes above_context[0], [1] and [2]
2016 // are allocated as part of the same buffer.
2017 memset(cm->above_context, 0,
2018 sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_mi_cols);
2019 memset(cm->above_seg_context, 0,
2020 sizeof(*cm->above_seg_context) * aligned_mi_cols);
2021
2022 vp9_reset_lfm(cm);
2023
2024 // Load tile data into tile_buffers
2025 get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows,
2026 &pbi->tile_buffers);
2027
2028 // Sort the buffers based on size in descending order.
2029 qsort(pbi->tile_buffers, tile_cols, sizeof(pbi->tile_buffers[0]),
2030 compare_tile_buffers);
2031
2032 if (num_workers == tile_cols) {
2033 // Rearrange the tile buffers such that the largest, and
2034 // presumably the most difficult, tile will be decoded in the main thread.
2035 // This should help minimize the number of instances where the main thread
2036 // is waiting for a worker to complete.
2037 const TileBuffer largest = pbi->tile_buffers[0];
2038 memmove(pbi->tile_buffers, pbi->tile_buffers + 1,
2039 (tile_cols - 1) * sizeof(pbi->tile_buffers[0]));
2040 pbi->tile_buffers[tile_cols - 1] = largest;
2041 } else {
2042 int start = 0, end = tile_cols - 2;
2043 TileBuffer tmp;
2044
2045 // Interleave the tiles to distribute the load between threads, assuming a
2046 // larger tile implies it is more difficult to decode.
2047 while (start < end) {
2048 tmp = pbi->tile_buffers[start];
2049 pbi->tile_buffers[start] = pbi->tile_buffers[end];
2050 pbi->tile_buffers[end] = tmp;
2051 start += 2;
2052 end -= 2;
2053 }
2054 }
2055
2056 // Initialize thread frame counts.
2057 if (!cm->frame_parallel_decoding_mode) {
2058 for (n = 0; n < num_workers; ++n) {
2059 TileWorkerData *const tile_data =
2060 (TileWorkerData *)pbi->tile_workers[n].data1;
2061 vp9_zero(tile_data->counts);
2062 }
2063 }
2064
2065 {
2066 const int base = tile_cols / num_workers;
2067 const int remain = tile_cols % num_workers;
2068 int buf_start = 0;
2069
2070 for (n = 0; n < num_workers; ++n) {
2071 const int count = base + (remain + n) / num_workers;
2072 VPxWorker *const worker = &pbi->tile_workers[n];
2073 TileWorkerData *const tile_data = (TileWorkerData *)worker->data1;
2074
2075 tile_data->buf_start = buf_start;
2076 tile_data->buf_end = buf_start + count - 1;
2077 tile_data->data_end = data_end;
2078 buf_start += count;
2079
2080 worker->had_error = 0;
2081 if (n == num_workers - 1) {
2082 assert(tile_data->buf_end == tile_cols - 1);
2083 winterface->execute(worker);
2084 } else {
2085 winterface->launch(worker);
2086 }
2087 }
2088
2089 for (; n > 0; --n) {
2090 VPxWorker *const worker = &pbi->tile_workers[n - 1];
2091 TileWorkerData *const tile_data = (TileWorkerData *)worker->data1;
2092 // TODO(jzern): The tile may have specific error data associated with
2093 // its vpx_internal_error_info which could be propagated to the main info
2094 // in cm. Additionally once the threads have been synced and an error is
2095 // detected, there's no point in continuing to decode tiles.
2096 pbi->mb.corrupted |= !winterface->sync(worker);
2097 if (!bit_reader_end) bit_reader_end = tile_data->data_end;
2098 }
2099 }
2100
2101 // Accumulate thread frame counts.
2102 if (!cm->frame_parallel_decoding_mode) {
2103 for (n = 0; n < num_workers; ++n) {
2104 TileWorkerData *const tile_data =
2105 (TileWorkerData *)pbi->tile_workers[n].data1;
2106 vp9_accumulate_frame_counts(&cm->counts, &tile_data->counts, 1);
2107 }
2108 }
2109
2110 assert(bit_reader_end || pbi->mb.corrupted);
2111 return bit_reader_end;
2112 }
2113
error_handler(void * data)2114 static void error_handler(void *data) {
2115 VP9_COMMON *const cm = (VP9_COMMON *)data;
2116 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, "Truncated packet");
2117 }
2118
read_bitdepth_colorspace_sampling(VP9_COMMON * cm,struct vpx_read_bit_buffer * rb)2119 static void read_bitdepth_colorspace_sampling(VP9_COMMON *cm,
2120 struct vpx_read_bit_buffer *rb) {
2121 if (cm->profile >= PROFILE_2) {
2122 cm->bit_depth = vpx_rb_read_bit(rb) ? VPX_BITS_12 : VPX_BITS_10;
2123 #if CONFIG_VP9_HIGHBITDEPTH
2124 cm->use_highbitdepth = 1;
2125 #endif
2126 } else {
2127 cm->bit_depth = VPX_BITS_8;
2128 #if CONFIG_VP9_HIGHBITDEPTH
2129 cm->use_highbitdepth = 0;
2130 #endif
2131 }
2132 cm->color_space = vpx_rb_read_literal(rb, 3);
2133 if (cm->color_space != VPX_CS_SRGB) {
2134 cm->color_range = (vpx_color_range_t)vpx_rb_read_bit(rb);
2135 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
2136 cm->subsampling_x = vpx_rb_read_bit(rb);
2137 cm->subsampling_y = vpx_rb_read_bit(rb);
2138 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
2139 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2140 "4:2:0 color not supported in profile 1 or 3");
2141 if (vpx_rb_read_bit(rb))
2142 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2143 "Reserved bit set");
2144 } else {
2145 cm->subsampling_y = cm->subsampling_x = 1;
2146 }
2147 } else {
2148 cm->color_range = VPX_CR_FULL_RANGE;
2149 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
2150 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
2151 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
2152 cm->subsampling_y = cm->subsampling_x = 0;
2153 if (vpx_rb_read_bit(rb))
2154 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2155 "Reserved bit set");
2156 } else {
2157 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2158 "4:4:4 color not supported in profile 0 or 2");
2159 }
2160 }
2161 }
2162
flush_all_fb_on_key(VP9_COMMON * cm)2163 static INLINE void flush_all_fb_on_key(VP9_COMMON *cm) {
2164 if (cm->frame_type == KEY_FRAME && cm->current_video_frame > 0) {
2165 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
2166 BufferPool *const pool = cm->buffer_pool;
2167 int i;
2168 for (i = 0; i < FRAME_BUFFERS; ++i) {
2169 if (i == cm->new_fb_idx) continue;
2170 frame_bufs[i].ref_count = 0;
2171 if (!frame_bufs[i].released) {
2172 pool->release_fb_cb(pool->cb_priv, &frame_bufs[i].raw_frame_buffer);
2173 frame_bufs[i].released = 1;
2174 }
2175 }
2176 }
2177 }
2178
read_uncompressed_header(VP9Decoder * pbi,struct vpx_read_bit_buffer * rb)2179 static size_t read_uncompressed_header(VP9Decoder *pbi,
2180 struct vpx_read_bit_buffer *rb) {
2181 VP9_COMMON *const cm = &pbi->common;
2182 BufferPool *const pool = cm->buffer_pool;
2183 RefCntBuffer *const frame_bufs = pool->frame_bufs;
2184 int i, mask, ref_index = 0;
2185 size_t sz;
2186
2187 cm->last_frame_type = cm->frame_type;
2188 cm->last_intra_only = cm->intra_only;
2189
2190 if (vpx_rb_read_literal(rb, 2) != VP9_FRAME_MARKER)
2191 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2192 "Invalid frame marker");
2193
2194 cm->profile = vp9_read_profile(rb);
2195 #if CONFIG_VP9_HIGHBITDEPTH
2196 if (cm->profile >= MAX_PROFILES)
2197 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2198 "Unsupported bitstream profile");
2199 #else
2200 if (cm->profile >= PROFILE_2)
2201 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2202 "Unsupported bitstream profile");
2203 #endif
2204
2205 cm->show_existing_frame = vpx_rb_read_bit(rb);
2206 if (cm->show_existing_frame) {
2207 // Show an existing frame directly.
2208 const int frame_to_show = cm->ref_frame_map[vpx_rb_read_literal(rb, 3)];
2209 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
2210 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2211 "Buffer %d does not contain a decoded frame",
2212 frame_to_show);
2213 }
2214
2215 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
2216 pbi->refresh_frame_flags = 0;
2217 cm->lf.filter_level = 0;
2218 cm->show_frame = 1;
2219
2220 return 0;
2221 }
2222
2223 cm->frame_type = (FRAME_TYPE)vpx_rb_read_bit(rb);
2224 cm->show_frame = vpx_rb_read_bit(rb);
2225 cm->error_resilient_mode = vpx_rb_read_bit(rb);
2226
2227 if (cm->frame_type == KEY_FRAME) {
2228 if (!vp9_read_sync_code(rb))
2229 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2230 "Invalid frame sync code");
2231
2232 read_bitdepth_colorspace_sampling(cm, rb);
2233 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
2234
2235 for (i = 0; i < REFS_PER_FRAME; ++i) {
2236 cm->frame_refs[i].idx = INVALID_IDX;
2237 cm->frame_refs[i].buf = NULL;
2238 }
2239
2240 setup_frame_size(cm, rb);
2241 if (pbi->need_resync) {
2242 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
2243 flush_all_fb_on_key(cm);
2244 pbi->need_resync = 0;
2245 }
2246 } else {
2247 cm->intra_only = cm->show_frame ? 0 : vpx_rb_read_bit(rb);
2248
2249 cm->reset_frame_context =
2250 cm->error_resilient_mode ? 0 : vpx_rb_read_literal(rb, 2);
2251
2252 if (cm->intra_only) {
2253 if (!vp9_read_sync_code(rb))
2254 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2255 "Invalid frame sync code");
2256 if (cm->profile > PROFILE_0) {
2257 read_bitdepth_colorspace_sampling(cm, rb);
2258 } else {
2259 // NOTE: The intra-only frame header does not include the specification
2260 // of either the color format or color sub-sampling in profile 0. VP9
2261 // specifies that the default color format should be YUV 4:2:0 in this
2262 // case (normative).
2263 cm->color_space = VPX_CS_BT_601;
2264 cm->color_range = VPX_CR_STUDIO_RANGE;
2265 cm->subsampling_y = cm->subsampling_x = 1;
2266 cm->bit_depth = VPX_BITS_8;
2267 #if CONFIG_VP9_HIGHBITDEPTH
2268 cm->use_highbitdepth = 0;
2269 #endif
2270 }
2271
2272 pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES);
2273 setup_frame_size(cm, rb);
2274 if (pbi->need_resync) {
2275 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
2276 pbi->need_resync = 0;
2277 }
2278 } else if (pbi->need_resync != 1) { /* Skip if need resync */
2279 pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES);
2280 for (i = 0; i < REFS_PER_FRAME; ++i) {
2281 const int ref = vpx_rb_read_literal(rb, REF_FRAMES_LOG2);
2282 const int idx = cm->ref_frame_map[ref];
2283 RefBuffer *const ref_frame = &cm->frame_refs[i];
2284 ref_frame->idx = idx;
2285 ref_frame->buf = &frame_bufs[idx].buf;
2286 cm->ref_frame_sign_bias[LAST_FRAME + i] = vpx_rb_read_bit(rb);
2287 }
2288
2289 setup_frame_size_with_refs(cm, rb);
2290
2291 cm->allow_high_precision_mv = vpx_rb_read_bit(rb);
2292 cm->interp_filter = read_interp_filter(rb);
2293
2294 for (i = 0; i < REFS_PER_FRAME; ++i) {
2295 RefBuffer *const ref_buf = &cm->frame_refs[i];
2296 #if CONFIG_VP9_HIGHBITDEPTH
2297 vp9_setup_scale_factors_for_frame(
2298 &ref_buf->sf, ref_buf->buf->y_crop_width,
2299 ref_buf->buf->y_crop_height, cm->width, cm->height,
2300 cm->use_highbitdepth);
2301 #else
2302 vp9_setup_scale_factors_for_frame(
2303 &ref_buf->sf, ref_buf->buf->y_crop_width,
2304 ref_buf->buf->y_crop_height, cm->width, cm->height);
2305 #endif
2306 }
2307 }
2308 }
2309 #if CONFIG_VP9_HIGHBITDEPTH
2310 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
2311 #endif
2312 get_frame_new_buffer(cm)->color_space = cm->color_space;
2313 get_frame_new_buffer(cm)->color_range = cm->color_range;
2314 get_frame_new_buffer(cm)->render_width = cm->render_width;
2315 get_frame_new_buffer(cm)->render_height = cm->render_height;
2316
2317 if (pbi->need_resync) {
2318 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2319 "Keyframe / intra-only frame required to reset decoder"
2320 " state");
2321 }
2322
2323 if (!cm->error_resilient_mode) {
2324 cm->refresh_frame_context = vpx_rb_read_bit(rb);
2325 cm->frame_parallel_decoding_mode = vpx_rb_read_bit(rb);
2326 if (!cm->frame_parallel_decoding_mode) vp9_zero(cm->counts);
2327 } else {
2328 cm->refresh_frame_context = 0;
2329 cm->frame_parallel_decoding_mode = 1;
2330 }
2331
2332 // This flag will be overridden by the call to vp9_setup_past_independence
2333 // below, forcing the use of context 0 for those frame types.
2334 cm->frame_context_idx = vpx_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
2335
2336 // Generate next_ref_frame_map.
2337 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
2338 if (mask & 1) {
2339 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
2340 ++frame_bufs[cm->new_fb_idx].ref_count;
2341 } else {
2342 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
2343 }
2344 // Current thread holds the reference frame.
2345 if (cm->ref_frame_map[ref_index] >= 0)
2346 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
2347 ++ref_index;
2348 }
2349
2350 for (; ref_index < REF_FRAMES; ++ref_index) {
2351 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
2352 // Current thread holds the reference frame.
2353 if (cm->ref_frame_map[ref_index] >= 0)
2354 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
2355 }
2356 pbi->hold_ref_buf = 1;
2357
2358 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
2359 vp9_setup_past_independence(cm);
2360
2361 setup_loopfilter(&cm->lf, rb);
2362 setup_quantization(cm, &pbi->mb, rb);
2363 setup_segmentation(&cm->seg, rb);
2364 setup_segmentation_dequant(cm);
2365
2366 setup_tile_info(cm, rb);
2367 if (pbi->row_mt == 1) {
2368 int num_sbs = 1;
2369
2370 if (pbi->row_mt_worker_data == NULL) {
2371 CHECK_MEM_ERROR(cm, pbi->row_mt_worker_data,
2372 vpx_calloc(1, sizeof(*pbi->row_mt_worker_data)));
2373 }
2374
2375 if (pbi->max_threads > 1) {
2376 const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
2377 const int sb_cols = aligned_cols >> MI_BLOCK_SIZE_LOG2;
2378 const int aligned_rows = mi_cols_aligned_to_sb(cm->mi_rows);
2379 const int sb_rows = aligned_rows >> MI_BLOCK_SIZE_LOG2;
2380
2381 num_sbs = sb_cols * sb_rows;
2382 }
2383
2384 if (num_sbs > pbi->row_mt_worker_data->num_sbs) {
2385 vp9_dec_free_row_mt_mem(pbi->row_mt_worker_data);
2386 vp9_dec_alloc_row_mt_mem(pbi->row_mt_worker_data, cm, num_sbs);
2387 }
2388 }
2389 sz = vpx_rb_read_literal(rb, 16);
2390
2391 if (sz == 0)
2392 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2393 "Invalid header size");
2394
2395 return sz;
2396 }
2397
read_compressed_header(VP9Decoder * pbi,const uint8_t * data,size_t partition_size)2398 static int read_compressed_header(VP9Decoder *pbi, const uint8_t *data,
2399 size_t partition_size) {
2400 VP9_COMMON *const cm = &pbi->common;
2401 MACROBLOCKD *const xd = &pbi->mb;
2402 FRAME_CONTEXT *const fc = cm->fc;
2403 vpx_reader r;
2404 int k;
2405
2406 if (vpx_reader_init(&r, data, partition_size, pbi->decrypt_cb,
2407 pbi->decrypt_state))
2408 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
2409 "Failed to allocate bool decoder 0");
2410
2411 cm->tx_mode = xd->lossless ? ONLY_4X4 : read_tx_mode(&r);
2412 if (cm->tx_mode == TX_MODE_SELECT) read_tx_mode_probs(&fc->tx_probs, &r);
2413 read_coef_probs(fc, cm->tx_mode, &r);
2414
2415 for (k = 0; k < SKIP_CONTEXTS; ++k)
2416 vp9_diff_update_prob(&r, &fc->skip_probs[k]);
2417
2418 if (!frame_is_intra_only(cm)) {
2419 nmv_context *const nmvc = &fc->nmvc;
2420 int i, j;
2421
2422 read_inter_mode_probs(fc, &r);
2423
2424 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
2425
2426 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
2427 vp9_diff_update_prob(&r, &fc->intra_inter_prob[i]);
2428
2429 cm->reference_mode = read_frame_reference_mode(cm, &r);
2430 if (cm->reference_mode != SINGLE_REFERENCE)
2431 vp9_setup_compound_reference_mode(cm);
2432 read_frame_reference_mode_probs(cm, &r);
2433
2434 for (j = 0; j < BLOCK_SIZE_GROUPS; j++)
2435 for (i = 0; i < INTRA_MODES - 1; ++i)
2436 vp9_diff_update_prob(&r, &fc->y_mode_prob[j][i]);
2437
2438 for (j = 0; j < PARTITION_CONTEXTS; ++j)
2439 for (i = 0; i < PARTITION_TYPES - 1; ++i)
2440 vp9_diff_update_prob(&r, &fc->partition_prob[j][i]);
2441
2442 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
2443 }
2444
2445 return vpx_reader_has_error(&r);
2446 }
2447
init_read_bit_buffer(VP9Decoder * pbi,struct vpx_read_bit_buffer * rb,const uint8_t * data,const uint8_t * data_end,uint8_t clear_data[MAX_VP9_HEADER_SIZE])2448 static struct vpx_read_bit_buffer *init_read_bit_buffer(
2449 VP9Decoder *pbi, struct vpx_read_bit_buffer *rb, const uint8_t *data,
2450 const uint8_t *data_end, uint8_t clear_data[MAX_VP9_HEADER_SIZE]) {
2451 rb->bit_offset = 0;
2452 rb->error_handler = error_handler;
2453 rb->error_handler_data = &pbi->common;
2454 if (pbi->decrypt_cb) {
2455 const int n = (int)VPXMIN(MAX_VP9_HEADER_SIZE, data_end - data);
2456 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
2457 rb->bit_buffer = clear_data;
2458 rb->bit_buffer_end = clear_data + n;
2459 } else {
2460 rb->bit_buffer = data;
2461 rb->bit_buffer_end = data_end;
2462 }
2463 return rb;
2464 }
2465
2466 //------------------------------------------------------------------------------
2467
vp9_read_sync_code(struct vpx_read_bit_buffer * const rb)2468 int vp9_read_sync_code(struct vpx_read_bit_buffer *const rb) {
2469 return vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_0 &&
2470 vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_1 &&
2471 vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_2;
2472 }
2473
vp9_read_frame_size(struct vpx_read_bit_buffer * rb,int * width,int * height)2474 void vp9_read_frame_size(struct vpx_read_bit_buffer *rb, int *width,
2475 int *height) {
2476 *width = vpx_rb_read_literal(rb, 16) + 1;
2477 *height = vpx_rb_read_literal(rb, 16) + 1;
2478 }
2479
vp9_read_profile(struct vpx_read_bit_buffer * rb)2480 BITSTREAM_PROFILE vp9_read_profile(struct vpx_read_bit_buffer *rb) {
2481 int profile = vpx_rb_read_bit(rb);
2482 profile |= vpx_rb_read_bit(rb) << 1;
2483 if (profile > 2) profile += vpx_rb_read_bit(rb);
2484 return (BITSTREAM_PROFILE)profile;
2485 }
2486
vp9_decode_frame(VP9Decoder * pbi,const uint8_t * data,const uint8_t * data_end,const uint8_t ** p_data_end)2487 void vp9_decode_frame(VP9Decoder *pbi, const uint8_t *data,
2488 const uint8_t *data_end, const uint8_t **p_data_end) {
2489 VP9_COMMON *const cm = &pbi->common;
2490 MACROBLOCKD *const xd = &pbi->mb;
2491 struct vpx_read_bit_buffer rb;
2492 int context_updated = 0;
2493 uint8_t clear_data[MAX_VP9_HEADER_SIZE];
2494 const size_t first_partition_size = read_uncompressed_header(
2495 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
2496 const int tile_rows = 1 << cm->log2_tile_rows;
2497 const int tile_cols = 1 << cm->log2_tile_cols;
2498 YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm);
2499 xd->cur_buf = new_fb;
2500
2501 if (!first_partition_size) {
2502 // showing a frame directly
2503 *p_data_end = data + (cm->profile <= PROFILE_2 ? 1 : 2);
2504 return;
2505 }
2506
2507 data += vpx_rb_bytes_read(&rb);
2508 if (!read_is_valid(data, first_partition_size, data_end))
2509 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2510 "Truncated packet or corrupt header length");
2511
2512 cm->use_prev_frame_mvs =
2513 !cm->error_resilient_mode && cm->width == cm->last_width &&
2514 cm->height == cm->last_height && !cm->last_intra_only &&
2515 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
2516
2517 vp9_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
2518
2519 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
2520 if (!cm->fc->initialized)
2521 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2522 "Uninitialized entropy context.");
2523
2524 xd->corrupted = 0;
2525 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
2526 if (new_fb->corrupted)
2527 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2528 "Decode failed. Frame data header is corrupted.");
2529
2530 if (cm->lf.filter_level && !cm->skip_loop_filter) {
2531 vp9_loop_filter_frame_init(cm, cm->lf.filter_level);
2532 }
2533
2534 if (pbi->tile_worker_data == NULL ||
2535 (tile_cols * tile_rows) != pbi->total_tiles) {
2536 const int num_tile_workers =
2537 tile_cols * tile_rows + ((pbi->max_threads > 1) ? pbi->max_threads : 0);
2538 const size_t twd_size = num_tile_workers * sizeof(*pbi->tile_worker_data);
2539 // Ensure tile data offsets will be properly aligned. This may fail on
2540 // platforms without DECLARE_ALIGNED().
2541 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
2542 vpx_free(pbi->tile_worker_data);
2543 CHECK_MEM_ERROR(cm, pbi->tile_worker_data, vpx_memalign(32, twd_size));
2544 pbi->total_tiles = tile_rows * tile_cols;
2545 }
2546
2547 if (pbi->max_threads > 1 && tile_rows == 1 && tile_cols > 1) {
2548 // Multi-threaded tile decoder
2549 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
2550 if (!pbi->lpf_mt_opt) {
2551 if (!xd->corrupted) {
2552 if (!cm->skip_loop_filter) {
2553 // If multiple threads are used to decode tiles, then we use those
2554 // threads to do parallel loopfiltering.
2555 vp9_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane,
2556 cm->lf.filter_level, 0, 0, pbi->tile_workers,
2557 pbi->num_tile_workers, &pbi->lf_row_sync);
2558 }
2559 } else {
2560 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2561 "Decode failed. Frame data is corrupted.");
2562 }
2563 }
2564 } else {
2565 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
2566 }
2567
2568 if (!xd->corrupted) {
2569 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
2570 vp9_adapt_coef_probs(cm);
2571
2572 if (!frame_is_intra_only(cm)) {
2573 vp9_adapt_mode_probs(cm);
2574 vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
2575 }
2576 }
2577 } else {
2578 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2579 "Decode failed. Frame data is corrupted.");
2580 }
2581
2582 // Non frame parallel update frame context here.
2583 if (cm->refresh_frame_context && !context_updated)
2584 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
2585 }
2586