blob: 8d3202c51fe2bfb12b546252a11c5668c15b5151 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <assert.h>
13#include <stdlib.h> // qsort()
14
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "./av1_rtcd.h"
16#include "./aom_config.h"
17#include "./aom_dsp_rtcd.h"
18#include "./aom_scale_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070019
20#include "aom_dsp/bitreader_buffer.h"
21#include "av1/decoder/bitreader.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070022#include "aom_dsp/aom_dsp_common.h"
23#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070024#include "aom_ports/mem.h"
25#include "aom_ports/mem_ops.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070026#include "aom_scale/aom_scale.h"
27#include "aom_util/aom_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070028
29#include "av1/common/alloccommon.h"
30#if CONFIG_CLPF
31#include "av1/common/clpf.h"
32#endif
33#include "av1/common/common.h"
34#if CONFIG_DERING
35#include "av1/common/dering.h"
36#endif // CONFIG_DERING
37#include "av1/common/entropy.h"
38#include "av1/common/entropymode.h"
39#include "av1/common/idct.h"
40#include "av1/common/thread_common.h"
41#include "av1/common/pred_common.h"
42#include "av1/common/quant_common.h"
43#include "av1/common/reconintra.h"
44#include "av1/common/reconinter.h"
45#include "av1/common/seg_common.h"
46#include "av1/common/tile_common.h"
47
48#include "av1/decoder/decodeframe.h"
49#include "av1/decoder/detokenize.h"
50#include "av1/decoder/decodemv.h"
51#include "av1/decoder/decoder.h"
52#include "av1/decoder/dsubexp.h"
53
Yaowu Xuf883b422016-08-30 14:01:10 -070054#define MAX_AV1_HEADER_SIZE 80
Yaowu Xuc27fc142016-08-22 16:08:15 -070055
Yaowu Xuf883b422016-08-30 14:01:10 -070056static int is_compound_reference_allowed(const AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070057 int i;
58 if (frame_is_intra_only(cm)) return 0;
59 for (i = 1; i < INTER_REFS_PER_FRAME; ++i)
60 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
61
62 return 0;
63}
64
Yaowu Xuf883b422016-08-30 14:01:10 -070065static void setup_compound_reference_mode(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070066#if CONFIG_EXT_REFS
67 cm->comp_fwd_ref[0] = LAST_FRAME;
68 cm->comp_fwd_ref[1] = LAST2_FRAME;
69 cm->comp_fwd_ref[2] = LAST3_FRAME;
70 cm->comp_fwd_ref[3] = GOLDEN_FRAME;
71
72 cm->comp_bwd_ref[0] = BWDREF_FRAME;
73 cm->comp_bwd_ref[1] = ALTREF_FRAME;
74#else
75 if (cm->ref_frame_sign_bias[LAST_FRAME] ==
76 cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
77 cm->comp_fixed_ref = ALTREF_FRAME;
78 cm->comp_var_ref[0] = LAST_FRAME;
79 cm->comp_var_ref[1] = GOLDEN_FRAME;
80 } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
81 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
82 cm->comp_fixed_ref = GOLDEN_FRAME;
83 cm->comp_var_ref[0] = LAST_FRAME;
84 cm->comp_var_ref[1] = ALTREF_FRAME;
85 } else {
86 cm->comp_fixed_ref = LAST_FRAME;
87 cm->comp_var_ref[0] = GOLDEN_FRAME;
88 cm->comp_var_ref[1] = ALTREF_FRAME;
89 }
90#endif // CONFIG_EXT_REFS
91}
92
93static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
94 return len != 0 && len <= (size_t)(end - start);
95}
96
Yaowu Xuf883b422016-08-30 14:01:10 -070097static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
98 const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
Yaowu Xuc27fc142016-08-22 16:08:15 -070099 return data > max ? max : data;
100}
101
Yaowu Xuf883b422016-08-30 14:01:10 -0700102static TX_MODE read_tx_mode(struct aom_read_bit_buffer *rb) {
103 return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700104}
105
Yaowu Xuf883b422016-08-30 14:01:10 -0700106static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700107 int i, j;
108 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
109 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700110 av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700111}
112
Yaowu Xuf883b422016-08-30 14:01:10 -0700113static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700114 int i;
115#if CONFIG_REF_MV
116 for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700117 av1_diff_update_prob(r, &fc->newmv_prob[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700118 for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700119 av1_diff_update_prob(r, &fc->zeromv_prob[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700120 for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700121 av1_diff_update_prob(r, &fc->refmv_prob[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700122 for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700123 av1_diff_update_prob(r, &fc->drl_prob[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700124#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700125 av1_diff_update_prob(r, &fc->new2mv_prob);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700126#endif // CONFIG_EXT_INTER
127#else
128 int j;
129 for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
130 for (j = 0; j < INTER_MODES - 1; ++j)
Yaowu Xuf883b422016-08-30 14:01:10 -0700131 av1_diff_update_prob(r, &fc->inter_mode_probs[i][j]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700132#endif
133}
134
135#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700136static void read_inter_compound_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700137 int i, j;
Yaowu Xuf883b422016-08-30 14:01:10 -0700138 if (aom_read(r, GROUP_DIFF_UPDATE_PROB)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700139 for (j = 0; j < INTER_MODE_CONTEXTS; ++j) {
140 for (i = 0; i < INTER_COMPOUND_MODES - 1; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700141 av1_diff_update_prob(r, &fc->inter_compound_mode_probs[j][i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700142 }
143 }
144 }
145}
146#endif // CONFIG_EXT_INTER
147
148static REFERENCE_MODE read_frame_reference_mode(
Yaowu Xuf883b422016-08-30 14:01:10 -0700149 const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700150 if (is_compound_reference_allowed(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700151 return aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700152 ? REFERENCE_MODE_SELECT
Yaowu Xuf883b422016-08-30 14:01:10 -0700153 : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700154 } else {
155 return SINGLE_REFERENCE;
156 }
157}
158
Yaowu Xuf883b422016-08-30 14:01:10 -0700159static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700160 FRAME_CONTEXT *const fc = cm->fc;
161 int i, j;
162
163 if (cm->reference_mode == REFERENCE_MODE_SELECT)
164 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700165 av1_diff_update_prob(r, &fc->comp_inter_prob[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700166
167 if (cm->reference_mode != COMPOUND_REFERENCE) {
168 for (i = 0; i < REF_CONTEXTS; ++i) {
169 for (j = 0; j < (SINGLE_REFS - 1); ++j) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700170 av1_diff_update_prob(r, &fc->single_ref_prob[i][j]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700171 }
172 }
173 }
174
175 if (cm->reference_mode != SINGLE_REFERENCE) {
176 for (i = 0; i < REF_CONTEXTS; ++i) {
177#if CONFIG_EXT_REFS
178 for (j = 0; j < (FWD_REFS - 1); ++j)
Yaowu Xuf883b422016-08-30 14:01:10 -0700179 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700180 for (j = 0; j < (BWD_REFS - 1); ++j)
Yaowu Xuf883b422016-08-30 14:01:10 -0700181 av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182#else
183 for (j = 0; j < (COMP_REFS - 1); ++j)
Yaowu Xuf883b422016-08-30 14:01:10 -0700184 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700185#endif // CONFIG_EXT_REFS
186 }
187 }
188}
189
Yaowu Xuf883b422016-08-30 14:01:10 -0700190static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700191 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700192 for (i = 0; i < n; ++i) av1_diff_update_prob(r, &p[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700193}
194
Yaowu Xuf883b422016-08-30 14:01:10 -0700195static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700196 int i, j;
197
198 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
199
200#if CONFIG_REF_MV
Yaowu Xuf883b422016-08-30 14:01:10 -0700201 av1_diff_update_prob(r, &ctx->zero_rmv);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700202#endif
203
204 for (i = 0; i < 2; ++i) {
205 nmv_component *const comp_ctx = &ctx->comps[i];
206 update_mv_probs(&comp_ctx->sign, 1, r);
207 update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
208 update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
209 update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
210 }
211
212 for (i = 0; i < 2; ++i) {
213 nmv_component *const comp_ctx = &ctx->comps[i];
214 for (j = 0; j < CLASS0_SIZE; ++j)
215 update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
216 update_mv_probs(comp_ctx->fp, 3, r);
217 }
218
219 if (allow_hp) {
220 for (i = 0; i < 2; ++i) {
221 nmv_component *const comp_ctx = &ctx->comps[i];
222 update_mv_probs(&comp_ctx->class0_hp, 1, r);
223 update_mv_probs(&comp_ctx->hp, 1, r);
224 }
225 }
226}
227
228static void inverse_transform_block(MACROBLOCKD *xd, int plane,
229 const TX_TYPE tx_type,
230 const TX_SIZE tx_size, uint8_t *dst,
231 int stride, int eob) {
232 struct macroblockd_plane *const pd = &xd->plane[plane];
233 if (eob > 0) {
234 tran_low_t *const dqcoeff = pd->dqcoeff;
235 INV_TXFM_PARAM inv_txfm_param;
236 inv_txfm_param.tx_type = tx_type;
237 inv_txfm_param.tx_size = tx_size;
238 inv_txfm_param.eob = eob;
239 inv_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];
240
Yaowu Xuf883b422016-08-30 14:01:10 -0700241#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700242 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
243 inv_txfm_param.bd = xd->bd;
244 highbd_inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
245 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700246#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700247 inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
Yaowu Xuf883b422016-08-30 14:01:10 -0700248#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700249 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700250#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700251
252 if (eob == 1) {
253 dqcoeff[0] = 0;
254 } else {
255 if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
256 memset(dqcoeff, 0, 4 * 4 * num_4x4_blocks_wide_txsize_lookup[tx_size] *
257 sizeof(dqcoeff[0]));
258#if CONFIG_EXT_TX
259 else
260 memset(dqcoeff, 0, get_tx2d_size(tx_size) * sizeof(dqcoeff[0]));
261#else
262 else if (tx_size == TX_32X32 && eob <= 34)
263 memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
264 else
265 memset(dqcoeff, 0, get_tx2d_size(tx_size) * sizeof(dqcoeff[0]));
266#endif
267 }
268 }
269}
270
271static void predict_and_reconstruct_intra_block(MACROBLOCKD *const xd,
272#if CONFIG_ANS
273 struct AnsDecoder *const r,
274#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700275 aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700276#endif // CONFIG_ANS
277 MB_MODE_INFO *const mbmi,
278 int plane, int row, int col,
279 TX_SIZE tx_size) {
280 struct macroblockd_plane *const pd = &xd->plane[plane];
281 PREDICTION_MODE mode = (plane == 0) ? mbmi->mode : mbmi->uv_mode;
282 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
283 uint8_t *dst;
284 int block_idx = (row << 1) + col;
285 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
286
287 if (mbmi->sb_type < BLOCK_8X8)
288 if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
289
Yaowu Xuf883b422016-08-30 14:01:10 -0700290 av1_predict_intra_block(xd, pd->n4_wl, pd->n4_hl, tx_size, mode, dst,
291 pd->dst.stride, dst, pd->dst.stride, col, row, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700292
293 if (!mbmi->skip) {
294 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
295 const scan_order *sc = get_scan(tx_size, tx_type, 0);
Yaowu Xuf883b422016-08-30 14:01:10 -0700296 const int eob = av1_decode_block_tokens(xd, plane, sc, col, row, tx_size,
297 tx_type, r, mbmi->segment_id);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700298 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
299 eob);
300 }
301}
302
303#if CONFIG_VAR_TX
Yaowu Xuf883b422016-08-30 14:01:10 -0700304static void decode_reconstruct_tx(MACROBLOCKD *const xd, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700305 MB_MODE_INFO *const mbmi, int plane,
306 BLOCK_SIZE plane_bsize, int block,
307 int blk_row, int blk_col, TX_SIZE tx_size,
308 int *eob_total) {
309 const struct macroblockd_plane *const pd = &xd->plane[plane];
310 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
311 const int tx_row = blk_row >> (1 - pd->subsampling_y);
312 const int tx_col = blk_col >> (1 - pd->subsampling_x);
313 const TX_SIZE plane_tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700314 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700315 : mbmi->inter_tx_size[tx_row][tx_col];
316 int max_blocks_high = num_4x4_blocks_high_lookup[plane_bsize];
317 int max_blocks_wide = num_4x4_blocks_wide_lookup[plane_bsize];
318
319 if (xd->mb_to_bottom_edge < 0)
320 max_blocks_high += xd->mb_to_bottom_edge >> (5 + pd->subsampling_y);
321 if (xd->mb_to_right_edge < 0)
322 max_blocks_wide += xd->mb_to_right_edge >> (5 + pd->subsampling_x);
323
324 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
325
326 if (tx_size == plane_tx_size) {
327 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
328 TX_TYPE tx_type = get_tx_type(plane_type, xd, block, plane_tx_size);
329 const scan_order *sc = get_scan(plane_tx_size, tx_type, 1);
330 const int eob =
Yaowu Xuf883b422016-08-30 14:01:10 -0700331 av1_decode_block_tokens(xd, plane, sc, blk_col, blk_row, plane_tx_size,
332 tx_type, r, mbmi->segment_id);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700333 inverse_transform_block(
334 xd, plane, tx_type, plane_tx_size,
335 &pd->dst.buf[4 * blk_row * pd->dst.stride + 4 * blk_col],
336 pd->dst.stride, eob);
337 *eob_total += eob;
338 } else {
339 int bsl = b_width_log2_lookup[bsize];
340 int i;
341
342 assert(bsl > 0);
343 --bsl;
344
345 for (i = 0; i < 4; ++i) {
346 const int offsetr = blk_row + ((i >> 1) << bsl);
347 const int offsetc = blk_col + ((i & 0x01) << bsl);
348 int step = num_4x4_blocks_txsize_lookup[tx_size - 1];
349
350 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
351
352 decode_reconstruct_tx(xd, r, mbmi, plane, plane_bsize, block + i * step,
353 offsetr, offsetc, tx_size - 1, eob_total);
354 }
355 }
356}
357#endif // CONFIG_VAR_TX
358
359#if !CONFIG_VAR_TX || CONFIG_SUPERTX || (CONFIG_EXT_TX && CONFIG_RECT_TX)
360static int reconstruct_inter_block(MACROBLOCKD *const xd,
361#if CONFIG_ANS
362 struct AnsDecoder *const r,
363#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700364 aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700365#endif
366 int segment_id, int plane, int row, int col,
367 TX_SIZE tx_size) {
368 struct macroblockd_plane *const pd = &xd->plane[plane];
369 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
370 int block_idx = (row << 1) + col;
371 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
372 const scan_order *sc = get_scan(tx_size, tx_type, 1);
Yaowu Xuf883b422016-08-30 14:01:10 -0700373 const int eob = av1_decode_block_tokens(xd, plane, sc, col, row, tx_size,
374 tx_type, r, segment_id);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700375
376 inverse_transform_block(xd, plane, tx_type, tx_size,
377 &pd->dst.buf[4 * row * pd->dst.stride + 4 * col],
378 pd->dst.stride, eob);
379 return eob;
380}
381#endif // !CONFIG_VAR_TX || CONFIG_SUPER_TX
382
Yaowu Xuc27fc142016-08-22 16:08:15 -0700383static INLINE void dec_reset_skip_context(MACROBLOCKD *xd) {
384 int i;
385 for (i = 0; i < MAX_MB_PLANE; i++) {
386 struct macroblockd_plane *const pd = &xd->plane[i];
387 memset(pd->above_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_w);
388 memset(pd->left_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_h);
389 }
390}
391
392static void set_plane_n4(MACROBLOCKD *const xd, int bw, int bh, int bwl,
393 int bhl) {
394 int i;
395 for (i = 0; i < MAX_MB_PLANE; i++) {
396 xd->plane[i].n4_w = (bw << 1) >> xd->plane[i].subsampling_x;
397 xd->plane[i].n4_h = (bh << 1) >> xd->plane[i].subsampling_y;
398 xd->plane[i].n4_wl = bwl - xd->plane[i].subsampling_x;
399 xd->plane[i].n4_hl = bhl - xd->plane[i].subsampling_y;
400 }
401}
402
Yaowu Xuf883b422016-08-30 14:01:10 -0700403static MB_MODE_INFO *set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700404 BLOCK_SIZE bsize, int mi_row, int mi_col,
405 int bw, int bh, int x_mis, int y_mis, int bwl,
406 int bhl) {
407 const int offset = mi_row * cm->mi_stride + mi_col;
408 int x, y;
409 const TileInfo *const tile = &xd->tile;
410
411 xd->mi = cm->mi_grid_visible + offset;
412 xd->mi[0] = &cm->mi[offset];
413 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
414 // passing bsize from decode_partition().
415 xd->mi[0]->mbmi.sb_type = bsize;
416 for (y = 0; y < y_mis; ++y)
417 for (x = !y; x < x_mis; ++x) {
418 xd->mi[y * cm->mi_stride + x] = xd->mi[0];
419 }
420
421 set_plane_n4(xd, bw, bh, bwl, bhl);
422
423 set_skip_context(xd, mi_row, mi_col);
424
425#if CONFIG_VAR_TX
426 xd->max_tx_size = max_txsize_lookup[bsize];
427#endif
428
429 // Distance of Mb to the various image edges. These are specified to 8th pel
430 // as they are always compared to values that are in 1/8th pel units
431 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
432
Yaowu Xuf883b422016-08-30 14:01:10 -0700433 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700434 return &xd->mi[0]->mbmi;
435}
436
437#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700438static MB_MODE_INFO *set_offsets_extend(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700439 MACROBLOCKD *const xd,
440 const TileInfo *const tile,
441 BLOCK_SIZE bsize_pred, int mi_row_pred,
442 int mi_col_pred, int mi_row_ori,
443 int mi_col_ori) {
444 // Used in supertx
445 // (mi_row_ori, mi_col_ori): location for mv
446 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
447 const int bw = num_8x8_blocks_wide_lookup[bsize_pred];
448 const int bh = num_8x8_blocks_high_lookup[bsize_pred];
449 const int offset = mi_row_ori * cm->mi_stride + mi_col_ori;
450 const int bwl = b_width_log2_lookup[bsize_pred];
451 const int bhl = b_height_log2_lookup[bsize_pred];
452 xd->mi = cm->mi_grid_visible + offset;
453 xd->mi[0] = cm->mi + offset;
454 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
455 cm->mi_cols);
456
457 xd->up_available = (mi_row_ori > tile->mi_row_start);
458 xd->left_available = (mi_col_ori > tile->mi_col_start);
459
460 set_plane_n4(xd, bw, bh, bwl, bhl);
461
462 return &xd->mi[0]->mbmi;
463}
464
Yaowu Xuf883b422016-08-30 14:01:10 -0700465static MB_MODE_INFO *set_mb_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
466 BLOCK_SIZE bsize, int mi_row, int mi_col,
467 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700468 const int offset = mi_row * cm->mi_stride + mi_col;
469 const TileInfo *const tile = &xd->tile;
470 int x, y;
471
472 xd->mi = cm->mi_grid_visible + offset;
473 xd->mi[0] = cm->mi + offset;
474 xd->mi[0]->mbmi.sb_type = bsize;
475 for (y = 0; y < y_mis; ++y)
476 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
477
478 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
479 return &xd->mi[0]->mbmi;
480}
481
Yaowu Xuf883b422016-08-30 14:01:10 -0700482static void set_offsets_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700483 const TileInfo *const tile, BLOCK_SIZE bsize,
484 int mi_row, int mi_col) {
485 const int bw = num_8x8_blocks_wide_lookup[bsize];
486 const int bh = num_8x8_blocks_high_lookup[bsize];
487 const int offset = mi_row * cm->mi_stride + mi_col;
488 const int bwl = b_width_log2_lookup[bsize];
489 const int bhl = b_height_log2_lookup[bsize];
490
491 xd->mi = cm->mi_grid_visible + offset;
492 xd->mi[0] = cm->mi + offset;
493
494 set_plane_n4(xd, bw, bh, bwl, bhl);
495
496 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
497
Yaowu Xuf883b422016-08-30 14:01:10 -0700498 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700499}
500
Yaowu Xuf883b422016-08-30 14:01:10 -0700501static void set_param_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700502 BLOCK_SIZE bsize, int mi_row, int mi_col,
503 int txfm, int skip) {
504 const int bw = num_8x8_blocks_wide_lookup[bsize];
505 const int bh = num_8x8_blocks_high_lookup[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -0700506 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
507 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700508 const int offset = mi_row * cm->mi_stride + mi_col;
509 int x, y;
510
511 xd->mi = cm->mi_grid_visible + offset;
512 xd->mi[0] = cm->mi + offset;
513
514 for (y = 0; y < y_mis; ++y)
515 for (x = 0; x < x_mis; ++x) {
516 xd->mi[y * cm->mi_stride + x]->mbmi.skip = skip;
517 xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
518 }
519#if CONFIG_VAR_TX
520 xd->above_txfm_context = cm->above_txfm_context + mi_col;
521 xd->left_txfm_context =
522 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
523 set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bw, bh, xd);
524#endif
525}
526
Yaowu Xuf883b422016-08-30 14:01:10 -0700527static void set_ref(AV1_COMMON *const cm, MACROBLOCKD *const xd, int idx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700528 int mi_row, int mi_col) {
529 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
530 RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
531 xd->block_refs[idx] = ref_buffer;
Yaowu Xuf883b422016-08-30 14:01:10 -0700532 if (!av1_is_valid_scale(&ref_buffer->sf))
533 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700534 "Invalid scale factors");
Yaowu Xuf883b422016-08-30 14:01:10 -0700535 av1_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
536 &ref_buffer->sf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700537 xd->corrupted |= ref_buffer->buf->corrupted;
538}
539
540static void dec_predict_b_extend(
Yaowu Xuf883b422016-08-30 14:01:10 -0700541 AV1Decoder *const pbi, MACROBLOCKD *const xd, const TileInfo *const tile,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700542 int block, int mi_row_ori, int mi_col_ori, int mi_row_pred, int mi_col_pred,
543 int mi_row_top, int mi_col_top, uint8_t *dst_buf[3], int dst_stride[3],
544 BLOCK_SIZE bsize_top, BLOCK_SIZE bsize_pred, int b_sub8x8, int bextend) {
545 // Used in supertx
546 // (mi_row_ori, mi_col_ori): location for mv
547 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
548 // (mi_row_top, mi_col_top, bsize_top): region of the top partition size
549 // block: sub location of sub8x8 blocks
550 // b_sub8x8: 1: ori is sub8x8; 0: ori is not sub8x8
551 // bextend: 1: region to predict is an extension of ori; 0: not
552 int r = (mi_row_pred - mi_row_top) * MI_SIZE;
553 int c = (mi_col_pred - mi_col_top) * MI_SIZE;
554 const int mi_width_top = num_8x8_blocks_wide_lookup[bsize_top];
555 const int mi_height_top = num_8x8_blocks_high_lookup[bsize_top];
556 MB_MODE_INFO *mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700557 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700558
559 if (mi_row_pred < mi_row_top || mi_col_pred < mi_col_top ||
560 mi_row_pred >= mi_row_top + mi_height_top ||
561 mi_col_pred >= mi_col_top + mi_width_top || mi_row_pred >= cm->mi_rows ||
562 mi_col_pred >= cm->mi_cols)
563 return;
564
565 mbmi = set_offsets_extend(cm, xd, tile, bsize_pred, mi_row_pred, mi_col_pred,
566 mi_row_ori, mi_col_ori);
567 set_ref(cm, xd, 0, mi_row_pred, mi_col_pred);
568 if (has_second_ref(&xd->mi[0]->mbmi))
569 set_ref(cm, xd, 1, mi_row_pred, mi_col_pred);
570
571 if (!bextend) {
572 mbmi->tx_size = b_width_log2_lookup[bsize_top];
573 }
574
575 xd->plane[0].dst.stride = dst_stride[0];
576 xd->plane[1].dst.stride = dst_stride[1];
577 xd->plane[2].dst.stride = dst_stride[2];
578 xd->plane[0].dst.buf = dst_buf[0] +
579 (r >> xd->plane[0].subsampling_y) * dst_stride[0] +
580 (c >> xd->plane[0].subsampling_x);
581 xd->plane[1].dst.buf = dst_buf[1] +
582 (r >> xd->plane[1].subsampling_y) * dst_stride[1] +
583 (c >> xd->plane[1].subsampling_x);
584 xd->plane[2].dst.buf = dst_buf[2] +
585 (r >> xd->plane[2].subsampling_y) * dst_stride[2] +
586 (c >> xd->plane[2].subsampling_x);
587
588 if (!b_sub8x8)
Yaowu Xuf883b422016-08-30 14:01:10 -0700589 av1_build_inter_predictors_sb_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700590#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700591 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700592#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700593 mi_row_pred, mi_col_pred, bsize_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700594 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700595 av1_build_inter_predictors_sb_sub8x8_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700596#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700597 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700598#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700599 mi_row_pred, mi_col_pred,
600 bsize_pred, block);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700601}
602
Yaowu Xuf883b422016-08-30 14:01:10 -0700603static void dec_extend_dir(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700604 const TileInfo *const tile, int block,
605 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
606 int mi_col, int mi_row_top, int mi_col_top,
607 uint8_t *dst_buf[3], int dst_stride[3], int dir) {
608 // dir: 0-lower, 1-upper, 2-left, 3-right
609 // 4-lowerleft, 5-upperleft, 6-lowerright, 7-upperright
610 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
611 const int mi_height = num_8x8_blocks_high_lookup[bsize];
612 int xss = xd->plane[1].subsampling_x;
613 int yss = xd->plane[1].subsampling_y;
614 int b_sub8x8 = (bsize < BLOCK_8X8) ? 1 : 0;
615 BLOCK_SIZE extend_bsize;
616 int unit, mi_row_pred, mi_col_pred;
617
618 if (dir == 0 || dir == 1) {
619 extend_bsize = (mi_width == 1 || bsize < BLOCK_8X8 || xss < yss)
620 ? BLOCK_8X8
621 : BLOCK_16X8;
622 unit = num_8x8_blocks_wide_lookup[extend_bsize];
623 mi_row_pred = mi_row + ((dir == 0) ? mi_height : -1);
624 mi_col_pred = mi_col;
625
626 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
627 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
628 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
629
630 if (mi_width > unit) {
631 int i;
632 assert(!b_sub8x8);
633 for (i = 0; i < mi_width / unit - 1; i++) {
634 mi_col_pred += unit;
635 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
636 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
637 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
638 }
639 }
640 } else if (dir == 2 || dir == 3) {
641 extend_bsize = (mi_height == 1 || bsize < BLOCK_8X8 || yss < xss)
642 ? BLOCK_8X8
643 : BLOCK_8X16;
644 unit = num_8x8_blocks_high_lookup[extend_bsize];
645 mi_row_pred = mi_row;
646 mi_col_pred = mi_col + ((dir == 3) ? mi_width : -1);
647
648 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
649 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
650 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
651
652 if (mi_height > unit) {
653 int i;
654 for (i = 0; i < mi_height / unit - 1; i++) {
655 mi_row_pred += unit;
656 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
657 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
658 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
659 }
660 }
661 } else {
662 extend_bsize = BLOCK_8X8;
663 mi_row_pred = mi_row + ((dir == 4 || dir == 6) ? mi_height : -1);
664 mi_col_pred = mi_col + ((dir == 6 || dir == 7) ? mi_width : -1);
665 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
666 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
667 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
668 }
669}
670
Yaowu Xuf883b422016-08-30 14:01:10 -0700671static void dec_extend_all(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700672 const TileInfo *const tile, int block,
673 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
674 int mi_col, int mi_row_top, int mi_col_top,
675 uint8_t *dst_buf[3], int dst_stride[3]) {
676 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
677 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
678 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
679 mi_row_top, mi_col_top, dst_buf, dst_stride, 1);
680 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
681 mi_row_top, mi_col_top, dst_buf, dst_stride, 2);
682 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
683 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
684 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
685 mi_row_top, mi_col_top, dst_buf, dst_stride, 4);
686 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
687 mi_row_top, mi_col_top, dst_buf, dst_stride, 5);
688 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
689 mi_row_top, mi_col_top, dst_buf, dst_stride, 6);
690 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
691 mi_row_top, mi_col_top, dst_buf, dst_stride, 7);
692}
693
Yaowu Xuf883b422016-08-30 14:01:10 -0700694static void dec_predict_sb_complex(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700695 const TileInfo *const tile, int mi_row,
696 int mi_col, int mi_row_top, int mi_col_top,
697 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize,
698 uint8_t *dst_buf[3], int dst_stride[3]) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700699 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700700 const int hbs = num_8x8_blocks_wide_lookup[bsize] / 2;
701 const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
702 const BLOCK_SIZE subsize = get_subsize(bsize, partition);
703#if CONFIG_EXT_PARTITION_TYPES
704 const BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
705#endif
706 int i;
707 const int mi_offset = mi_row * cm->mi_stride + mi_col;
708 uint8_t *dst_buf1[3], *dst_buf2[3], *dst_buf3[3];
709
710 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
711 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
712 DECLARE_ALIGNED(16, uint8_t, tmp_buf3[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
713 int dst_stride1[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
714 int dst_stride2[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
715 int dst_stride3[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
716
Yaowu Xuf883b422016-08-30 14:01:10 -0700717#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700718 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
719 int len = sizeof(uint16_t);
720 dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
721 dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_TX_SQUARE * len);
722 dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_TX_SQUARE * len);
723 dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
724 dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_TX_SQUARE * len);
725 dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_TX_SQUARE * len);
726 dst_buf3[0] = CONVERT_TO_BYTEPTR(tmp_buf3);
727 dst_buf3[1] = CONVERT_TO_BYTEPTR(tmp_buf3 + MAX_TX_SQUARE * len);
728 dst_buf3[2] = CONVERT_TO_BYTEPTR(tmp_buf3 + 2 * MAX_TX_SQUARE * len);
729 } else {
730#endif
731 dst_buf1[0] = tmp_buf1;
732 dst_buf1[1] = tmp_buf1 + MAX_TX_SQUARE;
733 dst_buf1[2] = tmp_buf1 + 2 * MAX_TX_SQUARE;
734 dst_buf2[0] = tmp_buf2;
735 dst_buf2[1] = tmp_buf2 + MAX_TX_SQUARE;
736 dst_buf2[2] = tmp_buf2 + 2 * MAX_TX_SQUARE;
737 dst_buf3[0] = tmp_buf3;
738 dst_buf3[1] = tmp_buf3 + MAX_TX_SQUARE;
739 dst_buf3[2] = tmp_buf3 + 2 * MAX_TX_SQUARE;
Yaowu Xuf883b422016-08-30 14:01:10 -0700740#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700741 }
742#endif
743
744 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
745
746 xd->mi = cm->mi_grid_visible + mi_offset;
747 xd->mi[0] = cm->mi + mi_offset;
748
749 for (i = 0; i < MAX_MB_PLANE; i++) {
750 xd->plane[i].dst.buf = dst_buf[i];
751 xd->plane[i].dst.stride = dst_stride[i];
752 }
753
754 switch (partition) {
755 case PARTITION_NONE:
756 assert(bsize < top_bsize);
757 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
758 mi_row_top, mi_col_top, dst_buf, dst_stride,
759 top_bsize, bsize, 0, 0);
760 dec_extend_all(pbi, xd, tile, 0, bsize, top_bsize, mi_row, mi_col,
761 mi_row_top, mi_col_top, dst_buf, dst_stride);
762 break;
763 case PARTITION_HORZ:
764 if (bsize == BLOCK_8X8) {
765 // For sub8x8, predict in 8x8 unit
766 // First half
767 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
768 mi_row_top, mi_col_top, dst_buf, dst_stride,
769 top_bsize, BLOCK_8X8, 1, 0);
770 if (bsize < top_bsize)
771 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
772 mi_row_top, mi_col_top, dst_buf, dst_stride);
773
774 // Second half
775 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
776 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
777 top_bsize, BLOCK_8X8, 1, 1);
778 if (bsize < top_bsize)
779 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
780 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
781
782 // weighted average to smooth the boundary
783 xd->plane[0].dst.buf = dst_buf[0];
784 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -0700785 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700786 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
787 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
788 0);
789 } else {
790 // First half
791 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
792 mi_row_top, mi_col_top, dst_buf, dst_stride,
793 top_bsize, subsize, 0, 0);
794 if (bsize < top_bsize)
795 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
796 mi_row_top, mi_col_top, dst_buf, dst_stride);
797 else
798 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
799 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
800
801 if (mi_row + hbs < cm->mi_rows) {
802 // Second half
803 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col,
804 mi_row + hbs, mi_col, mi_row_top, mi_col_top,
805 dst_buf1, dst_stride1, top_bsize, subsize, 0, 0);
806 if (bsize < top_bsize)
807 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
808 mi_col, mi_row_top, mi_col_top, dst_buf1,
809 dst_stride1);
810 else
811 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
812 mi_col, mi_row_top, mi_col_top, dst_buf1,
813 dst_stride1, 1);
814
815 // weighted average to smooth the boundary
816 for (i = 0; i < MAX_MB_PLANE; i++) {
817 xd->plane[i].dst.buf = dst_buf[i];
818 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700819 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700820 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
821 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
822 PARTITION_HORZ, i);
823 }
824 }
825 }
826 break;
827 case PARTITION_VERT:
828 if (bsize == BLOCK_8X8) {
829 // First half
830 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
831 mi_row_top, mi_col_top, dst_buf, dst_stride,
832 top_bsize, BLOCK_8X8, 1, 0);
833 if (bsize < top_bsize)
834 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
835 mi_row_top, mi_col_top, dst_buf, dst_stride);
836
837 // Second half
838 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
839 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
840 top_bsize, BLOCK_8X8, 1, 1);
841 if (bsize < top_bsize)
842 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
843 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
844
845 // Smooth
846 xd->plane[0].dst.buf = dst_buf[0];
847 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -0700848 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700849 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
850 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
851 0);
852 } else {
853 // First half
854 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
855 mi_row_top, mi_col_top, dst_buf, dst_stride,
856 top_bsize, subsize, 0, 0);
857 if (bsize < top_bsize)
858 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
859 mi_row_top, mi_col_top, dst_buf, dst_stride);
860 else
861 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
862 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
863
864 // Second half
865 if (mi_col + hbs < cm->mi_cols) {
866 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
867 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
868 dst_stride1, top_bsize, subsize, 0, 0);
869 if (bsize < top_bsize)
870 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
871 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
872 dst_stride1);
873 else
874 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
875 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
876 dst_stride1, 2);
877
878 // Smooth
879 for (i = 0; i < MAX_MB_PLANE; i++) {
880 xd->plane[i].dst.buf = dst_buf[i];
881 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700882 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700883 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
884 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
885 PARTITION_VERT, i);
886 }
887 }
888 }
889 break;
890 case PARTITION_SPLIT:
891 if (bsize == BLOCK_8X8) {
892 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
893 mi_row_top, mi_col_top, dst_buf, dst_stride,
894 top_bsize, BLOCK_8X8, 1, 0);
895 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
896 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
897 top_bsize, BLOCK_8X8, 1, 1);
898 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
899 mi_row_top, mi_col_top, dst_buf2, dst_stride2,
900 top_bsize, BLOCK_8X8, 1, 1);
901 dec_predict_b_extend(pbi, xd, tile, 3, mi_row, mi_col, mi_row, mi_col,
902 mi_row_top, mi_col_top, dst_buf3, dst_stride3,
903 top_bsize, BLOCK_8X8, 1, 1);
904 if (bsize < top_bsize) {
905 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
906 mi_row_top, mi_col_top, dst_buf, dst_stride);
907 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
908 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
909 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
910 mi_row_top, mi_col_top, dst_buf2, dst_stride2);
911 dec_extend_all(pbi, xd, tile, 3, subsize, top_bsize, mi_row, mi_col,
912 mi_row_top, mi_col_top, dst_buf3, dst_stride3);
913 }
914 } else {
915 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row_top,
916 mi_col_top, subsize, top_bsize, dst_buf,
917 dst_stride);
918 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols)
919 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col + hbs,
920 mi_row_top, mi_col_top, subsize, top_bsize,
921 dst_buf1, dst_stride1);
922 if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols)
923 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col,
924 mi_row_top, mi_col_top, subsize, top_bsize,
925 dst_buf2, dst_stride2);
926 if (mi_row + hbs < cm->mi_rows && mi_col + hbs < cm->mi_cols)
927 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col + hbs,
928 mi_row_top, mi_col_top, subsize, top_bsize,
929 dst_buf3, dst_stride3);
930 }
931 for (i = 0; i < MAX_MB_PLANE; i++) {
932 if (bsize == BLOCK_8X8 && i != 0)
933 continue; // Skip <4x4 chroma smoothing
934 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700935 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700936 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
937 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
938 PARTITION_VERT, i);
939 if (mi_row + hbs < cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700940 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700941 xd, dst_buf2[i], dst_stride2[i], dst_buf3[i], dst_stride3[i],
942 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
943 PARTITION_VERT, i);
Yaowu Xuf883b422016-08-30 14:01:10 -0700944 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700945 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
946 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
947 PARTITION_HORZ, i);
948 }
949 } else if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700950 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700951 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
952 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
953 PARTITION_HORZ, i);
954 }
955 }
956 break;
957#if CONFIG_EXT_PARTITION_TYPES
958 case PARTITION_HORZ_A:
959 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
960 mi_row_top, mi_col_top, dst_buf, dst_stride,
961 top_bsize, bsize2, 0, 0);
962 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
963 mi_row_top, mi_col_top, dst_buf, dst_stride);
964
965 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
966 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
967 dst_stride1, top_bsize, bsize2, 0, 0);
968 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
969 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
970
971 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
972 mi_col, mi_row_top, mi_col_top, dst_buf2,
973 dst_stride2, top_bsize, subsize, 0, 0);
974 if (bsize < top_bsize)
975 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
976 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2);
977 else
978 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
979 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2,
980 1);
981
982 for (i = 0; i < MAX_MB_PLANE; i++) {
983 xd->plane[i].dst.buf = dst_buf[i];
984 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700985 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700986 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
987 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
988 i);
989 }
990 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700991 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700992 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
993 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
994 i);
995 }
996 break;
997 case PARTITION_VERT_A:
998
999 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1000 mi_row_top, mi_col_top, dst_buf, dst_stride,
1001 top_bsize, bsize2, 0, 0);
1002 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1003 mi_row_top, mi_col_top, dst_buf, dst_stride);
1004
1005 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1006 mi_col, mi_row_top, mi_col_top, dst_buf1,
1007 dst_stride1, top_bsize, bsize2, 0, 0);
1008 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1009 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1010
1011 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1012 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1013 dst_stride2, top_bsize, subsize, 0, 0);
1014 if (bsize < top_bsize)
1015 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1016 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1017 dst_stride2);
1018 else
1019 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1020 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1021 dst_stride2, 2);
1022
1023 for (i = 0; i < MAX_MB_PLANE; i++) {
1024 xd->plane[i].dst.buf = dst_buf[i];
1025 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001026 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001027 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1028 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1029 i);
1030 }
1031 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001032 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001033 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1034 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1035 i);
1036 }
1037 break;
1038 case PARTITION_HORZ_B:
1039 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1040 mi_row_top, mi_col_top, dst_buf, dst_stride,
1041 top_bsize, subsize, 0, 0);
1042 if (bsize < top_bsize)
1043 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1044 mi_row_top, mi_col_top, dst_buf, dst_stride);
1045 else
1046 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1047 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1048
1049 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1050 mi_col, mi_row_top, mi_col_top, dst_buf1,
1051 dst_stride1, top_bsize, bsize2, 0, 0);
1052 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1053 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1054
1055 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1056 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1057 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1058 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1059 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1060 dst_stride2);
1061
1062 for (i = 0; i < MAX_MB_PLANE; i++) {
1063 xd->plane[i].dst.buf = dst_buf1[i];
1064 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001065 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001066 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1067 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1068 PARTITION_VERT, i);
1069 }
1070 for (i = 0; i < MAX_MB_PLANE; i++) {
1071 xd->plane[i].dst.buf = dst_buf[i];
1072 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001073 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001074 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1075 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1076 i);
1077 }
1078 break;
1079 case PARTITION_VERT_B:
1080 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1081 mi_row_top, mi_col_top, dst_buf, dst_stride,
1082 top_bsize, subsize, 0, 0);
1083 if (bsize < top_bsize)
1084 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1085 mi_row_top, mi_col_top, dst_buf, dst_stride);
1086 else
1087 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1088 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1089
1090 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1091 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1092 dst_stride1, top_bsize, bsize2, 0, 0);
1093 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1094 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1095
1096 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1097 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1098 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1099 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1100 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1101 dst_stride2);
1102
1103 for (i = 0; i < MAX_MB_PLANE; i++) {
1104 xd->plane[i].dst.buf = dst_buf1[i];
1105 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001106 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001107 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1108 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1109 PARTITION_HORZ, i);
1110 }
1111 for (i = 0; i < MAX_MB_PLANE; i++) {
1112 xd->plane[i].dst.buf = dst_buf[i];
1113 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001114 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001115 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1116 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1117 i);
1118 }
1119 break;
1120#endif // CONFIG_EXT_PARTITION_TYPES
1121 default: assert(0);
1122 }
1123}
1124
Yaowu Xuf883b422016-08-30 14:01:10 -07001125static void set_segment_id_supertx(const AV1_COMMON *const cm, const int mi_row,
1126 const int mi_col, const BLOCK_SIZE bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001127 const struct segmentation *seg = &cm->seg;
1128 const int miw =
Yaowu Xuf883b422016-08-30 14:01:10 -07001129 AOMMIN(num_8x8_blocks_wide_lookup[bsize], cm->mi_cols - mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001130 const int mih =
Yaowu Xuf883b422016-08-30 14:01:10 -07001131 AOMMIN(num_8x8_blocks_high_lookup[bsize], cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001132 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1133 MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
1134 int r, c;
1135 int seg_id_supertx = MAX_SEGMENTS;
1136
1137 if (!seg->enabled) {
1138 seg_id_supertx = 0;
1139 } else {
1140 // Find the minimum segment_id
1141 for (r = 0; r < mih; r++)
1142 for (c = 0; c < miw; c++)
1143 seg_id_supertx =
Yaowu Xuf883b422016-08-30 14:01:10 -07001144 AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001145 assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);
1146 }
1147
1148 // Assign the the segment_id back to segment_id_supertx
1149 for (r = 0; r < mih; r++)
1150 for (c = 0; c < miw; c++)
1151 mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
1152}
1153#endif // CONFIG_SUPERTX
1154
Yaowu Xuf883b422016-08-30 14:01:10 -07001155static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001156#if CONFIG_SUPERTX
1157 int supertx_enabled,
1158#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001159 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001160#if CONFIG_EXT_PARTITION_TYPES
1161 PARTITION_TYPE partition,
1162#endif // CONFIG_EXT_PARTITION_TYPES
1163 BLOCK_SIZE bsize, int bwl, int bhl) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001164 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001165 const int less8x8 = bsize < BLOCK_8X8;
1166 const int bw = 1 << (bwl - 1);
1167 const int bh = 1 << (bhl - 1);
Yaowu Xuf883b422016-08-30 14:01:10 -07001168 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1169 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001170
1171#if CONFIG_SUPERTX
1172 MB_MODE_INFO *mbmi;
1173 if (supertx_enabled) {
1174 mbmi = set_mb_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
1175 } else {
1176 mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis, bwl,
1177 bhl);
1178 }
1179#if CONFIG_EXT_PARTITION_TYPES
1180 xd->mi[0]->mbmi.partition = partition;
1181#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001182 av1_read_mode_info(pbi, xd, supertx_enabled, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001183#else
1184 MB_MODE_INFO *mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis,
1185 y_mis, bwl, bhl);
1186#if CONFIG_EXT_PARTITION_TYPES
1187 xd->mi[0]->mbmi.partition = partition;
1188#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001189 av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001190#endif // CONFIG_SUPERTX
1191
1192 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1193 const BLOCK_SIZE uv_subsize =
1194 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1195 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -07001196 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001197 "Invalid block size.");
1198 }
1199
1200#if CONFIG_SUPERTX
1201 mbmi->segment_id_supertx = MAX_SEGMENTS;
1202
1203 if (supertx_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001204 xd->corrupted |= aom_reader_has_error(r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001205 return;
1206 }
1207#endif // CONFIG_SUPERTX
1208
1209 if (mbmi->skip) {
1210 dec_reset_skip_context(xd);
1211 }
1212 if (!is_inter_block(mbmi)) {
1213 int plane;
1214 for (plane = 0; plane <= 1; ++plane) {
1215 if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xuf883b422016-08-30 14:01:10 -07001216 av1_decode_palette_tokens(xd, plane, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001217 }
1218 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1219 const struct macroblockd_plane *const pd = &xd->plane[plane];
Debargha Mukherjee2f123402016-08-30 17:43:38 -07001220 const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001221 const int num_4x4_w = pd->n4_w;
1222 const int num_4x4_h = pd->n4_h;
1223 const int stepr = num_4x4_blocks_high_txsize_lookup[tx_size];
1224 const int stepc = num_4x4_blocks_wide_txsize_lookup[tx_size];
1225 int row, col;
1226 const int max_blocks_wide =
1227 num_4x4_w + (xd->mb_to_right_edge >= 0
1228 ? 0
1229 : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
1230 const int max_blocks_high =
1231 num_4x4_h + (xd->mb_to_bottom_edge >= 0
1232 ? 0
1233 : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
1234
1235 for (row = 0; row < max_blocks_high; row += stepr)
1236 for (col = 0; col < max_blocks_wide; col += stepc)
1237 predict_and_reconstruct_intra_block(xd, r, mbmi, plane, row, col,
1238 tx_size);
1239 }
1240 } else {
1241 // Prediction
Yaowu Xuf883b422016-08-30 14:01:10 -07001242 av1_build_inter_predictors_sb(xd, mi_row, mi_col, AOMMAX(bsize, BLOCK_8X8));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001243#if CONFIG_OBMC
1244 if (mbmi->motion_variation == OBMC_CAUSAL) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001245#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001246 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[2 * MAX_MB_PLANE * MAX_SB_SQUARE]);
1247 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[2 * MAX_MB_PLANE * MAX_SB_SQUARE]);
1248#else
1249 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_SB_SQUARE]);
1250 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_SB_SQUARE]);
Yaowu Xuf883b422016-08-30 14:01:10 -07001251#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001252 uint8_t *dst_buf1[MAX_MB_PLANE], *dst_buf2[MAX_MB_PLANE];
1253 int dst_width1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
1254 int dst_width2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
1255 int dst_height1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
1256 int dst_height2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
1257 int dst_stride1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
1258 int dst_stride2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
1259
1260 assert(mbmi->sb_type >= BLOCK_8X8);
Yaowu Xuf883b422016-08-30 14:01:10 -07001261#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001262 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1263 int len = sizeof(uint16_t);
1264 dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
1265 dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_SB_SQUARE * len);
1266 dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_SB_SQUARE * 2 * len);
1267 dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
1268 dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_SB_SQUARE * len);
1269 dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_SB_SQUARE * 2 * len);
1270 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001271#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001272 dst_buf1[0] = tmp_buf1;
1273 dst_buf1[1] = tmp_buf1 + MAX_SB_SQUARE;
1274 dst_buf1[2] = tmp_buf1 + MAX_SB_SQUARE * 2;
1275 dst_buf2[0] = tmp_buf2;
1276 dst_buf2[1] = tmp_buf2 + MAX_SB_SQUARE;
1277 dst_buf2[2] = tmp_buf2 + MAX_SB_SQUARE * 2;
Yaowu Xuf883b422016-08-30 14:01:10 -07001278#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001279 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001280#endif // CONFIG_AOM_HIGHBITDEPTH
1281 av1_build_prediction_by_above_preds(cm, xd, mi_row, mi_col, dst_buf1,
1282 dst_width1, dst_height1, dst_stride1);
1283 av1_build_prediction_by_left_preds(cm, xd, mi_row, mi_col, dst_buf2,
1284 dst_width2, dst_height2, dst_stride2);
1285 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
1286 av1_build_obmc_inter_prediction(cm, xd, mi_row, mi_col, dst_buf1,
1287 dst_stride1, dst_buf2, dst_stride2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001288 }
1289#endif // CONFIG_OBMC
1290
1291 // Reconstruction
1292 if (!mbmi->skip) {
1293 int eobtotal = 0;
1294 int plane;
1295
1296 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1297 const struct macroblockd_plane *const pd = &xd->plane[plane];
1298 const int num_4x4_w = pd->n4_w;
1299 const int num_4x4_h = pd->n4_h;
1300 int row, col;
1301#if CONFIG_VAR_TX
1302 // TODO(jingning): This can be simplified for decoder performance.
1303 const BLOCK_SIZE plane_bsize =
Yaowu Xuf883b422016-08-30 14:01:10 -07001304 get_plane_block_size(AOMMAX(bsize, BLOCK_8X8), pd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001305 const TX_SIZE max_tx_size = max_txsize_lookup[plane_bsize];
1306 int bw = num_4x4_blocks_wide_txsize_lookup[max_tx_size];
1307 int bh = num_4x4_blocks_high_txsize_lookup[max_tx_size];
1308 const int step = num_4x4_blocks_txsize_lookup[max_tx_size];
1309 int block = 0;
1310#if CONFIG_EXT_TX && CONFIG_RECT_TX
Yue Chena1e48dc2016-08-29 17:29:33 -07001311 if (is_rect_tx(mbmi->tx_size)) {
1312 const TX_SIZE tx_size =
1313 plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001314 const int stepr = num_4x4_blocks_high_txsize_lookup[tx_size];
1315 const int stepc = num_4x4_blocks_wide_txsize_lookup[tx_size];
1316 const int max_blocks_wide =
1317 num_4x4_w +
1318 (xd->mb_to_right_edge >= 0 ? 0 : xd->mb_to_right_edge >>
1319 (5 + pd->subsampling_x));
1320 const int max_blocks_high =
1321 num_4x4_h +
1322 (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >>
1323 (5 + pd->subsampling_y));
1324
1325 for (row = 0; row < max_blocks_high; row += stepr)
1326 for (col = 0; col < max_blocks_wide; col += stepc)
1327 eobtotal += reconstruct_inter_block(xd, r, mbmi->segment_id,
1328 plane, row, col, tx_size);
1329 } else {
1330#endif
1331 for (row = 0; row < num_4x4_h; row += bh) {
1332 for (col = 0; col < num_4x4_w; col += bw) {
1333 decode_reconstruct_tx(xd, r, mbmi, plane, plane_bsize, block, row,
1334 col, max_tx_size, &eobtotal);
1335 block += step;
1336 }
1337 }
1338#if CONFIG_EXT_TX && CONFIG_RECT_TX
1339 }
1340#endif
1341#else
1342 const TX_SIZE tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -07001343 plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001344 const int stepr = num_4x4_blocks_high_txsize_lookup[tx_size];
1345 const int stepc = num_4x4_blocks_wide_txsize_lookup[tx_size];
1346 const int max_blocks_wide =
1347 num_4x4_w + (xd->mb_to_right_edge >= 0
1348 ? 0
1349 : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
1350 const int max_blocks_high =
1351 num_4x4_h +
1352 (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >>
1353 (5 + pd->subsampling_y));
1354
1355 for (row = 0; row < max_blocks_high; row += stepr)
1356 for (col = 0; col < max_blocks_wide; col += stepc)
1357 eobtotal += reconstruct_inter_block(xd, r, mbmi->segment_id, plane,
1358 row, col, tx_size);
1359#endif
1360 }
1361
1362 if (!less8x8 && eobtotal == 0)
1363 mbmi->has_no_coeffs = 1; // skip loopfilter
1364 }
1365 }
1366
Yaowu Xuf883b422016-08-30 14:01:10 -07001367 xd->corrupted |= aom_reader_has_error(r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001368}
1369
1370static INLINE int dec_partition_plane_context(const MACROBLOCKD *xd, int mi_row,
1371 int mi_col, int bsl) {
1372 const PARTITION_CONTEXT *above_ctx = xd->above_seg_context + mi_col;
1373 const PARTITION_CONTEXT *left_ctx =
1374 xd->left_seg_context + (mi_row & MAX_MIB_MASK);
1375 int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1;
1376
1377 // assert(bsl >= 0);
1378
1379 return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
1380}
1381
1382#if !CONFIG_EXT_PARTITION_TYPES
1383static INLINE void dec_update_partition_context(MACROBLOCKD *xd, int mi_row,
1384 int mi_col, BLOCK_SIZE subsize,
1385 int bw) {
1386 PARTITION_CONTEXT *const above_ctx = xd->above_seg_context + mi_col;
1387 PARTITION_CONTEXT *const left_ctx =
1388 xd->left_seg_context + (mi_row & MAX_MIB_MASK);
1389
1390 // update the partition context at the end notes. set partition bits
1391 // of block sizes larger than the current one to be one, and partition
1392 // bits of smaller block sizes to be zero.
1393 memset(above_ctx, partition_context_lookup[subsize].above, bw);
1394 memset(left_ctx, partition_context_lookup[subsize].left, bw);
1395}
1396#endif // !CONFIG_EXT_PARTITION_TYPES
1397
Yaowu Xuf883b422016-08-30 14:01:10 -07001398static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
1399 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001400 int has_rows, int has_cols,
1401#if CONFIG_EXT_PARTITION_TYPES
1402 BLOCK_SIZE bsize,
1403#endif
1404 int bsl) {
1405 const int ctx = dec_partition_plane_context(xd, mi_row, mi_col, bsl);
Yaowu Xuf883b422016-08-30 14:01:10 -07001406 const aom_prob *const probs = cm->fc->partition_prob[ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001407 FRAME_COUNTS *counts = xd->counts;
1408 PARTITION_TYPE p;
1409
1410 if (has_rows && has_cols)
1411#if CONFIG_EXT_PARTITION_TYPES
1412 if (bsize <= BLOCK_8X8)
Yaowu Xuf883b422016-08-30 14:01:10 -07001413 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001414 else
Yaowu Xuf883b422016-08-30 14:01:10 -07001415 p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001416#else
Yaowu Xuf883b422016-08-30 14:01:10 -07001417 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001418#endif // CONFIG_EXT_PARTITION_TYPES
1419 else if (!has_rows && has_cols)
Yaowu Xuf883b422016-08-30 14:01:10 -07001420 p = aom_read(r, probs[1]) ? PARTITION_SPLIT : PARTITION_HORZ;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001421 else if (has_rows && !has_cols)
Yaowu Xuf883b422016-08-30 14:01:10 -07001422 p = aom_read(r, probs[2]) ? PARTITION_SPLIT : PARTITION_VERT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001423 else
1424 p = PARTITION_SPLIT;
1425
1426 if (counts) ++counts->partition[ctx][p];
1427
1428 return p;
1429}
1430
1431#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001432static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
1433 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001434 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
1435 return 1;
1436 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001437 const int ctx = av1_get_skip_context(xd);
1438 const int skip = aom_read(r, cm->fc->skip_probs[ctx]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001439 FRAME_COUNTS *counts = xd->counts;
1440 if (counts) ++counts->skip[ctx][skip];
1441 return skip;
1442 }
1443}
1444#endif // CONFIG_SUPERTX
1445
1446// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -07001447static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001448#if CONFIG_SUPERTX
1449 int supertx_enabled,
1450#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001451 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001452 BLOCK_SIZE bsize, int n4x4_l2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001453 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001454 const int n8x8_l2 = n4x4_l2 - 1;
1455 const int num_8x8_wh = 1 << n8x8_l2;
1456 const int hbs = num_8x8_wh >> 1;
1457 PARTITION_TYPE partition;
1458 BLOCK_SIZE subsize;
1459#if CONFIG_EXT_PARTITION_TYPES
1460 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1461#endif
1462 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1463 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1464#if CONFIG_SUPERTX
1465 const int read_token = !supertx_enabled;
1466 int skip = 0;
1467 TX_SIZE supertx_size = b_width_log2_lookup[bsize];
1468 const TileInfo *const tile = &xd->tile;
1469 int txfm = DCT_DCT;
1470#endif // CONFIG_SUPERTX
1471
1472 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1473
1474 partition = read_partition(cm, xd, mi_row, mi_col, r, has_rows, has_cols,
1475#if CONFIG_EXT_PARTITION_TYPES
1476 bsize,
1477#endif
1478 n8x8_l2);
1479 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
1480#if CONFIG_SUPERTX
1481 if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
1482 bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
1483 const int supertx_context = partition_supertx_context_lookup[partition];
1484 supertx_enabled =
Yaowu Xuf883b422016-08-30 14:01:10 -07001485 aom_read(r, cm->fc->supertx_prob[supertx_context][supertx_size]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001486 if (xd->counts)
1487 xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
1488#if CONFIG_VAR_TX
1489 if (supertx_enabled) xd->supertx_size = supertx_size;
1490#endif
1491 }
1492#endif // CONFIG_SUPERTX
1493 if (!hbs) {
1494 // calculate bmode block dimensions (log 2)
1495 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1496 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1497 decode_block(pbi, xd,
1498#if CONFIG_SUPERTX
1499 supertx_enabled,
1500#endif // CONFIG_SUPERTX
1501 mi_row, mi_col, r,
1502#if CONFIG_EXT_PARTITION_TYPES
1503 partition,
1504#endif // CONFIG_EXT_PARTITION_TYPES
1505 subsize, 1, 1);
1506 } else {
1507 switch (partition) {
1508 case PARTITION_NONE:
1509 decode_block(pbi, xd,
1510#if CONFIG_SUPERTX
1511 supertx_enabled,
1512#endif // CONFIG_SUPERTX
1513 mi_row, mi_col, r,
1514#if CONFIG_EXT_PARTITION_TYPES
1515 partition,
1516#endif // CONFIG_EXT_PARTITION_TYPES
1517 subsize, n4x4_l2, n4x4_l2);
1518 break;
1519 case PARTITION_HORZ:
1520 decode_block(pbi, xd,
1521#if CONFIG_SUPERTX
1522 supertx_enabled,
1523#endif // CONFIG_SUPERTX
1524 mi_row, mi_col, r,
1525#if CONFIG_EXT_PARTITION_TYPES
1526 partition,
1527#endif // CONFIG_EXT_PARTITION_TYPES
1528 subsize, n4x4_l2, n8x8_l2);
1529 if (has_rows)
1530 decode_block(pbi, xd,
1531#if CONFIG_SUPERTX
1532 supertx_enabled,
1533#endif // CONFIG_SUPERTX
1534 mi_row + hbs, mi_col, r,
1535#if CONFIG_EXT_PARTITION_TYPES
1536 partition,
1537#endif // CONFIG_EXT_PARTITION_TYPES
1538 subsize, n4x4_l2, n8x8_l2);
1539 break;
1540 case PARTITION_VERT:
1541 decode_block(pbi, xd,
1542#if CONFIG_SUPERTX
1543 supertx_enabled,
1544#endif // CONFIG_SUPERTX
1545 mi_row, mi_col, r,
1546#if CONFIG_EXT_PARTITION_TYPES
1547 partition,
1548#endif // CONFIG_EXT_PARTITION_TYPES
1549 subsize, n8x8_l2, n4x4_l2);
1550 if (has_cols)
1551 decode_block(pbi, xd,
1552#if CONFIG_SUPERTX
1553 supertx_enabled,
1554#endif // CONFIG_SUPERTX
1555 mi_row, mi_col + hbs, r,
1556#if CONFIG_EXT_PARTITION_TYPES
1557 partition,
1558#endif // CONFIG_EXT_PARTITION_TYPES
1559 subsize, n8x8_l2, n4x4_l2);
1560 break;
1561 case PARTITION_SPLIT:
1562 decode_partition(pbi, xd,
1563#if CONFIG_SUPERTX
1564 supertx_enabled,
1565#endif // CONFIG_SUPERTX
1566 mi_row, mi_col, r, subsize, n8x8_l2);
1567 decode_partition(pbi, xd,
1568#if CONFIG_SUPERTX
1569 supertx_enabled,
1570#endif // CONFIG_SUPERTX
1571 mi_row, mi_col + hbs, r, subsize, n8x8_l2);
1572 decode_partition(pbi, xd,
1573#if CONFIG_SUPERTX
1574 supertx_enabled,
1575#endif // CONFIG_SUPERTX
1576 mi_row + hbs, mi_col, r, subsize, n8x8_l2);
1577 decode_partition(pbi, xd,
1578#if CONFIG_SUPERTX
1579 supertx_enabled,
1580#endif // CONFIG_SUPERTX
1581 mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
1582 break;
1583#if CONFIG_EXT_PARTITION_TYPES
1584 case PARTITION_HORZ_A:
1585 decode_block(pbi, xd,
1586#if CONFIG_SUPERTX
1587 supertx_enabled,
1588#endif
1589 mi_row, mi_col, r, partition, bsize2, n8x8_l2, n8x8_l2);
1590 decode_block(pbi, xd,
1591#if CONFIG_SUPERTX
1592 supertx_enabled,
1593#endif
1594 mi_row, mi_col + hbs, r, partition, bsize2, n8x8_l2,
1595 n8x8_l2);
1596 decode_block(pbi, xd,
1597#if CONFIG_SUPERTX
1598 supertx_enabled,
1599#endif
1600 mi_row + hbs, mi_col, r, partition, subsize, n4x4_l2,
1601 n8x8_l2);
1602 break;
1603 case PARTITION_HORZ_B:
1604 decode_block(pbi, xd,
1605#if CONFIG_SUPERTX
1606 supertx_enabled,
1607#endif
1608 mi_row, mi_col, r, partition, subsize, n4x4_l2, n8x8_l2);
1609 decode_block(pbi, xd,
1610#if CONFIG_SUPERTX
1611 supertx_enabled,
1612#endif
1613 mi_row + hbs, mi_col, r, partition, bsize2, n8x8_l2,
1614 n8x8_l2);
1615 decode_block(pbi, xd,
1616#if CONFIG_SUPERTX
1617 supertx_enabled,
1618#endif
1619 mi_row + hbs, mi_col + hbs, r, partition, bsize2, n8x8_l2,
1620 n8x8_l2);
1621 break;
1622 case PARTITION_VERT_A:
1623 decode_block(pbi, xd,
1624#if CONFIG_SUPERTX
1625 supertx_enabled,
1626#endif
1627 mi_row, mi_col, r, partition, bsize2, n8x8_l2, n8x8_l2);
1628 decode_block(pbi, xd,
1629#if CONFIG_SUPERTX
1630 supertx_enabled,
1631#endif
1632 mi_row + hbs, mi_col, r, partition, bsize2, n8x8_l2,
1633 n8x8_l2);
1634 decode_block(pbi, xd,
1635#if CONFIG_SUPERTX
1636 supertx_enabled,
1637#endif
1638 mi_row, mi_col + hbs, r, partition, subsize, n8x8_l2,
1639 n4x4_l2);
1640 break;
1641 case PARTITION_VERT_B:
1642 decode_block(pbi, xd,
1643#if CONFIG_SUPERTX
1644 supertx_enabled,
1645#endif
1646 mi_row, mi_col, r, partition, subsize, n8x8_l2, n4x4_l2);
1647 decode_block(pbi, xd,
1648#if CONFIG_SUPERTX
1649 supertx_enabled,
1650#endif
1651 mi_row, mi_col + hbs, r, partition, bsize2, n8x8_l2,
1652 n8x8_l2);
1653 decode_block(pbi, xd,
1654#if CONFIG_SUPERTX
1655 supertx_enabled,
1656#endif
1657 mi_row + hbs, mi_col + hbs, r, partition, bsize2, n8x8_l2,
1658 n8x8_l2);
1659 break;
1660#endif
1661 default: assert(0 && "Invalid partition type");
1662 }
1663 }
1664
1665#if CONFIG_SUPERTX
1666 if (supertx_enabled && read_token) {
1667 uint8_t *dst_buf[3];
1668 int dst_stride[3], i;
1669 int offset = mi_row * cm->mi_stride + mi_col;
1670
1671 set_segment_id_supertx(cm, mi_row, mi_col, bsize);
1672
1673 xd->mi = cm->mi_grid_visible + offset;
1674 xd->mi[0] = cm->mi + offset;
1675 set_mi_row_col(xd, tile, mi_row, num_8x8_blocks_high_lookup[bsize], mi_col,
1676 num_8x8_blocks_wide_lookup[bsize], cm->mi_rows, cm->mi_cols);
1677 set_skip_context(xd, mi_row, mi_col);
1678 skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
1679 if (skip) {
1680 reset_skip_context(xd, bsize);
1681 } else {
1682#if CONFIG_EXT_TX
1683 if (get_ext_tx_types(supertx_size, bsize, 1) > 1) {
1684 int eset = get_ext_tx_set(supertx_size, bsize, 1);
1685 if (eset > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001686 txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
1687 cm->fc->inter_ext_tx_prob[eset][supertx_size]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001688 if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
1689 }
1690 }
1691#else
1692 if (supertx_size < TX_32X32) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001693 txfm = aom_read_tree(r, av1_ext_tx_tree,
1694 cm->fc->inter_ext_tx_prob[supertx_size]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001695 if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
1696 }
1697#endif // CONFIG_EXT_TX
1698 }
1699
Yaowu Xuf883b422016-08-30 14:01:10 -07001700 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001701 for (i = 0; i < MAX_MB_PLANE; i++) {
1702 dst_buf[i] = xd->plane[i].dst.buf;
1703 dst_stride[i] = xd->plane[i].dst.stride;
1704 }
1705 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
1706 bsize, dst_buf, dst_stride);
1707
1708 if (!skip) {
1709 int eobtotal = 0;
1710 MB_MODE_INFO *mbmi;
1711 set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
1712 mbmi = &xd->mi[0]->mbmi;
1713 mbmi->tx_type = txfm;
1714 assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
1715 for (i = 0; i < MAX_MB_PLANE; ++i) {
1716 const struct macroblockd_plane *const pd = &xd->plane[i];
1717 const int num_4x4_w = pd->n4_w;
1718 const int num_4x4_h = pd->n4_h;
1719 int row, col;
Debargha Mukherjee2f123402016-08-30 17:43:38 -07001720 const TX_SIZE tx_size = i ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001721 const int stepr = num_4x4_blocks_high_txsize_lookup[tx_size];
1722 const int stepc = num_4x4_blocks_wide_txsize_lookup[tx_size];
1723 const int max_blocks_wide =
1724 num_4x4_w + (xd->mb_to_right_edge >= 0
1725 ? 0
1726 : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
1727 const int max_blocks_high =
1728 num_4x4_h +
1729 (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >>
1730 (5 + pd->subsampling_y));
1731
1732 for (row = 0; row < max_blocks_high; row += stepr)
1733 for (col = 0; col < max_blocks_wide; col += stepc)
1734 eobtotal += reconstruct_inter_block(xd, r, mbmi->segment_id_supertx,
1735 i, row, col, tx_size);
1736 }
1737 if (!(subsize < BLOCK_8X8) && eobtotal == 0) skip = 1;
1738 }
1739 set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
1740 }
1741#endif // CONFIG_SUPERTX
1742
1743#if CONFIG_EXT_PARTITION_TYPES
1744 if (bsize >= BLOCK_8X8) {
1745 switch (partition) {
1746 case PARTITION_SPLIT:
1747 if (bsize > BLOCK_8X8) break;
1748 case PARTITION_NONE:
1749 case PARTITION_HORZ:
1750 case PARTITION_VERT:
1751 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
1752 break;
1753 case PARTITION_HORZ_A:
1754 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
1755 update_partition_context(xd, mi_row + hbs, mi_col, subsize, subsize);
1756 break;
1757 case PARTITION_HORZ_B:
1758 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
1759 update_partition_context(xd, mi_row + hbs, mi_col, bsize2, subsize);
1760 break;
1761 case PARTITION_VERT_A:
1762 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
1763 update_partition_context(xd, mi_row, mi_col + hbs, subsize, subsize);
1764 break;
1765 case PARTITION_VERT_B:
1766 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
1767 update_partition_context(xd, mi_row, mi_col + hbs, bsize2, subsize);
1768 break;
1769 default: assert(0 && "Invalid partition type");
1770 }
1771 }
1772#else
1773 // update partition context
1774 if (bsize >= BLOCK_8X8 &&
1775 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
1776 dec_update_partition_context(xd, mi_row, mi_col, subsize, num_8x8_wh);
1777#if DERING_REFINEMENT
1778 if (bsize == BLOCK_64X64) {
1779 if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
1780 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
Yaowu Xuf883b422016-08-30 14:01:10 -07001781 aom_read_literal(r, DERING_REFINEMENT_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001782 } else {
1783 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
1784 0;
1785 }
1786 }
1787#endif // DERGING_REFINEMENT
1788#endif // CONFIG_EXT_PARTITION_TYPES
1789}
1790
1791#if !CONFIG_ANS
1792static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
1793 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07001794 struct aom_internal_error_info *error_info,
1795 aom_reader *r, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001796 void *decrypt_state) {
1797 // Validate the calculated partition length. If the buffer
1798 // described by the partition can't be fully read, then restrict
1799 // it to the portion that can be (for EC mode) or throw an error.
1800 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07001801 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001802 "Truncated packet or corrupt tile length");
1803
Yaowu Xuf883b422016-08-30 14:01:10 -07001804 if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
1805 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001806 "Failed to allocate bool decoder %d", 1);
1807}
1808#else
1809static void setup_token_decoder(const uint8_t *data, const uint8_t *data_end,
1810 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07001811 struct aom_internal_error_info *error_info,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001812 struct AnsDecoder *const ans,
Yaowu Xuf883b422016-08-30 14:01:10 -07001813 aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001814 void *decrypt_state) {
1815 (void)decrypt_cb;
1816 (void)decrypt_state;
1817 // Validate the calculated partition length. If the buffer
1818 // described by the partition can't be fully read, then restrict
1819 // it to the portion that can be (for EC mode) or throw an error.
1820 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07001821 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001822 "Truncated packet or corrupt tile length");
1823
1824 if (read_size > INT_MAX || ans_read_init(ans, data, (int)read_size))
Yaowu Xuf883b422016-08-30 14:01:10 -07001825 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001826 "Failed to allocate token decoder %d", 1);
1827}
1828#endif
1829
Yaowu Xuf883b422016-08-30 14:01:10 -07001830static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
1831 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001832 int i, j, k, l, m;
1833
Yaowu Xuf883b422016-08-30 14:01:10 -07001834 if (aom_read_bit(r))
Yaowu Xuc27fc142016-08-22 16:08:15 -07001835 for (i = 0; i < PLANE_TYPES; ++i)
1836 for (j = 0; j < REF_TYPES; ++j)
1837 for (k = 0; k < COEF_BANDS; ++k)
1838 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
1839 for (m = 0; m < UNCONSTRAINED_NODES; ++m)
Yaowu Xuf883b422016-08-30 14:01:10 -07001840 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001841}
1842
Yaowu Xuf883b422016-08-30 14:01:10 -07001843static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001844 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
1845 TX_SIZE tx_size;
1846 for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size)
1847 read_coef_probs_common(fc->coef_probs[tx_size], r);
1848#if CONFIG_ANS
Yaowu Xuf883b422016-08-30 14:01:10 -07001849 av1_coef_pareto_cdfs(fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001850#endif // CONFIG_ANS
1851}
1852
Yaowu Xuf883b422016-08-30 14:01:10 -07001853static void setup_segmentation(AV1_COMMON *const cm,
1854 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001855 struct segmentation *const seg = &cm->seg;
1856 int i, j;
1857
1858 seg->update_map = 0;
1859 seg->update_data = 0;
1860
Yaowu Xuf883b422016-08-30 14:01:10 -07001861 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001862 if (!seg->enabled) return;
1863
1864 // Segmentation map update
1865 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
1866 seg->update_map = 1;
1867 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001868 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001869 }
1870 if (seg->update_map) {
1871 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
1872 seg->temporal_update = 0;
1873 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001874 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001875 }
1876 }
1877
1878 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07001879 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001880 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001881 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001882
Yaowu Xuf883b422016-08-30 14:01:10 -07001883 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001884
1885 for (i = 0; i < MAX_SEGMENTS; i++) {
1886 for (j = 0; j < SEG_LVL_MAX; j++) {
1887 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001888 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001889 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001890 av1_enable_segfeature(seg, i, j);
1891 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
1892 if (av1_is_segfeature_signed(j))
1893 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001894 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001895 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001896 }
1897 }
1898 }
1899}
1900
1901#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -07001902static void setup_restoration(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001903 int i;
1904 RestorationInfo *rsi = &cm->rst_info;
1905 int ntiles;
Yaowu Xuf883b422016-08-30 14:01:10 -07001906 if (aom_rb_read_bit(rb)) {
1907 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001908 rsi->restoration_type = RESTORE_BILATERAL;
Yaowu Xuf883b422016-08-30 14:01:10 -07001909 ntiles =
1910 av1_get_restoration_ntiles(BILATERAL_TILESIZE, cm->width, cm->height);
1911 rsi->bilateral_level = (int *)aom_realloc(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001912 rsi->bilateral_level, sizeof(*rsi->bilateral_level) * ntiles);
1913 assert(rsi->bilateral_level != NULL);
1914 for (i = 0; i < ntiles; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001915 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001916 rsi->bilateral_level[i] =
Yaowu Xuf883b422016-08-30 14:01:10 -07001917 aom_rb_read_literal(rb, av1_bilateral_level_bits(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001918 } else {
1919 rsi->bilateral_level[i] = -1;
1920 }
1921 }
1922 } else {
1923 rsi->restoration_type = RESTORE_WIENER;
1924 ntiles =
Yaowu Xuf883b422016-08-30 14:01:10 -07001925 av1_get_restoration_ntiles(WIENER_TILESIZE, cm->width, cm->height);
1926 rsi->wiener_level = (int *)aom_realloc(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001927 rsi->wiener_level, sizeof(*rsi->wiener_level) * ntiles);
1928 assert(rsi->wiener_level != NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -07001929 rsi->vfilter = (int(*)[RESTORATION_HALFWIN])aom_realloc(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001930 rsi->vfilter, sizeof(*rsi->vfilter) * ntiles);
1931 assert(rsi->vfilter != NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -07001932 rsi->hfilter = (int(*)[RESTORATION_HALFWIN])aom_realloc(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001933 rsi->hfilter, sizeof(*rsi->hfilter) * ntiles);
1934 assert(rsi->hfilter != NULL);
1935 for (i = 0; i < ntiles; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001936 rsi->wiener_level[i] = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001937 if (rsi->wiener_level[i]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001938 rsi->vfilter[i][0] = aom_rb_read_literal(rb, WIENER_FILT_TAP0_BITS) +
Yaowu Xuc27fc142016-08-22 16:08:15 -07001939 WIENER_FILT_TAP0_MINV;
Yaowu Xuf883b422016-08-30 14:01:10 -07001940 rsi->vfilter[i][1] = aom_rb_read_literal(rb, WIENER_FILT_TAP1_BITS) +
Yaowu Xuc27fc142016-08-22 16:08:15 -07001941 WIENER_FILT_TAP1_MINV;
Yaowu Xuf883b422016-08-30 14:01:10 -07001942 rsi->vfilter[i][2] = aom_rb_read_literal(rb, WIENER_FILT_TAP2_BITS) +
Yaowu Xuc27fc142016-08-22 16:08:15 -07001943 WIENER_FILT_TAP2_MINV;
Yaowu Xuf883b422016-08-30 14:01:10 -07001944 rsi->hfilter[i][0] = aom_rb_read_literal(rb, WIENER_FILT_TAP0_BITS) +
Yaowu Xuc27fc142016-08-22 16:08:15 -07001945 WIENER_FILT_TAP0_MINV;
Yaowu Xuf883b422016-08-30 14:01:10 -07001946 rsi->hfilter[i][1] = aom_rb_read_literal(rb, WIENER_FILT_TAP1_BITS) +
Yaowu Xuc27fc142016-08-22 16:08:15 -07001947 WIENER_FILT_TAP1_MINV;
Yaowu Xuf883b422016-08-30 14:01:10 -07001948 rsi->hfilter[i][2] = aom_rb_read_literal(rb, WIENER_FILT_TAP2_BITS) +
Yaowu Xuc27fc142016-08-22 16:08:15 -07001949 WIENER_FILT_TAP2_MINV;
1950 } else {
1951 rsi->vfilter[i][0] = rsi->vfilter[i][1] = rsi->vfilter[i][2] = 0;
1952 rsi->hfilter[i][0] = rsi->hfilter[i][1] = rsi->hfilter[i][2] = 0;
1953 }
1954 }
1955 }
1956 } else {
1957 rsi->restoration_type = RESTORE_NONE;
1958 }
1959}
1960#endif // CONFIG_LOOP_RESTORATION
1961
Yaowu Xuf883b422016-08-30 14:01:10 -07001962static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001963 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07001964 lf->filter_level = aom_rb_read_literal(rb, 6);
1965 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001966
1967 // Read in loop filter deltas applied at the MB level based on mode or ref
1968 // frame.
1969 lf->mode_ref_delta_update = 0;
1970
Yaowu Xuf883b422016-08-30 14:01:10 -07001971 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001972 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001973 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001974 if (lf->mode_ref_delta_update) {
1975 int i;
1976
1977 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07001978 if (aom_rb_read_bit(rb))
1979 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001980
1981 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07001982 if (aom_rb_read_bit(rb))
1983 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001984 }
1985 }
1986}
1987
1988#if CONFIG_CLPF
Yaowu Xuf883b422016-08-30 14:01:10 -07001989static void setup_clpf(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
1990 cm->clpf = aom_rb_read_literal(rb, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001991}
1992#endif
1993
1994#if CONFIG_DERING
Yaowu Xuf883b422016-08-30 14:01:10 -07001995static void setup_dering(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
1996 cm->dering_level = aom_rb_read_literal(rb, DERING_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001997}
1998#endif // CONFIG_DERING
1999
Yaowu Xuf883b422016-08-30 14:01:10 -07002000static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2001 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002002}
2003
Yaowu Xuf883b422016-08-30 14:01:10 -07002004static void setup_quantization(AV1_COMMON *const cm,
2005 struct aom_read_bit_buffer *rb) {
2006 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002007 cm->y_dc_delta_q = read_delta_q(rb);
2008 cm->uv_dc_delta_q = read_delta_q(rb);
2009 cm->uv_ac_delta_q = read_delta_q(rb);
2010 cm->dequant_bit_depth = cm->bit_depth;
2011#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002012 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002013 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002014 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2015 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002016 } else {
2017 cm->min_qmlevel = 0;
2018 cm->max_qmlevel = 0;
2019 }
2020#endif
2021}
2022
Yaowu Xuf883b422016-08-30 14:01:10 -07002023static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002024 // Build y/uv dequant values based on segmentation.
2025 int i = 0;
2026#if CONFIG_AOM_QM
2027 int lossless;
2028 int j = 0;
2029 int qmlevel;
2030 int using_qm = cm->using_qmatrix;
2031 int minqm = cm->min_qmlevel;
2032 int maxqm = cm->max_qmlevel;
2033#endif
2034#if CONFIG_NEW_QUANT
2035 int b;
2036 int dq;
2037#endif // CONFIG_NEW_QUANT
2038 if (cm->seg.enabled) {
2039 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002040 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002041 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002042 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2043 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002044 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002045 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002046 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002047 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002048#if CONFIG_AOM_QM
2049 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2050 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2051 // NB: depends on base index so there is only 1 set per frame
2052 // No quant weighting when lossless or signalled not using QM
2053 qmlevel = (lossless || using_qm == 0)
2054 ? NUM_QM_LEVELS - 1
2055 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2056 for (j = 0; j < TX_SIZES; ++j) {
2057 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2058 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2059 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2060 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2061 }
2062#endif // CONFIG_AOM_QM
2063#if CONFIG_NEW_QUANT
2064 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2065 for (b = 0; b < COEF_BANDS; ++b) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002066 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], qindex, b,
2067 cm->y_dequant_nuq[i][dq][b], NULL, dq);
2068 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], qindex, b,
2069 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002070 }
2071 }
2072#endif // CONFIG_NEW_QUANT
2073 }
2074 } else {
2075 const int qindex = cm->base_qindex;
2076 // When segmentation is disabled, only the first value is used. The
2077 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002078 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2079 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002080 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002081 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002082 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002083 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002084#if CONFIG_AOM_QM
2085 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2086 cm->uv_ac_delta_q == 0;
2087 // No quant weighting when lossless or signalled not using QM
2088 qmlevel = (lossless || using_qm == 0)
2089 ? NUM_QM_LEVELS - 1
2090 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2091 for (j = 0; j < TX_SIZES; ++j) {
2092 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2093 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2094 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2095 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2096 }
2097#endif
2098#if CONFIG_NEW_QUANT
2099 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2100 for (b = 0; b < COEF_BANDS; ++b) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002101 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], qindex, b,
2102 cm->y_dequant_nuq[0][dq][b], NULL, dq);
2103 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], qindex, b,
2104 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002105 }
2106 }
2107#endif // CONFIG_NEW_QUANT
2108 }
2109}
2110
Yaowu Xuf883b422016-08-30 14:01:10 -07002111static INTERP_FILTER read_interp_filter(struct aom_read_bit_buffer *rb) {
2112 return aom_rb_read_bit(rb) ? SWITCHABLE
2113 : aom_rb_read_literal(rb, 2 + CONFIG_EXT_INTERP);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002114}
2115
Yaowu Xuf883b422016-08-30 14:01:10 -07002116static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002117 cm->render_width = cm->width;
2118 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002119 if (aom_rb_read_bit(rb))
2120 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002121}
2122
Yaowu Xuf883b422016-08-30 14:01:10 -07002123static void resize_mv_buffer(AV1_COMMON *cm) {
2124 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002125 cm->cur_frame->mi_rows = cm->mi_rows;
2126 cm->cur_frame->mi_cols = cm->mi_cols;
2127 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002128 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002129 sizeof(*cm->cur_frame->mvs)));
2130}
2131
Yaowu Xuf883b422016-08-30 14:01:10 -07002132static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002133#if CONFIG_SIZE_LIMIT
2134 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002135 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002136 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2137 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2138#endif
2139 if (cm->width != width || cm->height != height) {
2140 const int new_mi_rows =
2141 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2142 const int new_mi_cols =
2143 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2144
Yaowu Xuf883b422016-08-30 14:01:10 -07002145 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002146 // dimensions as well as the overall size.
2147 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002148 if (av1_alloc_context_buffers(cm, width, height))
2149 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002150 "Failed to allocate context buffers");
2151 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002152 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002153 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002154 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002155 cm->width = width;
2156 cm->height = height;
2157 }
2158 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2159 cm->mi_cols > cm->cur_frame->mi_cols) {
2160 resize_mv_buffer(cm);
2161 }
2162}
2163
Yaowu Xuf883b422016-08-30 14:01:10 -07002164static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002165 int width, height;
2166 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002167 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002168 resize_context_buffers(cm, width, height);
2169 setup_render_size(cm, rb);
2170
2171 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002172 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002173 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2174 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002175#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002176 cm->use_highbitdepth,
2177#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002178 AOM_DEC_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002179 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2180 pool->cb_priv)) {
2181 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002182 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002183 "Failed to allocate frame buffer");
2184 }
2185 unlock_buffer_pool(pool);
2186
2187 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2188 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2189 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2190 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2191 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2192 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2193 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2194}
2195
Yaowu Xuf883b422016-08-30 14:01:10 -07002196static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002197 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002198 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002199 int this_xss, int this_yss) {
2200 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2201 ref_yss == this_yss;
2202}
2203
Yaowu Xuf883b422016-08-30 14:01:10 -07002204static void setup_frame_size_with_refs(AV1_COMMON *cm,
2205 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002206 int width, height;
2207 int found = 0, i;
2208 int has_valid_ref_frame = 0;
2209 BufferPool *const pool = cm->buffer_pool;
2210 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002211 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002212 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2213 width = buf->y_crop_width;
2214 height = buf->y_crop_height;
2215 cm->render_width = buf->render_width;
2216 cm->render_height = buf->render_height;
2217 found = 1;
2218 break;
2219 }
2220 }
2221
2222 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002223 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002224 setup_render_size(cm, rb);
2225 }
2226
2227 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002228 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002229 "Invalid frame size");
2230
2231 // Check to make sure at least one of frames that this frame references
2232 // has valid dimensions.
2233 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2234 RefBuffer *const ref_frame = &cm->frame_refs[i];
2235 has_valid_ref_frame |=
2236 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2237 ref_frame->buf->y_crop_height, width, height);
2238 }
2239 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002240 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002241 "Referenced frame has invalid size");
2242 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2243 RefBuffer *const ref_frame = &cm->frame_refs[i];
2244 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
2245 ref_frame->buf->subsampling_x,
2246 ref_frame->buf->subsampling_y, cm->bit_depth,
2247 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07002248 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002249 "Referenced frame has incompatible color format");
2250 }
2251
2252 resize_context_buffers(cm, width, height);
2253
2254 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002255 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002256 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2257 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002258#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002259 cm->use_highbitdepth,
2260#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002261 AOM_DEC_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002262 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2263 pool->cb_priv)) {
2264 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002265 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002266 "Failed to allocate frame buffer");
2267 }
2268 unlock_buffer_pool(pool);
2269
2270 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2271 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2272 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2273 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2274 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2275 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2276 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2277}
2278
Yaowu Xuf883b422016-08-30 14:01:10 -07002279static void read_tile_info(AV1Decoder *const pbi,
2280 struct aom_read_bit_buffer *const rb) {
2281 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002282#if CONFIG_EXT_TILE
2283// Read the tile width/height
2284#if CONFIG_EXT_PARTITION
2285 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002286 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
2287 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002288 } else
2289#endif // CONFIG_EXT_PARTITION
2290 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002291 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
2292 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002293 }
2294
2295 cm->tile_width <<= cm->mib_size_log2;
2296 cm->tile_height <<= cm->mib_size_log2;
2297
Yaowu Xuf883b422016-08-30 14:01:10 -07002298 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
2299 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002300
2301 // Get the number of tiles
2302 cm->tile_cols = 1;
2303 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
2304
2305 cm->tile_rows = 1;
2306 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
2307
2308 if (cm->tile_cols * cm->tile_rows > 1) {
2309 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07002310 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2311 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002312 }
2313#else
2314 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07002315 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002316
2317 // columns
2318 max_ones = max_log2_tile_cols - min_log2_tile_cols;
2319 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07002320 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002321
2322 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07002323 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002324 "Invalid number of tile columns");
2325
2326 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07002327 cm->log2_tile_rows = aom_rb_read_bit(rb);
2328 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002329
2330 cm->tile_cols = 1 << cm->log2_tile_cols;
2331 cm->tile_rows = 1 << cm->log2_tile_rows;
2332
2333 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
2334 cm->tile_width >>= cm->log2_tile_cols;
2335 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
2336 cm->tile_height >>= cm->log2_tile_rows;
2337
2338 // round to integer multiples of superblock size
2339 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
2340 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
2341
2342 // tile size magnitude
2343 if (cm->tile_rows > 1 || cm->tile_cols > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002344 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002345 }
2346#endif // CONFIG_EXT_TILE
2347}
2348
2349static int mem_get_varsize(const uint8_t *src, const int sz) {
2350 switch (sz) {
2351 case 1: return src[0];
2352 case 2: return mem_get_le16(src);
2353 case 3: return mem_get_le24(src);
2354 case 4: return mem_get_le32(src);
2355 default: assert("Invalid size" && 0); return -1;
2356 }
2357}
2358
2359#if CONFIG_EXT_TILE
2360// Reads the next tile returning its size and adjusting '*data' accordingly
2361// based on 'is_last'.
2362static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07002363 struct aom_internal_error_info *error_info,
2364 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002365 void *decrypt_state,
2366 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
2367 int tile_size_bytes, int col, int row) {
2368 size_t size;
2369
2370 size_t copy_size = 0;
2371 const uint8_t *copy_data = NULL;
2372
2373 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002374 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002375 "Truncated packet or corrupt tile length");
2376 if (decrypt_cb) {
2377 uint8_t be_data[4];
2378 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
2379
2380 // Only read number of bytes in cm->tile_size_bytes.
2381 size = mem_get_varsize(be_data, tile_size_bytes);
2382 } else {
2383 size = mem_get_varsize(*data, tile_size_bytes);
2384 }
2385
2386 // The top bit indicates copy mode
2387 if ((size >> (tile_size_bytes * 8 - 1)) == 1) {
2388 // The remaining bits in the top byte signal the row offset
2389 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
2390
2391 // Currently, only use tiles in same column as reference tiles.
2392 copy_data = tile_buffers[row - offset][col].data;
2393 copy_size = tile_buffers[row - offset][col].size;
2394 size = 0;
2395 }
2396
2397 *data += tile_size_bytes;
2398
2399 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07002400 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002401 "Truncated packet or corrupt tile size");
2402
2403 if (size > 0) {
2404 tile_buffers[row][col].data = *data;
2405 tile_buffers[row][col].size = size;
2406 } else {
2407 tile_buffers[row][col].data = copy_data;
2408 tile_buffers[row][col].size = copy_size;
2409 }
2410
2411 *data += size;
2412
2413 tile_buffers[row][col].raw_data_end = *data;
2414}
2415
2416static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07002417 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002418 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002419 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002420 const int tile_cols = cm->tile_cols;
2421 const int tile_rows = cm->tile_rows;
2422 const int have_tiles = tile_cols * tile_rows > 1;
2423
2424 if (!have_tiles) {
2425 const uint32_t tile_size = data_end - data;
2426 tile_buffers[0][0].data = data;
2427 tile_buffers[0][0].size = tile_size;
2428 tile_buffers[0][0].raw_data_end = NULL;
2429 } else {
2430 // We locate only the tile buffers that are required, which are the ones
2431 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
2432 // need the last (bottom right) tile buffer, as we need to know where the
2433 // end of the compressed frame buffer is for proper superframe decoding.
2434
2435 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
2436 const uint8_t *const data_start = data;
2437
Yaowu Xuf883b422016-08-30 14:01:10 -07002438 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002439 const int single_row = pbi->dec_tile_row >= 0;
2440 const int tile_rows_start = single_row ? dec_tile_row : 0;
2441 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07002442 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002443 const int single_col = pbi->dec_tile_col >= 0;
2444 const int tile_cols_start = single_col ? dec_tile_col : 0;
2445 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
2446
2447 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
2448 const int tile_size_bytes = pbi->tile_size_bytes;
2449
2450 size_t tile_col_size;
2451 int r, c;
2452
2453 // Read tile column sizes for all columns (we need the last tile buffer)
2454 for (c = 0; c < tile_cols; ++c) {
2455 const int is_last = c == tile_cols - 1;
2456 if (!is_last) {
2457 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
2458 data += tile_col_size_bytes;
2459 tile_col_data_end[c] = data + tile_col_size;
2460 } else {
2461 tile_col_size = data_end - data;
2462 tile_col_data_end[c] = data_end;
2463 }
2464 data += tile_col_size;
2465 }
2466
2467 data = data_start;
2468
2469 // Read the required tile sizes.
2470 for (c = tile_cols_start; c < tile_cols_end; ++c) {
2471 const int is_last = c == tile_cols - 1;
2472
2473 if (c > 0) data = tile_col_data_end[c - 1];
2474
2475 if (!is_last) data += tile_col_size_bytes;
2476
2477 // Get the whole of the last column, otherwise stop at the required tile.
2478 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
2479 tile_buffers[r][c].col = c;
2480
2481 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
2482 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
2483 tile_size_bytes, c, r);
2484 }
2485 }
2486
2487 // If we have not read the last column, then read it to get the last tile.
2488 if (tile_cols_end != tile_cols) {
2489 c = tile_cols - 1;
2490
2491 data = tile_col_data_end[c - 1];
2492
2493 for (r = 0; r < tile_rows; ++r) {
2494 tile_buffers[r][c].col = c;
2495
2496 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
2497 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
2498 tile_size_bytes, c, r);
2499 }
2500 }
2501 }
2502}
2503#else
2504// Reads the next tile returning its size and adjusting '*data' accordingly
2505// based on 'is_last'.
2506static void get_tile_buffer(const uint8_t *const data_end,
2507 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07002508 struct aom_internal_error_info *error_info,
2509 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002510 void *decrypt_state, TileBufferDec *const buf) {
2511 size_t size;
2512
2513 if (!is_last) {
2514 if (!read_is_valid(*data, 4, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002515 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002516 "Truncated packet or corrupt tile length");
2517
2518 if (decrypt_cb) {
2519 uint8_t be_data[4];
2520 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
2521 size = mem_get_varsize(be_data, tile_size_bytes);
2522 } else {
2523 size = mem_get_varsize(*data, tile_size_bytes);
2524 }
2525 *data += tile_size_bytes;
2526
2527 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07002528 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002529 "Truncated packet or corrupt tile size");
2530 } else {
2531 size = data_end - *data;
2532 }
2533
2534 buf->data = *data;
2535 buf->size = size;
2536
2537 *data += size;
2538}
2539
2540static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07002541 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002542 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002543 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002544 int r, c;
2545 const int tile_cols = cm->tile_cols;
2546 const int tile_rows = cm->tile_rows;
2547
2548 for (r = 0; r < tile_rows; ++r) {
2549 for (c = 0; c < tile_cols; ++c) {
2550 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
2551 TileBufferDec *const buf = &tile_buffers[r][c];
2552 buf->col = c;
2553 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
2554 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
2555 }
2556 }
2557}
2558#endif // CONFIG_EXT_TILE
2559
Yaowu Xuf883b422016-08-30 14:01:10 -07002560static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002561 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002562 AV1_COMMON *const cm = &pbi->common;
2563 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002564 const int tile_cols = cm->tile_cols;
2565 const int tile_rows = cm->tile_rows;
2566 const int n_tiles = tile_cols * tile_rows;
2567 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
2568#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07002569 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002570 const int single_row = pbi->dec_tile_row >= 0;
2571 const int tile_rows_start = single_row ? dec_tile_row : 0;
2572 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07002573 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002574 const int single_col = pbi->dec_tile_col >= 0;
2575 const int tile_cols_start = single_col ? dec_tile_col : 0;
2576 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
2577 const int inv_col_order = pbi->inv_tile_order && !single_col;
2578 const int inv_row_order = pbi->inv_tile_order && !single_row;
2579#else
2580 const int tile_rows_start = 0;
2581 const int tile_rows_end = tile_rows;
2582 const int tile_cols_start = 0;
2583 const int tile_cols_end = tile_cols;
2584 const int inv_col_order = pbi->inv_tile_order;
2585 const int inv_row_order = pbi->inv_tile_order;
2586#endif // CONFIG_EXT_TILE
2587 int tile_row, tile_col;
2588
2589#if CONFIG_ENTROPY
2590 cm->do_subframe_update = n_tiles == 1;
2591#endif // CONFIG_ENTROPY
2592
2593 if (cm->lf.filter_level && !cm->skip_loop_filter &&
2594 pbi->lf_worker.data1 == NULL) {
2595 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07002596 aom_memalign(32, sizeof(LFWorkerData)));
2597 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002598 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002599 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002600 "Loop filter thread creation failed");
2601 }
2602 }
2603
2604 if (cm->lf.filter_level && !cm->skip_loop_filter) {
2605 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
2606 // Be sure to sync as we might be resuming after a failed frame decode.
2607 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07002608 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
2609 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002610 }
2611
2612 assert(tile_rows <= MAX_TILE_ROWS);
2613 assert(tile_cols <= MAX_TILE_COLS);
2614
2615 get_tile_buffers(pbi, data, data_end, tile_buffers);
2616
2617 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002618 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002619 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07002620 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002621 pbi->allocated_tiles = n_tiles;
2622 }
2623
2624 // Load all tile information into tile_data.
2625 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
2626 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
2627 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
2628 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
2629
2630 td->cm = cm;
2631 td->xd = pbi->mb;
2632 td->xd.corrupted = 0;
2633 td->xd.counts =
2634 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
2635 ? &cm->counts
2636 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07002637 av1_zero(td->dqcoeff);
2638 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002639#if !CONFIG_ANS
2640 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
2641 &td->bit_reader, pbi->decrypt_cb, pbi->decrypt_state);
2642#else
2643 setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
2644 &td->bit_reader, pbi->decrypt_cb, pbi->decrypt_state);
2645#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002646 av1_init_macroblockd(cm, &td->xd, td->dqcoeff);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002647 td->xd.plane[0].color_index_map = td->color_index_map[0];
2648 td->xd.plane[1].color_index_map = td->color_index_map[1];
2649 }
2650 }
2651
2652 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
2653 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
2654 int mi_row = 0;
2655 TileInfo tile_info;
2656
Yaowu Xuf883b422016-08-30 14:01:10 -07002657 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002658
2659 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
2660 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
2661 TileData *const td = pbi->tile_data + tile_cols * row + col;
2662
Yaowu Xuf883b422016-08-30 14:01:10 -07002663 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002664
Yaowu Xuf883b422016-08-30 14:01:10 -07002665 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002666
2667 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
2668 mi_row += cm->mib_size) {
2669 int mi_col;
2670
Yaowu Xuf883b422016-08-30 14:01:10 -07002671 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002672
2673 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
2674 mi_col += cm->mib_size) {
2675 decode_partition(pbi, &td->xd,
2676#if CONFIG_SUPERTX
2677 0,
2678#endif // CONFIG_SUPERTX
2679 mi_row, mi_col, &td->bit_reader, cm->sb_size,
2680 b_width_log2_lookup[cm->sb_size]);
2681 }
2682 pbi->mb.corrupted |= td->xd.corrupted;
2683 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07002684 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002685 "Failed to decode tile data");
2686#if CONFIG_ENTROPY
2687 if (cm->do_subframe_update &&
2688 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
2689 if ((mi_row + MI_SIZE) %
2690 (MI_SIZE *
Yaowu Xuf883b422016-08-30 14:01:10 -07002691 AOMMAX(cm->mi_rows / MI_SIZE / COEF_PROBS_BUFS, 1)) ==
Yaowu Xuc27fc142016-08-22 16:08:15 -07002692 0 &&
2693 mi_row + MI_SIZE < cm->mi_rows &&
2694 cm->coef_probs_update_idx < COEF_PROBS_BUFS - 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002695 av1_partial_adapt_probs(cm, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002696 ++cm->coef_probs_update_idx;
2697 }
2698 }
2699#endif // CONFIG_ENTROPY
2700 }
2701 }
2702
2703 assert(mi_row > 0);
2704
2705#if !CONFIG_VAR_TX
2706 // Loopfilter one tile row.
2707 if (cm->lf.filter_level && !cm->skip_loop_filter) {
2708 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07002709 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002710 const int lf_end = tile_info.mi_row_end - cm->mib_size;
2711
2712 // Delay the loopfilter if the first tile row is only
2713 // a single superblock high.
2714 if (lf_end <= 0) continue;
2715
2716 // Decoding has completed. Finish up the loop filter in this thread.
2717 if (tile_info.mi_row_end >= cm->mi_rows) continue;
2718
2719 winterface->sync(&pbi->lf_worker);
2720 lf_data->start = lf_start;
2721 lf_data->stop = lf_end;
2722 if (pbi->max_threads > 1) {
2723 winterface->launch(&pbi->lf_worker);
2724 } else {
2725 winterface->execute(&pbi->lf_worker);
2726 }
2727 }
2728
2729 // After loopfiltering, the last 7 row pixels in each superblock row may
2730 // still be changed by the longest loopfilter of the next superblock row.
2731 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07002732 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002733#endif // !CONFIG_VAR_TX
2734 }
2735
2736#if CONFIG_VAR_TX
2737 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07002738 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
2739 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002740#else
2741 // Loopfilter remaining rows in the frame.
2742 if (cm->lf.filter_level && !cm->skip_loop_filter) {
2743 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
2744 winterface->sync(&pbi->lf_worker);
2745 lf_data->start = lf_data->stop;
2746 lf_data->stop = cm->mi_rows;
2747 winterface->execute(&pbi->lf_worker);
2748 }
2749#endif // CONFIG_VAR_TX
2750#if CONFIG_CLPF
2751 if (cm->clpf && !cm->skip_loop_filter)
Yaowu Xuf883b422016-08-30 14:01:10 -07002752 av1_clpf_frame(&pbi->cur_buf->buf, cm, &pbi->mb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002753#endif
2754#if CONFIG_DERING
2755 if (cm->dering_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002756 av1_dering_frame(&pbi->cur_buf->buf, cm, &pbi->mb, cm->dering_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002757 }
2758#endif // CONFIG_DERING
2759
2760 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07002761 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002762
2763#if CONFIG_EXT_TILE
2764 if (n_tiles == 1) {
2765#if CONFIG_ANS
2766 return data_end;
2767#else
2768 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07002769 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002770#endif // CONFIG_ANS
2771 } else {
2772 // Return the end of the last tile buffer
2773 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
2774 }
2775#else
2776#if CONFIG_ANS
2777 return data_end;
2778#else
2779 {
2780 // Get last tile data.
2781 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07002782 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002783 }
2784#endif // CONFIG_ANS
2785#endif // CONFIG_EXT_TILE
2786}
2787
2788static int tile_worker_hook(TileWorkerData *const tile_data,
2789 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002790 AV1Decoder *const pbi = tile_data->pbi;
2791 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002792 int mi_row, mi_col;
2793
2794 if (setjmp(tile_data->error_info.jmp)) {
2795 tile_data->error_info.setjmp = 0;
2796 tile_data->xd.corrupted = 1;
2797 return 0;
2798 }
2799
2800 tile_data->error_info.setjmp = 1;
2801 tile_data->xd.error_info = &tile_data->error_info;
2802
Yaowu Xuf883b422016-08-30 14:01:10 -07002803 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002804
2805 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
2806 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002807 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002808
2809 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
2810 mi_col += cm->mib_size) {
2811 decode_partition(pbi, &tile_data->xd,
2812#if CONFIG_SUPERTX
2813 0,
2814#endif
2815 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
2816 b_width_log2_lookup[cm->sb_size]);
2817 }
2818 }
2819 return !tile_data->xd.corrupted;
2820}
2821
2822// sorts in descending order
2823static int compare_tile_buffers(const void *a, const void *b) {
2824 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
2825 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
2826 return (int)(buf2->size - buf1->size);
2827}
2828
Yaowu Xuf883b422016-08-30 14:01:10 -07002829static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002830 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002831 AV1_COMMON *const cm = &pbi->common;
2832 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002833 const int tile_cols = cm->tile_cols;
2834 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07002835 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002836 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
2837#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07002838 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002839 const int single_row = pbi->dec_tile_row >= 0;
2840 const int tile_rows_start = single_row ? dec_tile_row : 0;
2841 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07002842 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002843 const int single_col = pbi->dec_tile_col >= 0;
2844 const int tile_cols_start = single_col ? dec_tile_col : 0;
2845 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
2846#else
2847 const int tile_rows_start = 0;
2848 const int tile_rows_end = tile_rows;
2849 const int tile_cols_start = 0;
2850 const int tile_cols_end = tile_cols;
2851#endif // CONFIG_EXT_TILE
2852 int tile_row, tile_col;
2853 int i;
2854
2855#if !(CONFIG_ANS || CONFIG_EXT_TILE)
2856 int final_worker = -1;
2857#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
2858
2859 assert(tile_rows <= MAX_TILE_ROWS);
2860 assert(tile_cols <= MAX_TILE_COLS);
2861
2862 assert(tile_cols * tile_rows > 1);
2863
2864#if CONFIG_ANS
2865 // TODO(any): This might just work now. Needs to be tested.
2866 abort(); // FIXME: Tile parsing broken
2867#endif // CONFIG_ANS
2868
2869 // TODO(jzern): See if we can remove the restriction of passing in max
2870 // threads to the decoder.
2871 if (pbi->num_tile_workers == 0) {
2872 const int num_threads = pbi->max_threads & ~1;
2873 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07002874 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002875 // Ensure tile data offsets will be properly aligned. This may fail on
2876 // platforms without DECLARE_ALIGNED().
2877 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
2878 CHECK_MEM_ERROR(
2879 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07002880 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002881 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07002882 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002883 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002884 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002885 ++pbi->num_tile_workers;
2886
2887 winterface->init(worker);
2888 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002889 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002890 "Tile decoder thread creation failed");
2891 }
2892 }
2893 }
2894
2895 // Reset tile decoding hook
2896 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002897 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002898 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07002899 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002900 worker->data1 = &pbi->tile_worker_data[i];
2901 worker->data2 = &pbi->tile_worker_info[i];
2902 }
2903
2904 // Initialize thread frame counts.
2905 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
2906 for (i = 0; i < num_workers; ++i) {
2907 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07002908 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002909 }
2910 }
2911
2912 // Load tile data into tile_buffers
2913 get_tile_buffers(pbi, data, data_end, tile_buffers);
2914
2915 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
2916 // Sort the buffers in this tile row based on size in descending order.
2917 qsort(&tile_buffers[tile_row][tile_cols_start],
2918 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
2919 compare_tile_buffers);
2920
2921 // Rearrange the tile buffers in this tile row such that per-tile group
2922 // the largest, and presumably the most difficult tile will be decoded in
2923 // the main thread. This should help minimize the number of instances
2924 // where the main thread is waiting for a worker to complete.
2925 {
2926 int group_start;
2927 for (group_start = tile_cols_start; group_start < tile_cols_end;
2928 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002929 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002930 const TileBufferDec largest = tile_buffers[tile_row][group_start];
2931 memmove(&tile_buffers[tile_row][group_start],
2932 &tile_buffers[tile_row][group_start + 1],
2933 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
2934 tile_buffers[tile_row][group_end - 1] = largest;
2935 }
2936 }
2937
2938 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
2939 // Launch workers for individual columns
2940 for (i = 0; i < num_workers && tile_col < tile_cols_end;
2941 ++i, ++tile_col) {
2942 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07002943 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002944 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
2945 TileInfo *const tile_info = (TileInfo *)worker->data2;
2946
2947 twd->pbi = pbi;
2948 twd->xd = pbi->mb;
2949 twd->xd.corrupted = 0;
2950 twd->xd.counts =
2951 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
2952 ? &twd->counts
2953 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07002954 av1_zero(twd->dqcoeff);
2955 av1_tile_init(tile_info, cm, tile_row, buf->col);
2956 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002957#if !CONFIG_ANS
2958 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
2959 &twd->bit_reader, pbi->decrypt_cb,
2960 pbi->decrypt_state);
2961#else
2962 setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
2963 &twd->bit_reader, pbi->decrypt_cb,
2964 pbi->decrypt_state);
2965#endif // CONFIG_ANS
Yaowu Xuf883b422016-08-30 14:01:10 -07002966 av1_init_macroblockd(cm, &twd->xd, twd->dqcoeff);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002967 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
2968 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
2969
2970 worker->had_error = 0;
2971 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
2972 winterface->execute(worker);
2973 } else {
2974 winterface->launch(worker);
2975 }
2976
2977#if !(CONFIG_ANS || CONFIG_EXT_TILE)
2978 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
2979 final_worker = i;
2980 }
2981#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
2982 }
2983
2984 // Sync all workers
2985 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002986 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002987 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07002988 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07002989 // info in cm. Additionally once the threads have been synced and an
2990 // error is detected, there's no point in continuing to decode tiles.
2991 pbi->mb.corrupted |= !winterface->sync(worker);
2992 }
2993 }
2994 }
2995
2996 // Accumulate thread frame counts.
2997 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
2998 for (i = 0; i < num_workers; ++i) {
2999 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003000 av1_accumulate_frame_counts(cm, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003001 }
3002 }
3003
3004#if CONFIG_EXT_TILE
3005 // Return the end of the last tile buffer
3006 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3007#else
3008#if CONFIG_ANS
3009 return data_end;
3010#else
3011 assert(final_worker != -1);
3012 {
3013 TileWorkerData *const twd =
3014 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003015 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003016 }
3017#endif // CONFIG_ANS
3018#endif // CONFIG_EXT_TILE
3019}
3020
3021static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003022 AV1_COMMON *const cm = (AV1_COMMON *)data;
3023 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003024}
3025
Yaowu Xuf883b422016-08-30 14:01:10 -07003026static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3027 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003028 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003029 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
3030#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003031 cm->use_highbitdepth = 1;
3032#endif
3033 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003034 cm->bit_depth = AOM_BITS_8;
3035#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003036 cm->use_highbitdepth = 0;
3037#endif
3038 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003039 cm->color_space = aom_rb_read_literal(rb, 3);
3040 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003041 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07003042 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003043 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003044 cm->subsampling_x = aom_rb_read_bit(rb);
3045 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003046 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003047 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003048 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07003049 if (aom_rb_read_bit(rb))
3050 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003051 "Reserved bit set");
3052 } else {
3053 cm->subsampling_y = cm->subsampling_x = 1;
3054 }
3055 } else {
3056 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
3057 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
3058 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
3059 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003060 if (aom_rb_read_bit(rb))
3061 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003062 "Reserved bit set");
3063 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003064 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003065 "4:4:4 color not supported in profile 0 or 2");
3066 }
3067 }
3068}
3069
Yaowu Xuf883b422016-08-30 14:01:10 -07003070static size_t read_uncompressed_header(AV1Decoder *pbi,
3071 struct aom_read_bit_buffer *rb) {
3072 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003073 MACROBLOCKD *const xd = &pbi->mb;
3074 BufferPool *const pool = cm->buffer_pool;
3075 RefCntBuffer *const frame_bufs = pool->frame_bufs;
3076 int i, mask, ref_index = 0;
3077 size_t sz;
3078#if CONFIG_EXT_REFS
3079 cm->last3_frame_type = cm->last2_frame_type;
3080 cm->last2_frame_type = cm->last_frame_type;
3081#endif // CONFIG_EXT_REFS
3082 cm->last_frame_type = cm->frame_type;
3083 cm->last_intra_only = cm->intra_only;
3084
3085#if CONFIG_EXT_REFS
3086 // NOTE: By default all coded frames to be used as a reference
3087 cm->is_reference_frame = 1;
3088#endif // CONFIG_EXT_REFS
3089
Yaowu Xuf883b422016-08-30 14:01:10 -07003090 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
3091 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003092 "Invalid frame marker");
3093
Yaowu Xuf883b422016-08-30 14:01:10 -07003094 cm->profile = av1_read_profile(rb);
3095#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003096 if (cm->profile >= MAX_PROFILES)
Yaowu Xuf883b422016-08-30 14:01:10 -07003097 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003098 "Unsupported bitstream profile");
3099#else
3100 if (cm->profile >= PROFILE_2)
Yaowu Xuf883b422016-08-30 14:01:10 -07003101 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003102 "Unsupported bitstream profile");
3103#endif
3104
Yaowu Xuf883b422016-08-30 14:01:10 -07003105 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003106
3107 if (cm->show_existing_frame) {
3108 // Show an existing frame directly.
Yaowu Xuf883b422016-08-30 14:01:10 -07003109 const int frame_to_show = cm->ref_frame_map[aom_rb_read_literal(rb, 3)];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003110
3111 lock_buffer_pool(pool);
3112 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
3113 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07003114 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003115 "Buffer %d does not contain a decoded frame",
3116 frame_to_show);
3117 }
3118 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
3119 unlock_buffer_pool(pool);
3120
3121 cm->lf.filter_level = 0;
3122 cm->show_frame = 1;
3123 pbi->refresh_frame_flags = 0;
3124
3125 if (cm->frame_parallel_decode) {
3126 for (i = 0; i < REF_FRAMES; ++i)
3127 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
3128 }
3129
3130 return 0;
3131 }
3132
Yaowu Xuf883b422016-08-30 14:01:10 -07003133 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
3134 cm->show_frame = aom_rb_read_bit(rb);
3135 cm->error_resilient_mode = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003136
3137 if (cm->frame_type == KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003138 if (!av1_read_sync_code(rb))
3139 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003140 "Invalid frame sync code");
3141
3142 read_bitdepth_colorspace_sampling(cm, rb);
3143 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
3144
3145 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3146 cm->frame_refs[i].idx = INVALID_IDX;
3147 cm->frame_refs[i].buf = NULL;
3148 }
3149
3150 setup_frame_size(cm, rb);
3151 if (pbi->need_resync) {
3152 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
3153 pbi->need_resync = 0;
3154 }
3155 if (frame_is_intra_only(cm))
Yaowu Xuf883b422016-08-30 14:01:10 -07003156 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003157 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003158 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003159
3160 if (cm->error_resilient_mode) {
3161 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
3162 } else {
3163 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003164 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003165 ? RESET_FRAME_CONTEXT_ALL
3166 : RESET_FRAME_CONTEXT_CURRENT;
3167 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003168 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003169 ? RESET_FRAME_CONTEXT_CURRENT
3170 : RESET_FRAME_CONTEXT_NONE;
3171 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07003172 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003173 ? RESET_FRAME_CONTEXT_ALL
3174 : RESET_FRAME_CONTEXT_CURRENT;
3175 }
3176 }
3177
3178 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003179 if (!av1_read_sync_code(rb))
3180 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003181 "Invalid frame sync code");
3182
3183 read_bitdepth_colorspace_sampling(cm, rb);
3184
Yaowu Xuf883b422016-08-30 14:01:10 -07003185 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003186 setup_frame_size(cm, rb);
3187 if (pbi->need_resync) {
3188 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
3189 pbi->need_resync = 0;
3190 }
3191 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Yaowu Xuf883b422016-08-30 14:01:10 -07003192 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003193
3194#if CONFIG_EXT_REFS
3195 if (!pbi->refresh_frame_flags) {
3196 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
3197 // will not be used as a reference
3198 cm->is_reference_frame = 0;
3199 }
3200#endif // CONFIG_EXT_REFS
3201
3202 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003203 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003204 const int idx = cm->ref_frame_map[ref];
3205 RefBuffer *const ref_frame = &cm->frame_refs[i];
3206 ref_frame->idx = idx;
3207 ref_frame->buf = &frame_bufs[idx].buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07003208 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003209 }
3210
3211 setup_frame_size_with_refs(cm, rb);
3212
Yaowu Xuf883b422016-08-30 14:01:10 -07003213 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003214 cm->interp_filter = read_interp_filter(rb);
3215
3216 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3217 RefBuffer *const ref_buf = &cm->frame_refs[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07003218#if CONFIG_AOM_HIGHBITDEPTH
3219 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003220 &ref_buf->sf, ref_buf->buf->y_crop_width,
3221 ref_buf->buf->y_crop_height, cm->width, cm->height,
3222 cm->use_highbitdepth);
3223#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003224 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003225 &ref_buf->sf, ref_buf->buf->y_crop_width,
3226 ref_buf->buf->y_crop_height, cm->width, cm->height);
3227#endif
3228 }
3229 }
3230 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003231#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003232 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
3233#endif
3234 get_frame_new_buffer(cm)->color_space = cm->color_space;
3235 get_frame_new_buffer(cm)->color_range = cm->color_range;
3236 get_frame_new_buffer(cm)->render_width = cm->render_width;
3237 get_frame_new_buffer(cm)->render_height = cm->render_height;
3238
3239 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003240 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003241 "Keyframe / intra-only frame required to reset decoder"
3242 " state");
3243 }
3244
3245 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003246 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003247 ? REFRESH_FRAME_CONTEXT_FORWARD
3248 : REFRESH_FRAME_CONTEXT_BACKWARD;
3249 } else {
3250 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
3251 }
3252
Yaowu Xuf883b422016-08-30 14:01:10 -07003253 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07003254 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07003255 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003256
3257 // Generate next_ref_frame_map.
3258 lock_buffer_pool(pool);
3259 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
3260 if (mask & 1) {
3261 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
3262 ++frame_bufs[cm->new_fb_idx].ref_count;
3263 } else {
3264 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
3265 }
3266 // Current thread holds the reference frame.
3267 if (cm->ref_frame_map[ref_index] >= 0)
3268 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
3269 ++ref_index;
3270 }
3271
3272 for (; ref_index < REF_FRAMES; ++ref_index) {
3273 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
3274
3275 // Current thread holds the reference frame.
3276 if (cm->ref_frame_map[ref_index] >= 0)
3277 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
3278 }
3279 unlock_buffer_pool(pool);
3280 pbi->hold_ref_buf = 1;
3281
3282 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003283 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003284
3285#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07003286 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003287#else
3288 set_sb_size(cm, BLOCK_64X64);
3289#endif // CONFIG_EXT_PARTITION
3290
3291 setup_loopfilter(cm, rb);
3292#if CONFIG_CLPF
3293 setup_clpf(cm, rb);
3294#endif
3295#if CONFIG_DERING
3296 setup_dering(cm, rb);
3297#endif
3298#if CONFIG_LOOP_RESTORATION
3299 setup_restoration(cm, rb);
3300#endif // CONFIG_LOOP_RESTORATION
3301 setup_quantization(cm, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07003302#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003303 xd->bd = (int)cm->bit_depth;
3304#endif
3305
3306#if CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07003307 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003308 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
3309 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
3310 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
3311 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
3312 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
3313 }
3314#endif // CONFIG_ENTROPY
3315
3316 setup_segmentation(cm, rb);
3317
3318 {
3319 int i;
3320 for (i = 0; i < MAX_SEGMENTS; ++i) {
3321 const int qindex = cm->seg.enabled
Yaowu Xuf883b422016-08-30 14:01:10 -07003322 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003323 : cm->base_qindex;
3324 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
3325 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
3326 }
3327 }
3328
3329 setup_segmentation_dequant(cm);
3330 cm->tx_mode =
3331 (!cm->seg.enabled && xd->lossless[0]) ? ONLY_4X4 : read_tx_mode(rb);
3332 cm->reference_mode = read_frame_reference_mode(cm, rb);
3333
3334 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07003335 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003336
3337 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07003338 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003339 "Invalid header size");
3340
3341 return sz;
3342}
3343
3344#if CONFIG_EXT_TX
Yaowu Xuf883b422016-08-30 14:01:10 -07003345static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003346 int i, j, k;
3347 int s;
3348 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003349 if (aom_read(r, GROUP_DIFF_UPDATE_PROB)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003350 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
3351 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
3352 for (j = 0; j < num_ext_tx_set_inter[s] - 1; ++j)
Yaowu Xuf883b422016-08-30 14:01:10 -07003353 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003354 }
3355 }
3356 }
3357
3358 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003359 if (aom_read(r, GROUP_DIFF_UPDATE_PROB)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003360 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
3361 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
3362 for (j = 0; j < INTRA_MODES; ++j)
3363 for (k = 0; k < num_ext_tx_set_intra[s] - 1; ++k)
Yaowu Xuf883b422016-08-30 14:01:10 -07003364 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003365 }
3366 }
3367 }
3368}
3369
3370#else
3371
Yaowu Xuf883b422016-08-30 14:01:10 -07003372static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003373 int i, j, k;
Yaowu Xuf883b422016-08-30 14:01:10 -07003374 if (aom_read(r, GROUP_DIFF_UPDATE_PROB)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003375 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
3376 for (j = 0; j < TX_TYPES; ++j)
3377 for (k = 0; k < TX_TYPES - 1; ++k)
Yaowu Xuf883b422016-08-30 14:01:10 -07003378 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[i][j][k]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003379 }
3380 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003381 if (aom_read(r, GROUP_DIFF_UPDATE_PROB)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003382 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
3383 for (k = 0; k < TX_TYPES - 1; ++k)
Yaowu Xuf883b422016-08-30 14:01:10 -07003384 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[i][k]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003385 }
3386 }
3387}
3388#endif // CONFIG_EXT_TX
3389
3390#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07003391static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003392 int i, j;
Yaowu Xuf883b422016-08-30 14:01:10 -07003393 if (aom_read(r, GROUP_DIFF_UPDATE_PROB)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003394 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
3395 for (j = 1; j < TX_SIZES; ++j) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003396 av1_diff_update_prob(r, &fc->supertx_prob[i][j]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003397 }
3398 }
3399 }
3400}
3401#endif // CONFIG_SUPERTX
3402
3403#if CONFIG_GLOBAL_MOTION
3404static void read_global_motion_params(Global_Motion_Params *params,
Yaowu Xuf883b422016-08-30 14:01:10 -07003405 aom_prob *probs, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003406 GLOBAL_MOTION_TYPE gmtype =
Yaowu Xuf883b422016-08-30 14:01:10 -07003407 aom_read_tree(r, av1_global_motion_types_tree, probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003408 params->gmtype = gmtype;
3409 params->motion_params.wmtype = gm_to_trans_type(gmtype);
3410 switch (gmtype) {
3411 case GLOBAL_ZERO: break;
3412 case GLOBAL_AFFINE:
Sarah Parkere5299862016-08-16 14:57:37 -07003413 params->motion_params.wmmat[2].as_mv.row =
Yaowu Xuf883b422016-08-30 14:01:10 -07003414 aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Yaowu Xuc27fc142016-08-22 16:08:15 -07003415 GM_ALPHA_DECODE_FACTOR +
3416 (1 << WARPEDMODEL_PREC_BITS);
Sarah Parkere5299862016-08-16 14:57:37 -07003417 params->motion_params.wmmat[2].as_mv.col =
3418 (aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
3419 GM_ALPHA_DECODE_FACTOR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003420 // fallthrough intended
3421 case GLOBAL_ROTZOOM:
Sarah Parkere5299862016-08-16 14:57:37 -07003422 params->motion_params.wmmat[1].as_mv.row =
3423 aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
3424 GM_ALPHA_DECODE_FACTOR;
3425 params->motion_params.wmmat[1].as_mv.col =
Yaowu Xuf883b422016-08-30 14:01:10 -07003426 (aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Yaowu Xuc27fc142016-08-22 16:08:15 -07003427 GM_ALPHA_DECODE_FACTOR) +
3428 (1 << WARPEDMODEL_PREC_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003429 // fallthrough intended
3430 case GLOBAL_TRANSLATION:
Sarah Parkere5299862016-08-16 14:57:37 -07003431 params->motion_params.wmmat[0].as_mv.row =
Yaowu Xuf883b422016-08-30 14:01:10 -07003432 aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
Yaowu Xuc27fc142016-08-22 16:08:15 -07003433 GM_TRANS_DECODE_FACTOR;
Sarah Parkere5299862016-08-16 14:57:37 -07003434 params->motion_params.wmmat[0].as_mv.col =
Yaowu Xuf883b422016-08-30 14:01:10 -07003435 aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
Yaowu Xuc27fc142016-08-22 16:08:15 -07003436 GM_TRANS_DECODE_FACTOR;
3437 break;
3438 default: assert(0);
3439 }
3440}
3441
Yaowu Xuf883b422016-08-30 14:01:10 -07003442static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003443 int frame;
3444 memset(cm->global_motion, 0, sizeof(cm->global_motion));
3445 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
3446 read_global_motion_params(&cm->global_motion[frame],
3447 cm->fc->global_motion_types_prob, r);
Sarah Parkere5299862016-08-16 14:57:37 -07003448 /*
3449 printf("Dec Ref %d [%d]: %d %d %d %d\n",
3450 frame, cm->current_video_frame,
3451 cm->global_motion[frame].motion_params.wmmat[0].as_mv.row,
3452 cm->global_motion[frame].motion_params.wmmat[0].as_mv.col,
3453 cm->global_motion[frame].motion_params.wmmat[1].as_mv.row,
3454 cm->global_motion[frame].motion_params.wmmat[1].as_mv.col);
3455 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07003456 }
3457}
3458#endif // CONFIG_GLOBAL_MOTION
3459
Yaowu Xuf883b422016-08-30 14:01:10 -07003460static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003461 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003462 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003463#if CONFIG_SUPERTX
3464 MACROBLOCKD *const xd = &pbi->mb;
3465#endif
3466 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003467 aom_reader r;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003468 int k, i, j;
3469
3470#if !CONFIG_ANS
Yaowu Xuf883b422016-08-30 14:01:10 -07003471 if (aom_reader_init(&r, data, partition_size, pbi->decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003472 pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07003473 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003474 "Failed to allocate bool decoder 0");
3475#else
Yaowu Xuf7ae12d2016-09-01 08:59:46 -07003476 if (ans_read_init(&r, data, (int)partition_size))
Yaowu Xuf883b422016-08-30 14:01:10 -07003477 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003478 "Failed to allocate compressed header ANS decoder");
3479#endif // !CONFIG_ANS
3480
3481 if (cm->tx_mode == TX_MODE_SELECT) {
3482 for (i = 0; i < TX_SIZES - 1; ++i)
3483 for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
3484 for (k = 0; k < i + 1; ++k)
Yaowu Xuf883b422016-08-30 14:01:10 -07003485 av1_diff_update_prob(&r, &fc->tx_size_probs[i][j][k]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003486 }
3487
3488 read_coef_probs(fc, cm->tx_mode, &r);
3489
3490#if CONFIG_VAR_TX
3491 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Yaowu Xuf883b422016-08-30 14:01:10 -07003492 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k]);
Yue Chena1e48dc2016-08-29 17:29:33 -07003493#if CONFIG_EXT_TX && CONFIG_RECT_TX
3494 if (cm->tx_mode == TX_MODE_SELECT) {
3495 for (i = 1; i < TX_SIZES - 1; ++i)
3496 av1_diff_update_prob(&r, &fc->rect_tx_prob[i]);
3497 }
3498#endif // CONFIG_EXT_TX && CONFIG_RECT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003499#endif
3500
3501 for (k = 0; k < SKIP_CONTEXTS; ++k)
Yaowu Xuf883b422016-08-30 14:01:10 -07003502 av1_diff_update_prob(&r, &fc->skip_probs[k]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003503
3504 if (cm->seg.enabled && cm->seg.update_map) {
3505 if (cm->seg.temporal_update) {
3506 for (k = 0; k < PREDICTION_PROBS; k++)
Yaowu Xuf883b422016-08-30 14:01:10 -07003507 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003508 }
3509 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Yaowu Xuf883b422016-08-30 14:01:10 -07003510 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003511 }
3512
3513 for (j = 0; j < INTRA_MODES; j++)
3514 for (i = 0; i < INTRA_MODES - 1; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -07003515 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003516
3517#if CONFIG_EXT_PARTITION_TYPES
3518 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -07003519 av1_diff_update_prob(&r, &fc->partition_prob[0][i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003520 for (j = 1; j < PARTITION_CONTEXTS; ++j)
3521 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -07003522 av1_diff_update_prob(&r, &fc->partition_prob[j][i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003523#else
3524 for (j = 0; j < PARTITION_CONTEXTS; ++j)
3525 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -07003526 av1_diff_update_prob(&r, &fc->partition_prob[j][i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003527#endif // CONFIG_EXT_PARTITION_TYPES
3528
3529#if CONFIG_EXT_INTRA
3530 for (i = 0; i < INTRA_FILTERS + 1; ++i)
3531 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Yaowu Xuf883b422016-08-30 14:01:10 -07003532 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003533#endif // CONFIG_EXT_INTRA
3534
3535 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003536 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003537 for (k = 0; k < INTRA_MODES; k++)
3538 for (j = 0; j < INTRA_MODES; j++)
3539 for (i = 0; i < INTRA_MODES - 1; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -07003540 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003541 } else {
3542#if !CONFIG_REF_MV
3543 nmv_context *const nmvc = &fc->nmvc;
3544#endif
3545
3546 read_inter_mode_probs(fc, &r);
3547
3548#if CONFIG_EXT_INTER
3549 read_inter_compound_mode_probs(fc, &r);
3550 if (cm->reference_mode != COMPOUND_REFERENCE) {
3551 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
3552 if (is_interintra_allowed_bsize_group(i)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003553 av1_diff_update_prob(&r, &fc->interintra_prob[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003554 }
3555 }
3556 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
3557 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Yaowu Xuf883b422016-08-30 14:01:10 -07003558 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003559 }
3560 for (i = 0; i < BLOCK_SIZES; i++) {
3561 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003562 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003563 }
3564 }
3565 }
3566 if (cm->reference_mode != SINGLE_REFERENCE) {
3567 for (i = 0; i < BLOCK_SIZES; i++) {
3568 if (is_interinter_wedge_used(i)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003569 av1_diff_update_prob(&r, &fc->wedge_interinter_prob[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003570 }
3571 }
3572 }
3573#endif // CONFIG_EXT_INTER
3574
3575#if CONFIG_OBMC || CONFIG_WARPED_MOTION
3576 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
3577 for (j = 0; j < MOTION_VARIATIONS - 1; ++j)
Yaowu Xuf883b422016-08-30 14:01:10 -07003578 av1_diff_update_prob(&r, &fc->motvar_prob[i][j]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003579 }
3580#endif // CONFIG_OBMC || CONFIG_WARPED_MOTION
3581
3582 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
3583
3584 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07003585 av1_diff_update_prob(&r, &fc->intra_inter_prob[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003586
3587 if (cm->reference_mode != SINGLE_REFERENCE)
3588 setup_compound_reference_mode(cm);
3589
3590 read_frame_reference_mode_probs(cm, &r);
3591
3592 for (j = 0; j < BLOCK_SIZE_GROUPS; j++)
3593 for (i = 0; i < INTRA_MODES - 1; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -07003594 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003595
3596#if CONFIG_REF_MV
3597 for (i = 0; i < NMV_CONTEXTS; ++i)
3598 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
3599#else
3600 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
3601#endif
3602 read_ext_tx_probs(fc, &r);
3603#if CONFIG_SUPERTX
3604 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
3605#endif
3606#if CONFIG_GLOBAL_MOTION
3607 read_global_motion(cm, &r);
3608#endif // CONFIG_GLOBAL_MOTION
3609 }
3610
Yaowu Xuf883b422016-08-30 14:01:10 -07003611 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003612}
3613
3614#ifdef NDEBUG
3615#define debug_check_frame_counts(cm) (void)0
3616#else // !NDEBUG
3617// Counts should only be incremented when frame_parallel_decoding_mode and
3618// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07003619static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003620 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07003621 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003622 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
3623 cm->error_resilient_mode);
3624 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
3625 sizeof(cm->counts.y_mode)));
3626 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
3627 sizeof(cm->counts.uv_mode)));
3628 assert(!memcmp(cm->counts.partition, zero_counts.partition,
3629 sizeof(cm->counts.partition)));
3630 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
3631 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
3632 sizeof(cm->counts.eob_branch)));
3633 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
3634 sizeof(cm->counts.switchable_interp)));
3635 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
3636 sizeof(cm->counts.inter_mode)));
3637#if CONFIG_EXT_INTER
3638 assert(!memcmp(cm->counts.inter_compound_mode,
3639 zero_counts.inter_compound_mode,
3640 sizeof(cm->counts.inter_compound_mode)));
3641 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
3642 sizeof(cm->counts.interintra)));
3643 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
3644 sizeof(cm->counts.wedge_interintra)));
3645 assert(!memcmp(cm->counts.wedge_interinter, zero_counts.wedge_interinter,
3646 sizeof(cm->counts.wedge_interinter)));
3647#endif // CONFIG_EXT_INTER
3648#if CONFIG_OBMC || CONFIG_WARPED_MOTION
3649 assert(!memcmp(cm->counts.motvar, zero_counts.motvar,
3650 sizeof(cm->counts.motvar)));
3651#endif // CONFIG_OBMC || CONFIG_WARPED_MOTION
3652 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
3653 sizeof(cm->counts.intra_inter)));
3654 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
3655 sizeof(cm->counts.comp_inter)));
3656 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
3657 sizeof(cm->counts.single_ref)));
3658 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
3659 sizeof(cm->counts.comp_ref)));
3660#if CONFIG_EXT_REFS
3661 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
3662 sizeof(cm->counts.comp_bwdref)));
3663#endif // CONFIG_EXT_REFS
3664 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
3665 sizeof(cm->counts.tx_size)));
3666 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
3667#if CONFIG_REF_MV
3668 assert(
3669 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
3670 assert(
3671 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
3672#else
3673 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
3674#endif
3675 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
3676 sizeof(cm->counts.inter_ext_tx)));
3677 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
3678 sizeof(cm->counts.intra_ext_tx)));
3679}
3680#endif // NDEBUG
3681
Yaowu Xuf883b422016-08-30 14:01:10 -07003682static struct aom_read_bit_buffer *init_read_bit_buffer(
3683 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
3684 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003685 rb->bit_offset = 0;
3686 rb->error_handler = error_handler;
3687 rb->error_handler_data = &pbi->common;
3688 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003689 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003690 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
3691 rb->bit_buffer = clear_data;
3692 rb->bit_buffer_end = clear_data + n;
3693 } else {
3694 rb->bit_buffer = data;
3695 rb->bit_buffer_end = data_end;
3696 }
3697 return rb;
3698}
3699
3700//------------------------------------------------------------------------------
3701
Yaowu Xuf883b422016-08-30 14:01:10 -07003702int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
3703 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
3704 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
3705 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003706}
3707
Yaowu Xuf883b422016-08-30 14:01:10 -07003708void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
3709 int *height) {
3710 *width = aom_rb_read_literal(rb, 16) + 1;
3711 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003712}
3713
Yaowu Xuf883b422016-08-30 14:01:10 -07003714BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
3715 int profile = aom_rb_read_bit(rb);
3716 profile |= aom_rb_read_bit(rb) << 1;
3717 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003718 return (BITSTREAM_PROFILE)profile;
3719}
3720
Yaowu Xuf883b422016-08-30 14:01:10 -07003721void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
3722 const uint8_t *data_end, const uint8_t **p_data_end) {
3723 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003724 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07003725 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003726 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003727 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07003728 size_t first_partition_size;
3729 YV12_BUFFER_CONFIG *new_fb;
3730
3731#if CONFIG_BITSTREAM_DEBUG
3732 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
3733#endif
3734
3735 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003736 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07003737 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003738 xd->cur_buf = new_fb;
3739#if CONFIG_GLOBAL_MOTION
3740 xd->global_motion = cm->global_motion;
3741#endif // CONFIG_GLOBAL_MOTION
3742
3743 if (!first_partition_size) {
3744// showing a frame directly
3745#if CONFIG_EXT_REFS
3746 if (cm->show_existing_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07003747 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003748 else
3749#endif // CONFIG_EXT_REFS
3750 *p_data_end = data + (cm->profile <= PROFILE_2 ? 1 : 2);
3751
3752 return;
3753 }
3754
Yaowu Xuf883b422016-08-30 14:01:10 -07003755 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003756 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003757 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003758 "Truncated packet or corrupt header length");
3759
3760 cm->use_prev_frame_mvs =
3761 !cm->error_resilient_mode && cm->width == cm->last_width &&
3762 cm->height == cm->last_height && !cm->last_intra_only &&
3763 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
3764#if CONFIG_EXT_REFS
3765 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
3766 // show_exisiting_frame=1, nor can it take a frame not used as
3767 // a reference, it is probable that by the time it is being
3768 // referred to, the frame buffer it originally points to may
3769 // already get expired and have been reassigned to the current
3770 // newly coded frame. Hence, we need to check whether this is
3771 // the case, and if yes, we have 2 choices:
3772 // (1) Simply disable the use of previous frame mvs; or
3773 // (2) Have cm->prev_frame point to one reference frame buffer,
3774 // e.g. LAST_FRAME.
3775 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
3776 // Reassign the LAST_FRAME buffer to cm->prev_frame.
3777 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
3778 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
3779 }
3780#endif // CONFIG_EXT_REFS
3781
Yaowu Xuf883b422016-08-30 14:01:10 -07003782 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003783
3784 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
3785 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07003786 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003787 "Uninitialized entropy context.");
3788
Yaowu Xuf883b422016-08-30 14:01:10 -07003789 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003790
3791 xd->corrupted = 0;
3792 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
3793 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003794 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003795 "Decode failed. Frame data header is corrupted.");
3796
3797 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003798 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003799 }
3800
3801 // If encoded in frame parallel mode, frame context is ready after decoding
3802 // the frame header.
3803 if (cm->frame_parallel_decode &&
3804 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003805 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003806 FrameWorkerData *const frame_worker_data = worker->data1;
3807 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
3808 context_updated = 1;
3809 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
3810 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003811 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003812 pbi->cur_buf->row = -1;
3813 pbi->cur_buf->col = -1;
3814 frame_worker_data->frame_context_ready = 1;
3815 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07003816 av1_frameworker_signal_stats(worker);
3817 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003818 }
3819
3820#if CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07003821 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003822 cm->coef_probs_update_idx = 0;
3823#endif // CONFIG_ENTROPY
3824
3825 if (pbi->max_threads > 1
3826#if CONFIG_EXT_TILE
3827 && pbi->dec_tile_col < 0 // Decoding all columns
3828#endif // CONFIG_EXT_TILE
3829 && cm->tile_cols > 1) {
3830 // Multi-threaded tile decoder
3831 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
3832 if (!xd->corrupted) {
3833 if (!cm->skip_loop_filter) {
3834 // If multiple threads are used to decode tiles, then we use those
3835 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07003836 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
3837 0, 0, pbi->tile_workers, pbi->num_tile_workers,
3838 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003839 }
3840 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003841 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003842 "Decode failed. Frame data is corrupted.");
3843 }
3844 } else {
3845 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
3846 }
3847#if CONFIG_LOOP_RESTORATION
3848 if (cm->rst_info.restoration_type != RESTORE_NONE) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003849 av1_loop_restoration_init(&cm->rst_internal, &cm->rst_info,
3850 cm->frame_type == KEY_FRAME, cm->width,
3851 cm->height);
3852 av1_loop_restoration_rows(new_fb, cm, 0, cm->mi_rows, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003853 }
3854#endif // CONFIG_LOOP_RESTORATION
3855
3856 if (!xd->corrupted) {
3857 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3858#if CONFIG_ENTROPY
3859 cm->partial_prob_update = 0;
3860#endif // CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07003861 av1_adapt_coef_probs(cm);
3862 av1_adapt_intra_frame_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003863
3864 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003865 av1_adapt_inter_frame_probs(cm);
3866 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003867 }
3868 } else {
3869 debug_check_frame_counts(cm);
3870 }
3871 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003872 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003873 "Decode failed. Frame data is corrupted.");
3874 }
3875
3876 // Non frame parallel update frame context here.
3877 if (!cm->error_resilient_mode && !context_updated)
3878 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
3879}