blob: 786638035c65e7283db9e82f70133ab2f4336639 [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"
Jingning Han1aab8182016-06-03 11:09:06 -070054#include "av1/decoder/detokenize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070055#include "av1/decoder/dsubexp.h"
56
Yue Chen69f18e12016-09-08 14:48:15 -070057#if CONFIG_WARPED_MOTION
58#include "av1/common/warped_motion.h"
59#endif // CONFIG_WARPED_MOTION
60
Yaowu Xuf883b422016-08-30 14:01:10 -070061#define MAX_AV1_HEADER_SIZE 80
Michael Bebenita6048d052016-08-25 14:40:54 -070062#define ACCT_STR __func__
Yaowu Xuc27fc142016-08-22 16:08:15 -070063
Yushin Cho77bba8d2016-11-04 16:36:56 -070064#if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -070065#include "av1/common/partition.h"
hui suff0da2b2017-03-07 15:51:37 -080066#include "av1/common/pvq.h"
67#include "av1/common/scan.h"
Yushin Cho77bba8d2016-11-04 16:36:56 -070068#include "av1/decoder/decint.h"
hui suff0da2b2017-03-07 15:51:37 -080069#include "av1/decoder/pvq_decoder.h"
70#include "av1/encoder/encodemb.h"
Yushin Cho77bba8d2016-11-04 16:36:56 -070071#include "av1/encoder/hybrid_fwd_txfm.h"
72#endif
73
Thomas Davies80188d12016-10-26 16:08:35 -070074static struct aom_read_bit_buffer *init_read_bit_buffer(
75 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
76 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]);
77static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
78 size_t partition_size);
79static size_t read_uncompressed_header(AV1Decoder *pbi,
80 struct aom_read_bit_buffer *rb);
81
Yaowu Xuf883b422016-08-30 14:01:10 -070082static int is_compound_reference_allowed(const AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070083 int i;
84 if (frame_is_intra_only(cm)) return 0;
85 for (i = 1; i < INTER_REFS_PER_FRAME; ++i)
86 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
87
88 return 0;
89}
90
Yaowu Xuf883b422016-08-30 14:01:10 -070091static void setup_compound_reference_mode(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070092#if CONFIG_EXT_REFS
93 cm->comp_fwd_ref[0] = LAST_FRAME;
94 cm->comp_fwd_ref[1] = LAST2_FRAME;
95 cm->comp_fwd_ref[2] = LAST3_FRAME;
96 cm->comp_fwd_ref[3] = GOLDEN_FRAME;
97
98 cm->comp_bwd_ref[0] = BWDREF_FRAME;
99 cm->comp_bwd_ref[1] = ALTREF_FRAME;
100#else
101 if (cm->ref_frame_sign_bias[LAST_FRAME] ==
102 cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
103 cm->comp_fixed_ref = ALTREF_FRAME;
104 cm->comp_var_ref[0] = LAST_FRAME;
105 cm->comp_var_ref[1] = GOLDEN_FRAME;
106 } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
107 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
108 cm->comp_fixed_ref = GOLDEN_FRAME;
109 cm->comp_var_ref[0] = LAST_FRAME;
110 cm->comp_var_ref[1] = ALTREF_FRAME;
111 } else {
112 cm->comp_fixed_ref = LAST_FRAME;
113 cm->comp_var_ref[0] = GOLDEN_FRAME;
114 cm->comp_var_ref[1] = ALTREF_FRAME;
115 }
116#endif // CONFIG_EXT_REFS
117}
118
119static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
120 return len != 0 && len <= (size_t)(end - start);
121}
122
Yaowu Xuf883b422016-08-30 14:01:10 -0700123static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
124 const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700125 return data > max ? max : data;
126}
127
Yue Cheneeacc4c2017-01-17 17:29:17 -0800128static TX_MODE read_tx_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
129 struct aom_read_bit_buffer *rb) {
130 int i, all_lossless = 1;
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800131#if CONFIG_TX64X64
Yue Cheneeacc4c2017-01-17 17:29:17 -0800132 TX_MODE tx_mode;
133#endif
134
135 if (cm->seg.enabled) {
136 for (i = 0; i < MAX_SEGMENTS; ++i) {
137 if (!xd->lossless[i]) {
138 all_lossless = 0;
139 break;
140 }
141 }
142 } else {
143 all_lossless = xd->lossless[0];
144 }
145
146 if (all_lossless) return ONLY_4X4;
147#if CONFIG_TX64X64
148 tx_mode = aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800149 if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
150 return tx_mode;
151#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700152 return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800153#endif // CONFIG_TX64X64
Yaowu Xuc27fc142016-08-22 16:08:15 -0700154}
155
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -0500156#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -0700157static void read_tx_size_probs(FRAME_CONTEXT *fc, aom_reader *r) {
158 int i, j, k;
159 for (i = 0; i < MAX_TX_DEPTH; ++i)
160 for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
161 for (k = 0; k < i + 1; ++k)
162 av1_diff_update_prob(r, &fc->tx_size_probs[i][j][k], ACCT_STR);
163}
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -0500164#endif
Yaowu Xuefc75352016-10-31 09:46:42 -0700165
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400166#if !CONFIG_EC_ADAPT
Yaowu Xuf883b422016-08-30 14:01:10 -0700167static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700168 int i, j;
Nathan E. Egge4947c292016-04-26 11:37:06 -0400169 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700170 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700171 av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i], ACCT_STR);
Nathan E. Egge4947c292016-04-26 11:37:06 -0400172 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700173}
Thomas Davies6519beb2016-10-19 14:46:07 +0100174#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700175
Yaowu Xuf883b422016-08-30 14:01:10 -0700176static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700177#if CONFIG_REF_MV
Yaowu Xu8af861b2016-11-01 12:12:11 -0700178 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700179 for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700180 av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700181 for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700182 av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700183 for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700184 av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700185 for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700186 av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700187#if CONFIG_EXT_INTER
Michael Bebenita6048d052016-08-25 14:40:54 -0700188 av1_diff_update_prob(r, &fc->new2mv_prob, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700189#endif // CONFIG_EXT_INTER
190#else
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400191#if !CONFIG_EC_ADAPT
Yaowu Xu8af861b2016-11-01 12:12:11 -0700192 int i, j;
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400193 for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700194 for (j = 0; j < INTER_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700195 av1_diff_update_prob(r, &fc->inter_mode_probs[i][j], ACCT_STR);
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400196 }
Yaowu Xu8af861b2016-11-01 12:12:11 -0700197#else
198 (void)fc;
199 (void)r;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700200#endif
Thomas Davies6519beb2016-10-19 14:46:07 +0100201#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700202}
203
204#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700205static void read_inter_compound_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700206 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -0700207 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700208 for (j = 0; j < INTER_MODE_CONTEXTS; ++j) {
209 for (i = 0; i < INTER_COMPOUND_MODES - 1; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700210 av1_diff_update_prob(r, &fc->inter_compound_mode_probs[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700211 }
212 }
213 }
214}
215#endif // CONFIG_EXT_INTER
Yaowu Xu17fd2f22016-11-17 18:23:28 -0800216#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100217#if !CONFIG_EXT_TX
218static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
219 int i, j, k;
220 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
221 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
222 for (j = 0; j < TX_TYPES; ++j) {
223 for (k = 0; k < TX_TYPES - 1; ++k)
224 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[i][j][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100225 }
226 }
227 }
228 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
229 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
230 for (k = 0; k < TX_TYPES - 1; ++k)
231 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[i][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100232 }
233 }
234}
235#endif
236#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700237
238static REFERENCE_MODE read_frame_reference_mode(
Yaowu Xuf883b422016-08-30 14:01:10 -0700239 const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700240 if (is_compound_reference_allowed(cm)) {
Zoe Liub05e5d12017-02-07 14:32:53 -0800241#if CONFIG_REF_ADAPT
242 return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
243#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700244 return aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700245 ? REFERENCE_MODE_SELECT
Yaowu Xuf883b422016-08-30 14:01:10 -0700246 : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
Zoe Liub05e5d12017-02-07 14:32:53 -0800247#endif // CONFIG_REF_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700248 } else {
249 return SINGLE_REFERENCE;
250 }
251}
252
Yaowu Xuf883b422016-08-30 14:01:10 -0700253static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700254 FRAME_CONTEXT *const fc = cm->fc;
255 int i, j;
256
257 if (cm->reference_mode == REFERENCE_MODE_SELECT)
258 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700259 av1_diff_update_prob(r, &fc->comp_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700260
261 if (cm->reference_mode != COMPOUND_REFERENCE) {
262 for (i = 0; i < REF_CONTEXTS; ++i) {
263 for (j = 0; j < (SINGLE_REFS - 1); ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700264 av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700265 }
266 }
267 }
268
269 if (cm->reference_mode != SINGLE_REFERENCE) {
270 for (i = 0; i < REF_CONTEXTS; ++i) {
271#if CONFIG_EXT_REFS
272 for (j = 0; j < (FWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700273 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700274 for (j = 0; j < (BWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700275 av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700276#else
277 for (j = 0; j < (COMP_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700278 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700279#endif // CONFIG_EXT_REFS
280 }
281 }
282}
283
Yaowu Xuf883b422016-08-30 14:01:10 -0700284static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700285 int i;
Michael Bebenita6048d052016-08-25 14:40:54 -0700286 for (i = 0; i < n; ++i) av1_diff_update_prob(r, &p[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700287}
288
Yaowu Xuf883b422016-08-30 14:01:10 -0700289static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Thomas9ac55082016-09-23 18:04:17 +0100290 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700291
Yaowu Xue86288d2016-10-31 15:56:38 -0700292#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100293 int j;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700294 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
295
Yaowu Xuc27fc142016-08-22 16:08:15 -0700296 for (i = 0; i < 2; ++i) {
297 nmv_component *const comp_ctx = &ctx->comps[i];
298 update_mv_probs(&comp_ctx->sign, 1, r);
299 update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
300 update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
301 update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
302 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700303 for (i = 0; i < 2; ++i) {
304 nmv_component *const comp_ctx = &ctx->comps[i];
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400305 for (j = 0; j < CLASS0_SIZE; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700306 update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400307 }
Nathan E. Eggeac499f32016-09-08 15:38:57 -0400308 update_mv_probs(comp_ctx->fp, MV_FP_SIZE - 1, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700309 }
Alex Converseaca9feb2016-10-10 11:08:10 -0700310#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700311
312 if (allow_hp) {
313 for (i = 0; i < 2; ++i) {
314 nmv_component *const comp_ctx = &ctx->comps[i];
315 update_mv_probs(&comp_ctx->class0_hp, 1, r);
316 update_mv_probs(&comp_ctx->hp, 1, r);
317 }
318 }
319}
320
321static void inverse_transform_block(MACROBLOCKD *xd, int plane,
322 const TX_TYPE tx_type,
323 const TX_SIZE tx_size, uint8_t *dst,
Jingning Han1be18782016-10-21 11:48:15 -0700324 int stride, int16_t scan_line, int eob) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700325 struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han1be18782016-10-21 11:48:15 -0700326 tran_low_t *const dqcoeff = pd->dqcoeff;
327 INV_TXFM_PARAM inv_txfm_param;
328 inv_txfm_param.tx_type = tx_type;
329 inv_txfm_param.tx_size = tx_size;
330 inv_txfm_param.eob = eob;
331 inv_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700332
Yaowu Xuf883b422016-08-30 14:01:10 -0700333#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han1be18782016-10-21 11:48:15 -0700334 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
335 inv_txfm_param.bd = xd->bd;
336 highbd_inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
337 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700338#endif // CONFIG_AOM_HIGHBITDEPTH
Jingning Han1be18782016-10-21 11:48:15 -0700339 inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
Yaowu Xuf883b422016-08-30 14:01:10 -0700340#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700341 }
Jingning Han1be18782016-10-21 11:48:15 -0700342#endif // CONFIG_AOM_HIGHBITDEPTH
343 memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700344}
345
Yushin Cho77bba8d2016-11-04 16:36:56 -0700346#if CONFIG_PVQ
Thomas Daede1dbda1b2017-02-06 16:06:29 -0800347static int av1_pvq_decode_helper(od_dec_ctx *dec, tran_low_t *ref_coeff,
348 tran_low_t *dqcoeff, int16_t *quant, int pli,
Yushin Cho77bba8d2016-11-04 16:36:56 -0700349 int bs, TX_TYPE tx_type, int xdec,
ltrudeaue1c09292017-01-20 15:42:13 -0500350 PVQ_SKIP_TYPE ac_dc_coded) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700351 unsigned int flags; // used for daala's stream analyzer.
352 int off;
353 const int is_keyframe = 0;
354 const int has_dc_skip = 1;
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800355 /*TODO(tterribe): Handle CONFIG_AOM_HIGHBITDEPTH.*/
356 int coeff_shift = 3 - get_tx_scale(bs);
357 int rounding_mask;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700358 // DC quantizer for PVQ
359 int pvq_dc_quant;
360 int lossless = (quant[0] == 0);
361 const int blk_size = tx_size_wide[bs];
362 int eob = 0;
363 int i;
Yushin Cho70669122016-12-08 09:53:14 -1000364 int use_activity_masking = dec->use_activity_masking;
Thomas Daede1dbda1b2017-02-06 16:06:29 -0800365 DECLARE_ALIGNED(16, tran_low_t, dqcoeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
366 DECLARE_ALIGNED(16, tran_low_t, ref_coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700367
Yushin Cho48f84db2016-11-07 21:20:17 -0800368 od_coeff ref_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
369 od_coeff out_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700370
371 od_raster_to_coding_order(ref_coeff_pvq, blk_size, tx_type, ref_coeff,
372 blk_size);
373
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800374 assert(OD_COEFF_SHIFT >= 3);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700375 if (lossless)
376 pvq_dc_quant = 1;
377 else {
Yushin Cho70669122016-12-08 09:53:14 -1000378 if (use_activity_masking)
379 pvq_dc_quant = OD_MAXI(
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800380 1, (quant[0] << (OD_COEFF_SHIFT - 3)) *
Yushin Cho70669122016-12-08 09:53:14 -1000381 dec->state.pvq_qm_q4[pli][od_qm_get_index(bs, 0)] >>
382 4);
383 else
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800384 pvq_dc_quant = OD_MAXI(1, quant[0] << (OD_COEFF_SHIFT - 3));
Yushin Cho77bba8d2016-11-04 16:36:56 -0700385 }
386
387 off = od_qm_offset(bs, xdec);
388
389 // copy int16 inputs to int32
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800390 for (i = 0; i < blk_size * blk_size; i++) {
Timothy B. Terriberry4e6a8f32017-02-24 11:00:59 -0800391 ref_int32[i] =
392 AOM_SIGNED_SHL(ref_coeff_pvq[i], OD_COEFF_SHIFT - coeff_shift);
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800393 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700394
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800395 od_pvq_decode(dec, ref_int32, out_int32, quant[1] << (OD_COEFF_SHIFT - 3),
396 pli, bs, OD_PVQ_BETA[use_activity_masking][pli][bs],
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800397 OD_ROBUST_STREAM, is_keyframe, &flags, ac_dc_coded,
398 dec->state.qm + off, dec->state.qm_inv + off);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700399
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800400 if (!has_dc_skip || out_int32[0]) {
401 out_int32[0] =
Nathan E. Egge89f58762016-12-28 16:31:50 -0500402 has_dc_skip + generic_decode(dec->r, &dec->state.adapt.model_dc[pli],
403 -1, &dec->state.adapt.ex_dc[pli][bs][0], 2,
404 "dc:mag");
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800405 if (out_int32[0]) out_int32[0] *= aom_read_bit(dec->r, "dc:sign") ? -1 : 1;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700406 }
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800407 out_int32[0] = out_int32[0] * pvq_dc_quant + ref_int32[0];
408
409 // copy int32 result back to int16
410 assert(OD_COEFF_SHIFT > coeff_shift);
411 rounding_mask = (1 << (OD_COEFF_SHIFT - coeff_shift - 1)) - 1;
412 for (i = 0; i < blk_size * blk_size; i++) {
413 dqcoeff_pvq[i] = (out_int32[i] + (out_int32[i] < 0) + rounding_mask) >>
414 (OD_COEFF_SHIFT - coeff_shift);
415 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700416
417 od_coding_order_to_raster(dqcoeff, blk_size, tx_type, dqcoeff_pvq, blk_size);
418
419 eob = blk_size * blk_size;
420
421 return eob;
422}
423
ltrudeaue1c09292017-01-20 15:42:13 -0500424static PVQ_SKIP_TYPE read_pvq_skip(AV1_COMMON *cm, MACROBLOCKD *const xd,
425 int plane, TX_SIZE tx_size) {
426 // decode ac/dc coded flag. bit0: DC coded, bit1 : AC coded
427 // NOTE : we don't use 5 symbols for luma here in aom codebase,
428 // since block partition is taken care of by aom.
429 // So, only AC/DC skip info is coded
430 const int ac_dc_coded = aom_decode_cdf_adapt(
431 xd->daala_dec.r,
432 xd->daala_dec.state.adapt.skip_cdf[2 * tx_size + (plane != 0)], 4,
433 xd->daala_dec.state.adapt.skip_increment, "skip");
434 if (ac_dc_coded < 0 || ac_dc_coded > 3) {
435 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
436 "Invalid PVQ Skip Type");
437 }
438 return ac_dc_coded;
439}
440
441static int av1_pvq_decode_helper2(AV1_COMMON *cm, MACROBLOCKD *const xd,
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800442 MB_MODE_INFO *const mbmi, int plane, int row,
443 int col, TX_SIZE tx_size, TX_TYPE tx_type) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700444 struct macroblockd_plane *const pd = &xd->plane[plane];
445 // transform block size in pixels
446 int tx_blk_size = tx_size_wide[tx_size];
447 int i, j;
448 tran_low_t *pvq_ref_coeff = pd->pvq_ref_coeff;
449 const int diff_stride = tx_blk_size;
450 int16_t *pred = pd->pred;
451 tran_low_t *const dqcoeff = pd->dqcoeff;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700452 uint8_t *dst;
453 int eob;
ltrudeaue1c09292017-01-20 15:42:13 -0500454 const PVQ_SKIP_TYPE ac_dc_coded = read_pvq_skip(cm, xd, plane, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700455
456 eob = 0;
457 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
458
Yushin Cho77bba8d2016-11-04 16:36:56 -0700459 if (ac_dc_coded) {
460 int xdec = pd->subsampling_x;
461 int seg_id = mbmi->segment_id;
462 int16_t *quant;
463 FWD_TXFM_PARAM fwd_txfm_param;
Yaowu Xufc1b2132016-11-07 15:16:15 -0800464 // ToDo(yaowu): correct this with optimal number from decoding process.
465 const int max_scan_line = tx_size_2d[tx_size];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700466
467 for (j = 0; j < tx_blk_size; j++)
468 for (i = 0; i < tx_blk_size; i++) {
469 pred[diff_stride * j + i] = dst[pd->dst.stride * j + i];
470 }
471
472 fwd_txfm_param.tx_type = tx_type;
473 fwd_txfm_param.tx_size = tx_size;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700474 fwd_txfm_param.lossless = xd->lossless[seg_id];
475
476 fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
477
478 quant = &pd->seg_dequant[seg_id][0]; // aom's quantizer
479
480 eob = av1_pvq_decode_helper(&xd->daala_dec, pvq_ref_coeff, dqcoeff, quant,
481 plane, tx_size, tx_type, xdec, ac_dc_coded);
482
483 // Since av1 does not have separate inverse transform
484 // but also contains adding to predicted image,
485 // pass blank dummy image to av1_inv_txfm_add_*x*(), i.e. set dst as zeros
486 for (j = 0; j < tx_blk_size; j++)
487 for (i = 0; i < tx_blk_size; i++) dst[j * pd->dst.stride + i] = 0;
488
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800489 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
490 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700491 }
492
493 return eob;
494}
495#endif
496
Alex Converse8aca36d2017-01-31 12:33:15 -0800497static void predict_and_reconstruct_intra_block(
498 AV1_COMMON *cm, MACROBLOCKD *const xd, aom_reader *const r,
499 MB_MODE_INFO *const mbmi, int plane, int row, int col, TX_SIZE tx_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700500 struct macroblockd_plane *const pd = &xd->plane[plane];
501 PREDICTION_MODE mode = (plane == 0) ? mbmi->mode : mbmi->uv_mode;
Luc Trudeau005feb62017-02-22 13:34:01 -0500502 PLANE_TYPE plane_type = get_plane_type(plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700503 uint8_t *dst;
Urvang Joshifeb925f2016-12-05 10:37:29 -0800504 const int block_idx = (row << 1) + col;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700505#if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -0700506 (void)r;
507#endif
Jingning Hanca14dda2016-12-09 09:36:00 -0800508 dst = &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700509
Jingning Han41bb3392016-12-14 10:46:48 -0800510#if !CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -0700511 if (mbmi->sb_type < BLOCK_8X8)
Urvang Joshifeb925f2016-12-05 10:37:29 -0800512 if (plane == 0) mode = xd->mi[0]->bmi[block_idx].as_mode;
Jingning Han41bb3392016-12-14 10:46:48 -0800513#endif
David Barker839467f2017-01-19 11:06:15 +0000514 av1_predict_intra_block(xd, pd->width, pd->height, txsize_to_bsize[tx_size],
515 mode, dst, pd->dst.stride, dst, pd->dst.stride, col,
516 row, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700517
518 if (!mbmi->skip) {
519 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700520#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700521 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 0);
Jingning Han1be18782016-10-21 11:48:15 -0700522 int16_t max_scan_line = 0;
523 const int eob =
524 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size,
525 tx_type, &max_scan_line, r, mbmi->segment_id);
Angie Chianged8cd9a2016-10-21 16:44:47 -0700526#if CONFIG_ADAPT_SCAN
Jingning Han45e0d462017-02-13 10:37:10 -0800527 if (xd->counts)
528 av1_update_scan_count_facade(cm, xd->counts, tx_size, tx_type,
529 pd->dqcoeff, eob);
Angie Chianged8cd9a2016-10-21 16:44:47 -0700530#endif
Jingning Han1be18782016-10-21 11:48:15 -0700531 if (eob)
532 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
533 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700534#else
ltrudeaue1c09292017-01-20 15:42:13 -0500535 av1_pvq_decode_helper2(cm, xd, mbmi, plane, row, col, tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700536#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700537 }
538}
539
Jingning Handddb21f2017-02-28 14:44:05 -0800540#if CONFIG_VAR_TX && !CONFIG_COEF_INTERLEAVE
Angie Chiangff6d8902016-10-21 11:02:09 -0700541static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
542 aom_reader *r, MB_MODE_INFO *const mbmi,
Jingning Han8fd62b72016-10-21 12:55:54 -0700543 int plane, BLOCK_SIZE plane_bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700544 int blk_row, int blk_col, TX_SIZE tx_size,
545 int *eob_total) {
546 const struct macroblockd_plane *const pd = &xd->plane[plane];
547 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
548 const int tx_row = blk_row >> (1 - pd->subsampling_y);
549 const int tx_col = blk_col >> (1 - pd->subsampling_x);
550 const TX_SIZE plane_tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700551 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700552 : mbmi->inter_tx_size[tx_row][tx_col];
Jingning Han5f614262016-10-27 14:27:43 -0700553 // Scale to match transform block unit.
Jingning Hanf64062f2016-11-02 16:22:18 -0700554 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
555 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700556
557 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
558
559 if (tx_size == plane_tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500560 PLANE_TYPE plane_type = get_plane_type(plane);
Jingning Han8fd62b72016-10-21 12:55:54 -0700561 int block_idx = (blk_row << 1) + blk_col;
562 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiangff6d8902016-10-21 11:02:09 -0700563 const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700564 int16_t max_scan_line = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700565 const int eob =
Yaowu Xuf883b422016-08-30 14:01:10 -0700566 av1_decode_block_tokens(xd, plane, sc, blk_col, blk_row, plane_tx_size,
Jingning Han1be18782016-10-21 11:48:15 -0700567 tx_type, &max_scan_line, r, mbmi->segment_id);
Jingning Han758b2ce2017-01-20 17:12:55 -0800568#if CONFIG_ADAPT_SCAN
Jingning Han45e0d462017-02-13 10:37:10 -0800569 if (xd->counts)
570 av1_update_scan_count_facade(cm, xd->counts, tx_size, tx_type,
571 pd->dqcoeff, eob);
Jingning Han758b2ce2017-01-20 17:12:55 -0800572#endif
Jingning Han9ca05b72017-01-03 14:41:36 -0800573 inverse_transform_block(xd, plane, tx_type, plane_tx_size,
574 &pd->dst.buf[(blk_row * pd->dst.stride + blk_col)
575 << tx_size_wide_log2[0]],
576 pd->dst.stride, max_scan_line, eob);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700577 *eob_total += eob;
578 } else {
Jingning Hanf64062f2016-11-02 16:22:18 -0700579 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
580 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700581 int i;
582
583 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700584
585 for (i = 0; i < 4; ++i) {
Jingning Han5f614262016-10-27 14:27:43 -0700586 const int offsetr = blk_row + (i >> 1) * bsl;
587 const int offsetc = blk_col + (i & 0x01) * bsl;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700588
589 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
590
Jingning Han8fd62b72016-10-21 12:55:54 -0700591 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
Jingning Hanf64062f2016-11-02 16:22:18 -0700592 offsetc, sub_txs, eob_total);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700593 }
594 }
595}
596#endif // CONFIG_VAR_TX
597
Jingning Handddb21f2017-02-28 14:44:05 -0800598#if !CONFIG_VAR_TX || CONFIG_SUPERTX || CONFIG_COEF_INTERLEAVE || \
Jingning Hanfe45b212016-11-22 10:30:23 -0800599 (!CONFIG_VAR_TX && CONFIG_EXT_TX && CONFIG_RECT_TX)
Angie Chiangff6d8902016-10-21 11:02:09 -0700600static int reconstruct_inter_block(AV1_COMMON *cm, MACROBLOCKD *const xd,
Alex Converse8aca36d2017-01-31 12:33:15 -0800601 aom_reader *const r, int segment_id,
602 int plane, int row, int col,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700603 TX_SIZE tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500604 PLANE_TYPE plane_type = get_plane_type(plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700605 int block_idx = (row << 1) + col;
606 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700607#if CONFIG_PVQ
608 int eob;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700609 (void)r;
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800610 (void)segment_id;
611#else
612 struct macroblockd_plane *const pd = &xd->plane[plane];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700613#endif
614
615#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700616 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700617 int16_t max_scan_line = 0;
618 const int eob =
619 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size, tx_type,
620 &max_scan_line, r, segment_id);
Jingning Hanca14dda2016-12-09 09:36:00 -0800621 uint8_t *dst =
622 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Angie Chianged8cd9a2016-10-21 16:44:47 -0700623#if CONFIG_ADAPT_SCAN
Jingning Han45e0d462017-02-13 10:37:10 -0800624 if (xd->counts)
625 av1_update_scan_count_facade(cm, xd->counts, tx_size, tx_type, pd->dqcoeff,
626 eob);
Angie Chianged8cd9a2016-10-21 16:44:47 -0700627#endif
Jingning Han1be18782016-10-21 11:48:15 -0700628 if (eob)
Jingning Hanca14dda2016-12-09 09:36:00 -0800629 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
630 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700631#else
ltrudeaue1c09292017-01-20 15:42:13 -0500632 eob = av1_pvq_decode_helper2(cm, xd, &xd->mi[0]->mbmi, plane, row, col,
633 tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700634#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700635 return eob;
636}
637#endif // !CONFIG_VAR_TX || CONFIG_SUPER_TX
638
Angie Chiang44701f22017-02-27 10:36:44 -0800639static void set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
640 BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
641 int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700642 const int offset = mi_row * cm->mi_stride + mi_col;
643 int x, y;
644 const TileInfo *const tile = &xd->tile;
645
646 xd->mi = cm->mi_grid_visible + offset;
647 xd->mi[0] = &cm->mi[offset];
648 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
649 // passing bsize from decode_partition().
650 xd->mi[0]->mbmi.sb_type = bsize;
Angie Chiang394c3372016-11-03 11:13:15 -0700651#if CONFIG_RD_DEBUG
652 xd->mi[0]->mbmi.mi_row = mi_row;
653 xd->mi[0]->mbmi.mi_col = mi_col;
654#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700655 for (y = 0; y < y_mis; ++y)
Jingning Han97d85482016-07-15 11:06:05 -0700656 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700657
Jingning Hanfaad0e12016-12-07 10:54:57 -0800658 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700659 set_skip_context(xd, mi_row, mi_col);
660
661#if CONFIG_VAR_TX
662 xd->max_tx_size = max_txsize_lookup[bsize];
663#endif
664
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800665// Distance of Mb to the various image edges. These are specified to 8th pel
666// as they are always compared to values that are in 1/8th pel units
667#if CONFIG_DEPENDENT_HORZTILES
668 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
669 cm->dependent_horz_tiles);
670#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700671 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 -0800672#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700673
Yaowu Xuf883b422016-08-30 14:01:10 -0700674 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700675}
676
677#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700678static MB_MODE_INFO *set_offsets_extend(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700679 MACROBLOCKD *const xd,
680 const TileInfo *const tile,
681 BLOCK_SIZE bsize_pred, int mi_row_pred,
682 int mi_col_pred, int mi_row_ori,
683 int mi_col_ori) {
684 // Used in supertx
685 // (mi_row_ori, mi_col_ori): location for mv
686 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
Jingning Han93531242016-12-20 11:54:36 -0800687 const int bw = mi_size_wide[bsize_pred];
688 const int bh = mi_size_high[bsize_pred];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700689 const int offset = mi_row_ori * cm->mi_stride + mi_col_ori;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700690 xd->mi = cm->mi_grid_visible + offset;
691 xd->mi[0] = cm->mi + offset;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800692#if CONFIG_DEPENDENT_HORZTILES
693 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
694 cm->mi_cols, cm->dependent_horz_tiles);
695#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700696 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
697 cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800698#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700699
700 xd->up_available = (mi_row_ori > tile->mi_row_start);
701 xd->left_available = (mi_col_ori > tile->mi_col_start);
702
Jingning Hanfaad0e12016-12-07 10:54:57 -0800703 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700704
705 return &xd->mi[0]->mbmi;
706}
707
Angie Chiang7fcfee42017-02-24 15:51:03 -0800708#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700709static MB_MODE_INFO *set_mb_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
710 BLOCK_SIZE bsize, int mi_row, int mi_col,
711 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700712 const int offset = mi_row * cm->mi_stride + mi_col;
713 const TileInfo *const tile = &xd->tile;
714 int x, y;
715
716 xd->mi = cm->mi_grid_visible + offset;
717 xd->mi[0] = cm->mi + offset;
718 xd->mi[0]->mbmi.sb_type = bsize;
719 for (y = 0; y < y_mis; ++y)
720 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
721
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800722#if CONFIG_DEPENDENT_HORZTILES
723 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
724 cm->dependent_horz_tiles);
725#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700726 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 -0800727#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700728 return &xd->mi[0]->mbmi;
729}
Angie Chiang7fcfee42017-02-24 15:51:03 -0800730#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700731
Yaowu Xuf883b422016-08-30 14:01:10 -0700732static void set_offsets_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700733 const TileInfo *const tile, BLOCK_SIZE bsize,
734 int mi_row, int mi_col) {
Jingning Han93531242016-12-20 11:54:36 -0800735 const int bw = mi_size_wide[bsize];
736 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700737 const int offset = mi_row * cm->mi_stride + mi_col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700738
739 xd->mi = cm->mi_grid_visible + offset;
740 xd->mi[0] = cm->mi + offset;
741
Jingning Hanfaad0e12016-12-07 10:54:57 -0800742 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700743
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800744#if CONFIG_DEPENDENT_HORZTILES
745 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
746 cm->dependent_horz_tiles);
747#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700748 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 -0800749#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700750
Yaowu Xuf883b422016-08-30 14:01:10 -0700751 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700752}
753
Yaowu Xuf883b422016-08-30 14:01:10 -0700754static void set_param_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700755 BLOCK_SIZE bsize, int mi_row, int mi_col,
756 int txfm, int skip) {
Jingning Han93531242016-12-20 11:54:36 -0800757 const int bw = mi_size_wide[bsize];
758 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -0700759 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
760 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700761 const int offset = mi_row * cm->mi_stride + mi_col;
762 int x, y;
763
764 xd->mi = cm->mi_grid_visible + offset;
765 xd->mi[0] = cm->mi + offset;
766
767 for (y = 0; y < y_mis; ++y)
768 for (x = 0; x < x_mis; ++x) {
769 xd->mi[y * cm->mi_stride + x]->mbmi.skip = skip;
770 xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
771 }
772#if CONFIG_VAR_TX
773 xd->above_txfm_context = cm->above_txfm_context + mi_col;
774 xd->left_txfm_context =
775 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
Yaowu Xu52a17632016-11-17 15:48:21 -0800776 set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bw, bh, skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700777#endif
778}
779
Yaowu Xuf883b422016-08-30 14:01:10 -0700780static void set_ref(AV1_COMMON *const cm, MACROBLOCKD *const xd, int idx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700781 int mi_row, int mi_col) {
782 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
783 RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
784 xd->block_refs[idx] = ref_buffer;
Yaowu Xuf883b422016-08-30 14:01:10 -0700785 if (!av1_is_valid_scale(&ref_buffer->sf))
786 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700787 "Invalid scale factors");
Yaowu Xuf883b422016-08-30 14:01:10 -0700788 av1_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
789 &ref_buffer->sf);
Angie Chiangd0916d92017-03-10 17:54:18 -0800790 aom_merge_corrupted_flag(&xd->corrupted, ref_buffer->buf->corrupted);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791}
792
793static void dec_predict_b_extend(
Yaowu Xuf883b422016-08-30 14:01:10 -0700794 AV1Decoder *const pbi, MACROBLOCKD *const xd, const TileInfo *const tile,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700795 int block, int mi_row_ori, int mi_col_ori, int mi_row_pred, int mi_col_pred,
796 int mi_row_top, int mi_col_top, uint8_t *dst_buf[3], int dst_stride[3],
797 BLOCK_SIZE bsize_top, BLOCK_SIZE bsize_pred, int b_sub8x8, int bextend) {
798 // Used in supertx
799 // (mi_row_ori, mi_col_ori): location for mv
800 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
801 // (mi_row_top, mi_col_top, bsize_top): region of the top partition size
802 // block: sub location of sub8x8 blocks
803 // b_sub8x8: 1: ori is sub8x8; 0: ori is not sub8x8
804 // bextend: 1: region to predict is an extension of ori; 0: not
805 int r = (mi_row_pred - mi_row_top) * MI_SIZE;
806 int c = (mi_col_pred - mi_col_top) * MI_SIZE;
Jingning Han93531242016-12-20 11:54:36 -0800807 const int mi_width_top = mi_size_wide[bsize_top];
808 const int mi_height_top = mi_size_high[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700809 MB_MODE_INFO *mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700810 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700811
812 if (mi_row_pred < mi_row_top || mi_col_pred < mi_col_top ||
813 mi_row_pred >= mi_row_top + mi_height_top ||
814 mi_col_pred >= mi_col_top + mi_width_top || mi_row_pred >= cm->mi_rows ||
815 mi_col_pred >= cm->mi_cols)
816 return;
817
818 mbmi = set_offsets_extend(cm, xd, tile, bsize_pred, mi_row_pred, mi_col_pred,
819 mi_row_ori, mi_col_ori);
820 set_ref(cm, xd, 0, mi_row_pred, mi_col_pred);
821 if (has_second_ref(&xd->mi[0]->mbmi))
822 set_ref(cm, xd, 1, mi_row_pred, mi_col_pred);
823
Jingning Han2511c662016-12-22 11:57:34 -0800824 if (!bextend) mbmi->tx_size = max_txsize_lookup[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700825
826 xd->plane[0].dst.stride = dst_stride[0];
827 xd->plane[1].dst.stride = dst_stride[1];
828 xd->plane[2].dst.stride = dst_stride[2];
829 xd->plane[0].dst.buf = dst_buf[0] +
830 (r >> xd->plane[0].subsampling_y) * dst_stride[0] +
831 (c >> xd->plane[0].subsampling_x);
832 xd->plane[1].dst.buf = dst_buf[1] +
833 (r >> xd->plane[1].subsampling_y) * dst_stride[1] +
834 (c >> xd->plane[1].subsampling_x);
835 xd->plane[2].dst.buf = dst_buf[2] +
836 (r >> xd->plane[2].subsampling_y) * dst_stride[2] +
837 (c >> xd->plane[2].subsampling_x);
838
839 if (!b_sub8x8)
Yaowu Xuf883b422016-08-30 14:01:10 -0700840 av1_build_inter_predictors_sb_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700841#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700842 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700843#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700844 mi_row_pred, mi_col_pred, bsize_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700845 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700846 av1_build_inter_predictors_sb_sub8x8_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700847#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700848 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700849#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700850 mi_row_pred, mi_col_pred,
851 bsize_pred, block);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700852}
853
Yaowu Xuf883b422016-08-30 14:01:10 -0700854static void dec_extend_dir(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700855 const TileInfo *const tile, int block,
856 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
857 int mi_col, int mi_row_top, int mi_col_top,
858 uint8_t *dst_buf[3], int dst_stride[3], int dir) {
859 // dir: 0-lower, 1-upper, 2-left, 3-right
860 // 4-lowerleft, 5-upperleft, 6-lowerright, 7-upperright
Jingning Han93531242016-12-20 11:54:36 -0800861 const int mi_width = mi_size_wide[bsize];
862 const int mi_height = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700863 int xss = xd->plane[1].subsampling_x;
864 int yss = xd->plane[1].subsampling_y;
Jingning Hanfeb517c2016-12-21 16:02:07 -0800865#if CONFIG_CB4X4
866 const int unify_bsize = 1;
867#else
868 const int unify_bsize = 0;
869#endif
870 int b_sub8x8 = (bsize < BLOCK_8X8) && !unify_bsize ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700871 BLOCK_SIZE extend_bsize;
Jingning Han24f24a52016-12-27 10:13:28 -0800872 int mi_row_pred, mi_col_pred;
873
874 int wide_unit, high_unit;
875 int i, j;
876 int ext_offset = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700877
878 if (dir == 0 || dir == 1) {
Jingning Han93531242016-12-20 11:54:36 -0800879 extend_bsize =
880 (mi_width == mi_size_wide[BLOCK_8X8] || bsize < BLOCK_8X8 || xss < yss)
881 ? BLOCK_8X8
882 : BLOCK_16X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800883#if CONFIG_CB4X4
884 if (bsize < BLOCK_8X8) {
885 extend_bsize = BLOCK_4X4;
886 ext_offset = mi_size_wide[BLOCK_8X8];
887 }
888#endif
889
890 wide_unit = mi_size_wide[extend_bsize];
891 high_unit = mi_size_high[extend_bsize];
892
893 mi_row_pred = mi_row + ((dir == 0) ? mi_height : -(mi_height + ext_offset));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700894 mi_col_pred = mi_col;
895
Jingning Han24f24a52016-12-27 10:13:28 -0800896 for (j = 0; j < mi_height + ext_offset; j += high_unit)
897 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
898 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
899 mi_row_pred + j, mi_col_pred + i, mi_row_top,
900 mi_col_top, dst_buf, dst_stride, top_bsize,
901 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700902 } else if (dir == 2 || dir == 3) {
Jingning Han5b7706a2016-12-21 09:55:10 -0800903 extend_bsize =
904 (mi_height == mi_size_high[BLOCK_8X8] || bsize < BLOCK_8X8 || yss < xss)
905 ? BLOCK_8X8
906 : BLOCK_8X16;
Jingning Han24f24a52016-12-27 10:13:28 -0800907#if CONFIG_CB4X4
908 if (bsize < BLOCK_8X8) {
909 extend_bsize = BLOCK_4X4;
910 ext_offset = mi_size_wide[BLOCK_8X8];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700911 }
Jingning Han24f24a52016-12-27 10:13:28 -0800912#endif
913
914 wide_unit = mi_size_wide[extend_bsize];
915 high_unit = mi_size_high[extend_bsize];
916
917 mi_row_pred = mi_row;
918 mi_col_pred = mi_col + ((dir == 3) ? mi_width : -(mi_width + ext_offset));
919
920 for (j = 0; j < mi_height + ext_offset; j += high_unit)
921 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
922 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
923 mi_row_pred + j, mi_col_pred + i, mi_row_top,
924 mi_col_top, dst_buf, dst_stride, top_bsize,
925 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700926 } else {
927 extend_bsize = BLOCK_8X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800928#if CONFIG_CB4X4
929 if (bsize < BLOCK_8X8) {
930 extend_bsize = BLOCK_4X4;
931 ext_offset = mi_size_wide[BLOCK_8X8];
932 }
933#endif
934 wide_unit = mi_size_wide[extend_bsize];
935 high_unit = mi_size_high[extend_bsize];
936
Jingning Han5b7706a2016-12-21 09:55:10 -0800937 mi_row_pred = mi_row + ((dir == 4 || dir == 6) ? mi_height
Jingning Han24f24a52016-12-27 10:13:28 -0800938 : -(mi_height + ext_offset));
Jingning Han5b7706a2016-12-21 09:55:10 -0800939 mi_col_pred =
Jingning Han24f24a52016-12-27 10:13:28 -0800940 mi_col + ((dir == 6 || dir == 7) ? mi_width : -(mi_width + ext_offset));
941
942 for (j = 0; j < mi_height + ext_offset; j += high_unit)
943 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
944 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
945 mi_row_pred + j, mi_col_pred + i, mi_row_top,
946 mi_col_top, dst_buf, dst_stride, top_bsize,
947 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700948 }
949}
950
Yaowu Xuf883b422016-08-30 14:01:10 -0700951static void dec_extend_all(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700952 const TileInfo *const tile, int block,
953 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
954 int mi_col, int mi_row_top, int mi_col_top,
955 uint8_t *dst_buf[3], int dst_stride[3]) {
956 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
957 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
958 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
959 mi_row_top, mi_col_top, dst_buf, dst_stride, 1);
960 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
961 mi_row_top, mi_col_top, dst_buf, dst_stride, 2);
962 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
963 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
964 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
965 mi_row_top, mi_col_top, dst_buf, dst_stride, 4);
966 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
967 mi_row_top, mi_col_top, dst_buf, dst_stride, 5);
968 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
969 mi_row_top, mi_col_top, dst_buf, dst_stride, 6);
970 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
971 mi_row_top, mi_col_top, dst_buf, dst_stride, 7);
972}
973
Yaowu Xuf883b422016-08-30 14:01:10 -0700974static void dec_predict_sb_complex(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700975 const TileInfo *const tile, int mi_row,
976 int mi_col, int mi_row_top, int mi_col_top,
977 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize,
978 uint8_t *dst_buf[3], int dst_stride[3]) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700979 const AV1_COMMON *const cm = &pbi->common;
Jingning Han5b7706a2016-12-21 09:55:10 -0800980 const int hbs = mi_size_wide[bsize] / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700981 const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
982 const BLOCK_SIZE subsize = get_subsize(bsize, partition);
983#if CONFIG_EXT_PARTITION_TYPES
984 const BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
985#endif
986 int i;
987 const int mi_offset = mi_row * cm->mi_stride + mi_col;
988 uint8_t *dst_buf1[3], *dst_buf2[3], *dst_buf3[3];
Jingning Hanfeb517c2016-12-21 16:02:07 -0800989#if CONFIG_CB4X4
990 const int unify_bsize = 1;
991#else
992 const int unify_bsize = 0;
993#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700994
995 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
996 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
997 DECLARE_ALIGNED(16, uint8_t, tmp_buf3[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
998 int dst_stride1[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
999 int dst_stride2[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1000 int dst_stride3[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1001
Yaowu Xuf883b422016-08-30 14:01:10 -07001002#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001003 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1004 int len = sizeof(uint16_t);
1005 dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
1006 dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_TX_SQUARE * len);
1007 dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_TX_SQUARE * len);
1008 dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
1009 dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_TX_SQUARE * len);
1010 dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_TX_SQUARE * len);
1011 dst_buf3[0] = CONVERT_TO_BYTEPTR(tmp_buf3);
1012 dst_buf3[1] = CONVERT_TO_BYTEPTR(tmp_buf3 + MAX_TX_SQUARE * len);
1013 dst_buf3[2] = CONVERT_TO_BYTEPTR(tmp_buf3 + 2 * MAX_TX_SQUARE * len);
1014 } else {
1015#endif
1016 dst_buf1[0] = tmp_buf1;
1017 dst_buf1[1] = tmp_buf1 + MAX_TX_SQUARE;
1018 dst_buf1[2] = tmp_buf1 + 2 * MAX_TX_SQUARE;
1019 dst_buf2[0] = tmp_buf2;
1020 dst_buf2[1] = tmp_buf2 + MAX_TX_SQUARE;
1021 dst_buf2[2] = tmp_buf2 + 2 * MAX_TX_SQUARE;
1022 dst_buf3[0] = tmp_buf3;
1023 dst_buf3[1] = tmp_buf3 + MAX_TX_SQUARE;
1024 dst_buf3[2] = tmp_buf3 + 2 * MAX_TX_SQUARE;
Yaowu Xuf883b422016-08-30 14:01:10 -07001025#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001026 }
1027#endif
1028
1029 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1030
1031 xd->mi = cm->mi_grid_visible + mi_offset;
1032 xd->mi[0] = cm->mi + mi_offset;
1033
1034 for (i = 0; i < MAX_MB_PLANE; i++) {
1035 xd->plane[i].dst.buf = dst_buf[i];
1036 xd->plane[i].dst.stride = dst_stride[i];
1037 }
1038
1039 switch (partition) {
1040 case PARTITION_NONE:
1041 assert(bsize < top_bsize);
1042 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1043 mi_row_top, mi_col_top, dst_buf, dst_stride,
1044 top_bsize, bsize, 0, 0);
1045 dec_extend_all(pbi, xd, tile, 0, bsize, top_bsize, mi_row, mi_col,
1046 mi_row_top, mi_col_top, dst_buf, dst_stride);
1047 break;
1048 case PARTITION_HORZ:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001049 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001050 // For sub8x8, predict in 8x8 unit
1051 // First half
1052 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1053 mi_row_top, mi_col_top, dst_buf, dst_stride,
1054 top_bsize, BLOCK_8X8, 1, 0);
1055 if (bsize < top_bsize)
1056 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1057 mi_row_top, mi_col_top, dst_buf, dst_stride);
1058
1059 // Second half
1060 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1061 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1062 top_bsize, BLOCK_8X8, 1, 1);
1063 if (bsize < top_bsize)
1064 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1065 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1066
1067 // weighted average to smooth the boundary
1068 xd->plane[0].dst.buf = dst_buf[0];
1069 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001070 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001071 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1072 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1073 0);
1074 } else {
1075 // First half
1076 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1077 mi_row_top, mi_col_top, dst_buf, dst_stride,
1078 top_bsize, subsize, 0, 0);
1079 if (bsize < top_bsize)
1080 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1081 mi_row_top, mi_col_top, dst_buf, dst_stride);
1082 else
1083 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1084 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1085
1086 if (mi_row + hbs < cm->mi_rows) {
1087 // Second half
1088 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col,
1089 mi_row + hbs, mi_col, mi_row_top, mi_col_top,
1090 dst_buf1, dst_stride1, top_bsize, subsize, 0, 0);
1091 if (bsize < top_bsize)
1092 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1093 mi_col, mi_row_top, mi_col_top, dst_buf1,
1094 dst_stride1);
1095 else
1096 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1097 mi_col, mi_row_top, mi_col_top, dst_buf1,
1098 dst_stride1, 1);
1099
1100 // weighted average to smooth the boundary
1101 for (i = 0; i < MAX_MB_PLANE; i++) {
1102 xd->plane[i].dst.buf = dst_buf[i];
1103 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001104 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001105 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1106 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1107 PARTITION_HORZ, i);
1108 }
1109 }
1110 }
1111 break;
1112 case PARTITION_VERT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001113 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001114 // First half
1115 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1116 mi_row_top, mi_col_top, dst_buf, dst_stride,
1117 top_bsize, BLOCK_8X8, 1, 0);
1118 if (bsize < top_bsize)
1119 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1120 mi_row_top, mi_col_top, dst_buf, dst_stride);
1121
1122 // Second half
1123 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1124 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1125 top_bsize, BLOCK_8X8, 1, 1);
1126 if (bsize < top_bsize)
1127 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1128 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1129
1130 // Smooth
1131 xd->plane[0].dst.buf = dst_buf[0];
1132 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001133 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001134 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1135 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1136 0);
1137 } else {
1138 // First half
1139 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1140 mi_row_top, mi_col_top, dst_buf, dst_stride,
1141 top_bsize, subsize, 0, 0);
1142 if (bsize < top_bsize)
1143 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1144 mi_row_top, mi_col_top, dst_buf, dst_stride);
1145 else
1146 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1147 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1148
1149 // Second half
1150 if (mi_col + hbs < cm->mi_cols) {
1151 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1152 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1153 dst_stride1, top_bsize, subsize, 0, 0);
1154 if (bsize < top_bsize)
1155 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1156 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1157 dst_stride1);
1158 else
1159 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1160 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1161 dst_stride1, 2);
1162
1163 // Smooth
1164 for (i = 0; i < MAX_MB_PLANE; i++) {
1165 xd->plane[i].dst.buf = dst_buf[i];
1166 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001167 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001168 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1169 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1170 PARTITION_VERT, i);
1171 }
1172 }
1173 }
1174 break;
1175 case PARTITION_SPLIT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001176 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001177 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1178 mi_row_top, mi_col_top, dst_buf, dst_stride,
1179 top_bsize, BLOCK_8X8, 1, 0);
1180 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1181 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1182 top_bsize, BLOCK_8X8, 1, 1);
1183 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1184 mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1185 top_bsize, BLOCK_8X8, 1, 1);
1186 dec_predict_b_extend(pbi, xd, tile, 3, mi_row, mi_col, mi_row, mi_col,
1187 mi_row_top, mi_col_top, dst_buf3, dst_stride3,
1188 top_bsize, BLOCK_8X8, 1, 1);
1189 if (bsize < top_bsize) {
1190 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1191 mi_row_top, mi_col_top, dst_buf, dst_stride);
1192 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1193 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1194 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1195 mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1196 dec_extend_all(pbi, xd, tile, 3, subsize, top_bsize, mi_row, mi_col,
1197 mi_row_top, mi_col_top, dst_buf3, dst_stride3);
1198 }
1199 } else {
1200 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row_top,
1201 mi_col_top, subsize, top_bsize, dst_buf,
1202 dst_stride);
1203 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1204 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col + hbs,
1205 mi_row_top, mi_col_top, subsize, top_bsize,
1206 dst_buf1, dst_stride1);
1207 if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols)
1208 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col,
1209 mi_row_top, mi_col_top, subsize, top_bsize,
1210 dst_buf2, dst_stride2);
1211 if (mi_row + hbs < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1212 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col + hbs,
1213 mi_row_top, mi_col_top, subsize, top_bsize,
1214 dst_buf3, dst_stride3);
1215 }
1216 for (i = 0; i < MAX_MB_PLANE; i++) {
Jingning Han9e0976a2016-12-27 17:52:42 -08001217#if !CONFIG_CB4X4
Jingning Han24f24a52016-12-27 10:13:28 -08001218 if (bsize == BLOCK_8X8 && i != 0)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001219 continue; // Skip <4x4 chroma smoothing
Jingning Han9e0976a2016-12-27 17:52:42 -08001220#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001221 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001222 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001223 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1224 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1225 PARTITION_VERT, i);
1226 if (mi_row + hbs < cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001227 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001228 xd, dst_buf2[i], dst_stride2[i], dst_buf3[i], dst_stride3[i],
1229 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1230 PARTITION_VERT, i);
Yaowu Xuf883b422016-08-30 14:01:10 -07001231 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001232 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1233 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1234 PARTITION_HORZ, i);
1235 }
1236 } else if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001237 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001238 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1239 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1240 PARTITION_HORZ, i);
1241 }
1242 }
1243 break;
1244#if CONFIG_EXT_PARTITION_TYPES
1245 case PARTITION_HORZ_A:
1246 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1247 mi_row_top, mi_col_top, dst_buf, dst_stride,
1248 top_bsize, bsize2, 0, 0);
1249 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1250 mi_row_top, mi_col_top, dst_buf, dst_stride);
1251
1252 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1253 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1254 dst_stride1, top_bsize, bsize2, 0, 0);
1255 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1256 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1257
1258 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1259 mi_col, mi_row_top, mi_col_top, dst_buf2,
1260 dst_stride2, top_bsize, subsize, 0, 0);
1261 if (bsize < top_bsize)
1262 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1263 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1264 else
1265 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1266 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1267 1);
1268
1269 for (i = 0; i < MAX_MB_PLANE; i++) {
1270 xd->plane[i].dst.buf = dst_buf[i];
1271 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001272 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001273 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1274 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1275 i);
1276 }
1277 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001278 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001279 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1280 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1281 i);
1282 }
1283 break;
1284 case PARTITION_VERT_A:
1285
1286 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1287 mi_row_top, mi_col_top, dst_buf, dst_stride,
1288 top_bsize, bsize2, 0, 0);
1289 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1290 mi_row_top, mi_col_top, dst_buf, dst_stride);
1291
1292 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1293 mi_col, mi_row_top, mi_col_top, dst_buf1,
1294 dst_stride1, top_bsize, bsize2, 0, 0);
1295 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1296 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1297
1298 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1299 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1300 dst_stride2, top_bsize, subsize, 0, 0);
1301 if (bsize < top_bsize)
1302 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1303 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1304 dst_stride2);
1305 else
1306 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1307 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1308 dst_stride2, 2);
1309
1310 for (i = 0; i < MAX_MB_PLANE; i++) {
1311 xd->plane[i].dst.buf = dst_buf[i];
1312 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001313 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001314 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1315 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1316 i);
1317 }
1318 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001319 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001320 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1321 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1322 i);
1323 }
1324 break;
1325 case PARTITION_HORZ_B:
1326 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1327 mi_row_top, mi_col_top, dst_buf, dst_stride,
1328 top_bsize, subsize, 0, 0);
1329 if (bsize < top_bsize)
1330 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1331 mi_row_top, mi_col_top, dst_buf, dst_stride);
1332 else
1333 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1334 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1335
1336 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1337 mi_col, mi_row_top, mi_col_top, dst_buf1,
1338 dst_stride1, top_bsize, bsize2, 0, 0);
1339 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1340 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1341
1342 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1343 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1344 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1345 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1346 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1347 dst_stride2);
1348
1349 for (i = 0; i < MAX_MB_PLANE; i++) {
1350 xd->plane[i].dst.buf = dst_buf1[i];
1351 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001352 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001353 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1354 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1355 PARTITION_VERT, i);
1356 }
1357 for (i = 0; i < MAX_MB_PLANE; i++) {
1358 xd->plane[i].dst.buf = dst_buf[i];
1359 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001360 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001361 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1362 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1363 i);
1364 }
1365 break;
1366 case PARTITION_VERT_B:
1367 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1368 mi_row_top, mi_col_top, dst_buf, dst_stride,
1369 top_bsize, subsize, 0, 0);
1370 if (bsize < top_bsize)
1371 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1372 mi_row_top, mi_col_top, dst_buf, dst_stride);
1373 else
1374 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1375 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1376
1377 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1378 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1379 dst_stride1, top_bsize, bsize2, 0, 0);
1380 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1381 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1382
1383 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1384 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1385 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1386 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1387 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1388 dst_stride2);
1389
1390 for (i = 0; i < MAX_MB_PLANE; i++) {
1391 xd->plane[i].dst.buf = dst_buf1[i];
1392 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001393 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001394 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1395 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1396 PARTITION_HORZ, i);
1397 }
1398 for (i = 0; i < MAX_MB_PLANE; i++) {
1399 xd->plane[i].dst.buf = dst_buf[i];
1400 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001401 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001402 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1403 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1404 i);
1405 }
1406 break;
1407#endif // CONFIG_EXT_PARTITION_TYPES
1408 default: assert(0);
1409 }
1410}
1411
Yaowu Xuf883b422016-08-30 14:01:10 -07001412static void set_segment_id_supertx(const AV1_COMMON *const cm, const int mi_row,
1413 const int mi_col, const BLOCK_SIZE bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001414 const struct segmentation *seg = &cm->seg;
Jingning Han5b7706a2016-12-21 09:55:10 -08001415 const int miw = AOMMIN(mi_size_wide[bsize], cm->mi_cols - mi_col);
1416 const int mih = AOMMIN(mi_size_high[bsize], cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001417 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1418 MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
1419 int r, c;
1420 int seg_id_supertx = MAX_SEGMENTS;
1421
1422 if (!seg->enabled) {
1423 seg_id_supertx = 0;
1424 } else {
1425 // Find the minimum segment_id
1426 for (r = 0; r < mih; r++)
1427 for (c = 0; c < miw; c++)
1428 seg_id_supertx =
Yaowu Xuf883b422016-08-30 14:01:10 -07001429 AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001430 assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);
1431 }
1432
1433 // Assign the the segment_id back to segment_id_supertx
1434 for (r = 0; r < mih; r++)
1435 for (c = 0; c < miw; c++)
1436 mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
1437}
1438#endif // CONFIG_SUPERTX
1439
Yue Chen64550b62017-01-12 12:18:22 -08001440static void decode_mbmi_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001441#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001442 int supertx_enabled,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001443#endif // CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001444 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001445#if CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001446 PARTITION_TYPE partition,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001447#endif // CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001448 BLOCK_SIZE bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001449 AV1_COMMON *const cm = &pbi->common;
Jingning Han85dc03f2016-12-06 16:03:10 -08001450 const int bw = mi_size_wide[bsize];
1451 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -07001452 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1453 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Nathan E. Eggeebbd4792016-10-05 19:30:15 -04001454
Michael Bebenita6048d052016-08-25 14:40:54 -07001455#if CONFIG_ACCOUNTING
1456 aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
1457#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001458#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001459 if (supertx_enabled) {
Yue Chen64550b62017-01-12 12:18:22 -08001460 set_mb_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001461 } else {
Yue Chen64550b62017-01-12 12:18:22 -08001462 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001463 }
1464#if CONFIG_EXT_PARTITION_TYPES
1465 xd->mi[0]->mbmi.partition = partition;
1466#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001467 av1_read_mode_info(pbi, xd, supertx_enabled, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001468#else
Yue Chen64550b62017-01-12 12:18:22 -08001469 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001470#if CONFIG_EXT_PARTITION_TYPES
1471 xd->mi[0]->mbmi.partition = partition;
1472#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001473 av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001474#endif // CONFIG_SUPERTX
1475
1476 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1477 const BLOCK_SIZE uv_subsize =
1478 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1479 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -07001480 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001481 "Invalid block size.");
1482 }
1483
1484#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001485 xd->mi[0]->mbmi.segment_id_supertx = MAX_SEGMENTS;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001486#endif // CONFIG_SUPERTX
1487
Angie Chiangd0916d92017-03-10 17:54:18 -08001488 int reader_corrupted_flag = aom_reader_has_error(r);
1489 aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
Yue Chen64550b62017-01-12 12:18:22 -08001490}
1491
1492static void decode_token_and_recon_block(AV1Decoder *const pbi,
1493 MACROBLOCKD *const xd, int mi_row,
1494 int mi_col, aom_reader *r,
1495 BLOCK_SIZE bsize) {
1496 AV1_COMMON *const cm = &pbi->common;
1497 const int bw = mi_size_wide[bsize];
1498 const int bh = mi_size_high[bsize];
1499 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1500 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yue Chen64550b62017-01-12 12:18:22 -08001501
Angie Chiang44701f22017-02-27 10:36:44 -08001502 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
1503 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Yue Chen19e7aa82016-11-30 14:05:39 -08001504
Arild Fuldseth07441162016-08-15 15:07:52 +02001505#if CONFIG_DELTA_Q
1506 if (cm->delta_q_present_flag) {
1507 int i;
1508 for (i = 0; i < MAX_SEGMENTS; i++) {
1509 xd->plane[0].seg_dequant[i][0] =
1510 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
1511 xd->plane[0].seg_dequant[i][1] =
1512 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
1513 xd->plane[1].seg_dequant[i][0] =
1514 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1515 xd->plane[1].seg_dequant[i][1] =
1516 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1517 xd->plane[2].seg_dequant[i][0] =
1518 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1519 xd->plane[2].seg_dequant[i][1] =
1520 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1521 }
1522 }
1523#endif
1524
Jingning Han41bb3392016-12-14 10:46:48 -08001525#if CONFIG_CB4X4
1526 if (mbmi->skip) reset_skip_context(xd, bsize);
1527#else
Jingning Hand39cc722016-12-02 14:03:26 -08001528 if (mbmi->skip) reset_skip_context(xd, AOMMAX(BLOCK_8X8, bsize));
Jingning Han41bb3392016-12-14 10:46:48 -08001529#endif
Jingning Hand39cc722016-12-02 14:03:26 -08001530
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001531#if CONFIG_COEF_INTERLEAVE
1532 {
1533 const struct macroblockd_plane *const pd_y = &xd->plane[0];
1534 const struct macroblockd_plane *const pd_c = &xd->plane[1];
1535 const TX_SIZE tx_log2_y = mbmi->tx_size;
1536 const TX_SIZE tx_log2_c = get_uv_tx_size(mbmi, pd_c);
1537 const int tx_sz_y = (1 << tx_log2_y);
1538 const int tx_sz_c = (1 << tx_log2_c);
1539 const int num_4x4_w_y = pd_y->n4_w;
1540 const int num_4x4_h_y = pd_y->n4_h;
1541 const int num_4x4_w_c = pd_c->n4_w;
1542 const int num_4x4_h_c = pd_c->n4_h;
1543 const int max_4x4_w_y = get_max_4x4_size(num_4x4_w_y, xd->mb_to_right_edge,
1544 pd_y->subsampling_x);
1545 const int max_4x4_h_y = get_max_4x4_size(num_4x4_h_y, xd->mb_to_bottom_edge,
1546 pd_y->subsampling_y);
1547 const int max_4x4_w_c = get_max_4x4_size(num_4x4_w_c, xd->mb_to_right_edge,
1548 pd_c->subsampling_x);
1549 const int max_4x4_h_c = get_max_4x4_size(num_4x4_h_c, xd->mb_to_bottom_edge,
1550 pd_c->subsampling_y);
1551
1552 // The max_4x4_w/h may be smaller than tx_sz under some corner cases,
1553 // i.e. when the SB is splitted by tile boundaries.
1554 const int tu_num_w_y = (max_4x4_w_y + tx_sz_y - 1) / tx_sz_y;
1555 const int tu_num_h_y = (max_4x4_h_y + tx_sz_y - 1) / tx_sz_y;
1556 const int tu_num_w_c = (max_4x4_w_c + tx_sz_c - 1) / tx_sz_c;
1557 const int tu_num_h_c = (max_4x4_h_c + tx_sz_c - 1) / tx_sz_c;
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001558 const int tu_num_c = tu_num_w_c * tu_num_h_c;
1559
1560 if (!is_inter_block(mbmi)) {
1561 int tu_idx_c = 0;
1562 int row_y, col_y, row_c, col_c;
1563 int plane;
1564
1565#if CONFIG_PALETTE
1566 for (plane = 0; plane <= 1; ++plane) {
1567 if (mbmi->palette_mode_info.palette_size[plane])
1568 av1_decode_palette_tokens(xd, plane, r);
1569 }
1570#endif
1571
1572 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1573 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1574 // luma
1575 predict_and_reconstruct_intra_block(
1576 cm, xd, r, mbmi, 0, row_y * tx_sz_y, col_y * tx_sz_y, tx_log2_y);
1577 // chroma
1578 if (tu_idx_c < tu_num_c) {
1579 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1580 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1581 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c,
1582 col_c, tx_log2_c);
1583 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c,
1584 col_c, tx_log2_c);
1585 tu_idx_c++;
1586 }
1587 }
1588 }
1589
1590 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1591 while (tu_idx_c < tu_num_c) {
1592 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1593 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1594 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c, col_c,
1595 tx_log2_c);
1596 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c, col_c,
1597 tx_log2_c);
1598 tu_idx_c++;
1599 }
1600 } else {
1601 // Prediction
David Barkerac37fa32016-12-02 12:30:21 +00001602 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001603 AOMMAX(bsize, BLOCK_8X8));
1604
1605 // Reconstruction
1606 if (!mbmi->skip) {
1607 int eobtotal = 0;
1608 int tu_idx_c = 0;
1609 int row_y, col_y, row_c, col_c;
1610
1611 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1612 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1613 // luma
1614 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 0,
1615 row_y * tx_sz_y,
1616 col_y * tx_sz_y, tx_log2_y);
1617 // chroma
1618 if (tu_idx_c < tu_num_c) {
1619 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1620 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1621 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1622 1, row_c, col_c, tx_log2_c);
1623 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1624 2, row_c, col_c, tx_log2_c);
1625 tu_idx_c++;
1626 }
1627 }
1628 }
1629
1630 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1631 while (tu_idx_c < tu_num_c) {
1632 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1633 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1634 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 1,
1635 row_c, col_c, tx_log2_c);
1636 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 2,
1637 row_c, col_c, tx_log2_c);
1638 tu_idx_c++;
1639 }
1640
1641 if (bsize >= BLOCK_8X8 && eobtotal == 0)
1642#if CONFIG_MISC_FIXES
1643 mbmi->has_no_coeffs = 1;
1644#else
1645 mbmi->skip = 1;
1646#endif
1647 }
1648 }
1649 }
1650#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001651 if (!is_inter_block(mbmi)) {
1652 int plane;
Fangwen Fub3be9262017-03-06 15:34:28 -08001653#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001654 for (plane = 0; plane <= 1; ++plane) {
1655 if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xuf883b422016-08-30 14:01:10 -07001656 av1_decode_palette_tokens(xd, plane, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001657 }
Fangwen Fub3be9262017-03-06 15:34:28 -08001658#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001659 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1660 const struct macroblockd_plane *const pd = &xd->plane[plane];
Angie Chiang7fcfee42017-02-24 15:51:03 -08001661 const TX_SIZE tx_size = get_tx_size(plane, xd);
Jingning Han2d64f122016-10-21 12:44:29 -07001662 const int stepr = tx_size_high_unit[tx_size];
1663 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han41bb3392016-12-14 10:46:48 -08001664#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001665#if CONFIG_CHROMA_2X2
1666 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1667#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001668 const BLOCK_SIZE plane_bsize =
1669 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001670#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001671#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001672 const BLOCK_SIZE plane_bsize =
1673 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001674#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001675 int row, col;
Jingning Hanbafee8d2016-12-02 10:25:03 -08001676 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1677 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001678#if CONFIG_CB4X4
1679 if (bsize < BLOCK_8X8 && plane && !is_chroma_reference(mi_row, mi_col))
1680 continue;
1681#endif
1682
Yaowu Xuc27fc142016-08-22 16:08:15 -07001683 for (row = 0; row < max_blocks_high; row += stepr)
1684 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001685 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane, row, col,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001686 tx_size);
1687 }
1688 } else {
Yue Chen9ab6d712017-01-12 15:50:46 -08001689 int ref;
1690
1691 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
1692 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
1693 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
1694
1695 xd->block_refs[ref] = ref_buf;
1696 if ((!av1_is_valid_scale(&ref_buf->sf)))
1697 aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
1698 "Reference frame has invalid dimensions");
1699 av1_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col, &ref_buf->sf);
1700 }
Yue Chen69f18e12016-09-08 14:48:15 -07001701#if CONFIG_WARPED_MOTION
1702 if (mbmi->motion_mode == WARPED_CAUSAL) {
1703 int i;
Sarah Parker19234cc2017-03-10 16:43:25 -08001704 assert_motion_mode_valid(WARPED_CAUSAL,
1705#if CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1706 0, cm->global_motion,
1707#endif // CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1708 xd->mi[0]);
Yue Chen69f18e12016-09-08 14:48:15 -07001709 for (i = 0; i < 3; ++i) {
1710 const struct macroblockd_plane *pd = &xd->plane[i];
1711
1712 av1_warp_plane(&mbmi->wm_params[0],
1713#if CONFIG_AOM_HIGHBITDEPTH
1714 xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH, xd->bd,
1715#endif // CONFIG_AOM_HIGHBITDEPTH
1716 pd->pre[0].buf0, pd->pre[0].width, pd->pre[0].height,
1717 pd->pre[0].stride, pd->dst.buf,
1718 ((mi_col * MI_SIZE) >> pd->subsampling_x),
1719 ((mi_row * MI_SIZE) >> pd->subsampling_y),
Jingning Hanff6ee6a2016-12-07 09:55:21 -08001720 xd->n8_w * (MI_SIZE >> pd->subsampling_x),
1721 xd->n8_h * (MI_SIZE >> pd->subsampling_y),
1722 pd->dst.stride, pd->subsampling_x, pd->subsampling_y, 16,
1723 16, 0);
Yue Chen69f18e12016-09-08 14:48:15 -07001724 }
1725 } else {
1726#endif // CONFIG_WARPED_MOTION
Jingning Han41bb3392016-12-14 10:46:48 -08001727#if CONFIG_CB4X4
1728 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL, bsize);
1729#else
1730 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
1731 AOMMAX(bsize, BLOCK_8X8));
1732#endif
Yue Chen69f18e12016-09-08 14:48:15 -07001733#if CONFIG_WARPED_MOTION
1734 }
1735#endif // CONFIG_WARPED_MOTION
Yue Chencb60b182016-10-13 15:18:22 -07001736#if CONFIG_MOTION_VAR
1737 if (mbmi->motion_mode == OBMC_CAUSAL) {
Sarah Parker19234cc2017-03-10 16:43:25 -08001738 assert_motion_mode_valid(OBMC_CAUSAL,
1739#if CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1740 0, cm->global_motion,
1741#endif // CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1742 xd->mi[0]);
Yue Chenf27b1602017-01-13 11:11:43 -08001743#if CONFIG_NCOBMC
1744 av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
1745#else
Yue Chen894fcce2016-10-21 16:50:52 -07001746 av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yue Chenf27b1602017-01-13 11:11:43 -08001747#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001748 }
Yue Chencb60b182016-10-13 15:18:22 -07001749#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001750
1751 // Reconstruction
1752 if (!mbmi->skip) {
1753 int eobtotal = 0;
1754 int plane;
1755
1756 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1757 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han41bb3392016-12-14 10:46:48 -08001758#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001759#if CONFIG_CHROMA_2X2
1760 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1761#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001762 const BLOCK_SIZE plane_bsize =
1763 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001764#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001765#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001766 const BLOCK_SIZE plane_bsize =
1767 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001768#endif
Jingning Hanbafee8d2016-12-02 10:25:03 -08001769 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1770 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001771 int row, col;
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001772
1773#if CONFIG_CB4X4
1774 if (bsize < BLOCK_8X8 && plane && !is_chroma_reference(mi_row, mi_col))
1775 continue;
1776#endif
1777
Yaowu Xuc27fc142016-08-22 16:08:15 -07001778#if CONFIG_VAR_TX
Jingning Han70e5f3f2016-11-09 17:03:07 -08001779 const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07001780 const int bh_var_tx = tx_size_high_unit[max_tx_size];
1781 const int bw_var_tx = tx_size_wide_unit[max_tx_size];
Jingning Hanbafee8d2016-12-02 10:25:03 -08001782 for (row = 0; row < max_blocks_high; row += bh_var_tx)
1783 for (col = 0; col < max_blocks_wide; col += bw_var_tx)
Jingning Hanfe45b212016-11-22 10:30:23 -08001784 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, row, col,
1785 max_tx_size, &eobtotal);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001786#else
Angie Chiang7fcfee42017-02-24 15:51:03 -08001787 const TX_SIZE tx_size = get_tx_size(plane, xd);
Jingning Han2d64f122016-10-21 12:44:29 -07001788 const int stepr = tx_size_high_unit[tx_size];
1789 const int stepc = tx_size_wide_unit[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001790 for (row = 0; row < max_blocks_high; row += stepr)
1791 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001792 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1793 plane, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001794#endif
1795 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001796 }
1797 }
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001798#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001799
Angie Chiangd0916d92017-03-10 17:54:18 -08001800 int reader_corrupted_flag = aom_reader_has_error(r);
1801 aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001802}
1803
Yue Chen9ab6d712017-01-12 15:50:46 -08001804#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
1805static void detoken_and_recon_sb(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1806 int mi_row, int mi_col, aom_reader *r,
1807 BLOCK_SIZE bsize) {
1808 AV1_COMMON *const cm = &pbi->common;
1809 const int hbs = mi_size_wide[bsize] >> 1;
1810#if CONFIG_CB4X4
1811 const int unify_bsize = 1;
1812#else
1813 const int unify_bsize = 0;
1814#endif
1815#if CONFIG_EXT_PARTITION_TYPES
1816 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1817#endif
1818 PARTITION_TYPE partition;
1819 BLOCK_SIZE subsize;
1820 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1821 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1822
1823 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1824
1825 partition = get_partition(cm, mi_row, mi_col, bsize);
1826 subsize = subsize_lookup[partition][bsize];
1827
1828 if (!hbs && !unify_bsize) {
1829 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1830 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1831 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1832 } else {
1833 switch (partition) {
1834 case PARTITION_NONE:
1835 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
1836 break;
1837 case PARTITION_HORZ:
1838 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1839 if (has_rows)
1840 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r,
1841 subsize);
1842 break;
1843 case PARTITION_VERT:
1844 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1845 if (has_cols)
1846 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r,
1847 subsize);
1848 break;
1849 case PARTITION_SPLIT:
1850 detoken_and_recon_sb(pbi, xd, mi_row, mi_col, r, subsize);
1851 detoken_and_recon_sb(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1852 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1853 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col + hbs, r, subsize);
1854 break;
1855#if CONFIG_EXT_PARTITION_TYPES
1856 case PARTITION_HORZ_A:
1857 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1858 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1859 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1860 break;
1861 case PARTITION_HORZ_B:
1862 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1863 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1864 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1865 bsize2);
1866 break;
1867 case PARTITION_VERT_A:
1868 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1869 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1870 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1871 break;
1872 case PARTITION_VERT_B:
1873 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1874 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1875 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1876 bsize2);
1877 break;
1878#endif
1879 default: assert(0 && "Invalid partition type");
1880 }
1881 }
1882}
1883#endif
1884
Yue Chen64550b62017-01-12 12:18:22 -08001885static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1886#if CONFIG_SUPERTX
1887 int supertx_enabled,
1888#endif // CONFIG_SUPERTX
1889 int mi_row, int mi_col, aom_reader *r,
1890#if CONFIG_EXT_PARTITION_TYPES
1891 PARTITION_TYPE partition,
1892#endif // CONFIG_EXT_PARTITION_TYPES
1893 BLOCK_SIZE bsize) {
1894 decode_mbmi_block(pbi, xd,
1895#if CONFIG_SUPERTX
1896 supertx_enabled,
1897#endif
1898 mi_row, mi_col, r,
1899#if CONFIG_EXT_PARTITION_TYPES
1900 partition,
1901#endif
1902 bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001903#if !(CONFIG_MOTION_VAR && CONFIG_NCOBMC)
Yue Chen64550b62017-01-12 12:18:22 -08001904#if CONFIG_SUPERTX
1905 if (!supertx_enabled)
1906#endif // CONFIG_SUPERTX
1907 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001908#endif
Yue Chen64550b62017-01-12 12:18:22 -08001909}
1910
Yaowu Xuf883b422016-08-30 14:01:10 -07001911static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
1912 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001913 int has_rows, int has_cols,
Jingning Han1beb0102016-12-07 11:08:30 -08001914 BLOCK_SIZE bsize) {
Alex Converse55c6bde2017-01-12 15:55:31 -08001915#if CONFIG_UNPOISON_PARTITION_CTX
1916 const int ctx =
1917 partition_plane_context(xd, mi_row, mi_col, has_rows, has_cols, bsize);
1918 const aom_prob *const probs = ctx >= 0 ? cm->fc->partition_prob[ctx] : NULL;
1919 FRAME_COUNTS *const counts = ctx >= 0 ? xd->counts : NULL;
1920#else
Jingning Han1beb0102016-12-07 11:08:30 -08001921 const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
Yaowu Xuf883b422016-08-30 14:01:10 -07001922 const aom_prob *const probs = cm->fc->partition_prob[ctx];
Alex Converse55c6bde2017-01-12 15:55:31 -08001923 FRAME_COUNTS *const counts = xd->counts;
1924#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001925 PARTITION_TYPE p;
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001926#if CONFIG_EC_ADAPT
1927 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1928 (void)cm;
1929#elif CONFIG_EC_MULTISYMBOL
1930 FRAME_CONTEXT *ec_ctx = cm->fc;
1931#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001932
1933 if (has_rows && has_cols)
1934#if CONFIG_EXT_PARTITION_TYPES
1935 if (bsize <= BLOCK_8X8)
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001936#if CONFIG_EC_MULTISYMBOL
1937 p = (PARTITION_TYPE)aom_read_symbol(r, ec_ctx->partition_cdf[ctx],
1938 PARTITION_TYPES, ACCT_STR);
1939#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001940 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001941#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001942 else
Alex Converse57795a42017-03-14 12:18:25 -07001943#if CONFIG_EC_MULTISYMBOL
1944 p = (PARTITION_TYPE)aom_read_symbol(r, ec_ctx->partition_cdf[ctx],
1945 EXT_PARTITION_TYPES, ACCT_STR);
1946#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001947 p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs,
1948 ACCT_STR);
Alex Converse57795a42017-03-14 12:18:25 -07001949#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001950#else
Nathan E. Egge9d9eb6c2016-11-16 09:44:26 -05001951#if CONFIG_EC_MULTISYMBOL
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001952 p = (PARTITION_TYPE)aom_read_symbol(r, ec_ctx->partition_cdf[ctx],
Yaowu Xuf2581a32016-10-20 13:05:47 -07001953 PARTITION_TYPES, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001954#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001955 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001956#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001957#endif // CONFIG_EXT_PARTITION_TYPES
1958 else if (!has_rows && has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001959 p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001960 else if (has_rows && !has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001961 p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001962 else
1963 p = PARTITION_SPLIT;
1964
1965 if (counts) ++counts->partition[ctx][p];
1966
1967 return p;
1968}
1969
1970#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001971static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
1972 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001973 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
1974 return 1;
1975 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001976 const int ctx = av1_get_skip_context(xd);
Michael Bebenita6048d052016-08-25 14:40:54 -07001977 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001978 FRAME_COUNTS *counts = xd->counts;
1979 if (counts) ++counts->skip[ctx][skip];
1980 return skip;
1981 }
1982}
1983#endif // CONFIG_SUPERTX
1984
1985// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -07001986static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001987#if CONFIG_SUPERTX
1988 int supertx_enabled,
1989#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001990 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001991 BLOCK_SIZE bsize, int n4x4_l2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001992 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001993 const int n8x8_l2 = n4x4_l2 - 1;
Jingning Hanff17e162016-12-07 17:58:18 -08001994 const int num_8x8_wh = mi_size_wide[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001995 const int hbs = num_8x8_wh >> 1;
Jingning Han41bb3392016-12-14 10:46:48 -08001996#if CONFIG_CB4X4
1997 const int unify_bsize = 1;
1998#else
1999 const int unify_bsize = 0;
2000#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002001 PARTITION_TYPE partition;
2002 BLOCK_SIZE subsize;
2003#if CONFIG_EXT_PARTITION_TYPES
2004 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
2005#endif
2006 const int has_rows = (mi_row + hbs) < cm->mi_rows;
2007 const int has_cols = (mi_col + hbs) < cm->mi_cols;
2008#if CONFIG_SUPERTX
2009 const int read_token = !supertx_enabled;
2010 int skip = 0;
Jingning Han2511c662016-12-22 11:57:34 -08002011 TX_SIZE supertx_size = max_txsize_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002012 const TileInfo *const tile = &xd->tile;
2013 int txfm = DCT_DCT;
2014#endif // CONFIG_SUPERTX
2015
2016 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
2017
Jingning Han6dbacf22016-12-14 17:28:35 -08002018 partition = (n4x4_l2 == 0) ? PARTITION_NONE
2019 : read_partition(cm, xd, mi_row, mi_col, r,
2020 has_rows, has_cols, bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002021 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
Yushin Cho77bba8d2016-11-04 16:36:56 -07002022
2023#if CONFIG_PVQ
2024 assert(partition < PARTITION_TYPES);
2025 assert(subsize < BLOCK_SIZES);
2026#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002027#if CONFIG_SUPERTX
2028 if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
2029 bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
2030 const int supertx_context = partition_supertx_context_lookup[partition];
Michael Bebenita6048d052016-08-25 14:40:54 -07002031 supertx_enabled = aom_read(
2032 r, cm->fc->supertx_prob[supertx_context][supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002033 if (xd->counts)
2034 xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
2035#if CONFIG_VAR_TX
2036 if (supertx_enabled) xd->supertx_size = supertx_size;
2037#endif
2038 }
2039#endif // CONFIG_SUPERTX
Jingning Han41bb3392016-12-14 10:46:48 -08002040 if (!hbs && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002041 // calculate bmode block dimensions (log 2)
2042 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
2043 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
2044 decode_block(pbi, xd,
2045#if CONFIG_SUPERTX
2046 supertx_enabled,
2047#endif // CONFIG_SUPERTX
2048 mi_row, mi_col, r,
2049#if CONFIG_EXT_PARTITION_TYPES
2050 partition,
2051#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002052 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002053 } else {
2054 switch (partition) {
2055 case PARTITION_NONE:
2056 decode_block(pbi, xd,
2057#if CONFIG_SUPERTX
2058 supertx_enabled,
2059#endif // CONFIG_SUPERTX
2060 mi_row, mi_col, r,
2061#if CONFIG_EXT_PARTITION_TYPES
2062 partition,
2063#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002064 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002065 break;
2066 case PARTITION_HORZ:
2067 decode_block(pbi, xd,
2068#if CONFIG_SUPERTX
2069 supertx_enabled,
2070#endif // CONFIG_SUPERTX
2071 mi_row, mi_col, r,
2072#if CONFIG_EXT_PARTITION_TYPES
2073 partition,
2074#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002075 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002076 if (has_rows)
2077 decode_block(pbi, xd,
2078#if CONFIG_SUPERTX
2079 supertx_enabled,
2080#endif // CONFIG_SUPERTX
2081 mi_row + hbs, mi_col, r,
2082#if CONFIG_EXT_PARTITION_TYPES
2083 partition,
2084#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002085 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002086 break;
2087 case PARTITION_VERT:
2088 decode_block(pbi, xd,
2089#if CONFIG_SUPERTX
2090 supertx_enabled,
2091#endif // CONFIG_SUPERTX
2092 mi_row, mi_col, r,
2093#if CONFIG_EXT_PARTITION_TYPES
2094 partition,
2095#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002096 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002097 if (has_cols)
2098 decode_block(pbi, xd,
2099#if CONFIG_SUPERTX
2100 supertx_enabled,
2101#endif // CONFIG_SUPERTX
2102 mi_row, mi_col + hbs, r,
2103#if CONFIG_EXT_PARTITION_TYPES
2104 partition,
2105#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002106 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002107 break;
2108 case PARTITION_SPLIT:
2109 decode_partition(pbi, xd,
2110#if CONFIG_SUPERTX
2111 supertx_enabled,
2112#endif // CONFIG_SUPERTX
2113 mi_row, mi_col, r, subsize, n8x8_l2);
2114 decode_partition(pbi, xd,
2115#if CONFIG_SUPERTX
2116 supertx_enabled,
2117#endif // CONFIG_SUPERTX
2118 mi_row, mi_col + hbs, r, subsize, n8x8_l2);
2119 decode_partition(pbi, xd,
2120#if CONFIG_SUPERTX
2121 supertx_enabled,
2122#endif // CONFIG_SUPERTX
2123 mi_row + hbs, mi_col, r, subsize, n8x8_l2);
2124 decode_partition(pbi, xd,
2125#if CONFIG_SUPERTX
2126 supertx_enabled,
2127#endif // CONFIG_SUPERTX
2128 mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
2129 break;
2130#if CONFIG_EXT_PARTITION_TYPES
2131 case PARTITION_HORZ_A:
2132 decode_block(pbi, xd,
2133#if CONFIG_SUPERTX
2134 supertx_enabled,
2135#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002136 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002137 decode_block(pbi, xd,
2138#if CONFIG_SUPERTX
2139 supertx_enabled,
2140#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002141 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002142 decode_block(pbi, xd,
2143#if CONFIG_SUPERTX
2144 supertx_enabled,
2145#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002146 mi_row + hbs, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002147 break;
2148 case PARTITION_HORZ_B:
2149 decode_block(pbi, xd,
2150#if CONFIG_SUPERTX
2151 supertx_enabled,
2152#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002153 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002154 decode_block(pbi, xd,
2155#if CONFIG_SUPERTX
2156 supertx_enabled,
2157#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002158 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002159 decode_block(pbi, xd,
2160#if CONFIG_SUPERTX
2161 supertx_enabled,
2162#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002163 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002164 break;
2165 case PARTITION_VERT_A:
2166 decode_block(pbi, xd,
2167#if CONFIG_SUPERTX
2168 supertx_enabled,
2169#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002170 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002171 decode_block(pbi, xd,
2172#if CONFIG_SUPERTX
2173 supertx_enabled,
2174#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002175 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002176 decode_block(pbi, xd,
2177#if CONFIG_SUPERTX
2178 supertx_enabled,
2179#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002180 mi_row, mi_col + hbs, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002181 break;
2182 case PARTITION_VERT_B:
2183 decode_block(pbi, xd,
2184#if CONFIG_SUPERTX
2185 supertx_enabled,
2186#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002187 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002188 decode_block(pbi, xd,
2189#if CONFIG_SUPERTX
2190 supertx_enabled,
2191#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002192 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002193 decode_block(pbi, xd,
2194#if CONFIG_SUPERTX
2195 supertx_enabled,
2196#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002197 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002198 break;
2199#endif
2200 default: assert(0 && "Invalid partition type");
2201 }
2202 }
2203
2204#if CONFIG_SUPERTX
2205 if (supertx_enabled && read_token) {
2206 uint8_t *dst_buf[3];
2207 int dst_stride[3], i;
2208 int offset = mi_row * cm->mi_stride + mi_col;
2209
2210 set_segment_id_supertx(cm, mi_row, mi_col, bsize);
2211
David Barker3aec8d62017-01-31 14:55:32 +00002212#if CONFIG_DELTA_Q
2213 if (cm->delta_q_present_flag) {
2214 for (i = 0; i < MAX_SEGMENTS; i++) {
2215 xd->plane[0].seg_dequant[i][0] =
2216 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
2217 xd->plane[0].seg_dequant[i][1] =
2218 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
2219 xd->plane[1].seg_dequant[i][0] =
2220 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2221 xd->plane[1].seg_dequant[i][1] =
2222 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2223 xd->plane[2].seg_dequant[i][0] =
2224 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2225 xd->plane[2].seg_dequant[i][1] =
2226 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2227 }
2228 }
2229#endif
2230
Yaowu Xuc27fc142016-08-22 16:08:15 -07002231 xd->mi = cm->mi_grid_visible + offset;
2232 xd->mi[0] = cm->mi + offset;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002233#if CONFIG_DEPENDENT_HORZTILES
2234 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2235 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols,
2236 cm->dependent_horz_tiles);
2237#else
Jingning Han5b7706a2016-12-21 09:55:10 -08002238 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2239 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002240#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002241 set_skip_context(xd, mi_row, mi_col);
2242 skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
2243 if (skip) {
2244 reset_skip_context(xd, bsize);
2245 } else {
2246#if CONFIG_EXT_TX
Sarah Parkere68a3e42017-02-16 14:03:24 -08002247 if (get_ext_tx_types(supertx_size, bsize, 1, cm->reduced_tx_set_used) >
2248 1) {
2249 const int eset =
2250 get_ext_tx_set(supertx_size, bsize, 1, cm->reduced_tx_set_used);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002251 if (eset > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002252 txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
Michael Bebenita6048d052016-08-25 14:40:54 -07002253 cm->fc->inter_ext_tx_prob[eset][supertx_size],
2254 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002255 if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
2256 }
2257 }
2258#else
2259 if (supertx_size < TX_32X32) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002260 txfm = aom_read_tree(r, av1_ext_tx_tree,
Jingning Han8f6eb182016-10-19 13:48:57 -07002261 cm->fc->inter_ext_tx_prob[supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002262 if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
2263 }
2264#endif // CONFIG_EXT_TX
2265 }
2266
Yaowu Xuf883b422016-08-30 14:01:10 -07002267 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002268 for (i = 0; i < MAX_MB_PLANE; i++) {
2269 dst_buf[i] = xd->plane[i].dst.buf;
2270 dst_stride[i] = xd->plane[i].dst.stride;
2271 }
2272 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
2273 bsize, dst_buf, dst_stride);
2274
2275 if (!skip) {
2276 int eobtotal = 0;
2277 MB_MODE_INFO *mbmi;
2278 set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
2279 mbmi = &xd->mi[0]->mbmi;
2280 mbmi->tx_type = txfm;
2281 assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
2282 for (i = 0; i < MAX_MB_PLANE; ++i) {
2283 const struct macroblockd_plane *const pd = &xd->plane[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002284 int row, col;
Angie Chiang7fcfee42017-02-24 15:51:03 -08002285 const TX_SIZE tx_size = get_tx_size(i, xd);
Jingning Han5b7706a2016-12-21 09:55:10 -08002286 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Jingning Han32b20282016-10-28 15:42:44 -07002287 const int stepr = tx_size_high_unit[tx_size];
2288 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han5b7706a2016-12-21 09:55:10 -08002289 const int max_blocks_wide = max_block_wide(xd, plane_bsize, i);
2290 const int max_blocks_high = max_block_high(xd, plane_bsize, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002291
2292 for (row = 0; row < max_blocks_high; row += stepr)
2293 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07002294 eobtotal += reconstruct_inter_block(
2295 cm, xd, r, mbmi->segment_id_supertx, i, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002296 }
Jingning Han24f24a52016-12-27 10:13:28 -08002297 if ((unify_bsize || !(subsize < BLOCK_8X8)) && eobtotal == 0) skip = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002298 }
2299 set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
2300 }
2301#endif // CONFIG_SUPERTX
2302
2303#if CONFIG_EXT_PARTITION_TYPES
2304 if (bsize >= BLOCK_8X8) {
2305 switch (partition) {
2306 case PARTITION_SPLIT:
2307 if (bsize > BLOCK_8X8) break;
2308 case PARTITION_NONE:
2309 case PARTITION_HORZ:
2310 case PARTITION_VERT:
2311 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
2312 break;
2313 case PARTITION_HORZ_A:
2314 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2315 update_partition_context(xd, mi_row + hbs, mi_col, subsize, subsize);
2316 break;
2317 case PARTITION_HORZ_B:
2318 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2319 update_partition_context(xd, mi_row + hbs, mi_col, bsize2, subsize);
2320 break;
2321 case PARTITION_VERT_A:
2322 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2323 update_partition_context(xd, mi_row, mi_col + hbs, subsize, subsize);
2324 break;
2325 case PARTITION_VERT_B:
2326 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2327 update_partition_context(xd, mi_row, mi_col + hbs, bsize2, subsize);
2328 break;
2329 default: assert(0 && "Invalid partition type");
2330 }
2331 }
2332#else
2333 // update partition context
2334 if (bsize >= BLOCK_8X8 &&
2335 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
Jingning Han1beb0102016-12-07 11:08:30 -08002336 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
David Barkerf8935c92016-10-26 14:54:06 +01002337#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xud71be782016-10-14 08:47:03 -07002338
Jean-Marc Valin01435132017-02-18 14:12:53 -05002339#if CONFIG_CDEF
David Barker9739f362016-11-10 09:29:32 +00002340#if CONFIG_EXT_PARTITION
2341 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128) {
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002342 if (!sb_all_skip(cm, mi_row, mi_col)) {
David Barker9739f362016-11-10 09:29:32 +00002343 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002344 aom_read_literal(r, cm->dering_bits, ACCT_STR);
2345 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.clpf_strength =
2346 aom_read_literal(r, cm->clpf_bits, ACCT_STR);
David Barker9739f362016-11-10 09:29:32 +00002347 } else {
2348 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002349 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]
2350 ->mbmi.clpf_strength = 0;
David Barker9739f362016-11-10 09:29:32 +00002351 }
2352 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64) {
2353#else
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002354 if (bsize == BLOCK_64X64) {
David Barker9739f362016-11-10 09:29:32 +00002355#endif
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002356 if (!sb_all_skip(cm, mi_row, mi_col)) {
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002357 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002358 aom_read_literal(r, cm->dering_bits, ACCT_STR);
2359 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.clpf_strength =
2360 aom_read_literal(r, cm->clpf_bits, ACCT_STR);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002361 } else {
2362 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002363 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]
2364 ->mbmi.clpf_strength = 0;
Yaowu Xud71be782016-10-14 08:47:03 -07002365 }
2366 }
Jean-Marc Valin01435132017-02-18 14:12:53 -05002367#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002368}
2369
Yaowu Xuc27fc142016-08-22 16:08:15 -07002370static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
2371 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002372 struct aom_internal_error_info *error_info,
Alex Converseeb780e72016-12-13 12:46:41 -08002373 aom_reader *r,
2374#if CONFIG_ANS && ANS_MAX_SYMBOLS
2375 int window_size,
2376#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
2377 aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002378 // Validate the calculated partition length. If the buffer
2379 // described by the partition can't be fully read, then restrict
2380 // it to the portion that can be (for EC mode) or throw an error.
2381 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002382 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002383 "Truncated packet or corrupt tile length");
2384
Alex Converse2cdf0d82016-12-13 13:53:09 -08002385#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08002386 r->window_size = window_size;
Alex Converse2cdf0d82016-12-13 13:53:09 -08002387#endif
Alex Converse346440b2017-01-03 13:47:37 -08002388 if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07002389 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002390 "Failed to allocate bool decoder %d", 1);
2391}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002392
Alex Conversea9598cd2017-02-03 14:18:05 -08002393#if !CONFIG_PVQ && !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Yaowu Xuf883b422016-08-30 14:01:10 -07002394static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
2395 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002396 int i, j, k, l, m;
Alex Conversea9598cd2017-02-03 14:18:05 -08002397#if CONFIG_EC_ADAPT
2398 const int node_limit = UNCONSTRAINED_NODES - 1;
2399#else
Thomas9ac55082016-09-23 18:04:17 +01002400 const int node_limit = UNCONSTRAINED_NODES;
Alex Conversea9598cd2017-02-03 14:18:05 -08002401#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002402
Michael Bebenita6048d052016-08-25 14:40:54 -07002403 if (aom_read_bit(r, ACCT_STR))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002404 for (i = 0; i < PLANE_TYPES; ++i)
2405 for (j = 0; j < REF_TYPES; ++j)
2406 for (k = 0; k < COEF_BANDS; ++k)
2407 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
Thomas9ac55082016-09-23 18:04:17 +01002408 for (m = 0; m < node_limit; ++m)
Michael Bebenita6048d052016-08-25 14:40:54 -07002409 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002410}
2411
Yaowu Xuf883b422016-08-30 14:01:10 -07002412static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002413 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
2414 TX_SIZE tx_size;
Jingning Han83630632016-12-16 11:27:25 -08002415 for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002416 read_coef_probs_common(fc->coef_probs[tx_size], r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002417}
Yushin Cho77bba8d2016-11-04 16:36:56 -07002418#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002419
Yaowu Xuf883b422016-08-30 14:01:10 -07002420static void setup_segmentation(AV1_COMMON *const cm,
2421 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002422 struct segmentation *const seg = &cm->seg;
2423 int i, j;
2424
2425 seg->update_map = 0;
2426 seg->update_data = 0;
2427
Yaowu Xuf883b422016-08-30 14:01:10 -07002428 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002429 if (!seg->enabled) return;
2430
2431 // Segmentation map update
2432 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2433 seg->update_map = 1;
2434 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002435 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002436 }
2437 if (seg->update_map) {
2438 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2439 seg->temporal_update = 0;
2440 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002441 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002442 }
2443 }
2444
2445 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07002446 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002447 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002448 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002449
Yaowu Xuf883b422016-08-30 14:01:10 -07002450 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002451
2452 for (i = 0; i < MAX_SEGMENTS; i++) {
2453 for (j = 0; j < SEG_LVL_MAX; j++) {
2454 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002455 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002456 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002457 av1_enable_segfeature(seg, i, j);
2458 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
2459 if (av1_is_segfeature_signed(j))
2460 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002461 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002462 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002463 }
2464 }
2465 }
2466}
2467
2468#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002469static void decode_restoration_mode(AV1_COMMON *cm,
2470 struct aom_read_bit_buffer *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002471 int p;
2472 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002473 if (aom_rb_read_bit(rb)) {
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002474 rsi->frame_restoration_type =
2475 aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002476 } else {
2477 rsi->frame_restoration_type =
2478 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
2479 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002480 for (p = 1; p < MAX_MB_PLANE; ++p) {
2481 cm->rst_info[p].frame_restoration_type =
2482 aom_rb_read_bit(rb) ? RESTORE_WIENER : RESTORE_NONE;
2483 }
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002484
2485 cm->rst_info[0].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2486 cm->rst_info[1].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2487 cm->rst_info[2].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2488 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
2489 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
2490 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
2491 rsi = &cm->rst_info[0];
2492 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2493 if (rsi->restoration_tilesize != RESTORATION_TILESIZE_MAX) {
2494 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2495 }
2496 cm->rst_info[1].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2497 cm->rst_info[2].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2498 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002499}
2500
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002501static void read_wiener_filter(WienerInfo *wiener_info, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002502 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002503 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2504 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002505 wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002506 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2507 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002508 wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002509 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2510 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002511 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002512 wiener_info->vfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002513 -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
2514 wiener_info->vfilter[2]);
2515
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002516 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002517 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2518 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002519 wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002520 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2521 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002522 wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002523 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2524 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002525 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002526 wiener_info->hfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002527 -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
2528 wiener_info->hfilter[2]);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002529}
2530
2531static void read_sgrproj_filter(SgrprojInfo *sgrproj_info, aom_reader *rb) {
2532 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
2533 sgrproj_info->xqd[0] =
2534 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN0;
2535 sgrproj_info->xqd[1] =
2536 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN1;
2537}
2538
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002539static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002540 int i, p;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002541 const int ntiles = av1_get_rest_ntiles(cm->width, cm->height,
2542 cm->rst_info[0].restoration_tilesize,
2543 NULL, NULL, NULL, NULL);
2544 const int ntiles_uv = av1_get_rest_ntiles(
2545 ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
2546 ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y),
2547 cm->rst_info[1].restoration_tilesize, NULL, NULL, NULL, NULL);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002548 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002549 if (rsi->frame_restoration_type != RESTORE_NONE) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002550 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002551 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002552 rsi->restoration_type[i] =
2553 aom_read_tree(rb, av1_switchable_restore_tree,
2554 cm->fc->switchable_restore_prob, ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002555 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002556 read_wiener_filter(&rsi->wiener_info[i], rb);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002557 } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002558 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002559 }
2560 }
2561 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002562 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002563 if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002564 rsi->restoration_type[i] = RESTORE_WIENER;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002565 read_wiener_filter(&rsi->wiener_info[i], rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002566 } else {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002567 rsi->restoration_type[i] = RESTORE_NONE;
2568 }
2569 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002570 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002571 for (i = 0; i < ntiles; ++i) {
2572 if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
2573 rsi->restoration_type[i] = RESTORE_SGRPROJ;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002574 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
2575 } else {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002576 rsi->restoration_type[i] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002577 }
2578 }
2579 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002580 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002581 for (p = 1; p < MAX_MB_PLANE; ++p) {
2582 rsi = &cm->rst_info[p];
2583 if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08002584 for (i = 0; i < ntiles_uv; ++i) {
2585 if (ntiles_uv > 1)
2586 rsi->restoration_type[i] =
2587 aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR) ? RESTORE_WIENER
2588 : RESTORE_NONE;
2589 else
2590 rsi->restoration_type[i] = RESTORE_WIENER;
2591 if (rsi->restoration_type[i] == RESTORE_WIENER) {
2592 read_wiener_filter(&rsi->wiener_info[i], rb);
2593 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002594 }
2595 }
2596 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002597}
2598#endif // CONFIG_LOOP_RESTORATION
2599
Yaowu Xuf883b422016-08-30 14:01:10 -07002600static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002601 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07002602 lf->filter_level = aom_rb_read_literal(rb, 6);
2603 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002604
2605 // Read in loop filter deltas applied at the MB level based on mode or ref
2606 // frame.
2607 lf->mode_ref_delta_update = 0;
2608
Yaowu Xuf883b422016-08-30 14:01:10 -07002609 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002610 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002611 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002612 if (lf->mode_ref_delta_update) {
2613 int i;
2614
2615 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002616 if (aom_rb_read_bit(rb))
2617 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002618
2619 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002620 if (aom_rb_read_bit(rb))
2621 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002622 }
2623 }
2624}
2625
Jean-Marc Valin01435132017-02-18 14:12:53 -05002626#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002627static void setup_cdef(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
2628 cm->dering_level = aom_rb_read_literal(rb, DERING_LEVEL_BITS);
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02002629 cm->clpf_strength_u = aom_rb_read_literal(rb, 2);
2630 cm->clpf_strength_v = aom_rb_read_literal(rb, 2);
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002631 id_to_levels(cm->dering_lev, cm->clpf_str, cm->dering_level);
2632 cdef_get_bits(cm->dering_lev, cm->clpf_str, &cm->dering_bits, &cm->clpf_bits);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002633}
Jean-Marc Valin01435132017-02-18 14:12:53 -05002634#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002635
Yaowu Xuf883b422016-08-30 14:01:10 -07002636static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2637 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002638}
2639
Yaowu Xuf883b422016-08-30 14:01:10 -07002640static void setup_quantization(AV1_COMMON *const cm,
2641 struct aom_read_bit_buffer *rb) {
2642 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002643 cm->y_dc_delta_q = read_delta_q(rb);
2644 cm->uv_dc_delta_q = read_delta_q(rb);
2645 cm->uv_ac_delta_q = read_delta_q(rb);
2646 cm->dequant_bit_depth = cm->bit_depth;
2647#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002648 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002649 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002650 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2651 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002652 } else {
2653 cm->min_qmlevel = 0;
2654 cm->max_qmlevel = 0;
2655 }
2656#endif
2657}
2658
Yaowu Xuf883b422016-08-30 14:01:10 -07002659static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002660 // Build y/uv dequant values based on segmentation.
2661 int i = 0;
2662#if CONFIG_AOM_QM
2663 int lossless;
2664 int j = 0;
2665 int qmlevel;
2666 int using_qm = cm->using_qmatrix;
2667 int minqm = cm->min_qmlevel;
2668 int maxqm = cm->max_qmlevel;
2669#endif
2670#if CONFIG_NEW_QUANT
2671 int b;
2672 int dq;
2673#endif // CONFIG_NEW_QUANT
2674 if (cm->seg.enabled) {
2675 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002676 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002677 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002678 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2679 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002680 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002681 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002682 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002683 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002684#if CONFIG_AOM_QM
2685 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2686 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2687 // NB: depends on base index so there is only 1 set per frame
2688 // No quant weighting when lossless or signalled not using QM
2689 qmlevel = (lossless || using_qm == 0)
2690 ? NUM_QM_LEVELS - 1
2691 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2692 for (j = 0; j < TX_SIZES; ++j) {
2693 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2694 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2695 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2696 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2697 }
2698#endif // CONFIG_AOM_QM
2699#if CONFIG_NEW_QUANT
2700 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2701 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002702 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002703 cm->y_dequant_nuq[i][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002704 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002705 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002706 }
2707 }
2708#endif // CONFIG_NEW_QUANT
2709 }
2710 } else {
2711 const int qindex = cm->base_qindex;
2712 // When segmentation is disabled, only the first value is used. The
2713 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002714 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2715 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002716 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002717 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002718 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002719 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002720#if CONFIG_AOM_QM
2721 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2722 cm->uv_ac_delta_q == 0;
2723 // No quant weighting when lossless or signalled not using QM
2724 qmlevel = (lossless || using_qm == 0)
2725 ? NUM_QM_LEVELS - 1
2726 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2727 for (j = 0; j < TX_SIZES; ++j) {
2728 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2729 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2730 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2731 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2732 }
2733#endif
2734#if CONFIG_NEW_QUANT
2735 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2736 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002737 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002738 cm->y_dequant_nuq[0][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002739 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002740 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002741 }
2742 }
2743#endif // CONFIG_NEW_QUANT
2744 }
2745}
2746
Angie Chiang5678ad92016-11-21 09:38:40 -08002747static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002748 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07002749 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002750}
2751
Yaowu Xuf883b422016-08-30 14:01:10 -07002752static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002753 cm->render_width = cm->width;
2754 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002755 if (aom_rb_read_bit(rb))
2756 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002757}
2758
Yaowu Xuf883b422016-08-30 14:01:10 -07002759static void resize_mv_buffer(AV1_COMMON *cm) {
2760 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002761 cm->cur_frame->mi_rows = cm->mi_rows;
2762 cm->cur_frame->mi_cols = cm->mi_cols;
2763 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002764 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002765 sizeof(*cm->cur_frame->mvs)));
2766}
2767
Yaowu Xuf883b422016-08-30 14:01:10 -07002768static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002769#if CONFIG_SIZE_LIMIT
2770 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002771 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002772 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2773 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2774#endif
2775 if (cm->width != width || cm->height != height) {
2776 const int new_mi_rows =
2777 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2778 const int new_mi_cols =
2779 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2780
Yaowu Xuf883b422016-08-30 14:01:10 -07002781 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002782 // dimensions as well as the overall size.
2783 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002784 if (av1_alloc_context_buffers(cm, width, height))
2785 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002786 "Failed to allocate context buffers");
2787 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002788 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002789 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002790 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002791 cm->width = width;
2792 cm->height = height;
2793 }
2794 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2795 cm->mi_cols > cm->cur_frame->mi_cols) {
2796 resize_mv_buffer(cm);
2797 }
2798}
2799
Yaowu Xuf883b422016-08-30 14:01:10 -07002800static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002801 int width, height;
2802 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002803 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002804 resize_context_buffers(cm, width, height);
2805 setup_render_size(cm, rb);
2806
2807 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002808 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002809 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2810 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002811#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002812 cm->use_highbitdepth,
2813#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002814 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002815 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2816 pool->cb_priv)) {
2817 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002818 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002819 "Failed to allocate frame buffer");
2820 }
2821 unlock_buffer_pool(pool);
2822
2823 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2824 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2825 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2826 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2827 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2828 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2829 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2830}
2831
Yaowu Xuf883b422016-08-30 14:01:10 -07002832static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002833 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002834 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002835 int this_xss, int this_yss) {
2836 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2837 ref_yss == this_yss;
2838}
2839
Yaowu Xuf883b422016-08-30 14:01:10 -07002840static void setup_frame_size_with_refs(AV1_COMMON *cm,
2841 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002842 int width, height;
2843 int found = 0, i;
2844 int has_valid_ref_frame = 0;
2845 BufferPool *const pool = cm->buffer_pool;
2846 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002847 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002848 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2849 width = buf->y_crop_width;
2850 height = buf->y_crop_height;
2851 cm->render_width = buf->render_width;
2852 cm->render_height = buf->render_height;
2853 found = 1;
2854 break;
2855 }
2856 }
2857
2858 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002859 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002860 setup_render_size(cm, rb);
2861 }
2862
2863 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002864 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002865 "Invalid frame size");
2866
2867 // Check to make sure at least one of frames that this frame references
2868 // has valid dimensions.
2869 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2870 RefBuffer *const ref_frame = &cm->frame_refs[i];
2871 has_valid_ref_frame |=
2872 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2873 ref_frame->buf->y_crop_height, width, height);
2874 }
2875 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002876 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002877 "Referenced frame has invalid size");
2878 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2879 RefBuffer *const ref_frame = &cm->frame_refs[i];
2880 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
2881 ref_frame->buf->subsampling_x,
2882 ref_frame->buf->subsampling_y, cm->bit_depth,
2883 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07002884 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002885 "Referenced frame has incompatible color format");
2886 }
2887
2888 resize_context_buffers(cm, width, height);
2889
2890 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002891 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002892 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2893 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002894#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002895 cm->use_highbitdepth,
2896#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002897 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002898 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2899 pool->cb_priv)) {
2900 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002901 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002902 "Failed to allocate frame buffer");
2903 }
2904 unlock_buffer_pool(pool);
2905
2906 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2907 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2908 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2909 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2910 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2911 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2912 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2913}
2914
Yaowu Xuf883b422016-08-30 14:01:10 -07002915static void read_tile_info(AV1Decoder *const pbi,
2916 struct aom_read_bit_buffer *const rb) {
2917 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002918#if CONFIG_EXT_TILE
2919// Read the tile width/height
2920#if CONFIG_EXT_PARTITION
2921 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002922 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
2923 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002924 } else
2925#endif // CONFIG_EXT_PARTITION
2926 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002927 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
2928 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002929 }
2930
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002931#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002932 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002933#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002934
Yaowu Xuc27fc142016-08-22 16:08:15 -07002935 cm->tile_width <<= cm->mib_size_log2;
2936 cm->tile_height <<= cm->mib_size_log2;
2937
Yaowu Xuf883b422016-08-30 14:01:10 -07002938 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
2939 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002940
2941 // Get the number of tiles
2942 cm->tile_cols = 1;
2943 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
2944
2945 cm->tile_rows = 1;
2946 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
2947
2948 if (cm->tile_cols * cm->tile_rows > 1) {
2949 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07002950 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2951 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002952 }
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002953
2954#if CONFIG_DEPENDENT_HORZTILES
2955 if (cm->tile_rows <= 1)
2956 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
2957 else
2958 cm->dependent_horz_tiles = 0;
2959#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002960#else
2961 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07002962 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002963
2964 // columns
2965 max_ones = max_log2_tile_cols - min_log2_tile_cols;
2966 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07002967 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002968
2969 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07002970 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002971 "Invalid number of tile columns");
2972
2973 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07002974 cm->log2_tile_rows = aom_rb_read_bit(rb);
2975 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002976#if CONFIG_DEPENDENT_HORZTILES
2977 if (cm->log2_tile_rows != 0)
2978 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
2979 else
2980 cm->dependent_horz_tiles = 0;
2981#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002982#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002983 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002984#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002985
Yaowu Xuc27fc142016-08-22 16:08:15 -07002986 cm->tile_cols = 1 << cm->log2_tile_cols;
2987 cm->tile_rows = 1 << cm->log2_tile_rows;
2988
2989 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
2990 cm->tile_width >>= cm->log2_tile_cols;
2991 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
2992 cm->tile_height >>= cm->log2_tile_rows;
2993
2994 // round to integer multiples of superblock size
2995 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
2996 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
2997
Thomas Davies4974e522016-11-07 17:44:05 +00002998// tile size magnitude
2999#if !CONFIG_TILE_GROUPS
3000 if (cm->tile_rows > 1 || cm->tile_cols > 1)
3001#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003002 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003003#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00003004
Thomas Davies80188d12016-10-26 16:08:35 -07003005#if CONFIG_TILE_GROUPS
3006 // Store an index to the location of the tile group information
3007 pbi->tg_size_bit_offset = rb->bit_offset;
3008 pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
3009 if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
3010 pbi->tg_start =
3011 aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3012 pbi->tg_size =
3013 1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3014 }
3015#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003016}
3017
3018static int mem_get_varsize(const uint8_t *src, const int sz) {
3019 switch (sz) {
3020 case 1: return src[0];
3021 case 2: return mem_get_le16(src);
3022 case 3: return mem_get_le24(src);
3023 case 4: return mem_get_le32(src);
3024 default: assert("Invalid size" && 0); return -1;
3025 }
3026}
3027
3028#if CONFIG_EXT_TILE
3029// Reads the next tile returning its size and adjusting '*data' accordingly
3030// based on 'is_last'.
3031static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07003032 struct aom_internal_error_info *error_info,
3033 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003034 void *decrypt_state,
3035 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
3036 int tile_size_bytes, int col, int row) {
3037 size_t size;
3038
3039 size_t copy_size = 0;
3040 const uint8_t *copy_data = NULL;
3041
3042 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003043 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003044 "Truncated packet or corrupt tile length");
3045 if (decrypt_cb) {
3046 uint8_t be_data[4];
3047 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3048
3049 // Only read number of bytes in cm->tile_size_bytes.
3050 size = mem_get_varsize(be_data, tile_size_bytes);
3051 } else {
3052 size = mem_get_varsize(*data, tile_size_bytes);
3053 }
3054
3055 // The top bit indicates copy mode
3056 if ((size >> (tile_size_bytes * 8 - 1)) == 1) {
3057 // The remaining bits in the top byte signal the row offset
3058 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
3059
3060 // Currently, only use tiles in same column as reference tiles.
3061 copy_data = tile_buffers[row - offset][col].data;
3062 copy_size = tile_buffers[row - offset][col].size;
3063 size = 0;
3064 }
3065
3066 *data += tile_size_bytes;
3067
3068 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003069 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003070 "Truncated packet or corrupt tile size");
3071
3072 if (size > 0) {
3073 tile_buffers[row][col].data = *data;
3074 tile_buffers[row][col].size = size;
3075 } else {
3076 tile_buffers[row][col].data = copy_data;
3077 tile_buffers[row][col].size = copy_size;
3078 }
3079
3080 *data += size;
3081
3082 tile_buffers[row][col].raw_data_end = *data;
3083}
3084
3085static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003086 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003087 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003088 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003089 const int tile_cols = cm->tile_cols;
3090 const int tile_rows = cm->tile_rows;
3091 const int have_tiles = tile_cols * tile_rows > 1;
3092
3093 if (!have_tiles) {
3094 const uint32_t tile_size = data_end - data;
3095 tile_buffers[0][0].data = data;
3096 tile_buffers[0][0].size = tile_size;
3097 tile_buffers[0][0].raw_data_end = NULL;
3098 } else {
3099 // We locate only the tile buffers that are required, which are the ones
3100 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
3101 // need the last (bottom right) tile buffer, as we need to know where the
3102 // end of the compressed frame buffer is for proper superframe decoding.
3103
3104 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
3105 const uint8_t *const data_start = data;
3106
Yaowu Xuf883b422016-08-30 14:01:10 -07003107 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003108 const int single_row = pbi->dec_tile_row >= 0;
3109 const int tile_rows_start = single_row ? dec_tile_row : 0;
3110 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003111 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003112 const int single_col = pbi->dec_tile_col >= 0;
3113 const int tile_cols_start = single_col ? dec_tile_col : 0;
3114 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3115
3116 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
3117 const int tile_size_bytes = pbi->tile_size_bytes;
3118
3119 size_t tile_col_size;
3120 int r, c;
3121
3122 // Read tile column sizes for all columns (we need the last tile buffer)
3123 for (c = 0; c < tile_cols; ++c) {
3124 const int is_last = c == tile_cols - 1;
3125 if (!is_last) {
3126 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
3127 data += tile_col_size_bytes;
3128 tile_col_data_end[c] = data + tile_col_size;
3129 } else {
3130 tile_col_size = data_end - data;
3131 tile_col_data_end[c] = data_end;
3132 }
3133 data += tile_col_size;
3134 }
3135
3136 data = data_start;
3137
3138 // Read the required tile sizes.
3139 for (c = tile_cols_start; c < tile_cols_end; ++c) {
3140 const int is_last = c == tile_cols - 1;
3141
3142 if (c > 0) data = tile_col_data_end[c - 1];
3143
3144 if (!is_last) data += tile_col_size_bytes;
3145
3146 // Get the whole of the last column, otherwise stop at the required tile.
3147 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
3148 tile_buffers[r][c].col = c;
3149
3150 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3151 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3152 tile_size_bytes, c, r);
3153 }
3154 }
3155
3156 // If we have not read the last column, then read it to get the last tile.
3157 if (tile_cols_end != tile_cols) {
3158 c = tile_cols - 1;
3159
3160 data = tile_col_data_end[c - 1];
3161
3162 for (r = 0; r < tile_rows; ++r) {
3163 tile_buffers[r][c].col = c;
3164
3165 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3166 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3167 tile_size_bytes, c, r);
3168 }
3169 }
3170 }
3171}
3172#else
3173// Reads the next tile returning its size and adjusting '*data' accordingly
3174// based on 'is_last'.
3175static void get_tile_buffer(const uint8_t *const data_end,
3176 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07003177 struct aom_internal_error_info *error_info,
3178 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003179 void *decrypt_state, TileBufferDec *const buf) {
3180 size_t size;
3181
3182 if (!is_last) {
Yaowu Xu0a79a1b2017-02-17 13:04:54 -08003183 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003184 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003185 "Truncated packet or corrupt tile length");
3186
3187 if (decrypt_cb) {
3188 uint8_t be_data[4];
3189 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3190 size = mem_get_varsize(be_data, tile_size_bytes);
3191 } else {
3192 size = mem_get_varsize(*data, tile_size_bytes);
3193 }
3194 *data += tile_size_bytes;
3195
3196 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003197 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003198 "Truncated packet or corrupt tile size");
3199 } else {
3200 size = data_end - *data;
3201 }
3202
3203 buf->data = *data;
3204 buf->size = size;
3205
3206 *data += size;
3207}
3208
3209static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003210 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003211 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003212 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07003213#if CONFIG_TILE_GROUPS
3214 int r, c;
3215 const int tile_cols = cm->tile_cols;
3216 const int tile_rows = cm->tile_rows;
3217 int tc = 0;
3218 int first_tile_in_tg = 0;
3219 int hdr_offset;
3220 struct aom_read_bit_buffer rb_tg_hdr;
3221 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3222 const int num_tiles = tile_rows * tile_cols;
3223 const int num_bits = OD_ILOG(num_tiles) - 1;
3224 const int hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
3225 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
Fangwen Fu73126c02017-02-08 22:37:47 -08003226#if CONFIG_DEPENDENT_HORZTILES
3227 int tile_group_start_col = 0;
3228 int tile_group_start_row = 0;
3229#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003230
3231 for (r = 0; r < tile_rows; ++r) {
3232 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07003233 TileBufferDec *const buf = &tile_buffers[r][c];
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003234 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
Thomas Davies80188d12016-10-26 16:08:35 -07003235 hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
3236
3237 buf->col = c;
3238 if (hdr_offset) {
3239 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
3240 rb_tg_hdr.bit_offset = tg_size_bit_offset;
3241 if (num_tiles) {
3242 pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
3243 pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
Fangwen Fu73126c02017-02-08 22:37:47 -08003244#if CONFIG_DEPENDENT_HORZTILES
3245 tile_group_start_row = r;
3246 tile_group_start_col = c;
3247#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003248 }
3249 }
3250 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
3251 data += hdr_offset;
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003252 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last,
3253 &pbi->common.error, &data, pbi->decrypt_cb,
3254 pbi->decrypt_state, buf);
Fangwen Fu73126c02017-02-08 22:37:47 -08003255#if CONFIG_DEPENDENT_HORZTILES
3256 cm->tile_group_start_row[r][c] = tile_group_start_row;
3257 cm->tile_group_start_col[r][c] = tile_group_start_col;
3258#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003259 }
3260 }
3261#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003262 int r, c;
3263 const int tile_cols = cm->tile_cols;
3264 const int tile_rows = cm->tile_rows;
3265
3266 for (r = 0; r < tile_rows; ++r) {
3267 for (c = 0; c < tile_cols; ++c) {
3268 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
3269 TileBufferDec *const buf = &tile_buffers[r][c];
3270 buf->col = c;
3271 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
3272 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
3273 }
3274 }
Thomas Davies80188d12016-10-26 16:08:35 -07003275#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003276}
3277#endif // CONFIG_EXT_TILE
3278
Yushin Cho77bba8d2016-11-04 16:36:56 -07003279#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003280static void daala_dec_init(AV1_COMMON *const cm, daala_dec_ctx *daala_dec,
Nathan E. Eggeab083972016-12-28 15:31:46 -05003281 aom_reader *r) {
3282 daala_dec->r = r;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003283 od_adapt_ctx_reset(&daala_dec->state.adapt, 0);
3284
Yushin Cho70669122016-12-08 09:53:14 -10003285 // TODO(yushin) : activity masking info needs be signaled by a bitstream
3286 daala_dec->use_activity_masking = AV1_PVQ_ENABLE_ACTIVITY_MASKING;
3287
Yushin Cho7a428ba2017-01-12 16:28:49 -08003288#if !CONFIG_DAALA_DIST
3289 daala_dec->use_activity_masking = 0;
3290#endif
3291
Yushin Cho70669122016-12-08 09:53:14 -10003292 if (daala_dec->use_activity_masking)
3293 daala_dec->qm = OD_HVS_QM;
3294 else
3295 daala_dec->qm = OD_FLAT_QM;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003296
3297 od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
3298 daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
Yushin Cho70669122016-12-08 09:53:14 -10003299
3300 if (daala_dec->use_activity_masking) {
3301 int pli;
3302 int use_masking = daala_dec->use_activity_masking;
3303 int segment_id = 0;
3304 int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
3305
3306 for (pli = 0; pli < MAX_MB_PLANE; pli++) {
3307 int i;
3308 int q;
3309
3310 q = qindex;
3311 if (q <= OD_DEFAULT_QMS[use_masking][0][pli].interp_q << OD_COEFF_SHIFT) {
3312 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3313 &OD_DEFAULT_QMS[use_masking][0][pli], NULL);
3314 } else {
3315 i = 0;
3316 while (OD_DEFAULT_QMS[use_masking][i + 1][pli].qm_q4 != NULL &&
3317 q > OD_DEFAULT_QMS[use_masking][i + 1][pli].interp_q
3318 << OD_COEFF_SHIFT) {
3319 i++;
3320 }
3321 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3322 &OD_DEFAULT_QMS[use_masking][i][pli],
3323 &OD_DEFAULT_QMS[use_masking][i + 1][pli]);
3324 }
3325 }
3326 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07003327}
Yushin Cho70669122016-12-08 09:53:14 -10003328#endif // #if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003329
Yaowu Xuf883b422016-08-30 14:01:10 -07003330static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003331 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003332 AV1_COMMON *const cm = &pbi->common;
3333 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003334 const int tile_cols = cm->tile_cols;
3335 const int tile_rows = cm->tile_rows;
3336 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07003337 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003338#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003339 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003340 const int single_row = pbi->dec_tile_row >= 0;
3341 const int tile_rows_start = single_row ? dec_tile_row : 0;
3342 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003343 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003344 const int single_col = pbi->dec_tile_col >= 0;
3345 const int tile_cols_start = single_col ? dec_tile_col : 0;
3346 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3347 const int inv_col_order = pbi->inv_tile_order && !single_col;
3348 const int inv_row_order = pbi->inv_tile_order && !single_row;
3349#else
3350 const int tile_rows_start = 0;
3351 const int tile_rows_end = tile_rows;
3352 const int tile_cols_start = 0;
3353 const int tile_cols_end = tile_cols;
3354 const int inv_col_order = pbi->inv_tile_order;
3355 const int inv_row_order = pbi->inv_tile_order;
3356#endif // CONFIG_EXT_TILE
3357 int tile_row, tile_col;
3358
hui su0d103572017-03-01 17:58:01 -08003359#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003360 cm->do_subframe_update = n_tiles == 1;
hui su0d103572017-03-01 17:58:01 -08003361#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003362
3363 if (cm->lf.filter_level && !cm->skip_loop_filter &&
3364 pbi->lf_worker.data1 == NULL) {
3365 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07003366 aom_memalign(32, sizeof(LFWorkerData)));
3367 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003368 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003369 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003370 "Loop filter thread creation failed");
3371 }
3372 }
3373
3374 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3375 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3376 // Be sure to sync as we might be resuming after a failed frame decode.
3377 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003378 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
3379 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003380 }
3381
3382 assert(tile_rows <= MAX_TILE_ROWS);
3383 assert(tile_cols <= MAX_TILE_COLS);
3384
3385 get_tile_buffers(pbi, data, data_end, tile_buffers);
3386
3387 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003388 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003389 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003390 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003391 pbi->allocated_tiles = n_tiles;
3392 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003393#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003394 if (pbi->acct_enabled) {
3395 aom_accounting_reset(&pbi->accounting);
3396 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003397#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003398 // Load all tile information into tile_data.
3399 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3400 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3401 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
3402 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
3403
3404 td->cm = cm;
3405 td->xd = pbi->mb;
3406 td->xd.corrupted = 0;
3407 td->xd.counts =
3408 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3409 ? &cm->counts
3410 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003411 av1_zero(td->dqcoeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003412#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003413 av1_zero(td->pvq_ref_coeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003414#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003415 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003416 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003417 &td->bit_reader,
3418#if CONFIG_ANS && ANS_MAX_SYMBOLS
3419 1 << cm->ans_window_size_log2,
3420#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3421 pbi->decrypt_cb, pbi->decrypt_state);
Michael Bebenita6048d052016-08-25 14:40:54 -07003422#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003423 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003424 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003425 } else {
David Barkerd971f402016-10-25 13:52:07 +01003426 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003427 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003428#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003429 av1_init_macroblockd(cm, &td->xd,
3430#if CONFIG_PVQ
3431 td->pvq_ref_coeff,
3432#endif
3433 td->dqcoeff);
3434#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003435 daala_dec_init(cm, &td->xd.daala_dec, &td->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003436#endif
Thomas Daviesf77d4ad2017-01-10 18:55:42 +00003437#if CONFIG_EC_ADAPT
3438 // Initialise the tile context from the frame context
3439 td->tctx = *cm->fc;
3440 td->xd.tile_ctx = &td->tctx;
3441#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003442#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003443 td->xd.plane[0].color_index_map = td->color_index_map[0];
3444 td->xd.plane[1].color_index_map = td->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003445#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003446 }
3447 }
3448
3449 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3450 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
3451 int mi_row = 0;
3452 TileInfo tile_info;
3453
Yaowu Xuf883b422016-08-30 14:01:10 -07003454 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003455
3456 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3457 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
3458 TileData *const td = pbi->tile_data + tile_cols * row + col;
Michael Bebenita6048d052016-08-25 14:40:54 -07003459#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003460 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003461 td->bit_reader.accounting->last_tell_frac =
3462 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003463 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003464#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003465
Yaowu Xuf883b422016-08-30 14:01:10 -07003466 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003467
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003468#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003469#if CONFIG_TILE_GROUPS
3470 av1_tile_set_tg_boundary(&tile_info, cm, tile_row, tile_col);
3471 if (!cm->dependent_horz_tiles || tile_row == 0 ||
3472 tile_info.tg_horz_boundary) {
3473#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003474 if (!cm->dependent_horz_tiles || tile_row == 0) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003475#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003476 av1_zero_above_context(cm, tile_info.mi_col_start,
3477 tile_info.mi_col_end);
3478 }
3479#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003480 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003481#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003482
3483 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
3484 mi_row += cm->mib_size) {
3485 int mi_col;
3486
Yaowu Xuf883b422016-08-30 14:01:10 -07003487 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003488
3489 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
3490 mi_col += cm->mib_size) {
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003491 av1_update_boundary_info(cm, &tile_info, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003492 decode_partition(pbi, &td->xd,
3493#if CONFIG_SUPERTX
3494 0,
3495#endif // CONFIG_SUPERTX
3496 mi_row, mi_col, &td->bit_reader, cm->sb_size,
3497 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003498#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3499 detoken_and_recon_sb(pbi, &td->xd, mi_row, mi_col, &td->bit_reader,
3500 cm->sb_size);
3501#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003502 }
Angie Chiangd0916d92017-03-10 17:54:18 -08003503 aom_merge_corrupted_flag(&pbi->mb.corrupted, td->xd.corrupted);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003504 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003505 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003506 "Failed to decode tile data");
hui su0d103572017-03-01 17:58:01 -08003507#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003508 if (cm->do_subframe_update &&
3509 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
hui su0161a932017-01-24 14:12:11 -08003510 const int mi_rows_per_update =
3511 MI_SIZE * AOMMAX(cm->mi_rows / MI_SIZE / COEF_PROBS_BUFS, 1);
3512 if ((mi_row + MI_SIZE) % mi_rows_per_update == 0 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003513 mi_row + MI_SIZE < cm->mi_rows &&
3514 cm->coef_probs_update_idx < COEF_PROBS_BUFS - 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003515 av1_partial_adapt_probs(cm, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003516 ++cm->coef_probs_update_idx;
3517 }
3518 }
hui su0d103572017-03-01 17:58:01 -08003519#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003520 }
3521 }
3522
3523 assert(mi_row > 0);
3524
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003525// when Parallel deblocking is enabled, deblocking should not
3526// be interleaved with decoding. Instead, deblocking should be done
3527// after the entire frame is decoded.
3528#if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003529 // Loopfilter one tile row.
3530 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3531 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003532 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003533 const int lf_end = tile_info.mi_row_end - cm->mib_size;
3534
3535 // Delay the loopfilter if the first tile row is only
3536 // a single superblock high.
3537 if (lf_end <= 0) continue;
3538
3539 // Decoding has completed. Finish up the loop filter in this thread.
3540 if (tile_info.mi_row_end >= cm->mi_rows) continue;
3541
3542 winterface->sync(&pbi->lf_worker);
3543 lf_data->start = lf_start;
3544 lf_data->stop = lf_end;
3545 if (pbi->max_threads > 1) {
3546 winterface->launch(&pbi->lf_worker);
3547 } else {
3548 winterface->execute(&pbi->lf_worker);
3549 }
3550 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003551#endif // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003552
3553 // After loopfiltering, the last 7 row pixels in each superblock row may
3554 // still be changed by the longest loopfilter of the next superblock row.
3555 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003556 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003557 }
3558
3559#if CONFIG_VAR_TX
3560 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003561 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
3562 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003563#else
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003564#if CONFIG_PARALLEL_DEBLOCKING
3565 // Loopfilter all rows in the frame in the frame.
3566 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3567 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3568 winterface->sync(&pbi->lf_worker);
3569 lf_data->start = 0;
3570 lf_data->stop = cm->mi_rows;
3571 winterface->execute(&pbi->lf_worker);
3572 }
3573#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003574 // Loopfilter remaining rows in the frame.
3575 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3576 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3577 winterface->sync(&pbi->lf_worker);
3578 lf_data->start = lf_data->stop;
3579 lf_data->stop = cm->mi_rows;
3580 winterface->execute(&pbi->lf_worker);
3581 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003582#endif // CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003583#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003584 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003585 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003586
3587#if CONFIG_EXT_TILE
3588 if (n_tiles == 1) {
3589#if CONFIG_ANS
3590 return data_end;
3591#else
3592 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07003593 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003594#endif // CONFIG_ANS
3595 } else {
3596 // Return the end of the last tile buffer
3597 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3598 }
3599#else
3600#if CONFIG_ANS
3601 return data_end;
3602#else
3603 {
3604 // Get last tile data.
3605 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003606 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003607 }
3608#endif // CONFIG_ANS
3609#endif // CONFIG_EXT_TILE
3610}
3611
3612static int tile_worker_hook(TileWorkerData *const tile_data,
3613 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003614 AV1Decoder *const pbi = tile_data->pbi;
3615 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003616 int mi_row, mi_col;
3617
3618 if (setjmp(tile_data->error_info.jmp)) {
3619 tile_data->error_info.setjmp = 0;
Angie Chiangd0916d92017-03-10 17:54:18 -08003620 aom_merge_corrupted_flag(&tile_data->xd.corrupted, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003621 return 0;
3622 }
3623
3624 tile_data->error_info.setjmp = 1;
3625 tile_data->xd.error_info = &tile_data->error_info;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003626#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003627#if CONFIG_TILE_GROUPS
3628 if (!cm->dependent_horz_tiles || tile->tg_horz_boundary) {
3629#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003630 if (!cm->dependent_horz_tiles) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003631#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003632 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
3633 }
3634#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003635 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003636#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003637
3638 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
3639 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003640 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003641
3642 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3643 mi_col += cm->mib_size) {
3644 decode_partition(pbi, &tile_data->xd,
3645#if CONFIG_SUPERTX
3646 0,
3647#endif
3648 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
3649 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003650#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3651 detoken_and_recon_sb(pbi, &tile_data->xd, mi_row, mi_col,
3652 &tile_data->bit_reader, cm->sb_size);
3653#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003654 }
3655 }
3656 return !tile_data->xd.corrupted;
3657}
3658
3659// sorts in descending order
3660static int compare_tile_buffers(const void *a, const void *b) {
3661 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
3662 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
3663 return (int)(buf2->size - buf1->size);
3664}
3665
Yaowu Xuf883b422016-08-30 14:01:10 -07003666static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003667 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003668 AV1_COMMON *const cm = &pbi->common;
3669 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003670 const int tile_cols = cm->tile_cols;
3671 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003672 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
clang-format67948d32016-09-07 22:40:40 -07003673 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003674#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003675 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003676 const int single_row = pbi->dec_tile_row >= 0;
3677 const int tile_rows_start = single_row ? dec_tile_row : 0;
3678 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003679 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003680 const int single_col = pbi->dec_tile_col >= 0;
3681 const int tile_cols_start = single_col ? dec_tile_col : 0;
3682 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3683#else
3684 const int tile_rows_start = 0;
3685 const int tile_rows_end = tile_rows;
3686 const int tile_cols_start = 0;
3687 const int tile_cols_end = tile_cols;
3688#endif // CONFIG_EXT_TILE
3689 int tile_row, tile_col;
3690 int i;
3691
3692#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3693 int final_worker = -1;
3694#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3695
3696 assert(tile_rows <= MAX_TILE_ROWS);
3697 assert(tile_cols <= MAX_TILE_COLS);
3698
3699 assert(tile_cols * tile_rows > 1);
3700
Yaowu Xuc27fc142016-08-22 16:08:15 -07003701 // TODO(jzern): See if we can remove the restriction of passing in max
3702 // threads to the decoder.
3703 if (pbi->num_tile_workers == 0) {
3704 const int num_threads = pbi->max_threads & ~1;
3705 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07003706 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003707 // Ensure tile data offsets will be properly aligned. This may fail on
3708 // platforms without DECLARE_ALIGNED().
3709 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3710 CHECK_MEM_ERROR(
3711 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003712 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003713 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07003714 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003715 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003716 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003717 ++pbi->num_tile_workers;
3718
3719 winterface->init(worker);
3720 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003721 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003722 "Tile decoder thread creation failed");
3723 }
3724 }
3725 }
3726
3727 // Reset tile decoding hook
3728 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003729 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003730 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003731 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003732 worker->data1 = &pbi->tile_worker_data[i];
3733 worker->data2 = &pbi->tile_worker_info[i];
3734 }
3735
3736 // Initialize thread frame counts.
3737 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3738 for (i = 0; i < num_workers; ++i) {
3739 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003740 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003741 }
3742 }
3743
3744 // Load tile data into tile_buffers
3745 get_tile_buffers(pbi, data, data_end, tile_buffers);
3746
3747 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3748 // Sort the buffers in this tile row based on size in descending order.
3749 qsort(&tile_buffers[tile_row][tile_cols_start],
3750 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
3751 compare_tile_buffers);
3752
3753 // Rearrange the tile buffers in this tile row such that per-tile group
3754 // the largest, and presumably the most difficult tile will be decoded in
3755 // the main thread. This should help minimize the number of instances
3756 // where the main thread is waiting for a worker to complete.
3757 {
3758 int group_start;
3759 for (group_start = tile_cols_start; group_start < tile_cols_end;
3760 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003761 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003762 const TileBufferDec largest = tile_buffers[tile_row][group_start];
3763 memmove(&tile_buffers[tile_row][group_start],
3764 &tile_buffers[tile_row][group_start + 1],
3765 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
3766 tile_buffers[tile_row][group_end - 1] = largest;
3767 }
3768 }
3769
3770 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
3771 // Launch workers for individual columns
3772 for (i = 0; i < num_workers && tile_col < tile_cols_end;
3773 ++i, ++tile_col) {
3774 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07003775 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003776 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
3777 TileInfo *const tile_info = (TileInfo *)worker->data2;
3778
3779 twd->pbi = pbi;
3780 twd->xd = pbi->mb;
3781 twd->xd.corrupted = 0;
3782 twd->xd.counts =
3783 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3784 ? &twd->counts
3785 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003786 av1_zero(twd->dqcoeff);
3787 av1_tile_init(tile_info, cm, tile_row, buf->col);
3788 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003789 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003790 &twd->bit_reader,
3791#if CONFIG_ANS && ANS_MAX_SYMBOLS
3792 1 << cm->ans_window_size_log2,
3793#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3794 pbi->decrypt_cb, pbi->decrypt_state);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003795 av1_init_macroblockd(cm, &twd->xd,
3796#if CONFIG_PVQ
3797 twd->pvq_ref_coeff,
3798#endif
3799 twd->dqcoeff);
3800#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003801 daala_dec_init(cm, &twd->xd.daala_dec, &twd->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003802#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003803#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003804 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
3805 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003806#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003807
3808 worker->had_error = 0;
3809 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
3810 winterface->execute(worker);
3811 } else {
3812 winterface->launch(worker);
3813 }
3814
3815#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3816 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
3817 final_worker = i;
3818 }
3819#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3820 }
3821
3822 // Sync all workers
3823 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003824 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003825 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07003826 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07003827 // info in cm. Additionally once the threads have been synced and an
3828 // error is detected, there's no point in continuing to decode tiles.
3829 pbi->mb.corrupted |= !winterface->sync(worker);
3830 }
3831 }
3832 }
3833
3834 // Accumulate thread frame counts.
3835 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3836 for (i = 0; i < num_workers; ++i) {
3837 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003838 av1_accumulate_frame_counts(&cm->counts, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003839 }
3840 }
3841
3842#if CONFIG_EXT_TILE
3843 // Return the end of the last tile buffer
3844 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3845#else
3846#if CONFIG_ANS
3847 return data_end;
3848#else
3849 assert(final_worker != -1);
3850 {
3851 TileWorkerData *const twd =
3852 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003853 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003854 }
3855#endif // CONFIG_ANS
3856#endif // CONFIG_EXT_TILE
3857}
3858
3859static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003860 AV1_COMMON *const cm = (AV1_COMMON *)data;
3861 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003862}
3863
Yaowu Xuf883b422016-08-30 14:01:10 -07003864static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3865 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003866 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003867 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003868 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003869 cm->bit_depth = AOM_BITS_8;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003870 }
3871
Yaowu Xuf883b422016-08-30 14:01:10 -07003872#if CONFIG_AOM_HIGHBITDEPTH
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003873 if (cm->bit_depth > AOM_BITS_8) {
3874 cm->use_highbitdepth = 1;
3875 } else {
Yaowu Xu345a22d2017-02-27 09:23:52 -08003876#if CONFIG_LOWBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003877 cm->use_highbitdepth = 0;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003878#else
3879 cm->use_highbitdepth = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003880#endif
3881 }
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003882#endif
3883
Yaowu Xuf883b422016-08-30 14:01:10 -07003884 cm->color_space = aom_rb_read_literal(rb, 3);
3885 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003886 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07003887 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003888 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003889 cm->subsampling_x = aom_rb_read_bit(rb);
3890 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003891 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003892 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003893 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07003894 if (aom_rb_read_bit(rb))
3895 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003896 "Reserved bit set");
3897 } else {
3898 cm->subsampling_y = cm->subsampling_x = 1;
3899 }
3900 } else {
3901 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
3902 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
3903 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
3904 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003905 if (aom_rb_read_bit(rb))
3906 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003907 "Reserved bit set");
3908 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003909 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003910 "4:4:4 color not supported in profile 0 or 2");
3911 }
3912 }
3913}
3914
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003915#if CONFIG_REFERENCE_BUFFER
3916void read_sequence_header(SequenceHeader *seq_params) {
3917 /* Placeholder for actually reading from the bitstream */
3918 seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
3919 seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
3920 seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
3921}
3922#endif
3923
Yaowu Xuf883b422016-08-30 14:01:10 -07003924static size_t read_uncompressed_header(AV1Decoder *pbi,
3925 struct aom_read_bit_buffer *rb) {
3926 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003927 MACROBLOCKD *const xd = &pbi->mb;
3928 BufferPool *const pool = cm->buffer_pool;
3929 RefCntBuffer *const frame_bufs = pool->frame_bufs;
3930 int i, mask, ref_index = 0;
3931 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003932
3933#if CONFIG_REFERENCE_BUFFER
3934 /* TODO: Move outside frame loop or inside key-frame branch */
3935 read_sequence_header(&pbi->seq_params);
3936#endif
3937
Yaowu Xuc27fc142016-08-22 16:08:15 -07003938 cm->last_frame_type = cm->frame_type;
3939 cm->last_intra_only = cm->intra_only;
3940
3941#if CONFIG_EXT_REFS
3942 // NOTE: By default all coded frames to be used as a reference
3943 cm->is_reference_frame = 1;
3944#endif // CONFIG_EXT_REFS
3945
Yaowu Xuf883b422016-08-30 14:01:10 -07003946 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
3947 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003948 "Invalid frame marker");
3949
Yaowu Xuf883b422016-08-30 14:01:10 -07003950 cm->profile = av1_read_profile(rb);
3951#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003952 if (cm->profile >= MAX_PROFILES)
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 "Unsupported bitstream profile");
3955#else
3956 if (cm->profile >= PROFILE_2)
Yaowu Xuf883b422016-08-30 14:01:10 -07003957 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003958 "Unsupported bitstream profile");
3959#endif
3960
Yaowu Xuf883b422016-08-30 14:01:10 -07003961 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003962
3963 if (cm->show_existing_frame) {
Yaowu Xu415ba932016-12-27 11:17:32 -08003964 // Show an existing frame directly.
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003965 const int existing_frame_idx = aom_rb_read_literal(rb, 3);
3966 const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
Yaowu Xu415ba932016-12-27 11:17:32 -08003967#if CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003968 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003969 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3970 int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003971 /* Compare display_frame_id with ref_frame_id and check valid for
3972 * referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003973 if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
3974 cm->valid_for_referencing[existing_frame_idx] == 0)
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003975 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3976 "Reference buffer frame ID mismatch");
3977 }
3978#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003979 lock_buffer_pool(pool);
3980 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
3981 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07003982 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003983 "Buffer %d does not contain a decoded frame",
3984 frame_to_show);
3985 }
3986 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
3987 unlock_buffer_pool(pool);
3988
3989 cm->lf.filter_level = 0;
3990 cm->show_frame = 1;
3991 pbi->refresh_frame_flags = 0;
3992
3993 if (cm->frame_parallel_decode) {
3994 for (i = 0; i < REF_FRAMES; ++i)
3995 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
3996 }
3997
3998 return 0;
3999 }
4000
Yaowu Xuf883b422016-08-30 14:01:10 -07004001 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
4002 cm->show_frame = aom_rb_read_bit(rb);
4003 cm->error_resilient_mode = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004004#if CONFIG_REFERENCE_BUFFER
4005 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004006 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4007 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4008 int prev_frame_id = 0;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004009 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004010 prev_frame_id = cm->current_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004011 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004012 cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004013
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004014 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004015 int diff_frame_id;
4016 if (cm->current_frame_id > prev_frame_id) {
4017 diff_frame_id = cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004018 } else {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004019 diff_frame_id =
4020 (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004021 }
4022 /* Check current_frame_id for conformance */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004023 if (prev_frame_id == cm->current_frame_id ||
4024 diff_frame_id >= (1 << (frame_id_length - 1))) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004025 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4026 "Invalid value of current_frame_id");
4027 }
4028 }
4029 /* Check if some frames need to be marked as not valid for referencing */
4030 for (i = 0; i < REF_FRAMES; i++) {
4031 if (cm->frame_type == KEY_FRAME) {
4032 cm->valid_for_referencing[i] = 0;
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004033 } else if (cm->current_frame_id - (1 << diff_len) > 0) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004034 if (cm->ref_frame_id[i] > cm->current_frame_id ||
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004035 cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004036 cm->valid_for_referencing[i] = 0;
4037 } else {
4038 if (cm->ref_frame_id[i] > cm->current_frame_id &&
4039 cm->ref_frame_id[i] <
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004040 (1 << frame_id_length) + cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004041 cm->valid_for_referencing[i] = 0;
4042 }
4043 }
4044 }
4045#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004046 if (cm->frame_type == KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004047 if (!av1_read_sync_code(rb))
4048 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004049 "Invalid frame sync code");
4050
4051 read_bitdepth_colorspace_sampling(cm, rb);
4052 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
4053
4054 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4055 cm->frame_refs[i].idx = INVALID_IDX;
4056 cm->frame_refs[i].buf = NULL;
4057 }
4058
4059 setup_frame_size(cm, rb);
4060 if (pbi->need_resync) {
4061 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4062 pbi->need_resync = 0;
4063 }
Alex Converseeb780e72016-12-13 12:46:41 -08004064#if CONFIG_ANS && ANS_MAX_SYMBOLS
4065 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4066#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
Urvang Joshib100db72016-10-12 16:28:56 -07004067#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004068 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004069#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004070 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004071 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004072#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004073 if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004074#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004075 if (cm->error_resilient_mode) {
4076 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
4077 } else {
4078 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004079 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004080 ? RESET_FRAME_CONTEXT_ALL
4081 : RESET_FRAME_CONTEXT_CURRENT;
4082 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004083 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004084 ? RESET_FRAME_CONTEXT_CURRENT
4085 : RESET_FRAME_CONTEXT_NONE;
4086 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07004087 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004088 ? RESET_FRAME_CONTEXT_ALL
4089 : RESET_FRAME_CONTEXT_CURRENT;
4090 }
4091 }
4092
4093 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004094 if (!av1_read_sync_code(rb))
4095 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004096 "Invalid frame sync code");
4097
4098 read_bitdepth_colorspace_sampling(cm, rb);
4099
Yaowu Xuf883b422016-08-30 14:01:10 -07004100 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004101 setup_frame_size(cm, rb);
4102 if (pbi->need_resync) {
4103 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4104 pbi->need_resync = 0;
4105 }
Alex Converseeb780e72016-12-13 12:46:41 -08004106#if CONFIG_ANS && ANS_MAX_SYMBOLS
4107 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4108#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004109 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Yaowu Xuf883b422016-08-30 14:01:10 -07004110 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004111
4112#if CONFIG_EXT_REFS
4113 if (!pbi->refresh_frame_flags) {
4114 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
4115 // will not be used as a reference
4116 cm->is_reference_frame = 0;
4117 }
4118#endif // CONFIG_EXT_REFS
4119
4120 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004121 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004122 const int idx = cm->ref_frame_map[ref];
4123 RefBuffer *const ref_frame = &cm->frame_refs[i];
4124 ref_frame->idx = idx;
4125 ref_frame->buf = &frame_bufs[idx].buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07004126 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004127#if CONFIG_REFERENCE_BUFFER
4128 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004129 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4130 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4131 int delta_frame_id_minus1 = aom_rb_read_literal(rb, diff_len);
4132 int ref_frame_id =
4133 ((cm->current_frame_id - (delta_frame_id_minus1 + 1) +
4134 (1 << frame_id_length)) %
4135 (1 << frame_id_length));
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004136 /* Compare values derived from delta_frame_id_minus1 and
4137 * refresh_frame_flags. Also, check valid for referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004138 if (ref_frame_id != cm->ref_frame_id[ref] ||
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004139 cm->valid_for_referencing[ref] == 0)
4140 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4141 "Reference buffer frame ID mismatch");
4142 }
4143#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004144 }
4145
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004146#if CONFIG_FRAME_SIZE
4147 if (cm->error_resilient_mode == 0) {
4148 setup_frame_size_with_refs(cm, rb);
4149 } else {
4150 setup_frame_size(cm, rb);
4151 }
4152#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004153 setup_frame_size_with_refs(cm, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004154#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004155
Yaowu Xuf883b422016-08-30 14:01:10 -07004156 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Angie Chiang5678ad92016-11-21 09:38:40 -08004157 cm->interp_filter = read_frame_interp_filter(rb);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004158#if CONFIG_TEMPMV_SIGNALING
4159 if (!cm->error_resilient_mode) {
4160 cm->use_prev_frame_mvs = aom_rb_read_bit(rb);
4161 }
4162#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004163 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4164 RefBuffer *const ref_buf = &cm->frame_refs[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07004165#if CONFIG_AOM_HIGHBITDEPTH
4166 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004167 &ref_buf->sf, ref_buf->buf->y_crop_width,
4168 ref_buf->buf->y_crop_height, cm->width, cm->height,
4169 cm->use_highbitdepth);
4170#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004171 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004172 &ref_buf->sf, ref_buf->buf->y_crop_width,
4173 ref_buf->buf->y_crop_height, cm->width, cm->height);
4174#endif
4175 }
4176 }
4177 }
Fangwen Fu8d164de2016-12-14 13:40:54 -08004178#if CONFIG_TEMPMV_SIGNALING
4179 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
4180#endif
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004181
4182#if CONFIG_REFERENCE_BUFFER
4183 if (pbi->seq_params.frame_id_numbers_present_flag) {
4184 /* If bitmask is set, update reference frame id values and
4185 mark frames as valid for reference */
4186 int refresh_frame_flags =
4187 cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
4188 for (i = 0; i < REF_FRAMES; i++) {
4189 if ((refresh_frame_flags >> i) & 1) {
4190 cm->ref_frame_id[i] = cm->current_frame_id;
4191 cm->valid_for_referencing[i] = 1;
4192 }
4193 }
4194 }
4195#endif
4196
Yaowu Xuf883b422016-08-30 14:01:10 -07004197#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004198 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
4199#endif
4200 get_frame_new_buffer(cm)->color_space = cm->color_space;
4201 get_frame_new_buffer(cm)->color_range = cm->color_range;
4202 get_frame_new_buffer(cm)->render_width = cm->render_width;
4203 get_frame_new_buffer(cm)->render_height = cm->render_height;
4204
4205 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004206 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004207 "Keyframe / intra-only frame required to reset decoder"
4208 " state");
4209 }
4210
4211 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004212 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004213 ? REFRESH_FRAME_CONTEXT_FORWARD
4214 : REFRESH_FRAME_CONTEXT_BACKWARD;
4215 } else {
4216 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
4217 }
4218
Yaowu Xuf883b422016-08-30 14:01:10 -07004219 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07004220 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07004221 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004222
4223 // Generate next_ref_frame_map.
4224 lock_buffer_pool(pool);
4225 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
4226 if (mask & 1) {
4227 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
4228 ++frame_bufs[cm->new_fb_idx].ref_count;
4229 } else {
4230 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4231 }
4232 // Current thread holds the reference frame.
4233 if (cm->ref_frame_map[ref_index] >= 0)
4234 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4235 ++ref_index;
4236 }
4237
4238 for (; ref_index < REF_FRAMES; ++ref_index) {
4239 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4240
4241 // Current thread holds the reference frame.
4242 if (cm->ref_frame_map[ref_index] >= 0)
4243 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4244 }
4245 unlock_buffer_pool(pool);
4246 pbi->hold_ref_buf = 1;
4247
4248 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07004249 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004250
4251#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07004252 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004253#else
4254 set_sb_size(cm, BLOCK_64X64);
4255#endif // CONFIG_EXT_PARTITION
4256
4257 setup_loopfilter(cm, rb);
Jean-Marc Valin01435132017-02-18 14:12:53 -05004258#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01004259 setup_cdef(cm, rb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004260#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004261#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004262 decode_restoration_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004263#endif // CONFIG_LOOP_RESTORATION
4264 setup_quantization(cm, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004265#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004266 xd->bd = (int)cm->bit_depth;
4267#endif
4268
hui su0d103572017-03-01 17:58:01 -08004269#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuf883b422016-08-30 14:01:10 -07004270 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004271 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4272 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4273 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4274 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
4275 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4276 }
hui su0d103572017-03-01 17:58:01 -08004277#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004278
4279 setup_segmentation(cm, rb);
4280
Arild Fuldseth07441162016-08-15 15:07:52 +02004281#if CONFIG_DELTA_Q
4282 {
4283 struct segmentation *const seg = &cm->seg;
4284 int segment_quantizer_active = 0;
4285 for (i = 0; i < MAX_SEGMENTS; i++) {
4286 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
4287 segment_quantizer_active = 1;
4288 }
4289 }
4290
Thomas Daviesf6936102016-09-05 16:51:31 +01004291 cm->delta_q_res = 1;
Arild Fuldseth07441162016-08-15 15:07:52 +02004292 if (segment_quantizer_active == 0) {
4293 cm->delta_q_present_flag = aom_rb_read_bit(rb);
4294 } else {
4295 cm->delta_q_present_flag = 0;
4296 }
4297 if (cm->delta_q_present_flag) {
4298 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01004299 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Arild Fuldseth07441162016-08-15 15:07:52 +02004300 }
4301 }
4302#endif
4303
Urvang Joshi454280d2016-10-14 16:51:44 -07004304 for (i = 0; i < MAX_SEGMENTS; ++i) {
4305 const int qindex = cm->seg.enabled
4306 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
4307 : cm->base_qindex;
4308 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
4309 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4310 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004311 }
4312
4313 setup_segmentation_dequant(cm);
Yue Cheneeacc4c2017-01-17 17:29:17 -08004314 cm->tx_mode = read_tx_mode(cm, xd, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004315 cm->reference_mode = read_frame_reference_mode(cm, rb);
4316
Sarah Parkere68a3e42017-02-16 14:03:24 -08004317#if CONFIG_EXT_TX
4318 cm->reduced_tx_set_used = aom_rb_read_bit(rb);
4319#endif // CONFIG_EXT_TX
4320
Yaowu Xuc27fc142016-08-22 16:08:15 -07004321 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004322 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004323
4324 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07004325 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004326 "Invalid header size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004327 return sz;
4328}
4329
4330#if CONFIG_EXT_TX
Thomas9ac55082016-09-23 18:04:17 +01004331#if !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuf883b422016-08-30 14:01:10 -07004332static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004333 int i, j, k;
4334 int s;
4335 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004336 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004337 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4338 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004339 for (j = 0; j < num_ext_tx_set[ext_tx_set_type_inter[s]] - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004340 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004341 }
4342 }
4343 }
4344
4345 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004346 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004347 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4348 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
4349 for (j = 0; j < INTRA_MODES; ++j)
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004350 for (k = 0; k < num_ext_tx_set[ext_tx_set_type_intra[s]] - 1; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004351 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
4352 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004353 }
4354 }
4355 }
4356}
Thomas9ac55082016-09-23 18:04:17 +01004357#endif // !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004358#else
4359
Yaowu Xuc27fc142016-08-22 16:08:15 -07004360#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004361#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07004362static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004363 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -07004364 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004365 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
Jingning Hanfeb517c2016-12-21 16:02:07 -08004366 for (j = TX_8X8; j < TX_SIZES; ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004367 av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004368 }
4369 }
4370 }
4371}
4372#endif // CONFIG_SUPERTX
4373
4374#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +00004375static void read_global_motion_params(WarpedMotionParams *params,
Yaowu Xuf883b422016-08-30 14:01:10 -07004376 aom_prob *probs, aom_reader *r) {
David Barkercf3d0b02016-11-10 10:14:49 +00004377 TransformationType type =
Michael Bebenita6048d052016-08-25 14:40:54 -07004378 aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004379 set_default_gmparams(params);
David Barkercf3d0b02016-11-10 10:14:49 +00004380 params->wmtype = type;
4381 switch (type) {
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004382 case HOMOGRAPHY:
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004383 case HORTRAPEZOID:
4384 case VERTRAPEZOID:
4385 if (type != HORTRAPEZOID)
4386 params->wmmat[6] =
4387 aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
4388 GM_ROW3HOMO_DECODE_FACTOR;
4389 if (type != VERTRAPEZOID)
4390 params->wmmat[7] =
4391 aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
4392 GM_ROW3HOMO_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004393 case AFFINE:
4394 case ROTZOOM:
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004395 params->wmmat[2] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004396 GM_ALPHA_DECODE_FACTOR +
David Barkercf3d0b02016-11-10 10:14:49 +00004397 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004398 if (type != VERTRAPEZOID)
4399 params->wmmat[3] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004400 GM_ALPHA_DECODE_FACTOR;
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004401 if (type >= AFFINE) {
4402 if (type != HORTRAPEZOID)
4403 params->wmmat[4] =
4404 aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4405 GM_ALPHA_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004406 params->wmmat[5] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4407 GM_ALPHA_DECODE_FACTOR +
4408 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004409 } else {
David Barkercf3d0b02016-11-10 10:14:49 +00004410 params->wmmat[4] = -params->wmmat[3];
4411 params->wmmat[5] = params->wmmat[2];
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004412 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004413 // fallthrough intended
David Barkercf3d0b02016-11-10 10:14:49 +00004414 case TRANSLATION:
4415 params->wmmat[0] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4416 GM_TRANS_DECODE_FACTOR;
4417 params->wmmat[1] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4418 GM_TRANS_DECODE_FACTOR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004419 break;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004420 case IDENTITY: break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004421 default: assert(0);
4422 }
4423}
4424
Yaowu Xuf883b422016-08-30 14:01:10 -07004425static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004426 int frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004427 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
4428 read_global_motion_params(&cm->global_motion[frame],
4429 cm->fc->global_motion_types_prob, r);
Sarah Parkere5299862016-08-16 14:57:37 -07004430 /*
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004431 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4432 frame, cm->current_video_frame, cm->show_frame,
David Barkercf3d0b02016-11-10 10:14:49 +00004433 cm->global_motion[frame].wmmat[0],
4434 cm->global_motion[frame].wmmat[1],
4435 cm->global_motion[frame].wmmat[2],
4436 cm->global_motion[frame].wmmat[3]);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004437 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004438 }
4439}
4440#endif // CONFIG_GLOBAL_MOTION
4441
Yaowu Xuf883b422016-08-30 14:01:10 -07004442static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004443 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004444 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004445#if CONFIG_SUPERTX
4446 MACROBLOCKD *const xd = &pbi->mb;
4447#endif
4448 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07004449 aom_reader r;
Yaowu Xu8af861b2016-11-01 12:12:11 -07004450 int k, i;
Yaowu Xud0af64f2016-11-17 12:50:47 -08004451#if !CONFIG_EC_ADAPT || CONFIG_EXT_INTRA
Yaowu Xu8af861b2016-11-01 12:12:11 -07004452 int j;
4453#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004454
Alex Converse2cdf0d82016-12-13 13:53:09 -08004455#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08004456 r.window_size = 1 << cm->ans_window_size_log2;
Alex Converse2cdf0d82016-12-13 13:53:09 -08004457#endif
Alex Converse346440b2017-01-03 13:47:37 -08004458 if (aom_reader_init(&r, data, partition_size, pbi->decrypt_cb,
4459 pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07004460 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004461 "Failed to allocate bool decoder 0");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004462
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004463#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004464 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4465 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4466 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4467 av1_alloc_restoration_buffers(cm);
4468 decode_restoration(cm, &r);
4469 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004470#endif
4471
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004472#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -07004473 if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004474#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004475
Yushin Cho77bba8d2016-11-04 16:36:56 -07004476#if !CONFIG_PVQ
Alex Conversea9598cd2017-02-03 14:18:05 -08004477#if !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004478 read_coef_probs(fc, cm->tx_mode, &r);
Thomas Daviesfc1598a2017-01-13 17:07:25 +00004479#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004480
4481#if CONFIG_VAR_TX
4482 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004483 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
Yushin Cho77bba8d2016-11-04 16:36:56 -07004484#endif // CONFIG_VAR_TX
4485#endif // !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004486 for (k = 0; k < SKIP_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004487 av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004488
Thomas Daviesd6ee8a82017-03-02 14:42:50 +00004489#if CONFIG_DELTA_Q && !CONFIG_EC_ADAPT
Thomas Davies665cd702017-03-02 10:20:30 +00004490 for (k = 0; k < DELTA_Q_PROBS; ++k)
Thomas Daviesf6936102016-09-05 16:51:31 +01004491 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
4492#endif
4493
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004494#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004495 if (cm->seg.enabled && cm->seg.update_map) {
4496 if (cm->seg.temporal_update) {
4497 for (k = 0; k < PREDICTION_PROBS; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004498 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004499 }
4500 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004501 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004502 }
4503
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004504 for (j = 0; j < INTRA_MODES; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004505 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004506 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004507 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004508
4509#if CONFIG_EXT_PARTITION_TYPES
Alex Converse4e18d402017-03-14 15:36:38 -07004510 for (j = 0; j < PARTITION_PLOFFSET; ++j)
4511 for (i = 0; i < PARTITION_TYPES - 1; ++i)
4512 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
4513 for (; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004514 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004515 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004516#else
Alex Converse55c6bde2017-01-12 15:55:31 -08004517 for (j = 0; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004518 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004519 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004520#endif // CONFIG_EXT_PARTITION_TYPES
hui su9aa97492017-01-26 16:46:01 -08004521
Alex Converse55c6bde2017-01-12 15:55:31 -08004522#if CONFIG_UNPOISON_PARTITION_CTX
4523 for (; j < PARTITION_CONTEXTS_PRIMARY + PARTITION_BLOCK_SIZES; ++j)
4524 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_VERT], ACCT_STR);
4525 for (; j < PARTITION_CONTEXTS_PRIMARY + 2 * PARTITION_BLOCK_SIZES; ++j)
4526 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_HORZ], ACCT_STR);
4527#endif // CONFIG_UNPOISON_PARTITION_CTX
hui su9aa97492017-01-26 16:46:01 -08004528#endif // !CONFIG_EC_ADAPT
4529
4530#if CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004531 for (i = 0; i < INTRA_FILTERS + 1; ++i)
4532 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004533 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
hui su9aa97492017-01-26 16:46:01 -08004534#endif // CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004535
4536 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004537 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Nathan E. Egge10ba2be2016-11-16 09:44:26 -05004538#if CONFIG_EC_MULTISYMBOL
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00004539 av1_copy(cm->fc->kf_y_cdf, av1_kf_y_mode_cdf);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004540#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004541#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004542 for (k = 0; k < INTRA_MODES; k++)
Thomas Davies6519beb2016-10-19 14:46:07 +01004543 for (j = 0; j < INTRA_MODES; j++)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004544 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004545 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004546#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004547 } else {
4548#if !CONFIG_REF_MV
4549 nmv_context *const nmvc = &fc->nmvc;
4550#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004551 read_inter_mode_probs(fc, &r);
4552
4553#if CONFIG_EXT_INTER
4554 read_inter_compound_mode_probs(fc, &r);
4555 if (cm->reference_mode != COMPOUND_REFERENCE) {
4556 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4557 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004558 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004559 }
4560 }
4561 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4562 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004563 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004564 }
4565 for (i = 0; i < BLOCK_SIZES; i++) {
4566 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004567 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004568 }
4569 }
4570 }
4571 if (cm->reference_mode != SINGLE_REFERENCE) {
4572 for (i = 0; i < BLOCK_SIZES; i++) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08004573 for (j = 0; j < COMPOUND_TYPES - 1; j++) {
4574 av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004575 }
4576 }
4577 }
4578#endif // CONFIG_EXT_INTER
4579
Yue Chencb60b182016-10-13 15:18:22 -07004580#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004581 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
Yue Chencb60b182016-10-13 15:18:22 -07004582 for (j = 0; j < MOTION_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004583 av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004584 }
Yue Chencb60b182016-10-13 15:18:22 -07004585#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004586
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004587#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004588 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004589#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004590
4591 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004592 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004593
4594 if (cm->reference_mode != SINGLE_REFERENCE)
4595 setup_compound_reference_mode(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004596 read_frame_reference_mode_probs(cm, &r);
4597
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004598#if !CONFIG_EC_ADAPT
Nathan E. Egge5710c722016-09-08 10:01:16 -04004599 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004600 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004601 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
Nathan E. Egge5710c722016-09-08 10:01:16 -04004602 }
Thomas9ac55082016-09-23 18:04:17 +01004603#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004604
4605#if CONFIG_REF_MV
4606 for (i = 0; i < NMV_CONTEXTS; ++i)
4607 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
4608#else
4609 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
4610#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004611#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004612 read_ext_tx_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004613#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004614#if CONFIG_SUPERTX
4615 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
4616#endif
4617#if CONFIG_GLOBAL_MOTION
4618 read_global_motion(cm, &r);
Thomas Davies6519beb2016-10-19 14:46:07 +01004619#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004620 }
Thomas Davies028b57f2017-02-22 16:42:11 +00004621#if CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Thomas Davies87aeeb82017-02-17 00:19:40 +00004622#if CONFIG_NEW_TOKENSET
4623 av1_coef_head_cdfs(fc);
4624#endif
4625 /* Make tail distribution from head */
Thomas Davies6519beb2016-10-19 14:46:07 +01004626 av1_coef_pareto_cdfs(fc);
David Barker599dfd02016-11-10 13:20:12 +00004627#if CONFIG_REF_MV
4628 for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
4629#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004630 av1_set_mv_cdfs(&fc->nmvc);
David Barker599dfd02016-11-10 13:20:12 +00004631#endif
Thomas Davies6519beb2016-10-19 14:46:07 +01004632 av1_set_mode_cdfs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00004633#endif // CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004634
Yaowu Xuf883b422016-08-30 14:01:10 -07004635 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004636}
4637
4638#ifdef NDEBUG
4639#define debug_check_frame_counts(cm) (void)0
4640#else // !NDEBUG
4641// Counts should only be incremented when frame_parallel_decoding_mode and
4642// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07004643static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004644 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07004645 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004646 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
4647 cm->error_resilient_mode);
4648 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
4649 sizeof(cm->counts.y_mode)));
4650 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
4651 sizeof(cm->counts.uv_mode)));
4652 assert(!memcmp(cm->counts.partition, zero_counts.partition,
4653 sizeof(cm->counts.partition)));
4654 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
4655 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
4656 sizeof(cm->counts.eob_branch)));
Thomas Daviesab780672017-02-01 12:07:29 +00004657#if CONFIG_EC_MULTISYMBOL
4658 assert(!memcmp(cm->counts.blockz_count, zero_counts.blockz_count,
4659 sizeof(cm->counts.blockz_count)));
4660#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004661 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
4662 sizeof(cm->counts.switchable_interp)));
4663 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
4664 sizeof(cm->counts.inter_mode)));
4665#if CONFIG_EXT_INTER
4666 assert(!memcmp(cm->counts.inter_compound_mode,
4667 zero_counts.inter_compound_mode,
4668 sizeof(cm->counts.inter_compound_mode)));
4669 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
4670 sizeof(cm->counts.interintra)));
4671 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
4672 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08004673 assert(!memcmp(cm->counts.compound_interinter,
4674 zero_counts.compound_interinter,
4675 sizeof(cm->counts.compound_interinter)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004676#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07004677#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
4678 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
4679 sizeof(cm->counts.motion_mode)));
4680#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004681 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
4682 sizeof(cm->counts.intra_inter)));
4683 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
4684 sizeof(cm->counts.comp_inter)));
4685 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
4686 sizeof(cm->counts.single_ref)));
4687 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
4688 sizeof(cm->counts.comp_ref)));
4689#if CONFIG_EXT_REFS
4690 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
4691 sizeof(cm->counts.comp_bwdref)));
4692#endif // CONFIG_EXT_REFS
4693 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
4694 sizeof(cm->counts.tx_size)));
4695 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
4696#if CONFIG_REF_MV
4697 assert(
4698 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
4699 assert(
4700 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
4701#else
4702 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
4703#endif
4704 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
4705 sizeof(cm->counts.inter_ext_tx)));
4706 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
4707 sizeof(cm->counts.intra_ext_tx)));
4708}
4709#endif // NDEBUG
4710
Yaowu Xuf883b422016-08-30 14:01:10 -07004711static struct aom_read_bit_buffer *init_read_bit_buffer(
4712 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
4713 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004714 rb->bit_offset = 0;
4715 rb->error_handler = error_handler;
4716 rb->error_handler_data = &pbi->common;
4717 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004718 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004719 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
4720 rb->bit_buffer = clear_data;
4721 rb->bit_buffer_end = clear_data + n;
4722 } else {
4723 rb->bit_buffer = data;
4724 rb->bit_buffer_end = data_end;
4725 }
4726 return rb;
4727}
4728
4729//------------------------------------------------------------------------------
4730
Yaowu Xuf883b422016-08-30 14:01:10 -07004731int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
4732 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
4733 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
4734 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004735}
4736
Yaowu Xuf883b422016-08-30 14:01:10 -07004737void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
4738 int *height) {
4739 *width = aom_rb_read_literal(rb, 16) + 1;
4740 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004741}
4742
Yaowu Xuf883b422016-08-30 14:01:10 -07004743BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
4744 int profile = aom_rb_read_bit(rb);
4745 profile |= aom_rb_read_bit(rb) << 1;
4746 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004747 return (BITSTREAM_PROFILE)profile;
4748}
4749
Thomas Davies028b57f2017-02-22 16:42:11 +00004750#if CONFIG_EC_ADAPT
4751static void make_update_tile_list_dec(AV1Decoder *pbi, const int tile_rows,
4752 const int tile_cols,
4753 FRAME_CONTEXT *ec_ctxs[]) {
4754 int i;
4755 for (i = 0; i < tile_rows * tile_cols; ++i)
4756 ec_ctxs[i] = &pbi->tile_data[i].tctx;
4757}
4758#endif
4759
Yaowu Xuf883b422016-08-30 14:01:10 -07004760void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
4761 const uint8_t *data_end, const uint8_t **p_data_end) {
4762 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004763 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07004764 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004765 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004766 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004767 size_t first_partition_size;
4768 YV12_BUFFER_CONFIG *new_fb;
4769
4770#if CONFIG_BITSTREAM_DEBUG
4771 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
4772#endif
4773
4774 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004775 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Thomas Davies72712e62016-11-09 12:17:51 +00004776#if CONFIG_TILE_GROUPS
4777 pbi->first_partition_size = first_partition_size;
4778 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
4779#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004780 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004781 xd->cur_buf = new_fb;
4782#if CONFIG_GLOBAL_MOTION
4783 xd->global_motion = cm->global_motion;
4784#endif // CONFIG_GLOBAL_MOTION
4785
4786 if (!first_partition_size) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004787 // showing a frame directly
4788 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004789 return;
4790 }
4791
Yaowu Xuf883b422016-08-30 14:01:10 -07004792 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004793 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07004794 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004795 "Truncated packet or corrupt header length");
4796
Jingning Han24e0a182016-11-20 22:34:12 -08004797#if CONFIG_REF_MV
Dengca8d24d2016-10-17 14:06:35 +08004798 cm->setup_mi(cm);
4799#endif
4800
Fangwen Fu8d164de2016-12-14 13:40:54 -08004801#if CONFIG_TEMPMV_SIGNALING
4802 if (cm->use_prev_frame_mvs) {
4803 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4804 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4805 assert(!cm->error_resilient_mode &&
4806 cm->width == last_fb_ref_buf->buf->y_width &&
4807 cm->height == last_fb_ref_buf->buf->y_height &&
4808 !cm->prev_frame->intra_only);
4809 }
4810#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004811 cm->use_prev_frame_mvs =
4812 !cm->error_resilient_mode && cm->width == cm->last_width &&
4813 cm->height == cm->last_height && !cm->last_intra_only &&
4814 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004815#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004816#if CONFIG_EXT_REFS
4817 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
4818 // show_exisiting_frame=1, nor can it take a frame not used as
4819 // a reference, it is probable that by the time it is being
4820 // referred to, the frame buffer it originally points to may
4821 // already get expired and have been reassigned to the current
4822 // newly coded frame. Hence, we need to check whether this is
4823 // the case, and if yes, we have 2 choices:
4824 // (1) Simply disable the use of previous frame mvs; or
4825 // (2) Have cm->prev_frame point to one reference frame buffer,
4826 // e.g. LAST_FRAME.
4827 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
4828 // Reassign the LAST_FRAME buffer to cm->prev_frame.
4829 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4830 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4831 }
4832#endif // CONFIG_EXT_REFS
4833
Yaowu Xuf883b422016-08-30 14:01:10 -07004834 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004835
4836 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
4837 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07004838 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004839 "Uninitialized entropy context.");
4840
Yaowu Xuf883b422016-08-30 14:01:10 -07004841 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004842
4843 xd->corrupted = 0;
4844 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
4845 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07004846 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004847 "Decode failed. Frame data header is corrupted.");
4848
4849 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004850 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004851 }
4852
4853 // If encoded in frame parallel mode, frame context is ready after decoding
4854 // the frame header.
4855 if (cm->frame_parallel_decode &&
4856 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004857 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004858 FrameWorkerData *const frame_worker_data = worker->data1;
4859 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
4860 context_updated = 1;
4861 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4862 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004863 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004864 pbi->cur_buf->row = -1;
4865 pbi->cur_buf->col = -1;
4866 frame_worker_data->frame_context_ready = 1;
4867 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07004868 av1_frameworker_signal_stats(worker);
4869 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004870 }
4871
hui su0d103572017-03-01 17:58:01 -08004872#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07004873 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004874 cm->coef_probs_update_idx = 0;
hui su0d103572017-03-01 17:58:01 -08004875#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004876
4877 if (pbi->max_threads > 1
4878#if CONFIG_EXT_TILE
4879 && pbi->dec_tile_col < 0 // Decoding all columns
4880#endif // CONFIG_EXT_TILE
4881 && cm->tile_cols > 1) {
4882 // Multi-threaded tile decoder
4883 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
4884 if (!xd->corrupted) {
4885 if (!cm->skip_loop_filter) {
4886 // If multiple threads are used to decode tiles, then we use those
4887 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07004888 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
4889 0, 0, pbi->tile_workers, pbi->num_tile_workers,
4890 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004891 }
4892 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004893 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004894 "Decode failed. Frame data is corrupted.");
4895 }
4896 } else {
4897 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
4898 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004899
Jean-Marc Valin01435132017-02-18 14:12:53 -05004900#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01004901 if ((cm->dering_level || cm->clpf_strength_u || cm->clpf_strength_v) &&
4902 !cm->skip_loop_filter) {
4903 av1_cdef_frame(&pbi->cur_buf->buf, cm, &pbi->mb, cm->dering_level,
4904 cm->clpf_strength_u, cm->clpf_strength_v);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004905 }
Debargha Mukherjee00c54332017-03-03 15:44:17 -08004906#endif // CONFIG_CDEF
4907
4908#if CONFIG_LOOP_RESTORATION
4909 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4910 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4911 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4912 av1_loop_restoration_frame(new_fb, cm, cm->rst_info, 7, 0, NULL);
4913 }
4914#endif // CONFIG_LOOP_RESTORATION
Thomas Daedef56859f2016-04-19 16:57:24 -07004915
Yaowu Xuc27fc142016-08-22 16:08:15 -07004916 if (!xd->corrupted) {
4917 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Thomas Davies028b57f2017-02-22 16:42:11 +00004918#if CONFIG_EC_ADAPT
4919 FRAME_CONTEXT *tile_ctxs[MAX_TILE_ROWS * MAX_TILE_COLS];
4920 make_update_tile_list_dec(pbi, cm->tile_rows, cm->tile_cols, tile_ctxs);
4921#endif
4922
hui su0d103572017-03-01 17:58:01 -08004923#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004924 cm->partial_prob_update = 0;
hui su0d103572017-03-01 17:58:01 -08004925#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07004926 av1_adapt_coef_probs(cm);
4927 av1_adapt_intra_frame_probs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00004928#if CONFIG_EC_ADAPT
4929 av1_average_tile_coef_cdfs(pbi->common.fc, tile_ctxs,
4930 cm->tile_rows * cm->tile_cols);
4931 av1_average_tile_intra_cdfs(pbi->common.fc, tile_ctxs,
4932 cm->tile_rows * cm->tile_cols);
4933#endif
hui suff0da2b2017-03-07 15:51:37 -08004934#if CONFIG_ADAPT_SCAN
4935 av1_adapt_scan_order(cm);
4936#endif // CONFIG_ADAPT_SCAN
Yaowu Xuc27fc142016-08-22 16:08:15 -07004937
4938 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004939 av1_adapt_inter_frame_probs(cm);
4940 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Thomas Davies028b57f2017-02-22 16:42:11 +00004941#if CONFIG_EC_ADAPT
4942 av1_average_tile_inter_cdfs(&pbi->common, pbi->common.fc, tile_ctxs,
4943 cm->tile_rows * cm->tile_cols);
4944 av1_average_tile_mv_cdfs(pbi->common.fc, tile_ctxs,
4945 cm->tile_rows * cm->tile_cols);
4946#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004947 }
4948 } else {
4949 debug_check_frame_counts(cm);
4950 }
4951 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004952 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004953 "Decode failed. Frame data is corrupted.");
4954 }
4955
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05004956#if CONFIG_INSPECTION
4957 if (pbi->inspect_cb != NULL) {
4958 (*pbi->inspect_cb)(pbi, pbi->inspect_ctx);
4959 }
4960#endif
4961
Yaowu Xuc27fc142016-08-22 16:08:15 -07004962 // Non frame parallel update frame context here.
4963 if (!cm->error_resilient_mode && !context_updated)
4964 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4965}