blob: 1ceae843f21197cf692ee6e96ad4e1b8d0bddc59 [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) {
Arild Fuldseth (arilfuld)38897302017-04-27 20:03:03 +020095#if CONFIG_LOWDELAY_COMPOUND // Normative in decoder
96 return !frame_is_intra_only(cm);
97#else
Yaowu Xuc27fc142016-08-22 16:08:15 -070098 int i;
99 if (frame_is_intra_only(cm)) return 0;
100 for (i = 1; i < INTER_REFS_PER_FRAME; ++i)
101 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
102
103 return 0;
Arild Fuldseth (arilfuld)38897302017-04-27 20:03:03 +0200104#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700105}
106
Yaowu Xuf883b422016-08-30 14:01:10 -0700107static void setup_compound_reference_mode(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700108#if CONFIG_EXT_REFS
109 cm->comp_fwd_ref[0] = LAST_FRAME;
110 cm->comp_fwd_ref[1] = LAST2_FRAME;
111 cm->comp_fwd_ref[2] = LAST3_FRAME;
112 cm->comp_fwd_ref[3] = GOLDEN_FRAME;
113
114 cm->comp_bwd_ref[0] = BWDREF_FRAME;
115 cm->comp_bwd_ref[1] = ALTREF_FRAME;
116#else
117 if (cm->ref_frame_sign_bias[LAST_FRAME] ==
118 cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
119 cm->comp_fixed_ref = ALTREF_FRAME;
120 cm->comp_var_ref[0] = LAST_FRAME;
121 cm->comp_var_ref[1] = GOLDEN_FRAME;
122 } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
123 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
124 cm->comp_fixed_ref = GOLDEN_FRAME;
125 cm->comp_var_ref[0] = LAST_FRAME;
126 cm->comp_var_ref[1] = ALTREF_FRAME;
127 } else {
128 cm->comp_fixed_ref = LAST_FRAME;
129 cm->comp_var_ref[0] = GOLDEN_FRAME;
130 cm->comp_var_ref[1] = ALTREF_FRAME;
131 }
132#endif // CONFIG_EXT_REFS
133}
134
135static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
136 return len != 0 && len <= (size_t)(end - start);
137}
138
Yaowu Xuf883b422016-08-30 14:01:10 -0700139static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
140 const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700141 return data > max ? max : data;
142}
143
Yue Cheneeacc4c2017-01-17 17:29:17 -0800144static TX_MODE read_tx_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
145 struct aom_read_bit_buffer *rb) {
146 int i, all_lossless = 1;
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800147#if CONFIG_TX64X64
Yue Cheneeacc4c2017-01-17 17:29:17 -0800148 TX_MODE tx_mode;
149#endif
150
151 if (cm->seg.enabled) {
152 for (i = 0; i < MAX_SEGMENTS; ++i) {
153 if (!xd->lossless[i]) {
154 all_lossless = 0;
155 break;
156 }
157 }
158 } else {
159 all_lossless = xd->lossless[0];
160 }
161
162 if (all_lossless) return ONLY_4X4;
163#if CONFIG_TX64X64
164 tx_mode = aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800165 if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
166 return tx_mode;
167#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700168 return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800169#endif // CONFIG_TX64X64
Yaowu Xuc27fc142016-08-22 16:08:15 -0700170}
171
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -0500172#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -0700173static void read_tx_size_probs(FRAME_CONTEXT *fc, aom_reader *r) {
174 int i, j, k;
175 for (i = 0; i < MAX_TX_DEPTH; ++i)
176 for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
177 for (k = 0; k < i + 1; ++k)
178 av1_diff_update_prob(r, &fc->tx_size_probs[i][j][k], ACCT_STR);
179}
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -0500180#endif
Yaowu Xuefc75352016-10-31 09:46:42 -0700181
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400182#if !CONFIG_EC_ADAPT
Yaowu Xuf883b422016-08-30 14:01:10 -0700183static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700184 int i, j;
Nathan E. Egge4947c292016-04-26 11:37:06 -0400185 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700186 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700187 av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i], ACCT_STR);
Nathan E. Egge4947c292016-04-26 11:37:06 -0400188 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700189}
Thomas Davies6519beb2016-10-19 14:46:07 +0100190#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700191
Yaowu Xuf883b422016-08-30 14:01:10 -0700192static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700193#if CONFIG_REF_MV
Yaowu Xu8af861b2016-11-01 12:12:11 -0700194 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700195 for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700196 av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700197 for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700198 av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700199 for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700200 av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700201 for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700202 av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700203#else
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400204#if !CONFIG_EC_ADAPT
Yaowu Xu8af861b2016-11-01 12:12:11 -0700205 int i, j;
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400206 for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700207 for (j = 0; j < INTER_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700208 av1_diff_update_prob(r, &fc->inter_mode_probs[i][j], ACCT_STR);
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400209 }
Yaowu Xu8af861b2016-11-01 12:12:11 -0700210#else
211 (void)fc;
212 (void)r;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700213#endif
Thomas Davies6519beb2016-10-19 14:46:07 +0100214#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700215}
216
217#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700218static void read_inter_compound_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700219 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -0700220 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700221 for (j = 0; j < INTER_MODE_CONTEXTS; ++j) {
222 for (i = 0; i < INTER_COMPOUND_MODES - 1; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700223 av1_diff_update_prob(r, &fc->inter_compound_mode_probs[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700224 }
225 }
226 }
227}
228#endif // CONFIG_EXT_INTER
Yaowu Xu17fd2f22016-11-17 18:23:28 -0800229#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100230#if !CONFIG_EXT_TX
231static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
232 int i, j, k;
233 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
234 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
235 for (j = 0; j < TX_TYPES; ++j) {
236 for (k = 0; k < TX_TYPES - 1; ++k)
237 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[i][j][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100238 }
239 }
240 }
241 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
242 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
243 for (k = 0; k < TX_TYPES - 1; ++k)
244 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[i][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100245 }
246 }
247}
248#endif
249#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700250
251static REFERENCE_MODE read_frame_reference_mode(
Yaowu Xuf883b422016-08-30 14:01:10 -0700252 const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700253 if (is_compound_reference_allowed(cm)) {
Zoe Liub05e5d12017-02-07 14:32:53 -0800254#if CONFIG_REF_ADAPT
255 return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
256#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700257 return aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700258 ? REFERENCE_MODE_SELECT
Yaowu Xuf883b422016-08-30 14:01:10 -0700259 : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
Zoe Liub05e5d12017-02-07 14:32:53 -0800260#endif // CONFIG_REF_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700261 } else {
262 return SINGLE_REFERENCE;
263 }
264}
265
Yaowu Xuf883b422016-08-30 14:01:10 -0700266static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700267 FRAME_CONTEXT *const fc = cm->fc;
268 int i, j;
269
270 if (cm->reference_mode == REFERENCE_MODE_SELECT)
271 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700272 av1_diff_update_prob(r, &fc->comp_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700273
274 if (cm->reference_mode != COMPOUND_REFERENCE) {
275 for (i = 0; i < REF_CONTEXTS; ++i) {
276 for (j = 0; j < (SINGLE_REFS - 1); ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700277 av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700278 }
279 }
280 }
281
282 if (cm->reference_mode != SINGLE_REFERENCE) {
283 for (i = 0; i < REF_CONTEXTS; ++i) {
284#if CONFIG_EXT_REFS
285 for (j = 0; j < (FWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700286 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700287 for (j = 0; j < (BWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700288 av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700289#else
290 for (j = 0; j < (COMP_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700291 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700292#endif // CONFIG_EXT_REFS
293 }
294 }
295}
296
Yaowu Xuf883b422016-08-30 14:01:10 -0700297static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700298 int i;
Michael Bebenita6048d052016-08-25 14:40:54 -0700299 for (i = 0; i < n; ++i) av1_diff_update_prob(r, &p[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700300}
301
Yaowu Xuf883b422016-08-30 14:01:10 -0700302static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Thomas9ac55082016-09-23 18:04:17 +0100303 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700304
Yaowu Xue86288d2016-10-31 15:56:38 -0700305#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100306 int j;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700307 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
308
Yaowu Xuc27fc142016-08-22 16:08:15 -0700309 for (i = 0; i < 2; ++i) {
310 nmv_component *const comp_ctx = &ctx->comps[i];
311 update_mv_probs(&comp_ctx->sign, 1, r);
312 update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
313 update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
314 update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
315 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700316 for (i = 0; i < 2; ++i) {
317 nmv_component *const comp_ctx = &ctx->comps[i];
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400318 for (j = 0; j < CLASS0_SIZE; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700319 update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400320 }
Nathan E. Eggeac499f32016-09-08 15:38:57 -0400321 update_mv_probs(comp_ctx->fp, MV_FP_SIZE - 1, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700322 }
Alex Converseaca9feb2016-10-10 11:08:10 -0700323#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700324
325 if (allow_hp) {
326 for (i = 0; i < 2; ++i) {
327 nmv_component *const comp_ctx = &ctx->comps[i];
328 update_mv_probs(&comp_ctx->class0_hp, 1, r);
329 update_mv_probs(&comp_ctx->hp, 1, r);
330 }
331 }
332}
333
334static void inverse_transform_block(MACROBLOCKD *xd, int plane,
335 const TX_TYPE tx_type,
336 const TX_SIZE tx_size, uint8_t *dst,
Jingning Han1be18782016-10-21 11:48:15 -0700337 int stride, int16_t scan_line, int eob) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700338 struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han1be18782016-10-21 11:48:15 -0700339 tran_low_t *const dqcoeff = pd->dqcoeff;
Angie Chiangd92d4bf2017-04-02 17:49:18 -0700340 av1_inverse_transform_block(xd, dqcoeff, tx_type, tx_size, dst, stride, eob);
Jingning Han1be18782016-10-21 11:48:15 -0700341 memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700342}
343
Yushin Cho77bba8d2016-11-04 16:36:56 -0700344#if CONFIG_PVQ
Thomas Daede6ff6af62017-02-03 16:29:24 -0800345static int av1_pvq_decode_helper(MACROBLOCKD *xd, tran_low_t *ref_coeff,
Thomas Daede1dbda1b2017-02-06 16:06:29 -0800346 tran_low_t *dqcoeff, int16_t *quant, int pli,
Yushin Cho77bba8d2016-11-04 16:36:56 -0700347 int bs, TX_TYPE tx_type, int xdec,
ltrudeaue1c09292017-01-20 15:42:13 -0500348 PVQ_SKIP_TYPE ac_dc_coded) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700349 unsigned int flags; // used for daala's stream analyzer.
350 int off;
351 const int is_keyframe = 0;
352 const int has_dc_skip = 1;
Jingning Hanff705452017-04-27 11:32:15 -0700353 int coeff_shift = 3 - av1_get_tx_scale(bs);
Thomas Daede6ff6af62017-02-03 16:29:24 -0800354 int hbd_downshift = 0;
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800355 int rounding_mask;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700356 // DC quantizer for PVQ
357 int pvq_dc_quant;
358 int lossless = (quant[0] == 0);
359 const int blk_size = tx_size_wide[bs];
360 int eob = 0;
361 int i;
Thomas Daede6ff6af62017-02-03 16:29:24 -0800362 od_dec_ctx *dec = &xd->daala_dec;
Yushin Cho70669122016-12-08 09:53:14 -1000363 int use_activity_masking = dec->use_activity_masking;
Thomas Daede1dbda1b2017-02-06 16:06:29 -0800364 DECLARE_ALIGNED(16, tran_low_t, dqcoeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
365 DECLARE_ALIGNED(16, tran_low_t, ref_coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700366
Yushin Cho48f84db2016-11-07 21:20:17 -0800367 od_coeff ref_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
368 od_coeff out_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700369
Thomas Daede6ff6af62017-02-03 16:29:24 -0800370 hbd_downshift = xd->bd - 8;
Thomas Daede6ff6af62017-02-03 16:29:24 -0800371
Yushin Cho77bba8d2016-11-04 16:36:56 -0700372 od_raster_to_coding_order(ref_coeff_pvq, blk_size, tx_type, ref_coeff,
373 blk_size);
374
Thomas Daede6ff6af62017-02-03 16:29:24 -0800375 assert(OD_COEFF_SHIFT >= 4);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700376 if (lossless)
377 pvq_dc_quant = 1;
378 else {
Yushin Cho70669122016-12-08 09:53:14 -1000379 if (use_activity_masking)
380 pvq_dc_quant = OD_MAXI(
Thomas Daede6ff6af62017-02-03 16:29:24 -0800381 1, (quant[0] << (OD_COEFF_SHIFT - 3) >> hbd_downshift) *
Yushin Cho70669122016-12-08 09:53:14 -1000382 dec->state.pvq_qm_q4[pli][od_qm_get_index(bs, 0)] >>
383 4);
384 else
Thomas Daede6ff6af62017-02-03 16:29:24 -0800385 pvq_dc_quant =
386 OD_MAXI(1, quant[0] << (OD_COEFF_SHIFT - 3) >> hbd_downshift);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700387 }
388
389 off = od_qm_offset(bs, xdec);
390
391 // copy int16 inputs to int32
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800392 for (i = 0; i < blk_size * blk_size; i++) {
Timothy B. Terriberry4e6a8f32017-02-24 11:00:59 -0800393 ref_int32[i] =
Thomas Daede6ff6af62017-02-03 16:29:24 -0800394 AOM_SIGNED_SHL(ref_coeff_pvq[i], OD_COEFF_SHIFT - coeff_shift) >>
395 hbd_downshift;
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800396 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700397
Thomas Daede6ff6af62017-02-03 16:29:24 -0800398 od_pvq_decode(dec, ref_int32, out_int32,
399 OD_MAXI(1, quant[1] << (OD_COEFF_SHIFT - 3) >> hbd_downshift),
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800400 pli, bs, OD_PVQ_BETA[use_activity_masking][pli][bs],
Timothy B. Terriberry44bb6d02017-04-07 15:44:14 -0700401 is_keyframe, &flags, ac_dc_coded, dec->state.qm + off,
402 dec->state.qm_inv + off);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700403
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800404 if (!has_dc_skip || out_int32[0]) {
405 out_int32[0] =
Yushin Choc49ef3a2017-03-13 17:27:25 -0700406 has_dc_skip + generic_decode(dec->r, &dec->state.adapt->model_dc[pli],
Timothy B. Terriberry44bb6d02017-04-07 15:44:14 -0700407 &dec->state.adapt->ex_dc[pli][bs][0], 2,
408 "dc:mag");
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800409 if (out_int32[0]) out_int32[0] *= aom_read_bit(dec->r, "dc:sign") ? -1 : 1;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700410 }
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800411 out_int32[0] = out_int32[0] * pvq_dc_quant + ref_int32[0];
412
413 // copy int32 result back to int16
414 assert(OD_COEFF_SHIFT > coeff_shift);
415 rounding_mask = (1 << (OD_COEFF_SHIFT - coeff_shift - 1)) - 1;
416 for (i = 0; i < blk_size * blk_size; i++) {
Thomas Daede6ff6af62017-02-03 16:29:24 -0800417 out_int32[i] = AOM_SIGNED_SHL(out_int32[i], hbd_downshift);
Timothy B. Terriberrye93acb22017-02-06 13:55:53 -0800418 dqcoeff_pvq[i] = (out_int32[i] + (out_int32[i] < 0) + rounding_mask) >>
419 (OD_COEFF_SHIFT - coeff_shift);
420 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700421
422 od_coding_order_to_raster(dqcoeff, blk_size, tx_type, dqcoeff_pvq, blk_size);
423
424 eob = blk_size * blk_size;
425
426 return eob;
427}
428
ltrudeaue1c09292017-01-20 15:42:13 -0500429static PVQ_SKIP_TYPE read_pvq_skip(AV1_COMMON *cm, MACROBLOCKD *const xd,
430 int plane, TX_SIZE tx_size) {
431 // decode ac/dc coded flag. bit0: DC coded, bit1 : AC coded
432 // NOTE : we don't use 5 symbols for luma here in aom codebase,
433 // since block partition is taken care of by aom.
434 // So, only AC/DC skip info is coded
Yushin Cho00779272017-02-21 10:38:16 -0800435 const int ac_dc_coded = aom_read_symbol(
ltrudeaue1c09292017-01-20 15:42:13 -0500436 xd->daala_dec.r,
Yushin Choc49ef3a2017-03-13 17:27:25 -0700437 xd->daala_dec.state.adapt->skip_cdf[2 * tx_size + (plane != 0)], 4,
Yushin Cho00779272017-02-21 10:38:16 -0800438 "skip");
ltrudeaue1c09292017-01-20 15:42:13 -0500439 if (ac_dc_coded < 0 || ac_dc_coded > 3) {
440 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
441 "Invalid PVQ Skip Type");
442 }
443 return ac_dc_coded;
444}
445
446static int av1_pvq_decode_helper2(AV1_COMMON *cm, MACROBLOCKD *const xd,
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800447 MB_MODE_INFO *const mbmi, int plane, int row,
448 int col, TX_SIZE tx_size, TX_TYPE tx_type) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700449 struct macroblockd_plane *const pd = &xd->plane[plane];
450 // transform block size in pixels
451 int tx_blk_size = tx_size_wide[tx_size];
452 int i, j;
453 tran_low_t *pvq_ref_coeff = pd->pvq_ref_coeff;
454 const int diff_stride = tx_blk_size;
455 int16_t *pred = pd->pred;
456 tran_low_t *const dqcoeff = pd->dqcoeff;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700457 uint8_t *dst;
458 int eob;
ltrudeaue1c09292017-01-20 15:42:13 -0500459 const PVQ_SKIP_TYPE ac_dc_coded = read_pvq_skip(cm, xd, plane, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700460
461 eob = 0;
462 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
463
Yushin Cho77bba8d2016-11-04 16:36:56 -0700464 if (ac_dc_coded) {
465 int xdec = pd->subsampling_x;
466 int seg_id = mbmi->segment_id;
467 int16_t *quant;
468 FWD_TXFM_PARAM fwd_txfm_param;
Yaowu Xufc1b2132016-11-07 15:16:15 -0800469 // ToDo(yaowu): correct this with optimal number from decoding process.
470 const int max_scan_line = tx_size_2d[tx_size];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200471#if CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -0800472 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
473 for (j = 0; j < tx_blk_size; j++)
474 for (i = 0; i < tx_blk_size; i++)
475 pred[diff_stride * j + i] =
476 CONVERT_TO_SHORTPTR(dst)[pd->dst.stride * j + i];
477 } else {
478#endif
479 for (j = 0; j < tx_blk_size; j++)
480 for (i = 0; i < tx_blk_size; i++)
481 pred[diff_stride * j + i] = dst[pd->dst.stride * j + i];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200482#if CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -0800483 }
484#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -0700485
486 fwd_txfm_param.tx_type = tx_type;
487 fwd_txfm_param.tx_size = tx_size;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700488 fwd_txfm_param.lossless = xd->lossless[seg_id];
489
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200490#if CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -0800491 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
492 fwd_txfm_param.bd = xd->bd;
hui suf11fb882017-03-27 14:56:33 -0700493 av1_highbd_fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
Thomas Daede6ff6af62017-02-03 16:29:24 -0800494 } else {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200495#endif // CONFIG_HIGHBITDEPTH
hui suf11fb882017-03-27 14:56:33 -0700496 av1_fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200497#if CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -0800498 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200499#endif // CONFIG_HIGHBITDEPTH
Yushin Cho77bba8d2016-11-04 16:36:56 -0700500
501 quant = &pd->seg_dequant[seg_id][0]; // aom's quantizer
502
Thomas Daede6ff6af62017-02-03 16:29:24 -0800503 eob = av1_pvq_decode_helper(xd, pvq_ref_coeff, dqcoeff, quant, plane,
504 tx_size, tx_type, xdec, ac_dc_coded);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700505
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800506 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
507 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700508 }
509
510 return eob;
511}
512#endif
513
Angie Chiang752ccce2017-04-09 13:41:13 -0700514static int get_block_idx(const MACROBLOCKD *xd, int plane, int row, int col) {
515 const int bsize = xd->mi[0]->mbmi.sb_type;
516 const struct macroblockd_plane *pd = &xd->plane[plane];
517#if CONFIG_CB4X4
518#if CONFIG_CHROMA_2X2
519 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
520#else
521 const BLOCK_SIZE plane_bsize =
522 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
523#endif // CONFIG_CHROMA_2X2
524#else
525 const BLOCK_SIZE plane_bsize =
526 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
527#endif
528 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
529 const TX_SIZE tx_size = get_tx_size(plane, xd);
530 const uint8_t txh_unit = tx_size_high_unit[tx_size];
531 return row * max_blocks_wide + col * txh_unit;
532}
533
Alex Converse8aca36d2017-01-31 12:33:15 -0800534static void predict_and_reconstruct_intra_block(
535 AV1_COMMON *cm, MACROBLOCKD *const xd, aom_reader *const r,
536 MB_MODE_INFO *const mbmi, int plane, int row, int col, TX_SIZE tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500537 PLANE_TYPE plane_type = get_plane_type(plane);
Angie Chiang752ccce2017-04-09 13:41:13 -0700538 const int block_idx = get_block_idx(xd, plane, row, col);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700539#if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -0700540 (void)r;
541#endif
Angie Chiang3d005e42017-04-02 16:31:35 -0700542 av1_predict_intra_block_facade(xd, plane, block_idx, col, row, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700543
544 if (!mbmi->skip) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700545#if !CONFIG_PVQ
Angie Chiang3d005e42017-04-02 16:31:35 -0700546 struct macroblockd_plane *const pd = &xd->plane[plane];
Angie Chiang133733c2017-03-17 12:50:20 -0700547#if CONFIG_LV_MAP
548 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700549 int eob;
550 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane,
551 pd->dqcoeff, &max_scan_line, &eob);
Angie Chiangb6d770c2017-04-14 16:27:57 -0700552 // tx_type will be read out in av1_read_coeffs_txb_facade
553 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Angie Chiang133733c2017-03-17 12:50:20 -0700554#else // CONFIG_LV_MAP
Angie Chiangb6d770c2017-04-14 16:27:57 -0700555 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Angie Chiangff6d8902016-10-21 11:02:09 -0700556 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 0);
Jingning Han1be18782016-10-21 11:48:15 -0700557 int16_t max_scan_line = 0;
558 const int eob =
Angie Chiang5c0568a2017-03-21 16:00:39 -0700559 av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
Jingning Han1be18782016-10-21 11:48:15 -0700560 tx_type, &max_scan_line, r, mbmi->segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700561#endif // CONFIG_LV_MAP
Angie Chiang3d005e42017-04-02 16:31:35 -0700562 if (eob) {
563 uint8_t *dst =
564 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Jingning Han1be18782016-10-21 11:48:15 -0700565 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
566 max_scan_line, eob);
Angie Chiang3d005e42017-04-02 16:31:35 -0700567 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700568#else
Angie Chiangb6d770c2017-04-14 16:27:57 -0700569 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
ltrudeaue1c09292017-01-20 15:42:13 -0500570 av1_pvq_decode_helper2(cm, xd, mbmi, plane, row, col, tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700571#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700572 }
Luc Trudeaue3980282017-04-25 23:17:21 -0400573#if CONFIG_CFL
574 if (plane == AOM_PLANE_Y) {
575 struct macroblockd_plane *const pd = &xd->plane[plane];
576 uint8_t *dst =
577 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
578 cfl_store(xd->cfl, dst, pd->dst.stride, row, col, tx_size);
579 }
580#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700581}
582
Jingning Handddb21f2017-02-28 14:44:05 -0800583#if CONFIG_VAR_TX && !CONFIG_COEF_INTERLEAVE
Angie Chiangff6d8902016-10-21 11:02:09 -0700584static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
585 aom_reader *r, MB_MODE_INFO *const mbmi,
Jingning Han8fd62b72016-10-21 12:55:54 -0700586 int plane, BLOCK_SIZE plane_bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700587 int blk_row, int blk_col, TX_SIZE tx_size,
588 int *eob_total) {
589 const struct macroblockd_plane *const pd = &xd->plane[plane];
590 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
591 const int tx_row = blk_row >> (1 - pd->subsampling_y);
592 const int tx_col = blk_col >> (1 - pd->subsampling_x);
593 const TX_SIZE plane_tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700594 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700595 : mbmi->inter_tx_size[tx_row][tx_col];
Jingning Han5f614262016-10-27 14:27:43 -0700596 // Scale to match transform block unit.
Jingning Hanf64062f2016-11-02 16:22:18 -0700597 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
598 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700599
600 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
601
602 if (tx_size == plane_tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500603 PLANE_TYPE plane_type = get_plane_type(plane);
Angie Chiang752ccce2017-04-09 13:41:13 -0700604 int block_idx = get_block_idx(xd, plane, blk_row, blk_col);
Angie Chiang133733c2017-03-17 12:50:20 -0700605#if CONFIG_LV_MAP
606 (void)segment_id;
607 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700608 int eob;
609 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane,
610 pd->dqcoeff, &max_scan_line, &eob);
Angie Chiangb6d770c2017-04-14 16:27:57 -0700611 // tx_type will be read out in av1_read_coeffs_txb_facade
612 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiang133733c2017-03-17 12:50:20 -0700613#else // CONFIG_LV_MAP
Angie Chiangb6d770c2017-04-14 16:27:57 -0700614 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiangff6d8902016-10-21 11:02:09 -0700615 const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700616 int16_t max_scan_line = 0;
Angie Chiang5c0568a2017-03-21 16:00:39 -0700617 const int eob = av1_decode_block_tokens(
618 cm, xd, plane, sc, blk_col, blk_row, plane_tx_size, tx_type,
619 &max_scan_line, r, mbmi->segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700620#endif // CONFIG_LV_MAP
Jingning Han9ca05b72017-01-03 14:41:36 -0800621 inverse_transform_block(xd, plane, tx_type, plane_tx_size,
622 &pd->dst.buf[(blk_row * pd->dst.stride + blk_col)
623 << tx_size_wide_log2[0]],
624 pd->dst.stride, max_scan_line, eob);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700625 *eob_total += eob;
626 } else {
Jingning Hanf64062f2016-11-02 16:22:18 -0700627 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
628 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700629 int i;
630
631 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700632
633 for (i = 0; i < 4; ++i) {
Jingning Han5f614262016-10-27 14:27:43 -0700634 const int offsetr = blk_row + (i >> 1) * bsl;
635 const int offsetc = blk_col + (i & 0x01) * bsl;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700636
637 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
638
Jingning Han8fd62b72016-10-21 12:55:54 -0700639 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
Jingning Hanf64062f2016-11-02 16:22:18 -0700640 offsetc, sub_txs, eob_total);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700641 }
642 }
643}
644#endif // CONFIG_VAR_TX
645
Jingning Handddb21f2017-02-28 14:44:05 -0800646#if !CONFIG_VAR_TX || CONFIG_SUPERTX || CONFIG_COEF_INTERLEAVE || \
Jingning Hanfe45b212016-11-22 10:30:23 -0800647 (!CONFIG_VAR_TX && CONFIG_EXT_TX && CONFIG_RECT_TX)
Angie Chiangff6d8902016-10-21 11:02:09 -0700648static int reconstruct_inter_block(AV1_COMMON *cm, MACROBLOCKD *const xd,
Alex Converse8aca36d2017-01-31 12:33:15 -0800649 aom_reader *const r, int segment_id,
650 int plane, int row, int col,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700651 TX_SIZE tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500652 PLANE_TYPE plane_type = get_plane_type(plane);
Angie Chiang752ccce2017-04-09 13:41:13 -0700653 int block_idx = get_block_idx(xd, plane, row, col);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700654#if CONFIG_PVQ
655 int eob;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700656 (void)r;
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800657 (void)segment_id;
658#else
659 struct macroblockd_plane *const pd = &xd->plane[plane];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700660#endif
661
662#if !CONFIG_PVQ
Angie Chiang133733c2017-03-17 12:50:20 -0700663#if CONFIG_LV_MAP
664 (void)segment_id;
Jingning Han1be18782016-10-21 11:48:15 -0700665 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700666 int eob;
667 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane, pd->dqcoeff,
668 &max_scan_line, &eob);
Angie Chiangb6d770c2017-04-14 16:27:57 -0700669 // tx_type will be read out in av1_read_coeffs_txb_facade
670 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Angie Chiang133733c2017-03-17 12:50:20 -0700671#else // CONFIG_LV_MAP
672 int16_t max_scan_line = 0;
Angie Chiangb6d770c2017-04-14 16:27:57 -0700673 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Angie Chiang133733c2017-03-17 12:50:20 -0700674 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700675 const int eob =
Angie Chiang5c0568a2017-03-21 16:00:39 -0700676 av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
677 tx_type, &max_scan_line, r, segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700678#endif // CONFIG_LV_MAP
Jingning Hanca14dda2016-12-09 09:36:00 -0800679 uint8_t *dst =
680 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Jingning Han1be18782016-10-21 11:48:15 -0700681 if (eob)
Jingning Hanca14dda2016-12-09 09:36:00 -0800682 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
683 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700684#else
Angie Chiangb6d770c2017-04-14 16:27:57 -0700685 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
ltrudeaue1c09292017-01-20 15:42:13 -0500686 eob = av1_pvq_decode_helper2(cm, xd, &xd->mi[0]->mbmi, plane, row, col,
687 tx_size, tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700688#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700689 return eob;
690}
691#endif // !CONFIG_VAR_TX || CONFIG_SUPER_TX
692
Angie Chiang44701f22017-02-27 10:36:44 -0800693static void set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
694 BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
695 int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700696 const int offset = mi_row * cm->mi_stride + mi_col;
697 int x, y;
698 const TileInfo *const tile = &xd->tile;
699
700 xd->mi = cm->mi_grid_visible + offset;
701 xd->mi[0] = &cm->mi[offset];
702 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
703 // passing bsize from decode_partition().
704 xd->mi[0]->mbmi.sb_type = bsize;
Angie Chiang394c3372016-11-03 11:13:15 -0700705#if CONFIG_RD_DEBUG
706 xd->mi[0]->mbmi.mi_row = mi_row;
707 xd->mi[0]->mbmi.mi_col = mi_col;
708#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700709 for (y = 0; y < y_mis; ++y)
Jingning Han97d85482016-07-15 11:06:05 -0700710 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700711
Jingning Hanfaad0e12016-12-07 10:54:57 -0800712 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700713 set_skip_context(xd, mi_row, mi_col);
714
715#if CONFIG_VAR_TX
716 xd->max_tx_size = max_txsize_lookup[bsize];
717#endif
718
Urvang Joshi359dc2b2017-04-27 15:41:47 -0700719 // Distance of Mb to the various image edges. These are specified to 8th pel
720 // as they are always compared to values that are in 1/8th pel units
721 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800722#if CONFIG_DEPENDENT_HORZTILES
Urvang Joshi359dc2b2017-04-27 15:41:47 -0700723 cm->dependent_horz_tiles,
724#endif // CONFIG_DEPENDENT_HORZTILES
725 cm->mi_rows, cm->mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700726
Jingning Han91d9a792017-04-18 12:01:52 -0700727 av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row,
728 mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700729}
730
731#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700732static MB_MODE_INFO *set_offsets_extend(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700733 MACROBLOCKD *const xd,
734 const TileInfo *const tile,
735 BLOCK_SIZE bsize_pred, int mi_row_pred,
736 int mi_col_pred, int mi_row_ori,
737 int mi_col_ori) {
738 // Used in supertx
739 // (mi_row_ori, mi_col_ori): location for mv
740 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
Jingning Han93531242016-12-20 11:54:36 -0800741 const int bw = mi_size_wide[bsize_pred];
742 const int bh = mi_size_high[bsize_pred];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700743 const int offset = mi_row_ori * cm->mi_stride + mi_col_ori;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700744 xd->mi = cm->mi_grid_visible + offset;
745 xd->mi[0] = cm->mi + offset;
Urvang Joshi359dc2b2017-04-27 15:41:47 -0700746 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800747#if CONFIG_DEPENDENT_HORZTILES
Urvang Joshi359dc2b2017-04-27 15:41:47 -0700748 cm->dependent_horz_tiles,
749#endif // CONFIG_DEPENDENT_HORZTILES
750 cm->mi_rows, cm->mi_cols);
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
Urvang Joshi359dc2b2017-04-27 15:41:47 -0700774 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800775#if CONFIG_DEPENDENT_HORZTILES
Urvang Joshi359dc2b2017-04-27 15:41:47 -0700776 cm->dependent_horz_tiles,
777#endif // CONFIG_DEPENDENT_HORZTILES
778 cm->mi_rows, cm->mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700779 return &xd->mi[0]->mbmi;
780}
Angie Chiang7fcfee42017-02-24 15:51:03 -0800781#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700782
Yaowu Xuf883b422016-08-30 14:01:10 -0700783static void set_offsets_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700784 const TileInfo *const tile, BLOCK_SIZE bsize,
785 int mi_row, int mi_col) {
Jingning Han93531242016-12-20 11:54:36 -0800786 const int bw = mi_size_wide[bsize];
787 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700788 const int offset = mi_row * cm->mi_stride + mi_col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700789
790 xd->mi = cm->mi_grid_visible + offset;
791 xd->mi[0] = cm->mi + offset;
792
Jingning Hanfaad0e12016-12-07 10:54:57 -0800793 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700794
Urvang Joshi359dc2b2017-04-27 15:41:47 -0700795 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800796#if CONFIG_DEPENDENT_HORZTILES
Urvang Joshi359dc2b2017-04-27 15:41:47 -0700797 cm->dependent_horz_tiles,
798#endif // CONFIG_DEPENDENT_HORZTILES
799 cm->mi_rows, cm->mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800
Jingning Han91d9a792017-04-18 12:01:52 -0700801 av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row,
802 mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700803}
804
Yaowu Xuf883b422016-08-30 14:01:10 -0700805static void set_param_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700806 BLOCK_SIZE bsize, int mi_row, int mi_col,
807 int txfm, int skip) {
Jingning Han93531242016-12-20 11:54:36 -0800808 const int bw = mi_size_wide[bsize];
809 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -0700810 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
811 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700812 const int offset = mi_row * cm->mi_stride + mi_col;
813 int x, y;
814
815 xd->mi = cm->mi_grid_visible + offset;
816 xd->mi[0] = cm->mi + offset;
817
818 for (y = 0; y < y_mis; ++y)
819 for (x = 0; x < x_mis; ++x) {
820 xd->mi[y * cm->mi_stride + x]->mbmi.skip = skip;
821 xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
822 }
823#if CONFIG_VAR_TX
824 xd->above_txfm_context = cm->above_txfm_context + mi_col;
825 xd->left_txfm_context =
826 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
Yaowu Xu52a17632016-11-17 15:48:21 -0800827 set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bw, bh, skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700828#endif
829}
830
Yaowu Xuf883b422016-08-30 14:01:10 -0700831static void set_ref(AV1_COMMON *const cm, MACROBLOCKD *const xd, int idx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700832 int mi_row, int mi_col) {
833 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
834 RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
835 xd->block_refs[idx] = ref_buffer;
Yaowu Xuf883b422016-08-30 14:01:10 -0700836 if (!av1_is_valid_scale(&ref_buffer->sf))
837 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700838 "Invalid scale factors");
Yaowu Xuf883b422016-08-30 14:01:10 -0700839 av1_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
840 &ref_buffer->sf);
Angie Chiangd0916d92017-03-10 17:54:18 -0800841 aom_merge_corrupted_flag(&xd->corrupted, ref_buffer->buf->corrupted);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700842}
843
844static void dec_predict_b_extend(
Yaowu Xuf883b422016-08-30 14:01:10 -0700845 AV1Decoder *const pbi, MACROBLOCKD *const xd, const TileInfo *const tile,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700846 int block, int mi_row_ori, int mi_col_ori, int mi_row_pred, int mi_col_pred,
847 int mi_row_top, int mi_col_top, uint8_t *dst_buf[3], int dst_stride[3],
848 BLOCK_SIZE bsize_top, BLOCK_SIZE bsize_pred, int b_sub8x8, int bextend) {
849 // Used in supertx
850 // (mi_row_ori, mi_col_ori): location for mv
851 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
852 // (mi_row_top, mi_col_top, bsize_top): region of the top partition size
853 // block: sub location of sub8x8 blocks
854 // b_sub8x8: 1: ori is sub8x8; 0: ori is not sub8x8
855 // bextend: 1: region to predict is an extension of ori; 0: not
856 int r = (mi_row_pred - mi_row_top) * MI_SIZE;
857 int c = (mi_col_pred - mi_col_top) * MI_SIZE;
Jingning Han93531242016-12-20 11:54:36 -0800858 const int mi_width_top = mi_size_wide[bsize_top];
859 const int mi_height_top = mi_size_high[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700860 MB_MODE_INFO *mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700861 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700862
863 if (mi_row_pred < mi_row_top || mi_col_pred < mi_col_top ||
864 mi_row_pred >= mi_row_top + mi_height_top ||
865 mi_col_pred >= mi_col_top + mi_width_top || mi_row_pred >= cm->mi_rows ||
866 mi_col_pred >= cm->mi_cols)
867 return;
868
869 mbmi = set_offsets_extend(cm, xd, tile, bsize_pred, mi_row_pred, mi_col_pred,
870 mi_row_ori, mi_col_ori);
871 set_ref(cm, xd, 0, mi_row_pred, mi_col_pred);
872 if (has_second_ref(&xd->mi[0]->mbmi))
873 set_ref(cm, xd, 1, mi_row_pred, mi_col_pred);
874
Jingning Han2511c662016-12-22 11:57:34 -0800875 if (!bextend) mbmi->tx_size = max_txsize_lookup[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700876
877 xd->plane[0].dst.stride = dst_stride[0];
878 xd->plane[1].dst.stride = dst_stride[1];
879 xd->plane[2].dst.stride = dst_stride[2];
880 xd->plane[0].dst.buf = dst_buf[0] +
881 (r >> xd->plane[0].subsampling_y) * dst_stride[0] +
882 (c >> xd->plane[0].subsampling_x);
883 xd->plane[1].dst.buf = dst_buf[1] +
884 (r >> xd->plane[1].subsampling_y) * dst_stride[1] +
885 (c >> xd->plane[1].subsampling_x);
886 xd->plane[2].dst.buf = dst_buf[2] +
887 (r >> xd->plane[2].subsampling_y) * dst_stride[2] +
888 (c >> xd->plane[2].subsampling_x);
889
890 if (!b_sub8x8)
Yaowu Xuf883b422016-08-30 14:01:10 -0700891 av1_build_inter_predictors_sb_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700892#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700893 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700894#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700895 mi_row_pred, mi_col_pred, bsize_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700896 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700897 av1_build_inter_predictors_sb_sub8x8_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700898#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700899 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700900#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700901 mi_row_pred, mi_col_pred,
902 bsize_pred, block);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700903}
904
Yaowu Xuf883b422016-08-30 14:01:10 -0700905static void dec_extend_dir(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700906 const TileInfo *const tile, int block,
907 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
908 int mi_col, int mi_row_top, int mi_col_top,
909 uint8_t *dst_buf[3], int dst_stride[3], int dir) {
910 // dir: 0-lower, 1-upper, 2-left, 3-right
911 // 4-lowerleft, 5-upperleft, 6-lowerright, 7-upperright
Jingning Han93531242016-12-20 11:54:36 -0800912 const int mi_width = mi_size_wide[bsize];
913 const int mi_height = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700914 int xss = xd->plane[1].subsampling_x;
915 int yss = xd->plane[1].subsampling_y;
Jingning Hanfeb517c2016-12-21 16:02:07 -0800916#if CONFIG_CB4X4
917 const int unify_bsize = 1;
918#else
919 const int unify_bsize = 0;
920#endif
921 int b_sub8x8 = (bsize < BLOCK_8X8) && !unify_bsize ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700922 BLOCK_SIZE extend_bsize;
Jingning Han24f24a52016-12-27 10:13:28 -0800923 int mi_row_pred, mi_col_pred;
924
925 int wide_unit, high_unit;
926 int i, j;
927 int ext_offset = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700928
929 if (dir == 0 || dir == 1) {
Jingning Han93531242016-12-20 11:54:36 -0800930 extend_bsize =
931 (mi_width == mi_size_wide[BLOCK_8X8] || bsize < BLOCK_8X8 || xss < yss)
932 ? BLOCK_8X8
933 : BLOCK_16X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800934#if CONFIG_CB4X4
935 if (bsize < BLOCK_8X8) {
936 extend_bsize = BLOCK_4X4;
937 ext_offset = mi_size_wide[BLOCK_8X8];
938 }
939#endif
940
941 wide_unit = mi_size_wide[extend_bsize];
942 high_unit = mi_size_high[extend_bsize];
943
944 mi_row_pred = mi_row + ((dir == 0) ? mi_height : -(mi_height + ext_offset));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700945 mi_col_pred = mi_col;
946
Jingning Han24f24a52016-12-27 10:13:28 -0800947 for (j = 0; j < mi_height + ext_offset; j += high_unit)
948 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
949 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
950 mi_row_pred + j, mi_col_pred + i, mi_row_top,
951 mi_col_top, dst_buf, dst_stride, top_bsize,
952 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700953 } else if (dir == 2 || dir == 3) {
Jingning Han5b7706a2016-12-21 09:55:10 -0800954 extend_bsize =
955 (mi_height == mi_size_high[BLOCK_8X8] || bsize < BLOCK_8X8 || yss < xss)
956 ? BLOCK_8X8
957 : BLOCK_8X16;
Jingning Han24f24a52016-12-27 10:13:28 -0800958#if CONFIG_CB4X4
959 if (bsize < BLOCK_8X8) {
960 extend_bsize = BLOCK_4X4;
961 ext_offset = mi_size_wide[BLOCK_8X8];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700962 }
Jingning Han24f24a52016-12-27 10:13:28 -0800963#endif
964
965 wide_unit = mi_size_wide[extend_bsize];
966 high_unit = mi_size_high[extend_bsize];
967
968 mi_row_pred = mi_row;
969 mi_col_pred = mi_col + ((dir == 3) ? mi_width : -(mi_width + ext_offset));
970
971 for (j = 0; j < mi_height + ext_offset; j += high_unit)
972 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
973 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
974 mi_row_pred + j, mi_col_pred + i, mi_row_top,
975 mi_col_top, dst_buf, dst_stride, top_bsize,
976 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700977 } else {
978 extend_bsize = BLOCK_8X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800979#if CONFIG_CB4X4
980 if (bsize < BLOCK_8X8) {
981 extend_bsize = BLOCK_4X4;
982 ext_offset = mi_size_wide[BLOCK_8X8];
983 }
984#endif
985 wide_unit = mi_size_wide[extend_bsize];
986 high_unit = mi_size_high[extend_bsize];
987
Jingning Han5b7706a2016-12-21 09:55:10 -0800988 mi_row_pred = mi_row + ((dir == 4 || dir == 6) ? mi_height
Jingning Han24f24a52016-12-27 10:13:28 -0800989 : -(mi_height + ext_offset));
Jingning Han5b7706a2016-12-21 09:55:10 -0800990 mi_col_pred =
Jingning Han24f24a52016-12-27 10:13:28 -0800991 mi_col + ((dir == 6 || dir == 7) ? mi_width : -(mi_width + ext_offset));
992
993 for (j = 0; j < mi_height + ext_offset; j += high_unit)
994 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
995 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
996 mi_row_pred + j, mi_col_pred + i, mi_row_top,
997 mi_col_top, dst_buf, dst_stride, top_bsize,
998 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700999 }
1000}
1001
Yaowu Xuf883b422016-08-30 14:01:10 -07001002static void dec_extend_all(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001003 const TileInfo *const tile, int block,
1004 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
1005 int mi_col, int mi_row_top, int mi_col_top,
1006 uint8_t *dst_buf[3], int dst_stride[3]) {
Sarah Parkerfb9e6652017-04-25 16:32:06 -07001007 for (int i = 0; i < 8; ++i) {
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, i);
1010 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001011}
1012
Yaowu Xuf883b422016-08-30 14:01:10 -07001013static void dec_predict_sb_complex(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001014 const TileInfo *const tile, int mi_row,
1015 int mi_col, int mi_row_top, int mi_col_top,
1016 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize,
1017 uint8_t *dst_buf[3], int dst_stride[3]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001018 const AV1_COMMON *const cm = &pbi->common;
Jingning Han5b7706a2016-12-21 09:55:10 -08001019 const int hbs = mi_size_wide[bsize] / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001020 const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
1021 const BLOCK_SIZE subsize = get_subsize(bsize, partition);
1022#if CONFIG_EXT_PARTITION_TYPES
1023 const BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1024#endif
1025 int i;
1026 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1027 uint8_t *dst_buf1[3], *dst_buf2[3], *dst_buf3[3];
Jingning Hanfeb517c2016-12-21 16:02:07 -08001028#if CONFIG_CB4X4
1029 const int unify_bsize = 1;
1030#else
1031 const int unify_bsize = 0;
1032#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001033
1034 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1035 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1036 DECLARE_ALIGNED(16, uint8_t, tmp_buf3[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
1037 int dst_stride1[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1038 int dst_stride2[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1039 int dst_stride3[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
1040
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001041#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001042 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1043 int len = sizeof(uint16_t);
1044 dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
1045 dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_TX_SQUARE * len);
1046 dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_TX_SQUARE * len);
1047 dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
1048 dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_TX_SQUARE * len);
1049 dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_TX_SQUARE * len);
1050 dst_buf3[0] = CONVERT_TO_BYTEPTR(tmp_buf3);
1051 dst_buf3[1] = CONVERT_TO_BYTEPTR(tmp_buf3 + MAX_TX_SQUARE * len);
1052 dst_buf3[2] = CONVERT_TO_BYTEPTR(tmp_buf3 + 2 * MAX_TX_SQUARE * len);
1053 } else {
1054#endif
1055 dst_buf1[0] = tmp_buf1;
1056 dst_buf1[1] = tmp_buf1 + MAX_TX_SQUARE;
1057 dst_buf1[2] = tmp_buf1 + 2 * MAX_TX_SQUARE;
1058 dst_buf2[0] = tmp_buf2;
1059 dst_buf2[1] = tmp_buf2 + MAX_TX_SQUARE;
1060 dst_buf2[2] = tmp_buf2 + 2 * MAX_TX_SQUARE;
1061 dst_buf3[0] = tmp_buf3;
1062 dst_buf3[1] = tmp_buf3 + MAX_TX_SQUARE;
1063 dst_buf3[2] = tmp_buf3 + 2 * MAX_TX_SQUARE;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001064#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001065 }
1066#endif
1067
1068 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1069
1070 xd->mi = cm->mi_grid_visible + mi_offset;
1071 xd->mi[0] = cm->mi + mi_offset;
1072
1073 for (i = 0; i < MAX_MB_PLANE; i++) {
1074 xd->plane[i].dst.buf = dst_buf[i];
1075 xd->plane[i].dst.stride = dst_stride[i];
1076 }
1077
1078 switch (partition) {
1079 case PARTITION_NONE:
1080 assert(bsize < top_bsize);
1081 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1082 mi_row_top, mi_col_top, dst_buf, dst_stride,
1083 top_bsize, bsize, 0, 0);
1084 dec_extend_all(pbi, xd, tile, 0, bsize, top_bsize, mi_row, mi_col,
1085 mi_row_top, mi_col_top, dst_buf, dst_stride);
1086 break;
1087 case PARTITION_HORZ:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001088 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001089 // For sub8x8, predict in 8x8 unit
1090 // First half
1091 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1092 mi_row_top, mi_col_top, dst_buf, dst_stride,
1093 top_bsize, BLOCK_8X8, 1, 0);
1094 if (bsize < top_bsize)
1095 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1096 mi_row_top, mi_col_top, dst_buf, dst_stride);
1097
1098 // Second half
1099 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1100 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1101 top_bsize, BLOCK_8X8, 1, 1);
1102 if (bsize < top_bsize)
1103 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1104 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1105
1106 // weighted average to smooth the boundary
1107 xd->plane[0].dst.buf = dst_buf[0];
1108 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001109 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001110 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1111 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1112 0);
1113 } else {
1114 // First half
1115 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1116 mi_row_top, mi_col_top, dst_buf, dst_stride,
1117 top_bsize, subsize, 0, 0);
1118 if (bsize < top_bsize)
1119 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1120 mi_row_top, mi_col_top, dst_buf, dst_stride);
1121 else
1122 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1123 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1124
1125 if (mi_row + hbs < cm->mi_rows) {
1126 // Second half
1127 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col,
1128 mi_row + hbs, mi_col, mi_row_top, mi_col_top,
1129 dst_buf1, dst_stride1, top_bsize, subsize, 0, 0);
1130 if (bsize < top_bsize)
1131 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1132 mi_col, mi_row_top, mi_col_top, dst_buf1,
1133 dst_stride1);
1134 else
1135 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1136 mi_col, mi_row_top, mi_col_top, dst_buf1,
1137 dst_stride1, 1);
1138
1139 // weighted average to smooth the boundary
1140 for (i = 0; i < MAX_MB_PLANE; i++) {
1141 xd->plane[i].dst.buf = dst_buf[i];
1142 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001143 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001144 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1145 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1146 PARTITION_HORZ, i);
1147 }
1148 }
1149 }
1150 break;
1151 case PARTITION_VERT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001152 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001153 // First half
1154 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1155 mi_row_top, mi_col_top, dst_buf, dst_stride,
1156 top_bsize, BLOCK_8X8, 1, 0);
1157 if (bsize < top_bsize)
1158 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1159 mi_row_top, mi_col_top, dst_buf, dst_stride);
1160
1161 // Second half
1162 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1163 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1164 top_bsize, BLOCK_8X8, 1, 1);
1165 if (bsize < top_bsize)
1166 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1167 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1168
1169 // Smooth
1170 xd->plane[0].dst.buf = dst_buf[0];
1171 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001172 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001173 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1174 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1175 0);
1176 } else {
1177 // First half
1178 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1179 mi_row_top, mi_col_top, dst_buf, dst_stride,
1180 top_bsize, subsize, 0, 0);
1181 if (bsize < top_bsize)
1182 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1183 mi_row_top, mi_col_top, dst_buf, dst_stride);
1184 else
1185 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1186 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1187
1188 // Second half
1189 if (mi_col + hbs < cm->mi_cols) {
1190 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1191 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1192 dst_stride1, top_bsize, subsize, 0, 0);
1193 if (bsize < top_bsize)
1194 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1195 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1196 dst_stride1);
1197 else
1198 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1199 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1200 dst_stride1, 2);
1201
1202 // Smooth
1203 for (i = 0; i < MAX_MB_PLANE; i++) {
1204 xd->plane[i].dst.buf = dst_buf[i];
1205 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001206 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001207 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1208 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1209 PARTITION_VERT, i);
1210 }
1211 }
1212 }
1213 break;
1214 case PARTITION_SPLIT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001215 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001216 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1217 mi_row_top, mi_col_top, dst_buf, dst_stride,
1218 top_bsize, BLOCK_8X8, 1, 0);
1219 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1220 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1221 top_bsize, BLOCK_8X8, 1, 1);
1222 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1223 mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1224 top_bsize, BLOCK_8X8, 1, 1);
1225 dec_predict_b_extend(pbi, xd, tile, 3, mi_row, mi_col, mi_row, mi_col,
1226 mi_row_top, mi_col_top, dst_buf3, dst_stride3,
1227 top_bsize, BLOCK_8X8, 1, 1);
1228 if (bsize < top_bsize) {
1229 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1230 mi_row_top, mi_col_top, dst_buf, dst_stride);
1231 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1232 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1233 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1234 mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1235 dec_extend_all(pbi, xd, tile, 3, subsize, top_bsize, mi_row, mi_col,
1236 mi_row_top, mi_col_top, dst_buf3, dst_stride3);
1237 }
1238 } else {
1239 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row_top,
1240 mi_col_top, subsize, top_bsize, dst_buf,
1241 dst_stride);
1242 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1243 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col + hbs,
1244 mi_row_top, mi_col_top, subsize, top_bsize,
1245 dst_buf1, dst_stride1);
1246 if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols)
1247 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col,
1248 mi_row_top, mi_col_top, subsize, top_bsize,
1249 dst_buf2, dst_stride2);
1250 if (mi_row + hbs < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1251 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col + hbs,
1252 mi_row_top, mi_col_top, subsize, top_bsize,
1253 dst_buf3, dst_stride3);
1254 }
1255 for (i = 0; i < MAX_MB_PLANE; i++) {
Jingning Han9e0976a2016-12-27 17:52:42 -08001256#if !CONFIG_CB4X4
Jingning Han24f24a52016-12-27 10:13:28 -08001257 if (bsize == BLOCK_8X8 && i != 0)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001258 continue; // Skip <4x4 chroma smoothing
Jingning Han9e0976a2016-12-27 17:52:42 -08001259#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001260 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001261 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001262 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1263 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1264 PARTITION_VERT, i);
1265 if (mi_row + hbs < cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001266 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001267 xd, dst_buf2[i], dst_stride2[i], dst_buf3[i], dst_stride3[i],
1268 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1269 PARTITION_VERT, i);
Yaowu Xuf883b422016-08-30 14:01:10 -07001270 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001271 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1272 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1273 PARTITION_HORZ, i);
1274 }
1275 } else if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001276 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001277 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1278 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1279 PARTITION_HORZ, i);
1280 }
1281 }
1282 break;
1283#if CONFIG_EXT_PARTITION_TYPES
1284 case PARTITION_HORZ_A:
1285 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1286 mi_row_top, mi_col_top, dst_buf, dst_stride,
1287 top_bsize, bsize2, 0, 0);
1288 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1289 mi_row_top, mi_col_top, dst_buf, dst_stride);
1290
1291 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1292 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1293 dst_stride1, top_bsize, bsize2, 0, 0);
1294 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1295 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1296
1297 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1298 mi_col, mi_row_top, mi_col_top, dst_buf2,
1299 dst_stride2, top_bsize, subsize, 0, 0);
1300 if (bsize < top_bsize)
1301 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1302 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1303 else
1304 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1305 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1306 1);
1307
1308 for (i = 0; i < MAX_MB_PLANE; i++) {
1309 xd->plane[i].dst.buf = dst_buf[i];
1310 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001311 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001312 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1313 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1314 i);
1315 }
1316 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001317 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001318 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1319 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1320 i);
1321 }
1322 break;
1323 case PARTITION_VERT_A:
1324
1325 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1326 mi_row_top, mi_col_top, dst_buf, dst_stride,
1327 top_bsize, bsize2, 0, 0);
1328 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1329 mi_row_top, mi_col_top, dst_buf, dst_stride);
1330
1331 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1332 mi_col, mi_row_top, mi_col_top, dst_buf1,
1333 dst_stride1, top_bsize, bsize2, 0, 0);
1334 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1335 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1336
1337 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1338 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1339 dst_stride2, top_bsize, subsize, 0, 0);
1340 if (bsize < top_bsize)
1341 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1342 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1343 dst_stride2);
1344 else
1345 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1346 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1347 dst_stride2, 2);
1348
1349 for (i = 0; i < MAX_MB_PLANE; i++) {
1350 xd->plane[i].dst.buf = dst_buf[i];
1351 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001352 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001353 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1354 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1355 i);
1356 }
1357 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001358 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001359 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1360 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1361 i);
1362 }
1363 break;
1364 case PARTITION_HORZ_B:
1365 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1366 mi_row_top, mi_col_top, dst_buf, dst_stride,
1367 top_bsize, subsize, 0, 0);
1368 if (bsize < top_bsize)
1369 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1370 mi_row_top, mi_col_top, dst_buf, dst_stride);
1371 else
1372 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1373 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1374
1375 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1376 mi_col, mi_row_top, mi_col_top, dst_buf1,
1377 dst_stride1, top_bsize, bsize2, 0, 0);
1378 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1379 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1380
1381 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1382 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1383 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1384 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1385 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1386 dst_stride2);
1387
1388 for (i = 0; i < MAX_MB_PLANE; i++) {
1389 xd->plane[i].dst.buf = dst_buf1[i];
1390 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001391 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001392 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1393 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1394 PARTITION_VERT, i);
1395 }
1396 for (i = 0; i < MAX_MB_PLANE; i++) {
1397 xd->plane[i].dst.buf = dst_buf[i];
1398 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001399 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001400 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1401 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1402 i);
1403 }
1404 break;
1405 case PARTITION_VERT_B:
1406 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1407 mi_row_top, mi_col_top, dst_buf, dst_stride,
1408 top_bsize, subsize, 0, 0);
1409 if (bsize < top_bsize)
1410 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1411 mi_row_top, mi_col_top, dst_buf, dst_stride);
1412 else
1413 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1414 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1415
1416 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1417 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1418 dst_stride1, top_bsize, bsize2, 0, 0);
1419 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1420 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1421
1422 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1423 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1424 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1425 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1426 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1427 dst_stride2);
1428
1429 for (i = 0; i < MAX_MB_PLANE; i++) {
1430 xd->plane[i].dst.buf = dst_buf1[i];
1431 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001432 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001433 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1434 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1435 PARTITION_HORZ, i);
1436 }
1437 for (i = 0; i < MAX_MB_PLANE; i++) {
1438 xd->plane[i].dst.buf = dst_buf[i];
1439 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001440 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001441 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1442 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1443 i);
1444 }
1445 break;
1446#endif // CONFIG_EXT_PARTITION_TYPES
1447 default: assert(0);
1448 }
1449}
1450
Yaowu Xu4ff59b52017-04-24 12:41:56 -07001451static void set_segment_id_supertx(const AV1_COMMON *const cm, int mi_row,
1452 int mi_col, BLOCK_SIZE bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001453 const struct segmentation *seg = &cm->seg;
Jingning Han5b7706a2016-12-21 09:55:10 -08001454 const int miw = AOMMIN(mi_size_wide[bsize], cm->mi_cols - mi_col);
1455 const int mih = AOMMIN(mi_size_high[bsize], cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001456 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1457 MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
1458 int r, c;
1459 int seg_id_supertx = MAX_SEGMENTS;
1460
1461 if (!seg->enabled) {
1462 seg_id_supertx = 0;
1463 } else {
1464 // Find the minimum segment_id
1465 for (r = 0; r < mih; r++)
1466 for (c = 0; c < miw; c++)
1467 seg_id_supertx =
Yaowu Xuf883b422016-08-30 14:01:10 -07001468 AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001469 assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);
1470 }
1471
1472 // Assign the the segment_id back to segment_id_supertx
1473 for (r = 0; r < mih; r++)
1474 for (c = 0; c < miw; c++)
1475 mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
1476}
1477#endif // CONFIG_SUPERTX
1478
Yue Chen64550b62017-01-12 12:18:22 -08001479static void decode_mbmi_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001480#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001481 int supertx_enabled,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001482#endif // CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001483 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001484#if CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001485 PARTITION_TYPE partition,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001486#endif // CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001487 BLOCK_SIZE bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001488 AV1_COMMON *const cm = &pbi->common;
Jingning Han85dc03f2016-12-06 16:03:10 -08001489 const int bw = mi_size_wide[bsize];
1490 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -07001491 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1492 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Nathan E. Eggeebbd4792016-10-05 19:30:15 -04001493
Michael Bebenita6048d052016-08-25 14:40:54 -07001494#if CONFIG_ACCOUNTING
1495 aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
1496#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001497#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001498 if (supertx_enabled) {
Yue Chen64550b62017-01-12 12:18:22 -08001499 set_mb_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001500 } else {
Yue Chen64550b62017-01-12 12:18:22 -08001501 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001502 }
1503#if CONFIG_EXT_PARTITION_TYPES
1504 xd->mi[0]->mbmi.partition = partition;
1505#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001506 av1_read_mode_info(pbi, xd, supertx_enabled, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001507#else
Yue Chen64550b62017-01-12 12:18:22 -08001508 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001509#if CONFIG_EXT_PARTITION_TYPES
1510 xd->mi[0]->mbmi.partition = partition;
1511#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001512 av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001513#endif // CONFIG_SUPERTX
1514
1515 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1516 const BLOCK_SIZE uv_subsize =
1517 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1518 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -07001519 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001520 "Invalid block size.");
1521 }
1522
1523#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001524 xd->mi[0]->mbmi.segment_id_supertx = MAX_SEGMENTS;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001525#endif // CONFIG_SUPERTX
1526
Angie Chiangd0916d92017-03-10 17:54:18 -08001527 int reader_corrupted_flag = aom_reader_has_error(r);
1528 aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
Yue Chen64550b62017-01-12 12:18:22 -08001529}
1530
1531static void decode_token_and_recon_block(AV1Decoder *const pbi,
1532 MACROBLOCKD *const xd, int mi_row,
1533 int mi_col, aom_reader *r,
1534 BLOCK_SIZE bsize) {
1535 AV1_COMMON *const cm = &pbi->common;
1536 const int bw = mi_size_wide[bsize];
1537 const int bh = mi_size_high[bsize];
1538 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1539 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yue Chen64550b62017-01-12 12:18:22 -08001540
Angie Chiang44701f22017-02-27 10:36:44 -08001541 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
1542 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Yue Chen19e7aa82016-11-30 14:05:39 -08001543
Arild Fuldseth07441162016-08-15 15:07:52 +02001544#if CONFIG_DELTA_Q
1545 if (cm->delta_q_present_flag) {
1546 int i;
1547 for (i = 0; i < MAX_SEGMENTS; i++) {
Fangwen Fu6160df22017-04-24 09:45:51 -07001548#if CONFIG_EXT_DELTA_Q
1549 xd->plane[0].seg_dequant[i][0] =
1550 av1_dc_quant(av1_get_qindex(&cm->seg, i, xd->current_qindex),
1551 cm->y_dc_delta_q, cm->bit_depth);
1552 xd->plane[0].seg_dequant[i][1] = av1_ac_quant(
1553 av1_get_qindex(&cm->seg, i, xd->current_qindex), 0, cm->bit_depth);
1554 xd->plane[1].seg_dequant[i][0] =
1555 av1_dc_quant(av1_get_qindex(&cm->seg, i, xd->current_qindex),
1556 cm->uv_dc_delta_q, cm->bit_depth);
1557 xd->plane[1].seg_dequant[i][1] =
1558 av1_ac_quant(av1_get_qindex(&cm->seg, i, xd->current_qindex),
1559 cm->uv_ac_delta_q, cm->bit_depth);
1560 xd->plane[2].seg_dequant[i][0] =
1561 av1_dc_quant(av1_get_qindex(&cm->seg, i, xd->current_qindex),
1562 cm->uv_dc_delta_q, cm->bit_depth);
1563 xd->plane[2].seg_dequant[i][1] =
1564 av1_ac_quant(av1_get_qindex(&cm->seg, i, xd->current_qindex),
1565 cm->uv_ac_delta_q, cm->bit_depth);
1566#else
Arild Fuldseth07441162016-08-15 15:07:52 +02001567 xd->plane[0].seg_dequant[i][0] =
1568 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
1569 xd->plane[0].seg_dequant[i][1] =
1570 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
1571 xd->plane[1].seg_dequant[i][0] =
1572 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1573 xd->plane[1].seg_dequant[i][1] =
1574 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1575 xd->plane[2].seg_dequant[i][0] =
1576 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1577 xd->plane[2].seg_dequant[i][1] =
1578 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
Fangwen Fu6160df22017-04-24 09:45:51 -07001579#endif
Arild Fuldseth07441162016-08-15 15:07:52 +02001580 }
1581 }
1582#endif
1583
Jingning Han41bb3392016-12-14 10:46:48 -08001584#if CONFIG_CB4X4
1585 if (mbmi->skip) reset_skip_context(xd, bsize);
1586#else
Jingning Hand39cc722016-12-02 14:03:26 -08001587 if (mbmi->skip) reset_skip_context(xd, AOMMAX(BLOCK_8X8, bsize));
Jingning Han41bb3392016-12-14 10:46:48 -08001588#endif
Jingning Hand39cc722016-12-02 14:03:26 -08001589
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001590#if CONFIG_COEF_INTERLEAVE
1591 {
1592 const struct macroblockd_plane *const pd_y = &xd->plane[0];
1593 const struct macroblockd_plane *const pd_c = &xd->plane[1];
1594 const TX_SIZE tx_log2_y = mbmi->tx_size;
1595 const TX_SIZE tx_log2_c = get_uv_tx_size(mbmi, pd_c);
1596 const int tx_sz_y = (1 << tx_log2_y);
1597 const int tx_sz_c = (1 << tx_log2_c);
1598 const int num_4x4_w_y = pd_y->n4_w;
1599 const int num_4x4_h_y = pd_y->n4_h;
1600 const int num_4x4_w_c = pd_c->n4_w;
1601 const int num_4x4_h_c = pd_c->n4_h;
1602 const int max_4x4_w_y = get_max_4x4_size(num_4x4_w_y, xd->mb_to_right_edge,
1603 pd_y->subsampling_x);
1604 const int max_4x4_h_y = get_max_4x4_size(num_4x4_h_y, xd->mb_to_bottom_edge,
1605 pd_y->subsampling_y);
1606 const int max_4x4_w_c = get_max_4x4_size(num_4x4_w_c, xd->mb_to_right_edge,
1607 pd_c->subsampling_x);
1608 const int max_4x4_h_c = get_max_4x4_size(num_4x4_h_c, xd->mb_to_bottom_edge,
1609 pd_c->subsampling_y);
1610
1611 // The max_4x4_w/h may be smaller than tx_sz under some corner cases,
1612 // i.e. when the SB is splitted by tile boundaries.
1613 const int tu_num_w_y = (max_4x4_w_y + tx_sz_y - 1) / tx_sz_y;
1614 const int tu_num_h_y = (max_4x4_h_y + tx_sz_y - 1) / tx_sz_y;
1615 const int tu_num_w_c = (max_4x4_w_c + tx_sz_c - 1) / tx_sz_c;
1616 const int tu_num_h_c = (max_4x4_h_c + tx_sz_c - 1) / tx_sz_c;
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001617 const int tu_num_c = tu_num_w_c * tu_num_h_c;
1618
1619 if (!is_inter_block(mbmi)) {
1620 int tu_idx_c = 0;
1621 int row_y, col_y, row_c, col_c;
1622 int plane;
1623
1624#if CONFIG_PALETTE
1625 for (plane = 0; plane <= 1; ++plane) {
1626 if (mbmi->palette_mode_info.palette_size[plane])
1627 av1_decode_palette_tokens(xd, plane, r);
1628 }
1629#endif
1630
1631 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1632 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1633 // luma
1634 predict_and_reconstruct_intra_block(
1635 cm, xd, r, mbmi, 0, row_y * tx_sz_y, col_y * tx_sz_y, tx_log2_y);
1636 // chroma
1637 if (tu_idx_c < tu_num_c) {
1638 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1639 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1640 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c,
1641 col_c, tx_log2_c);
1642 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c,
1643 col_c, tx_log2_c);
1644 tu_idx_c++;
1645 }
1646 }
1647 }
1648
1649 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1650 while (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, col_c,
1654 tx_log2_c);
1655 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c, col_c,
1656 tx_log2_c);
1657 tu_idx_c++;
1658 }
1659 } else {
1660 // Prediction
David Barkerac37fa32016-12-02 12:30:21 +00001661 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001662 AOMMAX(bsize, BLOCK_8X8));
1663
1664 // Reconstruction
1665 if (!mbmi->skip) {
1666 int eobtotal = 0;
1667 int tu_idx_c = 0;
1668 int row_y, col_y, row_c, col_c;
1669
1670 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1671 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1672 // luma
1673 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 0,
1674 row_y * tx_sz_y,
1675 col_y * tx_sz_y, tx_log2_y);
1676 // chroma
1677 if (tu_idx_c < tu_num_c) {
1678 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1679 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1680 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1681 1, row_c, col_c, tx_log2_c);
1682 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1683 2, row_c, col_c, tx_log2_c);
1684 tu_idx_c++;
1685 }
1686 }
1687 }
1688
1689 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1690 while (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, 1,
1694 row_c, col_c, tx_log2_c);
1695 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 2,
1696 row_c, col_c, tx_log2_c);
1697 tu_idx_c++;
1698 }
1699
Alex Converse64d7ef62017-03-22 18:09:16 -07001700 // TODO(CONFIG_COEF_INTERLEAVE owners): bring eob == 0 corner case
1701 // into line with the defaut configuration
1702 if (bsize >= BLOCK_8X8 && eobtotal == 0) mbmi->skip = 1;
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001703 }
1704 }
1705 }
Angie Chiang133733c2017-03-17 12:50:20 -07001706#else // CONFIG_COEF_INTERLEAVE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001707 if (!is_inter_block(mbmi)) {
1708 int plane;
Fangwen Fub3be9262017-03-06 15:34:28 -08001709#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001710 for (plane = 0; plane <= 1; ++plane) {
1711 if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xuf883b422016-08-30 14:01:10 -07001712 av1_decode_palette_tokens(xd, plane, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001713 }
Fangwen Fub3be9262017-03-06 15:34:28 -08001714#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001715 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1716 const struct macroblockd_plane *const pd = &xd->plane[plane];
Angie Chiang7fcfee42017-02-24 15:51:03 -08001717 const TX_SIZE tx_size = get_tx_size(plane, xd);
Jingning Han2d64f122016-10-21 12:44:29 -07001718 const int stepr = tx_size_high_unit[tx_size];
1719 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han41bb3392016-12-14 10:46:48 -08001720#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001721#if CONFIG_CHROMA_2X2
1722 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1723#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001724 const BLOCK_SIZE plane_bsize =
1725 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001726#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001727#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001728 const BLOCK_SIZE plane_bsize =
1729 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001730#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001731 int row, col;
Jingning Hanbafee8d2016-12-02 10:25:03 -08001732 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1733 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001734#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001735 if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
1736 pd->subsampling_y))
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001737 continue;
1738#endif
1739
Yaowu Xuc27fc142016-08-22 16:08:15 -07001740 for (row = 0; row < max_blocks_high; row += stepr)
1741 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001742 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane, row, col,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001743 tx_size);
1744 }
1745 } else {
Yue Chen9ab6d712017-01-12 15:50:46 -08001746 int ref;
1747
1748 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
1749 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
Alex Converse28744302017-04-13 14:46:22 -07001750 if (frame < LAST_FRAME) {
1751#if CONFIG_INTRABC
1752 assert(is_intrabc_block(mbmi));
1753 assert(frame == INTRA_FRAME);
1754 assert(ref == 0);
1755#else
1756 assert(0);
1757#endif // CONFIG_INTRABC
1758 } else {
1759 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
Yue Chen9ab6d712017-01-12 15:50:46 -08001760
Alex Converse28744302017-04-13 14:46:22 -07001761 xd->block_refs[ref] = ref_buf;
1762 if ((!av1_is_valid_scale(&ref_buf->sf)))
1763 aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
1764 "Reference frame has invalid dimensions");
1765 av1_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
1766 &ref_buf->sf);
1767 }
Yue Chen9ab6d712017-01-12 15:50:46 -08001768 }
Yue Chen69f18e12016-09-08 14:48:15 -07001769
Jingning Han41bb3392016-12-14 10:46:48 -08001770#if CONFIG_CB4X4
Sarah Parker4c10a3c2017-04-10 19:37:59 -07001771 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL, bsize);
Jingning Han41bb3392016-12-14 10:46:48 -08001772#else
1773 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
1774 AOMMAX(bsize, BLOCK_8X8));
1775#endif
Sarah Parker4c10a3c2017-04-10 19:37:59 -07001776
Yue Chencb60b182016-10-13 15:18:22 -07001777#if CONFIG_MOTION_VAR
1778 if (mbmi->motion_mode == OBMC_CAUSAL) {
Yue Chenf27b1602017-01-13 11:11:43 -08001779#if CONFIG_NCOBMC
1780 av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
1781#else
Yue Chen894fcce2016-10-21 16:50:52 -07001782 av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yue Chenf27b1602017-01-13 11:11:43 -08001783#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001784 }
Yue Chencb60b182016-10-13 15:18:22 -07001785#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001786
1787 // Reconstruction
1788 if (!mbmi->skip) {
1789 int eobtotal = 0;
1790 int plane;
1791
1792 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1793 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han41bb3392016-12-14 10:46:48 -08001794#if CONFIG_CB4X4
Jingning Han31b6a4f2017-02-23 11:05:53 -08001795#if CONFIG_CHROMA_2X2
1796 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1797#else
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001798 const BLOCK_SIZE plane_bsize =
1799 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Han31b6a4f2017-02-23 11:05:53 -08001800#endif // CONFIG_CHROMA_2X2
Jingning Han41bb3392016-12-14 10:46:48 -08001801#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001802 const BLOCK_SIZE plane_bsize =
1803 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001804#endif
Jingning Hanbafee8d2016-12-02 10:25:03 -08001805 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1806 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001807 int row, col;
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001808
1809#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001810 if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
1811 pd->subsampling_y))
Jingning Hanc20dc8e2017-02-17 15:37:28 -08001812 continue;
1813#endif
1814
Yaowu Xuc27fc142016-08-22 16:08:15 -07001815#if CONFIG_VAR_TX
Sarah Parker106b3cb2017-04-21 12:13:37 -07001816 const TX_SIZE max_tx_size = get_vartx_max_txsize(mbmi, plane_bsize);
Jingning Hanf64062f2016-11-02 16:22:18 -07001817 const int bh_var_tx = tx_size_high_unit[max_tx_size];
1818 const int bw_var_tx = tx_size_wide_unit[max_tx_size];
Jingning Hanbafee8d2016-12-02 10:25:03 -08001819 for (row = 0; row < max_blocks_high; row += bh_var_tx)
1820 for (col = 0; col < max_blocks_wide; col += bw_var_tx)
Jingning Hanfe45b212016-11-22 10:30:23 -08001821 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, row, col,
1822 max_tx_size, &eobtotal);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001823#else
Angie Chiang7fcfee42017-02-24 15:51:03 -08001824 const TX_SIZE tx_size = get_tx_size(plane, xd);
Jingning Han2d64f122016-10-21 12:44:29 -07001825 const int stepr = tx_size_high_unit[tx_size];
1826 const int stepc = tx_size_wide_unit[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001827 for (row = 0; row < max_blocks_high; row += stepr)
1828 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001829 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1830 plane, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001831#endif
1832 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001833 }
1834 }
Angie Chiang133733c2017-03-17 12:50:20 -07001835#endif // CONFIG_COEF_INTERLEAVE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001836
Angie Chiangd0916d92017-03-10 17:54:18 -08001837 int reader_corrupted_flag = aom_reader_has_error(r);
1838 aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001839}
1840
Yue Chen9ab6d712017-01-12 15:50:46 -08001841#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
1842static void detoken_and_recon_sb(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1843 int mi_row, int mi_col, aom_reader *r,
1844 BLOCK_SIZE bsize) {
1845 AV1_COMMON *const cm = &pbi->common;
1846 const int hbs = mi_size_wide[bsize] >> 1;
1847#if CONFIG_CB4X4
1848 const int unify_bsize = 1;
1849#else
1850 const int unify_bsize = 0;
1851#endif
1852#if CONFIG_EXT_PARTITION_TYPES
1853 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1854#endif
1855 PARTITION_TYPE partition;
1856 BLOCK_SIZE subsize;
1857 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1858 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1859
1860 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1861
1862 partition = get_partition(cm, mi_row, mi_col, bsize);
1863 subsize = subsize_lookup[partition][bsize];
1864
1865 if (!hbs && !unify_bsize) {
1866 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1867 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1868 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1869 } else {
1870 switch (partition) {
1871 case PARTITION_NONE:
1872 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
1873 break;
1874 case PARTITION_HORZ:
1875 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1876 if (has_rows)
1877 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r,
1878 subsize);
1879 break;
1880 case PARTITION_VERT:
1881 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1882 if (has_cols)
1883 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r,
1884 subsize);
1885 break;
1886 case PARTITION_SPLIT:
1887 detoken_and_recon_sb(pbi, xd, mi_row, mi_col, r, subsize);
1888 detoken_and_recon_sb(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1889 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1890 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col + hbs, r, subsize);
1891 break;
1892#if CONFIG_EXT_PARTITION_TYPES
1893 case PARTITION_HORZ_A:
1894 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1895 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1896 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, subsize);
1897 break;
1898 case PARTITION_HORZ_B:
1899 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1900 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1901 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1902 bsize2);
1903 break;
1904 case PARTITION_VERT_A:
1905 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
1906 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
1907 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, subsize);
1908 break;
1909 case PARTITION_VERT_B:
1910 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
1911 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
1912 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
1913 bsize2);
1914 break;
1915#endif
1916 default: assert(0 && "Invalid partition type");
1917 }
1918 }
1919}
1920#endif
1921
Yue Chen64550b62017-01-12 12:18:22 -08001922static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1923#if CONFIG_SUPERTX
1924 int supertx_enabled,
1925#endif // CONFIG_SUPERTX
1926 int mi_row, int mi_col, aom_reader *r,
1927#if CONFIG_EXT_PARTITION_TYPES
1928 PARTITION_TYPE partition,
1929#endif // CONFIG_EXT_PARTITION_TYPES
1930 BLOCK_SIZE bsize) {
1931 decode_mbmi_block(pbi, xd,
1932#if CONFIG_SUPERTX
1933 supertx_enabled,
1934#endif
1935 mi_row, mi_col, r,
1936#if CONFIG_EXT_PARTITION_TYPES
1937 partition,
1938#endif
1939 bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001940#if !(CONFIG_MOTION_VAR && CONFIG_NCOBMC)
Yue Chen64550b62017-01-12 12:18:22 -08001941#if CONFIG_SUPERTX
1942 if (!supertx_enabled)
1943#endif // CONFIG_SUPERTX
1944 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -08001945#endif
Yue Chen64550b62017-01-12 12:18:22 -08001946}
1947
Yaowu Xuf883b422016-08-30 14:01:10 -07001948static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
1949 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001950 int has_rows, int has_cols,
Jingning Han1beb0102016-12-07 11:08:30 -08001951 BLOCK_SIZE bsize) {
Alex Converse55c6bde2017-01-12 15:55:31 -08001952#if CONFIG_UNPOISON_PARTITION_CTX
1953 const int ctx =
1954 partition_plane_context(xd, mi_row, mi_col, has_rows, has_cols, bsize);
Alex Converse2b9d19d2017-04-03 11:11:17 -07001955 const aom_prob *const probs =
1956 ctx < PARTITION_CONTEXTS ? cm->fc->partition_prob[ctx] : NULL;
1957 FRAME_COUNTS *const counts = ctx < PARTITION_CONTEXTS ? xd->counts : NULL;
Alex Converse55c6bde2017-01-12 15:55:31 -08001958#else
Jingning Han1beb0102016-12-07 11:08:30 -08001959 const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
Yaowu Xuf883b422016-08-30 14:01:10 -07001960 const aom_prob *const probs = cm->fc->partition_prob[ctx];
Alex Converse55c6bde2017-01-12 15:55:31 -08001961 FRAME_COUNTS *const counts = xd->counts;
1962#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001963 PARTITION_TYPE p;
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001964#if CONFIG_EC_ADAPT
1965 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1966 (void)cm;
1967#elif CONFIG_EC_MULTISYMBOL
1968 FRAME_CONTEXT *ec_ctx = cm->fc;
1969#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001970
Jingning Han5fe79db2017-03-27 15:10:30 -07001971#if CONFIG_EC_MULTISYMBOL
1972 aom_cdf_prob *partition_cdf = (ctx >= 0) ? ec_ctx->partition_cdf[ctx] : NULL;
1973#endif
1974
Yaowu Xuc27fc142016-08-22 16:08:15 -07001975 if (has_rows && has_cols)
1976#if CONFIG_EXT_PARTITION_TYPES
1977 if (bsize <= BLOCK_8X8)
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001978#if CONFIG_EC_MULTISYMBOL
Jingning Han5fe79db2017-03-27 15:10:30 -07001979 p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, PARTITION_TYPES,
1980 ACCT_STR);
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001981#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001982 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001983#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001984 else
Alex Converse57795a42017-03-14 12:18:25 -07001985#if CONFIG_EC_MULTISYMBOL
Jingning Han5fe79db2017-03-27 15:10:30 -07001986 p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, EXT_PARTITION_TYPES,
1987 ACCT_STR);
Alex Converse57795a42017-03-14 12:18:25 -07001988#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001989 p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs,
1990 ACCT_STR);
Alex Converse57795a42017-03-14 12:18:25 -07001991#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001992#else
Nathan E. Egge9d9eb6c2016-11-16 09:44:26 -05001993#if CONFIG_EC_MULTISYMBOL
Jingning Han5fe79db2017-03-27 15:10:30 -07001994 p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, PARTITION_TYPES,
1995 ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001996#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001997 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001998#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001999#endif // CONFIG_EXT_PARTITION_TYPES
2000 else if (!has_rows && has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07002001 p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002002 else if (has_rows && !has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07002003 p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002004 else
2005 p = PARTITION_SPLIT;
2006
2007 if (counts) ++counts->partition[ctx][p];
2008
2009 return p;
2010}
2011
2012#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002013static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
2014 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002015 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
2016 return 1;
2017 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002018 const int ctx = av1_get_skip_context(xd);
Michael Bebenita6048d052016-08-25 14:40:54 -07002019 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002020 FRAME_COUNTS *counts = xd->counts;
2021 if (counts) ++counts->skip[ctx][skip];
2022 return skip;
2023 }
2024}
2025#endif // CONFIG_SUPERTX
2026
2027// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -07002028static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002029#if CONFIG_SUPERTX
2030 int supertx_enabled,
2031#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002032 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002033 BLOCK_SIZE bsize, int n4x4_l2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002034 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002035 const int n8x8_l2 = n4x4_l2 - 1;
Jingning Hanff17e162016-12-07 17:58:18 -08002036 const int num_8x8_wh = mi_size_wide[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002037 const int hbs = num_8x8_wh >> 1;
Jingning Han41bb3392016-12-14 10:46:48 -08002038#if CONFIG_CB4X4
2039 const int unify_bsize = 1;
2040#else
2041 const int unify_bsize = 0;
2042#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002043 PARTITION_TYPE partition;
2044 BLOCK_SIZE subsize;
2045#if CONFIG_EXT_PARTITION_TYPES
2046 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
2047#endif
2048 const int has_rows = (mi_row + hbs) < cm->mi_rows;
2049 const int has_cols = (mi_col + hbs) < cm->mi_cols;
2050#if CONFIG_SUPERTX
2051 const int read_token = !supertx_enabled;
2052 int skip = 0;
Jingning Han2511c662016-12-22 11:57:34 -08002053 TX_SIZE supertx_size = max_txsize_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002054 const TileInfo *const tile = &xd->tile;
2055 int txfm = DCT_DCT;
2056#endif // CONFIG_SUPERTX
2057
2058 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
2059
Jingning Hancd959762017-03-27 14:49:59 -07002060 partition = (bsize < BLOCK_8X8) ? PARTITION_NONE
2061 : read_partition(cm, xd, mi_row, mi_col, r,
2062 has_rows, has_cols, bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002063 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
Yushin Cho77bba8d2016-11-04 16:36:56 -07002064
2065#if CONFIG_PVQ
2066 assert(partition < PARTITION_TYPES);
2067 assert(subsize < BLOCK_SIZES);
2068#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002069#if CONFIG_SUPERTX
2070 if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
2071 bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
2072 const int supertx_context = partition_supertx_context_lookup[partition];
Michael Bebenita6048d052016-08-25 14:40:54 -07002073 supertx_enabled = aom_read(
2074 r, cm->fc->supertx_prob[supertx_context][supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002075 if (xd->counts)
2076 xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
2077#if CONFIG_VAR_TX
2078 if (supertx_enabled) xd->supertx_size = supertx_size;
2079#endif
2080 }
2081#endif // CONFIG_SUPERTX
Jingning Han41bb3392016-12-14 10:46:48 -08002082 if (!hbs && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002083 // calculate bmode block dimensions (log 2)
2084 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
2085 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
2086 decode_block(pbi, xd,
2087#if CONFIG_SUPERTX
2088 supertx_enabled,
2089#endif // CONFIG_SUPERTX
2090 mi_row, mi_col, r,
2091#if CONFIG_EXT_PARTITION_TYPES
2092 partition,
2093#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002094 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002095 } else {
2096 switch (partition) {
2097 case PARTITION_NONE:
2098 decode_block(pbi, xd,
2099#if CONFIG_SUPERTX
2100 supertx_enabled,
2101#endif // CONFIG_SUPERTX
2102 mi_row, mi_col, r,
2103#if CONFIG_EXT_PARTITION_TYPES
2104 partition,
2105#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002106 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002107 break;
2108 case PARTITION_HORZ:
2109 decode_block(pbi, xd,
2110#if CONFIG_SUPERTX
2111 supertx_enabled,
2112#endif // CONFIG_SUPERTX
2113 mi_row, mi_col, r,
2114#if CONFIG_EXT_PARTITION_TYPES
2115 partition,
2116#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002117 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002118 if (has_rows)
2119 decode_block(pbi, xd,
2120#if CONFIG_SUPERTX
2121 supertx_enabled,
2122#endif // CONFIG_SUPERTX
2123 mi_row + hbs, mi_col, r,
2124#if CONFIG_EXT_PARTITION_TYPES
2125 partition,
2126#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002127 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002128 break;
2129 case PARTITION_VERT:
2130 decode_block(pbi, xd,
2131#if CONFIG_SUPERTX
2132 supertx_enabled,
2133#endif // CONFIG_SUPERTX
2134 mi_row, mi_col, r,
2135#if CONFIG_EXT_PARTITION_TYPES
2136 partition,
2137#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002138 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002139 if (has_cols)
2140 decode_block(pbi, xd,
2141#if CONFIG_SUPERTX
2142 supertx_enabled,
2143#endif // CONFIG_SUPERTX
2144 mi_row, mi_col + hbs, r,
2145#if CONFIG_EXT_PARTITION_TYPES
2146 partition,
2147#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08002148 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002149 break;
2150 case PARTITION_SPLIT:
2151 decode_partition(pbi, xd,
2152#if CONFIG_SUPERTX
2153 supertx_enabled,
2154#endif // CONFIG_SUPERTX
2155 mi_row, mi_col, r, subsize, n8x8_l2);
2156 decode_partition(pbi, xd,
2157#if CONFIG_SUPERTX
2158 supertx_enabled,
2159#endif // CONFIG_SUPERTX
2160 mi_row, mi_col + hbs, r, subsize, n8x8_l2);
2161 decode_partition(pbi, xd,
2162#if CONFIG_SUPERTX
2163 supertx_enabled,
2164#endif // CONFIG_SUPERTX
2165 mi_row + hbs, mi_col, r, subsize, n8x8_l2);
2166 decode_partition(pbi, xd,
2167#if CONFIG_SUPERTX
2168 supertx_enabled,
2169#endif // CONFIG_SUPERTX
2170 mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
2171 break;
2172#if CONFIG_EXT_PARTITION_TYPES
2173 case PARTITION_HORZ_A:
2174 decode_block(pbi, xd,
2175#if CONFIG_SUPERTX
2176 supertx_enabled,
2177#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002178 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002179 decode_block(pbi, xd,
2180#if CONFIG_SUPERTX
2181 supertx_enabled,
2182#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002183 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002184 decode_block(pbi, xd,
2185#if CONFIG_SUPERTX
2186 supertx_enabled,
2187#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002188 mi_row + hbs, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002189 break;
2190 case PARTITION_HORZ_B:
2191 decode_block(pbi, xd,
2192#if CONFIG_SUPERTX
2193 supertx_enabled,
2194#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002195 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002196 decode_block(pbi, xd,
2197#if CONFIG_SUPERTX
2198 supertx_enabled,
2199#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002200 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002201 decode_block(pbi, xd,
2202#if CONFIG_SUPERTX
2203 supertx_enabled,
2204#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002205 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002206 break;
2207 case PARTITION_VERT_A:
2208 decode_block(pbi, xd,
2209#if CONFIG_SUPERTX
2210 supertx_enabled,
2211#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002212 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002213 decode_block(pbi, xd,
2214#if CONFIG_SUPERTX
2215 supertx_enabled,
2216#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002217 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002218 decode_block(pbi, xd,
2219#if CONFIG_SUPERTX
2220 supertx_enabled,
2221#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002222 mi_row, mi_col + hbs, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002223 break;
2224 case PARTITION_VERT_B:
2225 decode_block(pbi, xd,
2226#if CONFIG_SUPERTX
2227 supertx_enabled,
2228#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002229 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002230 decode_block(pbi, xd,
2231#if CONFIG_SUPERTX
2232 supertx_enabled,
2233#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002234 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002235 decode_block(pbi, xd,
2236#if CONFIG_SUPERTX
2237 supertx_enabled,
2238#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002239 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002240 break;
2241#endif
2242 default: assert(0 && "Invalid partition type");
2243 }
2244 }
2245
2246#if CONFIG_SUPERTX
2247 if (supertx_enabled && read_token) {
2248 uint8_t *dst_buf[3];
2249 int dst_stride[3], i;
2250 int offset = mi_row * cm->mi_stride + mi_col;
2251
2252 set_segment_id_supertx(cm, mi_row, mi_col, bsize);
2253
David Barker3aec8d62017-01-31 14:55:32 +00002254#if CONFIG_DELTA_Q
2255 if (cm->delta_q_present_flag) {
2256 for (i = 0; i < MAX_SEGMENTS; i++) {
2257 xd->plane[0].seg_dequant[i][0] =
2258 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
2259 xd->plane[0].seg_dequant[i][1] =
2260 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
2261 xd->plane[1].seg_dequant[i][0] =
2262 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2263 xd->plane[1].seg_dequant[i][1] =
2264 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2265 xd->plane[2].seg_dequant[i][0] =
2266 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
2267 xd->plane[2].seg_dequant[i][1] =
2268 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
2269 }
2270 }
2271#endif
2272
Yaowu Xuc27fc142016-08-22 16:08:15 -07002273 xd->mi = cm->mi_grid_visible + offset;
2274 xd->mi[0] = cm->mi + offset;
Urvang Joshi359dc2b2017-04-27 15:41:47 -07002275 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2276 mi_size_wide[bsize],
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002277#if CONFIG_DEPENDENT_HORZTILES
Urvang Joshi359dc2b2017-04-27 15:41:47 -07002278 cm->dependent_horz_tiles,
2279#endif // CONFIG_DEPENDENT_HORZTILES
2280 cm->mi_rows, cm->mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002281 set_skip_context(xd, mi_row, mi_col);
2282 skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
2283 if (skip) {
2284 reset_skip_context(xd, bsize);
2285 } else {
2286#if CONFIG_EXT_TX
Sarah Parkere68a3e42017-02-16 14:03:24 -08002287 if (get_ext_tx_types(supertx_size, bsize, 1, cm->reduced_tx_set_used) >
2288 1) {
2289 const int eset =
2290 get_ext_tx_set(supertx_size, bsize, 1, cm->reduced_tx_set_used);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002291 if (eset > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002292 txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
Michael Bebenita6048d052016-08-25 14:40:54 -07002293 cm->fc->inter_ext_tx_prob[eset][supertx_size],
2294 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002295 if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
2296 }
2297 }
2298#else
2299 if (supertx_size < TX_32X32) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002300 txfm = aom_read_tree(r, av1_ext_tx_tree,
Jingning Han8f6eb182016-10-19 13:48:57 -07002301 cm->fc->inter_ext_tx_prob[supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002302 if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
2303 }
2304#endif // CONFIG_EXT_TX
2305 }
2306
Jingning Han91d9a792017-04-18 12:01:52 -07002307 av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row,
2308 mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002309 for (i = 0; i < MAX_MB_PLANE; i++) {
2310 dst_buf[i] = xd->plane[i].dst.buf;
2311 dst_stride[i] = xd->plane[i].dst.stride;
2312 }
2313 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
2314 bsize, dst_buf, dst_stride);
2315
2316 if (!skip) {
2317 int eobtotal = 0;
2318 MB_MODE_INFO *mbmi;
2319 set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
2320 mbmi = &xd->mi[0]->mbmi;
2321 mbmi->tx_type = txfm;
2322 assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
2323 for (i = 0; i < MAX_MB_PLANE; ++i) {
2324 const struct macroblockd_plane *const pd = &xd->plane[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002325 int row, col;
Angie Chiang7fcfee42017-02-24 15:51:03 -08002326 const TX_SIZE tx_size = get_tx_size(i, xd);
Jingning Han5b7706a2016-12-21 09:55:10 -08002327 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Jingning Han32b20282016-10-28 15:42:44 -07002328 const int stepr = tx_size_high_unit[tx_size];
2329 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han5b7706a2016-12-21 09:55:10 -08002330 const int max_blocks_wide = max_block_wide(xd, plane_bsize, i);
2331 const int max_blocks_high = max_block_high(xd, plane_bsize, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002332
2333 for (row = 0; row < max_blocks_high; row += stepr)
2334 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07002335 eobtotal += reconstruct_inter_block(
2336 cm, xd, r, mbmi->segment_id_supertx, i, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002337 }
Jingning Han24f24a52016-12-27 10:13:28 -08002338 if ((unify_bsize || !(subsize < BLOCK_8X8)) && eobtotal == 0) skip = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002339 }
2340 set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
2341 }
2342#endif // CONFIG_SUPERTX
2343
2344#if CONFIG_EXT_PARTITION_TYPES
Alex Converseffabff32017-03-27 09:52:19 -07002345 update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002346#else
2347 // update partition context
2348 if (bsize >= BLOCK_8X8 &&
2349 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
Jingning Han1beb0102016-12-07 11:08:30 -08002350 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
David Barkerf8935c92016-10-26 14:54:06 +01002351#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xud71be782016-10-14 08:47:03 -07002352
Jean-Marc Valin01435132017-02-18 14:12:53 -05002353#if CONFIG_CDEF
David Barker9739f362016-11-10 09:29:32 +00002354#if CONFIG_EXT_PARTITION
2355 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128) {
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002356 if (!sb_all_skip(cm, mi_row, mi_col)) {
Jean-Marc Valin1df427e2017-03-23 18:32:57 -04002357 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2358 aom_read_literal(r, cm->cdef_bits, ACCT_STR);
David Barker9739f362016-11-10 09:29:32 +00002359 } else {
Jean-Marc Valin1df427e2017-03-23 18:32:57 -04002360 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2361 0;
David Barker9739f362016-11-10 09:29:32 +00002362 }
2363 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64) {
2364#else
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002365 if (bsize == BLOCK_64X64) {
David Barker9739f362016-11-10 09:29:32 +00002366#endif
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002367 if (!sb_all_skip(cm, mi_row, mi_col)) {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002368 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
2369 aom_read_literal(r, cm->cdef_bits, ACCT_STR);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002370 } else {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002371 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
Jean-Marc Valina8764952017-04-11 04:01:15 -04002372 -1;
Yaowu Xud71be782016-10-14 08:47:03 -07002373 }
2374 }
Jean-Marc Valin01435132017-02-18 14:12:53 -05002375#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002376}
2377
Yaowu Xuc27fc142016-08-22 16:08:15 -07002378static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
2379 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002380 struct aom_internal_error_info *error_info,
Alex Converseeb780e72016-12-13 12:46:41 -08002381 aom_reader *r,
2382#if CONFIG_ANS && ANS_MAX_SYMBOLS
2383 int window_size,
2384#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
2385 aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002386 // Validate the calculated partition length. If the buffer
2387 // described by the partition can't be fully read, then restrict
2388 // it to the portion that can be (for EC mode) or throw an error.
2389 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002390 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002391 "Truncated packet or corrupt tile length");
2392
Alex Converse2cdf0d82016-12-13 13:53:09 -08002393#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08002394 r->window_size = window_size;
Alex Converse2cdf0d82016-12-13 13:53:09 -08002395#endif
Alex Converse346440b2017-01-03 13:47:37 -08002396 if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07002397 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002398 "Failed to allocate bool decoder %d", 1);
2399}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002400
Angie Chiang800df032017-03-22 11:14:12 -07002401#if !CONFIG_PVQ && !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET) && !CONFIG_LV_MAP
Yaowu Xuf883b422016-08-30 14:01:10 -07002402static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
2403 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002404 int i, j, k, l, m;
Alex Conversea9598cd2017-02-03 14:18:05 -08002405#if CONFIG_EC_ADAPT
2406 const int node_limit = UNCONSTRAINED_NODES - 1;
2407#else
Thomas9ac55082016-09-23 18:04:17 +01002408 const int node_limit = UNCONSTRAINED_NODES;
Alex Conversea9598cd2017-02-03 14:18:05 -08002409#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002410
Michael Bebenita6048d052016-08-25 14:40:54 -07002411 if (aom_read_bit(r, ACCT_STR))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002412 for (i = 0; i < PLANE_TYPES; ++i)
2413 for (j = 0; j < REF_TYPES; ++j)
2414 for (k = 0; k < COEF_BANDS; ++k)
2415 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
Thomas9ac55082016-09-23 18:04:17 +01002416 for (m = 0; m < node_limit; ++m)
Michael Bebenita6048d052016-08-25 14:40:54 -07002417 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002418}
2419
Yaowu Xuf883b422016-08-30 14:01:10 -07002420static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002421 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
2422 TX_SIZE tx_size;
Jingning Han83630632016-12-16 11:27:25 -08002423 for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002424 read_coef_probs_common(fc->coef_probs[tx_size], r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002425}
Yushin Cho77bba8d2016-11-04 16:36:56 -07002426#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002427
Yaowu Xuf883b422016-08-30 14:01:10 -07002428static void setup_segmentation(AV1_COMMON *const cm,
2429 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002430 struct segmentation *const seg = &cm->seg;
2431 int i, j;
2432
2433 seg->update_map = 0;
2434 seg->update_data = 0;
2435
Yaowu Xuf883b422016-08-30 14:01:10 -07002436 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002437 if (!seg->enabled) return;
2438
2439 // Segmentation map update
2440 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2441 seg->update_map = 1;
2442 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002443 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002444 }
2445 if (seg->update_map) {
2446 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2447 seg->temporal_update = 0;
2448 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002449 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002450 }
2451 }
2452
2453 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07002454 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002455 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002456 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002457
Yaowu Xuf883b422016-08-30 14:01:10 -07002458 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002459
2460 for (i = 0; i < MAX_SEGMENTS; i++) {
2461 for (j = 0; j < SEG_LVL_MAX; j++) {
2462 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002463 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002464 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002465 av1_enable_segfeature(seg, i, j);
2466 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
2467 if (av1_is_segfeature_signed(j))
2468 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002469 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002470 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002471 }
2472 }
2473 }
2474}
2475
2476#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002477static void decode_restoration_mode(AV1_COMMON *cm,
2478 struct aom_read_bit_buffer *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002479 int p;
2480 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002481 if (aom_rb_read_bit(rb)) {
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -08002482 rsi->frame_restoration_type =
2483 aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002484 } else {
2485 rsi->frame_restoration_type =
2486 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
2487 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002488 for (p = 1; p < MAX_MB_PLANE; ++p) {
2489 cm->rst_info[p].frame_restoration_type =
2490 aom_rb_read_bit(rb) ? RESTORE_WIENER : RESTORE_NONE;
2491 }
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002492
2493 cm->rst_info[0].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2494 cm->rst_info[1].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2495 cm->rst_info[2].restoration_tilesize = RESTORATION_TILESIZE_MAX;
2496 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
2497 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
2498 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
2499 rsi = &cm->rst_info[0];
2500 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2501 if (rsi->restoration_tilesize != RESTORATION_TILESIZE_MAX) {
2502 rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
2503 }
2504 cm->rst_info[1].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2505 cm->rst_info[2].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
2506 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002507}
2508
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002509static void read_wiener_filter(WienerInfo *wiener_info,
2510 WienerInfo *ref_wiener_info, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002511 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002512 aom_read_primitive_refsubexpfin(
2513 rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
2514 WIENER_FILT_TAP0_SUBEXP_K,
2515 ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV) +
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002516 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002517 wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002518 aom_read_primitive_refsubexpfin(
2519 rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
2520 WIENER_FILT_TAP1_SUBEXP_K,
2521 ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV) +
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002522 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002523 wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002524 aom_read_primitive_refsubexpfin(
2525 rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
2526 WIENER_FILT_TAP2_SUBEXP_K,
2527 ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV) +
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002528 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002529 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002530 wiener_info->vfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002531 -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
2532 wiener_info->vfilter[2]);
2533
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002534 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002535 aom_read_primitive_refsubexpfin(
2536 rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
2537 WIENER_FILT_TAP0_SUBEXP_K,
2538 ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV) +
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002539 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002540 wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002541 aom_read_primitive_refsubexpfin(
2542 rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
2543 WIENER_FILT_TAP1_SUBEXP_K,
2544 ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV) +
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002545 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002546 wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002547 aom_read_primitive_refsubexpfin(
2548 rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
2549 WIENER_FILT_TAP2_SUBEXP_K,
2550 ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV) +
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002551 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00002552 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002553 wiener_info->hfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00002554 -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
2555 wiener_info->hfilter[2]);
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002556 memcpy(ref_wiener_info, wiener_info, sizeof(*wiener_info));
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002557}
2558
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002559static void read_sgrproj_filter(SgrprojInfo *sgrproj_info,
2560 SgrprojInfo *ref_sgrproj_info, aom_reader *rb) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002561 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
2562 sgrproj_info->xqd[0] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002563 aom_read_primitive_refsubexpfin(
2564 rb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
2565 ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0) +
2566 SGRPROJ_PRJ_MIN0;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002567 sgrproj_info->xqd[1] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002568 aom_read_primitive_refsubexpfin(
2569 rb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
2570 ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1) +
2571 SGRPROJ_PRJ_MIN1;
2572 memcpy(ref_sgrproj_info, sgrproj_info, sizeof(*sgrproj_info));
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002573}
2574
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002575static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002576 int i, p;
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002577 SgrprojInfo ref_sgrproj_info;
2578 WienerInfo ref_wiener_info;
2579 set_default_wiener(&ref_wiener_info);
2580 set_default_sgrproj(&ref_sgrproj_info);
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08002581 const int ntiles = av1_get_rest_ntiles(cm->width, cm->height,
2582 cm->rst_info[0].restoration_tilesize,
2583 NULL, NULL, NULL, NULL);
2584 const int ntiles_uv = av1_get_rest_ntiles(
2585 ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
2586 ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y),
2587 cm->rst_info[1].restoration_tilesize, NULL, NULL, NULL, NULL);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002588 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002589 if (rsi->frame_restoration_type != RESTORE_NONE) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002590 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002591 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002592 rsi->restoration_type[i] =
2593 aom_read_tree(rb, av1_switchable_restore_tree,
2594 cm->fc->switchable_restore_prob, ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002595 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002596 read_wiener_filter(&rsi->wiener_info[i], &ref_wiener_info, rb);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002597 } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002598 read_sgrproj_filter(&rsi->sgrproj_info[i], &ref_sgrproj_info, rb);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002599 }
2600 }
2601 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002602 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002603 if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002604 rsi->restoration_type[i] = RESTORE_WIENER;
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002605 read_wiener_filter(&rsi->wiener_info[i], &ref_wiener_info, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002606 } else {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002607 rsi->restoration_type[i] = RESTORE_NONE;
2608 }
2609 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002610 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002611 for (i = 0; i < ntiles; ++i) {
2612 if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
2613 rsi->restoration_type[i] = RESTORE_SGRPROJ;
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002614 read_sgrproj_filter(&rsi->sgrproj_info[i], &ref_sgrproj_info, rb);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002615 } else {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002616 rsi->restoration_type[i] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002617 }
2618 }
2619 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002620 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002621 for (p = 1; p < MAX_MB_PLANE; ++p) {
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002622 set_default_wiener(&ref_wiener_info);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002623 rsi = &cm->rst_info[p];
2624 if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08002625 for (i = 0; i < ntiles_uv; ++i) {
2626 if (ntiles_uv > 1)
2627 rsi->restoration_type[i] =
2628 aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR) ? RESTORE_WIENER
2629 : RESTORE_NONE;
2630 else
2631 rsi->restoration_type[i] = RESTORE_WIENER;
2632 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07002633 read_wiener_filter(&rsi->wiener_info[i], &ref_wiener_info, rb);
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08002634 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002635 }
2636 }
2637 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002638}
2639#endif // CONFIG_LOOP_RESTORATION
2640
Yaowu Xuf883b422016-08-30 14:01:10 -07002641static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002642 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07002643 lf->filter_level = aom_rb_read_literal(rb, 6);
2644 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002645
2646 // Read in loop filter deltas applied at the MB level based on mode or ref
2647 // frame.
2648 lf->mode_ref_delta_update = 0;
2649
Yaowu Xuf883b422016-08-30 14:01:10 -07002650 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002651 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002652 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002653 if (lf->mode_ref_delta_update) {
2654 int i;
2655
2656 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002657 if (aom_rb_read_bit(rb))
2658 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002659
2660 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002661 if (aom_rb_read_bit(rb))
2662 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002663 }
2664 }
2665}
2666
Jean-Marc Valin01435132017-02-18 14:12:53 -05002667#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01002668static void setup_cdef(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002669 int i;
Steinar Midtskogen0c966a52017-04-18 14:38:13 +02002670 cm->cdef_dering_damping = aom_rb_read_literal(rb, 1) + 5;
2671 cm->cdef_clpf_damping = aom_rb_read_literal(rb, 2) + 3;
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002672 cm->cdef_bits = aom_rb_read_literal(rb, 2);
2673 cm->nb_cdef_strengths = 1 << cm->cdef_bits;
2674 for (i = 0; i < cm->nb_cdef_strengths; i++) {
2675 cm->cdef_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
Jean-Marc Valine9f77422017-03-22 17:09:51 -04002676 cm->cdef_uv_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04002677 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002678}
Jean-Marc Valin01435132017-02-18 14:12:53 -05002679#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002680
Yaowu Xuf883b422016-08-30 14:01:10 -07002681static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2682 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002683}
2684
Yaowu Xuf883b422016-08-30 14:01:10 -07002685static void setup_quantization(AV1_COMMON *const cm,
2686 struct aom_read_bit_buffer *rb) {
2687 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002688 cm->y_dc_delta_q = read_delta_q(rb);
2689 cm->uv_dc_delta_q = read_delta_q(rb);
2690 cm->uv_ac_delta_q = read_delta_q(rb);
2691 cm->dequant_bit_depth = cm->bit_depth;
2692#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002693 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002694 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002695 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2696 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002697 } else {
2698 cm->min_qmlevel = 0;
2699 cm->max_qmlevel = 0;
2700 }
2701#endif
2702}
2703
Yaowu Xuf883b422016-08-30 14:01:10 -07002704static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002705 // Build y/uv dequant values based on segmentation.
2706 int i = 0;
2707#if CONFIG_AOM_QM
2708 int lossless;
2709 int j = 0;
2710 int qmlevel;
2711 int using_qm = cm->using_qmatrix;
2712 int minqm = cm->min_qmlevel;
2713 int maxqm = cm->max_qmlevel;
2714#endif
2715#if CONFIG_NEW_QUANT
2716 int b;
2717 int dq;
2718#endif // CONFIG_NEW_QUANT
2719 if (cm->seg.enabled) {
2720 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002721 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002722 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002723 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2724 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002725 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002726 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002727 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002728 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002729#if CONFIG_AOM_QM
2730 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2731 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2732 // NB: depends on base index so there is only 1 set per frame
2733 // No quant weighting when lossless or signalled not using QM
2734 qmlevel = (lossless || using_qm == 0)
2735 ? NUM_QM_LEVELS - 1
2736 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2737 for (j = 0; j < TX_SIZES; ++j) {
2738 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2739 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2740 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2741 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2742 }
2743#endif // CONFIG_AOM_QM
2744#if CONFIG_NEW_QUANT
2745 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2746 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002747 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002748 cm->y_dequant_nuq[i][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002749 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002750 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002751 }
2752 }
2753#endif // CONFIG_NEW_QUANT
2754 }
2755 } else {
2756 const int qindex = cm->base_qindex;
2757 // When segmentation is disabled, only the first value is used. The
2758 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002759 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2760 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002761 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002762 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002763 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002764 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002765#if CONFIG_AOM_QM
2766 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2767 cm->uv_ac_delta_q == 0;
2768 // No quant weighting when lossless or signalled not using QM
2769 qmlevel = (lossless || using_qm == 0)
2770 ? NUM_QM_LEVELS - 1
2771 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2772 for (j = 0; j < TX_SIZES; ++j) {
2773 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2774 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2775 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2776 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2777 }
2778#endif
2779#if CONFIG_NEW_QUANT
2780 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2781 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002782 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002783 cm->y_dequant_nuq[0][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002784 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002785 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002786 }
2787 }
2788#endif // CONFIG_NEW_QUANT
2789 }
2790}
2791
Angie Chiang5678ad92016-11-21 09:38:40 -08002792static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002793 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07002794 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002795}
2796
Yaowu Xuf883b422016-08-30 14:01:10 -07002797static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002798 cm->render_width = cm->width;
2799 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002800 if (aom_rb_read_bit(rb))
2801 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002802}
2803
Fergus Simpsond91c8c92017-04-07 12:12:00 -07002804#if CONFIG_FRAME_SUPERRES
Fergus Simpsone7508412017-03-14 18:14:09 -07002805// TODO(afergs): make "struct aom_read_bit_buffer *const rb"?
2806static void setup_superres_size(AV1_COMMON *const cm,
2807 struct aom_read_bit_buffer *rb, int *width,
2808 int *height) {
2809 // TODO(afergs): Test this behaviour
2810 // Frame superres is probably in compatible with this render resolution
2811 assert(cm->width == cm->render_width && cm->height == cm->render_height);
2812
2813 cm->superres_width = cm->width;
2814 cm->superres_height = cm->height;
2815 if (aom_rb_read_bit(rb)) {
2816 cm->superres_scale_numerator =
2817 (uint8_t)aom_rb_read_literal(rb, SUPERRES_SCALE_BITS);
2818 cm->superres_scale_numerator += SUPERRES_SCALE_NUMERATOR_MIN;
2819 // Don't edit cm->width or cm->height directly, or the buffers won't get
2820 // resized correctly
2821 // TODO(afergs): Should the render resolution not be modified? It's the same
2822 // by default (ie. when it isn't sent)...
2823 // resize_context_buffers() will change cm->width to equal cm->render_width,
2824 // then they'll be the same again
2825 *width = cm->render_width =
2826 cm->width * cm->superres_scale_numerator / SUPERRES_SCALE_DENOMINATOR;
2827 *height = cm->render_height =
2828 cm->height * cm->superres_scale_numerator / SUPERRES_SCALE_DENOMINATOR;
2829 } else {
2830 // 1:1 scaling - ie. no scaling, scale not provided
2831 cm->superres_scale_numerator = SUPERRES_SCALE_DENOMINATOR;
2832 }
2833}
Fergus Simpsond91c8c92017-04-07 12:12:00 -07002834#endif // CONFIG_FRAME_SUPERRES
Fergus Simpsone7508412017-03-14 18:14:09 -07002835
Yaowu Xuf883b422016-08-30 14:01:10 -07002836static void resize_mv_buffer(AV1_COMMON *cm) {
2837 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002838 cm->cur_frame->mi_rows = cm->mi_rows;
2839 cm->cur_frame->mi_cols = cm->mi_cols;
2840 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002841 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002842 sizeof(*cm->cur_frame->mvs)));
2843}
2844
Yaowu Xuf883b422016-08-30 14:01:10 -07002845static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002846#if CONFIG_SIZE_LIMIT
2847 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002848 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002849 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2850 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2851#endif
2852 if (cm->width != width || cm->height != height) {
2853 const int new_mi_rows =
2854 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2855 const int new_mi_cols =
2856 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2857
Yaowu Xuf883b422016-08-30 14:01:10 -07002858 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002859 // dimensions as well as the overall size.
2860 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002861 if (av1_alloc_context_buffers(cm, width, height))
2862 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002863 "Failed to allocate context buffers");
2864 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002865 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002866 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002867 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002868 cm->width = width;
2869 cm->height = height;
2870 }
2871 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2872 cm->mi_cols > cm->cur_frame->mi_cols) {
2873 resize_mv_buffer(cm);
2874 }
2875}
2876
Yaowu Xuf883b422016-08-30 14:01:10 -07002877static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002878 int width, height;
2879 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002880 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002881 setup_render_size(cm, rb);
Fergus Simpsond91c8c92017-04-07 12:12:00 -07002882#if CONFIG_FRAME_SUPERRES
Fergus Simpsone7508412017-03-14 18:14:09 -07002883 setup_superres_size(cm, rb, &width, &height);
Fergus Simpsond91c8c92017-04-07 12:12:00 -07002884#endif // CONFIG_FRAME_SUPERRES
Fergus Simpsone7508412017-03-14 18:14:09 -07002885 resize_context_buffers(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002886
2887 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002888 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002889 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2890 cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002891#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002892 cm->use_highbitdepth,
2893#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002894 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002895 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2896 pool->cb_priv)) {
2897 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002898 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002899 "Failed to allocate frame buffer");
2900 }
2901 unlock_buffer_pool(pool);
2902
2903 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2904 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2905 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2906 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2907 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2908 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2909 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2910}
2911
Yaowu Xuf883b422016-08-30 14:01:10 -07002912static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002913 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002914 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002915 int this_xss, int this_yss) {
2916 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2917 ref_yss == this_yss;
2918}
2919
Yaowu Xuf883b422016-08-30 14:01:10 -07002920static void setup_frame_size_with_refs(AV1_COMMON *cm,
2921 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002922 int width, height;
2923 int found = 0, i;
2924 int has_valid_ref_frame = 0;
2925 BufferPool *const pool = cm->buffer_pool;
2926 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002927 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002928 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2929 width = buf->y_crop_width;
2930 height = buf->y_crop_height;
2931 cm->render_width = buf->render_width;
2932 cm->render_height = buf->render_height;
2933 found = 1;
2934 break;
2935 }
2936 }
2937
2938 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002939 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002940 setup_render_size(cm, rb);
2941 }
2942
2943 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002944 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002945 "Invalid frame size");
2946
2947 // Check to make sure at least one of frames that this frame references
2948 // has valid dimensions.
2949 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2950 RefBuffer *const ref_frame = &cm->frame_refs[i];
2951 has_valid_ref_frame |=
2952 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2953 ref_frame->buf->y_crop_height, width, height);
2954 }
2955 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002956 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002957 "Referenced frame has invalid size");
2958 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2959 RefBuffer *const ref_frame = &cm->frame_refs[i];
2960 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
2961 ref_frame->buf->subsampling_x,
2962 ref_frame->buf->subsampling_y, cm->bit_depth,
2963 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07002964 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002965 "Referenced frame has incompatible color format");
2966 }
2967
2968 resize_context_buffers(cm, width, height);
2969
2970 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002971 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002972 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2973 cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002974#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002975 cm->use_highbitdepth,
2976#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002977 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002978 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2979 pool->cb_priv)) {
2980 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002981 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002982 "Failed to allocate frame buffer");
2983 }
2984 unlock_buffer_pool(pool);
2985
2986 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2987 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2988 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2989 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2990 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2991 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2992 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2993}
2994
Yaowu Xuf883b422016-08-30 14:01:10 -07002995static void read_tile_info(AV1Decoder *const pbi,
2996 struct aom_read_bit_buffer *const rb) {
2997 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002998#if CONFIG_EXT_TILE
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08002999 cm->tile_encoding_mode = aom_rb_read_literal(rb, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003000// Read the tile width/height
3001#if CONFIG_EXT_PARTITION
3002 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003003 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
3004 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003005 } else
3006#endif // CONFIG_EXT_PARTITION
3007 {
Yaowu Xuf883b422016-08-30 14:01:10 -07003008 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
3009 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003010 }
3011
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003012#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003013 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003014#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003015
Yaowu Xuc27fc142016-08-22 16:08:15 -07003016 cm->tile_width <<= cm->mib_size_log2;
3017 cm->tile_height <<= cm->mib_size_log2;
3018
Yaowu Xuf883b422016-08-30 14:01:10 -07003019 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
3020 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003021
3022 // Get the number of tiles
3023 cm->tile_cols = 1;
3024 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
3025
3026 cm->tile_rows = 1;
3027 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
3028
3029 if (cm->tile_cols * cm->tile_rows > 1) {
3030 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07003031 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
3032 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003033 }
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003034
3035#if CONFIG_DEPENDENT_HORZTILES
3036 if (cm->tile_rows <= 1)
3037 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
3038 else
3039 cm->dependent_horz_tiles = 0;
3040#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003041#else
3042 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07003043 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003044
3045 // columns
3046 max_ones = max_log2_tile_cols - min_log2_tile_cols;
3047 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07003048 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003049
3050 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07003051 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003052 "Invalid number of tile columns");
3053
3054 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07003055 cm->log2_tile_rows = aom_rb_read_bit(rb);
3056 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003057#if CONFIG_DEPENDENT_HORZTILES
3058 if (cm->log2_tile_rows != 0)
3059 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
3060 else
3061 cm->dependent_horz_tiles = 0;
3062#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003063#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003064 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003065#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08003066
Yaowu Xuc27fc142016-08-22 16:08:15 -07003067 cm->tile_cols = 1 << cm->log2_tile_cols;
3068 cm->tile_rows = 1 << cm->log2_tile_rows;
3069
3070 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
3071 cm->tile_width >>= cm->log2_tile_cols;
3072 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
3073 cm->tile_height >>= cm->log2_tile_rows;
3074
3075 // round to integer multiples of superblock size
3076 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
3077 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
3078
Thomas Davies4974e522016-11-07 17:44:05 +00003079// tile size magnitude
3080#if !CONFIG_TILE_GROUPS
3081 if (cm->tile_rows > 1 || cm->tile_cols > 1)
3082#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003083 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003084#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00003085
Thomas Davies80188d12016-10-26 16:08:35 -07003086#if CONFIG_TILE_GROUPS
3087 // Store an index to the location of the tile group information
3088 pbi->tg_size_bit_offset = rb->bit_offset;
3089 pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
3090 if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
3091 pbi->tg_start =
3092 aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3093 pbi->tg_size =
3094 1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3095 }
3096#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003097}
3098
Yaowu Xu4ff59b52017-04-24 12:41:56 -07003099static int mem_get_varsize(const uint8_t *src, int sz) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003100 switch (sz) {
3101 case 1: return src[0];
3102 case 2: return mem_get_le16(src);
3103 case 3: return mem_get_le24(src);
3104 case 4: return mem_get_le32(src);
3105 default: assert("Invalid size" && 0); return -1;
3106 }
3107}
3108
3109#if CONFIG_EXT_TILE
3110// Reads the next tile returning its size and adjusting '*data' accordingly
3111// based on 'is_last'.
3112static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07003113 struct aom_internal_error_info *error_info,
3114 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003115 void *decrypt_state,
3116 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08003117 int tile_size_bytes, int col, int row,
3118 unsigned int tile_encoding_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003119 size_t size;
3120
3121 size_t copy_size = 0;
3122 const uint8_t *copy_data = NULL;
3123
3124 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003125 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003126 "Truncated packet or corrupt tile length");
3127 if (decrypt_cb) {
3128 uint8_t be_data[4];
3129 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3130
3131 // Only read number of bytes in cm->tile_size_bytes.
3132 size = mem_get_varsize(be_data, tile_size_bytes);
3133 } else {
3134 size = mem_get_varsize(*data, tile_size_bytes);
3135 }
3136
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08003137 // If cm->tile_encoding_mode = 1 (i.e. TILE_VR), then the top bit of the tile
3138 // header indicates copy mode.
3139 if (tile_encoding_mode && (size >> (tile_size_bytes * 8 - 1)) == 1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003140 // The remaining bits in the top byte signal the row offset
3141 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
3142
3143 // Currently, only use tiles in same column as reference tiles.
3144 copy_data = tile_buffers[row - offset][col].data;
3145 copy_size = tile_buffers[row - offset][col].size;
3146 size = 0;
3147 }
3148
3149 *data += tile_size_bytes;
3150
3151 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003152 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003153 "Truncated packet or corrupt tile size");
3154
3155 if (size > 0) {
3156 tile_buffers[row][col].data = *data;
3157 tile_buffers[row][col].size = size;
3158 } else {
3159 tile_buffers[row][col].data = copy_data;
3160 tile_buffers[row][col].size = copy_size;
3161 }
3162
3163 *data += size;
3164
3165 tile_buffers[row][col].raw_data_end = *data;
3166}
3167
3168static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003169 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003170 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003171 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003172 const int tile_cols = cm->tile_cols;
3173 const int tile_rows = cm->tile_rows;
3174 const int have_tiles = tile_cols * tile_rows > 1;
3175
3176 if (!have_tiles) {
Jingning Han99ffce62017-04-25 15:48:41 -07003177 const size_t tile_size = data_end - data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003178 tile_buffers[0][0].data = data;
3179 tile_buffers[0][0].size = tile_size;
3180 tile_buffers[0][0].raw_data_end = NULL;
3181 } else {
3182 // We locate only the tile buffers that are required, which are the ones
3183 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
3184 // need the last (bottom right) tile buffer, as we need to know where the
3185 // end of the compressed frame buffer is for proper superframe decoding.
3186
3187 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
3188 const uint8_t *const data_start = data;
3189
Yaowu Xuf883b422016-08-30 14:01:10 -07003190 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003191 const int single_row = pbi->dec_tile_row >= 0;
3192 const int tile_rows_start = single_row ? dec_tile_row : 0;
3193 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003194 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003195 const int single_col = pbi->dec_tile_col >= 0;
3196 const int tile_cols_start = single_col ? dec_tile_col : 0;
3197 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3198
3199 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
3200 const int tile_size_bytes = pbi->tile_size_bytes;
3201
3202 size_t tile_col_size;
3203 int r, c;
3204
3205 // Read tile column sizes for all columns (we need the last tile buffer)
3206 for (c = 0; c < tile_cols; ++c) {
3207 const int is_last = c == tile_cols - 1;
3208 if (!is_last) {
3209 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
3210 data += tile_col_size_bytes;
3211 tile_col_data_end[c] = data + tile_col_size;
3212 } else {
3213 tile_col_size = data_end - data;
3214 tile_col_data_end[c] = data_end;
3215 }
3216 data += tile_col_size;
3217 }
3218
3219 data = data_start;
3220
3221 // Read the required tile sizes.
3222 for (c = tile_cols_start; c < tile_cols_end; ++c) {
3223 const int is_last = c == tile_cols - 1;
3224
3225 if (c > 0) data = tile_col_data_end[c - 1];
3226
3227 if (!is_last) data += tile_col_size_bytes;
3228
3229 // Get the whole of the last column, otherwise stop at the required tile.
3230 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
3231 tile_buffers[r][c].col = c;
3232
3233 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3234 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08003235 tile_size_bytes, c, r, cm->tile_encoding_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003236 }
3237 }
3238
3239 // If we have not read the last column, then read it to get the last tile.
3240 if (tile_cols_end != tile_cols) {
3241 c = tile_cols - 1;
3242
3243 data = tile_col_data_end[c - 1];
3244
3245 for (r = 0; r < tile_rows; ++r) {
3246 tile_buffers[r][c].col = c;
3247
3248 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3249 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08003250 tile_size_bytes, c, r, cm->tile_encoding_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003251 }
3252 }
3253 }
3254}
3255#else
3256// Reads the next tile returning its size and adjusting '*data' accordingly
3257// based on 'is_last'.
3258static void get_tile_buffer(const uint8_t *const data_end,
3259 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07003260 struct aom_internal_error_info *error_info,
3261 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003262 void *decrypt_state, TileBufferDec *const buf) {
3263 size_t size;
3264
3265 if (!is_last) {
Yaowu Xu0a79a1b2017-02-17 13:04:54 -08003266 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003267 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003268 "Truncated packet or corrupt tile length");
3269
3270 if (decrypt_cb) {
3271 uint8_t be_data[4];
3272 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3273 size = mem_get_varsize(be_data, tile_size_bytes);
3274 } else {
3275 size = mem_get_varsize(*data, tile_size_bytes);
3276 }
3277 *data += tile_size_bytes;
3278
3279 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003280 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003281 "Truncated packet or corrupt tile size");
3282 } else {
3283 size = data_end - *data;
3284 }
3285
3286 buf->data = *data;
3287 buf->size = size;
3288
3289 *data += size;
3290}
3291
3292static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003293 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003294 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003295 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07003296#if CONFIG_TILE_GROUPS
3297 int r, c;
3298 const int tile_cols = cm->tile_cols;
3299 const int tile_rows = cm->tile_rows;
3300 int tc = 0;
3301 int first_tile_in_tg = 0;
Thomas Davies80188d12016-10-26 16:08:35 -07003302 struct aom_read_bit_buffer rb_tg_hdr;
3303 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3304 const int num_tiles = tile_rows * tile_cols;
3305 const int num_bits = OD_ILOG(num_tiles) - 1;
James Zern6efba482017-04-20 20:53:49 -07003306 const size_t hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
Thomas Davies80188d12016-10-26 16:08:35 -07003307 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
Fangwen Fu73126c02017-02-08 22:37:47 -08003308#if CONFIG_DEPENDENT_HORZTILES
3309 int tile_group_start_col = 0;
3310 int tile_group_start_row = 0;
3311#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003312
3313 for (r = 0; r < tile_rows; ++r) {
3314 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07003315 TileBufferDec *const buf = &tile_buffers[r][c];
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003316 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
James Zern6efba482017-04-20 20:53:49 -07003317 const size_t hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
Thomas Davies80188d12016-10-26 16:08:35 -07003318
3319 buf->col = c;
3320 if (hdr_offset) {
3321 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
3322 rb_tg_hdr.bit_offset = tg_size_bit_offset;
3323 if (num_tiles) {
3324 pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
3325 pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
Fangwen Fu73126c02017-02-08 22:37:47 -08003326#if CONFIG_DEPENDENT_HORZTILES
3327 tile_group_start_row = r;
3328 tile_group_start_col = c;
3329#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003330 }
3331 }
3332 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
3333 data += hdr_offset;
Thomas Daviesa0de6d52017-01-20 14:45:25 +00003334 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last,
3335 &pbi->common.error, &data, pbi->decrypt_cb,
3336 pbi->decrypt_state, buf);
Fangwen Fu73126c02017-02-08 22:37:47 -08003337#if CONFIG_DEPENDENT_HORZTILES
3338 cm->tile_group_start_row[r][c] = tile_group_start_row;
3339 cm->tile_group_start_col[r][c] = tile_group_start_col;
3340#endif
Thomas Davies80188d12016-10-26 16:08:35 -07003341 }
3342 }
3343#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003344 int r, c;
3345 const int tile_cols = cm->tile_cols;
3346 const int tile_rows = cm->tile_rows;
3347
3348 for (r = 0; r < tile_rows; ++r) {
3349 for (c = 0; c < tile_cols; ++c) {
3350 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
3351 TileBufferDec *const buf = &tile_buffers[r][c];
3352 buf->col = c;
3353 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
3354 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
3355 }
3356 }
Thomas Davies80188d12016-10-26 16:08:35 -07003357#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003358}
3359#endif // CONFIG_EXT_TILE
3360
Yushin Cho77bba8d2016-11-04 16:36:56 -07003361#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003362static void daala_dec_init(AV1_COMMON *const cm, daala_dec_ctx *daala_dec,
Nathan E. Eggeab083972016-12-28 15:31:46 -05003363 aom_reader *r) {
3364 daala_dec->r = r;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003365
Yushin Cho70669122016-12-08 09:53:14 -10003366 // TODO(yushin) : activity masking info needs be signaled by a bitstream
3367 daala_dec->use_activity_masking = AV1_PVQ_ENABLE_ACTIVITY_MASKING;
3368
Yushin Cho7a428ba2017-01-12 16:28:49 -08003369#if !CONFIG_DAALA_DIST
3370 daala_dec->use_activity_masking = 0;
3371#endif
3372
Yushin Cho70669122016-12-08 09:53:14 -10003373 if (daala_dec->use_activity_masking)
3374 daala_dec->qm = OD_HVS_QM;
3375 else
3376 daala_dec->qm = OD_FLAT_QM;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003377
3378 od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
3379 daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
Yushin Cho70669122016-12-08 09:53:14 -10003380
3381 if (daala_dec->use_activity_masking) {
3382 int pli;
3383 int use_masking = daala_dec->use_activity_masking;
3384 int segment_id = 0;
3385 int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
3386
3387 for (pli = 0; pli < MAX_MB_PLANE; pli++) {
3388 int i;
3389 int q;
3390
3391 q = qindex;
3392 if (q <= OD_DEFAULT_QMS[use_masking][0][pli].interp_q << OD_COEFF_SHIFT) {
3393 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3394 &OD_DEFAULT_QMS[use_masking][0][pli], NULL);
3395 } else {
3396 i = 0;
3397 while (OD_DEFAULT_QMS[use_masking][i + 1][pli].qm_q4 != NULL &&
3398 q > OD_DEFAULT_QMS[use_masking][i + 1][pli].interp_q
3399 << OD_COEFF_SHIFT) {
3400 i++;
3401 }
3402 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3403 &OD_DEFAULT_QMS[use_masking][i][pli],
3404 &OD_DEFAULT_QMS[use_masking][i + 1][pli]);
3405 }
3406 }
3407 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07003408}
Yushin Cho70669122016-12-08 09:53:14 -10003409#endif // #if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003410
Yaowu Xuf883b422016-08-30 14:01:10 -07003411static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003412 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003413 AV1_COMMON *const cm = &pbi->common;
3414 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003415 const int tile_cols = cm->tile_cols;
3416 const int tile_rows = cm->tile_rows;
3417 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07003418 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003419#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003420 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003421 const int single_row = pbi->dec_tile_row >= 0;
3422 const int tile_rows_start = single_row ? dec_tile_row : 0;
3423 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003424 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003425 const int single_col = pbi->dec_tile_col >= 0;
3426 const int tile_cols_start = single_col ? dec_tile_col : 0;
3427 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3428 const int inv_col_order = pbi->inv_tile_order && !single_col;
3429 const int inv_row_order = pbi->inv_tile_order && !single_row;
3430#else
3431 const int tile_rows_start = 0;
3432 const int tile_rows_end = tile_rows;
3433 const int tile_cols_start = 0;
3434 const int tile_cols_end = tile_cols;
3435 const int inv_col_order = pbi->inv_tile_order;
3436 const int inv_row_order = pbi->inv_tile_order;
3437#endif // CONFIG_EXT_TILE
3438 int tile_row, tile_col;
3439
Yaowu Xuc27fc142016-08-22 16:08:15 -07003440 if (cm->lf.filter_level && !cm->skip_loop_filter &&
3441 pbi->lf_worker.data1 == NULL) {
3442 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07003443 aom_memalign(32, sizeof(LFWorkerData)));
3444 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003445 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003446 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003447 "Loop filter thread creation failed");
3448 }
3449 }
3450
3451 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3452 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3453 // Be sure to sync as we might be resuming after a failed frame decode.
3454 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003455 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
3456 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003457 }
3458
3459 assert(tile_rows <= MAX_TILE_ROWS);
3460 assert(tile_cols <= MAX_TILE_COLS);
3461
3462 get_tile_buffers(pbi, data, data_end, tile_buffers);
3463
3464 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003465 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003466 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003467 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003468 pbi->allocated_tiles = n_tiles;
3469 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003470#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003471 if (pbi->acct_enabled) {
3472 aom_accounting_reset(&pbi->accounting);
3473 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003474#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003475 // Load all tile information into tile_data.
3476 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3477 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3478 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
3479 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
3480
3481 td->cm = cm;
3482 td->xd = pbi->mb;
3483 td->xd.corrupted = 0;
3484 td->xd.counts =
3485 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3486 ? &cm->counts
3487 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003488 av1_zero(td->dqcoeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003489#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003490 av1_zero(td->pvq_ref_coeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003491#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003492 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003493 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003494 &td->bit_reader,
3495#if CONFIG_ANS && ANS_MAX_SYMBOLS
3496 1 << cm->ans_window_size_log2,
3497#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3498 pbi->decrypt_cb, pbi->decrypt_state);
Michael Bebenita6048d052016-08-25 14:40:54 -07003499#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003500 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003501 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003502 } else {
David Barkerd971f402016-10-25 13:52:07 +01003503 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003504 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003505#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003506 av1_init_macroblockd(cm, &td->xd,
3507#if CONFIG_PVQ
3508 td->pvq_ref_coeff,
3509#endif
Luc Trudeauf8164152017-04-11 16:20:51 -04003510#if CONFIG_CFL
3511 &td->cfl,
3512#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003513 td->dqcoeff);
Yushin Choc49ef3a2017-03-13 17:27:25 -07003514
Thomas Daviesf77d4ad2017-01-10 18:55:42 +00003515#if CONFIG_EC_ADAPT
3516 // Initialise the tile context from the frame context
3517 td->tctx = *cm->fc;
3518 td->xd.tile_ctx = &td->tctx;
3519#endif
Yushin Choc49ef3a2017-03-13 17:27:25 -07003520
3521#if CONFIG_PVQ
3522 daala_dec_init(cm, &td->xd.daala_dec, &td->bit_reader);
3523 td->xd.daala_dec.state.adapt = &td->tctx.pvq_context;
3524#endif
3525
Urvang Joshib100db72016-10-12 16:28:56 -07003526#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003527 td->xd.plane[0].color_index_map = td->color_index_map[0];
3528 td->xd.plane[1].color_index_map = td->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003529#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003530 }
3531 }
3532
3533 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3534 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
3535 int mi_row = 0;
3536 TileInfo tile_info;
3537
Yaowu Xuf883b422016-08-30 14:01:10 -07003538 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003539
3540 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3541 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
3542 TileData *const td = pbi->tile_data + tile_cols * row + col;
Michael Bebenita6048d052016-08-25 14:40:54 -07003543#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003544 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003545 td->bit_reader.accounting->last_tell_frac =
3546 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003547 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003548#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003549
Yaowu Xuf883b422016-08-30 14:01:10 -07003550 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003551
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003552#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003553#if CONFIG_TILE_GROUPS
3554 av1_tile_set_tg_boundary(&tile_info, cm, tile_row, tile_col);
3555 if (!cm->dependent_horz_tiles || tile_row == 0 ||
3556 tile_info.tg_horz_boundary) {
3557#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003558 if (!cm->dependent_horz_tiles || tile_row == 0) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003559#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003560 av1_zero_above_context(cm, tile_info.mi_col_start,
3561 tile_info.mi_col_end);
3562 }
3563#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003564 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003565#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003566
3567 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
3568 mi_row += cm->mib_size) {
3569 int mi_col;
3570
Yaowu Xuf883b422016-08-30 14:01:10 -07003571 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003572
3573 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
3574 mi_col += cm->mib_size) {
Ryan Lei9b02b0e2017-01-30 15:52:20 -08003575 av1_update_boundary_info(cm, &tile_info, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003576 decode_partition(pbi, &td->xd,
3577#if CONFIG_SUPERTX
3578 0,
3579#endif // CONFIG_SUPERTX
3580 mi_row, mi_col, &td->bit_reader, cm->sb_size,
3581 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003582#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3583 detoken_and_recon_sb(pbi, &td->xd, mi_row, mi_col, &td->bit_reader,
3584 cm->sb_size);
3585#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003586 }
Angie Chiangd0916d92017-03-10 17:54:18 -08003587 aom_merge_corrupted_flag(&pbi->mb.corrupted, td->xd.corrupted);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003588 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003589 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003590 "Failed to decode tile data");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003591 }
3592 }
3593
3594 assert(mi_row > 0);
3595
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003596// when Parallel deblocking is enabled, deblocking should not
3597// be interleaved with decoding. Instead, deblocking should be done
3598// after the entire frame is decoded.
Jingning Han52ece882017-04-07 14:58:25 -07003599#if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING && !CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -07003600 // Loopfilter one tile row.
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08003601 // Note: If out-of-order tile decoding is used(for example, inv_row_order
3602 // = 1), the loopfiltering has be done after all tile rows are decoded.
3603 if (!inv_row_order && cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003604 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003605 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003606 const int lf_end = tile_info.mi_row_end - cm->mib_size;
3607
3608 // Delay the loopfilter if the first tile row is only
3609 // a single superblock high.
3610 if (lf_end <= 0) continue;
3611
3612 // Decoding has completed. Finish up the loop filter in this thread.
3613 if (tile_info.mi_row_end >= cm->mi_rows) continue;
3614
3615 winterface->sync(&pbi->lf_worker);
3616 lf_data->start = lf_start;
3617 lf_data->stop = lf_end;
3618 if (pbi->max_threads > 1) {
3619 winterface->launch(&pbi->lf_worker);
3620 } else {
3621 winterface->execute(&pbi->lf_worker);
3622 }
3623 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003624#endif // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003625
3626 // After loopfiltering, the last 7 row pixels in each superblock row may
3627 // still be changed by the longest loopfilter of the next superblock row.
3628 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003629 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003630 }
3631
Jingning Han52ece882017-04-07 14:58:25 -07003632#if CONFIG_VAR_TX || CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -07003633 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003634 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
3635 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003636#else
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003637#if CONFIG_PARALLEL_DEBLOCKING
3638 // Loopfilter all rows in the frame in the frame.
3639 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3640 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3641 winterface->sync(&pbi->lf_worker);
3642 lf_data->start = 0;
3643 lf_data->stop = cm->mi_rows;
3644 winterface->execute(&pbi->lf_worker);
3645 }
3646#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003647 // Loopfilter remaining rows in the frame.
3648 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3649 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3650 winterface->sync(&pbi->lf_worker);
3651 lf_data->start = lf_data->stop;
3652 lf_data->stop = cm->mi_rows;
3653 winterface->execute(&pbi->lf_worker);
3654 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003655#endif // CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003656#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003657 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003658 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003659
3660#if CONFIG_EXT_TILE
3661 if (n_tiles == 1) {
3662#if CONFIG_ANS
3663 return data_end;
3664#else
3665 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07003666 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003667#endif // CONFIG_ANS
3668 } else {
3669 // Return the end of the last tile buffer
3670 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3671 }
3672#else
3673#if CONFIG_ANS
3674 return data_end;
3675#else
3676 {
3677 // Get last tile data.
3678 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003679 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003680 }
3681#endif // CONFIG_ANS
3682#endif // CONFIG_EXT_TILE
3683}
3684
3685static int tile_worker_hook(TileWorkerData *const tile_data,
3686 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003687 AV1Decoder *const pbi = tile_data->pbi;
3688 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003689 int mi_row, mi_col;
3690
3691 if (setjmp(tile_data->error_info.jmp)) {
3692 tile_data->error_info.setjmp = 0;
Angie Chiangd0916d92017-03-10 17:54:18 -08003693 aom_merge_corrupted_flag(&tile_data->xd.corrupted, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003694 return 0;
3695 }
3696
3697 tile_data->error_info.setjmp = 1;
3698 tile_data->xd.error_info = &tile_data->error_info;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003699#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08003700#if CONFIG_TILE_GROUPS
3701 if (!cm->dependent_horz_tiles || tile->tg_horz_boundary) {
3702#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003703 if (!cm->dependent_horz_tiles) {
Fangwen Fu73126c02017-02-08 22:37:47 -08003704#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003705 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
3706 }
3707#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003708 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08003709#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003710
3711 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
3712 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003713 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003714
3715 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3716 mi_col += cm->mib_size) {
3717 decode_partition(pbi, &tile_data->xd,
3718#if CONFIG_SUPERTX
3719 0,
3720#endif
3721 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
3722 b_width_log2_lookup[cm->sb_size]);
Yue Chen9ab6d712017-01-12 15:50:46 -08003723#if CONFIG_NCOBMC && CONFIG_MOTION_VAR
3724 detoken_and_recon_sb(pbi, &tile_data->xd, mi_row, mi_col,
3725 &tile_data->bit_reader, cm->sb_size);
3726#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003727 }
3728 }
3729 return !tile_data->xd.corrupted;
3730}
3731
3732// sorts in descending order
3733static int compare_tile_buffers(const void *a, const void *b) {
3734 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
3735 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
3736 return (int)(buf2->size - buf1->size);
3737}
3738
Yaowu Xuf883b422016-08-30 14:01:10 -07003739static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003740 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003741 AV1_COMMON *const cm = &pbi->common;
3742 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003743 const int tile_cols = cm->tile_cols;
3744 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003745 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
clang-format67948d32016-09-07 22:40:40 -07003746 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003747#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003748 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003749 const int single_row = pbi->dec_tile_row >= 0;
3750 const int tile_rows_start = single_row ? dec_tile_row : 0;
3751 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003752 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003753 const int single_col = pbi->dec_tile_col >= 0;
3754 const int tile_cols_start = single_col ? dec_tile_col : 0;
3755 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3756#else
3757 const int tile_rows_start = 0;
3758 const int tile_rows_end = tile_rows;
3759 const int tile_cols_start = 0;
3760 const int tile_cols_end = tile_cols;
3761#endif // CONFIG_EXT_TILE
3762 int tile_row, tile_col;
3763 int i;
3764
3765#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3766 int final_worker = -1;
3767#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3768
3769 assert(tile_rows <= MAX_TILE_ROWS);
3770 assert(tile_cols <= MAX_TILE_COLS);
3771
3772 assert(tile_cols * tile_rows > 1);
3773
Yaowu Xuc27fc142016-08-22 16:08:15 -07003774 // TODO(jzern): See if we can remove the restriction of passing in max
3775 // threads to the decoder.
3776 if (pbi->num_tile_workers == 0) {
3777 const int num_threads = pbi->max_threads & ~1;
3778 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07003779 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003780 // Ensure tile data offsets will be properly aligned. This may fail on
3781 // platforms without DECLARE_ALIGNED().
3782 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3783 CHECK_MEM_ERROR(
3784 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003785 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003786 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07003787 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003788 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003789 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003790 ++pbi->num_tile_workers;
3791
3792 winterface->init(worker);
3793 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003794 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003795 "Tile decoder thread creation failed");
3796 }
3797 }
3798 }
3799
3800 // Reset tile decoding hook
3801 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003802 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003803 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003804 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003805 worker->data1 = &pbi->tile_worker_data[i];
3806 worker->data2 = &pbi->tile_worker_info[i];
3807 }
3808
3809 // Initialize thread frame counts.
3810 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3811 for (i = 0; i < num_workers; ++i) {
3812 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003813 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003814 }
3815 }
3816
3817 // Load tile data into tile_buffers
3818 get_tile_buffers(pbi, data, data_end, tile_buffers);
3819
3820 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3821 // Sort the buffers in this tile row based on size in descending order.
3822 qsort(&tile_buffers[tile_row][tile_cols_start],
3823 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
3824 compare_tile_buffers);
3825
3826 // Rearrange the tile buffers in this tile row such that per-tile group
3827 // the largest, and presumably the most difficult tile will be decoded in
3828 // the main thread. This should help minimize the number of instances
3829 // where the main thread is waiting for a worker to complete.
3830 {
3831 int group_start;
3832 for (group_start = tile_cols_start; group_start < tile_cols_end;
3833 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003834 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003835 const TileBufferDec largest = tile_buffers[tile_row][group_start];
3836 memmove(&tile_buffers[tile_row][group_start],
3837 &tile_buffers[tile_row][group_start + 1],
3838 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
3839 tile_buffers[tile_row][group_end - 1] = largest;
3840 }
3841 }
3842
3843 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
3844 // Launch workers for individual columns
3845 for (i = 0; i < num_workers && tile_col < tile_cols_end;
3846 ++i, ++tile_col) {
3847 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07003848 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003849 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
3850 TileInfo *const tile_info = (TileInfo *)worker->data2;
3851
3852 twd->pbi = pbi;
3853 twd->xd = pbi->mb;
3854 twd->xd.corrupted = 0;
3855 twd->xd.counts =
3856 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3857 ? &twd->counts
3858 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003859 av1_zero(twd->dqcoeff);
3860 av1_tile_init(tile_info, cm, tile_row, buf->col);
3861 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003862 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Alex Converseeb780e72016-12-13 12:46:41 -08003863 &twd->bit_reader,
3864#if CONFIG_ANS && ANS_MAX_SYMBOLS
3865 1 << cm->ans_window_size_log2,
3866#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
3867 pbi->decrypt_cb, pbi->decrypt_state);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003868 av1_init_macroblockd(cm, &twd->xd,
3869#if CONFIG_PVQ
3870 twd->pvq_ref_coeff,
3871#endif
Luc Trudeauf8164152017-04-11 16:20:51 -04003872#if CONFIG_CFL
3873 &twd->cfl,
3874#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003875 twd->dqcoeff);
3876#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003877 daala_dec_init(cm, &twd->xd.daala_dec, &twd->bit_reader);
Yushin Choc49ef3a2017-03-13 17:27:25 -07003878 twd->xd.daala_dec.state.adapt = &twd->tctx.pvq_context;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003879#endif
Yushin Chod767beb2017-03-24 10:15:47 -07003880#if CONFIG_EC_ADAPT
3881 // Initialise the tile context from the frame context
3882 twd->tctx = *cm->fc;
3883 twd->xd.tile_ctx = &twd->tctx;
3884#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003885#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003886 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
3887 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003888#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003889
3890 worker->had_error = 0;
3891 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
3892 winterface->execute(worker);
3893 } else {
3894 winterface->launch(worker);
3895 }
3896
3897#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3898 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
3899 final_worker = i;
3900 }
3901#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3902 }
3903
3904 // Sync all workers
3905 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003906 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003907 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07003908 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07003909 // info in cm. Additionally once the threads have been synced and an
3910 // error is detected, there's no point in continuing to decode tiles.
3911 pbi->mb.corrupted |= !winterface->sync(worker);
3912 }
3913 }
3914 }
3915
3916 // Accumulate thread frame counts.
3917 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3918 for (i = 0; i < num_workers; ++i) {
3919 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003920 av1_accumulate_frame_counts(&cm->counts, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003921 }
3922 }
3923
3924#if CONFIG_EXT_TILE
3925 // Return the end of the last tile buffer
3926 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3927#else
3928#if CONFIG_ANS
3929 return data_end;
3930#else
3931 assert(final_worker != -1);
3932 {
3933 TileWorkerData *const twd =
3934 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003935 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003936 }
3937#endif // CONFIG_ANS
3938#endif // CONFIG_EXT_TILE
3939}
3940
3941static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003942 AV1_COMMON *const cm = (AV1_COMMON *)data;
3943 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003944}
3945
Yaowu Xuf883b422016-08-30 14:01:10 -07003946static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3947 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003948 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003949 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003950 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003951 cm->bit_depth = AOM_BITS_8;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003952 }
3953
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003954#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003955 if (cm->bit_depth > AOM_BITS_8) {
3956 cm->use_highbitdepth = 1;
3957 } else {
Yaowu Xu345a22d2017-02-27 09:23:52 -08003958#if CONFIG_LOWBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003959 cm->use_highbitdepth = 0;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003960#else
3961 cm->use_highbitdepth = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003962#endif
3963 }
Sebastien Alaiwan98378132017-01-04 11:23:09 +01003964#endif
3965
Yaowu Xuf883b422016-08-30 14:01:10 -07003966 cm->color_space = aom_rb_read_literal(rb, 3);
3967 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003968 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07003969 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003970 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003971 cm->subsampling_x = aom_rb_read_bit(rb);
3972 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003973 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003974 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003975 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07003976 if (aom_rb_read_bit(rb))
3977 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003978 "Reserved bit set");
3979 } else {
3980 cm->subsampling_y = cm->subsampling_x = 1;
3981 }
3982 } else {
3983 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
3984 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
3985 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
3986 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003987 if (aom_rb_read_bit(rb))
3988 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003989 "Reserved bit set");
3990 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003991 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003992 "4:4:4 color not supported in profile 0 or 2");
3993 }
3994 }
3995}
3996
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003997#if CONFIG_REFERENCE_BUFFER
3998void read_sequence_header(SequenceHeader *seq_params) {
3999 /* Placeholder for actually reading from the bitstream */
4000 seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
4001 seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
4002 seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
4003}
4004#endif
4005
Yaowu Xuf883b422016-08-30 14:01:10 -07004006static size_t read_uncompressed_header(AV1Decoder *pbi,
4007 struct aom_read_bit_buffer *rb) {
4008 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004009 MACROBLOCKD *const xd = &pbi->mb;
4010 BufferPool *const pool = cm->buffer_pool;
4011 RefCntBuffer *const frame_bufs = pool->frame_bufs;
4012 int i, mask, ref_index = 0;
4013 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004014
4015#if CONFIG_REFERENCE_BUFFER
4016 /* TODO: Move outside frame loop or inside key-frame branch */
4017 read_sequence_header(&pbi->seq_params);
4018#endif
4019
Yaowu Xuc27fc142016-08-22 16:08:15 -07004020 cm->last_frame_type = cm->frame_type;
4021 cm->last_intra_only = cm->intra_only;
4022
4023#if CONFIG_EXT_REFS
4024 // NOTE: By default all coded frames to be used as a reference
4025 cm->is_reference_frame = 1;
4026#endif // CONFIG_EXT_REFS
4027
Yaowu Xuf883b422016-08-30 14:01:10 -07004028 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
4029 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004030 "Invalid frame marker");
4031
Yaowu Xuf883b422016-08-30 14:01:10 -07004032 cm->profile = av1_read_profile(rb);
Sebastien Alaiwanb9c652a2017-05-03 15:44:28 +02004033
4034 const BITSTREAM_PROFILE MAX_SUPPORTED_PROFILE =
4035 CONFIG_HIGHBITDEPTH ? MAX_PROFILES : PROFILE_2;
4036
4037 if (cm->profile >= MAX_SUPPORTED_PROFILE)
Yaowu Xuf883b422016-08-30 14:01:10 -07004038 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004039 "Unsupported bitstream profile");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004040
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
Yaowu Xuc27fc142016-08-22 16:08:15 -07004277 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004278 get_frame_new_buffer(cm)->color_space = cm->color_space;
4279 get_frame_new_buffer(cm)->color_range = cm->color_range;
4280 get_frame_new_buffer(cm)->render_width = cm->render_width;
4281 get_frame_new_buffer(cm)->render_height = cm->render_height;
4282
4283 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004284 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004285 "Keyframe / intra-only frame required to reset decoder"
4286 " state");
4287 }
4288
4289 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004290 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004291 ? REFRESH_FRAME_CONTEXT_FORWARD
4292 : REFRESH_FRAME_CONTEXT_BACKWARD;
4293 } else {
4294 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
4295 }
4296
Yaowu Xuf883b422016-08-30 14:01:10 -07004297 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07004298 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07004299 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004300
4301 // Generate next_ref_frame_map.
4302 lock_buffer_pool(pool);
4303 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
4304 if (mask & 1) {
4305 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
4306 ++frame_bufs[cm->new_fb_idx].ref_count;
4307 } else {
4308 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4309 }
4310 // Current thread holds the reference frame.
4311 if (cm->ref_frame_map[ref_index] >= 0)
4312 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4313 ++ref_index;
4314 }
4315
4316 for (; ref_index < REF_FRAMES; ++ref_index) {
4317 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4318
4319 // Current thread holds the reference frame.
4320 if (cm->ref_frame_map[ref_index] >= 0)
4321 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4322 }
4323 unlock_buffer_pool(pool);
4324 pbi->hold_ref_buf = 1;
4325
4326 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07004327 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004328
4329#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07004330 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004331#else
4332 set_sb_size(cm, BLOCK_64X64);
4333#endif // CONFIG_EXT_PARTITION
4334
4335 setup_loopfilter(cm, rb);
Jean-Marc Valin01435132017-02-18 14:12:53 -05004336#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01004337 setup_cdef(cm, rb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004338#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004339#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004340 decode_restoration_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004341#endif // CONFIG_LOOP_RESTORATION
4342 setup_quantization(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004343 xd->bd = (int)cm->bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004344
hui su0d103572017-03-01 17:58:01 -08004345#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuf883b422016-08-30 14:01:10 -07004346 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004347 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4348 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4349 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4350 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
4351 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4352 }
hui su0d103572017-03-01 17:58:01 -08004353#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004354
4355 setup_segmentation(cm, rb);
4356
Arild Fuldseth07441162016-08-15 15:07:52 +02004357#if CONFIG_DELTA_Q
4358 {
4359 struct segmentation *const seg = &cm->seg;
4360 int segment_quantizer_active = 0;
4361 for (i = 0; i < MAX_SEGMENTS; i++) {
4362 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
4363 segment_quantizer_active = 1;
4364 }
4365 }
4366
Thomas Daviesf6936102016-09-05 16:51:31 +01004367 cm->delta_q_res = 1;
Fangwen Fu231fe422017-04-24 17:52:29 -07004368#if CONFIG_EXT_DELTA_Q
4369 cm->delta_lf_res = 1;
4370#endif
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01004371 if (segment_quantizer_active == 0 && cm->base_qindex > 0) {
Arild Fuldseth07441162016-08-15 15:07:52 +02004372 cm->delta_q_present_flag = aom_rb_read_bit(rb);
4373 } else {
4374 cm->delta_q_present_flag = 0;
4375 }
4376 if (cm->delta_q_present_flag) {
4377 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01004378 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Fangwen Fu231fe422017-04-24 17:52:29 -07004379#if CONFIG_EXT_DELTA_Q
4380 if (segment_quantizer_active) {
4381 assert(seg->abs_delta == SEGMENT_DELTADATA);
4382 }
4383 cm->delta_lf_present_flag = aom_rb_read_bit(rb);
4384 if (cm->delta_lf_present_flag) {
4385 xd->prev_delta_lf_from_base = 0;
4386 cm->delta_lf_res = 1 << aom_rb_read_literal(rb, 2);
4387 } else {
4388 cm->delta_lf_present_flag = 0;
4389 }
4390#endif // CONFIG_EXT_DELTA_Q
Arild Fuldseth07441162016-08-15 15:07:52 +02004391 }
4392 }
4393#endif
4394
Urvang Joshi454280d2016-10-14 16:51:44 -07004395 for (i = 0; i < MAX_SEGMENTS; ++i) {
4396 const int qindex = cm->seg.enabled
4397 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
4398 : cm->base_qindex;
4399 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
4400 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4401 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004402 }
4403
4404 setup_segmentation_dequant(cm);
Yue Cheneeacc4c2017-01-17 17:29:17 -08004405 cm->tx_mode = read_tx_mode(cm, xd, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004406 cm->reference_mode = read_frame_reference_mode(cm, rb);
4407
Sarah Parkere68a3e42017-02-16 14:03:24 -08004408#if CONFIG_EXT_TX
4409 cm->reduced_tx_set_used = aom_rb_read_bit(rb);
4410#endif // CONFIG_EXT_TX
4411
Yaowu Xuc27fc142016-08-22 16:08:15 -07004412 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004413 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004414
4415 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07004416 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004417 "Invalid header size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004418 return sz;
4419}
4420
4421#if CONFIG_EXT_TX
Sarah Parkerb926f322017-04-24 16:19:48 -07004422#if !CONFIG_EC_ADAPT
Yaowu Xuf883b422016-08-30 14:01:10 -07004423static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004424 int i, j, k;
4425 int s;
4426 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004427 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004428 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4429 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004430 for (j = 0; j < num_ext_tx_set[ext_tx_set_type_inter[s]] - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004431 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004432 }
4433 }
4434 }
4435
4436 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004437 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004438 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4439 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
4440 for (j = 0; j < INTRA_MODES; ++j)
Debargha Mukherjee08542b92017-02-21 01:08:14 -08004441 for (k = 0; k < num_ext_tx_set[ext_tx_set_type_intra[s]] - 1; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004442 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
4443 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004444 }
4445 }
4446 }
4447}
Sarah Parkerb926f322017-04-24 16:19:48 -07004448#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004449#else
4450
Yaowu Xuc27fc142016-08-22 16:08:15 -07004451#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004452#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07004453static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004454 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -07004455 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004456 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
Jingning Hanfeb517c2016-12-21 16:02:07 -08004457 for (j = TX_8X8; j < TX_SIZES; ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004458 av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004459 }
4460 }
4461 }
4462}
4463#endif // CONFIG_SUPERTX
4464
4465#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +00004466static void read_global_motion_params(WarpedMotionParams *params,
Sarah Parkerf1783292017-04-05 11:55:27 -07004467 WarpedMotionParams *ref_params,
Sarah Parker13d06622017-03-10 17:03:28 -08004468 aom_prob *probs, aom_reader *r,
4469 int allow_hp) {
David Barkercf3d0b02016-11-10 10:14:49 +00004470 TransformationType type =
Michael Bebenita6048d052016-08-25 14:40:54 -07004471 aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
Sarah Parker13d06622017-03-10 17:03:28 -08004472 int trans_bits;
4473 int trans_dec_factor;
Sarah Parkerf1783292017-04-05 11:55:27 -07004474 int trans_prec_diff;
Sarah Parker4c10a3c2017-04-10 19:37:59 -07004475 set_default_warp_params(params);
David Barkercf3d0b02016-11-10 10:14:49 +00004476 params->wmtype = type;
4477 switch (type) {
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004478 case HOMOGRAPHY:
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004479 case HORTRAPEZOID:
4480 case VERTRAPEZOID:
4481 if (type != HORTRAPEZOID)
4482 params->wmmat[6] =
Sarah Parkerf1783292017-04-05 11:55:27 -07004483 aom_read_signed_primitive_refsubexpfin(
4484 r, GM_ROW3HOMO_MAX + 1, SUBEXPFIN_K,
4485 (ref_params->wmmat[6] >> GM_ROW3HOMO_PREC_DIFF)) *
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004486 GM_ROW3HOMO_DECODE_FACTOR;
4487 if (type != VERTRAPEZOID)
4488 params->wmmat[7] =
Sarah Parkerf1783292017-04-05 11:55:27 -07004489 aom_read_signed_primitive_refsubexpfin(
4490 r, GM_ROW3HOMO_MAX + 1, SUBEXPFIN_K,
4491 (ref_params->wmmat[7] >> GM_ROW3HOMO_PREC_DIFF)) *
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004492 GM_ROW3HOMO_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004493 case AFFINE:
4494 case ROTZOOM:
Sarah Parkerf1783292017-04-05 11:55:27 -07004495 params->wmmat[2] = aom_read_signed_primitive_refsubexpfin(
4496 r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4497 (ref_params->wmmat[2] >> GM_ALPHA_PREC_DIFF) -
4498 (1 << GM_ALPHA_PREC_BITS)) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004499 GM_ALPHA_DECODE_FACTOR +
David Barkercf3d0b02016-11-10 10:14:49 +00004500 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004501 if (type != VERTRAPEZOID)
Sarah Parkerf1783292017-04-05 11:55:27 -07004502 params->wmmat[3] = aom_read_signed_primitive_refsubexpfin(
4503 r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4504 (ref_params->wmmat[3] >> GM_ALPHA_PREC_DIFF)) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004505 GM_ALPHA_DECODE_FACTOR;
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -08004506 if (type >= AFFINE) {
4507 if (type != HORTRAPEZOID)
Sarah Parkerf1783292017-04-05 11:55:27 -07004508 params->wmmat[4] = aom_read_signed_primitive_refsubexpfin(
4509 r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4510 (ref_params->wmmat[4] >> GM_ALPHA_PREC_DIFF)) *
4511 GM_ALPHA_DECODE_FACTOR;
4512 params->wmmat[5] = aom_read_signed_primitive_refsubexpfin(
4513 r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4514 (ref_params->wmmat[5] >> GM_ALPHA_PREC_DIFF) -
4515 (1 << GM_ALPHA_PREC_BITS)) *
David Barkercf3d0b02016-11-10 10:14:49 +00004516 GM_ALPHA_DECODE_FACTOR +
4517 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004518 } else {
David Barkercf3d0b02016-11-10 10:14:49 +00004519 params->wmmat[4] = -params->wmmat[3];
4520 params->wmmat[5] = params->wmmat[2];
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004521 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004522 // fallthrough intended
David Barkercf3d0b02016-11-10 10:14:49 +00004523 case TRANSLATION:
Sarah Parker13d06622017-03-10 17:03:28 -08004524 trans_bits = (type == TRANSLATION) ? GM_ABS_TRANS_ONLY_BITS - !allow_hp
4525 : GM_ABS_TRANS_BITS;
4526 trans_dec_factor = (type == TRANSLATION)
4527 ? GM_TRANS_ONLY_DECODE_FACTOR * (1 << !allow_hp)
4528 : GM_TRANS_DECODE_FACTOR;
Sarah Parkerf1783292017-04-05 11:55:27 -07004529 trans_prec_diff = (type == TRANSLATION)
4530 ? GM_TRANS_ONLY_PREC_DIFF + !allow_hp
4531 : GM_TRANS_PREC_DIFF;
4532 params->wmmat[0] = aom_read_signed_primitive_refsubexpfin(
4533 r, (1 << trans_bits) + 1, SUBEXPFIN_K,
4534 (ref_params->wmmat[0] >> trans_prec_diff)) *
4535 trans_dec_factor;
4536 params->wmmat[1] = aom_read_signed_primitive_refsubexpfin(
4537 r, (1 << trans_bits) + 1, SUBEXPFIN_K,
4538 (ref_params->wmmat[1] >> trans_prec_diff)) *
4539 trans_dec_factor;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004540 case IDENTITY: break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004541 default: assert(0);
4542 }
Debargha Mukherjee3b6c5442017-03-30 08:22:00 -07004543 if (params->wmtype <= AFFINE)
4544 if (!get_shear_params(params)) assert(0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004545}
4546
Yaowu Xuf883b422016-08-30 14:01:10 -07004547static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004548 int frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004549 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
Sarah Parkerf1783292017-04-05 11:55:27 -07004550 read_global_motion_params(
4551 &cm->global_motion[frame], &cm->prev_frame->global_motion[frame],
4552 cm->fc->global_motion_types_prob, r, cm->allow_high_precision_mv);
Sarah Parkere5299862016-08-16 14:57:37 -07004553 /*
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004554 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4555 frame, cm->current_video_frame, cm->show_frame,
David Barkercf3d0b02016-11-10 10:14:49 +00004556 cm->global_motion[frame].wmmat[0],
4557 cm->global_motion[frame].wmmat[1],
4558 cm->global_motion[frame].wmmat[2],
4559 cm->global_motion[frame].wmmat[3]);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004560 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004561 }
Sarah Parkerf1783292017-04-05 11:55:27 -07004562 memcpy(cm->cur_frame->global_motion, cm->global_motion,
4563 TOTAL_REFS_PER_FRAME * sizeof(WarpedMotionParams));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004564}
4565#endif // CONFIG_GLOBAL_MOTION
4566
Yaowu Xuf883b422016-08-30 14:01:10 -07004567static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004568 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004569 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004570#if CONFIG_SUPERTX
4571 MACROBLOCKD *const xd = &pbi->mb;
4572#endif
4573 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07004574 aom_reader r;
Yaowu Xu8af861b2016-11-01 12:12:11 -07004575 int k, i;
Thomas Davies493623e2017-03-31 16:12:25 +01004576#if !CONFIG_EC_ADAPT || \
4577 (CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION || CONFIG_EXT_INTER)
Yaowu Xu8af861b2016-11-01 12:12:11 -07004578 int j;
4579#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004580
Alex Converse2cdf0d82016-12-13 13:53:09 -08004581#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08004582 r.window_size = 1 << cm->ans_window_size_log2;
Alex Converse2cdf0d82016-12-13 13:53:09 -08004583#endif
Alex Converse346440b2017-01-03 13:47:37 -08004584 if (aom_reader_init(&r, data, partition_size, pbi->decrypt_cb,
4585 pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07004586 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004587 "Failed to allocate bool decoder 0");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004588
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004589#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004590 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4591 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4592 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4593 av1_alloc_restoration_buffers(cm);
4594 decode_restoration(cm, &r);
4595 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004596#endif
4597
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004598#if !CONFIG_EC_ADAPT
Yaowu Xuefc75352016-10-31 09:46:42 -07004599 if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
Nathan E. Eggeb353a8e2017-02-17 10:27:37 -05004600#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004601
Angie Chiang800df032017-03-22 11:14:12 -07004602#if CONFIG_LV_MAP
4603 av1_read_txb_probs(fc, cm->tx_mode, &r);
4604#else // CONFIG_LV_MAP
Yushin Cho77bba8d2016-11-04 16:36:56 -07004605#if !CONFIG_PVQ
Alex Conversea9598cd2017-02-03 14:18:05 -08004606#if !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004607 read_coef_probs(fc, cm->tx_mode, &r);
Angie Chiang800df032017-03-22 11:14:12 -07004608#endif // !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET)
Angie Chiang7d7ead92017-03-22 10:35:51 -07004609#endif // !CONFIG_PVQ
Angie Chiang800df032017-03-22 11:14:12 -07004610#endif // CONFIG_LV_MAP
4611
Yaowu Xuc27fc142016-08-22 16:08:15 -07004612#if CONFIG_VAR_TX
4613 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004614 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
Yushin Cho77bba8d2016-11-04 16:36:56 -07004615#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004616 for (k = 0; k < SKIP_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004617 av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004618
Thomas Daviesd6ee8a82017-03-02 14:42:50 +00004619#if CONFIG_DELTA_Q && !CONFIG_EC_ADAPT
Fangwen Fu06173632017-04-26 13:42:08 -07004620#if CONFIG_EXT_DELTA_Q
Fangwen Fu6160df22017-04-24 09:45:51 -07004621 if (cm->delta_q_present_flag) {
4622 for (k = 0; k < DELTA_Q_PROBS; ++k)
4623 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
4624 }
Fangwen Fu231fe422017-04-24 17:52:29 -07004625 if (cm->delta_lf_present_flag) {
4626 for (k = 0; k < DELTA_LF_PROBS; ++k)
4627 av1_diff_update_prob(&r, &fc->delta_lf_prob[k], ACCT_STR);
4628 }
Fangwen Fu06173632017-04-26 13:42:08 -07004629#else
4630 for (k = 0; k < DELTA_Q_PROBS; ++k)
4631 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
Fangwen Fu231fe422017-04-24 17:52:29 -07004632#endif
Thomas Daviesf6936102016-09-05 16:51:31 +01004633#endif
4634
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004635#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004636 if (cm->seg.enabled && cm->seg.update_map) {
4637 if (cm->seg.temporal_update) {
4638 for (k = 0; k < PREDICTION_PROBS; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004639 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004640 }
4641 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004642 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004643 }
4644
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004645 for (j = 0; j < INTRA_MODES; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004646 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004647 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004648 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004649
4650#if CONFIG_EXT_PARTITION_TYPES
Alex Converse4e18d402017-03-14 15:36:38 -07004651 for (j = 0; j < PARTITION_PLOFFSET; ++j)
4652 for (i = 0; i < PARTITION_TYPES - 1; ++i)
4653 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
4654 for (; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004655 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004656 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004657#else
Alex Converse55c6bde2017-01-12 15:55:31 -08004658 for (j = 0; j < PARTITION_CONTEXTS_PRIMARY; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004659 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004660 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004661#endif // CONFIG_EXT_PARTITION_TYPES
hui su9aa97492017-01-26 16:46:01 -08004662
Alex Converse55c6bde2017-01-12 15:55:31 -08004663#if CONFIG_UNPOISON_PARTITION_CTX
4664 for (; j < PARTITION_CONTEXTS_PRIMARY + PARTITION_BLOCK_SIZES; ++j)
4665 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_VERT], ACCT_STR);
4666 for (; j < PARTITION_CONTEXTS_PRIMARY + 2 * PARTITION_BLOCK_SIZES; ++j)
4667 av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_HORZ], ACCT_STR);
4668#endif // CONFIG_UNPOISON_PARTITION_CTX
hui su9aa97492017-01-26 16:46:01 -08004669
4670#if CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004671 for (i = 0; i < INTRA_FILTERS + 1; ++i)
4672 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004673 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
hui su9aa97492017-01-26 16:46:01 -08004674#endif // CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
hui sub4e25d22017-03-09 15:32:30 -08004675#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004676
4677 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004678 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Nathan E. Egge10ba2be2016-11-16 09:44:26 -05004679#if CONFIG_EC_MULTISYMBOL
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00004680 av1_copy(cm->fc->kf_y_cdf, av1_kf_y_mode_cdf);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004681#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004682#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004683 for (k = 0; k < INTRA_MODES; k++)
Thomas Davies6519beb2016-10-19 14:46:07 +01004684 for (j = 0; j < INTRA_MODES; j++)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004685 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004686 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004687#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004688 } else {
4689#if !CONFIG_REF_MV
4690 nmv_context *const nmvc = &fc->nmvc;
4691#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004692 read_inter_mode_probs(fc, &r);
4693
4694#if CONFIG_EXT_INTER
4695 read_inter_compound_mode_probs(fc, &r);
4696 if (cm->reference_mode != COMPOUND_REFERENCE) {
4697 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4698 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004699 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004700 }
4701 }
4702 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4703 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004704 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004705 }
4706 for (i = 0; i < BLOCK_SIZES; i++) {
4707 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004708 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004709 }
4710 }
4711 }
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00004712#if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004713 if (cm->reference_mode != SINGLE_REFERENCE) {
4714 for (i = 0; i < BLOCK_SIZES; i++) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08004715 for (j = 0; j < COMPOUND_TYPES - 1; j++) {
4716 av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004717 }
4718 }
4719 }
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00004720#endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Yaowu Xuc27fc142016-08-22 16:08:15 -07004721#endif // CONFIG_EXT_INTER
4722
Yue Chencb60b182016-10-13 15:18:22 -07004723#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004724 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
Yue Chencb60b182016-10-13 15:18:22 -07004725 for (j = 0; j < MOTION_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004726 av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004727 }
Yue Chencb60b182016-10-13 15:18:22 -07004728#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004729
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004730#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004731 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004732#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004733
4734 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004735 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004736
4737 if (cm->reference_mode != SINGLE_REFERENCE)
4738 setup_compound_reference_mode(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004739 read_frame_reference_mode_probs(cm, &r);
4740
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004741#if !CONFIG_EC_ADAPT
Nathan E. Egge5710c722016-09-08 10:01:16 -04004742 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004743 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004744 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
Nathan E. Egge5710c722016-09-08 10:01:16 -04004745 }
Thomas9ac55082016-09-23 18:04:17 +01004746#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004747
4748#if CONFIG_REF_MV
4749 for (i = 0; i < NMV_CONTEXTS; ++i)
4750 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
4751#else
4752 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
4753#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004754#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004755 read_ext_tx_probs(fc, &r);
Sarah Parkerb926f322017-04-24 16:19:48 -07004756#endif // EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004757#if CONFIG_SUPERTX
4758 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
4759#endif
4760#if CONFIG_GLOBAL_MOTION
4761 read_global_motion(cm, &r);
Thomas Davies6519beb2016-10-19 14:46:07 +01004762#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004763 }
Thomas Davies028b57f2017-02-22 16:42:11 +00004764#if CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Thomas Davies87aeeb82017-02-17 00:19:40 +00004765#if CONFIG_NEW_TOKENSET
4766 av1_coef_head_cdfs(fc);
4767#endif
4768 /* Make tail distribution from head */
Thomas Davies6519beb2016-10-19 14:46:07 +01004769 av1_coef_pareto_cdfs(fc);
David Barker599dfd02016-11-10 13:20:12 +00004770#if CONFIG_REF_MV
4771 for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
4772#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004773 av1_set_mv_cdfs(&fc->nmvc);
David Barker599dfd02016-11-10 13:20:12 +00004774#endif
Thomas Davies6519beb2016-10-19 14:46:07 +01004775 av1_set_mode_cdfs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00004776#endif // CONFIG_EC_MULTISYMBOL && !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004777
Yaowu Xuf883b422016-08-30 14:01:10 -07004778 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004779}
4780
4781#ifdef NDEBUG
4782#define debug_check_frame_counts(cm) (void)0
4783#else // !NDEBUG
4784// Counts should only be incremented when frame_parallel_decoding_mode and
4785// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07004786static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004787 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07004788 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004789 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
4790 cm->error_resilient_mode);
4791 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
4792 sizeof(cm->counts.y_mode)));
4793 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
4794 sizeof(cm->counts.uv_mode)));
4795 assert(!memcmp(cm->counts.partition, zero_counts.partition,
4796 sizeof(cm->counts.partition)));
4797 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
4798 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
4799 sizeof(cm->counts.eob_branch)));
Thomas Daviesab780672017-02-01 12:07:29 +00004800#if CONFIG_EC_MULTISYMBOL
4801 assert(!memcmp(cm->counts.blockz_count, zero_counts.blockz_count,
4802 sizeof(cm->counts.blockz_count)));
4803#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004804 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
4805 sizeof(cm->counts.switchable_interp)));
4806 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
4807 sizeof(cm->counts.inter_mode)));
4808#if CONFIG_EXT_INTER
4809 assert(!memcmp(cm->counts.inter_compound_mode,
4810 zero_counts.inter_compound_mode,
4811 sizeof(cm->counts.inter_compound_mode)));
4812 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
4813 sizeof(cm->counts.interintra)));
4814 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
4815 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08004816 assert(!memcmp(cm->counts.compound_interinter,
4817 zero_counts.compound_interinter,
4818 sizeof(cm->counts.compound_interinter)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004819#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07004820#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
4821 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
4822 sizeof(cm->counts.motion_mode)));
4823#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004824 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
4825 sizeof(cm->counts.intra_inter)));
4826 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
4827 sizeof(cm->counts.comp_inter)));
4828 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
4829 sizeof(cm->counts.single_ref)));
4830 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
4831 sizeof(cm->counts.comp_ref)));
4832#if CONFIG_EXT_REFS
4833 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
4834 sizeof(cm->counts.comp_bwdref)));
4835#endif // CONFIG_EXT_REFS
4836 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
4837 sizeof(cm->counts.tx_size)));
4838 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
4839#if CONFIG_REF_MV
4840 assert(
4841 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
4842 assert(
4843 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
4844#else
4845 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
4846#endif
4847 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
4848 sizeof(cm->counts.inter_ext_tx)));
4849 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
4850 sizeof(cm->counts.intra_ext_tx)));
4851}
4852#endif // NDEBUG
4853
Yaowu Xuf883b422016-08-30 14:01:10 -07004854static struct aom_read_bit_buffer *init_read_bit_buffer(
4855 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
4856 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004857 rb->bit_offset = 0;
4858 rb->error_handler = error_handler;
4859 rb->error_handler_data = &pbi->common;
4860 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004861 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004862 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
4863 rb->bit_buffer = clear_data;
4864 rb->bit_buffer_end = clear_data + n;
4865 } else {
4866 rb->bit_buffer = data;
4867 rb->bit_buffer_end = data_end;
4868 }
4869 return rb;
4870}
4871
4872//------------------------------------------------------------------------------
4873
Yaowu Xuf883b422016-08-30 14:01:10 -07004874int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
4875 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
4876 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
4877 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004878}
4879
Yaowu Xuf883b422016-08-30 14:01:10 -07004880void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
4881 int *height) {
4882 *width = aom_rb_read_literal(rb, 16) + 1;
4883 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004884}
4885
Yaowu Xuf883b422016-08-30 14:01:10 -07004886BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
4887 int profile = aom_rb_read_bit(rb);
4888 profile |= aom_rb_read_bit(rb) << 1;
4889 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004890 return (BITSTREAM_PROFILE)profile;
4891}
4892
Thomas Davies028b57f2017-02-22 16:42:11 +00004893#if CONFIG_EC_ADAPT
Yaowu Xu4ff59b52017-04-24 12:41:56 -07004894static void make_update_tile_list_dec(AV1Decoder *pbi, int tile_rows,
4895 int tile_cols, FRAME_CONTEXT *ec_ctxs[]) {
Thomas Davies028b57f2017-02-22 16:42:11 +00004896 int i;
4897 for (i = 0; i < tile_rows * tile_cols; ++i)
4898 ec_ctxs[i] = &pbi->tile_data[i].tctx;
4899}
4900#endif
4901
Yaowu Xuf883b422016-08-30 14:01:10 -07004902void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
4903 const uint8_t *data_end, const uint8_t **p_data_end) {
4904 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004905 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07004906 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004907 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004908 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004909 size_t first_partition_size;
4910 YV12_BUFFER_CONFIG *new_fb;
4911
Yi Luof8e87b42017-04-14 17:20:27 -07004912#if CONFIG_ADAPT_SCAN
4913 av1_deliver_eob_threshold(cm, xd);
4914#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004915#if CONFIG_BITSTREAM_DEBUG
4916 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
4917#endif
4918
4919 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004920 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08004921
4922#if CONFIG_EXT_TILE
4923 // If cm->tile_encoding_mode == TILE_NORMAL, the independent decoding of a
4924 // single tile or a section of a frame is not allowed.
4925 if (!cm->tile_encoding_mode &&
4926 (pbi->dec_tile_row >= 0 || pbi->dec_tile_col >= 0)) {
4927 pbi->dec_tile_row = -1;
4928 pbi->dec_tile_col = -1;
4929 }
4930#endif // CONFIG_EXT_TILE
4931
Thomas Davies72712e62016-11-09 12:17:51 +00004932#if CONFIG_TILE_GROUPS
4933 pbi->first_partition_size = first_partition_size;
4934 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
4935#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004936 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004937 xd->cur_buf = new_fb;
Alex Conversee816b312017-05-01 09:51:24 -07004938#if CONFIG_INTRABC
4939#if CONFIG_HIGHBITDEPTH
4940 av1_setup_scale_factors_for_frame(
4941 &xd->sf_identity, xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height,
4942 xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height,
4943 cm->use_highbitdepth);
4944#else
4945 av1_setup_scale_factors_for_frame(
4946 &xd->sf_identity, xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height,
4947 xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height);
4948#endif // CONFIG_HIGHBITDEPTH
4949#endif // CONFIG_INTRABC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004950#if CONFIG_GLOBAL_MOTION
Sarah Parkerf1783292017-04-05 11:55:27 -07004951 int i;
4952 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Sarah Parker4c10a3c2017-04-10 19:37:59 -07004953 set_default_warp_params(&cm->global_motion[i]);
4954 set_default_warp_params(&cm->cur_frame->global_motion[i]);
Sarah Parkerf1783292017-04-05 11:55:27 -07004955 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004956 xd->global_motion = cm->global_motion;
4957#endif // CONFIG_GLOBAL_MOTION
4958
4959 if (!first_partition_size) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004960 // showing a frame directly
4961 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004962 return;
4963 }
4964
Yaowu Xuf883b422016-08-30 14:01:10 -07004965 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004966 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07004967 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004968 "Truncated packet or corrupt header length");
4969
Jingning Han24e0a182016-11-20 22:34:12 -08004970#if CONFIG_REF_MV
Dengca8d24d2016-10-17 14:06:35 +08004971 cm->setup_mi(cm);
4972#endif
4973
Fangwen Fu8d164de2016-12-14 13:40:54 -08004974#if CONFIG_TEMPMV_SIGNALING
4975 if (cm->use_prev_frame_mvs) {
4976 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4977 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4978 assert(!cm->error_resilient_mode &&
4979 cm->width == last_fb_ref_buf->buf->y_width &&
4980 cm->height == last_fb_ref_buf->buf->y_height &&
4981 !cm->prev_frame->intra_only);
4982 }
4983#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004984 cm->use_prev_frame_mvs =
4985 !cm->error_resilient_mode && cm->width == cm->last_width &&
4986 cm->height == cm->last_height && !cm->last_intra_only &&
4987 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004988#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004989#if CONFIG_EXT_REFS
4990 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
4991 // show_exisiting_frame=1, nor can it take a frame not used as
4992 // a reference, it is probable that by the time it is being
4993 // referred to, the frame buffer it originally points to may
4994 // already get expired and have been reassigned to the current
4995 // newly coded frame. Hence, we need to check whether this is
4996 // the case, and if yes, we have 2 choices:
4997 // (1) Simply disable the use of previous frame mvs; or
4998 // (2) Have cm->prev_frame point to one reference frame buffer,
4999 // e.g. LAST_FRAME.
5000 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
5001 // Reassign the LAST_FRAME buffer to cm->prev_frame.
5002 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
5003 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
5004 }
5005#endif // CONFIG_EXT_REFS
5006
Yaowu Xuf883b422016-08-30 14:01:10 -07005007 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005008
5009 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
Thomas Daede10e1da92017-04-26 13:22:21 -07005010 cm->pre_fc = &cm->frame_contexts[cm->frame_context_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005011 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07005012 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005013 "Uninitialized entropy context.");
5014
Yaowu Xuf883b422016-08-30 14:01:10 -07005015 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005016
5017 xd->corrupted = 0;
5018 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
5019 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07005020 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005021 "Decode failed. Frame data header is corrupted.");
5022
5023 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005024 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005025 }
5026
5027 // If encoded in frame parallel mode, frame context is ready after decoding
5028 // the frame header.
5029 if (cm->frame_parallel_decode &&
5030 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005031 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005032 FrameWorkerData *const frame_worker_data = worker->data1;
5033 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
5034 context_updated = 1;
5035 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
5036 }
Yaowu Xuf883b422016-08-30 14:01:10 -07005037 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005038 pbi->cur_buf->row = -1;
5039 pbi->cur_buf->col = -1;
5040 frame_worker_data->frame_context_ready = 1;
5041 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07005042 av1_frameworker_signal_stats(worker);
5043 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005044 }
5045
Jingning Han52ece882017-04-07 14:58:25 -07005046 if (pbi->max_threads > 1 && !CONFIG_CB4X4 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005047#if CONFIG_EXT_TILE
Jingning Han52ece882017-04-07 14:58:25 -07005048 pbi->dec_tile_col < 0 && // Decoding all columns
Yaowu Xuc27fc142016-08-22 16:08:15 -07005049#endif // CONFIG_EXT_TILE
Jingning Han52ece882017-04-07 14:58:25 -07005050 cm->tile_cols > 1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005051 // Multi-threaded tile decoder
5052 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
5053 if (!xd->corrupted) {
5054 if (!cm->skip_loop_filter) {
5055 // If multiple threads are used to decode tiles, then we use those
5056 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07005057 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
5058 0, 0, pbi->tile_workers, pbi->num_tile_workers,
5059 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005060 }
5061 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005062 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005063 "Decode failed. Frame data is corrupted.");
5064 }
5065 } else {
5066 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
5067 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005068
Jean-Marc Valin01435132017-02-18 14:12:53 -05005069#if CONFIG_CDEF
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04005070 if (!cm->skip_loop_filter) {
Jean-Marc Valine9f77422017-03-22 17:09:51 -04005071 av1_cdef_frame(&pbi->cur_buf->buf, cm, &pbi->mb);
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02005072 }
Debargha Mukherjee00c54332017-03-03 15:44:17 -08005073#endif // CONFIG_CDEF
5074
5075#if CONFIG_LOOP_RESTORATION
5076 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
5077 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
5078 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
5079 av1_loop_restoration_frame(new_fb, cm, cm->rst_info, 7, 0, NULL);
5080 }
5081#endif // CONFIG_LOOP_RESTORATION
Thomas Daedef56859f2016-04-19 16:57:24 -07005082
Yaowu Xuc27fc142016-08-22 16:08:15 -07005083 if (!xd->corrupted) {
5084 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Thomas Davies028b57f2017-02-22 16:42:11 +00005085#if CONFIG_EC_ADAPT
Thomas Davies493623e2017-03-31 16:12:25 +01005086 FRAME_CONTEXT **tile_ctxs = aom_malloc(cm->tile_rows * cm->tile_cols *
5087 sizeof(&pbi->tile_data[0].tctx));
5088 aom_cdf_prob **cdf_ptrs =
5089 aom_malloc(cm->tile_rows * cm->tile_cols *
5090 sizeof(&pbi->tile_data[0].tctx.partition_cdf[0][0]));
Thomas Davies028b57f2017-02-22 16:42:11 +00005091 make_update_tile_list_dec(pbi, cm->tile_rows, cm->tile_cols, tile_ctxs);
5092#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07005093 av1_adapt_coef_probs(cm);
5094 av1_adapt_intra_frame_probs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00005095#if CONFIG_EC_ADAPT
Thomas Davies493623e2017-03-31 16:12:25 +01005096 av1_average_tile_coef_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005097 cm->tile_rows * cm->tile_cols);
Thomas Davies493623e2017-03-31 16:12:25 +01005098 av1_average_tile_intra_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005099 cm->tile_rows * cm->tile_cols);
Yushin Chob188ea12017-03-13 13:45:23 -07005100#if CONFIG_PVQ
5101 av1_average_tile_pvq_cdfs(pbi->common.fc, tile_ctxs,
5102 cm->tile_rows * cm->tile_cols);
5103#endif // CONFIG_PVQ
5104#endif // CONFIG_EC_ADAPT
hui suff0da2b2017-03-07 15:51:37 -08005105#if CONFIG_ADAPT_SCAN
5106 av1_adapt_scan_order(cm);
5107#endif // CONFIG_ADAPT_SCAN
Yaowu Xuc27fc142016-08-22 16:08:15 -07005108
5109 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005110 av1_adapt_inter_frame_probs(cm);
5111 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Thomas Davies028b57f2017-02-22 16:42:11 +00005112#if CONFIG_EC_ADAPT
5113 av1_average_tile_inter_cdfs(&pbi->common, pbi->common.fc, tile_ctxs,
Thomas Davies493623e2017-03-31 16:12:25 +01005114 cdf_ptrs, cm->tile_rows * cm->tile_cols);
5115 av1_average_tile_mv_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005116 cm->tile_rows * cm->tile_cols);
5117#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005118 }
Thomas Davies493623e2017-03-31 16:12:25 +01005119#if CONFIG_EC_ADAPT
5120 aom_free(tile_ctxs);
5121 aom_free(cdf_ptrs);
5122#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005123 } else {
5124 debug_check_frame_counts(cm);
5125 }
5126 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005127 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005128 "Decode failed. Frame data is corrupted.");
5129 }
5130
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05005131#if CONFIG_INSPECTION
5132 if (pbi->inspect_cb != NULL) {
5133 (*pbi->inspect_cb)(pbi, pbi->inspect_ctx);
5134 }
5135#endif
5136
Yaowu Xuc27fc142016-08-22 16:08:15 -07005137 // Non frame parallel update frame context here.
5138 if (!cm->error_resilient_mode && !context_updated)
5139 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
5140}