blob: 9de00fc238db3da59ad0e2df5dbd6bec6dc74fe4 [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
Yaowu Xuf883b422016-08-30 14:01:10 -070057#define MAX_AV1_HEADER_SIZE 80
Michael Bebenita6048d052016-08-25 14:40:54 -070058#define ACCT_STR __func__
Yaowu Xuc27fc142016-08-22 16:08:15 -070059
Yushin Cho77bba8d2016-11-04 16:36:56 -070060#if CONFIG_PVQ
61#include "av1/decoder/pvq_decoder.h"
62#include "av1/encoder/encodemb.h"
63
64#include "aom_dsp/entdec.h"
65#include "av1/common/partition.h"
66#include "av1/decoder/decint.h"
67#include "av1/encoder/hybrid_fwd_txfm.h"
68#endif
69
Thomas Davies80188d12016-10-26 16:08:35 -070070static struct aom_read_bit_buffer *init_read_bit_buffer(
71 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
72 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]);
73static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
74 size_t partition_size);
75static size_t read_uncompressed_header(AV1Decoder *pbi,
76 struct aom_read_bit_buffer *rb);
77
Yaowu Xuf883b422016-08-30 14:01:10 -070078static int is_compound_reference_allowed(const AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070079 int i;
80 if (frame_is_intra_only(cm)) return 0;
81 for (i = 1; i < INTER_REFS_PER_FRAME; ++i)
82 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
83
84 return 0;
85}
86
Yaowu Xuf883b422016-08-30 14:01:10 -070087static void setup_compound_reference_mode(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070088#if CONFIG_EXT_REFS
89 cm->comp_fwd_ref[0] = LAST_FRAME;
90 cm->comp_fwd_ref[1] = LAST2_FRAME;
91 cm->comp_fwd_ref[2] = LAST3_FRAME;
92 cm->comp_fwd_ref[3] = GOLDEN_FRAME;
93
94 cm->comp_bwd_ref[0] = BWDREF_FRAME;
95 cm->comp_bwd_ref[1] = ALTREF_FRAME;
96#else
97 if (cm->ref_frame_sign_bias[LAST_FRAME] ==
98 cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
99 cm->comp_fixed_ref = ALTREF_FRAME;
100 cm->comp_var_ref[0] = LAST_FRAME;
101 cm->comp_var_ref[1] = GOLDEN_FRAME;
102 } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
103 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
104 cm->comp_fixed_ref = GOLDEN_FRAME;
105 cm->comp_var_ref[0] = LAST_FRAME;
106 cm->comp_var_ref[1] = ALTREF_FRAME;
107 } else {
108 cm->comp_fixed_ref = LAST_FRAME;
109 cm->comp_var_ref[0] = GOLDEN_FRAME;
110 cm->comp_var_ref[1] = ALTREF_FRAME;
111 }
112#endif // CONFIG_EXT_REFS
113}
114
115static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
116 return len != 0 && len <= (size_t)(end - start);
117}
118
Yaowu Xuf883b422016-08-30 14:01:10 -0700119static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
120 const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700121 return data > max ? max : data;
122}
123
Yaowu Xuf883b422016-08-30 14:01:10 -0700124static TX_MODE read_tx_mode(struct aom_read_bit_buffer *rb) {
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800125#if CONFIG_TX64X64
126 TX_MODE tx_mode =
127 aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
128 if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
129 return tx_mode;
130#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700131 return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800132#endif // CONFIG_TX64X64
Yaowu Xuc27fc142016-08-22 16:08:15 -0700133}
134
Yaowu Xuefc75352016-10-31 09:46:42 -0700135static void read_tx_size_probs(FRAME_CONTEXT *fc, aom_reader *r) {
136 int i, j, k;
137 for (i = 0; i < MAX_TX_DEPTH; ++i)
138 for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
139 for (k = 0; k < i + 1; ++k)
140 av1_diff_update_prob(r, &fc->tx_size_probs[i][j][k], ACCT_STR);
141}
142
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400143#if !CONFIG_EC_ADAPT
Yaowu Xuf883b422016-08-30 14:01:10 -0700144static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700145 int i, j;
Nathan E. Egge4947c292016-04-26 11:37:06 -0400146 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700147 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700148 av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i], ACCT_STR);
Nathan E. Egge4947c292016-04-26 11:37:06 -0400149 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700150}
Thomas Davies6519beb2016-10-19 14:46:07 +0100151#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700152
Yaowu Xuf883b422016-08-30 14:01:10 -0700153static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700154#if CONFIG_REF_MV
Yaowu Xu8af861b2016-11-01 12:12:11 -0700155 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700156 for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700157 av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700158 for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700159 av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700160 for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700161 av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700162 for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700163 av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700164#if CONFIG_EXT_INTER
Michael Bebenita6048d052016-08-25 14:40:54 -0700165 av1_diff_update_prob(r, &fc->new2mv_prob, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700166#endif // CONFIG_EXT_INTER
167#else
Nathan E. Eggebaaaa162016-10-24 09:50:52 -0400168#if !CONFIG_EC_ADAPT
Yaowu Xu8af861b2016-11-01 12:12:11 -0700169 int i, j;
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400170 for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700171 for (j = 0; j < INTER_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700172 av1_diff_update_prob(r, &fc->inter_mode_probs[i][j], ACCT_STR);
Nathan E. Egge6ec4d102016-09-08 10:41:20 -0400173 }
Yaowu Xu8af861b2016-11-01 12:12:11 -0700174#else
175 (void)fc;
176 (void)r;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700177#endif
Thomas Davies6519beb2016-10-19 14:46:07 +0100178#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700179}
180
181#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700182static void read_inter_compound_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700183 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -0700184 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700185 for (j = 0; j < INTER_MODE_CONTEXTS; ++j) {
186 for (i = 0; i < INTER_COMPOUND_MODES - 1; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700187 av1_diff_update_prob(r, &fc->inter_compound_mode_probs[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700188 }
189 }
190 }
191}
192#endif // CONFIG_EXT_INTER
Yaowu Xu17fd2f22016-11-17 18:23:28 -0800193#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100194#if !CONFIG_EXT_TX
195static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
196 int i, j, k;
197 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
198 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
199 for (j = 0; j < TX_TYPES; ++j) {
200 for (k = 0; k < TX_TYPES - 1; ++k)
201 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[i][j][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100202 }
203 }
204 }
205 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
206 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
207 for (k = 0; k < TX_TYPES - 1; ++k)
208 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[i][k], ACCT_STR);
Thomas9ac55082016-09-23 18:04:17 +0100209 }
210 }
211}
212#endif
213#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700214
215static REFERENCE_MODE read_frame_reference_mode(
Yaowu Xuf883b422016-08-30 14:01:10 -0700216 const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700217 if (is_compound_reference_allowed(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700218 return aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700219 ? REFERENCE_MODE_SELECT
Yaowu Xuf883b422016-08-30 14:01:10 -0700220 : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700221 } else {
222 return SINGLE_REFERENCE;
223 }
224}
225
Yaowu Xuf883b422016-08-30 14:01:10 -0700226static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700227 FRAME_CONTEXT *const fc = cm->fc;
228 int i, j;
229
230 if (cm->reference_mode == REFERENCE_MODE_SELECT)
231 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700232 av1_diff_update_prob(r, &fc->comp_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700233
234 if (cm->reference_mode != COMPOUND_REFERENCE) {
235 for (i = 0; i < REF_CONTEXTS; ++i) {
236 for (j = 0; j < (SINGLE_REFS - 1); ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700237 av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700238 }
239 }
240 }
241
242 if (cm->reference_mode != SINGLE_REFERENCE) {
243 for (i = 0; i < REF_CONTEXTS; ++i) {
244#if CONFIG_EXT_REFS
245 for (j = 0; j < (FWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700246 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700247 for (j = 0; j < (BWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700248 av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700249#else
250 for (j = 0; j < (COMP_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700251 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700252#endif // CONFIG_EXT_REFS
253 }
254 }
255}
256
Yaowu Xuf883b422016-08-30 14:01:10 -0700257static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700258 int i;
Michael Bebenita6048d052016-08-25 14:40:54 -0700259 for (i = 0; i < n; ++i) av1_diff_update_prob(r, &p[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700260}
261
Yaowu Xuf883b422016-08-30 14:01:10 -0700262static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Thomas9ac55082016-09-23 18:04:17 +0100263 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700264
Yaowu Xue86288d2016-10-31 15:56:38 -0700265#if !CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100266 int j;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700267 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
268
Yaowu Xuc27fc142016-08-22 16:08:15 -0700269 for (i = 0; i < 2; ++i) {
270 nmv_component *const comp_ctx = &ctx->comps[i];
271 update_mv_probs(&comp_ctx->sign, 1, r);
272 update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
273 update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
274 update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
275 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700276 for (i = 0; i < 2; ++i) {
277 nmv_component *const comp_ctx = &ctx->comps[i];
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400278 for (j = 0; j < CLASS0_SIZE; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700279 update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
Nathan E. Egge5f34b612016-09-08 15:59:53 -0400280 }
Nathan E. Eggeac499f32016-09-08 15:38:57 -0400281 update_mv_probs(comp_ctx->fp, MV_FP_SIZE - 1, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700282 }
Alex Converseaca9feb2016-10-10 11:08:10 -0700283#endif // !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700284
285 if (allow_hp) {
286 for (i = 0; i < 2; ++i) {
287 nmv_component *const comp_ctx = &ctx->comps[i];
288 update_mv_probs(&comp_ctx->class0_hp, 1, r);
289 update_mv_probs(&comp_ctx->hp, 1, r);
290 }
291 }
292}
293
294static void inverse_transform_block(MACROBLOCKD *xd, int plane,
295 const TX_TYPE tx_type,
296 const TX_SIZE tx_size, uint8_t *dst,
Jingning Han1be18782016-10-21 11:48:15 -0700297 int stride, int16_t scan_line, int eob) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700298 struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han1be18782016-10-21 11:48:15 -0700299 tran_low_t *const dqcoeff = pd->dqcoeff;
300 INV_TXFM_PARAM inv_txfm_param;
301 inv_txfm_param.tx_type = tx_type;
302 inv_txfm_param.tx_size = tx_size;
303 inv_txfm_param.eob = eob;
304 inv_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700305
Yaowu Xuf883b422016-08-30 14:01:10 -0700306#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han1be18782016-10-21 11:48:15 -0700307 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
308 inv_txfm_param.bd = xd->bd;
309 highbd_inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
310 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700311#endif // CONFIG_AOM_HIGHBITDEPTH
Jingning Han1be18782016-10-21 11:48:15 -0700312 inv_txfm_add(dqcoeff, dst, stride, &inv_txfm_param);
Yaowu Xuf883b422016-08-30 14:01:10 -0700313#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700314 }
Jingning Han1be18782016-10-21 11:48:15 -0700315#endif // CONFIG_AOM_HIGHBITDEPTH
316 memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700317}
318
Yushin Cho77bba8d2016-11-04 16:36:56 -0700319#if CONFIG_PVQ
320static int av1_pvq_decode_helper(od_dec_ctx *dec, int16_t *ref_coeff,
321 int16_t *dqcoeff, int16_t *quant, int pli,
322 int bs, TX_TYPE tx_type, int xdec,
323 int ac_dc_coded) {
324 unsigned int flags; // used for daala's stream analyzer.
325 int off;
326 const int is_keyframe = 0;
327 const int has_dc_skip = 1;
Debargha Mukherjee153e1f82016-11-17 09:59:14 -0800328 int quant_shift = get_tx_scale(bs);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700329 // DC quantizer for PVQ
330 int pvq_dc_quant;
331 int lossless = (quant[0] == 0);
332 const int blk_size = tx_size_wide[bs];
333 int eob = 0;
334 int i;
335 // TODO(yushin) : To enable activity masking,
336 // int use_activity_masking = dec->use_activity_masking;
337 int use_activity_masking = 0;
338
Yushin Cho48f84db2016-11-07 21:20:17 -0800339 DECLARE_ALIGNED(16, int16_t, dqcoeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
340 DECLARE_ALIGNED(16, int16_t, ref_coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700341
Yushin Cho48f84db2016-11-07 21:20:17 -0800342 od_coeff ref_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
343 od_coeff out_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700344
345 od_raster_to_coding_order(ref_coeff_pvq, blk_size, tx_type, ref_coeff,
346 blk_size);
347
348 if (lossless)
349 pvq_dc_quant = 1;
350 else {
351 // TODO(yushin): Enable this for activity masking,
352 // when pvq_qm_q4 is available in AOM.
353 // pvq_dc_quant = OD_MAXI(1, quant*
354 // dec->state.pvq_qm_q4[pli][od_qm_get_index(bs, 0)] >> 4);
355 pvq_dc_quant = OD_MAXI(1, quant[0] >> quant_shift);
356 }
357
358 off = od_qm_offset(bs, xdec);
359
360 // copy int16 inputs to int32
361 for (i = 0; i < blk_size * blk_size; i++) ref_int32[i] = ref_coeff_pvq[i];
362
363 od_pvq_decode(dec, ref_int32, out_int32, (int)quant[1] >> quant_shift, pli,
364 bs, OD_PVQ_BETA[use_activity_masking][pli][bs],
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800365 OD_ROBUST_STREAM, is_keyframe, &flags, ac_dc_coded,
366 dec->state.qm + off, dec->state.qm_inv + off);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700367
368 // copy int32 result back to int16
369 for (i = 0; i < blk_size * blk_size; i++) dqcoeff_pvq[i] = out_int32[i];
370
371 if (!has_dc_skip || dqcoeff_pvq[0]) {
372 dqcoeff_pvq[0] =
373 has_dc_skip + generic_decode(dec->ec, &dec->state.adapt.model_dc[pli],
374 -1, &dec->state.adapt.ex_dc[pli][bs][0], 2,
375 "dc:mag");
376 if (dqcoeff_pvq[0])
377 dqcoeff_pvq[0] *= od_ec_dec_bits(dec->ec, 1, "dc:sign") ? -1 : 1;
378 }
379 dqcoeff_pvq[0] = dqcoeff_pvq[0] * pvq_dc_quant + ref_coeff_pvq[0];
380
381 od_coding_order_to_raster(dqcoeff, blk_size, tx_type, dqcoeff_pvq, blk_size);
382
383 eob = blk_size * blk_size;
384
385 return eob;
386}
387
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800388static int av1_pvq_decode_helper2(MACROBLOCKD *const xd,
389 MB_MODE_INFO *const mbmi, int plane, int row,
390 int col, TX_SIZE tx_size, TX_TYPE tx_type) {
Yushin Cho77bba8d2016-11-04 16:36:56 -0700391 struct macroblockd_plane *const pd = &xd->plane[plane];
392 // transform block size in pixels
393 int tx_blk_size = tx_size_wide[tx_size];
394 int i, j;
395 tran_low_t *pvq_ref_coeff = pd->pvq_ref_coeff;
396 const int diff_stride = tx_blk_size;
397 int16_t *pred = pd->pred;
398 tran_low_t *const dqcoeff = pd->dqcoeff;
399 int ac_dc_coded; // bit0: DC coded, bit1 : AC coded
400 uint8_t *dst;
401 int eob;
402
403 eob = 0;
404 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
405
406 // decode ac/dc coded flag. bit0: DC coded, bit1 : AC coded
407 // NOTE : we don't use 5 symbols for luma here in aom codebase,
408 // since block partition is taken care of by aom.
409 // So, only AC/DC skip info is coded
410 ac_dc_coded = od_decode_cdf_adapt(
411 xd->daala_dec.ec,
412 xd->daala_dec.state.adapt.skip_cdf[2 * tx_size + (plane != 0)], 4,
413 xd->daala_dec.state.adapt.skip_increment, "skip");
414
415 if (ac_dc_coded) {
416 int xdec = pd->subsampling_x;
417 int seg_id = mbmi->segment_id;
418 int16_t *quant;
419 FWD_TXFM_PARAM fwd_txfm_param;
Yaowu Xufc1b2132016-11-07 15:16:15 -0800420 // ToDo(yaowu): correct this with optimal number from decoding process.
421 const int max_scan_line = tx_size_2d[tx_size];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700422
423 for (j = 0; j < tx_blk_size; j++)
424 for (i = 0; i < tx_blk_size; i++) {
425 pred[diff_stride * j + i] = dst[pd->dst.stride * j + i];
426 }
427
428 fwd_txfm_param.tx_type = tx_type;
429 fwd_txfm_param.tx_size = tx_size;
430 fwd_txfm_param.fwd_txfm_opt = FWD_TXFM_OPT_NORMAL;
431 fwd_txfm_param.rd_transform = 0;
432 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
Yaowu Xuc27fc142016-08-22 16:08:15 -0700474 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
475
476 if (mbmi->sb_type < BLOCK_8X8)
477 if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
478
Jingning Hanc4c99da2016-10-24 10:27:28 -0700479 av1_predict_intra_block(xd, pd->width, pd->height, tx_size, mode, dst,
Yaowu Xuf883b422016-08-30 14:01:10 -0700480 pd->dst.stride, dst, pd->dst.stride, col, row, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700481
482 if (!mbmi->skip) {
483 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700484#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700485 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 0);
Jingning Han1be18782016-10-21 11:48:15 -0700486 int16_t max_scan_line = 0;
487 const int eob =
488 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size,
489 tx_type, &max_scan_line, r, mbmi->segment_id);
Angie Chianged8cd9a2016-10-21 16:44:47 -0700490#if CONFIG_ADAPT_SCAN
491 av1_update_scan_count_facade(cm, tx_size, tx_type, pd->dqcoeff, eob);
492#endif
Jingning Han1be18782016-10-21 11:48:15 -0700493 if (eob)
494 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
495 max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700496#else
497 av1_pvq_decode_helper2(xd, mbmi, plane, row, col, tx_size, tx_type);
498#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700499 }
500}
501
502#if CONFIG_VAR_TX
Angie Chiangff6d8902016-10-21 11:02:09 -0700503static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
504 aom_reader *r, MB_MODE_INFO *const mbmi,
Jingning Han8fd62b72016-10-21 12:55:54 -0700505 int plane, BLOCK_SIZE plane_bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700506 int blk_row, int blk_col, TX_SIZE tx_size,
507 int *eob_total) {
508 const struct macroblockd_plane *const pd = &xd->plane[plane];
509 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
510 const int tx_row = blk_row >> (1 - pd->subsampling_y);
511 const int tx_col = blk_col >> (1 - pd->subsampling_x);
512 const TX_SIZE plane_tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700513 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700514 : mbmi->inter_tx_size[tx_row][tx_col];
Jingning Han5f614262016-10-27 14:27:43 -0700515 // Scale to match transform block unit.
Jingning Hanf64062f2016-11-02 16:22:18 -0700516 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
517 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700518
519 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
520
521 if (tx_size == plane_tx_size) {
522 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
Jingning Han8fd62b72016-10-21 12:55:54 -0700523 int block_idx = (blk_row << 1) + blk_col;
524 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
Angie Chiangff6d8902016-10-21 11:02:09 -0700525 const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700526 int16_t max_scan_line = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700527 const int eob =
Yaowu Xuf883b422016-08-30 14:01:10 -0700528 av1_decode_block_tokens(xd, plane, sc, blk_col, blk_row, plane_tx_size,
Jingning Han1be18782016-10-21 11:48:15 -0700529 tx_type, &max_scan_line, r, mbmi->segment_id);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700530 inverse_transform_block(
531 xd, plane, tx_type, plane_tx_size,
532 &pd->dst.buf[4 * blk_row * pd->dst.stride + 4 * blk_col],
Jingning Han1be18782016-10-21 11:48:15 -0700533 pd->dst.stride, max_scan_line, eob);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700534 *eob_total += eob;
535 } else {
Jingning Hanf64062f2016-11-02 16:22:18 -0700536 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
537 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700538 int i;
539
540 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700541
542 for (i = 0; i < 4; ++i) {
Jingning Han5f614262016-10-27 14:27:43 -0700543 const int offsetr = blk_row + (i >> 1) * bsl;
544 const int offsetc = blk_col + (i & 0x01) * bsl;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700545
546 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
547
Jingning Han8fd62b72016-10-21 12:55:54 -0700548 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
Jingning Hanf64062f2016-11-02 16:22:18 -0700549 offsetc, sub_txs, eob_total);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700550 }
551 }
552}
553#endif // CONFIG_VAR_TX
554
555#if !CONFIG_VAR_TX || CONFIG_SUPERTX || (CONFIG_EXT_TX && CONFIG_RECT_TX)
Angie Chiangff6d8902016-10-21 11:02:09 -0700556static int reconstruct_inter_block(AV1_COMMON *cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700557#if CONFIG_ANS
558 struct AnsDecoder *const r,
559#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700560 aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700561#endif
562 int segment_id, int plane, int row, int col,
563 TX_SIZE tx_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700564 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
565 int block_idx = (row << 1) + col;
566 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700567#if CONFIG_PVQ
568 int eob;
569 (void)cm;
570 (void)r;
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800571 (void)segment_id;
572#else
573 struct macroblockd_plane *const pd = &xd->plane[plane];
Yushin Cho77bba8d2016-11-04 16:36:56 -0700574#endif
575
576#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -0700577 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 1);
Jingning Han1be18782016-10-21 11:48:15 -0700578 int16_t max_scan_line = 0;
579 const int eob =
580 av1_decode_block_tokens(xd, plane, scan_order, col, row, tx_size, tx_type,
581 &max_scan_line, r, segment_id);
Angie Chianged8cd9a2016-10-21 16:44:47 -0700582#if CONFIG_ADAPT_SCAN
583 av1_update_scan_count_facade(cm, tx_size, tx_type, pd->dqcoeff, eob);
584#endif
Jingning Han1be18782016-10-21 11:48:15 -0700585 if (eob)
586 inverse_transform_block(xd, plane, tx_type, tx_size,
587 &pd->dst.buf[4 * row * pd->dst.stride + 4 * col],
588 pd->dst.stride, max_scan_line, eob);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700589#else
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800590 eob = av1_pvq_decode_helper2(xd, &xd->mi[0]->mbmi, plane, row, col, tx_size,
591 tx_type);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700592#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700593 return eob;
594}
595#endif // !CONFIG_VAR_TX || CONFIG_SUPER_TX
596
Yaowu Xuc27fc142016-08-22 16:08:15 -0700597static INLINE void dec_reset_skip_context(MACROBLOCKD *xd) {
598 int i;
599 for (i = 0; i < MAX_MB_PLANE; i++) {
600 struct macroblockd_plane *const pd = &xd->plane[i];
601 memset(pd->above_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_w);
602 memset(pd->left_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_h);
603 }
604}
605
Yaowu Xuf883b422016-08-30 14:01:10 -0700606static MB_MODE_INFO *set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700607 BLOCK_SIZE bsize, int mi_row, int mi_col,
608 int bw, int bh, int x_mis, int y_mis, int bwl,
609 int bhl) {
610 const int offset = mi_row * cm->mi_stride + mi_col;
611 int x, y;
612 const TileInfo *const tile = &xd->tile;
613
614 xd->mi = cm->mi_grid_visible + offset;
615 xd->mi[0] = &cm->mi[offset];
616 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
617 // passing bsize from decode_partition().
618 xd->mi[0]->mbmi.sb_type = bsize;
Angie Chiang394c3372016-11-03 11:13:15 -0700619#if CONFIG_RD_DEBUG
620 xd->mi[0]->mbmi.mi_row = mi_row;
621 xd->mi[0]->mbmi.mi_col = mi_col;
622#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700623 for (y = 0; y < y_mis; ++y)
Jingning Han97d85482016-07-15 11:06:05 -0700624 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700625
626 set_plane_n4(xd, bw, bh, bwl, bhl);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700627 set_skip_context(xd, mi_row, mi_col);
628
629#if CONFIG_VAR_TX
630 xd->max_tx_size = max_txsize_lookup[bsize];
631#endif
632
633 // Distance of Mb to the various image edges. These are specified to 8th pel
634 // as they are always compared to values that are in 1/8th pel units
635 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
636
Yaowu Xuf883b422016-08-30 14:01:10 -0700637 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700638 return &xd->mi[0]->mbmi;
639}
640
641#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700642static MB_MODE_INFO *set_offsets_extend(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700643 MACROBLOCKD *const xd,
644 const TileInfo *const tile,
645 BLOCK_SIZE bsize_pred, int mi_row_pred,
646 int mi_col_pred, int mi_row_ori,
647 int mi_col_ori) {
648 // Used in supertx
649 // (mi_row_ori, mi_col_ori): location for mv
650 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
651 const int bw = num_8x8_blocks_wide_lookup[bsize_pred];
652 const int bh = num_8x8_blocks_high_lookup[bsize_pred];
653 const int offset = mi_row_ori * cm->mi_stride + mi_col_ori;
654 const int bwl = b_width_log2_lookup[bsize_pred];
655 const int bhl = b_height_log2_lookup[bsize_pred];
656 xd->mi = cm->mi_grid_visible + offset;
657 xd->mi[0] = cm->mi + offset;
658 set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw, cm->mi_rows,
659 cm->mi_cols);
660
661 xd->up_available = (mi_row_ori > tile->mi_row_start);
662 xd->left_available = (mi_col_ori > tile->mi_col_start);
663
664 set_plane_n4(xd, bw, bh, bwl, bhl);
665
666 return &xd->mi[0]->mbmi;
667}
668
Yaowu Xuf883b422016-08-30 14:01:10 -0700669static MB_MODE_INFO *set_mb_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
670 BLOCK_SIZE bsize, int mi_row, int mi_col,
671 int bw, int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700672 const int offset = mi_row * cm->mi_stride + mi_col;
673 const TileInfo *const tile = &xd->tile;
674 int x, y;
675
676 xd->mi = cm->mi_grid_visible + offset;
677 xd->mi[0] = cm->mi + offset;
678 xd->mi[0]->mbmi.sb_type = bsize;
679 for (y = 0; y < y_mis; ++y)
680 for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
681
682 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
683 return &xd->mi[0]->mbmi;
684}
685
Yaowu Xuf883b422016-08-30 14:01:10 -0700686static void set_offsets_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700687 const TileInfo *const tile, BLOCK_SIZE bsize,
688 int mi_row, int mi_col) {
689 const int bw = num_8x8_blocks_wide_lookup[bsize];
690 const int bh = num_8x8_blocks_high_lookup[bsize];
691 const int offset = mi_row * cm->mi_stride + mi_col;
692 const int bwl = b_width_log2_lookup[bsize];
693 const int bhl = b_height_log2_lookup[bsize];
694
695 xd->mi = cm->mi_grid_visible + offset;
696 xd->mi[0] = cm->mi + offset;
697
698 set_plane_n4(xd, bw, bh, bwl, bhl);
699
700 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
701
Yaowu Xuf883b422016-08-30 14:01:10 -0700702 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700703}
704
Yaowu Xuf883b422016-08-30 14:01:10 -0700705static void set_param_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700706 BLOCK_SIZE bsize, int mi_row, int mi_col,
707 int txfm, int skip) {
708 const int bw = num_8x8_blocks_wide_lookup[bsize];
709 const int bh = num_8x8_blocks_high_lookup[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -0700710 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
711 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700712 const int offset = mi_row * cm->mi_stride + mi_col;
713 int x, y;
714
715 xd->mi = cm->mi_grid_visible + offset;
716 xd->mi[0] = cm->mi + offset;
717
718 for (y = 0; y < y_mis; ++y)
719 for (x = 0; x < x_mis; ++x) {
720 xd->mi[y * cm->mi_stride + x]->mbmi.skip = skip;
721 xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
722 }
723#if CONFIG_VAR_TX
724 xd->above_txfm_context = cm->above_txfm_context + mi_col;
725 xd->left_txfm_context =
726 xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
Yaowu Xu52a17632016-11-17 15:48:21 -0800727 set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bw, bh, skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700728#endif
729}
730
Yaowu Xuf883b422016-08-30 14:01:10 -0700731static void set_ref(AV1_COMMON *const cm, MACROBLOCKD *const xd, int idx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700732 int mi_row, int mi_col) {
733 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
734 RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
735 xd->block_refs[idx] = ref_buffer;
Yaowu Xuf883b422016-08-30 14:01:10 -0700736 if (!av1_is_valid_scale(&ref_buffer->sf))
737 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700738 "Invalid scale factors");
Yaowu Xuf883b422016-08-30 14:01:10 -0700739 av1_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
740 &ref_buffer->sf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700741 xd->corrupted |= ref_buffer->buf->corrupted;
742}
743
744static void dec_predict_b_extend(
Yaowu Xuf883b422016-08-30 14:01:10 -0700745 AV1Decoder *const pbi, MACROBLOCKD *const xd, const TileInfo *const tile,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700746 int block, int mi_row_ori, int mi_col_ori, int mi_row_pred, int mi_col_pred,
747 int mi_row_top, int mi_col_top, uint8_t *dst_buf[3], int dst_stride[3],
748 BLOCK_SIZE bsize_top, BLOCK_SIZE bsize_pred, int b_sub8x8, int bextend) {
749 // Used in supertx
750 // (mi_row_ori, mi_col_ori): location for mv
751 // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
752 // (mi_row_top, mi_col_top, bsize_top): region of the top partition size
753 // block: sub location of sub8x8 blocks
754 // b_sub8x8: 1: ori is sub8x8; 0: ori is not sub8x8
755 // bextend: 1: region to predict is an extension of ori; 0: not
756 int r = (mi_row_pred - mi_row_top) * MI_SIZE;
757 int c = (mi_col_pred - mi_col_top) * MI_SIZE;
758 const int mi_width_top = num_8x8_blocks_wide_lookup[bsize_top];
759 const int mi_height_top = num_8x8_blocks_high_lookup[bsize_top];
760 MB_MODE_INFO *mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700761 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700762
763 if (mi_row_pred < mi_row_top || mi_col_pred < mi_col_top ||
764 mi_row_pred >= mi_row_top + mi_height_top ||
765 mi_col_pred >= mi_col_top + mi_width_top || mi_row_pred >= cm->mi_rows ||
766 mi_col_pred >= cm->mi_cols)
767 return;
768
769 mbmi = set_offsets_extend(cm, xd, tile, bsize_pred, mi_row_pred, mi_col_pred,
770 mi_row_ori, mi_col_ori);
771 set_ref(cm, xd, 0, mi_row_pred, mi_col_pred);
772 if (has_second_ref(&xd->mi[0]->mbmi))
773 set_ref(cm, xd, 1, mi_row_pred, mi_col_pred);
774
775 if (!bextend) {
776 mbmi->tx_size = b_width_log2_lookup[bsize_top];
777 }
778
779 xd->plane[0].dst.stride = dst_stride[0];
780 xd->plane[1].dst.stride = dst_stride[1];
781 xd->plane[2].dst.stride = dst_stride[2];
782 xd->plane[0].dst.buf = dst_buf[0] +
783 (r >> xd->plane[0].subsampling_y) * dst_stride[0] +
784 (c >> xd->plane[0].subsampling_x);
785 xd->plane[1].dst.buf = dst_buf[1] +
786 (r >> xd->plane[1].subsampling_y) * dst_stride[1] +
787 (c >> xd->plane[1].subsampling_x);
788 xd->plane[2].dst.buf = dst_buf[2] +
789 (r >> xd->plane[2].subsampling_y) * dst_stride[2] +
790 (c >> xd->plane[2].subsampling_x);
791
792 if (!b_sub8x8)
Yaowu Xuf883b422016-08-30 14:01:10 -0700793 av1_build_inter_predictors_sb_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700794#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700795 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700796#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700797 mi_row_pred, mi_col_pred, bsize_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700798 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700799 av1_build_inter_predictors_sb_sub8x8_extend(xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700801 mi_row_ori, mi_col_ori,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700802#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700803 mi_row_pred, mi_col_pred,
804 bsize_pred, block);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700805}
806
Yaowu Xuf883b422016-08-30 14:01:10 -0700807static void dec_extend_dir(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700808 const TileInfo *const tile, int block,
809 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
810 int mi_col, int mi_row_top, int mi_col_top,
811 uint8_t *dst_buf[3], int dst_stride[3], int dir) {
812 // dir: 0-lower, 1-upper, 2-left, 3-right
813 // 4-lowerleft, 5-upperleft, 6-lowerright, 7-upperright
814 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
815 const int mi_height = num_8x8_blocks_high_lookup[bsize];
816 int xss = xd->plane[1].subsampling_x;
817 int yss = xd->plane[1].subsampling_y;
818 int b_sub8x8 = (bsize < BLOCK_8X8) ? 1 : 0;
819 BLOCK_SIZE extend_bsize;
820 int unit, mi_row_pred, mi_col_pred;
821
822 if (dir == 0 || dir == 1) {
823 extend_bsize = (mi_width == 1 || bsize < BLOCK_8X8 || xss < yss)
824 ? BLOCK_8X8
825 : BLOCK_16X8;
826 unit = num_8x8_blocks_wide_lookup[extend_bsize];
827 mi_row_pred = mi_row + ((dir == 0) ? mi_height : -1);
828 mi_col_pred = mi_col;
829
830 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
831 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
832 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
833
834 if (mi_width > unit) {
835 int i;
836 assert(!b_sub8x8);
837 for (i = 0; i < mi_width / unit - 1; i++) {
838 mi_col_pred += unit;
839 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
840 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
841 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
842 }
843 }
844 } else if (dir == 2 || dir == 3) {
845 extend_bsize = (mi_height == 1 || bsize < BLOCK_8X8 || yss < xss)
846 ? BLOCK_8X8
847 : BLOCK_8X16;
848 unit = num_8x8_blocks_high_lookup[extend_bsize];
849 mi_row_pred = mi_row;
850 mi_col_pred = mi_col + ((dir == 3) ? mi_width : -1);
851
852 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
853 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
854 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
855
856 if (mi_height > unit) {
857 int i;
858 for (i = 0; i < mi_height / unit - 1; i++) {
859 mi_row_pred += unit;
860 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
861 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
862 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
863 }
864 }
865 } else {
866 extend_bsize = BLOCK_8X8;
867 mi_row_pred = mi_row + ((dir == 4 || dir == 6) ? mi_height : -1);
868 mi_col_pred = mi_col + ((dir == 6 || dir == 7) ? mi_width : -1);
869 dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col, mi_row_pred,
870 mi_col_pred, mi_row_top, mi_col_top, dst_buf,
871 dst_stride, top_bsize, extend_bsize, b_sub8x8, 1);
872 }
873}
874
Yaowu Xuf883b422016-08-30 14:01:10 -0700875static void dec_extend_all(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700876 const TileInfo *const tile, int block,
877 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
878 int mi_col, int mi_row_top, int mi_col_top,
879 uint8_t *dst_buf[3], int dst_stride[3]) {
880 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
881 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
882 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
883 mi_row_top, mi_col_top, dst_buf, dst_stride, 1);
884 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
885 mi_row_top, mi_col_top, dst_buf, dst_stride, 2);
886 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
887 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
888 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
889 mi_row_top, mi_col_top, dst_buf, dst_stride, 4);
890 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
891 mi_row_top, mi_col_top, dst_buf, dst_stride, 5);
892 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
893 mi_row_top, mi_col_top, dst_buf, dst_stride, 6);
894 dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
895 mi_row_top, mi_col_top, dst_buf, dst_stride, 7);
896}
897
Yaowu Xuf883b422016-08-30 14:01:10 -0700898static void dec_predict_sb_complex(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700899 const TileInfo *const tile, int mi_row,
900 int mi_col, int mi_row_top, int mi_col_top,
901 BLOCK_SIZE bsize, BLOCK_SIZE top_bsize,
902 uint8_t *dst_buf[3], int dst_stride[3]) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700903 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700904 const int hbs = num_8x8_blocks_wide_lookup[bsize] / 2;
905 const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
906 const BLOCK_SIZE subsize = get_subsize(bsize, partition);
907#if CONFIG_EXT_PARTITION_TYPES
908 const BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
909#endif
910 int i;
911 const int mi_offset = mi_row * cm->mi_stride + mi_col;
912 uint8_t *dst_buf1[3], *dst_buf2[3], *dst_buf3[3];
913
914 DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
915 DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
916 DECLARE_ALIGNED(16, uint8_t, tmp_buf3[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
917 int dst_stride1[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
918 int dst_stride2[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
919 int dst_stride3[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
920
Yaowu Xuf883b422016-08-30 14:01:10 -0700921#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700922 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
923 int len = sizeof(uint16_t);
924 dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
925 dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_TX_SQUARE * len);
926 dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_TX_SQUARE * len);
927 dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
928 dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_TX_SQUARE * len);
929 dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_TX_SQUARE * len);
930 dst_buf3[0] = CONVERT_TO_BYTEPTR(tmp_buf3);
931 dst_buf3[1] = CONVERT_TO_BYTEPTR(tmp_buf3 + MAX_TX_SQUARE * len);
932 dst_buf3[2] = CONVERT_TO_BYTEPTR(tmp_buf3 + 2 * MAX_TX_SQUARE * len);
933 } else {
934#endif
935 dst_buf1[0] = tmp_buf1;
936 dst_buf1[1] = tmp_buf1 + MAX_TX_SQUARE;
937 dst_buf1[2] = tmp_buf1 + 2 * MAX_TX_SQUARE;
938 dst_buf2[0] = tmp_buf2;
939 dst_buf2[1] = tmp_buf2 + MAX_TX_SQUARE;
940 dst_buf2[2] = tmp_buf2 + 2 * MAX_TX_SQUARE;
941 dst_buf3[0] = tmp_buf3;
942 dst_buf3[1] = tmp_buf3 + MAX_TX_SQUARE;
943 dst_buf3[2] = tmp_buf3 + 2 * MAX_TX_SQUARE;
Yaowu Xuf883b422016-08-30 14:01:10 -0700944#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700945 }
946#endif
947
948 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
949
950 xd->mi = cm->mi_grid_visible + mi_offset;
951 xd->mi[0] = cm->mi + mi_offset;
952
953 for (i = 0; i < MAX_MB_PLANE; i++) {
954 xd->plane[i].dst.buf = dst_buf[i];
955 xd->plane[i].dst.stride = dst_stride[i];
956 }
957
958 switch (partition) {
959 case PARTITION_NONE:
960 assert(bsize < top_bsize);
961 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
962 mi_row_top, mi_col_top, dst_buf, dst_stride,
963 top_bsize, bsize, 0, 0);
964 dec_extend_all(pbi, xd, tile, 0, bsize, top_bsize, mi_row, mi_col,
965 mi_row_top, mi_col_top, dst_buf, dst_stride);
966 break;
967 case PARTITION_HORZ:
968 if (bsize == BLOCK_8X8) {
969 // For sub8x8, predict in 8x8 unit
970 // First half
971 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
972 mi_row_top, mi_col_top, dst_buf, dst_stride,
973 top_bsize, BLOCK_8X8, 1, 0);
974 if (bsize < top_bsize)
975 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
976 mi_row_top, mi_col_top, dst_buf, dst_stride);
977
978 // Second half
979 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
980 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
981 top_bsize, BLOCK_8X8, 1, 1);
982 if (bsize < top_bsize)
983 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
984 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
985
986 // weighted average to smooth the boundary
987 xd->plane[0].dst.buf = dst_buf[0];
988 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -0700989 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700990 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
991 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
992 0);
993 } else {
994 // First half
995 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
996 mi_row_top, mi_col_top, dst_buf, dst_stride,
997 top_bsize, subsize, 0, 0);
998 if (bsize < top_bsize)
999 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1000 mi_row_top, mi_col_top, dst_buf, dst_stride);
1001 else
1002 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1003 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1004
1005 if (mi_row + hbs < cm->mi_rows) {
1006 // Second half
1007 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col,
1008 mi_row + hbs, mi_col, mi_row_top, mi_col_top,
1009 dst_buf1, dst_stride1, top_bsize, subsize, 0, 0);
1010 if (bsize < top_bsize)
1011 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1012 mi_col, mi_row_top, mi_col_top, dst_buf1,
1013 dst_stride1);
1014 else
1015 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1016 mi_col, mi_row_top, mi_col_top, dst_buf1,
1017 dst_stride1, 1);
1018
1019 // weighted average to smooth the boundary
1020 for (i = 0; i < MAX_MB_PLANE; i++) {
1021 xd->plane[i].dst.buf = dst_buf[i];
1022 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001023 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001024 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1025 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1026 PARTITION_HORZ, i);
1027 }
1028 }
1029 }
1030 break;
1031 case PARTITION_VERT:
1032 if (bsize == BLOCK_8X8) {
1033 // First half
1034 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1035 mi_row_top, mi_col_top, dst_buf, dst_stride,
1036 top_bsize, BLOCK_8X8, 1, 0);
1037 if (bsize < top_bsize)
1038 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1039 mi_row_top, mi_col_top, dst_buf, dst_stride);
1040
1041 // Second half
1042 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1043 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1044 top_bsize, BLOCK_8X8, 1, 1);
1045 if (bsize < top_bsize)
1046 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1047 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1048
1049 // Smooth
1050 xd->plane[0].dst.buf = dst_buf[0];
1051 xd->plane[0].dst.stride = dst_stride[0];
Yaowu Xuf883b422016-08-30 14:01:10 -07001052 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001053 xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
1054 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1055 0);
1056 } else {
1057 // First half
1058 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1059 mi_row_top, mi_col_top, dst_buf, dst_stride,
1060 top_bsize, subsize, 0, 0);
1061 if (bsize < top_bsize)
1062 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1063 mi_row_top, mi_col_top, dst_buf, dst_stride);
1064 else
1065 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1066 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1067
1068 // Second half
1069 if (mi_col + hbs < cm->mi_cols) {
1070 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1071 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1072 dst_stride1, top_bsize, subsize, 0, 0);
1073 if (bsize < top_bsize)
1074 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1075 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1076 dst_stride1);
1077 else
1078 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1079 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1080 dst_stride1, 2);
1081
1082 // Smooth
1083 for (i = 0; i < MAX_MB_PLANE; i++) {
1084 xd->plane[i].dst.buf = dst_buf[i];
1085 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001086 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001087 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1088 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1089 PARTITION_VERT, i);
1090 }
1091 }
1092 }
1093 break;
1094 case PARTITION_SPLIT:
1095 if (bsize == BLOCK_8X8) {
1096 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1097 mi_row_top, mi_col_top, dst_buf, dst_stride,
1098 top_bsize, BLOCK_8X8, 1, 0);
1099 dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
1100 mi_row_top, mi_col_top, dst_buf1, dst_stride1,
1101 top_bsize, BLOCK_8X8, 1, 1);
1102 dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
1103 mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1104 top_bsize, BLOCK_8X8, 1, 1);
1105 dec_predict_b_extend(pbi, xd, tile, 3, mi_row, mi_col, mi_row, mi_col,
1106 mi_row_top, mi_col_top, dst_buf3, dst_stride3,
1107 top_bsize, BLOCK_8X8, 1, 1);
1108 if (bsize < top_bsize) {
1109 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1110 mi_row_top, mi_col_top, dst_buf, dst_stride);
1111 dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
1112 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1113 dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
1114 mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1115 dec_extend_all(pbi, xd, tile, 3, subsize, top_bsize, mi_row, mi_col,
1116 mi_row_top, mi_col_top, dst_buf3, dst_stride3);
1117 }
1118 } else {
1119 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row_top,
1120 mi_col_top, subsize, top_bsize, dst_buf,
1121 dst_stride);
1122 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1123 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col + hbs,
1124 mi_row_top, mi_col_top, subsize, top_bsize,
1125 dst_buf1, dst_stride1);
1126 if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols)
1127 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col,
1128 mi_row_top, mi_col_top, subsize, top_bsize,
1129 dst_buf2, dst_stride2);
1130 if (mi_row + hbs < cm->mi_rows && mi_col + hbs < cm->mi_cols)
1131 dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col + hbs,
1132 mi_row_top, mi_col_top, subsize, top_bsize,
1133 dst_buf3, dst_stride3);
1134 }
1135 for (i = 0; i < MAX_MB_PLANE; i++) {
1136 if (bsize == BLOCK_8X8 && i != 0)
1137 continue; // Skip <4x4 chroma smoothing
1138 if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001139 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001140 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
1141 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1142 PARTITION_VERT, i);
1143 if (mi_row + hbs < cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001144 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001145 xd, dst_buf2[i], dst_stride2[i], dst_buf3[i], dst_stride3[i],
1146 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1147 PARTITION_VERT, i);
Yaowu Xuf883b422016-08-30 14:01:10 -07001148 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001149 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1150 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1151 PARTITION_HORZ, i);
1152 }
1153 } else if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001154 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001155 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
1156 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1157 PARTITION_HORZ, i);
1158 }
1159 }
1160 break;
1161#if CONFIG_EXT_PARTITION_TYPES
1162 case PARTITION_HORZ_A:
1163 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1164 mi_row_top, mi_col_top, dst_buf, dst_stride,
1165 top_bsize, bsize2, 0, 0);
1166 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1167 mi_row_top, mi_col_top, dst_buf, dst_stride);
1168
1169 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1170 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1171 dst_stride1, top_bsize, bsize2, 0, 0);
1172 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1173 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1174
1175 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1176 mi_col, mi_row_top, mi_col_top, dst_buf2,
1177 dst_stride2, top_bsize, subsize, 0, 0);
1178 if (bsize < top_bsize)
1179 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1180 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2);
1181 else
1182 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
1183 mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2,
1184 1);
1185
1186 for (i = 0; i < MAX_MB_PLANE; i++) {
1187 xd->plane[i].dst.buf = dst_buf[i];
1188 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001189 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001190 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1191 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1192 i);
1193 }
1194 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001195 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001196 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1197 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1198 i);
1199 }
1200 break;
1201 case PARTITION_VERT_A:
1202
1203 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1204 mi_row_top, mi_col_top, dst_buf, dst_stride,
1205 top_bsize, bsize2, 0, 0);
1206 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
1207 mi_row_top, mi_col_top, dst_buf, dst_stride);
1208
1209 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1210 mi_col, mi_row_top, mi_col_top, dst_buf1,
1211 dst_stride1, top_bsize, bsize2, 0, 0);
1212 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1213 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1214
1215 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1216 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1217 dst_stride2, top_bsize, subsize, 0, 0);
1218 if (bsize < top_bsize)
1219 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1220 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1221 dst_stride2);
1222 else
1223 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
1224 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1225 dst_stride2, 2);
1226
1227 for (i = 0; i < MAX_MB_PLANE; i++) {
1228 xd->plane[i].dst.buf = dst_buf[i];
1229 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001230 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001231 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1232 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1233 i);
1234 }
1235 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001236 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001237 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
1238 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1239 i);
1240 }
1241 break;
1242 case PARTITION_HORZ_B:
1243 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1244 mi_row_top, mi_col_top, dst_buf, dst_stride,
1245 top_bsize, subsize, 0, 0);
1246 if (bsize < top_bsize)
1247 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1248 mi_row_top, mi_col_top, dst_buf, dst_stride);
1249 else
1250 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1251 mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
1252
1253 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
1254 mi_col, mi_row_top, mi_col_top, dst_buf1,
1255 dst_stride1, top_bsize, bsize2, 0, 0);
1256 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
1257 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1258
1259 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1260 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1261 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1262 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1263 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1264 dst_stride2);
1265
1266 for (i = 0; i < MAX_MB_PLANE; i++) {
1267 xd->plane[i].dst.buf = dst_buf1[i];
1268 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001269 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001270 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1271 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1272 PARTITION_VERT, i);
1273 }
1274 for (i = 0; i < MAX_MB_PLANE; i++) {
1275 xd->plane[i].dst.buf = dst_buf[i];
1276 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001277 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001278 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1279 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
1280 i);
1281 }
1282 break;
1283 case PARTITION_VERT_B:
1284 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
1285 mi_row_top, mi_col_top, dst_buf, dst_stride,
1286 top_bsize, subsize, 0, 0);
1287 if (bsize < top_bsize)
1288 dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1289 mi_row_top, mi_col_top, dst_buf, dst_stride);
1290 else
1291 dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
1292 mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
1293
1294 dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
1295 mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
1296 dst_stride1, top_bsize, bsize2, 0, 0);
1297 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
1298 mi_row_top, mi_col_top, dst_buf1, dst_stride1);
1299
1300 dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
1301 mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
1302 dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
1303 dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
1304 mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
1305 dst_stride2);
1306
1307 for (i = 0; i < MAX_MB_PLANE; i++) {
1308 xd->plane[i].dst.buf = dst_buf1[i];
1309 xd->plane[i].dst.stride = dst_stride1[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001310 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001311 xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
1312 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
1313 PARTITION_HORZ, i);
1314 }
1315 for (i = 0; i < MAX_MB_PLANE; i++) {
1316 xd->plane[i].dst.buf = dst_buf[i];
1317 xd->plane[i].dst.stride = dst_stride[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07001318 av1_build_masked_inter_predictor_complex(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001319 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
1320 mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
1321 i);
1322 }
1323 break;
1324#endif // CONFIG_EXT_PARTITION_TYPES
1325 default: assert(0);
1326 }
1327}
1328
Yaowu Xuf883b422016-08-30 14:01:10 -07001329static void set_segment_id_supertx(const AV1_COMMON *const cm, const int mi_row,
1330 const int mi_col, const BLOCK_SIZE bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001331 const struct segmentation *seg = &cm->seg;
1332 const int miw =
Yaowu Xuf883b422016-08-30 14:01:10 -07001333 AOMMIN(num_8x8_blocks_wide_lookup[bsize], cm->mi_cols - mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001334 const int mih =
Yaowu Xuf883b422016-08-30 14:01:10 -07001335 AOMMIN(num_8x8_blocks_high_lookup[bsize], cm->mi_rows - mi_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001336 const int mi_offset = mi_row * cm->mi_stride + mi_col;
1337 MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
1338 int r, c;
1339 int seg_id_supertx = MAX_SEGMENTS;
1340
1341 if (!seg->enabled) {
1342 seg_id_supertx = 0;
1343 } else {
1344 // Find the minimum segment_id
1345 for (r = 0; r < mih; r++)
1346 for (c = 0; c < miw; c++)
1347 seg_id_supertx =
Yaowu Xuf883b422016-08-30 14:01:10 -07001348 AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001349 assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);
1350 }
1351
1352 // Assign the the segment_id back to segment_id_supertx
1353 for (r = 0; r < mih; r++)
1354 for (c = 0; c < miw; c++)
1355 mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
1356}
1357#endif // CONFIG_SUPERTX
1358
Yaowu Xuf883b422016-08-30 14:01:10 -07001359static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001360#if CONFIG_SUPERTX
1361 int supertx_enabled,
1362#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001363 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001364#if CONFIG_EXT_PARTITION_TYPES
1365 PARTITION_TYPE partition,
1366#endif // CONFIG_EXT_PARTITION_TYPES
1367 BLOCK_SIZE bsize, int bwl, int bhl) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001368 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001369 const int bw = 1 << (bwl - 1);
1370 const int bh = 1 << (bhl - 1);
Yaowu Xuf883b422016-08-30 14:01:10 -07001371 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
1372 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Nathan E. Eggeebbd4792016-10-05 19:30:15 -04001373 MB_MODE_INFO *mbmi;
1374
Michael Bebenita6048d052016-08-25 14:40:54 -07001375#if CONFIG_ACCOUNTING
1376 aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
1377#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001378#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001379 if (supertx_enabled) {
1380 mbmi = set_mb_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
1381 } else {
1382 mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis, bwl,
1383 bhl);
1384 }
1385#if CONFIG_EXT_PARTITION_TYPES
1386 xd->mi[0]->mbmi.partition = partition;
1387#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001388 av1_read_mode_info(pbi, xd, supertx_enabled, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001389#else
Nathan E. Eggeebbd4792016-10-05 19:30:15 -04001390 mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis, bwl,
1391 bhl);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001392#if CONFIG_EXT_PARTITION_TYPES
1393 xd->mi[0]->mbmi.partition = partition;
1394#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001395 av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001396#endif // CONFIG_SUPERTX
1397
1398 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1399 const BLOCK_SIZE uv_subsize =
1400 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1401 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -07001402 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001403 "Invalid block size.");
1404 }
1405
1406#if CONFIG_SUPERTX
1407 mbmi->segment_id_supertx = MAX_SEGMENTS;
1408
1409 if (supertx_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001410 xd->corrupted |= aom_reader_has_error(r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001411 return;
1412 }
1413#endif // CONFIG_SUPERTX
1414
Arild Fuldseth07441162016-08-15 15:07:52 +02001415#if CONFIG_DELTA_Q
1416 if (cm->delta_q_present_flag) {
1417 int i;
1418 for (i = 0; i < MAX_SEGMENTS; i++) {
1419 xd->plane[0].seg_dequant[i][0] =
1420 av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
1421 xd->plane[0].seg_dequant[i][1] =
1422 av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
1423 xd->plane[1].seg_dequant[i][0] =
1424 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1425 xd->plane[1].seg_dequant[i][1] =
1426 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1427 xd->plane[2].seg_dequant[i][0] =
1428 av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
1429 xd->plane[2].seg_dequant[i][1] =
1430 av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
1431 }
1432 }
1433#endif
1434
Yaowu Xuc27fc142016-08-22 16:08:15 -07001435 if (mbmi->skip) {
1436 dec_reset_skip_context(xd);
1437 }
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001438#if CONFIG_COEF_INTERLEAVE
1439 {
1440 const struct macroblockd_plane *const pd_y = &xd->plane[0];
1441 const struct macroblockd_plane *const pd_c = &xd->plane[1];
1442 const TX_SIZE tx_log2_y = mbmi->tx_size;
1443 const TX_SIZE tx_log2_c = get_uv_tx_size(mbmi, pd_c);
1444 const int tx_sz_y = (1 << tx_log2_y);
1445 const int tx_sz_c = (1 << tx_log2_c);
1446 const int num_4x4_w_y = pd_y->n4_w;
1447 const int num_4x4_h_y = pd_y->n4_h;
1448 const int num_4x4_w_c = pd_c->n4_w;
1449 const int num_4x4_h_c = pd_c->n4_h;
1450 const int max_4x4_w_y = get_max_4x4_size(num_4x4_w_y, xd->mb_to_right_edge,
1451 pd_y->subsampling_x);
1452 const int max_4x4_h_y = get_max_4x4_size(num_4x4_h_y, xd->mb_to_bottom_edge,
1453 pd_y->subsampling_y);
1454 const int max_4x4_w_c = get_max_4x4_size(num_4x4_w_c, xd->mb_to_right_edge,
1455 pd_c->subsampling_x);
1456 const int max_4x4_h_c = get_max_4x4_size(num_4x4_h_c, xd->mb_to_bottom_edge,
1457 pd_c->subsampling_y);
1458
1459 // The max_4x4_w/h may be smaller than tx_sz under some corner cases,
1460 // i.e. when the SB is splitted by tile boundaries.
1461 const int tu_num_w_y = (max_4x4_w_y + tx_sz_y - 1) / tx_sz_y;
1462 const int tu_num_h_y = (max_4x4_h_y + tx_sz_y - 1) / tx_sz_y;
1463 const int tu_num_w_c = (max_4x4_w_c + tx_sz_c - 1) / tx_sz_c;
1464 const int tu_num_h_c = (max_4x4_h_c + tx_sz_c - 1) / tx_sz_c;
1465 const int tu_num_y = tu_num_w_y * tu_num_h_y;
1466 const int tu_num_c = tu_num_w_c * tu_num_h_c;
1467
1468 if (!is_inter_block(mbmi)) {
1469 int tu_idx_c = 0;
1470 int row_y, col_y, row_c, col_c;
1471 int plane;
1472
1473#if CONFIG_PALETTE
1474 for (plane = 0; plane <= 1; ++plane) {
1475 if (mbmi->palette_mode_info.palette_size[plane])
1476 av1_decode_palette_tokens(xd, plane, r);
1477 }
1478#endif
1479
1480 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1481 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1482 // luma
1483 predict_and_reconstruct_intra_block(
1484 cm, xd, r, mbmi, 0, row_y * tx_sz_y, col_y * tx_sz_y, tx_log2_y);
1485 // chroma
1486 if (tu_idx_c < tu_num_c) {
1487 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1488 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1489 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c,
1490 col_c, tx_log2_c);
1491 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c,
1492 col_c, tx_log2_c);
1493 tu_idx_c++;
1494 }
1495 }
1496 }
1497
1498 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1499 while (tu_idx_c < tu_num_c) {
1500 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1501 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1502 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c, col_c,
1503 tx_log2_c);
1504 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c, col_c,
1505 tx_log2_c);
1506 tu_idx_c++;
1507 }
1508 } else {
1509 // Prediction
1510 av1_build_inter_predictors_sb(xd, mi_row, mi_col,
1511 AOMMAX(bsize, BLOCK_8X8));
1512
1513 // Reconstruction
1514 if (!mbmi->skip) {
1515 int eobtotal = 0;
1516 int tu_idx_c = 0;
1517 int row_y, col_y, row_c, col_c;
1518
1519 for (row_y = 0; row_y < tu_num_h_y; row_y++) {
1520 for (col_y = 0; col_y < tu_num_w_y; col_y++) {
1521 // luma
1522 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 0,
1523 row_y * tx_sz_y,
1524 col_y * tx_sz_y, tx_log2_y);
1525 // chroma
1526 if (tu_idx_c < tu_num_c) {
1527 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1528 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1529 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1530 1, row_c, col_c, tx_log2_c);
1531 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1532 2, row_c, col_c, tx_log2_c);
1533 tu_idx_c++;
1534 }
1535 }
1536 }
1537
1538 // In 422 case, it's possilbe that Chroma has more TUs than Luma
1539 while (tu_idx_c < tu_num_c) {
1540 row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
1541 col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
1542 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 1,
1543 row_c, col_c, tx_log2_c);
1544 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 2,
1545 row_c, col_c, tx_log2_c);
1546 tu_idx_c++;
1547 }
1548
1549 if (bsize >= BLOCK_8X8 && eobtotal == 0)
1550#if CONFIG_MISC_FIXES
1551 mbmi->has_no_coeffs = 1;
1552#else
1553 mbmi->skip = 1;
1554#endif
1555 }
1556 }
1557 }
1558#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001559 if (!is_inter_block(mbmi)) {
1560 int plane;
Urvang Joshib100db72016-10-12 16:28:56 -07001561#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001562 for (plane = 0; plane <= 1; ++plane) {
1563 if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xuf883b422016-08-30 14:01:10 -07001564 av1_decode_palette_tokens(xd, plane, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001565 }
Urvang Joshib100db72016-10-12 16:28:56 -07001566#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001567 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1568 const struct macroblockd_plane *const pd = &xd->plane[plane];
Debargha Mukherjee2f123402016-08-30 17:43:38 -07001569 const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001570 const int num_4x4_w = pd->n4_w;
1571 const int num_4x4_h = pd->n4_h;
Jingning Han2d64f122016-10-21 12:44:29 -07001572 const int stepr = tx_size_high_unit[tx_size];
1573 const int stepc = tx_size_wide_unit[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001574 int row, col;
1575 const int max_blocks_wide =
1576 num_4x4_w + (xd->mb_to_right_edge >= 0
1577 ? 0
1578 : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
1579 const int max_blocks_high =
1580 num_4x4_h + (xd->mb_to_bottom_edge >= 0
1581 ? 0
1582 : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
1583
1584 for (row = 0; row < max_blocks_high; row += stepr)
1585 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001586 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane, row, col,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001587 tx_size);
1588 }
1589 } else {
1590 // Prediction
Yaowu Xuf883b422016-08-30 14:01:10 -07001591 av1_build_inter_predictors_sb(xd, mi_row, mi_col, AOMMAX(bsize, BLOCK_8X8));
Yue Chencb60b182016-10-13 15:18:22 -07001592#if CONFIG_MOTION_VAR
1593 if (mbmi->motion_mode == OBMC_CAUSAL) {
Yue Chen894fcce2016-10-21 16:50:52 -07001594 av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001595 }
Yue Chencb60b182016-10-13 15:18:22 -07001596#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001597
1598 // Reconstruction
1599 if (!mbmi->skip) {
1600 int eobtotal = 0;
1601 int plane;
1602
1603 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1604 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han5f614262016-10-27 14:27:43 -07001605 int block_width = pd->width;
1606 int block_height = pd->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001607 int row, col;
1608#if CONFIG_VAR_TX
1609 // TODO(jingning): This can be simplified for decoder performance.
1610 const BLOCK_SIZE plane_bsize =
Yaowu Xuf883b422016-08-30 14:01:10 -07001611 get_plane_block_size(AOMMAX(bsize, BLOCK_8X8), pd);
Jingning Han70e5f3f2016-11-09 17:03:07 -08001612 const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07001613 const int bh_var_tx = tx_size_high_unit[max_tx_size];
1614 const int bw_var_tx = tx_size_wide_unit[max_tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001615#if CONFIG_EXT_TX && CONFIG_RECT_TX
Yue Chena1e48dc2016-08-29 17:29:33 -07001616 if (is_rect_tx(mbmi->tx_size)) {
1617 const TX_SIZE tx_size =
1618 plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han2d64f122016-10-21 12:44:29 -07001619 const int stepr = tx_size_high_unit[tx_size];
1620 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han5f614262016-10-27 14:27:43 -07001621 int max_blocks_wide =
1622 block_width +
Yaowu Xuc27fc142016-08-22 16:08:15 -07001623 (xd->mb_to_right_edge >= 0 ? 0 : xd->mb_to_right_edge >>
Jingning Han5f614262016-10-27 14:27:43 -07001624 (3 + pd->subsampling_x));
1625 int max_blocks_high =
1626 block_height +
Yaowu Xuc27fc142016-08-22 16:08:15 -07001627 (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >>
Jingning Han5f614262016-10-27 14:27:43 -07001628 (3 + pd->subsampling_y));
1629 max_blocks_wide >>= tx_size_wide_log2[0];
1630 max_blocks_high >>= tx_size_wide_log2[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001631 for (row = 0; row < max_blocks_high; row += stepr)
1632 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001633 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001634 plane, row, col, tx_size);
1635 } else {
1636#endif
Jingning Han5f614262016-10-27 14:27:43 -07001637 block_width >>= tx_size_wide_log2[0];
1638 block_height >>= tx_size_wide_log2[0];
1639 for (row = 0; row < block_height; row += bh_var_tx)
1640 for (col = 0; col < block_width; col += bw_var_tx)
Jingning Han8fd62b72016-10-21 12:55:54 -07001641 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, row,
1642 col, max_tx_size, &eobtotal);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001643#if CONFIG_EXT_TX && CONFIG_RECT_TX
1644 }
1645#endif
1646#else
1647 const TX_SIZE tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -07001648 plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han2d64f122016-10-21 12:44:29 -07001649 const int stepr = tx_size_high_unit[tx_size];
1650 const int stepc = tx_size_wide_unit[tx_size];
Jingning Han5f614262016-10-27 14:27:43 -07001651 int max_blocks_wide =
1652 block_width +
1653 (xd->mb_to_right_edge >= 0 ? 0 : xd->mb_to_right_edge >>
1654 (3 + pd->subsampling_x));
1655 int max_blocks_high =
1656 block_height +
Yaowu Xuc27fc142016-08-22 16:08:15 -07001657 (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >>
Jingning Han5f614262016-10-27 14:27:43 -07001658 (3 + pd->subsampling_y));
1659 max_blocks_wide >>= tx_size_wide_log2[0];
1660 max_blocks_high >>= tx_size_wide_log2[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001661 for (row = 0; row < max_blocks_high; row += stepr)
1662 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07001663 eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
1664 plane, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001665#endif
1666 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001667 }
1668 }
iole moccagattaf25a4cf2016-11-11 23:57:57 -08001669#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001670
Yaowu Xuf883b422016-08-30 14:01:10 -07001671 xd->corrupted |= aom_reader_has_error(r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001672}
1673
1674static INLINE int dec_partition_plane_context(const MACROBLOCKD *xd, int mi_row,
1675 int mi_col, int bsl) {
1676 const PARTITION_CONTEXT *above_ctx = xd->above_seg_context + mi_col;
1677 const PARTITION_CONTEXT *left_ctx =
1678 xd->left_seg_context + (mi_row & MAX_MIB_MASK);
1679 int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1;
1680
1681 // assert(bsl >= 0);
1682
1683 return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
1684}
1685
1686#if !CONFIG_EXT_PARTITION_TYPES
1687static INLINE void dec_update_partition_context(MACROBLOCKD *xd, int mi_row,
1688 int mi_col, BLOCK_SIZE subsize,
1689 int bw) {
1690 PARTITION_CONTEXT *const above_ctx = xd->above_seg_context + mi_col;
1691 PARTITION_CONTEXT *const left_ctx =
1692 xd->left_seg_context + (mi_row & MAX_MIB_MASK);
1693
1694 // update the partition context at the end notes. set partition bits
1695 // of block sizes larger than the current one to be one, and partition
1696 // bits of smaller block sizes to be zero.
1697 memset(above_ctx, partition_context_lookup[subsize].above, bw);
1698 memset(left_ctx, partition_context_lookup[subsize].left, bw);
1699}
1700#endif // !CONFIG_EXT_PARTITION_TYPES
1701
Yaowu Xuf883b422016-08-30 14:01:10 -07001702static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
1703 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001704 int has_rows, int has_cols,
1705#if CONFIG_EXT_PARTITION_TYPES
1706 BLOCK_SIZE bsize,
1707#endif
1708 int bsl) {
1709 const int ctx = dec_partition_plane_context(xd, mi_row, mi_col, bsl);
Yaowu Xuf883b422016-08-30 14:01:10 -07001710 const aom_prob *const probs = cm->fc->partition_prob[ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001711 FRAME_COUNTS *counts = xd->counts;
1712 PARTITION_TYPE p;
1713
1714 if (has_rows && has_cols)
1715#if CONFIG_EXT_PARTITION_TYPES
1716 if (bsize <= BLOCK_8X8)
Michael Bebenita6048d052016-08-25 14:40:54 -07001717 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001718 else
Michael Bebenita6048d052016-08-25 14:40:54 -07001719 p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs,
1720 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001721#else
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001722#if CONFIG_DAALA_EC
Yaowu Xuf2581a32016-10-20 13:05:47 -07001723 p = (PARTITION_TYPE)aom_read_symbol(r, cm->fc->partition_cdf[ctx],
1724 PARTITION_TYPES, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001725#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001726 p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
Nathan E. Eggefba2be62016-05-03 09:48:54 -04001727#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001728#endif // CONFIG_EXT_PARTITION_TYPES
1729 else if (!has_rows && has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001730 p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001731 else if (has_rows && !has_cols)
Michael Bebenita6048d052016-08-25 14:40:54 -07001732 p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001733 else
1734 p = PARTITION_SPLIT;
1735
1736 if (counts) ++counts->partition[ctx][p];
1737
1738 return p;
1739}
1740
1741#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07001742static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
1743 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001744 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
1745 return 1;
1746 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001747 const int ctx = av1_get_skip_context(xd);
Michael Bebenita6048d052016-08-25 14:40:54 -07001748 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001749 FRAME_COUNTS *counts = xd->counts;
1750 if (counts) ++counts->skip[ctx][skip];
1751 return skip;
1752 }
1753}
1754#endif // CONFIG_SUPERTX
Yaowu Xud71be782016-10-14 08:47:03 -07001755#if CONFIG_CLPF
1756static int clpf_all_skip(const AV1_COMMON *cm, int mi_col, int mi_row,
1757 int size) {
1758 int r, c;
1759 int skip = 1;
1760 const int maxc = AOMMIN(size, cm->mi_cols - mi_col);
1761 const int maxr = AOMMIN(size, cm->mi_rows - mi_row);
1762 for (r = 0; r < maxr && skip; r++) {
1763 for (c = 0; c < maxc && skip; c++) {
1764 skip &= !!cm->mi_grid_visible[(mi_row + r) * cm->mi_stride + mi_col + c]
1765 ->mbmi.skip;
1766 }
1767 }
1768 return skip;
1769}
1770#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001771
1772// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -07001773static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001774#if CONFIG_SUPERTX
1775 int supertx_enabled,
1776#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001777 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001778 BLOCK_SIZE bsize, int n4x4_l2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001779 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001780 const int n8x8_l2 = n4x4_l2 - 1;
1781 const int num_8x8_wh = 1 << n8x8_l2;
1782 const int hbs = num_8x8_wh >> 1;
1783 PARTITION_TYPE partition;
1784 BLOCK_SIZE subsize;
1785#if CONFIG_EXT_PARTITION_TYPES
1786 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
1787#endif
1788 const int has_rows = (mi_row + hbs) < cm->mi_rows;
1789 const int has_cols = (mi_col + hbs) < cm->mi_cols;
1790#if CONFIG_SUPERTX
1791 const int read_token = !supertx_enabled;
1792 int skip = 0;
1793 TX_SIZE supertx_size = b_width_log2_lookup[bsize];
1794 const TileInfo *const tile = &xd->tile;
1795 int txfm = DCT_DCT;
1796#endif // CONFIG_SUPERTX
1797
1798 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1799
1800 partition = read_partition(cm, xd, mi_row, mi_col, r, has_rows, has_cols,
1801#if CONFIG_EXT_PARTITION_TYPES
1802 bsize,
1803#endif
1804 n8x8_l2);
1805 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
Yushin Cho77bba8d2016-11-04 16:36:56 -07001806
1807#if CONFIG_PVQ
1808 assert(partition < PARTITION_TYPES);
1809 assert(subsize < BLOCK_SIZES);
1810#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001811#if CONFIG_SUPERTX
1812 if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
1813 bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
1814 const int supertx_context = partition_supertx_context_lookup[partition];
Michael Bebenita6048d052016-08-25 14:40:54 -07001815 supertx_enabled = aom_read(
1816 r, cm->fc->supertx_prob[supertx_context][supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001817 if (xd->counts)
1818 xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
1819#if CONFIG_VAR_TX
1820 if (supertx_enabled) xd->supertx_size = supertx_size;
1821#endif
1822 }
1823#endif // CONFIG_SUPERTX
1824 if (!hbs) {
1825 // calculate bmode block dimensions (log 2)
1826 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1827 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1828 decode_block(pbi, xd,
1829#if CONFIG_SUPERTX
1830 supertx_enabled,
1831#endif // CONFIG_SUPERTX
1832 mi_row, mi_col, r,
1833#if CONFIG_EXT_PARTITION_TYPES
1834 partition,
1835#endif // CONFIG_EXT_PARTITION_TYPES
1836 subsize, 1, 1);
1837 } else {
1838 switch (partition) {
1839 case PARTITION_NONE:
1840 decode_block(pbi, xd,
1841#if CONFIG_SUPERTX
1842 supertx_enabled,
1843#endif // CONFIG_SUPERTX
1844 mi_row, mi_col, r,
1845#if CONFIG_EXT_PARTITION_TYPES
1846 partition,
1847#endif // CONFIG_EXT_PARTITION_TYPES
1848 subsize, n4x4_l2, n4x4_l2);
1849 break;
1850 case PARTITION_HORZ:
1851 decode_block(pbi, xd,
1852#if CONFIG_SUPERTX
1853 supertx_enabled,
1854#endif // CONFIG_SUPERTX
1855 mi_row, mi_col, r,
1856#if CONFIG_EXT_PARTITION_TYPES
1857 partition,
1858#endif // CONFIG_EXT_PARTITION_TYPES
1859 subsize, n4x4_l2, n8x8_l2);
1860 if (has_rows)
1861 decode_block(pbi, xd,
1862#if CONFIG_SUPERTX
1863 supertx_enabled,
1864#endif // CONFIG_SUPERTX
1865 mi_row + hbs, mi_col, r,
1866#if CONFIG_EXT_PARTITION_TYPES
1867 partition,
1868#endif // CONFIG_EXT_PARTITION_TYPES
1869 subsize, n4x4_l2, n8x8_l2);
1870 break;
1871 case PARTITION_VERT:
1872 decode_block(pbi, xd,
1873#if CONFIG_SUPERTX
1874 supertx_enabled,
1875#endif // CONFIG_SUPERTX
1876 mi_row, mi_col, r,
1877#if CONFIG_EXT_PARTITION_TYPES
1878 partition,
1879#endif // CONFIG_EXT_PARTITION_TYPES
1880 subsize, n8x8_l2, n4x4_l2);
1881 if (has_cols)
1882 decode_block(pbi, xd,
1883#if CONFIG_SUPERTX
1884 supertx_enabled,
1885#endif // CONFIG_SUPERTX
1886 mi_row, mi_col + hbs, r,
1887#if CONFIG_EXT_PARTITION_TYPES
1888 partition,
1889#endif // CONFIG_EXT_PARTITION_TYPES
1890 subsize, n8x8_l2, n4x4_l2);
1891 break;
1892 case PARTITION_SPLIT:
1893 decode_partition(pbi, xd,
1894#if CONFIG_SUPERTX
1895 supertx_enabled,
1896#endif // CONFIG_SUPERTX
1897 mi_row, mi_col, r, subsize, n8x8_l2);
1898 decode_partition(pbi, xd,
1899#if CONFIG_SUPERTX
1900 supertx_enabled,
1901#endif // CONFIG_SUPERTX
1902 mi_row, mi_col + hbs, r, subsize, n8x8_l2);
1903 decode_partition(pbi, xd,
1904#if CONFIG_SUPERTX
1905 supertx_enabled,
1906#endif // CONFIG_SUPERTX
1907 mi_row + hbs, mi_col, r, subsize, n8x8_l2);
1908 decode_partition(pbi, xd,
1909#if CONFIG_SUPERTX
1910 supertx_enabled,
1911#endif // CONFIG_SUPERTX
1912 mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
1913 break;
1914#if CONFIG_EXT_PARTITION_TYPES
1915 case PARTITION_HORZ_A:
1916 decode_block(pbi, xd,
1917#if CONFIG_SUPERTX
1918 supertx_enabled,
1919#endif
1920 mi_row, mi_col, r, partition, bsize2, n8x8_l2, n8x8_l2);
1921 decode_block(pbi, xd,
1922#if CONFIG_SUPERTX
1923 supertx_enabled,
1924#endif
1925 mi_row, mi_col + hbs, r, partition, bsize2, n8x8_l2,
1926 n8x8_l2);
1927 decode_block(pbi, xd,
1928#if CONFIG_SUPERTX
1929 supertx_enabled,
1930#endif
1931 mi_row + hbs, mi_col, r, partition, subsize, n4x4_l2,
1932 n8x8_l2);
1933 break;
1934 case PARTITION_HORZ_B:
1935 decode_block(pbi, xd,
1936#if CONFIG_SUPERTX
1937 supertx_enabled,
1938#endif
1939 mi_row, mi_col, r, partition, subsize, n4x4_l2, n8x8_l2);
1940 decode_block(pbi, xd,
1941#if CONFIG_SUPERTX
1942 supertx_enabled,
1943#endif
1944 mi_row + hbs, mi_col, r, partition, bsize2, n8x8_l2,
1945 n8x8_l2);
1946 decode_block(pbi, xd,
1947#if CONFIG_SUPERTX
1948 supertx_enabled,
1949#endif
1950 mi_row + hbs, mi_col + hbs, r, partition, bsize2, n8x8_l2,
1951 n8x8_l2);
1952 break;
1953 case PARTITION_VERT_A:
1954 decode_block(pbi, xd,
1955#if CONFIG_SUPERTX
1956 supertx_enabled,
1957#endif
1958 mi_row, mi_col, r, partition, bsize2, n8x8_l2, n8x8_l2);
1959 decode_block(pbi, xd,
1960#if CONFIG_SUPERTX
1961 supertx_enabled,
1962#endif
1963 mi_row + hbs, mi_col, r, partition, bsize2, n8x8_l2,
1964 n8x8_l2);
1965 decode_block(pbi, xd,
1966#if CONFIG_SUPERTX
1967 supertx_enabled,
1968#endif
1969 mi_row, mi_col + hbs, r, partition, subsize, n8x8_l2,
1970 n4x4_l2);
1971 break;
1972 case PARTITION_VERT_B:
1973 decode_block(pbi, xd,
1974#if CONFIG_SUPERTX
1975 supertx_enabled,
1976#endif
1977 mi_row, mi_col, r, partition, subsize, n8x8_l2, n4x4_l2);
1978 decode_block(pbi, xd,
1979#if CONFIG_SUPERTX
1980 supertx_enabled,
1981#endif
1982 mi_row, mi_col + hbs, r, partition, bsize2, n8x8_l2,
1983 n8x8_l2);
1984 decode_block(pbi, xd,
1985#if CONFIG_SUPERTX
1986 supertx_enabled,
1987#endif
1988 mi_row + hbs, mi_col + hbs, r, partition, bsize2, n8x8_l2,
1989 n8x8_l2);
1990 break;
1991#endif
1992 default: assert(0 && "Invalid partition type");
1993 }
1994 }
1995
1996#if CONFIG_SUPERTX
1997 if (supertx_enabled && read_token) {
1998 uint8_t *dst_buf[3];
1999 int dst_stride[3], i;
2000 int offset = mi_row * cm->mi_stride + mi_col;
2001
2002 set_segment_id_supertx(cm, mi_row, mi_col, bsize);
2003
2004 xd->mi = cm->mi_grid_visible + offset;
2005 xd->mi[0] = cm->mi + offset;
2006 set_mi_row_col(xd, tile, mi_row, num_8x8_blocks_high_lookup[bsize], mi_col,
2007 num_8x8_blocks_wide_lookup[bsize], cm->mi_rows, cm->mi_cols);
2008 set_skip_context(xd, mi_row, mi_col);
2009 skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
2010 if (skip) {
2011 reset_skip_context(xd, bsize);
2012 } else {
2013#if CONFIG_EXT_TX
2014 if (get_ext_tx_types(supertx_size, bsize, 1) > 1) {
2015 int eset = get_ext_tx_set(supertx_size, bsize, 1);
2016 if (eset > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002017 txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
Michael Bebenita6048d052016-08-25 14:40:54 -07002018 cm->fc->inter_ext_tx_prob[eset][supertx_size],
2019 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002020 if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
2021 }
2022 }
2023#else
2024 if (supertx_size < TX_32X32) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002025 txfm = aom_read_tree(r, av1_ext_tx_tree,
Jingning Han8f6eb182016-10-19 13:48:57 -07002026 cm->fc->inter_ext_tx_prob[supertx_size], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002027 if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
2028 }
2029#endif // CONFIG_EXT_TX
2030 }
2031
Yaowu Xuf883b422016-08-30 14:01:10 -07002032 av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002033 for (i = 0; i < MAX_MB_PLANE; i++) {
2034 dst_buf[i] = xd->plane[i].dst.buf;
2035 dst_stride[i] = xd->plane[i].dst.stride;
2036 }
2037 dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
2038 bsize, dst_buf, dst_stride);
2039
2040 if (!skip) {
2041 int eobtotal = 0;
2042 MB_MODE_INFO *mbmi;
2043 set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
2044 mbmi = &xd->mi[0]->mbmi;
2045 mbmi->tx_type = txfm;
2046 assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
2047 for (i = 0; i < MAX_MB_PLANE; ++i) {
2048 const struct macroblockd_plane *const pd = &xd->plane[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002049 int row, col;
Debargha Mukherjee2f123402016-08-30 17:43:38 -07002050 const TX_SIZE tx_size = i ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han32b20282016-10-28 15:42:44 -07002051 const int stepr = tx_size_high_unit[tx_size];
2052 const int stepc = tx_size_wide_unit[tx_size];
2053 int max_blocks_wide =
2054 pd->width + (xd->mb_to_right_edge >= 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07002055 ? 0
Jingning Han32b20282016-10-28 15:42:44 -07002056 : xd->mb_to_right_edge >> (3 + pd->subsampling_x));
2057 int max_blocks_high =
2058 pd->height +
Yaowu Xuc27fc142016-08-22 16:08:15 -07002059 (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >>
Jingning Han32b20282016-10-28 15:42:44 -07002060 (3 + pd->subsampling_y));
2061
2062 max_blocks_wide >>= tx_size_wide_log2[0];
2063 max_blocks_high >>= tx_size_wide_log2[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002064
2065 for (row = 0; row < max_blocks_high; row += stepr)
2066 for (col = 0; col < max_blocks_wide; col += stepc)
Angie Chiangff6d8902016-10-21 11:02:09 -07002067 eobtotal += reconstruct_inter_block(
2068 cm, xd, r, mbmi->segment_id_supertx, i, row, col, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002069 }
2070 if (!(subsize < BLOCK_8X8) && eobtotal == 0) skip = 1;
2071 }
2072 set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
2073 }
2074#endif // CONFIG_SUPERTX
2075
2076#if CONFIG_EXT_PARTITION_TYPES
2077 if (bsize >= BLOCK_8X8) {
2078 switch (partition) {
2079 case PARTITION_SPLIT:
2080 if (bsize > BLOCK_8X8) break;
2081 case PARTITION_NONE:
2082 case PARTITION_HORZ:
2083 case PARTITION_VERT:
2084 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
2085 break;
2086 case PARTITION_HORZ_A:
2087 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2088 update_partition_context(xd, mi_row + hbs, mi_col, subsize, subsize);
2089 break;
2090 case PARTITION_HORZ_B:
2091 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2092 update_partition_context(xd, mi_row + hbs, mi_col, bsize2, subsize);
2093 break;
2094 case PARTITION_VERT_A:
2095 update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
2096 update_partition_context(xd, mi_row, mi_col + hbs, subsize, subsize);
2097 break;
2098 case PARTITION_VERT_B:
2099 update_partition_context(xd, mi_row, mi_col, subsize, subsize);
2100 update_partition_context(xd, mi_row, mi_col + hbs, bsize2, subsize);
2101 break;
2102 default: assert(0 && "Invalid partition type");
2103 }
2104 }
2105#else
2106 // update partition context
2107 if (bsize >= BLOCK_8X8 &&
2108 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
2109 dec_update_partition_context(xd, mi_row, mi_col, subsize, num_8x8_wh);
David Barkerf8935c92016-10-26 14:54:06 +01002110#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xud71be782016-10-14 08:47:03 -07002111
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002112#if CONFIG_DERING
David Barker9739f362016-11-10 09:29:32 +00002113#if CONFIG_EXT_PARTITION
2114 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128) {
2115 if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
2116 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2117 aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
2118 } else {
2119 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2120 0;
2121 }
2122 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64) {
2123#else
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002124 if (bsize == BLOCK_64X64) {
David Barker9739f362016-11-10 09:29:32 +00002125#endif
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02002126 if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
2127 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2128 aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
2129 } else {
2130 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
2131 0;
2132 }
2133 }
2134#endif
2135
Yaowu Xud71be782016-10-14 08:47:03 -07002136#if CONFIG_CLPF
David Barker9739f362016-11-10 09:29:32 +00002137#if CONFIG_EXT_PARTITION
2138 if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128 &&
2139 cm->clpf_strength_y && cm->clpf_size != CLPF_NOSIZE) {
2140 const int tl = mi_row * MI_SIZE / MIN_FB_SIZE * cm->clpf_stride +
2141 mi_col * MI_SIZE / MIN_FB_SIZE;
2142 if (cm->clpf_size == CLPF_128X128) {
2143 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
2144 } else if (cm->clpf_size == CLPF_64X64) {
2145 const int tr = tl + 2;
2146 const int bl = tl + 2 * cm->clpf_stride;
2147 const int br = tr + 2 * cm->clpf_stride;
2148 const int size = 64 / MI_SIZE;
2149
2150 // Up to four bits per SB
2151 if (!clpf_all_skip(cm, mi_col, mi_row, size))
2152 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
2153
2154 if (mi_col + size < cm->mi_cols &&
2155 !clpf_all_skip(cm, mi_col + size, mi_row, size))
2156 cm->clpf_blocks[tr] = aom_read_literal(r, 1, ACCT_STR);
2157
2158 if (mi_row + size < cm->mi_rows &&
2159 !clpf_all_skip(cm, mi_col, mi_row + size, size))
2160 cm->clpf_blocks[bl] = aom_read_literal(r, 1, ACCT_STR);
2161
2162 if (mi_col + size < cm->mi_cols && mi_row + size < cm->mi_rows &&
2163 !clpf_all_skip(cm, mi_col + size, mi_row + size, size))
2164 cm->clpf_blocks[br] = aom_read_literal(r, 1, ACCT_STR);
2165 } else if (cm->clpf_size == CLPF_32X32) {
2166 int i, j;
2167 const int size = 32 / MI_SIZE;
2168 for (i = 0; i < 4; ++i)
2169 for (j = 0; j < 4; ++j) {
2170 const int index = tl + i * cm->clpf_stride + j;
2171 if (mi_row + i * size < cm->mi_rows &&
2172 mi_col + j * size < cm->mi_cols &&
2173 !clpf_all_skip(cm, mi_col + j * size, mi_row + i * size, size))
2174 cm->clpf_blocks[index] = aom_read_literal(r, 1, ACCT_STR);
2175 }
2176 }
2177 } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64 &&
2178#else
2179 if (bsize == BLOCK_64X64 &&
2180#endif // CONFIG_EXT_PARTITION
2181 cm->clpf_strength_y && cm->clpf_size != CLPF_NOSIZE) {
Yaowu Xud71be782016-10-14 08:47:03 -07002182 const int tl = mi_row * MI_SIZE / MIN_FB_SIZE * cm->clpf_stride +
2183 mi_col * MI_SIZE / MIN_FB_SIZE;
2184
2185 if (!((mi_row * MI_SIZE) & 127) && !((mi_col * MI_SIZE) & 127) &&
2186 cm->clpf_size == CLPF_128X128) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002187 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002188 } else if (cm->clpf_size == CLPF_64X64 &&
2189 !clpf_all_skip(cm, mi_col, mi_row, 64 / MI_SIZE)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002190 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002191 } else if (cm->clpf_size == CLPF_32X32) {
2192 const int tr = tl + 1;
2193 const int bl = tl + cm->clpf_stride;
2194 const int br = tr + cm->clpf_stride;
2195 const int size = 32 / MI_SIZE;
2196
2197 // Up to four bits per SB
2198 if (!clpf_all_skip(cm, mi_col, mi_row, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002199 cm->clpf_blocks[tl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002200
2201 if (mi_col + size < cm->mi_cols &&
2202 !clpf_all_skip(cm, mi_col + size, mi_row, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002203 cm->clpf_blocks[tr] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002204
2205 if (mi_row + size < cm->mi_rows &&
2206 !clpf_all_skip(cm, mi_col, mi_row + size, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002207 cm->clpf_blocks[bl] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002208
2209 if (mi_col + size < cm->mi_cols && mi_row + size < cm->mi_rows &&
2210 !clpf_all_skip(cm, mi_col + size, mi_row + size, size))
Michael Bebenita6048d052016-08-25 14:40:54 -07002211 cm->clpf_blocks[br] = aom_read_literal(r, 1, ACCT_STR);
Yaowu Xud71be782016-10-14 08:47:03 -07002212 }
2213 }
David Barker9739f362016-11-10 09:29:32 +00002214#endif // CONFIG_CLPF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002215}
2216
2217#if !CONFIG_ANS
2218static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
2219 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002220 struct aom_internal_error_info *error_info,
2221 aom_reader *r, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002222 void *decrypt_state) {
2223 // Validate the calculated partition length. If the buffer
2224 // described by the partition can't be fully read, then restrict
2225 // it to the portion that can be (for EC mode) or throw an error.
2226 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002227 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002228 "Truncated packet or corrupt tile length");
2229
Yaowu Xuf883b422016-08-30 14:01:10 -07002230 if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
2231 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002232 "Failed to allocate bool decoder %d", 1);
2233}
2234#else
2235static void setup_token_decoder(const uint8_t *data, const uint8_t *data_end,
2236 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07002237 struct aom_internal_error_info *error_info,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002238 struct AnsDecoder *const ans,
Yaowu Xuf883b422016-08-30 14:01:10 -07002239 aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002240 void *decrypt_state) {
2241 (void)decrypt_cb;
2242 (void)decrypt_state;
2243 // Validate the calculated partition length. If the buffer
2244 // described by the partition can't be fully read, then restrict
2245 // it to the portion that can be (for EC mode) or throw an error.
2246 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002247 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002248 "Truncated packet or corrupt tile length");
2249
2250 if (read_size > INT_MAX || ans_read_init(ans, data, (int)read_size))
Yaowu Xuf883b422016-08-30 14:01:10 -07002251 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002252 "Failed to allocate token decoder %d", 1);
2253}
2254#endif
2255
Yushin Cho77bba8d2016-11-04 16:36:56 -07002256#if !CONFIG_PVQ
Yaowu Xuf883b422016-08-30 14:01:10 -07002257static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
2258 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002259 int i, j, k, l, m;
Thomas9ac55082016-09-23 18:04:17 +01002260#if CONFIG_EC_ADAPT
Thomas Davies09ebbfb2016-10-20 18:28:47 +01002261 const int node_limit = UNCONSTRAINED_NODES - 1;
Thomas9ac55082016-09-23 18:04:17 +01002262#else
2263 const int node_limit = UNCONSTRAINED_NODES;
2264#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002265
Michael Bebenita6048d052016-08-25 14:40:54 -07002266 if (aom_read_bit(r, ACCT_STR))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002267 for (i = 0; i < PLANE_TYPES; ++i)
2268 for (j = 0; j < REF_TYPES; ++j)
2269 for (k = 0; k < COEF_BANDS; ++k)
2270 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
Thomas9ac55082016-09-23 18:04:17 +01002271 for (m = 0; m < node_limit; ++m)
Michael Bebenita6048d052016-08-25 14:40:54 -07002272 av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002273}
2274
Yaowu Xuf883b422016-08-30 14:01:10 -07002275static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002276 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
2277 TX_SIZE tx_size;
2278 for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size)
2279 read_coef_probs_common(fc->coef_probs[tx_size], r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002280}
Yushin Cho77bba8d2016-11-04 16:36:56 -07002281#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002282
Yaowu Xuf883b422016-08-30 14:01:10 -07002283static void setup_segmentation(AV1_COMMON *const cm,
2284 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002285 struct segmentation *const seg = &cm->seg;
2286 int i, j;
2287
2288 seg->update_map = 0;
2289 seg->update_data = 0;
2290
Yaowu Xuf883b422016-08-30 14:01:10 -07002291 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002292 if (!seg->enabled) return;
2293
2294 // Segmentation map update
2295 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2296 seg->update_map = 1;
2297 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002298 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002299 }
2300 if (seg->update_map) {
2301 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
2302 seg->temporal_update = 0;
2303 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002304 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002305 }
2306 }
2307
2308 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07002309 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002310 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002311 seg->abs_delta = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002312
Yaowu Xuf883b422016-08-30 14:01:10 -07002313 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002314
2315 for (i = 0; i < MAX_SEGMENTS; i++) {
2316 for (j = 0; j < SEG_LVL_MAX; j++) {
2317 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002318 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002319 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002320 av1_enable_segfeature(seg, i, j);
2321 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
2322 if (av1_is_segfeature_signed(j))
2323 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002324 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002325 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002326 }
2327 }
2328 }
2329}
2330
2331#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002332static void decode_restoration_mode(AV1_COMMON *cm,
2333 struct aom_read_bit_buffer *rb) {
2334 RestorationInfo *rsi = &cm->rst_info;
2335 if (aom_rb_read_bit(rb)) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002336 if (aom_rb_read_bit(rb))
2337 rsi->frame_restoration_type =
2338 (aom_rb_read_bit(rb) ? RESTORE_WIENER : RESTORE_BILATERAL);
2339 else
2340 rsi->frame_restoration_type = RESTORE_SGRPROJ;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002341 } else {
2342 rsi->frame_restoration_type =
2343 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
2344 }
2345}
2346
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002347static void read_wiener_filter(WienerInfo *wiener_info, aom_reader *rb) {
2348 wiener_info->vfilter[0] =
2349 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2350 WIENER_FILT_TAP0_MINV;
2351 wiener_info->vfilter[1] =
2352 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2353 WIENER_FILT_TAP1_MINV;
2354 wiener_info->vfilter[2] =
2355 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2356 WIENER_FILT_TAP2_MINV;
2357 wiener_info->hfilter[0] =
2358 aom_read_literal(rb, WIENER_FILT_TAP0_BITS, ACCT_STR) +
2359 WIENER_FILT_TAP0_MINV;
2360 wiener_info->hfilter[1] =
2361 aom_read_literal(rb, WIENER_FILT_TAP1_BITS, ACCT_STR) +
2362 WIENER_FILT_TAP1_MINV;
2363 wiener_info->hfilter[2] =
2364 aom_read_literal(rb, WIENER_FILT_TAP2_BITS, ACCT_STR) +
2365 WIENER_FILT_TAP2_MINV;
2366}
2367
2368static void read_sgrproj_filter(SgrprojInfo *sgrproj_info, aom_reader *rb) {
2369 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
2370 sgrproj_info->xqd[0] =
2371 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN0;
2372 sgrproj_info->xqd[1] =
2373 aom_read_literal(rb, SGRPROJ_PRJ_BITS, ACCT_STR) + SGRPROJ_PRJ_MIN1;
2374}
2375
2376static void read_bilateral_filter(const AV1_COMMON *cm,
2377 BilateralInfo *bilateral_info,
2378 aom_reader *rb) {
2379 int s;
2380 for (s = 0; s < BILATERAL_SUBTILES; ++s) {
2381 if (aom_read(rb, RESTORE_NONE_BILATERAL_PROB, ACCT_STR)) {
2382 bilateral_info->level[s] =
2383 aom_read_literal(rb, av1_bilateral_level_bits(cm), ACCT_STR);
2384 } else {
2385 bilateral_info->level[s] = -1;
2386 }
2387 }
2388}
2389
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002390static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002391 int i;
2392 RestorationInfo *rsi = &cm->rst_info;
clang-formatbda8d612016-09-19 15:55:46 -07002393 const int ntiles =
2394 av1_get_rest_ntiles(cm->width, cm->height, NULL, NULL, NULL, NULL);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002395 if (rsi->frame_restoration_type != RESTORE_NONE) {
2396 rsi->restoration_type = (RestorationType *)aom_realloc(
2397 rsi->restoration_type, sizeof(*rsi->restoration_type) * ntiles);
2398 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07002399 rsi->bilateral_info = (BilateralInfo *)aom_realloc(
2400 rsi->bilateral_info, sizeof(*rsi->bilateral_info) * ntiles);
2401 assert(rsi->bilateral_info != NULL);
2402 rsi->wiener_info = (WienerInfo *)aom_realloc(
2403 rsi->wiener_info, sizeof(*rsi->wiener_info) * ntiles);
2404 assert(rsi->wiener_info != NULL);
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002405 rsi->sgrproj_info = (SgrprojInfo *)aom_realloc(
2406 rsi->sgrproj_info, sizeof(*rsi->sgrproj_info) * ntiles);
2407 assert(rsi->sgrproj_info != NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002408 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002409 rsi->restoration_type[i] =
2410 aom_read_tree(rb, av1_switchable_restore_tree,
2411 cm->fc->switchable_restore_prob, ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002412 if (rsi->restoration_type[i] == RESTORE_WIENER) {
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07002413 rsi->wiener_info[i].level = 1;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002414 read_wiener_filter(&rsi->wiener_info[i], rb);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002415 } else if (rsi->restoration_type[i] == RESTORE_BILATERAL) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002416#if BILATERAL_SUBTILES == 0
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002417 rsi->bilateral_info[i].level[0] =
2418 aom_read_literal(rb, av1_bilateral_level_bits(cm), ACCT_STR);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002419#else
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002420 read_bilateral_filter(cm, &rsi->bilateral_info[i], rb);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002421#endif
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002422 } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
2423 rsi->sgrproj_info[i].level = 1;
2424 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002425 }
2426 }
2427 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07002428 rsi->wiener_info = (WienerInfo *)aom_realloc(
2429 rsi->wiener_info, sizeof(*rsi->wiener_info) * ntiles);
2430 assert(rsi->wiener_info != NULL);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002431 for (i = 0; i < ntiles; ++i) {
Michael Bebenita6048d052016-08-25 14:40:54 -07002432 if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002433 rsi->restoration_type[i] = RESTORE_WIENER;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002434 rsi->wiener_info[i].level = 1;
2435 read_wiener_filter(&rsi->wiener_info[i], rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002436 } else {
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07002437 rsi->wiener_info[i].level = 0;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002438 rsi->restoration_type[i] = RESTORE_NONE;
2439 }
2440 }
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002441 } else if (rsi->frame_restoration_type == RESTORE_BILATERAL) {
Debargha Mukherjee5d89a632016-09-17 13:16:58 -07002442 rsi->bilateral_info = (BilateralInfo *)aom_realloc(
2443 rsi->bilateral_info, sizeof(*rsi->bilateral_info) * ntiles);
2444 assert(rsi->bilateral_info != NULL);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002445 for (i = 0; i < ntiles; ++i) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07002446 rsi->restoration_type[i] = RESTORE_BILATERAL;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07002447 read_bilateral_filter(cm, &rsi->bilateral_info[i], rb);
2448 }
2449 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
2450 rsi->sgrproj_info = (SgrprojInfo *)aom_realloc(
2451 rsi->sgrproj_info, sizeof(*rsi->sgrproj_info) * ntiles);
2452 assert(rsi->sgrproj_info != NULL);
2453 for (i = 0; i < ntiles; ++i) {
2454 if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
2455 rsi->restoration_type[i] = RESTORE_SGRPROJ;
2456 rsi->sgrproj_info[i].level = 1;
2457 read_sgrproj_filter(&rsi->sgrproj_info[i], rb);
2458 } else {
2459 rsi->sgrproj_info[i].level = 0;
2460 rsi->restoration_type[i] = RESTORE_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002461 }
2462 }
2463 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002464 }
2465}
2466#endif // CONFIG_LOOP_RESTORATION
2467
Yaowu Xuf883b422016-08-30 14:01:10 -07002468static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002469 struct loopfilter *lf = &cm->lf;
Yaowu Xuf883b422016-08-30 14:01:10 -07002470 lf->filter_level = aom_rb_read_literal(rb, 6);
2471 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002472
2473 // Read in loop filter deltas applied at the MB level based on mode or ref
2474 // frame.
2475 lf->mode_ref_delta_update = 0;
2476
Yaowu Xuf883b422016-08-30 14:01:10 -07002477 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002478 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002479 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002480 if (lf->mode_ref_delta_update) {
2481 int i;
2482
2483 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002484 if (aom_rb_read_bit(rb))
2485 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002486
2487 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07002488 if (aom_rb_read_bit(rb))
2489 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002490 }
2491 }
2492}
2493
2494#if CONFIG_CLPF
Yaowu Xud71be782016-10-14 08:47:03 -07002495static void setup_clpf(AV1Decoder *pbi, struct aom_read_bit_buffer *rb) {
2496 AV1_COMMON *const cm = &pbi->common;
2497 const int width = pbi->cur_buf->buf.y_crop_width;
2498 const int height = pbi->cur_buf->buf.y_crop_height;
2499
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002500 cm->clpf_blocks = 0;
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02002501 cm->clpf_strength_y = aom_rb_read_literal(rb, 2);
2502 cm->clpf_strength_u = aom_rb_read_literal(rb, 2);
2503 cm->clpf_strength_v = aom_rb_read_literal(rb, 2);
2504 if (cm->clpf_strength_y) {
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002505 cm->clpf_size = aom_rb_read_literal(rb, 2);
Yaowu Xud71be782016-10-14 08:47:03 -07002506 if (cm->clpf_size != CLPF_NOSIZE) {
2507 int size;
2508 cm->clpf_stride =
2509 ((width + MIN_FB_SIZE - 1) & ~(MIN_FB_SIZE - 1)) >> MIN_FB_SIZE_LOG2;
2510 size =
2511 cm->clpf_stride * ((height + MIN_FB_SIZE - 1) & ~(MIN_FB_SIZE - 1)) >>
2512 MIN_FB_SIZE_LOG2;
2513 CHECK_MEM_ERROR(cm, cm->clpf_blocks, aom_malloc(size));
2514 memset(cm->clpf_blocks, -1, size);
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002515 }
2516 }
2517}
2518
Steinar Midtskogen2fd70ee2016-09-02 10:02:30 +02002519static int clpf_bit(UNUSED int k, UNUSED int l,
2520 UNUSED const YV12_BUFFER_CONFIG *rec,
2521 UNUSED const YV12_BUFFER_CONFIG *org,
2522 UNUSED const AV1_COMMON *cm, UNUSED int block_size,
2523 UNUSED int w, UNUSED int h, UNUSED unsigned int strength,
Yaowu Xud71be782016-10-14 08:47:03 -07002524 UNUSED unsigned int fb_size_log2, int8_t *bit) {
Steinar Midtskogend06588a2016-05-06 13:48:20 +02002525 return *bit;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002526}
2527#endif
2528
2529#if CONFIG_DERING
Yaowu Xuf883b422016-08-30 14:01:10 -07002530static void setup_dering(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
2531 cm->dering_level = aom_rb_read_literal(rb, DERING_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002532}
2533#endif // CONFIG_DERING
2534
Yaowu Xuf883b422016-08-30 14:01:10 -07002535static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
2536 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002537}
2538
Yaowu Xuf883b422016-08-30 14:01:10 -07002539static void setup_quantization(AV1_COMMON *const cm,
2540 struct aom_read_bit_buffer *rb) {
2541 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002542 cm->y_dc_delta_q = read_delta_q(rb);
2543 cm->uv_dc_delta_q = read_delta_q(rb);
2544 cm->uv_ac_delta_q = read_delta_q(rb);
2545 cm->dequant_bit_depth = cm->bit_depth;
2546#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07002547 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002548 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002549 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
2550 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002551 } else {
2552 cm->min_qmlevel = 0;
2553 cm->max_qmlevel = 0;
2554 }
2555#endif
2556}
2557
Yaowu Xuf883b422016-08-30 14:01:10 -07002558static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002559 // Build y/uv dequant values based on segmentation.
2560 int i = 0;
2561#if CONFIG_AOM_QM
2562 int lossless;
2563 int j = 0;
2564 int qmlevel;
2565 int using_qm = cm->using_qmatrix;
2566 int minqm = cm->min_qmlevel;
2567 int maxqm = cm->max_qmlevel;
2568#endif
2569#if CONFIG_NEW_QUANT
2570 int b;
2571 int dq;
2572#endif // CONFIG_NEW_QUANT
2573 if (cm->seg.enabled) {
2574 for (i = 0; i < MAX_SEGMENTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002575 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002576 cm->y_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002577 av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2578 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002579 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002580 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002581 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002582 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002583#if CONFIG_AOM_QM
2584 lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
2585 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2586 // NB: depends on base index so there is only 1 set per frame
2587 // No quant weighting when lossless or signalled not using QM
2588 qmlevel = (lossless || using_qm == 0)
2589 ? NUM_QM_LEVELS - 1
2590 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2591 for (j = 0; j < TX_SIZES; ++j) {
2592 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2593 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2594 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2595 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2596 }
2597#endif // CONFIG_AOM_QM
2598#if CONFIG_NEW_QUANT
2599 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2600 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002601 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002602 cm->y_dequant_nuq[i][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002603 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002604 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002605 }
2606 }
2607#endif // CONFIG_NEW_QUANT
2608 }
2609 } else {
2610 const int qindex = cm->base_qindex;
2611 // When segmentation is disabled, only the first value is used. The
2612 // remaining are don't cares.
Yaowu Xuf883b422016-08-30 14:01:10 -07002613 cm->y_dequant[0][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
2614 cm->y_dequant[0][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002615 cm->uv_dequant[0][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002616 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002617 cm->uv_dequant[0][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07002618 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002619#if CONFIG_AOM_QM
2620 lossless = qindex == 0 && cm->y_dc_delta_q == 0 && cm->uv_dc_delta_q == 0 &&
2621 cm->uv_ac_delta_q == 0;
2622 // No quant weighting when lossless or signalled not using QM
2623 qmlevel = (lossless || using_qm == 0)
2624 ? NUM_QM_LEVELS - 1
2625 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
2626 for (j = 0; j < TX_SIZES; ++j) {
2627 cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
2628 cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
2629 cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
2630 cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
2631 }
2632#endif
2633#if CONFIG_NEW_QUANT
2634 for (dq = 0; dq < QUANT_PROFILES; dq++) {
2635 for (b = 0; b < COEF_BANDS; ++b) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002636 av1_get_dequant_val_nuq(cm->y_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002637 cm->y_dequant_nuq[0][dq][b], NULL, dq);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -07002638 av1_get_dequant_val_nuq(cm->uv_dequant[0][b != 0], b,
Yaowu Xuf883b422016-08-30 14:01:10 -07002639 cm->uv_dequant_nuq[0][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002640 }
2641 }
2642#endif // CONFIG_NEW_QUANT
2643 }
2644}
2645
Angie Chiang5678ad92016-11-21 09:38:40 -08002646static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002647 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07002648 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002649}
2650
Yaowu Xuf883b422016-08-30 14:01:10 -07002651static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002652 cm->render_width = cm->width;
2653 cm->render_height = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002654 if (aom_rb_read_bit(rb))
2655 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002656}
2657
Yaowu Xuf883b422016-08-30 14:01:10 -07002658static void resize_mv_buffer(AV1_COMMON *cm) {
2659 aom_free(cm->cur_frame->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002660 cm->cur_frame->mi_rows = cm->mi_rows;
2661 cm->cur_frame->mi_cols = cm->mi_cols;
2662 CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07002663 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002664 sizeof(*cm->cur_frame->mvs)));
2665}
2666
Yaowu Xuf883b422016-08-30 14:01:10 -07002667static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002668#if CONFIG_SIZE_LIMIT
2669 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002670 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002671 "Dimensions of %dx%d beyond allowed size of %dx%d.",
2672 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
2673#endif
2674 if (cm->width != width || cm->height != height) {
2675 const int new_mi_rows =
2676 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2677 const int new_mi_cols =
2678 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
2679
Yaowu Xuf883b422016-08-30 14:01:10 -07002680 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07002681 // dimensions as well as the overall size.
2682 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002683 if (av1_alloc_context_buffers(cm, width, height))
2684 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002685 "Failed to allocate context buffers");
2686 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002687 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002688 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002689 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002690 cm->width = width;
2691 cm->height = height;
2692 }
2693 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
2694 cm->mi_cols > cm->cur_frame->mi_cols) {
2695 resize_mv_buffer(cm);
2696 }
2697}
2698
Yaowu Xuf883b422016-08-30 14:01:10 -07002699static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002700 int width, height;
2701 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuf883b422016-08-30 14:01:10 -07002702 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002703 resize_context_buffers(cm, width, height);
2704 setup_render_size(cm, rb);
2705
2706 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002707 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002708 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2709 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002710#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002711 cm->use_highbitdepth,
2712#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002713 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002714 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2715 pool->cb_priv)) {
2716 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002717 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002718 "Failed to allocate frame buffer");
2719 }
2720 unlock_buffer_pool(pool);
2721
2722 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2723 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2724 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2725 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2726 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2727 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2728 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2729}
2730
Yaowu Xuf883b422016-08-30 14:01:10 -07002731static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002732 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002733 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002734 int this_xss, int this_yss) {
2735 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2736 ref_yss == this_yss;
2737}
2738
Yaowu Xuf883b422016-08-30 14:01:10 -07002739static void setup_frame_size_with_refs(AV1_COMMON *cm,
2740 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002741 int width, height;
2742 int found = 0, i;
2743 int has_valid_ref_frame = 0;
2744 BufferPool *const pool = cm->buffer_pool;
2745 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002746 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002747 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
2748 width = buf->y_crop_width;
2749 height = buf->y_crop_height;
2750 cm->render_width = buf->render_width;
2751 cm->render_height = buf->render_height;
2752 found = 1;
2753 break;
2754 }
2755 }
2756
2757 if (!found) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002758 av1_read_frame_size(rb, &width, &height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002759 setup_render_size(cm, rb);
2760 }
2761
2762 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07002763 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002764 "Invalid frame size");
2765
2766 // Check to make sure at least one of frames that this frame references
2767 // has valid dimensions.
2768 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2769 RefBuffer *const ref_frame = &cm->frame_refs[i];
2770 has_valid_ref_frame |=
2771 valid_ref_frame_size(ref_frame->buf->y_crop_width,
2772 ref_frame->buf->y_crop_height, width, height);
2773 }
2774 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07002775 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002776 "Referenced frame has invalid size");
2777 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2778 RefBuffer *const ref_frame = &cm->frame_refs[i];
2779 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
2780 ref_frame->buf->subsampling_x,
2781 ref_frame->buf->subsampling_y, cm->bit_depth,
2782 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07002783 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002784 "Referenced frame has incompatible color format");
2785 }
2786
2787 resize_context_buffers(cm, width, height);
2788
2789 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002790 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002791 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
2792 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07002793#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002794 cm->use_highbitdepth,
2795#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002796 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002797 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
2798 pool->cb_priv)) {
2799 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002800 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002801 "Failed to allocate frame buffer");
2802 }
2803 unlock_buffer_pool(pool);
2804
2805 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
2806 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
2807 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
2808 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
2809 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
2810 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
2811 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
2812}
2813
Yaowu Xuf883b422016-08-30 14:01:10 -07002814static void read_tile_info(AV1Decoder *const pbi,
2815 struct aom_read_bit_buffer *const rb) {
2816 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002817#if CONFIG_EXT_TILE
2818// Read the tile width/height
2819#if CONFIG_EXT_PARTITION
2820 if (cm->sb_size == BLOCK_128X128) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002821 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
2822 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002823 } else
2824#endif // CONFIG_EXT_PARTITION
2825 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002826 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
2827 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002828 }
2829
2830 cm->tile_width <<= cm->mib_size_log2;
2831 cm->tile_height <<= cm->mib_size_log2;
2832
Yaowu Xuf883b422016-08-30 14:01:10 -07002833 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
2834 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002835
2836 // Get the number of tiles
2837 cm->tile_cols = 1;
2838 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
2839
2840 cm->tile_rows = 1;
2841 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
2842
2843 if (cm->tile_cols * cm->tile_rows > 1) {
2844 // Read the number of bytes used to store tile size
Yaowu Xuf883b422016-08-30 14:01:10 -07002845 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2846 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002847 }
2848#else
2849 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
Yaowu Xuf883b422016-08-30 14:01:10 -07002850 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002851
2852 // columns
2853 max_ones = max_log2_tile_cols - min_log2_tile_cols;
2854 cm->log2_tile_cols = min_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -07002855 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002856
2857 if (cm->log2_tile_cols > 6)
Yaowu Xuf883b422016-08-30 14:01:10 -07002858 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002859 "Invalid number of tile columns");
2860
2861 // rows
Yaowu Xuf883b422016-08-30 14:01:10 -07002862 cm->log2_tile_rows = aom_rb_read_bit(rb);
2863 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002864
2865 cm->tile_cols = 1 << cm->log2_tile_cols;
2866 cm->tile_rows = 1 << cm->log2_tile_rows;
2867
2868 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
2869 cm->tile_width >>= cm->log2_tile_cols;
2870 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
2871 cm->tile_height >>= cm->log2_tile_rows;
2872
2873 // round to integer multiples of superblock size
2874 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
2875 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
2876
Thomas Davies4974e522016-11-07 17:44:05 +00002877// tile size magnitude
2878#if !CONFIG_TILE_GROUPS
2879 if (cm->tile_rows > 1 || cm->tile_cols > 1)
2880#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002881 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002882#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00002883
Thomas Davies80188d12016-10-26 16:08:35 -07002884#if CONFIG_TILE_GROUPS
2885 // Store an index to the location of the tile group information
2886 pbi->tg_size_bit_offset = rb->bit_offset;
2887 pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
2888 if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
2889 pbi->tg_start =
2890 aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
2891 pbi->tg_size =
2892 1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
2893 }
2894#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002895}
2896
2897static int mem_get_varsize(const uint8_t *src, const int sz) {
2898 switch (sz) {
2899 case 1: return src[0];
2900 case 2: return mem_get_le16(src);
2901 case 3: return mem_get_le24(src);
2902 case 4: return mem_get_le32(src);
2903 default: assert("Invalid size" && 0); return -1;
2904 }
2905}
2906
2907#if CONFIG_EXT_TILE
2908// Reads the next tile returning its size and adjusting '*data' accordingly
2909// based on 'is_last'.
2910static void get_tile_buffer(const uint8_t *const data_end,
Yaowu Xuf883b422016-08-30 14:01:10 -07002911 struct aom_internal_error_info *error_info,
2912 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002913 void *decrypt_state,
2914 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
2915 int tile_size_bytes, int col, int row) {
2916 size_t size;
2917
2918 size_t copy_size = 0;
2919 const uint8_t *copy_data = NULL;
2920
2921 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002922 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002923 "Truncated packet or corrupt tile length");
2924 if (decrypt_cb) {
2925 uint8_t be_data[4];
2926 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
2927
2928 // Only read number of bytes in cm->tile_size_bytes.
2929 size = mem_get_varsize(be_data, tile_size_bytes);
2930 } else {
2931 size = mem_get_varsize(*data, tile_size_bytes);
2932 }
2933
2934 // The top bit indicates copy mode
2935 if ((size >> (tile_size_bytes * 8 - 1)) == 1) {
2936 // The remaining bits in the top byte signal the row offset
2937 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
2938
2939 // Currently, only use tiles in same column as reference tiles.
2940 copy_data = tile_buffers[row - offset][col].data;
2941 copy_size = tile_buffers[row - offset][col].size;
2942 size = 0;
2943 }
2944
2945 *data += tile_size_bytes;
2946
2947 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07002948 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002949 "Truncated packet or corrupt tile size");
2950
2951 if (size > 0) {
2952 tile_buffers[row][col].data = *data;
2953 tile_buffers[row][col].size = size;
2954 } else {
2955 tile_buffers[row][col].data = copy_data;
2956 tile_buffers[row][col].size = copy_size;
2957 }
2958
2959 *data += size;
2960
2961 tile_buffers[row][col].raw_data_end = *data;
2962}
2963
2964static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07002965 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002966 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002967 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002968 const int tile_cols = cm->tile_cols;
2969 const int tile_rows = cm->tile_rows;
2970 const int have_tiles = tile_cols * tile_rows > 1;
2971
2972 if (!have_tiles) {
2973 const uint32_t tile_size = data_end - data;
2974 tile_buffers[0][0].data = data;
2975 tile_buffers[0][0].size = tile_size;
2976 tile_buffers[0][0].raw_data_end = NULL;
2977 } else {
2978 // We locate only the tile buffers that are required, which are the ones
2979 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
2980 // need the last (bottom right) tile buffer, as we need to know where the
2981 // end of the compressed frame buffer is for proper superframe decoding.
2982
2983 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
2984 const uint8_t *const data_start = data;
2985
Yaowu Xuf883b422016-08-30 14:01:10 -07002986 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002987 const int single_row = pbi->dec_tile_row >= 0;
2988 const int tile_rows_start = single_row ? dec_tile_row : 0;
2989 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07002990 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002991 const int single_col = pbi->dec_tile_col >= 0;
2992 const int tile_cols_start = single_col ? dec_tile_col : 0;
2993 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
2994
2995 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
2996 const int tile_size_bytes = pbi->tile_size_bytes;
2997
2998 size_t tile_col_size;
2999 int r, c;
3000
3001 // Read tile column sizes for all columns (we need the last tile buffer)
3002 for (c = 0; c < tile_cols; ++c) {
3003 const int is_last = c == tile_cols - 1;
3004 if (!is_last) {
3005 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
3006 data += tile_col_size_bytes;
3007 tile_col_data_end[c] = data + tile_col_size;
3008 } else {
3009 tile_col_size = data_end - data;
3010 tile_col_data_end[c] = data_end;
3011 }
3012 data += tile_col_size;
3013 }
3014
3015 data = data_start;
3016
3017 // Read the required tile sizes.
3018 for (c = tile_cols_start; c < tile_cols_end; ++c) {
3019 const int is_last = c == tile_cols - 1;
3020
3021 if (c > 0) data = tile_col_data_end[c - 1];
3022
3023 if (!is_last) data += tile_col_size_bytes;
3024
3025 // Get the whole of the last column, otherwise stop at the required tile.
3026 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
3027 tile_buffers[r][c].col = c;
3028
3029 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3030 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3031 tile_size_bytes, c, r);
3032 }
3033 }
3034
3035 // If we have not read the last column, then read it to get the last tile.
3036 if (tile_cols_end != tile_cols) {
3037 c = tile_cols - 1;
3038
3039 data = tile_col_data_end[c - 1];
3040
3041 for (r = 0; r < tile_rows; ++r) {
3042 tile_buffers[r][c].col = c;
3043
3044 get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
3045 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
3046 tile_size_bytes, c, r);
3047 }
3048 }
3049 }
3050}
3051#else
3052// Reads the next tile returning its size and adjusting '*data' accordingly
3053// based on 'is_last'.
3054static void get_tile_buffer(const uint8_t *const data_end,
3055 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07003056 struct aom_internal_error_info *error_info,
3057 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003058 void *decrypt_state, TileBufferDec *const buf) {
3059 size_t size;
3060
3061 if (!is_last) {
3062 if (!read_is_valid(*data, 4, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07003063 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003064 "Truncated packet or corrupt tile length");
3065
3066 if (decrypt_cb) {
3067 uint8_t be_data[4];
3068 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
3069 size = mem_get_varsize(be_data, tile_size_bytes);
3070 } else {
3071 size = mem_get_varsize(*data, tile_size_bytes);
3072 }
3073 *data += tile_size_bytes;
3074
3075 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07003076 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003077 "Truncated packet or corrupt tile size");
3078 } else {
3079 size = data_end - *data;
3080 }
3081
3082 buf->data = *data;
3083 buf->size = size;
3084
3085 *data += size;
3086}
3087
3088static void get_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07003089 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003090 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003091 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07003092#if CONFIG_TILE_GROUPS
3093 int r, c;
3094 const int tile_cols = cm->tile_cols;
3095 const int tile_rows = cm->tile_rows;
3096 int tc = 0;
3097 int first_tile_in_tg = 0;
3098 int hdr_offset;
3099 struct aom_read_bit_buffer rb_tg_hdr;
3100 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3101 const int num_tiles = tile_rows * tile_cols;
3102 const int num_bits = OD_ILOG(num_tiles) - 1;
3103 const int hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
3104 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
3105
3106 for (r = 0; r < tile_rows; ++r) {
3107 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07003108 TileBufferDec *const buf = &tile_buffers[r][c];
3109 hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
3110
3111 buf->col = c;
3112 if (hdr_offset) {
3113 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
3114 rb_tg_hdr.bit_offset = tg_size_bit_offset;
3115 if (num_tiles) {
3116 pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
3117 pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
3118 }
3119 }
3120 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
3121 data += hdr_offset;
Thomas Davies8fe64a32016-10-04 13:19:31 +01003122 get_tile_buffer(data_end, pbi->tile_size_bytes, 0, &pbi->common.error,
3123 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
Thomas Davies80188d12016-10-26 16:08:35 -07003124 }
3125 }
3126#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003127 int r, c;
3128 const int tile_cols = cm->tile_cols;
3129 const int tile_rows = cm->tile_rows;
3130
3131 for (r = 0; r < tile_rows; ++r) {
3132 for (c = 0; c < tile_cols; ++c) {
3133 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
3134 TileBufferDec *const buf = &tile_buffers[r][c];
3135 buf->col = c;
3136 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
3137 &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
3138 }
3139 }
Thomas Davies80188d12016-10-26 16:08:35 -07003140#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003141}
3142#endif // CONFIG_EXT_TILE
3143
Yushin Cho77bba8d2016-11-04 16:36:56 -07003144#if CONFIG_PVQ
3145static void daala_dec_init(daala_dec_ctx *daala_dec, od_ec_dec *ec) {
3146 daala_dec->ec = ec;
3147 od_adapt_ctx_reset(&daala_dec->state.adapt, 0);
3148
3149 daala_dec->qm = OD_FLAT_QM;
3150
3151 od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
3152 daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
3153}
3154#endif
3155
Yaowu Xuf883b422016-08-30 14:01:10 -07003156static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003157 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003158 AV1_COMMON *const cm = &pbi->common;
3159 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003160 const int tile_cols = cm->tile_cols;
3161 const int tile_rows = cm->tile_rows;
3162 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07003163 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003164#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003165 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003166 const int single_row = pbi->dec_tile_row >= 0;
3167 const int tile_rows_start = single_row ? dec_tile_row : 0;
3168 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003169 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003170 const int single_col = pbi->dec_tile_col >= 0;
3171 const int tile_cols_start = single_col ? dec_tile_col : 0;
3172 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3173 const int inv_col_order = pbi->inv_tile_order && !single_col;
3174 const int inv_row_order = pbi->inv_tile_order && !single_row;
3175#else
3176 const int tile_rows_start = 0;
3177 const int tile_rows_end = tile_rows;
3178 const int tile_cols_start = 0;
3179 const int tile_cols_end = tile_cols;
3180 const int inv_col_order = pbi->inv_tile_order;
3181 const int inv_row_order = pbi->inv_tile_order;
3182#endif // CONFIG_EXT_TILE
3183 int tile_row, tile_col;
3184
3185#if CONFIG_ENTROPY
3186 cm->do_subframe_update = n_tiles == 1;
3187#endif // CONFIG_ENTROPY
3188
3189 if (cm->lf.filter_level && !cm->skip_loop_filter &&
3190 pbi->lf_worker.data1 == NULL) {
3191 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07003192 aom_memalign(32, sizeof(LFWorkerData)));
3193 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003194 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003195 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003196 "Loop filter thread creation failed");
3197 }
3198 }
3199
3200 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3201 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3202 // Be sure to sync as we might be resuming after a failed frame decode.
3203 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003204 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
3205 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003206 }
3207
3208 assert(tile_rows <= MAX_TILE_ROWS);
3209 assert(tile_cols <= MAX_TILE_COLS);
3210
3211 get_tile_buffers(pbi, data, data_end, tile_buffers);
3212
3213 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003214 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003215 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003216 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003217 pbi->allocated_tiles = n_tiles;
3218 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003219#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003220 if (pbi->acct_enabled) {
3221 aom_accounting_reset(&pbi->accounting);
3222 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003223#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003224 // Load all tile information into tile_data.
3225 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3226 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3227 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
3228 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
3229
3230 td->cm = cm;
3231 td->xd = pbi->mb;
3232 td->xd.corrupted = 0;
3233 td->xd.counts =
3234 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3235 ? &cm->counts
3236 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003237 av1_zero(td->dqcoeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003238#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003239 av1_zero(td->pvq_ref_coeff);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003240#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07003241 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003242#if !CONFIG_ANS
3243 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
3244 &td->bit_reader, pbi->decrypt_cb, pbi->decrypt_state);
3245#else
3246 setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
3247 &td->bit_reader, pbi->decrypt_cb, pbi->decrypt_state);
3248#endif
Michael Bebenita6048d052016-08-25 14:40:54 -07003249#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003250 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003251 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003252 } else {
David Barkerd971f402016-10-25 13:52:07 +01003253 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003254 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003255#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07003256 av1_init_macroblockd(cm, &td->xd,
3257#if CONFIG_PVQ
3258 td->pvq_ref_coeff,
3259#endif
3260 td->dqcoeff);
3261#if CONFIG_PVQ
3262 daala_dec_init(&td->xd.daala_dec, &td->bit_reader.ec);
3263#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003264#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003265 td->xd.plane[0].color_index_map = td->color_index_map[0];
3266 td->xd.plane[1].color_index_map = td->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003267#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003268 }
3269 }
3270
3271 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3272 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
3273 int mi_row = 0;
3274 TileInfo tile_info;
3275
Yaowu Xuf883b422016-08-30 14:01:10 -07003276 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003277
3278 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3279 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
3280 TileData *const td = pbi->tile_data + tile_cols * row + col;
Michael Bebenita6048d052016-08-25 14:40:54 -07003281#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003282 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01003283 td->bit_reader.accounting->last_tell_frac =
3284 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04003285 }
Michael Bebenita6048d052016-08-25 14:40:54 -07003286#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003287
Yaowu Xuf883b422016-08-30 14:01:10 -07003288 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003289
Yaowu Xuf883b422016-08-30 14:01:10 -07003290 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003291
3292 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
3293 mi_row += cm->mib_size) {
3294 int mi_col;
3295
Yaowu Xuf883b422016-08-30 14:01:10 -07003296 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003297
3298 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
3299 mi_col += cm->mib_size) {
3300 decode_partition(pbi, &td->xd,
3301#if CONFIG_SUPERTX
3302 0,
3303#endif // CONFIG_SUPERTX
3304 mi_row, mi_col, &td->bit_reader, cm->sb_size,
3305 b_width_log2_lookup[cm->sb_size]);
3306 }
3307 pbi->mb.corrupted |= td->xd.corrupted;
3308 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07003309 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003310 "Failed to decode tile data");
3311#if CONFIG_ENTROPY
3312 if (cm->do_subframe_update &&
3313 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3314 if ((mi_row + MI_SIZE) %
3315 (MI_SIZE *
Yaowu Xuf883b422016-08-30 14:01:10 -07003316 AOMMAX(cm->mi_rows / MI_SIZE / COEF_PROBS_BUFS, 1)) ==
Yaowu Xuc27fc142016-08-22 16:08:15 -07003317 0 &&
3318 mi_row + MI_SIZE < cm->mi_rows &&
3319 cm->coef_probs_update_idx < COEF_PROBS_BUFS - 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003320 av1_partial_adapt_probs(cm, mi_row, mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003321 ++cm->coef_probs_update_idx;
3322 }
3323 }
3324#endif // CONFIG_ENTROPY
3325 }
3326 }
3327
3328 assert(mi_row > 0);
3329
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003330// when Parallel deblocking is enabled, deblocking should not
3331// be interleaved with decoding. Instead, deblocking should be done
3332// after the entire frame is decoded.
3333#if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003334 // Loopfilter one tile row.
3335 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3336 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003337 const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003338 const int lf_end = tile_info.mi_row_end - cm->mib_size;
3339
3340 // Delay the loopfilter if the first tile row is only
3341 // a single superblock high.
3342 if (lf_end <= 0) continue;
3343
3344 // Decoding has completed. Finish up the loop filter in this thread.
3345 if (tile_info.mi_row_end >= cm->mi_rows) continue;
3346
3347 winterface->sync(&pbi->lf_worker);
3348 lf_data->start = lf_start;
3349 lf_data->stop = lf_end;
3350 if (pbi->max_threads > 1) {
3351 winterface->launch(&pbi->lf_worker);
3352 } else {
3353 winterface->execute(&pbi->lf_worker);
3354 }
3355 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003356#endif // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003357
3358 // After loopfiltering, the last 7 row pixels in each superblock row may
3359 // still be changed by the longest loopfilter of the next superblock row.
3360 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003361 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003362 }
3363
3364#if CONFIG_VAR_TX
3365 // Loopfilter the whole frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003366 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
3367 cm->lf.filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003368#else
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003369#if CONFIG_PARALLEL_DEBLOCKING
3370 // Loopfilter all rows in the frame in the frame.
3371 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3372 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3373 winterface->sync(&pbi->lf_worker);
3374 lf_data->start = 0;
3375 lf_data->stop = cm->mi_rows;
3376 winterface->execute(&pbi->lf_worker);
3377 }
3378#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003379 // Loopfilter remaining rows in the frame.
3380 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3381 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
3382 winterface->sync(&pbi->lf_worker);
3383 lf_data->start = lf_data->stop;
3384 lf_data->stop = cm->mi_rows;
3385 winterface->execute(&pbi->lf_worker);
3386 }
Ryan Lei6f8c1a72016-10-26 10:52:12 -07003387#endif // CONFIG_PARALLEL_DEBLOCKING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003388#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003389 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003390 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003391
3392#if CONFIG_EXT_TILE
3393 if (n_tiles == 1) {
3394#if CONFIG_ANS
3395 return data_end;
3396#else
3397 // Find the end of the single tile buffer
Yaowu Xuf883b422016-08-30 14:01:10 -07003398 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003399#endif // CONFIG_ANS
3400 } else {
3401 // Return the end of the last tile buffer
3402 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3403 }
3404#else
3405#if CONFIG_ANS
3406 return data_end;
3407#else
3408 {
3409 // Get last tile data.
3410 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003411 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003412 }
3413#endif // CONFIG_ANS
3414#endif // CONFIG_EXT_TILE
3415}
3416
3417static int tile_worker_hook(TileWorkerData *const tile_data,
3418 const TileInfo *const tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003419 AV1Decoder *const pbi = tile_data->pbi;
3420 const AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003421 int mi_row, mi_col;
3422
3423 if (setjmp(tile_data->error_info.jmp)) {
3424 tile_data->error_info.setjmp = 0;
3425 tile_data->xd.corrupted = 1;
3426 return 0;
3427 }
3428
3429 tile_data->error_info.setjmp = 1;
3430 tile_data->xd.error_info = &tile_data->error_info;
3431
Yaowu Xuf883b422016-08-30 14:01:10 -07003432 av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003433
3434 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
3435 mi_row += cm->mib_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003436 av1_zero_left_context(&tile_data->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003437
3438 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3439 mi_col += cm->mib_size) {
3440 decode_partition(pbi, &tile_data->xd,
3441#if CONFIG_SUPERTX
3442 0,
3443#endif
3444 mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
3445 b_width_log2_lookup[cm->sb_size]);
3446 }
3447 }
3448 return !tile_data->xd.corrupted;
3449}
3450
3451// sorts in descending order
3452static int compare_tile_buffers(const void *a, const void *b) {
3453 const TileBufferDec *const buf1 = (const TileBufferDec *)a;
3454 const TileBufferDec *const buf2 = (const TileBufferDec *)b;
3455 return (int)(buf2->size - buf1->size);
3456}
3457
Yaowu Xuf883b422016-08-30 14:01:10 -07003458static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003459 const uint8_t *data_end) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003460 AV1_COMMON *const cm = &pbi->common;
3461 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003462 const int tile_cols = cm->tile_cols;
3463 const int tile_rows = cm->tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003464 const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
clang-format67948d32016-09-07 22:40:40 -07003465 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003466#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07003467 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003468 const int single_row = pbi->dec_tile_row >= 0;
3469 const int tile_rows_start = single_row ? dec_tile_row : 0;
3470 const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07003471 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003472 const int single_col = pbi->dec_tile_col >= 0;
3473 const int tile_cols_start = single_col ? dec_tile_col : 0;
3474 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3475#else
3476 const int tile_rows_start = 0;
3477 const int tile_rows_end = tile_rows;
3478 const int tile_cols_start = 0;
3479 const int tile_cols_end = tile_cols;
3480#endif // CONFIG_EXT_TILE
3481 int tile_row, tile_col;
3482 int i;
3483
3484#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3485 int final_worker = -1;
3486#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3487
3488 assert(tile_rows <= MAX_TILE_ROWS);
3489 assert(tile_cols <= MAX_TILE_COLS);
3490
3491 assert(tile_cols * tile_rows > 1);
3492
3493#if CONFIG_ANS
3494 // TODO(any): This might just work now. Needs to be tested.
3495 abort(); // FIXME: Tile parsing broken
3496#endif // CONFIG_ANS
3497
3498 // TODO(jzern): See if we can remove the restriction of passing in max
3499 // threads to the decoder.
3500 if (pbi->num_tile_workers == 0) {
3501 const int num_threads = pbi->max_threads & ~1;
3502 CHECK_MEM_ERROR(cm, pbi->tile_workers,
Yaowu Xuf883b422016-08-30 14:01:10 -07003503 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003504 // Ensure tile data offsets will be properly aligned. This may fail on
3505 // platforms without DECLARE_ALIGNED().
3506 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3507 CHECK_MEM_ERROR(
3508 cm, pbi->tile_worker_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07003509 aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003510 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
Yaowu Xuf883b422016-08-30 14:01:10 -07003511 aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003512 for (i = 0; i < num_threads; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003513 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003514 ++pbi->num_tile_workers;
3515
3516 winterface->init(worker);
3517 if (i < num_threads - 1 && !winterface->reset(worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003518 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003519 "Tile decoder thread creation failed");
3520 }
3521 }
3522 }
3523
3524 // Reset tile decoding hook
3525 for (i = 0; i < num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003526 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003527 winterface->sync(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07003528 worker->hook = (AVxWorkerHook)tile_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003529 worker->data1 = &pbi->tile_worker_data[i];
3530 worker->data2 = &pbi->tile_worker_info[i];
3531 }
3532
3533 // Initialize thread frame counts.
3534 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3535 for (i = 0; i < num_workers; ++i) {
3536 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003537 av1_zero(twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003538 }
3539 }
3540
3541 // Load tile data into tile_buffers
3542 get_tile_buffers(pbi, data, data_end, tile_buffers);
3543
3544 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3545 // Sort the buffers in this tile row based on size in descending order.
3546 qsort(&tile_buffers[tile_row][tile_cols_start],
3547 tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
3548 compare_tile_buffers);
3549
3550 // Rearrange the tile buffers in this tile row such that per-tile group
3551 // the largest, and presumably the most difficult tile will be decoded in
3552 // the main thread. This should help minimize the number of instances
3553 // where the main thread is waiting for a worker to complete.
3554 {
3555 int group_start;
3556 for (group_start = tile_cols_start; group_start < tile_cols_end;
3557 group_start += num_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003558 const int group_end = AOMMIN(group_start + num_workers, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003559 const TileBufferDec largest = tile_buffers[tile_row][group_start];
3560 memmove(&tile_buffers[tile_row][group_start],
3561 &tile_buffers[tile_row][group_start + 1],
3562 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
3563 tile_buffers[tile_row][group_end - 1] = largest;
3564 }
3565 }
3566
3567 for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
3568 // Launch workers for individual columns
3569 for (i = 0; i < num_workers && tile_col < tile_cols_end;
3570 ++i, ++tile_col) {
3571 TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
Yaowu Xuf883b422016-08-30 14:01:10 -07003572 AVxWorker *const worker = &pbi->tile_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003573 TileWorkerData *const twd = (TileWorkerData *)worker->data1;
3574 TileInfo *const tile_info = (TileInfo *)worker->data2;
3575
3576 twd->pbi = pbi;
3577 twd->xd = pbi->mb;
3578 twd->xd.corrupted = 0;
3579 twd->xd.counts =
3580 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
3581 ? &twd->counts
3582 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07003583 av1_zero(twd->dqcoeff);
3584 av1_tile_init(tile_info, cm, tile_row, buf->col);
3585 av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003586#if !CONFIG_ANS
3587 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
3588 &twd->bit_reader, pbi->decrypt_cb,
3589 pbi->decrypt_state);
3590#else
3591 setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
3592 &twd->bit_reader, pbi->decrypt_cb,
3593 pbi->decrypt_state);
3594#endif // CONFIG_ANS
Yushin Cho77bba8d2016-11-04 16:36:56 -07003595 av1_init_macroblockd(cm, &twd->xd,
3596#if CONFIG_PVQ
3597 twd->pvq_ref_coeff,
3598#endif
3599 twd->dqcoeff);
3600#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08003601 daala_dec_init(&twd->xd.daala_dec, &twd->bit_reader.ec);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003602#endif
Urvang Joshib100db72016-10-12 16:28:56 -07003603#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003604 twd->xd.plane[0].color_index_map = twd->color_index_map[0];
3605 twd->xd.plane[1].color_index_map = twd->color_index_map[1];
Urvang Joshib100db72016-10-12 16:28:56 -07003606#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003607
3608 worker->had_error = 0;
3609 if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
3610 winterface->execute(worker);
3611 } else {
3612 winterface->launch(worker);
3613 }
3614
3615#if !(CONFIG_ANS || CONFIG_EXT_TILE)
3616 if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
3617 final_worker = i;
3618 }
3619#endif // !(CONFIG_ANS || CONFIG_EXT_TILE)
3620 }
3621
3622 // Sync all workers
3623 for (; i > 0; --i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003624 AVxWorker *const worker = &pbi->tile_workers[i - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003625 // TODO(jzern): The tile may have specific error data associated with
Yaowu Xuf883b422016-08-30 14:01:10 -07003626 // its aom_internal_error_info which could be propagated to the main
Yaowu Xuc27fc142016-08-22 16:08:15 -07003627 // info in cm. Additionally once the threads have been synced and an
3628 // error is detected, there's no point in continuing to decode tiles.
3629 pbi->mb.corrupted |= !winterface->sync(worker);
3630 }
3631 }
3632 }
3633
3634 // Accumulate thread frame counts.
3635 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3636 for (i = 0; i < num_workers; ++i) {
3637 TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003638 av1_accumulate_frame_counts(cm, &twd->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003639 }
3640 }
3641
3642#if CONFIG_EXT_TILE
3643 // Return the end of the last tile buffer
3644 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
3645#else
3646#if CONFIG_ANS
3647 return data_end;
3648#else
3649 assert(final_worker != -1);
3650 {
3651 TileWorkerData *const twd =
3652 (TileWorkerData *)pbi->tile_workers[final_worker].data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003653 return aom_reader_find_end(&twd->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003654 }
3655#endif // CONFIG_ANS
3656#endif // CONFIG_EXT_TILE
3657}
3658
3659static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003660 AV1_COMMON *const cm = (AV1_COMMON *)data;
3661 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003662}
3663
Yaowu Xuf883b422016-08-30 14:01:10 -07003664static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
3665 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003666 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003667 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
3668#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003669 cm->use_highbitdepth = 1;
3670#endif
3671 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003672 cm->bit_depth = AOM_BITS_8;
3673#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003674 cm->use_highbitdepth = 0;
3675#endif
3676 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003677 cm->color_space = aom_rb_read_literal(rb, 3);
3678 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003679 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07003680 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003681 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003682 cm->subsampling_x = aom_rb_read_bit(rb);
3683 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003684 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003685 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003686 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07003687 if (aom_rb_read_bit(rb))
3688 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003689 "Reserved bit set");
3690 } else {
3691 cm->subsampling_y = cm->subsampling_x = 1;
3692 }
3693 } else {
3694 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
3695 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
3696 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
3697 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003698 if (aom_rb_read_bit(rb))
3699 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003700 "Reserved bit set");
3701 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003702 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003703 "4:4:4 color not supported in profile 0 or 2");
3704 }
3705 }
3706}
3707
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003708#if CONFIG_REFERENCE_BUFFER
3709void read_sequence_header(SequenceHeader *seq_params) {
3710 /* Placeholder for actually reading from the bitstream */
3711 seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
3712 seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
3713 seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
3714}
3715#endif
3716
Yaowu Xuf883b422016-08-30 14:01:10 -07003717static size_t read_uncompressed_header(AV1Decoder *pbi,
3718 struct aom_read_bit_buffer *rb) {
3719 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003720 MACROBLOCKD *const xd = &pbi->mb;
3721 BufferPool *const pool = cm->buffer_pool;
3722 RefCntBuffer *const frame_bufs = pool->frame_bufs;
3723 int i, mask, ref_index = 0;
3724 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003725
3726#if CONFIG_REFERENCE_BUFFER
3727 /* TODO: Move outside frame loop or inside key-frame branch */
3728 read_sequence_header(&pbi->seq_params);
3729#endif
3730
Yaowu Xuc27fc142016-08-22 16:08:15 -07003731 cm->last_frame_type = cm->frame_type;
3732 cm->last_intra_only = cm->intra_only;
3733
3734#if CONFIG_EXT_REFS
3735 // NOTE: By default all coded frames to be used as a reference
3736 cm->is_reference_frame = 1;
3737#endif // CONFIG_EXT_REFS
3738
Yaowu Xuf883b422016-08-30 14:01:10 -07003739 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
3740 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003741 "Invalid frame marker");
3742
Yaowu Xuf883b422016-08-30 14:01:10 -07003743 cm->profile = av1_read_profile(rb);
3744#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003745 if (cm->profile >= MAX_PROFILES)
Yaowu Xuf883b422016-08-30 14:01:10 -07003746 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003747 "Unsupported bitstream profile");
3748#else
3749 if (cm->profile >= PROFILE_2)
Yaowu Xuf883b422016-08-30 14:01:10 -07003750 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003751 "Unsupported bitstream profile");
3752#endif
3753
Yaowu Xuf883b422016-08-30 14:01:10 -07003754 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003755
3756 if (cm->show_existing_frame) {
3757 // Show an existing frame directly.
Yaowu Xuf883b422016-08-30 14:01:10 -07003758 const int frame_to_show = cm->ref_frame_map[aom_rb_read_literal(rb, 3)];
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003759#if CONFIG_REFERENCE_BUFFER
3760 if (pbi->seq_params.frame_id_numbers_present_flag) {
3761 int FidLen = pbi->seq_params.frame_id_length_minus7 + 7;
3762 int display_frame_id = aom_rb_read_literal(rb, FidLen);
3763 /* Compare display_frame_id with ref_frame_id and check valid for
3764 * referencing */
3765 if (display_frame_id != cm->ref_frame_id[frame_to_show] ||
3766 cm->valid_for_referencing[frame_to_show] == 0)
3767 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3768 "Reference buffer frame ID mismatch");
3769 }
3770#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003771 lock_buffer_pool(pool);
3772 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
3773 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07003774 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003775 "Buffer %d does not contain a decoded frame",
3776 frame_to_show);
3777 }
3778 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
3779 unlock_buffer_pool(pool);
3780
3781 cm->lf.filter_level = 0;
3782 cm->show_frame = 1;
3783 pbi->refresh_frame_flags = 0;
3784
3785 if (cm->frame_parallel_decode) {
3786 for (i = 0; i < REF_FRAMES; ++i)
3787 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
3788 }
3789
3790 return 0;
3791 }
3792
Yaowu Xuf883b422016-08-30 14:01:10 -07003793 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
3794 cm->show_frame = aom_rb_read_bit(rb);
3795 cm->error_resilient_mode = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003796#if CONFIG_REFERENCE_BUFFER
3797 if (pbi->seq_params.frame_id_numbers_present_flag) {
3798 int FidLen = pbi->seq_params.frame_id_length_minus7 + 7;
3799 int DiffLen = pbi->seq_params.delta_frame_id_length_minus2 + 2;
3800 int PrevFrameId = 0;
3801 if (cm->frame_type != KEY_FRAME) {
3802 PrevFrameId = cm->current_frame_id;
3803 }
3804 cm->current_frame_id = aom_rb_read_literal(rb, FidLen);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003805
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003806 if (cm->frame_type != KEY_FRAME) {
3807 int DiffFrameID;
3808 if (cm->current_frame_id > PrevFrameId) {
3809 DiffFrameID = cm->current_frame_id - PrevFrameId;
3810 } else {
3811 DiffFrameID = (1 << FidLen) + cm->current_frame_id - PrevFrameId;
3812 }
3813 /* Check current_frame_id for conformance */
3814 if (PrevFrameId == cm->current_frame_id ||
3815 DiffFrameID >= (1 << (FidLen - 1))) {
3816 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3817 "Invalid value of current_frame_id");
3818 }
3819 }
3820 /* Check if some frames need to be marked as not valid for referencing */
3821 for (i = 0; i < REF_FRAMES; i++) {
3822 if (cm->frame_type == KEY_FRAME) {
3823 cm->valid_for_referencing[i] = 0;
3824 } else if (cm->current_frame_id - (1 << DiffLen) > 0) {
3825 if (cm->ref_frame_id[i] > cm->current_frame_id ||
3826 cm->ref_frame_id[i] < cm->current_frame_id - (1 << DiffLen))
3827 cm->valid_for_referencing[i] = 0;
3828 } else {
3829 if (cm->ref_frame_id[i] > cm->current_frame_id &&
3830 cm->ref_frame_id[i] <
3831 (1 << FidLen) + cm->current_frame_id - (1 << DiffLen))
3832 cm->valid_for_referencing[i] = 0;
3833 }
3834 }
3835 }
3836#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003837 if (cm->frame_type == KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003838 if (!av1_read_sync_code(rb))
3839 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003840 "Invalid frame sync code");
3841
3842 read_bitdepth_colorspace_sampling(cm, rb);
3843 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
3844
3845 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3846 cm->frame_refs[i].idx = INVALID_IDX;
3847 cm->frame_refs[i].buf = NULL;
3848 }
3849
3850 setup_frame_size(cm, rb);
3851 if (pbi->need_resync) {
3852 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
3853 pbi->need_resync = 0;
3854 }
Urvang Joshib100db72016-10-12 16:28:56 -07003855#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07003856 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003857#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003858 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003859 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003860#if CONFIG_PALETTE
hui su24f7b072016-10-12 11:36:24 -07003861 if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Urvang Joshib100db72016-10-12 16:28:56 -07003862#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07003863 if (cm->error_resilient_mode) {
3864 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
3865 } else {
3866 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003867 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003868 ? RESET_FRAME_CONTEXT_ALL
3869 : RESET_FRAME_CONTEXT_CURRENT;
3870 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003871 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003872 ? RESET_FRAME_CONTEXT_CURRENT
3873 : RESET_FRAME_CONTEXT_NONE;
3874 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07003875 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003876 ? RESET_FRAME_CONTEXT_ALL
3877 : RESET_FRAME_CONTEXT_CURRENT;
3878 }
3879 }
3880
3881 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003882 if (!av1_read_sync_code(rb))
3883 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003884 "Invalid frame sync code");
3885
3886 read_bitdepth_colorspace_sampling(cm, rb);
3887
Yaowu Xuf883b422016-08-30 14:01:10 -07003888 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003889 setup_frame_size(cm, rb);
3890 if (pbi->need_resync) {
3891 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
3892 pbi->need_resync = 0;
3893 }
3894 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Yaowu Xuf883b422016-08-30 14:01:10 -07003895 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003896
3897#if CONFIG_EXT_REFS
3898 if (!pbi->refresh_frame_flags) {
3899 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
3900 // will not be used as a reference
3901 cm->is_reference_frame = 0;
3902 }
3903#endif // CONFIG_EXT_REFS
3904
3905 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003906 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003907 const int idx = cm->ref_frame_map[ref];
3908 RefBuffer *const ref_frame = &cm->frame_refs[i];
3909 ref_frame->idx = idx;
3910 ref_frame->buf = &frame_bufs[idx].buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07003911 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003912#if CONFIG_REFERENCE_BUFFER
3913 if (pbi->seq_params.frame_id_numbers_present_flag) {
3914 int FidLen = pbi->seq_params.frame_id_length_minus7 + 7;
3915 int DiffLen = pbi->seq_params.delta_frame_id_length_minus2 + 2;
3916 int delta_frame_id_minus1 = aom_rb_read_literal(rb, DiffLen);
3917 int refFrameId = ((cm->current_frame_id -
3918 (delta_frame_id_minus1 + 1) + (1 << FidLen)) %
3919 (1 << FidLen));
3920 /* Compare values derived from delta_frame_id_minus1 and
3921 * refresh_frame_flags. Also, check valid for referencing */
3922 if (refFrameId != cm->ref_frame_id[ref] ||
3923 cm->valid_for_referencing[ref] == 0)
3924 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3925 "Reference buffer frame ID mismatch");
3926 }
3927#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003928 }
3929
Arild Fuldseth842e9b02016-09-02 13:00:05 +02003930#if CONFIG_FRAME_SIZE
3931 if (cm->error_resilient_mode == 0) {
3932 setup_frame_size_with_refs(cm, rb);
3933 } else {
3934 setup_frame_size(cm, rb);
3935 }
3936#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003937 setup_frame_size_with_refs(cm, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02003938#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003939
Yaowu Xuf883b422016-08-30 14:01:10 -07003940 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Angie Chiang5678ad92016-11-21 09:38:40 -08003941 cm->interp_filter = read_frame_interp_filter(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003942
3943 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3944 RefBuffer *const ref_buf = &cm->frame_refs[i];
Yaowu Xuf883b422016-08-30 14:01:10 -07003945#if CONFIG_AOM_HIGHBITDEPTH
3946 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003947 &ref_buf->sf, ref_buf->buf->y_crop_width,
3948 ref_buf->buf->y_crop_height, cm->width, cm->height,
3949 cm->use_highbitdepth);
3950#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003951 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003952 &ref_buf->sf, ref_buf->buf->y_crop_width,
3953 ref_buf->buf->y_crop_height, cm->width, cm->height);
3954#endif
3955 }
3956 }
3957 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003958
3959#if CONFIG_REFERENCE_BUFFER
3960 if (pbi->seq_params.frame_id_numbers_present_flag) {
3961 /* If bitmask is set, update reference frame id values and
3962 mark frames as valid for reference */
3963 int refresh_frame_flags =
3964 cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
3965 for (i = 0; i < REF_FRAMES; i++) {
3966 if ((refresh_frame_flags >> i) & 1) {
3967 cm->ref_frame_id[i] = cm->current_frame_id;
3968 cm->valid_for_referencing[i] = 1;
3969 }
3970 }
3971 }
3972#endif
3973
Yaowu Xuf883b422016-08-30 14:01:10 -07003974#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003975 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
3976#endif
3977 get_frame_new_buffer(cm)->color_space = cm->color_space;
3978 get_frame_new_buffer(cm)->color_range = cm->color_range;
3979 get_frame_new_buffer(cm)->render_width = cm->render_width;
3980 get_frame_new_buffer(cm)->render_height = cm->render_height;
3981
3982 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003983 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003984 "Keyframe / intra-only frame required to reset decoder"
3985 " state");
3986 }
3987
3988 if (!cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003989 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003990 ? REFRESH_FRAME_CONTEXT_FORWARD
3991 : REFRESH_FRAME_CONTEXT_BACKWARD;
3992 } else {
3993 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
3994 }
3995
Yaowu Xuf883b422016-08-30 14:01:10 -07003996 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07003997 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07003998 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003999
4000 // Generate next_ref_frame_map.
4001 lock_buffer_pool(pool);
4002 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
4003 if (mask & 1) {
4004 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
4005 ++frame_bufs[cm->new_fb_idx].ref_count;
4006 } else {
4007 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4008 }
4009 // Current thread holds the reference frame.
4010 if (cm->ref_frame_map[ref_index] >= 0)
4011 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4012 ++ref_index;
4013 }
4014
4015 for (; ref_index < REF_FRAMES; ++ref_index) {
4016 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
4017
4018 // Current thread holds the reference frame.
4019 if (cm->ref_frame_map[ref_index] >= 0)
4020 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
4021 }
4022 unlock_buffer_pool(pool);
4023 pbi->hold_ref_buf = 1;
4024
4025 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07004026 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004027
4028#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07004029 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004030#else
4031 set_sb_size(cm, BLOCK_64X64);
4032#endif // CONFIG_EXT_PARTITION
4033
4034 setup_loopfilter(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004035#if CONFIG_DERING
4036 setup_dering(cm, rb);
4037#endif
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004038#if CONFIG_CLPF
4039 setup_clpf(pbi, rb);
4040#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004041#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004042 decode_restoration_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004043#endif // CONFIG_LOOP_RESTORATION
4044 setup_quantization(cm, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004045#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004046 xd->bd = (int)cm->bit_depth;
4047#endif
4048
4049#if CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004050 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004051 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4052 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4053 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4054 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
4055 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4056 }
4057#endif // CONFIG_ENTROPY
4058
4059 setup_segmentation(cm, rb);
4060
Arild Fuldseth07441162016-08-15 15:07:52 +02004061#if CONFIG_DELTA_Q
4062 {
4063 struct segmentation *const seg = &cm->seg;
4064 int segment_quantizer_active = 0;
4065 for (i = 0; i < MAX_SEGMENTS; i++) {
4066 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
4067 segment_quantizer_active = 1;
4068 }
4069 }
4070
Thomas Daviesf6936102016-09-05 16:51:31 +01004071 cm->delta_q_res = 1;
Arild Fuldseth07441162016-08-15 15:07:52 +02004072 if (segment_quantizer_active == 0) {
4073 cm->delta_q_present_flag = aom_rb_read_bit(rb);
4074 } else {
4075 cm->delta_q_present_flag = 0;
4076 }
4077 if (cm->delta_q_present_flag) {
4078 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01004079 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Arild Fuldseth07441162016-08-15 15:07:52 +02004080 }
4081 }
4082#endif
4083
Urvang Joshi454280d2016-10-14 16:51:44 -07004084 for (i = 0; i < MAX_SEGMENTS; ++i) {
4085 const int qindex = cm->seg.enabled
4086 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
4087 : cm->base_qindex;
4088 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
4089 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4090 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004091 }
4092
4093 setup_segmentation_dequant(cm);
4094 cm->tx_mode =
4095 (!cm->seg.enabled && xd->lossless[0]) ? ONLY_4X4 : read_tx_mode(rb);
4096 cm->reference_mode = read_frame_reference_mode(cm, rb);
4097
4098 read_tile_info(pbi, rb);
Yaowu Xuf883b422016-08-30 14:01:10 -07004099 sz = aom_rb_read_literal(rb, 16);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004100
4101 if (sz == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07004102 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004103 "Invalid header size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004104 return sz;
4105}
4106
4107#if CONFIG_EXT_TX
Thomas9ac55082016-09-23 18:04:17 +01004108#if !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuf883b422016-08-30 14:01:10 -07004109static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004110 int i, j, k;
4111 int s;
4112 for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004113 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004114 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4115 if (!use_inter_ext_tx_for_txsize[s][i]) continue;
4116 for (j = 0; j < num_ext_tx_set_inter[s] - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004117 av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004118 }
4119 }
4120 }
4121
4122 for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004123 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004124 for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
4125 if (!use_intra_ext_tx_for_txsize[s][i]) continue;
4126 for (j = 0; j < INTRA_MODES; ++j)
4127 for (k = 0; k < num_ext_tx_set_intra[s] - 1; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004128 av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
4129 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004130 }
4131 }
4132 }
4133}
Thomas9ac55082016-09-23 18:04:17 +01004134#endif // !CONFIG_EC_ADAPT || !CONFIG_DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004135#else
4136
Yaowu Xuc27fc142016-08-22 16:08:15 -07004137#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07004138#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07004139static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004140 int i, j;
Michael Bebenita6048d052016-08-25 14:40:54 -07004141 if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004142 for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
4143 for (j = 1; j < TX_SIZES; ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004144 av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004145 }
4146 }
4147 }
4148}
4149#endif // CONFIG_SUPERTX
4150
4151#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +00004152static void read_global_motion_params(WarpedMotionParams *params,
Yaowu Xuf883b422016-08-30 14:01:10 -07004153 aom_prob *probs, aom_reader *r) {
David Barkercf3d0b02016-11-10 10:14:49 +00004154 TransformationType type =
Michael Bebenita6048d052016-08-25 14:40:54 -07004155 aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004156 set_default_gmparams(params);
David Barkercf3d0b02016-11-10 10:14:49 +00004157 params->wmtype = type;
4158 switch (type) {
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004159 case HOMOGRAPHY:
4160 params->wmmat[6] = aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004161 GM_ROW3HOMO_DECODE_FACTOR;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004162 params->wmmat[7] = aom_read_primitive_symmetric(r, GM_ABS_ROW3HOMO_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004163 GM_ROW3HOMO_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004164 case AFFINE:
4165 case ROTZOOM:
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004166 params->wmmat[2] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004167 GM_ALPHA_DECODE_FACTOR +
David Barkercf3d0b02016-11-10 10:14:49 +00004168 (1 << WARPEDMODEL_PREC_BITS);
4169 params->wmmat[3] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4170 GM_ALPHA_DECODE_FACTOR;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004171 if (type == AFFINE || type == HOMOGRAPHY) {
4172 params->wmmat[4] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
Debargha Mukherjee949097c2016-11-15 17:27:38 -08004173 GM_ALPHA_DECODE_FACTOR;
David Barkercf3d0b02016-11-10 10:14:49 +00004174 params->wmmat[5] = aom_read_primitive_symmetric(r, GM_ABS_ALPHA_BITS) *
4175 GM_ALPHA_DECODE_FACTOR +
4176 (1 << WARPEDMODEL_PREC_BITS);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004177 } else {
David Barkercf3d0b02016-11-10 10:14:49 +00004178 params->wmmat[4] = -params->wmmat[3];
4179 params->wmmat[5] = params->wmmat[2];
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004180 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004181 // fallthrough intended
David Barkercf3d0b02016-11-10 10:14:49 +00004182 case TRANSLATION:
4183 params->wmmat[0] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4184 GM_TRANS_DECODE_FACTOR;
4185 params->wmmat[1] = aom_read_primitive_symmetric(r, GM_ABS_TRANS_BITS) *
4186 GM_TRANS_DECODE_FACTOR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004187 break;
Debargha Mukherjee3fb33f02016-11-12 10:43:50 -08004188 case IDENTITY: break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004189 default: assert(0);
4190 }
4191}
4192
Yaowu Xuf883b422016-08-30 14:01:10 -07004193static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004194 int frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004195 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
4196 read_global_motion_params(&cm->global_motion[frame],
4197 cm->fc->global_motion_types_prob, r);
Sarah Parkere5299862016-08-16 14:57:37 -07004198 /*
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004199 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4200 frame, cm->current_video_frame, cm->show_frame,
David Barkercf3d0b02016-11-10 10:14:49 +00004201 cm->global_motion[frame].wmmat[0],
4202 cm->global_motion[frame].wmmat[1],
4203 cm->global_motion[frame].wmmat[2],
4204 cm->global_motion[frame].wmmat[3]);
Debargha Mukherjee8db4c772016-11-07 12:54:21 -08004205 */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004206 }
4207}
4208#endif // CONFIG_GLOBAL_MOTION
4209
Yaowu Xuf883b422016-08-30 14:01:10 -07004210static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004211 size_t partition_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004212 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004213#if CONFIG_SUPERTX
4214 MACROBLOCKD *const xd = &pbi->mb;
4215#endif
4216 FRAME_CONTEXT *const fc = cm->fc;
Yaowu Xuf883b422016-08-30 14:01:10 -07004217 aom_reader r;
Yaowu Xu8af861b2016-11-01 12:12:11 -07004218 int k, i;
Yaowu Xud0af64f2016-11-17 12:50:47 -08004219#if !CONFIG_EC_ADAPT || CONFIG_EXT_INTRA
Yaowu Xu8af861b2016-11-01 12:12:11 -07004220 int j;
4221#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004222
4223#if !CONFIG_ANS
Yaowu Xuf883b422016-08-30 14:01:10 -07004224 if (aom_reader_init(&r, data, partition_size, pbi->decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004225 pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07004226 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004227 "Failed to allocate bool decoder 0");
4228#else
Yaowu Xuf7ae12d2016-09-01 08:59:46 -07004229 if (ans_read_init(&r, data, (int)partition_size))
Yaowu Xuf883b422016-08-30 14:01:10 -07004230 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004231 "Failed to allocate compressed header ANS decoder");
4232#endif // !CONFIG_ANS
4233
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07004234#if CONFIG_LOOP_RESTORATION
4235 decode_restoration(cm, &r);
4236#endif
4237
Yaowu Xuefc75352016-10-31 09:46:42 -07004238 if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004239
Yushin Cho77bba8d2016-11-04 16:36:56 -07004240#if !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004241 read_coef_probs(fc, cm->tx_mode, &r);
4242
4243#if CONFIG_VAR_TX
4244 for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004245 av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
Yue Chena1e48dc2016-08-29 17:29:33 -07004246#if CONFIG_EXT_TX && CONFIG_RECT_TX
4247 if (cm->tx_mode == TX_MODE_SELECT) {
Jingning Han607fa6a2016-10-26 10:46:28 -07004248 for (i = 1; i < MAX_TX_DEPTH; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004249 av1_diff_update_prob(&r, &fc->rect_tx_prob[i], ACCT_STR);
Yue Chena1e48dc2016-08-29 17:29:33 -07004250 }
4251#endif // CONFIG_EXT_TX && CONFIG_RECT_TX
Yushin Cho77bba8d2016-11-04 16:36:56 -07004252#endif // CONFIG_VAR_TX
4253#endif // !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004254 for (k = 0; k < SKIP_CONTEXTS; ++k)
Michael Bebenita6048d052016-08-25 14:40:54 -07004255 av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004256
Thomas Daviesf6936102016-09-05 16:51:31 +01004257#if CONFIG_DELTA_Q
4258 for (k = 0; k < DELTA_Q_CONTEXTS; ++k)
4259 av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
4260#endif
4261
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004262#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004263 if (cm->seg.enabled && cm->seg.update_map) {
4264 if (cm->seg.temporal_update) {
4265 for (k = 0; k < PREDICTION_PROBS; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004266 av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004267 }
4268 for (k = 0; k < MAX_SEGMENTS - 1; k++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004269 av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004270 }
4271
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004272 for (j = 0; j < INTRA_MODES; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004273 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004274 av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
Nathan E. Egge380cb1a2016-09-08 10:13:42 -04004275 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004276
4277#if CONFIG_EXT_PARTITION_TYPES
4278 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004279 av1_diff_update_prob(&r, &fc->partition_prob[0][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004280 for (j = 1; j < PARTITION_CONTEXTS; ++j)
4281 for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004282 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004283#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004284 for (j = 0; j < PARTITION_CONTEXTS; ++j)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004285 for (i = 0; i < PARTITION_TYPES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004286 av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004287#endif // CONFIG_EXT_PARTITION_TYPES
Thomas9ac55082016-09-23 18:04:17 +01004288#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004289#if CONFIG_EXT_INTRA
4290 for (i = 0; i < INTRA_FILTERS + 1; ++i)
4291 for (j = 0; j < INTRA_FILTERS - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004292 av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
Thomas Davies6519beb2016-10-19 14:46:07 +01004293#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004294
4295 if (frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004296 av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004297#if CONFIG_DAALA_EC
4298 av1_copy(cm->kf_y_cdf, av1_kf_y_mode_cdf);
4299#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004300#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004301 for (k = 0; k < INTRA_MODES; k++)
Thomas Davies6519beb2016-10-19 14:46:07 +01004302 for (j = 0; j < INTRA_MODES; j++)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004303 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004304 av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -04004305#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004306 } else {
4307#if !CONFIG_REF_MV
4308 nmv_context *const nmvc = &fc->nmvc;
4309#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004310 read_inter_mode_probs(fc, &r);
4311
4312#if CONFIG_EXT_INTER
4313 read_inter_compound_mode_probs(fc, &r);
4314 if (cm->reference_mode != COMPOUND_REFERENCE) {
4315 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4316 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004317 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004318 }
4319 }
4320 for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
4321 for (j = 0; j < INTERINTRA_MODES - 1; j++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004322 av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004323 }
4324 for (i = 0; i < BLOCK_SIZES; i++) {
4325 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07004326 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004327 }
4328 }
4329 }
4330 if (cm->reference_mode != SINGLE_REFERENCE) {
4331 for (i = 0; i < BLOCK_SIZES; i++) {
4332 if (is_interinter_wedge_used(i)) {
Sarah Parker6fddd182016-11-10 20:57:20 -08004333 for (j = 0; j < COMPOUND_TYPES - 1; j++) {
4334 av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
4335 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004336 }
4337 }
4338 }
4339#endif // CONFIG_EXT_INTER
4340
Yue Chencb60b182016-10-13 15:18:22 -07004341#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004342 for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
Yue Chencb60b182016-10-13 15:18:22 -07004343 for (j = 0; j < MOTION_MODES - 1; ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -07004344 av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004345 }
Yue Chencb60b182016-10-13 15:18:22 -07004346#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004347
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004348#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004349 if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004350#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004351
4352 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07004353 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004354
4355 if (cm->reference_mode != SINGLE_REFERENCE)
4356 setup_compound_reference_mode(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004357 read_frame_reference_mode_probs(cm, &r);
4358
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004359#if !CONFIG_EC_ADAPT
Nathan E. Egge5710c722016-09-08 10:01:16 -04004360 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004361 for (i = 0; i < INTRA_MODES - 1; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -07004362 av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
Nathan E. Egge5710c722016-09-08 10:01:16 -04004363 }
Thomas9ac55082016-09-23 18:04:17 +01004364#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004365
4366#if CONFIG_REF_MV
4367 for (i = 0; i < NMV_CONTEXTS; ++i)
4368 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
4369#else
4370 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
4371#endif
Nathan E. Eggebaaaa162016-10-24 09:50:52 -04004372#if !CONFIG_EC_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -07004373 read_ext_tx_probs(fc, &r);
Thomas9ac55082016-09-23 18:04:17 +01004374#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004375#if CONFIG_SUPERTX
4376 if (!xd->lossless[0]) read_supertx_probs(fc, &r);
4377#endif
4378#if CONFIG_GLOBAL_MOTION
4379 read_global_motion(cm, &r);
Thomas Davies6519beb2016-10-19 14:46:07 +01004380#endif // EC_ADAPT, DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004381 }
Thomas Davies6519beb2016-10-19 14:46:07 +01004382#if CONFIG_EC_MULTISYMBOL
4383 av1_coef_pareto_cdfs(fc);
David Barker599dfd02016-11-10 13:20:12 +00004384#if CONFIG_REF_MV
4385 for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
4386#else
Thomas Davies6519beb2016-10-19 14:46:07 +01004387 av1_set_mv_cdfs(&fc->nmvc);
David Barker599dfd02016-11-10 13:20:12 +00004388#endif
Thomas Davies6519beb2016-10-19 14:46:07 +01004389#if CONFIG_DAALA_EC
4390 av1_set_mode_cdfs(cm);
4391#endif
4392#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004393
Yaowu Xuf883b422016-08-30 14:01:10 -07004394 return aom_reader_has_error(&r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004395}
4396
4397#ifdef NDEBUG
4398#define debug_check_frame_counts(cm) (void)0
4399#else // !NDEBUG
4400// Counts should only be incremented when frame_parallel_decoding_mode and
4401// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07004402static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004403 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07004404 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004405 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
4406 cm->error_resilient_mode);
4407 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
4408 sizeof(cm->counts.y_mode)));
4409 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
4410 sizeof(cm->counts.uv_mode)));
4411 assert(!memcmp(cm->counts.partition, zero_counts.partition,
4412 sizeof(cm->counts.partition)));
4413 assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
4414 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
4415 sizeof(cm->counts.eob_branch)));
4416 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
4417 sizeof(cm->counts.switchable_interp)));
4418 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
4419 sizeof(cm->counts.inter_mode)));
4420#if CONFIG_EXT_INTER
4421 assert(!memcmp(cm->counts.inter_compound_mode,
4422 zero_counts.inter_compound_mode,
4423 sizeof(cm->counts.inter_compound_mode)));
4424 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
4425 sizeof(cm->counts.interintra)));
4426 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
4427 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08004428 assert(!memcmp(cm->counts.compound_interinter,
4429 zero_counts.compound_interinter,
4430 sizeof(cm->counts.compound_interinter)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004431#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07004432#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
4433 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
4434 sizeof(cm->counts.motion_mode)));
4435#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07004436 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
4437 sizeof(cm->counts.intra_inter)));
4438 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
4439 sizeof(cm->counts.comp_inter)));
4440 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
4441 sizeof(cm->counts.single_ref)));
4442 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
4443 sizeof(cm->counts.comp_ref)));
4444#if CONFIG_EXT_REFS
4445 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
4446 sizeof(cm->counts.comp_bwdref)));
4447#endif // CONFIG_EXT_REFS
4448 assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
4449 sizeof(cm->counts.tx_size)));
4450 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
4451#if CONFIG_REF_MV
4452 assert(
4453 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
4454 assert(
4455 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
4456#else
4457 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
4458#endif
4459 assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
4460 sizeof(cm->counts.inter_ext_tx)));
4461 assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
4462 sizeof(cm->counts.intra_ext_tx)));
4463}
4464#endif // NDEBUG
4465
Yaowu Xuf883b422016-08-30 14:01:10 -07004466static struct aom_read_bit_buffer *init_read_bit_buffer(
4467 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
4468 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004469 rb->bit_offset = 0;
4470 rb->error_handler = error_handler;
4471 rb->error_handler_data = &pbi->common;
4472 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004473 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004474 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
4475 rb->bit_buffer = clear_data;
4476 rb->bit_buffer_end = clear_data + n;
4477 } else {
4478 rb->bit_buffer = data;
4479 rb->bit_buffer_end = data_end;
4480 }
4481 return rb;
4482}
4483
4484//------------------------------------------------------------------------------
4485
Yaowu Xuf883b422016-08-30 14:01:10 -07004486int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
4487 return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
4488 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
4489 aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004490}
4491
Yaowu Xuf883b422016-08-30 14:01:10 -07004492void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
4493 int *height) {
4494 *width = aom_rb_read_literal(rb, 16) + 1;
4495 *height = aom_rb_read_literal(rb, 16) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004496}
4497
Yaowu Xuf883b422016-08-30 14:01:10 -07004498BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
4499 int profile = aom_rb_read_bit(rb);
4500 profile |= aom_rb_read_bit(rb) << 1;
4501 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004502 return (BITSTREAM_PROFILE)profile;
4503}
4504
Yaowu Xuf883b422016-08-30 14:01:10 -07004505void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
4506 const uint8_t *data_end, const uint8_t **p_data_end) {
4507 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004508 MACROBLOCKD *const xd = &pbi->mb;
Yaowu Xuf883b422016-08-30 14:01:10 -07004509 struct aom_read_bit_buffer rb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004510 int context_updated = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004511 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004512 size_t first_partition_size;
4513 YV12_BUFFER_CONFIG *new_fb;
4514
4515#if CONFIG_BITSTREAM_DEBUG
4516 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
4517#endif
4518
4519 first_partition_size = read_uncompressed_header(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004520 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
Thomas Davies72712e62016-11-09 12:17:51 +00004521#if CONFIG_TILE_GROUPS
4522 pbi->first_partition_size = first_partition_size;
4523 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
4524#endif
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07004525 new_fb = get_frame_new_buffer(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004526 xd->cur_buf = new_fb;
4527#if CONFIG_GLOBAL_MOTION
4528 xd->global_motion = cm->global_motion;
4529#endif // CONFIG_GLOBAL_MOTION
4530
4531 if (!first_partition_size) {
4532// showing a frame directly
4533#if CONFIG_EXT_REFS
4534 if (cm->show_existing_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07004535 *p_data_end = data + aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004536 else
4537#endif // CONFIG_EXT_REFS
4538 *p_data_end = data + (cm->profile <= PROFILE_2 ? 1 : 2);
4539
4540 return;
4541 }
4542
Yaowu Xuf883b422016-08-30 14:01:10 -07004543 data += aom_rb_bytes_read(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004544 if (!read_is_valid(data, first_partition_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07004545 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004546 "Truncated packet or corrupt header length");
4547
Jingning Han24e0a182016-11-20 22:34:12 -08004548#if CONFIG_REF_MV
Dengca8d24d2016-10-17 14:06:35 +08004549 cm->setup_mi(cm);
4550#endif
4551
Yaowu Xuc27fc142016-08-22 16:08:15 -07004552 cm->use_prev_frame_mvs =
4553 !cm->error_resilient_mode && cm->width == cm->last_width &&
4554 cm->height == cm->last_height && !cm->last_intra_only &&
4555 cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
4556#if CONFIG_EXT_REFS
4557 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
4558 // show_exisiting_frame=1, nor can it take a frame not used as
4559 // a reference, it is probable that by the time it is being
4560 // referred to, the frame buffer it originally points to may
4561 // already get expired and have been reassigned to the current
4562 // newly coded frame. Hence, we need to check whether this is
4563 // the case, and if yes, we have 2 choices:
4564 // (1) Simply disable the use of previous frame mvs; or
4565 // (2) Have cm->prev_frame point to one reference frame buffer,
4566 // e.g. LAST_FRAME.
4567 if (cm->use_prev_frame_mvs && !dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
4568 // Reassign the LAST_FRAME buffer to cm->prev_frame.
4569 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
4570 cm->prev_frame = &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx];
4571 }
4572#endif // CONFIG_EXT_REFS
4573
Yaowu Xuf883b422016-08-30 14:01:10 -07004574 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004575
4576 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
4577 if (!cm->fc->initialized)
Yaowu Xuf883b422016-08-30 14:01:10 -07004578 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004579 "Uninitialized entropy context.");
4580
Yaowu Xuf883b422016-08-30 14:01:10 -07004581 av1_zero(cm->counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004582
4583 xd->corrupted = 0;
4584 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
4585 if (new_fb->corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07004586 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004587 "Decode failed. Frame data header is corrupted.");
4588
4589 if (cm->lf.filter_level && !cm->skip_loop_filter) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004590 av1_loop_filter_frame_init(cm, cm->lf.filter_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004591 }
4592
4593 // If encoded in frame parallel mode, frame context is ready after decoding
4594 // the frame header.
4595 if (cm->frame_parallel_decode &&
4596 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004597 AVxWorker *const worker = pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004598 FrameWorkerData *const frame_worker_data = worker->data1;
4599 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
4600 context_updated = 1;
4601 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4602 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004603 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004604 pbi->cur_buf->row = -1;
4605 pbi->cur_buf->col = -1;
4606 frame_worker_data->frame_context_ready = 1;
4607 // Signal the main thread that context is ready.
Yaowu Xuf883b422016-08-30 14:01:10 -07004608 av1_frameworker_signal_stats(worker);
4609 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004610 }
4611
4612#if CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004613 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004614 cm->coef_probs_update_idx = 0;
4615#endif // CONFIG_ENTROPY
4616
4617 if (pbi->max_threads > 1
4618#if CONFIG_EXT_TILE
4619 && pbi->dec_tile_col < 0 // Decoding all columns
4620#endif // CONFIG_EXT_TILE
4621 && cm->tile_cols > 1) {
4622 // Multi-threaded tile decoder
4623 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
4624 if (!xd->corrupted) {
4625 if (!cm->skip_loop_filter) {
4626 // If multiple threads are used to decode tiles, then we use those
4627 // threads to do parallel loopfiltering.
Yaowu Xuf883b422016-08-30 14:01:10 -07004628 av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
4629 0, 0, pbi->tile_workers, pbi->num_tile_workers,
4630 &pbi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004631 }
4632 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004633 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004634 "Decode failed. Frame data is corrupted.");
4635 }
4636 } else {
4637 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
4638 }
4639#if CONFIG_LOOP_RESTORATION
4640 if (cm->rst_info.restoration_type != RESTORE_NONE) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004641 av1_loop_restoration_init(&cm->rst_internal, &cm->rst_info,
4642 cm->frame_type == KEY_FRAME, cm->width,
4643 cm->height);
4644 av1_loop_restoration_rows(new_fb, cm, 0, cm->mi_rows, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004645 }
4646#endif // CONFIG_LOOP_RESTORATION
4647
Steinar Midtskogen5d56f4d2016-09-25 09:23:16 +02004648#if CONFIG_DERING
4649 if (cm->dering_level && !cm->skip_loop_filter) {
4650 av1_dering_frame(&pbi->cur_buf->buf, cm, &pbi->mb, cm->dering_level);
4651 }
4652#endif // CONFIG_DERING
4653
Thomas Daedef56859f2016-04-19 16:57:24 -07004654#if CONFIG_CLPF
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004655 if (!cm->skip_loop_filter) {
4656 const YV12_BUFFER_CONFIG *const frame = &pbi->cur_buf->buf;
4657 if (cm->clpf_strength_y) {
Yaowu Xud71be782016-10-14 08:47:03 -07004658 av1_clpf_frame(frame, NULL, cm, cm->clpf_size != CLPF_NOSIZE,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004659 cm->clpf_strength_y + (cm->clpf_strength_y == 3),
Yaowu Xud71be782016-10-14 08:47:03 -07004660 4 + cm->clpf_size, AOM_PLANE_Y, clpf_bit);
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004661 }
4662 if (cm->clpf_strength_u) {
Yaowu Xud71be782016-10-14 08:47:03 -07004663 av1_clpf_frame(frame, NULL, cm, 0, // No block signals for chroma
4664 cm->clpf_strength_u + (cm->clpf_strength_u == 3), 4,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004665 AOM_PLANE_U, NULL);
4666 }
4667 if (cm->clpf_strength_v) {
Yaowu Xud71be782016-10-14 08:47:03 -07004668 av1_clpf_frame(frame, NULL, cm, 0, // No block signals for chroma
4669 cm->clpf_strength_v + (cm->clpf_strength_v == 3), 4,
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02004670 AOM_PLANE_V, NULL);
4671 }
Steinar Midtskogend06588a2016-05-06 13:48:20 +02004672 }
4673 if (cm->clpf_blocks) aom_free(cm->clpf_blocks);
Thomas Daedef56859f2016-04-19 16:57:24 -07004674#endif
Thomas Daedef56859f2016-04-19 16:57:24 -07004675
Yaowu Xuc27fc142016-08-22 16:08:15 -07004676 if (!xd->corrupted) {
4677 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
4678#if CONFIG_ENTROPY
4679 cm->partial_prob_update = 0;
4680#endif // CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004681 av1_adapt_coef_probs(cm);
4682 av1_adapt_intra_frame_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004683
4684 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004685 av1_adapt_inter_frame_probs(cm);
4686 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004687 }
4688 } else {
4689 debug_check_frame_counts(cm);
4690 }
4691 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004692 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004693 "Decode failed. Frame data is corrupted.");
4694 }
4695
4696 // Non frame parallel update frame context here.
4697 if (!cm->error_resilient_mode && !context_updated)
4698 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4699}