blob: a5abb904d1ce9875c77090231c52c7c8f78e7afb [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)) {
David Barker9739f362016-11-10 09:29:32 +00002388 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002389 aom_read_literal(r, cm->dering_bits, ACCT_STR);
2390 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.clpf_strength =
2391 aom_read_literal(r, cm->clpf_bits, ACCT_STR);
David Barker9739f362016-11-10 09:29:32 +00002392 } else {
2393 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002394 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]
2395 ->mbmi.clpf_strength = 0;
David Barker9739f362016-11-10 09:29:32 +00002396 }
2397 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64) {
2398#else
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002399 if (bsize == BLOCK_64X64) {
David Barker9739f362016-11-10 09:29:32 +00002400#endif
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002401 if (!sb_all_skip(cm, mi_row, mi_col)) {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002402 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2403 aom_read_literal(r, cm->cdef_bits, ACCT_STR);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002404 } else {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002405 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2406 0;
Yaowu Xud71be782016-10-14 08:47:03 -07002407 }
2408 }
Jean-Marc Valin01435132017-02-18 14:12:53 -05002409#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002410}
2411
Yaowu Xuc27fc142016-08-22 16:08:15 -07002412static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
2413 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002414 struct aom_internal_error_info *error_info,
Alex Converseeb780e72016-12-13 12:46:41 -08002415 aom_reader *r,
2416#if CONFIG_ANS && ANS_MAX_SYMBOLS
2417 int window_size,
2418#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
2419 aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002420 // Validate the calculated partition length. If the buffer
2421 // described by the partition can't be fully read, then restrict
2422 // it to the portion that can be (for EC mode) or throw an error.
2423 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002424 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002425 "Truncated packet or corrupt tile length");
2426
Alex Converse2cdf0d82016-12-13 13:53:09 -08002427#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08002428 r->window_size = window_size;
Alex Converse2cdf0d82016-12-13 13:53:09 -08002429#endif
Alex Converse346440b2017-01-03 13:47:37 -08002430 if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07002431 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002432 "Failed to allocate bool decoder %d", 1);
2433}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002434
Angie Chiang800df032017-03-22 11:14:12 -07002435#if !CONFIG_PVQ && !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET) && !CONFIG_LV_MAP
Yaowu Xuf883b422016-08-30 14:01:10 -07002436static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
2437 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002438 int i, j, k, l, m;
Alex Conversea9598cd2017-02-03 14:18:05 -08002439#if CONFIG_EC_ADAPT
2440 const int node_limit = UNCONSTRAINED_NODES - 1;
2441#else
Thomas9ac55082016-09-23 18:04:17 +01002442 const int node_limit = UNCONSTRAINED_NODES;
Alex Conversea9598cd2017-02-03 14:18:05 -08002443#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002444
Michael Bebenita6048d052016-08-25 14:40:54 -07002445 if (aom_read_bit(r, ACCT_STR))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002446 for (i = 0; i < PLANE_TYPES; ++i)
2447 for (j = 0; j < REF_TYPES; ++j)
2448 for (k = 0; k < COEF_BANDS; ++k)
2449 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
Thomas9ac55082016-09-23 18:04:17 +01002450 for (m = 0; m < node_limit; ++m)
Michael Bebenita6048d052016-08-25 14:40:54 -07002451 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002452}
2453
Yaowu Xuf883b422016-08-30 14:01:10 -07002454static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002455 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
2456 TX_SIZE tx_size;
Jingning Han83630632016-12-16 11:27:25 -08002457 for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002458 read_coef_probs_common(fc->coef_probs[tx_size], r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002459}
Yushin Cho77bba8d2016-11-04 16:36:56 -07002460#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002461
Yaowu Xuf883b422016-08-30 14:01:10 -07002462static void setup_segmentation(AV1_COMMON *const cm,
2463 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002464 struct segmentation *const seg = &cm->seg;
2465 int i, j;
2466
2467 seg->update_map = 0;
2468 seg->update_data = 0;
2469
Yaowu Xuf883b422016-08-30 14:01:10 -07002470 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002471 if (!seg->enabled) return;
2472
2473 // Segmentation map update
2474 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2475 seg->update_map = 1;
2476 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002477 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002478 }
2479 if (seg->update_map) {
2480 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2481 seg->temporal_update = 0;
2482 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002483 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002484 }
2485 }
2486
2487 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07002488 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002489 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002490 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002491
Yaowu Xuf883b422016-08-30 14:01:10 -07002492 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002493
2494 for (i = 0; i < MAX_SEGMENTS; i++) {
2495 for (j = 0; j < SEG_LVL_MAX; j++) {
2496 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002497 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002498 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002499 av1_enable_segfeature(seg, i, j);
2500 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
2501 if (av1_is_segfeature_signed(j))
2502 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002503 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002504 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002505 }
2506 }
2507 }
2508}
2509
2510#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002511static void decode_restoration_mode(AV1_COMMON *cm,
2512 struct aom_read_bit_buffer *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002513 int p;
2514 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002515 if (aom_rb_read_bit(rb)) {
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002516 rsi->frame_restoration_type =
2517 aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002518 } else {
2519 rsi->frame_restoration_type =
2520 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
2521 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002522 for (p = 1; p < MAX_MB_PLANE; ++p) {
2523 cm->rst_info[p].frame_restoration_type =
2524 aom_rb_read_bit(rb) ? RESTORE_WIENER : RESTORE_NONE;
2525 }
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002526
2527 cm->rst_info[0].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2528 cm->rst_info[1].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2529 cm->rst_info[2].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2530 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
2531 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
2532 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
2533 rsi = &cm->rst_info[0];
2534 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2535 if (rsi->restoration_tilesize != RESTORATION_TILESIZE_MAX) {
2536 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2537 }
2538 cm->rst_info[1].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2539 cm->rst_info[2].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2540 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002541}
2542
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002543static void read_wiener_filter(WienerInfo *wiener_info, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002544 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002545 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2546 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002547 wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002548 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2549 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002550 wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002551 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2552 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002553 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002554 wiener_info->vfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002555 -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
2556 wiener_info->vfilter[2]);
2557
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002558 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002559 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2560 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002561 wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002562 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2563 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002564 wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002565 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2566 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002567 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002568 wiener_info->hfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002569 -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
2570 wiener_info->hfilter[2]);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002571}
2572
2573static void read_sgrproj_filter(SgrprojInfo *sgrproj_info, aom_reader *rb) {
2574 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
2575 sgrproj_info->xqd[0] =
2576 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN0;
2577 sgrproj_info->xqd[1] =
2578 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN1;
2579}
2580
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002581static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002582 int i, p;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002583 const int ntiles = av1_get_rest_ntiles(cm->width, cm->height,
2584 cm->rst_info[0].restoration_tilesize,
2585 NULL, NULL, NULL, NULL);
2586 const int ntiles_uv = av1_get_rest_ntiles(
2587 ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
2588 ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y),
2589 cm->rst_info[1].restoration_tilesize, NULL, NULL, NULL, NULL);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002590 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002591 if (rsi->frame_restoration_type != RESTORE_NONE) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002592 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002593 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002594 rsi->restoration_type[i] =
2595 aom_read_tree(rb, av1_switchable_restore_tree,
2596 cm->fc->switchable_restore_prob, ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002597 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002598 read_wiener_filter(&rsi->wiener_info[i], rb);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002599 } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002600 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002601 }
2602 }
2603 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002604 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002605 if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002606 rsi->restoration_type[i] = RESTORE_WIENER;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002607 read_wiener_filter(&rsi->wiener_info[i], rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002608 } else {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002609 rsi->restoration_type[i] = RESTORE_NONE;
2610 }
2611 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002612 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002613 for (i = 0; i < ntiles; ++i) {
2614 if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
2615 rsi->restoration_type[i] = RESTORE_SGRPROJ;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002616 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
2617 } else {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002618 rsi->restoration_type[i] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002619 }
2620 }
2621 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002622 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002623 for (p = 1; p < MAX_MB_PLANE; ++p) {
2624 rsi = &cm->rst_info[p];
2625 if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08002626 for (i = 0; i < ntiles_uv; ++i) {
2627 if (ntiles_uv > 1)
2628 rsi->restoration_type[i] =
2629 aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR) ? RESTORE_WIENER
2630 : RESTORE_NONE;
2631 else
2632 rsi->restoration_type[i] = RESTORE_WIENER;
2633 if (rsi->restoration_type[i] == RESTORE_WIENER) {
2634 read_wiener_filter(&rsi->wiener_info[i], rb);
2635 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002636 }
2637 }
2638 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002639}
2640#endif // CONFIG_LOOP_RESTORATION
2641
Yaowu Xuf883b422016-08-30 14:01:10 -07002642static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002643 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07002644 lf->filter_level = aom_rb_read_literal(rb, 6);
2645 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002646
2647 // Read in loop filter deltas applied at the MB level based on mode or ref
2648 // frame.
2649 lf->mode_ref_delta_update = 0;
2650
Yaowu Xuf883b422016-08-30 14:01:10 -07002651 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002652 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002653 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002654 if (lf->mode_ref_delta_update) {
2655 int i;
2656
2657 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002658 if (aom_rb_read_bit(rb))
2659 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002660
2661 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002662 if (aom_rb_read_bit(rb))
2663 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002664 }
2665 }
2666}
2667
Jean-Marc Valin01435132017-02-18 14:12:53 -05002668#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002669static void setup_cdef(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002670 int i;
2671 cm->cdef_bits = aom_rb_read_literal(rb, 2);
2672 cm->nb_cdef_strengths = 1 << cm->cdef_bits;
2673 for (i = 0; i < cm->nb_cdef_strengths; i++) {
2674 cm->cdef_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
Jean-Marc Valine9f77422017-03-22 17:09:51 -04002675 cm->cdef_uv_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002676 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002677}
Jean-Marc Valin01435132017-02-18 14:12:53 -05002678#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002679
Yaowu Xuf883b422016-08-30 14:01:10 -07002680static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2681 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002682}
2683
Yaowu Xuf883b422016-08-30 14:01:10 -07002684static void setup_quantization(AV1_COMMON *const cm,
2685 struct aom_read_bit_buffer *rb) {
2686 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002687 cm->y_dc_delta_q = read_delta_q(rb);
2688 cm->uv_dc_delta_q = read_delta_q(rb);
2689 cm->uv_ac_delta_q = read_delta_q(rb);
2690 cm->dequant_bit_depth = cm->bit_depth;
2691#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002692 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002693 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002694 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2695 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002696 } else {
2697 cm->min_qmlevel = 0;
2698 cm->max_qmlevel = 0;
2699 }
2700#endif
2701}
2702
Yaowu Xuf883b422016-08-30 14:01:10 -07002703static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002704 // Build y/uv dequant values based on segmentation.
2705 int i = 0;
2706#if CONFIG_AOM_QM
2707 int lossless;
2708 int j = 0;
2709 int qmlevel;
2710 int using_qm = cm->using_qmatrix;
2711 int minqm = cm->min_qmlevel;
2712 int maxqm = cm->max_qmlevel;
2713#endif
2714#if CONFIG_NEW_QUANT
2715 int b;
2716 int dq;
2717#endif // CONFIG_NEW_QUANT
2718 if (cm->seg.enabled) {
2719 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002720 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002721 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002722 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2723 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002724 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002725 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002726 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002727 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002728#if CONFIG_AOM_QM
2729 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2730 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2731 // NB: depends on base index so there is only 1 set per frame
2732 // No quant weighting when lossless or signalled not using QM
2733 qmlevel = (lossless || using_qm == 0)
2734 ? NUM_QM_LEVELS - 1
2735 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2736 for (j = 0; j < TX_SIZES; ++j) {
2737 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2738 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2739 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2740 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2741 }
2742#endif // CONFIG_AOM_QM
2743#if CONFIG_NEW_QUANT
2744 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2745 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002746 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002747 cm->y_dequant_nuq[i][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002748 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002749 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002750 }
2751 }
2752#endif // CONFIG_NEW_QUANT
2753 }
2754 } else {
2755 const int qindex = cm->base_qindex;
2756 // When segmentation is disabled, only the first value is used. The
2757 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002758 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2759 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002760 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002761 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002762 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002763 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002764#if CONFIG_AOM_QM
2765 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2766 cm->uv_ac_delta_q == 0;
2767 // No quant weighting when lossless or signalled not using QM
2768 qmlevel = (lossless || using_qm == 0)
2769 ? NUM_QM_LEVELS - 1
2770 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2771 for (j = 0; j < TX_SIZES; ++j) {
2772 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2773 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2774 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2775 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2776 }
2777#endif
2778#if CONFIG_NEW_QUANT
2779 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2780 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002781 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002782 cm->y_dequant_nuq[0][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002783 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002784 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002785 }
2786 }
2787#endif // CONFIG_NEW_QUANT
2788 }
2789}
2790
Angie Chiang5678ad92016-11-21 09:38:40 -08002791static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002792 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07002793 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002794}
2795
Yaowu Xuf883b422016-08-30 14:01:10 -07002796static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002797 cm->render_width = cm->width;
2798 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002799 if (aom_rb_read_bit(rb))
2800 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002801}
2802
Yaowu Xuf883b422016-08-30 14:01:10 -07002803static void resize_mv_buffer(AV1_COMMON *cm) {
2804 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002805 cm->cur_frame->mi_rows = cm->mi_rows;
2806 cm->cur_frame->mi_cols = cm->mi_cols;
2807 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002808 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002809 sizeof(*cm->cur_frame->mvs)));
2810}
2811
Yaowu Xuf883b422016-08-30 14:01:10 -07002812static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002813#if CONFIG_SIZE_LIMIT
2814 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002815 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002816 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2817 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2818#endif
2819 if (cm->width != width || cm->height != height) {
2820 const int new_mi_rows =
2821 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2822 const int new_mi_cols =
2823 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2824
Yaowu Xuf883b422016-08-30 14:01:10 -07002825 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002826 // dimensions as well as the overall size.
2827 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002828 if (av1_alloc_context_buffers(cm, width, height))
2829 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002830 "Failed to allocate context buffers");
2831 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002832 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002833 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002834 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002835 cm->width = width;
2836 cm->height = height;
2837 }
2838 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2839 cm->mi_cols > cm->cur_frame->mi_cols) {
2840 resize_mv_buffer(cm);
2841 }
2842}
2843
Yaowu Xuf883b422016-08-30 14:01:10 -07002844static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002845 int width, height;
2846 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002847 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002848 resize_context_buffers(cm, width, height);
2849 setup_render_size(cm, rb);
2850
2851 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002852 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002853 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2854 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002855#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002856 cm->use_highbitdepth,
2857#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002858 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002859 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2860 pool->cb_priv)) {
2861 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002862 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002863 "Failed to allocate frame buffer");
2864 }
2865 unlock_buffer_pool(pool);
2866
2867 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2868 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2869 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2870 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2871 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2872 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2873 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2874}
2875
Yaowu Xuf883b422016-08-30 14:01:10 -07002876static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002877 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002878 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002879 int this_xss, int this_yss) {
2880 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2881 ref_yss == this_yss;
2882}
2883
Yaowu Xuf883b422016-08-30 14:01:10 -07002884static void setup_frame_size_with_refs(AV1_COMMON *cm,
2885 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002886 int width, height;
2887 int found = 0, i;
2888 int has_valid_ref_frame = 0;
2889 BufferPool *const pool = cm->buffer_pool;
2890 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002891 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002892 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2893 width = buf->y_crop_width;
2894 height = buf->y_crop_height;
2895 cm->render_width = buf->render_width;
2896 cm->render_height = buf->render_height;
2897 found = 1;
2898 break;
2899 }
2900 }
2901
2902 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002903 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002904 setup_render_size(cm, rb);
2905 }
2906
2907 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002908 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002909 "Invalid frame size");
2910
2911 // Check to make sure at least one of frames that this frame references
2912 // has valid dimensions.
2913 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2914 RefBuffer *const ref_frame = &cm->frame_refs[i];
2915 has_valid_ref_frame |=
2916 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2917 ref_frame->buf->y_crop_height, width, height);
2918 }
2919 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002920 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002921 "Referenced frame has invalid size");
2922 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2923 RefBuffer *const ref_frame = &cm->frame_refs[i];
2924 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
2925 ref_frame->buf->subsampling_x,
2926 ref_frame->buf->subsampling_y, cm->bit_depth,
2927 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07002928 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002929 "Referenced frame has incompatible color format");
2930 }
2931
2932 resize_context_buffers(cm, width, height);
2933
2934 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002935 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002936 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2937 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002938#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002939 cm->use_highbitdepth,
2940#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002941 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002942 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2943 pool->cb_priv)) {
2944 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002945 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002946 "Failed to allocate frame buffer");
2947 }
2948 unlock_buffer_pool(pool);
2949
2950 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2951 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2952 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2953 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2954 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2955 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2956 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2957}
2958
Yaowu Xuf883b422016-08-30 14:01:10 -07002959static void read_tile_info(AV1Decoder *const pbi,
2960 struct aom_read_bit_buffer *const rb) {
2961 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002962#if CONFIG_EXT_TILE
2963// Read the tile width/height
2964#if CONFIG_EXT_PARTITION
2965 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002966 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
2967 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002968 } else
2969#endif // CONFIG_EXT_PARTITION
2970 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002971 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
2972 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002973 }
2974
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002975#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002976 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002977#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002978
Yaowu Xuc27fc142016-08-22 16:08:15 -07002979 cm->tile_width <<= cm->mib_size_log2;
2980 cm->tile_height <<= cm->mib_size_log2;
2981
Yaowu Xuf883b422016-08-30 14:01:10 -07002982 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
2983 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002984
2985 // Get the number of tiles
2986 cm->tile_cols = 1;
2987 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
2988
2989 cm->tile_rows = 1;
2990 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
2991
2992 if (cm->tile_cols * cm->tile_rows > 1) {
2993 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07002994 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2995 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002996 }
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002997
2998#if CONFIG_DEPENDENT_HORZTILES
2999 if (cm->tile_rows <= 1)
3000 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
3001 else
3002 cm->dependent_horz_tiles = 0;
3003#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003004#else
3005 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07003006 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003007
3008 // columns
3009 max_ones = max_log2_tile_cols - min_log2_tile_cols;
3010 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07003011 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003012
3013 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07003014 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003015 "Invalid number of tile columns");
3016
3017 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07003018 cm->log2_tile_rows = aom_rb_read_bit(rb);
3019 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003020#if CONFIG_DEPENDENT_HORZTILES
3021 if (cm->log2_tile_rows != 0)
3022 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
3023 else
3024 cm->dependent_horz_tiles = 0;
3025#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003026#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003027 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003028#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003029
Yaowu Xuc27fc142016-08-22 16:08:15 -07003030 cm->tile_cols = 1 << cm->log2_tile_cols;
3031 cm->tile_rows = 1 << cm->log2_tile_rows;
3032
3033 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
3034 cm->tile_width >>= cm->log2_tile_cols;
3035 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
3036 cm->tile_height >>= cm->log2_tile_rows;
3037
3038 // round to integer multiples of superblock size
3039 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
3040 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
3041
Thomas Davies4974e522016-11-07 17:44:05 +00003042// tile size magnitude
3043#if !CONFIG_TILE_GROUPS
3044 if (cm->tile_rows > 1 || cm->tile_cols > 1)
3045#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003046 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003047#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00003048
Thomas Davies80188d12016-10-26 16:08:35 -07003049#if CONFIG_TILE_GROUPS
3050 // Store an index to the location of the tile group information
3051 pbi->tg_size_bit_offset = rb->bit_offset;
3052 pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
3053 if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
3054 pbi->tg_start =
3055 aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3056 pbi->tg_size =
3057 1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3058 }
3059#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003060}
3061
3062static int mem_get_varsize(const uint8_t *src, const int sz) {
3063 switch (sz) {
3064 case 1: return src[0];
3065 case 2: return mem_get_le16(src);
3066 case 3: return mem_get_le24(src);
3067 case 4: return mem_get_le32(src);
3068 default: assert("Invalid size" && 0); return -1;
3069 }
3070}
3071
3072#if CONFIG_EXT_TILE
3073// Reads the next tile returning its size and adjusting '*data' accordingly
3074// based on 'is_last'.
3075static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07003076 struct aom_internal_error_info *error_info,
3077 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003078 void *decrypt_state,
3079 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
3080 int tile_size_bytes, int col, int row) {
3081 size_t size;
3082
3083 size_t copy_size = 0;
3084 const uint8_t *copy_data = NULL;
3085
3086 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003087 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003088 "Truncated packet or corrupt tile length");
3089 if (decrypt_cb) {
3090 uint8_t be_data[4];
3091 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3092
3093 // Only read number of bytes in cm->tile_size_bytes.
3094 size = mem_get_varsize(be_data, tile_size_bytes);
3095 } else {
3096 size = mem_get_varsize(*data, tile_size_bytes);
3097 }
3098
3099 // The top bit indicates copy mode
3100 if ((size >> (tile_size_bytes * 8 - 1)) == 1) {
3101 // The remaining bits in the top byte signal the row offset
3102 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
3103
3104 // Currently, only use tiles in same column as reference tiles.
3105 copy_data = tile_buffers[row - offset][col].data;
3106 copy_size = tile_buffers[row - offset][col].size;
3107 size = 0;
3108 }
3109
3110 *data += tile_size_bytes;
3111
3112 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003113 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003114 "Truncated packet or corrupt tile size");
3115
3116 if (size > 0) {
3117 tile_buffers[row][col].data = *data;
3118 tile_buffers[row][col].size = size;
3119 } else {
3120 tile_buffers[row][col].data = copy_data;
3121 tile_buffers[row][col].size = copy_size;
3122 }
3123
3124 *data += size;
3125
3126 tile_buffers[row][col].raw_data_end = *data;
3127}
3128
3129static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003130 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003131 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003132 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003133 const int tile_cols = cm->tile_cols;
3134 const int tile_rows = cm->tile_rows;
3135 const int have_tiles = tile_cols * tile_rows > 1;
3136
3137 if (!have_tiles) {
3138 const uint32_t tile_size = data_end - data;
3139 tile_buffers[0][0].data = data;
3140 tile_buffers[0][0].size = tile_size;
3141 tile_buffers[0][0].raw_data_end = NULL;
3142 } else {
3143 // We locate only the tile buffers that are required, which are the ones
3144 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
3145 // need the last (bottom right) tile buffer, as we need to know where the
3146 // end of the compressed frame buffer is for proper superframe decoding.
3147
3148 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
3149 const uint8_t *const data_start = data;
3150
Yaowu Xuf883b422016-08-30 14:01:10 -07003151 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003152 const int single_row = pbi->dec_tile_row >= 0;
3153 const int tile_rows_start = single_row ? dec_tile_row : 0;
3154 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003155 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003156 const int single_col = pbi->dec_tile_col >= 0;
3157 const int tile_cols_start = single_col ? dec_tile_col : 0;
3158 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3159
3160 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
3161 const int tile_size_bytes = pbi->tile_size_bytes;
3162
3163 size_t tile_col_size;
3164 int r, c;
3165
3166 // Read tile column sizes for all columns (we need the last tile buffer)
3167 for (c = 0; c < tile_cols; ++c) {
3168 const int is_last = c == tile_cols - 1;
3169 if (!is_last) {
3170 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
3171 data += tile_col_size_bytes;
3172 tile_col_data_end[c] = data + tile_col_size;
3173 } else {
3174 tile_col_size = data_end - data;
3175 tile_col_data_end[c] = data_end;
3176 }
3177 data += tile_col_size;
3178 }
3179
3180 data = data_start;
3181
3182 // Read the required tile sizes.
3183 for (c = tile_cols_start; c < tile_cols_end; ++c) {
3184 const int is_last = c == tile_cols - 1;
3185
3186 if (c > 0) data = tile_col_data_end[c - 1];
3187
3188 if (!is_last) data += tile_col_size_bytes;
3189
3190 // Get the whole of the last column, otherwise stop at the required tile.
3191 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
3192 tile_buffers[r][c].col = c;
3193
3194 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3195 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3196 tile_size_bytes, c, r);
3197 }
3198 }
3199
3200 // If we have not read the last column, then read it to get the last tile.
3201 if (tile_cols_end != tile_cols) {
3202 c = tile_cols - 1;
3203
3204 data = tile_col_data_end[c - 1];
3205
3206 for (r = 0; r < tile_rows; ++r) {
3207 tile_buffers[r][c].col = c;
3208
3209 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3210 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3211 tile_size_bytes, c, r);
3212 }
3213 }
3214 }
3215}
3216#else
3217// Reads the next tile returning its size and adjusting '*data' accordingly
3218// based on 'is_last'.
3219static void get_tile_buffer(const uint8_t *const data_end,
3220 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07003221 struct aom_internal_error_info *error_info,
3222 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003223 void *decrypt_state, TileBufferDec *const buf) {
3224 size_t size;
3225
3226 if (!is_last) {
Yaowu Xu0a79a1b2017-02-17 13:04:54 -08003227 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003228 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003229 "Truncated packet or corrupt tile length");
3230
3231 if (decrypt_cb) {
3232 uint8_t be_data[4];
3233 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3234 size = mem_get_varsize(be_data, tile_size_bytes);
3235 } else {
3236 size = mem_get_varsize(*data, tile_size_bytes);
3237 }
3238 *data += tile_size_bytes;
3239
3240 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003241 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003242 "Truncated packet or corrupt tile size");
3243 } else {
3244 size = data_end - *data;
3245 }
3246
3247 buf->data = *data;
3248 buf->size = size;
3249
3250 *data += size;
3251}
3252
3253static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003254 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003255 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003256 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07003257#if CONFIG_TILE_GROUPS
3258 int r, c;
3259 const int tile_cols = cm->tile_cols;
3260 const int tile_rows = cm->tile_rows;
3261 int tc = 0;
3262 int first_tile_in_tg = 0;
3263 int hdr_offset;
3264 struct aom_read_bit_buffer rb_tg_hdr;
3265 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3266 const int num_tiles = tile_rows * tile_cols;
3267 const int num_bits = OD_ILOG(num_tiles) - 1;
3268 const int hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
3269 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
Fangwen Fu73126c02017-02-08 22:37:47 -08003270#if CONFIG_DEPENDENT_HORZTILES
3271 int tile_group_start_col = 0;
3272 int tile_group_start_row = 0;
3273#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003274
3275 for (r = 0; r < tile_rows; ++r) {
3276 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07003277 TileBufferDec *const buf = &tile_buffers[r][c];
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003278 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
Thomas Davies80188d12016-10-26 16:08:35 -07003279 hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
3280
3281 buf->col = c;
3282 if (hdr_offset) {
3283 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
3284 rb_tg_hdr.bit_offset = tg_size_bit_offset;
3285 if (num_tiles) {
3286 pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
3287 pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
Fangwen Fu73126c02017-02-08 22:37:47 -08003288#if CONFIG_DEPENDENT_HORZTILES
3289 tile_group_start_row = r;
3290 tile_group_start_col = c;
3291#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003292 }
3293 }
3294 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
3295 data += hdr_offset;
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003296 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last,
3297 &pbi->common.error, &data, pbi->decrypt_cb,
3298 pbi->decrypt_state, buf);
Fangwen Fu73126c02017-02-08 22:37:47 -08003299#if CONFIG_DEPENDENT_HORZTILES
3300 cm->tile_group_start_row[r][c] = tile_group_start_row;
3301 cm->tile_group_start_col[r][c] = tile_group_start_col;
3302#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003303 }
3304 }
3305#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003306 int r, c;
3307 const int tile_cols = cm->tile_cols;
3308 const int tile_rows = cm->tile_rows;
3309
3310 for (r = 0; r < tile_rows; ++r) {
3311 for (c = 0; c < tile_cols; ++c) {
3312 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
3313 TileBufferDec *const buf = &tile_buffers[r][c];
3314 buf->col = c;
3315 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
3316 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
3317 }
3318 }
Thomas Davies80188d12016-10-26 16:08:35 -07003319#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003320}
3321#endif // CONFIG_EXT_TILE
3322
Yushin Cho77bba8d2016-11-04 16:36:56 -07003323#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003324static void daala_dec_init(AV1_COMMON *const cm, daala_dec_ctx *daala_dec,
Nathan E. Eggeab083972016-12-28 15:31:46 -05003325 aom_reader *r) {
3326 daala_dec->r = r;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003327 od_adapt_ctx_reset(&daala_dec->state.adapt, 0);
3328
Yushin Cho70669122016-12-08 09:53:14 -10003329 // TODO(yushin) : activity masking info needs be signaled by a bitstream
3330 daala_dec->use_activity_masking = AV1_PVQ_ENABLE_ACTIVITY_MASKING;
3331
Yushin Cho7a428ba2017-01-12 16:28:49 -08003332#if !CONFIG_DAALA_DIST
3333 daala_dec->use_activity_masking = 0;
3334#endif
3335
Yushin Cho70669122016-12-08 09:53:14 -10003336 if (daala_dec->use_activity_masking)
3337 daala_dec->qm = OD_HVS_QM;
3338 else
3339 daala_dec->qm = OD_FLAT_QM;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003340
3341 od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
3342 daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
Yushin Cho70669122016-12-08 09:53:14 -10003343
3344 if (daala_dec->use_activity_masking) {
3345 int pli;
3346 int use_masking = daala_dec->use_activity_masking;
3347 int segment_id = 0;
3348 int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
3349
3350 for (pli = 0; pli < MAX_MB_PLANE; pli++) {
3351 int i;
3352 int q;
3353
3354 q = qindex;
3355 if (q <= OD_DEFAULT_QMS[use_masking][0][pli].interp_q << OD_COEFF_SHIFT) {
3356 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3357 &OD_DEFAULT_QMS[use_masking][0][pli], NULL);
3358 } else {
3359 i = 0;
3360 while (OD_DEFAULT_QMS[use_masking][i + 1][pli].qm_q4 != NULL &&
3361 q > OD_DEFAULT_QMS[use_masking][i + 1][pli].interp_q
3362 << OD_COEFF_SHIFT) {
3363 i++;
3364 }
3365 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3366 &OD_DEFAULT_QMS[use_masking][i][pli],
3367 &OD_DEFAULT_QMS[use_masking][i + 1][pli]);
3368 }
3369 }
3370 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07003371}
Yushin Cho70669122016-12-08 09:53:14 -10003372#endif // #if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003373
Yaowu Xuf883b422016-08-30 14:01:10 -07003374static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003375 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003376 AV1_COMMON *const cm = &pbi->common;
3377 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003378 const int tile_cols = cm->tile_cols;
3379 const int tile_rows = cm->tile_rows;
3380 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07003381 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003382#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003383 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003384 const int single_row = pbi->dec_tile_row >= 0;
3385 const int tile_rows_start = single_row ? dec_tile_row : 0;
3386 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003387 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003388 const int single_col = pbi->dec_tile_col >= 0;
3389 const int tile_cols_start = single_col ? dec_tile_col : 0;
3390 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3391 const int inv_col_order = pbi->inv_tile_order && !single_col;
3392 const int inv_row_order = pbi->inv_tile_order && !single_row;
3393#else
3394 const int tile_rows_start = 0;
3395 const int tile_rows_end = tile_rows;
3396 const int tile_cols_start = 0;
3397 const int tile_cols_end = tile_cols;
3398 const int inv_col_order = pbi->inv_tile_order;
3399 const int inv_row_order = pbi->inv_tile_order;
3400#endif // CONFIG_EXT_TILE
3401 int tile_row, tile_col;
3402
hui su0d103572017-03-01 17:58:01 -08003403#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003404 cm->do_subframe_update = n_tiles == 1;
hui su0d103572017-03-01 17:58:01 -08003405#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003406
3407 if (cm->lf.filter_level && !cm->skip_loop_filter &&
3408 pbi->lf_worker.data1 == NULL) {
3409 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07003410 aom_memalign(32, sizeof(LFWorkerData)));
3411 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003412 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003413 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003414 "Loop filter thread creation failed");
3415 }
3416 }
3417
3418 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3419 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3420 // Be sure to sync as we might be resuming after a failed frame decode.
3421 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003422 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
3423 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003424 }
3425
3426 assert(tile_rows <= MAX_TILE_ROWS);
3427 assert(tile_cols <= MAX_TILE_COLS);
3428
3429 get_tile_buffers(pbi, data, data_end, tile_buffers);
3430
3431 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003432 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003433 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003434 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003435 pbi->allocated_tiles = n_tiles;
3436 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003437#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003438 if (pbi->acct_enabled) {
3439 aom_accounting_reset(&pbi->accounting);
3440 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003441#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003442 // Load all tile information into tile_data.
3443 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3444 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3445 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
3446 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
3447
3448 td->cm = cm;
3449 td->xd = pbi->mb;
3450 td->xd.corrupted = 0;
3451 td->xd.counts =
3452 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3453 ? &cm->counts
3454 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003455 av1_zero(td->dqcoeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003456#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003457 av1_zero(td->pvq_ref_coeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003458#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003459 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003460 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003461 &td->bit_reader,
3462#if CONFIG_ANS && ANS_MAX_SYMBOLS
3463 1 << cm->ans_window_size_log2,
3464#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3465 pbi->decrypt_cb, pbi->decrypt_state);
Michael Bebenita6048d052016-08-25 14:40:54 -07003466#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003467 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003468 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003469 } else {
David Barkerd971f402016-10-25 13:52:07 +01003470 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003471 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003472#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003473 av1_init_macroblockd(cm, &td->xd,
3474#if CONFIG_PVQ
3475 td->pvq_ref_coeff,
3476#endif
3477 td->dqcoeff);
3478#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003479 daala_dec_init(cm, &td->xd.daala_dec, &td->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003480#endif
Thomas Daviesf77d4ad2017-01-10 18:55:42 +00003481#if CONFIG_EC_ADAPT
3482 // Initialise the tile context from the frame context
3483 td->tctx = *cm->fc;
3484 td->xd.tile_ctx = &td->tctx;
3485#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003486#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003487 td->xd.plane[0].color_index_map = td->color_index_map[0];
3488 td->xd.plane[1].color_index_map = td->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003489#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003490 }
3491 }
3492
3493 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3494 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
3495 int mi_row = 0;
3496 TileInfo tile_info;
3497
Yaowu Xuf883b422016-08-30 14:01:10 -07003498 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003499
3500 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3501 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
3502 TileData *const td = pbi->tile_data + tile_cols * row + col;
Michael Bebenita6048d052016-08-25 14:40:54 -07003503#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003504 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003505 td->bit_reader.accounting->last_tell_frac =
3506 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003507 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003508#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003509
Yaowu Xuf883b422016-08-30 14:01:10 -07003510 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003511
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003512#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003513#if CONFIG_TILE_GROUPS
3514 av1_tile_set_tg_boundary(&tile_info, cm, tile_row, tile_col);
3515 if (!cm->dependent_horz_tiles || tile_row == 0 ||
3516 tile_info.tg_horz_boundary) {
3517#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003518 if (!cm->dependent_horz_tiles || tile_row == 0) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003519#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003520 av1_zero_above_context(cm, tile_info.mi_col_start,
3521 tile_info.mi_col_end);
3522 }
3523#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003524 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003525#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003526
3527 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
3528 mi_row += cm->mib_size) {
3529 int mi_col;
3530
Yaowu Xuf883b422016-08-30 14:01:10 -07003531 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003532
3533 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
3534 mi_col += cm->mib_size) {
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003535 av1_update_boundary_info(cm, &tile_info, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003536 decode_partition(pbi, &td->xd,
3537#if CONFIG_SUPERTX
3538 0,
3539#endif // CONFIG_SUPERTX
3540 mi_row, mi_col, &td->bit_reader, cm->sb_size,
3541 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003542#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3543 detoken_and_recon_sb(pbi, &td->xd, mi_row, mi_col, &td->bit_reader,
3544 cm->sb_size);
3545#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003546 }
Angie Chiangd0916d92017-03-10 17:54:18 -08003547 aom_merge_corrupted_flag(&pbi->mb.corrupted, td->xd.corrupted);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003548 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003549 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003550 "Failed to decode tile data");
hui su0d103572017-03-01 17:58:01 -08003551#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003552 if (cm->do_subframe_update &&
3553 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
hui su0161a932017-01-24 14:12:11 -08003554 const int mi_rows_per_update =
3555 MI_SIZE * AOMMAX(cm->mi_rows / MI_SIZE / COEF_PROBS_BUFS, 1);
3556 if ((mi_row + MI_SIZE) % mi_rows_per_update == 0 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003557 mi_row + MI_SIZE < cm->mi_rows &&
3558 cm->coef_probs_update_idx < COEF_PROBS_BUFS - 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003559 av1_partial_adapt_probs(cm, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003560 ++cm->coef_probs_update_idx;
3561 }
3562 }
hui su0d103572017-03-01 17:58:01 -08003563#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003564 }
3565 }
3566
3567 assert(mi_row > 0);
3568
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003569// when Parallel deblocking is enabled, deblocking should not
3570// be interleaved with decoding. Instead, deblocking should be done
3571// after the entire frame is decoded.
3572#if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003573 // Loopfilter one tile row.
3574 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3575 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003576 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003577 const int lf_end = tile_info.mi_row_end - cm->mib_size;
3578
3579 // Delay the loopfilter if the first tile row is only
3580 // a single superblock high.
3581 if (lf_end <= 0) continue;
3582
3583 // Decoding has completed. Finish up the loop filter in this thread.
3584 if (tile_info.mi_row_end >= cm->mi_rows) continue;
3585
3586 winterface->sync(&pbi->lf_worker);
3587 lf_data->start = lf_start;
3588 lf_data->stop = lf_end;
3589 if (pbi->max_threads > 1) {
3590 winterface->launch(&pbi->lf_worker);
3591 } else {
3592 winterface->execute(&pbi->lf_worker);
3593 }
3594 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003595#endif // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003596
3597 // After loopfiltering, the last 7 row pixels in each superblock row may
3598 // still be changed by the longest loopfilter of the next superblock row.
3599 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003600 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003601 }
3602
3603#if CONFIG_VAR_TX
3604 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003605 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
3606 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003607#else
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003608#if CONFIG_PARALLEL_DEBLOCKING
3609 // Loopfilter all rows in the frame in the frame.
3610 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3611 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3612 winterface->sync(&pbi->lf_worker);
3613 lf_data->start = 0;
3614 lf_data->stop = cm->mi_rows;
3615 winterface->execute(&pbi->lf_worker);
3616 }
3617#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003618 // Loopfilter remaining rows in the frame.
3619 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3620 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3621 winterface->sync(&pbi->lf_worker);
3622 lf_data->start = lf_data->stop;
3623 lf_data->stop = cm->mi_rows;
3624 winterface->execute(&pbi->lf_worker);
3625 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003626#endif // CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003627#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003628 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003629 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003630
3631#if CONFIG_EXT_TILE
3632 if (n_tiles == 1) {
3633#if CONFIG_ANS
3634 return data_end;
3635#else
3636 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07003637 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003638#endif // CONFIG_ANS
3639 } else {
3640 // Return the end of the last tile buffer
3641 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3642 }
3643#else
3644#if CONFIG_ANS
3645 return data_end;
3646#else
3647 {
3648 // Get last tile data.
3649 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003650 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003651 }
3652#endif // CONFIG_ANS
3653#endif // CONFIG_EXT_TILE
3654}
3655
3656static int tile_worker_hook(TileWorkerData *const tile_data,
3657 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003658 AV1Decoder *const pbi = tile_data->pbi;
3659 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003660 int mi_row, mi_col;
3661
3662 if (setjmp(tile_data->error_info.jmp)) {
3663 tile_data->error_info.setjmp = 0;
Angie Chiangd0916d92017-03-10 17:54:18 -08003664 aom_merge_corrupted_flag(&tile_data->xd.corrupted, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003665 return 0;
3666 }
3667
3668 tile_data->error_info.setjmp = 1;
3669 tile_data->xd.error_info = &tile_data->error_info;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003670#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003671#if CONFIG_TILE_GROUPS
3672 if (!cm->dependent_horz_tiles || tile->tg_horz_boundary) {
3673#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003674 if (!cm->dependent_horz_tiles) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003675#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003676 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
3677 }
3678#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003679 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003680#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003681
3682 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
3683 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003684 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003685
3686 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3687 mi_col += cm->mib_size) {
3688 decode_partition(pbi, &tile_data->xd,
3689#if CONFIG_SUPERTX
3690 0,
3691#endif
3692 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
3693 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003694#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3695 detoken_and_recon_sb(pbi, &tile_data->xd, mi_row, mi_col,
3696 &tile_data->bit_reader, cm->sb_size);
3697#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003698 }
3699 }
3700 return !tile_data->xd.corrupted;
3701}
3702
3703// sorts in descending order
3704static int compare_tile_buffers(const void *a, const void *b) {
3705 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
3706 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
3707 return (int)(buf2->size - buf1->size);
3708}
3709
Yaowu Xuf883b422016-08-30 14:01:10 -07003710static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003711 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003712 AV1_COMMON *const cm = &pbi->common;
3713 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003714 const int tile_cols = cm->tile_cols;
3715 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003716 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
clang-format67948d32016-09-07 22:40:40 -07003717 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003718#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003719 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003720 const int single_row = pbi->dec_tile_row >= 0;
3721 const int tile_rows_start = single_row ? dec_tile_row : 0;
3722 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003723 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003724 const int single_col = pbi->dec_tile_col >= 0;
3725 const int tile_cols_start = single_col ? dec_tile_col : 0;
3726 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3727#else
3728 const int tile_rows_start = 0;
3729 const int tile_rows_end = tile_rows;
3730 const int tile_cols_start = 0;
3731 const int tile_cols_end = tile_cols;
3732#endif // CONFIG_EXT_TILE
3733 int tile_row, tile_col;
3734 int i;
3735
3736#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3737 int final_worker = -1;
3738#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3739
3740 assert(tile_rows <= MAX_TILE_ROWS);
3741 assert(tile_cols <= MAX_TILE_COLS);
3742
3743 assert(tile_cols * tile_rows > 1);
3744
Yaowu Xuc27fc142016-08-22 16:08:15 -07003745 // TODO(jzern): See if we can remove the restriction of passing in max
3746 // threads to the decoder.
3747 if (pbi->num_tile_workers == 0) {
3748 const int num_threads = pbi->max_threads & ~1;
3749 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07003750 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003751 // Ensure tile data offsets will be properly aligned. This may fail on
3752 // platforms without DECLARE_ALIGNED().
3753 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3754 CHECK_MEM_ERROR(
3755 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003756 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003757 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07003758 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003759 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003760 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003761 ++pbi->num_tile_workers;
3762
3763 winterface->init(worker);
3764 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003765 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003766 "Tile decoder thread creation failed");
3767 }
3768 }
3769 }
3770
3771 // Reset tile decoding hook
3772 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003773 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003774 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003775 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003776 worker->data1 = &pbi->tile_worker_data[i];
3777 worker->data2 = &pbi->tile_worker_info[i];
3778 }
3779
3780 // Initialize thread frame counts.
3781 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3782 for (i = 0; i < num_workers; ++i) {
3783 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003784 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003785 }
3786 }
3787
3788 // Load tile data into tile_buffers
3789 get_tile_buffers(pbi, data, data_end, tile_buffers);
3790
3791 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3792 // Sort the buffers in this tile row based on size in descending order.
3793 qsort(&tile_buffers[tile_row][tile_cols_start],
3794 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
3795 compare_tile_buffers);
3796
3797 // Rearrange the tile buffers in this tile row such that per-tile group
3798 // the largest, and presumably the most difficult tile will be decoded in
3799 // the main thread. This should help minimize the number of instances
3800 // where the main thread is waiting for a worker to complete.
3801 {
3802 int group_start;
3803 for (group_start = tile_cols_start; group_start < tile_cols_end;
3804 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003805 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003806 const TileBufferDec largest = tile_buffers[tile_row][group_start];
3807 memmove(&tile_buffers[tile_row][group_start],
3808 &tile_buffers[tile_row][group_start + 1],
3809 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
3810 tile_buffers[tile_row][group_end - 1] = largest;
3811 }
3812 }
3813
3814 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
3815 // Launch workers for individual columns
3816 for (i = 0; i < num_workers && tile_col < tile_cols_end;
3817 ++i, ++tile_col) {
3818 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07003819 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003820 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
3821 TileInfo *const tile_info = (TileInfo *)worker->data2;
3822
3823 twd->pbi = pbi;
3824 twd->xd = pbi->mb;
3825 twd->xd.corrupted = 0;
3826 twd->xd.counts =
3827 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3828 ? &twd->counts
3829 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003830 av1_zero(twd->dqcoeff);
3831 av1_tile_init(tile_info, cm, tile_row, buf->col);
3832 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003833 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003834 &twd->bit_reader,
3835#if CONFIG_ANS && ANS_MAX_SYMBOLS
3836 1 << cm->ans_window_size_log2,
3837#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3838 pbi->decrypt_cb, pbi->decrypt_state);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003839 av1_init_macroblockd(cm, &twd->xd,
3840#if CONFIG_PVQ
3841 twd->pvq_ref_coeff,
3842#endif
3843 twd->dqcoeff);
3844#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003845 daala_dec_init(cm, &twd->xd.daala_dec, &twd->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003846#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003847#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003848 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
3849 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003850#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003851
3852 worker->had_error = 0;
3853 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
3854 winterface->execute(worker);
3855 } else {
3856 winterface->launch(worker);
3857 }
3858
3859#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3860 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
3861 final_worker = i;
3862 }
3863#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3864 }
3865
3866 // Sync all workers
3867 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003868 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003869 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07003870 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07003871 // info in cm. Additionally once the threads have been synced and an
3872 // error is detected, there's no point in continuing to decode tiles.
3873 pbi->mb.corrupted |= !winterface->sync(worker);
3874 }
3875 }
3876 }
3877
3878 // Accumulate thread frame counts.
3879 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3880 for (i = 0; i < num_workers; ++i) {
3881 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003882 av1_accumulate_frame_counts(&cm->counts, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003883 }
3884 }
3885
3886#if CONFIG_EXT_TILE
3887 // Return the end of the last tile buffer
3888 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3889#else
3890#if CONFIG_ANS
3891 return data_end;
3892#else
3893 assert(final_worker != -1);
3894 {
3895 TileWorkerData *const twd =
3896 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003897 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003898 }
3899#endif // CONFIG_ANS
3900#endif // CONFIG_EXT_TILE
3901}
3902
3903static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003904 AV1_COMMON *const cm = (AV1_COMMON *)data;
3905 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003906}
3907
Yaowu Xuf883b422016-08-30 14:01:10 -07003908static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3909 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003910 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003911 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003912 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003913 cm->bit_depth = AOM_BITS_8;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003914 }
3915
Yaowu Xuf883b422016-08-30 14:01:10 -07003916#if CONFIG_AOM_HIGHBITDEPTH
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003917 if (cm->bit_depth > AOM_BITS_8) {
3918 cm->use_highbitdepth = 1;
3919 } else {
Yaowu Xu345a22d2017-02-27 09:23:52 -08003920#if CONFIG_LOWBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003921 cm->use_highbitdepth = 0;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003922#else
3923 cm->use_highbitdepth = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003924#endif
3925 }
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003926#endif
3927
Yaowu Xuf883b422016-08-30 14:01:10 -07003928 cm->color_space = aom_rb_read_literal(rb, 3);
3929 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003930 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07003931 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003932 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003933 cm->subsampling_x = aom_rb_read_bit(rb);
3934 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003935 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003936 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003937 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07003938 if (aom_rb_read_bit(rb))
3939 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003940 "Reserved bit set");
3941 } else {
3942 cm->subsampling_y = cm->subsampling_x = 1;
3943 }
3944 } else {
3945 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
3946 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
3947 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
3948 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003949 if (aom_rb_read_bit(rb))
3950 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003951 "Reserved bit set");
3952 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003953 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003954 "4:4:4 color not supported in profile 0 or 2");
3955 }
3956 }
3957}
3958
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003959#if CONFIG_REFERENCE_BUFFER
3960void read_sequence_header(SequenceHeader *seq_params) {
3961 /* Placeholder for actually reading from the bitstream */
3962 seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
3963 seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
3964 seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
3965}
3966#endif
3967
Yaowu Xuf883b422016-08-30 14:01:10 -07003968static size_t read_uncompressed_header(AV1Decoder *pbi,
3969 struct aom_read_bit_buffer *rb) {
3970 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003971 MACROBLOCKD *const xd = &pbi->mb;
3972 BufferPool *const pool = cm->buffer_pool;
3973 RefCntBuffer *const frame_bufs = pool->frame_bufs;
3974 int i, mask, ref_index = 0;
3975 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003976
3977#if CONFIG_REFERENCE_BUFFER
3978 /* TODO: Move outside frame loop or inside key-frame branch */
3979 read_sequence_header(&pbi->seq_params);
3980#endif
3981
Yaowu Xuc27fc142016-08-22 16:08:15 -07003982 cm->last_frame_type = cm->frame_type;
3983 cm->last_intra_only = cm->intra_only;
3984
3985#if CONFIG_EXT_REFS
3986 // NOTE: By default all coded frames to be used as a reference
3987 cm->is_reference_frame = 1;
3988#endif // CONFIG_EXT_REFS
3989
Yaowu Xuf883b422016-08-30 14:01:10 -07003990 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
3991 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003992 "Invalid frame marker");
3993
Yaowu Xuf883b422016-08-30 14:01:10 -07003994 cm->profile = av1_read_profile(rb);
3995#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003996 if (cm->profile >= MAX_PROFILES)
Yaowu Xuf883b422016-08-30 14:01:10 -07003997 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003998 "Unsupported bitstream profile");
3999#else
4000 if (cm->profile >= PROFILE_2)
Yaowu Xuf883b422016-08-30 14:01:10 -07004001 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004002 "Unsupported bitstream profile");
4003#endif
4004
Yaowu Xuf883b422016-08-30 14:01:10 -07004005 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004006
4007 if (cm->show_existing_frame) {
Yaowu Xu415ba932016-12-27 11:17:32 -08004008 // Show an existing frame directly.
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004009 const int existing_frame_idx = aom_rb_read_literal(rb, 3);
4010 const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
Yaowu Xu415ba932016-12-27 11:17:32 -08004011#if CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004012 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004013 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4014 int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004015 /* Compare display_frame_id with ref_frame_id and check valid for
4016 * referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004017 if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
4018 cm->valid_for_referencing[existing_frame_idx] == 0)
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004019 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4020 "Reference buffer frame ID mismatch");
4021 }
4022#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004023 lock_buffer_pool(pool);
4024 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
4025 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07004026 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004027 "Buffer %d does not contain a decoded frame",
4028 frame_to_show);
4029 }
4030 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
4031 unlock_buffer_pool(pool);
4032
4033 cm->lf.filter_level = 0;
4034 cm->show_frame = 1;
4035 pbi->refresh_frame_flags = 0;
4036
4037 if (cm->frame_parallel_decode) {
4038 for (i = 0; i < REF_FRAMES; ++i)
4039 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
4040 }
4041
4042 return 0;
4043 }
4044
Yaowu Xuf883b422016-08-30 14:01:10 -07004045 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
4046 cm->show_frame = aom_rb_read_bit(rb);
4047 cm->error_resilient_mode = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004048#if CONFIG_REFERENCE_BUFFER
4049 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004050 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4051 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4052 int prev_frame_id = 0;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004053 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004054 prev_frame_id = cm->current_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004055 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004056 cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004057
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004058 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004059 int diff_frame_id;
4060 if (cm->current_frame_id > prev_frame_id) {
4061 diff_frame_id = cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004062 } else {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004063 diff_frame_id =
4064 (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004065 }
4066 /* Check current_frame_id for conformance */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004067 if (prev_frame_id == cm->current_frame_id ||
4068 diff_frame_id >= (1 << (frame_id_length - 1))) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004069 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4070 "Invalid value of current_frame_id");
4071 }
4072 }
4073 /* Check if some frames need to be marked as not valid for referencing */
4074 for (i = 0; i < REF_FRAMES; i++) {
4075 if (cm->frame_type == KEY_FRAME) {
4076 cm->valid_for_referencing[i] = 0;
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004077 } else if (cm->current_frame_id - (1 << diff_len) > 0) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004078 if (cm->ref_frame_id[i] > cm->current_frame_id ||
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004079 cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004080 cm->valid_for_referencing[i] = 0;
4081 } else {
4082 if (cm->ref_frame_id[i] > cm->current_frame_id &&
4083 cm->ref_frame_id[i] <
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004084 (1 << frame_id_length) + cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004085 cm->valid_for_referencing[i] = 0;
4086 }
4087 }
4088 }
4089#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004090 if (cm->frame_type == KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004091 if (!av1_read_sync_code(rb))
4092 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004093 "Invalid frame sync code");
4094
4095 read_bitdepth_colorspace_sampling(cm, rb);
4096 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
4097
4098 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4099 cm->frame_refs[i].idx = INVALID_IDX;
4100 cm->frame_refs[i].buf = NULL;
4101 }
4102
4103 setup_frame_size(cm, rb);
4104 if (pbi->need_resync) {
4105 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4106 pbi->need_resync = 0;
4107 }
Alex Converseeb780e72016-12-13 12:46:41 -08004108#if CONFIG_ANS && ANS_MAX_SYMBOLS
4109 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4110#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
Urvang Joshib100db72016-10-12 16:28:56 -07004111#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004112 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004113#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004114 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004115 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004116#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004117 if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004118#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004119 if (cm->error_resilient_mode) {
4120 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
4121 } else {
4122 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004123 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004124 ? RESET_FRAME_CONTEXT_ALL
4125 : RESET_FRAME_CONTEXT_CURRENT;
4126 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004127 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004128 ? RESET_FRAME_CONTEXT_CURRENT
4129 : RESET_FRAME_CONTEXT_NONE;
4130 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07004131 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004132 ? RESET_FRAME_CONTEXT_ALL
4133 : RESET_FRAME_CONTEXT_CURRENT;
4134 }
4135 }
4136
4137 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004138 if (!av1_read_sync_code(rb))
4139 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004140 "Invalid frame sync code");
4141
4142 read_bitdepth_colorspace_sampling(cm, rb);
4143
Yaowu Xuf883b422016-08-30 14:01:10 -07004144 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004145 setup_frame_size(cm, rb);
4146 if (pbi->need_resync) {
4147 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4148 pbi->need_resync = 0;
4149 }
Alex Converseeb780e72016-12-13 12:46:41 -08004150#if CONFIG_ANS && ANS_MAX_SYMBOLS
4151 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4152#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004153 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Yaowu Xuf883b422016-08-30 14:01:10 -07004154 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004155
4156#if CONFIG_EXT_REFS
4157 if (!pbi->refresh_frame_flags) {
4158 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
4159 // will not be used as a reference
4160 cm->is_reference_frame = 0;
4161 }
4162#endif // CONFIG_EXT_REFS
4163
4164 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004165 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004166 const int idx = cm->ref_frame_map[ref];
4167 RefBuffer *const ref_frame = &cm->frame_refs[i];
4168 ref_frame->idx = idx;
4169 ref_frame->buf = &frame_bufs[idx].buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07004170 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004171#if CONFIG_REFERENCE_BUFFER
4172 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004173 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4174 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4175 int delta_frame_id_minus1 = aom_rb_read_literal(rb, diff_len);
4176 int ref_frame_id =
4177 ((cm->current_frame_id - (delta_frame_id_minus1 + 1) +
4178 (1 << frame_id_length)) %
4179 (1 << frame_id_length));
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004180 /* Compare values derived from delta_frame_id_minus1 and
4181 * refresh_frame_flags. Also, check valid for referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004182 if (ref_frame_id != cm->ref_frame_id[ref] ||
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004183 cm->valid_for_referencing[ref] == 0)
4184 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4185 "Reference buffer frame ID mismatch");
4186 }
4187#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004188 }
4189
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004190#if CONFIG_FRAME_SIZE
4191 if (cm->error_resilient_mode == 0) {
4192 setup_frame_size_with_refs(cm, rb);
4193 } else {
4194 setup_frame_size(cm, rb);
4195 }
4196#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004197 setup_frame_size_with_refs(cm, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004198#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004199
Yaowu Xuf883b422016-08-30 14:01:10 -07004200 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Angie Chiang5678ad92016-11-21 09:38:40 -08004201 cm->interp_filter = read_frame_interp_filter(rb);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004202#if CONFIG_TEMPMV_SIGNALING
4203 if (!cm->error_resilient_mode) {
4204 cm->use_prev_frame_mvs = aom_rb_read_bit(rb);
4205 }
4206#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004207 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4208 RefBuffer *const ref_buf = &cm->frame_refs[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07004209#if CONFIG_AOM_HIGHBITDEPTH
4210 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004211 &ref_buf->sf, ref_buf->buf->y_crop_width,
4212 ref_buf->buf->y_crop_height, cm->width, cm->height,
4213 cm->use_highbitdepth);
4214#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004215 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004216 &ref_buf->sf, ref_buf->buf->y_crop_width,
4217 ref_buf->buf->y_crop_height, cm->width, cm->height);
4218#endif
4219 }
4220 }
4221 }
Fangwen Fu8d164de2016-12-14 13:40:54 -08004222#if CONFIG_TEMPMV_SIGNALING
4223 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
4224#endif
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004225
4226#if CONFIG_REFERENCE_BUFFER
4227 if (pbi->seq_params.frame_id_numbers_present_flag) {
4228 /* If bitmask is set, update reference frame id values and
4229 mark frames as valid for reference */
4230 int refresh_frame_flags =
4231 cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
4232 for (i = 0; i < REF_FRAMES; i++) {
4233 if ((refresh_frame_flags >> i) & 1) {
4234 cm->ref_frame_id[i] = cm->current_frame_id;
4235 cm->valid_for_referencing[i] = 1;
4236 }
4237 }
4238 }
4239#endif
4240
Yaowu Xuf883b422016-08-30 14:01:10 -07004241#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004242 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
4243#endif
4244 get_frame_new_buffer(cm)->color_space = cm->color_space;
4245 get_frame_new_buffer(cm)->color_range = cm->color_range;
4246 get_frame_new_buffer(cm)->render_width = cm->render_width;
4247 get_frame_new_buffer(cm)->render_height = cm->render_height;
4248
4249 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004250 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004251 "Keyframe / intra-only frame required to reset decoder"
4252 " state");
4253 }
4254
4255 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004256 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004257 ? REFRESH_FRAME_CONTEXT_FORWARD
4258 : REFRESH_FRAME_CONTEXT_BACKWARD;
4259 } else {
4260 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
4261 }
4262
Yaowu Xuf883b422016-08-30 14:01:10 -07004263 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07004264 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07004265 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004266
4267 // Generate next_ref_frame_map.
4268 lock_buffer_pool(pool);
4269 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
4270 if (mask & 1) {
4271 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
4272 ++frame_bufs[cm->new_fb_idx].ref_count;
4273 } else {
4274 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4275 }
4276 // Current thread holds the reference frame.
4277 if (cm->ref_frame_map[ref_index] >= 0)
4278 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4279 ++ref_index;
4280 }
4281
4282 for (; ref_index < REF_FRAMES; ++ref_index) {
4283 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4284
4285 // Current thread holds the reference frame.
4286 if (cm->ref_frame_map[ref_index] >= 0)
4287 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4288 }
4289 unlock_buffer_pool(pool);
4290 pbi->hold_ref_buf = 1;
4291
4292 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07004293 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004294
4295#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07004296 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004297#else
4298 set_sb_size(cm, BLOCK_64X64);
4299#endif // CONFIG_EXT_PARTITION
4300
4301 setup_loopfilter(cm, rb);
Jean-Marc Valin01435132017-02-18 14:12:53 -05004302#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01004303 setup_cdef(cm, rb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004304#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004305#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004306 decode_restoration_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004307#endif // CONFIG_LOOP_RESTORATION
4308 setup_quantization(cm, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004309#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004310 xd->bd = (int)cm->bit_depth;
4311#endif
4312
hui su0d103572017-03-01 17:58:01 -08004313#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuf883b422016-08-30 14:01:10 -07004314 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004315 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4316 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4317 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4318 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
4319 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4320 }
hui su0d103572017-03-01 17:58:01 -08004321#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004322
4323 setup_segmentation(cm, rb);
4324
Arild Fuldseth07441162016-08-15 15:07:52 +02004325#if CONFIG_DELTA_Q
4326 {
4327 struct segmentation *const seg = &cm->seg;
4328 int segment_quantizer_active = 0;
4329 for (i = 0; i < MAX_SEGMENTS; i++) {
4330 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
4331 segment_quantizer_active = 1;
4332 }
4333 }
4334
Thomas Daviesf6936102016-09-05 16:51:31 +01004335 cm->delta_q_res = 1;
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01004336 if (segment_quantizer_active == 0 && cm->base_qindex > 0) {
Arild Fuldseth07441162016-08-15 15:07:52 +02004337 cm->delta_q_present_flag = aom_rb_read_bit(rb);
4338 } else {
4339 cm->delta_q_present_flag = 0;
4340 }
4341 if (cm->delta_q_present_flag) {
4342 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01004343 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Arild Fuldseth07441162016-08-15 15:07:52 +02004344 }
4345 }
4346#endif
4347
Urvang Joshi454280d2016-10-14 16:51:44 -07004348 for (i = 0; i < MAX_SEGMENTS; ++i) {
4349 const int qindex = cm->seg.enabled
4350 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
4351 : cm->base_qindex;
4352 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
4353 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4354 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004355 }
4356
4357 setup_segmentation_dequant(cm);
Yue Cheneeacc4c2017-01-17 17:29:17 -08004358 cm->tx_mode = read_tx_mode(cm, xd, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004359 cm->reference_mode = read_frame_reference_mode(cm, rb);
4360
Sarah Parkere68a3e42017-02-16 14:03:24 -08004361#if CONFIG_EXT_TX
4362 cm->reduced_tx_set_used = aom_rb_read_bit(rb);
4363#endif // CONFIG_EXT_TX
4364
Yaowu Xuc27fc142016-08-22 16:08:15 -07004365 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004366 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004367
4368 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07004369 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004370 "Invalid header size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004371 return sz;
4372}
4373
4374#if CONFIG_EXT_TX
Thomas9ac55082016-09-23 18:04:17 +01004375#if !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuf883b422016-08-30 14:01:10 -07004376static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004377 int i, j, k;
4378 int s;
4379 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004380 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004381 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4382 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004383 for (j = 0; j < num_ext_tx_set[ext_tx_set_type_inter[s]] - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004384 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004385 }
4386 }
4387 }
4388
4389 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004390 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004391 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4392 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
4393 for (j = 0; j < INTRA_MODES; ++j)
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004394 for (k = 0; k < num_ext_tx_set[ext_tx_set_type_intra[s]] - 1; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004395 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
4396 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004397 }
4398 }
4399 }
4400}
Thomas9ac55082016-09-23 18:04:17 +01004401#endif // !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004402#else
4403
Yaowu Xuc27fc142016-08-22 16:08:15 -07004404#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004405#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07004406static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004407 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -07004408 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004409 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
Jingning Hanfeb517c2016-12-21 16:02:07 -08004410 for (j = TX_8X8; j < TX_SIZES; ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004411 av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004412 }
4413 }
4414 }
4415}
4416#endif // CONFIG_SUPERTX
4417
4418#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +00004419static void read_global_motion_params(WarpedMotionParams *params,
Yaowu Xuf883b422016-08-30 14:01:10 -07004420 aom_prob *probs, aom_reader *r) {
David Barkercf3d0b02016-11-10 10:14:49 +00004421 TransformationType type =
Michael Bebenita6048d052016-08-25 14:40:54 -07004422 aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004423 set_default_gmparams(params);
David Barkercf3d0b02016-11-10 10:14:49 +00004424 params->wmtype = type;
4425 switch (type) {
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004426 case HOMOGRAPHY:
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004427 case HORTRAPEZOID:
4428 case VERTRAPEZOID:
4429 if (type != HORTRAPEZOID)
4430 params->wmmat[6] =
4431 aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
4432 GM_ROW3HOMO_DECODE_FACTOR;
4433 if (type != VERTRAPEZOID)
4434 params->wmmat[7] =
4435 aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
4436 GM_ROW3HOMO_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004437 case AFFINE:
4438 case ROTZOOM:
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004439 params->wmmat[2] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004440 GM_ALPHA_DECODE_FACTOR +
David Barkercf3d0b02016-11-10 10:14:49 +00004441 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004442 if (type != VERTRAPEZOID)
4443 params->wmmat[3] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004444 GM_ALPHA_DECODE_FACTOR;
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004445 if (type >= AFFINE) {
4446 if (type != HORTRAPEZOID)
4447 params->wmmat[4] =
4448 aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4449 GM_ALPHA_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004450 params->wmmat[5] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4451 GM_ALPHA_DECODE_FACTOR +
4452 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004453 } else {
David Barkercf3d0b02016-11-10 10:14:49 +00004454 params->wmmat[4] = -params->wmmat[3];
4455 params->wmmat[5] = params->wmmat[2];
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004456 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004457 // fallthrough intended
David Barkercf3d0b02016-11-10 10:14:49 +00004458 case TRANSLATION:
4459 params->wmmat[0] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4460 GM_TRANS_DECODE_FACTOR;
4461 params->wmmat[1] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4462 GM_TRANS_DECODE_FACTOR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004463 break;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004464 case IDENTITY: break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004465 default: assert(0);
4466 }
4467}
4468
Yaowu Xuf883b422016-08-30 14:01:10 -07004469static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004470 int frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004471 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
4472 read_global_motion_params(&cm->global_motion[frame],
4473 cm->fc->global_motion_types_prob, r);
Sarah Parkere5299862016-08-16 14:57:37 -07004474 /*
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004475 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4476 frame, cm->current_video_frame, cm->show_frame,
David Barkercf3d0b02016-11-10 10:14:49 +00004477 cm->global_motion[frame].wmmat[0],
4478 cm->global_motion[frame].wmmat[1],
4479 cm->global_motion[frame].wmmat[2],
4480 cm->global_motion[frame].wmmat[3]);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004481 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004482 }
4483}
4484#endif // CONFIG_GLOBAL_MOTION
4485
Yaowu Xuf883b422016-08-30 14:01:10 -07004486static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004487 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004488 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004489#if CONFIG_SUPERTX
4490 MACROBLOCKD *const xd = &pbi->mb;
4491#endif
4492 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07004493 aom_reader r;
Yaowu Xu8af861b2016-11-01 12:12:11 -07004494 int k, i;
hui sub4e25d22017-03-09 15:32:30 -08004495#if !CONFIG_EC_ADAPT
Yaowu Xu8af861b2016-11-01 12:12:11 -07004496 int j;
4497#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004498
Alex Converse2cdf0d82016-12-13 13:53:09 -08004499#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08004500 r.window_size = 1 << cm->ans_window_size_log2;
Alex Converse2cdf0d82016-12-13 13:53:09 -08004501#endif
Alex Converse346440b2017-01-03 13:47:37 -08004502 if (aom_reader_init(&r, data, partition_size, pbi->decrypt_cb,
4503 pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07004504 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004505 "Failed to allocate bool decoder 0");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004506
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004507#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004508 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4509 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4510 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4511 av1_alloc_restoration_buffers(cm);
4512 decode_restoration(cm, &r);
4513 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004514#endif
4515
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004516#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -07004517 if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004518#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004519
Angie Chiang800df032017-03-22 11:14:12 -07004520#if CONFIG_LV_MAP
4521 av1_read_txb_probs(fc, cm->tx_mode, &r);
4522#else // CONFIG_LV_MAP
Yushin Cho77bba8d2016-11-04 16:36:56 -07004523#if !CONFIG_PVQ
Alex Conversea9598cd2017-02-03 14:18:05 -08004524#if !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004525 read_coef_probs(fc, cm->tx_mode, &r);
Angie Chiang800df032017-03-22 11:14:12 -07004526#endif // !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Angie Chiang7d7ead92017-03-22 10:35:51 -07004527#endif // !CONFIG_PVQ
Angie Chiang800df032017-03-22 11:14:12 -07004528#endif // CONFIG_LV_MAP
4529
Yaowu Xuc27fc142016-08-22 16:08:15 -07004530#if CONFIG_VAR_TX
4531 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004532 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
Yushin Cho77bba8d2016-11-04 16:36:56 -07004533#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004534 for (k = 0; k < SKIP_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004535 av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004536
Thomas Daviesd6ee8a82017-03-02 14:42:50 +00004537#if CONFIG_DELTA_Q && !CONFIG_EC_ADAPT
Thomas Davies665cd702017-03-02 10:20:30 +00004538 for (k = 0; k < DELTA_Q_PROBS; ++k)
Thomas Daviesf6936102016-09-05 16:51:31 +01004539 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
4540#endif
4541
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004542#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004543 if (cm->seg.enabled && cm->seg.update_map) {
4544 if (cm->seg.temporal_update) {
4545 for (k = 0; k < PREDICTION_PROBS; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004546 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004547 }
4548 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004549 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004550 }
4551
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004552 for (j = 0; j < INTRA_MODES; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004553 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004554 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004555 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004556
4557#if CONFIG_EXT_PARTITION_TYPES
Alex Converse4e18d402017-03-14 15:36:38 -07004558 for (j = 0; j < PARTITION_PLOFFSET; ++j)
4559 for (i = 0; i < PARTITION_TYPES - 1; ++i)
4560 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
4561 for (; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004562 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004563 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004564#else
Alex Converse55c6bde2017-01-12 15:55:31 -08004565 for (j = 0; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004566 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004567 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004568#endif // CONFIG_EXT_PARTITION_TYPES
hui su9aa97492017-01-26 16:46:01 -08004569
Alex Converse55c6bde2017-01-12 15:55:31 -08004570#if CONFIG_UNPOISON_PARTITION_CTX
4571 for (; j < PARTITION_CONTEXTS_PRIMARY + PARTITION_BLOCK_SIZES; ++j)
4572 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_VERT], ACCT_STR);
4573 for (; j < PARTITION_CONTEXTS_PRIMARY + 2 * PARTITION_BLOCK_SIZES; ++j)
4574 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_HORZ], ACCT_STR);
4575#endif // CONFIG_UNPOISON_PARTITION_CTX
hui su9aa97492017-01-26 16:46:01 -08004576
4577#if CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004578 for (i = 0; i < INTRA_FILTERS + 1; ++i)
4579 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004580 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
hui su9aa97492017-01-26 16:46:01 -08004581#endif // CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
hui sub4e25d22017-03-09 15:32:30 -08004582#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004583
4584 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004585 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Nathan E. Egge10ba2be2016-11-16 09:44:26 -05004586#if CONFIG_EC_MULTISYMBOL
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00004587 av1_copy(cm->fc->kf_y_cdf, av1_kf_y_mode_cdf);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004588#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004589#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004590 for (k = 0; k < INTRA_MODES; k++)
Thomas Davies6519beb2016-10-19 14:46:07 +01004591 for (j = 0; j < INTRA_MODES; j++)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004592 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004593 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004594#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004595 } else {
4596#if !CONFIG_REF_MV
4597 nmv_context *const nmvc = &fc->nmvc;
4598#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004599 read_inter_mode_probs(fc, &r);
4600
4601#if CONFIG_EXT_INTER
4602 read_inter_compound_mode_probs(fc, &r);
4603 if (cm->reference_mode != COMPOUND_REFERENCE) {
4604 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4605 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004606 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004607 }
4608 }
4609 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4610 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004611 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004612 }
4613 for (i = 0; i < BLOCK_SIZES; i++) {
4614 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004615 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004616 }
4617 }
4618 }
4619 if (cm->reference_mode != SINGLE_REFERENCE) {
4620 for (i = 0; i < BLOCK_SIZES; i++) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08004621 for (j = 0; j < COMPOUND_TYPES - 1; j++) {
4622 av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004623 }
4624 }
4625 }
4626#endif // CONFIG_EXT_INTER
4627
Yue Chencb60b182016-10-13 15:18:22 -07004628#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004629 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
Yue Chencb60b182016-10-13 15:18:22 -07004630 for (j = 0; j < MOTION_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004631 av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004632 }
Yue Chencb60b182016-10-13 15:18:22 -07004633#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004634
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004635#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004636 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004637#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004638
4639 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004640 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004641
4642 if (cm->reference_mode != SINGLE_REFERENCE)
4643 setup_compound_reference_mode(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004644 read_frame_reference_mode_probs(cm, &r);
4645
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004646#if !CONFIG_EC_ADAPT
Nathan E. Egge5710c722016-09-08 10:01:16 -04004647 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004648 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004649 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
Nathan E. Egge5710c722016-09-08 10:01:16 -04004650 }
Thomas9ac55082016-09-23 18:04:17 +01004651#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004652
4653#if CONFIG_REF_MV
4654 for (i = 0; i < NMV_CONTEXTS; ++i)
4655 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
4656#else
4657 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
4658#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004659#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004660 read_ext_tx_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004661#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004662#if CONFIG_SUPERTX
4663 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
4664#endif
4665#if CONFIG_GLOBAL_MOTION
4666 read_global_motion(cm, &r);
Thomas Davies6519beb2016-10-19 14:46:07 +01004667#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004668 }
Thomas Davies028b57f2017-02-22 16:42:11 +00004669#if CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Thomas Davies87aeeb82017-02-17 00:19:40 +00004670#if CONFIG_NEW_TOKENSET
4671 av1_coef_head_cdfs(fc);
4672#endif
4673 /* Make tail distribution from head */
Thomas Davies6519beb2016-10-19 14:46:07 +01004674 av1_coef_pareto_cdfs(fc);
David Barker599dfd02016-11-10 13:20:12 +00004675#if CONFIG_REF_MV
4676 for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
4677#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004678 av1_set_mv_cdfs(&fc->nmvc);
David Barker599dfd02016-11-10 13:20:12 +00004679#endif
Thomas Davies6519beb2016-10-19 14:46:07 +01004680 av1_set_mode_cdfs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00004681#endif // CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004682
Yaowu Xuf883b422016-08-30 14:01:10 -07004683 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004684}
4685
4686#ifdef NDEBUG
4687#define debug_check_frame_counts(cm) (void)0
4688#else // !NDEBUG
4689// Counts should only be incremented when frame_parallel_decoding_mode and
4690// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07004691static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004692 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07004693 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004694 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
4695 cm->error_resilient_mode);
4696 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
4697 sizeof(cm->counts.y_mode)));
4698 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
4699 sizeof(cm->counts.uv_mode)));
4700 assert(!memcmp(cm->counts.partition, zero_counts.partition,
4701 sizeof(cm->counts.partition)));
4702 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
4703 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
4704 sizeof(cm->counts.eob_branch)));
Thomas Daviesab780672017-02-01 12:07:29 +00004705#if CONFIG_EC_MULTISYMBOL
4706 assert(!memcmp(cm->counts.blockz_count, zero_counts.blockz_count,
4707 sizeof(cm->counts.blockz_count)));
4708#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004709 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
4710 sizeof(cm->counts.switchable_interp)));
4711 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
4712 sizeof(cm->counts.inter_mode)));
4713#if CONFIG_EXT_INTER
4714 assert(!memcmp(cm->counts.inter_compound_mode,
4715 zero_counts.inter_compound_mode,
4716 sizeof(cm->counts.inter_compound_mode)));
4717 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
4718 sizeof(cm->counts.interintra)));
4719 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
4720 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08004721 assert(!memcmp(cm->counts.compound_interinter,
4722 zero_counts.compound_interinter,
4723 sizeof(cm->counts.compound_interinter)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004724#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07004725#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
4726 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
4727 sizeof(cm->counts.motion_mode)));
4728#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004729 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
4730 sizeof(cm->counts.intra_inter)));
4731 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
4732 sizeof(cm->counts.comp_inter)));
4733 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
4734 sizeof(cm->counts.single_ref)));
4735 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
4736 sizeof(cm->counts.comp_ref)));
4737#if CONFIG_EXT_REFS
4738 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
4739 sizeof(cm->counts.comp_bwdref)));
4740#endif // CONFIG_EXT_REFS
4741 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
4742 sizeof(cm->counts.tx_size)));
4743 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
4744#if CONFIG_REF_MV
4745 assert(
4746 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
4747 assert(
4748 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
4749#else
4750 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
4751#endif
4752 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
4753 sizeof(cm->counts.inter_ext_tx)));
4754 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
4755 sizeof(cm->counts.intra_ext_tx)));
4756}
4757#endif // NDEBUG
4758
Yaowu Xuf883b422016-08-30 14:01:10 -07004759static struct aom_read_bit_buffer *init_read_bit_buffer(
4760 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
4761 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004762 rb->bit_offset = 0;
4763 rb->error_handler = error_handler;
4764 rb->error_handler_data = &pbi->common;
4765 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004766 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004767 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
4768 rb->bit_buffer = clear_data;
4769 rb->bit_buffer_end = clear_data + n;
4770 } else {
4771 rb->bit_buffer = data;
4772 rb->bit_buffer_end = data_end;
4773 }
4774 return rb;
4775}
4776
4777//------------------------------------------------------------------------------
4778
Yaowu Xuf883b422016-08-30 14:01:10 -07004779int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
4780 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
4781 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
4782 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004783}
4784
Yaowu Xuf883b422016-08-30 14:01:10 -07004785void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
4786 int *height) {
4787 *width = aom_rb_read_literal(rb, 16) + 1;
4788 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004789}
4790
Yaowu Xuf883b422016-08-30 14:01:10 -07004791BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
4792 int profile = aom_rb_read_bit(rb);
4793 profile |= aom_rb_read_bit(rb) << 1;
4794 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004795 return (BITSTREAM_PROFILE)profile;
4796}
4797
Thomas Davies028b57f2017-02-22 16:42:11 +00004798#if CONFIG_EC_ADAPT
4799static void make_update_tile_list_dec(AV1Decoder *pbi, const int tile_rows,
4800 const int tile_cols,
4801 FRAME_CONTEXT *ec_ctxs[]) {
4802 int i;
4803 for (i = 0; i < tile_rows * tile_cols; ++i)
4804 ec_ctxs[i] = &pbi->tile_data[i].tctx;
4805}
4806#endif
4807
Yaowu Xuf883b422016-08-30 14:01:10 -07004808void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
4809 const uint8_t *data_end, const uint8_t **p_data_end) {
4810 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004811 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07004812 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004813 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004814 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004815 size_t first_partition_size;
4816 YV12_BUFFER_CONFIG *new_fb;
4817
4818#if CONFIG_BITSTREAM_DEBUG
4819 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
4820#endif
4821
4822 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004823 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Thomas Davies72712e62016-11-09 12:17:51 +00004824#if CONFIG_TILE_GROUPS
4825 pbi->first_partition_size = first_partition_size;
4826 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
4827#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004828 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004829 xd->cur_buf = new_fb;
4830#if CONFIG_GLOBAL_MOTION
4831 xd->global_motion = cm->global_motion;
4832#endif // CONFIG_GLOBAL_MOTION
4833
4834 if (!first_partition_size) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004835 // showing a frame directly
4836 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004837 return;
4838 }
4839
Yaowu Xuf883b422016-08-30 14:01:10 -07004840 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004841 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07004842 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004843 "Truncated packet or corrupt header length");
4844
Jingning Han24e0a182016-11-20 22:34:12 -08004845#if CONFIG_REF_MV
Dengca8d24d2016-10-17 14:06:35 +08004846 cm->setup_mi(cm);
4847#endif
4848
Fangwen Fu8d164de2016-12-14 13:40:54 -08004849#if CONFIG_TEMPMV_SIGNALING
4850 if (cm->use_prev_frame_mvs) {
4851 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4852 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4853 assert(!cm->error_resilient_mode &&
4854 cm->width == last_fb_ref_buf->buf->y_width &&
4855 cm->height == last_fb_ref_buf->buf->y_height &&
4856 !cm->prev_frame->intra_only);
4857 }
4858#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004859 cm->use_prev_frame_mvs =
4860 !cm->error_resilient_mode && cm->width == cm->last_width &&
4861 cm->height == cm->last_height && !cm->last_intra_only &&
4862 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004863#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004864#if CONFIG_EXT_REFS
4865 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
4866 // show_exisiting_frame=1, nor can it take a frame not used as
4867 // a reference, it is probable that by the time it is being
4868 // referred to, the frame buffer it originally points to may
4869 // already get expired and have been reassigned to the current
4870 // newly coded frame. Hence, we need to check whether this is
4871 // the case, and if yes, we have 2 choices:
4872 // (1) Simply disable the use of previous frame mvs; or
4873 // (2) Have cm->prev_frame point to one reference frame buffer,
4874 // e.g. LAST_FRAME.
4875 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
4876 // Reassign the LAST_FRAME buffer to cm->prev_frame.
4877 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4878 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4879 }
4880#endif // CONFIG_EXT_REFS
4881
Yaowu Xuf883b422016-08-30 14:01:10 -07004882 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004883
4884 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
4885 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07004886 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004887 "Uninitialized entropy context.");
4888
Yaowu Xuf883b422016-08-30 14:01:10 -07004889 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004890
4891 xd->corrupted = 0;
4892 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
4893 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07004894 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004895 "Decode failed. Frame data header is corrupted.");
4896
4897 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004898 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004899 }
4900
4901 // If encoded in frame parallel mode, frame context is ready after decoding
4902 // the frame header.
4903 if (cm->frame_parallel_decode &&
4904 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004905 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004906 FrameWorkerData *const frame_worker_data = worker->data1;
4907 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
4908 context_updated = 1;
4909 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4910 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004911 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004912 pbi->cur_buf->row = -1;
4913 pbi->cur_buf->col = -1;
4914 frame_worker_data->frame_context_ready = 1;
4915 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07004916 av1_frameworker_signal_stats(worker);
4917 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004918 }
4919
hui su0d103572017-03-01 17:58:01 -08004920#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07004921 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004922 cm->coef_probs_update_idx = 0;
hui su0d103572017-03-01 17:58:01 -08004923#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004924
4925 if (pbi->max_threads > 1
4926#if CONFIG_EXT_TILE
4927 && pbi->dec_tile_col < 0 // Decoding all columns
4928#endif // CONFIG_EXT_TILE
4929 && cm->tile_cols > 1) {
4930 // Multi-threaded tile decoder
4931 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
4932 if (!xd->corrupted) {
4933 if (!cm->skip_loop_filter) {
4934 // If multiple threads are used to decode tiles, then we use those
4935 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07004936 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
4937 0, 0, pbi->tile_workers, pbi->num_tile_workers,
4938 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004939 }
4940 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004941 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004942 "Decode failed. Frame data is corrupted.");
4943 }
4944 } else {
4945 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
4946 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004947
Jean-Marc Valin01435132017-02-18 14:12:53 -05004948#if CONFIG_CDEF
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04004949 if (!cm->skip_loop_filter) {
Jean-Marc Valine9f77422017-03-22 17:09:51 -04004950 av1_cdef_frame(&pbi->cur_buf->buf, cm, &pbi->mb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004951 }
Debargha Mukherjee00c54332017-03-03 15:44:17 -08004952#endif // CONFIG_CDEF
4953
4954#if CONFIG_LOOP_RESTORATION
4955 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4956 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4957 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4958 av1_loop_restoration_frame(new_fb, cm, cm->rst_info, 7, 0, NULL);
4959 }
4960#endif // CONFIG_LOOP_RESTORATION
Thomas Daedef56859f2016-04-19 16:57:24 -07004961
Yaowu Xuc27fc142016-08-22 16:08:15 -07004962 if (!xd->corrupted) {
4963 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Thomas Davies028b57f2017-02-22 16:42:11 +00004964#if CONFIG_EC_ADAPT
4965 FRAME_CONTEXT *tile_ctxs[MAX_TILE_ROWS * MAX_TILE_COLS];
4966 make_update_tile_list_dec(pbi, cm->tile_rows, cm->tile_cols, tile_ctxs);
4967#endif
4968
hui su0d103572017-03-01 17:58:01 -08004969#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004970 cm->partial_prob_update = 0;
hui su0d103572017-03-01 17:58:01 -08004971#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07004972 av1_adapt_coef_probs(cm);
4973 av1_adapt_intra_frame_probs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00004974#if CONFIG_EC_ADAPT
4975 av1_average_tile_coef_cdfs(pbi->common.fc, tile_ctxs,
4976 cm->tile_rows * cm->tile_cols);
4977 av1_average_tile_intra_cdfs(pbi->common.fc, tile_ctxs,
4978 cm->tile_rows * cm->tile_cols);
4979#endif
hui suff0da2b2017-03-07 15:51:37 -08004980#if CONFIG_ADAPT_SCAN
4981 av1_adapt_scan_order(cm);
4982#endif // CONFIG_ADAPT_SCAN
Yaowu Xuc27fc142016-08-22 16:08:15 -07004983
4984 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004985 av1_adapt_inter_frame_probs(cm);
4986 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Thomas Davies028b57f2017-02-22 16:42:11 +00004987#if CONFIG_EC_ADAPT
4988 av1_average_tile_inter_cdfs(&pbi->common, pbi->common.fc, tile_ctxs,
4989 cm->tile_rows * cm->tile_cols);
4990 av1_average_tile_mv_cdfs(pbi->common.fc, tile_ctxs,
4991 cm->tile_rows * cm->tile_cols);
4992#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004993 }
4994 } else {
4995 debug_check_frame_counts(cm);
4996 }
4997 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004998 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004999 "Decode failed. Frame data is corrupted.");
5000 }
5001
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05005002#if CONFIG_INSPECTION
5003 if (pbi->inspect_cb != NULL) {
5004 (*pbi->inspect_cb)(pbi, pbi->inspect_ctx);
5005 }
5006#endif
5007
Yaowu Xuc27fc142016-08-22 16:08:15 -07005008 // Non frame parallel update frame context here.
5009 if (!cm->error_resilient_mode && !context_updated)
5010 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
5011}