blob: 42629f2113217927e8213cf823608c4ddcb921d6 [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
69#include "aom_dsp/entdec.h"
70#include "av1/common/partition.h"
71#include "av1/decoder/decint.h"
72#include "av1/encoder/hybrid_fwd_txfm.h"
73#endif
74
Thomas Davies80188d12016-10-26 16:08:35 -070075static struct aom_read_bit_buffer *init_read_bit_buffer(
76 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
77 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]);
78static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
79 size_t partition_size);
80static size_t read_uncompressed_header(AV1Decoder *pbi,
81 struct aom_read_bit_buffer *rb);
82
Yaowu Xuf883b422016-08-30 14:01:10 -070083static int is_compound_reference_allowed(const AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070084 int i;
85 if (frame_is_intra_only(cm)) return 0;
86 for (i = 1; i < INTER_REFS_PER_FRAME; ++i)
87 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
88
89 return 0;
90}
91
Yaowu Xuf883b422016-08-30 14:01:10 -070092static void setup_compound_reference_mode(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070093#if CONFIG_EXT_REFS
94 cm->comp_fwd_ref[0] = LAST_FRAME;
95 cm->comp_fwd_ref[1] = LAST2_FRAME;
96 cm->comp_fwd_ref[2] = LAST3_FRAME;
97 cm->comp_fwd_ref[3] = GOLDEN_FRAME;
98
99 cm->comp_bwd_ref[0] = BWDREF_FRAME;
100 cm->comp_bwd_ref[1] = ALTREF_FRAME;
101#else
102 if (cm->ref_frame_sign_bias[LAST_FRAME] ==
103 cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
104 cm->comp_fixed_ref = ALTREF_FRAME;
105 cm->comp_var_ref[0] = LAST_FRAME;
106 cm->comp_var_ref[1] = GOLDEN_FRAME;
107 } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
108 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
109 cm->comp_fixed_ref = GOLDEN_FRAME;
110 cm->comp_var_ref[0] = LAST_FRAME;
111 cm->comp_var_ref[1] = ALTREF_FRAME;
112 } else {
113 cm->comp_fixed_ref = LAST_FRAME;
114 cm->comp_var_ref[0] = GOLDEN_FRAME;
115 cm->comp_var_ref[1] = ALTREF_FRAME;
116 }
117#endif // CONFIG_EXT_REFS
118}
119
120static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
121 return len != 0 && len <= (size_t)(end - start);
122}
123
Yaowu Xuf883b422016-08-30 14:01:10 -0700124static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
125 const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700126 return data > max ? max : data;
127}
128
Yaowu Xuf883b422016-08-30 14:01:10 -0700129static TX_MODE read_tx_mode(struct aom_read_bit_buffer *rb) {
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800130#if CONFIG_TX64X64
131 TX_MODE tx_mode =
132 aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
133 if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
134 return tx_mode;
135#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700136 return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800137#endif // CONFIG_TX64X64
Yaowu Xuc27fc142016-08-22 16:08:15 -0700138}
139
Yaowu Xuefc75352016-10-31 09:46:42 -0700140static void read_tx_size_probs(FRAME_CONTEXT *fc, aom_reader *r) {
141 int i, j, k;
142 for (i = 0; i < MAX_TX_DEPTH; ++i)
143 for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
144 for (k = 0; k < i + 1; ++k)
145 av1_diff_update_prob(r, &fc->tx_size_probs[i][j][k], ACCT_STR);
146}
147
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400148#if !CONFIG_EC_ADAPT
Yaowu Xuf883b422016-08-30 14:01:10 -0700149static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700150 int i, j;
Nathan E. Egge4947c292016-04-26 11:37:06 -0400151 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700152 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700153 av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i], ACCT_STR);
Nathan E. Egge4947c292016-04-26 11:37:06 -0400154 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700155}
Thomas Davies6519beb2016-10-19 14:46:07 +0100156#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700157
Yaowu Xuf883b422016-08-30 14:01:10 -0700158static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700159#if CONFIG_REF_MV
Yaowu Xu8af861b2016-11-01 12:12:11 -0700160 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700161 for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700162 av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700163 for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700164 av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700165 for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700166 av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700167 for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700168 av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700169#if CONFIG_EXT_INTER
Michael Bebenita6048d052016-08-25 14:40:54 -0700170 av1_diff_update_prob(r, &fc->new2mv_prob, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700171#endif // CONFIG_EXT_INTER
172#else
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400173#if !CONFIG_EC_ADAPT
Yaowu Xu8af861b2016-11-01 12:12:11 -0700174 int i, j;
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400175 for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700176 for (j = 0; j < INTER_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700177 av1_diff_update_prob(r, &fc->inter_mode_probs[i][j], ACCT_STR);
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400178 }
Yaowu Xu8af861b2016-11-01 12:12:11 -0700179#else
180 (void)fc;
181 (void)r;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182#endif
Thomas Davies6519beb2016-10-19 14:46:07 +0100183#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700184}
185
186#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700187static void read_inter_compound_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700188 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -0700189 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700190 for (j = 0; j < INTER_MODE_CONTEXTS; ++j) {
191 for (i = 0; i < INTER_COMPOUND_MODES - 1; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700192 av1_diff_update_prob(r, &fc->inter_compound_mode_probs[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700193 }
194 }
195 }
196}
197#endif // CONFIG_EXT_INTER
Yaowu Xu17fd2f22016-11-17 18:23:28 -0800198#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100199#if !CONFIG_EXT_TX
200static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
201 int i, j, k;
202 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
203 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
204 for (j = 0; j < TX_TYPES; ++j) {
205 for (k = 0; k < TX_TYPES - 1; ++k)
206 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[i][j][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100207 }
208 }
209 }
210 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
211 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
212 for (k = 0; k < TX_TYPES - 1; ++k)
213 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[i][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100214 }
215 }
216}
217#endif
218#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700219
220static REFERENCE_MODE read_frame_reference_mode(
Yaowu Xuf883b422016-08-30 14:01:10 -0700221 const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700222 if (is_compound_reference_allowed(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700223 return aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700224 ? REFERENCE_MODE_SELECT
Yaowu Xuf883b422016-08-30 14:01:10 -0700225 : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700226 } else {
227 return SINGLE_REFERENCE;
228 }
229}
230
Yaowu Xuf883b422016-08-30 14:01:10 -0700231static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700232 FRAME_CONTEXT *const fc = cm->fc;
233 int i, j;
234
235 if (cm->reference_mode == REFERENCE_MODE_SELECT)
236 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700237 av1_diff_update_prob(r, &fc->comp_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700238
239 if (cm->reference_mode != COMPOUND_REFERENCE) {
240 for (i = 0; i < REF_CONTEXTS; ++i) {
241 for (j = 0; j < (SINGLE_REFS - 1); ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700242 av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700243 }
244 }
245 }
246
247 if (cm->reference_mode != SINGLE_REFERENCE) {
248 for (i = 0; i < REF_CONTEXTS; ++i) {
249#if CONFIG_EXT_REFS
250 for (j = 0; j < (FWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700251 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700252 for (j = 0; j < (BWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700253 av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700254#else
255 for (j = 0; j < (COMP_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700256 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700257#endif // CONFIG_EXT_REFS
258 }
259 }
260}
261
Yaowu Xuf883b422016-08-30 14:01:10 -0700262static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700263 int i;
Michael Bebenita6048d052016-08-25 14:40:54 -0700264 for (i = 0; i < n; ++i) av1_diff_update_prob(r, &p[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700265}
266
Yaowu Xuf883b422016-08-30 14:01:10 -0700267static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Thomas9ac55082016-09-23 18:04:17 +0100268 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700269
Yaowu Xue86288d2016-10-31 15:56:38 -0700270#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100271 int j;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700272 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
273
Yaowu Xuc27fc142016-08-22 16:08:15 -0700274 for (i = 0; i < 2; ++i) {
275 nmv_component *const comp_ctx = &ctx->comps[i];
276 update_mv_probs(&comp_ctx->sign, 1, r);
277 update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
278 update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
279 update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
280 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700281 for (i = 0; i < 2; ++i) {
282 nmv_component *const comp_ctx = &ctx->comps[i];
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400283 for (j = 0; j < CLASS0_SIZE; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700284 update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400285 }
Nathan E. Eggeac499f32016-09-08 15:38:57 -0400286 update_mv_probs(comp_ctx->fp, MV_FP_SIZE - 1, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700287 }
Alex Converseaca9feb2016-10-10 11:08:10 -0700288#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700289
290 if (allow_hp) {
291 for (i = 0; i < 2; ++i) {
292 nmv_component *const comp_ctx = &ctx->comps[i];
293 update_mv_probs(&comp_ctx->class0_hp, 1, r);
294 update_mv_probs(&comp_ctx->hp, 1, r);
295 }
296 }
297}
298
299static void inverse_transform_block(MACROBLOCKD *xd, int plane,
300 const TX_TYPE tx_type,
301 const TX_SIZE tx_size, uint8_t *dst,
Jingning Han1be18782016-10-21 11:48:15 -0700302 int stride, int16_t scan_line, int eob) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700303 struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han1be18782016-10-21 11:48:15 -0700304 tran_low_t *const dqcoeff = pd->dqcoeff;
305 INV_TXFM_PARAM inv_txfm_param;
306 inv_txfm_param.tx_type = tx_type;
307 inv_txfm_param.tx_size = tx_size;
308 inv_txfm_param.eob = eob;
309 inv_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700310
Yaowu Xuf883b422016-08-30 14:01:10 -0700311#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han1be18782016-10-21 11:48:15 -0700312 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
313 inv_txfm_param.bd = xd->bd;
314 highbd_inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
315 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700316#endif // CONFIG_AOM_HIGHBITDEPTH
Jingning Han1be18782016-10-21 11:48:15 -0700317 inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
Yaowu Xuf883b422016-08-30 14:01:10 -0700318#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700319 }
Jingning Han1be18782016-10-21 11:48:15 -0700320#endif // CONFIG_AOM_HIGHBITDEPTH
321 memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700322}
323
Yushin Cho77bba8d2016-11-04 16:36:56 -0700324#if CONFIG_PVQ
325static int av1_pvq_decode_helper(od_dec_ctx *dec, int16_t *ref_coeff,
326 int16_t *dqcoeff, int16_t *quant, int pli,
327 int bs, TX_TYPE tx_type, int xdec,
328 int ac_dc_coded) {
329 unsigned int flags; // used for daala's stream analyzer.
330 int off;
331 const int is_keyframe = 0;
332 const int has_dc_skip = 1;
Debargha Mukherjee153e1f82016-11-17 09:59:14 -0800333 int quant_shift = get_tx_scale(bs);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700334 // DC quantizer for PVQ
335 int pvq_dc_quant;
336 int lossless = (quant[0] == 0);
337 const int blk_size = tx_size_wide[bs];
338 int eob = 0;
339 int i;
Yushin Cho70669122016-12-08 09:53:14 -1000340 int use_activity_masking = dec->use_activity_masking;
Yushin Cho48f84db2016-11-07 21:20:17 -0800341 DECLARE_ALIGNED(16, int16_t, dqcoeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
342 DECLARE_ALIGNED(16, int16_t, ref_coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700343
Yushin Cho48f84db2016-11-07 21:20:17 -0800344 od_coeff ref_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
345 od_coeff out_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700346
347 od_raster_to_coding_order(ref_coeff_pvq, blk_size, tx_type, ref_coeff,
348 blk_size);
349
350 if (lossless)
351 pvq_dc_quant = 1;
352 else {
Yushin Cho70669122016-12-08 09:53:14 -1000353 if (use_activity_masking)
354 pvq_dc_quant = OD_MAXI(
355 1, (quant[0] >> quant_shift) *
356 dec->state.pvq_qm_q4[pli][od_qm_get_index(bs, 0)] >>
357 4);
358 else
359 pvq_dc_quant = OD_MAXI(1, quant[0] >> quant_shift);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700360 }
361
362 off = od_qm_offset(bs, xdec);
363
364 // copy int16 inputs to int32
365 for (i = 0; i < blk_size * blk_size; i++) ref_int32[i] = ref_coeff_pvq[i];
366
367 od_pvq_decode(dec, ref_int32, out_int32, (int)quant[1] >> quant_shift, pli,
368 bs, OD_PVQ_BETA[use_activity_masking][pli][bs],
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800369 OD_ROBUST_STREAM, is_keyframe, &flags, ac_dc_coded,
370 dec->state.qm + off, dec->state.qm_inv + off);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700371
372 // copy int32 result back to int16
373 for (i = 0; i < blk_size * blk_size; i++) dqcoeff_pvq[i] = out_int32[i];
374
375 if (!has_dc_skip || dqcoeff_pvq[0]) {
376 dqcoeff_pvq[0] =
377 has_dc_skip + generic_decode(dec->ec, &dec->state.adapt.model_dc[pli],
378 -1, &dec->state.adapt.ex_dc[pli][bs][0], 2,
379 "dc:mag");
380 if (dqcoeff_pvq[0])
381 dqcoeff_pvq[0] *= od_ec_dec_bits(dec->ec, 1, "dc:sign") ? -1 : 1;
382 }
383 dqcoeff_pvq[0] = dqcoeff_pvq[0] * pvq_dc_quant + ref_coeff_pvq[0];
384
385 od_coding_order_to_raster(dqcoeff, blk_size, tx_type, dqcoeff_pvq, blk_size);
386
387 eob = blk_size * blk_size;
388
389 return eob;
390}
391
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800392static int av1_pvq_decode_helper2(MACROBLOCKD *const xd,
393 MB_MODE_INFO *const mbmi, int plane, int row,
394 int col, TX_SIZE tx_size, TX_TYPE tx_type) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700395 struct macroblockd_plane *const pd = &xd->plane[plane];
396 // transform block size in pixels
397 int tx_blk_size = tx_size_wide[tx_size];
398 int i, j;
399 tran_low_t *pvq_ref_coeff = pd->pvq_ref_coeff;
400 const int diff_stride = tx_blk_size;
401 int16_t *pred = pd->pred;
402 tran_low_t *const dqcoeff = pd->dqcoeff;
403 int ac_dc_coded; // bit0: DC coded, bit1 : AC coded
404 uint8_t *dst;
405 int eob;
406
407 eob = 0;
408 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
409
410 // decode ac/dc coded flag. bit0: DC coded, bit1 : AC coded
411 // NOTE : we don't use 5 symbols for luma here in aom codebase,
412 // since block partition is taken care of by aom.
413 // So, only AC/DC skip info is coded
414 ac_dc_coded = od_decode_cdf_adapt(
415 xd->daala_dec.ec,
416 xd->daala_dec.state.adapt.skip_cdf[2 * tx_size + (plane != 0)], 4,
417 xd->daala_dec.state.adapt.skip_increment, "skip");
418
419 if (ac_dc_coded) {
420 int xdec = pd->subsampling_x;
421 int seg_id = mbmi->segment_id;
422 int16_t *quant;
423 FWD_TXFM_PARAM fwd_txfm_param;
Yaowu Xufc1b2132016-11-07 15:16:15 -0800424 // ToDo(yaowu): correct this with optimal number from decoding process.
425 const int max_scan_line = tx_size_2d[tx_size];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700426
427 for (j = 0; j < tx_blk_size; j++)
428 for (i = 0; i < tx_blk_size; i++) {
429 pred[diff_stride * j + i] = dst[pd->dst.stride * j + i];
430 }
431
432 fwd_txfm_param.tx_type = tx_type;
433 fwd_txfm_param.tx_size = tx_size;
434 fwd_txfm_param.fwd_txfm_opt = FWD_TXFM_OPT_NORMAL;
435 fwd_txfm_param.rd_transform = 0;
436 fwd_txfm_param.lossless = xd->lossless[seg_id];
437
438 fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
439
440 quant = &pd->seg_dequant[seg_id][0]; // aom's quantizer
441
442 eob = av1_pvq_decode_helper(&xd->daala_dec, pvq_ref_coeff, dqcoeff, quant,
443 plane, tx_size, tx_type, xdec, ac_dc_coded);
444
445 // Since av1 does not have separate inverse transform
446 // but also contains adding to predicted image,
447 // pass blank dummy image to av1_inv_txfm_add_*x*(), i.e. set dst as zeros
448 for (j = 0; j < tx_blk_size; j++)
449 for (i = 0; i < tx_blk_size; i++) dst[j * pd->dst.stride + i] = 0;
450
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800451 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
452 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700453 }
454
455 return eob;
456}
457#endif
458
Angie Chiangff6d8902016-10-21 11:02:09 -0700459static void predict_and_reconstruct_intra_block(AV1_COMMON *cm,
460 MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700461#if CONFIG_ANS
462 struct AnsDecoder *const r,
463#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700464 aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700465#endif // CONFIG_ANS
466 MB_MODE_INFO *const mbmi,
467 int plane, int row, int col,
468 TX_SIZE tx_size) {
469 struct macroblockd_plane *const pd = &xd->plane[plane];
470 PREDICTION_MODE mode = (plane == 0) ? mbmi->mode : mbmi->uv_mode;
471 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
472 uint8_t *dst;
473 int block_idx = (row << 1) + col;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700474#if CONFIG_PVQ
475 (void)cm;
476 (void)r;
477#endif
Jingning Hanca14dda2016-12-09 09:36:00 -0800478 dst = &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700479
Jingning Han41bb3392016-12-14 10:46:48 -0800480#if !CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -0700481 if (mbmi->sb_type < BLOCK_8X8)
482 if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
Jingning Han41bb3392016-12-14 10:46:48 -0800483#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700484
Jingning Hanc4c99da2016-10-24 10:27:28 -0700485 av1_predict_intra_block(xd, pd->width, pd->height, tx_size, mode, dst,
Yaowu Xuf883b422016-08-30 14:01:10 -0700486 pd->dst.stride, dst, pd->dst.stride, col, row, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700487
488 if (!mbmi->skip) {
489 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700490#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700491 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 0);
Jingning Han1be18782016-10-21 11:48:15 -0700492 int16_t max_scan_line = 0;
493 const int eob =
494 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size,
495 tx_type, &max_scan_line, r, mbmi->segment_id);
Angie Chianged8cd9a2016-10-21 16:44:47 -0700496#if CONFIG_ADAPT_SCAN
497 av1_update_scan_count_facade(cm, tx_size, tx_type, pd->dqcoeff, eob);
498#endif
Jingning Han1be18782016-10-21 11:48:15 -0700499 if (eob)
500 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
501 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700502#else
503 av1_pvq_decode_helper2(xd, mbmi, plane, row, col, tx_size, tx_type);
504#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700505 }
506}
507
508#if CONFIG_VAR_TX
Angie Chiangff6d8902016-10-21 11:02:09 -0700509static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
510 aom_reader *r, MB_MODE_INFO *const mbmi,
Jingning Han8fd62b72016-10-21 12:55:54 -0700511 int plane, BLOCK_SIZE plane_bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700512 int blk_row, int blk_col, TX_SIZE tx_size,
513 int *eob_total) {
514 const struct macroblockd_plane *const pd = &xd->plane[plane];
515 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
516 const int tx_row = blk_row >> (1 - pd->subsampling_y);
517 const int tx_col = blk_col >> (1 - pd->subsampling_x);
518 const TX_SIZE plane_tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700519 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700520 : mbmi->inter_tx_size[tx_row][tx_col];
Jingning Han5f614262016-10-27 14:27:43 -0700521 // Scale to match transform block unit.
Jingning Hanf64062f2016-11-02 16:22:18 -0700522 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
523 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700524
525 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
526
527 if (tx_size == plane_tx_size) {
528 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
Jingning Han8fd62b72016-10-21 12:55:54 -0700529 int block_idx = (blk_row << 1) + blk_col;
530 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiangff6d8902016-10-21 11:02:09 -0700531 const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700532 int16_t max_scan_line = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700533 const int eob =
Yaowu Xuf883b422016-08-30 14:01:10 -0700534 av1_decode_block_tokens(xd, plane, sc, blk_col, blk_row, plane_tx_size,
Jingning Han1be18782016-10-21 11:48:15 -0700535 tx_type, &max_scan_line, r, mbmi->segment_id);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700536 inverse_transform_block(
537 xd, plane, tx_type, plane_tx_size,
538 &pd->dst.buf[4 * blk_row * pd->dst.stride + 4 * blk_col],
Jingning Han1be18782016-10-21 11:48:15 -0700539 pd->dst.stride, max_scan_line, eob);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700540 *eob_total += eob;
541 } else {
Jingning Hanf64062f2016-11-02 16:22:18 -0700542 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
543 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700544 int i;
545
546 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700547
548 for (i = 0; i < 4; ++i) {
Jingning Han5f614262016-10-27 14:27:43 -0700549 const int offsetr = blk_row + (i >> 1) * bsl;
550 const int offsetc = blk_col + (i & 0x01) * bsl;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700551
552 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
553
Jingning Han8fd62b72016-10-21 12:55:54 -0700554 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
Jingning Hanf64062f2016-11-02 16:22:18 -0700555 offsetc, sub_txs, eob_total);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700556 }
557 }
558}
559#endif // CONFIG_VAR_TX
560
Jingning Hanfe45b212016-11-22 10:30:23 -0800561#if !CONFIG_VAR_TX || CONFIG_SUPERTX || \
562 (!CONFIG_VAR_TX && CONFIG_EXT_TX && CONFIG_RECT_TX)
Angie Chiangff6d8902016-10-21 11:02:09 -0700563static int reconstruct_inter_block(AV1_COMMON *cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700564#if CONFIG_ANS
565 struct AnsDecoder *const r,
566#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700567 aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700568#endif
569 int segment_id, int plane, int row, int col,
570 TX_SIZE tx_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700571 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
572 int block_idx = (row << 1) + col;
573 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700574#if CONFIG_PVQ
575 int eob;
576 (void)cm;
577 (void)r;
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800578 (void)segment_id;
579#else
580 struct macroblockd_plane *const pd = &xd->plane[plane];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700581#endif
582
583#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700584 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700585 int16_t max_scan_line = 0;
586 const int eob =
587 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size, tx_type,
588 &max_scan_line, r, segment_id);
Jingning Hanca14dda2016-12-09 09:36:00 -0800589 uint8_t *dst =
590 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Angie Chianged8cd9a2016-10-21 16:44:47 -0700591#if CONFIG_ADAPT_SCAN
592 av1_update_scan_count_facade(cm, tx_size, tx_type, pd->dqcoeff, eob);
593#endif
Jingning Han1be18782016-10-21 11:48:15 -0700594 if (eob)
Jingning Hanca14dda2016-12-09 09:36:00 -0800595 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
596 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700597#else
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800598 eob = av1_pvq_decode_helper2(xd, &xd->mi[0]->mbmi, plane, row, col, tx_size,
599 tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700600#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700601 return eob;
602}
603#endif // !CONFIG_VAR_TX || CONFIG_SUPER_TX
604
Yaowu Xuf883b422016-08-30 14:01:10 -0700605static MB_MODE_INFO *set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606 BLOCK_SIZE bsize, int mi_row, int mi_col,
Jingning Hanfaad0e12016-12-07 10:54:57 -0800607 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700608 const int offset = mi_row * cm->mi_stride + mi_col;
609 int x, y;
610 const TileInfo *const tile = &xd->tile;
611
612 xd->mi = cm->mi_grid_visible + offset;
613 xd->mi[0] = &cm->mi[offset];
614 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
615 // passing bsize from decode_partition().
616 xd->mi[0]->mbmi.sb_type = bsize;
Angie Chiang394c3372016-11-03 11:13:15 -0700617#if CONFIG_RD_DEBUG
618 xd->mi[0]->mbmi.mi_row = mi_row;
619 xd->mi[0]->mbmi.mi_col = mi_col;
620#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621 for (y = 0; y < y_mis; ++y)
Jingning Han97d85482016-07-15 11:06:05 -0700622 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700623
Jingning Hanfaad0e12016-12-07 10:54:57 -0800624 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700625 set_skip_context(xd, mi_row, mi_col);
626
627#if CONFIG_VAR_TX
628 xd->max_tx_size = max_txsize_lookup[bsize];
629#endif
630
631 // Distance of Mb to the various image edges. These are specified to 8th pel
632 // as they are always compared to values that are in 1/8th pel units
633 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
634
Yaowu Xuf883b422016-08-30 14:01:10 -0700635 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700636 return &xd->mi[0]->mbmi;
637}
638
639#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700640static MB_MODE_INFO *set_offsets_extend(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700641 MACROBLOCKD *const xd,
642 const TileInfo *const tile,
643 BLOCK_SIZE bsize_pred, int mi_row_pred,
644 int mi_col_pred, int mi_row_ori,
645 int mi_col_ori) {
646 // Used in supertx
647 // (mi_row_ori, mi_col_ori): location for mv
648 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
Jingning Han93531242016-12-20 11:54:36 -0800649 const int bw = mi_size_wide[bsize_pred];
650 const int bh = mi_size_high[bsize_pred];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700651 const int offset = mi_row_ori * cm->mi_stride + mi_col_ori;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700652 xd->mi = cm->mi_grid_visible + offset;
653 xd->mi[0] = cm->mi + offset;
654 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
655 cm->mi_cols);
656
657 xd->up_available = (mi_row_ori > tile->mi_row_start);
658 xd->left_available = (mi_col_ori > tile->mi_col_start);
659
Jingning Hanfaad0e12016-12-07 10:54:57 -0800660 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700661
662 return &xd->mi[0]->mbmi;
663}
664
Yaowu Xuf883b422016-08-30 14:01:10 -0700665static MB_MODE_INFO *set_mb_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
666 BLOCK_SIZE bsize, int mi_row, int mi_col,
667 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700668 const int offset = mi_row * cm->mi_stride + mi_col;
669 const TileInfo *const tile = &xd->tile;
670 int x, y;
671
672 xd->mi = cm->mi_grid_visible + offset;
673 xd->mi[0] = cm->mi + offset;
674 xd->mi[0]->mbmi.sb_type = bsize;
675 for (y = 0; y < y_mis; ++y)
676 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
677
678 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
679 return &xd->mi[0]->mbmi;
680}
681
Yaowu Xuf883b422016-08-30 14:01:10 -0700682static void set_offsets_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700683 const TileInfo *const tile, BLOCK_SIZE bsize,
684 int mi_row, int mi_col) {
Jingning Han93531242016-12-20 11:54:36 -0800685 const int bw = mi_size_wide[bsize];
686 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700687 const int offset = mi_row * cm->mi_stride + mi_col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700688
689 xd->mi = cm->mi_grid_visible + offset;
690 xd->mi[0] = cm->mi + offset;
691
Jingning Hanfaad0e12016-12-07 10:54:57 -0800692 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700693
694 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
695
Yaowu Xuf883b422016-08-30 14:01:10 -0700696 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700697}
698
Yaowu Xuf883b422016-08-30 14:01:10 -0700699static void set_param_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700700 BLOCK_SIZE bsize, int mi_row, int mi_col,
701 int txfm, int skip) {
Jingning Han93531242016-12-20 11:54:36 -0800702 const int bw = mi_size_wide[bsize];
703 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -0700704 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
705 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700706 const int offset = mi_row * cm->mi_stride + mi_col;
707 int x, y;
708
709 xd->mi = cm->mi_grid_visible + offset;
710 xd->mi[0] = cm->mi + offset;
711
712 for (y = 0; y < y_mis; ++y)
713 for (x = 0; x < x_mis; ++x) {
714 xd->mi[y * cm->mi_stride + x]->mbmi.skip = skip;
715 xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
716 }
717#if CONFIG_VAR_TX
718 xd->above_txfm_context = cm->above_txfm_context + mi_col;
719 xd->left_txfm_context =
720 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
Yaowu Xu52a17632016-11-17 15:48:21 -0800721 set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bw, bh, skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700722#endif
723}
724
Yaowu Xuf883b422016-08-30 14:01:10 -0700725static void set_ref(AV1_COMMON *const cm, MACROBLOCKD *const xd, int idx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700726 int mi_row, int mi_col) {
727 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
728 RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
729 xd->block_refs[idx] = ref_buffer;
Yaowu Xuf883b422016-08-30 14:01:10 -0700730 if (!av1_is_valid_scale(&ref_buffer->sf))
731 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700732 "Invalid scale factors");
Yaowu Xuf883b422016-08-30 14:01:10 -0700733 av1_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
734 &ref_buffer->sf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700735 xd->corrupted |= ref_buffer->buf->corrupted;
736}
737
738static void dec_predict_b_extend(
Yaowu Xuf883b422016-08-30 14:01:10 -0700739 AV1Decoder *const pbi, MACROBLOCKD *const xd, const TileInfo *const tile,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700740 int block, int mi_row_ori, int mi_col_ori, int mi_row_pred, int mi_col_pred,
741 int mi_row_top, int mi_col_top, uint8_t *dst_buf[3], int dst_stride[3],
742 BLOCK_SIZE bsize_top, BLOCK_SIZE bsize_pred, int b_sub8x8, int bextend) {
743 // Used in supertx
744 // (mi_row_ori, mi_col_ori): location for mv
745 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
746 // (mi_row_top, mi_col_top, bsize_top): region of the top partition size
747 // block: sub location of sub8x8 blocks
748 // b_sub8x8: 1: ori is sub8x8; 0: ori is not sub8x8
749 // bextend: 1: region to predict is an extension of ori; 0: not
750 int r = (mi_row_pred - mi_row_top) * MI_SIZE;
751 int c = (mi_col_pred - mi_col_top) * MI_SIZE;
Jingning Han93531242016-12-20 11:54:36 -0800752 const int mi_width_top = mi_size_wide[bsize_top];
753 const int mi_height_top = mi_size_high[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700754 MB_MODE_INFO *mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700755 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700756
757 if (mi_row_pred < mi_row_top || mi_col_pred < mi_col_top ||
758 mi_row_pred >= mi_row_top + mi_height_top ||
759 mi_col_pred >= mi_col_top + mi_width_top || mi_row_pred >= cm->mi_rows ||
760 mi_col_pred >= cm->mi_cols)
761 return;
762
763 mbmi = set_offsets_extend(cm, xd, tile, bsize_pred, mi_row_pred, mi_col_pred,
764 mi_row_ori, mi_col_ori);
765 set_ref(cm, xd, 0, mi_row_pred, mi_col_pred);
766 if (has_second_ref(&xd->mi[0]->mbmi))
767 set_ref(cm, xd, 1, mi_row_pred, mi_col_pred);
768
769 if (!bextend) {
770 mbmi->tx_size = b_width_log2_lookup[bsize_top];
Jingning Han93531242016-12-20 11:54:36 -0800771#if CONFIG_CB4X4
772 ++mbmi->tx_size;
773#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700774 }
775
776 xd->plane[0].dst.stride = dst_stride[0];
777 xd->plane[1].dst.stride = dst_stride[1];
778 xd->plane[2].dst.stride = dst_stride[2];
779 xd->plane[0].dst.buf = dst_buf[0] +
780 (r >> xd->plane[0].subsampling_y) * dst_stride[0] +
781 (c >> xd->plane[0].subsampling_x);
782 xd->plane[1].dst.buf = dst_buf[1] +
783 (r >> xd->plane[1].subsampling_y) * dst_stride[1] +
784 (c >> xd->plane[1].subsampling_x);
785 xd->plane[2].dst.buf = dst_buf[2] +
786 (r >> xd->plane[2].subsampling_y) * dst_stride[2] +
787 (c >> xd->plane[2].subsampling_x);
788
789 if (!b_sub8x8)
Yaowu Xuf883b422016-08-30 14:01:10 -0700790 av1_build_inter_predictors_sb_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700792 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700793#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700794 mi_row_pred, mi_col_pred, bsize_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700795 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700796 av1_build_inter_predictors_sb_sub8x8_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700797#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700798 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700799#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700800 mi_row_pred, mi_col_pred,
801 bsize_pred, block);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700802}
803
Yaowu Xuf883b422016-08-30 14:01:10 -0700804static void dec_extend_dir(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700805 const TileInfo *const tile, int block,
806 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
807 int mi_col, int mi_row_top, int mi_col_top,
808 uint8_t *dst_buf[3], int dst_stride[3], int dir) {
809 // dir: 0-lower, 1-upper, 2-left, 3-right
810 // 4-lowerleft, 5-upperleft, 6-lowerright, 7-upperright
Jingning Han93531242016-12-20 11:54:36 -0800811 const int mi_width = mi_size_wide[bsize];
812 const int mi_height = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700813 int xss = xd->plane[1].subsampling_x;
814 int yss = xd->plane[1].subsampling_y;
Jingning Hanfeb517c2016-12-21 16:02:07 -0800815#if CONFIG_CB4X4
816 const int unify_bsize = 1;
817#else
818 const int unify_bsize = 0;
819#endif
820 int b_sub8x8 = (bsize < BLOCK_8X8) && !unify_bsize ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700821 BLOCK_SIZE extend_bsize;
822 int unit, mi_row_pred, mi_col_pred;
823
824 if (dir == 0 || dir == 1) {
Jingning Han93531242016-12-20 11:54:36 -0800825 extend_bsize =
826 (mi_width == mi_size_wide[BLOCK_8X8] || bsize < BLOCK_8X8 || xss < yss)
827 ? BLOCK_8X8
828 : BLOCK_16X8;
829 unit = mi_size_wide[extend_bsize];
830 mi_row_pred = mi_row + ((dir == 0) ? mi_height : -mi_size_high[BLOCK_8X8]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700831 mi_col_pred = mi_col;
832
833 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
834 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
835 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
836
837 if (mi_width > unit) {
838 int i;
839 assert(!b_sub8x8);
Jingning Han863694a2016-12-21 09:42:24 -0800840 for (i = 0; i < mi_width; i += unit) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700841 mi_col_pred += unit;
842 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
843 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
844 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
845 }
846 }
847 } else if (dir == 2 || dir == 3) {
Jingning Han5b7706a2016-12-21 09:55:10 -0800848 extend_bsize =
849 (mi_height == mi_size_high[BLOCK_8X8] || bsize < BLOCK_8X8 || yss < xss)
850 ? BLOCK_8X8
851 : BLOCK_8X16;
852 unit = mi_size_high[extend_bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700853 mi_row_pred = mi_row;
Jingning Han5b7706a2016-12-21 09:55:10 -0800854 mi_col_pred = mi_col + ((dir == 3) ? mi_width : -mi_size_wide[BLOCK_8X8]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700855
856 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
857 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
858 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
859
860 if (mi_height > unit) {
861 int i;
Jingning Han863694a2016-12-21 09:42:24 -0800862 for (i = 0; i < mi_height; i += unit) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700863 mi_row_pred += unit;
864 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
865 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
866 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
867 }
868 }
869 } else {
870 extend_bsize = BLOCK_8X8;
Jingning Han5b7706a2016-12-21 09:55:10 -0800871 mi_row_pred = mi_row + ((dir == 4 || dir == 6) ? mi_height
872 : -mi_size_high[BLOCK_8X8]);
873 mi_col_pred =
874 mi_col + ((dir == 6 || dir == 7) ? mi_width : -mi_size_wide[BLOCK_8X8]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700875 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
876 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
877 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
878 }
879}
880
Yaowu Xuf883b422016-08-30 14:01:10 -0700881static void dec_extend_all(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700882 const TileInfo *const tile, int block,
883 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
884 int mi_col, int mi_row_top, int mi_col_top,
885 uint8_t *dst_buf[3], int dst_stride[3]) {
886 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
887 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
888 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
889 mi_row_top, mi_col_top, dst_buf, dst_stride, 1);
890 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
891 mi_row_top, mi_col_top, dst_buf, dst_stride, 2);
892 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
893 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
894 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
895 mi_row_top, mi_col_top, dst_buf, dst_stride, 4);
896 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
897 mi_row_top, mi_col_top, dst_buf, dst_stride, 5);
898 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
899 mi_row_top, mi_col_top, dst_buf, dst_stride, 6);
900 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
901 mi_row_top, mi_col_top, dst_buf, dst_stride, 7);
902}
903
Yaowu Xuf883b422016-08-30 14:01:10 -0700904static void dec_predict_sb_complex(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700905 const TileInfo *const tile, int mi_row,
906 int mi_col, int mi_row_top, int mi_col_top,
907 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize,
908 uint8_t *dst_buf[3], int dst_stride[3]) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700909 const AV1_COMMON *const cm = &pbi->common;
Jingning Han5b7706a2016-12-21 09:55:10 -0800910 const int hbs = mi_size_wide[bsize] / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700911 const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
912 const BLOCK_SIZE subsize = get_subsize(bsize, partition);
913#if CONFIG_EXT_PARTITION_TYPES
914 const BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
915#endif
916 int i;
917 const int mi_offset = mi_row * cm->mi_stride + mi_col;
918 uint8_t *dst_buf1[3], *dst_buf2[3], *dst_buf3[3];
Jingning Hanfeb517c2016-12-21 16:02:07 -0800919#if CONFIG_CB4X4
920 const int unify_bsize = 1;
921#else
922 const int unify_bsize = 0;
923#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700924
925 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
926 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
927 DECLARE_ALIGNED(16, uint8_t, tmp_buf3[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
928 int dst_stride1[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
929 int dst_stride2[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
930 int dst_stride3[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
931
Yaowu Xuf883b422016-08-30 14:01:10 -0700932#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700933 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
934 int len = sizeof(uint16_t);
935 dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
936 dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_TX_SQUARE * len);
937 dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_TX_SQUARE * len);
938 dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
939 dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_TX_SQUARE * len);
940 dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_TX_SQUARE * len);
941 dst_buf3[0] = CONVERT_TO_BYTEPTR(tmp_buf3);
942 dst_buf3[1] = CONVERT_TO_BYTEPTR(tmp_buf3 + MAX_TX_SQUARE * len);
943 dst_buf3[2] = CONVERT_TO_BYTEPTR(tmp_buf3 + 2 * MAX_TX_SQUARE * len);
944 } else {
945#endif
946 dst_buf1[0] = tmp_buf1;
947 dst_buf1[1] = tmp_buf1 + MAX_TX_SQUARE;
948 dst_buf1[2] = tmp_buf1 + 2 * MAX_TX_SQUARE;
949 dst_buf2[0] = tmp_buf2;
950 dst_buf2[1] = tmp_buf2 + MAX_TX_SQUARE;
951 dst_buf2[2] = tmp_buf2 + 2 * MAX_TX_SQUARE;
952 dst_buf3[0] = tmp_buf3;
953 dst_buf3[1] = tmp_buf3 + MAX_TX_SQUARE;
954 dst_buf3[2] = tmp_buf3 + 2 * MAX_TX_SQUARE;
Yaowu Xuf883b422016-08-30 14:01:10 -0700955#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700956 }
957#endif
958
959 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
960
961 xd->mi = cm->mi_grid_visible + mi_offset;
962 xd->mi[0] = cm->mi + mi_offset;
963
964 for (i = 0; i < MAX_MB_PLANE; i++) {
965 xd->plane[i].dst.buf = dst_buf[i];
966 xd->plane[i].dst.stride = dst_stride[i];
967 }
968
969 switch (partition) {
970 case PARTITION_NONE:
971 assert(bsize < top_bsize);
972 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
973 mi_row_top, mi_col_top, dst_buf, dst_stride,
974 top_bsize, bsize, 0, 0);
975 dec_extend_all(pbi, xd, tile, 0, bsize, top_bsize, mi_row, mi_col,
976 mi_row_top, mi_col_top, dst_buf, dst_stride);
977 break;
978 case PARTITION_HORZ:
Jingning Hanfeb517c2016-12-21 16:02:07 -0800979 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700980 // For sub8x8, predict in 8x8 unit
981 // First half
982 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
983 mi_row_top, mi_col_top, dst_buf, dst_stride,
984 top_bsize, BLOCK_8X8, 1, 0);
985 if (bsize < top_bsize)
986 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
987 mi_row_top, mi_col_top, dst_buf, dst_stride);
988
989 // Second half
990 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
991 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
992 top_bsize, BLOCK_8X8, 1, 1);
993 if (bsize < top_bsize)
994 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
995 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
996
997 // weighted average to smooth the boundary
998 xd->plane[0].dst.buf = dst_buf[0];
999 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001000 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001001 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1002 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1003 0);
1004 } else {
1005 // First half
1006 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1007 mi_row_top, mi_col_top, dst_buf, dst_stride,
1008 top_bsize, subsize, 0, 0);
1009 if (bsize < top_bsize)
1010 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1011 mi_row_top, mi_col_top, dst_buf, dst_stride);
1012 else
1013 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1014 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1015
1016 if (mi_row + hbs < cm->mi_rows) {
1017 // Second half
1018 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col,
1019 mi_row + hbs, mi_col, mi_row_top, mi_col_top,
1020 dst_buf1, dst_stride1, top_bsize, subsize, 0, 0);
1021 if (bsize < top_bsize)
1022 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1023 mi_col, mi_row_top, mi_col_top, dst_buf1,
1024 dst_stride1);
1025 else
1026 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1027 mi_col, mi_row_top, mi_col_top, dst_buf1,
1028 dst_stride1, 1);
1029
1030 // weighted average to smooth the boundary
1031 for (i = 0; i < MAX_MB_PLANE; i++) {
1032 xd->plane[i].dst.buf = dst_buf[i];
1033 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001034 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001035 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1036 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1037 PARTITION_HORZ, i);
1038 }
1039 }
1040 }
1041 break;
1042 case PARTITION_VERT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001043 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001044 // First half
1045 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1046 mi_row_top, mi_col_top, dst_buf, dst_stride,
1047 top_bsize, BLOCK_8X8, 1, 0);
1048 if (bsize < top_bsize)
1049 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1050 mi_row_top, mi_col_top, dst_buf, dst_stride);
1051
1052 // Second half
1053 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1054 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1055 top_bsize, BLOCK_8X8, 1, 1);
1056 if (bsize < top_bsize)
1057 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1058 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1059
1060 // Smooth
1061 xd->plane[0].dst.buf = dst_buf[0];
1062 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001063 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001064 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1065 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1066 0);
1067 } else {
1068 // First half
1069 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1070 mi_row_top, mi_col_top, dst_buf, dst_stride,
1071 top_bsize, subsize, 0, 0);
1072 if (bsize < top_bsize)
1073 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1074 mi_row_top, mi_col_top, dst_buf, dst_stride);
1075 else
1076 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1077 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1078
1079 // Second half
1080 if (mi_col + hbs < cm->mi_cols) {
1081 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1082 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1083 dst_stride1, top_bsize, subsize, 0, 0);
1084 if (bsize < top_bsize)
1085 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1086 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1087 dst_stride1);
1088 else
1089 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1090 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1091 dst_stride1, 2);
1092
1093 // Smooth
1094 for (i = 0; i < MAX_MB_PLANE; i++) {
1095 xd->plane[i].dst.buf = dst_buf[i];
1096 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001097 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001098 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1099 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1100 PARTITION_VERT, i);
1101 }
1102 }
1103 }
1104 break;
1105 case PARTITION_SPLIT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001106 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001107 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1108 mi_row_top, mi_col_top, dst_buf, dst_stride,
1109 top_bsize, BLOCK_8X8, 1, 0);
1110 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1111 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1112 top_bsize, BLOCK_8X8, 1, 1);
1113 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1114 mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1115 top_bsize, BLOCK_8X8, 1, 1);
1116 dec_predict_b_extend(pbi, xd, tile, 3, mi_row, mi_col, mi_row, mi_col,
1117 mi_row_top, mi_col_top, dst_buf3, dst_stride3,
1118 top_bsize, BLOCK_8X8, 1, 1);
1119 if (bsize < top_bsize) {
1120 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1121 mi_row_top, mi_col_top, dst_buf, dst_stride);
1122 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1123 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1124 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1125 mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1126 dec_extend_all(pbi, xd, tile, 3, subsize, top_bsize, mi_row, mi_col,
1127 mi_row_top, mi_col_top, dst_buf3, dst_stride3);
1128 }
1129 } else {
1130 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row_top,
1131 mi_col_top, subsize, top_bsize, dst_buf,
1132 dst_stride);
1133 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1134 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col + hbs,
1135 mi_row_top, mi_col_top, subsize, top_bsize,
1136 dst_buf1, dst_stride1);
1137 if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols)
1138 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col,
1139 mi_row_top, mi_col_top, subsize, top_bsize,
1140 dst_buf2, dst_stride2);
1141 if (mi_row + hbs < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1142 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col + hbs,
1143 mi_row_top, mi_col_top, subsize, top_bsize,
1144 dst_buf3, dst_stride3);
1145 }
1146 for (i = 0; i < MAX_MB_PLANE; i++) {
Jingning Hanfeb517c2016-12-21 16:02:07 -08001147 if (bsize == BLOCK_8X8 && i != 0 && !unify_bsize)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001148 continue; // Skip <4x4 chroma smoothing
1149 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001150 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001151 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1152 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1153 PARTITION_VERT, i);
1154 if (mi_row + hbs < cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001155 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001156 xd, dst_buf2[i], dst_stride2[i], dst_buf3[i], dst_stride3[i],
1157 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1158 PARTITION_VERT, i);
Yaowu Xuf883b422016-08-30 14:01:10 -07001159 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001160 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1161 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1162 PARTITION_HORZ, i);
1163 }
1164 } else if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001165 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001166 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1167 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1168 PARTITION_HORZ, i);
1169 }
1170 }
1171 break;
1172#if CONFIG_EXT_PARTITION_TYPES
1173 case PARTITION_HORZ_A:
1174 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1175 mi_row_top, mi_col_top, dst_buf, dst_stride,
1176 top_bsize, bsize2, 0, 0);
1177 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1178 mi_row_top, mi_col_top, dst_buf, dst_stride);
1179
1180 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1181 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1182 dst_stride1, top_bsize, bsize2, 0, 0);
1183 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1184 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1185
1186 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1187 mi_col, mi_row_top, mi_col_top, dst_buf2,
1188 dst_stride2, top_bsize, subsize, 0, 0);
1189 if (bsize < top_bsize)
1190 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1191 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1192 else
1193 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1194 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1195 1);
1196
1197 for (i = 0; i < MAX_MB_PLANE; i++) {
1198 xd->plane[i].dst.buf = dst_buf[i];
1199 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001200 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001201 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1202 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1203 i);
1204 }
1205 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001206 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001207 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1208 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1209 i);
1210 }
1211 break;
1212 case PARTITION_VERT_A:
1213
1214 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1215 mi_row_top, mi_col_top, dst_buf, dst_stride,
1216 top_bsize, bsize2, 0, 0);
1217 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1218 mi_row_top, mi_col_top, dst_buf, dst_stride);
1219
1220 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1221 mi_col, mi_row_top, mi_col_top, dst_buf1,
1222 dst_stride1, top_bsize, bsize2, 0, 0);
1223 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1224 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1225
1226 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1227 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1228 dst_stride2, top_bsize, subsize, 0, 0);
1229 if (bsize < top_bsize)
1230 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1231 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1232 dst_stride2);
1233 else
1234 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1235 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1236 dst_stride2, 2);
1237
1238 for (i = 0; i < MAX_MB_PLANE; i++) {
1239 xd->plane[i].dst.buf = dst_buf[i];
1240 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001241 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001242 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1243 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1244 i);
1245 }
1246 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001247 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001248 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1249 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1250 i);
1251 }
1252 break;
1253 case PARTITION_HORZ_B:
1254 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1255 mi_row_top, mi_col_top, dst_buf, dst_stride,
1256 top_bsize, subsize, 0, 0);
1257 if (bsize < top_bsize)
1258 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1259 mi_row_top, mi_col_top, dst_buf, dst_stride);
1260 else
1261 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1262 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1263
1264 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1265 mi_col, mi_row_top, mi_col_top, dst_buf1,
1266 dst_stride1, top_bsize, bsize2, 0, 0);
1267 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1268 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1269
1270 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1271 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1272 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1273 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1274 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1275 dst_stride2);
1276
1277 for (i = 0; i < MAX_MB_PLANE; i++) {
1278 xd->plane[i].dst.buf = dst_buf1[i];
1279 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001280 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001281 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1282 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1283 PARTITION_VERT, i);
1284 }
1285 for (i = 0; i < MAX_MB_PLANE; i++) {
1286 xd->plane[i].dst.buf = dst_buf[i];
1287 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001288 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001289 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1290 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1291 i);
1292 }
1293 break;
1294 case PARTITION_VERT_B:
1295 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1296 mi_row_top, mi_col_top, dst_buf, dst_stride,
1297 top_bsize, subsize, 0, 0);
1298 if (bsize < top_bsize)
1299 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1300 mi_row_top, mi_col_top, dst_buf, dst_stride);
1301 else
1302 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1303 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1304
1305 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1306 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1307 dst_stride1, top_bsize, bsize2, 0, 0);
1308 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1309 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1310
1311 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1312 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1313 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1314 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1315 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1316 dst_stride2);
1317
1318 for (i = 0; i < MAX_MB_PLANE; i++) {
1319 xd->plane[i].dst.buf = dst_buf1[i];
1320 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001321 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001322 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1323 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1324 PARTITION_HORZ, i);
1325 }
1326 for (i = 0; i < MAX_MB_PLANE; i++) {
1327 xd->plane[i].dst.buf = dst_buf[i];
1328 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001329 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001330 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1331 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1332 i);
1333 }
1334 break;
1335#endif // CONFIG_EXT_PARTITION_TYPES
1336 default: assert(0);
1337 }
1338}
1339
Yaowu Xuf883b422016-08-30 14:01:10 -07001340static void set_segment_id_supertx(const AV1_COMMON *const cm, const int mi_row,
1341 const int mi_col, const BLOCK_SIZE bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001342 const struct segmentation *seg = &cm->seg;
Jingning Han5b7706a2016-12-21 09:55:10 -08001343 const int miw = AOMMIN(mi_size_wide[bsize], cm->mi_cols - mi_col);
1344 const int mih = AOMMIN(mi_size_high[bsize], cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001345 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1346 MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
1347 int r, c;
1348 int seg_id_supertx = MAX_SEGMENTS;
1349
1350 if (!seg->enabled) {
1351 seg_id_supertx = 0;
1352 } else {
1353 // Find the minimum segment_id
1354 for (r = 0; r < mih; r++)
1355 for (c = 0; c < miw; c++)
1356 seg_id_supertx =
Yaowu Xuf883b422016-08-30 14:01:10 -07001357 AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001358 assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);
1359 }
1360
1361 // Assign the the segment_id back to segment_id_supertx
1362 for (r = 0; r < mih; r++)
1363 for (c = 0; c < miw; c++)
1364 mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
1365}
1366#endif // CONFIG_SUPERTX
1367
Yaowu Xuf883b422016-08-30 14:01:10 -07001368static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001369#if CONFIG_SUPERTX
1370 int supertx_enabled,
1371#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001372 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001373#if CONFIG_EXT_PARTITION_TYPES
1374 PARTITION_TYPE partition,
1375#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001376 BLOCK_SIZE bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001377 AV1_COMMON *const cm = &pbi->common;
Jingning Han85dc03f2016-12-06 16:03:10 -08001378 const int bw = mi_size_wide[bsize];
1379 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -07001380 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1381 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Nathan E. Eggeebbd4792016-10-05 19:30:15 -04001382 MB_MODE_INFO *mbmi;
1383
Michael Bebenita6048d052016-08-25 14:40:54 -07001384#if CONFIG_ACCOUNTING
1385 aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
1386#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001387#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001388 if (supertx_enabled) {
1389 mbmi = set_mb_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
1390 } else {
Jingning Hanfaad0e12016-12-07 10:54:57 -08001391 mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001392 }
1393#if CONFIG_EXT_PARTITION_TYPES
1394 xd->mi[0]->mbmi.partition = partition;
1395#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001396 av1_read_mode_info(pbi, xd, supertx_enabled, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001397#else
Jingning Hanfaad0e12016-12-07 10:54:57 -08001398 mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001399#if CONFIG_EXT_PARTITION_TYPES
1400 xd->mi[0]->mbmi.partition = partition;
1401#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001402 av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001403#endif // CONFIG_SUPERTX
1404
1405 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1406 const BLOCK_SIZE uv_subsize =
1407 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1408 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -07001409 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001410 "Invalid block size.");
1411 }
1412
1413#if CONFIG_SUPERTX
1414 mbmi->segment_id_supertx = MAX_SEGMENTS;
1415
1416 if (supertx_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001417 xd->corrupted |= aom_reader_has_error(r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001418 return;
1419 }
1420#endif // CONFIG_SUPERTX
1421
Arild Fuldseth07441162016-08-15 15:07:52 +02001422#if CONFIG_DELTA_Q
1423 if (cm->delta_q_present_flag) {
1424 int i;
1425 for (i = 0; i < MAX_SEGMENTS; i++) {
1426 xd->plane[0].seg_dequant[i][0] =
1427 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
1428 xd->plane[0].seg_dequant[i][1] =
1429 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
1430 xd->plane[1].seg_dequant[i][0] =
1431 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1432 xd->plane[1].seg_dequant[i][1] =
1433 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1434 xd->plane[2].seg_dequant[i][0] =
1435 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1436 xd->plane[2].seg_dequant[i][1] =
1437 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1438 }
1439 }
1440#endif
1441
Jingning Han41bb3392016-12-14 10:46:48 -08001442#if CONFIG_CB4X4
1443 if (mbmi->skip) reset_skip_context(xd, bsize);
1444#else
Jingning Hand39cc722016-12-02 14:03:26 -08001445 if (mbmi->skip) reset_skip_context(xd, AOMMAX(BLOCK_8X8, bsize));
Jingning Han41bb3392016-12-14 10:46:48 -08001446#endif
Jingning Hand39cc722016-12-02 14:03:26 -08001447
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001448#if CONFIG_COEF_INTERLEAVE
1449 {
1450 const struct macroblockd_plane *const pd_y = &xd->plane[0];
1451 const struct macroblockd_plane *const pd_c = &xd->plane[1];
1452 const TX_SIZE tx_log2_y = mbmi->tx_size;
1453 const TX_SIZE tx_log2_c = get_uv_tx_size(mbmi, pd_c);
1454 const int tx_sz_y = (1 << tx_log2_y);
1455 const int tx_sz_c = (1 << tx_log2_c);
1456 const int num_4x4_w_y = pd_y->n4_w;
1457 const int num_4x4_h_y = pd_y->n4_h;
1458 const int num_4x4_w_c = pd_c->n4_w;
1459 const int num_4x4_h_c = pd_c->n4_h;
1460 const int max_4x4_w_y = get_max_4x4_size(num_4x4_w_y, xd->mb_to_right_edge,
1461 pd_y->subsampling_x);
1462 const int max_4x4_h_y = get_max_4x4_size(num_4x4_h_y, xd->mb_to_bottom_edge,
1463 pd_y->subsampling_y);
1464 const int max_4x4_w_c = get_max_4x4_size(num_4x4_w_c, xd->mb_to_right_edge,
1465 pd_c->subsampling_x);
1466 const int max_4x4_h_c = get_max_4x4_size(num_4x4_h_c, xd->mb_to_bottom_edge,
1467 pd_c->subsampling_y);
1468
1469 // The max_4x4_w/h may be smaller than tx_sz under some corner cases,
1470 // i.e. when the SB is splitted by tile boundaries.
1471 const int tu_num_w_y = (max_4x4_w_y + tx_sz_y - 1) / tx_sz_y;
1472 const int tu_num_h_y = (max_4x4_h_y + tx_sz_y - 1) / tx_sz_y;
1473 const int tu_num_w_c = (max_4x4_w_c + tx_sz_c - 1) / tx_sz_c;
1474 const int tu_num_h_c = (max_4x4_h_c + tx_sz_c - 1) / tx_sz_c;
1475 const int tu_num_y = tu_num_w_y * tu_num_h_y;
1476 const int tu_num_c = tu_num_w_c * tu_num_h_c;
1477
1478 if (!is_inter_block(mbmi)) {
1479 int tu_idx_c = 0;
1480 int row_y, col_y, row_c, col_c;
1481 int plane;
1482
1483#if CONFIG_PALETTE
1484 for (plane = 0; plane <= 1; ++plane) {
1485 if (mbmi->palette_mode_info.palette_size[plane])
1486 av1_decode_palette_tokens(xd, plane, r);
1487 }
1488#endif
1489
1490 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1491 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1492 // luma
1493 predict_and_reconstruct_intra_block(
1494 cm, xd, r, mbmi, 0, row_y * tx_sz_y, col_y * tx_sz_y, tx_log2_y);
1495 // chroma
1496 if (tu_idx_c < tu_num_c) {
1497 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1498 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1499 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c,
1500 col_c, tx_log2_c);
1501 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c,
1502 col_c, tx_log2_c);
1503 tu_idx_c++;
1504 }
1505 }
1506 }
1507
1508 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1509 while (tu_idx_c < tu_num_c) {
1510 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1511 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1512 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c, col_c,
1513 tx_log2_c);
1514 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c, col_c,
1515 tx_log2_c);
1516 tu_idx_c++;
1517 }
1518 } else {
1519 // Prediction
David Barkerac37fa32016-12-02 12:30:21 +00001520 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001521 AOMMAX(bsize, BLOCK_8X8));
1522
1523 // Reconstruction
1524 if (!mbmi->skip) {
1525 int eobtotal = 0;
1526 int tu_idx_c = 0;
1527 int row_y, col_y, row_c, col_c;
1528
1529 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1530 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1531 // luma
1532 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 0,
1533 row_y * tx_sz_y,
1534 col_y * tx_sz_y, tx_log2_y);
1535 // chroma
1536 if (tu_idx_c < tu_num_c) {
1537 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1538 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1539 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1540 1, row_c, col_c, tx_log2_c);
1541 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1542 2, row_c, col_c, tx_log2_c);
1543 tu_idx_c++;
1544 }
1545 }
1546 }
1547
1548 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1549 while (tu_idx_c < tu_num_c) {
1550 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1551 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1552 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 1,
1553 row_c, col_c, tx_log2_c);
1554 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 2,
1555 row_c, col_c, tx_log2_c);
1556 tu_idx_c++;
1557 }
1558
1559 if (bsize >= BLOCK_8X8 && eobtotal == 0)
1560#if CONFIG_MISC_FIXES
1561 mbmi->has_no_coeffs = 1;
1562#else
1563 mbmi->skip = 1;
1564#endif
1565 }
1566 }
1567 }
1568#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001569 if (!is_inter_block(mbmi)) {
1570 int plane;
Urvang Joshib100db72016-10-12 16:28:56 -07001571#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001572 for (plane = 0; plane <= 1; ++plane) {
1573 if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xuf883b422016-08-30 14:01:10 -07001574 av1_decode_palette_tokens(xd, plane, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001575 }
Urvang Joshib100db72016-10-12 16:28:56 -07001576#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001577 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1578 const struct macroblockd_plane *const pd = &xd->plane[plane];
Debargha Mukherjee2f123402016-08-30 17:43:38 -07001579 const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han2d64f122016-10-21 12:44:29 -07001580 const int stepr = tx_size_high_unit[tx_size];
1581 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han41bb3392016-12-14 10:46:48 -08001582#if CONFIG_CB4X4
1583 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1584#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001585 const BLOCK_SIZE plane_bsize =
1586 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001587#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001588 int row, col;
Jingning Hanbafee8d2016-12-02 10:25:03 -08001589 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1590 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001591
1592 for (row = 0; row < max_blocks_high; row += stepr)
1593 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001594 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane, row, col,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001595 tx_size);
1596 }
1597 } else {
Yue Chen69f18e12016-09-08 14:48:15 -07001598// Prediction
1599#if CONFIG_WARPED_MOTION
1600 if (mbmi->motion_mode == WARPED_CAUSAL) {
1601 int i;
1602#if CONFIG_AOM_HIGHBITDEPTH
1603 int use_hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH;
1604#endif // CONFIG_AOM_HIGHBITDEPTH
1605
1606 for (i = 0; i < 3; ++i) {
1607 const struct macroblockd_plane *pd = &xd->plane[i];
1608
1609 av1_warp_plane(&mbmi->wm_params[0],
1610#if CONFIG_AOM_HIGHBITDEPTH
1611 xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH, xd->bd,
1612#endif // CONFIG_AOM_HIGHBITDEPTH
1613 pd->pre[0].buf0, pd->pre[0].width, pd->pre[0].height,
1614 pd->pre[0].stride, pd->dst.buf,
1615 ((mi_col * MI_SIZE) >> pd->subsampling_x),
1616 ((mi_row * MI_SIZE) >> pd->subsampling_y),
Jingning Hanff6ee6a2016-12-07 09:55:21 -08001617 xd->n8_w * (MI_SIZE >> pd->subsampling_x),
1618 xd->n8_h * (MI_SIZE >> pd->subsampling_y),
1619 pd->dst.stride, pd->subsampling_x, pd->subsampling_y, 16,
1620 16, 0);
Yue Chen69f18e12016-09-08 14:48:15 -07001621 }
1622 } else {
1623#endif // CONFIG_WARPED_MOTION
Jingning Han41bb3392016-12-14 10:46:48 -08001624#if CONFIG_CB4X4
1625 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL, bsize);
1626#else
1627 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
1628 AOMMAX(bsize, BLOCK_8X8));
1629#endif
Yue Chen69f18e12016-09-08 14:48:15 -07001630#if CONFIG_WARPED_MOTION
1631 }
1632#endif // CONFIG_WARPED_MOTION
Yue Chencb60b182016-10-13 15:18:22 -07001633#if CONFIG_MOTION_VAR
1634 if (mbmi->motion_mode == OBMC_CAUSAL) {
Yue Chen894fcce2016-10-21 16:50:52 -07001635 av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001636 }
Yue Chencb60b182016-10-13 15:18:22 -07001637#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001638
1639 // Reconstruction
1640 if (!mbmi->skip) {
1641 int eobtotal = 0;
1642 int plane;
1643
1644 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1645 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han41bb3392016-12-14 10:46:48 -08001646#if CONFIG_CB4X4
1647 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1648#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001649 const BLOCK_SIZE plane_bsize =
1650 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001651#endif
Jingning Hanbafee8d2016-12-02 10:25:03 -08001652 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1653 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001654 int row, col;
1655#if CONFIG_VAR_TX
Jingning Han70e5f3f2016-11-09 17:03:07 -08001656 const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07001657 const int bh_var_tx = tx_size_high_unit[max_tx_size];
1658 const int bw_var_tx = tx_size_wide_unit[max_tx_size];
Jingning Hanfe45b212016-11-22 10:30:23 -08001659
Jingning Hanbafee8d2016-12-02 10:25:03 -08001660 for (row = 0; row < max_blocks_high; row += bh_var_tx)
1661 for (col = 0; col < max_blocks_wide; col += bw_var_tx)
Jingning Hanfe45b212016-11-22 10:30:23 -08001662 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, row, col,
1663 max_tx_size, &eobtotal);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001664#else
1665 const TX_SIZE tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -07001666 plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han2d64f122016-10-21 12:44:29 -07001667 const int stepr = tx_size_high_unit[tx_size];
1668 const int stepc = tx_size_wide_unit[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001669 for (row = 0; row < max_blocks_high; row += stepr)
1670 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001671 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1672 plane, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001673#endif
1674 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001675 }
1676 }
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001677#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001678
Yaowu Xuf883b422016-08-30 14:01:10 -07001679 xd->corrupted |= aom_reader_has_error(r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001680}
1681
Yaowu Xuf883b422016-08-30 14:01:10 -07001682static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
1683 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001684 int has_rows, int has_cols,
Jingning Han1beb0102016-12-07 11:08:30 -08001685 BLOCK_SIZE bsize) {
1686 const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
Yaowu Xuf883b422016-08-30 14:01:10 -07001687 const aom_prob *const probs = cm->fc->partition_prob[ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001688 FRAME_COUNTS *counts = xd->counts;
1689 PARTITION_TYPE p;
1690
1691 if (has_rows && has_cols)
1692#if CONFIG_EXT_PARTITION_TYPES
1693 if (bsize <= BLOCK_8X8)
Michael Bebenita6048d052016-08-25 14:40:54 -07001694 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001695 else
Michael Bebenita6048d052016-08-25 14:40:54 -07001696 p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs,
1697 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001698#else
Nathan E. Egge9d9eb6c2016-11-16 09:44:26 -05001699#if CONFIG_EC_MULTISYMBOL
Yaowu Xuf2581a32016-10-20 13:05:47 -07001700 p = (PARTITION_TYPE)aom_read_symbol(r, cm->fc->partition_cdf[ctx],
1701 PARTITION_TYPES, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001702#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001703 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001704#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001705#endif // CONFIG_EXT_PARTITION_TYPES
1706 else if (!has_rows && has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001707 p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001708 else if (has_rows && !has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001709 p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001710 else
1711 p = PARTITION_SPLIT;
1712
1713 if (counts) ++counts->partition[ctx][p];
1714
1715 return p;
1716}
1717
1718#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001719static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
1720 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001721 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
1722 return 1;
1723 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001724 const int ctx = av1_get_skip_context(xd);
Michael Bebenita6048d052016-08-25 14:40:54 -07001725 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001726 FRAME_COUNTS *counts = xd->counts;
1727 if (counts) ++counts->skip[ctx][skip];
1728 return skip;
1729 }
1730}
1731#endif // CONFIG_SUPERTX
Yaowu Xud71be782016-10-14 08:47:03 -07001732#if CONFIG_CLPF
1733static int clpf_all_skip(const AV1_COMMON *cm, int mi_col, int mi_row,
1734 int size) {
1735 int r, c;
1736 int skip = 1;
1737 const int maxc = AOMMIN(size, cm->mi_cols - mi_col);
1738 const int maxr = AOMMIN(size, cm->mi_rows - mi_row);
1739 for (r = 0; r < maxr && skip; r++) {
1740 for (c = 0; c < maxc && skip; c++) {
1741 skip &= !!cm->mi_grid_visible[(mi_row + r) * cm->mi_stride + mi_col + c]
1742 ->mbmi.skip;
1743 }
1744 }
1745 return skip;
1746}
1747#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001748
1749// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -07001750static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001751#if CONFIG_SUPERTX
1752 int supertx_enabled,
1753#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001754 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001755 BLOCK_SIZE bsize, int n4x4_l2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001756 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001757 const int n8x8_l2 = n4x4_l2 - 1;
Jingning Hanff17e162016-12-07 17:58:18 -08001758 const int num_8x8_wh = mi_size_wide[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001759 const int hbs = num_8x8_wh >> 1;
Jingning Han41bb3392016-12-14 10:46:48 -08001760#if CONFIG_CB4X4
1761 const int unify_bsize = 1;
1762#else
1763 const int unify_bsize = 0;
1764#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001765 PARTITION_TYPE partition;
1766 BLOCK_SIZE subsize;
1767#if CONFIG_EXT_PARTITION_TYPES
1768 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1769#endif
1770 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1771 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1772#if CONFIG_SUPERTX
1773 const int read_token = !supertx_enabled;
1774 int skip = 0;
Jingning Han5b7706a2016-12-21 09:55:10 -08001775 TX_SIZE supertx_size = b_width_log2_lookup[bsize] + CONFIG_CB4X4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001776 const TileInfo *const tile = &xd->tile;
1777 int txfm = DCT_DCT;
1778#endif // CONFIG_SUPERTX
1779
1780 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1781
Jingning Han6dbacf22016-12-14 17:28:35 -08001782 partition = (n4x4_l2 == 0) ? PARTITION_NONE
1783 : read_partition(cm, xd, mi_row, mi_col, r,
1784 has_rows, has_cols, bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001785 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
Yushin Cho77bba8d2016-11-04 16:36:56 -07001786
1787#if CONFIG_PVQ
1788 assert(partition < PARTITION_TYPES);
1789 assert(subsize < BLOCK_SIZES);
1790#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001791#if CONFIG_SUPERTX
1792 if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
1793 bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
1794 const int supertx_context = partition_supertx_context_lookup[partition];
Michael Bebenita6048d052016-08-25 14:40:54 -07001795 supertx_enabled = aom_read(
1796 r, cm->fc->supertx_prob[supertx_context][supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001797 if (xd->counts)
1798 xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
1799#if CONFIG_VAR_TX
1800 if (supertx_enabled) xd->supertx_size = supertx_size;
1801#endif
1802 }
1803#endif // CONFIG_SUPERTX
Jingning Han41bb3392016-12-14 10:46:48 -08001804 if (!hbs && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001805 // calculate bmode block dimensions (log 2)
1806 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1807 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1808 decode_block(pbi, xd,
1809#if CONFIG_SUPERTX
1810 supertx_enabled,
1811#endif // CONFIG_SUPERTX
1812 mi_row, mi_col, r,
1813#if CONFIG_EXT_PARTITION_TYPES
1814 partition,
1815#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001816 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001817 } else {
1818 switch (partition) {
1819 case PARTITION_NONE:
1820 decode_block(pbi, xd,
1821#if CONFIG_SUPERTX
1822 supertx_enabled,
1823#endif // CONFIG_SUPERTX
1824 mi_row, mi_col, r,
1825#if CONFIG_EXT_PARTITION_TYPES
1826 partition,
1827#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001828 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001829 break;
1830 case PARTITION_HORZ:
1831 decode_block(pbi, xd,
1832#if CONFIG_SUPERTX
1833 supertx_enabled,
1834#endif // CONFIG_SUPERTX
1835 mi_row, mi_col, r,
1836#if CONFIG_EXT_PARTITION_TYPES
1837 partition,
1838#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001839 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001840 if (has_rows)
1841 decode_block(pbi, xd,
1842#if CONFIG_SUPERTX
1843 supertx_enabled,
1844#endif // CONFIG_SUPERTX
1845 mi_row + hbs, mi_col, r,
1846#if CONFIG_EXT_PARTITION_TYPES
1847 partition,
1848#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001849 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001850 break;
1851 case PARTITION_VERT:
1852 decode_block(pbi, xd,
1853#if CONFIG_SUPERTX
1854 supertx_enabled,
1855#endif // CONFIG_SUPERTX
1856 mi_row, mi_col, r,
1857#if CONFIG_EXT_PARTITION_TYPES
1858 partition,
1859#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001860 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001861 if (has_cols)
1862 decode_block(pbi, xd,
1863#if CONFIG_SUPERTX
1864 supertx_enabled,
1865#endif // CONFIG_SUPERTX
1866 mi_row, mi_col + hbs, r,
1867#if CONFIG_EXT_PARTITION_TYPES
1868 partition,
1869#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001870 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001871 break;
1872 case PARTITION_SPLIT:
1873 decode_partition(pbi, xd,
1874#if CONFIG_SUPERTX
1875 supertx_enabled,
1876#endif // CONFIG_SUPERTX
1877 mi_row, mi_col, r, subsize, n8x8_l2);
1878 decode_partition(pbi, xd,
1879#if CONFIG_SUPERTX
1880 supertx_enabled,
1881#endif // CONFIG_SUPERTX
1882 mi_row, mi_col + hbs, r, subsize, n8x8_l2);
1883 decode_partition(pbi, xd,
1884#if CONFIG_SUPERTX
1885 supertx_enabled,
1886#endif // CONFIG_SUPERTX
1887 mi_row + hbs, mi_col, r, subsize, n8x8_l2);
1888 decode_partition(pbi, xd,
1889#if CONFIG_SUPERTX
1890 supertx_enabled,
1891#endif // CONFIG_SUPERTX
1892 mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
1893 break;
1894#if CONFIG_EXT_PARTITION_TYPES
1895 case PARTITION_HORZ_A:
1896 decode_block(pbi, xd,
1897#if CONFIG_SUPERTX
1898 supertx_enabled,
1899#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001900 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001901 decode_block(pbi, xd,
1902#if CONFIG_SUPERTX
1903 supertx_enabled,
1904#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001905 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001906 decode_block(pbi, xd,
1907#if CONFIG_SUPERTX
1908 supertx_enabled,
1909#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001910 mi_row + hbs, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001911 break;
1912 case PARTITION_HORZ_B:
1913 decode_block(pbi, xd,
1914#if CONFIG_SUPERTX
1915 supertx_enabled,
1916#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001917 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001918 decode_block(pbi, xd,
1919#if CONFIG_SUPERTX
1920 supertx_enabled,
1921#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001922 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001923 decode_block(pbi, xd,
1924#if CONFIG_SUPERTX
1925 supertx_enabled,
1926#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001927 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001928 break;
1929 case PARTITION_VERT_A:
1930 decode_block(pbi, xd,
1931#if CONFIG_SUPERTX
1932 supertx_enabled,
1933#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001934 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001935 decode_block(pbi, xd,
1936#if CONFIG_SUPERTX
1937 supertx_enabled,
1938#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001939 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001940 decode_block(pbi, xd,
1941#if CONFIG_SUPERTX
1942 supertx_enabled,
1943#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001944 mi_row, mi_col + hbs, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001945 break;
1946 case PARTITION_VERT_B:
1947 decode_block(pbi, xd,
1948#if CONFIG_SUPERTX
1949 supertx_enabled,
1950#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001951 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001952 decode_block(pbi, xd,
1953#if CONFIG_SUPERTX
1954 supertx_enabled,
1955#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001956 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001957 decode_block(pbi, xd,
1958#if CONFIG_SUPERTX
1959 supertx_enabled,
1960#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001961 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001962 break;
1963#endif
1964 default: assert(0 && "Invalid partition type");
1965 }
1966 }
1967
1968#if CONFIG_SUPERTX
1969 if (supertx_enabled && read_token) {
1970 uint8_t *dst_buf[3];
1971 int dst_stride[3], i;
1972 int offset = mi_row * cm->mi_stride + mi_col;
1973
1974 set_segment_id_supertx(cm, mi_row, mi_col, bsize);
1975
1976 xd->mi = cm->mi_grid_visible + offset;
1977 xd->mi[0] = cm->mi + offset;
Jingning Han5b7706a2016-12-21 09:55:10 -08001978 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
1979 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001980 set_skip_context(xd, mi_row, mi_col);
1981 skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
1982 if (skip) {
1983 reset_skip_context(xd, bsize);
1984 } else {
1985#if CONFIG_EXT_TX
1986 if (get_ext_tx_types(supertx_size, bsize, 1) > 1) {
1987 int eset = get_ext_tx_set(supertx_size, bsize, 1);
1988 if (eset > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001989 txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
Michael Bebenita6048d052016-08-25 14:40:54 -07001990 cm->fc->inter_ext_tx_prob[eset][supertx_size],
1991 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001992 if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
1993 }
1994 }
1995#else
1996 if (supertx_size < TX_32X32) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001997 txfm = aom_read_tree(r, av1_ext_tx_tree,
Jingning Han8f6eb182016-10-19 13:48:57 -07001998 cm->fc->inter_ext_tx_prob[supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001999 if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
2000 }
2001#endif // CONFIG_EXT_TX
2002 }
2003
Yaowu Xuf883b422016-08-30 14:01:10 -07002004 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002005 for (i = 0; i < MAX_MB_PLANE; i++) {
2006 dst_buf[i] = xd->plane[i].dst.buf;
2007 dst_stride[i] = xd->plane[i].dst.stride;
2008 }
2009 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
2010 bsize, dst_buf, dst_stride);
2011
2012 if (!skip) {
2013 int eobtotal = 0;
2014 MB_MODE_INFO *mbmi;
2015 set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
2016 mbmi = &xd->mi[0]->mbmi;
2017 mbmi->tx_type = txfm;
2018 assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
2019 for (i = 0; i < MAX_MB_PLANE; ++i) {
2020 const struct macroblockd_plane *const pd = &xd->plane[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002021 int row, col;
Debargha Mukherjee2f123402016-08-30 17:43:38 -07002022 const TX_SIZE tx_size = i ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han5b7706a2016-12-21 09:55:10 -08002023 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Jingning Han32b20282016-10-28 15:42:44 -07002024 const int stepr = tx_size_high_unit[tx_size];
2025 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han5b7706a2016-12-21 09:55:10 -08002026 const int max_blocks_wide = max_block_wide(xd, plane_bsize, i);
2027 const int max_blocks_high = max_block_high(xd, plane_bsize, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002028
2029 for (row = 0; row < max_blocks_high; row += stepr)
2030 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07002031 eobtotal += reconstruct_inter_block(
2032 cm, xd, r, mbmi->segment_id_supertx, i, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002033 }
2034 if (!(subsize < BLOCK_8X8) && eobtotal == 0) skip = 1;
2035 }
2036 set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
2037 }
2038#endif // CONFIG_SUPERTX
2039
2040#if CONFIG_EXT_PARTITION_TYPES
2041 if (bsize >= BLOCK_8X8) {
2042 switch (partition) {
2043 case PARTITION_SPLIT:
2044 if (bsize > BLOCK_8X8) break;
2045 case PARTITION_NONE:
2046 case PARTITION_HORZ:
2047 case PARTITION_VERT:
2048 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
2049 break;
2050 case PARTITION_HORZ_A:
2051 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2052 update_partition_context(xd, mi_row + hbs, mi_col, subsize, subsize);
2053 break;
2054 case PARTITION_HORZ_B:
2055 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2056 update_partition_context(xd, mi_row + hbs, mi_col, bsize2, subsize);
2057 break;
2058 case PARTITION_VERT_A:
2059 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2060 update_partition_context(xd, mi_row, mi_col + hbs, subsize, subsize);
2061 break;
2062 case PARTITION_VERT_B:
2063 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2064 update_partition_context(xd, mi_row, mi_col + hbs, bsize2, subsize);
2065 break;
2066 default: assert(0 && "Invalid partition type");
2067 }
2068 }
2069#else
2070 // update partition context
2071 if (bsize >= BLOCK_8X8 &&
2072 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
Jingning Han1beb0102016-12-07 11:08:30 -08002073 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
David Barkerf8935c92016-10-26 14:54:06 +01002074#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xud71be782016-10-14 08:47:03 -07002075
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002076#if CONFIG_DERING
David Barker9739f362016-11-10 09:29:32 +00002077#if CONFIG_EXT_PARTITION
2078 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128) {
2079 if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
2080 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2081 aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
2082 } else {
2083 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2084 0;
2085 }
2086 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64) {
2087#else
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002088 if (bsize == BLOCK_64X64) {
David Barker9739f362016-11-10 09:29:32 +00002089#endif
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002090 if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
2091 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2092 aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
2093 } else {
2094 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2095 0;
2096 }
2097 }
2098#endif
2099
Yaowu Xud71be782016-10-14 08:47:03 -07002100#if CONFIG_CLPF
David Barker9739f362016-11-10 09:29:32 +00002101#if CONFIG_EXT_PARTITION
2102 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128 &&
2103 cm->clpf_strength_y && cm->clpf_size != CLPF_NOSIZE) {
2104 const int tl = mi_row * MI_SIZE / MIN_FB_SIZE * cm->clpf_stride +
2105 mi_col * MI_SIZE / MIN_FB_SIZE;
2106 if (cm->clpf_size == CLPF_128X128) {
2107 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
2108 } else if (cm->clpf_size == CLPF_64X64) {
2109 const int tr = tl + 2;
2110 const int bl = tl + 2 * cm->clpf_stride;
2111 const int br = tr + 2 * cm->clpf_stride;
2112 const int size = 64 / MI_SIZE;
2113
2114 // Up to four bits per SB
2115 if (!clpf_all_skip(cm, mi_col, mi_row, size))
2116 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
2117
2118 if (mi_col + size < cm->mi_cols &&
2119 !clpf_all_skip(cm, mi_col + size, mi_row, size))
2120 cm->clpf_blocks[tr] = aom_read_literal(r, 1, ACCT_STR);
2121
2122 if (mi_row + size < cm->mi_rows &&
2123 !clpf_all_skip(cm, mi_col, mi_row + size, size))
2124 cm->clpf_blocks[bl] = aom_read_literal(r, 1, ACCT_STR);
2125
2126 if (mi_col + size < cm->mi_cols && mi_row + size < cm->mi_rows &&
2127 !clpf_all_skip(cm, mi_col + size, mi_row + size, size))
2128 cm->clpf_blocks[br] = aom_read_literal(r, 1, ACCT_STR);
2129 } else if (cm->clpf_size == CLPF_32X32) {
2130 int i, j;
2131 const int size = 32 / MI_SIZE;
2132 for (i = 0; i < 4; ++i)
2133 for (j = 0; j < 4; ++j) {
2134 const int index = tl + i * cm->clpf_stride + j;
2135 if (mi_row + i * size < cm->mi_rows &&
2136 mi_col + j * size < cm->mi_cols &&
2137 !clpf_all_skip(cm, mi_col + j * size, mi_row + i * size, size))
2138 cm->clpf_blocks[index] = aom_read_literal(r, 1, ACCT_STR);
2139 }
2140 }
2141 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64 &&
2142#else
2143 if (bsize == BLOCK_64X64 &&
2144#endif // CONFIG_EXT_PARTITION
2145 cm->clpf_strength_y && cm->clpf_size != CLPF_NOSIZE) {
Yaowu Xud71be782016-10-14 08:47:03 -07002146 const int tl = mi_row * MI_SIZE / MIN_FB_SIZE * cm->clpf_stride +
2147 mi_col * MI_SIZE / MIN_FB_SIZE;
2148
2149 if (!((mi_row * MI_SIZE) & 127) && !((mi_col * MI_SIZE) & 127) &&
2150 cm->clpf_size == CLPF_128X128) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002151 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002152 } else if (cm->clpf_size == CLPF_64X64 &&
2153 !clpf_all_skip(cm, mi_col, mi_row, 64 / MI_SIZE)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002154 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002155 } else if (cm->clpf_size == CLPF_32X32) {
2156 const int tr = tl + 1;
2157 const int bl = tl + cm->clpf_stride;
2158 const int br = tr + cm->clpf_stride;
2159 const int size = 32 / MI_SIZE;
2160
2161 // Up to four bits per SB
2162 if (!clpf_all_skip(cm, mi_col, mi_row, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002163 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002164
2165 if (mi_col + size < cm->mi_cols &&
2166 !clpf_all_skip(cm, mi_col + size, mi_row, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002167 cm->clpf_blocks[tr] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002168
2169 if (mi_row + size < cm->mi_rows &&
2170 !clpf_all_skip(cm, mi_col, mi_row + size, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002171 cm->clpf_blocks[bl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002172
2173 if (mi_col + size < cm->mi_cols && mi_row + size < cm->mi_rows &&
2174 !clpf_all_skip(cm, mi_col + size, mi_row + size, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002175 cm->clpf_blocks[br] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002176 }
2177 }
David Barker9739f362016-11-10 09:29:32 +00002178#endif // CONFIG_CLPF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002179}
2180
Yaowu Xuc27fc142016-08-22 16:08:15 -07002181static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
2182 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002183 struct aom_internal_error_info *error_info,
2184 aom_reader *r, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002185 void *decrypt_state) {
2186 // Validate the calculated partition length. If the buffer
2187 // described by the partition can't be fully read, then restrict
2188 // it to the portion that can be (for EC mode) or throw an error.
2189 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002190 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002191 "Truncated packet or corrupt tile length");
2192
Alex Converse2cdf0d82016-12-13 13:53:09 -08002193 if (aom_reader_init(r, data, read_size,
2194#if CONFIG_ANS && ANS_MAX_SYMBOLS
2195 ANS_MAX_SYMBOLS,
2196#endif
2197 decrypt_cb, decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07002198 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002199 "Failed to allocate bool decoder %d", 1);
2200}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002201
Yushin Cho77bba8d2016-11-04 16:36:56 -07002202#if !CONFIG_PVQ
Yaowu Xuf883b422016-08-30 14:01:10 -07002203static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
2204 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002205 int i, j, k, l, m;
Thomas9ac55082016-09-23 18:04:17 +01002206#if CONFIG_EC_ADAPT
Thomas Davies09ebbfb2016-10-20 18:28:47 +01002207 const int node_limit = UNCONSTRAINED_NODES - 1;
Thomas9ac55082016-09-23 18:04:17 +01002208#else
2209 const int node_limit = UNCONSTRAINED_NODES;
2210#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002211
Michael Bebenita6048d052016-08-25 14:40:54 -07002212 if (aom_read_bit(r, ACCT_STR))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002213 for (i = 0; i < PLANE_TYPES; ++i)
2214 for (j = 0; j < REF_TYPES; ++j)
2215 for (k = 0; k < COEF_BANDS; ++k)
2216 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
Thomas9ac55082016-09-23 18:04:17 +01002217 for (m = 0; m < node_limit; ++m)
Michael Bebenita6048d052016-08-25 14:40:54 -07002218 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002219}
2220
Yaowu Xuf883b422016-08-30 14:01:10 -07002221static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002222 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
2223 TX_SIZE tx_size;
Jingning Han83630632016-12-16 11:27:25 -08002224 for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002225 read_coef_probs_common(fc->coef_probs[tx_size], r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002226}
Yushin Cho77bba8d2016-11-04 16:36:56 -07002227#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002228
Yaowu Xuf883b422016-08-30 14:01:10 -07002229static void setup_segmentation(AV1_COMMON *const cm,
2230 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002231 struct segmentation *const seg = &cm->seg;
2232 int i, j;
2233
2234 seg->update_map = 0;
2235 seg->update_data = 0;
2236
Yaowu Xuf883b422016-08-30 14:01:10 -07002237 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002238 if (!seg->enabled) return;
2239
2240 // Segmentation map update
2241 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2242 seg->update_map = 1;
2243 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002244 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002245 }
2246 if (seg->update_map) {
2247 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2248 seg->temporal_update = 0;
2249 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002250 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002251 }
2252 }
2253
2254 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07002255 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002256 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002257 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002258
Yaowu Xuf883b422016-08-30 14:01:10 -07002259 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002260
2261 for (i = 0; i < MAX_SEGMENTS; i++) {
2262 for (j = 0; j < SEG_LVL_MAX; j++) {
2263 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002264 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002265 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002266 av1_enable_segfeature(seg, i, j);
2267 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
2268 if (av1_is_segfeature_signed(j))
2269 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002270 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002271 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002272 }
2273 }
2274 }
2275}
2276
2277#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002278static void decode_restoration_mode(AV1_COMMON *cm,
2279 struct aom_read_bit_buffer *rb) {
2280 RestorationInfo *rsi = &cm->rst_info;
2281 if (aom_rb_read_bit(rb)) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002282 if (aom_rb_read_bit(rb))
2283 rsi->frame_restoration_type =
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002284 (aom_rb_read_bit(rb) ? RESTORE_DOMAINTXFMRF : RESTORE_SGRPROJ);
Debargha Mukherjee0e67b252016-12-08 09:22:44 -08002285 else
2286 rsi->frame_restoration_type = RESTORE_WIENER;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002287 } else {
2288 rsi->frame_restoration_type =
2289 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
2290 }
2291}
2292
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002293static void read_wiener_filter(WienerInfo *wiener_info, aom_reader *rb) {
2294 wiener_info->vfilter[0] =
2295 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2296 WIENER_FILT_TAP0_MINV;
2297 wiener_info->vfilter[1] =
2298 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2299 WIENER_FILT_TAP1_MINV;
2300 wiener_info->vfilter[2] =
2301 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2302 WIENER_FILT_TAP2_MINV;
2303 wiener_info->hfilter[0] =
2304 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2305 WIENER_FILT_TAP0_MINV;
2306 wiener_info->hfilter[1] =
2307 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2308 WIENER_FILT_TAP1_MINV;
2309 wiener_info->hfilter[2] =
2310 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2311 WIENER_FILT_TAP2_MINV;
2312}
2313
2314static void read_sgrproj_filter(SgrprojInfo *sgrproj_info, aom_reader *rb) {
2315 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
2316 sgrproj_info->xqd[0] =
2317 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN0;
2318 sgrproj_info->xqd[1] =
2319 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN1;
2320}
2321
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002322static void read_domaintxfmrf_filter(DomaintxfmrfInfo *domaintxfmrf_info,
2323 aom_reader *rb) {
2324 domaintxfmrf_info->sigma_r =
2325 aom_read_literal(rb, DOMAINTXFMRF_PARAMS_BITS, ACCT_STR);
2326}
2327
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002328static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002329 int i;
2330 RestorationInfo *rsi = &cm->rst_info;
clang-formatbda8d612016-09-19 15:55:46 -07002331 const int ntiles =
2332 av1_get_rest_ntiles(cm->width, cm->height, NULL, NULL, NULL, NULL);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002333 if (rsi->frame_restoration_type != RESTORE_NONE) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002334 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002335 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002336 rsi->restoration_type[i] =
2337 aom_read_tree(rb, av1_switchable_restore_tree,
2338 cm->fc->switchable_restore_prob, ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002339 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07002340 rsi->wiener_info[i].level = 1;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002341 read_wiener_filter(&rsi->wiener_info[i], rb);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002342 } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
2343 rsi->sgrproj_info[i].level = 1;
2344 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002345 } else if (rsi->restoration_type[i] == RESTORE_DOMAINTXFMRF) {
2346 rsi->domaintxfmrf_info[i].level = 1;
2347 read_domaintxfmrf_filter(&rsi->domaintxfmrf_info[i], rb);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002348 }
2349 }
2350 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002351 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002352 if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002353 rsi->restoration_type[i] = RESTORE_WIENER;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002354 rsi->wiener_info[i].level = 1;
2355 read_wiener_filter(&rsi->wiener_info[i], rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002356 } else {
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07002357 rsi->wiener_info[i].level = 0;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002358 rsi->restoration_type[i] = RESTORE_NONE;
2359 }
2360 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002361 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002362 for (i = 0; i < ntiles; ++i) {
2363 if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
2364 rsi->restoration_type[i] = RESTORE_SGRPROJ;
2365 rsi->sgrproj_info[i].level = 1;
2366 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
2367 } else {
2368 rsi->sgrproj_info[i].level = 0;
2369 rsi->restoration_type[i] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002370 }
2371 }
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002372 } else if (rsi->frame_restoration_type == RESTORE_DOMAINTXFMRF) {
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002373 for (i = 0; i < ntiles; ++i) {
2374 if (aom_read(rb, RESTORE_NONE_DOMAINTXFMRF_PROB, ACCT_STR)) {
2375 rsi->restoration_type[i] = RESTORE_DOMAINTXFMRF;
2376 rsi->domaintxfmrf_info[i].level = 1;
2377 read_domaintxfmrf_filter(&rsi->domaintxfmrf_info[i], rb);
2378 } else {
2379 rsi->domaintxfmrf_info[i].level = 0;
2380 rsi->restoration_type[i] = RESTORE_NONE;
2381 }
2382 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002383 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002384 }
2385}
2386#endif // CONFIG_LOOP_RESTORATION
2387
Yaowu Xuf883b422016-08-30 14:01:10 -07002388static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002389 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07002390 lf->filter_level = aom_rb_read_literal(rb, 6);
2391 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002392
2393 // Read in loop filter deltas applied at the MB level based on mode or ref
2394 // frame.
2395 lf->mode_ref_delta_update = 0;
2396
Yaowu Xuf883b422016-08-30 14:01:10 -07002397 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002398 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002399 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002400 if (lf->mode_ref_delta_update) {
2401 int i;
2402
2403 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002404 if (aom_rb_read_bit(rb))
2405 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002406
2407 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002408 if (aom_rb_read_bit(rb))
2409 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002410 }
2411 }
2412}
2413
2414#if CONFIG_CLPF
Yaowu Xud71be782016-10-14 08:47:03 -07002415static void setup_clpf(AV1Decoder *pbi, struct aom_read_bit_buffer *rb) {
2416 AV1_COMMON *const cm = &pbi->common;
2417 const int width = pbi->cur_buf->buf.y_crop_width;
2418 const int height = pbi->cur_buf->buf.y_crop_height;
2419
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002420 cm->clpf_blocks = 0;
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02002421 cm->clpf_strength_y = aom_rb_read_literal(rb, 2);
2422 cm->clpf_strength_u = aom_rb_read_literal(rb, 2);
2423 cm->clpf_strength_v = aom_rb_read_literal(rb, 2);
2424 if (cm->clpf_strength_y) {
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002425 cm->clpf_size = aom_rb_read_literal(rb, 2);
Yaowu Xud71be782016-10-14 08:47:03 -07002426 if (cm->clpf_size != CLPF_NOSIZE) {
2427 int size;
2428 cm->clpf_stride =
2429 ((width + MIN_FB_SIZE - 1) & ~(MIN_FB_SIZE - 1)) >> MIN_FB_SIZE_LOG2;
2430 size =
2431 cm->clpf_stride * ((height + MIN_FB_SIZE - 1) & ~(MIN_FB_SIZE - 1)) >>
2432 MIN_FB_SIZE_LOG2;
2433 CHECK_MEM_ERROR(cm, cm->clpf_blocks, aom_malloc(size));
2434 memset(cm->clpf_blocks, -1, size);
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002435 }
2436 }
2437}
2438
Steinar Midtskogen2fd70ee2016-09-02 10:02:30 +02002439static int clpf_bit(UNUSED int k, UNUSED int l,
2440 UNUSED const YV12_BUFFER_CONFIG *rec,
2441 UNUSED const YV12_BUFFER_CONFIG *org,
2442 UNUSED const AV1_COMMON *cm, UNUSED int block_size,
2443 UNUSED int w, UNUSED int h, UNUSED unsigned int strength,
Yaowu Xud71be782016-10-14 08:47:03 -07002444 UNUSED unsigned int fb_size_log2, int8_t *bit) {
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002445 return *bit;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002446}
2447#endif
2448
2449#if CONFIG_DERING
Yaowu Xuf883b422016-08-30 14:01:10 -07002450static void setup_dering(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
2451 cm->dering_level = aom_rb_read_literal(rb, DERING_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002452}
2453#endif // CONFIG_DERING
2454
Yaowu Xuf883b422016-08-30 14:01:10 -07002455static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2456 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002457}
2458
Yaowu Xuf883b422016-08-30 14:01:10 -07002459static void setup_quantization(AV1_COMMON *const cm,
2460 struct aom_read_bit_buffer *rb) {
2461 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002462 cm->y_dc_delta_q = read_delta_q(rb);
2463 cm->uv_dc_delta_q = read_delta_q(rb);
2464 cm->uv_ac_delta_q = read_delta_q(rb);
2465 cm->dequant_bit_depth = cm->bit_depth;
2466#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002467 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002468 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002469 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2470 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002471 } else {
2472 cm->min_qmlevel = 0;
2473 cm->max_qmlevel = 0;
2474 }
2475#endif
2476}
2477
Yaowu Xuf883b422016-08-30 14:01:10 -07002478static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002479 // Build y/uv dequant values based on segmentation.
2480 int i = 0;
2481#if CONFIG_AOM_QM
2482 int lossless;
2483 int j = 0;
2484 int qmlevel;
2485 int using_qm = cm->using_qmatrix;
2486 int minqm = cm->min_qmlevel;
2487 int maxqm = cm->max_qmlevel;
2488#endif
2489#if CONFIG_NEW_QUANT
2490 int b;
2491 int dq;
2492#endif // CONFIG_NEW_QUANT
2493 if (cm->seg.enabled) {
2494 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002495 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002496 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002497 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2498 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002499 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002500 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002501 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002502 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002503#if CONFIG_AOM_QM
2504 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2505 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2506 // NB: depends on base index so there is only 1 set per frame
2507 // No quant weighting when lossless or signalled not using QM
2508 qmlevel = (lossless || using_qm == 0)
2509 ? NUM_QM_LEVELS - 1
2510 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2511 for (j = 0; j < TX_SIZES; ++j) {
2512 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2513 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2514 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2515 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2516 }
2517#endif // CONFIG_AOM_QM
2518#if CONFIG_NEW_QUANT
2519 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2520 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002521 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002522 cm->y_dequant_nuq[i][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002523 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002524 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002525 }
2526 }
2527#endif // CONFIG_NEW_QUANT
2528 }
2529 } else {
2530 const int qindex = cm->base_qindex;
2531 // When segmentation is disabled, only the first value is used. The
2532 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002533 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2534 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002535 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002536 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002537 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002538 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002539#if CONFIG_AOM_QM
2540 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2541 cm->uv_ac_delta_q == 0;
2542 // No quant weighting when lossless or signalled not using QM
2543 qmlevel = (lossless || using_qm == 0)
2544 ? NUM_QM_LEVELS - 1
2545 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2546 for (j = 0; j < TX_SIZES; ++j) {
2547 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2548 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2549 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2550 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2551 }
2552#endif
2553#if CONFIG_NEW_QUANT
2554 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2555 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002556 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002557 cm->y_dequant_nuq[0][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002558 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002559 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002560 }
2561 }
2562#endif // CONFIG_NEW_QUANT
2563 }
2564}
2565
Angie Chiang5678ad92016-11-21 09:38:40 -08002566static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002567 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07002568 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002569}
2570
Yaowu Xuf883b422016-08-30 14:01:10 -07002571static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002572 cm->render_width = cm->width;
2573 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002574 if (aom_rb_read_bit(rb))
2575 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002576}
2577
Yaowu Xuf883b422016-08-30 14:01:10 -07002578static void resize_mv_buffer(AV1_COMMON *cm) {
2579 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002580 cm->cur_frame->mi_rows = cm->mi_rows;
2581 cm->cur_frame->mi_cols = cm->mi_cols;
2582 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002583 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002584 sizeof(*cm->cur_frame->mvs)));
2585}
2586
Yaowu Xuf883b422016-08-30 14:01:10 -07002587static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002588#if CONFIG_SIZE_LIMIT
2589 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002590 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002591 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2592 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2593#endif
2594 if (cm->width != width || cm->height != height) {
2595 const int new_mi_rows =
2596 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2597 const int new_mi_cols =
2598 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2599
Yaowu Xuf883b422016-08-30 14:01:10 -07002600 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002601 // dimensions as well as the overall size.
2602 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002603 if (av1_alloc_context_buffers(cm, width, height))
2604 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002605 "Failed to allocate context buffers");
2606 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002607 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002608 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002609 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002610 cm->width = width;
2611 cm->height = height;
2612 }
2613 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2614 cm->mi_cols > cm->cur_frame->mi_cols) {
2615 resize_mv_buffer(cm);
2616 }
2617}
2618
Yaowu Xuf883b422016-08-30 14:01:10 -07002619static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002620 int width, height;
2621 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002622 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002623 resize_context_buffers(cm, width, height);
2624 setup_render_size(cm, rb);
2625
2626 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002627 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002628 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2629 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002630#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002631 cm->use_highbitdepth,
2632#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002633 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002634 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2635 pool->cb_priv)) {
2636 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002637 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002638 "Failed to allocate frame buffer");
2639 }
2640 unlock_buffer_pool(pool);
2641
2642 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2643 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2644 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2645 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2646 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2647 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2648 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2649}
2650
Yaowu Xuf883b422016-08-30 14:01:10 -07002651static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002652 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002653 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002654 int this_xss, int this_yss) {
2655 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2656 ref_yss == this_yss;
2657}
2658
Yaowu Xuf883b422016-08-30 14:01:10 -07002659static void setup_frame_size_with_refs(AV1_COMMON *cm,
2660 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002661 int width, height;
2662 int found = 0, i;
2663 int has_valid_ref_frame = 0;
2664 BufferPool *const pool = cm->buffer_pool;
2665 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002666 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002667 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2668 width = buf->y_crop_width;
2669 height = buf->y_crop_height;
2670 cm->render_width = buf->render_width;
2671 cm->render_height = buf->render_height;
2672 found = 1;
2673 break;
2674 }
2675 }
2676
2677 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002678 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002679 setup_render_size(cm, rb);
2680 }
2681
2682 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002683 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002684 "Invalid frame size");
2685
2686 // Check to make sure at least one of frames that this frame references
2687 // has valid dimensions.
2688 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2689 RefBuffer *const ref_frame = &cm->frame_refs[i];
2690 has_valid_ref_frame |=
2691 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2692 ref_frame->buf->y_crop_height, width, height);
2693 }
2694 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002695 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002696 "Referenced frame has invalid size");
2697 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2698 RefBuffer *const ref_frame = &cm->frame_refs[i];
2699 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
2700 ref_frame->buf->subsampling_x,
2701 ref_frame->buf->subsampling_y, cm->bit_depth,
2702 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07002703 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002704 "Referenced frame has incompatible color format");
2705 }
2706
2707 resize_context_buffers(cm, width, height);
2708
2709 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002710 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002711 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2712 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002713#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002714 cm->use_highbitdepth,
2715#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002716 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002717 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2718 pool->cb_priv)) {
2719 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002720 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002721 "Failed to allocate frame buffer");
2722 }
2723 unlock_buffer_pool(pool);
2724
2725 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2726 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2727 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2728 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2729 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2730 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2731 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2732}
2733
Yaowu Xuf883b422016-08-30 14:01:10 -07002734static void read_tile_info(AV1Decoder *const pbi,
2735 struct aom_read_bit_buffer *const rb) {
2736 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002737#if CONFIG_EXT_TILE
2738// Read the tile width/height
2739#if CONFIG_EXT_PARTITION
2740 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002741 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
2742 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002743 } else
2744#endif // CONFIG_EXT_PARTITION
2745 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002746 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
2747 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002748 }
2749
2750 cm->tile_width <<= cm->mib_size_log2;
2751 cm->tile_height <<= cm->mib_size_log2;
2752
Yaowu Xuf883b422016-08-30 14:01:10 -07002753 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
2754 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002755
2756 // Get the number of tiles
2757 cm->tile_cols = 1;
2758 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
2759
2760 cm->tile_rows = 1;
2761 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
2762
2763 if (cm->tile_cols * cm->tile_rows > 1) {
2764 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07002765 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2766 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002767 }
2768#else
2769 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07002770 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002771
2772 // columns
2773 max_ones = max_log2_tile_cols - min_log2_tile_cols;
2774 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07002775 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002776
2777 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07002778 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002779 "Invalid number of tile columns");
2780
2781 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07002782 cm->log2_tile_rows = aom_rb_read_bit(rb);
2783 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002784
2785 cm->tile_cols = 1 << cm->log2_tile_cols;
2786 cm->tile_rows = 1 << cm->log2_tile_rows;
2787
2788 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
2789 cm->tile_width >>= cm->log2_tile_cols;
2790 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
2791 cm->tile_height >>= cm->log2_tile_rows;
2792
2793 // round to integer multiples of superblock size
2794 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
2795 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
2796
Thomas Davies4974e522016-11-07 17:44:05 +00002797// tile size magnitude
2798#if !CONFIG_TILE_GROUPS
2799 if (cm->tile_rows > 1 || cm->tile_cols > 1)
2800#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002801 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002802#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00002803
Thomas Davies80188d12016-10-26 16:08:35 -07002804#if CONFIG_TILE_GROUPS
2805 // Store an index to the location of the tile group information
2806 pbi->tg_size_bit_offset = rb->bit_offset;
2807 pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
2808 if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
2809 pbi->tg_start =
2810 aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
2811 pbi->tg_size =
2812 1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
2813 }
2814#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002815}
2816
2817static int mem_get_varsize(const uint8_t *src, const int sz) {
2818 switch (sz) {
2819 case 1: return src[0];
2820 case 2: return mem_get_le16(src);
2821 case 3: return mem_get_le24(src);
2822 case 4: return mem_get_le32(src);
2823 default: assert("Invalid size" && 0); return -1;
2824 }
2825}
2826
2827#if CONFIG_EXT_TILE
2828// Reads the next tile returning its size and adjusting '*data' accordingly
2829// based on 'is_last'.
2830static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07002831 struct aom_internal_error_info *error_info,
2832 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002833 void *decrypt_state,
2834 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
2835 int tile_size_bytes, int col, int row) {
2836 size_t size;
2837
2838 size_t copy_size = 0;
2839 const uint8_t *copy_data = NULL;
2840
2841 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002842 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002843 "Truncated packet or corrupt tile length");
2844 if (decrypt_cb) {
2845 uint8_t be_data[4];
2846 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
2847
2848 // Only read number of bytes in cm->tile_size_bytes.
2849 size = mem_get_varsize(be_data, tile_size_bytes);
2850 } else {
2851 size = mem_get_varsize(*data, tile_size_bytes);
2852 }
2853
2854 // The top bit indicates copy mode
2855 if ((size >> (tile_size_bytes * 8 - 1)) == 1) {
2856 // The remaining bits in the top byte signal the row offset
2857 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
2858
2859 // Currently, only use tiles in same column as reference tiles.
2860 copy_data = tile_buffers[row - offset][col].data;
2861 copy_size = tile_buffers[row - offset][col].size;
2862 size = 0;
2863 }
2864
2865 *data += tile_size_bytes;
2866
2867 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07002868 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002869 "Truncated packet or corrupt tile size");
2870
2871 if (size > 0) {
2872 tile_buffers[row][col].data = *data;
2873 tile_buffers[row][col].size = size;
2874 } else {
2875 tile_buffers[row][col].data = copy_data;
2876 tile_buffers[row][col].size = copy_size;
2877 }
2878
2879 *data += size;
2880
2881 tile_buffers[row][col].raw_data_end = *data;
2882}
2883
2884static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07002885 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002886 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002887 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002888 const int tile_cols = cm->tile_cols;
2889 const int tile_rows = cm->tile_rows;
2890 const int have_tiles = tile_cols * tile_rows > 1;
2891
2892 if (!have_tiles) {
2893 const uint32_t tile_size = data_end - data;
2894 tile_buffers[0][0].data = data;
2895 tile_buffers[0][0].size = tile_size;
2896 tile_buffers[0][0].raw_data_end = NULL;
2897 } else {
2898 // We locate only the tile buffers that are required, which are the ones
2899 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
2900 // need the last (bottom right) tile buffer, as we need to know where the
2901 // end of the compressed frame buffer is for proper superframe decoding.
2902
2903 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
2904 const uint8_t *const data_start = data;
2905
Yaowu Xuf883b422016-08-30 14:01:10 -07002906 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002907 const int single_row = pbi->dec_tile_row >= 0;
2908 const int tile_rows_start = single_row ? dec_tile_row : 0;
2909 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07002910 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002911 const int single_col = pbi->dec_tile_col >= 0;
2912 const int tile_cols_start = single_col ? dec_tile_col : 0;
2913 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
2914
2915 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
2916 const int tile_size_bytes = pbi->tile_size_bytes;
2917
2918 size_t tile_col_size;
2919 int r, c;
2920
2921 // Read tile column sizes for all columns (we need the last tile buffer)
2922 for (c = 0; c < tile_cols; ++c) {
2923 const int is_last = c == tile_cols - 1;
2924 if (!is_last) {
2925 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
2926 data += tile_col_size_bytes;
2927 tile_col_data_end[c] = data + tile_col_size;
2928 } else {
2929 tile_col_size = data_end - data;
2930 tile_col_data_end[c] = data_end;
2931 }
2932 data += tile_col_size;
2933 }
2934
2935 data = data_start;
2936
2937 // Read the required tile sizes.
2938 for (c = tile_cols_start; c < tile_cols_end; ++c) {
2939 const int is_last = c == tile_cols - 1;
2940
2941 if (c > 0) data = tile_col_data_end[c - 1];
2942
2943 if (!is_last) data += tile_col_size_bytes;
2944
2945 // Get the whole of the last column, otherwise stop at the required tile.
2946 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
2947 tile_buffers[r][c].col = c;
2948
2949 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
2950 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
2951 tile_size_bytes, c, r);
2952 }
2953 }
2954
2955 // If we have not read the last column, then read it to get the last tile.
2956 if (tile_cols_end != tile_cols) {
2957 c = tile_cols - 1;
2958
2959 data = tile_col_data_end[c - 1];
2960
2961 for (r = 0; r < tile_rows; ++r) {
2962 tile_buffers[r][c].col = c;
2963
2964 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
2965 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
2966 tile_size_bytes, c, r);
2967 }
2968 }
2969 }
2970}
2971#else
2972// Reads the next tile returning its size and adjusting '*data' accordingly
2973// based on 'is_last'.
2974static void get_tile_buffer(const uint8_t *const data_end,
2975 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07002976 struct aom_internal_error_info *error_info,
2977 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002978 void *decrypt_state, TileBufferDec *const buf) {
2979 size_t size;
2980
2981 if (!is_last) {
2982 if (!read_is_valid(*data, 4, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002983 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002984 "Truncated packet or corrupt tile length");
2985
2986 if (decrypt_cb) {
2987 uint8_t be_data[4];
2988 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
2989 size = mem_get_varsize(be_data, tile_size_bytes);
2990 } else {
2991 size = mem_get_varsize(*data, tile_size_bytes);
2992 }
2993 *data += tile_size_bytes;
2994
2995 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07002996 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002997 "Truncated packet or corrupt tile size");
2998 } else {
2999 size = data_end - *data;
3000 }
3001
3002 buf->data = *data;
3003 buf->size = size;
3004
3005 *data += size;
3006}
3007
3008static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003009 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003010 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003011 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07003012#if CONFIG_TILE_GROUPS
3013 int r, c;
3014 const int tile_cols = cm->tile_cols;
3015 const int tile_rows = cm->tile_rows;
3016 int tc = 0;
3017 int first_tile_in_tg = 0;
3018 int hdr_offset;
3019 struct aom_read_bit_buffer rb_tg_hdr;
3020 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3021 const int num_tiles = tile_rows * tile_cols;
3022 const int num_bits = OD_ILOG(num_tiles) - 1;
3023 const int hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
3024 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
3025
3026 for (r = 0; r < tile_rows; ++r) {
3027 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07003028 TileBufferDec *const buf = &tile_buffers[r][c];
3029 hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
3030
3031 buf->col = c;
3032 if (hdr_offset) {
3033 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
3034 rb_tg_hdr.bit_offset = tg_size_bit_offset;
3035 if (num_tiles) {
3036 pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
3037 pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
3038 }
3039 }
3040 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
3041 data += hdr_offset;
Thomas Davies8fe64a32016-10-04 13:19:31 +01003042 get_tile_buffer(data_end, pbi->tile_size_bytes, 0, &pbi->common.error,
3043 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
Thomas Davies80188d12016-10-26 16:08:35 -07003044 }
3045 }
3046#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003047 int r, c;
3048 const int tile_cols = cm->tile_cols;
3049 const int tile_rows = cm->tile_rows;
3050
3051 for (r = 0; r < tile_rows; ++r) {
3052 for (c = 0; c < tile_cols; ++c) {
3053 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
3054 TileBufferDec *const buf = &tile_buffers[r][c];
3055 buf->col = c;
3056 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
3057 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
3058 }
3059 }
Thomas Davies80188d12016-10-26 16:08:35 -07003060#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003061}
3062#endif // CONFIG_EXT_TILE
3063
Yushin Cho77bba8d2016-11-04 16:36:56 -07003064#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003065static void daala_dec_init(AV1_COMMON *const cm, daala_dec_ctx *daala_dec,
3066 od_ec_dec *ec) {
Yushin Cho77bba8d2016-11-04 16:36:56 -07003067 daala_dec->ec = ec;
3068 od_adapt_ctx_reset(&daala_dec->state.adapt, 0);
3069
Yushin Cho70669122016-12-08 09:53:14 -10003070 // TODO(yushin) : activity masking info needs be signaled by a bitstream
3071 daala_dec->use_activity_masking = AV1_PVQ_ENABLE_ACTIVITY_MASKING;
3072
3073 if (daala_dec->use_activity_masking)
3074 daala_dec->qm = OD_HVS_QM;
3075 else
3076 daala_dec->qm = OD_FLAT_QM;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003077
3078 od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
3079 daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
Yushin Cho70669122016-12-08 09:53:14 -10003080
3081 if (daala_dec->use_activity_masking) {
3082 int pli;
3083 int use_masking = daala_dec->use_activity_masking;
3084 int segment_id = 0;
3085 int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
3086
3087 for (pli = 0; pli < MAX_MB_PLANE; pli++) {
3088 int i;
3089 int q;
3090
3091 q = qindex;
3092 if (q <= OD_DEFAULT_QMS[use_masking][0][pli].interp_q << OD_COEFF_SHIFT) {
3093 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3094 &OD_DEFAULT_QMS[use_masking][0][pli], NULL);
3095 } else {
3096 i = 0;
3097 while (OD_DEFAULT_QMS[use_masking][i + 1][pli].qm_q4 != NULL &&
3098 q > OD_DEFAULT_QMS[use_masking][i + 1][pli].interp_q
3099 << OD_COEFF_SHIFT) {
3100 i++;
3101 }
3102 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3103 &OD_DEFAULT_QMS[use_masking][i][pli],
3104 &OD_DEFAULT_QMS[use_masking][i + 1][pli]);
3105 }
3106 }
3107 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07003108}
Yushin Cho70669122016-12-08 09:53:14 -10003109#endif // #if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003110
Yaowu Xuf883b422016-08-30 14:01:10 -07003111static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003112 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003113 AV1_COMMON *const cm = &pbi->common;
3114 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003115 const int tile_cols = cm->tile_cols;
3116 const int tile_rows = cm->tile_rows;
3117 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07003118 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003119#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003120 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003121 const int single_row = pbi->dec_tile_row >= 0;
3122 const int tile_rows_start = single_row ? dec_tile_row : 0;
3123 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003124 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003125 const int single_col = pbi->dec_tile_col >= 0;
3126 const int tile_cols_start = single_col ? dec_tile_col : 0;
3127 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3128 const int inv_col_order = pbi->inv_tile_order && !single_col;
3129 const int inv_row_order = pbi->inv_tile_order && !single_row;
3130#else
3131 const int tile_rows_start = 0;
3132 const int tile_rows_end = tile_rows;
3133 const int tile_cols_start = 0;
3134 const int tile_cols_end = tile_cols;
3135 const int inv_col_order = pbi->inv_tile_order;
3136 const int inv_row_order = pbi->inv_tile_order;
3137#endif // CONFIG_EXT_TILE
3138 int tile_row, tile_col;
3139
3140#if CONFIG_ENTROPY
3141 cm->do_subframe_update = n_tiles == 1;
3142#endif // CONFIG_ENTROPY
3143
3144 if (cm->lf.filter_level && !cm->skip_loop_filter &&
3145 pbi->lf_worker.data1 == NULL) {
3146 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07003147 aom_memalign(32, sizeof(LFWorkerData)));
3148 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003149 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003150 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003151 "Loop filter thread creation failed");
3152 }
3153 }
3154
3155 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3156 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3157 // Be sure to sync as we might be resuming after a failed frame decode.
3158 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003159 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
3160 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003161 }
3162
3163 assert(tile_rows <= MAX_TILE_ROWS);
3164 assert(tile_cols <= MAX_TILE_COLS);
3165
3166 get_tile_buffers(pbi, data, data_end, tile_buffers);
3167
3168 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003169 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003170 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003171 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003172 pbi->allocated_tiles = n_tiles;
3173 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003174#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003175 if (pbi->acct_enabled) {
3176 aom_accounting_reset(&pbi->accounting);
3177 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003178#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003179 // Load all tile information into tile_data.
3180 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3181 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3182 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
3183 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
3184
3185 td->cm = cm;
3186 td->xd = pbi->mb;
3187 td->xd.corrupted = 0;
3188 td->xd.counts =
3189 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3190 ? &cm->counts
3191 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003192 av1_zero(td->dqcoeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003193#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003194 av1_zero(td->pvq_ref_coeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003195#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003196 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003197 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
3198 &td->bit_reader, pbi->decrypt_cb, pbi->decrypt_state);
Michael Bebenita6048d052016-08-25 14:40:54 -07003199#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003200 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003201 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003202 } else {
David Barkerd971f402016-10-25 13:52:07 +01003203 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003204 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003205#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003206 av1_init_macroblockd(cm, &td->xd,
3207#if CONFIG_PVQ
3208 td->pvq_ref_coeff,
3209#endif
3210 td->dqcoeff);
3211#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003212 daala_dec_init(cm, &td->xd.daala_dec, &td->bit_reader.ec);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003213#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003214#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003215 td->xd.plane[0].color_index_map = td->color_index_map[0];
3216 td->xd.plane[1].color_index_map = td->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003217#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003218 }
3219 }
3220
3221 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3222 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
3223 int mi_row = 0;
3224 TileInfo tile_info;
3225
Yaowu Xuf883b422016-08-30 14:01:10 -07003226 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003227
3228 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3229 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
3230 TileData *const td = pbi->tile_data + tile_cols * row + col;
Michael Bebenita6048d052016-08-25 14:40:54 -07003231#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003232 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003233 td->bit_reader.accounting->last_tell_frac =
3234 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003235 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003236#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003237
Yaowu Xuf883b422016-08-30 14:01:10 -07003238 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003239
Yaowu Xuf883b422016-08-30 14:01:10 -07003240 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003241
3242 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
3243 mi_row += cm->mib_size) {
3244 int mi_col;
3245
Yaowu Xuf883b422016-08-30 14:01:10 -07003246 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003247
3248 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
3249 mi_col += cm->mib_size) {
3250 decode_partition(pbi, &td->xd,
3251#if CONFIG_SUPERTX
3252 0,
3253#endif // CONFIG_SUPERTX
3254 mi_row, mi_col, &td->bit_reader, cm->sb_size,
3255 b_width_log2_lookup[cm->sb_size]);
3256 }
3257 pbi->mb.corrupted |= td->xd.corrupted;
3258 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003259 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003260 "Failed to decode tile data");
3261#if CONFIG_ENTROPY
3262 if (cm->do_subframe_update &&
3263 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3264 if ((mi_row + MI_SIZE) %
3265 (MI_SIZE *
Yaowu Xuf883b422016-08-30 14:01:10 -07003266 AOMMAX(cm->mi_rows / MI_SIZE / COEF_PROBS_BUFS, 1)) ==
Yaowu Xuc27fc142016-08-22 16:08:15 -07003267 0 &&
3268 mi_row + MI_SIZE < cm->mi_rows &&
3269 cm->coef_probs_update_idx < COEF_PROBS_BUFS - 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003270 av1_partial_adapt_probs(cm, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003271 ++cm->coef_probs_update_idx;
3272 }
3273 }
3274#endif // CONFIG_ENTROPY
3275 }
3276 }
3277
3278 assert(mi_row > 0);
3279
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003280// when Parallel deblocking is enabled, deblocking should not
3281// be interleaved with decoding. Instead, deblocking should be done
3282// after the entire frame is decoded.
3283#if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003284 // Loopfilter one tile row.
3285 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3286 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003287 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003288 const int lf_end = tile_info.mi_row_end - cm->mib_size;
3289
3290 // Delay the loopfilter if the first tile row is only
3291 // a single superblock high.
3292 if (lf_end <= 0) continue;
3293
3294 // Decoding has completed. Finish up the loop filter in this thread.
3295 if (tile_info.mi_row_end >= cm->mi_rows) continue;
3296
3297 winterface->sync(&pbi->lf_worker);
3298 lf_data->start = lf_start;
3299 lf_data->stop = lf_end;
3300 if (pbi->max_threads > 1) {
3301 winterface->launch(&pbi->lf_worker);
3302 } else {
3303 winterface->execute(&pbi->lf_worker);
3304 }
3305 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003306#endif // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003307
3308 // After loopfiltering, the last 7 row pixels in each superblock row may
3309 // still be changed by the longest loopfilter of the next superblock row.
3310 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003311 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003312 }
3313
3314#if CONFIG_VAR_TX
3315 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003316 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
3317 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003318#else
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003319#if CONFIG_PARALLEL_DEBLOCKING
3320 // Loopfilter all rows in the frame in the frame.
3321 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3322 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3323 winterface->sync(&pbi->lf_worker);
3324 lf_data->start = 0;
3325 lf_data->stop = cm->mi_rows;
3326 winterface->execute(&pbi->lf_worker);
3327 }
3328#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003329 // Loopfilter remaining rows in the frame.
3330 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3331 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3332 winterface->sync(&pbi->lf_worker);
3333 lf_data->start = lf_data->stop;
3334 lf_data->stop = cm->mi_rows;
3335 winterface->execute(&pbi->lf_worker);
3336 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003337#endif // CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003338#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003339 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003340 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003341
3342#if CONFIG_EXT_TILE
3343 if (n_tiles == 1) {
3344#if CONFIG_ANS
3345 return data_end;
3346#else
3347 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07003348 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003349#endif // CONFIG_ANS
3350 } else {
3351 // Return the end of the last tile buffer
3352 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3353 }
3354#else
3355#if CONFIG_ANS
3356 return data_end;
3357#else
3358 {
3359 // Get last tile data.
3360 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003361 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003362 }
3363#endif // CONFIG_ANS
3364#endif // CONFIG_EXT_TILE
3365}
3366
3367static int tile_worker_hook(TileWorkerData *const tile_data,
3368 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003369 AV1Decoder *const pbi = tile_data->pbi;
3370 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003371 int mi_row, mi_col;
3372
3373 if (setjmp(tile_data->error_info.jmp)) {
3374 tile_data->error_info.setjmp = 0;
3375 tile_data->xd.corrupted = 1;
3376 return 0;
3377 }
3378
3379 tile_data->error_info.setjmp = 1;
3380 tile_data->xd.error_info = &tile_data->error_info;
3381
Yaowu Xuf883b422016-08-30 14:01:10 -07003382 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003383
3384 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
3385 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003386 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003387
3388 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3389 mi_col += cm->mib_size) {
3390 decode_partition(pbi, &tile_data->xd,
3391#if CONFIG_SUPERTX
3392 0,
3393#endif
3394 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
3395 b_width_log2_lookup[cm->sb_size]);
3396 }
3397 }
3398 return !tile_data->xd.corrupted;
3399}
3400
3401// sorts in descending order
3402static int compare_tile_buffers(const void *a, const void *b) {
3403 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
3404 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
3405 return (int)(buf2->size - buf1->size);
3406}
3407
Yaowu Xuf883b422016-08-30 14:01:10 -07003408static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003409 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003410 AV1_COMMON *const cm = &pbi->common;
3411 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003412 const int tile_cols = cm->tile_cols;
3413 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003414 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
clang-format67948d32016-09-07 22:40:40 -07003415 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003416#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003417 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003418 const int single_row = pbi->dec_tile_row >= 0;
3419 const int tile_rows_start = single_row ? dec_tile_row : 0;
3420 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003421 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003422 const int single_col = pbi->dec_tile_col >= 0;
3423 const int tile_cols_start = single_col ? dec_tile_col : 0;
3424 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3425#else
3426 const int tile_rows_start = 0;
3427 const int tile_rows_end = tile_rows;
3428 const int tile_cols_start = 0;
3429 const int tile_cols_end = tile_cols;
3430#endif // CONFIG_EXT_TILE
3431 int tile_row, tile_col;
3432 int i;
3433
3434#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3435 int final_worker = -1;
3436#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3437
3438 assert(tile_rows <= MAX_TILE_ROWS);
3439 assert(tile_cols <= MAX_TILE_COLS);
3440
3441 assert(tile_cols * tile_rows > 1);
3442
Yaowu Xuc27fc142016-08-22 16:08:15 -07003443 // TODO(jzern): See if we can remove the restriction of passing in max
3444 // threads to the decoder.
3445 if (pbi->num_tile_workers == 0) {
3446 const int num_threads = pbi->max_threads & ~1;
3447 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07003448 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003449 // Ensure tile data offsets will be properly aligned. This may fail on
3450 // platforms without DECLARE_ALIGNED().
3451 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3452 CHECK_MEM_ERROR(
3453 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003454 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003455 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07003456 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003457 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003458 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003459 ++pbi->num_tile_workers;
3460
3461 winterface->init(worker);
3462 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003463 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003464 "Tile decoder thread creation failed");
3465 }
3466 }
3467 }
3468
3469 // Reset tile decoding hook
3470 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003471 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003472 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003473 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003474 worker->data1 = &pbi->tile_worker_data[i];
3475 worker->data2 = &pbi->tile_worker_info[i];
3476 }
3477
3478 // Initialize thread frame counts.
3479 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3480 for (i = 0; i < num_workers; ++i) {
3481 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003482 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003483 }
3484 }
3485
3486 // Load tile data into tile_buffers
3487 get_tile_buffers(pbi, data, data_end, tile_buffers);
3488
3489 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3490 // Sort the buffers in this tile row based on size in descending order.
3491 qsort(&tile_buffers[tile_row][tile_cols_start],
3492 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
3493 compare_tile_buffers);
3494
3495 // Rearrange the tile buffers in this tile row such that per-tile group
3496 // the largest, and presumably the most difficult tile will be decoded in
3497 // the main thread. This should help minimize the number of instances
3498 // where the main thread is waiting for a worker to complete.
3499 {
3500 int group_start;
3501 for (group_start = tile_cols_start; group_start < tile_cols_end;
3502 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003503 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003504 const TileBufferDec largest = tile_buffers[tile_row][group_start];
3505 memmove(&tile_buffers[tile_row][group_start],
3506 &tile_buffers[tile_row][group_start + 1],
3507 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
3508 tile_buffers[tile_row][group_end - 1] = largest;
3509 }
3510 }
3511
3512 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
3513 // Launch workers for individual columns
3514 for (i = 0; i < num_workers && tile_col < tile_cols_end;
3515 ++i, ++tile_col) {
3516 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07003517 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003518 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
3519 TileInfo *const tile_info = (TileInfo *)worker->data2;
3520
3521 twd->pbi = pbi;
3522 twd->xd = pbi->mb;
3523 twd->xd.corrupted = 0;
3524 twd->xd.counts =
3525 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3526 ? &twd->counts
3527 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003528 av1_zero(twd->dqcoeff);
3529 av1_tile_init(tile_info, cm, tile_row, buf->col);
3530 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003531 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
3532 &twd->bit_reader, pbi->decrypt_cb,
3533 pbi->decrypt_state);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003534 av1_init_macroblockd(cm, &twd->xd,
3535#if CONFIG_PVQ
3536 twd->pvq_ref_coeff,
3537#endif
3538 twd->dqcoeff);
3539#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003540 daala_dec_init(cm, &twd->xd.daala_dec, &twd->bit_reader.ec);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003541#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003542#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003543 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
3544 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003545#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003546
3547 worker->had_error = 0;
3548 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
3549 winterface->execute(worker);
3550 } else {
3551 winterface->launch(worker);
3552 }
3553
3554#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3555 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
3556 final_worker = i;
3557 }
3558#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3559 }
3560
3561 // Sync all workers
3562 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003563 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003564 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07003565 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07003566 // info in cm. Additionally once the threads have been synced and an
3567 // error is detected, there's no point in continuing to decode tiles.
3568 pbi->mb.corrupted |= !winterface->sync(worker);
3569 }
3570 }
3571 }
3572
3573 // Accumulate thread frame counts.
3574 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3575 for (i = 0; i < num_workers; ++i) {
3576 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003577 av1_accumulate_frame_counts(cm, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003578 }
3579 }
3580
3581#if CONFIG_EXT_TILE
3582 // Return the end of the last tile buffer
3583 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3584#else
3585#if CONFIG_ANS
3586 return data_end;
3587#else
3588 assert(final_worker != -1);
3589 {
3590 TileWorkerData *const twd =
3591 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003592 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003593 }
3594#endif // CONFIG_ANS
3595#endif // CONFIG_EXT_TILE
3596}
3597
3598static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003599 AV1_COMMON *const cm = (AV1_COMMON *)data;
3600 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003601}
3602
Yaowu Xuf883b422016-08-30 14:01:10 -07003603static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3604 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003605 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003606 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
3607#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003608 cm->use_highbitdepth = 1;
3609#endif
3610 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003611 cm->bit_depth = AOM_BITS_8;
3612#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003613 cm->use_highbitdepth = 0;
3614#endif
3615 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003616 cm->color_space = aom_rb_read_literal(rb, 3);
3617 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003618 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07003619 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003620 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003621 cm->subsampling_x = aom_rb_read_bit(rb);
3622 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003623 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003624 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003625 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07003626 if (aom_rb_read_bit(rb))
3627 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003628 "Reserved bit set");
3629 } else {
3630 cm->subsampling_y = cm->subsampling_x = 1;
3631 }
3632 } else {
3633 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
3634 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
3635 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
3636 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003637 if (aom_rb_read_bit(rb))
3638 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003639 "Reserved bit set");
3640 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003641 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003642 "4:4:4 color not supported in profile 0 or 2");
3643 }
3644 }
3645}
3646
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003647#if CONFIG_REFERENCE_BUFFER
3648void read_sequence_header(SequenceHeader *seq_params) {
3649 /* Placeholder for actually reading from the bitstream */
3650 seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
3651 seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
3652 seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
3653}
3654#endif
3655
Yaowu Xuf883b422016-08-30 14:01:10 -07003656static size_t read_uncompressed_header(AV1Decoder *pbi,
3657 struct aom_read_bit_buffer *rb) {
3658 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003659 MACROBLOCKD *const xd = &pbi->mb;
3660 BufferPool *const pool = cm->buffer_pool;
3661 RefCntBuffer *const frame_bufs = pool->frame_bufs;
3662 int i, mask, ref_index = 0;
3663 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003664
3665#if CONFIG_REFERENCE_BUFFER
3666 /* TODO: Move outside frame loop or inside key-frame branch */
3667 read_sequence_header(&pbi->seq_params);
3668#endif
3669
Yaowu Xuc27fc142016-08-22 16:08:15 -07003670 cm->last_frame_type = cm->frame_type;
3671 cm->last_intra_only = cm->intra_only;
3672
3673#if CONFIG_EXT_REFS
3674 // NOTE: By default all coded frames to be used as a reference
3675 cm->is_reference_frame = 1;
3676#endif // CONFIG_EXT_REFS
3677
Yaowu Xuf883b422016-08-30 14:01:10 -07003678 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
3679 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003680 "Invalid frame marker");
3681
Yaowu Xuf883b422016-08-30 14:01:10 -07003682 cm->profile = av1_read_profile(rb);
3683#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003684 if (cm->profile >= MAX_PROFILES)
Yaowu Xuf883b422016-08-30 14:01:10 -07003685 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003686 "Unsupported bitstream profile");
3687#else
3688 if (cm->profile >= PROFILE_2)
Yaowu Xuf883b422016-08-30 14:01:10 -07003689 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003690 "Unsupported bitstream profile");
3691#endif
3692
Yaowu Xuf883b422016-08-30 14:01:10 -07003693 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003694
3695 if (cm->show_existing_frame) {
Yaowu Xu415ba932016-12-27 11:17:32 -08003696 // Show an existing frame directly.
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003697 const int existing_frame_idx = aom_rb_read_literal(rb, 3);
3698 const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
Yaowu Xu415ba932016-12-27 11:17:32 -08003699#if CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003700 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003701 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3702 int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003703 /* Compare display_frame_id with ref_frame_id and check valid for
3704 * referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003705 if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
3706 cm->valid_for_referencing[existing_frame_idx] == 0)
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003707 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3708 "Reference buffer frame ID mismatch");
3709 }
3710#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003711 lock_buffer_pool(pool);
3712 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
3713 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07003714 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003715 "Buffer %d does not contain a decoded frame",
3716 frame_to_show);
3717 }
3718 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
3719 unlock_buffer_pool(pool);
3720
3721 cm->lf.filter_level = 0;
3722 cm->show_frame = 1;
3723 pbi->refresh_frame_flags = 0;
3724
3725 if (cm->frame_parallel_decode) {
3726 for (i = 0; i < REF_FRAMES; ++i)
3727 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
3728 }
3729
3730 return 0;
3731 }
3732
Yaowu Xuf883b422016-08-30 14:01:10 -07003733 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
3734 cm->show_frame = aom_rb_read_bit(rb);
3735 cm->error_resilient_mode = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003736#if CONFIG_REFERENCE_BUFFER
3737 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003738 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3739 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
3740 int prev_frame_id = 0;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003741 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003742 prev_frame_id = cm->current_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003743 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003744 cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003745
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003746 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003747 int diff_frame_id;
3748 if (cm->current_frame_id > prev_frame_id) {
3749 diff_frame_id = cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003750 } else {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003751 diff_frame_id =
3752 (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003753 }
3754 /* Check current_frame_id for conformance */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003755 if (prev_frame_id == cm->current_frame_id ||
3756 diff_frame_id >= (1 << (frame_id_length - 1))) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003757 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3758 "Invalid value of current_frame_id");
3759 }
3760 }
3761 /* Check if some frames need to be marked as not valid for referencing */
3762 for (i = 0; i < REF_FRAMES; i++) {
3763 if (cm->frame_type == KEY_FRAME) {
3764 cm->valid_for_referencing[i] = 0;
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003765 } else if (cm->current_frame_id - (1 << diff_len) > 0) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003766 if (cm->ref_frame_id[i] > cm->current_frame_id ||
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003767 cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003768 cm->valid_for_referencing[i] = 0;
3769 } else {
3770 if (cm->ref_frame_id[i] > cm->current_frame_id &&
3771 cm->ref_frame_id[i] <
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003772 (1 << frame_id_length) + cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003773 cm->valid_for_referencing[i] = 0;
3774 }
3775 }
3776 }
3777#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003778 if (cm->frame_type == KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003779 if (!av1_read_sync_code(rb))
3780 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003781 "Invalid frame sync code");
3782
3783 read_bitdepth_colorspace_sampling(cm, rb);
3784 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
3785
3786 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3787 cm->frame_refs[i].idx = INVALID_IDX;
3788 cm->frame_refs[i].buf = NULL;
3789 }
3790
3791 setup_frame_size(cm, rb);
3792 if (pbi->need_resync) {
3793 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
3794 pbi->need_resync = 0;
3795 }
Urvang Joshib100db72016-10-12 16:28:56 -07003796#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07003797 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003798#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003799 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003800 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003801#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07003802 if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003803#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003804 if (cm->error_resilient_mode) {
3805 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
3806 } else {
3807 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003808 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003809 ? RESET_FRAME_CONTEXT_ALL
3810 : RESET_FRAME_CONTEXT_CURRENT;
3811 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003812 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003813 ? RESET_FRAME_CONTEXT_CURRENT
3814 : RESET_FRAME_CONTEXT_NONE;
3815 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07003816 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003817 ? RESET_FRAME_CONTEXT_ALL
3818 : RESET_FRAME_CONTEXT_CURRENT;
3819 }
3820 }
3821
3822 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003823 if (!av1_read_sync_code(rb))
3824 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003825 "Invalid frame sync code");
3826
3827 read_bitdepth_colorspace_sampling(cm, rb);
3828
Yaowu Xuf883b422016-08-30 14:01:10 -07003829 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003830 setup_frame_size(cm, rb);
3831 if (pbi->need_resync) {
3832 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
3833 pbi->need_resync = 0;
3834 }
3835 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Yaowu Xuf883b422016-08-30 14:01:10 -07003836 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003837
3838#if CONFIG_EXT_REFS
3839 if (!pbi->refresh_frame_flags) {
3840 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
3841 // will not be used as a reference
3842 cm->is_reference_frame = 0;
3843 }
3844#endif // CONFIG_EXT_REFS
3845
3846 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003847 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003848 const int idx = cm->ref_frame_map[ref];
3849 RefBuffer *const ref_frame = &cm->frame_refs[i];
3850 ref_frame->idx = idx;
3851 ref_frame->buf = &frame_bufs[idx].buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07003852 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003853#if CONFIG_REFERENCE_BUFFER
3854 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003855 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3856 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
3857 int delta_frame_id_minus1 = aom_rb_read_literal(rb, diff_len);
3858 int ref_frame_id =
3859 ((cm->current_frame_id - (delta_frame_id_minus1 + 1) +
3860 (1 << frame_id_length)) %
3861 (1 << frame_id_length));
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003862 /* Compare values derived from delta_frame_id_minus1 and
3863 * refresh_frame_flags. Also, check valid for referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003864 if (ref_frame_id != cm->ref_frame_id[ref] ||
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003865 cm->valid_for_referencing[ref] == 0)
3866 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3867 "Reference buffer frame ID mismatch");
3868 }
3869#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003870 }
3871
Arild Fuldseth842e9b02016-09-02 13:00:05 +02003872#if CONFIG_FRAME_SIZE
3873 if (cm->error_resilient_mode == 0) {
3874 setup_frame_size_with_refs(cm, rb);
3875 } else {
3876 setup_frame_size(cm, rb);
3877 }
3878#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003879 setup_frame_size_with_refs(cm, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02003880#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003881
Yaowu Xuf883b422016-08-30 14:01:10 -07003882 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Angie Chiang5678ad92016-11-21 09:38:40 -08003883 cm->interp_filter = read_frame_interp_filter(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003884
3885 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3886 RefBuffer *const ref_buf = &cm->frame_refs[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07003887#if CONFIG_AOM_HIGHBITDEPTH
3888 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003889 &ref_buf->sf, ref_buf->buf->y_crop_width,
3890 ref_buf->buf->y_crop_height, cm->width, cm->height,
3891 cm->use_highbitdepth);
3892#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003893 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003894 &ref_buf->sf, ref_buf->buf->y_crop_width,
3895 ref_buf->buf->y_crop_height, cm->width, cm->height);
3896#endif
3897 }
3898 }
3899 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003900
3901#if CONFIG_REFERENCE_BUFFER
3902 if (pbi->seq_params.frame_id_numbers_present_flag) {
3903 /* If bitmask is set, update reference frame id values and
3904 mark frames as valid for reference */
3905 int refresh_frame_flags =
3906 cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
3907 for (i = 0; i < REF_FRAMES; i++) {
3908 if ((refresh_frame_flags >> i) & 1) {
3909 cm->ref_frame_id[i] = cm->current_frame_id;
3910 cm->valid_for_referencing[i] = 1;
3911 }
3912 }
3913 }
3914#endif
3915
Yaowu Xuf883b422016-08-30 14:01:10 -07003916#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003917 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
3918#endif
3919 get_frame_new_buffer(cm)->color_space = cm->color_space;
3920 get_frame_new_buffer(cm)->color_range = cm->color_range;
3921 get_frame_new_buffer(cm)->render_width = cm->render_width;
3922 get_frame_new_buffer(cm)->render_height = cm->render_height;
3923
3924 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003925 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003926 "Keyframe / intra-only frame required to reset decoder"
3927 " state");
3928 }
3929
3930 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003931 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003932 ? REFRESH_FRAME_CONTEXT_FORWARD
3933 : REFRESH_FRAME_CONTEXT_BACKWARD;
3934 } else {
3935 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
3936 }
3937
Yaowu Xuf883b422016-08-30 14:01:10 -07003938 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07003939 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07003940 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003941
3942 // Generate next_ref_frame_map.
3943 lock_buffer_pool(pool);
3944 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
3945 if (mask & 1) {
3946 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
3947 ++frame_bufs[cm->new_fb_idx].ref_count;
3948 } else {
3949 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
3950 }
3951 // Current thread holds the reference frame.
3952 if (cm->ref_frame_map[ref_index] >= 0)
3953 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
3954 ++ref_index;
3955 }
3956
3957 for (; ref_index < REF_FRAMES; ++ref_index) {
3958 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
3959
3960 // Current thread holds the reference frame.
3961 if (cm->ref_frame_map[ref_index] >= 0)
3962 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
3963 }
3964 unlock_buffer_pool(pool);
3965 pbi->hold_ref_buf = 1;
3966
3967 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003968 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003969
3970#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07003971 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003972#else
3973 set_sb_size(cm, BLOCK_64X64);
3974#endif // CONFIG_EXT_PARTITION
3975
3976 setup_loopfilter(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003977#if CONFIG_DERING
3978 setup_dering(cm, rb);
3979#endif
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02003980#if CONFIG_CLPF
3981 setup_clpf(pbi, rb);
3982#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003983#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee874d36d2016-12-14 16:53:17 -08003984 av1_alloc_restoration_buffers(cm);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07003985 decode_restoration_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003986#endif // CONFIG_LOOP_RESTORATION
3987 setup_quantization(cm, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07003988#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003989 xd->bd = (int)cm->bit_depth;
3990#endif
3991
3992#if CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07003993 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003994 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
3995 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
3996 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
3997 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
3998 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
3999 }
4000#endif // CONFIG_ENTROPY
4001
4002 setup_segmentation(cm, rb);
4003
Arild Fuldseth07441162016-08-15 15:07:52 +02004004#if CONFIG_DELTA_Q
4005 {
4006 struct segmentation *const seg = &cm->seg;
4007 int segment_quantizer_active = 0;
4008 for (i = 0; i < MAX_SEGMENTS; i++) {
4009 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
4010 segment_quantizer_active = 1;
4011 }
4012 }
4013
Thomas Daviesf6936102016-09-05 16:51:31 +01004014 cm->delta_q_res = 1;
Arild Fuldseth07441162016-08-15 15:07:52 +02004015 if (segment_quantizer_active == 0) {
4016 cm->delta_q_present_flag = aom_rb_read_bit(rb);
4017 } else {
4018 cm->delta_q_present_flag = 0;
4019 }
4020 if (cm->delta_q_present_flag) {
4021 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01004022 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Arild Fuldseth07441162016-08-15 15:07:52 +02004023 }
4024 }
4025#endif
4026
Urvang Joshi454280d2016-10-14 16:51:44 -07004027 for (i = 0; i < MAX_SEGMENTS; ++i) {
4028 const int qindex = cm->seg.enabled
4029 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
4030 : cm->base_qindex;
4031 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
4032 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4033 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004034 }
4035
4036 setup_segmentation_dequant(cm);
4037 cm->tx_mode =
4038 (!cm->seg.enabled && xd->lossless[0]) ? ONLY_4X4 : read_tx_mode(rb);
4039 cm->reference_mode = read_frame_reference_mode(cm, rb);
4040
4041 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004042 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004043
4044 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07004045 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004046 "Invalid header size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004047 return sz;
4048}
4049
4050#if CONFIG_EXT_TX
Thomas9ac55082016-09-23 18:04:17 +01004051#if !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuf883b422016-08-30 14:01:10 -07004052static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004053 int i, j, k;
4054 int s;
4055 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004056 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004057 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4058 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
4059 for (j = 0; j < num_ext_tx_set_inter[s] - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004060 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004061 }
4062 }
4063 }
4064
4065 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004066 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004067 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4068 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
4069 for (j = 0; j < INTRA_MODES; ++j)
4070 for (k = 0; k < num_ext_tx_set_intra[s] - 1; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004071 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
4072 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004073 }
4074 }
4075 }
4076}
Thomas9ac55082016-09-23 18:04:17 +01004077#endif // !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004078#else
4079
Yaowu Xuc27fc142016-08-22 16:08:15 -07004080#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004081#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07004082static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004083 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -07004084 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004085 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
Jingning Hanfeb517c2016-12-21 16:02:07 -08004086 for (j = TX_8X8; j < TX_SIZES; ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004087 av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004088 }
4089 }
4090 }
4091}
4092#endif // CONFIG_SUPERTX
4093
4094#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +00004095static void read_global_motion_params(WarpedMotionParams *params,
Yaowu Xuf883b422016-08-30 14:01:10 -07004096 aom_prob *probs, aom_reader *r) {
David Barkercf3d0b02016-11-10 10:14:49 +00004097 TransformationType type =
Michael Bebenita6048d052016-08-25 14:40:54 -07004098 aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004099 set_default_gmparams(params);
David Barkercf3d0b02016-11-10 10:14:49 +00004100 params->wmtype = type;
4101 switch (type) {
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004102 case HOMOGRAPHY:
4103 params->wmmat[6] = aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004104 GM_ROW3HOMO_DECODE_FACTOR;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004105 params->wmmat[7] = aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004106 GM_ROW3HOMO_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004107 case AFFINE:
4108 case ROTZOOM:
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004109 params->wmmat[2] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004110 GM_ALPHA_DECODE_FACTOR +
David Barkercf3d0b02016-11-10 10:14:49 +00004111 (1 << WARPEDMODEL_PREC_BITS);
4112 params->wmmat[3] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4113 GM_ALPHA_DECODE_FACTOR;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004114 if (type == AFFINE || type == HOMOGRAPHY) {
4115 params->wmmat[4] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004116 GM_ALPHA_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004117 params->wmmat[5] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4118 GM_ALPHA_DECODE_FACTOR +
4119 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004120 } else {
David Barkercf3d0b02016-11-10 10:14:49 +00004121 params->wmmat[4] = -params->wmmat[3];
4122 params->wmmat[5] = params->wmmat[2];
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004123 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004124 // fallthrough intended
David Barkercf3d0b02016-11-10 10:14:49 +00004125 case TRANSLATION:
4126 params->wmmat[0] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4127 GM_TRANS_DECODE_FACTOR;
4128 params->wmmat[1] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4129 GM_TRANS_DECODE_FACTOR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004130 break;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004131 case IDENTITY: break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004132 default: assert(0);
4133 }
4134}
4135
Yaowu Xuf883b422016-08-30 14:01:10 -07004136static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004137 int frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004138 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
4139 read_global_motion_params(&cm->global_motion[frame],
4140 cm->fc->global_motion_types_prob, r);
Sarah Parkere5299862016-08-16 14:57:37 -07004141 /*
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004142 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4143 frame, cm->current_video_frame, cm->show_frame,
David Barkercf3d0b02016-11-10 10:14:49 +00004144 cm->global_motion[frame].wmmat[0],
4145 cm->global_motion[frame].wmmat[1],
4146 cm->global_motion[frame].wmmat[2],
4147 cm->global_motion[frame].wmmat[3]);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004148 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004149 }
4150}
4151#endif // CONFIG_GLOBAL_MOTION
4152
Yaowu Xuf883b422016-08-30 14:01:10 -07004153static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004154 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004155 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004156#if CONFIG_SUPERTX
4157 MACROBLOCKD *const xd = &pbi->mb;
4158#endif
4159 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07004160 aom_reader r;
Yaowu Xu8af861b2016-11-01 12:12:11 -07004161 int k, i;
Yaowu Xud0af64f2016-11-17 12:50:47 -08004162#if !CONFIG_EC_ADAPT || CONFIG_EXT_INTRA
Yaowu Xu8af861b2016-11-01 12:12:11 -07004163 int j;
4164#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004165
Alex Converse2cdf0d82016-12-13 13:53:09 -08004166 if (aom_reader_init(&r, data, partition_size,
4167#if CONFIG_ANS && ANS_MAX_SYMBOLS
4168 ANS_MAX_SYMBOLS,
4169#endif
4170 pbi->decrypt_cb, pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07004171 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004172 "Failed to allocate bool decoder 0");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004173
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004174#if CONFIG_LOOP_RESTORATION
4175 decode_restoration(cm, &r);
4176#endif
4177
Yaowu Xuefc75352016-10-31 09:46:42 -07004178 if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004179
Yushin Cho77bba8d2016-11-04 16:36:56 -07004180#if !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004181 read_coef_probs(fc, cm->tx_mode, &r);
4182
4183#if CONFIG_VAR_TX
4184 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004185 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
Yushin Cho77bba8d2016-11-04 16:36:56 -07004186#endif // CONFIG_VAR_TX
4187#endif // !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004188 for (k = 0; k < SKIP_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004189 av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004190
Thomas Daviesf6936102016-09-05 16:51:31 +01004191#if CONFIG_DELTA_Q
4192 for (k = 0; k < DELTA_Q_CONTEXTS; ++k)
4193 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
4194#endif
4195
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004196#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004197 if (cm->seg.enabled && cm->seg.update_map) {
4198 if (cm->seg.temporal_update) {
4199 for (k = 0; k < PREDICTION_PROBS; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004200 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004201 }
4202 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004203 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004204 }
4205
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004206 for (j = 0; j < INTRA_MODES; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004207 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004208 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004209 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004210
4211#if CONFIG_EXT_PARTITION_TYPES
4212 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004213 av1_diff_update_prob(&r, &fc->partition_prob[0][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004214 for (j = 1; j < PARTITION_CONTEXTS; ++j)
4215 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004216 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004217#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004218 for (j = 0; j < PARTITION_CONTEXTS; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004219 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004220 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004221#endif // CONFIG_EXT_PARTITION_TYPES
Thomas9ac55082016-09-23 18:04:17 +01004222#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004223#if CONFIG_EXT_INTRA
hui sueda3d762016-12-06 16:58:23 -08004224#if CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004225 for (i = 0; i < INTRA_FILTERS + 1; ++i)
4226 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004227 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
hui sueda3d762016-12-06 16:58:23 -08004228#endif // CONFIG_INTRA_INTERP
Thomas Davies6519beb2016-10-19 14:46:07 +01004229#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004230
4231 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004232 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004233#if CONFIG_DAALA_EC
4234 av1_copy(cm->kf_y_cdf, av1_kf_y_mode_cdf);
4235#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004236#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004237 for (k = 0; k < INTRA_MODES; k++)
Thomas Davies6519beb2016-10-19 14:46:07 +01004238 for (j = 0; j < INTRA_MODES; j++)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004239 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004240 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004241#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004242 } else {
4243#if !CONFIG_REF_MV
4244 nmv_context *const nmvc = &fc->nmvc;
4245#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004246 read_inter_mode_probs(fc, &r);
4247
4248#if CONFIG_EXT_INTER
4249 read_inter_compound_mode_probs(fc, &r);
4250 if (cm->reference_mode != COMPOUND_REFERENCE) {
4251 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4252 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004253 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004254 }
4255 }
4256 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4257 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004258 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004259 }
4260 for (i = 0; i < BLOCK_SIZES; i++) {
4261 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004262 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004263 }
4264 }
4265 }
4266 if (cm->reference_mode != SINGLE_REFERENCE) {
4267 for (i = 0; i < BLOCK_SIZES; i++) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08004268 for (j = 0; j < COMPOUND_TYPES - 1; j++) {
4269 av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004270 }
4271 }
4272 }
4273#endif // CONFIG_EXT_INTER
4274
Yue Chencb60b182016-10-13 15:18:22 -07004275#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004276 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
Yue Chencb60b182016-10-13 15:18:22 -07004277 for (j = 0; j < MOTION_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004278 av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004279 }
Yue Chencb60b182016-10-13 15:18:22 -07004280#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004281
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004282#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004283 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004284#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004285
4286 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004287 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004288
4289 if (cm->reference_mode != SINGLE_REFERENCE)
4290 setup_compound_reference_mode(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004291 read_frame_reference_mode_probs(cm, &r);
4292
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004293#if !CONFIG_EC_ADAPT
Nathan E. Egge5710c722016-09-08 10:01:16 -04004294 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004295 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004296 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
Nathan E. Egge5710c722016-09-08 10:01:16 -04004297 }
Thomas9ac55082016-09-23 18:04:17 +01004298#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004299
4300#if CONFIG_REF_MV
4301 for (i = 0; i < NMV_CONTEXTS; ++i)
4302 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
4303#else
4304 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
4305#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004306#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004307 read_ext_tx_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004308#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004309#if CONFIG_SUPERTX
4310 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
4311#endif
4312#if CONFIG_GLOBAL_MOTION
4313 read_global_motion(cm, &r);
Thomas Davies6519beb2016-10-19 14:46:07 +01004314#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004315 }
Thomas Davies6519beb2016-10-19 14:46:07 +01004316#if CONFIG_EC_MULTISYMBOL
4317 av1_coef_pareto_cdfs(fc);
David Barker599dfd02016-11-10 13:20:12 +00004318#if CONFIG_REF_MV
4319 for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
4320#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004321 av1_set_mv_cdfs(&fc->nmvc);
David Barker599dfd02016-11-10 13:20:12 +00004322#endif
Nathan E. Egge31296062016-11-16 09:44:26 -05004323#if CONFIG_EC_MULTISYMBOL
Thomas Davies6519beb2016-10-19 14:46:07 +01004324 av1_set_mode_cdfs(cm);
4325#endif
4326#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004327
Yaowu Xuf883b422016-08-30 14:01:10 -07004328 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004329}
4330
4331#ifdef NDEBUG
4332#define debug_check_frame_counts(cm) (void)0
4333#else // !NDEBUG
4334// Counts should only be incremented when frame_parallel_decoding_mode and
4335// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07004336static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004337 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07004338 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004339 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
4340 cm->error_resilient_mode);
4341 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
4342 sizeof(cm->counts.y_mode)));
4343 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
4344 sizeof(cm->counts.uv_mode)));
4345 assert(!memcmp(cm->counts.partition, zero_counts.partition,
4346 sizeof(cm->counts.partition)));
4347 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
4348 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
4349 sizeof(cm->counts.eob_branch)));
4350 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
4351 sizeof(cm->counts.switchable_interp)));
4352 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
4353 sizeof(cm->counts.inter_mode)));
4354#if CONFIG_EXT_INTER
4355 assert(!memcmp(cm->counts.inter_compound_mode,
4356 zero_counts.inter_compound_mode,
4357 sizeof(cm->counts.inter_compound_mode)));
4358 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
4359 sizeof(cm->counts.interintra)));
4360 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
4361 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08004362 assert(!memcmp(cm->counts.compound_interinter,
4363 zero_counts.compound_interinter,
4364 sizeof(cm->counts.compound_interinter)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004365#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07004366#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
4367 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
4368 sizeof(cm->counts.motion_mode)));
4369#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004370 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
4371 sizeof(cm->counts.intra_inter)));
4372 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
4373 sizeof(cm->counts.comp_inter)));
4374 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
4375 sizeof(cm->counts.single_ref)));
4376 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
4377 sizeof(cm->counts.comp_ref)));
4378#if CONFIG_EXT_REFS
4379 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
4380 sizeof(cm->counts.comp_bwdref)));
4381#endif // CONFIG_EXT_REFS
4382 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
4383 sizeof(cm->counts.tx_size)));
4384 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
4385#if CONFIG_REF_MV
4386 assert(
4387 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
4388 assert(
4389 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
4390#else
4391 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
4392#endif
4393 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
4394 sizeof(cm->counts.inter_ext_tx)));
4395 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
4396 sizeof(cm->counts.intra_ext_tx)));
4397}
4398#endif // NDEBUG
4399
Yaowu Xuf883b422016-08-30 14:01:10 -07004400static struct aom_read_bit_buffer *init_read_bit_buffer(
4401 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
4402 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004403 rb->bit_offset = 0;
4404 rb->error_handler = error_handler;
4405 rb->error_handler_data = &pbi->common;
4406 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004407 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004408 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
4409 rb->bit_buffer = clear_data;
4410 rb->bit_buffer_end = clear_data + n;
4411 } else {
4412 rb->bit_buffer = data;
4413 rb->bit_buffer_end = data_end;
4414 }
4415 return rb;
4416}
4417
4418//------------------------------------------------------------------------------
4419
Yaowu Xuf883b422016-08-30 14:01:10 -07004420int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
4421 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
4422 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
4423 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004424}
4425
Yaowu Xuf883b422016-08-30 14:01:10 -07004426void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
4427 int *height) {
4428 *width = aom_rb_read_literal(rb, 16) + 1;
4429 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004430}
4431
Yaowu Xuf883b422016-08-30 14:01:10 -07004432BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
4433 int profile = aom_rb_read_bit(rb);
4434 profile |= aom_rb_read_bit(rb) << 1;
4435 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004436 return (BITSTREAM_PROFILE)profile;
4437}
4438
Yaowu Xuf883b422016-08-30 14:01:10 -07004439void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
4440 const uint8_t *data_end, const uint8_t **p_data_end) {
4441 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004442 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07004443 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004444 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004445 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004446 size_t first_partition_size;
4447 YV12_BUFFER_CONFIG *new_fb;
4448
4449#if CONFIG_BITSTREAM_DEBUG
4450 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
4451#endif
4452
4453 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004454 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Thomas Davies72712e62016-11-09 12:17:51 +00004455#if CONFIG_TILE_GROUPS
4456 pbi->first_partition_size = first_partition_size;
4457 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
4458#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004459 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004460 xd->cur_buf = new_fb;
4461#if CONFIG_GLOBAL_MOTION
4462 xd->global_motion = cm->global_motion;
4463#endif // CONFIG_GLOBAL_MOTION
4464
4465 if (!first_partition_size) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004466 // showing a frame directly
4467 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004468 return;
4469 }
4470
Yaowu Xuf883b422016-08-30 14:01:10 -07004471 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004472 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07004473 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004474 "Truncated packet or corrupt header length");
4475
Jingning Han24e0a182016-11-20 22:34:12 -08004476#if CONFIG_REF_MV
Dengca8d24d2016-10-17 14:06:35 +08004477 cm->setup_mi(cm);
4478#endif
4479
Yaowu Xuc27fc142016-08-22 16:08:15 -07004480 cm->use_prev_frame_mvs =
4481 !cm->error_resilient_mode && cm->width == cm->last_width &&
4482 cm->height == cm->last_height && !cm->last_intra_only &&
4483 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
4484#if CONFIG_EXT_REFS
4485 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
4486 // show_exisiting_frame=1, nor can it take a frame not used as
4487 // a reference, it is probable that by the time it is being
4488 // referred to, the frame buffer it originally points to may
4489 // already get expired and have been reassigned to the current
4490 // newly coded frame. Hence, we need to check whether this is
4491 // the case, and if yes, we have 2 choices:
4492 // (1) Simply disable the use of previous frame mvs; or
4493 // (2) Have cm->prev_frame point to one reference frame buffer,
4494 // e.g. LAST_FRAME.
4495 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
4496 // Reassign the LAST_FRAME buffer to cm->prev_frame.
4497 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4498 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4499 }
4500#endif // CONFIG_EXT_REFS
4501
Yaowu Xuf883b422016-08-30 14:01:10 -07004502 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004503
4504 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
4505 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07004506 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004507 "Uninitialized entropy context.");
4508
Yaowu Xuf883b422016-08-30 14:01:10 -07004509 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004510
4511 xd->corrupted = 0;
4512 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
4513 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07004514 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004515 "Decode failed. Frame data header is corrupted.");
4516
4517 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004518 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004519 }
4520
4521 // If encoded in frame parallel mode, frame context is ready after decoding
4522 // the frame header.
4523 if (cm->frame_parallel_decode &&
4524 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004525 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004526 FrameWorkerData *const frame_worker_data = worker->data1;
4527 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
4528 context_updated = 1;
4529 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4530 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004531 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004532 pbi->cur_buf->row = -1;
4533 pbi->cur_buf->col = -1;
4534 frame_worker_data->frame_context_ready = 1;
4535 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07004536 av1_frameworker_signal_stats(worker);
4537 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004538 }
4539
4540#if CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004541 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004542 cm->coef_probs_update_idx = 0;
4543#endif // CONFIG_ENTROPY
4544
4545 if (pbi->max_threads > 1
4546#if CONFIG_EXT_TILE
4547 && pbi->dec_tile_col < 0 // Decoding all columns
4548#endif // CONFIG_EXT_TILE
4549 && cm->tile_cols > 1) {
4550 // Multi-threaded tile decoder
4551 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
4552 if (!xd->corrupted) {
4553 if (!cm->skip_loop_filter) {
4554 // If multiple threads are used to decode tiles, then we use those
4555 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07004556 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
4557 0, 0, pbi->tile_workers, pbi->num_tile_workers,
4558 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004559 }
4560 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004561 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004562 "Decode failed. Frame data is corrupted.");
4563 }
4564 } else {
4565 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
4566 }
4567#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee874d36d2016-12-14 16:53:17 -08004568 if (cm->rst_info.frame_restoration_type != RESTORE_NONE) {
4569 av1_loop_restoration_frame(new_fb, cm, &cm->rst_info, 0, 0, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004570 }
4571#endif // CONFIG_LOOP_RESTORATION
4572
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004573#if CONFIG_DERING
4574 if (cm->dering_level && !cm->skip_loop_filter) {
4575 av1_dering_frame(&pbi->cur_buf->buf, cm, &pbi->mb, cm->dering_level);
4576 }
4577#endif // CONFIG_DERING
4578
Thomas Daedef56859f2016-04-19 16:57:24 -07004579#if CONFIG_CLPF
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004580 if (!cm->skip_loop_filter) {
4581 const YV12_BUFFER_CONFIG *const frame = &pbi->cur_buf->buf;
4582 if (cm->clpf_strength_y) {
Yaowu Xud71be782016-10-14 08:47:03 -07004583 av1_clpf_frame(frame, NULL, cm, cm->clpf_size != CLPF_NOSIZE,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004584 cm->clpf_strength_y + (cm->clpf_strength_y == 3),
Yaowu Xud71be782016-10-14 08:47:03 -07004585 4 + cm->clpf_size, AOM_PLANE_Y, clpf_bit);
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004586 }
4587 if (cm->clpf_strength_u) {
Yaowu Xud71be782016-10-14 08:47:03 -07004588 av1_clpf_frame(frame, NULL, cm, 0, // No block signals for chroma
4589 cm->clpf_strength_u + (cm->clpf_strength_u == 3), 4,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004590 AOM_PLANE_U, NULL);
4591 }
4592 if (cm->clpf_strength_v) {
Yaowu Xud71be782016-10-14 08:47:03 -07004593 av1_clpf_frame(frame, NULL, cm, 0, // No block signals for chroma
4594 cm->clpf_strength_v + (cm->clpf_strength_v == 3), 4,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004595 AOM_PLANE_V, NULL);
4596 }
Steinar Midtskogend06588a2016-05-06 13:48:20 +02004597 }
4598 if (cm->clpf_blocks) aom_free(cm->clpf_blocks);
Thomas Daedef56859f2016-04-19 16:57:24 -07004599#endif
Thomas Daedef56859f2016-04-19 16:57:24 -07004600
Yaowu Xuc27fc142016-08-22 16:08:15 -07004601 if (!xd->corrupted) {
4602 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
4603#if CONFIG_ENTROPY
4604 cm->partial_prob_update = 0;
4605#endif // CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004606 av1_adapt_coef_probs(cm);
4607 av1_adapt_intra_frame_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004608
4609 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004610 av1_adapt_inter_frame_probs(cm);
4611 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004612 }
4613 } else {
4614 debug_check_frame_counts(cm);
4615 }
4616 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004617 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004618 "Decode failed. Frame data is corrupted.");
4619 }
4620
4621 // Non frame parallel update frame context here.
4622 if (!cm->error_resilient_mode && !context_updated)
4623 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4624}