blob: fc7dcdeaeecad5b8432ccd08ce151645a31e9356 [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
Michael Bebenita6048d052016-08-25 14:40:54 -07001943 p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs,
1944 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001945#else
Nathan E. Egge9d9eb6c2016-11-16 09:44:26 -05001946#if CONFIG_EC_MULTISYMBOL
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001947 p = (PARTITION_TYPE)aom_read_symbol(r, ec_ctx->partition_cdf[ctx],
Yaowu Xuf2581a32016-10-20 13:05:47 -07001948 PARTITION_TYPES, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001949#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001950 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001951#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001952#endif // CONFIG_EXT_PARTITION_TYPES
1953 else if (!has_rows && has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001954 p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001955 else if (has_rows && !has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001956 p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001957 else
1958 p = PARTITION_SPLIT;
1959
1960 if (counts) ++counts->partition[ctx][p];
1961
1962 return p;
1963}
1964
1965#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001966static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
1967 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001968 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
1969 return 1;
1970 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001971 const int ctx = av1_get_skip_context(xd);
Michael Bebenita6048d052016-08-25 14:40:54 -07001972 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001973 FRAME_COUNTS *counts = xd->counts;
1974 if (counts) ++counts->skip[ctx][skip];
1975 return skip;
1976 }
1977}
1978#endif // CONFIG_SUPERTX
1979
1980// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -07001981static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001982#if CONFIG_SUPERTX
1983 int supertx_enabled,
1984#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001985 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001986 BLOCK_SIZE bsize, int n4x4_l2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001987 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001988 const int n8x8_l2 = n4x4_l2 - 1;
Jingning Hanff17e162016-12-07 17:58:18 -08001989 const int num_8x8_wh = mi_size_wide[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001990 const int hbs = num_8x8_wh >> 1;
Jingning Han41bb3392016-12-14 10:46:48 -08001991#if CONFIG_CB4X4
1992 const int unify_bsize = 1;
1993#else
1994 const int unify_bsize = 0;
1995#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001996 PARTITION_TYPE partition;
1997 BLOCK_SIZE subsize;
1998#if CONFIG_EXT_PARTITION_TYPES
1999 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
2000#endif
2001 const int has_rows = (mi_row + hbs) < cm->mi_rows;
2002 const int has_cols = (mi_col + hbs) < cm->mi_cols;
2003#if CONFIG_SUPERTX
2004 const int read_token = !supertx_enabled;
2005 int skip = 0;
Jingning Han2511c662016-12-22 11:57:34 -08002006 TX_SIZE supertx_size = max_txsize_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002007 const TileInfo *const tile = &xd->tile;
2008 int txfm = DCT_DCT;
2009#endif // CONFIG_SUPERTX
2010
2011 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
2012
Jingning Han6dbacf22016-12-14 17:28:35 -08002013 partition = (n4x4_l2 == 0) ? PARTITION_NONE
2014 : read_partition(cm, xd, mi_row, mi_col, r,
2015 has_rows, has_cols, bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002016 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
Yushin Cho77bba8d2016-11-04 16:36:56 -07002017
2018#if CONFIG_PVQ
2019 assert(partition < PARTITION_TYPES);
2020 assert(subsize < BLOCK_SIZES);
2021#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002022#if CONFIG_SUPERTX
2023 if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
2024 bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
2025 const int supertx_context = partition_supertx_context_lookup[partition];
Michael Bebenita6048d052016-08-25 14:40:54 -07002026 supertx_enabled = aom_read(
2027 r, cm->fc->supertx_prob[supertx_context][supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002028 if (xd->counts)
2029 xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
2030#if CONFIG_VAR_TX
2031 if (supertx_enabled) xd->supertx_size = supertx_size;
2032#endif
2033 }
2034#endif // CONFIG_SUPERTX
Jingning Han41bb3392016-12-14 10:46:48 -08002035 if (!hbs && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002036 // calculate bmode block dimensions (log 2)
2037 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
2038 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
2039 decode_block(pbi, xd,
2040#if CONFIG_SUPERTX
2041 supertx_enabled,
2042#endif // CONFIG_SUPERTX
2043 mi_row, mi_col, r,
2044#if CONFIG_EXT_PARTITION_TYPES
2045 partition,
2046#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002047 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002048 } else {
2049 switch (partition) {
2050 case PARTITION_NONE:
2051 decode_block(pbi, xd,
2052#if CONFIG_SUPERTX
2053 supertx_enabled,
2054#endif // CONFIG_SUPERTX
2055 mi_row, mi_col, r,
2056#if CONFIG_EXT_PARTITION_TYPES
2057 partition,
2058#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002059 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002060 break;
2061 case PARTITION_HORZ:
2062 decode_block(pbi, xd,
2063#if CONFIG_SUPERTX
2064 supertx_enabled,
2065#endif // CONFIG_SUPERTX
2066 mi_row, mi_col, r,
2067#if CONFIG_EXT_PARTITION_TYPES
2068 partition,
2069#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002070 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002071 if (has_rows)
2072 decode_block(pbi, xd,
2073#if CONFIG_SUPERTX
2074 supertx_enabled,
2075#endif // CONFIG_SUPERTX
2076 mi_row + hbs, mi_col, r,
2077#if CONFIG_EXT_PARTITION_TYPES
2078 partition,
2079#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002080 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002081 break;
2082 case PARTITION_VERT:
2083 decode_block(pbi, xd,
2084#if CONFIG_SUPERTX
2085 supertx_enabled,
2086#endif // CONFIG_SUPERTX
2087 mi_row, mi_col, r,
2088#if CONFIG_EXT_PARTITION_TYPES
2089 partition,
2090#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002091 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002092 if (has_cols)
2093 decode_block(pbi, xd,
2094#if CONFIG_SUPERTX
2095 supertx_enabled,
2096#endif // CONFIG_SUPERTX
2097 mi_row, mi_col + hbs, r,
2098#if CONFIG_EXT_PARTITION_TYPES
2099 partition,
2100#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002101 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002102 break;
2103 case PARTITION_SPLIT:
2104 decode_partition(pbi, xd,
2105#if CONFIG_SUPERTX
2106 supertx_enabled,
2107#endif // CONFIG_SUPERTX
2108 mi_row, mi_col, r, subsize, n8x8_l2);
2109 decode_partition(pbi, xd,
2110#if CONFIG_SUPERTX
2111 supertx_enabled,
2112#endif // CONFIG_SUPERTX
2113 mi_row, mi_col + hbs, r, subsize, n8x8_l2);
2114 decode_partition(pbi, xd,
2115#if CONFIG_SUPERTX
2116 supertx_enabled,
2117#endif // CONFIG_SUPERTX
2118 mi_row + hbs, mi_col, 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 + hbs, r, subsize, n8x8_l2);
2124 break;
2125#if CONFIG_EXT_PARTITION_TYPES
2126 case PARTITION_HORZ_A:
2127 decode_block(pbi, xd,
2128#if CONFIG_SUPERTX
2129 supertx_enabled,
2130#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002131 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002132 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 + hbs, 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 + hbs, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002142 break;
2143 case PARTITION_HORZ_B:
2144 decode_block(pbi, xd,
2145#if CONFIG_SUPERTX
2146 supertx_enabled,
2147#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002148 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002149 decode_block(pbi, xd,
2150#if CONFIG_SUPERTX
2151 supertx_enabled,
2152#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002153 mi_row + hbs, mi_col, r, partition, bsize2);
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 + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002159 break;
2160 case PARTITION_VERT_A:
2161 decode_block(pbi, xd,
2162#if CONFIG_SUPERTX
2163 supertx_enabled,
2164#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002165 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002166 decode_block(pbi, xd,
2167#if CONFIG_SUPERTX
2168 supertx_enabled,
2169#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002170 mi_row + hbs, 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, mi_col + hbs, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002176 break;
2177 case PARTITION_VERT_B:
2178 decode_block(pbi, xd,
2179#if CONFIG_SUPERTX
2180 supertx_enabled,
2181#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002182 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002183 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 + hbs, r, partition, bsize2);
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 + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002193 break;
2194#endif
2195 default: assert(0 && "Invalid partition type");
2196 }
2197 }
2198
2199#if CONFIG_SUPERTX
2200 if (supertx_enabled && read_token) {
2201 uint8_t *dst_buf[3];
2202 int dst_stride[3], i;
2203 int offset = mi_row * cm->mi_stride + mi_col;
2204
2205 set_segment_id_supertx(cm, mi_row, mi_col, bsize);
2206
David Barker3aec8d62017-01-31 14:55:32 +00002207#if CONFIG_DELTA_Q
2208 if (cm->delta_q_present_flag) {
2209 for (i = 0; i < MAX_SEGMENTS; i++) {
2210 xd->plane[0].seg_dequant[i][0] =
2211 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
2212 xd->plane[0].seg_dequant[i][1] =
2213 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
2214 xd->plane[1].seg_dequant[i][0] =
2215 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2216 xd->plane[1].seg_dequant[i][1] =
2217 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2218 xd->plane[2].seg_dequant[i][0] =
2219 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2220 xd->plane[2].seg_dequant[i][1] =
2221 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2222 }
2223 }
2224#endif
2225
Yaowu Xuc27fc142016-08-22 16:08:15 -07002226 xd->mi = cm->mi_grid_visible + offset;
2227 xd->mi[0] = cm->mi + offset;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002228#if CONFIG_DEPENDENT_HORZTILES
2229 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2230 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols,
2231 cm->dependent_horz_tiles);
2232#else
Jingning Han5b7706a2016-12-21 09:55:10 -08002233 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2234 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002235#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002236 set_skip_context(xd, mi_row, mi_col);
2237 skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
2238 if (skip) {
2239 reset_skip_context(xd, bsize);
2240 } else {
2241#if CONFIG_EXT_TX
Sarah Parkere68a3e42017-02-16 14:03:24 -08002242 if (get_ext_tx_types(supertx_size, bsize, 1, cm->reduced_tx_set_used) >
2243 1) {
2244 const int eset =
2245 get_ext_tx_set(supertx_size, bsize, 1, cm->reduced_tx_set_used);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002246 if (eset > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002247 txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
Michael Bebenita6048d052016-08-25 14:40:54 -07002248 cm->fc->inter_ext_tx_prob[eset][supertx_size],
2249 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002250 if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
2251 }
2252 }
2253#else
2254 if (supertx_size < TX_32X32) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002255 txfm = aom_read_tree(r, av1_ext_tx_tree,
Jingning Han8f6eb182016-10-19 13:48:57 -07002256 cm->fc->inter_ext_tx_prob[supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002257 if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
2258 }
2259#endif // CONFIG_EXT_TX
2260 }
2261
Yaowu Xuf883b422016-08-30 14:01:10 -07002262 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002263 for (i = 0; i < MAX_MB_PLANE; i++) {
2264 dst_buf[i] = xd->plane[i].dst.buf;
2265 dst_stride[i] = xd->plane[i].dst.stride;
2266 }
2267 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
2268 bsize, dst_buf, dst_stride);
2269
2270 if (!skip) {
2271 int eobtotal = 0;
2272 MB_MODE_INFO *mbmi;
2273 set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
2274 mbmi = &xd->mi[0]->mbmi;
2275 mbmi->tx_type = txfm;
2276 assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
2277 for (i = 0; i < MAX_MB_PLANE; ++i) {
2278 const struct macroblockd_plane *const pd = &xd->plane[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002279 int row, col;
Angie Chiang7fcfee42017-02-24 15:51:03 -08002280 const TX_SIZE tx_size = get_tx_size(i, xd);
Jingning Han5b7706a2016-12-21 09:55:10 -08002281 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Jingning Han32b20282016-10-28 15:42:44 -07002282 const int stepr = tx_size_high_unit[tx_size];
2283 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han5b7706a2016-12-21 09:55:10 -08002284 const int max_blocks_wide = max_block_wide(xd, plane_bsize, i);
2285 const int max_blocks_high = max_block_high(xd, plane_bsize, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002286
2287 for (row = 0; row < max_blocks_high; row += stepr)
2288 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07002289 eobtotal += reconstruct_inter_block(
2290 cm, xd, r, mbmi->segment_id_supertx, i, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002291 }
Jingning Han24f24a52016-12-27 10:13:28 -08002292 if ((unify_bsize || !(subsize < BLOCK_8X8)) && eobtotal == 0) skip = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002293 }
2294 set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
2295 }
2296#endif // CONFIG_SUPERTX
2297
2298#if CONFIG_EXT_PARTITION_TYPES
2299 if (bsize >= BLOCK_8X8) {
2300 switch (partition) {
2301 case PARTITION_SPLIT:
2302 if (bsize > BLOCK_8X8) break;
2303 case PARTITION_NONE:
2304 case PARTITION_HORZ:
2305 case PARTITION_VERT:
2306 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
2307 break;
2308 case PARTITION_HORZ_A:
2309 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2310 update_partition_context(xd, mi_row + hbs, mi_col, subsize, subsize);
2311 break;
2312 case PARTITION_HORZ_B:
2313 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2314 update_partition_context(xd, mi_row + hbs, mi_col, bsize2, subsize);
2315 break;
2316 case PARTITION_VERT_A:
2317 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2318 update_partition_context(xd, mi_row, mi_col + hbs, subsize, subsize);
2319 break;
2320 case PARTITION_VERT_B:
2321 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2322 update_partition_context(xd, mi_row, mi_col + hbs, bsize2, subsize);
2323 break;
2324 default: assert(0 && "Invalid partition type");
2325 }
2326 }
2327#else
2328 // update partition context
2329 if (bsize >= BLOCK_8X8 &&
2330 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
Jingning Han1beb0102016-12-07 11:08:30 -08002331 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
David Barkerf8935c92016-10-26 14:54:06 +01002332#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xud71be782016-10-14 08:47:03 -07002333
Jean-Marc Valin01435132017-02-18 14:12:53 -05002334#if CONFIG_CDEF
David Barker9739f362016-11-10 09:29:32 +00002335#if CONFIG_EXT_PARTITION
2336 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128) {
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002337 if (!sb_all_skip(cm, mi_row, mi_col)) {
David Barker9739f362016-11-10 09:29:32 +00002338 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002339 aom_read_literal(r, cm->dering_bits, ACCT_STR);
2340 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.clpf_strength =
2341 aom_read_literal(r, cm->clpf_bits, ACCT_STR);
David Barker9739f362016-11-10 09:29:32 +00002342 } else {
2343 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002344 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]
2345 ->mbmi.clpf_strength = 0;
David Barker9739f362016-11-10 09:29:32 +00002346 }
2347 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64) {
2348#else
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002349 if (bsize == BLOCK_64X64) {
David Barker9739f362016-11-10 09:29:32 +00002350#endif
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002351 if (!sb_all_skip(cm, mi_row, mi_col)) {
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002352 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002353 aom_read_literal(r, cm->dering_bits, ACCT_STR);
2354 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.clpf_strength =
2355 aom_read_literal(r, cm->clpf_bits, ACCT_STR);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002356 } else {
2357 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002358 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]
2359 ->mbmi.clpf_strength = 0;
Yaowu Xud71be782016-10-14 08:47:03 -07002360 }
2361 }
Jean-Marc Valin01435132017-02-18 14:12:53 -05002362#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002363}
2364
Yaowu Xuc27fc142016-08-22 16:08:15 -07002365static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
2366 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002367 struct aom_internal_error_info *error_info,
Alex Converseeb780e72016-12-13 12:46:41 -08002368 aom_reader *r,
2369#if CONFIG_ANS && ANS_MAX_SYMBOLS
2370 int window_size,
2371#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
2372 aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002373 // Validate the calculated partition length. If the buffer
2374 // described by the partition can't be fully read, then restrict
2375 // it to the portion that can be (for EC mode) or throw an error.
2376 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002377 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002378 "Truncated packet or corrupt tile length");
2379
Alex Converse2cdf0d82016-12-13 13:53:09 -08002380#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08002381 r->window_size = window_size;
Alex Converse2cdf0d82016-12-13 13:53:09 -08002382#endif
Alex Converse346440b2017-01-03 13:47:37 -08002383 if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07002384 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002385 "Failed to allocate bool decoder %d", 1);
2386}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002387
Alex Conversea9598cd2017-02-03 14:18:05 -08002388#if !CONFIG_PVQ && !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Yaowu Xuf883b422016-08-30 14:01:10 -07002389static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
2390 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002391 int i, j, k, l, m;
Alex Conversea9598cd2017-02-03 14:18:05 -08002392#if CONFIG_EC_ADAPT
2393 const int node_limit = UNCONSTRAINED_NODES - 1;
2394#else
Thomas9ac55082016-09-23 18:04:17 +01002395 const int node_limit = UNCONSTRAINED_NODES;
Alex Conversea9598cd2017-02-03 14:18:05 -08002396#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002397
Michael Bebenita6048d052016-08-25 14:40:54 -07002398 if (aom_read_bit(r, ACCT_STR))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002399 for (i = 0; i < PLANE_TYPES; ++i)
2400 for (j = 0; j < REF_TYPES; ++j)
2401 for (k = 0; k < COEF_BANDS; ++k)
2402 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
Thomas9ac55082016-09-23 18:04:17 +01002403 for (m = 0; m < node_limit; ++m)
Michael Bebenita6048d052016-08-25 14:40:54 -07002404 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002405}
2406
Yaowu Xuf883b422016-08-30 14:01:10 -07002407static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002408 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
2409 TX_SIZE tx_size;
Jingning Han83630632016-12-16 11:27:25 -08002410 for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002411 read_coef_probs_common(fc->coef_probs[tx_size], r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002412}
Yushin Cho77bba8d2016-11-04 16:36:56 -07002413#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002414
Yaowu Xuf883b422016-08-30 14:01:10 -07002415static void setup_segmentation(AV1_COMMON *const cm,
2416 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002417 struct segmentation *const seg = &cm->seg;
2418 int i, j;
2419
2420 seg->update_map = 0;
2421 seg->update_data = 0;
2422
Yaowu Xuf883b422016-08-30 14:01:10 -07002423 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002424 if (!seg->enabled) return;
2425
2426 // Segmentation map update
2427 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2428 seg->update_map = 1;
2429 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002430 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002431 }
2432 if (seg->update_map) {
2433 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2434 seg->temporal_update = 0;
2435 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002436 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002437 }
2438 }
2439
2440 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07002441 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002442 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002443 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002444
Yaowu Xuf883b422016-08-30 14:01:10 -07002445 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002446
2447 for (i = 0; i < MAX_SEGMENTS; i++) {
2448 for (j = 0; j < SEG_LVL_MAX; j++) {
2449 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002450 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002451 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002452 av1_enable_segfeature(seg, i, j);
2453 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
2454 if (av1_is_segfeature_signed(j))
2455 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002456 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002457 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002458 }
2459 }
2460 }
2461}
2462
2463#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002464static void decode_restoration_mode(AV1_COMMON *cm,
2465 struct aom_read_bit_buffer *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002466 int p;
2467 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002468 if (aom_rb_read_bit(rb)) {
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002469 rsi->frame_restoration_type =
2470 aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002471 } else {
2472 rsi->frame_restoration_type =
2473 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
2474 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002475 for (p = 1; p < MAX_MB_PLANE; ++p) {
2476 cm->rst_info[p].frame_restoration_type =
2477 aom_rb_read_bit(rb) ? RESTORE_WIENER : RESTORE_NONE;
2478 }
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002479
2480 cm->rst_info[0].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2481 cm->rst_info[1].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2482 cm->rst_info[2].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2483 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
2484 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
2485 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
2486 rsi = &cm->rst_info[0];
2487 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2488 if (rsi->restoration_tilesize != RESTORATION_TILESIZE_MAX) {
2489 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2490 }
2491 cm->rst_info[1].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2492 cm->rst_info[2].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2493 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002494}
2495
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002496static void read_wiener_filter(WienerInfo *wiener_info, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002497 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002498 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2499 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002500 wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002501 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2502 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002503 wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002504 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2505 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002506 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002507 wiener_info->vfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002508 -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
2509 wiener_info->vfilter[2]);
2510
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002511 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002512 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2513 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002514 wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002515 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2516 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002517 wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002518 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2519 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002520 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002521 wiener_info->hfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002522 -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
2523 wiener_info->hfilter[2]);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002524}
2525
2526static void read_sgrproj_filter(SgrprojInfo *sgrproj_info, aom_reader *rb) {
2527 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
2528 sgrproj_info->xqd[0] =
2529 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN0;
2530 sgrproj_info->xqd[1] =
2531 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN1;
2532}
2533
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002534static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002535 int i, p;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002536 const int ntiles = av1_get_rest_ntiles(cm->width, cm->height,
2537 cm->rst_info[0].restoration_tilesize,
2538 NULL, NULL, NULL, NULL);
2539 const int ntiles_uv = av1_get_rest_ntiles(
2540 ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
2541 ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y),
2542 cm->rst_info[1].restoration_tilesize, NULL, NULL, NULL, NULL);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002543 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002544 if (rsi->frame_restoration_type != RESTORE_NONE) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002545 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002546 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002547 rsi->restoration_type[i] =
2548 aom_read_tree(rb, av1_switchable_restore_tree,
2549 cm->fc->switchable_restore_prob, ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002550 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002551 read_wiener_filter(&rsi->wiener_info[i], rb);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002552 } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002553 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002554 }
2555 }
2556 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002557 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002558 if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002559 rsi->restoration_type[i] = RESTORE_WIENER;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002560 read_wiener_filter(&rsi->wiener_info[i], rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002561 } else {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002562 rsi->restoration_type[i] = RESTORE_NONE;
2563 }
2564 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002565 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002566 for (i = 0; i < ntiles; ++i) {
2567 if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
2568 rsi->restoration_type[i] = RESTORE_SGRPROJ;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002569 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
2570 } else {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002571 rsi->restoration_type[i] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002572 }
2573 }
2574 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002575 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002576 for (p = 1; p < MAX_MB_PLANE; ++p) {
2577 rsi = &cm->rst_info[p];
2578 if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08002579 for (i = 0; i < ntiles_uv; ++i) {
2580 if (ntiles_uv > 1)
2581 rsi->restoration_type[i] =
2582 aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR) ? RESTORE_WIENER
2583 : RESTORE_NONE;
2584 else
2585 rsi->restoration_type[i] = RESTORE_WIENER;
2586 if (rsi->restoration_type[i] == RESTORE_WIENER) {
2587 read_wiener_filter(&rsi->wiener_info[i], rb);
2588 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002589 }
2590 }
2591 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002592}
2593#endif // CONFIG_LOOP_RESTORATION
2594
Yaowu Xuf883b422016-08-30 14:01:10 -07002595static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002596 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07002597 lf->filter_level = aom_rb_read_literal(rb, 6);
2598 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002599
2600 // Read in loop filter deltas applied at the MB level based on mode or ref
2601 // frame.
2602 lf->mode_ref_delta_update = 0;
2603
Yaowu Xuf883b422016-08-30 14:01:10 -07002604 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002605 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002606 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002607 if (lf->mode_ref_delta_update) {
2608 int i;
2609
2610 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002611 if (aom_rb_read_bit(rb))
2612 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002613
2614 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002615 if (aom_rb_read_bit(rb))
2616 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002617 }
2618 }
2619}
2620
Jean-Marc Valin01435132017-02-18 14:12:53 -05002621#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002622static void setup_cdef(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
2623 cm->dering_level = aom_rb_read_literal(rb, DERING_LEVEL_BITS);
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02002624 cm->clpf_strength_u = aom_rb_read_literal(rb, 2);
2625 cm->clpf_strength_v = aom_rb_read_literal(rb, 2);
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002626 id_to_levels(cm->dering_lev, cm->clpf_str, cm->dering_level);
2627 cdef_get_bits(cm->dering_lev, cm->clpf_str, &cm->dering_bits, &cm->clpf_bits);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002628}
Jean-Marc Valin01435132017-02-18 14:12:53 -05002629#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002630
Yaowu Xuf883b422016-08-30 14:01:10 -07002631static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2632 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002633}
2634
Yaowu Xuf883b422016-08-30 14:01:10 -07002635static void setup_quantization(AV1_COMMON *const cm,
2636 struct aom_read_bit_buffer *rb) {
2637 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002638 cm->y_dc_delta_q = read_delta_q(rb);
2639 cm->uv_dc_delta_q = read_delta_q(rb);
2640 cm->uv_ac_delta_q = read_delta_q(rb);
2641 cm->dequant_bit_depth = cm->bit_depth;
2642#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002643 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002644 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002645 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2646 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002647 } else {
2648 cm->min_qmlevel = 0;
2649 cm->max_qmlevel = 0;
2650 }
2651#endif
2652}
2653
Yaowu Xuf883b422016-08-30 14:01:10 -07002654static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002655 // Build y/uv dequant values based on segmentation.
2656 int i = 0;
2657#if CONFIG_AOM_QM
2658 int lossless;
2659 int j = 0;
2660 int qmlevel;
2661 int using_qm = cm->using_qmatrix;
2662 int minqm = cm->min_qmlevel;
2663 int maxqm = cm->max_qmlevel;
2664#endif
2665#if CONFIG_NEW_QUANT
2666 int b;
2667 int dq;
2668#endif // CONFIG_NEW_QUANT
2669 if (cm->seg.enabled) {
2670 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002671 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002672 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002673 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2674 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002675 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002676 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002677 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002678 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002679#if CONFIG_AOM_QM
2680 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2681 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2682 // NB: depends on base index so there is only 1 set per frame
2683 // No quant weighting when lossless or signalled not using QM
2684 qmlevel = (lossless || using_qm == 0)
2685 ? NUM_QM_LEVELS - 1
2686 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2687 for (j = 0; j < TX_SIZES; ++j) {
2688 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2689 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2690 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2691 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2692 }
2693#endif // CONFIG_AOM_QM
2694#if CONFIG_NEW_QUANT
2695 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2696 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002697 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002698 cm->y_dequant_nuq[i][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002699 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002700 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002701 }
2702 }
2703#endif // CONFIG_NEW_QUANT
2704 }
2705 } else {
2706 const int qindex = cm->base_qindex;
2707 // When segmentation is disabled, only the first value is used. The
2708 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002709 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2710 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002711 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002712 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002713 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002714 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002715#if CONFIG_AOM_QM
2716 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2717 cm->uv_ac_delta_q == 0;
2718 // No quant weighting when lossless or signalled not using QM
2719 qmlevel = (lossless || using_qm == 0)
2720 ? NUM_QM_LEVELS - 1
2721 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2722 for (j = 0; j < TX_SIZES; ++j) {
2723 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2724 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2725 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2726 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2727 }
2728#endif
2729#if CONFIG_NEW_QUANT
2730 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2731 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002732 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002733 cm->y_dequant_nuq[0][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002734 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002735 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002736 }
2737 }
2738#endif // CONFIG_NEW_QUANT
2739 }
2740}
2741
Angie Chiang5678ad92016-11-21 09:38:40 -08002742static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002743 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07002744 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002745}
2746
Yaowu Xuf883b422016-08-30 14:01:10 -07002747static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002748 cm->render_width = cm->width;
2749 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002750 if (aom_rb_read_bit(rb))
2751 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002752}
2753
Yaowu Xuf883b422016-08-30 14:01:10 -07002754static void resize_mv_buffer(AV1_COMMON *cm) {
2755 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002756 cm->cur_frame->mi_rows = cm->mi_rows;
2757 cm->cur_frame->mi_cols = cm->mi_cols;
2758 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002759 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002760 sizeof(*cm->cur_frame->mvs)));
2761}
2762
Yaowu Xuf883b422016-08-30 14:01:10 -07002763static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002764#if CONFIG_SIZE_LIMIT
2765 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002766 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002767 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2768 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2769#endif
2770 if (cm->width != width || cm->height != height) {
2771 const int new_mi_rows =
2772 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2773 const int new_mi_cols =
2774 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2775
Yaowu Xuf883b422016-08-30 14:01:10 -07002776 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002777 // dimensions as well as the overall size.
2778 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002779 if (av1_alloc_context_buffers(cm, width, height))
2780 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002781 "Failed to allocate context buffers");
2782 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002783 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002784 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002785 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002786 cm->width = width;
2787 cm->height = height;
2788 }
2789 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2790 cm->mi_cols > cm->cur_frame->mi_cols) {
2791 resize_mv_buffer(cm);
2792 }
2793}
2794
Yaowu Xuf883b422016-08-30 14:01:10 -07002795static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002796 int width, height;
2797 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002798 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002799 resize_context_buffers(cm, width, height);
2800 setup_render_size(cm, rb);
2801
2802 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002803 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002804 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2805 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002806#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002807 cm->use_highbitdepth,
2808#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002809 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002810 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2811 pool->cb_priv)) {
2812 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002813 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002814 "Failed to allocate frame buffer");
2815 }
2816 unlock_buffer_pool(pool);
2817
2818 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2819 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2820 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2821 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2822 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2823 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2824 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2825}
2826
Yaowu Xuf883b422016-08-30 14:01:10 -07002827static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002828 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002829 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002830 int this_xss, int this_yss) {
2831 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2832 ref_yss == this_yss;
2833}
2834
Yaowu Xuf883b422016-08-30 14:01:10 -07002835static void setup_frame_size_with_refs(AV1_COMMON *cm,
2836 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002837 int width, height;
2838 int found = 0, i;
2839 int has_valid_ref_frame = 0;
2840 BufferPool *const pool = cm->buffer_pool;
2841 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002842 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002843 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2844 width = buf->y_crop_width;
2845 height = buf->y_crop_height;
2846 cm->render_width = buf->render_width;
2847 cm->render_height = buf->render_height;
2848 found = 1;
2849 break;
2850 }
2851 }
2852
2853 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002854 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002855 setup_render_size(cm, rb);
2856 }
2857
2858 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002859 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002860 "Invalid frame size");
2861
2862 // Check to make sure at least one of frames that this frame references
2863 // has valid dimensions.
2864 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2865 RefBuffer *const ref_frame = &cm->frame_refs[i];
2866 has_valid_ref_frame |=
2867 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2868 ref_frame->buf->y_crop_height, width, height);
2869 }
2870 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002871 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002872 "Referenced frame has invalid size");
2873 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2874 RefBuffer *const ref_frame = &cm->frame_refs[i];
2875 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
2876 ref_frame->buf->subsampling_x,
2877 ref_frame->buf->subsampling_y, cm->bit_depth,
2878 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07002879 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002880 "Referenced frame has incompatible color format");
2881 }
2882
2883 resize_context_buffers(cm, width, height);
2884
2885 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002886 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002887 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2888 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002889#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002890 cm->use_highbitdepth,
2891#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002892 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002893 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2894 pool->cb_priv)) {
2895 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002896 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002897 "Failed to allocate frame buffer");
2898 }
2899 unlock_buffer_pool(pool);
2900
2901 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2902 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2903 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2904 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2905 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2906 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2907 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2908}
2909
Yaowu Xuf883b422016-08-30 14:01:10 -07002910static void read_tile_info(AV1Decoder *const pbi,
2911 struct aom_read_bit_buffer *const rb) {
2912 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002913#if CONFIG_EXT_TILE
2914// Read the tile width/height
2915#if CONFIG_EXT_PARTITION
2916 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002917 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
2918 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002919 } else
2920#endif // CONFIG_EXT_PARTITION
2921 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002922 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
2923 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002924 }
2925
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002926#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002927 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002928#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002929
Yaowu Xuc27fc142016-08-22 16:08:15 -07002930 cm->tile_width <<= cm->mib_size_log2;
2931 cm->tile_height <<= cm->mib_size_log2;
2932
Yaowu Xuf883b422016-08-30 14:01:10 -07002933 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
2934 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002935
2936 // Get the number of tiles
2937 cm->tile_cols = 1;
2938 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
2939
2940 cm->tile_rows = 1;
2941 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
2942
2943 if (cm->tile_cols * cm->tile_rows > 1) {
2944 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07002945 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2946 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002947 }
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002948
2949#if CONFIG_DEPENDENT_HORZTILES
2950 if (cm->tile_rows <= 1)
2951 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
2952 else
2953 cm->dependent_horz_tiles = 0;
2954#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002955#else
2956 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07002957 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002958
2959 // columns
2960 max_ones = max_log2_tile_cols - min_log2_tile_cols;
2961 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07002962 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002963
2964 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07002965 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002966 "Invalid number of tile columns");
2967
2968 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07002969 cm->log2_tile_rows = aom_rb_read_bit(rb);
2970 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002971#if CONFIG_DEPENDENT_HORZTILES
2972 if (cm->log2_tile_rows != 0)
2973 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
2974 else
2975 cm->dependent_horz_tiles = 0;
2976#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002977#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002978 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002979#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002980
Yaowu Xuc27fc142016-08-22 16:08:15 -07002981 cm->tile_cols = 1 << cm->log2_tile_cols;
2982 cm->tile_rows = 1 << cm->log2_tile_rows;
2983
2984 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
2985 cm->tile_width >>= cm->log2_tile_cols;
2986 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
2987 cm->tile_height >>= cm->log2_tile_rows;
2988
2989 // round to integer multiples of superblock size
2990 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
2991 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
2992
Thomas Davies4974e522016-11-07 17:44:05 +00002993// tile size magnitude
2994#if !CONFIG_TILE_GROUPS
2995 if (cm->tile_rows > 1 || cm->tile_cols > 1)
2996#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002997 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002998#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00002999
Thomas Davies80188d12016-10-26 16:08:35 -07003000#if CONFIG_TILE_GROUPS
3001 // Store an index to the location of the tile group information
3002 pbi->tg_size_bit_offset = rb->bit_offset;
3003 pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
3004 if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
3005 pbi->tg_start =
3006 aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3007 pbi->tg_size =
3008 1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3009 }
3010#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003011}
3012
3013static int mem_get_varsize(const uint8_t *src, const int sz) {
3014 switch (sz) {
3015 case 1: return src[0];
3016 case 2: return mem_get_le16(src);
3017 case 3: return mem_get_le24(src);
3018 case 4: return mem_get_le32(src);
3019 default: assert("Invalid size" && 0); return -1;
3020 }
3021}
3022
3023#if CONFIG_EXT_TILE
3024// Reads the next tile returning its size and adjusting '*data' accordingly
3025// based on 'is_last'.
3026static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07003027 struct aom_internal_error_info *error_info,
3028 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003029 void *decrypt_state,
3030 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
3031 int tile_size_bytes, int col, int row) {
3032 size_t size;
3033
3034 size_t copy_size = 0;
3035 const uint8_t *copy_data = NULL;
3036
3037 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003038 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003039 "Truncated packet or corrupt tile length");
3040 if (decrypt_cb) {
3041 uint8_t be_data[4];
3042 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3043
3044 // Only read number of bytes in cm->tile_size_bytes.
3045 size = mem_get_varsize(be_data, tile_size_bytes);
3046 } else {
3047 size = mem_get_varsize(*data, tile_size_bytes);
3048 }
3049
3050 // The top bit indicates copy mode
3051 if ((size >> (tile_size_bytes * 8 - 1)) == 1) {
3052 // The remaining bits in the top byte signal the row offset
3053 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
3054
3055 // Currently, only use tiles in same column as reference tiles.
3056 copy_data = tile_buffers[row - offset][col].data;
3057 copy_size = tile_buffers[row - offset][col].size;
3058 size = 0;
3059 }
3060
3061 *data += tile_size_bytes;
3062
3063 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003064 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003065 "Truncated packet or corrupt tile size");
3066
3067 if (size > 0) {
3068 tile_buffers[row][col].data = *data;
3069 tile_buffers[row][col].size = size;
3070 } else {
3071 tile_buffers[row][col].data = copy_data;
3072 tile_buffers[row][col].size = copy_size;
3073 }
3074
3075 *data += size;
3076
3077 tile_buffers[row][col].raw_data_end = *data;
3078}
3079
3080static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003081 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003082 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003083 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003084 const int tile_cols = cm->tile_cols;
3085 const int tile_rows = cm->tile_rows;
3086 const int have_tiles = tile_cols * tile_rows > 1;
3087
3088 if (!have_tiles) {
3089 const uint32_t tile_size = data_end - data;
3090 tile_buffers[0][0].data = data;
3091 tile_buffers[0][0].size = tile_size;
3092 tile_buffers[0][0].raw_data_end = NULL;
3093 } else {
3094 // We locate only the tile buffers that are required, which are the ones
3095 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
3096 // need the last (bottom right) tile buffer, as we need to know where the
3097 // end of the compressed frame buffer is for proper superframe decoding.
3098
3099 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
3100 const uint8_t *const data_start = data;
3101
Yaowu Xuf883b422016-08-30 14:01:10 -07003102 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003103 const int single_row = pbi->dec_tile_row >= 0;
3104 const int tile_rows_start = single_row ? dec_tile_row : 0;
3105 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003106 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003107 const int single_col = pbi->dec_tile_col >= 0;
3108 const int tile_cols_start = single_col ? dec_tile_col : 0;
3109 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3110
3111 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
3112 const int tile_size_bytes = pbi->tile_size_bytes;
3113
3114 size_t tile_col_size;
3115 int r, c;
3116
3117 // Read tile column sizes for all columns (we need the last tile buffer)
3118 for (c = 0; c < tile_cols; ++c) {
3119 const int is_last = c == tile_cols - 1;
3120 if (!is_last) {
3121 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
3122 data += tile_col_size_bytes;
3123 tile_col_data_end[c] = data + tile_col_size;
3124 } else {
3125 tile_col_size = data_end - data;
3126 tile_col_data_end[c] = data_end;
3127 }
3128 data += tile_col_size;
3129 }
3130
3131 data = data_start;
3132
3133 // Read the required tile sizes.
3134 for (c = tile_cols_start; c < tile_cols_end; ++c) {
3135 const int is_last = c == tile_cols - 1;
3136
3137 if (c > 0) data = tile_col_data_end[c - 1];
3138
3139 if (!is_last) data += tile_col_size_bytes;
3140
3141 // Get the whole of the last column, otherwise stop at the required tile.
3142 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
3143 tile_buffers[r][c].col = c;
3144
3145 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3146 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3147 tile_size_bytes, c, r);
3148 }
3149 }
3150
3151 // If we have not read the last column, then read it to get the last tile.
3152 if (tile_cols_end != tile_cols) {
3153 c = tile_cols - 1;
3154
3155 data = tile_col_data_end[c - 1];
3156
3157 for (r = 0; r < tile_rows; ++r) {
3158 tile_buffers[r][c].col = c;
3159
3160 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3161 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3162 tile_size_bytes, c, r);
3163 }
3164 }
3165 }
3166}
3167#else
3168// Reads the next tile returning its size and adjusting '*data' accordingly
3169// based on 'is_last'.
3170static void get_tile_buffer(const uint8_t *const data_end,
3171 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07003172 struct aom_internal_error_info *error_info,
3173 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003174 void *decrypt_state, TileBufferDec *const buf) {
3175 size_t size;
3176
3177 if (!is_last) {
Yaowu Xu0a79a1b2017-02-17 13:04:54 -08003178 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003179 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003180 "Truncated packet or corrupt tile length");
3181
3182 if (decrypt_cb) {
3183 uint8_t be_data[4];
3184 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3185 size = mem_get_varsize(be_data, tile_size_bytes);
3186 } else {
3187 size = mem_get_varsize(*data, tile_size_bytes);
3188 }
3189 *data += tile_size_bytes;
3190
3191 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003192 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003193 "Truncated packet or corrupt tile size");
3194 } else {
3195 size = data_end - *data;
3196 }
3197
3198 buf->data = *data;
3199 buf->size = size;
3200
3201 *data += size;
3202}
3203
3204static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003205 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003206 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003207 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07003208#if CONFIG_TILE_GROUPS
3209 int r, c;
3210 const int tile_cols = cm->tile_cols;
3211 const int tile_rows = cm->tile_rows;
3212 int tc = 0;
3213 int first_tile_in_tg = 0;
3214 int hdr_offset;
3215 struct aom_read_bit_buffer rb_tg_hdr;
3216 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3217 const int num_tiles = tile_rows * tile_cols;
3218 const int num_bits = OD_ILOG(num_tiles) - 1;
3219 const int hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
3220 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
Fangwen Fu73126c02017-02-08 22:37:47 -08003221#if CONFIG_DEPENDENT_HORZTILES
3222 int tile_group_start_col = 0;
3223 int tile_group_start_row = 0;
3224#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003225
3226 for (r = 0; r < tile_rows; ++r) {
3227 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07003228 TileBufferDec *const buf = &tile_buffers[r][c];
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003229 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
Thomas Davies80188d12016-10-26 16:08:35 -07003230 hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
3231
3232 buf->col = c;
3233 if (hdr_offset) {
3234 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
3235 rb_tg_hdr.bit_offset = tg_size_bit_offset;
3236 if (num_tiles) {
3237 pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
3238 pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
Fangwen Fu73126c02017-02-08 22:37:47 -08003239#if CONFIG_DEPENDENT_HORZTILES
3240 tile_group_start_row = r;
3241 tile_group_start_col = c;
3242#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003243 }
3244 }
3245 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
3246 data += hdr_offset;
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003247 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last,
3248 &pbi->common.error, &data, pbi->decrypt_cb,
3249 pbi->decrypt_state, buf);
Fangwen Fu73126c02017-02-08 22:37:47 -08003250#if CONFIG_DEPENDENT_HORZTILES
3251 cm->tile_group_start_row[r][c] = tile_group_start_row;
3252 cm->tile_group_start_col[r][c] = tile_group_start_col;
3253#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003254 }
3255 }
3256#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003257 int r, c;
3258 const int tile_cols = cm->tile_cols;
3259 const int tile_rows = cm->tile_rows;
3260
3261 for (r = 0; r < tile_rows; ++r) {
3262 for (c = 0; c < tile_cols; ++c) {
3263 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
3264 TileBufferDec *const buf = &tile_buffers[r][c];
3265 buf->col = c;
3266 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
3267 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
3268 }
3269 }
Thomas Davies80188d12016-10-26 16:08:35 -07003270#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003271}
3272#endif // CONFIG_EXT_TILE
3273
Yushin Cho77bba8d2016-11-04 16:36:56 -07003274#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003275static void daala_dec_init(AV1_COMMON *const cm, daala_dec_ctx *daala_dec,
Nathan E. Eggeab083972016-12-28 15:31:46 -05003276 aom_reader *r) {
3277 daala_dec->r = r;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003278 od_adapt_ctx_reset(&daala_dec->state.adapt, 0);
3279
Yushin Cho70669122016-12-08 09:53:14 -10003280 // TODO(yushin) : activity masking info needs be signaled by a bitstream
3281 daala_dec->use_activity_masking = AV1_PVQ_ENABLE_ACTIVITY_MASKING;
3282
Yushin Cho7a428ba2017-01-12 16:28:49 -08003283#if !CONFIG_DAALA_DIST
3284 daala_dec->use_activity_masking = 0;
3285#endif
3286
Yushin Cho70669122016-12-08 09:53:14 -10003287 if (daala_dec->use_activity_masking)
3288 daala_dec->qm = OD_HVS_QM;
3289 else
3290 daala_dec->qm = OD_FLAT_QM;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003291
3292 od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
3293 daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
Yushin Cho70669122016-12-08 09:53:14 -10003294
3295 if (daala_dec->use_activity_masking) {
3296 int pli;
3297 int use_masking = daala_dec->use_activity_masking;
3298 int segment_id = 0;
3299 int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
3300
3301 for (pli = 0; pli < MAX_MB_PLANE; pli++) {
3302 int i;
3303 int q;
3304
3305 q = qindex;
3306 if (q <= OD_DEFAULT_QMS[use_masking][0][pli].interp_q << OD_COEFF_SHIFT) {
3307 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3308 &OD_DEFAULT_QMS[use_masking][0][pli], NULL);
3309 } else {
3310 i = 0;
3311 while (OD_DEFAULT_QMS[use_masking][i + 1][pli].qm_q4 != NULL &&
3312 q > OD_DEFAULT_QMS[use_masking][i + 1][pli].interp_q
3313 << OD_COEFF_SHIFT) {
3314 i++;
3315 }
3316 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3317 &OD_DEFAULT_QMS[use_masking][i][pli],
3318 &OD_DEFAULT_QMS[use_masking][i + 1][pli]);
3319 }
3320 }
3321 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07003322}
Yushin Cho70669122016-12-08 09:53:14 -10003323#endif // #if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003324
Yaowu Xuf883b422016-08-30 14:01:10 -07003325static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003326 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003327 AV1_COMMON *const cm = &pbi->common;
3328 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003329 const int tile_cols = cm->tile_cols;
3330 const int tile_rows = cm->tile_rows;
3331 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07003332 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003333#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003334 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003335 const int single_row = pbi->dec_tile_row >= 0;
3336 const int tile_rows_start = single_row ? dec_tile_row : 0;
3337 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003338 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003339 const int single_col = pbi->dec_tile_col >= 0;
3340 const int tile_cols_start = single_col ? dec_tile_col : 0;
3341 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3342 const int inv_col_order = pbi->inv_tile_order && !single_col;
3343 const int inv_row_order = pbi->inv_tile_order && !single_row;
3344#else
3345 const int tile_rows_start = 0;
3346 const int tile_rows_end = tile_rows;
3347 const int tile_cols_start = 0;
3348 const int tile_cols_end = tile_cols;
3349 const int inv_col_order = pbi->inv_tile_order;
3350 const int inv_row_order = pbi->inv_tile_order;
3351#endif // CONFIG_EXT_TILE
3352 int tile_row, tile_col;
3353
hui su0d103572017-03-01 17:58:01 -08003354#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003355 cm->do_subframe_update = n_tiles == 1;
hui su0d103572017-03-01 17:58:01 -08003356#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003357
3358 if (cm->lf.filter_level && !cm->skip_loop_filter &&
3359 pbi->lf_worker.data1 == NULL) {
3360 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07003361 aom_memalign(32, sizeof(LFWorkerData)));
3362 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003363 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003364 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003365 "Loop filter thread creation failed");
3366 }
3367 }
3368
3369 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3370 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3371 // Be sure to sync as we might be resuming after a failed frame decode.
3372 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003373 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
3374 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003375 }
3376
3377 assert(tile_rows <= MAX_TILE_ROWS);
3378 assert(tile_cols <= MAX_TILE_COLS);
3379
3380 get_tile_buffers(pbi, data, data_end, tile_buffers);
3381
3382 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003383 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003384 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003385 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003386 pbi->allocated_tiles = n_tiles;
3387 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003388#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003389 if (pbi->acct_enabled) {
3390 aom_accounting_reset(&pbi->accounting);
3391 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003392#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003393 // Load all tile information into tile_data.
3394 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3395 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3396 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
3397 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
3398
3399 td->cm = cm;
3400 td->xd = pbi->mb;
3401 td->xd.corrupted = 0;
3402 td->xd.counts =
3403 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3404 ? &cm->counts
3405 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003406 av1_zero(td->dqcoeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003407#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003408 av1_zero(td->pvq_ref_coeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003409#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003410 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003411 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003412 &td->bit_reader,
3413#if CONFIG_ANS && ANS_MAX_SYMBOLS
3414 1 << cm->ans_window_size_log2,
3415#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3416 pbi->decrypt_cb, pbi->decrypt_state);
Michael Bebenita6048d052016-08-25 14:40:54 -07003417#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003418 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003419 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003420 } else {
David Barkerd971f402016-10-25 13:52:07 +01003421 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003422 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003423#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003424 av1_init_macroblockd(cm, &td->xd,
3425#if CONFIG_PVQ
3426 td->pvq_ref_coeff,
3427#endif
3428 td->dqcoeff);
3429#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003430 daala_dec_init(cm, &td->xd.daala_dec, &td->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003431#endif
Thomas Daviesf77d4ad2017-01-10 18:55:42 +00003432#if CONFIG_EC_ADAPT
3433 // Initialise the tile context from the frame context
3434 td->tctx = *cm->fc;
3435 td->xd.tile_ctx = &td->tctx;
3436#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003437#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003438 td->xd.plane[0].color_index_map = td->color_index_map[0];
3439 td->xd.plane[1].color_index_map = td->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003440#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003441 }
3442 }
3443
3444 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3445 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
3446 int mi_row = 0;
3447 TileInfo tile_info;
3448
Yaowu Xuf883b422016-08-30 14:01:10 -07003449 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003450
3451 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3452 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
3453 TileData *const td = pbi->tile_data + tile_cols * row + col;
Michael Bebenita6048d052016-08-25 14:40:54 -07003454#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003455 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003456 td->bit_reader.accounting->last_tell_frac =
3457 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003458 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003459#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003460
Yaowu Xuf883b422016-08-30 14:01:10 -07003461 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003462
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003463#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003464#if CONFIG_TILE_GROUPS
3465 av1_tile_set_tg_boundary(&tile_info, cm, tile_row, tile_col);
3466 if (!cm->dependent_horz_tiles || tile_row == 0 ||
3467 tile_info.tg_horz_boundary) {
3468#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003469 if (!cm->dependent_horz_tiles || tile_row == 0) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003470#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003471 av1_zero_above_context(cm, tile_info.mi_col_start,
3472 tile_info.mi_col_end);
3473 }
3474#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003475 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003476#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003477
3478 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
3479 mi_row += cm->mib_size) {
3480 int mi_col;
3481
Yaowu Xuf883b422016-08-30 14:01:10 -07003482 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003483
3484 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
3485 mi_col += cm->mib_size) {
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003486 av1_update_boundary_info(cm, &tile_info, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003487 decode_partition(pbi, &td->xd,
3488#if CONFIG_SUPERTX
3489 0,
3490#endif // CONFIG_SUPERTX
3491 mi_row, mi_col, &td->bit_reader, cm->sb_size,
3492 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003493#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3494 detoken_and_recon_sb(pbi, &td->xd, mi_row, mi_col, &td->bit_reader,
3495 cm->sb_size);
3496#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003497 }
Angie Chiangd0916d92017-03-10 17:54:18 -08003498 aom_merge_corrupted_flag(&pbi->mb.corrupted, td->xd.corrupted);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003499 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003500 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003501 "Failed to decode tile data");
hui su0d103572017-03-01 17:58:01 -08003502#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003503 if (cm->do_subframe_update &&
3504 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
hui su0161a932017-01-24 14:12:11 -08003505 const int mi_rows_per_update =
3506 MI_SIZE * AOMMAX(cm->mi_rows / MI_SIZE / COEF_PROBS_BUFS, 1);
3507 if ((mi_row + MI_SIZE) % mi_rows_per_update == 0 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003508 mi_row + MI_SIZE < cm->mi_rows &&
3509 cm->coef_probs_update_idx < COEF_PROBS_BUFS - 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003510 av1_partial_adapt_probs(cm, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003511 ++cm->coef_probs_update_idx;
3512 }
3513 }
hui su0d103572017-03-01 17:58:01 -08003514#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003515 }
3516 }
3517
3518 assert(mi_row > 0);
3519
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003520// when Parallel deblocking is enabled, deblocking should not
3521// be interleaved with decoding. Instead, deblocking should be done
3522// after the entire frame is decoded.
3523#if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003524 // Loopfilter one tile row.
3525 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3526 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003527 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003528 const int lf_end = tile_info.mi_row_end - cm->mib_size;
3529
3530 // Delay the loopfilter if the first tile row is only
3531 // a single superblock high.
3532 if (lf_end <= 0) continue;
3533
3534 // Decoding has completed. Finish up the loop filter in this thread.
3535 if (tile_info.mi_row_end >= cm->mi_rows) continue;
3536
3537 winterface->sync(&pbi->lf_worker);
3538 lf_data->start = lf_start;
3539 lf_data->stop = lf_end;
3540 if (pbi->max_threads > 1) {
3541 winterface->launch(&pbi->lf_worker);
3542 } else {
3543 winterface->execute(&pbi->lf_worker);
3544 }
3545 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003546#endif // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003547
3548 // After loopfiltering, the last 7 row pixels in each superblock row may
3549 // still be changed by the longest loopfilter of the next superblock row.
3550 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003551 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003552 }
3553
3554#if CONFIG_VAR_TX
3555 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003556 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
3557 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003558#else
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003559#if CONFIG_PARALLEL_DEBLOCKING
3560 // Loopfilter all rows in the frame in the frame.
3561 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3562 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3563 winterface->sync(&pbi->lf_worker);
3564 lf_data->start = 0;
3565 lf_data->stop = cm->mi_rows;
3566 winterface->execute(&pbi->lf_worker);
3567 }
3568#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003569 // Loopfilter remaining rows in the frame.
3570 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3571 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3572 winterface->sync(&pbi->lf_worker);
3573 lf_data->start = lf_data->stop;
3574 lf_data->stop = cm->mi_rows;
3575 winterface->execute(&pbi->lf_worker);
3576 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003577#endif // CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003578#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003579 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003580 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003581
3582#if CONFIG_EXT_TILE
3583 if (n_tiles == 1) {
3584#if CONFIG_ANS
3585 return data_end;
3586#else
3587 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07003588 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003589#endif // CONFIG_ANS
3590 } else {
3591 // Return the end of the last tile buffer
3592 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3593 }
3594#else
3595#if CONFIG_ANS
3596 return data_end;
3597#else
3598 {
3599 // Get last tile data.
3600 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003601 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003602 }
3603#endif // CONFIG_ANS
3604#endif // CONFIG_EXT_TILE
3605}
3606
3607static int tile_worker_hook(TileWorkerData *const tile_data,
3608 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003609 AV1Decoder *const pbi = tile_data->pbi;
3610 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003611 int mi_row, mi_col;
3612
3613 if (setjmp(tile_data->error_info.jmp)) {
3614 tile_data->error_info.setjmp = 0;
Angie Chiangd0916d92017-03-10 17:54:18 -08003615 aom_merge_corrupted_flag(&tile_data->xd.corrupted, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003616 return 0;
3617 }
3618
3619 tile_data->error_info.setjmp = 1;
3620 tile_data->xd.error_info = &tile_data->error_info;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003621#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003622#if CONFIG_TILE_GROUPS
3623 if (!cm->dependent_horz_tiles || tile->tg_horz_boundary) {
3624#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003625 if (!cm->dependent_horz_tiles) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003626#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003627 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
3628 }
3629#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003630 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003631#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003632
3633 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
3634 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003635 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003636
3637 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3638 mi_col += cm->mib_size) {
3639 decode_partition(pbi, &tile_data->xd,
3640#if CONFIG_SUPERTX
3641 0,
3642#endif
3643 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
3644 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003645#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3646 detoken_and_recon_sb(pbi, &tile_data->xd, mi_row, mi_col,
3647 &tile_data->bit_reader, cm->sb_size);
3648#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003649 }
3650 }
3651 return !tile_data->xd.corrupted;
3652}
3653
3654// sorts in descending order
3655static int compare_tile_buffers(const void *a, const void *b) {
3656 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
3657 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
3658 return (int)(buf2->size - buf1->size);
3659}
3660
Yaowu Xuf883b422016-08-30 14:01:10 -07003661static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003662 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003663 AV1_COMMON *const cm = &pbi->common;
3664 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003665 const int tile_cols = cm->tile_cols;
3666 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003667 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
clang-format67948d32016-09-07 22:40:40 -07003668 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003669#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003670 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003671 const int single_row = pbi->dec_tile_row >= 0;
3672 const int tile_rows_start = single_row ? dec_tile_row : 0;
3673 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003674 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003675 const int single_col = pbi->dec_tile_col >= 0;
3676 const int tile_cols_start = single_col ? dec_tile_col : 0;
3677 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3678#else
3679 const int tile_rows_start = 0;
3680 const int tile_rows_end = tile_rows;
3681 const int tile_cols_start = 0;
3682 const int tile_cols_end = tile_cols;
3683#endif // CONFIG_EXT_TILE
3684 int tile_row, tile_col;
3685 int i;
3686
3687#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3688 int final_worker = -1;
3689#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3690
3691 assert(tile_rows <= MAX_TILE_ROWS);
3692 assert(tile_cols <= MAX_TILE_COLS);
3693
3694 assert(tile_cols * tile_rows > 1);
3695
Yaowu Xuc27fc142016-08-22 16:08:15 -07003696 // TODO(jzern): See if we can remove the restriction of passing in max
3697 // threads to the decoder.
3698 if (pbi->num_tile_workers == 0) {
3699 const int num_threads = pbi->max_threads & ~1;
3700 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07003701 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003702 // Ensure tile data offsets will be properly aligned. This may fail on
3703 // platforms without DECLARE_ALIGNED().
3704 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3705 CHECK_MEM_ERROR(
3706 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003707 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003708 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07003709 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003710 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003711 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003712 ++pbi->num_tile_workers;
3713
3714 winterface->init(worker);
3715 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003716 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003717 "Tile decoder thread creation failed");
3718 }
3719 }
3720 }
3721
3722 // Reset tile decoding hook
3723 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003724 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003725 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003726 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003727 worker->data1 = &pbi->tile_worker_data[i];
3728 worker->data2 = &pbi->tile_worker_info[i];
3729 }
3730
3731 // Initialize thread frame counts.
3732 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3733 for (i = 0; i < num_workers; ++i) {
3734 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003735 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003736 }
3737 }
3738
3739 // Load tile data into tile_buffers
3740 get_tile_buffers(pbi, data, data_end, tile_buffers);
3741
3742 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3743 // Sort the buffers in this tile row based on size in descending order.
3744 qsort(&tile_buffers[tile_row][tile_cols_start],
3745 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
3746 compare_tile_buffers);
3747
3748 // Rearrange the tile buffers in this tile row such that per-tile group
3749 // the largest, and presumably the most difficult tile will be decoded in
3750 // the main thread. This should help minimize the number of instances
3751 // where the main thread is waiting for a worker to complete.
3752 {
3753 int group_start;
3754 for (group_start = tile_cols_start; group_start < tile_cols_end;
3755 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003756 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003757 const TileBufferDec largest = tile_buffers[tile_row][group_start];
3758 memmove(&tile_buffers[tile_row][group_start],
3759 &tile_buffers[tile_row][group_start + 1],
3760 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
3761 tile_buffers[tile_row][group_end - 1] = largest;
3762 }
3763 }
3764
3765 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
3766 // Launch workers for individual columns
3767 for (i = 0; i < num_workers && tile_col < tile_cols_end;
3768 ++i, ++tile_col) {
3769 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07003770 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003771 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
3772 TileInfo *const tile_info = (TileInfo *)worker->data2;
3773
3774 twd->pbi = pbi;
3775 twd->xd = pbi->mb;
3776 twd->xd.corrupted = 0;
3777 twd->xd.counts =
3778 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3779 ? &twd->counts
3780 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003781 av1_zero(twd->dqcoeff);
3782 av1_tile_init(tile_info, cm, tile_row, buf->col);
3783 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003784 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003785 &twd->bit_reader,
3786#if CONFIG_ANS && ANS_MAX_SYMBOLS
3787 1 << cm->ans_window_size_log2,
3788#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3789 pbi->decrypt_cb, pbi->decrypt_state);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003790 av1_init_macroblockd(cm, &twd->xd,
3791#if CONFIG_PVQ
3792 twd->pvq_ref_coeff,
3793#endif
3794 twd->dqcoeff);
3795#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003796 daala_dec_init(cm, &twd->xd.daala_dec, &twd->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003797#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003798#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003799 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
3800 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003801#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003802
3803 worker->had_error = 0;
3804 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
3805 winterface->execute(worker);
3806 } else {
3807 winterface->launch(worker);
3808 }
3809
3810#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3811 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
3812 final_worker = i;
3813 }
3814#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3815 }
3816
3817 // Sync all workers
3818 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003819 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003820 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07003821 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07003822 // info in cm. Additionally once the threads have been synced and an
3823 // error is detected, there's no point in continuing to decode tiles.
3824 pbi->mb.corrupted |= !winterface->sync(worker);
3825 }
3826 }
3827 }
3828
3829 // Accumulate thread frame counts.
3830 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3831 for (i = 0; i < num_workers; ++i) {
3832 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003833 av1_accumulate_frame_counts(&cm->counts, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003834 }
3835 }
3836
3837#if CONFIG_EXT_TILE
3838 // Return the end of the last tile buffer
3839 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3840#else
3841#if CONFIG_ANS
3842 return data_end;
3843#else
3844 assert(final_worker != -1);
3845 {
3846 TileWorkerData *const twd =
3847 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003848 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003849 }
3850#endif // CONFIG_ANS
3851#endif // CONFIG_EXT_TILE
3852}
3853
3854static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003855 AV1_COMMON *const cm = (AV1_COMMON *)data;
3856 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003857}
3858
Yaowu Xuf883b422016-08-30 14:01:10 -07003859static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3860 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003861 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003862 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003863 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003864 cm->bit_depth = AOM_BITS_8;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003865 }
3866
Yaowu Xuf883b422016-08-30 14:01:10 -07003867#if CONFIG_AOM_HIGHBITDEPTH
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003868 if (cm->bit_depth > AOM_BITS_8) {
3869 cm->use_highbitdepth = 1;
3870 } else {
Yaowu Xu345a22d2017-02-27 09:23:52 -08003871#if CONFIG_LOWBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003872 cm->use_highbitdepth = 0;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003873#else
3874 cm->use_highbitdepth = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003875#endif
3876 }
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003877#endif
3878
Yaowu Xuf883b422016-08-30 14:01:10 -07003879 cm->color_space = aom_rb_read_literal(rb, 3);
3880 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003881 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07003882 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003883 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003884 cm->subsampling_x = aom_rb_read_bit(rb);
3885 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003886 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003887 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003888 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07003889 if (aom_rb_read_bit(rb))
3890 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003891 "Reserved bit set");
3892 } else {
3893 cm->subsampling_y = cm->subsampling_x = 1;
3894 }
3895 } else {
3896 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
3897 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
3898 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
3899 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003900 if (aom_rb_read_bit(rb))
3901 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003902 "Reserved bit set");
3903 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003904 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003905 "4:4:4 color not supported in profile 0 or 2");
3906 }
3907 }
3908}
3909
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003910#if CONFIG_REFERENCE_BUFFER
3911void read_sequence_header(SequenceHeader *seq_params) {
3912 /* Placeholder for actually reading from the bitstream */
3913 seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
3914 seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
3915 seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
3916}
3917#endif
3918
Yaowu Xuf883b422016-08-30 14:01:10 -07003919static size_t read_uncompressed_header(AV1Decoder *pbi,
3920 struct aom_read_bit_buffer *rb) {
3921 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003922 MACROBLOCKD *const xd = &pbi->mb;
3923 BufferPool *const pool = cm->buffer_pool;
3924 RefCntBuffer *const frame_bufs = pool->frame_bufs;
3925 int i, mask, ref_index = 0;
3926 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003927
3928#if CONFIG_REFERENCE_BUFFER
3929 /* TODO: Move outside frame loop or inside key-frame branch */
3930 read_sequence_header(&pbi->seq_params);
3931#endif
3932
Yaowu Xuc27fc142016-08-22 16:08:15 -07003933 cm->last_frame_type = cm->frame_type;
3934 cm->last_intra_only = cm->intra_only;
3935
3936#if CONFIG_EXT_REFS
3937 // NOTE: By default all coded frames to be used as a reference
3938 cm->is_reference_frame = 1;
3939#endif // CONFIG_EXT_REFS
3940
Yaowu Xuf883b422016-08-30 14:01:10 -07003941 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
3942 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003943 "Invalid frame marker");
3944
Yaowu Xuf883b422016-08-30 14:01:10 -07003945 cm->profile = av1_read_profile(rb);
3946#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003947 if (cm->profile >= MAX_PROFILES)
Yaowu Xuf883b422016-08-30 14:01:10 -07003948 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003949 "Unsupported bitstream profile");
3950#else
3951 if (cm->profile >= PROFILE_2)
Yaowu Xuf883b422016-08-30 14:01:10 -07003952 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003953 "Unsupported bitstream profile");
3954#endif
3955
Yaowu Xuf883b422016-08-30 14:01:10 -07003956 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003957
3958 if (cm->show_existing_frame) {
Yaowu Xu415ba932016-12-27 11:17:32 -08003959 // Show an existing frame directly.
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003960 const int existing_frame_idx = aom_rb_read_literal(rb, 3);
3961 const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
Yaowu Xu415ba932016-12-27 11:17:32 -08003962#if CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003963 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003964 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3965 int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003966 /* Compare display_frame_id with ref_frame_id and check valid for
3967 * referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003968 if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
3969 cm->valid_for_referencing[existing_frame_idx] == 0)
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003970 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3971 "Reference buffer frame ID mismatch");
3972 }
3973#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003974 lock_buffer_pool(pool);
3975 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
3976 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07003977 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003978 "Buffer %d does not contain a decoded frame",
3979 frame_to_show);
3980 }
3981 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
3982 unlock_buffer_pool(pool);
3983
3984 cm->lf.filter_level = 0;
3985 cm->show_frame = 1;
3986 pbi->refresh_frame_flags = 0;
3987
3988 if (cm->frame_parallel_decode) {
3989 for (i = 0; i < REF_FRAMES; ++i)
3990 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
3991 }
3992
3993 return 0;
3994 }
3995
Yaowu Xuf883b422016-08-30 14:01:10 -07003996 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
3997 cm->show_frame = aom_rb_read_bit(rb);
3998 cm->error_resilient_mode = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003999#if CONFIG_REFERENCE_BUFFER
4000 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004001 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4002 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4003 int prev_frame_id = 0;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004004 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004005 prev_frame_id = cm->current_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004006 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004007 cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004008
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 int diff_frame_id;
4011 if (cm->current_frame_id > prev_frame_id) {
4012 diff_frame_id = cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004013 } else {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004014 diff_frame_id =
4015 (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004016 }
4017 /* Check current_frame_id for conformance */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004018 if (prev_frame_id == cm->current_frame_id ||
4019 diff_frame_id >= (1 << (frame_id_length - 1))) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004020 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4021 "Invalid value of current_frame_id");
4022 }
4023 }
4024 /* Check if some frames need to be marked as not valid for referencing */
4025 for (i = 0; i < REF_FRAMES; i++) {
4026 if (cm->frame_type == KEY_FRAME) {
4027 cm->valid_for_referencing[i] = 0;
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004028 } else if (cm->current_frame_id - (1 << diff_len) > 0) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004029 if (cm->ref_frame_id[i] > cm->current_frame_id ||
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004030 cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004031 cm->valid_for_referencing[i] = 0;
4032 } else {
4033 if (cm->ref_frame_id[i] > cm->current_frame_id &&
4034 cm->ref_frame_id[i] <
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004035 (1 << frame_id_length) + cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004036 cm->valid_for_referencing[i] = 0;
4037 }
4038 }
4039 }
4040#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004041 if (cm->frame_type == KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004042 if (!av1_read_sync_code(rb))
4043 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004044 "Invalid frame sync code");
4045
4046 read_bitdepth_colorspace_sampling(cm, rb);
4047 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
4048
4049 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4050 cm->frame_refs[i].idx = INVALID_IDX;
4051 cm->frame_refs[i].buf = NULL;
4052 }
4053
4054 setup_frame_size(cm, rb);
4055 if (pbi->need_resync) {
4056 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4057 pbi->need_resync = 0;
4058 }
Alex Converseeb780e72016-12-13 12:46:41 -08004059#if CONFIG_ANS && ANS_MAX_SYMBOLS
4060 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4061#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
Urvang Joshib100db72016-10-12 16:28:56 -07004062#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004063 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004064#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004065 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004066 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004067#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004068 if (cm->intra_only) 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 if (cm->error_resilient_mode) {
4071 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
4072 } else {
4073 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004074 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004075 ? RESET_FRAME_CONTEXT_ALL
4076 : RESET_FRAME_CONTEXT_CURRENT;
4077 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004078 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004079 ? RESET_FRAME_CONTEXT_CURRENT
4080 : RESET_FRAME_CONTEXT_NONE;
4081 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07004082 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004083 ? RESET_FRAME_CONTEXT_ALL
4084 : RESET_FRAME_CONTEXT_CURRENT;
4085 }
4086 }
4087
4088 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004089 if (!av1_read_sync_code(rb))
4090 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004091 "Invalid frame sync code");
4092
4093 read_bitdepth_colorspace_sampling(cm, rb);
4094
Yaowu Xuf883b422016-08-30 14:01:10 -07004095 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004096 setup_frame_size(cm, rb);
4097 if (pbi->need_resync) {
4098 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4099 pbi->need_resync = 0;
4100 }
Alex Converseeb780e72016-12-13 12:46:41 -08004101#if CONFIG_ANS && ANS_MAX_SYMBOLS
4102 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4103#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004104 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Yaowu Xuf883b422016-08-30 14:01:10 -07004105 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004106
4107#if CONFIG_EXT_REFS
4108 if (!pbi->refresh_frame_flags) {
4109 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
4110 // will not be used as a reference
4111 cm->is_reference_frame = 0;
4112 }
4113#endif // CONFIG_EXT_REFS
4114
4115 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004116 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004117 const int idx = cm->ref_frame_map[ref];
4118 RefBuffer *const ref_frame = &cm->frame_refs[i];
4119 ref_frame->idx = idx;
4120 ref_frame->buf = &frame_bufs[idx].buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07004121 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004122#if CONFIG_REFERENCE_BUFFER
4123 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004124 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4125 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4126 int delta_frame_id_minus1 = aom_rb_read_literal(rb, diff_len);
4127 int ref_frame_id =
4128 ((cm->current_frame_id - (delta_frame_id_minus1 + 1) +
4129 (1 << frame_id_length)) %
4130 (1 << frame_id_length));
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004131 /* Compare values derived from delta_frame_id_minus1 and
4132 * refresh_frame_flags. Also, check valid for referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004133 if (ref_frame_id != cm->ref_frame_id[ref] ||
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004134 cm->valid_for_referencing[ref] == 0)
4135 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4136 "Reference buffer frame ID mismatch");
4137 }
4138#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004139 }
4140
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004141#if CONFIG_FRAME_SIZE
4142 if (cm->error_resilient_mode == 0) {
4143 setup_frame_size_with_refs(cm, rb);
4144 } else {
4145 setup_frame_size(cm, rb);
4146 }
4147#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004148 setup_frame_size_with_refs(cm, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004149#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004150
Yaowu Xuf883b422016-08-30 14:01:10 -07004151 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Angie Chiang5678ad92016-11-21 09:38:40 -08004152 cm->interp_filter = read_frame_interp_filter(rb);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004153#if CONFIG_TEMPMV_SIGNALING
4154 if (!cm->error_resilient_mode) {
4155 cm->use_prev_frame_mvs = aom_rb_read_bit(rb);
4156 }
4157#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004158 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4159 RefBuffer *const ref_buf = &cm->frame_refs[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07004160#if CONFIG_AOM_HIGHBITDEPTH
4161 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004162 &ref_buf->sf, ref_buf->buf->y_crop_width,
4163 ref_buf->buf->y_crop_height, cm->width, cm->height,
4164 cm->use_highbitdepth);
4165#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004166 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#endif
4170 }
4171 }
4172 }
Fangwen Fu8d164de2016-12-14 13:40:54 -08004173#if CONFIG_TEMPMV_SIGNALING
4174 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
4175#endif
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004176
4177#if CONFIG_REFERENCE_BUFFER
4178 if (pbi->seq_params.frame_id_numbers_present_flag) {
4179 /* If bitmask is set, update reference frame id values and
4180 mark frames as valid for reference */
4181 int refresh_frame_flags =
4182 cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
4183 for (i = 0; i < REF_FRAMES; i++) {
4184 if ((refresh_frame_flags >> i) & 1) {
4185 cm->ref_frame_id[i] = cm->current_frame_id;
4186 cm->valid_for_referencing[i] = 1;
4187 }
4188 }
4189 }
4190#endif
4191
Yaowu Xuf883b422016-08-30 14:01:10 -07004192#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004193 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
4194#endif
4195 get_frame_new_buffer(cm)->color_space = cm->color_space;
4196 get_frame_new_buffer(cm)->color_range = cm->color_range;
4197 get_frame_new_buffer(cm)->render_width = cm->render_width;
4198 get_frame_new_buffer(cm)->render_height = cm->render_height;
4199
4200 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004201 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004202 "Keyframe / intra-only frame required to reset decoder"
4203 " state");
4204 }
4205
4206 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004207 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004208 ? REFRESH_FRAME_CONTEXT_FORWARD
4209 : REFRESH_FRAME_CONTEXT_BACKWARD;
4210 } else {
4211 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
4212 }
4213
Yaowu Xuf883b422016-08-30 14:01:10 -07004214 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07004215 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07004216 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004217
4218 // Generate next_ref_frame_map.
4219 lock_buffer_pool(pool);
4220 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
4221 if (mask & 1) {
4222 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
4223 ++frame_bufs[cm->new_fb_idx].ref_count;
4224 } else {
4225 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4226 }
4227 // Current thread holds the reference frame.
4228 if (cm->ref_frame_map[ref_index] >= 0)
4229 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4230 ++ref_index;
4231 }
4232
4233 for (; ref_index < REF_FRAMES; ++ref_index) {
4234 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4235
4236 // Current thread holds the reference frame.
4237 if (cm->ref_frame_map[ref_index] >= 0)
4238 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4239 }
4240 unlock_buffer_pool(pool);
4241 pbi->hold_ref_buf = 1;
4242
4243 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07004244 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004245
4246#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07004247 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004248#else
4249 set_sb_size(cm, BLOCK_64X64);
4250#endif // CONFIG_EXT_PARTITION
4251
4252 setup_loopfilter(cm, rb);
Jean-Marc Valin01435132017-02-18 14:12:53 -05004253#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01004254 setup_cdef(cm, rb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004255#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004256#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004257 decode_restoration_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004258#endif // CONFIG_LOOP_RESTORATION
4259 setup_quantization(cm, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004260#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004261 xd->bd = (int)cm->bit_depth;
4262#endif
4263
hui su0d103572017-03-01 17:58:01 -08004264#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuf883b422016-08-30 14:01:10 -07004265 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004266 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4267 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4268 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4269 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
4270 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4271 }
hui su0d103572017-03-01 17:58:01 -08004272#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004273
4274 setup_segmentation(cm, rb);
4275
Arild Fuldseth07441162016-08-15 15:07:52 +02004276#if CONFIG_DELTA_Q
4277 {
4278 struct segmentation *const seg = &cm->seg;
4279 int segment_quantizer_active = 0;
4280 for (i = 0; i < MAX_SEGMENTS; i++) {
4281 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
4282 segment_quantizer_active = 1;
4283 }
4284 }
4285
Thomas Daviesf6936102016-09-05 16:51:31 +01004286 cm->delta_q_res = 1;
Arild Fuldseth07441162016-08-15 15:07:52 +02004287 if (segment_quantizer_active == 0) {
4288 cm->delta_q_present_flag = aom_rb_read_bit(rb);
4289 } else {
4290 cm->delta_q_present_flag = 0;
4291 }
4292 if (cm->delta_q_present_flag) {
4293 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01004294 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Arild Fuldseth07441162016-08-15 15:07:52 +02004295 }
4296 }
4297#endif
4298
Urvang Joshi454280d2016-10-14 16:51:44 -07004299 for (i = 0; i < MAX_SEGMENTS; ++i) {
4300 const int qindex = cm->seg.enabled
4301 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
4302 : cm->base_qindex;
4303 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
4304 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4305 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004306 }
4307
4308 setup_segmentation_dequant(cm);
Yue Cheneeacc4c2017-01-17 17:29:17 -08004309 cm->tx_mode = read_tx_mode(cm, xd, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004310 cm->reference_mode = read_frame_reference_mode(cm, rb);
4311
Sarah Parkere68a3e42017-02-16 14:03:24 -08004312#if CONFIG_EXT_TX
4313 cm->reduced_tx_set_used = aom_rb_read_bit(rb);
4314#endif // CONFIG_EXT_TX
4315
Yaowu Xuc27fc142016-08-22 16:08:15 -07004316 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004317 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004318
4319 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07004320 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004321 "Invalid header size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004322 return sz;
4323}
4324
4325#if CONFIG_EXT_TX
Thomas9ac55082016-09-23 18:04:17 +01004326#if !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuf883b422016-08-30 14:01:10 -07004327static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004328 int i, j, k;
4329 int s;
4330 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004331 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004332 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4333 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004334 for (j = 0; j < num_ext_tx_set[ext_tx_set_type_inter[s]] - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004335 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004336 }
4337 }
4338 }
4339
4340 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004341 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004342 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4343 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
4344 for (j = 0; j < INTRA_MODES; ++j)
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004345 for (k = 0; k < num_ext_tx_set[ext_tx_set_type_intra[s]] - 1; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004346 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
4347 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004348 }
4349 }
4350 }
4351}
Thomas9ac55082016-09-23 18:04:17 +01004352#endif // !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004353#else
4354
Yaowu Xuc27fc142016-08-22 16:08:15 -07004355#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004356#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07004357static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004358 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -07004359 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004360 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
Jingning Hanfeb517c2016-12-21 16:02:07 -08004361 for (j = TX_8X8; j < TX_SIZES; ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004362 av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004363 }
4364 }
4365 }
4366}
4367#endif // CONFIG_SUPERTX
4368
4369#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +00004370static void read_global_motion_params(WarpedMotionParams *params,
Yaowu Xuf883b422016-08-30 14:01:10 -07004371 aom_prob *probs, aom_reader *r) {
David Barkercf3d0b02016-11-10 10:14:49 +00004372 TransformationType type =
Michael Bebenita6048d052016-08-25 14:40:54 -07004373 aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004374 set_default_gmparams(params);
David Barkercf3d0b02016-11-10 10:14:49 +00004375 params->wmtype = type;
4376 switch (type) {
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004377 case HOMOGRAPHY:
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004378 case HORTRAPEZOID:
4379 case VERTRAPEZOID:
4380 if (type != HORTRAPEZOID)
4381 params->wmmat[6] =
4382 aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
4383 GM_ROW3HOMO_DECODE_FACTOR;
4384 if (type != VERTRAPEZOID)
4385 params->wmmat[7] =
4386 aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
4387 GM_ROW3HOMO_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004388 case AFFINE:
4389 case ROTZOOM:
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004390 params->wmmat[2] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004391 GM_ALPHA_DECODE_FACTOR +
David Barkercf3d0b02016-11-10 10:14:49 +00004392 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004393 if (type != VERTRAPEZOID)
4394 params->wmmat[3] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004395 GM_ALPHA_DECODE_FACTOR;
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004396 if (type >= AFFINE) {
4397 if (type != HORTRAPEZOID)
4398 params->wmmat[4] =
4399 aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4400 GM_ALPHA_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004401 params->wmmat[5] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4402 GM_ALPHA_DECODE_FACTOR +
4403 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004404 } else {
David Barkercf3d0b02016-11-10 10:14:49 +00004405 params->wmmat[4] = -params->wmmat[3];
4406 params->wmmat[5] = params->wmmat[2];
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004407 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004408 // fallthrough intended
David Barkercf3d0b02016-11-10 10:14:49 +00004409 case TRANSLATION:
4410 params->wmmat[0] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4411 GM_TRANS_DECODE_FACTOR;
4412 params->wmmat[1] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4413 GM_TRANS_DECODE_FACTOR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004414 break;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004415 case IDENTITY: break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004416 default: assert(0);
4417 }
4418}
4419
Yaowu Xuf883b422016-08-30 14:01:10 -07004420static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004421 int frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004422 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
4423 read_global_motion_params(&cm->global_motion[frame],
4424 cm->fc->global_motion_types_prob, r);
Sarah Parkere5299862016-08-16 14:57:37 -07004425 /*
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004426 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4427 frame, cm->current_video_frame, cm->show_frame,
David Barkercf3d0b02016-11-10 10:14:49 +00004428 cm->global_motion[frame].wmmat[0],
4429 cm->global_motion[frame].wmmat[1],
4430 cm->global_motion[frame].wmmat[2],
4431 cm->global_motion[frame].wmmat[3]);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004432 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004433 }
4434}
4435#endif // CONFIG_GLOBAL_MOTION
4436
Yaowu Xuf883b422016-08-30 14:01:10 -07004437static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004438 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004439 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004440#if CONFIG_SUPERTX
4441 MACROBLOCKD *const xd = &pbi->mb;
4442#endif
4443 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07004444 aom_reader r;
Yaowu Xu8af861b2016-11-01 12:12:11 -07004445 int k, i;
Yaowu Xud0af64f2016-11-17 12:50:47 -08004446#if !CONFIG_EC_ADAPT || CONFIG_EXT_INTRA
Yaowu Xu8af861b2016-11-01 12:12:11 -07004447 int j;
4448#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004449
Alex Converse2cdf0d82016-12-13 13:53:09 -08004450#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08004451 r.window_size = 1 << cm->ans_window_size_log2;
Alex Converse2cdf0d82016-12-13 13:53:09 -08004452#endif
Alex Converse346440b2017-01-03 13:47:37 -08004453 if (aom_reader_init(&r, data, partition_size, pbi->decrypt_cb,
4454 pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07004455 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004456 "Failed to allocate bool decoder 0");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004457
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004458#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004459 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4460 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4461 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4462 av1_alloc_restoration_buffers(cm);
4463 decode_restoration(cm, &r);
4464 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004465#endif
4466
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004467#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -07004468 if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004469#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004470
Yushin Cho77bba8d2016-11-04 16:36:56 -07004471#if !CONFIG_PVQ
Alex Conversea9598cd2017-02-03 14:18:05 -08004472#if !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004473 read_coef_probs(fc, cm->tx_mode, &r);
Thomas Daviesfc1598a2017-01-13 17:07:25 +00004474#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004475
4476#if CONFIG_VAR_TX
4477 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004478 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
Yushin Cho77bba8d2016-11-04 16:36:56 -07004479#endif // CONFIG_VAR_TX
4480#endif // !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004481 for (k = 0; k < SKIP_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004482 av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004483
Thomas Daviesd6ee8a82017-03-02 14:42:50 +00004484#if CONFIG_DELTA_Q && !CONFIG_EC_ADAPT
Thomas Davies665cd702017-03-02 10:20:30 +00004485 for (k = 0; k < DELTA_Q_PROBS; ++k)
Thomas Daviesf6936102016-09-05 16:51:31 +01004486 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
4487#endif
4488
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004489#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004490 if (cm->seg.enabled && cm->seg.update_map) {
4491 if (cm->seg.temporal_update) {
4492 for (k = 0; k < PREDICTION_PROBS; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004493 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004494 }
4495 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004496 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004497 }
4498
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004499 for (j = 0; j < INTRA_MODES; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004500 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004501 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004502 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004503
4504#if CONFIG_EXT_PARTITION_TYPES
Alex Converse4e18d402017-03-14 15:36:38 -07004505 for (j = 0; j < PARTITION_PLOFFSET; ++j)
4506 for (i = 0; i < PARTITION_TYPES - 1; ++i)
4507 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
4508 for (; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004509 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004510 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004511#else
Alex Converse55c6bde2017-01-12 15:55:31 -08004512 for (j = 0; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004513 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004514 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004515#endif // CONFIG_EXT_PARTITION_TYPES
hui su9aa97492017-01-26 16:46:01 -08004516
Alex Converse55c6bde2017-01-12 15:55:31 -08004517#if CONFIG_UNPOISON_PARTITION_CTX
4518 for (; j < PARTITION_CONTEXTS_PRIMARY + PARTITION_BLOCK_SIZES; ++j)
4519 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_VERT], ACCT_STR);
4520 for (; j < PARTITION_CONTEXTS_PRIMARY + 2 * PARTITION_BLOCK_SIZES; ++j)
4521 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_HORZ], ACCT_STR);
4522#endif // CONFIG_UNPOISON_PARTITION_CTX
hui su9aa97492017-01-26 16:46:01 -08004523#endif // !CONFIG_EC_ADAPT
4524
4525#if CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004526 for (i = 0; i < INTRA_FILTERS + 1; ++i)
4527 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004528 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
hui su9aa97492017-01-26 16:46:01 -08004529#endif // CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004530
4531 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004532 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Nathan E. Egge10ba2be2016-11-16 09:44:26 -05004533#if CONFIG_EC_MULTISYMBOL
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00004534 av1_copy(cm->fc->kf_y_cdf, av1_kf_y_mode_cdf);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004535#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004536#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004537 for (k = 0; k < INTRA_MODES; k++)
Thomas Davies6519beb2016-10-19 14:46:07 +01004538 for (j = 0; j < INTRA_MODES; j++)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004539 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004540 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004541#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004542 } else {
4543#if !CONFIG_REF_MV
4544 nmv_context *const nmvc = &fc->nmvc;
4545#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004546 read_inter_mode_probs(fc, &r);
4547
4548#if CONFIG_EXT_INTER
4549 read_inter_compound_mode_probs(fc, &r);
4550 if (cm->reference_mode != COMPOUND_REFERENCE) {
4551 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4552 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004553 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004554 }
4555 }
4556 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4557 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004558 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004559 }
4560 for (i = 0; i < BLOCK_SIZES; i++) {
4561 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004562 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004563 }
4564 }
4565 }
4566 if (cm->reference_mode != SINGLE_REFERENCE) {
4567 for (i = 0; i < BLOCK_SIZES; i++) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08004568 for (j = 0; j < COMPOUND_TYPES - 1; j++) {
4569 av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004570 }
4571 }
4572 }
4573#endif // CONFIG_EXT_INTER
4574
Yue Chencb60b182016-10-13 15:18:22 -07004575#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004576 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
Yue Chencb60b182016-10-13 15:18:22 -07004577 for (j = 0; j < MOTION_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004578 av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004579 }
Yue Chencb60b182016-10-13 15:18:22 -07004580#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004581
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004582#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004583 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004584#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004585
4586 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004587 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004588
4589 if (cm->reference_mode != SINGLE_REFERENCE)
4590 setup_compound_reference_mode(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004591 read_frame_reference_mode_probs(cm, &r);
4592
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004593#if !CONFIG_EC_ADAPT
Nathan E. Egge5710c722016-09-08 10:01:16 -04004594 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004595 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004596 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
Nathan E. Egge5710c722016-09-08 10:01:16 -04004597 }
Thomas9ac55082016-09-23 18:04:17 +01004598#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004599
4600#if CONFIG_REF_MV
4601 for (i = 0; i < NMV_CONTEXTS; ++i)
4602 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
4603#else
4604 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
4605#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004606#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004607 read_ext_tx_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004608#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004609#if CONFIG_SUPERTX
4610 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
4611#endif
4612#if CONFIG_GLOBAL_MOTION
4613 read_global_motion(cm, &r);
Thomas Davies6519beb2016-10-19 14:46:07 +01004614#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004615 }
Thomas Davies028b57f2017-02-22 16:42:11 +00004616#if CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Thomas Davies87aeeb82017-02-17 00:19:40 +00004617#if CONFIG_NEW_TOKENSET
4618 av1_coef_head_cdfs(fc);
4619#endif
4620 /* Make tail distribution from head */
Thomas Davies6519beb2016-10-19 14:46:07 +01004621 av1_coef_pareto_cdfs(fc);
David Barker599dfd02016-11-10 13:20:12 +00004622#if CONFIG_REF_MV
4623 for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
4624#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004625 av1_set_mv_cdfs(&fc->nmvc);
David Barker599dfd02016-11-10 13:20:12 +00004626#endif
Thomas Davies6519beb2016-10-19 14:46:07 +01004627 av1_set_mode_cdfs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00004628#endif // CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004629
Yaowu Xuf883b422016-08-30 14:01:10 -07004630 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004631}
4632
4633#ifdef NDEBUG
4634#define debug_check_frame_counts(cm) (void)0
4635#else // !NDEBUG
4636// Counts should only be incremented when frame_parallel_decoding_mode and
4637// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07004638static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004639 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07004640 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004641 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
4642 cm->error_resilient_mode);
4643 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
4644 sizeof(cm->counts.y_mode)));
4645 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
4646 sizeof(cm->counts.uv_mode)));
4647 assert(!memcmp(cm->counts.partition, zero_counts.partition,
4648 sizeof(cm->counts.partition)));
4649 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
4650 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
4651 sizeof(cm->counts.eob_branch)));
Thomas Daviesab780672017-02-01 12:07:29 +00004652#if CONFIG_EC_MULTISYMBOL
4653 assert(!memcmp(cm->counts.blockz_count, zero_counts.blockz_count,
4654 sizeof(cm->counts.blockz_count)));
4655#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004656 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
4657 sizeof(cm->counts.switchable_interp)));
4658 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
4659 sizeof(cm->counts.inter_mode)));
4660#if CONFIG_EXT_INTER
4661 assert(!memcmp(cm->counts.inter_compound_mode,
4662 zero_counts.inter_compound_mode,
4663 sizeof(cm->counts.inter_compound_mode)));
4664 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
4665 sizeof(cm->counts.interintra)));
4666 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
4667 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08004668 assert(!memcmp(cm->counts.compound_interinter,
4669 zero_counts.compound_interinter,
4670 sizeof(cm->counts.compound_interinter)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004671#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07004672#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
4673 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
4674 sizeof(cm->counts.motion_mode)));
4675#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004676 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
4677 sizeof(cm->counts.intra_inter)));
4678 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
4679 sizeof(cm->counts.comp_inter)));
4680 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
4681 sizeof(cm->counts.single_ref)));
4682 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
4683 sizeof(cm->counts.comp_ref)));
4684#if CONFIG_EXT_REFS
4685 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
4686 sizeof(cm->counts.comp_bwdref)));
4687#endif // CONFIG_EXT_REFS
4688 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
4689 sizeof(cm->counts.tx_size)));
4690 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
4691#if CONFIG_REF_MV
4692 assert(
4693 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
4694 assert(
4695 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
4696#else
4697 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
4698#endif
4699 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
4700 sizeof(cm->counts.inter_ext_tx)));
4701 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
4702 sizeof(cm->counts.intra_ext_tx)));
4703}
4704#endif // NDEBUG
4705
Yaowu Xuf883b422016-08-30 14:01:10 -07004706static struct aom_read_bit_buffer *init_read_bit_buffer(
4707 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
4708 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004709 rb->bit_offset = 0;
4710 rb->error_handler = error_handler;
4711 rb->error_handler_data = &pbi->common;
4712 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004713 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004714 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
4715 rb->bit_buffer = clear_data;
4716 rb->bit_buffer_end = clear_data + n;
4717 } else {
4718 rb->bit_buffer = data;
4719 rb->bit_buffer_end = data_end;
4720 }
4721 return rb;
4722}
4723
4724//------------------------------------------------------------------------------
4725
Yaowu Xuf883b422016-08-30 14:01:10 -07004726int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
4727 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
4728 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
4729 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004730}
4731
Yaowu Xuf883b422016-08-30 14:01:10 -07004732void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
4733 int *height) {
4734 *width = aom_rb_read_literal(rb, 16) + 1;
4735 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004736}
4737
Yaowu Xuf883b422016-08-30 14:01:10 -07004738BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
4739 int profile = aom_rb_read_bit(rb);
4740 profile |= aom_rb_read_bit(rb) << 1;
4741 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004742 return (BITSTREAM_PROFILE)profile;
4743}
4744
Thomas Davies028b57f2017-02-22 16:42:11 +00004745#if CONFIG_EC_ADAPT
4746static void make_update_tile_list_dec(AV1Decoder *pbi, const int tile_rows,
4747 const int tile_cols,
4748 FRAME_CONTEXT *ec_ctxs[]) {
4749 int i;
4750 for (i = 0; i < tile_rows * tile_cols; ++i)
4751 ec_ctxs[i] = &pbi->tile_data[i].tctx;
4752}
4753#endif
4754
Yaowu Xuf883b422016-08-30 14:01:10 -07004755void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
4756 const uint8_t *data_end, const uint8_t **p_data_end) {
4757 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004758 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07004759 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004760 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004761 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004762 size_t first_partition_size;
4763 YV12_BUFFER_CONFIG *new_fb;
4764
4765#if CONFIG_BITSTREAM_DEBUG
4766 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
4767#endif
4768
4769 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004770 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Thomas Davies72712e62016-11-09 12:17:51 +00004771#if CONFIG_TILE_GROUPS
4772 pbi->first_partition_size = first_partition_size;
4773 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
4774#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004775 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004776 xd->cur_buf = new_fb;
4777#if CONFIG_GLOBAL_MOTION
4778 xd->global_motion = cm->global_motion;
4779#endif // CONFIG_GLOBAL_MOTION
4780
4781 if (!first_partition_size) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004782 // showing a frame directly
4783 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004784 return;
4785 }
4786
Yaowu Xuf883b422016-08-30 14:01:10 -07004787 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004788 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07004789 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004790 "Truncated packet or corrupt header length");
4791
Jingning Han24e0a182016-11-20 22:34:12 -08004792#if CONFIG_REF_MV
Dengca8d24d2016-10-17 14:06:35 +08004793 cm->setup_mi(cm);
4794#endif
4795
Fangwen Fu8d164de2016-12-14 13:40:54 -08004796#if CONFIG_TEMPMV_SIGNALING
4797 if (cm->use_prev_frame_mvs) {
4798 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4799 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4800 assert(!cm->error_resilient_mode &&
4801 cm->width == last_fb_ref_buf->buf->y_width &&
4802 cm->height == last_fb_ref_buf->buf->y_height &&
4803 !cm->prev_frame->intra_only);
4804 }
4805#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004806 cm->use_prev_frame_mvs =
4807 !cm->error_resilient_mode && cm->width == cm->last_width &&
4808 cm->height == cm->last_height && !cm->last_intra_only &&
4809 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004810#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004811#if CONFIG_EXT_REFS
4812 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
4813 // show_exisiting_frame=1, nor can it take a frame not used as
4814 // a reference, it is probable that by the time it is being
4815 // referred to, the frame buffer it originally points to may
4816 // already get expired and have been reassigned to the current
4817 // newly coded frame. Hence, we need to check whether this is
4818 // the case, and if yes, we have 2 choices:
4819 // (1) Simply disable the use of previous frame mvs; or
4820 // (2) Have cm->prev_frame point to one reference frame buffer,
4821 // e.g. LAST_FRAME.
4822 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
4823 // Reassign the LAST_FRAME buffer to cm->prev_frame.
4824 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4825 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4826 }
4827#endif // CONFIG_EXT_REFS
4828
Yaowu Xuf883b422016-08-30 14:01:10 -07004829 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004830
4831 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
4832 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07004833 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004834 "Uninitialized entropy context.");
4835
Yaowu Xuf883b422016-08-30 14:01:10 -07004836 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004837
4838 xd->corrupted = 0;
4839 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
4840 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07004841 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004842 "Decode failed. Frame data header is corrupted.");
4843
4844 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004845 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004846 }
4847
4848 // If encoded in frame parallel mode, frame context is ready after decoding
4849 // the frame header.
4850 if (cm->frame_parallel_decode &&
4851 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004852 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004853 FrameWorkerData *const frame_worker_data = worker->data1;
4854 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
4855 context_updated = 1;
4856 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4857 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004858 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004859 pbi->cur_buf->row = -1;
4860 pbi->cur_buf->col = -1;
4861 frame_worker_data->frame_context_ready = 1;
4862 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07004863 av1_frameworker_signal_stats(worker);
4864 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004865 }
4866
hui su0d103572017-03-01 17:58:01 -08004867#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07004868 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004869 cm->coef_probs_update_idx = 0;
hui su0d103572017-03-01 17:58:01 -08004870#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004871
4872 if (pbi->max_threads > 1
4873#if CONFIG_EXT_TILE
4874 && pbi->dec_tile_col < 0 // Decoding all columns
4875#endif // CONFIG_EXT_TILE
4876 && cm->tile_cols > 1) {
4877 // Multi-threaded tile decoder
4878 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
4879 if (!xd->corrupted) {
4880 if (!cm->skip_loop_filter) {
4881 // If multiple threads are used to decode tiles, then we use those
4882 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07004883 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
4884 0, 0, pbi->tile_workers, pbi->num_tile_workers,
4885 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004886 }
4887 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004888 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004889 "Decode failed. Frame data is corrupted.");
4890 }
4891 } else {
4892 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
4893 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004894
Jean-Marc Valin01435132017-02-18 14:12:53 -05004895#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01004896 if ((cm->dering_level || cm->clpf_strength_u || cm->clpf_strength_v) &&
4897 !cm->skip_loop_filter) {
4898 av1_cdef_frame(&pbi->cur_buf->buf, cm, &pbi->mb, cm->dering_level,
4899 cm->clpf_strength_u, cm->clpf_strength_v);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004900 }
Debargha Mukherjee00c54332017-03-03 15:44:17 -08004901#endif // CONFIG_CDEF
4902
4903#if CONFIG_LOOP_RESTORATION
4904 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4905 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4906 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4907 av1_loop_restoration_frame(new_fb, cm, cm->rst_info, 7, 0, NULL);
4908 }
4909#endif // CONFIG_LOOP_RESTORATION
Thomas Daedef56859f2016-04-19 16:57:24 -07004910
Yaowu Xuc27fc142016-08-22 16:08:15 -07004911 if (!xd->corrupted) {
4912 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Thomas Davies028b57f2017-02-22 16:42:11 +00004913#if CONFIG_EC_ADAPT
4914 FRAME_CONTEXT *tile_ctxs[MAX_TILE_ROWS * MAX_TILE_COLS];
4915 make_update_tile_list_dec(pbi, cm->tile_rows, cm->tile_cols, tile_ctxs);
4916#endif
4917
hui su0d103572017-03-01 17:58:01 -08004918#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004919 cm->partial_prob_update = 0;
hui su0d103572017-03-01 17:58:01 -08004920#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07004921 av1_adapt_coef_probs(cm);
4922 av1_adapt_intra_frame_probs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00004923#if CONFIG_EC_ADAPT
4924 av1_average_tile_coef_cdfs(pbi->common.fc, tile_ctxs,
4925 cm->tile_rows * cm->tile_cols);
4926 av1_average_tile_intra_cdfs(pbi->common.fc, tile_ctxs,
4927 cm->tile_rows * cm->tile_cols);
4928#endif
hui suff0da2b2017-03-07 15:51:37 -08004929#if CONFIG_ADAPT_SCAN
4930 av1_adapt_scan_order(cm);
4931#endif // CONFIG_ADAPT_SCAN
Yaowu Xuc27fc142016-08-22 16:08:15 -07004932
4933 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004934 av1_adapt_inter_frame_probs(cm);
4935 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Thomas Davies028b57f2017-02-22 16:42:11 +00004936#if CONFIG_EC_ADAPT
4937 av1_average_tile_inter_cdfs(&pbi->common, pbi->common.fc, tile_ctxs,
4938 cm->tile_rows * cm->tile_cols);
4939 av1_average_tile_mv_cdfs(pbi->common.fc, tile_ctxs,
4940 cm->tile_rows * cm->tile_cols);
4941#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004942 }
4943 } else {
4944 debug_check_frame_counts(cm);
4945 }
4946 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004947 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004948 "Decode failed. Frame data is corrupted.");
4949 }
4950
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05004951#if CONFIG_INSPECTION
4952 if (pbi->inspect_cb != NULL) {
4953 (*pbi->inspect_cb)(pbi, pbi->inspect_ctx);
4954 }
4955#endif
4956
Yaowu Xuc27fc142016-08-22 16:08:15 -07004957 // Non frame parallel update frame context here.
4958 if (!cm->error_resilient_mode && !context_updated)
4959 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4960}