blob: f3135de0997e4bc51d0cf2aa65f881de01f17bff [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <assert.h>
13#include <stdlib.h> // qsort()
14
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "./aom_config.h"
16#include "./aom_dsp_rtcd.h"
17#include "./aom_scale_rtcd.h"
Jingning Han1aab8182016-06-03 11:09:06 -070018#include "./av1_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070019
Steinar Midtskogen2fd70ee2016-09-02 10:02:30 +020020#include "aom/aom_codec.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070021#include "aom_dsp/aom_dsp_common.h"
Jingning Han1aab8182016-06-03 11:09:06 -070022#include "aom_dsp/bitreader.h"
23#include "aom_dsp/bitreader_buffer.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070024#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070025#include "aom_ports/mem.h"
26#include "aom_ports/mem_ops.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070027#include "aom_scale/aom_scale.h"
28#include "aom_util/aom_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070029
30#include "av1/common/alloccommon.h"
31#if CONFIG_CLPF
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +020032#include "aom/aom_image.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070033#include "av1/common/clpf.h"
34#endif
35#include "av1/common/common.h"
36#if CONFIG_DERING
37#include "av1/common/dering.h"
38#endif // CONFIG_DERING
39#include "av1/common/entropy.h"
40#include "av1/common/entropymode.h"
Thomas Davies6519beb2016-10-19 14:46:07 +010041#include "av1/common/entropymv.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070042#include "av1/common/idct.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070043#include "av1/common/pred_common.h"
44#include "av1/common/quant_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070045#include "av1/common/reconinter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070046#include "av1/common/reconintra.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070047#include "av1/common/seg_common.h"
Jingning Han1aab8182016-06-03 11:09:06 -070048#include "av1/common/thread_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070049#include "av1/common/tile_common.h"
50
51#include "av1/decoder/decodeframe.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070052#include "av1/decoder/decodemv.h"
53#include "av1/decoder/decoder.h"
Jingning Han1aab8182016-06-03 11:09:06 -070054#include "av1/decoder/detokenize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070055#include "av1/decoder/dsubexp.h"
56
Yue Chen69f18e12016-09-08 14:48:15 -070057#if CONFIG_WARPED_MOTION
58#include "av1/common/warped_motion.h"
59#endif // CONFIG_WARPED_MOTION
60
Yaowu Xuf883b422016-08-30 14:01:10 -070061#define MAX_AV1_HEADER_SIZE 80
Michael Bebenita6048d052016-08-25 14:40:54 -070062#define ACCT_STR __func__
Yaowu Xuc27fc142016-08-22 16:08:15 -070063
Yushin Cho77bba8d2016-11-04 16:36:56 -070064#if CONFIG_PVQ
65#include "av1/decoder/pvq_decoder.h"
Yushin Cho70669122016-12-08 09:53:14 -100066#include "av1/common/pvq.h"
Yushin Cho77bba8d2016-11-04 16:36:56 -070067#include "av1/encoder/encodemb.h"
68
Yushin Cho77bba8d2016-11-04 16:36:56 -070069#include "av1/common/partition.h"
70#include "av1/decoder/decint.h"
71#include "av1/encoder/hybrid_fwd_txfm.h"
72#endif
73
Thomas Davies80188d12016-10-26 16:08:35 -070074static struct aom_read_bit_buffer *init_read_bit_buffer(
75 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
76 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]);
77static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
78 size_t partition_size);
79static size_t read_uncompressed_header(AV1Decoder *pbi,
80 struct aom_read_bit_buffer *rb);
81
Yaowu Xuf883b422016-08-30 14:01:10 -070082static int is_compound_reference_allowed(const AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070083 int i;
84 if (frame_is_intra_only(cm)) return 0;
85 for (i = 1; i < INTER_REFS_PER_FRAME; ++i)
86 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
87
88 return 0;
89}
90
Yaowu Xuf883b422016-08-30 14:01:10 -070091static void setup_compound_reference_mode(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070092#if CONFIG_EXT_REFS
93 cm->comp_fwd_ref[0] = LAST_FRAME;
94 cm->comp_fwd_ref[1] = LAST2_FRAME;
95 cm->comp_fwd_ref[2] = LAST3_FRAME;
96 cm->comp_fwd_ref[3] = GOLDEN_FRAME;
97
98 cm->comp_bwd_ref[0] = BWDREF_FRAME;
99 cm->comp_bwd_ref[1] = ALTREF_FRAME;
100#else
101 if (cm->ref_frame_sign_bias[LAST_FRAME] ==
102 cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
103 cm->comp_fixed_ref = ALTREF_FRAME;
104 cm->comp_var_ref[0] = LAST_FRAME;
105 cm->comp_var_ref[1] = GOLDEN_FRAME;
106 } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
107 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
108 cm->comp_fixed_ref = GOLDEN_FRAME;
109 cm->comp_var_ref[0] = LAST_FRAME;
110 cm->comp_var_ref[1] = ALTREF_FRAME;
111 } else {
112 cm->comp_fixed_ref = LAST_FRAME;
113 cm->comp_var_ref[0] = GOLDEN_FRAME;
114 cm->comp_var_ref[1] = ALTREF_FRAME;
115 }
116#endif // CONFIG_EXT_REFS
117}
118
119static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
120 return len != 0 && len <= (size_t)(end - start);
121}
122
Yaowu Xuf883b422016-08-30 14:01:10 -0700123static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
124 const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700125 return data > max ? max : data;
126}
127
Yaowu Xuf883b422016-08-30 14:01:10 -0700128static TX_MODE read_tx_mode(struct aom_read_bit_buffer *rb) {
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800129#if CONFIG_TX64X64
130 TX_MODE tx_mode =
131 aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
132 if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
133 return tx_mode;
134#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700135 return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800136#endif // CONFIG_TX64X64
Yaowu Xuc27fc142016-08-22 16:08:15 -0700137}
138
Yaowu Xuefc75352016-10-31 09:46:42 -0700139static void read_tx_size_probs(FRAME_CONTEXT *fc, aom_reader *r) {
140 int i, j, k;
141 for (i = 0; i < MAX_TX_DEPTH; ++i)
142 for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
143 for (k = 0; k < i + 1; ++k)
144 av1_diff_update_prob(r, &fc->tx_size_probs[i][j][k], ACCT_STR);
145}
146
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400147#if !CONFIG_EC_ADAPT
Yaowu Xuf883b422016-08-30 14:01:10 -0700148static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700149 int i, j;
Nathan E. Egge4947c292016-04-26 11:37:06 -0400150 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700151 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700152 av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i], ACCT_STR);
Nathan E. Egge4947c292016-04-26 11:37:06 -0400153 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700154}
Thomas Davies6519beb2016-10-19 14:46:07 +0100155#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700156
Yaowu Xuf883b422016-08-30 14:01:10 -0700157static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700158#if CONFIG_REF_MV
Yaowu Xu8af861b2016-11-01 12:12:11 -0700159 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700160 for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700161 av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700162 for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700163 av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700164 for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700165 av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700166 for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700167 av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700168#if CONFIG_EXT_INTER
Michael Bebenita6048d052016-08-25 14:40:54 -0700169 av1_diff_update_prob(r, &fc->new2mv_prob, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700170#endif // CONFIG_EXT_INTER
171#else
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400172#if !CONFIG_EC_ADAPT
Yaowu Xu8af861b2016-11-01 12:12:11 -0700173 int i, j;
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400174 for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700175 for (j = 0; j < INTER_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700176 av1_diff_update_prob(r, &fc->inter_mode_probs[i][j], ACCT_STR);
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400177 }
Yaowu Xu8af861b2016-11-01 12:12:11 -0700178#else
179 (void)fc;
180 (void)r;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700181#endif
Thomas Davies6519beb2016-10-19 14:46:07 +0100182#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700183}
184
185#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700186static void read_inter_compound_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700187 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -0700188 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700189 for (j = 0; j < INTER_MODE_CONTEXTS; ++j) {
190 for (i = 0; i < INTER_COMPOUND_MODES - 1; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700191 av1_diff_update_prob(r, &fc->inter_compound_mode_probs[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700192 }
193 }
194 }
195}
196#endif // CONFIG_EXT_INTER
Yaowu Xu17fd2f22016-11-17 18:23:28 -0800197#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100198#if !CONFIG_EXT_TX
199static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
200 int i, j, k;
201 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
202 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
203 for (j = 0; j < TX_TYPES; ++j) {
204 for (k = 0; k < TX_TYPES - 1; ++k)
205 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[i][j][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100206 }
207 }
208 }
209 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
210 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
211 for (k = 0; k < TX_TYPES - 1; ++k)
212 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[i][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100213 }
214 }
215}
216#endif
217#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700218
219static REFERENCE_MODE read_frame_reference_mode(
Yaowu Xuf883b422016-08-30 14:01:10 -0700220 const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700221 if (is_compound_reference_allowed(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700222 return aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700223 ? REFERENCE_MODE_SELECT
Yaowu Xuf883b422016-08-30 14:01:10 -0700224 : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700225 } else {
226 return SINGLE_REFERENCE;
227 }
228}
229
Yaowu Xuf883b422016-08-30 14:01:10 -0700230static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700231 FRAME_CONTEXT *const fc = cm->fc;
232 int i, j;
233
234 if (cm->reference_mode == REFERENCE_MODE_SELECT)
235 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700236 av1_diff_update_prob(r, &fc->comp_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700237
238 if (cm->reference_mode != COMPOUND_REFERENCE) {
239 for (i = 0; i < REF_CONTEXTS; ++i) {
240 for (j = 0; j < (SINGLE_REFS - 1); ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700241 av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700242 }
243 }
244 }
245
246 if (cm->reference_mode != SINGLE_REFERENCE) {
247 for (i = 0; i < REF_CONTEXTS; ++i) {
248#if CONFIG_EXT_REFS
249 for (j = 0; j < (FWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700250 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700251 for (j = 0; j < (BWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700252 av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700253#else
254 for (j = 0; j < (COMP_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700255 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700256#endif // CONFIG_EXT_REFS
257 }
258 }
259}
260
Yaowu Xuf883b422016-08-30 14:01:10 -0700261static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700262 int i;
Michael Bebenita6048d052016-08-25 14:40:54 -0700263 for (i = 0; i < n; ++i) av1_diff_update_prob(r, &p[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700264}
265
Yaowu Xuf883b422016-08-30 14:01:10 -0700266static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Thomas9ac55082016-09-23 18:04:17 +0100267 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700268
Yaowu Xue86288d2016-10-31 15:56:38 -0700269#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100270 int j;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700271 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
272
Yaowu Xuc27fc142016-08-22 16:08:15 -0700273 for (i = 0; i < 2; ++i) {
274 nmv_component *const comp_ctx = &ctx->comps[i];
275 update_mv_probs(&comp_ctx->sign, 1, r);
276 update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
277 update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
278 update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
279 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700280 for (i = 0; i < 2; ++i) {
281 nmv_component *const comp_ctx = &ctx->comps[i];
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400282 for (j = 0; j < CLASS0_SIZE; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700283 update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400284 }
Nathan E. Eggeac499f32016-09-08 15:38:57 -0400285 update_mv_probs(comp_ctx->fp, MV_FP_SIZE - 1, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700286 }
Alex Converseaca9feb2016-10-10 11:08:10 -0700287#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700288
289 if (allow_hp) {
290 for (i = 0; i < 2; ++i) {
291 nmv_component *const comp_ctx = &ctx->comps[i];
292 update_mv_probs(&comp_ctx->class0_hp, 1, r);
293 update_mv_probs(&comp_ctx->hp, 1, r);
294 }
295 }
296}
297
298static void inverse_transform_block(MACROBLOCKD *xd, int plane,
299 const TX_TYPE tx_type,
300 const TX_SIZE tx_size, uint8_t *dst,
Jingning Han1be18782016-10-21 11:48:15 -0700301 int stride, int16_t scan_line, int eob) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700302 struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han1be18782016-10-21 11:48:15 -0700303 tran_low_t *const dqcoeff = pd->dqcoeff;
304 INV_TXFM_PARAM inv_txfm_param;
305 inv_txfm_param.tx_type = tx_type;
306 inv_txfm_param.tx_size = tx_size;
307 inv_txfm_param.eob = eob;
308 inv_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700309
Yaowu Xuf883b422016-08-30 14:01:10 -0700310#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han1be18782016-10-21 11:48:15 -0700311 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
312 inv_txfm_param.bd = xd->bd;
313 highbd_inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
314 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700315#endif // CONFIG_AOM_HIGHBITDEPTH
Jingning Han1be18782016-10-21 11:48:15 -0700316 inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
Yaowu Xuf883b422016-08-30 14:01:10 -0700317#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700318 }
Jingning Han1be18782016-10-21 11:48:15 -0700319#endif // CONFIG_AOM_HIGHBITDEPTH
320 memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700321}
322
Yushin Cho77bba8d2016-11-04 16:36:56 -0700323#if CONFIG_PVQ
324static int av1_pvq_decode_helper(od_dec_ctx *dec, int16_t *ref_coeff,
325 int16_t *dqcoeff, int16_t *quant, int pli,
326 int bs, TX_TYPE tx_type, int xdec,
327 int ac_dc_coded) {
328 unsigned int flags; // used for daala's stream analyzer.
329 int off;
330 const int is_keyframe = 0;
331 const int has_dc_skip = 1;
Debargha Mukherjee153e1f82016-11-17 09:59:14 -0800332 int quant_shift = get_tx_scale(bs);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700333 // DC quantizer for PVQ
334 int pvq_dc_quant;
335 int lossless = (quant[0] == 0);
336 const int blk_size = tx_size_wide[bs];
337 int eob = 0;
338 int i;
Yushin Cho70669122016-12-08 09:53:14 -1000339 int use_activity_masking = dec->use_activity_masking;
Yushin Cho48f84db2016-11-07 21:20:17 -0800340 DECLARE_ALIGNED(16, int16_t, dqcoeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
341 DECLARE_ALIGNED(16, int16_t, ref_coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700342
Yushin Cho48f84db2016-11-07 21:20:17 -0800343 od_coeff ref_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
344 od_coeff out_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700345
346 od_raster_to_coding_order(ref_coeff_pvq, blk_size, tx_type, ref_coeff,
347 blk_size);
348
349 if (lossless)
350 pvq_dc_quant = 1;
351 else {
Yushin Cho70669122016-12-08 09:53:14 -1000352 if (use_activity_masking)
353 pvq_dc_quant = OD_MAXI(
354 1, (quant[0] >> quant_shift) *
355 dec->state.pvq_qm_q4[pli][od_qm_get_index(bs, 0)] >>
356 4);
357 else
358 pvq_dc_quant = OD_MAXI(1, quant[0] >> quant_shift);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700359 }
360
361 off = od_qm_offset(bs, xdec);
362
363 // copy int16 inputs to int32
364 for (i = 0; i < blk_size * blk_size; i++) ref_int32[i] = ref_coeff_pvq[i];
365
366 od_pvq_decode(dec, ref_int32, out_int32, (int)quant[1] >> quant_shift, pli,
367 bs, OD_PVQ_BETA[use_activity_masking][pli][bs],
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800368 OD_ROBUST_STREAM, is_keyframe, &flags, ac_dc_coded,
369 dec->state.qm + off, dec->state.qm_inv + off);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700370
371 // copy int32 result back to int16
372 for (i = 0; i < blk_size * blk_size; i++) dqcoeff_pvq[i] = out_int32[i];
373
374 if (!has_dc_skip || dqcoeff_pvq[0]) {
375 dqcoeff_pvq[0] =
Nathan E. Egge89f58762016-12-28 16:31:50 -0500376 has_dc_skip + generic_decode(dec->r, &dec->state.adapt.model_dc[pli],
377 -1, &dec->state.adapt.ex_dc[pli][bs][0], 2,
378 "dc:mag");
Yushin Cho77bba8d2016-11-04 16:36:56 -0700379 if (dqcoeff_pvq[0])
Nathan E. Eggeab083972016-12-28 15:31:46 -0500380 dqcoeff_pvq[0] *= aom_read_bit(dec->r, "dc:sign") ? -1 : 1;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700381 }
382 dqcoeff_pvq[0] = dqcoeff_pvq[0] * pvq_dc_quant + ref_coeff_pvq[0];
383
384 od_coding_order_to_raster(dqcoeff, blk_size, tx_type, dqcoeff_pvq, blk_size);
385
386 eob = blk_size * blk_size;
387
388 return eob;
389}
390
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800391static int av1_pvq_decode_helper2(MACROBLOCKD *const xd,
392 MB_MODE_INFO *const mbmi, int plane, int row,
393 int col, TX_SIZE tx_size, TX_TYPE tx_type) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700394 struct macroblockd_plane *const pd = &xd->plane[plane];
395 // transform block size in pixels
396 int tx_blk_size = tx_size_wide[tx_size];
397 int i, j;
398 tran_low_t *pvq_ref_coeff = pd->pvq_ref_coeff;
399 const int diff_stride = tx_blk_size;
400 int16_t *pred = pd->pred;
401 tran_low_t *const dqcoeff = pd->dqcoeff;
402 int ac_dc_coded; // bit0: DC coded, bit1 : AC coded
403 uint8_t *dst;
404 int eob;
405
406 eob = 0;
407 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
408
409 // decode ac/dc coded flag. bit0: DC coded, bit1 : AC coded
410 // NOTE : we don't use 5 symbols for luma here in aom codebase,
411 // since block partition is taken care of by aom.
412 // So, only AC/DC skip info is coded
Nathan E. Eggef25bae42016-12-29 10:15:06 -0500413 ac_dc_coded = aom_decode_cdf_adapt(
414 xd->daala_dec.r,
Yushin Cho77bba8d2016-11-04 16:36:56 -0700415 xd->daala_dec.state.adapt.skip_cdf[2 * tx_size + (plane != 0)], 4,
416 xd->daala_dec.state.adapt.skip_increment, "skip");
Yushin Cho77bba8d2016-11-04 16:36:56 -0700417 if (ac_dc_coded) {
418 int xdec = pd->subsampling_x;
419 int seg_id = mbmi->segment_id;
420 int16_t *quant;
421 FWD_TXFM_PARAM fwd_txfm_param;
Yaowu Xufc1b2132016-11-07 15:16:15 -0800422 // ToDo(yaowu): correct this with optimal number from decoding process.
423 const int max_scan_line = tx_size_2d[tx_size];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700424
425 for (j = 0; j < tx_blk_size; j++)
426 for (i = 0; i < tx_blk_size; i++) {
427 pred[diff_stride * j + i] = dst[pd->dst.stride * j + i];
428 }
429
430 fwd_txfm_param.tx_type = tx_type;
431 fwd_txfm_param.tx_size = tx_size;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700432 fwd_txfm_param.lossless = xd->lossless[seg_id];
433
434 fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
435
436 quant = &pd->seg_dequant[seg_id][0]; // aom's quantizer
437
438 eob = av1_pvq_decode_helper(&xd->daala_dec, pvq_ref_coeff, dqcoeff, quant,
439 plane, tx_size, tx_type, xdec, ac_dc_coded);
440
441 // Since av1 does not have separate inverse transform
442 // but also contains adding to predicted image,
443 // pass blank dummy image to av1_inv_txfm_add_*x*(), i.e. set dst as zeros
444 for (j = 0; j < tx_blk_size; j++)
445 for (i = 0; i < tx_blk_size; i++) dst[j * pd->dst.stride + i] = 0;
446
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800447 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
448 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700449 }
450
451 return eob;
452}
453#endif
454
Angie Chiangff6d8902016-10-21 11:02:09 -0700455static void predict_and_reconstruct_intra_block(AV1_COMMON *cm,
456 MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700457#if CONFIG_ANS
458 struct AnsDecoder *const r,
459#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700460 aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700461#endif // CONFIG_ANS
462 MB_MODE_INFO *const mbmi,
463 int plane, int row, int col,
464 TX_SIZE tx_size) {
465 struct macroblockd_plane *const pd = &xd->plane[plane];
466 PREDICTION_MODE mode = (plane == 0) ? mbmi->mode : mbmi->uv_mode;
467 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
468 uint8_t *dst;
469 int block_idx = (row << 1) + col;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700470#if CONFIG_PVQ
471 (void)cm;
472 (void)r;
473#endif
Jingning Hanca14dda2016-12-09 09:36:00 -0800474 dst = &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700475
Jingning Han41bb3392016-12-14 10:46:48 -0800476#if !CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -0700477 if (mbmi->sb_type < BLOCK_8X8)
478 if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
Jingning Han41bb3392016-12-14 10:46:48 -0800479#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700480
Jingning Hanc4c99da2016-10-24 10:27:28 -0700481 av1_predict_intra_block(xd, pd->width, pd->height, tx_size, mode, dst,
Yaowu Xuf883b422016-08-30 14:01:10 -0700482 pd->dst.stride, dst, pd->dst.stride, col, row, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700483
484 if (!mbmi->skip) {
485 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700486#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700487 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 0);
Jingning Han1be18782016-10-21 11:48:15 -0700488 int16_t max_scan_line = 0;
489 const int eob =
490 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size,
491 tx_type, &max_scan_line, r, mbmi->segment_id);
Angie Chianged8cd9a2016-10-21 16:44:47 -0700492#if CONFIG_ADAPT_SCAN
493 av1_update_scan_count_facade(cm, tx_size, tx_type, pd->dqcoeff, eob);
494#endif
Jingning Han1be18782016-10-21 11:48:15 -0700495 if (eob)
496 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
497 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700498#else
499 av1_pvq_decode_helper2(xd, mbmi, plane, row, col, tx_size, tx_type);
500#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700501 }
502}
503
504#if CONFIG_VAR_TX
Angie Chiangff6d8902016-10-21 11:02:09 -0700505static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
506 aom_reader *r, MB_MODE_INFO *const mbmi,
Jingning Han8fd62b72016-10-21 12:55:54 -0700507 int plane, BLOCK_SIZE plane_bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700508 int blk_row, int blk_col, TX_SIZE tx_size,
509 int *eob_total) {
510 const struct macroblockd_plane *const pd = &xd->plane[plane];
511 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
512 const int tx_row = blk_row >> (1 - pd->subsampling_y);
513 const int tx_col = blk_col >> (1 - pd->subsampling_x);
514 const TX_SIZE plane_tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700515 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700516 : mbmi->inter_tx_size[tx_row][tx_col];
Jingning Han5f614262016-10-27 14:27:43 -0700517 // Scale to match transform block unit.
Jingning Hanf64062f2016-11-02 16:22:18 -0700518 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
519 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700520
521 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
522
523 if (tx_size == plane_tx_size) {
524 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
Jingning Han8fd62b72016-10-21 12:55:54 -0700525 int block_idx = (blk_row << 1) + blk_col;
526 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiangff6d8902016-10-21 11:02:09 -0700527 const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700528 int16_t max_scan_line = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700529 const int eob =
Yaowu Xuf883b422016-08-30 14:01:10 -0700530 av1_decode_block_tokens(xd, plane, sc, blk_col, blk_row, plane_tx_size,
Jingning Han1be18782016-10-21 11:48:15 -0700531 tx_type, &max_scan_line, r, mbmi->segment_id);
Jingning Han9ca05b72017-01-03 14:41:36 -0800532 inverse_transform_block(xd, plane, tx_type, plane_tx_size,
533 &pd->dst.buf[(blk_row * pd->dst.stride + blk_col)
534 << tx_size_wide_log2[0]],
535 pd->dst.stride, max_scan_line, eob);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700536 *eob_total += eob;
537 } else {
Jingning Hanf64062f2016-11-02 16:22:18 -0700538 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
539 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700540 int i;
541
542 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700543
544 for (i = 0; i < 4; ++i) {
Jingning Han5f614262016-10-27 14:27:43 -0700545 const int offsetr = blk_row + (i >> 1) * bsl;
546 const int offsetc = blk_col + (i & 0x01) * bsl;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700547
548 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
549
Jingning Han8fd62b72016-10-21 12:55:54 -0700550 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
Jingning Hanf64062f2016-11-02 16:22:18 -0700551 offsetc, sub_txs, eob_total);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700552 }
553 }
554}
555#endif // CONFIG_VAR_TX
556
Jingning Hanfe45b212016-11-22 10:30:23 -0800557#if !CONFIG_VAR_TX || CONFIG_SUPERTX || \
558 (!CONFIG_VAR_TX && CONFIG_EXT_TX && CONFIG_RECT_TX)
Angie Chiangff6d8902016-10-21 11:02:09 -0700559static int reconstruct_inter_block(AV1_COMMON *cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700560#if CONFIG_ANS
561 struct AnsDecoder *const r,
562#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700563 aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700564#endif
565 int segment_id, int plane, int row, int col,
566 TX_SIZE tx_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700567 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
568 int block_idx = (row << 1) + col;
569 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700570#if CONFIG_PVQ
571 int eob;
572 (void)cm;
573 (void)r;
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800574 (void)segment_id;
575#else
576 struct macroblockd_plane *const pd = &xd->plane[plane];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700577#endif
578
579#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700580 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700581 int16_t max_scan_line = 0;
582 const int eob =
583 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size, tx_type,
584 &max_scan_line, r, segment_id);
Jingning Hanca14dda2016-12-09 09:36:00 -0800585 uint8_t *dst =
586 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Angie Chianged8cd9a2016-10-21 16:44:47 -0700587#if CONFIG_ADAPT_SCAN
588 av1_update_scan_count_facade(cm, tx_size, tx_type, pd->dqcoeff, eob);
589#endif
Jingning Han1be18782016-10-21 11:48:15 -0700590 if (eob)
Jingning Hanca14dda2016-12-09 09:36:00 -0800591 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
592 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700593#else
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800594 eob = av1_pvq_decode_helper2(xd, &xd->mi[0]->mbmi, plane, row, col, tx_size,
595 tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700596#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700597 return eob;
598}
599#endif // !CONFIG_VAR_TX || CONFIG_SUPER_TX
600
Yaowu Xuf883b422016-08-30 14:01:10 -0700601static MB_MODE_INFO *set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700602 BLOCK_SIZE bsize, int mi_row, int mi_col,
Jingning Hanfaad0e12016-12-07 10:54:57 -0800603 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700604 const int offset = mi_row * cm->mi_stride + mi_col;
605 int x, y;
606 const TileInfo *const tile = &xd->tile;
607
608 xd->mi = cm->mi_grid_visible + offset;
609 xd->mi[0] = &cm->mi[offset];
610 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
611 // passing bsize from decode_partition().
612 xd->mi[0]->mbmi.sb_type = bsize;
Angie Chiang394c3372016-11-03 11:13:15 -0700613#if CONFIG_RD_DEBUG
614 xd->mi[0]->mbmi.mi_row = mi_row;
615 xd->mi[0]->mbmi.mi_col = mi_col;
616#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700617 for (y = 0; y < y_mis; ++y)
Jingning Han97d85482016-07-15 11:06:05 -0700618 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700619
Jingning Hanfaad0e12016-12-07 10:54:57 -0800620 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621 set_skip_context(xd, mi_row, mi_col);
622
623#if CONFIG_VAR_TX
624 xd->max_tx_size = max_txsize_lookup[bsize];
625#endif
626
627 // Distance of Mb to the various image edges. These are specified to 8th pel
628 // as they are always compared to values that are in 1/8th pel units
629 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
630
Yaowu Xuf883b422016-08-30 14:01:10 -0700631 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700632 return &xd->mi[0]->mbmi;
633}
634
635#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700636static MB_MODE_INFO *set_offsets_extend(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700637 MACROBLOCKD *const xd,
638 const TileInfo *const tile,
639 BLOCK_SIZE bsize_pred, int mi_row_pred,
640 int mi_col_pred, int mi_row_ori,
641 int mi_col_ori) {
642 // Used in supertx
643 // (mi_row_ori, mi_col_ori): location for mv
644 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
Jingning Han93531242016-12-20 11:54:36 -0800645 const int bw = mi_size_wide[bsize_pred];
646 const int bh = mi_size_high[bsize_pred];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700647 const int offset = mi_row_ori * cm->mi_stride + mi_col_ori;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700648 xd->mi = cm->mi_grid_visible + offset;
649 xd->mi[0] = cm->mi + offset;
650 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
651 cm->mi_cols);
652
653 xd->up_available = (mi_row_ori > tile->mi_row_start);
654 xd->left_available = (mi_col_ori > tile->mi_col_start);
655
Jingning Hanfaad0e12016-12-07 10:54:57 -0800656 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700657
658 return &xd->mi[0]->mbmi;
659}
660
Yaowu Xuf883b422016-08-30 14:01:10 -0700661static MB_MODE_INFO *set_mb_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
662 BLOCK_SIZE bsize, int mi_row, int mi_col,
663 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700664 const int offset = mi_row * cm->mi_stride + mi_col;
665 const TileInfo *const tile = &xd->tile;
666 int x, y;
667
668 xd->mi = cm->mi_grid_visible + offset;
669 xd->mi[0] = cm->mi + offset;
670 xd->mi[0]->mbmi.sb_type = bsize;
671 for (y = 0; y < y_mis; ++y)
672 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
673
674 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
675 return &xd->mi[0]->mbmi;
676}
677
Yaowu Xuf883b422016-08-30 14:01:10 -0700678static void set_offsets_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700679 const TileInfo *const tile, BLOCK_SIZE bsize,
680 int mi_row, int mi_col) {
Jingning Han93531242016-12-20 11:54:36 -0800681 const int bw = mi_size_wide[bsize];
682 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700683 const int offset = mi_row * cm->mi_stride + mi_col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700684
685 xd->mi = cm->mi_grid_visible + offset;
686 xd->mi[0] = cm->mi + offset;
687
Jingning Hanfaad0e12016-12-07 10:54:57 -0800688 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700689
690 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
691
Yaowu Xuf883b422016-08-30 14:01:10 -0700692 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700693}
694
Yaowu Xuf883b422016-08-30 14:01:10 -0700695static void set_param_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700696 BLOCK_SIZE bsize, int mi_row, int mi_col,
697 int txfm, int skip) {
Jingning Han93531242016-12-20 11:54:36 -0800698 const int bw = mi_size_wide[bsize];
699 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -0700700 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
701 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700702 const int offset = mi_row * cm->mi_stride + mi_col;
703 int x, y;
704
705 xd->mi = cm->mi_grid_visible + offset;
706 xd->mi[0] = cm->mi + offset;
707
708 for (y = 0; y < y_mis; ++y)
709 for (x = 0; x < x_mis; ++x) {
710 xd->mi[y * cm->mi_stride + x]->mbmi.skip = skip;
711 xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
712 }
713#if CONFIG_VAR_TX
714 xd->above_txfm_context = cm->above_txfm_context + mi_col;
715 xd->left_txfm_context =
716 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
Yaowu Xu52a17632016-11-17 15:48:21 -0800717 set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bw, bh, skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700718#endif
719}
720
Yaowu Xuf883b422016-08-30 14:01:10 -0700721static void set_ref(AV1_COMMON *const cm, MACROBLOCKD *const xd, int idx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700722 int mi_row, int mi_col) {
723 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
724 RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
725 xd->block_refs[idx] = ref_buffer;
Yaowu Xuf883b422016-08-30 14:01:10 -0700726 if (!av1_is_valid_scale(&ref_buffer->sf))
727 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700728 "Invalid scale factors");
Yaowu Xuf883b422016-08-30 14:01:10 -0700729 av1_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
730 &ref_buffer->sf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700731 xd->corrupted |= ref_buffer->buf->corrupted;
732}
733
734static void dec_predict_b_extend(
Yaowu Xuf883b422016-08-30 14:01:10 -0700735 AV1Decoder *const pbi, MACROBLOCKD *const xd, const TileInfo *const tile,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700736 int block, int mi_row_ori, int mi_col_ori, int mi_row_pred, int mi_col_pred,
737 int mi_row_top, int mi_col_top, uint8_t *dst_buf[3], int dst_stride[3],
738 BLOCK_SIZE bsize_top, BLOCK_SIZE bsize_pred, int b_sub8x8, int bextend) {
739 // Used in supertx
740 // (mi_row_ori, mi_col_ori): location for mv
741 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
742 // (mi_row_top, mi_col_top, bsize_top): region of the top partition size
743 // block: sub location of sub8x8 blocks
744 // b_sub8x8: 1: ori is sub8x8; 0: ori is not sub8x8
745 // bextend: 1: region to predict is an extension of ori; 0: not
746 int r = (mi_row_pred - mi_row_top) * MI_SIZE;
747 int c = (mi_col_pred - mi_col_top) * MI_SIZE;
Jingning Han93531242016-12-20 11:54:36 -0800748 const int mi_width_top = mi_size_wide[bsize_top];
749 const int mi_height_top = mi_size_high[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700750 MB_MODE_INFO *mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700751 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700752
753 if (mi_row_pred < mi_row_top || mi_col_pred < mi_col_top ||
754 mi_row_pred >= mi_row_top + mi_height_top ||
755 mi_col_pred >= mi_col_top + mi_width_top || mi_row_pred >= cm->mi_rows ||
756 mi_col_pred >= cm->mi_cols)
757 return;
758
759 mbmi = set_offsets_extend(cm, xd, tile, bsize_pred, mi_row_pred, mi_col_pred,
760 mi_row_ori, mi_col_ori);
761 set_ref(cm, xd, 0, mi_row_pred, mi_col_pred);
762 if (has_second_ref(&xd->mi[0]->mbmi))
763 set_ref(cm, xd, 1, mi_row_pred, mi_col_pred);
764
Jingning Han2511c662016-12-22 11:57:34 -0800765 if (!bextend) mbmi->tx_size = max_txsize_lookup[bsize_top];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700766
767 xd->plane[0].dst.stride = dst_stride[0];
768 xd->plane[1].dst.stride = dst_stride[1];
769 xd->plane[2].dst.stride = dst_stride[2];
770 xd->plane[0].dst.buf = dst_buf[0] +
771 (r >> xd->plane[0].subsampling_y) * dst_stride[0] +
772 (c >> xd->plane[0].subsampling_x);
773 xd->plane[1].dst.buf = dst_buf[1] +
774 (r >> xd->plane[1].subsampling_y) * dst_stride[1] +
775 (c >> xd->plane[1].subsampling_x);
776 xd->plane[2].dst.buf = dst_buf[2] +
777 (r >> xd->plane[2].subsampling_y) * dst_stride[2] +
778 (c >> xd->plane[2].subsampling_x);
779
780 if (!b_sub8x8)
Yaowu Xuf883b422016-08-30 14:01:10 -0700781 av1_build_inter_predictors_sb_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700782#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700783 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700784#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700785 mi_row_pred, mi_col_pred, bsize_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700786 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700787 av1_build_inter_predictors_sb_sub8x8_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700788#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700789 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700790#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700791 mi_row_pred, mi_col_pred,
792 bsize_pred, block);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700793}
794
Yaowu Xuf883b422016-08-30 14:01:10 -0700795static void dec_extend_dir(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700796 const TileInfo *const tile, int block,
797 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
798 int mi_col, int mi_row_top, int mi_col_top,
799 uint8_t *dst_buf[3], int dst_stride[3], int dir) {
800 // dir: 0-lower, 1-upper, 2-left, 3-right
801 // 4-lowerleft, 5-upperleft, 6-lowerright, 7-upperright
Jingning Han93531242016-12-20 11:54:36 -0800802 const int mi_width = mi_size_wide[bsize];
803 const int mi_height = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700804 int xss = xd->plane[1].subsampling_x;
805 int yss = xd->plane[1].subsampling_y;
Jingning Hanfeb517c2016-12-21 16:02:07 -0800806#if CONFIG_CB4X4
807 const int unify_bsize = 1;
808#else
809 const int unify_bsize = 0;
810#endif
811 int b_sub8x8 = (bsize < BLOCK_8X8) && !unify_bsize ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700812 BLOCK_SIZE extend_bsize;
Jingning Han24f24a52016-12-27 10:13:28 -0800813 int mi_row_pred, mi_col_pred;
814
815 int wide_unit, high_unit;
816 int i, j;
817 int ext_offset = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700818
819 if (dir == 0 || dir == 1) {
Jingning Han93531242016-12-20 11:54:36 -0800820 extend_bsize =
821 (mi_width == mi_size_wide[BLOCK_8X8] || bsize < BLOCK_8X8 || xss < yss)
822 ? BLOCK_8X8
823 : BLOCK_16X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800824#if CONFIG_CB4X4
825 if (bsize < BLOCK_8X8) {
826 extend_bsize = BLOCK_4X4;
827 ext_offset = mi_size_wide[BLOCK_8X8];
828 }
829#endif
830
831 wide_unit = mi_size_wide[extend_bsize];
832 high_unit = mi_size_high[extend_bsize];
833
834 mi_row_pred = mi_row + ((dir == 0) ? mi_height : -(mi_height + ext_offset));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700835 mi_col_pred = mi_col;
836
Jingning Han24f24a52016-12-27 10:13:28 -0800837 for (j = 0; j < mi_height + ext_offset; j += high_unit)
838 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
839 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
840 mi_row_pred + j, mi_col_pred + i, mi_row_top,
841 mi_col_top, dst_buf, dst_stride, top_bsize,
842 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700843 } else if (dir == 2 || dir == 3) {
Jingning Han5b7706a2016-12-21 09:55:10 -0800844 extend_bsize =
845 (mi_height == mi_size_high[BLOCK_8X8] || bsize < BLOCK_8X8 || yss < xss)
846 ? BLOCK_8X8
847 : BLOCK_8X16;
Jingning Han24f24a52016-12-27 10:13:28 -0800848#if CONFIG_CB4X4
849 if (bsize < BLOCK_8X8) {
850 extend_bsize = BLOCK_4X4;
851 ext_offset = mi_size_wide[BLOCK_8X8];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700852 }
Jingning Han24f24a52016-12-27 10:13:28 -0800853#endif
854
855 wide_unit = mi_size_wide[extend_bsize];
856 high_unit = mi_size_high[extend_bsize];
857
858 mi_row_pred = mi_row;
859 mi_col_pred = mi_col + ((dir == 3) ? mi_width : -(mi_width + ext_offset));
860
861 for (j = 0; j < mi_height + ext_offset; j += high_unit)
862 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
863 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
864 mi_row_pred + j, mi_col_pred + i, mi_row_top,
865 mi_col_top, dst_buf, dst_stride, top_bsize,
866 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700867 } else {
868 extend_bsize = BLOCK_8X8;
Jingning Han24f24a52016-12-27 10:13:28 -0800869#if CONFIG_CB4X4
870 if (bsize < BLOCK_8X8) {
871 extend_bsize = BLOCK_4X4;
872 ext_offset = mi_size_wide[BLOCK_8X8];
873 }
874#endif
875 wide_unit = mi_size_wide[extend_bsize];
876 high_unit = mi_size_high[extend_bsize];
877
Jingning Han5b7706a2016-12-21 09:55:10 -0800878 mi_row_pred = mi_row + ((dir == 4 || dir == 6) ? mi_height
Jingning Han24f24a52016-12-27 10:13:28 -0800879 : -(mi_height + ext_offset));
Jingning Han5b7706a2016-12-21 09:55:10 -0800880 mi_col_pred =
Jingning Han24f24a52016-12-27 10:13:28 -0800881 mi_col + ((dir == 6 || dir == 7) ? mi_width : -(mi_width + ext_offset));
882
883 for (j = 0; j < mi_height + ext_offset; j += high_unit)
884 for (i = 0; i < mi_width + ext_offset; i += wide_unit)
885 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
886 mi_row_pred + j, mi_col_pred + i, mi_row_top,
887 mi_col_top, dst_buf, dst_stride, top_bsize,
888 extend_bsize, b_sub8x8, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700889 }
890}
891
Yaowu Xuf883b422016-08-30 14:01:10 -0700892static void dec_extend_all(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700893 const TileInfo *const tile, int block,
894 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
895 int mi_col, int mi_row_top, int mi_col_top,
896 uint8_t *dst_buf[3], int dst_stride[3]) {
897 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
898 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
899 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
900 mi_row_top, mi_col_top, dst_buf, dst_stride, 1);
901 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
902 mi_row_top, mi_col_top, dst_buf, dst_stride, 2);
903 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
904 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
905 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
906 mi_row_top, mi_col_top, dst_buf, dst_stride, 4);
907 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
908 mi_row_top, mi_col_top, dst_buf, dst_stride, 5);
909 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
910 mi_row_top, mi_col_top, dst_buf, dst_stride, 6);
911 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
912 mi_row_top, mi_col_top, dst_buf, dst_stride, 7);
913}
914
Yaowu Xuf883b422016-08-30 14:01:10 -0700915static void dec_predict_sb_complex(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700916 const TileInfo *const tile, int mi_row,
917 int mi_col, int mi_row_top, int mi_col_top,
918 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize,
919 uint8_t *dst_buf[3], int dst_stride[3]) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700920 const AV1_COMMON *const cm = &pbi->common;
Jingning Han5b7706a2016-12-21 09:55:10 -0800921 const int hbs = mi_size_wide[bsize] / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700922 const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
923 const BLOCK_SIZE subsize = get_subsize(bsize, partition);
924#if CONFIG_EXT_PARTITION_TYPES
925 const BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
926#endif
927 int i;
928 const int mi_offset = mi_row * cm->mi_stride + mi_col;
929 uint8_t *dst_buf1[3], *dst_buf2[3], *dst_buf3[3];
Jingning Hanfeb517c2016-12-21 16:02:07 -0800930#if CONFIG_CB4X4
931 const int unify_bsize = 1;
932#else
933 const int unify_bsize = 0;
934#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700935
936 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
937 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
938 DECLARE_ALIGNED(16, uint8_t, tmp_buf3[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
939 int dst_stride1[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
940 int dst_stride2[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
941 int dst_stride3[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
942
Yaowu Xuf883b422016-08-30 14:01:10 -0700943#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700944 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
945 int len = sizeof(uint16_t);
946 dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
947 dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_TX_SQUARE * len);
948 dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_TX_SQUARE * len);
949 dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
950 dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_TX_SQUARE * len);
951 dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_TX_SQUARE * len);
952 dst_buf3[0] = CONVERT_TO_BYTEPTR(tmp_buf3);
953 dst_buf3[1] = CONVERT_TO_BYTEPTR(tmp_buf3 + MAX_TX_SQUARE * len);
954 dst_buf3[2] = CONVERT_TO_BYTEPTR(tmp_buf3 + 2 * MAX_TX_SQUARE * len);
955 } else {
956#endif
957 dst_buf1[0] = tmp_buf1;
958 dst_buf1[1] = tmp_buf1 + MAX_TX_SQUARE;
959 dst_buf1[2] = tmp_buf1 + 2 * MAX_TX_SQUARE;
960 dst_buf2[0] = tmp_buf2;
961 dst_buf2[1] = tmp_buf2 + MAX_TX_SQUARE;
962 dst_buf2[2] = tmp_buf2 + 2 * MAX_TX_SQUARE;
963 dst_buf3[0] = tmp_buf3;
964 dst_buf3[1] = tmp_buf3 + MAX_TX_SQUARE;
965 dst_buf3[2] = tmp_buf3 + 2 * MAX_TX_SQUARE;
Yaowu Xuf883b422016-08-30 14:01:10 -0700966#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700967 }
968#endif
969
970 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
971
972 xd->mi = cm->mi_grid_visible + mi_offset;
973 xd->mi[0] = cm->mi + mi_offset;
974
975 for (i = 0; i < MAX_MB_PLANE; i++) {
976 xd->plane[i].dst.buf = dst_buf[i];
977 xd->plane[i].dst.stride = dst_stride[i];
978 }
979
980 switch (partition) {
981 case PARTITION_NONE:
982 assert(bsize < top_bsize);
983 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
984 mi_row_top, mi_col_top, dst_buf, dst_stride,
985 top_bsize, bsize, 0, 0);
986 dec_extend_all(pbi, xd, tile, 0, bsize, top_bsize, mi_row, mi_col,
987 mi_row_top, mi_col_top, dst_buf, dst_stride);
988 break;
989 case PARTITION_HORZ:
Jingning Hanfeb517c2016-12-21 16:02:07 -0800990 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700991 // For sub8x8, predict in 8x8 unit
992 // First half
993 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
994 mi_row_top, mi_col_top, dst_buf, dst_stride,
995 top_bsize, BLOCK_8X8, 1, 0);
996 if (bsize < top_bsize)
997 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
998 mi_row_top, mi_col_top, dst_buf, dst_stride);
999
1000 // Second half
1001 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1002 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1003 top_bsize, BLOCK_8X8, 1, 1);
1004 if (bsize < top_bsize)
1005 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1006 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1007
1008 // weighted average to smooth the boundary
1009 xd->plane[0].dst.buf = dst_buf[0];
1010 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001011 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001012 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1013 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1014 0);
1015 } else {
1016 // First half
1017 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1018 mi_row_top, mi_col_top, dst_buf, dst_stride,
1019 top_bsize, subsize, 0, 0);
1020 if (bsize < top_bsize)
1021 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1022 mi_row_top, mi_col_top, dst_buf, dst_stride);
1023 else
1024 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1025 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1026
1027 if (mi_row + hbs < cm->mi_rows) {
1028 // Second half
1029 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col,
1030 mi_row + hbs, mi_col, mi_row_top, mi_col_top,
1031 dst_buf1, dst_stride1, top_bsize, subsize, 0, 0);
1032 if (bsize < top_bsize)
1033 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1034 mi_col, mi_row_top, mi_col_top, dst_buf1,
1035 dst_stride1);
1036 else
1037 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1038 mi_col, mi_row_top, mi_col_top, dst_buf1,
1039 dst_stride1, 1);
1040
1041 // weighted average to smooth the boundary
1042 for (i = 0; i < MAX_MB_PLANE; i++) {
1043 xd->plane[i].dst.buf = dst_buf[i];
1044 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001045 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001046 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1047 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1048 PARTITION_HORZ, i);
1049 }
1050 }
1051 }
1052 break;
1053 case PARTITION_VERT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001054 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001055 // First half
1056 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1057 mi_row_top, mi_col_top, dst_buf, dst_stride,
1058 top_bsize, BLOCK_8X8, 1, 0);
1059 if (bsize < top_bsize)
1060 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1061 mi_row_top, mi_col_top, dst_buf, dst_stride);
1062
1063 // Second half
1064 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1065 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1066 top_bsize, BLOCK_8X8, 1, 1);
1067 if (bsize < top_bsize)
1068 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1069 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1070
1071 // Smooth
1072 xd->plane[0].dst.buf = dst_buf[0];
1073 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001074 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001075 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1076 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1077 0);
1078 } else {
1079 // First half
1080 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1081 mi_row_top, mi_col_top, dst_buf, dst_stride,
1082 top_bsize, subsize, 0, 0);
1083 if (bsize < top_bsize)
1084 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1085 mi_row_top, mi_col_top, dst_buf, dst_stride);
1086 else
1087 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1088 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1089
1090 // Second half
1091 if (mi_col + hbs < cm->mi_cols) {
1092 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1093 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1094 dst_stride1, top_bsize, subsize, 0, 0);
1095 if (bsize < top_bsize)
1096 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1097 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1098 dst_stride1);
1099 else
1100 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1101 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1102 dst_stride1, 2);
1103
1104 // Smooth
1105 for (i = 0; i < MAX_MB_PLANE; i++) {
1106 xd->plane[i].dst.buf = dst_buf[i];
1107 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001108 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001109 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1110 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1111 PARTITION_VERT, i);
1112 }
1113 }
1114 }
1115 break;
1116 case PARTITION_SPLIT:
Jingning Hanfeb517c2016-12-21 16:02:07 -08001117 if (bsize == BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001118 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1119 mi_row_top, mi_col_top, dst_buf, dst_stride,
1120 top_bsize, BLOCK_8X8, 1, 0);
1121 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1122 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1123 top_bsize, BLOCK_8X8, 1, 1);
1124 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1125 mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1126 top_bsize, BLOCK_8X8, 1, 1);
1127 dec_predict_b_extend(pbi, xd, tile, 3, mi_row, mi_col, mi_row, mi_col,
1128 mi_row_top, mi_col_top, dst_buf3, dst_stride3,
1129 top_bsize, BLOCK_8X8, 1, 1);
1130 if (bsize < top_bsize) {
1131 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1132 mi_row_top, mi_col_top, dst_buf, dst_stride);
1133 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1134 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1135 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1136 mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1137 dec_extend_all(pbi, xd, tile, 3, subsize, top_bsize, mi_row, mi_col,
1138 mi_row_top, mi_col_top, dst_buf3, dst_stride3);
1139 }
1140 } else {
1141 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row_top,
1142 mi_col_top, subsize, top_bsize, dst_buf,
1143 dst_stride);
1144 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1145 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col + hbs,
1146 mi_row_top, mi_col_top, subsize, top_bsize,
1147 dst_buf1, dst_stride1);
1148 if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols)
1149 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col,
1150 mi_row_top, mi_col_top, subsize, top_bsize,
1151 dst_buf2, dst_stride2);
1152 if (mi_row + hbs < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1153 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col + hbs,
1154 mi_row_top, mi_col_top, subsize, top_bsize,
1155 dst_buf3, dst_stride3);
1156 }
1157 for (i = 0; i < MAX_MB_PLANE; i++) {
Jingning Han9e0976a2016-12-27 17:52:42 -08001158#if !CONFIG_CB4X4
Jingning Han24f24a52016-12-27 10:13:28 -08001159 if (bsize == BLOCK_8X8 && i != 0)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001160 continue; // Skip <4x4 chroma smoothing
Jingning Han9e0976a2016-12-27 17:52:42 -08001161#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001162 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001163 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001164 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1165 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1166 PARTITION_VERT, i);
1167 if (mi_row + hbs < cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001168 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001169 xd, dst_buf2[i], dst_stride2[i], dst_buf3[i], dst_stride3[i],
1170 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1171 PARTITION_VERT, i);
Yaowu Xuf883b422016-08-30 14:01:10 -07001172 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001173 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1174 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1175 PARTITION_HORZ, i);
1176 }
1177 } else if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001178 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001179 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1180 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1181 PARTITION_HORZ, i);
1182 }
1183 }
1184 break;
1185#if CONFIG_EXT_PARTITION_TYPES
1186 case PARTITION_HORZ_A:
1187 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1188 mi_row_top, mi_col_top, dst_buf, dst_stride,
1189 top_bsize, bsize2, 0, 0);
1190 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1191 mi_row_top, mi_col_top, dst_buf, dst_stride);
1192
1193 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1194 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1195 dst_stride1, top_bsize, bsize2, 0, 0);
1196 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1197 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1198
1199 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1200 mi_col, mi_row_top, mi_col_top, dst_buf2,
1201 dst_stride2, top_bsize, subsize, 0, 0);
1202 if (bsize < top_bsize)
1203 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1204 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1205 else
1206 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1207 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1208 1);
1209
1210 for (i = 0; i < MAX_MB_PLANE; i++) {
1211 xd->plane[i].dst.buf = dst_buf[i];
1212 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001213 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001214 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1215 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1216 i);
1217 }
1218 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001219 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001220 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1221 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1222 i);
1223 }
1224 break;
1225 case PARTITION_VERT_A:
1226
1227 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1228 mi_row_top, mi_col_top, dst_buf, dst_stride,
1229 top_bsize, bsize2, 0, 0);
1230 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1231 mi_row_top, mi_col_top, dst_buf, dst_stride);
1232
1233 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1234 mi_col, mi_row_top, mi_col_top, dst_buf1,
1235 dst_stride1, top_bsize, bsize2, 0, 0);
1236 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1237 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1238
1239 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1240 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1241 dst_stride2, top_bsize, subsize, 0, 0);
1242 if (bsize < top_bsize)
1243 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1244 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1245 dst_stride2);
1246 else
1247 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1248 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1249 dst_stride2, 2);
1250
1251 for (i = 0; i < MAX_MB_PLANE; i++) {
1252 xd->plane[i].dst.buf = dst_buf[i];
1253 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001254 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001255 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1256 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1257 i);
1258 }
1259 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001260 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001261 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1262 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1263 i);
1264 }
1265 break;
1266 case PARTITION_HORZ_B:
1267 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1268 mi_row_top, mi_col_top, dst_buf, dst_stride,
1269 top_bsize, subsize, 0, 0);
1270 if (bsize < top_bsize)
1271 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1272 mi_row_top, mi_col_top, dst_buf, dst_stride);
1273 else
1274 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1275 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1276
1277 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1278 mi_col, mi_row_top, mi_col_top, dst_buf1,
1279 dst_stride1, top_bsize, bsize2, 0, 0);
1280 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1281 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1282
1283 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1284 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1285 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1286 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1287 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1288 dst_stride2);
1289
1290 for (i = 0; i < MAX_MB_PLANE; i++) {
1291 xd->plane[i].dst.buf = dst_buf1[i];
1292 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001293 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001294 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1295 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1296 PARTITION_VERT, i);
1297 }
1298 for (i = 0; i < MAX_MB_PLANE; i++) {
1299 xd->plane[i].dst.buf = dst_buf[i];
1300 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001301 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001302 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1303 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1304 i);
1305 }
1306 break;
1307 case PARTITION_VERT_B:
1308 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1309 mi_row_top, mi_col_top, dst_buf, dst_stride,
1310 top_bsize, subsize, 0, 0);
1311 if (bsize < top_bsize)
1312 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1313 mi_row_top, mi_col_top, dst_buf, dst_stride);
1314 else
1315 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1316 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1317
1318 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1319 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1320 dst_stride1, top_bsize, bsize2, 0, 0);
1321 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1322 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1323
1324 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1325 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1326 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1327 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1328 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1329 dst_stride2);
1330
1331 for (i = 0; i < MAX_MB_PLANE; i++) {
1332 xd->plane[i].dst.buf = dst_buf1[i];
1333 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001334 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001335 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1336 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1337 PARTITION_HORZ, i);
1338 }
1339 for (i = 0; i < MAX_MB_PLANE; i++) {
1340 xd->plane[i].dst.buf = dst_buf[i];
1341 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001342 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001343 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1344 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1345 i);
1346 }
1347 break;
1348#endif // CONFIG_EXT_PARTITION_TYPES
1349 default: assert(0);
1350 }
1351}
1352
Yaowu Xuf883b422016-08-30 14:01:10 -07001353static void set_segment_id_supertx(const AV1_COMMON *const cm, const int mi_row,
1354 const int mi_col, const BLOCK_SIZE bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001355 const struct segmentation *seg = &cm->seg;
Jingning Han5b7706a2016-12-21 09:55:10 -08001356 const int miw = AOMMIN(mi_size_wide[bsize], cm->mi_cols - mi_col);
1357 const int mih = AOMMIN(mi_size_high[bsize], cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001358 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1359 MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
1360 int r, c;
1361 int seg_id_supertx = MAX_SEGMENTS;
1362
1363 if (!seg->enabled) {
1364 seg_id_supertx = 0;
1365 } else {
1366 // Find the minimum segment_id
1367 for (r = 0; r < mih; r++)
1368 for (c = 0; c < miw; c++)
1369 seg_id_supertx =
Yaowu Xuf883b422016-08-30 14:01:10 -07001370 AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001371 assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);
1372 }
1373
1374 // Assign the the segment_id back to segment_id_supertx
1375 for (r = 0; r < mih; r++)
1376 for (c = 0; c < miw; c++)
1377 mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
1378}
1379#endif // CONFIG_SUPERTX
1380
Yaowu Xuf883b422016-08-30 14:01:10 -07001381static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001382#if CONFIG_SUPERTX
1383 int supertx_enabled,
1384#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001385 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001386#if CONFIG_EXT_PARTITION_TYPES
1387 PARTITION_TYPE partition,
1388#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001389 BLOCK_SIZE bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001390 AV1_COMMON *const cm = &pbi->common;
Jingning Han85dc03f2016-12-06 16:03:10 -08001391 const int bw = mi_size_wide[bsize];
1392 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -07001393 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1394 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Nathan E. Eggeebbd4792016-10-05 19:30:15 -04001395 MB_MODE_INFO *mbmi;
1396
Michael Bebenita6048d052016-08-25 14:40:54 -07001397#if CONFIG_ACCOUNTING
1398 aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
1399#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001400#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001401 if (supertx_enabled) {
1402 mbmi = set_mb_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
1403 } else {
Jingning Hanfaad0e12016-12-07 10:54:57 -08001404 mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001405 }
1406#if CONFIG_EXT_PARTITION_TYPES
1407 xd->mi[0]->mbmi.partition = partition;
1408#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001409 av1_read_mode_info(pbi, xd, supertx_enabled, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001410#else
Jingning Hanfaad0e12016-12-07 10:54:57 -08001411 mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001412#if CONFIG_EXT_PARTITION_TYPES
1413 xd->mi[0]->mbmi.partition = partition;
1414#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001415 av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001416#endif // CONFIG_SUPERTX
1417
1418 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1419 const BLOCK_SIZE uv_subsize =
1420 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1421 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -07001422 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001423 "Invalid block size.");
1424 }
1425
1426#if CONFIG_SUPERTX
1427 mbmi->segment_id_supertx = MAX_SEGMENTS;
1428
1429 if (supertx_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001430 xd->corrupted |= aom_reader_has_error(r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001431 return;
1432 }
1433#endif // CONFIG_SUPERTX
1434
Arild Fuldseth07441162016-08-15 15:07:52 +02001435#if CONFIG_DELTA_Q
1436 if (cm->delta_q_present_flag) {
1437 int i;
1438 for (i = 0; i < MAX_SEGMENTS; i++) {
1439 xd->plane[0].seg_dequant[i][0] =
1440 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
1441 xd->plane[0].seg_dequant[i][1] =
1442 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
1443 xd->plane[1].seg_dequant[i][0] =
1444 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1445 xd->plane[1].seg_dequant[i][1] =
1446 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1447 xd->plane[2].seg_dequant[i][0] =
1448 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1449 xd->plane[2].seg_dequant[i][1] =
1450 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1451 }
1452 }
1453#endif
1454
Jingning Han41bb3392016-12-14 10:46:48 -08001455#if CONFIG_CB4X4
1456 if (mbmi->skip) reset_skip_context(xd, bsize);
1457#else
Jingning Hand39cc722016-12-02 14:03:26 -08001458 if (mbmi->skip) reset_skip_context(xd, AOMMAX(BLOCK_8X8, bsize));
Jingning Han41bb3392016-12-14 10:46:48 -08001459#endif
Jingning Hand39cc722016-12-02 14:03:26 -08001460
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001461#if CONFIG_COEF_INTERLEAVE
1462 {
1463 const struct macroblockd_plane *const pd_y = &xd->plane[0];
1464 const struct macroblockd_plane *const pd_c = &xd->plane[1];
1465 const TX_SIZE tx_log2_y = mbmi->tx_size;
1466 const TX_SIZE tx_log2_c = get_uv_tx_size(mbmi, pd_c);
1467 const int tx_sz_y = (1 << tx_log2_y);
1468 const int tx_sz_c = (1 << tx_log2_c);
1469 const int num_4x4_w_y = pd_y->n4_w;
1470 const int num_4x4_h_y = pd_y->n4_h;
1471 const int num_4x4_w_c = pd_c->n4_w;
1472 const int num_4x4_h_c = pd_c->n4_h;
1473 const int max_4x4_w_y = get_max_4x4_size(num_4x4_w_y, xd->mb_to_right_edge,
1474 pd_y->subsampling_x);
1475 const int max_4x4_h_y = get_max_4x4_size(num_4x4_h_y, xd->mb_to_bottom_edge,
1476 pd_y->subsampling_y);
1477 const int max_4x4_w_c = get_max_4x4_size(num_4x4_w_c, xd->mb_to_right_edge,
1478 pd_c->subsampling_x);
1479 const int max_4x4_h_c = get_max_4x4_size(num_4x4_h_c, xd->mb_to_bottom_edge,
1480 pd_c->subsampling_y);
1481
1482 // The max_4x4_w/h may be smaller than tx_sz under some corner cases,
1483 // i.e. when the SB is splitted by tile boundaries.
1484 const int tu_num_w_y = (max_4x4_w_y + tx_sz_y - 1) / tx_sz_y;
1485 const int tu_num_h_y = (max_4x4_h_y + tx_sz_y - 1) / tx_sz_y;
1486 const int tu_num_w_c = (max_4x4_w_c + tx_sz_c - 1) / tx_sz_c;
1487 const int tu_num_h_c = (max_4x4_h_c + tx_sz_c - 1) / tx_sz_c;
1488 const int tu_num_y = tu_num_w_y * tu_num_h_y;
1489 const int tu_num_c = tu_num_w_c * tu_num_h_c;
1490
1491 if (!is_inter_block(mbmi)) {
1492 int tu_idx_c = 0;
1493 int row_y, col_y, row_c, col_c;
1494 int plane;
1495
1496#if CONFIG_PALETTE
1497 for (plane = 0; plane <= 1; ++plane) {
1498 if (mbmi->palette_mode_info.palette_size[plane])
1499 av1_decode_palette_tokens(xd, plane, r);
1500 }
1501#endif
1502
1503 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1504 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1505 // luma
1506 predict_and_reconstruct_intra_block(
1507 cm, xd, r, mbmi, 0, row_y * tx_sz_y, col_y * tx_sz_y, tx_log2_y);
1508 // chroma
1509 if (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,
1513 col_c, tx_log2_c);
1514 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c,
1515 col_c, tx_log2_c);
1516 tu_idx_c++;
1517 }
1518 }
1519 }
1520
1521 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1522 while (tu_idx_c < tu_num_c) {
1523 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1524 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1525 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c, col_c,
1526 tx_log2_c);
1527 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c, col_c,
1528 tx_log2_c);
1529 tu_idx_c++;
1530 }
1531 } else {
1532 // Prediction
David Barkerac37fa32016-12-02 12:30:21 +00001533 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001534 AOMMAX(bsize, BLOCK_8X8));
1535
1536 // Reconstruction
1537 if (!mbmi->skip) {
1538 int eobtotal = 0;
1539 int tu_idx_c = 0;
1540 int row_y, col_y, row_c, col_c;
1541
1542 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1543 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1544 // luma
1545 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 0,
1546 row_y * tx_sz_y,
1547 col_y * tx_sz_y, tx_log2_y);
1548 // chroma
1549 if (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,
1553 1, row_c, col_c, tx_log2_c);
1554 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1555 2, row_c, col_c, tx_log2_c);
1556 tu_idx_c++;
1557 }
1558 }
1559 }
1560
1561 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1562 while (tu_idx_c < tu_num_c) {
1563 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1564 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1565 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 1,
1566 row_c, col_c, tx_log2_c);
1567 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 2,
1568 row_c, col_c, tx_log2_c);
1569 tu_idx_c++;
1570 }
1571
1572 if (bsize >= BLOCK_8X8 && eobtotal == 0)
1573#if CONFIG_MISC_FIXES
1574 mbmi->has_no_coeffs = 1;
1575#else
1576 mbmi->skip = 1;
1577#endif
1578 }
1579 }
1580 }
1581#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001582 if (!is_inter_block(mbmi)) {
1583 int plane;
Urvang Joshib100db72016-10-12 16:28:56 -07001584#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001585 for (plane = 0; plane <= 1; ++plane) {
1586 if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xuf883b422016-08-30 14:01:10 -07001587 av1_decode_palette_tokens(xd, plane, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001588 }
Urvang Joshib100db72016-10-12 16:28:56 -07001589#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001590 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1591 const struct macroblockd_plane *const pd = &xd->plane[plane];
Debargha Mukherjee2f123402016-08-30 17:43:38 -07001592 const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han2d64f122016-10-21 12:44:29 -07001593 const int stepr = tx_size_high_unit[tx_size];
1594 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han41bb3392016-12-14 10:46:48 -08001595#if CONFIG_CB4X4
1596 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1597#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001598 const BLOCK_SIZE plane_bsize =
1599 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001600#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001601 int row, col;
Jingning Hanbafee8d2016-12-02 10:25:03 -08001602 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1603 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001604
1605 for (row = 0; row < max_blocks_high; row += stepr)
1606 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001607 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane, row, col,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001608 tx_size);
1609 }
1610 } else {
Yue Chen69f18e12016-09-08 14:48:15 -07001611// Prediction
1612#if CONFIG_WARPED_MOTION
1613 if (mbmi->motion_mode == WARPED_CAUSAL) {
1614 int i;
1615#if CONFIG_AOM_HIGHBITDEPTH
1616 int use_hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH;
1617#endif // CONFIG_AOM_HIGHBITDEPTH
1618
1619 for (i = 0; i < 3; ++i) {
1620 const struct macroblockd_plane *pd = &xd->plane[i];
1621
1622 av1_warp_plane(&mbmi->wm_params[0],
1623#if CONFIG_AOM_HIGHBITDEPTH
1624 xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH, xd->bd,
1625#endif // CONFIG_AOM_HIGHBITDEPTH
1626 pd->pre[0].buf0, pd->pre[0].width, pd->pre[0].height,
1627 pd->pre[0].stride, pd->dst.buf,
1628 ((mi_col * MI_SIZE) >> pd->subsampling_x),
1629 ((mi_row * MI_SIZE) >> pd->subsampling_y),
Jingning Hanff6ee6a2016-12-07 09:55:21 -08001630 xd->n8_w * (MI_SIZE >> pd->subsampling_x),
1631 xd->n8_h * (MI_SIZE >> pd->subsampling_y),
1632 pd->dst.stride, pd->subsampling_x, pd->subsampling_y, 16,
1633 16, 0);
Yue Chen69f18e12016-09-08 14:48:15 -07001634 }
1635 } else {
1636#endif // CONFIG_WARPED_MOTION
Jingning Han41bb3392016-12-14 10:46:48 -08001637#if CONFIG_CB4X4
1638 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL, bsize);
1639#else
1640 av1_build_inter_predictors_sb(xd, mi_row, mi_col, NULL,
1641 AOMMAX(bsize, BLOCK_8X8));
1642#endif
Yue Chen69f18e12016-09-08 14:48:15 -07001643#if CONFIG_WARPED_MOTION
1644 }
1645#endif // CONFIG_WARPED_MOTION
Yue Chencb60b182016-10-13 15:18:22 -07001646#if CONFIG_MOTION_VAR
1647 if (mbmi->motion_mode == OBMC_CAUSAL) {
Yue Chen894fcce2016-10-21 16:50:52 -07001648 av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001649 }
Yue Chencb60b182016-10-13 15:18:22 -07001650#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001651
1652 // Reconstruction
1653 if (!mbmi->skip) {
1654 int eobtotal = 0;
1655 int plane;
1656
1657 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1658 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han41bb3392016-12-14 10:46:48 -08001659#if CONFIG_CB4X4
1660 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
1661#else
Jingning Hanbafee8d2016-12-02 10:25:03 -08001662 const BLOCK_SIZE plane_bsize =
1663 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
Jingning Han41bb3392016-12-14 10:46:48 -08001664#endif
Jingning Hanbafee8d2016-12-02 10:25:03 -08001665 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1666 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001667 int row, col;
1668#if CONFIG_VAR_TX
Jingning Han70e5f3f2016-11-09 17:03:07 -08001669 const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07001670 const int bh_var_tx = tx_size_high_unit[max_tx_size];
1671 const int bw_var_tx = tx_size_wide_unit[max_tx_size];
Jingning Hanbafee8d2016-12-02 10:25:03 -08001672 for (row = 0; row < max_blocks_high; row += bh_var_tx)
1673 for (col = 0; col < max_blocks_wide; col += bw_var_tx)
Jingning Hanfe45b212016-11-22 10:30:23 -08001674 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, row, col,
1675 max_tx_size, &eobtotal);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001676#else
1677 const TX_SIZE tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -07001678 plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han2d64f122016-10-21 12:44:29 -07001679 const int stepr = tx_size_high_unit[tx_size];
1680 const int stepc = tx_size_wide_unit[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001681 for (row = 0; row < max_blocks_high; row += stepr)
1682 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001683 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1684 plane, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001685#endif
1686 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001687 }
1688 }
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001689#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001690
Yaowu Xuf883b422016-08-30 14:01:10 -07001691 xd->corrupted |= aom_reader_has_error(r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001692}
1693
Yaowu Xuf883b422016-08-30 14:01:10 -07001694static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
1695 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001696 int has_rows, int has_cols,
Jingning Han1beb0102016-12-07 11:08:30 -08001697 BLOCK_SIZE bsize) {
1698 const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
Yaowu Xuf883b422016-08-30 14:01:10 -07001699 const aom_prob *const probs = cm->fc->partition_prob[ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001700 FRAME_COUNTS *counts = xd->counts;
1701 PARTITION_TYPE p;
1702
1703 if (has_rows && has_cols)
1704#if CONFIG_EXT_PARTITION_TYPES
1705 if (bsize <= BLOCK_8X8)
Michael Bebenita6048d052016-08-25 14:40:54 -07001706 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001707 else
Michael Bebenita6048d052016-08-25 14:40:54 -07001708 p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs,
1709 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001710#else
Nathan E. Egge9d9eb6c2016-11-16 09:44:26 -05001711#if CONFIG_EC_MULTISYMBOL
Yaowu Xuf2581a32016-10-20 13:05:47 -07001712 p = (PARTITION_TYPE)aom_read_symbol(r, cm->fc->partition_cdf[ctx],
1713 PARTITION_TYPES, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001714#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001715 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001716#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001717#endif // CONFIG_EXT_PARTITION_TYPES
1718 else if (!has_rows && has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001719 p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001720 else if (has_rows && !has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001721 p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001722 else
1723 p = PARTITION_SPLIT;
1724
1725 if (counts) ++counts->partition[ctx][p];
1726
1727 return p;
1728}
1729
1730#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001731static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
1732 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001733 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
1734 return 1;
1735 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001736 const int ctx = av1_get_skip_context(xd);
Michael Bebenita6048d052016-08-25 14:40:54 -07001737 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001738 FRAME_COUNTS *counts = xd->counts;
1739 if (counts) ++counts->skip[ctx][skip];
1740 return skip;
1741 }
1742}
1743#endif // CONFIG_SUPERTX
Yaowu Xud71be782016-10-14 08:47:03 -07001744#if CONFIG_CLPF
1745static int clpf_all_skip(const AV1_COMMON *cm, int mi_col, int mi_row,
1746 int size) {
1747 int r, c;
1748 int skip = 1;
1749 const int maxc = AOMMIN(size, cm->mi_cols - mi_col);
1750 const int maxr = AOMMIN(size, cm->mi_rows - mi_row);
1751 for (r = 0; r < maxr && skip; r++) {
1752 for (c = 0; c < maxc && skip; c++) {
1753 skip &= !!cm->mi_grid_visible[(mi_row + r) * cm->mi_stride + mi_col + c]
1754 ->mbmi.skip;
1755 }
1756 }
1757 return skip;
1758}
1759#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001760
1761// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -07001762static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001763#if CONFIG_SUPERTX
1764 int supertx_enabled,
1765#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001766 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001767 BLOCK_SIZE bsize, int n4x4_l2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001768 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001769 const int n8x8_l2 = n4x4_l2 - 1;
Jingning Hanff17e162016-12-07 17:58:18 -08001770 const int num_8x8_wh = mi_size_wide[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001771 const int hbs = num_8x8_wh >> 1;
Jingning Han41bb3392016-12-14 10:46:48 -08001772#if CONFIG_CB4X4
1773 const int unify_bsize = 1;
1774#else
1775 const int unify_bsize = 0;
1776#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001777 PARTITION_TYPE partition;
1778 BLOCK_SIZE subsize;
1779#if CONFIG_EXT_PARTITION_TYPES
1780 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1781#endif
1782 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1783 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1784#if CONFIG_SUPERTX
1785 const int read_token = !supertx_enabled;
1786 int skip = 0;
Jingning Han2511c662016-12-22 11:57:34 -08001787 TX_SIZE supertx_size = max_txsize_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001788 const TileInfo *const tile = &xd->tile;
1789 int txfm = DCT_DCT;
1790#endif // CONFIG_SUPERTX
1791
1792 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1793
Jingning Han6dbacf22016-12-14 17:28:35 -08001794 partition = (n4x4_l2 == 0) ? PARTITION_NONE
1795 : read_partition(cm, xd, mi_row, mi_col, r,
1796 has_rows, has_cols, bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001797 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
Yushin Cho77bba8d2016-11-04 16:36:56 -07001798
1799#if CONFIG_PVQ
1800 assert(partition < PARTITION_TYPES);
1801 assert(subsize < BLOCK_SIZES);
1802#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001803#if CONFIG_SUPERTX
1804 if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
1805 bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
1806 const int supertx_context = partition_supertx_context_lookup[partition];
Michael Bebenita6048d052016-08-25 14:40:54 -07001807 supertx_enabled = aom_read(
1808 r, cm->fc->supertx_prob[supertx_context][supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001809 if (xd->counts)
1810 xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
1811#if CONFIG_VAR_TX
1812 if (supertx_enabled) xd->supertx_size = supertx_size;
1813#endif
1814 }
1815#endif // CONFIG_SUPERTX
Jingning Han41bb3392016-12-14 10:46:48 -08001816 if (!hbs && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001817 // calculate bmode block dimensions (log 2)
1818 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1819 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
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 } else {
1830 switch (partition) {
1831 case PARTITION_NONE:
1832 decode_block(pbi, xd,
1833#if CONFIG_SUPERTX
1834 supertx_enabled,
1835#endif // CONFIG_SUPERTX
1836 mi_row, mi_col, r,
1837#if CONFIG_EXT_PARTITION_TYPES
1838 partition,
1839#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001840 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001841 break;
1842 case PARTITION_HORZ:
1843 decode_block(pbi, xd,
1844#if CONFIG_SUPERTX
1845 supertx_enabled,
1846#endif // CONFIG_SUPERTX
1847 mi_row, mi_col, r,
1848#if CONFIG_EXT_PARTITION_TYPES
1849 partition,
1850#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001851 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001852 if (has_rows)
1853 decode_block(pbi, xd,
1854#if CONFIG_SUPERTX
1855 supertx_enabled,
1856#endif // CONFIG_SUPERTX
1857 mi_row + hbs, mi_col, r,
1858#if CONFIG_EXT_PARTITION_TYPES
1859 partition,
1860#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001861 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001862 break;
1863 case PARTITION_VERT:
1864 decode_block(pbi, xd,
1865#if CONFIG_SUPERTX
1866 supertx_enabled,
1867#endif // CONFIG_SUPERTX
1868 mi_row, mi_col, r,
1869#if CONFIG_EXT_PARTITION_TYPES
1870 partition,
1871#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001872 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001873 if (has_cols)
1874 decode_block(pbi, xd,
1875#if CONFIG_SUPERTX
1876 supertx_enabled,
1877#endif // CONFIG_SUPERTX
1878 mi_row, mi_col + hbs, r,
1879#if CONFIG_EXT_PARTITION_TYPES
1880 partition,
1881#endif // CONFIG_EXT_PARTITION_TYPES
Jingning Hanfaad0e12016-12-07 10:54:57 -08001882 subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001883 break;
1884 case PARTITION_SPLIT:
1885 decode_partition(pbi, xd,
1886#if CONFIG_SUPERTX
1887 supertx_enabled,
1888#endif // CONFIG_SUPERTX
1889 mi_row, mi_col, r, subsize, n8x8_l2);
1890 decode_partition(pbi, xd,
1891#if CONFIG_SUPERTX
1892 supertx_enabled,
1893#endif // CONFIG_SUPERTX
1894 mi_row, mi_col + hbs, r, subsize, n8x8_l2);
1895 decode_partition(pbi, xd,
1896#if CONFIG_SUPERTX
1897 supertx_enabled,
1898#endif // CONFIG_SUPERTX
1899 mi_row + hbs, mi_col, r, subsize, n8x8_l2);
1900 decode_partition(pbi, xd,
1901#if CONFIG_SUPERTX
1902 supertx_enabled,
1903#endif // CONFIG_SUPERTX
1904 mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
1905 break;
1906#if CONFIG_EXT_PARTITION_TYPES
1907 case PARTITION_HORZ_A:
1908 decode_block(pbi, xd,
1909#if CONFIG_SUPERTX
1910 supertx_enabled,
1911#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001912 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001913 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 + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001918 decode_block(pbi, xd,
1919#if CONFIG_SUPERTX
1920 supertx_enabled,
1921#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001922 mi_row + hbs, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001923 break;
1924 case PARTITION_HORZ_B:
1925 decode_block(pbi, xd,
1926#if CONFIG_SUPERTX
1927 supertx_enabled,
1928#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001929 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001930 decode_block(pbi, xd,
1931#if CONFIG_SUPERTX
1932 supertx_enabled,
1933#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001934 mi_row + hbs, 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 + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001940 break;
1941 case PARTITION_VERT_A:
1942 decode_block(pbi, xd,
1943#if CONFIG_SUPERTX
1944 supertx_enabled,
1945#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001946 mi_row, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001947 decode_block(pbi, xd,
1948#if CONFIG_SUPERTX
1949 supertx_enabled,
1950#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001951 mi_row + hbs, mi_col, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001952 decode_block(pbi, xd,
1953#if CONFIG_SUPERTX
1954 supertx_enabled,
1955#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001956 mi_row, mi_col + hbs, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001957 break;
1958 case PARTITION_VERT_B:
1959 decode_block(pbi, xd,
1960#if CONFIG_SUPERTX
1961 supertx_enabled,
1962#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001963 mi_row, mi_col, r, partition, subsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001964 decode_block(pbi, xd,
1965#if CONFIG_SUPERTX
1966 supertx_enabled,
1967#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001968 mi_row, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001969 decode_block(pbi, xd,
1970#if CONFIG_SUPERTX
1971 supertx_enabled,
1972#endif
Jingning Hanfaad0e12016-12-07 10:54:57 -08001973 mi_row + hbs, mi_col + hbs, r, partition, bsize2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001974 break;
1975#endif
1976 default: assert(0 && "Invalid partition type");
1977 }
1978 }
1979
1980#if CONFIG_SUPERTX
1981 if (supertx_enabled && read_token) {
1982 uint8_t *dst_buf[3];
1983 int dst_stride[3], i;
1984 int offset = mi_row * cm->mi_stride + mi_col;
1985
1986 set_segment_id_supertx(cm, mi_row, mi_col, bsize);
1987
1988 xd->mi = cm->mi_grid_visible + offset;
1989 xd->mi[0] = cm->mi + offset;
Jingning Han5b7706a2016-12-21 09:55:10 -08001990 set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
1991 mi_size_wide[bsize], cm->mi_rows, cm->mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001992 set_skip_context(xd, mi_row, mi_col);
1993 skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
1994 if (skip) {
1995 reset_skip_context(xd, bsize);
1996 } else {
1997#if CONFIG_EXT_TX
1998 if (get_ext_tx_types(supertx_size, bsize, 1) > 1) {
1999 int eset = get_ext_tx_set(supertx_size, bsize, 1);
2000 if (eset > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002001 txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
Michael Bebenita6048d052016-08-25 14:40:54 -07002002 cm->fc->inter_ext_tx_prob[eset][supertx_size],
2003 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002004 if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
2005 }
2006 }
2007#else
2008 if (supertx_size < TX_32X32) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002009 txfm = aom_read_tree(r, av1_ext_tx_tree,
Jingning Han8f6eb182016-10-19 13:48:57 -07002010 cm->fc->inter_ext_tx_prob[supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002011 if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
2012 }
2013#endif // CONFIG_EXT_TX
2014 }
2015
Yaowu Xuf883b422016-08-30 14:01:10 -07002016 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002017 for (i = 0; i < MAX_MB_PLANE; i++) {
2018 dst_buf[i] = xd->plane[i].dst.buf;
2019 dst_stride[i] = xd->plane[i].dst.stride;
2020 }
2021 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
2022 bsize, dst_buf, dst_stride);
2023
2024 if (!skip) {
2025 int eobtotal = 0;
2026 MB_MODE_INFO *mbmi;
2027 set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
2028 mbmi = &xd->mi[0]->mbmi;
2029 mbmi->tx_type = txfm;
2030 assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
2031 for (i = 0; i < MAX_MB_PLANE; ++i) {
2032 const struct macroblockd_plane *const pd = &xd->plane[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002033 int row, col;
Debargha Mukherjee2f123402016-08-30 17:43:38 -07002034 const TX_SIZE tx_size = i ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han5b7706a2016-12-21 09:55:10 -08002035 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Jingning Han32b20282016-10-28 15:42:44 -07002036 const int stepr = tx_size_high_unit[tx_size];
2037 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han5b7706a2016-12-21 09:55:10 -08002038 const int max_blocks_wide = max_block_wide(xd, plane_bsize, i);
2039 const int max_blocks_high = max_block_high(xd, plane_bsize, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002040
2041 for (row = 0; row < max_blocks_high; row += stepr)
2042 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07002043 eobtotal += reconstruct_inter_block(
2044 cm, xd, r, mbmi->segment_id_supertx, i, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002045 }
Jingning Han24f24a52016-12-27 10:13:28 -08002046 if ((unify_bsize || !(subsize < BLOCK_8X8)) && eobtotal == 0) skip = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002047 }
2048 set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
2049 }
2050#endif // CONFIG_SUPERTX
2051
2052#if CONFIG_EXT_PARTITION_TYPES
2053 if (bsize >= BLOCK_8X8) {
2054 switch (partition) {
2055 case PARTITION_SPLIT:
2056 if (bsize > BLOCK_8X8) break;
2057 case PARTITION_NONE:
2058 case PARTITION_HORZ:
2059 case PARTITION_VERT:
2060 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
2061 break;
2062 case PARTITION_HORZ_A:
2063 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2064 update_partition_context(xd, mi_row + hbs, mi_col, subsize, subsize);
2065 break;
2066 case PARTITION_HORZ_B:
2067 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2068 update_partition_context(xd, mi_row + hbs, mi_col, bsize2, subsize);
2069 break;
2070 case PARTITION_VERT_A:
2071 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2072 update_partition_context(xd, mi_row, mi_col + hbs, subsize, subsize);
2073 break;
2074 case PARTITION_VERT_B:
2075 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2076 update_partition_context(xd, mi_row, mi_col + hbs, bsize2, subsize);
2077 break;
2078 default: assert(0 && "Invalid partition type");
2079 }
2080 }
2081#else
2082 // update partition context
2083 if (bsize >= BLOCK_8X8 &&
2084 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
Jingning Han1beb0102016-12-07 11:08:30 -08002085 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
David Barkerf8935c92016-10-26 14:54:06 +01002086#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xud71be782016-10-14 08:47:03 -07002087
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002088#if CONFIG_DERING
David Barker9739f362016-11-10 09:29:32 +00002089#if CONFIG_EXT_PARTITION
2090 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128) {
2091 if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
2092 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2093 aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
2094 } else {
2095 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2096 0;
2097 }
2098 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64) {
2099#else
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002100 if (bsize == BLOCK_64X64) {
David Barker9739f362016-11-10 09:29:32 +00002101#endif
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002102 if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
2103 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2104 aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
2105 } else {
2106 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2107 0;
2108 }
2109 }
2110#endif
2111
Yaowu Xud71be782016-10-14 08:47:03 -07002112#if CONFIG_CLPF
David Barker9739f362016-11-10 09:29:32 +00002113#if CONFIG_EXT_PARTITION
2114 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128 &&
2115 cm->clpf_strength_y && cm->clpf_size != CLPF_NOSIZE) {
2116 const int tl = mi_row * MI_SIZE / MIN_FB_SIZE * cm->clpf_stride +
2117 mi_col * MI_SIZE / MIN_FB_SIZE;
2118 if (cm->clpf_size == CLPF_128X128) {
2119 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
2120 } else if (cm->clpf_size == CLPF_64X64) {
2121 const int tr = tl + 2;
2122 const int bl = tl + 2 * cm->clpf_stride;
2123 const int br = tr + 2 * cm->clpf_stride;
2124 const int size = 64 / MI_SIZE;
2125
2126 // Up to four bits per SB
2127 if (!clpf_all_skip(cm, mi_col, mi_row, size))
2128 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
2129
2130 if (mi_col + size < cm->mi_cols &&
2131 !clpf_all_skip(cm, mi_col + size, mi_row, size))
2132 cm->clpf_blocks[tr] = aom_read_literal(r, 1, ACCT_STR);
2133
2134 if (mi_row + size < cm->mi_rows &&
2135 !clpf_all_skip(cm, mi_col, mi_row + size, size))
2136 cm->clpf_blocks[bl] = aom_read_literal(r, 1, ACCT_STR);
2137
2138 if (mi_col + size < cm->mi_cols && mi_row + size < cm->mi_rows &&
2139 !clpf_all_skip(cm, mi_col + size, mi_row + size, size))
2140 cm->clpf_blocks[br] = aom_read_literal(r, 1, ACCT_STR);
2141 } else if (cm->clpf_size == CLPF_32X32) {
2142 int i, j;
2143 const int size = 32 / MI_SIZE;
2144 for (i = 0; i < 4; ++i)
2145 for (j = 0; j < 4; ++j) {
2146 const int index = tl + i * cm->clpf_stride + j;
2147 if (mi_row + i * size < cm->mi_rows &&
2148 mi_col + j * size < cm->mi_cols &&
2149 !clpf_all_skip(cm, mi_col + j * size, mi_row + i * size, size))
2150 cm->clpf_blocks[index] = aom_read_literal(r, 1, ACCT_STR);
2151 }
2152 }
2153 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64 &&
2154#else
2155 if (bsize == BLOCK_64X64 &&
2156#endif // CONFIG_EXT_PARTITION
2157 cm->clpf_strength_y && cm->clpf_size != CLPF_NOSIZE) {
Yaowu Xud71be782016-10-14 08:47:03 -07002158 const int tl = mi_row * MI_SIZE / MIN_FB_SIZE * cm->clpf_stride +
2159 mi_col * MI_SIZE / MIN_FB_SIZE;
2160
2161 if (!((mi_row * MI_SIZE) & 127) && !((mi_col * MI_SIZE) & 127) &&
2162 cm->clpf_size == CLPF_128X128) {
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 } else if (cm->clpf_size == CLPF_64X64 &&
2165 !clpf_all_skip(cm, mi_col, mi_row, 64 / MI_SIZE)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002166 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002167 } else if (cm->clpf_size == CLPF_32X32) {
2168 const int tr = tl + 1;
2169 const int bl = tl + cm->clpf_stride;
2170 const int br = tr + cm->clpf_stride;
2171 const int size = 32 / MI_SIZE;
2172
2173 // Up to four bits per SB
2174 if (!clpf_all_skip(cm, mi_col, mi_row, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002175 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002176
2177 if (mi_col + size < cm->mi_cols &&
2178 !clpf_all_skip(cm, mi_col + size, mi_row, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002179 cm->clpf_blocks[tr] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002180
2181 if (mi_row + size < cm->mi_rows &&
2182 !clpf_all_skip(cm, mi_col, mi_row + size, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002183 cm->clpf_blocks[bl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002184
2185 if (mi_col + size < cm->mi_cols && mi_row + size < cm->mi_rows &&
2186 !clpf_all_skip(cm, mi_col + size, mi_row + size, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002187 cm->clpf_blocks[br] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002188 }
2189 }
David Barker9739f362016-11-10 09:29:32 +00002190#endif // CONFIG_CLPF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002191}
2192
Yaowu Xuc27fc142016-08-22 16:08:15 -07002193static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
2194 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002195 struct aom_internal_error_info *error_info,
2196 aom_reader *r, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002197 void *decrypt_state) {
2198 // Validate the calculated partition length. If the buffer
2199 // described by the partition can't be fully read, then restrict
2200 // it to the portion that can be (for EC mode) or throw an error.
2201 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002202 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002203 "Truncated packet or corrupt tile length");
2204
Alex Converse2cdf0d82016-12-13 13:53:09 -08002205#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converse346440b2017-01-03 13:47:37 -08002206 r->window_size = ANS_MAX_SYMBOLS;
Alex Converse2cdf0d82016-12-13 13:53:09 -08002207#endif
Alex Converse346440b2017-01-03 13:47:37 -08002208 if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07002209 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002210 "Failed to allocate bool decoder %d", 1);
2211}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002212
Yushin Cho77bba8d2016-11-04 16:36:56 -07002213#if !CONFIG_PVQ
Yaowu Xuf883b422016-08-30 14:01:10 -07002214static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
2215 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002216 int i, j, k, l, m;
Thomas9ac55082016-09-23 18:04:17 +01002217#if CONFIG_EC_ADAPT
Thomas Davies09ebbfb2016-10-20 18:28:47 +01002218 const int node_limit = UNCONSTRAINED_NODES - 1;
Thomas9ac55082016-09-23 18:04:17 +01002219#else
2220 const int node_limit = UNCONSTRAINED_NODES;
2221#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002222
Michael Bebenita6048d052016-08-25 14:40:54 -07002223 if (aom_read_bit(r, ACCT_STR))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002224 for (i = 0; i < PLANE_TYPES; ++i)
2225 for (j = 0; j < REF_TYPES; ++j)
2226 for (k = 0; k < COEF_BANDS; ++k)
2227 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
Thomas9ac55082016-09-23 18:04:17 +01002228 for (m = 0; m < node_limit; ++m)
Michael Bebenita6048d052016-08-25 14:40:54 -07002229 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002230}
2231
Yaowu Xuf883b422016-08-30 14:01:10 -07002232static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002233 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
2234 TX_SIZE tx_size;
Jingning Han83630632016-12-16 11:27:25 -08002235 for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002236 read_coef_probs_common(fc->coef_probs[tx_size], r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002237}
Yushin Cho77bba8d2016-11-04 16:36:56 -07002238#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002239
Yaowu Xuf883b422016-08-30 14:01:10 -07002240static void setup_segmentation(AV1_COMMON *const cm,
2241 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002242 struct segmentation *const seg = &cm->seg;
2243 int i, j;
2244
2245 seg->update_map = 0;
2246 seg->update_data = 0;
2247
Yaowu Xuf883b422016-08-30 14:01:10 -07002248 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002249 if (!seg->enabled) return;
2250
2251 // Segmentation map update
2252 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2253 seg->update_map = 1;
2254 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002255 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002256 }
2257 if (seg->update_map) {
2258 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2259 seg->temporal_update = 0;
2260 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002261 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002262 }
2263 }
2264
2265 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07002266 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002267 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002268 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002269
Yaowu Xuf883b422016-08-30 14:01:10 -07002270 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002271
2272 for (i = 0; i < MAX_SEGMENTS; i++) {
2273 for (j = 0; j < SEG_LVL_MAX; j++) {
2274 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002275 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002276 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002277 av1_enable_segfeature(seg, i, j);
2278 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
2279 if (av1_is_segfeature_signed(j))
2280 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002281 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002282 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002283 }
2284 }
2285 }
2286}
2287
2288#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002289static void decode_restoration_mode(AV1_COMMON *cm,
2290 struct aom_read_bit_buffer *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002291 int p;
2292 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002293 if (aom_rb_read_bit(rb)) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002294 if (aom_rb_read_bit(rb))
2295 rsi->frame_restoration_type =
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002296 (aom_rb_read_bit(rb) ? RESTORE_DOMAINTXFMRF : RESTORE_SGRPROJ);
Debargha Mukherjee0e67b252016-12-08 09:22:44 -08002297 else
2298 rsi->frame_restoration_type = RESTORE_WIENER;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002299 } else {
2300 rsi->frame_restoration_type =
2301 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
2302 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002303 for (p = 1; p < MAX_MB_PLANE; ++p) {
2304 cm->rst_info[p].frame_restoration_type =
2305 aom_rb_read_bit(rb) ? RESTORE_WIENER : RESTORE_NONE;
2306 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002307}
2308
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002309static void read_wiener_filter(WienerInfo *wiener_info, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002310 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002311 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2312 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002313 wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002314 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2315 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002316 wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002317 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2318 WIENER_FILT_TAP2_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002319 wiener_info->vfilter[WIENER_HALFWIN] =
2320 WIENER_FILT_STEP -
2321 2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
2322 wiener_info->vfilter[2]);
2323 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002324 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2325 WIENER_FILT_TAP0_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002326 wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002327 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2328 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002329 wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002330 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2331 WIENER_FILT_TAP2_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002332 wiener_info->hfilter[WIENER_HALFWIN] =
2333 WIENER_FILT_STEP -
2334 2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
2335 wiener_info->hfilter[2]);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002336}
2337
2338static void read_sgrproj_filter(SgrprojInfo *sgrproj_info, aom_reader *rb) {
2339 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
2340 sgrproj_info->xqd[0] =
2341 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN0;
2342 sgrproj_info->xqd[1] =
2343 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN1;
2344}
2345
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002346static void read_domaintxfmrf_filter(DomaintxfmrfInfo *domaintxfmrf_info,
2347 aom_reader *rb) {
2348 domaintxfmrf_info->sigma_r =
2349 aom_read_literal(rb, DOMAINTXFMRF_PARAMS_BITS, ACCT_STR);
2350}
2351
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002352static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002353 int i, p;
clang-formatbda8d612016-09-19 15:55:46 -07002354 const int ntiles =
2355 av1_get_rest_ntiles(cm->width, cm->height, NULL, NULL, NULL, NULL);
Debargha Mukherjeed7489142017-01-05 13:58:16 -08002356 const int ntiles_uv = av1_get_rest_ntiles(cm->width >> cm->subsampling_x,
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08002357 cm->height >> cm->subsampling_y,
Debargha Mukherjeed7489142017-01-05 13:58:16 -08002358 NULL, NULL, NULL, NULL);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002359 RestorationInfo *rsi = &cm->rst_info[0];
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002360 if (rsi->frame_restoration_type != RESTORE_NONE) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002361 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002362 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002363 rsi->restoration_type[i] =
2364 aom_read_tree(rb, av1_switchable_restore_tree,
2365 cm->fc->switchable_restore_prob, ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002366 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002367 read_wiener_filter(&rsi->wiener_info[i], rb);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002368 } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002369 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002370 } else if (rsi->restoration_type[i] == RESTORE_DOMAINTXFMRF) {
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002371 read_domaintxfmrf_filter(&rsi->domaintxfmrf_info[i], rb);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002372 }
2373 }
2374 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002375 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002376 if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002377 rsi->restoration_type[i] = RESTORE_WIENER;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002378 read_wiener_filter(&rsi->wiener_info[i], rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002379 } else {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002380 rsi->restoration_type[i] = RESTORE_NONE;
2381 }
2382 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002383 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002384 for (i = 0; i < ntiles; ++i) {
2385 if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
2386 rsi->restoration_type[i] = RESTORE_SGRPROJ;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002387 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
2388 } else {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002389 rsi->restoration_type[i] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002390 }
2391 }
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002392 } else if (rsi->frame_restoration_type == RESTORE_DOMAINTXFMRF) {
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002393 for (i = 0; i < ntiles; ++i) {
2394 if (aom_read(rb, RESTORE_NONE_DOMAINTXFMRF_PROB, ACCT_STR)) {
2395 rsi->restoration_type[i] = RESTORE_DOMAINTXFMRF;
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002396 read_domaintxfmrf_filter(&rsi->domaintxfmrf_info[i], rb);
2397 } else {
Debargha Mukherjee3981be92016-11-21 09:35:44 -08002398 rsi->restoration_type[i] = RESTORE_NONE;
2399 }
2400 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002401 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002402 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002403 for (p = 1; p < MAX_MB_PLANE; ++p) {
2404 rsi = &cm->rst_info[p];
2405 if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee994ccd72017-01-06 11:18:23 -08002406 for (i = 0; i < ntiles_uv; ++i) {
2407 if (ntiles_uv > 1)
2408 rsi->restoration_type[i] =
2409 aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR) ? RESTORE_WIENER
2410 : RESTORE_NONE;
2411 else
2412 rsi->restoration_type[i] = RESTORE_WIENER;
2413 if (rsi->restoration_type[i] == RESTORE_WIENER) {
2414 read_wiener_filter(&rsi->wiener_info[i], rb);
2415 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08002416 }
2417 }
2418 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002419}
2420#endif // CONFIG_LOOP_RESTORATION
2421
Yaowu Xuf883b422016-08-30 14:01:10 -07002422static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002423 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07002424 lf->filter_level = aom_rb_read_literal(rb, 6);
2425 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002426
2427 // Read in loop filter deltas applied at the MB level based on mode or ref
2428 // frame.
2429 lf->mode_ref_delta_update = 0;
2430
Yaowu Xuf883b422016-08-30 14:01:10 -07002431 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002432 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002433 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002434 if (lf->mode_ref_delta_update) {
2435 int i;
2436
2437 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002438 if (aom_rb_read_bit(rb))
2439 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002440
2441 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002442 if (aom_rb_read_bit(rb))
2443 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002444 }
2445 }
2446}
2447
2448#if CONFIG_CLPF
Yaowu Xud71be782016-10-14 08:47:03 -07002449static void setup_clpf(AV1Decoder *pbi, struct aom_read_bit_buffer *rb) {
2450 AV1_COMMON *const cm = &pbi->common;
2451 const int width = pbi->cur_buf->buf.y_crop_width;
2452 const int height = pbi->cur_buf->buf.y_crop_height;
2453
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002454 cm->clpf_blocks = 0;
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02002455 cm->clpf_strength_y = aom_rb_read_literal(rb, 2);
2456 cm->clpf_strength_u = aom_rb_read_literal(rb, 2);
2457 cm->clpf_strength_v = aom_rb_read_literal(rb, 2);
2458 if (cm->clpf_strength_y) {
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002459 cm->clpf_size = aom_rb_read_literal(rb, 2);
Yaowu Xud71be782016-10-14 08:47:03 -07002460 if (cm->clpf_size != CLPF_NOSIZE) {
2461 int size;
2462 cm->clpf_stride =
2463 ((width + MIN_FB_SIZE - 1) & ~(MIN_FB_SIZE - 1)) >> MIN_FB_SIZE_LOG2;
2464 size =
2465 cm->clpf_stride * ((height + MIN_FB_SIZE - 1) & ~(MIN_FB_SIZE - 1)) >>
2466 MIN_FB_SIZE_LOG2;
2467 CHECK_MEM_ERROR(cm, cm->clpf_blocks, aom_malloc(size));
2468 memset(cm->clpf_blocks, -1, size);
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002469 }
2470 }
2471}
2472
Steinar Midtskogen2fd70ee2016-09-02 10:02:30 +02002473static int clpf_bit(UNUSED int k, UNUSED int l,
2474 UNUSED const YV12_BUFFER_CONFIG *rec,
2475 UNUSED const YV12_BUFFER_CONFIG *org,
2476 UNUSED const AV1_COMMON *cm, UNUSED int block_size,
2477 UNUSED int w, UNUSED int h, UNUSED unsigned int strength,
Yaowu Xud71be782016-10-14 08:47:03 -07002478 UNUSED unsigned int fb_size_log2, int8_t *bit) {
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002479 return *bit;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002480}
2481#endif
2482
2483#if CONFIG_DERING
Yaowu Xuf883b422016-08-30 14:01:10 -07002484static void setup_dering(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
2485 cm->dering_level = aom_rb_read_literal(rb, DERING_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002486}
2487#endif // CONFIG_DERING
2488
Yaowu Xuf883b422016-08-30 14:01:10 -07002489static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2490 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002491}
2492
Yaowu Xuf883b422016-08-30 14:01:10 -07002493static void setup_quantization(AV1_COMMON *const cm,
2494 struct aom_read_bit_buffer *rb) {
2495 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002496 cm->y_dc_delta_q = read_delta_q(rb);
2497 cm->uv_dc_delta_q = read_delta_q(rb);
2498 cm->uv_ac_delta_q = read_delta_q(rb);
2499 cm->dequant_bit_depth = cm->bit_depth;
2500#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002501 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002502 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002503 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2504 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002505 } else {
2506 cm->min_qmlevel = 0;
2507 cm->max_qmlevel = 0;
2508 }
2509#endif
2510}
2511
Yaowu Xuf883b422016-08-30 14:01:10 -07002512static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002513 // Build y/uv dequant values based on segmentation.
2514 int i = 0;
2515#if CONFIG_AOM_QM
2516 int lossless;
2517 int j = 0;
2518 int qmlevel;
2519 int using_qm = cm->using_qmatrix;
2520 int minqm = cm->min_qmlevel;
2521 int maxqm = cm->max_qmlevel;
2522#endif
2523#if CONFIG_NEW_QUANT
2524 int b;
2525 int dq;
2526#endif // CONFIG_NEW_QUANT
2527 if (cm->seg.enabled) {
2528 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002529 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002530 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002531 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2532 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002533 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002534 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002535 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002536 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002537#if CONFIG_AOM_QM
2538 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2539 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2540 // NB: depends on base index so there is only 1 set per frame
2541 // No quant weighting when lossless or signalled not using QM
2542 qmlevel = (lossless || using_qm == 0)
2543 ? NUM_QM_LEVELS - 1
2544 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2545 for (j = 0; j < TX_SIZES; ++j) {
2546 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2547 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2548 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2549 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2550 }
2551#endif // CONFIG_AOM_QM
2552#if CONFIG_NEW_QUANT
2553 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2554 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002555 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002556 cm->y_dequant_nuq[i][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002557 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002558 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002559 }
2560 }
2561#endif // CONFIG_NEW_QUANT
2562 }
2563 } else {
2564 const int qindex = cm->base_qindex;
2565 // When segmentation is disabled, only the first value is used. The
2566 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002567 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2568 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002569 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002570 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002571 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002572 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002573#if CONFIG_AOM_QM
2574 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2575 cm->uv_ac_delta_q == 0;
2576 // No quant weighting when lossless or signalled not using QM
2577 qmlevel = (lossless || using_qm == 0)
2578 ? NUM_QM_LEVELS - 1
2579 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2580 for (j = 0; j < TX_SIZES; ++j) {
2581 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2582 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2583 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2584 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2585 }
2586#endif
2587#if CONFIG_NEW_QUANT
2588 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2589 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002590 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002591 cm->y_dequant_nuq[0][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002592 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002593 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002594 }
2595 }
2596#endif // CONFIG_NEW_QUANT
2597 }
2598}
2599
Angie Chiang5678ad92016-11-21 09:38:40 -08002600static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002601 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07002602 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002603}
2604
Yaowu Xuf883b422016-08-30 14:01:10 -07002605static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002606 cm->render_width = cm->width;
2607 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002608 if (aom_rb_read_bit(rb))
2609 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002610}
2611
Yaowu Xuf883b422016-08-30 14:01:10 -07002612static void resize_mv_buffer(AV1_COMMON *cm) {
2613 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002614 cm->cur_frame->mi_rows = cm->mi_rows;
2615 cm->cur_frame->mi_cols = cm->mi_cols;
2616 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002617 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002618 sizeof(*cm->cur_frame->mvs)));
2619}
2620
Yaowu Xuf883b422016-08-30 14:01:10 -07002621static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002622#if CONFIG_SIZE_LIMIT
2623 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002624 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002625 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2626 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2627#endif
2628 if (cm->width != width || cm->height != height) {
2629 const int new_mi_rows =
2630 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2631 const int new_mi_cols =
2632 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2633
Yaowu Xuf883b422016-08-30 14:01:10 -07002634 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002635 // dimensions as well as the overall size.
2636 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002637 if (av1_alloc_context_buffers(cm, width, height))
2638 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002639 "Failed to allocate context buffers");
2640 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002641 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002642 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002643 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002644 cm->width = width;
2645 cm->height = height;
2646 }
2647 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2648 cm->mi_cols > cm->cur_frame->mi_cols) {
2649 resize_mv_buffer(cm);
2650 }
2651}
2652
Yaowu Xuf883b422016-08-30 14:01:10 -07002653static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002654 int width, height;
2655 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002656 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002657 resize_context_buffers(cm, width, height);
2658 setup_render_size(cm, rb);
2659
2660 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002661 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002662 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2663 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002664#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002665 cm->use_highbitdepth,
2666#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002667 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002668 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2669 pool->cb_priv)) {
2670 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002671 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002672 "Failed to allocate frame buffer");
2673 }
2674 unlock_buffer_pool(pool);
2675
2676 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2677 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2678 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2679 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2680 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2681 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2682 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2683}
2684
Yaowu Xuf883b422016-08-30 14:01:10 -07002685static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002686 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002687 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002688 int this_xss, int this_yss) {
2689 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2690 ref_yss == this_yss;
2691}
2692
Yaowu Xuf883b422016-08-30 14:01:10 -07002693static void setup_frame_size_with_refs(AV1_COMMON *cm,
2694 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002695 int width, height;
2696 int found = 0, i;
2697 int has_valid_ref_frame = 0;
2698 BufferPool *const pool = cm->buffer_pool;
2699 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002700 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002701 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2702 width = buf->y_crop_width;
2703 height = buf->y_crop_height;
2704 cm->render_width = buf->render_width;
2705 cm->render_height = buf->render_height;
2706 found = 1;
2707 break;
2708 }
2709 }
2710
2711 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002712 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002713 setup_render_size(cm, rb);
2714 }
2715
2716 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002717 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002718 "Invalid frame size");
2719
2720 // Check to make sure at least one of frames that this frame references
2721 // has valid dimensions.
2722 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2723 RefBuffer *const ref_frame = &cm->frame_refs[i];
2724 has_valid_ref_frame |=
2725 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2726 ref_frame->buf->y_crop_height, width, height);
2727 }
2728 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002729 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002730 "Referenced frame has invalid size");
2731 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2732 RefBuffer *const ref_frame = &cm->frame_refs[i];
2733 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
2734 ref_frame->buf->subsampling_x,
2735 ref_frame->buf->subsampling_y, cm->bit_depth,
2736 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07002737 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002738 "Referenced frame has incompatible color format");
2739 }
2740
2741 resize_context_buffers(cm, width, height);
2742
2743 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002744 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002745 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2746 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002747#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002748 cm->use_highbitdepth,
2749#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002750 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002751 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2752 pool->cb_priv)) {
2753 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002754 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002755 "Failed to allocate frame buffer");
2756 }
2757 unlock_buffer_pool(pool);
2758
2759 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2760 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2761 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2762 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2763 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2764 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2765 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2766}
2767
Yaowu Xuf883b422016-08-30 14:01:10 -07002768static void read_tile_info(AV1Decoder *const pbi,
2769 struct aom_read_bit_buffer *const rb) {
2770 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002771#if CONFIG_EXT_TILE
2772// Read the tile width/height
2773#if CONFIG_EXT_PARTITION
2774 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002775 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
2776 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002777 } else
2778#endif // CONFIG_EXT_PARTITION
2779 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002780 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
2781 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002782 }
2783
Ryan Lei7386eda2016-12-08 21:08:31 -08002784#if CONFIG_DEBLOCKING_ACROSS_TILES
2785 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
2786#endif // CONFIG_DEBLOCKING_ACROSS_TILES
2787
Yaowu Xuc27fc142016-08-22 16:08:15 -07002788 cm->tile_width <<= cm->mib_size_log2;
2789 cm->tile_height <<= cm->mib_size_log2;
2790
Yaowu Xuf883b422016-08-30 14:01:10 -07002791 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
2792 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002793
2794 // Get the number of tiles
2795 cm->tile_cols = 1;
2796 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
2797
2798 cm->tile_rows = 1;
2799 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
2800
2801 if (cm->tile_cols * cm->tile_rows > 1) {
2802 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07002803 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2804 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002805 }
2806#else
2807 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07002808 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002809
2810 // columns
2811 max_ones = max_log2_tile_cols - min_log2_tile_cols;
2812 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07002813 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002814
2815 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07002816 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002817 "Invalid number of tile columns");
2818
2819 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07002820 cm->log2_tile_rows = aom_rb_read_bit(rb);
2821 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002822
Ryan Lei7386eda2016-12-08 21:08:31 -08002823#if CONFIG_DEBLOCKING_ACROSS_TILES
2824 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
2825#endif // CONFIG_DEBLOCKING_ACROSS_TILES
2826
Yaowu Xuc27fc142016-08-22 16:08:15 -07002827 cm->tile_cols = 1 << cm->log2_tile_cols;
2828 cm->tile_rows = 1 << cm->log2_tile_rows;
2829
2830 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
2831 cm->tile_width >>= cm->log2_tile_cols;
2832 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
2833 cm->tile_height >>= cm->log2_tile_rows;
2834
2835 // round to integer multiples of superblock size
2836 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
2837 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
2838
Thomas Davies4974e522016-11-07 17:44:05 +00002839// tile size magnitude
2840#if !CONFIG_TILE_GROUPS
2841 if (cm->tile_rows > 1 || cm->tile_cols > 1)
2842#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002843 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002844#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00002845
Thomas Davies80188d12016-10-26 16:08:35 -07002846#if CONFIG_TILE_GROUPS
2847 // Store an index to the location of the tile group information
2848 pbi->tg_size_bit_offset = rb->bit_offset;
2849 pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
2850 if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
2851 pbi->tg_start =
2852 aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
2853 pbi->tg_size =
2854 1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
2855 }
2856#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002857}
2858
2859static int mem_get_varsize(const uint8_t *src, const int sz) {
2860 switch (sz) {
2861 case 1: return src[0];
2862 case 2: return mem_get_le16(src);
2863 case 3: return mem_get_le24(src);
2864 case 4: return mem_get_le32(src);
2865 default: assert("Invalid size" && 0); return -1;
2866 }
2867}
2868
2869#if CONFIG_EXT_TILE
2870// Reads the next tile returning its size and adjusting '*data' accordingly
2871// based on 'is_last'.
2872static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07002873 struct aom_internal_error_info *error_info,
2874 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002875 void *decrypt_state,
2876 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
2877 int tile_size_bytes, int col, int row) {
2878 size_t size;
2879
2880 size_t copy_size = 0;
2881 const uint8_t *copy_data = NULL;
2882
2883 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002884 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002885 "Truncated packet or corrupt tile length");
2886 if (decrypt_cb) {
2887 uint8_t be_data[4];
2888 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
2889
2890 // Only read number of bytes in cm->tile_size_bytes.
2891 size = mem_get_varsize(be_data, tile_size_bytes);
2892 } else {
2893 size = mem_get_varsize(*data, tile_size_bytes);
2894 }
2895
2896 // The top bit indicates copy mode
2897 if ((size >> (tile_size_bytes * 8 - 1)) == 1) {
2898 // The remaining bits in the top byte signal the row offset
2899 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
2900
2901 // Currently, only use tiles in same column as reference tiles.
2902 copy_data = tile_buffers[row - offset][col].data;
2903 copy_size = tile_buffers[row - offset][col].size;
2904 size = 0;
2905 }
2906
2907 *data += tile_size_bytes;
2908
2909 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07002910 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002911 "Truncated packet or corrupt tile size");
2912
2913 if (size > 0) {
2914 tile_buffers[row][col].data = *data;
2915 tile_buffers[row][col].size = size;
2916 } else {
2917 tile_buffers[row][col].data = copy_data;
2918 tile_buffers[row][col].size = copy_size;
2919 }
2920
2921 *data += size;
2922
2923 tile_buffers[row][col].raw_data_end = *data;
2924}
2925
2926static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07002927 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002928 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002929 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002930 const int tile_cols = cm->tile_cols;
2931 const int tile_rows = cm->tile_rows;
2932 const int have_tiles = tile_cols * tile_rows > 1;
2933
2934 if (!have_tiles) {
2935 const uint32_t tile_size = data_end - data;
2936 tile_buffers[0][0].data = data;
2937 tile_buffers[0][0].size = tile_size;
2938 tile_buffers[0][0].raw_data_end = NULL;
2939 } else {
2940 // We locate only the tile buffers that are required, which are the ones
2941 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
2942 // need the last (bottom right) tile buffer, as we need to know where the
2943 // end of the compressed frame buffer is for proper superframe decoding.
2944
2945 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
2946 const uint8_t *const data_start = data;
2947
Yaowu Xuf883b422016-08-30 14:01:10 -07002948 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002949 const int single_row = pbi->dec_tile_row >= 0;
2950 const int tile_rows_start = single_row ? dec_tile_row : 0;
2951 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07002952 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002953 const int single_col = pbi->dec_tile_col >= 0;
2954 const int tile_cols_start = single_col ? dec_tile_col : 0;
2955 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
2956
2957 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
2958 const int tile_size_bytes = pbi->tile_size_bytes;
2959
2960 size_t tile_col_size;
2961 int r, c;
2962
2963 // Read tile column sizes for all columns (we need the last tile buffer)
2964 for (c = 0; c < tile_cols; ++c) {
2965 const int is_last = c == tile_cols - 1;
2966 if (!is_last) {
2967 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
2968 data += tile_col_size_bytes;
2969 tile_col_data_end[c] = data + tile_col_size;
2970 } else {
2971 tile_col_size = data_end - data;
2972 tile_col_data_end[c] = data_end;
2973 }
2974 data += tile_col_size;
2975 }
2976
2977 data = data_start;
2978
2979 // Read the required tile sizes.
2980 for (c = tile_cols_start; c < tile_cols_end; ++c) {
2981 const int is_last = c == tile_cols - 1;
2982
2983 if (c > 0) data = tile_col_data_end[c - 1];
2984
2985 if (!is_last) data += tile_col_size_bytes;
2986
2987 // Get the whole of the last column, otherwise stop at the required tile.
2988 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
2989 tile_buffers[r][c].col = c;
2990
2991 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
2992 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
2993 tile_size_bytes, c, r);
2994 }
2995 }
2996
2997 // If we have not read the last column, then read it to get the last tile.
2998 if (tile_cols_end != tile_cols) {
2999 c = tile_cols - 1;
3000
3001 data = tile_col_data_end[c - 1];
3002
3003 for (r = 0; r < tile_rows; ++r) {
3004 tile_buffers[r][c].col = c;
3005
3006 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3007 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3008 tile_size_bytes, c, r);
3009 }
3010 }
3011 }
3012}
3013#else
3014// Reads the next tile returning its size and adjusting '*data' accordingly
3015// based on 'is_last'.
3016static void get_tile_buffer(const uint8_t *const data_end,
3017 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07003018 struct aom_internal_error_info *error_info,
3019 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003020 void *decrypt_state, TileBufferDec *const buf) {
3021 size_t size;
3022
3023 if (!is_last) {
3024 if (!read_is_valid(*data, 4, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003025 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003026 "Truncated packet or corrupt tile length");
3027
3028 if (decrypt_cb) {
3029 uint8_t be_data[4];
3030 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3031 size = mem_get_varsize(be_data, tile_size_bytes);
3032 } else {
3033 size = mem_get_varsize(*data, tile_size_bytes);
3034 }
3035 *data += tile_size_bytes;
3036
3037 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003038 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003039 "Truncated packet or corrupt tile size");
3040 } else {
3041 size = data_end - *data;
3042 }
3043
3044 buf->data = *data;
3045 buf->size = size;
3046
3047 *data += size;
3048}
3049
3050static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003051 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003052 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003053 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07003054#if CONFIG_TILE_GROUPS
3055 int r, c;
3056 const int tile_cols = cm->tile_cols;
3057 const int tile_rows = cm->tile_rows;
3058 int tc = 0;
3059 int first_tile_in_tg = 0;
3060 int hdr_offset;
3061 struct aom_read_bit_buffer rb_tg_hdr;
3062 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3063 const int num_tiles = tile_rows * tile_cols;
3064 const int num_bits = OD_ILOG(num_tiles) - 1;
3065 const int hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
3066 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
3067
3068 for (r = 0; r < tile_rows; ++r) {
3069 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07003070 TileBufferDec *const buf = &tile_buffers[r][c];
3071 hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
3072
3073 buf->col = c;
3074 if (hdr_offset) {
3075 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
3076 rb_tg_hdr.bit_offset = tg_size_bit_offset;
3077 if (num_tiles) {
3078 pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
3079 pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
3080 }
3081 }
3082 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
3083 data += hdr_offset;
Thomas Davies8fe64a32016-10-04 13:19:31 +01003084 get_tile_buffer(data_end, pbi->tile_size_bytes, 0, &pbi->common.error,
3085 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
Thomas Davies80188d12016-10-26 16:08:35 -07003086 }
3087 }
3088#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003089 int r, c;
3090 const int tile_cols = cm->tile_cols;
3091 const int tile_rows = cm->tile_rows;
3092
3093 for (r = 0; r < tile_rows; ++r) {
3094 for (c = 0; c < tile_cols; ++c) {
3095 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
3096 TileBufferDec *const buf = &tile_buffers[r][c];
3097 buf->col = c;
3098 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
3099 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
3100 }
3101 }
Thomas Davies80188d12016-10-26 16:08:35 -07003102#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003103}
3104#endif // CONFIG_EXT_TILE
3105
Yushin Cho77bba8d2016-11-04 16:36:56 -07003106#if CONFIG_PVQ
Yushin Cho70669122016-12-08 09:53:14 -10003107static void daala_dec_init(AV1_COMMON *const cm, daala_dec_ctx *daala_dec,
Nathan E. Eggeab083972016-12-28 15:31:46 -05003108 aom_reader *r) {
3109 daala_dec->r = r;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003110 od_adapt_ctx_reset(&daala_dec->state.adapt, 0);
3111
Yushin Cho70669122016-12-08 09:53:14 -10003112 // TODO(yushin) : activity masking info needs be signaled by a bitstream
3113 daala_dec->use_activity_masking = AV1_PVQ_ENABLE_ACTIVITY_MASKING;
3114
3115 if (daala_dec->use_activity_masking)
3116 daala_dec->qm = OD_HVS_QM;
3117 else
3118 daala_dec->qm = OD_FLAT_QM;
Yushin Cho77bba8d2016-11-04 16:36:56 -07003119
3120 od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
3121 daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
Yushin Cho70669122016-12-08 09:53:14 -10003122
3123 if (daala_dec->use_activity_masking) {
3124 int pli;
3125 int use_masking = daala_dec->use_activity_masking;
3126 int segment_id = 0;
3127 int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
3128
3129 for (pli = 0; pli < MAX_MB_PLANE; pli++) {
3130 int i;
3131 int q;
3132
3133 q = qindex;
3134 if (q <= OD_DEFAULT_QMS[use_masking][0][pli].interp_q << OD_COEFF_SHIFT) {
3135 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3136 &OD_DEFAULT_QMS[use_masking][0][pli], NULL);
3137 } else {
3138 i = 0;
3139 while (OD_DEFAULT_QMS[use_masking][i + 1][pli].qm_q4 != NULL &&
3140 q > OD_DEFAULT_QMS[use_masking][i + 1][pli].interp_q
3141 << OD_COEFF_SHIFT) {
3142 i++;
3143 }
3144 od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
3145 &OD_DEFAULT_QMS[use_masking][i][pli],
3146 &OD_DEFAULT_QMS[use_masking][i + 1][pli]);
3147 }
3148 }
3149 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07003150}
Yushin Cho70669122016-12-08 09:53:14 -10003151#endif // #if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003152
Yaowu Xuf883b422016-08-30 14:01:10 -07003153static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003154 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003155 AV1_COMMON *const cm = &pbi->common;
3156 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003157 const int tile_cols = cm->tile_cols;
3158 const int tile_rows = cm->tile_rows;
3159 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07003160 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003161#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003162 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003163 const int single_row = pbi->dec_tile_row >= 0;
3164 const int tile_rows_start = single_row ? dec_tile_row : 0;
3165 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003166 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003167 const int single_col = pbi->dec_tile_col >= 0;
3168 const int tile_cols_start = single_col ? dec_tile_col : 0;
3169 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3170 const int inv_col_order = pbi->inv_tile_order && !single_col;
3171 const int inv_row_order = pbi->inv_tile_order && !single_row;
3172#else
3173 const int tile_rows_start = 0;
3174 const int tile_rows_end = tile_rows;
3175 const int tile_cols_start = 0;
3176 const int tile_cols_end = tile_cols;
3177 const int inv_col_order = pbi->inv_tile_order;
3178 const int inv_row_order = pbi->inv_tile_order;
3179#endif // CONFIG_EXT_TILE
3180 int tile_row, tile_col;
3181
3182#if CONFIG_ENTROPY
3183 cm->do_subframe_update = n_tiles == 1;
3184#endif // CONFIG_ENTROPY
3185
3186 if (cm->lf.filter_level && !cm->skip_loop_filter &&
3187 pbi->lf_worker.data1 == NULL) {
3188 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07003189 aom_memalign(32, sizeof(LFWorkerData)));
3190 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003191 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003192 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003193 "Loop filter thread creation failed");
3194 }
3195 }
3196
3197 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3198 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3199 // Be sure to sync as we might be resuming after a failed frame decode.
3200 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003201 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
3202 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003203 }
3204
3205 assert(tile_rows <= MAX_TILE_ROWS);
3206 assert(tile_cols <= MAX_TILE_COLS);
3207
3208 get_tile_buffers(pbi, data, data_end, tile_buffers);
3209
3210 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003211 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003212 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003213 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003214 pbi->allocated_tiles = n_tiles;
3215 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003216#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003217 if (pbi->acct_enabled) {
3218 aom_accounting_reset(&pbi->accounting);
3219 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003220#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003221 // Load all tile information into tile_data.
3222 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3223 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3224 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
3225 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
3226
3227 td->cm = cm;
3228 td->xd = pbi->mb;
3229 td->xd.corrupted = 0;
3230 td->xd.counts =
3231 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3232 ? &cm->counts
3233 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003234 av1_zero(td->dqcoeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003235#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003236 av1_zero(td->pvq_ref_coeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003237#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003238 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003239 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
3240 &td->bit_reader, pbi->decrypt_cb, pbi->decrypt_state);
Michael Bebenita6048d052016-08-25 14:40:54 -07003241#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003242 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003243 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003244 } else {
David Barkerd971f402016-10-25 13:52:07 +01003245 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003246 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003247#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003248 av1_init_macroblockd(cm, &td->xd,
3249#if CONFIG_PVQ
3250 td->pvq_ref_coeff,
3251#endif
3252 td->dqcoeff);
3253#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003254 daala_dec_init(cm, &td->xd.daala_dec, &td->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003255#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003256#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003257 td->xd.plane[0].color_index_map = td->color_index_map[0];
3258 td->xd.plane[1].color_index_map = td->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003259#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003260 }
3261 }
3262
3263 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3264 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
3265 int mi_row = 0;
3266 TileInfo tile_info;
3267
Yaowu Xuf883b422016-08-30 14:01:10 -07003268 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003269
3270 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3271 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
3272 TileData *const td = pbi->tile_data + tile_cols * row + col;
Michael Bebenita6048d052016-08-25 14:40:54 -07003273#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003274 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003275 td->bit_reader.accounting->last_tell_frac =
3276 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003277 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003278#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003279
Yaowu Xuf883b422016-08-30 14:01:10 -07003280 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003281
Yaowu Xuf883b422016-08-30 14:01:10 -07003282 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003283
3284 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
3285 mi_row += cm->mib_size) {
3286 int mi_col;
3287
Yaowu Xuf883b422016-08-30 14:01:10 -07003288 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003289
3290 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
3291 mi_col += cm->mib_size) {
Ryan Lei7386eda2016-12-08 21:08:31 -08003292#if CONFIG_DEBLOCKING_ACROSS_TILES
3293 if (av1_disable_loopfilter_on_tile_boundary(cm)) {
3294 av1_update_tile_boundary_info(cm, &tile_info, mi_row, mi_col);
3295 }
3296#endif // CONFIG_DEBLOCKING_ACROSS_TILES
3297
Yaowu Xuc27fc142016-08-22 16:08:15 -07003298 decode_partition(pbi, &td->xd,
3299#if CONFIG_SUPERTX
3300 0,
3301#endif // CONFIG_SUPERTX
3302 mi_row, mi_col, &td->bit_reader, cm->sb_size,
3303 b_width_log2_lookup[cm->sb_size]);
3304 }
3305 pbi->mb.corrupted |= td->xd.corrupted;
3306 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003307 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003308 "Failed to decode tile data");
3309#if CONFIG_ENTROPY
3310 if (cm->do_subframe_update &&
3311 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3312 if ((mi_row + MI_SIZE) %
3313 (MI_SIZE *
Yaowu Xuf883b422016-08-30 14:01:10 -07003314 AOMMAX(cm->mi_rows / MI_SIZE / COEF_PROBS_BUFS, 1)) ==
Yaowu Xuc27fc142016-08-22 16:08:15 -07003315 0 &&
3316 mi_row + MI_SIZE < cm->mi_rows &&
3317 cm->coef_probs_update_idx < COEF_PROBS_BUFS - 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003318 av1_partial_adapt_probs(cm, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003319 ++cm->coef_probs_update_idx;
3320 }
3321 }
3322#endif // CONFIG_ENTROPY
3323 }
3324 }
3325
3326 assert(mi_row > 0);
3327
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003328// when Parallel deblocking is enabled, deblocking should not
3329// be interleaved with decoding. Instead, deblocking should be done
3330// after the entire frame is decoded.
3331#if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003332 // Loopfilter one tile row.
3333 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3334 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003335 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003336 const int lf_end = tile_info.mi_row_end - cm->mib_size;
3337
3338 // Delay the loopfilter if the first tile row is only
3339 // a single superblock high.
3340 if (lf_end <= 0) continue;
3341
3342 // Decoding has completed. Finish up the loop filter in this thread.
3343 if (tile_info.mi_row_end >= cm->mi_rows) continue;
3344
3345 winterface->sync(&pbi->lf_worker);
3346 lf_data->start = lf_start;
3347 lf_data->stop = lf_end;
3348 if (pbi->max_threads > 1) {
3349 winterface->launch(&pbi->lf_worker);
3350 } else {
3351 winterface->execute(&pbi->lf_worker);
3352 }
3353 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003354#endif // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003355
3356 // After loopfiltering, the last 7 row pixels in each superblock row may
3357 // still be changed by the longest loopfilter of the next superblock row.
3358 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003359 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003360 }
3361
3362#if CONFIG_VAR_TX
3363 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003364 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
3365 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003366#else
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003367#if CONFIG_PARALLEL_DEBLOCKING
3368 // Loopfilter all rows in the frame in the frame.
3369 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3370 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3371 winterface->sync(&pbi->lf_worker);
3372 lf_data->start = 0;
3373 lf_data->stop = cm->mi_rows;
3374 winterface->execute(&pbi->lf_worker);
3375 }
3376#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003377 // Loopfilter remaining rows in the frame.
3378 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3379 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3380 winterface->sync(&pbi->lf_worker);
3381 lf_data->start = lf_data->stop;
3382 lf_data->stop = cm->mi_rows;
3383 winterface->execute(&pbi->lf_worker);
3384 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003385#endif // CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003386#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003387 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003388 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003389
3390#if CONFIG_EXT_TILE
3391 if (n_tiles == 1) {
3392#if CONFIG_ANS
3393 return data_end;
3394#else
3395 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07003396 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003397#endif // CONFIG_ANS
3398 } else {
3399 // Return the end of the last tile buffer
3400 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3401 }
3402#else
3403#if CONFIG_ANS
3404 return data_end;
3405#else
3406 {
3407 // Get last tile data.
3408 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003409 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003410 }
3411#endif // CONFIG_ANS
3412#endif // CONFIG_EXT_TILE
3413}
3414
3415static int tile_worker_hook(TileWorkerData *const tile_data,
3416 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003417 AV1Decoder *const pbi = tile_data->pbi;
3418 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003419 int mi_row, mi_col;
3420
3421 if (setjmp(tile_data->error_info.jmp)) {
3422 tile_data->error_info.setjmp = 0;
3423 tile_data->xd.corrupted = 1;
3424 return 0;
3425 }
3426
3427 tile_data->error_info.setjmp = 1;
3428 tile_data->xd.error_info = &tile_data->error_info;
3429
Yaowu Xuf883b422016-08-30 14:01:10 -07003430 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003431
3432 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
3433 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003434 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003435
3436 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3437 mi_col += cm->mib_size) {
3438 decode_partition(pbi, &tile_data->xd,
3439#if CONFIG_SUPERTX
3440 0,
3441#endif
3442 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
3443 b_width_log2_lookup[cm->sb_size]);
3444 }
3445 }
3446 return !tile_data->xd.corrupted;
3447}
3448
3449// sorts in descending order
3450static int compare_tile_buffers(const void *a, const void *b) {
3451 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
3452 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
3453 return (int)(buf2->size - buf1->size);
3454}
3455
Yaowu Xuf883b422016-08-30 14:01:10 -07003456static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003457 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003458 AV1_COMMON *const cm = &pbi->common;
3459 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003460 const int tile_cols = cm->tile_cols;
3461 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003462 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
clang-format67948d32016-09-07 22:40:40 -07003463 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003464#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003465 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003466 const int single_row = pbi->dec_tile_row >= 0;
3467 const int tile_rows_start = single_row ? dec_tile_row : 0;
3468 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003469 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003470 const int single_col = pbi->dec_tile_col >= 0;
3471 const int tile_cols_start = single_col ? dec_tile_col : 0;
3472 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3473#else
3474 const int tile_rows_start = 0;
3475 const int tile_rows_end = tile_rows;
3476 const int tile_cols_start = 0;
3477 const int tile_cols_end = tile_cols;
3478#endif // CONFIG_EXT_TILE
3479 int tile_row, tile_col;
3480 int i;
3481
3482#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3483 int final_worker = -1;
3484#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3485
3486 assert(tile_rows <= MAX_TILE_ROWS);
3487 assert(tile_cols <= MAX_TILE_COLS);
3488
3489 assert(tile_cols * tile_rows > 1);
3490
Yaowu Xuc27fc142016-08-22 16:08:15 -07003491 // TODO(jzern): See if we can remove the restriction of passing in max
3492 // threads to the decoder.
3493 if (pbi->num_tile_workers == 0) {
3494 const int num_threads = pbi->max_threads & ~1;
3495 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07003496 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003497 // Ensure tile data offsets will be properly aligned. This may fail on
3498 // platforms without DECLARE_ALIGNED().
3499 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3500 CHECK_MEM_ERROR(
3501 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003502 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003503 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07003504 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003505 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003506 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003507 ++pbi->num_tile_workers;
3508
3509 winterface->init(worker);
3510 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003511 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003512 "Tile decoder thread creation failed");
3513 }
3514 }
3515 }
3516
3517 // Reset tile decoding hook
3518 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003519 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003520 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003521 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003522 worker->data1 = &pbi->tile_worker_data[i];
3523 worker->data2 = &pbi->tile_worker_info[i];
3524 }
3525
3526 // Initialize thread frame counts.
3527 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3528 for (i = 0; i < num_workers; ++i) {
3529 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003530 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003531 }
3532 }
3533
3534 // Load tile data into tile_buffers
3535 get_tile_buffers(pbi, data, data_end, tile_buffers);
3536
3537 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3538 // Sort the buffers in this tile row based on size in descending order.
3539 qsort(&tile_buffers[tile_row][tile_cols_start],
3540 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
3541 compare_tile_buffers);
3542
3543 // Rearrange the tile buffers in this tile row such that per-tile group
3544 // the largest, and presumably the most difficult tile will be decoded in
3545 // the main thread. This should help minimize the number of instances
3546 // where the main thread is waiting for a worker to complete.
3547 {
3548 int group_start;
3549 for (group_start = tile_cols_start; group_start < tile_cols_end;
3550 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003551 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003552 const TileBufferDec largest = tile_buffers[tile_row][group_start];
3553 memmove(&tile_buffers[tile_row][group_start],
3554 &tile_buffers[tile_row][group_start + 1],
3555 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
3556 tile_buffers[tile_row][group_end - 1] = largest;
3557 }
3558 }
3559
3560 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
3561 // Launch workers for individual columns
3562 for (i = 0; i < num_workers && tile_col < tile_cols_end;
3563 ++i, ++tile_col) {
3564 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07003565 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003566 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
3567 TileInfo *const tile_info = (TileInfo *)worker->data2;
3568
3569 twd->pbi = pbi;
3570 twd->xd = pbi->mb;
3571 twd->xd.corrupted = 0;
3572 twd->xd.counts =
3573 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3574 ? &twd->counts
3575 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003576 av1_zero(twd->dqcoeff);
3577 av1_tile_init(tile_info, cm, tile_row, buf->col);
3578 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003579 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
3580 &twd->bit_reader, pbi->decrypt_cb,
3581 pbi->decrypt_state);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003582 av1_init_macroblockd(cm, &twd->xd,
3583#if CONFIG_PVQ
3584 twd->pvq_ref_coeff,
3585#endif
3586 twd->dqcoeff);
3587#if CONFIG_PVQ
Nathan E. Eggeab083972016-12-28 15:31:46 -05003588 daala_dec_init(cm, &twd->xd.daala_dec, &twd->bit_reader);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003589#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003590#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003591 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
3592 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003593#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003594
3595 worker->had_error = 0;
3596 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
3597 winterface->execute(worker);
3598 } else {
3599 winterface->launch(worker);
3600 }
3601
3602#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3603 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
3604 final_worker = i;
3605 }
3606#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3607 }
3608
3609 // Sync all workers
3610 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003611 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003612 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07003613 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07003614 // info in cm. Additionally once the threads have been synced and an
3615 // error is detected, there's no point in continuing to decode tiles.
3616 pbi->mb.corrupted |= !winterface->sync(worker);
3617 }
3618 }
3619 }
3620
3621 // Accumulate thread frame counts.
3622 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3623 for (i = 0; i < num_workers; ++i) {
3624 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003625 av1_accumulate_frame_counts(&cm->counts, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003626 }
3627 }
3628
3629#if CONFIG_EXT_TILE
3630 // Return the end of the last tile buffer
3631 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3632#else
3633#if CONFIG_ANS
3634 return data_end;
3635#else
3636 assert(final_worker != -1);
3637 {
3638 TileWorkerData *const twd =
3639 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003640 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003641 }
3642#endif // CONFIG_ANS
3643#endif // CONFIG_EXT_TILE
3644}
3645
3646static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003647 AV1_COMMON *const cm = (AV1_COMMON *)data;
3648 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003649}
3650
Yaowu Xuf883b422016-08-30 14:01:10 -07003651static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3652 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003653 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003654 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
3655#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003656 cm->use_highbitdepth = 1;
3657#endif
3658 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003659 cm->bit_depth = AOM_BITS_8;
3660#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003661 cm->use_highbitdepth = 0;
3662#endif
3663 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003664 cm->color_space = aom_rb_read_literal(rb, 3);
3665 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003666 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07003667 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003668 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003669 cm->subsampling_x = aom_rb_read_bit(rb);
3670 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003671 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003672 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003673 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07003674 if (aom_rb_read_bit(rb))
3675 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003676 "Reserved bit set");
3677 } else {
3678 cm->subsampling_y = cm->subsampling_x = 1;
3679 }
3680 } else {
3681 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
3682 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
3683 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
3684 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003685 if (aom_rb_read_bit(rb))
3686 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003687 "Reserved bit set");
3688 } else {
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 "4:4:4 color not supported in profile 0 or 2");
3691 }
3692 }
3693}
3694
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003695#if CONFIG_REFERENCE_BUFFER
3696void read_sequence_header(SequenceHeader *seq_params) {
3697 /* Placeholder for actually reading from the bitstream */
3698 seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
3699 seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
3700 seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
3701}
3702#endif
3703
Yaowu Xuf883b422016-08-30 14:01:10 -07003704static size_t read_uncompressed_header(AV1Decoder *pbi,
3705 struct aom_read_bit_buffer *rb) {
3706 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003707 MACROBLOCKD *const xd = &pbi->mb;
3708 BufferPool *const pool = cm->buffer_pool;
3709 RefCntBuffer *const frame_bufs = pool->frame_bufs;
3710 int i, mask, ref_index = 0;
3711 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003712
3713#if CONFIG_REFERENCE_BUFFER
3714 /* TODO: Move outside frame loop or inside key-frame branch */
3715 read_sequence_header(&pbi->seq_params);
3716#endif
3717
Yaowu Xuc27fc142016-08-22 16:08:15 -07003718 cm->last_frame_type = cm->frame_type;
3719 cm->last_intra_only = cm->intra_only;
3720
3721#if CONFIG_EXT_REFS
3722 // NOTE: By default all coded frames to be used as a reference
3723 cm->is_reference_frame = 1;
3724#endif // CONFIG_EXT_REFS
3725
Yaowu Xuf883b422016-08-30 14:01:10 -07003726 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
3727 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003728 "Invalid frame marker");
3729
Yaowu Xuf883b422016-08-30 14:01:10 -07003730 cm->profile = av1_read_profile(rb);
3731#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003732 if (cm->profile >= MAX_PROFILES)
Yaowu Xuf883b422016-08-30 14:01:10 -07003733 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003734 "Unsupported bitstream profile");
3735#else
3736 if (cm->profile >= PROFILE_2)
Yaowu Xuf883b422016-08-30 14:01:10 -07003737 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003738 "Unsupported bitstream profile");
3739#endif
3740
Yaowu Xuf883b422016-08-30 14:01:10 -07003741 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003742
3743 if (cm->show_existing_frame) {
Yaowu Xu415ba932016-12-27 11:17:32 -08003744 // Show an existing frame directly.
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003745 const int existing_frame_idx = aom_rb_read_literal(rb, 3);
3746 const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
Yaowu Xu415ba932016-12-27 11:17:32 -08003747#if CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003748 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003749 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3750 int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003751 /* Compare display_frame_id with ref_frame_id and check valid for
3752 * referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003753 if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
3754 cm->valid_for_referencing[existing_frame_idx] == 0)
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003755 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3756 "Reference buffer frame ID mismatch");
3757 }
3758#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003759 lock_buffer_pool(pool);
3760 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
3761 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07003762 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003763 "Buffer %d does not contain a decoded frame",
3764 frame_to_show);
3765 }
3766 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
3767 unlock_buffer_pool(pool);
3768
3769 cm->lf.filter_level = 0;
3770 cm->show_frame = 1;
3771 pbi->refresh_frame_flags = 0;
3772
3773 if (cm->frame_parallel_decode) {
3774 for (i = 0; i < REF_FRAMES; ++i)
3775 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
3776 }
3777
3778 return 0;
3779 }
3780
Yaowu Xuf883b422016-08-30 14:01:10 -07003781 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
3782 cm->show_frame = aom_rb_read_bit(rb);
3783 cm->error_resilient_mode = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003784#if CONFIG_REFERENCE_BUFFER
3785 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003786 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3787 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
3788 int prev_frame_id = 0;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003789 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003790 prev_frame_id = cm->current_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003791 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003792 cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003793
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003794 if (cm->frame_type != KEY_FRAME) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003795 int diff_frame_id;
3796 if (cm->current_frame_id > prev_frame_id) {
3797 diff_frame_id = cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003798 } else {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003799 diff_frame_id =
3800 (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003801 }
3802 /* Check current_frame_id for conformance */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003803 if (prev_frame_id == cm->current_frame_id ||
3804 diff_frame_id >= (1 << (frame_id_length - 1))) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003805 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3806 "Invalid value of current_frame_id");
3807 }
3808 }
3809 /* Check if some frames need to be marked as not valid for referencing */
3810 for (i = 0; i < REF_FRAMES; i++) {
3811 if (cm->frame_type == KEY_FRAME) {
3812 cm->valid_for_referencing[i] = 0;
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003813 } else if (cm->current_frame_id - (1 << diff_len) > 0) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003814 if (cm->ref_frame_id[i] > cm->current_frame_id ||
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003815 cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003816 cm->valid_for_referencing[i] = 0;
3817 } else {
3818 if (cm->ref_frame_id[i] > cm->current_frame_id &&
3819 cm->ref_frame_id[i] <
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003820 (1 << frame_id_length) + cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003821 cm->valid_for_referencing[i] = 0;
3822 }
3823 }
3824 }
3825#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003826 if (cm->frame_type == KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003827 if (!av1_read_sync_code(rb))
3828 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003829 "Invalid frame sync code");
3830
3831 read_bitdepth_colorspace_sampling(cm, rb);
3832 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
3833
3834 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3835 cm->frame_refs[i].idx = INVALID_IDX;
3836 cm->frame_refs[i].buf = NULL;
3837 }
3838
3839 setup_frame_size(cm, rb);
3840 if (pbi->need_resync) {
3841 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
3842 pbi->need_resync = 0;
3843 }
Urvang Joshib100db72016-10-12 16:28:56 -07003844#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07003845 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003846#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003847 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003848 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003849#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07003850 if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003851#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003852 if (cm->error_resilient_mode) {
3853 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
3854 } else {
3855 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003856 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003857 ? RESET_FRAME_CONTEXT_ALL
3858 : RESET_FRAME_CONTEXT_CURRENT;
3859 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003860 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003861 ? RESET_FRAME_CONTEXT_CURRENT
3862 : RESET_FRAME_CONTEXT_NONE;
3863 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07003864 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003865 ? RESET_FRAME_CONTEXT_ALL
3866 : RESET_FRAME_CONTEXT_CURRENT;
3867 }
3868 }
3869
3870 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003871 if (!av1_read_sync_code(rb))
3872 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003873 "Invalid frame sync code");
3874
3875 read_bitdepth_colorspace_sampling(cm, rb);
3876
Yaowu Xuf883b422016-08-30 14:01:10 -07003877 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003878 setup_frame_size(cm, rb);
3879 if (pbi->need_resync) {
3880 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
3881 pbi->need_resync = 0;
3882 }
3883 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Yaowu Xuf883b422016-08-30 14:01:10 -07003884 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003885
3886#if CONFIG_EXT_REFS
3887 if (!pbi->refresh_frame_flags) {
3888 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
3889 // will not be used as a reference
3890 cm->is_reference_frame = 0;
3891 }
3892#endif // CONFIG_EXT_REFS
3893
3894 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003895 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003896 const int idx = cm->ref_frame_map[ref];
3897 RefBuffer *const ref_frame = &cm->frame_refs[i];
3898 ref_frame->idx = idx;
3899 ref_frame->buf = &frame_bufs[idx].buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07003900 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003901#if CONFIG_REFERENCE_BUFFER
3902 if (pbi->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003903 int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
3904 int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
3905 int delta_frame_id_minus1 = aom_rb_read_literal(rb, diff_len);
3906 int ref_frame_id =
3907 ((cm->current_frame_id - (delta_frame_id_minus1 + 1) +
3908 (1 << frame_id_length)) %
3909 (1 << frame_id_length));
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003910 /* Compare values derived from delta_frame_id_minus1 and
3911 * refresh_frame_flags. Also, check valid for referencing */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01003912 if (ref_frame_id != cm->ref_frame_id[ref] ||
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003913 cm->valid_for_referencing[ref] == 0)
3914 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3915 "Reference buffer frame ID mismatch");
3916 }
3917#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003918 }
3919
Arild Fuldseth842e9b02016-09-02 13:00:05 +02003920#if CONFIG_FRAME_SIZE
3921 if (cm->error_resilient_mode == 0) {
3922 setup_frame_size_with_refs(cm, rb);
3923 } else {
3924 setup_frame_size(cm, rb);
3925 }
3926#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003927 setup_frame_size_with_refs(cm, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02003928#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003929
Yaowu Xuf883b422016-08-30 14:01:10 -07003930 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Angie Chiang5678ad92016-11-21 09:38:40 -08003931 cm->interp_filter = read_frame_interp_filter(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003932
3933 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3934 RefBuffer *const ref_buf = &cm->frame_refs[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07003935#if CONFIG_AOM_HIGHBITDEPTH
3936 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003937 &ref_buf->sf, ref_buf->buf->y_crop_width,
3938 ref_buf->buf->y_crop_height, cm->width, cm->height,
3939 cm->use_highbitdepth);
3940#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003941 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003942 &ref_buf->sf, ref_buf->buf->y_crop_width,
3943 ref_buf->buf->y_crop_height, cm->width, cm->height);
3944#endif
3945 }
3946 }
3947 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003948
3949#if CONFIG_REFERENCE_BUFFER
3950 if (pbi->seq_params.frame_id_numbers_present_flag) {
3951 /* If bitmask is set, update reference frame id values and
3952 mark frames as valid for reference */
3953 int refresh_frame_flags =
3954 cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
3955 for (i = 0; i < REF_FRAMES; i++) {
3956 if ((refresh_frame_flags >> i) & 1) {
3957 cm->ref_frame_id[i] = cm->current_frame_id;
3958 cm->valid_for_referencing[i] = 1;
3959 }
3960 }
3961 }
3962#endif
3963
Yaowu Xuf883b422016-08-30 14:01:10 -07003964#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003965 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
3966#endif
3967 get_frame_new_buffer(cm)->color_space = cm->color_space;
3968 get_frame_new_buffer(cm)->color_range = cm->color_range;
3969 get_frame_new_buffer(cm)->render_width = cm->render_width;
3970 get_frame_new_buffer(cm)->render_height = cm->render_height;
3971
3972 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003973 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003974 "Keyframe / intra-only frame required to reset decoder"
3975 " state");
3976 }
3977
3978 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003979 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003980 ? REFRESH_FRAME_CONTEXT_FORWARD
3981 : REFRESH_FRAME_CONTEXT_BACKWARD;
3982 } else {
3983 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
3984 }
3985
Yaowu Xuf883b422016-08-30 14:01:10 -07003986 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07003987 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07003988 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003989
3990 // Generate next_ref_frame_map.
3991 lock_buffer_pool(pool);
3992 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
3993 if (mask & 1) {
3994 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
3995 ++frame_bufs[cm->new_fb_idx].ref_count;
3996 } else {
3997 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
3998 }
3999 // Current thread holds the reference frame.
4000 if (cm->ref_frame_map[ref_index] >= 0)
4001 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4002 ++ref_index;
4003 }
4004
4005 for (; ref_index < REF_FRAMES; ++ref_index) {
4006 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4007
4008 // Current thread holds the reference frame.
4009 if (cm->ref_frame_map[ref_index] >= 0)
4010 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4011 }
4012 unlock_buffer_pool(pool);
4013 pbi->hold_ref_buf = 1;
4014
4015 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07004016 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004017
4018#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07004019 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004020#else
4021 set_sb_size(cm, BLOCK_64X64);
4022#endif // CONFIG_EXT_PARTITION
4023
4024 setup_loopfilter(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004025#if CONFIG_DERING
4026 setup_dering(cm, rb);
4027#endif
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004028#if CONFIG_CLPF
4029 setup_clpf(pbi, rb);
4030#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004031#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee874d36d2016-12-14 16:53:17 -08004032 av1_alloc_restoration_buffers(cm);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004033 decode_restoration_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004034#endif // CONFIG_LOOP_RESTORATION
4035 setup_quantization(cm, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004036#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004037 xd->bd = (int)cm->bit_depth;
4038#endif
4039
4040#if CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004041 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004042 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4043 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4044 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4045 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
4046 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4047 }
4048#endif // CONFIG_ENTROPY
4049
4050 setup_segmentation(cm, rb);
4051
Arild Fuldseth07441162016-08-15 15:07:52 +02004052#if CONFIG_DELTA_Q
4053 {
4054 struct segmentation *const seg = &cm->seg;
4055 int segment_quantizer_active = 0;
4056 for (i = 0; i < MAX_SEGMENTS; i++) {
4057 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
4058 segment_quantizer_active = 1;
4059 }
4060 }
4061
Thomas Daviesf6936102016-09-05 16:51:31 +01004062 cm->delta_q_res = 1;
Arild Fuldseth07441162016-08-15 15:07:52 +02004063 if (segment_quantizer_active == 0) {
4064 cm->delta_q_present_flag = aom_rb_read_bit(rb);
4065 } else {
4066 cm->delta_q_present_flag = 0;
4067 }
4068 if (cm->delta_q_present_flag) {
4069 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01004070 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Arild Fuldseth07441162016-08-15 15:07:52 +02004071 }
4072 }
4073#endif
4074
Urvang Joshi454280d2016-10-14 16:51:44 -07004075 for (i = 0; i < MAX_SEGMENTS; ++i) {
4076 const int qindex = cm->seg.enabled
4077 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
4078 : cm->base_qindex;
4079 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
4080 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4081 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004082 }
4083
4084 setup_segmentation_dequant(cm);
4085 cm->tx_mode =
4086 (!cm->seg.enabled && xd->lossless[0]) ? ONLY_4X4 : read_tx_mode(rb);
4087 cm->reference_mode = read_frame_reference_mode(cm, rb);
4088
4089 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004090 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004091
4092 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07004093 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004094 "Invalid header size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004095 return sz;
4096}
4097
4098#if CONFIG_EXT_TX
Thomas9ac55082016-09-23 18:04:17 +01004099#if !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuf883b422016-08-30 14:01:10 -07004100static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004101 int i, j, k;
4102 int s;
4103 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004104 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004105 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4106 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
4107 for (j = 0; j < num_ext_tx_set_inter[s] - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004108 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004109 }
4110 }
4111 }
4112
4113 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004114 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004115 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4116 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
4117 for (j = 0; j < INTRA_MODES; ++j)
4118 for (k = 0; k < num_ext_tx_set_intra[s] - 1; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004119 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
4120 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004121 }
4122 }
4123 }
4124}
Thomas9ac55082016-09-23 18:04:17 +01004125#endif // !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004126#else
4127
Yaowu Xuc27fc142016-08-22 16:08:15 -07004128#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004129#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07004130static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004131 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -07004132 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004133 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
Jingning Hanfeb517c2016-12-21 16:02:07 -08004134 for (j = TX_8X8; j < TX_SIZES; ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004135 av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004136 }
4137 }
4138 }
4139}
4140#endif // CONFIG_SUPERTX
4141
4142#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +00004143static void read_global_motion_params(WarpedMotionParams *params,
Yaowu Xuf883b422016-08-30 14:01:10 -07004144 aom_prob *probs, aom_reader *r) {
David Barkercf3d0b02016-11-10 10:14:49 +00004145 TransformationType type =
Michael Bebenita6048d052016-08-25 14:40:54 -07004146 aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004147 set_default_gmparams(params);
David Barkercf3d0b02016-11-10 10:14:49 +00004148 params->wmtype = type;
4149 switch (type) {
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004150 case HOMOGRAPHY:
4151 params->wmmat[6] = aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004152 GM_ROW3HOMO_DECODE_FACTOR;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004153 params->wmmat[7] = aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004154 GM_ROW3HOMO_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004155 case AFFINE:
4156 case ROTZOOM:
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004157 params->wmmat[2] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004158 GM_ALPHA_DECODE_FACTOR +
David Barkercf3d0b02016-11-10 10:14:49 +00004159 (1 << WARPEDMODEL_PREC_BITS);
4160 params->wmmat[3] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4161 GM_ALPHA_DECODE_FACTOR;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004162 if (type == AFFINE || type == HOMOGRAPHY) {
4163 params->wmmat[4] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004164 GM_ALPHA_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004165 params->wmmat[5] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4166 GM_ALPHA_DECODE_FACTOR +
4167 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004168 } else {
David Barkercf3d0b02016-11-10 10:14:49 +00004169 params->wmmat[4] = -params->wmmat[3];
4170 params->wmmat[5] = params->wmmat[2];
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004171 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004172 // fallthrough intended
David Barkercf3d0b02016-11-10 10:14:49 +00004173 case TRANSLATION:
4174 params->wmmat[0] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4175 GM_TRANS_DECODE_FACTOR;
4176 params->wmmat[1] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4177 GM_TRANS_DECODE_FACTOR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004178 break;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004179 case IDENTITY: break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004180 default: assert(0);
4181 }
4182}
4183
Yaowu Xuf883b422016-08-30 14:01:10 -07004184static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004185 int frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004186 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
4187 read_global_motion_params(&cm->global_motion[frame],
4188 cm->fc->global_motion_types_prob, r);
Sarah Parkere5299862016-08-16 14:57:37 -07004189 /*
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004190 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4191 frame, cm->current_video_frame, cm->show_frame,
David Barkercf3d0b02016-11-10 10:14:49 +00004192 cm->global_motion[frame].wmmat[0],
4193 cm->global_motion[frame].wmmat[1],
4194 cm->global_motion[frame].wmmat[2],
4195 cm->global_motion[frame].wmmat[3]);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004196 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004197 }
4198}
4199#endif // CONFIG_GLOBAL_MOTION
4200
Yaowu Xuf883b422016-08-30 14:01:10 -07004201static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004202 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004203 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004204#if CONFIG_SUPERTX
4205 MACROBLOCKD *const xd = &pbi->mb;
4206#endif
4207 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07004208 aom_reader r;
Yaowu Xu8af861b2016-11-01 12:12:11 -07004209 int k, i;
Yaowu Xud0af64f2016-11-17 12:50:47 -08004210#if !CONFIG_EC_ADAPT || CONFIG_EXT_INTRA
Yaowu Xu8af861b2016-11-01 12:12:11 -07004211 int j;
4212#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004213
Alex Converse2cdf0d82016-12-13 13:53:09 -08004214#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converse346440b2017-01-03 13:47:37 -08004215 r.window_size = ANS_MAX_SYMBOLS;
Alex Converse2cdf0d82016-12-13 13:53:09 -08004216#endif
Alex Converse346440b2017-01-03 13:47:37 -08004217 if (aom_reader_init(&r, data, partition_size, pbi->decrypt_cb,
4218 pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07004219 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004220 "Failed to allocate bool decoder 0");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004221
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004222#if CONFIG_LOOP_RESTORATION
4223 decode_restoration(cm, &r);
4224#endif
4225
Yaowu Xuefc75352016-10-31 09:46:42 -07004226 if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004227
Yushin Cho77bba8d2016-11-04 16:36:56 -07004228#if !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004229 read_coef_probs(fc, cm->tx_mode, &r);
4230
4231#if CONFIG_VAR_TX
4232 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004233 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
Yushin Cho77bba8d2016-11-04 16:36:56 -07004234#endif // CONFIG_VAR_TX
4235#endif // !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004236 for (k = 0; k < SKIP_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004237 av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004238
Thomas Daviesf6936102016-09-05 16:51:31 +01004239#if CONFIG_DELTA_Q
4240 for (k = 0; k < DELTA_Q_CONTEXTS; ++k)
4241 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
4242#endif
4243
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004244#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004245 if (cm->seg.enabled && cm->seg.update_map) {
4246 if (cm->seg.temporal_update) {
4247 for (k = 0; k < PREDICTION_PROBS; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004248 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004249 }
4250 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004251 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004252 }
4253
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004254 for (j = 0; j < INTRA_MODES; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004255 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004256 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004257 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004258
4259#if CONFIG_EXT_PARTITION_TYPES
4260 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004261 av1_diff_update_prob(&r, &fc->partition_prob[0][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004262 for (j = 1; j < PARTITION_CONTEXTS; ++j)
4263 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004264 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004265#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004266 for (j = 0; j < PARTITION_CONTEXTS; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004267 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004268 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004269#endif // CONFIG_EXT_PARTITION_TYPES
Thomas9ac55082016-09-23 18:04:17 +01004270#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004271#if CONFIG_EXT_INTRA
hui sueda3d762016-12-06 16:58:23 -08004272#if CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004273 for (i = 0; i < INTRA_FILTERS + 1; ++i)
4274 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004275 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
hui sueda3d762016-12-06 16:58:23 -08004276#endif // CONFIG_INTRA_INTERP
Thomas Davies6519beb2016-10-19 14:46:07 +01004277#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004278
4279 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004280 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Nathan E. Egge10ba2be2016-11-16 09:44:26 -05004281#if CONFIG_EC_MULTISYMBOL
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004282 av1_copy(cm->kf_y_cdf, av1_kf_y_mode_cdf);
4283#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004284#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004285 for (k = 0; k < INTRA_MODES; k++)
Thomas Davies6519beb2016-10-19 14:46:07 +01004286 for (j = 0; j < INTRA_MODES; j++)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004287 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004288 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004289#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004290 } else {
4291#if !CONFIG_REF_MV
4292 nmv_context *const nmvc = &fc->nmvc;
4293#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004294 read_inter_mode_probs(fc, &r);
4295
4296#if CONFIG_EXT_INTER
4297 read_inter_compound_mode_probs(fc, &r);
4298 if (cm->reference_mode != COMPOUND_REFERENCE) {
4299 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4300 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004301 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004302 }
4303 }
4304 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4305 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004306 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004307 }
4308 for (i = 0; i < BLOCK_SIZES; i++) {
4309 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004310 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004311 }
4312 }
4313 }
4314 if (cm->reference_mode != SINGLE_REFERENCE) {
4315 for (i = 0; i < BLOCK_SIZES; i++) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08004316 for (j = 0; j < COMPOUND_TYPES - 1; j++) {
4317 av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004318 }
4319 }
4320 }
4321#endif // CONFIG_EXT_INTER
4322
Yue Chencb60b182016-10-13 15:18:22 -07004323#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004324 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
Yue Chencb60b182016-10-13 15:18:22 -07004325 for (j = 0; j < MOTION_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004326 av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004327 }
Yue Chencb60b182016-10-13 15:18:22 -07004328#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004329
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004330#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004331 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004332#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004333
4334 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004335 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004336
4337 if (cm->reference_mode != SINGLE_REFERENCE)
4338 setup_compound_reference_mode(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004339 read_frame_reference_mode_probs(cm, &r);
4340
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004341#if !CONFIG_EC_ADAPT
Nathan E. Egge5710c722016-09-08 10:01:16 -04004342 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004343 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004344 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
Nathan E. Egge5710c722016-09-08 10:01:16 -04004345 }
Thomas9ac55082016-09-23 18:04:17 +01004346#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004347
4348#if CONFIG_REF_MV
4349 for (i = 0; i < NMV_CONTEXTS; ++i)
4350 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
4351#else
4352 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
4353#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004354#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004355 read_ext_tx_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004356#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004357#if CONFIG_SUPERTX
4358 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
4359#endif
4360#if CONFIG_GLOBAL_MOTION
4361 read_global_motion(cm, &r);
Thomas Davies6519beb2016-10-19 14:46:07 +01004362#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004363 }
Thomas Davies6519beb2016-10-19 14:46:07 +01004364#if CONFIG_EC_MULTISYMBOL
4365 av1_coef_pareto_cdfs(fc);
David Barker599dfd02016-11-10 13:20:12 +00004366#if CONFIG_REF_MV
4367 for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
4368#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004369 av1_set_mv_cdfs(&fc->nmvc);
David Barker599dfd02016-11-10 13:20:12 +00004370#endif
Nathan E. Egge31296062016-11-16 09:44:26 -05004371#if CONFIG_EC_MULTISYMBOL
Thomas Davies6519beb2016-10-19 14:46:07 +01004372 av1_set_mode_cdfs(cm);
4373#endif
4374#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004375
Yaowu Xuf883b422016-08-30 14:01:10 -07004376 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004377}
4378
4379#ifdef NDEBUG
4380#define debug_check_frame_counts(cm) (void)0
4381#else // !NDEBUG
4382// Counts should only be incremented when frame_parallel_decoding_mode and
4383// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07004384static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004385 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07004386 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004387 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
4388 cm->error_resilient_mode);
4389 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
4390 sizeof(cm->counts.y_mode)));
4391 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
4392 sizeof(cm->counts.uv_mode)));
4393 assert(!memcmp(cm->counts.partition, zero_counts.partition,
4394 sizeof(cm->counts.partition)));
4395 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
4396 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
4397 sizeof(cm->counts.eob_branch)));
4398 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
4399 sizeof(cm->counts.switchable_interp)));
4400 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
4401 sizeof(cm->counts.inter_mode)));
4402#if CONFIG_EXT_INTER
4403 assert(!memcmp(cm->counts.inter_compound_mode,
4404 zero_counts.inter_compound_mode,
4405 sizeof(cm->counts.inter_compound_mode)));
4406 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
4407 sizeof(cm->counts.interintra)));
4408 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
4409 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08004410 assert(!memcmp(cm->counts.compound_interinter,
4411 zero_counts.compound_interinter,
4412 sizeof(cm->counts.compound_interinter)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004413#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07004414#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
4415 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
4416 sizeof(cm->counts.motion_mode)));
4417#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004418 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
4419 sizeof(cm->counts.intra_inter)));
4420 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
4421 sizeof(cm->counts.comp_inter)));
4422 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
4423 sizeof(cm->counts.single_ref)));
4424 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
4425 sizeof(cm->counts.comp_ref)));
4426#if CONFIG_EXT_REFS
4427 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
4428 sizeof(cm->counts.comp_bwdref)));
4429#endif // CONFIG_EXT_REFS
4430 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
4431 sizeof(cm->counts.tx_size)));
4432 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
4433#if CONFIG_REF_MV
4434 assert(
4435 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
4436 assert(
4437 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
4438#else
4439 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
4440#endif
4441 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
4442 sizeof(cm->counts.inter_ext_tx)));
4443 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
4444 sizeof(cm->counts.intra_ext_tx)));
4445}
4446#endif // NDEBUG
4447
Yaowu Xuf883b422016-08-30 14:01:10 -07004448static struct aom_read_bit_buffer *init_read_bit_buffer(
4449 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
4450 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004451 rb->bit_offset = 0;
4452 rb->error_handler = error_handler;
4453 rb->error_handler_data = &pbi->common;
4454 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004455 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004456 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
4457 rb->bit_buffer = clear_data;
4458 rb->bit_buffer_end = clear_data + n;
4459 } else {
4460 rb->bit_buffer = data;
4461 rb->bit_buffer_end = data_end;
4462 }
4463 return rb;
4464}
4465
4466//------------------------------------------------------------------------------
4467
Yaowu Xuf883b422016-08-30 14:01:10 -07004468int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
4469 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
4470 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
4471 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004472}
4473
Yaowu Xuf883b422016-08-30 14:01:10 -07004474void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
4475 int *height) {
4476 *width = aom_rb_read_literal(rb, 16) + 1;
4477 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004478}
4479
Yaowu Xuf883b422016-08-30 14:01:10 -07004480BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
4481 int profile = aom_rb_read_bit(rb);
4482 profile |= aom_rb_read_bit(rb) << 1;
4483 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004484 return (BITSTREAM_PROFILE)profile;
4485}
4486
Yaowu Xuf883b422016-08-30 14:01:10 -07004487void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
4488 const uint8_t *data_end, const uint8_t **p_data_end) {
4489 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004490 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07004491 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004492 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004493 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004494 size_t first_partition_size;
4495 YV12_BUFFER_CONFIG *new_fb;
4496
4497#if CONFIG_BITSTREAM_DEBUG
4498 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
4499#endif
4500
4501 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004502 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Thomas Davies72712e62016-11-09 12:17:51 +00004503#if CONFIG_TILE_GROUPS
4504 pbi->first_partition_size = first_partition_size;
4505 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
4506#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004507 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004508 xd->cur_buf = new_fb;
4509#if CONFIG_GLOBAL_MOTION
4510 xd->global_motion = cm->global_motion;
4511#endif // CONFIG_GLOBAL_MOTION
4512
4513 if (!first_partition_size) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004514 // showing a frame directly
4515 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004516 return;
4517 }
4518
Yaowu Xuf883b422016-08-30 14:01:10 -07004519 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004520 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07004521 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004522 "Truncated packet or corrupt header length");
4523
Jingning Han24e0a182016-11-20 22:34:12 -08004524#if CONFIG_REF_MV
Dengca8d24d2016-10-17 14:06:35 +08004525 cm->setup_mi(cm);
4526#endif
4527
Yaowu Xuc27fc142016-08-22 16:08:15 -07004528 cm->use_prev_frame_mvs =
4529 !cm->error_resilient_mode && cm->width == cm->last_width &&
4530 cm->height == cm->last_height && !cm->last_intra_only &&
4531 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
4532#if CONFIG_EXT_REFS
4533 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
4534 // show_exisiting_frame=1, nor can it take a frame not used as
4535 // a reference, it is probable that by the time it is being
4536 // referred to, the frame buffer it originally points to may
4537 // already get expired and have been reassigned to the current
4538 // newly coded frame. Hence, we need to check whether this is
4539 // the case, and if yes, we have 2 choices:
4540 // (1) Simply disable the use of previous frame mvs; or
4541 // (2) Have cm->prev_frame point to one reference frame buffer,
4542 // e.g. LAST_FRAME.
4543 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
4544 // Reassign the LAST_FRAME buffer to cm->prev_frame.
4545 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4546 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4547 }
4548#endif // CONFIG_EXT_REFS
4549
Yaowu Xuf883b422016-08-30 14:01:10 -07004550 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004551
4552 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
4553 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07004554 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004555 "Uninitialized entropy context.");
4556
Yaowu Xuf883b422016-08-30 14:01:10 -07004557 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004558
4559 xd->corrupted = 0;
4560 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
4561 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07004562 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004563 "Decode failed. Frame data header is corrupted.");
4564
4565 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004566 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004567 }
4568
4569 // If encoded in frame parallel mode, frame context is ready after decoding
4570 // the frame header.
4571 if (cm->frame_parallel_decode &&
4572 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004573 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004574 FrameWorkerData *const frame_worker_data = worker->data1;
4575 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
4576 context_updated = 1;
4577 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4578 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004579 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004580 pbi->cur_buf->row = -1;
4581 pbi->cur_buf->col = -1;
4582 frame_worker_data->frame_context_ready = 1;
4583 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07004584 av1_frameworker_signal_stats(worker);
4585 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004586 }
4587
4588#if CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004589 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004590 cm->coef_probs_update_idx = 0;
4591#endif // CONFIG_ENTROPY
4592
4593 if (pbi->max_threads > 1
4594#if CONFIG_EXT_TILE
4595 && pbi->dec_tile_col < 0 // Decoding all columns
4596#endif // CONFIG_EXT_TILE
4597 && cm->tile_cols > 1) {
4598 // Multi-threaded tile decoder
4599 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
4600 if (!xd->corrupted) {
4601 if (!cm->skip_loop_filter) {
4602 // If multiple threads are used to decode tiles, then we use those
4603 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07004604 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
4605 0, 0, pbi->tile_workers, pbi->num_tile_workers,
4606 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004607 }
4608 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004609 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004610 "Decode failed. Frame data is corrupted.");
4611 }
4612 } else {
4613 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
4614 }
4615#if CONFIG_LOOP_RESTORATION
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08004616 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4617 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4618 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4619 av1_loop_restoration_frame(new_fb, cm, cm->rst_info, 7, 0, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004620 }
4621#endif // CONFIG_LOOP_RESTORATION
4622
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004623#if CONFIG_DERING
4624 if (cm->dering_level && !cm->skip_loop_filter) {
4625 av1_dering_frame(&pbi->cur_buf->buf, cm, &pbi->mb, cm->dering_level);
4626 }
4627#endif // CONFIG_DERING
4628
Thomas Daedef56859f2016-04-19 16:57:24 -07004629#if CONFIG_CLPF
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004630 if (!cm->skip_loop_filter) {
4631 const YV12_BUFFER_CONFIG *const frame = &pbi->cur_buf->buf;
4632 if (cm->clpf_strength_y) {
Yaowu Xud71be782016-10-14 08:47:03 -07004633 av1_clpf_frame(frame, NULL, cm, cm->clpf_size != CLPF_NOSIZE,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004634 cm->clpf_strength_y + (cm->clpf_strength_y == 3),
Yaowu Xud71be782016-10-14 08:47:03 -07004635 4 + cm->clpf_size, AOM_PLANE_Y, clpf_bit);
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004636 }
4637 if (cm->clpf_strength_u) {
Yaowu Xud71be782016-10-14 08:47:03 -07004638 av1_clpf_frame(frame, NULL, cm, 0, // No block signals for chroma
4639 cm->clpf_strength_u + (cm->clpf_strength_u == 3), 4,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004640 AOM_PLANE_U, NULL);
4641 }
4642 if (cm->clpf_strength_v) {
Yaowu Xud71be782016-10-14 08:47:03 -07004643 av1_clpf_frame(frame, NULL, cm, 0, // No block signals for chroma
4644 cm->clpf_strength_v + (cm->clpf_strength_v == 3), 4,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004645 AOM_PLANE_V, NULL);
4646 }
Steinar Midtskogend06588a2016-05-06 13:48:20 +02004647 }
4648 if (cm->clpf_blocks) aom_free(cm->clpf_blocks);
Thomas Daedef56859f2016-04-19 16:57:24 -07004649#endif
Thomas Daedef56859f2016-04-19 16:57:24 -07004650
Yaowu Xuc27fc142016-08-22 16:08:15 -07004651 if (!xd->corrupted) {
4652 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
4653#if CONFIG_ENTROPY
4654 cm->partial_prob_update = 0;
4655#endif // CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004656 av1_adapt_coef_probs(cm);
4657 av1_adapt_intra_frame_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004658
4659 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004660 av1_adapt_inter_frame_probs(cm);
4661 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004662 }
4663 } else {
4664 debug_check_frame_counts(cm);
4665 }
4666 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004667 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004668 "Decode failed. Frame data is corrupted.");
4669 }
4670
4671 // Non frame parallel update frame context here.
4672 if (!cm->error_resilient_mode && !context_updated)
4673 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4674}