blob: a89aba21bb2e0ec325bbd130c1a23808fa011269 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <assert.h>
13#include <stdlib.h> // qsort()
14
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "./aom_config.h"
16#include "./aom_dsp_rtcd.h"
17#include "./aom_scale_rtcd.h"
Jingning Han1aab8182016-06-03 11:09:06 -070018#include "./av1_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070019
Steinar Midtskogen2fd70ee2016-09-02 10:02:30 +020020#include "aom/aom_codec.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070021#include "aom_dsp/aom_dsp_common.h"
Jingning Han1aab8182016-06-03 11:09:06 -070022#include "aom_dsp/bitreader.h"
23#include "aom_dsp/bitreader_buffer.h"
Debargha Mukherjee47748b52017-03-24 12:20:49 -070024#include "aom_dsp/binary_codes_reader.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070025#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070026#include "aom_ports/mem.h"
27#include "aom_ports/mem_ops.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070028#include "aom_scale/aom_scale.h"
29#include "aom_util/aom_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070030
Fergus Simpson350a9b72017-04-17 15:08:45 -070031#if CONFIG_BITSTREAM_DEBUG
32#include "aom_util/debug_util.h"
33#endif // CONFIG_BITSTREAM_DEBUG
34
Yaowu Xuc27fc142016-08-22 16:08:15 -070035#include "av1/common/alloccommon.h"
Jean-Marc Valin01435132017-02-18 14:12:53 -050036#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010037#include "av1/common/cdef.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070038#include "av1/common/clpf.h"
39#endif
Nathan E. Egge2cf03b12017-02-22 16:19:59 -050040#if CONFIG_INSPECTION
41#include "av1/decoder/inspection.h"
42#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070043#include "av1/common/common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070044#include "av1/common/entropy.h"
45#include "av1/common/entropymode.h"
Thomas Davies6519beb2016-10-19 14:46:07 +010046#include "av1/common/entropymv.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070047#include "av1/common/idct.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070048#include "av1/common/pred_common.h"
49#include "av1/common/quant_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070050#include "av1/common/reconinter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070051#include "av1/common/reconintra.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070052#include "av1/common/seg_common.h"
Jingning Han1aab8182016-06-03 11:09:06 -070053#include "av1/common/thread_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070054#include "av1/common/tile_common.h"
55
56#include "av1/decoder/decodeframe.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070057#include "av1/decoder/decodemv.h"
58#include "av1/decoder/decoder.h"
Angie Chiang133733c2017-03-17 12:50:20 -070059#if CONFIG_LV_MAP
60#include "av1/decoder/decodetxb.h"
61#endif
Jingning Han1aab8182016-06-03 11:09:06 -070062#include "av1/decoder/detokenize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070063#include "av1/decoder/dsubexp.h"
64
Debargha Mukherjee3b6c5442017-03-30 08:22:00 -070065#if CONFIG_WARPED_MOTION || CONFIG_GLOBAL_MOTION
Yue Chen69f18e12016-09-08 14:48:15 -070066#include "av1/common/warped_motion.h"
Debargha Mukherjee3b6c5442017-03-30 08:22:00 -070067#endif // CONFIG_WARPED_MOTION || CONFIG_GLOBAL_MOTION
Yue Chen69f18e12016-09-08 14:48:15 -070068
Yaowu Xuf883b422016-08-30 14:01:10 -070069#define MAX_AV1_HEADER_SIZE 80
Michael Bebenita6048d052016-08-25 14:40:54 -070070#define ACCT_STR __func__
Yaowu Xuc27fc142016-08-22 16:08:15 -070071
Yushin Cho77bba8d2016-11-04 16:36:56 -070072#if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -070073#include "av1/common/partition.h"
hui suff0da2b2017-03-07 15:51:37 -080074#include "av1/common/pvq.h"
75#include "av1/common/scan.h"
Yushin Cho77bba8d2016-11-04 16:36:56 -070076#include "av1/decoder/decint.h"
hui suff0da2b2017-03-07 15:51:37 -080077#include "av1/decoder/pvq_decoder.h"
78#include "av1/encoder/encodemb.h"
Yushin Cho77bba8d2016-11-04 16:36:56 -070079#include "av1/encoder/hybrid_fwd_txfm.h"
80#endif
81
Thomas Davies80188d12016-10-26 16:08:35 -070082static struct aom_read_bit_buffer *init_read_bit_buffer(
83 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
84 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]);
85static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
86 size_t partition_size);
87static size_t read_uncompressed_header(AV1Decoder *pbi,
88 struct aom_read_bit_buffer *rb);
89
Yaowu Xuf883b422016-08-30 14:01:10 -070090static int is_compound_reference_allowed(const AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070091 int i;
92 if (frame_is_intra_only(cm)) return 0;
93 for (i = 1; i < INTER_REFS_PER_FRAME; ++i)
94 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
95
96 return 0;
97}
98
Yaowu Xuf883b422016-08-30 14:01:10 -070099static void setup_compound_reference_mode(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700100#if CONFIG_EXT_REFS
101 cm->comp_fwd_ref[0] = LAST_FRAME;
102 cm->comp_fwd_ref[1] = LAST2_FRAME;
103 cm->comp_fwd_ref[2] = LAST3_FRAME;
104 cm->comp_fwd_ref[3] = GOLDEN_FRAME;
105
106 cm->comp_bwd_ref[0] = BWDREF_FRAME;
107 cm->comp_bwd_ref[1] = ALTREF_FRAME;
108#else
109 if (cm->ref_frame_sign_bias[LAST_FRAME] ==
110 cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
111 cm->comp_fixed_ref = ALTREF_FRAME;
112 cm->comp_var_ref[0] = LAST_FRAME;
113 cm->comp_var_ref[1] = GOLDEN_FRAME;
114 } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
115 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
116 cm->comp_fixed_ref = GOLDEN_FRAME;
117 cm->comp_var_ref[0] = LAST_FRAME;
118 cm->comp_var_ref[1] = ALTREF_FRAME;
119 } else {
120 cm->comp_fixed_ref = LAST_FRAME;
121 cm->comp_var_ref[0] = GOLDEN_FRAME;
122 cm->comp_var_ref[1] = ALTREF_FRAME;
123 }
124#endif // CONFIG_EXT_REFS
125}
126
127static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
128 return len != 0 && len <= (size_t)(end - start);
129}
130
Yaowu Xuf883b422016-08-30 14:01:10 -0700131static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
132 const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700133 return data > max ? max : data;
134}
135
Yue Cheneeacc4c2017-01-17 17:29:17 -0800136static TX_MODE read_tx_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
137 struct aom_read_bit_buffer *rb) {
138 int i, all_lossless = 1;
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800139#if CONFIG_TX64X64
Yue Cheneeacc4c2017-01-17 17:29:17 -0800140 TX_MODE tx_mode;
141#endif
142
143 if (cm->seg.enabled) {
144 for (i = 0; i < MAX_SEGMENTS; ++i) {
145 if (!xd->lossless[i]) {
146 all_lossless = 0;
147 break;
148 }
149 }
150 } else {
151 all_lossless = xd->lossless[0];
152 }
153
154 if (all_lossless) return ONLY_4X4;
155#if CONFIG_TX64X64
156 tx_mode = aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800157 if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
158 return tx_mode;
159#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700160 return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800161#endif // CONFIG_TX64X64
Yaowu Xuc27fc142016-08-22 16:08:15 -0700162}
163
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -0500164#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -0700165static void read_tx_size_probs(FRAME_CONTEXT *fc, aom_reader *r) {
166 int i, j, k;
167 for (i = 0; i < MAX_TX_DEPTH; ++i)
168 for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
169 for (k = 0; k < i + 1; ++k)
170 av1_diff_update_prob(r, &fc->tx_size_probs[i][j][k], ACCT_STR);
171}
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -0500172#endif
Yaowu Xuefc75352016-10-31 09:46:42 -0700173
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400174#if !CONFIG_EC_ADAPT
Yaowu Xuf883b422016-08-30 14:01:10 -0700175static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700176 int i, j;
Nathan E. Egge4947c292016-04-26 11:37:06 -0400177 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700178 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700179 av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i], ACCT_STR);
Nathan E. Egge4947c292016-04-26 11:37:06 -0400180 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700181}
Thomas Davies6519beb2016-10-19 14:46:07 +0100182#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700183
Yaowu Xuf883b422016-08-30 14:01:10 -0700184static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700185#if CONFIG_REF_MV
Yaowu Xu8af861b2016-11-01 12:12:11 -0700186 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700187 for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700188 av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700189 for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700190 av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700191 for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700192 av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700193 for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700194 av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700195#else
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400196#if !CONFIG_EC_ADAPT
Yaowu Xu8af861b2016-11-01 12:12:11 -0700197 int i, j;
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400198 for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700199 for (j = 0; j < INTER_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700200 av1_diff_update_prob(r, &fc->inter_mode_probs[i][j], ACCT_STR);
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400201 }
Yaowu Xu8af861b2016-11-01 12:12:11 -0700202#else
203 (void)fc;
204 (void)r;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700205#endif
Thomas Davies6519beb2016-10-19 14:46:07 +0100206#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700207}
208
209#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700210static void read_inter_compound_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700211 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -0700212 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700213 for (j = 0; j < INTER_MODE_CONTEXTS; ++j) {
214 for (i = 0; i < INTER_COMPOUND_MODES - 1; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700215 av1_diff_update_prob(r, &fc->inter_compound_mode_probs[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700216 }
217 }
218 }
219}
220#endif // CONFIG_EXT_INTER
Yaowu Xu17fd2f22016-11-17 18:23:28 -0800221#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100222#if !CONFIG_EXT_TX
223static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
224 int i, j, k;
225 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
226 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
227 for (j = 0; j < TX_TYPES; ++j) {
228 for (k = 0; k < TX_TYPES - 1; ++k)
229 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[i][j][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100230 }
231 }
232 }
233 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
234 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
235 for (k = 0; k < TX_TYPES - 1; ++k)
236 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[i][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100237 }
238 }
239}
240#endif
241#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700242
243static REFERENCE_MODE read_frame_reference_mode(
Yaowu Xuf883b422016-08-30 14:01:10 -0700244 const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700245 if (is_compound_reference_allowed(cm)) {
Zoe Liub05e5d12017-02-07 14:32:53 -0800246#if CONFIG_REF_ADAPT
247 return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
248#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700249 return aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700250 ? REFERENCE_MODE_SELECT
Yaowu Xuf883b422016-08-30 14:01:10 -0700251 : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
Zoe Liub05e5d12017-02-07 14:32:53 -0800252#endif // CONFIG_REF_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700253 } else {
254 return SINGLE_REFERENCE;
255 }
256}
257
Yaowu Xuf883b422016-08-30 14:01:10 -0700258static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700259 FRAME_CONTEXT *const fc = cm->fc;
260 int i, j;
261
262 if (cm->reference_mode == REFERENCE_MODE_SELECT)
263 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700264 av1_diff_update_prob(r, &fc->comp_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700265
266 if (cm->reference_mode != COMPOUND_REFERENCE) {
267 for (i = 0; i < REF_CONTEXTS; ++i) {
268 for (j = 0; j < (SINGLE_REFS - 1); ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700269 av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700270 }
271 }
272 }
273
274 if (cm->reference_mode != SINGLE_REFERENCE) {
275 for (i = 0; i < REF_CONTEXTS; ++i) {
276#if CONFIG_EXT_REFS
277 for (j = 0; j < (FWD_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 for (j = 0; j < (BWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700280 av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700281#else
282 for (j = 0; j < (COMP_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700283 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700284#endif // CONFIG_EXT_REFS
285 }
286 }
287}
288
Yaowu Xuf883b422016-08-30 14:01:10 -0700289static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700290 int i;
Michael Bebenita6048d052016-08-25 14:40:54 -0700291 for (i = 0; i < n; ++i) av1_diff_update_prob(r, &p[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700292}
293
Yaowu Xuf883b422016-08-30 14:01:10 -0700294static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Thomas9ac55082016-09-23 18:04:17 +0100295 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700296
Yaowu Xue86288d2016-10-31 15:56:38 -0700297#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100298 int j;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700299 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
300
Yaowu Xuc27fc142016-08-22 16:08:15 -0700301 for (i = 0; i < 2; ++i) {
302 nmv_component *const comp_ctx = &ctx->comps[i];
303 update_mv_probs(&comp_ctx->sign, 1, r);
304 update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
305 update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
306 update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
307 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700308 for (i = 0; i < 2; ++i) {
309 nmv_component *const comp_ctx = &ctx->comps[i];
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400310 for (j = 0; j < CLASS0_SIZE; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700311 update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400312 }
Nathan E. Eggeac499f32016-09-08 15:38:57 -0400313 update_mv_probs(comp_ctx->fp, MV_FP_SIZE - 1, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700314 }
Alex Converseaca9feb2016-10-10 11:08:10 -0700315#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700316
317 if (allow_hp) {
318 for (i = 0; i < 2; ++i) {
319 nmv_component *const comp_ctx = &ctx->comps[i];
320 update_mv_probs(&comp_ctx->class0_hp, 1, r);
321 update_mv_probs(&comp_ctx->hp, 1, r);
322 }
323 }
324}
325
326static void inverse_transform_block(MACROBLOCKD *xd, int plane,
327 const TX_TYPE tx_type,
328 const TX_SIZE tx_size, uint8_t *dst,
Jingning Han1be18782016-10-21 11:48:15 -0700329 int stride, int16_t scan_line, int eob) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700330 struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han1be18782016-10-21 11:48:15 -0700331 tran_low_t *const dqcoeff = pd->dqcoeff;
Angie Chiangd92d4bf2017-04-02 17:49:18 -0700332 av1_inverse_transform_block(xd, dqcoeff, tx_type, tx_size, dst, stride, eob);
Jingning Han1be18782016-10-21 11:48:15 -0700333 memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700334}
335
Yushin Cho77bba8d2016-11-04 16:36:56 -0700336#if CONFIG_PVQ
Thomas Daede6ff6af62017-02-03 16:29:24 -0800337static int av1_pvq_decode_helper(MACROBLOCKD *xd, tran_low_t *ref_coeff,
Thomas Daede1dbda1b2017-02-06 16:06:29 -0800338 tran_low_t *dqcoeff, int16_t *quant, int pli,
Yushin Cho77bba8d2016-11-04 16:36:56 -0700339 int bs, TX_TYPE tx_type, int xdec,
ltrudeaue1c09292017-01-20 15:42:13 -0500340 PVQ_SKIP_TYPE ac_dc_coded) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700341 unsigned int flags; // used for daala's stream analyzer.
342 int off;
343 const int is_keyframe = 0;
344 const int has_dc_skip = 1;
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800345 int coeff_shift = 3 - get_tx_scale(bs);
Thomas Daede6ff6af62017-02-03 16:29:24 -0800346 int hbd_downshift = 0;
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800347 int rounding_mask;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700348 // DC quantizer for PVQ
349 int pvq_dc_quant;
350 int lossless = (quant[0] == 0);
351 const int blk_size = tx_size_wide[bs];
352 int eob = 0;
353 int i;
Thomas Daede6ff6af62017-02-03 16:29:24 -0800354 od_dec_ctx *dec = &xd->daala_dec;
Yushin Cho70669122016-12-08 09:53:14 -1000355 int use_activity_masking = dec->use_activity_masking;
Thomas Daede1dbda1b2017-02-06 16:06:29 -0800356 DECLARE_ALIGNED(16, tran_low_t, dqcoeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
357 DECLARE_ALIGNED(16, tran_low_t, ref_coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700358
Yushin Cho48f84db2016-11-07 21:20:17 -0800359 od_coeff ref_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
360 od_coeff out_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700361
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200362#if CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -0800363 hbd_downshift = xd->bd - 8;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200364#endif // CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -0800365
Yushin Cho77bba8d2016-11-04 16:36:56 -0700366 od_raster_to_coding_order(ref_coeff_pvq, blk_size, tx_type, ref_coeff,
367 blk_size);
368
Thomas Daede6ff6af62017-02-03 16:29:24 -0800369 assert(OD_COEFF_SHIFT >= 4);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700370 if (lossless)
371 pvq_dc_quant = 1;
372 else {
Yushin Cho70669122016-12-08 09:53:14 -1000373 if (use_activity_masking)
374 pvq_dc_quant = OD_MAXI(
Thomas Daede6ff6af62017-02-03 16:29:24 -0800375 1, (quant[0] << (OD_COEFF_SHIFT - 3) >> hbd_downshift) *
Yushin Cho70669122016-12-08 09:53:14 -1000376 dec->state.pvq_qm_q4[pli][od_qm_get_index(bs, 0)] >>
377 4);
378 else
Thomas Daede6ff6af62017-02-03 16:29:24 -0800379 pvq_dc_quant =
380 OD_MAXI(1, quant[0] << (OD_COEFF_SHIFT - 3) >> hbd_downshift);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700381 }
382
383 off = od_qm_offset(bs, xdec);
384
385 // copy int16 inputs to int32
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800386 for (i = 0; i < blk_size * blk_size; i++) {
Timothy B. Terriberry4e6a8f32017-02-24 11:00:59 -0800387 ref_int32[i] =
Thomas Daede6ff6af62017-02-03 16:29:24 -0800388 AOM_SIGNED_SHL(ref_coeff_pvq[i], OD_COEFF_SHIFT - coeff_shift) >>
389 hbd_downshift;
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800390 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700391
Thomas Daede6ff6af62017-02-03 16:29:24 -0800392 od_pvq_decode(dec, ref_int32, out_int32,
393 OD_MAXI(1, quant[1] << (OD_COEFF_SHIFT - 3) >> hbd_downshift),
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800394 pli, bs, OD_PVQ_BETA[use_activity_masking][pli][bs],
Timothy B. Terriberry44bb6d02017-04-07 15:44:14 -0700395 is_keyframe, &flags, ac_dc_coded, dec->state.qm + off,
396 dec->state.qm_inv + off);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700397
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800398 if (!has_dc_skip || out_int32[0]) {
399 out_int32[0] =
Yushin Choc49ef3a2017-03-13 17:27:25 -0700400 has_dc_skip + generic_decode(dec->r, &dec->state.adapt->model_dc[pli],
Timothy B. Terriberry44bb6d02017-04-07 15:44:14 -0700401 &dec->state.adapt->ex_dc[pli][bs][0], 2,
402 "dc:mag");
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800403 if (out_int32[0]) out_int32[0] *= aom_read_bit(dec->r, "dc:sign") ? -1 : 1;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700404 }
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800405 out_int32[0] = out_int32[0] * pvq_dc_quant + ref_int32[0];
406
407 // copy int32 result back to int16
408 assert(OD_COEFF_SHIFT > coeff_shift);
409 rounding_mask = (1 << (OD_COEFF_SHIFT - coeff_shift - 1)) - 1;
410 for (i = 0; i < blk_size * blk_size; i++) {
Thomas Daede6ff6af62017-02-03 16:29:24 -0800411 out_int32[i] = AOM_SIGNED_SHL(out_int32[i], hbd_downshift);
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800412 dqcoeff_pvq[i] = (out_int32[i] + (out_int32[i] < 0) + rounding_mask) >>
413 (OD_COEFF_SHIFT - coeff_shift);
414 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700415
416 od_coding_order_to_raster(dqcoeff, blk_size, tx_type, dqcoeff_pvq, blk_size);
417
418 eob = blk_size * blk_size;
419
420 return eob;
421}
422
ltrudeaue1c09292017-01-20 15:42:13 -0500423static PVQ_SKIP_TYPE read_pvq_skip(AV1_COMMON *cm, MACROBLOCKD *const xd,
424 int plane, TX_SIZE tx_size) {
425 // decode ac/dc coded flag. bit0: DC coded, bit1 : AC coded
426 // NOTE : we don't use 5 symbols for luma here in aom codebase,
427 // since block partition is taken care of by aom.
428 // So, only AC/DC skip info is coded
Yushin Cho00779272017-02-21 10:38:16 -0800429 const int ac_dc_coded = aom_read_symbol(
ltrudeaue1c09292017-01-20 15:42:13 -0500430 xd->daala_dec.r,
Yushin Choc49ef3a2017-03-13 17:27:25 -0700431 xd->daala_dec.state.adapt->skip_cdf[2 * tx_size + (plane != 0)], 4,
Yushin Cho00779272017-02-21 10:38:16 -0800432 "skip");
ltrudeaue1c09292017-01-20 15:42:13 -0500433 if (ac_dc_coded < 0 || ac_dc_coded > 3) {
434 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
435 "Invalid PVQ Skip Type");
436 }
437 return ac_dc_coded;
438}
439
440static int av1_pvq_decode_helper2(AV1_COMMON *cm, MACROBLOCKD *const xd,
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800441 MB_MODE_INFO *const mbmi, int plane, int row,
442 int col, TX_SIZE tx_size, TX_TYPE tx_type) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700443 struct macroblockd_plane *const pd = &xd->plane[plane];
444 // transform block size in pixels
445 int tx_blk_size = tx_size_wide[tx_size];
446 int i, j;
447 tran_low_t *pvq_ref_coeff = pd->pvq_ref_coeff;
448 const int diff_stride = tx_blk_size;
449 int16_t *pred = pd->pred;
450 tran_low_t *const dqcoeff = pd->dqcoeff;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700451 uint8_t *dst;
452 int eob;
ltrudeaue1c09292017-01-20 15:42:13 -0500453 const PVQ_SKIP_TYPE ac_dc_coded = read_pvq_skip(cm, xd, plane, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700454
455 eob = 0;
456 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
457
Yushin Cho77bba8d2016-11-04 16:36:56 -0700458 if (ac_dc_coded) {
459 int xdec = pd->subsampling_x;
460 int seg_id = mbmi->segment_id;
461 int16_t *quant;
462 FWD_TXFM_PARAM fwd_txfm_param;
Yaowu Xufc1b2132016-11-07 15:16:15 -0800463 // ToDo(yaowu): correct this with optimal number from decoding process.
464 const int max_scan_line = tx_size_2d[tx_size];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200465#if CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -0800466 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
467 for (j = 0; j < tx_blk_size; j++)
468 for (i = 0; i < tx_blk_size; i++)
469 pred[diff_stride * j + i] =
470 CONVERT_TO_SHORTPTR(dst)[pd->dst.stride * j + i];
471 } else {
472#endif
473 for (j = 0; j < tx_blk_size; j++)
474 for (i = 0; i < tx_blk_size; i++)
475 pred[diff_stride * j + i] = dst[pd->dst.stride * j + i];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200476#if CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -0800477 }
478#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -0700479
480 fwd_txfm_param.tx_type = tx_type;
481 fwd_txfm_param.tx_size = tx_size;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700482 fwd_txfm_param.lossless = xd->lossless[seg_id];
483
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200484#if CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -0800485 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
486 fwd_txfm_param.bd = xd->bd;
hui suf11fb882017-03-27 14:56:33 -0700487 av1_highbd_fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
Thomas Daede6ff6af62017-02-03 16:29:24 -0800488 } else {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200489#endif // CONFIG_HIGHBITDEPTH
hui suf11fb882017-03-27 14:56:33 -0700490 av1_fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200491#if CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -0800492 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200493#endif // CONFIG_HIGHBITDEPTH
Yushin Cho77bba8d2016-11-04 16:36:56 -0700494
495 quant = &pd->seg_dequant[seg_id][0]; // aom's quantizer
496
Thomas Daede6ff6af62017-02-03 16:29:24 -0800497 eob = av1_pvq_decode_helper(xd, pvq_ref_coeff, dqcoeff, quant, plane,
498 tx_size, tx_type, xdec, ac_dc_coded);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700499
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800500 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
501 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700502 }
503
504 return eob;
505}
506#endif
507
Angie Chiang752ccce2017-04-09 13:41:13 -0700508static int get_block_idx(const MACROBLOCKD *xd, int plane, int row, int col) {
509 const int bsize = xd->mi[0]->mbmi.sb_type;
510 const struct macroblockd_plane *pd = &xd->plane[plane];
511#if CONFIG_CB4X4
512#if CONFIG_CHROMA_2X2
513 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
514#else
515 const BLOCK_SIZE plane_bsize =
516 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
517#endif // CONFIG_CHROMA_2X2
518#else
519 const BLOCK_SIZE plane_bsize =
520 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
521#endif
522 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
523 const TX_SIZE tx_size = get_tx_size(plane, xd);
524 const uint8_t txh_unit = tx_size_high_unit[tx_size];
525 return row * max_blocks_wide + col * txh_unit;
526}
527
Alex Converse8aca36d2017-01-31 12:33:15 -0800528static void predict_and_reconstruct_intra_block(
529 AV1_COMMON *cm, MACROBLOCKD *const xd, aom_reader *const r,
530 MB_MODE_INFO *const mbmi, int plane, int row, int col, TX_SIZE tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500531 PLANE_TYPE plane_type = get_plane_type(plane);
Angie Chiang752ccce2017-04-09 13:41:13 -0700532 const int block_idx = get_block_idx(xd, plane, row, col);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700533#if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -0700534 (void)r;
535#endif
Angie Chiang3d005e42017-04-02 16:31:35 -0700536 av1_predict_intra_block_facade(xd, plane, block_idx, col, row, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700537
538 if (!mbmi->skip) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700539#if !CONFIG_PVQ
Angie Chiang3d005e42017-04-02 16:31:35 -0700540 struct macroblockd_plane *const pd = &xd->plane[plane];
Angie Chiang133733c2017-03-17 12:50:20 -0700541#if CONFIG_LV_MAP
542 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700543 int eob;
544 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane,
545 pd->dqcoeff, &max_scan_line, &eob);
Angie Chiangb6d770c2017-04-14 16:27:57 -0700546 // tx_type will be read out in av1_read_coeffs_txb_facade
547 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Angie Chiang133733c2017-03-17 12:50:20 -0700548#else // CONFIG_LV_MAP
Angie Chiangb6d770c2017-04-14 16:27:57 -0700549 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Angie Chiangff6d8902016-10-21 11:02:09 -0700550 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 0);
Jingning Han1be18782016-10-21 11:48:15 -0700551 int16_t max_scan_line = 0;
552 const int eob =
Angie Chiang5c0568a2017-03-21 16:00:39 -0700553 av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
Jingning Han1be18782016-10-21 11:48:15 -0700554 tx_type, &max_scan_line, r, mbmi->segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700555#endif // CONFIG_LV_MAP
Angie Chiang3d005e42017-04-02 16:31:35 -0700556 if (eob) {
557 uint8_t *dst =
558 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Jingning Han1be18782016-10-21 11:48:15 -0700559 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
560 max_scan_line, eob);
Angie Chiang3d005e42017-04-02 16:31:35 -0700561 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700562#else
Angie Chiangb6d770c2017-04-14 16:27:57 -0700563 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
ltrudeaue1c09292017-01-20 15:42:13 -0500564 av1_pvq_decode_helper2(cm, xd, mbmi, plane, row, col, tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700565#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700566 }
567}
568
Jingning Handddb21f2017-02-28 14:44:05 -0800569#if CONFIG_VAR_TX && !CONFIG_COEF_INTERLEAVE
Angie Chiangff6d8902016-10-21 11:02:09 -0700570static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
571 aom_reader *r, MB_MODE_INFO *const mbmi,
Jingning Han8fd62b72016-10-21 12:55:54 -0700572 int plane, BLOCK_SIZE plane_bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700573 int blk_row, int blk_col, TX_SIZE tx_size,
574 int *eob_total) {
575 const struct macroblockd_plane *const pd = &xd->plane[plane];
576 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
577 const int tx_row = blk_row >> (1 - pd->subsampling_y);
578 const int tx_col = blk_col >> (1 - pd->subsampling_x);
579 const TX_SIZE plane_tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700580 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700581 : mbmi->inter_tx_size[tx_row][tx_col];
Jingning Han5f614262016-10-27 14:27:43 -0700582 // Scale to match transform block unit.
Jingning Hanf64062f2016-11-02 16:22:18 -0700583 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
584 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700585
586 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
587
588 if (tx_size == plane_tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500589 PLANE_TYPE plane_type = get_plane_type(plane);
Angie Chiang752ccce2017-04-09 13:41:13 -0700590 int block_idx = get_block_idx(xd, plane, blk_row, blk_col);
Angie Chiang133733c2017-03-17 12:50:20 -0700591#if CONFIG_LV_MAP
592 (void)segment_id;
593 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700594 int eob;
595 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane,
596 pd->dqcoeff, &max_scan_line, &eob);
Angie Chiangb6d770c2017-04-14 16:27:57 -0700597 // tx_type will be read out in av1_read_coeffs_txb_facade
598 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiang133733c2017-03-17 12:50:20 -0700599#else // CONFIG_LV_MAP
Angie Chiangb6d770c2017-04-14 16:27:57 -0700600 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiangff6d8902016-10-21 11:02:09 -0700601 const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700602 int16_t max_scan_line = 0;
Angie Chiang5c0568a2017-03-21 16:00:39 -0700603 const int eob = av1_decode_block_tokens(
604 cm, xd, plane, sc, blk_col, blk_row, plane_tx_size, tx_type,
605 &max_scan_line, r, mbmi->segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700606#endif // CONFIG_LV_MAP
Jingning Han9ca05b72017-01-03 14:41:36 -0800607 inverse_transform_block(xd, plane, tx_type, plane_tx_size,
608 &pd->dst.buf[(blk_row * pd->dst.stride + blk_col)
609 << tx_size_wide_log2[0]],
610 pd->dst.stride, max_scan_line, eob);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700611 *eob_total += eob;
612 } else {
Jingning Hanf64062f2016-11-02 16:22:18 -0700613 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
614 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700615 int i;
616
617 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700618
619 for (i = 0; i < 4; ++i) {
Jingning Han5f614262016-10-27 14:27:43 -0700620 const int offsetr = blk_row + (i >> 1) * bsl;
621 const int offsetc = blk_col + (i & 0x01) * bsl;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700622
623 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
624
Jingning Han8fd62b72016-10-21 12:55:54 -0700625 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
Jingning Hanf64062f2016-11-02 16:22:18 -0700626 offsetc, sub_txs, eob_total);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700627 }
628 }
629}
630#endif // CONFIG_VAR_TX
631
Jingning Handddb21f2017-02-28 14:44:05 -0800632#if !CONFIG_VAR_TX || CONFIG_SUPERTX || CONFIG_COEF_INTERLEAVE || \
Jingning Hanfe45b212016-11-22 10:30:23 -0800633 (!CONFIG_VAR_TX && CONFIG_EXT_TX && CONFIG_RECT_TX)
Angie Chiangff6d8902016-10-21 11:02:09 -0700634static int reconstruct_inter_block(AV1_COMMON *cm, MACROBLOCKD *const xd,
Alex Converse8aca36d2017-01-31 12:33:15 -0800635 aom_reader *const r, int segment_id,
636 int plane, int row, int col,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700637 TX_SIZE tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500638 PLANE_TYPE plane_type = get_plane_type(plane);
Angie Chiang752ccce2017-04-09 13:41:13 -0700639 int block_idx = get_block_idx(xd, plane, row, col);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700640#if CONFIG_PVQ
641 int eob;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700642 (void)r;
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800643 (void)segment_id;
644#else
645 struct macroblockd_plane *const pd = &xd->plane[plane];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700646#endif
647
648#if !CONFIG_PVQ
Angie Chiang133733c2017-03-17 12:50:20 -0700649#if CONFIG_LV_MAP
650 (void)segment_id;
Jingning Han1be18782016-10-21 11:48:15 -0700651 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700652 int eob;
653 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane, pd->dqcoeff,
654 &max_scan_line, &eob);
Angie Chiangb6d770c2017-04-14 16:27:57 -0700655 // tx_type will be read out in av1_read_coeffs_txb_facade
656 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Angie Chiang133733c2017-03-17 12:50:20 -0700657#else // CONFIG_LV_MAP
658 int16_t max_scan_line = 0;
Angie Chiangb6d770c2017-04-14 16:27:57 -0700659 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Angie Chiang133733c2017-03-17 12:50:20 -0700660 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700661 const int eob =
Angie Chiang5c0568a2017-03-21 16:00:39 -0700662 av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
663 tx_type, &max_scan_line, r, segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700664#endif // CONFIG_LV_MAP
Jingning Hanca14dda2016-12-09 09:36:00 -0800665 uint8_t *dst =
666 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Jingning Han1be18782016-10-21 11:48:15 -0700667 if (eob)
Jingning Hanca14dda2016-12-09 09:36:00 -0800668 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
669 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700670#else
Angie Chiangb6d770c2017-04-14 16:27:57 -0700671 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
ltrudeaue1c09292017-01-20 15:42:13 -0500672 eob = av1_pvq_decode_helper2(cm, xd, &xd->mi[0]->mbmi, plane, row, col,
673 tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700674#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700675 return eob;
676}
677#endif // !CONFIG_VAR_TX || CONFIG_SUPER_TX
678
Angie Chiang44701f22017-02-27 10:36:44 -0800679static void set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
680 BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
681 int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700682 const int offset = mi_row * cm->mi_stride + mi_col;
683 int x, y;
684 const TileInfo *const tile = &xd->tile;
685
686 xd->mi = cm->mi_grid_visible + offset;
687 xd->mi[0] = &cm->mi[offset];
688 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
689 // passing bsize from decode_partition().
690 xd->mi[0]->mbmi.sb_type = bsize;
Angie Chiang394c3372016-11-03 11:13:15 -0700691#if CONFIG_RD_DEBUG
692 xd->mi[0]->mbmi.mi_row = mi_row;
693 xd->mi[0]->mbmi.mi_col = mi_col;
694#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700695 for (y = 0; y < y_mis; ++y)
Jingning Han97d85482016-07-15 11:06:05 -0700696 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700697
Jingning Hanfaad0e12016-12-07 10:54:57 -0800698 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700699 set_skip_context(xd, mi_row, mi_col);
700
701#if CONFIG_VAR_TX
702 xd->max_tx_size = max_txsize_lookup[bsize];
703#endif
704
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800705// Distance of Mb to the various image edges. These are specified to 8th pel
706// as they are always compared to values that are in 1/8th pel units
707#if CONFIG_DEPENDENT_HORZTILES
708 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
709 cm->dependent_horz_tiles);
710#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700711 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 -0800712#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700713
Yaowu Xuf883b422016-08-30 14:01:10 -0700714 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700715}
716
717#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700718static MB_MODE_INFO *set_offsets_extend(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700719 MACROBLOCKD *const xd,
720 const TileInfo *const tile,
721 BLOCK_SIZE bsize_pred, int mi_row_pred,
722 int mi_col_pred, int mi_row_ori,
723 int mi_col_ori) {
724 // Used in supertx
725 // (mi_row_ori, mi_col_ori): location for mv
726 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
Jingning Han93531242016-12-20 11:54:36 -0800727 const int bw = mi_size_wide[bsize_pred];
728 const int bh = mi_size_high[bsize_pred];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700729 const int offset = mi_row_ori * cm->mi_stride + mi_col_ori;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700730 xd->mi = cm->mi_grid_visible + offset;
731 xd->mi[0] = cm->mi + offset;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800732#if CONFIG_DEPENDENT_HORZTILES
733 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
734 cm->mi_cols, cm->dependent_horz_tiles);
735#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700736 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
737 cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800738#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700739
740 xd->up_available = (mi_row_ori > tile->mi_row_start);
741 xd->left_available = (mi_col_ori > tile->mi_col_start);
742
Jingning Hanfaad0e12016-12-07 10:54:57 -0800743 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700744
745 return &xd->mi[0]->mbmi;
746}
747
Angie Chiang7fcfee42017-02-24 15:51:03 -0800748#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700749static MB_MODE_INFO *set_mb_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
750 BLOCK_SIZE bsize, int mi_row, int mi_col,
751 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700752 const int offset = mi_row * cm->mi_stride + mi_col;
753 const TileInfo *const tile = &xd->tile;
754 int x, y;
755
756 xd->mi = cm->mi_grid_visible + offset;
757 xd->mi[0] = cm->mi + offset;
758 xd->mi[0]->mbmi.sb_type = bsize;
759 for (y = 0; y < y_mis; ++y)
760 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
761
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800762#if CONFIG_DEPENDENT_HORZTILES
763 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
764 cm->dependent_horz_tiles);
765#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700766 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 -0800767#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700768 return &xd->mi[0]->mbmi;
769}
Angie Chiang7fcfee42017-02-24 15:51:03 -0800770#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700771
Yaowu Xuf883b422016-08-30 14:01:10 -0700772static void set_offsets_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700773 const TileInfo *const tile, BLOCK_SIZE bsize,
774 int mi_row, int mi_col) {
Jingning Han93531242016-12-20 11:54:36 -0800775 const int bw = mi_size_wide[bsize];
776 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700777 const int offset = mi_row * cm->mi_stride + mi_col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700778
779 xd->mi = cm->mi_grid_visible + offset;
780 xd->mi[0] = cm->mi + offset;
781
Jingning Hanfaad0e12016-12-07 10:54:57 -0800782 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700783
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800784#if CONFIG_DEPENDENT_HORZTILES
785 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
786 cm->dependent_horz_tiles);
787#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700788 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 -0800789#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700790
Yaowu Xuf883b422016-08-30 14:01:10 -0700791 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700792}
793
Yaowu Xuf883b422016-08-30 14:01:10 -0700794static void set_param_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700795 BLOCK_SIZE bsize, int mi_row, int mi_col,
796 int txfm, int skip) {
Jingning Han93531242016-12-20 11:54:36 -0800797 const int bw = mi_size_wide[bsize];
798 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -0700799 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
800 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700801 const int offset = mi_row * cm->mi_stride + mi_col;
802 int x, y;
803
804 xd->mi = cm->mi_grid_visible + offset;
805 xd->mi[0] = cm->mi + offset;
806
807 for (y = 0; y < y_mis; ++y)
808 for (x = 0; x < x_mis; ++x) {
809 xd->mi[y * cm->mi_stride + x]->mbmi.skip = skip;
810 xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
811 }
812#if CONFIG_VAR_TX
813 xd->above_txfm_context = cm->above_txfm_context + mi_col;
814 xd->left_txfm_context =
815 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
Yaowu Xu52a17632016-11-17 15:48:21 -0800816 set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bw, bh, skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700817#endif
818}
819
Yaowu Xuf883b422016-08-30 14:01:10 -0700820static void set_ref(AV1_COMMON *const cm, MACROBLOCKD *const xd, int idx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700821 int mi_row, int mi_col) {
822 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
823 RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
824 xd->block_refs[idx] = ref_buffer;
Yaowu Xuf883b422016-08-30 14:01:10 -0700825 if (!av1_is_valid_scale(&ref_buffer->sf))
826 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700827 "Invalid scale factors");
Yaowu Xuf883b422016-08-30 14:01:10 -0700828 av1_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
829 &ref_buffer->sf);
Angie Chiangd0916d92017-03-10 17:54:18 -0800830 aom_merge_corrupted_flag(&xd->corrupted, ref_buffer->buf->corrupted);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700831}
832
833static void dec_predict_b_extend(
Yaowu Xuf883b422016-08-30 14:01:10 -0700834 AV1Decoder *const pbi, MACROBLOCKD *const xd, const TileInfo *const tile,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700835 int block, int mi_row_ori, int mi_col_ori, int mi_row_pred, int mi_col_pred,
836 int mi_row_top, int mi_col_top, uint8_t *dst_buf[3], int dst_stride[3],
837 BLOCK_SIZE bsize_top, BLOCK_SIZE bsize_pred, int b_sub8x8, int bextend) {
838 // Used in supertx
839 // (mi_row_ori, mi_col_ori): location for mv
840 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
841 // (mi_row_top, mi_col_top, bsize_top): region of the top partition size
842 // block: sub location of sub8x8 blocks
843 // b_sub8x8: 1: ori is sub8x8; 0: ori is not sub8x8
844 // bextend: 1: region to predict is an extension of ori; 0: not
845 int r = (mi_row_pred - mi_row_top) * MI_SIZE;
846 int c = (mi_col_pred - mi_col_top) * MI_SIZE;
Jingning Han93531242016-12-20 11:54:36 -0800847 const int mi_width_top = mi_size_wide[bsize_top];
848 const int mi_height_top = mi_size_high[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700849 MB_MODE_INFO *mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700850 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700851
852 if (mi_row_pred < mi_row_top || mi_col_pred < mi_col_top ||
853 mi_row_pred >= mi_row_top + mi_height_top ||
854 mi_col_pred >= mi_col_top + mi_width_top || mi_row_pred >= cm->mi_rows ||
855 mi_col_pred >= cm->mi_cols)
856 return;
857
858 mbmi = set_offsets_extend(cm, xd, tile, bsize_pred, mi_row_pred, mi_col_pred,
859 mi_row_ori, mi_col_ori);
860 set_ref(cm, xd, 0, mi_row_pred, mi_col_pred);
861 if (has_second_ref(&xd->mi[0]->mbmi))
862 set_ref(cm, xd, 1, mi_row_pred, mi_col_pred);
863
Jingning Han2511c662016-12-22 11:57:34 -0800864 if (!bextend) mbmi->tx_size = max_txsize_lookup[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700865
866 xd->plane[0].dst.stride = dst_stride[0];
867 xd->plane[1].dst.stride = dst_stride[1];
868 xd->plane[2].dst.stride = dst_stride[2];
869 xd->plane[0].dst.buf = dst_buf[0] +
870 (r >> xd->plane[0].subsampling_y) * dst_stride[0] +
871 (c >> xd->plane[0].subsampling_x);
872 xd->plane[1].dst.buf = dst_buf[1] +
873 (r >> xd->plane[1].subsampling_y) * dst_stride[1] +
874 (c >> xd->plane[1].subsampling_x);
875 xd->plane[2].dst.buf = dst_buf[2] +
876 (r >> xd->plane[2].subsampling_y) * dst_stride[2] +
877 (c >> xd->plane[2].subsampling_x);
878
879 if (!b_sub8x8)
Yaowu Xuf883b422016-08-30 14:01:10 -0700880 av1_build_inter_predictors_sb_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700881#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700882 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700883#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700884 mi_row_pred, mi_col_pred, bsize_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700885 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700886 av1_build_inter_predictors_sb_sub8x8_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700887#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700888 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700889#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700890 mi_row_pred, mi_col_pred,
891 bsize_pred, block);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700892}
893
Yaowu Xuf883b422016-08-30 14:01:10 -0700894static void dec_extend_dir(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700895 const TileInfo *const tile, int block,
896 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
897 int mi_col, int mi_row_top, int mi_col_top,
898 uint8_t *dst_buf[3], int dst_stride[3], int dir) {
899 // dir: 0-lower, 1-upper, 2-left, 3-right
900 // 4-lowerleft, 5-upperleft, 6-lowerright, 7-upperright
Jingning Han93531242016-12-20 11:54:36 -0800901 const int mi_width = mi_size_wide[bsize];
902 const int mi_height = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700903 int xss = xd->plane[1].subsampling_x;
904 int yss = xd->plane[1].subsampling_y;
Jingning Hanfeb517c2016-12-21 16:02:07 -0800905#if CONFIG_CB4X4
906 const int unify_bsize = 1;
907#else
908 const int unify_bsize = 0;
909#endif
910 int b_sub8x8 = (bsize < BLOCK_8X8) && !unify_bsize ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700911 BLOCK_SIZE extend_bsize;
Jingning Han24f24a52016-12-27 10:13:28 -0800912 int mi_row_pred, mi_col_pred;
913
914 int wide_unit, high_unit;
915 int i, j;
916 int ext_offset = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700917
918 if (dir == 0 || dir == 1) {
Jingning Han93531242016-12-20 11:54:36 -0800919 extend_bsize =
920 (mi_width == mi_size_wide[BLOCK_8X8] || bsize < BLOCK_8X8 || xss < yss)
921 ? BLOCK_8X8
922 : BLOCK_16X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800923#if CONFIG_CB4X4
924 if (bsize < BLOCK_8X8) {
925 extend_bsize = BLOCK_4X4;
926 ext_offset = mi_size_wide[BLOCK_8X8];
927 }
928#endif
929
930 wide_unit = mi_size_wide[extend_bsize];
931 high_unit = mi_size_high[extend_bsize];
932
933 mi_row_pred = mi_row + ((dir == 0) ? mi_height : -(mi_height + ext_offset));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700934 mi_col_pred = mi_col;
935
Jingning Han24f24a52016-12-27 10:13:28 -0800936 for (j = 0; j < mi_height + ext_offset; j += high_unit)
937 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
938 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
939 mi_row_pred + j, mi_col_pred + i, mi_row_top,
940 mi_col_top, dst_buf, dst_stride, top_bsize,
941 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700942 } else if (dir == 2 || dir == 3) {
Jingning Han5b7706a2016-12-21 09:55:10 -0800943 extend_bsize =
944 (mi_height == mi_size_high[BLOCK_8X8] || bsize < BLOCK_8X8 || yss < xss)
945 ? BLOCK_8X8
946 : BLOCK_8X16;
Jingning Han24f24a52016-12-27 10:13:28 -0800947#if CONFIG_CB4X4
948 if (bsize < BLOCK_8X8) {
949 extend_bsize = BLOCK_4X4;
950 ext_offset = mi_size_wide[BLOCK_8X8];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700951 }
Jingning Han24f24a52016-12-27 10:13:28 -0800952#endif
953
954 wide_unit = mi_size_wide[extend_bsize];
955 high_unit = mi_size_high[extend_bsize];
956
957 mi_row_pred = mi_row;
958 mi_col_pred = mi_col + ((dir == 3) ? mi_width : -(mi_width + ext_offset));
959
960 for (j = 0; j < mi_height + ext_offset; j += high_unit)
961 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
962 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
963 mi_row_pred + j, mi_col_pred + i, mi_row_top,
964 mi_col_top, dst_buf, dst_stride, top_bsize,
965 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700966 } else {
967 extend_bsize = BLOCK_8X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800968#if CONFIG_CB4X4
969 if (bsize < BLOCK_8X8) {
970 extend_bsize = BLOCK_4X4;
971 ext_offset = mi_size_wide[BLOCK_8X8];
972 }
973#endif
974 wide_unit = mi_size_wide[extend_bsize];
975 high_unit = mi_size_high[extend_bsize];
976
Jingning Han5b7706a2016-12-21 09:55:10 -0800977 mi_row_pred = mi_row + ((dir == 4 || dir == 6) ? mi_height
Jingning Han24f24a52016-12-27 10:13:28 -0800978 : -(mi_height + ext_offset));
Jingning Han5b7706a2016-12-21 09:55:10 -0800979 mi_col_pred =
Jingning Han24f24a52016-12-27 10:13:28 -0800980 mi_col + ((dir == 6 || dir == 7) ? mi_width : -(mi_width + ext_offset));
981
982 for (j = 0; j < mi_height + ext_offset; j += high_unit)
983 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
984 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
985 mi_row_pred + j, mi_col_pred + i, mi_row_top,
986 mi_col_top, dst_buf, dst_stride, top_bsize,
987 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700988 }
989}
990
Yaowu Xuf883b422016-08-30 14:01:10 -0700991static void dec_extend_all(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700992 const TileInfo *const tile, int block,
993 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
994 int mi_col, int mi_row_top, int mi_col_top,
995 uint8_t *dst_buf[3], int dst_stride[3]) {
996 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
997 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
998 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
999 mi_row_top, mi_col_top, dst_buf, dst_stride, 1);
1000 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1001 mi_row_top, mi_col_top, dst_buf, dst_stride, 2);
1002 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1003 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1004 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1005 mi_row_top, mi_col_top, dst_buf, dst_stride, 4);
1006 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1007 mi_row_top, mi_col_top, dst_buf, dst_stride, 5);
1008 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1009 mi_row_top, mi_col_top, dst_buf, dst_stride, 6);
1010 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1011 mi_row_top, mi_col_top, dst_buf, dst_stride, 7);
1012}
1013
Yaowu Xuf883b422016-08-30 14:01:10 -07001014static void dec_predict_sb_complex(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001015 const TileInfo *const tile, int mi_row,
1016 int mi_col, int mi_row_top, int mi_col_top,
1017 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize,
1018 uint8_t *dst_buf[3], int dst_stride[3]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001019 const AV1_COMMON *const cm = &pbi->common;
Jingning Han5b7706a2016-12-21 09:55:10 -08001020 const int hbs = mi_size_wide[bsize] / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001021 const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
1022 const BLOCK_SIZE subsize = get_subsize(bsize, partition);
1023#if CONFIG_EXT_PARTITION_TYPES
1024 const BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1025#endif
1026 int i;
1027 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1028 uint8_t *dst_buf1[3], *dst_buf2[3], *dst_buf3[3];
Jingning Hanfeb517c2016-12-21 16:02:07 -08001029#if CONFIG_CB4X4
1030 const int unify_bsize = 1;
1031#else
1032 const int unify_bsize = 0;
1033#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001034
1035 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1036 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1037 DECLARE_ALIGNED(16, uint8_t, tmp_buf3[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1038 int dst_stride1[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1039 int dst_stride2[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1040 int dst_stride3[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1041
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001042#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001043 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1044 int len = sizeof(uint16_t);
1045 dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
1046 dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_TX_SQUARE * len);
1047 dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_TX_SQUARE * len);
1048 dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
1049 dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_TX_SQUARE * len);
1050 dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_TX_SQUARE * len);
1051 dst_buf3[0] = CONVERT_TO_BYTEPTR(tmp_buf3);
1052 dst_buf3[1] = CONVERT_TO_BYTEPTR(tmp_buf3 + MAX_TX_SQUARE * len);
1053 dst_buf3[2] = CONVERT_TO_BYTEPTR(tmp_buf3 + 2 * MAX_TX_SQUARE * len);
1054 } else {
1055#endif
1056 dst_buf1[0] = tmp_buf1;
1057 dst_buf1[1] = tmp_buf1 + MAX_TX_SQUARE;
1058 dst_buf1[2] = tmp_buf1 + 2 * MAX_TX_SQUARE;
1059 dst_buf2[0] = tmp_buf2;
1060 dst_buf2[1] = tmp_buf2 + MAX_TX_SQUARE;
1061 dst_buf2[2] = tmp_buf2 + 2 * MAX_TX_SQUARE;
1062 dst_buf3[0] = tmp_buf3;
1063 dst_buf3[1] = tmp_buf3 + MAX_TX_SQUARE;
1064 dst_buf3[2] = tmp_buf3 + 2 * MAX_TX_SQUARE;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001065#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001066 }
1067#endif
1068
1069 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1070
1071 xd->mi = cm->mi_grid_visible + mi_offset;
1072 xd->mi[0] = cm->mi + mi_offset;
1073
1074 for (i = 0; i < MAX_MB_PLANE; i++) {
1075 xd->plane[i].dst.buf = dst_buf[i];
1076 xd->plane[i].dst.stride = dst_stride[i];
1077 }
1078
1079 switch (partition) {
1080 case PARTITION_NONE:
1081 assert(bsize < top_bsize);
1082 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1083 mi_row_top, mi_col_top, dst_buf, dst_stride,
1084 top_bsize, bsize, 0, 0);
1085 dec_extend_all(pbi, xd, tile, 0, bsize, top_bsize, mi_row, mi_col,
1086 mi_row_top, mi_col_top, dst_buf, dst_stride);
1087 break;
1088 case PARTITION_HORZ:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001089 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001090 // For sub8x8, predict in 8x8 unit
1091 // First half
1092 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1093 mi_row_top, mi_col_top, dst_buf, dst_stride,
1094 top_bsize, BLOCK_8X8, 1, 0);
1095 if (bsize < top_bsize)
1096 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1097 mi_row_top, mi_col_top, dst_buf, dst_stride);
1098
1099 // Second half
1100 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1101 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1102 top_bsize, BLOCK_8X8, 1, 1);
1103 if (bsize < top_bsize)
1104 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1105 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1106
1107 // weighted average to smooth the boundary
1108 xd->plane[0].dst.buf = dst_buf[0];
1109 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001110 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001111 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1112 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1113 0);
1114 } else {
1115 // First half
1116 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1117 mi_row_top, mi_col_top, dst_buf, dst_stride,
1118 top_bsize, subsize, 0, 0);
1119 if (bsize < top_bsize)
1120 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1121 mi_row_top, mi_col_top, dst_buf, dst_stride);
1122 else
1123 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1124 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1125
1126 if (mi_row + hbs < cm->mi_rows) {
1127 // Second half
1128 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col,
1129 mi_row + hbs, mi_col, mi_row_top, mi_col_top,
1130 dst_buf1, dst_stride1, top_bsize, subsize, 0, 0);
1131 if (bsize < top_bsize)
1132 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1133 mi_col, mi_row_top, mi_col_top, dst_buf1,
1134 dst_stride1);
1135 else
1136 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1137 mi_col, mi_row_top, mi_col_top, dst_buf1,
1138 dst_stride1, 1);
1139
1140 // weighted average to smooth the boundary
1141 for (i = 0; i < MAX_MB_PLANE; i++) {
1142 xd->plane[i].dst.buf = dst_buf[i];
1143 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001144 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001145 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1146 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1147 PARTITION_HORZ, i);
1148 }
1149 }
1150 }
1151 break;
1152 case PARTITION_VERT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001153 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001154 // First half
1155 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1156 mi_row_top, mi_col_top, dst_buf, dst_stride,
1157 top_bsize, BLOCK_8X8, 1, 0);
1158 if (bsize < top_bsize)
1159 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1160 mi_row_top, mi_col_top, dst_buf, dst_stride);
1161
1162 // Second half
1163 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1164 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1165 top_bsize, BLOCK_8X8, 1, 1);
1166 if (bsize < top_bsize)
1167 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1168 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1169
1170 // Smooth
1171 xd->plane[0].dst.buf = dst_buf[0];
1172 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001173 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001174 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1175 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1176 0);
1177 } else {
1178 // First half
1179 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1180 mi_row_top, mi_col_top, dst_buf, dst_stride,
1181 top_bsize, subsize, 0, 0);
1182 if (bsize < top_bsize)
1183 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1184 mi_row_top, mi_col_top, dst_buf, dst_stride);
1185 else
1186 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1187 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1188
1189 // Second half
1190 if (mi_col + hbs < cm->mi_cols) {
1191 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1192 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1193 dst_stride1, top_bsize, subsize, 0, 0);
1194 if (bsize < top_bsize)
1195 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1196 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1197 dst_stride1);
1198 else
1199 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1200 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1201 dst_stride1, 2);
1202
1203 // Smooth
1204 for (i = 0; i < MAX_MB_PLANE; i++) {
1205 xd->plane[i].dst.buf = dst_buf[i];
1206 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001207 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001208 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1209 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1210 PARTITION_VERT, i);
1211 }
1212 }
1213 }
1214 break;
1215 case PARTITION_SPLIT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001216 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001217 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1218 mi_row_top, mi_col_top, dst_buf, dst_stride,
1219 top_bsize, BLOCK_8X8, 1, 0);
1220 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1221 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1222 top_bsize, BLOCK_8X8, 1, 1);
1223 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1224 mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1225 top_bsize, BLOCK_8X8, 1, 1);
1226 dec_predict_b_extend(pbi, xd, tile, 3, mi_row, mi_col, mi_row, mi_col,
1227 mi_row_top, mi_col_top, dst_buf3, dst_stride3,
1228 top_bsize, BLOCK_8X8, 1, 1);
1229 if (bsize < top_bsize) {
1230 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1231 mi_row_top, mi_col_top, dst_buf, dst_stride);
1232 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1233 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1234 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1235 mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1236 dec_extend_all(pbi, xd, tile, 3, subsize, top_bsize, mi_row, mi_col,
1237 mi_row_top, mi_col_top, dst_buf3, dst_stride3);
1238 }
1239 } else {
1240 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row_top,
1241 mi_col_top, subsize, top_bsize, dst_buf,
1242 dst_stride);
1243 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1244 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col + hbs,
1245 mi_row_top, mi_col_top, subsize, top_bsize,
1246 dst_buf1, dst_stride1);
1247 if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols)
1248 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col,
1249 mi_row_top, mi_col_top, subsize, top_bsize,
1250 dst_buf2, dst_stride2);
1251 if (mi_row + hbs < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1252 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col + hbs,
1253 mi_row_top, mi_col_top, subsize, top_bsize,
1254 dst_buf3, dst_stride3);
1255 }
1256 for (i = 0; i < MAX_MB_PLANE; i++) {
Jingning Han9e0976a2016-12-27 17:52:42 -08001257#if !CONFIG_CB4X4
Jingning Han24f24a52016-12-27 10:13:28 -08001258 if (bsize == BLOCK_8X8 && i != 0)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001259 continue; // Skip <4x4 chroma smoothing
Jingning Han9e0976a2016-12-27 17:52:42 -08001260#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001261 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001262 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001263 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1264 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1265 PARTITION_VERT, i);
1266 if (mi_row + hbs < cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001267 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001268 xd, dst_buf2[i], dst_stride2[i], dst_buf3[i], dst_stride3[i],
1269 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1270 PARTITION_VERT, i);
Yaowu Xuf883b422016-08-30 14:01:10 -07001271 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001272 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1273 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1274 PARTITION_HORZ, i);
1275 }
1276 } else if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001277 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001278 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1279 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1280 PARTITION_HORZ, i);
1281 }
1282 }
1283 break;
1284#if CONFIG_EXT_PARTITION_TYPES
1285 case PARTITION_HORZ_A:
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, mi_col + hbs, mi_row,
1293 mi_col + hbs, 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, mi_col + hbs,
1296 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1297
1298 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1299 mi_col, 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 + hbs,
1303 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1304 else
1305 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1306 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1307 1);
1308
1309 for (i = 0; i < MAX_MB_PLANE; i++) {
1310 xd->plane[i].dst.buf = dst_buf[i];
1311 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001312 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001313 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1314 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1315 i);
1316 }
1317 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001318 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001319 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1320 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1321 i);
1322 }
1323 break;
1324 case PARTITION_VERT_A:
1325
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, bsize2, 0, 0);
1329 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1330 mi_row_top, mi_col_top, dst_buf, dst_stride);
1331
1332 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1333 mi_col, mi_row_top, mi_col_top, dst_buf1,
1334 dst_stride1, top_bsize, bsize2, 0, 0);
1335 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1336 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1337
1338 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1339 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1340 dst_stride2, top_bsize, subsize, 0, 0);
1341 if (bsize < top_bsize)
1342 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1343 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1344 dst_stride2);
1345 else
1346 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1347 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1348 dst_stride2, 2);
1349
1350 for (i = 0; i < MAX_MB_PLANE; i++) {
1351 xd->plane[i].dst.buf = dst_buf[i];
1352 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001353 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001354 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1355 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1356 i);
1357 }
1358 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001359 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001360 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1361 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1362 i);
1363 }
1364 break;
1365 case PARTITION_HORZ_B:
1366 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1367 mi_row_top, mi_col_top, dst_buf, dst_stride,
1368 top_bsize, subsize, 0, 0);
1369 if (bsize < top_bsize)
1370 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1371 mi_row_top, mi_col_top, dst_buf, dst_stride);
1372 else
1373 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1374 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1375
1376 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1377 mi_col, mi_row_top, mi_col_top, dst_buf1,
1378 dst_stride1, top_bsize, bsize2, 0, 0);
1379 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1380 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1381
1382 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1383 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1384 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1385 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1386 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1387 dst_stride2);
1388
1389 for (i = 0; i < MAX_MB_PLANE; i++) {
1390 xd->plane[i].dst.buf = dst_buf1[i];
1391 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001392 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001393 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1394 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1395 PARTITION_VERT, i);
1396 }
1397 for (i = 0; i < MAX_MB_PLANE; i++) {
1398 xd->plane[i].dst.buf = dst_buf[i];
1399 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001400 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001401 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1402 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1403 i);
1404 }
1405 break;
1406 case PARTITION_VERT_B:
1407 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1408 mi_row_top, mi_col_top, dst_buf, dst_stride,
1409 top_bsize, subsize, 0, 0);
1410 if (bsize < top_bsize)
1411 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1412 mi_row_top, mi_col_top, dst_buf, dst_stride);
1413 else
1414 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1415 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1416
1417 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1418 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1419 dst_stride1, top_bsize, bsize2, 0, 0);
1420 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1421 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1422
1423 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1424 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1425 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1426 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1427 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1428 dst_stride2);
1429
1430 for (i = 0; i < MAX_MB_PLANE; i++) {
1431 xd->plane[i].dst.buf = dst_buf1[i];
1432 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001433 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001434 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1435 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1436 PARTITION_HORZ, i);
1437 }
1438 for (i = 0; i < MAX_MB_PLANE; i++) {
1439 xd->plane[i].dst.buf = dst_buf[i];
1440 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001441 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001442 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1443 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1444 i);
1445 }
1446 break;
1447#endif // CONFIG_EXT_PARTITION_TYPES
1448 default: assert(0);
1449 }
1450}
1451
Yaowu Xuf883b422016-08-30 14:01:10 -07001452static void set_segment_id_supertx(const AV1_COMMON *const cm, const int mi_row,
1453 const int mi_col, const BLOCK_SIZE bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001454 const struct segmentation *seg = &cm->seg;
Jingning Han5b7706a2016-12-21 09:55:10 -08001455 const int miw = AOMMIN(mi_size_wide[bsize], cm->mi_cols - mi_col);
1456 const int mih = AOMMIN(mi_size_high[bsize], cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001457 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1458 MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
1459 int r, c;
1460 int seg_id_supertx = MAX_SEGMENTS;
1461
1462 if (!seg->enabled) {
1463 seg_id_supertx = 0;
1464 } else {
1465 // Find the minimum segment_id
1466 for (r = 0; r < mih; r++)
1467 for (c = 0; c < miw; c++)
1468 seg_id_supertx =
Yaowu Xuf883b422016-08-30 14:01:10 -07001469 AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001470 assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);
1471 }
1472
1473 // Assign the the segment_id back to segment_id_supertx
1474 for (r = 0; r < mih; r++)
1475 for (c = 0; c < miw; c++)
1476 mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
1477}
1478#endif // CONFIG_SUPERTX
1479
Yue Chen64550b62017-01-12 12:18:22 -08001480static void decode_mbmi_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001481#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001482 int supertx_enabled,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001483#endif // CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001484 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001485#if CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001486 PARTITION_TYPE partition,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001487#endif // CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001488 BLOCK_SIZE bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001489 AV1_COMMON *const cm = &pbi->common;
Jingning Han85dc03f2016-12-06 16:03:10 -08001490 const int bw = mi_size_wide[bsize];
1491 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -07001492 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1493 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Nathan E. Eggeebbd4792016-10-05 19:30:15 -04001494
Michael Bebenita6048d052016-08-25 14:40:54 -07001495#if CONFIG_ACCOUNTING
1496 aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
1497#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001498#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001499 if (supertx_enabled) {
Yue Chen64550b62017-01-12 12:18:22 -08001500 set_mb_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001501 } else {
Yue Chen64550b62017-01-12 12:18:22 -08001502 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001503 }
1504#if CONFIG_EXT_PARTITION_TYPES
1505 xd->mi[0]->mbmi.partition = partition;
1506#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001507 av1_read_mode_info(pbi, xd, supertx_enabled, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001508#else
Yue Chen64550b62017-01-12 12:18:22 -08001509 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001510#if CONFIG_EXT_PARTITION_TYPES
1511 xd->mi[0]->mbmi.partition = partition;
1512#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001513 av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001514#endif // CONFIG_SUPERTX
1515
1516 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1517 const BLOCK_SIZE uv_subsize =
1518 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1519 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -07001520 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001521 "Invalid block size.");
1522 }
1523
1524#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001525 xd->mi[0]->mbmi.segment_id_supertx = MAX_SEGMENTS;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001526#endif // CONFIG_SUPERTX
1527
Angie Chiangd0916d92017-03-10 17:54:18 -08001528 int reader_corrupted_flag = aom_reader_has_error(r);
1529 aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
Yue Chen64550b62017-01-12 12:18:22 -08001530}
1531
1532static void decode_token_and_recon_block(AV1Decoder *const pbi,
1533 MACROBLOCKD *const xd, int mi_row,
1534 int mi_col, aom_reader *r,
1535 BLOCK_SIZE bsize) {
1536 AV1_COMMON *const cm = &pbi->common;
1537 const int bw = mi_size_wide[bsize];
1538 const int bh = mi_size_high[bsize];
1539 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1540 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yue Chen64550b62017-01-12 12:18:22 -08001541
Angie Chiang44701f22017-02-27 10:36:44 -08001542 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
1543 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Yue Chen19e7aa82016-11-30 14:05:39 -08001544
Arild Fuldseth07441162016-08-15 15:07:52 +02001545#if CONFIG_DELTA_Q
1546 if (cm->delta_q_present_flag) {
1547 int i;
1548 for (i = 0; i < MAX_SEGMENTS; i++) {
1549 xd->plane[0].seg_dequant[i][0] =
1550 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
1551 xd->plane[0].seg_dequant[i][1] =
1552 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
1553 xd->plane[1].seg_dequant[i][0] =
1554 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1555 xd->plane[1].seg_dequant[i][1] =
1556 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1557 xd->plane[2].seg_dequant[i][0] =
1558 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1559 xd->plane[2].seg_dequant[i][1] =
1560 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1561 }
1562 }
1563#endif
1564
Jingning Han41bb3392016-12-14 10:46:48 -08001565#if CONFIG_CB4X4
1566 if (mbmi->skip) reset_skip_context(xd, bsize);
1567#else
Jingning Hand39cc722016-12-02 14:03:26 -08001568 if (mbmi->skip) reset_skip_context(xd, AOMMAX(BLOCK_8X8, bsize));
Jingning Han41bb3392016-12-14 10:46:48 -08001569#endif
Jingning Hand39cc722016-12-02 14:03:26 -08001570
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001571#if CONFIG_COEF_INTERLEAVE
1572 {
1573 const struct macroblockd_plane *const pd_y = &xd->plane[0];
1574 const struct macroblockd_plane *const pd_c = &xd->plane[1];
1575 const TX_SIZE tx_log2_y = mbmi->tx_size;
1576 const TX_SIZE tx_log2_c = get_uv_tx_size(mbmi, pd_c);
1577 const int tx_sz_y = (1 << tx_log2_y);
1578 const int tx_sz_c = (1 << tx_log2_c);
1579 const int num_4x4_w_y = pd_y->n4_w;
1580 const int num_4x4_h_y = pd_y->n4_h;
1581 const int num_4x4_w_c = pd_c->n4_w;
1582 const int num_4x4_h_c = pd_c->n4_h;
1583 const int max_4x4_w_y = get_max_4x4_size(num_4x4_w_y, xd->mb_to_right_edge,
1584 pd_y->subsampling_x);
1585 const int max_4x4_h_y = get_max_4x4_size(num_4x4_h_y, xd->mb_to_bottom_edge,
1586 pd_y->subsampling_y);
1587 const int max_4x4_w_c = get_max_4x4_size(num_4x4_w_c, xd->mb_to_right_edge,
1588 pd_c->subsampling_x);
1589 const int max_4x4_h_c = get_max_4x4_size(num_4x4_h_c, xd->mb_to_bottom_edge,
1590 pd_c->subsampling_y);
1591
1592 // The max_4x4_w/h may be smaller than tx_sz under some corner cases,
1593 // i.e. when the SB is splitted by tile boundaries.
1594 const int tu_num_w_y = (max_4x4_w_y + tx_sz_y - 1) / tx_sz_y;
1595 const int tu_num_h_y = (max_4x4_h_y + tx_sz_y - 1) / tx_sz_y;
1596 const int tu_num_w_c = (max_4x4_w_c + tx_sz_c - 1) / tx_sz_c;
1597 const int tu_num_h_c = (max_4x4_h_c + tx_sz_c - 1) / tx_sz_c;
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001598 const int tu_num_c = tu_num_w_c * tu_num_h_c;
1599
1600 if (!is_inter_block(mbmi)) {
1601 int tu_idx_c = 0;
1602 int row_y, col_y, row_c, col_c;
1603 int plane;
1604
1605#if CONFIG_PALETTE
1606 for (plane = 0; plane <= 1; ++plane) {
1607 if (mbmi->palette_mode_info.palette_size[plane])
1608 av1_decode_palette_tokens(xd, plane, r);
1609 }
1610#endif
1611
1612 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1613 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1614 // luma
1615 predict_and_reconstruct_intra_block(
1616 cm, xd, r, mbmi, 0, row_y * tx_sz_y, 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 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c,
1622 col_c, tx_log2_c);
1623 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c,
1624 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 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c, col_c,
1635 tx_log2_c);
1636 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c, col_c,
1637 tx_log2_c);
1638 tu_idx_c++;
1639 }
1640 } else {
1641 // Prediction
David Barkerac37fa32016-12-02 12:30:21 +00001642 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001643 AOMMAX(bsize, BLOCK_8X8));
1644
1645 // Reconstruction
1646 if (!mbmi->skip) {
1647 int eobtotal = 0;
1648 int tu_idx_c = 0;
1649 int row_y, col_y, row_c, col_c;
1650
1651 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1652 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1653 // luma
1654 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 0,
1655 row_y * tx_sz_y,
1656 col_y * tx_sz_y, tx_log2_y);
1657 // chroma
1658 if (tu_idx_c < tu_num_c) {
1659 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1660 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1661 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1662 1, row_c, col_c, tx_log2_c);
1663 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1664 2, row_c, col_c, tx_log2_c);
1665 tu_idx_c++;
1666 }
1667 }
1668 }
1669
1670 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1671 while (tu_idx_c < tu_num_c) {
1672 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1673 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1674 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 1,
1675 row_c, col_c, tx_log2_c);
1676 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 2,
1677 row_c, col_c, tx_log2_c);
1678 tu_idx_c++;
1679 }
1680
Alex Converse64d7ef62017-03-22 18:09:16 -07001681 // TODO(CONFIG_COEF_INTERLEAVE owners): bring eob == 0 corner case
1682 // into line with the defaut configuration
1683 if (bsize >= BLOCK_8X8 && eobtotal == 0) mbmi->skip = 1;
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001684 }
1685 }
1686 }
Angie Chiang133733c2017-03-17 12:50:20 -07001687#else // CONFIG_COEF_INTERLEAVE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001688 if (!is_inter_block(mbmi)) {
1689 int plane;
Fangwen Fub3be9262017-03-06 15:34:28 -08001690#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001691 for (plane = 0; plane <= 1; ++plane) {
1692 if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xuf883b422016-08-30 14:01:10 -07001693 av1_decode_palette_tokens(xd, plane, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001694 }
Fangwen Fub3be9262017-03-06 15:34:28 -08001695#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001696 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1697 const struct macroblockd_plane *const pd = &xd->plane[plane];
Angie Chiang7fcfee42017-02-24 15:51:03 -08001698 const TX_SIZE tx_size = get_tx_size(plane, xd);
Jingning Han2d64f122016-10-21 12:44:29 -07001699 const int stepr = tx_size_high_unit[tx_size];
1700 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han41bb3392016-12-14 10:46:48 -08001701#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001702#if CONFIG_CHROMA_2X2
1703 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1704#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001705 const BLOCK_SIZE plane_bsize =
1706 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001707#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001708#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001709 const BLOCK_SIZE plane_bsize =
1710 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001711#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001712 int row, col;
Jingning Hanbafee8d2016-12-02 10:25:03 -08001713 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1714 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001715#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001716 if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
1717 pd->subsampling_y))
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001718 continue;
1719#endif
1720
Yaowu Xuc27fc142016-08-22 16:08:15 -07001721 for (row = 0; row < max_blocks_high; row += stepr)
1722 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001723 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane, row, col,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001724 tx_size);
1725 }
1726 } else {
Yue Chen9ab6d712017-01-12 15:50:46 -08001727 int ref;
1728
1729 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
1730 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
Alex Converse28744302017-04-13 14:46:22 -07001731 if (frame < LAST_FRAME) {
1732#if CONFIG_INTRABC
1733 assert(is_intrabc_block(mbmi));
1734 assert(frame == INTRA_FRAME);
1735 assert(ref == 0);
1736#else
1737 assert(0);
1738#endif // CONFIG_INTRABC
1739 } else {
1740 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
Yue Chen9ab6d712017-01-12 15:50:46 -08001741
Alex Converse28744302017-04-13 14:46:22 -07001742 xd->block_refs[ref] = ref_buf;
1743 if ((!av1_is_valid_scale(&ref_buf->sf)))
1744 aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
1745 "Reference frame has invalid dimensions");
1746 av1_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
1747 &ref_buf->sf);
1748 }
Yue Chen9ab6d712017-01-12 15:50:46 -08001749 }
Yue Chen69f18e12016-09-08 14:48:15 -07001750
Jingning Han41bb3392016-12-14 10:46:48 -08001751#if CONFIG_CB4X4
Sarah Parker4c10a3c2017-04-10 19:37:59 -07001752 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL, bsize);
Jingning Han41bb3392016-12-14 10:46:48 -08001753#else
1754 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
1755 AOMMAX(bsize, BLOCK_8X8));
1756#endif
Sarah Parker4c10a3c2017-04-10 19:37:59 -07001757
Yue Chencb60b182016-10-13 15:18:22 -07001758#if CONFIG_MOTION_VAR
1759 if (mbmi->motion_mode == OBMC_CAUSAL) {
Yue Chenf27b1602017-01-13 11:11:43 -08001760#if CONFIG_NCOBMC
1761 av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
1762#else
Yue Chen894fcce2016-10-21 16:50:52 -07001763 av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yue Chenf27b1602017-01-13 11:11:43 -08001764#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001765 }
Yue Chencb60b182016-10-13 15:18:22 -07001766#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001767
1768 // Reconstruction
1769 if (!mbmi->skip) {
1770 int eobtotal = 0;
1771 int plane;
1772
1773 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1774 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han41bb3392016-12-14 10:46:48 -08001775#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001776#if CONFIG_CHROMA_2X2
1777 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1778#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001779 const BLOCK_SIZE plane_bsize =
1780 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001781#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001782#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001783 const BLOCK_SIZE plane_bsize =
1784 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001785#endif
Jingning Hanbafee8d2016-12-02 10:25:03 -08001786 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1787 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001788 int row, col;
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001789
1790#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001791 if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
1792 pd->subsampling_y))
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001793 continue;
1794#endif
1795
Yaowu Xuc27fc142016-08-22 16:08:15 -07001796#if CONFIG_VAR_TX
Jingning Han70e5f3f2016-11-09 17:03:07 -08001797 const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07001798 const int bh_var_tx = tx_size_high_unit[max_tx_size];
1799 const int bw_var_tx = tx_size_wide_unit[max_tx_size];
Jingning Hanbafee8d2016-12-02 10:25:03 -08001800 for (row = 0; row < max_blocks_high; row += bh_var_tx)
1801 for (col = 0; col < max_blocks_wide; col += bw_var_tx)
Jingning Hanfe45b212016-11-22 10:30:23 -08001802 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, row, col,
1803 max_tx_size, &eobtotal);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001804#else
Angie Chiang7fcfee42017-02-24 15:51:03 -08001805 const TX_SIZE tx_size = get_tx_size(plane, xd);
Jingning Han2d64f122016-10-21 12:44:29 -07001806 const int stepr = tx_size_high_unit[tx_size];
1807 const int stepc = tx_size_wide_unit[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001808 for (row = 0; row < max_blocks_high; row += stepr)
1809 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001810 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1811 plane, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001812#endif
1813 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001814 }
1815 }
Angie Chiang133733c2017-03-17 12:50:20 -07001816#endif // CONFIG_COEF_INTERLEAVE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001817
Angie Chiangd0916d92017-03-10 17:54:18 -08001818 int reader_corrupted_flag = aom_reader_has_error(r);
1819 aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001820}
1821
Yue Chen9ab6d712017-01-12 15:50:46 -08001822#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
1823static void detoken_and_recon_sb(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1824 int mi_row, int mi_col, aom_reader *r,
1825 BLOCK_SIZE bsize) {
1826 AV1_COMMON *const cm = &pbi->common;
1827 const int hbs = mi_size_wide[bsize] >> 1;
1828#if CONFIG_CB4X4
1829 const int unify_bsize = 1;
1830#else
1831 const int unify_bsize = 0;
1832#endif
1833#if CONFIG_EXT_PARTITION_TYPES
1834 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1835#endif
1836 PARTITION_TYPE partition;
1837 BLOCK_SIZE subsize;
1838 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1839 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1840
1841 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1842
1843 partition = get_partition(cm, mi_row, mi_col, bsize);
1844 subsize = subsize_lookup[partition][bsize];
1845
1846 if (!hbs && !unify_bsize) {
1847 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1848 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1849 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1850 } else {
1851 switch (partition) {
1852 case PARTITION_NONE:
1853 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
1854 break;
1855 case PARTITION_HORZ:
1856 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1857 if (has_rows)
1858 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r,
1859 subsize);
1860 break;
1861 case PARTITION_VERT:
1862 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1863 if (has_cols)
1864 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r,
1865 subsize);
1866 break;
1867 case PARTITION_SPLIT:
1868 detoken_and_recon_sb(pbi, xd, mi_row, mi_col, r, subsize);
1869 detoken_and_recon_sb(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1870 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1871 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col + hbs, r, subsize);
1872 break;
1873#if CONFIG_EXT_PARTITION_TYPES
1874 case PARTITION_HORZ_A:
1875 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1876 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1877 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1878 break;
1879 case PARTITION_HORZ_B:
1880 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1881 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1882 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1883 bsize2);
1884 break;
1885 case PARTITION_VERT_A:
1886 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1887 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1888 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1889 break;
1890 case PARTITION_VERT_B:
1891 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1892 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1893 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1894 bsize2);
1895 break;
1896#endif
1897 default: assert(0 && "Invalid partition type");
1898 }
1899 }
1900}
1901#endif
1902
Yue Chen64550b62017-01-12 12:18:22 -08001903static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1904#if CONFIG_SUPERTX
1905 int supertx_enabled,
1906#endif // CONFIG_SUPERTX
1907 int mi_row, int mi_col, aom_reader *r,
1908#if CONFIG_EXT_PARTITION_TYPES
1909 PARTITION_TYPE partition,
1910#endif // CONFIG_EXT_PARTITION_TYPES
1911 BLOCK_SIZE bsize) {
1912 decode_mbmi_block(pbi, xd,
1913#if CONFIG_SUPERTX
1914 supertx_enabled,
1915#endif
1916 mi_row, mi_col, r,
1917#if CONFIG_EXT_PARTITION_TYPES
1918 partition,
1919#endif
1920 bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001921#if !(CONFIG_MOTION_VAR && CONFIG_NCOBMC)
Yue Chen64550b62017-01-12 12:18:22 -08001922#if CONFIG_SUPERTX
1923 if (!supertx_enabled)
1924#endif // CONFIG_SUPERTX
1925 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001926#endif
Yue Chen64550b62017-01-12 12:18:22 -08001927}
1928
Yaowu Xuf883b422016-08-30 14:01:10 -07001929static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
1930 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001931 int has_rows, int has_cols,
Jingning Han1beb0102016-12-07 11:08:30 -08001932 BLOCK_SIZE bsize) {
Alex Converse55c6bde2017-01-12 15:55:31 -08001933#if CONFIG_UNPOISON_PARTITION_CTX
1934 const int ctx =
1935 partition_plane_context(xd, mi_row, mi_col, has_rows, has_cols, bsize);
Alex Converse2b9d19d2017-04-03 11:11:17 -07001936 const aom_prob *const probs =
1937 ctx < PARTITION_CONTEXTS ? cm->fc->partition_prob[ctx] : NULL;
1938 FRAME_COUNTS *const counts = ctx < PARTITION_CONTEXTS ? xd->counts : NULL;
Alex Converse55c6bde2017-01-12 15:55:31 -08001939#else
Jingning Han1beb0102016-12-07 11:08:30 -08001940 const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
Yaowu Xuf883b422016-08-30 14:01:10 -07001941 const aom_prob *const probs = cm->fc->partition_prob[ctx];
Alex Converse55c6bde2017-01-12 15:55:31 -08001942 FRAME_COUNTS *const counts = xd->counts;
1943#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001944 PARTITION_TYPE p;
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001945#if CONFIG_EC_ADAPT
1946 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1947 (void)cm;
1948#elif CONFIG_EC_MULTISYMBOL
1949 FRAME_CONTEXT *ec_ctx = cm->fc;
1950#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001951
Jingning Han5fe79db2017-03-27 15:10:30 -07001952#if CONFIG_EC_MULTISYMBOL
1953 aom_cdf_prob *partition_cdf = (ctx >= 0) ? ec_ctx->partition_cdf[ctx] : NULL;
1954#endif
1955
Yaowu Xuc27fc142016-08-22 16:08:15 -07001956 if (has_rows && has_cols)
1957#if CONFIG_EXT_PARTITION_TYPES
1958 if (bsize <= BLOCK_8X8)
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001959#if CONFIG_EC_MULTISYMBOL
Jingning Han5fe79db2017-03-27 15:10:30 -07001960 p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, PARTITION_TYPES,
1961 ACCT_STR);
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001962#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001963 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001964#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001965 else
Alex Converse57795a42017-03-14 12:18:25 -07001966#if CONFIG_EC_MULTISYMBOL
Jingning Han5fe79db2017-03-27 15:10:30 -07001967 p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, EXT_PARTITION_TYPES,
1968 ACCT_STR);
Alex Converse57795a42017-03-14 12:18:25 -07001969#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001970 p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs,
1971 ACCT_STR);
Alex Converse57795a42017-03-14 12:18:25 -07001972#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001973#else
Nathan E. Egge9d9eb6c2016-11-16 09:44:26 -05001974#if CONFIG_EC_MULTISYMBOL
Jingning Han5fe79db2017-03-27 15:10:30 -07001975 p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, PARTITION_TYPES,
1976 ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001977#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001978 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001979#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001980#endif // CONFIG_EXT_PARTITION_TYPES
1981 else if (!has_rows && has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001982 p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001983 else if (has_rows && !has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001984 p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001985 else
1986 p = PARTITION_SPLIT;
1987
1988 if (counts) ++counts->partition[ctx][p];
1989
1990 return p;
1991}
1992
1993#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001994static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
1995 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001996 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
1997 return 1;
1998 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001999 const int ctx = av1_get_skip_context(xd);
Michael Bebenita6048d052016-08-25 14:40:54 -07002000 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002001 FRAME_COUNTS *counts = xd->counts;
2002 if (counts) ++counts->skip[ctx][skip];
2003 return skip;
2004 }
2005}
2006#endif // CONFIG_SUPERTX
2007
2008// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -07002009static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002010#if CONFIG_SUPERTX
2011 int supertx_enabled,
2012#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002013 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002014 BLOCK_SIZE bsize, int n4x4_l2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002015 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002016 const int n8x8_l2 = n4x4_l2 - 1;
Jingning Hanff17e162016-12-07 17:58:18 -08002017 const int num_8x8_wh = mi_size_wide[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002018 const int hbs = num_8x8_wh >> 1;
Jingning Han41bb3392016-12-14 10:46:48 -08002019#if CONFIG_CB4X4
2020 const int unify_bsize = 1;
2021#else
2022 const int unify_bsize = 0;
2023#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002024 PARTITION_TYPE partition;
2025 BLOCK_SIZE subsize;
2026#if CONFIG_EXT_PARTITION_TYPES
2027 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
2028#endif
2029 const int has_rows = (mi_row + hbs) < cm->mi_rows;
2030 const int has_cols = (mi_col + hbs) < cm->mi_cols;
2031#if CONFIG_SUPERTX
2032 const int read_token = !supertx_enabled;
2033 int skip = 0;
Jingning Han2511c662016-12-22 11:57:34 -08002034 TX_SIZE supertx_size = max_txsize_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002035 const TileInfo *const tile = &xd->tile;
2036 int txfm = DCT_DCT;
2037#endif // CONFIG_SUPERTX
2038
2039 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
2040
Jingning Hancd959762017-03-27 14:49:59 -07002041 partition = (bsize < BLOCK_8X8) ? PARTITION_NONE
2042 : read_partition(cm, xd, mi_row, mi_col, r,
2043 has_rows, has_cols, bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002044 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
Yushin Cho77bba8d2016-11-04 16:36:56 -07002045
2046#if CONFIG_PVQ
2047 assert(partition < PARTITION_TYPES);
2048 assert(subsize < BLOCK_SIZES);
2049#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002050#if CONFIG_SUPERTX
2051 if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
2052 bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
2053 const int supertx_context = partition_supertx_context_lookup[partition];
Michael Bebenita6048d052016-08-25 14:40:54 -07002054 supertx_enabled = aom_read(
2055 r, cm->fc->supertx_prob[supertx_context][supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002056 if (xd->counts)
2057 xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
2058#if CONFIG_VAR_TX
2059 if (supertx_enabled) xd->supertx_size = supertx_size;
2060#endif
2061 }
2062#endif // CONFIG_SUPERTX
Jingning Han41bb3392016-12-14 10:46:48 -08002063 if (!hbs && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002064 // calculate bmode block dimensions (log 2)
2065 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
2066 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
2067 decode_block(pbi, xd,
2068#if CONFIG_SUPERTX
2069 supertx_enabled,
2070#endif // CONFIG_SUPERTX
2071 mi_row, mi_col, r,
2072#if CONFIG_EXT_PARTITION_TYPES
2073 partition,
2074#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002075 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002076 } else {
2077 switch (partition) {
2078 case PARTITION_NONE:
2079 decode_block(pbi, xd,
2080#if CONFIG_SUPERTX
2081 supertx_enabled,
2082#endif // CONFIG_SUPERTX
2083 mi_row, mi_col, r,
2084#if CONFIG_EXT_PARTITION_TYPES
2085 partition,
2086#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002087 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002088 break;
2089 case PARTITION_HORZ:
2090 decode_block(pbi, xd,
2091#if CONFIG_SUPERTX
2092 supertx_enabled,
2093#endif // CONFIG_SUPERTX
2094 mi_row, mi_col, r,
2095#if CONFIG_EXT_PARTITION_TYPES
2096 partition,
2097#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002098 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002099 if (has_rows)
2100 decode_block(pbi, xd,
2101#if CONFIG_SUPERTX
2102 supertx_enabled,
2103#endif // CONFIG_SUPERTX
2104 mi_row + hbs, mi_col, r,
2105#if CONFIG_EXT_PARTITION_TYPES
2106 partition,
2107#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002108 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002109 break;
2110 case PARTITION_VERT:
2111 decode_block(pbi, xd,
2112#if CONFIG_SUPERTX
2113 supertx_enabled,
2114#endif // CONFIG_SUPERTX
2115 mi_row, mi_col, r,
2116#if CONFIG_EXT_PARTITION_TYPES
2117 partition,
2118#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002119 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002120 if (has_cols)
2121 decode_block(pbi, xd,
2122#if CONFIG_SUPERTX
2123 supertx_enabled,
2124#endif // CONFIG_SUPERTX
2125 mi_row, mi_col + hbs, r,
2126#if CONFIG_EXT_PARTITION_TYPES
2127 partition,
2128#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002129 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002130 break;
2131 case PARTITION_SPLIT:
2132 decode_partition(pbi, xd,
2133#if CONFIG_SUPERTX
2134 supertx_enabled,
2135#endif // CONFIG_SUPERTX
2136 mi_row, mi_col, r, subsize, n8x8_l2);
2137 decode_partition(pbi, xd,
2138#if CONFIG_SUPERTX
2139 supertx_enabled,
2140#endif // CONFIG_SUPERTX
2141 mi_row, mi_col + hbs, r, subsize, n8x8_l2);
2142 decode_partition(pbi, xd,
2143#if CONFIG_SUPERTX
2144 supertx_enabled,
2145#endif // CONFIG_SUPERTX
2146 mi_row + hbs, mi_col, r, subsize, n8x8_l2);
2147 decode_partition(pbi, xd,
2148#if CONFIG_SUPERTX
2149 supertx_enabled,
2150#endif // CONFIG_SUPERTX
2151 mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
2152 break;
2153#if CONFIG_EXT_PARTITION_TYPES
2154 case PARTITION_HORZ_A:
2155 decode_block(pbi, xd,
2156#if CONFIG_SUPERTX
2157 supertx_enabled,
2158#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002159 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002160 decode_block(pbi, xd,
2161#if CONFIG_SUPERTX
2162 supertx_enabled,
2163#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002164 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002165 decode_block(pbi, xd,
2166#if CONFIG_SUPERTX
2167 supertx_enabled,
2168#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002169 mi_row + hbs, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002170 break;
2171 case PARTITION_HORZ_B:
2172 decode_block(pbi, xd,
2173#if CONFIG_SUPERTX
2174 supertx_enabled,
2175#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002176 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002177 decode_block(pbi, xd,
2178#if CONFIG_SUPERTX
2179 supertx_enabled,
2180#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002181 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002182 decode_block(pbi, xd,
2183#if CONFIG_SUPERTX
2184 supertx_enabled,
2185#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002186 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002187 break;
2188 case PARTITION_VERT_A:
2189 decode_block(pbi, xd,
2190#if CONFIG_SUPERTX
2191 supertx_enabled,
2192#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002193 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002194 decode_block(pbi, xd,
2195#if CONFIG_SUPERTX
2196 supertx_enabled,
2197#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002198 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002199 decode_block(pbi, xd,
2200#if CONFIG_SUPERTX
2201 supertx_enabled,
2202#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002203 mi_row, mi_col + hbs, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002204 break;
2205 case PARTITION_VERT_B:
2206 decode_block(pbi, xd,
2207#if CONFIG_SUPERTX
2208 supertx_enabled,
2209#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002210 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002211 decode_block(pbi, xd,
2212#if CONFIG_SUPERTX
2213 supertx_enabled,
2214#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002215 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002216 decode_block(pbi, xd,
2217#if CONFIG_SUPERTX
2218 supertx_enabled,
2219#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002220 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002221 break;
2222#endif
2223 default: assert(0 && "Invalid partition type");
2224 }
2225 }
2226
2227#if CONFIG_SUPERTX
2228 if (supertx_enabled && read_token) {
2229 uint8_t *dst_buf[3];
2230 int dst_stride[3], i;
2231 int offset = mi_row * cm->mi_stride + mi_col;
2232
2233 set_segment_id_supertx(cm, mi_row, mi_col, bsize);
2234
David Barker3aec8d62017-01-31 14:55:32 +00002235#if CONFIG_DELTA_Q
2236 if (cm->delta_q_present_flag) {
2237 for (i = 0; i < MAX_SEGMENTS; i++) {
2238 xd->plane[0].seg_dequant[i][0] =
2239 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
2240 xd->plane[0].seg_dequant[i][1] =
2241 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
2242 xd->plane[1].seg_dequant[i][0] =
2243 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2244 xd->plane[1].seg_dequant[i][1] =
2245 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2246 xd->plane[2].seg_dequant[i][0] =
2247 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2248 xd->plane[2].seg_dequant[i][1] =
2249 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2250 }
2251 }
2252#endif
2253
Yaowu Xuc27fc142016-08-22 16:08:15 -07002254 xd->mi = cm->mi_grid_visible + offset;
2255 xd->mi[0] = cm->mi + offset;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002256#if CONFIG_DEPENDENT_HORZTILES
2257 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2258 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols,
2259 cm->dependent_horz_tiles);
2260#else
Jingning Han5b7706a2016-12-21 09:55:10 -08002261 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2262 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002263#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002264 set_skip_context(xd, mi_row, mi_col);
2265 skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
2266 if (skip) {
2267 reset_skip_context(xd, bsize);
2268 } else {
2269#if CONFIG_EXT_TX
Sarah Parkere68a3e42017-02-16 14:03:24 -08002270 if (get_ext_tx_types(supertx_size, bsize, 1, cm->reduced_tx_set_used) >
2271 1) {
2272 const int eset =
2273 get_ext_tx_set(supertx_size, bsize, 1, cm->reduced_tx_set_used);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002274 if (eset > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002275 txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
Michael Bebenita6048d052016-08-25 14:40:54 -07002276 cm->fc->inter_ext_tx_prob[eset][supertx_size],
2277 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002278 if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
2279 }
2280 }
2281#else
2282 if (supertx_size < TX_32X32) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002283 txfm = aom_read_tree(r, av1_ext_tx_tree,
Jingning Han8f6eb182016-10-19 13:48:57 -07002284 cm->fc->inter_ext_tx_prob[supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002285 if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
2286 }
2287#endif // CONFIG_EXT_TX
2288 }
2289
Yaowu Xuf883b422016-08-30 14:01:10 -07002290 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002291 for (i = 0; i < MAX_MB_PLANE; i++) {
2292 dst_buf[i] = xd->plane[i].dst.buf;
2293 dst_stride[i] = xd->plane[i].dst.stride;
2294 }
2295 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
2296 bsize, dst_buf, dst_stride);
2297
2298 if (!skip) {
2299 int eobtotal = 0;
2300 MB_MODE_INFO *mbmi;
2301 set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
2302 mbmi = &xd->mi[0]->mbmi;
2303 mbmi->tx_type = txfm;
2304 assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
2305 for (i = 0; i < MAX_MB_PLANE; ++i) {
2306 const struct macroblockd_plane *const pd = &xd->plane[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002307 int row, col;
Angie Chiang7fcfee42017-02-24 15:51:03 -08002308 const TX_SIZE tx_size = get_tx_size(i, xd);
Jingning Han5b7706a2016-12-21 09:55:10 -08002309 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Jingning Han32b20282016-10-28 15:42:44 -07002310 const int stepr = tx_size_high_unit[tx_size];
2311 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han5b7706a2016-12-21 09:55:10 -08002312 const int max_blocks_wide = max_block_wide(xd, plane_bsize, i);
2313 const int max_blocks_high = max_block_high(xd, plane_bsize, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002314
2315 for (row = 0; row < max_blocks_high; row += stepr)
2316 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07002317 eobtotal += reconstruct_inter_block(
2318 cm, xd, r, mbmi->segment_id_supertx, i, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002319 }
Jingning Han24f24a52016-12-27 10:13:28 -08002320 if ((unify_bsize || !(subsize < BLOCK_8X8)) && eobtotal == 0) skip = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002321 }
2322 set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
2323 }
2324#endif // CONFIG_SUPERTX
2325
2326#if CONFIG_EXT_PARTITION_TYPES
Alex Converseffabff32017-03-27 09:52:19 -07002327 update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002328#else
2329 // update partition context
2330 if (bsize >= BLOCK_8X8 &&
2331 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
Jingning Han1beb0102016-12-07 11:08:30 -08002332 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
David Barkerf8935c92016-10-26 14:54:06 +01002333#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xud71be782016-10-14 08:47:03 -07002334
Jean-Marc Valin01435132017-02-18 14:12:53 -05002335#if CONFIG_CDEF
David Barker9739f362016-11-10 09:29:32 +00002336#if CONFIG_EXT_PARTITION
2337 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128) {
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002338 if (!sb_all_skip(cm, mi_row, mi_col)) {
Jean-Marc Valin1df427e2017-03-23 18:32:57 -04002339 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2340 aom_read_literal(r, cm->cdef_bits, ACCT_STR);
David Barker9739f362016-11-10 09:29:32 +00002341 } else {
Jean-Marc Valin1df427e2017-03-23 18:32:57 -04002342 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2343 0;
David Barker9739f362016-11-10 09:29:32 +00002344 }
2345 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64) {
2346#else
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002347 if (bsize == BLOCK_64X64) {
David Barker9739f362016-11-10 09:29:32 +00002348#endif
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002349 if (!sb_all_skip(cm, mi_row, mi_col)) {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002350 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2351 aom_read_literal(r, cm->cdef_bits, ACCT_STR);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002352 } else {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002353 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
Jean-Marc Valina8764952017-04-11 04:01:15 -04002354 -1;
Yaowu Xud71be782016-10-14 08:47:03 -07002355 }
2356 }
Jean-Marc Valin01435132017-02-18 14:12:53 -05002357#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002358}
2359
Yaowu Xuc27fc142016-08-22 16:08:15 -07002360static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
2361 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002362 struct aom_internal_error_info *error_info,
Alex Converseeb780e72016-12-13 12:46:41 -08002363 aom_reader *r,
2364#if CONFIG_ANS && ANS_MAX_SYMBOLS
2365 int window_size,
2366#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
2367 aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002368 // Validate the calculated partition length. If the buffer
2369 // described by the partition can't be fully read, then restrict
2370 // it to the portion that can be (for EC mode) or throw an error.
2371 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002372 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002373 "Truncated packet or corrupt tile length");
2374
Alex Converse2cdf0d82016-12-13 13:53:09 -08002375#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08002376 r->window_size = window_size;
Alex Converse2cdf0d82016-12-13 13:53:09 -08002377#endif
Alex Converse346440b2017-01-03 13:47:37 -08002378 if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07002379 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002380 "Failed to allocate bool decoder %d", 1);
2381}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002382
Angie Chiang800df032017-03-22 11:14:12 -07002383#if !CONFIG_PVQ && !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET) && !CONFIG_LV_MAP
Yaowu Xuf883b422016-08-30 14:01:10 -07002384static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
2385 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002386 int i, j, k, l, m;
Alex Conversea9598cd2017-02-03 14:18:05 -08002387#if CONFIG_EC_ADAPT
2388 const int node_limit = UNCONSTRAINED_NODES - 1;
2389#else
Thomas9ac55082016-09-23 18:04:17 +01002390 const int node_limit = UNCONSTRAINED_NODES;
Alex Conversea9598cd2017-02-03 14:18:05 -08002391#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002392
Michael Bebenita6048d052016-08-25 14:40:54 -07002393 if (aom_read_bit(r, ACCT_STR))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002394 for (i = 0; i < PLANE_TYPES; ++i)
2395 for (j = 0; j < REF_TYPES; ++j)
2396 for (k = 0; k < COEF_BANDS; ++k)
2397 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
Thomas9ac55082016-09-23 18:04:17 +01002398 for (m = 0; m < node_limit; ++m)
Michael Bebenita6048d052016-08-25 14:40:54 -07002399 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002400}
2401
Yaowu Xuf883b422016-08-30 14:01:10 -07002402static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002403 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
2404 TX_SIZE tx_size;
Jingning Han83630632016-12-16 11:27:25 -08002405 for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002406 read_coef_probs_common(fc->coef_probs[tx_size], r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002407}
Yushin Cho77bba8d2016-11-04 16:36:56 -07002408#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002409
Yaowu Xuf883b422016-08-30 14:01:10 -07002410static void setup_segmentation(AV1_COMMON *const cm,
2411 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002412 struct segmentation *const seg = &cm->seg;
2413 int i, j;
2414
2415 seg->update_map = 0;
2416 seg->update_data = 0;
2417
Yaowu Xuf883b422016-08-30 14:01:10 -07002418 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002419 if (!seg->enabled) return;
2420
2421 // Segmentation map update
2422 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2423 seg->update_map = 1;
2424 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002425 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002426 }
2427 if (seg->update_map) {
2428 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2429 seg->temporal_update = 0;
2430 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002431 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002432 }
2433 }
2434
2435 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07002436 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002437 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002438 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002439
Yaowu Xuf883b422016-08-30 14:01:10 -07002440 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002441
2442 for (i = 0; i < MAX_SEGMENTS; i++) {
2443 for (j = 0; j < SEG_LVL_MAX; j++) {
2444 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002445 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002446 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002447 av1_enable_segfeature(seg, i, j);
2448 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
2449 if (av1_is_segfeature_signed(j))
2450 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002451 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002452 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002453 }
2454 }
2455 }
2456}
2457
2458#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002459static void decode_restoration_mode(AV1_COMMON *cm,
2460 struct aom_read_bit_buffer *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002461 int p;
2462 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002463 if (aom_rb_read_bit(rb)) {
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002464 rsi->frame_restoration_type =
2465 aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002466 } else {
2467 rsi->frame_restoration_type =
2468 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
2469 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002470 for (p = 1; p < MAX_MB_PLANE; ++p) {
2471 cm->rst_info[p].frame_restoration_type =
2472 aom_rb_read_bit(rb) ? RESTORE_WIENER : RESTORE_NONE;
2473 }
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002474
2475 cm->rst_info[0].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2476 cm->rst_info[1].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2477 cm->rst_info[2].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2478 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
2479 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
2480 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
2481 rsi = &cm->rst_info[0];
2482 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2483 if (rsi->restoration_tilesize != RESTORATION_TILESIZE_MAX) {
2484 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2485 }
2486 cm->rst_info[1].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2487 cm->rst_info[2].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2488 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002489}
2490
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002491static void read_wiener_filter(WienerInfo *wiener_info, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002492 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002493 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2494 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002495 wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002496 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2497 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002498 wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002499 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2500 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002501 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002502 wiener_info->vfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002503 -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
2504 wiener_info->vfilter[2]);
2505
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002506 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002507 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2508 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002509 wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002510 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2511 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002512 wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002513 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2514 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002515 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002516 wiener_info->hfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002517 -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
2518 wiener_info->hfilter[2]);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002519}
2520
2521static void read_sgrproj_filter(SgrprojInfo *sgrproj_info, aom_reader *rb) {
2522 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
2523 sgrproj_info->xqd[0] =
2524 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN0;
2525 sgrproj_info->xqd[1] =
2526 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN1;
2527}
2528
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002529static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002530 int i, p;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002531 const int ntiles = av1_get_rest_ntiles(cm->width, cm->height,
2532 cm->rst_info[0].restoration_tilesize,
2533 NULL, NULL, NULL, NULL);
2534 const int ntiles_uv = av1_get_rest_ntiles(
2535 ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
2536 ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y),
2537 cm->rst_info[1].restoration_tilesize, NULL, NULL, NULL, NULL);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002538 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002539 if (rsi->frame_restoration_type != RESTORE_NONE) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002540 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002541 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002542 rsi->restoration_type[i] =
2543 aom_read_tree(rb, av1_switchable_restore_tree,
2544 cm->fc->switchable_restore_prob, ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002545 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002546 read_wiener_filter(&rsi->wiener_info[i], rb);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002547 } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002548 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002549 }
2550 }
2551 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002552 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002553 if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002554 rsi->restoration_type[i] = RESTORE_WIENER;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002555 read_wiener_filter(&rsi->wiener_info[i], rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002556 } else {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002557 rsi->restoration_type[i] = RESTORE_NONE;
2558 }
2559 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002560 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002561 for (i = 0; i < ntiles; ++i) {
2562 if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
2563 rsi->restoration_type[i] = RESTORE_SGRPROJ;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002564 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
2565 } else {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002566 rsi->restoration_type[i] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002567 }
2568 }
2569 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002570 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002571 for (p = 1; p < MAX_MB_PLANE; ++p) {
2572 rsi = &cm->rst_info[p];
2573 if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08002574 for (i = 0; i < ntiles_uv; ++i) {
2575 if (ntiles_uv > 1)
2576 rsi->restoration_type[i] =
2577 aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR) ? RESTORE_WIENER
2578 : RESTORE_NONE;
2579 else
2580 rsi->restoration_type[i] = RESTORE_WIENER;
2581 if (rsi->restoration_type[i] == RESTORE_WIENER) {
2582 read_wiener_filter(&rsi->wiener_info[i], rb);
2583 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002584 }
2585 }
2586 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002587}
2588#endif // CONFIG_LOOP_RESTORATION
2589
Yaowu Xuf883b422016-08-30 14:01:10 -07002590static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002591 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07002592 lf->filter_level = aom_rb_read_literal(rb, 6);
2593 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002594
2595 // Read in loop filter deltas applied at the MB level based on mode or ref
2596 // frame.
2597 lf->mode_ref_delta_update = 0;
2598
Yaowu Xuf883b422016-08-30 14:01:10 -07002599 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002600 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002601 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002602 if (lf->mode_ref_delta_update) {
2603 int i;
2604
2605 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002606 if (aom_rb_read_bit(rb))
2607 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002608
2609 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002610 if (aom_rb_read_bit(rb))
2611 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002612 }
2613 }
2614}
2615
Jean-Marc Valin01435132017-02-18 14:12:53 -05002616#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002617static void setup_cdef(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002618 int i;
Steinar Midtskogen0c966a52017-04-18 14:38:13 +02002619 cm->cdef_dering_damping = aom_rb_read_literal(rb, 1) + 5;
2620 cm->cdef_clpf_damping = aom_rb_read_literal(rb, 2) + 3;
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002621 cm->cdef_bits = aom_rb_read_literal(rb, 2);
2622 cm->nb_cdef_strengths = 1 << cm->cdef_bits;
2623 for (i = 0; i < cm->nb_cdef_strengths; i++) {
2624 cm->cdef_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
Jean-Marc Valine9f77422017-03-22 17:09:51 -04002625 cm->cdef_uv_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002626 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002627}
Jean-Marc Valin01435132017-02-18 14:12:53 -05002628#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002629
Yaowu Xuf883b422016-08-30 14:01:10 -07002630static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2631 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002632}
2633
Yaowu Xuf883b422016-08-30 14:01:10 -07002634static void setup_quantization(AV1_COMMON *const cm,
2635 struct aom_read_bit_buffer *rb) {
2636 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002637 cm->y_dc_delta_q = read_delta_q(rb);
2638 cm->uv_dc_delta_q = read_delta_q(rb);
2639 cm->uv_ac_delta_q = read_delta_q(rb);
2640 cm->dequant_bit_depth = cm->bit_depth;
2641#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002642 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002643 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002644 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2645 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002646 } else {
2647 cm->min_qmlevel = 0;
2648 cm->max_qmlevel = 0;
2649 }
2650#endif
2651}
2652
Yaowu Xuf883b422016-08-30 14:01:10 -07002653static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002654 // Build y/uv dequant values based on segmentation.
2655 int i = 0;
2656#if CONFIG_AOM_QM
2657 int lossless;
2658 int j = 0;
2659 int qmlevel;
2660 int using_qm = cm->using_qmatrix;
2661 int minqm = cm->min_qmlevel;
2662 int maxqm = cm->max_qmlevel;
2663#endif
2664#if CONFIG_NEW_QUANT
2665 int b;
2666 int dq;
2667#endif // CONFIG_NEW_QUANT
2668 if (cm->seg.enabled) {
2669 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002670 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002671 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002672 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2673 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002674 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002675 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002676 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002677 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002678#if CONFIG_AOM_QM
2679 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2680 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2681 // NB: depends on base index so there is only 1 set per frame
2682 // No quant weighting when lossless or signalled not using QM
2683 qmlevel = (lossless || using_qm == 0)
2684 ? NUM_QM_LEVELS - 1
2685 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2686 for (j = 0; j < TX_SIZES; ++j) {
2687 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2688 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2689 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2690 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2691 }
2692#endif // CONFIG_AOM_QM
2693#if CONFIG_NEW_QUANT
2694 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2695 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002696 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002697 cm->y_dequant_nuq[i][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002698 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002699 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002700 }
2701 }
2702#endif // CONFIG_NEW_QUANT
2703 }
2704 } else {
2705 const int qindex = cm->base_qindex;
2706 // When segmentation is disabled, only the first value is used. The
2707 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002708 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2709 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002710 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002711 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002712 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002713 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002714#if CONFIG_AOM_QM
2715 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2716 cm->uv_ac_delta_q == 0;
2717 // No quant weighting when lossless or signalled not using QM
2718 qmlevel = (lossless || using_qm == 0)
2719 ? NUM_QM_LEVELS - 1
2720 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2721 for (j = 0; j < TX_SIZES; ++j) {
2722 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2723 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2724 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2725 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2726 }
2727#endif
2728#if CONFIG_NEW_QUANT
2729 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2730 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002731 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002732 cm->y_dequant_nuq[0][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002733 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002734 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002735 }
2736 }
2737#endif // CONFIG_NEW_QUANT
2738 }
2739}
2740
Angie Chiang5678ad92016-11-21 09:38:40 -08002741static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002742 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07002743 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002744}
2745
Yaowu Xuf883b422016-08-30 14:01:10 -07002746static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002747 cm->render_width = cm->width;
2748 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002749 if (aom_rb_read_bit(rb))
2750 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002751}
2752
Fergus Simpsone7508412017-03-14 18:14:09 -07002753#if CONFIG_LOOP_RESTORATION && CONFIG_FRAME_SUPERRES
2754// TODO(afergs): make "struct aom_read_bit_buffer *const rb"?
2755static void setup_superres_size(AV1_COMMON *const cm,
2756 struct aom_read_bit_buffer *rb, int *width,
2757 int *height) {
2758 // TODO(afergs): Test this behaviour
2759 // Frame superres is probably in compatible with this render resolution
2760 assert(cm->width == cm->render_width && cm->height == cm->render_height);
2761
2762 cm->superres_width = cm->width;
2763 cm->superres_height = cm->height;
2764 if (aom_rb_read_bit(rb)) {
2765 cm->superres_scale_numerator =
2766 (uint8_t)aom_rb_read_literal(rb, SUPERRES_SCALE_BITS);
2767 cm->superres_scale_numerator += SUPERRES_SCALE_NUMERATOR_MIN;
2768 // Don't edit cm->width or cm->height directly, or the buffers won't get
2769 // resized correctly
2770 // TODO(afergs): Should the render resolution not be modified? It's the same
2771 // by default (ie. when it isn't sent)...
2772 // resize_context_buffers() will change cm->width to equal cm->render_width,
2773 // then they'll be the same again
2774 *width = cm->render_width =
2775 cm->width * cm->superres_scale_numerator / SUPERRES_SCALE_DENOMINATOR;
2776 *height = cm->render_height =
2777 cm->height * cm->superres_scale_numerator / SUPERRES_SCALE_DENOMINATOR;
2778 } else {
2779 // 1:1 scaling - ie. no scaling, scale not provided
2780 cm->superres_scale_numerator = SUPERRES_SCALE_DENOMINATOR;
2781 }
2782}
2783#endif // CONFIG_LOOP_RESTORATION && CONFIG_FRAME_SUPERRES
2784
Yaowu Xuf883b422016-08-30 14:01:10 -07002785static void resize_mv_buffer(AV1_COMMON *cm) {
2786 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002787 cm->cur_frame->mi_rows = cm->mi_rows;
2788 cm->cur_frame->mi_cols = cm->mi_cols;
2789 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002790 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002791 sizeof(*cm->cur_frame->mvs)));
2792}
2793
Yaowu Xuf883b422016-08-30 14:01:10 -07002794static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002795#if CONFIG_SIZE_LIMIT
2796 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002797 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002798 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2799 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2800#endif
2801 if (cm->width != width || cm->height != height) {
2802 const int new_mi_rows =
2803 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2804 const int new_mi_cols =
2805 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2806
Yaowu Xuf883b422016-08-30 14:01:10 -07002807 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002808 // dimensions as well as the overall size.
2809 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002810 if (av1_alloc_context_buffers(cm, width, height))
2811 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002812 "Failed to allocate context buffers");
2813 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002814 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002815 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002816 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002817 cm->width = width;
2818 cm->height = height;
2819 }
2820 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2821 cm->mi_cols > cm->cur_frame->mi_cols) {
2822 resize_mv_buffer(cm);
2823 }
2824}
2825
Yaowu Xuf883b422016-08-30 14:01:10 -07002826static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002827 int width, height;
2828 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002829 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002830 setup_render_size(cm, rb);
Fergus Simpsone7508412017-03-14 18:14:09 -07002831#if CONFIG_LOOP_RESTORATION && CONFIG_FRAME_SUPERRES
2832 setup_superres_size(cm, rb, &width, &height);
2833#endif // CONFIG_LOOP_RESTORATION && CONFIG_FRAME_SUPERRES
2834 resize_context_buffers(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002835
2836 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002837 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002838 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2839 cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002840#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002841 cm->use_highbitdepth,
2842#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002843 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002844 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2845 pool->cb_priv)) {
2846 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002847 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002848 "Failed to allocate frame buffer");
2849 }
2850 unlock_buffer_pool(pool);
2851
2852 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2853 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2854 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2855 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2856 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2857 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2858 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2859}
2860
Yaowu Xuf883b422016-08-30 14:01:10 -07002861static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002862 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002863 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002864 int this_xss, int this_yss) {
2865 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2866 ref_yss == this_yss;
2867}
2868
Yaowu Xuf883b422016-08-30 14:01:10 -07002869static void setup_frame_size_with_refs(AV1_COMMON *cm,
2870 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002871 int width, height;
2872 int found = 0, i;
2873 int has_valid_ref_frame = 0;
2874 BufferPool *const pool = cm->buffer_pool;
2875 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002876 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002877 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2878 width = buf->y_crop_width;
2879 height = buf->y_crop_height;
2880 cm->render_width = buf->render_width;
2881 cm->render_height = buf->render_height;
2882 found = 1;
2883 break;
2884 }
2885 }
2886
2887 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002888 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002889 setup_render_size(cm, rb);
2890 }
2891
2892 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002893 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002894 "Invalid frame size");
2895
2896 // Check to make sure at least one of frames that this frame references
2897 // has valid dimensions.
2898 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2899 RefBuffer *const ref_frame = &cm->frame_refs[i];
2900 has_valid_ref_frame |=
2901 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2902 ref_frame->buf->y_crop_height, width, height);
2903 }
2904 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002905 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002906 "Referenced frame has invalid size");
2907 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2908 RefBuffer *const ref_frame = &cm->frame_refs[i];
2909 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
2910 ref_frame->buf->subsampling_x,
2911 ref_frame->buf->subsampling_y, cm->bit_depth,
2912 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07002913 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002914 "Referenced frame has incompatible color format");
2915 }
2916
2917 resize_context_buffers(cm, width, height);
2918
2919 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002920 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002921 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2922 cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002923#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002924 cm->use_highbitdepth,
2925#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002926 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002927 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2928 pool->cb_priv)) {
2929 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002930 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002931 "Failed to allocate frame buffer");
2932 }
2933 unlock_buffer_pool(pool);
2934
2935 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2936 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2937 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2938 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2939 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2940 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2941 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2942}
2943
Yaowu Xuf883b422016-08-30 14:01:10 -07002944static void read_tile_info(AV1Decoder *const pbi,
2945 struct aom_read_bit_buffer *const rb) {
2946 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002947#if CONFIG_EXT_TILE
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08002948 cm->tile_encoding_mode = aom_rb_read_literal(rb, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002949// Read the tile width/height
2950#if CONFIG_EXT_PARTITION
2951 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002952 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
2953 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002954 } else
2955#endif // CONFIG_EXT_PARTITION
2956 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002957 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
2958 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002959 }
2960
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002961#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002962 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002963#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002964
Yaowu Xuc27fc142016-08-22 16:08:15 -07002965 cm->tile_width <<= cm->mib_size_log2;
2966 cm->tile_height <<= cm->mib_size_log2;
2967
Yaowu Xuf883b422016-08-30 14:01:10 -07002968 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
2969 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002970
2971 // Get the number of tiles
2972 cm->tile_cols = 1;
2973 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
2974
2975 cm->tile_rows = 1;
2976 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
2977
2978 if (cm->tile_cols * cm->tile_rows > 1) {
2979 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07002980 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2981 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002982 }
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002983
2984#if CONFIG_DEPENDENT_HORZTILES
2985 if (cm->tile_rows <= 1)
2986 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
2987 else
2988 cm->dependent_horz_tiles = 0;
2989#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002990#else
2991 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07002992 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002993
2994 // columns
2995 max_ones = max_log2_tile_cols - min_log2_tile_cols;
2996 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07002997 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002998
2999 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07003000 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003001 "Invalid number of tile columns");
3002
3003 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07003004 cm->log2_tile_rows = aom_rb_read_bit(rb);
3005 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003006#if CONFIG_DEPENDENT_HORZTILES
3007 if (cm->log2_tile_rows != 0)
3008 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
3009 else
3010 cm->dependent_horz_tiles = 0;
3011#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003012#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003013 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003014#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003015
Yaowu Xuc27fc142016-08-22 16:08:15 -07003016 cm->tile_cols = 1 << cm->log2_tile_cols;
3017 cm->tile_rows = 1 << cm->log2_tile_rows;
3018
3019 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
3020 cm->tile_width >>= cm->log2_tile_cols;
3021 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
3022 cm->tile_height >>= cm->log2_tile_rows;
3023
3024 // round to integer multiples of superblock size
3025 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
3026 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
3027
Thomas Davies4974e522016-11-07 17:44:05 +00003028// tile size magnitude
3029#if !CONFIG_TILE_GROUPS
3030 if (cm->tile_rows > 1 || cm->tile_cols > 1)
3031#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003032 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003033#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00003034
Thomas Davies80188d12016-10-26 16:08:35 -07003035#if CONFIG_TILE_GROUPS
3036 // Store an index to the location of the tile group information
3037 pbi->tg_size_bit_offset = rb->bit_offset;
3038 pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
3039 if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
3040 pbi->tg_start =
3041 aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3042 pbi->tg_size =
3043 1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3044 }
3045#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003046}
3047
3048static int mem_get_varsize(const uint8_t *src, const int sz) {
3049 switch (sz) {
3050 case 1: return src[0];
3051 case 2: return mem_get_le16(src);
3052 case 3: return mem_get_le24(src);
3053 case 4: return mem_get_le32(src);
3054 default: assert("Invalid size" && 0); return -1;
3055 }
3056}
3057
3058#if CONFIG_EXT_TILE
3059// Reads the next tile returning its size and adjusting '*data' accordingly
3060// based on 'is_last'.
3061static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07003062 struct aom_internal_error_info *error_info,
3063 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003064 void *decrypt_state,
3065 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08003066 int tile_size_bytes, int col, int row,
3067 unsigned int tile_encoding_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003068 size_t size;
3069
3070 size_t copy_size = 0;
3071 const uint8_t *copy_data = NULL;
3072
3073 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003074 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003075 "Truncated packet or corrupt tile length");
3076 if (decrypt_cb) {
3077 uint8_t be_data[4];
3078 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3079
3080 // Only read number of bytes in cm->tile_size_bytes.
3081 size = mem_get_varsize(be_data, tile_size_bytes);
3082 } else {
3083 size = mem_get_varsize(*data, tile_size_bytes);
3084 }
3085
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08003086 // If cm->tile_encoding_mode = 1 (i.e. TILE_VR), then the top bit of the tile
3087 // header indicates copy mode.
3088 if (tile_encoding_mode && (size >> (tile_size_bytes * 8 - 1)) == 1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003089 // The remaining bits in the top byte signal the row offset
3090 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
3091
3092 // Currently, only use tiles in same column as reference tiles.
3093 copy_data = tile_buffers[row - offset][col].data;
3094 copy_size = tile_buffers[row - offset][col].size;
3095 size = 0;
3096 }
3097
3098 *data += tile_size_bytes;
3099
3100 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003101 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003102 "Truncated packet or corrupt tile size");
3103
3104 if (size > 0) {
3105 tile_buffers[row][col].data = *data;
3106 tile_buffers[row][col].size = size;
3107 } else {
3108 tile_buffers[row][col].data = copy_data;
3109 tile_buffers[row][col].size = copy_size;
3110 }
3111
3112 *data += size;
3113
3114 tile_buffers[row][col].raw_data_end = *data;
3115}
3116
3117static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003118 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003119 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003120 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003121 const int tile_cols = cm->tile_cols;
3122 const int tile_rows = cm->tile_rows;
3123 const int have_tiles = tile_cols * tile_rows > 1;
3124
3125 if (!have_tiles) {
3126 const uint32_t tile_size = data_end - data;
3127 tile_buffers[0][0].data = data;
3128 tile_buffers[0][0].size = tile_size;
3129 tile_buffers[0][0].raw_data_end = NULL;
3130 } else {
3131 // We locate only the tile buffers that are required, which are the ones
3132 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
3133 // need the last (bottom right) tile buffer, as we need to know where the
3134 // end of the compressed frame buffer is for proper superframe decoding.
3135
3136 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
3137 const uint8_t *const data_start = data;
3138
Yaowu Xuf883b422016-08-30 14:01:10 -07003139 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003140 const int single_row = pbi->dec_tile_row >= 0;
3141 const int tile_rows_start = single_row ? dec_tile_row : 0;
3142 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003143 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003144 const int single_col = pbi->dec_tile_col >= 0;
3145 const int tile_cols_start = single_col ? dec_tile_col : 0;
3146 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3147
3148 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
3149 const int tile_size_bytes = pbi->tile_size_bytes;
3150
3151 size_t tile_col_size;
3152 int r, c;
3153
3154 // Read tile column sizes for all columns (we need the last tile buffer)
3155 for (c = 0; c < tile_cols; ++c) {
3156 const int is_last = c == tile_cols - 1;
3157 if (!is_last) {
3158 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
3159 data += tile_col_size_bytes;
3160 tile_col_data_end[c] = data + tile_col_size;
3161 } else {
3162 tile_col_size = data_end - data;
3163 tile_col_data_end[c] = data_end;
3164 }
3165 data += tile_col_size;
3166 }
3167
3168 data = data_start;
3169
3170 // Read the required tile sizes.
3171 for (c = tile_cols_start; c < tile_cols_end; ++c) {
3172 const int is_last = c == tile_cols - 1;
3173
3174 if (c > 0) data = tile_col_data_end[c - 1];
3175
3176 if (!is_last) data += tile_col_size_bytes;
3177
3178 // Get the whole of the last column, otherwise stop at the required tile.
3179 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
3180 tile_buffers[r][c].col = c;
3181
3182 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3183 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08003184 tile_size_bytes, c, r, cm->tile_encoding_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003185 }
3186 }
3187
3188 // If we have not read the last column, then read it to get the last tile.
3189 if (tile_cols_end != tile_cols) {
3190 c = tile_cols - 1;
3191
3192 data = tile_col_data_end[c - 1];
3193
3194 for (r = 0; r < tile_rows; ++r) {
3195 tile_buffers[r][c].col = c;
3196
3197 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3198 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08003199 tile_size_bytes, c, r, cm->tile_encoding_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003200 }
3201 }
3202 }
3203}
3204#else
3205// Reads the next tile returning its size and adjusting '*data' accordingly
3206// based on 'is_last'.
3207static void get_tile_buffer(const uint8_t *const data_end,
3208 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07003209 struct aom_internal_error_info *error_info,
3210 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003211 void *decrypt_state, TileBufferDec *const buf) {
3212 size_t size;
3213
3214 if (!is_last) {
Yaowu Xu0a79a1b2017-02-17 13:04:54 -08003215 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003216 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003217 "Truncated packet or corrupt tile length");
3218
3219 if (decrypt_cb) {
3220 uint8_t be_data[4];
3221 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3222 size = mem_get_varsize(be_data, tile_size_bytes);
3223 } else {
3224 size = mem_get_varsize(*data, tile_size_bytes);
3225 }
3226 *data += tile_size_bytes;
3227
3228 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003229 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003230 "Truncated packet or corrupt tile size");
3231 } else {
3232 size = data_end - *data;
3233 }
3234
3235 buf->data = *data;
3236 buf->size = size;
3237
3238 *data += size;
3239}
3240
3241static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003242 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003243 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003244 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07003245#if CONFIG_TILE_GROUPS
3246 int r, c;
3247 const int tile_cols = cm->tile_cols;
3248 const int tile_rows = cm->tile_rows;
3249 int tc = 0;
3250 int first_tile_in_tg = 0;
Thomas Davies80188d12016-10-26 16:08:35 -07003251 struct aom_read_bit_buffer rb_tg_hdr;
3252 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3253 const int num_tiles = tile_rows * tile_cols;
3254 const int num_bits = OD_ILOG(num_tiles) - 1;
James Zern6efba482017-04-20 20:53:49 -07003255 const size_t hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
Thomas Davies80188d12016-10-26 16:08:35 -07003256 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
Fangwen Fu73126c02017-02-08 22:37:47 -08003257#if CONFIG_DEPENDENT_HORZTILES
3258 int tile_group_start_col = 0;
3259 int tile_group_start_row = 0;
3260#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003261
3262 for (r = 0; r < tile_rows; ++r) {
3263 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07003264 TileBufferDec *const buf = &tile_buffers[r][c];
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003265 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
James Zern6efba482017-04-20 20:53:49 -07003266 const size_t hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
Thomas Davies80188d12016-10-26 16:08:35 -07003267
3268 buf->col = c;
3269 if (hdr_offset) {
3270 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
3271 rb_tg_hdr.bit_offset = tg_size_bit_offset;
3272 if (num_tiles) {
3273 pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
3274 pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
Fangwen Fu73126c02017-02-08 22:37:47 -08003275#if CONFIG_DEPENDENT_HORZTILES
3276 tile_group_start_row = r;
3277 tile_group_start_col = c;
3278#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003279 }
3280 }
3281 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
3282 data += hdr_offset;
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003283 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last,
3284 &pbi->common.error, &data, pbi->decrypt_cb,
3285 pbi->decrypt_state, buf);
Fangwen Fu73126c02017-02-08 22:37:47 -08003286#if CONFIG_DEPENDENT_HORZTILES
3287 cm->tile_group_start_row[r][c] = tile_group_start_row;
3288 cm->tile_group_start_col[r][c] = tile_group_start_col;
3289#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003290 }
3291 }
3292#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003293 int r, c;
3294 const int tile_cols = cm->tile_cols;
3295 const int tile_rows = cm->tile_rows;
3296
3297 for (r = 0; r < tile_rows; ++r) {
3298 for (c = 0; c < tile_cols; ++c) {
3299 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
3300 TileBufferDec *const buf = &tile_buffers[r][c];
3301 buf->col = c;
3302 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
3303 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
3304 }
3305 }
Thomas Davies80188d12016-10-26 16:08:35 -07003306#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003307}
3308#endif // CONFIG_EXT_TILE
3309
Yushin Cho77bba8d2016-11-04 16:36:56 -07003310#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003311static void daala_dec_init(AV1_COMMON *const cm, daala_dec_ctx *daala_dec,
Nathan E. Eggeab083972016-12-28 15:31:46 -05003312 aom_reader *r) {
3313 daala_dec->r = r;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003314
Yushin Cho70669122016-12-08 09:53:14 -10003315 // TODO(yushin) : activity masking info needs be signaled by a bitstream
3316 daala_dec->use_activity_masking = AV1_PVQ_ENABLE_ACTIVITY_MASKING;
3317
Yushin Cho7a428ba2017-01-12 16:28:49 -08003318#if !CONFIG_DAALA_DIST
3319 daala_dec->use_activity_masking = 0;
3320#endif
3321
Yushin Cho70669122016-12-08 09:53:14 -10003322 if (daala_dec->use_activity_masking)
3323 daala_dec->qm = OD_HVS_QM;
3324 else
3325 daala_dec->qm = OD_FLAT_QM;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003326
3327 od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
3328 daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
Yushin Cho70669122016-12-08 09:53:14 -10003329
3330 if (daala_dec->use_activity_masking) {
3331 int pli;
3332 int use_masking = daala_dec->use_activity_masking;
3333 int segment_id = 0;
3334 int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
3335
3336 for (pli = 0; pli < MAX_MB_PLANE; pli++) {
3337 int i;
3338 int q;
3339
3340 q = qindex;
3341 if (q <= OD_DEFAULT_QMS[use_masking][0][pli].interp_q << OD_COEFF_SHIFT) {
3342 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3343 &OD_DEFAULT_QMS[use_masking][0][pli], NULL);
3344 } else {
3345 i = 0;
3346 while (OD_DEFAULT_QMS[use_masking][i + 1][pli].qm_q4 != NULL &&
3347 q > OD_DEFAULT_QMS[use_masking][i + 1][pli].interp_q
3348 << OD_COEFF_SHIFT) {
3349 i++;
3350 }
3351 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3352 &OD_DEFAULT_QMS[use_masking][i][pli],
3353 &OD_DEFAULT_QMS[use_masking][i + 1][pli]);
3354 }
3355 }
3356 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07003357}
Yushin Cho70669122016-12-08 09:53:14 -10003358#endif // #if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003359
Yaowu Xuf883b422016-08-30 14:01:10 -07003360static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003361 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003362 AV1_COMMON *const cm = &pbi->common;
3363 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003364 const int tile_cols = cm->tile_cols;
3365 const int tile_rows = cm->tile_rows;
3366 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07003367 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003368#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003369 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003370 const int single_row = pbi->dec_tile_row >= 0;
3371 const int tile_rows_start = single_row ? dec_tile_row : 0;
3372 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003373 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003374 const int single_col = pbi->dec_tile_col >= 0;
3375 const int tile_cols_start = single_col ? dec_tile_col : 0;
3376 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3377 const int inv_col_order = pbi->inv_tile_order && !single_col;
3378 const int inv_row_order = pbi->inv_tile_order && !single_row;
3379#else
3380 const int tile_rows_start = 0;
3381 const int tile_rows_end = tile_rows;
3382 const int tile_cols_start = 0;
3383 const int tile_cols_end = tile_cols;
3384 const int inv_col_order = pbi->inv_tile_order;
3385 const int inv_row_order = pbi->inv_tile_order;
3386#endif // CONFIG_EXT_TILE
3387 int tile_row, tile_col;
3388
hui su0d103572017-03-01 17:58:01 -08003389#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003390 cm->do_subframe_update = n_tiles == 1;
hui su0d103572017-03-01 17:58:01 -08003391#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003392
3393 if (cm->lf.filter_level && !cm->skip_loop_filter &&
3394 pbi->lf_worker.data1 == NULL) {
3395 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07003396 aom_memalign(32, sizeof(LFWorkerData)));
3397 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003398 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003399 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003400 "Loop filter thread creation failed");
3401 }
3402 }
3403
3404 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3405 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3406 // Be sure to sync as we might be resuming after a failed frame decode.
3407 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003408 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
3409 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003410 }
3411
3412 assert(tile_rows <= MAX_TILE_ROWS);
3413 assert(tile_cols <= MAX_TILE_COLS);
3414
3415 get_tile_buffers(pbi, data, data_end, tile_buffers);
3416
3417 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003418 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003419 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003420 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003421 pbi->allocated_tiles = n_tiles;
3422 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003423#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003424 if (pbi->acct_enabled) {
3425 aom_accounting_reset(&pbi->accounting);
3426 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003427#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003428 // Load all tile information into tile_data.
3429 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3430 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3431 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
3432 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
3433
3434 td->cm = cm;
3435 td->xd = pbi->mb;
3436 td->xd.corrupted = 0;
3437 td->xd.counts =
3438 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3439 ? &cm->counts
3440 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003441 av1_zero(td->dqcoeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003442#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003443 av1_zero(td->pvq_ref_coeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003444#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003445 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003446 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003447 &td->bit_reader,
3448#if CONFIG_ANS && ANS_MAX_SYMBOLS
3449 1 << cm->ans_window_size_log2,
3450#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3451 pbi->decrypt_cb, pbi->decrypt_state);
Michael Bebenita6048d052016-08-25 14:40:54 -07003452#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003453 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003454 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003455 } else {
David Barkerd971f402016-10-25 13:52:07 +01003456 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003457 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003458#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003459 av1_init_macroblockd(cm, &td->xd,
3460#if CONFIG_PVQ
3461 td->pvq_ref_coeff,
3462#endif
3463 td->dqcoeff);
Yushin Choc49ef3a2017-03-13 17:27:25 -07003464
Thomas Daviesf77d4ad2017-01-10 18:55:42 +00003465#if CONFIG_EC_ADAPT
3466 // Initialise the tile context from the frame context
3467 td->tctx = *cm->fc;
3468 td->xd.tile_ctx = &td->tctx;
3469#endif
Yushin Choc49ef3a2017-03-13 17:27:25 -07003470
3471#if CONFIG_PVQ
3472 daala_dec_init(cm, &td->xd.daala_dec, &td->bit_reader);
3473 td->xd.daala_dec.state.adapt = &td->tctx.pvq_context;
3474#endif
3475
Urvang Joshib100db72016-10-12 16:28:56 -07003476#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003477 td->xd.plane[0].color_index_map = td->color_index_map[0];
3478 td->xd.plane[1].color_index_map = td->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003479#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003480 }
3481 }
3482
3483 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3484 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
3485 int mi_row = 0;
3486 TileInfo tile_info;
3487
Yaowu Xuf883b422016-08-30 14:01:10 -07003488 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003489
3490 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3491 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
3492 TileData *const td = pbi->tile_data + tile_cols * row + col;
Michael Bebenita6048d052016-08-25 14:40:54 -07003493#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003494 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003495 td->bit_reader.accounting->last_tell_frac =
3496 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003497 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003498#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003499
Yaowu Xuf883b422016-08-30 14:01:10 -07003500 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003501
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003502#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003503#if CONFIG_TILE_GROUPS
3504 av1_tile_set_tg_boundary(&tile_info, cm, tile_row, tile_col);
3505 if (!cm->dependent_horz_tiles || tile_row == 0 ||
3506 tile_info.tg_horz_boundary) {
3507#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003508 if (!cm->dependent_horz_tiles || tile_row == 0) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003509#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003510 av1_zero_above_context(cm, tile_info.mi_col_start,
3511 tile_info.mi_col_end);
3512 }
3513#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003514 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003515#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003516
3517 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
3518 mi_row += cm->mib_size) {
3519 int mi_col;
3520
Yaowu Xuf883b422016-08-30 14:01:10 -07003521 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003522
3523 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
3524 mi_col += cm->mib_size) {
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003525 av1_update_boundary_info(cm, &tile_info, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003526 decode_partition(pbi, &td->xd,
3527#if CONFIG_SUPERTX
3528 0,
3529#endif // CONFIG_SUPERTX
3530 mi_row, mi_col, &td->bit_reader, cm->sb_size,
3531 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003532#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3533 detoken_and_recon_sb(pbi, &td->xd, mi_row, mi_col, &td->bit_reader,
3534 cm->sb_size);
3535#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003536 }
Angie Chiangd0916d92017-03-10 17:54:18 -08003537 aom_merge_corrupted_flag(&pbi->mb.corrupted, td->xd.corrupted);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003538 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003539 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003540 "Failed to decode tile data");
hui su0d103572017-03-01 17:58:01 -08003541#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003542 if (cm->do_subframe_update &&
3543 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
hui su0161a932017-01-24 14:12:11 -08003544 const int mi_rows_per_update =
3545 MI_SIZE * AOMMAX(cm->mi_rows / MI_SIZE / COEF_PROBS_BUFS, 1);
3546 if ((mi_row + MI_SIZE) % mi_rows_per_update == 0 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003547 mi_row + MI_SIZE < cm->mi_rows &&
3548 cm->coef_probs_update_idx < COEF_PROBS_BUFS - 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003549 av1_partial_adapt_probs(cm, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003550 ++cm->coef_probs_update_idx;
3551 }
3552 }
hui su0d103572017-03-01 17:58:01 -08003553#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003554 }
3555 }
3556
3557 assert(mi_row > 0);
3558
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003559// when Parallel deblocking is enabled, deblocking should not
3560// be interleaved with decoding. Instead, deblocking should be done
3561// after the entire frame is decoded.
Jingning Han52ece882017-04-07 14:58:25 -07003562#if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING && !CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -07003563 // Loopfilter one tile row.
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08003564 // Note: If out-of-order tile decoding is used(for example, inv_row_order
3565 // = 1), the loopfiltering has be done after all tile rows are decoded.
3566 if (!inv_row_order && cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003567 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003568 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003569 const int lf_end = tile_info.mi_row_end - cm->mib_size;
3570
3571 // Delay the loopfilter if the first tile row is only
3572 // a single superblock high.
3573 if (lf_end <= 0) continue;
3574
3575 // Decoding has completed. Finish up the loop filter in this thread.
3576 if (tile_info.mi_row_end >= cm->mi_rows) continue;
3577
3578 winterface->sync(&pbi->lf_worker);
3579 lf_data->start = lf_start;
3580 lf_data->stop = lf_end;
3581 if (pbi->max_threads > 1) {
3582 winterface->launch(&pbi->lf_worker);
3583 } else {
3584 winterface->execute(&pbi->lf_worker);
3585 }
3586 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003587#endif // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003588
3589 // After loopfiltering, the last 7 row pixels in each superblock row may
3590 // still be changed by the longest loopfilter of the next superblock row.
3591 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003592 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003593 }
3594
Jingning Han52ece882017-04-07 14:58:25 -07003595#if CONFIG_VAR_TX || CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -07003596 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003597 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
3598 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003599#else
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003600#if CONFIG_PARALLEL_DEBLOCKING
3601 // Loopfilter all rows in the frame in the frame.
3602 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3603 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3604 winterface->sync(&pbi->lf_worker);
3605 lf_data->start = 0;
3606 lf_data->stop = cm->mi_rows;
3607 winterface->execute(&pbi->lf_worker);
3608 }
3609#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003610 // Loopfilter remaining rows in the frame.
3611 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3612 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3613 winterface->sync(&pbi->lf_worker);
3614 lf_data->start = lf_data->stop;
3615 lf_data->stop = cm->mi_rows;
3616 winterface->execute(&pbi->lf_worker);
3617 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003618#endif // CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003619#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003620 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003621 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003622
3623#if CONFIG_EXT_TILE
3624 if (n_tiles == 1) {
3625#if CONFIG_ANS
3626 return data_end;
3627#else
3628 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07003629 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003630#endif // CONFIG_ANS
3631 } else {
3632 // Return the end of the last tile buffer
3633 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3634 }
3635#else
3636#if CONFIG_ANS
3637 return data_end;
3638#else
3639 {
3640 // Get last tile data.
3641 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003642 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003643 }
3644#endif // CONFIG_ANS
3645#endif // CONFIG_EXT_TILE
3646}
3647
3648static int tile_worker_hook(TileWorkerData *const tile_data,
3649 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003650 AV1Decoder *const pbi = tile_data->pbi;
3651 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003652 int mi_row, mi_col;
3653
3654 if (setjmp(tile_data->error_info.jmp)) {
3655 tile_data->error_info.setjmp = 0;
Angie Chiangd0916d92017-03-10 17:54:18 -08003656 aom_merge_corrupted_flag(&tile_data->xd.corrupted, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003657 return 0;
3658 }
3659
3660 tile_data->error_info.setjmp = 1;
3661 tile_data->xd.error_info = &tile_data->error_info;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003662#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003663#if CONFIG_TILE_GROUPS
3664 if (!cm->dependent_horz_tiles || tile->tg_horz_boundary) {
3665#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003666 if (!cm->dependent_horz_tiles) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003667#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003668 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
3669 }
3670#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003671 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003672#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003673
3674 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
3675 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003676 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003677
3678 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3679 mi_col += cm->mib_size) {
3680 decode_partition(pbi, &tile_data->xd,
3681#if CONFIG_SUPERTX
3682 0,
3683#endif
3684 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
3685 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003686#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3687 detoken_and_recon_sb(pbi, &tile_data->xd, mi_row, mi_col,
3688 &tile_data->bit_reader, cm->sb_size);
3689#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003690 }
3691 }
3692 return !tile_data->xd.corrupted;
3693}
3694
3695// sorts in descending order
3696static int compare_tile_buffers(const void *a, const void *b) {
3697 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
3698 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
3699 return (int)(buf2->size - buf1->size);
3700}
3701
Yaowu Xuf883b422016-08-30 14:01:10 -07003702static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003703 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003704 AV1_COMMON *const cm = &pbi->common;
3705 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003706 const int tile_cols = cm->tile_cols;
3707 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003708 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
clang-format67948d32016-09-07 22:40:40 -07003709 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003710#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003711 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003712 const int single_row = pbi->dec_tile_row >= 0;
3713 const int tile_rows_start = single_row ? dec_tile_row : 0;
3714 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003715 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003716 const int single_col = pbi->dec_tile_col >= 0;
3717 const int tile_cols_start = single_col ? dec_tile_col : 0;
3718 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3719#else
3720 const int tile_rows_start = 0;
3721 const int tile_rows_end = tile_rows;
3722 const int tile_cols_start = 0;
3723 const int tile_cols_end = tile_cols;
3724#endif // CONFIG_EXT_TILE
3725 int tile_row, tile_col;
3726 int i;
3727
3728#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3729 int final_worker = -1;
3730#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3731
3732 assert(tile_rows <= MAX_TILE_ROWS);
3733 assert(tile_cols <= MAX_TILE_COLS);
3734
3735 assert(tile_cols * tile_rows > 1);
3736
Yaowu Xuc27fc142016-08-22 16:08:15 -07003737 // TODO(jzern): See if we can remove the restriction of passing in max
3738 // threads to the decoder.
3739 if (pbi->num_tile_workers == 0) {
3740 const int num_threads = pbi->max_threads & ~1;
3741 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07003742 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003743 // Ensure tile data offsets will be properly aligned. This may fail on
3744 // platforms without DECLARE_ALIGNED().
3745 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3746 CHECK_MEM_ERROR(
3747 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003748 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003749 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07003750 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003751 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003752 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003753 ++pbi->num_tile_workers;
3754
3755 winterface->init(worker);
3756 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003757 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003758 "Tile decoder thread creation failed");
3759 }
3760 }
3761 }
3762
3763 // Reset tile decoding hook
3764 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003765 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003766 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003767 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003768 worker->data1 = &pbi->tile_worker_data[i];
3769 worker->data2 = &pbi->tile_worker_info[i];
3770 }
3771
3772 // Initialize thread frame counts.
3773 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3774 for (i = 0; i < num_workers; ++i) {
3775 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003776 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003777 }
3778 }
3779
3780 // Load tile data into tile_buffers
3781 get_tile_buffers(pbi, data, data_end, tile_buffers);
3782
3783 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3784 // Sort the buffers in this tile row based on size in descending order.
3785 qsort(&tile_buffers[tile_row][tile_cols_start],
3786 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
3787 compare_tile_buffers);
3788
3789 // Rearrange the tile buffers in this tile row such that per-tile group
3790 // the largest, and presumably the most difficult tile will be decoded in
3791 // the main thread. This should help minimize the number of instances
3792 // where the main thread is waiting for a worker to complete.
3793 {
3794 int group_start;
3795 for (group_start = tile_cols_start; group_start < tile_cols_end;
3796 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003797 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003798 const TileBufferDec largest = tile_buffers[tile_row][group_start];
3799 memmove(&tile_buffers[tile_row][group_start],
3800 &tile_buffers[tile_row][group_start + 1],
3801 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
3802 tile_buffers[tile_row][group_end - 1] = largest;
3803 }
3804 }
3805
3806 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
3807 // Launch workers for individual columns
3808 for (i = 0; i < num_workers && tile_col < tile_cols_end;
3809 ++i, ++tile_col) {
3810 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07003811 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003812 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
3813 TileInfo *const tile_info = (TileInfo *)worker->data2;
3814
3815 twd->pbi = pbi;
3816 twd->xd = pbi->mb;
3817 twd->xd.corrupted = 0;
3818 twd->xd.counts =
3819 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3820 ? &twd->counts
3821 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003822 av1_zero(twd->dqcoeff);
3823 av1_tile_init(tile_info, cm, tile_row, buf->col);
3824 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003825 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003826 &twd->bit_reader,
3827#if CONFIG_ANS && ANS_MAX_SYMBOLS
3828 1 << cm->ans_window_size_log2,
3829#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3830 pbi->decrypt_cb, pbi->decrypt_state);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003831 av1_init_macroblockd(cm, &twd->xd,
3832#if CONFIG_PVQ
3833 twd->pvq_ref_coeff,
3834#endif
3835 twd->dqcoeff);
3836#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003837 daala_dec_init(cm, &twd->xd.daala_dec, &twd->bit_reader);
Yushin Choc49ef3a2017-03-13 17:27:25 -07003838 twd->xd.daala_dec.state.adapt = &twd->tctx.pvq_context;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003839#endif
Yushin Chod767beb2017-03-24 10:15:47 -07003840#if CONFIG_EC_ADAPT
3841 // Initialise the tile context from the frame context
3842 twd->tctx = *cm->fc;
3843 twd->xd.tile_ctx = &twd->tctx;
3844#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003845#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003846 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
3847 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003848#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003849
3850 worker->had_error = 0;
3851 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
3852 winterface->execute(worker);
3853 } else {
3854 winterface->launch(worker);
3855 }
3856
3857#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3858 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
3859 final_worker = i;
3860 }
3861#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3862 }
3863
3864 // Sync all workers
3865 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003866 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003867 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07003868 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07003869 // info in cm. Additionally once the threads have been synced and an
3870 // error is detected, there's no point in continuing to decode tiles.
3871 pbi->mb.corrupted |= !winterface->sync(worker);
3872 }
3873 }
3874 }
3875
3876 // Accumulate thread frame counts.
3877 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3878 for (i = 0; i < num_workers; ++i) {
3879 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003880 av1_accumulate_frame_counts(&cm->counts, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003881 }
3882 }
3883
3884#if CONFIG_EXT_TILE
3885 // Return the end of the last tile buffer
3886 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3887#else
3888#if CONFIG_ANS
3889 return data_end;
3890#else
3891 assert(final_worker != -1);
3892 {
3893 TileWorkerData *const twd =
3894 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003895 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003896 }
3897#endif // CONFIG_ANS
3898#endif // CONFIG_EXT_TILE
3899}
3900
3901static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003902 AV1_COMMON *const cm = (AV1_COMMON *)data;
3903 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003904}
3905
Yaowu Xuf883b422016-08-30 14:01:10 -07003906static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3907 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003908 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003909 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003910 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003911 cm->bit_depth = AOM_BITS_8;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003912 }
3913
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003914#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003915 if (cm->bit_depth > AOM_BITS_8) {
3916 cm->use_highbitdepth = 1;
3917 } else {
Yaowu Xu345a22d2017-02-27 09:23:52 -08003918#if CONFIG_LOWBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003919 cm->use_highbitdepth = 0;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003920#else
3921 cm->use_highbitdepth = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003922#endif
3923 }
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003924#endif
3925
Yaowu Xuf883b422016-08-30 14:01:10 -07003926 cm->color_space = aom_rb_read_literal(rb, 3);
3927 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003928 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07003929 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003930 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003931 cm->subsampling_x = aom_rb_read_bit(rb);
3932 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003933 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003934 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003935 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07003936 if (aom_rb_read_bit(rb))
3937 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003938 "Reserved bit set");
3939 } else {
3940 cm->subsampling_y = cm->subsampling_x = 1;
3941 }
3942 } else {
3943 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
3944 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
3945 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
3946 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003947 if (aom_rb_read_bit(rb))
3948 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003949 "Reserved bit set");
3950 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003951 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003952 "4:4:4 color not supported in profile 0 or 2");
3953 }
3954 }
3955}
3956
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003957#if CONFIG_REFERENCE_BUFFER
3958void read_sequence_header(SequenceHeader *seq_params) {
3959 /* Placeholder for actually reading from the bitstream */
3960 seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
3961 seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
3962 seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
3963}
3964#endif
3965
Yaowu Xuf883b422016-08-30 14:01:10 -07003966static size_t read_uncompressed_header(AV1Decoder *pbi,
3967 struct aom_read_bit_buffer *rb) {
3968 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003969 MACROBLOCKD *const xd = &pbi->mb;
3970 BufferPool *const pool = cm->buffer_pool;
3971 RefCntBuffer *const frame_bufs = pool->frame_bufs;
3972 int i, mask, ref_index = 0;
3973 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003974
3975#if CONFIG_REFERENCE_BUFFER
3976 /* TODO: Move outside frame loop or inside key-frame branch */
3977 read_sequence_header(&pbi->seq_params);
3978#endif
3979
Yaowu Xuc27fc142016-08-22 16:08:15 -07003980 cm->last_frame_type = cm->frame_type;
3981 cm->last_intra_only = cm->intra_only;
3982
3983#if CONFIG_EXT_REFS
3984 // NOTE: By default all coded frames to be used as a reference
3985 cm->is_reference_frame = 1;
3986#endif // CONFIG_EXT_REFS
3987
Yaowu Xuf883b422016-08-30 14:01:10 -07003988 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
3989 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003990 "Invalid frame marker");
3991
Yaowu Xuf883b422016-08-30 14:01:10 -07003992 cm->profile = av1_read_profile(rb);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003993#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003994 if (cm->profile >= MAX_PROFILES)
Yaowu Xuf883b422016-08-30 14:01:10 -07003995 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003996 "Unsupported bitstream profile");
3997#else
3998 if (cm->profile >= PROFILE_2)
Yaowu Xuf883b422016-08-30 14:01:10 -07003999 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004000 "Unsupported bitstream profile");
4001#endif
4002
Yaowu Xuf883b422016-08-30 14:01:10 -07004003 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004004
4005 if (cm->show_existing_frame) {
Yaowu Xu415ba932016-12-27 11:17:32 -08004006 // Show an existing frame directly.
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004007 const int existing_frame_idx = aom_rb_read_literal(rb, 3);
4008 const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
Yaowu Xu415ba932016-12-27 11:17:32 -08004009#if CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004010 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004011 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4012 int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004013 /* Compare display_frame_id with ref_frame_id and check valid for
4014 * referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004015 if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
4016 cm->valid_for_referencing[existing_frame_idx] == 0)
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004017 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4018 "Reference buffer frame ID mismatch");
4019 }
4020#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004021 lock_buffer_pool(pool);
4022 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
4023 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07004024 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004025 "Buffer %d does not contain a decoded frame",
4026 frame_to_show);
4027 }
4028 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
4029 unlock_buffer_pool(pool);
4030
4031 cm->lf.filter_level = 0;
4032 cm->show_frame = 1;
4033 pbi->refresh_frame_flags = 0;
4034
4035 if (cm->frame_parallel_decode) {
4036 for (i = 0; i < REF_FRAMES; ++i)
4037 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
4038 }
4039
4040 return 0;
4041 }
4042
Yaowu Xuf883b422016-08-30 14:01:10 -07004043 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
4044 cm->show_frame = aom_rb_read_bit(rb);
4045 cm->error_resilient_mode = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004046#if CONFIG_REFERENCE_BUFFER
4047 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004048 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4049 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4050 int prev_frame_id = 0;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004051 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004052 prev_frame_id = cm->current_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004053 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004054 cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004055
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004056 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004057 int diff_frame_id;
4058 if (cm->current_frame_id > prev_frame_id) {
4059 diff_frame_id = cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004060 } else {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004061 diff_frame_id =
4062 (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004063 }
4064 /* Check current_frame_id for conformance */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004065 if (prev_frame_id == cm->current_frame_id ||
4066 diff_frame_id >= (1 << (frame_id_length - 1))) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004067 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4068 "Invalid value of current_frame_id");
4069 }
4070 }
4071 /* Check if some frames need to be marked as not valid for referencing */
4072 for (i = 0; i < REF_FRAMES; i++) {
4073 if (cm->frame_type == KEY_FRAME) {
4074 cm->valid_for_referencing[i] = 0;
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004075 } else if (cm->current_frame_id - (1 << diff_len) > 0) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004076 if (cm->ref_frame_id[i] > cm->current_frame_id ||
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004077 cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004078 cm->valid_for_referencing[i] = 0;
4079 } else {
4080 if (cm->ref_frame_id[i] > cm->current_frame_id &&
4081 cm->ref_frame_id[i] <
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004082 (1 << frame_id_length) + cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004083 cm->valid_for_referencing[i] = 0;
4084 }
4085 }
4086 }
4087#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004088 if (cm->frame_type == KEY_FRAME) {
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 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
4095
4096 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4097 cm->frame_refs[i].idx = INVALID_IDX;
4098 cm->frame_refs[i].buf = NULL;
4099 }
4100
4101 setup_frame_size(cm, rb);
4102 if (pbi->need_resync) {
4103 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4104 pbi->need_resync = 0;
4105 }
Alex Converseeb780e72016-12-13 12:46:41 -08004106#if CONFIG_ANS && ANS_MAX_SYMBOLS
4107 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4108#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
Urvang Joshib100db72016-10-12 16:28:56 -07004109#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004110 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004111#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004112 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004113 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004114#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004115 if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004116#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004117 if (cm->error_resilient_mode) {
4118 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
4119 } else {
4120 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004121 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004122 ? RESET_FRAME_CONTEXT_ALL
4123 : RESET_FRAME_CONTEXT_CURRENT;
4124 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004125 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004126 ? RESET_FRAME_CONTEXT_CURRENT
4127 : RESET_FRAME_CONTEXT_NONE;
4128 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07004129 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004130 ? RESET_FRAME_CONTEXT_ALL
4131 : RESET_FRAME_CONTEXT_CURRENT;
4132 }
4133 }
4134
4135 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004136 if (!av1_read_sync_code(rb))
4137 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004138 "Invalid frame sync code");
4139
4140 read_bitdepth_colorspace_sampling(cm, rb);
4141
Yaowu Xuf883b422016-08-30 14:01:10 -07004142 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004143 setup_frame_size(cm, rb);
4144 if (pbi->need_resync) {
4145 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4146 pbi->need_resync = 0;
4147 }
Alex Converseeb780e72016-12-13 12:46:41 -08004148#if CONFIG_ANS && ANS_MAX_SYMBOLS
4149 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4150#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004151 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Yaowu Xuf883b422016-08-30 14:01:10 -07004152 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004153
4154#if CONFIG_EXT_REFS
4155 if (!pbi->refresh_frame_flags) {
4156 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
4157 // will not be used as a reference
4158 cm->is_reference_frame = 0;
4159 }
4160#endif // CONFIG_EXT_REFS
4161
4162 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004163 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004164 const int idx = cm->ref_frame_map[ref];
4165 RefBuffer *const ref_frame = &cm->frame_refs[i];
4166 ref_frame->idx = idx;
4167 ref_frame->buf = &frame_bufs[idx].buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07004168 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004169#if CONFIG_REFERENCE_BUFFER
4170 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004171 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4172 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4173 int delta_frame_id_minus1 = aom_rb_read_literal(rb, diff_len);
4174 int ref_frame_id =
4175 ((cm->current_frame_id - (delta_frame_id_minus1 + 1) +
4176 (1 << frame_id_length)) %
4177 (1 << frame_id_length));
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004178 /* Compare values derived from delta_frame_id_minus1 and
4179 * refresh_frame_flags. Also, check valid for referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004180 if (ref_frame_id != cm->ref_frame_id[ref] ||
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004181 cm->valid_for_referencing[ref] == 0)
4182 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4183 "Reference buffer frame ID mismatch");
4184 }
4185#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004186 }
4187
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004188#if CONFIG_FRAME_SIZE
4189 if (cm->error_resilient_mode == 0) {
4190 setup_frame_size_with_refs(cm, rb);
4191 } else {
4192 setup_frame_size(cm, rb);
4193 }
4194#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004195 setup_frame_size_with_refs(cm, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004196#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004197
Yaowu Xuf883b422016-08-30 14:01:10 -07004198 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Angie Chiang5678ad92016-11-21 09:38:40 -08004199 cm->interp_filter = read_frame_interp_filter(rb);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004200#if CONFIG_TEMPMV_SIGNALING
4201 if (!cm->error_resilient_mode) {
4202 cm->use_prev_frame_mvs = aom_rb_read_bit(rb);
4203 }
4204#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004205 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4206 RefBuffer *const ref_buf = &cm->frame_refs[i];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004207#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07004208 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004209 &ref_buf->sf, ref_buf->buf->y_crop_width,
4210 ref_buf->buf->y_crop_height, cm->width, cm->height,
4211 cm->use_highbitdepth);
4212#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004213 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004214 &ref_buf->sf, ref_buf->buf->y_crop_width,
4215 ref_buf->buf->y_crop_height, cm->width, cm->height);
4216#endif
4217 }
4218 }
4219 }
Fangwen Fu8d164de2016-12-14 13:40:54 -08004220#if CONFIG_TEMPMV_SIGNALING
4221 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
4222#endif
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004223
4224#if CONFIG_REFERENCE_BUFFER
4225 if (pbi->seq_params.frame_id_numbers_present_flag) {
4226 /* If bitmask is set, update reference frame id values and
4227 mark frames as valid for reference */
4228 int refresh_frame_flags =
4229 cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
4230 for (i = 0; i < REF_FRAMES; i++) {
4231 if ((refresh_frame_flags >> i) & 1) {
4232 cm->ref_frame_id[i] = cm->current_frame_id;
4233 cm->valid_for_referencing[i] = 1;
4234 }
4235 }
4236 }
4237#endif
4238
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004239#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004240 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
4241#endif
4242 get_frame_new_buffer(cm)->color_space = cm->color_space;
4243 get_frame_new_buffer(cm)->color_range = cm->color_range;
4244 get_frame_new_buffer(cm)->render_width = cm->render_width;
4245 get_frame_new_buffer(cm)->render_height = cm->render_height;
4246
4247 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004248 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004249 "Keyframe / intra-only frame required to reset decoder"
4250 " state");
4251 }
4252
4253 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004254 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004255 ? REFRESH_FRAME_CONTEXT_FORWARD
4256 : REFRESH_FRAME_CONTEXT_BACKWARD;
4257 } else {
4258 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
4259 }
4260
Yaowu Xuf883b422016-08-30 14:01:10 -07004261 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07004262 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07004263 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004264
4265 // Generate next_ref_frame_map.
4266 lock_buffer_pool(pool);
4267 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
4268 if (mask & 1) {
4269 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
4270 ++frame_bufs[cm->new_fb_idx].ref_count;
4271 } else {
4272 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4273 }
4274 // Current thread holds the reference frame.
4275 if (cm->ref_frame_map[ref_index] >= 0)
4276 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4277 ++ref_index;
4278 }
4279
4280 for (; ref_index < REF_FRAMES; ++ref_index) {
4281 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4282
4283 // Current thread holds the reference frame.
4284 if (cm->ref_frame_map[ref_index] >= 0)
4285 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4286 }
4287 unlock_buffer_pool(pool);
4288 pbi->hold_ref_buf = 1;
4289
4290 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07004291 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004292
4293#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07004294 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004295#else
4296 set_sb_size(cm, BLOCK_64X64);
4297#endif // CONFIG_EXT_PARTITION
4298
4299 setup_loopfilter(cm, rb);
Jean-Marc Valin01435132017-02-18 14:12:53 -05004300#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01004301 setup_cdef(cm, rb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004302#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004303#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004304 decode_restoration_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004305#endif // CONFIG_LOOP_RESTORATION
4306 setup_quantization(cm, rb);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004307#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004308 xd->bd = (int)cm->bit_depth;
4309#endif
4310
hui su0d103572017-03-01 17:58:01 -08004311#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuf883b422016-08-30 14:01:10 -07004312 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004313 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4314 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4315 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4316 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
4317 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4318 }
hui su0d103572017-03-01 17:58:01 -08004319#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004320
4321 setup_segmentation(cm, rb);
4322
Arild Fuldseth07441162016-08-15 15:07:52 +02004323#if CONFIG_DELTA_Q
4324 {
4325 struct segmentation *const seg = &cm->seg;
4326 int segment_quantizer_active = 0;
4327 for (i = 0; i < MAX_SEGMENTS; i++) {
4328 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
4329 segment_quantizer_active = 1;
4330 }
4331 }
4332
Thomas Daviesf6936102016-09-05 16:51:31 +01004333 cm->delta_q_res = 1;
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01004334 if (segment_quantizer_active == 0 && cm->base_qindex > 0) {
Arild Fuldseth07441162016-08-15 15:07:52 +02004335 cm->delta_q_present_flag = aom_rb_read_bit(rb);
4336 } else {
4337 cm->delta_q_present_flag = 0;
4338 }
4339 if (cm->delta_q_present_flag) {
4340 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01004341 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Arild Fuldseth07441162016-08-15 15:07:52 +02004342 }
4343 }
4344#endif
4345
Urvang Joshi454280d2016-10-14 16:51:44 -07004346 for (i = 0; i < MAX_SEGMENTS; ++i) {
4347 const int qindex = cm->seg.enabled
4348 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
4349 : cm->base_qindex;
4350 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
4351 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4352 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004353 }
4354
4355 setup_segmentation_dequant(cm);
Yue Cheneeacc4c2017-01-17 17:29:17 -08004356 cm->tx_mode = read_tx_mode(cm, xd, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004357 cm->reference_mode = read_frame_reference_mode(cm, rb);
4358
Sarah Parkere68a3e42017-02-16 14:03:24 -08004359#if CONFIG_EXT_TX
4360 cm->reduced_tx_set_used = aom_rb_read_bit(rb);
4361#endif // CONFIG_EXT_TX
4362
Yaowu Xuc27fc142016-08-22 16:08:15 -07004363 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004364 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004365
4366 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07004367 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004368 "Invalid header size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004369 return sz;
4370}
4371
4372#if CONFIG_EXT_TX
Thomas9ac55082016-09-23 18:04:17 +01004373#if !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuf883b422016-08-30 14:01:10 -07004374static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004375 int i, j, k;
4376 int s;
4377 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004378 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004379 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4380 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004381 for (j = 0; j < num_ext_tx_set[ext_tx_set_type_inter[s]] - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004382 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004383 }
4384 }
4385 }
4386
4387 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004388 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004389 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4390 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
4391 for (j = 0; j < INTRA_MODES; ++j)
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004392 for (k = 0; k < num_ext_tx_set[ext_tx_set_type_intra[s]] - 1; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004393 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
4394 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004395 }
4396 }
4397 }
4398}
Thomas9ac55082016-09-23 18:04:17 +01004399#endif // !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004400#else
4401
Yaowu Xuc27fc142016-08-22 16:08:15 -07004402#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004403#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07004404static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004405 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -07004406 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004407 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
Jingning Hanfeb517c2016-12-21 16:02:07 -08004408 for (j = TX_8X8; j < TX_SIZES; ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004409 av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004410 }
4411 }
4412 }
4413}
4414#endif // CONFIG_SUPERTX
4415
4416#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +00004417static void read_global_motion_params(WarpedMotionParams *params,
Sarah Parkerf1783292017-04-05 11:55:27 -07004418 WarpedMotionParams *ref_params,
Sarah Parker13d06622017-03-10 17:03:28 -08004419 aom_prob *probs, aom_reader *r,
4420 int allow_hp) {
David Barkercf3d0b02016-11-10 10:14:49 +00004421 TransformationType type =
Michael Bebenita6048d052016-08-25 14:40:54 -07004422 aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
Sarah Parker13d06622017-03-10 17:03:28 -08004423 int trans_bits;
4424 int trans_dec_factor;
Sarah Parkerf1783292017-04-05 11:55:27 -07004425 int trans_prec_diff;
Sarah Parker4c10a3c2017-04-10 19:37:59 -07004426 set_default_warp_params(params);
David Barkercf3d0b02016-11-10 10:14:49 +00004427 params->wmtype = type;
4428 switch (type) {
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004429 case HOMOGRAPHY:
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004430 case HORTRAPEZOID:
4431 case VERTRAPEZOID:
4432 if (type != HORTRAPEZOID)
4433 params->wmmat[6] =
Sarah Parkerf1783292017-04-05 11:55:27 -07004434 aom_read_signed_primitive_refsubexpfin(
4435 r, GM_ROW3HOMO_MAX + 1, SUBEXPFIN_K,
4436 (ref_params->wmmat[6] >> GM_ROW3HOMO_PREC_DIFF)) *
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004437 GM_ROW3HOMO_DECODE_FACTOR;
4438 if (type != VERTRAPEZOID)
4439 params->wmmat[7] =
Sarah Parkerf1783292017-04-05 11:55:27 -07004440 aom_read_signed_primitive_refsubexpfin(
4441 r, GM_ROW3HOMO_MAX + 1, SUBEXPFIN_K,
4442 (ref_params->wmmat[7] >> GM_ROW3HOMO_PREC_DIFF)) *
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004443 GM_ROW3HOMO_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004444 case AFFINE:
4445 case ROTZOOM:
Sarah Parkerf1783292017-04-05 11:55:27 -07004446 params->wmmat[2] = aom_read_signed_primitive_refsubexpfin(
4447 r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4448 (ref_params->wmmat[2] >> GM_ALPHA_PREC_DIFF) -
4449 (1 << GM_ALPHA_PREC_BITS)) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004450 GM_ALPHA_DECODE_FACTOR +
David Barkercf3d0b02016-11-10 10:14:49 +00004451 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004452 if (type != VERTRAPEZOID)
Sarah Parkerf1783292017-04-05 11:55:27 -07004453 params->wmmat[3] = aom_read_signed_primitive_refsubexpfin(
4454 r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4455 (ref_params->wmmat[3] >> GM_ALPHA_PREC_DIFF)) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004456 GM_ALPHA_DECODE_FACTOR;
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004457 if (type >= AFFINE) {
4458 if (type != HORTRAPEZOID)
Sarah Parkerf1783292017-04-05 11:55:27 -07004459 params->wmmat[4] = aom_read_signed_primitive_refsubexpfin(
4460 r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4461 (ref_params->wmmat[4] >> GM_ALPHA_PREC_DIFF)) *
4462 GM_ALPHA_DECODE_FACTOR;
4463 params->wmmat[5] = aom_read_signed_primitive_refsubexpfin(
4464 r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4465 (ref_params->wmmat[5] >> GM_ALPHA_PREC_DIFF) -
4466 (1 << GM_ALPHA_PREC_BITS)) *
David Barkercf3d0b02016-11-10 10:14:49 +00004467 GM_ALPHA_DECODE_FACTOR +
4468 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004469 } else {
David Barkercf3d0b02016-11-10 10:14:49 +00004470 params->wmmat[4] = -params->wmmat[3];
4471 params->wmmat[5] = params->wmmat[2];
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004472 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004473 // fallthrough intended
David Barkercf3d0b02016-11-10 10:14:49 +00004474 case TRANSLATION:
Sarah Parker13d06622017-03-10 17:03:28 -08004475 trans_bits = (type == TRANSLATION) ? GM_ABS_TRANS_ONLY_BITS - !allow_hp
4476 : GM_ABS_TRANS_BITS;
4477 trans_dec_factor = (type == TRANSLATION)
4478 ? GM_TRANS_ONLY_DECODE_FACTOR * (1 << !allow_hp)
4479 : GM_TRANS_DECODE_FACTOR;
Sarah Parkerf1783292017-04-05 11:55:27 -07004480 trans_prec_diff = (type == TRANSLATION)
4481 ? GM_TRANS_ONLY_PREC_DIFF + !allow_hp
4482 : GM_TRANS_PREC_DIFF;
4483 params->wmmat[0] = aom_read_signed_primitive_refsubexpfin(
4484 r, (1 << trans_bits) + 1, SUBEXPFIN_K,
4485 (ref_params->wmmat[0] >> trans_prec_diff)) *
4486 trans_dec_factor;
4487 params->wmmat[1] = aom_read_signed_primitive_refsubexpfin(
4488 r, (1 << trans_bits) + 1, SUBEXPFIN_K,
4489 (ref_params->wmmat[1] >> trans_prec_diff)) *
4490 trans_dec_factor;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004491 case IDENTITY: break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004492 default: assert(0);
4493 }
Debargha Mukherjee3b6c5442017-03-30 08:22:00 -07004494 if (params->wmtype <= AFFINE)
4495 if (!get_shear_params(params)) assert(0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004496}
4497
Yaowu Xuf883b422016-08-30 14:01:10 -07004498static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004499 int frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004500 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
Sarah Parkerf1783292017-04-05 11:55:27 -07004501 read_global_motion_params(
4502 &cm->global_motion[frame], &cm->prev_frame->global_motion[frame],
4503 cm->fc->global_motion_types_prob, r, cm->allow_high_precision_mv);
Sarah Parkere5299862016-08-16 14:57:37 -07004504 /*
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004505 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4506 frame, cm->current_video_frame, cm->show_frame,
David Barkercf3d0b02016-11-10 10:14:49 +00004507 cm->global_motion[frame].wmmat[0],
4508 cm->global_motion[frame].wmmat[1],
4509 cm->global_motion[frame].wmmat[2],
4510 cm->global_motion[frame].wmmat[3]);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004511 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004512 }
Sarah Parkerf1783292017-04-05 11:55:27 -07004513 memcpy(cm->cur_frame->global_motion, cm->global_motion,
4514 TOTAL_REFS_PER_FRAME * sizeof(WarpedMotionParams));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004515}
4516#endif // CONFIG_GLOBAL_MOTION
4517
Yaowu Xuf883b422016-08-30 14:01:10 -07004518static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004519 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004520 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004521#if CONFIG_SUPERTX
4522 MACROBLOCKD *const xd = &pbi->mb;
4523#endif
4524 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07004525 aom_reader r;
Yaowu Xu8af861b2016-11-01 12:12:11 -07004526 int k, i;
Thomas Davies493623e2017-03-31 16:12:25 +01004527#if !CONFIG_EC_ADAPT || \
4528 (CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION || CONFIG_EXT_INTER)
Yaowu Xu8af861b2016-11-01 12:12:11 -07004529 int j;
4530#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004531
Alex Converse2cdf0d82016-12-13 13:53:09 -08004532#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08004533 r.window_size = 1 << cm->ans_window_size_log2;
Alex Converse2cdf0d82016-12-13 13:53:09 -08004534#endif
Alex Converse346440b2017-01-03 13:47:37 -08004535 if (aom_reader_init(&r, data, partition_size, pbi->decrypt_cb,
4536 pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07004537 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004538 "Failed to allocate bool decoder 0");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004539
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004540#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004541 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4542 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4543 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4544 av1_alloc_restoration_buffers(cm);
4545 decode_restoration(cm, &r);
4546 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004547#endif
4548
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004549#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -07004550 if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004551#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004552
Angie Chiang800df032017-03-22 11:14:12 -07004553#if CONFIG_LV_MAP
4554 av1_read_txb_probs(fc, cm->tx_mode, &r);
4555#else // CONFIG_LV_MAP
Yushin Cho77bba8d2016-11-04 16:36:56 -07004556#if !CONFIG_PVQ
Alex Conversea9598cd2017-02-03 14:18:05 -08004557#if !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004558 read_coef_probs(fc, cm->tx_mode, &r);
Angie Chiang800df032017-03-22 11:14:12 -07004559#endif // !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Angie Chiang7d7ead92017-03-22 10:35:51 -07004560#endif // !CONFIG_PVQ
Angie Chiang800df032017-03-22 11:14:12 -07004561#endif // CONFIG_LV_MAP
4562
Yaowu Xuc27fc142016-08-22 16:08:15 -07004563#if CONFIG_VAR_TX
4564 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004565 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
Yushin Cho77bba8d2016-11-04 16:36:56 -07004566#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004567 for (k = 0; k < SKIP_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004568 av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004569
Thomas Daviesd6ee8a82017-03-02 14:42:50 +00004570#if CONFIG_DELTA_Q && !CONFIG_EC_ADAPT
Thomas Davies665cd702017-03-02 10:20:30 +00004571 for (k = 0; k < DELTA_Q_PROBS; ++k)
Thomas Daviesf6936102016-09-05 16:51:31 +01004572 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
4573#endif
4574
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004575#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004576 if (cm->seg.enabled && cm->seg.update_map) {
4577 if (cm->seg.temporal_update) {
4578 for (k = 0; k < PREDICTION_PROBS; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004579 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004580 }
4581 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004582 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004583 }
4584
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004585 for (j = 0; j < INTRA_MODES; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004586 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004587 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004588 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004589
4590#if CONFIG_EXT_PARTITION_TYPES
Alex Converse4e18d402017-03-14 15:36:38 -07004591 for (j = 0; j < PARTITION_PLOFFSET; ++j)
4592 for (i = 0; i < PARTITION_TYPES - 1; ++i)
4593 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
4594 for (; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004595 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004596 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004597#else
Alex Converse55c6bde2017-01-12 15:55:31 -08004598 for (j = 0; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004599 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004600 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004601#endif // CONFIG_EXT_PARTITION_TYPES
hui su9aa97492017-01-26 16:46:01 -08004602
Alex Converse55c6bde2017-01-12 15:55:31 -08004603#if CONFIG_UNPOISON_PARTITION_CTX
4604 for (; j < PARTITION_CONTEXTS_PRIMARY + PARTITION_BLOCK_SIZES; ++j)
4605 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_VERT], ACCT_STR);
4606 for (; j < PARTITION_CONTEXTS_PRIMARY + 2 * PARTITION_BLOCK_SIZES; ++j)
4607 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_HORZ], ACCT_STR);
4608#endif // CONFIG_UNPOISON_PARTITION_CTX
hui su9aa97492017-01-26 16:46:01 -08004609
4610#if CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004611 for (i = 0; i < INTRA_FILTERS + 1; ++i)
4612 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004613 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
hui su9aa97492017-01-26 16:46:01 -08004614#endif // CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
hui sub4e25d22017-03-09 15:32:30 -08004615#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004616
4617 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004618 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Nathan E. Egge10ba2be2016-11-16 09:44:26 -05004619#if CONFIG_EC_MULTISYMBOL
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00004620 av1_copy(cm->fc->kf_y_cdf, av1_kf_y_mode_cdf);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004621#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004622#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004623 for (k = 0; k < INTRA_MODES; k++)
Thomas Davies6519beb2016-10-19 14:46:07 +01004624 for (j = 0; j < INTRA_MODES; j++)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004625 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004626 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004627#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004628 } else {
4629#if !CONFIG_REF_MV
4630 nmv_context *const nmvc = &fc->nmvc;
4631#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004632 read_inter_mode_probs(fc, &r);
4633
4634#if CONFIG_EXT_INTER
4635 read_inter_compound_mode_probs(fc, &r);
4636 if (cm->reference_mode != COMPOUND_REFERENCE) {
4637 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4638 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004639 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004640 }
4641 }
4642 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4643 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004644 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004645 }
4646 for (i = 0; i < BLOCK_SIZES; i++) {
4647 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004648 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004649 }
4650 }
4651 }
4652 if (cm->reference_mode != SINGLE_REFERENCE) {
4653 for (i = 0; i < BLOCK_SIZES; i++) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08004654 for (j = 0; j < COMPOUND_TYPES - 1; j++) {
4655 av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004656 }
4657 }
4658 }
4659#endif // CONFIG_EXT_INTER
4660
Yue Chencb60b182016-10-13 15:18:22 -07004661#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004662 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
Yue Chencb60b182016-10-13 15:18:22 -07004663 for (j = 0; j < MOTION_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004664 av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004665 }
Yue Chencb60b182016-10-13 15:18:22 -07004666#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004667
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004668#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004669 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004670#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004671
4672 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004673 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004674
4675 if (cm->reference_mode != SINGLE_REFERENCE)
4676 setup_compound_reference_mode(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004677 read_frame_reference_mode_probs(cm, &r);
4678
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004679#if !CONFIG_EC_ADAPT
Nathan E. Egge5710c722016-09-08 10:01:16 -04004680 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004681 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004682 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
Nathan E. Egge5710c722016-09-08 10:01:16 -04004683 }
Thomas9ac55082016-09-23 18:04:17 +01004684#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004685
4686#if CONFIG_REF_MV
4687 for (i = 0; i < NMV_CONTEXTS; ++i)
4688 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
4689#else
4690 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
4691#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004692#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004693 read_ext_tx_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004694#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004695#if CONFIG_SUPERTX
4696 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
4697#endif
4698#if CONFIG_GLOBAL_MOTION
4699 read_global_motion(cm, &r);
Thomas Davies6519beb2016-10-19 14:46:07 +01004700#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004701 }
Thomas Davies028b57f2017-02-22 16:42:11 +00004702#if CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Thomas Davies87aeeb82017-02-17 00:19:40 +00004703#if CONFIG_NEW_TOKENSET
4704 av1_coef_head_cdfs(fc);
4705#endif
4706 /* Make tail distribution from head */
Thomas Davies6519beb2016-10-19 14:46:07 +01004707 av1_coef_pareto_cdfs(fc);
David Barker599dfd02016-11-10 13:20:12 +00004708#if CONFIG_REF_MV
4709 for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
4710#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004711 av1_set_mv_cdfs(&fc->nmvc);
David Barker599dfd02016-11-10 13:20:12 +00004712#endif
Thomas Davies6519beb2016-10-19 14:46:07 +01004713 av1_set_mode_cdfs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00004714#endif // CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004715
Yaowu Xuf883b422016-08-30 14:01:10 -07004716 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004717}
4718
4719#ifdef NDEBUG
4720#define debug_check_frame_counts(cm) (void)0
4721#else // !NDEBUG
4722// Counts should only be incremented when frame_parallel_decoding_mode and
4723// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07004724static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004725 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07004726 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004727 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
4728 cm->error_resilient_mode);
4729 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
4730 sizeof(cm->counts.y_mode)));
4731 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
4732 sizeof(cm->counts.uv_mode)));
4733 assert(!memcmp(cm->counts.partition, zero_counts.partition,
4734 sizeof(cm->counts.partition)));
4735 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
4736 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
4737 sizeof(cm->counts.eob_branch)));
Thomas Daviesab780672017-02-01 12:07:29 +00004738#if CONFIG_EC_MULTISYMBOL
4739 assert(!memcmp(cm->counts.blockz_count, zero_counts.blockz_count,
4740 sizeof(cm->counts.blockz_count)));
4741#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004742 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
4743 sizeof(cm->counts.switchable_interp)));
4744 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
4745 sizeof(cm->counts.inter_mode)));
4746#if CONFIG_EXT_INTER
4747 assert(!memcmp(cm->counts.inter_compound_mode,
4748 zero_counts.inter_compound_mode,
4749 sizeof(cm->counts.inter_compound_mode)));
4750 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
4751 sizeof(cm->counts.interintra)));
4752 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
4753 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08004754 assert(!memcmp(cm->counts.compound_interinter,
4755 zero_counts.compound_interinter,
4756 sizeof(cm->counts.compound_interinter)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004757#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07004758#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
4759 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
4760 sizeof(cm->counts.motion_mode)));
4761#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004762 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
4763 sizeof(cm->counts.intra_inter)));
4764 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
4765 sizeof(cm->counts.comp_inter)));
4766 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
4767 sizeof(cm->counts.single_ref)));
4768 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
4769 sizeof(cm->counts.comp_ref)));
4770#if CONFIG_EXT_REFS
4771 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
4772 sizeof(cm->counts.comp_bwdref)));
4773#endif // CONFIG_EXT_REFS
4774 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
4775 sizeof(cm->counts.tx_size)));
4776 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
4777#if CONFIG_REF_MV
4778 assert(
4779 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
4780 assert(
4781 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
4782#else
4783 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
4784#endif
4785 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
4786 sizeof(cm->counts.inter_ext_tx)));
4787 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
4788 sizeof(cm->counts.intra_ext_tx)));
4789}
4790#endif // NDEBUG
4791
Yaowu Xuf883b422016-08-30 14:01:10 -07004792static struct aom_read_bit_buffer *init_read_bit_buffer(
4793 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
4794 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004795 rb->bit_offset = 0;
4796 rb->error_handler = error_handler;
4797 rb->error_handler_data = &pbi->common;
4798 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004799 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004800 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
4801 rb->bit_buffer = clear_data;
4802 rb->bit_buffer_end = clear_data + n;
4803 } else {
4804 rb->bit_buffer = data;
4805 rb->bit_buffer_end = data_end;
4806 }
4807 return rb;
4808}
4809
4810//------------------------------------------------------------------------------
4811
Yaowu Xuf883b422016-08-30 14:01:10 -07004812int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
4813 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
4814 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
4815 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004816}
4817
Yaowu Xuf883b422016-08-30 14:01:10 -07004818void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
4819 int *height) {
4820 *width = aom_rb_read_literal(rb, 16) + 1;
4821 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004822}
4823
Yaowu Xuf883b422016-08-30 14:01:10 -07004824BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
4825 int profile = aom_rb_read_bit(rb);
4826 profile |= aom_rb_read_bit(rb) << 1;
4827 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004828 return (BITSTREAM_PROFILE)profile;
4829}
4830
Thomas Davies028b57f2017-02-22 16:42:11 +00004831#if CONFIG_EC_ADAPT
4832static void make_update_tile_list_dec(AV1Decoder *pbi, const int tile_rows,
4833 const int tile_cols,
4834 FRAME_CONTEXT *ec_ctxs[]) {
4835 int i;
4836 for (i = 0; i < tile_rows * tile_cols; ++i)
4837 ec_ctxs[i] = &pbi->tile_data[i].tctx;
4838}
4839#endif
4840
Yaowu Xuf883b422016-08-30 14:01:10 -07004841void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
4842 const uint8_t *data_end, const uint8_t **p_data_end) {
4843 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004844 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07004845 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004846 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004847 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004848 size_t first_partition_size;
4849 YV12_BUFFER_CONFIG *new_fb;
4850
Yi Luof8e87b42017-04-14 17:20:27 -07004851#if CONFIG_ADAPT_SCAN
4852 av1_deliver_eob_threshold(cm, xd);
4853#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004854#if CONFIG_BITSTREAM_DEBUG
4855 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
4856#endif
4857
4858 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004859 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08004860
4861#if CONFIG_EXT_TILE
4862 // If cm->tile_encoding_mode == TILE_NORMAL, the independent decoding of a
4863 // single tile or a section of a frame is not allowed.
4864 if (!cm->tile_encoding_mode &&
4865 (pbi->dec_tile_row >= 0 || pbi->dec_tile_col >= 0)) {
4866 pbi->dec_tile_row = -1;
4867 pbi->dec_tile_col = -1;
4868 }
4869#endif // CONFIG_EXT_TILE
4870
Thomas Davies72712e62016-11-09 12:17:51 +00004871#if CONFIG_TILE_GROUPS
4872 pbi->first_partition_size = first_partition_size;
4873 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
4874#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004875 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004876 xd->cur_buf = new_fb;
4877#if CONFIG_GLOBAL_MOTION
Sarah Parkerf1783292017-04-05 11:55:27 -07004878 int i;
4879 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Sarah Parker4c10a3c2017-04-10 19:37:59 -07004880 set_default_warp_params(&cm->global_motion[i]);
4881 set_default_warp_params(&cm->cur_frame->global_motion[i]);
Sarah Parkerf1783292017-04-05 11:55:27 -07004882 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004883 xd->global_motion = cm->global_motion;
4884#endif // CONFIG_GLOBAL_MOTION
4885
4886 if (!first_partition_size) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004887 // showing a frame directly
4888 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004889 return;
4890 }
4891
Yaowu Xuf883b422016-08-30 14:01:10 -07004892 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004893 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07004894 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004895 "Truncated packet or corrupt header length");
4896
Jingning Han24e0a182016-11-20 22:34:12 -08004897#if CONFIG_REF_MV
Dengca8d24d2016-10-17 14:06:35 +08004898 cm->setup_mi(cm);
4899#endif
4900
Fangwen Fu8d164de2016-12-14 13:40:54 -08004901#if CONFIG_TEMPMV_SIGNALING
4902 if (cm->use_prev_frame_mvs) {
4903 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4904 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4905 assert(!cm->error_resilient_mode &&
4906 cm->width == last_fb_ref_buf->buf->y_width &&
4907 cm->height == last_fb_ref_buf->buf->y_height &&
4908 !cm->prev_frame->intra_only);
4909 }
4910#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004911 cm->use_prev_frame_mvs =
4912 !cm->error_resilient_mode && cm->width == cm->last_width &&
4913 cm->height == cm->last_height && !cm->last_intra_only &&
4914 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004915#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004916#if CONFIG_EXT_REFS
4917 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
4918 // show_exisiting_frame=1, nor can it take a frame not used as
4919 // a reference, it is probable that by the time it is being
4920 // referred to, the frame buffer it originally points to may
4921 // already get expired and have been reassigned to the current
4922 // newly coded frame. Hence, we need to check whether this is
4923 // the case, and if yes, we have 2 choices:
4924 // (1) Simply disable the use of previous frame mvs; or
4925 // (2) Have cm->prev_frame point to one reference frame buffer,
4926 // e.g. LAST_FRAME.
4927 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
4928 // Reassign the LAST_FRAME buffer to cm->prev_frame.
4929 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4930 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4931 }
4932#endif // CONFIG_EXT_REFS
4933
Yaowu Xuf883b422016-08-30 14:01:10 -07004934 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004935
4936 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
4937 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07004938 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004939 "Uninitialized entropy context.");
4940
Yaowu Xuf883b422016-08-30 14:01:10 -07004941 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004942
4943 xd->corrupted = 0;
4944 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
4945 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07004946 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004947 "Decode failed. Frame data header is corrupted.");
4948
4949 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004950 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004951 }
4952
4953 // If encoded in frame parallel mode, frame context is ready after decoding
4954 // the frame header.
4955 if (cm->frame_parallel_decode &&
4956 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004957 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004958 FrameWorkerData *const frame_worker_data = worker->data1;
4959 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
4960 context_updated = 1;
4961 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4962 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004963 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004964 pbi->cur_buf->row = -1;
4965 pbi->cur_buf->col = -1;
4966 frame_worker_data->frame_context_ready = 1;
4967 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07004968 av1_frameworker_signal_stats(worker);
4969 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004970 }
4971
hui su0d103572017-03-01 17:58:01 -08004972#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07004973 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004974 cm->coef_probs_update_idx = 0;
hui su0d103572017-03-01 17:58:01 -08004975#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004976
Jingning Han52ece882017-04-07 14:58:25 -07004977 if (pbi->max_threads > 1 && !CONFIG_CB4X4 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004978#if CONFIG_EXT_TILE
Jingning Han52ece882017-04-07 14:58:25 -07004979 pbi->dec_tile_col < 0 && // Decoding all columns
Yaowu Xuc27fc142016-08-22 16:08:15 -07004980#endif // CONFIG_EXT_TILE
Jingning Han52ece882017-04-07 14:58:25 -07004981 cm->tile_cols > 1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004982 // Multi-threaded tile decoder
4983 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
4984 if (!xd->corrupted) {
4985 if (!cm->skip_loop_filter) {
4986 // If multiple threads are used to decode tiles, then we use those
4987 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07004988 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
4989 0, 0, pbi->tile_workers, pbi->num_tile_workers,
4990 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004991 }
4992 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004993 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004994 "Decode failed. Frame data is corrupted.");
4995 }
4996 } else {
4997 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
4998 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004999
Jean-Marc Valin01435132017-02-18 14:12:53 -05005000#if CONFIG_CDEF
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04005001 if (!cm->skip_loop_filter) {
Jean-Marc Valine9f77422017-03-22 17:09:51 -04005002 av1_cdef_frame(&pbi->cur_buf->buf, cm, &pbi->mb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02005003 }
Debargha Mukherjee00c54332017-03-03 15:44:17 -08005004#endif // CONFIG_CDEF
5005
5006#if CONFIG_LOOP_RESTORATION
5007 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
5008 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
5009 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
5010 av1_loop_restoration_frame(new_fb, cm, cm->rst_info, 7, 0, NULL);
5011 }
5012#endif // CONFIG_LOOP_RESTORATION
Thomas Daedef56859f2016-04-19 16:57:24 -07005013
Yaowu Xuc27fc142016-08-22 16:08:15 -07005014 if (!xd->corrupted) {
5015 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Thomas Davies028b57f2017-02-22 16:42:11 +00005016#if CONFIG_EC_ADAPT
Thomas Davies493623e2017-03-31 16:12:25 +01005017 FRAME_CONTEXT **tile_ctxs = aom_malloc(cm->tile_rows * cm->tile_cols *
5018 sizeof(&pbi->tile_data[0].tctx));
5019 aom_cdf_prob **cdf_ptrs =
5020 aom_malloc(cm->tile_rows * cm->tile_cols *
5021 sizeof(&pbi->tile_data[0].tctx.partition_cdf[0][0]));
Thomas Davies028b57f2017-02-22 16:42:11 +00005022 make_update_tile_list_dec(pbi, cm->tile_rows, cm->tile_cols, tile_ctxs);
5023#endif
5024
hui su0d103572017-03-01 17:58:01 -08005025#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07005026 cm->partial_prob_update = 0;
hui su0d103572017-03-01 17:58:01 -08005027#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07005028 av1_adapt_coef_probs(cm);
5029 av1_adapt_intra_frame_probs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00005030#if CONFIG_EC_ADAPT
Thomas Davies493623e2017-03-31 16:12:25 +01005031 av1_average_tile_coef_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005032 cm->tile_rows * cm->tile_cols);
Thomas Davies493623e2017-03-31 16:12:25 +01005033 av1_average_tile_intra_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005034 cm->tile_rows * cm->tile_cols);
Yushin Chob188ea12017-03-13 13:45:23 -07005035#if CONFIG_PVQ
5036 av1_average_tile_pvq_cdfs(pbi->common.fc, tile_ctxs,
5037 cm->tile_rows * cm->tile_cols);
5038#endif // CONFIG_PVQ
5039#endif // CONFIG_EC_ADAPT
hui suff0da2b2017-03-07 15:51:37 -08005040#if CONFIG_ADAPT_SCAN
5041 av1_adapt_scan_order(cm);
5042#endif // CONFIG_ADAPT_SCAN
Yaowu Xuc27fc142016-08-22 16:08:15 -07005043
5044 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005045 av1_adapt_inter_frame_probs(cm);
5046 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Thomas Davies028b57f2017-02-22 16:42:11 +00005047#if CONFIG_EC_ADAPT
5048 av1_average_tile_inter_cdfs(&pbi->common, pbi->common.fc, tile_ctxs,
Thomas Davies493623e2017-03-31 16:12:25 +01005049 cdf_ptrs, cm->tile_rows * cm->tile_cols);
5050 av1_average_tile_mv_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005051 cm->tile_rows * cm->tile_cols);
5052#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005053 }
Thomas Davies493623e2017-03-31 16:12:25 +01005054#if CONFIG_EC_ADAPT
5055 aom_free(tile_ctxs);
5056 aom_free(cdf_ptrs);
5057#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005058 } else {
5059 debug_check_frame_counts(cm);
5060 }
5061 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005062 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005063 "Decode failed. Frame data is corrupted.");
5064 }
5065
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05005066#if CONFIG_INSPECTION
5067 if (pbi->inspect_cb != NULL) {
5068 (*pbi->inspect_cb)(pbi, pbi->inspect_ctx);
5069 }
5070#endif
5071
Yaowu Xuc27fc142016-08-22 16:08:15 -07005072 // Non frame parallel update frame context here.
5073 if (!cm->error_resilient_mode && !context_updated)
5074 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
5075}