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