blob: e79221a2a8d89ce2d6b87f74c2261430a1189319 [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
Nathan E. Egge2cf03b12017-02-22 16:19:59 -050036#if CONFIG_INSPECTION
37#include "av1/decoder/inspection.h"
38#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070039#include "av1/common/common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070040#include "av1/common/entropy.h"
41#include "av1/common/entropymode.h"
Thomas Davies6519beb2016-10-19 14:46:07 +010042#include "av1/common/entropymv.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070043#include "av1/common/idct.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070044#include "av1/common/pred_common.h"
45#include "av1/common/quant_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070046#include "av1/common/reconinter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070047#include "av1/common/reconintra.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070048#include "av1/common/seg_common.h"
Jingning Han1aab8182016-06-03 11:09:06 -070049#include "av1/common/thread_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070050#include "av1/common/tile_common.h"
51
52#include "av1/decoder/decodeframe.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070053#include "av1/decoder/decodemv.h"
54#include "av1/decoder/decoder.h"
Jingning Han1aab8182016-06-03 11:09:06 -070055#include "av1/decoder/detokenize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070056#include "av1/decoder/dsubexp.h"
57
Yue Chen69f18e12016-09-08 14:48:15 -070058#if CONFIG_WARPED_MOTION
59#include "av1/common/warped_motion.h"
60#endif // CONFIG_WARPED_MOTION
61
Yaowu Xuf883b422016-08-30 14:01:10 -070062#define MAX_AV1_HEADER_SIZE 80
Michael Bebenita6048d052016-08-25 14:40:54 -070063#define ACCT_STR __func__
Yaowu Xuc27fc142016-08-22 16:08:15 -070064
Yushin Cho77bba8d2016-11-04 16:36:56 -070065#if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -070066#include "av1/common/partition.h"
hui suff0da2b2017-03-07 15:51:37 -080067#include "av1/common/pvq.h"
68#include "av1/common/scan.h"
Yushin Cho77bba8d2016-11-04 16:36:56 -070069#include "av1/decoder/decint.h"
hui suff0da2b2017-03-07 15:51:37 -080070#include "av1/decoder/pvq_decoder.h"
71#include "av1/encoder/encodemb.h"
Yushin Cho77bba8d2016-11-04 16:36:56 -070072#include "av1/encoder/hybrid_fwd_txfm.h"
73#endif
74
Thomas Davies80188d12016-10-26 16:08:35 -070075static struct aom_read_bit_buffer *init_read_bit_buffer(
76 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
77 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]);
78static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
79 size_t partition_size);
80static size_t read_uncompressed_header(AV1Decoder *pbi,
81 struct aom_read_bit_buffer *rb);
82
Yaowu Xuf883b422016-08-30 14:01:10 -070083static int is_compound_reference_allowed(const AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070084 int i;
85 if (frame_is_intra_only(cm)) return 0;
86 for (i = 1; i < INTER_REFS_PER_FRAME; ++i)
87 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
88
89 return 0;
90}
91
Yaowu Xuf883b422016-08-30 14:01:10 -070092static void setup_compound_reference_mode(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070093#if CONFIG_EXT_REFS
94 cm->comp_fwd_ref[0] = LAST_FRAME;
95 cm->comp_fwd_ref[1] = LAST2_FRAME;
96 cm->comp_fwd_ref[2] = LAST3_FRAME;
97 cm->comp_fwd_ref[3] = GOLDEN_FRAME;
98
99 cm->comp_bwd_ref[0] = BWDREF_FRAME;
100 cm->comp_bwd_ref[1] = ALTREF_FRAME;
101#else
102 if (cm->ref_frame_sign_bias[LAST_FRAME] ==
103 cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
104 cm->comp_fixed_ref = ALTREF_FRAME;
105 cm->comp_var_ref[0] = LAST_FRAME;
106 cm->comp_var_ref[1] = GOLDEN_FRAME;
107 } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
108 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
109 cm->comp_fixed_ref = GOLDEN_FRAME;
110 cm->comp_var_ref[0] = LAST_FRAME;
111 cm->comp_var_ref[1] = ALTREF_FRAME;
112 } else {
113 cm->comp_fixed_ref = LAST_FRAME;
114 cm->comp_var_ref[0] = GOLDEN_FRAME;
115 cm->comp_var_ref[1] = ALTREF_FRAME;
116 }
117#endif // CONFIG_EXT_REFS
118}
119
120static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
121 return len != 0 && len <= (size_t)(end - start);
122}
123
Yaowu Xuf883b422016-08-30 14:01:10 -0700124static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
125 const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700126 return data > max ? max : data;
127}
128
Yue Cheneeacc4c2017-01-17 17:29:17 -0800129static TX_MODE read_tx_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
130 struct aom_read_bit_buffer *rb) {
131 int i, all_lossless = 1;
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800132#if CONFIG_TX64X64
Yue Cheneeacc4c2017-01-17 17:29:17 -0800133 TX_MODE tx_mode;
134#endif
135
136 if (cm->seg.enabled) {
137 for (i = 0; i < MAX_SEGMENTS; ++i) {
138 if (!xd->lossless[i]) {
139 all_lossless = 0;
140 break;
141 }
142 }
143 } else {
144 all_lossless = xd->lossless[0];
145 }
146
147 if (all_lossless) return ONLY_4X4;
148#if CONFIG_TX64X64
149 tx_mode = aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800150 if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
151 return tx_mode;
152#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700153 return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800154#endif // CONFIG_TX64X64
Yaowu Xuc27fc142016-08-22 16:08:15 -0700155}
156
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -0500157#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -0700158static void read_tx_size_probs(FRAME_CONTEXT *fc, aom_reader *r) {
159 int i, j, k;
160 for (i = 0; i < MAX_TX_DEPTH; ++i)
161 for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
162 for (k = 0; k < i + 1; ++k)
163 av1_diff_update_prob(r, &fc->tx_size_probs[i][j][k], ACCT_STR);
164}
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -0500165#endif
Yaowu Xuefc75352016-10-31 09:46:42 -0700166
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400167#if !CONFIG_EC_ADAPT
Yaowu Xuf883b422016-08-30 14:01:10 -0700168static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700169 int i, j;
Nathan E. Egge4947c292016-04-26 11:37:06 -0400170 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700171 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700172 av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i], ACCT_STR);
Nathan E. Egge4947c292016-04-26 11:37:06 -0400173 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700174}
Thomas Davies6519beb2016-10-19 14:46:07 +0100175#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700176
Yaowu Xuf883b422016-08-30 14:01:10 -0700177static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700178#if CONFIG_REF_MV
Yaowu Xu8af861b2016-11-01 12:12:11 -0700179 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700180 for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700181 av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182 for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700183 av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700184 for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700185 av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700186 for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700187 av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700188#if CONFIG_EXT_INTER
Michael Bebenita6048d052016-08-25 14:40:54 -0700189 av1_diff_update_prob(r, &fc->new2mv_prob, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700190#endif // CONFIG_EXT_INTER
191#else
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400192#if !CONFIG_EC_ADAPT
Yaowu Xu8af861b2016-11-01 12:12:11 -0700193 int i, j;
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400194 for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700195 for (j = 0; j < INTER_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700196 av1_diff_update_prob(r, &fc->inter_mode_probs[i][j], ACCT_STR);
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400197 }
Yaowu Xu8af861b2016-11-01 12:12:11 -0700198#else
199 (void)fc;
200 (void)r;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700201#endif
Thomas Davies6519beb2016-10-19 14:46:07 +0100202#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700203}
204
205#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700206static void read_inter_compound_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700207 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -0700208 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700209 for (j = 0; j < INTER_MODE_CONTEXTS; ++j) {
210 for (i = 0; i < INTER_COMPOUND_MODES - 1; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700211 av1_diff_update_prob(r, &fc->inter_compound_mode_probs[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700212 }
213 }
214 }
215}
216#endif // CONFIG_EXT_INTER
Yaowu Xu17fd2f22016-11-17 18:23:28 -0800217#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100218#if !CONFIG_EXT_TX
219static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
220 int i, j, k;
221 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
222 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
223 for (j = 0; j < TX_TYPES; ++j) {
224 for (k = 0; k < TX_TYPES - 1; ++k)
225 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[i][j][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100226 }
227 }
228 }
229 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
230 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
231 for (k = 0; k < TX_TYPES - 1; ++k)
232 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[i][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100233 }
234 }
235}
236#endif
237#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700238
239static REFERENCE_MODE read_frame_reference_mode(
Yaowu Xuf883b422016-08-30 14:01:10 -0700240 const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700241 if (is_compound_reference_allowed(cm)) {
Zoe Liub05e5d12017-02-07 14:32:53 -0800242#if CONFIG_REF_ADAPT
243 return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
244#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700245 return aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700246 ? REFERENCE_MODE_SELECT
Yaowu Xuf883b422016-08-30 14:01:10 -0700247 : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
Zoe Liub05e5d12017-02-07 14:32:53 -0800248#endif // CONFIG_REF_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700249 } else {
250 return SINGLE_REFERENCE;
251 }
252}
253
Yaowu Xuf883b422016-08-30 14:01:10 -0700254static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700255 FRAME_CONTEXT *const fc = cm->fc;
256 int i, j;
257
258 if (cm->reference_mode == REFERENCE_MODE_SELECT)
259 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700260 av1_diff_update_prob(r, &fc->comp_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700261
262 if (cm->reference_mode != COMPOUND_REFERENCE) {
263 for (i = 0; i < REF_CONTEXTS; ++i) {
264 for (j = 0; j < (SINGLE_REFS - 1); ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700265 av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700266 }
267 }
268 }
269
270 if (cm->reference_mode != SINGLE_REFERENCE) {
271 for (i = 0; i < REF_CONTEXTS; ++i) {
272#if CONFIG_EXT_REFS
273 for (j = 0; j < (FWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700274 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700275 for (j = 0; j < (BWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700276 av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700277#else
278 for (j = 0; j < (COMP_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700279 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700280#endif // CONFIG_EXT_REFS
281 }
282 }
283}
284
Yaowu Xuf883b422016-08-30 14:01:10 -0700285static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700286 int i;
Michael Bebenita6048d052016-08-25 14:40:54 -0700287 for (i = 0; i < n; ++i) av1_diff_update_prob(r, &p[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700288}
289
Yaowu Xuf883b422016-08-30 14:01:10 -0700290static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Thomas9ac55082016-09-23 18:04:17 +0100291 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700292
Yaowu Xue86288d2016-10-31 15:56:38 -0700293#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100294 int j;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700295 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
296
Yaowu Xuc27fc142016-08-22 16:08:15 -0700297 for (i = 0; i < 2; ++i) {
298 nmv_component *const comp_ctx = &ctx->comps[i];
299 update_mv_probs(&comp_ctx->sign, 1, r);
300 update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
301 update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
302 update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
303 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700304 for (i = 0; i < 2; ++i) {
305 nmv_component *const comp_ctx = &ctx->comps[i];
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400306 for (j = 0; j < CLASS0_SIZE; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700307 update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400308 }
Nathan E. Eggeac499f32016-09-08 15:38:57 -0400309 update_mv_probs(comp_ctx->fp, MV_FP_SIZE - 1, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700310 }
Alex Converseaca9feb2016-10-10 11:08:10 -0700311#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700312
313 if (allow_hp) {
314 for (i = 0; i < 2; ++i) {
315 nmv_component *const comp_ctx = &ctx->comps[i];
316 update_mv_probs(&comp_ctx->class0_hp, 1, r);
317 update_mv_probs(&comp_ctx->hp, 1, r);
318 }
319 }
320}
321
322static void inverse_transform_block(MACROBLOCKD *xd, int plane,
323 const TX_TYPE tx_type,
324 const TX_SIZE tx_size, uint8_t *dst,
Jingning Han1be18782016-10-21 11:48:15 -0700325 int stride, int16_t scan_line, int eob) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700326 struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han1be18782016-10-21 11:48:15 -0700327 tran_low_t *const dqcoeff = pd->dqcoeff;
328 INV_TXFM_PARAM inv_txfm_param;
329 inv_txfm_param.tx_type = tx_type;
330 inv_txfm_param.tx_size = tx_size;
331 inv_txfm_param.eob = eob;
332 inv_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700333
Yaowu Xuf883b422016-08-30 14:01:10 -0700334#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han1be18782016-10-21 11:48:15 -0700335 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
336 inv_txfm_param.bd = xd->bd;
337 highbd_inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
338 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700339#endif // CONFIG_AOM_HIGHBITDEPTH
Jingning Han1be18782016-10-21 11:48:15 -0700340 inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
Yaowu Xuf883b422016-08-30 14:01:10 -0700341#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700342 }
Jingning Han1be18782016-10-21 11:48:15 -0700343#endif // CONFIG_AOM_HIGHBITDEPTH
344 memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700345}
346
Yushin Cho77bba8d2016-11-04 16:36:56 -0700347#if CONFIG_PVQ
Thomas Daede1dbda1b2017-02-06 16:06:29 -0800348static int av1_pvq_decode_helper(od_dec_ctx *dec, tran_low_t *ref_coeff,
349 tran_low_t *dqcoeff, int16_t *quant, int pli,
Yushin Cho77bba8d2016-11-04 16:36:56 -0700350 int bs, TX_TYPE tx_type, int xdec,
ltrudeaue1c09292017-01-20 15:42:13 -0500351 PVQ_SKIP_TYPE ac_dc_coded) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700352 unsigned int flags; // used for daala's stream analyzer.
353 int off;
354 const int is_keyframe = 0;
355 const int has_dc_skip = 1;
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800356 /*TODO(tterribe): Handle CONFIG_AOM_HIGHBITDEPTH.*/
357 int coeff_shift = 3 - get_tx_scale(bs);
358 int rounding_mask;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700359 // DC quantizer for PVQ
360 int pvq_dc_quant;
361 int lossless = (quant[0] == 0);
362 const int blk_size = tx_size_wide[bs];
363 int eob = 0;
364 int i;
Yushin Cho70669122016-12-08 09:53:14 -1000365 int use_activity_masking = dec->use_activity_masking;
Thomas Daede1dbda1b2017-02-06 16:06:29 -0800366 DECLARE_ALIGNED(16, tran_low_t, dqcoeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
367 DECLARE_ALIGNED(16, tran_low_t, ref_coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700368
Yushin Cho48f84db2016-11-07 21:20:17 -0800369 od_coeff ref_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
370 od_coeff out_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700371
372 od_raster_to_coding_order(ref_coeff_pvq, blk_size, tx_type, ref_coeff,
373 blk_size);
374
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800375 assert(OD_COEFF_SHIFT >= 3);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700376 if (lossless)
377 pvq_dc_quant = 1;
378 else {
Yushin Cho70669122016-12-08 09:53:14 -1000379 if (use_activity_masking)
380 pvq_dc_quant = OD_MAXI(
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800381 1, (quant[0] << (OD_COEFF_SHIFT - 3)) *
Yushin Cho70669122016-12-08 09:53:14 -1000382 dec->state.pvq_qm_q4[pli][od_qm_get_index(bs, 0)] >>
383 4);
384 else
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800385 pvq_dc_quant = OD_MAXI(1, quant[0] << (OD_COEFF_SHIFT - 3));
Yushin Cho77bba8d2016-11-04 16:36:56 -0700386 }
387
388 off = od_qm_offset(bs, xdec);
389
390 // copy int16 inputs to int32
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800391 for (i = 0; i < blk_size * blk_size; i++) {
Timothy B. Terriberry4e6a8f32017-02-24 11:00:59 -0800392 ref_int32[i] =
393 AOM_SIGNED_SHL(ref_coeff_pvq[i], OD_COEFF_SHIFT - coeff_shift);
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800394 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700395
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800396 od_pvq_decode(dec, ref_int32, out_int32, quant[1] << (OD_COEFF_SHIFT - 3),
397 pli, bs, OD_PVQ_BETA[use_activity_masking][pli][bs],
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800398 OD_ROBUST_STREAM, is_keyframe, &flags, ac_dc_coded,
399 dec->state.qm + off, dec->state.qm_inv + off);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700400
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800401 if (!has_dc_skip || out_int32[0]) {
402 out_int32[0] =
Nathan E. Egge89f58762016-12-28 16:31:50 -0500403 has_dc_skip + generic_decode(dec->r, &dec->state.adapt.model_dc[pli],
404 -1, &dec->state.adapt.ex_dc[pli][bs][0], 2,
405 "dc:mag");
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800406 if (out_int32[0]) out_int32[0] *= aom_read_bit(dec->r, "dc:sign") ? -1 : 1;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700407 }
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800408 out_int32[0] = out_int32[0] * pvq_dc_quant + ref_int32[0];
409
410 // copy int32 result back to int16
411 assert(OD_COEFF_SHIFT > coeff_shift);
412 rounding_mask = (1 << (OD_COEFF_SHIFT - coeff_shift - 1)) - 1;
413 for (i = 0; i < blk_size * blk_size; i++) {
414 dqcoeff_pvq[i] = (out_int32[i] + (out_int32[i] < 0) + rounding_mask) >>
415 (OD_COEFF_SHIFT - coeff_shift);
416 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700417
418 od_coding_order_to_raster(dqcoeff, blk_size, tx_type, dqcoeff_pvq, blk_size);
419
420 eob = blk_size * blk_size;
421
422 return eob;
423}
424
ltrudeaue1c09292017-01-20 15:42:13 -0500425static PVQ_SKIP_TYPE read_pvq_skip(AV1_COMMON *cm, MACROBLOCKD *const xd,
426 int plane, TX_SIZE tx_size) {
427 // decode ac/dc coded flag. bit0: DC coded, bit1 : AC coded
428 // NOTE : we don't use 5 symbols for luma here in aom codebase,
429 // since block partition is taken care of by aom.
430 // So, only AC/DC skip info is coded
431 const int ac_dc_coded = aom_decode_cdf_adapt(
432 xd->daala_dec.r,
433 xd->daala_dec.state.adapt.skip_cdf[2 * tx_size + (plane != 0)], 4,
434 xd->daala_dec.state.adapt.skip_increment, "skip");
435 if (ac_dc_coded < 0 || ac_dc_coded > 3) {
436 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
437 "Invalid PVQ Skip Type");
438 }
439 return ac_dc_coded;
440}
441
442static int av1_pvq_decode_helper2(AV1_COMMON *cm, MACROBLOCKD *const xd,
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800443 MB_MODE_INFO *const mbmi, int plane, int row,
444 int col, TX_SIZE tx_size, TX_TYPE tx_type) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700445 struct macroblockd_plane *const pd = &xd->plane[plane];
446 // transform block size in pixels
447 int tx_blk_size = tx_size_wide[tx_size];
448 int i, j;
449 tran_low_t *pvq_ref_coeff = pd->pvq_ref_coeff;
450 const int diff_stride = tx_blk_size;
451 int16_t *pred = pd->pred;
452 tran_low_t *const dqcoeff = pd->dqcoeff;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700453 uint8_t *dst;
454 int eob;
ltrudeaue1c09292017-01-20 15:42:13 -0500455 const PVQ_SKIP_TYPE ac_dc_coded = read_pvq_skip(cm, xd, plane, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700456
457 eob = 0;
458 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
459
Yushin Cho77bba8d2016-11-04 16:36:56 -0700460 if (ac_dc_coded) {
461 int xdec = pd->subsampling_x;
462 int seg_id = mbmi->segment_id;
463 int16_t *quant;
464 FWD_TXFM_PARAM fwd_txfm_param;
Yaowu Xufc1b2132016-11-07 15:16:15 -0800465 // ToDo(yaowu): correct this with optimal number from decoding process.
466 const int max_scan_line = tx_size_2d[tx_size];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700467
468 for (j = 0; j < tx_blk_size; j++)
469 for (i = 0; i < tx_blk_size; i++) {
470 pred[diff_stride * j + i] = dst[pd->dst.stride * j + i];
471 }
472
473 fwd_txfm_param.tx_type = tx_type;
474 fwd_txfm_param.tx_size = tx_size;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700475 fwd_txfm_param.lossless = xd->lossless[seg_id];
476
477 fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
478
479 quant = &pd->seg_dequant[seg_id][0]; // aom's quantizer
480
481 eob = av1_pvq_decode_helper(&xd->daala_dec, pvq_ref_coeff, dqcoeff, quant,
482 plane, tx_size, tx_type, xdec, ac_dc_coded);
483
484 // Since av1 does not have separate inverse transform
485 // but also contains adding to predicted image,
486 // pass blank dummy image to av1_inv_txfm_add_*x*(), i.e. set dst as zeros
487 for (j = 0; j < tx_blk_size; j++)
488 for (i = 0; i < tx_blk_size; i++) dst[j * pd->dst.stride + i] = 0;
489
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800490 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
491 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700492 }
493
494 return eob;
495}
496#endif
497
Alex Converse8aca36d2017-01-31 12:33:15 -0800498static void predict_and_reconstruct_intra_block(
499 AV1_COMMON *cm, MACROBLOCKD *const xd, aom_reader *const r,
500 MB_MODE_INFO *const mbmi, int plane, int row, int col, TX_SIZE tx_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700501 struct macroblockd_plane *const pd = &xd->plane[plane];
502 PREDICTION_MODE mode = (plane == 0) ? mbmi->mode : mbmi->uv_mode;
Luc Trudeau005feb62017-02-22 13:34:01 -0500503 PLANE_TYPE plane_type = get_plane_type(plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700504 uint8_t *dst;
Urvang Joshifeb925f2016-12-05 10:37:29 -0800505 const int block_idx = (row << 1) + col;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700506#if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -0700507 (void)r;
508#endif
Jingning Hanca14dda2016-12-09 09:36:00 -0800509 dst = &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700510
Jingning Han41bb3392016-12-14 10:46:48 -0800511#if !CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -0700512 if (mbmi->sb_type < BLOCK_8X8)
Urvang Joshifeb925f2016-12-05 10:37:29 -0800513 if (plane == 0) mode = xd->mi[0]->bmi[block_idx].as_mode;
Jingning Han41bb3392016-12-14 10:46:48 -0800514#endif
David Barker839467f2017-01-19 11:06:15 +0000515 av1_predict_intra_block(xd, pd->width, pd->height, txsize_to_bsize[tx_size],
516 mode, dst, pd->dst.stride, dst, pd->dst.stride, col,
517 row, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700518
519 if (!mbmi->skip) {
520 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700521#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700522 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 0);
Jingning Han1be18782016-10-21 11:48:15 -0700523 int16_t max_scan_line = 0;
524 const int eob =
525 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size,
526 tx_type, &max_scan_line, r, mbmi->segment_id);
Angie Chianged8cd9a2016-10-21 16:44:47 -0700527#if CONFIG_ADAPT_SCAN
Jingning Han45e0d462017-02-13 10:37:10 -0800528 if (xd->counts)
529 av1_update_scan_count_facade(cm, xd->counts, tx_size, tx_type,
530 pd->dqcoeff, eob);
Angie Chianged8cd9a2016-10-21 16:44:47 -0700531#endif
Jingning Han1be18782016-10-21 11:48:15 -0700532 if (eob)
533 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
534 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700535#else
ltrudeaue1c09292017-01-20 15:42:13 -0500536 av1_pvq_decode_helper2(cm, xd, mbmi, plane, row, col, tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700537#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700538 }
539}
540
Jingning Handddb21f2017-02-28 14:44:05 -0800541#if CONFIG_VAR_TX && !CONFIG_COEF_INTERLEAVE
Angie Chiangff6d8902016-10-21 11:02:09 -0700542static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
543 aom_reader *r, MB_MODE_INFO *const mbmi,
Jingning Han8fd62b72016-10-21 12:55:54 -0700544 int plane, BLOCK_SIZE plane_bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700545 int blk_row, int blk_col, TX_SIZE tx_size,
546 int *eob_total) {
547 const struct macroblockd_plane *const pd = &xd->plane[plane];
548 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
549 const int tx_row = blk_row >> (1 - pd->subsampling_y);
550 const int tx_col = blk_col >> (1 - pd->subsampling_x);
551 const TX_SIZE plane_tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700552 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700553 : mbmi->inter_tx_size[tx_row][tx_col];
Jingning Han5f614262016-10-27 14:27:43 -0700554 // Scale to match transform block unit.
Jingning Hanf64062f2016-11-02 16:22:18 -0700555 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
556 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700557
558 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
559
560 if (tx_size == plane_tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500561 PLANE_TYPE plane_type = get_plane_type(plane);
Jingning Han8fd62b72016-10-21 12:55:54 -0700562 int block_idx = (blk_row << 1) + blk_col;
563 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiangff6d8902016-10-21 11:02:09 -0700564 const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700565 int16_t max_scan_line = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700566 const int eob =
Yaowu Xuf883b422016-08-30 14:01:10 -0700567 av1_decode_block_tokens(xd, plane, sc, blk_col, blk_row, plane_tx_size,
Jingning Han1be18782016-10-21 11:48:15 -0700568 tx_type, &max_scan_line, r, mbmi->segment_id);
Jingning Han758b2ce2017-01-20 17:12:55 -0800569#if CONFIG_ADAPT_SCAN
Jingning Han45e0d462017-02-13 10:37:10 -0800570 if (xd->counts)
571 av1_update_scan_count_facade(cm, xd->counts, tx_size, tx_type,
572 pd->dqcoeff, eob);
Jingning Han758b2ce2017-01-20 17:12:55 -0800573#endif
Jingning Han9ca05b72017-01-03 14:41:36 -0800574 inverse_transform_block(xd, plane, tx_type, plane_tx_size,
575 &pd->dst.buf[(blk_row * pd->dst.stride + blk_col)
576 << tx_size_wide_log2[0]],
577 pd->dst.stride, max_scan_line, eob);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700578 *eob_total += eob;
579 } else {
Jingning Hanf64062f2016-11-02 16:22:18 -0700580 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
581 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700582 int i;
583
584 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700585
586 for (i = 0; i < 4; ++i) {
Jingning Han5f614262016-10-27 14:27:43 -0700587 const int offsetr = blk_row + (i >> 1) * bsl;
588 const int offsetc = blk_col + (i & 0x01) * bsl;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700589
590 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
591
Jingning Han8fd62b72016-10-21 12:55:54 -0700592 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
Jingning Hanf64062f2016-11-02 16:22:18 -0700593 offsetc, sub_txs, eob_total);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700594 }
595 }
596}
597#endif // CONFIG_VAR_TX
598
Jingning Handddb21f2017-02-28 14:44:05 -0800599#if !CONFIG_VAR_TX || CONFIG_SUPERTX || CONFIG_COEF_INTERLEAVE || \
Jingning Hanfe45b212016-11-22 10:30:23 -0800600 (!CONFIG_VAR_TX && CONFIG_EXT_TX && CONFIG_RECT_TX)
Angie Chiangff6d8902016-10-21 11:02:09 -0700601static int reconstruct_inter_block(AV1_COMMON *cm, MACROBLOCKD *const xd,
Alex Converse8aca36d2017-01-31 12:33:15 -0800602 aom_reader *const r, int segment_id,
603 int plane, int row, int col,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700604 TX_SIZE tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500605 PLANE_TYPE plane_type = get_plane_type(plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606 int block_idx = (row << 1) + col;
607 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700608#if CONFIG_PVQ
609 int eob;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700610 (void)r;
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800611 (void)segment_id;
612#else
613 struct macroblockd_plane *const pd = &xd->plane[plane];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700614#endif
615
616#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700617 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700618 int16_t max_scan_line = 0;
619 const int eob =
620 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size, tx_type,
621 &max_scan_line, r, segment_id);
Jingning Hanca14dda2016-12-09 09:36:00 -0800622 uint8_t *dst =
623 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Angie Chianged8cd9a2016-10-21 16:44:47 -0700624#if CONFIG_ADAPT_SCAN
Jingning Han45e0d462017-02-13 10:37:10 -0800625 if (xd->counts)
626 av1_update_scan_count_facade(cm, xd->counts, tx_size, tx_type, pd->dqcoeff,
627 eob);
Angie Chianged8cd9a2016-10-21 16:44:47 -0700628#endif
Jingning Han1be18782016-10-21 11:48:15 -0700629 if (eob)
Jingning Hanca14dda2016-12-09 09:36:00 -0800630 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
631 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700632#else
ltrudeaue1c09292017-01-20 15:42:13 -0500633 eob = av1_pvq_decode_helper2(cm, xd, &xd->mi[0]->mbmi, plane, row, col,
634 tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700635#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700636 return eob;
637}
638#endif // !CONFIG_VAR_TX || CONFIG_SUPER_TX
639
Angie Chiang44701f22017-02-27 10:36:44 -0800640static void set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
641 BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
642 int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700643 const int offset = mi_row * cm->mi_stride + mi_col;
644 int x, y;
645 const TileInfo *const tile = &xd->tile;
646
647 xd->mi = cm->mi_grid_visible + offset;
648 xd->mi[0] = &cm->mi[offset];
649 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
650 // passing bsize from decode_partition().
651 xd->mi[0]->mbmi.sb_type = bsize;
Angie Chiang394c3372016-11-03 11:13:15 -0700652#if CONFIG_RD_DEBUG
653 xd->mi[0]->mbmi.mi_row = mi_row;
654 xd->mi[0]->mbmi.mi_col = mi_col;
655#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700656 for (y = 0; y < y_mis; ++y)
Jingning Han97d85482016-07-15 11:06:05 -0700657 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700658
Jingning Hanfaad0e12016-12-07 10:54:57 -0800659 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700660 set_skip_context(xd, mi_row, mi_col);
661
662#if CONFIG_VAR_TX
663 xd->max_tx_size = max_txsize_lookup[bsize];
664#endif
665
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800666// Distance of Mb to the various image edges. These are specified to 8th pel
667// as they are always compared to values that are in 1/8th pel units
668#if CONFIG_DEPENDENT_HORZTILES
669 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
670 cm->dependent_horz_tiles);
671#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700672 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 -0800673#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700674
Yaowu Xuf883b422016-08-30 14:01:10 -0700675 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700676}
677
678#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700679static MB_MODE_INFO *set_offsets_extend(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700680 MACROBLOCKD *const xd,
681 const TileInfo *const tile,
682 BLOCK_SIZE bsize_pred, int mi_row_pred,
683 int mi_col_pred, int mi_row_ori,
684 int mi_col_ori) {
685 // Used in supertx
686 // (mi_row_ori, mi_col_ori): location for mv
687 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
Jingning Han93531242016-12-20 11:54:36 -0800688 const int bw = mi_size_wide[bsize_pred];
689 const int bh = mi_size_high[bsize_pred];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700690 const int offset = mi_row_ori * cm->mi_stride + mi_col_ori;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700691 xd->mi = cm->mi_grid_visible + offset;
692 xd->mi[0] = cm->mi + offset;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800693#if CONFIG_DEPENDENT_HORZTILES
694 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
695 cm->mi_cols, cm->dependent_horz_tiles);
696#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700697 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
698 cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800699#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700700
701 xd->up_available = (mi_row_ori > tile->mi_row_start);
702 xd->left_available = (mi_col_ori > tile->mi_col_start);
703
Jingning Hanfaad0e12016-12-07 10:54:57 -0800704 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700705
706 return &xd->mi[0]->mbmi;
707}
708
Angie Chiang7fcfee42017-02-24 15:51:03 -0800709#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700710static MB_MODE_INFO *set_mb_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
711 BLOCK_SIZE bsize, int mi_row, int mi_col,
712 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700713 const int offset = mi_row * cm->mi_stride + mi_col;
714 const TileInfo *const tile = &xd->tile;
715 int x, y;
716
717 xd->mi = cm->mi_grid_visible + offset;
718 xd->mi[0] = cm->mi + offset;
719 xd->mi[0]->mbmi.sb_type = bsize;
720 for (y = 0; y < y_mis; ++y)
721 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
722
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800723#if CONFIG_DEPENDENT_HORZTILES
724 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
725 cm->dependent_horz_tiles);
726#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700727 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 -0800728#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700729 return &xd->mi[0]->mbmi;
730}
Angie Chiang7fcfee42017-02-24 15:51:03 -0800731#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700732
Yaowu Xuf883b422016-08-30 14:01:10 -0700733static void set_offsets_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700734 const TileInfo *const tile, BLOCK_SIZE bsize,
735 int mi_row, int mi_col) {
Jingning Han93531242016-12-20 11:54:36 -0800736 const int bw = mi_size_wide[bsize];
737 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700738 const int offset = mi_row * cm->mi_stride + mi_col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700739
740 xd->mi = cm->mi_grid_visible + offset;
741 xd->mi[0] = cm->mi + offset;
742
Jingning Hanfaad0e12016-12-07 10:54:57 -0800743 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700744
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800745#if CONFIG_DEPENDENT_HORZTILES
746 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
747 cm->dependent_horz_tiles);
748#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700749 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 -0800750#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700751
Yaowu Xuf883b422016-08-30 14:01:10 -0700752 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700753}
754
Yaowu Xuf883b422016-08-30 14:01:10 -0700755static void set_param_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700756 BLOCK_SIZE bsize, int mi_row, int mi_col,
757 int txfm, int skip) {
Jingning Han93531242016-12-20 11:54:36 -0800758 const int bw = mi_size_wide[bsize];
759 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -0700760 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
761 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700762 const int offset = mi_row * cm->mi_stride + mi_col;
763 int x, y;
764
765 xd->mi = cm->mi_grid_visible + offset;
766 xd->mi[0] = cm->mi + offset;
767
768 for (y = 0; y < y_mis; ++y)
769 for (x = 0; x < x_mis; ++x) {
770 xd->mi[y * cm->mi_stride + x]->mbmi.skip = skip;
771 xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
772 }
773#if CONFIG_VAR_TX
774 xd->above_txfm_context = cm->above_txfm_context + mi_col;
775 xd->left_txfm_context =
776 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
Yaowu Xu52a17632016-11-17 15:48:21 -0800777 set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bw, bh, skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700778#endif
779}
780
Yaowu Xuf883b422016-08-30 14:01:10 -0700781static void set_ref(AV1_COMMON *const cm, MACROBLOCKD *const xd, int idx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700782 int mi_row, int mi_col) {
783 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
784 RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
785 xd->block_refs[idx] = ref_buffer;
Yaowu Xuf883b422016-08-30 14:01:10 -0700786 if (!av1_is_valid_scale(&ref_buffer->sf))
787 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700788 "Invalid scale factors");
Yaowu Xuf883b422016-08-30 14:01:10 -0700789 av1_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
790 &ref_buffer->sf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791 xd->corrupted |= ref_buffer->buf->corrupted;
792}
793
794static void dec_predict_b_extend(
Yaowu Xuf883b422016-08-30 14:01:10 -0700795 AV1Decoder *const pbi, MACROBLOCKD *const xd, const TileInfo *const tile,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700796 int block, int mi_row_ori, int mi_col_ori, int mi_row_pred, int mi_col_pred,
797 int mi_row_top, int mi_col_top, uint8_t *dst_buf[3], int dst_stride[3],
798 BLOCK_SIZE bsize_top, BLOCK_SIZE bsize_pred, int b_sub8x8, int bextend) {
799 // Used in supertx
800 // (mi_row_ori, mi_col_ori): location for mv
801 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
802 // (mi_row_top, mi_col_top, bsize_top): region of the top partition size
803 // block: sub location of sub8x8 blocks
804 // b_sub8x8: 1: ori is sub8x8; 0: ori is not sub8x8
805 // bextend: 1: region to predict is an extension of ori; 0: not
806 int r = (mi_row_pred - mi_row_top) * MI_SIZE;
807 int c = (mi_col_pred - mi_col_top) * MI_SIZE;
Jingning Han93531242016-12-20 11:54:36 -0800808 const int mi_width_top = mi_size_wide[bsize_top];
809 const int mi_height_top = mi_size_high[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700810 MB_MODE_INFO *mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700811 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700812
813 if (mi_row_pred < mi_row_top || mi_col_pred < mi_col_top ||
814 mi_row_pred >= mi_row_top + mi_height_top ||
815 mi_col_pred >= mi_col_top + mi_width_top || mi_row_pred >= cm->mi_rows ||
816 mi_col_pred >= cm->mi_cols)
817 return;
818
819 mbmi = set_offsets_extend(cm, xd, tile, bsize_pred, mi_row_pred, mi_col_pred,
820 mi_row_ori, mi_col_ori);
821 set_ref(cm, xd, 0, mi_row_pred, mi_col_pred);
822 if (has_second_ref(&xd->mi[0]->mbmi))
823 set_ref(cm, xd, 1, mi_row_pred, mi_col_pred);
824
Jingning Han2511c662016-12-22 11:57:34 -0800825 if (!bextend) mbmi->tx_size = max_txsize_lookup[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700826
827 xd->plane[0].dst.stride = dst_stride[0];
828 xd->plane[1].dst.stride = dst_stride[1];
829 xd->plane[2].dst.stride = dst_stride[2];
830 xd->plane[0].dst.buf = dst_buf[0] +
831 (r >> xd->plane[0].subsampling_y) * dst_stride[0] +
832 (c >> xd->plane[0].subsampling_x);
833 xd->plane[1].dst.buf = dst_buf[1] +
834 (r >> xd->plane[1].subsampling_y) * dst_stride[1] +
835 (c >> xd->plane[1].subsampling_x);
836 xd->plane[2].dst.buf = dst_buf[2] +
837 (r >> xd->plane[2].subsampling_y) * dst_stride[2] +
838 (c >> xd->plane[2].subsampling_x);
839
840 if (!b_sub8x8)
Yaowu Xuf883b422016-08-30 14:01:10 -0700841 av1_build_inter_predictors_sb_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700842#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700843 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700844#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700845 mi_row_pred, mi_col_pred, bsize_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700846 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700847 av1_build_inter_predictors_sb_sub8x8_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700848#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700849 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700850#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700851 mi_row_pred, mi_col_pred,
852 bsize_pred, block);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700853}
854
Yaowu Xuf883b422016-08-30 14:01:10 -0700855static void dec_extend_dir(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700856 const TileInfo *const tile, int block,
857 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
858 int mi_col, int mi_row_top, int mi_col_top,
859 uint8_t *dst_buf[3], int dst_stride[3], int dir) {
860 // dir: 0-lower, 1-upper, 2-left, 3-right
861 // 4-lowerleft, 5-upperleft, 6-lowerright, 7-upperright
Jingning Han93531242016-12-20 11:54:36 -0800862 const int mi_width = mi_size_wide[bsize];
863 const int mi_height = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700864 int xss = xd->plane[1].subsampling_x;
865 int yss = xd->plane[1].subsampling_y;
Jingning Hanfeb517c2016-12-21 16:02:07 -0800866#if CONFIG_CB4X4
867 const int unify_bsize = 1;
868#else
869 const int unify_bsize = 0;
870#endif
871 int b_sub8x8 = (bsize < BLOCK_8X8) && !unify_bsize ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700872 BLOCK_SIZE extend_bsize;
Jingning Han24f24a52016-12-27 10:13:28 -0800873 int mi_row_pred, mi_col_pred;
874
875 int wide_unit, high_unit;
876 int i, j;
877 int ext_offset = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700878
879 if (dir == 0 || dir == 1) {
Jingning Han93531242016-12-20 11:54:36 -0800880 extend_bsize =
881 (mi_width == mi_size_wide[BLOCK_8X8] || bsize < BLOCK_8X8 || xss < yss)
882 ? BLOCK_8X8
883 : BLOCK_16X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800884#if CONFIG_CB4X4
885 if (bsize < BLOCK_8X8) {
886 extend_bsize = BLOCK_4X4;
887 ext_offset = mi_size_wide[BLOCK_8X8];
888 }
889#endif
890
891 wide_unit = mi_size_wide[extend_bsize];
892 high_unit = mi_size_high[extend_bsize];
893
894 mi_row_pred = mi_row + ((dir == 0) ? mi_height : -(mi_height + ext_offset));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700895 mi_col_pred = mi_col;
896
Jingning Han24f24a52016-12-27 10:13:28 -0800897 for (j = 0; j < mi_height + ext_offset; j += high_unit)
898 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
899 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
900 mi_row_pred + j, mi_col_pred + i, mi_row_top,
901 mi_col_top, dst_buf, dst_stride, top_bsize,
902 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700903 } else if (dir == 2 || dir == 3) {
Jingning Han5b7706a2016-12-21 09:55:10 -0800904 extend_bsize =
905 (mi_height == mi_size_high[BLOCK_8X8] || bsize < BLOCK_8X8 || yss < xss)
906 ? BLOCK_8X8
907 : BLOCK_8X16;
Jingning Han24f24a52016-12-27 10:13:28 -0800908#if CONFIG_CB4X4
909 if (bsize < BLOCK_8X8) {
910 extend_bsize = BLOCK_4X4;
911 ext_offset = mi_size_wide[BLOCK_8X8];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700912 }
Jingning Han24f24a52016-12-27 10:13:28 -0800913#endif
914
915 wide_unit = mi_size_wide[extend_bsize];
916 high_unit = mi_size_high[extend_bsize];
917
918 mi_row_pred = mi_row;
919 mi_col_pred = mi_col + ((dir == 3) ? mi_width : -(mi_width + ext_offset));
920
921 for (j = 0; j < mi_height + ext_offset; j += high_unit)
922 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
923 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
924 mi_row_pred + j, mi_col_pred + i, mi_row_top,
925 mi_col_top, dst_buf, dst_stride, top_bsize,
926 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700927 } else {
928 extend_bsize = BLOCK_8X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800929#if CONFIG_CB4X4
930 if (bsize < BLOCK_8X8) {
931 extend_bsize = BLOCK_4X4;
932 ext_offset = mi_size_wide[BLOCK_8X8];
933 }
934#endif
935 wide_unit = mi_size_wide[extend_bsize];
936 high_unit = mi_size_high[extend_bsize];
937
Jingning Han5b7706a2016-12-21 09:55:10 -0800938 mi_row_pred = mi_row + ((dir == 4 || dir == 6) ? mi_height
Jingning Han24f24a52016-12-27 10:13:28 -0800939 : -(mi_height + ext_offset));
Jingning Han5b7706a2016-12-21 09:55:10 -0800940 mi_col_pred =
Jingning Han24f24a52016-12-27 10:13:28 -0800941 mi_col + ((dir == 6 || dir == 7) ? mi_width : -(mi_width + ext_offset));
942
943 for (j = 0; j < mi_height + ext_offset; j += high_unit)
944 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
945 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
946 mi_row_pred + j, mi_col_pred + i, mi_row_top,
947 mi_col_top, dst_buf, dst_stride, top_bsize,
948 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700949 }
950}
951
Yaowu Xuf883b422016-08-30 14:01:10 -0700952static void dec_extend_all(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700953 const TileInfo *const tile, int block,
954 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
955 int mi_col, int mi_row_top, int mi_col_top,
956 uint8_t *dst_buf[3], int dst_stride[3]) {
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, 0);
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, 1);
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, 2);
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, 3);
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, 4);
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, 5);
969 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
970 mi_row_top, mi_col_top, dst_buf, dst_stride, 6);
971 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
972 mi_row_top, mi_col_top, dst_buf, dst_stride, 7);
973}
974
Yaowu Xuf883b422016-08-30 14:01:10 -0700975static void dec_predict_sb_complex(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700976 const TileInfo *const tile, int mi_row,
977 int mi_col, int mi_row_top, int mi_col_top,
978 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize,
979 uint8_t *dst_buf[3], int dst_stride[3]) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700980 const AV1_COMMON *const cm = &pbi->common;
Jingning Han5b7706a2016-12-21 09:55:10 -0800981 const int hbs = mi_size_wide[bsize] / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700982 const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
983 const BLOCK_SIZE subsize = get_subsize(bsize, partition);
984#if CONFIG_EXT_PARTITION_TYPES
985 const BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
986#endif
987 int i;
988 const int mi_offset = mi_row * cm->mi_stride + mi_col;
989 uint8_t *dst_buf1[3], *dst_buf2[3], *dst_buf3[3];
Jingning Hanfeb517c2016-12-21 16:02:07 -0800990#if CONFIG_CB4X4
991 const int unify_bsize = 1;
992#else
993 const int unify_bsize = 0;
994#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700995
996 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
997 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
998 DECLARE_ALIGNED(16, uint8_t, tmp_buf3[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
999 int dst_stride1[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1000 int dst_stride2[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1001 int dst_stride3[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1002
Yaowu Xuf883b422016-08-30 14:01:10 -07001003#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001004 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1005 int len = sizeof(uint16_t);
1006 dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
1007 dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_TX_SQUARE * len);
1008 dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_TX_SQUARE * len);
1009 dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
1010 dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_TX_SQUARE * len);
1011 dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_TX_SQUARE * len);
1012 dst_buf3[0] = CONVERT_TO_BYTEPTR(tmp_buf3);
1013 dst_buf3[1] = CONVERT_TO_BYTEPTR(tmp_buf3 + MAX_TX_SQUARE * len);
1014 dst_buf3[2] = CONVERT_TO_BYTEPTR(tmp_buf3 + 2 * MAX_TX_SQUARE * len);
1015 } else {
1016#endif
1017 dst_buf1[0] = tmp_buf1;
1018 dst_buf1[1] = tmp_buf1 + MAX_TX_SQUARE;
1019 dst_buf1[2] = tmp_buf1 + 2 * MAX_TX_SQUARE;
1020 dst_buf2[0] = tmp_buf2;
1021 dst_buf2[1] = tmp_buf2 + MAX_TX_SQUARE;
1022 dst_buf2[2] = tmp_buf2 + 2 * MAX_TX_SQUARE;
1023 dst_buf3[0] = tmp_buf3;
1024 dst_buf3[1] = tmp_buf3 + MAX_TX_SQUARE;
1025 dst_buf3[2] = tmp_buf3 + 2 * MAX_TX_SQUARE;
Yaowu Xuf883b422016-08-30 14:01:10 -07001026#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001027 }
1028#endif
1029
1030 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1031
1032 xd->mi = cm->mi_grid_visible + mi_offset;
1033 xd->mi[0] = cm->mi + mi_offset;
1034
1035 for (i = 0; i < MAX_MB_PLANE; i++) {
1036 xd->plane[i].dst.buf = dst_buf[i];
1037 xd->plane[i].dst.stride = dst_stride[i];
1038 }
1039
1040 switch (partition) {
1041 case PARTITION_NONE:
1042 assert(bsize < top_bsize);
1043 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1044 mi_row_top, mi_col_top, dst_buf, dst_stride,
1045 top_bsize, bsize, 0, 0);
1046 dec_extend_all(pbi, xd, tile, 0, bsize, top_bsize, mi_row, mi_col,
1047 mi_row_top, mi_col_top, dst_buf, dst_stride);
1048 break;
1049 case PARTITION_HORZ:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001050 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001051 // For sub8x8, predict in 8x8 unit
1052 // First half
1053 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1054 mi_row_top, mi_col_top, dst_buf, dst_stride,
1055 top_bsize, BLOCK_8X8, 1, 0);
1056 if (bsize < top_bsize)
1057 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1058 mi_row_top, mi_col_top, dst_buf, dst_stride);
1059
1060 // Second half
1061 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1062 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1063 top_bsize, BLOCK_8X8, 1, 1);
1064 if (bsize < top_bsize)
1065 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1066 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1067
1068 // weighted average to smooth the boundary
1069 xd->plane[0].dst.buf = dst_buf[0];
1070 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001071 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001072 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1073 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1074 0);
1075 } else {
1076 // First half
1077 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1078 mi_row_top, mi_col_top, dst_buf, dst_stride,
1079 top_bsize, subsize, 0, 0);
1080 if (bsize < top_bsize)
1081 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1082 mi_row_top, mi_col_top, dst_buf, dst_stride);
1083 else
1084 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1085 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1086
1087 if (mi_row + hbs < cm->mi_rows) {
1088 // Second half
1089 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col,
1090 mi_row + hbs, mi_col, mi_row_top, mi_col_top,
1091 dst_buf1, dst_stride1, top_bsize, subsize, 0, 0);
1092 if (bsize < top_bsize)
1093 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1094 mi_col, mi_row_top, mi_col_top, dst_buf1,
1095 dst_stride1);
1096 else
1097 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1098 mi_col, mi_row_top, mi_col_top, dst_buf1,
1099 dst_stride1, 1);
1100
1101 // weighted average to smooth the boundary
1102 for (i = 0; i < MAX_MB_PLANE; i++) {
1103 xd->plane[i].dst.buf = dst_buf[i];
1104 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001105 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001106 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1107 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1108 PARTITION_HORZ, i);
1109 }
1110 }
1111 }
1112 break;
1113 case PARTITION_VERT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001114 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001115 // First half
1116 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1117 mi_row_top, mi_col_top, dst_buf, dst_stride,
1118 top_bsize, BLOCK_8X8, 1, 0);
1119 if (bsize < top_bsize)
1120 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1121 mi_row_top, mi_col_top, dst_buf, dst_stride);
1122
1123 // Second half
1124 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1125 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1126 top_bsize, BLOCK_8X8, 1, 1);
1127 if (bsize < top_bsize)
1128 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1129 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1130
1131 // Smooth
1132 xd->plane[0].dst.buf = dst_buf[0];
1133 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001134 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001135 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1136 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1137 0);
1138 } else {
1139 // First half
1140 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1141 mi_row_top, mi_col_top, dst_buf, dst_stride,
1142 top_bsize, subsize, 0, 0);
1143 if (bsize < top_bsize)
1144 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1145 mi_row_top, mi_col_top, dst_buf, dst_stride);
1146 else
1147 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1148 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1149
1150 // Second half
1151 if (mi_col + hbs < cm->mi_cols) {
1152 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1153 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1154 dst_stride1, top_bsize, subsize, 0, 0);
1155 if (bsize < top_bsize)
1156 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1157 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1158 dst_stride1);
1159 else
1160 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1161 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1162 dst_stride1, 2);
1163
1164 // Smooth
1165 for (i = 0; i < MAX_MB_PLANE; i++) {
1166 xd->plane[i].dst.buf = dst_buf[i];
1167 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001168 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001169 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1170 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1171 PARTITION_VERT, i);
1172 }
1173 }
1174 }
1175 break;
1176 case PARTITION_SPLIT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001177 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001178 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1179 mi_row_top, mi_col_top, dst_buf, dst_stride,
1180 top_bsize, BLOCK_8X8, 1, 0);
1181 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1182 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1183 top_bsize, BLOCK_8X8, 1, 1);
1184 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1185 mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1186 top_bsize, BLOCK_8X8, 1, 1);
1187 dec_predict_b_extend(pbi, xd, tile, 3, mi_row, mi_col, mi_row, mi_col,
1188 mi_row_top, mi_col_top, dst_buf3, dst_stride3,
1189 top_bsize, BLOCK_8X8, 1, 1);
1190 if (bsize < top_bsize) {
1191 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1192 mi_row_top, mi_col_top, dst_buf, dst_stride);
1193 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1194 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1195 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1196 mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1197 dec_extend_all(pbi, xd, tile, 3, subsize, top_bsize, mi_row, mi_col,
1198 mi_row_top, mi_col_top, dst_buf3, dst_stride3);
1199 }
1200 } else {
1201 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row_top,
1202 mi_col_top, subsize, top_bsize, dst_buf,
1203 dst_stride);
1204 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1205 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col + hbs,
1206 mi_row_top, mi_col_top, subsize, top_bsize,
1207 dst_buf1, dst_stride1);
1208 if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols)
1209 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col,
1210 mi_row_top, mi_col_top, subsize, top_bsize,
1211 dst_buf2, dst_stride2);
1212 if (mi_row + hbs < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1213 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col + hbs,
1214 mi_row_top, mi_col_top, subsize, top_bsize,
1215 dst_buf3, dst_stride3);
1216 }
1217 for (i = 0; i < MAX_MB_PLANE; i++) {
Jingning Han9e0976a2016-12-27 17:52:42 -08001218#if !CONFIG_CB4X4
Jingning Han24f24a52016-12-27 10:13:28 -08001219 if (bsize == BLOCK_8X8 && i != 0)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001220 continue; // Skip <4x4 chroma smoothing
Jingning Han9e0976a2016-12-27 17:52:42 -08001221#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001222 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001223 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001224 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1225 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1226 PARTITION_VERT, i);
1227 if (mi_row + hbs < cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001228 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001229 xd, dst_buf2[i], dst_stride2[i], dst_buf3[i], dst_stride3[i],
1230 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1231 PARTITION_VERT, i);
Yaowu Xuf883b422016-08-30 14:01:10 -07001232 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001233 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1234 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1235 PARTITION_HORZ, i);
1236 }
1237 } else if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001238 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001239 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1240 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1241 PARTITION_HORZ, i);
1242 }
1243 }
1244 break;
1245#if CONFIG_EXT_PARTITION_TYPES
1246 case PARTITION_HORZ_A:
1247 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1248 mi_row_top, mi_col_top, dst_buf, dst_stride,
1249 top_bsize, bsize2, 0, 0);
1250 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1251 mi_row_top, mi_col_top, dst_buf, dst_stride);
1252
1253 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1254 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1255 dst_stride1, top_bsize, bsize2, 0, 0);
1256 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1257 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1258
1259 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1260 mi_col, mi_row_top, mi_col_top, dst_buf2,
1261 dst_stride2, top_bsize, subsize, 0, 0);
1262 if (bsize < top_bsize)
1263 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1264 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1265 else
1266 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1267 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1268 1);
1269
1270 for (i = 0; i < MAX_MB_PLANE; i++) {
1271 xd->plane[i].dst.buf = dst_buf[i];
1272 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001273 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001274 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1275 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1276 i);
1277 }
1278 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001279 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001280 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1281 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1282 i);
1283 }
1284 break;
1285 case PARTITION_VERT_A:
1286
1287 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1288 mi_row_top, mi_col_top, dst_buf, dst_stride,
1289 top_bsize, bsize2, 0, 0);
1290 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1291 mi_row_top, mi_col_top, dst_buf, dst_stride);
1292
1293 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1294 mi_col, mi_row_top, mi_col_top, dst_buf1,
1295 dst_stride1, top_bsize, bsize2, 0, 0);
1296 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1297 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1298
1299 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1300 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1301 dst_stride2, top_bsize, subsize, 0, 0);
1302 if (bsize < top_bsize)
1303 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1304 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1305 dst_stride2);
1306 else
1307 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1308 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1309 dst_stride2, 2);
1310
1311 for (i = 0; i < MAX_MB_PLANE; i++) {
1312 xd->plane[i].dst.buf = dst_buf[i];
1313 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001314 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001315 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1316 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1317 i);
1318 }
1319 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001320 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001321 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1322 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1323 i);
1324 }
1325 break;
1326 case PARTITION_HORZ_B:
1327 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1328 mi_row_top, mi_col_top, dst_buf, dst_stride,
1329 top_bsize, subsize, 0, 0);
1330 if (bsize < top_bsize)
1331 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1332 mi_row_top, mi_col_top, dst_buf, dst_stride);
1333 else
1334 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1335 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1336
1337 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1338 mi_col, mi_row_top, mi_col_top, dst_buf1,
1339 dst_stride1, top_bsize, bsize2, 0, 0);
1340 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1341 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1342
1343 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1344 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1345 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1346 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1347 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1348 dst_stride2);
1349
1350 for (i = 0; i < MAX_MB_PLANE; i++) {
1351 xd->plane[i].dst.buf = dst_buf1[i];
1352 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001353 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001354 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1355 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1356 PARTITION_VERT, i);
1357 }
1358 for (i = 0; i < MAX_MB_PLANE; i++) {
1359 xd->plane[i].dst.buf = dst_buf[i];
1360 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001361 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001362 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1363 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1364 i);
1365 }
1366 break;
1367 case PARTITION_VERT_B:
1368 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1369 mi_row_top, mi_col_top, dst_buf, dst_stride,
1370 top_bsize, subsize, 0, 0);
1371 if (bsize < top_bsize)
1372 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1373 mi_row_top, mi_col_top, dst_buf, dst_stride);
1374 else
1375 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1376 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1377
1378 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1379 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1380 dst_stride1, top_bsize, bsize2, 0, 0);
1381 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1382 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1383
1384 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1385 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1386 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1387 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1388 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1389 dst_stride2);
1390
1391 for (i = 0; i < MAX_MB_PLANE; i++) {
1392 xd->plane[i].dst.buf = dst_buf1[i];
1393 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001394 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001395 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1396 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1397 PARTITION_HORZ, i);
1398 }
1399 for (i = 0; i < MAX_MB_PLANE; i++) {
1400 xd->plane[i].dst.buf = dst_buf[i];
1401 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001402 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001403 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1404 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1405 i);
1406 }
1407 break;
1408#endif // CONFIG_EXT_PARTITION_TYPES
1409 default: assert(0);
1410 }
1411}
1412
Yaowu Xuf883b422016-08-30 14:01:10 -07001413static void set_segment_id_supertx(const AV1_COMMON *const cm, const int mi_row,
1414 const int mi_col, const BLOCK_SIZE bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001415 const struct segmentation *seg = &cm->seg;
Jingning Han5b7706a2016-12-21 09:55:10 -08001416 const int miw = AOMMIN(mi_size_wide[bsize], cm->mi_cols - mi_col);
1417 const int mih = AOMMIN(mi_size_high[bsize], cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001418 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1419 MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
1420 int r, c;
1421 int seg_id_supertx = MAX_SEGMENTS;
1422
1423 if (!seg->enabled) {
1424 seg_id_supertx = 0;
1425 } else {
1426 // Find the minimum segment_id
1427 for (r = 0; r < mih; r++)
1428 for (c = 0; c < miw; c++)
1429 seg_id_supertx =
Yaowu Xuf883b422016-08-30 14:01:10 -07001430 AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001431 assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);
1432 }
1433
1434 // Assign the the segment_id back to segment_id_supertx
1435 for (r = 0; r < mih; r++)
1436 for (c = 0; c < miw; c++)
1437 mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
1438}
1439#endif // CONFIG_SUPERTX
1440
Yue Chen64550b62017-01-12 12:18:22 -08001441static void decode_mbmi_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001442#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001443 int supertx_enabled,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001444#endif // CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001445 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001446#if CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001447 PARTITION_TYPE partition,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001448#endif // CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001449 BLOCK_SIZE bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001450 AV1_COMMON *const cm = &pbi->common;
Jingning Han85dc03f2016-12-06 16:03:10 -08001451 const int bw = mi_size_wide[bsize];
1452 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -07001453 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1454 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Nathan E. Eggeebbd4792016-10-05 19:30:15 -04001455
Michael Bebenita6048d052016-08-25 14:40:54 -07001456#if CONFIG_ACCOUNTING
1457 aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
1458#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001459#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001460 if (supertx_enabled) {
Yue Chen64550b62017-01-12 12:18:22 -08001461 set_mb_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001462 } else {
Yue Chen64550b62017-01-12 12:18:22 -08001463 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001464 }
1465#if CONFIG_EXT_PARTITION_TYPES
1466 xd->mi[0]->mbmi.partition = partition;
1467#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001468 av1_read_mode_info(pbi, xd, supertx_enabled, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001469#else
Yue Chen64550b62017-01-12 12:18:22 -08001470 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001471#if CONFIG_EXT_PARTITION_TYPES
1472 xd->mi[0]->mbmi.partition = partition;
1473#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001474 av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001475#endif // CONFIG_SUPERTX
1476
1477 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1478 const BLOCK_SIZE uv_subsize =
1479 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1480 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -07001481 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001482 "Invalid block size.");
1483 }
1484
1485#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001486 xd->mi[0]->mbmi.segment_id_supertx = MAX_SEGMENTS;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001487#endif // CONFIG_SUPERTX
1488
Yue Chen64550b62017-01-12 12:18:22 -08001489 xd->corrupted |= aom_reader_has_error(r);
1490}
1491
1492static void decode_token_and_recon_block(AV1Decoder *const pbi,
1493 MACROBLOCKD *const xd, int mi_row,
1494 int mi_col, aom_reader *r,
1495 BLOCK_SIZE bsize) {
1496 AV1_COMMON *const cm = &pbi->common;
1497 const int bw = mi_size_wide[bsize];
1498 const int bh = mi_size_high[bsize];
1499 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1500 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yue Chen64550b62017-01-12 12:18:22 -08001501
Angie Chiang44701f22017-02-27 10:36:44 -08001502 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
1503 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Yue Chen19e7aa82016-11-30 14:05:39 -08001504
Arild Fuldseth07441162016-08-15 15:07:52 +02001505#if CONFIG_DELTA_Q
1506 if (cm->delta_q_present_flag) {
1507 int i;
1508 for (i = 0; i < MAX_SEGMENTS; i++) {
1509 xd->plane[0].seg_dequant[i][0] =
1510 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
1511 xd->plane[0].seg_dequant[i][1] =
1512 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
1513 xd->plane[1].seg_dequant[i][0] =
1514 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1515 xd->plane[1].seg_dequant[i][1] =
1516 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1517 xd->plane[2].seg_dequant[i][0] =
1518 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1519 xd->plane[2].seg_dequant[i][1] =
1520 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1521 }
1522 }
1523#endif
1524
Jingning Han41bb3392016-12-14 10:46:48 -08001525#if CONFIG_CB4X4
1526 if (mbmi->skip) reset_skip_context(xd, bsize);
1527#else
Jingning Hand39cc722016-12-02 14:03:26 -08001528 if (mbmi->skip) reset_skip_context(xd, AOMMAX(BLOCK_8X8, bsize));
Jingning Han41bb3392016-12-14 10:46:48 -08001529#endif
Jingning Hand39cc722016-12-02 14:03:26 -08001530
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001531#if CONFIG_COEF_INTERLEAVE
1532 {
1533 const struct macroblockd_plane *const pd_y = &xd->plane[0];
1534 const struct macroblockd_plane *const pd_c = &xd->plane[1];
1535 const TX_SIZE tx_log2_y = mbmi->tx_size;
1536 const TX_SIZE tx_log2_c = get_uv_tx_size(mbmi, pd_c);
1537 const int tx_sz_y = (1 << tx_log2_y);
1538 const int tx_sz_c = (1 << tx_log2_c);
1539 const int num_4x4_w_y = pd_y->n4_w;
1540 const int num_4x4_h_y = pd_y->n4_h;
1541 const int num_4x4_w_c = pd_c->n4_w;
1542 const int num_4x4_h_c = pd_c->n4_h;
1543 const int max_4x4_w_y = get_max_4x4_size(num_4x4_w_y, xd->mb_to_right_edge,
1544 pd_y->subsampling_x);
1545 const int max_4x4_h_y = get_max_4x4_size(num_4x4_h_y, xd->mb_to_bottom_edge,
1546 pd_y->subsampling_y);
1547 const int max_4x4_w_c = get_max_4x4_size(num_4x4_w_c, xd->mb_to_right_edge,
1548 pd_c->subsampling_x);
1549 const int max_4x4_h_c = get_max_4x4_size(num_4x4_h_c, xd->mb_to_bottom_edge,
1550 pd_c->subsampling_y);
1551
1552 // The max_4x4_w/h may be smaller than tx_sz under some corner cases,
1553 // i.e. when the SB is splitted by tile boundaries.
1554 const int tu_num_w_y = (max_4x4_w_y + tx_sz_y - 1) / tx_sz_y;
1555 const int tu_num_h_y = (max_4x4_h_y + tx_sz_y - 1) / tx_sz_y;
1556 const int tu_num_w_c = (max_4x4_w_c + tx_sz_c - 1) / tx_sz_c;
1557 const int tu_num_h_c = (max_4x4_h_c + tx_sz_c - 1) / tx_sz_c;
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001558 const int tu_num_c = tu_num_w_c * tu_num_h_c;
1559
1560 if (!is_inter_block(mbmi)) {
1561 int tu_idx_c = 0;
1562 int row_y, col_y, row_c, col_c;
1563 int plane;
1564
1565#if CONFIG_PALETTE
1566 for (plane = 0; plane <= 1; ++plane) {
1567 if (mbmi->palette_mode_info.palette_size[plane])
1568 av1_decode_palette_tokens(xd, plane, r);
1569 }
1570#endif
1571
1572 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1573 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1574 // luma
1575 predict_and_reconstruct_intra_block(
1576 cm, xd, r, mbmi, 0, row_y * tx_sz_y, col_y * tx_sz_y, tx_log2_y);
1577 // chroma
1578 if (tu_idx_c < tu_num_c) {
1579 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1580 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1581 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c,
1582 col_c, tx_log2_c);
1583 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c,
1584 col_c, tx_log2_c);
1585 tu_idx_c++;
1586 }
1587 }
1588 }
1589
1590 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1591 while (tu_idx_c < tu_num_c) {
1592 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1593 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1594 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c, col_c,
1595 tx_log2_c);
1596 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c, col_c,
1597 tx_log2_c);
1598 tu_idx_c++;
1599 }
1600 } else {
1601 // Prediction
David Barkerac37fa32016-12-02 12:30:21 +00001602 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001603 AOMMAX(bsize, BLOCK_8X8));
1604
1605 // Reconstruction
1606 if (!mbmi->skip) {
1607 int eobtotal = 0;
1608 int tu_idx_c = 0;
1609 int row_y, col_y, row_c, col_c;
1610
1611 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1612 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1613 // luma
1614 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 0,
1615 row_y * tx_sz_y,
1616 col_y * tx_sz_y, tx_log2_y);
1617 // chroma
1618 if (tu_idx_c < tu_num_c) {
1619 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1620 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1621 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1622 1, row_c, col_c, tx_log2_c);
1623 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1624 2, row_c, col_c, tx_log2_c);
1625 tu_idx_c++;
1626 }
1627 }
1628 }
1629
1630 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1631 while (tu_idx_c < tu_num_c) {
1632 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1633 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1634 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 1,
1635 row_c, col_c, tx_log2_c);
1636 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 2,
1637 row_c, col_c, tx_log2_c);
1638 tu_idx_c++;
1639 }
1640
1641 if (bsize >= BLOCK_8X8 && eobtotal == 0)
1642#if CONFIG_MISC_FIXES
1643 mbmi->has_no_coeffs = 1;
1644#else
1645 mbmi->skip = 1;
1646#endif
1647 }
1648 }
1649 }
1650#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001651 if (!is_inter_block(mbmi)) {
1652 int plane;
Fangwen Fub3be9262017-03-06 15:34:28 -08001653#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001654 for (plane = 0; plane <= 1; ++plane) {
1655 if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xuf883b422016-08-30 14:01:10 -07001656 av1_decode_palette_tokens(xd, plane, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001657 }
Fangwen Fub3be9262017-03-06 15:34:28 -08001658#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001659 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1660 const struct macroblockd_plane *const pd = &xd->plane[plane];
Angie Chiang7fcfee42017-02-24 15:51:03 -08001661 const TX_SIZE tx_size = get_tx_size(plane, xd);
Jingning Han2d64f122016-10-21 12:44:29 -07001662 const int stepr = tx_size_high_unit[tx_size];
1663 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han41bb3392016-12-14 10:46:48 -08001664#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001665#if CONFIG_CHROMA_2X2
1666 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1667#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001668 const BLOCK_SIZE plane_bsize =
1669 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001670#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001671#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001672 const BLOCK_SIZE plane_bsize =
1673 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001674#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001675 int row, col;
Jingning Hanbafee8d2016-12-02 10:25:03 -08001676 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1677 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001678#if CONFIG_CB4X4
1679 if (bsize < BLOCK_8X8 && plane && !is_chroma_reference(mi_row, mi_col))
1680 continue;
1681#endif
1682
Yaowu Xuc27fc142016-08-22 16:08:15 -07001683 for (row = 0; row < max_blocks_high; row += stepr)
1684 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001685 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane, row, col,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001686 tx_size);
1687 }
1688 } else {
Yue Chen9ab6d712017-01-12 15:50:46 -08001689 int ref;
1690
1691 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
1692 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
1693 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
1694
1695 xd->block_refs[ref] = ref_buf;
1696 if ((!av1_is_valid_scale(&ref_buf->sf)))
1697 aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
1698 "Reference frame has invalid dimensions");
1699 av1_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col, &ref_buf->sf);
1700 }
Yue Chen69f18e12016-09-08 14:48:15 -07001701#if CONFIG_WARPED_MOTION
1702 if (mbmi->motion_mode == WARPED_CAUSAL) {
1703 int i;
1704#if CONFIG_AOM_HIGHBITDEPTH
1705 int use_hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH;
1706#endif // CONFIG_AOM_HIGHBITDEPTH
1707
1708 for (i = 0; i < 3; ++i) {
1709 const struct macroblockd_plane *pd = &xd->plane[i];
1710
1711 av1_warp_plane(&mbmi->wm_params[0],
1712#if CONFIG_AOM_HIGHBITDEPTH
1713 xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH, xd->bd,
1714#endif // CONFIG_AOM_HIGHBITDEPTH
1715 pd->pre[0].buf0, pd->pre[0].width, pd->pre[0].height,
1716 pd->pre[0].stride, pd->dst.buf,
1717 ((mi_col * MI_SIZE) >> pd->subsampling_x),
1718 ((mi_row * MI_SIZE) >> pd->subsampling_y),
Jingning Hanff6ee6a2016-12-07 09:55:21 -08001719 xd->n8_w * (MI_SIZE >> pd->subsampling_x),
1720 xd->n8_h * (MI_SIZE >> pd->subsampling_y),
1721 pd->dst.stride, pd->subsampling_x, pd->subsampling_y, 16,
1722 16, 0);
Yue Chen69f18e12016-09-08 14:48:15 -07001723 }
1724 } else {
1725#endif // CONFIG_WARPED_MOTION
Jingning Han41bb3392016-12-14 10:46:48 -08001726#if CONFIG_CB4X4
1727 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL, bsize);
1728#else
1729 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
1730 AOMMAX(bsize, BLOCK_8X8));
1731#endif
Yue Chen69f18e12016-09-08 14:48:15 -07001732#if CONFIG_WARPED_MOTION
1733 }
1734#endif // CONFIG_WARPED_MOTION
Yue Chencb60b182016-10-13 15:18:22 -07001735#if CONFIG_MOTION_VAR
1736 if (mbmi->motion_mode == OBMC_CAUSAL) {
Yue Chenf27b1602017-01-13 11:11:43 -08001737#if CONFIG_NCOBMC
1738 av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
1739#else
Yue Chen894fcce2016-10-21 16:50:52 -07001740 av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yue Chenf27b1602017-01-13 11:11:43 -08001741#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001742 }
Yue Chencb60b182016-10-13 15:18:22 -07001743#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001744
1745 // Reconstruction
1746 if (!mbmi->skip) {
1747 int eobtotal = 0;
1748 int plane;
1749
1750 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1751 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han41bb3392016-12-14 10:46:48 -08001752#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001753#if CONFIG_CHROMA_2X2
1754 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1755#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001756 const BLOCK_SIZE plane_bsize =
1757 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001758#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001759#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001760 const BLOCK_SIZE plane_bsize =
1761 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001762#endif
Jingning Hanbafee8d2016-12-02 10:25:03 -08001763 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1764 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001765 int row, col;
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001766
1767#if CONFIG_CB4X4
1768 if (bsize < BLOCK_8X8 && plane && !is_chroma_reference(mi_row, mi_col))
1769 continue;
1770#endif
1771
Yaowu Xuc27fc142016-08-22 16:08:15 -07001772#if CONFIG_VAR_TX
Jingning Han70e5f3f2016-11-09 17:03:07 -08001773 const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07001774 const int bh_var_tx = tx_size_high_unit[max_tx_size];
1775 const int bw_var_tx = tx_size_wide_unit[max_tx_size];
Jingning Hanbafee8d2016-12-02 10:25:03 -08001776 for (row = 0; row < max_blocks_high; row += bh_var_tx)
1777 for (col = 0; col < max_blocks_wide; col += bw_var_tx)
Jingning Hanfe45b212016-11-22 10:30:23 -08001778 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, row, col,
1779 max_tx_size, &eobtotal);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001780#else
Angie Chiang7fcfee42017-02-24 15:51:03 -08001781 const TX_SIZE tx_size = get_tx_size(plane, xd);
Jingning Han2d64f122016-10-21 12:44:29 -07001782 const int stepr = tx_size_high_unit[tx_size];
1783 const int stepc = tx_size_wide_unit[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001784 for (row = 0; row < max_blocks_high; row += stepr)
1785 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001786 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1787 plane, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001788#endif
1789 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001790 }
1791 }
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001792#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001793
Yaowu Xuf883b422016-08-30 14:01:10 -07001794 xd->corrupted |= aom_reader_has_error(r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001795}
1796
Yue Chen9ab6d712017-01-12 15:50:46 -08001797#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
1798static void detoken_and_recon_sb(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1799 int mi_row, int mi_col, aom_reader *r,
1800 BLOCK_SIZE bsize) {
1801 AV1_COMMON *const cm = &pbi->common;
1802 const int hbs = mi_size_wide[bsize] >> 1;
1803#if CONFIG_CB4X4
1804 const int unify_bsize = 1;
1805#else
1806 const int unify_bsize = 0;
1807#endif
1808#if CONFIG_EXT_PARTITION_TYPES
1809 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1810#endif
1811 PARTITION_TYPE partition;
1812 BLOCK_SIZE subsize;
1813 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1814 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1815
1816 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1817
1818 partition = get_partition(cm, mi_row, mi_col, bsize);
1819 subsize = subsize_lookup[partition][bsize];
1820
1821 if (!hbs && !unify_bsize) {
1822 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1823 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1824 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1825 } else {
1826 switch (partition) {
1827 case PARTITION_NONE:
1828 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
1829 break;
1830 case PARTITION_HORZ:
1831 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1832 if (has_rows)
1833 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r,
1834 subsize);
1835 break;
1836 case PARTITION_VERT:
1837 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1838 if (has_cols)
1839 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r,
1840 subsize);
1841 break;
1842 case PARTITION_SPLIT:
1843 detoken_and_recon_sb(pbi, xd, mi_row, mi_col, r, subsize);
1844 detoken_and_recon_sb(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1845 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1846 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col + hbs, r, subsize);
1847 break;
1848#if CONFIG_EXT_PARTITION_TYPES
1849 case PARTITION_HORZ_A:
1850 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1851 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1852 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1853 break;
1854 case PARTITION_HORZ_B:
1855 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1856 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1857 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1858 bsize2);
1859 break;
1860 case PARTITION_VERT_A:
1861 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1862 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1863 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1864 break;
1865 case PARTITION_VERT_B:
1866 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1867 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1868 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1869 bsize2);
1870 break;
1871#endif
1872 default: assert(0 && "Invalid partition type");
1873 }
1874 }
1875}
1876#endif
1877
Yue Chen64550b62017-01-12 12:18:22 -08001878static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1879#if CONFIG_SUPERTX
1880 int supertx_enabled,
1881#endif // CONFIG_SUPERTX
1882 int mi_row, int mi_col, aom_reader *r,
1883#if CONFIG_EXT_PARTITION_TYPES
1884 PARTITION_TYPE partition,
1885#endif // CONFIG_EXT_PARTITION_TYPES
1886 BLOCK_SIZE bsize) {
1887 decode_mbmi_block(pbi, xd,
1888#if CONFIG_SUPERTX
1889 supertx_enabled,
1890#endif
1891 mi_row, mi_col, r,
1892#if CONFIG_EXT_PARTITION_TYPES
1893 partition,
1894#endif
1895 bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001896#if !(CONFIG_MOTION_VAR && CONFIG_NCOBMC)
Yue Chen64550b62017-01-12 12:18:22 -08001897#if CONFIG_SUPERTX
1898 if (!supertx_enabled)
1899#endif // CONFIG_SUPERTX
1900 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001901#endif
Yue Chen64550b62017-01-12 12:18:22 -08001902}
1903
Yaowu Xuf883b422016-08-30 14:01:10 -07001904static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
1905 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001906 int has_rows, int has_cols,
Jingning Han1beb0102016-12-07 11:08:30 -08001907 BLOCK_SIZE bsize) {
Alex Converse55c6bde2017-01-12 15:55:31 -08001908#if CONFIG_UNPOISON_PARTITION_CTX
1909 const int ctx =
1910 partition_plane_context(xd, mi_row, mi_col, has_rows, has_cols, bsize);
1911 const aom_prob *const probs = ctx >= 0 ? cm->fc->partition_prob[ctx] : NULL;
1912 FRAME_COUNTS *const counts = ctx >= 0 ? xd->counts : NULL;
1913#else
Jingning Han1beb0102016-12-07 11:08:30 -08001914 const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
Yaowu Xuf883b422016-08-30 14:01:10 -07001915 const aom_prob *const probs = cm->fc->partition_prob[ctx];
Alex Converse55c6bde2017-01-12 15:55:31 -08001916 FRAME_COUNTS *const counts = xd->counts;
1917#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001918 PARTITION_TYPE p;
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001919#if CONFIG_EC_ADAPT
1920 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1921 (void)cm;
1922#elif CONFIG_EC_MULTISYMBOL
1923 FRAME_CONTEXT *ec_ctx = cm->fc;
1924#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001925
1926 if (has_rows && has_cols)
1927#if CONFIG_EXT_PARTITION_TYPES
1928 if (bsize <= BLOCK_8X8)
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001929#if CONFIG_EC_MULTISYMBOL
1930 p = (PARTITION_TYPE)aom_read_symbol(r, ec_ctx->partition_cdf[ctx],
1931 PARTITION_TYPES, ACCT_STR);
1932#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001933 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001934#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001935 else
Michael Bebenita6048d052016-08-25 14:40:54 -07001936 p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs,
1937 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001938#else
Nathan E. Egge9d9eb6c2016-11-16 09:44:26 -05001939#if CONFIG_EC_MULTISYMBOL
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001940 p = (PARTITION_TYPE)aom_read_symbol(r, ec_ctx->partition_cdf[ctx],
Yaowu Xuf2581a32016-10-20 13:05:47 -07001941 PARTITION_TYPES, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001942#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001943 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001944#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001945#endif // CONFIG_EXT_PARTITION_TYPES
1946 else if (!has_rows && has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001947 p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001948 else if (has_rows && !has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001949 p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001950 else
1951 p = PARTITION_SPLIT;
1952
1953 if (counts) ++counts->partition[ctx][p];
1954
1955 return p;
1956}
1957
1958#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001959static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
1960 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001961 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
1962 return 1;
1963 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001964 const int ctx = av1_get_skip_context(xd);
Michael Bebenita6048d052016-08-25 14:40:54 -07001965 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001966 FRAME_COUNTS *counts = xd->counts;
1967 if (counts) ++counts->skip[ctx][skip];
1968 return skip;
1969 }
1970}
1971#endif // CONFIG_SUPERTX
Jean-Marc Valin01435132017-02-18 14:12:53 -05001972#if CONFIG_CDEF
Yaowu Xud71be782016-10-14 08:47:03 -07001973static int clpf_all_skip(const AV1_COMMON *cm, int mi_col, int mi_row,
1974 int size) {
1975 int r, c;
1976 int skip = 1;
1977 const int maxc = AOMMIN(size, cm->mi_cols - mi_col);
1978 const int maxr = AOMMIN(size, cm->mi_rows - mi_row);
1979 for (r = 0; r < maxr && skip; r++) {
1980 for (c = 0; c < maxc && skip; c++) {
1981 skip &= !!cm->mi_grid_visible[(mi_row + r) * cm->mi_stride + mi_col + c]
1982 ->mbmi.skip;
1983 }
1984 }
1985 return skip;
1986}
1987#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001988
1989// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -07001990static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001991#if CONFIG_SUPERTX
1992 int supertx_enabled,
1993#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001994 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001995 BLOCK_SIZE bsize, int n4x4_l2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001996 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001997 const int n8x8_l2 = n4x4_l2 - 1;
Jingning Hanff17e162016-12-07 17:58:18 -08001998 const int num_8x8_wh = mi_size_wide[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001999 const int hbs = num_8x8_wh >> 1;
Jingning Han41bb3392016-12-14 10:46:48 -08002000#if CONFIG_CB4X4
2001 const int unify_bsize = 1;
2002#else
2003 const int unify_bsize = 0;
2004#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002005 PARTITION_TYPE partition;
2006 BLOCK_SIZE subsize;
2007#if CONFIG_EXT_PARTITION_TYPES
2008 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
2009#endif
2010 const int has_rows = (mi_row + hbs) < cm->mi_rows;
2011 const int has_cols = (mi_col + hbs) < cm->mi_cols;
2012#if CONFIG_SUPERTX
2013 const int read_token = !supertx_enabled;
2014 int skip = 0;
Jingning Han2511c662016-12-22 11:57:34 -08002015 TX_SIZE supertx_size = max_txsize_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002016 const TileInfo *const tile = &xd->tile;
2017 int txfm = DCT_DCT;
2018#endif // CONFIG_SUPERTX
2019
2020 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
2021
Jingning Han6dbacf22016-12-14 17:28:35 -08002022 partition = (n4x4_l2 == 0) ? PARTITION_NONE
2023 : read_partition(cm, xd, mi_row, mi_col, r,
2024 has_rows, has_cols, bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002025 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
Yushin Cho77bba8d2016-11-04 16:36:56 -07002026
2027#if CONFIG_PVQ
2028 assert(partition < PARTITION_TYPES);
2029 assert(subsize < BLOCK_SIZES);
2030#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002031#if CONFIG_SUPERTX
2032 if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
2033 bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
2034 const int supertx_context = partition_supertx_context_lookup[partition];
Michael Bebenita6048d052016-08-25 14:40:54 -07002035 supertx_enabled = aom_read(
2036 r, cm->fc->supertx_prob[supertx_context][supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002037 if (xd->counts)
2038 xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
2039#if CONFIG_VAR_TX
2040 if (supertx_enabled) xd->supertx_size = supertx_size;
2041#endif
2042 }
2043#endif // CONFIG_SUPERTX
Jingning Han41bb3392016-12-14 10:46:48 -08002044 if (!hbs && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002045 // calculate bmode block dimensions (log 2)
2046 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
2047 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
2048 decode_block(pbi, xd,
2049#if CONFIG_SUPERTX
2050 supertx_enabled,
2051#endif // CONFIG_SUPERTX
2052 mi_row, mi_col, r,
2053#if CONFIG_EXT_PARTITION_TYPES
2054 partition,
2055#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002056 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002057 } else {
2058 switch (partition) {
2059 case PARTITION_NONE:
2060 decode_block(pbi, xd,
2061#if CONFIG_SUPERTX
2062 supertx_enabled,
2063#endif // CONFIG_SUPERTX
2064 mi_row, mi_col, r,
2065#if CONFIG_EXT_PARTITION_TYPES
2066 partition,
2067#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002068 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002069 break;
2070 case PARTITION_HORZ:
2071 decode_block(pbi, xd,
2072#if CONFIG_SUPERTX
2073 supertx_enabled,
2074#endif // CONFIG_SUPERTX
2075 mi_row, mi_col, r,
2076#if CONFIG_EXT_PARTITION_TYPES
2077 partition,
2078#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002079 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002080 if (has_rows)
2081 decode_block(pbi, xd,
2082#if CONFIG_SUPERTX
2083 supertx_enabled,
2084#endif // CONFIG_SUPERTX
2085 mi_row + hbs, mi_col, r,
2086#if CONFIG_EXT_PARTITION_TYPES
2087 partition,
2088#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002089 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002090 break;
2091 case PARTITION_VERT:
2092 decode_block(pbi, xd,
2093#if CONFIG_SUPERTX
2094 supertx_enabled,
2095#endif // CONFIG_SUPERTX
2096 mi_row, mi_col, r,
2097#if CONFIG_EXT_PARTITION_TYPES
2098 partition,
2099#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002100 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002101 if (has_cols)
2102 decode_block(pbi, xd,
2103#if CONFIG_SUPERTX
2104 supertx_enabled,
2105#endif // CONFIG_SUPERTX
2106 mi_row, mi_col + hbs, r,
2107#if CONFIG_EXT_PARTITION_TYPES
2108 partition,
2109#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002110 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002111 break;
2112 case PARTITION_SPLIT:
2113 decode_partition(pbi, xd,
2114#if CONFIG_SUPERTX
2115 supertx_enabled,
2116#endif // CONFIG_SUPERTX
2117 mi_row, mi_col, r, subsize, n8x8_l2);
2118 decode_partition(pbi, xd,
2119#if CONFIG_SUPERTX
2120 supertx_enabled,
2121#endif // CONFIG_SUPERTX
2122 mi_row, mi_col + hbs, r, subsize, n8x8_l2);
2123 decode_partition(pbi, xd,
2124#if CONFIG_SUPERTX
2125 supertx_enabled,
2126#endif // CONFIG_SUPERTX
2127 mi_row + hbs, mi_col, r, subsize, n8x8_l2);
2128 decode_partition(pbi, xd,
2129#if CONFIG_SUPERTX
2130 supertx_enabled,
2131#endif // CONFIG_SUPERTX
2132 mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
2133 break;
2134#if CONFIG_EXT_PARTITION_TYPES
2135 case PARTITION_HORZ_A:
2136 decode_block(pbi, xd,
2137#if CONFIG_SUPERTX
2138 supertx_enabled,
2139#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002140 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002141 decode_block(pbi, xd,
2142#if CONFIG_SUPERTX
2143 supertx_enabled,
2144#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002145 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002146 decode_block(pbi, xd,
2147#if CONFIG_SUPERTX
2148 supertx_enabled,
2149#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002150 mi_row + hbs, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002151 break;
2152 case PARTITION_HORZ_B:
2153 decode_block(pbi, xd,
2154#if CONFIG_SUPERTX
2155 supertx_enabled,
2156#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002157 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002158 decode_block(pbi, xd,
2159#if CONFIG_SUPERTX
2160 supertx_enabled,
2161#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002162 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002163 decode_block(pbi, xd,
2164#if CONFIG_SUPERTX
2165 supertx_enabled,
2166#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002167 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002168 break;
2169 case PARTITION_VERT_A:
2170 decode_block(pbi, xd,
2171#if CONFIG_SUPERTX
2172 supertx_enabled,
2173#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002174 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002175 decode_block(pbi, xd,
2176#if CONFIG_SUPERTX
2177 supertx_enabled,
2178#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002179 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002180 decode_block(pbi, xd,
2181#if CONFIG_SUPERTX
2182 supertx_enabled,
2183#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002184 mi_row, mi_col + hbs, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002185 break;
2186 case PARTITION_VERT_B:
2187 decode_block(pbi, xd,
2188#if CONFIG_SUPERTX
2189 supertx_enabled,
2190#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002191 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002192 decode_block(pbi, xd,
2193#if CONFIG_SUPERTX
2194 supertx_enabled,
2195#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002196 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002197 decode_block(pbi, xd,
2198#if CONFIG_SUPERTX
2199 supertx_enabled,
2200#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002201 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002202 break;
2203#endif
2204 default: assert(0 && "Invalid partition type");
2205 }
2206 }
2207
2208#if CONFIG_SUPERTX
2209 if (supertx_enabled && read_token) {
2210 uint8_t *dst_buf[3];
2211 int dst_stride[3], i;
2212 int offset = mi_row * cm->mi_stride + mi_col;
2213
2214 set_segment_id_supertx(cm, mi_row, mi_col, bsize);
2215
David Barker3aec8d62017-01-31 14:55:32 +00002216#if CONFIG_DELTA_Q
2217 if (cm->delta_q_present_flag) {
2218 for (i = 0; i < MAX_SEGMENTS; i++) {
2219 xd->plane[0].seg_dequant[i][0] =
2220 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
2221 xd->plane[0].seg_dequant[i][1] =
2222 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
2223 xd->plane[1].seg_dequant[i][0] =
2224 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2225 xd->plane[1].seg_dequant[i][1] =
2226 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2227 xd->plane[2].seg_dequant[i][0] =
2228 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2229 xd->plane[2].seg_dequant[i][1] =
2230 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2231 }
2232 }
2233#endif
2234
Yaowu Xuc27fc142016-08-22 16:08:15 -07002235 xd->mi = cm->mi_grid_visible + offset;
2236 xd->mi[0] = cm->mi + offset;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002237#if CONFIG_DEPENDENT_HORZTILES
2238 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2239 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols,
2240 cm->dependent_horz_tiles);
2241#else
Jingning Han5b7706a2016-12-21 09:55:10 -08002242 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2243 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002244#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002245 set_skip_context(xd, mi_row, mi_col);
2246 skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
2247 if (skip) {
2248 reset_skip_context(xd, bsize);
2249 } else {
2250#if CONFIG_EXT_TX
Sarah Parkere68a3e42017-02-16 14:03:24 -08002251 if (get_ext_tx_types(supertx_size, bsize, 1, cm->reduced_tx_set_used) >
2252 1) {
2253 const int eset =
2254 get_ext_tx_set(supertx_size, bsize, 1, cm->reduced_tx_set_used);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002255 if (eset > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002256 txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
Michael Bebenita6048d052016-08-25 14:40:54 -07002257 cm->fc->inter_ext_tx_prob[eset][supertx_size],
2258 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002259 if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
2260 }
2261 }
2262#else
2263 if (supertx_size < TX_32X32) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002264 txfm = aom_read_tree(r, av1_ext_tx_tree,
Jingning Han8f6eb182016-10-19 13:48:57 -07002265 cm->fc->inter_ext_tx_prob[supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002266 if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
2267 }
2268#endif // CONFIG_EXT_TX
2269 }
2270
Yaowu Xuf883b422016-08-30 14:01:10 -07002271 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002272 for (i = 0; i < MAX_MB_PLANE; i++) {
2273 dst_buf[i] = xd->plane[i].dst.buf;
2274 dst_stride[i] = xd->plane[i].dst.stride;
2275 }
2276 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
2277 bsize, dst_buf, dst_stride);
2278
2279 if (!skip) {
2280 int eobtotal = 0;
2281 MB_MODE_INFO *mbmi;
2282 set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
2283 mbmi = &xd->mi[0]->mbmi;
2284 mbmi->tx_type = txfm;
2285 assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
2286 for (i = 0; i < MAX_MB_PLANE; ++i) {
2287 const struct macroblockd_plane *const pd = &xd->plane[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002288 int row, col;
Angie Chiang7fcfee42017-02-24 15:51:03 -08002289 const TX_SIZE tx_size = get_tx_size(i, xd);
Jingning Han5b7706a2016-12-21 09:55:10 -08002290 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Jingning Han32b20282016-10-28 15:42:44 -07002291 const int stepr = tx_size_high_unit[tx_size];
2292 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han5b7706a2016-12-21 09:55:10 -08002293 const int max_blocks_wide = max_block_wide(xd, plane_bsize, i);
2294 const int max_blocks_high = max_block_high(xd, plane_bsize, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002295
2296 for (row = 0; row < max_blocks_high; row += stepr)
2297 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07002298 eobtotal += reconstruct_inter_block(
2299 cm, xd, r, mbmi->segment_id_supertx, i, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002300 }
Jingning Han24f24a52016-12-27 10:13:28 -08002301 if ((unify_bsize || !(subsize < BLOCK_8X8)) && eobtotal == 0) skip = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002302 }
2303 set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
2304 }
2305#endif // CONFIG_SUPERTX
2306
2307#if CONFIG_EXT_PARTITION_TYPES
2308 if (bsize >= BLOCK_8X8) {
2309 switch (partition) {
2310 case PARTITION_SPLIT:
2311 if (bsize > BLOCK_8X8) break;
2312 case PARTITION_NONE:
2313 case PARTITION_HORZ:
2314 case PARTITION_VERT:
2315 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
2316 break;
2317 case PARTITION_HORZ_A:
2318 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2319 update_partition_context(xd, mi_row + hbs, mi_col, subsize, subsize);
2320 break;
2321 case PARTITION_HORZ_B:
2322 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2323 update_partition_context(xd, mi_row + hbs, mi_col, bsize2, subsize);
2324 break;
2325 case PARTITION_VERT_A:
2326 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2327 update_partition_context(xd, mi_row, mi_col + hbs, subsize, subsize);
2328 break;
2329 case PARTITION_VERT_B:
2330 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2331 update_partition_context(xd, mi_row, mi_col + hbs, bsize2, subsize);
2332 break;
2333 default: assert(0 && "Invalid partition type");
2334 }
2335 }
2336#else
2337 // update partition context
2338 if (bsize >= BLOCK_8X8 &&
2339 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
Jingning Han1beb0102016-12-07 11:08:30 -08002340 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
David Barkerf8935c92016-10-26 14:54:06 +01002341#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xud71be782016-10-14 08:47:03 -07002342
Jean-Marc Valin01435132017-02-18 14:12:53 -05002343#if CONFIG_CDEF
David Barker9739f362016-11-10 09:29:32 +00002344#if CONFIG_EXT_PARTITION
2345 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128) {
2346 if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
2347 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2348 aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
2349 } else {
2350 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2351 0;
2352 }
2353 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64) {
2354#else
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002355 if (bsize == BLOCK_64X64) {
David Barker9739f362016-11-10 09:29:32 +00002356#endif
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002357 if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
2358 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2359 aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
2360 } else {
2361 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2362 0;
2363 }
2364 }
David Barker9739f362016-11-10 09:29:32 +00002365#if CONFIG_EXT_PARTITION
2366 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128 &&
2367 cm->clpf_strength_y && cm->clpf_size != CLPF_NOSIZE) {
2368 const int tl = mi_row * MI_SIZE / MIN_FB_SIZE * cm->clpf_stride +
2369 mi_col * MI_SIZE / MIN_FB_SIZE;
2370 if (cm->clpf_size == CLPF_128X128) {
2371 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
2372 } else if (cm->clpf_size == CLPF_64X64) {
2373 const int tr = tl + 2;
2374 const int bl = tl + 2 * cm->clpf_stride;
2375 const int br = tr + 2 * cm->clpf_stride;
2376 const int size = 64 / MI_SIZE;
2377
2378 // Up to four bits per SB
2379 if (!clpf_all_skip(cm, mi_col, mi_row, size))
2380 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
2381
2382 if (mi_col + size < cm->mi_cols &&
2383 !clpf_all_skip(cm, mi_col + size, mi_row, size))
2384 cm->clpf_blocks[tr] = aom_read_literal(r, 1, ACCT_STR);
2385
2386 if (mi_row + size < cm->mi_rows &&
2387 !clpf_all_skip(cm, mi_col, mi_row + size, size))
2388 cm->clpf_blocks[bl] = aom_read_literal(r, 1, ACCT_STR);
2389
2390 if (mi_col + size < cm->mi_cols && mi_row + size < cm->mi_rows &&
2391 !clpf_all_skip(cm, mi_col + size, mi_row + size, size))
2392 cm->clpf_blocks[br] = aom_read_literal(r, 1, ACCT_STR);
2393 } else if (cm->clpf_size == CLPF_32X32) {
2394 int i, j;
2395 const int size = 32 / MI_SIZE;
2396 for (i = 0; i < 4; ++i)
2397 for (j = 0; j < 4; ++j) {
2398 const int index = tl + i * cm->clpf_stride + j;
2399 if (mi_row + i * size < cm->mi_rows &&
2400 mi_col + j * size < cm->mi_cols &&
2401 !clpf_all_skip(cm, mi_col + j * size, mi_row + i * size, size))
2402 cm->clpf_blocks[index] = aom_read_literal(r, 1, ACCT_STR);
2403 }
2404 }
2405 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64 &&
2406#else
2407 if (bsize == BLOCK_64X64 &&
2408#endif // CONFIG_EXT_PARTITION
2409 cm->clpf_strength_y && cm->clpf_size != CLPF_NOSIZE) {
Yaowu Xud71be782016-10-14 08:47:03 -07002410 const int tl = mi_row * MI_SIZE / MIN_FB_SIZE * cm->clpf_stride +
2411 mi_col * MI_SIZE / MIN_FB_SIZE;
2412
2413 if (!((mi_row * MI_SIZE) & 127) && !((mi_col * MI_SIZE) & 127) &&
2414 cm->clpf_size == CLPF_128X128) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002415 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002416 } else if (cm->clpf_size == CLPF_64X64 &&
2417 !clpf_all_skip(cm, mi_col, mi_row, 64 / MI_SIZE)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002418 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002419 } else if (cm->clpf_size == CLPF_32X32) {
2420 const int tr = tl + 1;
2421 const int bl = tl + cm->clpf_stride;
2422 const int br = tr + cm->clpf_stride;
2423 const int size = 32 / MI_SIZE;
2424
2425 // Up to four bits per SB
2426 if (!clpf_all_skip(cm, mi_col, mi_row, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002427 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002428
2429 if (mi_col + size < cm->mi_cols &&
2430 !clpf_all_skip(cm, mi_col + size, mi_row, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002431 cm->clpf_blocks[tr] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002432
2433 if (mi_row + size < cm->mi_rows &&
2434 !clpf_all_skip(cm, mi_col, mi_row + size, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002435 cm->clpf_blocks[bl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002436
2437 if (mi_col + size < cm->mi_cols && mi_row + size < cm->mi_rows &&
2438 !clpf_all_skip(cm, mi_col + size, mi_row + size, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002439 cm->clpf_blocks[br] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002440 }
2441 }
Jean-Marc Valin01435132017-02-18 14:12:53 -05002442#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002443}
2444
Yaowu Xuc27fc142016-08-22 16:08:15 -07002445static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
2446 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002447 struct aom_internal_error_info *error_info,
Alex Converseeb780e72016-12-13 12:46:41 -08002448 aom_reader *r,
2449#if CONFIG_ANS && ANS_MAX_SYMBOLS
2450 int window_size,
2451#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
2452 aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002453 // Validate the calculated partition length. If the buffer
2454 // described by the partition can't be fully read, then restrict
2455 // it to the portion that can be (for EC mode) or throw an error.
2456 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002457 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002458 "Truncated packet or corrupt tile length");
2459
Alex Converse2cdf0d82016-12-13 13:53:09 -08002460#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08002461 r->window_size = window_size;
Alex Converse2cdf0d82016-12-13 13:53:09 -08002462#endif
Alex Converse346440b2017-01-03 13:47:37 -08002463 if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07002464 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002465 "Failed to allocate bool decoder %d", 1);
2466}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002467
Alex Conversea9598cd2017-02-03 14:18:05 -08002468#if !CONFIG_PVQ && !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Yaowu Xuf883b422016-08-30 14:01:10 -07002469static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
2470 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002471 int i, j, k, l, m;
Alex Conversea9598cd2017-02-03 14:18:05 -08002472#if CONFIG_EC_ADAPT
2473 const int node_limit = UNCONSTRAINED_NODES - 1;
2474#else
Thomas9ac55082016-09-23 18:04:17 +01002475 const int node_limit = UNCONSTRAINED_NODES;
Alex Conversea9598cd2017-02-03 14:18:05 -08002476#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002477
Michael Bebenita6048d052016-08-25 14:40:54 -07002478 if (aom_read_bit(r, ACCT_STR))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002479 for (i = 0; i < PLANE_TYPES; ++i)
2480 for (j = 0; j < REF_TYPES; ++j)
2481 for (k = 0; k < COEF_BANDS; ++k)
2482 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
Thomas9ac55082016-09-23 18:04:17 +01002483 for (m = 0; m < node_limit; ++m)
Michael Bebenita6048d052016-08-25 14:40:54 -07002484 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002485}
2486
Yaowu Xuf883b422016-08-30 14:01:10 -07002487static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002488 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
2489 TX_SIZE tx_size;
Jingning Han83630632016-12-16 11:27:25 -08002490 for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002491 read_coef_probs_common(fc->coef_probs[tx_size], r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002492}
Yushin Cho77bba8d2016-11-04 16:36:56 -07002493#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002494
Yaowu Xuf883b422016-08-30 14:01:10 -07002495static void setup_segmentation(AV1_COMMON *const cm,
2496 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002497 struct segmentation *const seg = &cm->seg;
2498 int i, j;
2499
2500 seg->update_map = 0;
2501 seg->update_data = 0;
2502
Yaowu Xuf883b422016-08-30 14:01:10 -07002503 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002504 if (!seg->enabled) return;
2505
2506 // Segmentation map update
2507 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2508 seg->update_map = 1;
2509 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002510 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002511 }
2512 if (seg->update_map) {
2513 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2514 seg->temporal_update = 0;
2515 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002516 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002517 }
2518 }
2519
2520 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07002521 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002522 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002523 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002524
Yaowu Xuf883b422016-08-30 14:01:10 -07002525 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002526
2527 for (i = 0; i < MAX_SEGMENTS; i++) {
2528 for (j = 0; j < SEG_LVL_MAX; j++) {
2529 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002530 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002531 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002532 av1_enable_segfeature(seg, i, j);
2533 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
2534 if (av1_is_segfeature_signed(j))
2535 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002536 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002537 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002538 }
2539 }
2540 }
2541}
2542
2543#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002544static void decode_restoration_mode(AV1_COMMON *cm,
2545 struct aom_read_bit_buffer *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002546 int p;
2547 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002548 if (aom_rb_read_bit(rb)) {
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002549#if USE_DOMAINTXFMRF
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002550 if (aom_rb_read_bit(rb))
2551 rsi->frame_restoration_type =
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002552 (aom_rb_read_bit(rb) ? RESTORE_DOMAINTXFMRF : RESTORE_SGRPROJ);
Debargha Mukherjee0e67b252016-12-08 09:22:44 -08002553 else
2554 rsi->frame_restoration_type = RESTORE_WIENER;
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002555#else
2556 rsi->frame_restoration_type =
2557 aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
2558#endif // USE_DOMAINTXFMRF
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002559 } else {
2560 rsi->frame_restoration_type =
2561 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
2562 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002563 for (p = 1; p < MAX_MB_PLANE; ++p) {
2564 cm->rst_info[p].frame_restoration_type =
2565 aom_rb_read_bit(rb) ? RESTORE_WIENER : RESTORE_NONE;
2566 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002567}
2568
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002569static void read_wiener_filter(WienerInfo *wiener_info, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002570 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002571 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2572 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002573 wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002574 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2575 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002576 wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002577 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2578 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002579 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002580 wiener_info->vfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002581 -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
2582 wiener_info->vfilter[2]);
2583
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002584 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002585 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2586 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002587 wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002588 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2589 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002590 wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002591 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2592 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002593 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002594 wiener_info->hfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002595 -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
2596 wiener_info->hfilter[2]);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002597}
2598
2599static void read_sgrproj_filter(SgrprojInfo *sgrproj_info, aom_reader *rb) {
2600 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
2601 sgrproj_info->xqd[0] =
2602 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN0;
2603 sgrproj_info->xqd[1] =
2604 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN1;
2605}
2606
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002607#if USE_DOMAINTXFMRF
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002608static void read_domaintxfmrf_filter(DomaintxfmrfInfo *domaintxfmrf_info,
2609 aom_reader *rb) {
2610 domaintxfmrf_info->sigma_r =
2611 aom_read_literal(rb, DOMAINTXFMRF_PARAMS_BITS, ACCT_STR);
2612}
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002613#endif // USE_DOMAINTXFMRF
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002614
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002615static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002616 int i, p;
clang-formatbda8d612016-09-19 15:55:46 -07002617 const int ntiles =
2618 av1_get_rest_ntiles(cm->width, cm->height, NULL, NULL, NULL, NULL);
Debargha Mukherjee1a0ae842017-01-26 16:45:22 -08002619 const int ntiles_uv =
2620 av1_get_rest_ntiles(ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
2621 ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y),
2622 NULL, NULL, NULL, NULL);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002623 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002624 if (rsi->frame_restoration_type != RESTORE_NONE) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002625 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002626 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002627 rsi->restoration_type[i] =
2628 aom_read_tree(rb, av1_switchable_restore_tree,
2629 cm->fc->switchable_restore_prob, ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002630 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002631 read_wiener_filter(&rsi->wiener_info[i], rb);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002632 } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002633 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002634#if USE_DOMAINTXFMRF
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002635 } else if (rsi->restoration_type[i] == RESTORE_DOMAINTXFMRF) {
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002636 read_domaintxfmrf_filter(&rsi->domaintxfmrf_info[i], rb);
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002637#endif // USE_DOMAINTXFMRF
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002638 }
2639 }
2640 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002641 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002642 if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002643 rsi->restoration_type[i] = RESTORE_WIENER;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002644 read_wiener_filter(&rsi->wiener_info[i], rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002645 } else {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002646 rsi->restoration_type[i] = RESTORE_NONE;
2647 }
2648 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002649 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002650 for (i = 0; i < ntiles; ++i) {
2651 if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
2652 rsi->restoration_type[i] = RESTORE_SGRPROJ;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002653 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
2654 } else {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002655 rsi->restoration_type[i] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002656 }
2657 }
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002658#if USE_DOMAINTXFMRF
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002659 } else if (rsi->frame_restoration_type == RESTORE_DOMAINTXFMRF) {
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002660 for (i = 0; i < ntiles; ++i) {
2661 if (aom_read(rb, RESTORE_NONE_DOMAINTXFMRF_PROB, ACCT_STR)) {
2662 rsi->restoration_type[i] = RESTORE_DOMAINTXFMRF;
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002663 read_domaintxfmrf_filter(&rsi->domaintxfmrf_info[i], rb);
2664 } else {
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002665 rsi->restoration_type[i] = RESTORE_NONE;
2666 }
2667 }
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002668#endif // USE_DOMAINTXFMRF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002669 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002670 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002671 for (p = 1; p < MAX_MB_PLANE; ++p) {
2672 rsi = &cm->rst_info[p];
2673 if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08002674 for (i = 0; i < ntiles_uv; ++i) {
2675 if (ntiles_uv > 1)
2676 rsi->restoration_type[i] =
2677 aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR) ? RESTORE_WIENER
2678 : RESTORE_NONE;
2679 else
2680 rsi->restoration_type[i] = RESTORE_WIENER;
2681 if (rsi->restoration_type[i] == RESTORE_WIENER) {
2682 read_wiener_filter(&rsi->wiener_info[i], rb);
2683 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002684 }
2685 }
2686 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002687}
2688#endif // CONFIG_LOOP_RESTORATION
2689
Yaowu Xuf883b422016-08-30 14:01:10 -07002690static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002691 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07002692 lf->filter_level = aom_rb_read_literal(rb, 6);
2693 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002694
2695 // Read in loop filter deltas applied at the MB level based on mode or ref
2696 // frame.
2697 lf->mode_ref_delta_update = 0;
2698
Yaowu Xuf883b422016-08-30 14:01:10 -07002699 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002700 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002701 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002702 if (lf->mode_ref_delta_update) {
2703 int i;
2704
2705 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002706 if (aom_rb_read_bit(rb))
2707 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002708
2709 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002710 if (aom_rb_read_bit(rb))
2711 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002712 }
2713 }
2714}
2715
Jean-Marc Valin01435132017-02-18 14:12:53 -05002716#if CONFIG_CDEF
Yaowu Xud71be782016-10-14 08:47:03 -07002717static void setup_clpf(AV1Decoder *pbi, struct aom_read_bit_buffer *rb) {
2718 AV1_COMMON *const cm = &pbi->common;
2719 const int width = pbi->cur_buf->buf.y_crop_width;
2720 const int height = pbi->cur_buf->buf.y_crop_height;
2721
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002722 cm->clpf_blocks = 0;
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02002723 cm->clpf_strength_y = aom_rb_read_literal(rb, 2);
2724 cm->clpf_strength_u = aom_rb_read_literal(rb, 2);
2725 cm->clpf_strength_v = aom_rb_read_literal(rb, 2);
2726 if (cm->clpf_strength_y) {
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002727 cm->clpf_size = aom_rb_read_literal(rb, 2);
Yaowu Xud71be782016-10-14 08:47:03 -07002728 if (cm->clpf_size != CLPF_NOSIZE) {
2729 int size;
2730 cm->clpf_stride =
2731 ((width + MIN_FB_SIZE - 1) & ~(MIN_FB_SIZE - 1)) >> MIN_FB_SIZE_LOG2;
2732 size =
2733 cm->clpf_stride * ((height + MIN_FB_SIZE - 1) & ~(MIN_FB_SIZE - 1)) >>
2734 MIN_FB_SIZE_LOG2;
2735 CHECK_MEM_ERROR(cm, cm->clpf_blocks, aom_malloc(size));
2736 memset(cm->clpf_blocks, -1, size);
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002737 }
2738 }
2739}
2740
Steinar Midtskogen2fd70ee2016-09-02 10:02:30 +02002741static int clpf_bit(UNUSED int k, UNUSED int l,
2742 UNUSED const YV12_BUFFER_CONFIG *rec,
2743 UNUSED const YV12_BUFFER_CONFIG *org,
2744 UNUSED const AV1_COMMON *cm, UNUSED int block_size,
2745 UNUSED int w, UNUSED int h, UNUSED unsigned int strength,
Steinar Midtskogen4305e6b2017-02-17 14:07:29 +01002746 UNUSED unsigned int fb_size_log2, int8_t *bit,
2747 UNUSED int plane) {
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002748 return *bit;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002749}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002750
Yaowu Xuf883b422016-08-30 14:01:10 -07002751static void setup_dering(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
2752 cm->dering_level = aom_rb_read_literal(rb, DERING_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002753}
Jean-Marc Valin01435132017-02-18 14:12:53 -05002754#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002755
Yaowu Xuf883b422016-08-30 14:01:10 -07002756static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2757 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002758}
2759
Yaowu Xuf883b422016-08-30 14:01:10 -07002760static void setup_quantization(AV1_COMMON *const cm,
2761 struct aom_read_bit_buffer *rb) {
2762 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002763 cm->y_dc_delta_q = read_delta_q(rb);
2764 cm->uv_dc_delta_q = read_delta_q(rb);
2765 cm->uv_ac_delta_q = read_delta_q(rb);
2766 cm->dequant_bit_depth = cm->bit_depth;
2767#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002768 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002769 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002770 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2771 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002772 } else {
2773 cm->min_qmlevel = 0;
2774 cm->max_qmlevel = 0;
2775 }
2776#endif
2777}
2778
Yaowu Xuf883b422016-08-30 14:01:10 -07002779static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002780 // Build y/uv dequant values based on segmentation.
2781 int i = 0;
2782#if CONFIG_AOM_QM
2783 int lossless;
2784 int j = 0;
2785 int qmlevel;
2786 int using_qm = cm->using_qmatrix;
2787 int minqm = cm->min_qmlevel;
2788 int maxqm = cm->max_qmlevel;
2789#endif
2790#if CONFIG_NEW_QUANT
2791 int b;
2792 int dq;
2793#endif // CONFIG_NEW_QUANT
2794 if (cm->seg.enabled) {
2795 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002796 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002797 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002798 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2799 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002800 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002801 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002802 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002803 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002804#if CONFIG_AOM_QM
2805 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2806 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2807 // NB: depends on base index so there is only 1 set per frame
2808 // No quant weighting when lossless or signalled not using QM
2809 qmlevel = (lossless || using_qm == 0)
2810 ? NUM_QM_LEVELS - 1
2811 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2812 for (j = 0; j < TX_SIZES; ++j) {
2813 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2814 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2815 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2816 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2817 }
2818#endif // CONFIG_AOM_QM
2819#if CONFIG_NEW_QUANT
2820 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2821 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002822 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002823 cm->y_dequant_nuq[i][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002824 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002825 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002826 }
2827 }
2828#endif // CONFIG_NEW_QUANT
2829 }
2830 } else {
2831 const int qindex = cm->base_qindex;
2832 // When segmentation is disabled, only the first value is used. The
2833 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002834 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2835 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002836 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002837 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002838 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002839 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002840#if CONFIG_AOM_QM
2841 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2842 cm->uv_ac_delta_q == 0;
2843 // No quant weighting when lossless or signalled not using QM
2844 qmlevel = (lossless || using_qm == 0)
2845 ? NUM_QM_LEVELS - 1
2846 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2847 for (j = 0; j < TX_SIZES; ++j) {
2848 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2849 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2850 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2851 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2852 }
2853#endif
2854#if CONFIG_NEW_QUANT
2855 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2856 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002857 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002858 cm->y_dequant_nuq[0][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002859 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002860 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002861 }
2862 }
2863#endif // CONFIG_NEW_QUANT
2864 }
2865}
2866
Angie Chiang5678ad92016-11-21 09:38:40 -08002867static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002868 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07002869 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002870}
2871
Yaowu Xuf883b422016-08-30 14:01:10 -07002872static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002873 cm->render_width = cm->width;
2874 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002875 if (aom_rb_read_bit(rb))
2876 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002877}
2878
Yaowu Xuf883b422016-08-30 14:01:10 -07002879static void resize_mv_buffer(AV1_COMMON *cm) {
2880 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002881 cm->cur_frame->mi_rows = cm->mi_rows;
2882 cm->cur_frame->mi_cols = cm->mi_cols;
2883 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002884 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002885 sizeof(*cm->cur_frame->mvs)));
2886}
2887
Yaowu Xuf883b422016-08-30 14:01:10 -07002888static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002889#if CONFIG_SIZE_LIMIT
2890 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002891 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002892 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2893 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2894#endif
2895 if (cm->width != width || cm->height != height) {
2896 const int new_mi_rows =
2897 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2898 const int new_mi_cols =
2899 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2900
Yaowu Xuf883b422016-08-30 14:01:10 -07002901 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002902 // dimensions as well as the overall size.
2903 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002904 if (av1_alloc_context_buffers(cm, width, height))
2905 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002906 "Failed to allocate context buffers");
2907 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002908 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002909 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002910 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002911 cm->width = width;
2912 cm->height = height;
2913 }
2914 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2915 cm->mi_cols > cm->cur_frame->mi_cols) {
2916 resize_mv_buffer(cm);
2917 }
2918}
2919
Yaowu Xuf883b422016-08-30 14:01:10 -07002920static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002921 int width, height;
2922 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002923 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002924 resize_context_buffers(cm, width, height);
2925 setup_render_size(cm, rb);
2926
2927 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002928 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002929 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2930 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002931#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002932 cm->use_highbitdepth,
2933#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002934 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002935 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2936 pool->cb_priv)) {
2937 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002938 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002939 "Failed to allocate frame buffer");
2940 }
2941 unlock_buffer_pool(pool);
2942
2943 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2944 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2945 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2946 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2947 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2948 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2949 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2950}
2951
Yaowu Xuf883b422016-08-30 14:01:10 -07002952static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002953 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002954 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002955 int this_xss, int this_yss) {
2956 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2957 ref_yss == this_yss;
2958}
2959
Yaowu Xuf883b422016-08-30 14:01:10 -07002960static void setup_frame_size_with_refs(AV1_COMMON *cm,
2961 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002962 int width, height;
2963 int found = 0, i;
2964 int has_valid_ref_frame = 0;
2965 BufferPool *const pool = cm->buffer_pool;
2966 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002967 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002968 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2969 width = buf->y_crop_width;
2970 height = buf->y_crop_height;
2971 cm->render_width = buf->render_width;
2972 cm->render_height = buf->render_height;
2973 found = 1;
2974 break;
2975 }
2976 }
2977
2978 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002979 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002980 setup_render_size(cm, rb);
2981 }
2982
2983 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002984 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002985 "Invalid frame size");
2986
2987 // Check to make sure at least one of frames that this frame references
2988 // has valid dimensions.
2989 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2990 RefBuffer *const ref_frame = &cm->frame_refs[i];
2991 has_valid_ref_frame |=
2992 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2993 ref_frame->buf->y_crop_height, width, height);
2994 }
2995 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002996 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002997 "Referenced frame has invalid size");
2998 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2999 RefBuffer *const ref_frame = &cm->frame_refs[i];
3000 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
3001 ref_frame->buf->subsampling_x,
3002 ref_frame->buf->subsampling_y, cm->bit_depth,
3003 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07003004 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003005 "Referenced frame has incompatible color format");
3006 }
3007
3008 resize_context_buffers(cm, width, height);
3009
3010 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07003011 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003012 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
3013 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07003014#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003015 cm->use_highbitdepth,
3016#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003017 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003018 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
3019 pool->cb_priv)) {
3020 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07003021 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003022 "Failed to allocate frame buffer");
3023 }
3024 unlock_buffer_pool(pool);
3025
3026 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
3027 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
3028 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
3029 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
3030 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
3031 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
3032 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
3033}
3034
Yaowu Xuf883b422016-08-30 14:01:10 -07003035static void read_tile_info(AV1Decoder *const pbi,
3036 struct aom_read_bit_buffer *const rb) {
3037 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003038#if CONFIG_EXT_TILE
3039// Read the tile width/height
3040#if CONFIG_EXT_PARTITION
3041 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003042 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
3043 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003044 } else
3045#endif // CONFIG_EXT_PARTITION
3046 {
Yaowu Xuf883b422016-08-30 14:01:10 -07003047 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
3048 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003049 }
3050
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003051#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003052 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003053#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003054
Yaowu Xuc27fc142016-08-22 16:08:15 -07003055 cm->tile_width <<= cm->mib_size_log2;
3056 cm->tile_height <<= cm->mib_size_log2;
3057
Yaowu Xuf883b422016-08-30 14:01:10 -07003058 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
3059 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003060
3061 // Get the number of tiles
3062 cm->tile_cols = 1;
3063 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
3064
3065 cm->tile_rows = 1;
3066 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
3067
3068 if (cm->tile_cols * cm->tile_rows > 1) {
3069 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07003070 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
3071 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003072 }
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003073
3074#if CONFIG_DEPENDENT_HORZTILES
3075 if (cm->tile_rows <= 1)
3076 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
3077 else
3078 cm->dependent_horz_tiles = 0;
3079#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003080#else
3081 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07003082 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003083
3084 // columns
3085 max_ones = max_log2_tile_cols - min_log2_tile_cols;
3086 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07003087 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003088
3089 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07003090 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003091 "Invalid number of tile columns");
3092
3093 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07003094 cm->log2_tile_rows = aom_rb_read_bit(rb);
3095 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003096#if CONFIG_DEPENDENT_HORZTILES
3097 if (cm->log2_tile_rows != 0)
3098 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
3099 else
3100 cm->dependent_horz_tiles = 0;
3101#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003102#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003103 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003104#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003105
Yaowu Xuc27fc142016-08-22 16:08:15 -07003106 cm->tile_cols = 1 << cm->log2_tile_cols;
3107 cm->tile_rows = 1 << cm->log2_tile_rows;
3108
3109 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
3110 cm->tile_width >>= cm->log2_tile_cols;
3111 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
3112 cm->tile_height >>= cm->log2_tile_rows;
3113
3114 // round to integer multiples of superblock size
3115 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
3116 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
3117
Thomas Davies4974e522016-11-07 17:44:05 +00003118// tile size magnitude
3119#if !CONFIG_TILE_GROUPS
3120 if (cm->tile_rows > 1 || cm->tile_cols > 1)
3121#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003122 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003123#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00003124
Thomas Davies80188d12016-10-26 16:08:35 -07003125#if CONFIG_TILE_GROUPS
3126 // Store an index to the location of the tile group information
3127 pbi->tg_size_bit_offset = rb->bit_offset;
3128 pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
3129 if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
3130 pbi->tg_start =
3131 aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3132 pbi->tg_size =
3133 1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3134 }
3135#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003136}
3137
3138static int mem_get_varsize(const uint8_t *src, const int sz) {
3139 switch (sz) {
3140 case 1: return src[0];
3141 case 2: return mem_get_le16(src);
3142 case 3: return mem_get_le24(src);
3143 case 4: return mem_get_le32(src);
3144 default: assert("Invalid size" && 0); return -1;
3145 }
3146}
3147
3148#if CONFIG_EXT_TILE
3149// Reads the next tile returning its size and adjusting '*data' accordingly
3150// based on 'is_last'.
3151static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07003152 struct aom_internal_error_info *error_info,
3153 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003154 void *decrypt_state,
3155 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
3156 int tile_size_bytes, int col, int row) {
3157 size_t size;
3158
3159 size_t copy_size = 0;
3160 const uint8_t *copy_data = NULL;
3161
3162 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003163 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003164 "Truncated packet or corrupt tile length");
3165 if (decrypt_cb) {
3166 uint8_t be_data[4];
3167 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3168
3169 // Only read number of bytes in cm->tile_size_bytes.
3170 size = mem_get_varsize(be_data, tile_size_bytes);
3171 } else {
3172 size = mem_get_varsize(*data, tile_size_bytes);
3173 }
3174
3175 // The top bit indicates copy mode
3176 if ((size >> (tile_size_bytes * 8 - 1)) == 1) {
3177 // The remaining bits in the top byte signal the row offset
3178 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
3179
3180 // Currently, only use tiles in same column as reference tiles.
3181 copy_data = tile_buffers[row - offset][col].data;
3182 copy_size = tile_buffers[row - offset][col].size;
3183 size = 0;
3184 }
3185
3186 *data += tile_size_bytes;
3187
3188 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003189 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003190 "Truncated packet or corrupt tile size");
3191
3192 if (size > 0) {
3193 tile_buffers[row][col].data = *data;
3194 tile_buffers[row][col].size = size;
3195 } else {
3196 tile_buffers[row][col].data = copy_data;
3197 tile_buffers[row][col].size = copy_size;
3198 }
3199
3200 *data += size;
3201
3202 tile_buffers[row][col].raw_data_end = *data;
3203}
3204
3205static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003206 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003207 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003208 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003209 const int tile_cols = cm->tile_cols;
3210 const int tile_rows = cm->tile_rows;
3211 const int have_tiles = tile_cols * tile_rows > 1;
3212
3213 if (!have_tiles) {
3214 const uint32_t tile_size = data_end - data;
3215 tile_buffers[0][0].data = data;
3216 tile_buffers[0][0].size = tile_size;
3217 tile_buffers[0][0].raw_data_end = NULL;
3218 } else {
3219 // We locate only the tile buffers that are required, which are the ones
3220 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
3221 // need the last (bottom right) tile buffer, as we need to know where the
3222 // end of the compressed frame buffer is for proper superframe decoding.
3223
3224 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
3225 const uint8_t *const data_start = data;
3226
Yaowu Xuf883b422016-08-30 14:01:10 -07003227 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003228 const int single_row = pbi->dec_tile_row >= 0;
3229 const int tile_rows_start = single_row ? dec_tile_row : 0;
3230 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003231 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003232 const int single_col = pbi->dec_tile_col >= 0;
3233 const int tile_cols_start = single_col ? dec_tile_col : 0;
3234 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3235
3236 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
3237 const int tile_size_bytes = pbi->tile_size_bytes;
3238
3239 size_t tile_col_size;
3240 int r, c;
3241
3242 // Read tile column sizes for all columns (we need the last tile buffer)
3243 for (c = 0; c < tile_cols; ++c) {
3244 const int is_last = c == tile_cols - 1;
3245 if (!is_last) {
3246 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
3247 data += tile_col_size_bytes;
3248 tile_col_data_end[c] = data + tile_col_size;
3249 } else {
3250 tile_col_size = data_end - data;
3251 tile_col_data_end[c] = data_end;
3252 }
3253 data += tile_col_size;
3254 }
3255
3256 data = data_start;
3257
3258 // Read the required tile sizes.
3259 for (c = tile_cols_start; c < tile_cols_end; ++c) {
3260 const int is_last = c == tile_cols - 1;
3261
3262 if (c > 0) data = tile_col_data_end[c - 1];
3263
3264 if (!is_last) data += tile_col_size_bytes;
3265
3266 // Get the whole of the last column, otherwise stop at the required tile.
3267 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
3268 tile_buffers[r][c].col = c;
3269
3270 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3271 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3272 tile_size_bytes, c, r);
3273 }
3274 }
3275
3276 // If we have not read the last column, then read it to get the last tile.
3277 if (tile_cols_end != tile_cols) {
3278 c = tile_cols - 1;
3279
3280 data = tile_col_data_end[c - 1];
3281
3282 for (r = 0; r < tile_rows; ++r) {
3283 tile_buffers[r][c].col = c;
3284
3285 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3286 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3287 tile_size_bytes, c, r);
3288 }
3289 }
3290 }
3291}
3292#else
3293// Reads the next tile returning its size and adjusting '*data' accordingly
3294// based on 'is_last'.
3295static void get_tile_buffer(const uint8_t *const data_end,
3296 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07003297 struct aom_internal_error_info *error_info,
3298 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003299 void *decrypt_state, TileBufferDec *const buf) {
3300 size_t size;
3301
3302 if (!is_last) {
Yaowu Xu0a79a1b2017-02-17 13:04:54 -08003303 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003304 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003305 "Truncated packet or corrupt tile length");
3306
3307 if (decrypt_cb) {
3308 uint8_t be_data[4];
3309 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3310 size = mem_get_varsize(be_data, tile_size_bytes);
3311 } else {
3312 size = mem_get_varsize(*data, tile_size_bytes);
3313 }
3314 *data += tile_size_bytes;
3315
3316 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003317 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003318 "Truncated packet or corrupt tile size");
3319 } else {
3320 size = data_end - *data;
3321 }
3322
3323 buf->data = *data;
3324 buf->size = size;
3325
3326 *data += size;
3327}
3328
3329static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003330 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003331 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003332 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07003333#if CONFIG_TILE_GROUPS
3334 int r, c;
3335 const int tile_cols = cm->tile_cols;
3336 const int tile_rows = cm->tile_rows;
3337 int tc = 0;
3338 int first_tile_in_tg = 0;
3339 int hdr_offset;
3340 struct aom_read_bit_buffer rb_tg_hdr;
3341 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3342 const int num_tiles = tile_rows * tile_cols;
3343 const int num_bits = OD_ILOG(num_tiles) - 1;
3344 const int hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
3345 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
Fangwen Fu73126c02017-02-08 22:37:47 -08003346#if CONFIG_DEPENDENT_HORZTILES
3347 int tile_group_start_col = 0;
3348 int tile_group_start_row = 0;
3349#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003350
3351 for (r = 0; r < tile_rows; ++r) {
3352 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07003353 TileBufferDec *const buf = &tile_buffers[r][c];
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003354 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
Thomas Davies80188d12016-10-26 16:08:35 -07003355 hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
3356
3357 buf->col = c;
3358 if (hdr_offset) {
3359 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
3360 rb_tg_hdr.bit_offset = tg_size_bit_offset;
3361 if (num_tiles) {
3362 pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
3363 pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
Fangwen Fu73126c02017-02-08 22:37:47 -08003364#if CONFIG_DEPENDENT_HORZTILES
3365 tile_group_start_row = r;
3366 tile_group_start_col = c;
3367#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003368 }
3369 }
3370 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
3371 data += hdr_offset;
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003372 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last,
3373 &pbi->common.error, &data, pbi->decrypt_cb,
3374 pbi->decrypt_state, buf);
Fangwen Fu73126c02017-02-08 22:37:47 -08003375#if CONFIG_DEPENDENT_HORZTILES
3376 cm->tile_group_start_row[r][c] = tile_group_start_row;
3377 cm->tile_group_start_col[r][c] = tile_group_start_col;
3378#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003379 }
3380 }
3381#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003382 int r, c;
3383 const int tile_cols = cm->tile_cols;
3384 const int tile_rows = cm->tile_rows;
3385
3386 for (r = 0; r < tile_rows; ++r) {
3387 for (c = 0; c < tile_cols; ++c) {
3388 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
3389 TileBufferDec *const buf = &tile_buffers[r][c];
3390 buf->col = c;
3391 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
3392 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
3393 }
3394 }
Thomas Davies80188d12016-10-26 16:08:35 -07003395#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003396}
3397#endif // CONFIG_EXT_TILE
3398
Yushin Cho77bba8d2016-11-04 16:36:56 -07003399#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003400static void daala_dec_init(AV1_COMMON *const cm, daala_dec_ctx *daala_dec,
Nathan E. Eggeab083972016-12-28 15:31:46 -05003401 aom_reader *r) {
3402 daala_dec->r = r;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003403 od_adapt_ctx_reset(&daala_dec->state.adapt, 0);
3404
Yushin Cho70669122016-12-08 09:53:14 -10003405 // TODO(yushin) : activity masking info needs be signaled by a bitstream
3406 daala_dec->use_activity_masking = AV1_PVQ_ENABLE_ACTIVITY_MASKING;
3407
Yushin Cho7a428ba2017-01-12 16:28:49 -08003408#if !CONFIG_DAALA_DIST
3409 daala_dec->use_activity_masking = 0;
3410#endif
3411
Yushin Cho70669122016-12-08 09:53:14 -10003412 if (daala_dec->use_activity_masking)
3413 daala_dec->qm = OD_HVS_QM;
3414 else
3415 daala_dec->qm = OD_FLAT_QM;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003416
3417 od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
3418 daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
Yushin Cho70669122016-12-08 09:53:14 -10003419
3420 if (daala_dec->use_activity_masking) {
3421 int pli;
3422 int use_masking = daala_dec->use_activity_masking;
3423 int segment_id = 0;
3424 int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
3425
3426 for (pli = 0; pli < MAX_MB_PLANE; pli++) {
3427 int i;
3428 int q;
3429
3430 q = qindex;
3431 if (q <= OD_DEFAULT_QMS[use_masking][0][pli].interp_q << OD_COEFF_SHIFT) {
3432 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3433 &OD_DEFAULT_QMS[use_masking][0][pli], NULL);
3434 } else {
3435 i = 0;
3436 while (OD_DEFAULT_QMS[use_masking][i + 1][pli].qm_q4 != NULL &&
3437 q > OD_DEFAULT_QMS[use_masking][i + 1][pli].interp_q
3438 << OD_COEFF_SHIFT) {
3439 i++;
3440 }
3441 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3442 &OD_DEFAULT_QMS[use_masking][i][pli],
3443 &OD_DEFAULT_QMS[use_masking][i + 1][pli]);
3444 }
3445 }
3446 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07003447}
Yushin Cho70669122016-12-08 09:53:14 -10003448#endif // #if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003449
Yaowu Xuf883b422016-08-30 14:01:10 -07003450static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003451 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003452 AV1_COMMON *const cm = &pbi->common;
3453 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003454 const int tile_cols = cm->tile_cols;
3455 const int tile_rows = cm->tile_rows;
3456 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07003457 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003458#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003459 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003460 const int single_row = pbi->dec_tile_row >= 0;
3461 const int tile_rows_start = single_row ? dec_tile_row : 0;
3462 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003463 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003464 const int single_col = pbi->dec_tile_col >= 0;
3465 const int tile_cols_start = single_col ? dec_tile_col : 0;
3466 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3467 const int inv_col_order = pbi->inv_tile_order && !single_col;
3468 const int inv_row_order = pbi->inv_tile_order && !single_row;
3469#else
3470 const int tile_rows_start = 0;
3471 const int tile_rows_end = tile_rows;
3472 const int tile_cols_start = 0;
3473 const int tile_cols_end = tile_cols;
3474 const int inv_col_order = pbi->inv_tile_order;
3475 const int inv_row_order = pbi->inv_tile_order;
3476#endif // CONFIG_EXT_TILE
3477 int tile_row, tile_col;
3478
hui su0d103572017-03-01 17:58:01 -08003479#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003480 cm->do_subframe_update = n_tiles == 1;
hui su0d103572017-03-01 17:58:01 -08003481#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003482
3483 if (cm->lf.filter_level && !cm->skip_loop_filter &&
3484 pbi->lf_worker.data1 == NULL) {
3485 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07003486 aom_memalign(32, sizeof(LFWorkerData)));
3487 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003488 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003489 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003490 "Loop filter thread creation failed");
3491 }
3492 }
3493
3494 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3495 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3496 // Be sure to sync as we might be resuming after a failed frame decode.
3497 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003498 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
3499 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003500 }
3501
3502 assert(tile_rows <= MAX_TILE_ROWS);
3503 assert(tile_cols <= MAX_TILE_COLS);
3504
3505 get_tile_buffers(pbi, data, data_end, tile_buffers);
3506
3507 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003508 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003509 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003510 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003511 pbi->allocated_tiles = n_tiles;
3512 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003513#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003514 if (pbi->acct_enabled) {
3515 aom_accounting_reset(&pbi->accounting);
3516 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003517#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003518 // Load all tile information into tile_data.
3519 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3520 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3521 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
3522 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
3523
3524 td->cm = cm;
3525 td->xd = pbi->mb;
3526 td->xd.corrupted = 0;
3527 td->xd.counts =
3528 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3529 ? &cm->counts
3530 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003531 av1_zero(td->dqcoeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003532#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003533 av1_zero(td->pvq_ref_coeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003534#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003535 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003536 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003537 &td->bit_reader,
3538#if CONFIG_ANS && ANS_MAX_SYMBOLS
3539 1 << cm->ans_window_size_log2,
3540#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3541 pbi->decrypt_cb, pbi->decrypt_state);
Michael Bebenita6048d052016-08-25 14:40:54 -07003542#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003543 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003544 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003545 } else {
David Barkerd971f402016-10-25 13:52:07 +01003546 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003547 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003548#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003549 av1_init_macroblockd(cm, &td->xd,
3550#if CONFIG_PVQ
3551 td->pvq_ref_coeff,
3552#endif
3553 td->dqcoeff);
3554#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003555 daala_dec_init(cm, &td->xd.daala_dec, &td->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003556#endif
Thomas Daviesf77d4ad2017-01-10 18:55:42 +00003557#if CONFIG_EC_ADAPT
3558 // Initialise the tile context from the frame context
3559 td->tctx = *cm->fc;
3560 td->xd.tile_ctx = &td->tctx;
3561#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003562#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003563 td->xd.plane[0].color_index_map = td->color_index_map[0];
3564 td->xd.plane[1].color_index_map = td->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003565#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003566 }
3567 }
3568
3569 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3570 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
3571 int mi_row = 0;
3572 TileInfo tile_info;
3573
Yaowu Xuf883b422016-08-30 14:01:10 -07003574 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003575
3576 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3577 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
3578 TileData *const td = pbi->tile_data + tile_cols * row + col;
Michael Bebenita6048d052016-08-25 14:40:54 -07003579#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003580 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003581 td->bit_reader.accounting->last_tell_frac =
3582 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003583 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003584#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003585
Yaowu Xuf883b422016-08-30 14:01:10 -07003586 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003587
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003588#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003589#if CONFIG_TILE_GROUPS
3590 av1_tile_set_tg_boundary(&tile_info, cm, tile_row, tile_col);
3591 if (!cm->dependent_horz_tiles || tile_row == 0 ||
3592 tile_info.tg_horz_boundary) {
3593#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003594 if (!cm->dependent_horz_tiles || tile_row == 0) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003595#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003596 av1_zero_above_context(cm, tile_info.mi_col_start,
3597 tile_info.mi_col_end);
3598 }
3599#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003600 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003601#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003602
3603 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
3604 mi_row += cm->mib_size) {
3605 int mi_col;
3606
Yaowu Xuf883b422016-08-30 14:01:10 -07003607 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003608
3609 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
3610 mi_col += cm->mib_size) {
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003611 av1_update_boundary_info(cm, &tile_info, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003612 decode_partition(pbi, &td->xd,
3613#if CONFIG_SUPERTX
3614 0,
3615#endif // CONFIG_SUPERTX
3616 mi_row, mi_col, &td->bit_reader, cm->sb_size,
3617 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003618#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3619 detoken_and_recon_sb(pbi, &td->xd, mi_row, mi_col, &td->bit_reader,
3620 cm->sb_size);
3621#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003622 }
3623 pbi->mb.corrupted |= td->xd.corrupted;
3624 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003625 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003626 "Failed to decode tile data");
hui su0d103572017-03-01 17:58:01 -08003627#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003628 if (cm->do_subframe_update &&
3629 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
hui su0161a932017-01-24 14:12:11 -08003630 const int mi_rows_per_update =
3631 MI_SIZE * AOMMAX(cm->mi_rows / MI_SIZE / COEF_PROBS_BUFS, 1);
3632 if ((mi_row + MI_SIZE) % mi_rows_per_update == 0 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003633 mi_row + MI_SIZE < cm->mi_rows &&
3634 cm->coef_probs_update_idx < COEF_PROBS_BUFS - 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003635 av1_partial_adapt_probs(cm, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003636 ++cm->coef_probs_update_idx;
3637 }
3638 }
hui su0d103572017-03-01 17:58:01 -08003639#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003640 }
3641 }
3642
3643 assert(mi_row > 0);
3644
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003645// when Parallel deblocking is enabled, deblocking should not
3646// be interleaved with decoding. Instead, deblocking should be done
3647// after the entire frame is decoded.
3648#if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003649 // Loopfilter one tile row.
3650 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3651 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003652 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003653 const int lf_end = tile_info.mi_row_end - cm->mib_size;
3654
3655 // Delay the loopfilter if the first tile row is only
3656 // a single superblock high.
3657 if (lf_end <= 0) continue;
3658
3659 // Decoding has completed. Finish up the loop filter in this thread.
3660 if (tile_info.mi_row_end >= cm->mi_rows) continue;
3661
3662 winterface->sync(&pbi->lf_worker);
3663 lf_data->start = lf_start;
3664 lf_data->stop = lf_end;
3665 if (pbi->max_threads > 1) {
3666 winterface->launch(&pbi->lf_worker);
3667 } else {
3668 winterface->execute(&pbi->lf_worker);
3669 }
3670 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003671#endif // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003672
3673 // After loopfiltering, the last 7 row pixels in each superblock row may
3674 // still be changed by the longest loopfilter of the next superblock row.
3675 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003676 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003677 }
3678
3679#if CONFIG_VAR_TX
3680 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003681 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
3682 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003683#else
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003684#if CONFIG_PARALLEL_DEBLOCKING
3685 // Loopfilter all rows in the frame in the frame.
3686 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3687 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3688 winterface->sync(&pbi->lf_worker);
3689 lf_data->start = 0;
3690 lf_data->stop = cm->mi_rows;
3691 winterface->execute(&pbi->lf_worker);
3692 }
3693#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003694 // Loopfilter remaining rows in the frame.
3695 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3696 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3697 winterface->sync(&pbi->lf_worker);
3698 lf_data->start = lf_data->stop;
3699 lf_data->stop = cm->mi_rows;
3700 winterface->execute(&pbi->lf_worker);
3701 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003702#endif // CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003703#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003704 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003705 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003706
3707#if CONFIG_EXT_TILE
3708 if (n_tiles == 1) {
3709#if CONFIG_ANS
3710 return data_end;
3711#else
3712 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07003713 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003714#endif // CONFIG_ANS
3715 } else {
3716 // Return the end of the last tile buffer
3717 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3718 }
3719#else
3720#if CONFIG_ANS
3721 return data_end;
3722#else
3723 {
3724 // Get last tile data.
3725 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003726 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003727 }
3728#endif // CONFIG_ANS
3729#endif // CONFIG_EXT_TILE
3730}
3731
3732static int tile_worker_hook(TileWorkerData *const tile_data,
3733 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003734 AV1Decoder *const pbi = tile_data->pbi;
3735 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003736 int mi_row, mi_col;
3737
3738 if (setjmp(tile_data->error_info.jmp)) {
3739 tile_data->error_info.setjmp = 0;
3740 tile_data->xd.corrupted = 1;
3741 return 0;
3742 }
3743
3744 tile_data->error_info.setjmp = 1;
3745 tile_data->xd.error_info = &tile_data->error_info;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003746#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003747#if CONFIG_TILE_GROUPS
3748 if (!cm->dependent_horz_tiles || tile->tg_horz_boundary) {
3749#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003750 if (!cm->dependent_horz_tiles) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003751#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003752 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
3753 }
3754#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003755 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003756#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003757
3758 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
3759 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003760 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003761
3762 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3763 mi_col += cm->mib_size) {
3764 decode_partition(pbi, &tile_data->xd,
3765#if CONFIG_SUPERTX
3766 0,
3767#endif
3768 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
3769 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003770#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3771 detoken_and_recon_sb(pbi, &tile_data->xd, mi_row, mi_col,
3772 &tile_data->bit_reader, cm->sb_size);
3773#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003774 }
3775 }
3776 return !tile_data->xd.corrupted;
3777}
3778
3779// sorts in descending order
3780static int compare_tile_buffers(const void *a, const void *b) {
3781 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
3782 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
3783 return (int)(buf2->size - buf1->size);
3784}
3785
Yaowu Xuf883b422016-08-30 14:01:10 -07003786static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003787 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003788 AV1_COMMON *const cm = &pbi->common;
3789 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003790 const int tile_cols = cm->tile_cols;
3791 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003792 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
clang-format67948d32016-09-07 22:40:40 -07003793 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003794#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003795 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003796 const int single_row = pbi->dec_tile_row >= 0;
3797 const int tile_rows_start = single_row ? dec_tile_row : 0;
3798 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003799 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003800 const int single_col = pbi->dec_tile_col >= 0;
3801 const int tile_cols_start = single_col ? dec_tile_col : 0;
3802 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3803#else
3804 const int tile_rows_start = 0;
3805 const int tile_rows_end = tile_rows;
3806 const int tile_cols_start = 0;
3807 const int tile_cols_end = tile_cols;
3808#endif // CONFIG_EXT_TILE
3809 int tile_row, tile_col;
3810 int i;
3811
3812#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3813 int final_worker = -1;
3814#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3815
3816 assert(tile_rows <= MAX_TILE_ROWS);
3817 assert(tile_cols <= MAX_TILE_COLS);
3818
3819 assert(tile_cols * tile_rows > 1);
3820
Yaowu Xuc27fc142016-08-22 16:08:15 -07003821 // TODO(jzern): See if we can remove the restriction of passing in max
3822 // threads to the decoder.
3823 if (pbi->num_tile_workers == 0) {
3824 const int num_threads = pbi->max_threads & ~1;
3825 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07003826 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003827 // Ensure tile data offsets will be properly aligned. This may fail on
3828 // platforms without DECLARE_ALIGNED().
3829 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3830 CHECK_MEM_ERROR(
3831 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003832 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003833 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07003834 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003835 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003836 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003837 ++pbi->num_tile_workers;
3838
3839 winterface->init(worker);
3840 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003841 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003842 "Tile decoder thread creation failed");
3843 }
3844 }
3845 }
3846
3847 // Reset tile decoding hook
3848 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003849 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003850 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003851 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003852 worker->data1 = &pbi->tile_worker_data[i];
3853 worker->data2 = &pbi->tile_worker_info[i];
3854 }
3855
3856 // Initialize thread frame counts.
3857 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3858 for (i = 0; i < num_workers; ++i) {
3859 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003860 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003861 }
3862 }
3863
3864 // Load tile data into tile_buffers
3865 get_tile_buffers(pbi, data, data_end, tile_buffers);
3866
3867 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3868 // Sort the buffers in this tile row based on size in descending order.
3869 qsort(&tile_buffers[tile_row][tile_cols_start],
3870 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
3871 compare_tile_buffers);
3872
3873 // Rearrange the tile buffers in this tile row such that per-tile group
3874 // the largest, and presumably the most difficult tile will be decoded in
3875 // the main thread. This should help minimize the number of instances
3876 // where the main thread is waiting for a worker to complete.
3877 {
3878 int group_start;
3879 for (group_start = tile_cols_start; group_start < tile_cols_end;
3880 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003881 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003882 const TileBufferDec largest = tile_buffers[tile_row][group_start];
3883 memmove(&tile_buffers[tile_row][group_start],
3884 &tile_buffers[tile_row][group_start + 1],
3885 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
3886 tile_buffers[tile_row][group_end - 1] = largest;
3887 }
3888 }
3889
3890 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
3891 // Launch workers for individual columns
3892 for (i = 0; i < num_workers && tile_col < tile_cols_end;
3893 ++i, ++tile_col) {
3894 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07003895 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003896 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
3897 TileInfo *const tile_info = (TileInfo *)worker->data2;
3898
3899 twd->pbi = pbi;
3900 twd->xd = pbi->mb;
3901 twd->xd.corrupted = 0;
3902 twd->xd.counts =
3903 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3904 ? &twd->counts
3905 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003906 av1_zero(twd->dqcoeff);
3907 av1_tile_init(tile_info, cm, tile_row, buf->col);
3908 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003909 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003910 &twd->bit_reader,
3911#if CONFIG_ANS && ANS_MAX_SYMBOLS
3912 1 << cm->ans_window_size_log2,
3913#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3914 pbi->decrypt_cb, pbi->decrypt_state);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003915 av1_init_macroblockd(cm, &twd->xd,
3916#if CONFIG_PVQ
3917 twd->pvq_ref_coeff,
3918#endif
3919 twd->dqcoeff);
3920#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003921 daala_dec_init(cm, &twd->xd.daala_dec, &twd->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003922#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003923#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003924 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
3925 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003926#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003927
3928 worker->had_error = 0;
3929 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
3930 winterface->execute(worker);
3931 } else {
3932 winterface->launch(worker);
3933 }
3934
3935#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3936 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
3937 final_worker = i;
3938 }
3939#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3940 }
3941
3942 // Sync all workers
3943 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003944 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003945 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07003946 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07003947 // info in cm. Additionally once the threads have been synced and an
3948 // error is detected, there's no point in continuing to decode tiles.
3949 pbi->mb.corrupted |= !winterface->sync(worker);
3950 }
3951 }
3952 }
3953
3954 // Accumulate thread frame counts.
3955 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3956 for (i = 0; i < num_workers; ++i) {
3957 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003958 av1_accumulate_frame_counts(&cm->counts, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003959 }
3960 }
3961
3962#if CONFIG_EXT_TILE
3963 // Return the end of the last tile buffer
3964 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3965#else
3966#if CONFIG_ANS
3967 return data_end;
3968#else
3969 assert(final_worker != -1);
3970 {
3971 TileWorkerData *const twd =
3972 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003973 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003974 }
3975#endif // CONFIG_ANS
3976#endif // CONFIG_EXT_TILE
3977}
3978
3979static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003980 AV1_COMMON *const cm = (AV1_COMMON *)data;
3981 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003982}
3983
Yaowu Xuf883b422016-08-30 14:01:10 -07003984static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3985 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003986 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003987 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003988 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003989 cm->bit_depth = AOM_BITS_8;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003990 }
3991
Yaowu Xuf883b422016-08-30 14:01:10 -07003992#if CONFIG_AOM_HIGHBITDEPTH
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003993 if (cm->bit_depth > AOM_BITS_8) {
3994 cm->use_highbitdepth = 1;
3995 } else {
Yaowu Xu345a22d2017-02-27 09:23:52 -08003996#if CONFIG_LOWBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003997 cm->use_highbitdepth = 0;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003998#else
3999 cm->use_highbitdepth = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004000#endif
4001 }
Sebastien Alaiwan98378132017-01-04 11:23:09 +01004002#endif
4003
Yaowu Xuf883b422016-08-30 14:01:10 -07004004 cm->color_space = aom_rb_read_literal(rb, 3);
4005 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004006 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07004007 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004008 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004009 cm->subsampling_x = aom_rb_read_bit(rb);
4010 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004011 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07004012 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004013 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07004014 if (aom_rb_read_bit(rb))
4015 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004016 "Reserved bit set");
4017 } else {
4018 cm->subsampling_y = cm->subsampling_x = 1;
4019 }
4020 } else {
4021 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
4022 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
4023 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
4024 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004025 if (aom_rb_read_bit(rb))
4026 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004027 "Reserved bit set");
4028 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004029 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004030 "4:4:4 color not supported in profile 0 or 2");
4031 }
4032 }
4033}
4034
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004035#if CONFIG_REFERENCE_BUFFER
4036void read_sequence_header(SequenceHeader *seq_params) {
4037 /* Placeholder for actually reading from the bitstream */
4038 seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
4039 seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
4040 seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
4041}
4042#endif
4043
Yaowu Xuf883b422016-08-30 14:01:10 -07004044static size_t read_uncompressed_header(AV1Decoder *pbi,
4045 struct aom_read_bit_buffer *rb) {
4046 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004047 MACROBLOCKD *const xd = &pbi->mb;
4048 BufferPool *const pool = cm->buffer_pool;
4049 RefCntBuffer *const frame_bufs = pool->frame_bufs;
4050 int i, mask, ref_index = 0;
4051 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004052
4053#if CONFIG_REFERENCE_BUFFER
4054 /* TODO: Move outside frame loop or inside key-frame branch */
4055 read_sequence_header(&pbi->seq_params);
4056#endif
4057
Yaowu Xuc27fc142016-08-22 16:08:15 -07004058 cm->last_frame_type = cm->frame_type;
4059 cm->last_intra_only = cm->intra_only;
4060
4061#if CONFIG_EXT_REFS
4062 // NOTE: By default all coded frames to be used as a reference
4063 cm->is_reference_frame = 1;
4064#endif // CONFIG_EXT_REFS
4065
Yaowu Xuf883b422016-08-30 14:01:10 -07004066 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
4067 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004068 "Invalid frame marker");
4069
Yaowu Xuf883b422016-08-30 14:01:10 -07004070 cm->profile = av1_read_profile(rb);
4071#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004072 if (cm->profile >= MAX_PROFILES)
Yaowu Xuf883b422016-08-30 14:01:10 -07004073 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004074 "Unsupported bitstream profile");
4075#else
4076 if (cm->profile >= PROFILE_2)
Yaowu Xuf883b422016-08-30 14:01:10 -07004077 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004078 "Unsupported bitstream profile");
4079#endif
4080
Yaowu Xuf883b422016-08-30 14:01:10 -07004081 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004082
4083 if (cm->show_existing_frame) {
Yaowu Xu415ba932016-12-27 11:17:32 -08004084 // Show an existing frame directly.
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004085 const int existing_frame_idx = aom_rb_read_literal(rb, 3);
4086 const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
Yaowu Xu415ba932016-12-27 11:17:32 -08004087#if CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004088 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004089 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4090 int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004091 /* Compare display_frame_id with ref_frame_id and check valid for
4092 * referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004093 if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
4094 cm->valid_for_referencing[existing_frame_idx] == 0)
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004095 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4096 "Reference buffer frame ID mismatch");
4097 }
4098#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004099 lock_buffer_pool(pool);
4100 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
4101 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07004102 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004103 "Buffer %d does not contain a decoded frame",
4104 frame_to_show);
4105 }
4106 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
4107 unlock_buffer_pool(pool);
4108
4109 cm->lf.filter_level = 0;
4110 cm->show_frame = 1;
4111 pbi->refresh_frame_flags = 0;
4112
4113 if (cm->frame_parallel_decode) {
4114 for (i = 0; i < REF_FRAMES; ++i)
4115 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
4116 }
4117
4118 return 0;
4119 }
4120
Yaowu Xuf883b422016-08-30 14:01:10 -07004121 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
4122 cm->show_frame = aom_rb_read_bit(rb);
4123 cm->error_resilient_mode = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004124#if CONFIG_REFERENCE_BUFFER
4125 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004126 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4127 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4128 int prev_frame_id = 0;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004129 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004130 prev_frame_id = cm->current_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004131 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004132 cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004133
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004134 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004135 int diff_frame_id;
4136 if (cm->current_frame_id > prev_frame_id) {
4137 diff_frame_id = cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004138 } else {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004139 diff_frame_id =
4140 (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004141 }
4142 /* Check current_frame_id for conformance */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004143 if (prev_frame_id == cm->current_frame_id ||
4144 diff_frame_id >= (1 << (frame_id_length - 1))) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004145 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4146 "Invalid value of current_frame_id");
4147 }
4148 }
4149 /* Check if some frames need to be marked as not valid for referencing */
4150 for (i = 0; i < REF_FRAMES; i++) {
4151 if (cm->frame_type == KEY_FRAME) {
4152 cm->valid_for_referencing[i] = 0;
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004153 } else if (cm->current_frame_id - (1 << diff_len) > 0) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004154 if (cm->ref_frame_id[i] > cm->current_frame_id ||
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004155 cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004156 cm->valid_for_referencing[i] = 0;
4157 } else {
4158 if (cm->ref_frame_id[i] > cm->current_frame_id &&
4159 cm->ref_frame_id[i] <
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004160 (1 << frame_id_length) + cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004161 cm->valid_for_referencing[i] = 0;
4162 }
4163 }
4164 }
4165#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004166 if (cm->frame_type == KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004167 if (!av1_read_sync_code(rb))
4168 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004169 "Invalid frame sync code");
4170
4171 read_bitdepth_colorspace_sampling(cm, rb);
4172 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
4173
4174 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4175 cm->frame_refs[i].idx = INVALID_IDX;
4176 cm->frame_refs[i].buf = NULL;
4177 }
4178
4179 setup_frame_size(cm, rb);
4180 if (pbi->need_resync) {
4181 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4182 pbi->need_resync = 0;
4183 }
Alex Converseeb780e72016-12-13 12:46:41 -08004184#if CONFIG_ANS && ANS_MAX_SYMBOLS
4185 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4186#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
Urvang Joshib100db72016-10-12 16:28:56 -07004187#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004188 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004189#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004190 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004191 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004192#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004193 if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004194#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004195 if (cm->error_resilient_mode) {
4196 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
4197 } else {
4198 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004199 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004200 ? RESET_FRAME_CONTEXT_ALL
4201 : RESET_FRAME_CONTEXT_CURRENT;
4202 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004203 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004204 ? RESET_FRAME_CONTEXT_CURRENT
4205 : RESET_FRAME_CONTEXT_NONE;
4206 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07004207 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004208 ? RESET_FRAME_CONTEXT_ALL
4209 : RESET_FRAME_CONTEXT_CURRENT;
4210 }
4211 }
4212
4213 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004214 if (!av1_read_sync_code(rb))
4215 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004216 "Invalid frame sync code");
4217
4218 read_bitdepth_colorspace_sampling(cm, rb);
4219
Yaowu Xuf883b422016-08-30 14:01:10 -07004220 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004221 setup_frame_size(cm, rb);
4222 if (pbi->need_resync) {
4223 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4224 pbi->need_resync = 0;
4225 }
Alex Converseeb780e72016-12-13 12:46:41 -08004226#if CONFIG_ANS && ANS_MAX_SYMBOLS
4227 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4228#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004229 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Yaowu Xuf883b422016-08-30 14:01:10 -07004230 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004231
4232#if CONFIG_EXT_REFS
4233 if (!pbi->refresh_frame_flags) {
4234 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
4235 // will not be used as a reference
4236 cm->is_reference_frame = 0;
4237 }
4238#endif // CONFIG_EXT_REFS
4239
4240 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004241 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004242 const int idx = cm->ref_frame_map[ref];
4243 RefBuffer *const ref_frame = &cm->frame_refs[i];
4244 ref_frame->idx = idx;
4245 ref_frame->buf = &frame_bufs[idx].buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07004246 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004247#if CONFIG_REFERENCE_BUFFER
4248 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004249 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4250 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4251 int delta_frame_id_minus1 = aom_rb_read_literal(rb, diff_len);
4252 int ref_frame_id =
4253 ((cm->current_frame_id - (delta_frame_id_minus1 + 1) +
4254 (1 << frame_id_length)) %
4255 (1 << frame_id_length));
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004256 /* Compare values derived from delta_frame_id_minus1 and
4257 * refresh_frame_flags. Also, check valid for referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004258 if (ref_frame_id != cm->ref_frame_id[ref] ||
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004259 cm->valid_for_referencing[ref] == 0)
4260 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4261 "Reference buffer frame ID mismatch");
4262 }
4263#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004264 }
4265
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004266#if CONFIG_FRAME_SIZE
4267 if (cm->error_resilient_mode == 0) {
4268 setup_frame_size_with_refs(cm, rb);
4269 } else {
4270 setup_frame_size(cm, rb);
4271 }
4272#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004273 setup_frame_size_with_refs(cm, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004274#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004275
Yaowu Xuf883b422016-08-30 14:01:10 -07004276 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Angie Chiang5678ad92016-11-21 09:38:40 -08004277 cm->interp_filter = read_frame_interp_filter(rb);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004278#if CONFIG_TEMPMV_SIGNALING
4279 if (!cm->error_resilient_mode) {
4280 cm->use_prev_frame_mvs = aom_rb_read_bit(rb);
4281 }
4282#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004283 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4284 RefBuffer *const ref_buf = &cm->frame_refs[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07004285#if CONFIG_AOM_HIGHBITDEPTH
4286 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004287 &ref_buf->sf, ref_buf->buf->y_crop_width,
4288 ref_buf->buf->y_crop_height, cm->width, cm->height,
4289 cm->use_highbitdepth);
4290#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004291 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004292 &ref_buf->sf, ref_buf->buf->y_crop_width,
4293 ref_buf->buf->y_crop_height, cm->width, cm->height);
4294#endif
4295 }
4296 }
4297 }
Fangwen Fu8d164de2016-12-14 13:40:54 -08004298#if CONFIG_TEMPMV_SIGNALING
4299 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
4300#endif
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004301
4302#if CONFIG_REFERENCE_BUFFER
4303 if (pbi->seq_params.frame_id_numbers_present_flag) {
4304 /* If bitmask is set, update reference frame id values and
4305 mark frames as valid for reference */
4306 int refresh_frame_flags =
4307 cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
4308 for (i = 0; i < REF_FRAMES; i++) {
4309 if ((refresh_frame_flags >> i) & 1) {
4310 cm->ref_frame_id[i] = cm->current_frame_id;
4311 cm->valid_for_referencing[i] = 1;
4312 }
4313 }
4314 }
4315#endif
4316
Yaowu Xuf883b422016-08-30 14:01:10 -07004317#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004318 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
4319#endif
4320 get_frame_new_buffer(cm)->color_space = cm->color_space;
4321 get_frame_new_buffer(cm)->color_range = cm->color_range;
4322 get_frame_new_buffer(cm)->render_width = cm->render_width;
4323 get_frame_new_buffer(cm)->render_height = cm->render_height;
4324
4325 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004326 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004327 "Keyframe / intra-only frame required to reset decoder"
4328 " state");
4329 }
4330
4331 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004332 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004333 ? REFRESH_FRAME_CONTEXT_FORWARD
4334 : REFRESH_FRAME_CONTEXT_BACKWARD;
4335 } else {
4336 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
4337 }
4338
Yaowu Xuf883b422016-08-30 14:01:10 -07004339 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07004340 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07004341 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004342
4343 // Generate next_ref_frame_map.
4344 lock_buffer_pool(pool);
4345 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
4346 if (mask & 1) {
4347 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
4348 ++frame_bufs[cm->new_fb_idx].ref_count;
4349 } else {
4350 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4351 }
4352 // Current thread holds the reference frame.
4353 if (cm->ref_frame_map[ref_index] >= 0)
4354 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4355 ++ref_index;
4356 }
4357
4358 for (; ref_index < REF_FRAMES; ++ref_index) {
4359 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4360
4361 // Current thread holds the reference frame.
4362 if (cm->ref_frame_map[ref_index] >= 0)
4363 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4364 }
4365 unlock_buffer_pool(pool);
4366 pbi->hold_ref_buf = 1;
4367
4368 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07004369 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004370
4371#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07004372 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004373#else
4374 set_sb_size(cm, BLOCK_64X64);
4375#endif // CONFIG_EXT_PARTITION
4376
4377 setup_loopfilter(cm, rb);
Jean-Marc Valin01435132017-02-18 14:12:53 -05004378#if CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07004379 setup_dering(cm, rb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004380 setup_clpf(pbi, rb);
4381#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004382#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee874d36d2016-12-14 16:53:17 -08004383 av1_alloc_restoration_buffers(cm);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004384 decode_restoration_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004385#endif // CONFIG_LOOP_RESTORATION
4386 setup_quantization(cm, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004387#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004388 xd->bd = (int)cm->bit_depth;
4389#endif
4390
hui su0d103572017-03-01 17:58:01 -08004391#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuf883b422016-08-30 14:01:10 -07004392 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004393 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4394 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4395 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4396 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
4397 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4398 }
hui su0d103572017-03-01 17:58:01 -08004399#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004400
4401 setup_segmentation(cm, rb);
4402
Arild Fuldseth07441162016-08-15 15:07:52 +02004403#if CONFIG_DELTA_Q
4404 {
4405 struct segmentation *const seg = &cm->seg;
4406 int segment_quantizer_active = 0;
4407 for (i = 0; i < MAX_SEGMENTS; i++) {
4408 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
4409 segment_quantizer_active = 1;
4410 }
4411 }
4412
Thomas Daviesf6936102016-09-05 16:51:31 +01004413 cm->delta_q_res = 1;
Arild Fuldseth07441162016-08-15 15:07:52 +02004414 if (segment_quantizer_active == 0) {
4415 cm->delta_q_present_flag = aom_rb_read_bit(rb);
4416 } else {
4417 cm->delta_q_present_flag = 0;
4418 }
4419 if (cm->delta_q_present_flag) {
4420 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01004421 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Arild Fuldseth07441162016-08-15 15:07:52 +02004422 }
4423 }
4424#endif
4425
Urvang Joshi454280d2016-10-14 16:51:44 -07004426 for (i = 0; i < MAX_SEGMENTS; ++i) {
4427 const int qindex = cm->seg.enabled
4428 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
4429 : cm->base_qindex;
4430 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
4431 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4432 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004433 }
4434
4435 setup_segmentation_dequant(cm);
Yue Cheneeacc4c2017-01-17 17:29:17 -08004436 cm->tx_mode = read_tx_mode(cm, xd, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004437 cm->reference_mode = read_frame_reference_mode(cm, rb);
4438
Sarah Parkere68a3e42017-02-16 14:03:24 -08004439#if CONFIG_EXT_TX
4440 cm->reduced_tx_set_used = aom_rb_read_bit(rb);
4441#endif // CONFIG_EXT_TX
4442
Yaowu Xuc27fc142016-08-22 16:08:15 -07004443 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004444 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004445
4446 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07004447 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004448 "Invalid header size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004449 return sz;
4450}
4451
4452#if CONFIG_EXT_TX
Thomas9ac55082016-09-23 18:04:17 +01004453#if !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuf883b422016-08-30 14:01:10 -07004454static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004455 int i, j, k;
4456 int s;
4457 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004458 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004459 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4460 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004461 for (j = 0; j < num_ext_tx_set[ext_tx_set_type_inter[s]] - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004462 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004463 }
4464 }
4465 }
4466
4467 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004468 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004469 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4470 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
4471 for (j = 0; j < INTRA_MODES; ++j)
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004472 for (k = 0; k < num_ext_tx_set[ext_tx_set_type_intra[s]] - 1; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004473 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
4474 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004475 }
4476 }
4477 }
4478}
Thomas9ac55082016-09-23 18:04:17 +01004479#endif // !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004480#else
4481
Yaowu Xuc27fc142016-08-22 16:08:15 -07004482#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004483#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07004484static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004485 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -07004486 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004487 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
Jingning Hanfeb517c2016-12-21 16:02:07 -08004488 for (j = TX_8X8; j < TX_SIZES; ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004489 av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004490 }
4491 }
4492 }
4493}
4494#endif // CONFIG_SUPERTX
4495
4496#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +00004497static void read_global_motion_params(WarpedMotionParams *params,
Yaowu Xuf883b422016-08-30 14:01:10 -07004498 aom_prob *probs, aom_reader *r) {
David Barkercf3d0b02016-11-10 10:14:49 +00004499 TransformationType type =
Michael Bebenita6048d052016-08-25 14:40:54 -07004500 aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004501 set_default_gmparams(params);
David Barkercf3d0b02016-11-10 10:14:49 +00004502 params->wmtype = type;
4503 switch (type) {
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004504 case HOMOGRAPHY:
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004505 case HORTRAPEZOID:
4506 case VERTRAPEZOID:
4507 if (type != HORTRAPEZOID)
4508 params->wmmat[6] =
4509 aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
4510 GM_ROW3HOMO_DECODE_FACTOR;
4511 if (type != VERTRAPEZOID)
4512 params->wmmat[7] =
4513 aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
4514 GM_ROW3HOMO_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004515 case AFFINE:
4516 case ROTZOOM:
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004517 params->wmmat[2] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004518 GM_ALPHA_DECODE_FACTOR +
David Barkercf3d0b02016-11-10 10:14:49 +00004519 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004520 if (type != VERTRAPEZOID)
4521 params->wmmat[3] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004522 GM_ALPHA_DECODE_FACTOR;
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004523 if (type >= AFFINE) {
4524 if (type != HORTRAPEZOID)
4525 params->wmmat[4] =
4526 aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4527 GM_ALPHA_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004528 params->wmmat[5] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4529 GM_ALPHA_DECODE_FACTOR +
4530 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004531 } else {
David Barkercf3d0b02016-11-10 10:14:49 +00004532 params->wmmat[4] = -params->wmmat[3];
4533 params->wmmat[5] = params->wmmat[2];
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004534 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004535 // fallthrough intended
David Barkercf3d0b02016-11-10 10:14:49 +00004536 case TRANSLATION:
4537 params->wmmat[0] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4538 GM_TRANS_DECODE_FACTOR;
4539 params->wmmat[1] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4540 GM_TRANS_DECODE_FACTOR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004541 break;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004542 case IDENTITY: break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004543 default: assert(0);
4544 }
4545}
4546
Yaowu Xuf883b422016-08-30 14:01:10 -07004547static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004548 int frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004549 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
4550 read_global_motion_params(&cm->global_motion[frame],
4551 cm->fc->global_motion_types_prob, r);
Sarah Parkere5299862016-08-16 14:57:37 -07004552 /*
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004553 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4554 frame, cm->current_video_frame, cm->show_frame,
David Barkercf3d0b02016-11-10 10:14:49 +00004555 cm->global_motion[frame].wmmat[0],
4556 cm->global_motion[frame].wmmat[1],
4557 cm->global_motion[frame].wmmat[2],
4558 cm->global_motion[frame].wmmat[3]);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004559 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004560 }
4561}
4562#endif // CONFIG_GLOBAL_MOTION
4563
Yaowu Xuf883b422016-08-30 14:01:10 -07004564static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004565 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004566 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004567#if CONFIG_SUPERTX
4568 MACROBLOCKD *const xd = &pbi->mb;
4569#endif
4570 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07004571 aom_reader r;
Yaowu Xu8af861b2016-11-01 12:12:11 -07004572 int k, i;
Yaowu Xud0af64f2016-11-17 12:50:47 -08004573#if !CONFIG_EC_ADAPT || CONFIG_EXT_INTRA
Yaowu Xu8af861b2016-11-01 12:12:11 -07004574 int j;
4575#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004576
Alex Converse2cdf0d82016-12-13 13:53:09 -08004577#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08004578 r.window_size = 1 << cm->ans_window_size_log2;
Alex Converse2cdf0d82016-12-13 13:53:09 -08004579#endif
Alex Converse346440b2017-01-03 13:47:37 -08004580 if (aom_reader_init(&r, data, partition_size, pbi->decrypt_cb,
4581 pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07004582 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004583 "Failed to allocate bool decoder 0");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004584
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004585#if CONFIG_LOOP_RESTORATION
4586 decode_restoration(cm, &r);
4587#endif
4588
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004589#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -07004590 if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004591#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004592
Yushin Cho77bba8d2016-11-04 16:36:56 -07004593#if !CONFIG_PVQ
Alex Conversea9598cd2017-02-03 14:18:05 -08004594#if !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004595 read_coef_probs(fc, cm->tx_mode, &r);
Thomas Daviesfc1598a2017-01-13 17:07:25 +00004596#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004597
4598#if CONFIG_VAR_TX
4599 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004600 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
Yushin Cho77bba8d2016-11-04 16:36:56 -07004601#endif // CONFIG_VAR_TX
4602#endif // !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004603 for (k = 0; k < SKIP_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004604 av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004605
Thomas Daviesd6ee8a82017-03-02 14:42:50 +00004606#if CONFIG_DELTA_Q && !CONFIG_EC_ADAPT
Thomas Davies665cd702017-03-02 10:20:30 +00004607 for (k = 0; k < DELTA_Q_PROBS; ++k)
Thomas Daviesf6936102016-09-05 16:51:31 +01004608 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
4609#endif
4610
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004611#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004612 if (cm->seg.enabled && cm->seg.update_map) {
4613 if (cm->seg.temporal_update) {
4614 for (k = 0; k < PREDICTION_PROBS; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004615 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004616 }
4617 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004618 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004619 }
4620
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004621 for (j = 0; j < INTRA_MODES; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004622 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004623 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004624 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004625
4626#if CONFIG_EXT_PARTITION_TYPES
4627 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004628 av1_diff_update_prob(&r, &fc->partition_prob[0][i], ACCT_STR);
Alex Converse55c6bde2017-01-12 15:55:31 -08004629 for (j = 1; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004630 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004631 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004632#else
Alex Converse55c6bde2017-01-12 15:55:31 -08004633 for (j = 0; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004634 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004635 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004636#endif // CONFIG_EXT_PARTITION_TYPES
hui su9aa97492017-01-26 16:46:01 -08004637
Alex Converse55c6bde2017-01-12 15:55:31 -08004638#if CONFIG_UNPOISON_PARTITION_CTX
4639 for (; j < PARTITION_CONTEXTS_PRIMARY + PARTITION_BLOCK_SIZES; ++j)
4640 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_VERT], ACCT_STR);
4641 for (; j < PARTITION_CONTEXTS_PRIMARY + 2 * PARTITION_BLOCK_SIZES; ++j)
4642 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_HORZ], ACCT_STR);
4643#endif // CONFIG_UNPOISON_PARTITION_CTX
hui su9aa97492017-01-26 16:46:01 -08004644#endif // !CONFIG_EC_ADAPT
4645
4646#if CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004647 for (i = 0; i < INTRA_FILTERS + 1; ++i)
4648 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004649 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
hui su9aa97492017-01-26 16:46:01 -08004650#endif // CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004651
4652 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004653 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Nathan E. Egge10ba2be2016-11-16 09:44:26 -05004654#if CONFIG_EC_MULTISYMBOL
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00004655 av1_copy(cm->fc->kf_y_cdf, av1_kf_y_mode_cdf);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004656#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004657#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004658 for (k = 0; k < INTRA_MODES; k++)
Thomas Davies6519beb2016-10-19 14:46:07 +01004659 for (j = 0; j < INTRA_MODES; j++)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004660 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004661 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004662#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004663 } else {
4664#if !CONFIG_REF_MV
4665 nmv_context *const nmvc = &fc->nmvc;
4666#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004667 read_inter_mode_probs(fc, &r);
4668
4669#if CONFIG_EXT_INTER
4670 read_inter_compound_mode_probs(fc, &r);
4671 if (cm->reference_mode != COMPOUND_REFERENCE) {
4672 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4673 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004674 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004675 }
4676 }
4677 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4678 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004679 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004680 }
4681 for (i = 0; i < BLOCK_SIZES; i++) {
4682 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004683 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004684 }
4685 }
4686 }
4687 if (cm->reference_mode != SINGLE_REFERENCE) {
4688 for (i = 0; i < BLOCK_SIZES; i++) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08004689 for (j = 0; j < COMPOUND_TYPES - 1; j++) {
4690 av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004691 }
4692 }
4693 }
4694#endif // CONFIG_EXT_INTER
4695
Yue Chencb60b182016-10-13 15:18:22 -07004696#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004697 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
Yue Chencb60b182016-10-13 15:18:22 -07004698 for (j = 0; j < MOTION_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004699 av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004700 }
Yue Chencb60b182016-10-13 15:18:22 -07004701#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004702
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004703#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004704 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004705#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004706
4707 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004708 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004709
4710 if (cm->reference_mode != SINGLE_REFERENCE)
4711 setup_compound_reference_mode(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004712 read_frame_reference_mode_probs(cm, &r);
4713
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004714#if !CONFIG_EC_ADAPT
Nathan E. Egge5710c722016-09-08 10:01:16 -04004715 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004716 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004717 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
Nathan E. Egge5710c722016-09-08 10:01:16 -04004718 }
Thomas9ac55082016-09-23 18:04:17 +01004719#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004720
4721#if CONFIG_REF_MV
4722 for (i = 0; i < NMV_CONTEXTS; ++i)
4723 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
4724#else
4725 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
4726#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004727#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004728 read_ext_tx_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004729#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004730#if CONFIG_SUPERTX
4731 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
4732#endif
4733#if CONFIG_GLOBAL_MOTION
4734 read_global_motion(cm, &r);
Thomas Davies6519beb2016-10-19 14:46:07 +01004735#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004736 }
Thomas Davies6519beb2016-10-19 14:46:07 +01004737#if CONFIG_EC_MULTISYMBOL
Thomas Davies87aeeb82017-02-17 00:19:40 +00004738#if CONFIG_NEW_TOKENSET
4739 av1_coef_head_cdfs(fc);
4740#endif
4741 /* Make tail distribution from head */
Thomas Davies6519beb2016-10-19 14:46:07 +01004742 av1_coef_pareto_cdfs(fc);
David Barker599dfd02016-11-10 13:20:12 +00004743#if CONFIG_REF_MV
4744 for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
4745#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004746 av1_set_mv_cdfs(&fc->nmvc);
David Barker599dfd02016-11-10 13:20:12 +00004747#endif
Thomas Davies6519beb2016-10-19 14:46:07 +01004748 av1_set_mode_cdfs(cm);
4749#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004750
Yaowu Xuf883b422016-08-30 14:01:10 -07004751 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004752}
4753
4754#ifdef NDEBUG
4755#define debug_check_frame_counts(cm) (void)0
4756#else // !NDEBUG
4757// Counts should only be incremented when frame_parallel_decoding_mode and
4758// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07004759static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004760 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07004761 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004762 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
4763 cm->error_resilient_mode);
4764 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
4765 sizeof(cm->counts.y_mode)));
4766 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
4767 sizeof(cm->counts.uv_mode)));
4768 assert(!memcmp(cm->counts.partition, zero_counts.partition,
4769 sizeof(cm->counts.partition)));
4770 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
4771 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
4772 sizeof(cm->counts.eob_branch)));
Thomas Daviesab780672017-02-01 12:07:29 +00004773#if CONFIG_EC_MULTISYMBOL
4774 assert(!memcmp(cm->counts.blockz_count, zero_counts.blockz_count,
4775 sizeof(cm->counts.blockz_count)));
4776#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004777 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
4778 sizeof(cm->counts.switchable_interp)));
4779 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
4780 sizeof(cm->counts.inter_mode)));
4781#if CONFIG_EXT_INTER
4782 assert(!memcmp(cm->counts.inter_compound_mode,
4783 zero_counts.inter_compound_mode,
4784 sizeof(cm->counts.inter_compound_mode)));
4785 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
4786 sizeof(cm->counts.interintra)));
4787 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
4788 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08004789 assert(!memcmp(cm->counts.compound_interinter,
4790 zero_counts.compound_interinter,
4791 sizeof(cm->counts.compound_interinter)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004792#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07004793#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
4794 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
4795 sizeof(cm->counts.motion_mode)));
4796#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004797 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
4798 sizeof(cm->counts.intra_inter)));
4799 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
4800 sizeof(cm->counts.comp_inter)));
4801 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
4802 sizeof(cm->counts.single_ref)));
4803 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
4804 sizeof(cm->counts.comp_ref)));
4805#if CONFIG_EXT_REFS
4806 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
4807 sizeof(cm->counts.comp_bwdref)));
4808#endif // CONFIG_EXT_REFS
4809 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
4810 sizeof(cm->counts.tx_size)));
4811 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
4812#if CONFIG_REF_MV
4813 assert(
4814 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
4815 assert(
4816 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
4817#else
4818 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
4819#endif
4820 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
4821 sizeof(cm->counts.inter_ext_tx)));
4822 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
4823 sizeof(cm->counts.intra_ext_tx)));
4824}
4825#endif // NDEBUG
4826
Yaowu Xuf883b422016-08-30 14:01:10 -07004827static struct aom_read_bit_buffer *init_read_bit_buffer(
4828 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
4829 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004830 rb->bit_offset = 0;
4831 rb->error_handler = error_handler;
4832 rb->error_handler_data = &pbi->common;
4833 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004834 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004835 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
4836 rb->bit_buffer = clear_data;
4837 rb->bit_buffer_end = clear_data + n;
4838 } else {
4839 rb->bit_buffer = data;
4840 rb->bit_buffer_end = data_end;
4841 }
4842 return rb;
4843}
4844
4845//------------------------------------------------------------------------------
4846
Yaowu Xuf883b422016-08-30 14:01:10 -07004847int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
4848 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
4849 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
4850 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004851}
4852
Yaowu Xuf883b422016-08-30 14:01:10 -07004853void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
4854 int *height) {
4855 *width = aom_rb_read_literal(rb, 16) + 1;
4856 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004857}
4858
Yaowu Xuf883b422016-08-30 14:01:10 -07004859BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
4860 int profile = aom_rb_read_bit(rb);
4861 profile |= aom_rb_read_bit(rb) << 1;
4862 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004863 return (BITSTREAM_PROFILE)profile;
4864}
4865
Yaowu Xuf883b422016-08-30 14:01:10 -07004866void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
4867 const uint8_t *data_end, const uint8_t **p_data_end) {
4868 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004869 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07004870 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004871 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004872 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004873 size_t first_partition_size;
4874 YV12_BUFFER_CONFIG *new_fb;
4875
4876#if CONFIG_BITSTREAM_DEBUG
4877 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
4878#endif
4879
4880 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004881 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Thomas Davies72712e62016-11-09 12:17:51 +00004882#if CONFIG_TILE_GROUPS
4883 pbi->first_partition_size = first_partition_size;
4884 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
4885#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004886 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004887 xd->cur_buf = new_fb;
4888#if CONFIG_GLOBAL_MOTION
4889 xd->global_motion = cm->global_motion;
4890#endif // CONFIG_GLOBAL_MOTION
4891
4892 if (!first_partition_size) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004893 // showing a frame directly
4894 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004895 return;
4896 }
4897
Yaowu Xuf883b422016-08-30 14:01:10 -07004898 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004899 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07004900 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004901 "Truncated packet or corrupt header length");
4902
Jingning Han24e0a182016-11-20 22:34:12 -08004903#if CONFIG_REF_MV
Dengca8d24d2016-10-17 14:06:35 +08004904 cm->setup_mi(cm);
4905#endif
4906
Fangwen Fu8d164de2016-12-14 13:40:54 -08004907#if CONFIG_TEMPMV_SIGNALING
4908 if (cm->use_prev_frame_mvs) {
4909 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4910 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4911 assert(!cm->error_resilient_mode &&
4912 cm->width == last_fb_ref_buf->buf->y_width &&
4913 cm->height == last_fb_ref_buf->buf->y_height &&
4914 !cm->prev_frame->intra_only);
4915 }
4916#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004917 cm->use_prev_frame_mvs =
4918 !cm->error_resilient_mode && cm->width == cm->last_width &&
4919 cm->height == cm->last_height && !cm->last_intra_only &&
4920 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004921#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004922#if CONFIG_EXT_REFS
4923 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
4924 // show_exisiting_frame=1, nor can it take a frame not used as
4925 // a reference, it is probable that by the time it is being
4926 // referred to, the frame buffer it originally points to may
4927 // already get expired and have been reassigned to the current
4928 // newly coded frame. Hence, we need to check whether this is
4929 // the case, and if yes, we have 2 choices:
4930 // (1) Simply disable the use of previous frame mvs; or
4931 // (2) Have cm->prev_frame point to one reference frame buffer,
4932 // e.g. LAST_FRAME.
4933 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
4934 // Reassign the LAST_FRAME buffer to cm->prev_frame.
4935 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4936 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4937 }
4938#endif // CONFIG_EXT_REFS
4939
Yaowu Xuf883b422016-08-30 14:01:10 -07004940 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004941
4942 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
4943 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07004944 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004945 "Uninitialized entropy context.");
4946
Yaowu Xuf883b422016-08-30 14:01:10 -07004947 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004948
4949 xd->corrupted = 0;
4950 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
4951 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07004952 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004953 "Decode failed. Frame data header is corrupted.");
4954
4955 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004956 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004957 }
4958
4959 // If encoded in frame parallel mode, frame context is ready after decoding
4960 // the frame header.
4961 if (cm->frame_parallel_decode &&
4962 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004963 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004964 FrameWorkerData *const frame_worker_data = worker->data1;
4965 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
4966 context_updated = 1;
4967 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4968 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004969 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004970 pbi->cur_buf->row = -1;
4971 pbi->cur_buf->col = -1;
4972 frame_worker_data->frame_context_ready = 1;
4973 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07004974 av1_frameworker_signal_stats(worker);
4975 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004976 }
4977
hui su0d103572017-03-01 17:58:01 -08004978#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07004979 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004980 cm->coef_probs_update_idx = 0;
hui su0d103572017-03-01 17:58:01 -08004981#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004982
4983 if (pbi->max_threads > 1
4984#if CONFIG_EXT_TILE
4985 && pbi->dec_tile_col < 0 // Decoding all columns
4986#endif // CONFIG_EXT_TILE
4987 && cm->tile_cols > 1) {
4988 // Multi-threaded tile decoder
4989 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
4990 if (!xd->corrupted) {
4991 if (!cm->skip_loop_filter) {
4992 // If multiple threads are used to decode tiles, then we use those
4993 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07004994 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
4995 0, 0, pbi->tile_workers, pbi->num_tile_workers,
4996 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004997 }
4998 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004999 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005000 "Decode failed. Frame data is corrupted.");
5001 }
5002 } else {
5003 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
5004 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005005
Jean-Marc Valin01435132017-02-18 14:12:53 -05005006#if CONFIG_CDEF
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02005007 if (cm->dering_level && !cm->skip_loop_filter) {
5008 av1_dering_frame(&pbi->cur_buf->buf, cm, &pbi->mb, cm->dering_level);
5009 }
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02005010 if (!cm->skip_loop_filter) {
5011 const YV12_BUFFER_CONFIG *const frame = &pbi->cur_buf->buf;
5012 if (cm->clpf_strength_y) {
Yaowu Xud71be782016-10-14 08:47:03 -07005013 av1_clpf_frame(frame, NULL, cm, cm->clpf_size != CLPF_NOSIZE,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02005014 cm->clpf_strength_y + (cm->clpf_strength_y == 3),
Yaowu Xud71be782016-10-14 08:47:03 -07005015 4 + cm->clpf_size, AOM_PLANE_Y, clpf_bit);
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02005016 }
5017 if (cm->clpf_strength_u) {
Yaowu Xud71be782016-10-14 08:47:03 -07005018 av1_clpf_frame(frame, NULL, cm, 0, // No block signals for chroma
5019 cm->clpf_strength_u + (cm->clpf_strength_u == 3), 4,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02005020 AOM_PLANE_U, NULL);
5021 }
5022 if (cm->clpf_strength_v) {
Yaowu Xud71be782016-10-14 08:47:03 -07005023 av1_clpf_frame(frame, NULL, cm, 0, // No block signals for chroma
5024 cm->clpf_strength_v + (cm->clpf_strength_v == 3), 4,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02005025 AOM_PLANE_V, NULL);
5026 }
Steinar Midtskogend06588a2016-05-06 13:48:20 +02005027 }
5028 if (cm->clpf_blocks) aom_free(cm->clpf_blocks);
Debargha Mukherjee00c54332017-03-03 15:44:17 -08005029#endif // CONFIG_CDEF
5030
5031#if CONFIG_LOOP_RESTORATION
5032 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
5033 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
5034 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
5035 av1_loop_restoration_frame(new_fb, cm, cm->rst_info, 7, 0, NULL);
5036 }
5037#endif // CONFIG_LOOP_RESTORATION
Thomas Daedef56859f2016-04-19 16:57:24 -07005038
Yaowu Xuc27fc142016-08-22 16:08:15 -07005039 if (!xd->corrupted) {
5040 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
hui su0d103572017-03-01 17:58:01 -08005041#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07005042 cm->partial_prob_update = 0;
hui su0d103572017-03-01 17:58:01 -08005043#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07005044 av1_adapt_coef_probs(cm);
5045 av1_adapt_intra_frame_probs(cm);
hui suff0da2b2017-03-07 15:51:37 -08005046#if CONFIG_ADAPT_SCAN
5047 av1_adapt_scan_order(cm);
5048#endif // CONFIG_ADAPT_SCAN
Yaowu Xuc27fc142016-08-22 16:08:15 -07005049
5050 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005051 av1_adapt_inter_frame_probs(cm);
5052 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005053 }
5054 } else {
5055 debug_check_frame_counts(cm);
5056 }
5057 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005058 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005059 "Decode failed. Frame data is corrupted.");
5060 }
5061
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05005062#if CONFIG_INSPECTION
5063 if (pbi->inspect_cb != NULL) {
5064 (*pbi->inspect_cb)(pbi, pbi->inspect_ctx);
5065 }
5066#endif
5067
Yaowu Xuc27fc142016-08-22 16:08:15 -07005068 // Non frame parallel update frame context here.
5069 if (!cm->error_resilient_mode && !context_updated)
5070 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
5071}