blob: 6c53113576c9c425f8549ada109c3510a492f597 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <assert.h>
13#include <stdlib.h> // qsort()
14
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "./aom_config.h"
16#include "./aom_dsp_rtcd.h"
17#include "./aom_scale_rtcd.h"
Jingning Han1aab8182016-06-03 11:09:06 -070018#include "./av1_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070019
Steinar Midtskogen2fd70ee2016-09-02 10:02:30 +020020#include "aom/aom_codec.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070021#include "aom_dsp/aom_dsp_common.h"
Jingning Han1aab8182016-06-03 11:09:06 -070022#include "aom_dsp/bitreader.h"
23#include "aom_dsp/bitreader_buffer.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070024#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070025#include "aom_ports/mem.h"
26#include "aom_ports/mem_ops.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070027#include "aom_scale/aom_scale.h"
28#include "aom_util/aom_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070029
30#include "av1/common/alloccommon.h"
Jean-Marc Valin01435132017-02-18 14:12:53 -050031#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010032#include "av1/common/cdef.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070033#include "av1/common/clpf.h"
34#endif
Nathan E. Egge2cf03b12017-02-22 16:19:59 -050035#if CONFIG_INSPECTION
36#include "av1/decoder/inspection.h"
37#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070038#include "av1/common/common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070039#include "av1/common/entropy.h"
40#include "av1/common/entropymode.h"
Thomas Davies6519beb2016-10-19 14:46:07 +010041#include "av1/common/entropymv.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070042#include "av1/common/idct.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070043#include "av1/common/pred_common.h"
44#include "av1/common/quant_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070045#include "av1/common/reconinter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070046#include "av1/common/reconintra.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070047#include "av1/common/seg_common.h"
Jingning Han1aab8182016-06-03 11:09:06 -070048#include "av1/common/thread_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070049#include "av1/common/tile_common.h"
50
51#include "av1/decoder/decodeframe.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070052#include "av1/decoder/decodemv.h"
53#include "av1/decoder/decoder.h"
Angie Chiang133733c2017-03-17 12:50:20 -070054#if CONFIG_LV_MAP
55#include "av1/decoder/decodetxb.h"
56#endif
Jingning Han1aab8182016-06-03 11:09:06 -070057#include "av1/decoder/detokenize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070058#include "av1/decoder/dsubexp.h"
59
Yue Chen69f18e12016-09-08 14:48:15 -070060#if CONFIG_WARPED_MOTION
61#include "av1/common/warped_motion.h"
62#endif // CONFIG_WARPED_MOTION
63
Yaowu Xuf883b422016-08-30 14:01:10 -070064#define MAX_AV1_HEADER_SIZE 80
Michael Bebenita6048d052016-08-25 14:40:54 -070065#define ACCT_STR __func__
Yaowu Xuc27fc142016-08-22 16:08:15 -070066
Yushin Cho77bba8d2016-11-04 16:36:56 -070067#if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -070068#include "av1/common/partition.h"
hui suff0da2b2017-03-07 15:51:37 -080069#include "av1/common/pvq.h"
70#include "av1/common/scan.h"
Yushin Cho77bba8d2016-11-04 16:36:56 -070071#include "av1/decoder/decint.h"
hui suff0da2b2017-03-07 15:51:37 -080072#include "av1/decoder/pvq_decoder.h"
73#include "av1/encoder/encodemb.h"
Yushin Cho77bba8d2016-11-04 16:36:56 -070074#include "av1/encoder/hybrid_fwd_txfm.h"
75#endif
76
Thomas Davies80188d12016-10-26 16:08:35 -070077static struct aom_read_bit_buffer *init_read_bit_buffer(
78 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
79 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]);
80static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
81 size_t partition_size);
82static size_t read_uncompressed_header(AV1Decoder *pbi,
83 struct aom_read_bit_buffer *rb);
84
Yaowu Xuf883b422016-08-30 14:01:10 -070085static int is_compound_reference_allowed(const AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070086 int i;
87 if (frame_is_intra_only(cm)) return 0;
88 for (i = 1; i < INTER_REFS_PER_FRAME; ++i)
89 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
90
91 return 0;
92}
93
Yaowu Xuf883b422016-08-30 14:01:10 -070094static void setup_compound_reference_mode(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070095#if CONFIG_EXT_REFS
96 cm->comp_fwd_ref[0] = LAST_FRAME;
97 cm->comp_fwd_ref[1] = LAST2_FRAME;
98 cm->comp_fwd_ref[2] = LAST3_FRAME;
99 cm->comp_fwd_ref[3] = GOLDEN_FRAME;
100
101 cm->comp_bwd_ref[0] = BWDREF_FRAME;
102 cm->comp_bwd_ref[1] = ALTREF_FRAME;
103#else
104 if (cm->ref_frame_sign_bias[LAST_FRAME] ==
105 cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
106 cm->comp_fixed_ref = ALTREF_FRAME;
107 cm->comp_var_ref[0] = LAST_FRAME;
108 cm->comp_var_ref[1] = GOLDEN_FRAME;
109 } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
110 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
111 cm->comp_fixed_ref = GOLDEN_FRAME;
112 cm->comp_var_ref[0] = LAST_FRAME;
113 cm->comp_var_ref[1] = ALTREF_FRAME;
114 } else {
115 cm->comp_fixed_ref = LAST_FRAME;
116 cm->comp_var_ref[0] = GOLDEN_FRAME;
117 cm->comp_var_ref[1] = ALTREF_FRAME;
118 }
119#endif // CONFIG_EXT_REFS
120}
121
122static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
123 return len != 0 && len <= (size_t)(end - start);
124}
125
Yaowu Xuf883b422016-08-30 14:01:10 -0700126static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
127 const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700128 return data > max ? max : data;
129}
130
Yue Cheneeacc4c2017-01-17 17:29:17 -0800131static TX_MODE read_tx_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
132 struct aom_read_bit_buffer *rb) {
133 int i, all_lossless = 1;
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800134#if CONFIG_TX64X64
Yue Cheneeacc4c2017-01-17 17:29:17 -0800135 TX_MODE tx_mode;
136#endif
137
138 if (cm->seg.enabled) {
139 for (i = 0; i < MAX_SEGMENTS; ++i) {
140 if (!xd->lossless[i]) {
141 all_lossless = 0;
142 break;
143 }
144 }
145 } else {
146 all_lossless = xd->lossless[0];
147 }
148
149 if (all_lossless) return ONLY_4X4;
150#if CONFIG_TX64X64
151 tx_mode = aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800152 if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
153 return tx_mode;
154#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700155 return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800156#endif // CONFIG_TX64X64
Yaowu Xuc27fc142016-08-22 16:08:15 -0700157}
158
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -0500159#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -0700160static void read_tx_size_probs(FRAME_CONTEXT *fc, aom_reader *r) {
161 int i, j, k;
162 for (i = 0; i < MAX_TX_DEPTH; ++i)
163 for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
164 for (k = 0; k < i + 1; ++k)
165 av1_diff_update_prob(r, &fc->tx_size_probs[i][j][k], ACCT_STR);
166}
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -0500167#endif
Yaowu Xuefc75352016-10-31 09:46:42 -0700168
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400169#if !CONFIG_EC_ADAPT
Yaowu Xuf883b422016-08-30 14:01:10 -0700170static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700171 int i, j;
Nathan E. Egge4947c292016-04-26 11:37:06 -0400172 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700173 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700174 av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i], ACCT_STR);
Nathan E. Egge4947c292016-04-26 11:37:06 -0400175 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700176}
Thomas Davies6519beb2016-10-19 14:46:07 +0100177#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700178
Yaowu Xuf883b422016-08-30 14:01:10 -0700179static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700180#if CONFIG_REF_MV
Yaowu Xu8af861b2016-11-01 12:12:11 -0700181 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182 for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700183 av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700184 for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700185 av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700186 for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700187 av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700188 for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700189 av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700190#if CONFIG_EXT_INTER
Michael Bebenita6048d052016-08-25 14:40:54 -0700191 av1_diff_update_prob(r, &fc->new2mv_prob, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700192#endif // CONFIG_EXT_INTER
193#else
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400194#if !CONFIG_EC_ADAPT
Yaowu Xu8af861b2016-11-01 12:12:11 -0700195 int i, j;
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400196 for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700197 for (j = 0; j < INTER_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700198 av1_diff_update_prob(r, &fc->inter_mode_probs[i][j], ACCT_STR);
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400199 }
Yaowu Xu8af861b2016-11-01 12:12:11 -0700200#else
201 (void)fc;
202 (void)r;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700203#endif
Thomas Davies6519beb2016-10-19 14:46:07 +0100204#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700205}
206
207#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700208static void read_inter_compound_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700209 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -0700210 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700211 for (j = 0; j < INTER_MODE_CONTEXTS; ++j) {
212 for (i = 0; i < INTER_COMPOUND_MODES - 1; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700213 av1_diff_update_prob(r, &fc->inter_compound_mode_probs[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700214 }
215 }
216 }
217}
218#endif // CONFIG_EXT_INTER
Yaowu Xu17fd2f22016-11-17 18:23:28 -0800219#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100220#if !CONFIG_EXT_TX
221static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
222 int i, j, k;
223 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
224 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
225 for (j = 0; j < TX_TYPES; ++j) {
226 for (k = 0; k < TX_TYPES - 1; ++k)
227 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[i][j][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100228 }
229 }
230 }
231 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
232 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
233 for (k = 0; k < TX_TYPES - 1; ++k)
234 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[i][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100235 }
236 }
237}
238#endif
239#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700240
241static REFERENCE_MODE read_frame_reference_mode(
Yaowu Xuf883b422016-08-30 14:01:10 -0700242 const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700243 if (is_compound_reference_allowed(cm)) {
Zoe Liub05e5d12017-02-07 14:32:53 -0800244#if CONFIG_REF_ADAPT
245 return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
246#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700247 return aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700248 ? REFERENCE_MODE_SELECT
Yaowu Xuf883b422016-08-30 14:01:10 -0700249 : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
Zoe Liub05e5d12017-02-07 14:32:53 -0800250#endif // CONFIG_REF_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700251 } else {
252 return SINGLE_REFERENCE;
253 }
254}
255
Yaowu Xuf883b422016-08-30 14:01:10 -0700256static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700257 FRAME_CONTEXT *const fc = cm->fc;
258 int i, j;
259
260 if (cm->reference_mode == REFERENCE_MODE_SELECT)
261 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700262 av1_diff_update_prob(r, &fc->comp_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700263
264 if (cm->reference_mode != COMPOUND_REFERENCE) {
265 for (i = 0; i < REF_CONTEXTS; ++i) {
266 for (j = 0; j < (SINGLE_REFS - 1); ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700267 av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700268 }
269 }
270 }
271
272 if (cm->reference_mode != SINGLE_REFERENCE) {
273 for (i = 0; i < REF_CONTEXTS; ++i) {
274#if CONFIG_EXT_REFS
275 for (j = 0; j < (FWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700276 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700277 for (j = 0; j < (BWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700278 av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700279#else
280 for (j = 0; j < (COMP_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700281 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700282#endif // CONFIG_EXT_REFS
283 }
284 }
285}
286
Yaowu Xuf883b422016-08-30 14:01:10 -0700287static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700288 int i;
Michael Bebenita6048d052016-08-25 14:40:54 -0700289 for (i = 0; i < n; ++i) av1_diff_update_prob(r, &p[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700290}
291
Yaowu Xuf883b422016-08-30 14:01:10 -0700292static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Thomas9ac55082016-09-23 18:04:17 +0100293 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700294
Yaowu Xue86288d2016-10-31 15:56:38 -0700295#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100296 int j;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700297 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
298
Yaowu Xuc27fc142016-08-22 16:08:15 -0700299 for (i = 0; i < 2; ++i) {
300 nmv_component *const comp_ctx = &ctx->comps[i];
301 update_mv_probs(&comp_ctx->sign, 1, r);
302 update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
303 update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
304 update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
305 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700306 for (i = 0; i < 2; ++i) {
307 nmv_component *const comp_ctx = &ctx->comps[i];
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400308 for (j = 0; j < CLASS0_SIZE; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700309 update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400310 }
Nathan E. Eggeac499f32016-09-08 15:38:57 -0400311 update_mv_probs(comp_ctx->fp, MV_FP_SIZE - 1, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700312 }
Alex Converseaca9feb2016-10-10 11:08:10 -0700313#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700314
315 if (allow_hp) {
316 for (i = 0; i < 2; ++i) {
317 nmv_component *const comp_ctx = &ctx->comps[i];
318 update_mv_probs(&comp_ctx->class0_hp, 1, r);
319 update_mv_probs(&comp_ctx->hp, 1, r);
320 }
321 }
322}
323
324static void inverse_transform_block(MACROBLOCKD *xd, int plane,
325 const TX_TYPE tx_type,
326 const TX_SIZE tx_size, uint8_t *dst,
Jingning Han1be18782016-10-21 11:48:15 -0700327 int stride, int16_t scan_line, int eob) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700328 struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han1be18782016-10-21 11:48:15 -0700329 tran_low_t *const dqcoeff = pd->dqcoeff;
330 INV_TXFM_PARAM inv_txfm_param;
331 inv_txfm_param.tx_type = tx_type;
332 inv_txfm_param.tx_size = tx_size;
333 inv_txfm_param.eob = eob;
334 inv_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700335
Yaowu Xuf883b422016-08-30 14:01:10 -0700336#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han1be18782016-10-21 11:48:15 -0700337 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
338 inv_txfm_param.bd = xd->bd;
hui subb9c73b2017-03-17 15:51:02 -0700339 av1_highbd_inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
Jingning Han1be18782016-10-21 11:48:15 -0700340 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700341#endif // CONFIG_AOM_HIGHBITDEPTH
hui subb9c73b2017-03-17 15:51:02 -0700342 av1_inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
Yaowu Xuf883b422016-08-30 14:01:10 -0700343#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700344 }
Jingning Han1be18782016-10-21 11:48:15 -0700345#endif // CONFIG_AOM_HIGHBITDEPTH
346 memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700347}
348
Yushin Cho77bba8d2016-11-04 16:36:56 -0700349#if CONFIG_PVQ
Thomas Daede6ff6af62017-02-03 16:29:24 -0800350static int av1_pvq_decode_helper(MACROBLOCKD *xd, tran_low_t *ref_coeff,
Thomas Daede1dbda1b2017-02-06 16:06:29 -0800351 tran_low_t *dqcoeff, int16_t *quant, int pli,
Yushin Cho77bba8d2016-11-04 16:36:56 -0700352 int bs, TX_TYPE tx_type, int xdec,
ltrudeaue1c09292017-01-20 15:42:13 -0500353 PVQ_SKIP_TYPE ac_dc_coded) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700354 unsigned int flags; // used for daala's stream analyzer.
355 int off;
356 const int is_keyframe = 0;
357 const int has_dc_skip = 1;
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800358 int coeff_shift = 3 - get_tx_scale(bs);
Thomas Daede6ff6af62017-02-03 16:29:24 -0800359 int hbd_downshift = 0;
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800360 int rounding_mask;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700361 // DC quantizer for PVQ
362 int pvq_dc_quant;
363 int lossless = (quant[0] == 0);
364 const int blk_size = tx_size_wide[bs];
365 int eob = 0;
366 int i;
Thomas Daede6ff6af62017-02-03 16:29:24 -0800367 od_dec_ctx *dec = &xd->daala_dec;
Yushin Cho70669122016-12-08 09:53:14 -1000368 int use_activity_masking = dec->use_activity_masking;
Thomas Daede1dbda1b2017-02-06 16:06:29 -0800369 DECLARE_ALIGNED(16, tran_low_t, dqcoeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
370 DECLARE_ALIGNED(16, tran_low_t, ref_coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700371
Yushin Cho48f84db2016-11-07 21:20:17 -0800372 od_coeff ref_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
373 od_coeff out_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700374
Thomas Daede6ff6af62017-02-03 16:29:24 -0800375#if CONFIG_AOM_HIGHBITDEPTH
376 hbd_downshift = xd->bd - 8;
377#endif // CONFIG_AOM_HIGHBITDEPTH
378
Yushin Cho77bba8d2016-11-04 16:36:56 -0700379 od_raster_to_coding_order(ref_coeff_pvq, blk_size, tx_type, ref_coeff,
380 blk_size);
381
Thomas Daede6ff6af62017-02-03 16:29:24 -0800382 assert(OD_COEFF_SHIFT >= 4);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700383 if (lossless)
384 pvq_dc_quant = 1;
385 else {
Yushin Cho70669122016-12-08 09:53:14 -1000386 if (use_activity_masking)
387 pvq_dc_quant = OD_MAXI(
Thomas Daede6ff6af62017-02-03 16:29:24 -0800388 1, (quant[0] << (OD_COEFF_SHIFT - 3) >> hbd_downshift) *
Yushin Cho70669122016-12-08 09:53:14 -1000389 dec->state.pvq_qm_q4[pli][od_qm_get_index(bs, 0)] >>
390 4);
391 else
Thomas Daede6ff6af62017-02-03 16:29:24 -0800392 pvq_dc_quant =
393 OD_MAXI(1, quant[0] << (OD_COEFF_SHIFT - 3) >> hbd_downshift);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700394 }
395
396 off = od_qm_offset(bs, xdec);
397
398 // copy int16 inputs to int32
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800399 for (i = 0; i < blk_size * blk_size; i++) {
Timothy B. Terriberry4e6a8f32017-02-24 11:00:59 -0800400 ref_int32[i] =
Thomas Daede6ff6af62017-02-03 16:29:24 -0800401 AOM_SIGNED_SHL(ref_coeff_pvq[i], OD_COEFF_SHIFT - coeff_shift) >>
402 hbd_downshift;
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800403 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700404
Thomas Daede6ff6af62017-02-03 16:29:24 -0800405 od_pvq_decode(dec, ref_int32, out_int32,
406 OD_MAXI(1, quant[1] << (OD_COEFF_SHIFT - 3) >> hbd_downshift),
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800407 pli, bs, OD_PVQ_BETA[use_activity_masking][pli][bs],
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800408 OD_ROBUST_STREAM, is_keyframe, &flags, ac_dc_coded,
409 dec->state.qm + off, dec->state.qm_inv + off);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700410
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800411 if (!has_dc_skip || out_int32[0]) {
412 out_int32[0] =
Nathan E. Egge89f58762016-12-28 16:31:50 -0500413 has_dc_skip + generic_decode(dec->r, &dec->state.adapt.model_dc[pli],
414 -1, &dec->state.adapt.ex_dc[pli][bs][0], 2,
415 "dc:mag");
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800416 if (out_int32[0]) out_int32[0] *= aom_read_bit(dec->r, "dc:sign") ? -1 : 1;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700417 }
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800418 out_int32[0] = out_int32[0] * pvq_dc_quant + ref_int32[0];
419
420 // copy int32 result back to int16
421 assert(OD_COEFF_SHIFT > coeff_shift);
422 rounding_mask = (1 << (OD_COEFF_SHIFT - coeff_shift - 1)) - 1;
423 for (i = 0; i < blk_size * blk_size; i++) {
Thomas Daede6ff6af62017-02-03 16:29:24 -0800424 out_int32[i] = AOM_SIGNED_SHL(out_int32[i], hbd_downshift);
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800425 dqcoeff_pvq[i] = (out_int32[i] + (out_int32[i] < 0) + rounding_mask) >>
426 (OD_COEFF_SHIFT - coeff_shift);
427 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700428
429 od_coding_order_to_raster(dqcoeff, blk_size, tx_type, dqcoeff_pvq, blk_size);
430
431 eob = blk_size * blk_size;
432
433 return eob;
434}
435
ltrudeaue1c09292017-01-20 15:42:13 -0500436static PVQ_SKIP_TYPE read_pvq_skip(AV1_COMMON *cm, MACROBLOCKD *const xd,
437 int plane, TX_SIZE tx_size) {
438 // decode ac/dc coded flag. bit0: DC coded, bit1 : AC coded
439 // NOTE : we don't use 5 symbols for luma here in aom codebase,
440 // since block partition is taken care of by aom.
441 // So, only AC/DC skip info is coded
442 const int ac_dc_coded = aom_decode_cdf_adapt(
443 xd->daala_dec.r,
444 xd->daala_dec.state.adapt.skip_cdf[2 * tx_size + (plane != 0)], 4,
445 xd->daala_dec.state.adapt.skip_increment, "skip");
446 if (ac_dc_coded < 0 || ac_dc_coded > 3) {
447 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
448 "Invalid PVQ Skip Type");
449 }
450 return ac_dc_coded;
451}
452
453static int av1_pvq_decode_helper2(AV1_COMMON *cm, MACROBLOCKD *const xd,
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800454 MB_MODE_INFO *const mbmi, int plane, int row,
455 int col, TX_SIZE tx_size, TX_TYPE tx_type) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700456 struct macroblockd_plane *const pd = &xd->plane[plane];
457 // transform block size in pixels
458 int tx_blk_size = tx_size_wide[tx_size];
459 int i, j;
460 tran_low_t *pvq_ref_coeff = pd->pvq_ref_coeff;
461 const int diff_stride = tx_blk_size;
462 int16_t *pred = pd->pred;
463 tran_low_t *const dqcoeff = pd->dqcoeff;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700464 uint8_t *dst;
465 int eob;
ltrudeaue1c09292017-01-20 15:42:13 -0500466 const PVQ_SKIP_TYPE ac_dc_coded = read_pvq_skip(cm, xd, plane, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700467
468 eob = 0;
469 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
470
Yushin Cho77bba8d2016-11-04 16:36:56 -0700471 if (ac_dc_coded) {
472 int xdec = pd->subsampling_x;
473 int seg_id = mbmi->segment_id;
474 int16_t *quant;
475 FWD_TXFM_PARAM fwd_txfm_param;
Yaowu Xufc1b2132016-11-07 15:16:15 -0800476 // ToDo(yaowu): correct this with optimal number from decoding process.
477 const int max_scan_line = tx_size_2d[tx_size];
Thomas Daede6ff6af62017-02-03 16:29:24 -0800478#if CONFIG_AOM_HIGHBITDEPTH
479 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
480 for (j = 0; j < tx_blk_size; j++)
481 for (i = 0; i < tx_blk_size; i++)
482 pred[diff_stride * j + i] =
483 CONVERT_TO_SHORTPTR(dst)[pd->dst.stride * j + i];
484 } else {
485#endif
486 for (j = 0; j < tx_blk_size; j++)
487 for (i = 0; i < tx_blk_size; i++)
488 pred[diff_stride * j + i] = dst[pd->dst.stride * j + i];
489#if CONFIG_AOM_HIGHBITDEPTH
490 }
491#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -0700492
493 fwd_txfm_param.tx_type = tx_type;
494 fwd_txfm_param.tx_size = tx_size;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700495 fwd_txfm_param.lossless = xd->lossless[seg_id];
496
Thomas Daede6ff6af62017-02-03 16:29:24 -0800497#if CONFIG_AOM_HIGHBITDEPTH
498 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
499 fwd_txfm_param.bd = xd->bd;
500 highbd_fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
501 } else {
502#endif // CONFIG_AOM_HIGHBITDEPTH
503 fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
504#if CONFIG_AOM_HIGHBITDEPTH
505 }
506#endif // CONFIG_AOM_HIGHBITDEPTH
Yushin Cho77bba8d2016-11-04 16:36:56 -0700507
508 quant = &pd->seg_dequant[seg_id][0]; // aom's quantizer
509
Thomas Daede6ff6af62017-02-03 16:29:24 -0800510 eob = av1_pvq_decode_helper(xd, pvq_ref_coeff, dqcoeff, quant, plane,
511 tx_size, tx_type, xdec, ac_dc_coded);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700512
Thomas Daede6ff6af62017-02-03 16:29:24 -0800513// Since av1 does not have separate inverse transform
514// but also contains adding to predicted image,
515// pass blank dummy image to av1_inv_txfm_add_*x*(), i.e. set dst as zeros
516#if CONFIG_AOM_HIGHBITDEPTH
517 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
518 for (j = 0; j < tx_blk_size; j++)
519 for (i = 0; i < tx_blk_size; i++)
520 CONVERT_TO_SHORTPTR(dst)[j * pd->dst.stride + i] = 0;
521 } else {
522#endif
523 for (j = 0; j < tx_blk_size; j++)
524 for (i = 0; i < tx_blk_size; i++) dst[j * pd->dst.stride + i] = 0;
525#if CONFIG_AOM_HIGHBITDEPTH
526 }
527#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -0700528
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800529 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
530 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700531 }
532
533 return eob;
534}
535#endif
536
Alex Converse8aca36d2017-01-31 12:33:15 -0800537static void predict_and_reconstruct_intra_block(
538 AV1_COMMON *cm, MACROBLOCKD *const xd, aom_reader *const r,
539 MB_MODE_INFO *const mbmi, int plane, int row, int col, TX_SIZE tx_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700540 struct macroblockd_plane *const pd = &xd->plane[plane];
541 PREDICTION_MODE mode = (plane == 0) ? mbmi->mode : mbmi->uv_mode;
Luc Trudeau005feb62017-02-22 13:34:01 -0500542 PLANE_TYPE plane_type = get_plane_type(plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700543 uint8_t *dst;
Urvang Joshifeb925f2016-12-05 10:37:29 -0800544 const int block_idx = (row << 1) + col;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700545#if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -0700546 (void)r;
547#endif
Jingning Hanca14dda2016-12-09 09:36:00 -0800548 dst = &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700549
Jingning Han41bb3392016-12-14 10:46:48 -0800550#if !CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -0700551 if (mbmi->sb_type < BLOCK_8X8)
Urvang Joshifeb925f2016-12-05 10:37:29 -0800552 if (plane == 0) mode = xd->mi[0]->bmi[block_idx].as_mode;
Jingning Han41bb3392016-12-14 10:46:48 -0800553#endif
David Barker839467f2017-01-19 11:06:15 +0000554 av1_predict_intra_block(xd, pd->width, pd->height, txsize_to_bsize[tx_size],
555 mode, dst, pd->dst.stride, dst, pd->dst.stride, col,
556 row, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700557
558 if (!mbmi->skip) {
559 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700560#if !CONFIG_PVQ
Angie Chiang133733c2017-03-17 12:50:20 -0700561#if CONFIG_LV_MAP
562 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700563 int eob;
564 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane,
565 pd->dqcoeff, &max_scan_line, &eob);
Angie Chiang133733c2017-03-17 12:50:20 -0700566#else // CONFIG_LV_MAP
Angie Chiangff6d8902016-10-21 11:02:09 -0700567 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 0);
Jingning Han1be18782016-10-21 11:48:15 -0700568 int16_t max_scan_line = 0;
569 const int eob =
Angie Chiang5c0568a2017-03-21 16:00:39 -0700570 av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
Jingning Han1be18782016-10-21 11:48:15 -0700571 tx_type, &max_scan_line, r, mbmi->segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700572#endif // CONFIG_LV_MAP
Jingning Han1be18782016-10-21 11:48:15 -0700573 if (eob)
574 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
575 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700576#else
ltrudeaue1c09292017-01-20 15:42:13 -0500577 av1_pvq_decode_helper2(cm, xd, mbmi, plane, row, col, tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700578#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700579 }
580}
581
Jingning Handddb21f2017-02-28 14:44:05 -0800582#if CONFIG_VAR_TX && !CONFIG_COEF_INTERLEAVE
Angie Chiangff6d8902016-10-21 11:02:09 -0700583static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
584 aom_reader *r, MB_MODE_INFO *const mbmi,
Jingning Han8fd62b72016-10-21 12:55:54 -0700585 int plane, BLOCK_SIZE plane_bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700586 int blk_row, int blk_col, TX_SIZE tx_size,
587 int *eob_total) {
588 const struct macroblockd_plane *const pd = &xd->plane[plane];
589 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
590 const int tx_row = blk_row >> (1 - pd->subsampling_y);
591 const int tx_col = blk_col >> (1 - pd->subsampling_x);
592 const TX_SIZE plane_tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700593 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700594 : mbmi->inter_tx_size[tx_row][tx_col];
Jingning Han5f614262016-10-27 14:27:43 -0700595 // Scale to match transform block unit.
Jingning Hanf64062f2016-11-02 16:22:18 -0700596 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
597 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700598
599 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
600
601 if (tx_size == plane_tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500602 PLANE_TYPE plane_type = get_plane_type(plane);
Jingning Han8fd62b72016-10-21 12:55:54 -0700603 int block_idx = (blk_row << 1) + blk_col;
604 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiang133733c2017-03-17 12:50:20 -0700605#if CONFIG_LV_MAP
606 (void)segment_id;
607 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700608 int eob;
609 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane,
610 pd->dqcoeff, &max_scan_line, &eob);
Angie Chiang133733c2017-03-17 12:50:20 -0700611#else // CONFIG_LV_MAP
Angie Chiangff6d8902016-10-21 11:02:09 -0700612 const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700613 int16_t max_scan_line = 0;
Angie Chiang5c0568a2017-03-21 16:00:39 -0700614 const int eob = av1_decode_block_tokens(
615 cm, xd, plane, sc, blk_col, blk_row, plane_tx_size, tx_type,
616 &max_scan_line, r, mbmi->segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700617#endif // CONFIG_LV_MAP
Jingning Han9ca05b72017-01-03 14:41:36 -0800618 inverse_transform_block(xd, plane, tx_type, plane_tx_size,
619 &pd->dst.buf[(blk_row * pd->dst.stride + blk_col)
620 << tx_size_wide_log2[0]],
621 pd->dst.stride, max_scan_line, eob);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700622 *eob_total += eob;
623 } else {
Jingning Hanf64062f2016-11-02 16:22:18 -0700624 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
625 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700626 int i;
627
628 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700629
630 for (i = 0; i < 4; ++i) {
Jingning Han5f614262016-10-27 14:27:43 -0700631 const int offsetr = blk_row + (i >> 1) * bsl;
632 const int offsetc = blk_col + (i & 0x01) * bsl;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700633
634 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
635
Jingning Han8fd62b72016-10-21 12:55:54 -0700636 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
Jingning Hanf64062f2016-11-02 16:22:18 -0700637 offsetc, sub_txs, eob_total);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700638 }
639 }
640}
641#endif // CONFIG_VAR_TX
642
Jingning Handddb21f2017-02-28 14:44:05 -0800643#if !CONFIG_VAR_TX || CONFIG_SUPERTX || CONFIG_COEF_INTERLEAVE || \
Jingning Hanfe45b212016-11-22 10:30:23 -0800644 (!CONFIG_VAR_TX && CONFIG_EXT_TX && CONFIG_RECT_TX)
Angie Chiangff6d8902016-10-21 11:02:09 -0700645static int reconstruct_inter_block(AV1_COMMON *cm, MACROBLOCKD *const xd,
Alex Converse8aca36d2017-01-31 12:33:15 -0800646 aom_reader *const r, int segment_id,
647 int plane, int row, int col,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700648 TX_SIZE tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500649 PLANE_TYPE plane_type = get_plane_type(plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700650 int block_idx = (row << 1) + col;
651 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700652#if CONFIG_PVQ
653 int eob;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700654 (void)r;
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800655 (void)segment_id;
656#else
657 struct macroblockd_plane *const pd = &xd->plane[plane];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700658#endif
659
660#if !CONFIG_PVQ
Angie Chiang133733c2017-03-17 12:50:20 -0700661#if CONFIG_LV_MAP
662 (void)segment_id;
Jingning Han1be18782016-10-21 11:48:15 -0700663 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700664 int eob;
665 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane, pd->dqcoeff,
666 &max_scan_line, &eob);
Angie Chiang133733c2017-03-17 12:50:20 -0700667#else // CONFIG_LV_MAP
668 int16_t max_scan_line = 0;
669 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700670 const int eob =
Angie Chiang5c0568a2017-03-21 16:00:39 -0700671 av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
672 tx_type, &max_scan_line, r, segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700673#endif // CONFIG_LV_MAP
Jingning Hanca14dda2016-12-09 09:36:00 -0800674 uint8_t *dst =
675 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Jingning Han1be18782016-10-21 11:48:15 -0700676 if (eob)
Jingning Hanca14dda2016-12-09 09:36:00 -0800677 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
678 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700679#else
ltrudeaue1c09292017-01-20 15:42:13 -0500680 eob = av1_pvq_decode_helper2(cm, xd, &xd->mi[0]->mbmi, plane, row, col,
681 tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700682#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700683 return eob;
684}
685#endif // !CONFIG_VAR_TX || CONFIG_SUPER_TX
686
Angie Chiang44701f22017-02-27 10:36:44 -0800687static void set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
688 BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
689 int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700690 const int offset = mi_row * cm->mi_stride + mi_col;
691 int x, y;
692 const TileInfo *const tile = &xd->tile;
693
694 xd->mi = cm->mi_grid_visible + offset;
695 xd->mi[0] = &cm->mi[offset];
696 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
697 // passing bsize from decode_partition().
698 xd->mi[0]->mbmi.sb_type = bsize;
Angie Chiang394c3372016-11-03 11:13:15 -0700699#if CONFIG_RD_DEBUG
700 xd->mi[0]->mbmi.mi_row = mi_row;
701 xd->mi[0]->mbmi.mi_col = mi_col;
702#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700703 for (y = 0; y < y_mis; ++y)
Jingning Han97d85482016-07-15 11:06:05 -0700704 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700705
Jingning Hanfaad0e12016-12-07 10:54:57 -0800706 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700707 set_skip_context(xd, mi_row, mi_col);
708
709#if CONFIG_VAR_TX
710 xd->max_tx_size = max_txsize_lookup[bsize];
711#endif
712
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800713// Distance of Mb to the various image edges. These are specified to 8th pel
714// as they are always compared to values that are in 1/8th pel units
715#if CONFIG_DEPENDENT_HORZTILES
716 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
717 cm->dependent_horz_tiles);
718#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700719 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 -0800720#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700721
Yaowu Xuf883b422016-08-30 14:01:10 -0700722 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700723}
724
725#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700726static MB_MODE_INFO *set_offsets_extend(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700727 MACROBLOCKD *const xd,
728 const TileInfo *const tile,
729 BLOCK_SIZE bsize_pred, int mi_row_pred,
730 int mi_col_pred, int mi_row_ori,
731 int mi_col_ori) {
732 // Used in supertx
733 // (mi_row_ori, mi_col_ori): location for mv
734 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
Jingning Han93531242016-12-20 11:54:36 -0800735 const int bw = mi_size_wide[bsize_pred];
736 const int bh = mi_size_high[bsize_pred];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700737 const int offset = mi_row_ori * cm->mi_stride + mi_col_ori;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700738 xd->mi = cm->mi_grid_visible + offset;
739 xd->mi[0] = cm->mi + offset;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800740#if CONFIG_DEPENDENT_HORZTILES
741 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
742 cm->mi_cols, cm->dependent_horz_tiles);
743#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700744 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
745 cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800746#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700747
748 xd->up_available = (mi_row_ori > tile->mi_row_start);
749 xd->left_available = (mi_col_ori > tile->mi_col_start);
750
Jingning Hanfaad0e12016-12-07 10:54:57 -0800751 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700752
753 return &xd->mi[0]->mbmi;
754}
755
Angie Chiang7fcfee42017-02-24 15:51:03 -0800756#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700757static MB_MODE_INFO *set_mb_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
758 BLOCK_SIZE bsize, int mi_row, int mi_col,
759 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700760 const int offset = mi_row * cm->mi_stride + mi_col;
761 const TileInfo *const tile = &xd->tile;
762 int x, y;
763
764 xd->mi = cm->mi_grid_visible + offset;
765 xd->mi[0] = cm->mi + offset;
766 xd->mi[0]->mbmi.sb_type = bsize;
767 for (y = 0; y < y_mis; ++y)
768 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
769
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800770#if CONFIG_DEPENDENT_HORZTILES
771 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
772 cm->dependent_horz_tiles);
773#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700774 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 -0800775#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700776 return &xd->mi[0]->mbmi;
777}
Angie Chiang7fcfee42017-02-24 15:51:03 -0800778#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700779
Yaowu Xuf883b422016-08-30 14:01:10 -0700780static void set_offsets_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700781 const TileInfo *const tile, BLOCK_SIZE bsize,
782 int mi_row, int mi_col) {
Jingning Han93531242016-12-20 11:54:36 -0800783 const int bw = mi_size_wide[bsize];
784 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700785 const int offset = mi_row * cm->mi_stride + mi_col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700786
787 xd->mi = cm->mi_grid_visible + offset;
788 xd->mi[0] = cm->mi + offset;
789
Jingning Hanfaad0e12016-12-07 10:54:57 -0800790 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800792#if CONFIG_DEPENDENT_HORZTILES
793 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
794 cm->dependent_horz_tiles);
795#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700796 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 -0800797#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700798
Yaowu Xuf883b422016-08-30 14:01:10 -0700799 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800}
801
Yaowu Xuf883b422016-08-30 14:01:10 -0700802static void set_param_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700803 BLOCK_SIZE bsize, int mi_row, int mi_col,
804 int txfm, int skip) {
Jingning Han93531242016-12-20 11:54:36 -0800805 const int bw = mi_size_wide[bsize];
806 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -0700807 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
808 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700809 const int offset = mi_row * cm->mi_stride + mi_col;
810 int x, y;
811
812 xd->mi = cm->mi_grid_visible + offset;
813 xd->mi[0] = cm->mi + offset;
814
815 for (y = 0; y < y_mis; ++y)
816 for (x = 0; x < x_mis; ++x) {
817 xd->mi[y * cm->mi_stride + x]->mbmi.skip = skip;
818 xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
819 }
820#if CONFIG_VAR_TX
821 xd->above_txfm_context = cm->above_txfm_context + mi_col;
822 xd->left_txfm_context =
823 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
Yaowu Xu52a17632016-11-17 15:48:21 -0800824 set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bw, bh, skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700825#endif
826}
827
Yaowu Xuf883b422016-08-30 14:01:10 -0700828static void set_ref(AV1_COMMON *const cm, MACROBLOCKD *const xd, int idx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700829 int mi_row, int mi_col) {
830 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
831 RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
832 xd->block_refs[idx] = ref_buffer;
Yaowu Xuf883b422016-08-30 14:01:10 -0700833 if (!av1_is_valid_scale(&ref_buffer->sf))
834 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700835 "Invalid scale factors");
Yaowu Xuf883b422016-08-30 14:01:10 -0700836 av1_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
837 &ref_buffer->sf);
Angie Chiangd0916d92017-03-10 17:54:18 -0800838 aom_merge_corrupted_flag(&xd->corrupted, ref_buffer->buf->corrupted);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700839}
840
841static void dec_predict_b_extend(
Yaowu Xuf883b422016-08-30 14:01:10 -0700842 AV1Decoder *const pbi, MACROBLOCKD *const xd, const TileInfo *const tile,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700843 int block, int mi_row_ori, int mi_col_ori, int mi_row_pred, int mi_col_pred,
844 int mi_row_top, int mi_col_top, uint8_t *dst_buf[3], int dst_stride[3],
845 BLOCK_SIZE bsize_top, BLOCK_SIZE bsize_pred, int b_sub8x8, int bextend) {
846 // Used in supertx
847 // (mi_row_ori, mi_col_ori): location for mv
848 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
849 // (mi_row_top, mi_col_top, bsize_top): region of the top partition size
850 // block: sub location of sub8x8 blocks
851 // b_sub8x8: 1: ori is sub8x8; 0: ori is not sub8x8
852 // bextend: 1: region to predict is an extension of ori; 0: not
853 int r = (mi_row_pred - mi_row_top) * MI_SIZE;
854 int c = (mi_col_pred - mi_col_top) * MI_SIZE;
Jingning Han93531242016-12-20 11:54:36 -0800855 const int mi_width_top = mi_size_wide[bsize_top];
856 const int mi_height_top = mi_size_high[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700857 MB_MODE_INFO *mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700858 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700859
860 if (mi_row_pred < mi_row_top || mi_col_pred < mi_col_top ||
861 mi_row_pred >= mi_row_top + mi_height_top ||
862 mi_col_pred >= mi_col_top + mi_width_top || mi_row_pred >= cm->mi_rows ||
863 mi_col_pred >= cm->mi_cols)
864 return;
865
866 mbmi = set_offsets_extend(cm, xd, tile, bsize_pred, mi_row_pred, mi_col_pred,
867 mi_row_ori, mi_col_ori);
868 set_ref(cm, xd, 0, mi_row_pred, mi_col_pred);
869 if (has_second_ref(&xd->mi[0]->mbmi))
870 set_ref(cm, xd, 1, mi_row_pred, mi_col_pred);
871
Jingning Han2511c662016-12-22 11:57:34 -0800872 if (!bextend) mbmi->tx_size = max_txsize_lookup[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700873
874 xd->plane[0].dst.stride = dst_stride[0];
875 xd->plane[1].dst.stride = dst_stride[1];
876 xd->plane[2].dst.stride = dst_stride[2];
877 xd->plane[0].dst.buf = dst_buf[0] +
878 (r >> xd->plane[0].subsampling_y) * dst_stride[0] +
879 (c >> xd->plane[0].subsampling_x);
880 xd->plane[1].dst.buf = dst_buf[1] +
881 (r >> xd->plane[1].subsampling_y) * dst_stride[1] +
882 (c >> xd->plane[1].subsampling_x);
883 xd->plane[2].dst.buf = dst_buf[2] +
884 (r >> xd->plane[2].subsampling_y) * dst_stride[2] +
885 (c >> xd->plane[2].subsampling_x);
886
887 if (!b_sub8x8)
Yaowu Xuf883b422016-08-30 14:01:10 -0700888 av1_build_inter_predictors_sb_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700889#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700890 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700891#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700892 mi_row_pred, mi_col_pred, bsize_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700893 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700894 av1_build_inter_predictors_sb_sub8x8_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700895#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700896 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700897#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700898 mi_row_pred, mi_col_pred,
899 bsize_pred, block);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700900}
901
Yaowu Xuf883b422016-08-30 14:01:10 -0700902static void dec_extend_dir(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700903 const TileInfo *const tile, int block,
904 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
905 int mi_col, int mi_row_top, int mi_col_top,
906 uint8_t *dst_buf[3], int dst_stride[3], int dir) {
907 // dir: 0-lower, 1-upper, 2-left, 3-right
908 // 4-lowerleft, 5-upperleft, 6-lowerright, 7-upperright
Jingning Han93531242016-12-20 11:54:36 -0800909 const int mi_width = mi_size_wide[bsize];
910 const int mi_height = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700911 int xss = xd->plane[1].subsampling_x;
912 int yss = xd->plane[1].subsampling_y;
Jingning Hanfeb517c2016-12-21 16:02:07 -0800913#if CONFIG_CB4X4
914 const int unify_bsize = 1;
915#else
916 const int unify_bsize = 0;
917#endif
918 int b_sub8x8 = (bsize < BLOCK_8X8) && !unify_bsize ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700919 BLOCK_SIZE extend_bsize;
Jingning Han24f24a52016-12-27 10:13:28 -0800920 int mi_row_pred, mi_col_pred;
921
922 int wide_unit, high_unit;
923 int i, j;
924 int ext_offset = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700925
926 if (dir == 0 || dir == 1) {
Jingning Han93531242016-12-20 11:54:36 -0800927 extend_bsize =
928 (mi_width == mi_size_wide[BLOCK_8X8] || bsize < BLOCK_8X8 || xss < yss)
929 ? BLOCK_8X8
930 : BLOCK_16X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800931#if CONFIG_CB4X4
932 if (bsize < BLOCK_8X8) {
933 extend_bsize = BLOCK_4X4;
934 ext_offset = mi_size_wide[BLOCK_8X8];
935 }
936#endif
937
938 wide_unit = mi_size_wide[extend_bsize];
939 high_unit = mi_size_high[extend_bsize];
940
941 mi_row_pred = mi_row + ((dir == 0) ? mi_height : -(mi_height + ext_offset));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700942 mi_col_pred = mi_col;
943
Jingning Han24f24a52016-12-27 10:13:28 -0800944 for (j = 0; j < mi_height + ext_offset; j += high_unit)
945 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
946 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
947 mi_row_pred + j, mi_col_pred + i, mi_row_top,
948 mi_col_top, dst_buf, dst_stride, top_bsize,
949 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700950 } else if (dir == 2 || dir == 3) {
Jingning Han5b7706a2016-12-21 09:55:10 -0800951 extend_bsize =
952 (mi_height == mi_size_high[BLOCK_8X8] || bsize < BLOCK_8X8 || yss < xss)
953 ? BLOCK_8X8
954 : BLOCK_8X16;
Jingning Han24f24a52016-12-27 10:13:28 -0800955#if CONFIG_CB4X4
956 if (bsize < BLOCK_8X8) {
957 extend_bsize = BLOCK_4X4;
958 ext_offset = mi_size_wide[BLOCK_8X8];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700959 }
Jingning Han24f24a52016-12-27 10:13:28 -0800960#endif
961
962 wide_unit = mi_size_wide[extend_bsize];
963 high_unit = mi_size_high[extend_bsize];
964
965 mi_row_pred = mi_row;
966 mi_col_pred = mi_col + ((dir == 3) ? mi_width : -(mi_width + ext_offset));
967
968 for (j = 0; j < mi_height + ext_offset; j += high_unit)
969 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
970 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
971 mi_row_pred + j, mi_col_pred + i, mi_row_top,
972 mi_col_top, dst_buf, dst_stride, top_bsize,
973 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700974 } else {
975 extend_bsize = BLOCK_8X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800976#if CONFIG_CB4X4
977 if (bsize < BLOCK_8X8) {
978 extend_bsize = BLOCK_4X4;
979 ext_offset = mi_size_wide[BLOCK_8X8];
980 }
981#endif
982 wide_unit = mi_size_wide[extend_bsize];
983 high_unit = mi_size_high[extend_bsize];
984
Jingning Han5b7706a2016-12-21 09:55:10 -0800985 mi_row_pred = mi_row + ((dir == 4 || dir == 6) ? mi_height
Jingning Han24f24a52016-12-27 10:13:28 -0800986 : -(mi_height + ext_offset));
Jingning Han5b7706a2016-12-21 09:55:10 -0800987 mi_col_pred =
Jingning Han24f24a52016-12-27 10:13:28 -0800988 mi_col + ((dir == 6 || dir == 7) ? mi_width : -(mi_width + ext_offset));
989
990 for (j = 0; j < mi_height + ext_offset; j += high_unit)
991 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
992 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
993 mi_row_pred + j, mi_col_pred + i, mi_row_top,
994 mi_col_top, dst_buf, dst_stride, top_bsize,
995 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700996 }
997}
998
Yaowu Xuf883b422016-08-30 14:01:10 -0700999static void dec_extend_all(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001000 const TileInfo *const tile, int block,
1001 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
1002 int mi_col, int mi_row_top, int mi_col_top,
1003 uint8_t *dst_buf[3], int dst_stride[3]) {
1004 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1005 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1006 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1007 mi_row_top, mi_col_top, dst_buf, dst_stride, 1);
1008 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1009 mi_row_top, mi_col_top, dst_buf, dst_stride, 2);
1010 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1011 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1012 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1013 mi_row_top, mi_col_top, dst_buf, dst_stride, 4);
1014 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1015 mi_row_top, mi_col_top, dst_buf, dst_stride, 5);
1016 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1017 mi_row_top, mi_col_top, dst_buf, dst_stride, 6);
1018 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1019 mi_row_top, mi_col_top, dst_buf, dst_stride, 7);
1020}
1021
Yaowu Xuf883b422016-08-30 14:01:10 -07001022static void dec_predict_sb_complex(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001023 const TileInfo *const tile, int mi_row,
1024 int mi_col, int mi_row_top, int mi_col_top,
1025 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize,
1026 uint8_t *dst_buf[3], int dst_stride[3]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001027 const AV1_COMMON *const cm = &pbi->common;
Jingning Han5b7706a2016-12-21 09:55:10 -08001028 const int hbs = mi_size_wide[bsize] / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001029 const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
1030 const BLOCK_SIZE subsize = get_subsize(bsize, partition);
1031#if CONFIG_EXT_PARTITION_TYPES
1032 const BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1033#endif
1034 int i;
1035 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1036 uint8_t *dst_buf1[3], *dst_buf2[3], *dst_buf3[3];
Jingning Hanfeb517c2016-12-21 16:02:07 -08001037#if CONFIG_CB4X4
1038 const int unify_bsize = 1;
1039#else
1040 const int unify_bsize = 0;
1041#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001042
1043 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1044 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1045 DECLARE_ALIGNED(16, uint8_t, tmp_buf3[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1046 int dst_stride1[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1047 int dst_stride2[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1048 int dst_stride3[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1049
Yaowu Xuf883b422016-08-30 14:01:10 -07001050#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001051 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1052 int len = sizeof(uint16_t);
1053 dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
1054 dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_TX_SQUARE * len);
1055 dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_TX_SQUARE * len);
1056 dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
1057 dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_TX_SQUARE * len);
1058 dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_TX_SQUARE * len);
1059 dst_buf3[0] = CONVERT_TO_BYTEPTR(tmp_buf3);
1060 dst_buf3[1] = CONVERT_TO_BYTEPTR(tmp_buf3 + MAX_TX_SQUARE * len);
1061 dst_buf3[2] = CONVERT_TO_BYTEPTR(tmp_buf3 + 2 * MAX_TX_SQUARE * len);
1062 } else {
1063#endif
1064 dst_buf1[0] = tmp_buf1;
1065 dst_buf1[1] = tmp_buf1 + MAX_TX_SQUARE;
1066 dst_buf1[2] = tmp_buf1 + 2 * MAX_TX_SQUARE;
1067 dst_buf2[0] = tmp_buf2;
1068 dst_buf2[1] = tmp_buf2 + MAX_TX_SQUARE;
1069 dst_buf2[2] = tmp_buf2 + 2 * MAX_TX_SQUARE;
1070 dst_buf3[0] = tmp_buf3;
1071 dst_buf3[1] = tmp_buf3 + MAX_TX_SQUARE;
1072 dst_buf3[2] = tmp_buf3 + 2 * MAX_TX_SQUARE;
Yaowu Xuf883b422016-08-30 14:01:10 -07001073#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001074 }
1075#endif
1076
1077 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1078
1079 xd->mi = cm->mi_grid_visible + mi_offset;
1080 xd->mi[0] = cm->mi + mi_offset;
1081
1082 for (i = 0; i < MAX_MB_PLANE; i++) {
1083 xd->plane[i].dst.buf = dst_buf[i];
1084 xd->plane[i].dst.stride = dst_stride[i];
1085 }
1086
1087 switch (partition) {
1088 case PARTITION_NONE:
1089 assert(bsize < top_bsize);
1090 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1091 mi_row_top, mi_col_top, dst_buf, dst_stride,
1092 top_bsize, bsize, 0, 0);
1093 dec_extend_all(pbi, xd, tile, 0, bsize, top_bsize, mi_row, mi_col,
1094 mi_row_top, mi_col_top, dst_buf, dst_stride);
1095 break;
1096 case PARTITION_HORZ:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001097 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001098 // For sub8x8, predict in 8x8 unit
1099 // First half
1100 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1101 mi_row_top, mi_col_top, dst_buf, dst_stride,
1102 top_bsize, BLOCK_8X8, 1, 0);
1103 if (bsize < top_bsize)
1104 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1105 mi_row_top, mi_col_top, dst_buf, dst_stride);
1106
1107 // Second half
1108 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1109 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1110 top_bsize, BLOCK_8X8, 1, 1);
1111 if (bsize < top_bsize)
1112 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1113 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1114
1115 // weighted average to smooth the boundary
1116 xd->plane[0].dst.buf = dst_buf[0];
1117 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001118 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001119 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1120 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1121 0);
1122 } else {
1123 // First half
1124 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1125 mi_row_top, mi_col_top, dst_buf, dst_stride,
1126 top_bsize, subsize, 0, 0);
1127 if (bsize < top_bsize)
1128 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1129 mi_row_top, mi_col_top, dst_buf, dst_stride);
1130 else
1131 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1132 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1133
1134 if (mi_row + hbs < cm->mi_rows) {
1135 // Second half
1136 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col,
1137 mi_row + hbs, mi_col, mi_row_top, mi_col_top,
1138 dst_buf1, dst_stride1, top_bsize, subsize, 0, 0);
1139 if (bsize < top_bsize)
1140 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1141 mi_col, mi_row_top, mi_col_top, dst_buf1,
1142 dst_stride1);
1143 else
1144 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1145 mi_col, mi_row_top, mi_col_top, dst_buf1,
1146 dst_stride1, 1);
1147
1148 // weighted average to smooth the boundary
1149 for (i = 0; i < MAX_MB_PLANE; i++) {
1150 xd->plane[i].dst.buf = dst_buf[i];
1151 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001152 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001153 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1154 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1155 PARTITION_HORZ, i);
1156 }
1157 }
1158 }
1159 break;
1160 case PARTITION_VERT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001161 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001162 // First half
1163 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1164 mi_row_top, mi_col_top, dst_buf, dst_stride,
1165 top_bsize, BLOCK_8X8, 1, 0);
1166 if (bsize < top_bsize)
1167 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1168 mi_row_top, mi_col_top, dst_buf, dst_stride);
1169
1170 // Second half
1171 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1172 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1173 top_bsize, BLOCK_8X8, 1, 1);
1174 if (bsize < top_bsize)
1175 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1176 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1177
1178 // Smooth
1179 xd->plane[0].dst.buf = dst_buf[0];
1180 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001181 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001182 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1183 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1184 0);
1185 } else {
1186 // First half
1187 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1188 mi_row_top, mi_col_top, dst_buf, dst_stride,
1189 top_bsize, subsize, 0, 0);
1190 if (bsize < top_bsize)
1191 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1192 mi_row_top, mi_col_top, dst_buf, dst_stride);
1193 else
1194 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1195 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1196
1197 // Second half
1198 if (mi_col + hbs < cm->mi_cols) {
1199 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1200 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1201 dst_stride1, top_bsize, subsize, 0, 0);
1202 if (bsize < top_bsize)
1203 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1204 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1205 dst_stride1);
1206 else
1207 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1208 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1209 dst_stride1, 2);
1210
1211 // Smooth
1212 for (i = 0; i < MAX_MB_PLANE; i++) {
1213 xd->plane[i].dst.buf = dst_buf[i];
1214 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001215 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001216 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1217 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1218 PARTITION_VERT, i);
1219 }
1220 }
1221 }
1222 break;
1223 case PARTITION_SPLIT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001224 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001225 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1226 mi_row_top, mi_col_top, dst_buf, dst_stride,
1227 top_bsize, BLOCK_8X8, 1, 0);
1228 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1229 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1230 top_bsize, BLOCK_8X8, 1, 1);
1231 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1232 mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1233 top_bsize, BLOCK_8X8, 1, 1);
1234 dec_predict_b_extend(pbi, xd, tile, 3, mi_row, mi_col, mi_row, mi_col,
1235 mi_row_top, mi_col_top, dst_buf3, dst_stride3,
1236 top_bsize, BLOCK_8X8, 1, 1);
1237 if (bsize < top_bsize) {
1238 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1239 mi_row_top, mi_col_top, dst_buf, dst_stride);
1240 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1241 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1242 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1243 mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1244 dec_extend_all(pbi, xd, tile, 3, subsize, top_bsize, mi_row, mi_col,
1245 mi_row_top, mi_col_top, dst_buf3, dst_stride3);
1246 }
1247 } else {
1248 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row_top,
1249 mi_col_top, subsize, top_bsize, dst_buf,
1250 dst_stride);
1251 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1252 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col + hbs,
1253 mi_row_top, mi_col_top, subsize, top_bsize,
1254 dst_buf1, dst_stride1);
1255 if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols)
1256 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col,
1257 mi_row_top, mi_col_top, subsize, top_bsize,
1258 dst_buf2, dst_stride2);
1259 if (mi_row + hbs < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1260 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col + hbs,
1261 mi_row_top, mi_col_top, subsize, top_bsize,
1262 dst_buf3, dst_stride3);
1263 }
1264 for (i = 0; i < MAX_MB_PLANE; i++) {
Jingning Han9e0976a2016-12-27 17:52:42 -08001265#if !CONFIG_CB4X4
Jingning Han24f24a52016-12-27 10:13:28 -08001266 if (bsize == BLOCK_8X8 && i != 0)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001267 continue; // Skip <4x4 chroma smoothing
Jingning Han9e0976a2016-12-27 17:52:42 -08001268#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001269 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001270 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001271 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1272 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1273 PARTITION_VERT, i);
1274 if (mi_row + hbs < cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001275 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001276 xd, dst_buf2[i], dst_stride2[i], dst_buf3[i], dst_stride3[i],
1277 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1278 PARTITION_VERT, i);
Yaowu Xuf883b422016-08-30 14:01:10 -07001279 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001280 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1281 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1282 PARTITION_HORZ, i);
1283 }
1284 } else if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001285 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001286 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1287 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1288 PARTITION_HORZ, i);
1289 }
1290 }
1291 break;
1292#if CONFIG_EXT_PARTITION_TYPES
1293 case PARTITION_HORZ_A:
1294 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1295 mi_row_top, mi_col_top, dst_buf, dst_stride,
1296 top_bsize, bsize2, 0, 0);
1297 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1298 mi_row_top, mi_col_top, dst_buf, dst_stride);
1299
1300 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1301 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1302 dst_stride1, top_bsize, bsize2, 0, 0);
1303 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1304 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1305
1306 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1307 mi_col, mi_row_top, mi_col_top, dst_buf2,
1308 dst_stride2, top_bsize, subsize, 0, 0);
1309 if (bsize < top_bsize)
1310 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1311 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1312 else
1313 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1314 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1315 1);
1316
1317 for (i = 0; i < MAX_MB_PLANE; i++) {
1318 xd->plane[i].dst.buf = dst_buf[i];
1319 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001320 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001321 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1322 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1323 i);
1324 }
1325 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001326 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001327 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1328 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1329 i);
1330 }
1331 break;
1332 case PARTITION_VERT_A:
1333
1334 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1335 mi_row_top, mi_col_top, dst_buf, dst_stride,
1336 top_bsize, bsize2, 0, 0);
1337 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1338 mi_row_top, mi_col_top, dst_buf, dst_stride);
1339
1340 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1341 mi_col, mi_row_top, mi_col_top, dst_buf1,
1342 dst_stride1, top_bsize, bsize2, 0, 0);
1343 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1344 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1345
1346 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1347 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1348 dst_stride2, top_bsize, subsize, 0, 0);
1349 if (bsize < top_bsize)
1350 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1351 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1352 dst_stride2);
1353 else
1354 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1355 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1356 dst_stride2, 2);
1357
1358 for (i = 0; i < MAX_MB_PLANE; i++) {
1359 xd->plane[i].dst.buf = dst_buf[i];
1360 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001361 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001362 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1363 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1364 i);
1365 }
1366 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001367 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001368 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1369 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1370 i);
1371 }
1372 break;
1373 case PARTITION_HORZ_B:
1374 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1375 mi_row_top, mi_col_top, dst_buf, dst_stride,
1376 top_bsize, subsize, 0, 0);
1377 if (bsize < top_bsize)
1378 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1379 mi_row_top, mi_col_top, dst_buf, dst_stride);
1380 else
1381 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1382 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1383
1384 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1385 mi_col, mi_row_top, mi_col_top, dst_buf1,
1386 dst_stride1, top_bsize, bsize2, 0, 0);
1387 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1388 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1389
1390 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1391 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1392 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1393 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1394 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1395 dst_stride2);
1396
1397 for (i = 0; i < MAX_MB_PLANE; i++) {
1398 xd->plane[i].dst.buf = dst_buf1[i];
1399 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001400 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001401 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1402 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1403 PARTITION_VERT, i);
1404 }
1405 for (i = 0; i < MAX_MB_PLANE; i++) {
1406 xd->plane[i].dst.buf = dst_buf[i];
1407 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001408 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001409 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1410 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1411 i);
1412 }
1413 break;
1414 case PARTITION_VERT_B:
1415 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1416 mi_row_top, mi_col_top, dst_buf, dst_stride,
1417 top_bsize, subsize, 0, 0);
1418 if (bsize < top_bsize)
1419 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1420 mi_row_top, mi_col_top, dst_buf, dst_stride);
1421 else
1422 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1423 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1424
1425 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1426 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1427 dst_stride1, top_bsize, bsize2, 0, 0);
1428 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1429 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1430
1431 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1432 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1433 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1434 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1435 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1436 dst_stride2);
1437
1438 for (i = 0; i < MAX_MB_PLANE; i++) {
1439 xd->plane[i].dst.buf = dst_buf1[i];
1440 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001441 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001442 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1443 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1444 PARTITION_HORZ, i);
1445 }
1446 for (i = 0; i < MAX_MB_PLANE; i++) {
1447 xd->plane[i].dst.buf = dst_buf[i];
1448 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001449 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001450 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1451 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1452 i);
1453 }
1454 break;
1455#endif // CONFIG_EXT_PARTITION_TYPES
1456 default: assert(0);
1457 }
1458}
1459
Yaowu Xuf883b422016-08-30 14:01:10 -07001460static void set_segment_id_supertx(const AV1_COMMON *const cm, const int mi_row,
1461 const int mi_col, const BLOCK_SIZE bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001462 const struct segmentation *seg = &cm->seg;
Jingning Han5b7706a2016-12-21 09:55:10 -08001463 const int miw = AOMMIN(mi_size_wide[bsize], cm->mi_cols - mi_col);
1464 const int mih = AOMMIN(mi_size_high[bsize], cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001465 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1466 MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
1467 int r, c;
1468 int seg_id_supertx = MAX_SEGMENTS;
1469
1470 if (!seg->enabled) {
1471 seg_id_supertx = 0;
1472 } else {
1473 // Find the minimum segment_id
1474 for (r = 0; r < mih; r++)
1475 for (c = 0; c < miw; c++)
1476 seg_id_supertx =
Yaowu Xuf883b422016-08-30 14:01:10 -07001477 AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001478 assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);
1479 }
1480
1481 // Assign the the segment_id back to segment_id_supertx
1482 for (r = 0; r < mih; r++)
1483 for (c = 0; c < miw; c++)
1484 mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
1485}
1486#endif // CONFIG_SUPERTX
1487
Yue Chen64550b62017-01-12 12:18:22 -08001488static void decode_mbmi_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001489#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001490 int supertx_enabled,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001491#endif // CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001492 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001493#if CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001494 PARTITION_TYPE partition,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001495#endif // CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001496 BLOCK_SIZE bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001497 AV1_COMMON *const cm = &pbi->common;
Jingning Han85dc03f2016-12-06 16:03:10 -08001498 const int bw = mi_size_wide[bsize];
1499 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -07001500 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1501 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Nathan E. Eggeebbd4792016-10-05 19:30:15 -04001502
Michael Bebenita6048d052016-08-25 14:40:54 -07001503#if CONFIG_ACCOUNTING
1504 aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
1505#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001506#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001507 if (supertx_enabled) {
Yue Chen64550b62017-01-12 12:18:22 -08001508 set_mb_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001509 } else {
Yue Chen64550b62017-01-12 12:18:22 -08001510 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001511 }
1512#if CONFIG_EXT_PARTITION_TYPES
1513 xd->mi[0]->mbmi.partition = partition;
1514#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001515 av1_read_mode_info(pbi, xd, supertx_enabled, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001516#else
Yue Chen64550b62017-01-12 12:18:22 -08001517 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001518#if CONFIG_EXT_PARTITION_TYPES
1519 xd->mi[0]->mbmi.partition = partition;
1520#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001521 av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001522#endif // CONFIG_SUPERTX
1523
1524 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1525 const BLOCK_SIZE uv_subsize =
1526 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1527 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -07001528 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001529 "Invalid block size.");
1530 }
1531
1532#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001533 xd->mi[0]->mbmi.segment_id_supertx = MAX_SEGMENTS;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001534#endif // CONFIG_SUPERTX
1535
Angie Chiangd0916d92017-03-10 17:54:18 -08001536 int reader_corrupted_flag = aom_reader_has_error(r);
1537 aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
Yue Chen64550b62017-01-12 12:18:22 -08001538}
1539
1540static void decode_token_and_recon_block(AV1Decoder *const pbi,
1541 MACROBLOCKD *const xd, int mi_row,
1542 int mi_col, aom_reader *r,
1543 BLOCK_SIZE bsize) {
1544 AV1_COMMON *const cm = &pbi->common;
1545 const int bw = mi_size_wide[bsize];
1546 const int bh = mi_size_high[bsize];
1547 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1548 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yue Chen64550b62017-01-12 12:18:22 -08001549
Angie Chiang44701f22017-02-27 10:36:44 -08001550 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
1551 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Yue Chen19e7aa82016-11-30 14:05:39 -08001552
Arild Fuldseth07441162016-08-15 15:07:52 +02001553#if CONFIG_DELTA_Q
1554 if (cm->delta_q_present_flag) {
1555 int i;
1556 for (i = 0; i < MAX_SEGMENTS; i++) {
1557 xd->plane[0].seg_dequant[i][0] =
1558 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
1559 xd->plane[0].seg_dequant[i][1] =
1560 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
1561 xd->plane[1].seg_dequant[i][0] =
1562 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1563 xd->plane[1].seg_dequant[i][1] =
1564 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1565 xd->plane[2].seg_dequant[i][0] =
1566 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1567 xd->plane[2].seg_dequant[i][1] =
1568 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1569 }
1570 }
1571#endif
1572
Jingning Han41bb3392016-12-14 10:46:48 -08001573#if CONFIG_CB4X4
1574 if (mbmi->skip) reset_skip_context(xd, bsize);
1575#else
Jingning Hand39cc722016-12-02 14:03:26 -08001576 if (mbmi->skip) reset_skip_context(xd, AOMMAX(BLOCK_8X8, bsize));
Jingning Han41bb3392016-12-14 10:46:48 -08001577#endif
Jingning Hand39cc722016-12-02 14:03:26 -08001578
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001579#if CONFIG_COEF_INTERLEAVE
1580 {
1581 const struct macroblockd_plane *const pd_y = &xd->plane[0];
1582 const struct macroblockd_plane *const pd_c = &xd->plane[1];
1583 const TX_SIZE tx_log2_y = mbmi->tx_size;
1584 const TX_SIZE tx_log2_c = get_uv_tx_size(mbmi, pd_c);
1585 const int tx_sz_y = (1 << tx_log2_y);
1586 const int tx_sz_c = (1 << tx_log2_c);
1587 const int num_4x4_w_y = pd_y->n4_w;
1588 const int num_4x4_h_y = pd_y->n4_h;
1589 const int num_4x4_w_c = pd_c->n4_w;
1590 const int num_4x4_h_c = pd_c->n4_h;
1591 const int max_4x4_w_y = get_max_4x4_size(num_4x4_w_y, xd->mb_to_right_edge,
1592 pd_y->subsampling_x);
1593 const int max_4x4_h_y = get_max_4x4_size(num_4x4_h_y, xd->mb_to_bottom_edge,
1594 pd_y->subsampling_y);
1595 const int max_4x4_w_c = get_max_4x4_size(num_4x4_w_c, xd->mb_to_right_edge,
1596 pd_c->subsampling_x);
1597 const int max_4x4_h_c = get_max_4x4_size(num_4x4_h_c, xd->mb_to_bottom_edge,
1598 pd_c->subsampling_y);
1599
1600 // The max_4x4_w/h may be smaller than tx_sz under some corner cases,
1601 // i.e. when the SB is splitted by tile boundaries.
1602 const int tu_num_w_y = (max_4x4_w_y + tx_sz_y - 1) / tx_sz_y;
1603 const int tu_num_h_y = (max_4x4_h_y + tx_sz_y - 1) / tx_sz_y;
1604 const int tu_num_w_c = (max_4x4_w_c + tx_sz_c - 1) / tx_sz_c;
1605 const int tu_num_h_c = (max_4x4_h_c + tx_sz_c - 1) / tx_sz_c;
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001606 const int tu_num_c = tu_num_w_c * tu_num_h_c;
1607
1608 if (!is_inter_block(mbmi)) {
1609 int tu_idx_c = 0;
1610 int row_y, col_y, row_c, col_c;
1611 int plane;
1612
1613#if CONFIG_PALETTE
1614 for (plane = 0; plane <= 1; ++plane) {
1615 if (mbmi->palette_mode_info.palette_size[plane])
1616 av1_decode_palette_tokens(xd, plane, r);
1617 }
1618#endif
1619
1620 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1621 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1622 // luma
1623 predict_and_reconstruct_intra_block(
1624 cm, xd, r, mbmi, 0, row_y * tx_sz_y, col_y * tx_sz_y, tx_log2_y);
1625 // chroma
1626 if (tu_idx_c < tu_num_c) {
1627 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1628 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1629 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c,
1630 col_c, tx_log2_c);
1631 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c,
1632 col_c, tx_log2_c);
1633 tu_idx_c++;
1634 }
1635 }
1636 }
1637
1638 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1639 while (tu_idx_c < tu_num_c) {
1640 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1641 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1642 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c, col_c,
1643 tx_log2_c);
1644 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c, col_c,
1645 tx_log2_c);
1646 tu_idx_c++;
1647 }
1648 } else {
1649 // Prediction
David Barkerac37fa32016-12-02 12:30:21 +00001650 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001651 AOMMAX(bsize, BLOCK_8X8));
1652
1653 // Reconstruction
1654 if (!mbmi->skip) {
1655 int eobtotal = 0;
1656 int tu_idx_c = 0;
1657 int row_y, col_y, row_c, col_c;
1658
1659 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1660 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1661 // luma
1662 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 0,
1663 row_y * tx_sz_y,
1664 col_y * tx_sz_y, tx_log2_y);
1665 // chroma
1666 if (tu_idx_c < tu_num_c) {
1667 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1668 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1669 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1670 1, row_c, col_c, tx_log2_c);
1671 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1672 2, row_c, col_c, tx_log2_c);
1673 tu_idx_c++;
1674 }
1675 }
1676 }
1677
1678 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1679 while (tu_idx_c < tu_num_c) {
1680 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1681 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1682 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 1,
1683 row_c, col_c, tx_log2_c);
1684 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 2,
1685 row_c, col_c, tx_log2_c);
1686 tu_idx_c++;
1687 }
1688
Alex Converse64d7ef62017-03-22 18:09:16 -07001689 // TODO(CONFIG_COEF_INTERLEAVE owners): bring eob == 0 corner case
1690 // into line with the defaut configuration
1691 if (bsize >= BLOCK_8X8 && eobtotal == 0) mbmi->skip = 1;
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001692 }
1693 }
1694 }
Angie Chiang133733c2017-03-17 12:50:20 -07001695#else // CONFIG_COEF_INTERLEAVE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001696 if (!is_inter_block(mbmi)) {
1697 int plane;
Fangwen Fub3be9262017-03-06 15:34:28 -08001698#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001699 for (plane = 0; plane <= 1; ++plane) {
1700 if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xuf883b422016-08-30 14:01:10 -07001701 av1_decode_palette_tokens(xd, plane, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001702 }
Fangwen Fub3be9262017-03-06 15:34:28 -08001703#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001704 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1705 const struct macroblockd_plane *const pd = &xd->plane[plane];
Angie Chiang7fcfee42017-02-24 15:51:03 -08001706 const TX_SIZE tx_size = get_tx_size(plane, xd);
Jingning Han2d64f122016-10-21 12:44:29 -07001707 const int stepr = tx_size_high_unit[tx_size];
1708 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han41bb3392016-12-14 10:46:48 -08001709#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001710#if CONFIG_CHROMA_2X2
1711 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1712#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001713 const BLOCK_SIZE plane_bsize =
1714 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001715#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001716#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001717 const BLOCK_SIZE plane_bsize =
1718 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001719#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001720 int row, col;
Jingning Hanbafee8d2016-12-02 10:25:03 -08001721 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1722 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001723#if CONFIG_CB4X4
1724 if (bsize < BLOCK_8X8 && plane && !is_chroma_reference(mi_row, mi_col))
1725 continue;
1726#endif
1727
Yaowu Xuc27fc142016-08-22 16:08:15 -07001728 for (row = 0; row < max_blocks_high; row += stepr)
1729 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001730 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane, row, col,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001731 tx_size);
1732 }
1733 } else {
Yue Chen9ab6d712017-01-12 15:50:46 -08001734 int ref;
1735
1736 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
1737 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
1738 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
1739
1740 xd->block_refs[ref] = ref_buf;
1741 if ((!av1_is_valid_scale(&ref_buf->sf)))
1742 aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
1743 "Reference frame has invalid dimensions");
1744 av1_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col, &ref_buf->sf);
1745 }
Yue Chen69f18e12016-09-08 14:48:15 -07001746#if CONFIG_WARPED_MOTION
1747 if (mbmi->motion_mode == WARPED_CAUSAL) {
1748 int i;
Sarah Parker19234cc2017-03-10 16:43:25 -08001749 assert_motion_mode_valid(WARPED_CAUSAL,
1750#if CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1751 0, cm->global_motion,
1752#endif // CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1753 xd->mi[0]);
Yue Chen69f18e12016-09-08 14:48:15 -07001754 for (i = 0; i < 3; ++i) {
1755 const struct macroblockd_plane *pd = &xd->plane[i];
1756
1757 av1_warp_plane(&mbmi->wm_params[0],
1758#if CONFIG_AOM_HIGHBITDEPTH
1759 xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH, xd->bd,
1760#endif // CONFIG_AOM_HIGHBITDEPTH
1761 pd->pre[0].buf0, pd->pre[0].width, pd->pre[0].height,
1762 pd->pre[0].stride, pd->dst.buf,
1763 ((mi_col * MI_SIZE) >> pd->subsampling_x),
1764 ((mi_row * MI_SIZE) >> pd->subsampling_y),
Jingning Hanff6ee6a2016-12-07 09:55:21 -08001765 xd->n8_w * (MI_SIZE >> pd->subsampling_x),
1766 xd->n8_h * (MI_SIZE >> pd->subsampling_y),
1767 pd->dst.stride, pd->subsampling_x, pd->subsampling_y, 16,
1768 16, 0);
Yue Chen69f18e12016-09-08 14:48:15 -07001769 }
1770 } else {
1771#endif // CONFIG_WARPED_MOTION
Jingning Han41bb3392016-12-14 10:46:48 -08001772#if CONFIG_CB4X4
1773 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL, bsize);
1774#else
1775 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
1776 AOMMAX(bsize, BLOCK_8X8));
1777#endif
Yue Chen69f18e12016-09-08 14:48:15 -07001778#if CONFIG_WARPED_MOTION
1779 }
1780#endif // CONFIG_WARPED_MOTION
Yue Chencb60b182016-10-13 15:18:22 -07001781#if CONFIG_MOTION_VAR
1782 if (mbmi->motion_mode == OBMC_CAUSAL) {
Sarah Parker19234cc2017-03-10 16:43:25 -08001783 assert_motion_mode_valid(OBMC_CAUSAL,
1784#if CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1785 0, cm->global_motion,
1786#endif // CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1787 xd->mi[0]);
Yue Chenf27b1602017-01-13 11:11:43 -08001788#if CONFIG_NCOBMC
1789 av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
1790#else
Yue Chen894fcce2016-10-21 16:50:52 -07001791 av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yue Chenf27b1602017-01-13 11:11:43 -08001792#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001793 }
Yue Chencb60b182016-10-13 15:18:22 -07001794#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001795
1796 // Reconstruction
1797 if (!mbmi->skip) {
1798 int eobtotal = 0;
1799 int plane;
1800
1801 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1802 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han41bb3392016-12-14 10:46:48 -08001803#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001804#if CONFIG_CHROMA_2X2
1805 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1806#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001807 const BLOCK_SIZE plane_bsize =
1808 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001809#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001810#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001811 const BLOCK_SIZE plane_bsize =
1812 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001813#endif
Jingning Hanbafee8d2016-12-02 10:25:03 -08001814 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1815 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001816 int row, col;
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001817
1818#if CONFIG_CB4X4
1819 if (bsize < BLOCK_8X8 && plane && !is_chroma_reference(mi_row, mi_col))
1820 continue;
1821#endif
1822
Yaowu Xuc27fc142016-08-22 16:08:15 -07001823#if CONFIG_VAR_TX
Jingning Han70e5f3f2016-11-09 17:03:07 -08001824 const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07001825 const int bh_var_tx = tx_size_high_unit[max_tx_size];
1826 const int bw_var_tx = tx_size_wide_unit[max_tx_size];
Jingning Hanbafee8d2016-12-02 10:25:03 -08001827 for (row = 0; row < max_blocks_high; row += bh_var_tx)
1828 for (col = 0; col < max_blocks_wide; col += bw_var_tx)
Jingning Hanfe45b212016-11-22 10:30:23 -08001829 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, row, col,
1830 max_tx_size, &eobtotal);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001831#else
Angie Chiang7fcfee42017-02-24 15:51:03 -08001832 const TX_SIZE tx_size = get_tx_size(plane, xd);
Jingning Han2d64f122016-10-21 12:44:29 -07001833 const int stepr = tx_size_high_unit[tx_size];
1834 const int stepc = tx_size_wide_unit[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001835 for (row = 0; row < max_blocks_high; row += stepr)
1836 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001837 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1838 plane, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001839#endif
1840 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001841 }
1842 }
Angie Chiang133733c2017-03-17 12:50:20 -07001843#endif // CONFIG_COEF_INTERLEAVE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001844
Angie Chiangd0916d92017-03-10 17:54:18 -08001845 int reader_corrupted_flag = aom_reader_has_error(r);
1846 aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001847}
1848
Yue Chen9ab6d712017-01-12 15:50:46 -08001849#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
1850static void detoken_and_recon_sb(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1851 int mi_row, int mi_col, aom_reader *r,
1852 BLOCK_SIZE bsize) {
1853 AV1_COMMON *const cm = &pbi->common;
1854 const int hbs = mi_size_wide[bsize] >> 1;
1855#if CONFIG_CB4X4
1856 const int unify_bsize = 1;
1857#else
1858 const int unify_bsize = 0;
1859#endif
1860#if CONFIG_EXT_PARTITION_TYPES
1861 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1862#endif
1863 PARTITION_TYPE partition;
1864 BLOCK_SIZE subsize;
1865 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1866 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1867
1868 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1869
1870 partition = get_partition(cm, mi_row, mi_col, bsize);
1871 subsize = subsize_lookup[partition][bsize];
1872
1873 if (!hbs && !unify_bsize) {
1874 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1875 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1876 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1877 } else {
1878 switch (partition) {
1879 case PARTITION_NONE:
1880 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
1881 break;
1882 case PARTITION_HORZ:
1883 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1884 if (has_rows)
1885 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r,
1886 subsize);
1887 break;
1888 case PARTITION_VERT:
1889 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1890 if (has_cols)
1891 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r,
1892 subsize);
1893 break;
1894 case PARTITION_SPLIT:
1895 detoken_and_recon_sb(pbi, xd, mi_row, mi_col, r, subsize);
1896 detoken_and_recon_sb(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1897 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1898 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col + hbs, r, subsize);
1899 break;
1900#if CONFIG_EXT_PARTITION_TYPES
1901 case PARTITION_HORZ_A:
1902 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1903 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1904 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1905 break;
1906 case PARTITION_HORZ_B:
1907 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1908 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1909 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1910 bsize2);
1911 break;
1912 case PARTITION_VERT_A:
1913 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1914 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1915 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1916 break;
1917 case PARTITION_VERT_B:
1918 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1919 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1920 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1921 bsize2);
1922 break;
1923#endif
1924 default: assert(0 && "Invalid partition type");
1925 }
1926 }
1927}
1928#endif
1929
Yue Chen64550b62017-01-12 12:18:22 -08001930static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1931#if CONFIG_SUPERTX
1932 int supertx_enabled,
1933#endif // CONFIG_SUPERTX
1934 int mi_row, int mi_col, aom_reader *r,
1935#if CONFIG_EXT_PARTITION_TYPES
1936 PARTITION_TYPE partition,
1937#endif // CONFIG_EXT_PARTITION_TYPES
1938 BLOCK_SIZE bsize) {
1939 decode_mbmi_block(pbi, xd,
1940#if CONFIG_SUPERTX
1941 supertx_enabled,
1942#endif
1943 mi_row, mi_col, r,
1944#if CONFIG_EXT_PARTITION_TYPES
1945 partition,
1946#endif
1947 bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001948#if !(CONFIG_MOTION_VAR && CONFIG_NCOBMC)
Yue Chen64550b62017-01-12 12:18:22 -08001949#if CONFIG_SUPERTX
1950 if (!supertx_enabled)
1951#endif // CONFIG_SUPERTX
1952 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001953#endif
Yue Chen64550b62017-01-12 12:18:22 -08001954}
1955
Yaowu Xuf883b422016-08-30 14:01:10 -07001956static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
1957 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001958 int has_rows, int has_cols,
Jingning Han1beb0102016-12-07 11:08:30 -08001959 BLOCK_SIZE bsize) {
Alex Converse55c6bde2017-01-12 15:55:31 -08001960#if CONFIG_UNPOISON_PARTITION_CTX
1961 const int ctx =
1962 partition_plane_context(xd, mi_row, mi_col, has_rows, has_cols, bsize);
1963 const aom_prob *const probs = ctx >= 0 ? cm->fc->partition_prob[ctx] : NULL;
1964 FRAME_COUNTS *const counts = ctx >= 0 ? xd->counts : NULL;
1965#else
Jingning Han1beb0102016-12-07 11:08:30 -08001966 const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
Yaowu Xuf883b422016-08-30 14:01:10 -07001967 const aom_prob *const probs = cm->fc->partition_prob[ctx];
Alex Converse55c6bde2017-01-12 15:55:31 -08001968 FRAME_COUNTS *const counts = xd->counts;
1969#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001970 PARTITION_TYPE p;
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001971#if CONFIG_EC_ADAPT
1972 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1973 (void)cm;
1974#elif CONFIG_EC_MULTISYMBOL
1975 FRAME_CONTEXT *ec_ctx = cm->fc;
1976#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001977
1978 if (has_rows && has_cols)
1979#if CONFIG_EXT_PARTITION_TYPES
1980 if (bsize <= BLOCK_8X8)
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001981#if CONFIG_EC_MULTISYMBOL
1982 p = (PARTITION_TYPE)aom_read_symbol(r, ec_ctx->partition_cdf[ctx],
1983 PARTITION_TYPES, ACCT_STR);
1984#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001985 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001986#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001987 else
Alex Converse57795a42017-03-14 12:18:25 -07001988#if CONFIG_EC_MULTISYMBOL
1989 p = (PARTITION_TYPE)aom_read_symbol(r, ec_ctx->partition_cdf[ctx],
1990 EXT_PARTITION_TYPES, ACCT_STR);
1991#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001992 p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs,
1993 ACCT_STR);
Alex Converse57795a42017-03-14 12:18:25 -07001994#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001995#else
Nathan E. Egge9d9eb6c2016-11-16 09:44:26 -05001996#if CONFIG_EC_MULTISYMBOL
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001997 p = (PARTITION_TYPE)aom_read_symbol(r, ec_ctx->partition_cdf[ctx],
Yaowu Xuf2581a32016-10-20 13:05:47 -07001998 PARTITION_TYPES, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001999#else
Michael Bebenita6048d052016-08-25 14:40:54 -07002000 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04002001#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002002#endif // CONFIG_EXT_PARTITION_TYPES
2003 else if (!has_rows && has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07002004 p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002005 else if (has_rows && !has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07002006 p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002007 else
2008 p = PARTITION_SPLIT;
2009
2010 if (counts) ++counts->partition[ctx][p];
2011
2012 return p;
2013}
2014
2015#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002016static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
2017 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002018 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
2019 return 1;
2020 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002021 const int ctx = av1_get_skip_context(xd);
Michael Bebenita6048d052016-08-25 14:40:54 -07002022 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002023 FRAME_COUNTS *counts = xd->counts;
2024 if (counts) ++counts->skip[ctx][skip];
2025 return skip;
2026 }
2027}
2028#endif // CONFIG_SUPERTX
2029
2030// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -07002031static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002032#if CONFIG_SUPERTX
2033 int supertx_enabled,
2034#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002035 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002036 BLOCK_SIZE bsize, int n4x4_l2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002037 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002038 const int n8x8_l2 = n4x4_l2 - 1;
Jingning Hanff17e162016-12-07 17:58:18 -08002039 const int num_8x8_wh = mi_size_wide[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002040 const int hbs = num_8x8_wh >> 1;
Jingning Han41bb3392016-12-14 10:46:48 -08002041#if CONFIG_CB4X4
2042 const int unify_bsize = 1;
2043#else
2044 const int unify_bsize = 0;
2045#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002046 PARTITION_TYPE partition;
2047 BLOCK_SIZE subsize;
2048#if CONFIG_EXT_PARTITION_TYPES
2049 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
2050#endif
2051 const int has_rows = (mi_row + hbs) < cm->mi_rows;
2052 const int has_cols = (mi_col + hbs) < cm->mi_cols;
2053#if CONFIG_SUPERTX
2054 const int read_token = !supertx_enabled;
2055 int skip = 0;
Jingning Han2511c662016-12-22 11:57:34 -08002056 TX_SIZE supertx_size = max_txsize_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002057 const TileInfo *const tile = &xd->tile;
2058 int txfm = DCT_DCT;
2059#endif // CONFIG_SUPERTX
2060
2061 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
2062
Jingning Han6dbacf22016-12-14 17:28:35 -08002063 partition = (n4x4_l2 == 0) ? PARTITION_NONE
2064 : read_partition(cm, xd, mi_row, mi_col, r,
2065 has_rows, has_cols, bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002066 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
Yushin Cho77bba8d2016-11-04 16:36:56 -07002067
2068#if CONFIG_PVQ
2069 assert(partition < PARTITION_TYPES);
2070 assert(subsize < BLOCK_SIZES);
2071#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002072#if CONFIG_SUPERTX
2073 if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
2074 bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
2075 const int supertx_context = partition_supertx_context_lookup[partition];
Michael Bebenita6048d052016-08-25 14:40:54 -07002076 supertx_enabled = aom_read(
2077 r, cm->fc->supertx_prob[supertx_context][supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002078 if (xd->counts)
2079 xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
2080#if CONFIG_VAR_TX
2081 if (supertx_enabled) xd->supertx_size = supertx_size;
2082#endif
2083 }
2084#endif // CONFIG_SUPERTX
Jingning Han41bb3392016-12-14 10:46:48 -08002085 if (!hbs && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002086 // calculate bmode block dimensions (log 2)
2087 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
2088 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
2089 decode_block(pbi, xd,
2090#if CONFIG_SUPERTX
2091 supertx_enabled,
2092#endif // CONFIG_SUPERTX
2093 mi_row, mi_col, r,
2094#if CONFIG_EXT_PARTITION_TYPES
2095 partition,
2096#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002097 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002098 } else {
2099 switch (partition) {
2100 case PARTITION_NONE:
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 break;
2111 case PARTITION_HORZ:
2112 decode_block(pbi, xd,
2113#if CONFIG_SUPERTX
2114 supertx_enabled,
2115#endif // CONFIG_SUPERTX
2116 mi_row, mi_col, r,
2117#if CONFIG_EXT_PARTITION_TYPES
2118 partition,
2119#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002120 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002121 if (has_rows)
2122 decode_block(pbi, xd,
2123#if CONFIG_SUPERTX
2124 supertx_enabled,
2125#endif // CONFIG_SUPERTX
2126 mi_row + hbs, mi_col, r,
2127#if CONFIG_EXT_PARTITION_TYPES
2128 partition,
2129#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002130 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002131 break;
2132 case PARTITION_VERT:
2133 decode_block(pbi, xd,
2134#if CONFIG_SUPERTX
2135 supertx_enabled,
2136#endif // CONFIG_SUPERTX
2137 mi_row, mi_col, r,
2138#if CONFIG_EXT_PARTITION_TYPES
2139 partition,
2140#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002141 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002142 if (has_cols)
2143 decode_block(pbi, xd,
2144#if CONFIG_SUPERTX
2145 supertx_enabled,
2146#endif // CONFIG_SUPERTX
2147 mi_row, mi_col + hbs, r,
2148#if CONFIG_EXT_PARTITION_TYPES
2149 partition,
2150#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002151 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002152 break;
2153 case PARTITION_SPLIT:
2154 decode_partition(pbi, xd,
2155#if CONFIG_SUPERTX
2156 supertx_enabled,
2157#endif // CONFIG_SUPERTX
2158 mi_row, mi_col, r, subsize, n8x8_l2);
2159 decode_partition(pbi, xd,
2160#if CONFIG_SUPERTX
2161 supertx_enabled,
2162#endif // CONFIG_SUPERTX
2163 mi_row, mi_col + hbs, r, subsize, n8x8_l2);
2164 decode_partition(pbi, xd,
2165#if CONFIG_SUPERTX
2166 supertx_enabled,
2167#endif // CONFIG_SUPERTX
2168 mi_row + hbs, mi_col, r, subsize, n8x8_l2);
2169 decode_partition(pbi, xd,
2170#if CONFIG_SUPERTX
2171 supertx_enabled,
2172#endif // CONFIG_SUPERTX
2173 mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
2174 break;
2175#if CONFIG_EXT_PARTITION_TYPES
2176 case PARTITION_HORZ_A:
2177 decode_block(pbi, xd,
2178#if CONFIG_SUPERTX
2179 supertx_enabled,
2180#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002181 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002182 decode_block(pbi, xd,
2183#if CONFIG_SUPERTX
2184 supertx_enabled,
2185#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002186 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002187 decode_block(pbi, xd,
2188#if CONFIG_SUPERTX
2189 supertx_enabled,
2190#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002191 mi_row + hbs, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002192 break;
2193 case PARTITION_HORZ_B:
2194 decode_block(pbi, xd,
2195#if CONFIG_SUPERTX
2196 supertx_enabled,
2197#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002198 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002199 decode_block(pbi, xd,
2200#if CONFIG_SUPERTX
2201 supertx_enabled,
2202#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002203 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002204 decode_block(pbi, xd,
2205#if CONFIG_SUPERTX
2206 supertx_enabled,
2207#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002208 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002209 break;
2210 case PARTITION_VERT_A:
2211 decode_block(pbi, xd,
2212#if CONFIG_SUPERTX
2213 supertx_enabled,
2214#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002215 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002216 decode_block(pbi, xd,
2217#if CONFIG_SUPERTX
2218 supertx_enabled,
2219#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002220 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002221 decode_block(pbi, xd,
2222#if CONFIG_SUPERTX
2223 supertx_enabled,
2224#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002225 mi_row, mi_col + hbs, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002226 break;
2227 case PARTITION_VERT_B:
2228 decode_block(pbi, xd,
2229#if CONFIG_SUPERTX
2230 supertx_enabled,
2231#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002232 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002233 decode_block(pbi, xd,
2234#if CONFIG_SUPERTX
2235 supertx_enabled,
2236#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002237 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002238 decode_block(pbi, xd,
2239#if CONFIG_SUPERTX
2240 supertx_enabled,
2241#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002242 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002243 break;
2244#endif
2245 default: assert(0 && "Invalid partition type");
2246 }
2247 }
2248
2249#if CONFIG_SUPERTX
2250 if (supertx_enabled && read_token) {
2251 uint8_t *dst_buf[3];
2252 int dst_stride[3], i;
2253 int offset = mi_row * cm->mi_stride + mi_col;
2254
2255 set_segment_id_supertx(cm, mi_row, mi_col, bsize);
2256
David Barker3aec8d62017-01-31 14:55:32 +00002257#if CONFIG_DELTA_Q
2258 if (cm->delta_q_present_flag) {
2259 for (i = 0; i < MAX_SEGMENTS; i++) {
2260 xd->plane[0].seg_dequant[i][0] =
2261 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
2262 xd->plane[0].seg_dequant[i][1] =
2263 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
2264 xd->plane[1].seg_dequant[i][0] =
2265 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2266 xd->plane[1].seg_dequant[i][1] =
2267 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2268 xd->plane[2].seg_dequant[i][0] =
2269 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2270 xd->plane[2].seg_dequant[i][1] =
2271 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2272 }
2273 }
2274#endif
2275
Yaowu Xuc27fc142016-08-22 16:08:15 -07002276 xd->mi = cm->mi_grid_visible + offset;
2277 xd->mi[0] = cm->mi + offset;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002278#if CONFIG_DEPENDENT_HORZTILES
2279 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2280 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols,
2281 cm->dependent_horz_tiles);
2282#else
Jingning Han5b7706a2016-12-21 09:55:10 -08002283 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2284 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002285#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002286 set_skip_context(xd, mi_row, mi_col);
2287 skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
2288 if (skip) {
2289 reset_skip_context(xd, bsize);
2290 } else {
2291#if CONFIG_EXT_TX
Sarah Parkere68a3e42017-02-16 14:03:24 -08002292 if (get_ext_tx_types(supertx_size, bsize, 1, cm->reduced_tx_set_used) >
2293 1) {
2294 const int eset =
2295 get_ext_tx_set(supertx_size, bsize, 1, cm->reduced_tx_set_used);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002296 if (eset > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002297 txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
Michael Bebenita6048d052016-08-25 14:40:54 -07002298 cm->fc->inter_ext_tx_prob[eset][supertx_size],
2299 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002300 if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
2301 }
2302 }
2303#else
2304 if (supertx_size < TX_32X32) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002305 txfm = aom_read_tree(r, av1_ext_tx_tree,
Jingning Han8f6eb182016-10-19 13:48:57 -07002306 cm->fc->inter_ext_tx_prob[supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002307 if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
2308 }
2309#endif // CONFIG_EXT_TX
2310 }
2311
Yaowu Xuf883b422016-08-30 14:01:10 -07002312 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002313 for (i = 0; i < MAX_MB_PLANE; i++) {
2314 dst_buf[i] = xd->plane[i].dst.buf;
2315 dst_stride[i] = xd->plane[i].dst.stride;
2316 }
2317 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
2318 bsize, dst_buf, dst_stride);
2319
2320 if (!skip) {
2321 int eobtotal = 0;
2322 MB_MODE_INFO *mbmi;
2323 set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
2324 mbmi = &xd->mi[0]->mbmi;
2325 mbmi->tx_type = txfm;
2326 assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
2327 for (i = 0; i < MAX_MB_PLANE; ++i) {
2328 const struct macroblockd_plane *const pd = &xd->plane[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002329 int row, col;
Angie Chiang7fcfee42017-02-24 15:51:03 -08002330 const TX_SIZE tx_size = get_tx_size(i, xd);
Jingning Han5b7706a2016-12-21 09:55:10 -08002331 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Jingning Han32b20282016-10-28 15:42:44 -07002332 const int stepr = tx_size_high_unit[tx_size];
2333 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han5b7706a2016-12-21 09:55:10 -08002334 const int max_blocks_wide = max_block_wide(xd, plane_bsize, i);
2335 const int max_blocks_high = max_block_high(xd, plane_bsize, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002336
2337 for (row = 0; row < max_blocks_high; row += stepr)
2338 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07002339 eobtotal += reconstruct_inter_block(
2340 cm, xd, r, mbmi->segment_id_supertx, i, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002341 }
Jingning Han24f24a52016-12-27 10:13:28 -08002342 if ((unify_bsize || !(subsize < BLOCK_8X8)) && eobtotal == 0) skip = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002343 }
2344 set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
2345 }
2346#endif // CONFIG_SUPERTX
2347
2348#if CONFIG_EXT_PARTITION_TYPES
2349 if (bsize >= BLOCK_8X8) {
2350 switch (partition) {
2351 case PARTITION_SPLIT:
2352 if (bsize > BLOCK_8X8) break;
2353 case PARTITION_NONE:
2354 case PARTITION_HORZ:
2355 case PARTITION_VERT:
2356 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
2357 break;
2358 case PARTITION_HORZ_A:
2359 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2360 update_partition_context(xd, mi_row + hbs, mi_col, subsize, subsize);
2361 break;
2362 case PARTITION_HORZ_B:
2363 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2364 update_partition_context(xd, mi_row + hbs, mi_col, bsize2, subsize);
2365 break;
2366 case PARTITION_VERT_A:
2367 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2368 update_partition_context(xd, mi_row, mi_col + hbs, subsize, subsize);
2369 break;
2370 case PARTITION_VERT_B:
2371 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2372 update_partition_context(xd, mi_row, mi_col + hbs, bsize2, subsize);
2373 break;
2374 default: assert(0 && "Invalid partition type");
2375 }
2376 }
2377#else
2378 // update partition context
2379 if (bsize >= BLOCK_8X8 &&
2380 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
Jingning Han1beb0102016-12-07 11:08:30 -08002381 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
David Barkerf8935c92016-10-26 14:54:06 +01002382#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xud71be782016-10-14 08:47:03 -07002383
Jean-Marc Valin01435132017-02-18 14:12:53 -05002384#if CONFIG_CDEF
David Barker9739f362016-11-10 09:29:32 +00002385#if CONFIG_EXT_PARTITION
2386 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128) {
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002387 if (!sb_all_skip(cm, mi_row, mi_col)) {
Jean-Marc Valin1df427e2017-03-23 18:32:57 -04002388 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2389 aom_read_literal(r, cm->cdef_bits, ACCT_STR);
David Barker9739f362016-11-10 09:29:32 +00002390 } else {
Jean-Marc Valin1df427e2017-03-23 18:32:57 -04002391 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2392 0;
David Barker9739f362016-11-10 09:29:32 +00002393 }
2394 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64) {
2395#else
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002396 if (bsize == BLOCK_64X64) {
David Barker9739f362016-11-10 09:29:32 +00002397#endif
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002398 if (!sb_all_skip(cm, mi_row, mi_col)) {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002399 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2400 aom_read_literal(r, cm->cdef_bits, ACCT_STR);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002401 } else {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002402 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2403 0;
Yaowu Xud71be782016-10-14 08:47:03 -07002404 }
2405 }
Jean-Marc Valin01435132017-02-18 14:12:53 -05002406#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002407}
2408
Yaowu Xuc27fc142016-08-22 16:08:15 -07002409static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
2410 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002411 struct aom_internal_error_info *error_info,
Alex Converseeb780e72016-12-13 12:46:41 -08002412 aom_reader *r,
2413#if CONFIG_ANS && ANS_MAX_SYMBOLS
2414 int window_size,
2415#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
2416 aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002417 // Validate the calculated partition length. If the buffer
2418 // described by the partition can't be fully read, then restrict
2419 // it to the portion that can be (for EC mode) or throw an error.
2420 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002421 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002422 "Truncated packet or corrupt tile length");
2423
Alex Converse2cdf0d82016-12-13 13:53:09 -08002424#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08002425 r->window_size = window_size;
Alex Converse2cdf0d82016-12-13 13:53:09 -08002426#endif
Alex Converse346440b2017-01-03 13:47:37 -08002427 if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07002428 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002429 "Failed to allocate bool decoder %d", 1);
2430}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002431
Angie Chiang800df032017-03-22 11:14:12 -07002432#if !CONFIG_PVQ && !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET) && !CONFIG_LV_MAP
Yaowu Xuf883b422016-08-30 14:01:10 -07002433static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
2434 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002435 int i, j, k, l, m;
Alex Conversea9598cd2017-02-03 14:18:05 -08002436#if CONFIG_EC_ADAPT
2437 const int node_limit = UNCONSTRAINED_NODES - 1;
2438#else
Thomas9ac55082016-09-23 18:04:17 +01002439 const int node_limit = UNCONSTRAINED_NODES;
Alex Conversea9598cd2017-02-03 14:18:05 -08002440#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002441
Michael Bebenita6048d052016-08-25 14:40:54 -07002442 if (aom_read_bit(r, ACCT_STR))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002443 for (i = 0; i < PLANE_TYPES; ++i)
2444 for (j = 0; j < REF_TYPES; ++j)
2445 for (k = 0; k < COEF_BANDS; ++k)
2446 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
Thomas9ac55082016-09-23 18:04:17 +01002447 for (m = 0; m < node_limit; ++m)
Michael Bebenita6048d052016-08-25 14:40:54 -07002448 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002449}
2450
Yaowu Xuf883b422016-08-30 14:01:10 -07002451static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002452 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
2453 TX_SIZE tx_size;
Jingning Han83630632016-12-16 11:27:25 -08002454 for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002455 read_coef_probs_common(fc->coef_probs[tx_size], r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002456}
Yushin Cho77bba8d2016-11-04 16:36:56 -07002457#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002458
Yaowu Xuf883b422016-08-30 14:01:10 -07002459static void setup_segmentation(AV1_COMMON *const cm,
2460 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002461 struct segmentation *const seg = &cm->seg;
2462 int i, j;
2463
2464 seg->update_map = 0;
2465 seg->update_data = 0;
2466
Yaowu Xuf883b422016-08-30 14:01:10 -07002467 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002468 if (!seg->enabled) return;
2469
2470 // Segmentation map update
2471 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2472 seg->update_map = 1;
2473 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002474 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002475 }
2476 if (seg->update_map) {
2477 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2478 seg->temporal_update = 0;
2479 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002480 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002481 }
2482 }
2483
2484 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07002485 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002486 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002487 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002488
Yaowu Xuf883b422016-08-30 14:01:10 -07002489 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002490
2491 for (i = 0; i < MAX_SEGMENTS; i++) {
2492 for (j = 0; j < SEG_LVL_MAX; j++) {
2493 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002494 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002495 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002496 av1_enable_segfeature(seg, i, j);
2497 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
2498 if (av1_is_segfeature_signed(j))
2499 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002500 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002501 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002502 }
2503 }
2504 }
2505}
2506
2507#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002508static void decode_restoration_mode(AV1_COMMON *cm,
2509 struct aom_read_bit_buffer *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002510 int p;
2511 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002512 if (aom_rb_read_bit(rb)) {
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002513 rsi->frame_restoration_type =
2514 aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002515 } else {
2516 rsi->frame_restoration_type =
2517 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
2518 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002519 for (p = 1; p < MAX_MB_PLANE; ++p) {
2520 cm->rst_info[p].frame_restoration_type =
2521 aom_rb_read_bit(rb) ? RESTORE_WIENER : RESTORE_NONE;
2522 }
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002523
2524 cm->rst_info[0].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2525 cm->rst_info[1].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2526 cm->rst_info[2].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2527 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
2528 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
2529 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
2530 rsi = &cm->rst_info[0];
2531 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2532 if (rsi->restoration_tilesize != RESTORATION_TILESIZE_MAX) {
2533 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2534 }
2535 cm->rst_info[1].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2536 cm->rst_info[2].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2537 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002538}
2539
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002540static void read_wiener_filter(WienerInfo *wiener_info, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002541 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002542 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2543 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002544 wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002545 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2546 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002547 wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002548 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2549 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002550 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002551 wiener_info->vfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002552 -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
2553 wiener_info->vfilter[2]);
2554
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002555 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002556 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2557 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002558 wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002559 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2560 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002561 wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002562 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2563 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002564 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002565 wiener_info->hfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002566 -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
2567 wiener_info->hfilter[2]);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002568}
2569
2570static void read_sgrproj_filter(SgrprojInfo *sgrproj_info, aom_reader *rb) {
2571 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
2572 sgrproj_info->xqd[0] =
2573 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN0;
2574 sgrproj_info->xqd[1] =
2575 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN1;
2576}
2577
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002578static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002579 int i, p;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002580 const int ntiles = av1_get_rest_ntiles(cm->width, cm->height,
2581 cm->rst_info[0].restoration_tilesize,
2582 NULL, NULL, NULL, NULL);
2583 const int ntiles_uv = av1_get_rest_ntiles(
2584 ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
2585 ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y),
2586 cm->rst_info[1].restoration_tilesize, NULL, NULL, NULL, NULL);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002587 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002588 if (rsi->frame_restoration_type != RESTORE_NONE) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002589 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002590 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002591 rsi->restoration_type[i] =
2592 aom_read_tree(rb, av1_switchable_restore_tree,
2593 cm->fc->switchable_restore_prob, ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002594 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002595 read_wiener_filter(&rsi->wiener_info[i], rb);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002596 } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002597 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002598 }
2599 }
2600 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002601 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002602 if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002603 rsi->restoration_type[i] = RESTORE_WIENER;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002604 read_wiener_filter(&rsi->wiener_info[i], rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002605 } else {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002606 rsi->restoration_type[i] = RESTORE_NONE;
2607 }
2608 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002609 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002610 for (i = 0; i < ntiles; ++i) {
2611 if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
2612 rsi->restoration_type[i] = RESTORE_SGRPROJ;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002613 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
2614 } else {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002615 rsi->restoration_type[i] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002616 }
2617 }
2618 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002619 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002620 for (p = 1; p < MAX_MB_PLANE; ++p) {
2621 rsi = &cm->rst_info[p];
2622 if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08002623 for (i = 0; i < ntiles_uv; ++i) {
2624 if (ntiles_uv > 1)
2625 rsi->restoration_type[i] =
2626 aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR) ? RESTORE_WIENER
2627 : RESTORE_NONE;
2628 else
2629 rsi->restoration_type[i] = RESTORE_WIENER;
2630 if (rsi->restoration_type[i] == RESTORE_WIENER) {
2631 read_wiener_filter(&rsi->wiener_info[i], rb);
2632 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002633 }
2634 }
2635 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002636}
2637#endif // CONFIG_LOOP_RESTORATION
2638
Yaowu Xuf883b422016-08-30 14:01:10 -07002639static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002640 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07002641 lf->filter_level = aom_rb_read_literal(rb, 6);
2642 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002643
2644 // Read in loop filter deltas applied at the MB level based on mode or ref
2645 // frame.
2646 lf->mode_ref_delta_update = 0;
2647
Yaowu Xuf883b422016-08-30 14:01:10 -07002648 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002649 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002650 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002651 if (lf->mode_ref_delta_update) {
2652 int i;
2653
2654 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002655 if (aom_rb_read_bit(rb))
2656 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002657
2658 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002659 if (aom_rb_read_bit(rb))
2660 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002661 }
2662 }
2663}
2664
Jean-Marc Valin01435132017-02-18 14:12:53 -05002665#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002666static void setup_cdef(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002667 int i;
2668 cm->cdef_bits = aom_rb_read_literal(rb, 2);
2669 cm->nb_cdef_strengths = 1 << cm->cdef_bits;
2670 for (i = 0; i < cm->nb_cdef_strengths; i++) {
2671 cm->cdef_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
Jean-Marc Valine9f77422017-03-22 17:09:51 -04002672 cm->cdef_uv_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002673 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002674}
Jean-Marc Valin01435132017-02-18 14:12:53 -05002675#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002676
Yaowu Xuf883b422016-08-30 14:01:10 -07002677static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2678 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002679}
2680
Yaowu Xuf883b422016-08-30 14:01:10 -07002681static void setup_quantization(AV1_COMMON *const cm,
2682 struct aom_read_bit_buffer *rb) {
2683 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002684 cm->y_dc_delta_q = read_delta_q(rb);
2685 cm->uv_dc_delta_q = read_delta_q(rb);
2686 cm->uv_ac_delta_q = read_delta_q(rb);
2687 cm->dequant_bit_depth = cm->bit_depth;
2688#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002689 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002690 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002691 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2692 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002693 } else {
2694 cm->min_qmlevel = 0;
2695 cm->max_qmlevel = 0;
2696 }
2697#endif
2698}
2699
Yaowu Xuf883b422016-08-30 14:01:10 -07002700static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002701 // Build y/uv dequant values based on segmentation.
2702 int i = 0;
2703#if CONFIG_AOM_QM
2704 int lossless;
2705 int j = 0;
2706 int qmlevel;
2707 int using_qm = cm->using_qmatrix;
2708 int minqm = cm->min_qmlevel;
2709 int maxqm = cm->max_qmlevel;
2710#endif
2711#if CONFIG_NEW_QUANT
2712 int b;
2713 int dq;
2714#endif // CONFIG_NEW_QUANT
2715 if (cm->seg.enabled) {
2716 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002717 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002718 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002719 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2720 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002721 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002722 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002723 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002724 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002725#if CONFIG_AOM_QM
2726 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2727 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2728 // NB: depends on base index so there is only 1 set per frame
2729 // No quant weighting when lossless or signalled not using QM
2730 qmlevel = (lossless || using_qm == 0)
2731 ? NUM_QM_LEVELS - 1
2732 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2733 for (j = 0; j < TX_SIZES; ++j) {
2734 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2735 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2736 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2737 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2738 }
2739#endif // CONFIG_AOM_QM
2740#if CONFIG_NEW_QUANT
2741 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2742 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002743 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002744 cm->y_dequant_nuq[i][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002745 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002746 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002747 }
2748 }
2749#endif // CONFIG_NEW_QUANT
2750 }
2751 } else {
2752 const int qindex = cm->base_qindex;
2753 // When segmentation is disabled, only the first value is used. The
2754 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002755 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2756 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002757 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002758 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002759 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002760 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002761#if CONFIG_AOM_QM
2762 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2763 cm->uv_ac_delta_q == 0;
2764 // No quant weighting when lossless or signalled not using QM
2765 qmlevel = (lossless || using_qm == 0)
2766 ? NUM_QM_LEVELS - 1
2767 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2768 for (j = 0; j < TX_SIZES; ++j) {
2769 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2770 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2771 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2772 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2773 }
2774#endif
2775#if CONFIG_NEW_QUANT
2776 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2777 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002778 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002779 cm->y_dequant_nuq[0][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002780 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002781 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002782 }
2783 }
2784#endif // CONFIG_NEW_QUANT
2785 }
2786}
2787
Angie Chiang5678ad92016-11-21 09:38:40 -08002788static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002789 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07002790 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002791}
2792
Yaowu Xuf883b422016-08-30 14:01:10 -07002793static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002794 cm->render_width = cm->width;
2795 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002796 if (aom_rb_read_bit(rb))
2797 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002798}
2799
Yaowu Xuf883b422016-08-30 14:01:10 -07002800static void resize_mv_buffer(AV1_COMMON *cm) {
2801 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002802 cm->cur_frame->mi_rows = cm->mi_rows;
2803 cm->cur_frame->mi_cols = cm->mi_cols;
2804 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002805 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002806 sizeof(*cm->cur_frame->mvs)));
2807}
2808
Yaowu Xuf883b422016-08-30 14:01:10 -07002809static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002810#if CONFIG_SIZE_LIMIT
2811 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002812 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002813 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2814 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2815#endif
2816 if (cm->width != width || cm->height != height) {
2817 const int new_mi_rows =
2818 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2819 const int new_mi_cols =
2820 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2821
Yaowu Xuf883b422016-08-30 14:01:10 -07002822 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002823 // dimensions as well as the overall size.
2824 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002825 if (av1_alloc_context_buffers(cm, width, height))
2826 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002827 "Failed to allocate context buffers");
2828 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002829 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002830 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002831 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002832 cm->width = width;
2833 cm->height = height;
2834 }
2835 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2836 cm->mi_cols > cm->cur_frame->mi_cols) {
2837 resize_mv_buffer(cm);
2838 }
2839}
2840
Yaowu Xuf883b422016-08-30 14:01:10 -07002841static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002842 int width, height;
2843 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002844 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002845 resize_context_buffers(cm, width, height);
2846 setup_render_size(cm, rb);
2847
2848 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002849 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002850 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2851 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002852#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002853 cm->use_highbitdepth,
2854#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002855 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002856 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2857 pool->cb_priv)) {
2858 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002859 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002860 "Failed to allocate frame buffer");
2861 }
2862 unlock_buffer_pool(pool);
2863
2864 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2865 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2866 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2867 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2868 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2869 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2870 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2871}
2872
Yaowu Xuf883b422016-08-30 14:01:10 -07002873static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002874 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002875 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002876 int this_xss, int this_yss) {
2877 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2878 ref_yss == this_yss;
2879}
2880
Yaowu Xuf883b422016-08-30 14:01:10 -07002881static void setup_frame_size_with_refs(AV1_COMMON *cm,
2882 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002883 int width, height;
2884 int found = 0, i;
2885 int has_valid_ref_frame = 0;
2886 BufferPool *const pool = cm->buffer_pool;
2887 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002888 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002889 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2890 width = buf->y_crop_width;
2891 height = buf->y_crop_height;
2892 cm->render_width = buf->render_width;
2893 cm->render_height = buf->render_height;
2894 found = 1;
2895 break;
2896 }
2897 }
2898
2899 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002900 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002901 setup_render_size(cm, rb);
2902 }
2903
2904 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002905 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002906 "Invalid frame size");
2907
2908 // Check to make sure at least one of frames that this frame references
2909 // has valid dimensions.
2910 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2911 RefBuffer *const ref_frame = &cm->frame_refs[i];
2912 has_valid_ref_frame |=
2913 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2914 ref_frame->buf->y_crop_height, width, height);
2915 }
2916 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002917 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002918 "Referenced frame has invalid size");
2919 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2920 RefBuffer *const ref_frame = &cm->frame_refs[i];
2921 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
2922 ref_frame->buf->subsampling_x,
2923 ref_frame->buf->subsampling_y, cm->bit_depth,
2924 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07002925 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002926 "Referenced frame has incompatible color format");
2927 }
2928
2929 resize_context_buffers(cm, width, height);
2930
2931 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002932 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002933 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2934 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002935#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002936 cm->use_highbitdepth,
2937#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002938 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002939 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2940 pool->cb_priv)) {
2941 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002942 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002943 "Failed to allocate frame buffer");
2944 }
2945 unlock_buffer_pool(pool);
2946
2947 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2948 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2949 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2950 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2951 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2952 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2953 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2954}
2955
Yaowu Xuf883b422016-08-30 14:01:10 -07002956static void read_tile_info(AV1Decoder *const pbi,
2957 struct aom_read_bit_buffer *const rb) {
2958 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002959#if CONFIG_EXT_TILE
2960// Read the tile width/height
2961#if CONFIG_EXT_PARTITION
2962 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002963 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
2964 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002965 } else
2966#endif // CONFIG_EXT_PARTITION
2967 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002968 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
2969 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002970 }
2971
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002972#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002973 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002974#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002975
Yaowu Xuc27fc142016-08-22 16:08:15 -07002976 cm->tile_width <<= cm->mib_size_log2;
2977 cm->tile_height <<= cm->mib_size_log2;
2978
Yaowu Xuf883b422016-08-30 14:01:10 -07002979 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
2980 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002981
2982 // Get the number of tiles
2983 cm->tile_cols = 1;
2984 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
2985
2986 cm->tile_rows = 1;
2987 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
2988
2989 if (cm->tile_cols * cm->tile_rows > 1) {
2990 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07002991 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2992 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002993 }
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002994
2995#if CONFIG_DEPENDENT_HORZTILES
2996 if (cm->tile_rows <= 1)
2997 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
2998 else
2999 cm->dependent_horz_tiles = 0;
3000#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003001#else
3002 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07003003 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003004
3005 // columns
3006 max_ones = max_log2_tile_cols - min_log2_tile_cols;
3007 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07003008 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003009
3010 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07003011 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003012 "Invalid number of tile columns");
3013
3014 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07003015 cm->log2_tile_rows = aom_rb_read_bit(rb);
3016 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003017#if CONFIG_DEPENDENT_HORZTILES
3018 if (cm->log2_tile_rows != 0)
3019 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
3020 else
3021 cm->dependent_horz_tiles = 0;
3022#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003023#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003024 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003025#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003026
Yaowu Xuc27fc142016-08-22 16:08:15 -07003027 cm->tile_cols = 1 << cm->log2_tile_cols;
3028 cm->tile_rows = 1 << cm->log2_tile_rows;
3029
3030 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
3031 cm->tile_width >>= cm->log2_tile_cols;
3032 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
3033 cm->tile_height >>= cm->log2_tile_rows;
3034
3035 // round to integer multiples of superblock size
3036 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
3037 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
3038
Thomas Davies4974e522016-11-07 17:44:05 +00003039// tile size magnitude
3040#if !CONFIG_TILE_GROUPS
3041 if (cm->tile_rows > 1 || cm->tile_cols > 1)
3042#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003043 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003044#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00003045
Thomas Davies80188d12016-10-26 16:08:35 -07003046#if CONFIG_TILE_GROUPS
3047 // Store an index to the location of the tile group information
3048 pbi->tg_size_bit_offset = rb->bit_offset;
3049 pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
3050 if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
3051 pbi->tg_start =
3052 aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3053 pbi->tg_size =
3054 1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3055 }
3056#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003057}
3058
3059static int mem_get_varsize(const uint8_t *src, const int sz) {
3060 switch (sz) {
3061 case 1: return src[0];
3062 case 2: return mem_get_le16(src);
3063 case 3: return mem_get_le24(src);
3064 case 4: return mem_get_le32(src);
3065 default: assert("Invalid size" && 0); return -1;
3066 }
3067}
3068
3069#if CONFIG_EXT_TILE
3070// Reads the next tile returning its size and adjusting '*data' accordingly
3071// based on 'is_last'.
3072static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07003073 struct aom_internal_error_info *error_info,
3074 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003075 void *decrypt_state,
3076 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
3077 int tile_size_bytes, int col, int row) {
3078 size_t size;
3079
3080 size_t copy_size = 0;
3081 const uint8_t *copy_data = NULL;
3082
3083 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003084 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003085 "Truncated packet or corrupt tile length");
3086 if (decrypt_cb) {
3087 uint8_t be_data[4];
3088 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3089
3090 // Only read number of bytes in cm->tile_size_bytes.
3091 size = mem_get_varsize(be_data, tile_size_bytes);
3092 } else {
3093 size = mem_get_varsize(*data, tile_size_bytes);
3094 }
3095
3096 // The top bit indicates copy mode
3097 if ((size >> (tile_size_bytes * 8 - 1)) == 1) {
3098 // The remaining bits in the top byte signal the row offset
3099 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
3100
3101 // Currently, only use tiles in same column as reference tiles.
3102 copy_data = tile_buffers[row - offset][col].data;
3103 copy_size = tile_buffers[row - offset][col].size;
3104 size = 0;
3105 }
3106
3107 *data += tile_size_bytes;
3108
3109 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003110 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003111 "Truncated packet or corrupt tile size");
3112
3113 if (size > 0) {
3114 tile_buffers[row][col].data = *data;
3115 tile_buffers[row][col].size = size;
3116 } else {
3117 tile_buffers[row][col].data = copy_data;
3118 tile_buffers[row][col].size = copy_size;
3119 }
3120
3121 *data += size;
3122
3123 tile_buffers[row][col].raw_data_end = *data;
3124}
3125
3126static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003127 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003128 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003129 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003130 const int tile_cols = cm->tile_cols;
3131 const int tile_rows = cm->tile_rows;
3132 const int have_tiles = tile_cols * tile_rows > 1;
3133
3134 if (!have_tiles) {
3135 const uint32_t tile_size = data_end - data;
3136 tile_buffers[0][0].data = data;
3137 tile_buffers[0][0].size = tile_size;
3138 tile_buffers[0][0].raw_data_end = NULL;
3139 } else {
3140 // We locate only the tile buffers that are required, which are the ones
3141 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
3142 // need the last (bottom right) tile buffer, as we need to know where the
3143 // end of the compressed frame buffer is for proper superframe decoding.
3144
3145 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
3146 const uint8_t *const data_start = data;
3147
Yaowu Xuf883b422016-08-30 14:01:10 -07003148 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003149 const int single_row = pbi->dec_tile_row >= 0;
3150 const int tile_rows_start = single_row ? dec_tile_row : 0;
3151 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003152 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003153 const int single_col = pbi->dec_tile_col >= 0;
3154 const int tile_cols_start = single_col ? dec_tile_col : 0;
3155 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3156
3157 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
3158 const int tile_size_bytes = pbi->tile_size_bytes;
3159
3160 size_t tile_col_size;
3161 int r, c;
3162
3163 // Read tile column sizes for all columns (we need the last tile buffer)
3164 for (c = 0; c < tile_cols; ++c) {
3165 const int is_last = c == tile_cols - 1;
3166 if (!is_last) {
3167 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
3168 data += tile_col_size_bytes;
3169 tile_col_data_end[c] = data + tile_col_size;
3170 } else {
3171 tile_col_size = data_end - data;
3172 tile_col_data_end[c] = data_end;
3173 }
3174 data += tile_col_size;
3175 }
3176
3177 data = data_start;
3178
3179 // Read the required tile sizes.
3180 for (c = tile_cols_start; c < tile_cols_end; ++c) {
3181 const int is_last = c == tile_cols - 1;
3182
3183 if (c > 0) data = tile_col_data_end[c - 1];
3184
3185 if (!is_last) data += tile_col_size_bytes;
3186
3187 // Get the whole of the last column, otherwise stop at the required tile.
3188 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
3189 tile_buffers[r][c].col = c;
3190
3191 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3192 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3193 tile_size_bytes, c, r);
3194 }
3195 }
3196
3197 // If we have not read the last column, then read it to get the last tile.
3198 if (tile_cols_end != tile_cols) {
3199 c = tile_cols - 1;
3200
3201 data = tile_col_data_end[c - 1];
3202
3203 for (r = 0; r < tile_rows; ++r) {
3204 tile_buffers[r][c].col = c;
3205
3206 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3207 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3208 tile_size_bytes, c, r);
3209 }
3210 }
3211 }
3212}
3213#else
3214// Reads the next tile returning its size and adjusting '*data' accordingly
3215// based on 'is_last'.
3216static void get_tile_buffer(const uint8_t *const data_end,
3217 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07003218 struct aom_internal_error_info *error_info,
3219 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003220 void *decrypt_state, TileBufferDec *const buf) {
3221 size_t size;
3222
3223 if (!is_last) {
Yaowu Xu0a79a1b2017-02-17 13:04:54 -08003224 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003225 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003226 "Truncated packet or corrupt tile length");
3227
3228 if (decrypt_cb) {
3229 uint8_t be_data[4];
3230 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3231 size = mem_get_varsize(be_data, tile_size_bytes);
3232 } else {
3233 size = mem_get_varsize(*data, tile_size_bytes);
3234 }
3235 *data += tile_size_bytes;
3236
3237 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003238 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003239 "Truncated packet or corrupt tile size");
3240 } else {
3241 size = data_end - *data;
3242 }
3243
3244 buf->data = *data;
3245 buf->size = size;
3246
3247 *data += size;
3248}
3249
3250static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003251 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003252 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003253 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07003254#if CONFIG_TILE_GROUPS
3255 int r, c;
3256 const int tile_cols = cm->tile_cols;
3257 const int tile_rows = cm->tile_rows;
3258 int tc = 0;
3259 int first_tile_in_tg = 0;
3260 int hdr_offset;
3261 struct aom_read_bit_buffer rb_tg_hdr;
3262 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3263 const int num_tiles = tile_rows * tile_cols;
3264 const int num_bits = OD_ILOG(num_tiles) - 1;
3265 const int hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
3266 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
Fangwen Fu73126c02017-02-08 22:37:47 -08003267#if CONFIG_DEPENDENT_HORZTILES
3268 int tile_group_start_col = 0;
3269 int tile_group_start_row = 0;
3270#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003271
3272 for (r = 0; r < tile_rows; ++r) {
3273 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07003274 TileBufferDec *const buf = &tile_buffers[r][c];
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003275 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
Thomas Davies80188d12016-10-26 16:08:35 -07003276 hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
3277
3278 buf->col = c;
3279 if (hdr_offset) {
3280 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
3281 rb_tg_hdr.bit_offset = tg_size_bit_offset;
3282 if (num_tiles) {
3283 pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
3284 pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
Fangwen Fu73126c02017-02-08 22:37:47 -08003285#if CONFIG_DEPENDENT_HORZTILES
3286 tile_group_start_row = r;
3287 tile_group_start_col = c;
3288#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003289 }
3290 }
3291 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
3292 data += hdr_offset;
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003293 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last,
3294 &pbi->common.error, &data, pbi->decrypt_cb,
3295 pbi->decrypt_state, buf);
Fangwen Fu73126c02017-02-08 22:37:47 -08003296#if CONFIG_DEPENDENT_HORZTILES
3297 cm->tile_group_start_row[r][c] = tile_group_start_row;
3298 cm->tile_group_start_col[r][c] = tile_group_start_col;
3299#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003300 }
3301 }
3302#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003303 int r, c;
3304 const int tile_cols = cm->tile_cols;
3305 const int tile_rows = cm->tile_rows;
3306
3307 for (r = 0; r < tile_rows; ++r) {
3308 for (c = 0; c < tile_cols; ++c) {
3309 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
3310 TileBufferDec *const buf = &tile_buffers[r][c];
3311 buf->col = c;
3312 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
3313 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
3314 }
3315 }
Thomas Davies80188d12016-10-26 16:08:35 -07003316#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003317}
3318#endif // CONFIG_EXT_TILE
3319
Yushin Cho77bba8d2016-11-04 16:36:56 -07003320#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003321static void daala_dec_init(AV1_COMMON *const cm, daala_dec_ctx *daala_dec,
Nathan E. Eggeab083972016-12-28 15:31:46 -05003322 aom_reader *r) {
3323 daala_dec->r = r;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003324 od_adapt_ctx_reset(&daala_dec->state.adapt, 0);
3325
Yushin Cho70669122016-12-08 09:53:14 -10003326 // TODO(yushin) : activity masking info needs be signaled by a bitstream
3327 daala_dec->use_activity_masking = AV1_PVQ_ENABLE_ACTIVITY_MASKING;
3328
Yushin Cho7a428ba2017-01-12 16:28:49 -08003329#if !CONFIG_DAALA_DIST
3330 daala_dec->use_activity_masking = 0;
3331#endif
3332
Yushin Cho70669122016-12-08 09:53:14 -10003333 if (daala_dec->use_activity_masking)
3334 daala_dec->qm = OD_HVS_QM;
3335 else
3336 daala_dec->qm = OD_FLAT_QM;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003337
3338 od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
3339 daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
Yushin Cho70669122016-12-08 09:53:14 -10003340
3341 if (daala_dec->use_activity_masking) {
3342 int pli;
3343 int use_masking = daala_dec->use_activity_masking;
3344 int segment_id = 0;
3345 int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
3346
3347 for (pli = 0; pli < MAX_MB_PLANE; pli++) {
3348 int i;
3349 int q;
3350
3351 q = qindex;
3352 if (q <= OD_DEFAULT_QMS[use_masking][0][pli].interp_q << OD_COEFF_SHIFT) {
3353 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3354 &OD_DEFAULT_QMS[use_masking][0][pli], NULL);
3355 } else {
3356 i = 0;
3357 while (OD_DEFAULT_QMS[use_masking][i + 1][pli].qm_q4 != NULL &&
3358 q > OD_DEFAULT_QMS[use_masking][i + 1][pli].interp_q
3359 << OD_COEFF_SHIFT) {
3360 i++;
3361 }
3362 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3363 &OD_DEFAULT_QMS[use_masking][i][pli],
3364 &OD_DEFAULT_QMS[use_masking][i + 1][pli]);
3365 }
3366 }
3367 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07003368}
Yushin Cho70669122016-12-08 09:53:14 -10003369#endif // #if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003370
Yaowu Xuf883b422016-08-30 14:01:10 -07003371static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003372 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003373 AV1_COMMON *const cm = &pbi->common;
3374 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003375 const int tile_cols = cm->tile_cols;
3376 const int tile_rows = cm->tile_rows;
3377 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07003378 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003379#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003380 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003381 const int single_row = pbi->dec_tile_row >= 0;
3382 const int tile_rows_start = single_row ? dec_tile_row : 0;
3383 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003384 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003385 const int single_col = pbi->dec_tile_col >= 0;
3386 const int tile_cols_start = single_col ? dec_tile_col : 0;
3387 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3388 const int inv_col_order = pbi->inv_tile_order && !single_col;
3389 const int inv_row_order = pbi->inv_tile_order && !single_row;
3390#else
3391 const int tile_rows_start = 0;
3392 const int tile_rows_end = tile_rows;
3393 const int tile_cols_start = 0;
3394 const int tile_cols_end = tile_cols;
3395 const int inv_col_order = pbi->inv_tile_order;
3396 const int inv_row_order = pbi->inv_tile_order;
3397#endif // CONFIG_EXT_TILE
3398 int tile_row, tile_col;
3399
hui su0d103572017-03-01 17:58:01 -08003400#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003401 cm->do_subframe_update = n_tiles == 1;
hui su0d103572017-03-01 17:58:01 -08003402#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003403
3404 if (cm->lf.filter_level && !cm->skip_loop_filter &&
3405 pbi->lf_worker.data1 == NULL) {
3406 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07003407 aom_memalign(32, sizeof(LFWorkerData)));
3408 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003409 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003410 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003411 "Loop filter thread creation failed");
3412 }
3413 }
3414
3415 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3416 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3417 // Be sure to sync as we might be resuming after a failed frame decode.
3418 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003419 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
3420 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003421 }
3422
3423 assert(tile_rows <= MAX_TILE_ROWS);
3424 assert(tile_cols <= MAX_TILE_COLS);
3425
3426 get_tile_buffers(pbi, data, data_end, tile_buffers);
3427
3428 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003429 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003430 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003431 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003432 pbi->allocated_tiles = n_tiles;
3433 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003434#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003435 if (pbi->acct_enabled) {
3436 aom_accounting_reset(&pbi->accounting);
3437 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003438#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003439 // Load all tile information into tile_data.
3440 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3441 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3442 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
3443 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
3444
3445 td->cm = cm;
3446 td->xd = pbi->mb;
3447 td->xd.corrupted = 0;
3448 td->xd.counts =
3449 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3450 ? &cm->counts
3451 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003452 av1_zero(td->dqcoeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003453#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003454 av1_zero(td->pvq_ref_coeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003455#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003456 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003457 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003458 &td->bit_reader,
3459#if CONFIG_ANS && ANS_MAX_SYMBOLS
3460 1 << cm->ans_window_size_log2,
3461#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3462 pbi->decrypt_cb, pbi->decrypt_state);
Michael Bebenita6048d052016-08-25 14:40:54 -07003463#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003464 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003465 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003466 } else {
David Barkerd971f402016-10-25 13:52:07 +01003467 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003468 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003469#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003470 av1_init_macroblockd(cm, &td->xd,
3471#if CONFIG_PVQ
3472 td->pvq_ref_coeff,
3473#endif
3474 td->dqcoeff);
3475#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003476 daala_dec_init(cm, &td->xd.daala_dec, &td->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003477#endif
Thomas Daviesf77d4ad2017-01-10 18:55:42 +00003478#if CONFIG_EC_ADAPT
3479 // Initialise the tile context from the frame context
3480 td->tctx = *cm->fc;
3481 td->xd.tile_ctx = &td->tctx;
3482#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003483#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003484 td->xd.plane[0].color_index_map = td->color_index_map[0];
3485 td->xd.plane[1].color_index_map = td->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003486#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003487 }
3488 }
3489
3490 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3491 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
3492 int mi_row = 0;
3493 TileInfo tile_info;
3494
Yaowu Xuf883b422016-08-30 14:01:10 -07003495 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003496
3497 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3498 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
3499 TileData *const td = pbi->tile_data + tile_cols * row + col;
Michael Bebenita6048d052016-08-25 14:40:54 -07003500#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003501 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003502 td->bit_reader.accounting->last_tell_frac =
3503 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003504 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003505#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003506
Yaowu Xuf883b422016-08-30 14:01:10 -07003507 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003508
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003509#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003510#if CONFIG_TILE_GROUPS
3511 av1_tile_set_tg_boundary(&tile_info, cm, tile_row, tile_col);
3512 if (!cm->dependent_horz_tiles || tile_row == 0 ||
3513 tile_info.tg_horz_boundary) {
3514#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003515 if (!cm->dependent_horz_tiles || tile_row == 0) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003516#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003517 av1_zero_above_context(cm, tile_info.mi_col_start,
3518 tile_info.mi_col_end);
3519 }
3520#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003521 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003522#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003523
3524 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
3525 mi_row += cm->mib_size) {
3526 int mi_col;
3527
Yaowu Xuf883b422016-08-30 14:01:10 -07003528 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003529
3530 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
3531 mi_col += cm->mib_size) {
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003532 av1_update_boundary_info(cm, &tile_info, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003533 decode_partition(pbi, &td->xd,
3534#if CONFIG_SUPERTX
3535 0,
3536#endif // CONFIG_SUPERTX
3537 mi_row, mi_col, &td->bit_reader, cm->sb_size,
3538 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003539#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3540 detoken_and_recon_sb(pbi, &td->xd, mi_row, mi_col, &td->bit_reader,
3541 cm->sb_size);
3542#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003543 }
Angie Chiangd0916d92017-03-10 17:54:18 -08003544 aom_merge_corrupted_flag(&pbi->mb.corrupted, td->xd.corrupted);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003545 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003546 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003547 "Failed to decode tile data");
hui su0d103572017-03-01 17:58:01 -08003548#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003549 if (cm->do_subframe_update &&
3550 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
hui su0161a932017-01-24 14:12:11 -08003551 const int mi_rows_per_update =
3552 MI_SIZE * AOMMAX(cm->mi_rows / MI_SIZE / COEF_PROBS_BUFS, 1);
3553 if ((mi_row + MI_SIZE) % mi_rows_per_update == 0 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003554 mi_row + MI_SIZE < cm->mi_rows &&
3555 cm->coef_probs_update_idx < COEF_PROBS_BUFS - 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003556 av1_partial_adapt_probs(cm, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003557 ++cm->coef_probs_update_idx;
3558 }
3559 }
hui su0d103572017-03-01 17:58:01 -08003560#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003561 }
3562 }
3563
3564 assert(mi_row > 0);
3565
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003566// when Parallel deblocking is enabled, deblocking should not
3567// be interleaved with decoding. Instead, deblocking should be done
3568// after the entire frame is decoded.
3569#if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003570 // Loopfilter one tile row.
3571 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3572 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003573 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003574 const int lf_end = tile_info.mi_row_end - cm->mib_size;
3575
3576 // Delay the loopfilter if the first tile row is only
3577 // a single superblock high.
3578 if (lf_end <= 0) continue;
3579
3580 // Decoding has completed. Finish up the loop filter in this thread.
3581 if (tile_info.mi_row_end >= cm->mi_rows) continue;
3582
3583 winterface->sync(&pbi->lf_worker);
3584 lf_data->start = lf_start;
3585 lf_data->stop = lf_end;
3586 if (pbi->max_threads > 1) {
3587 winterface->launch(&pbi->lf_worker);
3588 } else {
3589 winterface->execute(&pbi->lf_worker);
3590 }
3591 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003592#endif // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003593
3594 // After loopfiltering, the last 7 row pixels in each superblock row may
3595 // still be changed by the longest loopfilter of the next superblock row.
3596 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003597 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003598 }
3599
3600#if CONFIG_VAR_TX
3601 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003602 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
3603 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003604#else
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003605#if CONFIG_PARALLEL_DEBLOCKING
3606 // Loopfilter all rows in the frame in the frame.
3607 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3608 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3609 winterface->sync(&pbi->lf_worker);
3610 lf_data->start = 0;
3611 lf_data->stop = cm->mi_rows;
3612 winterface->execute(&pbi->lf_worker);
3613 }
3614#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003615 // Loopfilter remaining rows in the frame.
3616 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3617 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3618 winterface->sync(&pbi->lf_worker);
3619 lf_data->start = lf_data->stop;
3620 lf_data->stop = cm->mi_rows;
3621 winterface->execute(&pbi->lf_worker);
3622 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003623#endif // CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003624#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003625 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003626 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003627
3628#if CONFIG_EXT_TILE
3629 if (n_tiles == 1) {
3630#if CONFIG_ANS
3631 return data_end;
3632#else
3633 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07003634 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003635#endif // CONFIG_ANS
3636 } else {
3637 // Return the end of the last tile buffer
3638 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3639 }
3640#else
3641#if CONFIG_ANS
3642 return data_end;
3643#else
3644 {
3645 // Get last tile data.
3646 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003647 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003648 }
3649#endif // CONFIG_ANS
3650#endif // CONFIG_EXT_TILE
3651}
3652
3653static int tile_worker_hook(TileWorkerData *const tile_data,
3654 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003655 AV1Decoder *const pbi = tile_data->pbi;
3656 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003657 int mi_row, mi_col;
3658
3659 if (setjmp(tile_data->error_info.jmp)) {
3660 tile_data->error_info.setjmp = 0;
Angie Chiangd0916d92017-03-10 17:54:18 -08003661 aom_merge_corrupted_flag(&tile_data->xd.corrupted, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003662 return 0;
3663 }
3664
3665 tile_data->error_info.setjmp = 1;
3666 tile_data->xd.error_info = &tile_data->error_info;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003667#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003668#if CONFIG_TILE_GROUPS
3669 if (!cm->dependent_horz_tiles || tile->tg_horz_boundary) {
3670#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003671 if (!cm->dependent_horz_tiles) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003672#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003673 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
3674 }
3675#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003676 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003677#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003678
3679 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
3680 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003681 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003682
3683 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3684 mi_col += cm->mib_size) {
3685 decode_partition(pbi, &tile_data->xd,
3686#if CONFIG_SUPERTX
3687 0,
3688#endif
3689 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
3690 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003691#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3692 detoken_and_recon_sb(pbi, &tile_data->xd, mi_row, mi_col,
3693 &tile_data->bit_reader, cm->sb_size);
3694#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003695 }
3696 }
3697 return !tile_data->xd.corrupted;
3698}
3699
3700// sorts in descending order
3701static int compare_tile_buffers(const void *a, const void *b) {
3702 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
3703 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
3704 return (int)(buf2->size - buf1->size);
3705}
3706
Yaowu Xuf883b422016-08-30 14:01:10 -07003707static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003708 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003709 AV1_COMMON *const cm = &pbi->common;
3710 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003711 const int tile_cols = cm->tile_cols;
3712 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003713 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
clang-format67948d32016-09-07 22:40:40 -07003714 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003715#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003716 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003717 const int single_row = pbi->dec_tile_row >= 0;
3718 const int tile_rows_start = single_row ? dec_tile_row : 0;
3719 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003720 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003721 const int single_col = pbi->dec_tile_col >= 0;
3722 const int tile_cols_start = single_col ? dec_tile_col : 0;
3723 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3724#else
3725 const int tile_rows_start = 0;
3726 const int tile_rows_end = tile_rows;
3727 const int tile_cols_start = 0;
3728 const int tile_cols_end = tile_cols;
3729#endif // CONFIG_EXT_TILE
3730 int tile_row, tile_col;
3731 int i;
3732
3733#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3734 int final_worker = -1;
3735#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3736
3737 assert(tile_rows <= MAX_TILE_ROWS);
3738 assert(tile_cols <= MAX_TILE_COLS);
3739
3740 assert(tile_cols * tile_rows > 1);
3741
Yaowu Xuc27fc142016-08-22 16:08:15 -07003742 // TODO(jzern): See if we can remove the restriction of passing in max
3743 // threads to the decoder.
3744 if (pbi->num_tile_workers == 0) {
3745 const int num_threads = pbi->max_threads & ~1;
3746 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07003747 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003748 // Ensure tile data offsets will be properly aligned. This may fail on
3749 // platforms without DECLARE_ALIGNED().
3750 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3751 CHECK_MEM_ERROR(
3752 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003753 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003754 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07003755 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003756 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003757 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003758 ++pbi->num_tile_workers;
3759
3760 winterface->init(worker);
3761 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003762 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003763 "Tile decoder thread creation failed");
3764 }
3765 }
3766 }
3767
3768 // Reset tile decoding hook
3769 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003770 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003771 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003772 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003773 worker->data1 = &pbi->tile_worker_data[i];
3774 worker->data2 = &pbi->tile_worker_info[i];
3775 }
3776
3777 // Initialize thread frame counts.
3778 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3779 for (i = 0; i < num_workers; ++i) {
3780 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003781 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003782 }
3783 }
3784
3785 // Load tile data into tile_buffers
3786 get_tile_buffers(pbi, data, data_end, tile_buffers);
3787
3788 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3789 // Sort the buffers in this tile row based on size in descending order.
3790 qsort(&tile_buffers[tile_row][tile_cols_start],
3791 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
3792 compare_tile_buffers);
3793
3794 // Rearrange the tile buffers in this tile row such that per-tile group
3795 // the largest, and presumably the most difficult tile will be decoded in
3796 // the main thread. This should help minimize the number of instances
3797 // where the main thread is waiting for a worker to complete.
3798 {
3799 int group_start;
3800 for (group_start = tile_cols_start; group_start < tile_cols_end;
3801 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003802 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003803 const TileBufferDec largest = tile_buffers[tile_row][group_start];
3804 memmove(&tile_buffers[tile_row][group_start],
3805 &tile_buffers[tile_row][group_start + 1],
3806 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
3807 tile_buffers[tile_row][group_end - 1] = largest;
3808 }
3809 }
3810
3811 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
3812 // Launch workers for individual columns
3813 for (i = 0; i < num_workers && tile_col < tile_cols_end;
3814 ++i, ++tile_col) {
3815 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07003816 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003817 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
3818 TileInfo *const tile_info = (TileInfo *)worker->data2;
3819
3820 twd->pbi = pbi;
3821 twd->xd = pbi->mb;
3822 twd->xd.corrupted = 0;
3823 twd->xd.counts =
3824 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3825 ? &twd->counts
3826 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003827 av1_zero(twd->dqcoeff);
3828 av1_tile_init(tile_info, cm, tile_row, buf->col);
3829 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003830 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003831 &twd->bit_reader,
3832#if CONFIG_ANS && ANS_MAX_SYMBOLS
3833 1 << cm->ans_window_size_log2,
3834#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3835 pbi->decrypt_cb, pbi->decrypt_state);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003836 av1_init_macroblockd(cm, &twd->xd,
3837#if CONFIG_PVQ
3838 twd->pvq_ref_coeff,
3839#endif
3840 twd->dqcoeff);
3841#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003842 daala_dec_init(cm, &twd->xd.daala_dec, &twd->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003843#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003844#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003845 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
3846 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003847#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003848
3849 worker->had_error = 0;
3850 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
3851 winterface->execute(worker);
3852 } else {
3853 winterface->launch(worker);
3854 }
3855
3856#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3857 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
3858 final_worker = i;
3859 }
3860#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3861 }
3862
3863 // Sync all workers
3864 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003865 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003866 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07003867 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07003868 // info in cm. Additionally once the threads have been synced and an
3869 // error is detected, there's no point in continuing to decode tiles.
3870 pbi->mb.corrupted |= !winterface->sync(worker);
3871 }
3872 }
3873 }
3874
3875 // Accumulate thread frame counts.
3876 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3877 for (i = 0; i < num_workers; ++i) {
3878 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003879 av1_accumulate_frame_counts(&cm->counts, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003880 }
3881 }
3882
3883#if CONFIG_EXT_TILE
3884 // Return the end of the last tile buffer
3885 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3886#else
3887#if CONFIG_ANS
3888 return data_end;
3889#else
3890 assert(final_worker != -1);
3891 {
3892 TileWorkerData *const twd =
3893 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003894 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003895 }
3896#endif // CONFIG_ANS
3897#endif // CONFIG_EXT_TILE
3898}
3899
3900static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003901 AV1_COMMON *const cm = (AV1_COMMON *)data;
3902 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003903}
3904
Yaowu Xuf883b422016-08-30 14:01:10 -07003905static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3906 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003907 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003908 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003909 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003910 cm->bit_depth = AOM_BITS_8;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003911 }
3912
Yaowu Xuf883b422016-08-30 14:01:10 -07003913#if CONFIG_AOM_HIGHBITDEPTH
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003914 if (cm->bit_depth > AOM_BITS_8) {
3915 cm->use_highbitdepth = 1;
3916 } else {
Yaowu Xu345a22d2017-02-27 09:23:52 -08003917#if CONFIG_LOWBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003918 cm->use_highbitdepth = 0;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003919#else
3920 cm->use_highbitdepth = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003921#endif
3922 }
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003923#endif
3924
Yaowu Xuf883b422016-08-30 14:01:10 -07003925 cm->color_space = aom_rb_read_literal(rb, 3);
3926 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003927 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07003928 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003929 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003930 cm->subsampling_x = aom_rb_read_bit(rb);
3931 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003932 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003933 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003934 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07003935 if (aom_rb_read_bit(rb))
3936 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003937 "Reserved bit set");
3938 } else {
3939 cm->subsampling_y = cm->subsampling_x = 1;
3940 }
3941 } else {
3942 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
3943 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
3944 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
3945 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003946 if (aom_rb_read_bit(rb))
3947 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003948 "Reserved bit set");
3949 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003950 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003951 "4:4:4 color not supported in profile 0 or 2");
3952 }
3953 }
3954}
3955
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003956#if CONFIG_REFERENCE_BUFFER
3957void read_sequence_header(SequenceHeader *seq_params) {
3958 /* Placeholder for actually reading from the bitstream */
3959 seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
3960 seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
3961 seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
3962}
3963#endif
3964
Yaowu Xuf883b422016-08-30 14:01:10 -07003965static size_t read_uncompressed_header(AV1Decoder *pbi,
3966 struct aom_read_bit_buffer *rb) {
3967 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003968 MACROBLOCKD *const xd = &pbi->mb;
3969 BufferPool *const pool = cm->buffer_pool;
3970 RefCntBuffer *const frame_bufs = pool->frame_bufs;
3971 int i, mask, ref_index = 0;
3972 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003973
3974#if CONFIG_REFERENCE_BUFFER
3975 /* TODO: Move outside frame loop or inside key-frame branch */
3976 read_sequence_header(&pbi->seq_params);
3977#endif
3978
Yaowu Xuc27fc142016-08-22 16:08:15 -07003979 cm->last_frame_type = cm->frame_type;
3980 cm->last_intra_only = cm->intra_only;
3981
3982#if CONFIG_EXT_REFS
3983 // NOTE: By default all coded frames to be used as a reference
3984 cm->is_reference_frame = 1;
3985#endif // CONFIG_EXT_REFS
3986
Yaowu Xuf883b422016-08-30 14:01:10 -07003987 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
3988 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003989 "Invalid frame marker");
3990
Yaowu Xuf883b422016-08-30 14:01:10 -07003991 cm->profile = av1_read_profile(rb);
3992#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003993 if (cm->profile >= MAX_PROFILES)
Yaowu Xuf883b422016-08-30 14:01:10 -07003994 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003995 "Unsupported bitstream profile");
3996#else
3997 if (cm->profile >= PROFILE_2)
Yaowu Xuf883b422016-08-30 14:01:10 -07003998 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003999 "Unsupported bitstream profile");
4000#endif
4001
Yaowu Xuf883b422016-08-30 14:01:10 -07004002 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004003
4004 if (cm->show_existing_frame) {
Yaowu Xu415ba932016-12-27 11:17:32 -08004005 // Show an existing frame directly.
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004006 const int existing_frame_idx = aom_rb_read_literal(rb, 3);
4007 const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
Yaowu Xu415ba932016-12-27 11:17:32 -08004008#if CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004009 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004010 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4011 int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004012 /* Compare display_frame_id with ref_frame_id and check valid for
4013 * referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004014 if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
4015 cm->valid_for_referencing[existing_frame_idx] == 0)
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004016 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4017 "Reference buffer frame ID mismatch");
4018 }
4019#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004020 lock_buffer_pool(pool);
4021 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
4022 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07004023 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004024 "Buffer %d does not contain a decoded frame",
4025 frame_to_show);
4026 }
4027 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
4028 unlock_buffer_pool(pool);
4029
4030 cm->lf.filter_level = 0;
4031 cm->show_frame = 1;
4032 pbi->refresh_frame_flags = 0;
4033
4034 if (cm->frame_parallel_decode) {
4035 for (i = 0; i < REF_FRAMES; ++i)
4036 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
4037 }
4038
4039 return 0;
4040 }
4041
Yaowu Xuf883b422016-08-30 14:01:10 -07004042 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
4043 cm->show_frame = aom_rb_read_bit(rb);
4044 cm->error_resilient_mode = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004045#if CONFIG_REFERENCE_BUFFER
4046 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004047 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4048 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4049 int prev_frame_id = 0;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004050 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004051 prev_frame_id = cm->current_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004052 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004053 cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004054
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004055 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004056 int diff_frame_id;
4057 if (cm->current_frame_id > prev_frame_id) {
4058 diff_frame_id = cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004059 } else {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004060 diff_frame_id =
4061 (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004062 }
4063 /* Check current_frame_id for conformance */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004064 if (prev_frame_id == cm->current_frame_id ||
4065 diff_frame_id >= (1 << (frame_id_length - 1))) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004066 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4067 "Invalid value of current_frame_id");
4068 }
4069 }
4070 /* Check if some frames need to be marked as not valid for referencing */
4071 for (i = 0; i < REF_FRAMES; i++) {
4072 if (cm->frame_type == KEY_FRAME) {
4073 cm->valid_for_referencing[i] = 0;
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004074 } else if (cm->current_frame_id - (1 << diff_len) > 0) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004075 if (cm->ref_frame_id[i] > cm->current_frame_id ||
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004076 cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004077 cm->valid_for_referencing[i] = 0;
4078 } else {
4079 if (cm->ref_frame_id[i] > cm->current_frame_id &&
4080 cm->ref_frame_id[i] <
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004081 (1 << frame_id_length) + cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004082 cm->valid_for_referencing[i] = 0;
4083 }
4084 }
4085 }
4086#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004087 if (cm->frame_type == KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004088 if (!av1_read_sync_code(rb))
4089 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004090 "Invalid frame sync code");
4091
4092 read_bitdepth_colorspace_sampling(cm, rb);
4093 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
4094
4095 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4096 cm->frame_refs[i].idx = INVALID_IDX;
4097 cm->frame_refs[i].buf = NULL;
4098 }
4099
4100 setup_frame_size(cm, rb);
4101 if (pbi->need_resync) {
4102 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4103 pbi->need_resync = 0;
4104 }
Alex Converseeb780e72016-12-13 12:46:41 -08004105#if CONFIG_ANS && ANS_MAX_SYMBOLS
4106 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4107#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
Urvang Joshib100db72016-10-12 16:28:56 -07004108#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004109 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004110#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004111 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004112 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004113#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004114 if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004115#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004116 if (cm->error_resilient_mode) {
4117 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
4118 } else {
4119 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004120 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004121 ? RESET_FRAME_CONTEXT_ALL
4122 : RESET_FRAME_CONTEXT_CURRENT;
4123 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004124 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004125 ? RESET_FRAME_CONTEXT_CURRENT
4126 : RESET_FRAME_CONTEXT_NONE;
4127 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07004128 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004129 ? RESET_FRAME_CONTEXT_ALL
4130 : RESET_FRAME_CONTEXT_CURRENT;
4131 }
4132 }
4133
4134 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004135 if (!av1_read_sync_code(rb))
4136 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004137 "Invalid frame sync code");
4138
4139 read_bitdepth_colorspace_sampling(cm, rb);
4140
Yaowu Xuf883b422016-08-30 14:01:10 -07004141 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004142 setup_frame_size(cm, rb);
4143 if (pbi->need_resync) {
4144 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4145 pbi->need_resync = 0;
4146 }
Alex Converseeb780e72016-12-13 12:46:41 -08004147#if CONFIG_ANS && ANS_MAX_SYMBOLS
4148 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4149#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004150 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Yaowu Xuf883b422016-08-30 14:01:10 -07004151 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004152
4153#if CONFIG_EXT_REFS
4154 if (!pbi->refresh_frame_flags) {
4155 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
4156 // will not be used as a reference
4157 cm->is_reference_frame = 0;
4158 }
4159#endif // CONFIG_EXT_REFS
4160
4161 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004162 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004163 const int idx = cm->ref_frame_map[ref];
4164 RefBuffer *const ref_frame = &cm->frame_refs[i];
4165 ref_frame->idx = idx;
4166 ref_frame->buf = &frame_bufs[idx].buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07004167 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004168#if CONFIG_REFERENCE_BUFFER
4169 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004170 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4171 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4172 int delta_frame_id_minus1 = aom_rb_read_literal(rb, diff_len);
4173 int ref_frame_id =
4174 ((cm->current_frame_id - (delta_frame_id_minus1 + 1) +
4175 (1 << frame_id_length)) %
4176 (1 << frame_id_length));
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004177 /* Compare values derived from delta_frame_id_minus1 and
4178 * refresh_frame_flags. Also, check valid for referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004179 if (ref_frame_id != cm->ref_frame_id[ref] ||
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004180 cm->valid_for_referencing[ref] == 0)
4181 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4182 "Reference buffer frame ID mismatch");
4183 }
4184#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004185 }
4186
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004187#if CONFIG_FRAME_SIZE
4188 if (cm->error_resilient_mode == 0) {
4189 setup_frame_size_with_refs(cm, rb);
4190 } else {
4191 setup_frame_size(cm, rb);
4192 }
4193#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004194 setup_frame_size_with_refs(cm, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004195#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004196
Yaowu Xuf883b422016-08-30 14:01:10 -07004197 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Angie Chiang5678ad92016-11-21 09:38:40 -08004198 cm->interp_filter = read_frame_interp_filter(rb);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004199#if CONFIG_TEMPMV_SIGNALING
4200 if (!cm->error_resilient_mode) {
4201 cm->use_prev_frame_mvs = aom_rb_read_bit(rb);
4202 }
4203#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004204 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4205 RefBuffer *const ref_buf = &cm->frame_refs[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07004206#if CONFIG_AOM_HIGHBITDEPTH
4207 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004208 &ref_buf->sf, ref_buf->buf->y_crop_width,
4209 ref_buf->buf->y_crop_height, cm->width, cm->height,
4210 cm->use_highbitdepth);
4211#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004212 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004213 &ref_buf->sf, ref_buf->buf->y_crop_width,
4214 ref_buf->buf->y_crop_height, cm->width, cm->height);
4215#endif
4216 }
4217 }
4218 }
Fangwen Fu8d164de2016-12-14 13:40:54 -08004219#if CONFIG_TEMPMV_SIGNALING
4220 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
4221#endif
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004222
4223#if CONFIG_REFERENCE_BUFFER
4224 if (pbi->seq_params.frame_id_numbers_present_flag) {
4225 /* If bitmask is set, update reference frame id values and
4226 mark frames as valid for reference */
4227 int refresh_frame_flags =
4228 cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
4229 for (i = 0; i < REF_FRAMES; i++) {
4230 if ((refresh_frame_flags >> i) & 1) {
4231 cm->ref_frame_id[i] = cm->current_frame_id;
4232 cm->valid_for_referencing[i] = 1;
4233 }
4234 }
4235 }
4236#endif
4237
Yaowu Xuf883b422016-08-30 14:01:10 -07004238#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004239 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
4240#endif
4241 get_frame_new_buffer(cm)->color_space = cm->color_space;
4242 get_frame_new_buffer(cm)->color_range = cm->color_range;
4243 get_frame_new_buffer(cm)->render_width = cm->render_width;
4244 get_frame_new_buffer(cm)->render_height = cm->render_height;
4245
4246 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004247 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004248 "Keyframe / intra-only frame required to reset decoder"
4249 " state");
4250 }
4251
4252 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004253 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004254 ? REFRESH_FRAME_CONTEXT_FORWARD
4255 : REFRESH_FRAME_CONTEXT_BACKWARD;
4256 } else {
4257 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
4258 }
4259
Yaowu Xuf883b422016-08-30 14:01:10 -07004260 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07004261 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07004262 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004263
4264 // Generate next_ref_frame_map.
4265 lock_buffer_pool(pool);
4266 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
4267 if (mask & 1) {
4268 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
4269 ++frame_bufs[cm->new_fb_idx].ref_count;
4270 } else {
4271 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4272 }
4273 // Current thread holds the reference frame.
4274 if (cm->ref_frame_map[ref_index] >= 0)
4275 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4276 ++ref_index;
4277 }
4278
4279 for (; ref_index < REF_FRAMES; ++ref_index) {
4280 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4281
4282 // Current thread holds the reference frame.
4283 if (cm->ref_frame_map[ref_index] >= 0)
4284 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4285 }
4286 unlock_buffer_pool(pool);
4287 pbi->hold_ref_buf = 1;
4288
4289 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07004290 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004291
4292#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07004293 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004294#else
4295 set_sb_size(cm, BLOCK_64X64);
4296#endif // CONFIG_EXT_PARTITION
4297
4298 setup_loopfilter(cm, rb);
Jean-Marc Valin01435132017-02-18 14:12:53 -05004299#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01004300 setup_cdef(cm, rb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004301#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004302#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004303 decode_restoration_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004304#endif // CONFIG_LOOP_RESTORATION
4305 setup_quantization(cm, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004306#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004307 xd->bd = (int)cm->bit_depth;
4308#endif
4309
hui su0d103572017-03-01 17:58:01 -08004310#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuf883b422016-08-30 14:01:10 -07004311 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004312 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4313 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4314 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4315 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
4316 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4317 }
hui su0d103572017-03-01 17:58:01 -08004318#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004319
4320 setup_segmentation(cm, rb);
4321
Arild Fuldseth07441162016-08-15 15:07:52 +02004322#if CONFIG_DELTA_Q
4323 {
4324 struct segmentation *const seg = &cm->seg;
4325 int segment_quantizer_active = 0;
4326 for (i = 0; i < MAX_SEGMENTS; i++) {
4327 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
4328 segment_quantizer_active = 1;
4329 }
4330 }
4331
Thomas Daviesf6936102016-09-05 16:51:31 +01004332 cm->delta_q_res = 1;
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01004333 if (segment_quantizer_active == 0 && cm->base_qindex > 0) {
Arild Fuldseth07441162016-08-15 15:07:52 +02004334 cm->delta_q_present_flag = aom_rb_read_bit(rb);
4335 } else {
4336 cm->delta_q_present_flag = 0;
4337 }
4338 if (cm->delta_q_present_flag) {
4339 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01004340 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Arild Fuldseth07441162016-08-15 15:07:52 +02004341 }
4342 }
4343#endif
4344
Urvang Joshi454280d2016-10-14 16:51:44 -07004345 for (i = 0; i < MAX_SEGMENTS; ++i) {
4346 const int qindex = cm->seg.enabled
4347 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
4348 : cm->base_qindex;
4349 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
4350 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4351 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004352 }
4353
4354 setup_segmentation_dequant(cm);
Yue Cheneeacc4c2017-01-17 17:29:17 -08004355 cm->tx_mode = read_tx_mode(cm, xd, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004356 cm->reference_mode = read_frame_reference_mode(cm, rb);
4357
Sarah Parkere68a3e42017-02-16 14:03:24 -08004358#if CONFIG_EXT_TX
4359 cm->reduced_tx_set_used = aom_rb_read_bit(rb);
4360#endif // CONFIG_EXT_TX
4361
Yaowu Xuc27fc142016-08-22 16:08:15 -07004362 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004363 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004364
4365 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07004366 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004367 "Invalid header size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004368 return sz;
4369}
4370
4371#if CONFIG_EXT_TX
Thomas9ac55082016-09-23 18:04:17 +01004372#if !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuf883b422016-08-30 14:01:10 -07004373static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004374 int i, j, k;
4375 int s;
4376 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004377 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004378 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4379 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004380 for (j = 0; j < num_ext_tx_set[ext_tx_set_type_inter[s]] - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004381 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004382 }
4383 }
4384 }
4385
4386 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004387 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004388 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4389 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
4390 for (j = 0; j < INTRA_MODES; ++j)
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004391 for (k = 0; k < num_ext_tx_set[ext_tx_set_type_intra[s]] - 1; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004392 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
4393 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004394 }
4395 }
4396 }
4397}
Thomas9ac55082016-09-23 18:04:17 +01004398#endif // !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004399#else
4400
Yaowu Xuc27fc142016-08-22 16:08:15 -07004401#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004402#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07004403static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004404 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -07004405 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004406 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
Jingning Hanfeb517c2016-12-21 16:02:07 -08004407 for (j = TX_8X8; j < TX_SIZES; ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004408 av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004409 }
4410 }
4411 }
4412}
4413#endif // CONFIG_SUPERTX
4414
4415#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +00004416static void read_global_motion_params(WarpedMotionParams *params,
Yaowu Xuf883b422016-08-30 14:01:10 -07004417 aom_prob *probs, aom_reader *r) {
David Barkercf3d0b02016-11-10 10:14:49 +00004418 TransformationType type =
Michael Bebenita6048d052016-08-25 14:40:54 -07004419 aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004420 set_default_gmparams(params);
David Barkercf3d0b02016-11-10 10:14:49 +00004421 params->wmtype = type;
4422 switch (type) {
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004423 case HOMOGRAPHY:
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004424 case HORTRAPEZOID:
4425 case VERTRAPEZOID:
4426 if (type != HORTRAPEZOID)
4427 params->wmmat[6] =
4428 aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
4429 GM_ROW3HOMO_DECODE_FACTOR;
4430 if (type != VERTRAPEZOID)
4431 params->wmmat[7] =
4432 aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
4433 GM_ROW3HOMO_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004434 case AFFINE:
4435 case ROTZOOM:
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004436 params->wmmat[2] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004437 GM_ALPHA_DECODE_FACTOR +
David Barkercf3d0b02016-11-10 10:14:49 +00004438 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004439 if (type != VERTRAPEZOID)
4440 params->wmmat[3] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004441 GM_ALPHA_DECODE_FACTOR;
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004442 if (type >= AFFINE) {
4443 if (type != HORTRAPEZOID)
4444 params->wmmat[4] =
4445 aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4446 GM_ALPHA_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004447 params->wmmat[5] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4448 GM_ALPHA_DECODE_FACTOR +
4449 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004450 } else {
David Barkercf3d0b02016-11-10 10:14:49 +00004451 params->wmmat[4] = -params->wmmat[3];
4452 params->wmmat[5] = params->wmmat[2];
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004453 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004454 // fallthrough intended
David Barkercf3d0b02016-11-10 10:14:49 +00004455 case TRANSLATION:
4456 params->wmmat[0] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4457 GM_TRANS_DECODE_FACTOR;
4458 params->wmmat[1] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4459 GM_TRANS_DECODE_FACTOR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004460 break;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004461 case IDENTITY: break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004462 default: assert(0);
4463 }
4464}
4465
Yaowu Xuf883b422016-08-30 14:01:10 -07004466static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004467 int frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004468 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
4469 read_global_motion_params(&cm->global_motion[frame],
4470 cm->fc->global_motion_types_prob, r);
Sarah Parkere5299862016-08-16 14:57:37 -07004471 /*
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004472 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4473 frame, cm->current_video_frame, cm->show_frame,
David Barkercf3d0b02016-11-10 10:14:49 +00004474 cm->global_motion[frame].wmmat[0],
4475 cm->global_motion[frame].wmmat[1],
4476 cm->global_motion[frame].wmmat[2],
4477 cm->global_motion[frame].wmmat[3]);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004478 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004479 }
4480}
4481#endif // CONFIG_GLOBAL_MOTION
4482
Yaowu Xuf883b422016-08-30 14:01:10 -07004483static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004484 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004485 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004486#if CONFIG_SUPERTX
4487 MACROBLOCKD *const xd = &pbi->mb;
4488#endif
4489 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07004490 aom_reader r;
Yaowu Xu8af861b2016-11-01 12:12:11 -07004491 int k, i;
hui sub4e25d22017-03-09 15:32:30 -08004492#if !CONFIG_EC_ADAPT
Yaowu Xu8af861b2016-11-01 12:12:11 -07004493 int j;
4494#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004495
Alex Converse2cdf0d82016-12-13 13:53:09 -08004496#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08004497 r.window_size = 1 << cm->ans_window_size_log2;
Alex Converse2cdf0d82016-12-13 13:53:09 -08004498#endif
Alex Converse346440b2017-01-03 13:47:37 -08004499 if (aom_reader_init(&r, data, partition_size, pbi->decrypt_cb,
4500 pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07004501 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004502 "Failed to allocate bool decoder 0");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004503
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004504#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004505 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4506 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4507 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4508 av1_alloc_restoration_buffers(cm);
4509 decode_restoration(cm, &r);
4510 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004511#endif
4512
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004513#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -07004514 if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004515#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004516
Angie Chiang800df032017-03-22 11:14:12 -07004517#if CONFIG_LV_MAP
4518 av1_read_txb_probs(fc, cm->tx_mode, &r);
4519#else // CONFIG_LV_MAP
Yushin Cho77bba8d2016-11-04 16:36:56 -07004520#if !CONFIG_PVQ
Alex Conversea9598cd2017-02-03 14:18:05 -08004521#if !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004522 read_coef_probs(fc, cm->tx_mode, &r);
Angie Chiang800df032017-03-22 11:14:12 -07004523#endif // !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Angie Chiang7d7ead92017-03-22 10:35:51 -07004524#endif // !CONFIG_PVQ
Angie Chiang800df032017-03-22 11:14:12 -07004525#endif // CONFIG_LV_MAP
4526
Yaowu Xuc27fc142016-08-22 16:08:15 -07004527#if CONFIG_VAR_TX
4528 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004529 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
Yushin Cho77bba8d2016-11-04 16:36:56 -07004530#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004531 for (k = 0; k < SKIP_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004532 av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004533
Thomas Daviesd6ee8a82017-03-02 14:42:50 +00004534#if CONFIG_DELTA_Q && !CONFIG_EC_ADAPT
Thomas Davies665cd702017-03-02 10:20:30 +00004535 for (k = 0; k < DELTA_Q_PROBS; ++k)
Thomas Daviesf6936102016-09-05 16:51:31 +01004536 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
4537#endif
4538
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004539#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004540 if (cm->seg.enabled && cm->seg.update_map) {
4541 if (cm->seg.temporal_update) {
4542 for (k = 0; k < PREDICTION_PROBS; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004543 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004544 }
4545 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004546 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004547 }
4548
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004549 for (j = 0; j < INTRA_MODES; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004550 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004551 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004552 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004553
4554#if CONFIG_EXT_PARTITION_TYPES
Alex Converse4e18d402017-03-14 15:36:38 -07004555 for (j = 0; j < PARTITION_PLOFFSET; ++j)
4556 for (i = 0; i < PARTITION_TYPES - 1; ++i)
4557 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
4558 for (; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004559 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004560 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004561#else
Alex Converse55c6bde2017-01-12 15:55:31 -08004562 for (j = 0; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004563 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004564 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004565#endif // CONFIG_EXT_PARTITION_TYPES
hui su9aa97492017-01-26 16:46:01 -08004566
Alex Converse55c6bde2017-01-12 15:55:31 -08004567#if CONFIG_UNPOISON_PARTITION_CTX
4568 for (; j < PARTITION_CONTEXTS_PRIMARY + PARTITION_BLOCK_SIZES; ++j)
4569 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_VERT], ACCT_STR);
4570 for (; j < PARTITION_CONTEXTS_PRIMARY + 2 * PARTITION_BLOCK_SIZES; ++j)
4571 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_HORZ], ACCT_STR);
4572#endif // CONFIG_UNPOISON_PARTITION_CTX
hui su9aa97492017-01-26 16:46:01 -08004573
4574#if CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004575 for (i = 0; i < INTRA_FILTERS + 1; ++i)
4576 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004577 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
hui su9aa97492017-01-26 16:46:01 -08004578#endif // CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
hui sub4e25d22017-03-09 15:32:30 -08004579#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004580
4581 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004582 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Nathan E. Egge10ba2be2016-11-16 09:44:26 -05004583#if CONFIG_EC_MULTISYMBOL
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00004584 av1_copy(cm->fc->kf_y_cdf, av1_kf_y_mode_cdf);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004585#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004586#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004587 for (k = 0; k < INTRA_MODES; k++)
Thomas Davies6519beb2016-10-19 14:46:07 +01004588 for (j = 0; j < INTRA_MODES; j++)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004589 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004590 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004591#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004592 } else {
4593#if !CONFIG_REF_MV
4594 nmv_context *const nmvc = &fc->nmvc;
4595#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004596 read_inter_mode_probs(fc, &r);
4597
4598#if CONFIG_EXT_INTER
4599 read_inter_compound_mode_probs(fc, &r);
4600 if (cm->reference_mode != COMPOUND_REFERENCE) {
4601 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4602 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004603 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004604 }
4605 }
4606 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4607 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004608 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004609 }
4610 for (i = 0; i < BLOCK_SIZES; i++) {
4611 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004612 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004613 }
4614 }
4615 }
4616 if (cm->reference_mode != SINGLE_REFERENCE) {
4617 for (i = 0; i < BLOCK_SIZES; i++) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08004618 for (j = 0; j < COMPOUND_TYPES - 1; j++) {
4619 av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004620 }
4621 }
4622 }
4623#endif // CONFIG_EXT_INTER
4624
Yue Chencb60b182016-10-13 15:18:22 -07004625#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004626 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
Yue Chencb60b182016-10-13 15:18:22 -07004627 for (j = 0; j < MOTION_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004628 av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004629 }
Yue Chencb60b182016-10-13 15:18:22 -07004630#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004631
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004632#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004633 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004634#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004635
4636 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004637 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004638
4639 if (cm->reference_mode != SINGLE_REFERENCE)
4640 setup_compound_reference_mode(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004641 read_frame_reference_mode_probs(cm, &r);
4642
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004643#if !CONFIG_EC_ADAPT
Nathan E. Egge5710c722016-09-08 10:01:16 -04004644 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004645 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004646 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
Nathan E. Egge5710c722016-09-08 10:01:16 -04004647 }
Thomas9ac55082016-09-23 18:04:17 +01004648#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004649
4650#if CONFIG_REF_MV
4651 for (i = 0; i < NMV_CONTEXTS; ++i)
4652 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
4653#else
4654 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
4655#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004656#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004657 read_ext_tx_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004658#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004659#if CONFIG_SUPERTX
4660 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
4661#endif
4662#if CONFIG_GLOBAL_MOTION
4663 read_global_motion(cm, &r);
Thomas Davies6519beb2016-10-19 14:46:07 +01004664#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004665 }
Thomas Davies028b57f2017-02-22 16:42:11 +00004666#if CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Thomas Davies87aeeb82017-02-17 00:19:40 +00004667#if CONFIG_NEW_TOKENSET
4668 av1_coef_head_cdfs(fc);
4669#endif
4670 /* Make tail distribution from head */
Thomas Davies6519beb2016-10-19 14:46:07 +01004671 av1_coef_pareto_cdfs(fc);
David Barker599dfd02016-11-10 13:20:12 +00004672#if CONFIG_REF_MV
4673 for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
4674#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004675 av1_set_mv_cdfs(&fc->nmvc);
David Barker599dfd02016-11-10 13:20:12 +00004676#endif
Thomas Davies6519beb2016-10-19 14:46:07 +01004677 av1_set_mode_cdfs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00004678#endif // CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004679
Yaowu Xuf883b422016-08-30 14:01:10 -07004680 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004681}
4682
4683#ifdef NDEBUG
4684#define debug_check_frame_counts(cm) (void)0
4685#else // !NDEBUG
4686// Counts should only be incremented when frame_parallel_decoding_mode and
4687// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07004688static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004689 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07004690 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004691 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
4692 cm->error_resilient_mode);
4693 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
4694 sizeof(cm->counts.y_mode)));
4695 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
4696 sizeof(cm->counts.uv_mode)));
4697 assert(!memcmp(cm->counts.partition, zero_counts.partition,
4698 sizeof(cm->counts.partition)));
4699 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
4700 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
4701 sizeof(cm->counts.eob_branch)));
Thomas Daviesab780672017-02-01 12:07:29 +00004702#if CONFIG_EC_MULTISYMBOL
4703 assert(!memcmp(cm->counts.blockz_count, zero_counts.blockz_count,
4704 sizeof(cm->counts.blockz_count)));
4705#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004706 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
4707 sizeof(cm->counts.switchable_interp)));
4708 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
4709 sizeof(cm->counts.inter_mode)));
4710#if CONFIG_EXT_INTER
4711 assert(!memcmp(cm->counts.inter_compound_mode,
4712 zero_counts.inter_compound_mode,
4713 sizeof(cm->counts.inter_compound_mode)));
4714 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
4715 sizeof(cm->counts.interintra)));
4716 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
4717 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08004718 assert(!memcmp(cm->counts.compound_interinter,
4719 zero_counts.compound_interinter,
4720 sizeof(cm->counts.compound_interinter)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004721#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07004722#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
4723 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
4724 sizeof(cm->counts.motion_mode)));
4725#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004726 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
4727 sizeof(cm->counts.intra_inter)));
4728 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
4729 sizeof(cm->counts.comp_inter)));
4730 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
4731 sizeof(cm->counts.single_ref)));
4732 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
4733 sizeof(cm->counts.comp_ref)));
4734#if CONFIG_EXT_REFS
4735 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
4736 sizeof(cm->counts.comp_bwdref)));
4737#endif // CONFIG_EXT_REFS
4738 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
4739 sizeof(cm->counts.tx_size)));
4740 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
4741#if CONFIG_REF_MV
4742 assert(
4743 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
4744 assert(
4745 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
4746#else
4747 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
4748#endif
4749 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
4750 sizeof(cm->counts.inter_ext_tx)));
4751 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
4752 sizeof(cm->counts.intra_ext_tx)));
4753}
4754#endif // NDEBUG
4755
Yaowu Xuf883b422016-08-30 14:01:10 -07004756static struct aom_read_bit_buffer *init_read_bit_buffer(
4757 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
4758 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004759 rb->bit_offset = 0;
4760 rb->error_handler = error_handler;
4761 rb->error_handler_data = &pbi->common;
4762 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004763 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004764 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
4765 rb->bit_buffer = clear_data;
4766 rb->bit_buffer_end = clear_data + n;
4767 } else {
4768 rb->bit_buffer = data;
4769 rb->bit_buffer_end = data_end;
4770 }
4771 return rb;
4772}
4773
4774//------------------------------------------------------------------------------
4775
Yaowu Xuf883b422016-08-30 14:01:10 -07004776int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
4777 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
4778 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
4779 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004780}
4781
Yaowu Xuf883b422016-08-30 14:01:10 -07004782void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
4783 int *height) {
4784 *width = aom_rb_read_literal(rb, 16) + 1;
4785 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004786}
4787
Yaowu Xuf883b422016-08-30 14:01:10 -07004788BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
4789 int profile = aom_rb_read_bit(rb);
4790 profile |= aom_rb_read_bit(rb) << 1;
4791 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004792 return (BITSTREAM_PROFILE)profile;
4793}
4794
Thomas Davies028b57f2017-02-22 16:42:11 +00004795#if CONFIG_EC_ADAPT
4796static void make_update_tile_list_dec(AV1Decoder *pbi, const int tile_rows,
4797 const int tile_cols,
4798 FRAME_CONTEXT *ec_ctxs[]) {
4799 int i;
4800 for (i = 0; i < tile_rows * tile_cols; ++i)
4801 ec_ctxs[i] = &pbi->tile_data[i].tctx;
4802}
4803#endif
4804
Yaowu Xuf883b422016-08-30 14:01:10 -07004805void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
4806 const uint8_t *data_end, const uint8_t **p_data_end) {
4807 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004808 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07004809 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004810 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004811 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004812 size_t first_partition_size;
4813 YV12_BUFFER_CONFIG *new_fb;
4814
4815#if CONFIG_BITSTREAM_DEBUG
4816 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
4817#endif
4818
4819 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004820 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Thomas Davies72712e62016-11-09 12:17:51 +00004821#if CONFIG_TILE_GROUPS
4822 pbi->first_partition_size = first_partition_size;
4823 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
4824#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004825 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004826 xd->cur_buf = new_fb;
4827#if CONFIG_GLOBAL_MOTION
4828 xd->global_motion = cm->global_motion;
4829#endif // CONFIG_GLOBAL_MOTION
4830
4831 if (!first_partition_size) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004832 // showing a frame directly
4833 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004834 return;
4835 }
4836
Yaowu Xuf883b422016-08-30 14:01:10 -07004837 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004838 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07004839 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004840 "Truncated packet or corrupt header length");
4841
Jingning Han24e0a182016-11-20 22:34:12 -08004842#if CONFIG_REF_MV
Dengca8d24d2016-10-17 14:06:35 +08004843 cm->setup_mi(cm);
4844#endif
4845
Fangwen Fu8d164de2016-12-14 13:40:54 -08004846#if CONFIG_TEMPMV_SIGNALING
4847 if (cm->use_prev_frame_mvs) {
4848 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4849 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4850 assert(!cm->error_resilient_mode &&
4851 cm->width == last_fb_ref_buf->buf->y_width &&
4852 cm->height == last_fb_ref_buf->buf->y_height &&
4853 !cm->prev_frame->intra_only);
4854 }
4855#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004856 cm->use_prev_frame_mvs =
4857 !cm->error_resilient_mode && cm->width == cm->last_width &&
4858 cm->height == cm->last_height && !cm->last_intra_only &&
4859 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004860#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004861#if CONFIG_EXT_REFS
4862 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
4863 // show_exisiting_frame=1, nor can it take a frame not used as
4864 // a reference, it is probable that by the time it is being
4865 // referred to, the frame buffer it originally points to may
4866 // already get expired and have been reassigned to the current
4867 // newly coded frame. Hence, we need to check whether this is
4868 // the case, and if yes, we have 2 choices:
4869 // (1) Simply disable the use of previous frame mvs; or
4870 // (2) Have cm->prev_frame point to one reference frame buffer,
4871 // e.g. LAST_FRAME.
4872 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
4873 // Reassign the LAST_FRAME buffer to cm->prev_frame.
4874 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4875 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4876 }
4877#endif // CONFIG_EXT_REFS
4878
Yaowu Xuf883b422016-08-30 14:01:10 -07004879 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004880
4881 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
4882 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07004883 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004884 "Uninitialized entropy context.");
4885
Yaowu Xuf883b422016-08-30 14:01:10 -07004886 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004887
4888 xd->corrupted = 0;
4889 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
4890 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07004891 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004892 "Decode failed. Frame data header is corrupted.");
4893
4894 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004895 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004896 }
4897
4898 // If encoded in frame parallel mode, frame context is ready after decoding
4899 // the frame header.
4900 if (cm->frame_parallel_decode &&
4901 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004902 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004903 FrameWorkerData *const frame_worker_data = worker->data1;
4904 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
4905 context_updated = 1;
4906 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4907 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004908 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004909 pbi->cur_buf->row = -1;
4910 pbi->cur_buf->col = -1;
4911 frame_worker_data->frame_context_ready = 1;
4912 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07004913 av1_frameworker_signal_stats(worker);
4914 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004915 }
4916
hui su0d103572017-03-01 17:58:01 -08004917#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07004918 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004919 cm->coef_probs_update_idx = 0;
hui su0d103572017-03-01 17:58:01 -08004920#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004921
4922 if (pbi->max_threads > 1
4923#if CONFIG_EXT_TILE
4924 && pbi->dec_tile_col < 0 // Decoding all columns
4925#endif // CONFIG_EXT_TILE
4926 && cm->tile_cols > 1) {
4927 // Multi-threaded tile decoder
4928 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
4929 if (!xd->corrupted) {
4930 if (!cm->skip_loop_filter) {
4931 // If multiple threads are used to decode tiles, then we use those
4932 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07004933 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
4934 0, 0, pbi->tile_workers, pbi->num_tile_workers,
4935 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004936 }
4937 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004938 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004939 "Decode failed. Frame data is corrupted.");
4940 }
4941 } else {
4942 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
4943 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004944
Jean-Marc Valin01435132017-02-18 14:12:53 -05004945#if CONFIG_CDEF
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04004946 if (!cm->skip_loop_filter) {
Jean-Marc Valine9f77422017-03-22 17:09:51 -04004947 av1_cdef_frame(&pbi->cur_buf->buf, cm, &pbi->mb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004948 }
Debargha Mukherjee00c54332017-03-03 15:44:17 -08004949#endif // CONFIG_CDEF
4950
4951#if CONFIG_LOOP_RESTORATION
4952 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4953 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4954 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4955 av1_loop_restoration_frame(new_fb, cm, cm->rst_info, 7, 0, NULL);
4956 }
4957#endif // CONFIG_LOOP_RESTORATION
Thomas Daedef56859f2016-04-19 16:57:24 -07004958
Yaowu Xuc27fc142016-08-22 16:08:15 -07004959 if (!xd->corrupted) {
4960 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Thomas Davies028b57f2017-02-22 16:42:11 +00004961#if CONFIG_EC_ADAPT
4962 FRAME_CONTEXT *tile_ctxs[MAX_TILE_ROWS * MAX_TILE_COLS];
4963 make_update_tile_list_dec(pbi, cm->tile_rows, cm->tile_cols, tile_ctxs);
4964#endif
4965
hui su0d103572017-03-01 17:58:01 -08004966#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004967 cm->partial_prob_update = 0;
hui su0d103572017-03-01 17:58:01 -08004968#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07004969 av1_adapt_coef_probs(cm);
4970 av1_adapt_intra_frame_probs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00004971#if CONFIG_EC_ADAPT
4972 av1_average_tile_coef_cdfs(pbi->common.fc, tile_ctxs,
4973 cm->tile_rows * cm->tile_cols);
4974 av1_average_tile_intra_cdfs(pbi->common.fc, tile_ctxs,
4975 cm->tile_rows * cm->tile_cols);
4976#endif
hui suff0da2b2017-03-07 15:51:37 -08004977#if CONFIG_ADAPT_SCAN
4978 av1_adapt_scan_order(cm);
4979#endif // CONFIG_ADAPT_SCAN
Yaowu Xuc27fc142016-08-22 16:08:15 -07004980
4981 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004982 av1_adapt_inter_frame_probs(cm);
4983 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Thomas Davies028b57f2017-02-22 16:42:11 +00004984#if CONFIG_EC_ADAPT
4985 av1_average_tile_inter_cdfs(&pbi->common, pbi->common.fc, tile_ctxs,
4986 cm->tile_rows * cm->tile_cols);
4987 av1_average_tile_mv_cdfs(pbi->common.fc, tile_ctxs,
4988 cm->tile_rows * cm->tile_cols);
4989#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004990 }
4991 } else {
4992 debug_check_frame_counts(cm);
4993 }
4994 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004995 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004996 "Decode failed. Frame data is corrupted.");
4997 }
4998
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05004999#if CONFIG_INSPECTION
5000 if (pbi->inspect_cb != NULL) {
5001 (*pbi->inspect_cb)(pbi, pbi->inspect_ctx);
5002 }
5003#endif
5004
Yaowu Xuc27fc142016-08-22 16:08:15 -07005005 // Non frame parallel update frame context here.
5006 if (!cm->error_resilient_mode && !context_updated)
5007 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
5008}