blob: ec85645b150ec64ac4cdaa0b8fc2fb736a770bdc [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 "./aom_config.h"
16#include "./aom_dsp_rtcd.h"
17#include "./aom_scale_rtcd.h"
Jingning Han1aab8182016-06-03 11:09:06 -070018#include "./av1_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070019
Steinar Midtskogen2fd70ee2016-09-02 10:02:30 +020020#include "aom/aom_codec.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070021#include "aom_dsp/aom_dsp_common.h"
Jingning Han1aab8182016-06-03 11:09:06 -070022#include "aom_dsp/bitreader.h"
23#include "aom_dsp/bitreader_buffer.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070024#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070025#include "aom_ports/mem.h"
26#include "aom_ports/mem_ops.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070027#include "aom_scale/aom_scale.h"
28#include "aom_util/aom_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070029
30#include "av1/common/alloccommon.h"
31#if CONFIG_CLPF
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +020032#include "aom/aom_image.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070033#include "av1/common/clpf.h"
34#endif
35#include "av1/common/common.h"
36#if CONFIG_DERING
37#include "av1/common/dering.h"
38#endif // CONFIG_DERING
39#include "av1/common/entropy.h"
40#include "av1/common/entropymode.h"
Thomas Davies6519beb2016-10-19 14:46:07 +010041#include "av1/common/entropymv.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070042#include "av1/common/idct.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070043#include "av1/common/pred_common.h"
44#include "av1/common/quant_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070045#include "av1/common/reconinter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070046#include "av1/common/reconintra.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070047#include "av1/common/seg_common.h"
Jingning Han1aab8182016-06-03 11:09:06 -070048#include "av1/common/thread_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070049#include "av1/common/tile_common.h"
50
51#include "av1/decoder/decodeframe.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070052#include "av1/decoder/decodemv.h"
53#include "av1/decoder/decoder.h"
Jingning Han1aab8182016-06-03 11:09:06 -070054#include "av1/decoder/detokenize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070055#include "av1/decoder/dsubexp.h"
56
Yue Chen69f18e12016-09-08 14:48:15 -070057#if CONFIG_WARPED_MOTION
58#include "av1/common/warped_motion.h"
59#endif // CONFIG_WARPED_MOTION
60
Yaowu Xuf883b422016-08-30 14:01:10 -070061#define MAX_AV1_HEADER_SIZE 80
Michael Bebenita6048d052016-08-25 14:40:54 -070062#define ACCT_STR __func__
Yaowu Xuc27fc142016-08-22 16:08:15 -070063
Yushin Cho77bba8d2016-11-04 16:36:56 -070064#if CONFIG_PVQ
65#include "av1/decoder/pvq_decoder.h"
Yushin Cho70669122016-12-08 09:53:14 -100066#include "av1/common/pvq.h"
Yushin Cho77bba8d2016-11-04 16:36:56 -070067#include "av1/encoder/encodemb.h"
68
69#include "aom_dsp/entdec.h"
70#include "av1/common/partition.h"
71#include "av1/decoder/decint.h"
72#include "av1/encoder/hybrid_fwd_txfm.h"
73#endif
74
Thomas Davies80188d12016-10-26 16:08:35 -070075static struct aom_read_bit_buffer *init_read_bit_buffer(
76 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
77 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]);
78static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
79 size_t partition_size);
80static size_t read_uncompressed_header(AV1Decoder *pbi,
81 struct aom_read_bit_buffer *rb);
82
Yaowu Xuf883b422016-08-30 14:01:10 -070083static int is_compound_reference_allowed(const AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070084 int i;
85 if (frame_is_intra_only(cm)) return 0;
86 for (i = 1; i < INTER_REFS_PER_FRAME; ++i)
87 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
88
89 return 0;
90}
91
Yaowu Xuf883b422016-08-30 14:01:10 -070092static void setup_compound_reference_mode(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070093#if CONFIG_EXT_REFS
94 cm->comp_fwd_ref[0] = LAST_FRAME;
95 cm->comp_fwd_ref[1] = LAST2_FRAME;
96 cm->comp_fwd_ref[2] = LAST3_FRAME;
97 cm->comp_fwd_ref[3] = GOLDEN_FRAME;
98
99 cm->comp_bwd_ref[0] = BWDREF_FRAME;
100 cm->comp_bwd_ref[1] = ALTREF_FRAME;
101#else
102 if (cm->ref_frame_sign_bias[LAST_FRAME] ==
103 cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
104 cm->comp_fixed_ref = ALTREF_FRAME;
105 cm->comp_var_ref[0] = LAST_FRAME;
106 cm->comp_var_ref[1] = GOLDEN_FRAME;
107 } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
108 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
109 cm->comp_fixed_ref = GOLDEN_FRAME;
110 cm->comp_var_ref[0] = LAST_FRAME;
111 cm->comp_var_ref[1] = ALTREF_FRAME;
112 } else {
113 cm->comp_fixed_ref = LAST_FRAME;
114 cm->comp_var_ref[0] = GOLDEN_FRAME;
115 cm->comp_var_ref[1] = ALTREF_FRAME;
116 }
117#endif // CONFIG_EXT_REFS
118}
119
120static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
121 return len != 0 && len <= (size_t)(end - start);
122}
123
Yaowu Xuf883b422016-08-30 14:01:10 -0700124static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
125 const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700126 return data > max ? max : data;
127}
128
Yaowu Xuf883b422016-08-30 14:01:10 -0700129static TX_MODE read_tx_mode(struct aom_read_bit_buffer *rb) {
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800130#if CONFIG_TX64X64
131 TX_MODE tx_mode =
132 aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
133 if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
134 return tx_mode;
135#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700136 return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800137#endif // CONFIG_TX64X64
Yaowu Xuc27fc142016-08-22 16:08:15 -0700138}
139
Yaowu Xuefc75352016-10-31 09:46:42 -0700140static void read_tx_size_probs(FRAME_CONTEXT *fc, aom_reader *r) {
141 int i, j, k;
142 for (i = 0; i < MAX_TX_DEPTH; ++i)
143 for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
144 for (k = 0; k < i + 1; ++k)
145 av1_diff_update_prob(r, &fc->tx_size_probs[i][j][k], ACCT_STR);
146}
147
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400148#if !CONFIG_EC_ADAPT
Yaowu Xuf883b422016-08-30 14:01:10 -0700149static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700150 int i, j;
Nathan E. Egge4947c292016-04-26 11:37:06 -0400151 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700152 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700153 av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i], ACCT_STR);
Nathan E. Egge4947c292016-04-26 11:37:06 -0400154 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700155}
Thomas Davies6519beb2016-10-19 14:46:07 +0100156#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700157
Yaowu Xuf883b422016-08-30 14:01:10 -0700158static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700159#if CONFIG_REF_MV
Yaowu Xu8af861b2016-11-01 12:12:11 -0700160 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700161 for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700162 av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700163 for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700164 av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700165 for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700166 av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700167 for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700168 av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700169#if CONFIG_EXT_INTER
Michael Bebenita6048d052016-08-25 14:40:54 -0700170 av1_diff_update_prob(r, &fc->new2mv_prob, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700171#endif // CONFIG_EXT_INTER
172#else
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400173#if !CONFIG_EC_ADAPT
Yaowu Xu8af861b2016-11-01 12:12:11 -0700174 int i, j;
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400175 for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700176 for (j = 0; j < INTER_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700177 av1_diff_update_prob(r, &fc->inter_mode_probs[i][j], ACCT_STR);
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400178 }
Yaowu Xu8af861b2016-11-01 12:12:11 -0700179#else
180 (void)fc;
181 (void)r;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182#endif
Thomas Davies6519beb2016-10-19 14:46:07 +0100183#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700184}
185
186#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700187static void read_inter_compound_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700188 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -0700189 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700190 for (j = 0; j < INTER_MODE_CONTEXTS; ++j) {
191 for (i = 0; i < INTER_COMPOUND_MODES - 1; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700192 av1_diff_update_prob(r, &fc->inter_compound_mode_probs[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700193 }
194 }
195 }
196}
197#endif // CONFIG_EXT_INTER
Yaowu Xu17fd2f22016-11-17 18:23:28 -0800198#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100199#if !CONFIG_EXT_TX
200static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
201 int i, j, k;
202 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
203 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
204 for (j = 0; j < TX_TYPES; ++j) {
205 for (k = 0; k < TX_TYPES - 1; ++k)
206 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[i][j][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100207 }
208 }
209 }
210 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
211 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
212 for (k = 0; k < TX_TYPES - 1; ++k)
213 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[i][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100214 }
215 }
216}
217#endif
218#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700219
220static REFERENCE_MODE read_frame_reference_mode(
Yaowu Xuf883b422016-08-30 14:01:10 -0700221 const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700222 if (is_compound_reference_allowed(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700223 return aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700224 ? REFERENCE_MODE_SELECT
Yaowu Xuf883b422016-08-30 14:01:10 -0700225 : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700226 } else {
227 return SINGLE_REFERENCE;
228 }
229}
230
Yaowu Xuf883b422016-08-30 14:01:10 -0700231static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700232 FRAME_CONTEXT *const fc = cm->fc;
233 int i, j;
234
235 if (cm->reference_mode == REFERENCE_MODE_SELECT)
236 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700237 av1_diff_update_prob(r, &fc->comp_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700238
239 if (cm->reference_mode != COMPOUND_REFERENCE) {
240 for (i = 0; i < REF_CONTEXTS; ++i) {
241 for (j = 0; j < (SINGLE_REFS - 1); ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700242 av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700243 }
244 }
245 }
246
247 if (cm->reference_mode != SINGLE_REFERENCE) {
248 for (i = 0; i < REF_CONTEXTS; ++i) {
249#if CONFIG_EXT_REFS
250 for (j = 0; j < (FWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700251 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700252 for (j = 0; j < (BWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700253 av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700254#else
255 for (j = 0; j < (COMP_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700256 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700257#endif // CONFIG_EXT_REFS
258 }
259 }
260}
261
Yaowu Xuf883b422016-08-30 14:01:10 -0700262static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700263 int i;
Michael Bebenita6048d052016-08-25 14:40:54 -0700264 for (i = 0; i < n; ++i) av1_diff_update_prob(r, &p[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700265}
266
Yaowu Xuf883b422016-08-30 14:01:10 -0700267static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Thomas9ac55082016-09-23 18:04:17 +0100268 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700269
Yaowu Xue86288d2016-10-31 15:56:38 -0700270#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100271 int j;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700272 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
273
Yaowu Xuc27fc142016-08-22 16:08:15 -0700274 for (i = 0; i < 2; ++i) {
275 nmv_component *const comp_ctx = &ctx->comps[i];
276 update_mv_probs(&comp_ctx->sign, 1, r);
277 update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
278 update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
279 update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
280 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700281 for (i = 0; i < 2; ++i) {
282 nmv_component *const comp_ctx = &ctx->comps[i];
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400283 for (j = 0; j < CLASS0_SIZE; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700284 update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400285 }
Nathan E. Eggeac499f32016-09-08 15:38:57 -0400286 update_mv_probs(comp_ctx->fp, MV_FP_SIZE - 1, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700287 }
Alex Converseaca9feb2016-10-10 11:08:10 -0700288#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700289
290 if (allow_hp) {
291 for (i = 0; i < 2; ++i) {
292 nmv_component *const comp_ctx = &ctx->comps[i];
293 update_mv_probs(&comp_ctx->class0_hp, 1, r);
294 update_mv_probs(&comp_ctx->hp, 1, r);
295 }
296 }
297}
298
299static void inverse_transform_block(MACROBLOCKD *xd, int plane,
300 const TX_TYPE tx_type,
301 const TX_SIZE tx_size, uint8_t *dst,
Jingning Han1be18782016-10-21 11:48:15 -0700302 int stride, int16_t scan_line, int eob) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700303 struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han1be18782016-10-21 11:48:15 -0700304 tran_low_t *const dqcoeff = pd->dqcoeff;
305 INV_TXFM_PARAM inv_txfm_param;
306 inv_txfm_param.tx_type = tx_type;
307 inv_txfm_param.tx_size = tx_size;
308 inv_txfm_param.eob = eob;
309 inv_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700310
Yaowu Xuf883b422016-08-30 14:01:10 -0700311#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han1be18782016-10-21 11:48:15 -0700312 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
313 inv_txfm_param.bd = xd->bd;
314 highbd_inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
315 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700316#endif // CONFIG_AOM_HIGHBITDEPTH
Jingning Han1be18782016-10-21 11:48:15 -0700317 inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
Yaowu Xuf883b422016-08-30 14:01:10 -0700318#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700319 }
Jingning Han1be18782016-10-21 11:48:15 -0700320#endif // CONFIG_AOM_HIGHBITDEPTH
321 memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700322}
323
Yushin Cho77bba8d2016-11-04 16:36:56 -0700324#if CONFIG_PVQ
325static int av1_pvq_decode_helper(od_dec_ctx *dec, int16_t *ref_coeff,
326 int16_t *dqcoeff, int16_t *quant, int pli,
327 int bs, TX_TYPE tx_type, int xdec,
328 int ac_dc_coded) {
329 unsigned int flags; // used for daala's stream analyzer.
330 int off;
331 const int is_keyframe = 0;
332 const int has_dc_skip = 1;
Debargha Mukherjee153e1f82016-11-17 09:59:14 -0800333 int quant_shift = get_tx_scale(bs);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700334 // DC quantizer for PVQ
335 int pvq_dc_quant;
336 int lossless = (quant[0] == 0);
337 const int blk_size = tx_size_wide[bs];
338 int eob = 0;
339 int i;
Yushin Cho70669122016-12-08 09:53:14 -1000340 int use_activity_masking = dec->use_activity_masking;
Yushin Cho48f84db2016-11-07 21:20:17 -0800341 DECLARE_ALIGNED(16, int16_t, dqcoeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
342 DECLARE_ALIGNED(16, int16_t, ref_coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700343
Yushin Cho48f84db2016-11-07 21:20:17 -0800344 od_coeff ref_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
345 od_coeff out_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700346
347 od_raster_to_coding_order(ref_coeff_pvq, blk_size, tx_type, ref_coeff,
348 blk_size);
349
350 if (lossless)
351 pvq_dc_quant = 1;
352 else {
Yushin Cho70669122016-12-08 09:53:14 -1000353 if (use_activity_masking)
354 pvq_dc_quant = OD_MAXI(
355 1, (quant[0] >> quant_shift) *
356 dec->state.pvq_qm_q4[pli][od_qm_get_index(bs, 0)] >>
357 4);
358 else
359 pvq_dc_quant = OD_MAXI(1, quant[0] >> quant_shift);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700360 }
361
362 off = od_qm_offset(bs, xdec);
363
364 // copy int16 inputs to int32
365 for (i = 0; i < blk_size * blk_size; i++) ref_int32[i] = ref_coeff_pvq[i];
366
367 od_pvq_decode(dec, ref_int32, out_int32, (int)quant[1] >> quant_shift, pli,
368 bs, OD_PVQ_BETA[use_activity_masking][pli][bs],
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800369 OD_ROBUST_STREAM, is_keyframe, &flags, ac_dc_coded,
370 dec->state.qm + off, dec->state.qm_inv + off);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700371
372 // copy int32 result back to int16
373 for (i = 0; i < blk_size * blk_size; i++) dqcoeff_pvq[i] = out_int32[i];
374
375 if (!has_dc_skip || dqcoeff_pvq[0]) {
376 dqcoeff_pvq[0] =
377 has_dc_skip + generic_decode(dec->ec, &dec->state.adapt.model_dc[pli],
378 -1, &dec->state.adapt.ex_dc[pli][bs][0], 2,
379 "dc:mag");
380 if (dqcoeff_pvq[0])
381 dqcoeff_pvq[0] *= od_ec_dec_bits(dec->ec, 1, "dc:sign") ? -1 : 1;
382 }
383 dqcoeff_pvq[0] = dqcoeff_pvq[0] * pvq_dc_quant + ref_coeff_pvq[0];
384
385 od_coding_order_to_raster(dqcoeff, blk_size, tx_type, dqcoeff_pvq, blk_size);
386
387 eob = blk_size * blk_size;
388
389 return eob;
390}
391
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800392static int av1_pvq_decode_helper2(MACROBLOCKD *const xd,
393 MB_MODE_INFO *const mbmi, int plane, int row,
394 int col, TX_SIZE tx_size, TX_TYPE tx_type) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700395 struct macroblockd_plane *const pd = &xd->plane[plane];
396 // transform block size in pixels
397 int tx_blk_size = tx_size_wide[tx_size];
398 int i, j;
399 tran_low_t *pvq_ref_coeff = pd->pvq_ref_coeff;
400 const int diff_stride = tx_blk_size;
401 int16_t *pred = pd->pred;
402 tran_low_t *const dqcoeff = pd->dqcoeff;
403 int ac_dc_coded; // bit0: DC coded, bit1 : AC coded
404 uint8_t *dst;
405 int eob;
406
407 eob = 0;
408 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
409
410 // decode ac/dc coded flag. bit0: DC coded, bit1 : AC coded
411 // NOTE : we don't use 5 symbols for luma here in aom codebase,
412 // since block partition is taken care of by aom.
413 // So, only AC/DC skip info is coded
414 ac_dc_coded = od_decode_cdf_adapt(
415 xd->daala_dec.ec,
416 xd->daala_dec.state.adapt.skip_cdf[2 * tx_size + (plane != 0)], 4,
417 xd->daala_dec.state.adapt.skip_increment, "skip");
418
419 if (ac_dc_coded) {
420 int xdec = pd->subsampling_x;
421 int seg_id = mbmi->segment_id;
422 int16_t *quant;
423 FWD_TXFM_PARAM fwd_txfm_param;
Yaowu Xufc1b2132016-11-07 15:16:15 -0800424 // ToDo(yaowu): correct this with optimal number from decoding process.
425 const int max_scan_line = tx_size_2d[tx_size];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700426
427 for (j = 0; j < tx_blk_size; j++)
428 for (i = 0; i < tx_blk_size; i++) {
429 pred[diff_stride * j + i] = dst[pd->dst.stride * j + i];
430 }
431
432 fwd_txfm_param.tx_type = tx_type;
433 fwd_txfm_param.tx_size = tx_size;
434 fwd_txfm_param.fwd_txfm_opt = FWD_TXFM_OPT_NORMAL;
435 fwd_txfm_param.rd_transform = 0;
436 fwd_txfm_param.lossless = xd->lossless[seg_id];
437
438 fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
439
440 quant = &pd->seg_dequant[seg_id][0]; // aom's quantizer
441
442 eob = av1_pvq_decode_helper(&xd->daala_dec, pvq_ref_coeff, dqcoeff, quant,
443 plane, tx_size, tx_type, xdec, ac_dc_coded);
444
445 // Since av1 does not have separate inverse transform
446 // but also contains adding to predicted image,
447 // pass blank dummy image to av1_inv_txfm_add_*x*(), i.e. set dst as zeros
448 for (j = 0; j < tx_blk_size; j++)
449 for (i = 0; i < tx_blk_size; i++) dst[j * pd->dst.stride + i] = 0;
450
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800451 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
452 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700453 }
454
455 return eob;
456}
457#endif
458
Angie Chiangff6d8902016-10-21 11:02:09 -0700459static void predict_and_reconstruct_intra_block(AV1_COMMON *cm,
460 MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700461#if CONFIG_ANS
462 struct AnsDecoder *const r,
463#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700464 aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700465#endif // CONFIG_ANS
466 MB_MODE_INFO *const mbmi,
467 int plane, int row, int col,
468 TX_SIZE tx_size) {
469 struct macroblockd_plane *const pd = &xd->plane[plane];
470 PREDICTION_MODE mode = (plane == 0) ? mbmi->mode : mbmi->uv_mode;
471 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
472 uint8_t *dst;
473 int block_idx = (row << 1) + col;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700474#if CONFIG_PVQ
475 (void)cm;
476 (void)r;
477#endif
Jingning Hanca14dda2016-12-09 09:36:00 -0800478 dst = &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700479
Jingning Han41bb3392016-12-14 10:46:48 -0800480#if !CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -0700481 if (mbmi->sb_type < BLOCK_8X8)
482 if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
Jingning Han41bb3392016-12-14 10:46:48 -0800483#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700484
Jingning Hanc4c99da2016-10-24 10:27:28 -0700485 av1_predict_intra_block(xd, pd->width, pd->height, tx_size, mode, dst,
Yaowu Xuf883b422016-08-30 14:01:10 -0700486 pd->dst.stride, dst, pd->dst.stride, col, row, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700487
488 if (!mbmi->skip) {
489 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700490#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700491 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 0);
Jingning Han1be18782016-10-21 11:48:15 -0700492 int16_t max_scan_line = 0;
493 const int eob =
494 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size,
495 tx_type, &max_scan_line, r, mbmi->segment_id);
Angie Chianged8cd9a2016-10-21 16:44:47 -0700496#if CONFIG_ADAPT_SCAN
497 av1_update_scan_count_facade(cm, tx_size, tx_type, pd->dqcoeff, eob);
498#endif
Jingning Han1be18782016-10-21 11:48:15 -0700499 if (eob)
500 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
501 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700502#else
503 av1_pvq_decode_helper2(xd, mbmi, plane, row, col, tx_size, tx_type);
504#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700505 }
506}
507
508#if CONFIG_VAR_TX
Angie Chiangff6d8902016-10-21 11:02:09 -0700509static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
510 aom_reader *r, MB_MODE_INFO *const mbmi,
Jingning Han8fd62b72016-10-21 12:55:54 -0700511 int plane, BLOCK_SIZE plane_bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700512 int blk_row, int blk_col, TX_SIZE tx_size,
513 int *eob_total) {
514 const struct macroblockd_plane *const pd = &xd->plane[plane];
515 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
516 const int tx_row = blk_row >> (1 - pd->subsampling_y);
517 const int tx_col = blk_col >> (1 - pd->subsampling_x);
518 const TX_SIZE plane_tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700519 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700520 : mbmi->inter_tx_size[tx_row][tx_col];
Jingning Han5f614262016-10-27 14:27:43 -0700521 // Scale to match transform block unit.
Jingning Hanf64062f2016-11-02 16:22:18 -0700522 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
523 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700524
525 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
526
527 if (tx_size == plane_tx_size) {
528 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
Jingning Han8fd62b72016-10-21 12:55:54 -0700529 int block_idx = (blk_row << 1) + blk_col;
530 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiangff6d8902016-10-21 11:02:09 -0700531 const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700532 int16_t max_scan_line = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700533 const int eob =
Yaowu Xuf883b422016-08-30 14:01:10 -0700534 av1_decode_block_tokens(xd, plane, sc, blk_col, blk_row, plane_tx_size,
Jingning Han1be18782016-10-21 11:48:15 -0700535 tx_type, &max_scan_line, r, mbmi->segment_id);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700536 inverse_transform_block(
537 xd, plane, tx_type, plane_tx_size,
538 &pd->dst.buf[4 * blk_row * pd->dst.stride + 4 * blk_col],
Jingning Han1be18782016-10-21 11:48:15 -0700539 pd->dst.stride, max_scan_line, eob);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700540 *eob_total += eob;
541 } else {
Jingning Hanf64062f2016-11-02 16:22:18 -0700542 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
543 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700544 int i;
545
546 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700547
548 for (i = 0; i < 4; ++i) {
Jingning Han5f614262016-10-27 14:27:43 -0700549 const int offsetr = blk_row + (i >> 1) * bsl;
550 const int offsetc = blk_col + (i & 0x01) * bsl;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700551
552 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
553
Jingning Han8fd62b72016-10-21 12:55:54 -0700554 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
Jingning Hanf64062f2016-11-02 16:22:18 -0700555 offsetc, sub_txs, eob_total);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700556 }
557 }
558}
559#endif // CONFIG_VAR_TX
560
Jingning Hanfe45b212016-11-22 10:30:23 -0800561#if !CONFIG_VAR_TX || CONFIG_SUPERTX || \
562 (!CONFIG_VAR_TX && CONFIG_EXT_TX && CONFIG_RECT_TX)
Angie Chiangff6d8902016-10-21 11:02:09 -0700563static int reconstruct_inter_block(AV1_COMMON *cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700564#if CONFIG_ANS
565 struct AnsDecoder *const r,
566#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700567 aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700568#endif
569 int segment_id, int plane, int row, int col,
570 TX_SIZE tx_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700571 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
572 int block_idx = (row << 1) + col;
573 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700574#if CONFIG_PVQ
575 int eob;
576 (void)cm;
577 (void)r;
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800578 (void)segment_id;
579#else
580 struct macroblockd_plane *const pd = &xd->plane[plane];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700581#endif
582
583#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700584 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700585 int16_t max_scan_line = 0;
586 const int eob =
587 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size, tx_type,
588 &max_scan_line, r, segment_id);
Jingning Hanca14dda2016-12-09 09:36:00 -0800589 uint8_t *dst =
590 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Angie Chianged8cd9a2016-10-21 16:44:47 -0700591#if CONFIG_ADAPT_SCAN
592 av1_update_scan_count_facade(cm, tx_size, tx_type, pd->dqcoeff, eob);
593#endif
Jingning Han1be18782016-10-21 11:48:15 -0700594 if (eob)
Jingning Hanca14dda2016-12-09 09:36:00 -0800595 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
596 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700597#else
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800598 eob = av1_pvq_decode_helper2(xd, &xd->mi[0]->mbmi, plane, row, col, tx_size,
599 tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700600#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700601 return eob;
602}
603#endif // !CONFIG_VAR_TX || CONFIG_SUPER_TX
604
Yaowu Xuf883b422016-08-30 14:01:10 -0700605static MB_MODE_INFO *set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606 BLOCK_SIZE bsize, int mi_row, int mi_col,
Jingning Hanfaad0e12016-12-07 10:54:57 -0800607 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700608 const int offset = mi_row * cm->mi_stride + mi_col;
609 int x, y;
610 const TileInfo *const tile = &xd->tile;
611
612 xd->mi = cm->mi_grid_visible + offset;
613 xd->mi[0] = &cm->mi[offset];
614 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
615 // passing bsize from decode_partition().
616 xd->mi[0]->mbmi.sb_type = bsize;
Angie Chiang394c3372016-11-03 11:13:15 -0700617#if CONFIG_RD_DEBUG
618 xd->mi[0]->mbmi.mi_row = mi_row;
619 xd->mi[0]->mbmi.mi_col = mi_col;
620#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621 for (y = 0; y < y_mis; ++y)
Jingning Han97d85482016-07-15 11:06:05 -0700622 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700623
Jingning Hanfaad0e12016-12-07 10:54:57 -0800624 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700625 set_skip_context(xd, mi_row, mi_col);
626
627#if CONFIG_VAR_TX
628 xd->max_tx_size = max_txsize_lookup[bsize];
629#endif
630
631 // Distance of Mb to the various image edges. These are specified to 8th pel
632 // as they are always compared to values that are in 1/8th pel units
633 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
634
Yaowu Xuf883b422016-08-30 14:01:10 -0700635 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700636 return &xd->mi[0]->mbmi;
637}
638
639#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700640static MB_MODE_INFO *set_offsets_extend(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700641 MACROBLOCKD *const xd,
642 const TileInfo *const tile,
643 BLOCK_SIZE bsize_pred, int mi_row_pred,
644 int mi_col_pred, int mi_row_ori,
645 int mi_col_ori) {
646 // Used in supertx
647 // (mi_row_ori, mi_col_ori): location for mv
648 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
Jingning Han93531242016-12-20 11:54:36 -0800649 const int bw = mi_size_wide[bsize_pred];
650 const int bh = mi_size_high[bsize_pred];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700651 const int offset = mi_row_ori * cm->mi_stride + mi_col_ori;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700652 xd->mi = cm->mi_grid_visible + offset;
653 xd->mi[0] = cm->mi + offset;
654 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
655 cm->mi_cols);
656
657 xd->up_available = (mi_row_ori > tile->mi_row_start);
658 xd->left_available = (mi_col_ori > tile->mi_col_start);
659
Jingning Hanfaad0e12016-12-07 10:54:57 -0800660 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700661
662 return &xd->mi[0]->mbmi;
663}
664
Yaowu Xuf883b422016-08-30 14:01:10 -0700665static MB_MODE_INFO *set_mb_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
666 BLOCK_SIZE bsize, int mi_row, int mi_col,
667 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700668 const int offset = mi_row * cm->mi_stride + mi_col;
669 const TileInfo *const tile = &xd->tile;
670 int x, y;
671
672 xd->mi = cm->mi_grid_visible + offset;
673 xd->mi[0] = cm->mi + offset;
674 xd->mi[0]->mbmi.sb_type = bsize;
675 for (y = 0; y < y_mis; ++y)
676 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
677
678 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
679 return &xd->mi[0]->mbmi;
680}
681
Yaowu Xuf883b422016-08-30 14:01:10 -0700682static void set_offsets_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700683 const TileInfo *const tile, BLOCK_SIZE bsize,
684 int mi_row, int mi_col) {
Jingning Han93531242016-12-20 11:54:36 -0800685 const int bw = mi_size_wide[bsize];
686 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700687 const int offset = mi_row * cm->mi_stride + mi_col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700688
689 xd->mi = cm->mi_grid_visible + offset;
690 xd->mi[0] = cm->mi + offset;
691
Jingning Hanfaad0e12016-12-07 10:54:57 -0800692 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700693
694 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
695
Yaowu Xuf883b422016-08-30 14:01:10 -0700696 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700697}
698
Yaowu Xuf883b422016-08-30 14:01:10 -0700699static void set_param_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700700 BLOCK_SIZE bsize, int mi_row, int mi_col,
701 int txfm, int skip) {
Jingning Han93531242016-12-20 11:54:36 -0800702 const int bw = mi_size_wide[bsize];
703 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -0700704 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
705 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700706 const int offset = mi_row * cm->mi_stride + mi_col;
707 int x, y;
708
709 xd->mi = cm->mi_grid_visible + offset;
710 xd->mi[0] = cm->mi + offset;
711
712 for (y = 0; y < y_mis; ++y)
713 for (x = 0; x < x_mis; ++x) {
714 xd->mi[y * cm->mi_stride + x]->mbmi.skip = skip;
715 xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
716 }
717#if CONFIG_VAR_TX
718 xd->above_txfm_context = cm->above_txfm_context + mi_col;
719 xd->left_txfm_context =
720 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
Yaowu Xu52a17632016-11-17 15:48:21 -0800721 set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bw, bh, skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700722#endif
723}
724
Yaowu Xuf883b422016-08-30 14:01:10 -0700725static void set_ref(AV1_COMMON *const cm, MACROBLOCKD *const xd, int idx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700726 int mi_row, int mi_col) {
727 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
728 RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
729 xd->block_refs[idx] = ref_buffer;
Yaowu Xuf883b422016-08-30 14:01:10 -0700730 if (!av1_is_valid_scale(&ref_buffer->sf))
731 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700732 "Invalid scale factors");
Yaowu Xuf883b422016-08-30 14:01:10 -0700733 av1_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
734 &ref_buffer->sf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700735 xd->corrupted |= ref_buffer->buf->corrupted;
736}
737
738static void dec_predict_b_extend(
Yaowu Xuf883b422016-08-30 14:01:10 -0700739 AV1Decoder *const pbi, MACROBLOCKD *const xd, const TileInfo *const tile,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700740 int block, int mi_row_ori, int mi_col_ori, int mi_row_pred, int mi_col_pred,
741 int mi_row_top, int mi_col_top, uint8_t *dst_buf[3], int dst_stride[3],
742 BLOCK_SIZE bsize_top, BLOCK_SIZE bsize_pred, int b_sub8x8, int bextend) {
743 // Used in supertx
744 // (mi_row_ori, mi_col_ori): location for mv
745 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
746 // (mi_row_top, mi_col_top, bsize_top): region of the top partition size
747 // block: sub location of sub8x8 blocks
748 // b_sub8x8: 1: ori is sub8x8; 0: ori is not sub8x8
749 // bextend: 1: region to predict is an extension of ori; 0: not
750 int r = (mi_row_pred - mi_row_top) * MI_SIZE;
751 int c = (mi_col_pred - mi_col_top) * MI_SIZE;
Jingning Han93531242016-12-20 11:54:36 -0800752 const int mi_width_top = mi_size_wide[bsize_top];
753 const int mi_height_top = mi_size_high[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700754 MB_MODE_INFO *mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700755 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700756
757 if (mi_row_pred < mi_row_top || mi_col_pred < mi_col_top ||
758 mi_row_pred >= mi_row_top + mi_height_top ||
759 mi_col_pred >= mi_col_top + mi_width_top || mi_row_pred >= cm->mi_rows ||
760 mi_col_pred >= cm->mi_cols)
761 return;
762
763 mbmi = set_offsets_extend(cm, xd, tile, bsize_pred, mi_row_pred, mi_col_pred,
764 mi_row_ori, mi_col_ori);
765 set_ref(cm, xd, 0, mi_row_pred, mi_col_pred);
766 if (has_second_ref(&xd->mi[0]->mbmi))
767 set_ref(cm, xd, 1, mi_row_pred, mi_col_pred);
768
769 if (!bextend) {
770 mbmi->tx_size = b_width_log2_lookup[bsize_top];
Jingning Han93531242016-12-20 11:54:36 -0800771#if CONFIG_CB4X4
772 ++mbmi->tx_size;
773#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700774 }
775
776 xd->plane[0].dst.stride = dst_stride[0];
777 xd->plane[1].dst.stride = dst_stride[1];
778 xd->plane[2].dst.stride = dst_stride[2];
779 xd->plane[0].dst.buf = dst_buf[0] +
780 (r >> xd->plane[0].subsampling_y) * dst_stride[0] +
781 (c >> xd->plane[0].subsampling_x);
782 xd->plane[1].dst.buf = dst_buf[1] +
783 (r >> xd->plane[1].subsampling_y) * dst_stride[1] +
784 (c >> xd->plane[1].subsampling_x);
785 xd->plane[2].dst.buf = dst_buf[2] +
786 (r >> xd->plane[2].subsampling_y) * dst_stride[2] +
787 (c >> xd->plane[2].subsampling_x);
788
789 if (!b_sub8x8)
Yaowu Xuf883b422016-08-30 14:01:10 -0700790 av1_build_inter_predictors_sb_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700792 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700793#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700794 mi_row_pred, mi_col_pred, bsize_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700795 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700796 av1_build_inter_predictors_sb_sub8x8_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700797#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700798 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700799#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700800 mi_row_pred, mi_col_pred,
801 bsize_pred, block);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700802}
803
Yaowu Xuf883b422016-08-30 14:01:10 -0700804static void dec_extend_dir(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700805 const TileInfo *const tile, int block,
806 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
807 int mi_col, int mi_row_top, int mi_col_top,
808 uint8_t *dst_buf[3], int dst_stride[3], int dir) {
809 // dir: 0-lower, 1-upper, 2-left, 3-right
810 // 4-lowerleft, 5-upperleft, 6-lowerright, 7-upperright
Jingning Han93531242016-12-20 11:54:36 -0800811 const int mi_width = mi_size_wide[bsize];
812 const int mi_height = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700813 int xss = xd->plane[1].subsampling_x;
814 int yss = xd->plane[1].subsampling_y;
815 int b_sub8x8 = (bsize < BLOCK_8X8) ? 1 : 0;
816 BLOCK_SIZE extend_bsize;
817 int unit, mi_row_pred, mi_col_pred;
818
819 if (dir == 0 || dir == 1) {
Jingning Han93531242016-12-20 11:54:36 -0800820 extend_bsize =
821 (mi_width == mi_size_wide[BLOCK_8X8] || bsize < BLOCK_8X8 || xss < yss)
822 ? BLOCK_8X8
823 : BLOCK_16X8;
824 unit = mi_size_wide[extend_bsize];
825 mi_row_pred = mi_row + ((dir == 0) ? mi_height : -mi_size_high[BLOCK_8X8]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700826 mi_col_pred = mi_col;
827
828 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
829 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
830 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
831
832 if (mi_width > unit) {
833 int i;
834 assert(!b_sub8x8);
Jingning Han863694a2016-12-21 09:42:24 -0800835 for (i = 0; i < mi_width; i += unit) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700836 mi_col_pred += unit;
837 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
838 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
839 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
840 }
841 }
842 } else if (dir == 2 || dir == 3) {
843 extend_bsize = (mi_height == 1 || bsize < BLOCK_8X8 || yss < xss)
844 ? BLOCK_8X8
845 : BLOCK_8X16;
846 unit = num_8x8_blocks_high_lookup[extend_bsize];
847 mi_row_pred = mi_row;
848 mi_col_pred = mi_col + ((dir == 3) ? mi_width : -1);
849
850 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
851 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
852 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
853
854 if (mi_height > unit) {
855 int i;
Jingning Han863694a2016-12-21 09:42:24 -0800856 for (i = 0; i < mi_height; i += unit) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700857 mi_row_pred += unit;
858 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
859 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
860 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
861 }
862 }
863 } else {
864 extend_bsize = BLOCK_8X8;
865 mi_row_pred = mi_row + ((dir == 4 || dir == 6) ? mi_height : -1);
866 mi_col_pred = mi_col + ((dir == 6 || dir == 7) ? mi_width : -1);
867 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
868 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
869 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
870 }
871}
872
Yaowu Xuf883b422016-08-30 14:01:10 -0700873static void dec_extend_all(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700874 const TileInfo *const tile, int block,
875 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
876 int mi_col, int mi_row_top, int mi_col_top,
877 uint8_t *dst_buf[3], int dst_stride[3]) {
878 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
879 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
880 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
881 mi_row_top, mi_col_top, dst_buf, dst_stride, 1);
882 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
883 mi_row_top, mi_col_top, dst_buf, dst_stride, 2);
884 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
885 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
886 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
887 mi_row_top, mi_col_top, dst_buf, dst_stride, 4);
888 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
889 mi_row_top, mi_col_top, dst_buf, dst_stride, 5);
890 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
891 mi_row_top, mi_col_top, dst_buf, dst_stride, 6);
892 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
893 mi_row_top, mi_col_top, dst_buf, dst_stride, 7);
894}
895
Yaowu Xuf883b422016-08-30 14:01:10 -0700896static void dec_predict_sb_complex(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700897 const TileInfo *const tile, int mi_row,
898 int mi_col, int mi_row_top, int mi_col_top,
899 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize,
900 uint8_t *dst_buf[3], int dst_stride[3]) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700901 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700902 const int hbs = num_8x8_blocks_wide_lookup[bsize] / 2;
903 const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
904 const BLOCK_SIZE subsize = get_subsize(bsize, partition);
905#if CONFIG_EXT_PARTITION_TYPES
906 const BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
907#endif
908 int i;
909 const int mi_offset = mi_row * cm->mi_stride + mi_col;
910 uint8_t *dst_buf1[3], *dst_buf2[3], *dst_buf3[3];
911
912 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
913 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
914 DECLARE_ALIGNED(16, uint8_t, tmp_buf3[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
915 int dst_stride1[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
916 int dst_stride2[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
917 int dst_stride3[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
918
Yaowu Xuf883b422016-08-30 14:01:10 -0700919#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700920 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
921 int len = sizeof(uint16_t);
922 dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
923 dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_TX_SQUARE * len);
924 dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_TX_SQUARE * len);
925 dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
926 dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_TX_SQUARE * len);
927 dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_TX_SQUARE * len);
928 dst_buf3[0] = CONVERT_TO_BYTEPTR(tmp_buf3);
929 dst_buf3[1] = CONVERT_TO_BYTEPTR(tmp_buf3 + MAX_TX_SQUARE * len);
930 dst_buf3[2] = CONVERT_TO_BYTEPTR(tmp_buf3 + 2 * MAX_TX_SQUARE * len);
931 } else {
932#endif
933 dst_buf1[0] = tmp_buf1;
934 dst_buf1[1] = tmp_buf1 + MAX_TX_SQUARE;
935 dst_buf1[2] = tmp_buf1 + 2 * MAX_TX_SQUARE;
936 dst_buf2[0] = tmp_buf2;
937 dst_buf2[1] = tmp_buf2 + MAX_TX_SQUARE;
938 dst_buf2[2] = tmp_buf2 + 2 * MAX_TX_SQUARE;
939 dst_buf3[0] = tmp_buf3;
940 dst_buf3[1] = tmp_buf3 + MAX_TX_SQUARE;
941 dst_buf3[2] = tmp_buf3 + 2 * MAX_TX_SQUARE;
Yaowu Xuf883b422016-08-30 14:01:10 -0700942#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700943 }
944#endif
945
946 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
947
948 xd->mi = cm->mi_grid_visible + mi_offset;
949 xd->mi[0] = cm->mi + mi_offset;
950
951 for (i = 0; i < MAX_MB_PLANE; i++) {
952 xd->plane[i].dst.buf = dst_buf[i];
953 xd->plane[i].dst.stride = dst_stride[i];
954 }
955
956 switch (partition) {
957 case PARTITION_NONE:
958 assert(bsize < top_bsize);
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, bsize, 0, 0);
962 dec_extend_all(pbi, xd, tile, 0, bsize, top_bsize, mi_row, mi_col,
963 mi_row_top, mi_col_top, dst_buf, dst_stride);
964 break;
965 case PARTITION_HORZ:
966 if (bsize == BLOCK_8X8) {
967 // For sub8x8, predict in 8x8 unit
968 // First half
969 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
970 mi_row_top, mi_col_top, dst_buf, dst_stride,
971 top_bsize, BLOCK_8X8, 1, 0);
972 if (bsize < top_bsize)
973 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
974 mi_row_top, mi_col_top, dst_buf, dst_stride);
975
976 // Second half
977 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
978 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
979 top_bsize, BLOCK_8X8, 1, 1);
980 if (bsize < top_bsize)
981 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
982 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
983
984 // weighted average to smooth the boundary
985 xd->plane[0].dst.buf = dst_buf[0];
986 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -0700987 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700988 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
989 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
990 0);
991 } else {
992 // First half
993 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
994 mi_row_top, mi_col_top, dst_buf, dst_stride,
995 top_bsize, subsize, 0, 0);
996 if (bsize < top_bsize)
997 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
998 mi_row_top, mi_col_top, dst_buf, dst_stride);
999 else
1000 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1001 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1002
1003 if (mi_row + hbs < cm->mi_rows) {
1004 // Second half
1005 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col,
1006 mi_row + hbs, mi_col, mi_row_top, mi_col_top,
1007 dst_buf1, dst_stride1, top_bsize, subsize, 0, 0);
1008 if (bsize < top_bsize)
1009 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1010 mi_col, mi_row_top, mi_col_top, dst_buf1,
1011 dst_stride1);
1012 else
1013 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1014 mi_col, mi_row_top, mi_col_top, dst_buf1,
1015 dst_stride1, 1);
1016
1017 // weighted average to smooth the boundary
1018 for (i = 0; i < MAX_MB_PLANE; i++) {
1019 xd->plane[i].dst.buf = dst_buf[i];
1020 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001021 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001022 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1023 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1024 PARTITION_HORZ, i);
1025 }
1026 }
1027 }
1028 break;
1029 case PARTITION_VERT:
1030 if (bsize == BLOCK_8X8) {
1031 // First half
1032 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1033 mi_row_top, mi_col_top, dst_buf, dst_stride,
1034 top_bsize, BLOCK_8X8, 1, 0);
1035 if (bsize < top_bsize)
1036 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1037 mi_row_top, mi_col_top, dst_buf, dst_stride);
1038
1039 // Second half
1040 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1041 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1042 top_bsize, BLOCK_8X8, 1, 1);
1043 if (bsize < top_bsize)
1044 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1045 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1046
1047 // Smooth
1048 xd->plane[0].dst.buf = dst_buf[0];
1049 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001050 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001051 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1052 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1053 0);
1054 } else {
1055 // First half
1056 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1057 mi_row_top, mi_col_top, dst_buf, dst_stride,
1058 top_bsize, subsize, 0, 0);
1059 if (bsize < top_bsize)
1060 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1061 mi_row_top, mi_col_top, dst_buf, dst_stride);
1062 else
1063 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1064 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1065
1066 // Second half
1067 if (mi_col + hbs < cm->mi_cols) {
1068 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1069 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1070 dst_stride1, top_bsize, subsize, 0, 0);
1071 if (bsize < top_bsize)
1072 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1073 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1074 dst_stride1);
1075 else
1076 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1077 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1078 dst_stride1, 2);
1079
1080 // Smooth
1081 for (i = 0; i < MAX_MB_PLANE; i++) {
1082 xd->plane[i].dst.buf = dst_buf[i];
1083 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001084 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001085 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1086 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1087 PARTITION_VERT, i);
1088 }
1089 }
1090 }
1091 break;
1092 case PARTITION_SPLIT:
1093 if (bsize == BLOCK_8X8) {
1094 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1095 mi_row_top, mi_col_top, dst_buf, dst_stride,
1096 top_bsize, BLOCK_8X8, 1, 0);
1097 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1098 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1099 top_bsize, BLOCK_8X8, 1, 1);
1100 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1101 mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1102 top_bsize, BLOCK_8X8, 1, 1);
1103 dec_predict_b_extend(pbi, xd, tile, 3, mi_row, mi_col, mi_row, mi_col,
1104 mi_row_top, mi_col_top, dst_buf3, dst_stride3,
1105 top_bsize, BLOCK_8X8, 1, 1);
1106 if (bsize < top_bsize) {
1107 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1108 mi_row_top, mi_col_top, dst_buf, dst_stride);
1109 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1110 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1111 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1112 mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1113 dec_extend_all(pbi, xd, tile, 3, subsize, top_bsize, mi_row, mi_col,
1114 mi_row_top, mi_col_top, dst_buf3, dst_stride3);
1115 }
1116 } else {
1117 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row_top,
1118 mi_col_top, subsize, top_bsize, dst_buf,
1119 dst_stride);
1120 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1121 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col + hbs,
1122 mi_row_top, mi_col_top, subsize, top_bsize,
1123 dst_buf1, dst_stride1);
1124 if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols)
1125 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col,
1126 mi_row_top, mi_col_top, subsize, top_bsize,
1127 dst_buf2, dst_stride2);
1128 if (mi_row + hbs < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1129 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col + hbs,
1130 mi_row_top, mi_col_top, subsize, top_bsize,
1131 dst_buf3, dst_stride3);
1132 }
1133 for (i = 0; i < MAX_MB_PLANE; i++) {
1134 if (bsize == BLOCK_8X8 && i != 0)
1135 continue; // Skip <4x4 chroma smoothing
1136 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001137 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001138 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1139 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1140 PARTITION_VERT, i);
1141 if (mi_row + hbs < cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001142 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001143 xd, dst_buf2[i], dst_stride2[i], dst_buf3[i], dst_stride3[i],
1144 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1145 PARTITION_VERT, i);
Yaowu Xuf883b422016-08-30 14:01:10 -07001146 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001147 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1148 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1149 PARTITION_HORZ, i);
1150 }
1151 } else if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001152 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001153 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1154 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1155 PARTITION_HORZ, i);
1156 }
1157 }
1158 break;
1159#if CONFIG_EXT_PARTITION_TYPES
1160 case PARTITION_HORZ_A:
1161 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1162 mi_row_top, mi_col_top, dst_buf, dst_stride,
1163 top_bsize, bsize2, 0, 0);
1164 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1165 mi_row_top, mi_col_top, dst_buf, dst_stride);
1166
1167 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1168 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1169 dst_stride1, top_bsize, bsize2, 0, 0);
1170 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1171 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1172
1173 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1174 mi_col, mi_row_top, mi_col_top, dst_buf2,
1175 dst_stride2, top_bsize, subsize, 0, 0);
1176 if (bsize < top_bsize)
1177 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1178 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1179 else
1180 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1181 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1182 1);
1183
1184 for (i = 0; i < MAX_MB_PLANE; i++) {
1185 xd->plane[i].dst.buf = dst_buf[i];
1186 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001187 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001188 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1189 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1190 i);
1191 }
1192 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001193 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001194 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1195 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1196 i);
1197 }
1198 break;
1199 case PARTITION_VERT_A:
1200
1201 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1202 mi_row_top, mi_col_top, dst_buf, dst_stride,
1203 top_bsize, bsize2, 0, 0);
1204 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1205 mi_row_top, mi_col_top, dst_buf, dst_stride);
1206
1207 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1208 mi_col, mi_row_top, mi_col_top, dst_buf1,
1209 dst_stride1, top_bsize, bsize2, 0, 0);
1210 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1211 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1212
1213 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1214 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1215 dst_stride2, top_bsize, subsize, 0, 0);
1216 if (bsize < top_bsize)
1217 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1218 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1219 dst_stride2);
1220 else
1221 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1222 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1223 dst_stride2, 2);
1224
1225 for (i = 0; i < MAX_MB_PLANE; i++) {
1226 xd->plane[i].dst.buf = dst_buf[i];
1227 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001228 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001229 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1230 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1231 i);
1232 }
1233 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001234 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001235 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1236 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1237 i);
1238 }
1239 break;
1240 case PARTITION_HORZ_B:
1241 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1242 mi_row_top, mi_col_top, dst_buf, dst_stride,
1243 top_bsize, subsize, 0, 0);
1244 if (bsize < top_bsize)
1245 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1246 mi_row_top, mi_col_top, dst_buf, dst_stride);
1247 else
1248 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1249 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1250
1251 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1252 mi_col, mi_row_top, mi_col_top, dst_buf1,
1253 dst_stride1, top_bsize, bsize2, 0, 0);
1254 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1255 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1256
1257 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1258 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1259 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1260 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1261 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1262 dst_stride2);
1263
1264 for (i = 0; i < MAX_MB_PLANE; i++) {
1265 xd->plane[i].dst.buf = dst_buf1[i];
1266 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001267 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001268 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1269 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1270 PARTITION_VERT, i);
1271 }
1272 for (i = 0; i < MAX_MB_PLANE; i++) {
1273 xd->plane[i].dst.buf = dst_buf[i];
1274 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001275 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001276 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1277 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1278 i);
1279 }
1280 break;
1281 case PARTITION_VERT_B:
1282 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1283 mi_row_top, mi_col_top, dst_buf, dst_stride,
1284 top_bsize, subsize, 0, 0);
1285 if (bsize < top_bsize)
1286 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1287 mi_row_top, mi_col_top, dst_buf, dst_stride);
1288 else
1289 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1290 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1291
1292 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1293 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1294 dst_stride1, top_bsize, bsize2, 0, 0);
1295 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1296 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1297
1298 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1299 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1300 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1301 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1302 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1303 dst_stride2);
1304
1305 for (i = 0; i < MAX_MB_PLANE; i++) {
1306 xd->plane[i].dst.buf = dst_buf1[i];
1307 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001308 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001309 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1310 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1311 PARTITION_HORZ, i);
1312 }
1313 for (i = 0; i < MAX_MB_PLANE; i++) {
1314 xd->plane[i].dst.buf = dst_buf[i];
1315 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001316 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001317 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1318 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1319 i);
1320 }
1321 break;
1322#endif // CONFIG_EXT_PARTITION_TYPES
1323 default: assert(0);
1324 }
1325}
1326
Yaowu Xuf883b422016-08-30 14:01:10 -07001327static void set_segment_id_supertx(const AV1_COMMON *const cm, const int mi_row,
1328 const int mi_col, const BLOCK_SIZE bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001329 const struct segmentation *seg = &cm->seg;
1330 const int miw =
Yaowu Xuf883b422016-08-30 14:01:10 -07001331 AOMMIN(num_8x8_blocks_wide_lookup[bsize], cm->mi_cols - mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001332 const int mih =
Yaowu Xuf883b422016-08-30 14:01:10 -07001333 AOMMIN(num_8x8_blocks_high_lookup[bsize], cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001334 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1335 MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
1336 int r, c;
1337 int seg_id_supertx = MAX_SEGMENTS;
1338
1339 if (!seg->enabled) {
1340 seg_id_supertx = 0;
1341 } else {
1342 // Find the minimum segment_id
1343 for (r = 0; r < mih; r++)
1344 for (c = 0; c < miw; c++)
1345 seg_id_supertx =
Yaowu Xuf883b422016-08-30 14:01:10 -07001346 AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001347 assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);
1348 }
1349
1350 // Assign the the segment_id back to segment_id_supertx
1351 for (r = 0; r < mih; r++)
1352 for (c = 0; c < miw; c++)
1353 mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
1354}
1355#endif // CONFIG_SUPERTX
1356
Yaowu Xuf883b422016-08-30 14:01:10 -07001357static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001358#if CONFIG_SUPERTX
1359 int supertx_enabled,
1360#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001361 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001362#if CONFIG_EXT_PARTITION_TYPES
1363 PARTITION_TYPE partition,
1364#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001365 BLOCK_SIZE bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001366 AV1_COMMON *const cm = &pbi->common;
Jingning Han85dc03f2016-12-06 16:03:10 -08001367 const int bw = mi_size_wide[bsize];
1368 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -07001369 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1370 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Nathan E. Eggeebbd4792016-10-05 19:30:15 -04001371 MB_MODE_INFO *mbmi;
1372
Michael Bebenita6048d052016-08-25 14:40:54 -07001373#if CONFIG_ACCOUNTING
1374 aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
1375#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001376#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001377 if (supertx_enabled) {
1378 mbmi = set_mb_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
1379 } else {
Jingning Hanfaad0e12016-12-07 10:54:57 -08001380 mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001381 }
1382#if CONFIG_EXT_PARTITION_TYPES
1383 xd->mi[0]->mbmi.partition = partition;
1384#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001385 av1_read_mode_info(pbi, xd, supertx_enabled, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001386#else
Jingning Hanfaad0e12016-12-07 10:54:57 -08001387 mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001388#if CONFIG_EXT_PARTITION_TYPES
1389 xd->mi[0]->mbmi.partition = partition;
1390#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001391 av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001392#endif // CONFIG_SUPERTX
1393
1394 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1395 const BLOCK_SIZE uv_subsize =
1396 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1397 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -07001398 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001399 "Invalid block size.");
1400 }
1401
1402#if CONFIG_SUPERTX
1403 mbmi->segment_id_supertx = MAX_SEGMENTS;
1404
1405 if (supertx_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001406 xd->corrupted |= aom_reader_has_error(r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001407 return;
1408 }
1409#endif // CONFIG_SUPERTX
1410
Arild Fuldseth07441162016-08-15 15:07:52 +02001411#if CONFIG_DELTA_Q
1412 if (cm->delta_q_present_flag) {
1413 int i;
1414 for (i = 0; i < MAX_SEGMENTS; i++) {
1415 xd->plane[0].seg_dequant[i][0] =
1416 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
1417 xd->plane[0].seg_dequant[i][1] =
1418 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
1419 xd->plane[1].seg_dequant[i][0] =
1420 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1421 xd->plane[1].seg_dequant[i][1] =
1422 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1423 xd->plane[2].seg_dequant[i][0] =
1424 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1425 xd->plane[2].seg_dequant[i][1] =
1426 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1427 }
1428 }
1429#endif
1430
Jingning Han41bb3392016-12-14 10:46:48 -08001431#if CONFIG_CB4X4
1432 if (mbmi->skip) reset_skip_context(xd, bsize);
1433#else
Jingning Hand39cc722016-12-02 14:03:26 -08001434 if (mbmi->skip) reset_skip_context(xd, AOMMAX(BLOCK_8X8, bsize));
Jingning Han41bb3392016-12-14 10:46:48 -08001435#endif
Jingning Hand39cc722016-12-02 14:03:26 -08001436
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001437#if CONFIG_COEF_INTERLEAVE
1438 {
1439 const struct macroblockd_plane *const pd_y = &xd->plane[0];
1440 const struct macroblockd_plane *const pd_c = &xd->plane[1];
1441 const TX_SIZE tx_log2_y = mbmi->tx_size;
1442 const TX_SIZE tx_log2_c = get_uv_tx_size(mbmi, pd_c);
1443 const int tx_sz_y = (1 << tx_log2_y);
1444 const int tx_sz_c = (1 << tx_log2_c);
1445 const int num_4x4_w_y = pd_y->n4_w;
1446 const int num_4x4_h_y = pd_y->n4_h;
1447 const int num_4x4_w_c = pd_c->n4_w;
1448 const int num_4x4_h_c = pd_c->n4_h;
1449 const int max_4x4_w_y = get_max_4x4_size(num_4x4_w_y, xd->mb_to_right_edge,
1450 pd_y->subsampling_x);
1451 const int max_4x4_h_y = get_max_4x4_size(num_4x4_h_y, xd->mb_to_bottom_edge,
1452 pd_y->subsampling_y);
1453 const int max_4x4_w_c = get_max_4x4_size(num_4x4_w_c, xd->mb_to_right_edge,
1454 pd_c->subsampling_x);
1455 const int max_4x4_h_c = get_max_4x4_size(num_4x4_h_c, xd->mb_to_bottom_edge,
1456 pd_c->subsampling_y);
1457
1458 // The max_4x4_w/h may be smaller than tx_sz under some corner cases,
1459 // i.e. when the SB is splitted by tile boundaries.
1460 const int tu_num_w_y = (max_4x4_w_y + tx_sz_y - 1) / tx_sz_y;
1461 const int tu_num_h_y = (max_4x4_h_y + tx_sz_y - 1) / tx_sz_y;
1462 const int tu_num_w_c = (max_4x4_w_c + tx_sz_c - 1) / tx_sz_c;
1463 const int tu_num_h_c = (max_4x4_h_c + tx_sz_c - 1) / tx_sz_c;
1464 const int tu_num_y = tu_num_w_y * tu_num_h_y;
1465 const int tu_num_c = tu_num_w_c * tu_num_h_c;
1466
1467 if (!is_inter_block(mbmi)) {
1468 int tu_idx_c = 0;
1469 int row_y, col_y, row_c, col_c;
1470 int plane;
1471
1472#if CONFIG_PALETTE
1473 for (plane = 0; plane <= 1; ++plane) {
1474 if (mbmi->palette_mode_info.palette_size[plane])
1475 av1_decode_palette_tokens(xd, plane, r);
1476 }
1477#endif
1478
1479 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1480 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1481 // luma
1482 predict_and_reconstruct_intra_block(
1483 cm, xd, r, mbmi, 0, row_y * tx_sz_y, col_y * tx_sz_y, tx_log2_y);
1484 // chroma
1485 if (tu_idx_c < tu_num_c) {
1486 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1487 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1488 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c,
1489 col_c, tx_log2_c);
1490 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c,
1491 col_c, tx_log2_c);
1492 tu_idx_c++;
1493 }
1494 }
1495 }
1496
1497 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1498 while (tu_idx_c < tu_num_c) {
1499 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1500 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1501 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c, col_c,
1502 tx_log2_c);
1503 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c, col_c,
1504 tx_log2_c);
1505 tu_idx_c++;
1506 }
1507 } else {
1508 // Prediction
David Barkerac37fa32016-12-02 12:30:21 +00001509 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001510 AOMMAX(bsize, BLOCK_8X8));
1511
1512 // Reconstruction
1513 if (!mbmi->skip) {
1514 int eobtotal = 0;
1515 int tu_idx_c = 0;
1516 int row_y, col_y, row_c, col_c;
1517
1518 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1519 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1520 // luma
1521 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 0,
1522 row_y * tx_sz_y,
1523 col_y * tx_sz_y, tx_log2_y);
1524 // chroma
1525 if (tu_idx_c < tu_num_c) {
1526 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1527 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1528 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1529 1, row_c, col_c, tx_log2_c);
1530 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1531 2, row_c, col_c, tx_log2_c);
1532 tu_idx_c++;
1533 }
1534 }
1535 }
1536
1537 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1538 while (tu_idx_c < tu_num_c) {
1539 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1540 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1541 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 1,
1542 row_c, col_c, tx_log2_c);
1543 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 2,
1544 row_c, col_c, tx_log2_c);
1545 tu_idx_c++;
1546 }
1547
1548 if (bsize >= BLOCK_8X8 && eobtotal == 0)
1549#if CONFIG_MISC_FIXES
1550 mbmi->has_no_coeffs = 1;
1551#else
1552 mbmi->skip = 1;
1553#endif
1554 }
1555 }
1556 }
1557#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001558 if (!is_inter_block(mbmi)) {
1559 int plane;
Urvang Joshib100db72016-10-12 16:28:56 -07001560#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001561 for (plane = 0; plane <= 1; ++plane) {
1562 if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xuf883b422016-08-30 14:01:10 -07001563 av1_decode_palette_tokens(xd, plane, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001564 }
Urvang Joshib100db72016-10-12 16:28:56 -07001565#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001566 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1567 const struct macroblockd_plane *const pd = &xd->plane[plane];
Debargha Mukherjee2f123402016-08-30 17:43:38 -07001568 const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han2d64f122016-10-21 12:44:29 -07001569 const int stepr = tx_size_high_unit[tx_size];
1570 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han41bb3392016-12-14 10:46:48 -08001571#if CONFIG_CB4X4
1572 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1573#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001574 const BLOCK_SIZE plane_bsize =
1575 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001576#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001577 int row, col;
Jingning Hanbafee8d2016-12-02 10:25:03 -08001578 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1579 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001580
1581 for (row = 0; row < max_blocks_high; row += stepr)
1582 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001583 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane, row, col,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001584 tx_size);
1585 }
1586 } else {
Yue Chen69f18e12016-09-08 14:48:15 -07001587// Prediction
1588#if CONFIG_WARPED_MOTION
1589 if (mbmi->motion_mode == WARPED_CAUSAL) {
1590 int i;
1591#if CONFIG_AOM_HIGHBITDEPTH
1592 int use_hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH;
1593#endif // CONFIG_AOM_HIGHBITDEPTH
1594
1595 for (i = 0; i < 3; ++i) {
1596 const struct macroblockd_plane *pd = &xd->plane[i];
1597
1598 av1_warp_plane(&mbmi->wm_params[0],
1599#if CONFIG_AOM_HIGHBITDEPTH
1600 xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH, xd->bd,
1601#endif // CONFIG_AOM_HIGHBITDEPTH
1602 pd->pre[0].buf0, pd->pre[0].width, pd->pre[0].height,
1603 pd->pre[0].stride, pd->dst.buf,
1604 ((mi_col * MI_SIZE) >> pd->subsampling_x),
1605 ((mi_row * MI_SIZE) >> pd->subsampling_y),
Jingning Hanff6ee6a2016-12-07 09:55:21 -08001606 xd->n8_w * (MI_SIZE >> pd->subsampling_x),
1607 xd->n8_h * (MI_SIZE >> pd->subsampling_y),
1608 pd->dst.stride, pd->subsampling_x, pd->subsampling_y, 16,
1609 16, 0);
Yue Chen69f18e12016-09-08 14:48:15 -07001610 }
1611 } else {
1612#endif // CONFIG_WARPED_MOTION
Jingning Han41bb3392016-12-14 10:46:48 -08001613#if CONFIG_CB4X4
1614 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL, bsize);
1615#else
1616 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
1617 AOMMAX(bsize, BLOCK_8X8));
1618#endif
Yue Chen69f18e12016-09-08 14:48:15 -07001619#if CONFIG_WARPED_MOTION
1620 }
1621#endif // CONFIG_WARPED_MOTION
Yue Chencb60b182016-10-13 15:18:22 -07001622#if CONFIG_MOTION_VAR
1623 if (mbmi->motion_mode == OBMC_CAUSAL) {
Yue Chen894fcce2016-10-21 16:50:52 -07001624 av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001625 }
Yue Chencb60b182016-10-13 15:18:22 -07001626#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001627
1628 // Reconstruction
1629 if (!mbmi->skip) {
1630 int eobtotal = 0;
1631 int plane;
1632
1633 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1634 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han41bb3392016-12-14 10:46:48 -08001635#if CONFIG_CB4X4
1636 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1637#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001638 const BLOCK_SIZE plane_bsize =
1639 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001640#endif
Jingning Hanbafee8d2016-12-02 10:25:03 -08001641 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1642 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001643 int row, col;
1644#if CONFIG_VAR_TX
Jingning Han70e5f3f2016-11-09 17:03:07 -08001645 const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07001646 const int bh_var_tx = tx_size_high_unit[max_tx_size];
1647 const int bw_var_tx = tx_size_wide_unit[max_tx_size];
Jingning Hanfe45b212016-11-22 10:30:23 -08001648
Jingning Hanbafee8d2016-12-02 10:25:03 -08001649 for (row = 0; row < max_blocks_high; row += bh_var_tx)
1650 for (col = 0; col < max_blocks_wide; col += bw_var_tx)
Jingning Hanfe45b212016-11-22 10:30:23 -08001651 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, row, col,
1652 max_tx_size, &eobtotal);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001653#else
1654 const TX_SIZE tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -07001655 plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han2d64f122016-10-21 12:44:29 -07001656 const int stepr = tx_size_high_unit[tx_size];
1657 const int stepc = tx_size_wide_unit[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001658 for (row = 0; row < max_blocks_high; row += stepr)
1659 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001660 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1661 plane, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001662#endif
1663 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001664 }
1665 }
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001666#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001667
Yaowu Xuf883b422016-08-30 14:01:10 -07001668 xd->corrupted |= aom_reader_has_error(r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001669}
1670
Yaowu Xuf883b422016-08-30 14:01:10 -07001671static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
1672 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001673 int has_rows, int has_cols,
Jingning Han1beb0102016-12-07 11:08:30 -08001674 BLOCK_SIZE bsize) {
1675 const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
Yaowu Xuf883b422016-08-30 14:01:10 -07001676 const aom_prob *const probs = cm->fc->partition_prob[ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001677 FRAME_COUNTS *counts = xd->counts;
1678 PARTITION_TYPE p;
1679
1680 if (has_rows && has_cols)
1681#if CONFIG_EXT_PARTITION_TYPES
1682 if (bsize <= BLOCK_8X8)
Michael Bebenita6048d052016-08-25 14:40:54 -07001683 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001684 else
Michael Bebenita6048d052016-08-25 14:40:54 -07001685 p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs,
1686 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001687#else
Nathan E. Egge9d9eb6c2016-11-16 09:44:26 -05001688#if CONFIG_EC_MULTISYMBOL
Yaowu Xuf2581a32016-10-20 13:05:47 -07001689 p = (PARTITION_TYPE)aom_read_symbol(r, cm->fc->partition_cdf[ctx],
1690 PARTITION_TYPES, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001691#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001692 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001693#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001694#endif // CONFIG_EXT_PARTITION_TYPES
1695 else if (!has_rows && has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001696 p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001697 else if (has_rows && !has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001698 p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001699 else
1700 p = PARTITION_SPLIT;
1701
1702 if (counts) ++counts->partition[ctx][p];
1703
1704 return p;
1705}
1706
1707#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001708static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
1709 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001710 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
1711 return 1;
1712 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001713 const int ctx = av1_get_skip_context(xd);
Michael Bebenita6048d052016-08-25 14:40:54 -07001714 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001715 FRAME_COUNTS *counts = xd->counts;
1716 if (counts) ++counts->skip[ctx][skip];
1717 return skip;
1718 }
1719}
1720#endif // CONFIG_SUPERTX
Yaowu Xud71be782016-10-14 08:47:03 -07001721#if CONFIG_CLPF
1722static int clpf_all_skip(const AV1_COMMON *cm, int mi_col, int mi_row,
1723 int size) {
1724 int r, c;
1725 int skip = 1;
1726 const int maxc = AOMMIN(size, cm->mi_cols - mi_col);
1727 const int maxr = AOMMIN(size, cm->mi_rows - mi_row);
1728 for (r = 0; r < maxr && skip; r++) {
1729 for (c = 0; c < maxc && skip; c++) {
1730 skip &= !!cm->mi_grid_visible[(mi_row + r) * cm->mi_stride + mi_col + c]
1731 ->mbmi.skip;
1732 }
1733 }
1734 return skip;
1735}
1736#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001737
1738// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -07001739static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001740#if CONFIG_SUPERTX
1741 int supertx_enabled,
1742#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001743 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001744 BLOCK_SIZE bsize, int n4x4_l2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001745 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001746 const int n8x8_l2 = n4x4_l2 - 1;
Jingning Hanff17e162016-12-07 17:58:18 -08001747 const int num_8x8_wh = mi_size_wide[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001748 const int hbs = num_8x8_wh >> 1;
Jingning Han41bb3392016-12-14 10:46:48 -08001749#if CONFIG_CB4X4
1750 const int unify_bsize = 1;
1751#else
1752 const int unify_bsize = 0;
1753#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001754 PARTITION_TYPE partition;
1755 BLOCK_SIZE subsize;
1756#if CONFIG_EXT_PARTITION_TYPES
1757 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1758#endif
1759 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1760 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1761#if CONFIG_SUPERTX
1762 const int read_token = !supertx_enabled;
1763 int skip = 0;
1764 TX_SIZE supertx_size = b_width_log2_lookup[bsize];
1765 const TileInfo *const tile = &xd->tile;
1766 int txfm = DCT_DCT;
1767#endif // CONFIG_SUPERTX
1768
1769 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1770
Jingning Han6dbacf22016-12-14 17:28:35 -08001771 partition = (n4x4_l2 == 0) ? PARTITION_NONE
1772 : read_partition(cm, xd, mi_row, mi_col, r,
1773 has_rows, has_cols, bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001774 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
Yushin Cho77bba8d2016-11-04 16:36:56 -07001775
1776#if CONFIG_PVQ
1777 assert(partition < PARTITION_TYPES);
1778 assert(subsize < BLOCK_SIZES);
1779#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001780#if CONFIG_SUPERTX
1781 if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
1782 bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
1783 const int supertx_context = partition_supertx_context_lookup[partition];
Michael Bebenita6048d052016-08-25 14:40:54 -07001784 supertx_enabled = aom_read(
1785 r, cm->fc->supertx_prob[supertx_context][supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001786 if (xd->counts)
1787 xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
1788#if CONFIG_VAR_TX
1789 if (supertx_enabled) xd->supertx_size = supertx_size;
1790#endif
1791 }
1792#endif // CONFIG_SUPERTX
Jingning Han41bb3392016-12-14 10:46:48 -08001793 if (!hbs && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001794 // calculate bmode block dimensions (log 2)
1795 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1796 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1797 decode_block(pbi, xd,
1798#if CONFIG_SUPERTX
1799 supertx_enabled,
1800#endif // CONFIG_SUPERTX
1801 mi_row, mi_col, r,
1802#if CONFIG_EXT_PARTITION_TYPES
1803 partition,
1804#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001805 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001806 } else {
1807 switch (partition) {
1808 case PARTITION_NONE:
1809 decode_block(pbi, xd,
1810#if CONFIG_SUPERTX
1811 supertx_enabled,
1812#endif // CONFIG_SUPERTX
1813 mi_row, mi_col, r,
1814#if CONFIG_EXT_PARTITION_TYPES
1815 partition,
1816#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001817 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001818 break;
1819 case PARTITION_HORZ:
1820 decode_block(pbi, xd,
1821#if CONFIG_SUPERTX
1822 supertx_enabled,
1823#endif // CONFIG_SUPERTX
1824 mi_row, mi_col, r,
1825#if CONFIG_EXT_PARTITION_TYPES
1826 partition,
1827#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001828 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001829 if (has_rows)
1830 decode_block(pbi, xd,
1831#if CONFIG_SUPERTX
1832 supertx_enabled,
1833#endif // CONFIG_SUPERTX
1834 mi_row + hbs, mi_col, r,
1835#if CONFIG_EXT_PARTITION_TYPES
1836 partition,
1837#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001838 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001839 break;
1840 case PARTITION_VERT:
1841 decode_block(pbi, xd,
1842#if CONFIG_SUPERTX
1843 supertx_enabled,
1844#endif // CONFIG_SUPERTX
1845 mi_row, mi_col, r,
1846#if CONFIG_EXT_PARTITION_TYPES
1847 partition,
1848#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001849 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001850 if (has_cols)
1851 decode_block(pbi, xd,
1852#if CONFIG_SUPERTX
1853 supertx_enabled,
1854#endif // CONFIG_SUPERTX
1855 mi_row, mi_col + hbs, r,
1856#if CONFIG_EXT_PARTITION_TYPES
1857 partition,
1858#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001859 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001860 break;
1861 case PARTITION_SPLIT:
1862 decode_partition(pbi, xd,
1863#if CONFIG_SUPERTX
1864 supertx_enabled,
1865#endif // CONFIG_SUPERTX
1866 mi_row, mi_col, r, subsize, n8x8_l2);
1867 decode_partition(pbi, xd,
1868#if CONFIG_SUPERTX
1869 supertx_enabled,
1870#endif // CONFIG_SUPERTX
1871 mi_row, mi_col + hbs, r, subsize, n8x8_l2);
1872 decode_partition(pbi, xd,
1873#if CONFIG_SUPERTX
1874 supertx_enabled,
1875#endif // CONFIG_SUPERTX
1876 mi_row + hbs, mi_col, r, subsize, n8x8_l2);
1877 decode_partition(pbi, xd,
1878#if CONFIG_SUPERTX
1879 supertx_enabled,
1880#endif // CONFIG_SUPERTX
1881 mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
1882 break;
1883#if CONFIG_EXT_PARTITION_TYPES
1884 case PARTITION_HORZ_A:
1885 decode_block(pbi, xd,
1886#if CONFIG_SUPERTX
1887 supertx_enabled,
1888#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001889 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001890 decode_block(pbi, xd,
1891#if CONFIG_SUPERTX
1892 supertx_enabled,
1893#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001894 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001895 decode_block(pbi, xd,
1896#if CONFIG_SUPERTX
1897 supertx_enabled,
1898#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001899 mi_row + hbs, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001900 break;
1901 case PARTITION_HORZ_B:
1902 decode_block(pbi, xd,
1903#if CONFIG_SUPERTX
1904 supertx_enabled,
1905#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001906 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001907 decode_block(pbi, xd,
1908#if CONFIG_SUPERTX
1909 supertx_enabled,
1910#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001911 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001912 decode_block(pbi, xd,
1913#if CONFIG_SUPERTX
1914 supertx_enabled,
1915#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001916 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001917 break;
1918 case PARTITION_VERT_A:
1919 decode_block(pbi, xd,
1920#if CONFIG_SUPERTX
1921 supertx_enabled,
1922#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001923 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001924 decode_block(pbi, xd,
1925#if CONFIG_SUPERTX
1926 supertx_enabled,
1927#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001928 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001929 decode_block(pbi, xd,
1930#if CONFIG_SUPERTX
1931 supertx_enabled,
1932#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001933 mi_row, mi_col + hbs, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001934 break;
1935 case PARTITION_VERT_B:
1936 decode_block(pbi, xd,
1937#if CONFIG_SUPERTX
1938 supertx_enabled,
1939#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001940 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001941 decode_block(pbi, xd,
1942#if CONFIG_SUPERTX
1943 supertx_enabled,
1944#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001945 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001946 decode_block(pbi, xd,
1947#if CONFIG_SUPERTX
1948 supertx_enabled,
1949#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001950 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001951 break;
1952#endif
1953 default: assert(0 && "Invalid partition type");
1954 }
1955 }
1956
1957#if CONFIG_SUPERTX
1958 if (supertx_enabled && read_token) {
1959 uint8_t *dst_buf[3];
1960 int dst_stride[3], i;
1961 int offset = mi_row * cm->mi_stride + mi_col;
1962
1963 set_segment_id_supertx(cm, mi_row, mi_col, bsize);
1964
1965 xd->mi = cm->mi_grid_visible + offset;
1966 xd->mi[0] = cm->mi + offset;
1967 set_mi_row_col(xd, tile, mi_row, num_8x8_blocks_high_lookup[bsize], mi_col,
1968 num_8x8_blocks_wide_lookup[bsize], cm->mi_rows, cm->mi_cols);
1969 set_skip_context(xd, mi_row, mi_col);
1970 skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
1971 if (skip) {
1972 reset_skip_context(xd, bsize);
1973 } else {
1974#if CONFIG_EXT_TX
1975 if (get_ext_tx_types(supertx_size, bsize, 1) > 1) {
1976 int eset = get_ext_tx_set(supertx_size, bsize, 1);
1977 if (eset > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001978 txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
Michael Bebenita6048d052016-08-25 14:40:54 -07001979 cm->fc->inter_ext_tx_prob[eset][supertx_size],
1980 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001981 if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
1982 }
1983 }
1984#else
1985 if (supertx_size < TX_32X32) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001986 txfm = aom_read_tree(r, av1_ext_tx_tree,
Jingning Han8f6eb182016-10-19 13:48:57 -07001987 cm->fc->inter_ext_tx_prob[supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001988 if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
1989 }
1990#endif // CONFIG_EXT_TX
1991 }
1992
Yaowu Xuf883b422016-08-30 14:01:10 -07001993 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001994 for (i = 0; i < MAX_MB_PLANE; i++) {
1995 dst_buf[i] = xd->plane[i].dst.buf;
1996 dst_stride[i] = xd->plane[i].dst.stride;
1997 }
1998 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
1999 bsize, dst_buf, dst_stride);
2000
2001 if (!skip) {
2002 int eobtotal = 0;
2003 MB_MODE_INFO *mbmi;
2004 set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
2005 mbmi = &xd->mi[0]->mbmi;
2006 mbmi->tx_type = txfm;
2007 assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
2008 for (i = 0; i < MAX_MB_PLANE; ++i) {
2009 const struct macroblockd_plane *const pd = &xd->plane[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002010 int row, col;
Debargha Mukherjee2f123402016-08-30 17:43:38 -07002011 const TX_SIZE tx_size = i ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han32b20282016-10-28 15:42:44 -07002012 const int stepr = tx_size_high_unit[tx_size];
2013 const int stepc = tx_size_wide_unit[tx_size];
2014 int max_blocks_wide =
2015 pd->width + (xd->mb_to_right_edge >= 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07002016 ? 0
Jingning Han32b20282016-10-28 15:42:44 -07002017 : xd->mb_to_right_edge >> (3 + pd->subsampling_x));
2018 int max_blocks_high =
2019 pd->height +
Yaowu Xuc27fc142016-08-22 16:08:15 -07002020 (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >>
Jingning Han32b20282016-10-28 15:42:44 -07002021 (3 + pd->subsampling_y));
2022
2023 max_blocks_wide >>= tx_size_wide_log2[0];
2024 max_blocks_high >>= tx_size_wide_log2[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002025
2026 for (row = 0; row < max_blocks_high; row += stepr)
2027 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07002028 eobtotal += reconstruct_inter_block(
2029 cm, xd, r, mbmi->segment_id_supertx, i, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002030 }
2031 if (!(subsize < BLOCK_8X8) && eobtotal == 0) skip = 1;
2032 }
2033 set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
2034 }
2035#endif // CONFIG_SUPERTX
2036
2037#if CONFIG_EXT_PARTITION_TYPES
2038 if (bsize >= BLOCK_8X8) {
2039 switch (partition) {
2040 case PARTITION_SPLIT:
2041 if (bsize > BLOCK_8X8) break;
2042 case PARTITION_NONE:
2043 case PARTITION_HORZ:
2044 case PARTITION_VERT:
2045 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
2046 break;
2047 case PARTITION_HORZ_A:
2048 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2049 update_partition_context(xd, mi_row + hbs, mi_col, subsize, subsize);
2050 break;
2051 case PARTITION_HORZ_B:
2052 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2053 update_partition_context(xd, mi_row + hbs, mi_col, bsize2, subsize);
2054 break;
2055 case PARTITION_VERT_A:
2056 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2057 update_partition_context(xd, mi_row, mi_col + hbs, subsize, subsize);
2058 break;
2059 case PARTITION_VERT_B:
2060 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2061 update_partition_context(xd, mi_row, mi_col + hbs, bsize2, subsize);
2062 break;
2063 default: assert(0 && "Invalid partition type");
2064 }
2065 }
2066#else
2067 // update partition context
2068 if (bsize >= BLOCK_8X8 &&
2069 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
Jingning Han1beb0102016-12-07 11:08:30 -08002070 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
David Barkerf8935c92016-10-26 14:54:06 +01002071#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xud71be782016-10-14 08:47:03 -07002072
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002073#if CONFIG_DERING
David Barker9739f362016-11-10 09:29:32 +00002074#if CONFIG_EXT_PARTITION
2075 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128) {
2076 if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
2077 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2078 aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
2079 } else {
2080 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2081 0;
2082 }
2083 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64) {
2084#else
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002085 if (bsize == BLOCK_64X64) {
David Barker9739f362016-11-10 09:29:32 +00002086#endif
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002087 if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
2088 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2089 aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
2090 } else {
2091 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2092 0;
2093 }
2094 }
2095#endif
2096
Yaowu Xud71be782016-10-14 08:47:03 -07002097#if CONFIG_CLPF
David Barker9739f362016-11-10 09:29:32 +00002098#if CONFIG_EXT_PARTITION
2099 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128 &&
2100 cm->clpf_strength_y && cm->clpf_size != CLPF_NOSIZE) {
2101 const int tl = mi_row * MI_SIZE / MIN_FB_SIZE * cm->clpf_stride +
2102 mi_col * MI_SIZE / MIN_FB_SIZE;
2103 if (cm->clpf_size == CLPF_128X128) {
2104 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
2105 } else if (cm->clpf_size == CLPF_64X64) {
2106 const int tr = tl + 2;
2107 const int bl = tl + 2 * cm->clpf_stride;
2108 const int br = tr + 2 * cm->clpf_stride;
2109 const int size = 64 / MI_SIZE;
2110
2111 // Up to four bits per SB
2112 if (!clpf_all_skip(cm, mi_col, mi_row, size))
2113 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
2114
2115 if (mi_col + size < cm->mi_cols &&
2116 !clpf_all_skip(cm, mi_col + size, mi_row, size))
2117 cm->clpf_blocks[tr] = aom_read_literal(r, 1, ACCT_STR);
2118
2119 if (mi_row + size < cm->mi_rows &&
2120 !clpf_all_skip(cm, mi_col, mi_row + size, size))
2121 cm->clpf_blocks[bl] = aom_read_literal(r, 1, ACCT_STR);
2122
2123 if (mi_col + size < cm->mi_cols && mi_row + size < cm->mi_rows &&
2124 !clpf_all_skip(cm, mi_col + size, mi_row + size, size))
2125 cm->clpf_blocks[br] = aom_read_literal(r, 1, ACCT_STR);
2126 } else if (cm->clpf_size == CLPF_32X32) {
2127 int i, j;
2128 const int size = 32 / MI_SIZE;
2129 for (i = 0; i < 4; ++i)
2130 for (j = 0; j < 4; ++j) {
2131 const int index = tl + i * cm->clpf_stride + j;
2132 if (mi_row + i * size < cm->mi_rows &&
2133 mi_col + j * size < cm->mi_cols &&
2134 !clpf_all_skip(cm, mi_col + j * size, mi_row + i * size, size))
2135 cm->clpf_blocks[index] = aom_read_literal(r, 1, ACCT_STR);
2136 }
2137 }
2138 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64 &&
2139#else
2140 if (bsize == BLOCK_64X64 &&
2141#endif // CONFIG_EXT_PARTITION
2142 cm->clpf_strength_y && cm->clpf_size != CLPF_NOSIZE) {
Yaowu Xud71be782016-10-14 08:47:03 -07002143 const int tl = mi_row * MI_SIZE / MIN_FB_SIZE * cm->clpf_stride +
2144 mi_col * MI_SIZE / MIN_FB_SIZE;
2145
2146 if (!((mi_row * MI_SIZE) & 127) && !((mi_col * MI_SIZE) & 127) &&
2147 cm->clpf_size == CLPF_128X128) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002148 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002149 } else if (cm->clpf_size == CLPF_64X64 &&
2150 !clpf_all_skip(cm, mi_col, mi_row, 64 / MI_SIZE)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002151 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002152 } else if (cm->clpf_size == CLPF_32X32) {
2153 const int tr = tl + 1;
2154 const int bl = tl + cm->clpf_stride;
2155 const int br = tr + cm->clpf_stride;
2156 const int size = 32 / MI_SIZE;
2157
2158 // Up to four bits per SB
2159 if (!clpf_all_skip(cm, mi_col, mi_row, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002160 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002161
2162 if (mi_col + size < cm->mi_cols &&
2163 !clpf_all_skip(cm, mi_col + size, mi_row, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002164 cm->clpf_blocks[tr] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002165
2166 if (mi_row + size < cm->mi_rows &&
2167 !clpf_all_skip(cm, mi_col, mi_row + size, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002168 cm->clpf_blocks[bl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002169
2170 if (mi_col + size < cm->mi_cols && mi_row + size < cm->mi_rows &&
2171 !clpf_all_skip(cm, mi_col + size, mi_row + size, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002172 cm->clpf_blocks[br] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002173 }
2174 }
David Barker9739f362016-11-10 09:29:32 +00002175#endif // CONFIG_CLPF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002176}
2177
Yaowu Xuc27fc142016-08-22 16:08:15 -07002178static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
2179 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002180 struct aom_internal_error_info *error_info,
2181 aom_reader *r, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002182 void *decrypt_state) {
2183 // Validate the calculated partition length. If the buffer
2184 // described by the partition can't be fully read, then restrict
2185 // it to the portion that can be (for EC mode) or throw an error.
2186 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002187 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002188 "Truncated packet or corrupt tile length");
2189
Alex Converse2cdf0d82016-12-13 13:53:09 -08002190 if (aom_reader_init(r, data, read_size,
2191#if CONFIG_ANS && ANS_MAX_SYMBOLS
2192 ANS_MAX_SYMBOLS,
2193#endif
2194 decrypt_cb, decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07002195 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002196 "Failed to allocate bool decoder %d", 1);
2197}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002198
Yushin Cho77bba8d2016-11-04 16:36:56 -07002199#if !CONFIG_PVQ
Yaowu Xuf883b422016-08-30 14:01:10 -07002200static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
2201 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002202 int i, j, k, l, m;
Thomas9ac55082016-09-23 18:04:17 +01002203#if CONFIG_EC_ADAPT
Thomas Davies09ebbfb2016-10-20 18:28:47 +01002204 const int node_limit = UNCONSTRAINED_NODES - 1;
Thomas9ac55082016-09-23 18:04:17 +01002205#else
2206 const int node_limit = UNCONSTRAINED_NODES;
2207#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002208
Michael Bebenita6048d052016-08-25 14:40:54 -07002209 if (aom_read_bit(r, ACCT_STR))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002210 for (i = 0; i < PLANE_TYPES; ++i)
2211 for (j = 0; j < REF_TYPES; ++j)
2212 for (k = 0; k < COEF_BANDS; ++k)
2213 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
Thomas9ac55082016-09-23 18:04:17 +01002214 for (m = 0; m < node_limit; ++m)
Michael Bebenita6048d052016-08-25 14:40:54 -07002215 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002216}
2217
Yaowu Xuf883b422016-08-30 14:01:10 -07002218static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002219 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
2220 TX_SIZE tx_size;
Jingning Han83630632016-12-16 11:27:25 -08002221 for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002222 read_coef_probs_common(fc->coef_probs[tx_size], r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002223}
Yushin Cho77bba8d2016-11-04 16:36:56 -07002224#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002225
Yaowu Xuf883b422016-08-30 14:01:10 -07002226static void setup_segmentation(AV1_COMMON *const cm,
2227 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002228 struct segmentation *const seg = &cm->seg;
2229 int i, j;
2230
2231 seg->update_map = 0;
2232 seg->update_data = 0;
2233
Yaowu Xuf883b422016-08-30 14:01:10 -07002234 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002235 if (!seg->enabled) return;
2236
2237 // Segmentation map update
2238 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2239 seg->update_map = 1;
2240 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002241 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002242 }
2243 if (seg->update_map) {
2244 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2245 seg->temporal_update = 0;
2246 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002247 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002248 }
2249 }
2250
2251 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07002252 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002253 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002254 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002255
Yaowu Xuf883b422016-08-30 14:01:10 -07002256 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002257
2258 for (i = 0; i < MAX_SEGMENTS; i++) {
2259 for (j = 0; j < SEG_LVL_MAX; j++) {
2260 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002261 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002262 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002263 av1_enable_segfeature(seg, i, j);
2264 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
2265 if (av1_is_segfeature_signed(j))
2266 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002267 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002268 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002269 }
2270 }
2271 }
2272}
2273
2274#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002275static void decode_restoration_mode(AV1_COMMON *cm,
2276 struct aom_read_bit_buffer *rb) {
2277 RestorationInfo *rsi = &cm->rst_info;
2278 if (aom_rb_read_bit(rb)) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002279 if (aom_rb_read_bit(rb))
2280 rsi->frame_restoration_type =
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002281 (aom_rb_read_bit(rb) ? RESTORE_DOMAINTXFMRF : RESTORE_SGRPROJ);
Debargha Mukherjee0e67b252016-12-08 09:22:44 -08002282 else
2283 rsi->frame_restoration_type = RESTORE_WIENER;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002284 } else {
2285 rsi->frame_restoration_type =
2286 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
2287 }
2288}
2289
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002290static void read_wiener_filter(WienerInfo *wiener_info, aom_reader *rb) {
2291 wiener_info->vfilter[0] =
2292 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2293 WIENER_FILT_TAP0_MINV;
2294 wiener_info->vfilter[1] =
2295 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2296 WIENER_FILT_TAP1_MINV;
2297 wiener_info->vfilter[2] =
2298 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2299 WIENER_FILT_TAP2_MINV;
2300 wiener_info->hfilter[0] =
2301 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2302 WIENER_FILT_TAP0_MINV;
2303 wiener_info->hfilter[1] =
2304 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2305 WIENER_FILT_TAP1_MINV;
2306 wiener_info->hfilter[2] =
2307 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2308 WIENER_FILT_TAP2_MINV;
2309}
2310
2311static void read_sgrproj_filter(SgrprojInfo *sgrproj_info, aom_reader *rb) {
2312 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
2313 sgrproj_info->xqd[0] =
2314 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN0;
2315 sgrproj_info->xqd[1] =
2316 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN1;
2317}
2318
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002319static void read_domaintxfmrf_filter(DomaintxfmrfInfo *domaintxfmrf_info,
2320 aom_reader *rb) {
2321 domaintxfmrf_info->sigma_r =
2322 aom_read_literal(rb, DOMAINTXFMRF_PARAMS_BITS, ACCT_STR);
2323}
2324
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002325static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002326 int i;
2327 RestorationInfo *rsi = &cm->rst_info;
clang-formatbda8d612016-09-19 15:55:46 -07002328 const int ntiles =
2329 av1_get_rest_ntiles(cm->width, cm->height, NULL, NULL, NULL, NULL);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002330 if (rsi->frame_restoration_type != RESTORE_NONE) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002331 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002332 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002333 rsi->restoration_type[i] =
2334 aom_read_tree(rb, av1_switchable_restore_tree,
2335 cm->fc->switchable_restore_prob, ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002336 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07002337 rsi->wiener_info[i].level = 1;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002338 read_wiener_filter(&rsi->wiener_info[i], rb);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002339 } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
2340 rsi->sgrproj_info[i].level = 1;
2341 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002342 } else if (rsi->restoration_type[i] == RESTORE_DOMAINTXFMRF) {
2343 rsi->domaintxfmrf_info[i].level = 1;
2344 read_domaintxfmrf_filter(&rsi->domaintxfmrf_info[i], rb);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002345 }
2346 }
2347 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002348 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002349 if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002350 rsi->restoration_type[i] = RESTORE_WIENER;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002351 rsi->wiener_info[i].level = 1;
2352 read_wiener_filter(&rsi->wiener_info[i], rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002353 } else {
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07002354 rsi->wiener_info[i].level = 0;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002355 rsi->restoration_type[i] = RESTORE_NONE;
2356 }
2357 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002358 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002359 for (i = 0; i < ntiles; ++i) {
2360 if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
2361 rsi->restoration_type[i] = RESTORE_SGRPROJ;
2362 rsi->sgrproj_info[i].level = 1;
2363 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
2364 } else {
2365 rsi->sgrproj_info[i].level = 0;
2366 rsi->restoration_type[i] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002367 }
2368 }
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002369 } else if (rsi->frame_restoration_type == RESTORE_DOMAINTXFMRF) {
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002370 for (i = 0; i < ntiles; ++i) {
2371 if (aom_read(rb, RESTORE_NONE_DOMAINTXFMRF_PROB, ACCT_STR)) {
2372 rsi->restoration_type[i] = RESTORE_DOMAINTXFMRF;
2373 rsi->domaintxfmrf_info[i].level = 1;
2374 read_domaintxfmrf_filter(&rsi->domaintxfmrf_info[i], rb);
2375 } else {
2376 rsi->domaintxfmrf_info[i].level = 0;
2377 rsi->restoration_type[i] = RESTORE_NONE;
2378 }
2379 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002380 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002381 }
2382}
2383#endif // CONFIG_LOOP_RESTORATION
2384
Yaowu Xuf883b422016-08-30 14:01:10 -07002385static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002386 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07002387 lf->filter_level = aom_rb_read_literal(rb, 6);
2388 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002389
2390 // Read in loop filter deltas applied at the MB level based on mode or ref
2391 // frame.
2392 lf->mode_ref_delta_update = 0;
2393
Yaowu Xuf883b422016-08-30 14:01:10 -07002394 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002395 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002396 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002397 if (lf->mode_ref_delta_update) {
2398 int i;
2399
2400 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002401 if (aom_rb_read_bit(rb))
2402 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002403
2404 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002405 if (aom_rb_read_bit(rb))
2406 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002407 }
2408 }
2409}
2410
2411#if CONFIG_CLPF
Yaowu Xud71be782016-10-14 08:47:03 -07002412static void setup_clpf(AV1Decoder *pbi, struct aom_read_bit_buffer *rb) {
2413 AV1_COMMON *const cm = &pbi->common;
2414 const int width = pbi->cur_buf->buf.y_crop_width;
2415 const int height = pbi->cur_buf->buf.y_crop_height;
2416
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002417 cm->clpf_blocks = 0;
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02002418 cm->clpf_strength_y = aom_rb_read_literal(rb, 2);
2419 cm->clpf_strength_u = aom_rb_read_literal(rb, 2);
2420 cm->clpf_strength_v = aom_rb_read_literal(rb, 2);
2421 if (cm->clpf_strength_y) {
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002422 cm->clpf_size = aom_rb_read_literal(rb, 2);
Yaowu Xud71be782016-10-14 08:47:03 -07002423 if (cm->clpf_size != CLPF_NOSIZE) {
2424 int size;
2425 cm->clpf_stride =
2426 ((width + MIN_FB_SIZE - 1) & ~(MIN_FB_SIZE - 1)) >> MIN_FB_SIZE_LOG2;
2427 size =
2428 cm->clpf_stride * ((height + MIN_FB_SIZE - 1) & ~(MIN_FB_SIZE - 1)) >>
2429 MIN_FB_SIZE_LOG2;
2430 CHECK_MEM_ERROR(cm, cm->clpf_blocks, aom_malloc(size));
2431 memset(cm->clpf_blocks, -1, size);
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002432 }
2433 }
2434}
2435
Steinar Midtskogen2fd70ee2016-09-02 10:02:30 +02002436static int clpf_bit(UNUSED int k, UNUSED int l,
2437 UNUSED const YV12_BUFFER_CONFIG *rec,
2438 UNUSED const YV12_BUFFER_CONFIG *org,
2439 UNUSED const AV1_COMMON *cm, UNUSED int block_size,
2440 UNUSED int w, UNUSED int h, UNUSED unsigned int strength,
Yaowu Xud71be782016-10-14 08:47:03 -07002441 UNUSED unsigned int fb_size_log2, int8_t *bit) {
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002442 return *bit;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002443}
2444#endif
2445
2446#if CONFIG_DERING
Yaowu Xuf883b422016-08-30 14:01:10 -07002447static void setup_dering(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
2448 cm->dering_level = aom_rb_read_literal(rb, DERING_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002449}
2450#endif // CONFIG_DERING
2451
Yaowu Xuf883b422016-08-30 14:01:10 -07002452static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2453 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002454}
2455
Yaowu Xuf883b422016-08-30 14:01:10 -07002456static void setup_quantization(AV1_COMMON *const cm,
2457 struct aom_read_bit_buffer *rb) {
2458 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002459 cm->y_dc_delta_q = read_delta_q(rb);
2460 cm->uv_dc_delta_q = read_delta_q(rb);
2461 cm->uv_ac_delta_q = read_delta_q(rb);
2462 cm->dequant_bit_depth = cm->bit_depth;
2463#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002464 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002465 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002466 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2467 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002468 } else {
2469 cm->min_qmlevel = 0;
2470 cm->max_qmlevel = 0;
2471 }
2472#endif
2473}
2474
Yaowu Xuf883b422016-08-30 14:01:10 -07002475static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002476 // Build y/uv dequant values based on segmentation.
2477 int i = 0;
2478#if CONFIG_AOM_QM
2479 int lossless;
2480 int j = 0;
2481 int qmlevel;
2482 int using_qm = cm->using_qmatrix;
2483 int minqm = cm->min_qmlevel;
2484 int maxqm = cm->max_qmlevel;
2485#endif
2486#if CONFIG_NEW_QUANT
2487 int b;
2488 int dq;
2489#endif // CONFIG_NEW_QUANT
2490 if (cm->seg.enabled) {
2491 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002492 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002493 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002494 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2495 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002496 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002497 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002498 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002499 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002500#if CONFIG_AOM_QM
2501 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2502 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2503 // NB: depends on base index so there is only 1 set per frame
2504 // No quant weighting when lossless or signalled not using QM
2505 qmlevel = (lossless || using_qm == 0)
2506 ? NUM_QM_LEVELS - 1
2507 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2508 for (j = 0; j < TX_SIZES; ++j) {
2509 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2510 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2511 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2512 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2513 }
2514#endif // CONFIG_AOM_QM
2515#if CONFIG_NEW_QUANT
2516 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2517 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002518 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002519 cm->y_dequant_nuq[i][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002520 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002521 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002522 }
2523 }
2524#endif // CONFIG_NEW_QUANT
2525 }
2526 } else {
2527 const int qindex = cm->base_qindex;
2528 // When segmentation is disabled, only the first value is used. The
2529 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002530 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2531 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002532 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002533 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002534 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002535 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002536#if CONFIG_AOM_QM
2537 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2538 cm->uv_ac_delta_q == 0;
2539 // No quant weighting when lossless or signalled not using QM
2540 qmlevel = (lossless || using_qm == 0)
2541 ? NUM_QM_LEVELS - 1
2542 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2543 for (j = 0; j < TX_SIZES; ++j) {
2544 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2545 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2546 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2547 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2548 }
2549#endif
2550#if CONFIG_NEW_QUANT
2551 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2552 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002553 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002554 cm->y_dequant_nuq[0][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002555 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002556 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002557 }
2558 }
2559#endif // CONFIG_NEW_QUANT
2560 }
2561}
2562
Angie Chiang5678ad92016-11-21 09:38:40 -08002563static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002564 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07002565 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002566}
2567
Yaowu Xuf883b422016-08-30 14:01:10 -07002568static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002569 cm->render_width = cm->width;
2570 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002571 if (aom_rb_read_bit(rb))
2572 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002573}
2574
Yaowu Xuf883b422016-08-30 14:01:10 -07002575static void resize_mv_buffer(AV1_COMMON *cm) {
2576 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002577 cm->cur_frame->mi_rows = cm->mi_rows;
2578 cm->cur_frame->mi_cols = cm->mi_cols;
2579 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002580 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002581 sizeof(*cm->cur_frame->mvs)));
2582}
2583
Yaowu Xuf883b422016-08-30 14:01:10 -07002584static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002585#if CONFIG_SIZE_LIMIT
2586 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002587 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002588 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2589 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2590#endif
2591 if (cm->width != width || cm->height != height) {
2592 const int new_mi_rows =
2593 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2594 const int new_mi_cols =
2595 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2596
Yaowu Xuf883b422016-08-30 14:01:10 -07002597 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002598 // dimensions as well as the overall size.
2599 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002600 if (av1_alloc_context_buffers(cm, width, height))
2601 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002602 "Failed to allocate context buffers");
2603 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002604 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002605 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002606 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002607 cm->width = width;
2608 cm->height = height;
2609 }
2610 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2611 cm->mi_cols > cm->cur_frame->mi_cols) {
2612 resize_mv_buffer(cm);
2613 }
2614}
2615
Yaowu Xuf883b422016-08-30 14:01:10 -07002616static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002617 int width, height;
2618 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002619 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002620 resize_context_buffers(cm, width, height);
2621 setup_render_size(cm, rb);
2622
2623 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002624 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002625 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2626 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002627#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002628 cm->use_highbitdepth,
2629#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002630 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002631 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2632 pool->cb_priv)) {
2633 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002634 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002635 "Failed to allocate frame buffer");
2636 }
2637 unlock_buffer_pool(pool);
2638
2639 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2640 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2641 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2642 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2643 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2644 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2645 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2646}
2647
Yaowu Xuf883b422016-08-30 14:01:10 -07002648static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002649 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002650 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002651 int this_xss, int this_yss) {
2652 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2653 ref_yss == this_yss;
2654}
2655
Yaowu Xuf883b422016-08-30 14:01:10 -07002656static void setup_frame_size_with_refs(AV1_COMMON *cm,
2657 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002658 int width, height;
2659 int found = 0, i;
2660 int has_valid_ref_frame = 0;
2661 BufferPool *const pool = cm->buffer_pool;
2662 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002663 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002664 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2665 width = buf->y_crop_width;
2666 height = buf->y_crop_height;
2667 cm->render_width = buf->render_width;
2668 cm->render_height = buf->render_height;
2669 found = 1;
2670 break;
2671 }
2672 }
2673
2674 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002675 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002676 setup_render_size(cm, rb);
2677 }
2678
2679 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002680 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002681 "Invalid frame size");
2682
2683 // Check to make sure at least one of frames that this frame references
2684 // has valid dimensions.
2685 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2686 RefBuffer *const ref_frame = &cm->frame_refs[i];
2687 has_valid_ref_frame |=
2688 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2689 ref_frame->buf->y_crop_height, width, height);
2690 }
2691 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002692 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002693 "Referenced frame has invalid size");
2694 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2695 RefBuffer *const ref_frame = &cm->frame_refs[i];
2696 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
2697 ref_frame->buf->subsampling_x,
2698 ref_frame->buf->subsampling_y, cm->bit_depth,
2699 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07002700 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002701 "Referenced frame has incompatible color format");
2702 }
2703
2704 resize_context_buffers(cm, width, height);
2705
2706 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002707 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002708 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2709 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002710#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002711 cm->use_highbitdepth,
2712#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002713 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002714 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2715 pool->cb_priv)) {
2716 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002717 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002718 "Failed to allocate frame buffer");
2719 }
2720 unlock_buffer_pool(pool);
2721
2722 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2723 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2724 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2725 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2726 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2727 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2728 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2729}
2730
Yaowu Xuf883b422016-08-30 14:01:10 -07002731static void read_tile_info(AV1Decoder *const pbi,
2732 struct aom_read_bit_buffer *const rb) {
2733 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002734#if CONFIG_EXT_TILE
2735// Read the tile width/height
2736#if CONFIG_EXT_PARTITION
2737 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002738 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
2739 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002740 } else
2741#endif // CONFIG_EXT_PARTITION
2742 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002743 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
2744 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002745 }
2746
2747 cm->tile_width <<= cm->mib_size_log2;
2748 cm->tile_height <<= cm->mib_size_log2;
2749
Yaowu Xuf883b422016-08-30 14:01:10 -07002750 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
2751 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002752
2753 // Get the number of tiles
2754 cm->tile_cols = 1;
2755 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
2756
2757 cm->tile_rows = 1;
2758 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
2759
2760 if (cm->tile_cols * cm->tile_rows > 1) {
2761 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07002762 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2763 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002764 }
2765#else
2766 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07002767 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002768
2769 // columns
2770 max_ones = max_log2_tile_cols - min_log2_tile_cols;
2771 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07002772 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002773
2774 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07002775 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002776 "Invalid number of tile columns");
2777
2778 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07002779 cm->log2_tile_rows = aom_rb_read_bit(rb);
2780 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002781
2782 cm->tile_cols = 1 << cm->log2_tile_cols;
2783 cm->tile_rows = 1 << cm->log2_tile_rows;
2784
2785 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
2786 cm->tile_width >>= cm->log2_tile_cols;
2787 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
2788 cm->tile_height >>= cm->log2_tile_rows;
2789
2790 // round to integer multiples of superblock size
2791 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
2792 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
2793
Thomas Davies4974e522016-11-07 17:44:05 +00002794// tile size magnitude
2795#if !CONFIG_TILE_GROUPS
2796 if (cm->tile_rows > 1 || cm->tile_cols > 1)
2797#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002798 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002799#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00002800
Thomas Davies80188d12016-10-26 16:08:35 -07002801#if CONFIG_TILE_GROUPS
2802 // Store an index to the location of the tile group information
2803 pbi->tg_size_bit_offset = rb->bit_offset;
2804 pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
2805 if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
2806 pbi->tg_start =
2807 aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
2808 pbi->tg_size =
2809 1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
2810 }
2811#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002812}
2813
2814static int mem_get_varsize(const uint8_t *src, const int sz) {
2815 switch (sz) {
2816 case 1: return src[0];
2817 case 2: return mem_get_le16(src);
2818 case 3: return mem_get_le24(src);
2819 case 4: return mem_get_le32(src);
2820 default: assert("Invalid size" && 0); return -1;
2821 }
2822}
2823
2824#if CONFIG_EXT_TILE
2825// Reads the next tile returning its size and adjusting '*data' accordingly
2826// based on 'is_last'.
2827static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07002828 struct aom_internal_error_info *error_info,
2829 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002830 void *decrypt_state,
2831 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
2832 int tile_size_bytes, int col, int row) {
2833 size_t size;
2834
2835 size_t copy_size = 0;
2836 const uint8_t *copy_data = NULL;
2837
2838 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002839 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002840 "Truncated packet or corrupt tile length");
2841 if (decrypt_cb) {
2842 uint8_t be_data[4];
2843 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
2844
2845 // Only read number of bytes in cm->tile_size_bytes.
2846 size = mem_get_varsize(be_data, tile_size_bytes);
2847 } else {
2848 size = mem_get_varsize(*data, tile_size_bytes);
2849 }
2850
2851 // The top bit indicates copy mode
2852 if ((size >> (tile_size_bytes * 8 - 1)) == 1) {
2853 // The remaining bits in the top byte signal the row offset
2854 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
2855
2856 // Currently, only use tiles in same column as reference tiles.
2857 copy_data = tile_buffers[row - offset][col].data;
2858 copy_size = tile_buffers[row - offset][col].size;
2859 size = 0;
2860 }
2861
2862 *data += tile_size_bytes;
2863
2864 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07002865 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002866 "Truncated packet or corrupt tile size");
2867
2868 if (size > 0) {
2869 tile_buffers[row][col].data = *data;
2870 tile_buffers[row][col].size = size;
2871 } else {
2872 tile_buffers[row][col].data = copy_data;
2873 tile_buffers[row][col].size = copy_size;
2874 }
2875
2876 *data += size;
2877
2878 tile_buffers[row][col].raw_data_end = *data;
2879}
2880
2881static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07002882 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002883 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002884 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002885 const int tile_cols = cm->tile_cols;
2886 const int tile_rows = cm->tile_rows;
2887 const int have_tiles = tile_cols * tile_rows > 1;
2888
2889 if (!have_tiles) {
2890 const uint32_t tile_size = data_end - data;
2891 tile_buffers[0][0].data = data;
2892 tile_buffers[0][0].size = tile_size;
2893 tile_buffers[0][0].raw_data_end = NULL;
2894 } else {
2895 // We locate only the tile buffers that are required, which are the ones
2896 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
2897 // need the last (bottom right) tile buffer, as we need to know where the
2898 // end of the compressed frame buffer is for proper superframe decoding.
2899
2900 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
2901 const uint8_t *const data_start = data;
2902
Yaowu Xuf883b422016-08-30 14:01:10 -07002903 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002904 const int single_row = pbi->dec_tile_row >= 0;
2905 const int tile_rows_start = single_row ? dec_tile_row : 0;
2906 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07002907 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002908 const int single_col = pbi->dec_tile_col >= 0;
2909 const int tile_cols_start = single_col ? dec_tile_col : 0;
2910 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
2911
2912 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
2913 const int tile_size_bytes = pbi->tile_size_bytes;
2914
2915 size_t tile_col_size;
2916 int r, c;
2917
2918 // Read tile column sizes for all columns (we need the last tile buffer)
2919 for (c = 0; c < tile_cols; ++c) {
2920 const int is_last = c == tile_cols - 1;
2921 if (!is_last) {
2922 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
2923 data += tile_col_size_bytes;
2924 tile_col_data_end[c] = data + tile_col_size;
2925 } else {
2926 tile_col_size = data_end - data;
2927 tile_col_data_end[c] = data_end;
2928 }
2929 data += tile_col_size;
2930 }
2931
2932 data = data_start;
2933
2934 // Read the required tile sizes.
2935 for (c = tile_cols_start; c < tile_cols_end; ++c) {
2936 const int is_last = c == tile_cols - 1;
2937
2938 if (c > 0) data = tile_col_data_end[c - 1];
2939
2940 if (!is_last) data += tile_col_size_bytes;
2941
2942 // Get the whole of the last column, otherwise stop at the required tile.
2943 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
2944 tile_buffers[r][c].col = c;
2945
2946 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
2947 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
2948 tile_size_bytes, c, r);
2949 }
2950 }
2951
2952 // If we have not read the last column, then read it to get the last tile.
2953 if (tile_cols_end != tile_cols) {
2954 c = tile_cols - 1;
2955
2956 data = tile_col_data_end[c - 1];
2957
2958 for (r = 0; r < tile_rows; ++r) {
2959 tile_buffers[r][c].col = c;
2960
2961 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
2962 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
2963 tile_size_bytes, c, r);
2964 }
2965 }
2966 }
2967}
2968#else
2969// Reads the next tile returning its size and adjusting '*data' accordingly
2970// based on 'is_last'.
2971static void get_tile_buffer(const uint8_t *const data_end,
2972 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07002973 struct aom_internal_error_info *error_info,
2974 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002975 void *decrypt_state, TileBufferDec *const buf) {
2976 size_t size;
2977
2978 if (!is_last) {
2979 if (!read_is_valid(*data, 4, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002980 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002981 "Truncated packet or corrupt tile length");
2982
2983 if (decrypt_cb) {
2984 uint8_t be_data[4];
2985 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
2986 size = mem_get_varsize(be_data, tile_size_bytes);
2987 } else {
2988 size = mem_get_varsize(*data, tile_size_bytes);
2989 }
2990 *data += tile_size_bytes;
2991
2992 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07002993 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002994 "Truncated packet or corrupt tile size");
2995 } else {
2996 size = data_end - *data;
2997 }
2998
2999 buf->data = *data;
3000 buf->size = size;
3001
3002 *data += size;
3003}
3004
3005static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003006 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003007 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003008 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07003009#if CONFIG_TILE_GROUPS
3010 int r, c;
3011 const int tile_cols = cm->tile_cols;
3012 const int tile_rows = cm->tile_rows;
3013 int tc = 0;
3014 int first_tile_in_tg = 0;
3015 int hdr_offset;
3016 struct aom_read_bit_buffer rb_tg_hdr;
3017 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3018 const int num_tiles = tile_rows * tile_cols;
3019 const int num_bits = OD_ILOG(num_tiles) - 1;
3020 const int hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
3021 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
3022
3023 for (r = 0; r < tile_rows; ++r) {
3024 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07003025 TileBufferDec *const buf = &tile_buffers[r][c];
3026 hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
3027
3028 buf->col = c;
3029 if (hdr_offset) {
3030 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
3031 rb_tg_hdr.bit_offset = tg_size_bit_offset;
3032 if (num_tiles) {
3033 pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
3034 pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
3035 }
3036 }
3037 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
3038 data += hdr_offset;
Thomas Davies8fe64a32016-10-04 13:19:31 +01003039 get_tile_buffer(data_end, pbi->tile_size_bytes, 0, &pbi->common.error,
3040 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
Thomas Davies80188d12016-10-26 16:08:35 -07003041 }
3042 }
3043#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003044 int r, c;
3045 const int tile_cols = cm->tile_cols;
3046 const int tile_rows = cm->tile_rows;
3047
3048 for (r = 0; r < tile_rows; ++r) {
3049 for (c = 0; c < tile_cols; ++c) {
3050 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
3051 TileBufferDec *const buf = &tile_buffers[r][c];
3052 buf->col = c;
3053 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
3054 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
3055 }
3056 }
Thomas Davies80188d12016-10-26 16:08:35 -07003057#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003058}
3059#endif // CONFIG_EXT_TILE
3060
Yushin Cho77bba8d2016-11-04 16:36:56 -07003061#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003062static void daala_dec_init(AV1_COMMON *const cm, daala_dec_ctx *daala_dec,
3063 od_ec_dec *ec) {
Yushin Cho77bba8d2016-11-04 16:36:56 -07003064 daala_dec->ec = ec;
3065 od_adapt_ctx_reset(&daala_dec->state.adapt, 0);
3066
Yushin Cho70669122016-12-08 09:53:14 -10003067 // TODO(yushin) : activity masking info needs be signaled by a bitstream
3068 daala_dec->use_activity_masking = AV1_PVQ_ENABLE_ACTIVITY_MASKING;
3069
3070 if (daala_dec->use_activity_masking)
3071 daala_dec->qm = OD_HVS_QM;
3072 else
3073 daala_dec->qm = OD_FLAT_QM;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003074
3075 od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
3076 daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
Yushin Cho70669122016-12-08 09:53:14 -10003077
3078 if (daala_dec->use_activity_masking) {
3079 int pli;
3080 int use_masking = daala_dec->use_activity_masking;
3081 int segment_id = 0;
3082 int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
3083
3084 for (pli = 0; pli < MAX_MB_PLANE; pli++) {
3085 int i;
3086 int q;
3087
3088 q = qindex;
3089 if (q <= OD_DEFAULT_QMS[use_masking][0][pli].interp_q << OD_COEFF_SHIFT) {
3090 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3091 &OD_DEFAULT_QMS[use_masking][0][pli], NULL);
3092 } else {
3093 i = 0;
3094 while (OD_DEFAULT_QMS[use_masking][i + 1][pli].qm_q4 != NULL &&
3095 q > OD_DEFAULT_QMS[use_masking][i + 1][pli].interp_q
3096 << OD_COEFF_SHIFT) {
3097 i++;
3098 }
3099 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3100 &OD_DEFAULT_QMS[use_masking][i][pli],
3101 &OD_DEFAULT_QMS[use_masking][i + 1][pli]);
3102 }
3103 }
3104 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07003105}
Yushin Cho70669122016-12-08 09:53:14 -10003106#endif // #if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003107
Yaowu Xuf883b422016-08-30 14:01:10 -07003108static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003109 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003110 AV1_COMMON *const cm = &pbi->common;
3111 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003112 const int tile_cols = cm->tile_cols;
3113 const int tile_rows = cm->tile_rows;
3114 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07003115 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003116#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003117 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003118 const int single_row = pbi->dec_tile_row >= 0;
3119 const int tile_rows_start = single_row ? dec_tile_row : 0;
3120 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003121 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003122 const int single_col = pbi->dec_tile_col >= 0;
3123 const int tile_cols_start = single_col ? dec_tile_col : 0;
3124 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3125 const int inv_col_order = pbi->inv_tile_order && !single_col;
3126 const int inv_row_order = pbi->inv_tile_order && !single_row;
3127#else
3128 const int tile_rows_start = 0;
3129 const int tile_rows_end = tile_rows;
3130 const int tile_cols_start = 0;
3131 const int tile_cols_end = tile_cols;
3132 const int inv_col_order = pbi->inv_tile_order;
3133 const int inv_row_order = pbi->inv_tile_order;
3134#endif // CONFIG_EXT_TILE
3135 int tile_row, tile_col;
3136
3137#if CONFIG_ENTROPY
3138 cm->do_subframe_update = n_tiles == 1;
3139#endif // CONFIG_ENTROPY
3140
3141 if (cm->lf.filter_level && !cm->skip_loop_filter &&
3142 pbi->lf_worker.data1 == NULL) {
3143 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07003144 aom_memalign(32, sizeof(LFWorkerData)));
3145 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003146 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003147 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003148 "Loop filter thread creation failed");
3149 }
3150 }
3151
3152 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3153 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3154 // Be sure to sync as we might be resuming after a failed frame decode.
3155 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003156 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
3157 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003158 }
3159
3160 assert(tile_rows <= MAX_TILE_ROWS);
3161 assert(tile_cols <= MAX_TILE_COLS);
3162
3163 get_tile_buffers(pbi, data, data_end, tile_buffers);
3164
3165 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003166 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003167 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003168 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003169 pbi->allocated_tiles = n_tiles;
3170 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003171#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003172 if (pbi->acct_enabled) {
3173 aom_accounting_reset(&pbi->accounting);
3174 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003175#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003176 // Load all tile information into tile_data.
3177 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3178 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3179 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
3180 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
3181
3182 td->cm = cm;
3183 td->xd = pbi->mb;
3184 td->xd.corrupted = 0;
3185 td->xd.counts =
3186 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3187 ? &cm->counts
3188 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003189 av1_zero(td->dqcoeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003190#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003191 av1_zero(td->pvq_ref_coeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003192#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003193 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003194 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
3195 &td->bit_reader, pbi->decrypt_cb, pbi->decrypt_state);
Michael Bebenita6048d052016-08-25 14:40:54 -07003196#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003197 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003198 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003199 } else {
David Barkerd971f402016-10-25 13:52:07 +01003200 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003201 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003202#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003203 av1_init_macroblockd(cm, &td->xd,
3204#if CONFIG_PVQ
3205 td->pvq_ref_coeff,
3206#endif
3207 td->dqcoeff);
3208#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003209 daala_dec_init(cm, &td->xd.daala_dec, &td->bit_reader.ec);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003210#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003211#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003212 td->xd.plane[0].color_index_map = td->color_index_map[0];
3213 td->xd.plane[1].color_index_map = td->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003214#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003215 }
3216 }
3217
3218 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3219 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
3220 int mi_row = 0;
3221 TileInfo tile_info;
3222
Yaowu Xuf883b422016-08-30 14:01:10 -07003223 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003224
3225 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3226 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
3227 TileData *const td = pbi->tile_data + tile_cols * row + col;
Michael Bebenita6048d052016-08-25 14:40:54 -07003228#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003229 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003230 td->bit_reader.accounting->last_tell_frac =
3231 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003232 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003233#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003234
Yaowu Xuf883b422016-08-30 14:01:10 -07003235 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003236
Yaowu Xuf883b422016-08-30 14:01:10 -07003237 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003238
3239 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
3240 mi_row += cm->mib_size) {
3241 int mi_col;
3242
Yaowu Xuf883b422016-08-30 14:01:10 -07003243 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003244
3245 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
3246 mi_col += cm->mib_size) {
3247 decode_partition(pbi, &td->xd,
3248#if CONFIG_SUPERTX
3249 0,
3250#endif // CONFIG_SUPERTX
3251 mi_row, mi_col, &td->bit_reader, cm->sb_size,
3252 b_width_log2_lookup[cm->sb_size]);
3253 }
3254 pbi->mb.corrupted |= td->xd.corrupted;
3255 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003256 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003257 "Failed to decode tile data");
3258#if CONFIG_ENTROPY
3259 if (cm->do_subframe_update &&
3260 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3261 if ((mi_row + MI_SIZE) %
3262 (MI_SIZE *
Yaowu Xuf883b422016-08-30 14:01:10 -07003263 AOMMAX(cm->mi_rows / MI_SIZE / COEF_PROBS_BUFS, 1)) ==
Yaowu Xuc27fc142016-08-22 16:08:15 -07003264 0 &&
3265 mi_row + MI_SIZE < cm->mi_rows &&
3266 cm->coef_probs_update_idx < COEF_PROBS_BUFS - 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003267 av1_partial_adapt_probs(cm, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003268 ++cm->coef_probs_update_idx;
3269 }
3270 }
3271#endif // CONFIG_ENTROPY
3272 }
3273 }
3274
3275 assert(mi_row > 0);
3276
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003277// when Parallel deblocking is enabled, deblocking should not
3278// be interleaved with decoding. Instead, deblocking should be done
3279// after the entire frame is decoded.
3280#if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003281 // Loopfilter one tile row.
3282 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3283 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003284 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003285 const int lf_end = tile_info.mi_row_end - cm->mib_size;
3286
3287 // Delay the loopfilter if the first tile row is only
3288 // a single superblock high.
3289 if (lf_end <= 0) continue;
3290
3291 // Decoding has completed. Finish up the loop filter in this thread.
3292 if (tile_info.mi_row_end >= cm->mi_rows) continue;
3293
3294 winterface->sync(&pbi->lf_worker);
3295 lf_data->start = lf_start;
3296 lf_data->stop = lf_end;
3297 if (pbi->max_threads > 1) {
3298 winterface->launch(&pbi->lf_worker);
3299 } else {
3300 winterface->execute(&pbi->lf_worker);
3301 }
3302 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003303#endif // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003304
3305 // After loopfiltering, the last 7 row pixels in each superblock row may
3306 // still be changed by the longest loopfilter of the next superblock row.
3307 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003308 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003309 }
3310
3311#if CONFIG_VAR_TX
3312 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003313 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
3314 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003315#else
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003316#if CONFIG_PARALLEL_DEBLOCKING
3317 // Loopfilter all rows in the frame in the frame.
3318 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3319 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3320 winterface->sync(&pbi->lf_worker);
3321 lf_data->start = 0;
3322 lf_data->stop = cm->mi_rows;
3323 winterface->execute(&pbi->lf_worker);
3324 }
3325#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003326 // Loopfilter remaining rows in the frame.
3327 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3328 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3329 winterface->sync(&pbi->lf_worker);
3330 lf_data->start = lf_data->stop;
3331 lf_data->stop = cm->mi_rows;
3332 winterface->execute(&pbi->lf_worker);
3333 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003334#endif // CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003335#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003336 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003337 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003338
3339#if CONFIG_EXT_TILE
3340 if (n_tiles == 1) {
3341#if CONFIG_ANS
3342 return data_end;
3343#else
3344 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07003345 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003346#endif // CONFIG_ANS
3347 } else {
3348 // Return the end of the last tile buffer
3349 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3350 }
3351#else
3352#if CONFIG_ANS
3353 return data_end;
3354#else
3355 {
3356 // Get last tile data.
3357 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003358 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003359 }
3360#endif // CONFIG_ANS
3361#endif // CONFIG_EXT_TILE
3362}
3363
3364static int tile_worker_hook(TileWorkerData *const tile_data,
3365 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003366 AV1Decoder *const pbi = tile_data->pbi;
3367 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003368 int mi_row, mi_col;
3369
3370 if (setjmp(tile_data->error_info.jmp)) {
3371 tile_data->error_info.setjmp = 0;
3372 tile_data->xd.corrupted = 1;
3373 return 0;
3374 }
3375
3376 tile_data->error_info.setjmp = 1;
3377 tile_data->xd.error_info = &tile_data->error_info;
3378
Yaowu Xuf883b422016-08-30 14:01:10 -07003379 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003380
3381 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
3382 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003383 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003384
3385 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3386 mi_col += cm->mib_size) {
3387 decode_partition(pbi, &tile_data->xd,
3388#if CONFIG_SUPERTX
3389 0,
3390#endif
3391 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
3392 b_width_log2_lookup[cm->sb_size]);
3393 }
3394 }
3395 return !tile_data->xd.corrupted;
3396}
3397
3398// sorts in descending order
3399static int compare_tile_buffers(const void *a, const void *b) {
3400 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
3401 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
3402 return (int)(buf2->size - buf1->size);
3403}
3404
Yaowu Xuf883b422016-08-30 14:01:10 -07003405static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003406 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003407 AV1_COMMON *const cm = &pbi->common;
3408 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003409 const int tile_cols = cm->tile_cols;
3410 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003411 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
clang-format67948d32016-09-07 22:40:40 -07003412 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003413#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003414 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003415 const int single_row = pbi->dec_tile_row >= 0;
3416 const int tile_rows_start = single_row ? dec_tile_row : 0;
3417 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003418 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003419 const int single_col = pbi->dec_tile_col >= 0;
3420 const int tile_cols_start = single_col ? dec_tile_col : 0;
3421 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3422#else
3423 const int tile_rows_start = 0;
3424 const int tile_rows_end = tile_rows;
3425 const int tile_cols_start = 0;
3426 const int tile_cols_end = tile_cols;
3427#endif // CONFIG_EXT_TILE
3428 int tile_row, tile_col;
3429 int i;
3430
3431#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3432 int final_worker = -1;
3433#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3434
3435 assert(tile_rows <= MAX_TILE_ROWS);
3436 assert(tile_cols <= MAX_TILE_COLS);
3437
3438 assert(tile_cols * tile_rows > 1);
3439
Yaowu Xuc27fc142016-08-22 16:08:15 -07003440 // TODO(jzern): See if we can remove the restriction of passing in max
3441 // threads to the decoder.
3442 if (pbi->num_tile_workers == 0) {
3443 const int num_threads = pbi->max_threads & ~1;
3444 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07003445 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003446 // Ensure tile data offsets will be properly aligned. This may fail on
3447 // platforms without DECLARE_ALIGNED().
3448 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3449 CHECK_MEM_ERROR(
3450 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003451 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003452 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07003453 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003454 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003455 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003456 ++pbi->num_tile_workers;
3457
3458 winterface->init(worker);
3459 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003460 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003461 "Tile decoder thread creation failed");
3462 }
3463 }
3464 }
3465
3466 // Reset tile decoding hook
3467 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003468 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003469 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003470 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003471 worker->data1 = &pbi->tile_worker_data[i];
3472 worker->data2 = &pbi->tile_worker_info[i];
3473 }
3474
3475 // Initialize thread frame counts.
3476 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3477 for (i = 0; i < num_workers; ++i) {
3478 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003479 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003480 }
3481 }
3482
3483 // Load tile data into tile_buffers
3484 get_tile_buffers(pbi, data, data_end, tile_buffers);
3485
3486 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3487 // Sort the buffers in this tile row based on size in descending order.
3488 qsort(&tile_buffers[tile_row][tile_cols_start],
3489 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
3490 compare_tile_buffers);
3491
3492 // Rearrange the tile buffers in this tile row such that per-tile group
3493 // the largest, and presumably the most difficult tile will be decoded in
3494 // the main thread. This should help minimize the number of instances
3495 // where the main thread is waiting for a worker to complete.
3496 {
3497 int group_start;
3498 for (group_start = tile_cols_start; group_start < tile_cols_end;
3499 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003500 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003501 const TileBufferDec largest = tile_buffers[tile_row][group_start];
3502 memmove(&tile_buffers[tile_row][group_start],
3503 &tile_buffers[tile_row][group_start + 1],
3504 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
3505 tile_buffers[tile_row][group_end - 1] = largest;
3506 }
3507 }
3508
3509 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
3510 // Launch workers for individual columns
3511 for (i = 0; i < num_workers && tile_col < tile_cols_end;
3512 ++i, ++tile_col) {
3513 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07003514 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003515 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
3516 TileInfo *const tile_info = (TileInfo *)worker->data2;
3517
3518 twd->pbi = pbi;
3519 twd->xd = pbi->mb;
3520 twd->xd.corrupted = 0;
3521 twd->xd.counts =
3522 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3523 ? &twd->counts
3524 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003525 av1_zero(twd->dqcoeff);
3526 av1_tile_init(tile_info, cm, tile_row, buf->col);
3527 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003528 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
3529 &twd->bit_reader, pbi->decrypt_cb,
3530 pbi->decrypt_state);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003531 av1_init_macroblockd(cm, &twd->xd,
3532#if CONFIG_PVQ
3533 twd->pvq_ref_coeff,
3534#endif
3535 twd->dqcoeff);
3536#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003537 daala_dec_init(cm, &twd->xd.daala_dec, &twd->bit_reader.ec);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003538#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003539#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003540 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
3541 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003542#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003543
3544 worker->had_error = 0;
3545 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
3546 winterface->execute(worker);
3547 } else {
3548 winterface->launch(worker);
3549 }
3550
3551#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3552 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
3553 final_worker = i;
3554 }
3555#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3556 }
3557
3558 // Sync all workers
3559 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003560 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003561 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07003562 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07003563 // info in cm. Additionally once the threads have been synced and an
3564 // error is detected, there's no point in continuing to decode tiles.
3565 pbi->mb.corrupted |= !winterface->sync(worker);
3566 }
3567 }
3568 }
3569
3570 // Accumulate thread frame counts.
3571 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3572 for (i = 0; i < num_workers; ++i) {
3573 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003574 av1_accumulate_frame_counts(cm, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003575 }
3576 }
3577
3578#if CONFIG_EXT_TILE
3579 // Return the end of the last tile buffer
3580 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3581#else
3582#if CONFIG_ANS
3583 return data_end;
3584#else
3585 assert(final_worker != -1);
3586 {
3587 TileWorkerData *const twd =
3588 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003589 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003590 }
3591#endif // CONFIG_ANS
3592#endif // CONFIG_EXT_TILE
3593}
3594
3595static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003596 AV1_COMMON *const cm = (AV1_COMMON *)data;
3597 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003598}
3599
Yaowu Xuf883b422016-08-30 14:01:10 -07003600static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3601 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003602 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003603 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
3604#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003605 cm->use_highbitdepth = 1;
3606#endif
3607 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003608 cm->bit_depth = AOM_BITS_8;
3609#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003610 cm->use_highbitdepth = 0;
3611#endif
3612 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003613 cm->color_space = aom_rb_read_literal(rb, 3);
3614 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003615 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07003616 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003617 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003618 cm->subsampling_x = aom_rb_read_bit(rb);
3619 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003620 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003621 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003622 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07003623 if (aom_rb_read_bit(rb))
3624 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003625 "Reserved bit set");
3626 } else {
3627 cm->subsampling_y = cm->subsampling_x = 1;
3628 }
3629 } else {
3630 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
3631 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
3632 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
3633 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003634 if (aom_rb_read_bit(rb))
3635 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003636 "Reserved bit set");
3637 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003638 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003639 "4:4:4 color not supported in profile 0 or 2");
3640 }
3641 }
3642}
3643
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003644#if CONFIG_REFERENCE_BUFFER
3645void read_sequence_header(SequenceHeader *seq_params) {
3646 /* Placeholder for actually reading from the bitstream */
3647 seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
3648 seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
3649 seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
3650}
3651#endif
3652
Yaowu Xuf883b422016-08-30 14:01:10 -07003653static size_t read_uncompressed_header(AV1Decoder *pbi,
3654 struct aom_read_bit_buffer *rb) {
3655 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003656 MACROBLOCKD *const xd = &pbi->mb;
3657 BufferPool *const pool = cm->buffer_pool;
3658 RefCntBuffer *const frame_bufs = pool->frame_bufs;
3659 int i, mask, ref_index = 0;
3660 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003661
3662#if CONFIG_REFERENCE_BUFFER
3663 /* TODO: Move outside frame loop or inside key-frame branch */
3664 read_sequence_header(&pbi->seq_params);
3665#endif
3666
Yaowu Xuc27fc142016-08-22 16:08:15 -07003667 cm->last_frame_type = cm->frame_type;
3668 cm->last_intra_only = cm->intra_only;
3669
3670#if CONFIG_EXT_REFS
3671 // NOTE: By default all coded frames to be used as a reference
3672 cm->is_reference_frame = 1;
3673#endif // CONFIG_EXT_REFS
3674
Yaowu Xuf883b422016-08-30 14:01:10 -07003675 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
3676 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003677 "Invalid frame marker");
3678
Yaowu Xuf883b422016-08-30 14:01:10 -07003679 cm->profile = av1_read_profile(rb);
3680#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003681 if (cm->profile >= MAX_PROFILES)
Yaowu Xuf883b422016-08-30 14:01:10 -07003682 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003683 "Unsupported bitstream profile");
3684#else
3685 if (cm->profile >= PROFILE_2)
Yaowu Xuf883b422016-08-30 14:01:10 -07003686 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003687 "Unsupported bitstream profile");
3688#endif
3689
Yaowu Xuf883b422016-08-30 14:01:10 -07003690 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003691
3692 if (cm->show_existing_frame) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003693// Show an existing frame directly.
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003694#if CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003695 const int existing_frame_idx = aom_rb_read_literal(rb, 3);
3696 const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003697 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003698 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3699 int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003700 /* Compare display_frame_id with ref_frame_id and check valid for
3701 * referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003702 if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
3703 cm->valid_for_referencing[existing_frame_idx] == 0)
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003704 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3705 "Reference buffer frame ID mismatch");
3706 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003707#else
3708 const int frame_to_show = cm->ref_frame_map[aom_rb_read_literal(rb, 3)];
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003709#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003710 lock_buffer_pool(pool);
3711 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
3712 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07003713 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003714 "Buffer %d does not contain a decoded frame",
3715 frame_to_show);
3716 }
3717 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
3718 unlock_buffer_pool(pool);
3719
3720 cm->lf.filter_level = 0;
3721 cm->show_frame = 1;
3722 pbi->refresh_frame_flags = 0;
3723
3724 if (cm->frame_parallel_decode) {
3725 for (i = 0; i < REF_FRAMES; ++i)
3726 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
3727 }
3728
3729 return 0;
3730 }
3731
Yaowu Xuf883b422016-08-30 14:01:10 -07003732 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
3733 cm->show_frame = aom_rb_read_bit(rb);
3734 cm->error_resilient_mode = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003735#if CONFIG_REFERENCE_BUFFER
3736 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003737 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3738 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
3739 int prev_frame_id = 0;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003740 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003741 prev_frame_id = cm->current_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003742 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003743 cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003744
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003745 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003746 int diff_frame_id;
3747 if (cm->current_frame_id > prev_frame_id) {
3748 diff_frame_id = cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003749 } else {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003750 diff_frame_id =
3751 (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003752 }
3753 /* Check current_frame_id for conformance */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003754 if (prev_frame_id == cm->current_frame_id ||
3755 diff_frame_id >= (1 << (frame_id_length - 1))) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003756 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3757 "Invalid value of current_frame_id");
3758 }
3759 }
3760 /* Check if some frames need to be marked as not valid for referencing */
3761 for (i = 0; i < REF_FRAMES; i++) {
3762 if (cm->frame_type == KEY_FRAME) {
3763 cm->valid_for_referencing[i] = 0;
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003764 } else if (cm->current_frame_id - (1 << diff_len) > 0) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003765 if (cm->ref_frame_id[i] > cm->current_frame_id ||
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003766 cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003767 cm->valid_for_referencing[i] = 0;
3768 } else {
3769 if (cm->ref_frame_id[i] > cm->current_frame_id &&
3770 cm->ref_frame_id[i] <
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003771 (1 << frame_id_length) + cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003772 cm->valid_for_referencing[i] = 0;
3773 }
3774 }
3775 }
3776#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003777 if (cm->frame_type == KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003778 if (!av1_read_sync_code(rb))
3779 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003780 "Invalid frame sync code");
3781
3782 read_bitdepth_colorspace_sampling(cm, rb);
3783 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
3784
3785 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3786 cm->frame_refs[i].idx = INVALID_IDX;
3787 cm->frame_refs[i].buf = NULL;
3788 }
3789
3790 setup_frame_size(cm, rb);
3791 if (pbi->need_resync) {
3792 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
3793 pbi->need_resync = 0;
3794 }
Urvang Joshib100db72016-10-12 16:28:56 -07003795#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07003796 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003797#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003798 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003799 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003800#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07003801 if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003802#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003803 if (cm->error_resilient_mode) {
3804 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
3805 } else {
3806 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003807 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003808 ? RESET_FRAME_CONTEXT_ALL
3809 : RESET_FRAME_CONTEXT_CURRENT;
3810 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003811 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003812 ? RESET_FRAME_CONTEXT_CURRENT
3813 : RESET_FRAME_CONTEXT_NONE;
3814 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07003815 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003816 ? RESET_FRAME_CONTEXT_ALL
3817 : RESET_FRAME_CONTEXT_CURRENT;
3818 }
3819 }
3820
3821 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003822 if (!av1_read_sync_code(rb))
3823 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003824 "Invalid frame sync code");
3825
3826 read_bitdepth_colorspace_sampling(cm, rb);
3827
Yaowu Xuf883b422016-08-30 14:01:10 -07003828 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003829 setup_frame_size(cm, rb);
3830 if (pbi->need_resync) {
3831 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
3832 pbi->need_resync = 0;
3833 }
3834 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Yaowu Xuf883b422016-08-30 14:01:10 -07003835 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003836
3837#if CONFIG_EXT_REFS
3838 if (!pbi->refresh_frame_flags) {
3839 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
3840 // will not be used as a reference
3841 cm->is_reference_frame = 0;
3842 }
3843#endif // CONFIG_EXT_REFS
3844
3845 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003846 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003847 const int idx = cm->ref_frame_map[ref];
3848 RefBuffer *const ref_frame = &cm->frame_refs[i];
3849 ref_frame->idx = idx;
3850 ref_frame->buf = &frame_bufs[idx].buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07003851 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003852#if CONFIG_REFERENCE_BUFFER
3853 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003854 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3855 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
3856 int delta_frame_id_minus1 = aom_rb_read_literal(rb, diff_len);
3857 int ref_frame_id =
3858 ((cm->current_frame_id - (delta_frame_id_minus1 + 1) +
3859 (1 << frame_id_length)) %
3860 (1 << frame_id_length));
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003861 /* Compare values derived from delta_frame_id_minus1 and
3862 * refresh_frame_flags. Also, check valid for referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003863 if (ref_frame_id != cm->ref_frame_id[ref] ||
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003864 cm->valid_for_referencing[ref] == 0)
3865 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3866 "Reference buffer frame ID mismatch");
3867 }
3868#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003869 }
3870
Arild Fuldseth842e9b02016-09-02 13:00:05 +02003871#if CONFIG_FRAME_SIZE
3872 if (cm->error_resilient_mode == 0) {
3873 setup_frame_size_with_refs(cm, rb);
3874 } else {
3875 setup_frame_size(cm, rb);
3876 }
3877#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003878 setup_frame_size_with_refs(cm, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02003879#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003880
Yaowu Xuf883b422016-08-30 14:01:10 -07003881 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Angie Chiang5678ad92016-11-21 09:38:40 -08003882 cm->interp_filter = read_frame_interp_filter(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003883
3884 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3885 RefBuffer *const ref_buf = &cm->frame_refs[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07003886#if CONFIG_AOM_HIGHBITDEPTH
3887 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003888 &ref_buf->sf, ref_buf->buf->y_crop_width,
3889 ref_buf->buf->y_crop_height, cm->width, cm->height,
3890 cm->use_highbitdepth);
3891#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003892 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003893 &ref_buf->sf, ref_buf->buf->y_crop_width,
3894 ref_buf->buf->y_crop_height, cm->width, cm->height);
3895#endif
3896 }
3897 }
3898 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003899
3900#if CONFIG_REFERENCE_BUFFER
3901 if (pbi->seq_params.frame_id_numbers_present_flag) {
3902 /* If bitmask is set, update reference frame id values and
3903 mark frames as valid for reference */
3904 int refresh_frame_flags =
3905 cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
3906 for (i = 0; i < REF_FRAMES; i++) {
3907 if ((refresh_frame_flags >> i) & 1) {
3908 cm->ref_frame_id[i] = cm->current_frame_id;
3909 cm->valid_for_referencing[i] = 1;
3910 }
3911 }
3912 }
3913#endif
3914
Yaowu Xuf883b422016-08-30 14:01:10 -07003915#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003916 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
3917#endif
3918 get_frame_new_buffer(cm)->color_space = cm->color_space;
3919 get_frame_new_buffer(cm)->color_range = cm->color_range;
3920 get_frame_new_buffer(cm)->render_width = cm->render_width;
3921 get_frame_new_buffer(cm)->render_height = cm->render_height;
3922
3923 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003924 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003925 "Keyframe / intra-only frame required to reset decoder"
3926 " state");
3927 }
3928
3929 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003930 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003931 ? REFRESH_FRAME_CONTEXT_FORWARD
3932 : REFRESH_FRAME_CONTEXT_BACKWARD;
3933 } else {
3934 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
3935 }
3936
Yaowu Xuf883b422016-08-30 14:01:10 -07003937 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07003938 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07003939 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003940
3941 // Generate next_ref_frame_map.
3942 lock_buffer_pool(pool);
3943 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
3944 if (mask & 1) {
3945 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
3946 ++frame_bufs[cm->new_fb_idx].ref_count;
3947 } else {
3948 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
3949 }
3950 // Current thread holds the reference frame.
3951 if (cm->ref_frame_map[ref_index] >= 0)
3952 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
3953 ++ref_index;
3954 }
3955
3956 for (; ref_index < REF_FRAMES; ++ref_index) {
3957 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
3958
3959 // Current thread holds the reference frame.
3960 if (cm->ref_frame_map[ref_index] >= 0)
3961 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
3962 }
3963 unlock_buffer_pool(pool);
3964 pbi->hold_ref_buf = 1;
3965
3966 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003967 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003968
3969#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07003970 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003971#else
3972 set_sb_size(cm, BLOCK_64X64);
3973#endif // CONFIG_EXT_PARTITION
3974
3975 setup_loopfilter(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003976#if CONFIG_DERING
3977 setup_dering(cm, rb);
3978#endif
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02003979#if CONFIG_CLPF
3980 setup_clpf(pbi, rb);
3981#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003982#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee874d36d2016-12-14 16:53:17 -08003983 av1_alloc_restoration_buffers(cm);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07003984 decode_restoration_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003985#endif // CONFIG_LOOP_RESTORATION
3986 setup_quantization(cm, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07003987#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003988 xd->bd = (int)cm->bit_depth;
3989#endif
3990
3991#if CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07003992 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003993 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
3994 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
3995 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
3996 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
3997 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
3998 }
3999#endif // CONFIG_ENTROPY
4000
4001 setup_segmentation(cm, rb);
4002
Arild Fuldseth07441162016-08-15 15:07:52 +02004003#if CONFIG_DELTA_Q
4004 {
4005 struct segmentation *const seg = &cm->seg;
4006 int segment_quantizer_active = 0;
4007 for (i = 0; i < MAX_SEGMENTS; i++) {
4008 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
4009 segment_quantizer_active = 1;
4010 }
4011 }
4012
Thomas Daviesf6936102016-09-05 16:51:31 +01004013 cm->delta_q_res = 1;
Arild Fuldseth07441162016-08-15 15:07:52 +02004014 if (segment_quantizer_active == 0) {
4015 cm->delta_q_present_flag = aom_rb_read_bit(rb);
4016 } else {
4017 cm->delta_q_present_flag = 0;
4018 }
4019 if (cm->delta_q_present_flag) {
4020 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01004021 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Arild Fuldseth07441162016-08-15 15:07:52 +02004022 }
4023 }
4024#endif
4025
Urvang Joshi454280d2016-10-14 16:51:44 -07004026 for (i = 0; i < MAX_SEGMENTS; ++i) {
4027 const int qindex = cm->seg.enabled
4028 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
4029 : cm->base_qindex;
4030 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
4031 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4032 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004033 }
4034
4035 setup_segmentation_dequant(cm);
4036 cm->tx_mode =
4037 (!cm->seg.enabled && xd->lossless[0]) ? ONLY_4X4 : read_tx_mode(rb);
4038 cm->reference_mode = read_frame_reference_mode(cm, rb);
4039
4040 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004041 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004042
4043 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07004044 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004045 "Invalid header size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004046 return sz;
4047}
4048
4049#if CONFIG_EXT_TX
Thomas9ac55082016-09-23 18:04:17 +01004050#if !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuf883b422016-08-30 14:01:10 -07004051static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004052 int i, j, k;
4053 int s;
4054 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004055 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004056 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4057 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
4058 for (j = 0; j < num_ext_tx_set_inter[s] - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004059 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004060 }
4061 }
4062 }
4063
4064 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004065 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004066 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4067 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
4068 for (j = 0; j < INTRA_MODES; ++j)
4069 for (k = 0; k < num_ext_tx_set_intra[s] - 1; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004070 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
4071 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004072 }
4073 }
4074 }
4075}
Thomas9ac55082016-09-23 18:04:17 +01004076#endif // !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004077#else
4078
Yaowu Xuc27fc142016-08-22 16:08:15 -07004079#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004080#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07004081static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004082 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -07004083 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004084 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
4085 for (j = 1; j < TX_SIZES; ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004086 av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004087 }
4088 }
4089 }
4090}
4091#endif // CONFIG_SUPERTX
4092
4093#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +00004094static void read_global_motion_params(WarpedMotionParams *params,
Yaowu Xuf883b422016-08-30 14:01:10 -07004095 aom_prob *probs, aom_reader *r) {
David Barkercf3d0b02016-11-10 10:14:49 +00004096 TransformationType type =
Michael Bebenita6048d052016-08-25 14:40:54 -07004097 aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004098 set_default_gmparams(params);
David Barkercf3d0b02016-11-10 10:14:49 +00004099 params->wmtype = type;
4100 switch (type) {
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004101 case HOMOGRAPHY:
4102 params->wmmat[6] = aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004103 GM_ROW3HOMO_DECODE_FACTOR;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004104 params->wmmat[7] = aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004105 GM_ROW3HOMO_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004106 case AFFINE:
4107 case ROTZOOM:
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004108 params->wmmat[2] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004109 GM_ALPHA_DECODE_FACTOR +
David Barkercf3d0b02016-11-10 10:14:49 +00004110 (1 << WARPEDMODEL_PREC_BITS);
4111 params->wmmat[3] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4112 GM_ALPHA_DECODE_FACTOR;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004113 if (type == AFFINE || type == HOMOGRAPHY) {
4114 params->wmmat[4] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004115 GM_ALPHA_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004116 params->wmmat[5] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4117 GM_ALPHA_DECODE_FACTOR +
4118 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004119 } else {
David Barkercf3d0b02016-11-10 10:14:49 +00004120 params->wmmat[4] = -params->wmmat[3];
4121 params->wmmat[5] = params->wmmat[2];
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004122 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004123 // fallthrough intended
David Barkercf3d0b02016-11-10 10:14:49 +00004124 case TRANSLATION:
4125 params->wmmat[0] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4126 GM_TRANS_DECODE_FACTOR;
4127 params->wmmat[1] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4128 GM_TRANS_DECODE_FACTOR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004129 break;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004130 case IDENTITY: break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004131 default: assert(0);
4132 }
4133}
4134
Yaowu Xuf883b422016-08-30 14:01:10 -07004135static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004136 int frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004137 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
4138 read_global_motion_params(&cm->global_motion[frame],
4139 cm->fc->global_motion_types_prob, r);
Sarah Parkere5299862016-08-16 14:57:37 -07004140 /*
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004141 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4142 frame, cm->current_video_frame, cm->show_frame,
David Barkercf3d0b02016-11-10 10:14:49 +00004143 cm->global_motion[frame].wmmat[0],
4144 cm->global_motion[frame].wmmat[1],
4145 cm->global_motion[frame].wmmat[2],
4146 cm->global_motion[frame].wmmat[3]);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004147 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004148 }
4149}
4150#endif // CONFIG_GLOBAL_MOTION
4151
Yaowu Xuf883b422016-08-30 14:01:10 -07004152static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004153 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004154 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004155#if CONFIG_SUPERTX
4156 MACROBLOCKD *const xd = &pbi->mb;
4157#endif
4158 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07004159 aom_reader r;
Yaowu Xu8af861b2016-11-01 12:12:11 -07004160 int k, i;
Yaowu Xud0af64f2016-11-17 12:50:47 -08004161#if !CONFIG_EC_ADAPT || CONFIG_EXT_INTRA
Yaowu Xu8af861b2016-11-01 12:12:11 -07004162 int j;
4163#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004164
Alex Converse2cdf0d82016-12-13 13:53:09 -08004165 if (aom_reader_init(&r, data, partition_size,
4166#if CONFIG_ANS && ANS_MAX_SYMBOLS
4167 ANS_MAX_SYMBOLS,
4168#endif
4169 pbi->decrypt_cb, pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07004170 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004171 "Failed to allocate bool decoder 0");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004172
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004173#if CONFIG_LOOP_RESTORATION
4174 decode_restoration(cm, &r);
4175#endif
4176
Yaowu Xuefc75352016-10-31 09:46:42 -07004177 if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004178
Yushin Cho77bba8d2016-11-04 16:36:56 -07004179#if !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004180 read_coef_probs(fc, cm->tx_mode, &r);
4181
4182#if CONFIG_VAR_TX
4183 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004184 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
Yushin Cho77bba8d2016-11-04 16:36:56 -07004185#endif // CONFIG_VAR_TX
4186#endif // !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004187 for (k = 0; k < SKIP_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004188 av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004189
Thomas Daviesf6936102016-09-05 16:51:31 +01004190#if CONFIG_DELTA_Q
4191 for (k = 0; k < DELTA_Q_CONTEXTS; ++k)
4192 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
4193#endif
4194
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004195#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004196 if (cm->seg.enabled && cm->seg.update_map) {
4197 if (cm->seg.temporal_update) {
4198 for (k = 0; k < PREDICTION_PROBS; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004199 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004200 }
4201 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004202 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004203 }
4204
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004205 for (j = 0; j < INTRA_MODES; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004206 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004207 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004208 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004209
4210#if CONFIG_EXT_PARTITION_TYPES
4211 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004212 av1_diff_update_prob(&r, &fc->partition_prob[0][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004213 for (j = 1; j < PARTITION_CONTEXTS; ++j)
4214 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004215 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004216#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004217 for (j = 0; j < PARTITION_CONTEXTS; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004218 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004219 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004220#endif // CONFIG_EXT_PARTITION_TYPES
Thomas9ac55082016-09-23 18:04:17 +01004221#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004222#if CONFIG_EXT_INTRA
hui sueda3d762016-12-06 16:58:23 -08004223#if CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004224 for (i = 0; i < INTRA_FILTERS + 1; ++i)
4225 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004226 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
hui sueda3d762016-12-06 16:58:23 -08004227#endif // CONFIG_INTRA_INTERP
Thomas Davies6519beb2016-10-19 14:46:07 +01004228#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004229
4230 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004231 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004232#if CONFIG_DAALA_EC
4233 av1_copy(cm->kf_y_cdf, av1_kf_y_mode_cdf);
4234#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004235#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004236 for (k = 0; k < INTRA_MODES; k++)
Thomas Davies6519beb2016-10-19 14:46:07 +01004237 for (j = 0; j < INTRA_MODES; j++)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004238 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004239 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004240#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004241 } else {
4242#if !CONFIG_REF_MV
4243 nmv_context *const nmvc = &fc->nmvc;
4244#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004245 read_inter_mode_probs(fc, &r);
4246
4247#if CONFIG_EXT_INTER
4248 read_inter_compound_mode_probs(fc, &r);
4249 if (cm->reference_mode != COMPOUND_REFERENCE) {
4250 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4251 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004252 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004253 }
4254 }
4255 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4256 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004257 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004258 }
4259 for (i = 0; i < BLOCK_SIZES; i++) {
4260 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004261 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004262 }
4263 }
4264 }
4265 if (cm->reference_mode != SINGLE_REFERENCE) {
4266 for (i = 0; i < BLOCK_SIZES; i++) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08004267 for (j = 0; j < COMPOUND_TYPES - 1; j++) {
4268 av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004269 }
4270 }
4271 }
4272#endif // CONFIG_EXT_INTER
4273
Yue Chencb60b182016-10-13 15:18:22 -07004274#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004275 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
Yue Chencb60b182016-10-13 15:18:22 -07004276 for (j = 0; j < MOTION_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004277 av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004278 }
Yue Chencb60b182016-10-13 15:18:22 -07004279#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004280
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004281#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004282 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004283#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004284
4285 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004286 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004287
4288 if (cm->reference_mode != SINGLE_REFERENCE)
4289 setup_compound_reference_mode(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004290 read_frame_reference_mode_probs(cm, &r);
4291
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004292#if !CONFIG_EC_ADAPT
Nathan E. Egge5710c722016-09-08 10:01:16 -04004293 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004294 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004295 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
Nathan E. Egge5710c722016-09-08 10:01:16 -04004296 }
Thomas9ac55082016-09-23 18:04:17 +01004297#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004298
4299#if CONFIG_REF_MV
4300 for (i = 0; i < NMV_CONTEXTS; ++i)
4301 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
4302#else
4303 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
4304#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004305#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004306 read_ext_tx_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004307#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004308#if CONFIG_SUPERTX
4309 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
4310#endif
4311#if CONFIG_GLOBAL_MOTION
4312 read_global_motion(cm, &r);
Thomas Davies6519beb2016-10-19 14:46:07 +01004313#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004314 }
Thomas Davies6519beb2016-10-19 14:46:07 +01004315#if CONFIG_EC_MULTISYMBOL
4316 av1_coef_pareto_cdfs(fc);
David Barker599dfd02016-11-10 13:20:12 +00004317#if CONFIG_REF_MV
4318 for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
4319#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004320 av1_set_mv_cdfs(&fc->nmvc);
David Barker599dfd02016-11-10 13:20:12 +00004321#endif
Nathan E. Egge31296062016-11-16 09:44:26 -05004322#if CONFIG_EC_MULTISYMBOL
Thomas Davies6519beb2016-10-19 14:46:07 +01004323 av1_set_mode_cdfs(cm);
4324#endif
4325#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004326
Yaowu Xuf883b422016-08-30 14:01:10 -07004327 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004328}
4329
4330#ifdef NDEBUG
4331#define debug_check_frame_counts(cm) (void)0
4332#else // !NDEBUG
4333// Counts should only be incremented when frame_parallel_decoding_mode and
4334// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07004335static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004336 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07004337 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004338 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
4339 cm->error_resilient_mode);
4340 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
4341 sizeof(cm->counts.y_mode)));
4342 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
4343 sizeof(cm->counts.uv_mode)));
4344 assert(!memcmp(cm->counts.partition, zero_counts.partition,
4345 sizeof(cm->counts.partition)));
4346 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
4347 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
4348 sizeof(cm->counts.eob_branch)));
4349 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
4350 sizeof(cm->counts.switchable_interp)));
4351 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
4352 sizeof(cm->counts.inter_mode)));
4353#if CONFIG_EXT_INTER
4354 assert(!memcmp(cm->counts.inter_compound_mode,
4355 zero_counts.inter_compound_mode,
4356 sizeof(cm->counts.inter_compound_mode)));
4357 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
4358 sizeof(cm->counts.interintra)));
4359 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
4360 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08004361 assert(!memcmp(cm->counts.compound_interinter,
4362 zero_counts.compound_interinter,
4363 sizeof(cm->counts.compound_interinter)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004364#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07004365#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
4366 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
4367 sizeof(cm->counts.motion_mode)));
4368#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004369 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
4370 sizeof(cm->counts.intra_inter)));
4371 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
4372 sizeof(cm->counts.comp_inter)));
4373 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
4374 sizeof(cm->counts.single_ref)));
4375 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
4376 sizeof(cm->counts.comp_ref)));
4377#if CONFIG_EXT_REFS
4378 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
4379 sizeof(cm->counts.comp_bwdref)));
4380#endif // CONFIG_EXT_REFS
4381 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
4382 sizeof(cm->counts.tx_size)));
4383 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
4384#if CONFIG_REF_MV
4385 assert(
4386 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
4387 assert(
4388 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
4389#else
4390 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
4391#endif
4392 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
4393 sizeof(cm->counts.inter_ext_tx)));
4394 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
4395 sizeof(cm->counts.intra_ext_tx)));
4396}
4397#endif // NDEBUG
4398
Yaowu Xuf883b422016-08-30 14:01:10 -07004399static struct aom_read_bit_buffer *init_read_bit_buffer(
4400 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
4401 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004402 rb->bit_offset = 0;
4403 rb->error_handler = error_handler;
4404 rb->error_handler_data = &pbi->common;
4405 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004406 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004407 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
4408 rb->bit_buffer = clear_data;
4409 rb->bit_buffer_end = clear_data + n;
4410 } else {
4411 rb->bit_buffer = data;
4412 rb->bit_buffer_end = data_end;
4413 }
4414 return rb;
4415}
4416
4417//------------------------------------------------------------------------------
4418
Yaowu Xuf883b422016-08-30 14:01:10 -07004419int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
4420 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
4421 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
4422 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004423}
4424
Yaowu Xuf883b422016-08-30 14:01:10 -07004425void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
4426 int *height) {
4427 *width = aom_rb_read_literal(rb, 16) + 1;
4428 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004429}
4430
Yaowu Xuf883b422016-08-30 14:01:10 -07004431BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
4432 int profile = aom_rb_read_bit(rb);
4433 profile |= aom_rb_read_bit(rb) << 1;
4434 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004435 return (BITSTREAM_PROFILE)profile;
4436}
4437
Yaowu Xuf883b422016-08-30 14:01:10 -07004438void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
4439 const uint8_t *data_end, const uint8_t **p_data_end) {
4440 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004441 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07004442 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004443 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004444 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004445 size_t first_partition_size;
4446 YV12_BUFFER_CONFIG *new_fb;
4447
4448#if CONFIG_BITSTREAM_DEBUG
4449 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
4450#endif
4451
4452 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004453 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Thomas Davies72712e62016-11-09 12:17:51 +00004454#if CONFIG_TILE_GROUPS
4455 pbi->first_partition_size = first_partition_size;
4456 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
4457#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004458 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004459 xd->cur_buf = new_fb;
4460#if CONFIG_GLOBAL_MOTION
4461 xd->global_motion = cm->global_motion;
4462#endif // CONFIG_GLOBAL_MOTION
4463
4464 if (!first_partition_size) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004465 // showing a frame directly
4466 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004467 return;
4468 }
4469
Yaowu Xuf883b422016-08-30 14:01:10 -07004470 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004471 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07004472 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004473 "Truncated packet or corrupt header length");
4474
Jingning Han24e0a182016-11-20 22:34:12 -08004475#if CONFIG_REF_MV
Dengca8d24d2016-10-17 14:06:35 +08004476 cm->setup_mi(cm);
4477#endif
4478
Yaowu Xuc27fc142016-08-22 16:08:15 -07004479 cm->use_prev_frame_mvs =
4480 !cm->error_resilient_mode && cm->width == cm->last_width &&
4481 cm->height == cm->last_height && !cm->last_intra_only &&
4482 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
4483#if CONFIG_EXT_REFS
4484 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
4485 // show_exisiting_frame=1, nor can it take a frame not used as
4486 // a reference, it is probable that by the time it is being
4487 // referred to, the frame buffer it originally points to may
4488 // already get expired and have been reassigned to the current
4489 // newly coded frame. Hence, we need to check whether this is
4490 // the case, and if yes, we have 2 choices:
4491 // (1) Simply disable the use of previous frame mvs; or
4492 // (2) Have cm->prev_frame point to one reference frame buffer,
4493 // e.g. LAST_FRAME.
4494 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
4495 // Reassign the LAST_FRAME buffer to cm->prev_frame.
4496 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4497 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4498 }
4499#endif // CONFIG_EXT_REFS
4500
Yaowu Xuf883b422016-08-30 14:01:10 -07004501 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004502
4503 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
4504 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07004505 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004506 "Uninitialized entropy context.");
4507
Yaowu Xuf883b422016-08-30 14:01:10 -07004508 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004509
4510 xd->corrupted = 0;
4511 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
4512 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07004513 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004514 "Decode failed. Frame data header is corrupted.");
4515
4516 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004517 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004518 }
4519
4520 // If encoded in frame parallel mode, frame context is ready after decoding
4521 // the frame header.
4522 if (cm->frame_parallel_decode &&
4523 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004524 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004525 FrameWorkerData *const frame_worker_data = worker->data1;
4526 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
4527 context_updated = 1;
4528 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4529 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004530 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004531 pbi->cur_buf->row = -1;
4532 pbi->cur_buf->col = -1;
4533 frame_worker_data->frame_context_ready = 1;
4534 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07004535 av1_frameworker_signal_stats(worker);
4536 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004537 }
4538
4539#if CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004540 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004541 cm->coef_probs_update_idx = 0;
4542#endif // CONFIG_ENTROPY
4543
4544 if (pbi->max_threads > 1
4545#if CONFIG_EXT_TILE
4546 && pbi->dec_tile_col < 0 // Decoding all columns
4547#endif // CONFIG_EXT_TILE
4548 && cm->tile_cols > 1) {
4549 // Multi-threaded tile decoder
4550 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
4551 if (!xd->corrupted) {
4552 if (!cm->skip_loop_filter) {
4553 // If multiple threads are used to decode tiles, then we use those
4554 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07004555 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
4556 0, 0, pbi->tile_workers, pbi->num_tile_workers,
4557 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004558 }
4559 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004560 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004561 "Decode failed. Frame data is corrupted.");
4562 }
4563 } else {
4564 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
4565 }
4566#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee874d36d2016-12-14 16:53:17 -08004567 if (cm->rst_info.frame_restoration_type != RESTORE_NONE) {
4568 av1_loop_restoration_frame(new_fb, cm, &cm->rst_info, 0, 0, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004569 }
4570#endif // CONFIG_LOOP_RESTORATION
4571
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004572#if CONFIG_DERING
4573 if (cm->dering_level && !cm->skip_loop_filter) {
4574 av1_dering_frame(&pbi->cur_buf->buf, cm, &pbi->mb, cm->dering_level);
4575 }
4576#endif // CONFIG_DERING
4577
Thomas Daedef56859f2016-04-19 16:57:24 -07004578#if CONFIG_CLPF
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004579 if (!cm->skip_loop_filter) {
4580 const YV12_BUFFER_CONFIG *const frame = &pbi->cur_buf->buf;
4581 if (cm->clpf_strength_y) {
Yaowu Xud71be782016-10-14 08:47:03 -07004582 av1_clpf_frame(frame, NULL, cm, cm->clpf_size != CLPF_NOSIZE,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004583 cm->clpf_strength_y + (cm->clpf_strength_y == 3),
Yaowu Xud71be782016-10-14 08:47:03 -07004584 4 + cm->clpf_size, AOM_PLANE_Y, clpf_bit);
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004585 }
4586 if (cm->clpf_strength_u) {
Yaowu Xud71be782016-10-14 08:47:03 -07004587 av1_clpf_frame(frame, NULL, cm, 0, // No block signals for chroma
4588 cm->clpf_strength_u + (cm->clpf_strength_u == 3), 4,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004589 AOM_PLANE_U, NULL);
4590 }
4591 if (cm->clpf_strength_v) {
Yaowu Xud71be782016-10-14 08:47:03 -07004592 av1_clpf_frame(frame, NULL, cm, 0, // No block signals for chroma
4593 cm->clpf_strength_v + (cm->clpf_strength_v == 3), 4,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004594 AOM_PLANE_V, NULL);
4595 }
Steinar Midtskogend06588a2016-05-06 13:48:20 +02004596 }
4597 if (cm->clpf_blocks) aom_free(cm->clpf_blocks);
Thomas Daedef56859f2016-04-19 16:57:24 -07004598#endif
Thomas Daedef56859f2016-04-19 16:57:24 -07004599
Yaowu Xuc27fc142016-08-22 16:08:15 -07004600 if (!xd->corrupted) {
4601 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
4602#if CONFIG_ENTROPY
4603 cm->partial_prob_update = 0;
4604#endif // CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004605 av1_adapt_coef_probs(cm);
4606 av1_adapt_intra_frame_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004607
4608 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004609 av1_adapt_inter_frame_probs(cm);
4610 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004611 }
4612 } else {
4613 debug_check_frame_counts(cm);
4614 }
4615 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004616 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004617 "Decode failed. Frame data is corrupted.");
4618 }
4619
4620 // Non frame parallel update frame context here.
4621 if (!cm->error_resilient_mode && !context_updated)
4622 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4623}