VP9: Combine TileData with TileWorkerData
[aom.git] / vp9 / decoder / vp9_decodeframe.c
blob01f7b62842313406be39234f454714d2352f5ee4
1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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 */
11 #include <assert.h>
12 #include <stdlib.h> // qsort()
14 #include "./vp9_rtcd.h"
15 #include "./vpx_dsp_rtcd.h"
16 #include "./vpx_scale_rtcd.h"
18 #include "vpx_dsp/bitreader_buffer.h"
19 #include "vpx_dsp/bitreader.h"
20 #include "vpx_dsp/vpx_dsp_common.h"
21 #include "vpx_mem/vpx_mem.h"
22 #include "vpx_ports/mem.h"
23 #include "vpx_ports/mem_ops.h"
24 #include "vpx_scale/vpx_scale.h"
25 #include "vpx_util/vpx_thread.h"
27 #include "vp9/common/vp9_alloccommon.h"
28 #include "vp9/common/vp9_common.h"
29 #include "vp9/common/vp9_entropy.h"
30 #include "vp9/common/vp9_entropymode.h"
31 #include "vp9/common/vp9_idct.h"
32 #include "vp9/common/vp9_thread_common.h"
33 #include "vp9/common/vp9_pred_common.h"
34 #include "vp9/common/vp9_quant_common.h"
35 #include "vp9/common/vp9_reconintra.h"
36 #include "vp9/common/vp9_reconinter.h"
37 #include "vp9/common/vp9_seg_common.h"
38 #include "vp9/common/vp9_tile_common.h"
40 #include "vp9/decoder/vp9_decodeframe.h"
41 #include "vp9/decoder/vp9_detokenize.h"
42 #include "vp9/decoder/vp9_decodemv.h"
43 #include "vp9/decoder/vp9_decoder.h"
44 #include "vp9/decoder/vp9_dsubexp.h"
46 #define MAX_VP9_HEADER_SIZE 80
48 static int is_compound_reference_allowed(const VP9_COMMON *cm) {
49 int i;
50 for (i = 1; i < REFS_PER_FRAME; ++i)
51 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1])
52 return 1;
54 return 0;
57 static void setup_compound_reference_mode(VP9_COMMON *cm) {
58 if (cm->ref_frame_sign_bias[LAST_FRAME] ==
59 cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
60 cm->comp_fixed_ref = ALTREF_FRAME;
61 cm->comp_var_ref[0] = LAST_FRAME;
62 cm->comp_var_ref[1] = GOLDEN_FRAME;
63 } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
64 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
65 cm->comp_fixed_ref = GOLDEN_FRAME;
66 cm->comp_var_ref[0] = LAST_FRAME;
67 cm->comp_var_ref[1] = ALTREF_FRAME;
68 } else {
69 cm->comp_fixed_ref = LAST_FRAME;
70 cm->comp_var_ref[0] = GOLDEN_FRAME;
71 cm->comp_var_ref[1] = ALTREF_FRAME;
75 static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
76 return len != 0 && len <= (size_t)(end - start);
79 static int decode_unsigned_max(struct vpx_read_bit_buffer *rb, int max) {
80 const int data = vpx_rb_read_literal(rb, get_unsigned_bits(max));
81 return data > max ? max : data;
84 static TX_MODE read_tx_mode(vpx_reader *r) {
85 TX_MODE tx_mode = vpx_read_literal(r, 2);
86 if (tx_mode == ALLOW_32X32)
87 tx_mode += vpx_read_bit(r);
88 return tx_mode;
91 static void read_tx_mode_probs(struct tx_probs *tx_probs, vpx_reader *r) {
92 int i, j;
94 for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
95 for (j = 0; j < TX_SIZES - 3; ++j)
96 vp9_diff_update_prob(r, &tx_probs->p8x8[i][j]);
98 for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
99 for (j = 0; j < TX_SIZES - 2; ++j)
100 vp9_diff_update_prob(r, &tx_probs->p16x16[i][j]);
102 for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
103 for (j = 0; j < TX_SIZES - 1; ++j)
104 vp9_diff_update_prob(r, &tx_probs->p32x32[i][j]);
107 static void read_switchable_interp_probs(FRAME_CONTEXT *fc, vpx_reader *r) {
108 int i, j;
109 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
110 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
111 vp9_diff_update_prob(r, &fc->switchable_interp_prob[j][i]);
114 static void read_inter_mode_probs(FRAME_CONTEXT *fc, vpx_reader *r) {
115 int i, j;
116 for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
117 for (j = 0; j < INTER_MODES - 1; ++j)
118 vp9_diff_update_prob(r, &fc->inter_mode_probs[i][j]);
121 static REFERENCE_MODE read_frame_reference_mode(const VP9_COMMON *cm,
122 vpx_reader *r) {
123 if (is_compound_reference_allowed(cm)) {
124 return vpx_read_bit(r) ? (vpx_read_bit(r) ? REFERENCE_MODE_SELECT
125 : COMPOUND_REFERENCE)
126 : SINGLE_REFERENCE;
127 } else {
128 return SINGLE_REFERENCE;
132 static void read_frame_reference_mode_probs(VP9_COMMON *cm, vpx_reader *r) {
133 FRAME_CONTEXT *const fc = cm->fc;
134 int i;
136 if (cm->reference_mode == REFERENCE_MODE_SELECT)
137 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
138 vp9_diff_update_prob(r, &fc->comp_inter_prob[i]);
140 if (cm->reference_mode != COMPOUND_REFERENCE)
141 for (i = 0; i < REF_CONTEXTS; ++i) {
142 vp9_diff_update_prob(r, &fc->single_ref_prob[i][0]);
143 vp9_diff_update_prob(r, &fc->single_ref_prob[i][1]);
146 if (cm->reference_mode != SINGLE_REFERENCE)
147 for (i = 0; i < REF_CONTEXTS; ++i)
148 vp9_diff_update_prob(r, &fc->comp_ref_prob[i]);
151 static void update_mv_probs(vpx_prob *p, int n, vpx_reader *r) {
152 int i;
153 for (i = 0; i < n; ++i)
154 if (vpx_read(r, MV_UPDATE_PROB))
155 p[i] = (vpx_read_literal(r, 7) << 1) | 1;
158 static void read_mv_probs(nmv_context *ctx, int allow_hp, vpx_reader *r) {
159 int i, j;
161 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
163 for (i = 0; i < 2; ++i) {
164 nmv_component *const comp_ctx = &ctx->comps[i];
165 update_mv_probs(&comp_ctx->sign, 1, r);
166 update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
167 update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
168 update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
171 for (i = 0; i < 2; ++i) {
172 nmv_component *const comp_ctx = &ctx->comps[i];
173 for (j = 0; j < CLASS0_SIZE; ++j)
174 update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
175 update_mv_probs(comp_ctx->fp, 3, r);
178 if (allow_hp) {
179 for (i = 0; i < 2; ++i) {
180 nmv_component *const comp_ctx = &ctx->comps[i];
181 update_mv_probs(&comp_ctx->class0_hp, 1, r);
182 update_mv_probs(&comp_ctx->hp, 1, r);
187 static void inverse_transform_block_inter(MACROBLOCKD* xd, int plane,
188 const TX_SIZE tx_size,
189 uint8_t *dst, int stride,
190 int eob) {
191 struct macroblockd_plane *const pd = &xd->plane[plane];
192 if (eob > 0) {
193 tran_low_t *const dqcoeff = pd->dqcoeff;
194 #if CONFIG_VP9_HIGHBITDEPTH
195 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
196 if (xd->lossless) {
197 vp9_highbd_iwht4x4_add(dqcoeff, dst, stride, eob, xd->bd);
198 } else {
199 switch (tx_size) {
200 case TX_4X4:
201 vp9_highbd_idct4x4_add(dqcoeff, dst, stride, eob, xd->bd);
202 break;
203 case TX_8X8:
204 vp9_highbd_idct8x8_add(dqcoeff, dst, stride, eob, xd->bd);
205 break;
206 case TX_16X16:
207 vp9_highbd_idct16x16_add(dqcoeff, dst, stride, eob, xd->bd);
208 break;
209 case TX_32X32:
210 vp9_highbd_idct32x32_add(dqcoeff, dst, stride, eob, xd->bd);
211 break;
212 default:
213 assert(0 && "Invalid transform size");
216 } else {
217 if (xd->lossless) {
218 vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
219 } else {
220 switch (tx_size) {
221 case TX_4X4:
222 vp9_idct4x4_add(dqcoeff, dst, stride, eob);
223 break;
224 case TX_8X8:
225 vp9_idct8x8_add(dqcoeff, dst, stride, eob);
226 break;
227 case TX_16X16:
228 vp9_idct16x16_add(dqcoeff, dst, stride, eob);
229 break;
230 case TX_32X32:
231 vp9_idct32x32_add(dqcoeff, dst, stride, eob);
232 break;
233 default:
234 assert(0 && "Invalid transform size");
235 return;
239 #else
240 if (xd->lossless) {
241 vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
242 } else {
243 switch (tx_size) {
244 case TX_4X4:
245 vp9_idct4x4_add(dqcoeff, dst, stride, eob);
246 break;
247 case TX_8X8:
248 vp9_idct8x8_add(dqcoeff, dst, stride, eob);
249 break;
250 case TX_16X16:
251 vp9_idct16x16_add(dqcoeff, dst, stride, eob);
252 break;
253 case TX_32X32:
254 vp9_idct32x32_add(dqcoeff, dst, stride, eob);
255 break;
256 default:
257 assert(0 && "Invalid transform size");
258 return;
261 #endif // CONFIG_VP9_HIGHBITDEPTH
263 if (eob == 1) {
264 dqcoeff[0] = 0;
265 } else {
266 if (tx_size <= TX_16X16 && eob <= 10)
267 memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
268 else if (tx_size == TX_32X32 && eob <= 34)
269 memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
270 else
271 memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
276 static void inverse_transform_block_intra(MACROBLOCKD* xd, int plane,
277 const TX_TYPE tx_type,
278 const TX_SIZE tx_size,
279 uint8_t *dst, int stride,
280 int eob) {
281 struct macroblockd_plane *const pd = &xd->plane[plane];
282 if (eob > 0) {
283 tran_low_t *const dqcoeff = pd->dqcoeff;
284 #if CONFIG_VP9_HIGHBITDEPTH
285 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
286 if (xd->lossless) {
287 vp9_highbd_iwht4x4_add(dqcoeff, dst, stride, eob, xd->bd);
288 } else {
289 switch (tx_size) {
290 case TX_4X4:
291 vp9_highbd_iht4x4_add(tx_type, dqcoeff, dst, stride, eob, xd->bd);
292 break;
293 case TX_8X8:
294 vp9_highbd_iht8x8_add(tx_type, dqcoeff, dst, stride, eob, xd->bd);
295 break;
296 case TX_16X16:
297 vp9_highbd_iht16x16_add(tx_type, dqcoeff, dst, stride, eob, xd->bd);
298 break;
299 case TX_32X32:
300 vp9_highbd_idct32x32_add(dqcoeff, dst, stride, eob, xd->bd);
301 break;
302 default:
303 assert(0 && "Invalid transform size");
306 } else {
307 if (xd->lossless) {
308 vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
309 } else {
310 switch (tx_size) {
311 case TX_4X4:
312 vp9_iht4x4_add(tx_type, dqcoeff, dst, stride, eob);
313 break;
314 case TX_8X8:
315 vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob);
316 break;
317 case TX_16X16:
318 vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob);
319 break;
320 case TX_32X32:
321 vp9_idct32x32_add(dqcoeff, dst, stride, eob);
322 break;
323 default:
324 assert(0 && "Invalid transform size");
325 return;
329 #else
330 if (xd->lossless) {
331 vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
332 } else {
333 switch (tx_size) {
334 case TX_4X4:
335 vp9_iht4x4_add(tx_type, dqcoeff, dst, stride, eob);
336 break;
337 case TX_8X8:
338 vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob);
339 break;
340 case TX_16X16:
341 vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob);
342 break;
343 case TX_32X32:
344 vp9_idct32x32_add(dqcoeff, dst, stride, eob);
345 break;
346 default:
347 assert(0 && "Invalid transform size");
348 return;
351 #endif // CONFIG_VP9_HIGHBITDEPTH
353 if (eob == 1) {
354 dqcoeff[0] = 0;
355 } else {
356 if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
357 memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
358 else if (tx_size == TX_32X32 && eob <= 34)
359 memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
360 else
361 memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
366 static void predict_and_reconstruct_intra_block(MACROBLOCKD *const xd,
367 vpx_reader *r,
368 MODE_INFO *const mi,
369 int plane,
370 int row, int col,
371 TX_SIZE tx_size) {
372 struct macroblockd_plane *const pd = &xd->plane[plane];
373 PREDICTION_MODE mode = (plane == 0) ? mi->mode : mi->uv_mode;
374 uint8_t *dst;
375 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
377 if (mi->sb_type < BLOCK_8X8)
378 if (plane == 0)
379 mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
381 vp9_predict_intra_block(xd, pd->n4_wl, tx_size, mode,
382 dst, pd->dst.stride, dst, pd->dst.stride,
383 col, row, plane);
385 if (!mi->skip) {
386 const TX_TYPE tx_type = (plane || xd->lossless) ?
387 DCT_DCT : intra_mode_to_tx_type_lookup[mode];
388 const scan_order *sc = (plane || xd->lossless) ?
389 &vp9_default_scan_orders[tx_size] : &vp9_scan_orders[tx_size][tx_type];
390 const int eob = vp9_decode_block_tokens(xd, plane, sc, col, row, tx_size,
391 r, mi->segment_id);
392 inverse_transform_block_intra(xd, plane, tx_type, tx_size,
393 dst, pd->dst.stride, eob);
397 static int reconstruct_inter_block(MACROBLOCKD *const xd, vpx_reader *r,
398 MODE_INFO *const mi, int plane,
399 int row, int col, TX_SIZE tx_size) {
400 struct macroblockd_plane *const pd = &xd->plane[plane];
401 const scan_order *sc = &vp9_default_scan_orders[tx_size];
402 const int eob = vp9_decode_block_tokens(xd, plane, sc, col, row, tx_size, r,
403 mi->segment_id);
405 inverse_transform_block_inter(xd, plane, tx_size,
406 &pd->dst.buf[4 * row * pd->dst.stride + 4 * col],
407 pd->dst.stride, eob);
408 return eob;
411 static void build_mc_border(const uint8_t *src, int src_stride,
412 uint8_t *dst, int dst_stride,
413 int x, int y, int b_w, int b_h, int w, int h) {
414 // Get a pointer to the start of the real data for this row.
415 const uint8_t *ref_row = src - x - y * src_stride;
417 if (y >= h)
418 ref_row += (h - 1) * src_stride;
419 else if (y > 0)
420 ref_row += y * src_stride;
422 do {
423 int right = 0, copy;
424 int left = x < 0 ? -x : 0;
426 if (left > b_w)
427 left = b_w;
429 if (x + b_w > w)
430 right = x + b_w - w;
432 if (right > b_w)
433 right = b_w;
435 copy = b_w - left - right;
437 if (left)
438 memset(dst, ref_row[0], left);
440 if (copy)
441 memcpy(dst + left, ref_row + x + left, copy);
443 if (right)
444 memset(dst + left + copy, ref_row[w - 1], right);
446 dst += dst_stride;
447 ++y;
449 if (y > 0 && y < h)
450 ref_row += src_stride;
451 } while (--b_h);
454 #if CONFIG_VP9_HIGHBITDEPTH
455 static void high_build_mc_border(const uint8_t *src8, int src_stride,
456 uint16_t *dst, int dst_stride,
457 int x, int y, int b_w, int b_h,
458 int w, int h) {
459 // Get a pointer to the start of the real data for this row.
460 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
461 const uint16_t *ref_row = src - x - y * src_stride;
463 if (y >= h)
464 ref_row += (h - 1) * src_stride;
465 else if (y > 0)
466 ref_row += y * src_stride;
468 do {
469 int right = 0, copy;
470 int left = x < 0 ? -x : 0;
472 if (left > b_w)
473 left = b_w;
475 if (x + b_w > w)
476 right = x + b_w - w;
478 if (right > b_w)
479 right = b_w;
481 copy = b_w - left - right;
483 if (left)
484 vpx_memset16(dst, ref_row[0], left);
486 if (copy)
487 memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t));
489 if (right)
490 vpx_memset16(dst + left + copy, ref_row[w - 1], right);
492 dst += dst_stride;
493 ++y;
495 if (y > 0 && y < h)
496 ref_row += src_stride;
497 } while (--b_h);
499 #endif // CONFIG_VP9_HIGHBITDEPTH
501 #if CONFIG_VP9_HIGHBITDEPTH
502 static void extend_and_predict(const uint8_t *buf_ptr1, int pre_buf_stride,
503 int x0, int y0, int b_w, int b_h,
504 int frame_width, int frame_height,
505 int border_offset,
506 uint8_t *const dst, int dst_buf_stride,
507 int subpel_x, int subpel_y,
508 const InterpKernel *kernel,
509 const struct scale_factors *sf,
510 MACROBLOCKD *xd,
511 int w, int h, int ref, int xs, int ys) {
512 DECLARE_ALIGNED(16, uint16_t, mc_buf_high[80 * 2 * 80 * 2]);
513 const uint8_t *buf_ptr;
515 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
516 high_build_mc_border(buf_ptr1, pre_buf_stride, mc_buf_high, b_w,
517 x0, y0, b_w, b_h, frame_width, frame_height);
518 buf_ptr = CONVERT_TO_BYTEPTR(mc_buf_high) + border_offset;
519 } else {
520 build_mc_border(buf_ptr1, pre_buf_stride, (uint8_t *)mc_buf_high, b_w,
521 x0, y0, b_w, b_h, frame_width, frame_height);
522 buf_ptr = ((uint8_t *)mc_buf_high) + border_offset;
525 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
526 high_inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x,
527 subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
528 } else {
529 inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x,
530 subpel_y, sf, w, h, ref, kernel, xs, ys);
533 #else
534 static void extend_and_predict(const uint8_t *buf_ptr1, int pre_buf_stride,
535 int x0, int y0, int b_w, int b_h,
536 int frame_width, int frame_height,
537 int border_offset,
538 uint8_t *const dst, int dst_buf_stride,
539 int subpel_x, int subpel_y,
540 const InterpKernel *kernel,
541 const struct scale_factors *sf,
542 int w, int h, int ref, int xs, int ys) {
543 DECLARE_ALIGNED(16, uint8_t, mc_buf[80 * 2 * 80 * 2]);
544 const uint8_t *buf_ptr;
546 build_mc_border(buf_ptr1, pre_buf_stride, mc_buf, b_w,
547 x0, y0, b_w, b_h, frame_width, frame_height);
548 buf_ptr = mc_buf + border_offset;
550 inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x,
551 subpel_y, sf, w, h, ref, kernel, xs, ys);
553 #endif // CONFIG_VP9_HIGHBITDEPTH
555 static void dec_build_inter_predictors(VPxWorker *const worker, MACROBLOCKD *xd,
556 int plane, int bw, int bh, int x,
557 int y, int w, int h, int mi_x, int mi_y,
558 const InterpKernel *kernel,
559 const struct scale_factors *sf,
560 struct buf_2d *pre_buf,
561 struct buf_2d *dst_buf, const MV* mv,
562 RefCntBuffer *ref_frame_buf,
563 int is_scaled, int ref) {
564 struct macroblockd_plane *const pd = &xd->plane[plane];
565 uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
566 MV32 scaled_mv;
567 int xs, ys, x0, y0, x0_16, y0_16, frame_width, frame_height,
568 buf_stride, subpel_x, subpel_y;
569 uint8_t *ref_frame, *buf_ptr;
571 // Get reference frame pointer, width and height.
572 if (plane == 0) {
573 frame_width = ref_frame_buf->buf.y_crop_width;
574 frame_height = ref_frame_buf->buf.y_crop_height;
575 ref_frame = ref_frame_buf->buf.y_buffer;
576 } else {
577 frame_width = ref_frame_buf->buf.uv_crop_width;
578 frame_height = ref_frame_buf->buf.uv_crop_height;
579 ref_frame = plane == 1 ? ref_frame_buf->buf.u_buffer
580 : ref_frame_buf->buf.v_buffer;
583 if (is_scaled) {
584 const MV mv_q4 = clamp_mv_to_umv_border_sb(xd, mv, bw, bh,
585 pd->subsampling_x,
586 pd->subsampling_y);
587 // Co-ordinate of containing block to pixel precision.
588 int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
589 int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y));
590 #if CONFIG_BETTER_HW_COMPATIBILITY
591 assert(xd->mi[0]->sb_type != BLOCK_4X8 &&
592 xd->mi[0]->sb_type != BLOCK_8X4);
593 assert(mv_q4.row == mv->row * (1 << (1 - pd->subsampling_y)) &&
594 mv_q4.col == mv->col * (1 << (1 - pd->subsampling_x)));
595 #endif
596 // Co-ordinate of the block to 1/16th pixel precision.
597 x0_16 = (x_start + x) << SUBPEL_BITS;
598 y0_16 = (y_start + y) << SUBPEL_BITS;
600 // Co-ordinate of current block in reference frame
601 // to 1/16th pixel precision.
602 x0_16 = sf->scale_value_x(x0_16, sf);
603 y0_16 = sf->scale_value_y(y0_16, sf);
605 // Map the top left corner of the block into the reference frame.
606 x0 = sf->scale_value_x(x_start + x, sf);
607 y0 = sf->scale_value_y(y_start + y, sf);
609 // Scale the MV and incorporate the sub-pixel offset of the block
610 // in the reference frame.
611 scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
612 xs = sf->x_step_q4;
613 ys = sf->y_step_q4;
614 } else {
615 // Co-ordinate of containing block to pixel precision.
616 x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
617 y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
619 // Co-ordinate of the block to 1/16th pixel precision.
620 x0_16 = x0 << SUBPEL_BITS;
621 y0_16 = y0 << SUBPEL_BITS;
623 scaled_mv.row = mv->row * (1 << (1 - pd->subsampling_y));
624 scaled_mv.col = mv->col * (1 << (1 - pd->subsampling_x));
625 xs = ys = 16;
627 subpel_x = scaled_mv.col & SUBPEL_MASK;
628 subpel_y = scaled_mv.row & SUBPEL_MASK;
630 // Calculate the top left corner of the best matching block in the
631 // reference frame.
632 x0 += scaled_mv.col >> SUBPEL_BITS;
633 y0 += scaled_mv.row >> SUBPEL_BITS;
634 x0_16 += scaled_mv.col;
635 y0_16 += scaled_mv.row;
637 // Get reference block pointer.
638 buf_ptr = ref_frame + y0 * pre_buf->stride + x0;
639 buf_stride = pre_buf->stride;
641 // Do border extension if there is motion or the
642 // width/height is not a multiple of 8 pixels.
643 if (is_scaled || scaled_mv.col || scaled_mv.row ||
644 (frame_width & 0x7) || (frame_height & 0x7)) {
645 int y1 = ((y0_16 + (h - 1) * ys) >> SUBPEL_BITS) + 1;
647 // Get reference block bottom right horizontal coordinate.
648 int x1 = ((x0_16 + (w - 1) * xs) >> SUBPEL_BITS) + 1;
649 int x_pad = 0, y_pad = 0;
651 if (subpel_x || (sf->x_step_q4 != SUBPEL_SHIFTS)) {
652 x0 -= VP9_INTERP_EXTEND - 1;
653 x1 += VP9_INTERP_EXTEND;
654 x_pad = 1;
657 if (subpel_y || (sf->y_step_q4 != SUBPEL_SHIFTS)) {
658 y0 -= VP9_INTERP_EXTEND - 1;
659 y1 += VP9_INTERP_EXTEND;
660 y_pad = 1;
663 // Wait until reference block is ready. Pad 7 more pixels as last 7
664 // pixels of each superblock row can be changed by next superblock row.
665 if (worker != NULL)
666 vp9_frameworker_wait(worker, ref_frame_buf,
667 VPXMAX(0, (y1 + 7)) << (plane == 0 ? 0 : 1));
669 // Skip border extension if block is inside the frame.
670 if (x0 < 0 || x0 > frame_width - 1 || x1 < 0 || x1 > frame_width - 1 ||
671 y0 < 0 || y0 > frame_height - 1 || y1 < 0 || y1 > frame_height - 1) {
672 // Extend the border.
673 const uint8_t *const buf_ptr1 = ref_frame + y0 * buf_stride + x0;
674 const int b_w = x1 - x0 + 1;
675 const int b_h = y1 - y0 + 1;
676 const int border_offset = y_pad * 3 * b_w + x_pad * 3;
678 extend_and_predict(buf_ptr1, buf_stride, x0, y0, b_w, b_h,
679 frame_width, frame_height, border_offset,
680 dst, dst_buf->stride,
681 subpel_x, subpel_y,
682 kernel, sf,
683 #if CONFIG_VP9_HIGHBITDEPTH
685 #endif
686 w, h, ref, xs, ys);
687 return;
689 } else {
690 // Wait until reference block is ready. Pad 7 more pixels as last 7
691 // pixels of each superblock row can be changed by next superblock row.
692 if (worker != NULL) {
693 const int y1 = (y0_16 + (h - 1) * ys) >> SUBPEL_BITS;
694 vp9_frameworker_wait(worker, ref_frame_buf,
695 VPXMAX(0, (y1 + 7)) << (plane == 0 ? 0 : 1));
698 #if CONFIG_VP9_HIGHBITDEPTH
699 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
700 high_inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
701 subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
702 } else {
703 inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
704 subpel_y, sf, w, h, ref, kernel, xs, ys);
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 #endif // CONFIG_VP9_HIGHBITDEPTH
712 static void dec_build_inter_predictors_sb(VP9Decoder *const pbi,
713 MACROBLOCKD *xd,
714 int mi_row, int mi_col) {
715 int plane;
716 const int mi_x = mi_col * MI_SIZE;
717 const int mi_y = mi_row * MI_SIZE;
718 const MODE_INFO *mi = xd->mi[0];
719 const InterpKernel *kernel = vp9_filter_kernels[mi->interp_filter];
720 const BLOCK_SIZE sb_type = mi->sb_type;
721 const int is_compound = has_second_ref(mi);
722 int ref;
723 int is_scaled;
724 VPxWorker *const fwo = pbi->frame_parallel_decode ?
725 pbi->frame_worker_owner : NULL;
727 for (ref = 0; ref < 1 + is_compound; ++ref) {
728 const MV_REFERENCE_FRAME frame = mi->ref_frame[ref];
729 RefBuffer *ref_buf = &pbi->common.frame_refs[frame - LAST_FRAME];
730 const struct scale_factors *const sf = &ref_buf->sf;
731 const int idx = ref_buf->idx;
732 BufferPool *const pool = pbi->common.buffer_pool;
733 RefCntBuffer *const ref_frame_buf = &pool->frame_bufs[idx];
735 if (!vp9_is_valid_scale(sf))
736 vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
737 "Reference frame has invalid dimensions");
739 is_scaled = vp9_is_scaled(sf);
740 vp9_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
741 is_scaled ? sf : NULL);
742 xd->block_refs[ref] = ref_buf;
744 if (sb_type < BLOCK_8X8) {
745 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
746 struct macroblockd_plane *const pd = &xd->plane[plane];
747 struct buf_2d *const dst_buf = &pd->dst;
748 const int num_4x4_w = pd->n4_w;
749 const int num_4x4_h = pd->n4_h;
750 const int n4w_x4 = 4 * num_4x4_w;
751 const int n4h_x4 = 4 * num_4x4_h;
752 struct buf_2d *const pre_buf = &pd->pre[ref];
753 int i = 0, x, y;
754 for (y = 0; y < num_4x4_h; ++y) {
755 for (x = 0; x < num_4x4_w; ++x) {
756 const MV mv = average_split_mvs(pd, mi, ref, i++);
757 dec_build_inter_predictors(fwo, xd, plane, n4w_x4, n4h_x4,
758 4 * x, 4 * y, 4, 4, mi_x, mi_y, kernel,
759 sf, pre_buf, dst_buf, &mv,
760 ref_frame_buf, is_scaled, ref);
764 } else {
765 const MV mv = mi->mv[ref].as_mv;
766 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
767 struct macroblockd_plane *const pd = &xd->plane[plane];
768 struct buf_2d *const dst_buf = &pd->dst;
769 const int num_4x4_w = pd->n4_w;
770 const int num_4x4_h = pd->n4_h;
771 const int n4w_x4 = 4 * num_4x4_w;
772 const int n4h_x4 = 4 * num_4x4_h;
773 struct buf_2d *const pre_buf = &pd->pre[ref];
774 dec_build_inter_predictors(fwo, xd, plane, n4w_x4, n4h_x4,
775 0, 0, n4w_x4, n4h_x4, mi_x, mi_y, kernel,
776 sf, pre_buf, dst_buf, &mv,
777 ref_frame_buf, is_scaled, ref);
783 static INLINE TX_SIZE dec_get_uv_tx_size(const MODE_INFO *mi,
784 int n4_wl, int n4_hl) {
785 // get minimum log2 num4x4s dimension
786 const int x = VPXMIN(n4_wl, n4_hl);
787 return VPXMIN(mi->tx_size, x);
790 static INLINE void dec_reset_skip_context(MACROBLOCKD *xd) {
791 int i;
792 for (i = 0; i < MAX_MB_PLANE; i++) {
793 struct macroblockd_plane *const pd = &xd->plane[i];
794 memset(pd->above_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_w);
795 memset(pd->left_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_h);
799 static void set_plane_n4(MACROBLOCKD *const xd, int bw, int bh, int bwl,
800 int bhl) {
801 int i;
802 for (i = 0; i < MAX_MB_PLANE; i++) {
803 xd->plane[i].n4_w = (bw << 1) >> xd->plane[i].subsampling_x;
804 xd->plane[i].n4_h = (bh << 1) >> xd->plane[i].subsampling_y;
805 xd->plane[i].n4_wl = bwl - xd->plane[i].subsampling_x;
806 xd->plane[i].n4_hl = bhl - xd->plane[i].subsampling_y;
810 static MODE_INFO *set_offsets(VP9_COMMON *const cm, MACROBLOCKD *const xd,
811 BLOCK_SIZE bsize, int mi_row, int mi_col,
812 int bw, int bh, int x_mis, int y_mis,
813 int bwl, int bhl) {
814 const int offset = mi_row * cm->mi_stride + mi_col;
815 int x, y;
816 const TileInfo *const tile = &xd->tile;
818 xd->mi = cm->mi_grid_visible + offset;
819 xd->mi[0] = &cm->mi[offset];
820 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
821 // passing bsize from decode_partition().
822 xd->mi[0]->sb_type = bsize;
823 for (y = 0; y < y_mis; ++y)
824 for (x = !y; x < x_mis; ++x) {
825 xd->mi[y * cm->mi_stride + x] = xd->mi[0];
828 set_plane_n4(xd, bw, bh, bwl, bhl);
830 set_skip_context(xd, mi_row, mi_col);
832 // Distance of Mb to the various image edges. These are specified to 8th pel
833 // as they are always compared to values that are in 1/8th pel units
834 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
836 vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
837 return xd->mi[0];
840 static void decode_block(VP9Decoder *const pbi, MACROBLOCKD *const xd,
841 int mi_row, int mi_col,
842 vpx_reader *r, BLOCK_SIZE bsize,
843 int bwl, int bhl) {
844 VP9_COMMON *const cm = &pbi->common;
845 const int less8x8 = bsize < BLOCK_8X8;
846 const int bw = 1 << (bwl - 1);
847 const int bh = 1 << (bhl - 1);
848 const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
849 const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
851 MODE_INFO *mi = set_offsets(cm, xd, bsize, mi_row, mi_col,
852 bw, bh, x_mis, y_mis, bwl, bhl);
854 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
855 const BLOCK_SIZE uv_subsize =
856 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
857 if (uv_subsize == BLOCK_INVALID)
858 vpx_internal_error(xd->error_info,
859 VPX_CODEC_CORRUPT_FRAME, "Invalid block size.");
862 vp9_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
864 if (mi->skip) {
865 dec_reset_skip_context(xd);
868 if (!is_inter_block(mi)) {
869 int plane;
870 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
871 const struct macroblockd_plane *const pd = &xd->plane[plane];
872 const TX_SIZE tx_size =
873 plane ? dec_get_uv_tx_size(mi, pd->n4_wl, pd->n4_hl)
874 : mi->tx_size;
875 const int num_4x4_w = pd->n4_w;
876 const int num_4x4_h = pd->n4_h;
877 const int step = (1 << tx_size);
878 int row, col;
879 const int max_blocks_wide = num_4x4_w + (xd->mb_to_right_edge >= 0 ?
880 0 : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
881 const int max_blocks_high = num_4x4_h + (xd->mb_to_bottom_edge >= 0 ?
882 0 : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
884 for (row = 0; row < max_blocks_high; row += step)
885 for (col = 0; col < max_blocks_wide; col += step)
886 predict_and_reconstruct_intra_block(xd, r, mi, plane,
887 row, col, tx_size);
889 } else {
890 // Prediction
891 dec_build_inter_predictors_sb(pbi, xd, mi_row, mi_col);
893 // Reconstruction
894 if (!mi->skip) {
895 int eobtotal = 0;
896 int plane;
898 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
899 const struct macroblockd_plane *const pd = &xd->plane[plane];
900 const TX_SIZE tx_size =
901 plane ? dec_get_uv_tx_size(mi, pd->n4_wl, pd->n4_hl)
902 : mi->tx_size;
903 const int num_4x4_w = pd->n4_w;
904 const int num_4x4_h = pd->n4_h;
905 const int step = (1 << tx_size);
906 int row, col;
907 const int max_blocks_wide = num_4x4_w + (xd->mb_to_right_edge >= 0 ?
908 0 : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
909 const int max_blocks_high = num_4x4_h + (xd->mb_to_bottom_edge >= 0 ?
910 0 : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
912 for (row = 0; row < max_blocks_high; row += step)
913 for (col = 0; col < max_blocks_wide; col += step)
914 eobtotal += reconstruct_inter_block(xd, r, mi, plane, row, col,
915 tx_size);
918 if (!less8x8 && eobtotal == 0)
919 mi->skip = 1; // skip loopfilter
923 xd->corrupted |= vpx_reader_has_error(r);
925 if (cm->lf.filter_level) {
926 vp9_build_mask(cm, mi, mi_row, mi_col, bw, bh);
930 static INLINE int dec_partition_plane_context(const MACROBLOCKD *xd,
931 int mi_row, int mi_col,
932 int bsl) {
933 const PARTITION_CONTEXT *above_ctx = xd->above_seg_context + mi_col;
934 const PARTITION_CONTEXT *left_ctx = xd->left_seg_context + (mi_row & MI_MASK);
935 int above = (*above_ctx >> bsl) & 1 , left = (*left_ctx >> bsl) & 1;
937 // assert(bsl >= 0);
939 return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
942 static INLINE void dec_update_partition_context(MACROBLOCKD *xd,
943 int mi_row, int mi_col,
944 BLOCK_SIZE subsize,
945 int bw) {
946 PARTITION_CONTEXT *const above_ctx = xd->above_seg_context + mi_col;
947 PARTITION_CONTEXT *const left_ctx = xd->left_seg_context + (mi_row & MI_MASK);
949 // update the partition context at the end notes. set partition bits
950 // of block sizes larger than the current one to be one, and partition
951 // bits of smaller block sizes to be zero.
952 memset(above_ctx, partition_context_lookup[subsize].above, bw);
953 memset(left_ctx, partition_context_lookup[subsize].left, bw);
956 static PARTITION_TYPE read_partition(MACROBLOCKD *xd, int mi_row, int mi_col,
957 vpx_reader *r,
958 int has_rows, int has_cols, int bsl) {
959 const int ctx = dec_partition_plane_context(xd, mi_row, mi_col, bsl);
960 const vpx_prob *const probs = get_partition_probs(xd, ctx);
961 FRAME_COUNTS *counts = xd->counts;
962 PARTITION_TYPE p;
964 if (has_rows && has_cols)
965 p = (PARTITION_TYPE)vpx_read_tree(r, vp9_partition_tree, probs);
966 else if (!has_rows && has_cols)
967 p = vpx_read(r, probs[1]) ? PARTITION_SPLIT : PARTITION_HORZ;
968 else if (has_rows && !has_cols)
969 p = vpx_read(r, probs[2]) ? PARTITION_SPLIT : PARTITION_VERT;
970 else
971 p = PARTITION_SPLIT;
973 if (counts)
974 ++counts->partition[ctx][p];
976 return p;
979 // TODO(slavarnway): eliminate bsize and subsize in future commits
980 static void decode_partition(VP9Decoder *const pbi, MACROBLOCKD *const xd,
981 int mi_row, int mi_col,
982 vpx_reader* r, BLOCK_SIZE bsize, int n4x4_l2) {
983 VP9_COMMON *const cm = &pbi->common;
984 const int n8x8_l2 = n4x4_l2 - 1;
985 const int num_8x8_wh = 1 << n8x8_l2;
986 const int hbs = num_8x8_wh >> 1;
987 PARTITION_TYPE partition;
988 BLOCK_SIZE subsize;
989 const int has_rows = (mi_row + hbs) < cm->mi_rows;
990 const int has_cols = (mi_col + hbs) < cm->mi_cols;
992 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
993 return;
995 partition = read_partition(xd, mi_row, mi_col, r, has_rows, has_cols,
996 n8x8_l2);
997 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
998 if (!hbs) {
999 // calculate bmode block dimensions (log 2)
1000 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1001 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1002 decode_block(pbi, xd, mi_row, mi_col, r, subsize, 1, 1);
1003 } else {
1004 switch (partition) {
1005 case PARTITION_NONE:
1006 decode_block(pbi, xd, mi_row, mi_col, r, subsize, n4x4_l2, n4x4_l2);
1007 break;
1008 case PARTITION_HORZ:
1009 decode_block(pbi, xd, mi_row, mi_col, r, subsize, n4x4_l2, n8x8_l2);
1010 if (has_rows)
1011 decode_block(pbi, xd, mi_row + hbs, mi_col, r, subsize, n4x4_l2,
1012 n8x8_l2);
1013 break;
1014 case PARTITION_VERT:
1015 decode_block(pbi, xd, mi_row, mi_col, r, subsize, n8x8_l2, n4x4_l2);
1016 if (has_cols)
1017 decode_block(pbi, xd, mi_row, mi_col + hbs, r, subsize, n8x8_l2,
1018 n4x4_l2);
1019 break;
1020 case PARTITION_SPLIT:
1021 decode_partition(pbi, xd, mi_row, mi_col, r, subsize, n8x8_l2);
1022 decode_partition(pbi, xd, mi_row, mi_col + hbs, r, subsize, n8x8_l2);
1023 decode_partition(pbi, xd, mi_row + hbs, mi_col, r, subsize, n8x8_l2);
1024 decode_partition(pbi, xd, mi_row + hbs, mi_col + hbs, r, subsize,
1025 n8x8_l2);
1026 break;
1027 default:
1028 assert(0 && "Invalid partition type");
1032 // update partition context
1033 if (bsize >= BLOCK_8X8 &&
1034 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
1035 dec_update_partition_context(xd, mi_row, mi_col, subsize, num_8x8_wh);
1038 static void setup_token_decoder(const uint8_t *data,
1039 const uint8_t *data_end,
1040 size_t read_size,
1041 struct vpx_internal_error_info *error_info,
1042 vpx_reader *r,
1043 vpx_decrypt_cb decrypt_cb,
1044 void *decrypt_state) {
1045 // Validate the calculated partition length. If the buffer
1046 // described by the partition can't be fully read, then restrict
1047 // it to the portion that can be (for EC mode) or throw an error.
1048 if (!read_is_valid(data, read_size, data_end))
1049 vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
1050 "Truncated packet or corrupt tile length");
1052 if (vpx_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
1053 vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR,
1054 "Failed to allocate bool decoder %d", 1);
1057 static void read_coef_probs_common(vp9_coeff_probs_model *coef_probs,
1058 vpx_reader *r) {
1059 int i, j, k, l, m;
1061 if (vpx_read_bit(r))
1062 for (i = 0; i < PLANE_TYPES; ++i)
1063 for (j = 0; j < REF_TYPES; ++j)
1064 for (k = 0; k < COEF_BANDS; ++k)
1065 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
1066 for (m = 0; m < UNCONSTRAINED_NODES; ++m)
1067 vp9_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
1070 static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode,
1071 vpx_reader *r) {
1072 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
1073 TX_SIZE tx_size;
1074 for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size)
1075 read_coef_probs_common(fc->coef_probs[tx_size], r);
1078 static void setup_segmentation(struct segmentation *seg,
1079 struct vpx_read_bit_buffer *rb) {
1080 int i, j;
1082 seg->update_map = 0;
1083 seg->update_data = 0;
1085 seg->enabled = vpx_rb_read_bit(rb);
1086 if (!seg->enabled)
1087 return;
1089 // Segmentation map update
1090 seg->update_map = vpx_rb_read_bit(rb);
1091 if (seg->update_map) {
1092 for (i = 0; i < SEG_TREE_PROBS; i++)
1093 seg->tree_probs[i] = vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8)
1094 : MAX_PROB;
1096 seg->temporal_update = vpx_rb_read_bit(rb);
1097 if (seg->temporal_update) {
1098 for (i = 0; i < PREDICTION_PROBS; i++)
1099 seg->pred_probs[i] = vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8)
1100 : MAX_PROB;
1101 } else {
1102 for (i = 0; i < PREDICTION_PROBS; i++)
1103 seg->pred_probs[i] = MAX_PROB;
1107 // Segmentation data update
1108 seg->update_data = vpx_rb_read_bit(rb);
1109 if (seg->update_data) {
1110 seg->abs_delta = vpx_rb_read_bit(rb);
1112 vp9_clearall_segfeatures(seg);
1114 for (i = 0; i < MAX_SEGMENTS; i++) {
1115 for (j = 0; j < SEG_LVL_MAX; j++) {
1116 int data = 0;
1117 const int feature_enabled = vpx_rb_read_bit(rb);
1118 if (feature_enabled) {
1119 vp9_enable_segfeature(seg, i, j);
1120 data = decode_unsigned_max(rb, vp9_seg_feature_data_max(j));
1121 if (vp9_is_segfeature_signed(j))
1122 data = vpx_rb_read_bit(rb) ? -data : data;
1124 vp9_set_segdata(seg, i, j, data);
1130 static void setup_loopfilter(struct loopfilter *lf,
1131 struct vpx_read_bit_buffer *rb) {
1132 lf->filter_level = vpx_rb_read_literal(rb, 6);
1133 lf->sharpness_level = vpx_rb_read_literal(rb, 3);
1135 // Read in loop filter deltas applied at the MB level based on mode or ref
1136 // frame.
1137 lf->mode_ref_delta_update = 0;
1139 lf->mode_ref_delta_enabled = vpx_rb_read_bit(rb);
1140 if (lf->mode_ref_delta_enabled) {
1141 lf->mode_ref_delta_update = vpx_rb_read_bit(rb);
1142 if (lf->mode_ref_delta_update) {
1143 int i;
1145 for (i = 0; i < MAX_REF_LF_DELTAS; i++)
1146 if (vpx_rb_read_bit(rb))
1147 lf->ref_deltas[i] = vpx_rb_read_signed_literal(rb, 6);
1149 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
1150 if (vpx_rb_read_bit(rb))
1151 lf->mode_deltas[i] = vpx_rb_read_signed_literal(rb, 6);
1156 static INLINE int read_delta_q(struct vpx_read_bit_buffer *rb) {
1157 return vpx_rb_read_bit(rb) ? vpx_rb_read_signed_literal(rb, 4) : 0;
1160 static void setup_quantization(VP9_COMMON *const cm, MACROBLOCKD *const xd,
1161 struct vpx_read_bit_buffer *rb) {
1162 cm->base_qindex = vpx_rb_read_literal(rb, QINDEX_BITS);
1163 cm->y_dc_delta_q = read_delta_q(rb);
1164 cm->uv_dc_delta_q = read_delta_q(rb);
1165 cm->uv_ac_delta_q = read_delta_q(rb);
1166 cm->dequant_bit_depth = cm->bit_depth;
1167 xd->lossless = cm->base_qindex == 0 &&
1168 cm->y_dc_delta_q == 0 &&
1169 cm->uv_dc_delta_q == 0 &&
1170 cm->uv_ac_delta_q == 0;
1172 #if CONFIG_VP9_HIGHBITDEPTH
1173 xd->bd = (int)cm->bit_depth;
1174 #endif
1177 static void setup_segmentation_dequant(VP9_COMMON *const cm) {
1178 // Build y/uv dequant values based on segmentation.
1179 if (cm->seg.enabled) {
1180 int i;
1181 for (i = 0; i < MAX_SEGMENTS; ++i) {
1182 const int qindex = vp9_get_qindex(&cm->seg, i, cm->base_qindex);
1183 cm->y_dequant[i][0] = vp9_dc_quant(qindex, cm->y_dc_delta_q,
1184 cm->bit_depth);
1185 cm->y_dequant[i][1] = vp9_ac_quant(qindex, 0, cm->bit_depth);
1186 cm->uv_dequant[i][0] = vp9_dc_quant(qindex, cm->uv_dc_delta_q,
1187 cm->bit_depth);
1188 cm->uv_dequant[i][1] = vp9_ac_quant(qindex, cm->uv_ac_delta_q,
1189 cm->bit_depth);
1191 } else {
1192 const int qindex = cm->base_qindex;
1193 // When segmentation is disabled, only the first value is used. The
1194 // remaining are don't cares.
1195 cm->y_dequant[0][0] = vp9_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
1196 cm->y_dequant[0][1] = vp9_ac_quant(qindex, 0, cm->bit_depth);
1197 cm->uv_dequant[0][0] = vp9_dc_quant(qindex, cm->uv_dc_delta_q,
1198 cm->bit_depth);
1199 cm->uv_dequant[0][1] = vp9_ac_quant(qindex, cm->uv_ac_delta_q,
1200 cm->bit_depth);
1204 static INTERP_FILTER read_interp_filter(struct vpx_read_bit_buffer *rb) {
1205 const INTERP_FILTER literal_to_filter[] = { EIGHTTAP_SMOOTH,
1206 EIGHTTAP,
1207 EIGHTTAP_SHARP,
1208 BILINEAR };
1209 return vpx_rb_read_bit(rb) ? SWITCHABLE
1210 : literal_to_filter[vpx_rb_read_literal(rb, 2)];
1213 static void setup_render_size(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1214 cm->render_width = cm->width;
1215 cm->render_height = cm->height;
1216 if (vpx_rb_read_bit(rb))
1217 vp9_read_frame_size(rb, &cm->render_width, &cm->render_height);
1220 static void resize_mv_buffer(VP9_COMMON *cm) {
1221 vpx_free(cm->cur_frame->mvs);
1222 cm->cur_frame->mi_rows = cm->mi_rows;
1223 cm->cur_frame->mi_cols = cm->mi_cols;
1224 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
1225 (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
1226 sizeof(*cm->cur_frame->mvs)));
1229 static void resize_context_buffers(VP9_COMMON *cm, int width, int height) {
1230 #if CONFIG_SIZE_LIMIT
1231 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
1232 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1233 "Dimensions of %dx%d beyond allowed size of %dx%d.",
1234 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
1235 #endif
1236 if (cm->width != width || cm->height != height) {
1237 const int new_mi_rows =
1238 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
1239 const int new_mi_cols =
1240 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
1242 // Allocations in vp9_alloc_context_buffers() depend on individual
1243 // dimensions as well as the overall size.
1244 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
1245 if (vp9_alloc_context_buffers(cm, width, height))
1246 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1247 "Failed to allocate context buffers");
1248 } else {
1249 vp9_set_mb_mi(cm, width, height);
1251 vp9_init_context_buffers(cm);
1252 cm->width = width;
1253 cm->height = height;
1255 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
1256 cm->mi_cols > cm->cur_frame->mi_cols) {
1257 resize_mv_buffer(cm);
1261 static void setup_frame_size(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1262 int width, height;
1263 BufferPool *const pool = cm->buffer_pool;
1264 vp9_read_frame_size(rb, &width, &height);
1265 resize_context_buffers(cm, width, height);
1266 setup_render_size(cm, rb);
1268 lock_buffer_pool(pool);
1269 if (vpx_realloc_frame_buffer(
1270 get_frame_new_buffer(cm), cm->width, cm->height,
1271 cm->subsampling_x, cm->subsampling_y,
1272 #if CONFIG_VP9_HIGHBITDEPTH
1273 cm->use_highbitdepth,
1274 #endif
1275 VP9_DEC_BORDER_IN_PIXELS,
1276 cm->byte_alignment,
1277 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
1278 pool->cb_priv)) {
1279 unlock_buffer_pool(pool);
1280 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1281 "Failed to allocate frame buffer");
1283 unlock_buffer_pool(pool);
1285 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
1286 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
1287 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
1288 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
1289 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
1290 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
1291 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
1294 static INLINE int valid_ref_frame_img_fmt(vpx_bit_depth_t ref_bit_depth,
1295 int ref_xss, int ref_yss,
1296 vpx_bit_depth_t this_bit_depth,
1297 int this_xss, int this_yss) {
1298 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
1299 ref_yss == this_yss;
1302 static void setup_frame_size_with_refs(VP9_COMMON *cm,
1303 struct vpx_read_bit_buffer *rb) {
1304 int width, height;
1305 int found = 0, i;
1306 int has_valid_ref_frame = 0;
1307 BufferPool *const pool = cm->buffer_pool;
1308 for (i = 0; i < REFS_PER_FRAME; ++i) {
1309 if (vpx_rb_read_bit(rb)) {
1310 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
1311 width = buf->y_crop_width;
1312 height = buf->y_crop_height;
1313 found = 1;
1314 break;
1318 if (!found)
1319 vp9_read_frame_size(rb, &width, &height);
1321 if (width <= 0 || height <= 0)
1322 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1323 "Invalid frame size");
1325 // Check to make sure at least one of frames that this frame references
1326 // has valid dimensions.
1327 for (i = 0; i < REFS_PER_FRAME; ++i) {
1328 RefBuffer *const ref_frame = &cm->frame_refs[i];
1329 has_valid_ref_frame |= valid_ref_frame_size(ref_frame->buf->y_crop_width,
1330 ref_frame->buf->y_crop_height,
1331 width, height);
1333 if (!has_valid_ref_frame)
1334 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1335 "Referenced frame has invalid size");
1336 for (i = 0; i < REFS_PER_FRAME; ++i) {
1337 RefBuffer *const ref_frame = &cm->frame_refs[i];
1338 if (!valid_ref_frame_img_fmt(
1339 ref_frame->buf->bit_depth,
1340 ref_frame->buf->subsampling_x,
1341 ref_frame->buf->subsampling_y,
1342 cm->bit_depth,
1343 cm->subsampling_x,
1344 cm->subsampling_y))
1345 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1346 "Referenced frame has incompatible color format");
1349 resize_context_buffers(cm, width, height);
1350 setup_render_size(cm, rb);
1352 lock_buffer_pool(pool);
1353 if (vpx_realloc_frame_buffer(
1354 get_frame_new_buffer(cm), cm->width, cm->height,
1355 cm->subsampling_x, cm->subsampling_y,
1356 #if CONFIG_VP9_HIGHBITDEPTH
1357 cm->use_highbitdepth,
1358 #endif
1359 VP9_DEC_BORDER_IN_PIXELS,
1360 cm->byte_alignment,
1361 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
1362 pool->cb_priv)) {
1363 unlock_buffer_pool(pool);
1364 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1365 "Failed to allocate frame buffer");
1367 unlock_buffer_pool(pool);
1369 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
1370 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
1371 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
1372 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
1373 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
1374 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
1375 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
1378 static void setup_tile_info(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1379 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
1380 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
1382 // columns
1383 max_ones = max_log2_tile_cols - min_log2_tile_cols;
1384 cm->log2_tile_cols = min_log2_tile_cols;
1385 while (max_ones-- && vpx_rb_read_bit(rb))
1386 cm->log2_tile_cols++;
1388 if (cm->log2_tile_cols > 6)
1389 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1390 "Invalid number of tile columns");
1392 // rows
1393 cm->log2_tile_rows = vpx_rb_read_bit(rb);
1394 if (cm->log2_tile_rows)
1395 cm->log2_tile_rows += vpx_rb_read_bit(rb);
1398 // Reads the next tile returning its size and adjusting '*data' accordingly
1399 // based on 'is_last'.
1400 static void get_tile_buffer(const uint8_t *const data_end,
1401 int is_last,
1402 struct vpx_internal_error_info *error_info,
1403 const uint8_t **data,
1404 vpx_decrypt_cb decrypt_cb, void *decrypt_state,
1405 TileBuffer *buf) {
1406 size_t size;
1408 if (!is_last) {
1409 if (!read_is_valid(*data, 4, data_end))
1410 vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
1411 "Truncated packet or corrupt tile length");
1413 if (decrypt_cb) {
1414 uint8_t be_data[4];
1415 decrypt_cb(decrypt_state, *data, be_data, 4);
1416 size = mem_get_be32(be_data);
1417 } else {
1418 size = mem_get_be32(*data);
1420 *data += 4;
1422 if (size > (size_t)(data_end - *data))
1423 vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
1424 "Truncated packet or corrupt tile size");
1425 } else {
1426 size = data_end - *data;
1429 buf->data = *data;
1430 buf->size = size;
1432 *data += size;
1435 static void get_tile_buffers(VP9Decoder *pbi,
1436 const uint8_t *data, const uint8_t *data_end,
1437 int tile_cols, int tile_rows,
1438 TileBuffer (*tile_buffers)[1 << 6]) {
1439 int r, c;
1441 for (r = 0; r < tile_rows; ++r) {
1442 for (c = 0; c < tile_cols; ++c) {
1443 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
1444 TileBuffer *const buf = &tile_buffers[r][c];
1445 buf->col = c;
1446 get_tile_buffer(data_end, is_last, &pbi->common.error, &data,
1447 pbi->decrypt_cb, pbi->decrypt_state, buf);
1452 static const uint8_t *decode_tiles(VP9Decoder *pbi,
1453 const uint8_t *data,
1454 const uint8_t *data_end) {
1455 VP9_COMMON *const cm = &pbi->common;
1456 const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
1457 const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
1458 const int tile_cols = 1 << cm->log2_tile_cols;
1459 const int tile_rows = 1 << cm->log2_tile_rows;
1460 TileBuffer tile_buffers[4][1 << 6];
1461 int tile_row, tile_col;
1462 int mi_row, mi_col;
1463 TileWorkerData *tile_data = NULL;
1465 if (cm->lf.filter_level && !cm->skip_loop_filter &&
1466 pbi->lf_worker.data1 == NULL) {
1467 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
1468 vpx_memalign(32, sizeof(LFWorkerData)));
1469 pbi->lf_worker.hook = (VPxWorkerHook)vp9_loop_filter_worker;
1470 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
1471 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
1472 "Loop filter thread creation failed");
1476 if (cm->lf.filter_level && !cm->skip_loop_filter) {
1477 LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
1478 // Be sure to sync as we might be resuming after a failed frame decode.
1479 winterface->sync(&pbi->lf_worker);
1480 vp9_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
1481 pbi->mb.plane);
1484 assert(tile_rows <= 4);
1485 assert(tile_cols <= (1 << 6));
1487 // Note: this memset assumes above_context[0], [1] and [2]
1488 // are allocated as part of the same buffer.
1489 memset(cm->above_context, 0,
1490 sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_cols);
1492 memset(cm->above_seg_context, 0,
1493 sizeof(*cm->above_seg_context) * aligned_cols);
1495 vp9_reset_lfm(cm);
1497 get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows, tile_buffers);
1499 // Load all tile information into tile_data.
1500 for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
1501 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
1502 const TileBuffer *const buf = &tile_buffers[tile_row][tile_col];
1503 tile_data = pbi->tile_worker_data + tile_cols * tile_row + tile_col;
1504 tile_data->xd = pbi->mb;
1505 tile_data->xd.corrupted = 0;
1506 tile_data->xd.counts =
1507 cm->frame_parallel_decoding_mode ? NULL : &cm->counts;
1508 vp9_zero(tile_data->dqcoeff);
1509 vp9_tile_init(&tile_data->xd.tile, cm, tile_row, tile_col);
1510 setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
1511 &tile_data->bit_reader, pbi->decrypt_cb,
1512 pbi->decrypt_state);
1513 vp9_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff);
1517 for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
1518 TileInfo tile;
1519 vp9_tile_set_row(&tile, cm, tile_row);
1520 for (mi_row = tile.mi_row_start; mi_row < tile.mi_row_end;
1521 mi_row += MI_BLOCK_SIZE) {
1522 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
1523 const int col = pbi->inv_tile_order ?
1524 tile_cols - tile_col - 1 : tile_col;
1525 tile_data = pbi->tile_worker_data + tile_cols * tile_row + col;
1526 vp9_tile_set_col(&tile, cm, col);
1527 vp9_zero(tile_data->xd.left_context);
1528 vp9_zero(tile_data->xd.left_seg_context);
1529 for (mi_col = tile.mi_col_start; mi_col < tile.mi_col_end;
1530 mi_col += MI_BLOCK_SIZE) {
1531 decode_partition(pbi, &tile_data->xd, mi_row,
1532 mi_col, &tile_data->bit_reader, BLOCK_64X64, 4);
1534 pbi->mb.corrupted |= tile_data->xd.corrupted;
1535 if (pbi->mb.corrupted)
1536 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1537 "Failed to decode tile data");
1539 // Loopfilter one row.
1540 if (cm->lf.filter_level && !cm->skip_loop_filter) {
1541 const int lf_start = mi_row - MI_BLOCK_SIZE;
1542 LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
1544 // delay the loopfilter by 1 macroblock row.
1545 if (lf_start < 0) continue;
1547 // decoding has completed: finish up the loop filter in this thread.
1548 if (mi_row + MI_BLOCK_SIZE >= cm->mi_rows) continue;
1550 winterface->sync(&pbi->lf_worker);
1551 lf_data->start = lf_start;
1552 lf_data->stop = mi_row;
1553 if (pbi->max_threads > 1) {
1554 winterface->launch(&pbi->lf_worker);
1555 } else {
1556 winterface->execute(&pbi->lf_worker);
1559 // After loopfiltering, the last 7 row pixels in each superblock row may
1560 // still be changed by the longest loopfilter of the next superblock
1561 // row.
1562 if (pbi->frame_parallel_decode)
1563 vp9_frameworker_broadcast(pbi->cur_buf,
1564 mi_row << MI_BLOCK_SIZE_LOG2);
1568 // Loopfilter remaining rows in the frame.
1569 if (cm->lf.filter_level && !cm->skip_loop_filter) {
1570 LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
1571 winterface->sync(&pbi->lf_worker);
1572 lf_data->start = lf_data->stop;
1573 lf_data->stop = cm->mi_rows;
1574 winterface->execute(&pbi->lf_worker);
1577 // Get last tile data.
1578 tile_data = pbi->tile_worker_data + tile_cols * tile_rows - 1;
1580 if (pbi->frame_parallel_decode)
1581 vp9_frameworker_broadcast(pbi->cur_buf, INT_MAX);
1582 return vpx_reader_find_end(&tile_data->bit_reader);
1585 // On entry 'tile_data->data_end' points to the end of the input frame, on exit
1586 // it is updated to reflect the bitreader position of the final tile column if
1587 // present in the tile buffer group or NULL otherwise.
1588 static int tile_worker_hook(TileWorkerData *const tile_data,
1589 VP9Decoder *const pbi) {
1590 TileInfo *volatile tile = &tile_data->xd.tile;
1591 const int final_col = (1 << pbi->common.log2_tile_cols) - 1;
1592 const uint8_t *volatile bit_reader_end = NULL;
1593 volatile int n = tile_data->buf_start;
1594 tile_data->error_info.setjmp = 1;
1596 if (setjmp(tile_data->error_info.jmp)) {
1597 tile_data->error_info.setjmp = 0;
1598 tile_data->xd.corrupted = 1;
1599 tile_data->data_end = NULL;
1600 return 0;
1603 tile_data->xd.error_info = &tile_data->error_info;
1604 tile_data->xd.corrupted = 0;
1606 do {
1607 int mi_row, mi_col;
1608 const TileBuffer *const buf = pbi->tile_buffers + n;
1609 vp9_zero(tile_data->dqcoeff);
1610 vp9_tile_init(tile, &pbi->common, 0, buf->col);
1611 setup_token_decoder(buf->data, tile_data->data_end, buf->size,
1612 &tile_data->error_info, &tile_data->bit_reader,
1613 pbi->decrypt_cb, pbi->decrypt_state);
1614 vp9_init_macroblockd(&pbi->common, &tile_data->xd, tile_data->dqcoeff);
1616 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
1617 mi_row += MI_BLOCK_SIZE) {
1618 vp9_zero(tile_data->xd.left_context);
1619 vp9_zero(tile_data->xd.left_seg_context);
1620 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
1621 mi_col += MI_BLOCK_SIZE) {
1622 decode_partition(pbi, &tile_data->xd, mi_row, mi_col,
1623 &tile_data->bit_reader, BLOCK_64X64, 4);
1627 if (buf->col == final_col) {
1628 bit_reader_end = vpx_reader_find_end(&tile_data->bit_reader);
1630 } while (!tile_data->xd.corrupted && ++n <= tile_data->buf_end);
1632 tile_data->data_end = bit_reader_end;
1633 return !tile_data->xd.corrupted;
1636 // sorts in descending order
1637 static int compare_tile_buffers(const void *a, const void *b) {
1638 const TileBuffer *const buf1 = (const TileBuffer*)a;
1639 const TileBuffer *const buf2 = (const TileBuffer*)b;
1640 return (int)(buf2->size - buf1->size);
1643 static const uint8_t *decode_tiles_mt(VP9Decoder *pbi,
1644 const uint8_t *data,
1645 const uint8_t *data_end) {
1646 VP9_COMMON *const cm = &pbi->common;
1647 const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
1648 const uint8_t *bit_reader_end = NULL;
1649 const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
1650 const int tile_cols = 1 << cm->log2_tile_cols;
1651 const int tile_rows = 1 << cm->log2_tile_rows;
1652 const int num_workers = VPXMIN(pbi->max_threads, tile_cols);
1653 int n;
1655 assert(tile_cols <= (1 << 6));
1656 assert(tile_rows == 1);
1657 (void)tile_rows;
1659 if (pbi->num_tile_workers == 0) {
1660 const int num_threads = pbi->max_threads;
1661 CHECK_MEM_ERROR(cm, pbi->tile_workers,
1662 vpx_malloc(num_threads * sizeof(*pbi->tile_workers)));
1663 for (n = 0; n < num_threads; ++n) {
1664 VPxWorker *const worker = &pbi->tile_workers[n];
1665 ++pbi->num_tile_workers;
1667 winterface->init(worker);
1668 if (n < num_threads - 1 && !winterface->reset(worker)) {
1669 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
1670 "Tile decoder thread creation failed");
1675 // Reset tile decoding hook
1676 for (n = 0; n < num_workers; ++n) {
1677 VPxWorker *const worker = &pbi->tile_workers[n];
1678 TileWorkerData *const tile_data =
1679 &pbi->tile_worker_data[n + pbi->total_tiles];
1680 winterface->sync(worker);
1681 tile_data->xd = pbi->mb;
1682 tile_data->xd.counts =
1683 cm->frame_parallel_decoding_mode ? NULL : &tile_data->counts;
1684 worker->hook = (VPxWorkerHook)tile_worker_hook;
1685 worker->data1 = tile_data;
1686 worker->data2 = pbi;
1689 // Note: this memset assumes above_context[0], [1] and [2]
1690 // are allocated as part of the same buffer.
1691 memset(cm->above_context, 0,
1692 sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_mi_cols);
1693 memset(cm->above_seg_context, 0,
1694 sizeof(*cm->above_seg_context) * aligned_mi_cols);
1696 vp9_reset_lfm(cm);
1698 // Load tile data into tile_buffers
1699 get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows,
1700 &pbi->tile_buffers);
1702 // Sort the buffers based on size in descending order.
1703 qsort(pbi->tile_buffers, tile_cols, sizeof(pbi->tile_buffers[0]),
1704 compare_tile_buffers);
1706 if (num_workers == tile_cols) {
1707 // Rearrange the tile buffers such that the largest, and
1708 // presumably the most difficult, tile will be decoded in the main thread.
1709 // This should help minimize the number of instances where the main thread
1710 // is waiting for a worker to complete.
1711 const TileBuffer largest = pbi->tile_buffers[0];
1712 memmove(pbi->tile_buffers, pbi->tile_buffers + 1,
1713 (tile_cols - 1) * sizeof(pbi->tile_buffers[0]));
1714 pbi->tile_buffers[tile_cols - 1] = largest;
1715 } else {
1716 int start = 0, end = tile_cols - 2;
1717 TileBuffer tmp;
1719 // Interleave the tiles to distribute the load between threads, assuming a
1720 // larger tile implies it is more difficult to decode.
1721 while (start < end) {
1722 tmp = pbi->tile_buffers[start];
1723 pbi->tile_buffers[start] = pbi->tile_buffers[end];
1724 pbi->tile_buffers[end] = tmp;
1725 start += 2;
1726 end -= 2;
1730 // Initialize thread frame counts.
1731 if (!cm->frame_parallel_decoding_mode) {
1732 for (n = 0; n < num_workers; ++n) {
1733 TileWorkerData *const tile_data =
1734 (TileWorkerData*)pbi->tile_workers[n].data1;
1735 vp9_zero(tile_data->counts);
1740 const int base = tile_cols / num_workers;
1741 const int remain = tile_cols % num_workers;
1742 int buf_start = 0;
1744 for (n = 0; n < num_workers; ++n) {
1745 const int count = base + (remain + n) / num_workers;
1746 VPxWorker *const worker = &pbi->tile_workers[n];
1747 TileWorkerData *const tile_data = (TileWorkerData*)worker->data1;
1749 tile_data->buf_start = buf_start;
1750 tile_data->buf_end = buf_start + count - 1;
1751 tile_data->data_end = data_end;
1752 buf_start += count;
1754 worker->had_error = 0;
1755 if (n == num_workers - 1) {
1756 assert(tile_data->buf_end == tile_cols - 1);
1757 winterface->execute(worker);
1758 } else {
1759 winterface->launch(worker);
1763 for (; n > 0; --n) {
1764 VPxWorker *const worker = &pbi->tile_workers[n - 1];
1765 TileWorkerData *const tile_data = (TileWorkerData*)worker->data1;
1766 // TODO(jzern): The tile may have specific error data associated with
1767 // its vpx_internal_error_info which could be propagated to the main info
1768 // in cm. Additionally once the threads have been synced and an error is
1769 // detected, there's no point in continuing to decode tiles.
1770 pbi->mb.corrupted |= !winterface->sync(worker);
1771 if (!bit_reader_end) bit_reader_end = tile_data->data_end;
1775 // Accumulate thread frame counts.
1776 if (!cm->frame_parallel_decoding_mode) {
1777 for (n = 0; n < num_workers; ++n) {
1778 TileWorkerData *const tile_data =
1779 (TileWorkerData*)pbi->tile_workers[n].data1;
1780 vp9_accumulate_frame_counts(&cm->counts, &tile_data->counts, 1);
1784 assert(bit_reader_end || pbi->mb.corrupted);
1785 return bit_reader_end;
1788 static void error_handler(void *data) {
1789 VP9_COMMON *const cm = (VP9_COMMON *)data;
1790 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, "Truncated packet");
1793 static void read_bitdepth_colorspace_sampling(
1794 VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1795 if (cm->profile >= PROFILE_2) {
1796 cm->bit_depth = vpx_rb_read_bit(rb) ? VPX_BITS_12 : VPX_BITS_10;
1797 #if CONFIG_VP9_HIGHBITDEPTH
1798 cm->use_highbitdepth = 1;
1799 #endif
1800 } else {
1801 cm->bit_depth = VPX_BITS_8;
1802 #if CONFIG_VP9_HIGHBITDEPTH
1803 cm->use_highbitdepth = 0;
1804 #endif
1806 cm->color_space = vpx_rb_read_literal(rb, 3);
1807 if (cm->color_space != VPX_CS_SRGB) {
1808 cm->color_range = (vpx_color_range_t)vpx_rb_read_bit(rb);
1809 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
1810 cm->subsampling_x = vpx_rb_read_bit(rb);
1811 cm->subsampling_y = vpx_rb_read_bit(rb);
1812 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
1813 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1814 "4:2:0 color not supported in profile 1 or 3");
1815 if (vpx_rb_read_bit(rb))
1816 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1817 "Reserved bit set");
1818 } else {
1819 cm->subsampling_y = cm->subsampling_x = 1;
1821 } else {
1822 cm->color_range = VPX_CR_FULL_RANGE;
1823 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
1824 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
1825 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
1826 cm->subsampling_y = cm->subsampling_x = 0;
1827 if (vpx_rb_read_bit(rb))
1828 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1829 "Reserved bit set");
1830 } else {
1831 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1832 "4:4:4 color not supported in profile 0 or 2");
1837 static size_t read_uncompressed_header(VP9Decoder *pbi,
1838 struct vpx_read_bit_buffer *rb) {
1839 VP9_COMMON *const cm = &pbi->common;
1840 BufferPool *const pool = cm->buffer_pool;
1841 RefCntBuffer *const frame_bufs = pool->frame_bufs;
1842 int i, mask, ref_index = 0;
1843 size_t sz;
1845 cm->last_frame_type = cm->frame_type;
1846 cm->last_intra_only = cm->intra_only;
1848 if (vpx_rb_read_literal(rb, 2) != VP9_FRAME_MARKER)
1849 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1850 "Invalid frame marker");
1852 cm->profile = vp9_read_profile(rb);
1853 #if CONFIG_VP9_HIGHBITDEPTH
1854 if (cm->profile >= MAX_PROFILES)
1855 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1856 "Unsupported bitstream profile");
1857 #else
1858 if (cm->profile >= PROFILE_2)
1859 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1860 "Unsupported bitstream profile");
1861 #endif
1863 cm->show_existing_frame = vpx_rb_read_bit(rb);
1864 if (cm->show_existing_frame) {
1865 // Show an existing frame directly.
1866 const int frame_to_show = cm->ref_frame_map[vpx_rb_read_literal(rb, 3)];
1867 lock_buffer_pool(pool);
1868 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
1869 unlock_buffer_pool(pool);
1870 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1871 "Buffer %d does not contain a decoded frame",
1872 frame_to_show);
1875 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
1876 unlock_buffer_pool(pool);
1877 pbi->refresh_frame_flags = 0;
1878 cm->lf.filter_level = 0;
1879 cm->show_frame = 1;
1881 if (pbi->frame_parallel_decode) {
1882 for (i = 0; i < REF_FRAMES; ++i)
1883 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
1885 return 0;
1888 cm->frame_type = (FRAME_TYPE) vpx_rb_read_bit(rb);
1889 cm->show_frame = vpx_rb_read_bit(rb);
1890 cm->error_resilient_mode = vpx_rb_read_bit(rb);
1892 if (cm->frame_type == KEY_FRAME) {
1893 if (!vp9_read_sync_code(rb))
1894 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1895 "Invalid frame sync code");
1897 read_bitdepth_colorspace_sampling(cm, rb);
1898 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
1900 for (i = 0; i < REFS_PER_FRAME; ++i) {
1901 cm->frame_refs[i].idx = INVALID_IDX;
1902 cm->frame_refs[i].buf = NULL;
1905 setup_frame_size(cm, rb);
1906 if (pbi->need_resync) {
1907 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
1908 pbi->need_resync = 0;
1910 } else {
1911 cm->intra_only = cm->show_frame ? 0 : vpx_rb_read_bit(rb);
1913 cm->reset_frame_context = cm->error_resilient_mode ?
1914 0 : vpx_rb_read_literal(rb, 2);
1916 if (cm->intra_only) {
1917 if (!vp9_read_sync_code(rb))
1918 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1919 "Invalid frame sync code");
1920 if (cm->profile > PROFILE_0) {
1921 read_bitdepth_colorspace_sampling(cm, rb);
1922 } else {
1923 // NOTE: The intra-only frame header does not include the specification
1924 // of either the color format or color sub-sampling in profile 0. VP9
1925 // specifies that the default color format should be YUV 4:2:0 in this
1926 // case (normative).
1927 cm->color_space = VPX_CS_BT_601;
1928 cm->color_range = VPX_CR_STUDIO_RANGE;
1929 cm->subsampling_y = cm->subsampling_x = 1;
1930 cm->bit_depth = VPX_BITS_8;
1931 #if CONFIG_VP9_HIGHBITDEPTH
1932 cm->use_highbitdepth = 0;
1933 #endif
1936 pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES);
1937 setup_frame_size(cm, rb);
1938 if (pbi->need_resync) {
1939 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
1940 pbi->need_resync = 0;
1942 } else if (pbi->need_resync != 1) { /* Skip if need resync */
1943 pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES);
1944 for (i = 0; i < REFS_PER_FRAME; ++i) {
1945 const int ref = vpx_rb_read_literal(rb, REF_FRAMES_LOG2);
1946 const int idx = cm->ref_frame_map[ref];
1947 RefBuffer *const ref_frame = &cm->frame_refs[i];
1948 ref_frame->idx = idx;
1949 ref_frame->buf = &frame_bufs[idx].buf;
1950 cm->ref_frame_sign_bias[LAST_FRAME + i] = vpx_rb_read_bit(rb);
1953 setup_frame_size_with_refs(cm, rb);
1955 cm->allow_high_precision_mv = vpx_rb_read_bit(rb);
1956 cm->interp_filter = read_interp_filter(rb);
1958 for (i = 0; i < REFS_PER_FRAME; ++i) {
1959 RefBuffer *const ref_buf = &cm->frame_refs[i];
1960 #if CONFIG_VP9_HIGHBITDEPTH
1961 vp9_setup_scale_factors_for_frame(&ref_buf->sf,
1962 ref_buf->buf->y_crop_width,
1963 ref_buf->buf->y_crop_height,
1964 cm->width, cm->height,
1965 cm->use_highbitdepth);
1966 #else
1967 vp9_setup_scale_factors_for_frame(&ref_buf->sf,
1968 ref_buf->buf->y_crop_width,
1969 ref_buf->buf->y_crop_height,
1970 cm->width, cm->height);
1971 #endif
1975 #if CONFIG_VP9_HIGHBITDEPTH
1976 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
1977 #endif
1978 get_frame_new_buffer(cm)->color_space = cm->color_space;
1979 get_frame_new_buffer(cm)->color_range = cm->color_range;
1980 get_frame_new_buffer(cm)->render_width = cm->render_width;
1981 get_frame_new_buffer(cm)->render_height = cm->render_height;
1983 if (pbi->need_resync) {
1984 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1985 "Keyframe / intra-only frame required to reset decoder"
1986 " state");
1989 if (!cm->error_resilient_mode) {
1990 cm->refresh_frame_context = vpx_rb_read_bit(rb);
1991 cm->frame_parallel_decoding_mode = vpx_rb_read_bit(rb);
1992 if (!cm->frame_parallel_decoding_mode)
1993 vp9_zero(cm->counts);
1994 } else {
1995 cm->refresh_frame_context = 0;
1996 cm->frame_parallel_decoding_mode = 1;
1999 // This flag will be overridden by the call to vp9_setup_past_independence
2000 // below, forcing the use of context 0 for those frame types.
2001 cm->frame_context_idx = vpx_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
2003 // Generate next_ref_frame_map.
2004 lock_buffer_pool(pool);
2005 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
2006 if (mask & 1) {
2007 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
2008 ++frame_bufs[cm->new_fb_idx].ref_count;
2009 } else {
2010 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
2012 // Current thread holds the reference frame.
2013 if (cm->ref_frame_map[ref_index] >= 0)
2014 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
2015 ++ref_index;
2018 for (; ref_index < REF_FRAMES; ++ref_index) {
2019 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
2020 // Current thread holds the reference frame.
2021 if (cm->ref_frame_map[ref_index] >= 0)
2022 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
2024 unlock_buffer_pool(pool);
2025 pbi->hold_ref_buf = 1;
2027 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
2028 vp9_setup_past_independence(cm);
2030 setup_loopfilter(&cm->lf, rb);
2031 setup_quantization(cm, &pbi->mb, rb);
2032 setup_segmentation(&cm->seg, rb);
2033 setup_segmentation_dequant(cm);
2035 setup_tile_info(cm, rb);
2036 sz = vpx_rb_read_literal(rb, 16);
2038 if (sz == 0)
2039 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2040 "Invalid header size");
2042 return sz;
2045 static int read_compressed_header(VP9Decoder *pbi, const uint8_t *data,
2046 size_t partition_size) {
2047 VP9_COMMON *const cm = &pbi->common;
2048 MACROBLOCKD *const xd = &pbi->mb;
2049 FRAME_CONTEXT *const fc = cm->fc;
2050 vpx_reader r;
2051 int k;
2053 if (vpx_reader_init(&r, data, partition_size, pbi->decrypt_cb,
2054 pbi->decrypt_state))
2055 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
2056 "Failed to allocate bool decoder 0");
2058 cm->tx_mode = xd->lossless ? ONLY_4X4 : read_tx_mode(&r);
2059 if (cm->tx_mode == TX_MODE_SELECT)
2060 read_tx_mode_probs(&fc->tx_probs, &r);
2061 read_coef_probs(fc, cm->tx_mode, &r);
2063 for (k = 0; k < SKIP_CONTEXTS; ++k)
2064 vp9_diff_update_prob(&r, &fc->skip_probs[k]);
2066 if (!frame_is_intra_only(cm)) {
2067 nmv_context *const nmvc = &fc->nmvc;
2068 int i, j;
2070 read_inter_mode_probs(fc, &r);
2072 if (cm->interp_filter == SWITCHABLE)
2073 read_switchable_interp_probs(fc, &r);
2075 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
2076 vp9_diff_update_prob(&r, &fc->intra_inter_prob[i]);
2078 cm->reference_mode = read_frame_reference_mode(cm, &r);
2079 if (cm->reference_mode != SINGLE_REFERENCE)
2080 setup_compound_reference_mode(cm);
2081 read_frame_reference_mode_probs(cm, &r);
2083 for (j = 0; j < BLOCK_SIZE_GROUPS; j++)
2084 for (i = 0; i < INTRA_MODES - 1; ++i)
2085 vp9_diff_update_prob(&r, &fc->y_mode_prob[j][i]);
2087 for (j = 0; j < PARTITION_CONTEXTS; ++j)
2088 for (i = 0; i < PARTITION_TYPES - 1; ++i)
2089 vp9_diff_update_prob(&r, &fc->partition_prob[j][i]);
2091 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
2094 return vpx_reader_has_error(&r);
2097 static struct vpx_read_bit_buffer *init_read_bit_buffer(
2098 VP9Decoder *pbi,
2099 struct vpx_read_bit_buffer *rb,
2100 const uint8_t *data,
2101 const uint8_t *data_end,
2102 uint8_t clear_data[MAX_VP9_HEADER_SIZE]) {
2103 rb->bit_offset = 0;
2104 rb->error_handler = error_handler;
2105 rb->error_handler_data = &pbi->common;
2106 if (pbi->decrypt_cb) {
2107 const int n = (int)VPXMIN(MAX_VP9_HEADER_SIZE, data_end - data);
2108 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
2109 rb->bit_buffer = clear_data;
2110 rb->bit_buffer_end = clear_data + n;
2111 } else {
2112 rb->bit_buffer = data;
2113 rb->bit_buffer_end = data_end;
2115 return rb;
2118 //------------------------------------------------------------------------------
2120 int vp9_read_sync_code(struct vpx_read_bit_buffer *const rb) {
2121 return vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_0 &&
2122 vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_1 &&
2123 vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_2;
2126 void vp9_read_frame_size(struct vpx_read_bit_buffer *rb,
2127 int *width, int *height) {
2128 *width = vpx_rb_read_literal(rb, 16) + 1;
2129 *height = vpx_rb_read_literal(rb, 16) + 1;
2132 BITSTREAM_PROFILE vp9_read_profile(struct vpx_read_bit_buffer *rb) {
2133 int profile = vpx_rb_read_bit(rb);
2134 profile |= vpx_rb_read_bit(rb) << 1;
2135 if (profile > 2)
2136 profile += vpx_rb_read_bit(rb);
2137 return (BITSTREAM_PROFILE) profile;
2140 void vp9_decode_frame(VP9Decoder *pbi,
2141 const uint8_t *data, const uint8_t *data_end,
2142 const uint8_t **p_data_end) {
2143 VP9_COMMON *const cm = &pbi->common;
2144 MACROBLOCKD *const xd = &pbi->mb;
2145 struct vpx_read_bit_buffer rb;
2146 int context_updated = 0;
2147 uint8_t clear_data[MAX_VP9_HEADER_SIZE];
2148 const size_t first_partition_size = read_uncompressed_header(pbi,
2149 init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
2150 const int tile_rows = 1 << cm->log2_tile_rows;
2151 const int tile_cols = 1 << cm->log2_tile_cols;
2152 YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm);
2153 xd->cur_buf = new_fb;
2155 if (!first_partition_size) {
2156 // showing a frame directly
2157 *p_data_end = data + (cm->profile <= PROFILE_2 ? 1 : 2);
2158 return;
2161 data += vpx_rb_bytes_read(&rb);
2162 if (!read_is_valid(data, first_partition_size, data_end))
2163 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2164 "Truncated packet or corrupt header length");
2166 cm->use_prev_frame_mvs = !cm->error_resilient_mode &&
2167 cm->width == cm->last_width &&
2168 cm->height == cm->last_height &&
2169 !cm->last_intra_only &&
2170 cm->last_show_frame &&
2171 (cm->last_frame_type != KEY_FRAME);
2173 vp9_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
2175 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
2176 if (!cm->fc->initialized)
2177 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2178 "Uninitialized entropy context.");
2180 xd->corrupted = 0;
2181 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
2182 if (new_fb->corrupted)
2183 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2184 "Decode failed. Frame data header is corrupted.");
2186 if (cm->lf.filter_level && !cm->skip_loop_filter) {
2187 vp9_loop_filter_frame_init(cm, cm->lf.filter_level);
2190 // If encoded in frame parallel mode, frame context is ready after decoding
2191 // the frame header.
2192 if (pbi->frame_parallel_decode && cm->frame_parallel_decoding_mode) {
2193 VPxWorker *const worker = pbi->frame_worker_owner;
2194 FrameWorkerData *const frame_worker_data = worker->data1;
2195 if (cm->refresh_frame_context) {
2196 context_updated = 1;
2197 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
2199 vp9_frameworker_lock_stats(worker);
2200 pbi->cur_buf->row = -1;
2201 pbi->cur_buf->col = -1;
2202 frame_worker_data->frame_context_ready = 1;
2203 // Signal the main thread that context is ready.
2204 vp9_frameworker_signal_stats(worker);
2205 vp9_frameworker_unlock_stats(worker);
2208 if (pbi->tile_worker_data == NULL ||
2209 (tile_cols * tile_rows) != pbi->total_tiles) {
2210 const int num_tile_workers = tile_cols * tile_rows +
2211 ((pbi->max_threads > 1) ? pbi->max_threads : 0);
2212 const size_t twd_size = num_tile_workers * sizeof(*pbi->tile_worker_data);
2213 // Ensure tile data offsets will be properly aligned. This may fail on
2214 // platforms without DECLARE_ALIGNED().
2215 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
2216 vpx_free(pbi->tile_worker_data);
2217 CHECK_MEM_ERROR(cm, pbi->tile_worker_data, vpx_memalign(32, twd_size));
2218 pbi->total_tiles = tile_rows * tile_cols;
2221 if (pbi->max_threads > 1 && tile_rows == 1 && tile_cols > 1) {
2222 // Multi-threaded tile decoder
2223 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
2224 if (!xd->corrupted) {
2225 if (!cm->skip_loop_filter) {
2226 // If multiple threads are used to decode tiles, then we use those
2227 // threads to do parallel loopfiltering.
2228 vp9_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane,
2229 cm->lf.filter_level, 0, 0, pbi->tile_workers,
2230 pbi->num_tile_workers, &pbi->lf_row_sync);
2232 } else {
2233 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2234 "Decode failed. Frame data is corrupted.");
2236 } else {
2237 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
2240 if (!xd->corrupted) {
2241 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
2242 vp9_adapt_coef_probs(cm);
2244 if (!frame_is_intra_only(cm)) {
2245 vp9_adapt_mode_probs(cm);
2246 vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
2249 } else {
2250 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2251 "Decode failed. Frame data is corrupted.");
2254 // Non frame parallel update frame context here.
2255 if (cm->refresh_frame_context && !context_updated)
2256 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;