blob: 4353bb62c5a34d80ce99c52394168588f7047526 [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
Luc Trudeaue3980282017-04-25 23:17:21 -040082#if CONFIG_CFL
83#include "av1/common/cfl.h"
84#endif
85
Thomas Davies80188d12016-10-26 16:08:35 -070086static struct aom_read_bit_buffer *init_read_bit_buffer(
87 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
88 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]);
89static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
90 size_t partition_size);
91static size_t read_uncompressed_header(AV1Decoder *pbi,
92 struct aom_read_bit_buffer *rb);
93
Yaowu Xuf883b422016-08-30 14:01:10 -070094static int is_compound_reference_allowed(const AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070095 int i;
96 if (frame_is_intra_only(cm)) return 0;
97 for (i = 1; i < INTER_REFS_PER_FRAME; ++i)
98 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
99
100 return 0;
101}
102
Yaowu Xuf883b422016-08-30 14:01:10 -0700103static void setup_compound_reference_mode(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700104#if CONFIG_EXT_REFS
105 cm->comp_fwd_ref[0] = LAST_FRAME;
106 cm->comp_fwd_ref[1] = LAST2_FRAME;
107 cm->comp_fwd_ref[2] = LAST3_FRAME;
108 cm->comp_fwd_ref[3] = GOLDEN_FRAME;
109
110 cm->comp_bwd_ref[0] = BWDREF_FRAME;
111 cm->comp_bwd_ref[1] = ALTREF_FRAME;
112#else
113 if (cm->ref_frame_sign_bias[LAST_FRAME] ==
114 cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
115 cm->comp_fixed_ref = ALTREF_FRAME;
116 cm->comp_var_ref[0] = LAST_FRAME;
117 cm->comp_var_ref[1] = GOLDEN_FRAME;
118 } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
119 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
120 cm->comp_fixed_ref = GOLDEN_FRAME;
121 cm->comp_var_ref[0] = LAST_FRAME;
122 cm->comp_var_ref[1] = ALTREF_FRAME;
123 } else {
124 cm->comp_fixed_ref = LAST_FRAME;
125 cm->comp_var_ref[0] = GOLDEN_FRAME;
126 cm->comp_var_ref[1] = ALTREF_FRAME;
127 }
128#endif // CONFIG_EXT_REFS
129}
130
131static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
132 return len != 0 && len <= (size_t)(end - start);
133}
134
Yaowu Xuf883b422016-08-30 14:01:10 -0700135static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
136 const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700137 return data > max ? max : data;
138}
139
Yue Cheneeacc4c2017-01-17 17:29:17 -0800140static TX_MODE read_tx_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
141 struct aom_read_bit_buffer *rb) {
142 int i, all_lossless = 1;
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800143#if CONFIG_TX64X64
Yue Cheneeacc4c2017-01-17 17:29:17 -0800144 TX_MODE tx_mode;
145#endif
146
147 if (cm->seg.enabled) {
148 for (i = 0; i < MAX_SEGMENTS; ++i) {
149 if (!xd->lossless[i]) {
150 all_lossless = 0;
151 break;
152 }
153 }
154 } else {
155 all_lossless = xd->lossless[0];
156 }
157
158 if (all_lossless) return ONLY_4X4;
159#if CONFIG_TX64X64
160 tx_mode = aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800161 if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
162 return tx_mode;
163#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700164 return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800165#endif // CONFIG_TX64X64
Yaowu Xuc27fc142016-08-22 16:08:15 -0700166}
167
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -0500168#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -0700169static void read_tx_size_probs(FRAME_CONTEXT *fc, aom_reader *r) {
170 int i, j, k;
171 for (i = 0; i < MAX_TX_DEPTH; ++i)
172 for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
173 for (k = 0; k < i + 1; ++k)
174 av1_diff_update_prob(r, &fc->tx_size_probs[i][j][k], ACCT_STR);
175}
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -0500176#endif
Yaowu Xuefc75352016-10-31 09:46:42 -0700177
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400178#if !CONFIG_EC_ADAPT
Yaowu Xuf883b422016-08-30 14:01:10 -0700179static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700180 int i, j;
Nathan E. Egge4947c292016-04-26 11:37:06 -0400181 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700183 av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i], ACCT_STR);
Nathan E. Egge4947c292016-04-26 11:37:06 -0400184 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700185}
Thomas Davies6519beb2016-10-19 14:46:07 +0100186#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700187
Yaowu Xuf883b422016-08-30 14:01:10 -0700188static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700189#if CONFIG_REF_MV
Yaowu Xu8af861b2016-11-01 12:12:11 -0700190 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700191 for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700192 av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700193 for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700194 av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700195 for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700196 av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700197 for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700198 av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700199#else
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400200#if !CONFIG_EC_ADAPT
Yaowu Xu8af861b2016-11-01 12:12:11 -0700201 int i, j;
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400202 for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700203 for (j = 0; j < INTER_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700204 av1_diff_update_prob(r, &fc->inter_mode_probs[i][j], ACCT_STR);
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400205 }
Yaowu Xu8af861b2016-11-01 12:12:11 -0700206#else
207 (void)fc;
208 (void)r;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700209#endif
Thomas Davies6519beb2016-10-19 14:46:07 +0100210#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700211}
212
213#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700214static void read_inter_compound_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700215 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -0700216 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700217 for (j = 0; j < INTER_MODE_CONTEXTS; ++j) {
218 for (i = 0; i < INTER_COMPOUND_MODES - 1; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700219 av1_diff_update_prob(r, &fc->inter_compound_mode_probs[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700220 }
221 }
222 }
223}
224#endif // CONFIG_EXT_INTER
Yaowu Xu17fd2f22016-11-17 18:23:28 -0800225#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100226#if !CONFIG_EXT_TX
227static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
228 int i, j, k;
229 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
230 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
231 for (j = 0; j < TX_TYPES; ++j) {
232 for (k = 0; k < TX_TYPES - 1; ++k)
233 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[i][j][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100234 }
235 }
236 }
237 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
238 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
239 for (k = 0; k < TX_TYPES - 1; ++k)
240 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[i][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100241 }
242 }
243}
244#endif
245#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700246
247static REFERENCE_MODE read_frame_reference_mode(
Yaowu Xuf883b422016-08-30 14:01:10 -0700248 const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700249 if (is_compound_reference_allowed(cm)) {
Zoe Liub05e5d12017-02-07 14:32:53 -0800250#if CONFIG_REF_ADAPT
251 return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
252#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700253 return aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700254 ? REFERENCE_MODE_SELECT
Yaowu Xuf883b422016-08-30 14:01:10 -0700255 : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
Zoe Liub05e5d12017-02-07 14:32:53 -0800256#endif // CONFIG_REF_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700257 } else {
258 return SINGLE_REFERENCE;
259 }
260}
261
Yaowu Xuf883b422016-08-30 14:01:10 -0700262static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700263 FRAME_CONTEXT *const fc = cm->fc;
264 int i, j;
265
266 if (cm->reference_mode == REFERENCE_MODE_SELECT)
267 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700268 av1_diff_update_prob(r, &fc->comp_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700269
270 if (cm->reference_mode != COMPOUND_REFERENCE) {
271 for (i = 0; i < REF_CONTEXTS; ++i) {
272 for (j = 0; j < (SINGLE_REFS - 1); ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700273 av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700274 }
275 }
276 }
277
278 if (cm->reference_mode != SINGLE_REFERENCE) {
279 for (i = 0; i < REF_CONTEXTS; ++i) {
280#if CONFIG_EXT_REFS
281 for (j = 0; j < (FWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700282 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700283 for (j = 0; j < (BWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700284 av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700285#else
286 for (j = 0; j < (COMP_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700287 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700288#endif // CONFIG_EXT_REFS
289 }
290 }
291}
292
Yaowu Xuf883b422016-08-30 14:01:10 -0700293static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700294 int i;
Michael Bebenita6048d052016-08-25 14:40:54 -0700295 for (i = 0; i < n; ++i) av1_diff_update_prob(r, &p[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700296}
297
Yaowu Xuf883b422016-08-30 14:01:10 -0700298static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Thomas9ac55082016-09-23 18:04:17 +0100299 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700300
Yaowu Xue86288d2016-10-31 15:56:38 -0700301#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100302 int j;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700303 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
304
Yaowu Xuc27fc142016-08-22 16:08:15 -0700305 for (i = 0; i < 2; ++i) {
306 nmv_component *const comp_ctx = &ctx->comps[i];
307 update_mv_probs(&comp_ctx->sign, 1, r);
308 update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
309 update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
310 update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
311 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700312 for (i = 0; i < 2; ++i) {
313 nmv_component *const comp_ctx = &ctx->comps[i];
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400314 for (j = 0; j < CLASS0_SIZE; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700315 update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400316 }
Nathan E. Eggeac499f32016-09-08 15:38:57 -0400317 update_mv_probs(comp_ctx->fp, MV_FP_SIZE - 1, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700318 }
Alex Converseaca9feb2016-10-10 11:08:10 -0700319#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700320
321 if (allow_hp) {
322 for (i = 0; i < 2; ++i) {
323 nmv_component *const comp_ctx = &ctx->comps[i];
324 update_mv_probs(&comp_ctx->class0_hp, 1, r);
325 update_mv_probs(&comp_ctx->hp, 1, r);
326 }
327 }
328}
329
330static void inverse_transform_block(MACROBLOCKD *xd, int plane,
331 const TX_TYPE tx_type,
332 const TX_SIZE tx_size, uint8_t *dst,
Jingning Han1be18782016-10-21 11:48:15 -0700333 int stride, int16_t scan_line, int eob) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700334 struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han1be18782016-10-21 11:48:15 -0700335 tran_low_t *const dqcoeff = pd->dqcoeff;
Angie Chiangd92d4bf2017-04-02 17:49:18 -0700336 av1_inverse_transform_block(xd, dqcoeff, tx_type, tx_size, dst, stride, eob);
Jingning Han1be18782016-10-21 11:48:15 -0700337 memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700338}
339
Yushin Cho77bba8d2016-11-04 16:36:56 -0700340#if CONFIG_PVQ
Thomas Daede6ff6af62017-02-03 16:29:24 -0800341static int av1_pvq_decode_helper(MACROBLOCKD *xd, tran_low_t *ref_coeff,
Thomas Daede1dbda1b2017-02-06 16:06:29 -0800342 tran_low_t *dqcoeff, int16_t *quant, int pli,
Yushin Cho77bba8d2016-11-04 16:36:56 -0700343 int bs, TX_TYPE tx_type, int xdec,
ltrudeaue1c09292017-01-20 15:42:13 -0500344 PVQ_SKIP_TYPE ac_dc_coded) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700345 unsigned int flags; // used for daala's stream analyzer.
346 int off;
347 const int is_keyframe = 0;
348 const int has_dc_skip = 1;
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800349 int coeff_shift = 3 - get_tx_scale(bs);
Thomas Daede6ff6af62017-02-03 16:29:24 -0800350 int hbd_downshift = 0;
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800351 int rounding_mask;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700352 // DC quantizer for PVQ
353 int pvq_dc_quant;
354 int lossless = (quant[0] == 0);
355 const int blk_size = tx_size_wide[bs];
356 int eob = 0;
357 int i;
Thomas Daede6ff6af62017-02-03 16:29:24 -0800358 od_dec_ctx *dec = &xd->daala_dec;
Yushin Cho70669122016-12-08 09:53:14 -1000359 int use_activity_masking = dec->use_activity_masking;
Thomas Daede1dbda1b2017-02-06 16:06:29 -0800360 DECLARE_ALIGNED(16, tran_low_t, dqcoeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
361 DECLARE_ALIGNED(16, tran_low_t, ref_coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700362
Yushin Cho48f84db2016-11-07 21:20:17 -0800363 od_coeff ref_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
364 od_coeff out_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700365
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200366#if CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -0800367 hbd_downshift = xd->bd - 8;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200368#endif // CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -0800369
Yushin Cho77bba8d2016-11-04 16:36:56 -0700370 od_raster_to_coding_order(ref_coeff_pvq, blk_size, tx_type, ref_coeff,
371 blk_size);
372
Thomas Daede6ff6af62017-02-03 16:29:24 -0800373 assert(OD_COEFF_SHIFT >= 4);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700374 if (lossless)
375 pvq_dc_quant = 1;
376 else {
Yushin Cho70669122016-12-08 09:53:14 -1000377 if (use_activity_masking)
378 pvq_dc_quant = OD_MAXI(
Thomas Daede6ff6af62017-02-03 16:29:24 -0800379 1, (quant[0] << (OD_COEFF_SHIFT - 3) >> hbd_downshift) *
Yushin Cho70669122016-12-08 09:53:14 -1000380 dec->state.pvq_qm_q4[pli][od_qm_get_index(bs, 0)] >>
381 4);
382 else
Thomas Daede6ff6af62017-02-03 16:29:24 -0800383 pvq_dc_quant =
384 OD_MAXI(1, quant[0] << (OD_COEFF_SHIFT - 3) >> hbd_downshift);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700385 }
386
387 off = od_qm_offset(bs, xdec);
388
389 // copy int16 inputs to int32
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800390 for (i = 0; i < blk_size * blk_size; i++) {
Timothy B. Terriberry4e6a8f32017-02-24 11:00:59 -0800391 ref_int32[i] =
Thomas Daede6ff6af62017-02-03 16:29:24 -0800392 AOM_SIGNED_SHL(ref_coeff_pvq[i], OD_COEFF_SHIFT - coeff_shift) >>
393 hbd_downshift;
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800394 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700395
Thomas Daede6ff6af62017-02-03 16:29:24 -0800396 od_pvq_decode(dec, ref_int32, out_int32,
397 OD_MAXI(1, quant[1] << (OD_COEFF_SHIFT - 3) >> hbd_downshift),
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800398 pli, bs, OD_PVQ_BETA[use_activity_masking][pli][bs],
Timothy B. Terriberry44bb6d02017-04-07 15:44:14 -0700399 is_keyframe, &flags, ac_dc_coded, dec->state.qm + off,
400 dec->state.qm_inv + off);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700401
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800402 if (!has_dc_skip || out_int32[0]) {
403 out_int32[0] =
Yushin Choc49ef3a2017-03-13 17:27:25 -0700404 has_dc_skip + generic_decode(dec->r, &dec->state.adapt->model_dc[pli],
Timothy B. Terriberry44bb6d02017-04-07 15:44:14 -0700405 &dec->state.adapt->ex_dc[pli][bs][0], 2,
406 "dc:mag");
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800407 if (out_int32[0]) out_int32[0] *= aom_read_bit(dec->r, "dc:sign") ? -1 : 1;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700408 }
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800409 out_int32[0] = out_int32[0] * pvq_dc_quant + ref_int32[0];
410
411 // copy int32 result back to int16
412 assert(OD_COEFF_SHIFT > coeff_shift);
413 rounding_mask = (1 << (OD_COEFF_SHIFT - coeff_shift - 1)) - 1;
414 for (i = 0; i < blk_size * blk_size; i++) {
Thomas Daede6ff6af62017-02-03 16:29:24 -0800415 out_int32[i] = AOM_SIGNED_SHL(out_int32[i], hbd_downshift);
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800416 dqcoeff_pvq[i] = (out_int32[i] + (out_int32[i] < 0) + rounding_mask) >>
417 (OD_COEFF_SHIFT - coeff_shift);
418 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700419
420 od_coding_order_to_raster(dqcoeff, blk_size, tx_type, dqcoeff_pvq, blk_size);
421
422 eob = blk_size * blk_size;
423
424 return eob;
425}
426
ltrudeaue1c09292017-01-20 15:42:13 -0500427static PVQ_SKIP_TYPE read_pvq_skip(AV1_COMMON *cm, MACROBLOCKD *const xd,
428 int plane, TX_SIZE tx_size) {
429 // decode ac/dc coded flag. bit0: DC coded, bit1 : AC coded
430 // NOTE : we don't use 5 symbols for luma here in aom codebase,
431 // since block partition is taken care of by aom.
432 // So, only AC/DC skip info is coded
Yushin Cho00779272017-02-21 10:38:16 -0800433 const int ac_dc_coded = aom_read_symbol(
ltrudeaue1c09292017-01-20 15:42:13 -0500434 xd->daala_dec.r,
Yushin Choc49ef3a2017-03-13 17:27:25 -0700435 xd->daala_dec.state.adapt->skip_cdf[2 * tx_size + (plane != 0)], 4,
Yushin Cho00779272017-02-21 10:38:16 -0800436 "skip");
ltrudeaue1c09292017-01-20 15:42:13 -0500437 if (ac_dc_coded < 0 || ac_dc_coded > 3) {
438 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
439 "Invalid PVQ Skip Type");
440 }
441 return ac_dc_coded;
442}
443
444static int av1_pvq_decode_helper2(AV1_COMMON *cm, MACROBLOCKD *const xd,
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800445 MB_MODE_INFO *const mbmi, int plane, int row,
446 int col, TX_SIZE tx_size, TX_TYPE tx_type) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700447 struct macroblockd_plane *const pd = &xd->plane[plane];
448 // transform block size in pixels
449 int tx_blk_size = tx_size_wide[tx_size];
450 int i, j;
451 tran_low_t *pvq_ref_coeff = pd->pvq_ref_coeff;
452 const int diff_stride = tx_blk_size;
453 int16_t *pred = pd->pred;
454 tran_low_t *const dqcoeff = pd->dqcoeff;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700455 uint8_t *dst;
456 int eob;
ltrudeaue1c09292017-01-20 15:42:13 -0500457 const PVQ_SKIP_TYPE ac_dc_coded = read_pvq_skip(cm, xd, plane, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700458
459 eob = 0;
460 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
461
Yushin Cho77bba8d2016-11-04 16:36:56 -0700462 if (ac_dc_coded) {
463 int xdec = pd->subsampling_x;
464 int seg_id = mbmi->segment_id;
465 int16_t *quant;
466 FWD_TXFM_PARAM fwd_txfm_param;
Yaowu Xufc1b2132016-11-07 15:16:15 -0800467 // ToDo(yaowu): correct this with optimal number from decoding process.
468 const int max_scan_line = tx_size_2d[tx_size];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200469#if CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -0800470 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
471 for (j = 0; j < tx_blk_size; j++)
472 for (i = 0; i < tx_blk_size; i++)
473 pred[diff_stride * j + i] =
474 CONVERT_TO_SHORTPTR(dst)[pd->dst.stride * j + i];
475 } else {
476#endif
477 for (j = 0; j < tx_blk_size; j++)
478 for (i = 0; i < tx_blk_size; i++)
479 pred[diff_stride * j + i] = dst[pd->dst.stride * j + i];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200480#if CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -0800481 }
482#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -0700483
484 fwd_txfm_param.tx_type = tx_type;
485 fwd_txfm_param.tx_size = tx_size;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700486 fwd_txfm_param.lossless = xd->lossless[seg_id];
487
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200488#if CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -0800489 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
490 fwd_txfm_param.bd = xd->bd;
hui suf11fb882017-03-27 14:56:33 -0700491 av1_highbd_fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
Thomas Daede6ff6af62017-02-03 16:29:24 -0800492 } else {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200493#endif // CONFIG_HIGHBITDEPTH
hui suf11fb882017-03-27 14:56:33 -0700494 av1_fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200495#if CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -0800496 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200497#endif // CONFIG_HIGHBITDEPTH
Yushin Cho77bba8d2016-11-04 16:36:56 -0700498
499 quant = &pd->seg_dequant[seg_id][0]; // aom's quantizer
500
Thomas Daede6ff6af62017-02-03 16:29:24 -0800501 eob = av1_pvq_decode_helper(xd, pvq_ref_coeff, dqcoeff, quant, plane,
502 tx_size, tx_type, xdec, ac_dc_coded);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700503
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800504 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
505 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700506 }
507
508 return eob;
509}
510#endif
511
Angie Chiang752ccce2017-04-09 13:41:13 -0700512static int get_block_idx(const MACROBLOCKD *xd, int plane, int row, int col) {
513 const int bsize = xd->mi[0]->mbmi.sb_type;
514 const struct macroblockd_plane *pd = &xd->plane[plane];
515#if CONFIG_CB4X4
516#if CONFIG_CHROMA_2X2
517 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
518#else
519 const BLOCK_SIZE plane_bsize =
520 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
521#endif // CONFIG_CHROMA_2X2
522#else
523 const BLOCK_SIZE plane_bsize =
524 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
525#endif
526 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
527 const TX_SIZE tx_size = get_tx_size(plane, xd);
528 const uint8_t txh_unit = tx_size_high_unit[tx_size];
529 return row * max_blocks_wide + col * txh_unit;
530}
531
Alex Converse8aca36d2017-01-31 12:33:15 -0800532static void predict_and_reconstruct_intra_block(
533 AV1_COMMON *cm, MACROBLOCKD *const xd, aom_reader *const r,
534 MB_MODE_INFO *const mbmi, int plane, int row, int col, TX_SIZE tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500535 PLANE_TYPE plane_type = get_plane_type(plane);
Angie Chiang752ccce2017-04-09 13:41:13 -0700536 const int block_idx = get_block_idx(xd, plane, row, col);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700537#if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -0700538 (void)r;
539#endif
Angie Chiang3d005e42017-04-02 16:31:35 -0700540 av1_predict_intra_block_facade(xd, plane, block_idx, col, row, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700541
542 if (!mbmi->skip) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700543#if !CONFIG_PVQ
Angie Chiang3d005e42017-04-02 16:31:35 -0700544 struct macroblockd_plane *const pd = &xd->plane[plane];
Angie Chiang133733c2017-03-17 12:50:20 -0700545#if CONFIG_LV_MAP
546 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700547 int eob;
548 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane,
549 pd->dqcoeff, &max_scan_line, &eob);
Angie Chiangb6d770c2017-04-14 16:27:57 -0700550 // tx_type will be read out in av1_read_coeffs_txb_facade
551 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Angie Chiang133733c2017-03-17 12:50:20 -0700552#else // CONFIG_LV_MAP
Angie Chiangb6d770c2017-04-14 16:27:57 -0700553 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Angie Chiangff6d8902016-10-21 11:02:09 -0700554 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 0);
Jingning Han1be18782016-10-21 11:48:15 -0700555 int16_t max_scan_line = 0;
556 const int eob =
Angie Chiang5c0568a2017-03-21 16:00:39 -0700557 av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
Jingning Han1be18782016-10-21 11:48:15 -0700558 tx_type, &max_scan_line, r, mbmi->segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700559#endif // CONFIG_LV_MAP
Angie Chiang3d005e42017-04-02 16:31:35 -0700560 if (eob) {
561 uint8_t *dst =
562 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Jingning Han1be18782016-10-21 11:48:15 -0700563 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
564 max_scan_line, eob);
Angie Chiang3d005e42017-04-02 16:31:35 -0700565 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700566#else
Angie Chiangb6d770c2017-04-14 16:27:57 -0700567 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
ltrudeaue1c09292017-01-20 15:42:13 -0500568 av1_pvq_decode_helper2(cm, xd, mbmi, plane, row, col, tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700569#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700570 }
Luc Trudeaue3980282017-04-25 23:17:21 -0400571#if CONFIG_CFL
572 if (plane == AOM_PLANE_Y) {
573 struct macroblockd_plane *const pd = &xd->plane[plane];
574 uint8_t *dst =
575 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
576 cfl_store(xd->cfl, dst, pd->dst.stride, row, col, tx_size);
577 }
578#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700579}
580
Jingning Handddb21f2017-02-28 14:44:05 -0800581#if CONFIG_VAR_TX && !CONFIG_COEF_INTERLEAVE
Angie Chiangff6d8902016-10-21 11:02:09 -0700582static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
583 aom_reader *r, MB_MODE_INFO *const mbmi,
Jingning Han8fd62b72016-10-21 12:55:54 -0700584 int plane, BLOCK_SIZE plane_bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700585 int blk_row, int blk_col, TX_SIZE tx_size,
586 int *eob_total) {
587 const struct macroblockd_plane *const pd = &xd->plane[plane];
588 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
589 const int tx_row = blk_row >> (1 - pd->subsampling_y);
590 const int tx_col = blk_col >> (1 - pd->subsampling_x);
591 const TX_SIZE plane_tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700592 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700593 : mbmi->inter_tx_size[tx_row][tx_col];
Jingning Han5f614262016-10-27 14:27:43 -0700594 // Scale to match transform block unit.
Jingning Hanf64062f2016-11-02 16:22:18 -0700595 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
596 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700597
598 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
599
600 if (tx_size == plane_tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500601 PLANE_TYPE plane_type = get_plane_type(plane);
Angie Chiang752ccce2017-04-09 13:41:13 -0700602 int block_idx = get_block_idx(xd, plane, blk_row, blk_col);
Angie Chiang133733c2017-03-17 12:50:20 -0700603#if CONFIG_LV_MAP
604 (void)segment_id;
605 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700606 int eob;
607 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane,
608 pd->dqcoeff, &max_scan_line, &eob);
Angie Chiangb6d770c2017-04-14 16:27:57 -0700609 // tx_type will be read out in av1_read_coeffs_txb_facade
610 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiang133733c2017-03-17 12:50:20 -0700611#else // CONFIG_LV_MAP
Angie Chiangb6d770c2017-04-14 16:27:57 -0700612 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiangff6d8902016-10-21 11:02:09 -0700613 const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700614 int16_t max_scan_line = 0;
Angie Chiang5c0568a2017-03-21 16:00:39 -0700615 const int eob = av1_decode_block_tokens(
616 cm, xd, plane, sc, blk_col, blk_row, plane_tx_size, tx_type,
617 &max_scan_line, r, mbmi->segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700618#endif // CONFIG_LV_MAP
Jingning Han9ca05b72017-01-03 14:41:36 -0800619 inverse_transform_block(xd, plane, tx_type, plane_tx_size,
620 &pd->dst.buf[(blk_row * pd->dst.stride + blk_col)
621 << tx_size_wide_log2[0]],
622 pd->dst.stride, max_scan_line, eob);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700623 *eob_total += eob;
624 } else {
Jingning Hanf64062f2016-11-02 16:22:18 -0700625 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
626 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700627 int i;
628
629 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700630
631 for (i = 0; i < 4; ++i) {
Jingning Han5f614262016-10-27 14:27:43 -0700632 const int offsetr = blk_row + (i >> 1) * bsl;
633 const int offsetc = blk_col + (i & 0x01) * bsl;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700634
635 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
636
Jingning Han8fd62b72016-10-21 12:55:54 -0700637 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
Jingning Hanf64062f2016-11-02 16:22:18 -0700638 offsetc, sub_txs, eob_total);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700639 }
640 }
641}
642#endif // CONFIG_VAR_TX
643
Jingning Handddb21f2017-02-28 14:44:05 -0800644#if !CONFIG_VAR_TX || CONFIG_SUPERTX || CONFIG_COEF_INTERLEAVE || \
Jingning Hanfe45b212016-11-22 10:30:23 -0800645 (!CONFIG_VAR_TX && CONFIG_EXT_TX && CONFIG_RECT_TX)
Angie Chiangff6d8902016-10-21 11:02:09 -0700646static int reconstruct_inter_block(AV1_COMMON *cm, MACROBLOCKD *const xd,
Alex Converse8aca36d2017-01-31 12:33:15 -0800647 aom_reader *const r, int segment_id,
648 int plane, int row, int col,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700649 TX_SIZE tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500650 PLANE_TYPE plane_type = get_plane_type(plane);
Angie Chiang752ccce2017-04-09 13:41:13 -0700651 int block_idx = get_block_idx(xd, plane, row, col);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700652#if CONFIG_PVQ
653 int eob;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700654 (void)r;
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800655 (void)segment_id;
656#else
657 struct macroblockd_plane *const pd = &xd->plane[plane];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700658#endif
659
660#if !CONFIG_PVQ
Angie Chiang133733c2017-03-17 12:50:20 -0700661#if CONFIG_LV_MAP
662 (void)segment_id;
Jingning Han1be18782016-10-21 11:48:15 -0700663 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700664 int eob;
665 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane, pd->dqcoeff,
666 &max_scan_line, &eob);
Angie Chiangb6d770c2017-04-14 16:27:57 -0700667 // tx_type will be read out in av1_read_coeffs_txb_facade
668 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Angie Chiang133733c2017-03-17 12:50:20 -0700669#else // CONFIG_LV_MAP
670 int16_t max_scan_line = 0;
Angie Chiangb6d770c2017-04-14 16:27:57 -0700671 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Angie Chiang133733c2017-03-17 12:50:20 -0700672 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700673 const int eob =
Angie Chiang5c0568a2017-03-21 16:00:39 -0700674 av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
675 tx_type, &max_scan_line, r, segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700676#endif // CONFIG_LV_MAP
Jingning Hanca14dda2016-12-09 09:36:00 -0800677 uint8_t *dst =
678 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Jingning Han1be18782016-10-21 11:48:15 -0700679 if (eob)
Jingning Hanca14dda2016-12-09 09:36:00 -0800680 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
681 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700682#else
Angie Chiangb6d770c2017-04-14 16:27:57 -0700683 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
ltrudeaue1c09292017-01-20 15:42:13 -0500684 eob = av1_pvq_decode_helper2(cm, xd, &xd->mi[0]->mbmi, plane, row, col,
685 tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700686#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700687 return eob;
688}
689#endif // !CONFIG_VAR_TX || CONFIG_SUPER_TX
690
Angie Chiang44701f22017-02-27 10:36:44 -0800691static void set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
692 BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
693 int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700694 const int offset = mi_row * cm->mi_stride + mi_col;
695 int x, y;
696 const TileInfo *const tile = &xd->tile;
697
698 xd->mi = cm->mi_grid_visible + offset;
699 xd->mi[0] = &cm->mi[offset];
700 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
701 // passing bsize from decode_partition().
702 xd->mi[0]->mbmi.sb_type = bsize;
Angie Chiang394c3372016-11-03 11:13:15 -0700703#if CONFIG_RD_DEBUG
704 xd->mi[0]->mbmi.mi_row = mi_row;
705 xd->mi[0]->mbmi.mi_col = mi_col;
706#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700707 for (y = 0; y < y_mis; ++y)
Jingning Han97d85482016-07-15 11:06:05 -0700708 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700709
Jingning Hanfaad0e12016-12-07 10:54:57 -0800710 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700711 set_skip_context(xd, mi_row, mi_col);
712
713#if CONFIG_VAR_TX
714 xd->max_tx_size = max_txsize_lookup[bsize];
715#endif
716
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800717// Distance of Mb to the various image edges. These are specified to 8th pel
718// as they are always compared to values that are in 1/8th pel units
719#if CONFIG_DEPENDENT_HORZTILES
720 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
721 cm->dependent_horz_tiles);
722#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700723 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 -0800724#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700725
Yaowu Xuf883b422016-08-30 14:01:10 -0700726 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700727}
728
729#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700730static MB_MODE_INFO *set_offsets_extend(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700731 MACROBLOCKD *const xd,
732 const TileInfo *const tile,
733 BLOCK_SIZE bsize_pred, int mi_row_pred,
734 int mi_col_pred, int mi_row_ori,
735 int mi_col_ori) {
736 // Used in supertx
737 // (mi_row_ori, mi_col_ori): location for mv
738 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
Jingning Han93531242016-12-20 11:54:36 -0800739 const int bw = mi_size_wide[bsize_pred];
740 const int bh = mi_size_high[bsize_pred];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700741 const int offset = mi_row_ori * cm->mi_stride + mi_col_ori;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700742 xd->mi = cm->mi_grid_visible + offset;
743 xd->mi[0] = cm->mi + offset;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800744#if CONFIG_DEPENDENT_HORZTILES
745 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
746 cm->mi_cols, cm->dependent_horz_tiles);
747#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700748 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
749 cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800750#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700751
752 xd->up_available = (mi_row_ori > tile->mi_row_start);
753 xd->left_available = (mi_col_ori > tile->mi_col_start);
754
Jingning Hanfaad0e12016-12-07 10:54:57 -0800755 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700756
757 return &xd->mi[0]->mbmi;
758}
759
Angie Chiang7fcfee42017-02-24 15:51:03 -0800760#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700761static MB_MODE_INFO *set_mb_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
762 BLOCK_SIZE bsize, int mi_row, int mi_col,
763 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700764 const int offset = mi_row * cm->mi_stride + mi_col;
765 const TileInfo *const tile = &xd->tile;
766 int x, y;
767
768 xd->mi = cm->mi_grid_visible + offset;
769 xd->mi[0] = cm->mi + offset;
770 xd->mi[0]->mbmi.sb_type = bsize;
771 for (y = 0; y < y_mis; ++y)
772 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
773
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800774#if CONFIG_DEPENDENT_HORZTILES
775 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
776 cm->dependent_horz_tiles);
777#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700778 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 -0800779#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700780 return &xd->mi[0]->mbmi;
781}
Angie Chiang7fcfee42017-02-24 15:51:03 -0800782#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700783
Yaowu Xuf883b422016-08-30 14:01:10 -0700784static void set_offsets_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700785 const TileInfo *const tile, BLOCK_SIZE bsize,
786 int mi_row, int mi_col) {
Jingning Han93531242016-12-20 11:54:36 -0800787 const int bw = mi_size_wide[bsize];
788 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700789 const int offset = mi_row * cm->mi_stride + mi_col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700790
791 xd->mi = cm->mi_grid_visible + offset;
792 xd->mi[0] = cm->mi + offset;
793
Jingning Hanfaad0e12016-12-07 10:54:57 -0800794 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700795
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800796#if CONFIG_DEPENDENT_HORZTILES
797 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols,
798 cm->dependent_horz_tiles);
799#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800 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 -0800801#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700802
Yaowu Xuf883b422016-08-30 14:01:10 -0700803 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700804}
805
Yaowu Xuf883b422016-08-30 14:01:10 -0700806static void set_param_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700807 BLOCK_SIZE bsize, int mi_row, int mi_col,
808 int txfm, int skip) {
Jingning Han93531242016-12-20 11:54:36 -0800809 const int bw = mi_size_wide[bsize];
810 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -0700811 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
812 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700813 const int offset = mi_row * cm->mi_stride + mi_col;
814 int x, y;
815
816 xd->mi = cm->mi_grid_visible + offset;
817 xd->mi[0] = cm->mi + offset;
818
819 for (y = 0; y < y_mis; ++y)
820 for (x = 0; x < x_mis; ++x) {
821 xd->mi[y * cm->mi_stride + x]->mbmi.skip = skip;
822 xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
823 }
824#if CONFIG_VAR_TX
825 xd->above_txfm_context = cm->above_txfm_context + mi_col;
826 xd->left_txfm_context =
827 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
Yaowu Xu52a17632016-11-17 15:48:21 -0800828 set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bw, bh, skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700829#endif
830}
831
Yaowu Xuf883b422016-08-30 14:01:10 -0700832static void set_ref(AV1_COMMON *const cm, MACROBLOCKD *const xd, int idx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700833 int mi_row, int mi_col) {
834 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
835 RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
836 xd->block_refs[idx] = ref_buffer;
Yaowu Xuf883b422016-08-30 14:01:10 -0700837 if (!av1_is_valid_scale(&ref_buffer->sf))
838 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700839 "Invalid scale factors");
Yaowu Xuf883b422016-08-30 14:01:10 -0700840 av1_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
841 &ref_buffer->sf);
Angie Chiangd0916d92017-03-10 17:54:18 -0800842 aom_merge_corrupted_flag(&xd->corrupted, ref_buffer->buf->corrupted);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700843}
844
845static void dec_predict_b_extend(
Yaowu Xuf883b422016-08-30 14:01:10 -0700846 AV1Decoder *const pbi, MACROBLOCKD *const xd, const TileInfo *const tile,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700847 int block, int mi_row_ori, int mi_col_ori, int mi_row_pred, int mi_col_pred,
848 int mi_row_top, int mi_col_top, uint8_t *dst_buf[3], int dst_stride[3],
849 BLOCK_SIZE bsize_top, BLOCK_SIZE bsize_pred, int b_sub8x8, int bextend) {
850 // Used in supertx
851 // (mi_row_ori, mi_col_ori): location for mv
852 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
853 // (mi_row_top, mi_col_top, bsize_top): region of the top partition size
854 // block: sub location of sub8x8 blocks
855 // b_sub8x8: 1: ori is sub8x8; 0: ori is not sub8x8
856 // bextend: 1: region to predict is an extension of ori; 0: not
857 int r = (mi_row_pred - mi_row_top) * MI_SIZE;
858 int c = (mi_col_pred - mi_col_top) * MI_SIZE;
Jingning Han93531242016-12-20 11:54:36 -0800859 const int mi_width_top = mi_size_wide[bsize_top];
860 const int mi_height_top = mi_size_high[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700861 MB_MODE_INFO *mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700862 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700863
864 if (mi_row_pred < mi_row_top || mi_col_pred < mi_col_top ||
865 mi_row_pred >= mi_row_top + mi_height_top ||
866 mi_col_pred >= mi_col_top + mi_width_top || mi_row_pred >= cm->mi_rows ||
867 mi_col_pred >= cm->mi_cols)
868 return;
869
870 mbmi = set_offsets_extend(cm, xd, tile, bsize_pred, mi_row_pred, mi_col_pred,
871 mi_row_ori, mi_col_ori);
872 set_ref(cm, xd, 0, mi_row_pred, mi_col_pred);
873 if (has_second_ref(&xd->mi[0]->mbmi))
874 set_ref(cm, xd, 1, mi_row_pred, mi_col_pred);
875
Jingning Han2511c662016-12-22 11:57:34 -0800876 if (!bextend) mbmi->tx_size = max_txsize_lookup[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700877
878 xd->plane[0].dst.stride = dst_stride[0];
879 xd->plane[1].dst.stride = dst_stride[1];
880 xd->plane[2].dst.stride = dst_stride[2];
881 xd->plane[0].dst.buf = dst_buf[0] +
882 (r >> xd->plane[0].subsampling_y) * dst_stride[0] +
883 (c >> xd->plane[0].subsampling_x);
884 xd->plane[1].dst.buf = dst_buf[1] +
885 (r >> xd->plane[1].subsampling_y) * dst_stride[1] +
886 (c >> xd->plane[1].subsampling_x);
887 xd->plane[2].dst.buf = dst_buf[2] +
888 (r >> xd->plane[2].subsampling_y) * dst_stride[2] +
889 (c >> xd->plane[2].subsampling_x);
890
891 if (!b_sub8x8)
Yaowu Xuf883b422016-08-30 14:01:10 -0700892 av1_build_inter_predictors_sb_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700893#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700894 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700895#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700896 mi_row_pred, mi_col_pred, bsize_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700897 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700898 av1_build_inter_predictors_sb_sub8x8_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700899#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700900 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700901#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700902 mi_row_pred, mi_col_pred,
903 bsize_pred, block);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700904}
905
Yaowu Xuf883b422016-08-30 14:01:10 -0700906static void dec_extend_dir(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700907 const TileInfo *const tile, int block,
908 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
909 int mi_col, int mi_row_top, int mi_col_top,
910 uint8_t *dst_buf[3], int dst_stride[3], int dir) {
911 // dir: 0-lower, 1-upper, 2-left, 3-right
912 // 4-lowerleft, 5-upperleft, 6-lowerright, 7-upperright
Jingning Han93531242016-12-20 11:54:36 -0800913 const int mi_width = mi_size_wide[bsize];
914 const int mi_height = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700915 int xss = xd->plane[1].subsampling_x;
916 int yss = xd->plane[1].subsampling_y;
Jingning Hanfeb517c2016-12-21 16:02:07 -0800917#if CONFIG_CB4X4
918 const int unify_bsize = 1;
919#else
920 const int unify_bsize = 0;
921#endif
922 int b_sub8x8 = (bsize < BLOCK_8X8) && !unify_bsize ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700923 BLOCK_SIZE extend_bsize;
Jingning Han24f24a52016-12-27 10:13:28 -0800924 int mi_row_pred, mi_col_pred;
925
926 int wide_unit, high_unit;
927 int i, j;
928 int ext_offset = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700929
930 if (dir == 0 || dir == 1) {
Jingning Han93531242016-12-20 11:54:36 -0800931 extend_bsize =
932 (mi_width == mi_size_wide[BLOCK_8X8] || bsize < BLOCK_8X8 || xss < yss)
933 ? BLOCK_8X8
934 : BLOCK_16X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800935#if CONFIG_CB4X4
936 if (bsize < BLOCK_8X8) {
937 extend_bsize = BLOCK_4X4;
938 ext_offset = mi_size_wide[BLOCK_8X8];
939 }
940#endif
941
942 wide_unit = mi_size_wide[extend_bsize];
943 high_unit = mi_size_high[extend_bsize];
944
945 mi_row_pred = mi_row + ((dir == 0) ? mi_height : -(mi_height + ext_offset));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700946 mi_col_pred = mi_col;
947
Jingning Han24f24a52016-12-27 10:13:28 -0800948 for (j = 0; j < mi_height + ext_offset; j += high_unit)
949 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
950 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
951 mi_row_pred + j, mi_col_pred + i, mi_row_top,
952 mi_col_top, dst_buf, dst_stride, top_bsize,
953 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700954 } else if (dir == 2 || dir == 3) {
Jingning Han5b7706a2016-12-21 09:55:10 -0800955 extend_bsize =
956 (mi_height == mi_size_high[BLOCK_8X8] || bsize < BLOCK_8X8 || yss < xss)
957 ? BLOCK_8X8
958 : BLOCK_8X16;
Jingning Han24f24a52016-12-27 10:13:28 -0800959#if CONFIG_CB4X4
960 if (bsize < BLOCK_8X8) {
961 extend_bsize = BLOCK_4X4;
962 ext_offset = mi_size_wide[BLOCK_8X8];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700963 }
Jingning Han24f24a52016-12-27 10:13:28 -0800964#endif
965
966 wide_unit = mi_size_wide[extend_bsize];
967 high_unit = mi_size_high[extend_bsize];
968
969 mi_row_pred = mi_row;
970 mi_col_pred = mi_col + ((dir == 3) ? mi_width : -(mi_width + ext_offset));
971
972 for (j = 0; j < mi_height + ext_offset; j += high_unit)
973 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
974 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
975 mi_row_pred + j, mi_col_pred + i, mi_row_top,
976 mi_col_top, dst_buf, dst_stride, top_bsize,
977 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700978 } else {
979 extend_bsize = BLOCK_8X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800980#if CONFIG_CB4X4
981 if (bsize < BLOCK_8X8) {
982 extend_bsize = BLOCK_4X4;
983 ext_offset = mi_size_wide[BLOCK_8X8];
984 }
985#endif
986 wide_unit = mi_size_wide[extend_bsize];
987 high_unit = mi_size_high[extend_bsize];
988
Jingning Han5b7706a2016-12-21 09:55:10 -0800989 mi_row_pred = mi_row + ((dir == 4 || dir == 6) ? mi_height
Jingning Han24f24a52016-12-27 10:13:28 -0800990 : -(mi_height + ext_offset));
Jingning Han5b7706a2016-12-21 09:55:10 -0800991 mi_col_pred =
Jingning Han24f24a52016-12-27 10:13:28 -0800992 mi_col + ((dir == 6 || dir == 7) ? mi_width : -(mi_width + ext_offset));
993
994 for (j = 0; j < mi_height + ext_offset; j += high_unit)
995 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
996 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
997 mi_row_pred + j, mi_col_pred + i, mi_row_top,
998 mi_col_top, dst_buf, dst_stride, top_bsize,
999 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001000 }
1001}
1002
Yaowu Xuf883b422016-08-30 14:01:10 -07001003static void dec_extend_all(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001004 const TileInfo *const tile, int block,
1005 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
1006 int mi_col, int mi_row_top, int mi_col_top,
1007 uint8_t *dst_buf[3], int dst_stride[3]) {
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, 0);
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, 1);
1012 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1013 mi_row_top, mi_col_top, dst_buf, dst_stride, 2);
1014 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1015 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1016 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1017 mi_row_top, mi_col_top, dst_buf, dst_stride, 4);
1018 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1019 mi_row_top, mi_col_top, dst_buf, dst_stride, 5);
1020 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1021 mi_row_top, mi_col_top, dst_buf, dst_stride, 6);
1022 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
1023 mi_row_top, mi_col_top, dst_buf, dst_stride, 7);
1024}
1025
Yaowu Xuf883b422016-08-30 14:01:10 -07001026static void dec_predict_sb_complex(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001027 const TileInfo *const tile, int mi_row,
1028 int mi_col, int mi_row_top, int mi_col_top,
1029 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize,
1030 uint8_t *dst_buf[3], int dst_stride[3]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001031 const AV1_COMMON *const cm = &pbi->common;
Jingning Han5b7706a2016-12-21 09:55:10 -08001032 const int hbs = mi_size_wide[bsize] / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001033 const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
1034 const BLOCK_SIZE subsize = get_subsize(bsize, partition);
1035#if CONFIG_EXT_PARTITION_TYPES
1036 const BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1037#endif
1038 int i;
1039 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1040 uint8_t *dst_buf1[3], *dst_buf2[3], *dst_buf3[3];
Jingning Hanfeb517c2016-12-21 16:02:07 -08001041#if CONFIG_CB4X4
1042 const int unify_bsize = 1;
1043#else
1044 const int unify_bsize = 0;
1045#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001046
1047 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1048 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1049 DECLARE_ALIGNED(16, uint8_t, tmp_buf3[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1050 int dst_stride1[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1051 int dst_stride2[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1052 int dst_stride3[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1053
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001054#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001055 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1056 int len = sizeof(uint16_t);
1057 dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
1058 dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_TX_SQUARE * len);
1059 dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_TX_SQUARE * len);
1060 dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
1061 dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_TX_SQUARE * len);
1062 dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_TX_SQUARE * len);
1063 dst_buf3[0] = CONVERT_TO_BYTEPTR(tmp_buf3);
1064 dst_buf3[1] = CONVERT_TO_BYTEPTR(tmp_buf3 + MAX_TX_SQUARE * len);
1065 dst_buf3[2] = CONVERT_TO_BYTEPTR(tmp_buf3 + 2 * MAX_TX_SQUARE * len);
1066 } else {
1067#endif
1068 dst_buf1[0] = tmp_buf1;
1069 dst_buf1[1] = tmp_buf1 + MAX_TX_SQUARE;
1070 dst_buf1[2] = tmp_buf1 + 2 * MAX_TX_SQUARE;
1071 dst_buf2[0] = tmp_buf2;
1072 dst_buf2[1] = tmp_buf2 + MAX_TX_SQUARE;
1073 dst_buf2[2] = tmp_buf2 + 2 * MAX_TX_SQUARE;
1074 dst_buf3[0] = tmp_buf3;
1075 dst_buf3[1] = tmp_buf3 + MAX_TX_SQUARE;
1076 dst_buf3[2] = tmp_buf3 + 2 * MAX_TX_SQUARE;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001077#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001078 }
1079#endif
1080
1081 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1082
1083 xd->mi = cm->mi_grid_visible + mi_offset;
1084 xd->mi[0] = cm->mi + mi_offset;
1085
1086 for (i = 0; i < MAX_MB_PLANE; i++) {
1087 xd->plane[i].dst.buf = dst_buf[i];
1088 xd->plane[i].dst.stride = dst_stride[i];
1089 }
1090
1091 switch (partition) {
1092 case PARTITION_NONE:
1093 assert(bsize < top_bsize);
1094 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1095 mi_row_top, mi_col_top, dst_buf, dst_stride,
1096 top_bsize, bsize, 0, 0);
1097 dec_extend_all(pbi, xd, tile, 0, bsize, top_bsize, mi_row, mi_col,
1098 mi_row_top, mi_col_top, dst_buf, dst_stride);
1099 break;
1100 case PARTITION_HORZ:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001101 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001102 // For sub8x8, predict in 8x8 unit
1103 // First half
1104 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1105 mi_row_top, mi_col_top, dst_buf, dst_stride,
1106 top_bsize, BLOCK_8X8, 1, 0);
1107 if (bsize < top_bsize)
1108 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1109 mi_row_top, mi_col_top, dst_buf, dst_stride);
1110
1111 // Second half
1112 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1113 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1114 top_bsize, BLOCK_8X8, 1, 1);
1115 if (bsize < top_bsize)
1116 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1117 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1118
1119 // weighted average to smooth the boundary
1120 xd->plane[0].dst.buf = dst_buf[0];
1121 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001122 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001123 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1124 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1125 0);
1126 } else {
1127 // First half
1128 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1129 mi_row_top, mi_col_top, dst_buf, dst_stride,
1130 top_bsize, subsize, 0, 0);
1131 if (bsize < top_bsize)
1132 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1133 mi_row_top, mi_col_top, dst_buf, dst_stride);
1134 else
1135 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1136 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1137
1138 if (mi_row + hbs < cm->mi_rows) {
1139 // Second half
1140 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col,
1141 mi_row + hbs, mi_col, mi_row_top, mi_col_top,
1142 dst_buf1, dst_stride1, top_bsize, subsize, 0, 0);
1143 if (bsize < top_bsize)
1144 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1145 mi_col, mi_row_top, mi_col_top, dst_buf1,
1146 dst_stride1);
1147 else
1148 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1149 mi_col, mi_row_top, mi_col_top, dst_buf1,
1150 dst_stride1, 1);
1151
1152 // weighted average to smooth the boundary
1153 for (i = 0; i < MAX_MB_PLANE; i++) {
1154 xd->plane[i].dst.buf = dst_buf[i];
1155 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001156 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001157 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1158 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1159 PARTITION_HORZ, i);
1160 }
1161 }
1162 }
1163 break;
1164 case PARTITION_VERT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001165 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001166 // First half
1167 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1168 mi_row_top, mi_col_top, dst_buf, dst_stride,
1169 top_bsize, BLOCK_8X8, 1, 0);
1170 if (bsize < top_bsize)
1171 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1172 mi_row_top, mi_col_top, dst_buf, dst_stride);
1173
1174 // Second half
1175 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1176 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1177 top_bsize, BLOCK_8X8, 1, 1);
1178 if (bsize < top_bsize)
1179 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1180 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1181
1182 // Smooth
1183 xd->plane[0].dst.buf = dst_buf[0];
1184 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001185 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001186 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1187 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1188 0);
1189 } else {
1190 // First half
1191 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1192 mi_row_top, mi_col_top, dst_buf, dst_stride,
1193 top_bsize, subsize, 0, 0);
1194 if (bsize < top_bsize)
1195 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1196 mi_row_top, mi_col_top, dst_buf, dst_stride);
1197 else
1198 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1199 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1200
1201 // Second half
1202 if (mi_col + hbs < cm->mi_cols) {
1203 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1204 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1205 dst_stride1, top_bsize, subsize, 0, 0);
1206 if (bsize < top_bsize)
1207 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1208 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1209 dst_stride1);
1210 else
1211 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1212 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1213 dst_stride1, 2);
1214
1215 // Smooth
1216 for (i = 0; i < MAX_MB_PLANE; i++) {
1217 xd->plane[i].dst.buf = dst_buf[i];
1218 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001219 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001220 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1221 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1222 PARTITION_VERT, i);
1223 }
1224 }
1225 }
1226 break;
1227 case PARTITION_SPLIT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001228 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001229 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1230 mi_row_top, mi_col_top, dst_buf, dst_stride,
1231 top_bsize, BLOCK_8X8, 1, 0);
1232 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1233 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1234 top_bsize, BLOCK_8X8, 1, 1);
1235 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1236 mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1237 top_bsize, BLOCK_8X8, 1, 1);
1238 dec_predict_b_extend(pbi, xd, tile, 3, mi_row, mi_col, mi_row, mi_col,
1239 mi_row_top, mi_col_top, dst_buf3, dst_stride3,
1240 top_bsize, BLOCK_8X8, 1, 1);
1241 if (bsize < top_bsize) {
1242 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1243 mi_row_top, mi_col_top, dst_buf, dst_stride);
1244 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1245 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1246 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1247 mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1248 dec_extend_all(pbi, xd, tile, 3, subsize, top_bsize, mi_row, mi_col,
1249 mi_row_top, mi_col_top, dst_buf3, dst_stride3);
1250 }
1251 } else {
1252 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row_top,
1253 mi_col_top, subsize, top_bsize, dst_buf,
1254 dst_stride);
1255 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1256 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col + hbs,
1257 mi_row_top, mi_col_top, subsize, top_bsize,
1258 dst_buf1, dst_stride1);
1259 if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols)
1260 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col,
1261 mi_row_top, mi_col_top, subsize, top_bsize,
1262 dst_buf2, dst_stride2);
1263 if (mi_row + hbs < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1264 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col + hbs,
1265 mi_row_top, mi_col_top, subsize, top_bsize,
1266 dst_buf3, dst_stride3);
1267 }
1268 for (i = 0; i < MAX_MB_PLANE; i++) {
Jingning Han9e0976a2016-12-27 17:52:42 -08001269#if !CONFIG_CB4X4
Jingning Han24f24a52016-12-27 10:13:28 -08001270 if (bsize == BLOCK_8X8 && i != 0)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001271 continue; // Skip <4x4 chroma smoothing
Jingning Han9e0976a2016-12-27 17:52:42 -08001272#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001273 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001274 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001275 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1276 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1277 PARTITION_VERT, i);
1278 if (mi_row + hbs < cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001279 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001280 xd, dst_buf2[i], dst_stride2[i], dst_buf3[i], dst_stride3[i],
1281 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1282 PARTITION_VERT, i);
Yaowu Xuf883b422016-08-30 14:01:10 -07001283 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001284 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1285 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1286 PARTITION_HORZ, i);
1287 }
1288 } else if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001289 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001290 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1291 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1292 PARTITION_HORZ, i);
1293 }
1294 }
1295 break;
1296#if CONFIG_EXT_PARTITION_TYPES
1297 case PARTITION_HORZ_A:
1298 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1299 mi_row_top, mi_col_top, dst_buf, dst_stride,
1300 top_bsize, bsize2, 0, 0);
1301 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1302 mi_row_top, mi_col_top, dst_buf, dst_stride);
1303
1304 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1305 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1306 dst_stride1, top_bsize, bsize2, 0, 0);
1307 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1308 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1309
1310 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1311 mi_col, mi_row_top, mi_col_top, dst_buf2,
1312 dst_stride2, top_bsize, subsize, 0, 0);
1313 if (bsize < top_bsize)
1314 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1315 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1316 else
1317 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1318 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1319 1);
1320
1321 for (i = 0; i < MAX_MB_PLANE; i++) {
1322 xd->plane[i].dst.buf = dst_buf[i];
1323 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001324 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001325 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1326 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1327 i);
1328 }
1329 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001330 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001331 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1332 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1333 i);
1334 }
1335 break;
1336 case PARTITION_VERT_A:
1337
1338 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1339 mi_row_top, mi_col_top, dst_buf, dst_stride,
1340 top_bsize, bsize2, 0, 0);
1341 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1342 mi_row_top, mi_col_top, dst_buf, dst_stride);
1343
1344 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1345 mi_col, mi_row_top, mi_col_top, dst_buf1,
1346 dst_stride1, top_bsize, bsize2, 0, 0);
1347 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1348 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1349
1350 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1351 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1352 dst_stride2, top_bsize, subsize, 0, 0);
1353 if (bsize < top_bsize)
1354 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1355 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1356 dst_stride2);
1357 else
1358 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1359 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1360 dst_stride2, 2);
1361
1362 for (i = 0; i < MAX_MB_PLANE; i++) {
1363 xd->plane[i].dst.buf = dst_buf[i];
1364 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001365 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001366 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1367 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1368 i);
1369 }
1370 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001371 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001372 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1373 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1374 i);
1375 }
1376 break;
1377 case PARTITION_HORZ_B:
1378 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1379 mi_row_top, mi_col_top, dst_buf, dst_stride,
1380 top_bsize, subsize, 0, 0);
1381 if (bsize < top_bsize)
1382 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1383 mi_row_top, mi_col_top, dst_buf, dst_stride);
1384 else
1385 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1386 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1387
1388 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1389 mi_col, mi_row_top, mi_col_top, dst_buf1,
1390 dst_stride1, top_bsize, bsize2, 0, 0);
1391 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1392 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1393
1394 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1395 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1396 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1397 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1398 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1399 dst_stride2);
1400
1401 for (i = 0; i < MAX_MB_PLANE; i++) {
1402 xd->plane[i].dst.buf = dst_buf1[i];
1403 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001404 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001405 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1406 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1407 PARTITION_VERT, i);
1408 }
1409 for (i = 0; i < MAX_MB_PLANE; i++) {
1410 xd->plane[i].dst.buf = dst_buf[i];
1411 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001412 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001413 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1414 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1415 i);
1416 }
1417 break;
1418 case PARTITION_VERT_B:
1419 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1420 mi_row_top, mi_col_top, dst_buf, dst_stride,
1421 top_bsize, subsize, 0, 0);
1422 if (bsize < top_bsize)
1423 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1424 mi_row_top, mi_col_top, dst_buf, dst_stride);
1425 else
1426 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1427 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1428
1429 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1430 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1431 dst_stride1, top_bsize, bsize2, 0, 0);
1432 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1433 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1434
1435 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1436 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1437 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1438 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1439 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1440 dst_stride2);
1441
1442 for (i = 0; i < MAX_MB_PLANE; i++) {
1443 xd->plane[i].dst.buf = dst_buf1[i];
1444 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001445 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001446 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1447 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1448 PARTITION_HORZ, i);
1449 }
1450 for (i = 0; i < MAX_MB_PLANE; i++) {
1451 xd->plane[i].dst.buf = dst_buf[i];
1452 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001453 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001454 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1455 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1456 i);
1457 }
1458 break;
1459#endif // CONFIG_EXT_PARTITION_TYPES
1460 default: assert(0);
1461 }
1462}
1463
Yaowu Xu4ff59b52017-04-24 12:41:56 -07001464static void set_segment_id_supertx(const AV1_COMMON *const cm, int mi_row,
1465 int mi_col, BLOCK_SIZE bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001466 const struct segmentation *seg = &cm->seg;
Jingning Han5b7706a2016-12-21 09:55:10 -08001467 const int miw = AOMMIN(mi_size_wide[bsize], cm->mi_cols - mi_col);
1468 const int mih = AOMMIN(mi_size_high[bsize], cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001469 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1470 MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
1471 int r, c;
1472 int seg_id_supertx = MAX_SEGMENTS;
1473
1474 if (!seg->enabled) {
1475 seg_id_supertx = 0;
1476 } else {
1477 // Find the minimum segment_id
1478 for (r = 0; r < mih; r++)
1479 for (c = 0; c < miw; c++)
1480 seg_id_supertx =
Yaowu Xuf883b422016-08-30 14:01:10 -07001481 AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001482 assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);
1483 }
1484
1485 // Assign the the segment_id back to segment_id_supertx
1486 for (r = 0; r < mih; r++)
1487 for (c = 0; c < miw; c++)
1488 mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
1489}
1490#endif // CONFIG_SUPERTX
1491
Yue Chen64550b62017-01-12 12:18:22 -08001492static void decode_mbmi_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001493#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001494 int supertx_enabled,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001495#endif // CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001496 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001497#if CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001498 PARTITION_TYPE partition,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001499#endif // CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001500 BLOCK_SIZE bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001501 AV1_COMMON *const cm = &pbi->common;
Jingning Han85dc03f2016-12-06 16:03:10 -08001502 const int bw = mi_size_wide[bsize];
1503 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -07001504 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1505 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Nathan E. Eggeebbd4792016-10-05 19:30:15 -04001506
Michael Bebenita6048d052016-08-25 14:40:54 -07001507#if CONFIG_ACCOUNTING
1508 aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
1509#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001510#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001511 if (supertx_enabled) {
Yue Chen64550b62017-01-12 12:18:22 -08001512 set_mb_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001513 } else {
Yue Chen64550b62017-01-12 12:18:22 -08001514 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001515 }
1516#if CONFIG_EXT_PARTITION_TYPES
1517 xd->mi[0]->mbmi.partition = partition;
1518#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001519 av1_read_mode_info(pbi, xd, supertx_enabled, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001520#else
Yue Chen64550b62017-01-12 12:18:22 -08001521 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001522#if CONFIG_EXT_PARTITION_TYPES
1523 xd->mi[0]->mbmi.partition = partition;
1524#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001525 av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001526#endif // CONFIG_SUPERTX
1527
1528 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1529 const BLOCK_SIZE uv_subsize =
1530 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1531 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -07001532 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001533 "Invalid block size.");
1534 }
1535
1536#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001537 xd->mi[0]->mbmi.segment_id_supertx = MAX_SEGMENTS;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001538#endif // CONFIG_SUPERTX
1539
Angie Chiangd0916d92017-03-10 17:54:18 -08001540 int reader_corrupted_flag = aom_reader_has_error(r);
1541 aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
Yue Chen64550b62017-01-12 12:18:22 -08001542}
1543
1544static void decode_token_and_recon_block(AV1Decoder *const pbi,
1545 MACROBLOCKD *const xd, int mi_row,
1546 int mi_col, aom_reader *r,
1547 BLOCK_SIZE bsize) {
1548 AV1_COMMON *const cm = &pbi->common;
1549 const int bw = mi_size_wide[bsize];
1550 const int bh = mi_size_high[bsize];
1551 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1552 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yue Chen64550b62017-01-12 12:18:22 -08001553
Angie Chiang44701f22017-02-27 10:36:44 -08001554 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
1555 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Yue Chen19e7aa82016-11-30 14:05:39 -08001556
Arild Fuldseth07441162016-08-15 15:07:52 +02001557#if CONFIG_DELTA_Q
1558 if (cm->delta_q_present_flag) {
1559 int i;
1560 for (i = 0; i < MAX_SEGMENTS; i++) {
Fangwen Fu6160df22017-04-24 09:45:51 -07001561#if CONFIG_EXT_DELTA_Q
1562 xd->plane[0].seg_dequant[i][0] =
1563 av1_dc_quant(av1_get_qindex(&cm->seg, i, xd->current_qindex),
1564 cm->y_dc_delta_q, cm->bit_depth);
1565 xd->plane[0].seg_dequant[i][1] = av1_ac_quant(
1566 av1_get_qindex(&cm->seg, i, xd->current_qindex), 0, cm->bit_depth);
1567 xd->plane[1].seg_dequant[i][0] =
1568 av1_dc_quant(av1_get_qindex(&cm->seg, i, xd->current_qindex),
1569 cm->uv_dc_delta_q, cm->bit_depth);
1570 xd->plane[1].seg_dequant[i][1] =
1571 av1_ac_quant(av1_get_qindex(&cm->seg, i, xd->current_qindex),
1572 cm->uv_ac_delta_q, cm->bit_depth);
1573 xd->plane[2].seg_dequant[i][0] =
1574 av1_dc_quant(av1_get_qindex(&cm->seg, i, xd->current_qindex),
1575 cm->uv_dc_delta_q, cm->bit_depth);
1576 xd->plane[2].seg_dequant[i][1] =
1577 av1_ac_quant(av1_get_qindex(&cm->seg, i, xd->current_qindex),
1578 cm->uv_ac_delta_q, cm->bit_depth);
1579#else
Arild Fuldseth07441162016-08-15 15:07:52 +02001580 xd->plane[0].seg_dequant[i][0] =
1581 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
1582 xd->plane[0].seg_dequant[i][1] =
1583 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
1584 xd->plane[1].seg_dequant[i][0] =
1585 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1586 xd->plane[1].seg_dequant[i][1] =
1587 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1588 xd->plane[2].seg_dequant[i][0] =
1589 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1590 xd->plane[2].seg_dequant[i][1] =
1591 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
Fangwen Fu6160df22017-04-24 09:45:51 -07001592#endif
Arild Fuldseth07441162016-08-15 15:07:52 +02001593 }
1594 }
1595#endif
1596
Jingning Han41bb3392016-12-14 10:46:48 -08001597#if CONFIG_CB4X4
1598 if (mbmi->skip) reset_skip_context(xd, bsize);
1599#else
Jingning Hand39cc722016-12-02 14:03:26 -08001600 if (mbmi->skip) reset_skip_context(xd, AOMMAX(BLOCK_8X8, bsize));
Jingning Han41bb3392016-12-14 10:46:48 -08001601#endif
Jingning Hand39cc722016-12-02 14:03:26 -08001602
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001603#if CONFIG_COEF_INTERLEAVE
1604 {
1605 const struct macroblockd_plane *const pd_y = &xd->plane[0];
1606 const struct macroblockd_plane *const pd_c = &xd->plane[1];
1607 const TX_SIZE tx_log2_y = mbmi->tx_size;
1608 const TX_SIZE tx_log2_c = get_uv_tx_size(mbmi, pd_c);
1609 const int tx_sz_y = (1 << tx_log2_y);
1610 const int tx_sz_c = (1 << tx_log2_c);
1611 const int num_4x4_w_y = pd_y->n4_w;
1612 const int num_4x4_h_y = pd_y->n4_h;
1613 const int num_4x4_w_c = pd_c->n4_w;
1614 const int num_4x4_h_c = pd_c->n4_h;
1615 const int max_4x4_w_y = get_max_4x4_size(num_4x4_w_y, xd->mb_to_right_edge,
1616 pd_y->subsampling_x);
1617 const int max_4x4_h_y = get_max_4x4_size(num_4x4_h_y, xd->mb_to_bottom_edge,
1618 pd_y->subsampling_y);
1619 const int max_4x4_w_c = get_max_4x4_size(num_4x4_w_c, xd->mb_to_right_edge,
1620 pd_c->subsampling_x);
1621 const int max_4x4_h_c = get_max_4x4_size(num_4x4_h_c, xd->mb_to_bottom_edge,
1622 pd_c->subsampling_y);
1623
1624 // The max_4x4_w/h may be smaller than tx_sz under some corner cases,
1625 // i.e. when the SB is splitted by tile boundaries.
1626 const int tu_num_w_y = (max_4x4_w_y + tx_sz_y - 1) / tx_sz_y;
1627 const int tu_num_h_y = (max_4x4_h_y + tx_sz_y - 1) / tx_sz_y;
1628 const int tu_num_w_c = (max_4x4_w_c + tx_sz_c - 1) / tx_sz_c;
1629 const int tu_num_h_c = (max_4x4_h_c + tx_sz_c - 1) / tx_sz_c;
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001630 const int tu_num_c = tu_num_w_c * tu_num_h_c;
1631
1632 if (!is_inter_block(mbmi)) {
1633 int tu_idx_c = 0;
1634 int row_y, col_y, row_c, col_c;
1635 int plane;
1636
1637#if CONFIG_PALETTE
1638 for (plane = 0; plane <= 1; ++plane) {
1639 if (mbmi->palette_mode_info.palette_size[plane])
1640 av1_decode_palette_tokens(xd, plane, r);
1641 }
1642#endif
1643
1644 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1645 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1646 // luma
1647 predict_and_reconstruct_intra_block(
1648 cm, xd, r, mbmi, 0, row_y * tx_sz_y, col_y * tx_sz_y, tx_log2_y);
1649 // chroma
1650 if (tu_idx_c < tu_num_c) {
1651 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1652 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1653 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c,
1654 col_c, tx_log2_c);
1655 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c,
1656 col_c, tx_log2_c);
1657 tu_idx_c++;
1658 }
1659 }
1660 }
1661
1662 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1663 while (tu_idx_c < tu_num_c) {
1664 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1665 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1666 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c, col_c,
1667 tx_log2_c);
1668 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c, col_c,
1669 tx_log2_c);
1670 tu_idx_c++;
1671 }
1672 } else {
1673 // Prediction
David Barkerac37fa32016-12-02 12:30:21 +00001674 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001675 AOMMAX(bsize, BLOCK_8X8));
1676
1677 // Reconstruction
1678 if (!mbmi->skip) {
1679 int eobtotal = 0;
1680 int tu_idx_c = 0;
1681 int row_y, col_y, row_c, col_c;
1682
1683 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1684 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1685 // luma
1686 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 0,
1687 row_y * tx_sz_y,
1688 col_y * tx_sz_y, tx_log2_y);
1689 // chroma
1690 if (tu_idx_c < tu_num_c) {
1691 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1692 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1693 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1694 1, row_c, col_c, tx_log2_c);
1695 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1696 2, row_c, col_c, tx_log2_c);
1697 tu_idx_c++;
1698 }
1699 }
1700 }
1701
1702 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1703 while (tu_idx_c < tu_num_c) {
1704 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1705 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1706 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 1,
1707 row_c, col_c, tx_log2_c);
1708 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 2,
1709 row_c, col_c, tx_log2_c);
1710 tu_idx_c++;
1711 }
1712
Alex Converse64d7ef62017-03-22 18:09:16 -07001713 // TODO(CONFIG_COEF_INTERLEAVE owners): bring eob == 0 corner case
1714 // into line with the defaut configuration
1715 if (bsize >= BLOCK_8X8 && eobtotal == 0) mbmi->skip = 1;
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001716 }
1717 }
1718 }
Angie Chiang133733c2017-03-17 12:50:20 -07001719#else // CONFIG_COEF_INTERLEAVE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001720 if (!is_inter_block(mbmi)) {
1721 int plane;
Fangwen Fub3be9262017-03-06 15:34:28 -08001722#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001723 for (plane = 0; plane <= 1; ++plane) {
1724 if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xuf883b422016-08-30 14:01:10 -07001725 av1_decode_palette_tokens(xd, plane, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001726 }
Fangwen Fub3be9262017-03-06 15:34:28 -08001727#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001728 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1729 const struct macroblockd_plane *const pd = &xd->plane[plane];
Angie Chiang7fcfee42017-02-24 15:51:03 -08001730 const TX_SIZE tx_size = get_tx_size(plane, xd);
Jingning Han2d64f122016-10-21 12:44:29 -07001731 const int stepr = tx_size_high_unit[tx_size];
1732 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han41bb3392016-12-14 10:46:48 -08001733#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001734#if CONFIG_CHROMA_2X2
1735 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1736#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001737 const BLOCK_SIZE plane_bsize =
1738 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001739#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001740#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001741 const BLOCK_SIZE plane_bsize =
1742 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001743#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001744 int row, col;
Jingning Hanbafee8d2016-12-02 10:25:03 -08001745 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1746 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001747#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001748 if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
1749 pd->subsampling_y))
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001750 continue;
1751#endif
1752
Yaowu Xuc27fc142016-08-22 16:08:15 -07001753 for (row = 0; row < max_blocks_high; row += stepr)
1754 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001755 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane, row, col,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001756 tx_size);
1757 }
1758 } else {
Yue Chen9ab6d712017-01-12 15:50:46 -08001759 int ref;
1760
1761 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
1762 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
Alex Converse28744302017-04-13 14:46:22 -07001763 if (frame < LAST_FRAME) {
1764#if CONFIG_INTRABC
1765 assert(is_intrabc_block(mbmi));
1766 assert(frame == INTRA_FRAME);
1767 assert(ref == 0);
1768#else
1769 assert(0);
1770#endif // CONFIG_INTRABC
1771 } else {
1772 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
Yue Chen9ab6d712017-01-12 15:50:46 -08001773
Alex Converse28744302017-04-13 14:46:22 -07001774 xd->block_refs[ref] = ref_buf;
1775 if ((!av1_is_valid_scale(&ref_buf->sf)))
1776 aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
1777 "Reference frame has invalid dimensions");
1778 av1_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
1779 &ref_buf->sf);
1780 }
Yue Chen9ab6d712017-01-12 15:50:46 -08001781 }
Yue Chen69f18e12016-09-08 14:48:15 -07001782
Jingning Han41bb3392016-12-14 10:46:48 -08001783#if CONFIG_CB4X4
Sarah Parker4c10a3c2017-04-10 19:37:59 -07001784 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL, bsize);
Jingning Han41bb3392016-12-14 10:46:48 -08001785#else
1786 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
1787 AOMMAX(bsize, BLOCK_8X8));
1788#endif
Sarah Parker4c10a3c2017-04-10 19:37:59 -07001789
Yue Chencb60b182016-10-13 15:18:22 -07001790#if CONFIG_MOTION_VAR
1791 if (mbmi->motion_mode == OBMC_CAUSAL) {
Yue Chenf27b1602017-01-13 11:11:43 -08001792#if CONFIG_NCOBMC
1793 av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
1794#else
Yue Chen894fcce2016-10-21 16:50:52 -07001795 av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yue Chenf27b1602017-01-13 11:11:43 -08001796#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001797 }
Yue Chencb60b182016-10-13 15:18:22 -07001798#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001799
1800 // Reconstruction
1801 if (!mbmi->skip) {
1802 int eobtotal = 0;
1803 int plane;
1804
1805 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1806 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han41bb3392016-12-14 10:46:48 -08001807#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001808#if CONFIG_CHROMA_2X2
1809 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1810#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001811 const BLOCK_SIZE plane_bsize =
1812 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001813#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001814#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001815 const BLOCK_SIZE plane_bsize =
1816 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001817#endif
Jingning Hanbafee8d2016-12-02 10:25:03 -08001818 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1819 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001820 int row, col;
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001821
1822#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001823 if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
1824 pd->subsampling_y))
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001825 continue;
1826#endif
1827
Yaowu Xuc27fc142016-08-22 16:08:15 -07001828#if CONFIG_VAR_TX
Sarah Parker106b3cb2017-04-21 12:13:37 -07001829 const TX_SIZE max_tx_size = get_vartx_max_txsize(mbmi, plane_bsize);
Jingning Hanf64062f2016-11-02 16:22:18 -07001830 const int bh_var_tx = tx_size_high_unit[max_tx_size];
1831 const int bw_var_tx = tx_size_wide_unit[max_tx_size];
Jingning Hanbafee8d2016-12-02 10:25:03 -08001832 for (row = 0; row < max_blocks_high; row += bh_var_tx)
1833 for (col = 0; col < max_blocks_wide; col += bw_var_tx)
Jingning Hanfe45b212016-11-22 10:30:23 -08001834 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, row, col,
1835 max_tx_size, &eobtotal);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001836#else
Angie Chiang7fcfee42017-02-24 15:51:03 -08001837 const TX_SIZE tx_size = get_tx_size(plane, xd);
Jingning Han2d64f122016-10-21 12:44:29 -07001838 const int stepr = tx_size_high_unit[tx_size];
1839 const int stepc = tx_size_wide_unit[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001840 for (row = 0; row < max_blocks_high; row += stepr)
1841 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001842 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1843 plane, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001844#endif
1845 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001846 }
1847 }
Angie Chiang133733c2017-03-17 12:50:20 -07001848#endif // CONFIG_COEF_INTERLEAVE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001849
Angie Chiangd0916d92017-03-10 17:54:18 -08001850 int reader_corrupted_flag = aom_reader_has_error(r);
1851 aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001852}
1853
Yue Chen9ab6d712017-01-12 15:50:46 -08001854#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
1855static void detoken_and_recon_sb(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1856 int mi_row, int mi_col, aom_reader *r,
1857 BLOCK_SIZE bsize) {
1858 AV1_COMMON *const cm = &pbi->common;
1859 const int hbs = mi_size_wide[bsize] >> 1;
1860#if CONFIG_CB4X4
1861 const int unify_bsize = 1;
1862#else
1863 const int unify_bsize = 0;
1864#endif
1865#if CONFIG_EXT_PARTITION_TYPES
1866 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1867#endif
1868 PARTITION_TYPE partition;
1869 BLOCK_SIZE subsize;
1870 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1871 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1872
1873 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1874
1875 partition = get_partition(cm, mi_row, mi_col, bsize);
1876 subsize = subsize_lookup[partition][bsize];
1877
1878 if (!hbs && !unify_bsize) {
1879 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1880 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1881 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1882 } else {
1883 switch (partition) {
1884 case PARTITION_NONE:
1885 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
1886 break;
1887 case PARTITION_HORZ:
1888 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1889 if (has_rows)
1890 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r,
1891 subsize);
1892 break;
1893 case PARTITION_VERT:
1894 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1895 if (has_cols)
1896 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r,
1897 subsize);
1898 break;
1899 case PARTITION_SPLIT:
1900 detoken_and_recon_sb(pbi, xd, mi_row, mi_col, r, subsize);
1901 detoken_and_recon_sb(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1902 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1903 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col + hbs, r, subsize);
1904 break;
1905#if CONFIG_EXT_PARTITION_TYPES
1906 case PARTITION_HORZ_A:
1907 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1908 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1909 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1910 break;
1911 case PARTITION_HORZ_B:
1912 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1913 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1914 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1915 bsize2);
1916 break;
1917 case PARTITION_VERT_A:
1918 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1919 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1920 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1921 break;
1922 case PARTITION_VERT_B:
1923 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1924 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1925 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1926 bsize2);
1927 break;
1928#endif
1929 default: assert(0 && "Invalid partition type");
1930 }
1931 }
1932}
1933#endif
1934
Yue Chen64550b62017-01-12 12:18:22 -08001935static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1936#if CONFIG_SUPERTX
1937 int supertx_enabled,
1938#endif // CONFIG_SUPERTX
1939 int mi_row, int mi_col, aom_reader *r,
1940#if CONFIG_EXT_PARTITION_TYPES
1941 PARTITION_TYPE partition,
1942#endif // CONFIG_EXT_PARTITION_TYPES
1943 BLOCK_SIZE bsize) {
1944 decode_mbmi_block(pbi, xd,
1945#if CONFIG_SUPERTX
1946 supertx_enabled,
1947#endif
1948 mi_row, mi_col, r,
1949#if CONFIG_EXT_PARTITION_TYPES
1950 partition,
1951#endif
1952 bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001953#if !(CONFIG_MOTION_VAR && CONFIG_NCOBMC)
Yue Chen64550b62017-01-12 12:18:22 -08001954#if CONFIG_SUPERTX
1955 if (!supertx_enabled)
1956#endif // CONFIG_SUPERTX
1957 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001958#endif
Yue Chen64550b62017-01-12 12:18:22 -08001959}
1960
Yaowu Xuf883b422016-08-30 14:01:10 -07001961static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
1962 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001963 int has_rows, int has_cols,
Jingning Han1beb0102016-12-07 11:08:30 -08001964 BLOCK_SIZE bsize) {
Alex Converse55c6bde2017-01-12 15:55:31 -08001965#if CONFIG_UNPOISON_PARTITION_CTX
1966 const int ctx =
1967 partition_plane_context(xd, mi_row, mi_col, has_rows, has_cols, bsize);
Alex Converse2b9d19d2017-04-03 11:11:17 -07001968 const aom_prob *const probs =
1969 ctx < PARTITION_CONTEXTS ? cm->fc->partition_prob[ctx] : NULL;
1970 FRAME_COUNTS *const counts = ctx < PARTITION_CONTEXTS ? xd->counts : NULL;
Alex Converse55c6bde2017-01-12 15:55:31 -08001971#else
Jingning Han1beb0102016-12-07 11:08:30 -08001972 const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
Yaowu Xuf883b422016-08-30 14:01:10 -07001973 const aom_prob *const probs = cm->fc->partition_prob[ctx];
Alex Converse55c6bde2017-01-12 15:55:31 -08001974 FRAME_COUNTS *const counts = xd->counts;
1975#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001976 PARTITION_TYPE p;
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001977#if CONFIG_EC_ADAPT
1978 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1979 (void)cm;
1980#elif CONFIG_EC_MULTISYMBOL
1981 FRAME_CONTEXT *ec_ctx = cm->fc;
1982#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001983
Jingning Han5fe79db2017-03-27 15:10:30 -07001984#if CONFIG_EC_MULTISYMBOL
1985 aom_cdf_prob *partition_cdf = (ctx >= 0) ? ec_ctx->partition_cdf[ctx] : NULL;
1986#endif
1987
Yaowu Xuc27fc142016-08-22 16:08:15 -07001988 if (has_rows && has_cols)
1989#if CONFIG_EXT_PARTITION_TYPES
1990 if (bsize <= BLOCK_8X8)
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001991#if CONFIG_EC_MULTISYMBOL
Jingning Han5fe79db2017-03-27 15:10:30 -07001992 p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, PARTITION_TYPES,
1993 ACCT_STR);
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001994#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001995 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001996#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001997 else
Alex Converse57795a42017-03-14 12:18:25 -07001998#if CONFIG_EC_MULTISYMBOL
Jingning Han5fe79db2017-03-27 15:10:30 -07001999 p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, EXT_PARTITION_TYPES,
2000 ACCT_STR);
Alex Converse57795a42017-03-14 12:18:25 -07002001#else
Michael Bebenita6048d052016-08-25 14:40:54 -07002002 p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs,
2003 ACCT_STR);
Alex Converse57795a42017-03-14 12:18:25 -07002004#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002005#else
Nathan E. Egge9d9eb6c2016-11-16 09:44:26 -05002006#if CONFIG_EC_MULTISYMBOL
Jingning Han5fe79db2017-03-27 15:10:30 -07002007 p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, PARTITION_TYPES,
2008 ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04002009#else
Michael Bebenita6048d052016-08-25 14:40:54 -07002010 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04002011#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002012#endif // CONFIG_EXT_PARTITION_TYPES
2013 else if (!has_rows && has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07002014 p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002015 else if (has_rows && !has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07002016 p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002017 else
2018 p = PARTITION_SPLIT;
2019
2020 if (counts) ++counts->partition[ctx][p];
2021
2022 return p;
2023}
2024
2025#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002026static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
2027 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002028 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
2029 return 1;
2030 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002031 const int ctx = av1_get_skip_context(xd);
Michael Bebenita6048d052016-08-25 14:40:54 -07002032 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002033 FRAME_COUNTS *counts = xd->counts;
2034 if (counts) ++counts->skip[ctx][skip];
2035 return skip;
2036 }
2037}
2038#endif // CONFIG_SUPERTX
2039
2040// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -07002041static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002042#if CONFIG_SUPERTX
2043 int supertx_enabled,
2044#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002045 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002046 BLOCK_SIZE bsize, int n4x4_l2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002047 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002048 const int n8x8_l2 = n4x4_l2 - 1;
Jingning Hanff17e162016-12-07 17:58:18 -08002049 const int num_8x8_wh = mi_size_wide[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002050 const int hbs = num_8x8_wh >> 1;
Jingning Han41bb3392016-12-14 10:46:48 -08002051#if CONFIG_CB4X4
2052 const int unify_bsize = 1;
2053#else
2054 const int unify_bsize = 0;
2055#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002056 PARTITION_TYPE partition;
2057 BLOCK_SIZE subsize;
2058#if CONFIG_EXT_PARTITION_TYPES
2059 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
2060#endif
2061 const int has_rows = (mi_row + hbs) < cm->mi_rows;
2062 const int has_cols = (mi_col + hbs) < cm->mi_cols;
2063#if CONFIG_SUPERTX
2064 const int read_token = !supertx_enabled;
2065 int skip = 0;
Jingning Han2511c662016-12-22 11:57:34 -08002066 TX_SIZE supertx_size = max_txsize_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002067 const TileInfo *const tile = &xd->tile;
2068 int txfm = DCT_DCT;
2069#endif // CONFIG_SUPERTX
2070
2071 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
2072
Jingning Hancd959762017-03-27 14:49:59 -07002073 partition = (bsize < BLOCK_8X8) ? PARTITION_NONE
2074 : read_partition(cm, xd, mi_row, mi_col, r,
2075 has_rows, has_cols, bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002076 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
Yushin Cho77bba8d2016-11-04 16:36:56 -07002077
2078#if CONFIG_PVQ
2079 assert(partition < PARTITION_TYPES);
2080 assert(subsize < BLOCK_SIZES);
2081#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002082#if CONFIG_SUPERTX
2083 if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
2084 bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
2085 const int supertx_context = partition_supertx_context_lookup[partition];
Michael Bebenita6048d052016-08-25 14:40:54 -07002086 supertx_enabled = aom_read(
2087 r, cm->fc->supertx_prob[supertx_context][supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002088 if (xd->counts)
2089 xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
2090#if CONFIG_VAR_TX
2091 if (supertx_enabled) xd->supertx_size = supertx_size;
2092#endif
2093 }
2094#endif // CONFIG_SUPERTX
Jingning Han41bb3392016-12-14 10:46:48 -08002095 if (!hbs && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002096 // calculate bmode block dimensions (log 2)
2097 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
2098 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
2099 decode_block(pbi, xd,
2100#if CONFIG_SUPERTX
2101 supertx_enabled,
2102#endif // CONFIG_SUPERTX
2103 mi_row, mi_col, r,
2104#if CONFIG_EXT_PARTITION_TYPES
2105 partition,
2106#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002107 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002108 } else {
2109 switch (partition) {
2110 case PARTITION_NONE:
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 break;
2121 case PARTITION_HORZ:
2122 decode_block(pbi, xd,
2123#if CONFIG_SUPERTX
2124 supertx_enabled,
2125#endif // CONFIG_SUPERTX
2126 mi_row, mi_col, r,
2127#if CONFIG_EXT_PARTITION_TYPES
2128 partition,
2129#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002130 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002131 if (has_rows)
2132 decode_block(pbi, xd,
2133#if CONFIG_SUPERTX
2134 supertx_enabled,
2135#endif // CONFIG_SUPERTX
2136 mi_row + hbs, mi_col, r,
2137#if CONFIG_EXT_PARTITION_TYPES
2138 partition,
2139#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002140 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002141 break;
2142 case PARTITION_VERT:
2143 decode_block(pbi, xd,
2144#if CONFIG_SUPERTX
2145 supertx_enabled,
2146#endif // CONFIG_SUPERTX
2147 mi_row, mi_col, r,
2148#if CONFIG_EXT_PARTITION_TYPES
2149 partition,
2150#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002151 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002152 if (has_cols)
2153 decode_block(pbi, xd,
2154#if CONFIG_SUPERTX
2155 supertx_enabled,
2156#endif // CONFIG_SUPERTX
2157 mi_row, mi_col + hbs, r,
2158#if CONFIG_EXT_PARTITION_TYPES
2159 partition,
2160#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002161 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002162 break;
2163 case PARTITION_SPLIT:
2164 decode_partition(pbi, xd,
2165#if CONFIG_SUPERTX
2166 supertx_enabled,
2167#endif // CONFIG_SUPERTX
2168 mi_row, mi_col, r, subsize, n8x8_l2);
2169 decode_partition(pbi, xd,
2170#if CONFIG_SUPERTX
2171 supertx_enabled,
2172#endif // CONFIG_SUPERTX
2173 mi_row, mi_col + hbs, r, subsize, n8x8_l2);
2174 decode_partition(pbi, xd,
2175#if CONFIG_SUPERTX
2176 supertx_enabled,
2177#endif // CONFIG_SUPERTX
2178 mi_row + hbs, mi_col, r, subsize, n8x8_l2);
2179 decode_partition(pbi, xd,
2180#if CONFIG_SUPERTX
2181 supertx_enabled,
2182#endif // CONFIG_SUPERTX
2183 mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
2184 break;
2185#if CONFIG_EXT_PARTITION_TYPES
2186 case PARTITION_HORZ_A:
2187 decode_block(pbi, xd,
2188#if CONFIG_SUPERTX
2189 supertx_enabled,
2190#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002191 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002192 decode_block(pbi, xd,
2193#if CONFIG_SUPERTX
2194 supertx_enabled,
2195#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002196 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002197 decode_block(pbi, xd,
2198#if CONFIG_SUPERTX
2199 supertx_enabled,
2200#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002201 mi_row + hbs, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002202 break;
2203 case PARTITION_HORZ_B:
2204 decode_block(pbi, xd,
2205#if CONFIG_SUPERTX
2206 supertx_enabled,
2207#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002208 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002209 decode_block(pbi, xd,
2210#if CONFIG_SUPERTX
2211 supertx_enabled,
2212#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002213 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002214 decode_block(pbi, xd,
2215#if CONFIG_SUPERTX
2216 supertx_enabled,
2217#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002218 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002219 break;
2220 case PARTITION_VERT_A:
2221 decode_block(pbi, xd,
2222#if CONFIG_SUPERTX
2223 supertx_enabled,
2224#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002225 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002226 decode_block(pbi, xd,
2227#if CONFIG_SUPERTX
2228 supertx_enabled,
2229#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002230 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002231 decode_block(pbi, xd,
2232#if CONFIG_SUPERTX
2233 supertx_enabled,
2234#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002235 mi_row, mi_col + hbs, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002236 break;
2237 case PARTITION_VERT_B:
2238 decode_block(pbi, xd,
2239#if CONFIG_SUPERTX
2240 supertx_enabled,
2241#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002242 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002243 decode_block(pbi, xd,
2244#if CONFIG_SUPERTX
2245 supertx_enabled,
2246#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002247 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002248 decode_block(pbi, xd,
2249#if CONFIG_SUPERTX
2250 supertx_enabled,
2251#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002252 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002253 break;
2254#endif
2255 default: assert(0 && "Invalid partition type");
2256 }
2257 }
2258
2259#if CONFIG_SUPERTX
2260 if (supertx_enabled && read_token) {
2261 uint8_t *dst_buf[3];
2262 int dst_stride[3], i;
2263 int offset = mi_row * cm->mi_stride + mi_col;
2264
2265 set_segment_id_supertx(cm, mi_row, mi_col, bsize);
2266
David Barker3aec8d62017-01-31 14:55:32 +00002267#if CONFIG_DELTA_Q
2268 if (cm->delta_q_present_flag) {
2269 for (i = 0; i < MAX_SEGMENTS; i++) {
2270 xd->plane[0].seg_dequant[i][0] =
2271 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
2272 xd->plane[0].seg_dequant[i][1] =
2273 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
2274 xd->plane[1].seg_dequant[i][0] =
2275 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2276 xd->plane[1].seg_dequant[i][1] =
2277 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2278 xd->plane[2].seg_dequant[i][0] =
2279 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2280 xd->plane[2].seg_dequant[i][1] =
2281 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2282 }
2283 }
2284#endif
2285
Yaowu Xuc27fc142016-08-22 16:08:15 -07002286 xd->mi = cm->mi_grid_visible + offset;
2287 xd->mi[0] = cm->mi + offset;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002288#if CONFIG_DEPENDENT_HORZTILES
2289 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2290 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols,
2291 cm->dependent_horz_tiles);
2292#else
Jingning Han5b7706a2016-12-21 09:55:10 -08002293 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2294 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002295#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002296 set_skip_context(xd, mi_row, mi_col);
2297 skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
2298 if (skip) {
2299 reset_skip_context(xd, bsize);
2300 } else {
2301#if CONFIG_EXT_TX
Sarah Parkere68a3e42017-02-16 14:03:24 -08002302 if (get_ext_tx_types(supertx_size, bsize, 1, cm->reduced_tx_set_used) >
2303 1) {
2304 const int eset =
2305 get_ext_tx_set(supertx_size, bsize, 1, cm->reduced_tx_set_used);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002306 if (eset > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002307 txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
Michael Bebenita6048d052016-08-25 14:40:54 -07002308 cm->fc->inter_ext_tx_prob[eset][supertx_size],
2309 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002310 if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
2311 }
2312 }
2313#else
2314 if (supertx_size < TX_32X32) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002315 txfm = aom_read_tree(r, av1_ext_tx_tree,
Jingning Han8f6eb182016-10-19 13:48:57 -07002316 cm->fc->inter_ext_tx_prob[supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002317 if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
2318 }
2319#endif // CONFIG_EXT_TX
2320 }
2321
Yaowu Xuf883b422016-08-30 14:01:10 -07002322 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002323 for (i = 0; i < MAX_MB_PLANE; i++) {
2324 dst_buf[i] = xd->plane[i].dst.buf;
2325 dst_stride[i] = xd->plane[i].dst.stride;
2326 }
2327 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
2328 bsize, dst_buf, dst_stride);
2329
2330 if (!skip) {
2331 int eobtotal = 0;
2332 MB_MODE_INFO *mbmi;
2333 set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
2334 mbmi = &xd->mi[0]->mbmi;
2335 mbmi->tx_type = txfm;
2336 assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
2337 for (i = 0; i < MAX_MB_PLANE; ++i) {
2338 const struct macroblockd_plane *const pd = &xd->plane[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002339 int row, col;
Angie Chiang7fcfee42017-02-24 15:51:03 -08002340 const TX_SIZE tx_size = get_tx_size(i, xd);
Jingning Han5b7706a2016-12-21 09:55:10 -08002341 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Jingning Han32b20282016-10-28 15:42:44 -07002342 const int stepr = tx_size_high_unit[tx_size];
2343 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han5b7706a2016-12-21 09:55:10 -08002344 const int max_blocks_wide = max_block_wide(xd, plane_bsize, i);
2345 const int max_blocks_high = max_block_high(xd, plane_bsize, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002346
2347 for (row = 0; row < max_blocks_high; row += stepr)
2348 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07002349 eobtotal += reconstruct_inter_block(
2350 cm, xd, r, mbmi->segment_id_supertx, i, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002351 }
Jingning Han24f24a52016-12-27 10:13:28 -08002352 if ((unify_bsize || !(subsize < BLOCK_8X8)) && eobtotal == 0) skip = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002353 }
2354 set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
2355 }
2356#endif // CONFIG_SUPERTX
2357
2358#if CONFIG_EXT_PARTITION_TYPES
Alex Converseffabff32017-03-27 09:52:19 -07002359 update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002360#else
2361 // update partition context
2362 if (bsize >= BLOCK_8X8 &&
2363 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
Jingning Han1beb0102016-12-07 11:08:30 -08002364 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
David Barkerf8935c92016-10-26 14:54:06 +01002365#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xud71be782016-10-14 08:47:03 -07002366
Jean-Marc Valin01435132017-02-18 14:12:53 -05002367#if CONFIG_CDEF
David Barker9739f362016-11-10 09:29:32 +00002368#if CONFIG_EXT_PARTITION
2369 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128) {
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002370 if (!sb_all_skip(cm, mi_row, mi_col)) {
Jean-Marc Valin1df427e2017-03-23 18:32:57 -04002371 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2372 aom_read_literal(r, cm->cdef_bits, ACCT_STR);
David Barker9739f362016-11-10 09:29:32 +00002373 } else {
Jean-Marc Valin1df427e2017-03-23 18:32:57 -04002374 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2375 0;
David Barker9739f362016-11-10 09:29:32 +00002376 }
2377 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64) {
2378#else
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002379 if (bsize == BLOCK_64X64) {
David Barker9739f362016-11-10 09:29:32 +00002380#endif
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002381 if (!sb_all_skip(cm, mi_row, mi_col)) {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002382 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2383 aom_read_literal(r, cm->cdef_bits, ACCT_STR);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002384 } else {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002385 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
Jean-Marc Valina8764952017-04-11 04:01:15 -04002386 -1;
Yaowu Xud71be782016-10-14 08:47:03 -07002387 }
2388 }
Jean-Marc Valin01435132017-02-18 14:12:53 -05002389#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002390}
2391
Yaowu Xuc27fc142016-08-22 16:08:15 -07002392static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
2393 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002394 struct aom_internal_error_info *error_info,
Alex Converseeb780e72016-12-13 12:46:41 -08002395 aom_reader *r,
2396#if CONFIG_ANS && ANS_MAX_SYMBOLS
2397 int window_size,
2398#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
2399 aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002400 // Validate the calculated partition length. If the buffer
2401 // described by the partition can't be fully read, then restrict
2402 // it to the portion that can be (for EC mode) or throw an error.
2403 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002404 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002405 "Truncated packet or corrupt tile length");
2406
Alex Converse2cdf0d82016-12-13 13:53:09 -08002407#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08002408 r->window_size = window_size;
Alex Converse2cdf0d82016-12-13 13:53:09 -08002409#endif
Alex Converse346440b2017-01-03 13:47:37 -08002410 if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07002411 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002412 "Failed to allocate bool decoder %d", 1);
2413}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002414
Angie Chiang800df032017-03-22 11:14:12 -07002415#if !CONFIG_PVQ && !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET) && !CONFIG_LV_MAP
Yaowu Xuf883b422016-08-30 14:01:10 -07002416static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
2417 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002418 int i, j, k, l, m;
Alex Conversea9598cd2017-02-03 14:18:05 -08002419#if CONFIG_EC_ADAPT
2420 const int node_limit = UNCONSTRAINED_NODES - 1;
2421#else
Thomas9ac55082016-09-23 18:04:17 +01002422 const int node_limit = UNCONSTRAINED_NODES;
Alex Conversea9598cd2017-02-03 14:18:05 -08002423#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002424
Michael Bebenita6048d052016-08-25 14:40:54 -07002425 if (aom_read_bit(r, ACCT_STR))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002426 for (i = 0; i < PLANE_TYPES; ++i)
2427 for (j = 0; j < REF_TYPES; ++j)
2428 for (k = 0; k < COEF_BANDS; ++k)
2429 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
Thomas9ac55082016-09-23 18:04:17 +01002430 for (m = 0; m < node_limit; ++m)
Michael Bebenita6048d052016-08-25 14:40:54 -07002431 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002432}
2433
Yaowu Xuf883b422016-08-30 14:01:10 -07002434static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002435 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
2436 TX_SIZE tx_size;
Jingning Han83630632016-12-16 11:27:25 -08002437 for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002438 read_coef_probs_common(fc->coef_probs[tx_size], r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002439}
Yushin Cho77bba8d2016-11-04 16:36:56 -07002440#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002441
Yaowu Xuf883b422016-08-30 14:01:10 -07002442static void setup_segmentation(AV1_COMMON *const cm,
2443 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002444 struct segmentation *const seg = &cm->seg;
2445 int i, j;
2446
2447 seg->update_map = 0;
2448 seg->update_data = 0;
2449
Yaowu Xuf883b422016-08-30 14:01:10 -07002450 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002451 if (!seg->enabled) return;
2452
2453 // Segmentation map update
2454 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2455 seg->update_map = 1;
2456 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002457 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002458 }
2459 if (seg->update_map) {
2460 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2461 seg->temporal_update = 0;
2462 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002463 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002464 }
2465 }
2466
2467 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07002468 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002469 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002470 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002471
Yaowu Xuf883b422016-08-30 14:01:10 -07002472 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002473
2474 for (i = 0; i < MAX_SEGMENTS; i++) {
2475 for (j = 0; j < SEG_LVL_MAX; j++) {
2476 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002477 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002478 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002479 av1_enable_segfeature(seg, i, j);
2480 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
2481 if (av1_is_segfeature_signed(j))
2482 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002483 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002484 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002485 }
2486 }
2487 }
2488}
2489
2490#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002491static void decode_restoration_mode(AV1_COMMON *cm,
2492 struct aom_read_bit_buffer *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002493 int p;
2494 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002495 if (aom_rb_read_bit(rb)) {
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002496 rsi->frame_restoration_type =
2497 aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002498 } else {
2499 rsi->frame_restoration_type =
2500 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
2501 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002502 for (p = 1; p < MAX_MB_PLANE; ++p) {
2503 cm->rst_info[p].frame_restoration_type =
2504 aom_rb_read_bit(rb) ? RESTORE_WIENER : RESTORE_NONE;
2505 }
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002506
2507 cm->rst_info[0].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2508 cm->rst_info[1].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2509 cm->rst_info[2].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2510 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
2511 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
2512 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
2513 rsi = &cm->rst_info[0];
2514 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2515 if (rsi->restoration_tilesize != RESTORATION_TILESIZE_MAX) {
2516 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2517 }
2518 cm->rst_info[1].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2519 cm->rst_info[2].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2520 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002521}
2522
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002523static void read_wiener_filter(WienerInfo *wiener_info, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002524 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002525 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2526 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002527 wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002528 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2529 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002530 wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002531 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2532 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002533 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002534 wiener_info->vfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002535 -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
2536 wiener_info->vfilter[2]);
2537
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002538 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002539 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2540 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002541 wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002542 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2543 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002544 wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002545 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2546 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002547 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002548 wiener_info->hfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002549 -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
2550 wiener_info->hfilter[2]);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002551}
2552
2553static void read_sgrproj_filter(SgrprojInfo *sgrproj_info, aom_reader *rb) {
2554 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
2555 sgrproj_info->xqd[0] =
2556 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN0;
2557 sgrproj_info->xqd[1] =
2558 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN1;
2559}
2560
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002561static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002562 int i, p;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002563 const int ntiles = av1_get_rest_ntiles(cm->width, cm->height,
2564 cm->rst_info[0].restoration_tilesize,
2565 NULL, NULL, NULL, NULL);
2566 const int ntiles_uv = av1_get_rest_ntiles(
2567 ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
2568 ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y),
2569 cm->rst_info[1].restoration_tilesize, NULL, NULL, NULL, NULL);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002570 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002571 if (rsi->frame_restoration_type != RESTORE_NONE) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002572 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002573 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002574 rsi->restoration_type[i] =
2575 aom_read_tree(rb, av1_switchable_restore_tree,
2576 cm->fc->switchable_restore_prob, ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002577 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002578 read_wiener_filter(&rsi->wiener_info[i], rb);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002579 } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002580 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002581 }
2582 }
2583 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002584 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002585 if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002586 rsi->restoration_type[i] = RESTORE_WIENER;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002587 read_wiener_filter(&rsi->wiener_info[i], rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002588 } else {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002589 rsi->restoration_type[i] = RESTORE_NONE;
2590 }
2591 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002592 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002593 for (i = 0; i < ntiles; ++i) {
2594 if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
2595 rsi->restoration_type[i] = RESTORE_SGRPROJ;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002596 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
2597 } else {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002598 rsi->restoration_type[i] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002599 }
2600 }
2601 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002602 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002603 for (p = 1; p < MAX_MB_PLANE; ++p) {
2604 rsi = &cm->rst_info[p];
2605 if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08002606 for (i = 0; i < ntiles_uv; ++i) {
2607 if (ntiles_uv > 1)
2608 rsi->restoration_type[i] =
2609 aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR) ? RESTORE_WIENER
2610 : RESTORE_NONE;
2611 else
2612 rsi->restoration_type[i] = RESTORE_WIENER;
2613 if (rsi->restoration_type[i] == RESTORE_WIENER) {
2614 read_wiener_filter(&rsi->wiener_info[i], rb);
2615 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002616 }
2617 }
2618 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002619}
2620#endif // CONFIG_LOOP_RESTORATION
2621
Yaowu Xuf883b422016-08-30 14:01:10 -07002622static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002623 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07002624 lf->filter_level = aom_rb_read_literal(rb, 6);
2625 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002626
2627 // Read in loop filter deltas applied at the MB level based on mode or ref
2628 // frame.
2629 lf->mode_ref_delta_update = 0;
2630
Yaowu Xuf883b422016-08-30 14:01:10 -07002631 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002632 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002633 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002634 if (lf->mode_ref_delta_update) {
2635 int i;
2636
2637 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002638 if (aom_rb_read_bit(rb))
2639 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002640
2641 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002642 if (aom_rb_read_bit(rb))
2643 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002644 }
2645 }
2646}
2647
Jean-Marc Valin01435132017-02-18 14:12:53 -05002648#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002649static void setup_cdef(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002650 int i;
Steinar Midtskogen0c966a52017-04-18 14:38:13 +02002651 cm->cdef_dering_damping = aom_rb_read_literal(rb, 1) + 5;
2652 cm->cdef_clpf_damping = aom_rb_read_literal(rb, 2) + 3;
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002653 cm->cdef_bits = aom_rb_read_literal(rb, 2);
2654 cm->nb_cdef_strengths = 1 << cm->cdef_bits;
2655 for (i = 0; i < cm->nb_cdef_strengths; i++) {
2656 cm->cdef_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
Jean-Marc Valine9f77422017-03-22 17:09:51 -04002657 cm->cdef_uv_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002658 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002659}
Jean-Marc Valin01435132017-02-18 14:12:53 -05002660#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002661
Yaowu Xuf883b422016-08-30 14:01:10 -07002662static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2663 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002664}
2665
Yaowu Xuf883b422016-08-30 14:01:10 -07002666static void setup_quantization(AV1_COMMON *const cm,
2667 struct aom_read_bit_buffer *rb) {
2668 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002669 cm->y_dc_delta_q = read_delta_q(rb);
2670 cm->uv_dc_delta_q = read_delta_q(rb);
2671 cm->uv_ac_delta_q = read_delta_q(rb);
2672 cm->dequant_bit_depth = cm->bit_depth;
2673#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002674 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002675 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002676 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2677 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002678 } else {
2679 cm->min_qmlevel = 0;
2680 cm->max_qmlevel = 0;
2681 }
2682#endif
2683}
2684
Yaowu Xuf883b422016-08-30 14:01:10 -07002685static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002686 // Build y/uv dequant values based on segmentation.
2687 int i = 0;
2688#if CONFIG_AOM_QM
2689 int lossless;
2690 int j = 0;
2691 int qmlevel;
2692 int using_qm = cm->using_qmatrix;
2693 int minqm = cm->min_qmlevel;
2694 int maxqm = cm->max_qmlevel;
2695#endif
2696#if CONFIG_NEW_QUANT
2697 int b;
2698 int dq;
2699#endif // CONFIG_NEW_QUANT
2700 if (cm->seg.enabled) {
2701 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002702 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002703 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002704 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2705 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002706 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002707 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002708 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002709 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002710#if CONFIG_AOM_QM
2711 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2712 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2713 // NB: depends on base index so there is only 1 set per frame
2714 // No quant weighting when lossless or signalled not using QM
2715 qmlevel = (lossless || using_qm == 0)
2716 ? NUM_QM_LEVELS - 1
2717 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2718 for (j = 0; j < TX_SIZES; ++j) {
2719 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2720 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2721 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2722 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2723 }
2724#endif // CONFIG_AOM_QM
2725#if CONFIG_NEW_QUANT
2726 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2727 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002728 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002729 cm->y_dequant_nuq[i][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002730 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002731 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002732 }
2733 }
2734#endif // CONFIG_NEW_QUANT
2735 }
2736 } else {
2737 const int qindex = cm->base_qindex;
2738 // When segmentation is disabled, only the first value is used. The
2739 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002740 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2741 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002742 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002743 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002744 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002745 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002746#if CONFIG_AOM_QM
2747 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2748 cm->uv_ac_delta_q == 0;
2749 // No quant weighting when lossless or signalled not using QM
2750 qmlevel = (lossless || using_qm == 0)
2751 ? NUM_QM_LEVELS - 1
2752 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2753 for (j = 0; j < TX_SIZES; ++j) {
2754 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2755 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2756 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2757 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2758 }
2759#endif
2760#if CONFIG_NEW_QUANT
2761 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2762 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002763 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002764 cm->y_dequant_nuq[0][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002765 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002766 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002767 }
2768 }
2769#endif // CONFIG_NEW_QUANT
2770 }
2771}
2772
Angie Chiang5678ad92016-11-21 09:38:40 -08002773static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002774 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07002775 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002776}
2777
Yaowu Xuf883b422016-08-30 14:01:10 -07002778static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002779 cm->render_width = cm->width;
2780 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002781 if (aom_rb_read_bit(rb))
2782 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002783}
2784
Fergus Simpsone7508412017-03-14 18:14:09 -07002785#if CONFIG_LOOP_RESTORATION && CONFIG_FRAME_SUPERRES
2786// TODO(afergs): make "struct aom_read_bit_buffer *const rb"?
2787static void setup_superres_size(AV1_COMMON *const cm,
2788 struct aom_read_bit_buffer *rb, int *width,
2789 int *height) {
2790 // TODO(afergs): Test this behaviour
2791 // Frame superres is probably in compatible with this render resolution
2792 assert(cm->width == cm->render_width && cm->height == cm->render_height);
2793
2794 cm->superres_width = cm->width;
2795 cm->superres_height = cm->height;
2796 if (aom_rb_read_bit(rb)) {
2797 cm->superres_scale_numerator =
2798 (uint8_t)aom_rb_read_literal(rb, SUPERRES_SCALE_BITS);
2799 cm->superres_scale_numerator += SUPERRES_SCALE_NUMERATOR_MIN;
2800 // Don't edit cm->width or cm->height directly, or the buffers won't get
2801 // resized correctly
2802 // TODO(afergs): Should the render resolution not be modified? It's the same
2803 // by default (ie. when it isn't sent)...
2804 // resize_context_buffers() will change cm->width to equal cm->render_width,
2805 // then they'll be the same again
2806 *width = cm->render_width =
2807 cm->width * cm->superres_scale_numerator / SUPERRES_SCALE_DENOMINATOR;
2808 *height = cm->render_height =
2809 cm->height * cm->superres_scale_numerator / SUPERRES_SCALE_DENOMINATOR;
2810 } else {
2811 // 1:1 scaling - ie. no scaling, scale not provided
2812 cm->superres_scale_numerator = SUPERRES_SCALE_DENOMINATOR;
2813 }
2814}
2815#endif // CONFIG_LOOP_RESTORATION && CONFIG_FRAME_SUPERRES
2816
Yaowu Xuf883b422016-08-30 14:01:10 -07002817static void resize_mv_buffer(AV1_COMMON *cm) {
2818 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002819 cm->cur_frame->mi_rows = cm->mi_rows;
2820 cm->cur_frame->mi_cols = cm->mi_cols;
2821 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002822 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002823 sizeof(*cm->cur_frame->mvs)));
2824}
2825
Yaowu Xuf883b422016-08-30 14:01:10 -07002826static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002827#if CONFIG_SIZE_LIMIT
2828 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002829 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002830 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2831 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2832#endif
2833 if (cm->width != width || cm->height != height) {
2834 const int new_mi_rows =
2835 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2836 const int new_mi_cols =
2837 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2838
Yaowu Xuf883b422016-08-30 14:01:10 -07002839 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002840 // dimensions as well as the overall size.
2841 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002842 if (av1_alloc_context_buffers(cm, width, height))
2843 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002844 "Failed to allocate context buffers");
2845 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002846 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002847 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002848 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002849 cm->width = width;
2850 cm->height = height;
2851 }
2852 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2853 cm->mi_cols > cm->cur_frame->mi_cols) {
2854 resize_mv_buffer(cm);
2855 }
2856}
2857
Yaowu Xuf883b422016-08-30 14:01:10 -07002858static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002859 int width, height;
2860 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002861 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002862 setup_render_size(cm, rb);
Fergus Simpsone7508412017-03-14 18:14:09 -07002863#if CONFIG_LOOP_RESTORATION && CONFIG_FRAME_SUPERRES
2864 setup_superres_size(cm, rb, &width, &height);
2865#endif // CONFIG_LOOP_RESTORATION && CONFIG_FRAME_SUPERRES
2866 resize_context_buffers(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002867
2868 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002869 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002870 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2871 cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002872#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002873 cm->use_highbitdepth,
2874#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002875 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002876 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2877 pool->cb_priv)) {
2878 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002879 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002880 "Failed to allocate frame buffer");
2881 }
2882 unlock_buffer_pool(pool);
2883
2884 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2885 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2886 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2887 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2888 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2889 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2890 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2891}
2892
Yaowu Xuf883b422016-08-30 14:01:10 -07002893static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002894 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002895 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002896 int this_xss, int this_yss) {
2897 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2898 ref_yss == this_yss;
2899}
2900
Yaowu Xuf883b422016-08-30 14:01:10 -07002901static void setup_frame_size_with_refs(AV1_COMMON *cm,
2902 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002903 int width, height;
2904 int found = 0, i;
2905 int has_valid_ref_frame = 0;
2906 BufferPool *const pool = cm->buffer_pool;
2907 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002908 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002909 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2910 width = buf->y_crop_width;
2911 height = buf->y_crop_height;
2912 cm->render_width = buf->render_width;
2913 cm->render_height = buf->render_height;
2914 found = 1;
2915 break;
2916 }
2917 }
2918
2919 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002920 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002921 setup_render_size(cm, rb);
2922 }
2923
2924 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002925 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002926 "Invalid frame size");
2927
2928 // Check to make sure at least one of frames that this frame references
2929 // has valid dimensions.
2930 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2931 RefBuffer *const ref_frame = &cm->frame_refs[i];
2932 has_valid_ref_frame |=
2933 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2934 ref_frame->buf->y_crop_height, width, height);
2935 }
2936 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002937 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002938 "Referenced frame has invalid size");
2939 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2940 RefBuffer *const ref_frame = &cm->frame_refs[i];
2941 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
2942 ref_frame->buf->subsampling_x,
2943 ref_frame->buf->subsampling_y, cm->bit_depth,
2944 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07002945 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002946 "Referenced frame has incompatible color format");
2947 }
2948
2949 resize_context_buffers(cm, width, height);
2950
2951 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002952 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002953 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2954 cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002955#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002956 cm->use_highbitdepth,
2957#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002958 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002959 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2960 pool->cb_priv)) {
2961 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002962 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002963 "Failed to allocate frame buffer");
2964 }
2965 unlock_buffer_pool(pool);
2966
2967 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2968 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2969 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2970 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2971 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2972 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2973 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2974}
2975
Yaowu Xuf883b422016-08-30 14:01:10 -07002976static void read_tile_info(AV1Decoder *const pbi,
2977 struct aom_read_bit_buffer *const rb) {
2978 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002979#if CONFIG_EXT_TILE
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08002980 cm->tile_encoding_mode = aom_rb_read_literal(rb, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002981// Read the tile width/height
2982#if CONFIG_EXT_PARTITION
2983 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002984 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
2985 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002986 } else
2987#endif // CONFIG_EXT_PARTITION
2988 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002989 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
2990 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002991 }
2992
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002993#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002994 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08002995#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08002996
Yaowu Xuc27fc142016-08-22 16:08:15 -07002997 cm->tile_width <<= cm->mib_size_log2;
2998 cm->tile_height <<= cm->mib_size_log2;
2999
Yaowu Xuf883b422016-08-30 14:01:10 -07003000 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
3001 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003002
3003 // Get the number of tiles
3004 cm->tile_cols = 1;
3005 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
3006
3007 cm->tile_rows = 1;
3008 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
3009
3010 if (cm->tile_cols * cm->tile_rows > 1) {
3011 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07003012 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
3013 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003014 }
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003015
3016#if CONFIG_DEPENDENT_HORZTILES
3017 if (cm->tile_rows <= 1)
3018 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
3019 else
3020 cm->dependent_horz_tiles = 0;
3021#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003022#else
3023 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07003024 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003025
3026 // columns
3027 max_ones = max_log2_tile_cols - min_log2_tile_cols;
3028 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07003029 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003030
3031 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07003032 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003033 "Invalid number of tile columns");
3034
3035 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07003036 cm->log2_tile_rows = aom_rb_read_bit(rb);
3037 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003038#if CONFIG_DEPENDENT_HORZTILES
3039 if (cm->log2_tile_rows != 0)
3040 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
3041 else
3042 cm->dependent_horz_tiles = 0;
3043#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003044#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003045 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003046#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003047
Yaowu Xuc27fc142016-08-22 16:08:15 -07003048 cm->tile_cols = 1 << cm->log2_tile_cols;
3049 cm->tile_rows = 1 << cm->log2_tile_rows;
3050
3051 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
3052 cm->tile_width >>= cm->log2_tile_cols;
3053 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
3054 cm->tile_height >>= cm->log2_tile_rows;
3055
3056 // round to integer multiples of superblock size
3057 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
3058 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
3059
Thomas Davies4974e522016-11-07 17:44:05 +00003060// tile size magnitude
3061#if !CONFIG_TILE_GROUPS
3062 if (cm->tile_rows > 1 || cm->tile_cols > 1)
3063#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003064 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003065#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00003066
Thomas Davies80188d12016-10-26 16:08:35 -07003067#if CONFIG_TILE_GROUPS
3068 // Store an index to the location of the tile group information
3069 pbi->tg_size_bit_offset = rb->bit_offset;
3070 pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
3071 if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
3072 pbi->tg_start =
3073 aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3074 pbi->tg_size =
3075 1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3076 }
3077#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003078}
3079
Yaowu Xu4ff59b52017-04-24 12:41:56 -07003080static int mem_get_varsize(const uint8_t *src, int sz) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003081 switch (sz) {
3082 case 1: return src[0];
3083 case 2: return mem_get_le16(src);
3084 case 3: return mem_get_le24(src);
3085 case 4: return mem_get_le32(src);
3086 default: assert("Invalid size" && 0); return -1;
3087 }
3088}
3089
3090#if CONFIG_EXT_TILE
3091// Reads the next tile returning its size and adjusting '*data' accordingly
3092// based on 'is_last'.
3093static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07003094 struct aom_internal_error_info *error_info,
3095 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003096 void *decrypt_state,
3097 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08003098 int tile_size_bytes, int col, int row,
3099 unsigned int tile_encoding_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003100 size_t size;
3101
3102 size_t copy_size = 0;
3103 const uint8_t *copy_data = NULL;
3104
3105 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003106 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003107 "Truncated packet or corrupt tile length");
3108 if (decrypt_cb) {
3109 uint8_t be_data[4];
3110 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3111
3112 // Only read number of bytes in cm->tile_size_bytes.
3113 size = mem_get_varsize(be_data, tile_size_bytes);
3114 } else {
3115 size = mem_get_varsize(*data, tile_size_bytes);
3116 }
3117
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08003118 // If cm->tile_encoding_mode = 1 (i.e. TILE_VR), then the top bit of the tile
3119 // header indicates copy mode.
3120 if (tile_encoding_mode && (size >> (tile_size_bytes * 8 - 1)) == 1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003121 // The remaining bits in the top byte signal the row offset
3122 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
3123
3124 // Currently, only use tiles in same column as reference tiles.
3125 copy_data = tile_buffers[row - offset][col].data;
3126 copy_size = tile_buffers[row - offset][col].size;
3127 size = 0;
3128 }
3129
3130 *data += tile_size_bytes;
3131
3132 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003133 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003134 "Truncated packet or corrupt tile size");
3135
3136 if (size > 0) {
3137 tile_buffers[row][col].data = *data;
3138 tile_buffers[row][col].size = size;
3139 } else {
3140 tile_buffers[row][col].data = copy_data;
3141 tile_buffers[row][col].size = copy_size;
3142 }
3143
3144 *data += size;
3145
3146 tile_buffers[row][col].raw_data_end = *data;
3147}
3148
3149static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003150 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003151 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003152 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003153 const int tile_cols = cm->tile_cols;
3154 const int tile_rows = cm->tile_rows;
3155 const int have_tiles = tile_cols * tile_rows > 1;
3156
3157 if (!have_tiles) {
Jingning Han99ffce62017-04-25 15:48:41 -07003158 const size_t tile_size = data_end - data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003159 tile_buffers[0][0].data = data;
3160 tile_buffers[0][0].size = tile_size;
3161 tile_buffers[0][0].raw_data_end = NULL;
3162 } else {
3163 // We locate only the tile buffers that are required, which are the ones
3164 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
3165 // need the last (bottom right) tile buffer, as we need to know where the
3166 // end of the compressed frame buffer is for proper superframe decoding.
3167
3168 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
3169 const uint8_t *const data_start = data;
3170
Yaowu Xuf883b422016-08-30 14:01:10 -07003171 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003172 const int single_row = pbi->dec_tile_row >= 0;
3173 const int tile_rows_start = single_row ? dec_tile_row : 0;
3174 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003175 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003176 const int single_col = pbi->dec_tile_col >= 0;
3177 const int tile_cols_start = single_col ? dec_tile_col : 0;
3178 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3179
3180 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
3181 const int tile_size_bytes = pbi->tile_size_bytes;
3182
3183 size_t tile_col_size;
3184 int r, c;
3185
3186 // Read tile column sizes for all columns (we need the last tile buffer)
3187 for (c = 0; c < tile_cols; ++c) {
3188 const int is_last = c == tile_cols - 1;
3189 if (!is_last) {
3190 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
3191 data += tile_col_size_bytes;
3192 tile_col_data_end[c] = data + tile_col_size;
3193 } else {
3194 tile_col_size = data_end - data;
3195 tile_col_data_end[c] = data_end;
3196 }
3197 data += tile_col_size;
3198 }
3199
3200 data = data_start;
3201
3202 // Read the required tile sizes.
3203 for (c = tile_cols_start; c < tile_cols_end; ++c) {
3204 const int is_last = c == tile_cols - 1;
3205
3206 if (c > 0) data = tile_col_data_end[c - 1];
3207
3208 if (!is_last) data += tile_col_size_bytes;
3209
3210 // Get the whole of the last column, otherwise stop at the required tile.
3211 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
3212 tile_buffers[r][c].col = c;
3213
3214 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3215 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08003216 tile_size_bytes, c, r, cm->tile_encoding_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003217 }
3218 }
3219
3220 // If we have not read the last column, then read it to get the last tile.
3221 if (tile_cols_end != tile_cols) {
3222 c = tile_cols - 1;
3223
3224 data = tile_col_data_end[c - 1];
3225
3226 for (r = 0; r < tile_rows; ++r) {
3227 tile_buffers[r][c].col = c;
3228
3229 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3230 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08003231 tile_size_bytes, c, r, cm->tile_encoding_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003232 }
3233 }
3234 }
3235}
3236#else
3237// Reads the next tile returning its size and adjusting '*data' accordingly
3238// based on 'is_last'.
3239static void get_tile_buffer(const uint8_t *const data_end,
3240 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07003241 struct aom_internal_error_info *error_info,
3242 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003243 void *decrypt_state, TileBufferDec *const buf) {
3244 size_t size;
3245
3246 if (!is_last) {
Yaowu Xu0a79a1b2017-02-17 13:04:54 -08003247 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003248 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003249 "Truncated packet or corrupt tile length");
3250
3251 if (decrypt_cb) {
3252 uint8_t be_data[4];
3253 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3254 size = mem_get_varsize(be_data, tile_size_bytes);
3255 } else {
3256 size = mem_get_varsize(*data, tile_size_bytes);
3257 }
3258 *data += tile_size_bytes;
3259
3260 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003261 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003262 "Truncated packet or corrupt tile size");
3263 } else {
3264 size = data_end - *data;
3265 }
3266
3267 buf->data = *data;
3268 buf->size = size;
3269
3270 *data += size;
3271}
3272
3273static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003274 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003275 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003276 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07003277#if CONFIG_TILE_GROUPS
3278 int r, c;
3279 const int tile_cols = cm->tile_cols;
3280 const int tile_rows = cm->tile_rows;
3281 int tc = 0;
3282 int first_tile_in_tg = 0;
Thomas Davies80188d12016-10-26 16:08:35 -07003283 struct aom_read_bit_buffer rb_tg_hdr;
3284 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3285 const int num_tiles = tile_rows * tile_cols;
3286 const int num_bits = OD_ILOG(num_tiles) - 1;
James Zern6efba482017-04-20 20:53:49 -07003287 const size_t hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
Thomas Davies80188d12016-10-26 16:08:35 -07003288 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
Fangwen Fu73126c02017-02-08 22:37:47 -08003289#if CONFIG_DEPENDENT_HORZTILES
3290 int tile_group_start_col = 0;
3291 int tile_group_start_row = 0;
3292#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003293
3294 for (r = 0; r < tile_rows; ++r) {
3295 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07003296 TileBufferDec *const buf = &tile_buffers[r][c];
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003297 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
James Zern6efba482017-04-20 20:53:49 -07003298 const size_t hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
Thomas Davies80188d12016-10-26 16:08:35 -07003299
3300 buf->col = c;
3301 if (hdr_offset) {
3302 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
3303 rb_tg_hdr.bit_offset = tg_size_bit_offset;
3304 if (num_tiles) {
3305 pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
3306 pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
Fangwen Fu73126c02017-02-08 22:37:47 -08003307#if CONFIG_DEPENDENT_HORZTILES
3308 tile_group_start_row = r;
3309 tile_group_start_col = c;
3310#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003311 }
3312 }
3313 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
3314 data += hdr_offset;
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003315 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last,
3316 &pbi->common.error, &data, pbi->decrypt_cb,
3317 pbi->decrypt_state, buf);
Fangwen Fu73126c02017-02-08 22:37:47 -08003318#if CONFIG_DEPENDENT_HORZTILES
3319 cm->tile_group_start_row[r][c] = tile_group_start_row;
3320 cm->tile_group_start_col[r][c] = tile_group_start_col;
3321#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003322 }
3323 }
3324#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003325 int r, c;
3326 const int tile_cols = cm->tile_cols;
3327 const int tile_rows = cm->tile_rows;
3328
3329 for (r = 0; r < tile_rows; ++r) {
3330 for (c = 0; c < tile_cols; ++c) {
3331 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
3332 TileBufferDec *const buf = &tile_buffers[r][c];
3333 buf->col = c;
3334 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
3335 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
3336 }
3337 }
Thomas Davies80188d12016-10-26 16:08:35 -07003338#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003339}
3340#endif // CONFIG_EXT_TILE
3341
Yushin Cho77bba8d2016-11-04 16:36:56 -07003342#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003343static void daala_dec_init(AV1_COMMON *const cm, daala_dec_ctx *daala_dec,
Nathan E. Eggeab083972016-12-28 15:31:46 -05003344 aom_reader *r) {
3345 daala_dec->r = r;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003346
Yushin Cho70669122016-12-08 09:53:14 -10003347 // TODO(yushin) : activity masking info needs be signaled by a bitstream
3348 daala_dec->use_activity_masking = AV1_PVQ_ENABLE_ACTIVITY_MASKING;
3349
Yushin Cho7a428ba2017-01-12 16:28:49 -08003350#if !CONFIG_DAALA_DIST
3351 daala_dec->use_activity_masking = 0;
3352#endif
3353
Yushin Cho70669122016-12-08 09:53:14 -10003354 if (daala_dec->use_activity_masking)
3355 daala_dec->qm = OD_HVS_QM;
3356 else
3357 daala_dec->qm = OD_FLAT_QM;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003358
3359 od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
3360 daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
Yushin Cho70669122016-12-08 09:53:14 -10003361
3362 if (daala_dec->use_activity_masking) {
3363 int pli;
3364 int use_masking = daala_dec->use_activity_masking;
3365 int segment_id = 0;
3366 int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
3367
3368 for (pli = 0; pli < MAX_MB_PLANE; pli++) {
3369 int i;
3370 int q;
3371
3372 q = qindex;
3373 if (q <= OD_DEFAULT_QMS[use_masking][0][pli].interp_q << OD_COEFF_SHIFT) {
3374 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3375 &OD_DEFAULT_QMS[use_masking][0][pli], NULL);
3376 } else {
3377 i = 0;
3378 while (OD_DEFAULT_QMS[use_masking][i + 1][pli].qm_q4 != NULL &&
3379 q > OD_DEFAULT_QMS[use_masking][i + 1][pli].interp_q
3380 << OD_COEFF_SHIFT) {
3381 i++;
3382 }
3383 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3384 &OD_DEFAULT_QMS[use_masking][i][pli],
3385 &OD_DEFAULT_QMS[use_masking][i + 1][pli]);
3386 }
3387 }
3388 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07003389}
Yushin Cho70669122016-12-08 09:53:14 -10003390#endif // #if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003391
Yaowu Xuf883b422016-08-30 14:01:10 -07003392static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003393 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003394 AV1_COMMON *const cm = &pbi->common;
3395 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003396 const int tile_cols = cm->tile_cols;
3397 const int tile_rows = cm->tile_rows;
3398 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07003399 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003400#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003401 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003402 const int single_row = pbi->dec_tile_row >= 0;
3403 const int tile_rows_start = single_row ? dec_tile_row : 0;
3404 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003405 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003406 const int single_col = pbi->dec_tile_col >= 0;
3407 const int tile_cols_start = single_col ? dec_tile_col : 0;
3408 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3409 const int inv_col_order = pbi->inv_tile_order && !single_col;
3410 const int inv_row_order = pbi->inv_tile_order && !single_row;
3411#else
3412 const int tile_rows_start = 0;
3413 const int tile_rows_end = tile_rows;
3414 const int tile_cols_start = 0;
3415 const int tile_cols_end = tile_cols;
3416 const int inv_col_order = pbi->inv_tile_order;
3417 const int inv_row_order = pbi->inv_tile_order;
3418#endif // CONFIG_EXT_TILE
3419 int tile_row, tile_col;
3420
hui su0d103572017-03-01 17:58:01 -08003421#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003422 cm->do_subframe_update = n_tiles == 1;
hui su0d103572017-03-01 17:58:01 -08003423#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003424
3425 if (cm->lf.filter_level && !cm->skip_loop_filter &&
3426 pbi->lf_worker.data1 == NULL) {
3427 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07003428 aom_memalign(32, sizeof(LFWorkerData)));
3429 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003430 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003431 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003432 "Loop filter thread creation failed");
3433 }
3434 }
3435
3436 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3437 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3438 // Be sure to sync as we might be resuming after a failed frame decode.
3439 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003440 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
3441 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003442 }
3443
3444 assert(tile_rows <= MAX_TILE_ROWS);
3445 assert(tile_cols <= MAX_TILE_COLS);
3446
3447 get_tile_buffers(pbi, data, data_end, tile_buffers);
3448
3449 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003450 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003451 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003452 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003453 pbi->allocated_tiles = n_tiles;
3454 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003455#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003456 if (pbi->acct_enabled) {
3457 aom_accounting_reset(&pbi->accounting);
3458 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003459#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003460 // Load all tile information into tile_data.
3461 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3462 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3463 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
3464 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
3465
3466 td->cm = cm;
3467 td->xd = pbi->mb;
3468 td->xd.corrupted = 0;
3469 td->xd.counts =
3470 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3471 ? &cm->counts
3472 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003473 av1_zero(td->dqcoeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003474#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003475 av1_zero(td->pvq_ref_coeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003476#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003477 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003478 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003479 &td->bit_reader,
3480#if CONFIG_ANS && ANS_MAX_SYMBOLS
3481 1 << cm->ans_window_size_log2,
3482#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3483 pbi->decrypt_cb, pbi->decrypt_state);
Michael Bebenita6048d052016-08-25 14:40:54 -07003484#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003485 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003486 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003487 } else {
David Barkerd971f402016-10-25 13:52:07 +01003488 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003489 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003490#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003491 av1_init_macroblockd(cm, &td->xd,
3492#if CONFIG_PVQ
3493 td->pvq_ref_coeff,
3494#endif
Luc Trudeauf8164152017-04-11 16:20:51 -04003495#if CONFIG_CFL
3496 &td->cfl,
3497#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003498 td->dqcoeff);
Yushin Choc49ef3a2017-03-13 17:27:25 -07003499
Thomas Daviesf77d4ad2017-01-10 18:55:42 +00003500#if CONFIG_EC_ADAPT
3501 // Initialise the tile context from the frame context
3502 td->tctx = *cm->fc;
3503 td->xd.tile_ctx = &td->tctx;
3504#endif
Yushin Choc49ef3a2017-03-13 17:27:25 -07003505
3506#if CONFIG_PVQ
3507 daala_dec_init(cm, &td->xd.daala_dec, &td->bit_reader);
3508 td->xd.daala_dec.state.adapt = &td->tctx.pvq_context;
3509#endif
3510
Urvang Joshib100db72016-10-12 16:28:56 -07003511#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003512 td->xd.plane[0].color_index_map = td->color_index_map[0];
3513 td->xd.plane[1].color_index_map = td->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003514#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003515 }
3516 }
3517
3518 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3519 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
3520 int mi_row = 0;
3521 TileInfo tile_info;
3522
Yaowu Xuf883b422016-08-30 14:01:10 -07003523 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003524
3525 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3526 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
3527 TileData *const td = pbi->tile_data + tile_cols * row + col;
Michael Bebenita6048d052016-08-25 14:40:54 -07003528#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003529 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003530 td->bit_reader.accounting->last_tell_frac =
3531 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003532 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003533#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003534
Yaowu Xuf883b422016-08-30 14:01:10 -07003535 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003536
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003537#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003538#if CONFIG_TILE_GROUPS
3539 av1_tile_set_tg_boundary(&tile_info, cm, tile_row, tile_col);
3540 if (!cm->dependent_horz_tiles || tile_row == 0 ||
3541 tile_info.tg_horz_boundary) {
3542#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003543 if (!cm->dependent_horz_tiles || tile_row == 0) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003544#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003545 av1_zero_above_context(cm, tile_info.mi_col_start,
3546 tile_info.mi_col_end);
3547 }
3548#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003549 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003550#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003551
3552 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
3553 mi_row += cm->mib_size) {
3554 int mi_col;
3555
Yaowu Xuf883b422016-08-30 14:01:10 -07003556 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003557
3558 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
3559 mi_col += cm->mib_size) {
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003560 av1_update_boundary_info(cm, &tile_info, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003561 decode_partition(pbi, &td->xd,
3562#if CONFIG_SUPERTX
3563 0,
3564#endif // CONFIG_SUPERTX
3565 mi_row, mi_col, &td->bit_reader, cm->sb_size,
3566 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003567#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3568 detoken_and_recon_sb(pbi, &td->xd, mi_row, mi_col, &td->bit_reader,
3569 cm->sb_size);
3570#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003571 }
Angie Chiangd0916d92017-03-10 17:54:18 -08003572 aom_merge_corrupted_flag(&pbi->mb.corrupted, td->xd.corrupted);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003573 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003574 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003575 "Failed to decode tile data");
hui su0d103572017-03-01 17:58:01 -08003576#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003577 if (cm->do_subframe_update &&
3578 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
hui su0161a932017-01-24 14:12:11 -08003579 const int mi_rows_per_update =
3580 MI_SIZE * AOMMAX(cm->mi_rows / MI_SIZE / COEF_PROBS_BUFS, 1);
3581 if ((mi_row + MI_SIZE) % mi_rows_per_update == 0 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003582 mi_row + MI_SIZE < cm->mi_rows &&
3583 cm->coef_probs_update_idx < COEF_PROBS_BUFS - 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003584 av1_partial_adapt_probs(cm, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003585 ++cm->coef_probs_update_idx;
3586 }
3587 }
hui su0d103572017-03-01 17:58:01 -08003588#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003589 }
3590 }
3591
3592 assert(mi_row > 0);
3593
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003594// when Parallel deblocking is enabled, deblocking should not
3595// be interleaved with decoding. Instead, deblocking should be done
3596// after the entire frame is decoded.
Jingning Han52ece882017-04-07 14:58:25 -07003597#if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING && !CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -07003598 // Loopfilter one tile row.
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08003599 // Note: If out-of-order tile decoding is used(for example, inv_row_order
3600 // = 1), the loopfiltering has be done after all tile rows are decoded.
3601 if (!inv_row_order && cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003602 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003603 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003604 const int lf_end = tile_info.mi_row_end - cm->mib_size;
3605
3606 // Delay the loopfilter if the first tile row is only
3607 // a single superblock high.
3608 if (lf_end <= 0) continue;
3609
3610 // Decoding has completed. Finish up the loop filter in this thread.
3611 if (tile_info.mi_row_end >= cm->mi_rows) continue;
3612
3613 winterface->sync(&pbi->lf_worker);
3614 lf_data->start = lf_start;
3615 lf_data->stop = lf_end;
3616 if (pbi->max_threads > 1) {
3617 winterface->launch(&pbi->lf_worker);
3618 } else {
3619 winterface->execute(&pbi->lf_worker);
3620 }
3621 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003622#endif // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003623
3624 // After loopfiltering, the last 7 row pixels in each superblock row may
3625 // still be changed by the longest loopfilter of the next superblock row.
3626 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003627 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003628 }
3629
Jingning Han52ece882017-04-07 14:58:25 -07003630#if CONFIG_VAR_TX || CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -07003631 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003632 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
3633 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003634#else
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003635#if CONFIG_PARALLEL_DEBLOCKING
3636 // Loopfilter all rows in the frame in the frame.
3637 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3638 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3639 winterface->sync(&pbi->lf_worker);
3640 lf_data->start = 0;
3641 lf_data->stop = cm->mi_rows;
3642 winterface->execute(&pbi->lf_worker);
3643 }
3644#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003645 // Loopfilter remaining rows in the frame.
3646 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3647 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3648 winterface->sync(&pbi->lf_worker);
3649 lf_data->start = lf_data->stop;
3650 lf_data->stop = cm->mi_rows;
3651 winterface->execute(&pbi->lf_worker);
3652 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003653#endif // CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003654#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003655 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003656 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003657
3658#if CONFIG_EXT_TILE
3659 if (n_tiles == 1) {
3660#if CONFIG_ANS
3661 return data_end;
3662#else
3663 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07003664 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003665#endif // CONFIG_ANS
3666 } else {
3667 // Return the end of the last tile buffer
3668 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3669 }
3670#else
3671#if CONFIG_ANS
3672 return data_end;
3673#else
3674 {
3675 // Get last tile data.
3676 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003677 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003678 }
3679#endif // CONFIG_ANS
3680#endif // CONFIG_EXT_TILE
3681}
3682
3683static int tile_worker_hook(TileWorkerData *const tile_data,
3684 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003685 AV1Decoder *const pbi = tile_data->pbi;
3686 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003687 int mi_row, mi_col;
3688
3689 if (setjmp(tile_data->error_info.jmp)) {
3690 tile_data->error_info.setjmp = 0;
Angie Chiangd0916d92017-03-10 17:54:18 -08003691 aom_merge_corrupted_flag(&tile_data->xd.corrupted, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003692 return 0;
3693 }
3694
3695 tile_data->error_info.setjmp = 1;
3696 tile_data->xd.error_info = &tile_data->error_info;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003697#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003698#if CONFIG_TILE_GROUPS
3699 if (!cm->dependent_horz_tiles || tile->tg_horz_boundary) {
3700#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003701 if (!cm->dependent_horz_tiles) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003702#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003703 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
3704 }
3705#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003706 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003707#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003708
3709 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
3710 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003711 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003712
3713 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3714 mi_col += cm->mib_size) {
3715 decode_partition(pbi, &tile_data->xd,
3716#if CONFIG_SUPERTX
3717 0,
3718#endif
3719 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
3720 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003721#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3722 detoken_and_recon_sb(pbi, &tile_data->xd, mi_row, mi_col,
3723 &tile_data->bit_reader, cm->sb_size);
3724#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003725 }
3726 }
3727 return !tile_data->xd.corrupted;
3728}
3729
3730// sorts in descending order
3731static int compare_tile_buffers(const void *a, const void *b) {
3732 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
3733 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
3734 return (int)(buf2->size - buf1->size);
3735}
3736
Yaowu Xuf883b422016-08-30 14:01:10 -07003737static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003738 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003739 AV1_COMMON *const cm = &pbi->common;
3740 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003741 const int tile_cols = cm->tile_cols;
3742 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003743 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
clang-format67948d32016-09-07 22:40:40 -07003744 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003745#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003746 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003747 const int single_row = pbi->dec_tile_row >= 0;
3748 const int tile_rows_start = single_row ? dec_tile_row : 0;
3749 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003750 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003751 const int single_col = pbi->dec_tile_col >= 0;
3752 const int tile_cols_start = single_col ? dec_tile_col : 0;
3753 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3754#else
3755 const int tile_rows_start = 0;
3756 const int tile_rows_end = tile_rows;
3757 const int tile_cols_start = 0;
3758 const int tile_cols_end = tile_cols;
3759#endif // CONFIG_EXT_TILE
3760 int tile_row, tile_col;
3761 int i;
3762
3763#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3764 int final_worker = -1;
3765#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3766
3767 assert(tile_rows <= MAX_TILE_ROWS);
3768 assert(tile_cols <= MAX_TILE_COLS);
3769
3770 assert(tile_cols * tile_rows > 1);
3771
Yaowu Xuc27fc142016-08-22 16:08:15 -07003772 // TODO(jzern): See if we can remove the restriction of passing in max
3773 // threads to the decoder.
3774 if (pbi->num_tile_workers == 0) {
3775 const int num_threads = pbi->max_threads & ~1;
3776 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07003777 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003778 // Ensure tile data offsets will be properly aligned. This may fail on
3779 // platforms without DECLARE_ALIGNED().
3780 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3781 CHECK_MEM_ERROR(
3782 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003783 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003784 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07003785 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003786 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003787 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003788 ++pbi->num_tile_workers;
3789
3790 winterface->init(worker);
3791 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003792 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003793 "Tile decoder thread creation failed");
3794 }
3795 }
3796 }
3797
3798 // Reset tile decoding hook
3799 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003800 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003801 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003802 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003803 worker->data1 = &pbi->tile_worker_data[i];
3804 worker->data2 = &pbi->tile_worker_info[i];
3805 }
3806
3807 // Initialize thread frame counts.
3808 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3809 for (i = 0; i < num_workers; ++i) {
3810 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003811 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003812 }
3813 }
3814
3815 // Load tile data into tile_buffers
3816 get_tile_buffers(pbi, data, data_end, tile_buffers);
3817
3818 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3819 // Sort the buffers in this tile row based on size in descending order.
3820 qsort(&tile_buffers[tile_row][tile_cols_start],
3821 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
3822 compare_tile_buffers);
3823
3824 // Rearrange the tile buffers in this tile row such that per-tile group
3825 // the largest, and presumably the most difficult tile will be decoded in
3826 // the main thread. This should help minimize the number of instances
3827 // where the main thread is waiting for a worker to complete.
3828 {
3829 int group_start;
3830 for (group_start = tile_cols_start; group_start < tile_cols_end;
3831 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003832 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003833 const TileBufferDec largest = tile_buffers[tile_row][group_start];
3834 memmove(&tile_buffers[tile_row][group_start],
3835 &tile_buffers[tile_row][group_start + 1],
3836 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
3837 tile_buffers[tile_row][group_end - 1] = largest;
3838 }
3839 }
3840
3841 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
3842 // Launch workers for individual columns
3843 for (i = 0; i < num_workers && tile_col < tile_cols_end;
3844 ++i, ++tile_col) {
3845 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07003846 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003847 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
3848 TileInfo *const tile_info = (TileInfo *)worker->data2;
3849
3850 twd->pbi = pbi;
3851 twd->xd = pbi->mb;
3852 twd->xd.corrupted = 0;
3853 twd->xd.counts =
3854 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3855 ? &twd->counts
3856 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003857 av1_zero(twd->dqcoeff);
3858 av1_tile_init(tile_info, cm, tile_row, buf->col);
3859 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003860 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003861 &twd->bit_reader,
3862#if CONFIG_ANS && ANS_MAX_SYMBOLS
3863 1 << cm->ans_window_size_log2,
3864#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3865 pbi->decrypt_cb, pbi->decrypt_state);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003866 av1_init_macroblockd(cm, &twd->xd,
3867#if CONFIG_PVQ
3868 twd->pvq_ref_coeff,
3869#endif
Luc Trudeauf8164152017-04-11 16:20:51 -04003870#if CONFIG_CFL
3871 &twd->cfl,
3872#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003873 twd->dqcoeff);
3874#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003875 daala_dec_init(cm, &twd->xd.daala_dec, &twd->bit_reader);
Yushin Choc49ef3a2017-03-13 17:27:25 -07003876 twd->xd.daala_dec.state.adapt = &twd->tctx.pvq_context;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003877#endif
Yushin Chod767beb2017-03-24 10:15:47 -07003878#if CONFIG_EC_ADAPT
3879 // Initialise the tile context from the frame context
3880 twd->tctx = *cm->fc;
3881 twd->xd.tile_ctx = &twd->tctx;
3882#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003883#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003884 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
3885 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003886#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003887
3888 worker->had_error = 0;
3889 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
3890 winterface->execute(worker);
3891 } else {
3892 winterface->launch(worker);
3893 }
3894
3895#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3896 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
3897 final_worker = i;
3898 }
3899#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3900 }
3901
3902 // Sync all workers
3903 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003904 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003905 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07003906 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07003907 // info in cm. Additionally once the threads have been synced and an
3908 // error is detected, there's no point in continuing to decode tiles.
3909 pbi->mb.corrupted |= !winterface->sync(worker);
3910 }
3911 }
3912 }
3913
3914 // Accumulate thread frame counts.
3915 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3916 for (i = 0; i < num_workers; ++i) {
3917 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003918 av1_accumulate_frame_counts(&cm->counts, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003919 }
3920 }
3921
3922#if CONFIG_EXT_TILE
3923 // Return the end of the last tile buffer
3924 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3925#else
3926#if CONFIG_ANS
3927 return data_end;
3928#else
3929 assert(final_worker != -1);
3930 {
3931 TileWorkerData *const twd =
3932 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003933 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003934 }
3935#endif // CONFIG_ANS
3936#endif // CONFIG_EXT_TILE
3937}
3938
3939static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003940 AV1_COMMON *const cm = (AV1_COMMON *)data;
3941 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003942}
3943
Yaowu Xuf883b422016-08-30 14:01:10 -07003944static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3945 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003946 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003947 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003948 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003949 cm->bit_depth = AOM_BITS_8;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003950 }
3951
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003952#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003953 if (cm->bit_depth > AOM_BITS_8) {
3954 cm->use_highbitdepth = 1;
3955 } else {
Yaowu Xu345a22d2017-02-27 09:23:52 -08003956#if CONFIG_LOWBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003957 cm->use_highbitdepth = 0;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003958#else
3959 cm->use_highbitdepth = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003960#endif
3961 }
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003962#endif
3963
Yaowu Xuf883b422016-08-30 14:01:10 -07003964 cm->color_space = aom_rb_read_literal(rb, 3);
3965 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003966 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07003967 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003968 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003969 cm->subsampling_x = aom_rb_read_bit(rb);
3970 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003971 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003972 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003973 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07003974 if (aom_rb_read_bit(rb))
3975 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003976 "Reserved bit set");
3977 } else {
3978 cm->subsampling_y = cm->subsampling_x = 1;
3979 }
3980 } else {
3981 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
3982 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
3983 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
3984 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003985 if (aom_rb_read_bit(rb))
3986 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003987 "Reserved bit set");
3988 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003989 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003990 "4:4:4 color not supported in profile 0 or 2");
3991 }
3992 }
3993}
3994
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003995#if CONFIG_REFERENCE_BUFFER
3996void read_sequence_header(SequenceHeader *seq_params) {
3997 /* Placeholder for actually reading from the bitstream */
3998 seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
3999 seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
4000 seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
4001}
4002#endif
4003
Yaowu Xuf883b422016-08-30 14:01:10 -07004004static size_t read_uncompressed_header(AV1Decoder *pbi,
4005 struct aom_read_bit_buffer *rb) {
4006 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004007 MACROBLOCKD *const xd = &pbi->mb;
4008 BufferPool *const pool = cm->buffer_pool;
4009 RefCntBuffer *const frame_bufs = pool->frame_bufs;
4010 int i, mask, ref_index = 0;
4011 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004012
4013#if CONFIG_REFERENCE_BUFFER
4014 /* TODO: Move outside frame loop or inside key-frame branch */
4015 read_sequence_header(&pbi->seq_params);
4016#endif
4017
Yaowu Xuc27fc142016-08-22 16:08:15 -07004018 cm->last_frame_type = cm->frame_type;
4019 cm->last_intra_only = cm->intra_only;
4020
4021#if CONFIG_EXT_REFS
4022 // NOTE: By default all coded frames to be used as a reference
4023 cm->is_reference_frame = 1;
4024#endif // CONFIG_EXT_REFS
4025
Yaowu Xuf883b422016-08-30 14:01:10 -07004026 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
4027 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004028 "Invalid frame marker");
4029
Yaowu Xuf883b422016-08-30 14:01:10 -07004030 cm->profile = av1_read_profile(rb);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004031#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004032 if (cm->profile >= MAX_PROFILES)
Yaowu Xuf883b422016-08-30 14:01:10 -07004033 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004034 "Unsupported bitstream profile");
4035#else
4036 if (cm->profile >= PROFILE_2)
Yaowu Xuf883b422016-08-30 14:01:10 -07004037 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004038 "Unsupported bitstream profile");
4039#endif
4040
Yaowu Xuf883b422016-08-30 14:01:10 -07004041 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004042
4043 if (cm->show_existing_frame) {
Yaowu Xu415ba932016-12-27 11:17:32 -08004044 // Show an existing frame directly.
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004045 const int existing_frame_idx = aom_rb_read_literal(rb, 3);
4046 const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
Yaowu Xu415ba932016-12-27 11:17:32 -08004047#if CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004048 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004049 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4050 int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004051 /* Compare display_frame_id with ref_frame_id and check valid for
4052 * referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004053 if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
4054 cm->valid_for_referencing[existing_frame_idx] == 0)
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004055 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4056 "Reference buffer frame ID mismatch");
4057 }
4058#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004059 lock_buffer_pool(pool);
4060 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
4061 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07004062 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004063 "Buffer %d does not contain a decoded frame",
4064 frame_to_show);
4065 }
4066 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
4067 unlock_buffer_pool(pool);
4068
4069 cm->lf.filter_level = 0;
4070 cm->show_frame = 1;
4071 pbi->refresh_frame_flags = 0;
4072
4073 if (cm->frame_parallel_decode) {
4074 for (i = 0; i < REF_FRAMES; ++i)
4075 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
4076 }
4077
4078 return 0;
4079 }
4080
Yaowu Xuf883b422016-08-30 14:01:10 -07004081 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
4082 cm->show_frame = aom_rb_read_bit(rb);
4083 cm->error_resilient_mode = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004084#if CONFIG_REFERENCE_BUFFER
4085 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004086 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4087 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4088 int prev_frame_id = 0;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004089 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004090 prev_frame_id = cm->current_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004091 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004092 cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004093
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004094 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004095 int diff_frame_id;
4096 if (cm->current_frame_id > prev_frame_id) {
4097 diff_frame_id = cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004098 } else {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004099 diff_frame_id =
4100 (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004101 }
4102 /* Check current_frame_id for conformance */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004103 if (prev_frame_id == cm->current_frame_id ||
4104 diff_frame_id >= (1 << (frame_id_length - 1))) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004105 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4106 "Invalid value of current_frame_id");
4107 }
4108 }
4109 /* Check if some frames need to be marked as not valid for referencing */
4110 for (i = 0; i < REF_FRAMES; i++) {
4111 if (cm->frame_type == KEY_FRAME) {
4112 cm->valid_for_referencing[i] = 0;
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004113 } else if (cm->current_frame_id - (1 << diff_len) > 0) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004114 if (cm->ref_frame_id[i] > cm->current_frame_id ||
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004115 cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004116 cm->valid_for_referencing[i] = 0;
4117 } else {
4118 if (cm->ref_frame_id[i] > cm->current_frame_id &&
4119 cm->ref_frame_id[i] <
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004120 (1 << frame_id_length) + cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004121 cm->valid_for_referencing[i] = 0;
4122 }
4123 }
4124 }
4125#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004126 if (cm->frame_type == KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004127 if (!av1_read_sync_code(rb))
4128 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004129 "Invalid frame sync code");
4130
4131 read_bitdepth_colorspace_sampling(cm, rb);
4132 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
4133
4134 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4135 cm->frame_refs[i].idx = INVALID_IDX;
4136 cm->frame_refs[i].buf = NULL;
4137 }
4138
4139 setup_frame_size(cm, rb);
4140 if (pbi->need_resync) {
4141 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4142 pbi->need_resync = 0;
4143 }
Alex Converseeb780e72016-12-13 12:46:41 -08004144#if CONFIG_ANS && ANS_MAX_SYMBOLS
4145 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4146#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
Urvang Joshib100db72016-10-12 16:28:56 -07004147#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004148 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004149#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004150 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004151 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004152#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07004153 if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07004154#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004155 if (cm->error_resilient_mode) {
4156 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
4157 } else {
4158 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004159 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004160 ? RESET_FRAME_CONTEXT_ALL
4161 : RESET_FRAME_CONTEXT_CURRENT;
4162 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004163 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004164 ? RESET_FRAME_CONTEXT_CURRENT
4165 : RESET_FRAME_CONTEXT_NONE;
4166 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07004167 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004168 ? RESET_FRAME_CONTEXT_ALL
4169 : RESET_FRAME_CONTEXT_CURRENT;
4170 }
4171 }
4172
4173 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004174 if (!av1_read_sync_code(rb))
4175 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004176 "Invalid frame sync code");
4177
4178 read_bitdepth_colorspace_sampling(cm, rb);
4179
Yaowu Xuf883b422016-08-30 14:01:10 -07004180 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004181 setup_frame_size(cm, rb);
4182 if (pbi->need_resync) {
4183 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
4184 pbi->need_resync = 0;
4185 }
Alex Converseeb780e72016-12-13 12:46:41 -08004186#if CONFIG_ANS && ANS_MAX_SYMBOLS
4187 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
4188#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004189 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Yaowu Xuf883b422016-08-30 14:01:10 -07004190 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004191
4192#if CONFIG_EXT_REFS
4193 if (!pbi->refresh_frame_flags) {
4194 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
4195 // will not be used as a reference
4196 cm->is_reference_frame = 0;
4197 }
4198#endif // CONFIG_EXT_REFS
4199
4200 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004201 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004202 const int idx = cm->ref_frame_map[ref];
4203 RefBuffer *const ref_frame = &cm->frame_refs[i];
4204 ref_frame->idx = idx;
4205 ref_frame->buf = &frame_bufs[idx].buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07004206 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004207#if CONFIG_REFERENCE_BUFFER
4208 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004209 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
4210 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
4211 int delta_frame_id_minus1 = aom_rb_read_literal(rb, diff_len);
4212 int ref_frame_id =
4213 ((cm->current_frame_id - (delta_frame_id_minus1 + 1) +
4214 (1 << frame_id_length)) %
4215 (1 << frame_id_length));
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004216 /* Compare values derived from delta_frame_id_minus1 and
4217 * refresh_frame_flags. Also, check valid for referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004218 if (ref_frame_id != cm->ref_frame_id[ref] ||
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004219 cm->valid_for_referencing[ref] == 0)
4220 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
4221 "Reference buffer frame ID mismatch");
4222 }
4223#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004224 }
4225
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004226#if CONFIG_FRAME_SIZE
4227 if (cm->error_resilient_mode == 0) {
4228 setup_frame_size_with_refs(cm, rb);
4229 } else {
4230 setup_frame_size(cm, rb);
4231 }
4232#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004233 setup_frame_size_with_refs(cm, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004234#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004235
Yaowu Xuf883b422016-08-30 14:01:10 -07004236 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Angie Chiang5678ad92016-11-21 09:38:40 -08004237 cm->interp_filter = read_frame_interp_filter(rb);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004238#if CONFIG_TEMPMV_SIGNALING
4239 if (!cm->error_resilient_mode) {
4240 cm->use_prev_frame_mvs = aom_rb_read_bit(rb);
4241 }
4242#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004243 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4244 RefBuffer *const ref_buf = &cm->frame_refs[i];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004245#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07004246 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004247 &ref_buf->sf, ref_buf->buf->y_crop_width,
4248 ref_buf->buf->y_crop_height, cm->width, cm->height,
4249 cm->use_highbitdepth);
4250#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004251 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004252 &ref_buf->sf, ref_buf->buf->y_crop_width,
4253 ref_buf->buf->y_crop_height, cm->width, cm->height);
4254#endif
4255 }
4256 }
4257 }
Fangwen Fu8d164de2016-12-14 13:40:54 -08004258#if CONFIG_TEMPMV_SIGNALING
4259 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
4260#endif
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004261
4262#if CONFIG_REFERENCE_BUFFER
4263 if (pbi->seq_params.frame_id_numbers_present_flag) {
4264 /* If bitmask is set, update reference frame id values and
4265 mark frames as valid for reference */
4266 int refresh_frame_flags =
4267 cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
4268 for (i = 0; i < REF_FRAMES; i++) {
4269 if ((refresh_frame_flags >> i) & 1) {
4270 cm->ref_frame_id[i] = cm->current_frame_id;
4271 cm->valid_for_referencing[i] = 1;
4272 }
4273 }
4274 }
4275#endif
4276
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004277#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004278 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
4279#endif
4280 get_frame_new_buffer(cm)->color_space = cm->color_space;
4281 get_frame_new_buffer(cm)->color_range = cm->color_range;
4282 get_frame_new_buffer(cm)->render_width = cm->render_width;
4283 get_frame_new_buffer(cm)->render_height = cm->render_height;
4284
4285 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004286 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004287 "Keyframe / intra-only frame required to reset decoder"
4288 " state");
4289 }
4290
4291 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004292 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004293 ? REFRESH_FRAME_CONTEXT_FORWARD
4294 : REFRESH_FRAME_CONTEXT_BACKWARD;
4295 } else {
4296 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
4297 }
4298
Yaowu Xuf883b422016-08-30 14:01:10 -07004299 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07004300 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07004301 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004302
4303 // Generate next_ref_frame_map.
4304 lock_buffer_pool(pool);
4305 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
4306 if (mask & 1) {
4307 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
4308 ++frame_bufs[cm->new_fb_idx].ref_count;
4309 } else {
4310 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4311 }
4312 // Current thread holds the reference frame.
4313 if (cm->ref_frame_map[ref_index] >= 0)
4314 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4315 ++ref_index;
4316 }
4317
4318 for (; ref_index < REF_FRAMES; ++ref_index) {
4319 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4320
4321 // Current thread holds the reference frame.
4322 if (cm->ref_frame_map[ref_index] >= 0)
4323 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4324 }
4325 unlock_buffer_pool(pool);
4326 pbi->hold_ref_buf = 1;
4327
4328 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07004329 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004330
4331#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07004332 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004333#else
4334 set_sb_size(cm, BLOCK_64X64);
4335#endif // CONFIG_EXT_PARTITION
4336
4337 setup_loopfilter(cm, rb);
Jean-Marc Valin01435132017-02-18 14:12:53 -05004338#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01004339 setup_cdef(cm, rb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004340#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004341#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004342 decode_restoration_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004343#endif // CONFIG_LOOP_RESTORATION
4344 setup_quantization(cm, rb);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004345#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004346 xd->bd = (int)cm->bit_depth;
4347#endif
4348
hui su0d103572017-03-01 17:58:01 -08004349#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuf883b422016-08-30 14:01:10 -07004350 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004351 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4352 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4353 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4354 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
4355 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4356 }
hui su0d103572017-03-01 17:58:01 -08004357#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004358
4359 setup_segmentation(cm, rb);
4360
Arild Fuldseth07441162016-08-15 15:07:52 +02004361#if CONFIG_DELTA_Q
4362 {
4363 struct segmentation *const seg = &cm->seg;
4364 int segment_quantizer_active = 0;
4365 for (i = 0; i < MAX_SEGMENTS; i++) {
4366 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
4367 segment_quantizer_active = 1;
4368 }
4369 }
4370
Thomas Daviesf6936102016-09-05 16:51:31 +01004371 cm->delta_q_res = 1;
Fangwen Fu231fe422017-04-24 17:52:29 -07004372#if CONFIG_EXT_DELTA_Q
4373 cm->delta_lf_res = 1;
4374#endif
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01004375 if (segment_quantizer_active == 0 && cm->base_qindex > 0) {
Arild Fuldseth07441162016-08-15 15:07:52 +02004376 cm->delta_q_present_flag = aom_rb_read_bit(rb);
4377 } else {
4378 cm->delta_q_present_flag = 0;
4379 }
4380 if (cm->delta_q_present_flag) {
4381 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01004382 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Fangwen Fu231fe422017-04-24 17:52:29 -07004383#if CONFIG_EXT_DELTA_Q
4384 if (segment_quantizer_active) {
4385 assert(seg->abs_delta == SEGMENT_DELTADATA);
4386 }
4387 cm->delta_lf_present_flag = aom_rb_read_bit(rb);
4388 if (cm->delta_lf_present_flag) {
4389 xd->prev_delta_lf_from_base = 0;
4390 cm->delta_lf_res = 1 << aom_rb_read_literal(rb, 2);
4391 } else {
4392 cm->delta_lf_present_flag = 0;
4393 }
4394#endif // CONFIG_EXT_DELTA_Q
Arild Fuldseth07441162016-08-15 15:07:52 +02004395 }
4396 }
4397#endif
4398
Urvang Joshi454280d2016-10-14 16:51:44 -07004399 for (i = 0; i < MAX_SEGMENTS; ++i) {
4400 const int qindex = cm->seg.enabled
4401 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
4402 : cm->base_qindex;
4403 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
4404 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4405 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004406 }
4407
4408 setup_segmentation_dequant(cm);
Yue Cheneeacc4c2017-01-17 17:29:17 -08004409 cm->tx_mode = read_tx_mode(cm, xd, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004410 cm->reference_mode = read_frame_reference_mode(cm, rb);
4411
Sarah Parkere68a3e42017-02-16 14:03:24 -08004412#if CONFIG_EXT_TX
4413 cm->reduced_tx_set_used = aom_rb_read_bit(rb);
4414#endif // CONFIG_EXT_TX
4415
Yaowu Xuc27fc142016-08-22 16:08:15 -07004416 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004417 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004418
4419 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07004420 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004421 "Invalid header size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004422 return sz;
4423}
4424
4425#if CONFIG_EXT_TX
Sarah Parkerb926f322017-04-24 16:19:48 -07004426#if !CONFIG_EC_ADAPT
Yaowu Xuf883b422016-08-30 14:01:10 -07004427static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004428 int i, j, k;
4429 int s;
4430 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004431 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004432 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4433 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004434 for (j = 0; j < num_ext_tx_set[ext_tx_set_type_inter[s]] - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004435 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004436 }
4437 }
4438 }
4439
4440 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004441 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004442 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4443 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
4444 for (j = 0; j < INTRA_MODES; ++j)
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004445 for (k = 0; k < num_ext_tx_set[ext_tx_set_type_intra[s]] - 1; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004446 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
4447 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004448 }
4449 }
4450 }
4451}
Sarah Parkerb926f322017-04-24 16:19:48 -07004452#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004453#else
4454
Yaowu Xuc27fc142016-08-22 16:08:15 -07004455#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004456#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07004457static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004458 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -07004459 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004460 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
Jingning Hanfeb517c2016-12-21 16:02:07 -08004461 for (j = TX_8X8; j < TX_SIZES; ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004462 av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004463 }
4464 }
4465 }
4466}
4467#endif // CONFIG_SUPERTX
4468
4469#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +00004470static void read_global_motion_params(WarpedMotionParams *params,
Sarah Parkerf1783292017-04-05 11:55:27 -07004471 WarpedMotionParams *ref_params,
Sarah Parker13d06622017-03-10 17:03:28 -08004472 aom_prob *probs, aom_reader *r,
4473 int allow_hp) {
David Barkercf3d0b02016-11-10 10:14:49 +00004474 TransformationType type =
Michael Bebenita6048d052016-08-25 14:40:54 -07004475 aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
Sarah Parker13d06622017-03-10 17:03:28 -08004476 int trans_bits;
4477 int trans_dec_factor;
Sarah Parkerf1783292017-04-05 11:55:27 -07004478 int trans_prec_diff;
Sarah Parker4c10a3c2017-04-10 19:37:59 -07004479 set_default_warp_params(params);
David Barkercf3d0b02016-11-10 10:14:49 +00004480 params->wmtype = type;
4481 switch (type) {
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004482 case HOMOGRAPHY:
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004483 case HORTRAPEZOID:
4484 case VERTRAPEZOID:
4485 if (type != HORTRAPEZOID)
4486 params->wmmat[6] =
Sarah Parkerf1783292017-04-05 11:55:27 -07004487 aom_read_signed_primitive_refsubexpfin(
4488 r, GM_ROW3HOMO_MAX + 1, SUBEXPFIN_K,
4489 (ref_params->wmmat[6] >> GM_ROW3HOMO_PREC_DIFF)) *
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004490 GM_ROW3HOMO_DECODE_FACTOR;
4491 if (type != VERTRAPEZOID)
4492 params->wmmat[7] =
Sarah Parkerf1783292017-04-05 11:55:27 -07004493 aom_read_signed_primitive_refsubexpfin(
4494 r, GM_ROW3HOMO_MAX + 1, SUBEXPFIN_K,
4495 (ref_params->wmmat[7] >> GM_ROW3HOMO_PREC_DIFF)) *
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004496 GM_ROW3HOMO_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004497 case AFFINE:
4498 case ROTZOOM:
Sarah Parkerf1783292017-04-05 11:55:27 -07004499 params->wmmat[2] = aom_read_signed_primitive_refsubexpfin(
4500 r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4501 (ref_params->wmmat[2] >> GM_ALPHA_PREC_DIFF) -
4502 (1 << GM_ALPHA_PREC_BITS)) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004503 GM_ALPHA_DECODE_FACTOR +
David Barkercf3d0b02016-11-10 10:14:49 +00004504 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004505 if (type != VERTRAPEZOID)
Sarah Parkerf1783292017-04-05 11:55:27 -07004506 params->wmmat[3] = aom_read_signed_primitive_refsubexpfin(
4507 r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4508 (ref_params->wmmat[3] >> GM_ALPHA_PREC_DIFF)) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004509 GM_ALPHA_DECODE_FACTOR;
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004510 if (type >= AFFINE) {
4511 if (type != HORTRAPEZOID)
Sarah Parkerf1783292017-04-05 11:55:27 -07004512 params->wmmat[4] = aom_read_signed_primitive_refsubexpfin(
4513 r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4514 (ref_params->wmmat[4] >> GM_ALPHA_PREC_DIFF)) *
4515 GM_ALPHA_DECODE_FACTOR;
4516 params->wmmat[5] = aom_read_signed_primitive_refsubexpfin(
4517 r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4518 (ref_params->wmmat[5] >> GM_ALPHA_PREC_DIFF) -
4519 (1 << GM_ALPHA_PREC_BITS)) *
David Barkercf3d0b02016-11-10 10:14:49 +00004520 GM_ALPHA_DECODE_FACTOR +
4521 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004522 } else {
David Barkercf3d0b02016-11-10 10:14:49 +00004523 params->wmmat[4] = -params->wmmat[3];
4524 params->wmmat[5] = params->wmmat[2];
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004525 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004526 // fallthrough intended
David Barkercf3d0b02016-11-10 10:14:49 +00004527 case TRANSLATION:
Sarah Parker13d06622017-03-10 17:03:28 -08004528 trans_bits = (type == TRANSLATION) ? GM_ABS_TRANS_ONLY_BITS - !allow_hp
4529 : GM_ABS_TRANS_BITS;
4530 trans_dec_factor = (type == TRANSLATION)
4531 ? GM_TRANS_ONLY_DECODE_FACTOR * (1 << !allow_hp)
4532 : GM_TRANS_DECODE_FACTOR;
Sarah Parkerf1783292017-04-05 11:55:27 -07004533 trans_prec_diff = (type == TRANSLATION)
4534 ? GM_TRANS_ONLY_PREC_DIFF + !allow_hp
4535 : GM_TRANS_PREC_DIFF;
4536 params->wmmat[0] = aom_read_signed_primitive_refsubexpfin(
4537 r, (1 << trans_bits) + 1, SUBEXPFIN_K,
4538 (ref_params->wmmat[0] >> trans_prec_diff)) *
4539 trans_dec_factor;
4540 params->wmmat[1] = aom_read_signed_primitive_refsubexpfin(
4541 r, (1 << trans_bits) + 1, SUBEXPFIN_K,
4542 (ref_params->wmmat[1] >> trans_prec_diff)) *
4543 trans_dec_factor;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004544 case IDENTITY: break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004545 default: assert(0);
4546 }
Debargha Mukherjee3b6c5442017-03-30 08:22:00 -07004547 if (params->wmtype <= AFFINE)
4548 if (!get_shear_params(params)) assert(0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004549}
4550
Yaowu Xuf883b422016-08-30 14:01:10 -07004551static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004552 int frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004553 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
Sarah Parkerf1783292017-04-05 11:55:27 -07004554 read_global_motion_params(
4555 &cm->global_motion[frame], &cm->prev_frame->global_motion[frame],
4556 cm->fc->global_motion_types_prob, r, cm->allow_high_precision_mv);
Sarah Parkere5299862016-08-16 14:57:37 -07004557 /*
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004558 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4559 frame, cm->current_video_frame, cm->show_frame,
David Barkercf3d0b02016-11-10 10:14:49 +00004560 cm->global_motion[frame].wmmat[0],
4561 cm->global_motion[frame].wmmat[1],
4562 cm->global_motion[frame].wmmat[2],
4563 cm->global_motion[frame].wmmat[3]);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004564 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004565 }
Sarah Parkerf1783292017-04-05 11:55:27 -07004566 memcpy(cm->cur_frame->global_motion, cm->global_motion,
4567 TOTAL_REFS_PER_FRAME * sizeof(WarpedMotionParams));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004568}
4569#endif // CONFIG_GLOBAL_MOTION
4570
Yaowu Xuf883b422016-08-30 14:01:10 -07004571static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004572 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004573 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004574#if CONFIG_SUPERTX
4575 MACROBLOCKD *const xd = &pbi->mb;
4576#endif
4577 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07004578 aom_reader r;
Yaowu Xu8af861b2016-11-01 12:12:11 -07004579 int k, i;
Thomas Davies493623e2017-03-31 16:12:25 +01004580#if !CONFIG_EC_ADAPT || \
4581 (CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION || CONFIG_EXT_INTER)
Yaowu Xu8af861b2016-11-01 12:12:11 -07004582 int j;
4583#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004584
Alex Converse2cdf0d82016-12-13 13:53:09 -08004585#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08004586 r.window_size = 1 << cm->ans_window_size_log2;
Alex Converse2cdf0d82016-12-13 13:53:09 -08004587#endif
Alex Converse346440b2017-01-03 13:47:37 -08004588 if (aom_reader_init(&r, data, partition_size, pbi->decrypt_cb,
4589 pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07004590 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004591 "Failed to allocate bool decoder 0");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004592
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004593#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004594 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4595 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4596 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4597 av1_alloc_restoration_buffers(cm);
4598 decode_restoration(cm, &r);
4599 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004600#endif
4601
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004602#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -07004603 if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004604#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004605
Angie Chiang800df032017-03-22 11:14:12 -07004606#if CONFIG_LV_MAP
4607 av1_read_txb_probs(fc, cm->tx_mode, &r);
4608#else // CONFIG_LV_MAP
Yushin Cho77bba8d2016-11-04 16:36:56 -07004609#if !CONFIG_PVQ
Alex Conversea9598cd2017-02-03 14:18:05 -08004610#if !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004611 read_coef_probs(fc, cm->tx_mode, &r);
Angie Chiang800df032017-03-22 11:14:12 -07004612#endif // !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Angie Chiang7d7ead92017-03-22 10:35:51 -07004613#endif // !CONFIG_PVQ
Angie Chiang800df032017-03-22 11:14:12 -07004614#endif // CONFIG_LV_MAP
4615
Yaowu Xuc27fc142016-08-22 16:08:15 -07004616#if CONFIG_VAR_TX
4617 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004618 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
Yushin Cho77bba8d2016-11-04 16:36:56 -07004619#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004620 for (k = 0; k < SKIP_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004621 av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004622
Thomas Daviesd6ee8a82017-03-02 14:42:50 +00004623#if CONFIG_DELTA_Q && !CONFIG_EC_ADAPT
Fangwen Fu6160df22017-04-24 09:45:51 -07004624 if (cm->delta_q_present_flag) {
4625 for (k = 0; k < DELTA_Q_PROBS; ++k)
4626 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
4627 }
Fangwen Fu231fe422017-04-24 17:52:29 -07004628#if CONFIG_EXT_DELTA_Q
4629 if (cm->delta_lf_present_flag) {
4630 for (k = 0; k < DELTA_LF_PROBS; ++k)
4631 av1_diff_update_prob(&r, &fc->delta_lf_prob[k], ACCT_STR);
4632 }
4633#endif
Thomas Daviesf6936102016-09-05 16:51:31 +01004634#endif
4635
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004636#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004637 if (cm->seg.enabled && cm->seg.update_map) {
4638 if (cm->seg.temporal_update) {
4639 for (k = 0; k < PREDICTION_PROBS; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004640 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004641 }
4642 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004643 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004644 }
4645
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004646 for (j = 0; j < INTRA_MODES; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004647 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004648 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004649 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004650
4651#if CONFIG_EXT_PARTITION_TYPES
Alex Converse4e18d402017-03-14 15:36:38 -07004652 for (j = 0; j < PARTITION_PLOFFSET; ++j)
4653 for (i = 0; i < PARTITION_TYPES - 1; ++i)
4654 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
4655 for (; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004656 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004657 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004658#else
Alex Converse55c6bde2017-01-12 15:55:31 -08004659 for (j = 0; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004660 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004661 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004662#endif // CONFIG_EXT_PARTITION_TYPES
hui su9aa97492017-01-26 16:46:01 -08004663
Alex Converse55c6bde2017-01-12 15:55:31 -08004664#if CONFIG_UNPOISON_PARTITION_CTX
4665 for (; j < PARTITION_CONTEXTS_PRIMARY + PARTITION_BLOCK_SIZES; ++j)
4666 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_VERT], ACCT_STR);
4667 for (; j < PARTITION_CONTEXTS_PRIMARY + 2 * PARTITION_BLOCK_SIZES; ++j)
4668 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_HORZ], ACCT_STR);
4669#endif // CONFIG_UNPOISON_PARTITION_CTX
hui su9aa97492017-01-26 16:46:01 -08004670
4671#if CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004672 for (i = 0; i < INTRA_FILTERS + 1; ++i)
4673 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004674 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
hui su9aa97492017-01-26 16:46:01 -08004675#endif // CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
hui sub4e25d22017-03-09 15:32:30 -08004676#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004677
4678 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004679 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Nathan E. Egge10ba2be2016-11-16 09:44:26 -05004680#if CONFIG_EC_MULTISYMBOL
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00004681 av1_copy(cm->fc->kf_y_cdf, av1_kf_y_mode_cdf);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004682#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004683#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004684 for (k = 0; k < INTRA_MODES; k++)
Thomas Davies6519beb2016-10-19 14:46:07 +01004685 for (j = 0; j < INTRA_MODES; j++)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004686 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004687 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004688#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004689 } else {
4690#if !CONFIG_REF_MV
4691 nmv_context *const nmvc = &fc->nmvc;
4692#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004693 read_inter_mode_probs(fc, &r);
4694
4695#if CONFIG_EXT_INTER
4696 read_inter_compound_mode_probs(fc, &r);
4697 if (cm->reference_mode != COMPOUND_REFERENCE) {
4698 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4699 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004700 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004701 }
4702 }
4703 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4704 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004705 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004706 }
4707 for (i = 0; i < BLOCK_SIZES; i++) {
4708 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004709 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004710 }
4711 }
4712 }
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00004713#if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004714 if (cm->reference_mode != SINGLE_REFERENCE) {
4715 for (i = 0; i < BLOCK_SIZES; i++) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08004716 for (j = 0; j < COMPOUND_TYPES - 1; j++) {
4717 av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004718 }
4719 }
4720 }
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00004721#endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004722#endif // CONFIG_EXT_INTER
4723
Yue Chencb60b182016-10-13 15:18:22 -07004724#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004725 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
Yue Chencb60b182016-10-13 15:18:22 -07004726 for (j = 0; j < MOTION_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004727 av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004728 }
Yue Chencb60b182016-10-13 15:18:22 -07004729#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004730
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004731#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004732 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004733#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004734
4735 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004736 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004737
4738 if (cm->reference_mode != SINGLE_REFERENCE)
4739 setup_compound_reference_mode(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004740 read_frame_reference_mode_probs(cm, &r);
4741
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004742#if !CONFIG_EC_ADAPT
Nathan E. Egge5710c722016-09-08 10:01:16 -04004743 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004744 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004745 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
Nathan E. Egge5710c722016-09-08 10:01:16 -04004746 }
Thomas9ac55082016-09-23 18:04:17 +01004747#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004748
4749#if CONFIG_REF_MV
4750 for (i = 0; i < NMV_CONTEXTS; ++i)
4751 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
4752#else
4753 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
4754#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004755#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004756 read_ext_tx_probs(fc, &r);
Sarah Parkerb926f322017-04-24 16:19:48 -07004757#endif // EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004758#if CONFIG_SUPERTX
4759 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
4760#endif
4761#if CONFIG_GLOBAL_MOTION
4762 read_global_motion(cm, &r);
Thomas Davies6519beb2016-10-19 14:46:07 +01004763#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004764 }
Thomas Davies028b57f2017-02-22 16:42:11 +00004765#if CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Thomas Davies87aeeb82017-02-17 00:19:40 +00004766#if CONFIG_NEW_TOKENSET
4767 av1_coef_head_cdfs(fc);
4768#endif
4769 /* Make tail distribution from head */
Thomas Davies6519beb2016-10-19 14:46:07 +01004770 av1_coef_pareto_cdfs(fc);
David Barker599dfd02016-11-10 13:20:12 +00004771#if CONFIG_REF_MV
4772 for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
4773#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004774 av1_set_mv_cdfs(&fc->nmvc);
David Barker599dfd02016-11-10 13:20:12 +00004775#endif
Thomas Davies6519beb2016-10-19 14:46:07 +01004776 av1_set_mode_cdfs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00004777#endif // CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004778
Yaowu Xuf883b422016-08-30 14:01:10 -07004779 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004780}
4781
4782#ifdef NDEBUG
4783#define debug_check_frame_counts(cm) (void)0
4784#else // !NDEBUG
4785// Counts should only be incremented when frame_parallel_decoding_mode and
4786// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07004787static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004788 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07004789 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004790 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
4791 cm->error_resilient_mode);
4792 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
4793 sizeof(cm->counts.y_mode)));
4794 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
4795 sizeof(cm->counts.uv_mode)));
4796 assert(!memcmp(cm->counts.partition, zero_counts.partition,
4797 sizeof(cm->counts.partition)));
4798 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
4799 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
4800 sizeof(cm->counts.eob_branch)));
Thomas Daviesab780672017-02-01 12:07:29 +00004801#if CONFIG_EC_MULTISYMBOL
4802 assert(!memcmp(cm->counts.blockz_count, zero_counts.blockz_count,
4803 sizeof(cm->counts.blockz_count)));
4804#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004805 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
4806 sizeof(cm->counts.switchable_interp)));
4807 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
4808 sizeof(cm->counts.inter_mode)));
4809#if CONFIG_EXT_INTER
4810 assert(!memcmp(cm->counts.inter_compound_mode,
4811 zero_counts.inter_compound_mode,
4812 sizeof(cm->counts.inter_compound_mode)));
4813 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
4814 sizeof(cm->counts.interintra)));
4815 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
4816 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08004817 assert(!memcmp(cm->counts.compound_interinter,
4818 zero_counts.compound_interinter,
4819 sizeof(cm->counts.compound_interinter)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004820#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07004821#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
4822 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
4823 sizeof(cm->counts.motion_mode)));
4824#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004825 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
4826 sizeof(cm->counts.intra_inter)));
4827 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
4828 sizeof(cm->counts.comp_inter)));
4829 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
4830 sizeof(cm->counts.single_ref)));
4831 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
4832 sizeof(cm->counts.comp_ref)));
4833#if CONFIG_EXT_REFS
4834 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
4835 sizeof(cm->counts.comp_bwdref)));
4836#endif // CONFIG_EXT_REFS
4837 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
4838 sizeof(cm->counts.tx_size)));
4839 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
4840#if CONFIG_REF_MV
4841 assert(
4842 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
4843 assert(
4844 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
4845#else
4846 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
4847#endif
4848 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
4849 sizeof(cm->counts.inter_ext_tx)));
4850 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
4851 sizeof(cm->counts.intra_ext_tx)));
4852}
4853#endif // NDEBUG
4854
Yaowu Xuf883b422016-08-30 14:01:10 -07004855static struct aom_read_bit_buffer *init_read_bit_buffer(
4856 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
4857 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004858 rb->bit_offset = 0;
4859 rb->error_handler = error_handler;
4860 rb->error_handler_data = &pbi->common;
4861 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004862 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004863 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
4864 rb->bit_buffer = clear_data;
4865 rb->bit_buffer_end = clear_data + n;
4866 } else {
4867 rb->bit_buffer = data;
4868 rb->bit_buffer_end = data_end;
4869 }
4870 return rb;
4871}
4872
4873//------------------------------------------------------------------------------
4874
Yaowu Xuf883b422016-08-30 14:01:10 -07004875int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
4876 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
4877 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
4878 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004879}
4880
Yaowu Xuf883b422016-08-30 14:01:10 -07004881void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
4882 int *height) {
4883 *width = aom_rb_read_literal(rb, 16) + 1;
4884 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004885}
4886
Yaowu Xuf883b422016-08-30 14:01:10 -07004887BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
4888 int profile = aom_rb_read_bit(rb);
4889 profile |= aom_rb_read_bit(rb) << 1;
4890 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004891 return (BITSTREAM_PROFILE)profile;
4892}
4893
Thomas Davies028b57f2017-02-22 16:42:11 +00004894#if CONFIG_EC_ADAPT
Yaowu Xu4ff59b52017-04-24 12:41:56 -07004895static void make_update_tile_list_dec(AV1Decoder *pbi, int tile_rows,
4896 int tile_cols, FRAME_CONTEXT *ec_ctxs[]) {
Thomas Davies028b57f2017-02-22 16:42:11 +00004897 int i;
4898 for (i = 0; i < tile_rows * tile_cols; ++i)
4899 ec_ctxs[i] = &pbi->tile_data[i].tctx;
4900}
4901#endif
4902
Yaowu Xuf883b422016-08-30 14:01:10 -07004903void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
4904 const uint8_t *data_end, const uint8_t **p_data_end) {
4905 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004906 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07004907 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004908 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004909 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004910 size_t first_partition_size;
4911 YV12_BUFFER_CONFIG *new_fb;
4912
Yi Luof8e87b42017-04-14 17:20:27 -07004913#if CONFIG_ADAPT_SCAN
4914 av1_deliver_eob_threshold(cm, xd);
4915#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004916#if CONFIG_BITSTREAM_DEBUG
4917 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
4918#endif
4919
4920 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004921 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08004922
4923#if CONFIG_EXT_TILE
4924 // If cm->tile_encoding_mode == TILE_NORMAL, the independent decoding of a
4925 // single tile or a section of a frame is not allowed.
4926 if (!cm->tile_encoding_mode &&
4927 (pbi->dec_tile_row >= 0 || pbi->dec_tile_col >= 0)) {
4928 pbi->dec_tile_row = -1;
4929 pbi->dec_tile_col = -1;
4930 }
4931#endif // CONFIG_EXT_TILE
4932
Thomas Davies72712e62016-11-09 12:17:51 +00004933#if CONFIG_TILE_GROUPS
4934 pbi->first_partition_size = first_partition_size;
4935 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
4936#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004937 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004938 xd->cur_buf = new_fb;
4939#if CONFIG_GLOBAL_MOTION
Sarah Parkerf1783292017-04-05 11:55:27 -07004940 int i;
4941 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Sarah Parker4c10a3c2017-04-10 19:37:59 -07004942 set_default_warp_params(&cm->global_motion[i]);
4943 set_default_warp_params(&cm->cur_frame->global_motion[i]);
Sarah Parkerf1783292017-04-05 11:55:27 -07004944 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004945 xd->global_motion = cm->global_motion;
4946#endif // CONFIG_GLOBAL_MOTION
4947
4948 if (!first_partition_size) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004949 // showing a frame directly
4950 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004951 return;
4952 }
4953
Yaowu Xuf883b422016-08-30 14:01:10 -07004954 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004955 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07004956 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004957 "Truncated packet or corrupt header length");
4958
Jingning Han24e0a182016-11-20 22:34:12 -08004959#if CONFIG_REF_MV
Dengca8d24d2016-10-17 14:06:35 +08004960 cm->setup_mi(cm);
4961#endif
4962
Fangwen Fu8d164de2016-12-14 13:40:54 -08004963#if CONFIG_TEMPMV_SIGNALING
4964 if (cm->use_prev_frame_mvs) {
4965 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4966 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4967 assert(!cm->error_resilient_mode &&
4968 cm->width == last_fb_ref_buf->buf->y_width &&
4969 cm->height == last_fb_ref_buf->buf->y_height &&
4970 !cm->prev_frame->intra_only);
4971 }
4972#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004973 cm->use_prev_frame_mvs =
4974 !cm->error_resilient_mode && cm->width == cm->last_width &&
4975 cm->height == cm->last_height && !cm->last_intra_only &&
4976 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004977#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004978#if CONFIG_EXT_REFS
4979 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
4980 // show_exisiting_frame=1, nor can it take a frame not used as
4981 // a reference, it is probable that by the time it is being
4982 // referred to, the frame buffer it originally points to may
4983 // already get expired and have been reassigned to the current
4984 // newly coded frame. Hence, we need to check whether this is
4985 // the case, and if yes, we have 2 choices:
4986 // (1) Simply disable the use of previous frame mvs; or
4987 // (2) Have cm->prev_frame point to one reference frame buffer,
4988 // e.g. LAST_FRAME.
4989 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
4990 // Reassign the LAST_FRAME buffer to cm->prev_frame.
4991 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4992 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4993 }
4994#endif // CONFIG_EXT_REFS
4995
Yaowu Xuf883b422016-08-30 14:01:10 -07004996 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004997
4998 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
4999 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07005000 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005001 "Uninitialized entropy context.");
5002
Yaowu Xuf883b422016-08-30 14:01:10 -07005003 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005004
5005 xd->corrupted = 0;
5006 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
5007 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07005008 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005009 "Decode failed. Frame data header is corrupted.");
5010
5011 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005012 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005013 }
5014
5015 // If encoded in frame parallel mode, frame context is ready after decoding
5016 // the frame header.
5017 if (cm->frame_parallel_decode &&
5018 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005019 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005020 FrameWorkerData *const frame_worker_data = worker->data1;
5021 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
5022 context_updated = 1;
5023 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
5024 }
Yaowu Xuf883b422016-08-30 14:01:10 -07005025 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005026 pbi->cur_buf->row = -1;
5027 pbi->cur_buf->col = -1;
5028 frame_worker_data->frame_context_ready = 1;
5029 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07005030 av1_frameworker_signal_stats(worker);
5031 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005032 }
5033
hui su0d103572017-03-01 17:58:01 -08005034#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07005035 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005036 cm->coef_probs_update_idx = 0;
hui su0d103572017-03-01 17:58:01 -08005037#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07005038
Jingning Han52ece882017-04-07 14:58:25 -07005039 if (pbi->max_threads > 1 && !CONFIG_CB4X4 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005040#if CONFIG_EXT_TILE
Jingning Han52ece882017-04-07 14:58:25 -07005041 pbi->dec_tile_col < 0 && // Decoding all columns
Yaowu Xuc27fc142016-08-22 16:08:15 -07005042#endif // CONFIG_EXT_TILE
Jingning Han52ece882017-04-07 14:58:25 -07005043 cm->tile_cols > 1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005044 // Multi-threaded tile decoder
5045 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
5046 if (!xd->corrupted) {
5047 if (!cm->skip_loop_filter) {
5048 // If multiple threads are used to decode tiles, then we use those
5049 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07005050 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
5051 0, 0, pbi->tile_workers, pbi->num_tile_workers,
5052 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005053 }
5054 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005055 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005056 "Decode failed. Frame data is corrupted.");
5057 }
5058 } else {
5059 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
5060 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005061
Jean-Marc Valin01435132017-02-18 14:12:53 -05005062#if CONFIG_CDEF
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04005063 if (!cm->skip_loop_filter) {
Jean-Marc Valine9f77422017-03-22 17:09:51 -04005064 av1_cdef_frame(&pbi->cur_buf->buf, cm, &pbi->mb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02005065 }
Debargha Mukherjee00c54332017-03-03 15:44:17 -08005066#endif // CONFIG_CDEF
5067
5068#if CONFIG_LOOP_RESTORATION
5069 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
5070 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
5071 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
5072 av1_loop_restoration_frame(new_fb, cm, cm->rst_info, 7, 0, NULL);
5073 }
5074#endif // CONFIG_LOOP_RESTORATION
Thomas Daedef56859f2016-04-19 16:57:24 -07005075
Yaowu Xuc27fc142016-08-22 16:08:15 -07005076 if (!xd->corrupted) {
5077 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Thomas Davies028b57f2017-02-22 16:42:11 +00005078#if CONFIG_EC_ADAPT
Thomas Davies493623e2017-03-31 16:12:25 +01005079 FRAME_CONTEXT **tile_ctxs = aom_malloc(cm->tile_rows * cm->tile_cols *
5080 sizeof(&pbi->tile_data[0].tctx));
5081 aom_cdf_prob **cdf_ptrs =
5082 aom_malloc(cm->tile_rows * cm->tile_cols *
5083 sizeof(&pbi->tile_data[0].tctx.partition_cdf[0][0]));
Thomas Davies028b57f2017-02-22 16:42:11 +00005084 make_update_tile_list_dec(pbi, cm->tile_rows, cm->tile_cols, tile_ctxs);
5085#endif
5086
hui su0d103572017-03-01 17:58:01 -08005087#if CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuc27fc142016-08-22 16:08:15 -07005088 cm->partial_prob_update = 0;
hui su0d103572017-03-01 17:58:01 -08005089#endif // CONFIG_SUBFRAME_PROB_UPDATE
Yaowu Xuf883b422016-08-30 14:01:10 -07005090 av1_adapt_coef_probs(cm);
5091 av1_adapt_intra_frame_probs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00005092#if CONFIG_EC_ADAPT
Thomas Davies493623e2017-03-31 16:12:25 +01005093 av1_average_tile_coef_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005094 cm->tile_rows * cm->tile_cols);
Thomas Davies493623e2017-03-31 16:12:25 +01005095 av1_average_tile_intra_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005096 cm->tile_rows * cm->tile_cols);
Yushin Chob188ea12017-03-13 13:45:23 -07005097#if CONFIG_PVQ
5098 av1_average_tile_pvq_cdfs(pbi->common.fc, tile_ctxs,
5099 cm->tile_rows * cm->tile_cols);
5100#endif // CONFIG_PVQ
5101#endif // CONFIG_EC_ADAPT
hui suff0da2b2017-03-07 15:51:37 -08005102#if CONFIG_ADAPT_SCAN
5103 av1_adapt_scan_order(cm);
5104#endif // CONFIG_ADAPT_SCAN
Yaowu Xuc27fc142016-08-22 16:08:15 -07005105
5106 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005107 av1_adapt_inter_frame_probs(cm);
5108 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Thomas Davies028b57f2017-02-22 16:42:11 +00005109#if CONFIG_EC_ADAPT
5110 av1_average_tile_inter_cdfs(&pbi->common, pbi->common.fc, tile_ctxs,
Thomas Davies493623e2017-03-31 16:12:25 +01005111 cdf_ptrs, cm->tile_rows * cm->tile_cols);
5112 av1_average_tile_mv_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005113 cm->tile_rows * cm->tile_cols);
5114#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005115 }
Thomas Davies493623e2017-03-31 16:12:25 +01005116#if CONFIG_EC_ADAPT
5117 aom_free(tile_ctxs);
5118 aom_free(cdf_ptrs);
5119#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005120 } else {
5121 debug_check_frame_counts(cm);
5122 }
5123 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005124 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005125 "Decode failed. Frame data is corrupted.");
5126 }
5127
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05005128#if CONFIG_INSPECTION
5129 if (pbi->inspect_cb != NULL) {
5130 (*pbi->inspect_cb)(pbi, pbi->inspect_ctx);
5131 }
5132#endif
5133
Yaowu Xuc27fc142016-08-22 16:08:15 -07005134 // Non frame parallel update frame context here.
5135 if (!cm->error_resilient_mode && !context_updated)
5136 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
5137}