blob: a7015c16bfb7fa36663a7c90246eb1459896c1df [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
409 // decode ac/dc coded flag. bit0: DC coded, bit1 : AC coded
410 // NOTE : we don't use 5 symbols for luma here in aom codebase,
411 // since block partition is taken care of by aom.
412 // So, only AC/DC skip info is coded
Nathan E. Eggef25bae42016-12-29 10:15:06 -0500413 ac_dc_coded = aom_decode_cdf_adapt(
414 xd->daala_dec.r,
Yushin Cho77bba8d2016-11-04 16:36:56 -0700415 xd->daala_dec.state.adapt.skip_cdf[2 * tx_size + (plane != 0)], 4,
416 xd->daala_dec.state.adapt.skip_increment, "skip");
Yushin Cho77bba8d2016-11-04 16:36:56 -0700417 if (ac_dc_coded) {
418 int xdec = pd->subsampling_x;
419 int seg_id = mbmi->segment_id;
420 int16_t *quant;
421 FWD_TXFM_PARAM fwd_txfm_param;
Yaowu Xufc1b2132016-11-07 15:16:15 -0800422 // ToDo(yaowu): correct this with optimal number from decoding process.
423 const int max_scan_line = tx_size_2d[tx_size];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700424
425 for (j = 0; j < tx_blk_size; j++)
426 for (i = 0; i < tx_blk_size; i++) {
427 pred[diff_stride * j + i] = dst[pd->dst.stride * j + i];
428 }
429
430 fwd_txfm_param.tx_type = tx_type;
431 fwd_txfm_param.tx_size = tx_size;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700432 fwd_txfm_param.lossless = xd->lossless[seg_id];
433
434 fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
435
436 quant = &pd->seg_dequant[seg_id][0]; // aom's quantizer
437
438 eob = av1_pvq_decode_helper(&xd->daala_dec, pvq_ref_coeff, dqcoeff, quant,
439 plane, tx_size, tx_type, xdec, ac_dc_coded);
440
441 // Since av1 does not have separate inverse transform
442 // but also contains adding to predicted image,
443 // pass blank dummy image to av1_inv_txfm_add_*x*(), i.e. set dst as zeros
444 for (j = 0; j < tx_blk_size; j++)
445 for (i = 0; i < tx_blk_size; i++) dst[j * pd->dst.stride + i] = 0;
446
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800447 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
448 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700449 }
450
451 return eob;
452}
453#endif
454
Angie Chiangff6d8902016-10-21 11:02:09 -0700455static void predict_and_reconstruct_intra_block(AV1_COMMON *cm,
456 MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700457#if CONFIG_ANS
458 struct AnsDecoder *const r,
459#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700460 aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700461#endif // CONFIG_ANS
462 MB_MODE_INFO *const mbmi,
463 int plane, int row, int col,
464 TX_SIZE tx_size) {
465 struct macroblockd_plane *const pd = &xd->plane[plane];
466 PREDICTION_MODE mode = (plane == 0) ? mbmi->mode : mbmi->uv_mode;
467 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
468 uint8_t *dst;
469 int block_idx = (row << 1) + col;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700470#if CONFIG_PVQ
471 (void)cm;
472 (void)r;
473#endif
Jingning Hanca14dda2016-12-09 09:36:00 -0800474 dst = &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700475
Jingning Han41bb3392016-12-14 10:46:48 -0800476#if !CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -0700477 if (mbmi->sb_type < BLOCK_8X8)
478 if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
Jingning Han41bb3392016-12-14 10:46:48 -0800479#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700480
Jingning Hanc4c99da2016-10-24 10:27:28 -0700481 av1_predict_intra_block(xd, pd->width, pd->height, tx_size, mode, dst,
Yaowu Xuf883b422016-08-30 14:01:10 -0700482 pd->dst.stride, dst, pd->dst.stride, col, row, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700483
484 if (!mbmi->skip) {
485 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700486#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700487 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 0);
Jingning Han1be18782016-10-21 11:48:15 -0700488 int16_t max_scan_line = 0;
489 const int eob =
490 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size,
491 tx_type, &max_scan_line, r, mbmi->segment_id);
Angie Chianged8cd9a2016-10-21 16:44:47 -0700492#if CONFIG_ADAPT_SCAN
493 av1_update_scan_count_facade(cm, tx_size, tx_type, pd->dqcoeff, eob);
494#endif
Jingning Han1be18782016-10-21 11:48:15 -0700495 if (eob)
496 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
497 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700498#else
499 av1_pvq_decode_helper2(xd, mbmi, plane, row, col, tx_size, tx_type);
500#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700501 }
502}
503
504#if CONFIG_VAR_TX
Angie Chiangff6d8902016-10-21 11:02:09 -0700505static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
506 aom_reader *r, MB_MODE_INFO *const mbmi,
Jingning Han8fd62b72016-10-21 12:55:54 -0700507 int plane, BLOCK_SIZE plane_bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700508 int blk_row, int blk_col, TX_SIZE tx_size,
509 int *eob_total) {
510 const struct macroblockd_plane *const pd = &xd->plane[plane];
511 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
512 const int tx_row = blk_row >> (1 - pd->subsampling_y);
513 const int tx_col = blk_col >> (1 - pd->subsampling_x);
514 const TX_SIZE plane_tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700515 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700516 : mbmi->inter_tx_size[tx_row][tx_col];
Jingning Han5f614262016-10-27 14:27:43 -0700517 // Scale to match transform block unit.
Jingning Hanf64062f2016-11-02 16:22:18 -0700518 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
519 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700520
521 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
522
523 if (tx_size == plane_tx_size) {
524 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
Jingning Han8fd62b72016-10-21 12:55:54 -0700525 int block_idx = (blk_row << 1) + blk_col;
526 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiangff6d8902016-10-21 11:02:09 -0700527 const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700528 int16_t max_scan_line = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700529 const int eob =
Yaowu Xuf883b422016-08-30 14:01:10 -0700530 av1_decode_block_tokens(xd, plane, sc, blk_col, blk_row, plane_tx_size,
Jingning Han1be18782016-10-21 11:48:15 -0700531 tx_type, &max_scan_line, r, mbmi->segment_id);
Jingning Han9ca05b72017-01-03 14:41:36 -0800532 inverse_transform_block(xd, plane, tx_type, plane_tx_size,
533 &pd->dst.buf[(blk_row * pd->dst.stride + blk_col)
534 << tx_size_wide_log2[0]],
535 pd->dst.stride, max_scan_line, eob);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700536 *eob_total += eob;
537 } else {
Jingning Hanf64062f2016-11-02 16:22:18 -0700538 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
539 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700540 int i;
541
542 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700543
544 for (i = 0; i < 4; ++i) {
Jingning Han5f614262016-10-27 14:27:43 -0700545 const int offsetr = blk_row + (i >> 1) * bsl;
546 const int offsetc = blk_col + (i & 0x01) * bsl;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700547
548 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
549
Jingning Han8fd62b72016-10-21 12:55:54 -0700550 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
Jingning Hanf64062f2016-11-02 16:22:18 -0700551 offsetc, sub_txs, eob_total);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700552 }
553 }
554}
555#endif // CONFIG_VAR_TX
556
Jingning Hanfe45b212016-11-22 10:30:23 -0800557#if !CONFIG_VAR_TX || CONFIG_SUPERTX || \
558 (!CONFIG_VAR_TX && CONFIG_EXT_TX && CONFIG_RECT_TX)
Angie Chiangff6d8902016-10-21 11:02:09 -0700559static int reconstruct_inter_block(AV1_COMMON *cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700560#if CONFIG_ANS
561 struct AnsDecoder *const r,
562#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700563 aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700564#endif
565 int segment_id, int plane, int row, int col,
566 TX_SIZE tx_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700567 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
568 int block_idx = (row << 1) + col;
569 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700570#if CONFIG_PVQ
571 int eob;
572 (void)cm;
573 (void)r;
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800574 (void)segment_id;
575#else
576 struct macroblockd_plane *const pd = &xd->plane[plane];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700577#endif
578
579#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700580 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700581 int16_t max_scan_line = 0;
582 const int eob =
583 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size, tx_type,
584 &max_scan_line, r, segment_id);
Jingning Hanca14dda2016-12-09 09:36:00 -0800585 uint8_t *dst =
586 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Angie Chianged8cd9a2016-10-21 16:44:47 -0700587#if CONFIG_ADAPT_SCAN
588 av1_update_scan_count_facade(cm, tx_size, tx_type, pd->dqcoeff, eob);
589#endif
Jingning Han1be18782016-10-21 11:48:15 -0700590 if (eob)
Jingning Hanca14dda2016-12-09 09:36:00 -0800591 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
592 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700593#else
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800594 eob = av1_pvq_decode_helper2(xd, &xd->mi[0]->mbmi, plane, row, col, tx_size,
595 tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700596#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700597 return eob;
598}
599#endif // !CONFIG_VAR_TX || CONFIG_SUPER_TX
600
Yaowu Xuf883b422016-08-30 14:01:10 -0700601static MB_MODE_INFO *set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700602 BLOCK_SIZE bsize, int mi_row, int mi_col,
Jingning Hanfaad0e12016-12-07 10:54:57 -0800603 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700604 const int offset = mi_row * cm->mi_stride + mi_col;
605 int x, y;
606 const TileInfo *const tile = &xd->tile;
607
608 xd->mi = cm->mi_grid_visible + offset;
609 xd->mi[0] = &cm->mi[offset];
610 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
611 // passing bsize from decode_partition().
612 xd->mi[0]->mbmi.sb_type = bsize;
Angie Chiang394c3372016-11-03 11:13:15 -0700613#if CONFIG_RD_DEBUG
614 xd->mi[0]->mbmi.mi_row = mi_row;
615 xd->mi[0]->mbmi.mi_col = mi_col;
616#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700617 for (y = 0; y < y_mis; ++y)
Jingning Han97d85482016-07-15 11:06:05 -0700618 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700619
Jingning Hanfaad0e12016-12-07 10:54:57 -0800620 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621 set_skip_context(xd, mi_row, mi_col);
622
623#if CONFIG_VAR_TX
624 xd->max_tx_size = max_txsize_lookup[bsize];
625#endif
626
627 // Distance of Mb to the various image edges. These are specified to 8th pel
628 // as they are always compared to values that are in 1/8th pel units
629 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
630
Yaowu Xuf883b422016-08-30 14:01:10 -0700631 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700632 return &xd->mi[0]->mbmi;
633}
634
635#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700636static MB_MODE_INFO *set_offsets_extend(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700637 MACROBLOCKD *const xd,
638 const TileInfo *const tile,
639 BLOCK_SIZE bsize_pred, int mi_row_pred,
640 int mi_col_pred, int mi_row_ori,
641 int mi_col_ori) {
642 // Used in supertx
643 // (mi_row_ori, mi_col_ori): location for mv
644 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
Jingning Han93531242016-12-20 11:54:36 -0800645 const int bw = mi_size_wide[bsize_pred];
646 const int bh = mi_size_high[bsize_pred];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700647 const int offset = mi_row_ori * cm->mi_stride + mi_col_ori;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700648 xd->mi = cm->mi_grid_visible + offset;
649 xd->mi[0] = cm->mi + offset;
650 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
651 cm->mi_cols);
652
653 xd->up_available = (mi_row_ori > tile->mi_row_start);
654 xd->left_available = (mi_col_ori > tile->mi_col_start);
655
Jingning Hanfaad0e12016-12-07 10:54:57 -0800656 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700657
658 return &xd->mi[0]->mbmi;
659}
660
Yaowu Xuf883b422016-08-30 14:01:10 -0700661static MB_MODE_INFO *set_mb_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
662 BLOCK_SIZE bsize, int mi_row, int mi_col,
663 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700664 const int offset = mi_row * cm->mi_stride + mi_col;
665 const TileInfo *const tile = &xd->tile;
666 int x, y;
667
668 xd->mi = cm->mi_grid_visible + offset;
669 xd->mi[0] = cm->mi + offset;
670 xd->mi[0]->mbmi.sb_type = bsize;
671 for (y = 0; y < y_mis; ++y)
672 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
673
674 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
675 return &xd->mi[0]->mbmi;
676}
677
Yaowu Xuf883b422016-08-30 14:01:10 -0700678static void set_offsets_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700679 const TileInfo *const tile, BLOCK_SIZE bsize,
680 int mi_row, int mi_col) {
Jingning Han93531242016-12-20 11:54:36 -0800681 const int bw = mi_size_wide[bsize];
682 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700683 const int offset = mi_row * cm->mi_stride + mi_col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700684
685 xd->mi = cm->mi_grid_visible + offset;
686 xd->mi[0] = cm->mi + offset;
687
Jingning Hanfaad0e12016-12-07 10:54:57 -0800688 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700689
690 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
691
Yaowu Xuf883b422016-08-30 14:01:10 -0700692 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700693}
694
Yaowu Xuf883b422016-08-30 14:01:10 -0700695static void set_param_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700696 BLOCK_SIZE bsize, int mi_row, int mi_col,
697 int txfm, int skip) {
Jingning Han93531242016-12-20 11:54:36 -0800698 const int bw = mi_size_wide[bsize];
699 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -0700700 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
701 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700702 const int offset = mi_row * cm->mi_stride + mi_col;
703 int x, y;
704
705 xd->mi = cm->mi_grid_visible + offset;
706 xd->mi[0] = cm->mi + offset;
707
708 for (y = 0; y < y_mis; ++y)
709 for (x = 0; x < x_mis; ++x) {
710 xd->mi[y * cm->mi_stride + x]->mbmi.skip = skip;
711 xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
712 }
713#if CONFIG_VAR_TX
714 xd->above_txfm_context = cm->above_txfm_context + mi_col;
715 xd->left_txfm_context =
716 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
Yaowu Xu52a17632016-11-17 15:48:21 -0800717 set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bw, bh, skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700718#endif
719}
720
Yaowu Xuf883b422016-08-30 14:01:10 -0700721static void set_ref(AV1_COMMON *const cm, MACROBLOCKD *const xd, int idx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700722 int mi_row, int mi_col) {
723 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
724 RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
725 xd->block_refs[idx] = ref_buffer;
Yaowu Xuf883b422016-08-30 14:01:10 -0700726 if (!av1_is_valid_scale(&ref_buffer->sf))
727 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700728 "Invalid scale factors");
Yaowu Xuf883b422016-08-30 14:01:10 -0700729 av1_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
730 &ref_buffer->sf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700731 xd->corrupted |= ref_buffer->buf->corrupted;
732}
733
734static void dec_predict_b_extend(
Yaowu Xuf883b422016-08-30 14:01:10 -0700735 AV1Decoder *const pbi, MACROBLOCKD *const xd, const TileInfo *const tile,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700736 int block, int mi_row_ori, int mi_col_ori, int mi_row_pred, int mi_col_pred,
737 int mi_row_top, int mi_col_top, uint8_t *dst_buf[3], int dst_stride[3],
738 BLOCK_SIZE bsize_top, BLOCK_SIZE bsize_pred, int b_sub8x8, int bextend) {
739 // Used in supertx
740 // (mi_row_ori, mi_col_ori): location for mv
741 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
742 // (mi_row_top, mi_col_top, bsize_top): region of the top partition size
743 // block: sub location of sub8x8 blocks
744 // b_sub8x8: 1: ori is sub8x8; 0: ori is not sub8x8
745 // bextend: 1: region to predict is an extension of ori; 0: not
746 int r = (mi_row_pred - mi_row_top) * MI_SIZE;
747 int c = (mi_col_pred - mi_col_top) * MI_SIZE;
Jingning Han93531242016-12-20 11:54:36 -0800748 const int mi_width_top = mi_size_wide[bsize_top];
749 const int mi_height_top = mi_size_high[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700750 MB_MODE_INFO *mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700751 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700752
753 if (mi_row_pred < mi_row_top || mi_col_pred < mi_col_top ||
754 mi_row_pred >= mi_row_top + mi_height_top ||
755 mi_col_pred >= mi_col_top + mi_width_top || mi_row_pred >= cm->mi_rows ||
756 mi_col_pred >= cm->mi_cols)
757 return;
758
759 mbmi = set_offsets_extend(cm, xd, tile, bsize_pred, mi_row_pred, mi_col_pred,
760 mi_row_ori, mi_col_ori);
761 set_ref(cm, xd, 0, mi_row_pred, mi_col_pred);
762 if (has_second_ref(&xd->mi[0]->mbmi))
763 set_ref(cm, xd, 1, mi_row_pred, mi_col_pred);
764
Jingning Han2511c662016-12-22 11:57:34 -0800765 if (!bextend) mbmi->tx_size = max_txsize_lookup[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700766
767 xd->plane[0].dst.stride = dst_stride[0];
768 xd->plane[1].dst.stride = dst_stride[1];
769 xd->plane[2].dst.stride = dst_stride[2];
770 xd->plane[0].dst.buf = dst_buf[0] +
771 (r >> xd->plane[0].subsampling_y) * dst_stride[0] +
772 (c >> xd->plane[0].subsampling_x);
773 xd->plane[1].dst.buf = dst_buf[1] +
774 (r >> xd->plane[1].subsampling_y) * dst_stride[1] +
775 (c >> xd->plane[1].subsampling_x);
776 xd->plane[2].dst.buf = dst_buf[2] +
777 (r >> xd->plane[2].subsampling_y) * dst_stride[2] +
778 (c >> xd->plane[2].subsampling_x);
779
780 if (!b_sub8x8)
Yaowu Xuf883b422016-08-30 14:01:10 -0700781 av1_build_inter_predictors_sb_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700782#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700783 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700784#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700785 mi_row_pred, mi_col_pred, bsize_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700786 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700787 av1_build_inter_predictors_sb_sub8x8_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700788#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700789 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700790#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700791 mi_row_pred, mi_col_pred,
792 bsize_pred, block);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700793}
794
Yaowu Xuf883b422016-08-30 14:01:10 -0700795static void dec_extend_dir(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700796 const TileInfo *const tile, int block,
797 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
798 int mi_col, int mi_row_top, int mi_col_top,
799 uint8_t *dst_buf[3], int dst_stride[3], int dir) {
800 // dir: 0-lower, 1-upper, 2-left, 3-right
801 // 4-lowerleft, 5-upperleft, 6-lowerright, 7-upperright
Jingning Han93531242016-12-20 11:54:36 -0800802 const int mi_width = mi_size_wide[bsize];
803 const int mi_height = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700804 int xss = xd->plane[1].subsampling_x;
805 int yss = xd->plane[1].subsampling_y;
Jingning Hanfeb517c2016-12-21 16:02:07 -0800806#if CONFIG_CB4X4
807 const int unify_bsize = 1;
808#else
809 const int unify_bsize = 0;
810#endif
811 int b_sub8x8 = (bsize < BLOCK_8X8) && !unify_bsize ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700812 BLOCK_SIZE extend_bsize;
Jingning Han24f24a52016-12-27 10:13:28 -0800813 int mi_row_pred, mi_col_pred;
814
815 int wide_unit, high_unit;
816 int i, j;
817 int ext_offset = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700818
819 if (dir == 0 || dir == 1) {
Jingning Han93531242016-12-20 11:54:36 -0800820 extend_bsize =
821 (mi_width == mi_size_wide[BLOCK_8X8] || bsize < BLOCK_8X8 || xss < yss)
822 ? BLOCK_8X8
823 : BLOCK_16X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800824#if CONFIG_CB4X4
825 if (bsize < BLOCK_8X8) {
826 extend_bsize = BLOCK_4X4;
827 ext_offset = mi_size_wide[BLOCK_8X8];
828 }
829#endif
830
831 wide_unit = mi_size_wide[extend_bsize];
832 high_unit = mi_size_high[extend_bsize];
833
834 mi_row_pred = mi_row + ((dir == 0) ? mi_height : -(mi_height + ext_offset));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700835 mi_col_pred = mi_col;
836
Jingning Han24f24a52016-12-27 10:13:28 -0800837 for (j = 0; j < mi_height + ext_offset; j += high_unit)
838 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
839 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
840 mi_row_pred + j, mi_col_pred + i, mi_row_top,
841 mi_col_top, dst_buf, dst_stride, top_bsize,
842 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700843 } else if (dir == 2 || dir == 3) {
Jingning Han5b7706a2016-12-21 09:55:10 -0800844 extend_bsize =
845 (mi_height == mi_size_high[BLOCK_8X8] || bsize < BLOCK_8X8 || yss < xss)
846 ? BLOCK_8X8
847 : BLOCK_8X16;
Jingning Han24f24a52016-12-27 10:13:28 -0800848#if CONFIG_CB4X4
849 if (bsize < BLOCK_8X8) {
850 extend_bsize = BLOCK_4X4;
851 ext_offset = mi_size_wide[BLOCK_8X8];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700852 }
Jingning Han24f24a52016-12-27 10:13:28 -0800853#endif
854
855 wide_unit = mi_size_wide[extend_bsize];
856 high_unit = mi_size_high[extend_bsize];
857
858 mi_row_pred = mi_row;
859 mi_col_pred = mi_col + ((dir == 3) ? mi_width : -(mi_width + ext_offset));
860
861 for (j = 0; j < mi_height + ext_offset; j += high_unit)
862 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
863 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
864 mi_row_pred + j, mi_col_pred + i, mi_row_top,
865 mi_col_top, dst_buf, dst_stride, top_bsize,
866 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700867 } else {
868 extend_bsize = BLOCK_8X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800869#if CONFIG_CB4X4
870 if (bsize < BLOCK_8X8) {
871 extend_bsize = BLOCK_4X4;
872 ext_offset = mi_size_wide[BLOCK_8X8];
873 }
874#endif
875 wide_unit = mi_size_wide[extend_bsize];
876 high_unit = mi_size_high[extend_bsize];
877
Jingning Han5b7706a2016-12-21 09:55:10 -0800878 mi_row_pred = mi_row + ((dir == 4 || dir == 6) ? mi_height
Jingning Han24f24a52016-12-27 10:13:28 -0800879 : -(mi_height + ext_offset));
Jingning Han5b7706a2016-12-21 09:55:10 -0800880 mi_col_pred =
Jingning Han24f24a52016-12-27 10:13:28 -0800881 mi_col + ((dir == 6 || dir == 7) ? mi_width : -(mi_width + ext_offset));
882
883 for (j = 0; j < mi_height + ext_offset; j += high_unit)
884 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
885 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
886 mi_row_pred + j, mi_col_pred + i, mi_row_top,
887 mi_col_top, dst_buf, dst_stride, top_bsize,
888 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700889 }
890}
891
Yaowu Xuf883b422016-08-30 14:01:10 -0700892static void dec_extend_all(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700893 const TileInfo *const tile, int block,
894 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
895 int mi_col, int mi_row_top, int mi_col_top,
896 uint8_t *dst_buf[3], int dst_stride[3]) {
897 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
898 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
899 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
900 mi_row_top, mi_col_top, dst_buf, dst_stride, 1);
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, 2);
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, 3);
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, 4);
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, 5);
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, 6);
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, 7);
913}
914
Yaowu Xuf883b422016-08-30 14:01:10 -0700915static void dec_predict_sb_complex(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700916 const TileInfo *const tile, int mi_row,
917 int mi_col, int mi_row_top, int mi_col_top,
918 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize,
919 uint8_t *dst_buf[3], int dst_stride[3]) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700920 const AV1_COMMON *const cm = &pbi->common;
Jingning Han5b7706a2016-12-21 09:55:10 -0800921 const int hbs = mi_size_wide[bsize] / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700922 const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
923 const BLOCK_SIZE subsize = get_subsize(bsize, partition);
924#if CONFIG_EXT_PARTITION_TYPES
925 const BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
926#endif
927 int i;
928 const int mi_offset = mi_row * cm->mi_stride + mi_col;
929 uint8_t *dst_buf1[3], *dst_buf2[3], *dst_buf3[3];
Jingning Hanfeb517c2016-12-21 16:02:07 -0800930#if CONFIG_CB4X4
931 const int unify_bsize = 1;
932#else
933 const int unify_bsize = 0;
934#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700935
936 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
937 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
938 DECLARE_ALIGNED(16, uint8_t, tmp_buf3[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
939 int dst_stride1[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
940 int dst_stride2[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
941 int dst_stride3[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
942
Yaowu Xuf883b422016-08-30 14:01:10 -0700943#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700944 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
945 int len = sizeof(uint16_t);
946 dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
947 dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_TX_SQUARE * len);
948 dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_TX_SQUARE * len);
949 dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
950 dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_TX_SQUARE * len);
951 dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_TX_SQUARE * len);
952 dst_buf3[0] = CONVERT_TO_BYTEPTR(tmp_buf3);
953 dst_buf3[1] = CONVERT_TO_BYTEPTR(tmp_buf3 + MAX_TX_SQUARE * len);
954 dst_buf3[2] = CONVERT_TO_BYTEPTR(tmp_buf3 + 2 * MAX_TX_SQUARE * len);
955 } else {
956#endif
957 dst_buf1[0] = tmp_buf1;
958 dst_buf1[1] = tmp_buf1 + MAX_TX_SQUARE;
959 dst_buf1[2] = tmp_buf1 + 2 * MAX_TX_SQUARE;
960 dst_buf2[0] = tmp_buf2;
961 dst_buf2[1] = tmp_buf2 + MAX_TX_SQUARE;
962 dst_buf2[2] = tmp_buf2 + 2 * MAX_TX_SQUARE;
963 dst_buf3[0] = tmp_buf3;
964 dst_buf3[1] = tmp_buf3 + MAX_TX_SQUARE;
965 dst_buf3[2] = tmp_buf3 + 2 * MAX_TX_SQUARE;
Yaowu Xuf883b422016-08-30 14:01:10 -0700966#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700967 }
968#endif
969
970 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
971
972 xd->mi = cm->mi_grid_visible + mi_offset;
973 xd->mi[0] = cm->mi + mi_offset;
974
975 for (i = 0; i < MAX_MB_PLANE; i++) {
976 xd->plane[i].dst.buf = dst_buf[i];
977 xd->plane[i].dst.stride = dst_stride[i];
978 }
979
980 switch (partition) {
981 case PARTITION_NONE:
982 assert(bsize < top_bsize);
983 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
984 mi_row_top, mi_col_top, dst_buf, dst_stride,
985 top_bsize, bsize, 0, 0);
986 dec_extend_all(pbi, xd, tile, 0, bsize, top_bsize, mi_row, mi_col,
987 mi_row_top, mi_col_top, dst_buf, dst_stride);
988 break;
989 case PARTITION_HORZ:
Jingning Hanfeb517c2016-12-21 16:02:07 -0800990 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700991 // For sub8x8, predict in 8x8 unit
992 // First half
993 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
994 mi_row_top, mi_col_top, dst_buf, dst_stride,
995 top_bsize, BLOCK_8X8, 1, 0);
996 if (bsize < top_bsize)
997 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
998 mi_row_top, mi_col_top, dst_buf, dst_stride);
999
1000 // Second half
1001 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1002 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1003 top_bsize, BLOCK_8X8, 1, 1);
1004 if (bsize < top_bsize)
1005 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1006 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1007
1008 // weighted average to smooth the boundary
1009 xd->plane[0].dst.buf = dst_buf[0];
1010 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001011 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001012 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1013 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1014 0);
1015 } else {
1016 // First half
1017 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1018 mi_row_top, mi_col_top, dst_buf, dst_stride,
1019 top_bsize, subsize, 0, 0);
1020 if (bsize < top_bsize)
1021 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1022 mi_row_top, mi_col_top, dst_buf, dst_stride);
1023 else
1024 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1025 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1026
1027 if (mi_row + hbs < cm->mi_rows) {
1028 // Second half
1029 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col,
1030 mi_row + hbs, mi_col, mi_row_top, mi_col_top,
1031 dst_buf1, dst_stride1, top_bsize, subsize, 0, 0);
1032 if (bsize < top_bsize)
1033 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1034 mi_col, mi_row_top, mi_col_top, dst_buf1,
1035 dst_stride1);
1036 else
1037 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1038 mi_col, mi_row_top, mi_col_top, dst_buf1,
1039 dst_stride1, 1);
1040
1041 // weighted average to smooth the boundary
1042 for (i = 0; i < MAX_MB_PLANE; i++) {
1043 xd->plane[i].dst.buf = dst_buf[i];
1044 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001045 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001046 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1047 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1048 PARTITION_HORZ, i);
1049 }
1050 }
1051 }
1052 break;
1053 case PARTITION_VERT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001054 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001055 // First half
1056 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1057 mi_row_top, mi_col_top, dst_buf, dst_stride,
1058 top_bsize, BLOCK_8X8, 1, 0);
1059 if (bsize < top_bsize)
1060 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1061 mi_row_top, mi_col_top, dst_buf, dst_stride);
1062
1063 // Second half
1064 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1065 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1066 top_bsize, BLOCK_8X8, 1, 1);
1067 if (bsize < top_bsize)
1068 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1069 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1070
1071 // Smooth
1072 xd->plane[0].dst.buf = dst_buf[0];
1073 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001074 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001075 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1076 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1077 0);
1078 } else {
1079 // First half
1080 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1081 mi_row_top, mi_col_top, dst_buf, dst_stride,
1082 top_bsize, subsize, 0, 0);
1083 if (bsize < top_bsize)
1084 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1085 mi_row_top, mi_col_top, dst_buf, dst_stride);
1086 else
1087 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1088 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1089
1090 // Second half
1091 if (mi_col + hbs < cm->mi_cols) {
1092 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1093 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1094 dst_stride1, top_bsize, subsize, 0, 0);
1095 if (bsize < top_bsize)
1096 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1097 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1098 dst_stride1);
1099 else
1100 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1101 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1102 dst_stride1, 2);
1103
1104 // Smooth
1105 for (i = 0; i < MAX_MB_PLANE; i++) {
1106 xd->plane[i].dst.buf = dst_buf[i];
1107 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001108 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001109 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1110 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1111 PARTITION_VERT, i);
1112 }
1113 }
1114 }
1115 break;
1116 case PARTITION_SPLIT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001117 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001118 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1119 mi_row_top, mi_col_top, dst_buf, dst_stride,
1120 top_bsize, BLOCK_8X8, 1, 0);
1121 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1122 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1123 top_bsize, BLOCK_8X8, 1, 1);
1124 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1125 mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1126 top_bsize, BLOCK_8X8, 1, 1);
1127 dec_predict_b_extend(pbi, xd, tile, 3, mi_row, mi_col, mi_row, mi_col,
1128 mi_row_top, mi_col_top, dst_buf3, dst_stride3,
1129 top_bsize, BLOCK_8X8, 1, 1);
1130 if (bsize < top_bsize) {
1131 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1132 mi_row_top, mi_col_top, dst_buf, dst_stride);
1133 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1134 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1135 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1136 mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1137 dec_extend_all(pbi, xd, tile, 3, subsize, top_bsize, mi_row, mi_col,
1138 mi_row_top, mi_col_top, dst_buf3, dst_stride3);
1139 }
1140 } else {
1141 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row_top,
1142 mi_col_top, subsize, top_bsize, dst_buf,
1143 dst_stride);
1144 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1145 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col + hbs,
1146 mi_row_top, mi_col_top, subsize, top_bsize,
1147 dst_buf1, dst_stride1);
1148 if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols)
1149 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col,
1150 mi_row_top, mi_col_top, subsize, top_bsize,
1151 dst_buf2, dst_stride2);
1152 if (mi_row + hbs < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1153 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col + hbs,
1154 mi_row_top, mi_col_top, subsize, top_bsize,
1155 dst_buf3, dst_stride3);
1156 }
1157 for (i = 0; i < MAX_MB_PLANE; i++) {
Jingning Han9e0976a2016-12-27 17:52:42 -08001158#if !CONFIG_CB4X4
Jingning Han24f24a52016-12-27 10:13:28 -08001159 if (bsize == BLOCK_8X8 && i != 0)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001160 continue; // Skip <4x4 chroma smoothing
Jingning Han9e0976a2016-12-27 17:52:42 -08001161#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001162 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001163 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001164 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1165 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1166 PARTITION_VERT, i);
1167 if (mi_row + hbs < cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001168 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001169 xd, dst_buf2[i], dst_stride2[i], dst_buf3[i], dst_stride3[i],
1170 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1171 PARTITION_VERT, i);
Yaowu Xuf883b422016-08-30 14:01:10 -07001172 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001173 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1174 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1175 PARTITION_HORZ, i);
1176 }
1177 } else if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001178 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001179 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1180 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1181 PARTITION_HORZ, i);
1182 }
1183 }
1184 break;
1185#if CONFIG_EXT_PARTITION_TYPES
1186 case PARTITION_HORZ_A:
1187 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1188 mi_row_top, mi_col_top, dst_buf, dst_stride,
1189 top_bsize, bsize2, 0, 0);
1190 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1191 mi_row_top, mi_col_top, dst_buf, dst_stride);
1192
1193 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1194 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1195 dst_stride1, top_bsize, bsize2, 0, 0);
1196 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1197 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1198
1199 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1200 mi_col, mi_row_top, mi_col_top, dst_buf2,
1201 dst_stride2, top_bsize, subsize, 0, 0);
1202 if (bsize < top_bsize)
1203 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1204 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1205 else
1206 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1207 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1208 1);
1209
1210 for (i = 0; i < MAX_MB_PLANE; i++) {
1211 xd->plane[i].dst.buf = dst_buf[i];
1212 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001213 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001214 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1215 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1216 i);
1217 }
1218 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001219 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001220 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1221 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1222 i);
1223 }
1224 break;
1225 case PARTITION_VERT_A:
1226
1227 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1228 mi_row_top, mi_col_top, dst_buf, dst_stride,
1229 top_bsize, bsize2, 0, 0);
1230 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1231 mi_row_top, mi_col_top, dst_buf, dst_stride);
1232
1233 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1234 mi_col, mi_row_top, mi_col_top, dst_buf1,
1235 dst_stride1, top_bsize, bsize2, 0, 0);
1236 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1237 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1238
1239 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1240 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1241 dst_stride2, top_bsize, subsize, 0, 0);
1242 if (bsize < top_bsize)
1243 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1244 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1245 dst_stride2);
1246 else
1247 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1248 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1249 dst_stride2, 2);
1250
1251 for (i = 0; i < MAX_MB_PLANE; i++) {
1252 xd->plane[i].dst.buf = dst_buf[i];
1253 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001254 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001255 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1256 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1257 i);
1258 }
1259 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001260 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001261 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1262 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1263 i);
1264 }
1265 break;
1266 case PARTITION_HORZ_B:
1267 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1268 mi_row_top, mi_col_top, dst_buf, dst_stride,
1269 top_bsize, subsize, 0, 0);
1270 if (bsize < top_bsize)
1271 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1272 mi_row_top, mi_col_top, dst_buf, dst_stride);
1273 else
1274 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1275 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1276
1277 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1278 mi_col, mi_row_top, mi_col_top, dst_buf1,
1279 dst_stride1, top_bsize, bsize2, 0, 0);
1280 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1281 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1282
1283 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1284 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1285 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1286 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1287 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1288 dst_stride2);
1289
1290 for (i = 0; i < MAX_MB_PLANE; i++) {
1291 xd->plane[i].dst.buf = dst_buf1[i];
1292 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001293 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001294 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1295 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1296 PARTITION_VERT, i);
1297 }
1298 for (i = 0; i < MAX_MB_PLANE; i++) {
1299 xd->plane[i].dst.buf = dst_buf[i];
1300 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001301 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001302 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1303 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1304 i);
1305 }
1306 break;
1307 case PARTITION_VERT_B:
1308 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1309 mi_row_top, mi_col_top, dst_buf, dst_stride,
1310 top_bsize, subsize, 0, 0);
1311 if (bsize < top_bsize)
1312 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1313 mi_row_top, mi_col_top, dst_buf, dst_stride);
1314 else
1315 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1316 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1317
1318 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1319 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1320 dst_stride1, top_bsize, bsize2, 0, 0);
1321 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1322 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1323
1324 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1325 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1326 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1327 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1328 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1329 dst_stride2);
1330
1331 for (i = 0; i < MAX_MB_PLANE; i++) {
1332 xd->plane[i].dst.buf = dst_buf1[i];
1333 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001334 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001335 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1336 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1337 PARTITION_HORZ, i);
1338 }
1339 for (i = 0; i < MAX_MB_PLANE; i++) {
1340 xd->plane[i].dst.buf = dst_buf[i];
1341 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001342 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001343 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1344 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1345 i);
1346 }
1347 break;
1348#endif // CONFIG_EXT_PARTITION_TYPES
1349 default: assert(0);
1350 }
1351}
1352
Yaowu Xuf883b422016-08-30 14:01:10 -07001353static void set_segment_id_supertx(const AV1_COMMON *const cm, const int mi_row,
1354 const int mi_col, const BLOCK_SIZE bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001355 const struct segmentation *seg = &cm->seg;
Jingning Han5b7706a2016-12-21 09:55:10 -08001356 const int miw = AOMMIN(mi_size_wide[bsize], cm->mi_cols - mi_col);
1357 const int mih = AOMMIN(mi_size_high[bsize], cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001358 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1359 MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
1360 int r, c;
1361 int seg_id_supertx = MAX_SEGMENTS;
1362
1363 if (!seg->enabled) {
1364 seg_id_supertx = 0;
1365 } else {
1366 // Find the minimum segment_id
1367 for (r = 0; r < mih; r++)
1368 for (c = 0; c < miw; c++)
1369 seg_id_supertx =
Yaowu Xuf883b422016-08-30 14:01:10 -07001370 AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001371 assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);
1372 }
1373
1374 // Assign the the segment_id back to segment_id_supertx
1375 for (r = 0; r < mih; r++)
1376 for (c = 0; c < miw; c++)
1377 mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
1378}
1379#endif // CONFIG_SUPERTX
1380
Yue Chen64550b62017-01-12 12:18:22 -08001381static void decode_mbmi_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001382#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001383 int supertx_enabled,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001384#endif // CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001385 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001386#if CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001387 PARTITION_TYPE partition,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001388#endif // CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -08001389 BLOCK_SIZE bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001390 AV1_COMMON *const cm = &pbi->common;
Jingning Han85dc03f2016-12-06 16:03:10 -08001391 const int bw = mi_size_wide[bsize];
1392 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -07001393 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1394 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Nathan E. Eggeebbd4792016-10-05 19:30:15 -04001395
Michael Bebenita6048d052016-08-25 14:40:54 -07001396#if CONFIG_ACCOUNTING
1397 aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
1398#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001399#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001400 if (supertx_enabled) {
Yue Chen64550b62017-01-12 12:18:22 -08001401 set_mb_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001402 } else {
Yue Chen64550b62017-01-12 12:18:22 -08001403 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001404 }
1405#if CONFIG_EXT_PARTITION_TYPES
1406 xd->mi[0]->mbmi.partition = partition;
1407#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001408 av1_read_mode_info(pbi, xd, supertx_enabled, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001409#else
Yue Chen64550b62017-01-12 12:18:22 -08001410 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001411#if CONFIG_EXT_PARTITION_TYPES
1412 xd->mi[0]->mbmi.partition = partition;
1413#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001414 av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001415#endif // CONFIG_SUPERTX
1416
1417 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1418 const BLOCK_SIZE uv_subsize =
1419 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1420 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -07001421 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001422 "Invalid block size.");
1423 }
1424
1425#if CONFIG_SUPERTX
Yue Chen64550b62017-01-12 12:18:22 -08001426 xd->mi[0]->mbmi.segment_id_supertx = MAX_SEGMENTS;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001427#endif // CONFIG_SUPERTX
1428
Yue Chen64550b62017-01-12 12:18:22 -08001429 xd->corrupted |= aom_reader_has_error(r);
1430}
1431
1432static void decode_token_and_recon_block(AV1Decoder *const pbi,
1433 MACROBLOCKD *const xd, int mi_row,
1434 int mi_col, aom_reader *r,
1435 BLOCK_SIZE bsize) {
1436 AV1_COMMON *const cm = &pbi->common;
1437 const int bw = mi_size_wide[bsize];
1438 const int bh = mi_size_high[bsize];
1439 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1440 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
1441 MB_MODE_INFO *mbmi;
1442
1443 mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Arild Fuldseth07441162016-08-15 15:07:52 +02001444#if CONFIG_DELTA_Q
1445 if (cm->delta_q_present_flag) {
1446 int i;
1447 for (i = 0; i < MAX_SEGMENTS; i++) {
1448 xd->plane[0].seg_dequant[i][0] =
1449 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
1450 xd->plane[0].seg_dequant[i][1] =
1451 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
1452 xd->plane[1].seg_dequant[i][0] =
1453 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1454 xd->plane[1].seg_dequant[i][1] =
1455 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1456 xd->plane[2].seg_dequant[i][0] =
1457 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1458 xd->plane[2].seg_dequant[i][1] =
1459 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1460 }
1461 }
1462#endif
1463
Jingning Han41bb3392016-12-14 10:46:48 -08001464#if CONFIG_CB4X4
1465 if (mbmi->skip) reset_skip_context(xd, bsize);
1466#else
Jingning Hand39cc722016-12-02 14:03:26 -08001467 if (mbmi->skip) reset_skip_context(xd, AOMMAX(BLOCK_8X8, bsize));
Jingning Han41bb3392016-12-14 10:46:48 -08001468#endif
Jingning Hand39cc722016-12-02 14:03:26 -08001469
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001470#if CONFIG_COEF_INTERLEAVE
1471 {
1472 const struct macroblockd_plane *const pd_y = &xd->plane[0];
1473 const struct macroblockd_plane *const pd_c = &xd->plane[1];
1474 const TX_SIZE tx_log2_y = mbmi->tx_size;
1475 const TX_SIZE tx_log2_c = get_uv_tx_size(mbmi, pd_c);
1476 const int tx_sz_y = (1 << tx_log2_y);
1477 const int tx_sz_c = (1 << tx_log2_c);
1478 const int num_4x4_w_y = pd_y->n4_w;
1479 const int num_4x4_h_y = pd_y->n4_h;
1480 const int num_4x4_w_c = pd_c->n4_w;
1481 const int num_4x4_h_c = pd_c->n4_h;
1482 const int max_4x4_w_y = get_max_4x4_size(num_4x4_w_y, xd->mb_to_right_edge,
1483 pd_y->subsampling_x);
1484 const int max_4x4_h_y = get_max_4x4_size(num_4x4_h_y, xd->mb_to_bottom_edge,
1485 pd_y->subsampling_y);
1486 const int max_4x4_w_c = get_max_4x4_size(num_4x4_w_c, xd->mb_to_right_edge,
1487 pd_c->subsampling_x);
1488 const int max_4x4_h_c = get_max_4x4_size(num_4x4_h_c, xd->mb_to_bottom_edge,
1489 pd_c->subsampling_y);
1490
1491 // The max_4x4_w/h may be smaller than tx_sz under some corner cases,
1492 // i.e. when the SB is splitted by tile boundaries.
1493 const int tu_num_w_y = (max_4x4_w_y + tx_sz_y - 1) / tx_sz_y;
1494 const int tu_num_h_y = (max_4x4_h_y + tx_sz_y - 1) / tx_sz_y;
1495 const int tu_num_w_c = (max_4x4_w_c + tx_sz_c - 1) / tx_sz_c;
1496 const int tu_num_h_c = (max_4x4_h_c + tx_sz_c - 1) / tx_sz_c;
1497 const int tu_num_y = tu_num_w_y * tu_num_h_y;
1498 const int tu_num_c = tu_num_w_c * tu_num_h_c;
1499
1500 if (!is_inter_block(mbmi)) {
1501 int tu_idx_c = 0;
1502 int row_y, col_y, row_c, col_c;
1503 int plane;
1504
1505#if CONFIG_PALETTE
1506 for (plane = 0; plane <= 1; ++plane) {
1507 if (mbmi->palette_mode_info.palette_size[plane])
1508 av1_decode_palette_tokens(xd, plane, r);
1509 }
1510#endif
1511
1512 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1513 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1514 // luma
1515 predict_and_reconstruct_intra_block(
1516 cm, xd, r, mbmi, 0, row_y * tx_sz_y, col_y * tx_sz_y, tx_log2_y);
1517 // chroma
1518 if (tu_idx_c < tu_num_c) {
1519 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1520 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1521 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c,
1522 col_c, tx_log2_c);
1523 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c,
1524 col_c, tx_log2_c);
1525 tu_idx_c++;
1526 }
1527 }
1528 }
1529
1530 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1531 while (tu_idx_c < tu_num_c) {
1532 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1533 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1534 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c, col_c,
1535 tx_log2_c);
1536 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c, col_c,
1537 tx_log2_c);
1538 tu_idx_c++;
1539 }
1540 } else {
1541 // Prediction
David Barkerac37fa32016-12-02 12:30:21 +00001542 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001543 AOMMAX(bsize, BLOCK_8X8));
1544
1545 // Reconstruction
1546 if (!mbmi->skip) {
1547 int eobtotal = 0;
1548 int tu_idx_c = 0;
1549 int row_y, col_y, row_c, col_c;
1550
1551 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1552 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1553 // luma
1554 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 0,
1555 row_y * tx_sz_y,
1556 col_y * tx_sz_y, tx_log2_y);
1557 // chroma
1558 if (tu_idx_c < tu_num_c) {
1559 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1560 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1561 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1562 1, row_c, col_c, tx_log2_c);
1563 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1564 2, row_c, col_c, tx_log2_c);
1565 tu_idx_c++;
1566 }
1567 }
1568 }
1569
1570 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1571 while (tu_idx_c < tu_num_c) {
1572 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1573 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1574 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 1,
1575 row_c, col_c, tx_log2_c);
1576 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 2,
1577 row_c, col_c, tx_log2_c);
1578 tu_idx_c++;
1579 }
1580
1581 if (bsize >= BLOCK_8X8 && eobtotal == 0)
1582#if CONFIG_MISC_FIXES
1583 mbmi->has_no_coeffs = 1;
1584#else
1585 mbmi->skip = 1;
1586#endif
1587 }
1588 }
1589 }
1590#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001591 if (!is_inter_block(mbmi)) {
1592 int plane;
Urvang Joshib100db72016-10-12 16:28:56 -07001593#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001594 for (plane = 0; plane <= 1; ++plane) {
1595 if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xuf883b422016-08-30 14:01:10 -07001596 av1_decode_palette_tokens(xd, plane, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001597 }
Urvang Joshib100db72016-10-12 16:28:56 -07001598#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001599 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1600 const struct macroblockd_plane *const pd = &xd->plane[plane];
Debargha Mukherjee2f123402016-08-30 17:43:38 -07001601 const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han2d64f122016-10-21 12:44:29 -07001602 const int stepr = tx_size_high_unit[tx_size];
1603 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han41bb3392016-12-14 10:46:48 -08001604#if CONFIG_CB4X4
1605 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1606#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001607 const BLOCK_SIZE plane_bsize =
1608 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001609#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001610 int row, col;
Jingning Hanbafee8d2016-12-02 10:25:03 -08001611 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1612 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001613
1614 for (row = 0; row < max_blocks_high; row += stepr)
1615 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001616 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane, row, col,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001617 tx_size);
1618 }
1619 } else {
Yue Chen69f18e12016-09-08 14:48:15 -07001620// Prediction
1621#if CONFIG_WARPED_MOTION
1622 if (mbmi->motion_mode == WARPED_CAUSAL) {
1623 int i;
1624#if CONFIG_AOM_HIGHBITDEPTH
1625 int use_hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH;
1626#endif // CONFIG_AOM_HIGHBITDEPTH
1627
1628 for (i = 0; i < 3; ++i) {
1629 const struct macroblockd_plane *pd = &xd->plane[i];
1630
1631 av1_warp_plane(&mbmi->wm_params[0],
1632#if CONFIG_AOM_HIGHBITDEPTH
1633 xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH, xd->bd,
1634#endif // CONFIG_AOM_HIGHBITDEPTH
1635 pd->pre[0].buf0, pd->pre[0].width, pd->pre[0].height,
1636 pd->pre[0].stride, pd->dst.buf,
1637 ((mi_col * MI_SIZE) >> pd->subsampling_x),
1638 ((mi_row * MI_SIZE) >> pd->subsampling_y),
Jingning Hanff6ee6a2016-12-07 09:55:21 -08001639 xd->n8_w * (MI_SIZE >> pd->subsampling_x),
1640 xd->n8_h * (MI_SIZE >> pd->subsampling_y),
1641 pd->dst.stride, pd->subsampling_x, pd->subsampling_y, 16,
1642 16, 0);
Yue Chen69f18e12016-09-08 14:48:15 -07001643 }
1644 } else {
1645#endif // CONFIG_WARPED_MOTION
Jingning Han41bb3392016-12-14 10:46:48 -08001646#if CONFIG_CB4X4
1647 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL, bsize);
1648#else
1649 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
1650 AOMMAX(bsize, BLOCK_8X8));
1651#endif
Yue Chen69f18e12016-09-08 14:48:15 -07001652#if CONFIG_WARPED_MOTION
1653 }
1654#endif // CONFIG_WARPED_MOTION
Yue Chencb60b182016-10-13 15:18:22 -07001655#if CONFIG_MOTION_VAR
1656 if (mbmi->motion_mode == OBMC_CAUSAL) {
Yue Chen894fcce2016-10-21 16:50:52 -07001657 av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001658 }
Yue Chencb60b182016-10-13 15:18:22 -07001659#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001660
1661 // Reconstruction
1662 if (!mbmi->skip) {
1663 int eobtotal = 0;
1664 int plane;
1665
1666 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1667 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han41bb3392016-12-14 10:46:48 -08001668#if CONFIG_CB4X4
1669 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1670#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001671 const BLOCK_SIZE plane_bsize =
1672 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001673#endif
Jingning Hanbafee8d2016-12-02 10:25:03 -08001674 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1675 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001676 int row, col;
1677#if CONFIG_VAR_TX
Jingning Han70e5f3f2016-11-09 17:03:07 -08001678 const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07001679 const int bh_var_tx = tx_size_high_unit[max_tx_size];
1680 const int bw_var_tx = tx_size_wide_unit[max_tx_size];
Jingning Hanbafee8d2016-12-02 10:25:03 -08001681 for (row = 0; row < max_blocks_high; row += bh_var_tx)
1682 for (col = 0; col < max_blocks_wide; col += bw_var_tx)
Jingning Hanfe45b212016-11-22 10:30:23 -08001683 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, row, col,
1684 max_tx_size, &eobtotal);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001685#else
1686 const TX_SIZE tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -07001687 plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han2d64f122016-10-21 12:44:29 -07001688 const int stepr = tx_size_high_unit[tx_size];
1689 const int stepc = tx_size_wide_unit[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001690 for (row = 0; row < max_blocks_high; row += stepr)
1691 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001692 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1693 plane, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001694#endif
1695 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001696 }
1697 }
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001698#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001699
Yaowu Xuf883b422016-08-30 14:01:10 -07001700 xd->corrupted |= aom_reader_has_error(r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001701}
1702
Yue Chen64550b62017-01-12 12:18:22 -08001703static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
1704#if CONFIG_SUPERTX
1705 int supertx_enabled,
1706#endif // CONFIG_SUPERTX
1707 int mi_row, int mi_col, aom_reader *r,
1708#if CONFIG_EXT_PARTITION_TYPES
1709 PARTITION_TYPE partition,
1710#endif // CONFIG_EXT_PARTITION_TYPES
1711 BLOCK_SIZE bsize) {
1712 decode_mbmi_block(pbi, xd,
1713#if CONFIG_SUPERTX
1714 supertx_enabled,
1715#endif
1716 mi_row, mi_col, r,
1717#if CONFIG_EXT_PARTITION_TYPES
1718 partition,
1719#endif
1720 bsize);
1721#if CONFIG_SUPERTX
1722 if (!supertx_enabled)
1723#endif // CONFIG_SUPERTX
1724 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
1725}
1726
Yaowu Xuf883b422016-08-30 14:01:10 -07001727static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
1728 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001729 int has_rows, int has_cols,
Jingning Han1beb0102016-12-07 11:08:30 -08001730 BLOCK_SIZE bsize) {
1731 const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
Yaowu Xuf883b422016-08-30 14:01:10 -07001732 const aom_prob *const probs = cm->fc->partition_prob[ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001733 FRAME_COUNTS *counts = xd->counts;
1734 PARTITION_TYPE p;
1735
1736 if (has_rows && has_cols)
1737#if CONFIG_EXT_PARTITION_TYPES
1738 if (bsize <= BLOCK_8X8)
Michael Bebenita6048d052016-08-25 14:40:54 -07001739 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001740 else
Michael Bebenita6048d052016-08-25 14:40:54 -07001741 p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs,
1742 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001743#else
Nathan E. Egge9d9eb6c2016-11-16 09:44:26 -05001744#if CONFIG_EC_MULTISYMBOL
Yaowu Xuf2581a32016-10-20 13:05:47 -07001745 p = (PARTITION_TYPE)aom_read_symbol(r, cm->fc->partition_cdf[ctx],
1746 PARTITION_TYPES, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001747#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001748 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001749#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001750#endif // CONFIG_EXT_PARTITION_TYPES
1751 else if (!has_rows && has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001752 p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001753 else if (has_rows && !has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001754 p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001755 else
1756 p = PARTITION_SPLIT;
1757
1758 if (counts) ++counts->partition[ctx][p];
1759
1760 return p;
1761}
1762
1763#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001764static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
1765 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001766 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
1767 return 1;
1768 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001769 const int ctx = av1_get_skip_context(xd);
Michael Bebenita6048d052016-08-25 14:40:54 -07001770 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001771 FRAME_COUNTS *counts = xd->counts;
1772 if (counts) ++counts->skip[ctx][skip];
1773 return skip;
1774 }
1775}
1776#endif // CONFIG_SUPERTX
Yaowu Xud71be782016-10-14 08:47:03 -07001777#if CONFIG_CLPF
1778static int clpf_all_skip(const AV1_COMMON *cm, int mi_col, int mi_row,
1779 int size) {
1780 int r, c;
1781 int skip = 1;
1782 const int maxc = AOMMIN(size, cm->mi_cols - mi_col);
1783 const int maxr = AOMMIN(size, cm->mi_rows - mi_row);
1784 for (r = 0; r < maxr && skip; r++) {
1785 for (c = 0; c < maxc && skip; c++) {
1786 skip &= !!cm->mi_grid_visible[(mi_row + r) * cm->mi_stride + mi_col + c]
1787 ->mbmi.skip;
1788 }
1789 }
1790 return skip;
1791}
1792#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001793
1794// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -07001795static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001796#if CONFIG_SUPERTX
1797 int supertx_enabled,
1798#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001799 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001800 BLOCK_SIZE bsize, int n4x4_l2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001801 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001802 const int n8x8_l2 = n4x4_l2 - 1;
Jingning Hanff17e162016-12-07 17:58:18 -08001803 const int num_8x8_wh = mi_size_wide[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001804 const int hbs = num_8x8_wh >> 1;
Jingning Han41bb3392016-12-14 10:46:48 -08001805#if CONFIG_CB4X4
1806 const int unify_bsize = 1;
1807#else
1808 const int unify_bsize = 0;
1809#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001810 PARTITION_TYPE partition;
1811 BLOCK_SIZE subsize;
1812#if CONFIG_EXT_PARTITION_TYPES
1813 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1814#endif
1815 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1816 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1817#if CONFIG_SUPERTX
1818 const int read_token = !supertx_enabled;
1819 int skip = 0;
Jingning Han2511c662016-12-22 11:57:34 -08001820 TX_SIZE supertx_size = max_txsize_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001821 const TileInfo *const tile = &xd->tile;
1822 int txfm = DCT_DCT;
1823#endif // CONFIG_SUPERTX
1824
1825 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1826
Jingning Han6dbacf22016-12-14 17:28:35 -08001827 partition = (n4x4_l2 == 0) ? PARTITION_NONE
1828 : read_partition(cm, xd, mi_row, mi_col, r,
1829 has_rows, has_cols, bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001830 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
Yushin Cho77bba8d2016-11-04 16:36:56 -07001831
1832#if CONFIG_PVQ
1833 assert(partition < PARTITION_TYPES);
1834 assert(subsize < BLOCK_SIZES);
1835#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001836#if CONFIG_SUPERTX
1837 if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
1838 bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
1839 const int supertx_context = partition_supertx_context_lookup[partition];
Michael Bebenita6048d052016-08-25 14:40:54 -07001840 supertx_enabled = aom_read(
1841 r, cm->fc->supertx_prob[supertx_context][supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001842 if (xd->counts)
1843 xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
1844#if CONFIG_VAR_TX
1845 if (supertx_enabled) xd->supertx_size = supertx_size;
1846#endif
1847 }
1848#endif // CONFIG_SUPERTX
Jingning Han41bb3392016-12-14 10:46:48 -08001849 if (!hbs && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001850 // calculate bmode block dimensions (log 2)
1851 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1852 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1853 decode_block(pbi, xd,
1854#if CONFIG_SUPERTX
1855 supertx_enabled,
1856#endif // CONFIG_SUPERTX
1857 mi_row, mi_col, r,
1858#if CONFIG_EXT_PARTITION_TYPES
1859 partition,
1860#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001861 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001862 } else {
1863 switch (partition) {
1864 case PARTITION_NONE:
1865 decode_block(pbi, xd,
1866#if CONFIG_SUPERTX
1867 supertx_enabled,
1868#endif // CONFIG_SUPERTX
1869 mi_row, mi_col, r,
1870#if CONFIG_EXT_PARTITION_TYPES
1871 partition,
1872#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001873 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001874 break;
1875 case PARTITION_HORZ:
1876 decode_block(pbi, xd,
1877#if CONFIG_SUPERTX
1878 supertx_enabled,
1879#endif // CONFIG_SUPERTX
1880 mi_row, mi_col, r,
1881#if CONFIG_EXT_PARTITION_TYPES
1882 partition,
1883#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001884 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001885 if (has_rows)
1886 decode_block(pbi, xd,
1887#if CONFIG_SUPERTX
1888 supertx_enabled,
1889#endif // CONFIG_SUPERTX
1890 mi_row + hbs, mi_col, r,
1891#if CONFIG_EXT_PARTITION_TYPES
1892 partition,
1893#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001894 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001895 break;
1896 case PARTITION_VERT:
1897 decode_block(pbi, xd,
1898#if CONFIG_SUPERTX
1899 supertx_enabled,
1900#endif // CONFIG_SUPERTX
1901 mi_row, mi_col, r,
1902#if CONFIG_EXT_PARTITION_TYPES
1903 partition,
1904#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001905 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001906 if (has_cols)
1907 decode_block(pbi, xd,
1908#if CONFIG_SUPERTX
1909 supertx_enabled,
1910#endif // CONFIG_SUPERTX
1911 mi_row, mi_col + hbs, r,
1912#if CONFIG_EXT_PARTITION_TYPES
1913 partition,
1914#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001915 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001916 break;
1917 case PARTITION_SPLIT:
1918 decode_partition(pbi, xd,
1919#if CONFIG_SUPERTX
1920 supertx_enabled,
1921#endif // CONFIG_SUPERTX
1922 mi_row, mi_col, r, subsize, n8x8_l2);
1923 decode_partition(pbi, xd,
1924#if CONFIG_SUPERTX
1925 supertx_enabled,
1926#endif // CONFIG_SUPERTX
1927 mi_row, mi_col + hbs, r, subsize, n8x8_l2);
1928 decode_partition(pbi, xd,
1929#if CONFIG_SUPERTX
1930 supertx_enabled,
1931#endif // CONFIG_SUPERTX
1932 mi_row + hbs, mi_col, r, subsize, n8x8_l2);
1933 decode_partition(pbi, xd,
1934#if CONFIG_SUPERTX
1935 supertx_enabled,
1936#endif // CONFIG_SUPERTX
1937 mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
1938 break;
1939#if CONFIG_EXT_PARTITION_TYPES
1940 case PARTITION_HORZ_A:
1941 decode_block(pbi, xd,
1942#if CONFIG_SUPERTX
1943 supertx_enabled,
1944#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001945 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001946 decode_block(pbi, xd,
1947#if CONFIG_SUPERTX
1948 supertx_enabled,
1949#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001950 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001951 decode_block(pbi, xd,
1952#if CONFIG_SUPERTX
1953 supertx_enabled,
1954#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001955 mi_row + hbs, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001956 break;
1957 case PARTITION_HORZ_B:
1958 decode_block(pbi, xd,
1959#if CONFIG_SUPERTX
1960 supertx_enabled,
1961#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001962 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001963 decode_block(pbi, xd,
1964#if CONFIG_SUPERTX
1965 supertx_enabled,
1966#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001967 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001968 decode_block(pbi, xd,
1969#if CONFIG_SUPERTX
1970 supertx_enabled,
1971#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001972 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001973 break;
1974 case PARTITION_VERT_A:
1975 decode_block(pbi, xd,
1976#if CONFIG_SUPERTX
1977 supertx_enabled,
1978#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001979 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001980 decode_block(pbi, xd,
1981#if CONFIG_SUPERTX
1982 supertx_enabled,
1983#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001984 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001985 decode_block(pbi, xd,
1986#if CONFIG_SUPERTX
1987 supertx_enabled,
1988#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001989 mi_row, mi_col + hbs, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001990 break;
1991 case PARTITION_VERT_B:
1992 decode_block(pbi, xd,
1993#if CONFIG_SUPERTX
1994 supertx_enabled,
1995#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001996 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001997 decode_block(pbi, xd,
1998#if CONFIG_SUPERTX
1999 supertx_enabled,
2000#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002001 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002002 decode_block(pbi, xd,
2003#if CONFIG_SUPERTX
2004 supertx_enabled,
2005#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08002006 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002007 break;
2008#endif
2009 default: assert(0 && "Invalid partition type");
2010 }
2011 }
2012
2013#if CONFIG_SUPERTX
2014 if (supertx_enabled && read_token) {
2015 uint8_t *dst_buf[3];
2016 int dst_stride[3], i;
2017 int offset = mi_row * cm->mi_stride + mi_col;
2018
2019 set_segment_id_supertx(cm, mi_row, mi_col, bsize);
2020
2021 xd->mi = cm->mi_grid_visible + offset;
2022 xd->mi[0] = cm->mi + offset;
Jingning Han5b7706a2016-12-21 09:55:10 -08002023 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
2024 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002025 set_skip_context(xd, mi_row, mi_col);
2026 skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
2027 if (skip) {
2028 reset_skip_context(xd, bsize);
2029 } else {
2030#if CONFIG_EXT_TX
2031 if (get_ext_tx_types(supertx_size, bsize, 1) > 1) {
2032 int eset = get_ext_tx_set(supertx_size, bsize, 1);
2033 if (eset > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002034 txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
Michael Bebenita6048d052016-08-25 14:40:54 -07002035 cm->fc->inter_ext_tx_prob[eset][supertx_size],
2036 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002037 if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
2038 }
2039 }
2040#else
2041 if (supertx_size < TX_32X32) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002042 txfm = aom_read_tree(r, av1_ext_tx_tree,
Jingning Han8f6eb182016-10-19 13:48:57 -07002043 cm->fc->inter_ext_tx_prob[supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002044 if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
2045 }
2046#endif // CONFIG_EXT_TX
2047 }
2048
Yaowu Xuf883b422016-08-30 14:01:10 -07002049 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002050 for (i = 0; i < MAX_MB_PLANE; i++) {
2051 dst_buf[i] = xd->plane[i].dst.buf;
2052 dst_stride[i] = xd->plane[i].dst.stride;
2053 }
2054 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
2055 bsize, dst_buf, dst_stride);
2056
2057 if (!skip) {
2058 int eobtotal = 0;
2059 MB_MODE_INFO *mbmi;
2060 set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
2061 mbmi = &xd->mi[0]->mbmi;
2062 mbmi->tx_type = txfm;
2063 assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
2064 for (i = 0; i < MAX_MB_PLANE; ++i) {
2065 const struct macroblockd_plane *const pd = &xd->plane[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002066 int row, col;
Debargha Mukherjee2f123402016-08-30 17:43:38 -07002067 const TX_SIZE tx_size = i ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han5b7706a2016-12-21 09:55:10 -08002068 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Jingning Han32b20282016-10-28 15:42:44 -07002069 const int stepr = tx_size_high_unit[tx_size];
2070 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han5b7706a2016-12-21 09:55:10 -08002071 const int max_blocks_wide = max_block_wide(xd, plane_bsize, i);
2072 const int max_blocks_high = max_block_high(xd, plane_bsize, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002073
2074 for (row = 0; row < max_blocks_high; row += stepr)
2075 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07002076 eobtotal += reconstruct_inter_block(
2077 cm, xd, r, mbmi->segment_id_supertx, i, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002078 }
Jingning Han24f24a52016-12-27 10:13:28 -08002079 if ((unify_bsize || !(subsize < BLOCK_8X8)) && eobtotal == 0) skip = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002080 }
2081 set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
2082 }
2083#endif // CONFIG_SUPERTX
2084
2085#if CONFIG_EXT_PARTITION_TYPES
2086 if (bsize >= BLOCK_8X8) {
2087 switch (partition) {
2088 case PARTITION_SPLIT:
2089 if (bsize > BLOCK_8X8) break;
2090 case PARTITION_NONE:
2091 case PARTITION_HORZ:
2092 case PARTITION_VERT:
2093 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
2094 break;
2095 case PARTITION_HORZ_A:
2096 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2097 update_partition_context(xd, mi_row + hbs, mi_col, subsize, subsize);
2098 break;
2099 case PARTITION_HORZ_B:
2100 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2101 update_partition_context(xd, mi_row + hbs, mi_col, bsize2, subsize);
2102 break;
2103 case PARTITION_VERT_A:
2104 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2105 update_partition_context(xd, mi_row, mi_col + hbs, subsize, subsize);
2106 break;
2107 case PARTITION_VERT_B:
2108 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2109 update_partition_context(xd, mi_row, mi_col + hbs, bsize2, subsize);
2110 break;
2111 default: assert(0 && "Invalid partition type");
2112 }
2113 }
2114#else
2115 // update partition context
2116 if (bsize >= BLOCK_8X8 &&
2117 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
Jingning Han1beb0102016-12-07 11:08:30 -08002118 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
David Barkerf8935c92016-10-26 14:54:06 +01002119#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xud71be782016-10-14 08:47:03 -07002120
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002121#if CONFIG_DERING
David Barker9739f362016-11-10 09:29:32 +00002122#if CONFIG_EXT_PARTITION
2123 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128) {
2124 if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
2125 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2126 aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
2127 } else {
2128 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2129 0;
2130 }
2131 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64) {
2132#else
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002133 if (bsize == BLOCK_64X64) {
David Barker9739f362016-11-10 09:29:32 +00002134#endif
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002135 if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
2136 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2137 aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
2138 } else {
2139 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2140 0;
2141 }
2142 }
2143#endif
2144
Yaowu Xud71be782016-10-14 08:47:03 -07002145#if CONFIG_CLPF
David Barker9739f362016-11-10 09:29:32 +00002146#if CONFIG_EXT_PARTITION
2147 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128 &&
2148 cm->clpf_strength_y && cm->clpf_size != CLPF_NOSIZE) {
2149 const int tl = mi_row * MI_SIZE / MIN_FB_SIZE * cm->clpf_stride +
2150 mi_col * MI_SIZE / MIN_FB_SIZE;
2151 if (cm->clpf_size == CLPF_128X128) {
2152 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
2153 } else if (cm->clpf_size == CLPF_64X64) {
2154 const int tr = tl + 2;
2155 const int bl = tl + 2 * cm->clpf_stride;
2156 const int br = tr + 2 * cm->clpf_stride;
2157 const int size = 64 / MI_SIZE;
2158
2159 // Up to four bits per SB
2160 if (!clpf_all_skip(cm, mi_col, mi_row, size))
2161 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
2162
2163 if (mi_col + size < cm->mi_cols &&
2164 !clpf_all_skip(cm, mi_col + size, mi_row, size))
2165 cm->clpf_blocks[tr] = aom_read_literal(r, 1, ACCT_STR);
2166
2167 if (mi_row + size < cm->mi_rows &&
2168 !clpf_all_skip(cm, mi_col, mi_row + size, size))
2169 cm->clpf_blocks[bl] = aom_read_literal(r, 1, ACCT_STR);
2170
2171 if (mi_col + size < cm->mi_cols && mi_row + size < cm->mi_rows &&
2172 !clpf_all_skip(cm, mi_col + size, mi_row + size, size))
2173 cm->clpf_blocks[br] = aom_read_literal(r, 1, ACCT_STR);
2174 } else if (cm->clpf_size == CLPF_32X32) {
2175 int i, j;
2176 const int size = 32 / MI_SIZE;
2177 for (i = 0; i < 4; ++i)
2178 for (j = 0; j < 4; ++j) {
2179 const int index = tl + i * cm->clpf_stride + j;
2180 if (mi_row + i * size < cm->mi_rows &&
2181 mi_col + j * size < cm->mi_cols &&
2182 !clpf_all_skip(cm, mi_col + j * size, mi_row + i * size, size))
2183 cm->clpf_blocks[index] = aom_read_literal(r, 1, ACCT_STR);
2184 }
2185 }
2186 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64 &&
2187#else
2188 if (bsize == BLOCK_64X64 &&
2189#endif // CONFIG_EXT_PARTITION
2190 cm->clpf_strength_y && cm->clpf_size != CLPF_NOSIZE) {
Yaowu Xud71be782016-10-14 08:47:03 -07002191 const int tl = mi_row * MI_SIZE / MIN_FB_SIZE * cm->clpf_stride +
2192 mi_col * MI_SIZE / MIN_FB_SIZE;
2193
2194 if (!((mi_row * MI_SIZE) & 127) && !((mi_col * MI_SIZE) & 127) &&
2195 cm->clpf_size == CLPF_128X128) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002196 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002197 } else if (cm->clpf_size == CLPF_64X64 &&
2198 !clpf_all_skip(cm, mi_col, mi_row, 64 / MI_SIZE)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002199 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002200 } else if (cm->clpf_size == CLPF_32X32) {
2201 const int tr = tl + 1;
2202 const int bl = tl + cm->clpf_stride;
2203 const int br = tr + cm->clpf_stride;
2204 const int size = 32 / MI_SIZE;
2205
2206 // Up to four bits per SB
2207 if (!clpf_all_skip(cm, mi_col, mi_row, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002208 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002209
2210 if (mi_col + size < cm->mi_cols &&
2211 !clpf_all_skip(cm, mi_col + size, mi_row, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002212 cm->clpf_blocks[tr] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002213
2214 if (mi_row + size < cm->mi_rows &&
2215 !clpf_all_skip(cm, mi_col, mi_row + size, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002216 cm->clpf_blocks[bl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002217
2218 if (mi_col + size < cm->mi_cols && mi_row + size < cm->mi_rows &&
2219 !clpf_all_skip(cm, mi_col + size, mi_row + size, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002220 cm->clpf_blocks[br] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002221 }
2222 }
David Barker9739f362016-11-10 09:29:32 +00002223#endif // CONFIG_CLPF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002224}
2225
Yaowu Xuc27fc142016-08-22 16:08:15 -07002226static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
2227 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002228 struct aom_internal_error_info *error_info,
2229 aom_reader *r, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002230 void *decrypt_state) {
2231 // Validate the calculated partition length. If the buffer
2232 // described by the partition can't be fully read, then restrict
2233 // it to the portion that can be (for EC mode) or throw an error.
2234 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002235 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002236 "Truncated packet or corrupt tile length");
2237
Alex Converse2cdf0d82016-12-13 13:53:09 -08002238#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converse346440b2017-01-03 13:47:37 -08002239 r->window_size = ANS_MAX_SYMBOLS;
Alex Converse2cdf0d82016-12-13 13:53:09 -08002240#endif
Alex Converse346440b2017-01-03 13:47:37 -08002241 if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07002242 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002243 "Failed to allocate bool decoder %d", 1);
2244}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002245
Yushin Cho77bba8d2016-11-04 16:36:56 -07002246#if !CONFIG_PVQ
Yaowu Xuf883b422016-08-30 14:01:10 -07002247static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
2248 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002249 int i, j, k, l, m;
Thomas9ac55082016-09-23 18:04:17 +01002250#if CONFIG_EC_ADAPT
Thomas Davies09ebbfb2016-10-20 18:28:47 +01002251 const int node_limit = UNCONSTRAINED_NODES - 1;
Thomas9ac55082016-09-23 18:04:17 +01002252#else
2253 const int node_limit = UNCONSTRAINED_NODES;
2254#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002255
Michael Bebenita6048d052016-08-25 14:40:54 -07002256 if (aom_read_bit(r, ACCT_STR))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002257 for (i = 0; i < PLANE_TYPES; ++i)
2258 for (j = 0; j < REF_TYPES; ++j)
2259 for (k = 0; k < COEF_BANDS; ++k)
2260 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
Thomas9ac55082016-09-23 18:04:17 +01002261 for (m = 0; m < node_limit; ++m)
Michael Bebenita6048d052016-08-25 14:40:54 -07002262 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002263}
2264
Yaowu Xuf883b422016-08-30 14:01:10 -07002265static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002266 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
2267 TX_SIZE tx_size;
Jingning Han83630632016-12-16 11:27:25 -08002268 for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002269 read_coef_probs_common(fc->coef_probs[tx_size], r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002270}
Yushin Cho77bba8d2016-11-04 16:36:56 -07002271#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002272
Yaowu Xuf883b422016-08-30 14:01:10 -07002273static void setup_segmentation(AV1_COMMON *const cm,
2274 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002275 struct segmentation *const seg = &cm->seg;
2276 int i, j;
2277
2278 seg->update_map = 0;
2279 seg->update_data = 0;
2280
Yaowu Xuf883b422016-08-30 14:01:10 -07002281 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002282 if (!seg->enabled) return;
2283
2284 // Segmentation map update
2285 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2286 seg->update_map = 1;
2287 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002288 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002289 }
2290 if (seg->update_map) {
2291 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2292 seg->temporal_update = 0;
2293 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002294 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002295 }
2296 }
2297
2298 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07002299 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002300 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002301 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002302
Yaowu Xuf883b422016-08-30 14:01:10 -07002303 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002304
2305 for (i = 0; i < MAX_SEGMENTS; i++) {
2306 for (j = 0; j < SEG_LVL_MAX; j++) {
2307 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002308 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002309 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002310 av1_enable_segfeature(seg, i, j);
2311 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
2312 if (av1_is_segfeature_signed(j))
2313 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002314 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002315 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002316 }
2317 }
2318 }
2319}
2320
2321#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002322static void decode_restoration_mode(AV1_COMMON *cm,
2323 struct aom_read_bit_buffer *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002324 int p;
2325 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002326 if (aom_rb_read_bit(rb)) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002327 if (aom_rb_read_bit(rb))
2328 rsi->frame_restoration_type =
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002329 (aom_rb_read_bit(rb) ? RESTORE_DOMAINTXFMRF : RESTORE_SGRPROJ);
Debargha Mukherjee0e67b252016-12-08 09:22:44 -08002330 else
2331 rsi->frame_restoration_type = RESTORE_WIENER;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002332 } else {
2333 rsi->frame_restoration_type =
2334 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
2335 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002336 for (p = 1; p < MAX_MB_PLANE; ++p) {
2337 cm->rst_info[p].frame_restoration_type =
2338 aom_rb_read_bit(rb) ? RESTORE_WIENER : RESTORE_NONE;
2339 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002340}
2341
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002342static void read_wiener_filter(WienerInfo *wiener_info, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002343 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002344 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2345 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002346 wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002347 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2348 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002349 wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002350 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2351 WIENER_FILT_TAP2_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002352 wiener_info->vfilter[WIENER_HALFWIN] =
2353 WIENER_FILT_STEP -
2354 2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
2355 wiener_info->vfilter[2]);
2356 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002357 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2358 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002359 wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002360 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2361 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002362 wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002363 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2364 WIENER_FILT_TAP2_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002365 wiener_info->hfilter[WIENER_HALFWIN] =
2366 WIENER_FILT_STEP -
2367 2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
2368 wiener_info->hfilter[2]);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002369}
2370
2371static void read_sgrproj_filter(SgrprojInfo *sgrproj_info, aom_reader *rb) {
2372 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
2373 sgrproj_info->xqd[0] =
2374 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN0;
2375 sgrproj_info->xqd[1] =
2376 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN1;
2377}
2378
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002379static void read_domaintxfmrf_filter(DomaintxfmrfInfo *domaintxfmrf_info,
2380 aom_reader *rb) {
2381 domaintxfmrf_info->sigma_r =
2382 aom_read_literal(rb, DOMAINTXFMRF_PARAMS_BITS, ACCT_STR);
2383}
2384
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002385static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002386 int i, p;
clang-formatbda8d612016-09-19 15:55:46 -07002387 const int ntiles =
2388 av1_get_rest_ntiles(cm->width, cm->height, NULL, NULL, NULL, NULL);
Debargha Mukherjeed7489142017-01-05 13:58:16 -08002389 const int ntiles_uv = av1_get_rest_ntiles(cm->width >> cm->subsampling_x,
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08002390 cm->height >> cm->subsampling_y,
Debargha Mukherjeed7489142017-01-05 13:58:16 -08002391 NULL, NULL, NULL, NULL);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002392 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002393 if (rsi->frame_restoration_type != RESTORE_NONE) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002394 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002395 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002396 rsi->restoration_type[i] =
2397 aom_read_tree(rb, av1_switchable_restore_tree,
2398 cm->fc->switchable_restore_prob, ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002399 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002400 read_wiener_filter(&rsi->wiener_info[i], rb);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002401 } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002402 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002403 } else if (rsi->restoration_type[i] == RESTORE_DOMAINTXFMRF) {
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002404 read_domaintxfmrf_filter(&rsi->domaintxfmrf_info[i], rb);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002405 }
2406 }
2407 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002408 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002409 if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002410 rsi->restoration_type[i] = RESTORE_WIENER;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002411 read_wiener_filter(&rsi->wiener_info[i], rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002412 } else {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002413 rsi->restoration_type[i] = RESTORE_NONE;
2414 }
2415 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002416 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002417 for (i = 0; i < ntiles; ++i) {
2418 if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
2419 rsi->restoration_type[i] = RESTORE_SGRPROJ;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002420 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
2421 } else {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002422 rsi->restoration_type[i] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002423 }
2424 }
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002425 } else if (rsi->frame_restoration_type == RESTORE_DOMAINTXFMRF) {
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002426 for (i = 0; i < ntiles; ++i) {
2427 if (aom_read(rb, RESTORE_NONE_DOMAINTXFMRF_PROB, ACCT_STR)) {
2428 rsi->restoration_type[i] = RESTORE_DOMAINTXFMRF;
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002429 read_domaintxfmrf_filter(&rsi->domaintxfmrf_info[i], rb);
2430 } else {
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002431 rsi->restoration_type[i] = RESTORE_NONE;
2432 }
2433 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002434 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002435 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002436 for (p = 1; p < MAX_MB_PLANE; ++p) {
2437 rsi = &cm->rst_info[p];
2438 if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08002439 for (i = 0; i < ntiles_uv; ++i) {
2440 if (ntiles_uv > 1)
2441 rsi->restoration_type[i] =
2442 aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR) ? RESTORE_WIENER
2443 : RESTORE_NONE;
2444 else
2445 rsi->restoration_type[i] = RESTORE_WIENER;
2446 if (rsi->restoration_type[i] == RESTORE_WIENER) {
2447 read_wiener_filter(&rsi->wiener_info[i], rb);
2448 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002449 }
2450 }
2451 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002452}
2453#endif // CONFIG_LOOP_RESTORATION
2454
Yaowu Xuf883b422016-08-30 14:01:10 -07002455static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002456 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07002457 lf->filter_level = aom_rb_read_literal(rb, 6);
2458 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002459
2460 // Read in loop filter deltas applied at the MB level based on mode or ref
2461 // frame.
2462 lf->mode_ref_delta_update = 0;
2463
Yaowu Xuf883b422016-08-30 14:01:10 -07002464 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002465 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002466 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002467 if (lf->mode_ref_delta_update) {
2468 int i;
2469
2470 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002471 if (aom_rb_read_bit(rb))
2472 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002473
2474 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002475 if (aom_rb_read_bit(rb))
2476 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002477 }
2478 }
2479}
2480
2481#if CONFIG_CLPF
Yaowu Xud71be782016-10-14 08:47:03 -07002482static void setup_clpf(AV1Decoder *pbi, struct aom_read_bit_buffer *rb) {
2483 AV1_COMMON *const cm = &pbi->common;
2484 const int width = pbi->cur_buf->buf.y_crop_width;
2485 const int height = pbi->cur_buf->buf.y_crop_height;
2486
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002487 cm->clpf_blocks = 0;
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02002488 cm->clpf_strength_y = aom_rb_read_literal(rb, 2);
2489 cm->clpf_strength_u = aom_rb_read_literal(rb, 2);
2490 cm->clpf_strength_v = aom_rb_read_literal(rb, 2);
2491 if (cm->clpf_strength_y) {
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002492 cm->clpf_size = aom_rb_read_literal(rb, 2);
Yaowu Xud71be782016-10-14 08:47:03 -07002493 if (cm->clpf_size != CLPF_NOSIZE) {
2494 int size;
2495 cm->clpf_stride =
2496 ((width + MIN_FB_SIZE - 1) & ~(MIN_FB_SIZE - 1)) >> MIN_FB_SIZE_LOG2;
2497 size =
2498 cm->clpf_stride * ((height + MIN_FB_SIZE - 1) & ~(MIN_FB_SIZE - 1)) >>
2499 MIN_FB_SIZE_LOG2;
2500 CHECK_MEM_ERROR(cm, cm->clpf_blocks, aom_malloc(size));
2501 memset(cm->clpf_blocks, -1, size);
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002502 }
2503 }
2504}
2505
Steinar Midtskogen2fd70ee2016-09-02 10:02:30 +02002506static int clpf_bit(UNUSED int k, UNUSED int l,
2507 UNUSED const YV12_BUFFER_CONFIG *rec,
2508 UNUSED const YV12_BUFFER_CONFIG *org,
2509 UNUSED const AV1_COMMON *cm, UNUSED int block_size,
2510 UNUSED int w, UNUSED int h, UNUSED unsigned int strength,
Yaowu Xud71be782016-10-14 08:47:03 -07002511 UNUSED unsigned int fb_size_log2, int8_t *bit) {
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002512 return *bit;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002513}
2514#endif
2515
2516#if CONFIG_DERING
Yaowu Xuf883b422016-08-30 14:01:10 -07002517static void setup_dering(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
2518 cm->dering_level = aom_rb_read_literal(rb, DERING_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002519}
2520#endif // CONFIG_DERING
2521
Yaowu Xuf883b422016-08-30 14:01:10 -07002522static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2523 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002524}
2525
Yaowu Xuf883b422016-08-30 14:01:10 -07002526static void setup_quantization(AV1_COMMON *const cm,
2527 struct aom_read_bit_buffer *rb) {
2528 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002529 cm->y_dc_delta_q = read_delta_q(rb);
2530 cm->uv_dc_delta_q = read_delta_q(rb);
2531 cm->uv_ac_delta_q = read_delta_q(rb);
2532 cm->dequant_bit_depth = cm->bit_depth;
2533#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002534 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002535 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002536 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2537 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002538 } else {
2539 cm->min_qmlevel = 0;
2540 cm->max_qmlevel = 0;
2541 }
2542#endif
2543}
2544
Yaowu Xuf883b422016-08-30 14:01:10 -07002545static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002546 // Build y/uv dequant values based on segmentation.
2547 int i = 0;
2548#if CONFIG_AOM_QM
2549 int lossless;
2550 int j = 0;
2551 int qmlevel;
2552 int using_qm = cm->using_qmatrix;
2553 int minqm = cm->min_qmlevel;
2554 int maxqm = cm->max_qmlevel;
2555#endif
2556#if CONFIG_NEW_QUANT
2557 int b;
2558 int dq;
2559#endif // CONFIG_NEW_QUANT
2560 if (cm->seg.enabled) {
2561 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002562 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002563 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002564 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2565 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002566 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002567 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002568 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002569 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002570#if CONFIG_AOM_QM
2571 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2572 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2573 // NB: depends on base index so there is only 1 set per frame
2574 // No quant weighting when lossless or signalled not using QM
2575 qmlevel = (lossless || using_qm == 0)
2576 ? NUM_QM_LEVELS - 1
2577 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2578 for (j = 0; j < TX_SIZES; ++j) {
2579 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2580 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2581 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2582 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2583 }
2584#endif // CONFIG_AOM_QM
2585#if CONFIG_NEW_QUANT
2586 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2587 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002588 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002589 cm->y_dequant_nuq[i][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002590 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002591 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002592 }
2593 }
2594#endif // CONFIG_NEW_QUANT
2595 }
2596 } else {
2597 const int qindex = cm->base_qindex;
2598 // When segmentation is disabled, only the first value is used. The
2599 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002600 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2601 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002602 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002603 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002604 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002605 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002606#if CONFIG_AOM_QM
2607 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2608 cm->uv_ac_delta_q == 0;
2609 // No quant weighting when lossless or signalled not using QM
2610 qmlevel = (lossless || using_qm == 0)
2611 ? NUM_QM_LEVELS - 1
2612 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2613 for (j = 0; j < TX_SIZES; ++j) {
2614 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2615 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2616 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2617 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2618 }
2619#endif
2620#if CONFIG_NEW_QUANT
2621 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2622 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002623 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002624 cm->y_dequant_nuq[0][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002625 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002626 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002627 }
2628 }
2629#endif // CONFIG_NEW_QUANT
2630 }
2631}
2632
Angie Chiang5678ad92016-11-21 09:38:40 -08002633static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002634 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07002635 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002636}
2637
Yaowu Xuf883b422016-08-30 14:01:10 -07002638static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002639 cm->render_width = cm->width;
2640 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002641 if (aom_rb_read_bit(rb))
2642 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002643}
2644
Yaowu Xuf883b422016-08-30 14:01:10 -07002645static void resize_mv_buffer(AV1_COMMON *cm) {
2646 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002647 cm->cur_frame->mi_rows = cm->mi_rows;
2648 cm->cur_frame->mi_cols = cm->mi_cols;
2649 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002650 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002651 sizeof(*cm->cur_frame->mvs)));
2652}
2653
Yaowu Xuf883b422016-08-30 14:01:10 -07002654static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002655#if CONFIG_SIZE_LIMIT
2656 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002657 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002658 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2659 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2660#endif
2661 if (cm->width != width || cm->height != height) {
2662 const int new_mi_rows =
2663 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2664 const int new_mi_cols =
2665 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2666
Yaowu Xuf883b422016-08-30 14:01:10 -07002667 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002668 // dimensions as well as the overall size.
2669 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002670 if (av1_alloc_context_buffers(cm, width, height))
2671 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002672 "Failed to allocate context buffers");
2673 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002674 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002675 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002676 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002677 cm->width = width;
2678 cm->height = height;
2679 }
2680 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2681 cm->mi_cols > cm->cur_frame->mi_cols) {
2682 resize_mv_buffer(cm);
2683 }
2684}
2685
Yaowu Xuf883b422016-08-30 14:01:10 -07002686static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002687 int width, height;
2688 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002689 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002690 resize_context_buffers(cm, width, height);
2691 setup_render_size(cm, rb);
2692
2693 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002694 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002695 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2696 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002697#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002698 cm->use_highbitdepth,
2699#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002700 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002701 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2702 pool->cb_priv)) {
2703 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002704 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002705 "Failed to allocate frame buffer");
2706 }
2707 unlock_buffer_pool(pool);
2708
2709 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2710 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2711 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2712 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2713 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2714 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2715 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2716}
2717
Yaowu Xuf883b422016-08-30 14:01:10 -07002718static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002719 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002720 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002721 int this_xss, int this_yss) {
2722 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2723 ref_yss == this_yss;
2724}
2725
Yaowu Xuf883b422016-08-30 14:01:10 -07002726static void setup_frame_size_with_refs(AV1_COMMON *cm,
2727 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002728 int width, height;
2729 int found = 0, i;
2730 int has_valid_ref_frame = 0;
2731 BufferPool *const pool = cm->buffer_pool;
2732 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002733 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002734 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2735 width = buf->y_crop_width;
2736 height = buf->y_crop_height;
2737 cm->render_width = buf->render_width;
2738 cm->render_height = buf->render_height;
2739 found = 1;
2740 break;
2741 }
2742 }
2743
2744 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002745 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002746 setup_render_size(cm, rb);
2747 }
2748
2749 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002750 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002751 "Invalid frame size");
2752
2753 // Check to make sure at least one of frames that this frame references
2754 // has valid dimensions.
2755 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2756 RefBuffer *const ref_frame = &cm->frame_refs[i];
2757 has_valid_ref_frame |=
2758 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2759 ref_frame->buf->y_crop_height, width, height);
2760 }
2761 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002762 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002763 "Referenced frame has invalid size");
2764 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2765 RefBuffer *const ref_frame = &cm->frame_refs[i];
2766 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
2767 ref_frame->buf->subsampling_x,
2768 ref_frame->buf->subsampling_y, cm->bit_depth,
2769 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07002770 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002771 "Referenced frame has incompatible color format");
2772 }
2773
2774 resize_context_buffers(cm, width, height);
2775
2776 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002777 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002778 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2779 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002780#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002781 cm->use_highbitdepth,
2782#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002783 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002784 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2785 pool->cb_priv)) {
2786 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002787 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002788 "Failed to allocate frame buffer");
2789 }
2790 unlock_buffer_pool(pool);
2791
2792 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2793 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2794 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2795 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2796 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2797 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2798 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2799}
2800
Yaowu Xuf883b422016-08-30 14:01:10 -07002801static void read_tile_info(AV1Decoder *const pbi,
2802 struct aom_read_bit_buffer *const rb) {
2803 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002804#if CONFIG_EXT_TILE
2805// Read the tile width/height
2806#if CONFIG_EXT_PARTITION
2807 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002808 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
2809 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002810 } else
2811#endif // CONFIG_EXT_PARTITION
2812 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002813 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
2814 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002815 }
2816
Ryan Lei7386eda2016-12-08 21:08:31 -08002817#if CONFIG_DEBLOCKING_ACROSS_TILES
2818 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
2819#endif // CONFIG_DEBLOCKING_ACROSS_TILES
2820
Yaowu Xuc27fc142016-08-22 16:08:15 -07002821 cm->tile_width <<= cm->mib_size_log2;
2822 cm->tile_height <<= cm->mib_size_log2;
2823
Yaowu Xuf883b422016-08-30 14:01:10 -07002824 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
2825 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002826
2827 // Get the number of tiles
2828 cm->tile_cols = 1;
2829 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
2830
2831 cm->tile_rows = 1;
2832 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
2833
2834 if (cm->tile_cols * cm->tile_rows > 1) {
2835 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07002836 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2837 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002838 }
2839#else
2840 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07002841 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002842
2843 // columns
2844 max_ones = max_log2_tile_cols - min_log2_tile_cols;
2845 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07002846 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002847
2848 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07002849 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002850 "Invalid number of tile columns");
2851
2852 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07002853 cm->log2_tile_rows = aom_rb_read_bit(rb);
2854 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002855
Ryan Lei7386eda2016-12-08 21:08:31 -08002856#if CONFIG_DEBLOCKING_ACROSS_TILES
2857 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
2858#endif // CONFIG_DEBLOCKING_ACROSS_TILES
2859
Yaowu Xuc27fc142016-08-22 16:08:15 -07002860 cm->tile_cols = 1 << cm->log2_tile_cols;
2861 cm->tile_rows = 1 << cm->log2_tile_rows;
2862
2863 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
2864 cm->tile_width >>= cm->log2_tile_cols;
2865 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
2866 cm->tile_height >>= cm->log2_tile_rows;
2867
2868 // round to integer multiples of superblock size
2869 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
2870 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
2871
Thomas Davies4974e522016-11-07 17:44:05 +00002872// tile size magnitude
2873#if !CONFIG_TILE_GROUPS
2874 if (cm->tile_rows > 1 || cm->tile_cols > 1)
2875#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002876 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002877#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00002878
Thomas Davies80188d12016-10-26 16:08:35 -07002879#if CONFIG_TILE_GROUPS
2880 // Store an index to the location of the tile group information
2881 pbi->tg_size_bit_offset = rb->bit_offset;
2882 pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
2883 if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
2884 pbi->tg_start =
2885 aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
2886 pbi->tg_size =
2887 1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
2888 }
2889#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002890}
2891
2892static int mem_get_varsize(const uint8_t *src, const int sz) {
2893 switch (sz) {
2894 case 1: return src[0];
2895 case 2: return mem_get_le16(src);
2896 case 3: return mem_get_le24(src);
2897 case 4: return mem_get_le32(src);
2898 default: assert("Invalid size" && 0); return -1;
2899 }
2900}
2901
2902#if CONFIG_EXT_TILE
2903// Reads the next tile returning its size and adjusting '*data' accordingly
2904// based on 'is_last'.
2905static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07002906 struct aom_internal_error_info *error_info,
2907 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002908 void *decrypt_state,
2909 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
2910 int tile_size_bytes, int col, int row) {
2911 size_t size;
2912
2913 size_t copy_size = 0;
2914 const uint8_t *copy_data = NULL;
2915
2916 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002917 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002918 "Truncated packet or corrupt tile length");
2919 if (decrypt_cb) {
2920 uint8_t be_data[4];
2921 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
2922
2923 // Only read number of bytes in cm->tile_size_bytes.
2924 size = mem_get_varsize(be_data, tile_size_bytes);
2925 } else {
2926 size = mem_get_varsize(*data, tile_size_bytes);
2927 }
2928
2929 // The top bit indicates copy mode
2930 if ((size >> (tile_size_bytes * 8 - 1)) == 1) {
2931 // The remaining bits in the top byte signal the row offset
2932 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
2933
2934 // Currently, only use tiles in same column as reference tiles.
2935 copy_data = tile_buffers[row - offset][col].data;
2936 copy_size = tile_buffers[row - offset][col].size;
2937 size = 0;
2938 }
2939
2940 *data += tile_size_bytes;
2941
2942 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07002943 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002944 "Truncated packet or corrupt tile size");
2945
2946 if (size > 0) {
2947 tile_buffers[row][col].data = *data;
2948 tile_buffers[row][col].size = size;
2949 } else {
2950 tile_buffers[row][col].data = copy_data;
2951 tile_buffers[row][col].size = copy_size;
2952 }
2953
2954 *data += size;
2955
2956 tile_buffers[row][col].raw_data_end = *data;
2957}
2958
2959static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07002960 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002961 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002962 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002963 const int tile_cols = cm->tile_cols;
2964 const int tile_rows = cm->tile_rows;
2965 const int have_tiles = tile_cols * tile_rows > 1;
2966
2967 if (!have_tiles) {
2968 const uint32_t tile_size = data_end - data;
2969 tile_buffers[0][0].data = data;
2970 tile_buffers[0][0].size = tile_size;
2971 tile_buffers[0][0].raw_data_end = NULL;
2972 } else {
2973 // We locate only the tile buffers that are required, which are the ones
2974 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
2975 // need the last (bottom right) tile buffer, as we need to know where the
2976 // end of the compressed frame buffer is for proper superframe decoding.
2977
2978 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
2979 const uint8_t *const data_start = data;
2980
Yaowu Xuf883b422016-08-30 14:01:10 -07002981 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002982 const int single_row = pbi->dec_tile_row >= 0;
2983 const int tile_rows_start = single_row ? dec_tile_row : 0;
2984 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07002985 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002986 const int single_col = pbi->dec_tile_col >= 0;
2987 const int tile_cols_start = single_col ? dec_tile_col : 0;
2988 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
2989
2990 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
2991 const int tile_size_bytes = pbi->tile_size_bytes;
2992
2993 size_t tile_col_size;
2994 int r, c;
2995
2996 // Read tile column sizes for all columns (we need the last tile buffer)
2997 for (c = 0; c < tile_cols; ++c) {
2998 const int is_last = c == tile_cols - 1;
2999 if (!is_last) {
3000 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
3001 data += tile_col_size_bytes;
3002 tile_col_data_end[c] = data + tile_col_size;
3003 } else {
3004 tile_col_size = data_end - data;
3005 tile_col_data_end[c] = data_end;
3006 }
3007 data += tile_col_size;
3008 }
3009
3010 data = data_start;
3011
3012 // Read the required tile sizes.
3013 for (c = tile_cols_start; c < tile_cols_end; ++c) {
3014 const int is_last = c == tile_cols - 1;
3015
3016 if (c > 0) data = tile_col_data_end[c - 1];
3017
3018 if (!is_last) data += tile_col_size_bytes;
3019
3020 // Get the whole of the last column, otherwise stop at the required tile.
3021 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
3022 tile_buffers[r][c].col = c;
3023
3024 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3025 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3026 tile_size_bytes, c, r);
3027 }
3028 }
3029
3030 // If we have not read the last column, then read it to get the last tile.
3031 if (tile_cols_end != tile_cols) {
3032 c = tile_cols - 1;
3033
3034 data = tile_col_data_end[c - 1];
3035
3036 for (r = 0; r < tile_rows; ++r) {
3037 tile_buffers[r][c].col = c;
3038
3039 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3040 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3041 tile_size_bytes, c, r);
3042 }
3043 }
3044 }
3045}
3046#else
3047// Reads the next tile returning its size and adjusting '*data' accordingly
3048// based on 'is_last'.
3049static void get_tile_buffer(const uint8_t *const data_end,
3050 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07003051 struct aom_internal_error_info *error_info,
3052 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003053 void *decrypt_state, TileBufferDec *const buf) {
3054 size_t size;
3055
3056 if (!is_last) {
3057 if (!read_is_valid(*data, 4, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003058 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003059 "Truncated packet or corrupt tile length");
3060
3061 if (decrypt_cb) {
3062 uint8_t be_data[4];
3063 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3064 size = mem_get_varsize(be_data, tile_size_bytes);
3065 } else {
3066 size = mem_get_varsize(*data, tile_size_bytes);
3067 }
3068 *data += tile_size_bytes;
3069
3070 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003071 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003072 "Truncated packet or corrupt tile size");
3073 } else {
3074 size = data_end - *data;
3075 }
3076
3077 buf->data = *data;
3078 buf->size = size;
3079
3080 *data += size;
3081}
3082
3083static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003084 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003085 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003086 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07003087#if CONFIG_TILE_GROUPS
3088 int r, c;
3089 const int tile_cols = cm->tile_cols;
3090 const int tile_rows = cm->tile_rows;
3091 int tc = 0;
3092 int first_tile_in_tg = 0;
3093 int hdr_offset;
3094 struct aom_read_bit_buffer rb_tg_hdr;
3095 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3096 const int num_tiles = tile_rows * tile_cols;
3097 const int num_bits = OD_ILOG(num_tiles) - 1;
3098 const int hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
3099 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
3100
3101 for (r = 0; r < tile_rows; ++r) {
3102 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07003103 TileBufferDec *const buf = &tile_buffers[r][c];
3104 hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
3105
3106 buf->col = c;
3107 if (hdr_offset) {
3108 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
3109 rb_tg_hdr.bit_offset = tg_size_bit_offset;
3110 if (num_tiles) {
3111 pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
3112 pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
3113 }
3114 }
3115 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
3116 data += hdr_offset;
Thomas Davies8fe64a32016-10-04 13:19:31 +01003117 get_tile_buffer(data_end, pbi->tile_size_bytes, 0, &pbi->common.error,
3118 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
Thomas Davies80188d12016-10-26 16:08:35 -07003119 }
3120 }
3121#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003122 int r, c;
3123 const int tile_cols = cm->tile_cols;
3124 const int tile_rows = cm->tile_rows;
3125
3126 for (r = 0; r < tile_rows; ++r) {
3127 for (c = 0; c < tile_cols; ++c) {
3128 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
3129 TileBufferDec *const buf = &tile_buffers[r][c];
3130 buf->col = c;
3131 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
3132 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
3133 }
3134 }
Thomas Davies80188d12016-10-26 16:08:35 -07003135#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003136}
3137#endif // CONFIG_EXT_TILE
3138
Yushin Cho77bba8d2016-11-04 16:36:56 -07003139#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003140static void daala_dec_init(AV1_COMMON *const cm, daala_dec_ctx *daala_dec,
Nathan E. Eggeab083972016-12-28 15:31:46 -05003141 aom_reader *r) {
3142 daala_dec->r = r;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003143 od_adapt_ctx_reset(&daala_dec->state.adapt, 0);
3144
Yushin Cho70669122016-12-08 09:53:14 -10003145 // TODO(yushin) : activity masking info needs be signaled by a bitstream
3146 daala_dec->use_activity_masking = AV1_PVQ_ENABLE_ACTIVITY_MASKING;
3147
3148 if (daala_dec->use_activity_masking)
3149 daala_dec->qm = OD_HVS_QM;
3150 else
3151 daala_dec->qm = OD_FLAT_QM;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003152
3153 od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
3154 daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
Yushin Cho70669122016-12-08 09:53:14 -10003155
3156 if (daala_dec->use_activity_masking) {
3157 int pli;
3158 int use_masking = daala_dec->use_activity_masking;
3159 int segment_id = 0;
3160 int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
3161
3162 for (pli = 0; pli < MAX_MB_PLANE; pli++) {
3163 int i;
3164 int q;
3165
3166 q = qindex;
3167 if (q <= OD_DEFAULT_QMS[use_masking][0][pli].interp_q << OD_COEFF_SHIFT) {
3168 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3169 &OD_DEFAULT_QMS[use_masking][0][pli], NULL);
3170 } else {
3171 i = 0;
3172 while (OD_DEFAULT_QMS[use_masking][i + 1][pli].qm_q4 != NULL &&
3173 q > OD_DEFAULT_QMS[use_masking][i + 1][pli].interp_q
3174 << OD_COEFF_SHIFT) {
3175 i++;
3176 }
3177 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3178 &OD_DEFAULT_QMS[use_masking][i][pli],
3179 &OD_DEFAULT_QMS[use_masking][i + 1][pli]);
3180 }
3181 }
3182 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07003183}
Yushin Cho70669122016-12-08 09:53:14 -10003184#endif // #if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003185
Yaowu Xuf883b422016-08-30 14:01:10 -07003186static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003187 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003188 AV1_COMMON *const cm = &pbi->common;
3189 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003190 const int tile_cols = cm->tile_cols;
3191 const int tile_rows = cm->tile_rows;
3192 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07003193 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003194#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003195 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003196 const int single_row = pbi->dec_tile_row >= 0;
3197 const int tile_rows_start = single_row ? dec_tile_row : 0;
3198 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003199 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003200 const int single_col = pbi->dec_tile_col >= 0;
3201 const int tile_cols_start = single_col ? dec_tile_col : 0;
3202 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3203 const int inv_col_order = pbi->inv_tile_order && !single_col;
3204 const int inv_row_order = pbi->inv_tile_order && !single_row;
3205#else
3206 const int tile_rows_start = 0;
3207 const int tile_rows_end = tile_rows;
3208 const int tile_cols_start = 0;
3209 const int tile_cols_end = tile_cols;
3210 const int inv_col_order = pbi->inv_tile_order;
3211 const int inv_row_order = pbi->inv_tile_order;
3212#endif // CONFIG_EXT_TILE
3213 int tile_row, tile_col;
3214
3215#if CONFIG_ENTROPY
3216 cm->do_subframe_update = n_tiles == 1;
3217#endif // CONFIG_ENTROPY
3218
3219 if (cm->lf.filter_level && !cm->skip_loop_filter &&
3220 pbi->lf_worker.data1 == NULL) {
3221 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07003222 aom_memalign(32, sizeof(LFWorkerData)));
3223 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003224 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003225 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003226 "Loop filter thread creation failed");
3227 }
3228 }
3229
3230 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3231 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3232 // Be sure to sync as we might be resuming after a failed frame decode.
3233 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003234 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
3235 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003236 }
3237
3238 assert(tile_rows <= MAX_TILE_ROWS);
3239 assert(tile_cols <= MAX_TILE_COLS);
3240
3241 get_tile_buffers(pbi, data, data_end, tile_buffers);
3242
3243 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003244 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003245 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003246 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003247 pbi->allocated_tiles = n_tiles;
3248 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003249#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003250 if (pbi->acct_enabled) {
3251 aom_accounting_reset(&pbi->accounting);
3252 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003253#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003254 // Load all tile information into tile_data.
3255 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3256 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3257 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
3258 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
3259
3260 td->cm = cm;
3261 td->xd = pbi->mb;
3262 td->xd.corrupted = 0;
3263 td->xd.counts =
3264 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3265 ? &cm->counts
3266 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003267 av1_zero(td->dqcoeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003268#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003269 av1_zero(td->pvq_ref_coeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003270#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003271 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003272 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
3273 &td->bit_reader, pbi->decrypt_cb, pbi->decrypt_state);
Michael Bebenita6048d052016-08-25 14:40:54 -07003274#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003275 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003276 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003277 } else {
David Barkerd971f402016-10-25 13:52:07 +01003278 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003279 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003280#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003281 av1_init_macroblockd(cm, &td->xd,
3282#if CONFIG_PVQ
3283 td->pvq_ref_coeff,
3284#endif
3285 td->dqcoeff);
3286#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003287 daala_dec_init(cm, &td->xd.daala_dec, &td->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003288#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003289#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003290 td->xd.plane[0].color_index_map = td->color_index_map[0];
3291 td->xd.plane[1].color_index_map = td->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003292#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003293 }
3294 }
3295
3296 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3297 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
3298 int mi_row = 0;
3299 TileInfo tile_info;
3300
Yaowu Xuf883b422016-08-30 14:01:10 -07003301 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003302
3303 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3304 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
3305 TileData *const td = pbi->tile_data + tile_cols * row + col;
Michael Bebenita6048d052016-08-25 14:40:54 -07003306#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003307 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003308 td->bit_reader.accounting->last_tell_frac =
3309 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003310 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003311#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003312
Yaowu Xuf883b422016-08-30 14:01:10 -07003313 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003314
Yaowu Xuf883b422016-08-30 14:01:10 -07003315 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003316
3317 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
3318 mi_row += cm->mib_size) {
3319 int mi_col;
3320
Yaowu Xuf883b422016-08-30 14:01:10 -07003321 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003322
3323 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
3324 mi_col += cm->mib_size) {
Ryan Lei7386eda2016-12-08 21:08:31 -08003325#if CONFIG_DEBLOCKING_ACROSS_TILES
3326 if (av1_disable_loopfilter_on_tile_boundary(cm)) {
3327 av1_update_tile_boundary_info(cm, &tile_info, mi_row, mi_col);
3328 }
3329#endif // CONFIG_DEBLOCKING_ACROSS_TILES
3330
Yaowu Xuc27fc142016-08-22 16:08:15 -07003331 decode_partition(pbi, &td->xd,
3332#if CONFIG_SUPERTX
3333 0,
3334#endif // CONFIG_SUPERTX
3335 mi_row, mi_col, &td->bit_reader, cm->sb_size,
3336 b_width_log2_lookup[cm->sb_size]);
3337 }
3338 pbi->mb.corrupted |= td->xd.corrupted;
3339 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003340 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003341 "Failed to decode tile data");
3342#if CONFIG_ENTROPY
3343 if (cm->do_subframe_update &&
3344 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3345 if ((mi_row + MI_SIZE) %
3346 (MI_SIZE *
Yaowu Xuf883b422016-08-30 14:01:10 -07003347 AOMMAX(cm->mi_rows / MI_SIZE / COEF_PROBS_BUFS, 1)) ==
Yaowu Xuc27fc142016-08-22 16:08:15 -07003348 0 &&
3349 mi_row + MI_SIZE < cm->mi_rows &&
3350 cm->coef_probs_update_idx < COEF_PROBS_BUFS - 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003351 av1_partial_adapt_probs(cm, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003352 ++cm->coef_probs_update_idx;
3353 }
3354 }
3355#endif // CONFIG_ENTROPY
3356 }
3357 }
3358
3359 assert(mi_row > 0);
3360
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003361// when Parallel deblocking is enabled, deblocking should not
3362// be interleaved with decoding. Instead, deblocking should be done
3363// after the entire frame is decoded.
3364#if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003365 // Loopfilter one tile row.
3366 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3367 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003368 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003369 const int lf_end = tile_info.mi_row_end - cm->mib_size;
3370
3371 // Delay the loopfilter if the first tile row is only
3372 // a single superblock high.
3373 if (lf_end <= 0) continue;
3374
3375 // Decoding has completed. Finish up the loop filter in this thread.
3376 if (tile_info.mi_row_end >= cm->mi_rows) continue;
3377
3378 winterface->sync(&pbi->lf_worker);
3379 lf_data->start = lf_start;
3380 lf_data->stop = lf_end;
3381 if (pbi->max_threads > 1) {
3382 winterface->launch(&pbi->lf_worker);
3383 } else {
3384 winterface->execute(&pbi->lf_worker);
3385 }
3386 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003387#endif // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003388
3389 // After loopfiltering, the last 7 row pixels in each superblock row may
3390 // still be changed by the longest loopfilter of the next superblock row.
3391 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003392 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003393 }
3394
3395#if CONFIG_VAR_TX
3396 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003397 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
3398 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003399#else
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003400#if CONFIG_PARALLEL_DEBLOCKING
3401 // Loopfilter all rows in the frame in the frame.
3402 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3403 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3404 winterface->sync(&pbi->lf_worker);
3405 lf_data->start = 0;
3406 lf_data->stop = cm->mi_rows;
3407 winterface->execute(&pbi->lf_worker);
3408 }
3409#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003410 // Loopfilter remaining rows in the frame.
3411 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3412 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3413 winterface->sync(&pbi->lf_worker);
3414 lf_data->start = lf_data->stop;
3415 lf_data->stop = cm->mi_rows;
3416 winterface->execute(&pbi->lf_worker);
3417 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003418#endif // CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003419#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003420 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003421 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003422
3423#if CONFIG_EXT_TILE
3424 if (n_tiles == 1) {
3425#if CONFIG_ANS
3426 return data_end;
3427#else
3428 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07003429 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003430#endif // CONFIG_ANS
3431 } else {
3432 // Return the end of the last tile buffer
3433 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3434 }
3435#else
3436#if CONFIG_ANS
3437 return data_end;
3438#else
3439 {
3440 // Get last tile data.
3441 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003442 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003443 }
3444#endif // CONFIG_ANS
3445#endif // CONFIG_EXT_TILE
3446}
3447
3448static int tile_worker_hook(TileWorkerData *const tile_data,
3449 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003450 AV1Decoder *const pbi = tile_data->pbi;
3451 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003452 int mi_row, mi_col;
3453
3454 if (setjmp(tile_data->error_info.jmp)) {
3455 tile_data->error_info.setjmp = 0;
3456 tile_data->xd.corrupted = 1;
3457 return 0;
3458 }
3459
3460 tile_data->error_info.setjmp = 1;
3461 tile_data->xd.error_info = &tile_data->error_info;
3462
Yaowu Xuf883b422016-08-30 14:01:10 -07003463 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003464
3465 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
3466 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003467 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003468
3469 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3470 mi_col += cm->mib_size) {
3471 decode_partition(pbi, &tile_data->xd,
3472#if CONFIG_SUPERTX
3473 0,
3474#endif
3475 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
3476 b_width_log2_lookup[cm->sb_size]);
3477 }
3478 }
3479 return !tile_data->xd.corrupted;
3480}
3481
3482// sorts in descending order
3483static int compare_tile_buffers(const void *a, const void *b) {
3484 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
3485 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
3486 return (int)(buf2->size - buf1->size);
3487}
3488
Yaowu Xuf883b422016-08-30 14:01:10 -07003489static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003490 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003491 AV1_COMMON *const cm = &pbi->common;
3492 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003493 const int tile_cols = cm->tile_cols;
3494 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003495 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
clang-format67948d32016-09-07 22:40:40 -07003496 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003497#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003498 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003499 const int single_row = pbi->dec_tile_row >= 0;
3500 const int tile_rows_start = single_row ? dec_tile_row : 0;
3501 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003502 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003503 const int single_col = pbi->dec_tile_col >= 0;
3504 const int tile_cols_start = single_col ? dec_tile_col : 0;
3505 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3506#else
3507 const int tile_rows_start = 0;
3508 const int tile_rows_end = tile_rows;
3509 const int tile_cols_start = 0;
3510 const int tile_cols_end = tile_cols;
3511#endif // CONFIG_EXT_TILE
3512 int tile_row, tile_col;
3513 int i;
3514
3515#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3516 int final_worker = -1;
3517#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3518
3519 assert(tile_rows <= MAX_TILE_ROWS);
3520 assert(tile_cols <= MAX_TILE_COLS);
3521
3522 assert(tile_cols * tile_rows > 1);
3523
Yaowu Xuc27fc142016-08-22 16:08:15 -07003524 // TODO(jzern): See if we can remove the restriction of passing in max
3525 // threads to the decoder.
3526 if (pbi->num_tile_workers == 0) {
3527 const int num_threads = pbi->max_threads & ~1;
3528 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07003529 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003530 // Ensure tile data offsets will be properly aligned. This may fail on
3531 // platforms without DECLARE_ALIGNED().
3532 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3533 CHECK_MEM_ERROR(
3534 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003535 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003536 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07003537 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003538 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003539 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003540 ++pbi->num_tile_workers;
3541
3542 winterface->init(worker);
3543 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003544 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003545 "Tile decoder thread creation failed");
3546 }
3547 }
3548 }
3549
3550 // Reset tile decoding hook
3551 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003552 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003553 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003554 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003555 worker->data1 = &pbi->tile_worker_data[i];
3556 worker->data2 = &pbi->tile_worker_info[i];
3557 }
3558
3559 // Initialize thread frame counts.
3560 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3561 for (i = 0; i < num_workers; ++i) {
3562 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003563 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003564 }
3565 }
3566
3567 // Load tile data into tile_buffers
3568 get_tile_buffers(pbi, data, data_end, tile_buffers);
3569
3570 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3571 // Sort the buffers in this tile row based on size in descending order.
3572 qsort(&tile_buffers[tile_row][tile_cols_start],
3573 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
3574 compare_tile_buffers);
3575
3576 // Rearrange the tile buffers in this tile row such that per-tile group
3577 // the largest, and presumably the most difficult tile will be decoded in
3578 // the main thread. This should help minimize the number of instances
3579 // where the main thread is waiting for a worker to complete.
3580 {
3581 int group_start;
3582 for (group_start = tile_cols_start; group_start < tile_cols_end;
3583 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003584 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003585 const TileBufferDec largest = tile_buffers[tile_row][group_start];
3586 memmove(&tile_buffers[tile_row][group_start],
3587 &tile_buffers[tile_row][group_start + 1],
3588 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
3589 tile_buffers[tile_row][group_end - 1] = largest;
3590 }
3591 }
3592
3593 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
3594 // Launch workers for individual columns
3595 for (i = 0; i < num_workers && tile_col < tile_cols_end;
3596 ++i, ++tile_col) {
3597 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07003598 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003599 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
3600 TileInfo *const tile_info = (TileInfo *)worker->data2;
3601
3602 twd->pbi = pbi;
3603 twd->xd = pbi->mb;
3604 twd->xd.corrupted = 0;
3605 twd->xd.counts =
3606 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3607 ? &twd->counts
3608 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003609 av1_zero(twd->dqcoeff);
3610 av1_tile_init(tile_info, cm, tile_row, buf->col);
3611 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003612 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
3613 &twd->bit_reader, pbi->decrypt_cb,
3614 pbi->decrypt_state);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003615 av1_init_macroblockd(cm, &twd->xd,
3616#if CONFIG_PVQ
3617 twd->pvq_ref_coeff,
3618#endif
3619 twd->dqcoeff);
3620#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003621 daala_dec_init(cm, &twd->xd.daala_dec, &twd->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003622#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003623#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003624 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
3625 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003626#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003627
3628 worker->had_error = 0;
3629 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
3630 winterface->execute(worker);
3631 } else {
3632 winterface->launch(worker);
3633 }
3634
3635#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3636 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
3637 final_worker = i;
3638 }
3639#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3640 }
3641
3642 // Sync all workers
3643 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003644 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003645 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07003646 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07003647 // info in cm. Additionally once the threads have been synced and an
3648 // error is detected, there's no point in continuing to decode tiles.
3649 pbi->mb.corrupted |= !winterface->sync(worker);
3650 }
3651 }
3652 }
3653
3654 // Accumulate thread frame counts.
3655 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3656 for (i = 0; i < num_workers; ++i) {
3657 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003658 av1_accumulate_frame_counts(&cm->counts, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003659 }
3660 }
3661
3662#if CONFIG_EXT_TILE
3663 // Return the end of the last tile buffer
3664 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3665#else
3666#if CONFIG_ANS
3667 return data_end;
3668#else
3669 assert(final_worker != -1);
3670 {
3671 TileWorkerData *const twd =
3672 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003673 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003674 }
3675#endif // CONFIG_ANS
3676#endif // CONFIG_EXT_TILE
3677}
3678
3679static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003680 AV1_COMMON *const cm = (AV1_COMMON *)data;
3681 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003682}
3683
Yaowu Xuf883b422016-08-30 14:01:10 -07003684static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3685 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003686 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003687 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
3688#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003689 cm->use_highbitdepth = 1;
3690#endif
3691 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003692 cm->bit_depth = AOM_BITS_8;
3693#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003694 cm->use_highbitdepth = 0;
3695#endif
3696 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003697 cm->color_space = aom_rb_read_literal(rb, 3);
3698 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003699 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07003700 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003701 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003702 cm->subsampling_x = aom_rb_read_bit(rb);
3703 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003704 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003705 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003706 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07003707 if (aom_rb_read_bit(rb))
3708 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003709 "Reserved bit set");
3710 } else {
3711 cm->subsampling_y = cm->subsampling_x = 1;
3712 }
3713 } else {
3714 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
3715 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
3716 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
3717 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003718 if (aom_rb_read_bit(rb))
3719 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003720 "Reserved bit set");
3721 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003722 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003723 "4:4:4 color not supported in profile 0 or 2");
3724 }
3725 }
3726}
3727
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003728#if CONFIG_REFERENCE_BUFFER
3729void read_sequence_header(SequenceHeader *seq_params) {
3730 /* Placeholder for actually reading from the bitstream */
3731 seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
3732 seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
3733 seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
3734}
3735#endif
3736
Yaowu Xuf883b422016-08-30 14:01:10 -07003737static size_t read_uncompressed_header(AV1Decoder *pbi,
3738 struct aom_read_bit_buffer *rb) {
3739 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003740 MACROBLOCKD *const xd = &pbi->mb;
3741 BufferPool *const pool = cm->buffer_pool;
3742 RefCntBuffer *const frame_bufs = pool->frame_bufs;
3743 int i, mask, ref_index = 0;
3744 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003745
3746#if CONFIG_REFERENCE_BUFFER
3747 /* TODO: Move outside frame loop or inside key-frame branch */
3748 read_sequence_header(&pbi->seq_params);
3749#endif
3750
Yaowu Xuc27fc142016-08-22 16:08:15 -07003751 cm->last_frame_type = cm->frame_type;
3752 cm->last_intra_only = cm->intra_only;
3753
3754#if CONFIG_EXT_REFS
3755 // NOTE: By default all coded frames to be used as a reference
3756 cm->is_reference_frame = 1;
3757#endif // CONFIG_EXT_REFS
3758
Yaowu Xuf883b422016-08-30 14:01:10 -07003759 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
3760 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003761 "Invalid frame marker");
3762
Yaowu Xuf883b422016-08-30 14:01:10 -07003763 cm->profile = av1_read_profile(rb);
3764#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003765 if (cm->profile >= MAX_PROFILES)
Yaowu Xuf883b422016-08-30 14:01:10 -07003766 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003767 "Unsupported bitstream profile");
3768#else
3769 if (cm->profile >= PROFILE_2)
Yaowu Xuf883b422016-08-30 14:01:10 -07003770 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003771 "Unsupported bitstream profile");
3772#endif
3773
Yaowu Xuf883b422016-08-30 14:01:10 -07003774 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003775
3776 if (cm->show_existing_frame) {
Yaowu Xu415ba932016-12-27 11:17:32 -08003777 // Show an existing frame directly.
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003778 const int existing_frame_idx = aom_rb_read_literal(rb, 3);
3779 const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
Yaowu Xu415ba932016-12-27 11:17:32 -08003780#if CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003781 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003782 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3783 int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003784 /* Compare display_frame_id with ref_frame_id and check valid for
3785 * referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003786 if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
3787 cm->valid_for_referencing[existing_frame_idx] == 0)
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003788 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3789 "Reference buffer frame ID mismatch");
3790 }
3791#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003792 lock_buffer_pool(pool);
3793 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
3794 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07003795 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003796 "Buffer %d does not contain a decoded frame",
3797 frame_to_show);
3798 }
3799 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
3800 unlock_buffer_pool(pool);
3801
3802 cm->lf.filter_level = 0;
3803 cm->show_frame = 1;
3804 pbi->refresh_frame_flags = 0;
3805
3806 if (cm->frame_parallel_decode) {
3807 for (i = 0; i < REF_FRAMES; ++i)
3808 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
3809 }
3810
3811 return 0;
3812 }
3813
Yaowu Xuf883b422016-08-30 14:01:10 -07003814 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
3815 cm->show_frame = aom_rb_read_bit(rb);
3816 cm->error_resilient_mode = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003817#if CONFIG_REFERENCE_BUFFER
3818 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003819 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3820 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
3821 int prev_frame_id = 0;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003822 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003823 prev_frame_id = cm->current_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003824 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003825 cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003826
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003827 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003828 int diff_frame_id;
3829 if (cm->current_frame_id > prev_frame_id) {
3830 diff_frame_id = cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003831 } else {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003832 diff_frame_id =
3833 (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003834 }
3835 /* Check current_frame_id for conformance */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003836 if (prev_frame_id == cm->current_frame_id ||
3837 diff_frame_id >= (1 << (frame_id_length - 1))) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003838 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3839 "Invalid value of current_frame_id");
3840 }
3841 }
3842 /* Check if some frames need to be marked as not valid for referencing */
3843 for (i = 0; i < REF_FRAMES; i++) {
3844 if (cm->frame_type == KEY_FRAME) {
3845 cm->valid_for_referencing[i] = 0;
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003846 } else if (cm->current_frame_id - (1 << diff_len) > 0) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003847 if (cm->ref_frame_id[i] > cm->current_frame_id ||
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003848 cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003849 cm->valid_for_referencing[i] = 0;
3850 } else {
3851 if (cm->ref_frame_id[i] > cm->current_frame_id &&
3852 cm->ref_frame_id[i] <
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003853 (1 << frame_id_length) + cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003854 cm->valid_for_referencing[i] = 0;
3855 }
3856 }
3857 }
3858#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003859 if (cm->frame_type == KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003860 if (!av1_read_sync_code(rb))
3861 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003862 "Invalid frame sync code");
3863
3864 read_bitdepth_colorspace_sampling(cm, rb);
3865 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
3866
3867 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3868 cm->frame_refs[i].idx = INVALID_IDX;
3869 cm->frame_refs[i].buf = NULL;
3870 }
3871
3872 setup_frame_size(cm, rb);
3873 if (pbi->need_resync) {
3874 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
3875 pbi->need_resync = 0;
3876 }
Urvang Joshib100db72016-10-12 16:28:56 -07003877#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07003878 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003879#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003880 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003881 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003882#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07003883 if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003884#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003885 if (cm->error_resilient_mode) {
3886 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
3887 } else {
3888 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003889 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003890 ? RESET_FRAME_CONTEXT_ALL
3891 : RESET_FRAME_CONTEXT_CURRENT;
3892 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003893 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003894 ? RESET_FRAME_CONTEXT_CURRENT
3895 : RESET_FRAME_CONTEXT_NONE;
3896 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07003897 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003898 ? RESET_FRAME_CONTEXT_ALL
3899 : RESET_FRAME_CONTEXT_CURRENT;
3900 }
3901 }
3902
3903 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003904 if (!av1_read_sync_code(rb))
3905 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003906 "Invalid frame sync code");
3907
3908 read_bitdepth_colorspace_sampling(cm, rb);
3909
Yaowu Xuf883b422016-08-30 14:01:10 -07003910 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003911 setup_frame_size(cm, rb);
3912 if (pbi->need_resync) {
3913 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
3914 pbi->need_resync = 0;
3915 }
3916 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Yaowu Xuf883b422016-08-30 14:01:10 -07003917 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003918
3919#if CONFIG_EXT_REFS
3920 if (!pbi->refresh_frame_flags) {
3921 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
3922 // will not be used as a reference
3923 cm->is_reference_frame = 0;
3924 }
3925#endif // CONFIG_EXT_REFS
3926
3927 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003928 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003929 const int idx = cm->ref_frame_map[ref];
3930 RefBuffer *const ref_frame = &cm->frame_refs[i];
3931 ref_frame->idx = idx;
3932 ref_frame->buf = &frame_bufs[idx].buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07003933 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003934#if CONFIG_REFERENCE_BUFFER
3935 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003936 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3937 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
3938 int delta_frame_id_minus1 = aom_rb_read_literal(rb, diff_len);
3939 int ref_frame_id =
3940 ((cm->current_frame_id - (delta_frame_id_minus1 + 1) +
3941 (1 << frame_id_length)) %
3942 (1 << frame_id_length));
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003943 /* Compare values derived from delta_frame_id_minus1 and
3944 * refresh_frame_flags. Also, check valid for referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003945 if (ref_frame_id != cm->ref_frame_id[ref] ||
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003946 cm->valid_for_referencing[ref] == 0)
3947 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3948 "Reference buffer frame ID mismatch");
3949 }
3950#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003951 }
3952
Arild Fuldseth842e9b02016-09-02 13:00:05 +02003953#if CONFIG_FRAME_SIZE
3954 if (cm->error_resilient_mode == 0) {
3955 setup_frame_size_with_refs(cm, rb);
3956 } else {
3957 setup_frame_size(cm, rb);
3958 }
3959#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003960 setup_frame_size_with_refs(cm, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02003961#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003962
Yaowu Xuf883b422016-08-30 14:01:10 -07003963 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Angie Chiang5678ad92016-11-21 09:38:40 -08003964 cm->interp_filter = read_frame_interp_filter(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003965
3966 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3967 RefBuffer *const ref_buf = &cm->frame_refs[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07003968#if CONFIG_AOM_HIGHBITDEPTH
3969 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003970 &ref_buf->sf, ref_buf->buf->y_crop_width,
3971 ref_buf->buf->y_crop_height, cm->width, cm->height,
3972 cm->use_highbitdepth);
3973#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003974 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003975 &ref_buf->sf, ref_buf->buf->y_crop_width,
3976 ref_buf->buf->y_crop_height, cm->width, cm->height);
3977#endif
3978 }
3979 }
3980 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003981
3982#if CONFIG_REFERENCE_BUFFER
3983 if (pbi->seq_params.frame_id_numbers_present_flag) {
3984 /* If bitmask is set, update reference frame id values and
3985 mark frames as valid for reference */
3986 int refresh_frame_flags =
3987 cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
3988 for (i = 0; i < REF_FRAMES; i++) {
3989 if ((refresh_frame_flags >> i) & 1) {
3990 cm->ref_frame_id[i] = cm->current_frame_id;
3991 cm->valid_for_referencing[i] = 1;
3992 }
3993 }
3994 }
3995#endif
3996
Yaowu Xuf883b422016-08-30 14:01:10 -07003997#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003998 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
3999#endif
4000 get_frame_new_buffer(cm)->color_space = cm->color_space;
4001 get_frame_new_buffer(cm)->color_range = cm->color_range;
4002 get_frame_new_buffer(cm)->render_width = cm->render_width;
4003 get_frame_new_buffer(cm)->render_height = cm->render_height;
4004
4005 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004006 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004007 "Keyframe / intra-only frame required to reset decoder"
4008 " state");
4009 }
4010
4011 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004012 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004013 ? REFRESH_FRAME_CONTEXT_FORWARD
4014 : REFRESH_FRAME_CONTEXT_BACKWARD;
4015 } else {
4016 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
4017 }
4018
Yaowu Xuf883b422016-08-30 14:01:10 -07004019 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07004020 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07004021 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004022
4023 // Generate next_ref_frame_map.
4024 lock_buffer_pool(pool);
4025 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
4026 if (mask & 1) {
4027 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
4028 ++frame_bufs[cm->new_fb_idx].ref_count;
4029 } else {
4030 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4031 }
4032 // Current thread holds the reference frame.
4033 if (cm->ref_frame_map[ref_index] >= 0)
4034 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4035 ++ref_index;
4036 }
4037
4038 for (; ref_index < REF_FRAMES; ++ref_index) {
4039 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4040
4041 // Current thread holds the reference frame.
4042 if (cm->ref_frame_map[ref_index] >= 0)
4043 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4044 }
4045 unlock_buffer_pool(pool);
4046 pbi->hold_ref_buf = 1;
4047
4048 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07004049 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004050
4051#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07004052 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004053#else
4054 set_sb_size(cm, BLOCK_64X64);
4055#endif // CONFIG_EXT_PARTITION
4056
4057 setup_loopfilter(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004058#if CONFIG_DERING
4059 setup_dering(cm, rb);
4060#endif
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004061#if CONFIG_CLPF
4062 setup_clpf(pbi, rb);
4063#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004064#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee874d36d2016-12-14 16:53:17 -08004065 av1_alloc_restoration_buffers(cm);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004066 decode_restoration_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004067#endif // CONFIG_LOOP_RESTORATION
4068 setup_quantization(cm, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004069#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004070 xd->bd = (int)cm->bit_depth;
4071#endif
4072
4073#if CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004074 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004075 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4076 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4077 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4078 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
4079 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4080 }
4081#endif // CONFIG_ENTROPY
4082
4083 setup_segmentation(cm, rb);
4084
Arild Fuldseth07441162016-08-15 15:07:52 +02004085#if CONFIG_DELTA_Q
4086 {
4087 struct segmentation *const seg = &cm->seg;
4088 int segment_quantizer_active = 0;
4089 for (i = 0; i < MAX_SEGMENTS; i++) {
4090 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
4091 segment_quantizer_active = 1;
4092 }
4093 }
4094
Thomas Daviesf6936102016-09-05 16:51:31 +01004095 cm->delta_q_res = 1;
Arild Fuldseth07441162016-08-15 15:07:52 +02004096 if (segment_quantizer_active == 0) {
4097 cm->delta_q_present_flag = aom_rb_read_bit(rb);
4098 } else {
4099 cm->delta_q_present_flag = 0;
4100 }
4101 if (cm->delta_q_present_flag) {
4102 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01004103 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Arild Fuldseth07441162016-08-15 15:07:52 +02004104 }
4105 }
4106#endif
4107
Urvang Joshi454280d2016-10-14 16:51:44 -07004108 for (i = 0; i < MAX_SEGMENTS; ++i) {
4109 const int qindex = cm->seg.enabled
4110 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
4111 : cm->base_qindex;
4112 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
4113 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4114 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004115 }
4116
4117 setup_segmentation_dequant(cm);
4118 cm->tx_mode =
4119 (!cm->seg.enabled && xd->lossless[0]) ? ONLY_4X4 : read_tx_mode(rb);
4120 cm->reference_mode = read_frame_reference_mode(cm, rb);
4121
4122 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004123 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004124
4125 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07004126 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004127 "Invalid header size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004128 return sz;
4129}
4130
4131#if CONFIG_EXT_TX
Thomas9ac55082016-09-23 18:04:17 +01004132#if !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuf883b422016-08-30 14:01:10 -07004133static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004134 int i, j, k;
4135 int s;
4136 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004137 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004138 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4139 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
4140 for (j = 0; j < num_ext_tx_set_inter[s] - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004141 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004142 }
4143 }
4144 }
4145
4146 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004147 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004148 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4149 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
4150 for (j = 0; j < INTRA_MODES; ++j)
4151 for (k = 0; k < num_ext_tx_set_intra[s] - 1; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004152 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
4153 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004154 }
4155 }
4156 }
4157}
Thomas9ac55082016-09-23 18:04:17 +01004158#endif // !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004159#else
4160
Yaowu Xuc27fc142016-08-22 16:08:15 -07004161#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004162#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07004163static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004164 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -07004165 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004166 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
Jingning Hanfeb517c2016-12-21 16:02:07 -08004167 for (j = TX_8X8; j < TX_SIZES; ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004168 av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004169 }
4170 }
4171 }
4172}
4173#endif // CONFIG_SUPERTX
4174
4175#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +00004176static void read_global_motion_params(WarpedMotionParams *params,
Yaowu Xuf883b422016-08-30 14:01:10 -07004177 aom_prob *probs, aom_reader *r) {
David Barkercf3d0b02016-11-10 10:14:49 +00004178 TransformationType type =
Michael Bebenita6048d052016-08-25 14:40:54 -07004179 aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004180 set_default_gmparams(params);
David Barkercf3d0b02016-11-10 10:14:49 +00004181 params->wmtype = type;
4182 switch (type) {
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004183 case HOMOGRAPHY:
4184 params->wmmat[6] = aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004185 GM_ROW3HOMO_DECODE_FACTOR;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004186 params->wmmat[7] = aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004187 GM_ROW3HOMO_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004188 case AFFINE:
4189 case ROTZOOM:
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004190 params->wmmat[2] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004191 GM_ALPHA_DECODE_FACTOR +
David Barkercf3d0b02016-11-10 10:14:49 +00004192 (1 << WARPEDMODEL_PREC_BITS);
4193 params->wmmat[3] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4194 GM_ALPHA_DECODE_FACTOR;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004195 if (type == AFFINE || type == HOMOGRAPHY) {
4196 params->wmmat[4] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004197 GM_ALPHA_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004198 params->wmmat[5] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4199 GM_ALPHA_DECODE_FACTOR +
4200 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004201 } else {
David Barkercf3d0b02016-11-10 10:14:49 +00004202 params->wmmat[4] = -params->wmmat[3];
4203 params->wmmat[5] = params->wmmat[2];
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004204 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004205 // fallthrough intended
David Barkercf3d0b02016-11-10 10:14:49 +00004206 case TRANSLATION:
4207 params->wmmat[0] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4208 GM_TRANS_DECODE_FACTOR;
4209 params->wmmat[1] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4210 GM_TRANS_DECODE_FACTOR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004211 break;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004212 case IDENTITY: break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004213 default: assert(0);
4214 }
4215}
4216
Yaowu Xuf883b422016-08-30 14:01:10 -07004217static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004218 int frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004219 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
4220 read_global_motion_params(&cm->global_motion[frame],
4221 cm->fc->global_motion_types_prob, r);
Sarah Parkere5299862016-08-16 14:57:37 -07004222 /*
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004223 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4224 frame, cm->current_video_frame, cm->show_frame,
David Barkercf3d0b02016-11-10 10:14:49 +00004225 cm->global_motion[frame].wmmat[0],
4226 cm->global_motion[frame].wmmat[1],
4227 cm->global_motion[frame].wmmat[2],
4228 cm->global_motion[frame].wmmat[3]);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004229 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004230 }
4231}
4232#endif // CONFIG_GLOBAL_MOTION
4233
Yaowu Xuf883b422016-08-30 14:01:10 -07004234static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004235 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004236 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004237#if CONFIG_SUPERTX
4238 MACROBLOCKD *const xd = &pbi->mb;
4239#endif
4240 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07004241 aom_reader r;
Yaowu Xu8af861b2016-11-01 12:12:11 -07004242 int k, i;
Yaowu Xud0af64f2016-11-17 12:50:47 -08004243#if !CONFIG_EC_ADAPT || CONFIG_EXT_INTRA
Yaowu Xu8af861b2016-11-01 12:12:11 -07004244 int j;
4245#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004246
Alex Converse2cdf0d82016-12-13 13:53:09 -08004247#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converse346440b2017-01-03 13:47:37 -08004248 r.window_size = ANS_MAX_SYMBOLS;
Alex Converse2cdf0d82016-12-13 13:53:09 -08004249#endif
Alex Converse346440b2017-01-03 13:47:37 -08004250 if (aom_reader_init(&r, data, partition_size, pbi->decrypt_cb,
4251 pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07004252 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004253 "Failed to allocate bool decoder 0");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004254
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004255#if CONFIG_LOOP_RESTORATION
4256 decode_restoration(cm, &r);
4257#endif
4258
Yaowu Xuefc75352016-10-31 09:46:42 -07004259 if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004260
Yushin Cho77bba8d2016-11-04 16:36:56 -07004261#if !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004262 read_coef_probs(fc, cm->tx_mode, &r);
4263
4264#if CONFIG_VAR_TX
4265 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004266 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
Yushin Cho77bba8d2016-11-04 16:36:56 -07004267#endif // CONFIG_VAR_TX
4268#endif // !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004269 for (k = 0; k < SKIP_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004270 av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004271
Thomas Daviesf6936102016-09-05 16:51:31 +01004272#if CONFIG_DELTA_Q
4273 for (k = 0; k < DELTA_Q_CONTEXTS; ++k)
4274 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
4275#endif
4276
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004277#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004278 if (cm->seg.enabled && cm->seg.update_map) {
4279 if (cm->seg.temporal_update) {
4280 for (k = 0; k < PREDICTION_PROBS; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004281 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004282 }
4283 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004284 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004285 }
4286
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004287 for (j = 0; j < INTRA_MODES; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004288 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004289 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004290 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004291
4292#if CONFIG_EXT_PARTITION_TYPES
4293 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004294 av1_diff_update_prob(&r, &fc->partition_prob[0][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004295 for (j = 1; j < PARTITION_CONTEXTS; ++j)
4296 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004297 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004298#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004299 for (j = 0; j < PARTITION_CONTEXTS; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004300 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004301 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004302#endif // CONFIG_EXT_PARTITION_TYPES
Thomas9ac55082016-09-23 18:04:17 +01004303#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004304#if CONFIG_EXT_INTRA
hui sueda3d762016-12-06 16:58:23 -08004305#if CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004306 for (i = 0; i < INTRA_FILTERS + 1; ++i)
4307 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004308 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
hui sueda3d762016-12-06 16:58:23 -08004309#endif // CONFIG_INTRA_INTERP
Thomas Davies6519beb2016-10-19 14:46:07 +01004310#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004311
4312 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004313 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Nathan E. Egge10ba2be2016-11-16 09:44:26 -05004314#if CONFIG_EC_MULTISYMBOL
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004315 av1_copy(cm->kf_y_cdf, av1_kf_y_mode_cdf);
4316#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004317#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004318 for (k = 0; k < INTRA_MODES; k++)
Thomas Davies6519beb2016-10-19 14:46:07 +01004319 for (j = 0; j < INTRA_MODES; j++)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004320 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004321 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004322#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004323 } else {
4324#if !CONFIG_REF_MV
4325 nmv_context *const nmvc = &fc->nmvc;
4326#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004327 read_inter_mode_probs(fc, &r);
4328
4329#if CONFIG_EXT_INTER
4330 read_inter_compound_mode_probs(fc, &r);
4331 if (cm->reference_mode != COMPOUND_REFERENCE) {
4332 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4333 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004334 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004335 }
4336 }
4337 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4338 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004339 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004340 }
4341 for (i = 0; i < BLOCK_SIZES; i++) {
4342 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004343 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004344 }
4345 }
4346 }
4347 if (cm->reference_mode != SINGLE_REFERENCE) {
4348 for (i = 0; i < BLOCK_SIZES; i++) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08004349 for (j = 0; j < COMPOUND_TYPES - 1; j++) {
4350 av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004351 }
4352 }
4353 }
4354#endif // CONFIG_EXT_INTER
4355
Yue Chencb60b182016-10-13 15:18:22 -07004356#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004357 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
Yue Chencb60b182016-10-13 15:18:22 -07004358 for (j = 0; j < MOTION_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004359 av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004360 }
Yue Chencb60b182016-10-13 15:18:22 -07004361#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004362
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004363#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004364 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004365#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004366
4367 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004368 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004369
4370 if (cm->reference_mode != SINGLE_REFERENCE)
4371 setup_compound_reference_mode(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004372 read_frame_reference_mode_probs(cm, &r);
4373
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004374#if !CONFIG_EC_ADAPT
Nathan E. Egge5710c722016-09-08 10:01:16 -04004375 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004376 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004377 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
Nathan E. Egge5710c722016-09-08 10:01:16 -04004378 }
Thomas9ac55082016-09-23 18:04:17 +01004379#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004380
4381#if CONFIG_REF_MV
4382 for (i = 0; i < NMV_CONTEXTS; ++i)
4383 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
4384#else
4385 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
4386#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004387#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004388 read_ext_tx_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004389#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004390#if CONFIG_SUPERTX
4391 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
4392#endif
4393#if CONFIG_GLOBAL_MOTION
4394 read_global_motion(cm, &r);
Thomas Davies6519beb2016-10-19 14:46:07 +01004395#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004396 }
Thomas Davies6519beb2016-10-19 14:46:07 +01004397#if CONFIG_EC_MULTISYMBOL
4398 av1_coef_pareto_cdfs(fc);
David Barker599dfd02016-11-10 13:20:12 +00004399#if CONFIG_REF_MV
4400 for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
4401#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004402 av1_set_mv_cdfs(&fc->nmvc);
David Barker599dfd02016-11-10 13:20:12 +00004403#endif
Nathan E. Egge31296062016-11-16 09:44:26 -05004404#if CONFIG_EC_MULTISYMBOL
Thomas Davies6519beb2016-10-19 14:46:07 +01004405 av1_set_mode_cdfs(cm);
4406#endif
4407#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004408
Yaowu Xuf883b422016-08-30 14:01:10 -07004409 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004410}
4411
4412#ifdef NDEBUG
4413#define debug_check_frame_counts(cm) (void)0
4414#else // !NDEBUG
4415// Counts should only be incremented when frame_parallel_decoding_mode and
4416// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07004417static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004418 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07004419 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004420 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
4421 cm->error_resilient_mode);
4422 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
4423 sizeof(cm->counts.y_mode)));
4424 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
4425 sizeof(cm->counts.uv_mode)));
4426 assert(!memcmp(cm->counts.partition, zero_counts.partition,
4427 sizeof(cm->counts.partition)));
4428 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
4429 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
4430 sizeof(cm->counts.eob_branch)));
4431 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
4432 sizeof(cm->counts.switchable_interp)));
4433 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
4434 sizeof(cm->counts.inter_mode)));
4435#if CONFIG_EXT_INTER
4436 assert(!memcmp(cm->counts.inter_compound_mode,
4437 zero_counts.inter_compound_mode,
4438 sizeof(cm->counts.inter_compound_mode)));
4439 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
4440 sizeof(cm->counts.interintra)));
4441 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
4442 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08004443 assert(!memcmp(cm->counts.compound_interinter,
4444 zero_counts.compound_interinter,
4445 sizeof(cm->counts.compound_interinter)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004446#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07004447#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
4448 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
4449 sizeof(cm->counts.motion_mode)));
4450#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004451 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
4452 sizeof(cm->counts.intra_inter)));
4453 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
4454 sizeof(cm->counts.comp_inter)));
4455 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
4456 sizeof(cm->counts.single_ref)));
4457 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
4458 sizeof(cm->counts.comp_ref)));
4459#if CONFIG_EXT_REFS
4460 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
4461 sizeof(cm->counts.comp_bwdref)));
4462#endif // CONFIG_EXT_REFS
4463 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
4464 sizeof(cm->counts.tx_size)));
4465 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
4466#if CONFIG_REF_MV
4467 assert(
4468 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
4469 assert(
4470 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
4471#else
4472 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
4473#endif
4474 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
4475 sizeof(cm->counts.inter_ext_tx)));
4476 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
4477 sizeof(cm->counts.intra_ext_tx)));
4478}
4479#endif // NDEBUG
4480
Yaowu Xuf883b422016-08-30 14:01:10 -07004481static struct aom_read_bit_buffer *init_read_bit_buffer(
4482 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
4483 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004484 rb->bit_offset = 0;
4485 rb->error_handler = error_handler;
4486 rb->error_handler_data = &pbi->common;
4487 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004488 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004489 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
4490 rb->bit_buffer = clear_data;
4491 rb->bit_buffer_end = clear_data + n;
4492 } else {
4493 rb->bit_buffer = data;
4494 rb->bit_buffer_end = data_end;
4495 }
4496 return rb;
4497}
4498
4499//------------------------------------------------------------------------------
4500
Yaowu Xuf883b422016-08-30 14:01:10 -07004501int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
4502 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
4503 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
4504 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004505}
4506
Yaowu Xuf883b422016-08-30 14:01:10 -07004507void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
4508 int *height) {
4509 *width = aom_rb_read_literal(rb, 16) + 1;
4510 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004511}
4512
Yaowu Xuf883b422016-08-30 14:01:10 -07004513BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
4514 int profile = aom_rb_read_bit(rb);
4515 profile |= aom_rb_read_bit(rb) << 1;
4516 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004517 return (BITSTREAM_PROFILE)profile;
4518}
4519
Yaowu Xuf883b422016-08-30 14:01:10 -07004520void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
4521 const uint8_t *data_end, const uint8_t **p_data_end) {
4522 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004523 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07004524 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004525 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004526 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004527 size_t first_partition_size;
4528 YV12_BUFFER_CONFIG *new_fb;
4529
4530#if CONFIG_BITSTREAM_DEBUG
4531 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
4532#endif
4533
4534 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004535 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Thomas Davies72712e62016-11-09 12:17:51 +00004536#if CONFIG_TILE_GROUPS
4537 pbi->first_partition_size = first_partition_size;
4538 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
4539#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004540 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004541 xd->cur_buf = new_fb;
4542#if CONFIG_GLOBAL_MOTION
4543 xd->global_motion = cm->global_motion;
4544#endif // CONFIG_GLOBAL_MOTION
4545
4546 if (!first_partition_size) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004547 // showing a frame directly
4548 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004549 return;
4550 }
4551
Yaowu Xuf883b422016-08-30 14:01:10 -07004552 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004553 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07004554 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004555 "Truncated packet or corrupt header length");
4556
Jingning Han24e0a182016-11-20 22:34:12 -08004557#if CONFIG_REF_MV
Dengca8d24d2016-10-17 14:06:35 +08004558 cm->setup_mi(cm);
4559#endif
4560
Yaowu Xuc27fc142016-08-22 16:08:15 -07004561 cm->use_prev_frame_mvs =
4562 !cm->error_resilient_mode && cm->width == cm->last_width &&
4563 cm->height == cm->last_height && !cm->last_intra_only &&
4564 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
4565#if CONFIG_EXT_REFS
4566 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
4567 // show_exisiting_frame=1, nor can it take a frame not used as
4568 // a reference, it is probable that by the time it is being
4569 // referred to, the frame buffer it originally points to may
4570 // already get expired and have been reassigned to the current
4571 // newly coded frame. Hence, we need to check whether this is
4572 // the case, and if yes, we have 2 choices:
4573 // (1) Simply disable the use of previous frame mvs; or
4574 // (2) Have cm->prev_frame point to one reference frame buffer,
4575 // e.g. LAST_FRAME.
4576 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
4577 // Reassign the LAST_FRAME buffer to cm->prev_frame.
4578 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4579 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4580 }
4581#endif // CONFIG_EXT_REFS
4582
Yaowu Xuf883b422016-08-30 14:01:10 -07004583 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004584
4585 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
4586 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07004587 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004588 "Uninitialized entropy context.");
4589
Yaowu Xuf883b422016-08-30 14:01:10 -07004590 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004591
4592 xd->corrupted = 0;
4593 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
4594 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07004595 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004596 "Decode failed. Frame data header is corrupted.");
4597
4598 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004599 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004600 }
4601
4602 // If encoded in frame parallel mode, frame context is ready after decoding
4603 // the frame header.
4604 if (cm->frame_parallel_decode &&
4605 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004606 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004607 FrameWorkerData *const frame_worker_data = worker->data1;
4608 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
4609 context_updated = 1;
4610 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4611 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004612 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004613 pbi->cur_buf->row = -1;
4614 pbi->cur_buf->col = -1;
4615 frame_worker_data->frame_context_ready = 1;
4616 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07004617 av1_frameworker_signal_stats(worker);
4618 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004619 }
4620
4621#if CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004622 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004623 cm->coef_probs_update_idx = 0;
4624#endif // CONFIG_ENTROPY
4625
4626 if (pbi->max_threads > 1
4627#if CONFIG_EXT_TILE
4628 && pbi->dec_tile_col < 0 // Decoding all columns
4629#endif // CONFIG_EXT_TILE
4630 && cm->tile_cols > 1) {
4631 // Multi-threaded tile decoder
4632 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
4633 if (!xd->corrupted) {
4634 if (!cm->skip_loop_filter) {
4635 // If multiple threads are used to decode tiles, then we use those
4636 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07004637 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
4638 0, 0, pbi->tile_workers, pbi->num_tile_workers,
4639 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004640 }
4641 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004642 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004643 "Decode failed. Frame data is corrupted.");
4644 }
4645 } else {
4646 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
4647 }
4648#if CONFIG_LOOP_RESTORATION
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08004649 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4650 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4651 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4652 av1_loop_restoration_frame(new_fb, cm, cm->rst_info, 7, 0, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004653 }
4654#endif // CONFIG_LOOP_RESTORATION
4655
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004656#if CONFIG_DERING
4657 if (cm->dering_level && !cm->skip_loop_filter) {
4658 av1_dering_frame(&pbi->cur_buf->buf, cm, &pbi->mb, cm->dering_level);
4659 }
4660#endif // CONFIG_DERING
4661
Thomas Daedef56859f2016-04-19 16:57:24 -07004662#if CONFIG_CLPF
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004663 if (!cm->skip_loop_filter) {
4664 const YV12_BUFFER_CONFIG *const frame = &pbi->cur_buf->buf;
4665 if (cm->clpf_strength_y) {
Yaowu Xud71be782016-10-14 08:47:03 -07004666 av1_clpf_frame(frame, NULL, cm, cm->clpf_size != CLPF_NOSIZE,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004667 cm->clpf_strength_y + (cm->clpf_strength_y == 3),
Yaowu Xud71be782016-10-14 08:47:03 -07004668 4 + cm->clpf_size, AOM_PLANE_Y, clpf_bit);
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004669 }
4670 if (cm->clpf_strength_u) {
Yaowu Xud71be782016-10-14 08:47:03 -07004671 av1_clpf_frame(frame, NULL, cm, 0, // No block signals for chroma
4672 cm->clpf_strength_u + (cm->clpf_strength_u == 3), 4,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004673 AOM_PLANE_U, NULL);
4674 }
4675 if (cm->clpf_strength_v) {
Yaowu Xud71be782016-10-14 08:47:03 -07004676 av1_clpf_frame(frame, NULL, cm, 0, // No block signals for chroma
4677 cm->clpf_strength_v + (cm->clpf_strength_v == 3), 4,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004678 AOM_PLANE_V, NULL);
4679 }
Steinar Midtskogend06588a2016-05-06 13:48:20 +02004680 }
4681 if (cm->clpf_blocks) aom_free(cm->clpf_blocks);
Thomas Daedef56859f2016-04-19 16:57:24 -07004682#endif
Thomas Daedef56859f2016-04-19 16:57:24 -07004683
Yaowu Xuc27fc142016-08-22 16:08:15 -07004684 if (!xd->corrupted) {
4685 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
4686#if CONFIG_ENTROPY
4687 cm->partial_prob_update = 0;
4688#endif // CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004689 av1_adapt_coef_probs(cm);
4690 av1_adapt_intra_frame_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004691
4692 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004693 av1_adapt_inter_frame_probs(cm);
4694 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004695 }
4696 } else {
4697 debug_check_frame_counts(cm);
4698 }
4699 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004700 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004701 "Decode failed. Frame data is corrupted.");
4702 }
4703
4704 // Non frame parallel update frame context here.
4705 if (!cm->error_resilient_mode && !context_updated)
4706 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4707}