blob: cdbbdcd71bed702eb8def2fc818550420d92c153 [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"
Jean-Marc Valin01435132017-02-18 14:12:53 -050031#if CONFIG_CDEF
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"
Jean-Marc Valin01435132017-02-18 14:12:53 -050034#include "av1/common/dering.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070035#endif
36#include "av1/common/common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070037#include "av1/common/entropy.h"
38#include "av1/common/entropymode.h"
Thomas Davies6519beb2016-10-19 14:46:07 +010039#include "av1/common/entropymv.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070040#include "av1/common/idct.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070041#include "av1/common/pred_common.h"
42#include "av1/common/quant_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070043#include "av1/common/reconinter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070044#include "av1/common/reconintra.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070045#include "av1/common/seg_common.h"
Jingning Han1aab8182016-06-03 11:09:06 -070046#include "av1/common/thread_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070047#include "av1/common/tile_common.h"
48
49#include "av1/decoder/decodeframe.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070050#include "av1/decoder/decodemv.h"
51#include "av1/decoder/decoder.h"
Jingning Han1aab8182016-06-03 11:09:06 -070052#include "av1/decoder/detokenize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070053#include "av1/decoder/dsubexp.h"
54
Yue Chen69f18e12016-09-08 14:48:15 -070055#if CONFIG_WARPED_MOTION
56#include "av1/common/warped_motion.h"
57#endif // CONFIG_WARPED_MOTION
58
Yaowu Xuf883b422016-08-30 14:01:10 -070059#define MAX_AV1_HEADER_SIZE 80
Michael Bebenita6048d052016-08-25 14:40:54 -070060#define ACCT_STR __func__
Yaowu Xuc27fc142016-08-22 16:08:15 -070061
Yushin Cho77bba8d2016-11-04 16:36:56 -070062#if CONFIG_PVQ
63#include "av1/decoder/pvq_decoder.h"
Yushin Cho70669122016-12-08 09:53:14 -100064#include "av1/common/pvq.h"
Yushin Cho77bba8d2016-11-04 16:36:56 -070065#include "av1/encoder/encodemb.h"
66
Yushin Cho77bba8d2016-11-04 16:36:56 -070067#include "av1/common/partition.h"
68#include "av1/decoder/decint.h"
69#include "av1/encoder/hybrid_fwd_txfm.h"
70#endif
71
Thomas Davies80188d12016-10-26 16:08:35 -070072static struct aom_read_bit_buffer *init_read_bit_buffer(
73 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
74 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]);
75static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
76 size_t partition_size);
77static size_t read_uncompressed_header(AV1Decoder *pbi,
78 struct aom_read_bit_buffer *rb);
79
Yaowu Xuf883b422016-08-30 14:01:10 -070080static int is_compound_reference_allowed(const AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070081 int i;
82 if (frame_is_intra_only(cm)) return 0;
83 for (i = 1; i < INTER_REFS_PER_FRAME; ++i)
84 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
85
86 return 0;
87}
88
Yaowu Xuf883b422016-08-30 14:01:10 -070089static void setup_compound_reference_mode(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070090#if CONFIG_EXT_REFS
91 cm->comp_fwd_ref[0] = LAST_FRAME;
92 cm->comp_fwd_ref[1] = LAST2_FRAME;
93 cm->comp_fwd_ref[2] = LAST3_FRAME;
94 cm->comp_fwd_ref[3] = GOLDEN_FRAME;
95
96 cm->comp_bwd_ref[0] = BWDREF_FRAME;
97 cm->comp_bwd_ref[1] = ALTREF_FRAME;
98#else
99 if (cm->ref_frame_sign_bias[LAST_FRAME] ==
100 cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
101 cm->comp_fixed_ref = ALTREF_FRAME;
102 cm->comp_var_ref[0] = LAST_FRAME;
103 cm->comp_var_ref[1] = GOLDEN_FRAME;
104 } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
105 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
106 cm->comp_fixed_ref = GOLDEN_FRAME;
107 cm->comp_var_ref[0] = LAST_FRAME;
108 cm->comp_var_ref[1] = ALTREF_FRAME;
109 } else {
110 cm->comp_fixed_ref = LAST_FRAME;
111 cm->comp_var_ref[0] = GOLDEN_FRAME;
112 cm->comp_var_ref[1] = ALTREF_FRAME;
113 }
114#endif // CONFIG_EXT_REFS
115}
116
117static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
118 return len != 0 && len <= (size_t)(end - start);
119}
120
Yaowu Xuf883b422016-08-30 14:01:10 -0700121static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
122 const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700123 return data > max ? max : data;
124}
125
Yue Cheneeacc4c2017-01-17 17:29:17 -0800126static TX_MODE read_tx_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
127 struct aom_read_bit_buffer *rb) {
128 int i, all_lossless = 1;
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800129#if CONFIG_TX64X64
Yue Cheneeacc4c2017-01-17 17:29:17 -0800130 TX_MODE tx_mode;
131#endif
132
133 if (cm->seg.enabled) {
134 for (i = 0; i < MAX_SEGMENTS; ++i) {
135 if (!xd->lossless[i]) {
136 all_lossless = 0;
137 break;
138 }
139 }
140 } else {
141 all_lossless = xd->lossless[0];
142 }
143
144 if (all_lossless) return ONLY_4X4;
145#if CONFIG_TX64X64
146 tx_mode = aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800147 if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
148 return tx_mode;
149#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700150 return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800151#endif // CONFIG_TX64X64
Yaowu Xuc27fc142016-08-22 16:08:15 -0700152}
153
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -0500154#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -0700155static void read_tx_size_probs(FRAME_CONTEXT *fc, aom_reader *r) {
156 int i, j, k;
157 for (i = 0; i < MAX_TX_DEPTH; ++i)
158 for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
159 for (k = 0; k < i + 1; ++k)
160 av1_diff_update_prob(r, &fc->tx_size_probs[i][j][k], ACCT_STR);
161}
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -0500162#endif
Yaowu Xuefc75352016-10-31 09:46:42 -0700163
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400164#if !CONFIG_EC_ADAPT
Yaowu Xuf883b422016-08-30 14:01:10 -0700165static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700166 int i, j;
Nathan E. Egge4947c292016-04-26 11:37:06 -0400167 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700168 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700169 av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i], ACCT_STR);
Nathan E. Egge4947c292016-04-26 11:37:06 -0400170 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700171}
Thomas Davies6519beb2016-10-19 14:46:07 +0100172#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700173
Yaowu Xuf883b422016-08-30 14:01:10 -0700174static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700175#if CONFIG_REF_MV
Yaowu Xu8af861b2016-11-01 12:12:11 -0700176 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700177 for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700178 av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700179 for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700180 av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700181 for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700182 av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700183 for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700184 av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700185#if CONFIG_EXT_INTER
Michael Bebenita6048d052016-08-25 14:40:54 -0700186 av1_diff_update_prob(r, &fc->new2mv_prob, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700187#endif // CONFIG_EXT_INTER
188#else
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400189#if !CONFIG_EC_ADAPT
Yaowu Xu8af861b2016-11-01 12:12:11 -0700190 int i, j;
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400191 for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700192 for (j = 0; j < INTER_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700193 av1_diff_update_prob(r, &fc->inter_mode_probs[i][j], ACCT_STR);
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400194 }
Yaowu Xu8af861b2016-11-01 12:12:11 -0700195#else
196 (void)fc;
197 (void)r;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700198#endif
Thomas Davies6519beb2016-10-19 14:46:07 +0100199#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700200}
201
202#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700203static void read_inter_compound_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700204 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -0700205 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700206 for (j = 0; j < INTER_MODE_CONTEXTS; ++j) {
207 for (i = 0; i < INTER_COMPOUND_MODES - 1; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700208 av1_diff_update_prob(r, &fc->inter_compound_mode_probs[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700209 }
210 }
211 }
212}
213#endif // CONFIG_EXT_INTER
Yaowu Xu17fd2f22016-11-17 18:23:28 -0800214#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100215#if !CONFIG_EXT_TX
216static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
217 int i, j, k;
218 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
219 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
220 for (j = 0; j < TX_TYPES; ++j) {
221 for (k = 0; k < TX_TYPES - 1; ++k)
222 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[i][j][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100223 }
224 }
225 }
226 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
227 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
228 for (k = 0; k < TX_TYPES - 1; ++k)
229 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[i][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100230 }
231 }
232}
233#endif
234#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700235
236static REFERENCE_MODE read_frame_reference_mode(
Yaowu Xuf883b422016-08-30 14:01:10 -0700237 const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700238 if (is_compound_reference_allowed(cm)) {
Zoe Liub05e5d12017-02-07 14:32:53 -0800239#if CONFIG_REF_ADAPT
240 return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
241#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700242 return aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700243 ? REFERENCE_MODE_SELECT
Yaowu Xuf883b422016-08-30 14:01:10 -0700244 : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
Zoe Liub05e5d12017-02-07 14:32:53 -0800245#endif // CONFIG_REF_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700246 } else {
247 return SINGLE_REFERENCE;
248 }
249}
250
Yaowu Xuf883b422016-08-30 14:01:10 -0700251static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700252 FRAME_CONTEXT *const fc = cm->fc;
253 int i, j;
254
255 if (cm->reference_mode == REFERENCE_MODE_SELECT)
256 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700257 av1_diff_update_prob(r, &fc->comp_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700258
259 if (cm->reference_mode != COMPOUND_REFERENCE) {
260 for (i = 0; i < REF_CONTEXTS; ++i) {
261 for (j = 0; j < (SINGLE_REFS - 1); ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700262 av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700263 }
264 }
265 }
266
267 if (cm->reference_mode != SINGLE_REFERENCE) {
268 for (i = 0; i < REF_CONTEXTS; ++i) {
269#if CONFIG_EXT_REFS
270 for (j = 0; j < (FWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700271 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700272 for (j = 0; j < (BWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700273 av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700274#else
275 for (j = 0; j < (COMP_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700276 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700277#endif // CONFIG_EXT_REFS
278 }
279 }
280}
281
Yaowu Xuf883b422016-08-30 14:01:10 -0700282static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700283 int i;
Michael Bebenita6048d052016-08-25 14:40:54 -0700284 for (i = 0; i < n; ++i) av1_diff_update_prob(r, &p[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700285}
286
Yaowu Xuf883b422016-08-30 14:01:10 -0700287static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Thomas9ac55082016-09-23 18:04:17 +0100288 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700289
Yaowu Xue86288d2016-10-31 15:56:38 -0700290#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100291 int j;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700292 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
293
Yaowu Xuc27fc142016-08-22 16:08:15 -0700294 for (i = 0; i < 2; ++i) {
295 nmv_component *const comp_ctx = &ctx->comps[i];
296 update_mv_probs(&comp_ctx->sign, 1, r);
297 update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
298 update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
299 update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
300 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700301 for (i = 0; i < 2; ++i) {
302 nmv_component *const comp_ctx = &ctx->comps[i];
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400303 for (j = 0; j < CLASS0_SIZE; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700304 update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400305 }
Nathan E. Eggeac499f32016-09-08 15:38:57 -0400306 update_mv_probs(comp_ctx->fp, MV_FP_SIZE - 1, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700307 }
Alex Converseaca9feb2016-10-10 11:08:10 -0700308#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700309
310 if (allow_hp) {
311 for (i = 0; i < 2; ++i) {
312 nmv_component *const comp_ctx = &ctx->comps[i];
313 update_mv_probs(&comp_ctx->class0_hp, 1, r);
314 update_mv_probs(&comp_ctx->hp, 1, r);
315 }
316 }
317}
318
319static void inverse_transform_block(MACROBLOCKD *xd, int plane,
320 const TX_TYPE tx_type,
321 const TX_SIZE tx_size, uint8_t *dst,
Jingning Han1be18782016-10-21 11:48:15 -0700322 int stride, int16_t scan_line, int eob) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700323 struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han1be18782016-10-21 11:48:15 -0700324 tran_low_t *const dqcoeff = pd->dqcoeff;
325 INV_TXFM_PARAM inv_txfm_param;
326 inv_txfm_param.tx_type = tx_type;
327 inv_txfm_param.tx_size = tx_size;
328 inv_txfm_param.eob = eob;
329 inv_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700330
Yaowu Xuf883b422016-08-30 14:01:10 -0700331#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han1be18782016-10-21 11:48:15 -0700332 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
333 inv_txfm_param.bd = xd->bd;
334 highbd_inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
335 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700336#endif // CONFIG_AOM_HIGHBITDEPTH
Jingning Han1be18782016-10-21 11:48:15 -0700337 inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
Yaowu Xuf883b422016-08-30 14:01:10 -0700338#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700339 }
Jingning Han1be18782016-10-21 11:48:15 -0700340#endif // CONFIG_AOM_HIGHBITDEPTH
341 memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700342}
343
Yushin Cho77bba8d2016-11-04 16:36:56 -0700344#if CONFIG_PVQ
Thomas Daede1dbda1b2017-02-06 16:06:29 -0800345static int av1_pvq_decode_helper(od_dec_ctx *dec, tran_low_t *ref_coeff,
346 tran_low_t *dqcoeff, int16_t *quant, int pli,
Yushin Cho77bba8d2016-11-04 16:36:56 -0700347 int bs, TX_TYPE tx_type, int xdec,
ltrudeaue1c09292017-01-20 15:42:13 -0500348 PVQ_SKIP_TYPE ac_dc_coded) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700349 unsigned int flags; // used for daala's stream analyzer.
350 int off;
351 const int is_keyframe = 0;
352 const int has_dc_skip = 1;
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800353 /*TODO(tterribe): Handle CONFIG_AOM_HIGHBITDEPTH.*/
354 int coeff_shift = 3 - get_tx_scale(bs);
355 int rounding_mask;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700356 // DC quantizer for PVQ
357 int pvq_dc_quant;
358 int lossless = (quant[0] == 0);
359 const int blk_size = tx_size_wide[bs];
360 int eob = 0;
361 int i;
Yushin Cho70669122016-12-08 09:53:14 -1000362 int use_activity_masking = dec->use_activity_masking;
Thomas Daede1dbda1b2017-02-06 16:06:29 -0800363 DECLARE_ALIGNED(16, tran_low_t, dqcoeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
364 DECLARE_ALIGNED(16, tran_low_t, ref_coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700365
Yushin Cho48f84db2016-11-07 21:20:17 -0800366 od_coeff ref_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
367 od_coeff out_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700368
369 od_raster_to_coding_order(ref_coeff_pvq, blk_size, tx_type, ref_coeff,
370 blk_size);
371
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800372 assert(OD_COEFF_SHIFT >= 3);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700373 if (lossless)
374 pvq_dc_quant = 1;
375 else {
Yushin Cho70669122016-12-08 09:53:14 -1000376 if (use_activity_masking)
377 pvq_dc_quant = OD_MAXI(
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800378 1, (quant[0] << (OD_COEFF_SHIFT - 3)) *
Yushin Cho70669122016-12-08 09:53:14 -1000379 dec->state.pvq_qm_q4[pli][od_qm_get_index(bs, 0)] >>
380 4);
381 else
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800382 pvq_dc_quant = OD_MAXI(1, quant[0] << (OD_COEFF_SHIFT - 3));
Yushin Cho77bba8d2016-11-04 16:36:56 -0700383 }
384
385 off = od_qm_offset(bs, xdec);
386
387 // copy int16 inputs to int32
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800388 for (i = 0; i < blk_size * blk_size; i++) {
389 ref_int32[i] = ref_coeff_pvq[i] << (OD_COEFF_SHIFT - coeff_shift);
390 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700391
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800392 od_pvq_decode(dec, ref_int32, out_int32, quant[1] << (OD_COEFF_SHIFT - 3),
393 pli, bs, OD_PVQ_BETA[use_activity_masking][pli][bs],
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800394 OD_ROBUST_STREAM, is_keyframe, &flags, ac_dc_coded,
395 dec->state.qm + off, dec->state.qm_inv + off);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700396
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800397 if (!has_dc_skip || out_int32[0]) {
398 out_int32[0] =
Nathan E. Egge89f58762016-12-28 16:31:50 -0500399 has_dc_skip + generic_decode(dec->r, &dec->state.adapt.model_dc[pli],
400 -1, &dec->state.adapt.ex_dc[pli][bs][0], 2,
401 "dc:mag");
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800402 if (out_int32[0]) out_int32[0] *= aom_read_bit(dec->r, "dc:sign") ? -1 : 1;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700403 }
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800404 out_int32[0] = out_int32[0] * pvq_dc_quant + ref_int32[0];
405
406 // copy int32 result back to int16
407 assert(OD_COEFF_SHIFT > coeff_shift);
408 rounding_mask = (1 << (OD_COEFF_SHIFT - coeff_shift - 1)) - 1;
409 for (i = 0; i < blk_size * blk_size; i++) {
410 dqcoeff_pvq[i] = (out_int32[i] + (out_int32[i] < 0) + rounding_mask) >>
411 (OD_COEFF_SHIFT - coeff_shift);
412 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700413
414 od_coding_order_to_raster(dqcoeff, blk_size, tx_type, dqcoeff_pvq, blk_size);
415
416 eob = blk_size * blk_size;
417
418 return eob;
419}
420
ltrudeaue1c09292017-01-20 15:42:13 -0500421static PVQ_SKIP_TYPE read_pvq_skip(AV1_COMMON *cm, MACROBLOCKD *const xd,
422 int plane, TX_SIZE tx_size) {
423 // decode ac/dc coded flag. bit0: DC coded, bit1 : AC coded
424 // NOTE : we don't use 5 symbols for luma here in aom codebase,
425 // since block partition is taken care of by aom.
426 // So, only AC/DC skip info is coded
427 const int ac_dc_coded = aom_decode_cdf_adapt(
428 xd->daala_dec.r,
429 xd->daala_dec.state.adapt.skip_cdf[2 * tx_size + (plane != 0)], 4,
430 xd->daala_dec.state.adapt.skip_increment, "skip");
431 if (ac_dc_coded < 0 || ac_dc_coded > 3) {
432 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
433 "Invalid PVQ Skip Type");
434 }
435 return ac_dc_coded;
436}
437
438static int av1_pvq_decode_helper2(AV1_COMMON *cm, MACROBLOCKD *const xd,
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800439 MB_MODE_INFO *const mbmi, int plane, int row,
440 int col, TX_SIZE tx_size, TX_TYPE tx_type) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700441 struct macroblockd_plane *const pd = &xd->plane[plane];
442 // transform block size in pixels
443 int tx_blk_size = tx_size_wide[tx_size];
444 int i, j;
445 tran_low_t *pvq_ref_coeff = pd->pvq_ref_coeff;
446 const int diff_stride = tx_blk_size;
447 int16_t *pred = pd->pred;
448 tran_low_t *const dqcoeff = pd->dqcoeff;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700449 uint8_t *dst;
450 int eob;
ltrudeaue1c09292017-01-20 15:42:13 -0500451 const PVQ_SKIP_TYPE ac_dc_coded = read_pvq_skip(cm, xd, plane, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700452
453 eob = 0;
454 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
455
Yushin Cho77bba8d2016-11-04 16:36:56 -0700456 if (ac_dc_coded) {
457 int xdec = pd->subsampling_x;
458 int seg_id = mbmi->segment_id;
459 int16_t *quant;
460 FWD_TXFM_PARAM fwd_txfm_param;
Yaowu Xufc1b2132016-11-07 15:16:15 -0800461 // ToDo(yaowu): correct this with optimal number from decoding process.
462 const int max_scan_line = tx_size_2d[tx_size];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700463
464 for (j = 0; j < tx_blk_size; j++)
465 for (i = 0; i < tx_blk_size; i++) {
466 pred[diff_stride * j + i] = dst[pd->dst.stride * j + i];
467 }
468
469 fwd_txfm_param.tx_type = tx_type;
470 fwd_txfm_param.tx_size = tx_size;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700471 fwd_txfm_param.lossless = xd->lossless[seg_id];
472
473 fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
474
475 quant = &pd->seg_dequant[seg_id][0]; // aom's quantizer
476
477 eob = av1_pvq_decode_helper(&xd->daala_dec, pvq_ref_coeff, dqcoeff, quant,
478 plane, tx_size, tx_type, xdec, ac_dc_coded);
479
480 // Since av1 does not have separate inverse transform
481 // but also contains adding to predicted image,
482 // pass blank dummy image to av1_inv_txfm_add_*x*(), i.e. set dst as zeros
483 for (j = 0; j < tx_blk_size; j++)
484 for (i = 0; i < tx_blk_size; i++) dst[j * pd->dst.stride + i] = 0;
485
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800486 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
487 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700488 }
489
490 return eob;
491}
492#endif
493
Alex Converse8aca36d2017-01-31 12:33:15 -0800494static void predict_and_reconstruct_intra_block(
495 AV1_COMMON *cm, MACROBLOCKD *const xd, aom_reader *const r,
496 MB_MODE_INFO *const mbmi, int plane, int row, int col, TX_SIZE tx_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700497 struct macroblockd_plane *const pd = &xd->plane[plane];
498 PREDICTION_MODE mode = (plane == 0) ? mbmi->mode : mbmi->uv_mode;
Luc Trudeau005feb62017-02-22 13:34:01 -0500499 PLANE_TYPE plane_type = get_plane_type(plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700500 uint8_t *dst;
Urvang Joshifeb925f2016-12-05 10:37:29 -0800501 const int block_idx = (row << 1) + col;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700502#if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -0700503 (void)r;
504#endif
Jingning Hanca14dda2016-12-09 09:36:00 -0800505 dst = &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700506
Jingning Han41bb3392016-12-14 10:46:48 -0800507#if !CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -0700508 if (mbmi->sb_type < BLOCK_8X8)
Urvang Joshifeb925f2016-12-05 10:37:29 -0800509 if (plane == 0) mode = xd->mi[0]->bmi[block_idx].as_mode;
Jingning Han41bb3392016-12-14 10:46:48 -0800510#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700511
David Barker839467f2017-01-19 11:06:15 +0000512 av1_predict_intra_block(xd, pd->width, pd->height, txsize_to_bsize[tx_size],
513 mode, dst, pd->dst.stride, dst, pd->dst.stride, col,
514 row, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700515
516 if (!mbmi->skip) {
517 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700518#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700519 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 0);
Jingning Han1be18782016-10-21 11:48:15 -0700520 int16_t max_scan_line = 0;
521 const int eob =
522 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size,
523 tx_type, &max_scan_line, r, mbmi->segment_id);
Angie Chianged8cd9a2016-10-21 16:44:47 -0700524#if CONFIG_ADAPT_SCAN
Jingning Han45e0d462017-02-13 10:37:10 -0800525 if (xd->counts)
526 av1_update_scan_count_facade(cm, xd->counts, tx_size, tx_type,
527 pd->dqcoeff, eob);
Angie Chianged8cd9a2016-10-21 16:44:47 -0700528#endif
Jingning Han1be18782016-10-21 11:48:15 -0700529 if (eob)
530 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
531 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700532#else
ltrudeaue1c09292017-01-20 15:42:13 -0500533 av1_pvq_decode_helper2(cm, xd, mbmi, plane, row, col, tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700534#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700535 }
536}
537
538#if CONFIG_VAR_TX
Angie Chiangff6d8902016-10-21 11:02:09 -0700539static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
540 aom_reader *r, MB_MODE_INFO *const mbmi,
Jingning Han8fd62b72016-10-21 12:55:54 -0700541 int plane, BLOCK_SIZE plane_bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700542 int blk_row, int blk_col, TX_SIZE tx_size,
543 int *eob_total) {
544 const struct macroblockd_plane *const pd = &xd->plane[plane];
545 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
546 const int tx_row = blk_row >> (1 - pd->subsampling_y);
547 const int tx_col = blk_col >> (1 - pd->subsampling_x);
548 const TX_SIZE plane_tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700549 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700550 : mbmi->inter_tx_size[tx_row][tx_col];
Jingning Han5f614262016-10-27 14:27:43 -0700551 // Scale to match transform block unit.
Jingning Hanf64062f2016-11-02 16:22:18 -0700552 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
553 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700554
555 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
556
557 if (tx_size == plane_tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500558 PLANE_TYPE plane_type = get_plane_type(plane);
Jingning Han8fd62b72016-10-21 12:55:54 -0700559 int block_idx = (blk_row << 1) + blk_col;
560 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiangff6d8902016-10-21 11:02:09 -0700561 const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700562 int16_t max_scan_line = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700563 const int eob =
Yaowu Xuf883b422016-08-30 14:01:10 -0700564 av1_decode_block_tokens(xd, plane, sc, blk_col, blk_row, plane_tx_size,
Jingning Han1be18782016-10-21 11:48:15 -0700565 tx_type, &max_scan_line, r, mbmi->segment_id);
Jingning Han758b2ce2017-01-20 17:12:55 -0800566#if CONFIG_ADAPT_SCAN
Jingning Han45e0d462017-02-13 10:37:10 -0800567 if (xd->counts)
568 av1_update_scan_count_facade(cm, xd->counts, tx_size, tx_type,
569 pd->dqcoeff, eob);
Jingning Han758b2ce2017-01-20 17:12:55 -0800570#endif
Jingning Han9ca05b72017-01-03 14:41:36 -0800571 inverse_transform_block(xd, plane, tx_type, plane_tx_size,
572 &pd->dst.buf[(blk_row * pd->dst.stride + blk_col)
573 << tx_size_wide_log2[0]],
574 pd->dst.stride, max_scan_line, eob);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700575 *eob_total += eob;
576 } else {
Jingning Hanf64062f2016-11-02 16:22:18 -0700577 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
578 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700579 int i;
580
581 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700582
583 for (i = 0; i < 4; ++i) {
Jingning Han5f614262016-10-27 14:27:43 -0700584 const int offsetr = blk_row + (i >> 1) * bsl;
585 const int offsetc = blk_col + (i & 0x01) * bsl;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700586
587 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
588
Jingning Han8fd62b72016-10-21 12:55:54 -0700589 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
Jingning Hanf64062f2016-11-02 16:22:18 -0700590 offsetc, sub_txs, eob_total);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700591 }
592 }
593}
594#endif // CONFIG_VAR_TX
595
Jingning Hanfe45b212016-11-22 10:30:23 -0800596#if !CONFIG_VAR_TX || CONFIG_SUPERTX || \
597 (!CONFIG_VAR_TX && CONFIG_EXT_TX && CONFIG_RECT_TX)
Angie Chiangff6d8902016-10-21 11:02:09 -0700598static int reconstruct_inter_block(AV1_COMMON *cm, MACROBLOCKD *const xd,
Alex Converse8aca36d2017-01-31 12:33:15 -0800599 aom_reader *const r, int segment_id,
600 int plane, int row, int col,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700601 TX_SIZE tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500602 PLANE_TYPE plane_type = get_plane_type(plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700603 int block_idx = (row << 1) + col;
604 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700605#if CONFIG_PVQ
606 int eob;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700607 (void)r;
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800608 (void)segment_id;
609#else
610 struct macroblockd_plane *const pd = &xd->plane[plane];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700611#endif
612
613#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700614 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700615 int16_t max_scan_line = 0;
616 const int eob =
617 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size, tx_type,
618 &max_scan_line, r, segment_id);
Jingning Hanca14dda2016-12-09 09:36:00 -0800619 uint8_t *dst =
620 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Angie Chianged8cd9a2016-10-21 16:44:47 -0700621#if CONFIG_ADAPT_SCAN
Jingning Han45e0d462017-02-13 10:37:10 -0800622 if (xd->counts)
623 av1_update_scan_count_facade(cm, xd->counts, tx_size, tx_type, pd->dqcoeff,
624 eob);
Angie Chianged8cd9a2016-10-21 16:44:47 -0700625#endif
Jingning Han1be18782016-10-21 11:48:15 -0700626 if (eob)
Jingning Hanca14dda2016-12-09 09:36:00 -0800627 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
628 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700629#else
ltrudeaue1c09292017-01-20 15:42:13 -0500630 eob = av1_pvq_decode_helper2(cm, xd, &xd->mi[0]->mbmi, plane, row, col,
631 tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700632#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700633 return eob;
634}
635#endif // !CONFIG_VAR_TX || CONFIG_SUPER_TX
636
Yaowu Xuf883b422016-08-30 14:01:10 -0700637static MB_MODE_INFO *set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700638 BLOCK_SIZE bsize, int mi_row, int mi_col,
Jingning Hanfaad0e12016-12-07 10:54:57 -0800639 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700640 const int offset = mi_row * cm->mi_stride + mi_col;
641 int x, y;
642 const TileInfo *const tile = &xd->tile;
643
644 xd->mi = cm->mi_grid_visible + offset;
645 xd->mi[0] = &cm->mi[offset];
646 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
647 // passing bsize from decode_partition().
648 xd->mi[0]->mbmi.sb_type = bsize;
Angie Chiang394c3372016-11-03 11:13:15 -0700649#if CONFIG_RD_DEBUG
650 xd->mi[0]->mbmi.mi_row = mi_row;
651 xd->mi[0]->mbmi.mi_col = mi_col;
652#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700653 for (y = 0; y < y_mis; ++y)
Jingning Han97d85482016-07-15 11:06:05 -0700654 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700655
Jingning Hanfaad0e12016-12-07 10:54:57 -0800656 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700657 set_skip_context(xd, mi_row, mi_col);
658
659#if CONFIG_VAR_TX
660 xd->max_tx_size = max_txsize_lookup[bsize];
661#endif
662
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800663// Distance of Mb to the various image edges. These are specified to 8th pel
664// as they are always compared to values that are in 1/8th pel units
665#if CONFIG_DEPENDENT_HORZTILES
666 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
667 cm->dependent_horz_tiles);
668#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700669 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800670#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700671
Yaowu Xuf883b422016-08-30 14:01:10 -0700672 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700673 return &xd->mi[0]->mbmi;
674}
675
676#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700677static MB_MODE_INFO *set_offsets_extend(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700678 MACROBLOCKD *const xd,
679 const TileInfo *const tile,
680 BLOCK_SIZE bsize_pred, int mi_row_pred,
681 int mi_col_pred, int mi_row_ori,
682 int mi_col_ori) {
683 // Used in supertx
684 // (mi_row_ori, mi_col_ori): location for mv
685 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
Jingning Han93531242016-12-20 11:54:36 -0800686 const int bw = mi_size_wide[bsize_pred];
687 const int bh = mi_size_high[bsize_pred];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700688 const int offset = mi_row_ori * cm->mi_stride + mi_col_ori;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700689 xd->mi = cm->mi_grid_visible + offset;
690 xd->mi[0] = cm->mi + offset;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800691#if CONFIG_DEPENDENT_HORZTILES
692 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
693 cm->mi_cols, cm->dependent_horz_tiles);
694#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700695 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
696 cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800697#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700698
699 xd->up_available = (mi_row_ori > tile->mi_row_start);
700 xd->left_available = (mi_col_ori > tile->mi_col_start);
701
Jingning Hanfaad0e12016-12-07 10:54:57 -0800702 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700703
704 return &xd->mi[0]->mbmi;
705}
706
Yaowu Xuf883b422016-08-30 14:01:10 -0700707static MB_MODE_INFO *set_mb_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
708 BLOCK_SIZE bsize, int mi_row, int mi_col,
709 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700710 const int offset = mi_row * cm->mi_stride + mi_col;
711 const TileInfo *const tile = &xd->tile;
712 int x, y;
713
714 xd->mi = cm->mi_grid_visible + offset;
715 xd->mi[0] = cm->mi + offset;
716 xd->mi[0]->mbmi.sb_type = bsize;
717 for (y = 0; y < y_mis; ++y)
718 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
719
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800720#if CONFIG_DEPENDENT_HORZTILES
721 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
722 cm->dependent_horz_tiles);
723#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700724 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800725#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700726 return &xd->mi[0]->mbmi;
727}
728
Yaowu Xuf883b422016-08-30 14:01:10 -0700729static void set_offsets_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700730 const TileInfo *const tile, BLOCK_SIZE bsize,
731 int mi_row, int mi_col) {
Jingning Han93531242016-12-20 11:54:36 -0800732 const int bw = mi_size_wide[bsize];
733 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700734 const int offset = mi_row * cm->mi_stride + mi_col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700735
736 xd->mi = cm->mi_grid_visible + offset;
737 xd->mi[0] = cm->mi + offset;
738
Jingning Hanfaad0e12016-12-07 10:54:57 -0800739 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700740
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800741#if CONFIG_DEPENDENT_HORZTILES
742 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
743 cm->dependent_horz_tiles);
744#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700745 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800746#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700747
Yaowu Xuf883b422016-08-30 14:01:10 -0700748 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700749}
750
Yaowu Xuf883b422016-08-30 14:01:10 -0700751static void set_param_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700752 BLOCK_SIZE bsize, int mi_row, int mi_col,
753 int txfm, int skip) {
Jingning Han93531242016-12-20 11:54:36 -0800754 const int bw = mi_size_wide[bsize];
755 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -0700756 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
757 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700758 const int offset = mi_row * cm->mi_stride + mi_col;
759 int x, y;
760
761 xd->mi = cm->mi_grid_visible + offset;
762 xd->mi[0] = cm->mi + offset;
763
764 for (y = 0; y < y_mis; ++y)
765 for (x = 0; x < x_mis; ++x) {
766 xd->mi[y * cm->mi_stride + x]->mbmi.skip = skip;
767 xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
768 }
769#if CONFIG_VAR_TX
770 xd->above_txfm_context = cm->above_txfm_context + mi_col;
771 xd->left_txfm_context =
772 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
Yaowu Xu52a17632016-11-17 15:48:21 -0800773 set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bw, bh, skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700774#endif
775}
776
Yaowu Xuf883b422016-08-30 14:01:10 -0700777static void set_ref(AV1_COMMON *const cm, MACROBLOCKD *const xd, int idx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700778 int mi_row, int mi_col) {
779 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
780 RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
781 xd->block_refs[idx] = ref_buffer;
Yaowu Xuf883b422016-08-30 14:01:10 -0700782 if (!av1_is_valid_scale(&ref_buffer->sf))
783 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700784 "Invalid scale factors");
Yaowu Xuf883b422016-08-30 14:01:10 -0700785 av1_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
786 &ref_buffer->sf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700787 xd->corrupted |= ref_buffer->buf->corrupted;
788}
789
790static void dec_predict_b_extend(
Yaowu Xuf883b422016-08-30 14:01:10 -0700791 AV1Decoder *const pbi, MACROBLOCKD *const xd, const TileInfo *const tile,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700792 int block, int mi_row_ori, int mi_col_ori, int mi_row_pred, int mi_col_pred,
793 int mi_row_top, int mi_col_top, uint8_t *dst_buf[3], int dst_stride[3],
794 BLOCK_SIZE bsize_top, BLOCK_SIZE bsize_pred, int b_sub8x8, int bextend) {
795 // Used in supertx
796 // (mi_row_ori, mi_col_ori): location for mv
797 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
798 // (mi_row_top, mi_col_top, bsize_top): region of the top partition size
799 // block: sub location of sub8x8 blocks
800 // b_sub8x8: 1: ori is sub8x8; 0: ori is not sub8x8
801 // bextend: 1: region to predict is an extension of ori; 0: not
802 int r = (mi_row_pred - mi_row_top) * MI_SIZE;
803 int c = (mi_col_pred - mi_col_top) * MI_SIZE;
Jingning Han93531242016-12-20 11:54:36 -0800804 const int mi_width_top = mi_size_wide[bsize_top];
805 const int mi_height_top = mi_size_high[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700806 MB_MODE_INFO *mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700807 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700808
809 if (mi_row_pred < mi_row_top || mi_col_pred < mi_col_top ||
810 mi_row_pred >= mi_row_top + mi_height_top ||
811 mi_col_pred >= mi_col_top + mi_width_top || mi_row_pred >= cm->mi_rows ||
812 mi_col_pred >= cm->mi_cols)
813 return;
814
815 mbmi = set_offsets_extend(cm, xd, tile, bsize_pred, mi_row_pred, mi_col_pred,
816 mi_row_ori, mi_col_ori);
817 set_ref(cm, xd, 0, mi_row_pred, mi_col_pred);
818 if (has_second_ref(&xd->mi[0]->mbmi))
819 set_ref(cm, xd, 1, mi_row_pred, mi_col_pred);
820
Jingning Han2511c662016-12-22 11:57:34 -0800821 if (!bextend) mbmi->tx_size = max_txsize_lookup[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700822
823 xd->plane[0].dst.stride = dst_stride[0];
824 xd->plane[1].dst.stride = dst_stride[1];
825 xd->plane[2].dst.stride = dst_stride[2];
826 xd->plane[0].dst.buf = dst_buf[0] +
827 (r >> xd->plane[0].subsampling_y) * dst_stride[0] +
828 (c >> xd->plane[0].subsampling_x);
829 xd->plane[1].dst.buf = dst_buf[1] +
830 (r >> xd->plane[1].subsampling_y) * dst_stride[1] +
831 (c >> xd->plane[1].subsampling_x);
832 xd->plane[2].dst.buf = dst_buf[2] +
833 (r >> xd->plane[2].subsampling_y) * dst_stride[2] +
834 (c >> xd->plane[2].subsampling_x);
835
836 if (!b_sub8x8)
Yaowu Xuf883b422016-08-30 14:01:10 -0700837 av1_build_inter_predictors_sb_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700838#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700839 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700840#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700841 mi_row_pred, mi_col_pred, bsize_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700842 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700843 av1_build_inter_predictors_sb_sub8x8_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700844#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700845 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700846#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700847 mi_row_pred, mi_col_pred,
848 bsize_pred, block);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700849}
850
Yaowu Xuf883b422016-08-30 14:01:10 -0700851static void dec_extend_dir(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700852 const TileInfo *const tile, int block,
853 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
854 int mi_col, int mi_row_top, int mi_col_top,
855 uint8_t *dst_buf[3], int dst_stride[3], int dir) {
856 // dir: 0-lower, 1-upper, 2-left, 3-right
857 // 4-lowerleft, 5-upperleft, 6-lowerright, 7-upperright
Jingning Han93531242016-12-20 11:54:36 -0800858 const int mi_width = mi_size_wide[bsize];
859 const int mi_height = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700860 int xss = xd->plane[1].subsampling_x;
861 int yss = xd->plane[1].subsampling_y;
Jingning Hanfeb517c2016-12-21 16:02:07 -0800862#if CONFIG_CB4X4
863 const int unify_bsize = 1;
864#else
865 const int unify_bsize = 0;
866#endif
867 int b_sub8x8 = (bsize < BLOCK_8X8) && !unify_bsize ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700868 BLOCK_SIZE extend_bsize;
Jingning Han24f24a52016-12-27 10:13:28 -0800869 int mi_row_pred, mi_col_pred;
870
871 int wide_unit, high_unit;
872 int i, j;
873 int ext_offset = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700874
875 if (dir == 0 || dir == 1) {
Jingning Han93531242016-12-20 11:54:36 -0800876 extend_bsize =
877 (mi_width == mi_size_wide[BLOCK_8X8] || bsize < BLOCK_8X8 || xss < yss)
878 ? BLOCK_8X8
879 : BLOCK_16X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800880#if CONFIG_CB4X4
881 if (bsize < BLOCK_8X8) {
882 extend_bsize = BLOCK_4X4;
883 ext_offset = mi_size_wide[BLOCK_8X8];
884 }
885#endif
886
887 wide_unit = mi_size_wide[extend_bsize];
888 high_unit = mi_size_high[extend_bsize];
889
890 mi_row_pred = mi_row + ((dir == 0) ? mi_height : -(mi_height + ext_offset));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700891 mi_col_pred = mi_col;
892
Jingning Han24f24a52016-12-27 10:13:28 -0800893 for (j = 0; j < mi_height + ext_offset; j += high_unit)
894 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
895 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
896 mi_row_pred + j, mi_col_pred + i, mi_row_top,
897 mi_col_top, dst_buf, dst_stride, top_bsize,
898 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700899 } else if (dir == 2 || dir == 3) {
Jingning Han5b7706a2016-12-21 09:55:10 -0800900 extend_bsize =
901 (mi_height == mi_size_high[BLOCK_8X8] || bsize < BLOCK_8X8 || yss < xss)
902 ? BLOCK_8X8
903 : BLOCK_8X16;
Jingning Han24f24a52016-12-27 10:13:28 -0800904#if CONFIG_CB4X4
905 if (bsize < BLOCK_8X8) {
906 extend_bsize = BLOCK_4X4;
907 ext_offset = mi_size_wide[BLOCK_8X8];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700908 }
Jingning Han24f24a52016-12-27 10:13:28 -0800909#endif
910
911 wide_unit = mi_size_wide[extend_bsize];
912 high_unit = mi_size_high[extend_bsize];
913
914 mi_row_pred = mi_row;
915 mi_col_pred = mi_col + ((dir == 3) ? mi_width : -(mi_width + ext_offset));
916
917 for (j = 0; j < mi_height + ext_offset; j += high_unit)
918 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
919 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
920 mi_row_pred + j, mi_col_pred + i, mi_row_top,
921 mi_col_top, dst_buf, dst_stride, top_bsize,
922 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700923 } else {
924 extend_bsize = BLOCK_8X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800925#if CONFIG_CB4X4
926 if (bsize < BLOCK_8X8) {
927 extend_bsize = BLOCK_4X4;
928 ext_offset = mi_size_wide[BLOCK_8X8];
929 }
930#endif
931 wide_unit = mi_size_wide[extend_bsize];
932 high_unit = mi_size_high[extend_bsize];
933
Jingning Han5b7706a2016-12-21 09:55:10 -0800934 mi_row_pred = mi_row + ((dir == 4 || dir == 6) ? mi_height
Jingning Han24f24a52016-12-27 10:13:28 -0800935 : -(mi_height + ext_offset));
Jingning Han5b7706a2016-12-21 09:55:10 -0800936 mi_col_pred =
Jingning Han24f24a52016-12-27 10:13:28 -0800937 mi_col + ((dir == 6 || dir == 7) ? mi_width : -(mi_width + ext_offset));
938
939 for (j = 0; j < mi_height + ext_offset; j += high_unit)
940 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
941 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
942 mi_row_pred + j, mi_col_pred + i, mi_row_top,
943 mi_col_top, dst_buf, dst_stride, top_bsize,
944 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700945 }
946}
947
Yaowu Xuf883b422016-08-30 14:01:10 -0700948static void dec_extend_all(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700949 const TileInfo *const tile, int block,
950 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
951 int mi_col, int mi_row_top, int mi_col_top,
952 uint8_t *dst_buf[3], int dst_stride[3]) {
953 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
954 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
955 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
956 mi_row_top, mi_col_top, dst_buf, dst_stride, 1);
957 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
958 mi_row_top, mi_col_top, dst_buf, dst_stride, 2);
959 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
960 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
961 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
962 mi_row_top, mi_col_top, dst_buf, dst_stride, 4);
963 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
964 mi_row_top, mi_col_top, dst_buf, dst_stride, 5);
965 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
966 mi_row_top, mi_col_top, dst_buf, dst_stride, 6);
967 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
968 mi_row_top, mi_col_top, dst_buf, dst_stride, 7);
969}
970
Yaowu Xuf883b422016-08-30 14:01:10 -0700971static void dec_predict_sb_complex(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700972 const TileInfo *const tile, int mi_row,
973 int mi_col, int mi_row_top, int mi_col_top,
974 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize,
975 uint8_t *dst_buf[3], int dst_stride[3]) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700976 const AV1_COMMON *const cm = &pbi->common;
Jingning Han5b7706a2016-12-21 09:55:10 -0800977 const int hbs = mi_size_wide[bsize] / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700978 const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
979 const BLOCK_SIZE subsize = get_subsize(bsize, partition);
980#if CONFIG_EXT_PARTITION_TYPES
981 const BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
982#endif
983 int i;
984 const int mi_offset = mi_row * cm->mi_stride + mi_col;
985 uint8_t *dst_buf1[3], *dst_buf2[3], *dst_buf3[3];
Jingning Hanfeb517c2016-12-21 16:02:07 -0800986#if CONFIG_CB4X4
987 const int unify_bsize = 1;
988#else
989 const int unify_bsize = 0;
990#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700991
992 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
993 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
994 DECLARE_ALIGNED(16, uint8_t, tmp_buf3[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
995 int dst_stride1[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
996 int dst_stride2[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
997 int dst_stride3[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
998
Yaowu Xuf883b422016-08-30 14:01:10 -0700999#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001000 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1001 int len = sizeof(uint16_t);
1002 dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
1003 dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_TX_SQUARE * len);
1004 dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_TX_SQUARE * len);
1005 dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
1006 dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_TX_SQUARE * len);
1007 dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_TX_SQUARE * len);
1008 dst_buf3[0] = CONVERT_TO_BYTEPTR(tmp_buf3);
1009 dst_buf3[1] = CONVERT_TO_BYTEPTR(tmp_buf3 + MAX_TX_SQUARE * len);
1010 dst_buf3[2] = CONVERT_TO_BYTEPTR(tmp_buf3 + 2 * MAX_TX_SQUARE * len);
1011 } else {
1012#endif
1013 dst_buf1[0] = tmp_buf1;
1014 dst_buf1[1] = tmp_buf1 + MAX_TX_SQUARE;
1015 dst_buf1[2] = tmp_buf1 + 2 * MAX_TX_SQUARE;
1016 dst_buf2[0] = tmp_buf2;
1017 dst_buf2[1] = tmp_buf2 + MAX_TX_SQUARE;
1018 dst_buf2[2] = tmp_buf2 + 2 * MAX_TX_SQUARE;
1019 dst_buf3[0] = tmp_buf3;
1020 dst_buf3[1] = tmp_buf3 + MAX_TX_SQUARE;
1021 dst_buf3[2] = tmp_buf3 + 2 * MAX_TX_SQUARE;
Yaowu Xuf883b422016-08-30 14:01:10 -07001022#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001023 }
1024#endif
1025
1026 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1027
1028 xd->mi = cm->mi_grid_visible + mi_offset;
1029 xd->mi[0] = cm->mi + mi_offset;
1030
1031 for (i = 0; i < MAX_MB_PLANE; i++) {
1032 xd->plane[i].dst.buf = dst_buf[i];
1033 xd->plane[i].dst.stride = dst_stride[i];
1034 }
1035
1036 switch (partition) {
1037 case PARTITION_NONE:
1038 assert(bsize < top_bsize);
1039 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1040 mi_row_top, mi_col_top, dst_buf, dst_stride,
1041 top_bsize, bsize, 0, 0);
1042 dec_extend_all(pbi, xd, tile, 0, bsize, top_bsize, mi_row, mi_col,
1043 mi_row_top, mi_col_top, dst_buf, dst_stride);
1044 break;
1045 case PARTITION_HORZ:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001046 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001047 // For sub8x8, predict in 8x8 unit
1048 // First half
1049 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1050 mi_row_top, mi_col_top, dst_buf, dst_stride,
1051 top_bsize, BLOCK_8X8, 1, 0);
1052 if (bsize < top_bsize)
1053 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1054 mi_row_top, mi_col_top, dst_buf, dst_stride);
1055
1056 // Second half
1057 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1058 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1059 top_bsize, BLOCK_8X8, 1, 1);
1060 if (bsize < top_bsize)
1061 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1062 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1063
1064 // weighted average to smooth the boundary
1065 xd->plane[0].dst.buf = dst_buf[0];
1066 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001067 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001068 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1069 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1070 0);
1071 } else {
1072 // First half
1073 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1074 mi_row_top, mi_col_top, dst_buf, dst_stride,
1075 top_bsize, subsize, 0, 0);
1076 if (bsize < top_bsize)
1077 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1078 mi_row_top, mi_col_top, dst_buf, dst_stride);
1079 else
1080 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1081 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1082
1083 if (mi_row + hbs < cm->mi_rows) {
1084 // Second half
1085 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col,
1086 mi_row + hbs, mi_col, mi_row_top, mi_col_top,
1087 dst_buf1, dst_stride1, top_bsize, subsize, 0, 0);
1088 if (bsize < top_bsize)
1089 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1090 mi_col, mi_row_top, mi_col_top, dst_buf1,
1091 dst_stride1);
1092 else
1093 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1094 mi_col, mi_row_top, mi_col_top, dst_buf1,
1095 dst_stride1, 1);
1096
1097 // weighted average to smooth the boundary
1098 for (i = 0; i < MAX_MB_PLANE; i++) {
1099 xd->plane[i].dst.buf = dst_buf[i];
1100 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001101 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001102 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1103 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1104 PARTITION_HORZ, i);
1105 }
1106 }
1107 }
1108 break;
1109 case PARTITION_VERT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001110 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001111 // First half
1112 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1113 mi_row_top, mi_col_top, dst_buf, dst_stride,
1114 top_bsize, BLOCK_8X8, 1, 0);
1115 if (bsize < top_bsize)
1116 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1117 mi_row_top, mi_col_top, dst_buf, dst_stride);
1118
1119 // Second half
1120 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1121 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1122 top_bsize, BLOCK_8X8, 1, 1);
1123 if (bsize < top_bsize)
1124 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1125 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1126
1127 // Smooth
1128 xd->plane[0].dst.buf = dst_buf[0];
1129 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001130 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001131 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1132 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1133 0);
1134 } else {
1135 // First half
1136 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1137 mi_row_top, mi_col_top, dst_buf, dst_stride,
1138 top_bsize, subsize, 0, 0);
1139 if (bsize < top_bsize)
1140 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1141 mi_row_top, mi_col_top, dst_buf, dst_stride);
1142 else
1143 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1144 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1145
1146 // Second half
1147 if (mi_col + hbs < cm->mi_cols) {
1148 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1149 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1150 dst_stride1, top_bsize, subsize, 0, 0);
1151 if (bsize < top_bsize)
1152 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1153 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1154 dst_stride1);
1155 else
1156 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1157 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1158 dst_stride1, 2);
1159
1160 // Smooth
1161 for (i = 0; i < MAX_MB_PLANE; i++) {
1162 xd->plane[i].dst.buf = dst_buf[i];
1163 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001164 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001165 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1166 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1167 PARTITION_VERT, i);
1168 }
1169 }
1170 }
1171 break;
1172 case PARTITION_SPLIT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001173 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001174 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1175 mi_row_top, mi_col_top, dst_buf, dst_stride,
1176 top_bsize, BLOCK_8X8, 1, 0);
1177 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1178 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1179 top_bsize, BLOCK_8X8, 1, 1);
1180 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1181 mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1182 top_bsize, BLOCK_8X8, 1, 1);
1183 dec_predict_b_extend(pbi, xd, tile, 3, mi_row, mi_col, mi_row, mi_col,
1184 mi_row_top, mi_col_top, dst_buf3, dst_stride3,
1185 top_bsize, BLOCK_8X8, 1, 1);
1186 if (bsize < top_bsize) {
1187 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1188 mi_row_top, mi_col_top, dst_buf, dst_stride);
1189 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1190 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1191 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1192 mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1193 dec_extend_all(pbi, xd, tile, 3, subsize, top_bsize, mi_row, mi_col,
1194 mi_row_top, mi_col_top, dst_buf3, dst_stride3);
1195 }
1196 } else {
1197 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row_top,
1198 mi_col_top, subsize, top_bsize, dst_buf,
1199 dst_stride);
1200 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1201 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col + hbs,
1202 mi_row_top, mi_col_top, subsize, top_bsize,
1203 dst_buf1, dst_stride1);
1204 if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols)
1205 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col,
1206 mi_row_top, mi_col_top, subsize, top_bsize,
1207 dst_buf2, dst_stride2);
1208 if (mi_row + hbs < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1209 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col + hbs,
1210 mi_row_top, mi_col_top, subsize, top_bsize,
1211 dst_buf3, dst_stride3);
1212 }
1213 for (i = 0; i < MAX_MB_PLANE; i++) {
Jingning Han9e0976a2016-12-27 17:52:42 -08001214#if !CONFIG_CB4X4
Jingning Han24f24a52016-12-27 10:13:28 -08001215 if (bsize == BLOCK_8X8 && i != 0)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001216 continue; // Skip <4x4 chroma smoothing
Jingning Han9e0976a2016-12-27 17:52:42 -08001217#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001218 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001219 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001220 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1221 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1222 PARTITION_VERT, i);
1223 if (mi_row + hbs < cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001224 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001225 xd, dst_buf2[i], dst_stride2[i], dst_buf3[i], dst_stride3[i],
1226 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1227 PARTITION_VERT, 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_buf2[i], dst_stride2[i],
1230 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1231 PARTITION_HORZ, i);
1232 }
1233 } else if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols) {
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],
1236 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1237 PARTITION_HORZ, i);
1238 }
1239 }
1240 break;
1241#if CONFIG_EXT_PARTITION_TYPES
1242 case PARTITION_HORZ_A:
1243 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1244 mi_row_top, mi_col_top, dst_buf, dst_stride,
1245 top_bsize, bsize2, 0, 0);
1246 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1247 mi_row_top, mi_col_top, dst_buf, dst_stride);
1248
1249 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1250 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1251 dst_stride1, top_bsize, bsize2, 0, 0);
1252 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1253 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1254
1255 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1256 mi_col, mi_row_top, mi_col_top, dst_buf2,
1257 dst_stride2, top_bsize, subsize, 0, 0);
1258 if (bsize < top_bsize)
1259 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1260 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1261 else
1262 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1263 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1264 1);
1265
1266 for (i = 0; i < MAX_MB_PLANE; i++) {
1267 xd->plane[i].dst.buf = dst_buf[i];
1268 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001269 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001270 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1271 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1272 i);
1273 }
1274 for (i = 0; i < MAX_MB_PLANE; 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_buf2[i], dst_stride2[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_A:
1282
1283 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1284 mi_row_top, mi_col_top, dst_buf, dst_stride,
1285 top_bsize, bsize2, 0, 0);
1286 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1287 mi_row_top, mi_col_top, dst_buf, dst_stride);
1288
1289 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1290 mi_col, mi_row_top, mi_col_top, dst_buf1,
1291 dst_stride1, top_bsize, bsize2, 0, 0);
1292 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1293 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1294
1295 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1296 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1297 dst_stride2, top_bsize, subsize, 0, 0);
1298 if (bsize < top_bsize)
1299 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1300 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1301 dst_stride2);
1302 else
1303 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1304 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1305 dst_stride2, 2);
1306
1307 for (i = 0; i < MAX_MB_PLANE; i++) {
1308 xd->plane[i].dst.buf = dst_buf[i];
1309 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001310 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001311 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1312 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1313 i);
1314 }
1315 for (i = 0; i < MAX_MB_PLANE; 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_buf2[i], dst_stride2[i], mi_row,
1318 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1319 i);
1320 }
1321 break;
1322 case PARTITION_HORZ_B:
1323 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1324 mi_row_top, mi_col_top, dst_buf, dst_stride,
1325 top_bsize, subsize, 0, 0);
1326 if (bsize < top_bsize)
1327 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1328 mi_row_top, mi_col_top, dst_buf, dst_stride);
1329 else
1330 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1331 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1332
1333 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1334 mi_col, mi_row_top, mi_col_top, dst_buf1,
1335 dst_stride1, top_bsize, bsize2, 0, 0);
1336 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1337 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1338
1339 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1340 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1341 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1342 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1343 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1344 dst_stride2);
1345
1346 for (i = 0; i < MAX_MB_PLANE; i++) {
1347 xd->plane[i].dst.buf = dst_buf1[i];
1348 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001349 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001350 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1351 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1352 PARTITION_VERT, i);
1353 }
1354 for (i = 0; i < MAX_MB_PLANE; i++) {
1355 xd->plane[i].dst.buf = dst_buf[i];
1356 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001357 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001358 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1359 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1360 i);
1361 }
1362 break;
1363 case PARTITION_VERT_B:
1364 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1365 mi_row_top, mi_col_top, dst_buf, dst_stride,
1366 top_bsize, subsize, 0, 0);
1367 if (bsize < top_bsize)
1368 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1369 mi_row_top, mi_col_top, dst_buf, dst_stride);
1370 else
1371 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1372 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1373
1374 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1375 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1376 dst_stride1, top_bsize, bsize2, 0, 0);
1377 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1378 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1379
1380 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1381 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1382 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1383 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1384 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1385 dst_stride2);
1386
1387 for (i = 0; i < MAX_MB_PLANE; i++) {
1388 xd->plane[i].dst.buf = dst_buf1[i];
1389 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001390 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001391 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1392 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1393 PARTITION_HORZ, i);
1394 }
1395 for (i = 0; i < MAX_MB_PLANE; i++) {
1396 xd->plane[i].dst.buf = dst_buf[i];
1397 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001398 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001399 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1400 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1401 i);
1402 }
1403 break;
1404#endif // CONFIG_EXT_PARTITION_TYPES
1405 default: assert(0);
1406 }
1407}
1408
Yaowu Xuf883b422016-08-30 14:01:10 -07001409static void set_segment_id_supertx(const AV1_COMMON *const cm, const int mi_row,
1410 const int mi_col, const BLOCK_SIZE bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001411 const struct segmentation *seg = &cm->seg;
Jingning Han5b7706a2016-12-21 09:55:10 -08001412 const int miw = AOMMIN(mi_size_wide[bsize], cm->mi_cols - mi_col);
1413 const int mih = AOMMIN(mi_size_high[bsize], cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001414 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1415 MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
1416 int r, c;
1417 int seg_id_supertx = MAX_SEGMENTS;
1418
1419 if (!seg->enabled) {
1420 seg_id_supertx = 0;
1421 } else {
1422 // Find the minimum segment_id
1423 for (r = 0; r < mih; r++)
1424 for (c = 0; c < miw; c++)
1425 seg_id_supertx =
Yaowu Xuf883b422016-08-30 14:01:10 -07001426 AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001427 assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);
1428 }
1429
1430 // Assign the the segment_id back to segment_id_supertx
1431 for (r = 0; r < mih; r++)
1432 for (c = 0; c < miw; c++)
1433 mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
1434}
1435#endif // CONFIG_SUPERTX
1436
Yue Chen64550b62017-01-12 12:18:22 -08001437static void decode_mbmi_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001438#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001439 int supertx_enabled,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001440#endif // CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001441 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001442#if CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001443 PARTITION_TYPE partition,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001444#endif // CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001445 BLOCK_SIZE bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001446 AV1_COMMON *const cm = &pbi->common;
Jingning Han85dc03f2016-12-06 16:03:10 -08001447 const int bw = mi_size_wide[bsize];
1448 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -07001449 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1450 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Nathan E. Eggeebbd4792016-10-05 19:30:15 -04001451
Michael Bebenita6048d052016-08-25 14:40:54 -07001452#if CONFIG_ACCOUNTING
1453 aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
1454#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001455#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001456 if (supertx_enabled) {
Yue Chen64550b62017-01-12 12:18:22 -08001457 set_mb_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001458 } else {
Yue Chen64550b62017-01-12 12:18:22 -08001459 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001460 }
1461#if CONFIG_EXT_PARTITION_TYPES
1462 xd->mi[0]->mbmi.partition = partition;
1463#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001464 av1_read_mode_info(pbi, xd, supertx_enabled, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001465#else
Yue Chen64550b62017-01-12 12:18:22 -08001466 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001467#if CONFIG_EXT_PARTITION_TYPES
1468 xd->mi[0]->mbmi.partition = partition;
1469#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001470 av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001471#endif // CONFIG_SUPERTX
1472
1473 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1474 const BLOCK_SIZE uv_subsize =
1475 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1476 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -07001477 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001478 "Invalid block size.");
1479 }
1480
1481#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001482 xd->mi[0]->mbmi.segment_id_supertx = MAX_SEGMENTS;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001483#endif // CONFIG_SUPERTX
1484
Yue Chen64550b62017-01-12 12:18:22 -08001485 xd->corrupted |= aom_reader_has_error(r);
1486}
1487
1488static void decode_token_and_recon_block(AV1Decoder *const pbi,
1489 MACROBLOCKD *const xd, int mi_row,
1490 int mi_col, aom_reader *r,
1491 BLOCK_SIZE bsize) {
1492 AV1_COMMON *const cm = &pbi->common;
1493 const int bw = mi_size_wide[bsize];
1494 const int bh = mi_size_high[bsize];
1495 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1496 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
1497 MB_MODE_INFO *mbmi;
1498
1499 mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yue Chen19e7aa82016-11-30 14:05:39 -08001500
Arild Fuldseth07441162016-08-15 15:07:52 +02001501#if CONFIG_DELTA_Q
1502 if (cm->delta_q_present_flag) {
1503 int i;
1504 for (i = 0; i < MAX_SEGMENTS; i++) {
1505 xd->plane[0].seg_dequant[i][0] =
1506 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
1507 xd->plane[0].seg_dequant[i][1] =
1508 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
1509 xd->plane[1].seg_dequant[i][0] =
1510 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1511 xd->plane[1].seg_dequant[i][1] =
1512 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1513 xd->plane[2].seg_dequant[i][0] =
1514 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1515 xd->plane[2].seg_dequant[i][1] =
1516 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1517 }
1518 }
1519#endif
1520
Jingning Han41bb3392016-12-14 10:46:48 -08001521#if CONFIG_CB4X4
1522 if (mbmi->skip) reset_skip_context(xd, bsize);
1523#else
Jingning Hand39cc722016-12-02 14:03:26 -08001524 if (mbmi->skip) reset_skip_context(xd, AOMMAX(BLOCK_8X8, bsize));
Jingning Han41bb3392016-12-14 10:46:48 -08001525#endif
Jingning Hand39cc722016-12-02 14:03:26 -08001526
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001527#if CONFIG_COEF_INTERLEAVE
1528 {
1529 const struct macroblockd_plane *const pd_y = &xd->plane[0];
1530 const struct macroblockd_plane *const pd_c = &xd->plane[1];
1531 const TX_SIZE tx_log2_y = mbmi->tx_size;
1532 const TX_SIZE tx_log2_c = get_uv_tx_size(mbmi, pd_c);
1533 const int tx_sz_y = (1 << tx_log2_y);
1534 const int tx_sz_c = (1 << tx_log2_c);
1535 const int num_4x4_w_y = pd_y->n4_w;
1536 const int num_4x4_h_y = pd_y->n4_h;
1537 const int num_4x4_w_c = pd_c->n4_w;
1538 const int num_4x4_h_c = pd_c->n4_h;
1539 const int max_4x4_w_y = get_max_4x4_size(num_4x4_w_y, xd->mb_to_right_edge,
1540 pd_y->subsampling_x);
1541 const int max_4x4_h_y = get_max_4x4_size(num_4x4_h_y, xd->mb_to_bottom_edge,
1542 pd_y->subsampling_y);
1543 const int max_4x4_w_c = get_max_4x4_size(num_4x4_w_c, xd->mb_to_right_edge,
1544 pd_c->subsampling_x);
1545 const int max_4x4_h_c = get_max_4x4_size(num_4x4_h_c, xd->mb_to_bottom_edge,
1546 pd_c->subsampling_y);
1547
1548 // The max_4x4_w/h may be smaller than tx_sz under some corner cases,
1549 // i.e. when the SB is splitted by tile boundaries.
1550 const int tu_num_w_y = (max_4x4_w_y + tx_sz_y - 1) / tx_sz_y;
1551 const int tu_num_h_y = (max_4x4_h_y + tx_sz_y - 1) / tx_sz_y;
1552 const int tu_num_w_c = (max_4x4_w_c + tx_sz_c - 1) / tx_sz_c;
1553 const int tu_num_h_c = (max_4x4_h_c + tx_sz_c - 1) / tx_sz_c;
1554 const int tu_num_y = tu_num_w_y * tu_num_h_y;
1555 const int tu_num_c = tu_num_w_c * tu_num_h_c;
1556
1557 if (!is_inter_block(mbmi)) {
1558 int tu_idx_c = 0;
1559 int row_y, col_y, row_c, col_c;
1560 int plane;
1561
1562#if CONFIG_PALETTE
1563 for (plane = 0; plane <= 1; ++plane) {
1564 if (mbmi->palette_mode_info.palette_size[plane])
1565 av1_decode_palette_tokens(xd, plane, r);
1566 }
1567#endif
1568
1569 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1570 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1571 // luma
1572 predict_and_reconstruct_intra_block(
1573 cm, xd, r, mbmi, 0, row_y * tx_sz_y, col_y * tx_sz_y, tx_log2_y);
1574 // chroma
1575 if (tu_idx_c < tu_num_c) {
1576 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1577 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1578 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c,
1579 col_c, tx_log2_c);
1580 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c,
1581 col_c, tx_log2_c);
1582 tu_idx_c++;
1583 }
1584 }
1585 }
1586
1587 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1588 while (tu_idx_c < tu_num_c) {
1589 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1590 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1591 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c, col_c,
1592 tx_log2_c);
1593 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c, col_c,
1594 tx_log2_c);
1595 tu_idx_c++;
1596 }
1597 } else {
1598 // Prediction
David Barkerac37fa32016-12-02 12:30:21 +00001599 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001600 AOMMAX(bsize, BLOCK_8X8));
1601
1602 // Reconstruction
1603 if (!mbmi->skip) {
1604 int eobtotal = 0;
1605 int tu_idx_c = 0;
1606 int row_y, col_y, row_c, col_c;
1607
1608 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1609 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1610 // luma
1611 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 0,
1612 row_y * tx_sz_y,
1613 col_y * tx_sz_y, tx_log2_y);
1614 // chroma
1615 if (tu_idx_c < tu_num_c) {
1616 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1617 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1618 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1619 1, row_c, col_c, tx_log2_c);
1620 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1621 2, row_c, col_c, tx_log2_c);
1622 tu_idx_c++;
1623 }
1624 }
1625 }
1626
1627 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1628 while (tu_idx_c < tu_num_c) {
1629 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1630 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1631 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 1,
1632 row_c, col_c, tx_log2_c);
1633 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 2,
1634 row_c, col_c, tx_log2_c);
1635 tu_idx_c++;
1636 }
1637
1638 if (bsize >= BLOCK_8X8 && eobtotal == 0)
1639#if CONFIG_MISC_FIXES
1640 mbmi->has_no_coeffs = 1;
1641#else
1642 mbmi->skip = 1;
1643#endif
1644 }
1645 }
1646 }
1647#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001648 if (!is_inter_block(mbmi)) {
1649 int plane;
Urvang Joshib100db72016-10-12 16:28:56 -07001650#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001651 for (plane = 0; plane <= 1; ++plane) {
1652 if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xuf883b422016-08-30 14:01:10 -07001653 av1_decode_palette_tokens(xd, plane, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001654 }
Urvang Joshib100db72016-10-12 16:28:56 -07001655#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001656 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1657 const struct macroblockd_plane *const pd = &xd->plane[plane];
Debargha Mukherjee2f123402016-08-30 17:43:38 -07001658 const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han2d64f122016-10-21 12:44:29 -07001659 const int stepr = tx_size_high_unit[tx_size];
1660 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han41bb3392016-12-14 10:46:48 -08001661#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001662#if CONFIG_CHROMA_2X2
1663 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1664#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001665 const BLOCK_SIZE plane_bsize =
1666 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001667#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001668#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001669 const BLOCK_SIZE plane_bsize =
1670 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001671#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001672 int row, col;
Jingning Hanbafee8d2016-12-02 10:25:03 -08001673 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1674 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001675
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001676#if CONFIG_CB4X4
1677 if (bsize < BLOCK_8X8 && plane && !is_chroma_reference(mi_row, mi_col))
1678 continue;
1679#endif
1680
Yaowu Xuc27fc142016-08-22 16:08:15 -07001681 for (row = 0; row < max_blocks_high; row += stepr)
1682 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001683 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane, row, col,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001684 tx_size);
1685 }
1686 } else {
Yue Chen9ab6d712017-01-12 15:50:46 -08001687 int ref;
1688
1689 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
1690 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
1691 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
1692
1693 xd->block_refs[ref] = ref_buf;
1694 if ((!av1_is_valid_scale(&ref_buf->sf)))
1695 aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
1696 "Reference frame has invalid dimensions");
1697 av1_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col, &ref_buf->sf);
1698 }
Yue Chen69f18e12016-09-08 14:48:15 -07001699#if CONFIG_WARPED_MOTION
1700 if (mbmi->motion_mode == WARPED_CAUSAL) {
1701 int i;
1702#if CONFIG_AOM_HIGHBITDEPTH
1703 int use_hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH;
1704#endif // CONFIG_AOM_HIGHBITDEPTH
1705
1706 for (i = 0; i < 3; ++i) {
1707 const struct macroblockd_plane *pd = &xd->plane[i];
1708
1709 av1_warp_plane(&mbmi->wm_params[0],
1710#if CONFIG_AOM_HIGHBITDEPTH
1711 xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH, xd->bd,
1712#endif // CONFIG_AOM_HIGHBITDEPTH
1713 pd->pre[0].buf0, pd->pre[0].width, pd->pre[0].height,
1714 pd->pre[0].stride, pd->dst.buf,
1715 ((mi_col * MI_SIZE) >> pd->subsampling_x),
1716 ((mi_row * MI_SIZE) >> pd->subsampling_y),
Jingning Hanff6ee6a2016-12-07 09:55:21 -08001717 xd->n8_w * (MI_SIZE >> pd->subsampling_x),
1718 xd->n8_h * (MI_SIZE >> pd->subsampling_y),
1719 pd->dst.stride, pd->subsampling_x, pd->subsampling_y, 16,
1720 16, 0);
Yue Chen69f18e12016-09-08 14:48:15 -07001721 }
1722 } else {
1723#endif // CONFIG_WARPED_MOTION
Jingning Han41bb3392016-12-14 10:46:48 -08001724#if CONFIG_CB4X4
1725 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL, bsize);
1726#else
1727 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
1728 AOMMAX(bsize, BLOCK_8X8));
1729#endif
Yue Chen69f18e12016-09-08 14:48:15 -07001730#if CONFIG_WARPED_MOTION
1731 }
1732#endif // CONFIG_WARPED_MOTION
Yue Chencb60b182016-10-13 15:18:22 -07001733#if CONFIG_MOTION_VAR
1734 if (mbmi->motion_mode == OBMC_CAUSAL) {
Yue Chenf27b1602017-01-13 11:11:43 -08001735#if CONFIG_NCOBMC
1736 av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
1737#else
Yue Chen894fcce2016-10-21 16:50:52 -07001738 av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yue Chenf27b1602017-01-13 11:11:43 -08001739#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001740 }
Yue Chencb60b182016-10-13 15:18:22 -07001741#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001742
1743 // Reconstruction
1744 if (!mbmi->skip) {
1745 int eobtotal = 0;
1746 int plane;
1747
1748 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1749 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han41bb3392016-12-14 10:46:48 -08001750#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001751#if CONFIG_CHROMA_2X2
1752 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1753#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001754 const BLOCK_SIZE plane_bsize =
1755 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001756#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001757#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001758 const BLOCK_SIZE plane_bsize =
1759 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001760#endif
Jingning Hanbafee8d2016-12-02 10:25:03 -08001761 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1762 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001763 int row, col;
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001764
1765#if CONFIG_CB4X4
1766 if (bsize < BLOCK_8X8 && plane && !is_chroma_reference(mi_row, mi_col))
1767 continue;
1768#endif
1769
Yaowu Xuc27fc142016-08-22 16:08:15 -07001770#if CONFIG_VAR_TX
Jingning Han70e5f3f2016-11-09 17:03:07 -08001771 const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07001772 const int bh_var_tx = tx_size_high_unit[max_tx_size];
1773 const int bw_var_tx = tx_size_wide_unit[max_tx_size];
Jingning Hanbafee8d2016-12-02 10:25:03 -08001774 for (row = 0; row < max_blocks_high; row += bh_var_tx)
1775 for (col = 0; col < max_blocks_wide; col += bw_var_tx)
Jingning Hanfe45b212016-11-22 10:30:23 -08001776 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, row, col,
1777 max_tx_size, &eobtotal);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001778#else
1779 const TX_SIZE tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -07001780 plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han2d64f122016-10-21 12:44:29 -07001781 const int stepr = tx_size_high_unit[tx_size];
1782 const int stepc = tx_size_wide_unit[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001783 for (row = 0; row < max_blocks_high; row += stepr)
1784 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001785 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1786 plane, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001787#endif
1788 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001789 }
1790 }
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001791#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001792
Yaowu Xuf883b422016-08-30 14:01:10 -07001793 xd->corrupted |= aom_reader_has_error(r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001794}
1795
Yue Chen9ab6d712017-01-12 15:50:46 -08001796#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
1797static void detoken_and_recon_sb(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1798 int mi_row, int mi_col, aom_reader *r,
1799 BLOCK_SIZE bsize) {
1800 AV1_COMMON *const cm = &pbi->common;
1801 const int hbs = mi_size_wide[bsize] >> 1;
1802#if CONFIG_CB4X4
1803 const int unify_bsize = 1;
1804#else
1805 const int unify_bsize = 0;
1806#endif
1807#if CONFIG_EXT_PARTITION_TYPES
1808 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1809#endif
1810 PARTITION_TYPE partition;
1811 BLOCK_SIZE subsize;
1812 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1813 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1814
1815 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1816
1817 partition = get_partition(cm, mi_row, mi_col, bsize);
1818 subsize = subsize_lookup[partition][bsize];
1819
1820 if (!hbs && !unify_bsize) {
1821 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1822 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1823 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1824 } else {
1825 switch (partition) {
1826 case PARTITION_NONE:
1827 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
1828 break;
1829 case PARTITION_HORZ:
1830 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1831 if (has_rows)
1832 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r,
1833 subsize);
1834 break;
1835 case PARTITION_VERT:
1836 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1837 if (has_cols)
1838 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r,
1839 subsize);
1840 break;
1841 case PARTITION_SPLIT:
1842 detoken_and_recon_sb(pbi, xd, mi_row, mi_col, r, subsize);
1843 detoken_and_recon_sb(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1844 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1845 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col + hbs, r, subsize);
1846 break;
1847#if CONFIG_EXT_PARTITION_TYPES
1848 case PARTITION_HORZ_A:
1849 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1850 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1851 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1852 break;
1853 case PARTITION_HORZ_B:
1854 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1855 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1856 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1857 bsize2);
1858 break;
1859 case PARTITION_VERT_A:
1860 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1861 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1862 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1863 break;
1864 case PARTITION_VERT_B:
1865 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1866 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1867 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1868 bsize2);
1869 break;
1870#endif
1871 default: assert(0 && "Invalid partition type");
1872 }
1873 }
1874}
1875#endif
1876
Yue Chen64550b62017-01-12 12:18:22 -08001877static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1878#if CONFIG_SUPERTX
1879 int supertx_enabled,
1880#endif // CONFIG_SUPERTX
1881 int mi_row, int mi_col, aom_reader *r,
1882#if CONFIG_EXT_PARTITION_TYPES
1883 PARTITION_TYPE partition,
1884#endif // CONFIG_EXT_PARTITION_TYPES
1885 BLOCK_SIZE bsize) {
1886 decode_mbmi_block(pbi, xd,
1887#if CONFIG_SUPERTX
1888 supertx_enabled,
1889#endif
1890 mi_row, mi_col, r,
1891#if CONFIG_EXT_PARTITION_TYPES
1892 partition,
1893#endif
1894 bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001895#if !(CONFIG_MOTION_VAR && CONFIG_NCOBMC)
Yue Chen64550b62017-01-12 12:18:22 -08001896#if CONFIG_SUPERTX
1897 if (!supertx_enabled)
1898#endif // CONFIG_SUPERTX
1899 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001900#endif
Yue Chen64550b62017-01-12 12:18:22 -08001901}
1902
Yaowu Xuf883b422016-08-30 14:01:10 -07001903static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
1904 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001905 int has_rows, int has_cols,
Jingning Han1beb0102016-12-07 11:08:30 -08001906 BLOCK_SIZE bsize) {
Alex Converse55c6bde2017-01-12 15:55:31 -08001907#if CONFIG_UNPOISON_PARTITION_CTX
1908 const int ctx =
1909 partition_plane_context(xd, mi_row, mi_col, has_rows, has_cols, bsize);
1910 const aom_prob *const probs = ctx >= 0 ? cm->fc->partition_prob[ctx] : NULL;
1911 FRAME_COUNTS *const counts = ctx >= 0 ? xd->counts : NULL;
1912#else
Jingning Han1beb0102016-12-07 11:08:30 -08001913 const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
Yaowu Xuf883b422016-08-30 14:01:10 -07001914 const aom_prob *const probs = cm->fc->partition_prob[ctx];
Alex Converse55c6bde2017-01-12 15:55:31 -08001915 FRAME_COUNTS *const counts = xd->counts;
1916#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001917 PARTITION_TYPE p;
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001918#if CONFIG_EC_ADAPT
1919 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1920 (void)cm;
1921#elif CONFIG_EC_MULTISYMBOL
1922 FRAME_CONTEXT *ec_ctx = cm->fc;
1923#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001924
1925 if (has_rows && has_cols)
1926#if CONFIG_EXT_PARTITION_TYPES
1927 if (bsize <= BLOCK_8X8)
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001928#if CONFIG_EC_MULTISYMBOL
1929 p = (PARTITION_TYPE)aom_read_symbol(r, ec_ctx->partition_cdf[ctx],
1930 PARTITION_TYPES, ACCT_STR);
1931#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001932 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001933#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001934 else
Michael Bebenita6048d052016-08-25 14:40:54 -07001935 p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs,
1936 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001937#else
Nathan E. Egge9d9eb6c2016-11-16 09:44:26 -05001938#if CONFIG_EC_MULTISYMBOL
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001939 p = (PARTITION_TYPE)aom_read_symbol(r, ec_ctx->partition_cdf[ctx],
Yaowu Xuf2581a32016-10-20 13:05:47 -07001940 PARTITION_TYPES, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001941#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001942 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001943#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001944#endif // CONFIG_EXT_PARTITION_TYPES
1945 else if (!has_rows && has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001946 p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001947 else if (has_rows && !has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001948 p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001949 else
1950 p = PARTITION_SPLIT;
1951
1952 if (counts) ++counts->partition[ctx][p];
1953
1954 return p;
1955}
1956
1957#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001958static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
1959 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001960 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
1961 return 1;
1962 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001963 const int ctx = av1_get_skip_context(xd);
Michael Bebenita6048d052016-08-25 14:40:54 -07001964 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001965 FRAME_COUNTS *counts = xd->counts;
1966 if (counts) ++counts->skip[ctx][skip];
1967 return skip;
1968 }
1969}
1970#endif // CONFIG_SUPERTX
Jean-Marc Valin01435132017-02-18 14:12:53 -05001971#if CONFIG_CDEF
Yaowu Xud71be782016-10-14 08:47:03 -07001972static int clpf_all_skip(const AV1_COMMON *cm, int mi_col, int mi_row,
1973 int size) {
1974 int r, c;
1975 int skip = 1;
1976 const int maxc = AOMMIN(size, cm->mi_cols - mi_col);
1977 const int maxr = AOMMIN(size, cm->mi_rows - mi_row);
1978 for (r = 0; r < maxr && skip; r++) {
1979 for (c = 0; c < maxc && skip; c++) {
1980 skip &= !!cm->mi_grid_visible[(mi_row + r) * cm->mi_stride + mi_col + c]
1981 ->mbmi.skip;
1982 }
1983 }
1984 return skip;
1985}
1986#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001987
1988// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -07001989static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001990#if CONFIG_SUPERTX
1991 int supertx_enabled,
1992#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001993 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001994 BLOCK_SIZE bsize, int n4x4_l2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001995 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001996 const int n8x8_l2 = n4x4_l2 - 1;
Jingning Hanff17e162016-12-07 17:58:18 -08001997 const int num_8x8_wh = mi_size_wide[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001998 const int hbs = num_8x8_wh >> 1;
Jingning Han41bb3392016-12-14 10:46:48 -08001999#if CONFIG_CB4X4
2000 const int unify_bsize = 1;
2001#else
2002 const int unify_bsize = 0;
2003#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002004 PARTITION_TYPE partition;
2005 BLOCK_SIZE subsize;
2006#if CONFIG_EXT_PARTITION_TYPES
2007 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
2008#endif
2009 const int has_rows = (mi_row + hbs) < cm->mi_rows;
2010 const int has_cols = (mi_col + hbs) < cm->mi_cols;
2011#if CONFIG_SUPERTX
2012 const int read_token = !supertx_enabled;
2013 int skip = 0;
Jingning Han2511c662016-12-22 11:57:34 -08002014 TX_SIZE supertx_size = max_txsize_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002015 const TileInfo *const tile = &xd->tile;
2016 int txfm = DCT_DCT;
2017#endif // CONFIG_SUPERTX
2018
2019 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
2020
Jingning Han6dbacf22016-12-14 17:28:35 -08002021 partition = (n4x4_l2 == 0) ? PARTITION_NONE
2022 : read_partition(cm, xd, mi_row, mi_col, r,
2023 has_rows, has_cols, bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002024 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
Yushin Cho77bba8d2016-11-04 16:36:56 -07002025
2026#if CONFIG_PVQ
2027 assert(partition < PARTITION_TYPES);
2028 assert(subsize < BLOCK_SIZES);
2029#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002030#if CONFIG_SUPERTX
2031 if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
2032 bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
2033 const int supertx_context = partition_supertx_context_lookup[partition];
Michael Bebenita6048d052016-08-25 14:40:54 -07002034 supertx_enabled = aom_read(
2035 r, cm->fc->supertx_prob[supertx_context][supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002036 if (xd->counts)
2037 xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
2038#if CONFIG_VAR_TX
2039 if (supertx_enabled) xd->supertx_size = supertx_size;
2040#endif
2041 }
2042#endif // CONFIG_SUPERTX
Jingning Han41bb3392016-12-14 10:46:48 -08002043 if (!hbs && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002044 // calculate bmode block dimensions (log 2)
2045 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
2046 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
2047 decode_block(pbi, xd,
2048#if CONFIG_SUPERTX
2049 supertx_enabled,
2050#endif // CONFIG_SUPERTX
2051 mi_row, mi_col, r,
2052#if CONFIG_EXT_PARTITION_TYPES
2053 partition,
2054#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002055 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002056 } else {
2057 switch (partition) {
2058 case PARTITION_NONE:
2059 decode_block(pbi, xd,
2060#if CONFIG_SUPERTX
2061 supertx_enabled,
2062#endif // CONFIG_SUPERTX
2063 mi_row, mi_col, r,
2064#if CONFIG_EXT_PARTITION_TYPES
2065 partition,
2066#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002067 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002068 break;
2069 case PARTITION_HORZ:
2070 decode_block(pbi, xd,
2071#if CONFIG_SUPERTX
2072 supertx_enabled,
2073#endif // CONFIG_SUPERTX
2074 mi_row, mi_col, r,
2075#if CONFIG_EXT_PARTITION_TYPES
2076 partition,
2077#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002078 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002079 if (has_rows)
2080 decode_block(pbi, xd,
2081#if CONFIG_SUPERTX
2082 supertx_enabled,
2083#endif // CONFIG_SUPERTX
2084 mi_row + hbs, mi_col, r,
2085#if CONFIG_EXT_PARTITION_TYPES
2086 partition,
2087#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002088 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002089 break;
2090 case PARTITION_VERT:
2091 decode_block(pbi, xd,
2092#if CONFIG_SUPERTX
2093 supertx_enabled,
2094#endif // CONFIG_SUPERTX
2095 mi_row, mi_col, r,
2096#if CONFIG_EXT_PARTITION_TYPES
2097 partition,
2098#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002099 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002100 if (has_cols)
2101 decode_block(pbi, xd,
2102#if CONFIG_SUPERTX
2103 supertx_enabled,
2104#endif // CONFIG_SUPERTX
2105 mi_row, mi_col + hbs, r,
2106#if CONFIG_EXT_PARTITION_TYPES
2107 partition,
2108#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002109 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002110 break;
2111 case PARTITION_SPLIT:
2112 decode_partition(pbi, xd,
2113#if CONFIG_SUPERTX
2114 supertx_enabled,
2115#endif // CONFIG_SUPERTX
2116 mi_row, mi_col, r, subsize, n8x8_l2);
2117 decode_partition(pbi, xd,
2118#if CONFIG_SUPERTX
2119 supertx_enabled,
2120#endif // CONFIG_SUPERTX
2121 mi_row, mi_col + hbs, r, subsize, n8x8_l2);
2122 decode_partition(pbi, xd,
2123#if CONFIG_SUPERTX
2124 supertx_enabled,
2125#endif // CONFIG_SUPERTX
2126 mi_row + hbs, mi_col, r, subsize, n8x8_l2);
2127 decode_partition(pbi, xd,
2128#if CONFIG_SUPERTX
2129 supertx_enabled,
2130#endif // CONFIG_SUPERTX
2131 mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
2132 break;
2133#if CONFIG_EXT_PARTITION_TYPES
2134 case PARTITION_HORZ_A:
2135 decode_block(pbi, xd,
2136#if CONFIG_SUPERTX
2137 supertx_enabled,
2138#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002139 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002140 decode_block(pbi, xd,
2141#if CONFIG_SUPERTX
2142 supertx_enabled,
2143#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002144 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002145 decode_block(pbi, xd,
2146#if CONFIG_SUPERTX
2147 supertx_enabled,
2148#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002149 mi_row + hbs, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002150 break;
2151 case PARTITION_HORZ_B:
2152 decode_block(pbi, xd,
2153#if CONFIG_SUPERTX
2154 supertx_enabled,
2155#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002156 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002157 decode_block(pbi, xd,
2158#if CONFIG_SUPERTX
2159 supertx_enabled,
2160#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002161 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002162 decode_block(pbi, xd,
2163#if CONFIG_SUPERTX
2164 supertx_enabled,
2165#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002166 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002167 break;
2168 case PARTITION_VERT_A:
2169 decode_block(pbi, xd,
2170#if CONFIG_SUPERTX
2171 supertx_enabled,
2172#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002173 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002174 decode_block(pbi, xd,
2175#if CONFIG_SUPERTX
2176 supertx_enabled,
2177#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002178 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002179 decode_block(pbi, xd,
2180#if CONFIG_SUPERTX
2181 supertx_enabled,
2182#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002183 mi_row, mi_col + hbs, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002184 break;
2185 case PARTITION_VERT_B:
2186 decode_block(pbi, xd,
2187#if CONFIG_SUPERTX
2188 supertx_enabled,
2189#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002190 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002191 decode_block(pbi, xd,
2192#if CONFIG_SUPERTX
2193 supertx_enabled,
2194#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002195 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002196 decode_block(pbi, xd,
2197#if CONFIG_SUPERTX
2198 supertx_enabled,
2199#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002200 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002201 break;
2202#endif
2203 default: assert(0 && "Invalid partition type");
2204 }
2205 }
2206
2207#if CONFIG_SUPERTX
2208 if (supertx_enabled && read_token) {
2209 uint8_t *dst_buf[3];
2210 int dst_stride[3], i;
2211 int offset = mi_row * cm->mi_stride + mi_col;
2212
2213 set_segment_id_supertx(cm, mi_row, mi_col, bsize);
2214
David Barker3aec8d62017-01-31 14:55:32 +00002215#if CONFIG_DELTA_Q
2216 if (cm->delta_q_present_flag) {
2217 for (i = 0; i < MAX_SEGMENTS; i++) {
2218 xd->plane[0].seg_dequant[i][0] =
2219 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
2220 xd->plane[0].seg_dequant[i][1] =
2221 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
2222 xd->plane[1].seg_dequant[i][0] =
2223 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2224 xd->plane[1].seg_dequant[i][1] =
2225 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2226 xd->plane[2].seg_dequant[i][0] =
2227 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2228 xd->plane[2].seg_dequant[i][1] =
2229 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2230 }
2231 }
2232#endif
2233
Yaowu Xuc27fc142016-08-22 16:08:15 -07002234 xd->mi = cm->mi_grid_visible + offset;
2235 xd->mi[0] = cm->mi + offset;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002236#if CONFIG_DEPENDENT_HORZTILES
2237 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2238 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols,
2239 cm->dependent_horz_tiles);
2240#else
Jingning Han5b7706a2016-12-21 09:55:10 -08002241 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2242 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002243#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002244 set_skip_context(xd, mi_row, mi_col);
2245 skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
2246 if (skip) {
2247 reset_skip_context(xd, bsize);
2248 } else {
2249#if CONFIG_EXT_TX
Sarah Parkere68a3e42017-02-16 14:03:24 -08002250 if (get_ext_tx_types(supertx_size, bsize, 1, cm->reduced_tx_set_used) >
2251 1) {
2252 const int eset =
2253 get_ext_tx_set(supertx_size, bsize, 1, cm->reduced_tx_set_used);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002254 if (eset > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002255 txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
Michael Bebenita6048d052016-08-25 14:40:54 -07002256 cm->fc->inter_ext_tx_prob[eset][supertx_size],
2257 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002258 if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
2259 }
2260 }
2261#else
2262 if (supertx_size < TX_32X32) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002263 txfm = aom_read_tree(r, av1_ext_tx_tree,
Jingning Han8f6eb182016-10-19 13:48:57 -07002264 cm->fc->inter_ext_tx_prob[supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002265 if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
2266 }
2267#endif // CONFIG_EXT_TX
2268 }
2269
Yaowu Xuf883b422016-08-30 14:01:10 -07002270 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002271 for (i = 0; i < MAX_MB_PLANE; i++) {
2272 dst_buf[i] = xd->plane[i].dst.buf;
2273 dst_stride[i] = xd->plane[i].dst.stride;
2274 }
2275 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
2276 bsize, dst_buf, dst_stride);
2277
2278 if (!skip) {
2279 int eobtotal = 0;
2280 MB_MODE_INFO *mbmi;
2281 set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
2282 mbmi = &xd->mi[0]->mbmi;
2283 mbmi->tx_type = txfm;
2284 assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
2285 for (i = 0; i < MAX_MB_PLANE; ++i) {
2286 const struct macroblockd_plane *const pd = &xd->plane[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002287 int row, col;
Debargha Mukherjee2f123402016-08-30 17:43:38 -07002288 const TX_SIZE tx_size = i ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han5b7706a2016-12-21 09:55:10 -08002289 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Jingning Han32b20282016-10-28 15:42:44 -07002290 const int stepr = tx_size_high_unit[tx_size];
2291 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han5b7706a2016-12-21 09:55:10 -08002292 const int max_blocks_wide = max_block_wide(xd, plane_bsize, i);
2293 const int max_blocks_high = max_block_high(xd, plane_bsize, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002294
2295 for (row = 0; row < max_blocks_high; row += stepr)
2296 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07002297 eobtotal += reconstruct_inter_block(
2298 cm, xd, r, mbmi->segment_id_supertx, i, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002299 }
Jingning Han24f24a52016-12-27 10:13:28 -08002300 if ((unify_bsize || !(subsize < BLOCK_8X8)) && eobtotal == 0) skip = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002301 }
2302 set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
2303 }
2304#endif // CONFIG_SUPERTX
2305
2306#if CONFIG_EXT_PARTITION_TYPES
2307 if (bsize >= BLOCK_8X8) {
2308 switch (partition) {
2309 case PARTITION_SPLIT:
2310 if (bsize > BLOCK_8X8) break;
2311 case PARTITION_NONE:
2312 case PARTITION_HORZ:
2313 case PARTITION_VERT:
2314 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
2315 break;
2316 case PARTITION_HORZ_A:
2317 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2318 update_partition_context(xd, mi_row + hbs, mi_col, subsize, subsize);
2319 break;
2320 case PARTITION_HORZ_B:
2321 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2322 update_partition_context(xd, mi_row + hbs, mi_col, bsize2, subsize);
2323 break;
2324 case PARTITION_VERT_A:
2325 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2326 update_partition_context(xd, mi_row, mi_col + hbs, subsize, subsize);
2327 break;
2328 case PARTITION_VERT_B:
2329 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2330 update_partition_context(xd, mi_row, mi_col + hbs, bsize2, subsize);
2331 break;
2332 default: assert(0 && "Invalid partition type");
2333 }
2334 }
2335#else
2336 // update partition context
2337 if (bsize >= BLOCK_8X8 &&
2338 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
Jingning Han1beb0102016-12-07 11:08:30 -08002339 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
David Barkerf8935c92016-10-26 14:54:06 +01002340#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xud71be782016-10-14 08:47:03 -07002341
Jean-Marc Valin01435132017-02-18 14:12:53 -05002342#if CONFIG_CDEF
David Barker9739f362016-11-10 09:29:32 +00002343#if CONFIG_EXT_PARTITION
2344 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128) {
2345 if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
2346 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2347 aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
2348 } else {
2349 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2350 0;
2351 }
2352 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64) {
2353#else
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002354 if (bsize == BLOCK_64X64) {
David Barker9739f362016-11-10 09:29:32 +00002355#endif
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002356 if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
2357 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2358 aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
2359 } else {
2360 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2361 0;
2362 }
2363 }
David Barker9739f362016-11-10 09:29:32 +00002364#if CONFIG_EXT_PARTITION
2365 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128 &&
2366 cm->clpf_strength_y && cm->clpf_size != CLPF_NOSIZE) {
2367 const int tl = mi_row * MI_SIZE / MIN_FB_SIZE * cm->clpf_stride +
2368 mi_col * MI_SIZE / MIN_FB_SIZE;
2369 if (cm->clpf_size == CLPF_128X128) {
2370 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
2371 } else if (cm->clpf_size == CLPF_64X64) {
2372 const int tr = tl + 2;
2373 const int bl = tl + 2 * cm->clpf_stride;
2374 const int br = tr + 2 * cm->clpf_stride;
2375 const int size = 64 / MI_SIZE;
2376
2377 // Up to four bits per SB
2378 if (!clpf_all_skip(cm, mi_col, mi_row, size))
2379 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
2380
2381 if (mi_col + size < cm->mi_cols &&
2382 !clpf_all_skip(cm, mi_col + size, mi_row, size))
2383 cm->clpf_blocks[tr] = aom_read_literal(r, 1, ACCT_STR);
2384
2385 if (mi_row + size < cm->mi_rows &&
2386 !clpf_all_skip(cm, mi_col, mi_row + size, size))
2387 cm->clpf_blocks[bl] = aom_read_literal(r, 1, ACCT_STR);
2388
2389 if (mi_col + size < cm->mi_cols && mi_row + size < cm->mi_rows &&
2390 !clpf_all_skip(cm, mi_col + size, mi_row + size, size))
2391 cm->clpf_blocks[br] = aom_read_literal(r, 1, ACCT_STR);
2392 } else if (cm->clpf_size == CLPF_32X32) {
2393 int i, j;
2394 const int size = 32 / MI_SIZE;
2395 for (i = 0; i < 4; ++i)
2396 for (j = 0; j < 4; ++j) {
2397 const int index = tl + i * cm->clpf_stride + j;
2398 if (mi_row + i * size < cm->mi_rows &&
2399 mi_col + j * size < cm->mi_cols &&
2400 !clpf_all_skip(cm, mi_col + j * size, mi_row + i * size, size))
2401 cm->clpf_blocks[index] = aom_read_literal(r, 1, ACCT_STR);
2402 }
2403 }
2404 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64 &&
2405#else
2406 if (bsize == BLOCK_64X64 &&
2407#endif // CONFIG_EXT_PARTITION
2408 cm->clpf_strength_y && cm->clpf_size != CLPF_NOSIZE) {
Yaowu Xud71be782016-10-14 08:47:03 -07002409 const int tl = mi_row * MI_SIZE / MIN_FB_SIZE * cm->clpf_stride +
2410 mi_col * MI_SIZE / MIN_FB_SIZE;
2411
2412 if (!((mi_row * MI_SIZE) & 127) && !((mi_col * MI_SIZE) & 127) &&
2413 cm->clpf_size == CLPF_128X128) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002414 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002415 } else if (cm->clpf_size == CLPF_64X64 &&
2416 !clpf_all_skip(cm, mi_col, mi_row, 64 / MI_SIZE)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002417 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002418 } else if (cm->clpf_size == CLPF_32X32) {
2419 const int tr = tl + 1;
2420 const int bl = tl + cm->clpf_stride;
2421 const int br = tr + cm->clpf_stride;
2422 const int size = 32 / MI_SIZE;
2423
2424 // Up to four bits per SB
2425 if (!clpf_all_skip(cm, mi_col, mi_row, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002426 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002427
2428 if (mi_col + size < cm->mi_cols &&
2429 !clpf_all_skip(cm, mi_col + size, mi_row, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002430 cm->clpf_blocks[tr] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002431
2432 if (mi_row + size < cm->mi_rows &&
2433 !clpf_all_skip(cm, mi_col, mi_row + size, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002434 cm->clpf_blocks[bl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002435
2436 if (mi_col + size < cm->mi_cols && mi_row + size < cm->mi_rows &&
2437 !clpf_all_skip(cm, mi_col + size, mi_row + size, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002438 cm->clpf_blocks[br] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002439 }
2440 }
Jean-Marc Valin01435132017-02-18 14:12:53 -05002441#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002442}
2443
Yaowu Xuc27fc142016-08-22 16:08:15 -07002444static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
2445 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002446 struct aom_internal_error_info *error_info,
Alex Converseeb780e72016-12-13 12:46:41 -08002447 aom_reader *r,
2448#if CONFIG_ANS && ANS_MAX_SYMBOLS
2449 int window_size,
2450#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
2451 aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002452 // Validate the calculated partition length. If the buffer
2453 // described by the partition can't be fully read, then restrict
2454 // it to the portion that can be (for EC mode) or throw an error.
2455 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002456 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002457 "Truncated packet or corrupt tile length");
2458
Alex Converse2cdf0d82016-12-13 13:53:09 -08002459#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08002460 r->window_size = window_size;
Alex Converse2cdf0d82016-12-13 13:53:09 -08002461#endif
Alex Converse346440b2017-01-03 13:47:37 -08002462 if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07002463 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002464 "Failed to allocate bool decoder %d", 1);
2465}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002466
Alex Conversea9598cd2017-02-03 14:18:05 -08002467#if !CONFIG_PVQ && !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Yaowu Xuf883b422016-08-30 14:01:10 -07002468static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
2469 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002470 int i, j, k, l, m;
Alex Conversea9598cd2017-02-03 14:18:05 -08002471#if CONFIG_EC_ADAPT
2472 const int node_limit = UNCONSTRAINED_NODES - 1;
2473#else
Thomas9ac55082016-09-23 18:04:17 +01002474 const int node_limit = UNCONSTRAINED_NODES;
Alex Conversea9598cd2017-02-03 14:18:05 -08002475#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002476
Michael Bebenita6048d052016-08-25 14:40:54 -07002477 if (aom_read_bit(r, ACCT_STR))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002478 for (i = 0; i < PLANE_TYPES; ++i)
2479 for (j = 0; j < REF_TYPES; ++j)
2480 for (k = 0; k < COEF_BANDS; ++k)
2481 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
Thomas9ac55082016-09-23 18:04:17 +01002482 for (m = 0; m < node_limit; ++m)
Michael Bebenita6048d052016-08-25 14:40:54 -07002483 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002484}
2485
Yaowu Xuf883b422016-08-30 14:01:10 -07002486static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002487 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
2488 TX_SIZE tx_size;
Jingning Han83630632016-12-16 11:27:25 -08002489 for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002490 read_coef_probs_common(fc->coef_probs[tx_size], r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002491}
Yushin Cho77bba8d2016-11-04 16:36:56 -07002492#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002493
Yaowu Xuf883b422016-08-30 14:01:10 -07002494static void setup_segmentation(AV1_COMMON *const cm,
2495 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002496 struct segmentation *const seg = &cm->seg;
2497 int i, j;
2498
2499 seg->update_map = 0;
2500 seg->update_data = 0;
2501
Yaowu Xuf883b422016-08-30 14:01:10 -07002502 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002503 if (!seg->enabled) return;
2504
2505 // Segmentation map update
2506 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2507 seg->update_map = 1;
2508 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002509 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002510 }
2511 if (seg->update_map) {
2512 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2513 seg->temporal_update = 0;
2514 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002515 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002516 }
2517 }
2518
2519 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07002520 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002521 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002522 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002523
Yaowu Xuf883b422016-08-30 14:01:10 -07002524 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002525
2526 for (i = 0; i < MAX_SEGMENTS; i++) {
2527 for (j = 0; j < SEG_LVL_MAX; j++) {
2528 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002529 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002530 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002531 av1_enable_segfeature(seg, i, j);
2532 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
2533 if (av1_is_segfeature_signed(j))
2534 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002535 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002536 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002537 }
2538 }
2539 }
2540}
2541
2542#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002543static void decode_restoration_mode(AV1_COMMON *cm,
2544 struct aom_read_bit_buffer *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002545 int p;
2546 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002547 if (aom_rb_read_bit(rb)) {
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002548#if USE_DOMAINTXFMRF
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002549 if (aom_rb_read_bit(rb))
2550 rsi->frame_restoration_type =
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002551 (aom_rb_read_bit(rb) ? RESTORE_DOMAINTXFMRF : RESTORE_SGRPROJ);
Debargha Mukherjee0e67b252016-12-08 09:22:44 -08002552 else
2553 rsi->frame_restoration_type = RESTORE_WIENER;
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002554#else
2555 rsi->frame_restoration_type =
2556 aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
2557#endif // USE_DOMAINTXFMRF
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002558 } else {
2559 rsi->frame_restoration_type =
2560 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
2561 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002562 for (p = 1; p < MAX_MB_PLANE; ++p) {
2563 cm->rst_info[p].frame_restoration_type =
2564 aom_rb_read_bit(rb) ? RESTORE_WIENER : RESTORE_NONE;
2565 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002566}
2567
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002568static void read_wiener_filter(WienerInfo *wiener_info, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002569 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002570 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2571 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002572 wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002573 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2574 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002575 wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002576 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2577 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002578 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002579 wiener_info->vfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002580 -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
2581 wiener_info->vfilter[2]);
2582
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002583 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002584 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2585 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002586 wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002587 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2588 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002589 wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002590 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2591 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002592 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002593 wiener_info->hfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002594 -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
2595 wiener_info->hfilter[2]);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002596}
2597
2598static void read_sgrproj_filter(SgrprojInfo *sgrproj_info, aom_reader *rb) {
2599 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
2600 sgrproj_info->xqd[0] =
2601 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN0;
2602 sgrproj_info->xqd[1] =
2603 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN1;
2604}
2605
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002606#if USE_DOMAINTXFMRF
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002607static void read_domaintxfmrf_filter(DomaintxfmrfInfo *domaintxfmrf_info,
2608 aom_reader *rb) {
2609 domaintxfmrf_info->sigma_r =
2610 aom_read_literal(rb, DOMAINTXFMRF_PARAMS_BITS, ACCT_STR);
2611}
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002612#endif // USE_DOMAINTXFMRF
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002613
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002614static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002615 int i, p;
clang-formatbda8d612016-09-19 15:55:46 -07002616 const int ntiles =
2617 av1_get_rest_ntiles(cm->width, cm->height, NULL, NULL, NULL, NULL);
Debargha Mukherjee1a0ae842017-01-26 16:45:22 -08002618 const int ntiles_uv =
2619 av1_get_rest_ntiles(ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
2620 ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y),
2621 NULL, NULL, NULL, NULL);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002622 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002623 if (rsi->frame_restoration_type != RESTORE_NONE) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002624 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002625 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002626 rsi->restoration_type[i] =
2627 aom_read_tree(rb, av1_switchable_restore_tree,
2628 cm->fc->switchable_restore_prob, ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002629 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002630 read_wiener_filter(&rsi->wiener_info[i], rb);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002631 } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002632 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002633#if USE_DOMAINTXFMRF
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002634 } else if (rsi->restoration_type[i] == RESTORE_DOMAINTXFMRF) {
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002635 read_domaintxfmrf_filter(&rsi->domaintxfmrf_info[i], rb);
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002636#endif // USE_DOMAINTXFMRF
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002637 }
2638 }
2639 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002640 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002641 if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002642 rsi->restoration_type[i] = RESTORE_WIENER;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002643 read_wiener_filter(&rsi->wiener_info[i], rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002644 } else {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002645 rsi->restoration_type[i] = RESTORE_NONE;
2646 }
2647 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002648 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002649 for (i = 0; i < ntiles; ++i) {
2650 if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
2651 rsi->restoration_type[i] = RESTORE_SGRPROJ;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002652 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
2653 } else {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002654 rsi->restoration_type[i] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002655 }
2656 }
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002657#if USE_DOMAINTXFMRF
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002658 } else if (rsi->frame_restoration_type == RESTORE_DOMAINTXFMRF) {
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002659 for (i = 0; i < ntiles; ++i) {
2660 if (aom_read(rb, RESTORE_NONE_DOMAINTXFMRF_PROB, ACCT_STR)) {
2661 rsi->restoration_type[i] = RESTORE_DOMAINTXFMRF;
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002662 read_domaintxfmrf_filter(&rsi->domaintxfmrf_info[i], rb);
2663 } else {
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002664 rsi->restoration_type[i] = RESTORE_NONE;
2665 }
2666 }
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002667#endif // USE_DOMAINTXFMRF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002668 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002669 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002670 for (p = 1; p < MAX_MB_PLANE; ++p) {
2671 rsi = &cm->rst_info[p];
2672 if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08002673 for (i = 0; i < ntiles_uv; ++i) {
2674 if (ntiles_uv > 1)
2675 rsi->restoration_type[i] =
2676 aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR) ? RESTORE_WIENER
2677 : RESTORE_NONE;
2678 else
2679 rsi->restoration_type[i] = RESTORE_WIENER;
2680 if (rsi->restoration_type[i] == RESTORE_WIENER) {
2681 read_wiener_filter(&rsi->wiener_info[i], rb);
2682 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002683 }
2684 }
2685 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002686}
2687#endif // CONFIG_LOOP_RESTORATION
2688
Yaowu Xuf883b422016-08-30 14:01:10 -07002689static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002690 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07002691 lf->filter_level = aom_rb_read_literal(rb, 6);
2692 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002693
2694 // Read in loop filter deltas applied at the MB level based on mode or ref
2695 // frame.
2696 lf->mode_ref_delta_update = 0;
2697
Yaowu Xuf883b422016-08-30 14:01:10 -07002698 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002699 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002700 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002701 if (lf->mode_ref_delta_update) {
2702 int i;
2703
2704 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002705 if (aom_rb_read_bit(rb))
2706 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002707
2708 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002709 if (aom_rb_read_bit(rb))
2710 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002711 }
2712 }
2713}
2714
Jean-Marc Valin01435132017-02-18 14:12:53 -05002715#if CONFIG_CDEF
Yaowu Xud71be782016-10-14 08:47:03 -07002716static void setup_clpf(AV1Decoder *pbi, struct aom_read_bit_buffer *rb) {
2717 AV1_COMMON *const cm = &pbi->common;
2718 const int width = pbi->cur_buf->buf.y_crop_width;
2719 const int height = pbi->cur_buf->buf.y_crop_height;
2720
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002721 cm->clpf_blocks = 0;
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02002722 cm->clpf_strength_y = aom_rb_read_literal(rb, 2);
2723 cm->clpf_strength_u = aom_rb_read_literal(rb, 2);
2724 cm->clpf_strength_v = aom_rb_read_literal(rb, 2);
2725 if (cm->clpf_strength_y) {
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002726 cm->clpf_size = aom_rb_read_literal(rb, 2);
Yaowu Xud71be782016-10-14 08:47:03 -07002727 if (cm->clpf_size != CLPF_NOSIZE) {
2728 int size;
2729 cm->clpf_stride =
2730 ((width + MIN_FB_SIZE - 1) & ~(MIN_FB_SIZE - 1)) >> MIN_FB_SIZE_LOG2;
2731 size =
2732 cm->clpf_stride * ((height + MIN_FB_SIZE - 1) & ~(MIN_FB_SIZE - 1)) >>
2733 MIN_FB_SIZE_LOG2;
2734 CHECK_MEM_ERROR(cm, cm->clpf_blocks, aom_malloc(size));
2735 memset(cm->clpf_blocks, -1, size);
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002736 }
2737 }
2738}
2739
Steinar Midtskogen2fd70ee2016-09-02 10:02:30 +02002740static int clpf_bit(UNUSED int k, UNUSED int l,
2741 UNUSED const YV12_BUFFER_CONFIG *rec,
2742 UNUSED const YV12_BUFFER_CONFIG *org,
2743 UNUSED const AV1_COMMON *cm, UNUSED int block_size,
2744 UNUSED int w, UNUSED int h, UNUSED unsigned int strength,
Yaowu Xud71be782016-10-14 08:47:03 -07002745 UNUSED unsigned int fb_size_log2, int8_t *bit) {
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002746 return *bit;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002747}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002748
Yaowu Xuf883b422016-08-30 14:01:10 -07002749static void setup_dering(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
2750 cm->dering_level = aom_rb_read_literal(rb, DERING_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002751}
Jean-Marc Valin01435132017-02-18 14:12:53 -05002752#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002753
Yaowu Xuf883b422016-08-30 14:01:10 -07002754static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2755 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002756}
2757
Yaowu Xuf883b422016-08-30 14:01:10 -07002758static void setup_quantization(AV1_COMMON *const cm,
2759 struct aom_read_bit_buffer *rb) {
2760 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002761 cm->y_dc_delta_q = read_delta_q(rb);
2762 cm->uv_dc_delta_q = read_delta_q(rb);
2763 cm->uv_ac_delta_q = read_delta_q(rb);
2764 cm->dequant_bit_depth = cm->bit_depth;
2765#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002766 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002767 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002768 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2769 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002770 } else {
2771 cm->min_qmlevel = 0;
2772 cm->max_qmlevel = 0;
2773 }
2774#endif
2775}
2776
Yaowu Xuf883b422016-08-30 14:01:10 -07002777static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002778 // Build y/uv dequant values based on segmentation.
2779 int i = 0;
2780#if CONFIG_AOM_QM
2781 int lossless;
2782 int j = 0;
2783 int qmlevel;
2784 int using_qm = cm->using_qmatrix;
2785 int minqm = cm->min_qmlevel;
2786 int maxqm = cm->max_qmlevel;
2787#endif
2788#if CONFIG_NEW_QUANT
2789 int b;
2790 int dq;
2791#endif // CONFIG_NEW_QUANT
2792 if (cm->seg.enabled) {
2793 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002794 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002795 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002796 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2797 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002798 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002799 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002800 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002801 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002802#if CONFIG_AOM_QM
2803 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2804 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2805 // NB: depends on base index so there is only 1 set per frame
2806 // No quant weighting when lossless or signalled not using QM
2807 qmlevel = (lossless || using_qm == 0)
2808 ? NUM_QM_LEVELS - 1
2809 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2810 for (j = 0; j < TX_SIZES; ++j) {
2811 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2812 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2813 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2814 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2815 }
2816#endif // CONFIG_AOM_QM
2817#if CONFIG_NEW_QUANT
2818 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2819 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002820 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002821 cm->y_dequant_nuq[i][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002822 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002823 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002824 }
2825 }
2826#endif // CONFIG_NEW_QUANT
2827 }
2828 } else {
2829 const int qindex = cm->base_qindex;
2830 // When segmentation is disabled, only the first value is used. The
2831 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002832 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2833 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002834 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002835 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002836 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002837 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002838#if CONFIG_AOM_QM
2839 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2840 cm->uv_ac_delta_q == 0;
2841 // No quant weighting when lossless or signalled not using QM
2842 qmlevel = (lossless || using_qm == 0)
2843 ? NUM_QM_LEVELS - 1
2844 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2845 for (j = 0; j < TX_SIZES; ++j) {
2846 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2847 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2848 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2849 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2850 }
2851#endif
2852#if CONFIG_NEW_QUANT
2853 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2854 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002855 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002856 cm->y_dequant_nuq[0][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002857 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002858 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002859 }
2860 }
2861#endif // CONFIG_NEW_QUANT
2862 }
2863}
2864
Angie Chiang5678ad92016-11-21 09:38:40 -08002865static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002866 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07002867 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002868}
2869
Yaowu Xuf883b422016-08-30 14:01:10 -07002870static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002871 cm->render_width = cm->width;
2872 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002873 if (aom_rb_read_bit(rb))
2874 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002875}
2876
Yaowu Xuf883b422016-08-30 14:01:10 -07002877static void resize_mv_buffer(AV1_COMMON *cm) {
2878 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002879 cm->cur_frame->mi_rows = cm->mi_rows;
2880 cm->cur_frame->mi_cols = cm->mi_cols;
2881 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002882 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002883 sizeof(*cm->cur_frame->mvs)));
2884}
2885
Yaowu Xuf883b422016-08-30 14:01:10 -07002886static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002887#if CONFIG_SIZE_LIMIT
2888 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002889 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002890 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2891 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2892#endif
2893 if (cm->width != width || cm->height != height) {
2894 const int new_mi_rows =
2895 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2896 const int new_mi_cols =
2897 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2898
Yaowu Xuf883b422016-08-30 14:01:10 -07002899 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002900 // dimensions as well as the overall size.
2901 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002902 if (av1_alloc_context_buffers(cm, width, height))
2903 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002904 "Failed to allocate context buffers");
2905 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002906 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002907 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002908 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002909 cm->width = width;
2910 cm->height = height;
2911 }
2912 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2913 cm->mi_cols > cm->cur_frame->mi_cols) {
2914 resize_mv_buffer(cm);
2915 }
2916}
2917
Yaowu Xuf883b422016-08-30 14:01:10 -07002918static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002919 int width, height;
2920 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002921 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002922 resize_context_buffers(cm, width, height);
2923 setup_render_size(cm, rb);
2924
2925 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002926 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002927 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2928 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002929#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002930 cm->use_highbitdepth,
2931#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002932 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002933 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2934 pool->cb_priv)) {
2935 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002936 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002937 "Failed to allocate frame buffer");
2938 }
2939 unlock_buffer_pool(pool);
2940
2941 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2942 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2943 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2944 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2945 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2946 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2947 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2948}
2949
Yaowu Xuf883b422016-08-30 14:01:10 -07002950static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002951 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002952 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002953 int this_xss, int this_yss) {
2954 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2955 ref_yss == this_yss;
2956}
2957
Yaowu Xuf883b422016-08-30 14:01:10 -07002958static void setup_frame_size_with_refs(AV1_COMMON *cm,
2959 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002960 int width, height;
2961 int found = 0, i;
2962 int has_valid_ref_frame = 0;
2963 BufferPool *const pool = cm->buffer_pool;
2964 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002965 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002966 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2967 width = buf->y_crop_width;
2968 height = buf->y_crop_height;
2969 cm->render_width = buf->render_width;
2970 cm->render_height = buf->render_height;
2971 found = 1;
2972 break;
2973 }
2974 }
2975
2976 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002977 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002978 setup_render_size(cm, rb);
2979 }
2980
2981 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002982 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002983 "Invalid frame size");
2984
2985 // Check to make sure at least one of frames that this frame references
2986 // has valid dimensions.
2987 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2988 RefBuffer *const ref_frame = &cm->frame_refs[i];
2989 has_valid_ref_frame |=
2990 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2991 ref_frame->buf->y_crop_height, width, height);
2992 }
2993 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002994 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002995 "Referenced frame has invalid size");
2996 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2997 RefBuffer *const ref_frame = &cm->frame_refs[i];
2998 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
2999 ref_frame->buf->subsampling_x,
3000 ref_frame->buf->subsampling_y, cm->bit_depth,
3001 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07003002 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003003 "Referenced frame has incompatible color format");
3004 }
3005
3006 resize_context_buffers(cm, width, height);
3007
3008 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07003009 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003010 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
3011 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07003012#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003013 cm->use_highbitdepth,
3014#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003015 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003016 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
3017 pool->cb_priv)) {
3018 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07003019 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003020 "Failed to allocate frame buffer");
3021 }
3022 unlock_buffer_pool(pool);
3023
3024 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
3025 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
3026 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
3027 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
3028 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
3029 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
3030 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
3031}
3032
Yaowu Xuf883b422016-08-30 14:01:10 -07003033static void read_tile_info(AV1Decoder *const pbi,
3034 struct aom_read_bit_buffer *const rb) {
3035 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003036#if CONFIG_EXT_TILE
3037// Read the tile width/height
3038#if CONFIG_EXT_PARTITION
3039 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003040 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
3041 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003042 } else
3043#endif // CONFIG_EXT_PARTITION
3044 {
Yaowu Xuf883b422016-08-30 14:01:10 -07003045 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
3046 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003047 }
3048
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003049#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003050 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003051#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003052
Yaowu Xuc27fc142016-08-22 16:08:15 -07003053 cm->tile_width <<= cm->mib_size_log2;
3054 cm->tile_height <<= cm->mib_size_log2;
3055
Yaowu Xuf883b422016-08-30 14:01:10 -07003056 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
3057 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003058
3059 // Get the number of tiles
3060 cm->tile_cols = 1;
3061 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
3062
3063 cm->tile_rows = 1;
3064 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
3065
3066 if (cm->tile_cols * cm->tile_rows > 1) {
3067 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07003068 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
3069 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003070 }
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003071
3072#if CONFIG_DEPENDENT_HORZTILES
3073 if (cm->tile_rows <= 1)
3074 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
3075 else
3076 cm->dependent_horz_tiles = 0;
3077#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003078#else
3079 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07003080 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003081
3082 // columns
3083 max_ones = max_log2_tile_cols - min_log2_tile_cols;
3084 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07003085 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003086
3087 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07003088 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003089 "Invalid number of tile columns");
3090
3091 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07003092 cm->log2_tile_rows = aom_rb_read_bit(rb);
3093 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003094#if CONFIG_DEPENDENT_HORZTILES
3095 if (cm->log2_tile_rows != 0)
3096 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
3097 else
3098 cm->dependent_horz_tiles = 0;
3099#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003100#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003101 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003102#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003103
Yaowu Xuc27fc142016-08-22 16:08:15 -07003104 cm->tile_cols = 1 << cm->log2_tile_cols;
3105 cm->tile_rows = 1 << cm->log2_tile_rows;
3106
3107 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
3108 cm->tile_width >>= cm->log2_tile_cols;
3109 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
3110 cm->tile_height >>= cm->log2_tile_rows;
3111
3112 // round to integer multiples of superblock size
3113 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
3114 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
3115
Thomas Davies4974e522016-11-07 17:44:05 +00003116// tile size magnitude
3117#if !CONFIG_TILE_GROUPS
3118 if (cm->tile_rows > 1 || cm->tile_cols > 1)
3119#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003120 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003121#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00003122
Thomas Davies80188d12016-10-26 16:08:35 -07003123#if CONFIG_TILE_GROUPS
3124 // Store an index to the location of the tile group information
3125 pbi->tg_size_bit_offset = rb->bit_offset;
3126 pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
3127 if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
3128 pbi->tg_start =
3129 aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3130 pbi->tg_size =
3131 1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3132 }
3133#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003134}
3135
3136static int mem_get_varsize(const uint8_t *src, const int sz) {
3137 switch (sz) {
3138 case 1: return src[0];
3139 case 2: return mem_get_le16(src);
3140 case 3: return mem_get_le24(src);
3141 case 4: return mem_get_le32(src);
3142 default: assert("Invalid size" && 0); return -1;
3143 }
3144}
3145
3146#if CONFIG_EXT_TILE
3147// Reads the next tile returning its size and adjusting '*data' accordingly
3148// based on 'is_last'.
3149static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07003150 struct aom_internal_error_info *error_info,
3151 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003152 void *decrypt_state,
3153 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
3154 int tile_size_bytes, int col, int row) {
3155 size_t size;
3156
3157 size_t copy_size = 0;
3158 const uint8_t *copy_data = NULL;
3159
3160 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003161 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003162 "Truncated packet or corrupt tile length");
3163 if (decrypt_cb) {
3164 uint8_t be_data[4];
3165 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3166
3167 // Only read number of bytes in cm->tile_size_bytes.
3168 size = mem_get_varsize(be_data, tile_size_bytes);
3169 } else {
3170 size = mem_get_varsize(*data, tile_size_bytes);
3171 }
3172
3173 // The top bit indicates copy mode
3174 if ((size >> (tile_size_bytes * 8 - 1)) == 1) {
3175 // The remaining bits in the top byte signal the row offset
3176 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
3177
3178 // Currently, only use tiles in same column as reference tiles.
3179 copy_data = tile_buffers[row - offset][col].data;
3180 copy_size = tile_buffers[row - offset][col].size;
3181 size = 0;
3182 }
3183
3184 *data += tile_size_bytes;
3185
3186 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003187 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003188 "Truncated packet or corrupt tile size");
3189
3190 if (size > 0) {
3191 tile_buffers[row][col].data = *data;
3192 tile_buffers[row][col].size = size;
3193 } else {
3194 tile_buffers[row][col].data = copy_data;
3195 tile_buffers[row][col].size = copy_size;
3196 }
3197
3198 *data += size;
3199
3200 tile_buffers[row][col].raw_data_end = *data;
3201}
3202
3203static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003204 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003205 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003206 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003207 const int tile_cols = cm->tile_cols;
3208 const int tile_rows = cm->tile_rows;
3209 const int have_tiles = tile_cols * tile_rows > 1;
3210
3211 if (!have_tiles) {
3212 const uint32_t tile_size = data_end - data;
3213 tile_buffers[0][0].data = data;
3214 tile_buffers[0][0].size = tile_size;
3215 tile_buffers[0][0].raw_data_end = NULL;
3216 } else {
3217 // We locate only the tile buffers that are required, which are the ones
3218 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
3219 // need the last (bottom right) tile buffer, as we need to know where the
3220 // end of the compressed frame buffer is for proper superframe decoding.
3221
3222 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
3223 const uint8_t *const data_start = data;
3224
Yaowu Xuf883b422016-08-30 14:01:10 -07003225 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003226 const int single_row = pbi->dec_tile_row >= 0;
3227 const int tile_rows_start = single_row ? dec_tile_row : 0;
3228 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003229 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003230 const int single_col = pbi->dec_tile_col >= 0;
3231 const int tile_cols_start = single_col ? dec_tile_col : 0;
3232 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3233
3234 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
3235 const int tile_size_bytes = pbi->tile_size_bytes;
3236
3237 size_t tile_col_size;
3238 int r, c;
3239
3240 // Read tile column sizes for all columns (we need the last tile buffer)
3241 for (c = 0; c < tile_cols; ++c) {
3242 const int is_last = c == tile_cols - 1;
3243 if (!is_last) {
3244 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
3245 data += tile_col_size_bytes;
3246 tile_col_data_end[c] = data + tile_col_size;
3247 } else {
3248 tile_col_size = data_end - data;
3249 tile_col_data_end[c] = data_end;
3250 }
3251 data += tile_col_size;
3252 }
3253
3254 data = data_start;
3255
3256 // Read the required tile sizes.
3257 for (c = tile_cols_start; c < tile_cols_end; ++c) {
3258 const int is_last = c == tile_cols - 1;
3259
3260 if (c > 0) data = tile_col_data_end[c - 1];
3261
3262 if (!is_last) data += tile_col_size_bytes;
3263
3264 // Get the whole of the last column, otherwise stop at the required tile.
3265 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
3266 tile_buffers[r][c].col = c;
3267
3268 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3269 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3270 tile_size_bytes, c, r);
3271 }
3272 }
3273
3274 // If we have not read the last column, then read it to get the last tile.
3275 if (tile_cols_end != tile_cols) {
3276 c = tile_cols - 1;
3277
3278 data = tile_col_data_end[c - 1];
3279
3280 for (r = 0; r < tile_rows; ++r) {
3281 tile_buffers[r][c].col = c;
3282
3283 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3284 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3285 tile_size_bytes, c, r);
3286 }
3287 }
3288 }
3289}
3290#else
3291// Reads the next tile returning its size and adjusting '*data' accordingly
3292// based on 'is_last'.
3293static void get_tile_buffer(const uint8_t *const data_end,
3294 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07003295 struct aom_internal_error_info *error_info,
3296 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003297 void *decrypt_state, TileBufferDec *const buf) {
3298 size_t size;
3299
3300 if (!is_last) {
Yaowu Xu0a79a1b2017-02-17 13:04:54 -08003301 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003302 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003303 "Truncated packet or corrupt tile length");
3304
3305 if (decrypt_cb) {
3306 uint8_t be_data[4];
3307 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3308 size = mem_get_varsize(be_data, tile_size_bytes);
3309 } else {
3310 size = mem_get_varsize(*data, tile_size_bytes);
3311 }
3312 *data += tile_size_bytes;
3313
3314 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003315 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003316 "Truncated packet or corrupt tile size");
3317 } else {
3318 size = data_end - *data;
3319 }
3320
3321 buf->data = *data;
3322 buf->size = size;
3323
3324 *data += size;
3325}
3326
3327static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003328 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003329 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003330 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07003331#if CONFIG_TILE_GROUPS
3332 int r, c;
3333 const int tile_cols = cm->tile_cols;
3334 const int tile_rows = cm->tile_rows;
3335 int tc = 0;
3336 int first_tile_in_tg = 0;
3337 int hdr_offset;
3338 struct aom_read_bit_buffer rb_tg_hdr;
3339 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3340 const int num_tiles = tile_rows * tile_cols;
3341 const int num_bits = OD_ILOG(num_tiles) - 1;
3342 const int hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
3343 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
3344
3345 for (r = 0; r < tile_rows; ++r) {
3346 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07003347 TileBufferDec *const buf = &tile_buffers[r][c];
3348 hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
3349
3350 buf->col = c;
3351 if (hdr_offset) {
3352 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
3353 rb_tg_hdr.bit_offset = tg_size_bit_offset;
3354 if (num_tiles) {
3355 pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
3356 pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
3357 }
3358 }
3359 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
3360 data += hdr_offset;
Thomas Davies8fe64a32016-10-04 13:19:31 +01003361 get_tile_buffer(data_end, pbi->tile_size_bytes, 0, &pbi->common.error,
3362 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
Thomas Davies80188d12016-10-26 16:08:35 -07003363 }
3364 }
3365#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003366 int r, c;
3367 const int tile_cols = cm->tile_cols;
3368 const int tile_rows = cm->tile_rows;
3369
3370 for (r = 0; r < tile_rows; ++r) {
3371 for (c = 0; c < tile_cols; ++c) {
3372 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
3373 TileBufferDec *const buf = &tile_buffers[r][c];
3374 buf->col = c;
3375 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
3376 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
3377 }
3378 }
Thomas Davies80188d12016-10-26 16:08:35 -07003379#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003380}
3381#endif // CONFIG_EXT_TILE
3382
Yushin Cho77bba8d2016-11-04 16:36:56 -07003383#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003384static void daala_dec_init(AV1_COMMON *const cm, daala_dec_ctx *daala_dec,
Nathan E. Eggeab083972016-12-28 15:31:46 -05003385 aom_reader *r) {
3386 daala_dec->r = r;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003387 od_adapt_ctx_reset(&daala_dec->state.adapt, 0);
3388
Yushin Cho70669122016-12-08 09:53:14 -10003389 // TODO(yushin) : activity masking info needs be signaled by a bitstream
3390 daala_dec->use_activity_masking = AV1_PVQ_ENABLE_ACTIVITY_MASKING;
3391
Yushin Cho7a428ba2017-01-12 16:28:49 -08003392#if !CONFIG_DAALA_DIST
3393 daala_dec->use_activity_masking = 0;
3394#endif
3395
Yushin Cho70669122016-12-08 09:53:14 -10003396 if (daala_dec->use_activity_masking)
3397 daala_dec->qm = OD_HVS_QM;
3398 else
3399 daala_dec->qm = OD_FLAT_QM;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003400
3401 od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
3402 daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
Yushin Cho70669122016-12-08 09:53:14 -10003403
3404 if (daala_dec->use_activity_masking) {
3405 int pli;
3406 int use_masking = daala_dec->use_activity_masking;
3407 int segment_id = 0;
3408 int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
3409
3410 for (pli = 0; pli < MAX_MB_PLANE; pli++) {
3411 int i;
3412 int q;
3413
3414 q = qindex;
3415 if (q <= OD_DEFAULT_QMS[use_masking][0][pli].interp_q << OD_COEFF_SHIFT) {
3416 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3417 &OD_DEFAULT_QMS[use_masking][0][pli], NULL);
3418 } else {
3419 i = 0;
3420 while (OD_DEFAULT_QMS[use_masking][i + 1][pli].qm_q4 != NULL &&
3421 q > OD_DEFAULT_QMS[use_masking][i + 1][pli].interp_q
3422 << OD_COEFF_SHIFT) {
3423 i++;
3424 }
3425 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3426 &OD_DEFAULT_QMS[use_masking][i][pli],
3427 &OD_DEFAULT_QMS[use_masking][i + 1][pli]);
3428 }
3429 }
3430 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07003431}
Yushin Cho70669122016-12-08 09:53:14 -10003432#endif // #if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003433
Yaowu Xuf883b422016-08-30 14:01:10 -07003434static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003435 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003436 AV1_COMMON *const cm = &pbi->common;
3437 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003438 const int tile_cols = cm->tile_cols;
3439 const int tile_rows = cm->tile_rows;
3440 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07003441 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003442#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003443 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003444 const int single_row = pbi->dec_tile_row >= 0;
3445 const int tile_rows_start = single_row ? dec_tile_row : 0;
3446 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003447 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003448 const int single_col = pbi->dec_tile_col >= 0;
3449 const int tile_cols_start = single_col ? dec_tile_col : 0;
3450 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3451 const int inv_col_order = pbi->inv_tile_order && !single_col;
3452 const int inv_row_order = pbi->inv_tile_order && !single_row;
3453#else
3454 const int tile_rows_start = 0;
3455 const int tile_rows_end = tile_rows;
3456 const int tile_cols_start = 0;
3457 const int tile_cols_end = tile_cols;
3458 const int inv_col_order = pbi->inv_tile_order;
3459 const int inv_row_order = pbi->inv_tile_order;
3460#endif // CONFIG_EXT_TILE
3461 int tile_row, tile_col;
3462
3463#if CONFIG_ENTROPY
3464 cm->do_subframe_update = n_tiles == 1;
3465#endif // CONFIG_ENTROPY
3466
3467 if (cm->lf.filter_level && !cm->skip_loop_filter &&
3468 pbi->lf_worker.data1 == NULL) {
3469 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07003470 aom_memalign(32, sizeof(LFWorkerData)));
3471 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003472 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003473 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003474 "Loop filter thread creation failed");
3475 }
3476 }
3477
3478 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3479 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3480 // Be sure to sync as we might be resuming after a failed frame decode.
3481 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003482 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
3483 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003484 }
3485
3486 assert(tile_rows <= MAX_TILE_ROWS);
3487 assert(tile_cols <= MAX_TILE_COLS);
3488
3489 get_tile_buffers(pbi, data, data_end, tile_buffers);
3490
3491 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003492 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003493 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003494 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003495 pbi->allocated_tiles = n_tiles;
3496 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003497#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003498 if (pbi->acct_enabled) {
3499 aom_accounting_reset(&pbi->accounting);
3500 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003501#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003502 // Load all tile information into tile_data.
3503 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3504 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3505 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
3506 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
3507
3508 td->cm = cm;
3509 td->xd = pbi->mb;
3510 td->xd.corrupted = 0;
3511 td->xd.counts =
3512 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3513 ? &cm->counts
3514 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003515 av1_zero(td->dqcoeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003516#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003517 av1_zero(td->pvq_ref_coeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003518#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003519 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003520 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003521 &td->bit_reader,
3522#if CONFIG_ANS && ANS_MAX_SYMBOLS
3523 1 << cm->ans_window_size_log2,
3524#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3525 pbi->decrypt_cb, pbi->decrypt_state);
Michael Bebenita6048d052016-08-25 14:40:54 -07003526#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003527 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003528 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003529 } else {
David Barkerd971f402016-10-25 13:52:07 +01003530 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003531 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003532#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003533 av1_init_macroblockd(cm, &td->xd,
3534#if CONFIG_PVQ
3535 td->pvq_ref_coeff,
3536#endif
3537 td->dqcoeff);
3538#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003539 daala_dec_init(cm, &td->xd.daala_dec, &td->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003540#endif
Thomas Daviesf77d4ad2017-01-10 18:55:42 +00003541#if CONFIG_EC_ADAPT
3542 // Initialise the tile context from the frame context
3543 td->tctx = *cm->fc;
3544 td->xd.tile_ctx = &td->tctx;
3545#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003546#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003547 td->xd.plane[0].color_index_map = td->color_index_map[0];
3548 td->xd.plane[1].color_index_map = td->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003549#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003550 }
3551 }
3552
3553 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3554 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
3555 int mi_row = 0;
3556 TileInfo tile_info;
3557
Yaowu Xuf883b422016-08-30 14:01:10 -07003558 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003559
3560 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3561 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
3562 TileData *const td = pbi->tile_data + tile_cols * row + col;
Michael Bebenita6048d052016-08-25 14:40:54 -07003563#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003564 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003565 td->bit_reader.accounting->last_tell_frac =
3566 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003567 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003568#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003569
Yaowu Xuf883b422016-08-30 14:01:10 -07003570 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003571
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003572#if CONFIG_DEPENDENT_HORZTILES
3573 if (!cm->dependent_horz_tiles || tile_row == 0) {
3574 av1_zero_above_context(cm, tile_info.mi_col_start,
3575 tile_info.mi_col_end);
3576 }
3577#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003578 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003579#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003580
3581 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
3582 mi_row += cm->mib_size) {
3583 int mi_col;
3584
Yaowu Xuf883b422016-08-30 14:01:10 -07003585 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003586
3587 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
3588 mi_col += cm->mib_size) {
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003589 av1_update_boundary_info(cm, &tile_info, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003590 decode_partition(pbi, &td->xd,
3591#if CONFIG_SUPERTX
3592 0,
3593#endif // CONFIG_SUPERTX
3594 mi_row, mi_col, &td->bit_reader, cm->sb_size,
3595 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003596#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3597 detoken_and_recon_sb(pbi, &td->xd, mi_row, mi_col, &td->bit_reader,
3598 cm->sb_size);
3599#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003600 }
3601 pbi->mb.corrupted |= td->xd.corrupted;
3602 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003603 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003604 "Failed to decode tile data");
3605#if CONFIG_ENTROPY
3606 if (cm->do_subframe_update &&
3607 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
hui su0161a932017-01-24 14:12:11 -08003608 const int mi_rows_per_update =
3609 MI_SIZE * AOMMAX(cm->mi_rows / MI_SIZE / COEF_PROBS_BUFS, 1);
3610 if ((mi_row + MI_SIZE) % mi_rows_per_update == 0 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003611 mi_row + MI_SIZE < cm->mi_rows &&
3612 cm->coef_probs_update_idx < COEF_PROBS_BUFS - 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003613 av1_partial_adapt_probs(cm, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003614 ++cm->coef_probs_update_idx;
3615 }
3616 }
3617#endif // CONFIG_ENTROPY
3618 }
3619 }
3620
3621 assert(mi_row > 0);
3622
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003623// when Parallel deblocking is enabled, deblocking should not
3624// be interleaved with decoding. Instead, deblocking should be done
3625// after the entire frame is decoded.
3626#if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003627 // Loopfilter one tile row.
3628 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3629 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003630 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003631 const int lf_end = tile_info.mi_row_end - cm->mib_size;
3632
3633 // Delay the loopfilter if the first tile row is only
3634 // a single superblock high.
3635 if (lf_end <= 0) continue;
3636
3637 // Decoding has completed. Finish up the loop filter in this thread.
3638 if (tile_info.mi_row_end >= cm->mi_rows) continue;
3639
3640 winterface->sync(&pbi->lf_worker);
3641 lf_data->start = lf_start;
3642 lf_data->stop = lf_end;
3643 if (pbi->max_threads > 1) {
3644 winterface->launch(&pbi->lf_worker);
3645 } else {
3646 winterface->execute(&pbi->lf_worker);
3647 }
3648 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003649#endif // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003650
3651 // After loopfiltering, the last 7 row pixels in each superblock row may
3652 // still be changed by the longest loopfilter of the next superblock row.
3653 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003654 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003655 }
3656
3657#if CONFIG_VAR_TX
3658 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003659 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
3660 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003661#else
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003662#if CONFIG_PARALLEL_DEBLOCKING
3663 // Loopfilter all rows in the frame in the frame.
3664 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3665 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3666 winterface->sync(&pbi->lf_worker);
3667 lf_data->start = 0;
3668 lf_data->stop = cm->mi_rows;
3669 winterface->execute(&pbi->lf_worker);
3670 }
3671#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003672 // Loopfilter remaining rows in the frame.
3673 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3674 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3675 winterface->sync(&pbi->lf_worker);
3676 lf_data->start = lf_data->stop;
3677 lf_data->stop = cm->mi_rows;
3678 winterface->execute(&pbi->lf_worker);
3679 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003680#endif // CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003681#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003682 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003683 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003684
3685#if CONFIG_EXT_TILE
3686 if (n_tiles == 1) {
3687#if CONFIG_ANS
3688 return data_end;
3689#else
3690 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07003691 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003692#endif // CONFIG_ANS
3693 } else {
3694 // Return the end of the last tile buffer
3695 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3696 }
3697#else
3698#if CONFIG_ANS
3699 return data_end;
3700#else
3701 {
3702 // Get last tile data.
3703 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003704 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003705 }
3706#endif // CONFIG_ANS
3707#endif // CONFIG_EXT_TILE
3708}
3709
3710static int tile_worker_hook(TileWorkerData *const tile_data,
3711 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003712 AV1Decoder *const pbi = tile_data->pbi;
3713 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003714 int mi_row, mi_col;
3715
3716 if (setjmp(tile_data->error_info.jmp)) {
3717 tile_data->error_info.setjmp = 0;
3718 tile_data->xd.corrupted = 1;
3719 return 0;
3720 }
3721
3722 tile_data->error_info.setjmp = 1;
3723 tile_data->xd.error_info = &tile_data->error_info;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003724#if CONFIG_DEPENDENT_HORZTILES
3725 if (!cm->dependent_horz_tiles) {
3726 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
3727 }
3728#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003729 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003730#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003731
3732 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
3733 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003734 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003735
3736 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3737 mi_col += cm->mib_size) {
3738 decode_partition(pbi, &tile_data->xd,
3739#if CONFIG_SUPERTX
3740 0,
3741#endif
3742 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
3743 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003744#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3745 detoken_and_recon_sb(pbi, &tile_data->xd, mi_row, mi_col,
3746 &tile_data->bit_reader, cm->sb_size);
3747#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003748 }
3749 }
3750 return !tile_data->xd.corrupted;
3751}
3752
3753// sorts in descending order
3754static int compare_tile_buffers(const void *a, const void *b) {
3755 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
3756 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
3757 return (int)(buf2->size - buf1->size);
3758}
3759
Yaowu Xuf883b422016-08-30 14:01:10 -07003760static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003761 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003762 AV1_COMMON *const cm = &pbi->common;
3763 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003764 const int tile_cols = cm->tile_cols;
3765 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003766 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
clang-format67948d32016-09-07 22:40:40 -07003767 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003768#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003769 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003770 const int single_row = pbi->dec_tile_row >= 0;
3771 const int tile_rows_start = single_row ? dec_tile_row : 0;
3772 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003773 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003774 const int single_col = pbi->dec_tile_col >= 0;
3775 const int tile_cols_start = single_col ? dec_tile_col : 0;
3776 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3777#else
3778 const int tile_rows_start = 0;
3779 const int tile_rows_end = tile_rows;
3780 const int tile_cols_start = 0;
3781 const int tile_cols_end = tile_cols;
3782#endif // CONFIG_EXT_TILE
3783 int tile_row, tile_col;
3784 int i;
3785
3786#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3787 int final_worker = -1;
3788#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3789
3790 assert(tile_rows <= MAX_TILE_ROWS);
3791 assert(tile_cols <= MAX_TILE_COLS);
3792
3793 assert(tile_cols * tile_rows > 1);
3794
Yaowu Xuc27fc142016-08-22 16:08:15 -07003795 // TODO(jzern): See if we can remove the restriction of passing in max
3796 // threads to the decoder.
3797 if (pbi->num_tile_workers == 0) {
3798 const int num_threads = pbi->max_threads & ~1;
3799 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07003800 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003801 // Ensure tile data offsets will be properly aligned. This may fail on
3802 // platforms without DECLARE_ALIGNED().
3803 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3804 CHECK_MEM_ERROR(
3805 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003806 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003807 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07003808 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003809 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003810 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003811 ++pbi->num_tile_workers;
3812
3813 winterface->init(worker);
3814 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003815 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003816 "Tile decoder thread creation failed");
3817 }
3818 }
3819 }
3820
3821 // Reset tile decoding hook
3822 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003823 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003824 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003825 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003826 worker->data1 = &pbi->tile_worker_data[i];
3827 worker->data2 = &pbi->tile_worker_info[i];
3828 }
3829
3830 // Initialize thread frame counts.
3831 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3832 for (i = 0; i < num_workers; ++i) {
3833 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003834 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003835 }
3836 }
3837
3838 // Load tile data into tile_buffers
3839 get_tile_buffers(pbi, data, data_end, tile_buffers);
3840
3841 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3842 // Sort the buffers in this tile row based on size in descending order.
3843 qsort(&tile_buffers[tile_row][tile_cols_start],
3844 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
3845 compare_tile_buffers);
3846
3847 // Rearrange the tile buffers in this tile row such that per-tile group
3848 // the largest, and presumably the most difficult tile will be decoded in
3849 // the main thread. This should help minimize the number of instances
3850 // where the main thread is waiting for a worker to complete.
3851 {
3852 int group_start;
3853 for (group_start = tile_cols_start; group_start < tile_cols_end;
3854 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003855 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003856 const TileBufferDec largest = tile_buffers[tile_row][group_start];
3857 memmove(&tile_buffers[tile_row][group_start],
3858 &tile_buffers[tile_row][group_start + 1],
3859 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
3860 tile_buffers[tile_row][group_end - 1] = largest;
3861 }
3862 }
3863
3864 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
3865 // Launch workers for individual columns
3866 for (i = 0; i < num_workers && tile_col < tile_cols_end;
3867 ++i, ++tile_col) {
3868 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07003869 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003870 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
3871 TileInfo *const tile_info = (TileInfo *)worker->data2;
3872
3873 twd->pbi = pbi;
3874 twd->xd = pbi->mb;
3875 twd->xd.corrupted = 0;
3876 twd->xd.counts =
3877 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3878 ? &twd->counts
3879 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003880 av1_zero(twd->dqcoeff);
3881 av1_tile_init(tile_info, cm, tile_row, buf->col);
3882 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003883 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003884 &twd->bit_reader,
3885#if CONFIG_ANS && ANS_MAX_SYMBOLS
3886 1 << cm->ans_window_size_log2,
3887#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3888 pbi->decrypt_cb, pbi->decrypt_state);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003889 av1_init_macroblockd(cm, &twd->xd,
3890#if CONFIG_PVQ
3891 twd->pvq_ref_coeff,
3892#endif
3893 twd->dqcoeff);
3894#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003895 daala_dec_init(cm, &twd->xd.daala_dec, &twd->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003896#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003897#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003898 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
3899 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003900#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003901
3902 worker->had_error = 0;
3903 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
3904 winterface->execute(worker);
3905 } else {
3906 winterface->launch(worker);
3907 }
3908
3909#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3910 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
3911 final_worker = i;
3912 }
3913#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3914 }
3915
3916 // Sync all workers
3917 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003918 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003919 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07003920 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07003921 // info in cm. Additionally once the threads have been synced and an
3922 // error is detected, there's no point in continuing to decode tiles.
3923 pbi->mb.corrupted |= !winterface->sync(worker);
3924 }
3925 }
3926 }
3927
3928 // Accumulate thread frame counts.
3929 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3930 for (i = 0; i < num_workers; ++i) {
3931 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003932 av1_accumulate_frame_counts(&cm->counts, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003933 }
3934 }
3935
3936#if CONFIG_EXT_TILE
3937 // Return the end of the last tile buffer
3938 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3939#else
3940#if CONFIG_ANS
3941 return data_end;
3942#else
3943 assert(final_worker != -1);
3944 {
3945 TileWorkerData *const twd =
3946 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003947 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003948 }
3949#endif // CONFIG_ANS
3950#endif // CONFIG_EXT_TILE
3951}
3952
3953static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003954 AV1_COMMON *const cm = (AV1_COMMON *)data;
3955 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003956}
3957
Yaowu Xuf883b422016-08-30 14:01:10 -07003958static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3959 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003960 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003961 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003962 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003963 cm->bit_depth = AOM_BITS_8;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003964 }
3965
Yaowu Xuf883b422016-08-30 14:01:10 -07003966#if CONFIG_AOM_HIGHBITDEPTH
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003967 if (cm->bit_depth > AOM_BITS_8) {
3968 cm->use_highbitdepth = 1;
3969 } else {
3970#if CONFIG_AOM_LOWBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003971 cm->use_highbitdepth = 0;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003972#else
3973 cm->use_highbitdepth = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003974#endif
3975 }
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003976#endif
3977
Yaowu Xuf883b422016-08-30 14:01:10 -07003978 cm->color_space = aom_rb_read_literal(rb, 3);
3979 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003980 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07003981 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003982 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003983 cm->subsampling_x = aom_rb_read_bit(rb);
3984 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003985 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003986 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003987 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07003988 if (aom_rb_read_bit(rb))
3989 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003990 "Reserved bit set");
3991 } else {
3992 cm->subsampling_y = cm->subsampling_x = 1;
3993 }
3994 } else {
3995 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
3996 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
3997 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
3998 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003999 if (aom_rb_read_bit(rb))
4000 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004001 "Reserved bit set");
4002 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004003 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004004 "4:4:4 color not supported in profile 0 or 2");
4005 }
4006 }
4007}
4008
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004009#if CONFIG_REFERENCE_BUFFER
4010void read_sequence_header(SequenceHeader *seq_params) {
4011 /* Placeholder for actually reading from the bitstream */
4012 seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
4013 seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
4014 seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
4015}
4016#endif
4017
Yaowu Xuf883b422016-08-30 14:01:10 -07004018static size_t read_uncompressed_header(AV1Decoder *pbi,
4019 struct aom_read_bit_buffer *rb) {
4020 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004021 MACROBLOCKD *const xd = &pbi->mb;
4022 BufferPool *const pool = cm->buffer_pool;
4023 RefCntBuffer *const frame_bufs = pool->frame_bufs;
4024 int i, mask, ref_index = 0;
4025 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004026
4027#if CONFIG_REFERENCE_BUFFER
4028 /* TODO: Move outside frame loop or inside key-frame branch */
4029 read_sequence_header(&pbi->seq_params);
4030#endif
4031
Yaowu Xuc27fc142016-08-22 16:08:15 -07004032 cm->last_frame_type = cm->frame_type;
4033 cm->last_intra_only = cm->intra_only;
4034
4035#if CONFIG_EXT_REFS
4036 // NOTE: By default all coded frames to be used as a reference
4037 cm->is_reference_frame = 1;
4038#endif // CONFIG_EXT_REFS
4039
Yaowu Xuf883b422016-08-30 14:01:10 -07004040 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
4041 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004042 "Invalid frame marker");
4043
Yaowu Xuf883b422016-08-30 14:01:10 -07004044 cm->profile = av1_read_profile(rb);
4045#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004046 if (cm->profile >= MAX_PROFILES)
Yaowu Xuf883b422016-08-30 14:01:10 -07004047 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004048 "Unsupported bitstream profile");
4049#else
4050 if (cm->profile >= PROFILE_2)
Yaowu Xuf883b422016-08-30 14:01:10 -07004051 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004052 "Unsupported bitstream profile");
4053#endif
4054
Yaowu Xuf883b422016-08-30 14:01:10 -07004055 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004056
4057 if (cm->show_existing_frame) {
Yaowu Xu415ba932016-12-27 11:17:32 -08004058 // Show an existing frame directly.
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004059 const int existing_frame_idx = aom_rb_read_literal(rb, 3);
4060 const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
Yaowu Xu415ba932016-12-27 11:17:32 -08004061#if CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004062 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004063 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4064 int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004065 /* Compare display_frame_id with ref_frame_id and check valid for
4066 * referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004067 if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
4068 cm->valid_for_referencing[existing_frame_idx] == 0)
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004069 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4070 "Reference buffer frame ID mismatch");
4071 }
4072#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004073 lock_buffer_pool(pool);
4074 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
4075 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07004076 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004077 "Buffer %d does not contain a decoded frame",
4078 frame_to_show);
4079 }
4080 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
4081 unlock_buffer_pool(pool);
4082
4083 cm->lf.filter_level = 0;
4084 cm->show_frame = 1;
4085 pbi->refresh_frame_flags = 0;
4086
4087 if (cm->frame_parallel_decode) {
4088 for (i = 0; i < REF_FRAMES; ++i)
4089 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
4090 }
4091
4092 return 0;
4093 }
4094
Yaowu Xuf883b422016-08-30 14:01:10 -07004095 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
4096 cm->show_frame = aom_rb_read_bit(rb);
4097 cm->error_resilient_mode = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004098#if CONFIG_REFERENCE_BUFFER
4099 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004100 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4101 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4102 int prev_frame_id = 0;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004103 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004104 prev_frame_id = cm->current_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004105 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004106 cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004107
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004108 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004109 int diff_frame_id;
4110 if (cm->current_frame_id > prev_frame_id) {
4111 diff_frame_id = cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004112 } else {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004113 diff_frame_id =
4114 (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004115 }
4116 /* Check current_frame_id for conformance */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004117 if (prev_frame_id == cm->current_frame_id ||
4118 diff_frame_id >= (1 << (frame_id_length - 1))) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004119 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4120 "Invalid value of current_frame_id");
4121 }
4122 }
4123 /* Check if some frames need to be marked as not valid for referencing */
4124 for (i = 0; i < REF_FRAMES; i++) {
4125 if (cm->frame_type == KEY_FRAME) {
4126 cm->valid_for_referencing[i] = 0;
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004127 } else if (cm->current_frame_id - (1 << diff_len) > 0) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004128 if (cm->ref_frame_id[i] > cm->current_frame_id ||
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004129 cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004130 cm->valid_for_referencing[i] = 0;
4131 } else {
4132 if (cm->ref_frame_id[i] > cm->current_frame_id &&
4133 cm->ref_frame_id[i] <
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004134 (1 << frame_id_length) + cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004135 cm->valid_for_referencing[i] = 0;
4136 }
4137 }
4138 }
4139#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004140 if (cm->frame_type == KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004141 if (!av1_read_sync_code(rb))
4142 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004143 "Invalid frame sync code");
4144
4145 read_bitdepth_colorspace_sampling(cm, rb);
4146 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
4147
4148 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4149 cm->frame_refs[i].idx = INVALID_IDX;
4150 cm->frame_refs[i].buf = NULL;
4151 }
4152
4153 setup_frame_size(cm, rb);
4154 if (pbi->need_resync) {
4155 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4156 pbi->need_resync = 0;
4157 }
Alex Converseeb780e72016-12-13 12:46:41 -08004158#if CONFIG_ANS && ANS_MAX_SYMBOLS
4159 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4160#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
Urvang Joshib100db72016-10-12 16:28:56 -07004161#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004162 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004163#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004164 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004165 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004166#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004167 if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004168#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004169 if (cm->error_resilient_mode) {
4170 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
4171 } else {
4172 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004173 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004174 ? RESET_FRAME_CONTEXT_ALL
4175 : RESET_FRAME_CONTEXT_CURRENT;
4176 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004177 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004178 ? RESET_FRAME_CONTEXT_CURRENT
4179 : RESET_FRAME_CONTEXT_NONE;
4180 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07004181 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004182 ? RESET_FRAME_CONTEXT_ALL
4183 : RESET_FRAME_CONTEXT_CURRENT;
4184 }
4185 }
4186
4187 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004188 if (!av1_read_sync_code(rb))
4189 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004190 "Invalid frame sync code");
4191
4192 read_bitdepth_colorspace_sampling(cm, rb);
4193
Yaowu Xuf883b422016-08-30 14:01:10 -07004194 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004195 setup_frame_size(cm, rb);
4196 if (pbi->need_resync) {
4197 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4198 pbi->need_resync = 0;
4199 }
Alex Converseeb780e72016-12-13 12:46:41 -08004200#if CONFIG_ANS && ANS_MAX_SYMBOLS
4201 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4202#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004203 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Yaowu Xuf883b422016-08-30 14:01:10 -07004204 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004205
4206#if CONFIG_EXT_REFS
4207 if (!pbi->refresh_frame_flags) {
4208 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
4209 // will not be used as a reference
4210 cm->is_reference_frame = 0;
4211 }
4212#endif // CONFIG_EXT_REFS
4213
4214 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004215 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004216 const int idx = cm->ref_frame_map[ref];
4217 RefBuffer *const ref_frame = &cm->frame_refs[i];
4218 ref_frame->idx = idx;
4219 ref_frame->buf = &frame_bufs[idx].buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07004220 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004221#if CONFIG_REFERENCE_BUFFER
4222 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004223 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4224 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4225 int delta_frame_id_minus1 = aom_rb_read_literal(rb, diff_len);
4226 int ref_frame_id =
4227 ((cm->current_frame_id - (delta_frame_id_minus1 + 1) +
4228 (1 << frame_id_length)) %
4229 (1 << frame_id_length));
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004230 /* Compare values derived from delta_frame_id_minus1 and
4231 * refresh_frame_flags. Also, check valid for referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004232 if (ref_frame_id != cm->ref_frame_id[ref] ||
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004233 cm->valid_for_referencing[ref] == 0)
4234 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4235 "Reference buffer frame ID mismatch");
4236 }
4237#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004238 }
4239
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004240#if CONFIG_FRAME_SIZE
4241 if (cm->error_resilient_mode == 0) {
4242 setup_frame_size_with_refs(cm, rb);
4243 } else {
4244 setup_frame_size(cm, rb);
4245 }
4246#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004247 setup_frame_size_with_refs(cm, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004248#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004249
Yaowu Xuf883b422016-08-30 14:01:10 -07004250 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Angie Chiang5678ad92016-11-21 09:38:40 -08004251 cm->interp_filter = read_frame_interp_filter(rb);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004252#if CONFIG_TEMPMV_SIGNALING
4253 if (!cm->error_resilient_mode) {
4254 cm->use_prev_frame_mvs = aom_rb_read_bit(rb);
4255 }
4256#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004257 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4258 RefBuffer *const ref_buf = &cm->frame_refs[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07004259#if CONFIG_AOM_HIGHBITDEPTH
4260 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004261 &ref_buf->sf, ref_buf->buf->y_crop_width,
4262 ref_buf->buf->y_crop_height, cm->width, cm->height,
4263 cm->use_highbitdepth);
4264#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004265 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004266 &ref_buf->sf, ref_buf->buf->y_crop_width,
4267 ref_buf->buf->y_crop_height, cm->width, cm->height);
4268#endif
4269 }
4270 }
4271 }
Fangwen Fu8d164de2016-12-14 13:40:54 -08004272#if CONFIG_TEMPMV_SIGNALING
4273 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
4274#endif
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004275
4276#if CONFIG_REFERENCE_BUFFER
4277 if (pbi->seq_params.frame_id_numbers_present_flag) {
4278 /* If bitmask is set, update reference frame id values and
4279 mark frames as valid for reference */
4280 int refresh_frame_flags =
4281 cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
4282 for (i = 0; i < REF_FRAMES; i++) {
4283 if ((refresh_frame_flags >> i) & 1) {
4284 cm->ref_frame_id[i] = cm->current_frame_id;
4285 cm->valid_for_referencing[i] = 1;
4286 }
4287 }
4288 }
4289#endif
4290
Yaowu Xuf883b422016-08-30 14:01:10 -07004291#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004292 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
4293#endif
4294 get_frame_new_buffer(cm)->color_space = cm->color_space;
4295 get_frame_new_buffer(cm)->color_range = cm->color_range;
4296 get_frame_new_buffer(cm)->render_width = cm->render_width;
4297 get_frame_new_buffer(cm)->render_height = cm->render_height;
4298
4299 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004300 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004301 "Keyframe / intra-only frame required to reset decoder"
4302 " state");
4303 }
4304
4305 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004306 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004307 ? REFRESH_FRAME_CONTEXT_FORWARD
4308 : REFRESH_FRAME_CONTEXT_BACKWARD;
4309 } else {
4310 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
4311 }
4312
Yaowu Xuf883b422016-08-30 14:01:10 -07004313 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07004314 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07004315 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004316
4317 // Generate next_ref_frame_map.
4318 lock_buffer_pool(pool);
4319 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
4320 if (mask & 1) {
4321 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
4322 ++frame_bufs[cm->new_fb_idx].ref_count;
4323 } else {
4324 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4325 }
4326 // Current thread holds the reference frame.
4327 if (cm->ref_frame_map[ref_index] >= 0)
4328 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4329 ++ref_index;
4330 }
4331
4332 for (; ref_index < REF_FRAMES; ++ref_index) {
4333 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4334
4335 // Current thread holds the reference frame.
4336 if (cm->ref_frame_map[ref_index] >= 0)
4337 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4338 }
4339 unlock_buffer_pool(pool);
4340 pbi->hold_ref_buf = 1;
4341
4342 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07004343 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004344
4345#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07004346 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004347#else
4348 set_sb_size(cm, BLOCK_64X64);
4349#endif // CONFIG_EXT_PARTITION
4350
4351 setup_loopfilter(cm, rb);
Jean-Marc Valin01435132017-02-18 14:12:53 -05004352#if CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07004353 setup_dering(cm, rb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004354 setup_clpf(pbi, rb);
4355#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004356#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee874d36d2016-12-14 16:53:17 -08004357 av1_alloc_restoration_buffers(cm);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004358 decode_restoration_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004359#endif // CONFIG_LOOP_RESTORATION
4360 setup_quantization(cm, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004361#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004362 xd->bd = (int)cm->bit_depth;
4363#endif
4364
4365#if CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004366 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004367 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4368 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4369 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4370 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
4371 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4372 }
4373#endif // CONFIG_ENTROPY
4374
4375 setup_segmentation(cm, rb);
4376
Arild Fuldseth07441162016-08-15 15:07:52 +02004377#if CONFIG_DELTA_Q
4378 {
4379 struct segmentation *const seg = &cm->seg;
4380 int segment_quantizer_active = 0;
4381 for (i = 0; i < MAX_SEGMENTS; i++) {
4382 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
4383 segment_quantizer_active = 1;
4384 }
4385 }
4386
Thomas Daviesf6936102016-09-05 16:51:31 +01004387 cm->delta_q_res = 1;
Arild Fuldseth07441162016-08-15 15:07:52 +02004388 if (segment_quantizer_active == 0) {
4389 cm->delta_q_present_flag = aom_rb_read_bit(rb);
4390 } else {
4391 cm->delta_q_present_flag = 0;
4392 }
4393 if (cm->delta_q_present_flag) {
4394 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01004395 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Arild Fuldseth07441162016-08-15 15:07:52 +02004396 }
4397 }
4398#endif
4399
Urvang Joshi454280d2016-10-14 16:51:44 -07004400 for (i = 0; i < MAX_SEGMENTS; ++i) {
4401 const int qindex = cm->seg.enabled
4402 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
4403 : cm->base_qindex;
4404 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
4405 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4406 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004407 }
4408
4409 setup_segmentation_dequant(cm);
Yue Cheneeacc4c2017-01-17 17:29:17 -08004410 cm->tx_mode = read_tx_mode(cm, xd, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004411 cm->reference_mode = read_frame_reference_mode(cm, rb);
4412
Sarah Parkere68a3e42017-02-16 14:03:24 -08004413#if CONFIG_EXT_TX
4414 cm->reduced_tx_set_used = aom_rb_read_bit(rb);
4415#endif // CONFIG_EXT_TX
4416
Yaowu Xuc27fc142016-08-22 16:08:15 -07004417 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004418 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004419
4420 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07004421 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004422 "Invalid header size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004423 return sz;
4424}
4425
4426#if CONFIG_EXT_TX
Thomas9ac55082016-09-23 18:04:17 +01004427#if !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuf883b422016-08-30 14:01:10 -07004428static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004429 int i, j, k;
4430 int s;
4431 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004432 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004433 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4434 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004435 for (j = 0; j < num_ext_tx_set[ext_tx_set_type_inter[s]] - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004436 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004437 }
4438 }
4439 }
4440
4441 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004442 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004443 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4444 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
4445 for (j = 0; j < INTRA_MODES; ++j)
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004446 for (k = 0; k < num_ext_tx_set[ext_tx_set_type_intra[s]] - 1; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004447 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
4448 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004449 }
4450 }
4451 }
4452}
Thomas9ac55082016-09-23 18:04:17 +01004453#endif // !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004454#else
4455
Yaowu Xuc27fc142016-08-22 16:08:15 -07004456#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004457#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07004458static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004459 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -07004460 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004461 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
Jingning Hanfeb517c2016-12-21 16:02:07 -08004462 for (j = TX_8X8; j < TX_SIZES; ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004463 av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004464 }
4465 }
4466 }
4467}
4468#endif // CONFIG_SUPERTX
4469
4470#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +00004471static void read_global_motion_params(WarpedMotionParams *params,
Yaowu Xuf883b422016-08-30 14:01:10 -07004472 aom_prob *probs, aom_reader *r) {
David Barkercf3d0b02016-11-10 10:14:49 +00004473 TransformationType type =
Michael Bebenita6048d052016-08-25 14:40:54 -07004474 aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004475 set_default_gmparams(params);
David Barkercf3d0b02016-11-10 10:14:49 +00004476 params->wmtype = type;
4477 switch (type) {
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004478 case HOMOGRAPHY:
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004479 case HORTRAPEZOID:
4480 case VERTRAPEZOID:
4481 if (type != HORTRAPEZOID)
4482 params->wmmat[6] =
4483 aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
4484 GM_ROW3HOMO_DECODE_FACTOR;
4485 if (type != VERTRAPEZOID)
4486 params->wmmat[7] =
4487 aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
4488 GM_ROW3HOMO_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004489 case AFFINE:
4490 case ROTZOOM:
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004491 params->wmmat[2] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004492 GM_ALPHA_DECODE_FACTOR +
David Barkercf3d0b02016-11-10 10:14:49 +00004493 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004494 if (type != VERTRAPEZOID)
4495 params->wmmat[3] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004496 GM_ALPHA_DECODE_FACTOR;
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004497 if (type >= AFFINE) {
4498 if (type != HORTRAPEZOID)
4499 params->wmmat[4] =
4500 aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4501 GM_ALPHA_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004502 params->wmmat[5] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4503 GM_ALPHA_DECODE_FACTOR +
4504 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004505 } else {
David Barkercf3d0b02016-11-10 10:14:49 +00004506 params->wmmat[4] = -params->wmmat[3];
4507 params->wmmat[5] = params->wmmat[2];
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004508 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004509 // fallthrough intended
David Barkercf3d0b02016-11-10 10:14:49 +00004510 case TRANSLATION:
4511 params->wmmat[0] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4512 GM_TRANS_DECODE_FACTOR;
4513 params->wmmat[1] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4514 GM_TRANS_DECODE_FACTOR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004515 break;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004516 case IDENTITY: break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004517 default: assert(0);
4518 }
4519}
4520
Yaowu Xuf883b422016-08-30 14:01:10 -07004521static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004522 int frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004523 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
4524 read_global_motion_params(&cm->global_motion[frame],
4525 cm->fc->global_motion_types_prob, r);
Sarah Parkere5299862016-08-16 14:57:37 -07004526 /*
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004527 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4528 frame, cm->current_video_frame, cm->show_frame,
David Barkercf3d0b02016-11-10 10:14:49 +00004529 cm->global_motion[frame].wmmat[0],
4530 cm->global_motion[frame].wmmat[1],
4531 cm->global_motion[frame].wmmat[2],
4532 cm->global_motion[frame].wmmat[3]);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004533 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004534 }
4535}
4536#endif // CONFIG_GLOBAL_MOTION
4537
Yaowu Xuf883b422016-08-30 14:01:10 -07004538static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004539 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004540 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004541#if CONFIG_SUPERTX
4542 MACROBLOCKD *const xd = &pbi->mb;
4543#endif
4544 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07004545 aom_reader r;
Yaowu Xu8af861b2016-11-01 12:12:11 -07004546 int k, i;
Yaowu Xud0af64f2016-11-17 12:50:47 -08004547#if !CONFIG_EC_ADAPT || CONFIG_EXT_INTRA
Yaowu Xu8af861b2016-11-01 12:12:11 -07004548 int j;
4549#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004550
Alex Converse2cdf0d82016-12-13 13:53:09 -08004551#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08004552 r.window_size = 1 << cm->ans_window_size_log2;
Alex Converse2cdf0d82016-12-13 13:53:09 -08004553#endif
Alex Converse346440b2017-01-03 13:47:37 -08004554 if (aom_reader_init(&r, data, partition_size, pbi->decrypt_cb,
4555 pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07004556 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004557 "Failed to allocate bool decoder 0");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004558
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004559#if CONFIG_LOOP_RESTORATION
4560 decode_restoration(cm, &r);
4561#endif
4562
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004563#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -07004564 if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004565#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004566
Yushin Cho77bba8d2016-11-04 16:36:56 -07004567#if !CONFIG_PVQ
Alex Conversea9598cd2017-02-03 14:18:05 -08004568#if !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004569 read_coef_probs(fc, cm->tx_mode, &r);
Thomas Daviesfc1598a2017-01-13 17:07:25 +00004570#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004571
4572#if CONFIG_VAR_TX
4573 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004574 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
Yushin Cho77bba8d2016-11-04 16:36:56 -07004575#endif // CONFIG_VAR_TX
4576#endif // !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004577 for (k = 0; k < SKIP_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004578 av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004579
Thomas Daviesf6936102016-09-05 16:51:31 +01004580#if CONFIG_DELTA_Q
4581 for (k = 0; k < DELTA_Q_CONTEXTS; ++k)
4582 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
4583#endif
4584
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004585#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004586 if (cm->seg.enabled && cm->seg.update_map) {
4587 if (cm->seg.temporal_update) {
4588 for (k = 0; k < PREDICTION_PROBS; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004589 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004590 }
4591 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004592 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004593 }
4594
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004595 for (j = 0; j < INTRA_MODES; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004596 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004597 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004598 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004599
4600#if CONFIG_EXT_PARTITION_TYPES
4601 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004602 av1_diff_update_prob(&r, &fc->partition_prob[0][i], ACCT_STR);
Alex Converse55c6bde2017-01-12 15:55:31 -08004603 for (j = 1; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004604 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004605 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004606#else
Alex Converse55c6bde2017-01-12 15:55:31 -08004607 for (j = 0; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004608 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004609 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004610#endif // CONFIG_EXT_PARTITION_TYPES
hui su9aa97492017-01-26 16:46:01 -08004611
Alex Converse55c6bde2017-01-12 15:55:31 -08004612#if CONFIG_UNPOISON_PARTITION_CTX
4613 for (; j < PARTITION_CONTEXTS_PRIMARY + PARTITION_BLOCK_SIZES; ++j)
4614 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_VERT], ACCT_STR);
4615 for (; j < PARTITION_CONTEXTS_PRIMARY + 2 * PARTITION_BLOCK_SIZES; ++j)
4616 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_HORZ], ACCT_STR);
4617#endif // CONFIG_UNPOISON_PARTITION_CTX
hui su9aa97492017-01-26 16:46:01 -08004618#endif // !CONFIG_EC_ADAPT
4619
4620#if CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004621 for (i = 0; i < INTRA_FILTERS + 1; ++i)
4622 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004623 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
hui su9aa97492017-01-26 16:46:01 -08004624#endif // CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004625
4626 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004627 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Nathan E. Egge10ba2be2016-11-16 09:44:26 -05004628#if CONFIG_EC_MULTISYMBOL
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00004629 av1_copy(cm->fc->kf_y_cdf, av1_kf_y_mode_cdf);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004630#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004631#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004632 for (k = 0; k < INTRA_MODES; k++)
Thomas Davies6519beb2016-10-19 14:46:07 +01004633 for (j = 0; j < INTRA_MODES; j++)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004634 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004635 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004636#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004637 } else {
4638#if !CONFIG_REF_MV
4639 nmv_context *const nmvc = &fc->nmvc;
4640#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004641 read_inter_mode_probs(fc, &r);
4642
4643#if CONFIG_EXT_INTER
4644 read_inter_compound_mode_probs(fc, &r);
4645 if (cm->reference_mode != COMPOUND_REFERENCE) {
4646 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4647 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004648 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004649 }
4650 }
4651 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4652 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004653 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004654 }
4655 for (i = 0; i < BLOCK_SIZES; i++) {
4656 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004657 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004658 }
4659 }
4660 }
4661 if (cm->reference_mode != SINGLE_REFERENCE) {
4662 for (i = 0; i < BLOCK_SIZES; i++) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08004663 for (j = 0; j < COMPOUND_TYPES - 1; j++) {
4664 av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004665 }
4666 }
4667 }
4668#endif // CONFIG_EXT_INTER
4669
Yue Chencb60b182016-10-13 15:18:22 -07004670#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004671 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
Yue Chencb60b182016-10-13 15:18:22 -07004672 for (j = 0; j < MOTION_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004673 av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004674 }
Yue Chencb60b182016-10-13 15:18:22 -07004675#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004676
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004677#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004678 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004679#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004680
4681 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004682 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004683
4684 if (cm->reference_mode != SINGLE_REFERENCE)
4685 setup_compound_reference_mode(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004686 read_frame_reference_mode_probs(cm, &r);
4687
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004688#if !CONFIG_EC_ADAPT
Nathan E. Egge5710c722016-09-08 10:01:16 -04004689 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004690 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004691 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
Nathan E. Egge5710c722016-09-08 10:01:16 -04004692 }
Thomas9ac55082016-09-23 18:04:17 +01004693#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004694
4695#if CONFIG_REF_MV
4696 for (i = 0; i < NMV_CONTEXTS; ++i)
4697 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
4698#else
4699 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
4700#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004701#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004702 read_ext_tx_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004703#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004704#if CONFIG_SUPERTX
4705 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
4706#endif
4707#if CONFIG_GLOBAL_MOTION
4708 read_global_motion(cm, &r);
Thomas Davies6519beb2016-10-19 14:46:07 +01004709#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004710 }
Thomas Davies6519beb2016-10-19 14:46:07 +01004711#if CONFIG_EC_MULTISYMBOL
4712 av1_coef_pareto_cdfs(fc);
David Barker599dfd02016-11-10 13:20:12 +00004713#if CONFIG_REF_MV
4714 for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
4715#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004716 av1_set_mv_cdfs(&fc->nmvc);
David Barker599dfd02016-11-10 13:20:12 +00004717#endif
Nathan E. Egge31296062016-11-16 09:44:26 -05004718#if CONFIG_EC_MULTISYMBOL
Thomas Davies6519beb2016-10-19 14:46:07 +01004719 av1_set_mode_cdfs(cm);
4720#endif
4721#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004722
Yaowu Xuf883b422016-08-30 14:01:10 -07004723 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004724}
4725
4726#ifdef NDEBUG
4727#define debug_check_frame_counts(cm) (void)0
4728#else // !NDEBUG
4729// Counts should only be incremented when frame_parallel_decoding_mode and
4730// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07004731static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004732 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07004733 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004734 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
4735 cm->error_resilient_mode);
4736 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
4737 sizeof(cm->counts.y_mode)));
4738 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
4739 sizeof(cm->counts.uv_mode)));
4740 assert(!memcmp(cm->counts.partition, zero_counts.partition,
4741 sizeof(cm->counts.partition)));
4742 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
4743 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
4744 sizeof(cm->counts.eob_branch)));
Thomas Daviesab780672017-02-01 12:07:29 +00004745#if CONFIG_EC_MULTISYMBOL
4746 assert(!memcmp(cm->counts.blockz_count, zero_counts.blockz_count,
4747 sizeof(cm->counts.blockz_count)));
4748#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004749 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
4750 sizeof(cm->counts.switchable_interp)));
4751 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
4752 sizeof(cm->counts.inter_mode)));
4753#if CONFIG_EXT_INTER
4754 assert(!memcmp(cm->counts.inter_compound_mode,
4755 zero_counts.inter_compound_mode,
4756 sizeof(cm->counts.inter_compound_mode)));
4757 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
4758 sizeof(cm->counts.interintra)));
4759 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
4760 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08004761 assert(!memcmp(cm->counts.compound_interinter,
4762 zero_counts.compound_interinter,
4763 sizeof(cm->counts.compound_interinter)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004764#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07004765#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
4766 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
4767 sizeof(cm->counts.motion_mode)));
4768#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004769 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
4770 sizeof(cm->counts.intra_inter)));
4771 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
4772 sizeof(cm->counts.comp_inter)));
4773 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
4774 sizeof(cm->counts.single_ref)));
4775 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
4776 sizeof(cm->counts.comp_ref)));
4777#if CONFIG_EXT_REFS
4778 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
4779 sizeof(cm->counts.comp_bwdref)));
4780#endif // CONFIG_EXT_REFS
4781 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
4782 sizeof(cm->counts.tx_size)));
4783 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
4784#if CONFIG_REF_MV
4785 assert(
4786 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
4787 assert(
4788 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
4789#else
4790 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
4791#endif
4792 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
4793 sizeof(cm->counts.inter_ext_tx)));
4794 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
4795 sizeof(cm->counts.intra_ext_tx)));
4796}
4797#endif // NDEBUG
4798
Yaowu Xuf883b422016-08-30 14:01:10 -07004799static struct aom_read_bit_buffer *init_read_bit_buffer(
4800 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
4801 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004802 rb->bit_offset = 0;
4803 rb->error_handler = error_handler;
4804 rb->error_handler_data = &pbi->common;
4805 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004806 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004807 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
4808 rb->bit_buffer = clear_data;
4809 rb->bit_buffer_end = clear_data + n;
4810 } else {
4811 rb->bit_buffer = data;
4812 rb->bit_buffer_end = data_end;
4813 }
4814 return rb;
4815}
4816
4817//------------------------------------------------------------------------------
4818
Yaowu Xuf883b422016-08-30 14:01:10 -07004819int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
4820 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
4821 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
4822 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004823}
4824
Yaowu Xuf883b422016-08-30 14:01:10 -07004825void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
4826 int *height) {
4827 *width = aom_rb_read_literal(rb, 16) + 1;
4828 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004829}
4830
Yaowu Xuf883b422016-08-30 14:01:10 -07004831BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
4832 int profile = aom_rb_read_bit(rb);
4833 profile |= aom_rb_read_bit(rb) << 1;
4834 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004835 return (BITSTREAM_PROFILE)profile;
4836}
4837
Yaowu Xuf883b422016-08-30 14:01:10 -07004838void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
4839 const uint8_t *data_end, const uint8_t **p_data_end) {
4840 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004841 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07004842 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004843 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004844 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004845 size_t first_partition_size;
4846 YV12_BUFFER_CONFIG *new_fb;
4847
4848#if CONFIG_BITSTREAM_DEBUG
4849 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
4850#endif
4851
4852 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004853 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Thomas Davies72712e62016-11-09 12:17:51 +00004854#if CONFIG_TILE_GROUPS
4855 pbi->first_partition_size = first_partition_size;
4856 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
4857#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004858 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004859 xd->cur_buf = new_fb;
4860#if CONFIG_GLOBAL_MOTION
4861 xd->global_motion = cm->global_motion;
4862#endif // CONFIG_GLOBAL_MOTION
4863
4864 if (!first_partition_size) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004865 // showing a frame directly
4866 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004867 return;
4868 }
4869
Yaowu Xuf883b422016-08-30 14:01:10 -07004870 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004871 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07004872 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004873 "Truncated packet or corrupt header length");
4874
Jingning Han24e0a182016-11-20 22:34:12 -08004875#if CONFIG_REF_MV
Dengca8d24d2016-10-17 14:06:35 +08004876 cm->setup_mi(cm);
4877#endif
4878
Fangwen Fu8d164de2016-12-14 13:40:54 -08004879#if CONFIG_TEMPMV_SIGNALING
4880 if (cm->use_prev_frame_mvs) {
4881 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4882 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4883 assert(!cm->error_resilient_mode &&
4884 cm->width == last_fb_ref_buf->buf->y_width &&
4885 cm->height == last_fb_ref_buf->buf->y_height &&
4886 !cm->prev_frame->intra_only);
4887 }
4888#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004889 cm->use_prev_frame_mvs =
4890 !cm->error_resilient_mode && cm->width == cm->last_width &&
4891 cm->height == cm->last_height && !cm->last_intra_only &&
4892 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004893#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004894#if CONFIG_EXT_REFS
4895 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
4896 // show_exisiting_frame=1, nor can it take a frame not used as
4897 // a reference, it is probable that by the time it is being
4898 // referred to, the frame buffer it originally points to may
4899 // already get expired and have been reassigned to the current
4900 // newly coded frame. Hence, we need to check whether this is
4901 // the case, and if yes, we have 2 choices:
4902 // (1) Simply disable the use of previous frame mvs; or
4903 // (2) Have cm->prev_frame point to one reference frame buffer,
4904 // e.g. LAST_FRAME.
4905 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
4906 // Reassign the LAST_FRAME buffer to cm->prev_frame.
4907 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4908 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4909 }
4910#endif // CONFIG_EXT_REFS
4911
Yaowu Xuf883b422016-08-30 14:01:10 -07004912 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004913
4914 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
4915 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07004916 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004917 "Uninitialized entropy context.");
4918
Yaowu Xuf883b422016-08-30 14:01:10 -07004919 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004920
4921 xd->corrupted = 0;
4922 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
4923 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07004924 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004925 "Decode failed. Frame data header is corrupted.");
4926
4927 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004928 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004929 }
4930
4931 // If encoded in frame parallel mode, frame context is ready after decoding
4932 // the frame header.
4933 if (cm->frame_parallel_decode &&
4934 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004935 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004936 FrameWorkerData *const frame_worker_data = worker->data1;
4937 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
4938 context_updated = 1;
4939 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4940 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004941 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004942 pbi->cur_buf->row = -1;
4943 pbi->cur_buf->col = -1;
4944 frame_worker_data->frame_context_ready = 1;
4945 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07004946 av1_frameworker_signal_stats(worker);
4947 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004948 }
4949
4950#if CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004951 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004952 cm->coef_probs_update_idx = 0;
4953#endif // CONFIG_ENTROPY
4954
4955 if (pbi->max_threads > 1
4956#if CONFIG_EXT_TILE
4957 && pbi->dec_tile_col < 0 // Decoding all columns
4958#endif // CONFIG_EXT_TILE
4959 && cm->tile_cols > 1) {
4960 // Multi-threaded tile decoder
4961 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
4962 if (!xd->corrupted) {
4963 if (!cm->skip_loop_filter) {
4964 // If multiple threads are used to decode tiles, then we use those
4965 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07004966 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
4967 0, 0, pbi->tile_workers, pbi->num_tile_workers,
4968 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004969 }
4970 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004971 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004972 "Decode failed. Frame data is corrupted.");
4973 }
4974 } else {
4975 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
4976 }
4977#if CONFIG_LOOP_RESTORATION
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08004978 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4979 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4980 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4981 av1_loop_restoration_frame(new_fb, cm, cm->rst_info, 7, 0, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004982 }
4983#endif // CONFIG_LOOP_RESTORATION
4984
Jean-Marc Valin01435132017-02-18 14:12:53 -05004985#if CONFIG_CDEF
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004986 if (cm->dering_level && !cm->skip_loop_filter) {
4987 av1_dering_frame(&pbi->cur_buf->buf, cm, &pbi->mb, cm->dering_level);
4988 }
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004989 if (!cm->skip_loop_filter) {
4990 const YV12_BUFFER_CONFIG *const frame = &pbi->cur_buf->buf;
4991 if (cm->clpf_strength_y) {
Yaowu Xud71be782016-10-14 08:47:03 -07004992 av1_clpf_frame(frame, NULL, cm, cm->clpf_size != CLPF_NOSIZE,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004993 cm->clpf_strength_y + (cm->clpf_strength_y == 3),
Yaowu Xud71be782016-10-14 08:47:03 -07004994 4 + cm->clpf_size, AOM_PLANE_Y, clpf_bit);
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004995 }
4996 if (cm->clpf_strength_u) {
Yaowu Xud71be782016-10-14 08:47:03 -07004997 av1_clpf_frame(frame, NULL, cm, 0, // No block signals for chroma
4998 cm->clpf_strength_u + (cm->clpf_strength_u == 3), 4,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004999 AOM_PLANE_U, NULL);
5000 }
5001 if (cm->clpf_strength_v) {
Yaowu Xud71be782016-10-14 08:47:03 -07005002 av1_clpf_frame(frame, NULL, cm, 0, // No block signals for chroma
5003 cm->clpf_strength_v + (cm->clpf_strength_v == 3), 4,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02005004 AOM_PLANE_V, NULL);
5005 }
Steinar Midtskogend06588a2016-05-06 13:48:20 +02005006 }
5007 if (cm->clpf_blocks) aom_free(cm->clpf_blocks);
Thomas Daedef56859f2016-04-19 16:57:24 -07005008#endif
Thomas Daedef56859f2016-04-19 16:57:24 -07005009
Yaowu Xuc27fc142016-08-22 16:08:15 -07005010 if (!xd->corrupted) {
5011 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
5012#if CONFIG_ENTROPY
5013 cm->partial_prob_update = 0;
5014#endif // CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07005015 av1_adapt_coef_probs(cm);
5016 av1_adapt_intra_frame_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005017
5018 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005019 av1_adapt_inter_frame_probs(cm);
5020 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005021 }
5022 } else {
5023 debug_check_frame_counts(cm);
5024 }
5025 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005026 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005027 "Decode failed. Frame data is corrupted.");
5028 }
5029
5030 // Non frame parallel update frame context here.
5031 if (!cm->error_resilient_mode && !context_updated)
5032 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
5033}