blob: e3f45cfe748f93e9ac27cf38c62318994c995cb6 [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"
Debargha Mukherjee47748b52017-03-24 12:20:49 -070024#include "aom_dsp/binary_codes_reader.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070025#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070026#include "aom_ports/mem.h"
27#include "aom_ports/mem_ops.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070028#include "aom_scale/aom_scale.h"
29#include "aom_util/aom_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070030
31#include "av1/common/alloccommon.h"
Jean-Marc Valin01435132017-02-18 14:12:53 -050032#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010033#include "av1/common/cdef.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070034#include "av1/common/clpf.h"
35#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"
Angie Chiang133733c2017-03-17 12:50:20 -070055#if CONFIG_LV_MAP
56#include "av1/decoder/decodetxb.h"
57#endif
Jingning Han1aab8182016-06-03 11:09:06 -070058#include "av1/decoder/detokenize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070059#include "av1/decoder/dsubexp.h"
60
Debargha Mukherjee3b6c5442017-03-30 08:22:00 -070061#if CONFIG_WARPED_MOTION || CONFIG_GLOBAL_MOTION
Yue Chen69f18e12016-09-08 14:48:15 -070062#include "av1/common/warped_motion.h"
Debargha Mukherjee3b6c5442017-03-30 08:22:00 -070063#endif // CONFIG_WARPED_MOTION || CONFIG_GLOBAL_MOTION
Yue Chen69f18e12016-09-08 14:48:15 -070064
Yaowu Xuf883b422016-08-30 14:01:10 -070065#define MAX_AV1_HEADER_SIZE 80
Michael Bebenita6048d052016-08-25 14:40:54 -070066#define ACCT_STR __func__
Yaowu Xuc27fc142016-08-22 16:08:15 -070067
Yushin Cho77bba8d2016-11-04 16:36:56 -070068#if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -070069#include "av1/common/partition.h"
hui suff0da2b2017-03-07 15:51:37 -080070#include "av1/common/pvq.h"
71#include "av1/common/scan.h"
Yushin Cho77bba8d2016-11-04 16:36:56 -070072#include "av1/decoder/decint.h"
hui suff0da2b2017-03-07 15:51:37 -080073#include "av1/decoder/pvq_decoder.h"
74#include "av1/encoder/encodemb.h"
Yushin Cho77bba8d2016-11-04 16:36:56 -070075#include "av1/encoder/hybrid_fwd_txfm.h"
76#endif
77
Thomas Davies80188d12016-10-26 16:08:35 -070078static struct aom_read_bit_buffer *init_read_bit_buffer(
79 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
80 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]);
81static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
82 size_t partition_size);
83static size_t read_uncompressed_header(AV1Decoder *pbi,
84 struct aom_read_bit_buffer *rb);
85
Yaowu Xuf883b422016-08-30 14:01:10 -070086static int is_compound_reference_allowed(const AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070087 int i;
88 if (frame_is_intra_only(cm)) return 0;
89 for (i = 1; i < INTER_REFS_PER_FRAME; ++i)
90 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
91
92 return 0;
93}
94
Yaowu Xuf883b422016-08-30 14:01:10 -070095static void setup_compound_reference_mode(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070096#if CONFIG_EXT_REFS
97 cm->comp_fwd_ref[0] = LAST_FRAME;
98 cm->comp_fwd_ref[1] = LAST2_FRAME;
99 cm->comp_fwd_ref[2] = LAST3_FRAME;
100 cm->comp_fwd_ref[3] = GOLDEN_FRAME;
101
102 cm->comp_bwd_ref[0] = BWDREF_FRAME;
103 cm->comp_bwd_ref[1] = ALTREF_FRAME;
104#else
105 if (cm->ref_frame_sign_bias[LAST_FRAME] ==
106 cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
107 cm->comp_fixed_ref = ALTREF_FRAME;
108 cm->comp_var_ref[0] = LAST_FRAME;
109 cm->comp_var_ref[1] = GOLDEN_FRAME;
110 } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
111 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
112 cm->comp_fixed_ref = GOLDEN_FRAME;
113 cm->comp_var_ref[0] = LAST_FRAME;
114 cm->comp_var_ref[1] = ALTREF_FRAME;
115 } else {
116 cm->comp_fixed_ref = LAST_FRAME;
117 cm->comp_var_ref[0] = GOLDEN_FRAME;
118 cm->comp_var_ref[1] = ALTREF_FRAME;
119 }
120#endif // CONFIG_EXT_REFS
121}
122
123static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
124 return len != 0 && len <= (size_t)(end - start);
125}
126
Yaowu Xuf883b422016-08-30 14:01:10 -0700127static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
128 const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700129 return data > max ? max : data;
130}
131
Yue Cheneeacc4c2017-01-17 17:29:17 -0800132static TX_MODE read_tx_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
133 struct aom_read_bit_buffer *rb) {
134 int i, all_lossless = 1;
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800135#if CONFIG_TX64X64
Yue Cheneeacc4c2017-01-17 17:29:17 -0800136 TX_MODE tx_mode;
137#endif
138
139 if (cm->seg.enabled) {
140 for (i = 0; i < MAX_SEGMENTS; ++i) {
141 if (!xd->lossless[i]) {
142 all_lossless = 0;
143 break;
144 }
145 }
146 } else {
147 all_lossless = xd->lossless[0];
148 }
149
150 if (all_lossless) return ONLY_4X4;
151#if CONFIG_TX64X64
152 tx_mode = aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800153 if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
154 return tx_mode;
155#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700156 return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800157#endif // CONFIG_TX64X64
Yaowu Xuc27fc142016-08-22 16:08:15 -0700158}
159
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -0500160#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -0700161static void read_tx_size_probs(FRAME_CONTEXT *fc, aom_reader *r) {
162 int i, j, k;
163 for (i = 0; i < MAX_TX_DEPTH; ++i)
164 for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
165 for (k = 0; k < i + 1; ++k)
166 av1_diff_update_prob(r, &fc->tx_size_probs[i][j][k], ACCT_STR);
167}
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -0500168#endif
Yaowu Xuefc75352016-10-31 09:46:42 -0700169
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400170#if !CONFIG_EC_ADAPT
Yaowu Xuf883b422016-08-30 14:01:10 -0700171static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700172 int i, j;
Nathan E. Egge4947c292016-04-26 11:37:06 -0400173 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700174 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700175 av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i], ACCT_STR);
Nathan E. Egge4947c292016-04-26 11:37:06 -0400176 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700177}
Thomas Davies6519beb2016-10-19 14:46:07 +0100178#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700179
Yaowu Xuf883b422016-08-30 14:01:10 -0700180static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700181#if CONFIG_REF_MV
Yaowu Xu8af861b2016-11-01 12:12:11 -0700182 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700183 for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700184 av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700185 for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700186 av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700187 for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700188 av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700189 for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700190 av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700191#if CONFIG_EXT_INTER
Michael Bebenita6048d052016-08-25 14:40:54 -0700192 av1_diff_update_prob(r, &fc->new2mv_prob, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700193#endif // CONFIG_EXT_INTER
194#else
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400195#if !CONFIG_EC_ADAPT
Yaowu Xu8af861b2016-11-01 12:12:11 -0700196 int i, j;
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400197 for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700198 for (j = 0; j < INTER_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700199 av1_diff_update_prob(r, &fc->inter_mode_probs[i][j], ACCT_STR);
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400200 }
Yaowu Xu8af861b2016-11-01 12:12:11 -0700201#else
202 (void)fc;
203 (void)r;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700204#endif
Thomas Davies6519beb2016-10-19 14:46:07 +0100205#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700206}
207
208#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700209static void read_inter_compound_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700210 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -0700211 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700212 for (j = 0; j < INTER_MODE_CONTEXTS; ++j) {
213 for (i = 0; i < INTER_COMPOUND_MODES - 1; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700214 av1_diff_update_prob(r, &fc->inter_compound_mode_probs[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700215 }
216 }
217 }
218}
219#endif // CONFIG_EXT_INTER
Yaowu Xu17fd2f22016-11-17 18:23:28 -0800220#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100221#if !CONFIG_EXT_TX
222static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
223 int i, j, k;
224 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
225 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
226 for (j = 0; j < TX_TYPES; ++j) {
227 for (k = 0; k < TX_TYPES - 1; ++k)
228 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[i][j][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100229 }
230 }
231 }
232 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
233 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
234 for (k = 0; k < TX_TYPES - 1; ++k)
235 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[i][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100236 }
237 }
238}
239#endif
240#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700241
242static REFERENCE_MODE read_frame_reference_mode(
Yaowu Xuf883b422016-08-30 14:01:10 -0700243 const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700244 if (is_compound_reference_allowed(cm)) {
Zoe Liub05e5d12017-02-07 14:32:53 -0800245#if CONFIG_REF_ADAPT
246 return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
247#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700248 return aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700249 ? REFERENCE_MODE_SELECT
Yaowu Xuf883b422016-08-30 14:01:10 -0700250 : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
Zoe Liub05e5d12017-02-07 14:32:53 -0800251#endif // CONFIG_REF_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700252 } else {
253 return SINGLE_REFERENCE;
254 }
255}
256
Yaowu Xuf883b422016-08-30 14:01:10 -0700257static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700258 FRAME_CONTEXT *const fc = cm->fc;
259 int i, j;
260
261 if (cm->reference_mode == REFERENCE_MODE_SELECT)
262 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700263 av1_diff_update_prob(r, &fc->comp_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700264
265 if (cm->reference_mode != COMPOUND_REFERENCE) {
266 for (i = 0; i < REF_CONTEXTS; ++i) {
267 for (j = 0; j < (SINGLE_REFS - 1); ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700268 av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700269 }
270 }
271 }
272
273 if (cm->reference_mode != SINGLE_REFERENCE) {
274 for (i = 0; i < REF_CONTEXTS; ++i) {
275#if CONFIG_EXT_REFS
276 for (j = 0; j < (FWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700277 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700278 for (j = 0; j < (BWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700279 av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700280#else
281 for (j = 0; j < (COMP_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700282 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700283#endif // CONFIG_EXT_REFS
284 }
285 }
286}
287
Yaowu Xuf883b422016-08-30 14:01:10 -0700288static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700289 int i;
Michael Bebenita6048d052016-08-25 14:40:54 -0700290 for (i = 0; i < n; ++i) av1_diff_update_prob(r, &p[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700291}
292
Yaowu Xuf883b422016-08-30 14:01:10 -0700293static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Thomas9ac55082016-09-23 18:04:17 +0100294 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700295
Yaowu Xue86288d2016-10-31 15:56:38 -0700296#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100297 int j;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700298 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
299
Yaowu Xuc27fc142016-08-22 16:08:15 -0700300 for (i = 0; i < 2; ++i) {
301 nmv_component *const comp_ctx = &ctx->comps[i];
302 update_mv_probs(&comp_ctx->sign, 1, r);
303 update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
304 update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
305 update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
306 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700307 for (i = 0; i < 2; ++i) {
308 nmv_component *const comp_ctx = &ctx->comps[i];
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400309 for (j = 0; j < CLASS0_SIZE; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700310 update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400311 }
Nathan E. Eggeac499f32016-09-08 15:38:57 -0400312 update_mv_probs(comp_ctx->fp, MV_FP_SIZE - 1, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700313 }
Alex Converseaca9feb2016-10-10 11:08:10 -0700314#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700315
316 if (allow_hp) {
317 for (i = 0; i < 2; ++i) {
318 nmv_component *const comp_ctx = &ctx->comps[i];
319 update_mv_probs(&comp_ctx->class0_hp, 1, r);
320 update_mv_probs(&comp_ctx->hp, 1, r);
321 }
322 }
323}
324
325static void inverse_transform_block(MACROBLOCKD *xd, int plane,
326 const TX_TYPE tx_type,
327 const TX_SIZE tx_size, uint8_t *dst,
Jingning Han1be18782016-10-21 11:48:15 -0700328 int stride, int16_t scan_line, int eob) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700329 struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han1be18782016-10-21 11:48:15 -0700330 tran_low_t *const dqcoeff = pd->dqcoeff;
Angie Chiangd92d4bf2017-04-02 17:49:18 -0700331 av1_inverse_transform_block(xd, dqcoeff, tx_type, tx_size, dst, stride, eob);
Jingning Han1be18782016-10-21 11:48:15 -0700332 memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700333}
334
Yushin Cho77bba8d2016-11-04 16:36:56 -0700335#if CONFIG_PVQ
Thomas Daede6ff6af62017-02-03 16:29:24 -0800336static int av1_pvq_decode_helper(MACROBLOCKD *xd, tran_low_t *ref_coeff,
Thomas Daede1dbda1b2017-02-06 16:06:29 -0800337 tran_low_t *dqcoeff, int16_t *quant, int pli,
Yushin Cho77bba8d2016-11-04 16:36:56 -0700338 int bs, TX_TYPE tx_type, int xdec,
ltrudeaue1c09292017-01-20 15:42:13 -0500339 PVQ_SKIP_TYPE ac_dc_coded) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700340 unsigned int flags; // used for daala's stream analyzer.
341 int off;
342 const int is_keyframe = 0;
343 const int has_dc_skip = 1;
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800344 int coeff_shift = 3 - get_tx_scale(bs);
Thomas Daede6ff6af62017-02-03 16:29:24 -0800345 int hbd_downshift = 0;
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800346 int rounding_mask;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700347 // DC quantizer for PVQ
348 int pvq_dc_quant;
349 int lossless = (quant[0] == 0);
350 const int blk_size = tx_size_wide[bs];
351 int eob = 0;
352 int i;
Thomas Daede6ff6af62017-02-03 16:29:24 -0800353 od_dec_ctx *dec = &xd->daala_dec;
Yushin Cho70669122016-12-08 09:53:14 -1000354 int use_activity_masking = dec->use_activity_masking;
Thomas Daede1dbda1b2017-02-06 16:06:29 -0800355 DECLARE_ALIGNED(16, tran_low_t, dqcoeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
356 DECLARE_ALIGNED(16, tran_low_t, ref_coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700357
Yushin Cho48f84db2016-11-07 21:20:17 -0800358 od_coeff ref_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
359 od_coeff out_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700360
Thomas Daede6ff6af62017-02-03 16:29:24 -0800361#if CONFIG_AOM_HIGHBITDEPTH
362 hbd_downshift = xd->bd - 8;
363#endif // CONFIG_AOM_HIGHBITDEPTH
364
Yushin Cho77bba8d2016-11-04 16:36:56 -0700365 od_raster_to_coding_order(ref_coeff_pvq, blk_size, tx_type, ref_coeff,
366 blk_size);
367
Thomas Daede6ff6af62017-02-03 16:29:24 -0800368 assert(OD_COEFF_SHIFT >= 4);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700369 if (lossless)
370 pvq_dc_quant = 1;
371 else {
Yushin Cho70669122016-12-08 09:53:14 -1000372 if (use_activity_masking)
373 pvq_dc_quant = OD_MAXI(
Thomas Daede6ff6af62017-02-03 16:29:24 -0800374 1, (quant[0] << (OD_COEFF_SHIFT - 3) >> hbd_downshift) *
Yushin Cho70669122016-12-08 09:53:14 -1000375 dec->state.pvq_qm_q4[pli][od_qm_get_index(bs, 0)] >>
376 4);
377 else
Thomas Daede6ff6af62017-02-03 16:29:24 -0800378 pvq_dc_quant =
379 OD_MAXI(1, quant[0] << (OD_COEFF_SHIFT - 3) >> hbd_downshift);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700380 }
381
382 off = od_qm_offset(bs, xdec);
383
384 // copy int16 inputs to int32
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800385 for (i = 0; i < blk_size * blk_size; i++) {
Timothy B. Terriberry4e6a8f32017-02-24 11:00:59 -0800386 ref_int32[i] =
Thomas Daede6ff6af62017-02-03 16:29:24 -0800387 AOM_SIGNED_SHL(ref_coeff_pvq[i], OD_COEFF_SHIFT - coeff_shift) >>
388 hbd_downshift;
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800389 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700390
Thomas Daede6ff6af62017-02-03 16:29:24 -0800391 od_pvq_decode(dec, ref_int32, out_int32,
392 OD_MAXI(1, quant[1] << (OD_COEFF_SHIFT - 3) >> hbd_downshift),
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800393 pli, bs, OD_PVQ_BETA[use_activity_masking][pli][bs],
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800394 OD_ROBUST_STREAM, is_keyframe, &flags, ac_dc_coded,
395 dec->state.qm + off, dec->state.qm_inv + off);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700396
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800397 if (!has_dc_skip || out_int32[0]) {
398 out_int32[0] =
Yushin Choc49ef3a2017-03-13 17:27:25 -0700399 has_dc_skip + generic_decode(dec->r, &dec->state.adapt->model_dc[pli],
400 -1, &dec->state.adapt->ex_dc[pli][bs][0],
401 2, "dc:mag");
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800402 if (out_int32[0]) out_int32[0] *= aom_read_bit(dec->r, "dc:sign") ? -1 : 1;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700403 }
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800404 out_int32[0] = out_int32[0] * pvq_dc_quant + ref_int32[0];
405
406 // copy int32 result back to int16
407 assert(OD_COEFF_SHIFT > coeff_shift);
408 rounding_mask = (1 << (OD_COEFF_SHIFT - coeff_shift - 1)) - 1;
409 for (i = 0; i < blk_size * blk_size; i++) {
Thomas Daede6ff6af62017-02-03 16:29:24 -0800410 out_int32[i] = AOM_SIGNED_SHL(out_int32[i], hbd_downshift);
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800411 dqcoeff_pvq[i] = (out_int32[i] + (out_int32[i] < 0) + rounding_mask) >>
412 (OD_COEFF_SHIFT - coeff_shift);
413 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700414
415 od_coding_order_to_raster(dqcoeff, blk_size, tx_type, dqcoeff_pvq, blk_size);
416
417 eob = blk_size * blk_size;
418
419 return eob;
420}
421
ltrudeaue1c09292017-01-20 15:42:13 -0500422static PVQ_SKIP_TYPE read_pvq_skip(AV1_COMMON *cm, MACROBLOCKD *const xd,
423 int plane, TX_SIZE tx_size) {
424 // decode ac/dc coded flag. bit0: DC coded, bit1 : AC coded
425 // NOTE : we don't use 5 symbols for luma here in aom codebase,
426 // since block partition is taken care of by aom.
427 // So, only AC/DC skip info is coded
Yushin Cho00779272017-02-21 10:38:16 -0800428 const int ac_dc_coded = aom_read_symbol(
ltrudeaue1c09292017-01-20 15:42:13 -0500429 xd->daala_dec.r,
Yushin Choc49ef3a2017-03-13 17:27:25 -0700430 xd->daala_dec.state.adapt->skip_cdf[2 * tx_size + (plane != 0)], 4,
Yushin Cho00779272017-02-21 10:38:16 -0800431 "skip");
ltrudeaue1c09292017-01-20 15:42:13 -0500432 if (ac_dc_coded < 0 || ac_dc_coded > 3) {
433 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
434 "Invalid PVQ Skip Type");
435 }
436 return ac_dc_coded;
437}
438
439static int av1_pvq_decode_helper2(AV1_COMMON *cm, MACROBLOCKD *const xd,
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800440 MB_MODE_INFO *const mbmi, int plane, int row,
441 int col, TX_SIZE tx_size, TX_TYPE tx_type) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700442 struct macroblockd_plane *const pd = &xd->plane[plane];
443 // transform block size in pixels
444 int tx_blk_size = tx_size_wide[tx_size];
445 int i, j;
446 tran_low_t *pvq_ref_coeff = pd->pvq_ref_coeff;
447 const int diff_stride = tx_blk_size;
448 int16_t *pred = pd->pred;
449 tran_low_t *const dqcoeff = pd->dqcoeff;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700450 uint8_t *dst;
451 int eob;
ltrudeaue1c09292017-01-20 15:42:13 -0500452 const PVQ_SKIP_TYPE ac_dc_coded = read_pvq_skip(cm, xd, plane, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700453
454 eob = 0;
455 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
456
Yushin Cho77bba8d2016-11-04 16:36:56 -0700457 if (ac_dc_coded) {
458 int xdec = pd->subsampling_x;
459 int seg_id = mbmi->segment_id;
460 int16_t *quant;
461 FWD_TXFM_PARAM fwd_txfm_param;
Yaowu Xufc1b2132016-11-07 15:16:15 -0800462 // ToDo(yaowu): correct this with optimal number from decoding process.
463 const int max_scan_line = tx_size_2d[tx_size];
Thomas Daede6ff6af62017-02-03 16:29:24 -0800464#if CONFIG_AOM_HIGHBITDEPTH
465 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
466 for (j = 0; j < tx_blk_size; j++)
467 for (i = 0; i < tx_blk_size; i++)
468 pred[diff_stride * j + i] =
469 CONVERT_TO_SHORTPTR(dst)[pd->dst.stride * j + i];
470 } else {
471#endif
472 for (j = 0; j < tx_blk_size; j++)
473 for (i = 0; i < tx_blk_size; i++)
474 pred[diff_stride * j + i] = dst[pd->dst.stride * j + i];
475#if CONFIG_AOM_HIGHBITDEPTH
476 }
477#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -0700478
479 fwd_txfm_param.tx_type = tx_type;
480 fwd_txfm_param.tx_size = tx_size;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700481 fwd_txfm_param.lossless = xd->lossless[seg_id];
482
Thomas Daede6ff6af62017-02-03 16:29:24 -0800483#if CONFIG_AOM_HIGHBITDEPTH
484 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
485 fwd_txfm_param.bd = xd->bd;
hui suf11fb882017-03-27 14:56:33 -0700486 av1_highbd_fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
Thomas Daede6ff6af62017-02-03 16:29:24 -0800487 } else {
488#endif // CONFIG_AOM_HIGHBITDEPTH
hui suf11fb882017-03-27 14:56:33 -0700489 av1_fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
Thomas Daede6ff6af62017-02-03 16:29:24 -0800490#if CONFIG_AOM_HIGHBITDEPTH
491 }
492#endif // CONFIG_AOM_HIGHBITDEPTH
Yushin Cho77bba8d2016-11-04 16:36:56 -0700493
494 quant = &pd->seg_dequant[seg_id][0]; // aom's quantizer
495
Thomas Daede6ff6af62017-02-03 16:29:24 -0800496 eob = av1_pvq_decode_helper(xd, pvq_ref_coeff, dqcoeff, quant, plane,
497 tx_size, tx_type, xdec, ac_dc_coded);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700498
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800499 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
500 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700501 }
502
503 return eob;
504}
505#endif
506
Angie Chiang752ccce2017-04-09 13:41:13 -0700507static int get_block_idx(const MACROBLOCKD *xd, int plane, int row, int col) {
508 const int bsize = xd->mi[0]->mbmi.sb_type;
509 const struct macroblockd_plane *pd = &xd->plane[plane];
510#if CONFIG_CB4X4
511#if CONFIG_CHROMA_2X2
512 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
513#else
514 const BLOCK_SIZE plane_bsize =
515 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
516#endif // CONFIG_CHROMA_2X2
517#else
518 const BLOCK_SIZE plane_bsize =
519 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
520#endif
521 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
522 const TX_SIZE tx_size = get_tx_size(plane, xd);
523 const uint8_t txh_unit = tx_size_high_unit[tx_size];
524 return row * max_blocks_wide + col * txh_unit;
525}
526
Alex Converse8aca36d2017-01-31 12:33:15 -0800527static void predict_and_reconstruct_intra_block(
528 AV1_COMMON *cm, MACROBLOCKD *const xd, aom_reader *const r,
529 MB_MODE_INFO *const mbmi, int plane, int row, int col, TX_SIZE tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500530 PLANE_TYPE plane_type = get_plane_type(plane);
Angie Chiang752ccce2017-04-09 13:41:13 -0700531 const int block_idx = get_block_idx(xd, plane, row, col);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700532#if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -0700533 (void)r;
534#endif
Angie Chiang3d005e42017-04-02 16:31:35 -0700535 av1_predict_intra_block_facade(xd, plane, block_idx, col, row, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700536
537 if (!mbmi->skip) {
538 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700539#if !CONFIG_PVQ
Angie Chiang3d005e42017-04-02 16:31:35 -0700540 struct macroblockd_plane *const pd = &xd->plane[plane];
Angie Chiang133733c2017-03-17 12:50:20 -0700541#if CONFIG_LV_MAP
542 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700543 int eob;
544 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane,
545 pd->dqcoeff, &max_scan_line, &eob);
Angie Chiang133733c2017-03-17 12:50:20 -0700546#else // CONFIG_LV_MAP
Angie Chiangff6d8902016-10-21 11:02:09 -0700547 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 0);
Jingning Han1be18782016-10-21 11:48:15 -0700548 int16_t max_scan_line = 0;
549 const int eob =
Angie Chiang5c0568a2017-03-21 16:00:39 -0700550 av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
Jingning Han1be18782016-10-21 11:48:15 -0700551 tx_type, &max_scan_line, r, mbmi->segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700552#endif // CONFIG_LV_MAP
Angie Chiang3d005e42017-04-02 16:31:35 -0700553 if (eob) {
554 uint8_t *dst =
555 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Jingning Han1be18782016-10-21 11:48:15 -0700556 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
557 max_scan_line, eob);
Angie Chiang3d005e42017-04-02 16:31:35 -0700558 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700559#else
ltrudeaue1c09292017-01-20 15:42:13 -0500560 av1_pvq_decode_helper2(cm, xd, mbmi, plane, row, col, tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700561#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700562 }
563}
564
Jingning Handddb21f2017-02-28 14:44:05 -0800565#if CONFIG_VAR_TX && !CONFIG_COEF_INTERLEAVE
Angie Chiangff6d8902016-10-21 11:02:09 -0700566static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
567 aom_reader *r, MB_MODE_INFO *const mbmi,
Jingning Han8fd62b72016-10-21 12:55:54 -0700568 int plane, BLOCK_SIZE plane_bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700569 int blk_row, int blk_col, TX_SIZE tx_size,
570 int *eob_total) {
571 const struct macroblockd_plane *const pd = &xd->plane[plane];
572 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
573 const int tx_row = blk_row >> (1 - pd->subsampling_y);
574 const int tx_col = blk_col >> (1 - pd->subsampling_x);
575 const TX_SIZE plane_tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700576 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700577 : mbmi->inter_tx_size[tx_row][tx_col];
Jingning Han5f614262016-10-27 14:27:43 -0700578 // Scale to match transform block unit.
Jingning Hanf64062f2016-11-02 16:22:18 -0700579 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
580 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700581
582 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
583
584 if (tx_size == plane_tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500585 PLANE_TYPE plane_type = get_plane_type(plane);
Angie Chiang752ccce2017-04-09 13:41:13 -0700586 int block_idx = get_block_idx(xd, plane, blk_row, blk_col);
Jingning Han8fd62b72016-10-21 12:55:54 -0700587 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiang133733c2017-03-17 12:50:20 -0700588#if CONFIG_LV_MAP
589 (void)segment_id;
590 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700591 int eob;
592 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane,
593 pd->dqcoeff, &max_scan_line, &eob);
Angie Chiang133733c2017-03-17 12:50:20 -0700594#else // CONFIG_LV_MAP
Angie Chiangff6d8902016-10-21 11:02:09 -0700595 const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700596 int16_t max_scan_line = 0;
Angie Chiang5c0568a2017-03-21 16:00:39 -0700597 const int eob = av1_decode_block_tokens(
598 cm, xd, plane, sc, blk_col, blk_row, plane_tx_size, tx_type,
599 &max_scan_line, r, mbmi->segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700600#endif // CONFIG_LV_MAP
Jingning Han9ca05b72017-01-03 14:41:36 -0800601 inverse_transform_block(xd, plane, tx_type, plane_tx_size,
602 &pd->dst.buf[(blk_row * pd->dst.stride + blk_col)
603 << tx_size_wide_log2[0]],
604 pd->dst.stride, max_scan_line, eob);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700605 *eob_total += eob;
606 } else {
Jingning Hanf64062f2016-11-02 16:22:18 -0700607 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
608 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700609 int i;
610
611 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700612
613 for (i = 0; i < 4; ++i) {
Jingning Han5f614262016-10-27 14:27:43 -0700614 const int offsetr = blk_row + (i >> 1) * bsl;
615 const int offsetc = blk_col + (i & 0x01) * bsl;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700616
617 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
618
Jingning Han8fd62b72016-10-21 12:55:54 -0700619 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
Jingning Hanf64062f2016-11-02 16:22:18 -0700620 offsetc, sub_txs, eob_total);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621 }
622 }
623}
624#endif // CONFIG_VAR_TX
625
Jingning Handddb21f2017-02-28 14:44:05 -0800626#if !CONFIG_VAR_TX || CONFIG_SUPERTX || CONFIG_COEF_INTERLEAVE || \
Jingning Hanfe45b212016-11-22 10:30:23 -0800627 (!CONFIG_VAR_TX && CONFIG_EXT_TX && CONFIG_RECT_TX)
Angie Chiangff6d8902016-10-21 11:02:09 -0700628static int reconstruct_inter_block(AV1_COMMON *cm, MACROBLOCKD *const xd,
Alex Converse8aca36d2017-01-31 12:33:15 -0800629 aom_reader *const r, int segment_id,
630 int plane, int row, int col,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700631 TX_SIZE tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500632 PLANE_TYPE plane_type = get_plane_type(plane);
Angie Chiang752ccce2017-04-09 13:41:13 -0700633 int block_idx = get_block_idx(xd, plane, row, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700634 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700635#if CONFIG_PVQ
636 int eob;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700637 (void)r;
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800638 (void)segment_id;
639#else
640 struct macroblockd_plane *const pd = &xd->plane[plane];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700641#endif
642
643#if !CONFIG_PVQ
Angie Chiang133733c2017-03-17 12:50:20 -0700644#if CONFIG_LV_MAP
645 (void)segment_id;
Jingning Han1be18782016-10-21 11:48:15 -0700646 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700647 int eob;
648 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane, pd->dqcoeff,
649 &max_scan_line, &eob);
Angie Chiang133733c2017-03-17 12:50:20 -0700650#else // CONFIG_LV_MAP
651 int16_t max_scan_line = 0;
652 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700653 const int eob =
Angie Chiang5c0568a2017-03-21 16:00:39 -0700654 av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
655 tx_type, &max_scan_line, r, segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700656#endif // CONFIG_LV_MAP
Jingning Hanca14dda2016-12-09 09:36:00 -0800657 uint8_t *dst =
658 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Jingning Han1be18782016-10-21 11:48:15 -0700659 if (eob)
Jingning Hanca14dda2016-12-09 09:36:00 -0800660 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
661 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700662#else
ltrudeaue1c09292017-01-20 15:42:13 -0500663 eob = av1_pvq_decode_helper2(cm, xd, &xd->mi[0]->mbmi, plane, row, col,
664 tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700665#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700666 return eob;
667}
668#endif // !CONFIG_VAR_TX || CONFIG_SUPER_TX
669
Angie Chiang44701f22017-02-27 10:36:44 -0800670static void set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
671 BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
672 int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700673 const int offset = mi_row * cm->mi_stride + mi_col;
674 int x, y;
675 const TileInfo *const tile = &xd->tile;
676
677 xd->mi = cm->mi_grid_visible + offset;
678 xd->mi[0] = &cm->mi[offset];
679 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
680 // passing bsize from decode_partition().
681 xd->mi[0]->mbmi.sb_type = bsize;
Angie Chiang394c3372016-11-03 11:13:15 -0700682#if CONFIG_RD_DEBUG
683 xd->mi[0]->mbmi.mi_row = mi_row;
684 xd->mi[0]->mbmi.mi_col = mi_col;
685#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700686 for (y = 0; y < y_mis; ++y)
Jingning Han97d85482016-07-15 11:06:05 -0700687 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700688
Jingning Hanfaad0e12016-12-07 10:54:57 -0800689 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700690 set_skip_context(xd, mi_row, mi_col);
691
692#if CONFIG_VAR_TX
693 xd->max_tx_size = max_txsize_lookup[bsize];
694#endif
695
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800696// Distance of Mb to the various image edges. These are specified to 8th pel
697// as they are always compared to values that are in 1/8th pel units
698#if CONFIG_DEPENDENT_HORZTILES
699 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
700 cm->dependent_horz_tiles);
701#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700702 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 -0800703#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700704
Yaowu Xuf883b422016-08-30 14:01:10 -0700705 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700706}
707
708#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700709static MB_MODE_INFO *set_offsets_extend(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700710 MACROBLOCKD *const xd,
711 const TileInfo *const tile,
712 BLOCK_SIZE bsize_pred, int mi_row_pred,
713 int mi_col_pred, int mi_row_ori,
714 int mi_col_ori) {
715 // Used in supertx
716 // (mi_row_ori, mi_col_ori): location for mv
717 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
Jingning Han93531242016-12-20 11:54:36 -0800718 const int bw = mi_size_wide[bsize_pred];
719 const int bh = mi_size_high[bsize_pred];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700720 const int offset = mi_row_ori * cm->mi_stride + mi_col_ori;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700721 xd->mi = cm->mi_grid_visible + offset;
722 xd->mi[0] = cm->mi + offset;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800723#if CONFIG_DEPENDENT_HORZTILES
724 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
725 cm->mi_cols, cm->dependent_horz_tiles);
726#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700727 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
728 cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800729#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700730
731 xd->up_available = (mi_row_ori > tile->mi_row_start);
732 xd->left_available = (mi_col_ori > tile->mi_col_start);
733
Jingning Hanfaad0e12016-12-07 10:54:57 -0800734 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700735
736 return &xd->mi[0]->mbmi;
737}
738
Angie Chiang7fcfee42017-02-24 15:51:03 -0800739#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700740static MB_MODE_INFO *set_mb_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
741 BLOCK_SIZE bsize, int mi_row, int mi_col,
742 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700743 const int offset = mi_row * cm->mi_stride + mi_col;
744 const TileInfo *const tile = &xd->tile;
745 int x, y;
746
747 xd->mi = cm->mi_grid_visible + offset;
748 xd->mi[0] = cm->mi + offset;
749 xd->mi[0]->mbmi.sb_type = bsize;
750 for (y = 0; y < y_mis; ++y)
751 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
752
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800753#if CONFIG_DEPENDENT_HORZTILES
754 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
755 cm->dependent_horz_tiles);
756#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700757 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 -0800758#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700759 return &xd->mi[0]->mbmi;
760}
Angie Chiang7fcfee42017-02-24 15:51:03 -0800761#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700762
Yaowu Xuf883b422016-08-30 14:01:10 -0700763static void set_offsets_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700764 const TileInfo *const tile, BLOCK_SIZE bsize,
765 int mi_row, int mi_col) {
Jingning Han93531242016-12-20 11:54:36 -0800766 const int bw = mi_size_wide[bsize];
767 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700768 const int offset = mi_row * cm->mi_stride + mi_col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700769
770 xd->mi = cm->mi_grid_visible + offset;
771 xd->mi[0] = cm->mi + offset;
772
Jingning Hanfaad0e12016-12-07 10:54:57 -0800773 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700774
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800775#if CONFIG_DEPENDENT_HORZTILES
776 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
777 cm->dependent_horz_tiles);
778#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700779 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 -0800780#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700781
Yaowu Xuf883b422016-08-30 14:01:10 -0700782 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700783}
784
Yaowu Xuf883b422016-08-30 14:01:10 -0700785static void set_param_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700786 BLOCK_SIZE bsize, int mi_row, int mi_col,
787 int txfm, int skip) {
Jingning Han93531242016-12-20 11:54:36 -0800788 const int bw = mi_size_wide[bsize];
789 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -0700790 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
791 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700792 const int offset = mi_row * cm->mi_stride + mi_col;
793 int x, y;
794
795 xd->mi = cm->mi_grid_visible + offset;
796 xd->mi[0] = cm->mi + offset;
797
798 for (y = 0; y < y_mis; ++y)
799 for (x = 0; x < x_mis; ++x) {
800 xd->mi[y * cm->mi_stride + x]->mbmi.skip = skip;
801 xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
802 }
803#if CONFIG_VAR_TX
804 xd->above_txfm_context = cm->above_txfm_context + mi_col;
805 xd->left_txfm_context =
806 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
Yaowu Xu52a17632016-11-17 15:48:21 -0800807 set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bw, bh, skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700808#endif
809}
810
Yaowu Xuf883b422016-08-30 14:01:10 -0700811static void set_ref(AV1_COMMON *const cm, MACROBLOCKD *const xd, int idx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700812 int mi_row, int mi_col) {
813 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
814 RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
815 xd->block_refs[idx] = ref_buffer;
Yaowu Xuf883b422016-08-30 14:01:10 -0700816 if (!av1_is_valid_scale(&ref_buffer->sf))
817 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700818 "Invalid scale factors");
Yaowu Xuf883b422016-08-30 14:01:10 -0700819 av1_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
820 &ref_buffer->sf);
Angie Chiangd0916d92017-03-10 17:54:18 -0800821 aom_merge_corrupted_flag(&xd->corrupted, ref_buffer->buf->corrupted);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700822}
823
824static void dec_predict_b_extend(
Yaowu Xuf883b422016-08-30 14:01:10 -0700825 AV1Decoder *const pbi, MACROBLOCKD *const xd, const TileInfo *const tile,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700826 int block, int mi_row_ori, int mi_col_ori, int mi_row_pred, int mi_col_pred,
827 int mi_row_top, int mi_col_top, uint8_t *dst_buf[3], int dst_stride[3],
828 BLOCK_SIZE bsize_top, BLOCK_SIZE bsize_pred, int b_sub8x8, int bextend) {
829 // Used in supertx
830 // (mi_row_ori, mi_col_ori): location for mv
831 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
832 // (mi_row_top, mi_col_top, bsize_top): region of the top partition size
833 // block: sub location of sub8x8 blocks
834 // b_sub8x8: 1: ori is sub8x8; 0: ori is not sub8x8
835 // bextend: 1: region to predict is an extension of ori; 0: not
836 int r = (mi_row_pred - mi_row_top) * MI_SIZE;
837 int c = (mi_col_pred - mi_col_top) * MI_SIZE;
Jingning Han93531242016-12-20 11:54:36 -0800838 const int mi_width_top = mi_size_wide[bsize_top];
839 const int mi_height_top = mi_size_high[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700840 MB_MODE_INFO *mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700841 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700842
843 if (mi_row_pred < mi_row_top || mi_col_pred < mi_col_top ||
844 mi_row_pred >= mi_row_top + mi_height_top ||
845 mi_col_pred >= mi_col_top + mi_width_top || mi_row_pred >= cm->mi_rows ||
846 mi_col_pred >= cm->mi_cols)
847 return;
848
849 mbmi = set_offsets_extend(cm, xd, tile, bsize_pred, mi_row_pred, mi_col_pred,
850 mi_row_ori, mi_col_ori);
851 set_ref(cm, xd, 0, mi_row_pred, mi_col_pred);
852 if (has_second_ref(&xd->mi[0]->mbmi))
853 set_ref(cm, xd, 1, mi_row_pred, mi_col_pred);
854
Jingning Han2511c662016-12-22 11:57:34 -0800855 if (!bextend) mbmi->tx_size = max_txsize_lookup[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700856
857 xd->plane[0].dst.stride = dst_stride[0];
858 xd->plane[1].dst.stride = dst_stride[1];
859 xd->plane[2].dst.stride = dst_stride[2];
860 xd->plane[0].dst.buf = dst_buf[0] +
861 (r >> xd->plane[0].subsampling_y) * dst_stride[0] +
862 (c >> xd->plane[0].subsampling_x);
863 xd->plane[1].dst.buf = dst_buf[1] +
864 (r >> xd->plane[1].subsampling_y) * dst_stride[1] +
865 (c >> xd->plane[1].subsampling_x);
866 xd->plane[2].dst.buf = dst_buf[2] +
867 (r >> xd->plane[2].subsampling_y) * dst_stride[2] +
868 (c >> xd->plane[2].subsampling_x);
869
870 if (!b_sub8x8)
Yaowu Xuf883b422016-08-30 14:01:10 -0700871 av1_build_inter_predictors_sb_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700872#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700873 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700874#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700875 mi_row_pred, mi_col_pred, bsize_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700876 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700877 av1_build_inter_predictors_sb_sub8x8_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700878#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700879 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700880#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700881 mi_row_pred, mi_col_pred,
882 bsize_pred, block);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700883}
884
Yaowu Xuf883b422016-08-30 14:01:10 -0700885static void dec_extend_dir(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700886 const TileInfo *const tile, int block,
887 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
888 int mi_col, int mi_row_top, int mi_col_top,
889 uint8_t *dst_buf[3], int dst_stride[3], int dir) {
890 // dir: 0-lower, 1-upper, 2-left, 3-right
891 // 4-lowerleft, 5-upperleft, 6-lowerright, 7-upperright
Jingning Han93531242016-12-20 11:54:36 -0800892 const int mi_width = mi_size_wide[bsize];
893 const int mi_height = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700894 int xss = xd->plane[1].subsampling_x;
895 int yss = xd->plane[1].subsampling_y;
Jingning Hanfeb517c2016-12-21 16:02:07 -0800896#if CONFIG_CB4X4
897 const int unify_bsize = 1;
898#else
899 const int unify_bsize = 0;
900#endif
901 int b_sub8x8 = (bsize < BLOCK_8X8) && !unify_bsize ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700902 BLOCK_SIZE extend_bsize;
Jingning Han24f24a52016-12-27 10:13:28 -0800903 int mi_row_pred, mi_col_pred;
904
905 int wide_unit, high_unit;
906 int i, j;
907 int ext_offset = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700908
909 if (dir == 0 || dir == 1) {
Jingning Han93531242016-12-20 11:54:36 -0800910 extend_bsize =
911 (mi_width == mi_size_wide[BLOCK_8X8] || bsize < BLOCK_8X8 || xss < yss)
912 ? BLOCK_8X8
913 : BLOCK_16X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800914#if CONFIG_CB4X4
915 if (bsize < BLOCK_8X8) {
916 extend_bsize = BLOCK_4X4;
917 ext_offset = mi_size_wide[BLOCK_8X8];
918 }
919#endif
920
921 wide_unit = mi_size_wide[extend_bsize];
922 high_unit = mi_size_high[extend_bsize];
923
924 mi_row_pred = mi_row + ((dir == 0) ? mi_height : -(mi_height + ext_offset));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700925 mi_col_pred = mi_col;
926
Jingning Han24f24a52016-12-27 10:13:28 -0800927 for (j = 0; j < mi_height + ext_offset; j += high_unit)
928 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
929 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
930 mi_row_pred + j, mi_col_pred + i, mi_row_top,
931 mi_col_top, dst_buf, dst_stride, top_bsize,
932 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700933 } else if (dir == 2 || dir == 3) {
Jingning Han5b7706a2016-12-21 09:55:10 -0800934 extend_bsize =
935 (mi_height == mi_size_high[BLOCK_8X8] || bsize < BLOCK_8X8 || yss < xss)
936 ? BLOCK_8X8
937 : BLOCK_8X16;
Jingning Han24f24a52016-12-27 10:13:28 -0800938#if CONFIG_CB4X4
939 if (bsize < BLOCK_8X8) {
940 extend_bsize = BLOCK_4X4;
941 ext_offset = mi_size_wide[BLOCK_8X8];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700942 }
Jingning Han24f24a52016-12-27 10:13:28 -0800943#endif
944
945 wide_unit = mi_size_wide[extend_bsize];
946 high_unit = mi_size_high[extend_bsize];
947
948 mi_row_pred = mi_row;
949 mi_col_pred = mi_col + ((dir == 3) ? mi_width : -(mi_width + ext_offset));
950
951 for (j = 0; j < mi_height + ext_offset; j += high_unit)
952 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
953 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
954 mi_row_pred + j, mi_col_pred + i, mi_row_top,
955 mi_col_top, dst_buf, dst_stride, top_bsize,
956 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700957 } else {
958 extend_bsize = BLOCK_8X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800959#if CONFIG_CB4X4
960 if (bsize < BLOCK_8X8) {
961 extend_bsize = BLOCK_4X4;
962 ext_offset = mi_size_wide[BLOCK_8X8];
963 }
964#endif
965 wide_unit = mi_size_wide[extend_bsize];
966 high_unit = mi_size_high[extend_bsize];
967
Jingning Han5b7706a2016-12-21 09:55:10 -0800968 mi_row_pred = mi_row + ((dir == 4 || dir == 6) ? mi_height
Jingning Han24f24a52016-12-27 10:13:28 -0800969 : -(mi_height + ext_offset));
Jingning Han5b7706a2016-12-21 09:55:10 -0800970 mi_col_pred =
Jingning Han24f24a52016-12-27 10:13:28 -0800971 mi_col + ((dir == 6 || dir == 7) ? mi_width : -(mi_width + ext_offset));
972
973 for (j = 0; j < mi_height + ext_offset; j += high_unit)
974 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
975 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
976 mi_row_pred + j, mi_col_pred + i, mi_row_top,
977 mi_col_top, dst_buf, dst_stride, top_bsize,
978 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700979 }
980}
981
Yaowu Xuf883b422016-08-30 14:01:10 -0700982static void dec_extend_all(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700983 const TileInfo *const tile, int block,
984 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
985 int mi_col, int mi_row_top, int mi_col_top,
986 uint8_t *dst_buf[3], int dst_stride[3]) {
987 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
988 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
989 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
990 mi_row_top, mi_col_top, dst_buf, dst_stride, 1);
991 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
992 mi_row_top, mi_col_top, dst_buf, dst_stride, 2);
993 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
994 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
995 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
996 mi_row_top, mi_col_top, dst_buf, dst_stride, 4);
997 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
998 mi_row_top, mi_col_top, dst_buf, dst_stride, 5);
999 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1000 mi_row_top, mi_col_top, dst_buf, dst_stride, 6);
1001 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1002 mi_row_top, mi_col_top, dst_buf, dst_stride, 7);
1003}
1004
Yaowu Xuf883b422016-08-30 14:01:10 -07001005static void dec_predict_sb_complex(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001006 const TileInfo *const tile, int mi_row,
1007 int mi_col, int mi_row_top, int mi_col_top,
1008 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize,
1009 uint8_t *dst_buf[3], int dst_stride[3]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001010 const AV1_COMMON *const cm = &pbi->common;
Jingning Han5b7706a2016-12-21 09:55:10 -08001011 const int hbs = mi_size_wide[bsize] / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001012 const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
1013 const BLOCK_SIZE subsize = get_subsize(bsize, partition);
1014#if CONFIG_EXT_PARTITION_TYPES
1015 const BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1016#endif
1017 int i;
1018 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1019 uint8_t *dst_buf1[3], *dst_buf2[3], *dst_buf3[3];
Jingning Hanfeb517c2016-12-21 16:02:07 -08001020#if CONFIG_CB4X4
1021 const int unify_bsize = 1;
1022#else
1023 const int unify_bsize = 0;
1024#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001025
1026 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1027 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1028 DECLARE_ALIGNED(16, uint8_t, tmp_buf3[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1029 int dst_stride1[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1030 int dst_stride2[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1031 int dst_stride3[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1032
Yaowu Xuf883b422016-08-30 14:01:10 -07001033#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001034 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1035 int len = sizeof(uint16_t);
1036 dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
1037 dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_TX_SQUARE * len);
1038 dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_TX_SQUARE * len);
1039 dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
1040 dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_TX_SQUARE * len);
1041 dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_TX_SQUARE * len);
1042 dst_buf3[0] = CONVERT_TO_BYTEPTR(tmp_buf3);
1043 dst_buf3[1] = CONVERT_TO_BYTEPTR(tmp_buf3 + MAX_TX_SQUARE * len);
1044 dst_buf3[2] = CONVERT_TO_BYTEPTR(tmp_buf3 + 2 * MAX_TX_SQUARE * len);
1045 } else {
1046#endif
1047 dst_buf1[0] = tmp_buf1;
1048 dst_buf1[1] = tmp_buf1 + MAX_TX_SQUARE;
1049 dst_buf1[2] = tmp_buf1 + 2 * MAX_TX_SQUARE;
1050 dst_buf2[0] = tmp_buf2;
1051 dst_buf2[1] = tmp_buf2 + MAX_TX_SQUARE;
1052 dst_buf2[2] = tmp_buf2 + 2 * MAX_TX_SQUARE;
1053 dst_buf3[0] = tmp_buf3;
1054 dst_buf3[1] = tmp_buf3 + MAX_TX_SQUARE;
1055 dst_buf3[2] = tmp_buf3 + 2 * MAX_TX_SQUARE;
Yaowu Xuf883b422016-08-30 14:01:10 -07001056#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001057 }
1058#endif
1059
1060 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1061
1062 xd->mi = cm->mi_grid_visible + mi_offset;
1063 xd->mi[0] = cm->mi + mi_offset;
1064
1065 for (i = 0; i < MAX_MB_PLANE; i++) {
1066 xd->plane[i].dst.buf = dst_buf[i];
1067 xd->plane[i].dst.stride = dst_stride[i];
1068 }
1069
1070 switch (partition) {
1071 case PARTITION_NONE:
1072 assert(bsize < top_bsize);
1073 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1074 mi_row_top, mi_col_top, dst_buf, dst_stride,
1075 top_bsize, bsize, 0, 0);
1076 dec_extend_all(pbi, xd, tile, 0, bsize, top_bsize, mi_row, mi_col,
1077 mi_row_top, mi_col_top, dst_buf, dst_stride);
1078 break;
1079 case PARTITION_HORZ:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001080 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001081 // For sub8x8, predict in 8x8 unit
1082 // First half
1083 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1084 mi_row_top, mi_col_top, dst_buf, dst_stride,
1085 top_bsize, BLOCK_8X8, 1, 0);
1086 if (bsize < top_bsize)
1087 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1088 mi_row_top, mi_col_top, dst_buf, dst_stride);
1089
1090 // Second half
1091 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1092 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1093 top_bsize, BLOCK_8X8, 1, 1);
1094 if (bsize < top_bsize)
1095 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1096 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1097
1098 // weighted average to smooth the boundary
1099 xd->plane[0].dst.buf = dst_buf[0];
1100 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001101 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001102 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1103 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1104 0);
1105 } else {
1106 // First half
1107 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1108 mi_row_top, mi_col_top, dst_buf, dst_stride,
1109 top_bsize, subsize, 0, 0);
1110 if (bsize < top_bsize)
1111 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1112 mi_row_top, mi_col_top, dst_buf, dst_stride);
1113 else
1114 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1115 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1116
1117 if (mi_row + hbs < cm->mi_rows) {
1118 // Second half
1119 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col,
1120 mi_row + hbs, mi_col, mi_row_top, mi_col_top,
1121 dst_buf1, dst_stride1, top_bsize, subsize, 0, 0);
1122 if (bsize < top_bsize)
1123 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1124 mi_col, mi_row_top, mi_col_top, dst_buf1,
1125 dst_stride1);
1126 else
1127 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1128 mi_col, mi_row_top, mi_col_top, dst_buf1,
1129 dst_stride1, 1);
1130
1131 // weighted average to smooth the boundary
1132 for (i = 0; i < MAX_MB_PLANE; i++) {
1133 xd->plane[i].dst.buf = dst_buf[i];
1134 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001135 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001136 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1137 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1138 PARTITION_HORZ, i);
1139 }
1140 }
1141 }
1142 break;
1143 case PARTITION_VERT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001144 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001145 // First half
1146 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1147 mi_row_top, mi_col_top, dst_buf, dst_stride,
1148 top_bsize, BLOCK_8X8, 1, 0);
1149 if (bsize < top_bsize)
1150 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1151 mi_row_top, mi_col_top, dst_buf, dst_stride);
1152
1153 // Second half
1154 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1155 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1156 top_bsize, BLOCK_8X8, 1, 1);
1157 if (bsize < top_bsize)
1158 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1159 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1160
1161 // Smooth
1162 xd->plane[0].dst.buf = dst_buf[0];
1163 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001164 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001165 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1166 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1167 0);
1168 } else {
1169 // First half
1170 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1171 mi_row_top, mi_col_top, dst_buf, dst_stride,
1172 top_bsize, subsize, 0, 0);
1173 if (bsize < top_bsize)
1174 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1175 mi_row_top, mi_col_top, dst_buf, dst_stride);
1176 else
1177 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1178 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1179
1180 // Second half
1181 if (mi_col + hbs < cm->mi_cols) {
1182 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1183 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1184 dst_stride1, top_bsize, subsize, 0, 0);
1185 if (bsize < top_bsize)
1186 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1187 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1188 dst_stride1);
1189 else
1190 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1191 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1192 dst_stride1, 2);
1193
1194 // Smooth
1195 for (i = 0; i < MAX_MB_PLANE; i++) {
1196 xd->plane[i].dst.buf = dst_buf[i];
1197 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001198 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001199 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1200 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1201 PARTITION_VERT, i);
1202 }
1203 }
1204 }
1205 break;
1206 case PARTITION_SPLIT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001207 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001208 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1209 mi_row_top, mi_col_top, dst_buf, dst_stride,
1210 top_bsize, BLOCK_8X8, 1, 0);
1211 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1212 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1213 top_bsize, BLOCK_8X8, 1, 1);
1214 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1215 mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1216 top_bsize, BLOCK_8X8, 1, 1);
1217 dec_predict_b_extend(pbi, xd, tile, 3, mi_row, mi_col, mi_row, mi_col,
1218 mi_row_top, mi_col_top, dst_buf3, dst_stride3,
1219 top_bsize, BLOCK_8X8, 1, 1);
1220 if (bsize < top_bsize) {
1221 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1222 mi_row_top, mi_col_top, dst_buf, dst_stride);
1223 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1224 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1225 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1226 mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1227 dec_extend_all(pbi, xd, tile, 3, subsize, top_bsize, mi_row, mi_col,
1228 mi_row_top, mi_col_top, dst_buf3, dst_stride3);
1229 }
1230 } else {
1231 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row_top,
1232 mi_col_top, subsize, top_bsize, dst_buf,
1233 dst_stride);
1234 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1235 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col + hbs,
1236 mi_row_top, mi_col_top, subsize, top_bsize,
1237 dst_buf1, dst_stride1);
1238 if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols)
1239 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col,
1240 mi_row_top, mi_col_top, subsize, top_bsize,
1241 dst_buf2, dst_stride2);
1242 if (mi_row + hbs < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1243 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col + hbs,
1244 mi_row_top, mi_col_top, subsize, top_bsize,
1245 dst_buf3, dst_stride3);
1246 }
1247 for (i = 0; i < MAX_MB_PLANE; i++) {
Jingning Han9e0976a2016-12-27 17:52:42 -08001248#if !CONFIG_CB4X4
Jingning Han24f24a52016-12-27 10:13:28 -08001249 if (bsize == BLOCK_8X8 && i != 0)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001250 continue; // Skip <4x4 chroma smoothing
Jingning Han9e0976a2016-12-27 17:52:42 -08001251#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001252 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001253 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001254 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1255 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1256 PARTITION_VERT, i);
1257 if (mi_row + hbs < cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001258 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001259 xd, dst_buf2[i], dst_stride2[i], dst_buf3[i], dst_stride3[i],
1260 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1261 PARTITION_VERT, i);
Yaowu Xuf883b422016-08-30 14:01:10 -07001262 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001263 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1264 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1265 PARTITION_HORZ, i);
1266 }
1267 } else if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001268 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001269 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1270 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1271 PARTITION_HORZ, i);
1272 }
1273 }
1274 break;
1275#if CONFIG_EXT_PARTITION_TYPES
1276 case PARTITION_HORZ_A:
1277 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1278 mi_row_top, mi_col_top, dst_buf, dst_stride,
1279 top_bsize, bsize2, 0, 0);
1280 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1281 mi_row_top, mi_col_top, dst_buf, dst_stride);
1282
1283 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1284 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1285 dst_stride1, top_bsize, bsize2, 0, 0);
1286 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1287 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1288
1289 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1290 mi_col, mi_row_top, mi_col_top, dst_buf2,
1291 dst_stride2, top_bsize, subsize, 0, 0);
1292 if (bsize < top_bsize)
1293 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1294 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1295 else
1296 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1297 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1298 1);
1299
1300 for (i = 0; i < MAX_MB_PLANE; i++) {
1301 xd->plane[i].dst.buf = dst_buf[i];
1302 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001303 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001304 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1305 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1306 i);
1307 }
1308 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001309 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001310 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1311 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1312 i);
1313 }
1314 break;
1315 case PARTITION_VERT_A:
1316
1317 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1318 mi_row_top, mi_col_top, dst_buf, dst_stride,
1319 top_bsize, bsize2, 0, 0);
1320 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1321 mi_row_top, mi_col_top, dst_buf, dst_stride);
1322
1323 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1324 mi_col, mi_row_top, mi_col_top, dst_buf1,
1325 dst_stride1, top_bsize, bsize2, 0, 0);
1326 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1327 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1328
1329 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1330 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1331 dst_stride2, top_bsize, subsize, 0, 0);
1332 if (bsize < top_bsize)
1333 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1334 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1335 dst_stride2);
1336 else
1337 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1338 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1339 dst_stride2, 2);
1340
1341 for (i = 0; i < MAX_MB_PLANE; i++) {
1342 xd->plane[i].dst.buf = dst_buf[i];
1343 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001344 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001345 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1346 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1347 i);
1348 }
1349 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001350 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001351 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1352 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1353 i);
1354 }
1355 break;
1356 case PARTITION_HORZ_B:
1357 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1358 mi_row_top, mi_col_top, dst_buf, dst_stride,
1359 top_bsize, subsize, 0, 0);
1360 if (bsize < top_bsize)
1361 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1362 mi_row_top, mi_col_top, dst_buf, dst_stride);
1363 else
1364 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1365 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1366
1367 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1368 mi_col, mi_row_top, mi_col_top, dst_buf1,
1369 dst_stride1, top_bsize, bsize2, 0, 0);
1370 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1371 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1372
1373 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1374 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1375 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1376 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1377 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1378 dst_stride2);
1379
1380 for (i = 0; i < MAX_MB_PLANE; i++) {
1381 xd->plane[i].dst.buf = dst_buf1[i];
1382 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001383 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001384 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1385 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1386 PARTITION_VERT, i);
1387 }
1388 for (i = 0; i < MAX_MB_PLANE; i++) {
1389 xd->plane[i].dst.buf = dst_buf[i];
1390 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001391 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001392 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1393 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1394 i);
1395 }
1396 break;
1397 case PARTITION_VERT_B:
1398 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1399 mi_row_top, mi_col_top, dst_buf, dst_stride,
1400 top_bsize, subsize, 0, 0);
1401 if (bsize < top_bsize)
1402 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1403 mi_row_top, mi_col_top, dst_buf, dst_stride);
1404 else
1405 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1406 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1407
1408 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1409 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1410 dst_stride1, top_bsize, bsize2, 0, 0);
1411 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1412 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1413
1414 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1415 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1416 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1417 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1418 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1419 dst_stride2);
1420
1421 for (i = 0; i < MAX_MB_PLANE; i++) {
1422 xd->plane[i].dst.buf = dst_buf1[i];
1423 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001424 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001425 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1426 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1427 PARTITION_HORZ, i);
1428 }
1429 for (i = 0; i < MAX_MB_PLANE; i++) {
1430 xd->plane[i].dst.buf = dst_buf[i];
1431 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001432 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001433 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1434 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1435 i);
1436 }
1437 break;
1438#endif // CONFIG_EXT_PARTITION_TYPES
1439 default: assert(0);
1440 }
1441}
1442
Yaowu Xuf883b422016-08-30 14:01:10 -07001443static void set_segment_id_supertx(const AV1_COMMON *const cm, const int mi_row,
1444 const int mi_col, const BLOCK_SIZE bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001445 const struct segmentation *seg = &cm->seg;
Jingning Han5b7706a2016-12-21 09:55:10 -08001446 const int miw = AOMMIN(mi_size_wide[bsize], cm->mi_cols - mi_col);
1447 const int mih = AOMMIN(mi_size_high[bsize], cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001448 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1449 MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
1450 int r, c;
1451 int seg_id_supertx = MAX_SEGMENTS;
1452
1453 if (!seg->enabled) {
1454 seg_id_supertx = 0;
1455 } else {
1456 // Find the minimum segment_id
1457 for (r = 0; r < mih; r++)
1458 for (c = 0; c < miw; c++)
1459 seg_id_supertx =
Yaowu Xuf883b422016-08-30 14:01:10 -07001460 AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001461 assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);
1462 }
1463
1464 // Assign the the segment_id back to segment_id_supertx
1465 for (r = 0; r < mih; r++)
1466 for (c = 0; c < miw; c++)
1467 mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
1468}
1469#endif // CONFIG_SUPERTX
1470
Yue Chen64550b62017-01-12 12:18:22 -08001471static void decode_mbmi_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001472#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001473 int supertx_enabled,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001474#endif // CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001475 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001476#if CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001477 PARTITION_TYPE partition,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001478#endif // CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001479 BLOCK_SIZE bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001480 AV1_COMMON *const cm = &pbi->common;
Jingning Han85dc03f2016-12-06 16:03:10 -08001481 const int bw = mi_size_wide[bsize];
1482 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -07001483 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1484 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Nathan E. Eggeebbd4792016-10-05 19:30:15 -04001485
Michael Bebenita6048d052016-08-25 14:40:54 -07001486#if CONFIG_ACCOUNTING
1487 aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
1488#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001489#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001490 if (supertx_enabled) {
Yue Chen64550b62017-01-12 12:18:22 -08001491 set_mb_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001492 } else {
Yue Chen64550b62017-01-12 12:18:22 -08001493 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001494 }
1495#if CONFIG_EXT_PARTITION_TYPES
1496 xd->mi[0]->mbmi.partition = partition;
1497#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001498 av1_read_mode_info(pbi, xd, supertx_enabled, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001499#else
Yue Chen64550b62017-01-12 12:18:22 -08001500 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001501#if CONFIG_EXT_PARTITION_TYPES
1502 xd->mi[0]->mbmi.partition = partition;
1503#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001504 av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001505#endif // CONFIG_SUPERTX
1506
1507 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1508 const BLOCK_SIZE uv_subsize =
1509 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1510 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -07001511 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001512 "Invalid block size.");
1513 }
1514
1515#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001516 xd->mi[0]->mbmi.segment_id_supertx = MAX_SEGMENTS;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001517#endif // CONFIG_SUPERTX
1518
Angie Chiangd0916d92017-03-10 17:54:18 -08001519 int reader_corrupted_flag = aom_reader_has_error(r);
1520 aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
Yue Chen64550b62017-01-12 12:18:22 -08001521}
1522
1523static void decode_token_and_recon_block(AV1Decoder *const pbi,
1524 MACROBLOCKD *const xd, int mi_row,
1525 int mi_col, aom_reader *r,
1526 BLOCK_SIZE bsize) {
1527 AV1_COMMON *const cm = &pbi->common;
1528 const int bw = mi_size_wide[bsize];
1529 const int bh = mi_size_high[bsize];
1530 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1531 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yue Chen64550b62017-01-12 12:18:22 -08001532
Angie Chiang44701f22017-02-27 10:36:44 -08001533 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
1534 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Yue Chen19e7aa82016-11-30 14:05:39 -08001535
Arild Fuldseth07441162016-08-15 15:07:52 +02001536#if CONFIG_DELTA_Q
1537 if (cm->delta_q_present_flag) {
1538 int i;
1539 for (i = 0; i < MAX_SEGMENTS; i++) {
1540 xd->plane[0].seg_dequant[i][0] =
1541 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
1542 xd->plane[0].seg_dequant[i][1] =
1543 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
1544 xd->plane[1].seg_dequant[i][0] =
1545 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1546 xd->plane[1].seg_dequant[i][1] =
1547 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1548 xd->plane[2].seg_dequant[i][0] =
1549 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1550 xd->plane[2].seg_dequant[i][1] =
1551 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1552 }
1553 }
1554#endif
1555
Jingning Han41bb3392016-12-14 10:46:48 -08001556#if CONFIG_CB4X4
1557 if (mbmi->skip) reset_skip_context(xd, bsize);
1558#else
Jingning Hand39cc722016-12-02 14:03:26 -08001559 if (mbmi->skip) reset_skip_context(xd, AOMMAX(BLOCK_8X8, bsize));
Jingning Han41bb3392016-12-14 10:46:48 -08001560#endif
Jingning Hand39cc722016-12-02 14:03:26 -08001561
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001562#if CONFIG_COEF_INTERLEAVE
1563 {
1564 const struct macroblockd_plane *const pd_y = &xd->plane[0];
1565 const struct macroblockd_plane *const pd_c = &xd->plane[1];
1566 const TX_SIZE tx_log2_y = mbmi->tx_size;
1567 const TX_SIZE tx_log2_c = get_uv_tx_size(mbmi, pd_c);
1568 const int tx_sz_y = (1 << tx_log2_y);
1569 const int tx_sz_c = (1 << tx_log2_c);
1570 const int num_4x4_w_y = pd_y->n4_w;
1571 const int num_4x4_h_y = pd_y->n4_h;
1572 const int num_4x4_w_c = pd_c->n4_w;
1573 const int num_4x4_h_c = pd_c->n4_h;
1574 const int max_4x4_w_y = get_max_4x4_size(num_4x4_w_y, xd->mb_to_right_edge,
1575 pd_y->subsampling_x);
1576 const int max_4x4_h_y = get_max_4x4_size(num_4x4_h_y, xd->mb_to_bottom_edge,
1577 pd_y->subsampling_y);
1578 const int max_4x4_w_c = get_max_4x4_size(num_4x4_w_c, xd->mb_to_right_edge,
1579 pd_c->subsampling_x);
1580 const int max_4x4_h_c = get_max_4x4_size(num_4x4_h_c, xd->mb_to_bottom_edge,
1581 pd_c->subsampling_y);
1582
1583 // The max_4x4_w/h may be smaller than tx_sz under some corner cases,
1584 // i.e. when the SB is splitted by tile boundaries.
1585 const int tu_num_w_y = (max_4x4_w_y + tx_sz_y - 1) / tx_sz_y;
1586 const int tu_num_h_y = (max_4x4_h_y + tx_sz_y - 1) / tx_sz_y;
1587 const int tu_num_w_c = (max_4x4_w_c + tx_sz_c - 1) / tx_sz_c;
1588 const int tu_num_h_c = (max_4x4_h_c + tx_sz_c - 1) / tx_sz_c;
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001589 const int tu_num_c = tu_num_w_c * tu_num_h_c;
1590
1591 if (!is_inter_block(mbmi)) {
1592 int tu_idx_c = 0;
1593 int row_y, col_y, row_c, col_c;
1594 int plane;
1595
1596#if CONFIG_PALETTE
1597 for (plane = 0; plane <= 1; ++plane) {
1598 if (mbmi->palette_mode_info.palette_size[plane])
1599 av1_decode_palette_tokens(xd, plane, r);
1600 }
1601#endif
1602
1603 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1604 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1605 // luma
1606 predict_and_reconstruct_intra_block(
1607 cm, xd, r, mbmi, 0, row_y * tx_sz_y, col_y * tx_sz_y, tx_log2_y);
1608 // chroma
1609 if (tu_idx_c < tu_num_c) {
1610 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1611 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1612 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c,
1613 col_c, tx_log2_c);
1614 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c,
1615 col_c, tx_log2_c);
1616 tu_idx_c++;
1617 }
1618 }
1619 }
1620
1621 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1622 while (tu_idx_c < tu_num_c) {
1623 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1624 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1625 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c, col_c,
1626 tx_log2_c);
1627 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c, col_c,
1628 tx_log2_c);
1629 tu_idx_c++;
1630 }
1631 } else {
1632 // Prediction
David Barkerac37fa32016-12-02 12:30:21 +00001633 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001634 AOMMAX(bsize, BLOCK_8X8));
1635
1636 // Reconstruction
1637 if (!mbmi->skip) {
1638 int eobtotal = 0;
1639 int tu_idx_c = 0;
1640 int row_y, col_y, row_c, col_c;
1641
1642 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1643 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1644 // luma
1645 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 0,
1646 row_y * tx_sz_y,
1647 col_y * tx_sz_y, tx_log2_y);
1648 // chroma
1649 if (tu_idx_c < tu_num_c) {
1650 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1651 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1652 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1653 1, row_c, col_c, tx_log2_c);
1654 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1655 2, row_c, col_c, tx_log2_c);
1656 tu_idx_c++;
1657 }
1658 }
1659 }
1660
1661 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1662 while (tu_idx_c < tu_num_c) {
1663 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1664 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1665 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 1,
1666 row_c, col_c, tx_log2_c);
1667 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 2,
1668 row_c, col_c, tx_log2_c);
1669 tu_idx_c++;
1670 }
1671
Alex Converse64d7ef62017-03-22 18:09:16 -07001672 // TODO(CONFIG_COEF_INTERLEAVE owners): bring eob == 0 corner case
1673 // into line with the defaut configuration
1674 if (bsize >= BLOCK_8X8 && eobtotal == 0) mbmi->skip = 1;
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001675 }
1676 }
1677 }
Angie Chiang133733c2017-03-17 12:50:20 -07001678#else // CONFIG_COEF_INTERLEAVE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001679 if (!is_inter_block(mbmi)) {
1680 int plane;
Fangwen Fub3be9262017-03-06 15:34:28 -08001681#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001682 for (plane = 0; plane <= 1; ++plane) {
1683 if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xuf883b422016-08-30 14:01:10 -07001684 av1_decode_palette_tokens(xd, plane, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001685 }
Fangwen Fub3be9262017-03-06 15:34:28 -08001686#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001687 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1688 const struct macroblockd_plane *const pd = &xd->plane[plane];
Angie Chiang7fcfee42017-02-24 15:51:03 -08001689 const TX_SIZE tx_size = get_tx_size(plane, xd);
Jingning Han2d64f122016-10-21 12:44:29 -07001690 const int stepr = tx_size_high_unit[tx_size];
1691 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han41bb3392016-12-14 10:46:48 -08001692#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001693#if CONFIG_CHROMA_2X2
1694 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1695#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001696 const BLOCK_SIZE plane_bsize =
1697 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001698#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001699#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001700 const BLOCK_SIZE plane_bsize =
1701 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001702#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001703 int row, col;
Jingning Hanbafee8d2016-12-02 10:25:03 -08001704 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1705 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001706#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001707 if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
1708 pd->subsampling_y))
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001709 continue;
1710#endif
1711
Yaowu Xuc27fc142016-08-22 16:08:15 -07001712 for (row = 0; row < max_blocks_high; row += stepr)
1713 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001714 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane, row, col,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001715 tx_size);
1716 }
1717 } else {
Yue Chen9ab6d712017-01-12 15:50:46 -08001718 int ref;
1719
1720 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
1721 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
1722 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
1723
1724 xd->block_refs[ref] = ref_buf;
1725 if ((!av1_is_valid_scale(&ref_buf->sf)))
1726 aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
1727 "Reference frame has invalid dimensions");
1728 av1_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col, &ref_buf->sf);
1729 }
Yue Chen69f18e12016-09-08 14:48:15 -07001730#if CONFIG_WARPED_MOTION
1731 if (mbmi->motion_mode == WARPED_CAUSAL) {
1732 int i;
Sarah Parker19234cc2017-03-10 16:43:25 -08001733 assert_motion_mode_valid(WARPED_CAUSAL,
1734#if CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1735 0, cm->global_motion,
1736#endif // CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1737 xd->mi[0]);
Yue Chen69f18e12016-09-08 14:48:15 -07001738 for (i = 0; i < 3; ++i) {
1739 const struct macroblockd_plane *pd = &xd->plane[i];
1740
1741 av1_warp_plane(&mbmi->wm_params[0],
1742#if CONFIG_AOM_HIGHBITDEPTH
1743 xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH, xd->bd,
1744#endif // CONFIG_AOM_HIGHBITDEPTH
1745 pd->pre[0].buf0, pd->pre[0].width, pd->pre[0].height,
1746 pd->pre[0].stride, pd->dst.buf,
1747 ((mi_col * MI_SIZE) >> pd->subsampling_x),
1748 ((mi_row * MI_SIZE) >> pd->subsampling_y),
Jingning Hanff6ee6a2016-12-07 09:55:21 -08001749 xd->n8_w * (MI_SIZE >> pd->subsampling_x),
1750 xd->n8_h * (MI_SIZE >> pd->subsampling_y),
1751 pd->dst.stride, pd->subsampling_x, pd->subsampling_y, 16,
1752 16, 0);
Yue Chen69f18e12016-09-08 14:48:15 -07001753 }
1754 } else {
1755#endif // CONFIG_WARPED_MOTION
Jingning Han41bb3392016-12-14 10:46:48 -08001756#if CONFIG_CB4X4
1757 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL, bsize);
1758#else
1759 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
1760 AOMMAX(bsize, BLOCK_8X8));
1761#endif
Yue Chen69f18e12016-09-08 14:48:15 -07001762#if CONFIG_WARPED_MOTION
1763 }
1764#endif // CONFIG_WARPED_MOTION
Yue Chencb60b182016-10-13 15:18:22 -07001765#if CONFIG_MOTION_VAR
1766 if (mbmi->motion_mode == OBMC_CAUSAL) {
Sarah Parker19234cc2017-03-10 16:43:25 -08001767 assert_motion_mode_valid(OBMC_CAUSAL,
1768#if CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1769 0, cm->global_motion,
1770#endif // CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1771 xd->mi[0]);
Yue Chenf27b1602017-01-13 11:11:43 -08001772#if CONFIG_NCOBMC
1773 av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
1774#else
Yue Chen894fcce2016-10-21 16:50:52 -07001775 av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yue Chenf27b1602017-01-13 11:11:43 -08001776#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001777 }
Yue Chencb60b182016-10-13 15:18:22 -07001778#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001779
1780 // Reconstruction
1781 if (!mbmi->skip) {
1782 int eobtotal = 0;
1783 int plane;
1784
1785 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1786 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han41bb3392016-12-14 10:46:48 -08001787#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001788#if CONFIG_CHROMA_2X2
1789 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1790#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001791 const BLOCK_SIZE plane_bsize =
1792 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001793#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001794#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001795 const BLOCK_SIZE plane_bsize =
1796 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001797#endif
Jingning Hanbafee8d2016-12-02 10:25:03 -08001798 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1799 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001800 int row, col;
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001801
1802#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001803 if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
1804 pd->subsampling_y))
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001805 continue;
1806#endif
1807
Yaowu Xuc27fc142016-08-22 16:08:15 -07001808#if CONFIG_VAR_TX
Jingning Han70e5f3f2016-11-09 17:03:07 -08001809 const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07001810 const int bh_var_tx = tx_size_high_unit[max_tx_size];
1811 const int bw_var_tx = tx_size_wide_unit[max_tx_size];
Jingning Hanbafee8d2016-12-02 10:25:03 -08001812 for (row = 0; row < max_blocks_high; row += bh_var_tx)
1813 for (col = 0; col < max_blocks_wide; col += bw_var_tx)
Jingning Hanfe45b212016-11-22 10:30:23 -08001814 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, row, col,
1815 max_tx_size, &eobtotal);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001816#else
Angie Chiang7fcfee42017-02-24 15:51:03 -08001817 const TX_SIZE tx_size = get_tx_size(plane, xd);
Jingning Han2d64f122016-10-21 12:44:29 -07001818 const int stepr = tx_size_high_unit[tx_size];
1819 const int stepc = tx_size_wide_unit[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001820 for (row = 0; row < max_blocks_high; row += stepr)
1821 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001822 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1823 plane, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001824#endif
1825 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001826 }
1827 }
Angie Chiang133733c2017-03-17 12:50:20 -07001828#endif // CONFIG_COEF_INTERLEAVE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001829
Angie Chiangd0916d92017-03-10 17:54:18 -08001830 int reader_corrupted_flag = aom_reader_has_error(r);
1831 aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001832}
1833
Yue Chen9ab6d712017-01-12 15:50:46 -08001834#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
1835static void detoken_and_recon_sb(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1836 int mi_row, int mi_col, aom_reader *r,
1837 BLOCK_SIZE bsize) {
1838 AV1_COMMON *const cm = &pbi->common;
1839 const int hbs = mi_size_wide[bsize] >> 1;
1840#if CONFIG_CB4X4
1841 const int unify_bsize = 1;
1842#else
1843 const int unify_bsize = 0;
1844#endif
1845#if CONFIG_EXT_PARTITION_TYPES
1846 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1847#endif
1848 PARTITION_TYPE partition;
1849 BLOCK_SIZE subsize;
1850 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1851 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1852
1853 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1854
1855 partition = get_partition(cm, mi_row, mi_col, bsize);
1856 subsize = subsize_lookup[partition][bsize];
1857
1858 if (!hbs && !unify_bsize) {
1859 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1860 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1861 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1862 } else {
1863 switch (partition) {
1864 case PARTITION_NONE:
1865 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
1866 break;
1867 case PARTITION_HORZ:
1868 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1869 if (has_rows)
1870 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r,
1871 subsize);
1872 break;
1873 case PARTITION_VERT:
1874 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1875 if (has_cols)
1876 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r,
1877 subsize);
1878 break;
1879 case PARTITION_SPLIT:
1880 detoken_and_recon_sb(pbi, xd, mi_row, mi_col, r, subsize);
1881 detoken_and_recon_sb(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1882 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1883 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col + hbs, r, subsize);
1884 break;
1885#if CONFIG_EXT_PARTITION_TYPES
1886 case PARTITION_HORZ_A:
1887 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1888 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1889 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1890 break;
1891 case PARTITION_HORZ_B:
1892 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1893 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1894 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1895 bsize2);
1896 break;
1897 case PARTITION_VERT_A:
1898 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1899 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1900 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1901 break;
1902 case PARTITION_VERT_B:
1903 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1904 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1905 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1906 bsize2);
1907 break;
1908#endif
1909 default: assert(0 && "Invalid partition type");
1910 }
1911 }
1912}
1913#endif
1914
Yue Chen64550b62017-01-12 12:18:22 -08001915static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1916#if CONFIG_SUPERTX
1917 int supertx_enabled,
1918#endif // CONFIG_SUPERTX
1919 int mi_row, int mi_col, aom_reader *r,
1920#if CONFIG_EXT_PARTITION_TYPES
1921 PARTITION_TYPE partition,
1922#endif // CONFIG_EXT_PARTITION_TYPES
1923 BLOCK_SIZE bsize) {
1924 decode_mbmi_block(pbi, xd,
1925#if CONFIG_SUPERTX
1926 supertx_enabled,
1927#endif
1928 mi_row, mi_col, r,
1929#if CONFIG_EXT_PARTITION_TYPES
1930 partition,
1931#endif
1932 bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001933#if !(CONFIG_MOTION_VAR && CONFIG_NCOBMC)
Yue Chen64550b62017-01-12 12:18:22 -08001934#if CONFIG_SUPERTX
1935 if (!supertx_enabled)
1936#endif // CONFIG_SUPERTX
1937 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001938#endif
Yue Chen64550b62017-01-12 12:18:22 -08001939}
1940
Yaowu Xuf883b422016-08-30 14:01:10 -07001941static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
1942 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001943 int has_rows, int has_cols,
Jingning Han1beb0102016-12-07 11:08:30 -08001944 BLOCK_SIZE bsize) {
Alex Converse55c6bde2017-01-12 15:55:31 -08001945#if CONFIG_UNPOISON_PARTITION_CTX
1946 const int ctx =
1947 partition_plane_context(xd, mi_row, mi_col, has_rows, has_cols, bsize);
Alex Converse2b9d19d2017-04-03 11:11:17 -07001948 const aom_prob *const probs =
1949 ctx < PARTITION_CONTEXTS ? cm->fc->partition_prob[ctx] : NULL;
1950 FRAME_COUNTS *const counts = ctx < PARTITION_CONTEXTS ? xd->counts : NULL;
Alex Converse55c6bde2017-01-12 15:55:31 -08001951#else
Jingning Han1beb0102016-12-07 11:08:30 -08001952 const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
Yaowu Xuf883b422016-08-30 14:01:10 -07001953 const aom_prob *const probs = cm->fc->partition_prob[ctx];
Alex Converse55c6bde2017-01-12 15:55:31 -08001954 FRAME_COUNTS *const counts = xd->counts;
1955#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001956 PARTITION_TYPE p;
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001957#if CONFIG_EC_ADAPT
1958 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1959 (void)cm;
1960#elif CONFIG_EC_MULTISYMBOL
1961 FRAME_CONTEXT *ec_ctx = cm->fc;
1962#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001963
Jingning Han5fe79db2017-03-27 15:10:30 -07001964#if CONFIG_EC_MULTISYMBOL
1965 aom_cdf_prob *partition_cdf = (ctx >= 0) ? ec_ctx->partition_cdf[ctx] : NULL;
1966#endif
1967
Yaowu Xuc27fc142016-08-22 16:08:15 -07001968 if (has_rows && has_cols)
1969#if CONFIG_EXT_PARTITION_TYPES
1970 if (bsize <= BLOCK_8X8)
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001971#if CONFIG_EC_MULTISYMBOL
Jingning Han5fe79db2017-03-27 15:10:30 -07001972 p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, PARTITION_TYPES,
1973 ACCT_STR);
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001974#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001975 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001976#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001977 else
Alex Converse57795a42017-03-14 12:18:25 -07001978#if CONFIG_EC_MULTISYMBOL
Jingning Han5fe79db2017-03-27 15:10:30 -07001979 p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, EXT_PARTITION_TYPES,
1980 ACCT_STR);
Alex Converse57795a42017-03-14 12:18:25 -07001981#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001982 p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs,
1983 ACCT_STR);
Alex Converse57795a42017-03-14 12:18:25 -07001984#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001985#else
Nathan E. Egge9d9eb6c2016-11-16 09:44:26 -05001986#if CONFIG_EC_MULTISYMBOL
Jingning Han5fe79db2017-03-27 15:10:30 -07001987 p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, PARTITION_TYPES,
1988 ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001989#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001990 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001991#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001992#endif // CONFIG_EXT_PARTITION_TYPES
1993 else if (!has_rows && has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001994 p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001995 else if (has_rows && !has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001996 p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001997 else
1998 p = PARTITION_SPLIT;
1999
2000 if (counts) ++counts->partition[ctx][p];
2001
2002 return p;
2003}
2004
2005#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002006static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
2007 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002008 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
2009 return 1;
2010 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002011 const int ctx = av1_get_skip_context(xd);
Michael Bebenita6048d052016-08-25 14:40:54 -07002012 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002013 FRAME_COUNTS *counts = xd->counts;
2014 if (counts) ++counts->skip[ctx][skip];
2015 return skip;
2016 }
2017}
2018#endif // CONFIG_SUPERTX
2019
2020// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -07002021static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002022#if CONFIG_SUPERTX
2023 int supertx_enabled,
2024#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002025 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002026 BLOCK_SIZE bsize, int n4x4_l2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002027 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002028 const int n8x8_l2 = n4x4_l2 - 1;
Jingning Hanff17e162016-12-07 17:58:18 -08002029 const int num_8x8_wh = mi_size_wide[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002030 const int hbs = num_8x8_wh >> 1;
Jingning Han41bb3392016-12-14 10:46:48 -08002031#if CONFIG_CB4X4
2032 const int unify_bsize = 1;
2033#else
2034 const int unify_bsize = 0;
2035#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002036 PARTITION_TYPE partition;
2037 BLOCK_SIZE subsize;
2038#if CONFIG_EXT_PARTITION_TYPES
2039 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
2040#endif
2041 const int has_rows = (mi_row + hbs) < cm->mi_rows;
2042 const int has_cols = (mi_col + hbs) < cm->mi_cols;
2043#if CONFIG_SUPERTX
2044 const int read_token = !supertx_enabled;
2045 int skip = 0;
Jingning Han2511c662016-12-22 11:57:34 -08002046 TX_SIZE supertx_size = max_txsize_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002047 const TileInfo *const tile = &xd->tile;
2048 int txfm = DCT_DCT;
2049#endif // CONFIG_SUPERTX
2050
2051 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
2052
Jingning Hancd959762017-03-27 14:49:59 -07002053 partition = (bsize < BLOCK_8X8) ? PARTITION_NONE
2054 : read_partition(cm, xd, mi_row, mi_col, r,
2055 has_rows, has_cols, bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002056 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
Yushin Cho77bba8d2016-11-04 16:36:56 -07002057
2058#if CONFIG_PVQ
2059 assert(partition < PARTITION_TYPES);
2060 assert(subsize < BLOCK_SIZES);
2061#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002062#if CONFIG_SUPERTX
2063 if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
2064 bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
2065 const int supertx_context = partition_supertx_context_lookup[partition];
Michael Bebenita6048d052016-08-25 14:40:54 -07002066 supertx_enabled = aom_read(
2067 r, cm->fc->supertx_prob[supertx_context][supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002068 if (xd->counts)
2069 xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
2070#if CONFIG_VAR_TX
2071 if (supertx_enabled) xd->supertx_size = supertx_size;
2072#endif
2073 }
2074#endif // CONFIG_SUPERTX
Jingning Han41bb3392016-12-14 10:46:48 -08002075 if (!hbs && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002076 // calculate bmode block dimensions (log 2)
2077 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
2078 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
2079 decode_block(pbi, xd,
2080#if CONFIG_SUPERTX
2081 supertx_enabled,
2082#endif // CONFIG_SUPERTX
2083 mi_row, mi_col, r,
2084#if CONFIG_EXT_PARTITION_TYPES
2085 partition,
2086#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002087 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002088 } else {
2089 switch (partition) {
2090 case PARTITION_NONE:
2091 decode_block(pbi, xd,
2092#if CONFIG_SUPERTX
2093 supertx_enabled,
2094#endif // CONFIG_SUPERTX
2095 mi_row, mi_col, r,
2096#if CONFIG_EXT_PARTITION_TYPES
2097 partition,
2098#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002099 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002100 break;
2101 case PARTITION_HORZ:
2102 decode_block(pbi, xd,
2103#if CONFIG_SUPERTX
2104 supertx_enabled,
2105#endif // CONFIG_SUPERTX
2106 mi_row, mi_col, 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 if (has_rows)
2112 decode_block(pbi, xd,
2113#if CONFIG_SUPERTX
2114 supertx_enabled,
2115#endif // CONFIG_SUPERTX
2116 mi_row + hbs, mi_col, r,
2117#if CONFIG_EXT_PARTITION_TYPES
2118 partition,
2119#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002120 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002121 break;
2122 case PARTITION_VERT:
2123 decode_block(pbi, xd,
2124#if CONFIG_SUPERTX
2125 supertx_enabled,
2126#endif // CONFIG_SUPERTX
2127 mi_row, mi_col, r,
2128#if CONFIG_EXT_PARTITION_TYPES
2129 partition,
2130#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002131 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002132 if (has_cols)
2133 decode_block(pbi, xd,
2134#if CONFIG_SUPERTX
2135 supertx_enabled,
2136#endif // CONFIG_SUPERTX
2137 mi_row, mi_col + hbs, r,
2138#if CONFIG_EXT_PARTITION_TYPES
2139 partition,
2140#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002141 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002142 break;
2143 case PARTITION_SPLIT:
2144 decode_partition(pbi, xd,
2145#if CONFIG_SUPERTX
2146 supertx_enabled,
2147#endif // CONFIG_SUPERTX
2148 mi_row, mi_col, r, subsize, n8x8_l2);
2149 decode_partition(pbi, xd,
2150#if CONFIG_SUPERTX
2151 supertx_enabled,
2152#endif // CONFIG_SUPERTX
2153 mi_row, mi_col + hbs, r, subsize, n8x8_l2);
2154 decode_partition(pbi, xd,
2155#if CONFIG_SUPERTX
2156 supertx_enabled,
2157#endif // CONFIG_SUPERTX
2158 mi_row + hbs, mi_col, r, subsize, n8x8_l2);
2159 decode_partition(pbi, xd,
2160#if CONFIG_SUPERTX
2161 supertx_enabled,
2162#endif // CONFIG_SUPERTX
2163 mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
2164 break;
2165#if CONFIG_EXT_PARTITION_TYPES
2166 case PARTITION_HORZ_A:
2167 decode_block(pbi, xd,
2168#if CONFIG_SUPERTX
2169 supertx_enabled,
2170#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002171 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002172 decode_block(pbi, xd,
2173#if CONFIG_SUPERTX
2174 supertx_enabled,
2175#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002176 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002177 decode_block(pbi, xd,
2178#if CONFIG_SUPERTX
2179 supertx_enabled,
2180#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002181 mi_row + hbs, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002182 break;
2183 case PARTITION_HORZ_B:
2184 decode_block(pbi, xd,
2185#if CONFIG_SUPERTX
2186 supertx_enabled,
2187#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002188 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002189 decode_block(pbi, xd,
2190#if CONFIG_SUPERTX
2191 supertx_enabled,
2192#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002193 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002194 decode_block(pbi, xd,
2195#if CONFIG_SUPERTX
2196 supertx_enabled,
2197#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002198 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002199 break;
2200 case PARTITION_VERT_A:
2201 decode_block(pbi, xd,
2202#if CONFIG_SUPERTX
2203 supertx_enabled,
2204#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002205 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002206 decode_block(pbi, xd,
2207#if CONFIG_SUPERTX
2208 supertx_enabled,
2209#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002210 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002211 decode_block(pbi, xd,
2212#if CONFIG_SUPERTX
2213 supertx_enabled,
2214#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002215 mi_row, mi_col + hbs, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002216 break;
2217 case PARTITION_VERT_B:
2218 decode_block(pbi, xd,
2219#if CONFIG_SUPERTX
2220 supertx_enabled,
2221#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002222 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002223 decode_block(pbi, xd,
2224#if CONFIG_SUPERTX
2225 supertx_enabled,
2226#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002227 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002228 decode_block(pbi, xd,
2229#if CONFIG_SUPERTX
2230 supertx_enabled,
2231#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002232 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002233 break;
2234#endif
2235 default: assert(0 && "Invalid partition type");
2236 }
2237 }
2238
2239#if CONFIG_SUPERTX
2240 if (supertx_enabled && read_token) {
2241 uint8_t *dst_buf[3];
2242 int dst_stride[3], i;
2243 int offset = mi_row * cm->mi_stride + mi_col;
2244
2245 set_segment_id_supertx(cm, mi_row, mi_col, bsize);
2246
David Barker3aec8d62017-01-31 14:55:32 +00002247#if CONFIG_DELTA_Q
2248 if (cm->delta_q_present_flag) {
2249 for (i = 0; i < MAX_SEGMENTS; i++) {
2250 xd->plane[0].seg_dequant[i][0] =
2251 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
2252 xd->plane[0].seg_dequant[i][1] =
2253 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
2254 xd->plane[1].seg_dequant[i][0] =
2255 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2256 xd->plane[1].seg_dequant[i][1] =
2257 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2258 xd->plane[2].seg_dequant[i][0] =
2259 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2260 xd->plane[2].seg_dequant[i][1] =
2261 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2262 }
2263 }
2264#endif
2265
Yaowu Xuc27fc142016-08-22 16:08:15 -07002266 xd->mi = cm->mi_grid_visible + offset;
2267 xd->mi[0] = cm->mi + offset;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002268#if CONFIG_DEPENDENT_HORZTILES
2269 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2270 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols,
2271 cm->dependent_horz_tiles);
2272#else
Jingning Han5b7706a2016-12-21 09:55:10 -08002273 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2274 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002275#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002276 set_skip_context(xd, mi_row, mi_col);
2277 skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
2278 if (skip) {
2279 reset_skip_context(xd, bsize);
2280 } else {
2281#if CONFIG_EXT_TX
Sarah Parkere68a3e42017-02-16 14:03:24 -08002282 if (get_ext_tx_types(supertx_size, bsize, 1, cm->reduced_tx_set_used) >
2283 1) {
2284 const int eset =
2285 get_ext_tx_set(supertx_size, bsize, 1, cm->reduced_tx_set_used);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002286 if (eset > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002287 txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
Michael Bebenita6048d052016-08-25 14:40:54 -07002288 cm->fc->inter_ext_tx_prob[eset][supertx_size],
2289 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002290 if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
2291 }
2292 }
2293#else
2294 if (supertx_size < TX_32X32) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002295 txfm = aom_read_tree(r, av1_ext_tx_tree,
Jingning Han8f6eb182016-10-19 13:48:57 -07002296 cm->fc->inter_ext_tx_prob[supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002297 if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
2298 }
2299#endif // CONFIG_EXT_TX
2300 }
2301
Yaowu Xuf883b422016-08-30 14:01:10 -07002302 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002303 for (i = 0; i < MAX_MB_PLANE; i++) {
2304 dst_buf[i] = xd->plane[i].dst.buf;
2305 dst_stride[i] = xd->plane[i].dst.stride;
2306 }
2307 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
2308 bsize, dst_buf, dst_stride);
2309
2310 if (!skip) {
2311 int eobtotal = 0;
2312 MB_MODE_INFO *mbmi;
2313 set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
2314 mbmi = &xd->mi[0]->mbmi;
2315 mbmi->tx_type = txfm;
2316 assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
2317 for (i = 0; i < MAX_MB_PLANE; ++i) {
2318 const struct macroblockd_plane *const pd = &xd->plane[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002319 int row, col;
Angie Chiang7fcfee42017-02-24 15:51:03 -08002320 const TX_SIZE tx_size = get_tx_size(i, xd);
Jingning Han5b7706a2016-12-21 09:55:10 -08002321 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Jingning Han32b20282016-10-28 15:42:44 -07002322 const int stepr = tx_size_high_unit[tx_size];
2323 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han5b7706a2016-12-21 09:55:10 -08002324 const int max_blocks_wide = max_block_wide(xd, plane_bsize, i);
2325 const int max_blocks_high = max_block_high(xd, plane_bsize, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002326
2327 for (row = 0; row < max_blocks_high; row += stepr)
2328 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07002329 eobtotal += reconstruct_inter_block(
2330 cm, xd, r, mbmi->segment_id_supertx, i, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002331 }
Jingning Han24f24a52016-12-27 10:13:28 -08002332 if ((unify_bsize || !(subsize < BLOCK_8X8)) && eobtotal == 0) skip = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002333 }
2334 set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
2335 }
2336#endif // CONFIG_SUPERTX
2337
2338#if CONFIG_EXT_PARTITION_TYPES
Alex Converseffabff32017-03-27 09:52:19 -07002339 update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002340#else
2341 // update partition context
2342 if (bsize >= BLOCK_8X8 &&
2343 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
Jingning Han1beb0102016-12-07 11:08:30 -08002344 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
David Barkerf8935c92016-10-26 14:54:06 +01002345#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xud71be782016-10-14 08:47:03 -07002346
Jean-Marc Valin01435132017-02-18 14:12:53 -05002347#if CONFIG_CDEF
David Barker9739f362016-11-10 09:29:32 +00002348#if CONFIG_EXT_PARTITION
2349 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128) {
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002350 if (!sb_all_skip(cm, mi_row, mi_col)) {
Jean-Marc Valin1df427e2017-03-23 18:32:57 -04002351 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2352 aom_read_literal(r, cm->cdef_bits, ACCT_STR);
David Barker9739f362016-11-10 09:29:32 +00002353 } else {
Jean-Marc Valin1df427e2017-03-23 18:32:57 -04002354 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2355 0;
David Barker9739f362016-11-10 09:29:32 +00002356 }
2357 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64) {
2358#else
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002359 if (bsize == BLOCK_64X64) {
David Barker9739f362016-11-10 09:29:32 +00002360#endif
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002361 if (!sb_all_skip(cm, mi_row, mi_col)) {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002362 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2363 aom_read_literal(r, cm->cdef_bits, ACCT_STR);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002364 } else {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002365 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2366 0;
Yaowu Xud71be782016-10-14 08:47:03 -07002367 }
2368 }
Jean-Marc Valin01435132017-02-18 14:12:53 -05002369#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002370}
2371
Yaowu Xuc27fc142016-08-22 16:08:15 -07002372static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
2373 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002374 struct aom_internal_error_info *error_info,
Alex Converseeb780e72016-12-13 12:46:41 -08002375 aom_reader *r,
2376#if CONFIG_ANS && ANS_MAX_SYMBOLS
2377 int window_size,
2378#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
2379 aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002380 // Validate the calculated partition length. If the buffer
2381 // described by the partition can't be fully read, then restrict
2382 // it to the portion that can be (for EC mode) or throw an error.
2383 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002384 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002385 "Truncated packet or corrupt tile length");
2386
Alex Converse2cdf0d82016-12-13 13:53:09 -08002387#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08002388 r->window_size = window_size;
Alex Converse2cdf0d82016-12-13 13:53:09 -08002389#endif
Alex Converse346440b2017-01-03 13:47:37 -08002390 if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07002391 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002392 "Failed to allocate bool decoder %d", 1);
2393}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002394
Angie Chiang800df032017-03-22 11:14:12 -07002395#if !CONFIG_PVQ && !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET) && !CONFIG_LV_MAP
Yaowu Xuf883b422016-08-30 14:01:10 -07002396static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
2397 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002398 int i, j, k, l, m;
Alex Conversea9598cd2017-02-03 14:18:05 -08002399#if CONFIG_EC_ADAPT
2400 const int node_limit = UNCONSTRAINED_NODES - 1;
2401#else
Thomas9ac55082016-09-23 18:04:17 +01002402 const int node_limit = UNCONSTRAINED_NODES;
Alex Conversea9598cd2017-02-03 14:18:05 -08002403#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002404
Michael Bebenita6048d052016-08-25 14:40:54 -07002405 if (aom_read_bit(r, ACCT_STR))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002406 for (i = 0; i < PLANE_TYPES; ++i)
2407 for (j = 0; j < REF_TYPES; ++j)
2408 for (k = 0; k < COEF_BANDS; ++k)
2409 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
Thomas9ac55082016-09-23 18:04:17 +01002410 for (m = 0; m < node_limit; ++m)
Michael Bebenita6048d052016-08-25 14:40:54 -07002411 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002412}
2413
Yaowu Xuf883b422016-08-30 14:01:10 -07002414static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002415 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
2416 TX_SIZE tx_size;
Jingning Han83630632016-12-16 11:27:25 -08002417 for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002418 read_coef_probs_common(fc->coef_probs[tx_size], r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002419}
Yushin Cho77bba8d2016-11-04 16:36:56 -07002420#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002421
Yaowu Xuf883b422016-08-30 14:01:10 -07002422static void setup_segmentation(AV1_COMMON *const cm,
2423 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002424 struct segmentation *const seg = &cm->seg;
2425 int i, j;
2426
2427 seg->update_map = 0;
2428 seg->update_data = 0;
2429
Yaowu Xuf883b422016-08-30 14:01:10 -07002430 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002431 if (!seg->enabled) return;
2432
2433 // Segmentation map update
2434 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2435 seg->update_map = 1;
2436 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002437 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002438 }
2439 if (seg->update_map) {
2440 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2441 seg->temporal_update = 0;
2442 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002443 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002444 }
2445 }
2446
2447 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07002448 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002449 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002450 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002451
Yaowu Xuf883b422016-08-30 14:01:10 -07002452 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002453
2454 for (i = 0; i < MAX_SEGMENTS; i++) {
2455 for (j = 0; j < SEG_LVL_MAX; j++) {
2456 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002457 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002458 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002459 av1_enable_segfeature(seg, i, j);
2460 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
2461 if (av1_is_segfeature_signed(j))
2462 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002463 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002464 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002465 }
2466 }
2467 }
2468}
2469
2470#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002471static void decode_restoration_mode(AV1_COMMON *cm,
2472 struct aom_read_bit_buffer *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002473 int p;
2474 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002475 if (aom_rb_read_bit(rb)) {
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002476 rsi->frame_restoration_type =
2477 aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002478 } else {
2479 rsi->frame_restoration_type =
2480 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
2481 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002482 for (p = 1; p < MAX_MB_PLANE; ++p) {
2483 cm->rst_info[p].frame_restoration_type =
2484 aom_rb_read_bit(rb) ? RESTORE_WIENER : RESTORE_NONE;
2485 }
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002486
2487 cm->rst_info[0].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2488 cm->rst_info[1].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2489 cm->rst_info[2].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2490 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
2491 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
2492 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
2493 rsi = &cm->rst_info[0];
2494 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2495 if (rsi->restoration_tilesize != RESTORATION_TILESIZE_MAX) {
2496 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2497 }
2498 cm->rst_info[1].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2499 cm->rst_info[2].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2500 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002501}
2502
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002503static void read_wiener_filter(WienerInfo *wiener_info, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002504 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002505 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2506 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002507 wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002508 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2509 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002510 wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002511 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2512 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002513 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002514 wiener_info->vfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002515 -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
2516 wiener_info->vfilter[2]);
2517
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002518 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002519 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2520 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002521 wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002522 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2523 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002524 wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002525 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2526 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002527 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002528 wiener_info->hfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002529 -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
2530 wiener_info->hfilter[2]);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002531}
2532
2533static void read_sgrproj_filter(SgrprojInfo *sgrproj_info, aom_reader *rb) {
2534 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
2535 sgrproj_info->xqd[0] =
2536 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN0;
2537 sgrproj_info->xqd[1] =
2538 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN1;
2539}
2540
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002541static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002542 int i, p;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002543 const int ntiles = av1_get_rest_ntiles(cm->width, cm->height,
2544 cm->rst_info[0].restoration_tilesize,
2545 NULL, NULL, NULL, NULL);
2546 const int ntiles_uv = av1_get_rest_ntiles(
2547 ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
2548 ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y),
2549 cm->rst_info[1].restoration_tilesize, NULL, NULL, NULL, NULL);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002550 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002551 if (rsi->frame_restoration_type != RESTORE_NONE) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002552 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002553 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002554 rsi->restoration_type[i] =
2555 aom_read_tree(rb, av1_switchable_restore_tree,
2556 cm->fc->switchable_restore_prob, ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002557 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002558 read_wiener_filter(&rsi->wiener_info[i], rb);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002559 } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002560 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002561 }
2562 }
2563 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002564 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002565 if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002566 rsi->restoration_type[i] = RESTORE_WIENER;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002567 read_wiener_filter(&rsi->wiener_info[i], rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002568 } else {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002569 rsi->restoration_type[i] = RESTORE_NONE;
2570 }
2571 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002572 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002573 for (i = 0; i < ntiles; ++i) {
2574 if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
2575 rsi->restoration_type[i] = RESTORE_SGRPROJ;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002576 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
2577 } else {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002578 rsi->restoration_type[i] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002579 }
2580 }
2581 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002582 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002583 for (p = 1; p < MAX_MB_PLANE; ++p) {
2584 rsi = &cm->rst_info[p];
2585 if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08002586 for (i = 0; i < ntiles_uv; ++i) {
2587 if (ntiles_uv > 1)
2588 rsi->restoration_type[i] =
2589 aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR) ? RESTORE_WIENER
2590 : RESTORE_NONE;
2591 else
2592 rsi->restoration_type[i] = RESTORE_WIENER;
2593 if (rsi->restoration_type[i] == RESTORE_WIENER) {
2594 read_wiener_filter(&rsi->wiener_info[i], rb);
2595 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002596 }
2597 }
2598 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002599}
2600#endif // CONFIG_LOOP_RESTORATION
2601
Yaowu Xuf883b422016-08-30 14:01:10 -07002602static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002603 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07002604 lf->filter_level = aom_rb_read_literal(rb, 6);
2605 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002606
2607 // Read in loop filter deltas applied at the MB level based on mode or ref
2608 // frame.
2609 lf->mode_ref_delta_update = 0;
2610
Yaowu Xuf883b422016-08-30 14:01:10 -07002611 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002612 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002613 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002614 if (lf->mode_ref_delta_update) {
2615 int i;
2616
2617 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002618 if (aom_rb_read_bit(rb))
2619 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002620
2621 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002622 if (aom_rb_read_bit(rb))
2623 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002624 }
2625 }
2626}
2627
Jean-Marc Valin01435132017-02-18 14:12:53 -05002628#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002629static void setup_cdef(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002630 int i;
2631 cm->cdef_bits = aom_rb_read_literal(rb, 2);
2632 cm->nb_cdef_strengths = 1 << cm->cdef_bits;
2633 for (i = 0; i < cm->nb_cdef_strengths; i++) {
2634 cm->cdef_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
Jean-Marc Valine9f77422017-03-22 17:09:51 -04002635 cm->cdef_uv_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002636 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002637}
Jean-Marc Valin01435132017-02-18 14:12:53 -05002638#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002639
Yaowu Xuf883b422016-08-30 14:01:10 -07002640static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2641 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002642}
2643
Yaowu Xuf883b422016-08-30 14:01:10 -07002644static void setup_quantization(AV1_COMMON *const cm,
2645 struct aom_read_bit_buffer *rb) {
2646 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002647 cm->y_dc_delta_q = read_delta_q(rb);
2648 cm->uv_dc_delta_q = read_delta_q(rb);
2649 cm->uv_ac_delta_q = read_delta_q(rb);
2650 cm->dequant_bit_depth = cm->bit_depth;
2651#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002652 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002653 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002654 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2655 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002656 } else {
2657 cm->min_qmlevel = 0;
2658 cm->max_qmlevel = 0;
2659 }
2660#endif
2661}
2662
Yaowu Xuf883b422016-08-30 14:01:10 -07002663static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002664 // Build y/uv dequant values based on segmentation.
2665 int i = 0;
2666#if CONFIG_AOM_QM
2667 int lossless;
2668 int j = 0;
2669 int qmlevel;
2670 int using_qm = cm->using_qmatrix;
2671 int minqm = cm->min_qmlevel;
2672 int maxqm = cm->max_qmlevel;
2673#endif
2674#if CONFIG_NEW_QUANT
2675 int b;
2676 int dq;
2677#endif // CONFIG_NEW_QUANT
2678 if (cm->seg.enabled) {
2679 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002680 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002681 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002682 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2683 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002684 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002685 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002686 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002687 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002688#if CONFIG_AOM_QM
2689 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2690 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2691 // NB: depends on base index so there is only 1 set per frame
2692 // No quant weighting when lossless or signalled not using QM
2693 qmlevel = (lossless || using_qm == 0)
2694 ? NUM_QM_LEVELS - 1
2695 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2696 for (j = 0; j < TX_SIZES; ++j) {
2697 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2698 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2699 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2700 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2701 }
2702#endif // CONFIG_AOM_QM
2703#if CONFIG_NEW_QUANT
2704 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2705 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002706 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002707 cm->y_dequant_nuq[i][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002708 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002709 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002710 }
2711 }
2712#endif // CONFIG_NEW_QUANT
2713 }
2714 } else {
2715 const int qindex = cm->base_qindex;
2716 // When segmentation is disabled, only the first value is used. The
2717 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002718 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2719 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002720 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002721 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002722 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002723 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002724#if CONFIG_AOM_QM
2725 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2726 cm->uv_ac_delta_q == 0;
2727 // No quant weighting when lossless or signalled not using QM
2728 qmlevel = (lossless || using_qm == 0)
2729 ? NUM_QM_LEVELS - 1
2730 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2731 for (j = 0; j < TX_SIZES; ++j) {
2732 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2733 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2734 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2735 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2736 }
2737#endif
2738#if CONFIG_NEW_QUANT
2739 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2740 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002741 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002742 cm->y_dequant_nuq[0][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002743 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002744 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002745 }
2746 }
2747#endif // CONFIG_NEW_QUANT
2748 }
2749}
2750
Angie Chiang5678ad92016-11-21 09:38:40 -08002751static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002752 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07002753 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002754}
2755
Yaowu Xuf883b422016-08-30 14:01:10 -07002756static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002757 cm->render_width = cm->width;
2758 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002759 if (aom_rb_read_bit(rb))
2760 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002761}
2762
Fergus Simpsone7508412017-03-14 18:14:09 -07002763#if CONFIG_LOOP_RESTORATION && CONFIG_FRAME_SUPERRES
2764// TODO(afergs): make "struct aom_read_bit_buffer *const rb"?
2765static void setup_superres_size(AV1_COMMON *const cm,
2766 struct aom_read_bit_buffer *rb, int *width,
2767 int *height) {
2768 // TODO(afergs): Test this behaviour
2769 // Frame superres is probably in compatible with this render resolution
2770 assert(cm->width == cm->render_width && cm->height == cm->render_height);
2771
2772 cm->superres_width = cm->width;
2773 cm->superres_height = cm->height;
2774 if (aom_rb_read_bit(rb)) {
2775 cm->superres_scale_numerator =
2776 (uint8_t)aom_rb_read_literal(rb, SUPERRES_SCALE_BITS);
2777 cm->superres_scale_numerator += SUPERRES_SCALE_NUMERATOR_MIN;
2778 // Don't edit cm->width or cm->height directly, or the buffers won't get
2779 // resized correctly
2780 // TODO(afergs): Should the render resolution not be modified? It's the same
2781 // by default (ie. when it isn't sent)...
2782 // resize_context_buffers() will change cm->width to equal cm->render_width,
2783 // then they'll be the same again
2784 *width = cm->render_width =
2785 cm->width * cm->superres_scale_numerator / SUPERRES_SCALE_DENOMINATOR;
2786 *height = cm->render_height =
2787 cm->height * cm->superres_scale_numerator / SUPERRES_SCALE_DENOMINATOR;
2788 } else {
2789 // 1:1 scaling - ie. no scaling, scale not provided
2790 cm->superres_scale_numerator = SUPERRES_SCALE_DENOMINATOR;
2791 }
2792}
2793#endif // CONFIG_LOOP_RESTORATION && CONFIG_FRAME_SUPERRES
2794
Yaowu Xuf883b422016-08-30 14:01:10 -07002795static void resize_mv_buffer(AV1_COMMON *cm) {
2796 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002797 cm->cur_frame->mi_rows = cm->mi_rows;
2798 cm->cur_frame->mi_cols = cm->mi_cols;
2799 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002800 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002801 sizeof(*cm->cur_frame->mvs)));
2802}
2803
Yaowu Xuf883b422016-08-30 14:01:10 -07002804static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002805#if CONFIG_SIZE_LIMIT
2806 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002807 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002808 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2809 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2810#endif
2811 if (cm->width != width || cm->height != height) {
2812 const int new_mi_rows =
2813 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2814 const int new_mi_cols =
2815 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2816
Yaowu Xuf883b422016-08-30 14:01:10 -07002817 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002818 // dimensions as well as the overall size.
2819 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002820 if (av1_alloc_context_buffers(cm, width, height))
2821 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002822 "Failed to allocate context buffers");
2823 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002824 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002825 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002826 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002827 cm->width = width;
2828 cm->height = height;
2829 }
2830 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2831 cm->mi_cols > cm->cur_frame->mi_cols) {
2832 resize_mv_buffer(cm);
2833 }
2834}
2835
Yaowu Xuf883b422016-08-30 14:01:10 -07002836static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002837 int width, height;
2838 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002839 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002840 setup_render_size(cm, rb);
Fergus Simpsone7508412017-03-14 18:14:09 -07002841#if CONFIG_LOOP_RESTORATION && CONFIG_FRAME_SUPERRES
2842 setup_superres_size(cm, rb, &width, &height);
2843#endif // CONFIG_LOOP_RESTORATION && CONFIG_FRAME_SUPERRES
2844 resize_context_buffers(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002845
2846 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002847 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002848 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2849 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002850#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002851 cm->use_highbitdepth,
2852#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002853 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002854 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2855 pool->cb_priv)) {
2856 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002857 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002858 "Failed to allocate frame buffer");
2859 }
2860 unlock_buffer_pool(pool);
2861
2862 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2863 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2864 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2865 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2866 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2867 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2868 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2869}
2870
Yaowu Xuf883b422016-08-30 14:01:10 -07002871static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002872 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002873 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002874 int this_xss, int this_yss) {
2875 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2876 ref_yss == this_yss;
2877}
2878
Yaowu Xuf883b422016-08-30 14:01:10 -07002879static void setup_frame_size_with_refs(AV1_COMMON *cm,
2880 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002881 int width, height;
2882 int found = 0, i;
2883 int has_valid_ref_frame = 0;
2884 BufferPool *const pool = cm->buffer_pool;
2885 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002886 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002887 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2888 width = buf->y_crop_width;
2889 height = buf->y_crop_height;
2890 cm->render_width = buf->render_width;
2891 cm->render_height = buf->render_height;
2892 found = 1;
2893 break;
2894 }
2895 }
2896
2897 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002898 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002899 setup_render_size(cm, rb);
2900 }
2901
2902 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002903 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002904 "Invalid frame size");
2905
2906 // Check to make sure at least one of frames that this frame references
2907 // has valid dimensions.
2908 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2909 RefBuffer *const ref_frame = &cm->frame_refs[i];
2910 has_valid_ref_frame |=
2911 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2912 ref_frame->buf->y_crop_height, width, height);
2913 }
2914 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002915 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002916 "Referenced frame has invalid size");
2917 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2918 RefBuffer *const ref_frame = &cm->frame_refs[i];
2919 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
2920 ref_frame->buf->subsampling_x,
2921 ref_frame->buf->subsampling_y, cm->bit_depth,
2922 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07002923 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002924 "Referenced frame has incompatible color format");
2925 }
2926
2927 resize_context_buffers(cm, width, height);
2928
2929 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002930 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002931 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2932 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002933#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002934 cm->use_highbitdepth,
2935#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002936 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002937 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2938 pool->cb_priv)) {
2939 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002940 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002941 "Failed to allocate frame buffer");
2942 }
2943 unlock_buffer_pool(pool);
2944
2945 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2946 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2947 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2948 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2949 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2950 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2951 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2952}
2953
Yaowu Xuf883b422016-08-30 14:01:10 -07002954static void read_tile_info(AV1Decoder *const pbi,
2955 struct aom_read_bit_buffer *const rb) {
2956 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002957#if CONFIG_EXT_TILE
2958// Read the tile width/height
2959#if CONFIG_EXT_PARTITION
2960 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002961 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
2962 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002963 } else
2964#endif // CONFIG_EXT_PARTITION
2965 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002966 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
2967 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002968 }
2969
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002970#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002971 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002972#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002973
Yaowu Xuc27fc142016-08-22 16:08:15 -07002974 cm->tile_width <<= cm->mib_size_log2;
2975 cm->tile_height <<= cm->mib_size_log2;
2976
Yaowu Xuf883b422016-08-30 14:01:10 -07002977 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
2978 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002979
2980 // Get the number of tiles
2981 cm->tile_cols = 1;
2982 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
2983
2984 cm->tile_rows = 1;
2985 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
2986
2987 if (cm->tile_cols * cm->tile_rows > 1) {
2988 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07002989 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2990 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002991 }
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002992
2993#if CONFIG_DEPENDENT_HORZTILES
2994 if (cm->tile_rows <= 1)
2995 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
2996 else
2997 cm->dependent_horz_tiles = 0;
2998#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002999#else
3000 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07003001 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003002
3003 // columns
3004 max_ones = max_log2_tile_cols - min_log2_tile_cols;
3005 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07003006 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003007
3008 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07003009 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003010 "Invalid number of tile columns");
3011
3012 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07003013 cm->log2_tile_rows = aom_rb_read_bit(rb);
3014 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003015#if CONFIG_DEPENDENT_HORZTILES
3016 if (cm->log2_tile_rows != 0)
3017 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
3018 else
3019 cm->dependent_horz_tiles = 0;
3020#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003021#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003022 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003023#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003024
Yaowu Xuc27fc142016-08-22 16:08:15 -07003025 cm->tile_cols = 1 << cm->log2_tile_cols;
3026 cm->tile_rows = 1 << cm->log2_tile_rows;
3027
3028 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
3029 cm->tile_width >>= cm->log2_tile_cols;
3030 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
3031 cm->tile_height >>= cm->log2_tile_rows;
3032
3033 // round to integer multiples of superblock size
3034 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
3035 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
3036
Thomas Davies4974e522016-11-07 17:44:05 +00003037// tile size magnitude
3038#if !CONFIG_TILE_GROUPS
3039 if (cm->tile_rows > 1 || cm->tile_cols > 1)
3040#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003041 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003042#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00003043
Thomas Davies80188d12016-10-26 16:08:35 -07003044#if CONFIG_TILE_GROUPS
3045 // Store an index to the location of the tile group information
3046 pbi->tg_size_bit_offset = rb->bit_offset;
3047 pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
3048 if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
3049 pbi->tg_start =
3050 aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3051 pbi->tg_size =
3052 1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3053 }
3054#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003055}
3056
3057static int mem_get_varsize(const uint8_t *src, const int sz) {
3058 switch (sz) {
3059 case 1: return src[0];
3060 case 2: return mem_get_le16(src);
3061 case 3: return mem_get_le24(src);
3062 case 4: return mem_get_le32(src);
3063 default: assert("Invalid size" && 0); return -1;
3064 }
3065}
3066
3067#if CONFIG_EXT_TILE
3068// Reads the next tile returning its size and adjusting '*data' accordingly
3069// based on 'is_last'.
3070static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07003071 struct aom_internal_error_info *error_info,
3072 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003073 void *decrypt_state,
3074 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
3075 int tile_size_bytes, int col, int row) {
3076 size_t size;
3077
3078 size_t copy_size = 0;
3079 const uint8_t *copy_data = NULL;
3080
3081 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003082 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003083 "Truncated packet or corrupt tile length");
3084 if (decrypt_cb) {
3085 uint8_t be_data[4];
3086 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3087
3088 // Only read number of bytes in cm->tile_size_bytes.
3089 size = mem_get_varsize(be_data, tile_size_bytes);
3090 } else {
3091 size = mem_get_varsize(*data, tile_size_bytes);
3092 }
3093
3094 // The top bit indicates copy mode
3095 if ((size >> (tile_size_bytes * 8 - 1)) == 1) {
3096 // The remaining bits in the top byte signal the row offset
3097 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
3098
3099 // Currently, only use tiles in same column as reference tiles.
3100 copy_data = tile_buffers[row - offset][col].data;
3101 copy_size = tile_buffers[row - offset][col].size;
3102 size = 0;
3103 }
3104
3105 *data += tile_size_bytes;
3106
3107 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003108 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003109 "Truncated packet or corrupt tile size");
3110
3111 if (size > 0) {
3112 tile_buffers[row][col].data = *data;
3113 tile_buffers[row][col].size = size;
3114 } else {
3115 tile_buffers[row][col].data = copy_data;
3116 tile_buffers[row][col].size = copy_size;
3117 }
3118
3119 *data += size;
3120
3121 tile_buffers[row][col].raw_data_end = *data;
3122}
3123
3124static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003125 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003126 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003127 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003128 const int tile_cols = cm->tile_cols;
3129 const int tile_rows = cm->tile_rows;
3130 const int have_tiles = tile_cols * tile_rows > 1;
3131
3132 if (!have_tiles) {
3133 const uint32_t tile_size = data_end - data;
3134 tile_buffers[0][0].data = data;
3135 tile_buffers[0][0].size = tile_size;
3136 tile_buffers[0][0].raw_data_end = NULL;
3137 } else {
3138 // We locate only the tile buffers that are required, which are the ones
3139 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
3140 // need the last (bottom right) tile buffer, as we need to know where the
3141 // end of the compressed frame buffer is for proper superframe decoding.
3142
3143 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
3144 const uint8_t *const data_start = data;
3145
Yaowu Xuf883b422016-08-30 14:01:10 -07003146 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003147 const int single_row = pbi->dec_tile_row >= 0;
3148 const int tile_rows_start = single_row ? dec_tile_row : 0;
3149 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003150 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003151 const int single_col = pbi->dec_tile_col >= 0;
3152 const int tile_cols_start = single_col ? dec_tile_col : 0;
3153 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3154
3155 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
3156 const int tile_size_bytes = pbi->tile_size_bytes;
3157
3158 size_t tile_col_size;
3159 int r, c;
3160
3161 // Read tile column sizes for all columns (we need the last tile buffer)
3162 for (c = 0; c < tile_cols; ++c) {
3163 const int is_last = c == tile_cols - 1;
3164 if (!is_last) {
3165 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
3166 data += tile_col_size_bytes;
3167 tile_col_data_end[c] = data + tile_col_size;
3168 } else {
3169 tile_col_size = data_end - data;
3170 tile_col_data_end[c] = data_end;
3171 }
3172 data += tile_col_size;
3173 }
3174
3175 data = data_start;
3176
3177 // Read the required tile sizes.
3178 for (c = tile_cols_start; c < tile_cols_end; ++c) {
3179 const int is_last = c == tile_cols - 1;
3180
3181 if (c > 0) data = tile_col_data_end[c - 1];
3182
3183 if (!is_last) data += tile_col_size_bytes;
3184
3185 // Get the whole of the last column, otherwise stop at the required tile.
3186 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
3187 tile_buffers[r][c].col = c;
3188
3189 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3190 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3191 tile_size_bytes, c, r);
3192 }
3193 }
3194
3195 // If we have not read the last column, then read it to get the last tile.
3196 if (tile_cols_end != tile_cols) {
3197 c = tile_cols - 1;
3198
3199 data = tile_col_data_end[c - 1];
3200
3201 for (r = 0; r < tile_rows; ++r) {
3202 tile_buffers[r][c].col = c;
3203
3204 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3205 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3206 tile_size_bytes, c, r);
3207 }
3208 }
3209 }
3210}
3211#else
3212// Reads the next tile returning its size and adjusting '*data' accordingly
3213// based on 'is_last'.
3214static void get_tile_buffer(const uint8_t *const data_end,
3215 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07003216 struct aom_internal_error_info *error_info,
3217 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003218 void *decrypt_state, TileBufferDec *const buf) {
3219 size_t size;
3220
3221 if (!is_last) {
Yaowu Xu0a79a1b2017-02-17 13:04:54 -08003222 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003223 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003224 "Truncated packet or corrupt tile length");
3225
3226 if (decrypt_cb) {
3227 uint8_t be_data[4];
3228 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3229 size = mem_get_varsize(be_data, tile_size_bytes);
3230 } else {
3231 size = mem_get_varsize(*data, tile_size_bytes);
3232 }
3233 *data += tile_size_bytes;
3234
3235 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003236 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003237 "Truncated packet or corrupt tile size");
3238 } else {
3239 size = data_end - *data;
3240 }
3241
3242 buf->data = *data;
3243 buf->size = size;
3244
3245 *data += size;
3246}
3247
3248static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003249 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003250 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003251 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07003252#if CONFIG_TILE_GROUPS
3253 int r, c;
3254 const int tile_cols = cm->tile_cols;
3255 const int tile_rows = cm->tile_rows;
3256 int tc = 0;
3257 int first_tile_in_tg = 0;
3258 int hdr_offset;
3259 struct aom_read_bit_buffer rb_tg_hdr;
3260 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3261 const int num_tiles = tile_rows * tile_cols;
3262 const int num_bits = OD_ILOG(num_tiles) - 1;
3263 const int hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
3264 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
Fangwen Fu73126c02017-02-08 22:37:47 -08003265#if CONFIG_DEPENDENT_HORZTILES
3266 int tile_group_start_col = 0;
3267 int tile_group_start_row = 0;
3268#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003269
3270 for (r = 0; r < tile_rows; ++r) {
3271 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07003272 TileBufferDec *const buf = &tile_buffers[r][c];
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003273 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
Thomas Davies80188d12016-10-26 16:08:35 -07003274 hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
3275
3276 buf->col = c;
3277 if (hdr_offset) {
3278 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
3279 rb_tg_hdr.bit_offset = tg_size_bit_offset;
3280 if (num_tiles) {
3281 pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
3282 pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
Fangwen Fu73126c02017-02-08 22:37:47 -08003283#if CONFIG_DEPENDENT_HORZTILES
3284 tile_group_start_row = r;
3285 tile_group_start_col = c;
3286#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003287 }
3288 }
3289 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
3290 data += hdr_offset;
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003291 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last,
3292 &pbi->common.error, &data, pbi->decrypt_cb,
3293 pbi->decrypt_state, buf);
Fangwen Fu73126c02017-02-08 22:37:47 -08003294#if CONFIG_DEPENDENT_HORZTILES
3295 cm->tile_group_start_row[r][c] = tile_group_start_row;
3296 cm->tile_group_start_col[r][c] = tile_group_start_col;
3297#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003298 }
3299 }
3300#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003301 int r, c;
3302 const int tile_cols = cm->tile_cols;
3303 const int tile_rows = cm->tile_rows;
3304
3305 for (r = 0; r < tile_rows; ++r) {
3306 for (c = 0; c < tile_cols; ++c) {
3307 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
3308 TileBufferDec *const buf = &tile_buffers[r][c];
3309 buf->col = c;
3310 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
3311 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
3312 }
3313 }
Thomas Davies80188d12016-10-26 16:08:35 -07003314#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003315}
3316#endif // CONFIG_EXT_TILE
3317
Yushin Cho77bba8d2016-11-04 16:36:56 -07003318#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003319static void daala_dec_init(AV1_COMMON *const cm, daala_dec_ctx *daala_dec,
Nathan E. Eggeab083972016-12-28 15:31:46 -05003320 aom_reader *r) {
3321 daala_dec->r = r;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003322
Yushin Cho70669122016-12-08 09:53:14 -10003323 // TODO(yushin) : activity masking info needs be signaled by a bitstream
3324 daala_dec->use_activity_masking = AV1_PVQ_ENABLE_ACTIVITY_MASKING;
3325
Yushin Cho7a428ba2017-01-12 16:28:49 -08003326#if !CONFIG_DAALA_DIST
3327 daala_dec->use_activity_masking = 0;
3328#endif
3329
Yushin Cho70669122016-12-08 09:53:14 -10003330 if (daala_dec->use_activity_masking)
3331 daala_dec->qm = OD_HVS_QM;
3332 else
3333 daala_dec->qm = OD_FLAT_QM;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003334
3335 od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
3336 daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
Yushin Cho70669122016-12-08 09:53:14 -10003337
3338 if (daala_dec->use_activity_masking) {
3339 int pli;
3340 int use_masking = daala_dec->use_activity_masking;
3341 int segment_id = 0;
3342 int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
3343
3344 for (pli = 0; pli < MAX_MB_PLANE; pli++) {
3345 int i;
3346 int q;
3347
3348 q = qindex;
3349 if (q <= OD_DEFAULT_QMS[use_masking][0][pli].interp_q << OD_COEFF_SHIFT) {
3350 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3351 &OD_DEFAULT_QMS[use_masking][0][pli], NULL);
3352 } else {
3353 i = 0;
3354 while (OD_DEFAULT_QMS[use_masking][i + 1][pli].qm_q4 != NULL &&
3355 q > OD_DEFAULT_QMS[use_masking][i + 1][pli].interp_q
3356 << OD_COEFF_SHIFT) {
3357 i++;
3358 }
3359 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3360 &OD_DEFAULT_QMS[use_masking][i][pli],
3361 &OD_DEFAULT_QMS[use_masking][i + 1][pli]);
3362 }
3363 }
3364 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07003365}
Yushin Cho70669122016-12-08 09:53:14 -10003366#endif // #if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003367
Yaowu Xuf883b422016-08-30 14:01:10 -07003368static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003369 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003370 AV1_COMMON *const cm = &pbi->common;
3371 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003372 const int tile_cols = cm->tile_cols;
3373 const int tile_rows = cm->tile_rows;
3374 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07003375 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003376#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003377 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003378 const int single_row = pbi->dec_tile_row >= 0;
3379 const int tile_rows_start = single_row ? dec_tile_row : 0;
3380 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003381 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003382 const int single_col = pbi->dec_tile_col >= 0;
3383 const int tile_cols_start = single_col ? dec_tile_col : 0;
3384 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3385 const int inv_col_order = pbi->inv_tile_order && !single_col;
3386 const int inv_row_order = pbi->inv_tile_order && !single_row;
3387#else
3388 const int tile_rows_start = 0;
3389 const int tile_rows_end = tile_rows;
3390 const int tile_cols_start = 0;
3391 const int tile_cols_end = tile_cols;
3392 const int inv_col_order = pbi->inv_tile_order;
3393 const int inv_row_order = pbi->inv_tile_order;
3394#endif // CONFIG_EXT_TILE
3395 int tile_row, tile_col;
3396
hui su0d103572017-03-01 17:58:01 -08003397#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003398 cm->do_subframe_update = n_tiles == 1;
hui su0d103572017-03-01 17:58:01 -08003399#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003400
3401 if (cm->lf.filter_level && !cm->skip_loop_filter &&
3402 pbi->lf_worker.data1 == NULL) {
3403 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07003404 aom_memalign(32, sizeof(LFWorkerData)));
3405 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003406 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003407 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003408 "Loop filter thread creation failed");
3409 }
3410 }
3411
3412 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3413 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3414 // Be sure to sync as we might be resuming after a failed frame decode.
3415 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003416 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
3417 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003418 }
3419
3420 assert(tile_rows <= MAX_TILE_ROWS);
3421 assert(tile_cols <= MAX_TILE_COLS);
3422
3423 get_tile_buffers(pbi, data, data_end, tile_buffers);
3424
3425 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003426 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003427 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003428 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003429 pbi->allocated_tiles = n_tiles;
3430 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003431#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003432 if (pbi->acct_enabled) {
3433 aom_accounting_reset(&pbi->accounting);
3434 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003435#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003436 // Load all tile information into tile_data.
3437 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3438 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3439 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
3440 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
3441
3442 td->cm = cm;
3443 td->xd = pbi->mb;
3444 td->xd.corrupted = 0;
3445 td->xd.counts =
3446 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3447 ? &cm->counts
3448 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003449 av1_zero(td->dqcoeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003450#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003451 av1_zero(td->pvq_ref_coeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003452#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003453 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003454 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003455 &td->bit_reader,
3456#if CONFIG_ANS && ANS_MAX_SYMBOLS
3457 1 << cm->ans_window_size_log2,
3458#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3459 pbi->decrypt_cb, pbi->decrypt_state);
Michael Bebenita6048d052016-08-25 14:40:54 -07003460#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003461 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003462 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003463 } else {
David Barkerd971f402016-10-25 13:52:07 +01003464 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003465 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003466#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003467 av1_init_macroblockd(cm, &td->xd,
3468#if CONFIG_PVQ
3469 td->pvq_ref_coeff,
3470#endif
3471 td->dqcoeff);
Yushin Choc49ef3a2017-03-13 17:27:25 -07003472
Thomas Daviesf77d4ad2017-01-10 18:55:42 +00003473#if CONFIG_EC_ADAPT
3474 // Initialise the tile context from the frame context
3475 td->tctx = *cm->fc;
3476 td->xd.tile_ctx = &td->tctx;
3477#endif
Yushin Choc49ef3a2017-03-13 17:27:25 -07003478
3479#if CONFIG_PVQ
3480 daala_dec_init(cm, &td->xd.daala_dec, &td->bit_reader);
3481 td->xd.daala_dec.state.adapt = &td->tctx.pvq_context;
3482#endif
3483
Urvang Joshib100db72016-10-12 16:28:56 -07003484#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003485 td->xd.plane[0].color_index_map = td->color_index_map[0];
3486 td->xd.plane[1].color_index_map = td->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003487#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003488 }
3489 }
3490
3491 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3492 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
3493 int mi_row = 0;
3494 TileInfo tile_info;
3495
Yaowu Xuf883b422016-08-30 14:01:10 -07003496 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003497
3498 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3499 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
3500 TileData *const td = pbi->tile_data + tile_cols * row + col;
Michael Bebenita6048d052016-08-25 14:40:54 -07003501#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003502 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003503 td->bit_reader.accounting->last_tell_frac =
3504 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003505 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003506#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003507
Yaowu Xuf883b422016-08-30 14:01:10 -07003508 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003509
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003510#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003511#if CONFIG_TILE_GROUPS
3512 av1_tile_set_tg_boundary(&tile_info, cm, tile_row, tile_col);
3513 if (!cm->dependent_horz_tiles || tile_row == 0 ||
3514 tile_info.tg_horz_boundary) {
3515#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003516 if (!cm->dependent_horz_tiles || tile_row == 0) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003517#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003518 av1_zero_above_context(cm, tile_info.mi_col_start,
3519 tile_info.mi_col_end);
3520 }
3521#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003522 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003523#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003524
3525 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
3526 mi_row += cm->mib_size) {
3527 int mi_col;
3528
Yaowu Xuf883b422016-08-30 14:01:10 -07003529 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003530
3531 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
3532 mi_col += cm->mib_size) {
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003533 av1_update_boundary_info(cm, &tile_info, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003534 decode_partition(pbi, &td->xd,
3535#if CONFIG_SUPERTX
3536 0,
3537#endif // CONFIG_SUPERTX
3538 mi_row, mi_col, &td->bit_reader, cm->sb_size,
3539 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003540#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3541 detoken_and_recon_sb(pbi, &td->xd, mi_row, mi_col, &td->bit_reader,
3542 cm->sb_size);
3543#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003544 }
Angie Chiangd0916d92017-03-10 17:54:18 -08003545 aom_merge_corrupted_flag(&pbi->mb.corrupted, td->xd.corrupted);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003546 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003547 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003548 "Failed to decode tile data");
hui su0d103572017-03-01 17:58:01 -08003549#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003550 if (cm->do_subframe_update &&
3551 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
hui su0161a932017-01-24 14:12:11 -08003552 const int mi_rows_per_update =
3553 MI_SIZE * AOMMAX(cm->mi_rows / MI_SIZE / COEF_PROBS_BUFS, 1);
3554 if ((mi_row + MI_SIZE) % mi_rows_per_update == 0 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003555 mi_row + MI_SIZE < cm->mi_rows &&
3556 cm->coef_probs_update_idx < COEF_PROBS_BUFS - 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003557 av1_partial_adapt_probs(cm, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003558 ++cm->coef_probs_update_idx;
3559 }
3560 }
hui su0d103572017-03-01 17:58:01 -08003561#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003562 }
3563 }
3564
3565 assert(mi_row > 0);
3566
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003567// when Parallel deblocking is enabled, deblocking should not
3568// be interleaved with decoding. Instead, deblocking should be done
3569// after the entire frame is decoded.
Jingning Han52ece882017-04-07 14:58:25 -07003570#if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING && !CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -07003571 // Loopfilter one tile row.
3572 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3573 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003574 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003575 const int lf_end = tile_info.mi_row_end - cm->mib_size;
3576
3577 // Delay the loopfilter if the first tile row is only
3578 // a single superblock high.
3579 if (lf_end <= 0) continue;
3580
3581 // Decoding has completed. Finish up the loop filter in this thread.
3582 if (tile_info.mi_row_end >= cm->mi_rows) continue;
3583
3584 winterface->sync(&pbi->lf_worker);
3585 lf_data->start = lf_start;
3586 lf_data->stop = lf_end;
3587 if (pbi->max_threads > 1) {
3588 winterface->launch(&pbi->lf_worker);
3589 } else {
3590 winterface->execute(&pbi->lf_worker);
3591 }
3592 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003593#endif // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003594
3595 // After loopfiltering, the last 7 row pixels in each superblock row may
3596 // still be changed by the longest loopfilter of the next superblock row.
3597 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003598 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003599 }
3600
Jingning Han52ece882017-04-07 14:58:25 -07003601#if CONFIG_VAR_TX || CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -07003602 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003603 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
3604 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003605#else
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003606#if CONFIG_PARALLEL_DEBLOCKING
3607 // Loopfilter all rows in the frame in the frame.
3608 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3609 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3610 winterface->sync(&pbi->lf_worker);
3611 lf_data->start = 0;
3612 lf_data->stop = cm->mi_rows;
3613 winterface->execute(&pbi->lf_worker);
3614 }
3615#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003616 // Loopfilter remaining rows in the frame.
3617 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3618 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3619 winterface->sync(&pbi->lf_worker);
3620 lf_data->start = lf_data->stop;
3621 lf_data->stop = cm->mi_rows;
3622 winterface->execute(&pbi->lf_worker);
3623 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003624#endif // CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003625#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003626 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003627 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003628
3629#if CONFIG_EXT_TILE
3630 if (n_tiles == 1) {
3631#if CONFIG_ANS
3632 return data_end;
3633#else
3634 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07003635 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003636#endif // CONFIG_ANS
3637 } else {
3638 // Return the end of the last tile buffer
3639 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3640 }
3641#else
3642#if CONFIG_ANS
3643 return data_end;
3644#else
3645 {
3646 // Get last tile data.
3647 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003648 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003649 }
3650#endif // CONFIG_ANS
3651#endif // CONFIG_EXT_TILE
3652}
3653
3654static int tile_worker_hook(TileWorkerData *const tile_data,
3655 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003656 AV1Decoder *const pbi = tile_data->pbi;
3657 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003658 int mi_row, mi_col;
3659
3660 if (setjmp(tile_data->error_info.jmp)) {
3661 tile_data->error_info.setjmp = 0;
Angie Chiangd0916d92017-03-10 17:54:18 -08003662 aom_merge_corrupted_flag(&tile_data->xd.corrupted, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003663 return 0;
3664 }
3665
3666 tile_data->error_info.setjmp = 1;
3667 tile_data->xd.error_info = &tile_data->error_info;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003668#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003669#if CONFIG_TILE_GROUPS
3670 if (!cm->dependent_horz_tiles || tile->tg_horz_boundary) {
3671#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003672 if (!cm->dependent_horz_tiles) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003673#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003674 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
3675 }
3676#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003677 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003678#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003679
3680 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
3681 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003682 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003683
3684 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3685 mi_col += cm->mib_size) {
3686 decode_partition(pbi, &tile_data->xd,
3687#if CONFIG_SUPERTX
3688 0,
3689#endif
3690 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
3691 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003692#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3693 detoken_and_recon_sb(pbi, &tile_data->xd, mi_row, mi_col,
3694 &tile_data->bit_reader, cm->sb_size);
3695#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003696 }
3697 }
3698 return !tile_data->xd.corrupted;
3699}
3700
3701// sorts in descending order
3702static int compare_tile_buffers(const void *a, const void *b) {
3703 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
3704 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
3705 return (int)(buf2->size - buf1->size);
3706}
3707
Yaowu Xuf883b422016-08-30 14:01:10 -07003708static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003709 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003710 AV1_COMMON *const cm = &pbi->common;
3711 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003712 const int tile_cols = cm->tile_cols;
3713 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003714 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
clang-format67948d32016-09-07 22:40:40 -07003715 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003716#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003717 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003718 const int single_row = pbi->dec_tile_row >= 0;
3719 const int tile_rows_start = single_row ? dec_tile_row : 0;
3720 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003721 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003722 const int single_col = pbi->dec_tile_col >= 0;
3723 const int tile_cols_start = single_col ? dec_tile_col : 0;
3724 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3725#else
3726 const int tile_rows_start = 0;
3727 const int tile_rows_end = tile_rows;
3728 const int tile_cols_start = 0;
3729 const int tile_cols_end = tile_cols;
3730#endif // CONFIG_EXT_TILE
3731 int tile_row, tile_col;
3732 int i;
3733
3734#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3735 int final_worker = -1;
3736#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3737
3738 assert(tile_rows <= MAX_TILE_ROWS);
3739 assert(tile_cols <= MAX_TILE_COLS);
3740
3741 assert(tile_cols * tile_rows > 1);
3742
Yaowu Xuc27fc142016-08-22 16:08:15 -07003743 // TODO(jzern): See if we can remove the restriction of passing in max
3744 // threads to the decoder.
3745 if (pbi->num_tile_workers == 0) {
3746 const int num_threads = pbi->max_threads & ~1;
3747 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07003748 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003749 // Ensure tile data offsets will be properly aligned. This may fail on
3750 // platforms without DECLARE_ALIGNED().
3751 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3752 CHECK_MEM_ERROR(
3753 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003754 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003755 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07003756 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003757 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003758 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003759 ++pbi->num_tile_workers;
3760
3761 winterface->init(worker);
3762 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003763 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003764 "Tile decoder thread creation failed");
3765 }
3766 }
3767 }
3768
3769 // Reset tile decoding hook
3770 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003771 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003772 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003773 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003774 worker->data1 = &pbi->tile_worker_data[i];
3775 worker->data2 = &pbi->tile_worker_info[i];
3776 }
3777
3778 // Initialize thread frame counts.
3779 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3780 for (i = 0; i < num_workers; ++i) {
3781 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003782 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003783 }
3784 }
3785
3786 // Load tile data into tile_buffers
3787 get_tile_buffers(pbi, data, data_end, tile_buffers);
3788
3789 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3790 // Sort the buffers in this tile row based on size in descending order.
3791 qsort(&tile_buffers[tile_row][tile_cols_start],
3792 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
3793 compare_tile_buffers);
3794
3795 // Rearrange the tile buffers in this tile row such that per-tile group
3796 // the largest, and presumably the most difficult tile will be decoded in
3797 // the main thread. This should help minimize the number of instances
3798 // where the main thread is waiting for a worker to complete.
3799 {
3800 int group_start;
3801 for (group_start = tile_cols_start; group_start < tile_cols_end;
3802 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003803 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003804 const TileBufferDec largest = tile_buffers[tile_row][group_start];
3805 memmove(&tile_buffers[tile_row][group_start],
3806 &tile_buffers[tile_row][group_start + 1],
3807 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
3808 tile_buffers[tile_row][group_end - 1] = largest;
3809 }
3810 }
3811
3812 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
3813 // Launch workers for individual columns
3814 for (i = 0; i < num_workers && tile_col < tile_cols_end;
3815 ++i, ++tile_col) {
3816 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07003817 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003818 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
3819 TileInfo *const tile_info = (TileInfo *)worker->data2;
3820
3821 twd->pbi = pbi;
3822 twd->xd = pbi->mb;
3823 twd->xd.corrupted = 0;
3824 twd->xd.counts =
3825 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3826 ? &twd->counts
3827 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003828 av1_zero(twd->dqcoeff);
3829 av1_tile_init(tile_info, cm, tile_row, buf->col);
3830 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003831 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003832 &twd->bit_reader,
3833#if CONFIG_ANS && ANS_MAX_SYMBOLS
3834 1 << cm->ans_window_size_log2,
3835#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3836 pbi->decrypt_cb, pbi->decrypt_state);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003837 av1_init_macroblockd(cm, &twd->xd,
3838#if CONFIG_PVQ
3839 twd->pvq_ref_coeff,
3840#endif
3841 twd->dqcoeff);
3842#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003843 daala_dec_init(cm, &twd->xd.daala_dec, &twd->bit_reader);
Yushin Choc49ef3a2017-03-13 17:27:25 -07003844 twd->xd.daala_dec.state.adapt = &twd->tctx.pvq_context;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003845#endif
Yushin Chod767beb2017-03-24 10:15:47 -07003846#if CONFIG_EC_ADAPT
3847 // Initialise the tile context from the frame context
3848 twd->tctx = *cm->fc;
3849 twd->xd.tile_ctx = &twd->tctx;
3850#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003851#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003852 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
3853 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003854#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003855
3856 worker->had_error = 0;
3857 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
3858 winterface->execute(worker);
3859 } else {
3860 winterface->launch(worker);
3861 }
3862
3863#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3864 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
3865 final_worker = i;
3866 }
3867#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3868 }
3869
3870 // Sync all workers
3871 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003872 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003873 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07003874 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07003875 // info in cm. Additionally once the threads have been synced and an
3876 // error is detected, there's no point in continuing to decode tiles.
3877 pbi->mb.corrupted |= !winterface->sync(worker);
3878 }
3879 }
3880 }
3881
3882 // Accumulate thread frame counts.
3883 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3884 for (i = 0; i < num_workers; ++i) {
3885 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003886 av1_accumulate_frame_counts(&cm->counts, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003887 }
3888 }
3889
3890#if CONFIG_EXT_TILE
3891 // Return the end of the last tile buffer
3892 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3893#else
3894#if CONFIG_ANS
3895 return data_end;
3896#else
3897 assert(final_worker != -1);
3898 {
3899 TileWorkerData *const twd =
3900 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003901 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003902 }
3903#endif // CONFIG_ANS
3904#endif // CONFIG_EXT_TILE
3905}
3906
3907static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003908 AV1_COMMON *const cm = (AV1_COMMON *)data;
3909 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003910}
3911
Yaowu Xuf883b422016-08-30 14:01:10 -07003912static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3913 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003914 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003915 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003916 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003917 cm->bit_depth = AOM_BITS_8;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003918 }
3919
Yaowu Xuf883b422016-08-30 14:01:10 -07003920#if CONFIG_AOM_HIGHBITDEPTH
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003921 if (cm->bit_depth > AOM_BITS_8) {
3922 cm->use_highbitdepth = 1;
3923 } else {
Yaowu Xu345a22d2017-02-27 09:23:52 -08003924#if CONFIG_LOWBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003925 cm->use_highbitdepth = 0;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003926#else
3927 cm->use_highbitdepth = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003928#endif
3929 }
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003930#endif
3931
Yaowu Xuf883b422016-08-30 14:01:10 -07003932 cm->color_space = aom_rb_read_literal(rb, 3);
3933 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003934 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07003935 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003936 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003937 cm->subsampling_x = aom_rb_read_bit(rb);
3938 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003939 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003940 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003941 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07003942 if (aom_rb_read_bit(rb))
3943 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003944 "Reserved bit set");
3945 } else {
3946 cm->subsampling_y = cm->subsampling_x = 1;
3947 }
3948 } else {
3949 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
3950 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
3951 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
3952 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003953 if (aom_rb_read_bit(rb))
3954 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003955 "Reserved bit set");
3956 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003957 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003958 "4:4:4 color not supported in profile 0 or 2");
3959 }
3960 }
3961}
3962
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003963#if CONFIG_REFERENCE_BUFFER
3964void read_sequence_header(SequenceHeader *seq_params) {
3965 /* Placeholder for actually reading from the bitstream */
3966 seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
3967 seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
3968 seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
3969}
3970#endif
3971
Yaowu Xuf883b422016-08-30 14:01:10 -07003972static size_t read_uncompressed_header(AV1Decoder *pbi,
3973 struct aom_read_bit_buffer *rb) {
3974 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003975 MACROBLOCKD *const xd = &pbi->mb;
3976 BufferPool *const pool = cm->buffer_pool;
3977 RefCntBuffer *const frame_bufs = pool->frame_bufs;
3978 int i, mask, ref_index = 0;
3979 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003980
3981#if CONFIG_REFERENCE_BUFFER
3982 /* TODO: Move outside frame loop or inside key-frame branch */
3983 read_sequence_header(&pbi->seq_params);
3984#endif
3985
Yaowu Xuc27fc142016-08-22 16:08:15 -07003986 cm->last_frame_type = cm->frame_type;
3987 cm->last_intra_only = cm->intra_only;
3988
3989#if CONFIG_EXT_REFS
3990 // NOTE: By default all coded frames to be used as a reference
3991 cm->is_reference_frame = 1;
3992#endif // CONFIG_EXT_REFS
3993
Yaowu Xuf883b422016-08-30 14:01:10 -07003994 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
3995 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003996 "Invalid frame marker");
3997
Yaowu Xuf883b422016-08-30 14:01:10 -07003998 cm->profile = av1_read_profile(rb);
3999#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004000 if (cm->profile >= MAX_PROFILES)
Yaowu Xuf883b422016-08-30 14:01:10 -07004001 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004002 "Unsupported bitstream profile");
4003#else
4004 if (cm->profile >= PROFILE_2)
Yaowu Xuf883b422016-08-30 14:01:10 -07004005 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004006 "Unsupported bitstream profile");
4007#endif
4008
Yaowu Xuf883b422016-08-30 14:01:10 -07004009 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004010
4011 if (cm->show_existing_frame) {
Yaowu Xu415ba932016-12-27 11:17:32 -08004012 // Show an existing frame directly.
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004013 const int existing_frame_idx = aom_rb_read_literal(rb, 3);
4014 const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
Yaowu Xu415ba932016-12-27 11:17:32 -08004015#if CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004016 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004017 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4018 int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004019 /* Compare display_frame_id with ref_frame_id and check valid for
4020 * referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004021 if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
4022 cm->valid_for_referencing[existing_frame_idx] == 0)
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004023 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4024 "Reference buffer frame ID mismatch");
4025 }
4026#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004027 lock_buffer_pool(pool);
4028 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
4029 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07004030 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004031 "Buffer %d does not contain a decoded frame",
4032 frame_to_show);
4033 }
4034 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
4035 unlock_buffer_pool(pool);
4036
4037 cm->lf.filter_level = 0;
4038 cm->show_frame = 1;
4039 pbi->refresh_frame_flags = 0;
4040
4041 if (cm->frame_parallel_decode) {
4042 for (i = 0; i < REF_FRAMES; ++i)
4043 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
4044 }
4045
4046 return 0;
4047 }
4048
Yaowu Xuf883b422016-08-30 14:01:10 -07004049 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
4050 cm->show_frame = aom_rb_read_bit(rb);
4051 cm->error_resilient_mode = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004052#if CONFIG_REFERENCE_BUFFER
4053 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004054 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4055 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4056 int prev_frame_id = 0;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004057 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004058 prev_frame_id = cm->current_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004059 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004060 cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004061
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004062 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004063 int diff_frame_id;
4064 if (cm->current_frame_id > prev_frame_id) {
4065 diff_frame_id = cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004066 } else {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004067 diff_frame_id =
4068 (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004069 }
4070 /* Check current_frame_id for conformance */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004071 if (prev_frame_id == cm->current_frame_id ||
4072 diff_frame_id >= (1 << (frame_id_length - 1))) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004073 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4074 "Invalid value of current_frame_id");
4075 }
4076 }
4077 /* Check if some frames need to be marked as not valid for referencing */
4078 for (i = 0; i < REF_FRAMES; i++) {
4079 if (cm->frame_type == KEY_FRAME) {
4080 cm->valid_for_referencing[i] = 0;
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004081 } else if (cm->current_frame_id - (1 << diff_len) > 0) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004082 if (cm->ref_frame_id[i] > cm->current_frame_id ||
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004083 cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004084 cm->valid_for_referencing[i] = 0;
4085 } else {
4086 if (cm->ref_frame_id[i] > cm->current_frame_id &&
4087 cm->ref_frame_id[i] <
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004088 (1 << frame_id_length) + cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004089 cm->valid_for_referencing[i] = 0;
4090 }
4091 }
4092 }
4093#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004094 if (cm->frame_type == KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004095 if (!av1_read_sync_code(rb))
4096 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004097 "Invalid frame sync code");
4098
4099 read_bitdepth_colorspace_sampling(cm, rb);
4100 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
4101
4102 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4103 cm->frame_refs[i].idx = INVALID_IDX;
4104 cm->frame_refs[i].buf = NULL;
4105 }
4106
4107 setup_frame_size(cm, rb);
4108 if (pbi->need_resync) {
4109 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4110 pbi->need_resync = 0;
4111 }
Alex Converseeb780e72016-12-13 12:46:41 -08004112#if CONFIG_ANS && ANS_MAX_SYMBOLS
4113 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4114#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
Urvang Joshib100db72016-10-12 16:28:56 -07004115#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004116 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004117#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004118 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004119 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004120#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004121 if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004122#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004123 if (cm->error_resilient_mode) {
4124 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
4125 } else {
4126 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004127 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004128 ? RESET_FRAME_CONTEXT_ALL
4129 : RESET_FRAME_CONTEXT_CURRENT;
4130 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004131 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004132 ? RESET_FRAME_CONTEXT_CURRENT
4133 : RESET_FRAME_CONTEXT_NONE;
4134 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07004135 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004136 ? RESET_FRAME_CONTEXT_ALL
4137 : RESET_FRAME_CONTEXT_CURRENT;
4138 }
4139 }
4140
4141 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004142 if (!av1_read_sync_code(rb))
4143 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004144 "Invalid frame sync code");
4145
4146 read_bitdepth_colorspace_sampling(cm, rb);
4147
Yaowu Xuf883b422016-08-30 14:01:10 -07004148 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004149 setup_frame_size(cm, rb);
4150 if (pbi->need_resync) {
4151 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4152 pbi->need_resync = 0;
4153 }
Alex Converseeb780e72016-12-13 12:46:41 -08004154#if CONFIG_ANS && ANS_MAX_SYMBOLS
4155 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4156#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004157 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Yaowu Xuf883b422016-08-30 14:01:10 -07004158 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004159
4160#if CONFIG_EXT_REFS
4161 if (!pbi->refresh_frame_flags) {
4162 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
4163 // will not be used as a reference
4164 cm->is_reference_frame = 0;
4165 }
4166#endif // CONFIG_EXT_REFS
4167
4168 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004169 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004170 const int idx = cm->ref_frame_map[ref];
4171 RefBuffer *const ref_frame = &cm->frame_refs[i];
4172 ref_frame->idx = idx;
4173 ref_frame->buf = &frame_bufs[idx].buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07004174 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004175#if CONFIG_REFERENCE_BUFFER
4176 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004177 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4178 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4179 int delta_frame_id_minus1 = aom_rb_read_literal(rb, diff_len);
4180 int ref_frame_id =
4181 ((cm->current_frame_id - (delta_frame_id_minus1 + 1) +
4182 (1 << frame_id_length)) %
4183 (1 << frame_id_length));
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004184 /* Compare values derived from delta_frame_id_minus1 and
4185 * refresh_frame_flags. Also, check valid for referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004186 if (ref_frame_id != cm->ref_frame_id[ref] ||
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004187 cm->valid_for_referencing[ref] == 0)
4188 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4189 "Reference buffer frame ID mismatch");
4190 }
4191#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004192 }
4193
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004194#if CONFIG_FRAME_SIZE
4195 if (cm->error_resilient_mode == 0) {
4196 setup_frame_size_with_refs(cm, rb);
4197 } else {
4198 setup_frame_size(cm, rb);
4199 }
4200#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004201 setup_frame_size_with_refs(cm, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004202#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004203
Yaowu Xuf883b422016-08-30 14:01:10 -07004204 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Angie Chiang5678ad92016-11-21 09:38:40 -08004205 cm->interp_filter = read_frame_interp_filter(rb);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004206#if CONFIG_TEMPMV_SIGNALING
4207 if (!cm->error_resilient_mode) {
4208 cm->use_prev_frame_mvs = aom_rb_read_bit(rb);
4209 }
4210#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004211 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4212 RefBuffer *const ref_buf = &cm->frame_refs[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07004213#if CONFIG_AOM_HIGHBITDEPTH
4214 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004215 &ref_buf->sf, ref_buf->buf->y_crop_width,
4216 ref_buf->buf->y_crop_height, cm->width, cm->height,
4217 cm->use_highbitdepth);
4218#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004219 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004220 &ref_buf->sf, ref_buf->buf->y_crop_width,
4221 ref_buf->buf->y_crop_height, cm->width, cm->height);
4222#endif
4223 }
4224 }
4225 }
Fangwen Fu8d164de2016-12-14 13:40:54 -08004226#if CONFIG_TEMPMV_SIGNALING
4227 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
4228#endif
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004229
4230#if CONFIG_REFERENCE_BUFFER
4231 if (pbi->seq_params.frame_id_numbers_present_flag) {
4232 /* If bitmask is set, update reference frame id values and
4233 mark frames as valid for reference */
4234 int refresh_frame_flags =
4235 cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
4236 for (i = 0; i < REF_FRAMES; i++) {
4237 if ((refresh_frame_flags >> i) & 1) {
4238 cm->ref_frame_id[i] = cm->current_frame_id;
4239 cm->valid_for_referencing[i] = 1;
4240 }
4241 }
4242 }
4243#endif
4244
Yaowu Xuf883b422016-08-30 14:01:10 -07004245#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004246 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
4247#endif
4248 get_frame_new_buffer(cm)->color_space = cm->color_space;
4249 get_frame_new_buffer(cm)->color_range = cm->color_range;
4250 get_frame_new_buffer(cm)->render_width = cm->render_width;
4251 get_frame_new_buffer(cm)->render_height = cm->render_height;
4252
4253 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004254 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004255 "Keyframe / intra-only frame required to reset decoder"
4256 " state");
4257 }
4258
4259 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004260 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004261 ? REFRESH_FRAME_CONTEXT_FORWARD
4262 : REFRESH_FRAME_CONTEXT_BACKWARD;
4263 } else {
4264 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
4265 }
4266
Yaowu Xuf883b422016-08-30 14:01:10 -07004267 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07004268 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07004269 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004270
4271 // Generate next_ref_frame_map.
4272 lock_buffer_pool(pool);
4273 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
4274 if (mask & 1) {
4275 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
4276 ++frame_bufs[cm->new_fb_idx].ref_count;
4277 } else {
4278 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4279 }
4280 // Current thread holds the reference frame.
4281 if (cm->ref_frame_map[ref_index] >= 0)
4282 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4283 ++ref_index;
4284 }
4285
4286 for (; ref_index < REF_FRAMES; ++ref_index) {
4287 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4288
4289 // Current thread holds the reference frame.
4290 if (cm->ref_frame_map[ref_index] >= 0)
4291 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4292 }
4293 unlock_buffer_pool(pool);
4294 pbi->hold_ref_buf = 1;
4295
4296 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07004297 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004298
4299#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07004300 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004301#else
4302 set_sb_size(cm, BLOCK_64X64);
4303#endif // CONFIG_EXT_PARTITION
4304
4305 setup_loopfilter(cm, rb);
Jean-Marc Valin01435132017-02-18 14:12:53 -05004306#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01004307 setup_cdef(cm, rb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004308#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004309#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004310 decode_restoration_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004311#endif // CONFIG_LOOP_RESTORATION
4312 setup_quantization(cm, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004313#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004314 xd->bd = (int)cm->bit_depth;
4315#endif
4316
hui su0d103572017-03-01 17:58:01 -08004317#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuf883b422016-08-30 14:01:10 -07004318 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004319 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4320 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4321 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4322 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
4323 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4324 }
hui su0d103572017-03-01 17:58:01 -08004325#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004326
4327 setup_segmentation(cm, rb);
4328
Arild Fuldseth07441162016-08-15 15:07:52 +02004329#if CONFIG_DELTA_Q
4330 {
4331 struct segmentation *const seg = &cm->seg;
4332 int segment_quantizer_active = 0;
4333 for (i = 0; i < MAX_SEGMENTS; i++) {
4334 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
4335 segment_quantizer_active = 1;
4336 }
4337 }
4338
Thomas Daviesf6936102016-09-05 16:51:31 +01004339 cm->delta_q_res = 1;
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01004340 if (segment_quantizer_active == 0 && cm->base_qindex > 0) {
Arild Fuldseth07441162016-08-15 15:07:52 +02004341 cm->delta_q_present_flag = aom_rb_read_bit(rb);
4342 } else {
4343 cm->delta_q_present_flag = 0;
4344 }
4345 if (cm->delta_q_present_flag) {
4346 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01004347 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Arild Fuldseth07441162016-08-15 15:07:52 +02004348 }
4349 }
4350#endif
4351
Urvang Joshi454280d2016-10-14 16:51:44 -07004352 for (i = 0; i < MAX_SEGMENTS; ++i) {
4353 const int qindex = cm->seg.enabled
4354 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
4355 : cm->base_qindex;
4356 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
4357 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4358 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004359 }
4360
4361 setup_segmentation_dequant(cm);
Yue Cheneeacc4c2017-01-17 17:29:17 -08004362 cm->tx_mode = read_tx_mode(cm, xd, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004363 cm->reference_mode = read_frame_reference_mode(cm, rb);
4364
Sarah Parkere68a3e42017-02-16 14:03:24 -08004365#if CONFIG_EXT_TX
4366 cm->reduced_tx_set_used = aom_rb_read_bit(rb);
4367#endif // CONFIG_EXT_TX
4368
Yaowu Xuc27fc142016-08-22 16:08:15 -07004369 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004370 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004371
4372 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07004373 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004374 "Invalid header size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004375 return sz;
4376}
4377
4378#if CONFIG_EXT_TX
Thomas9ac55082016-09-23 18:04:17 +01004379#if !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuf883b422016-08-30 14:01:10 -07004380static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004381 int i, j, k;
4382 int s;
4383 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004384 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004385 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4386 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004387 for (j = 0; j < num_ext_tx_set[ext_tx_set_type_inter[s]] - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004388 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004389 }
4390 }
4391 }
4392
4393 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004394 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004395 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4396 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
4397 for (j = 0; j < INTRA_MODES; ++j)
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004398 for (k = 0; k < num_ext_tx_set[ext_tx_set_type_intra[s]] - 1; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004399 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
4400 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004401 }
4402 }
4403 }
4404}
Thomas9ac55082016-09-23 18:04:17 +01004405#endif // !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004406#else
4407
Yaowu Xuc27fc142016-08-22 16:08:15 -07004408#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004409#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07004410static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004411 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -07004412 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004413 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
Jingning Hanfeb517c2016-12-21 16:02:07 -08004414 for (j = TX_8X8; j < TX_SIZES; ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004415 av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004416 }
4417 }
4418 }
4419}
4420#endif // CONFIG_SUPERTX
4421
4422#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +00004423static void read_global_motion_params(WarpedMotionParams *params,
Sarah Parkerf1783292017-04-05 11:55:27 -07004424 WarpedMotionParams *ref_params,
Sarah Parker13d06622017-03-10 17:03:28 -08004425 aom_prob *probs, aom_reader *r,
4426 int allow_hp) {
David Barkercf3d0b02016-11-10 10:14:49 +00004427 TransformationType type =
Michael Bebenita6048d052016-08-25 14:40:54 -07004428 aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
Sarah Parker13d06622017-03-10 17:03:28 -08004429 int trans_bits;
4430 int trans_dec_factor;
Sarah Parkerf1783292017-04-05 11:55:27 -07004431 int trans_prec_diff;
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004432 set_default_gmparams(params);
David Barkercf3d0b02016-11-10 10:14:49 +00004433 params->wmtype = type;
4434 switch (type) {
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004435 case HOMOGRAPHY:
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004436 case HORTRAPEZOID:
4437 case VERTRAPEZOID:
4438 if (type != HORTRAPEZOID)
4439 params->wmmat[6] =
Sarah Parkerf1783292017-04-05 11:55:27 -07004440 aom_read_signed_primitive_refsubexpfin(
4441 r, GM_ROW3HOMO_MAX + 1, SUBEXPFIN_K,
4442 (ref_params->wmmat[6] >> GM_ROW3HOMO_PREC_DIFF)) *
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004443 GM_ROW3HOMO_DECODE_FACTOR;
4444 if (type != VERTRAPEZOID)
4445 params->wmmat[7] =
Sarah Parkerf1783292017-04-05 11:55:27 -07004446 aom_read_signed_primitive_refsubexpfin(
4447 r, GM_ROW3HOMO_MAX + 1, SUBEXPFIN_K,
4448 (ref_params->wmmat[7] >> GM_ROW3HOMO_PREC_DIFF)) *
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004449 GM_ROW3HOMO_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004450 case AFFINE:
4451 case ROTZOOM:
Sarah Parkerf1783292017-04-05 11:55:27 -07004452 params->wmmat[2] = aom_read_signed_primitive_refsubexpfin(
4453 r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4454 (ref_params->wmmat[2] >> GM_ALPHA_PREC_DIFF) -
4455 (1 << GM_ALPHA_PREC_BITS)) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004456 GM_ALPHA_DECODE_FACTOR +
David Barkercf3d0b02016-11-10 10:14:49 +00004457 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004458 if (type != VERTRAPEZOID)
Sarah Parkerf1783292017-04-05 11:55:27 -07004459 params->wmmat[3] = aom_read_signed_primitive_refsubexpfin(
4460 r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4461 (ref_params->wmmat[3] >> GM_ALPHA_PREC_DIFF)) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004462 GM_ALPHA_DECODE_FACTOR;
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004463 if (type >= AFFINE) {
4464 if (type != HORTRAPEZOID)
Sarah Parkerf1783292017-04-05 11:55:27 -07004465 params->wmmat[4] = aom_read_signed_primitive_refsubexpfin(
4466 r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4467 (ref_params->wmmat[4] >> GM_ALPHA_PREC_DIFF)) *
4468 GM_ALPHA_DECODE_FACTOR;
4469 params->wmmat[5] = aom_read_signed_primitive_refsubexpfin(
4470 r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4471 (ref_params->wmmat[5] >> GM_ALPHA_PREC_DIFF) -
4472 (1 << GM_ALPHA_PREC_BITS)) *
David Barkercf3d0b02016-11-10 10:14:49 +00004473 GM_ALPHA_DECODE_FACTOR +
4474 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004475 } else {
David Barkercf3d0b02016-11-10 10:14:49 +00004476 params->wmmat[4] = -params->wmmat[3];
4477 params->wmmat[5] = params->wmmat[2];
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004478 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004479 // fallthrough intended
David Barkercf3d0b02016-11-10 10:14:49 +00004480 case TRANSLATION:
Sarah Parker13d06622017-03-10 17:03:28 -08004481 trans_bits = (type == TRANSLATION) ? GM_ABS_TRANS_ONLY_BITS - !allow_hp
4482 : GM_ABS_TRANS_BITS;
4483 trans_dec_factor = (type == TRANSLATION)
4484 ? GM_TRANS_ONLY_DECODE_FACTOR * (1 << !allow_hp)
4485 : GM_TRANS_DECODE_FACTOR;
Sarah Parkerf1783292017-04-05 11:55:27 -07004486 trans_prec_diff = (type == TRANSLATION)
4487 ? GM_TRANS_ONLY_PREC_DIFF + !allow_hp
4488 : GM_TRANS_PREC_DIFF;
4489 params->wmmat[0] = aom_read_signed_primitive_refsubexpfin(
4490 r, (1 << trans_bits) + 1, SUBEXPFIN_K,
4491 (ref_params->wmmat[0] >> trans_prec_diff)) *
4492 trans_dec_factor;
4493 params->wmmat[1] = aom_read_signed_primitive_refsubexpfin(
4494 r, (1 << trans_bits) + 1, SUBEXPFIN_K,
4495 (ref_params->wmmat[1] >> trans_prec_diff)) *
4496 trans_dec_factor;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004497 case IDENTITY: break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004498 default: assert(0);
4499 }
Debargha Mukherjee3b6c5442017-03-30 08:22:00 -07004500 if (params->wmtype <= AFFINE)
4501 if (!get_shear_params(params)) assert(0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004502}
4503
Yaowu Xuf883b422016-08-30 14:01:10 -07004504static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004505 int frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004506 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
Sarah Parkerf1783292017-04-05 11:55:27 -07004507 read_global_motion_params(
4508 &cm->global_motion[frame], &cm->prev_frame->global_motion[frame],
4509 cm->fc->global_motion_types_prob, r, cm->allow_high_precision_mv);
Sarah Parkere5299862016-08-16 14:57:37 -07004510 /*
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004511 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4512 frame, cm->current_video_frame, cm->show_frame,
David Barkercf3d0b02016-11-10 10:14:49 +00004513 cm->global_motion[frame].wmmat[0],
4514 cm->global_motion[frame].wmmat[1],
4515 cm->global_motion[frame].wmmat[2],
4516 cm->global_motion[frame].wmmat[3]);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004517 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004518 }
Sarah Parkerf1783292017-04-05 11:55:27 -07004519 memcpy(cm->cur_frame->global_motion, cm->global_motion,
4520 TOTAL_REFS_PER_FRAME * sizeof(WarpedMotionParams));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004521}
4522#endif // CONFIG_GLOBAL_MOTION
4523
Yaowu Xuf883b422016-08-30 14:01:10 -07004524static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004525 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004526 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004527#if CONFIG_SUPERTX
4528 MACROBLOCKD *const xd = &pbi->mb;
4529#endif
4530 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07004531 aom_reader r;
Yaowu Xu8af861b2016-11-01 12:12:11 -07004532 int k, i;
Thomas Davies493623e2017-03-31 16:12:25 +01004533#if !CONFIG_EC_ADAPT || \
4534 (CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION || CONFIG_EXT_INTER)
Yaowu Xu8af861b2016-11-01 12:12:11 -07004535 int j;
4536#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004537
Alex Converse2cdf0d82016-12-13 13:53:09 -08004538#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08004539 r.window_size = 1 << cm->ans_window_size_log2;
Alex Converse2cdf0d82016-12-13 13:53:09 -08004540#endif
Alex Converse346440b2017-01-03 13:47:37 -08004541 if (aom_reader_init(&r, data, partition_size, pbi->decrypt_cb,
4542 pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07004543 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004544 "Failed to allocate bool decoder 0");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004545
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004546#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004547 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4548 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4549 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4550 av1_alloc_restoration_buffers(cm);
4551 decode_restoration(cm, &r);
4552 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004553#endif
4554
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004555#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -07004556 if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004557#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004558
Angie Chiang800df032017-03-22 11:14:12 -07004559#if CONFIG_LV_MAP
4560 av1_read_txb_probs(fc, cm->tx_mode, &r);
4561#else // CONFIG_LV_MAP
Yushin Cho77bba8d2016-11-04 16:36:56 -07004562#if !CONFIG_PVQ
Alex Conversea9598cd2017-02-03 14:18:05 -08004563#if !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004564 read_coef_probs(fc, cm->tx_mode, &r);
Angie Chiang800df032017-03-22 11:14:12 -07004565#endif // !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Angie Chiang7d7ead92017-03-22 10:35:51 -07004566#endif // !CONFIG_PVQ
Angie Chiang800df032017-03-22 11:14:12 -07004567#endif // CONFIG_LV_MAP
4568
Yaowu Xuc27fc142016-08-22 16:08:15 -07004569#if CONFIG_VAR_TX
4570 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004571 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
Yushin Cho77bba8d2016-11-04 16:36:56 -07004572#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004573 for (k = 0; k < SKIP_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004574 av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004575
Thomas Daviesd6ee8a82017-03-02 14:42:50 +00004576#if CONFIG_DELTA_Q && !CONFIG_EC_ADAPT
Thomas Davies665cd702017-03-02 10:20:30 +00004577 for (k = 0; k < DELTA_Q_PROBS; ++k)
Thomas Daviesf6936102016-09-05 16:51:31 +01004578 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
4579#endif
4580
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004581#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004582 if (cm->seg.enabled && cm->seg.update_map) {
4583 if (cm->seg.temporal_update) {
4584 for (k = 0; k < PREDICTION_PROBS; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004585 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004586 }
4587 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004588 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004589 }
4590
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004591 for (j = 0; j < INTRA_MODES; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004592 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004593 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004594 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004595
4596#if CONFIG_EXT_PARTITION_TYPES
Alex Converse4e18d402017-03-14 15:36:38 -07004597 for (j = 0; j < PARTITION_PLOFFSET; ++j)
4598 for (i = 0; i < PARTITION_TYPES - 1; ++i)
4599 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
4600 for (; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004601 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004602 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004603#else
Alex Converse55c6bde2017-01-12 15:55:31 -08004604 for (j = 0; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004605 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004606 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004607#endif // CONFIG_EXT_PARTITION_TYPES
hui su9aa97492017-01-26 16:46:01 -08004608
Alex Converse55c6bde2017-01-12 15:55:31 -08004609#if CONFIG_UNPOISON_PARTITION_CTX
4610 for (; j < PARTITION_CONTEXTS_PRIMARY + PARTITION_BLOCK_SIZES; ++j)
4611 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_VERT], ACCT_STR);
4612 for (; j < PARTITION_CONTEXTS_PRIMARY + 2 * PARTITION_BLOCK_SIZES; ++j)
4613 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_HORZ], ACCT_STR);
4614#endif // CONFIG_UNPOISON_PARTITION_CTX
hui su9aa97492017-01-26 16:46:01 -08004615
4616#if CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004617 for (i = 0; i < INTRA_FILTERS + 1; ++i)
4618 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004619 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
hui su9aa97492017-01-26 16:46:01 -08004620#endif // CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
hui sub4e25d22017-03-09 15:32:30 -08004621#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004622
4623 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004624 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Nathan E. Egge10ba2be2016-11-16 09:44:26 -05004625#if CONFIG_EC_MULTISYMBOL
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00004626 av1_copy(cm->fc->kf_y_cdf, av1_kf_y_mode_cdf);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004627#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004628#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004629 for (k = 0; k < INTRA_MODES; k++)
Thomas Davies6519beb2016-10-19 14:46:07 +01004630 for (j = 0; j < INTRA_MODES; j++)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004631 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004632 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004633#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004634 } else {
4635#if !CONFIG_REF_MV
4636 nmv_context *const nmvc = &fc->nmvc;
4637#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004638 read_inter_mode_probs(fc, &r);
4639
4640#if CONFIG_EXT_INTER
4641 read_inter_compound_mode_probs(fc, &r);
4642 if (cm->reference_mode != COMPOUND_REFERENCE) {
4643 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4644 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004645 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004646 }
4647 }
4648 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4649 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004650 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004651 }
4652 for (i = 0; i < BLOCK_SIZES; i++) {
4653 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004654 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004655 }
4656 }
4657 }
4658 if (cm->reference_mode != SINGLE_REFERENCE) {
4659 for (i = 0; i < BLOCK_SIZES; i++) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08004660 for (j = 0; j < COMPOUND_TYPES - 1; j++) {
4661 av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004662 }
4663 }
4664 }
4665#endif // CONFIG_EXT_INTER
4666
Yue Chencb60b182016-10-13 15:18:22 -07004667#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004668 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
Yue Chencb60b182016-10-13 15:18:22 -07004669 for (j = 0; j < MOTION_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004670 av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004671 }
Yue Chencb60b182016-10-13 15:18:22 -07004672#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004673
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004674#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004675 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004676#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004677
4678 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004679 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004680
4681 if (cm->reference_mode != SINGLE_REFERENCE)
4682 setup_compound_reference_mode(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004683 read_frame_reference_mode_probs(cm, &r);
4684
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004685#if !CONFIG_EC_ADAPT
Nathan E. Egge5710c722016-09-08 10:01:16 -04004686 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004687 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004688 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
Nathan E. Egge5710c722016-09-08 10:01:16 -04004689 }
Thomas9ac55082016-09-23 18:04:17 +01004690#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004691
4692#if CONFIG_REF_MV
4693 for (i = 0; i < NMV_CONTEXTS; ++i)
4694 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
4695#else
4696 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
4697#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004698#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004699 read_ext_tx_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004700#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004701#if CONFIG_SUPERTX
4702 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
4703#endif
4704#if CONFIG_GLOBAL_MOTION
4705 read_global_motion(cm, &r);
Thomas Davies6519beb2016-10-19 14:46:07 +01004706#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004707 }
Thomas Davies028b57f2017-02-22 16:42:11 +00004708#if CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Thomas Davies87aeeb82017-02-17 00:19:40 +00004709#if CONFIG_NEW_TOKENSET
4710 av1_coef_head_cdfs(fc);
4711#endif
4712 /* Make tail distribution from head */
Thomas Davies6519beb2016-10-19 14:46:07 +01004713 av1_coef_pareto_cdfs(fc);
David Barker599dfd02016-11-10 13:20:12 +00004714#if CONFIG_REF_MV
4715 for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
4716#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004717 av1_set_mv_cdfs(&fc->nmvc);
David Barker599dfd02016-11-10 13:20:12 +00004718#endif
Thomas Davies6519beb2016-10-19 14:46:07 +01004719 av1_set_mode_cdfs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00004720#endif // CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004721
Yaowu Xuf883b422016-08-30 14:01:10 -07004722 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004723}
4724
4725#ifdef NDEBUG
4726#define debug_check_frame_counts(cm) (void)0
4727#else // !NDEBUG
4728// Counts should only be incremented when frame_parallel_decoding_mode and
4729// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07004730static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004731 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07004732 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004733 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
4734 cm->error_resilient_mode);
4735 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
4736 sizeof(cm->counts.y_mode)));
4737 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
4738 sizeof(cm->counts.uv_mode)));
4739 assert(!memcmp(cm->counts.partition, zero_counts.partition,
4740 sizeof(cm->counts.partition)));
4741 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
4742 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
4743 sizeof(cm->counts.eob_branch)));
Thomas Daviesab780672017-02-01 12:07:29 +00004744#if CONFIG_EC_MULTISYMBOL
4745 assert(!memcmp(cm->counts.blockz_count, zero_counts.blockz_count,
4746 sizeof(cm->counts.blockz_count)));
4747#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004748 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
4749 sizeof(cm->counts.switchable_interp)));
4750 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
4751 sizeof(cm->counts.inter_mode)));
4752#if CONFIG_EXT_INTER
4753 assert(!memcmp(cm->counts.inter_compound_mode,
4754 zero_counts.inter_compound_mode,
4755 sizeof(cm->counts.inter_compound_mode)));
4756 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
4757 sizeof(cm->counts.interintra)));
4758 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
4759 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08004760 assert(!memcmp(cm->counts.compound_interinter,
4761 zero_counts.compound_interinter,
4762 sizeof(cm->counts.compound_interinter)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004763#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07004764#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
4765 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
4766 sizeof(cm->counts.motion_mode)));
4767#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004768 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
4769 sizeof(cm->counts.intra_inter)));
4770 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
4771 sizeof(cm->counts.comp_inter)));
4772 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
4773 sizeof(cm->counts.single_ref)));
4774 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
4775 sizeof(cm->counts.comp_ref)));
4776#if CONFIG_EXT_REFS
4777 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
4778 sizeof(cm->counts.comp_bwdref)));
4779#endif // CONFIG_EXT_REFS
4780 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
4781 sizeof(cm->counts.tx_size)));
4782 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
4783#if CONFIG_REF_MV
4784 assert(
4785 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
4786 assert(
4787 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
4788#else
4789 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
4790#endif
4791 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
4792 sizeof(cm->counts.inter_ext_tx)));
4793 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
4794 sizeof(cm->counts.intra_ext_tx)));
4795}
4796#endif // NDEBUG
4797
Yaowu Xuf883b422016-08-30 14:01:10 -07004798static struct aom_read_bit_buffer *init_read_bit_buffer(
4799 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
4800 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004801 rb->bit_offset = 0;
4802 rb->error_handler = error_handler;
4803 rb->error_handler_data = &pbi->common;
4804 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004805 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004806 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
4807 rb->bit_buffer = clear_data;
4808 rb->bit_buffer_end = clear_data + n;
4809 } else {
4810 rb->bit_buffer = data;
4811 rb->bit_buffer_end = data_end;
4812 }
4813 return rb;
4814}
4815
4816//------------------------------------------------------------------------------
4817
Yaowu Xuf883b422016-08-30 14:01:10 -07004818int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
4819 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
4820 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
4821 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004822}
4823
Yaowu Xuf883b422016-08-30 14:01:10 -07004824void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
4825 int *height) {
4826 *width = aom_rb_read_literal(rb, 16) + 1;
4827 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004828}
4829
Yaowu Xuf883b422016-08-30 14:01:10 -07004830BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
4831 int profile = aom_rb_read_bit(rb);
4832 profile |= aom_rb_read_bit(rb) << 1;
4833 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004834 return (BITSTREAM_PROFILE)profile;
4835}
4836
Thomas Davies028b57f2017-02-22 16:42:11 +00004837#if CONFIG_EC_ADAPT
4838static void make_update_tile_list_dec(AV1Decoder *pbi, const int tile_rows,
4839 const int tile_cols,
4840 FRAME_CONTEXT *ec_ctxs[]) {
4841 int i;
4842 for (i = 0; i < tile_rows * tile_cols; ++i)
4843 ec_ctxs[i] = &pbi->tile_data[i].tctx;
4844}
4845#endif
4846
Yaowu Xuf883b422016-08-30 14:01:10 -07004847void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
4848 const uint8_t *data_end, const uint8_t **p_data_end) {
4849 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004850 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07004851 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004852 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004853 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004854 size_t first_partition_size;
4855 YV12_BUFFER_CONFIG *new_fb;
4856
4857#if CONFIG_BITSTREAM_DEBUG
4858 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
4859#endif
4860
4861 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004862 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Thomas Davies72712e62016-11-09 12:17:51 +00004863#if CONFIG_TILE_GROUPS
4864 pbi->first_partition_size = first_partition_size;
4865 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
4866#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004867 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004868 xd->cur_buf = new_fb;
4869#if CONFIG_GLOBAL_MOTION
Sarah Parkerf1783292017-04-05 11:55:27 -07004870 int i;
4871 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
4872 set_default_gmparams(&cm->global_motion[i]);
4873 set_default_gmparams(&cm->cur_frame->global_motion[i]);
4874 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004875 xd->global_motion = cm->global_motion;
4876#endif // CONFIG_GLOBAL_MOTION
4877
4878 if (!first_partition_size) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004879 // showing a frame directly
4880 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004881 return;
4882 }
4883
Yaowu Xuf883b422016-08-30 14:01:10 -07004884 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004885 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07004886 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004887 "Truncated packet or corrupt header length");
4888
Jingning Han24e0a182016-11-20 22:34:12 -08004889#if CONFIG_REF_MV
Dengca8d24d2016-10-17 14:06:35 +08004890 cm->setup_mi(cm);
4891#endif
4892
Fangwen Fu8d164de2016-12-14 13:40:54 -08004893#if CONFIG_TEMPMV_SIGNALING
4894 if (cm->use_prev_frame_mvs) {
4895 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4896 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4897 assert(!cm->error_resilient_mode &&
4898 cm->width == last_fb_ref_buf->buf->y_width &&
4899 cm->height == last_fb_ref_buf->buf->y_height &&
4900 !cm->prev_frame->intra_only);
4901 }
4902#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004903 cm->use_prev_frame_mvs =
4904 !cm->error_resilient_mode && cm->width == cm->last_width &&
4905 cm->height == cm->last_height && !cm->last_intra_only &&
4906 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004907#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004908#if CONFIG_EXT_REFS
4909 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
4910 // show_exisiting_frame=1, nor can it take a frame not used as
4911 // a reference, it is probable that by the time it is being
4912 // referred to, the frame buffer it originally points to may
4913 // already get expired and have been reassigned to the current
4914 // newly coded frame. Hence, we need to check whether this is
4915 // the case, and if yes, we have 2 choices:
4916 // (1) Simply disable the use of previous frame mvs; or
4917 // (2) Have cm->prev_frame point to one reference frame buffer,
4918 // e.g. LAST_FRAME.
4919 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
4920 // Reassign the LAST_FRAME buffer to cm->prev_frame.
4921 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4922 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4923 }
4924#endif // CONFIG_EXT_REFS
4925
Yaowu Xuf883b422016-08-30 14:01:10 -07004926 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004927
4928 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
4929 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07004930 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004931 "Uninitialized entropy context.");
4932
Yaowu Xuf883b422016-08-30 14:01:10 -07004933 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004934
4935 xd->corrupted = 0;
4936 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
4937 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07004938 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004939 "Decode failed. Frame data header is corrupted.");
4940
4941 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004942 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004943 }
4944
4945 // If encoded in frame parallel mode, frame context is ready after decoding
4946 // the frame header.
4947 if (cm->frame_parallel_decode &&
4948 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004949 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004950 FrameWorkerData *const frame_worker_data = worker->data1;
4951 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
4952 context_updated = 1;
4953 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4954 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004955 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004956 pbi->cur_buf->row = -1;
4957 pbi->cur_buf->col = -1;
4958 frame_worker_data->frame_context_ready = 1;
4959 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07004960 av1_frameworker_signal_stats(worker);
4961 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004962 }
4963
hui su0d103572017-03-01 17:58:01 -08004964#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07004965 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004966 cm->coef_probs_update_idx = 0;
hui su0d103572017-03-01 17:58:01 -08004967#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004968
Jingning Han52ece882017-04-07 14:58:25 -07004969 if (pbi->max_threads > 1 && !CONFIG_CB4X4 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004970#if CONFIG_EXT_TILE
Jingning Han52ece882017-04-07 14:58:25 -07004971 pbi->dec_tile_col < 0 && // Decoding all columns
Yaowu Xuc27fc142016-08-22 16:08:15 -07004972#endif // CONFIG_EXT_TILE
Jingning Han52ece882017-04-07 14:58:25 -07004973 cm->tile_cols > 1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004974 // Multi-threaded tile decoder
4975 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
4976 if (!xd->corrupted) {
4977 if (!cm->skip_loop_filter) {
4978 // If multiple threads are used to decode tiles, then we use those
4979 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07004980 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
4981 0, 0, pbi->tile_workers, pbi->num_tile_workers,
4982 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004983 }
4984 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004985 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004986 "Decode failed. Frame data is corrupted.");
4987 }
4988 } else {
4989 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
4990 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004991
Jean-Marc Valin01435132017-02-18 14:12:53 -05004992#if CONFIG_CDEF
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04004993 if (!cm->skip_loop_filter) {
Jean-Marc Valine9f77422017-03-22 17:09:51 -04004994 av1_cdef_frame(&pbi->cur_buf->buf, cm, &pbi->mb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004995 }
Debargha Mukherjee00c54332017-03-03 15:44:17 -08004996#endif // CONFIG_CDEF
4997
4998#if CONFIG_LOOP_RESTORATION
4999 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
5000 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
5001 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
5002 av1_loop_restoration_frame(new_fb, cm, cm->rst_info, 7, 0, NULL);
5003 }
5004#endif // CONFIG_LOOP_RESTORATION
Thomas Daedef56859f2016-04-19 16:57:24 -07005005
Yaowu Xuc27fc142016-08-22 16:08:15 -07005006 if (!xd->corrupted) {
5007 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Thomas Davies028b57f2017-02-22 16:42:11 +00005008#if CONFIG_EC_ADAPT
Thomas Davies493623e2017-03-31 16:12:25 +01005009 FRAME_CONTEXT **tile_ctxs = aom_malloc(cm->tile_rows * cm->tile_cols *
5010 sizeof(&pbi->tile_data[0].tctx));
5011 aom_cdf_prob **cdf_ptrs =
5012 aom_malloc(cm->tile_rows * cm->tile_cols *
5013 sizeof(&pbi->tile_data[0].tctx.partition_cdf[0][0]));
Thomas Davies028b57f2017-02-22 16:42:11 +00005014 make_update_tile_list_dec(pbi, cm->tile_rows, cm->tile_cols, tile_ctxs);
5015#endif
5016
hui su0d103572017-03-01 17:58:01 -08005017#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07005018 cm->partial_prob_update = 0;
hui su0d103572017-03-01 17:58:01 -08005019#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07005020 av1_adapt_coef_probs(cm);
5021 av1_adapt_intra_frame_probs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00005022#if CONFIG_EC_ADAPT
Thomas Davies493623e2017-03-31 16:12:25 +01005023 av1_average_tile_coef_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005024 cm->tile_rows * cm->tile_cols);
Thomas Davies493623e2017-03-31 16:12:25 +01005025 av1_average_tile_intra_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005026 cm->tile_rows * cm->tile_cols);
Yushin Chob188ea12017-03-13 13:45:23 -07005027#if CONFIG_PVQ
5028 av1_average_tile_pvq_cdfs(pbi->common.fc, tile_ctxs,
5029 cm->tile_rows * cm->tile_cols);
5030#endif // CONFIG_PVQ
5031#endif // CONFIG_EC_ADAPT
hui suff0da2b2017-03-07 15:51:37 -08005032#if CONFIG_ADAPT_SCAN
5033 av1_adapt_scan_order(cm);
5034#endif // CONFIG_ADAPT_SCAN
Yaowu Xuc27fc142016-08-22 16:08:15 -07005035
5036 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005037 av1_adapt_inter_frame_probs(cm);
5038 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Thomas Davies028b57f2017-02-22 16:42:11 +00005039#if CONFIG_EC_ADAPT
5040 av1_average_tile_inter_cdfs(&pbi->common, pbi->common.fc, tile_ctxs,
Thomas Davies493623e2017-03-31 16:12:25 +01005041 cdf_ptrs, cm->tile_rows * cm->tile_cols);
5042 av1_average_tile_mv_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005043 cm->tile_rows * cm->tile_cols);
5044#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005045 }
Thomas Davies493623e2017-03-31 16:12:25 +01005046#if CONFIG_EC_ADAPT
5047 aom_free(tile_ctxs);
5048 aom_free(cdf_ptrs);
5049#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005050 } else {
5051 debug_check_frame_counts(cm);
5052 }
5053 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005054 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005055 "Decode failed. Frame data is corrupted.");
5056 }
5057
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05005058#if CONFIG_INSPECTION
5059 if (pbi->inspect_cb != NULL) {
5060 (*pbi->inspect_cb)(pbi, pbi->inspect_ctx);
5061 }
5062#endif
5063
Yaowu Xuc27fc142016-08-22 16:08:15 -07005064 // Non frame parallel update frame context here.
5065 if (!cm->error_resilient_mode && !context_updated)
5066 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
5067}