blob: 165164d27f292559fdb0bd8824a37ad3c53ecf3a [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"
Cheng Chenc7855b12017-09-05 10:49:08 -070022#include "aom_dsp/binary_codes_reader.h"
Jingning Han1aab8182016-06-03 11:09:06 -070023#include "aom_dsp/bitreader.h"
24#include "aom_dsp/bitreader_buffer.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070025#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070026#include "aom_ports/mem.h"
27#include "aom_ports/mem_ops.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070028#include "aom_scale/aom_scale.h"
29#include "aom_util/aom_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070030
Fergus Simpson350a9b72017-04-17 15:08:45 -070031#if CONFIG_BITSTREAM_DEBUG
32#include "aom_util/debug_util.h"
33#endif // CONFIG_BITSTREAM_DEBUG
34
Yaowu Xuc27fc142016-08-22 16:08:15 -070035#include "av1/common/alloccommon.h"
Jean-Marc Valin01435132017-02-18 14:12:53 -050036#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010037#include "av1/common/cdef.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070038#endif
Nathan E. Egge2cf03b12017-02-22 16:19:59 -050039#if CONFIG_INSPECTION
40#include "av1/decoder/inspection.h"
41#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070042#include "av1/common/common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070043#include "av1/common/entropy.h"
44#include "av1/common/entropymode.h"
Thomas Davies6519beb2016-10-19 14:46:07 +010045#include "av1/common/entropymv.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070046#include "av1/common/idct.h"
Jingning Hanc723b342017-08-24 11:19:46 -070047#include "av1/common/mvref_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070048#include "av1/common/pred_common.h"
49#include "av1/common/quant_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070050#include "av1/common/reconinter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070051#include "av1/common/reconintra.h"
Fergus Simpsond2bcbb52017-05-22 23:15:05 -070052#if CONFIG_FRAME_SUPERRES
53#include "av1/common/resize.h"
54#endif // CONFIG_FRAME_SUPERRES
Yaowu Xuc27fc142016-08-22 16:08:15 -070055#include "av1/common/seg_common.h"
Jingning Han1aab8182016-06-03 11:09:06 -070056#include "av1/common/thread_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070057#include "av1/common/tile_common.h"
58
59#include "av1/decoder/decodeframe.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070060#include "av1/decoder/decodemv.h"
61#include "av1/decoder/decoder.h"
Angie Chiang133733c2017-03-17 12:50:20 -070062#if CONFIG_LV_MAP
63#include "av1/decoder/decodetxb.h"
64#endif
Jingning Han1aab8182016-06-03 11:09:06 -070065#include "av1/decoder/detokenize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070066#include "av1/decoder/dsubexp.h"
Angie Chiang85e3b962017-10-01 16:04:43 -070067#include "av1/decoder/symbolrate.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070068
Yue Chen69f18e12016-09-08 14:48:15 -070069#include "av1/common/warped_motion.h"
Yue Chen69f18e12016-09-08 14:48:15 -070070
Yaowu Xuf883b422016-08-30 14:01:10 -070071#define MAX_AV1_HEADER_SIZE 80
Michael Bebenita6048d052016-08-25 14:40:54 -070072#define ACCT_STR __func__
Yaowu Xuc27fc142016-08-22 16:08:15 -070073
Luc Trudeaue3980282017-04-25 23:17:21 -040074#if CONFIG_CFL
75#include "av1/common/cfl.h"
76#endif
77
Ola Hugosson1e7f2d02017-09-22 21:36:26 +020078#if CONFIG_STRIPED_LOOP_RESTORATION && !CONFIG_LOOP_RESTORATION
79#error "striped_loop_restoration requires loop_restoration"
80#endif
81
Rupert Swarbrick6c545212017-09-01 17:17:25 +010082#if CONFIG_LOOP_RESTORATION
83static void loop_restoration_read_sb_coeffs(const AV1_COMMON *const cm,
84 MACROBLOCKD *xd,
85 aom_reader *const r, int plane,
86 int rtile_idx);
87#endif
88
Thomas Davies80188d12016-10-26 16:08:35 -070089static struct aom_read_bit_buffer *init_read_bit_buffer(
90 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
91 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]);
92static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
93 size_t partition_size);
94static size_t read_uncompressed_header(AV1Decoder *pbi,
95 struct aom_read_bit_buffer *rb);
96
Yaowu Xuf883b422016-08-30 14:01:10 -070097static int is_compound_reference_allowed(const AV1_COMMON *cm) {
Zoe Liu5a978832017-08-15 16:33:34 -070098#if CONFIG_ONE_SIDED_COMPOUND // Normative in decoder
Arild Fuldseth (arilfuld)38897302017-04-27 20:03:03 +020099 return !frame_is_intra_only(cm);
100#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700101 int i;
102 if (frame_is_intra_only(cm)) return 0;
103 for (i = 1; i < INTER_REFS_PER_FRAME; ++i)
104 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
105
106 return 0;
Zoe Liu5a978832017-08-15 16:33:34 -0700107#endif // CONFIG_ONE_SIDED_COMPOUND
Yaowu Xuc27fc142016-08-22 16:08:15 -0700108}
109
Yaowu Xuf883b422016-08-30 14:01:10 -0700110static void setup_compound_reference_mode(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700111 cm->comp_fwd_ref[0] = LAST_FRAME;
112 cm->comp_fwd_ref[1] = LAST2_FRAME;
113 cm->comp_fwd_ref[2] = LAST3_FRAME;
114 cm->comp_fwd_ref[3] = GOLDEN_FRAME;
115
116 cm->comp_bwd_ref[0] = BWDREF_FRAME;
Zoe Liu043c2272017-07-19 12:40:29 -0700117 cm->comp_bwd_ref[1] = ALTREF2_FRAME;
118 cm->comp_bwd_ref[2] = ALTREF_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700119}
120
121static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
122 return len != 0 && len <= (size_t)(end - start);
123}
124
Yaowu Xuf883b422016-08-30 14:01:10 -0700125static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
126 const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700127 return data > max ? max : data;
128}
129
Debargha Mukherjee923b73d2017-10-31 18:11:34 -0700130#if CONFIG_SIMPLIFY_TX_MODE
131static TX_MODE read_tx_mode(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
132 if (cm->all_lossless) return ONLY_4X4;
133#if CONFIG_VAR_TX_NO_TX_MODE
134 (void)rb;
135 return TX_MODE_SELECT;
136#else
137 return aom_rb_read_bit(rb) ? TX_MODE_SELECT : TX_MODE_LARGEST;
138#endif // CONFIG_VAR_TX_NO_TX_MODE
139}
140#else
Thomas Daedef636d5c2017-06-29 13:48:27 -0700141static TX_MODE read_tx_mode(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800142#if CONFIG_TX64X64
Yue Cheneeacc4c2017-01-17 17:29:17 -0800143 TX_MODE tx_mode;
144#endif
Thomas Daedef636d5c2017-06-29 13:48:27 -0700145 if (cm->all_lossless) return ONLY_4X4;
Nathan E. Eggea33304f2017-06-28 20:48:34 -0400146#if CONFIG_VAR_TX_NO_TX_MODE
147 (void)rb;
148 return TX_MODE_SELECT;
149#else
Yue Cheneeacc4c2017-01-17 17:29:17 -0800150#if CONFIG_TX64X64
151 tx_mode = aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800152 if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
153 return tx_mode;
154#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700155 return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
Debargha Mukherjee18d38f62016-11-17 20:30:16 -0800156#endif // CONFIG_TX64X64
Nathan E. Eggea33304f2017-06-28 20:48:34 -0400157#endif // CONFIG_VAR_TX_NO_TX_MODE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700158}
Debargha Mukherjee923b73d2017-10-31 18:11:34 -0700159#endif // CONFIG_SIMPLIFY_TX_MODE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700160
Thomas Davies2e868ab2017-10-24 10:42:27 +0100161#if !CONFIG_NEW_MULTISYMBOL
Yaowu Xuf883b422016-08-30 14:01:10 -0700162static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Yaowu Xu8af861b2016-11-01 12:12:11 -0700163 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700164 for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700165 av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
Sarah Parker2b9ec2e2017-10-30 17:34:08 -0700166 for (i = 0; i < GLOBALMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700167 av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700168 for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700169 av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700170 for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700171 av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700172}
Thomas Davies149eda52017-06-12 18:11:55 +0100173#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700174
Yaowu Xuc27fc142016-08-22 16:08:15 -0700175static REFERENCE_MODE read_frame_reference_mode(
Yaowu Xuf883b422016-08-30 14:01:10 -0700176 const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700177 if (is_compound_reference_allowed(cm)) {
Zoe Liub05e5d12017-02-07 14:32:53 -0800178#if CONFIG_REF_ADAPT
179 return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
180#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700181 return aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182 ? REFERENCE_MODE_SELECT
Yaowu Xuf883b422016-08-30 14:01:10 -0700183 : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
Zoe Liub05e5d12017-02-07 14:32:53 -0800184#endif // CONFIG_REF_ADAPT
Yaowu Xuc27fc142016-08-22 16:08:15 -0700185 } else {
186 return SINGLE_REFERENCE;
187 }
188}
189
Thomas Davies2e868ab2017-10-24 10:42:27 +0100190#if !CONFIG_NEW_MULTISYMBOL
Yaowu Xuf883b422016-08-30 14:01:10 -0700191static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700192 FRAME_CONTEXT *const fc = cm->fc;
Thomas Davies894cc812017-06-22 17:51:33 +0100193 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700194
195 if (cm->reference_mode == REFERENCE_MODE_SELECT)
196 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700197 av1_diff_update_prob(r, &fc->comp_inter_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700198
199 if (cm->reference_mode != COMPOUND_REFERENCE) {
200 for (i = 0; i < REF_CONTEXTS; ++i) {
Thomas Davies894cc812017-06-22 17:51:33 +0100201 int j;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700202 for (j = 0; j < (SINGLE_REFS - 1); ++j) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700203 av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700204 }
205 }
206 }
207
208 if (cm->reference_mode != SINGLE_REFERENCE) {
Zoe Liuc082bbc2017-05-17 13:31:37 -0700209#if CONFIG_EXT_COMP_REFS
210 for (i = 0; i < COMP_REF_TYPE_CONTEXTS; ++i)
211 av1_diff_update_prob(r, &fc->comp_ref_type_prob[i], ACCT_STR);
212
Thomas Davies894cc812017-06-22 17:51:33 +0100213 for (i = 0; i < UNI_COMP_REF_CONTEXTS; ++i) {
214 int j;
Zoe Liuc082bbc2017-05-17 13:31:37 -0700215 for (j = 0; j < (UNIDIR_COMP_REFS - 1); ++j)
216 av1_diff_update_prob(r, &fc->uni_comp_ref_prob[i][j], ACCT_STR);
Thomas Davies894cc812017-06-22 17:51:33 +0100217 }
Zoe Liuc082bbc2017-05-17 13:31:37 -0700218#endif // CONFIG_EXT_COMP_REFS
219
Yaowu Xuc27fc142016-08-22 16:08:15 -0700220 for (i = 0; i < REF_CONTEXTS; ++i) {
Thomas Davies894cc812017-06-22 17:51:33 +0100221 int j;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700222 for (j = 0; j < (FWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700223 av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700224 for (j = 0; j < (BWD_REFS - 1); ++j)
Michael Bebenita6048d052016-08-25 14:40:54 -0700225 av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700226 }
227 }
228}
229
Yaowu Xuf883b422016-08-30 14:01:10 -0700230static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700231 int i;
Michael Bebenita6048d052016-08-25 14:40:54 -0700232 for (i = 0; i < n; ++i) av1_diff_update_prob(r, &p[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700233}
234
Yaowu Xuf883b422016-08-30 14:01:10 -0700235static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Thomas9ac55082016-09-23 18:04:17 +0100236 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700237 if (allow_hp) {
238 for (i = 0; i < 2; ++i) {
239 nmv_component *const comp_ctx = &ctx->comps[i];
240 update_mv_probs(&comp_ctx->class0_hp, 1, r);
241 update_mv_probs(&comp_ctx->hp, 1, r);
242 }
243 }
244}
Thomas Davies599395e2017-07-21 18:02:48 +0100245#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700246
247static void inverse_transform_block(MACROBLOCKD *xd, int plane,
248 const TX_TYPE tx_type,
249 const TX_SIZE tx_size, uint8_t *dst,
Jingning Han1be18782016-10-21 11:48:15 -0700250 int stride, int16_t scan_line, int eob) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700251 struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han1be18782016-10-21 11:48:15 -0700252 tran_low_t *const dqcoeff = pd->dqcoeff;
Lester Lu708c1ec2017-06-14 14:54:49 -0700253 av1_inverse_transform_block(xd, dqcoeff,
Sarah Parker99e7daa2017-08-29 10:30:13 -0700254#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
255 xd->mrc_mask,
256#endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
Sebastien Alaiwan3bac9922017-11-02 12:34:41 +0100257 plane, tx_type, tx_size, dst, stride, eob);
Jingning Han1be18782016-10-21 11:48:15 -0700258 memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700259}
260
Lester Lu9c521922017-07-11 11:16:45 -0700261static int get_block_idx(const MACROBLOCKD *xd, int plane, int row, int col) {
262 const int bsize = xd->mi[0]->mbmi.sb_type;
263 const struct macroblockd_plane *pd = &xd->plane[plane];
Lester Lu9c521922017-07-11 11:16:45 -0700264 const BLOCK_SIZE plane_bsize =
265 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Lester Lu9c521922017-07-11 11:16:45 -0700266 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
hui su0c6244b2017-07-12 17:11:43 -0700267 const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
Lester Lu9c521922017-07-11 11:16:45 -0700268 const uint8_t txh_unit = tx_size_high_unit[tx_size];
269 return row * max_blocks_wide + col * txh_unit;
270}
271
Alex Converse8aca36d2017-01-31 12:33:15 -0800272static void predict_and_reconstruct_intra_block(
273 AV1_COMMON *cm, MACROBLOCKD *const xd, aom_reader *const r,
274 MB_MODE_INFO *const mbmi, int plane, int row, int col, TX_SIZE tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500275 PLANE_TYPE plane_type = get_plane_type(plane);
Angie Chiang752ccce2017-04-09 13:41:13 -0700276 const int block_idx = get_block_idx(xd, plane, row, col);
David Barker761b1ac2017-09-25 11:23:03 +0100277 av1_predict_intra_block_facade(cm, xd, plane, block_idx, col, row, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700278
279 if (!mbmi->skip) {
Angie Chiang3d005e42017-04-02 16:31:35 -0700280 struct macroblockd_plane *const pd = &xd->plane[plane];
Angie Chiang133733c2017-03-17 12:50:20 -0700281#if CONFIG_LV_MAP
282 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700283 int eob;
284 av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane,
Jingning Hanaba246d2017-06-14 12:00:16 -0700285 pd->dqcoeff, tx_size, &max_scan_line, &eob);
Angie Chiangb6d770c2017-04-14 16:27:57 -0700286 // tx_type will be read out in av1_read_coeffs_txb_facade
Jingning Han19b5c8f2017-07-06 15:10:12 -0700287 const TX_TYPE tx_type =
288 av1_get_tx_type(plane_type, xd, row, col, block_idx, tx_size);
Angie Chiang133733c2017-03-17 12:50:20 -0700289#else // CONFIG_LV_MAP
Jingning Han19b5c8f2017-07-06 15:10:12 -0700290 const TX_TYPE tx_type =
291 av1_get_tx_type(plane_type, xd, row, col, block_idx, tx_size);
Angie Chiangbd99b382017-06-20 15:11:16 -0700292 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, mbmi);
Jingning Han1be18782016-10-21 11:48:15 -0700293 int16_t max_scan_line = 0;
294 const int eob =
Angie Chiang5c0568a2017-03-21 16:00:39 -0700295 av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
Jingning Han1be18782016-10-21 11:48:15 -0700296 tx_type, &max_scan_line, r, mbmi->segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700297#endif // CONFIG_LV_MAP
Angie Chiang3d005e42017-04-02 16:31:35 -0700298 if (eob) {
299 uint8_t *dst =
300 &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
Sebastien Alaiwan7fc6b2a2017-11-02 18:14:50 +0100301 inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
Hui Su400bf652017-08-15 15:42:19 -0700302 max_scan_line, eob);
Angie Chiang3d005e42017-04-02 16:31:35 -0700303 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700304 }
Luc Trudeaue3980282017-04-25 23:17:21 -0400305#if CONFIG_CFL
Luc Trudeaue784b3f2017-08-14 15:25:28 -0400306 if (plane == AOM_PLANE_Y && xd->cfl->store_y) {
Luc Trudeaub05eeae2017-08-18 15:14:30 -0400307 cfl_store_tx(xd, row, col, tx_size, mbmi->sb_type);
Luc Trudeaue3980282017-04-25 23:17:21 -0400308 }
Sebastien Alaiwanc4559ca2017-09-27 09:47:30 +0200309#endif // CONFIG_CFL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700310}
311
Angie Chiangff6d8902016-10-21 11:02:09 -0700312static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
313 aom_reader *r, MB_MODE_INFO *const mbmi,
Jingning Han8fd62b72016-10-21 12:55:54 -0700314 int plane, BLOCK_SIZE plane_bsize,
Jingning Hana65f3052017-06-23 10:52:05 -0700315 int blk_row, int blk_col, int block,
316 TX_SIZE tx_size, int *eob_total) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700317 const struct macroblockd_plane *const pd = &xd->plane[plane];
318 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
319 const int tx_row = blk_row >> (1 - pd->subsampling_y);
320 const int tx_col = blk_col >> (1 - pd->subsampling_x);
321 const TX_SIZE plane_tx_size =
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700322 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700323 : mbmi->inter_tx_size[tx_row][tx_col];
Jingning Han5f614262016-10-27 14:27:43 -0700324 // Scale to match transform block unit.
Jingning Hanf64062f2016-11-02 16:22:18 -0700325 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
326 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700327
328 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
329
330 if (tx_size == plane_tx_size) {
Luc Trudeau005feb62017-02-22 13:34:01 -0500331 PLANE_TYPE plane_type = get_plane_type(plane);
Angie Chiang133733c2017-03-17 12:50:20 -0700332#if CONFIG_LV_MAP
Angie Chiang133733c2017-03-17 12:50:20 -0700333 int16_t max_scan_line = 0;
Angie Chiang29b0fad2017-03-20 16:18:45 -0700334 int eob;
Jingning Hana65f3052017-06-23 10:52:05 -0700335 av1_read_coeffs_txb_facade(cm, xd, r, blk_row, blk_col, block, plane,
Jingning Hanaba246d2017-06-14 12:00:16 -0700336 pd->dqcoeff, tx_size, &max_scan_line, &eob);
Angie Chiangb6d770c2017-04-14 16:27:57 -0700337 // tx_type will be read out in av1_read_coeffs_txb_facade
hui su45b64752017-07-12 16:54:35 -0700338 const TX_TYPE tx_type =
Jingning Han19b5c8f2017-07-06 15:10:12 -0700339 av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, plane_tx_size);
Angie Chiang133733c2017-03-17 12:50:20 -0700340#else // CONFIG_LV_MAP
hui su45b64752017-07-12 16:54:35 -0700341 const TX_TYPE tx_type =
Jingning Han19b5c8f2017-07-06 15:10:12 -0700342 av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, plane_tx_size);
Angie Chiangbd99b382017-06-20 15:11:16 -0700343 const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, mbmi);
Jingning Han1be18782016-10-21 11:48:15 -0700344 int16_t max_scan_line = 0;
Angie Chiang5c0568a2017-03-21 16:00:39 -0700345 const int eob = av1_decode_block_tokens(
346 cm, xd, plane, sc, blk_col, blk_row, plane_tx_size, tx_type,
347 &max_scan_line, r, mbmi->segment_id);
Angie Chiang133733c2017-03-17 12:50:20 -0700348#endif // CONFIG_LV_MAP
Sebastien Alaiwan7fc6b2a2017-11-02 18:14:50 +0100349 inverse_transform_block(xd, plane, tx_type, plane_tx_size,
Jingning Han9ca05b72017-01-03 14:41:36 -0800350 &pd->dst.buf[(blk_row * pd->dst.stride + blk_col)
351 << tx_size_wide_log2[0]],
352 pd->dst.stride, max_scan_line, eob);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700353 *eob_total += eob;
354 } else {
Yue Chend6bdd462017-07-19 16:05:43 -0700355#if CONFIG_RECT_TX_EXT
356 int is_qttx = plane_tx_size == quarter_txsize_lookup[plane_bsize];
357 const TX_SIZE sub_txs = is_qttx ? plane_tx_size : sub_tx_size_map[tx_size];
358 if (is_qttx) assert(blk_row == 0 && blk_col == 0 && block == 0);
359#else
Jingning Hanf64062f2016-11-02 16:22:18 -0700360 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
Urvang Joshidff57e02017-09-29 11:15:48 -0700361 assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
362 assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
Yue Chend6bdd462017-07-19 16:05:43 -0700363#endif
Jingning Hanf64062f2016-11-02 16:22:18 -0700364 const int bsl = tx_size_wide_unit[sub_txs];
Jingning Hana65f3052017-06-23 10:52:05 -0700365 int sub_step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700366 int i;
367
368 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700369
370 for (i = 0; i < 4; ++i) {
Yue Chend6bdd462017-07-19 16:05:43 -0700371#if CONFIG_RECT_TX_EXT
372 int is_wide_tx = tx_size_wide_unit[sub_txs] > tx_size_high_unit[sub_txs];
373 const int offsetr =
374 is_qttx ? (is_wide_tx ? i * tx_size_high_unit[sub_txs] : 0)
375 : blk_row + ((i >> 1) * bsl);
376 const int offsetc =
377 is_qttx ? (is_wide_tx ? 0 : i * tx_size_wide_unit[sub_txs])
378 : blk_col + (i & 0x01) * bsl;
379#else
Jingning Han5f614262016-10-27 14:27:43 -0700380 const int offsetr = blk_row + (i >> 1) * bsl;
381 const int offsetc = blk_col + (i & 0x01) * bsl;
Yue Chend6bdd462017-07-19 16:05:43 -0700382#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700383
384 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
385
Jingning Han8fd62b72016-10-21 12:55:54 -0700386 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
Jingning Hana65f3052017-06-23 10:52:05 -0700387 offsetc, block, sub_txs, eob_total);
388 block += sub_step;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700389 }
390 }
391}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700392
Angie Chiang44701f22017-02-27 10:36:44 -0800393static void set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
394 BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
395 int bh, int x_mis, int y_mis) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700396 const int offset = mi_row * cm->mi_stride + mi_col;
397 int x, y;
398 const TileInfo *const tile = &xd->tile;
399
400 xd->mi = cm->mi_grid_visible + offset;
401 xd->mi[0] = &cm->mi[offset];
402 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
403 // passing bsize from decode_partition().
404 xd->mi[0]->mbmi.sb_type = bsize;
Angie Chiang394c3372016-11-03 11:13:15 -0700405#if CONFIG_RD_DEBUG
406 xd->mi[0]->mbmi.mi_row = mi_row;
407 xd->mi[0]->mbmi.mi_col = mi_col;
408#endif
Luc Trudeau780d2492017-06-15 22:26:41 -0400409#if CONFIG_CFL
410 xd->cfl->mi_row = mi_row;
411 xd->cfl->mi_col = mi_col;
412#endif
Yunqing Wangb90a97a2017-10-24 11:50:15 -0700413
414 assert(x_mis && y_mis);
415 for (x = 1; x < x_mis; ++x) xd->mi[x] = xd->mi[0];
416 int idx = cm->mi_stride;
417 for (y = 1; y < y_mis; ++y) {
418 memcpy(&xd->mi[idx], &xd->mi[0], x_mis * sizeof(xd->mi[0]));
419 idx += cm->mi_stride;
420 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700421
Jingning Hanfaad0e12016-12-07 10:54:57 -0800422 set_plane_n4(xd, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700423 set_skip_context(xd, mi_row, mi_col);
424
Urvang Joshi359dc2b2017-04-27 15:41:47 -0700425 // Distance of Mb to the various image edges. These are specified to 8th pel
426 // as they are always compared to values that are in 1/8th pel units
427 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800428#if CONFIG_DEPENDENT_HORZTILES
Urvang Joshi359dc2b2017-04-27 15:41:47 -0700429 cm->dependent_horz_tiles,
430#endif // CONFIG_DEPENDENT_HORZTILES
431 cm->mi_rows, cm->mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700432
Jingning Han91d9a792017-04-18 12:01:52 -0700433 av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row,
434 mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700435}
436
Yue Chen64550b62017-01-12 12:18:22 -0800437static void decode_mbmi_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yue Chen64550b62017-01-12 12:18:22 -0800438 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700439#if CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -0800440 PARTITION_TYPE partition,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700441#endif // CONFIG_EXT_PARTITION_TYPES
Yue Chen64550b62017-01-12 12:18:22 -0800442 BLOCK_SIZE bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700443 AV1_COMMON *const cm = &pbi->common;
Jingning Han85dc03f2016-12-06 16:03:10 -0800444 const int bw = mi_size_wide[bsize];
445 const int bh = mi_size_high[bsize];
Yaowu Xuf883b422016-08-30 14:01:10 -0700446 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
447 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Nathan E. Eggeebbd4792016-10-05 19:30:15 -0400448
Michael Bebenita6048d052016-08-25 14:40:54 -0700449#if CONFIG_ACCOUNTING
450 aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
451#endif
Yue Chen64550b62017-01-12 12:18:22 -0800452 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700453#if CONFIG_EXT_PARTITION_TYPES
454 xd->mi[0]->mbmi.partition = partition;
455#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700456 av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700457 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
458 const BLOCK_SIZE uv_subsize =
459 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
460 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -0700461 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700462 "Invalid block size.");
463 }
464
Angie Chiangd0916d92017-03-10 17:54:18 -0800465 int reader_corrupted_flag = aom_reader_has_error(r);
466 aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
Yue Chen64550b62017-01-12 12:18:22 -0800467}
468
469static void decode_token_and_recon_block(AV1Decoder *const pbi,
470 MACROBLOCKD *const xd, int mi_row,
471 int mi_col, aom_reader *r,
472 BLOCK_SIZE bsize) {
473 AV1_COMMON *const cm = &pbi->common;
474 const int bw = mi_size_wide[bsize];
475 const int bh = mi_size_high[bsize];
476 const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
477 const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Yue Chen64550b62017-01-12 12:18:22 -0800478
Angie Chiang44701f22017-02-27 10:36:44 -0800479 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
480 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Hui Su9fa96232017-10-23 15:46:04 -0700481#if CONFIG_CFL
Luc Trudeaub05eeae2017-08-18 15:14:30 -0400482 CFL_CTX *const cfl = xd->cfl;
483 cfl->is_chroma_reference = is_chroma_reference(
484 mi_row, mi_col, bsize, cfl->subsampling_x, cfl->subsampling_y);
Hui Su9fa96232017-10-23 15:46:04 -0700485#endif // CONFIG_CFL
Yue Chen19e7aa82016-11-30 14:05:39 -0800486
Arild Fuldseth07441162016-08-15 15:07:52 +0200487 if (cm->delta_q_present_flag) {
488 int i;
489 for (i = 0; i < MAX_SEGMENTS; i++) {
Fangwen Fu6160df22017-04-24 09:45:51 -0700490#if CONFIG_EXT_DELTA_Q
Cheng Chen49d30e62017-08-28 20:59:27 -0700491 const int current_qindex =
492 av1_get_qindex(&cm->seg, i, xd->current_qindex);
Fangwen Fu6160df22017-04-24 09:45:51 -0700493#else
Cheng Chen49d30e62017-08-28 20:59:27 -0700494 const int current_qindex = xd->current_qindex;
495#endif // CONFIG_EXT_DELTA_Q
496 int j;
497 for (j = 0; j < MAX_MB_PLANE; ++j) {
498 const int dc_delta_q = j == 0 ? cm->y_dc_delta_q : cm->uv_dc_delta_q;
499 const int ac_delta_q = j == 0 ? 0 : cm->uv_ac_delta_q;
500
501 xd->plane[j].seg_dequant[i][0] =
502 av1_dc_quant(current_qindex, dc_delta_q, cm->bit_depth);
503 xd->plane[j].seg_dequant[i][1] =
504 av1_ac_quant(current_qindex, ac_delta_q, cm->bit_depth);
505 }
Arild Fuldseth07441162016-08-15 15:07:52 +0200506 }
507 }
Timothy B. Terriberrya2d5cde2017-05-10 18:33:50 -0700508 if (mbmi->skip) av1_reset_skip_context(xd, mi_row, mi_col, bsize);
Jingning Hand39cc722016-12-02 14:03:26 -0800509
Yaowu Xuc27fc142016-08-22 16:08:15 -0700510 if (!is_inter_block(mbmi)) {
511 int plane;
Yushin Choa8810392017-09-06 15:16:14 -0700512
Yaowu Xuc27fc142016-08-22 16:08:15 -0700513 for (plane = 0; plane <= 1; ++plane) {
514 if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xuf883b422016-08-30 14:01:10 -0700515 av1_decode_palette_tokens(xd, plane, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700516 }
Yushin Choa8810392017-09-06 15:16:14 -0700517
Yaowu Xuc27fc142016-08-22 16:08:15 -0700518 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
519 const struct macroblockd_plane *const pd = &xd->plane[plane];
hui su0c6244b2017-07-12 17:11:43 -0700520 const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
Jingning Han2d64f122016-10-21 12:44:29 -0700521 const int stepr = tx_size_high_unit[tx_size];
522 const int stepc = tx_size_wide_unit[tx_size];
Jingning Hanc20dc8e2017-02-17 15:37:28 -0800523 const BLOCK_SIZE plane_bsize =
524 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700525 int row, col;
Jingning Hanbafee8d2016-12-02 10:25:03 -0800526 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
527 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Jingning Hand3a64432017-04-06 17:04:17 -0700528 if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
529 pd->subsampling_y))
Jingning Hanc20dc8e2017-02-17 15:37:28 -0800530 continue;
Jingning Han5b701742017-07-19 14:39:07 -0700531 int blk_row, blk_col;
532 const BLOCK_SIZE max_unit_bsize = get_plane_block_size(BLOCK_64X64, pd);
533 int mu_blocks_wide =
534 block_size_wide[max_unit_bsize] >> tx_size_wide_log2[0];
535 int mu_blocks_high =
536 block_size_high[max_unit_bsize] >> tx_size_high_log2[0];
537 mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
538 mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
Jingning Hanc20dc8e2017-02-17 15:37:28 -0800539
Jingning Han5b701742017-07-19 14:39:07 -0700540 for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
Luc Trudeauda9397a2017-07-21 12:00:22 -0400541 const int unit_height = AOMMIN(mu_blocks_high + row, max_blocks_high);
Jingning Han5b701742017-07-19 14:39:07 -0700542 for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
Jingning Han5b701742017-07-19 14:39:07 -0700543 const int unit_width = AOMMIN(mu_blocks_wide + col, max_blocks_wide);
544
545 for (blk_row = row; blk_row < unit_height; blk_row += stepr)
546 for (blk_col = col; blk_col < unit_width; blk_col += stepc)
547 predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane,
548 blk_row, blk_col, tx_size);
549 }
550 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700551 }
552 } else {
Yue Chen9ab6d712017-01-12 15:50:46 -0800553 int ref;
554
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +0200555#if CONFIG_COMPOUND_SINGLEREF
Yushin Cho127c5832017-07-28 16:39:04 -0700556 for (ref = 0; ref < 1 + is_inter_anyref_comp_mode(mbmi->mode); ++ref)
557#else
558 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref)
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +0200559#endif // CONFIG_COMPOUND_SINGLEREF
Yushin Cho127c5832017-07-28 16:39:04 -0700560 {
Zoe Liu85b66462017-04-20 14:28:19 -0700561 const MV_REFERENCE_FRAME frame =
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +0200562#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -0700563 has_second_ref(mbmi) ? mbmi->ref_frame[ref] : mbmi->ref_frame[0];
564#else
Yushin Cho127c5832017-07-28 16:39:04 -0700565 mbmi->ref_frame[ref];
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +0200566#endif // CONFIG_COMPOUND_SINGLEREF
Alex Converse28744302017-04-13 14:46:22 -0700567 if (frame < LAST_FRAME) {
568#if CONFIG_INTRABC
569 assert(is_intrabc_block(mbmi));
570 assert(frame == INTRA_FRAME);
571 assert(ref == 0);
572#else
573 assert(0);
574#endif // CONFIG_INTRABC
575 } else {
576 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
Yue Chen9ab6d712017-01-12 15:50:46 -0800577
Alex Converse28744302017-04-13 14:46:22 -0700578 xd->block_refs[ref] = ref_buf;
579 if ((!av1_is_valid_scale(&ref_buf->sf)))
580 aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
581 "Reference frame has invalid dimensions");
582 av1_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
583 &ref_buf->sf);
584 }
Yue Chen9ab6d712017-01-12 15:50:46 -0800585 }
Yue Chen69f18e12016-09-08 14:48:15 -0700586
Jingning Hanc44009c2017-05-06 11:36:49 -0700587 av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL, bsize);
Sarah Parker4c10a3c2017-04-10 19:37:59 -0700588
Yue Chencb60b182016-10-13 15:18:22 -0700589 if (mbmi->motion_mode == OBMC_CAUSAL) {
Yue Chenf27b1602017-01-13 11:11:43 -0800590#if CONFIG_NCOBMC
591 av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
592#else
Yue Chen894fcce2016-10-21 16:50:52 -0700593 av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yue Chenf27b1602017-01-13 11:11:43 -0800594#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700595 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700596 // Reconstruction
597 if (!mbmi->skip) {
598 int eobtotal = 0;
599 int plane;
600
601 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
602 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Hanc20dc8e2017-02-17 15:37:28 -0800603 const BLOCK_SIZE plane_bsize =
604 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Jingning Hanbafee8d2016-12-02 10:25:03 -0800605 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
606 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700607 int row, col;
Jingning Hanc20dc8e2017-02-17 15:37:28 -0800608
Jingning Hand3a64432017-04-06 17:04:17 -0700609 if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
610 pd->subsampling_y))
Jingning Hanc20dc8e2017-02-17 15:37:28 -0800611 continue;
Jingning Hanc20dc8e2017-02-17 15:37:28 -0800612
Jingning Hanc2b797f2017-07-19 09:37:11 -0700613 const BLOCK_SIZE max_unit_bsize = get_plane_block_size(BLOCK_64X64, pd);
614 int mu_blocks_wide =
615 block_size_wide[max_unit_bsize] >> tx_size_wide_log2[0];
616 int mu_blocks_high =
617 block_size_high[max_unit_bsize] >> tx_size_high_log2[0];
618
619 mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
620 mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
621
Rupert Swarbrick4e7b7d62017-09-28 17:30:44 +0100622 const TX_SIZE max_tx_size = get_vartx_max_txsize(
623 mbmi, plane_bsize, pd->subsampling_x || pd->subsampling_y);
Jingning Hanf64062f2016-11-02 16:22:18 -0700624 const int bh_var_tx = tx_size_high_unit[max_tx_size];
625 const int bw_var_tx = tx_size_wide_unit[max_tx_size];
Jingning Hana65f3052017-06-23 10:52:05 -0700626 int block = 0;
627 int step =
628 tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
Jingning Hanc2b797f2017-07-19 09:37:11 -0700629
630 for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
631 for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
632 int blk_row, blk_col;
633 const int unit_height =
634 AOMMIN(mu_blocks_high + row, max_blocks_high);
635 const int unit_width =
636 AOMMIN(mu_blocks_wide + col, max_blocks_wide);
637 for (blk_row = row; blk_row < unit_height; blk_row += bh_var_tx) {
638 for (blk_col = col; blk_col < unit_width; blk_col += bw_var_tx) {
639 decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize,
640 blk_row, blk_col, block, max_tx_size,
641 &eobtotal);
642 block += step;
643 }
644 }
Jingning Hana65f3052017-06-23 10:52:05 -0700645 }
646 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700647 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700648 }
649 }
Hui Su9fa96232017-10-23 15:46:04 -0700650#if CONFIG_CFL
Luc Trudeaub05eeae2017-08-18 15:14:30 -0400651 if (mbmi->uv_mode != UV_CFL_PRED) {
Luc Trudeaub05eeae2017-08-18 15:14:30 -0400652 if (!cfl->is_chroma_reference && is_inter_block(mbmi)) {
653 cfl_store_block(xd, mbmi->sb_type, mbmi->tx_size);
654 }
655 }
Hui Su9fa96232017-10-23 15:46:04 -0700656#endif // CONFIG_CFL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700657
Angie Chiangd0916d92017-03-10 17:54:18 -0800658 int reader_corrupted_flag = aom_reader_has_error(r);
659 aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700660}
661
Sebastien Alaiwan1bc94fc2017-10-31 10:25:17 +0100662#if NC_MODE_INFO
Yue Chen9ab6d712017-01-12 15:50:46 -0800663static void detoken_and_recon_sb(AV1Decoder *const pbi, MACROBLOCKD *const xd,
664 int mi_row, int mi_col, aom_reader *r,
665 BLOCK_SIZE bsize) {
666 AV1_COMMON *const cm = &pbi->common;
667 const int hbs = mi_size_wide[bsize] >> 1;
Yue Chen9ab6d712017-01-12 15:50:46 -0800668#if CONFIG_EXT_PARTITION_TYPES
669 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
670#endif
671 PARTITION_TYPE partition;
672 BLOCK_SIZE subsize;
673 const int has_rows = (mi_row + hbs) < cm->mi_rows;
674 const int has_cols = (mi_col + hbs) < cm->mi_cols;
675
676 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
677
678 partition = get_partition(cm, mi_row, mi_col, bsize);
679 subsize = subsize_lookup[partition][bsize];
680
Debargha Mukherjeeedced252017-10-20 00:02:00 -0700681 switch (partition) {
682 case PARTITION_NONE:
683 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
684 break;
685 case PARTITION_HORZ:
686 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
687 if (has_rows)
688 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, subsize);
689 break;
690 case PARTITION_VERT:
691 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
692 if (has_cols)
693 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, subsize);
694 break;
695 case PARTITION_SPLIT:
696 detoken_and_recon_sb(pbi, xd, mi_row, mi_col, r, subsize);
697 detoken_and_recon_sb(pbi, xd, mi_row, mi_col + hbs, r, subsize);
698 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col, r, subsize);
699 detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col + hbs, r, subsize);
700 break;
Yue Chen9ab6d712017-01-12 15:50:46 -0800701#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick3dd33912017-09-12 14:24:11 +0100702#if CONFIG_EXT_PARTITION_TYPES_AB
703#error NC_MODE_INFO+MOTION_VAR not yet supported for new HORZ/VERT_AB partitions
704#endif
Debargha Mukherjeeedced252017-10-20 00:02:00 -0700705 case PARTITION_HORZ_A:
706 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
707 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
708 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, subsize);
709 break;
710 case PARTITION_HORZ_B:
711 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
712 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
713 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
714 bsize2);
715 break;
716 case PARTITION_VERT_A:
717 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
718 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
719 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, subsize);
720 break;
721 case PARTITION_VERT_B:
722 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
723 decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
724 decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
725 bsize2);
726 break;
Yue Chen9ab6d712017-01-12 15:50:46 -0800727#endif
Debargha Mukherjeeedced252017-10-20 00:02:00 -0700728 default: assert(0 && "Invalid partition type");
Yue Chen9ab6d712017-01-12 15:50:46 -0800729 }
730}
731#endif
732
Yue Chen64550b62017-01-12 12:18:22 -0800733static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yue Chen64550b62017-01-12 12:18:22 -0800734 int mi_row, int mi_col, aom_reader *r,
735#if CONFIG_EXT_PARTITION_TYPES
736 PARTITION_TYPE partition,
737#endif // CONFIG_EXT_PARTITION_TYPES
738 BLOCK_SIZE bsize) {
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +0200739 decode_mbmi_block(pbi, xd, mi_row, mi_col, r,
Yue Chen64550b62017-01-12 12:18:22 -0800740#if CONFIG_EXT_PARTITION_TYPES
741 partition,
742#endif
743 bsize);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700744
Sebastien Alaiwan1bc94fc2017-10-31 10:25:17 +0100745#if !(NC_MODE_INFO)
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +0200746 decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
Yue Chen9ab6d712017-01-12 15:50:46 -0800747#endif
Yue Chen64550b62017-01-12 12:18:22 -0800748}
749
Yaowu Xuf883b422016-08-30 14:01:10 -0700750static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
751 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700752 int has_rows, int has_cols,
Jingning Han1beb0102016-12-07 11:08:30 -0800753 BLOCK_SIZE bsize) {
Alex Converse55c6bde2017-01-12 15:55:31 -0800754#if CONFIG_UNPOISON_PARTITION_CTX
755 const int ctx =
756 partition_plane_context(xd, mi_row, mi_col, has_rows, has_cols, bsize);
Alex Converse55c6bde2017-01-12 15:55:31 -0800757#else
Jingning Han1beb0102016-12-07 11:08:30 -0800758 const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
Alex Converse55c6bde2017-01-12 15:55:31 -0800759#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700760 PARTITION_TYPE p;
Thomas Daviesc2ec0e42017-01-11 16:27:27 +0000761 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
762 (void)cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700763
Jingning Han5fe79db2017-03-27 15:10:30 -0700764 aom_cdf_prob *partition_cdf = (ctx >= 0) ? ec_ctx->partition_cdf[ctx] : NULL;
Jingning Han5fe79db2017-03-27 15:10:30 -0700765
Rupert Swarbrickb95cf122017-07-31 16:51:12 +0100766 if (has_rows && has_cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700767#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +0100768 const int num_partition_types =
769 (mi_width_log2_lookup[bsize] > mi_width_log2_lookup[BLOCK_8X8])
770 ? EXT_PARTITION_TYPES
771 : PARTITION_TYPES;
Alex Converse57795a42017-03-14 12:18:25 -0700772#else
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +0100773 const int num_partition_types = PARTITION_TYPES;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700774#endif // CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +0100775 p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, num_partition_types,
776 ACCT_STR);
Rupert Swarbrickb95cf122017-07-31 16:51:12 +0100777 } else if (!has_rows && has_cols) {
Stanislav Vitvitskyy8711cf52017-08-18 15:17:57 -0700778 assert(bsize > BLOCK_8X8);
779 aom_cdf_prob cdf[2];
780 partition_gather_vert_alike(cdf, partition_cdf);
781 assert(cdf[1] == AOM_ICDF(CDF_PROB_TOP));
782 p = aom_read_cdf(r, cdf, 2, ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
783 // gather cols
Rupert Swarbrickb95cf122017-07-31 16:51:12 +0100784 } else if (has_rows && !has_cols) {
Stanislav Vitvitskyy8711cf52017-08-18 15:17:57 -0700785 assert(bsize > BLOCK_8X8);
786 aom_cdf_prob cdf[2];
787 partition_gather_horz_alike(cdf, partition_cdf);
788 assert(cdf[1] == AOM_ICDF(CDF_PROB_TOP));
789 p = aom_read_cdf(r, cdf, 2, ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Rupert Swarbrickb95cf122017-07-31 16:51:12 +0100790 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791 p = PARTITION_SPLIT;
Rupert Swarbrickb95cf122017-07-31 16:51:12 +0100792 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700793
Yaowu Xuc27fc142016-08-22 16:08:15 -0700794 return p;
795}
796
Yaowu Xuc27fc142016-08-22 16:08:15 -0700797// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xuf883b422016-08-30 14:01:10 -0700798static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Yaowu Xuf883b422016-08-30 14:01:10 -0700799 int mi_row, int mi_col, aom_reader *r,
Jingning Hanea10ad42017-07-20 11:19:08 -0700800 BLOCK_SIZE bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700801 AV1_COMMON *const cm = &pbi->common;
Jingning Hanff17e162016-12-07 17:58:18 -0800802 const int num_8x8_wh = mi_size_wide[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700803 const int hbs = num_8x8_wh >> 1;
Rupert Swarbrick3dd33912017-09-12 14:24:11 +0100804#if CONFIG_EXT_PARTITION_TYPES && CONFIG_EXT_PARTITION_TYPES_AB
805 const int qbs = num_8x8_wh >> 2;
806#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700807 PARTITION_TYPE partition;
808 BLOCK_SIZE subsize;
809#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick93c39e92017-07-12 11:11:02 +0100810 const int quarter_step = num_8x8_wh / 4;
811 int i;
Rupert Swarbrick3dd33912017-09-12 14:24:11 +0100812#if !CONFIG_EXT_PARTITION_TYPES_AB
813 BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
814#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700815#endif
816 const int has_rows = (mi_row + hbs) < cm->mi_rows;
817 const int has_cols = (mi_col + hbs) < cm->mi_cols;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700818
819 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
820
Jingning Hancd959762017-03-27 14:49:59 -0700821 partition = (bsize < BLOCK_8X8) ? PARTITION_NONE
822 : read_partition(cm, xd, mi_row, mi_col, r,
823 has_rows, has_cols, bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700824 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700825
Rupert Swarbrick415c8f12017-10-09 16:26:23 +0100826 // Check the bitstream is conformant: if there is subsampling on the
827 // chroma planes, subsize must subsample to a valid block size.
828 const struct macroblockd_plane *const pd_u = &xd->plane[1];
829 if (get_plane_block_size(subsize, pd_u) == BLOCK_INVALID) {
830 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
831 "Block size %dx%d invalid with this subsampling mode",
832 block_size_wide[subsize], block_size_high[subsize]);
833 }
834
Rupert Swarbrick668d3d92017-09-06 16:09:51 +0100835#define DEC_BLOCK_STX_ARG
Rupert Swarbrick668d3d92017-09-06 16:09:51 +0100836#if CONFIG_EXT_PARTITION_TYPES
837#define DEC_BLOCK_EPT_ARG partition,
838#else
839#define DEC_BLOCK_EPT_ARG
840#endif
841#define DEC_BLOCK(db_r, db_c, db_subsize) \
842 decode_block(pbi, xd, DEC_BLOCK_STX_ARG(db_r), (db_c), r, \
843 DEC_BLOCK_EPT_ARG(db_subsize))
844#define DEC_PARTITION(db_r, db_c, db_subsize) \
845 decode_partition(pbi, xd, DEC_BLOCK_STX_ARG(db_r), (db_c), r, (db_subsize))
846
Debargha Mukherjeeedced252017-10-20 00:02:00 -0700847 switch (partition) {
848 case PARTITION_NONE: DEC_BLOCK(mi_row, mi_col, subsize); break;
849 case PARTITION_HORZ:
850 DEC_BLOCK(mi_row, mi_col, subsize);
851 if (has_rows) DEC_BLOCK(mi_row + hbs, mi_col, subsize);
852 break;
853 case PARTITION_VERT:
854 DEC_BLOCK(mi_row, mi_col, subsize);
855 if (has_cols) DEC_BLOCK(mi_row, mi_col + hbs, subsize);
856 break;
857 case PARTITION_SPLIT:
858 DEC_PARTITION(mi_row, mi_col, subsize);
859 DEC_PARTITION(mi_row, mi_col + hbs, subsize);
860 DEC_PARTITION(mi_row + hbs, mi_col, subsize);
861 DEC_PARTITION(mi_row + hbs, mi_col + hbs, subsize);
862 break;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700863#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick3dd33912017-09-12 14:24:11 +0100864#if CONFIG_EXT_PARTITION_TYPES_AB
Debargha Mukherjeeedced252017-10-20 00:02:00 -0700865 case PARTITION_HORZ_A:
866 DEC_BLOCK(mi_row, mi_col, get_subsize(bsize, PARTITION_HORZ_4));
867 DEC_BLOCK(mi_row + qbs, mi_col, get_subsize(bsize, PARTITION_HORZ_4));
868 DEC_BLOCK(mi_row + hbs, mi_col, subsize);
869 break;
870 case PARTITION_HORZ_B:
871 DEC_BLOCK(mi_row, mi_col, subsize);
872 DEC_BLOCK(mi_row + hbs, mi_col, get_subsize(bsize, PARTITION_HORZ_4));
873 if (mi_row + 3 * qbs < cm->mi_rows)
874 DEC_BLOCK(mi_row + 3 * qbs, mi_col,
875 get_subsize(bsize, PARTITION_HORZ_4));
876 break;
877 case PARTITION_VERT_A:
878 DEC_BLOCK(mi_row, mi_col, get_subsize(bsize, PARTITION_VERT_4));
879 DEC_BLOCK(mi_row, mi_col + qbs, get_subsize(bsize, PARTITION_VERT_4));
880 DEC_BLOCK(mi_row, mi_col + hbs, subsize);
881 break;
882 case PARTITION_VERT_B:
883 DEC_BLOCK(mi_row, mi_col, subsize);
884 DEC_BLOCK(mi_row, mi_col + hbs, get_subsize(bsize, PARTITION_VERT_4));
885 if (mi_col + 3 * qbs < cm->mi_cols)
886 DEC_BLOCK(mi_row, mi_col + 3 * qbs,
887 get_subsize(bsize, PARTITION_VERT_4));
888 break;
Rupert Swarbrick3dd33912017-09-12 14:24:11 +0100889#else
Debargha Mukherjeeedced252017-10-20 00:02:00 -0700890 case PARTITION_HORZ_A:
891 DEC_BLOCK(mi_row, mi_col, bsize2);
892 DEC_BLOCK(mi_row, mi_col + hbs, bsize2);
893 DEC_BLOCK(mi_row + hbs, mi_col, subsize);
894 break;
895 case PARTITION_HORZ_B:
896 DEC_BLOCK(mi_row, mi_col, subsize);
897 DEC_BLOCK(mi_row + hbs, mi_col, bsize2);
898 DEC_BLOCK(mi_row + hbs, mi_col + hbs, bsize2);
899 break;
900 case PARTITION_VERT_A:
901 DEC_BLOCK(mi_row, mi_col, bsize2);
902 DEC_BLOCK(mi_row + hbs, mi_col, bsize2);
903 DEC_BLOCK(mi_row, mi_col + hbs, subsize);
904 break;
905 case PARTITION_VERT_B:
906 DEC_BLOCK(mi_row, mi_col, subsize);
907 DEC_BLOCK(mi_row, mi_col + hbs, bsize2);
908 DEC_BLOCK(mi_row + hbs, mi_col + hbs, bsize2);
909 break;
Rupert Swarbrick3dd33912017-09-12 14:24:11 +0100910#endif
Debargha Mukherjeeedced252017-10-20 00:02:00 -0700911 case PARTITION_HORZ_4:
912 for (i = 0; i < 4; ++i) {
913 int this_mi_row = mi_row + i * quarter_step;
914 if (i > 0 && this_mi_row >= cm->mi_rows) break;
915 DEC_BLOCK(this_mi_row, mi_col, subsize);
916 }
917 break;
918 case PARTITION_VERT_4:
919 for (i = 0; i < 4; ++i) {
920 int this_mi_col = mi_col + i * quarter_step;
921 if (i > 0 && this_mi_col >= cm->mi_cols) break;
922 DEC_BLOCK(mi_row, this_mi_col, subsize);
923 }
924 break;
Rupert Swarbrick93c39e92017-07-12 11:11:02 +0100925#endif // CONFIG_EXT_PARTITION_TYPES
Debargha Mukherjeeedced252017-10-20 00:02:00 -0700926 default: assert(0 && "Invalid partition type");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700927 }
928
Rupert Swarbrick668d3d92017-09-06 16:09:51 +0100929#undef DEC_PARTITION
930#undef DEC_BLOCK
931#undef DEC_BLOCK_EPT_ARG
932#undef DEC_BLOCK_STX_ARG
933
Yaowu Xuc27fc142016-08-22 16:08:15 -0700934#if CONFIG_EXT_PARTITION_TYPES
Alex Converseffabff32017-03-27 09:52:19 -0700935 update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700936#else
937 // update partition context
938 if (bsize >= BLOCK_8X8 &&
939 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
Jingning Han1beb0102016-12-07 11:08:30 -0800940 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
David Barkerf8935c92016-10-26 14:54:06 +0100941#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xud71be782016-10-14 08:47:03 -0700942
Cheng Chenf572cd32017-08-25 18:34:51 -0700943#if CONFIG_LPF_SB
944 if (bsize == cm->sb_size) {
Cheng Chena4b27de2017-08-31 16:05:19 -0700945 int filt_lvl;
946 if (mi_row == 0 && mi_col == 0) {
947 filt_lvl = aom_read_literal(r, 6, ACCT_STR);
Cheng Chen41d37c22017-09-08 19:00:21 -0700948 cm->mi_grid_visible[0]->mbmi.reuse_sb_lvl = 0;
949 cm->mi_grid_visible[0]->mbmi.delta = 0;
950 cm->mi_grid_visible[0]->mbmi.sign = 0;
Cheng Chena4b27de2017-08-31 16:05:19 -0700951 } else {
952 int prev_mi_row, prev_mi_col;
953 if (mi_col - MAX_MIB_SIZE < 0) {
954 prev_mi_row = mi_row - MAX_MIB_SIZE;
955 prev_mi_col = mi_col;
956 } else {
957 prev_mi_row = mi_row;
958 prev_mi_col = mi_col - MAX_MIB_SIZE;
959 }
Cheng Chenc7855b12017-09-05 10:49:08 -0700960
Cheng Chen41d37c22017-09-08 19:00:21 -0700961 MB_MODE_INFO *curr_mbmi =
962 &cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi;
963 MB_MODE_INFO *prev_mbmi =
964 &cm->mi_grid_visible[prev_mi_row * cm->mi_stride + prev_mi_col]->mbmi;
965 const uint8_t prev_lvl = prev_mbmi->filt_lvl;
Cheng Chena4b27de2017-08-31 16:05:19 -0700966
Cheng Chen41d37c22017-09-08 19:00:21 -0700967 const int reuse_ctx = prev_mbmi->reuse_sb_lvl;
968 const int reuse_prev_lvl = aom_read_symbol(
969 r, xd->tile_ctx->lpf_reuse_cdf[reuse_ctx], 2, ACCT_STR);
970 curr_mbmi->reuse_sb_lvl = reuse_prev_lvl;
971
Cheng Chenc7855b12017-09-05 10:49:08 -0700972 if (reuse_prev_lvl) {
Cheng Chena4b27de2017-08-31 16:05:19 -0700973 filt_lvl = prev_lvl;
Cheng Chen41d37c22017-09-08 19:00:21 -0700974 curr_mbmi->delta = 0;
975 curr_mbmi->sign = 0;
Cheng Chenc7855b12017-09-05 10:49:08 -0700976 } else {
Cheng Chen41d37c22017-09-08 19:00:21 -0700977 const int delta_ctx = prev_mbmi->delta;
978 unsigned int delta = aom_read_symbol(
979 r, xd->tile_ctx->lpf_delta_cdf[delta_ctx], DELTA_RANGE, ACCT_STR);
980 curr_mbmi->delta = delta;
Cheng Chenf89ca3e2017-09-07 14:47:47 -0700981 delta *= LPF_STEP;
Cheng Chenc7855b12017-09-05 10:49:08 -0700982
983 if (delta) {
Cheng Chen41d37c22017-09-08 19:00:21 -0700984 const int sign_ctx = prev_mbmi->sign;
985 const int sign = aom_read_symbol(
986 r, xd->tile_ctx->lpf_sign_cdf[reuse_ctx][sign_ctx], 2, ACCT_STR);
987 curr_mbmi->sign = sign;
Cheng Chenc7855b12017-09-05 10:49:08 -0700988 filt_lvl = sign ? prev_lvl + delta : prev_lvl - delta;
989 } else {
990 filt_lvl = prev_lvl;
Cheng Chen41d37c22017-09-08 19:00:21 -0700991 curr_mbmi->sign = 0;
Cheng Chenc7855b12017-09-05 10:49:08 -0700992 }
Cheng Chena4b27de2017-08-31 16:05:19 -0700993 }
994 }
Cheng Chen5589d712017-09-05 12:03:25 -0700995
996 av1_loop_filter_sb_level_init(cm, mi_row, mi_col, filt_lvl);
Cheng Chenf572cd32017-08-25 18:34:51 -0700997 }
998#endif
999
Jean-Marc Valin01435132017-02-18 14:12:53 -05001000#if CONFIG_CDEF
Jingning Handf068332017-05-09 09:03:17 -07001001 if (bsize == cm->sb_size) {
Cheng Chenf5bdeac2017-07-24 14:31:30 -07001002 int width_step = mi_size_wide[BLOCK_64X64];
1003 int height_step = mi_size_wide[BLOCK_64X64];
1004 int w, h;
1005 for (h = 0; (h < mi_size_high[cm->sb_size]) && (mi_row + h < cm->mi_rows);
1006 h += height_step) {
1007 for (w = 0; (w < mi_size_wide[cm->sb_size]) && (mi_col + w < cm->mi_cols);
1008 w += width_step) {
1009 if (!cm->all_lossless && !sb_all_skip(cm, mi_row + h, mi_col + w))
1010 cm->mi_grid_visible[(mi_row + h) * cm->mi_stride + (mi_col + w)]
1011 ->mbmi.cdef_strength =
1012 aom_read_literal(r, cm->cdef_bits, ACCT_STR);
1013 else
1014 cm->mi_grid_visible[(mi_row + h) * cm->mi_stride + (mi_col + w)]
1015 ->mbmi.cdef_strength = -1;
1016 }
Yaowu Xud71be782016-10-14 08:47:03 -07001017 }
1018 }
Jean-Marc Valin01435132017-02-18 14:12:53 -05001019#endif // CONFIG_CDEF
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001020#if CONFIG_LOOP_RESTORATION
1021 for (int plane = 0; plane < MAX_MB_PLANE; ++plane) {
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001022 int rcol0, rcol1, rrow0, rrow1, tile_tl_idx;
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001023 if (av1_loop_restoration_corners_in_sb(cm, plane, mi_row, mi_col, bsize,
1024 &rcol0, &rcol1, &rrow0, &rrow1,
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001025 &tile_tl_idx)) {
1026 const int rstride = cm->rst_info[plane].horz_units_per_tile;
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001027 for (int rrow = rrow0; rrow < rrow1; ++rrow) {
1028 for (int rcol = rcol0; rcol < rcol1; ++rcol) {
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001029 const int rtile_idx = tile_tl_idx + rcol + rrow * rstride;
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001030 loop_restoration_read_sb_coeffs(cm, xd, r, plane, rtile_idx);
1031 }
1032 }
1033 }
1034 }
1035#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001036}
1037
Yaowu Xuc27fc142016-08-22 16:08:15 -07001038static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
1039 const size_t read_size,
Yaowu Xuf883b422016-08-30 14:01:10 -07001040 struct aom_internal_error_info *error_info,
Yunqing Wang0e141b52017-11-02 15:08:58 -07001041 aom_reader *r, uint8_t allow_update_cdf,
Alex Converseeb780e72016-12-13 12:46:41 -08001042#if CONFIG_ANS && ANS_MAX_SYMBOLS
1043 int window_size,
1044#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
1045 aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001046 // Validate the calculated partition length. If the buffer
1047 // described by the partition can't be fully read, then restrict
1048 // it to the portion that can be (for EC mode) or throw an error.
1049 if (!read_is_valid(data, read_size, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07001050 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001051 "Truncated packet or corrupt tile length");
1052
Alex Converse2cdf0d82016-12-13 13:53:09 -08001053#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08001054 r->window_size = window_size;
Alex Converse2cdf0d82016-12-13 13:53:09 -08001055#endif
Alex Converse346440b2017-01-03 13:47:37 -08001056 if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07001057 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001058 "Failed to allocate bool decoder %d", 1);
Yunqing Wang0e141b52017-11-02 15:08:58 -07001059
1060 r->allow_update_cdf = allow_update_cdf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001061}
Yaowu Xuc27fc142016-08-22 16:08:15 -07001062
Yaowu Xuf883b422016-08-30 14:01:10 -07001063static void setup_segmentation(AV1_COMMON *const cm,
1064 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001065 struct segmentation *const seg = &cm->seg;
1066 int i, j;
1067
1068 seg->update_map = 0;
1069 seg->update_data = 0;
Ryandd8df162017-09-27 15:40:13 -07001070 seg->temporal_update = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001071
Yaowu Xuf883b422016-08-30 14:01:10 -07001072 seg->enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001073 if (!seg->enabled) return;
1074
1075 // Segmentation map update
1076 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
1077 seg->update_map = 1;
1078 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001079 seg->update_map = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001080 }
1081 if (seg->update_map) {
1082 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
1083 seg->temporal_update = 0;
1084 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001085 seg->temporal_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001086 }
1087 }
1088
1089 // Segmentation data update
Yaowu Xuf883b422016-08-30 14:01:10 -07001090 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001091 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001092 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001093
1094 for (i = 0; i < MAX_SEGMENTS; i++) {
1095 for (j = 0; j < SEG_LVL_MAX; j++) {
1096 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001097 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001098 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001099 av1_enable_segfeature(seg, i, j);
1100 data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
1101 if (av1_is_segfeature_signed(j))
1102 data = aom_rb_read_bit(rb) ? -data : data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001103 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001104 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001105 }
1106 }
1107 }
1108}
1109
1110#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07001111static void decode_restoration_mode(AV1_COMMON *cm,
1112 struct aom_read_bit_buffer *rb) {
Rupert Swarbrick4596deb2017-11-07 18:06:38 +00001113 for (int p = 0; p < MAX_MB_PLANE; ++p) {
1114 RestorationInfo *rsi = &cm->rst_info[p];
Debargha Mukherjeed23ceea2017-05-18 20:33:52 -07001115 if (aom_rb_read_bit(rb)) {
1116 rsi->frame_restoration_type =
1117 aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
1118 } else {
Debargha Mukherjeea3d4fe52017-05-19 16:22:54 -07001119 rsi->frame_restoration_type =
1120 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
Debargha Mukherjeed23ceea2017-05-18 20:33:52 -07001121 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001122 }
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08001123 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
1124 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
1125 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Rupert Swarbrick4596deb2017-11-07 18:06:38 +00001126 const int qsize = RESTORATION_TILESIZE_MAX >> 2;
1127 for (int p = 0; p < MAX_MB_PLANE; ++p)
1128 cm->rst_info[p].restoration_unit_size = qsize;
1129
1130 RestorationInfo *rsi = &cm->rst_info[0];
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001131 rsi->restoration_unit_size <<= aom_rb_read_bit(rb);
Rupert Swarbrick4596deb2017-11-07 18:06:38 +00001132 if (rsi->restoration_unit_size != qsize) {
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001133 rsi->restoration_unit_size <<= aom_rb_read_bit(rb);
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08001134 }
Rupert Swarbrick4596deb2017-11-07 18:06:38 +00001135 } else {
1136 const int size = RESTORATION_TILESIZE_MAX;
1137 for (int p = 0; p < MAX_MB_PLANE; ++p)
1138 cm->rst_info[p].restoration_unit_size = size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08001139 }
Rupert Swarbrick4596deb2017-11-07 18:06:38 +00001140
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07001141 int s = AOMMIN(cm->subsampling_x, cm->subsampling_y);
1142 if (s && (cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
1143 cm->rst_info[2].frame_restoration_type != RESTORE_NONE)) {
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001144 cm->rst_info[1].restoration_unit_size =
1145 cm->rst_info[0].restoration_unit_size >> (aom_rb_read_bit(rb) * s);
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07001146 } else {
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001147 cm->rst_info[1].restoration_unit_size =
1148 cm->rst_info[0].restoration_unit_size;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07001149 }
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001150 cm->rst_info[2].restoration_unit_size = cm->rst_info[1].restoration_unit_size;
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07001151}
1152
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -07001153static void read_wiener_filter(int wiener_win, WienerInfo *wiener_info,
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07001154 WienerInfo *ref_wiener_info, aom_reader *rb) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001155 memset(wiener_info->vfilter, 0, sizeof(wiener_info->vfilter));
1156 memset(wiener_info->hfilter, 0, sizeof(wiener_info->hfilter));
1157
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -07001158 if (wiener_win == WIENER_WIN)
1159 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
1160 aom_read_primitive_refsubexpfin(
1161 rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1162 WIENER_FILT_TAP0_SUBEXP_K,
1163 ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV, ACCT_STR) +
1164 WIENER_FILT_TAP0_MINV;
1165 else
1166 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] = 0;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001167 wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07001168 aom_read_primitive_refsubexpfin(
1169 rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1170 WIENER_FILT_TAP1_SUBEXP_K,
Debargha Mukherjeee23d5c32017-04-13 15:33:58 -07001171 ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV, ACCT_STR) +
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07001172 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001173 wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07001174 aom_read_primitive_refsubexpfin(
1175 rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1176 WIENER_FILT_TAP2_SUBEXP_K,
Debargha Mukherjeee23d5c32017-04-13 15:33:58 -07001177 ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV, ACCT_STR) +
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07001178 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00001179 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001180 wiener_info->vfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00001181 -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
1182 wiener_info->vfilter[2]);
1183
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -07001184 if (wiener_win == WIENER_WIN)
1185 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
1186 aom_read_primitive_refsubexpfin(
1187 rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1188 WIENER_FILT_TAP0_SUBEXP_K,
1189 ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV, ACCT_STR) +
1190 WIENER_FILT_TAP0_MINV;
1191 else
1192 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] = 0;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001193 wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07001194 aom_read_primitive_refsubexpfin(
1195 rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1196 WIENER_FILT_TAP1_SUBEXP_K,
Debargha Mukherjeee23d5c32017-04-13 15:33:58 -07001197 ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV, ACCT_STR) +
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07001198 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001199 wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07001200 aom_read_primitive_refsubexpfin(
1201 rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1202 WIENER_FILT_TAP2_SUBEXP_K,
Debargha Mukherjeee23d5c32017-04-13 15:33:58 -07001203 ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV, ACCT_STR) +
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07001204 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00001205 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001206 wiener_info->hfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00001207 -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
1208 wiener_info->hfilter[2]);
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07001209 memcpy(ref_wiener_info, wiener_info, sizeof(*wiener_info));
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07001210}
1211
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07001212static void read_sgrproj_filter(SgrprojInfo *sgrproj_info,
1213 SgrprojInfo *ref_sgrproj_info, aom_reader *rb) {
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07001214 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
1215 sgrproj_info->xqd[0] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07001216 aom_read_primitive_refsubexpfin(
1217 rb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
Debargha Mukherjeee23d5c32017-04-13 15:33:58 -07001218 ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0, ACCT_STR) +
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07001219 SGRPROJ_PRJ_MIN0;
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07001220 sgrproj_info->xqd[1] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07001221 aom_read_primitive_refsubexpfin(
1222 rb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
Debargha Mukherjeee23d5c32017-04-13 15:33:58 -07001223 ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1, ACCT_STR) +
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07001224 SGRPROJ_PRJ_MIN1;
1225 memcpy(ref_sgrproj_info, sgrproj_info, sizeof(*sgrproj_info));
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07001226}
1227
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001228static void loop_restoration_read_sb_coeffs(const AV1_COMMON *const cm,
1229 MACROBLOCKD *xd,
1230 aom_reader *const r, int plane,
1231 int rtile_idx) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001232 const RestorationInfo *rsi = &cm->rst_info[plane];
1233 RestorationUnitInfo *rui = &rsi->unit_info[rtile_idx];
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001234 if (rsi->frame_restoration_type == RESTORE_NONE) return;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001235
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001236 const int wiener_win = (plane > 0) ? WIENER_WIN_CHROMA : WIENER_WIN;
1237 WienerInfo *wiener_info = xd->wiener_info + plane;
1238 SgrprojInfo *sgrproj_info = xd->sgrproj_info + plane;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001239
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001240 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001241 rui->restoration_type =
Debargha Mukherjeebc732ef2017-10-12 12:40:25 -07001242 aom_read_symbol(r, xd->tile_ctx->switchable_restore_cdf,
1243 RESTORE_SWITCHABLE_TYPES, ACCT_STR);
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001244 switch (rui->restoration_type) {
1245 case RESTORE_WIENER:
1246 read_wiener_filter(wiener_win, &rui->wiener_info, wiener_info, r);
1247 break;
1248 case RESTORE_SGRPROJ:
1249 read_sgrproj_filter(&rui->sgrproj_info, sgrproj_info, r);
1250 break;
1251 default: assert(rui->restoration_type == RESTORE_NONE); break;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001252 }
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001253 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjeebc732ef2017-10-12 12:40:25 -07001254#if CONFIG_NEW_MULTISYMBOL
1255 if (aom_read_symbol(r, xd->tile_ctx->wiener_restore_cdf, 2, ACCT_STR)) {
1256#else
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001257 if (aom_read(r, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
Debargha Mukherjeebc732ef2017-10-12 12:40:25 -07001258#endif // CONFIG_NEW_MULTISYMBOL
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001259 rui->restoration_type = RESTORE_WIENER;
1260 read_wiener_filter(wiener_win, &rui->wiener_info, wiener_info, r);
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001261 } else {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001262 rui->restoration_type = RESTORE_NONE;
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001263 }
1264 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjeebc732ef2017-10-12 12:40:25 -07001265#if CONFIG_NEW_MULTISYMBOL
1266 if (aom_read_symbol(r, xd->tile_ctx->sgrproj_restore_cdf, 2, ACCT_STR)) {
1267#else
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001268 if (aom_read(r, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
Debargha Mukherjeebc732ef2017-10-12 12:40:25 -07001269#endif // CONFIG_NEW_MULTISYMBOL
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001270 rui->restoration_type = RESTORE_SGRPROJ;
1271 read_sgrproj_filter(&rui->sgrproj_info, sgrproj_info, r);
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001272 } else {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001273 rui->restoration_type = RESTORE_NONE;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001274 }
1275 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001276}
1277#endif // CONFIG_LOOP_RESTORATION
1278
Yaowu Xuf883b422016-08-30 14:01:10 -07001279static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001280 struct loopfilter *lf = &cm->lf;
Cheng Chenf572cd32017-08-25 18:34:51 -07001281#if !CONFIG_LPF_SB
Cheng Chen13fc8192017-08-19 11:49:28 -07001282#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -07001283 lf->filter_level[0] = aom_rb_read_literal(rb, 6);
1284 lf->filter_level[1] = aom_rb_read_literal(rb, 6);
1285 if (lf->filter_level[0] || lf->filter_level[1]) {
Cheng Chene94df5c2017-07-19 17:25:33 -07001286 lf->filter_level_u = aom_rb_read_literal(rb, 6);
1287 lf->filter_level_v = aom_rb_read_literal(rb, 6);
1288 }
Cheng Chen179479f2017-08-04 10:56:39 -07001289#else
1290 lf->filter_level = aom_rb_read_literal(rb, 6);
Cheng Chene94df5c2017-07-19 17:25:33 -07001291#endif
Cheng Chenf572cd32017-08-25 18:34:51 -07001292#endif // CONFIG_LPF_SB
Yaowu Xuf883b422016-08-30 14:01:10 -07001293 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001294
1295 // Read in loop filter deltas applied at the MB level based on mode or ref
1296 // frame.
1297 lf->mode_ref_delta_update = 0;
1298
Yaowu Xuf883b422016-08-30 14:01:10 -07001299 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001300 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001301 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001302 if (lf->mode_ref_delta_update) {
1303 int i;
1304
1305 for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07001306 if (aom_rb_read_bit(rb))
1307 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001308
1309 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07001310 if (aom_rb_read_bit(rb))
1311 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001312 }
1313 }
1314}
1315
Jean-Marc Valin01435132017-02-18 14:12:53 -05001316#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +01001317static void setup_cdef(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04001318 int i;
Steinar Midtskogen59782122017-07-20 08:49:43 +02001319#if CONFIG_CDEF_SINGLEPASS
1320 cm->cdef_pri_damping = cm->cdef_sec_damping = aom_rb_read_literal(rb, 2) + 3;
1321#else
Steinar Midtskogen94de0aa2017-08-02 10:30:12 +02001322 cm->cdef_pri_damping = aom_rb_read_literal(rb, 1) + 5;
1323 cm->cdef_sec_damping = aom_rb_read_literal(rb, 2) + 3;
Steinar Midtskogen59782122017-07-20 08:49:43 +02001324#endif
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04001325 cm->cdef_bits = aom_rb_read_literal(rb, 2);
1326 cm->nb_cdef_strengths = 1 << cm->cdef_bits;
1327 for (i = 0; i < cm->nb_cdef_strengths; i++) {
1328 cm->cdef_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
Steinar Midtskogen1c1161f2017-09-08 15:03:51 +02001329 cm->cdef_uv_strengths[i] = cm->subsampling_x == cm->subsampling_y
1330 ? aom_rb_read_literal(rb, CDEF_STRENGTH_BITS)
1331 : 0;
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04001332 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001333}
Jean-Marc Valin01435132017-02-18 14:12:53 -05001334#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -07001335
Yaowu Xuf883b422016-08-30 14:01:10 -07001336static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
1337 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001338}
1339
Yaowu Xuf883b422016-08-30 14:01:10 -07001340static void setup_quantization(AV1_COMMON *const cm,
1341 struct aom_read_bit_buffer *rb) {
1342 cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001343 cm->y_dc_delta_q = read_delta_q(rb);
1344 cm->uv_dc_delta_q = read_delta_q(rb);
1345 cm->uv_ac_delta_q = read_delta_q(rb);
1346 cm->dequant_bit_depth = cm->bit_depth;
1347#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07001348 cm->using_qmatrix = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001349 if (cm->using_qmatrix) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001350 cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
1351 cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001352 } else {
1353 cm->min_qmlevel = 0;
1354 cm->max_qmlevel = 0;
1355 }
1356#endif
1357}
1358
Alex Converse05a3e7d2017-05-16 12:20:07 -07001359// Build y/uv dequant values based on segmentation.
Yaowu Xuf883b422016-08-30 14:01:10 -07001360static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001361#if CONFIG_AOM_QM
Alex Converse05a3e7d2017-05-16 12:20:07 -07001362 const int using_qm = cm->using_qmatrix;
1363 const int minqm = cm->min_qmlevel;
1364 const int maxqm = cm->max_qmlevel;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001365#endif
Alex Converse05a3e7d2017-05-16 12:20:07 -07001366 // When segmentation is disabled, only the first value is used. The
1367 // remaining are don't cares.
1368 const int max_segments = cm->seg.enabled ? MAX_SEGMENTS : 1;
1369 for (int i = 0; i < max_segments; ++i) {
1370 const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
1371 cm->y_dequant[i][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
1372 cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
1373 cm->uv_dequant[i][0] =
Yaowu Xuf883b422016-08-30 14:01:10 -07001374 av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
Alex Converse05a3e7d2017-05-16 12:20:07 -07001375 cm->uv_dequant[i][1] =
Yaowu Xuf883b422016-08-30 14:01:10 -07001376 av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001377#if CONFIG_AOM_QM
Alex Converse05a3e7d2017-05-16 12:20:07 -07001378 const int lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
1379 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
1380 // NB: depends on base index so there is only 1 set per frame
Yaowu Xuc27fc142016-08-22 16:08:15 -07001381 // No quant weighting when lossless or signalled not using QM
Alex Converse05a3e7d2017-05-16 12:20:07 -07001382 const int qmlevel = (lossless || using_qm == 0)
1383 ? NUM_QM_LEVELS - 1
1384 : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
Thomas Davies6675adf2017-05-04 17:39:21 +01001385 for (int j = 0; j < TX_SIZES_ALL; ++j) {
Thomas Daviesdd3cf832017-10-20 15:49:57 +01001386 cm->y_iqmatrix[i][j] = aom_iqmatrix(cm, qmlevel, 0, j);
1387 cm->uv_iqmatrix[i][j] = aom_iqmatrix(cm, qmlevel, 1, j);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001388 }
Alex Converse05a3e7d2017-05-16 12:20:07 -07001389#endif // CONFIG_AOM_QM
Yaowu Xuc27fc142016-08-22 16:08:15 -07001390#if CONFIG_NEW_QUANT
Alex Converse05a3e7d2017-05-16 12:20:07 -07001391 for (int dq = 0; dq < QUANT_PROFILES; dq++) {
1392 for (int b = 0; b < COEF_BANDS; ++b) {
1393 av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
1394 cm->y_dequant_nuq[i][dq][b], NULL, dq);
1395 av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
1396 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001397 }
1398 }
1399#endif // CONFIG_NEW_QUANT
1400 }
1401}
1402
Angie Chiang5678ad92016-11-21 09:38:40 -08001403static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001404 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07001405 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001406}
1407
Yaowu Xuf883b422016-08-30 14:01:10 -07001408static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001409#if CONFIG_FRAME_SUPERRES
1410 cm->render_width = cm->superres_upscaled_width;
1411 cm->render_height = cm->superres_upscaled_height;
1412#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001413 cm->render_width = cm->width;
1414 cm->render_height = cm->height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001415#endif // CONFIG_FRAME_SUPERRES
Yaowu Xuf883b422016-08-30 14:01:10 -07001416 if (aom_rb_read_bit(rb))
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01001417#if CONFIG_FRAME_SIZE
1418 av1_read_frame_size(rb, 16, 16, &cm->render_width, &cm->render_height);
1419#else
Yaowu Xuf883b422016-08-30 14:01:10 -07001420 av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01001421#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001422}
1423
Fergus Simpsond91c8c92017-04-07 12:12:00 -07001424#if CONFIG_FRAME_SUPERRES
Fergus Simpsone7508412017-03-14 18:14:09 -07001425// TODO(afergs): make "struct aom_read_bit_buffer *const rb"?
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001426static void setup_superres(AV1_COMMON *const cm, struct aom_read_bit_buffer *rb,
1427 int *width, int *height) {
1428 cm->superres_upscaled_width = *width;
1429 cm->superres_upscaled_height = *height;
Fergus Simpsone7508412017-03-14 18:14:09 -07001430 if (aom_rb_read_bit(rb)) {
Urvang Joshide71d142017-10-05 12:12:15 -07001431 cm->superres_scale_denominator =
Fergus Simpsone7508412017-03-14 18:14:09 -07001432 (uint8_t)aom_rb_read_literal(rb, SUPERRES_SCALE_BITS);
Urvang Joshide71d142017-10-05 12:12:15 -07001433 cm->superres_scale_denominator += SUPERRES_SCALE_DENOMINATOR_MIN;
Fergus Simpson7b2d1442017-05-22 17:18:33 -07001434 // Don't edit cm->width or cm->height directly, or the buffers won't get
1435 // resized correctly
Urvang Joshi69fde2e2017-10-09 15:34:18 -07001436 av1_calculate_scaled_superres_size(width, height,
1437 cm->superres_scale_denominator);
Fergus Simpsone7508412017-03-14 18:14:09 -07001438 } else {
1439 // 1:1 scaling - ie. no scaling, scale not provided
Urvang Joshide71d142017-10-05 12:12:15 -07001440 cm->superres_scale_denominator = SCALE_NUMERATOR;
Fergus Simpsone7508412017-03-14 18:14:09 -07001441 }
1442}
Fergus Simpsond91c8c92017-04-07 12:12:00 -07001443#endif // CONFIG_FRAME_SUPERRES
Fergus Simpsone7508412017-03-14 18:14:09 -07001444
Yaowu Xuf883b422016-08-30 14:01:10 -07001445static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001446#if CONFIG_SIZE_LIMIT
1447 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Yaowu Xuf883b422016-08-30 14:01:10 -07001448 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001449 "Dimensions of %dx%d beyond allowed size of %dx%d.",
1450 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
1451#endif
1452 if (cm->width != width || cm->height != height) {
1453 const int new_mi_rows =
1454 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
1455 const int new_mi_cols =
1456 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
1457
Yaowu Xuf883b422016-08-30 14:01:10 -07001458 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07001459 // dimensions as well as the overall size.
1460 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001461 if (av1_alloc_context_buffers(cm, width, height))
1462 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001463 "Failed to allocate context buffers");
1464 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001465 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001466 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001467 av1_init_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001468 cm->width = width;
1469 cm->height = height;
1470 }
Rupert Swarbrick1f990a62017-07-11 11:09:33 +01001471
1472 ensure_mv_buffer(cm->cur_frame, cm);
1473 cm->cur_frame->width = cm->width;
1474 cm->cur_frame->height = cm->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001475}
1476
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01001477#if CONFIG_FRAME_SIZE
1478static void setup_frame_size(AV1_COMMON *cm, int frame_size_override_flag,
1479 struct aom_read_bit_buffer *rb) {
1480#else
Yaowu Xuf883b422016-08-30 14:01:10 -07001481static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01001482#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001483 int width, height;
1484 BufferPool *const pool = cm->buffer_pool;
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01001485#if CONFIG_FRAME_SIZE
1486 if (frame_size_override_flag) {
1487 int num_bits_width = cm->seq_params.num_bits_width;
1488 int num_bits_height = cm->seq_params.num_bits_height;
1489 av1_read_frame_size(rb, num_bits_width, num_bits_height, &width, &height);
1490 } else {
1491 width = cm->seq_params.max_frame_width;
1492 height = cm->seq_params.max_frame_height;
1493 }
1494#else
Yaowu Xuf883b422016-08-30 14:01:10 -07001495 av1_read_frame_size(rb, &width, &height);
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01001496#endif
Fergus Simpson7b2d1442017-05-22 17:18:33 -07001497#if CONFIG_FRAME_SUPERRES
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001498 setup_superres(cm, rb, &width, &height);
Fergus Simpson7b2d1442017-05-22 17:18:33 -07001499#endif // CONFIG_FRAME_SUPERRES
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001500 setup_render_size(cm, rb);
Fergus Simpson7b2d1442017-05-22 17:18:33 -07001501 resize_context_buffers(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001502
1503 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07001504 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001505 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
1506 cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001507#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001508 cm->use_highbitdepth,
1509#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07001510 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001511 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
1512 pool->cb_priv)) {
1513 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07001514 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001515 "Failed to allocate frame buffer");
1516 }
1517 unlock_buffer_pool(pool);
1518
1519 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
1520 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
1521 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
1522 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
anorkin76fb1262017-03-22 15:12:12 -07001523#if CONFIG_COLORSPACE_HEADERS
1524 pool->frame_bufs[cm->new_fb_idx].buf.transfer_function =
1525 cm->transfer_function;
1526 pool->frame_bufs[cm->new_fb_idx].buf.chroma_sample_position =
1527 cm->chroma_sample_position;
1528#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001529 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
1530 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
1531 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
1532}
1533
Debargha Mukherjeed2630fa2017-09-22 10:32:51 -07001534static void setup_sb_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
1535 (void)rb;
1536#if CONFIG_EXT_PARTITION
1537 set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
1538#else
1539 set_sb_size(cm, BLOCK_64X64);
1540#endif // CONFIG_EXT_PARTITION
1541}
1542
Yaowu Xuf883b422016-08-30 14:01:10 -07001543static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001544 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07001545 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001546 int this_xss, int this_yss) {
1547 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
1548 ref_yss == this_yss;
1549}
1550
Yaowu Xuf883b422016-08-30 14:01:10 -07001551static void setup_frame_size_with_refs(AV1_COMMON *cm,
1552 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001553 int width, height;
1554 int found = 0, i;
1555 int has_valid_ref_frame = 0;
1556 BufferPool *const pool = cm->buffer_pool;
1557 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001558 if (aom_rb_read_bit(rb)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001559 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
1560 width = buf->y_crop_width;
1561 height = buf->y_crop_height;
1562 cm->render_width = buf->render_width;
1563 cm->render_height = buf->render_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001564#if CONFIG_FRAME_SUPERRES
1565 setup_superres(cm, rb, &width, &height);
1566#endif // CONFIG_FRAME_SUPERRES
Yaowu Xuc27fc142016-08-22 16:08:15 -07001567 found = 1;
1568 break;
1569 }
1570 }
1571
1572 if (!found) {
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01001573#if CONFIG_FRAME_SIZE
1574 int num_bits_width = cm->seq_params.num_bits_width;
1575 int num_bits_height = cm->seq_params.num_bits_height;
1576 av1_read_frame_size(rb, num_bits_width, num_bits_height, &width, &height);
1577#else
Yaowu Xuf883b422016-08-30 14:01:10 -07001578 av1_read_frame_size(rb, &width, &height);
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01001579#endif
Fergus Simpson7b2d1442017-05-22 17:18:33 -07001580#if CONFIG_FRAME_SUPERRES
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001581 setup_superres(cm, rb, &width, &height);
Fergus Simpson7b2d1442017-05-22 17:18:33 -07001582#endif // CONFIG_FRAME_SUPERRES
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001583 setup_render_size(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001584 }
1585
1586 if (width <= 0 || height <= 0)
Yaowu Xuf883b422016-08-30 14:01:10 -07001587 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001588 "Invalid frame size");
1589
1590 // Check to make sure at least one of frames that this frame references
1591 // has valid dimensions.
1592 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
1593 RefBuffer *const ref_frame = &cm->frame_refs[i];
1594 has_valid_ref_frame |=
1595 valid_ref_frame_size(ref_frame->buf->y_crop_width,
1596 ref_frame->buf->y_crop_height, width, height);
1597 }
1598 if (!has_valid_ref_frame)
Yaowu Xuf883b422016-08-30 14:01:10 -07001599 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001600 "Referenced frame has invalid size");
1601 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
1602 RefBuffer *const ref_frame = &cm->frame_refs[i];
1603 if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
1604 ref_frame->buf->subsampling_x,
1605 ref_frame->buf->subsampling_y, cm->bit_depth,
1606 cm->subsampling_x, cm->subsampling_y))
Yaowu Xuf883b422016-08-30 14:01:10 -07001607 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001608 "Referenced frame has incompatible color format");
1609 }
1610
1611 resize_context_buffers(cm, width, height);
1612
1613 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07001614 if (aom_realloc_frame_buffer(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001615 get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
1616 cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001617#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001618 cm->use_highbitdepth,
1619#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07001620 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001621 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
1622 pool->cb_priv)) {
1623 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07001624 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001625 "Failed to allocate frame buffer");
1626 }
1627 unlock_buffer_pool(pool);
1628
1629 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
1630 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
1631 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
1632 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
anorkin76fb1262017-03-22 15:12:12 -07001633#if CONFIG_COLORSPACE_HEADERS
1634 pool->frame_bufs[cm->new_fb_idx].buf.transfer_function =
1635 cm->transfer_function;
1636 pool->frame_bufs[cm->new_fb_idx].buf.chroma_sample_position =
1637 cm->chroma_sample_position;
1638#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001639 pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
1640 pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
1641 pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
1642}
1643
David Barker01563082017-10-09 13:59:16 +01001644#if !CONFIG_OBU
David Barker1a191122017-09-06 15:24:16 +01001645static void read_tile_group_range(AV1Decoder *pbi,
1646 struct aom_read_bit_buffer *const rb) {
1647 AV1_COMMON *const cm = &pbi->common;
1648 const int num_bits = cm->log2_tile_rows + cm->log2_tile_cols;
1649 const int num_tiles =
1650 cm->tile_rows * cm->tile_cols; // Note: May be < (1<<num_bits)
1651 pbi->tg_start = aom_rb_read_literal(rb, num_bits);
1652 pbi->tg_size = 1 + aom_rb_read_literal(rb, num_bits);
1653 if (pbi->tg_start + pbi->tg_size > num_tiles)
1654 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
1655 "Tile group extends past last tile in frame");
1656}
David Barker01563082017-10-09 13:59:16 +01001657#endif // !CONFIG_OBU
David Barker1a191122017-09-06 15:24:16 +01001658
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001659#if CONFIG_MAX_TILE
1660
1661// Same function as av1_read_uniform but reading from uncompresses header wb
1662static int rb_read_uniform(struct aom_read_bit_buffer *const rb, int n) {
1663 const int l = get_unsigned_bits(n);
1664 const int m = (1 << l) - n;
1665 const int v = aom_rb_read_literal(rb, l - 1);
1666 assert(l != 0);
1667 if (v < m)
1668 return v;
1669 else
1670 return (v << 1) - m + aom_rb_read_literal(rb, 1);
1671}
1672
1673static void read_tile_info_max_tile(AV1_COMMON *const cm,
1674 struct aom_read_bit_buffer *const rb) {
Dominic Symesab8bb8b2017-10-11 18:00:52 +02001675 int width_mi = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->mib_size_log2);
1676 int height_mi = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->mib_size_log2);
1677 int width_sb = width_mi >> cm->mib_size_log2;
1678 int height_sb = height_mi >> cm->mib_size_log2;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001679 int start_sb, size_sb, i;
1680
1681 av1_get_tile_limits(cm);
1682 cm->uniform_tile_spacing_flag = aom_rb_read_bit(rb);
1683
1684 // Read tile columns
1685 if (cm->uniform_tile_spacing_flag) {
1686 cm->log2_tile_cols = cm->min_log2_tile_cols;
1687 while (cm->log2_tile_cols < cm->max_log2_tile_cols) {
1688 if (!aom_rb_read_bit(rb)) {
1689 break;
1690 }
1691 cm->log2_tile_cols++;
1692 }
1693 } else {
Dominic Symesf58f1112017-09-25 12:47:40 +02001694 for (i = 0, start_sb = 0; width_sb > 0 && i < MAX_TILE_COLS; i++) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001695 size_sb = 1 + rb_read_uniform(rb, AOMMIN(width_sb, MAX_TILE_WIDTH_SB));
1696 cm->tile_col_start_sb[i] = start_sb;
1697 start_sb += size_sb;
1698 width_sb -= size_sb;
1699 }
1700 cm->tile_cols = i;
Dominic Symesf58f1112017-09-25 12:47:40 +02001701 cm->tile_col_start_sb[i] = start_sb + width_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001702 }
1703 av1_calculate_tile_cols(cm);
1704
1705 // Read tile rows
1706 if (cm->uniform_tile_spacing_flag) {
1707 cm->log2_tile_rows = cm->min_log2_tile_rows;
1708 while (cm->log2_tile_rows < cm->max_log2_tile_rows) {
1709 if (!aom_rb_read_bit(rb)) {
1710 break;
1711 }
1712 cm->log2_tile_rows++;
1713 }
1714 } else {
Dominic Symesf58f1112017-09-25 12:47:40 +02001715 for (i = 0, start_sb = 0; height_sb > 0 && i < MAX_TILE_ROWS; i++) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001716 size_sb =
1717 1 + rb_read_uniform(rb, AOMMIN(height_sb, cm->max_tile_height_sb));
1718 cm->tile_row_start_sb[i] = start_sb;
1719 start_sb += size_sb;
1720 height_sb -= size_sb;
1721 }
1722 cm->tile_rows = i;
Dominic Symesf58f1112017-09-25 12:47:40 +02001723 cm->tile_row_start_sb[i] = start_sb + height_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001724 }
1725 av1_calculate_tile_rows(cm);
1726}
1727#endif
1728
Yaowu Xuf883b422016-08-30 14:01:10 -07001729static void read_tile_info(AV1Decoder *const pbi,
1730 struct aom_read_bit_buffer *const rb) {
1731 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001732#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001733 cm->single_tile_decoding = 0;
1734 if (cm->large_scale_tile) {
1735 struct loopfilter *lf = &cm->lf;
1736
Rupert Swarbrick566155f2017-10-27 11:59:17 +01001737// Figure out single_tile_decoding by loopfilter_level.
1738#if CONFIG_LOOPFILTER_LEVEL
1739 const int no_loopfilter = !(lf->filter_level[0] || lf->filter_level[1]);
1740#else
1741 const int no_loopfilter = !lf->filter_level;
1742#endif
1743 cm->single_tile_decoding = no_loopfilter ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001744// Read the tile width/height
1745#if CONFIG_EXT_PARTITION
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001746 if (cm->sb_size == BLOCK_128X128) {
1747 cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
1748 cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
1749 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001750#endif // CONFIG_EXT_PARTITION
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001751 cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
1752 cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
1753#if CONFIG_EXT_PARTITION
1754 }
1755#endif // CONFIG_EXT_PARTITION
Yaowu Xuc27fc142016-08-22 16:08:15 -07001756
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001757 cm->tile_width <<= cm->mib_size_log2;
1758 cm->tile_height <<= cm->mib_size_log2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001759
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001760 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
1761 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001762
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001763 // Get the number of tiles
1764 cm->tile_cols = 1;
1765 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001766
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001767 cm->tile_rows = 1;
1768 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001769
Yunqing Wang42015d12017-10-17 15:43:49 -07001770#if CONFIG_DEPENDENT_HORZTILES
1771 cm->dependent_horz_tiles = 0;
1772#endif
1773#if CONFIG_LOOPFILTERING_ACROSS_TILES
1774 if (cm->tile_cols * cm->tile_rows > 1)
1775 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
1776 else
1777 cm->loop_filter_across_tiles_enabled = 1;
1778#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
1779
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001780 if (cm->tile_cols * cm->tile_rows > 1) {
1781 // Read the number of bytes used to store tile size
1782 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
1783 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
1784 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001785 } else {
1786#endif // CONFIG_EXT_TILE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001787
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001788#if CONFIG_MAX_TILE
1789 read_tile_info_max_tile(cm, rb);
1790#else
1791 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
1792 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001793
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001794 // columns
1795 max_ones = max_log2_tile_cols - min_log2_tile_cols;
1796 cm->log2_tile_cols = min_log2_tile_cols;
1797 while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001798
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001799 if (cm->log2_tile_cols > 6)
1800 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
1801 "Invalid number of tile columns");
1802
1803 // rows
1804 cm->log2_tile_rows = aom_rb_read_bit(rb);
1805 if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
1806
Rupert Swarbrick5a010aa2017-09-26 16:16:48 +01001807 cm->tile_width =
1808 get_tile_size(cm->mi_cols, cm->log2_tile_cols, &cm->tile_cols);
1809 cm->tile_height =
1810 get_tile_size(cm->mi_rows, cm->log2_tile_rows, &cm->tile_rows);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001811
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001812#endif // CONFIG_MAX_TILE
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08001813#if CONFIG_DEPENDENT_HORZTILES
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001814 if (cm->tile_rows > 1)
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001815 cm->dependent_horz_tiles = aom_rb_read_bit(rb);
1816 else
1817 cm->dependent_horz_tiles = 0;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08001818#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -08001819#if CONFIG_LOOPFILTERING_ACROSS_TILES
Yunqing Wang42015d12017-10-17 15:43:49 -07001820 if (cm->tile_cols * cm->tile_rows > 1)
1821 cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
1822 else
1823 cm->loop_filter_across_tiles_enabled = 1;
Ryan Lei9b02b0e2017-01-30 15:52:20 -08001824#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08001825
Thomas Daviesb25ba502017-07-18 10:18:24 +01001826 // tile size magnitude
1827 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001828#if CONFIG_EXT_TILE
1829 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001830#endif // CONFIG_EXT_TILE
Thomas Davies4974e522016-11-07 17:44:05 +00001831
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04001832// each tile group header is in its own tile group OBU
1833#if !CONFIG_OBU
Thomas Davies80188d12016-10-26 16:08:35 -07001834 // Store an index to the location of the tile group information
1835 pbi->tg_size_bit_offset = rb->bit_offset;
David Barker1a191122017-09-06 15:24:16 +01001836 read_tile_group_range(pbi, rb);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04001837#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001838}
1839
Yaowu Xu4ff59b52017-04-24 12:41:56 -07001840static int mem_get_varsize(const uint8_t *src, int sz) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001841 switch (sz) {
1842 case 1: return src[0];
1843 case 2: return mem_get_le16(src);
1844 case 3: return mem_get_le24(src);
1845 case 4: return mem_get_le32(src);
James Zern88896732017-06-23 15:55:09 -07001846 default: assert(0 && "Invalid size"); return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001847 }
1848}
1849
1850#if CONFIG_EXT_TILE
1851// Reads the next tile returning its size and adjusting '*data' accordingly
1852// based on 'is_last'.
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001853static void get_ls_tile_buffer(
1854 const uint8_t *const data_end, struct aom_internal_error_info *error_info,
1855 const uint8_t **data, aom_decrypt_cb decrypt_cb, void *decrypt_state,
1856 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS], int tile_size_bytes,
1857 int col, int row, int tile_copy_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001858 size_t size;
1859
1860 size_t copy_size = 0;
1861 const uint8_t *copy_data = NULL;
1862
1863 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07001864 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001865 "Truncated packet or corrupt tile length");
1866 if (decrypt_cb) {
1867 uint8_t be_data[4];
1868 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
1869
1870 // Only read number of bytes in cm->tile_size_bytes.
1871 size = mem_get_varsize(be_data, tile_size_bytes);
1872 } else {
1873 size = mem_get_varsize(*data, tile_size_bytes);
1874 }
1875
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001876 // If tile_copy_mode = 1, then the top bit of the tile header indicates copy
1877 // mode.
1878 if (tile_copy_mode && (size >> (tile_size_bytes * 8 - 1)) == 1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001879 // The remaining bits in the top byte signal the row offset
1880 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
1881
1882 // Currently, only use tiles in same column as reference tiles.
1883 copy_data = tile_buffers[row - offset][col].data;
1884 copy_size = tile_buffers[row - offset][col].size;
1885 size = 0;
1886 }
1887
1888 *data += tile_size_bytes;
1889
1890 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07001891 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001892 "Truncated packet or corrupt tile size");
1893
1894 if (size > 0) {
1895 tile_buffers[row][col].data = *data;
1896 tile_buffers[row][col].size = size;
1897 } else {
1898 tile_buffers[row][col].data = copy_data;
1899 tile_buffers[row][col].size = copy_size;
1900 }
1901
1902 *data += size;
1903
1904 tile_buffers[row][col].raw_data_end = *data;
1905}
1906
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001907static void get_ls_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07001908 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001909 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001910 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001911 const int tile_cols = cm->tile_cols;
1912 const int tile_rows = cm->tile_rows;
1913 const int have_tiles = tile_cols * tile_rows > 1;
1914
1915 if (!have_tiles) {
Jingning Han99ffce62017-04-25 15:48:41 -07001916 const size_t tile_size = data_end - data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001917 tile_buffers[0][0].data = data;
1918 tile_buffers[0][0].size = tile_size;
1919 tile_buffers[0][0].raw_data_end = NULL;
1920 } else {
1921 // We locate only the tile buffers that are required, which are the ones
1922 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
1923 // need the last (bottom right) tile buffer, as we need to know where the
1924 // end of the compressed frame buffer is for proper superframe decoding.
1925
1926 const uint8_t *tile_col_data_end[MAX_TILE_COLS];
1927 const uint8_t *const data_start = data;
1928
Yaowu Xuf883b422016-08-30 14:01:10 -07001929 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001930 const int single_row = pbi->dec_tile_row >= 0;
1931 const int tile_rows_start = single_row ? dec_tile_row : 0;
1932 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07001933 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001934 const int single_col = pbi->dec_tile_col >= 0;
1935 const int tile_cols_start = single_col ? dec_tile_col : 0;
1936 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
1937
1938 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
1939 const int tile_size_bytes = pbi->tile_size_bytes;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001940 const int tile_copy_mode =
1941 ((AOMMAX(cm->tile_width, cm->tile_height) << MI_SIZE_LOG2) <= 256) ? 1
1942 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001943 size_t tile_col_size;
1944 int r, c;
1945
1946 // Read tile column sizes for all columns (we need the last tile buffer)
1947 for (c = 0; c < tile_cols; ++c) {
1948 const int is_last = c == tile_cols - 1;
1949 if (!is_last) {
1950 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
1951 data += tile_col_size_bytes;
1952 tile_col_data_end[c] = data + tile_col_size;
1953 } else {
1954 tile_col_size = data_end - data;
1955 tile_col_data_end[c] = data_end;
1956 }
1957 data += tile_col_size;
1958 }
1959
1960 data = data_start;
1961
1962 // Read the required tile sizes.
1963 for (c = tile_cols_start; c < tile_cols_end; ++c) {
1964 const int is_last = c == tile_cols - 1;
1965
1966 if (c > 0) data = tile_col_data_end[c - 1];
1967
1968 if (!is_last) data += tile_col_size_bytes;
1969
1970 // Get the whole of the last column, otherwise stop at the required tile.
1971 for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
1972 tile_buffers[r][c].col = c;
1973
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001974 get_ls_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
1975 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
1976 tile_size_bytes, c, r, tile_copy_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001977 }
1978 }
1979
1980 // If we have not read the last column, then read it to get the last tile.
1981 if (tile_cols_end != tile_cols) {
1982 c = tile_cols - 1;
1983
1984 data = tile_col_data_end[c - 1];
1985
1986 for (r = 0; r < tile_rows; ++r) {
1987 tile_buffers[r][c].col = c;
1988
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001989 get_ls_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
1990 pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
1991 tile_size_bytes, c, r, tile_copy_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001992 }
1993 }
1994 }
1995}
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001996#endif // CONFIG_EXT_TILE
1997
Yaowu Xuc27fc142016-08-22 16:08:15 -07001998// Reads the next tile returning its size and adjusting '*data' accordingly
1999// based on 'is_last'.
2000static void get_tile_buffer(const uint8_t *const data_end,
2001 const int tile_size_bytes, int is_last,
Yaowu Xuf883b422016-08-30 14:01:10 -07002002 struct aom_internal_error_info *error_info,
2003 const uint8_t **data, aom_decrypt_cb decrypt_cb,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002004 void *decrypt_state, TileBufferDec *const buf) {
2005 size_t size;
2006
2007 if (!is_last) {
Yaowu Xu0a79a1b2017-02-17 13:04:54 -08002008 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002009 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002010 "Truncated packet or corrupt tile length");
2011
2012 if (decrypt_cb) {
2013 uint8_t be_data[4];
2014 decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
2015 size = mem_get_varsize(be_data, tile_size_bytes);
2016 } else {
2017 size = mem_get_varsize(*data, tile_size_bytes);
2018 }
2019 *data += tile_size_bytes;
2020
2021 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07002022 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002023 "Truncated packet or corrupt tile size");
2024 } else {
Soo-Chul Han38427e82017-09-27 15:06:13 -04002025#if !CONFIG_OBU || CONFIG_ADD_4BYTES_OBUSIZE
Yaowu Xuc27fc142016-08-22 16:08:15 -07002026 size = data_end - *data;
Soo-Chul Han38427e82017-09-27 15:06:13 -04002027#else
2028 size = mem_get_varsize(*data, tile_size_bytes);
2029 *data += tile_size_bytes;
2030#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002031 }
2032
2033 buf->data = *data;
2034 buf->size = size;
2035
2036 *data += size;
2037}
2038
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002039static void get_tile_buffers(AV1Decoder *pbi, const uint8_t *data,
2040 const uint8_t *data_end,
2041 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
2042 int startTile, int endTile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002043 AV1_COMMON *const cm = &pbi->common;
Thomas Davies80188d12016-10-26 16:08:35 -07002044 int r, c;
2045 const int tile_cols = cm->tile_cols;
2046 const int tile_rows = cm->tile_rows;
2047 int tc = 0;
2048 int first_tile_in_tg = 0;
David Barker01563082017-10-09 13:59:16 +01002049#if !CONFIG_OBU
Thomas Davies80188d12016-10-26 16:08:35 -07002050 struct aom_read_bit_buffer rb_tg_hdr;
2051 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
James Zern6efba482017-04-20 20:53:49 -07002052 const size_t hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
Thomas Davies80188d12016-10-26 16:08:35 -07002053 const int tg_size_bit_offset = pbi->tg_size_bit_offset;
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002054#endif
2055
Fangwen Fu73126c02017-02-08 22:37:47 -08002056#if CONFIG_DEPENDENT_HORZTILES
2057 int tile_group_start_col = 0;
2058 int tile_group_start_row = 0;
2059#endif
Thomas Davies80188d12016-10-26 16:08:35 -07002060
Thomas Davies4822e142017-10-10 11:30:36 +01002061#if CONFIG_SIMPLE_BWD_ADAPT
2062 size_t max_tile_size = 0;
2063 cm->largest_tile_id = 0;
2064#endif
Thomas Davies80188d12016-10-26 16:08:35 -07002065 for (r = 0; r < tile_rows; ++r) {
2066 for (c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07002067 TileBufferDec *const buf = &tile_buffers[r][c];
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002068#if CONFIG_OBU
2069 const int is_last = (tc == endTile);
2070 const size_t hdr_offset = 0;
2071#else
Thomas Daviesa0de6d52017-01-20 14:45:25 +00002072 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
James Zern6efba482017-04-20 20:53:49 -07002073 const size_t hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002074#endif
2075
2076 if (tc < startTile || tc > endTile) continue;
Thomas Davies80188d12016-10-26 16:08:35 -07002077
Rupert Swarbrickcd757392017-09-01 13:57:53 +01002078 if (data + hdr_offset >= data_end)
2079 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
2080 "Data ended before all tiles were read.");
Thomas Davies80188d12016-10-26 16:08:35 -07002081 buf->col = c;
David Barker01563082017-10-09 13:59:16 +01002082#if CONFIG_OBU
2083#if CONFIG_DEPENDENT_HORZTILES
2084 if (tc == startTile) {
2085 tile_group_start_row = r;
2086 tile_group_start_col = c;
2087 }
2088#endif // CONFIG_DEPENDENT_HORZTILES
2089#else // CONFIG_OBU
Thomas Davies80188d12016-10-26 16:08:35 -07002090 if (hdr_offset) {
2091 init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
2092 rb_tg_hdr.bit_offset = tg_size_bit_offset;
David Barker1a191122017-09-06 15:24:16 +01002093 read_tile_group_range(pbi, &rb_tg_hdr);
Fangwen Fu73126c02017-02-08 22:37:47 -08002094#if CONFIG_DEPENDENT_HORZTILES
David Barker1a191122017-09-06 15:24:16 +01002095 tile_group_start_row = r;
2096 tile_group_start_col = c;
Fangwen Fu73126c02017-02-08 22:37:47 -08002097#endif
Thomas Davies80188d12016-10-26 16:08:35 -07002098 }
David Barker01563082017-10-09 13:59:16 +01002099#endif // CONFIG_OBU
Thomas Davies80188d12016-10-26 16:08:35 -07002100 first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
2101 data += hdr_offset;
Thomas Daviesa0de6d52017-01-20 14:45:25 +00002102 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last,
2103 &pbi->common.error, &data, pbi->decrypt_cb,
2104 pbi->decrypt_state, buf);
Fangwen Fu73126c02017-02-08 22:37:47 -08002105#if CONFIG_DEPENDENT_HORZTILES
2106 cm->tile_group_start_row[r][c] = tile_group_start_row;
2107 cm->tile_group_start_col[r][c] = tile_group_start_col;
2108#endif
Thomas Davies4822e142017-10-10 11:30:36 +01002109#if CONFIG_SIMPLE_BWD_ADAPT
2110 if (buf->size > max_tile_size) {
2111 max_tile_size = buf->size;
2112 cm->largest_tile_id = r * tile_cols + c;
2113 }
2114#endif
Thomas Davies80188d12016-10-26 16:08:35 -07002115 }
2116 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002117}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002118
David Barker5c06a642017-08-18 13:18:16 +01002119#if CONFIG_LOOPFILTERING_ACROSS_TILES
Yi Luo10e23002017-07-31 11:54:43 -07002120static void dec_setup_across_tile_boundary_info(
2121 const AV1_COMMON *const cm, const TileInfo *const tile_info) {
Frederic Barbier94e38562017-08-16 14:38:48 +02002122 if (tile_info->mi_row_start >= tile_info->mi_row_end ||
2123 tile_info->mi_col_start >= tile_info->mi_col_end)
2124 return;
2125
David Barker5c06a642017-08-18 13:18:16 +01002126 if (!cm->loop_filter_across_tiles_enabled) {
Yi Luo10e23002017-07-31 11:54:43 -07002127 av1_setup_across_tile_boundary_info(cm, tile_info);
2128 }
2129}
David Barker5c06a642017-08-18 13:18:16 +01002130#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yi Luo10e23002017-07-31 11:54:43 -07002131
Yaowu Xuf883b422016-08-30 14:01:10 -07002132static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002133 const uint8_t *data_end, int startTile,
2134 int endTile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002135 AV1_COMMON *const cm = &pbi->common;
Cheng Chen9ac7a0f2017-10-17 20:36:46 -07002136#if !CONFIG_LOOPFILTER_LEVEL
Yaowu Xuf883b422016-08-30 14:01:10 -07002137 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Cheng Chen9ac7a0f2017-10-17 20:36:46 -07002138#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002139 const int tile_cols = cm->tile_cols;
2140 const int tile_rows = cm->tile_rows;
2141 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07002142 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002143#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07002144 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002145 const int single_row = pbi->dec_tile_row >= 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002146 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002147 const int single_col = pbi->dec_tile_col >= 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002148#endif // CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002149 int tile_rows_start;
2150 int tile_rows_end;
2151 int tile_cols_start;
2152 int tile_cols_end;
2153 int inv_col_order;
2154 int inv_row_order;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002155 int tile_row, tile_col;
Yunqing Wang0e141b52017-11-02 15:08:58 -07002156 uint8_t allow_update_cdf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002157
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002158#if CONFIG_EXT_TILE
2159 if (cm->large_scale_tile) {
2160 tile_rows_start = single_row ? dec_tile_row : 0;
2161 tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
2162 tile_cols_start = single_col ? dec_tile_col : 0;
2163 tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
2164 inv_col_order = pbi->inv_tile_order && !single_col;
2165 inv_row_order = pbi->inv_tile_order && !single_row;
Yunqing Wang0e141b52017-11-02 15:08:58 -07002166 allow_update_cdf = 0;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002167 } else {
2168#endif // CONFIG_EXT_TILE
2169 tile_rows_start = 0;
2170 tile_rows_end = tile_rows;
2171 tile_cols_start = 0;
2172 tile_cols_end = tile_cols;
2173 inv_col_order = pbi->inv_tile_order;
2174 inv_row_order = pbi->inv_tile_order;
Yunqing Wang0e141b52017-11-02 15:08:58 -07002175 allow_update_cdf = 1;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002176#if CONFIG_EXT_TILE
2177 }
2178#endif // CONFIG_EXT_TILE
2179
Cheng Chen9ac7a0f2017-10-17 20:36:46 -07002180#if !CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07002181 if (cm->lf.filter_level && !cm->skip_loop_filter &&
2182 pbi->lf_worker.data1 == NULL) {
2183 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
Yaowu Xuf883b422016-08-30 14:01:10 -07002184 aom_memalign(32, sizeof(LFWorkerData)));
2185 pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002186 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002187 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002188 "Loop filter thread creation failed");
2189 }
2190 }
2191
2192 if (cm->lf.filter_level && !cm->skip_loop_filter) {
2193 LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
2194 // Be sure to sync as we might be resuming after a failed frame decode.
2195 winterface->sync(&pbi->lf_worker);
Yaowu Xuf883b422016-08-30 14:01:10 -07002196 av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
2197 pbi->mb.plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002198 }
Cheng Chen9ac7a0f2017-10-17 20:36:46 -07002199#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07002200
2201 assert(tile_rows <= MAX_TILE_ROWS);
2202 assert(tile_cols <= MAX_TILE_COLS);
2203
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002204#if CONFIG_EXT_TILE
2205 if (cm->large_scale_tile)
2206 get_ls_tile_buffers(pbi, data, data_end, tile_buffers);
2207 else
2208#endif // CONFIG_EXT_TILE
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002209 get_tile_buffers(pbi, data, data_end, tile_buffers, startTile, endTile);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002210
2211 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002212 aom_free(pbi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002213 CHECK_MEM_ERROR(cm, pbi->tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -07002214 aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002215 pbi->allocated_tiles = n_tiles;
2216 }
Michael Bebenita6048d052016-08-25 14:40:54 -07002217#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04002218 if (pbi->acct_enabled) {
2219 aom_accounting_reset(&pbi->accounting);
2220 }
Michael Bebenita6048d052016-08-25 14:40:54 -07002221#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002222 // Load all tile information into tile_data.
2223 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
2224 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
2225 const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
2226 TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
2227
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002228 if (tile_row * cm->tile_cols + tile_col < startTile ||
2229 tile_row * cm->tile_cols + tile_col > endTile)
2230 continue;
2231
Yaowu Xuc27fc142016-08-22 16:08:15 -07002232 td->cm = cm;
2233 td->xd = pbi->mb;
2234 td->xd.corrupted = 0;
2235 td->xd.counts =
2236 cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
2237 ? &cm->counts
2238 : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07002239 av1_zero(td->dqcoeff);
2240 av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002241 setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
Yunqing Wang0e141b52017-11-02 15:08:58 -07002242 &td->bit_reader, allow_update_cdf,
Alex Converseeb780e72016-12-13 12:46:41 -08002243#if CONFIG_ANS && ANS_MAX_SYMBOLS
2244 1 << cm->ans_window_size_log2,
2245#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
2246 pbi->decrypt_cb, pbi->decrypt_state);
Michael Bebenita6048d052016-08-25 14:40:54 -07002247#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04002248 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01002249 td->bit_reader.accounting = &pbi->accounting;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04002250 } else {
David Barkerd971f402016-10-25 13:52:07 +01002251 td->bit_reader.accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04002252 }
Michael Bebenita6048d052016-08-25 14:40:54 -07002253#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07002254 av1_init_macroblockd(cm, &td->xd,
Luc Trudeauf8164152017-04-11 16:20:51 -04002255#if CONFIG_CFL
2256 &td->cfl,
2257#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07002258 td->dqcoeff);
Yushin Choc49ef3a2017-03-13 17:27:25 -07002259
Thomas Daviesf77d4ad2017-01-10 18:55:42 +00002260 // Initialise the tile context from the frame context
2261 td->tctx = *cm->fc;
2262 td->xd.tile_ctx = &td->tctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002263 td->xd.plane[0].color_index_map = td->color_index_map[0];
2264 td->xd.plane[1].color_index_map = td->color_index_map[1];
Sarah Parker5c6744b2017-08-25 17:27:45 -07002265#if CONFIG_MRC_TX
2266 td->xd.mrc_mask = td->mrc_mask;
2267#endif // CONFIG_MRC_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002268 }
2269 }
2270
2271 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
2272 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
2273 int mi_row = 0;
2274 TileInfo tile_info;
2275
Yaowu Xuf883b422016-08-30 14:01:10 -07002276 av1_tile_set_row(&tile_info, cm, row);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002277
2278 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
2279 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
2280 TileData *const td = pbi->tile_data + tile_cols * row + col;
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002281
2282 if (tile_row * cm->tile_cols + tile_col < startTile ||
2283 tile_row * cm->tile_cols + tile_col > endTile)
2284 continue;
2285
Michael Bebenita6048d052016-08-25 14:40:54 -07002286#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04002287 if (pbi->acct_enabled) {
David Barkerd971f402016-10-25 13:52:07 +01002288 td->bit_reader.accounting->last_tell_frac =
2289 aom_reader_tell_frac(&td->bit_reader);
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04002290 }
Michael Bebenita6048d052016-08-25 14:40:54 -07002291#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002292
Yaowu Xuf883b422016-08-30 14:01:10 -07002293 av1_tile_set_col(&tile_info, cm, col);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002294
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002295#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -08002296 av1_tile_set_tg_boundary(&tile_info, cm, tile_row, tile_col);
2297 if (!cm->dependent_horz_tiles || tile_row == 0 ||
2298 tile_info.tg_horz_boundary) {
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002299 av1_zero_above_context(cm, tile_info.mi_col_start,
2300 tile_info.mi_col_end);
2301 }
2302#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002303 av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08002304#endif
Rupert Swarbrick6c545212017-09-01 17:17:25 +01002305#if CONFIG_LOOP_RESTORATION
2306 for (int p = 0; p < MAX_MB_PLANE; ++p) {
2307 set_default_wiener(td->xd.wiener_info + p);
2308 set_default_sgrproj(td->xd.sgrproj_info + p);
2309 }
2310#endif // CONFIG_LOOP_RESTORATION
Yaowu Xuc27fc142016-08-22 16:08:15 -07002311
David Barker5c06a642017-08-18 13:18:16 +01002312#if CONFIG_LOOPFILTERING_ACROSS_TILES
Yi Luo10e23002017-07-31 11:54:43 -07002313 dec_setup_across_tile_boundary_info(cm, &tile_info);
David Barker5c06a642017-08-18 13:18:16 +01002314#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yi Luof190a162017-07-13 16:16:56 -07002315
Yaowu Xuc27fc142016-08-22 16:08:15 -07002316 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
2317 mi_row += cm->mib_size) {
2318 int mi_col;
2319
Yaowu Xuf883b422016-08-30 14:01:10 -07002320 av1_zero_left_context(&td->xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002321
2322 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
2323 mi_col += cm->mib_size) {
Angie Chiangd9af8ac2017-10-25 10:48:53 -07002324#if CONFIG_SYMBOLRATE
2325 av1_record_superblock(td->xd.counts);
2326#endif
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002327 decode_partition(pbi, &td->xd, mi_row, mi_col, &td->bit_reader,
2328 cm->sb_size);
Sebastien Alaiwan1bc94fc2017-10-31 10:25:17 +01002329#if NC_MODE_INFO
Yue Chen9ab6d712017-01-12 15:50:46 -08002330 detoken_and_recon_sb(pbi, &td->xd, mi_row, mi_col, &td->bit_reader,
2331 cm->sb_size);
2332#endif
Cheng Chen5ad5b282017-10-05 16:36:06 -07002333#if CONFIG_LPF_SB
2334 if (USE_LOOP_FILTER_SUPERBLOCK) {
2335 // apply deblocking filtering right after each superblock is decoded
2336 const int guess_filter_lvl = FAKE_FILTER_LEVEL;
2337 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
2338 guess_filter_lvl, 0, 1, mi_row, mi_col);
2339 }
2340#endif // CONFIG_LPF_SB
Yaowu Xuc27fc142016-08-22 16:08:15 -07002341 }
Angie Chiangd0916d92017-03-10 17:54:18 -08002342 aom_merge_corrupted_flag(&pbi->mb.corrupted, td->xd.corrupted);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002343 if (pbi->mb.corrupted)
Yaowu Xuf883b422016-08-30 14:01:10 -07002344 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002345 "Failed to decode tile data");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002346 }
2347 }
2348
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002349#if !CONFIG_OBU
Yaowu Xuc27fc142016-08-22 16:08:15 -07002350 assert(mi_row > 0);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002351#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002352
Yaowu Xuc27fc142016-08-22 16:08:15 -07002353 // After loopfiltering, the last 7 row pixels in each superblock row may
2354 // still be changed by the longest loopfilter of the next superblock row.
2355 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07002356 av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002357 }
2358
Cheng Chen5ad5b282017-10-05 16:36:06 -07002359#if CONFIG_INTRABC
2360// When intraBC is on, do loop filtering per superblock,
2361// instead of do it after the whole frame has been encoded,
2362// as is in the else branch
2363#else
Cheng Chene94df5c2017-07-19 17:25:33 -07002364// Loopfilter the whole frame.
Cheng Chenf572cd32017-08-25 18:34:51 -07002365#if CONFIG_LPF_SB
2366 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
2367 cm->lf.filter_level, 0, 0, 0, 0);
2368#else
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002369#if CONFIG_OBU
2370 if (endTile == cm->tile_rows * cm->tile_cols - 1)
2371#endif
David Barker3dffa272017-10-18 17:07:26 +01002372#if CONFIG_LOOPFILTER_LEVEL
2373 if (cm->lf.filter_level[0] || cm->lf.filter_level[1]) {
2374 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
2375 cm->lf.filter_level[0], cm->lf.filter_level[1], 0,
2376 0);
2377 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
2378 cm->lf.filter_level_u, cm->lf.filter_level_u, 1, 0);
2379 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
2380 cm->lf.filter_level_v, cm->lf.filter_level_v, 2, 0);
2381 }
2382#else
2383 av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
2384 cm->lf.filter_level, 0, 0);
Cheng Chen13fc8192017-08-19 11:49:28 -07002385#endif // CONFIG_LOOPFILTER_LEVEL
Cheng Chenf572cd32017-08-25 18:34:51 -07002386#endif // CONFIG_LPF_SB
Cheng Chen5ad5b282017-10-05 16:36:06 -07002387#endif // CONFIG_INTRABC
Yaowu Xuc27fc142016-08-22 16:08:15 -07002388 if (cm->frame_parallel_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -07002389 av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002390
2391#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002392 if (cm->large_scale_tile) {
2393 if (n_tiles == 1) {
2394#if CONFIG_ANS
2395 return data_end;
2396#else
2397 // Find the end of the single tile buffer
2398 return aom_reader_find_end(&pbi->tile_data->bit_reader);
2399#endif // CONFIG_ANS
2400 } else {
2401 // Return the end of the last tile buffer
2402 return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
2403 }
2404 } else {
2405#endif // CONFIG_EXT_TILE
Yaowu Xuc27fc142016-08-22 16:08:15 -07002406#if CONFIG_ANS
2407 return data_end;
2408#else
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002409#if !CONFIG_OBU
Yaowu Xuc27fc142016-08-22 16:08:15 -07002410 {
2411 // Get last tile data.
2412 TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07002413 return aom_reader_find_end(&td->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002414 }
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002415#else
2416 TileData *const td = pbi->tile_data + endTile;
2417 return aom_reader_find_end(&td->bit_reader);
2418#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002419#endif // CONFIG_ANS
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002420#if CONFIG_EXT_TILE
2421 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002422#endif // CONFIG_EXT_TILE
2423}
2424
Yaowu Xuc27fc142016-08-22 16:08:15 -07002425static void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002426 AV1_COMMON *const cm = (AV1_COMMON *)data;
2427 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002428}
2429
Yaowu Xuf883b422016-08-30 14:01:10 -07002430static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
Sebastien Alaiwan8b7a4e12017-06-13 11:25:57 +02002431 struct aom_read_bit_buffer *rb,
2432 int allow_lowbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002433 if (cm->profile >= PROFILE_2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002434 cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002435 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002436 cm->bit_depth = AOM_BITS_8;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01002437 }
2438
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002439#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan8b7a4e12017-06-13 11:25:57 +02002440 cm->use_highbitdepth = cm->bit_depth > AOM_BITS_8 || !allow_lowbitdepth;
James Zern91adea52017-06-15 23:27:26 -07002441#else
2442 (void)allow_lowbitdepth;
Sebastien Alaiwan98378132017-01-04 11:23:09 +01002443#endif
anorkin76fb1262017-03-22 15:12:12 -07002444#if CONFIG_COLORSPACE_HEADERS
2445 cm->color_space = aom_rb_read_literal(rb, 5);
2446 cm->transfer_function = aom_rb_read_literal(rb, 5);
2447#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002448 cm->color_space = aom_rb_read_literal(rb, 3);
anorkin76fb1262017-03-22 15:12:12 -07002449#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002450 if (cm->color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002451 // [16,235] (including xvycc) vs [0,255] range
Yaowu Xuf883b422016-08-30 14:01:10 -07002452 cm->color_range = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002453 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002454 cm->subsampling_x = aom_rb_read_bit(rb);
2455 cm->subsampling_y = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002456 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07002457 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002458 "4:2:0 color not supported in profile 1 or 3");
Yaowu Xuf883b422016-08-30 14:01:10 -07002459 if (aom_rb_read_bit(rb))
2460 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002461 "Reserved bit set");
2462 } else {
2463 cm->subsampling_y = cm->subsampling_x = 1;
2464 }
anorkin76fb1262017-03-22 15:12:12 -07002465#if CONFIG_COLORSPACE_HEADERS
2466 if (cm->subsampling_x == 1 && cm->subsampling_y == 1) {
2467 cm->chroma_sample_position = aom_rb_read_literal(rb, 2);
2468 }
2469#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002470 } else {
2471 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
2472 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
2473 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
2474 cm->subsampling_y = cm->subsampling_x = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002475 if (aom_rb_read_bit(rb))
2476 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002477 "Reserved bit set");
2478 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002479 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002480 "4:4:4 color not supported in profile 0 or 2");
2481 }
2482 }
2483}
2484
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01002485#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +01002486void read_sequence_header(SequenceHeader *seq_params,
2487 struct aom_read_bit_buffer *rb) {
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01002488#if CONFIG_FRAME_SIZE
2489 int num_bits_width = aom_rb_read_literal(rb, 4) + 1;
2490 int num_bits_height = aom_rb_read_literal(rb, 4) + 1;
2491 int max_frame_width = aom_rb_read_literal(rb, num_bits_width) + 1;
2492 int max_frame_height = aom_rb_read_literal(rb, num_bits_height) + 1;
2493
2494 seq_params->num_bits_width = num_bits_width;
2495 seq_params->num_bits_height = num_bits_height;
2496 seq_params->max_frame_width = max_frame_width;
2497 seq_params->max_frame_height = max_frame_height;
2498#endif
2499
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01002500 /* Placeholder for actually reading from the bitstream */
David Barker5e70a112017-10-03 14:28:17 +01002501 seq_params->frame_id_numbers_present_flag = aom_rb_read_bit(rb);
2502 if (seq_params->frame_id_numbers_present_flag) {
Frederic Barbier4d5d90e2017-10-13 09:22:33 +02002503 // We must always have delta_frame_id_length < frame_id_length,
2504 // in order for a frame to be referenced with a unique delta.
2505 // Avoid wasting bits by using a coding that enforces this restriction.
Frederic Barbiere83fcfe2017-10-13 10:37:50 +02002506 seq_params->delta_frame_id_length = aom_rb_read_literal(rb, 4) + 2;
Frederic Barbier4d5d90e2017-10-13 09:22:33 +02002507 seq_params->frame_id_length =
2508 aom_rb_read_literal(rb, 3) + seq_params->delta_frame_id_length + 1;
David Barker5e70a112017-10-03 14:28:17 +01002509 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01002510}
Debargha Mukherjee778023d2017-09-26 17:50:27 -07002511#endif // CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01002512
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002513static void read_compound_tools(AV1_COMMON *cm,
2514 struct aom_read_bit_buffer *rb) {
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002515 if (!frame_is_intra_only(cm) && cm->reference_mode != COMPOUND_REFERENCE) {
2516 cm->allow_interintra_compound = aom_rb_read_bit(rb);
2517 } else {
2518 cm->allow_interintra_compound = 0;
2519 }
Zoe Liu85b66462017-04-20 14:28:19 -07002520#if CONFIG_COMPOUND_SINGLEREF
2521 if (!frame_is_intra_only(cm)) {
2522#else // !CONFIG_COMPOUND_SINGLEREF
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002523 if (!frame_is_intra_only(cm) && cm->reference_mode != SINGLE_REFERENCE) {
Zoe Liu85b66462017-04-20 14:28:19 -07002524#endif // CONFIG_COMPOUND_SINGLEREF
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002525 cm->allow_masked_compound = aom_rb_read_bit(rb);
2526 } else {
2527 cm->allow_masked_compound = 0;
2528 }
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002529}
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002530
Sarah Parker3e579a62017-08-23 16:53:20 -07002531static int read_global_motion_params(WarpedMotionParams *params,
David Barkerd7c8bd52017-09-25 14:47:29 +01002532 const WarpedMotionParams *ref_params,
Sarah Parker3e579a62017-08-23 16:53:20 -07002533 struct aom_read_bit_buffer *rb,
2534 int allow_hp) {
2535 TransformationType type = aom_rb_read_bit(rb);
2536 if (type != IDENTITY) {
2537#if GLOBAL_TRANS_TYPES > 4
2538 type += aom_rb_read_literal(rb, GLOBAL_TYPE_BITS);
2539#else
2540 if (aom_rb_read_bit(rb))
2541 type = ROTZOOM;
2542 else
2543 type = aom_rb_read_bit(rb) ? TRANSLATION : AFFINE;
2544#endif // GLOBAL_TRANS_TYPES > 4
2545 }
2546
David Barkerd7c8bd52017-09-25 14:47:29 +01002547 *params = default_warp_params;
Sarah Parker3e579a62017-08-23 16:53:20 -07002548 params->wmtype = type;
Sebastien Alaiwan238a6d62017-11-01 11:29:46 +01002549
2550 if (type >= ROTZOOM) {
2551 params->wmmat[2] = aom_rb_read_signed_primitive_refsubexpfin(
2552 rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
2553 (ref_params->wmmat[2] >> GM_ALPHA_PREC_DIFF) -
2554 (1 << GM_ALPHA_PREC_BITS)) *
2555 GM_ALPHA_DECODE_FACTOR +
2556 (1 << WARPEDMODEL_PREC_BITS);
2557 params->wmmat[3] = aom_rb_read_signed_primitive_refsubexpfin(
2558 rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
2559 (ref_params->wmmat[3] >> GM_ALPHA_PREC_DIFF)) *
2560 GM_ALPHA_DECODE_FACTOR;
Sarah Parker3e579a62017-08-23 16:53:20 -07002561 }
Sebastien Alaiwan238a6d62017-11-01 11:29:46 +01002562
2563 if (type >= AFFINE) {
2564 params->wmmat[4] = aom_rb_read_signed_primitive_refsubexpfin(
2565 rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
2566 (ref_params->wmmat[4] >> GM_ALPHA_PREC_DIFF)) *
2567 GM_ALPHA_DECODE_FACTOR;
2568 params->wmmat[5] = aom_rb_read_signed_primitive_refsubexpfin(
2569 rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
2570 (ref_params->wmmat[5] >> GM_ALPHA_PREC_DIFF) -
2571 (1 << GM_ALPHA_PREC_BITS)) *
2572 GM_ALPHA_DECODE_FACTOR +
2573 (1 << WARPEDMODEL_PREC_BITS);
2574 } else {
2575 params->wmmat[4] = -params->wmmat[3];
2576 params->wmmat[5] = params->wmmat[2];
2577 }
2578
2579 if (type >= TRANSLATION) {
2580 const int trans_bits = (type == TRANSLATION)
2581 ? GM_ABS_TRANS_ONLY_BITS - !allow_hp
2582 : GM_ABS_TRANS_BITS;
2583 const int trans_dec_factor =
2584 (type == TRANSLATION) ? GM_TRANS_ONLY_DECODE_FACTOR * (1 << !allow_hp)
2585 : GM_TRANS_DECODE_FACTOR;
2586 const int trans_prec_diff = (type == TRANSLATION)
2587 ? GM_TRANS_ONLY_PREC_DIFF + !allow_hp
2588 : GM_TRANS_PREC_DIFF;
2589 params->wmmat[0] = aom_rb_read_signed_primitive_refsubexpfin(
2590 rb, (1 << trans_bits) + 1, SUBEXPFIN_K,
2591 (ref_params->wmmat[0] >> trans_prec_diff)) *
2592 trans_dec_factor;
2593 params->wmmat[1] = aom_rb_read_signed_primitive_refsubexpfin(
2594 rb, (1 << trans_bits) + 1, SUBEXPFIN_K,
2595 (ref_params->wmmat[1] >> trans_prec_diff)) *
2596 trans_dec_factor;
2597 }
2598
Sarah Parker3e579a62017-08-23 16:53:20 -07002599 if (params->wmtype <= AFFINE) {
2600 int good_shear_params = get_shear_params(params);
2601 if (!good_shear_params) return 0;
2602 }
2603
2604 return 1;
2605}
2606
2607static void read_global_motion(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
2608 int frame;
2609 for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
David Barkerd7c8bd52017-09-25 14:47:29 +01002610 const WarpedMotionParams *ref_params =
2611 cm->error_resilient_mode ? &default_warp_params
2612 : &cm->prev_frame->global_motion[frame];
Sarah Parker3e579a62017-08-23 16:53:20 -07002613 int good_params = read_global_motion_params(
David Barkerd7c8bd52017-09-25 14:47:29 +01002614 &cm->global_motion[frame], ref_params, rb, cm->allow_high_precision_mv);
Sebastien Alaiwan8d88b292017-11-03 17:38:30 +01002615 if (!good_params) {
2616#if WARPED_MOTION_DEBUG
2617 printf("Warning: unexpected global motion shear params from aomenc\n");
2618#endif
2619 cm->global_motion[frame].invalid = 1;
2620 }
Sarah Parker3e579a62017-08-23 16:53:20 -07002621
2622 // TODO(sarahparker, debargha): The logic in the commented out code below
2623 // does not work currently and causes mismatches when resize is on. Fix it
2624 // before turning the optimization back on.
2625 /*
2626 YV12_BUFFER_CONFIG *ref_buf = get_ref_frame(cm, frame);
2627 if (cm->width == ref_buf->y_crop_width &&
2628 cm->height == ref_buf->y_crop_height) {
2629 read_global_motion_params(&cm->global_motion[frame],
2630 &cm->prev_frame->global_motion[frame], rb,
2631 cm->allow_high_precision_mv);
2632 } else {
David Barkerd7c8bd52017-09-25 14:47:29 +01002633 cm->global_motion[frame] = default_warp_params;
Sarah Parker3e579a62017-08-23 16:53:20 -07002634 }
2635 */
2636 /*
2637 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
2638 frame, cm->current_video_frame, cm->show_frame,
2639 cm->global_motion[frame].wmmat[0],
2640 cm->global_motion[frame].wmmat[1],
2641 cm->global_motion[frame].wmmat[2],
2642 cm->global_motion[frame].wmmat[3]);
2643 */
2644 }
David Barkercba7da72017-09-14 11:24:27 +01002645 memcpy(cm->cur_frame->global_motion, cm->global_motion,
2646 TOTAL_REFS_PER_FRAME * sizeof(WarpedMotionParams));
Sarah Parker3e579a62017-08-23 16:53:20 -07002647}
Sarah Parker3e579a62017-08-23 16:53:20 -07002648
Yaowu Xuf883b422016-08-30 14:01:10 -07002649static size_t read_uncompressed_header(AV1Decoder *pbi,
2650 struct aom_read_bit_buffer *rb) {
2651 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002652 MACROBLOCKD *const xd = &pbi->mb;
2653 BufferPool *const pool = cm->buffer_pool;
2654 RefCntBuffer *const frame_bufs = pool->frame_bufs;
2655 int i, mask, ref_index = 0;
2656 size_t sz;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01002657
Yaowu Xuc27fc142016-08-22 16:08:15 -07002658 cm->last_frame_type = cm->frame_type;
2659 cm->last_intra_only = cm->intra_only;
2660
Yaowu Xuc27fc142016-08-22 16:08:15 -07002661 // NOTE: By default all coded frames to be used as a reference
2662 cm->is_reference_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002663
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002664#if !CONFIG_OBU
Yaowu Xuf883b422016-08-30 14:01:10 -07002665 if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
2666 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002667 "Invalid frame marker");
2668
Yaowu Xuf883b422016-08-30 14:01:10 -07002669 cm->profile = av1_read_profile(rb);
Sebastien Alaiwanb9c652a2017-05-03 15:44:28 +02002670
2671 const BITSTREAM_PROFILE MAX_SUPPORTED_PROFILE =
2672 CONFIG_HIGHBITDEPTH ? MAX_PROFILES : PROFILE_2;
2673
2674 if (cm->profile >= MAX_SUPPORTED_PROFILE)
Yaowu Xuf883b422016-08-30 14:01:10 -07002675 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002676 "Unsupported bitstream profile");
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002677#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002678
Yunqing Wangc2502b52017-07-19 17:44:18 -07002679#if CONFIG_EXT_TILE
2680 cm->large_scale_tile = aom_rb_read_literal(rb, 1);
2681#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +01002682 if (cm->large_scale_tile) cm->seq_params.frame_id_numbers_present_flag = 0;
Yunqing Wangc2502b52017-07-19 17:44:18 -07002683#endif // CONFIG_REFERENCE_BUFFER
2684#endif // CONFIG_EXT_TILE
2685
Yaowu Xuf883b422016-08-30 14:01:10 -07002686 cm->show_existing_frame = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002687
2688 if (cm->show_existing_frame) {
Yaowu Xu415ba932016-12-27 11:17:32 -08002689 // Show an existing frame directly.
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01002690 const int existing_frame_idx = aom_rb_read_literal(rb, 3);
2691 const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
Yaowu Xu415ba932016-12-27 11:17:32 -08002692#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +01002693 if (cm->seq_params.frame_id_numbers_present_flag) {
Frederic Barbiere83fcfe2017-10-13 10:37:50 +02002694 int frame_id_length = cm->seq_params.frame_id_length;
David Barker5e70a112017-10-03 14:28:17 +01002695 int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
2696 /* Compare display_frame_id with ref_frame_id and check valid for
2697 * referencing */
2698 if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
2699 cm->valid_for_referencing[existing_frame_idx] == 0)
2700 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
2701 "Reference buffer frame ID mismatch");
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01002702 }
2703#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002704 lock_buffer_pool(pool);
2705 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
2706 unlock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07002707 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002708 "Buffer %d does not contain a decoded frame",
2709 frame_to_show);
2710 }
2711 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
2712 unlock_buffer_pool(pool);
2713
Cheng Chen13fc8192017-08-19 11:49:28 -07002714#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -07002715 cm->lf.filter_level[0] = 0;
2716 cm->lf.filter_level[1] = 0;
2717#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002718 cm->lf.filter_level = 0;
Cheng Chen179479f2017-08-04 10:56:39 -07002719#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002720 cm->show_frame = 1;
2721 pbi->refresh_frame_flags = 0;
2722
2723 if (cm->frame_parallel_decode) {
2724 for (i = 0; i < REF_FRAMES; ++i)
2725 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
2726 }
2727
2728 return 0;
2729 }
2730
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002731#if !CONFIG_OBU
Yaowu Xuf883b422016-08-30 14:01:10 -07002732 cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
Debargha Mukherjee778023d2017-09-26 17:50:27 -07002733 cm->show_frame = aom_rb_read_bit(rb);
2734 if (cm->frame_type != KEY_FRAME)
2735 cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002736#else
2737 cm->frame_type = (FRAME_TYPE)aom_rb_read_literal(rb, 2); // 2 bits
Debargha Mukherjee778023d2017-09-26 17:50:27 -07002738 cm->show_frame = aom_rb_read_bit(rb);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002739 cm->intra_only = cm->frame_type == INTRA_ONLY_FRAME;
2740#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002741 cm->error_resilient_mode = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01002742#if CONFIG_REFERENCE_BUFFER
Debargha Mukherjee778023d2017-09-26 17:50:27 -07002743#if !CONFIG_OBU
David Barker5e70a112017-10-03 14:28:17 +01002744 if (frame_is_intra_only(cm)) read_sequence_header(&cm->seq_params, rb);
Debargha Mukherjee778023d2017-09-26 17:50:27 -07002745#endif // !CONFIG_OBU
David Barker5e70a112017-10-03 14:28:17 +01002746 if (cm->seq_params.frame_id_numbers_present_flag) {
Frederic Barbiere83fcfe2017-10-13 10:37:50 +02002747 int frame_id_length = cm->seq_params.frame_id_length;
2748 int diff_len = cm->seq_params.delta_frame_id_length;
David Barker5e70a112017-10-03 14:28:17 +01002749 int prev_frame_id = 0;
2750 if (cm->frame_type != KEY_FRAME) {
2751 prev_frame_id = cm->current_frame_id;
2752 }
2753 cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002754
David Barker5e70a112017-10-03 14:28:17 +01002755 if (cm->frame_type != KEY_FRAME) {
2756 int diff_frame_id;
2757 if (cm->current_frame_id > prev_frame_id) {
2758 diff_frame_id = cm->current_frame_id - prev_frame_id;
2759 } else {
2760 diff_frame_id =
2761 (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01002762 }
David Barker5e70a112017-10-03 14:28:17 +01002763 /* Check current_frame_id for conformance */
2764 if (prev_frame_id == cm->current_frame_id ||
2765 diff_frame_id >= (1 << (frame_id_length - 1))) {
2766 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
2767 "Invalid value of current_frame_id");
2768 }
2769 }
2770 /* Check if some frames need to be marked as not valid for referencing */
2771 for (i = 0; i < REF_FRAMES; i++) {
2772 if (cm->frame_type == KEY_FRAME) {
2773 cm->valid_for_referencing[i] = 0;
2774 } else if (cm->current_frame_id - (1 << diff_len) > 0) {
2775 if (cm->ref_frame_id[i] > cm->current_frame_id ||
2776 cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01002777 cm->valid_for_referencing[i] = 0;
David Barker5e70a112017-10-03 14:28:17 +01002778 } else {
2779 if (cm->ref_frame_id[i] > cm->current_frame_id &&
2780 cm->ref_frame_id[i] <
2781 (1 << frame_id_length) + cm->current_frame_id - (1 << diff_len))
2782 cm->valid_for_referencing[i] = 0;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01002783 }
2784 }
2785 }
Debargha Mukherjee778023d2017-09-26 17:50:27 -07002786#endif // CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01002787
2788#if CONFIG_FRAME_SIZE
2789 int frame_size_override_flag = aom_rb_read_literal(rb, 1);
2790#endif
2791
Yaowu Xuc27fc142016-08-22 16:08:15 -07002792 if (cm->frame_type == KEY_FRAME) {
Jingning Hand8a15a62017-10-30 10:53:42 -07002793 cm->current_video_frame = 0;
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002794#if !CONFIG_OBU
Sebastien Alaiwan8b7a4e12017-06-13 11:25:57 +02002795 read_bitdepth_colorspace_sampling(cm, rb, pbi->allow_lowbitdepth);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002796#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002797 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
2798
2799 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2800 cm->frame_refs[i].idx = INVALID_IDX;
2801 cm->frame_refs[i].buf = NULL;
2802 }
2803
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01002804#if CONFIG_FRAME_SIZE
2805 setup_frame_size(cm, frame_size_override_flag, rb);
2806#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002807 setup_frame_size(cm, rb);
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01002808#endif
Debargha Mukherjeed2630fa2017-09-22 10:32:51 -07002809 setup_sb_size(cm, rb);
2810
Yaowu Xuc27fc142016-08-22 16:08:15 -07002811 if (pbi->need_resync) {
2812 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
2813 pbi->need_resync = 0;
2814 }
Alex Converseeb780e72016-12-13 12:46:41 -08002815#if CONFIG_ANS && ANS_MAX_SYMBOLS
2816 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
2817#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
hui su24f7b072016-10-12 11:36:24 -07002818 cm->allow_screen_content_tools = aom_rb_read_bit(rb);
RogerZhou3b635242017-09-19 10:06:46 -07002819#if CONFIG_AMVR
2820 if (cm->allow_screen_content_tools) {
2821 if (aom_rb_read_bit(rb)) {
RogerZhou10a03802017-10-26 11:49:48 -07002822 cm->seq_force_integer_mv = 2;
RogerZhou3b635242017-09-19 10:06:46 -07002823 } else {
RogerZhou10a03802017-10-26 11:49:48 -07002824 cm->seq_force_integer_mv = aom_rb_read_bit(rb);
RogerZhou3b635242017-09-19 10:06:46 -07002825 }
2826 } else {
RogerZhou10a03802017-10-26 11:49:48 -07002827 cm->seq_force_integer_mv = 0;
RogerZhou3b635242017-09-19 10:06:46 -07002828 }
2829#endif
Fangwen Fu930c51c2017-05-07 20:39:17 -07002830#if CONFIG_TEMPMV_SIGNALING
2831 cm->use_prev_frame_mvs = 0;
2832#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002833 } else {
hui su24f7b072016-10-12 11:36:24 -07002834 if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
Thomas Daedea6a854b2017-06-22 17:49:11 -07002835#if CONFIG_TEMPMV_SIGNALING
2836 if (cm->intra_only || cm->error_resilient_mode) cm->use_prev_frame_mvs = 0;
2837#endif
2838#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
2839// The only way to reset all frame contexts to their default values is with a
2840// keyframe.
2841#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002842 if (cm->error_resilient_mode) {
2843 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
2844 } else {
2845 if (cm->intra_only) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002846 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002847 ? RESET_FRAME_CONTEXT_ALL
2848 : RESET_FRAME_CONTEXT_CURRENT;
2849 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002850 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002851 ? RESET_FRAME_CONTEXT_CURRENT
2852 : RESET_FRAME_CONTEXT_NONE;
2853 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
Yaowu Xuf883b422016-08-30 14:01:10 -07002854 cm->reset_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002855 ? RESET_FRAME_CONTEXT_ALL
2856 : RESET_FRAME_CONTEXT_CURRENT;
2857 }
2858 }
Thomas Daedea6a854b2017-06-22 17:49:11 -07002859#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002860
2861 if (cm->intra_only) {
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002862#if !CONFIG_OBU
Sebastien Alaiwan8b7a4e12017-06-13 11:25:57 +02002863 read_bitdepth_colorspace_sampling(cm, rb, pbi->allow_lowbitdepth);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002864#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002865
Yaowu Xuf883b422016-08-30 14:01:10 -07002866 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01002867#if CONFIG_FRAME_SIZE
2868 setup_frame_size(cm, frame_size_override_flag, rb);
2869#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002870 setup_frame_size(cm, rb);
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01002871#endif
Pavel Frolovea3dd3a2017-09-25 16:06:19 +03002872 setup_sb_size(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002873 if (pbi->need_resync) {
2874 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
2875 pbi->need_resync = 0;
2876 }
Alex Converseeb780e72016-12-13 12:46:41 -08002877#if CONFIG_ANS && ANS_MAX_SYMBOLS
2878 cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
2879#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002880 } else if (pbi->need_resync != 1) { /* Skip if need resync */
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002881#if CONFIG_OBU
2882 pbi->refresh_frame_flags = (cm->frame_type == S_FRAME)
2883 ? ~(1 << REF_FRAMES)
2884 : aom_rb_read_literal(rb, REF_FRAMES);
2885#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002886 pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002887#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002888
Yaowu Xuc27fc142016-08-22 16:08:15 -07002889 if (!pbi->refresh_frame_flags) {
2890 // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
2891 // will not be used as a reference
2892 cm->is_reference_frame = 0;
2893 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002894
2895 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002896 const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002897 const int idx = cm->ref_frame_map[ref];
Rupert Swarbrick5eb471c2017-10-02 16:06:54 +01002898
2899 // Most of the time, streams start with a keyframe. In that case,
2900 // ref_frame_map will have been filled in at that point and will not
2901 // contain any -1's. However, streams are explicitly allowed to start
2902 // with an intra-only frame, so long as they don't then signal a
2903 // reference to a slot that hasn't been set yet. That's what we are
2904 // checking here.
2905 if (idx == -1)
2906 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
2907 "Inter frame requests nonexistent reference");
2908
Yaowu Xuc27fc142016-08-22 16:08:15 -07002909 RefBuffer *const ref_frame = &cm->frame_refs[i];
2910 ref_frame->idx = idx;
2911 ref_frame->buf = &frame_bufs[idx].buf;
Zoe Liu17af2742017-10-06 10:36:42 -07002912#if CONFIG_FRAME_SIGN_BIAS
2913#if CONFIG_OBU
2914 // NOTE: For the scenario of (cm->frame_type != S_FRAME),
2915 // ref_frame_sign_bias will be reset based on frame offsets.
2916 cm->ref_frame_sign_bias[LAST_FRAME + i] = 0;
2917#endif // CONFIG_OBU
2918#else // !CONFIG_FRAME_SIGN_BIAS
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04002919#if CONFIG_OBU
2920 cm->ref_frame_sign_bias[LAST_FRAME + i] =
2921 (cm->frame_type == S_FRAME) ? 0 : aom_rb_read_bit(rb);
Zoe Liu17af2742017-10-06 10:36:42 -07002922#else // !CONFIG_OBU
Yaowu Xuf883b422016-08-30 14:01:10 -07002923 cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
Zoe Liu17af2742017-10-06 10:36:42 -07002924#endif // CONFIG_OBU
2925#endif // CONFIG_FRAME_SIGN_BIAS
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01002926#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +01002927 if (cm->seq_params.frame_id_numbers_present_flag) {
Frederic Barbiere83fcfe2017-10-13 10:37:50 +02002928 int frame_id_length = cm->seq_params.frame_id_length;
2929 int diff_len = cm->seq_params.delta_frame_id_length;
David Barker5e70a112017-10-03 14:28:17 +01002930 int delta_frame_id_minus1 = aom_rb_read_literal(rb, diff_len);
2931 int ref_frame_id =
2932 ((cm->current_frame_id - (delta_frame_id_minus1 + 1) +
2933 (1 << frame_id_length)) %
2934 (1 << frame_id_length));
2935 /* Compare values derived from delta_frame_id_minus1 and
2936 * refresh_frame_flags. Also, check valid for referencing */
2937 if (ref_frame_id != cm->ref_frame_id[ref] ||
2938 cm->valid_for_referencing[ref] == 0)
2939 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
2940 "Reference buffer frame ID mismatch");
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01002941 }
Debargha Mukherjee778023d2017-09-26 17:50:27 -07002942#endif // CONFIG_REFERENCE_BUFFER
Yaowu Xuc27fc142016-08-22 16:08:15 -07002943 }
2944
Arild Fuldseth842e9b02016-09-02 13:00:05 +02002945#if CONFIG_FRAME_SIZE
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01002946 if (cm->error_resilient_mode == 0 && frame_size_override_flag) {
Arild Fuldseth842e9b02016-09-02 13:00:05 +02002947 setup_frame_size_with_refs(cm, rb);
2948 } else {
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01002949 setup_frame_size(cm, frame_size_override_flag, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02002950 }
2951#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002952 setup_frame_size_with_refs(cm, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02002953#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002954
RogerZhou3b635242017-09-19 10:06:46 -07002955#if CONFIG_AMVR
RogerZhou10a03802017-10-26 11:49:48 -07002956 if (cm->seq_force_integer_mv == 2) {
2957 cm->cur_frame_force_integer_mv = aom_rb_read_bit(rb);
RogerZhou3b635242017-09-19 10:06:46 -07002958 } else {
RogerZhou10a03802017-10-26 11:49:48 -07002959 cm->cur_frame_force_integer_mv = cm->seq_force_integer_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002960 }
RogerZhou10a03802017-10-26 11:49:48 -07002961
2962 if (cm->cur_frame_force_integer_mv) {
2963 cm->allow_high_precision_mv = 0;
2964 } else {
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002965#if CONFIG_EIGHTH_PEL_MV_ONLY
2966 cm->allow_high_precision_mv = 1;
2967#else
RogerZhou10a03802017-10-26 11:49:48 -07002968 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002969#endif // CONFIG_EIGHTH_PEL_MV_ONLY
RogerZhou10a03802017-10-26 11:49:48 -07002970 }
2971#else
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002972#if CONFIG_EIGHTH_PEL_MV_ONLY
2973 cm->allow_high_precision_mv = 1;
2974#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002975 cm->allow_high_precision_mv = aom_rb_read_bit(rb);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002976#endif // CONFIG_EIGHTH_PEL_MV_ONLY
RogerZhou10a03802017-10-26 11:49:48 -07002977#endif
Angie Chiang5678ad92016-11-21 09:38:40 -08002978 cm->interp_filter = read_frame_interp_filter(rb);
Fangwen Fu8d164de2016-12-14 13:40:54 -08002979#if CONFIG_TEMPMV_SIGNALING
Rupert Swarbrick1f990a62017-07-11 11:09:33 +01002980 if (frame_might_use_prev_frame_mvs(cm))
Jingning Hane17ebe92017-11-03 15:25:42 -07002981 cm->use_ref_frame_mvs = aom_rb_read_bit(rb);
Rupert Swarbrick1f990a62017-07-11 11:09:33 +01002982 else
Jingning Hane17ebe92017-11-03 15:25:42 -07002983 cm->use_ref_frame_mvs = 0;
2984
2985 cm->use_prev_frame_mvs =
2986 cm->use_ref_frame_mvs && frame_can_use_prev_frame_mvs(cm);
Fangwen Fu8d164de2016-12-14 13:40:54 -08002987#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002988 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2989 RefBuffer *const ref_buf = &cm->frame_refs[i];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002990#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07002991 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002992 &ref_buf->sf, ref_buf->buf->y_crop_width,
2993 ref_buf->buf->y_crop_height, cm->width, cm->height,
2994 cm->use_highbitdepth);
2995#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002996 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002997 &ref_buf->sf, ref_buf->buf->y_crop_width,
2998 ref_buf->buf->y_crop_height, cm->width, cm->height);
2999#endif
3000 }
3001 }
3002 }
Jingning Hanc723b342017-08-24 11:19:46 -07003003
Jingning Hanea255c92017-09-29 08:12:09 -07003004#if CONFIG_FRAME_MARKER
Jingning Hanc723b342017-08-24 11:19:46 -07003005 if (cm->show_frame == 0) {
3006 cm->frame_offset = cm->current_video_frame + aom_rb_read_literal(rb, 4);
3007 } else {
3008 cm->frame_offset = cm->current_video_frame;
3009 }
Zoe Liu17af2742017-10-06 10:36:42 -07003010 av1_setup_frame_buf_refs(cm);
3011
3012#if CONFIG_FRAME_SIGN_BIAS
3013#if CONFIG_OBU
3014 if (cm->frame_type != S_FRAME)
3015#endif // CONFIG_OBU
3016 av1_setup_frame_sign_bias(cm);
Zoe Liu17af2742017-10-06 10:36:42 -07003017#endif // CONFIG_FRAME_SIGN_BIAS
3018#endif // CONFIG_FRAME_MARKER
Jingning Hanc723b342017-08-24 11:19:46 -07003019
Fangwen Fu8d164de2016-12-14 13:40:54 -08003020#if CONFIG_TEMPMV_SIGNALING
3021 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
3022#endif
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003023
3024#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +01003025 if (cm->seq_params.frame_id_numbers_present_flag) {
3026 /* If bitmask is set, update reference frame id values and
3027 mark frames as valid for reference */
3028 int refresh_frame_flags =
3029 cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
3030 for (i = 0; i < REF_FRAMES; i++) {
3031 if ((refresh_frame_flags >> i) & 1) {
3032 cm->ref_frame_id[i] = cm->current_frame_id;
3033 cm->valid_for_referencing[i] = 1;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003034 }
3035 }
3036 }
Debargha Mukherjee778023d2017-09-26 17:50:27 -07003037#endif // CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01003038
Yaowu Xuc27fc142016-08-22 16:08:15 -07003039 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003040 get_frame_new_buffer(cm)->color_space = cm->color_space;
anorkin76fb1262017-03-22 15:12:12 -07003041#if CONFIG_COLORSPACE_HEADERS
3042 get_frame_new_buffer(cm)->transfer_function = cm->transfer_function;
3043 get_frame_new_buffer(cm)->chroma_sample_position = cm->chroma_sample_position;
3044#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003045 get_frame_new_buffer(cm)->color_range = cm->color_range;
3046 get_frame_new_buffer(cm)->render_width = cm->render_width;
3047 get_frame_new_buffer(cm)->render_height = cm->render_height;
3048
3049 if (pbi->need_resync) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003050 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003051 "Keyframe / intra-only frame required to reset decoder"
3052 " state");
3053 }
3054
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01003055#if CONFIG_EXT_TILE
3056 const int might_bwd_adapt =
3057 !(cm->error_resilient_mode || cm->large_scale_tile);
3058#else
3059 const int might_bwd_adapt = !cm->error_resilient_mode;
3060#endif // CONFIG_EXT_TILE
3061 if (might_bwd_adapt) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003062 cm->refresh_frame_context = aom_rb_read_bit(rb)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003063 ? REFRESH_FRAME_CONTEXT_FORWARD
3064 : REFRESH_FRAME_CONTEXT_BACKWARD;
3065 } else {
3066 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
3067 }
Thomas Daededa4d8b92017-06-05 15:44:14 -07003068#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuf883b422016-08-30 14:01:10 -07003069 // This flag will be overridden by the call to av1_setup_past_independence
Yaowu Xuc27fc142016-08-22 16:08:15 -07003070 // below, forcing the use of context 0 for those frame types.
Yaowu Xuf883b422016-08-30 14:01:10 -07003071 cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
Thomas Daededa4d8b92017-06-05 15:44:14 -07003072#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003073
3074 // Generate next_ref_frame_map.
3075 lock_buffer_pool(pool);
3076 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
3077 if (mask & 1) {
3078 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
3079 ++frame_bufs[cm->new_fb_idx].ref_count;
3080 } else {
3081 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
3082 }
3083 // Current thread holds the reference frame.
3084 if (cm->ref_frame_map[ref_index] >= 0)
3085 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
3086 ++ref_index;
3087 }
3088
3089 for (; ref_index < REF_FRAMES; ++ref_index) {
3090 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
3091
3092 // Current thread holds the reference frame.
3093 if (cm->ref_frame_map[ref_index] >= 0)
3094 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
3095 }
3096 unlock_buffer_pool(pool);
3097 pbi->hold_ref_buf = 1;
3098
3099 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
Yaowu Xuf883b422016-08-30 14:01:10 -07003100 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003101
Yaowu Xuc27fc142016-08-22 16:08:15 -07003102 setup_loopfilter(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003103 setup_quantization(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003104 xd->bd = (int)cm->bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003105
hui su0d103572017-03-01 17:58:01 -08003106#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuf883b422016-08-30 14:01:10 -07003107 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003108 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
3109 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
3110 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
3111 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
Thomas Daededa4d8b92017-06-05 15:44:14 -07003112#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
3113 if (cm->frame_refs[0].idx <= 0) {
3114 cm->frame_contexts[cm->frame_refs[0].idx] = *cm->fc;
3115 }
3116#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003117 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07003118#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003119 }
hui su0d103572017-03-01 17:58:01 -08003120#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003121
3122 setup_segmentation(cm, rb);
3123
Arild Fuldseth07441162016-08-15 15:07:52 +02003124 {
Thomas Davies28444be2017-10-13 18:12:25 +01003125 int delta_q_allowed = 1;
3126#if !CONFIG_EXT_DELTA_Q
Arild Fuldseth07441162016-08-15 15:07:52 +02003127 struct segmentation *const seg = &cm->seg;
3128 int segment_quantizer_active = 0;
3129 for (i = 0; i < MAX_SEGMENTS; i++) {
3130 if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
3131 segment_quantizer_active = 1;
3132 }
3133 }
Thomas Davies28444be2017-10-13 18:12:25 +01003134 delta_q_allowed = !segment_quantizer_active;
3135#endif
Arild Fuldseth07441162016-08-15 15:07:52 +02003136
Thomas Daviesf6936102016-09-05 16:51:31 +01003137 cm->delta_q_res = 1;
Fangwen Fu231fe422017-04-24 17:52:29 -07003138#if CONFIG_EXT_DELTA_Q
3139 cm->delta_lf_res = 1;
Jonathan Matthewsa48b1e62017-09-01 14:58:47 +01003140 cm->delta_lf_present_flag = 0;
Cheng Chen880166a2017-10-02 17:48:48 -07003141#if CONFIG_LOOPFILTER_LEVEL
3142 cm->delta_lf_multi = 0;
3143#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu231fe422017-04-24 17:52:29 -07003144#endif
Thomas Davies28444be2017-10-13 18:12:25 +01003145 if (delta_q_allowed == 1 && cm->base_qindex > 0) {
Arild Fuldseth07441162016-08-15 15:07:52 +02003146 cm->delta_q_present_flag = aom_rb_read_bit(rb);
3147 } else {
3148 cm->delta_q_present_flag = 0;
3149 }
3150 if (cm->delta_q_present_flag) {
3151 xd->prev_qindex = cm->base_qindex;
Thomas Daviesf6936102016-09-05 16:51:31 +01003152 cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Fangwen Fu231fe422017-04-24 17:52:29 -07003153#if CONFIG_EXT_DELTA_Q
Fangwen Fu231fe422017-04-24 17:52:29 -07003154 cm->delta_lf_present_flag = aom_rb_read_bit(rb);
3155 if (cm->delta_lf_present_flag) {
Cheng Chen880166a2017-10-02 17:48:48 -07003156 xd->prev_delta_lf_from_base = 0;
3157 cm->delta_lf_res = 1 << aom_rb_read_literal(rb, 2);
Cheng Chena97394f2017-09-27 15:05:14 -07003158#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen880166a2017-10-02 17:48:48 -07003159 cm->delta_lf_multi = aom_rb_read_bit(rb);
Cheng Chena97394f2017-09-27 15:05:14 -07003160 for (int lf_id = 0; lf_id < FRAME_LF_COUNT; ++lf_id)
3161 xd->prev_delta_lf[lf_id] = 0;
3162#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu231fe422017-04-24 17:52:29 -07003163 }
3164#endif // CONFIG_EXT_DELTA_Q
Arild Fuldseth07441162016-08-15 15:07:52 +02003165 }
3166 }
RogerZhou3b635242017-09-19 10:06:46 -07003167#if CONFIG_AMVR
RogerZhou10a03802017-10-26 11:49:48 -07003168 xd->cur_frame_force_integer_mv = cm->cur_frame_force_integer_mv;
RogerZhou3b635242017-09-19 10:06:46 -07003169#endif
Thomas Davies3ab20b42017-09-19 10:30:53 +01003170
Urvang Joshi454280d2016-10-14 16:51:44 -07003171 for (i = 0; i < MAX_SEGMENTS; ++i) {
3172 const int qindex = cm->seg.enabled
3173 ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
3174 : cm->base_qindex;
3175 xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
3176 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
3177 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003178 }
Thomas Daedef636d5c2017-06-29 13:48:27 -07003179 cm->all_lossless = all_lossless(cm, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003180 setup_segmentation_dequant(cm);
Thomas Daedef636d5c2017-06-29 13:48:27 -07003181#if CONFIG_CDEF
3182 if (!cm->all_lossless) {
3183 setup_cdef(cm, rb);
3184 }
3185#endif
3186#if CONFIG_LOOP_RESTORATION
3187 decode_restoration_mode(cm, rb);
3188#endif // CONFIG_LOOP_RESTORATION
3189 cm->tx_mode = read_tx_mode(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003190 cm->reference_mode = read_frame_reference_mode(cm, rb);
Debargha Mukherjee6f3c8982017-09-22 21:14:01 -07003191 if (cm->reference_mode != SINGLE_REFERENCE) setup_compound_reference_mode(cm);
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07003192 read_compound_tools(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003193
Sarah Parkere68a3e42017-02-16 14:03:24 -08003194 cm->reduced_tx_set_used = aom_rb_read_bit(rb);
Sarah Parkere68a3e42017-02-16 14:03:24 -08003195
Angie Chiang6dbffbf2017-10-06 16:59:54 -07003196#if CONFIG_ADAPT_SCAN
3197 cm->use_adapt_scan = aom_rb_read_bit(rb);
3198 // TODO(angiebird): call av1_init_scan_order only when use_adapt_scan
3199 // switches from 1 to 0
3200 if (cm->use_adapt_scan == 0) av1_init_scan_order(cm);
3201#endif // CONFIG_ADAPT_SCAN
3202
Pavel Frolov57c36e12017-09-12 15:00:40 +03003203 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
3204 // show_exisiting_frame=1, nor can it take a frame not used as
3205 // a reference, it is probable that by the time it is being
3206 // referred to, the frame buffer it originally points to may
3207 // already get expired and have been reassigned to the current
3208 // newly coded frame. Hence, we need to check whether this is
3209 // the case, and if yes, we have 2 choices:
3210 // (1) Simply disable the use of previous frame mvs; or
3211 // (2) Have cm->prev_frame point to one reference frame buffer,
3212 // e.g. LAST_FRAME.
3213 if (!dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
3214 // Reassign the LAST_FRAME buffer to cm->prev_frame.
3215 cm->prev_frame =
3216 cm->frame_refs[LAST_FRAME - LAST_FRAME].idx != INVALID_IDX
3217 ? &cm->buffer_pool
3218 ->frame_bufs[cm->frame_refs[LAST_FRAME - LAST_FRAME].idx]
3219 : NULL;
3220 }
Pavel Frolov57c36e12017-09-12 15:00:40 +03003221
3222#if CONFIG_TEMPMV_SIGNALING
3223 if (cm->use_prev_frame_mvs && !frame_can_use_prev_frame_mvs(cm)) {
3224 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3225 "Frame wrongly requests previous frame MVs");
3226 }
3227#else
3228 cm->use_prev_frame_mvs = !cm->error_resilient_mode && cm->prev_frame &&
3229#if CONFIG_FRAME_SUPERRES
3230 cm->width == cm->last_width &&
3231 cm->height == cm->last_height &&
3232#else
3233 cm->width == cm->prev_frame->buf.y_crop_width &&
3234 cm->height == cm->prev_frame->buf.y_crop_height &&
3235#endif // CONFIG_FRAME_SUPERRES
3236 !cm->last_intra_only && cm->last_show_frame &&
3237 (cm->last_frame_type != KEY_FRAME);
3238#endif // CONFIG_TEMPMV_SIGNALING
3239
Sarah Parkerf289f9f2017-09-12 18:50:02 -07003240 if (!frame_is_intra_only(cm)) read_global_motion(cm, rb);
Sarah Parker3e579a62017-08-23 16:53:20 -07003241
Yaowu Xuc27fc142016-08-22 16:08:15 -07003242 read_tile_info(pbi, rb);
Debargha Mukherjee2eada612017-09-22 15:37:39 -07003243 if (use_compressed_header(cm)) {
3244 sz = aom_rb_read_literal(rb, 16);
3245 if (sz == 0)
3246 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3247 "Invalid header size");
3248 } else {
3249 sz = 0;
3250 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003251 return sz;
3252}
3253
Yaowu Xuf883b422016-08-30 14:01:10 -07003254static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003255 size_t partition_size) {
Thomas Davies2e868ab2017-10-24 10:42:27 +01003256#if CONFIG_NEW_MULTISYMBOL
Thomas Daviese7154832017-10-03 10:12:17 +01003257 (void)pbi;
3258 (void)data;
3259 (void)partition_size;
3260 return 0;
3261#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003262 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuf883b422016-08-30 14:01:10 -07003263 aom_reader r;
Ryanf0e39192017-10-09 09:45:13 -07003264
Sebastien Alaiwanfb838772017-10-24 12:02:54 +02003265#if ((CONFIG_RECT_TX_EXT) || (!CONFIG_NEW_MULTISYMBOL || CONFIG_LV_MAP) || \
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02003266 (CONFIG_COMPOUND_SINGLEREF))
Thomas Davies599395e2017-07-21 18:02:48 +01003267 FRAME_CONTEXT *const fc = cm->fc;
Thomas Davies599395e2017-07-21 18:02:48 +01003268#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003269
Alex Converse2cdf0d82016-12-13 13:53:09 -08003270#if CONFIG_ANS && ANS_MAX_SYMBOLS
Alex Converseeb780e72016-12-13 12:46:41 -08003271 r.window_size = 1 << cm->ans_window_size_log2;
Alex Converse2cdf0d82016-12-13 13:53:09 -08003272#endif
Alex Converse346440b2017-01-03 13:47:37 -08003273 if (aom_reader_init(&r, data, partition_size, pbi->decrypt_cb,
3274 pbi->decrypt_state))
Yaowu Xuf883b422016-08-30 14:01:10 -07003275 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003276 "Failed to allocate bool decoder 0");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003277
Sebastien Alaiwanfb838772017-10-24 12:02:54 +02003278#if CONFIG_RECT_TX_EXT
Yue Chen56e226e2017-05-02 16:21:40 -07003279 if (cm->tx_mode == TX_MODE_SELECT)
3280 av1_diff_update_prob(&r, &fc->quarter_tx_size_prob, ACCT_STR);
Yue Chend6bdd462017-07-19 16:05:43 -07003281#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003282
Thomas Davies985bfc32017-06-27 16:51:26 +01003283#if !CONFIG_NEW_MULTISYMBOL
David Barker16c64e32017-08-23 16:54:59 +01003284 if (cm->tx_mode == TX_MODE_SELECT)
Ryanf0e39192017-10-09 09:45:13 -07003285 for (int i = 0; i < TXFM_PARTITION_CONTEXTS; ++i)
David Barker16c64e32017-08-23 16:54:59 +01003286 av1_diff_update_prob(&r, &fc->txfm_partition_prob[i], ACCT_STR);
Ryanf0e39192017-10-09 09:45:13 -07003287 for (int i = 0; i < SKIP_CONTEXTS; ++i)
Thomas Davies61e3e372017-04-04 16:10:23 +01003288 av1_diff_update_prob(&r, &fc->skip_probs[i], ACCT_STR);
Cheng Chen0a7f2f52017-10-10 15:16:09 -07003289
3290#if CONFIG_JNT_COMP
3291 for (int i = 0; i < COMP_INDEX_CONTEXTS; ++i)
3292 av1_diff_update_prob(&r, &fc->compound_index_probs[i], ACCT_STR);
3293#endif // CONFIG_JNT_COMP
Thomas Davies61e3e372017-04-04 16:10:23 +01003294#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003295
Debargha Mukherjee801cc922017-09-22 17:22:50 -07003296 if (!frame_is_intra_only(cm)) {
Thomas Davies149eda52017-06-12 18:11:55 +01003297#if !CONFIG_NEW_MULTISYMBOL
Yaowu Xuc27fc142016-08-22 16:08:15 -07003298 read_inter_mode_probs(fc, &r);
Thomas Davies149eda52017-06-12 18:11:55 +01003299#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003300
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07003301 if (cm->reference_mode != COMPOUND_REFERENCE &&
3302 cm->allow_interintra_compound) {
Thomas Daviescff91712017-07-07 11:49:55 +01003303#if !CONFIG_NEW_MULTISYMBOL
Ryanf0e39192017-10-09 09:45:13 -07003304 for (int i = 0; i < BLOCK_SIZE_GROUPS; i++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003305 if (is_interintra_allowed_bsize_group(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07003306 av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003307 }
3308 }
Thomas Daviescff91712017-07-07 11:49:55 +01003309#endif
Debargha Mukherjee371968c2017-10-29 12:30:04 -07003310#if !CONFIG_NEW_MULTISYMBOL
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003311#if CONFIG_EXT_PARTITION_TYPES
3312 int block_sizes_to_update = BLOCK_SIZES_ALL;
3313#else
3314 int block_sizes_to_update = BLOCK_SIZES;
3315#endif
Ryanf0e39192017-10-09 09:45:13 -07003316 for (int i = 0; i < block_sizes_to_update; i++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003317 if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
Michael Bebenita6048d052016-08-25 14:40:54 -07003318 av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003319 }
3320 }
Debargha Mukherjee371968c2017-10-29 12:30:04 -07003321#endif // !CONFIG_NEW_MULTISYMBOL
Yaowu Xuc27fc142016-08-22 16:08:15 -07003322 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003323
Thomas Daviesf6ad9352017-04-19 11:38:06 +01003324#if !CONFIG_NEW_MULTISYMBOL
Ryanf0e39192017-10-09 09:45:13 -07003325 for (int i = 0; i < INTRA_INTER_CONTEXTS; i++)
Michael Bebenita6048d052016-08-25 14:40:54 -07003326 av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
Thomas Daviesf6ad9352017-04-19 11:38:06 +01003327#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003328
David Barker037ee412017-09-19 12:43:46 +01003329#if !CONFIG_NEW_MULTISYMBOL
Yaowu Xuc27fc142016-08-22 16:08:15 -07003330 read_frame_reference_mode_probs(cm, &r);
David Barker037ee412017-09-19 12:43:46 +01003331#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003332
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02003333#if CONFIG_COMPOUND_SINGLEREF
Ryanf0e39192017-10-09 09:45:13 -07003334 for (int i = 0; i < COMP_INTER_MODE_CONTEXTS; i++)
Zoe Liu85b66462017-04-20 14:28:19 -07003335 av1_diff_update_prob(&r, &fc->comp_inter_mode_prob[i], ACCT_STR);
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02003336#endif // CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07003337
Thomas Davies599395e2017-07-21 18:02:48 +01003338#if !CONFIG_NEW_MULTISYMBOL
RogerZhou3b635242017-09-19 10:06:46 -07003339#if CONFIG_AMVR
RogerZhou10a03802017-10-26 11:49:48 -07003340 if (cm->cur_frame_force_integer_mv == 0) {
RogerZhou3b635242017-09-19 10:06:46 -07003341#endif
Ryanf0e39192017-10-09 09:45:13 -07003342 for (int i = 0; i < NMV_CONTEXTS; ++i)
RogerZhou3b635242017-09-19 10:06:46 -07003343 read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
3344#if CONFIG_AMVR
3345 }
3346#endif
Thomas Davies599395e2017-07-21 18:02:48 +01003347#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003348 }
3349
Yaowu Xuf883b422016-08-30 14:01:10 -07003350 return aom_reader_has_error(&r);
Thomas Davies2e868ab2017-10-24 10:42:27 +01003351#endif // CONFIG_NEW_MULTISYMBOL
Yaowu Xuc27fc142016-08-22 16:08:15 -07003352}
Debargha Mukherjee2eada612017-09-22 15:37:39 -07003353
Yaowu Xuc27fc142016-08-22 16:08:15 -07003354#ifdef NDEBUG
3355#define debug_check_frame_counts(cm) (void)0
3356#else // !NDEBUG
3357// Counts should only be incremented when frame_parallel_decoding_mode and
3358// error_resilient_mode are disabled.
Yaowu Xuf883b422016-08-30 14:01:10 -07003359static void debug_check_frame_counts(const AV1_COMMON *const cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003360 FRAME_COUNTS zero_counts;
Yaowu Xuf883b422016-08-30 14:01:10 -07003361 av1_zero(zero_counts);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003362 assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
3363 cm->error_resilient_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003364 assert(!memcmp(cm->counts.partition, zero_counts.partition,
3365 sizeof(cm->counts.partition)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003366 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
3367 sizeof(cm->counts.switchable_interp)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003368 assert(!memcmp(cm->counts.inter_compound_mode,
3369 zero_counts.inter_compound_mode,
3370 sizeof(cm->counts.inter_compound_mode)));
3371 assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
3372 sizeof(cm->counts.interintra)));
3373 assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
3374 sizeof(cm->counts.wedge_interintra)));
Sarah Parker6fddd182016-11-10 20:57:20 -08003375 assert(!memcmp(cm->counts.compound_interinter,
3376 zero_counts.compound_interinter,
3377 sizeof(cm->counts.compound_interinter)));
Yue Chencb60b182016-10-13 15:18:22 -07003378 assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
3379 sizeof(cm->counts.motion_mode)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003380 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
3381 sizeof(cm->counts.intra_inter)));
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02003382#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07003383 assert(!memcmp(cm->counts.comp_inter_mode, zero_counts.comp_inter_mode,
3384 sizeof(cm->counts.comp_inter_mode)));
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02003385#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07003386 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
3387 sizeof(cm->counts.comp_inter)));
Zoe Liuc082bbc2017-05-17 13:31:37 -07003388#if CONFIG_EXT_COMP_REFS
3389 assert(!memcmp(cm->counts.comp_ref_type, zero_counts.comp_ref_type,
3390 sizeof(cm->counts.comp_ref_type)));
3391 assert(!memcmp(cm->counts.uni_comp_ref, zero_counts.uni_comp_ref,
3392 sizeof(cm->counts.uni_comp_ref)));
3393#endif // CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003394 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
3395 sizeof(cm->counts.single_ref)));
3396 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
3397 sizeof(cm->counts.comp_ref)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003398 assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
3399 sizeof(cm->counts.comp_bwdref)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003400 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003401 assert(
3402 !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
3403 assert(
3404 !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003405}
3406#endif // NDEBUG
3407
Yaowu Xuf883b422016-08-30 14:01:10 -07003408static struct aom_read_bit_buffer *init_read_bit_buffer(
3409 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
3410 const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003411 rb->bit_offset = 0;
3412 rb->error_handler = error_handler;
3413 rb->error_handler_data = &pbi->common;
3414 if (pbi->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003415 const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003416 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
3417 rb->bit_buffer = clear_data;
3418 rb->bit_buffer_end = clear_data + n;
3419 } else {
3420 rb->bit_buffer = data;
3421 rb->bit_buffer_end = data_end;
3422 }
3423 return rb;
3424}
3425
3426//------------------------------------------------------------------------------
3427
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01003428#if CONFIG_FRAME_SIZE
3429void av1_read_frame_size(struct aom_read_bit_buffer *rb, int num_bits_width,
3430 int num_bits_height, int *width, int *height) {
3431 *width = aom_rb_read_literal(rb, num_bits_width) + 1;
3432 *height = aom_rb_read_literal(rb, num_bits_height) + 1;
3433#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003434void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
3435 int *height) {
3436 *width = aom_rb_read_literal(rb, 16) + 1;
3437 *height = aom_rb_read_literal(rb, 16) + 1;
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01003438#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003439}
3440
Yaowu Xuf883b422016-08-30 14:01:10 -07003441BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
3442 int profile = aom_rb_read_bit(rb);
3443 profile |= aom_rb_read_bit(rb) << 1;
3444 if (profile > 2) profile += aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003445 return (BITSTREAM_PROFILE)profile;
3446}
3447
Thomas Davies4822e142017-10-10 11:30:36 +01003448static void make_update_tile_list_dec(AV1Decoder *pbi, int start_tile,
3449 int num_tile, FRAME_CONTEXT *ec_ctxs[]) {
Thomas Davies028b57f2017-02-22 16:42:11 +00003450 int i;
Thomas Davies4822e142017-10-10 11:30:36 +01003451 for (i = start_tile; i < start_tile + num_tile; ++i)
3452 ec_ctxs[i - start_tile] = &pbi->tile_data[i].tctx;
Thomas Davies028b57f2017-02-22 16:42:11 +00003453}
Thomas Davies028b57f2017-02-22 16:42:11 +00003454
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003455#if CONFIG_FRAME_SUPERRES
3456void superres_post_decode(AV1Decoder *pbi) {
3457 AV1_COMMON *const cm = &pbi->common;
3458 BufferPool *const pool = cm->buffer_pool;
3459
3460 if (av1_superres_unscaled(cm)) return;
3461
3462 lock_buffer_pool(pool);
3463 av1_superres_upscale(cm, pool);
3464 unlock_buffer_pool(pool);
3465}
3466#endif // CONFIG_FRAME_SUPERRES
3467
Yi Luo10e23002017-07-31 11:54:43 -07003468static void dec_setup_frame_boundary_info(AV1_COMMON *const cm) {
David Barker5c06a642017-08-18 13:18:16 +01003469// Note: When LOOPFILTERING_ACROSS_TILES is enabled, we need to clear the
3470// boundary information every frame, since the tile boundaries may
3471// change every frame (particularly when dependent-horztiles is also
3472// enabled); when it is disabled, the only information stored is the frame
3473// boundaries, which only depend on the frame size.
3474#if !CONFIG_LOOPFILTERING_ACROSS_TILES
3475 if (cm->width != cm->last_width || cm->height != cm->last_height)
3476#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
3477 {
Yi Luo10e23002017-07-31 11:54:43 -07003478 int row, col;
3479 for (row = 0; row < cm->mi_rows; ++row) {
3480 MODE_INFO *mi = cm->mi + row * cm->mi_stride;
3481 for (col = 0; col < cm->mi_cols; ++col) {
3482 mi->mbmi.boundary_info = 0;
3483 mi++;
3484 }
3485 }
3486 av1_setup_frame_boundary_info(cm);
3487 }
3488}
3489
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003490size_t av1_decode_frame_headers_and_setup(AV1Decoder *pbi, const uint8_t *data,
3491 const uint8_t *data_end,
3492 const uint8_t **p_data_end) {
3493 AV1_COMMON *const cm = &pbi->common;
3494 MACROBLOCKD *const xd = &pbi->mb;
3495 struct aom_read_bit_buffer rb;
3496 uint8_t clear_data[MAX_AV1_HEADER_SIZE];
3497 size_t first_partition_size;
3498 YV12_BUFFER_CONFIG *new_fb;
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003499 RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003500
3501#if CONFIG_ADAPT_SCAN
3502 av1_deliver_eob_threshold(cm, xd);
3503#endif
3504#if CONFIG_BITSTREAM_DEBUG
3505 bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
3506#endif
3507
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003508 int i;
3509 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003510 cm->global_motion[i] = default_warp_params;
3511 cm->cur_frame->global_motion[i] = default_warp_params;
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003512 }
3513 xd->global_motion = cm->global_motion;
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003514
3515 first_partition_size = read_uncompressed_header(
3516 pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
3517
3518#if CONFIG_EXT_TILE
3519 // If cm->single_tile_decoding = 0, the independent decoding of a single tile
3520 // or a section of a frame is not allowed.
3521 if (!cm->single_tile_decoding &&
3522 (pbi->dec_tile_row >= 0 || pbi->dec_tile_col >= 0)) {
3523 pbi->dec_tile_row = -1;
3524 pbi->dec_tile_col = -1;
3525 }
3526#endif // CONFIG_EXT_TILE
3527
3528 pbi->first_partition_size = first_partition_size;
3529 pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
3530 new_fb = get_frame_new_buffer(cm);
3531 xd->cur_buf = new_fb;
3532#if CONFIG_INTRABC
3533#if CONFIG_HIGHBITDEPTH
3534 av1_setup_scale_factors_for_frame(
3535 &xd->sf_identity, xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height,
3536 xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height,
3537 cm->use_highbitdepth);
3538#else
3539 av1_setup_scale_factors_for_frame(
3540 &xd->sf_identity, xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height,
3541 xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height);
3542#endif // CONFIG_HIGHBITDEPTH
3543#endif // CONFIG_INTRABC
3544
Debargha Mukherjee2eada612017-09-22 15:37:39 -07003545 if (cm->show_existing_frame) {
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003546 // showing a frame directly
3547 *p_data_end = data + aom_rb_bytes_read(&rb);
3548 return 0;
3549 }
3550
3551 data += aom_rb_bytes_read(&rb);
Debargha Mukherjee2eada612017-09-22 15:37:39 -07003552 if (first_partition_size)
3553 if (!read_is_valid(data, first_partition_size, data_end))
3554 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3555 "Truncated packet or corrupt header length");
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003556
3557 cm->setup_mi(cm);
3558
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003559 // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
3560 // show_exisiting_frame=1, nor can it take a frame not used as
3561 // a reference, it is probable that by the time it is being
3562 // referred to, the frame buffer it originally points to may
3563 // already get expired and have been reassigned to the current
3564 // newly coded frame. Hence, we need to check whether this is
3565 // the case, and if yes, we have 2 choices:
3566 // (1) Simply disable the use of previous frame mvs; or
3567 // (2) Have cm->prev_frame point to one reference frame buffer,
3568 // e.g. LAST_FRAME.
3569 if (!dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
3570 // Reassign the LAST_FRAME buffer to cm->prev_frame.
3571 cm->prev_frame = last_fb_ref_buf->idx != INVALID_IDX
3572 ? &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx]
3573 : NULL;
3574 }
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003575
3576#if CONFIG_TEMPMV_SIGNALING
3577 if (cm->use_prev_frame_mvs && !frame_can_use_prev_frame_mvs(cm)) {
3578 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3579 "Frame wrongly requests previous frame MVs");
3580 }
3581#else
3582 cm->use_prev_frame_mvs = !cm->error_resilient_mode && cm->prev_frame &&
3583#if CONFIG_FRAME_SUPERRES
3584 cm->width == cm->last_width &&
3585 cm->height == cm->last_height &&
3586#else
3587 cm->width == cm->prev_frame->buf.y_crop_width &&
3588 cm->height == cm->prev_frame->buf.y_crop_height &&
3589#endif // CONFIG_FRAME_SUPERRES
3590 !cm->last_intra_only && cm->last_show_frame &&
3591 (cm->last_frame_type != KEY_FRAME);
3592#endif // CONFIG_TEMPMV_SIGNALING
3593
Zoe Liuf704a1c2017-10-02 16:55:59 -07003594#if CONFIG_EXT_SKIP
3595 av1_setup_skip_mode_allowed(cm);
3596#if 0
3597 printf("\nDECODER: Frame=%d, frame_offset=%d, show_frame=%d, "
3598 "is_skip_mode_allowed=%d, ref_frame_idx=(%d,%d)\n",
3599 cm->current_video_frame, cm->frame_offset, cm->show_frame,
3600 cm->is_skip_mode_allowed, cm->ref_frame_idx_0, cm->ref_frame_idx_1);
3601#endif // 0
3602#endif // CONFIG_EXT_SKIP
3603
Jingning Hanea255c92017-09-29 08:12:09 -07003604#if CONFIG_MFMV
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003605 av1_setup_motion_field(cm);
Jingning Hanea255c92017-09-29 08:12:09 -07003606#endif // CONFIG_MFMV
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003607
3608 av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
3609#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
3610 if (cm->error_resilient_mode || frame_is_intra_only(cm)) {
3611 // use the default frame context values
3612 *cm->fc = cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
3613 cm->pre_fc = &cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
3614 } else {
3615 *cm->fc = cm->frame_contexts[cm->frame_refs[0].idx];
3616 cm->pre_fc = &cm->frame_contexts[cm->frame_refs[0].idx];
3617 }
3618#else
3619 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
3620 cm->pre_fc = &cm->frame_contexts[cm->frame_context_idx];
3621#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
3622 if (!cm->fc->initialized)
3623 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3624 "Uninitialized entropy context.");
3625
3626 av1_zero(cm->counts);
3627
3628 xd->corrupted = 0;
Debargha Mukherjee2eada612017-09-22 15:37:39 -07003629 if (first_partition_size) {
3630 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
3631 if (new_fb->corrupted)
3632 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3633 "Decode failed. Frame data header is corrupted.");
3634 }
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003635 return first_partition_size;
3636}
3637
David Barker01563082017-10-09 13:59:16 +01003638// Once-per-frame initialization
3639static void setup_frame_info(AV1Decoder *pbi) {
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003640 AV1_COMMON *const cm = &pbi->common;
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003641
3642#if CONFIG_LOOP_RESTORATION
3643 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
3644 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
3645 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
3646 av1_alloc_restoration_buffers(cm);
3647 }
3648#endif
3649
Cheng Chend8184da2017-09-26 18:15:22 -07003650#if !CONFIG_LOOPFILTER_LEVEL
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003651 if (cm->lf.filter_level && !cm->skip_loop_filter) {
3652 av1_loop_filter_frame_init(cm, cm->lf.filter_level, cm->lf.filter_level);
3653 }
3654#endif
3655
3656 // If encoded in frame parallel mode, frame context is ready after decoding
3657 // the frame header.
David Barker01563082017-10-09 13:59:16 +01003658 if (cm->frame_parallel_decode &&
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003659 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
3660 AVxWorker *const worker = pbi->frame_worker_owner;
3661 FrameWorkerData *const frame_worker_data = worker->data1;
3662 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003663#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
3664 cm->frame_contexts[cm->new_fb_idx] = *cm->fc;
3665#else
3666 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
3667#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
3668 }
3669 av1_frameworker_lock_stats(worker);
3670 pbi->cur_buf->row = -1;
3671 pbi->cur_buf->col = -1;
3672 frame_worker_data->frame_context_ready = 1;
3673 // Signal the main thread that context is ready.
3674 av1_frameworker_signal_stats(worker);
3675 av1_frameworker_unlock_stats(worker);
3676 }
3677
3678 dec_setup_frame_boundary_info(cm);
David Barker01563082017-10-09 13:59:16 +01003679}
3680
3681void av1_decode_tg_tiles_and_wrapup(AV1Decoder *pbi, const uint8_t *data,
3682 const uint8_t *data_end,
3683 const uint8_t **p_data_end, int startTile,
3684 int endTile, int initialize_flag) {
3685 AV1_COMMON *const cm = &pbi->common;
3686 MACROBLOCKD *const xd = &pbi->mb;
3687
3688 if (initialize_flag) setup_frame_info(pbi);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003689
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003690#if CONFIG_OBU
Debargha Mukherjee6ea917e2017-10-19 09:31:29 -07003691 *p_data_end = decode_tiles(pbi, data, data_end, startTile, endTile);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003692#else
Debargha Mukherjee6ea917e2017-10-19 09:31:29 -07003693 *p_data_end =
3694 decode_tiles(pbi, data + pbi->uncomp_hdr_size + pbi->first_partition_size,
3695 data_end, startTile, endTile);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003696#endif
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003697
3698 if (endTile != cm->tile_rows * cm->tile_cols - 1) {
3699 return;
3700 }
3701
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02003702#if CONFIG_STRIPED_LOOP_RESTORATION
Rupert Swarbrick8ce049e2017-11-06 16:47:47 +00003703#if CONFIG_FRAME_SUPERRES && CONFIG_HORZONLY_FRAME_SUPERRES
3704 if (!av1_superres_unscaled(cm)) aom_extend_frame_borders(&pbi->cur_buf->buf);
3705#endif
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02003706 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
3707 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
3708 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Rupert Swarbrick921b3342017-11-06 18:04:22 +00003709 av1_loop_restoration_save_boundary_lines(&pbi->cur_buf->buf, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02003710 }
3711#endif
3712
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003713#if CONFIG_CDEF
3714 if (!cm->skip_loop_filter && !cm->all_lossless) {
3715 av1_cdef_frame(&pbi->cur_buf->buf, cm, &pbi->mb);
3716 }
3717#endif // CONFIG_CDEF
3718
3719#if CONFIG_FRAME_SUPERRES
3720 superres_post_decode(pbi);
3721#endif // CONFIG_FRAME_SUPERRES
3722
3723#if CONFIG_LOOP_RESTORATION
3724 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
3725 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
3726 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Rupert Swarbrick921b3342017-11-06 18:04:22 +00003727 av1_loop_restoration_save_boundary_lines(&pbi->cur_buf->buf, cm, 1);
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01003728 av1_loop_restoration_filter_frame((YV12_BUFFER_CONFIG *)xd->cur_buf, cm,
3729 cm->rst_info, 7, NULL);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003730 }
3731#endif // CONFIG_LOOP_RESTORATION
3732
3733 if (!xd->corrupted) {
3734 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Thomas Davies4822e142017-10-10 11:30:36 +01003735#if CONFIG_SIMPLE_BWD_ADAPT
3736 const int num_bwd_ctxs = 1;
3737#else
3738 const int num_bwd_ctxs = cm->tile_rows * cm->tile_cols;
3739#endif
3740 FRAME_CONTEXT **tile_ctxs =
3741 aom_malloc(num_bwd_ctxs * sizeof(&pbi->tile_data[0].tctx));
3742 aom_cdf_prob **cdf_ptrs = aom_malloc(
3743 num_bwd_ctxs * sizeof(&pbi->tile_data[0].tctx.partition_cdf[0][0]));
3744#if CONFIG_SIMPLE_BWD_ADAPT
3745 make_update_tile_list_dec(pbi, cm->largest_tile_id, num_bwd_ctxs,
3746 tile_ctxs);
3747#else
3748 make_update_tile_list_dec(pbi, 0, num_bwd_ctxs, tile_ctxs);
3749#endif
Angie Chiang85e3b962017-10-01 16:04:43 -07003750#if CONFIG_SYMBOLRATE
3751 av1_dump_symbol_rate(cm);
3752#endif
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003753 av1_adapt_intra_frame_probs(cm);
3754 av1_average_tile_coef_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies4822e142017-10-10 11:30:36 +01003755 num_bwd_ctxs);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003756 av1_average_tile_intra_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies4822e142017-10-10 11:30:36 +01003757 num_bwd_ctxs);
Debargha Mukherjee43061b32017-10-13 16:50:17 -07003758 av1_average_tile_loopfilter_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
3759 num_bwd_ctxs);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003760#if CONFIG_ADAPT_SCAN
3761 av1_adapt_scan_order(cm);
3762#endif // CONFIG_ADAPT_SCAN
3763
3764 if (!frame_is_intra_only(cm)) {
3765 av1_adapt_inter_frame_probs(cm);
Thomas Davies0e7b1d72017-10-02 10:54:24 +01003766#if !CONFIG_NEW_MULTISYMBOL
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003767 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Thomas Davies0e7b1d72017-10-02 10:54:24 +01003768#endif
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003769 av1_average_tile_inter_cdfs(&pbi->common, pbi->common.fc, tile_ctxs,
Thomas Davies4822e142017-10-10 11:30:36 +01003770 cdf_ptrs, num_bwd_ctxs);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003771 av1_average_tile_mv_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies4822e142017-10-10 11:30:36 +01003772 num_bwd_ctxs);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003773 }
3774 aom_free(tile_ctxs);
3775 aom_free(cdf_ptrs);
3776 } else {
3777 debug_check_frame_counts(cm);
3778 }
3779 } else {
3780 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
3781 "Decode failed. Frame data is corrupted.");
3782 }
3783
3784#if CONFIG_INSPECTION
3785 if (pbi->inspect_cb != NULL) {
3786 (*pbi->inspect_cb)(pbi, pbi->inspect_ctx);
3787 }
3788#endif
3789
David Barker01563082017-10-09 13:59:16 +01003790 // Non frame parallel update frame context here.
David Barkerb44deca2017-11-03 17:06:10 +00003791 if (!cm->frame_parallel_decode ||
3792 cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_FORWARD) {
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003793#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
David Barker01563082017-10-09 13:59:16 +01003794 cm->frame_contexts[cm->new_fb_idx] = *cm->fc;
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003795#else
David Barker01563082017-10-09 13:59:16 +01003796 if (!cm->error_resilient_mode)
3797 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003798#endif
David Barker01563082017-10-09 13:59:16 +01003799 }
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003800}
3801
3802#if CONFIG_OBU
3803
3804static OBU_TYPE read_obu_header(struct aom_read_bit_buffer *rb,
3805 uint32_t *header_size) {
3806 OBU_TYPE obu_type;
3807 int obu_extension_flag;
3808
3809 *header_size = 1;
3810
Soo-Chul Han38427e82017-09-27 15:06:13 -04003811 // first bit is obu_forbidden_bit (0) according to R19
3812 aom_rb_read_bit(rb);
3813
3814 obu_type = (OBU_TYPE)aom_rb_read_literal(rb, 4);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003815 aom_rb_read_literal(rb, 2); // reserved
3816 obu_extension_flag = aom_rb_read_bit(rb);
3817 if (obu_extension_flag) {
3818 *header_size += 1;
3819 aom_rb_read_literal(rb, 3); // temporal_id
3820 aom_rb_read_literal(rb, 2);
3821 aom_rb_read_literal(rb, 2);
3822 aom_rb_read_literal(rb, 1); // reserved
3823 }
3824
3825 return obu_type;
3826}
3827
3828static uint32_t read_temporal_delimiter_obu() { return 0; }
3829
3830static uint32_t read_sequence_header_obu(AV1Decoder *pbi,
3831 struct aom_read_bit_buffer *rb) {
3832 AV1_COMMON *const cm = &pbi->common;
David Barker5e70a112017-10-03 14:28:17 +01003833 SequenceHeader *const seq_params = &cm->seq_params;
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003834 uint32_t saved_bit_offset = rb->bit_offset;
3835
3836 cm->profile = av1_read_profile(rb);
3837 aom_rb_read_literal(rb, 4); // level
3838
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01003839#if CONFIG_FRAME_SIZE
3840 int num_bits_width = aom_rb_read_literal(rb, 4) + 1;
3841 int num_bits_height = aom_rb_read_literal(rb, 4) + 1;
3842 int max_frame_width = aom_rb_read_literal(rb, num_bits_width) + 1;
3843 int max_frame_height = aom_rb_read_literal(rb, num_bits_height) + 1;
3844
3845 seq_params->num_bits_width = num_bits_width;
3846 seq_params->num_bits_height = num_bits_height;
3847 seq_params->max_frame_width = max_frame_width;
3848 seq_params->max_frame_height = max_frame_height;
3849#endif
3850
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003851 seq_params->frame_id_numbers_present_flag = aom_rb_read_bit(rb);
3852 if (seq_params->frame_id_numbers_present_flag) {
Frederic Barbier4d5d90e2017-10-13 09:22:33 +02003853 // We must always have delta_frame_id_length < frame_id_length,
3854 // in order for a frame to be referenced with a unique delta.
3855 // Avoid wasting bits by using a coding that enforces this restriction.
Frederic Barbiere83fcfe2017-10-13 10:37:50 +02003856 seq_params->delta_frame_id_length = aom_rb_read_literal(rb, 4) + 2;
Frederic Barbier4d5d90e2017-10-13 09:22:33 +02003857 seq_params->frame_id_length =
3858 aom_rb_read_literal(rb, 3) + seq_params->delta_frame_id_length + 1;
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003859 }
3860
3861 read_bitdepth_colorspace_sampling(cm, rb, pbi->allow_lowbitdepth);
3862
3863 return ((rb->bit_offset - saved_bit_offset + 7) >> 3);
3864}
3865
3866static uint32_t read_frame_header_obu(AV1Decoder *pbi, const uint8_t *data,
3867 const uint8_t *data_end,
3868 const uint8_t **p_data_end) {
3869 size_t header_size;
3870
3871 header_size =
3872 av1_decode_frame_headers_and_setup(pbi, data, data_end, p_data_end);
3873 return (uint32_t)(pbi->uncomp_hdr_size + header_size);
3874}
3875
3876static uint32_t read_tile_group_header(AV1Decoder *pbi,
3877 struct aom_read_bit_buffer *rb,
3878 int *startTile, int *endTile) {
3879 AV1_COMMON *const cm = &pbi->common;
3880 uint32_t saved_bit_offset = rb->bit_offset;
3881
3882 *startTile = aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3883 *endTile = aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
3884
3885 return ((rb->bit_offset - saved_bit_offset + 7) >> 3);
3886}
3887
3888static uint32_t read_one_tile_group_obu(AV1Decoder *pbi,
3889 struct aom_read_bit_buffer *rb,
3890 int is_first_tg, const uint8_t *data,
3891 const uint8_t *data_end,
3892 const uint8_t **p_data_end,
3893 int *is_last_tg) {
3894 AV1_COMMON *const cm = &pbi->common;
3895 int startTile, endTile;
3896 uint32_t header_size, tg_payload_size;
3897
3898 header_size = read_tile_group_header(pbi, rb, &startTile, &endTile);
3899 data += header_size;
3900 av1_decode_tg_tiles_and_wrapup(pbi, data, data_end, p_data_end, startTile,
3901 endTile, is_first_tg);
3902 tg_payload_size = (uint32_t)(*p_data_end - data);
3903
3904 // TODO(shan): For now, assume all tile groups received in order
3905 *is_last_tg = endTile == cm->tile_rows * cm->tile_cols - 1;
3906
3907 return header_size + tg_payload_size;
3908}
3909
Soo-Chul Han38427e82017-09-27 15:06:13 -04003910static void read_metadata_private_data(const uint8_t *data, uint32_t sz) {
3911 int i;
3912
3913 for (i = 0; i < (int)sz; i++) {
3914 mem_get_le16(data);
3915 data += 2;
3916 }
3917}
3918
3919static void read_metadata_hdr_cll(const uint8_t *data) {
3920 mem_get_le16(data);
3921 mem_get_le16(data + 2);
3922}
3923
3924static void read_metadata_hdr_mdcv(const uint8_t *data) {
3925 int i;
3926
3927 for (i = 0; i < 3; i++) {
3928 mem_get_le16(data);
3929 data += 2;
3930 mem_get_le16(data);
3931 data += 2;
3932 }
3933
3934 mem_get_le16(data);
3935 data += 2;
3936 mem_get_le16(data);
3937 data += 2;
3938 mem_get_le16(data);
3939 data += 2;
3940 mem_get_le16(data);
3941}
3942
3943static uint32_t read_metadata(const uint8_t *data, uint32_t sz) {
3944 METADATA_TYPE metadata_type;
3945
3946 metadata_type = (METADATA_TYPE)mem_get_le16(data);
3947
3948 if (metadata_type == METADATA_TYPE_PRIVATE_DATA) {
3949 read_metadata_private_data(data + 2, sz - 2);
3950 } else if (metadata_type == METADATA_TYPE_HDR_CLL) {
3951 read_metadata_hdr_cll(data + 2);
3952 } else if (metadata_type == METADATA_TYPE_HDR_MDCV) {
3953 read_metadata_hdr_mdcv(data + 2);
3954 }
3955
3956 return sz;
3957}
3958
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003959void av1_decode_frame_from_obus(struct AV1Decoder *pbi, const uint8_t *data,
3960 const uint8_t *data_end,
3961 const uint8_t **p_data_end) {
3962 AV1_COMMON *const cm = &pbi->common;
3963 int frame_decoding_finished = 0;
3964 int is_first_tg_obu_received = 1;
3965 int frame_header_received = 0;
3966 int frame_header_size = 0;
3967
3968 // decode frame as a series of OBUs
3969 while (!frame_decoding_finished && !cm->error.error_code) {
3970 struct aom_read_bit_buffer rb;
3971 uint8_t clear_data[80];
3972 uint32_t obu_size, obu_header_size, obu_payload_size = 0;
3973 OBU_TYPE obu_type;
3974
Soo-Chul Han38427e82017-09-27 15:06:13 -04003975 init_read_bit_buffer(pbi, &rb, data + PRE_OBU_SIZE_BYTES, data_end,
3976 clear_data);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003977
Soo-Chul Han38427e82017-09-27 15:06:13 -04003978// every obu is preceded by PRE_OBU_SIZE_BYTES-byte size of obu (obu header +
3979// payload size)
3980// The obu size is only needed for tile group OBUs
3981#if CONFIG_ADD_4BYTES_OBUSIZE
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003982 obu_size = mem_get_le32(data);
Soo-Chul Han38427e82017-09-27 15:06:13 -04003983#else
3984 obu_size = data_end - data;
3985#endif
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003986 obu_type = read_obu_header(&rb, &obu_header_size);
Soo-Chul Han38427e82017-09-27 15:06:13 -04003987 data += (PRE_OBU_SIZE_BYTES + obu_header_size);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04003988
3989 switch (obu_type) {
3990 case OBU_TD: obu_payload_size = read_temporal_delimiter_obu(); break;
3991 case OBU_SEQUENCE_HEADER:
3992 obu_payload_size = read_sequence_header_obu(pbi, &rb);
3993 break;
3994 case OBU_FRAME_HEADER:
3995 // Only decode first frame header received
3996 if (!frame_header_received) {
3997 frame_header_size = obu_payload_size =
3998 read_frame_header_obu(pbi, data, data_end, p_data_end);
3999 frame_header_received = 1;
4000 } else {
4001 obu_payload_size = frame_header_size;
4002 }
4003 if (cm->show_existing_frame) frame_decoding_finished = 1;
4004 break;
4005 case OBU_TILE_GROUP:
David Barker01563082017-10-09 13:59:16 +01004006 obu_payload_size =
4007 read_one_tile_group_obu(pbi, &rb, is_first_tg_obu_received, data,
4008 data + obu_size - obu_header_size,
4009 p_data_end, &frame_decoding_finished);
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04004010 is_first_tg_obu_received = 0;
4011 break;
Soo-Chul Han38427e82017-09-27 15:06:13 -04004012 case OBU_METADATA:
4013 obu_payload_size = read_metadata(data, obu_size);
4014 break;
Soo-Chul Han65c00ae2017-09-07 13:12:35 -04004015 default: break;
4016 }
4017 data += obu_payload_size;
4018 }
4019}
4020#endif