blob: 35de101b3929b5e262bcfbacd673716fbea95ad4 [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] =
Nathan E. Egge89f58762016-12-28 16:31:50 -0500399 has_dc_skip + generic_decode(dec->r, &dec->state.adapt.model_dc[pli],
400 -1, &dec->state.adapt.ex_dc[pli][bs][0], 2,
401 "dc:mag");
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800402 if (out_int32[0]) out_int32[0] *= aom_read_bit(dec->r, "dc:sign") ? -1 : 1;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700403 }
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800404 out_int32[0] = out_int32[0] * pvq_dc_quant + ref_int32[0];
405
406 // copy int32 result back to int16
407 assert(OD_COEFF_SHIFT > coeff_shift);
408 rounding_mask = (1 << (OD_COEFF_SHIFT - coeff_shift - 1)) - 1;
409 for (i = 0; i < blk_size * blk_size; i++) {
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,
430 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
Alex Converse8aca36d2017-01-31 12:33:15 -0800507static void predict_and_reconstruct_intra_block(
508 AV1_COMMON *cm, MACROBLOCKD *const xd, aom_reader *const r,
509 MB_MODE_INFO *const mbmi, int plane, int row, int col, TX_SIZE tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500510 PLANE_TYPE plane_type = get_plane_type(plane);
Urvang Joshifeb925f2016-12-05 10:37:29 -0800511 const int block_idx = (row << 1) + col;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700512#if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -0700513 (void)r;
514#endif
Angie Chiang3d005e42017-04-02 16:31:35 -0700515 av1_predict_intra_block_facade(xd, plane, block_idx, col, row, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700516
517 if (!mbmi->skip) {
518 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700519#if !CONFIG_PVQ
Angie Chiang3d005e42017-04-02 16:31:35 -0700520 struct macroblockd_plane *const pd = &xd->plane[plane];
Angie Chiang133733c2017-03-17 12:50:20 -0700521#if CONFIG_LV_MAP
522 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700523 int eob;
524 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane,
525 pd->dqcoeff, &max_scan_line, &eob);
Angie Chiang133733c2017-03-17 12:50:20 -0700526#else // CONFIG_LV_MAP
Angie Chiangff6d8902016-10-21 11:02:09 -0700527 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 0);
Jingning Han1be18782016-10-21 11:48:15 -0700528 int16_t max_scan_line = 0;
529 const int eob =
Angie Chiang5c0568a2017-03-21 16:00:39 -0700530 av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
Jingning Han1be18782016-10-21 11:48:15 -0700531 tx_type, &max_scan_line, r, mbmi->segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700532#endif // CONFIG_LV_MAP
Angie Chiang3d005e42017-04-02 16:31:35 -0700533 if (eob) {
534 uint8_t *dst =
535 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Jingning Han1be18782016-10-21 11:48:15 -0700536 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
537 max_scan_line, eob);
Angie Chiang3d005e42017-04-02 16:31:35 -0700538 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700539#else
ltrudeaue1c09292017-01-20 15:42:13 -0500540 av1_pvq_decode_helper2(cm, xd, mbmi, plane, row, col, tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700541#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700542 }
543}
544
Jingning Handddb21f2017-02-28 14:44:05 -0800545#if CONFIG_VAR_TX && !CONFIG_COEF_INTERLEAVE
Angie Chiangff6d8902016-10-21 11:02:09 -0700546static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
547 aom_reader *r, MB_MODE_INFO *const mbmi,
Jingning Han8fd62b72016-10-21 12:55:54 -0700548 int plane, BLOCK_SIZE plane_bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700549 int blk_row, int blk_col, TX_SIZE tx_size,
550 int *eob_total) {
551 const struct macroblockd_plane *const pd = &xd->plane[plane];
552 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
553 const int tx_row = blk_row >> (1 - pd->subsampling_y);
554 const int tx_col = blk_col >> (1 - pd->subsampling_x);
555 const TX_SIZE plane_tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700556 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700557 : mbmi->inter_tx_size[tx_row][tx_col];
Jingning Han5f614262016-10-27 14:27:43 -0700558 // Scale to match transform block unit.
Jingning Hanf64062f2016-11-02 16:22:18 -0700559 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
560 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700561
562 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
563
564 if (tx_size == plane_tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500565 PLANE_TYPE plane_type = get_plane_type(plane);
Jingning Han8fd62b72016-10-21 12:55:54 -0700566 int block_idx = (blk_row << 1) + blk_col;
567 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiang133733c2017-03-17 12:50:20 -0700568#if CONFIG_LV_MAP
569 (void)segment_id;
570 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700571 int eob;
572 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane,
573 pd->dqcoeff, &max_scan_line, &eob);
Angie Chiang133733c2017-03-17 12:50:20 -0700574#else // CONFIG_LV_MAP
Angie Chiangff6d8902016-10-21 11:02:09 -0700575 const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700576 int16_t max_scan_line = 0;
Angie Chiang5c0568a2017-03-21 16:00:39 -0700577 const int eob = av1_decode_block_tokens(
578 cm, xd, plane, sc, blk_col, blk_row, plane_tx_size, tx_type,
579 &max_scan_line, r, mbmi->segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700580#endif // CONFIG_LV_MAP
Jingning Han9ca05b72017-01-03 14:41:36 -0800581 inverse_transform_block(xd, plane, tx_type, plane_tx_size,
582 &pd->dst.buf[(blk_row * pd->dst.stride + blk_col)
583 << tx_size_wide_log2[0]],
584 pd->dst.stride, max_scan_line, eob);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700585 *eob_total += eob;
586 } else {
Jingning Hanf64062f2016-11-02 16:22:18 -0700587 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
588 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700589 int i;
590
591 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700592
593 for (i = 0; i < 4; ++i) {
Jingning Han5f614262016-10-27 14:27:43 -0700594 const int offsetr = blk_row + (i >> 1) * bsl;
595 const int offsetc = blk_col + (i & 0x01) * bsl;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700596
597 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
598
Jingning Han8fd62b72016-10-21 12:55:54 -0700599 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
Jingning Hanf64062f2016-11-02 16:22:18 -0700600 offsetc, sub_txs, eob_total);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700601 }
602 }
603}
604#endif // CONFIG_VAR_TX
605
Jingning Handddb21f2017-02-28 14:44:05 -0800606#if !CONFIG_VAR_TX || CONFIG_SUPERTX || CONFIG_COEF_INTERLEAVE || \
Jingning Hanfe45b212016-11-22 10:30:23 -0800607 (!CONFIG_VAR_TX && CONFIG_EXT_TX && CONFIG_RECT_TX)
Angie Chiangff6d8902016-10-21 11:02:09 -0700608static int reconstruct_inter_block(AV1_COMMON *cm, MACROBLOCKD *const xd,
Alex Converse8aca36d2017-01-31 12:33:15 -0800609 aom_reader *const r, int segment_id,
610 int plane, int row, int col,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700611 TX_SIZE tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500612 PLANE_TYPE plane_type = get_plane_type(plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700613 int block_idx = (row << 1) + col;
614 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700615#if CONFIG_PVQ
616 int eob;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700617 (void)r;
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800618 (void)segment_id;
619#else
620 struct macroblockd_plane *const pd = &xd->plane[plane];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700621#endif
622
623#if !CONFIG_PVQ
Angie Chiang133733c2017-03-17 12:50:20 -0700624#if CONFIG_LV_MAP
625 (void)segment_id;
Jingning Han1be18782016-10-21 11:48:15 -0700626 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700627 int eob;
628 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane, pd->dqcoeff,
629 &max_scan_line, &eob);
Angie Chiang133733c2017-03-17 12:50:20 -0700630#else // CONFIG_LV_MAP
631 int16_t max_scan_line = 0;
632 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700633 const int eob =
Angie Chiang5c0568a2017-03-21 16:00:39 -0700634 av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
635 tx_type, &max_scan_line, r, segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700636#endif // CONFIG_LV_MAP
Jingning Hanca14dda2016-12-09 09:36:00 -0800637 uint8_t *dst =
638 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Jingning Han1be18782016-10-21 11:48:15 -0700639 if (eob)
Jingning Hanca14dda2016-12-09 09:36:00 -0800640 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
641 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700642#else
ltrudeaue1c09292017-01-20 15:42:13 -0500643 eob = av1_pvq_decode_helper2(cm, xd, &xd->mi[0]->mbmi, plane, row, col,
644 tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700645#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700646 return eob;
647}
648#endif // !CONFIG_VAR_TX || CONFIG_SUPER_TX
649
Angie Chiang44701f22017-02-27 10:36:44 -0800650static void set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
651 BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
652 int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700653 const int offset = mi_row * cm->mi_stride + mi_col;
654 int x, y;
655 const TileInfo *const tile = &xd->tile;
656
657 xd->mi = cm->mi_grid_visible + offset;
658 xd->mi[0] = &cm->mi[offset];
659 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
660 // passing bsize from decode_partition().
661 xd->mi[0]->mbmi.sb_type = bsize;
Angie Chiang394c3372016-11-03 11:13:15 -0700662#if CONFIG_RD_DEBUG
663 xd->mi[0]->mbmi.mi_row = mi_row;
664 xd->mi[0]->mbmi.mi_col = mi_col;
665#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700666 for (y = 0; y < y_mis; ++y)
Jingning Han97d85482016-07-15 11:06:05 -0700667 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700668
Jingning Hanfaad0e12016-12-07 10:54:57 -0800669 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700670 set_skip_context(xd, mi_row, mi_col);
671
672#if CONFIG_VAR_TX
673 xd->max_tx_size = max_txsize_lookup[bsize];
674#endif
675
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800676// Distance of Mb to the various image edges. These are specified to 8th pel
677// as they are always compared to values that are in 1/8th pel units
678#if CONFIG_DEPENDENT_HORZTILES
679 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
680 cm->dependent_horz_tiles);
681#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700682 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 -0800683#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700684
Yaowu Xuf883b422016-08-30 14:01:10 -0700685 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700686}
687
688#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700689static MB_MODE_INFO *set_offsets_extend(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700690 MACROBLOCKD *const xd,
691 const TileInfo *const tile,
692 BLOCK_SIZE bsize_pred, int mi_row_pred,
693 int mi_col_pred, int mi_row_ori,
694 int mi_col_ori) {
695 // Used in supertx
696 // (mi_row_ori, mi_col_ori): location for mv
697 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
Jingning Han93531242016-12-20 11:54:36 -0800698 const int bw = mi_size_wide[bsize_pred];
699 const int bh = mi_size_high[bsize_pred];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700700 const int offset = mi_row_ori * cm->mi_stride + mi_col_ori;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700701 xd->mi = cm->mi_grid_visible + offset;
702 xd->mi[0] = cm->mi + offset;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800703#if CONFIG_DEPENDENT_HORZTILES
704 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
705 cm->mi_cols, cm->dependent_horz_tiles);
706#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700707 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
708 cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800709#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700710
711 xd->up_available = (mi_row_ori > tile->mi_row_start);
712 xd->left_available = (mi_col_ori > tile->mi_col_start);
713
Jingning Hanfaad0e12016-12-07 10:54:57 -0800714 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700715
716 return &xd->mi[0]->mbmi;
717}
718
Angie Chiang7fcfee42017-02-24 15:51:03 -0800719#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700720static MB_MODE_INFO *set_mb_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
721 BLOCK_SIZE bsize, int mi_row, int mi_col,
722 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700723 const int offset = mi_row * cm->mi_stride + mi_col;
724 const TileInfo *const tile = &xd->tile;
725 int x, y;
726
727 xd->mi = cm->mi_grid_visible + offset;
728 xd->mi[0] = cm->mi + offset;
729 xd->mi[0]->mbmi.sb_type = bsize;
730 for (y = 0; y < y_mis; ++y)
731 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
732
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800733#if CONFIG_DEPENDENT_HORZTILES
734 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
735 cm->dependent_horz_tiles);
736#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700737 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 -0800738#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700739 return &xd->mi[0]->mbmi;
740}
Angie Chiang7fcfee42017-02-24 15:51:03 -0800741#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700742
Yaowu Xuf883b422016-08-30 14:01:10 -0700743static void set_offsets_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700744 const TileInfo *const tile, BLOCK_SIZE bsize,
745 int mi_row, int mi_col) {
Jingning Han93531242016-12-20 11:54:36 -0800746 const int bw = mi_size_wide[bsize];
747 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700748 const int offset = mi_row * cm->mi_stride + mi_col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700749
750 xd->mi = cm->mi_grid_visible + offset;
751 xd->mi[0] = cm->mi + offset;
752
Jingning Hanfaad0e12016-12-07 10:54:57 -0800753 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700754
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800755#if CONFIG_DEPENDENT_HORZTILES
756 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
757 cm->dependent_horz_tiles);
758#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700759 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 -0800760#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700761
Yaowu Xuf883b422016-08-30 14:01:10 -0700762 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700763}
764
Yaowu Xuf883b422016-08-30 14:01:10 -0700765static void set_param_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700766 BLOCK_SIZE bsize, int mi_row, int mi_col,
767 int txfm, int skip) {
Jingning Han93531242016-12-20 11:54:36 -0800768 const int bw = mi_size_wide[bsize];
769 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -0700770 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
771 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700772 const int offset = mi_row * cm->mi_stride + mi_col;
773 int x, y;
774
775 xd->mi = cm->mi_grid_visible + offset;
776 xd->mi[0] = cm->mi + offset;
777
778 for (y = 0; y < y_mis; ++y)
779 for (x = 0; x < x_mis; ++x) {
780 xd->mi[y * cm->mi_stride + x]->mbmi.skip = skip;
781 xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
782 }
783#if CONFIG_VAR_TX
784 xd->above_txfm_context = cm->above_txfm_context + mi_col;
785 xd->left_txfm_context =
786 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
Yaowu Xu52a17632016-11-17 15:48:21 -0800787 set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bw, bh, skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700788#endif
789}
790
Yaowu Xuf883b422016-08-30 14:01:10 -0700791static void set_ref(AV1_COMMON *const cm, MACROBLOCKD *const xd, int idx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700792 int mi_row, int mi_col) {
793 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
794 RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
795 xd->block_refs[idx] = ref_buffer;
Yaowu Xuf883b422016-08-30 14:01:10 -0700796 if (!av1_is_valid_scale(&ref_buffer->sf))
797 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700798 "Invalid scale factors");
Yaowu Xuf883b422016-08-30 14:01:10 -0700799 av1_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
800 &ref_buffer->sf);
Angie Chiangd0916d92017-03-10 17:54:18 -0800801 aom_merge_corrupted_flag(&xd->corrupted, ref_buffer->buf->corrupted);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700802}
803
804static void dec_predict_b_extend(
Yaowu Xuf883b422016-08-30 14:01:10 -0700805 AV1Decoder *const pbi, MACROBLOCKD *const xd, const TileInfo *const tile,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700806 int block, int mi_row_ori, int mi_col_ori, int mi_row_pred, int mi_col_pred,
807 int mi_row_top, int mi_col_top, uint8_t *dst_buf[3], int dst_stride[3],
808 BLOCK_SIZE bsize_top, BLOCK_SIZE bsize_pred, int b_sub8x8, int bextend) {
809 // Used in supertx
810 // (mi_row_ori, mi_col_ori): location for mv
811 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
812 // (mi_row_top, mi_col_top, bsize_top): region of the top partition size
813 // block: sub location of sub8x8 blocks
814 // b_sub8x8: 1: ori is sub8x8; 0: ori is not sub8x8
815 // bextend: 1: region to predict is an extension of ori; 0: not
816 int r = (mi_row_pred - mi_row_top) * MI_SIZE;
817 int c = (mi_col_pred - mi_col_top) * MI_SIZE;
Jingning Han93531242016-12-20 11:54:36 -0800818 const int mi_width_top = mi_size_wide[bsize_top];
819 const int mi_height_top = mi_size_high[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700820 MB_MODE_INFO *mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700821 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700822
823 if (mi_row_pred < mi_row_top || mi_col_pred < mi_col_top ||
824 mi_row_pred >= mi_row_top + mi_height_top ||
825 mi_col_pred >= mi_col_top + mi_width_top || mi_row_pred >= cm->mi_rows ||
826 mi_col_pred >= cm->mi_cols)
827 return;
828
829 mbmi = set_offsets_extend(cm, xd, tile, bsize_pred, mi_row_pred, mi_col_pred,
830 mi_row_ori, mi_col_ori);
831 set_ref(cm, xd, 0, mi_row_pred, mi_col_pred);
832 if (has_second_ref(&xd->mi[0]->mbmi))
833 set_ref(cm, xd, 1, mi_row_pred, mi_col_pred);
834
Jingning Han2511c662016-12-22 11:57:34 -0800835 if (!bextend) mbmi->tx_size = max_txsize_lookup[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700836
837 xd->plane[0].dst.stride = dst_stride[0];
838 xd->plane[1].dst.stride = dst_stride[1];
839 xd->plane[2].dst.stride = dst_stride[2];
840 xd->plane[0].dst.buf = dst_buf[0] +
841 (r >> xd->plane[0].subsampling_y) * dst_stride[0] +
842 (c >> xd->plane[0].subsampling_x);
843 xd->plane[1].dst.buf = dst_buf[1] +
844 (r >> xd->plane[1].subsampling_y) * dst_stride[1] +
845 (c >> xd->plane[1].subsampling_x);
846 xd->plane[2].dst.buf = dst_buf[2] +
847 (r >> xd->plane[2].subsampling_y) * dst_stride[2] +
848 (c >> xd->plane[2].subsampling_x);
849
850 if (!b_sub8x8)
Yaowu Xuf883b422016-08-30 14:01:10 -0700851 av1_build_inter_predictors_sb_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700852#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700853 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700854#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700855 mi_row_pred, mi_col_pred, bsize_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700856 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700857 av1_build_inter_predictors_sb_sub8x8_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700858#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700859 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700860#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700861 mi_row_pred, mi_col_pred,
862 bsize_pred, block);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700863}
864
Yaowu Xuf883b422016-08-30 14:01:10 -0700865static void dec_extend_dir(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700866 const TileInfo *const tile, int block,
867 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
868 int mi_col, int mi_row_top, int mi_col_top,
869 uint8_t *dst_buf[3], int dst_stride[3], int dir) {
870 // dir: 0-lower, 1-upper, 2-left, 3-right
871 // 4-lowerleft, 5-upperleft, 6-lowerright, 7-upperright
Jingning Han93531242016-12-20 11:54:36 -0800872 const int mi_width = mi_size_wide[bsize];
873 const int mi_height = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700874 int xss = xd->plane[1].subsampling_x;
875 int yss = xd->plane[1].subsampling_y;
Jingning Hanfeb517c2016-12-21 16:02:07 -0800876#if CONFIG_CB4X4
877 const int unify_bsize = 1;
878#else
879 const int unify_bsize = 0;
880#endif
881 int b_sub8x8 = (bsize < BLOCK_8X8) && !unify_bsize ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700882 BLOCK_SIZE extend_bsize;
Jingning Han24f24a52016-12-27 10:13:28 -0800883 int mi_row_pred, mi_col_pred;
884
885 int wide_unit, high_unit;
886 int i, j;
887 int ext_offset = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700888
889 if (dir == 0 || dir == 1) {
Jingning Han93531242016-12-20 11:54:36 -0800890 extend_bsize =
891 (mi_width == mi_size_wide[BLOCK_8X8] || bsize < BLOCK_8X8 || xss < yss)
892 ? BLOCK_8X8
893 : BLOCK_16X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800894#if CONFIG_CB4X4
895 if (bsize < BLOCK_8X8) {
896 extend_bsize = BLOCK_4X4;
897 ext_offset = mi_size_wide[BLOCK_8X8];
898 }
899#endif
900
901 wide_unit = mi_size_wide[extend_bsize];
902 high_unit = mi_size_high[extend_bsize];
903
904 mi_row_pred = mi_row + ((dir == 0) ? mi_height : -(mi_height + ext_offset));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700905 mi_col_pred = mi_col;
906
Jingning Han24f24a52016-12-27 10:13:28 -0800907 for (j = 0; j < mi_height + ext_offset; j += high_unit)
908 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
909 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
910 mi_row_pred + j, mi_col_pred + i, mi_row_top,
911 mi_col_top, dst_buf, dst_stride, top_bsize,
912 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700913 } else if (dir == 2 || dir == 3) {
Jingning Han5b7706a2016-12-21 09:55:10 -0800914 extend_bsize =
915 (mi_height == mi_size_high[BLOCK_8X8] || bsize < BLOCK_8X8 || yss < xss)
916 ? BLOCK_8X8
917 : BLOCK_8X16;
Jingning Han24f24a52016-12-27 10:13:28 -0800918#if CONFIG_CB4X4
919 if (bsize < BLOCK_8X8) {
920 extend_bsize = BLOCK_4X4;
921 ext_offset = mi_size_wide[BLOCK_8X8];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700922 }
Jingning Han24f24a52016-12-27 10:13:28 -0800923#endif
924
925 wide_unit = mi_size_wide[extend_bsize];
926 high_unit = mi_size_high[extend_bsize];
927
928 mi_row_pred = mi_row;
929 mi_col_pred = mi_col + ((dir == 3) ? mi_width : -(mi_width + ext_offset));
930
931 for (j = 0; j < mi_height + ext_offset; j += high_unit)
932 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
933 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
934 mi_row_pred + j, mi_col_pred + i, mi_row_top,
935 mi_col_top, dst_buf, dst_stride, top_bsize,
936 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700937 } else {
938 extend_bsize = BLOCK_8X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800939#if CONFIG_CB4X4
940 if (bsize < BLOCK_8X8) {
941 extend_bsize = BLOCK_4X4;
942 ext_offset = mi_size_wide[BLOCK_8X8];
943 }
944#endif
945 wide_unit = mi_size_wide[extend_bsize];
946 high_unit = mi_size_high[extend_bsize];
947
Jingning Han5b7706a2016-12-21 09:55:10 -0800948 mi_row_pred = mi_row + ((dir == 4 || dir == 6) ? mi_height
Jingning Han24f24a52016-12-27 10:13:28 -0800949 : -(mi_height + ext_offset));
Jingning Han5b7706a2016-12-21 09:55:10 -0800950 mi_col_pred =
Jingning Han24f24a52016-12-27 10:13:28 -0800951 mi_col + ((dir == 6 || dir == 7) ? mi_width : -(mi_width + ext_offset));
952
953 for (j = 0; j < mi_height + ext_offset; j += high_unit)
954 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
955 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
956 mi_row_pred + j, mi_col_pred + i, mi_row_top,
957 mi_col_top, dst_buf, dst_stride, top_bsize,
958 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700959 }
960}
961
Yaowu Xuf883b422016-08-30 14:01:10 -0700962static void dec_extend_all(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700963 const TileInfo *const tile, int block,
964 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
965 int mi_col, int mi_row_top, int mi_col_top,
966 uint8_t *dst_buf[3], int dst_stride[3]) {
967 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
968 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
969 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
970 mi_row_top, mi_col_top, dst_buf, dst_stride, 1);
971 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
972 mi_row_top, mi_col_top, dst_buf, dst_stride, 2);
973 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
974 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
975 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
976 mi_row_top, mi_col_top, dst_buf, dst_stride, 4);
977 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
978 mi_row_top, mi_col_top, dst_buf, dst_stride, 5);
979 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
980 mi_row_top, mi_col_top, dst_buf, dst_stride, 6);
981 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
982 mi_row_top, mi_col_top, dst_buf, dst_stride, 7);
983}
984
Yaowu Xuf883b422016-08-30 14:01:10 -0700985static void dec_predict_sb_complex(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700986 const TileInfo *const tile, int mi_row,
987 int mi_col, int mi_row_top, int mi_col_top,
988 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize,
989 uint8_t *dst_buf[3], int dst_stride[3]) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700990 const AV1_COMMON *const cm = &pbi->common;
Jingning Han5b7706a2016-12-21 09:55:10 -0800991 const int hbs = mi_size_wide[bsize] / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700992 const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
993 const BLOCK_SIZE subsize = get_subsize(bsize, partition);
994#if CONFIG_EXT_PARTITION_TYPES
995 const BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
996#endif
997 int i;
998 const int mi_offset = mi_row * cm->mi_stride + mi_col;
999 uint8_t *dst_buf1[3], *dst_buf2[3], *dst_buf3[3];
Jingning Hanfeb517c2016-12-21 16:02:07 -08001000#if CONFIG_CB4X4
1001 const int unify_bsize = 1;
1002#else
1003 const int unify_bsize = 0;
1004#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001005
1006 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1007 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1008 DECLARE_ALIGNED(16, uint8_t, tmp_buf3[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1009 int dst_stride1[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1010 int dst_stride2[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1011 int dst_stride3[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1012
Yaowu Xuf883b422016-08-30 14:01:10 -07001013#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001014 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1015 int len = sizeof(uint16_t);
1016 dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
1017 dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_TX_SQUARE * len);
1018 dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_TX_SQUARE * len);
1019 dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
1020 dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_TX_SQUARE * len);
1021 dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_TX_SQUARE * len);
1022 dst_buf3[0] = CONVERT_TO_BYTEPTR(tmp_buf3);
1023 dst_buf3[1] = CONVERT_TO_BYTEPTR(tmp_buf3 + MAX_TX_SQUARE * len);
1024 dst_buf3[2] = CONVERT_TO_BYTEPTR(tmp_buf3 + 2 * MAX_TX_SQUARE * len);
1025 } else {
1026#endif
1027 dst_buf1[0] = tmp_buf1;
1028 dst_buf1[1] = tmp_buf1 + MAX_TX_SQUARE;
1029 dst_buf1[2] = tmp_buf1 + 2 * MAX_TX_SQUARE;
1030 dst_buf2[0] = tmp_buf2;
1031 dst_buf2[1] = tmp_buf2 + MAX_TX_SQUARE;
1032 dst_buf2[2] = tmp_buf2 + 2 * MAX_TX_SQUARE;
1033 dst_buf3[0] = tmp_buf3;
1034 dst_buf3[1] = tmp_buf3 + MAX_TX_SQUARE;
1035 dst_buf3[2] = tmp_buf3 + 2 * MAX_TX_SQUARE;
Yaowu Xuf883b422016-08-30 14:01:10 -07001036#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001037 }
1038#endif
1039
1040 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1041
1042 xd->mi = cm->mi_grid_visible + mi_offset;
1043 xd->mi[0] = cm->mi + mi_offset;
1044
1045 for (i = 0; i < MAX_MB_PLANE; i++) {
1046 xd->plane[i].dst.buf = dst_buf[i];
1047 xd->plane[i].dst.stride = dst_stride[i];
1048 }
1049
1050 switch (partition) {
1051 case PARTITION_NONE:
1052 assert(bsize < top_bsize);
1053 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1054 mi_row_top, mi_col_top, dst_buf, dst_stride,
1055 top_bsize, bsize, 0, 0);
1056 dec_extend_all(pbi, xd, tile, 0, bsize, top_bsize, mi_row, mi_col,
1057 mi_row_top, mi_col_top, dst_buf, dst_stride);
1058 break;
1059 case PARTITION_HORZ:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001060 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001061 // For sub8x8, predict in 8x8 unit
1062 // First half
1063 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1064 mi_row_top, mi_col_top, dst_buf, dst_stride,
1065 top_bsize, BLOCK_8X8, 1, 0);
1066 if (bsize < top_bsize)
1067 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1068 mi_row_top, mi_col_top, dst_buf, dst_stride);
1069
1070 // Second half
1071 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1072 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1073 top_bsize, BLOCK_8X8, 1, 1);
1074 if (bsize < top_bsize)
1075 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1076 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1077
1078 // weighted average to smooth the boundary
1079 xd->plane[0].dst.buf = dst_buf[0];
1080 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001081 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001082 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1083 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1084 0);
1085 } else {
1086 // First half
1087 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1088 mi_row_top, mi_col_top, dst_buf, dst_stride,
1089 top_bsize, subsize, 0, 0);
1090 if (bsize < top_bsize)
1091 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1092 mi_row_top, mi_col_top, dst_buf, dst_stride);
1093 else
1094 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1095 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1096
1097 if (mi_row + hbs < cm->mi_rows) {
1098 // Second half
1099 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col,
1100 mi_row + hbs, mi_col, mi_row_top, mi_col_top,
1101 dst_buf1, dst_stride1, top_bsize, subsize, 0, 0);
1102 if (bsize < top_bsize)
1103 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1104 mi_col, mi_row_top, mi_col_top, dst_buf1,
1105 dst_stride1);
1106 else
1107 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1108 mi_col, mi_row_top, mi_col_top, dst_buf1,
1109 dst_stride1, 1);
1110
1111 // weighted average to smooth the boundary
1112 for (i = 0; i < MAX_MB_PLANE; i++) {
1113 xd->plane[i].dst.buf = dst_buf[i];
1114 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001115 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001116 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1117 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1118 PARTITION_HORZ, i);
1119 }
1120 }
1121 }
1122 break;
1123 case PARTITION_VERT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001124 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001125 // First half
1126 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1127 mi_row_top, mi_col_top, dst_buf, dst_stride,
1128 top_bsize, BLOCK_8X8, 1, 0);
1129 if (bsize < top_bsize)
1130 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1131 mi_row_top, mi_col_top, dst_buf, dst_stride);
1132
1133 // Second half
1134 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1135 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1136 top_bsize, BLOCK_8X8, 1, 1);
1137 if (bsize < top_bsize)
1138 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1139 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1140
1141 // Smooth
1142 xd->plane[0].dst.buf = dst_buf[0];
1143 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001144 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001145 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1146 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1147 0);
1148 } else {
1149 // First half
1150 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1151 mi_row_top, mi_col_top, dst_buf, dst_stride,
1152 top_bsize, subsize, 0, 0);
1153 if (bsize < top_bsize)
1154 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1155 mi_row_top, mi_col_top, dst_buf, dst_stride);
1156 else
1157 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1158 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1159
1160 // Second half
1161 if (mi_col + hbs < cm->mi_cols) {
1162 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1163 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1164 dst_stride1, top_bsize, subsize, 0, 0);
1165 if (bsize < top_bsize)
1166 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1167 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1168 dst_stride1);
1169 else
1170 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1171 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1172 dst_stride1, 2);
1173
1174 // Smooth
1175 for (i = 0; i < MAX_MB_PLANE; i++) {
1176 xd->plane[i].dst.buf = dst_buf[i];
1177 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001178 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001179 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1180 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1181 PARTITION_VERT, i);
1182 }
1183 }
1184 }
1185 break;
1186 case PARTITION_SPLIT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001187 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001188 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1189 mi_row_top, mi_col_top, dst_buf, dst_stride,
1190 top_bsize, BLOCK_8X8, 1, 0);
1191 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1192 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1193 top_bsize, BLOCK_8X8, 1, 1);
1194 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1195 mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1196 top_bsize, BLOCK_8X8, 1, 1);
1197 dec_predict_b_extend(pbi, xd, tile, 3, mi_row, mi_col, mi_row, mi_col,
1198 mi_row_top, mi_col_top, dst_buf3, dst_stride3,
1199 top_bsize, BLOCK_8X8, 1, 1);
1200 if (bsize < top_bsize) {
1201 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1202 mi_row_top, mi_col_top, dst_buf, dst_stride);
1203 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1204 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1205 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1206 mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1207 dec_extend_all(pbi, xd, tile, 3, subsize, top_bsize, mi_row, mi_col,
1208 mi_row_top, mi_col_top, dst_buf3, dst_stride3);
1209 }
1210 } else {
1211 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row_top,
1212 mi_col_top, subsize, top_bsize, dst_buf,
1213 dst_stride);
1214 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1215 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col + hbs,
1216 mi_row_top, mi_col_top, subsize, top_bsize,
1217 dst_buf1, dst_stride1);
1218 if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols)
1219 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col,
1220 mi_row_top, mi_col_top, subsize, top_bsize,
1221 dst_buf2, dst_stride2);
1222 if (mi_row + hbs < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1223 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col + hbs,
1224 mi_row_top, mi_col_top, subsize, top_bsize,
1225 dst_buf3, dst_stride3);
1226 }
1227 for (i = 0; i < MAX_MB_PLANE; i++) {
Jingning Han9e0976a2016-12-27 17:52:42 -08001228#if !CONFIG_CB4X4
Jingning Han24f24a52016-12-27 10:13:28 -08001229 if (bsize == BLOCK_8X8 && i != 0)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001230 continue; // Skip <4x4 chroma smoothing
Jingning Han9e0976a2016-12-27 17:52:42 -08001231#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001232 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001233 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001234 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1235 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1236 PARTITION_VERT, i);
1237 if (mi_row + hbs < cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001238 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001239 xd, dst_buf2[i], dst_stride2[i], dst_buf3[i], dst_stride3[i],
1240 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1241 PARTITION_VERT, i);
Yaowu Xuf883b422016-08-30 14:01:10 -07001242 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001243 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1244 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1245 PARTITION_HORZ, i);
1246 }
1247 } else if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001248 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001249 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1250 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1251 PARTITION_HORZ, i);
1252 }
1253 }
1254 break;
1255#if CONFIG_EXT_PARTITION_TYPES
1256 case PARTITION_HORZ_A:
1257 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1258 mi_row_top, mi_col_top, dst_buf, dst_stride,
1259 top_bsize, bsize2, 0, 0);
1260 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1261 mi_row_top, mi_col_top, dst_buf, dst_stride);
1262
1263 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1264 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1265 dst_stride1, top_bsize, bsize2, 0, 0);
1266 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1267 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1268
1269 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1270 mi_col, mi_row_top, mi_col_top, dst_buf2,
1271 dst_stride2, top_bsize, subsize, 0, 0);
1272 if (bsize < top_bsize)
1273 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1274 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1275 else
1276 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1277 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1278 1);
1279
1280 for (i = 0; i < MAX_MB_PLANE; i++) {
1281 xd->plane[i].dst.buf = dst_buf[i];
1282 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001283 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001284 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1285 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1286 i);
1287 }
1288 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001289 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001290 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1291 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1292 i);
1293 }
1294 break;
1295 case PARTITION_VERT_A:
1296
1297 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1298 mi_row_top, mi_col_top, dst_buf, dst_stride,
1299 top_bsize, bsize2, 0, 0);
1300 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1301 mi_row_top, mi_col_top, dst_buf, dst_stride);
1302
1303 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1304 mi_col, mi_row_top, mi_col_top, dst_buf1,
1305 dst_stride1, top_bsize, bsize2, 0, 0);
1306 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1307 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1308
1309 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1310 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1311 dst_stride2, top_bsize, subsize, 0, 0);
1312 if (bsize < top_bsize)
1313 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1314 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1315 dst_stride2);
1316 else
1317 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1318 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1319 dst_stride2, 2);
1320
1321 for (i = 0; i < MAX_MB_PLANE; i++) {
1322 xd->plane[i].dst.buf = dst_buf[i];
1323 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001324 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001325 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1326 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1327 i);
1328 }
1329 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001330 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001331 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1332 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1333 i);
1334 }
1335 break;
1336 case PARTITION_HORZ_B:
1337 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1338 mi_row_top, mi_col_top, dst_buf, dst_stride,
1339 top_bsize, subsize, 0, 0);
1340 if (bsize < top_bsize)
1341 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1342 mi_row_top, mi_col_top, dst_buf, dst_stride);
1343 else
1344 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1345 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1346
1347 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1348 mi_col, mi_row_top, mi_col_top, dst_buf1,
1349 dst_stride1, top_bsize, bsize2, 0, 0);
1350 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1351 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1352
1353 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1354 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1355 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1356 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1357 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1358 dst_stride2);
1359
1360 for (i = 0; i < MAX_MB_PLANE; i++) {
1361 xd->plane[i].dst.buf = dst_buf1[i];
1362 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001363 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001364 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1365 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1366 PARTITION_VERT, i);
1367 }
1368 for (i = 0; i < MAX_MB_PLANE; i++) {
1369 xd->plane[i].dst.buf = dst_buf[i];
1370 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001371 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001372 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1373 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1374 i);
1375 }
1376 break;
1377 case PARTITION_VERT_B:
1378 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1379 mi_row_top, mi_col_top, dst_buf, dst_stride,
1380 top_bsize, subsize, 0, 0);
1381 if (bsize < top_bsize)
1382 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1383 mi_row_top, mi_col_top, dst_buf, dst_stride);
1384 else
1385 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1386 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1387
1388 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1389 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1390 dst_stride1, top_bsize, bsize2, 0, 0);
1391 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1392 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1393
1394 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1395 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1396 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1397 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1398 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1399 dst_stride2);
1400
1401 for (i = 0; i < MAX_MB_PLANE; i++) {
1402 xd->plane[i].dst.buf = dst_buf1[i];
1403 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001404 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001405 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1406 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1407 PARTITION_HORZ, i);
1408 }
1409 for (i = 0; i < MAX_MB_PLANE; i++) {
1410 xd->plane[i].dst.buf = dst_buf[i];
1411 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001412 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001413 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1414 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1415 i);
1416 }
1417 break;
1418#endif // CONFIG_EXT_PARTITION_TYPES
1419 default: assert(0);
1420 }
1421}
1422
Yaowu Xuf883b422016-08-30 14:01:10 -07001423static void set_segment_id_supertx(const AV1_COMMON *const cm, const int mi_row,
1424 const int mi_col, const BLOCK_SIZE bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001425 const struct segmentation *seg = &cm->seg;
Jingning Han5b7706a2016-12-21 09:55:10 -08001426 const int miw = AOMMIN(mi_size_wide[bsize], cm->mi_cols - mi_col);
1427 const int mih = AOMMIN(mi_size_high[bsize], cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001428 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1429 MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
1430 int r, c;
1431 int seg_id_supertx = MAX_SEGMENTS;
1432
1433 if (!seg->enabled) {
1434 seg_id_supertx = 0;
1435 } else {
1436 // Find the minimum segment_id
1437 for (r = 0; r < mih; r++)
1438 for (c = 0; c < miw; c++)
1439 seg_id_supertx =
Yaowu Xuf883b422016-08-30 14:01:10 -07001440 AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001441 assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);
1442 }
1443
1444 // Assign the the segment_id back to segment_id_supertx
1445 for (r = 0; r < mih; r++)
1446 for (c = 0; c < miw; c++)
1447 mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
1448}
1449#endif // CONFIG_SUPERTX
1450
Yue Chen64550b62017-01-12 12:18:22 -08001451static void decode_mbmi_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001452#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001453 int supertx_enabled,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001454#endif // CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001455 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001456#if CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001457 PARTITION_TYPE partition,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001458#endif // CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001459 BLOCK_SIZE bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001460 AV1_COMMON *const cm = &pbi->common;
Jingning Han85dc03f2016-12-06 16:03:10 -08001461 const int bw = mi_size_wide[bsize];
1462 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -07001463 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1464 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Nathan E. Eggeebbd4792016-10-05 19:30:15 -04001465
Michael Bebenita6048d052016-08-25 14:40:54 -07001466#if CONFIG_ACCOUNTING
1467 aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
1468#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001469#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001470 if (supertx_enabled) {
Yue Chen64550b62017-01-12 12:18:22 -08001471 set_mb_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001472 } else {
Yue Chen64550b62017-01-12 12:18:22 -08001473 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001474 }
1475#if CONFIG_EXT_PARTITION_TYPES
1476 xd->mi[0]->mbmi.partition = partition;
1477#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001478 av1_read_mode_info(pbi, xd, supertx_enabled, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001479#else
Yue Chen64550b62017-01-12 12:18:22 -08001480 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001481#if CONFIG_EXT_PARTITION_TYPES
1482 xd->mi[0]->mbmi.partition = partition;
1483#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001484 av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001485#endif // CONFIG_SUPERTX
1486
1487 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1488 const BLOCK_SIZE uv_subsize =
1489 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1490 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -07001491 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001492 "Invalid block size.");
1493 }
1494
1495#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001496 xd->mi[0]->mbmi.segment_id_supertx = MAX_SEGMENTS;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001497#endif // CONFIG_SUPERTX
1498
Angie Chiangd0916d92017-03-10 17:54:18 -08001499 int reader_corrupted_flag = aom_reader_has_error(r);
1500 aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
Yue Chen64550b62017-01-12 12:18:22 -08001501}
1502
1503static void decode_token_and_recon_block(AV1Decoder *const pbi,
1504 MACROBLOCKD *const xd, int mi_row,
1505 int mi_col, aom_reader *r,
1506 BLOCK_SIZE bsize) {
1507 AV1_COMMON *const cm = &pbi->common;
1508 const int bw = mi_size_wide[bsize];
1509 const int bh = mi_size_high[bsize];
1510 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1511 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yue Chen64550b62017-01-12 12:18:22 -08001512
Angie Chiang44701f22017-02-27 10:36:44 -08001513 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
1514 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Yue Chen19e7aa82016-11-30 14:05:39 -08001515
Arild Fuldseth07441162016-08-15 15:07:52 +02001516#if CONFIG_DELTA_Q
1517 if (cm->delta_q_present_flag) {
1518 int i;
1519 for (i = 0; i < MAX_SEGMENTS; i++) {
1520 xd->plane[0].seg_dequant[i][0] =
1521 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
1522 xd->plane[0].seg_dequant[i][1] =
1523 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
1524 xd->plane[1].seg_dequant[i][0] =
1525 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1526 xd->plane[1].seg_dequant[i][1] =
1527 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1528 xd->plane[2].seg_dequant[i][0] =
1529 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1530 xd->plane[2].seg_dequant[i][1] =
1531 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1532 }
1533 }
1534#endif
1535
Jingning Han41bb3392016-12-14 10:46:48 -08001536#if CONFIG_CB4X4
1537 if (mbmi->skip) reset_skip_context(xd, bsize);
1538#else
Jingning Hand39cc722016-12-02 14:03:26 -08001539 if (mbmi->skip) reset_skip_context(xd, AOMMAX(BLOCK_8X8, bsize));
Jingning Han41bb3392016-12-14 10:46:48 -08001540#endif
Jingning Hand39cc722016-12-02 14:03:26 -08001541
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001542#if CONFIG_COEF_INTERLEAVE
1543 {
1544 const struct macroblockd_plane *const pd_y = &xd->plane[0];
1545 const struct macroblockd_plane *const pd_c = &xd->plane[1];
1546 const TX_SIZE tx_log2_y = mbmi->tx_size;
1547 const TX_SIZE tx_log2_c = get_uv_tx_size(mbmi, pd_c);
1548 const int tx_sz_y = (1 << tx_log2_y);
1549 const int tx_sz_c = (1 << tx_log2_c);
1550 const int num_4x4_w_y = pd_y->n4_w;
1551 const int num_4x4_h_y = pd_y->n4_h;
1552 const int num_4x4_w_c = pd_c->n4_w;
1553 const int num_4x4_h_c = pd_c->n4_h;
1554 const int max_4x4_w_y = get_max_4x4_size(num_4x4_w_y, xd->mb_to_right_edge,
1555 pd_y->subsampling_x);
1556 const int max_4x4_h_y = get_max_4x4_size(num_4x4_h_y, xd->mb_to_bottom_edge,
1557 pd_y->subsampling_y);
1558 const int max_4x4_w_c = get_max_4x4_size(num_4x4_w_c, xd->mb_to_right_edge,
1559 pd_c->subsampling_x);
1560 const int max_4x4_h_c = get_max_4x4_size(num_4x4_h_c, xd->mb_to_bottom_edge,
1561 pd_c->subsampling_y);
1562
1563 // The max_4x4_w/h may be smaller than tx_sz under some corner cases,
1564 // i.e. when the SB is splitted by tile boundaries.
1565 const int tu_num_w_y = (max_4x4_w_y + tx_sz_y - 1) / tx_sz_y;
1566 const int tu_num_h_y = (max_4x4_h_y + tx_sz_y - 1) / tx_sz_y;
1567 const int tu_num_w_c = (max_4x4_w_c + tx_sz_c - 1) / tx_sz_c;
1568 const int tu_num_h_c = (max_4x4_h_c + tx_sz_c - 1) / tx_sz_c;
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001569 const int tu_num_c = tu_num_w_c * tu_num_h_c;
1570
1571 if (!is_inter_block(mbmi)) {
1572 int tu_idx_c = 0;
1573 int row_y, col_y, row_c, col_c;
1574 int plane;
1575
1576#if CONFIG_PALETTE
1577 for (plane = 0; plane <= 1; ++plane) {
1578 if (mbmi->palette_mode_info.palette_size[plane])
1579 av1_decode_palette_tokens(xd, plane, r);
1580 }
1581#endif
1582
1583 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1584 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1585 // luma
1586 predict_and_reconstruct_intra_block(
1587 cm, xd, r, mbmi, 0, row_y * tx_sz_y, col_y * tx_sz_y, tx_log2_y);
1588 // chroma
1589 if (tu_idx_c < tu_num_c) {
1590 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1591 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1592 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c,
1593 col_c, tx_log2_c);
1594 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c,
1595 col_c, tx_log2_c);
1596 tu_idx_c++;
1597 }
1598 }
1599 }
1600
1601 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1602 while (tu_idx_c < tu_num_c) {
1603 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1604 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1605 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c, col_c,
1606 tx_log2_c);
1607 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c, col_c,
1608 tx_log2_c);
1609 tu_idx_c++;
1610 }
1611 } else {
1612 // Prediction
David Barkerac37fa32016-12-02 12:30:21 +00001613 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001614 AOMMAX(bsize, BLOCK_8X8));
1615
1616 // Reconstruction
1617 if (!mbmi->skip) {
1618 int eobtotal = 0;
1619 int tu_idx_c = 0;
1620 int row_y, col_y, row_c, col_c;
1621
1622 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1623 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1624 // luma
1625 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 0,
1626 row_y * tx_sz_y,
1627 col_y * tx_sz_y, tx_log2_y);
1628 // chroma
1629 if (tu_idx_c < tu_num_c) {
1630 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1631 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1632 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1633 1, row_c, col_c, tx_log2_c);
1634 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1635 2, row_c, col_c, tx_log2_c);
1636 tu_idx_c++;
1637 }
1638 }
1639 }
1640
1641 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1642 while (tu_idx_c < tu_num_c) {
1643 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1644 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1645 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 1,
1646 row_c, col_c, tx_log2_c);
1647 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 2,
1648 row_c, col_c, tx_log2_c);
1649 tu_idx_c++;
1650 }
1651
Alex Converse64d7ef62017-03-22 18:09:16 -07001652 // TODO(CONFIG_COEF_INTERLEAVE owners): bring eob == 0 corner case
1653 // into line with the defaut configuration
1654 if (bsize >= BLOCK_8X8 && eobtotal == 0) mbmi->skip = 1;
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001655 }
1656 }
1657 }
Angie Chiang133733c2017-03-17 12:50:20 -07001658#else // CONFIG_COEF_INTERLEAVE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001659 if (!is_inter_block(mbmi)) {
1660 int plane;
Fangwen Fub3be9262017-03-06 15:34:28 -08001661#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001662 for (plane = 0; plane <= 1; ++plane) {
1663 if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xuf883b422016-08-30 14:01:10 -07001664 av1_decode_palette_tokens(xd, plane, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001665 }
Fangwen Fub3be9262017-03-06 15:34:28 -08001666#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001667 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1668 const struct macroblockd_plane *const pd = &xd->plane[plane];
Angie Chiang7fcfee42017-02-24 15:51:03 -08001669 const TX_SIZE tx_size = get_tx_size(plane, xd);
Jingning Han2d64f122016-10-21 12:44:29 -07001670 const int stepr = tx_size_high_unit[tx_size];
1671 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han41bb3392016-12-14 10:46:48 -08001672#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001673#if CONFIG_CHROMA_2X2
1674 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1675#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001676 const BLOCK_SIZE plane_bsize =
1677 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001678#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001679#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001680 const BLOCK_SIZE plane_bsize =
1681 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001682#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001683 int row, col;
Jingning Hanbafee8d2016-12-02 10:25:03 -08001684 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1685 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001686#if CONFIG_CB4X4
1687 if (bsize < BLOCK_8X8 && plane && !is_chroma_reference(mi_row, mi_col))
1688 continue;
1689#endif
1690
Yaowu Xuc27fc142016-08-22 16:08:15 -07001691 for (row = 0; row < max_blocks_high; row += stepr)
1692 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001693 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane, row, col,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001694 tx_size);
1695 }
1696 } else {
Yue Chen9ab6d712017-01-12 15:50:46 -08001697 int ref;
1698
1699 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
1700 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
1701 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
1702
1703 xd->block_refs[ref] = ref_buf;
1704 if ((!av1_is_valid_scale(&ref_buf->sf)))
1705 aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
1706 "Reference frame has invalid dimensions");
1707 av1_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col, &ref_buf->sf);
1708 }
Yue Chen69f18e12016-09-08 14:48:15 -07001709#if CONFIG_WARPED_MOTION
1710 if (mbmi->motion_mode == WARPED_CAUSAL) {
1711 int i;
Sarah Parker19234cc2017-03-10 16:43:25 -08001712 assert_motion_mode_valid(WARPED_CAUSAL,
1713#if CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1714 0, cm->global_motion,
1715#endif // CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1716 xd->mi[0]);
Yue Chen69f18e12016-09-08 14:48:15 -07001717 for (i = 0; i < 3; ++i) {
1718 const struct macroblockd_plane *pd = &xd->plane[i];
1719
1720 av1_warp_plane(&mbmi->wm_params[0],
1721#if CONFIG_AOM_HIGHBITDEPTH
1722 xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH, xd->bd,
1723#endif // CONFIG_AOM_HIGHBITDEPTH
1724 pd->pre[0].buf0, pd->pre[0].width, pd->pre[0].height,
1725 pd->pre[0].stride, pd->dst.buf,
1726 ((mi_col * MI_SIZE) >> pd->subsampling_x),
1727 ((mi_row * MI_SIZE) >> pd->subsampling_y),
Jingning Hanff6ee6a2016-12-07 09:55:21 -08001728 xd->n8_w * (MI_SIZE >> pd->subsampling_x),
1729 xd->n8_h * (MI_SIZE >> pd->subsampling_y),
1730 pd->dst.stride, pd->subsampling_x, pd->subsampling_y, 16,
1731 16, 0);
Yue Chen69f18e12016-09-08 14:48:15 -07001732 }
1733 } else {
1734#endif // CONFIG_WARPED_MOTION
Jingning Han41bb3392016-12-14 10:46:48 -08001735#if CONFIG_CB4X4
1736 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL, bsize);
1737#else
1738 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
1739 AOMMAX(bsize, BLOCK_8X8));
1740#endif
Yue Chen69f18e12016-09-08 14:48:15 -07001741#if CONFIG_WARPED_MOTION
1742 }
1743#endif // CONFIG_WARPED_MOTION
Yue Chencb60b182016-10-13 15:18:22 -07001744#if CONFIG_MOTION_VAR
1745 if (mbmi->motion_mode == OBMC_CAUSAL) {
Sarah Parker19234cc2017-03-10 16:43:25 -08001746 assert_motion_mode_valid(OBMC_CAUSAL,
1747#if CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1748 0, cm->global_motion,
1749#endif // CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1750 xd->mi[0]);
Yue Chenf27b1602017-01-13 11:11:43 -08001751#if CONFIG_NCOBMC
1752 av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
1753#else
Yue Chen894fcce2016-10-21 16:50:52 -07001754 av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yue Chenf27b1602017-01-13 11:11:43 -08001755#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001756 }
Yue Chencb60b182016-10-13 15:18:22 -07001757#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001758
1759 // Reconstruction
1760 if (!mbmi->skip) {
1761 int eobtotal = 0;
1762 int plane;
1763
1764 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1765 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han41bb3392016-12-14 10:46:48 -08001766#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001767#if CONFIG_CHROMA_2X2
1768 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1769#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001770 const BLOCK_SIZE plane_bsize =
1771 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001772#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001773#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001774 const BLOCK_SIZE plane_bsize =
1775 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001776#endif
Jingning Hanbafee8d2016-12-02 10:25:03 -08001777 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1778 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001779 int row, col;
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001780
1781#if CONFIG_CB4X4
1782 if (bsize < BLOCK_8X8 && plane && !is_chroma_reference(mi_row, mi_col))
1783 continue;
1784#endif
1785
Yaowu Xuc27fc142016-08-22 16:08:15 -07001786#if CONFIG_VAR_TX
Jingning Han70e5f3f2016-11-09 17:03:07 -08001787 const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07001788 const int bh_var_tx = tx_size_high_unit[max_tx_size];
1789 const int bw_var_tx = tx_size_wide_unit[max_tx_size];
Jingning Hanbafee8d2016-12-02 10:25:03 -08001790 for (row = 0; row < max_blocks_high; row += bh_var_tx)
1791 for (col = 0; col < max_blocks_wide; col += bw_var_tx)
Jingning Hanfe45b212016-11-22 10:30:23 -08001792 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, row, col,
1793 max_tx_size, &eobtotal);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001794#else
Angie Chiang7fcfee42017-02-24 15:51:03 -08001795 const TX_SIZE tx_size = get_tx_size(plane, xd);
Jingning Han2d64f122016-10-21 12:44:29 -07001796 const int stepr = tx_size_high_unit[tx_size];
1797 const int stepc = tx_size_wide_unit[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001798 for (row = 0; row < max_blocks_high; row += stepr)
1799 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001800 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1801 plane, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001802#endif
1803 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001804 }
1805 }
Angie Chiang133733c2017-03-17 12:50:20 -07001806#endif // CONFIG_COEF_INTERLEAVE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001807
Angie Chiangd0916d92017-03-10 17:54:18 -08001808 int reader_corrupted_flag = aom_reader_has_error(r);
1809 aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001810}
1811
Yue Chen9ab6d712017-01-12 15:50:46 -08001812#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
1813static void detoken_and_recon_sb(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1814 int mi_row, int mi_col, aom_reader *r,
1815 BLOCK_SIZE bsize) {
1816 AV1_COMMON *const cm = &pbi->common;
1817 const int hbs = mi_size_wide[bsize] >> 1;
1818#if CONFIG_CB4X4
1819 const int unify_bsize = 1;
1820#else
1821 const int unify_bsize = 0;
1822#endif
1823#if CONFIG_EXT_PARTITION_TYPES
1824 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1825#endif
1826 PARTITION_TYPE partition;
1827 BLOCK_SIZE subsize;
1828 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1829 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1830
1831 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1832
1833 partition = get_partition(cm, mi_row, mi_col, bsize);
1834 subsize = subsize_lookup[partition][bsize];
1835
1836 if (!hbs && !unify_bsize) {
1837 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1838 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1839 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1840 } else {
1841 switch (partition) {
1842 case PARTITION_NONE:
1843 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
1844 break;
1845 case PARTITION_HORZ:
1846 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1847 if (has_rows)
1848 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r,
1849 subsize);
1850 break;
1851 case PARTITION_VERT:
1852 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1853 if (has_cols)
1854 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r,
1855 subsize);
1856 break;
1857 case PARTITION_SPLIT:
1858 detoken_and_recon_sb(pbi, xd, mi_row, mi_col, r, subsize);
1859 detoken_and_recon_sb(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1860 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1861 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col + hbs, r, subsize);
1862 break;
1863#if CONFIG_EXT_PARTITION_TYPES
1864 case PARTITION_HORZ_A:
1865 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1866 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1867 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1868 break;
1869 case PARTITION_HORZ_B:
1870 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1871 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1872 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1873 bsize2);
1874 break;
1875 case PARTITION_VERT_A:
1876 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1877 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1878 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1879 break;
1880 case PARTITION_VERT_B:
1881 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1882 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1883 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1884 bsize2);
1885 break;
1886#endif
1887 default: assert(0 && "Invalid partition type");
1888 }
1889 }
1890}
1891#endif
1892
Yue Chen64550b62017-01-12 12:18:22 -08001893static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1894#if CONFIG_SUPERTX
1895 int supertx_enabled,
1896#endif // CONFIG_SUPERTX
1897 int mi_row, int mi_col, aom_reader *r,
1898#if CONFIG_EXT_PARTITION_TYPES
1899 PARTITION_TYPE partition,
1900#endif // CONFIG_EXT_PARTITION_TYPES
1901 BLOCK_SIZE bsize) {
1902 decode_mbmi_block(pbi, xd,
1903#if CONFIG_SUPERTX
1904 supertx_enabled,
1905#endif
1906 mi_row, mi_col, r,
1907#if CONFIG_EXT_PARTITION_TYPES
1908 partition,
1909#endif
1910 bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001911#if !(CONFIG_MOTION_VAR && CONFIG_NCOBMC)
Yue Chen64550b62017-01-12 12:18:22 -08001912#if CONFIG_SUPERTX
1913 if (!supertx_enabled)
1914#endif // CONFIG_SUPERTX
1915 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001916#endif
Yue Chen64550b62017-01-12 12:18:22 -08001917}
1918
Yaowu Xuf883b422016-08-30 14:01:10 -07001919static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
1920 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001921 int has_rows, int has_cols,
Jingning Han1beb0102016-12-07 11:08:30 -08001922 BLOCK_SIZE bsize) {
Alex Converse55c6bde2017-01-12 15:55:31 -08001923#if CONFIG_UNPOISON_PARTITION_CTX
1924 const int ctx =
1925 partition_plane_context(xd, mi_row, mi_col, has_rows, has_cols, bsize);
Alex Converse2b9d19d2017-04-03 11:11:17 -07001926 const aom_prob *const probs =
1927 ctx < PARTITION_CONTEXTS ? cm->fc->partition_prob[ctx] : NULL;
1928 FRAME_COUNTS *const counts = ctx < PARTITION_CONTEXTS ? xd->counts : NULL;
Alex Converse55c6bde2017-01-12 15:55:31 -08001929#else
Jingning Han1beb0102016-12-07 11:08:30 -08001930 const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
Yaowu Xuf883b422016-08-30 14:01:10 -07001931 const aom_prob *const probs = cm->fc->partition_prob[ctx];
Alex Converse55c6bde2017-01-12 15:55:31 -08001932 FRAME_COUNTS *const counts = xd->counts;
1933#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001934 PARTITION_TYPE p;
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001935#if CONFIG_EC_ADAPT
1936 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1937 (void)cm;
1938#elif CONFIG_EC_MULTISYMBOL
1939 FRAME_CONTEXT *ec_ctx = cm->fc;
1940#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001941
Jingning Han5fe79db2017-03-27 15:10:30 -07001942#if CONFIG_EC_MULTISYMBOL
1943 aom_cdf_prob *partition_cdf = (ctx >= 0) ? ec_ctx->partition_cdf[ctx] : NULL;
1944#endif
1945
Yaowu Xuc27fc142016-08-22 16:08:15 -07001946 if (has_rows && has_cols)
1947#if CONFIG_EXT_PARTITION_TYPES
1948 if (bsize <= BLOCK_8X8)
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001949#if CONFIG_EC_MULTISYMBOL
Jingning Han5fe79db2017-03-27 15:10:30 -07001950 p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, PARTITION_TYPES,
1951 ACCT_STR);
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001952#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001953 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001954#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001955 else
Alex Converse57795a42017-03-14 12:18:25 -07001956#if CONFIG_EC_MULTISYMBOL
Jingning Han5fe79db2017-03-27 15:10:30 -07001957 p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, EXT_PARTITION_TYPES,
1958 ACCT_STR);
Alex Converse57795a42017-03-14 12:18:25 -07001959#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001960 p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs,
1961 ACCT_STR);
Alex Converse57795a42017-03-14 12:18:25 -07001962#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001963#else
Nathan E. Egge9d9eb6c2016-11-16 09:44:26 -05001964#if CONFIG_EC_MULTISYMBOL
Jingning Han5fe79db2017-03-27 15:10:30 -07001965 p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, PARTITION_TYPES,
1966 ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001967#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001968 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001969#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001970#endif // CONFIG_EXT_PARTITION_TYPES
1971 else if (!has_rows && has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001972 p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001973 else if (has_rows && !has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001974 p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001975 else
1976 p = PARTITION_SPLIT;
1977
1978 if (counts) ++counts->partition[ctx][p];
1979
1980 return p;
1981}
1982
1983#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001984static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
1985 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001986 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
1987 return 1;
1988 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001989 const int ctx = av1_get_skip_context(xd);
Michael Bebenita6048d052016-08-25 14:40:54 -07001990 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001991 FRAME_COUNTS *counts = xd->counts;
1992 if (counts) ++counts->skip[ctx][skip];
1993 return skip;
1994 }
1995}
1996#endif // CONFIG_SUPERTX
1997
1998// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -07001999static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002000#if CONFIG_SUPERTX
2001 int supertx_enabled,
2002#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002003 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002004 BLOCK_SIZE bsize, int n4x4_l2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002005 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002006 const int n8x8_l2 = n4x4_l2 - 1;
Jingning Hanff17e162016-12-07 17:58:18 -08002007 const int num_8x8_wh = mi_size_wide[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002008 const int hbs = num_8x8_wh >> 1;
Jingning Han41bb3392016-12-14 10:46:48 -08002009#if CONFIG_CB4X4
2010 const int unify_bsize = 1;
2011#else
2012 const int unify_bsize = 0;
2013#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002014 PARTITION_TYPE partition;
2015 BLOCK_SIZE subsize;
2016#if CONFIG_EXT_PARTITION_TYPES
2017 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
2018#endif
2019 const int has_rows = (mi_row + hbs) < cm->mi_rows;
2020 const int has_cols = (mi_col + hbs) < cm->mi_cols;
2021#if CONFIG_SUPERTX
2022 const int read_token = !supertx_enabled;
2023 int skip = 0;
Jingning Han2511c662016-12-22 11:57:34 -08002024 TX_SIZE supertx_size = max_txsize_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002025 const TileInfo *const tile = &xd->tile;
2026 int txfm = DCT_DCT;
2027#endif // CONFIG_SUPERTX
2028
2029 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
2030
Jingning Hancd959762017-03-27 14:49:59 -07002031 partition = (bsize < BLOCK_8X8) ? PARTITION_NONE
2032 : read_partition(cm, xd, mi_row, mi_col, r,
2033 has_rows, has_cols, bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002034 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
Yushin Cho77bba8d2016-11-04 16:36:56 -07002035
2036#if CONFIG_PVQ
2037 assert(partition < PARTITION_TYPES);
2038 assert(subsize < BLOCK_SIZES);
2039#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002040#if CONFIG_SUPERTX
2041 if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
2042 bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
2043 const int supertx_context = partition_supertx_context_lookup[partition];
Michael Bebenita6048d052016-08-25 14:40:54 -07002044 supertx_enabled = aom_read(
2045 r, cm->fc->supertx_prob[supertx_context][supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002046 if (xd->counts)
2047 xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
2048#if CONFIG_VAR_TX
2049 if (supertx_enabled) xd->supertx_size = supertx_size;
2050#endif
2051 }
2052#endif // CONFIG_SUPERTX
Jingning Han41bb3392016-12-14 10:46:48 -08002053 if (!hbs && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002054 // calculate bmode block dimensions (log 2)
2055 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
2056 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
2057 decode_block(pbi, xd,
2058#if CONFIG_SUPERTX
2059 supertx_enabled,
2060#endif // CONFIG_SUPERTX
2061 mi_row, mi_col, r,
2062#if CONFIG_EXT_PARTITION_TYPES
2063 partition,
2064#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002065 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002066 } else {
2067 switch (partition) {
2068 case PARTITION_NONE:
2069 decode_block(pbi, xd,
2070#if CONFIG_SUPERTX
2071 supertx_enabled,
2072#endif // CONFIG_SUPERTX
2073 mi_row, mi_col, r,
2074#if CONFIG_EXT_PARTITION_TYPES
2075 partition,
2076#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002077 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002078 break;
2079 case PARTITION_HORZ:
2080 decode_block(pbi, xd,
2081#if CONFIG_SUPERTX
2082 supertx_enabled,
2083#endif // CONFIG_SUPERTX
2084 mi_row, mi_col, r,
2085#if CONFIG_EXT_PARTITION_TYPES
2086 partition,
2087#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002088 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002089 if (has_rows)
2090 decode_block(pbi, xd,
2091#if CONFIG_SUPERTX
2092 supertx_enabled,
2093#endif // CONFIG_SUPERTX
2094 mi_row + hbs, mi_col, r,
2095#if CONFIG_EXT_PARTITION_TYPES
2096 partition,
2097#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002098 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002099 break;
2100 case PARTITION_VERT:
2101 decode_block(pbi, xd,
2102#if CONFIG_SUPERTX
2103 supertx_enabled,
2104#endif // CONFIG_SUPERTX
2105 mi_row, mi_col, r,
2106#if CONFIG_EXT_PARTITION_TYPES
2107 partition,
2108#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002109 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002110 if (has_cols)
2111 decode_block(pbi, xd,
2112#if CONFIG_SUPERTX
2113 supertx_enabled,
2114#endif // CONFIG_SUPERTX
2115 mi_row, mi_col + hbs, r,
2116#if CONFIG_EXT_PARTITION_TYPES
2117 partition,
2118#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002119 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002120 break;
2121 case PARTITION_SPLIT:
2122 decode_partition(pbi, xd,
2123#if CONFIG_SUPERTX
2124 supertx_enabled,
2125#endif // CONFIG_SUPERTX
2126 mi_row, mi_col, r, subsize, n8x8_l2);
2127 decode_partition(pbi, xd,
2128#if CONFIG_SUPERTX
2129 supertx_enabled,
2130#endif // CONFIG_SUPERTX
2131 mi_row, mi_col + hbs, r, subsize, n8x8_l2);
2132 decode_partition(pbi, xd,
2133#if CONFIG_SUPERTX
2134 supertx_enabled,
2135#endif // CONFIG_SUPERTX
2136 mi_row + hbs, mi_col, r, subsize, n8x8_l2);
2137 decode_partition(pbi, xd,
2138#if CONFIG_SUPERTX
2139 supertx_enabled,
2140#endif // CONFIG_SUPERTX
2141 mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
2142 break;
2143#if CONFIG_EXT_PARTITION_TYPES
2144 case PARTITION_HORZ_A:
2145 decode_block(pbi, xd,
2146#if CONFIG_SUPERTX
2147 supertx_enabled,
2148#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002149 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002150 decode_block(pbi, xd,
2151#if CONFIG_SUPERTX
2152 supertx_enabled,
2153#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002154 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002155 decode_block(pbi, xd,
2156#if CONFIG_SUPERTX
2157 supertx_enabled,
2158#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002159 mi_row + hbs, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002160 break;
2161 case PARTITION_HORZ_B:
2162 decode_block(pbi, xd,
2163#if CONFIG_SUPERTX
2164 supertx_enabled,
2165#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002166 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002167 decode_block(pbi, xd,
2168#if CONFIG_SUPERTX
2169 supertx_enabled,
2170#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002171 mi_row + hbs, 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 + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002177 break;
2178 case PARTITION_VERT_A:
2179 decode_block(pbi, xd,
2180#if CONFIG_SUPERTX
2181 supertx_enabled,
2182#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002183 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002184 decode_block(pbi, xd,
2185#if CONFIG_SUPERTX
2186 supertx_enabled,
2187#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002188 mi_row + hbs, mi_col, r, partition, bsize2);
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, mi_col + hbs, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002194 break;
2195 case PARTITION_VERT_B:
2196 decode_block(pbi, xd,
2197#if CONFIG_SUPERTX
2198 supertx_enabled,
2199#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002200 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002201 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 + hbs, 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 + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002211 break;
2212#endif
2213 default: assert(0 && "Invalid partition type");
2214 }
2215 }
2216
2217#if CONFIG_SUPERTX
2218 if (supertx_enabled && read_token) {
2219 uint8_t *dst_buf[3];
2220 int dst_stride[3], i;
2221 int offset = mi_row * cm->mi_stride + mi_col;
2222
2223 set_segment_id_supertx(cm, mi_row, mi_col, bsize);
2224
David Barker3aec8d62017-01-31 14:55:32 +00002225#if CONFIG_DELTA_Q
2226 if (cm->delta_q_present_flag) {
2227 for (i = 0; i < MAX_SEGMENTS; i++) {
2228 xd->plane[0].seg_dequant[i][0] =
2229 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
2230 xd->plane[0].seg_dequant[i][1] =
2231 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
2232 xd->plane[1].seg_dequant[i][0] =
2233 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2234 xd->plane[1].seg_dequant[i][1] =
2235 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2236 xd->plane[2].seg_dequant[i][0] =
2237 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2238 xd->plane[2].seg_dequant[i][1] =
2239 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2240 }
2241 }
2242#endif
2243
Yaowu Xuc27fc142016-08-22 16:08:15 -07002244 xd->mi = cm->mi_grid_visible + offset;
2245 xd->mi[0] = cm->mi + offset;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002246#if CONFIG_DEPENDENT_HORZTILES
2247 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2248 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols,
2249 cm->dependent_horz_tiles);
2250#else
Jingning Han5b7706a2016-12-21 09:55:10 -08002251 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2252 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002253#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002254 set_skip_context(xd, mi_row, mi_col);
2255 skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
2256 if (skip) {
2257 reset_skip_context(xd, bsize);
2258 } else {
2259#if CONFIG_EXT_TX
Sarah Parkere68a3e42017-02-16 14:03:24 -08002260 if (get_ext_tx_types(supertx_size, bsize, 1, cm->reduced_tx_set_used) >
2261 1) {
2262 const int eset =
2263 get_ext_tx_set(supertx_size, bsize, 1, cm->reduced_tx_set_used);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002264 if (eset > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002265 txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
Michael Bebenita6048d052016-08-25 14:40:54 -07002266 cm->fc->inter_ext_tx_prob[eset][supertx_size],
2267 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002268 if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
2269 }
2270 }
2271#else
2272 if (supertx_size < TX_32X32) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002273 txfm = aom_read_tree(r, av1_ext_tx_tree,
Jingning Han8f6eb182016-10-19 13:48:57 -07002274 cm->fc->inter_ext_tx_prob[supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002275 if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
2276 }
2277#endif // CONFIG_EXT_TX
2278 }
2279
Yaowu Xuf883b422016-08-30 14:01:10 -07002280 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002281 for (i = 0; i < MAX_MB_PLANE; i++) {
2282 dst_buf[i] = xd->plane[i].dst.buf;
2283 dst_stride[i] = xd->plane[i].dst.stride;
2284 }
2285 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
2286 bsize, dst_buf, dst_stride);
2287
2288 if (!skip) {
2289 int eobtotal = 0;
2290 MB_MODE_INFO *mbmi;
2291 set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
2292 mbmi = &xd->mi[0]->mbmi;
2293 mbmi->tx_type = txfm;
2294 assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
2295 for (i = 0; i < MAX_MB_PLANE; ++i) {
2296 const struct macroblockd_plane *const pd = &xd->plane[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002297 int row, col;
Angie Chiang7fcfee42017-02-24 15:51:03 -08002298 const TX_SIZE tx_size = get_tx_size(i, xd);
Jingning Han5b7706a2016-12-21 09:55:10 -08002299 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Jingning Han32b20282016-10-28 15:42:44 -07002300 const int stepr = tx_size_high_unit[tx_size];
2301 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han5b7706a2016-12-21 09:55:10 -08002302 const int max_blocks_wide = max_block_wide(xd, plane_bsize, i);
2303 const int max_blocks_high = max_block_high(xd, plane_bsize, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002304
2305 for (row = 0; row < max_blocks_high; row += stepr)
2306 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07002307 eobtotal += reconstruct_inter_block(
2308 cm, xd, r, mbmi->segment_id_supertx, i, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002309 }
Jingning Han24f24a52016-12-27 10:13:28 -08002310 if ((unify_bsize || !(subsize < BLOCK_8X8)) && eobtotal == 0) skip = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002311 }
2312 set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
2313 }
2314#endif // CONFIG_SUPERTX
2315
2316#if CONFIG_EXT_PARTITION_TYPES
Alex Converseffabff32017-03-27 09:52:19 -07002317 update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002318#else
2319 // update partition context
2320 if (bsize >= BLOCK_8X8 &&
2321 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
Jingning Han1beb0102016-12-07 11:08:30 -08002322 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
David Barkerf8935c92016-10-26 14:54:06 +01002323#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xud71be782016-10-14 08:47:03 -07002324
Jean-Marc Valin01435132017-02-18 14:12:53 -05002325#if CONFIG_CDEF
David Barker9739f362016-11-10 09:29:32 +00002326#if CONFIG_EXT_PARTITION
2327 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128) {
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002328 if (!sb_all_skip(cm, mi_row, mi_col)) {
Jean-Marc Valin1df427e2017-03-23 18:32:57 -04002329 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2330 aom_read_literal(r, cm->cdef_bits, ACCT_STR);
David Barker9739f362016-11-10 09:29:32 +00002331 } else {
Jean-Marc Valin1df427e2017-03-23 18:32:57 -04002332 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2333 0;
David Barker9739f362016-11-10 09:29:32 +00002334 }
2335 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64) {
2336#else
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002337 if (bsize == BLOCK_64X64) {
David Barker9739f362016-11-10 09:29:32 +00002338#endif
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002339 if (!sb_all_skip(cm, mi_row, mi_col)) {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002340 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2341 aom_read_literal(r, cm->cdef_bits, ACCT_STR);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002342 } else {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002343 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2344 0;
Yaowu Xud71be782016-10-14 08:47:03 -07002345 }
2346 }
Jean-Marc Valin01435132017-02-18 14:12:53 -05002347#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002348}
2349
Yaowu Xuc27fc142016-08-22 16:08:15 -07002350static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
2351 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002352 struct aom_internal_error_info *error_info,
Alex Converseeb780e72016-12-13 12:46:41 -08002353 aom_reader *r,
2354#if CONFIG_ANS && ANS_MAX_SYMBOLS
2355 int window_size,
2356#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
2357 aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002358 // Validate the calculated partition length. If the buffer
2359 // described by the partition can't be fully read, then restrict
2360 // it to the portion that can be (for EC mode) or throw an error.
2361 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002362 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002363 "Truncated packet or corrupt tile length");
2364
Alex Converse2cdf0d82016-12-13 13:53:09 -08002365#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08002366 r->window_size = window_size;
Alex Converse2cdf0d82016-12-13 13:53:09 -08002367#endif
Alex Converse346440b2017-01-03 13:47:37 -08002368 if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07002369 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002370 "Failed to allocate bool decoder %d", 1);
2371}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002372
Angie Chiang800df032017-03-22 11:14:12 -07002373#if !CONFIG_PVQ && !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET) && !CONFIG_LV_MAP
Yaowu Xuf883b422016-08-30 14:01:10 -07002374static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
2375 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002376 int i, j, k, l, m;
Alex Conversea9598cd2017-02-03 14:18:05 -08002377#if CONFIG_EC_ADAPT
2378 const int node_limit = UNCONSTRAINED_NODES - 1;
2379#else
Thomas9ac55082016-09-23 18:04:17 +01002380 const int node_limit = UNCONSTRAINED_NODES;
Alex Conversea9598cd2017-02-03 14:18:05 -08002381#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002382
Michael Bebenita6048d052016-08-25 14:40:54 -07002383 if (aom_read_bit(r, ACCT_STR))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002384 for (i = 0; i < PLANE_TYPES; ++i)
2385 for (j = 0; j < REF_TYPES; ++j)
2386 for (k = 0; k < COEF_BANDS; ++k)
2387 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
Thomas9ac55082016-09-23 18:04:17 +01002388 for (m = 0; m < node_limit; ++m)
Michael Bebenita6048d052016-08-25 14:40:54 -07002389 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002390}
2391
Yaowu Xuf883b422016-08-30 14:01:10 -07002392static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002393 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
2394 TX_SIZE tx_size;
Jingning Han83630632016-12-16 11:27:25 -08002395 for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002396 read_coef_probs_common(fc->coef_probs[tx_size], r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002397}
Yushin Cho77bba8d2016-11-04 16:36:56 -07002398#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002399
Yaowu Xuf883b422016-08-30 14:01:10 -07002400static void setup_segmentation(AV1_COMMON *const cm,
2401 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002402 struct segmentation *const seg = &cm->seg;
2403 int i, j;
2404
2405 seg->update_map = 0;
2406 seg->update_data = 0;
2407
Yaowu Xuf883b422016-08-30 14:01:10 -07002408 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002409 if (!seg->enabled) return;
2410
2411 // Segmentation map update
2412 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2413 seg->update_map = 1;
2414 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002415 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002416 }
2417 if (seg->update_map) {
2418 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2419 seg->temporal_update = 0;
2420 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002421 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002422 }
2423 }
2424
2425 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07002426 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002427 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002428 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002429
Yaowu Xuf883b422016-08-30 14:01:10 -07002430 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002431
2432 for (i = 0; i < MAX_SEGMENTS; i++) {
2433 for (j = 0; j < SEG_LVL_MAX; j++) {
2434 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002435 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002436 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002437 av1_enable_segfeature(seg, i, j);
2438 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
2439 if (av1_is_segfeature_signed(j))
2440 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002441 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002442 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002443 }
2444 }
2445 }
2446}
2447
2448#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002449static void decode_restoration_mode(AV1_COMMON *cm,
2450 struct aom_read_bit_buffer *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002451 int p;
2452 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002453 if (aom_rb_read_bit(rb)) {
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002454 rsi->frame_restoration_type =
2455 aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002456 } else {
2457 rsi->frame_restoration_type =
2458 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
2459 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002460 for (p = 1; p < MAX_MB_PLANE; ++p) {
2461 cm->rst_info[p].frame_restoration_type =
2462 aom_rb_read_bit(rb) ? RESTORE_WIENER : RESTORE_NONE;
2463 }
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002464
2465 cm->rst_info[0].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2466 cm->rst_info[1].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2467 cm->rst_info[2].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2468 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
2469 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
2470 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
2471 rsi = &cm->rst_info[0];
2472 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2473 if (rsi->restoration_tilesize != RESTORATION_TILESIZE_MAX) {
2474 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2475 }
2476 cm->rst_info[1].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2477 cm->rst_info[2].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2478 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002479}
2480
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002481static void read_wiener_filter(WienerInfo *wiener_info, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002482 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002483 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2484 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002485 wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002486 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2487 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002488 wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002489 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2490 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002491 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002492 wiener_info->vfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002493 -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
2494 wiener_info->vfilter[2]);
2495
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002496 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002497 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2498 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002499 wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002500 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2501 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002502 wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002503 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2504 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002505 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002506 wiener_info->hfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002507 -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
2508 wiener_info->hfilter[2]);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002509}
2510
2511static void read_sgrproj_filter(SgrprojInfo *sgrproj_info, aom_reader *rb) {
2512 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
2513 sgrproj_info->xqd[0] =
2514 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN0;
2515 sgrproj_info->xqd[1] =
2516 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN1;
2517}
2518
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002519static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002520 int i, p;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002521 const int ntiles = av1_get_rest_ntiles(cm->width, cm->height,
2522 cm->rst_info[0].restoration_tilesize,
2523 NULL, NULL, NULL, NULL);
2524 const int ntiles_uv = av1_get_rest_ntiles(
2525 ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
2526 ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y),
2527 cm->rst_info[1].restoration_tilesize, NULL, NULL, NULL, NULL);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002528 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002529 if (rsi->frame_restoration_type != RESTORE_NONE) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002530 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002531 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002532 rsi->restoration_type[i] =
2533 aom_read_tree(rb, av1_switchable_restore_tree,
2534 cm->fc->switchable_restore_prob, ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002535 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002536 read_wiener_filter(&rsi->wiener_info[i], rb);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002537 } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002538 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002539 }
2540 }
2541 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002542 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002543 if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002544 rsi->restoration_type[i] = RESTORE_WIENER;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002545 read_wiener_filter(&rsi->wiener_info[i], rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002546 } else {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002547 rsi->restoration_type[i] = RESTORE_NONE;
2548 }
2549 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002550 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002551 for (i = 0; i < ntiles; ++i) {
2552 if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
2553 rsi->restoration_type[i] = RESTORE_SGRPROJ;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002554 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
2555 } else {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002556 rsi->restoration_type[i] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002557 }
2558 }
2559 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002560 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002561 for (p = 1; p < MAX_MB_PLANE; ++p) {
2562 rsi = &cm->rst_info[p];
2563 if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08002564 for (i = 0; i < ntiles_uv; ++i) {
2565 if (ntiles_uv > 1)
2566 rsi->restoration_type[i] =
2567 aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR) ? RESTORE_WIENER
2568 : RESTORE_NONE;
2569 else
2570 rsi->restoration_type[i] = RESTORE_WIENER;
2571 if (rsi->restoration_type[i] == RESTORE_WIENER) {
2572 read_wiener_filter(&rsi->wiener_info[i], rb);
2573 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002574 }
2575 }
2576 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002577}
2578#endif // CONFIG_LOOP_RESTORATION
2579
Yaowu Xuf883b422016-08-30 14:01:10 -07002580static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002581 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07002582 lf->filter_level = aom_rb_read_literal(rb, 6);
2583 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002584
2585 // Read in loop filter deltas applied at the MB level based on mode or ref
2586 // frame.
2587 lf->mode_ref_delta_update = 0;
2588
Yaowu Xuf883b422016-08-30 14:01:10 -07002589 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002590 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002591 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002592 if (lf->mode_ref_delta_update) {
2593 int i;
2594
2595 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002596 if (aom_rb_read_bit(rb))
2597 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002598
2599 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002600 if (aom_rb_read_bit(rb))
2601 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002602 }
2603 }
2604}
2605
Jean-Marc Valin01435132017-02-18 14:12:53 -05002606#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002607static void setup_cdef(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002608 int i;
2609 cm->cdef_bits = aom_rb_read_literal(rb, 2);
2610 cm->nb_cdef_strengths = 1 << cm->cdef_bits;
2611 for (i = 0; i < cm->nb_cdef_strengths; i++) {
2612 cm->cdef_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
Jean-Marc Valine9f77422017-03-22 17:09:51 -04002613 cm->cdef_uv_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002614 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002615}
Jean-Marc Valin01435132017-02-18 14:12:53 -05002616#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002617
Yaowu Xuf883b422016-08-30 14:01:10 -07002618static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2619 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002620}
2621
Yaowu Xuf883b422016-08-30 14:01:10 -07002622static void setup_quantization(AV1_COMMON *const cm,
2623 struct aom_read_bit_buffer *rb) {
2624 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002625 cm->y_dc_delta_q = read_delta_q(rb);
2626 cm->uv_dc_delta_q = read_delta_q(rb);
2627 cm->uv_ac_delta_q = read_delta_q(rb);
2628 cm->dequant_bit_depth = cm->bit_depth;
2629#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002630 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002631 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002632 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2633 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002634 } else {
2635 cm->min_qmlevel = 0;
2636 cm->max_qmlevel = 0;
2637 }
2638#endif
2639}
2640
Yaowu Xuf883b422016-08-30 14:01:10 -07002641static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002642 // Build y/uv dequant values based on segmentation.
2643 int i = 0;
2644#if CONFIG_AOM_QM
2645 int lossless;
2646 int j = 0;
2647 int qmlevel;
2648 int using_qm = cm->using_qmatrix;
2649 int minqm = cm->min_qmlevel;
2650 int maxqm = cm->max_qmlevel;
2651#endif
2652#if CONFIG_NEW_QUANT
2653 int b;
2654 int dq;
2655#endif // CONFIG_NEW_QUANT
2656 if (cm->seg.enabled) {
2657 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002658 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002659 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002660 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2661 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002662 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002663 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002664 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002665 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002666#if CONFIG_AOM_QM
2667 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2668 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2669 // NB: depends on base index so there is only 1 set per frame
2670 // No quant weighting when lossless or signalled not using QM
2671 qmlevel = (lossless || using_qm == 0)
2672 ? NUM_QM_LEVELS - 1
2673 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2674 for (j = 0; j < TX_SIZES; ++j) {
2675 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2676 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2677 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2678 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2679 }
2680#endif // CONFIG_AOM_QM
2681#if CONFIG_NEW_QUANT
2682 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2683 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002684 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002685 cm->y_dequant_nuq[i][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002686 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002687 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002688 }
2689 }
2690#endif // CONFIG_NEW_QUANT
2691 }
2692 } else {
2693 const int qindex = cm->base_qindex;
2694 // When segmentation is disabled, only the first value is used. The
2695 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002696 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2697 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002698 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002699 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002700 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002701 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002702#if CONFIG_AOM_QM
2703 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2704 cm->uv_ac_delta_q == 0;
2705 // No quant weighting when lossless or signalled not using QM
2706 qmlevel = (lossless || using_qm == 0)
2707 ? NUM_QM_LEVELS - 1
2708 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2709 for (j = 0; j < TX_SIZES; ++j) {
2710 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2711 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2712 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2713 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2714 }
2715#endif
2716#if CONFIG_NEW_QUANT
2717 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2718 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002719 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002720 cm->y_dequant_nuq[0][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002721 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002722 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002723 }
2724 }
2725#endif // CONFIG_NEW_QUANT
2726 }
2727}
2728
Angie Chiang5678ad92016-11-21 09:38:40 -08002729static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002730 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07002731 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002732}
2733
Yaowu Xuf883b422016-08-30 14:01:10 -07002734static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002735 cm->render_width = cm->width;
2736 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002737 if (aom_rb_read_bit(rb))
2738 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002739}
2740
Fergus Simpsone7508412017-03-14 18:14:09 -07002741#if CONFIG_LOOP_RESTORATION && CONFIG_FRAME_SUPERRES
2742// TODO(afergs): make "struct aom_read_bit_buffer *const rb"?
2743static void setup_superres_size(AV1_COMMON *const cm,
2744 struct aom_read_bit_buffer *rb, int *width,
2745 int *height) {
2746 // TODO(afergs): Test this behaviour
2747 // Frame superres is probably in compatible with this render resolution
2748 assert(cm->width == cm->render_width && cm->height == cm->render_height);
2749
2750 cm->superres_width = cm->width;
2751 cm->superres_height = cm->height;
2752 if (aom_rb_read_bit(rb)) {
2753 cm->superres_scale_numerator =
2754 (uint8_t)aom_rb_read_literal(rb, SUPERRES_SCALE_BITS);
2755 cm->superres_scale_numerator += SUPERRES_SCALE_NUMERATOR_MIN;
2756 // Don't edit cm->width or cm->height directly, or the buffers won't get
2757 // resized correctly
2758 // TODO(afergs): Should the render resolution not be modified? It's the same
2759 // by default (ie. when it isn't sent)...
2760 // resize_context_buffers() will change cm->width to equal cm->render_width,
2761 // then they'll be the same again
2762 *width = cm->render_width =
2763 cm->width * cm->superres_scale_numerator / SUPERRES_SCALE_DENOMINATOR;
2764 *height = cm->render_height =
2765 cm->height * cm->superres_scale_numerator / SUPERRES_SCALE_DENOMINATOR;
2766 } else {
2767 // 1:1 scaling - ie. no scaling, scale not provided
2768 cm->superres_scale_numerator = SUPERRES_SCALE_DENOMINATOR;
2769 }
2770}
2771#endif // CONFIG_LOOP_RESTORATION && CONFIG_FRAME_SUPERRES
2772
Yaowu Xuf883b422016-08-30 14:01:10 -07002773static void resize_mv_buffer(AV1_COMMON *cm) {
2774 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002775 cm->cur_frame->mi_rows = cm->mi_rows;
2776 cm->cur_frame->mi_cols = cm->mi_cols;
2777 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002778 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002779 sizeof(*cm->cur_frame->mvs)));
2780}
2781
Yaowu Xuf883b422016-08-30 14:01:10 -07002782static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002783#if CONFIG_SIZE_LIMIT
2784 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002785 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002786 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2787 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2788#endif
2789 if (cm->width != width || cm->height != height) {
2790 const int new_mi_rows =
2791 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2792 const int new_mi_cols =
2793 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2794
Yaowu Xuf883b422016-08-30 14:01:10 -07002795 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002796 // dimensions as well as the overall size.
2797 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002798 if (av1_alloc_context_buffers(cm, width, height))
2799 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002800 "Failed to allocate context buffers");
2801 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002802 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002803 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002804 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002805 cm->width = width;
2806 cm->height = height;
2807 }
2808 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2809 cm->mi_cols > cm->cur_frame->mi_cols) {
2810 resize_mv_buffer(cm);
2811 }
2812}
2813
Yaowu Xuf883b422016-08-30 14:01:10 -07002814static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002815 int width, height;
2816 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002817 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002818 setup_render_size(cm, rb);
Fergus Simpsone7508412017-03-14 18:14:09 -07002819#if CONFIG_LOOP_RESTORATION && CONFIG_FRAME_SUPERRES
2820 setup_superres_size(cm, rb, &width, &height);
2821#endif // CONFIG_LOOP_RESTORATION && CONFIG_FRAME_SUPERRES
2822 resize_context_buffers(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002823
2824 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002825 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002826 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2827 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002828#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002829 cm->use_highbitdepth,
2830#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002831 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002832 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2833 pool->cb_priv)) {
2834 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002835 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002836 "Failed to allocate frame buffer");
2837 }
2838 unlock_buffer_pool(pool);
2839
2840 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2841 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2842 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2843 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2844 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2845 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2846 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2847}
2848
Yaowu Xuf883b422016-08-30 14:01:10 -07002849static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002850 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002851 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002852 int this_xss, int this_yss) {
2853 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2854 ref_yss == this_yss;
2855}
2856
Yaowu Xuf883b422016-08-30 14:01:10 -07002857static void setup_frame_size_with_refs(AV1_COMMON *cm,
2858 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002859 int width, height;
2860 int found = 0, i;
2861 int has_valid_ref_frame = 0;
2862 BufferPool *const pool = cm->buffer_pool;
2863 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002864 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002865 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2866 width = buf->y_crop_width;
2867 height = buf->y_crop_height;
2868 cm->render_width = buf->render_width;
2869 cm->render_height = buf->render_height;
2870 found = 1;
2871 break;
2872 }
2873 }
2874
2875 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002876 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002877 setup_render_size(cm, rb);
2878 }
2879
2880 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002881 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002882 "Invalid frame size");
2883
2884 // Check to make sure at least one of frames that this frame references
2885 // has valid dimensions.
2886 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2887 RefBuffer *const ref_frame = &cm->frame_refs[i];
2888 has_valid_ref_frame |=
2889 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2890 ref_frame->buf->y_crop_height, width, height);
2891 }
2892 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002893 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002894 "Referenced frame has invalid size");
2895 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2896 RefBuffer *const ref_frame = &cm->frame_refs[i];
2897 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
2898 ref_frame->buf->subsampling_x,
2899 ref_frame->buf->subsampling_y, cm->bit_depth,
2900 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07002901 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002902 "Referenced frame has incompatible color format");
2903 }
2904
2905 resize_context_buffers(cm, width, height);
2906
2907 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002908 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002909 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2910 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002911#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002912 cm->use_highbitdepth,
2913#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002914 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002915 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2916 pool->cb_priv)) {
2917 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002918 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002919 "Failed to allocate frame buffer");
2920 }
2921 unlock_buffer_pool(pool);
2922
2923 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2924 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2925 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2926 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2927 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2928 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2929 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2930}
2931
Yaowu Xuf883b422016-08-30 14:01:10 -07002932static void read_tile_info(AV1Decoder *const pbi,
2933 struct aom_read_bit_buffer *const rb) {
2934 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002935#if CONFIG_EXT_TILE
2936// Read the tile width/height
2937#if CONFIG_EXT_PARTITION
2938 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002939 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
2940 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002941 } else
2942#endif // CONFIG_EXT_PARTITION
2943 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002944 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
2945 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002946 }
2947
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002948#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002949 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002950#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002951
Yaowu Xuc27fc142016-08-22 16:08:15 -07002952 cm->tile_width <<= cm->mib_size_log2;
2953 cm->tile_height <<= cm->mib_size_log2;
2954
Yaowu Xuf883b422016-08-30 14:01:10 -07002955 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
2956 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002957
2958 // Get the number of tiles
2959 cm->tile_cols = 1;
2960 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
2961
2962 cm->tile_rows = 1;
2963 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
2964
2965 if (cm->tile_cols * cm->tile_rows > 1) {
2966 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07002967 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2968 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002969 }
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002970
2971#if CONFIG_DEPENDENT_HORZTILES
2972 if (cm->tile_rows <= 1)
2973 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
2974 else
2975 cm->dependent_horz_tiles = 0;
2976#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002977#else
2978 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07002979 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002980
2981 // columns
2982 max_ones = max_log2_tile_cols - min_log2_tile_cols;
2983 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07002984 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002985
2986 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07002987 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002988 "Invalid number of tile columns");
2989
2990 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07002991 cm->log2_tile_rows = aom_rb_read_bit(rb);
2992 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002993#if CONFIG_DEPENDENT_HORZTILES
2994 if (cm->log2_tile_rows != 0)
2995 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
2996 else
2997 cm->dependent_horz_tiles = 0;
2998#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002999#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003000 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003001#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003002
Yaowu Xuc27fc142016-08-22 16:08:15 -07003003 cm->tile_cols = 1 << cm->log2_tile_cols;
3004 cm->tile_rows = 1 << cm->log2_tile_rows;
3005
3006 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
3007 cm->tile_width >>= cm->log2_tile_cols;
3008 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
3009 cm->tile_height >>= cm->log2_tile_rows;
3010
3011 // round to integer multiples of superblock size
3012 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
3013 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
3014
Thomas Davies4974e522016-11-07 17:44:05 +00003015// tile size magnitude
3016#if !CONFIG_TILE_GROUPS
3017 if (cm->tile_rows > 1 || cm->tile_cols > 1)
3018#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003019 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003020#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00003021
Thomas Davies80188d12016-10-26 16:08:35 -07003022#if CONFIG_TILE_GROUPS
3023 // Store an index to the location of the tile group information
3024 pbi->tg_size_bit_offset = rb->bit_offset;
3025 pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
3026 if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
3027 pbi->tg_start =
3028 aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3029 pbi->tg_size =
3030 1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3031 }
3032#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003033}
3034
3035static int mem_get_varsize(const uint8_t *src, const int sz) {
3036 switch (sz) {
3037 case 1: return src[0];
3038 case 2: return mem_get_le16(src);
3039 case 3: return mem_get_le24(src);
3040 case 4: return mem_get_le32(src);
3041 default: assert("Invalid size" && 0); return -1;
3042 }
3043}
3044
3045#if CONFIG_EXT_TILE
3046// Reads the next tile returning its size and adjusting '*data' accordingly
3047// based on 'is_last'.
3048static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07003049 struct aom_internal_error_info *error_info,
3050 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003051 void *decrypt_state,
3052 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
3053 int tile_size_bytes, int col, int row) {
3054 size_t size;
3055
3056 size_t copy_size = 0;
3057 const uint8_t *copy_data = NULL;
3058
3059 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003060 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003061 "Truncated packet or corrupt tile length");
3062 if (decrypt_cb) {
3063 uint8_t be_data[4];
3064 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3065
3066 // Only read number of bytes in cm->tile_size_bytes.
3067 size = mem_get_varsize(be_data, tile_size_bytes);
3068 } else {
3069 size = mem_get_varsize(*data, tile_size_bytes);
3070 }
3071
3072 // The top bit indicates copy mode
3073 if ((size >> (tile_size_bytes * 8 - 1)) == 1) {
3074 // The remaining bits in the top byte signal the row offset
3075 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
3076
3077 // Currently, only use tiles in same column as reference tiles.
3078 copy_data = tile_buffers[row - offset][col].data;
3079 copy_size = tile_buffers[row - offset][col].size;
3080 size = 0;
3081 }
3082
3083 *data += tile_size_bytes;
3084
3085 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003086 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003087 "Truncated packet or corrupt tile size");
3088
3089 if (size > 0) {
3090 tile_buffers[row][col].data = *data;
3091 tile_buffers[row][col].size = size;
3092 } else {
3093 tile_buffers[row][col].data = copy_data;
3094 tile_buffers[row][col].size = copy_size;
3095 }
3096
3097 *data += size;
3098
3099 tile_buffers[row][col].raw_data_end = *data;
3100}
3101
3102static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003103 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003104 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003105 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003106 const int tile_cols = cm->tile_cols;
3107 const int tile_rows = cm->tile_rows;
3108 const int have_tiles = tile_cols * tile_rows > 1;
3109
3110 if (!have_tiles) {
3111 const uint32_t tile_size = data_end - data;
3112 tile_buffers[0][0].data = data;
3113 tile_buffers[0][0].size = tile_size;
3114 tile_buffers[0][0].raw_data_end = NULL;
3115 } else {
3116 // We locate only the tile buffers that are required, which are the ones
3117 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
3118 // need the last (bottom right) tile buffer, as we need to know where the
3119 // end of the compressed frame buffer is for proper superframe decoding.
3120
3121 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
3122 const uint8_t *const data_start = data;
3123
Yaowu Xuf883b422016-08-30 14:01:10 -07003124 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003125 const int single_row = pbi->dec_tile_row >= 0;
3126 const int tile_rows_start = single_row ? dec_tile_row : 0;
3127 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003128 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003129 const int single_col = pbi->dec_tile_col >= 0;
3130 const int tile_cols_start = single_col ? dec_tile_col : 0;
3131 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3132
3133 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
3134 const int tile_size_bytes = pbi->tile_size_bytes;
3135
3136 size_t tile_col_size;
3137 int r, c;
3138
3139 // Read tile column sizes for all columns (we need the last tile buffer)
3140 for (c = 0; c < tile_cols; ++c) {
3141 const int is_last = c == tile_cols - 1;
3142 if (!is_last) {
3143 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
3144 data += tile_col_size_bytes;
3145 tile_col_data_end[c] = data + tile_col_size;
3146 } else {
3147 tile_col_size = data_end - data;
3148 tile_col_data_end[c] = data_end;
3149 }
3150 data += tile_col_size;
3151 }
3152
3153 data = data_start;
3154
3155 // Read the required tile sizes.
3156 for (c = tile_cols_start; c < tile_cols_end; ++c) {
3157 const int is_last = c == tile_cols - 1;
3158
3159 if (c > 0) data = tile_col_data_end[c - 1];
3160
3161 if (!is_last) data += tile_col_size_bytes;
3162
3163 // Get the whole of the last column, otherwise stop at the required tile.
3164 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
3165 tile_buffers[r][c].col = c;
3166
3167 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3168 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3169 tile_size_bytes, c, r);
3170 }
3171 }
3172
3173 // If we have not read the last column, then read it to get the last tile.
3174 if (tile_cols_end != tile_cols) {
3175 c = tile_cols - 1;
3176
3177 data = tile_col_data_end[c - 1];
3178
3179 for (r = 0; r < tile_rows; ++r) {
3180 tile_buffers[r][c].col = c;
3181
3182 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3183 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3184 tile_size_bytes, c, r);
3185 }
3186 }
3187 }
3188}
3189#else
3190// Reads the next tile returning its size and adjusting '*data' accordingly
3191// based on 'is_last'.
3192static void get_tile_buffer(const uint8_t *const data_end,
3193 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07003194 struct aom_internal_error_info *error_info,
3195 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003196 void *decrypt_state, TileBufferDec *const buf) {
3197 size_t size;
3198
3199 if (!is_last) {
Yaowu Xu0a79a1b2017-02-17 13:04:54 -08003200 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003201 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003202 "Truncated packet or corrupt tile length");
3203
3204 if (decrypt_cb) {
3205 uint8_t be_data[4];
3206 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3207 size = mem_get_varsize(be_data, tile_size_bytes);
3208 } else {
3209 size = mem_get_varsize(*data, tile_size_bytes);
3210 }
3211 *data += tile_size_bytes;
3212
3213 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003214 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003215 "Truncated packet or corrupt tile size");
3216 } else {
3217 size = data_end - *data;
3218 }
3219
3220 buf->data = *data;
3221 buf->size = size;
3222
3223 *data += size;
3224}
3225
3226static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003227 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003228 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003229 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07003230#if CONFIG_TILE_GROUPS
3231 int r, c;
3232 const int tile_cols = cm->tile_cols;
3233 const int tile_rows = cm->tile_rows;
3234 int tc = 0;
3235 int first_tile_in_tg = 0;
3236 int hdr_offset;
3237 struct aom_read_bit_buffer rb_tg_hdr;
3238 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3239 const int num_tiles = tile_rows * tile_cols;
3240 const int num_bits = OD_ILOG(num_tiles) - 1;
3241 const int hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
3242 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
Fangwen Fu73126c02017-02-08 22:37:47 -08003243#if CONFIG_DEPENDENT_HORZTILES
3244 int tile_group_start_col = 0;
3245 int tile_group_start_row = 0;
3246#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003247
3248 for (r = 0; r < tile_rows; ++r) {
3249 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07003250 TileBufferDec *const buf = &tile_buffers[r][c];
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003251 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
Thomas Davies80188d12016-10-26 16:08:35 -07003252 hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
3253
3254 buf->col = c;
3255 if (hdr_offset) {
3256 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
3257 rb_tg_hdr.bit_offset = tg_size_bit_offset;
3258 if (num_tiles) {
3259 pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
3260 pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
Fangwen Fu73126c02017-02-08 22:37:47 -08003261#if CONFIG_DEPENDENT_HORZTILES
3262 tile_group_start_row = r;
3263 tile_group_start_col = c;
3264#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003265 }
3266 }
3267 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
3268 data += hdr_offset;
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003269 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last,
3270 &pbi->common.error, &data, pbi->decrypt_cb,
3271 pbi->decrypt_state, buf);
Fangwen Fu73126c02017-02-08 22:37:47 -08003272#if CONFIG_DEPENDENT_HORZTILES
3273 cm->tile_group_start_row[r][c] = tile_group_start_row;
3274 cm->tile_group_start_col[r][c] = tile_group_start_col;
3275#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003276 }
3277 }
3278#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003279 int r, c;
3280 const int tile_cols = cm->tile_cols;
3281 const int tile_rows = cm->tile_rows;
3282
3283 for (r = 0; r < tile_rows; ++r) {
3284 for (c = 0; c < tile_cols; ++c) {
3285 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
3286 TileBufferDec *const buf = &tile_buffers[r][c];
3287 buf->col = c;
3288 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
3289 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
3290 }
3291 }
Thomas Davies80188d12016-10-26 16:08:35 -07003292#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003293}
3294#endif // CONFIG_EXT_TILE
3295
Yushin Cho77bba8d2016-11-04 16:36:56 -07003296#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003297static void daala_dec_init(AV1_COMMON *const cm, daala_dec_ctx *daala_dec,
Nathan E. Eggeab083972016-12-28 15:31:46 -05003298 aom_reader *r) {
3299 daala_dec->r = r;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003300 od_adapt_ctx_reset(&daala_dec->state.adapt, 0);
3301
Yushin Cho70669122016-12-08 09:53:14 -10003302 // TODO(yushin) : activity masking info needs be signaled by a bitstream
3303 daala_dec->use_activity_masking = AV1_PVQ_ENABLE_ACTIVITY_MASKING;
3304
Yushin Cho7a428ba2017-01-12 16:28:49 -08003305#if !CONFIG_DAALA_DIST
3306 daala_dec->use_activity_masking = 0;
3307#endif
3308
Yushin Cho70669122016-12-08 09:53:14 -10003309 if (daala_dec->use_activity_masking)
3310 daala_dec->qm = OD_HVS_QM;
3311 else
3312 daala_dec->qm = OD_FLAT_QM;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003313
3314 od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
3315 daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
Yushin Cho70669122016-12-08 09:53:14 -10003316
3317 if (daala_dec->use_activity_masking) {
3318 int pli;
3319 int use_masking = daala_dec->use_activity_masking;
3320 int segment_id = 0;
3321 int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
3322
3323 for (pli = 0; pli < MAX_MB_PLANE; pli++) {
3324 int i;
3325 int q;
3326
3327 q = qindex;
3328 if (q <= OD_DEFAULT_QMS[use_masking][0][pli].interp_q << OD_COEFF_SHIFT) {
3329 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3330 &OD_DEFAULT_QMS[use_masking][0][pli], NULL);
3331 } else {
3332 i = 0;
3333 while (OD_DEFAULT_QMS[use_masking][i + 1][pli].qm_q4 != NULL &&
3334 q > OD_DEFAULT_QMS[use_masking][i + 1][pli].interp_q
3335 << OD_COEFF_SHIFT) {
3336 i++;
3337 }
3338 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3339 &OD_DEFAULT_QMS[use_masking][i][pli],
3340 &OD_DEFAULT_QMS[use_masking][i + 1][pli]);
3341 }
3342 }
3343 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07003344}
Yushin Cho70669122016-12-08 09:53:14 -10003345#endif // #if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003346
Yaowu Xuf883b422016-08-30 14:01:10 -07003347static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003348 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003349 AV1_COMMON *const cm = &pbi->common;
3350 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003351 const int tile_cols = cm->tile_cols;
3352 const int tile_rows = cm->tile_rows;
3353 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07003354 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003355#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003356 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003357 const int single_row = pbi->dec_tile_row >= 0;
3358 const int tile_rows_start = single_row ? dec_tile_row : 0;
3359 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003360 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003361 const int single_col = pbi->dec_tile_col >= 0;
3362 const int tile_cols_start = single_col ? dec_tile_col : 0;
3363 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3364 const int inv_col_order = pbi->inv_tile_order && !single_col;
3365 const int inv_row_order = pbi->inv_tile_order && !single_row;
3366#else
3367 const int tile_rows_start = 0;
3368 const int tile_rows_end = tile_rows;
3369 const int tile_cols_start = 0;
3370 const int tile_cols_end = tile_cols;
3371 const int inv_col_order = pbi->inv_tile_order;
3372 const int inv_row_order = pbi->inv_tile_order;
3373#endif // CONFIG_EXT_TILE
3374 int tile_row, tile_col;
3375
hui su0d103572017-03-01 17:58:01 -08003376#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003377 cm->do_subframe_update = n_tiles == 1;
hui su0d103572017-03-01 17:58:01 -08003378#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003379
3380 if (cm->lf.filter_level && !cm->skip_loop_filter &&
3381 pbi->lf_worker.data1 == NULL) {
3382 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07003383 aom_memalign(32, sizeof(LFWorkerData)));
3384 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003385 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003386 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003387 "Loop filter thread creation failed");
3388 }
3389 }
3390
3391 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3392 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3393 // Be sure to sync as we might be resuming after a failed frame decode.
3394 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003395 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
3396 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003397 }
3398
3399 assert(tile_rows <= MAX_TILE_ROWS);
3400 assert(tile_cols <= MAX_TILE_COLS);
3401
3402 get_tile_buffers(pbi, data, data_end, tile_buffers);
3403
3404 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003405 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003406 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003407 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003408 pbi->allocated_tiles = n_tiles;
3409 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003410#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003411 if (pbi->acct_enabled) {
3412 aom_accounting_reset(&pbi->accounting);
3413 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003414#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003415 // Load all tile information into tile_data.
3416 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3417 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3418 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
3419 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
3420
3421 td->cm = cm;
3422 td->xd = pbi->mb;
3423 td->xd.corrupted = 0;
3424 td->xd.counts =
3425 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3426 ? &cm->counts
3427 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003428 av1_zero(td->dqcoeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003429#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003430 av1_zero(td->pvq_ref_coeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003431#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003432 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003433 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003434 &td->bit_reader,
3435#if CONFIG_ANS && ANS_MAX_SYMBOLS
3436 1 << cm->ans_window_size_log2,
3437#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3438 pbi->decrypt_cb, pbi->decrypt_state);
Michael Bebenita6048d052016-08-25 14:40:54 -07003439#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003440 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003441 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003442 } else {
David Barkerd971f402016-10-25 13:52:07 +01003443 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003444 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003445#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003446 av1_init_macroblockd(cm, &td->xd,
3447#if CONFIG_PVQ
3448 td->pvq_ref_coeff,
3449#endif
3450 td->dqcoeff);
3451#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003452 daala_dec_init(cm, &td->xd.daala_dec, &td->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003453#endif
Thomas Daviesf77d4ad2017-01-10 18:55:42 +00003454#if CONFIG_EC_ADAPT
3455 // Initialise the tile context from the frame context
3456 td->tctx = *cm->fc;
3457 td->xd.tile_ctx = &td->tctx;
3458#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003459#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003460 td->xd.plane[0].color_index_map = td->color_index_map[0];
3461 td->xd.plane[1].color_index_map = td->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003462#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003463 }
3464 }
3465
3466 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3467 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
3468 int mi_row = 0;
3469 TileInfo tile_info;
3470
Yaowu Xuf883b422016-08-30 14:01:10 -07003471 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003472
3473 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3474 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
3475 TileData *const td = pbi->tile_data + tile_cols * row + col;
Michael Bebenita6048d052016-08-25 14:40:54 -07003476#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003477 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003478 td->bit_reader.accounting->last_tell_frac =
3479 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003480 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003481#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003482
Yaowu Xuf883b422016-08-30 14:01:10 -07003483 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003484
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003485#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003486#if CONFIG_TILE_GROUPS
3487 av1_tile_set_tg_boundary(&tile_info, cm, tile_row, tile_col);
3488 if (!cm->dependent_horz_tiles || tile_row == 0 ||
3489 tile_info.tg_horz_boundary) {
3490#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003491 if (!cm->dependent_horz_tiles || tile_row == 0) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003492#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003493 av1_zero_above_context(cm, tile_info.mi_col_start,
3494 tile_info.mi_col_end);
3495 }
3496#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003497 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003498#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003499
3500 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
3501 mi_row += cm->mib_size) {
3502 int mi_col;
3503
Yaowu Xuf883b422016-08-30 14:01:10 -07003504 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003505
3506 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
3507 mi_col += cm->mib_size) {
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003508 av1_update_boundary_info(cm, &tile_info, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003509 decode_partition(pbi, &td->xd,
3510#if CONFIG_SUPERTX
3511 0,
3512#endif // CONFIG_SUPERTX
3513 mi_row, mi_col, &td->bit_reader, cm->sb_size,
3514 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003515#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3516 detoken_and_recon_sb(pbi, &td->xd, mi_row, mi_col, &td->bit_reader,
3517 cm->sb_size);
3518#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003519 }
Angie Chiangd0916d92017-03-10 17:54:18 -08003520 aom_merge_corrupted_flag(&pbi->mb.corrupted, td->xd.corrupted);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003521 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003522 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003523 "Failed to decode tile data");
hui su0d103572017-03-01 17:58:01 -08003524#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003525 if (cm->do_subframe_update &&
3526 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
hui su0161a932017-01-24 14:12:11 -08003527 const int mi_rows_per_update =
3528 MI_SIZE * AOMMAX(cm->mi_rows / MI_SIZE / COEF_PROBS_BUFS, 1);
3529 if ((mi_row + MI_SIZE) % mi_rows_per_update == 0 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003530 mi_row + MI_SIZE < cm->mi_rows &&
3531 cm->coef_probs_update_idx < COEF_PROBS_BUFS - 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003532 av1_partial_adapt_probs(cm, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003533 ++cm->coef_probs_update_idx;
3534 }
3535 }
hui su0d103572017-03-01 17:58:01 -08003536#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003537 }
3538 }
3539
3540 assert(mi_row > 0);
3541
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003542// when Parallel deblocking is enabled, deblocking should not
3543// be interleaved with decoding. Instead, deblocking should be done
3544// after the entire frame is decoded.
3545#if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003546 // Loopfilter one tile row.
3547 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3548 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003549 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003550 const int lf_end = tile_info.mi_row_end - cm->mib_size;
3551
3552 // Delay the loopfilter if the first tile row is only
3553 // a single superblock high.
3554 if (lf_end <= 0) continue;
3555
3556 // Decoding has completed. Finish up the loop filter in this thread.
3557 if (tile_info.mi_row_end >= cm->mi_rows) continue;
3558
3559 winterface->sync(&pbi->lf_worker);
3560 lf_data->start = lf_start;
3561 lf_data->stop = lf_end;
3562 if (pbi->max_threads > 1) {
3563 winterface->launch(&pbi->lf_worker);
3564 } else {
3565 winterface->execute(&pbi->lf_worker);
3566 }
3567 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003568#endif // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003569
3570 // After loopfiltering, the last 7 row pixels in each superblock row may
3571 // still be changed by the longest loopfilter of the next superblock row.
3572 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003573 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003574 }
3575
3576#if CONFIG_VAR_TX
3577 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003578 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
3579 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003580#else
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003581#if CONFIG_PARALLEL_DEBLOCKING
3582 // Loopfilter all rows in the frame in the frame.
3583 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3584 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3585 winterface->sync(&pbi->lf_worker);
3586 lf_data->start = 0;
3587 lf_data->stop = cm->mi_rows;
3588 winterface->execute(&pbi->lf_worker);
3589 }
3590#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003591 // Loopfilter remaining rows in the frame.
3592 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3593 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3594 winterface->sync(&pbi->lf_worker);
3595 lf_data->start = lf_data->stop;
3596 lf_data->stop = cm->mi_rows;
3597 winterface->execute(&pbi->lf_worker);
3598 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003599#endif // CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003600#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003601 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003602 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003603
3604#if CONFIG_EXT_TILE
3605 if (n_tiles == 1) {
3606#if CONFIG_ANS
3607 return data_end;
3608#else
3609 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07003610 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003611#endif // CONFIG_ANS
3612 } else {
3613 // Return the end of the last tile buffer
3614 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3615 }
3616#else
3617#if CONFIG_ANS
3618 return data_end;
3619#else
3620 {
3621 // Get last tile data.
3622 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003623 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003624 }
3625#endif // CONFIG_ANS
3626#endif // CONFIG_EXT_TILE
3627}
3628
3629static int tile_worker_hook(TileWorkerData *const tile_data,
3630 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003631 AV1Decoder *const pbi = tile_data->pbi;
3632 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003633 int mi_row, mi_col;
3634
3635 if (setjmp(tile_data->error_info.jmp)) {
3636 tile_data->error_info.setjmp = 0;
Angie Chiangd0916d92017-03-10 17:54:18 -08003637 aom_merge_corrupted_flag(&tile_data->xd.corrupted, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003638 return 0;
3639 }
3640
3641 tile_data->error_info.setjmp = 1;
3642 tile_data->xd.error_info = &tile_data->error_info;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003643#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003644#if CONFIG_TILE_GROUPS
3645 if (!cm->dependent_horz_tiles || tile->tg_horz_boundary) {
3646#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003647 if (!cm->dependent_horz_tiles) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003648#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003649 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
3650 }
3651#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003652 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003653#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003654
3655 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
3656 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003657 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003658
3659 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3660 mi_col += cm->mib_size) {
3661 decode_partition(pbi, &tile_data->xd,
3662#if CONFIG_SUPERTX
3663 0,
3664#endif
3665 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
3666 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003667#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3668 detoken_and_recon_sb(pbi, &tile_data->xd, mi_row, mi_col,
3669 &tile_data->bit_reader, cm->sb_size);
3670#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003671 }
3672 }
3673 return !tile_data->xd.corrupted;
3674}
3675
3676// sorts in descending order
3677static int compare_tile_buffers(const void *a, const void *b) {
3678 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
3679 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
3680 return (int)(buf2->size - buf1->size);
3681}
3682
Yaowu Xuf883b422016-08-30 14:01:10 -07003683static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003684 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003685 AV1_COMMON *const cm = &pbi->common;
3686 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003687 const int tile_cols = cm->tile_cols;
3688 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003689 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
clang-format67948d32016-09-07 22:40:40 -07003690 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003691#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003692 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003693 const int single_row = pbi->dec_tile_row >= 0;
3694 const int tile_rows_start = single_row ? dec_tile_row : 0;
3695 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003696 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003697 const int single_col = pbi->dec_tile_col >= 0;
3698 const int tile_cols_start = single_col ? dec_tile_col : 0;
3699 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3700#else
3701 const int tile_rows_start = 0;
3702 const int tile_rows_end = tile_rows;
3703 const int tile_cols_start = 0;
3704 const int tile_cols_end = tile_cols;
3705#endif // CONFIG_EXT_TILE
3706 int tile_row, tile_col;
3707 int i;
3708
3709#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3710 int final_worker = -1;
3711#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3712
3713 assert(tile_rows <= MAX_TILE_ROWS);
3714 assert(tile_cols <= MAX_TILE_COLS);
3715
3716 assert(tile_cols * tile_rows > 1);
3717
Yaowu Xuc27fc142016-08-22 16:08:15 -07003718 // TODO(jzern): See if we can remove the restriction of passing in max
3719 // threads to the decoder.
3720 if (pbi->num_tile_workers == 0) {
3721 const int num_threads = pbi->max_threads & ~1;
3722 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07003723 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003724 // Ensure tile data offsets will be properly aligned. This may fail on
3725 // platforms without DECLARE_ALIGNED().
3726 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3727 CHECK_MEM_ERROR(
3728 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003729 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003730 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07003731 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003732 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003733 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003734 ++pbi->num_tile_workers;
3735
3736 winterface->init(worker);
3737 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003738 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003739 "Tile decoder thread creation failed");
3740 }
3741 }
3742 }
3743
3744 // Reset tile decoding hook
3745 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003746 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003747 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003748 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003749 worker->data1 = &pbi->tile_worker_data[i];
3750 worker->data2 = &pbi->tile_worker_info[i];
3751 }
3752
3753 // Initialize thread frame counts.
3754 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3755 for (i = 0; i < num_workers; ++i) {
3756 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003757 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003758 }
3759 }
3760
3761 // Load tile data into tile_buffers
3762 get_tile_buffers(pbi, data, data_end, tile_buffers);
3763
3764 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3765 // Sort the buffers in this tile row based on size in descending order.
3766 qsort(&tile_buffers[tile_row][tile_cols_start],
3767 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
3768 compare_tile_buffers);
3769
3770 // Rearrange the tile buffers in this tile row such that per-tile group
3771 // the largest, and presumably the most difficult tile will be decoded in
3772 // the main thread. This should help minimize the number of instances
3773 // where the main thread is waiting for a worker to complete.
3774 {
3775 int group_start;
3776 for (group_start = tile_cols_start; group_start < tile_cols_end;
3777 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003778 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003779 const TileBufferDec largest = tile_buffers[tile_row][group_start];
3780 memmove(&tile_buffers[tile_row][group_start],
3781 &tile_buffers[tile_row][group_start + 1],
3782 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
3783 tile_buffers[tile_row][group_end - 1] = largest;
3784 }
3785 }
3786
3787 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
3788 // Launch workers for individual columns
3789 for (i = 0; i < num_workers && tile_col < tile_cols_end;
3790 ++i, ++tile_col) {
3791 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07003792 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003793 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
3794 TileInfo *const tile_info = (TileInfo *)worker->data2;
3795
3796 twd->pbi = pbi;
3797 twd->xd = pbi->mb;
3798 twd->xd.corrupted = 0;
3799 twd->xd.counts =
3800 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3801 ? &twd->counts
3802 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003803 av1_zero(twd->dqcoeff);
3804 av1_tile_init(tile_info, cm, tile_row, buf->col);
3805 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003806 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003807 &twd->bit_reader,
3808#if CONFIG_ANS && ANS_MAX_SYMBOLS
3809 1 << cm->ans_window_size_log2,
3810#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3811 pbi->decrypt_cb, pbi->decrypt_state);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003812 av1_init_macroblockd(cm, &twd->xd,
3813#if CONFIG_PVQ
3814 twd->pvq_ref_coeff,
3815#endif
3816 twd->dqcoeff);
3817#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003818 daala_dec_init(cm, &twd->xd.daala_dec, &twd->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003819#endif
Yushin Chod767beb2017-03-24 10:15:47 -07003820#if CONFIG_EC_ADAPT
3821 // Initialise the tile context from the frame context
3822 twd->tctx = *cm->fc;
3823 twd->xd.tile_ctx = &twd->tctx;
3824#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003825#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003826 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
3827 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003828#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003829
3830 worker->had_error = 0;
3831 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
3832 winterface->execute(worker);
3833 } else {
3834 winterface->launch(worker);
3835 }
3836
3837#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3838 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
3839 final_worker = i;
3840 }
3841#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3842 }
3843
3844 // Sync all workers
3845 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003846 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003847 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07003848 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07003849 // info in cm. Additionally once the threads have been synced and an
3850 // error is detected, there's no point in continuing to decode tiles.
3851 pbi->mb.corrupted |= !winterface->sync(worker);
3852 }
3853 }
3854 }
3855
3856 // Accumulate thread frame counts.
3857 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3858 for (i = 0; i < num_workers; ++i) {
3859 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003860 av1_accumulate_frame_counts(&cm->counts, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003861 }
3862 }
3863
3864#if CONFIG_EXT_TILE
3865 // Return the end of the last tile buffer
3866 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3867#else
3868#if CONFIG_ANS
3869 return data_end;
3870#else
3871 assert(final_worker != -1);
3872 {
3873 TileWorkerData *const twd =
3874 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003875 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003876 }
3877#endif // CONFIG_ANS
3878#endif // CONFIG_EXT_TILE
3879}
3880
3881static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003882 AV1_COMMON *const cm = (AV1_COMMON *)data;
3883 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003884}
3885
Yaowu Xuf883b422016-08-30 14:01:10 -07003886static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3887 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003888 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003889 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003890 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003891 cm->bit_depth = AOM_BITS_8;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003892 }
3893
Yaowu Xuf883b422016-08-30 14:01:10 -07003894#if CONFIG_AOM_HIGHBITDEPTH
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003895 if (cm->bit_depth > AOM_BITS_8) {
3896 cm->use_highbitdepth = 1;
3897 } else {
Yaowu Xu345a22d2017-02-27 09:23:52 -08003898#if CONFIG_LOWBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003899 cm->use_highbitdepth = 0;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003900#else
3901 cm->use_highbitdepth = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003902#endif
3903 }
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003904#endif
3905
Yaowu Xuf883b422016-08-30 14:01:10 -07003906 cm->color_space = aom_rb_read_literal(rb, 3);
3907 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003908 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07003909 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003910 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003911 cm->subsampling_x = aom_rb_read_bit(rb);
3912 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003913 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003914 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003915 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07003916 if (aom_rb_read_bit(rb))
3917 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003918 "Reserved bit set");
3919 } else {
3920 cm->subsampling_y = cm->subsampling_x = 1;
3921 }
3922 } else {
3923 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
3924 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
3925 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
3926 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003927 if (aom_rb_read_bit(rb))
3928 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003929 "Reserved bit set");
3930 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003931 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003932 "4:4:4 color not supported in profile 0 or 2");
3933 }
3934 }
3935}
3936
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003937#if CONFIG_REFERENCE_BUFFER
3938void read_sequence_header(SequenceHeader *seq_params) {
3939 /* Placeholder for actually reading from the bitstream */
3940 seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
3941 seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
3942 seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
3943}
3944#endif
3945
Yaowu Xuf883b422016-08-30 14:01:10 -07003946static size_t read_uncompressed_header(AV1Decoder *pbi,
3947 struct aom_read_bit_buffer *rb) {
3948 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003949 MACROBLOCKD *const xd = &pbi->mb;
3950 BufferPool *const pool = cm->buffer_pool;
3951 RefCntBuffer *const frame_bufs = pool->frame_bufs;
3952 int i, mask, ref_index = 0;
3953 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003954
3955#if CONFIG_REFERENCE_BUFFER
3956 /* TODO: Move outside frame loop or inside key-frame branch */
3957 read_sequence_header(&pbi->seq_params);
3958#endif
3959
Yaowu Xuc27fc142016-08-22 16:08:15 -07003960 cm->last_frame_type = cm->frame_type;
3961 cm->last_intra_only = cm->intra_only;
3962
3963#if CONFIG_EXT_REFS
3964 // NOTE: By default all coded frames to be used as a reference
3965 cm->is_reference_frame = 1;
3966#endif // CONFIG_EXT_REFS
3967
Yaowu Xuf883b422016-08-30 14:01:10 -07003968 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
3969 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003970 "Invalid frame marker");
3971
Yaowu Xuf883b422016-08-30 14:01:10 -07003972 cm->profile = av1_read_profile(rb);
3973#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003974 if (cm->profile >= MAX_PROFILES)
Yaowu Xuf883b422016-08-30 14:01:10 -07003975 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003976 "Unsupported bitstream profile");
3977#else
3978 if (cm->profile >= PROFILE_2)
Yaowu Xuf883b422016-08-30 14:01:10 -07003979 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003980 "Unsupported bitstream profile");
3981#endif
3982
Yaowu Xuf883b422016-08-30 14:01:10 -07003983 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003984
3985 if (cm->show_existing_frame) {
Yaowu Xu415ba932016-12-27 11:17:32 -08003986 // Show an existing frame directly.
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003987 const int existing_frame_idx = aom_rb_read_literal(rb, 3);
3988 const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
Yaowu Xu415ba932016-12-27 11:17:32 -08003989#if CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003990 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003991 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3992 int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003993 /* Compare display_frame_id with ref_frame_id and check valid for
3994 * referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003995 if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
3996 cm->valid_for_referencing[existing_frame_idx] == 0)
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003997 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3998 "Reference buffer frame ID mismatch");
3999 }
4000#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004001 lock_buffer_pool(pool);
4002 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
4003 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07004004 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004005 "Buffer %d does not contain a decoded frame",
4006 frame_to_show);
4007 }
4008 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
4009 unlock_buffer_pool(pool);
4010
4011 cm->lf.filter_level = 0;
4012 cm->show_frame = 1;
4013 pbi->refresh_frame_flags = 0;
4014
4015 if (cm->frame_parallel_decode) {
4016 for (i = 0; i < REF_FRAMES; ++i)
4017 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
4018 }
4019
4020 return 0;
4021 }
4022
Yaowu Xuf883b422016-08-30 14:01:10 -07004023 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
4024 cm->show_frame = aom_rb_read_bit(rb);
4025 cm->error_resilient_mode = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004026#if CONFIG_REFERENCE_BUFFER
4027 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004028 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4029 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4030 int prev_frame_id = 0;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004031 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004032 prev_frame_id = cm->current_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004033 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004034 cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004035
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004036 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004037 int diff_frame_id;
4038 if (cm->current_frame_id > prev_frame_id) {
4039 diff_frame_id = cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004040 } else {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004041 diff_frame_id =
4042 (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004043 }
4044 /* Check current_frame_id for conformance */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004045 if (prev_frame_id == cm->current_frame_id ||
4046 diff_frame_id >= (1 << (frame_id_length - 1))) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004047 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4048 "Invalid value of current_frame_id");
4049 }
4050 }
4051 /* Check if some frames need to be marked as not valid for referencing */
4052 for (i = 0; i < REF_FRAMES; i++) {
4053 if (cm->frame_type == KEY_FRAME) {
4054 cm->valid_for_referencing[i] = 0;
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004055 } else if (cm->current_frame_id - (1 << diff_len) > 0) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004056 if (cm->ref_frame_id[i] > cm->current_frame_id ||
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004057 cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004058 cm->valid_for_referencing[i] = 0;
4059 } else {
4060 if (cm->ref_frame_id[i] > cm->current_frame_id &&
4061 cm->ref_frame_id[i] <
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004062 (1 << frame_id_length) + cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004063 cm->valid_for_referencing[i] = 0;
4064 }
4065 }
4066 }
4067#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004068 if (cm->frame_type == KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004069 if (!av1_read_sync_code(rb))
4070 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004071 "Invalid frame sync code");
4072
4073 read_bitdepth_colorspace_sampling(cm, rb);
4074 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
4075
4076 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4077 cm->frame_refs[i].idx = INVALID_IDX;
4078 cm->frame_refs[i].buf = NULL;
4079 }
4080
4081 setup_frame_size(cm, rb);
4082 if (pbi->need_resync) {
4083 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4084 pbi->need_resync = 0;
4085 }
Alex Converseeb780e72016-12-13 12:46:41 -08004086#if CONFIG_ANS && ANS_MAX_SYMBOLS
4087 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4088#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
Urvang Joshib100db72016-10-12 16:28:56 -07004089#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004090 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004091#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004092 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004093 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004094#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004095 if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004096#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004097 if (cm->error_resilient_mode) {
4098 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
4099 } else {
4100 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004101 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004102 ? RESET_FRAME_CONTEXT_ALL
4103 : RESET_FRAME_CONTEXT_CURRENT;
4104 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004105 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004106 ? RESET_FRAME_CONTEXT_CURRENT
4107 : RESET_FRAME_CONTEXT_NONE;
4108 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07004109 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004110 ? RESET_FRAME_CONTEXT_ALL
4111 : RESET_FRAME_CONTEXT_CURRENT;
4112 }
4113 }
4114
4115 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004116 if (!av1_read_sync_code(rb))
4117 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004118 "Invalid frame sync code");
4119
4120 read_bitdepth_colorspace_sampling(cm, rb);
4121
Yaowu Xuf883b422016-08-30 14:01:10 -07004122 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004123 setup_frame_size(cm, rb);
4124 if (pbi->need_resync) {
4125 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4126 pbi->need_resync = 0;
4127 }
Alex Converseeb780e72016-12-13 12:46:41 -08004128#if CONFIG_ANS && ANS_MAX_SYMBOLS
4129 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4130#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004131 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Yaowu Xuf883b422016-08-30 14:01:10 -07004132 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004133
4134#if CONFIG_EXT_REFS
4135 if (!pbi->refresh_frame_flags) {
4136 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
4137 // will not be used as a reference
4138 cm->is_reference_frame = 0;
4139 }
4140#endif // CONFIG_EXT_REFS
4141
4142 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004143 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004144 const int idx = cm->ref_frame_map[ref];
4145 RefBuffer *const ref_frame = &cm->frame_refs[i];
4146 ref_frame->idx = idx;
4147 ref_frame->buf = &frame_bufs[idx].buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07004148 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004149#if CONFIG_REFERENCE_BUFFER
4150 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004151 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4152 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4153 int delta_frame_id_minus1 = aom_rb_read_literal(rb, diff_len);
4154 int ref_frame_id =
4155 ((cm->current_frame_id - (delta_frame_id_minus1 + 1) +
4156 (1 << frame_id_length)) %
4157 (1 << frame_id_length));
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004158 /* Compare values derived from delta_frame_id_minus1 and
4159 * refresh_frame_flags. Also, check valid for referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004160 if (ref_frame_id != cm->ref_frame_id[ref] ||
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004161 cm->valid_for_referencing[ref] == 0)
4162 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4163 "Reference buffer frame ID mismatch");
4164 }
4165#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004166 }
4167
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004168#if CONFIG_FRAME_SIZE
4169 if (cm->error_resilient_mode == 0) {
4170 setup_frame_size_with_refs(cm, rb);
4171 } else {
4172 setup_frame_size(cm, rb);
4173 }
4174#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004175 setup_frame_size_with_refs(cm, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004176#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004177
Yaowu Xuf883b422016-08-30 14:01:10 -07004178 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Angie Chiang5678ad92016-11-21 09:38:40 -08004179 cm->interp_filter = read_frame_interp_filter(rb);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004180#if CONFIG_TEMPMV_SIGNALING
4181 if (!cm->error_resilient_mode) {
4182 cm->use_prev_frame_mvs = aom_rb_read_bit(rb);
4183 }
4184#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004185 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4186 RefBuffer *const ref_buf = &cm->frame_refs[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07004187#if CONFIG_AOM_HIGHBITDEPTH
4188 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004189 &ref_buf->sf, ref_buf->buf->y_crop_width,
4190 ref_buf->buf->y_crop_height, cm->width, cm->height,
4191 cm->use_highbitdepth);
4192#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004193 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004194 &ref_buf->sf, ref_buf->buf->y_crop_width,
4195 ref_buf->buf->y_crop_height, cm->width, cm->height);
4196#endif
4197 }
4198 }
4199 }
Fangwen Fu8d164de2016-12-14 13:40:54 -08004200#if CONFIG_TEMPMV_SIGNALING
4201 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
4202#endif
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004203
4204#if CONFIG_REFERENCE_BUFFER
4205 if (pbi->seq_params.frame_id_numbers_present_flag) {
4206 /* If bitmask is set, update reference frame id values and
4207 mark frames as valid for reference */
4208 int refresh_frame_flags =
4209 cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
4210 for (i = 0; i < REF_FRAMES; i++) {
4211 if ((refresh_frame_flags >> i) & 1) {
4212 cm->ref_frame_id[i] = cm->current_frame_id;
4213 cm->valid_for_referencing[i] = 1;
4214 }
4215 }
4216 }
4217#endif
4218
Yaowu Xuf883b422016-08-30 14:01:10 -07004219#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004220 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
4221#endif
4222 get_frame_new_buffer(cm)->color_space = cm->color_space;
4223 get_frame_new_buffer(cm)->color_range = cm->color_range;
4224 get_frame_new_buffer(cm)->render_width = cm->render_width;
4225 get_frame_new_buffer(cm)->render_height = cm->render_height;
4226
4227 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004228 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004229 "Keyframe / intra-only frame required to reset decoder"
4230 " state");
4231 }
4232
4233 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004234 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004235 ? REFRESH_FRAME_CONTEXT_FORWARD
4236 : REFRESH_FRAME_CONTEXT_BACKWARD;
4237 } else {
4238 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
4239 }
4240
Yaowu Xuf883b422016-08-30 14:01:10 -07004241 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07004242 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07004243 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004244
4245 // Generate next_ref_frame_map.
4246 lock_buffer_pool(pool);
4247 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
4248 if (mask & 1) {
4249 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
4250 ++frame_bufs[cm->new_fb_idx].ref_count;
4251 } else {
4252 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4253 }
4254 // Current thread holds the reference frame.
4255 if (cm->ref_frame_map[ref_index] >= 0)
4256 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4257 ++ref_index;
4258 }
4259
4260 for (; ref_index < REF_FRAMES; ++ref_index) {
4261 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4262
4263 // Current thread holds the reference frame.
4264 if (cm->ref_frame_map[ref_index] >= 0)
4265 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4266 }
4267 unlock_buffer_pool(pool);
4268 pbi->hold_ref_buf = 1;
4269
4270 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07004271 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004272
4273#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07004274 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004275#else
4276 set_sb_size(cm, BLOCK_64X64);
4277#endif // CONFIG_EXT_PARTITION
4278
4279 setup_loopfilter(cm, rb);
Jean-Marc Valin01435132017-02-18 14:12:53 -05004280#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01004281 setup_cdef(cm, rb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004282#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004283#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004284 decode_restoration_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004285#endif // CONFIG_LOOP_RESTORATION
4286 setup_quantization(cm, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004287#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004288 xd->bd = (int)cm->bit_depth;
4289#endif
4290
hui su0d103572017-03-01 17:58:01 -08004291#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuf883b422016-08-30 14:01:10 -07004292 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004293 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4294 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4295 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4296 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
4297 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4298 }
hui su0d103572017-03-01 17:58:01 -08004299#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004300
4301 setup_segmentation(cm, rb);
4302
Arild Fuldseth07441162016-08-15 15:07:52 +02004303#if CONFIG_DELTA_Q
4304 {
4305 struct segmentation *const seg = &cm->seg;
4306 int segment_quantizer_active = 0;
4307 for (i = 0; i < MAX_SEGMENTS; i++) {
4308 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
4309 segment_quantizer_active = 1;
4310 }
4311 }
4312
Thomas Daviesf6936102016-09-05 16:51:31 +01004313 cm->delta_q_res = 1;
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01004314 if (segment_quantizer_active == 0 && cm->base_qindex > 0) {
Arild Fuldseth07441162016-08-15 15:07:52 +02004315 cm->delta_q_present_flag = aom_rb_read_bit(rb);
4316 } else {
4317 cm->delta_q_present_flag = 0;
4318 }
4319 if (cm->delta_q_present_flag) {
4320 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01004321 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Arild Fuldseth07441162016-08-15 15:07:52 +02004322 }
4323 }
4324#endif
4325
Urvang Joshi454280d2016-10-14 16:51:44 -07004326 for (i = 0; i < MAX_SEGMENTS; ++i) {
4327 const int qindex = cm->seg.enabled
4328 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
4329 : cm->base_qindex;
4330 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
4331 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4332 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004333 }
4334
4335 setup_segmentation_dequant(cm);
Yue Cheneeacc4c2017-01-17 17:29:17 -08004336 cm->tx_mode = read_tx_mode(cm, xd, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004337 cm->reference_mode = read_frame_reference_mode(cm, rb);
4338
Sarah Parkere68a3e42017-02-16 14:03:24 -08004339#if CONFIG_EXT_TX
4340 cm->reduced_tx_set_used = aom_rb_read_bit(rb);
4341#endif // CONFIG_EXT_TX
4342
Yaowu Xuc27fc142016-08-22 16:08:15 -07004343 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004344 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004345
4346 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07004347 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004348 "Invalid header size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004349 return sz;
4350}
4351
4352#if CONFIG_EXT_TX
Thomas9ac55082016-09-23 18:04:17 +01004353#if !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuf883b422016-08-30 14:01:10 -07004354static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004355 int i, j, k;
4356 int s;
4357 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004358 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004359 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4360 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004361 for (j = 0; j < num_ext_tx_set[ext_tx_set_type_inter[s]] - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004362 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004363 }
4364 }
4365 }
4366
4367 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004368 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004369 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4370 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
4371 for (j = 0; j < INTRA_MODES; ++j)
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004372 for (k = 0; k < num_ext_tx_set[ext_tx_set_type_intra[s]] - 1; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004373 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
4374 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004375 }
4376 }
4377 }
4378}
Thomas9ac55082016-09-23 18:04:17 +01004379#endif // !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004380#else
4381
Yaowu Xuc27fc142016-08-22 16:08:15 -07004382#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004383#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07004384static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004385 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -07004386 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004387 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
Jingning Hanfeb517c2016-12-21 16:02:07 -08004388 for (j = TX_8X8; j < TX_SIZES; ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004389 av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004390 }
4391 }
4392 }
4393}
4394#endif // CONFIG_SUPERTX
4395
4396#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +00004397static void read_global_motion_params(WarpedMotionParams *params,
Sarah Parker13d06622017-03-10 17:03:28 -08004398 aom_prob *probs, aom_reader *r,
4399 int allow_hp) {
David Barkercf3d0b02016-11-10 10:14:49 +00004400 TransformationType type =
Michael Bebenita6048d052016-08-25 14:40:54 -07004401 aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
Sarah Parker13d06622017-03-10 17:03:28 -08004402 int trans_bits;
4403 int trans_dec_factor;
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004404 set_default_gmparams(params);
David Barkercf3d0b02016-11-10 10:14:49 +00004405 params->wmtype = type;
4406 switch (type) {
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004407 case HOMOGRAPHY:
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004408 case HORTRAPEZOID:
4409 case VERTRAPEZOID:
4410 if (type != HORTRAPEZOID)
4411 params->wmmat[6] =
4412 aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
4413 GM_ROW3HOMO_DECODE_FACTOR;
4414 if (type != VERTRAPEZOID)
4415 params->wmmat[7] =
4416 aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
4417 GM_ROW3HOMO_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004418 case AFFINE:
4419 case ROTZOOM:
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004420 params->wmmat[2] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004421 GM_ALPHA_DECODE_FACTOR +
David Barkercf3d0b02016-11-10 10:14:49 +00004422 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004423 if (type != VERTRAPEZOID)
4424 params->wmmat[3] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004425 GM_ALPHA_DECODE_FACTOR;
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004426 if (type >= AFFINE) {
4427 if (type != HORTRAPEZOID)
4428 params->wmmat[4] =
4429 aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4430 GM_ALPHA_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004431 params->wmmat[5] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4432 GM_ALPHA_DECODE_FACTOR +
4433 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004434 } else {
David Barkercf3d0b02016-11-10 10:14:49 +00004435 params->wmmat[4] = -params->wmmat[3];
4436 params->wmmat[5] = params->wmmat[2];
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004437 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004438 // fallthrough intended
David Barkercf3d0b02016-11-10 10:14:49 +00004439 case TRANSLATION:
Sarah Parker13d06622017-03-10 17:03:28 -08004440 trans_bits = (type == TRANSLATION) ? GM_ABS_TRANS_ONLY_BITS - !allow_hp
4441 : GM_ABS_TRANS_BITS;
4442 trans_dec_factor = (type == TRANSLATION)
4443 ? GM_TRANS_ONLY_DECODE_FACTOR * (1 << !allow_hp)
4444 : GM_TRANS_DECODE_FACTOR;
4445 params->wmmat[0] =
4446 aom_read_primitive_symmetric(r, trans_bits) * trans_dec_factor;
4447 params->wmmat[1] =
4448 aom_read_primitive_symmetric(r, trans_bits) * trans_dec_factor;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004449 break;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004450 case IDENTITY: break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004451 default: assert(0);
4452 }
Debargha Mukherjee3b6c5442017-03-30 08:22:00 -07004453 if (params->wmtype <= AFFINE)
4454 if (!get_shear_params(params)) assert(0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004455}
4456
Yaowu Xuf883b422016-08-30 14:01:10 -07004457static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004458 int frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004459 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
4460 read_global_motion_params(&cm->global_motion[frame],
Sarah Parker13d06622017-03-10 17:03:28 -08004461 cm->fc->global_motion_types_prob, r,
4462 cm->allow_high_precision_mv);
Sarah Parkere5299862016-08-16 14:57:37 -07004463 /*
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004464 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4465 frame, cm->current_video_frame, cm->show_frame,
David Barkercf3d0b02016-11-10 10:14:49 +00004466 cm->global_motion[frame].wmmat[0],
4467 cm->global_motion[frame].wmmat[1],
4468 cm->global_motion[frame].wmmat[2],
4469 cm->global_motion[frame].wmmat[3]);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004470 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004471 }
4472}
4473#endif // CONFIG_GLOBAL_MOTION
4474
Yaowu Xuf883b422016-08-30 14:01:10 -07004475static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004476 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004477 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004478#if CONFIG_SUPERTX
4479 MACROBLOCKD *const xd = &pbi->mb;
4480#endif
4481 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07004482 aom_reader r;
Yaowu Xu8af861b2016-11-01 12:12:11 -07004483 int k, i;
Thomas Davies493623e2017-03-31 16:12:25 +01004484#if !CONFIG_EC_ADAPT || \
4485 (CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION || CONFIG_EXT_INTER)
Yaowu Xu8af861b2016-11-01 12:12:11 -07004486 int j;
4487#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004488
Alex Converse2cdf0d82016-12-13 13:53:09 -08004489#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08004490 r.window_size = 1 << cm->ans_window_size_log2;
Alex Converse2cdf0d82016-12-13 13:53:09 -08004491#endif
Alex Converse346440b2017-01-03 13:47:37 -08004492 if (aom_reader_init(&r, data, partition_size, pbi->decrypt_cb,
4493 pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07004494 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004495 "Failed to allocate bool decoder 0");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004496
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004497#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004498 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4499 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4500 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4501 av1_alloc_restoration_buffers(cm);
4502 decode_restoration(cm, &r);
4503 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004504#endif
4505
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004506#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -07004507 if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004508#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004509
Angie Chiang800df032017-03-22 11:14:12 -07004510#if CONFIG_LV_MAP
4511 av1_read_txb_probs(fc, cm->tx_mode, &r);
4512#else // CONFIG_LV_MAP
Yushin Cho77bba8d2016-11-04 16:36:56 -07004513#if !CONFIG_PVQ
Alex Conversea9598cd2017-02-03 14:18:05 -08004514#if !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004515 read_coef_probs(fc, cm->tx_mode, &r);
Angie Chiang800df032017-03-22 11:14:12 -07004516#endif // !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Angie Chiang7d7ead92017-03-22 10:35:51 -07004517#endif // !CONFIG_PVQ
Angie Chiang800df032017-03-22 11:14:12 -07004518#endif // CONFIG_LV_MAP
4519
Yaowu Xuc27fc142016-08-22 16:08:15 -07004520#if CONFIG_VAR_TX
4521 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004522 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
Yushin Cho77bba8d2016-11-04 16:36:56 -07004523#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004524 for (k = 0; k < SKIP_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004525 av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004526
Thomas Daviesd6ee8a82017-03-02 14:42:50 +00004527#if CONFIG_DELTA_Q && !CONFIG_EC_ADAPT
Thomas Davies665cd702017-03-02 10:20:30 +00004528 for (k = 0; k < DELTA_Q_PROBS; ++k)
Thomas Daviesf6936102016-09-05 16:51:31 +01004529 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
4530#endif
4531
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004532#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004533 if (cm->seg.enabled && cm->seg.update_map) {
4534 if (cm->seg.temporal_update) {
4535 for (k = 0; k < PREDICTION_PROBS; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004536 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004537 }
4538 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004539 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004540 }
4541
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004542 for (j = 0; j < INTRA_MODES; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004543 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004544 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004545 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004546
4547#if CONFIG_EXT_PARTITION_TYPES
Alex Converse4e18d402017-03-14 15:36:38 -07004548 for (j = 0; j < PARTITION_PLOFFSET; ++j)
4549 for (i = 0; i < PARTITION_TYPES - 1; ++i)
4550 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
4551 for (; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004552 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004553 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004554#else
Alex Converse55c6bde2017-01-12 15:55:31 -08004555 for (j = 0; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004556 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004557 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004558#endif // CONFIG_EXT_PARTITION_TYPES
hui su9aa97492017-01-26 16:46:01 -08004559
Alex Converse55c6bde2017-01-12 15:55:31 -08004560#if CONFIG_UNPOISON_PARTITION_CTX
4561 for (; j < PARTITION_CONTEXTS_PRIMARY + PARTITION_BLOCK_SIZES; ++j)
4562 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_VERT], ACCT_STR);
4563 for (; j < PARTITION_CONTEXTS_PRIMARY + 2 * PARTITION_BLOCK_SIZES; ++j)
4564 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_HORZ], ACCT_STR);
4565#endif // CONFIG_UNPOISON_PARTITION_CTX
hui su9aa97492017-01-26 16:46:01 -08004566
4567#if CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004568 for (i = 0; i < INTRA_FILTERS + 1; ++i)
4569 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004570 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
hui su9aa97492017-01-26 16:46:01 -08004571#endif // CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
hui sub4e25d22017-03-09 15:32:30 -08004572#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004573
4574 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004575 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Nathan E. Egge10ba2be2016-11-16 09:44:26 -05004576#if CONFIG_EC_MULTISYMBOL
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00004577 av1_copy(cm->fc->kf_y_cdf, av1_kf_y_mode_cdf);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004578#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004579#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004580 for (k = 0; k < INTRA_MODES; k++)
Thomas Davies6519beb2016-10-19 14:46:07 +01004581 for (j = 0; j < INTRA_MODES; j++)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004582 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004583 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004584#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004585 } else {
4586#if !CONFIG_REF_MV
4587 nmv_context *const nmvc = &fc->nmvc;
4588#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004589 read_inter_mode_probs(fc, &r);
4590
4591#if CONFIG_EXT_INTER
4592 read_inter_compound_mode_probs(fc, &r);
4593 if (cm->reference_mode != COMPOUND_REFERENCE) {
4594 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4595 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004596 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004597 }
4598 }
4599 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4600 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004601 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004602 }
4603 for (i = 0; i < BLOCK_SIZES; i++) {
4604 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004605 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004606 }
4607 }
4608 }
4609 if (cm->reference_mode != SINGLE_REFERENCE) {
4610 for (i = 0; i < BLOCK_SIZES; i++) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08004611 for (j = 0; j < COMPOUND_TYPES - 1; j++) {
4612 av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004613 }
4614 }
4615 }
4616#endif // CONFIG_EXT_INTER
4617
Yue Chencb60b182016-10-13 15:18:22 -07004618#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004619 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
Yue Chencb60b182016-10-13 15:18:22 -07004620 for (j = 0; j < MOTION_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004621 av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004622 }
Yue Chencb60b182016-10-13 15:18:22 -07004623#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004624
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004625#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004626 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004627#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004628
4629 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004630 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004631
4632 if (cm->reference_mode != SINGLE_REFERENCE)
4633 setup_compound_reference_mode(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004634 read_frame_reference_mode_probs(cm, &r);
4635
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004636#if !CONFIG_EC_ADAPT
Nathan E. Egge5710c722016-09-08 10:01:16 -04004637 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004638 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004639 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
Nathan E. Egge5710c722016-09-08 10:01:16 -04004640 }
Thomas9ac55082016-09-23 18:04:17 +01004641#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004642
4643#if CONFIG_REF_MV
4644 for (i = 0; i < NMV_CONTEXTS; ++i)
4645 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
4646#else
4647 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
4648#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004649#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004650 read_ext_tx_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004651#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004652#if CONFIG_SUPERTX
4653 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
4654#endif
4655#if CONFIG_GLOBAL_MOTION
4656 read_global_motion(cm, &r);
Thomas Davies6519beb2016-10-19 14:46:07 +01004657#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004658 }
Thomas Davies028b57f2017-02-22 16:42:11 +00004659#if CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Thomas Davies87aeeb82017-02-17 00:19:40 +00004660#if CONFIG_NEW_TOKENSET
4661 av1_coef_head_cdfs(fc);
4662#endif
4663 /* Make tail distribution from head */
Thomas Davies6519beb2016-10-19 14:46:07 +01004664 av1_coef_pareto_cdfs(fc);
David Barker599dfd02016-11-10 13:20:12 +00004665#if CONFIG_REF_MV
4666 for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
4667#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004668 av1_set_mv_cdfs(&fc->nmvc);
David Barker599dfd02016-11-10 13:20:12 +00004669#endif
Thomas Davies6519beb2016-10-19 14:46:07 +01004670 av1_set_mode_cdfs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00004671#endif // CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004672
Yaowu Xuf883b422016-08-30 14:01:10 -07004673 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004674}
4675
4676#ifdef NDEBUG
4677#define debug_check_frame_counts(cm) (void)0
4678#else // !NDEBUG
4679// Counts should only be incremented when frame_parallel_decoding_mode and
4680// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07004681static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004682 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07004683 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004684 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
4685 cm->error_resilient_mode);
4686 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
4687 sizeof(cm->counts.y_mode)));
4688 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
4689 sizeof(cm->counts.uv_mode)));
4690 assert(!memcmp(cm->counts.partition, zero_counts.partition,
4691 sizeof(cm->counts.partition)));
4692 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
4693 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
4694 sizeof(cm->counts.eob_branch)));
Thomas Daviesab780672017-02-01 12:07:29 +00004695#if CONFIG_EC_MULTISYMBOL
4696 assert(!memcmp(cm->counts.blockz_count, zero_counts.blockz_count,
4697 sizeof(cm->counts.blockz_count)));
4698#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004699 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
4700 sizeof(cm->counts.switchable_interp)));
4701 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
4702 sizeof(cm->counts.inter_mode)));
4703#if CONFIG_EXT_INTER
4704 assert(!memcmp(cm->counts.inter_compound_mode,
4705 zero_counts.inter_compound_mode,
4706 sizeof(cm->counts.inter_compound_mode)));
4707 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
4708 sizeof(cm->counts.interintra)));
4709 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
4710 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08004711 assert(!memcmp(cm->counts.compound_interinter,
4712 zero_counts.compound_interinter,
4713 sizeof(cm->counts.compound_interinter)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004714#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07004715#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
4716 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
4717 sizeof(cm->counts.motion_mode)));
4718#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004719 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
4720 sizeof(cm->counts.intra_inter)));
4721 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
4722 sizeof(cm->counts.comp_inter)));
4723 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
4724 sizeof(cm->counts.single_ref)));
4725 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
4726 sizeof(cm->counts.comp_ref)));
4727#if CONFIG_EXT_REFS
4728 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
4729 sizeof(cm->counts.comp_bwdref)));
4730#endif // CONFIG_EXT_REFS
4731 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
4732 sizeof(cm->counts.tx_size)));
4733 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
4734#if CONFIG_REF_MV
4735 assert(
4736 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
4737 assert(
4738 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
4739#else
4740 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
4741#endif
4742 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
4743 sizeof(cm->counts.inter_ext_tx)));
4744 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
4745 sizeof(cm->counts.intra_ext_tx)));
4746}
4747#endif // NDEBUG
4748
Yaowu Xuf883b422016-08-30 14:01:10 -07004749static struct aom_read_bit_buffer *init_read_bit_buffer(
4750 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
4751 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004752 rb->bit_offset = 0;
4753 rb->error_handler = error_handler;
4754 rb->error_handler_data = &pbi->common;
4755 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004756 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004757 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
4758 rb->bit_buffer = clear_data;
4759 rb->bit_buffer_end = clear_data + n;
4760 } else {
4761 rb->bit_buffer = data;
4762 rb->bit_buffer_end = data_end;
4763 }
4764 return rb;
4765}
4766
4767//------------------------------------------------------------------------------
4768
Yaowu Xuf883b422016-08-30 14:01:10 -07004769int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
4770 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
4771 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
4772 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004773}
4774
Yaowu Xuf883b422016-08-30 14:01:10 -07004775void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
4776 int *height) {
4777 *width = aom_rb_read_literal(rb, 16) + 1;
4778 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004779}
4780
Yaowu Xuf883b422016-08-30 14:01:10 -07004781BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
4782 int profile = aom_rb_read_bit(rb);
4783 profile |= aom_rb_read_bit(rb) << 1;
4784 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004785 return (BITSTREAM_PROFILE)profile;
4786}
4787
Thomas Davies028b57f2017-02-22 16:42:11 +00004788#if CONFIG_EC_ADAPT
4789static void make_update_tile_list_dec(AV1Decoder *pbi, const int tile_rows,
4790 const int tile_cols,
4791 FRAME_CONTEXT *ec_ctxs[]) {
4792 int i;
4793 for (i = 0; i < tile_rows * tile_cols; ++i)
4794 ec_ctxs[i] = &pbi->tile_data[i].tctx;
4795}
4796#endif
4797
Yaowu Xuf883b422016-08-30 14:01:10 -07004798void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
4799 const uint8_t *data_end, const uint8_t **p_data_end) {
4800 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004801 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07004802 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004803 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004804 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004805 size_t first_partition_size;
4806 YV12_BUFFER_CONFIG *new_fb;
4807
4808#if CONFIG_BITSTREAM_DEBUG
4809 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
4810#endif
4811
4812 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004813 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Thomas Davies72712e62016-11-09 12:17:51 +00004814#if CONFIG_TILE_GROUPS
4815 pbi->first_partition_size = first_partition_size;
4816 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
4817#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004818 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004819 xd->cur_buf = new_fb;
4820#if CONFIG_GLOBAL_MOTION
4821 xd->global_motion = cm->global_motion;
4822#endif // CONFIG_GLOBAL_MOTION
4823
4824 if (!first_partition_size) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004825 // showing a frame directly
4826 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004827 return;
4828 }
4829
Yaowu Xuf883b422016-08-30 14:01:10 -07004830 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004831 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07004832 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004833 "Truncated packet or corrupt header length");
4834
Jingning Han24e0a182016-11-20 22:34:12 -08004835#if CONFIG_REF_MV
Dengca8d24d2016-10-17 14:06:35 +08004836 cm->setup_mi(cm);
4837#endif
4838
Fangwen Fu8d164de2016-12-14 13:40:54 -08004839#if CONFIG_TEMPMV_SIGNALING
4840 if (cm->use_prev_frame_mvs) {
4841 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4842 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4843 assert(!cm->error_resilient_mode &&
4844 cm->width == last_fb_ref_buf->buf->y_width &&
4845 cm->height == last_fb_ref_buf->buf->y_height &&
4846 !cm->prev_frame->intra_only);
4847 }
4848#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004849 cm->use_prev_frame_mvs =
4850 !cm->error_resilient_mode && cm->width == cm->last_width &&
4851 cm->height == cm->last_height && !cm->last_intra_only &&
4852 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004853#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004854#if CONFIG_EXT_REFS
4855 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
4856 // show_exisiting_frame=1, nor can it take a frame not used as
4857 // a reference, it is probable that by the time it is being
4858 // referred to, the frame buffer it originally points to may
4859 // already get expired and have been reassigned to the current
4860 // newly coded frame. Hence, we need to check whether this is
4861 // the case, and if yes, we have 2 choices:
4862 // (1) Simply disable the use of previous frame mvs; or
4863 // (2) Have cm->prev_frame point to one reference frame buffer,
4864 // e.g. LAST_FRAME.
4865 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
4866 // Reassign the LAST_FRAME buffer to cm->prev_frame.
4867 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4868 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4869 }
4870#endif // CONFIG_EXT_REFS
4871
Yaowu Xuf883b422016-08-30 14:01:10 -07004872 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004873
4874 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
4875 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07004876 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004877 "Uninitialized entropy context.");
4878
Yaowu Xuf883b422016-08-30 14:01:10 -07004879 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004880
4881 xd->corrupted = 0;
4882 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
4883 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07004884 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004885 "Decode failed. Frame data header is corrupted.");
4886
4887 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004888 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004889 }
4890
4891 // If encoded in frame parallel mode, frame context is ready after decoding
4892 // the frame header.
4893 if (cm->frame_parallel_decode &&
4894 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004895 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004896 FrameWorkerData *const frame_worker_data = worker->data1;
4897 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
4898 context_updated = 1;
4899 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4900 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004901 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004902 pbi->cur_buf->row = -1;
4903 pbi->cur_buf->col = -1;
4904 frame_worker_data->frame_context_ready = 1;
4905 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07004906 av1_frameworker_signal_stats(worker);
4907 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004908 }
4909
hui su0d103572017-03-01 17:58:01 -08004910#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07004911 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004912 cm->coef_probs_update_idx = 0;
hui su0d103572017-03-01 17:58:01 -08004913#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004914
4915 if (pbi->max_threads > 1
4916#if CONFIG_EXT_TILE
4917 && pbi->dec_tile_col < 0 // Decoding all columns
4918#endif // CONFIG_EXT_TILE
4919 && cm->tile_cols > 1) {
4920 // Multi-threaded tile decoder
4921 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
4922 if (!xd->corrupted) {
4923 if (!cm->skip_loop_filter) {
4924 // If multiple threads are used to decode tiles, then we use those
4925 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07004926 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
4927 0, 0, pbi->tile_workers, pbi->num_tile_workers,
4928 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004929 }
4930 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004931 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004932 "Decode failed. Frame data is corrupted.");
4933 }
4934 } else {
4935 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
4936 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004937
Jean-Marc Valin01435132017-02-18 14:12:53 -05004938#if CONFIG_CDEF
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04004939 if (!cm->skip_loop_filter) {
Jean-Marc Valine9f77422017-03-22 17:09:51 -04004940 av1_cdef_frame(&pbi->cur_buf->buf, cm, &pbi->mb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004941 }
Debargha Mukherjee00c54332017-03-03 15:44:17 -08004942#endif // CONFIG_CDEF
4943
4944#if CONFIG_LOOP_RESTORATION
4945 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4946 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4947 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4948 av1_loop_restoration_frame(new_fb, cm, cm->rst_info, 7, 0, NULL);
4949 }
4950#endif // CONFIG_LOOP_RESTORATION
Thomas Daedef56859f2016-04-19 16:57:24 -07004951
Yaowu Xuc27fc142016-08-22 16:08:15 -07004952 if (!xd->corrupted) {
4953 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Thomas Davies028b57f2017-02-22 16:42:11 +00004954#if CONFIG_EC_ADAPT
Thomas Davies493623e2017-03-31 16:12:25 +01004955 FRAME_CONTEXT **tile_ctxs = aom_malloc(cm->tile_rows * cm->tile_cols *
4956 sizeof(&pbi->tile_data[0].tctx));
4957 aom_cdf_prob **cdf_ptrs =
4958 aom_malloc(cm->tile_rows * cm->tile_cols *
4959 sizeof(&pbi->tile_data[0].tctx.partition_cdf[0][0]));
Thomas Davies028b57f2017-02-22 16:42:11 +00004960 make_update_tile_list_dec(pbi, cm->tile_rows, cm->tile_cols, tile_ctxs);
4961#endif
4962
hui su0d103572017-03-01 17:58:01 -08004963#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004964 cm->partial_prob_update = 0;
hui su0d103572017-03-01 17:58:01 -08004965#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07004966 av1_adapt_coef_probs(cm);
4967 av1_adapt_intra_frame_probs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00004968#if CONFIG_EC_ADAPT
Thomas Davies493623e2017-03-31 16:12:25 +01004969 av1_average_tile_coef_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00004970 cm->tile_rows * cm->tile_cols);
Thomas Davies493623e2017-03-31 16:12:25 +01004971 av1_average_tile_intra_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00004972 cm->tile_rows * cm->tile_cols);
Yushin Chob188ea12017-03-13 13:45:23 -07004973#if CONFIG_PVQ
4974 av1_average_tile_pvq_cdfs(pbi->common.fc, tile_ctxs,
4975 cm->tile_rows * cm->tile_cols);
4976#endif // CONFIG_PVQ
4977#endif // CONFIG_EC_ADAPT
hui suff0da2b2017-03-07 15:51:37 -08004978#if CONFIG_ADAPT_SCAN
4979 av1_adapt_scan_order(cm);
4980#endif // CONFIG_ADAPT_SCAN
Yaowu Xuc27fc142016-08-22 16:08:15 -07004981
4982 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004983 av1_adapt_inter_frame_probs(cm);
4984 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Thomas Davies028b57f2017-02-22 16:42:11 +00004985#if CONFIG_EC_ADAPT
4986 av1_average_tile_inter_cdfs(&pbi->common, pbi->common.fc, tile_ctxs,
Thomas Davies493623e2017-03-31 16:12:25 +01004987 cdf_ptrs, cm->tile_rows * cm->tile_cols);
4988 av1_average_tile_mv_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00004989 cm->tile_rows * cm->tile_cols);
4990#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004991 }
Thomas Davies493623e2017-03-31 16:12:25 +01004992#if CONFIG_EC_ADAPT
4993 aom_free(tile_ctxs);
4994 aom_free(cdf_ptrs);
4995#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004996 } else {
4997 debug_check_frame_counts(cm);
4998 }
4999 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005000 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005001 "Decode failed. Frame data is corrupted.");
5002 }
5003
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05005004#if CONFIG_INSPECTION
5005 if (pbi->inspect_cb != NULL) {
5006 (*pbi->inspect_cb)(pbi, pbi->inspect_ctx);
5007 }
5008#endif
5009
Yaowu Xuc27fc142016-08-22 16:08:15 -07005010 // Non frame parallel update frame context here.
5011 if (!cm->error_resilient_mode && !context_updated)
5012 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
5013}