blob: 499da3d86c8445ad2e2a17ebfd6bbcae0208ac52 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <assert.h>
13#include <stdlib.h> // qsort()
14
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "./aom_config.h"
16#include "./aom_dsp_rtcd.h"
17#include "./aom_scale_rtcd.h"
Jingning Han1aab8182016-06-03 11:09:06 -070018#include "./av1_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070019
Steinar Midtskogen2fd70ee2016-09-02 10:02:30 +020020#include "aom/aom_codec.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070021#include "aom_dsp/aom_dsp_common.h"
Jingning Han1aab8182016-06-03 11:09:06 -070022#include "aom_dsp/bitreader.h"
23#include "aom_dsp/bitreader_buffer.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070024#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070025#include "aom_ports/mem.h"
26#include "aom_ports/mem_ops.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070027#include "aom_scale/aom_scale.h"
28#include "aom_util/aom_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070029
30#include "av1/common/alloccommon.h"
31#if CONFIG_CLPF
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +020032#include "aom/aom_image.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070033#include "av1/common/clpf.h"
34#endif
35#include "av1/common/common.h"
36#if CONFIG_DERING
37#include "av1/common/dering.h"
38#endif // CONFIG_DERING
39#include "av1/common/entropy.h"
40#include "av1/common/entropymode.h"
Thomas Davies6519beb2016-10-19 14:46:07 +010041#include "av1/common/entropymv.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070042#include "av1/common/idct.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070043#include "av1/common/pred_common.h"
44#include "av1/common/quant_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070045#include "av1/common/reconinter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070046#include "av1/common/reconintra.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070047#include "av1/common/seg_common.h"
Jingning Han1aab8182016-06-03 11:09:06 -070048#include "av1/common/thread_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070049#include "av1/common/tile_common.h"
50
51#include "av1/decoder/decodeframe.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070052#include "av1/decoder/decodemv.h"
53#include "av1/decoder/decoder.h"
Jingning Han1aab8182016-06-03 11:09:06 -070054#include "av1/decoder/detokenize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070055#include "av1/decoder/dsubexp.h"
56
Yue Chen69f18e12016-09-08 14:48:15 -070057#if CONFIG_WARPED_MOTION
58#include "av1/common/warped_motion.h"
59#endif // CONFIG_WARPED_MOTION
60
Yaowu Xuf883b422016-08-30 14:01:10 -070061#define MAX_AV1_HEADER_SIZE 80
Michael Bebenita6048d052016-08-25 14:40:54 -070062#define ACCT_STR __func__
Yaowu Xuc27fc142016-08-22 16:08:15 -070063
Yushin Cho77bba8d2016-11-04 16:36:56 -070064#if CONFIG_PVQ
65#include "av1/decoder/pvq_decoder.h"
Yushin Cho70669122016-12-08 09:53:14 -100066#include "av1/common/pvq.h"
Yushin Cho77bba8d2016-11-04 16:36:56 -070067#include "av1/encoder/encodemb.h"
68
Yushin Cho77bba8d2016-11-04 16:36:56 -070069#include "av1/common/partition.h"
70#include "av1/decoder/decint.h"
71#include "av1/encoder/hybrid_fwd_txfm.h"
72#endif
73
Thomas Davies80188d12016-10-26 16:08:35 -070074static struct aom_read_bit_buffer *init_read_bit_buffer(
75 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
76 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]);
77static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
78 size_t partition_size);
79static size_t read_uncompressed_header(AV1Decoder *pbi,
80 struct aom_read_bit_buffer *rb);
81
Yaowu Xuf883b422016-08-30 14:01:10 -070082static int is_compound_reference_allowed(const AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070083 int i;
84 if (frame_is_intra_only(cm)) return 0;
85 for (i = 1; i < INTER_REFS_PER_FRAME; ++i)
86 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
87
88 return 0;
89}
90
Yaowu Xuf883b422016-08-30 14:01:10 -070091static void setup_compound_reference_mode(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070092#if CONFIG_EXT_REFS
93 cm->comp_fwd_ref[0] = LAST_FRAME;
94 cm->comp_fwd_ref[1] = LAST2_FRAME;
95 cm->comp_fwd_ref[2] = LAST3_FRAME;
96 cm->comp_fwd_ref[3] = GOLDEN_FRAME;
97
98 cm->comp_bwd_ref[0] = BWDREF_FRAME;
99 cm->comp_bwd_ref[1] = ALTREF_FRAME;
100#else
101 if (cm->ref_frame_sign_bias[LAST_FRAME] ==
102 cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
103 cm->comp_fixed_ref = ALTREF_FRAME;
104 cm->comp_var_ref[0] = LAST_FRAME;
105 cm->comp_var_ref[1] = GOLDEN_FRAME;
106 } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
107 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
108 cm->comp_fixed_ref = GOLDEN_FRAME;
109 cm->comp_var_ref[0] = LAST_FRAME;
110 cm->comp_var_ref[1] = ALTREF_FRAME;
111 } else {
112 cm->comp_fixed_ref = LAST_FRAME;
113 cm->comp_var_ref[0] = GOLDEN_FRAME;
114 cm->comp_var_ref[1] = ALTREF_FRAME;
115 }
116#endif // CONFIG_EXT_REFS
117}
118
119static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
120 return len != 0 && len <= (size_t)(end - start);
121}
122
Yaowu Xuf883b422016-08-30 14:01:10 -0700123static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
124 const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700125 return data > max ? max : data;
126}
127
Yaowu Xuf883b422016-08-30 14:01:10 -0700128static TX_MODE read_tx_mode(struct aom_read_bit_buffer *rb) {
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800129#if CONFIG_TX64X64
130 TX_MODE tx_mode =
131 aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
132 if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
133 return tx_mode;
134#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700135 return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800136#endif // CONFIG_TX64X64
Yaowu Xuc27fc142016-08-22 16:08:15 -0700137}
138
Yaowu Xuefc75352016-10-31 09:46:42 -0700139static void read_tx_size_probs(FRAME_CONTEXT *fc, aom_reader *r) {
140 int i, j, k;
141 for (i = 0; i < MAX_TX_DEPTH; ++i)
142 for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
143 for (k = 0; k < i + 1; ++k)
144 av1_diff_update_prob(r, &fc->tx_size_probs[i][j][k], ACCT_STR);
145}
146
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400147#if !CONFIG_EC_ADAPT
Yaowu Xuf883b422016-08-30 14:01:10 -0700148static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700149 int i, j;
Nathan E. Egge4947c292016-04-26 11:37:06 -0400150 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700151 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700152 av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i], ACCT_STR);
Nathan E. Egge4947c292016-04-26 11:37:06 -0400153 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700154}
Thomas Davies6519beb2016-10-19 14:46:07 +0100155#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700156
Yaowu Xuf883b422016-08-30 14:01:10 -0700157static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700158#if CONFIG_REF_MV
Yaowu Xu8af861b2016-11-01 12:12:11 -0700159 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700160 for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700161 av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700162 for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700163 av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700164 for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700165 av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700166 for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700167 av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700168#if CONFIG_EXT_INTER
Michael Bebenita6048d052016-08-25 14:40:54 -0700169 av1_diff_update_prob(r, &fc->new2mv_prob, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700170#endif // CONFIG_EXT_INTER
171#else
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400172#if !CONFIG_EC_ADAPT
Yaowu Xu8af861b2016-11-01 12:12:11 -0700173 int i, j;
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400174 for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700175 for (j = 0; j < INTER_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700176 av1_diff_update_prob(r, &fc->inter_mode_probs[i][j], ACCT_STR);
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400177 }
Yaowu Xu8af861b2016-11-01 12:12:11 -0700178#else
179 (void)fc;
180 (void)r;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700181#endif
Thomas Davies6519beb2016-10-19 14:46:07 +0100182#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700183}
184
185#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700186static void read_inter_compound_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700187 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -0700188 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700189 for (j = 0; j < INTER_MODE_CONTEXTS; ++j) {
190 for (i = 0; i < INTER_COMPOUND_MODES - 1; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700191 av1_diff_update_prob(r, &fc->inter_compound_mode_probs[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700192 }
193 }
194 }
195}
196#endif // CONFIG_EXT_INTER
Yaowu Xu17fd2f22016-11-17 18:23:28 -0800197#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100198#if !CONFIG_EXT_TX
199static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
200 int i, j, k;
201 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
202 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
203 for (j = 0; j < TX_TYPES; ++j) {
204 for (k = 0; k < TX_TYPES - 1; ++k)
205 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[i][j][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100206 }
207 }
208 }
209 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
210 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
211 for (k = 0; k < TX_TYPES - 1; ++k)
212 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[i][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100213 }
214 }
215}
216#endif
217#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700218
219static REFERENCE_MODE read_frame_reference_mode(
Yaowu Xuf883b422016-08-30 14:01:10 -0700220 const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700221 if (is_compound_reference_allowed(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700222 return aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700223 ? REFERENCE_MODE_SELECT
Yaowu Xuf883b422016-08-30 14:01:10 -0700224 : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700225 } else {
226 return SINGLE_REFERENCE;
227 }
228}
229
Yaowu Xuf883b422016-08-30 14:01:10 -0700230static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700231 FRAME_CONTEXT *const fc = cm->fc;
232 int i, j;
233
234 if (cm->reference_mode == REFERENCE_MODE_SELECT)
235 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700236 av1_diff_update_prob(r, &fc->comp_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700237
238 if (cm->reference_mode != COMPOUND_REFERENCE) {
239 for (i = 0; i < REF_CONTEXTS; ++i) {
240 for (j = 0; j < (SINGLE_REFS - 1); ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700241 av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700242 }
243 }
244 }
245
246 if (cm->reference_mode != SINGLE_REFERENCE) {
247 for (i = 0; i < REF_CONTEXTS; ++i) {
248#if CONFIG_EXT_REFS
249 for (j = 0; j < (FWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700250 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700251 for (j = 0; j < (BWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700252 av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700253#else
254 for (j = 0; j < (COMP_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700255 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700256#endif // CONFIG_EXT_REFS
257 }
258 }
259}
260
Yaowu Xuf883b422016-08-30 14:01:10 -0700261static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700262 int i;
Michael Bebenita6048d052016-08-25 14:40:54 -0700263 for (i = 0; i < n; ++i) av1_diff_update_prob(r, &p[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700264}
265
Yaowu Xuf883b422016-08-30 14:01:10 -0700266static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Thomas9ac55082016-09-23 18:04:17 +0100267 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700268
Yaowu Xue86288d2016-10-31 15:56:38 -0700269#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100270 int j;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700271 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
272
Yaowu Xuc27fc142016-08-22 16:08:15 -0700273 for (i = 0; i < 2; ++i) {
274 nmv_component *const comp_ctx = &ctx->comps[i];
275 update_mv_probs(&comp_ctx->sign, 1, r);
276 update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
277 update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
278 update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
279 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700280 for (i = 0; i < 2; ++i) {
281 nmv_component *const comp_ctx = &ctx->comps[i];
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400282 for (j = 0; j < CLASS0_SIZE; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700283 update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400284 }
Nathan E. Eggeac499f32016-09-08 15:38:57 -0400285 update_mv_probs(comp_ctx->fp, MV_FP_SIZE - 1, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700286 }
Alex Converseaca9feb2016-10-10 11:08:10 -0700287#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700288
289 if (allow_hp) {
290 for (i = 0; i < 2; ++i) {
291 nmv_component *const comp_ctx = &ctx->comps[i];
292 update_mv_probs(&comp_ctx->class0_hp, 1, r);
293 update_mv_probs(&comp_ctx->hp, 1, r);
294 }
295 }
296}
297
298static void inverse_transform_block(MACROBLOCKD *xd, int plane,
299 const TX_TYPE tx_type,
300 const TX_SIZE tx_size, uint8_t *dst,
Jingning Han1be18782016-10-21 11:48:15 -0700301 int stride, int16_t scan_line, int eob) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700302 struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han1be18782016-10-21 11:48:15 -0700303 tran_low_t *const dqcoeff = pd->dqcoeff;
304 INV_TXFM_PARAM inv_txfm_param;
305 inv_txfm_param.tx_type = tx_type;
306 inv_txfm_param.tx_size = tx_size;
307 inv_txfm_param.eob = eob;
308 inv_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700309
Yaowu Xuf883b422016-08-30 14:01:10 -0700310#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han1be18782016-10-21 11:48:15 -0700311 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
312 inv_txfm_param.bd = xd->bd;
313 highbd_inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
314 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700315#endif // CONFIG_AOM_HIGHBITDEPTH
Jingning Han1be18782016-10-21 11:48:15 -0700316 inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
Yaowu Xuf883b422016-08-30 14:01:10 -0700317#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700318 }
Jingning Han1be18782016-10-21 11:48:15 -0700319#endif // CONFIG_AOM_HIGHBITDEPTH
320 memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700321}
322
Yushin Cho77bba8d2016-11-04 16:36:56 -0700323#if CONFIG_PVQ
324static int av1_pvq_decode_helper(od_dec_ctx *dec, int16_t *ref_coeff,
325 int16_t *dqcoeff, int16_t *quant, int pli,
326 int bs, TX_TYPE tx_type, int xdec,
327 int ac_dc_coded) {
328 unsigned int flags; // used for daala's stream analyzer.
329 int off;
330 const int is_keyframe = 0;
331 const int has_dc_skip = 1;
Debargha Mukherjee153e1f82016-11-17 09:59:14 -0800332 int quant_shift = get_tx_scale(bs);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700333 // DC quantizer for PVQ
334 int pvq_dc_quant;
335 int lossless = (quant[0] == 0);
336 const int blk_size = tx_size_wide[bs];
337 int eob = 0;
338 int i;
Yushin Cho70669122016-12-08 09:53:14 -1000339 int use_activity_masking = dec->use_activity_masking;
Yushin Cho48f84db2016-11-07 21:20:17 -0800340 DECLARE_ALIGNED(16, int16_t, dqcoeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
341 DECLARE_ALIGNED(16, int16_t, ref_coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700342
Yushin Cho48f84db2016-11-07 21:20:17 -0800343 od_coeff ref_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
344 od_coeff out_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700345
346 od_raster_to_coding_order(ref_coeff_pvq, blk_size, tx_type, ref_coeff,
347 blk_size);
348
349 if (lossless)
350 pvq_dc_quant = 1;
351 else {
Yushin Cho70669122016-12-08 09:53:14 -1000352 if (use_activity_masking)
353 pvq_dc_quant = OD_MAXI(
354 1, (quant[0] >> quant_shift) *
355 dec->state.pvq_qm_q4[pli][od_qm_get_index(bs, 0)] >>
356 4);
357 else
358 pvq_dc_quant = OD_MAXI(1, quant[0] >> quant_shift);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700359 }
360
361 off = od_qm_offset(bs, xdec);
362
363 // copy int16 inputs to int32
364 for (i = 0; i < blk_size * blk_size; i++) ref_int32[i] = ref_coeff_pvq[i];
365
366 od_pvq_decode(dec, ref_int32, out_int32, (int)quant[1] >> quant_shift, pli,
367 bs, OD_PVQ_BETA[use_activity_masking][pli][bs],
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800368 OD_ROBUST_STREAM, is_keyframe, &flags, ac_dc_coded,
369 dec->state.qm + off, dec->state.qm_inv + off);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700370
371 // copy int32 result back to int16
372 for (i = 0; i < blk_size * blk_size; i++) dqcoeff_pvq[i] = out_int32[i];
373
374 if (!has_dc_skip || dqcoeff_pvq[0]) {
375 dqcoeff_pvq[0] =
Nathan E. Egge89f58762016-12-28 16:31:50 -0500376 has_dc_skip + generic_decode(dec->r, &dec->state.adapt.model_dc[pli],
377 -1, &dec->state.adapt.ex_dc[pli][bs][0], 2,
378 "dc:mag");
Yushin Cho77bba8d2016-11-04 16:36:56 -0700379 if (dqcoeff_pvq[0])
Nathan E. Eggeab083972016-12-28 15:31:46 -0500380 dqcoeff_pvq[0] *= aom_read_bit(dec->r, "dc:sign") ? -1 : 1;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700381 }
382 dqcoeff_pvq[0] = dqcoeff_pvq[0] * pvq_dc_quant + ref_coeff_pvq[0];
383
384 od_coding_order_to_raster(dqcoeff, blk_size, tx_type, dqcoeff_pvq, blk_size);
385
386 eob = blk_size * blk_size;
387
388 return eob;
389}
390
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800391static int av1_pvq_decode_helper2(MACROBLOCKD *const xd,
392 MB_MODE_INFO *const mbmi, int plane, int row,
393 int col, TX_SIZE tx_size, TX_TYPE tx_type) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700394 struct macroblockd_plane *const pd = &xd->plane[plane];
395 // transform block size in pixels
396 int tx_blk_size = tx_size_wide[tx_size];
397 int i, j;
398 tran_low_t *pvq_ref_coeff = pd->pvq_ref_coeff;
399 const int diff_stride = tx_blk_size;
400 int16_t *pred = pd->pred;
401 tran_low_t *const dqcoeff = pd->dqcoeff;
402 int ac_dc_coded; // bit0: DC coded, bit1 : AC coded
403 uint8_t *dst;
404 int eob;
405
406 eob = 0;
407 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
408
Nathan E. Eggeab083972016-12-28 15:31:46 -0500409#if CONFIG_DAALA_EC
Yushin Cho77bba8d2016-11-04 16:36:56 -0700410 // decode ac/dc coded flag. bit0: DC coded, bit1 : AC coded
411 // NOTE : we don't use 5 symbols for luma here in aom codebase,
412 // since block partition is taken care of by aom.
413 // So, only AC/DC skip info is coded
414 ac_dc_coded = od_decode_cdf_adapt(
Nathan E. Eggeab083972016-12-28 15:31:46 -0500415 &xd->daala_dec.r->ec,
Yushin Cho77bba8d2016-11-04 16:36:56 -0700416 xd->daala_dec.state.adapt.skip_cdf[2 * tx_size + (plane != 0)], 4,
417 xd->daala_dec.state.adapt.skip_increment, "skip");
Nathan E. Eggeab083972016-12-28 15:31:46 -0500418#else
419#error "CONFIG_PVQ currently requires CONFIG_DAALA_EC."
420#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -0700421 if (ac_dc_coded) {
422 int xdec = pd->subsampling_x;
423 int seg_id = mbmi->segment_id;
424 int16_t *quant;
425 FWD_TXFM_PARAM fwd_txfm_param;
Yaowu Xufc1b2132016-11-07 15:16:15 -0800426 // ToDo(yaowu): correct this with optimal number from decoding process.
427 const int max_scan_line = tx_size_2d[tx_size];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700428
429 for (j = 0; j < tx_blk_size; j++)
430 for (i = 0; i < tx_blk_size; i++) {
431 pred[diff_stride * j + i] = dst[pd->dst.stride * j + i];
432 }
433
434 fwd_txfm_param.tx_type = tx_type;
435 fwd_txfm_param.tx_size = tx_size;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700436 fwd_txfm_param.lossless = xd->lossless[seg_id];
437
438 fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
439
440 quant = &pd->seg_dequant[seg_id][0]; // aom's quantizer
441
442 eob = av1_pvq_decode_helper(&xd->daala_dec, pvq_ref_coeff, dqcoeff, quant,
443 plane, tx_size, tx_type, xdec, ac_dc_coded);
444
445 // Since av1 does not have separate inverse transform
446 // but also contains adding to predicted image,
447 // pass blank dummy image to av1_inv_txfm_add_*x*(), i.e. set dst as zeros
448 for (j = 0; j < tx_blk_size; j++)
449 for (i = 0; i < tx_blk_size; i++) dst[j * pd->dst.stride + i] = 0;
450
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800451 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
452 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700453 }
454
455 return eob;
456}
457#endif
458
Angie Chiangff6d8902016-10-21 11:02:09 -0700459static void predict_and_reconstruct_intra_block(AV1_COMMON *cm,
460 MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700461#if CONFIG_ANS
462 struct AnsDecoder *const r,
463#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700464 aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700465#endif // CONFIG_ANS
466 MB_MODE_INFO *const mbmi,
467 int plane, int row, int col,
468 TX_SIZE tx_size) {
469 struct macroblockd_plane *const pd = &xd->plane[plane];
470 PREDICTION_MODE mode = (plane == 0) ? mbmi->mode : mbmi->uv_mode;
471 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
472 uint8_t *dst;
473 int block_idx = (row << 1) + col;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700474#if CONFIG_PVQ
475 (void)cm;
476 (void)r;
477#endif
Jingning Hanca14dda2016-12-09 09:36:00 -0800478 dst = &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700479
Jingning Han41bb3392016-12-14 10:46:48 -0800480#if !CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -0700481 if (mbmi->sb_type < BLOCK_8X8)
482 if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
Jingning Han41bb3392016-12-14 10:46:48 -0800483#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700484
Jingning Hanc4c99da2016-10-24 10:27:28 -0700485 av1_predict_intra_block(xd, pd->width, pd->height, tx_size, mode, dst,
Yaowu Xuf883b422016-08-30 14:01:10 -0700486 pd->dst.stride, dst, pd->dst.stride, col, row, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700487
488 if (!mbmi->skip) {
489 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700490#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700491 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 0);
Jingning Han1be18782016-10-21 11:48:15 -0700492 int16_t max_scan_line = 0;
493 const int eob =
494 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size,
495 tx_type, &max_scan_line, r, mbmi->segment_id);
Angie Chianged8cd9a2016-10-21 16:44:47 -0700496#if CONFIG_ADAPT_SCAN
497 av1_update_scan_count_facade(cm, tx_size, tx_type, pd->dqcoeff, eob);
498#endif
Jingning Han1be18782016-10-21 11:48:15 -0700499 if (eob)
500 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
501 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700502#else
503 av1_pvq_decode_helper2(xd, mbmi, plane, row, col, tx_size, tx_type);
504#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700505 }
506}
507
508#if CONFIG_VAR_TX
Angie Chiangff6d8902016-10-21 11:02:09 -0700509static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
510 aom_reader *r, MB_MODE_INFO *const mbmi,
Jingning Han8fd62b72016-10-21 12:55:54 -0700511 int plane, BLOCK_SIZE plane_bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700512 int blk_row, int blk_col, TX_SIZE tx_size,
513 int *eob_total) {
514 const struct macroblockd_plane *const pd = &xd->plane[plane];
515 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
516 const int tx_row = blk_row >> (1 - pd->subsampling_y);
517 const int tx_col = blk_col >> (1 - pd->subsampling_x);
518 const TX_SIZE plane_tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700519 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700520 : mbmi->inter_tx_size[tx_row][tx_col];
Jingning Han5f614262016-10-27 14:27:43 -0700521 // Scale to match transform block unit.
Jingning Hanf64062f2016-11-02 16:22:18 -0700522 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
523 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700524
525 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
526
527 if (tx_size == plane_tx_size) {
528 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
Jingning Han8fd62b72016-10-21 12:55:54 -0700529 int block_idx = (blk_row << 1) + blk_col;
530 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiangff6d8902016-10-21 11:02:09 -0700531 const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700532 int16_t max_scan_line = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700533 const int eob =
Yaowu Xuf883b422016-08-30 14:01:10 -0700534 av1_decode_block_tokens(xd, plane, sc, blk_col, blk_row, plane_tx_size,
Jingning Han1be18782016-10-21 11:48:15 -0700535 tx_type, &max_scan_line, r, mbmi->segment_id);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700536 inverse_transform_block(
537 xd, plane, tx_type, plane_tx_size,
538 &pd->dst.buf[4 * blk_row * pd->dst.stride + 4 * blk_col],
Jingning Han1be18782016-10-21 11:48:15 -0700539 pd->dst.stride, max_scan_line, eob);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700540 *eob_total += eob;
541 } else {
Jingning Hanf64062f2016-11-02 16:22:18 -0700542 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
543 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700544 int i;
545
546 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700547
548 for (i = 0; i < 4; ++i) {
Jingning Han5f614262016-10-27 14:27:43 -0700549 const int offsetr = blk_row + (i >> 1) * bsl;
550 const int offsetc = blk_col + (i & 0x01) * bsl;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700551
552 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
553
Jingning Han8fd62b72016-10-21 12:55:54 -0700554 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
Jingning Hanf64062f2016-11-02 16:22:18 -0700555 offsetc, sub_txs, eob_total);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700556 }
557 }
558}
559#endif // CONFIG_VAR_TX
560
Jingning Hanfe45b212016-11-22 10:30:23 -0800561#if !CONFIG_VAR_TX || CONFIG_SUPERTX || \
562 (!CONFIG_VAR_TX && CONFIG_EXT_TX && CONFIG_RECT_TX)
Angie Chiangff6d8902016-10-21 11:02:09 -0700563static int reconstruct_inter_block(AV1_COMMON *cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700564#if CONFIG_ANS
565 struct AnsDecoder *const r,
566#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700567 aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700568#endif
569 int segment_id, int plane, int row, int col,
570 TX_SIZE tx_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700571 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
572 int block_idx = (row << 1) + col;
573 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700574#if CONFIG_PVQ
575 int eob;
576 (void)cm;
577 (void)r;
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800578 (void)segment_id;
579#else
580 struct macroblockd_plane *const pd = &xd->plane[plane];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700581#endif
582
583#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700584 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700585 int16_t max_scan_line = 0;
586 const int eob =
587 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size, tx_type,
588 &max_scan_line, r, segment_id);
Jingning Hanca14dda2016-12-09 09:36:00 -0800589 uint8_t *dst =
590 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Angie Chianged8cd9a2016-10-21 16:44:47 -0700591#if CONFIG_ADAPT_SCAN
592 av1_update_scan_count_facade(cm, tx_size, tx_type, pd->dqcoeff, eob);
593#endif
Jingning Han1be18782016-10-21 11:48:15 -0700594 if (eob)
Jingning Hanca14dda2016-12-09 09:36:00 -0800595 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
596 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700597#else
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800598 eob = av1_pvq_decode_helper2(xd, &xd->mi[0]->mbmi, plane, row, col, tx_size,
599 tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700600#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700601 return eob;
602}
603#endif // !CONFIG_VAR_TX || CONFIG_SUPER_TX
604
Yaowu Xuf883b422016-08-30 14:01:10 -0700605static MB_MODE_INFO *set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606 BLOCK_SIZE bsize, int mi_row, int mi_col,
Jingning Hanfaad0e12016-12-07 10:54:57 -0800607 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700608 const int offset = mi_row * cm->mi_stride + mi_col;
609 int x, y;
610 const TileInfo *const tile = &xd->tile;
611
612 xd->mi = cm->mi_grid_visible + offset;
613 xd->mi[0] = &cm->mi[offset];
614 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
615 // passing bsize from decode_partition().
616 xd->mi[0]->mbmi.sb_type = bsize;
Angie Chiang394c3372016-11-03 11:13:15 -0700617#if CONFIG_RD_DEBUG
618 xd->mi[0]->mbmi.mi_row = mi_row;
619 xd->mi[0]->mbmi.mi_col = mi_col;
620#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621 for (y = 0; y < y_mis; ++y)
Jingning Han97d85482016-07-15 11:06:05 -0700622 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700623
Jingning Hanfaad0e12016-12-07 10:54:57 -0800624 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700625 set_skip_context(xd, mi_row, mi_col);
626
627#if CONFIG_VAR_TX
628 xd->max_tx_size = max_txsize_lookup[bsize];
629#endif
630
631 // Distance of Mb to the various image edges. These are specified to 8th pel
632 // as they are always compared to values that are in 1/8th pel units
633 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
634
Yaowu Xuf883b422016-08-30 14:01:10 -0700635 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700636 return &xd->mi[0]->mbmi;
637}
638
639#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700640static MB_MODE_INFO *set_offsets_extend(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700641 MACROBLOCKD *const xd,
642 const TileInfo *const tile,
643 BLOCK_SIZE bsize_pred, int mi_row_pred,
644 int mi_col_pred, int mi_row_ori,
645 int mi_col_ori) {
646 // Used in supertx
647 // (mi_row_ori, mi_col_ori): location for mv
648 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
Jingning Han93531242016-12-20 11:54:36 -0800649 const int bw = mi_size_wide[bsize_pred];
650 const int bh = mi_size_high[bsize_pred];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700651 const int offset = mi_row_ori * cm->mi_stride + mi_col_ori;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700652 xd->mi = cm->mi_grid_visible + offset;
653 xd->mi[0] = cm->mi + offset;
654 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
655 cm->mi_cols);
656
657 xd->up_available = (mi_row_ori > tile->mi_row_start);
658 xd->left_available = (mi_col_ori > tile->mi_col_start);
659
Jingning Hanfaad0e12016-12-07 10:54:57 -0800660 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700661
662 return &xd->mi[0]->mbmi;
663}
664
Yaowu Xuf883b422016-08-30 14:01:10 -0700665static MB_MODE_INFO *set_mb_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
666 BLOCK_SIZE bsize, int mi_row, int mi_col,
667 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700668 const int offset = mi_row * cm->mi_stride + mi_col;
669 const TileInfo *const tile = &xd->tile;
670 int x, y;
671
672 xd->mi = cm->mi_grid_visible + offset;
673 xd->mi[0] = cm->mi + offset;
674 xd->mi[0]->mbmi.sb_type = bsize;
675 for (y = 0; y < y_mis; ++y)
676 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
677
678 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
679 return &xd->mi[0]->mbmi;
680}
681
Yaowu Xuf883b422016-08-30 14:01:10 -0700682static void set_offsets_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700683 const TileInfo *const tile, BLOCK_SIZE bsize,
684 int mi_row, int mi_col) {
Jingning Han93531242016-12-20 11:54:36 -0800685 const int bw = mi_size_wide[bsize];
686 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700687 const int offset = mi_row * cm->mi_stride + mi_col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700688
689 xd->mi = cm->mi_grid_visible + offset;
690 xd->mi[0] = cm->mi + offset;
691
Jingning Hanfaad0e12016-12-07 10:54:57 -0800692 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700693
694 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
695
Yaowu Xuf883b422016-08-30 14:01:10 -0700696 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700697}
698
Yaowu Xuf883b422016-08-30 14:01:10 -0700699static void set_param_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700700 BLOCK_SIZE bsize, int mi_row, int mi_col,
701 int txfm, int skip) {
Jingning Han93531242016-12-20 11:54:36 -0800702 const int bw = mi_size_wide[bsize];
703 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -0700704 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
705 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700706 const int offset = mi_row * cm->mi_stride + mi_col;
707 int x, y;
708
709 xd->mi = cm->mi_grid_visible + offset;
710 xd->mi[0] = cm->mi + offset;
711
712 for (y = 0; y < y_mis; ++y)
713 for (x = 0; x < x_mis; ++x) {
714 xd->mi[y * cm->mi_stride + x]->mbmi.skip = skip;
715 xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
716 }
717#if CONFIG_VAR_TX
718 xd->above_txfm_context = cm->above_txfm_context + mi_col;
719 xd->left_txfm_context =
720 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
Yaowu Xu52a17632016-11-17 15:48:21 -0800721 set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bw, bh, skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700722#endif
723}
724
Yaowu Xuf883b422016-08-30 14:01:10 -0700725static void set_ref(AV1_COMMON *const cm, MACROBLOCKD *const xd, int idx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700726 int mi_row, int mi_col) {
727 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
728 RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
729 xd->block_refs[idx] = ref_buffer;
Yaowu Xuf883b422016-08-30 14:01:10 -0700730 if (!av1_is_valid_scale(&ref_buffer->sf))
731 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700732 "Invalid scale factors");
Yaowu Xuf883b422016-08-30 14:01:10 -0700733 av1_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
734 &ref_buffer->sf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700735 xd->corrupted |= ref_buffer->buf->corrupted;
736}
737
738static void dec_predict_b_extend(
Yaowu Xuf883b422016-08-30 14:01:10 -0700739 AV1Decoder *const pbi, MACROBLOCKD *const xd, const TileInfo *const tile,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700740 int block, int mi_row_ori, int mi_col_ori, int mi_row_pred, int mi_col_pred,
741 int mi_row_top, int mi_col_top, uint8_t *dst_buf[3], int dst_stride[3],
742 BLOCK_SIZE bsize_top, BLOCK_SIZE bsize_pred, int b_sub8x8, int bextend) {
743 // Used in supertx
744 // (mi_row_ori, mi_col_ori): location for mv
745 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
746 // (mi_row_top, mi_col_top, bsize_top): region of the top partition size
747 // block: sub location of sub8x8 blocks
748 // b_sub8x8: 1: ori is sub8x8; 0: ori is not sub8x8
749 // bextend: 1: region to predict is an extension of ori; 0: not
750 int r = (mi_row_pred - mi_row_top) * MI_SIZE;
751 int c = (mi_col_pred - mi_col_top) * MI_SIZE;
Jingning Han93531242016-12-20 11:54:36 -0800752 const int mi_width_top = mi_size_wide[bsize_top];
753 const int mi_height_top = mi_size_high[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700754 MB_MODE_INFO *mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700755 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700756
757 if (mi_row_pred < mi_row_top || mi_col_pred < mi_col_top ||
758 mi_row_pred >= mi_row_top + mi_height_top ||
759 mi_col_pred >= mi_col_top + mi_width_top || mi_row_pred >= cm->mi_rows ||
760 mi_col_pred >= cm->mi_cols)
761 return;
762
763 mbmi = set_offsets_extend(cm, xd, tile, bsize_pred, mi_row_pred, mi_col_pred,
764 mi_row_ori, mi_col_ori);
765 set_ref(cm, xd, 0, mi_row_pred, mi_col_pred);
766 if (has_second_ref(&xd->mi[0]->mbmi))
767 set_ref(cm, xd, 1, mi_row_pred, mi_col_pred);
768
Jingning Han2511c662016-12-22 11:57:34 -0800769 if (!bextend) mbmi->tx_size = max_txsize_lookup[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700770
771 xd->plane[0].dst.stride = dst_stride[0];
772 xd->plane[1].dst.stride = dst_stride[1];
773 xd->plane[2].dst.stride = dst_stride[2];
774 xd->plane[0].dst.buf = dst_buf[0] +
775 (r >> xd->plane[0].subsampling_y) * dst_stride[0] +
776 (c >> xd->plane[0].subsampling_x);
777 xd->plane[1].dst.buf = dst_buf[1] +
778 (r >> xd->plane[1].subsampling_y) * dst_stride[1] +
779 (c >> xd->plane[1].subsampling_x);
780 xd->plane[2].dst.buf = dst_buf[2] +
781 (r >> xd->plane[2].subsampling_y) * dst_stride[2] +
782 (c >> xd->plane[2].subsampling_x);
783
784 if (!b_sub8x8)
Yaowu Xuf883b422016-08-30 14:01:10 -0700785 av1_build_inter_predictors_sb_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700786#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700787 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700788#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700789 mi_row_pred, mi_col_pred, bsize_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700790 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700791 av1_build_inter_predictors_sb_sub8x8_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700792#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700793 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700794#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700795 mi_row_pred, mi_col_pred,
796 bsize_pred, block);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700797}
798
Yaowu Xuf883b422016-08-30 14:01:10 -0700799static void dec_extend_dir(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800 const TileInfo *const tile, int block,
801 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
802 int mi_col, int mi_row_top, int mi_col_top,
803 uint8_t *dst_buf[3], int dst_stride[3], int dir) {
804 // dir: 0-lower, 1-upper, 2-left, 3-right
805 // 4-lowerleft, 5-upperleft, 6-lowerright, 7-upperright
Jingning Han93531242016-12-20 11:54:36 -0800806 const int mi_width = mi_size_wide[bsize];
807 const int mi_height = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700808 int xss = xd->plane[1].subsampling_x;
809 int yss = xd->plane[1].subsampling_y;
Jingning Hanfeb517c2016-12-21 16:02:07 -0800810#if CONFIG_CB4X4
811 const int unify_bsize = 1;
812#else
813 const int unify_bsize = 0;
814#endif
815 int b_sub8x8 = (bsize < BLOCK_8X8) && !unify_bsize ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700816 BLOCK_SIZE extend_bsize;
Jingning Han24f24a52016-12-27 10:13:28 -0800817 int mi_row_pred, mi_col_pred;
818
819 int wide_unit, high_unit;
820 int i, j;
821 int ext_offset = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700822
823 if (dir == 0 || dir == 1) {
Jingning Han93531242016-12-20 11:54:36 -0800824 extend_bsize =
825 (mi_width == mi_size_wide[BLOCK_8X8] || bsize < BLOCK_8X8 || xss < yss)
826 ? BLOCK_8X8
827 : BLOCK_16X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800828#if CONFIG_CB4X4
829 if (bsize < BLOCK_8X8) {
830 extend_bsize = BLOCK_4X4;
831 ext_offset = mi_size_wide[BLOCK_8X8];
832 }
833#endif
834
835 wide_unit = mi_size_wide[extend_bsize];
836 high_unit = mi_size_high[extend_bsize];
837
838 mi_row_pred = mi_row + ((dir == 0) ? mi_height : -(mi_height + ext_offset));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700839 mi_col_pred = mi_col;
840
Jingning Han24f24a52016-12-27 10:13:28 -0800841 for (j = 0; j < mi_height + ext_offset; j += high_unit)
842 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
843 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
844 mi_row_pred + j, mi_col_pred + i, mi_row_top,
845 mi_col_top, dst_buf, dst_stride, top_bsize,
846 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700847 } else if (dir == 2 || dir == 3) {
Jingning Han5b7706a2016-12-21 09:55:10 -0800848 extend_bsize =
849 (mi_height == mi_size_high[BLOCK_8X8] || bsize < BLOCK_8X8 || yss < xss)
850 ? BLOCK_8X8
851 : BLOCK_8X16;
Jingning Han24f24a52016-12-27 10:13:28 -0800852#if CONFIG_CB4X4
853 if (bsize < BLOCK_8X8) {
854 extend_bsize = BLOCK_4X4;
855 ext_offset = mi_size_wide[BLOCK_8X8];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700856 }
Jingning Han24f24a52016-12-27 10:13:28 -0800857#endif
858
859 wide_unit = mi_size_wide[extend_bsize];
860 high_unit = mi_size_high[extend_bsize];
861
862 mi_row_pred = mi_row;
863 mi_col_pred = mi_col + ((dir == 3) ? mi_width : -(mi_width + ext_offset));
864
865 for (j = 0; j < mi_height + ext_offset; j += high_unit)
866 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
867 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
868 mi_row_pred + j, mi_col_pred + i, mi_row_top,
869 mi_col_top, dst_buf, dst_stride, top_bsize,
870 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700871 } else {
872 extend_bsize = BLOCK_8X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800873#if CONFIG_CB4X4
874 if (bsize < BLOCK_8X8) {
875 extend_bsize = BLOCK_4X4;
876 ext_offset = mi_size_wide[BLOCK_8X8];
877 }
878#endif
879 wide_unit = mi_size_wide[extend_bsize];
880 high_unit = mi_size_high[extend_bsize];
881
Jingning Han5b7706a2016-12-21 09:55:10 -0800882 mi_row_pred = mi_row + ((dir == 4 || dir == 6) ? mi_height
Jingning Han24f24a52016-12-27 10:13:28 -0800883 : -(mi_height + ext_offset));
Jingning Han5b7706a2016-12-21 09:55:10 -0800884 mi_col_pred =
Jingning Han24f24a52016-12-27 10:13:28 -0800885 mi_col + ((dir == 6 || dir == 7) ? mi_width : -(mi_width + ext_offset));
886
887 for (j = 0; j < mi_height + ext_offset; j += high_unit)
888 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
889 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
890 mi_row_pred + j, mi_col_pred + i, mi_row_top,
891 mi_col_top, dst_buf, dst_stride, top_bsize,
892 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700893 }
894}
895
Yaowu Xuf883b422016-08-30 14:01:10 -0700896static void dec_extend_all(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700897 const TileInfo *const tile, int block,
898 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
899 int mi_col, int mi_row_top, int mi_col_top,
900 uint8_t *dst_buf[3], int dst_stride[3]) {
901 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
902 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
903 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
904 mi_row_top, mi_col_top, dst_buf, dst_stride, 1);
905 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
906 mi_row_top, mi_col_top, dst_buf, dst_stride, 2);
907 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
908 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
909 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
910 mi_row_top, mi_col_top, dst_buf, dst_stride, 4);
911 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
912 mi_row_top, mi_col_top, dst_buf, dst_stride, 5);
913 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
914 mi_row_top, mi_col_top, dst_buf, dst_stride, 6);
915 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
916 mi_row_top, mi_col_top, dst_buf, dst_stride, 7);
917}
918
Yaowu Xuf883b422016-08-30 14:01:10 -0700919static void dec_predict_sb_complex(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700920 const TileInfo *const tile, int mi_row,
921 int mi_col, int mi_row_top, int mi_col_top,
922 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize,
923 uint8_t *dst_buf[3], int dst_stride[3]) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700924 const AV1_COMMON *const cm = &pbi->common;
Jingning Han5b7706a2016-12-21 09:55:10 -0800925 const int hbs = mi_size_wide[bsize] / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700926 const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
927 const BLOCK_SIZE subsize = get_subsize(bsize, partition);
928#if CONFIG_EXT_PARTITION_TYPES
929 const BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
930#endif
931 int i;
932 const int mi_offset = mi_row * cm->mi_stride + mi_col;
933 uint8_t *dst_buf1[3], *dst_buf2[3], *dst_buf3[3];
Jingning Hanfeb517c2016-12-21 16:02:07 -0800934#if CONFIG_CB4X4
935 const int unify_bsize = 1;
936#else
937 const int unify_bsize = 0;
938#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700939
940 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
941 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
942 DECLARE_ALIGNED(16, uint8_t, tmp_buf3[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
943 int dst_stride1[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
944 int dst_stride2[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
945 int dst_stride3[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
946
Yaowu Xuf883b422016-08-30 14:01:10 -0700947#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700948 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
949 int len = sizeof(uint16_t);
950 dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
951 dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_TX_SQUARE * len);
952 dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_TX_SQUARE * len);
953 dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
954 dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_TX_SQUARE * len);
955 dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_TX_SQUARE * len);
956 dst_buf3[0] = CONVERT_TO_BYTEPTR(tmp_buf3);
957 dst_buf3[1] = CONVERT_TO_BYTEPTR(tmp_buf3 + MAX_TX_SQUARE * len);
958 dst_buf3[2] = CONVERT_TO_BYTEPTR(tmp_buf3 + 2 * MAX_TX_SQUARE * len);
959 } else {
960#endif
961 dst_buf1[0] = tmp_buf1;
962 dst_buf1[1] = tmp_buf1 + MAX_TX_SQUARE;
963 dst_buf1[2] = tmp_buf1 + 2 * MAX_TX_SQUARE;
964 dst_buf2[0] = tmp_buf2;
965 dst_buf2[1] = tmp_buf2 + MAX_TX_SQUARE;
966 dst_buf2[2] = tmp_buf2 + 2 * MAX_TX_SQUARE;
967 dst_buf3[0] = tmp_buf3;
968 dst_buf3[1] = tmp_buf3 + MAX_TX_SQUARE;
969 dst_buf3[2] = tmp_buf3 + 2 * MAX_TX_SQUARE;
Yaowu Xuf883b422016-08-30 14:01:10 -0700970#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700971 }
972#endif
973
974 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
975
976 xd->mi = cm->mi_grid_visible + mi_offset;
977 xd->mi[0] = cm->mi + mi_offset;
978
979 for (i = 0; i < MAX_MB_PLANE; i++) {
980 xd->plane[i].dst.buf = dst_buf[i];
981 xd->plane[i].dst.stride = dst_stride[i];
982 }
983
984 switch (partition) {
985 case PARTITION_NONE:
986 assert(bsize < top_bsize);
987 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
988 mi_row_top, mi_col_top, dst_buf, dst_stride,
989 top_bsize, bsize, 0, 0);
990 dec_extend_all(pbi, xd, tile, 0, bsize, top_bsize, mi_row, mi_col,
991 mi_row_top, mi_col_top, dst_buf, dst_stride);
992 break;
993 case PARTITION_HORZ:
Jingning Hanfeb517c2016-12-21 16:02:07 -0800994 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700995 // For sub8x8, predict in 8x8 unit
996 // First half
997 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
998 mi_row_top, mi_col_top, dst_buf, dst_stride,
999 top_bsize, BLOCK_8X8, 1, 0);
1000 if (bsize < top_bsize)
1001 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1002 mi_row_top, mi_col_top, dst_buf, dst_stride);
1003
1004 // Second half
1005 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1006 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1007 top_bsize, BLOCK_8X8, 1, 1);
1008 if (bsize < top_bsize)
1009 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1010 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1011
1012 // weighted average to smooth the boundary
1013 xd->plane[0].dst.buf = dst_buf[0];
1014 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001015 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001016 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1017 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1018 0);
1019 } else {
1020 // First half
1021 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1022 mi_row_top, mi_col_top, dst_buf, dst_stride,
1023 top_bsize, subsize, 0, 0);
1024 if (bsize < top_bsize)
1025 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1026 mi_row_top, mi_col_top, dst_buf, dst_stride);
1027 else
1028 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1029 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1030
1031 if (mi_row + hbs < cm->mi_rows) {
1032 // Second half
1033 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col,
1034 mi_row + hbs, mi_col, mi_row_top, mi_col_top,
1035 dst_buf1, dst_stride1, top_bsize, subsize, 0, 0);
1036 if (bsize < top_bsize)
1037 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1038 mi_col, mi_row_top, mi_col_top, dst_buf1,
1039 dst_stride1);
1040 else
1041 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1042 mi_col, mi_row_top, mi_col_top, dst_buf1,
1043 dst_stride1, 1);
1044
1045 // weighted average to smooth the boundary
1046 for (i = 0; i < MAX_MB_PLANE; i++) {
1047 xd->plane[i].dst.buf = dst_buf[i];
1048 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001049 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001050 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1051 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1052 PARTITION_HORZ, i);
1053 }
1054 }
1055 }
1056 break;
1057 case PARTITION_VERT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001058 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001059 // First half
1060 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1061 mi_row_top, mi_col_top, dst_buf, dst_stride,
1062 top_bsize, BLOCK_8X8, 1, 0);
1063 if (bsize < top_bsize)
1064 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1065 mi_row_top, mi_col_top, dst_buf, dst_stride);
1066
1067 // Second half
1068 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1069 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1070 top_bsize, BLOCK_8X8, 1, 1);
1071 if (bsize < top_bsize)
1072 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1073 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1074
1075 // Smooth
1076 xd->plane[0].dst.buf = dst_buf[0];
1077 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001078 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001079 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1080 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1081 0);
1082 } else {
1083 // First half
1084 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1085 mi_row_top, mi_col_top, dst_buf, dst_stride,
1086 top_bsize, subsize, 0, 0);
1087 if (bsize < top_bsize)
1088 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1089 mi_row_top, mi_col_top, dst_buf, dst_stride);
1090 else
1091 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1092 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1093
1094 // Second half
1095 if (mi_col + hbs < cm->mi_cols) {
1096 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1097 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1098 dst_stride1, top_bsize, subsize, 0, 0);
1099 if (bsize < top_bsize)
1100 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1101 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1102 dst_stride1);
1103 else
1104 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1105 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1106 dst_stride1, 2);
1107
1108 // Smooth
1109 for (i = 0; i < MAX_MB_PLANE; i++) {
1110 xd->plane[i].dst.buf = dst_buf[i];
1111 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001112 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001113 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1114 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1115 PARTITION_VERT, i);
1116 }
1117 }
1118 }
1119 break;
1120 case PARTITION_SPLIT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001121 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001122 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1123 mi_row_top, mi_col_top, dst_buf, dst_stride,
1124 top_bsize, BLOCK_8X8, 1, 0);
1125 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1126 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1127 top_bsize, BLOCK_8X8, 1, 1);
1128 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1129 mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1130 top_bsize, BLOCK_8X8, 1, 1);
1131 dec_predict_b_extend(pbi, xd, tile, 3, mi_row, mi_col, mi_row, mi_col,
1132 mi_row_top, mi_col_top, dst_buf3, dst_stride3,
1133 top_bsize, BLOCK_8X8, 1, 1);
1134 if (bsize < top_bsize) {
1135 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1136 mi_row_top, mi_col_top, dst_buf, dst_stride);
1137 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1138 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1139 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1140 mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1141 dec_extend_all(pbi, xd, tile, 3, subsize, top_bsize, mi_row, mi_col,
1142 mi_row_top, mi_col_top, dst_buf3, dst_stride3);
1143 }
1144 } else {
1145 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row_top,
1146 mi_col_top, subsize, top_bsize, dst_buf,
1147 dst_stride);
1148 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1149 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col + hbs,
1150 mi_row_top, mi_col_top, subsize, top_bsize,
1151 dst_buf1, dst_stride1);
1152 if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols)
1153 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col,
1154 mi_row_top, mi_col_top, subsize, top_bsize,
1155 dst_buf2, dst_stride2);
1156 if (mi_row + hbs < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1157 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col + hbs,
1158 mi_row_top, mi_col_top, subsize, top_bsize,
1159 dst_buf3, dst_stride3);
1160 }
1161 for (i = 0; i < MAX_MB_PLANE; i++) {
Jingning Han9e0976a2016-12-27 17:52:42 -08001162#if !CONFIG_CB4X4
Jingning Han24f24a52016-12-27 10:13:28 -08001163 if (bsize == BLOCK_8X8 && i != 0)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001164 continue; // Skip <4x4 chroma smoothing
Jingning Han9e0976a2016-12-27 17:52:42 -08001165#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001166 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001167 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001168 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1169 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1170 PARTITION_VERT, i);
1171 if (mi_row + hbs < cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001172 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001173 xd, dst_buf2[i], dst_stride2[i], dst_buf3[i], dst_stride3[i],
1174 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1175 PARTITION_VERT, i);
Yaowu Xuf883b422016-08-30 14:01:10 -07001176 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001177 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1178 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1179 PARTITION_HORZ, i);
1180 }
1181 } else if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001182 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001183 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1184 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1185 PARTITION_HORZ, i);
1186 }
1187 }
1188 break;
1189#if CONFIG_EXT_PARTITION_TYPES
1190 case PARTITION_HORZ_A:
1191 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1192 mi_row_top, mi_col_top, dst_buf, dst_stride,
1193 top_bsize, bsize2, 0, 0);
1194 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1195 mi_row_top, mi_col_top, dst_buf, dst_stride);
1196
1197 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1198 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1199 dst_stride1, top_bsize, bsize2, 0, 0);
1200 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1201 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1202
1203 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1204 mi_col, mi_row_top, mi_col_top, dst_buf2,
1205 dst_stride2, top_bsize, subsize, 0, 0);
1206 if (bsize < top_bsize)
1207 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1208 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1209 else
1210 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1211 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1212 1);
1213
1214 for (i = 0; i < MAX_MB_PLANE; i++) {
1215 xd->plane[i].dst.buf = dst_buf[i];
1216 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001217 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001218 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1219 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1220 i);
1221 }
1222 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001223 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001224 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1225 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1226 i);
1227 }
1228 break;
1229 case PARTITION_VERT_A:
1230
1231 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1232 mi_row_top, mi_col_top, dst_buf, dst_stride,
1233 top_bsize, bsize2, 0, 0);
1234 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1235 mi_row_top, mi_col_top, dst_buf, dst_stride);
1236
1237 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1238 mi_col, mi_row_top, mi_col_top, dst_buf1,
1239 dst_stride1, top_bsize, bsize2, 0, 0);
1240 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1241 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1242
1243 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1244 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1245 dst_stride2, top_bsize, subsize, 0, 0);
1246 if (bsize < top_bsize)
1247 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1248 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1249 dst_stride2);
1250 else
1251 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1252 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1253 dst_stride2, 2);
1254
1255 for (i = 0; i < MAX_MB_PLANE; i++) {
1256 xd->plane[i].dst.buf = dst_buf[i];
1257 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001258 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001259 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1260 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1261 i);
1262 }
1263 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001264 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001265 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1266 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1267 i);
1268 }
1269 break;
1270 case PARTITION_HORZ_B:
1271 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1272 mi_row_top, mi_col_top, dst_buf, dst_stride,
1273 top_bsize, subsize, 0, 0);
1274 if (bsize < top_bsize)
1275 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1276 mi_row_top, mi_col_top, dst_buf, dst_stride);
1277 else
1278 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1279 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1280
1281 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1282 mi_col, mi_row_top, mi_col_top, dst_buf1,
1283 dst_stride1, top_bsize, bsize2, 0, 0);
1284 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1285 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1286
1287 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1288 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1289 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1290 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1291 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1292 dst_stride2);
1293
1294 for (i = 0; i < MAX_MB_PLANE; i++) {
1295 xd->plane[i].dst.buf = dst_buf1[i];
1296 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001297 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001298 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1299 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1300 PARTITION_VERT, i);
1301 }
1302 for (i = 0; i < MAX_MB_PLANE; i++) {
1303 xd->plane[i].dst.buf = dst_buf[i];
1304 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001305 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001306 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1307 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1308 i);
1309 }
1310 break;
1311 case PARTITION_VERT_B:
1312 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1313 mi_row_top, mi_col_top, dst_buf, dst_stride,
1314 top_bsize, subsize, 0, 0);
1315 if (bsize < top_bsize)
1316 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1317 mi_row_top, mi_col_top, dst_buf, dst_stride);
1318 else
1319 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1320 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1321
1322 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1323 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1324 dst_stride1, top_bsize, bsize2, 0, 0);
1325 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1326 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1327
1328 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1329 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1330 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1331 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1332 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1333 dst_stride2);
1334
1335 for (i = 0; i < MAX_MB_PLANE; i++) {
1336 xd->plane[i].dst.buf = dst_buf1[i];
1337 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001338 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001339 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1340 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1341 PARTITION_HORZ, i);
1342 }
1343 for (i = 0; i < MAX_MB_PLANE; i++) {
1344 xd->plane[i].dst.buf = dst_buf[i];
1345 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001346 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001347 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1348 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1349 i);
1350 }
1351 break;
1352#endif // CONFIG_EXT_PARTITION_TYPES
1353 default: assert(0);
1354 }
1355}
1356
Yaowu Xuf883b422016-08-30 14:01:10 -07001357static void set_segment_id_supertx(const AV1_COMMON *const cm, const int mi_row,
1358 const int mi_col, const BLOCK_SIZE bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001359 const struct segmentation *seg = &cm->seg;
Jingning Han5b7706a2016-12-21 09:55:10 -08001360 const int miw = AOMMIN(mi_size_wide[bsize], cm->mi_cols - mi_col);
1361 const int mih = AOMMIN(mi_size_high[bsize], cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001362 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1363 MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
1364 int r, c;
1365 int seg_id_supertx = MAX_SEGMENTS;
1366
1367 if (!seg->enabled) {
1368 seg_id_supertx = 0;
1369 } else {
1370 // Find the minimum segment_id
1371 for (r = 0; r < mih; r++)
1372 for (c = 0; c < miw; c++)
1373 seg_id_supertx =
Yaowu Xuf883b422016-08-30 14:01:10 -07001374 AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001375 assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);
1376 }
1377
1378 // Assign the the segment_id back to segment_id_supertx
1379 for (r = 0; r < mih; r++)
1380 for (c = 0; c < miw; c++)
1381 mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
1382}
1383#endif // CONFIG_SUPERTX
1384
Yaowu Xuf883b422016-08-30 14:01:10 -07001385static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001386#if CONFIG_SUPERTX
1387 int supertx_enabled,
1388#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001389 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001390#if CONFIG_EXT_PARTITION_TYPES
1391 PARTITION_TYPE partition,
1392#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001393 BLOCK_SIZE bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001394 AV1_COMMON *const cm = &pbi->common;
Jingning Han85dc03f2016-12-06 16:03:10 -08001395 const int bw = mi_size_wide[bsize];
1396 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -07001397 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1398 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Nathan E. Eggeebbd4792016-10-05 19:30:15 -04001399 MB_MODE_INFO *mbmi;
1400
Michael Bebenita6048d052016-08-25 14:40:54 -07001401#if CONFIG_ACCOUNTING
1402 aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
1403#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001404#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001405 if (supertx_enabled) {
1406 mbmi = set_mb_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
1407 } else {
Jingning Hanfaad0e12016-12-07 10:54:57 -08001408 mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001409 }
1410#if CONFIG_EXT_PARTITION_TYPES
1411 xd->mi[0]->mbmi.partition = partition;
1412#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001413 av1_read_mode_info(pbi, xd, supertx_enabled, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001414#else
Jingning Hanfaad0e12016-12-07 10:54:57 -08001415 mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001416#if CONFIG_EXT_PARTITION_TYPES
1417 xd->mi[0]->mbmi.partition = partition;
1418#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001419 av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001420#endif // CONFIG_SUPERTX
1421
1422 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1423 const BLOCK_SIZE uv_subsize =
1424 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1425 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -07001426 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001427 "Invalid block size.");
1428 }
1429
1430#if CONFIG_SUPERTX
1431 mbmi->segment_id_supertx = MAX_SEGMENTS;
1432
1433 if (supertx_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001434 xd->corrupted |= aom_reader_has_error(r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001435 return;
1436 }
1437#endif // CONFIG_SUPERTX
1438
Arild Fuldseth07441162016-08-15 15:07:52 +02001439#if CONFIG_DELTA_Q
1440 if (cm->delta_q_present_flag) {
1441 int i;
1442 for (i = 0; i < MAX_SEGMENTS; i++) {
1443 xd->plane[0].seg_dequant[i][0] =
1444 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
1445 xd->plane[0].seg_dequant[i][1] =
1446 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
1447 xd->plane[1].seg_dequant[i][0] =
1448 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1449 xd->plane[1].seg_dequant[i][1] =
1450 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1451 xd->plane[2].seg_dequant[i][0] =
1452 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1453 xd->plane[2].seg_dequant[i][1] =
1454 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1455 }
1456 }
1457#endif
1458
Jingning Han41bb3392016-12-14 10:46:48 -08001459#if CONFIG_CB4X4
1460 if (mbmi->skip) reset_skip_context(xd, bsize);
1461#else
Jingning Hand39cc722016-12-02 14:03:26 -08001462 if (mbmi->skip) reset_skip_context(xd, AOMMAX(BLOCK_8X8, bsize));
Jingning Han41bb3392016-12-14 10:46:48 -08001463#endif
Jingning Hand39cc722016-12-02 14:03:26 -08001464
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001465#if CONFIG_COEF_INTERLEAVE
1466 {
1467 const struct macroblockd_plane *const pd_y = &xd->plane[0];
1468 const struct macroblockd_plane *const pd_c = &xd->plane[1];
1469 const TX_SIZE tx_log2_y = mbmi->tx_size;
1470 const TX_SIZE tx_log2_c = get_uv_tx_size(mbmi, pd_c);
1471 const int tx_sz_y = (1 << tx_log2_y);
1472 const int tx_sz_c = (1 << tx_log2_c);
1473 const int num_4x4_w_y = pd_y->n4_w;
1474 const int num_4x4_h_y = pd_y->n4_h;
1475 const int num_4x4_w_c = pd_c->n4_w;
1476 const int num_4x4_h_c = pd_c->n4_h;
1477 const int max_4x4_w_y = get_max_4x4_size(num_4x4_w_y, xd->mb_to_right_edge,
1478 pd_y->subsampling_x);
1479 const int max_4x4_h_y = get_max_4x4_size(num_4x4_h_y, xd->mb_to_bottom_edge,
1480 pd_y->subsampling_y);
1481 const int max_4x4_w_c = get_max_4x4_size(num_4x4_w_c, xd->mb_to_right_edge,
1482 pd_c->subsampling_x);
1483 const int max_4x4_h_c = get_max_4x4_size(num_4x4_h_c, xd->mb_to_bottom_edge,
1484 pd_c->subsampling_y);
1485
1486 // The max_4x4_w/h may be smaller than tx_sz under some corner cases,
1487 // i.e. when the SB is splitted by tile boundaries.
1488 const int tu_num_w_y = (max_4x4_w_y + tx_sz_y - 1) / tx_sz_y;
1489 const int tu_num_h_y = (max_4x4_h_y + tx_sz_y - 1) / tx_sz_y;
1490 const int tu_num_w_c = (max_4x4_w_c + tx_sz_c - 1) / tx_sz_c;
1491 const int tu_num_h_c = (max_4x4_h_c + tx_sz_c - 1) / tx_sz_c;
1492 const int tu_num_y = tu_num_w_y * tu_num_h_y;
1493 const int tu_num_c = tu_num_w_c * tu_num_h_c;
1494
1495 if (!is_inter_block(mbmi)) {
1496 int tu_idx_c = 0;
1497 int row_y, col_y, row_c, col_c;
1498 int plane;
1499
1500#if CONFIG_PALETTE
1501 for (plane = 0; plane <= 1; ++plane) {
1502 if (mbmi->palette_mode_info.palette_size[plane])
1503 av1_decode_palette_tokens(xd, plane, r);
1504 }
1505#endif
1506
1507 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1508 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1509 // luma
1510 predict_and_reconstruct_intra_block(
1511 cm, xd, r, mbmi, 0, row_y * tx_sz_y, col_y * tx_sz_y, tx_log2_y);
1512 // chroma
1513 if (tu_idx_c < tu_num_c) {
1514 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1515 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1516 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c,
1517 col_c, tx_log2_c);
1518 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c,
1519 col_c, tx_log2_c);
1520 tu_idx_c++;
1521 }
1522 }
1523 }
1524
1525 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1526 while (tu_idx_c < tu_num_c) {
1527 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1528 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1529 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c, col_c,
1530 tx_log2_c);
1531 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c, col_c,
1532 tx_log2_c);
1533 tu_idx_c++;
1534 }
1535 } else {
1536 // Prediction
David Barkerac37fa32016-12-02 12:30:21 +00001537 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001538 AOMMAX(bsize, BLOCK_8X8));
1539
1540 // Reconstruction
1541 if (!mbmi->skip) {
1542 int eobtotal = 0;
1543 int tu_idx_c = 0;
1544 int row_y, col_y, row_c, col_c;
1545
1546 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1547 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1548 // luma
1549 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 0,
1550 row_y * tx_sz_y,
1551 col_y * tx_sz_y, tx_log2_y);
1552 // chroma
1553 if (tu_idx_c < tu_num_c) {
1554 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1555 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1556 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1557 1, row_c, col_c, tx_log2_c);
1558 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1559 2, row_c, col_c, tx_log2_c);
1560 tu_idx_c++;
1561 }
1562 }
1563 }
1564
1565 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1566 while (tu_idx_c < tu_num_c) {
1567 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1568 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1569 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 1,
1570 row_c, col_c, tx_log2_c);
1571 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 2,
1572 row_c, col_c, tx_log2_c);
1573 tu_idx_c++;
1574 }
1575
1576 if (bsize >= BLOCK_8X8 && eobtotal == 0)
1577#if CONFIG_MISC_FIXES
1578 mbmi->has_no_coeffs = 1;
1579#else
1580 mbmi->skip = 1;
1581#endif
1582 }
1583 }
1584 }
1585#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001586 if (!is_inter_block(mbmi)) {
1587 int plane;
Urvang Joshib100db72016-10-12 16:28:56 -07001588#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001589 for (plane = 0; plane <= 1; ++plane) {
1590 if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xuf883b422016-08-30 14:01:10 -07001591 av1_decode_palette_tokens(xd, plane, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001592 }
Urvang Joshib100db72016-10-12 16:28:56 -07001593#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001594 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1595 const struct macroblockd_plane *const pd = &xd->plane[plane];
Debargha Mukherjee2f123402016-08-30 17:43:38 -07001596 const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han2d64f122016-10-21 12:44:29 -07001597 const int stepr = tx_size_high_unit[tx_size];
1598 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han41bb3392016-12-14 10:46:48 -08001599#if CONFIG_CB4X4
1600 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1601#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001602 const BLOCK_SIZE plane_bsize =
1603 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001604#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001605 int row, col;
Jingning Hanbafee8d2016-12-02 10:25:03 -08001606 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1607 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001608
1609 for (row = 0; row < max_blocks_high; row += stepr)
1610 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001611 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane, row, col,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001612 tx_size);
1613 }
1614 } else {
Yue Chen69f18e12016-09-08 14:48:15 -07001615// Prediction
1616#if CONFIG_WARPED_MOTION
1617 if (mbmi->motion_mode == WARPED_CAUSAL) {
1618 int i;
1619#if CONFIG_AOM_HIGHBITDEPTH
1620 int use_hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH;
1621#endif // CONFIG_AOM_HIGHBITDEPTH
1622
1623 for (i = 0; i < 3; ++i) {
1624 const struct macroblockd_plane *pd = &xd->plane[i];
1625
1626 av1_warp_plane(&mbmi->wm_params[0],
1627#if CONFIG_AOM_HIGHBITDEPTH
1628 xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH, xd->bd,
1629#endif // CONFIG_AOM_HIGHBITDEPTH
1630 pd->pre[0].buf0, pd->pre[0].width, pd->pre[0].height,
1631 pd->pre[0].stride, pd->dst.buf,
1632 ((mi_col * MI_SIZE) >> pd->subsampling_x),
1633 ((mi_row * MI_SIZE) >> pd->subsampling_y),
Jingning Hanff6ee6a2016-12-07 09:55:21 -08001634 xd->n8_w * (MI_SIZE >> pd->subsampling_x),
1635 xd->n8_h * (MI_SIZE >> pd->subsampling_y),
1636 pd->dst.stride, pd->subsampling_x, pd->subsampling_y, 16,
1637 16, 0);
Yue Chen69f18e12016-09-08 14:48:15 -07001638 }
1639 } else {
1640#endif // CONFIG_WARPED_MOTION
Jingning Han41bb3392016-12-14 10:46:48 -08001641#if CONFIG_CB4X4
1642 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL, bsize);
1643#else
1644 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
1645 AOMMAX(bsize, BLOCK_8X8));
1646#endif
Yue Chen69f18e12016-09-08 14:48:15 -07001647#if CONFIG_WARPED_MOTION
1648 }
1649#endif // CONFIG_WARPED_MOTION
Yue Chencb60b182016-10-13 15:18:22 -07001650#if CONFIG_MOTION_VAR
1651 if (mbmi->motion_mode == OBMC_CAUSAL) {
Yue Chen894fcce2016-10-21 16:50:52 -07001652 av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001653 }
Yue Chencb60b182016-10-13 15:18:22 -07001654#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001655
1656 // Reconstruction
1657 if (!mbmi->skip) {
1658 int eobtotal = 0;
1659 int plane;
1660
1661 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1662 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han41bb3392016-12-14 10:46:48 -08001663#if CONFIG_CB4X4
1664 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1665#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001666 const BLOCK_SIZE plane_bsize =
1667 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001668#endif
Jingning Hanbafee8d2016-12-02 10:25:03 -08001669 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1670 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001671 int row, col;
1672#if CONFIG_VAR_TX
Jingning Han70e5f3f2016-11-09 17:03:07 -08001673 const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07001674 const int bh_var_tx = tx_size_high_unit[max_tx_size];
1675 const int bw_var_tx = tx_size_wide_unit[max_tx_size];
Jingning Hanfe45b212016-11-22 10:30:23 -08001676
Jingning Hanbafee8d2016-12-02 10:25:03 -08001677 for (row = 0; row < max_blocks_high; row += bh_var_tx)
1678 for (col = 0; col < max_blocks_wide; col += bw_var_tx)
Jingning Hanfe45b212016-11-22 10:30:23 -08001679 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, row, col,
1680 max_tx_size, &eobtotal);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001681#else
1682 const TX_SIZE tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -07001683 plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han2d64f122016-10-21 12:44:29 -07001684 const int stepr = tx_size_high_unit[tx_size];
1685 const int stepc = tx_size_wide_unit[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001686 for (row = 0; row < max_blocks_high; row += stepr)
1687 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001688 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1689 plane, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001690#endif
1691 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001692 }
1693 }
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001694#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001695
Yaowu Xuf883b422016-08-30 14:01:10 -07001696 xd->corrupted |= aom_reader_has_error(r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001697}
1698
Yaowu Xuf883b422016-08-30 14:01:10 -07001699static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
1700 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001701 int has_rows, int has_cols,
Jingning Han1beb0102016-12-07 11:08:30 -08001702 BLOCK_SIZE bsize) {
1703 const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
Yaowu Xuf883b422016-08-30 14:01:10 -07001704 const aom_prob *const probs = cm->fc->partition_prob[ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001705 FRAME_COUNTS *counts = xd->counts;
1706 PARTITION_TYPE p;
1707
1708 if (has_rows && has_cols)
1709#if CONFIG_EXT_PARTITION_TYPES
1710 if (bsize <= BLOCK_8X8)
Michael Bebenita6048d052016-08-25 14:40:54 -07001711 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001712 else
Michael Bebenita6048d052016-08-25 14:40:54 -07001713 p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs,
1714 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001715#else
Nathan E. Egge9d9eb6c2016-11-16 09:44:26 -05001716#if CONFIG_EC_MULTISYMBOL
Yaowu Xuf2581a32016-10-20 13:05:47 -07001717 p = (PARTITION_TYPE)aom_read_symbol(r, cm->fc->partition_cdf[ctx],
1718 PARTITION_TYPES, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001719#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001720 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001721#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001722#endif // CONFIG_EXT_PARTITION_TYPES
1723 else if (!has_rows && has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001724 p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001725 else if (has_rows && !has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001726 p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001727 else
1728 p = PARTITION_SPLIT;
1729
1730 if (counts) ++counts->partition[ctx][p];
1731
1732 return p;
1733}
1734
1735#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001736static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
1737 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001738 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
1739 return 1;
1740 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001741 const int ctx = av1_get_skip_context(xd);
Michael Bebenita6048d052016-08-25 14:40:54 -07001742 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001743 FRAME_COUNTS *counts = xd->counts;
1744 if (counts) ++counts->skip[ctx][skip];
1745 return skip;
1746 }
1747}
1748#endif // CONFIG_SUPERTX
Yaowu Xud71be782016-10-14 08:47:03 -07001749#if CONFIG_CLPF
1750static int clpf_all_skip(const AV1_COMMON *cm, int mi_col, int mi_row,
1751 int size) {
1752 int r, c;
1753 int skip = 1;
1754 const int maxc = AOMMIN(size, cm->mi_cols - mi_col);
1755 const int maxr = AOMMIN(size, cm->mi_rows - mi_row);
1756 for (r = 0; r < maxr && skip; r++) {
1757 for (c = 0; c < maxc && skip; c++) {
1758 skip &= !!cm->mi_grid_visible[(mi_row + r) * cm->mi_stride + mi_col + c]
1759 ->mbmi.skip;
1760 }
1761 }
1762 return skip;
1763}
1764#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001765
1766// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -07001767static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001768#if CONFIG_SUPERTX
1769 int supertx_enabled,
1770#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001771 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001772 BLOCK_SIZE bsize, int n4x4_l2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001773 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001774 const int n8x8_l2 = n4x4_l2 - 1;
Jingning Hanff17e162016-12-07 17:58:18 -08001775 const int num_8x8_wh = mi_size_wide[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001776 const int hbs = num_8x8_wh >> 1;
Jingning Han41bb3392016-12-14 10:46:48 -08001777#if CONFIG_CB4X4
1778 const int unify_bsize = 1;
1779#else
1780 const int unify_bsize = 0;
1781#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001782 PARTITION_TYPE partition;
1783 BLOCK_SIZE subsize;
1784#if CONFIG_EXT_PARTITION_TYPES
1785 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1786#endif
1787 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1788 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1789#if CONFIG_SUPERTX
1790 const int read_token = !supertx_enabled;
1791 int skip = 0;
Jingning Han2511c662016-12-22 11:57:34 -08001792 TX_SIZE supertx_size = max_txsize_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001793 const TileInfo *const tile = &xd->tile;
1794 int txfm = DCT_DCT;
1795#endif // CONFIG_SUPERTX
1796
1797 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1798
Jingning Han6dbacf22016-12-14 17:28:35 -08001799 partition = (n4x4_l2 == 0) ? PARTITION_NONE
1800 : read_partition(cm, xd, mi_row, mi_col, r,
1801 has_rows, has_cols, bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001802 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
Yushin Cho77bba8d2016-11-04 16:36:56 -07001803
1804#if CONFIG_PVQ
1805 assert(partition < PARTITION_TYPES);
1806 assert(subsize < BLOCK_SIZES);
1807#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001808#if CONFIG_SUPERTX
1809 if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
1810 bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
1811 const int supertx_context = partition_supertx_context_lookup[partition];
Michael Bebenita6048d052016-08-25 14:40:54 -07001812 supertx_enabled = aom_read(
1813 r, cm->fc->supertx_prob[supertx_context][supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001814 if (xd->counts)
1815 xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
1816#if CONFIG_VAR_TX
1817 if (supertx_enabled) xd->supertx_size = supertx_size;
1818#endif
1819 }
1820#endif // CONFIG_SUPERTX
Jingning Han41bb3392016-12-14 10:46:48 -08001821 if (!hbs && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001822 // calculate bmode block dimensions (log 2)
1823 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1824 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1825 decode_block(pbi, xd,
1826#if CONFIG_SUPERTX
1827 supertx_enabled,
1828#endif // CONFIG_SUPERTX
1829 mi_row, mi_col, r,
1830#if CONFIG_EXT_PARTITION_TYPES
1831 partition,
1832#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001833 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001834 } else {
1835 switch (partition) {
1836 case PARTITION_NONE:
1837 decode_block(pbi, xd,
1838#if CONFIG_SUPERTX
1839 supertx_enabled,
1840#endif // CONFIG_SUPERTX
1841 mi_row, mi_col, r,
1842#if CONFIG_EXT_PARTITION_TYPES
1843 partition,
1844#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001845 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001846 break;
1847 case PARTITION_HORZ:
1848 decode_block(pbi, xd,
1849#if CONFIG_SUPERTX
1850 supertx_enabled,
1851#endif // CONFIG_SUPERTX
1852 mi_row, mi_col, r,
1853#if CONFIG_EXT_PARTITION_TYPES
1854 partition,
1855#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001856 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001857 if (has_rows)
1858 decode_block(pbi, xd,
1859#if CONFIG_SUPERTX
1860 supertx_enabled,
1861#endif // CONFIG_SUPERTX
1862 mi_row + hbs, mi_col, r,
1863#if CONFIG_EXT_PARTITION_TYPES
1864 partition,
1865#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001866 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001867 break;
1868 case PARTITION_VERT:
1869 decode_block(pbi, xd,
1870#if CONFIG_SUPERTX
1871 supertx_enabled,
1872#endif // CONFIG_SUPERTX
1873 mi_row, mi_col, r,
1874#if CONFIG_EXT_PARTITION_TYPES
1875 partition,
1876#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001877 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001878 if (has_cols)
1879 decode_block(pbi, xd,
1880#if CONFIG_SUPERTX
1881 supertx_enabled,
1882#endif // CONFIG_SUPERTX
1883 mi_row, mi_col + hbs, r,
1884#if CONFIG_EXT_PARTITION_TYPES
1885 partition,
1886#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001887 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001888 break;
1889 case PARTITION_SPLIT:
1890 decode_partition(pbi, xd,
1891#if CONFIG_SUPERTX
1892 supertx_enabled,
1893#endif // CONFIG_SUPERTX
1894 mi_row, mi_col, r, subsize, n8x8_l2);
1895 decode_partition(pbi, xd,
1896#if CONFIG_SUPERTX
1897 supertx_enabled,
1898#endif // CONFIG_SUPERTX
1899 mi_row, mi_col + hbs, r, subsize, n8x8_l2);
1900 decode_partition(pbi, xd,
1901#if CONFIG_SUPERTX
1902 supertx_enabled,
1903#endif // CONFIG_SUPERTX
1904 mi_row + hbs, mi_col, r, subsize, n8x8_l2);
1905 decode_partition(pbi, xd,
1906#if CONFIG_SUPERTX
1907 supertx_enabled,
1908#endif // CONFIG_SUPERTX
1909 mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
1910 break;
1911#if CONFIG_EXT_PARTITION_TYPES
1912 case PARTITION_HORZ_A:
1913 decode_block(pbi, xd,
1914#if CONFIG_SUPERTX
1915 supertx_enabled,
1916#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001917 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001918 decode_block(pbi, xd,
1919#if CONFIG_SUPERTX
1920 supertx_enabled,
1921#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001922 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001923 decode_block(pbi, xd,
1924#if CONFIG_SUPERTX
1925 supertx_enabled,
1926#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001927 mi_row + hbs, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001928 break;
1929 case PARTITION_HORZ_B:
1930 decode_block(pbi, xd,
1931#if CONFIG_SUPERTX
1932 supertx_enabled,
1933#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001934 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001935 decode_block(pbi, xd,
1936#if CONFIG_SUPERTX
1937 supertx_enabled,
1938#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001939 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001940 decode_block(pbi, xd,
1941#if CONFIG_SUPERTX
1942 supertx_enabled,
1943#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001944 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001945 break;
1946 case PARTITION_VERT_A:
1947 decode_block(pbi, xd,
1948#if CONFIG_SUPERTX
1949 supertx_enabled,
1950#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001951 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001952 decode_block(pbi, xd,
1953#if CONFIG_SUPERTX
1954 supertx_enabled,
1955#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001956 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001957 decode_block(pbi, xd,
1958#if CONFIG_SUPERTX
1959 supertx_enabled,
1960#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001961 mi_row, mi_col + hbs, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001962 break;
1963 case PARTITION_VERT_B:
1964 decode_block(pbi, xd,
1965#if CONFIG_SUPERTX
1966 supertx_enabled,
1967#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001968 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001969 decode_block(pbi, xd,
1970#if CONFIG_SUPERTX
1971 supertx_enabled,
1972#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001973 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001974 decode_block(pbi, xd,
1975#if CONFIG_SUPERTX
1976 supertx_enabled,
1977#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001978 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001979 break;
1980#endif
1981 default: assert(0 && "Invalid partition type");
1982 }
1983 }
1984
1985#if CONFIG_SUPERTX
1986 if (supertx_enabled && read_token) {
1987 uint8_t *dst_buf[3];
1988 int dst_stride[3], i;
1989 int offset = mi_row * cm->mi_stride + mi_col;
1990
1991 set_segment_id_supertx(cm, mi_row, mi_col, bsize);
1992
1993 xd->mi = cm->mi_grid_visible + offset;
1994 xd->mi[0] = cm->mi + offset;
Jingning Han5b7706a2016-12-21 09:55:10 -08001995 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
1996 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001997 set_skip_context(xd, mi_row, mi_col);
1998 skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
1999 if (skip) {
2000 reset_skip_context(xd, bsize);
2001 } else {
2002#if CONFIG_EXT_TX
2003 if (get_ext_tx_types(supertx_size, bsize, 1) > 1) {
2004 int eset = get_ext_tx_set(supertx_size, bsize, 1);
2005 if (eset > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002006 txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
Michael Bebenita6048d052016-08-25 14:40:54 -07002007 cm->fc->inter_ext_tx_prob[eset][supertx_size],
2008 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002009 if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
2010 }
2011 }
2012#else
2013 if (supertx_size < TX_32X32) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002014 txfm = aom_read_tree(r, av1_ext_tx_tree,
Jingning Han8f6eb182016-10-19 13:48:57 -07002015 cm->fc->inter_ext_tx_prob[supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002016 if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
2017 }
2018#endif // CONFIG_EXT_TX
2019 }
2020
Yaowu Xuf883b422016-08-30 14:01:10 -07002021 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002022 for (i = 0; i < MAX_MB_PLANE; i++) {
2023 dst_buf[i] = xd->plane[i].dst.buf;
2024 dst_stride[i] = xd->plane[i].dst.stride;
2025 }
2026 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
2027 bsize, dst_buf, dst_stride);
2028
2029 if (!skip) {
2030 int eobtotal = 0;
2031 MB_MODE_INFO *mbmi;
2032 set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
2033 mbmi = &xd->mi[0]->mbmi;
2034 mbmi->tx_type = txfm;
2035 assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
2036 for (i = 0; i < MAX_MB_PLANE; ++i) {
2037 const struct macroblockd_plane *const pd = &xd->plane[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002038 int row, col;
Debargha Mukherjee2f123402016-08-30 17:43:38 -07002039 const TX_SIZE tx_size = i ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han5b7706a2016-12-21 09:55:10 -08002040 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Jingning Han32b20282016-10-28 15:42:44 -07002041 const int stepr = tx_size_high_unit[tx_size];
2042 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han5b7706a2016-12-21 09:55:10 -08002043 const int max_blocks_wide = max_block_wide(xd, plane_bsize, i);
2044 const int max_blocks_high = max_block_high(xd, plane_bsize, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002045
2046 for (row = 0; row < max_blocks_high; row += stepr)
2047 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07002048 eobtotal += reconstruct_inter_block(
2049 cm, xd, r, mbmi->segment_id_supertx, i, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002050 }
Jingning Han24f24a52016-12-27 10:13:28 -08002051 if ((unify_bsize || !(subsize < BLOCK_8X8)) && eobtotal == 0) skip = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002052 }
2053 set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
2054 }
2055#endif // CONFIG_SUPERTX
2056
2057#if CONFIG_EXT_PARTITION_TYPES
2058 if (bsize >= BLOCK_8X8) {
2059 switch (partition) {
2060 case PARTITION_SPLIT:
2061 if (bsize > BLOCK_8X8) break;
2062 case PARTITION_NONE:
2063 case PARTITION_HORZ:
2064 case PARTITION_VERT:
2065 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
2066 break;
2067 case PARTITION_HORZ_A:
2068 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2069 update_partition_context(xd, mi_row + hbs, mi_col, subsize, subsize);
2070 break;
2071 case PARTITION_HORZ_B:
2072 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2073 update_partition_context(xd, mi_row + hbs, mi_col, bsize2, subsize);
2074 break;
2075 case PARTITION_VERT_A:
2076 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2077 update_partition_context(xd, mi_row, mi_col + hbs, subsize, subsize);
2078 break;
2079 case PARTITION_VERT_B:
2080 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2081 update_partition_context(xd, mi_row, mi_col + hbs, bsize2, subsize);
2082 break;
2083 default: assert(0 && "Invalid partition type");
2084 }
2085 }
2086#else
2087 // update partition context
2088 if (bsize >= BLOCK_8X8 &&
2089 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
Jingning Han1beb0102016-12-07 11:08:30 -08002090 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
David Barkerf8935c92016-10-26 14:54:06 +01002091#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xud71be782016-10-14 08:47:03 -07002092
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002093#if CONFIG_DERING
David Barker9739f362016-11-10 09:29:32 +00002094#if CONFIG_EXT_PARTITION
2095 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128) {
2096 if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
2097 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2098 aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
2099 } else {
2100 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2101 0;
2102 }
2103 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64) {
2104#else
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002105 if (bsize == BLOCK_64X64) {
David Barker9739f362016-11-10 09:29:32 +00002106#endif
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002107 if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
2108 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2109 aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
2110 } else {
2111 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2112 0;
2113 }
2114 }
2115#endif
2116
Yaowu Xud71be782016-10-14 08:47:03 -07002117#if CONFIG_CLPF
David Barker9739f362016-11-10 09:29:32 +00002118#if CONFIG_EXT_PARTITION
2119 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128 &&
2120 cm->clpf_strength_y && cm->clpf_size != CLPF_NOSIZE) {
2121 const int tl = mi_row * MI_SIZE / MIN_FB_SIZE * cm->clpf_stride +
2122 mi_col * MI_SIZE / MIN_FB_SIZE;
2123 if (cm->clpf_size == CLPF_128X128) {
2124 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
2125 } else if (cm->clpf_size == CLPF_64X64) {
2126 const int tr = tl + 2;
2127 const int bl = tl + 2 * cm->clpf_stride;
2128 const int br = tr + 2 * cm->clpf_stride;
2129 const int size = 64 / MI_SIZE;
2130
2131 // Up to four bits per SB
2132 if (!clpf_all_skip(cm, mi_col, mi_row, size))
2133 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
2134
2135 if (mi_col + size < cm->mi_cols &&
2136 !clpf_all_skip(cm, mi_col + size, mi_row, size))
2137 cm->clpf_blocks[tr] = aom_read_literal(r, 1, ACCT_STR);
2138
2139 if (mi_row + size < cm->mi_rows &&
2140 !clpf_all_skip(cm, mi_col, mi_row + size, size))
2141 cm->clpf_blocks[bl] = aom_read_literal(r, 1, ACCT_STR);
2142
2143 if (mi_col + size < cm->mi_cols && mi_row + size < cm->mi_rows &&
2144 !clpf_all_skip(cm, mi_col + size, mi_row + size, size))
2145 cm->clpf_blocks[br] = aom_read_literal(r, 1, ACCT_STR);
2146 } else if (cm->clpf_size == CLPF_32X32) {
2147 int i, j;
2148 const int size = 32 / MI_SIZE;
2149 for (i = 0; i < 4; ++i)
2150 for (j = 0; j < 4; ++j) {
2151 const int index = tl + i * cm->clpf_stride + j;
2152 if (mi_row + i * size < cm->mi_rows &&
2153 mi_col + j * size < cm->mi_cols &&
2154 !clpf_all_skip(cm, mi_col + j * size, mi_row + i * size, size))
2155 cm->clpf_blocks[index] = aom_read_literal(r, 1, ACCT_STR);
2156 }
2157 }
2158 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64 &&
2159#else
2160 if (bsize == BLOCK_64X64 &&
2161#endif // CONFIG_EXT_PARTITION
2162 cm->clpf_strength_y && cm->clpf_size != CLPF_NOSIZE) {
Yaowu Xud71be782016-10-14 08:47:03 -07002163 const int tl = mi_row * MI_SIZE / MIN_FB_SIZE * cm->clpf_stride +
2164 mi_col * MI_SIZE / MIN_FB_SIZE;
2165
2166 if (!((mi_row * MI_SIZE) & 127) && !((mi_col * MI_SIZE) & 127) &&
2167 cm->clpf_size == CLPF_128X128) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002168 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002169 } else if (cm->clpf_size == CLPF_64X64 &&
2170 !clpf_all_skip(cm, mi_col, mi_row, 64 / MI_SIZE)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002171 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002172 } else if (cm->clpf_size == CLPF_32X32) {
2173 const int tr = tl + 1;
2174 const int bl = tl + cm->clpf_stride;
2175 const int br = tr + cm->clpf_stride;
2176 const int size = 32 / MI_SIZE;
2177
2178 // Up to four bits per SB
2179 if (!clpf_all_skip(cm, mi_col, mi_row, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002180 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002181
2182 if (mi_col + size < cm->mi_cols &&
2183 !clpf_all_skip(cm, mi_col + size, mi_row, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002184 cm->clpf_blocks[tr] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002185
2186 if (mi_row + size < cm->mi_rows &&
2187 !clpf_all_skip(cm, mi_col, mi_row + size, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002188 cm->clpf_blocks[bl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002189
2190 if (mi_col + size < cm->mi_cols && mi_row + size < cm->mi_rows &&
2191 !clpf_all_skip(cm, mi_col + size, mi_row + size, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002192 cm->clpf_blocks[br] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002193 }
2194 }
David Barker9739f362016-11-10 09:29:32 +00002195#endif // CONFIG_CLPF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002196}
2197
Yaowu Xuc27fc142016-08-22 16:08:15 -07002198static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
2199 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002200 struct aom_internal_error_info *error_info,
2201 aom_reader *r, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002202 void *decrypt_state) {
2203 // Validate the calculated partition length. If the buffer
2204 // described by the partition can't be fully read, then restrict
2205 // it to the portion that can be (for EC mode) or throw an error.
2206 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002207 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002208 "Truncated packet or corrupt tile length");
2209
Alex Converse2cdf0d82016-12-13 13:53:09 -08002210 if (aom_reader_init(r, data, read_size,
2211#if CONFIG_ANS && ANS_MAX_SYMBOLS
2212 ANS_MAX_SYMBOLS,
2213#endif
2214 decrypt_cb, decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07002215 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002216 "Failed to allocate bool decoder %d", 1);
2217}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002218
Yushin Cho77bba8d2016-11-04 16:36:56 -07002219#if !CONFIG_PVQ
Yaowu Xuf883b422016-08-30 14:01:10 -07002220static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
2221 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002222 int i, j, k, l, m;
Thomas9ac55082016-09-23 18:04:17 +01002223#if CONFIG_EC_ADAPT
Thomas Davies09ebbfb2016-10-20 18:28:47 +01002224 const int node_limit = UNCONSTRAINED_NODES - 1;
Thomas9ac55082016-09-23 18:04:17 +01002225#else
2226 const int node_limit = UNCONSTRAINED_NODES;
2227#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002228
Michael Bebenita6048d052016-08-25 14:40:54 -07002229 if (aom_read_bit(r, ACCT_STR))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002230 for (i = 0; i < PLANE_TYPES; ++i)
2231 for (j = 0; j < REF_TYPES; ++j)
2232 for (k = 0; k < COEF_BANDS; ++k)
2233 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
Thomas9ac55082016-09-23 18:04:17 +01002234 for (m = 0; m < node_limit; ++m)
Michael Bebenita6048d052016-08-25 14:40:54 -07002235 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002236}
2237
Yaowu Xuf883b422016-08-30 14:01:10 -07002238static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002239 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
2240 TX_SIZE tx_size;
Jingning Han83630632016-12-16 11:27:25 -08002241 for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002242 read_coef_probs_common(fc->coef_probs[tx_size], r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002243}
Yushin Cho77bba8d2016-11-04 16:36:56 -07002244#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002245
Yaowu Xuf883b422016-08-30 14:01:10 -07002246static void setup_segmentation(AV1_COMMON *const cm,
2247 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002248 struct segmentation *const seg = &cm->seg;
2249 int i, j;
2250
2251 seg->update_map = 0;
2252 seg->update_data = 0;
2253
Yaowu Xuf883b422016-08-30 14:01:10 -07002254 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002255 if (!seg->enabled) return;
2256
2257 // Segmentation map update
2258 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2259 seg->update_map = 1;
2260 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002261 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002262 }
2263 if (seg->update_map) {
2264 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2265 seg->temporal_update = 0;
2266 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002267 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002268 }
2269 }
2270
2271 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07002272 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002273 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002274 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002275
Yaowu Xuf883b422016-08-30 14:01:10 -07002276 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002277
2278 for (i = 0; i < MAX_SEGMENTS; i++) {
2279 for (j = 0; j < SEG_LVL_MAX; j++) {
2280 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002281 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002282 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002283 av1_enable_segfeature(seg, i, j);
2284 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
2285 if (av1_is_segfeature_signed(j))
2286 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002287 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002288 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002289 }
2290 }
2291 }
2292}
2293
2294#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002295static void decode_restoration_mode(AV1_COMMON *cm,
2296 struct aom_read_bit_buffer *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002297 int p;
2298 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002299 if (aom_rb_read_bit(rb)) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002300 if (aom_rb_read_bit(rb))
2301 rsi->frame_restoration_type =
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002302 (aom_rb_read_bit(rb) ? RESTORE_DOMAINTXFMRF : RESTORE_SGRPROJ);
Debargha Mukherjee0e67b252016-12-08 09:22:44 -08002303 else
2304 rsi->frame_restoration_type = RESTORE_WIENER;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002305 } else {
2306 rsi->frame_restoration_type =
2307 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
2308 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002309 for (p = 1; p < MAX_MB_PLANE; ++p) {
2310 cm->rst_info[p].frame_restoration_type =
2311 aom_rb_read_bit(rb) ? RESTORE_WIENER : RESTORE_NONE;
2312 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002313}
2314
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002315static void read_wiener_filter(WienerInfo *wiener_info, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002316 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002317 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2318 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002319 wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002320 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2321 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002322 wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002323 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2324 WIENER_FILT_TAP2_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002325 wiener_info->vfilter[WIENER_HALFWIN] =
2326 WIENER_FILT_STEP -
2327 2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
2328 wiener_info->vfilter[2]);
2329 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002330 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2331 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002332 wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002333 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2334 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002335 wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002336 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2337 WIENER_FILT_TAP2_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002338 wiener_info->hfilter[WIENER_HALFWIN] =
2339 WIENER_FILT_STEP -
2340 2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
2341 wiener_info->hfilter[2]);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002342}
2343
2344static void read_sgrproj_filter(SgrprojInfo *sgrproj_info, aom_reader *rb) {
2345 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
2346 sgrproj_info->xqd[0] =
2347 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN0;
2348 sgrproj_info->xqd[1] =
2349 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN1;
2350}
2351
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002352static void read_domaintxfmrf_filter(DomaintxfmrfInfo *domaintxfmrf_info,
2353 aom_reader *rb) {
2354 domaintxfmrf_info->sigma_r =
2355 aom_read_literal(rb, DOMAINTXFMRF_PARAMS_BITS, ACCT_STR);
2356}
2357
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002358static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002359 int i, p;
clang-formatbda8d612016-09-19 15:55:46 -07002360 const int ntiles =
2361 av1_get_rest_ntiles(cm->width, cm->height, NULL, NULL, NULL, NULL);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002362 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002363 if (rsi->frame_restoration_type != RESTORE_NONE) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002364 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002365 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002366 rsi->restoration_type[i] =
2367 aom_read_tree(rb, av1_switchable_restore_tree,
2368 cm->fc->switchable_restore_prob, ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002369 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07002370 rsi->wiener_info[i].level = 1;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002371 read_wiener_filter(&rsi->wiener_info[i], rb);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002372 } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
2373 rsi->sgrproj_info[i].level = 1;
2374 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002375 } else if (rsi->restoration_type[i] == RESTORE_DOMAINTXFMRF) {
2376 rsi->domaintxfmrf_info[i].level = 1;
2377 read_domaintxfmrf_filter(&rsi->domaintxfmrf_info[i], rb);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002378 }
2379 }
2380 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002381 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002382 if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002383 rsi->restoration_type[i] = RESTORE_WIENER;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002384 rsi->wiener_info[i].level = 1;
2385 read_wiener_filter(&rsi->wiener_info[i], rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002386 } else {
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07002387 rsi->wiener_info[i].level = 0;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002388 rsi->restoration_type[i] = RESTORE_NONE;
2389 }
2390 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002391 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002392 for (i = 0; i < ntiles; ++i) {
2393 if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
2394 rsi->restoration_type[i] = RESTORE_SGRPROJ;
2395 rsi->sgrproj_info[i].level = 1;
2396 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
2397 } else {
2398 rsi->sgrproj_info[i].level = 0;
2399 rsi->restoration_type[i] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002400 }
2401 }
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002402 } else if (rsi->frame_restoration_type == RESTORE_DOMAINTXFMRF) {
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002403 for (i = 0; i < ntiles; ++i) {
2404 if (aom_read(rb, RESTORE_NONE_DOMAINTXFMRF_PROB, ACCT_STR)) {
2405 rsi->restoration_type[i] = RESTORE_DOMAINTXFMRF;
2406 rsi->domaintxfmrf_info[i].level = 1;
2407 read_domaintxfmrf_filter(&rsi->domaintxfmrf_info[i], rb);
2408 } else {
2409 rsi->domaintxfmrf_info[i].level = 0;
2410 rsi->restoration_type[i] = RESTORE_NONE;
2411 }
2412 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002413 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002414 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002415 for (p = 1; p < MAX_MB_PLANE; ++p) {
2416 rsi = &cm->rst_info[p];
2417 if (rsi->frame_restoration_type == RESTORE_WIENER) {
2418 rsi->restoration_type[0] = RESTORE_WIENER;
2419 rsi->wiener_info[0].level = 1;
2420 read_wiener_filter(&rsi->wiener_info[0], rb);
2421 for (i = 1; i < ntiles; ++i) {
2422 rsi->restoration_type[i] = RESTORE_WIENER;
2423 memcpy(&rsi->wiener_info[i], &rsi->wiener_info[0],
2424 sizeof(rsi->wiener_info[0]));
2425 }
2426 }
2427 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002428}
2429#endif // CONFIG_LOOP_RESTORATION
2430
Yaowu Xuf883b422016-08-30 14:01:10 -07002431static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002432 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07002433 lf->filter_level = aom_rb_read_literal(rb, 6);
2434 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002435
2436 // Read in loop filter deltas applied at the MB level based on mode or ref
2437 // frame.
2438 lf->mode_ref_delta_update = 0;
2439
Yaowu Xuf883b422016-08-30 14:01:10 -07002440 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002441 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002442 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002443 if (lf->mode_ref_delta_update) {
2444 int i;
2445
2446 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002447 if (aom_rb_read_bit(rb))
2448 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002449
2450 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002451 if (aom_rb_read_bit(rb))
2452 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002453 }
2454 }
2455}
2456
2457#if CONFIG_CLPF
Yaowu Xud71be782016-10-14 08:47:03 -07002458static void setup_clpf(AV1Decoder *pbi, struct aom_read_bit_buffer *rb) {
2459 AV1_COMMON *const cm = &pbi->common;
2460 const int width = pbi->cur_buf->buf.y_crop_width;
2461 const int height = pbi->cur_buf->buf.y_crop_height;
2462
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002463 cm->clpf_blocks = 0;
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02002464 cm->clpf_strength_y = aom_rb_read_literal(rb, 2);
2465 cm->clpf_strength_u = aom_rb_read_literal(rb, 2);
2466 cm->clpf_strength_v = aom_rb_read_literal(rb, 2);
2467 if (cm->clpf_strength_y) {
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002468 cm->clpf_size = aom_rb_read_literal(rb, 2);
Yaowu Xud71be782016-10-14 08:47:03 -07002469 if (cm->clpf_size != CLPF_NOSIZE) {
2470 int size;
2471 cm->clpf_stride =
2472 ((width + MIN_FB_SIZE - 1) & ~(MIN_FB_SIZE - 1)) >> MIN_FB_SIZE_LOG2;
2473 size =
2474 cm->clpf_stride * ((height + MIN_FB_SIZE - 1) & ~(MIN_FB_SIZE - 1)) >>
2475 MIN_FB_SIZE_LOG2;
2476 CHECK_MEM_ERROR(cm, cm->clpf_blocks, aom_malloc(size));
2477 memset(cm->clpf_blocks, -1, size);
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002478 }
2479 }
2480}
2481
Steinar Midtskogen2fd70ee2016-09-02 10:02:30 +02002482static int clpf_bit(UNUSED int k, UNUSED int l,
2483 UNUSED const YV12_BUFFER_CONFIG *rec,
2484 UNUSED const YV12_BUFFER_CONFIG *org,
2485 UNUSED const AV1_COMMON *cm, UNUSED int block_size,
2486 UNUSED int w, UNUSED int h, UNUSED unsigned int strength,
Yaowu Xud71be782016-10-14 08:47:03 -07002487 UNUSED unsigned int fb_size_log2, int8_t *bit) {
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002488 return *bit;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002489}
2490#endif
2491
2492#if CONFIG_DERING
Yaowu Xuf883b422016-08-30 14:01:10 -07002493static void setup_dering(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
2494 cm->dering_level = aom_rb_read_literal(rb, DERING_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002495}
2496#endif // CONFIG_DERING
2497
Yaowu Xuf883b422016-08-30 14:01:10 -07002498static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2499 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002500}
2501
Yaowu Xuf883b422016-08-30 14:01:10 -07002502static void setup_quantization(AV1_COMMON *const cm,
2503 struct aom_read_bit_buffer *rb) {
2504 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002505 cm->y_dc_delta_q = read_delta_q(rb);
2506 cm->uv_dc_delta_q = read_delta_q(rb);
2507 cm->uv_ac_delta_q = read_delta_q(rb);
2508 cm->dequant_bit_depth = cm->bit_depth;
2509#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002510 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002511 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002512 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2513 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002514 } else {
2515 cm->min_qmlevel = 0;
2516 cm->max_qmlevel = 0;
2517 }
2518#endif
2519}
2520
Yaowu Xuf883b422016-08-30 14:01:10 -07002521static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002522 // Build y/uv dequant values based on segmentation.
2523 int i = 0;
2524#if CONFIG_AOM_QM
2525 int lossless;
2526 int j = 0;
2527 int qmlevel;
2528 int using_qm = cm->using_qmatrix;
2529 int minqm = cm->min_qmlevel;
2530 int maxqm = cm->max_qmlevel;
2531#endif
2532#if CONFIG_NEW_QUANT
2533 int b;
2534 int dq;
2535#endif // CONFIG_NEW_QUANT
2536 if (cm->seg.enabled) {
2537 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002538 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002539 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002540 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2541 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002542 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002543 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002544 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002545 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002546#if CONFIG_AOM_QM
2547 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2548 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2549 // NB: depends on base index so there is only 1 set per frame
2550 // No quant weighting when lossless or signalled not using QM
2551 qmlevel = (lossless || using_qm == 0)
2552 ? NUM_QM_LEVELS - 1
2553 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2554 for (j = 0; j < TX_SIZES; ++j) {
2555 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2556 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2557 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2558 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2559 }
2560#endif // CONFIG_AOM_QM
2561#if CONFIG_NEW_QUANT
2562 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2563 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002564 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002565 cm->y_dequant_nuq[i][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002566 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002567 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002568 }
2569 }
2570#endif // CONFIG_NEW_QUANT
2571 }
2572 } else {
2573 const int qindex = cm->base_qindex;
2574 // When segmentation is disabled, only the first value is used. The
2575 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002576 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2577 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002578 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002579 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002580 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002581 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002582#if CONFIG_AOM_QM
2583 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2584 cm->uv_ac_delta_q == 0;
2585 // No quant weighting when lossless or signalled not using QM
2586 qmlevel = (lossless || using_qm == 0)
2587 ? NUM_QM_LEVELS - 1
2588 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2589 for (j = 0; j < TX_SIZES; ++j) {
2590 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2591 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2592 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2593 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2594 }
2595#endif
2596#if CONFIG_NEW_QUANT
2597 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2598 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002599 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002600 cm->y_dequant_nuq[0][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002601 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002602 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002603 }
2604 }
2605#endif // CONFIG_NEW_QUANT
2606 }
2607}
2608
Angie Chiang5678ad92016-11-21 09:38:40 -08002609static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002610 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07002611 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002612}
2613
Yaowu Xuf883b422016-08-30 14:01:10 -07002614static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002615 cm->render_width = cm->width;
2616 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002617 if (aom_rb_read_bit(rb))
2618 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002619}
2620
Yaowu Xuf883b422016-08-30 14:01:10 -07002621static void resize_mv_buffer(AV1_COMMON *cm) {
2622 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002623 cm->cur_frame->mi_rows = cm->mi_rows;
2624 cm->cur_frame->mi_cols = cm->mi_cols;
2625 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002626 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002627 sizeof(*cm->cur_frame->mvs)));
2628}
2629
Yaowu Xuf883b422016-08-30 14:01:10 -07002630static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002631#if CONFIG_SIZE_LIMIT
2632 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002633 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002634 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2635 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2636#endif
2637 if (cm->width != width || cm->height != height) {
2638 const int new_mi_rows =
2639 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2640 const int new_mi_cols =
2641 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2642
Yaowu Xuf883b422016-08-30 14:01:10 -07002643 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002644 // dimensions as well as the overall size.
2645 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002646 if (av1_alloc_context_buffers(cm, width, height))
2647 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002648 "Failed to allocate context buffers");
2649 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002650 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002651 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002652 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002653 cm->width = width;
2654 cm->height = height;
2655 }
2656 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2657 cm->mi_cols > cm->cur_frame->mi_cols) {
2658 resize_mv_buffer(cm);
2659 }
2660}
2661
Yaowu Xuf883b422016-08-30 14:01:10 -07002662static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002663 int width, height;
2664 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002665 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002666 resize_context_buffers(cm, width, height);
2667 setup_render_size(cm, rb);
2668
2669 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002670 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002671 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2672 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002673#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002674 cm->use_highbitdepth,
2675#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002676 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002677 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2678 pool->cb_priv)) {
2679 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002680 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002681 "Failed to allocate frame buffer");
2682 }
2683 unlock_buffer_pool(pool);
2684
2685 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2686 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2687 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2688 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2689 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2690 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2691 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2692}
2693
Yaowu Xuf883b422016-08-30 14:01:10 -07002694static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002695 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002696 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002697 int this_xss, int this_yss) {
2698 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2699 ref_yss == this_yss;
2700}
2701
Yaowu Xuf883b422016-08-30 14:01:10 -07002702static void setup_frame_size_with_refs(AV1_COMMON *cm,
2703 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002704 int width, height;
2705 int found = 0, i;
2706 int has_valid_ref_frame = 0;
2707 BufferPool *const pool = cm->buffer_pool;
2708 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002709 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002710 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2711 width = buf->y_crop_width;
2712 height = buf->y_crop_height;
2713 cm->render_width = buf->render_width;
2714 cm->render_height = buf->render_height;
2715 found = 1;
2716 break;
2717 }
2718 }
2719
2720 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002721 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002722 setup_render_size(cm, rb);
2723 }
2724
2725 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002726 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002727 "Invalid frame size");
2728
2729 // Check to make sure at least one of frames that this frame references
2730 // has valid dimensions.
2731 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2732 RefBuffer *const ref_frame = &cm->frame_refs[i];
2733 has_valid_ref_frame |=
2734 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2735 ref_frame->buf->y_crop_height, width, height);
2736 }
2737 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002738 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002739 "Referenced frame has invalid size");
2740 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2741 RefBuffer *const ref_frame = &cm->frame_refs[i];
2742 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
2743 ref_frame->buf->subsampling_x,
2744 ref_frame->buf->subsampling_y, cm->bit_depth,
2745 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07002746 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002747 "Referenced frame has incompatible color format");
2748 }
2749
2750 resize_context_buffers(cm, width, height);
2751
2752 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002753 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002754 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2755 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002756#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002757 cm->use_highbitdepth,
2758#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002759 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002760 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2761 pool->cb_priv)) {
2762 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002763 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002764 "Failed to allocate frame buffer");
2765 }
2766 unlock_buffer_pool(pool);
2767
2768 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2769 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2770 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2771 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2772 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2773 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2774 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2775}
2776
Yaowu Xuf883b422016-08-30 14:01:10 -07002777static void read_tile_info(AV1Decoder *const pbi,
2778 struct aom_read_bit_buffer *const rb) {
2779 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002780#if CONFIG_EXT_TILE
2781// Read the tile width/height
2782#if CONFIG_EXT_PARTITION
2783 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002784 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
2785 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002786 } else
2787#endif // CONFIG_EXT_PARTITION
2788 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002789 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
2790 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002791 }
2792
Ryan Lei7386eda2016-12-08 21:08:31 -08002793#if CONFIG_DEBLOCKING_ACROSS_TILES
2794 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
2795#endif // CONFIG_DEBLOCKING_ACROSS_TILES
2796
Yaowu Xuc27fc142016-08-22 16:08:15 -07002797 cm->tile_width <<= cm->mib_size_log2;
2798 cm->tile_height <<= cm->mib_size_log2;
2799
Yaowu Xuf883b422016-08-30 14:01:10 -07002800 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
2801 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002802
2803 // Get the number of tiles
2804 cm->tile_cols = 1;
2805 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
2806
2807 cm->tile_rows = 1;
2808 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
2809
2810 if (cm->tile_cols * cm->tile_rows > 1) {
2811 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07002812 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2813 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002814 }
2815#else
2816 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07002817 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002818
2819 // columns
2820 max_ones = max_log2_tile_cols - min_log2_tile_cols;
2821 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07002822 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002823
2824 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07002825 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002826 "Invalid number of tile columns");
2827
2828 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07002829 cm->log2_tile_rows = aom_rb_read_bit(rb);
2830 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002831
Ryan Lei7386eda2016-12-08 21:08:31 -08002832#if CONFIG_DEBLOCKING_ACROSS_TILES
2833 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
2834#endif // CONFIG_DEBLOCKING_ACROSS_TILES
2835
Yaowu Xuc27fc142016-08-22 16:08:15 -07002836 cm->tile_cols = 1 << cm->log2_tile_cols;
2837 cm->tile_rows = 1 << cm->log2_tile_rows;
2838
2839 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
2840 cm->tile_width >>= cm->log2_tile_cols;
2841 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
2842 cm->tile_height >>= cm->log2_tile_rows;
2843
2844 // round to integer multiples of superblock size
2845 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
2846 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
2847
Thomas Davies4974e522016-11-07 17:44:05 +00002848// tile size magnitude
2849#if !CONFIG_TILE_GROUPS
2850 if (cm->tile_rows > 1 || cm->tile_cols > 1)
2851#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002852 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002853#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00002854
Thomas Davies80188d12016-10-26 16:08:35 -07002855#if CONFIG_TILE_GROUPS
2856 // Store an index to the location of the tile group information
2857 pbi->tg_size_bit_offset = rb->bit_offset;
2858 pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
2859 if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
2860 pbi->tg_start =
2861 aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
2862 pbi->tg_size =
2863 1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
2864 }
2865#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002866}
2867
2868static int mem_get_varsize(const uint8_t *src, const int sz) {
2869 switch (sz) {
2870 case 1: return src[0];
2871 case 2: return mem_get_le16(src);
2872 case 3: return mem_get_le24(src);
2873 case 4: return mem_get_le32(src);
2874 default: assert("Invalid size" && 0); return -1;
2875 }
2876}
2877
2878#if CONFIG_EXT_TILE
2879// Reads the next tile returning its size and adjusting '*data' accordingly
2880// based on 'is_last'.
2881static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07002882 struct aom_internal_error_info *error_info,
2883 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002884 void *decrypt_state,
2885 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
2886 int tile_size_bytes, int col, int row) {
2887 size_t size;
2888
2889 size_t copy_size = 0;
2890 const uint8_t *copy_data = NULL;
2891
2892 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002893 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002894 "Truncated packet or corrupt tile length");
2895 if (decrypt_cb) {
2896 uint8_t be_data[4];
2897 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
2898
2899 // Only read number of bytes in cm->tile_size_bytes.
2900 size = mem_get_varsize(be_data, tile_size_bytes);
2901 } else {
2902 size = mem_get_varsize(*data, tile_size_bytes);
2903 }
2904
2905 // The top bit indicates copy mode
2906 if ((size >> (tile_size_bytes * 8 - 1)) == 1) {
2907 // The remaining bits in the top byte signal the row offset
2908 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
2909
2910 // Currently, only use tiles in same column as reference tiles.
2911 copy_data = tile_buffers[row - offset][col].data;
2912 copy_size = tile_buffers[row - offset][col].size;
2913 size = 0;
2914 }
2915
2916 *data += tile_size_bytes;
2917
2918 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07002919 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002920 "Truncated packet or corrupt tile size");
2921
2922 if (size > 0) {
2923 tile_buffers[row][col].data = *data;
2924 tile_buffers[row][col].size = size;
2925 } else {
2926 tile_buffers[row][col].data = copy_data;
2927 tile_buffers[row][col].size = copy_size;
2928 }
2929
2930 *data += size;
2931
2932 tile_buffers[row][col].raw_data_end = *data;
2933}
2934
2935static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07002936 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002937 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002938 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002939 const int tile_cols = cm->tile_cols;
2940 const int tile_rows = cm->tile_rows;
2941 const int have_tiles = tile_cols * tile_rows > 1;
2942
2943 if (!have_tiles) {
2944 const uint32_t tile_size = data_end - data;
2945 tile_buffers[0][0].data = data;
2946 tile_buffers[0][0].size = tile_size;
2947 tile_buffers[0][0].raw_data_end = NULL;
2948 } else {
2949 // We locate only the tile buffers that are required, which are the ones
2950 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
2951 // need the last (bottom right) tile buffer, as we need to know where the
2952 // end of the compressed frame buffer is for proper superframe decoding.
2953
2954 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
2955 const uint8_t *const data_start = data;
2956
Yaowu Xuf883b422016-08-30 14:01:10 -07002957 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002958 const int single_row = pbi->dec_tile_row >= 0;
2959 const int tile_rows_start = single_row ? dec_tile_row : 0;
2960 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07002961 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002962 const int single_col = pbi->dec_tile_col >= 0;
2963 const int tile_cols_start = single_col ? dec_tile_col : 0;
2964 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
2965
2966 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
2967 const int tile_size_bytes = pbi->tile_size_bytes;
2968
2969 size_t tile_col_size;
2970 int r, c;
2971
2972 // Read tile column sizes for all columns (we need the last tile buffer)
2973 for (c = 0; c < tile_cols; ++c) {
2974 const int is_last = c == tile_cols - 1;
2975 if (!is_last) {
2976 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
2977 data += tile_col_size_bytes;
2978 tile_col_data_end[c] = data + tile_col_size;
2979 } else {
2980 tile_col_size = data_end - data;
2981 tile_col_data_end[c] = data_end;
2982 }
2983 data += tile_col_size;
2984 }
2985
2986 data = data_start;
2987
2988 // Read the required tile sizes.
2989 for (c = tile_cols_start; c < tile_cols_end; ++c) {
2990 const int is_last = c == tile_cols - 1;
2991
2992 if (c > 0) data = tile_col_data_end[c - 1];
2993
2994 if (!is_last) data += tile_col_size_bytes;
2995
2996 // Get the whole of the last column, otherwise stop at the required tile.
2997 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
2998 tile_buffers[r][c].col = c;
2999
3000 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3001 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3002 tile_size_bytes, c, r);
3003 }
3004 }
3005
3006 // If we have not read the last column, then read it to get the last tile.
3007 if (tile_cols_end != tile_cols) {
3008 c = tile_cols - 1;
3009
3010 data = tile_col_data_end[c - 1];
3011
3012 for (r = 0; r < tile_rows; ++r) {
3013 tile_buffers[r][c].col = c;
3014
3015 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3016 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3017 tile_size_bytes, c, r);
3018 }
3019 }
3020 }
3021}
3022#else
3023// Reads the next tile returning its size and adjusting '*data' accordingly
3024// based on 'is_last'.
3025static void get_tile_buffer(const uint8_t *const data_end,
3026 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07003027 struct aom_internal_error_info *error_info,
3028 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003029 void *decrypt_state, TileBufferDec *const buf) {
3030 size_t size;
3031
3032 if (!is_last) {
3033 if (!read_is_valid(*data, 4, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003034 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003035 "Truncated packet or corrupt tile length");
3036
3037 if (decrypt_cb) {
3038 uint8_t be_data[4];
3039 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3040 size = mem_get_varsize(be_data, tile_size_bytes);
3041 } else {
3042 size = mem_get_varsize(*data, tile_size_bytes);
3043 }
3044 *data += tile_size_bytes;
3045
3046 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003047 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003048 "Truncated packet or corrupt tile size");
3049 } else {
3050 size = data_end - *data;
3051 }
3052
3053 buf->data = *data;
3054 buf->size = size;
3055
3056 *data += size;
3057}
3058
3059static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003060 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003061 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003062 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07003063#if CONFIG_TILE_GROUPS
3064 int r, c;
3065 const int tile_cols = cm->tile_cols;
3066 const int tile_rows = cm->tile_rows;
3067 int tc = 0;
3068 int first_tile_in_tg = 0;
3069 int hdr_offset;
3070 struct aom_read_bit_buffer rb_tg_hdr;
3071 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3072 const int num_tiles = tile_rows * tile_cols;
3073 const int num_bits = OD_ILOG(num_tiles) - 1;
3074 const int hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
3075 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
3076
3077 for (r = 0; r < tile_rows; ++r) {
3078 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07003079 TileBufferDec *const buf = &tile_buffers[r][c];
3080 hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
3081
3082 buf->col = c;
3083 if (hdr_offset) {
3084 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
3085 rb_tg_hdr.bit_offset = tg_size_bit_offset;
3086 if (num_tiles) {
3087 pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
3088 pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
3089 }
3090 }
3091 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
3092 data += hdr_offset;
Thomas Davies8fe64a32016-10-04 13:19:31 +01003093 get_tile_buffer(data_end, pbi->tile_size_bytes, 0, &pbi->common.error,
3094 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
Thomas Davies80188d12016-10-26 16:08:35 -07003095 }
3096 }
3097#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003098 int r, c;
3099 const int tile_cols = cm->tile_cols;
3100 const int tile_rows = cm->tile_rows;
3101
3102 for (r = 0; r < tile_rows; ++r) {
3103 for (c = 0; c < tile_cols; ++c) {
3104 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
3105 TileBufferDec *const buf = &tile_buffers[r][c];
3106 buf->col = c;
3107 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
3108 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
3109 }
3110 }
Thomas Davies80188d12016-10-26 16:08:35 -07003111#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003112}
3113#endif // CONFIG_EXT_TILE
3114
Yushin Cho77bba8d2016-11-04 16:36:56 -07003115#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003116static void daala_dec_init(AV1_COMMON *const cm, daala_dec_ctx *daala_dec,
Nathan E. Eggeab083972016-12-28 15:31:46 -05003117 aom_reader *r) {
3118 daala_dec->r = r;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003119 od_adapt_ctx_reset(&daala_dec->state.adapt, 0);
3120
Yushin Cho70669122016-12-08 09:53:14 -10003121 // TODO(yushin) : activity masking info needs be signaled by a bitstream
3122 daala_dec->use_activity_masking = AV1_PVQ_ENABLE_ACTIVITY_MASKING;
3123
3124 if (daala_dec->use_activity_masking)
3125 daala_dec->qm = OD_HVS_QM;
3126 else
3127 daala_dec->qm = OD_FLAT_QM;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003128
3129 od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
3130 daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
Yushin Cho70669122016-12-08 09:53:14 -10003131
3132 if (daala_dec->use_activity_masking) {
3133 int pli;
3134 int use_masking = daala_dec->use_activity_masking;
3135 int segment_id = 0;
3136 int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
3137
3138 for (pli = 0; pli < MAX_MB_PLANE; pli++) {
3139 int i;
3140 int q;
3141
3142 q = qindex;
3143 if (q <= OD_DEFAULT_QMS[use_masking][0][pli].interp_q << OD_COEFF_SHIFT) {
3144 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3145 &OD_DEFAULT_QMS[use_masking][0][pli], NULL);
3146 } else {
3147 i = 0;
3148 while (OD_DEFAULT_QMS[use_masking][i + 1][pli].qm_q4 != NULL &&
3149 q > OD_DEFAULT_QMS[use_masking][i + 1][pli].interp_q
3150 << OD_COEFF_SHIFT) {
3151 i++;
3152 }
3153 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3154 &OD_DEFAULT_QMS[use_masking][i][pli],
3155 &OD_DEFAULT_QMS[use_masking][i + 1][pli]);
3156 }
3157 }
3158 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07003159}
Yushin Cho70669122016-12-08 09:53:14 -10003160#endif // #if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003161
Yaowu Xuf883b422016-08-30 14:01:10 -07003162static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003163 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003164 AV1_COMMON *const cm = &pbi->common;
3165 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003166 const int tile_cols = cm->tile_cols;
3167 const int tile_rows = cm->tile_rows;
3168 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07003169 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003170#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003171 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003172 const int single_row = pbi->dec_tile_row >= 0;
3173 const int tile_rows_start = single_row ? dec_tile_row : 0;
3174 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003175 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003176 const int single_col = pbi->dec_tile_col >= 0;
3177 const int tile_cols_start = single_col ? dec_tile_col : 0;
3178 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3179 const int inv_col_order = pbi->inv_tile_order && !single_col;
3180 const int inv_row_order = pbi->inv_tile_order && !single_row;
3181#else
3182 const int tile_rows_start = 0;
3183 const int tile_rows_end = tile_rows;
3184 const int tile_cols_start = 0;
3185 const int tile_cols_end = tile_cols;
3186 const int inv_col_order = pbi->inv_tile_order;
3187 const int inv_row_order = pbi->inv_tile_order;
3188#endif // CONFIG_EXT_TILE
3189 int tile_row, tile_col;
3190
3191#if CONFIG_ENTROPY
3192 cm->do_subframe_update = n_tiles == 1;
3193#endif // CONFIG_ENTROPY
3194
3195 if (cm->lf.filter_level && !cm->skip_loop_filter &&
3196 pbi->lf_worker.data1 == NULL) {
3197 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07003198 aom_memalign(32, sizeof(LFWorkerData)));
3199 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003200 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003201 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003202 "Loop filter thread creation failed");
3203 }
3204 }
3205
3206 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3207 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3208 // Be sure to sync as we might be resuming after a failed frame decode.
3209 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003210 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
3211 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003212 }
3213
3214 assert(tile_rows <= MAX_TILE_ROWS);
3215 assert(tile_cols <= MAX_TILE_COLS);
3216
3217 get_tile_buffers(pbi, data, data_end, tile_buffers);
3218
3219 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003220 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003221 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003222 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003223 pbi->allocated_tiles = n_tiles;
3224 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003225#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003226 if (pbi->acct_enabled) {
3227 aom_accounting_reset(&pbi->accounting);
3228 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003229#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003230 // Load all tile information into tile_data.
3231 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3232 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3233 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
3234 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
3235
3236 td->cm = cm;
3237 td->xd = pbi->mb;
3238 td->xd.corrupted = 0;
3239 td->xd.counts =
3240 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3241 ? &cm->counts
3242 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003243 av1_zero(td->dqcoeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003244#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003245 av1_zero(td->pvq_ref_coeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003246#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003247 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003248 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
3249 &td->bit_reader, pbi->decrypt_cb, pbi->decrypt_state);
Michael Bebenita6048d052016-08-25 14:40:54 -07003250#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003251 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003252 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003253 } else {
David Barkerd971f402016-10-25 13:52:07 +01003254 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003255 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003256#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003257 av1_init_macroblockd(cm, &td->xd,
3258#if CONFIG_PVQ
3259 td->pvq_ref_coeff,
3260#endif
3261 td->dqcoeff);
3262#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003263 daala_dec_init(cm, &td->xd.daala_dec, &td->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003264#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003265#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003266 td->xd.plane[0].color_index_map = td->color_index_map[0];
3267 td->xd.plane[1].color_index_map = td->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003268#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003269 }
3270 }
3271
3272 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3273 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
3274 int mi_row = 0;
3275 TileInfo tile_info;
3276
Yaowu Xuf883b422016-08-30 14:01:10 -07003277 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003278
3279 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3280 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
3281 TileData *const td = pbi->tile_data + tile_cols * row + col;
Michael Bebenita6048d052016-08-25 14:40:54 -07003282#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003283 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003284 td->bit_reader.accounting->last_tell_frac =
3285 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003286 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003287#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003288
Yaowu Xuf883b422016-08-30 14:01:10 -07003289 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003290
Yaowu Xuf883b422016-08-30 14:01:10 -07003291 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003292
3293 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
3294 mi_row += cm->mib_size) {
3295 int mi_col;
3296
Yaowu Xuf883b422016-08-30 14:01:10 -07003297 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003298
3299 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
3300 mi_col += cm->mib_size) {
Ryan Lei7386eda2016-12-08 21:08:31 -08003301#if CONFIG_DEBLOCKING_ACROSS_TILES
3302 if (av1_disable_loopfilter_on_tile_boundary(cm)) {
3303 av1_update_tile_boundary_info(cm, &tile_info, mi_row, mi_col);
3304 }
3305#endif // CONFIG_DEBLOCKING_ACROSS_TILES
3306
Yaowu Xuc27fc142016-08-22 16:08:15 -07003307 decode_partition(pbi, &td->xd,
3308#if CONFIG_SUPERTX
3309 0,
3310#endif // CONFIG_SUPERTX
3311 mi_row, mi_col, &td->bit_reader, cm->sb_size,
3312 b_width_log2_lookup[cm->sb_size]);
3313 }
3314 pbi->mb.corrupted |= td->xd.corrupted;
3315 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003316 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003317 "Failed to decode tile data");
3318#if CONFIG_ENTROPY
3319 if (cm->do_subframe_update &&
3320 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3321 if ((mi_row + MI_SIZE) %
3322 (MI_SIZE *
Yaowu Xuf883b422016-08-30 14:01:10 -07003323 AOMMAX(cm->mi_rows / MI_SIZE / COEF_PROBS_BUFS, 1)) ==
Yaowu Xuc27fc142016-08-22 16:08:15 -07003324 0 &&
3325 mi_row + MI_SIZE < cm->mi_rows &&
3326 cm->coef_probs_update_idx < COEF_PROBS_BUFS - 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003327 av1_partial_adapt_probs(cm, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003328 ++cm->coef_probs_update_idx;
3329 }
3330 }
3331#endif // CONFIG_ENTROPY
3332 }
3333 }
3334
3335 assert(mi_row > 0);
3336
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003337// when Parallel deblocking is enabled, deblocking should not
3338// be interleaved with decoding. Instead, deblocking should be done
3339// after the entire frame is decoded.
3340#if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003341 // Loopfilter one tile row.
3342 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3343 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003344 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003345 const int lf_end = tile_info.mi_row_end - cm->mib_size;
3346
3347 // Delay the loopfilter if the first tile row is only
3348 // a single superblock high.
3349 if (lf_end <= 0) continue;
3350
3351 // Decoding has completed. Finish up the loop filter in this thread.
3352 if (tile_info.mi_row_end >= cm->mi_rows) continue;
3353
3354 winterface->sync(&pbi->lf_worker);
3355 lf_data->start = lf_start;
3356 lf_data->stop = lf_end;
3357 if (pbi->max_threads > 1) {
3358 winterface->launch(&pbi->lf_worker);
3359 } else {
3360 winterface->execute(&pbi->lf_worker);
3361 }
3362 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003363#endif // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003364
3365 // After loopfiltering, the last 7 row pixels in each superblock row may
3366 // still be changed by the longest loopfilter of the next superblock row.
3367 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003368 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003369 }
3370
3371#if CONFIG_VAR_TX
3372 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003373 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
3374 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003375#else
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003376#if CONFIG_PARALLEL_DEBLOCKING
3377 // Loopfilter all rows in the frame in the frame.
3378 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3379 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3380 winterface->sync(&pbi->lf_worker);
3381 lf_data->start = 0;
3382 lf_data->stop = cm->mi_rows;
3383 winterface->execute(&pbi->lf_worker);
3384 }
3385#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003386 // Loopfilter remaining rows in the frame.
3387 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3388 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3389 winterface->sync(&pbi->lf_worker);
3390 lf_data->start = lf_data->stop;
3391 lf_data->stop = cm->mi_rows;
3392 winterface->execute(&pbi->lf_worker);
3393 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003394#endif // CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003395#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003396 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003397 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003398
3399#if CONFIG_EXT_TILE
3400 if (n_tiles == 1) {
3401#if CONFIG_ANS
3402 return data_end;
3403#else
3404 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07003405 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003406#endif // CONFIG_ANS
3407 } else {
3408 // Return the end of the last tile buffer
3409 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3410 }
3411#else
3412#if CONFIG_ANS
3413 return data_end;
3414#else
3415 {
3416 // Get last tile data.
3417 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003418 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003419 }
3420#endif // CONFIG_ANS
3421#endif // CONFIG_EXT_TILE
3422}
3423
3424static int tile_worker_hook(TileWorkerData *const tile_data,
3425 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003426 AV1Decoder *const pbi = tile_data->pbi;
3427 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003428 int mi_row, mi_col;
3429
3430 if (setjmp(tile_data->error_info.jmp)) {
3431 tile_data->error_info.setjmp = 0;
3432 tile_data->xd.corrupted = 1;
3433 return 0;
3434 }
3435
3436 tile_data->error_info.setjmp = 1;
3437 tile_data->xd.error_info = &tile_data->error_info;
3438
Yaowu Xuf883b422016-08-30 14:01:10 -07003439 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003440
3441 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
3442 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003443 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003444
3445 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3446 mi_col += cm->mib_size) {
3447 decode_partition(pbi, &tile_data->xd,
3448#if CONFIG_SUPERTX
3449 0,
3450#endif
3451 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
3452 b_width_log2_lookup[cm->sb_size]);
3453 }
3454 }
3455 return !tile_data->xd.corrupted;
3456}
3457
3458// sorts in descending order
3459static int compare_tile_buffers(const void *a, const void *b) {
3460 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
3461 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
3462 return (int)(buf2->size - buf1->size);
3463}
3464
Yaowu Xuf883b422016-08-30 14:01:10 -07003465static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003466 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003467 AV1_COMMON *const cm = &pbi->common;
3468 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003469 const int tile_cols = cm->tile_cols;
3470 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003471 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
clang-format67948d32016-09-07 22:40:40 -07003472 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003473#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003474 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003475 const int single_row = pbi->dec_tile_row >= 0;
3476 const int tile_rows_start = single_row ? dec_tile_row : 0;
3477 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003478 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003479 const int single_col = pbi->dec_tile_col >= 0;
3480 const int tile_cols_start = single_col ? dec_tile_col : 0;
3481 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3482#else
3483 const int tile_rows_start = 0;
3484 const int tile_rows_end = tile_rows;
3485 const int tile_cols_start = 0;
3486 const int tile_cols_end = tile_cols;
3487#endif // CONFIG_EXT_TILE
3488 int tile_row, tile_col;
3489 int i;
3490
3491#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3492 int final_worker = -1;
3493#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3494
3495 assert(tile_rows <= MAX_TILE_ROWS);
3496 assert(tile_cols <= MAX_TILE_COLS);
3497
3498 assert(tile_cols * tile_rows > 1);
3499
Yaowu Xuc27fc142016-08-22 16:08:15 -07003500 // TODO(jzern): See if we can remove the restriction of passing in max
3501 // threads to the decoder.
3502 if (pbi->num_tile_workers == 0) {
3503 const int num_threads = pbi->max_threads & ~1;
3504 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07003505 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003506 // Ensure tile data offsets will be properly aligned. This may fail on
3507 // platforms without DECLARE_ALIGNED().
3508 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3509 CHECK_MEM_ERROR(
3510 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003511 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003512 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07003513 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003514 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003515 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003516 ++pbi->num_tile_workers;
3517
3518 winterface->init(worker);
3519 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003520 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003521 "Tile decoder thread creation failed");
3522 }
3523 }
3524 }
3525
3526 // Reset tile decoding hook
3527 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003528 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003529 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003530 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003531 worker->data1 = &pbi->tile_worker_data[i];
3532 worker->data2 = &pbi->tile_worker_info[i];
3533 }
3534
3535 // Initialize thread frame counts.
3536 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3537 for (i = 0; i < num_workers; ++i) {
3538 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003539 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003540 }
3541 }
3542
3543 // Load tile data into tile_buffers
3544 get_tile_buffers(pbi, data, data_end, tile_buffers);
3545
3546 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3547 // Sort the buffers in this tile row based on size in descending order.
3548 qsort(&tile_buffers[tile_row][tile_cols_start],
3549 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
3550 compare_tile_buffers);
3551
3552 // Rearrange the tile buffers in this tile row such that per-tile group
3553 // the largest, and presumably the most difficult tile will be decoded in
3554 // the main thread. This should help minimize the number of instances
3555 // where the main thread is waiting for a worker to complete.
3556 {
3557 int group_start;
3558 for (group_start = tile_cols_start; group_start < tile_cols_end;
3559 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003560 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003561 const TileBufferDec largest = tile_buffers[tile_row][group_start];
3562 memmove(&tile_buffers[tile_row][group_start],
3563 &tile_buffers[tile_row][group_start + 1],
3564 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
3565 tile_buffers[tile_row][group_end - 1] = largest;
3566 }
3567 }
3568
3569 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
3570 // Launch workers for individual columns
3571 for (i = 0; i < num_workers && tile_col < tile_cols_end;
3572 ++i, ++tile_col) {
3573 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07003574 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003575 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
3576 TileInfo *const tile_info = (TileInfo *)worker->data2;
3577
3578 twd->pbi = pbi;
3579 twd->xd = pbi->mb;
3580 twd->xd.corrupted = 0;
3581 twd->xd.counts =
3582 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3583 ? &twd->counts
3584 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003585 av1_zero(twd->dqcoeff);
3586 av1_tile_init(tile_info, cm, tile_row, buf->col);
3587 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003588 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
3589 &twd->bit_reader, pbi->decrypt_cb,
3590 pbi->decrypt_state);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003591 av1_init_macroblockd(cm, &twd->xd,
3592#if CONFIG_PVQ
3593 twd->pvq_ref_coeff,
3594#endif
3595 twd->dqcoeff);
3596#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003597 daala_dec_init(cm, &twd->xd.daala_dec, &twd->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003598#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003599#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003600 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
3601 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003602#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003603
3604 worker->had_error = 0;
3605 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
3606 winterface->execute(worker);
3607 } else {
3608 winterface->launch(worker);
3609 }
3610
3611#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3612 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
3613 final_worker = i;
3614 }
3615#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3616 }
3617
3618 // Sync all workers
3619 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003620 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003621 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07003622 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07003623 // info in cm. Additionally once the threads have been synced and an
3624 // error is detected, there's no point in continuing to decode tiles.
3625 pbi->mb.corrupted |= !winterface->sync(worker);
3626 }
3627 }
3628 }
3629
3630 // Accumulate thread frame counts.
3631 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3632 for (i = 0; i < num_workers; ++i) {
3633 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003634 av1_accumulate_frame_counts(&cm->counts, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003635 }
3636 }
3637
3638#if CONFIG_EXT_TILE
3639 // Return the end of the last tile buffer
3640 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3641#else
3642#if CONFIG_ANS
3643 return data_end;
3644#else
3645 assert(final_worker != -1);
3646 {
3647 TileWorkerData *const twd =
3648 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003649 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003650 }
3651#endif // CONFIG_ANS
3652#endif // CONFIG_EXT_TILE
3653}
3654
3655static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003656 AV1_COMMON *const cm = (AV1_COMMON *)data;
3657 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003658}
3659
Yaowu Xuf883b422016-08-30 14:01:10 -07003660static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3661 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003662 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003663 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
3664#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003665 cm->use_highbitdepth = 1;
3666#endif
3667 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003668 cm->bit_depth = AOM_BITS_8;
3669#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003670 cm->use_highbitdepth = 0;
3671#endif
3672 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003673 cm->color_space = aom_rb_read_literal(rb, 3);
3674 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003675 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07003676 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003677 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003678 cm->subsampling_x = aom_rb_read_bit(rb);
3679 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003680 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003681 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003682 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07003683 if (aom_rb_read_bit(rb))
3684 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003685 "Reserved bit set");
3686 } else {
3687 cm->subsampling_y = cm->subsampling_x = 1;
3688 }
3689 } else {
3690 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
3691 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
3692 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
3693 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003694 if (aom_rb_read_bit(rb))
3695 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003696 "Reserved bit set");
3697 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003698 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003699 "4:4:4 color not supported in profile 0 or 2");
3700 }
3701 }
3702}
3703
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003704#if CONFIG_REFERENCE_BUFFER
3705void read_sequence_header(SequenceHeader *seq_params) {
3706 /* Placeholder for actually reading from the bitstream */
3707 seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
3708 seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
3709 seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
3710}
3711#endif
3712
Yaowu Xuf883b422016-08-30 14:01:10 -07003713static size_t read_uncompressed_header(AV1Decoder *pbi,
3714 struct aom_read_bit_buffer *rb) {
3715 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003716 MACROBLOCKD *const xd = &pbi->mb;
3717 BufferPool *const pool = cm->buffer_pool;
3718 RefCntBuffer *const frame_bufs = pool->frame_bufs;
3719 int i, mask, ref_index = 0;
3720 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003721
3722#if CONFIG_REFERENCE_BUFFER
3723 /* TODO: Move outside frame loop or inside key-frame branch */
3724 read_sequence_header(&pbi->seq_params);
3725#endif
3726
Yaowu Xuc27fc142016-08-22 16:08:15 -07003727 cm->last_frame_type = cm->frame_type;
3728 cm->last_intra_only = cm->intra_only;
3729
3730#if CONFIG_EXT_REFS
3731 // NOTE: By default all coded frames to be used as a reference
3732 cm->is_reference_frame = 1;
3733#endif // CONFIG_EXT_REFS
3734
Yaowu Xuf883b422016-08-30 14:01:10 -07003735 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
3736 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003737 "Invalid frame marker");
3738
Yaowu Xuf883b422016-08-30 14:01:10 -07003739 cm->profile = av1_read_profile(rb);
3740#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003741 if (cm->profile >= MAX_PROFILES)
Yaowu Xuf883b422016-08-30 14:01:10 -07003742 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003743 "Unsupported bitstream profile");
3744#else
3745 if (cm->profile >= PROFILE_2)
Yaowu Xuf883b422016-08-30 14:01:10 -07003746 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003747 "Unsupported bitstream profile");
3748#endif
3749
Yaowu Xuf883b422016-08-30 14:01:10 -07003750 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003751
3752 if (cm->show_existing_frame) {
Yaowu Xu415ba932016-12-27 11:17:32 -08003753 // Show an existing frame directly.
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003754 const int existing_frame_idx = aom_rb_read_literal(rb, 3);
3755 const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
Yaowu Xu415ba932016-12-27 11:17:32 -08003756#if CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003757 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003758 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3759 int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003760 /* Compare display_frame_id with ref_frame_id and check valid for
3761 * referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003762 if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
3763 cm->valid_for_referencing[existing_frame_idx] == 0)
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003764 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3765 "Reference buffer frame ID mismatch");
3766 }
3767#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003768 lock_buffer_pool(pool);
3769 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
3770 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07003771 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003772 "Buffer %d does not contain a decoded frame",
3773 frame_to_show);
3774 }
3775 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
3776 unlock_buffer_pool(pool);
3777
3778 cm->lf.filter_level = 0;
3779 cm->show_frame = 1;
3780 pbi->refresh_frame_flags = 0;
3781
3782 if (cm->frame_parallel_decode) {
3783 for (i = 0; i < REF_FRAMES; ++i)
3784 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
3785 }
3786
3787 return 0;
3788 }
3789
Yaowu Xuf883b422016-08-30 14:01:10 -07003790 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
3791 cm->show_frame = aom_rb_read_bit(rb);
3792 cm->error_resilient_mode = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003793#if CONFIG_REFERENCE_BUFFER
3794 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003795 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3796 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
3797 int prev_frame_id = 0;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003798 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003799 prev_frame_id = cm->current_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003800 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003801 cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003802
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003803 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003804 int diff_frame_id;
3805 if (cm->current_frame_id > prev_frame_id) {
3806 diff_frame_id = cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003807 } else {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003808 diff_frame_id =
3809 (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003810 }
3811 /* Check current_frame_id for conformance */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003812 if (prev_frame_id == cm->current_frame_id ||
3813 diff_frame_id >= (1 << (frame_id_length - 1))) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003814 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3815 "Invalid value of current_frame_id");
3816 }
3817 }
3818 /* Check if some frames need to be marked as not valid for referencing */
3819 for (i = 0; i < REF_FRAMES; i++) {
3820 if (cm->frame_type == KEY_FRAME) {
3821 cm->valid_for_referencing[i] = 0;
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003822 } else if (cm->current_frame_id - (1 << diff_len) > 0) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003823 if (cm->ref_frame_id[i] > cm->current_frame_id ||
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003824 cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003825 cm->valid_for_referencing[i] = 0;
3826 } else {
3827 if (cm->ref_frame_id[i] > cm->current_frame_id &&
3828 cm->ref_frame_id[i] <
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003829 (1 << frame_id_length) + cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003830 cm->valid_for_referencing[i] = 0;
3831 }
3832 }
3833 }
3834#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003835 if (cm->frame_type == KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003836 if (!av1_read_sync_code(rb))
3837 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003838 "Invalid frame sync code");
3839
3840 read_bitdepth_colorspace_sampling(cm, rb);
3841 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
3842
3843 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3844 cm->frame_refs[i].idx = INVALID_IDX;
3845 cm->frame_refs[i].buf = NULL;
3846 }
3847
3848 setup_frame_size(cm, rb);
3849 if (pbi->need_resync) {
3850 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
3851 pbi->need_resync = 0;
3852 }
Urvang Joshib100db72016-10-12 16:28:56 -07003853#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07003854 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003855#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003856 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003857 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003858#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07003859 if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003860#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003861 if (cm->error_resilient_mode) {
3862 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
3863 } else {
3864 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003865 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003866 ? RESET_FRAME_CONTEXT_ALL
3867 : RESET_FRAME_CONTEXT_CURRENT;
3868 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003869 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003870 ? RESET_FRAME_CONTEXT_CURRENT
3871 : RESET_FRAME_CONTEXT_NONE;
3872 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07003873 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003874 ? RESET_FRAME_CONTEXT_ALL
3875 : RESET_FRAME_CONTEXT_CURRENT;
3876 }
3877 }
3878
3879 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003880 if (!av1_read_sync_code(rb))
3881 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003882 "Invalid frame sync code");
3883
3884 read_bitdepth_colorspace_sampling(cm, rb);
3885
Yaowu Xuf883b422016-08-30 14:01:10 -07003886 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003887 setup_frame_size(cm, rb);
3888 if (pbi->need_resync) {
3889 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
3890 pbi->need_resync = 0;
3891 }
3892 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Yaowu Xuf883b422016-08-30 14:01:10 -07003893 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003894
3895#if CONFIG_EXT_REFS
3896 if (!pbi->refresh_frame_flags) {
3897 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
3898 // will not be used as a reference
3899 cm->is_reference_frame = 0;
3900 }
3901#endif // CONFIG_EXT_REFS
3902
3903 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003904 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003905 const int idx = cm->ref_frame_map[ref];
3906 RefBuffer *const ref_frame = &cm->frame_refs[i];
3907 ref_frame->idx = idx;
3908 ref_frame->buf = &frame_bufs[idx].buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07003909 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003910#if CONFIG_REFERENCE_BUFFER
3911 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003912 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3913 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
3914 int delta_frame_id_minus1 = aom_rb_read_literal(rb, diff_len);
3915 int ref_frame_id =
3916 ((cm->current_frame_id - (delta_frame_id_minus1 + 1) +
3917 (1 << frame_id_length)) %
3918 (1 << frame_id_length));
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003919 /* Compare values derived from delta_frame_id_minus1 and
3920 * refresh_frame_flags. Also, check valid for referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003921 if (ref_frame_id != cm->ref_frame_id[ref] ||
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003922 cm->valid_for_referencing[ref] == 0)
3923 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3924 "Reference buffer frame ID mismatch");
3925 }
3926#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003927 }
3928
Arild Fuldseth842e9b02016-09-02 13:00:05 +02003929#if CONFIG_FRAME_SIZE
3930 if (cm->error_resilient_mode == 0) {
3931 setup_frame_size_with_refs(cm, rb);
3932 } else {
3933 setup_frame_size(cm, rb);
3934 }
3935#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003936 setup_frame_size_with_refs(cm, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02003937#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003938
Yaowu Xuf883b422016-08-30 14:01:10 -07003939 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Angie Chiang5678ad92016-11-21 09:38:40 -08003940 cm->interp_filter = read_frame_interp_filter(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003941
3942 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3943 RefBuffer *const ref_buf = &cm->frame_refs[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07003944#if CONFIG_AOM_HIGHBITDEPTH
3945 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003946 &ref_buf->sf, ref_buf->buf->y_crop_width,
3947 ref_buf->buf->y_crop_height, cm->width, cm->height,
3948 cm->use_highbitdepth);
3949#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003950 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003951 &ref_buf->sf, ref_buf->buf->y_crop_width,
3952 ref_buf->buf->y_crop_height, cm->width, cm->height);
3953#endif
3954 }
3955 }
3956 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003957
3958#if CONFIG_REFERENCE_BUFFER
3959 if (pbi->seq_params.frame_id_numbers_present_flag) {
3960 /* If bitmask is set, update reference frame id values and
3961 mark frames as valid for reference */
3962 int refresh_frame_flags =
3963 cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
3964 for (i = 0; i < REF_FRAMES; i++) {
3965 if ((refresh_frame_flags >> i) & 1) {
3966 cm->ref_frame_id[i] = cm->current_frame_id;
3967 cm->valid_for_referencing[i] = 1;
3968 }
3969 }
3970 }
3971#endif
3972
Yaowu Xuf883b422016-08-30 14:01:10 -07003973#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003974 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
3975#endif
3976 get_frame_new_buffer(cm)->color_space = cm->color_space;
3977 get_frame_new_buffer(cm)->color_range = cm->color_range;
3978 get_frame_new_buffer(cm)->render_width = cm->render_width;
3979 get_frame_new_buffer(cm)->render_height = cm->render_height;
3980
3981 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003982 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003983 "Keyframe / intra-only frame required to reset decoder"
3984 " state");
3985 }
3986
3987 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003988 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003989 ? REFRESH_FRAME_CONTEXT_FORWARD
3990 : REFRESH_FRAME_CONTEXT_BACKWARD;
3991 } else {
3992 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
3993 }
3994
Yaowu Xuf883b422016-08-30 14:01:10 -07003995 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07003996 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07003997 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003998
3999 // Generate next_ref_frame_map.
4000 lock_buffer_pool(pool);
4001 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
4002 if (mask & 1) {
4003 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
4004 ++frame_bufs[cm->new_fb_idx].ref_count;
4005 } else {
4006 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4007 }
4008 // Current thread holds the reference frame.
4009 if (cm->ref_frame_map[ref_index] >= 0)
4010 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4011 ++ref_index;
4012 }
4013
4014 for (; ref_index < REF_FRAMES; ++ref_index) {
4015 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4016
4017 // Current thread holds the reference frame.
4018 if (cm->ref_frame_map[ref_index] >= 0)
4019 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4020 }
4021 unlock_buffer_pool(pool);
4022 pbi->hold_ref_buf = 1;
4023
4024 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07004025 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004026
4027#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07004028 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004029#else
4030 set_sb_size(cm, BLOCK_64X64);
4031#endif // CONFIG_EXT_PARTITION
4032
4033 setup_loopfilter(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004034#if CONFIG_DERING
4035 setup_dering(cm, rb);
4036#endif
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004037#if CONFIG_CLPF
4038 setup_clpf(pbi, rb);
4039#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004040#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee874d36d2016-12-14 16:53:17 -08004041 av1_alloc_restoration_buffers(cm);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004042 decode_restoration_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004043#endif // CONFIG_LOOP_RESTORATION
4044 setup_quantization(cm, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004045#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004046 xd->bd = (int)cm->bit_depth;
4047#endif
4048
4049#if CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004050 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004051 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4052 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4053 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4054 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
4055 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4056 }
4057#endif // CONFIG_ENTROPY
4058
4059 setup_segmentation(cm, rb);
4060
Arild Fuldseth07441162016-08-15 15:07:52 +02004061#if CONFIG_DELTA_Q
4062 {
4063 struct segmentation *const seg = &cm->seg;
4064 int segment_quantizer_active = 0;
4065 for (i = 0; i < MAX_SEGMENTS; i++) {
4066 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
4067 segment_quantizer_active = 1;
4068 }
4069 }
4070
Thomas Daviesf6936102016-09-05 16:51:31 +01004071 cm->delta_q_res = 1;
Arild Fuldseth07441162016-08-15 15:07:52 +02004072 if (segment_quantizer_active == 0) {
4073 cm->delta_q_present_flag = aom_rb_read_bit(rb);
4074 } else {
4075 cm->delta_q_present_flag = 0;
4076 }
4077 if (cm->delta_q_present_flag) {
4078 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01004079 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Arild Fuldseth07441162016-08-15 15:07:52 +02004080 }
4081 }
4082#endif
4083
Urvang Joshi454280d2016-10-14 16:51:44 -07004084 for (i = 0; i < MAX_SEGMENTS; ++i) {
4085 const int qindex = cm->seg.enabled
4086 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
4087 : cm->base_qindex;
4088 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
4089 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4090 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004091 }
4092
4093 setup_segmentation_dequant(cm);
4094 cm->tx_mode =
4095 (!cm->seg.enabled && xd->lossless[0]) ? ONLY_4X4 : read_tx_mode(rb);
4096 cm->reference_mode = read_frame_reference_mode(cm, rb);
4097
4098 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004099 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004100
4101 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07004102 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004103 "Invalid header size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004104 return sz;
4105}
4106
4107#if CONFIG_EXT_TX
Thomas9ac55082016-09-23 18:04:17 +01004108#if !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuf883b422016-08-30 14:01:10 -07004109static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004110 int i, j, k;
4111 int s;
4112 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004113 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004114 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4115 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
4116 for (j = 0; j < num_ext_tx_set_inter[s] - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004117 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004118 }
4119 }
4120 }
4121
4122 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004123 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004124 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4125 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
4126 for (j = 0; j < INTRA_MODES; ++j)
4127 for (k = 0; k < num_ext_tx_set_intra[s] - 1; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004128 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
4129 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004130 }
4131 }
4132 }
4133}
Thomas9ac55082016-09-23 18:04:17 +01004134#endif // !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004135#else
4136
Yaowu Xuc27fc142016-08-22 16:08:15 -07004137#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004138#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07004139static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004140 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -07004141 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004142 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
Jingning Hanfeb517c2016-12-21 16:02:07 -08004143 for (j = TX_8X8; j < TX_SIZES; ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004144 av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004145 }
4146 }
4147 }
4148}
4149#endif // CONFIG_SUPERTX
4150
4151#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +00004152static void read_global_motion_params(WarpedMotionParams *params,
Yaowu Xuf883b422016-08-30 14:01:10 -07004153 aom_prob *probs, aom_reader *r) {
David Barkercf3d0b02016-11-10 10:14:49 +00004154 TransformationType type =
Michael Bebenita6048d052016-08-25 14:40:54 -07004155 aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004156 set_default_gmparams(params);
David Barkercf3d0b02016-11-10 10:14:49 +00004157 params->wmtype = type;
4158 switch (type) {
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004159 case HOMOGRAPHY:
4160 params->wmmat[6] = aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004161 GM_ROW3HOMO_DECODE_FACTOR;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004162 params->wmmat[7] = aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004163 GM_ROW3HOMO_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004164 case AFFINE:
4165 case ROTZOOM:
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004166 params->wmmat[2] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004167 GM_ALPHA_DECODE_FACTOR +
David Barkercf3d0b02016-11-10 10:14:49 +00004168 (1 << WARPEDMODEL_PREC_BITS);
4169 params->wmmat[3] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4170 GM_ALPHA_DECODE_FACTOR;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004171 if (type == AFFINE || type == HOMOGRAPHY) {
4172 params->wmmat[4] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004173 GM_ALPHA_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004174 params->wmmat[5] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4175 GM_ALPHA_DECODE_FACTOR +
4176 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004177 } else {
David Barkercf3d0b02016-11-10 10:14:49 +00004178 params->wmmat[4] = -params->wmmat[3];
4179 params->wmmat[5] = params->wmmat[2];
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004180 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004181 // fallthrough intended
David Barkercf3d0b02016-11-10 10:14:49 +00004182 case TRANSLATION:
4183 params->wmmat[0] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4184 GM_TRANS_DECODE_FACTOR;
4185 params->wmmat[1] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4186 GM_TRANS_DECODE_FACTOR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004187 break;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004188 case IDENTITY: break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004189 default: assert(0);
4190 }
4191}
4192
Yaowu Xuf883b422016-08-30 14:01:10 -07004193static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004194 int frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004195 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
4196 read_global_motion_params(&cm->global_motion[frame],
4197 cm->fc->global_motion_types_prob, r);
Sarah Parkere5299862016-08-16 14:57:37 -07004198 /*
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004199 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4200 frame, cm->current_video_frame, cm->show_frame,
David Barkercf3d0b02016-11-10 10:14:49 +00004201 cm->global_motion[frame].wmmat[0],
4202 cm->global_motion[frame].wmmat[1],
4203 cm->global_motion[frame].wmmat[2],
4204 cm->global_motion[frame].wmmat[3]);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004205 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004206 }
4207}
4208#endif // CONFIG_GLOBAL_MOTION
4209
Yaowu Xuf883b422016-08-30 14:01:10 -07004210static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004211 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004212 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004213#if CONFIG_SUPERTX
4214 MACROBLOCKD *const xd = &pbi->mb;
4215#endif
4216 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07004217 aom_reader r;
Yaowu Xu8af861b2016-11-01 12:12:11 -07004218 int k, i;
Yaowu Xud0af64f2016-11-17 12:50:47 -08004219#if !CONFIG_EC_ADAPT || CONFIG_EXT_INTRA
Yaowu Xu8af861b2016-11-01 12:12:11 -07004220 int j;
4221#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004222
Alex Converse2cdf0d82016-12-13 13:53:09 -08004223 if (aom_reader_init(&r, data, partition_size,
4224#if CONFIG_ANS && ANS_MAX_SYMBOLS
4225 ANS_MAX_SYMBOLS,
4226#endif
4227 pbi->decrypt_cb, pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07004228 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004229 "Failed to allocate bool decoder 0");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004230
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004231#if CONFIG_LOOP_RESTORATION
4232 decode_restoration(cm, &r);
4233#endif
4234
Yaowu Xuefc75352016-10-31 09:46:42 -07004235 if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004236
Yushin Cho77bba8d2016-11-04 16:36:56 -07004237#if !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004238 read_coef_probs(fc, cm->tx_mode, &r);
4239
4240#if CONFIG_VAR_TX
4241 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004242 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
Yushin Cho77bba8d2016-11-04 16:36:56 -07004243#endif // CONFIG_VAR_TX
4244#endif // !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004245 for (k = 0; k < SKIP_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004246 av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004247
Thomas Daviesf6936102016-09-05 16:51:31 +01004248#if CONFIG_DELTA_Q
4249 for (k = 0; k < DELTA_Q_CONTEXTS; ++k)
4250 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
4251#endif
4252
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004253#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004254 if (cm->seg.enabled && cm->seg.update_map) {
4255 if (cm->seg.temporal_update) {
4256 for (k = 0; k < PREDICTION_PROBS; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004257 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004258 }
4259 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004260 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004261 }
4262
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004263 for (j = 0; j < INTRA_MODES; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004264 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004265 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004266 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004267
4268#if CONFIG_EXT_PARTITION_TYPES
4269 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004270 av1_diff_update_prob(&r, &fc->partition_prob[0][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004271 for (j = 1; j < PARTITION_CONTEXTS; ++j)
4272 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004273 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004274#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004275 for (j = 0; j < PARTITION_CONTEXTS; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004276 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004277 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004278#endif // CONFIG_EXT_PARTITION_TYPES
Thomas9ac55082016-09-23 18:04:17 +01004279#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004280#if CONFIG_EXT_INTRA
hui sueda3d762016-12-06 16:58:23 -08004281#if CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004282 for (i = 0; i < INTRA_FILTERS + 1; ++i)
4283 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004284 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
hui sueda3d762016-12-06 16:58:23 -08004285#endif // CONFIG_INTRA_INTERP
Thomas Davies6519beb2016-10-19 14:46:07 +01004286#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004287
4288 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004289 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Nathan E. Egge10ba2be2016-11-16 09:44:26 -05004290#if CONFIG_EC_MULTISYMBOL
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004291 av1_copy(cm->kf_y_cdf, av1_kf_y_mode_cdf);
4292#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004293#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004294 for (k = 0; k < INTRA_MODES; k++)
Thomas Davies6519beb2016-10-19 14:46:07 +01004295 for (j = 0; j < INTRA_MODES; j++)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004296 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004297 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004298#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004299 } else {
4300#if !CONFIG_REF_MV
4301 nmv_context *const nmvc = &fc->nmvc;
4302#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004303 read_inter_mode_probs(fc, &r);
4304
4305#if CONFIG_EXT_INTER
4306 read_inter_compound_mode_probs(fc, &r);
4307 if (cm->reference_mode != COMPOUND_REFERENCE) {
4308 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4309 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004310 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004311 }
4312 }
4313 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4314 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004315 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004316 }
4317 for (i = 0; i < BLOCK_SIZES; i++) {
4318 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004319 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004320 }
4321 }
4322 }
4323 if (cm->reference_mode != SINGLE_REFERENCE) {
4324 for (i = 0; i < BLOCK_SIZES; i++) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08004325 for (j = 0; j < COMPOUND_TYPES - 1; j++) {
4326 av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004327 }
4328 }
4329 }
4330#endif // CONFIG_EXT_INTER
4331
Yue Chencb60b182016-10-13 15:18:22 -07004332#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004333 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
Yue Chencb60b182016-10-13 15:18:22 -07004334 for (j = 0; j < MOTION_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004335 av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004336 }
Yue Chencb60b182016-10-13 15:18:22 -07004337#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004338
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004339#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004340 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004341#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004342
4343 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004344 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004345
4346 if (cm->reference_mode != SINGLE_REFERENCE)
4347 setup_compound_reference_mode(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004348 read_frame_reference_mode_probs(cm, &r);
4349
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004350#if !CONFIG_EC_ADAPT
Nathan E. Egge5710c722016-09-08 10:01:16 -04004351 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004352 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004353 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
Nathan E. Egge5710c722016-09-08 10:01:16 -04004354 }
Thomas9ac55082016-09-23 18:04:17 +01004355#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004356
4357#if CONFIG_REF_MV
4358 for (i = 0; i < NMV_CONTEXTS; ++i)
4359 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
4360#else
4361 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
4362#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004363#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004364 read_ext_tx_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004365#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004366#if CONFIG_SUPERTX
4367 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
4368#endif
4369#if CONFIG_GLOBAL_MOTION
4370 read_global_motion(cm, &r);
Thomas Davies6519beb2016-10-19 14:46:07 +01004371#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004372 }
Thomas Davies6519beb2016-10-19 14:46:07 +01004373#if CONFIG_EC_MULTISYMBOL
4374 av1_coef_pareto_cdfs(fc);
David Barker599dfd02016-11-10 13:20:12 +00004375#if CONFIG_REF_MV
4376 for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
4377#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004378 av1_set_mv_cdfs(&fc->nmvc);
David Barker599dfd02016-11-10 13:20:12 +00004379#endif
Nathan E. Egge31296062016-11-16 09:44:26 -05004380#if CONFIG_EC_MULTISYMBOL
Thomas Davies6519beb2016-10-19 14:46:07 +01004381 av1_set_mode_cdfs(cm);
4382#endif
4383#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004384
Yaowu Xuf883b422016-08-30 14:01:10 -07004385 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004386}
4387
4388#ifdef NDEBUG
4389#define debug_check_frame_counts(cm) (void)0
4390#else // !NDEBUG
4391// Counts should only be incremented when frame_parallel_decoding_mode and
4392// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07004393static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004394 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07004395 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004396 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
4397 cm->error_resilient_mode);
4398 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
4399 sizeof(cm->counts.y_mode)));
4400 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
4401 sizeof(cm->counts.uv_mode)));
4402 assert(!memcmp(cm->counts.partition, zero_counts.partition,
4403 sizeof(cm->counts.partition)));
4404 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
4405 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
4406 sizeof(cm->counts.eob_branch)));
4407 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
4408 sizeof(cm->counts.switchable_interp)));
4409 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
4410 sizeof(cm->counts.inter_mode)));
4411#if CONFIG_EXT_INTER
4412 assert(!memcmp(cm->counts.inter_compound_mode,
4413 zero_counts.inter_compound_mode,
4414 sizeof(cm->counts.inter_compound_mode)));
4415 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
4416 sizeof(cm->counts.interintra)));
4417 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
4418 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08004419 assert(!memcmp(cm->counts.compound_interinter,
4420 zero_counts.compound_interinter,
4421 sizeof(cm->counts.compound_interinter)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004422#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07004423#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
4424 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
4425 sizeof(cm->counts.motion_mode)));
4426#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004427 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
4428 sizeof(cm->counts.intra_inter)));
4429 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
4430 sizeof(cm->counts.comp_inter)));
4431 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
4432 sizeof(cm->counts.single_ref)));
4433 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
4434 sizeof(cm->counts.comp_ref)));
4435#if CONFIG_EXT_REFS
4436 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
4437 sizeof(cm->counts.comp_bwdref)));
4438#endif // CONFIG_EXT_REFS
4439 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
4440 sizeof(cm->counts.tx_size)));
4441 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
4442#if CONFIG_REF_MV
4443 assert(
4444 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
4445 assert(
4446 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
4447#else
4448 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
4449#endif
4450 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
4451 sizeof(cm->counts.inter_ext_tx)));
4452 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
4453 sizeof(cm->counts.intra_ext_tx)));
4454}
4455#endif // NDEBUG
4456
Yaowu Xuf883b422016-08-30 14:01:10 -07004457static struct aom_read_bit_buffer *init_read_bit_buffer(
4458 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
4459 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004460 rb->bit_offset = 0;
4461 rb->error_handler = error_handler;
4462 rb->error_handler_data = &pbi->common;
4463 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004464 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004465 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
4466 rb->bit_buffer = clear_data;
4467 rb->bit_buffer_end = clear_data + n;
4468 } else {
4469 rb->bit_buffer = data;
4470 rb->bit_buffer_end = data_end;
4471 }
4472 return rb;
4473}
4474
4475//------------------------------------------------------------------------------
4476
Yaowu Xuf883b422016-08-30 14:01:10 -07004477int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
4478 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
4479 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
4480 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004481}
4482
Yaowu Xuf883b422016-08-30 14:01:10 -07004483void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
4484 int *height) {
4485 *width = aom_rb_read_literal(rb, 16) + 1;
4486 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004487}
4488
Yaowu Xuf883b422016-08-30 14:01:10 -07004489BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
4490 int profile = aom_rb_read_bit(rb);
4491 profile |= aom_rb_read_bit(rb) << 1;
4492 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004493 return (BITSTREAM_PROFILE)profile;
4494}
4495
Yaowu Xuf883b422016-08-30 14:01:10 -07004496void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
4497 const uint8_t *data_end, const uint8_t **p_data_end) {
4498 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004499 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07004500 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004501 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004502 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004503 size_t first_partition_size;
4504 YV12_BUFFER_CONFIG *new_fb;
4505
4506#if CONFIG_BITSTREAM_DEBUG
4507 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
4508#endif
4509
4510 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004511 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Thomas Davies72712e62016-11-09 12:17:51 +00004512#if CONFIG_TILE_GROUPS
4513 pbi->first_partition_size = first_partition_size;
4514 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
4515#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004516 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004517 xd->cur_buf = new_fb;
4518#if CONFIG_GLOBAL_MOTION
4519 xd->global_motion = cm->global_motion;
4520#endif // CONFIG_GLOBAL_MOTION
4521
4522 if (!first_partition_size) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004523 // showing a frame directly
4524 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004525 return;
4526 }
4527
Yaowu Xuf883b422016-08-30 14:01:10 -07004528 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004529 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07004530 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004531 "Truncated packet or corrupt header length");
4532
Jingning Han24e0a182016-11-20 22:34:12 -08004533#if CONFIG_REF_MV
Dengca8d24d2016-10-17 14:06:35 +08004534 cm->setup_mi(cm);
4535#endif
4536
Yaowu Xuc27fc142016-08-22 16:08:15 -07004537 cm->use_prev_frame_mvs =
4538 !cm->error_resilient_mode && cm->width == cm->last_width &&
4539 cm->height == cm->last_height && !cm->last_intra_only &&
4540 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
4541#if CONFIG_EXT_REFS
4542 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
4543 // show_exisiting_frame=1, nor can it take a frame not used as
4544 // a reference, it is probable that by the time it is being
4545 // referred to, the frame buffer it originally points to may
4546 // already get expired and have been reassigned to the current
4547 // newly coded frame. Hence, we need to check whether this is
4548 // the case, and if yes, we have 2 choices:
4549 // (1) Simply disable the use of previous frame mvs; or
4550 // (2) Have cm->prev_frame point to one reference frame buffer,
4551 // e.g. LAST_FRAME.
4552 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
4553 // Reassign the LAST_FRAME buffer to cm->prev_frame.
4554 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4555 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4556 }
4557#endif // CONFIG_EXT_REFS
4558
Yaowu Xuf883b422016-08-30 14:01:10 -07004559 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004560
4561 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
4562 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07004563 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004564 "Uninitialized entropy context.");
4565
Yaowu Xuf883b422016-08-30 14:01:10 -07004566 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004567
4568 xd->corrupted = 0;
4569 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
4570 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07004571 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004572 "Decode failed. Frame data header is corrupted.");
4573
4574 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004575 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004576 }
4577
4578 // If encoded in frame parallel mode, frame context is ready after decoding
4579 // the frame header.
4580 if (cm->frame_parallel_decode &&
4581 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004582 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004583 FrameWorkerData *const frame_worker_data = worker->data1;
4584 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
4585 context_updated = 1;
4586 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4587 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004588 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004589 pbi->cur_buf->row = -1;
4590 pbi->cur_buf->col = -1;
4591 frame_worker_data->frame_context_ready = 1;
4592 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07004593 av1_frameworker_signal_stats(worker);
4594 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004595 }
4596
4597#if CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004598 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004599 cm->coef_probs_update_idx = 0;
4600#endif // CONFIG_ENTROPY
4601
4602 if (pbi->max_threads > 1
4603#if CONFIG_EXT_TILE
4604 && pbi->dec_tile_col < 0 // Decoding all columns
4605#endif // CONFIG_EXT_TILE
4606 && cm->tile_cols > 1) {
4607 // Multi-threaded tile decoder
4608 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
4609 if (!xd->corrupted) {
4610 if (!cm->skip_loop_filter) {
4611 // If multiple threads are used to decode tiles, then we use those
4612 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07004613 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
4614 0, 0, pbi->tile_workers, pbi->num_tile_workers,
4615 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004616 }
4617 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004618 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004619 "Decode failed. Frame data is corrupted.");
4620 }
4621 } else {
4622 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
4623 }
4624#if CONFIG_LOOP_RESTORATION
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08004625 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4626 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4627 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4628 av1_loop_restoration_frame(new_fb, cm, cm->rst_info, 7, 0, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004629 }
4630#endif // CONFIG_LOOP_RESTORATION
4631
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004632#if CONFIG_DERING
4633 if (cm->dering_level && !cm->skip_loop_filter) {
4634 av1_dering_frame(&pbi->cur_buf->buf, cm, &pbi->mb, cm->dering_level);
4635 }
4636#endif // CONFIG_DERING
4637
Thomas Daedef56859f2016-04-19 16:57:24 -07004638#if CONFIG_CLPF
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004639 if (!cm->skip_loop_filter) {
4640 const YV12_BUFFER_CONFIG *const frame = &pbi->cur_buf->buf;
4641 if (cm->clpf_strength_y) {
Yaowu Xud71be782016-10-14 08:47:03 -07004642 av1_clpf_frame(frame, NULL, cm, cm->clpf_size != CLPF_NOSIZE,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004643 cm->clpf_strength_y + (cm->clpf_strength_y == 3),
Yaowu Xud71be782016-10-14 08:47:03 -07004644 4 + cm->clpf_size, AOM_PLANE_Y, clpf_bit);
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004645 }
4646 if (cm->clpf_strength_u) {
Yaowu Xud71be782016-10-14 08:47:03 -07004647 av1_clpf_frame(frame, NULL, cm, 0, // No block signals for chroma
4648 cm->clpf_strength_u + (cm->clpf_strength_u == 3), 4,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004649 AOM_PLANE_U, NULL);
4650 }
4651 if (cm->clpf_strength_v) {
Yaowu Xud71be782016-10-14 08:47:03 -07004652 av1_clpf_frame(frame, NULL, cm, 0, // No block signals for chroma
4653 cm->clpf_strength_v + (cm->clpf_strength_v == 3), 4,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004654 AOM_PLANE_V, NULL);
4655 }
Steinar Midtskogend06588a2016-05-06 13:48:20 +02004656 }
4657 if (cm->clpf_blocks) aom_free(cm->clpf_blocks);
Thomas Daedef56859f2016-04-19 16:57:24 -07004658#endif
Thomas Daedef56859f2016-04-19 16:57:24 -07004659
Yaowu Xuc27fc142016-08-22 16:08:15 -07004660 if (!xd->corrupted) {
4661 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
4662#if CONFIG_ENTROPY
4663 cm->partial_prob_update = 0;
4664#endif // CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004665 av1_adapt_coef_probs(cm);
4666 av1_adapt_intra_frame_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004667
4668 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004669 av1_adapt_inter_frame_probs(cm);
4670 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004671 }
4672 } else {
4673 debug_check_frame_counts(cm);
4674 }
4675 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004676 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004677 "Decode failed. Frame data is corrupted.");
4678 }
4679
4680 // Non frame parallel update frame context here.
4681 if (!cm->error_resilient_mode && !context_updated)
4682 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4683}