xref: /aosp_15_r20/external/libaom/av1/decoder/obu.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2017, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include <stdbool.h>
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_scale_rtcd.h"
17*77c1e3ccSAndroid Build Coastguard Worker 
18*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_codec.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/bitreader_buffer.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/mem_ops.h"
21*77c1e3ccSAndroid Build Coastguard Worker 
22*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/common.h"
23*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/obu_util.h"
24*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/timing.h"
25*77c1e3ccSAndroid Build Coastguard Worker #include "av1/decoder/decoder.h"
26*77c1e3ccSAndroid Build Coastguard Worker #include "av1/decoder/decodeframe.h"
27*77c1e3ccSAndroid Build Coastguard Worker #include "av1/decoder/obu.h"
28*77c1e3ccSAndroid Build Coastguard Worker 
aom_get_num_layers_from_operating_point_idc(int operating_point_idc,unsigned int * number_spatial_layers,unsigned int * number_temporal_layers)29*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t aom_get_num_layers_from_operating_point_idc(
30*77c1e3ccSAndroid Build Coastguard Worker     int operating_point_idc, unsigned int *number_spatial_layers,
31*77c1e3ccSAndroid Build Coastguard Worker     unsigned int *number_temporal_layers) {
32*77c1e3ccSAndroid Build Coastguard Worker   // derive number of spatial/temporal layers from operating_point_idc
33*77c1e3ccSAndroid Build Coastguard Worker 
34*77c1e3ccSAndroid Build Coastguard Worker   if (!number_spatial_layers || !number_temporal_layers)
35*77c1e3ccSAndroid Build Coastguard Worker     return AOM_CODEC_INVALID_PARAM;
36*77c1e3ccSAndroid Build Coastguard Worker 
37*77c1e3ccSAndroid Build Coastguard Worker   if (operating_point_idc == 0) {
38*77c1e3ccSAndroid Build Coastguard Worker     *number_temporal_layers = 1;
39*77c1e3ccSAndroid Build Coastguard Worker     *number_spatial_layers = 1;
40*77c1e3ccSAndroid Build Coastguard Worker   } else {
41*77c1e3ccSAndroid Build Coastguard Worker     *number_spatial_layers = 0;
42*77c1e3ccSAndroid Build Coastguard Worker     *number_temporal_layers = 0;
43*77c1e3ccSAndroid Build Coastguard Worker     for (int j = 0; j < MAX_NUM_SPATIAL_LAYERS; j++) {
44*77c1e3ccSAndroid Build Coastguard Worker       *number_spatial_layers +=
45*77c1e3ccSAndroid Build Coastguard Worker           (operating_point_idc >> (j + MAX_NUM_TEMPORAL_LAYERS)) & 0x1;
46*77c1e3ccSAndroid Build Coastguard Worker     }
47*77c1e3ccSAndroid Build Coastguard Worker     for (int j = 0; j < MAX_NUM_TEMPORAL_LAYERS; j++) {
48*77c1e3ccSAndroid Build Coastguard Worker       *number_temporal_layers += (operating_point_idc >> j) & 0x1;
49*77c1e3ccSAndroid Build Coastguard Worker     }
50*77c1e3ccSAndroid Build Coastguard Worker   }
51*77c1e3ccSAndroid Build Coastguard Worker 
52*77c1e3ccSAndroid Build Coastguard Worker   return AOM_CODEC_OK;
53*77c1e3ccSAndroid Build Coastguard Worker }
54*77c1e3ccSAndroid Build Coastguard Worker 
is_obu_in_current_operating_point(AV1Decoder * pbi,const ObuHeader * obu_header)55*77c1e3ccSAndroid Build Coastguard Worker static int is_obu_in_current_operating_point(AV1Decoder *pbi,
56*77c1e3ccSAndroid Build Coastguard Worker                                              const ObuHeader *obu_header) {
57*77c1e3ccSAndroid Build Coastguard Worker   if (!pbi->current_operating_point || !obu_header->has_extension) {
58*77c1e3ccSAndroid Build Coastguard Worker     return 1;
59*77c1e3ccSAndroid Build Coastguard Worker   }
60*77c1e3ccSAndroid Build Coastguard Worker 
61*77c1e3ccSAndroid Build Coastguard Worker   if ((pbi->current_operating_point >> obu_header->temporal_layer_id) & 0x1 &&
62*77c1e3ccSAndroid Build Coastguard Worker       (pbi->current_operating_point >> (obu_header->spatial_layer_id + 8)) &
63*77c1e3ccSAndroid Build Coastguard Worker           0x1) {
64*77c1e3ccSAndroid Build Coastguard Worker     return 1;
65*77c1e3ccSAndroid Build Coastguard Worker   }
66*77c1e3ccSAndroid Build Coastguard Worker   return 0;
67*77c1e3ccSAndroid Build Coastguard Worker }
68*77c1e3ccSAndroid Build Coastguard Worker 
byte_alignment(AV1_COMMON * const cm,struct aom_read_bit_buffer * const rb)69*77c1e3ccSAndroid Build Coastguard Worker static int byte_alignment(AV1_COMMON *const cm,
70*77c1e3ccSAndroid Build Coastguard Worker                           struct aom_read_bit_buffer *const rb) {
71*77c1e3ccSAndroid Build Coastguard Worker   while (rb->bit_offset & 7) {
72*77c1e3ccSAndroid Build Coastguard Worker     if (aom_rb_read_bit(rb)) {
73*77c1e3ccSAndroid Build Coastguard Worker       cm->error->error_code = AOM_CODEC_CORRUPT_FRAME;
74*77c1e3ccSAndroid Build Coastguard Worker       return -1;
75*77c1e3ccSAndroid Build Coastguard Worker     }
76*77c1e3ccSAndroid Build Coastguard Worker   }
77*77c1e3ccSAndroid Build Coastguard Worker   return 0;
78*77c1e3ccSAndroid Build Coastguard Worker }
79*77c1e3ccSAndroid Build Coastguard Worker 
read_temporal_delimiter_obu(void)80*77c1e3ccSAndroid Build Coastguard Worker static uint32_t read_temporal_delimiter_obu(void) { return 0; }
81*77c1e3ccSAndroid Build Coastguard Worker 
82*77c1e3ccSAndroid Build Coastguard Worker // Returns a boolean that indicates success.
read_bitstream_level(AV1_LEVEL * seq_level_idx,struct aom_read_bit_buffer * rb)83*77c1e3ccSAndroid Build Coastguard Worker static int read_bitstream_level(AV1_LEVEL *seq_level_idx,
84*77c1e3ccSAndroid Build Coastguard Worker                                 struct aom_read_bit_buffer *rb) {
85*77c1e3ccSAndroid Build Coastguard Worker   *seq_level_idx = aom_rb_read_literal(rb, LEVEL_BITS);
86*77c1e3ccSAndroid Build Coastguard Worker   if (!is_valid_seq_level_idx(*seq_level_idx)) return 0;
87*77c1e3ccSAndroid Build Coastguard Worker   return 1;
88*77c1e3ccSAndroid Build Coastguard Worker }
89*77c1e3ccSAndroid Build Coastguard Worker 
90*77c1e3ccSAndroid Build Coastguard Worker // Returns whether two sequence headers are consistent with each other.
91*77c1e3ccSAndroid Build Coastguard Worker // Note that the 'op_params' field is not compared per Section 7.5 in the spec:
92*77c1e3ccSAndroid Build Coastguard Worker //   Within a particular coded video sequence, the contents of
93*77c1e3ccSAndroid Build Coastguard Worker //   sequence_header_obu must be bit-identical each time the sequence header
94*77c1e3ccSAndroid Build Coastguard Worker //   appears except for the contents of operating_parameters_info.
are_seq_headers_consistent(const SequenceHeader * seq_params_old,const SequenceHeader * seq_params_new)95*77c1e3ccSAndroid Build Coastguard Worker static int are_seq_headers_consistent(const SequenceHeader *seq_params_old,
96*77c1e3ccSAndroid Build Coastguard Worker                                       const SequenceHeader *seq_params_new) {
97*77c1e3ccSAndroid Build Coastguard Worker   return !memcmp(seq_params_old, seq_params_new,
98*77c1e3ccSAndroid Build Coastguard Worker                  offsetof(SequenceHeader, op_params));
99*77c1e3ccSAndroid Build Coastguard Worker }
100*77c1e3ccSAndroid Build Coastguard Worker 
101*77c1e3ccSAndroid Build Coastguard Worker // On success, sets pbi->sequence_header_ready to 1 and returns the number of
102*77c1e3ccSAndroid Build Coastguard Worker // bytes read from 'rb'.
103*77c1e3ccSAndroid Build Coastguard Worker // On failure, sets pbi->common.error.error_code and returns 0.
read_sequence_header_obu(AV1Decoder * pbi,struct aom_read_bit_buffer * rb)104*77c1e3ccSAndroid Build Coastguard Worker static uint32_t read_sequence_header_obu(AV1Decoder *pbi,
105*77c1e3ccSAndroid Build Coastguard Worker                                          struct aom_read_bit_buffer *rb) {
106*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &pbi->common;
107*77c1e3ccSAndroid Build Coastguard Worker   const uint32_t saved_bit_offset = rb->bit_offset;
108*77c1e3ccSAndroid Build Coastguard Worker 
109*77c1e3ccSAndroid Build Coastguard Worker   // Verify rb has been configured to report errors.
110*77c1e3ccSAndroid Build Coastguard Worker   assert(rb->error_handler);
111*77c1e3ccSAndroid Build Coastguard Worker 
112*77c1e3ccSAndroid Build Coastguard Worker   // Use a local variable to store the information as we decode. At the end,
113*77c1e3ccSAndroid Build Coastguard Worker   // if no errors have occurred, cm->seq_params is updated.
114*77c1e3ccSAndroid Build Coastguard Worker   SequenceHeader sh = *cm->seq_params;
115*77c1e3ccSAndroid Build Coastguard Worker   SequenceHeader *const seq_params = &sh;
116*77c1e3ccSAndroid Build Coastguard Worker 
117*77c1e3ccSAndroid Build Coastguard Worker   seq_params->profile = av1_read_profile(rb);
118*77c1e3ccSAndroid Build Coastguard Worker   if (seq_params->profile > CONFIG_MAX_DECODE_PROFILE) {
119*77c1e3ccSAndroid Build Coastguard Worker     pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
120*77c1e3ccSAndroid Build Coastguard Worker     return 0;
121*77c1e3ccSAndroid Build Coastguard Worker   }
122*77c1e3ccSAndroid Build Coastguard Worker 
123*77c1e3ccSAndroid Build Coastguard Worker   // Still picture or not
124*77c1e3ccSAndroid Build Coastguard Worker   seq_params->still_picture = aom_rb_read_bit(rb);
125*77c1e3ccSAndroid Build Coastguard Worker   seq_params->reduced_still_picture_hdr = aom_rb_read_bit(rb);
126*77c1e3ccSAndroid Build Coastguard Worker   // Video must have reduced_still_picture_hdr = 0
127*77c1e3ccSAndroid Build Coastguard Worker   if (!seq_params->still_picture && seq_params->reduced_still_picture_hdr) {
128*77c1e3ccSAndroid Build Coastguard Worker     pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
129*77c1e3ccSAndroid Build Coastguard Worker     return 0;
130*77c1e3ccSAndroid Build Coastguard Worker   }
131*77c1e3ccSAndroid Build Coastguard Worker 
132*77c1e3ccSAndroid Build Coastguard Worker   if (seq_params->reduced_still_picture_hdr) {
133*77c1e3ccSAndroid Build Coastguard Worker     seq_params->timing_info_present = 0;
134*77c1e3ccSAndroid Build Coastguard Worker     seq_params->decoder_model_info_present_flag = 0;
135*77c1e3ccSAndroid Build Coastguard Worker     seq_params->display_model_info_present_flag = 0;
136*77c1e3ccSAndroid Build Coastguard Worker     seq_params->operating_points_cnt_minus_1 = 0;
137*77c1e3ccSAndroid Build Coastguard Worker     seq_params->operating_point_idc[0] = 0;
138*77c1e3ccSAndroid Build Coastguard Worker     seq_params->has_nonzero_operating_point_idc = false;
139*77c1e3ccSAndroid Build Coastguard Worker     if (!read_bitstream_level(&seq_params->seq_level_idx[0], rb)) {
140*77c1e3ccSAndroid Build Coastguard Worker       pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
141*77c1e3ccSAndroid Build Coastguard Worker       return 0;
142*77c1e3ccSAndroid Build Coastguard Worker     }
143*77c1e3ccSAndroid Build Coastguard Worker     seq_params->tier[0] = 0;
144*77c1e3ccSAndroid Build Coastguard Worker     seq_params->op_params[0].decoder_model_param_present_flag = 0;
145*77c1e3ccSAndroid Build Coastguard Worker     seq_params->op_params[0].display_model_param_present_flag = 0;
146*77c1e3ccSAndroid Build Coastguard Worker   } else {
147*77c1e3ccSAndroid Build Coastguard Worker     seq_params->timing_info_present = aom_rb_read_bit(rb);
148*77c1e3ccSAndroid Build Coastguard Worker     if (seq_params->timing_info_present) {
149*77c1e3ccSAndroid Build Coastguard Worker       av1_read_timing_info_header(&seq_params->timing_info, &pbi->error, rb);
150*77c1e3ccSAndroid Build Coastguard Worker 
151*77c1e3ccSAndroid Build Coastguard Worker       seq_params->decoder_model_info_present_flag = aom_rb_read_bit(rb);
152*77c1e3ccSAndroid Build Coastguard Worker       if (seq_params->decoder_model_info_present_flag)
153*77c1e3ccSAndroid Build Coastguard Worker         av1_read_decoder_model_info(&seq_params->decoder_model_info, rb);
154*77c1e3ccSAndroid Build Coastguard Worker     } else {
155*77c1e3ccSAndroid Build Coastguard Worker       seq_params->decoder_model_info_present_flag = 0;
156*77c1e3ccSAndroid Build Coastguard Worker     }
157*77c1e3ccSAndroid Build Coastguard Worker     seq_params->display_model_info_present_flag = aom_rb_read_bit(rb);
158*77c1e3ccSAndroid Build Coastguard Worker     seq_params->operating_points_cnt_minus_1 =
159*77c1e3ccSAndroid Build Coastguard Worker         aom_rb_read_literal(rb, OP_POINTS_CNT_MINUS_1_BITS);
160*77c1e3ccSAndroid Build Coastguard Worker     seq_params->has_nonzero_operating_point_idc = false;
161*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < seq_params->operating_points_cnt_minus_1 + 1; i++) {
162*77c1e3ccSAndroid Build Coastguard Worker       seq_params->operating_point_idc[i] =
163*77c1e3ccSAndroid Build Coastguard Worker           aom_rb_read_literal(rb, OP_POINTS_IDC_BITS);
164*77c1e3ccSAndroid Build Coastguard Worker       if (seq_params->operating_point_idc[i] != 0)
165*77c1e3ccSAndroid Build Coastguard Worker         seq_params->has_nonzero_operating_point_idc = true;
166*77c1e3ccSAndroid Build Coastguard Worker       if (!read_bitstream_level(&seq_params->seq_level_idx[i], rb)) {
167*77c1e3ccSAndroid Build Coastguard Worker         pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
168*77c1e3ccSAndroid Build Coastguard Worker         return 0;
169*77c1e3ccSAndroid Build Coastguard Worker       }
170*77c1e3ccSAndroid Build Coastguard Worker       // This is the seq_level_idx[i] > 7 check in the spec. seq_level_idx 7
171*77c1e3ccSAndroid Build Coastguard Worker       // is equivalent to level 3.3.
172*77c1e3ccSAndroid Build Coastguard Worker       if (seq_params->seq_level_idx[i] >= SEQ_LEVEL_4_0)
173*77c1e3ccSAndroid Build Coastguard Worker         seq_params->tier[i] = aom_rb_read_bit(rb);
174*77c1e3ccSAndroid Build Coastguard Worker       else
175*77c1e3ccSAndroid Build Coastguard Worker         seq_params->tier[i] = 0;
176*77c1e3ccSAndroid Build Coastguard Worker       if (seq_params->decoder_model_info_present_flag) {
177*77c1e3ccSAndroid Build Coastguard Worker         seq_params->op_params[i].decoder_model_param_present_flag =
178*77c1e3ccSAndroid Build Coastguard Worker             aom_rb_read_bit(rb);
179*77c1e3ccSAndroid Build Coastguard Worker         if (seq_params->op_params[i].decoder_model_param_present_flag)
180*77c1e3ccSAndroid Build Coastguard Worker           av1_read_op_parameters_info(&seq_params->op_params[i],
181*77c1e3ccSAndroid Build Coastguard Worker                                       seq_params->decoder_model_info
182*77c1e3ccSAndroid Build Coastguard Worker                                           .encoder_decoder_buffer_delay_length,
183*77c1e3ccSAndroid Build Coastguard Worker                                       rb);
184*77c1e3ccSAndroid Build Coastguard Worker       } else {
185*77c1e3ccSAndroid Build Coastguard Worker         seq_params->op_params[i].decoder_model_param_present_flag = 0;
186*77c1e3ccSAndroid Build Coastguard Worker       }
187*77c1e3ccSAndroid Build Coastguard Worker       if (seq_params->timing_info_present &&
188*77c1e3ccSAndroid Build Coastguard Worker           (seq_params->timing_info.equal_picture_interval ||
189*77c1e3ccSAndroid Build Coastguard Worker            seq_params->op_params[i].decoder_model_param_present_flag)) {
190*77c1e3ccSAndroid Build Coastguard Worker         seq_params->op_params[i].bitrate = av1_max_level_bitrate(
191*77c1e3ccSAndroid Build Coastguard Worker             seq_params->profile, seq_params->seq_level_idx[i],
192*77c1e3ccSAndroid Build Coastguard Worker             seq_params->tier[i]);
193*77c1e3ccSAndroid Build Coastguard Worker         // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass
194*77c1e3ccSAndroid Build Coastguard Worker         // the check
195*77c1e3ccSAndroid Build Coastguard Worker         if (seq_params->op_params[i].bitrate == 0)
196*77c1e3ccSAndroid Build Coastguard Worker           aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
197*77c1e3ccSAndroid Build Coastguard Worker                              "AV1 does not support this combination of "
198*77c1e3ccSAndroid Build Coastguard Worker                              "profile, level, and tier.");
199*77c1e3ccSAndroid Build Coastguard Worker         // Buffer size in bits/s is bitrate in bits/s * 1 s
200*77c1e3ccSAndroid Build Coastguard Worker         seq_params->op_params[i].buffer_size = seq_params->op_params[i].bitrate;
201*77c1e3ccSAndroid Build Coastguard Worker       }
202*77c1e3ccSAndroid Build Coastguard Worker       if (seq_params->timing_info_present &&
203*77c1e3ccSAndroid Build Coastguard Worker           seq_params->timing_info.equal_picture_interval &&
204*77c1e3ccSAndroid Build Coastguard Worker           !seq_params->op_params[i].decoder_model_param_present_flag) {
205*77c1e3ccSAndroid Build Coastguard Worker         // When the decoder_model_parameters are not sent for this op, set
206*77c1e3ccSAndroid Build Coastguard Worker         // the default ones that can be used with the resource availability mode
207*77c1e3ccSAndroid Build Coastguard Worker         seq_params->op_params[i].decoder_buffer_delay = 70000;
208*77c1e3ccSAndroid Build Coastguard Worker         seq_params->op_params[i].encoder_buffer_delay = 20000;
209*77c1e3ccSAndroid Build Coastguard Worker         seq_params->op_params[i].low_delay_mode_flag = 0;
210*77c1e3ccSAndroid Build Coastguard Worker       }
211*77c1e3ccSAndroid Build Coastguard Worker 
212*77c1e3ccSAndroid Build Coastguard Worker       if (seq_params->display_model_info_present_flag) {
213*77c1e3ccSAndroid Build Coastguard Worker         seq_params->op_params[i].display_model_param_present_flag =
214*77c1e3ccSAndroid Build Coastguard Worker             aom_rb_read_bit(rb);
215*77c1e3ccSAndroid Build Coastguard Worker         if (seq_params->op_params[i].display_model_param_present_flag) {
216*77c1e3ccSAndroid Build Coastguard Worker           seq_params->op_params[i].initial_display_delay =
217*77c1e3ccSAndroid Build Coastguard Worker               aom_rb_read_literal(rb, 4) + 1;
218*77c1e3ccSAndroid Build Coastguard Worker           if (seq_params->op_params[i].initial_display_delay > 10)
219*77c1e3ccSAndroid Build Coastguard Worker             aom_internal_error(
220*77c1e3ccSAndroid Build Coastguard Worker                 &pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
221*77c1e3ccSAndroid Build Coastguard Worker                 "AV1 does not support more than 10 decoded frames delay");
222*77c1e3ccSAndroid Build Coastguard Worker         } else {
223*77c1e3ccSAndroid Build Coastguard Worker           seq_params->op_params[i].initial_display_delay = 10;
224*77c1e3ccSAndroid Build Coastguard Worker         }
225*77c1e3ccSAndroid Build Coastguard Worker       } else {
226*77c1e3ccSAndroid Build Coastguard Worker         seq_params->op_params[i].display_model_param_present_flag = 0;
227*77c1e3ccSAndroid Build Coastguard Worker         seq_params->op_params[i].initial_display_delay = 10;
228*77c1e3ccSAndroid Build Coastguard Worker       }
229*77c1e3ccSAndroid Build Coastguard Worker     }
230*77c1e3ccSAndroid Build Coastguard Worker   }
231*77c1e3ccSAndroid Build Coastguard Worker   // This decoder supports all levels.  Choose operating point provided by
232*77c1e3ccSAndroid Build Coastguard Worker   // external means
233*77c1e3ccSAndroid Build Coastguard Worker   int operating_point = pbi->operating_point;
234*77c1e3ccSAndroid Build Coastguard Worker   if (operating_point < 0 ||
235*77c1e3ccSAndroid Build Coastguard Worker       operating_point > seq_params->operating_points_cnt_minus_1)
236*77c1e3ccSAndroid Build Coastguard Worker     operating_point = 0;
237*77c1e3ccSAndroid Build Coastguard Worker   pbi->current_operating_point =
238*77c1e3ccSAndroid Build Coastguard Worker       seq_params->operating_point_idc[operating_point];
239*77c1e3ccSAndroid Build Coastguard Worker   if (aom_get_num_layers_from_operating_point_idc(
240*77c1e3ccSAndroid Build Coastguard Worker           pbi->current_operating_point, &pbi->number_spatial_layers,
241*77c1e3ccSAndroid Build Coastguard Worker           &pbi->number_temporal_layers) != AOM_CODEC_OK) {
242*77c1e3ccSAndroid Build Coastguard Worker     pbi->error.error_code = AOM_CODEC_ERROR;
243*77c1e3ccSAndroid Build Coastguard Worker     return 0;
244*77c1e3ccSAndroid Build Coastguard Worker   }
245*77c1e3ccSAndroid Build Coastguard Worker 
246*77c1e3ccSAndroid Build Coastguard Worker   av1_read_sequence_header(cm, rb, seq_params);
247*77c1e3ccSAndroid Build Coastguard Worker 
248*77c1e3ccSAndroid Build Coastguard Worker   av1_read_color_config(rb, pbi->allow_lowbitdepth, seq_params, &pbi->error);
249*77c1e3ccSAndroid Build Coastguard Worker   if (!(seq_params->subsampling_x == 0 && seq_params->subsampling_y == 0) &&
250*77c1e3ccSAndroid Build Coastguard Worker       !(seq_params->subsampling_x == 1 && seq_params->subsampling_y == 1) &&
251*77c1e3ccSAndroid Build Coastguard Worker       !(seq_params->subsampling_x == 1 && seq_params->subsampling_y == 0)) {
252*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
253*77c1e3ccSAndroid Build Coastguard Worker                        "Only 4:4:4, 4:2:2 and 4:2:0 are currently supported, "
254*77c1e3ccSAndroid Build Coastguard Worker                        "%d %d subsampling is not supported.\n",
255*77c1e3ccSAndroid Build Coastguard Worker                        seq_params->subsampling_x, seq_params->subsampling_y);
256*77c1e3ccSAndroid Build Coastguard Worker   }
257*77c1e3ccSAndroid Build Coastguard Worker 
258*77c1e3ccSAndroid Build Coastguard Worker   seq_params->film_grain_params_present = aom_rb_read_bit(rb);
259*77c1e3ccSAndroid Build Coastguard Worker 
260*77c1e3ccSAndroid Build Coastguard Worker   if (av1_check_trailing_bits(pbi, rb) != 0) {
261*77c1e3ccSAndroid Build Coastguard Worker     // pbi->error.error_code is already set.
262*77c1e3ccSAndroid Build Coastguard Worker     return 0;
263*77c1e3ccSAndroid Build Coastguard Worker   }
264*77c1e3ccSAndroid Build Coastguard Worker 
265*77c1e3ccSAndroid Build Coastguard Worker   // If a sequence header has been decoded before, we check if the new
266*77c1e3ccSAndroid Build Coastguard Worker   // one is consistent with the old one.
267*77c1e3ccSAndroid Build Coastguard Worker   if (pbi->sequence_header_ready) {
268*77c1e3ccSAndroid Build Coastguard Worker     if (!are_seq_headers_consistent(cm->seq_params, seq_params))
269*77c1e3ccSAndroid Build Coastguard Worker       pbi->sequence_header_changed = 1;
270*77c1e3ccSAndroid Build Coastguard Worker   }
271*77c1e3ccSAndroid Build Coastguard Worker 
272*77c1e3ccSAndroid Build Coastguard Worker   *cm->seq_params = *seq_params;
273*77c1e3ccSAndroid Build Coastguard Worker   pbi->sequence_header_ready = 1;
274*77c1e3ccSAndroid Build Coastguard Worker 
275*77c1e3ccSAndroid Build Coastguard Worker   return ((rb->bit_offset - saved_bit_offset + 7) >> 3);
276*77c1e3ccSAndroid Build Coastguard Worker }
277*77c1e3ccSAndroid Build Coastguard Worker 
278*77c1e3ccSAndroid Build Coastguard Worker // On success, returns the frame header size. On failure, calls
279*77c1e3ccSAndroid Build Coastguard Worker // aom_internal_error and does not return. If show existing frame,
280*77c1e3ccSAndroid Build Coastguard Worker // also marks the data processing to end after the frame header.
read_frame_header_obu(AV1Decoder * pbi,struct aom_read_bit_buffer * rb,const uint8_t * data,const uint8_t ** p_data_end,int trailing_bits_present)281*77c1e3ccSAndroid Build Coastguard Worker static uint32_t read_frame_header_obu(AV1Decoder *pbi,
282*77c1e3ccSAndroid Build Coastguard Worker                                       struct aom_read_bit_buffer *rb,
283*77c1e3ccSAndroid Build Coastguard Worker                                       const uint8_t *data,
284*77c1e3ccSAndroid Build Coastguard Worker                                       const uint8_t **p_data_end,
285*77c1e3ccSAndroid Build Coastguard Worker                                       int trailing_bits_present) {
286*77c1e3ccSAndroid Build Coastguard Worker   const uint32_t hdr_size =
287*77c1e3ccSAndroid Build Coastguard Worker       av1_decode_frame_headers_and_setup(pbi, rb, trailing_bits_present);
288*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *cm = &pbi->common;
289*77c1e3ccSAndroid Build Coastguard Worker   if (cm->show_existing_frame) {
290*77c1e3ccSAndroid Build Coastguard Worker     *p_data_end = data + hdr_size;
291*77c1e3ccSAndroid Build Coastguard Worker   }
292*77c1e3ccSAndroid Build Coastguard Worker   return hdr_size;
293*77c1e3ccSAndroid Build Coastguard Worker }
294*77c1e3ccSAndroid Build Coastguard Worker 
295*77c1e3ccSAndroid Build Coastguard Worker // On success, returns the tile group header size. On failure, calls
296*77c1e3ccSAndroid Build Coastguard Worker // aom_internal_error() and returns -1.
read_tile_group_header(AV1Decoder * pbi,struct aom_read_bit_buffer * rb,int * start_tile,int * end_tile,int tile_start_implicit)297*77c1e3ccSAndroid Build Coastguard Worker static int32_t read_tile_group_header(AV1Decoder *pbi,
298*77c1e3ccSAndroid Build Coastguard Worker                                       struct aom_read_bit_buffer *rb,
299*77c1e3ccSAndroid Build Coastguard Worker                                       int *start_tile, int *end_tile,
300*77c1e3ccSAndroid Build Coastguard Worker                                       int tile_start_implicit) {
301*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &pbi->common;
302*77c1e3ccSAndroid Build Coastguard Worker   CommonTileParams *const tiles = &cm->tiles;
303*77c1e3ccSAndroid Build Coastguard Worker   uint32_t saved_bit_offset = rb->bit_offset;
304*77c1e3ccSAndroid Build Coastguard Worker   int tile_start_and_end_present_flag = 0;
305*77c1e3ccSAndroid Build Coastguard Worker   const int num_tiles = tiles->rows * tiles->cols;
306*77c1e3ccSAndroid Build Coastguard Worker 
307*77c1e3ccSAndroid Build Coastguard Worker   if (!tiles->large_scale && num_tiles > 1) {
308*77c1e3ccSAndroid Build Coastguard Worker     tile_start_and_end_present_flag = aom_rb_read_bit(rb);
309*77c1e3ccSAndroid Build Coastguard Worker     if (tile_start_implicit && tile_start_and_end_present_flag) {
310*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(
311*77c1e3ccSAndroid Build Coastguard Worker           &pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
312*77c1e3ccSAndroid Build Coastguard Worker           "For OBU_FRAME type obu tile_start_and_end_present_flag must be 0");
313*77c1e3ccSAndroid Build Coastguard Worker       return -1;
314*77c1e3ccSAndroid Build Coastguard Worker     }
315*77c1e3ccSAndroid Build Coastguard Worker   }
316*77c1e3ccSAndroid Build Coastguard Worker   if (tiles->large_scale || num_tiles == 1 ||
317*77c1e3ccSAndroid Build Coastguard Worker       !tile_start_and_end_present_flag) {
318*77c1e3ccSAndroid Build Coastguard Worker     *start_tile = 0;
319*77c1e3ccSAndroid Build Coastguard Worker     *end_tile = num_tiles - 1;
320*77c1e3ccSAndroid Build Coastguard Worker   } else {
321*77c1e3ccSAndroid Build Coastguard Worker     int tile_bits = tiles->log2_rows + tiles->log2_cols;
322*77c1e3ccSAndroid Build Coastguard Worker     *start_tile = aom_rb_read_literal(rb, tile_bits);
323*77c1e3ccSAndroid Build Coastguard Worker     *end_tile = aom_rb_read_literal(rb, tile_bits);
324*77c1e3ccSAndroid Build Coastguard Worker   }
325*77c1e3ccSAndroid Build Coastguard Worker   if (*start_tile != pbi->next_start_tile) {
326*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
327*77c1e3ccSAndroid Build Coastguard Worker                        "tg_start (%d) must be equal to %d", *start_tile,
328*77c1e3ccSAndroid Build Coastguard Worker                        pbi->next_start_tile);
329*77c1e3ccSAndroid Build Coastguard Worker     return -1;
330*77c1e3ccSAndroid Build Coastguard Worker   }
331*77c1e3ccSAndroid Build Coastguard Worker   if (*start_tile > *end_tile) {
332*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(
333*77c1e3ccSAndroid Build Coastguard Worker         &pbi->error, AOM_CODEC_CORRUPT_FRAME,
334*77c1e3ccSAndroid Build Coastguard Worker         "tg_end (%d) must be greater than or equal to tg_start (%d)", *end_tile,
335*77c1e3ccSAndroid Build Coastguard Worker         *start_tile);
336*77c1e3ccSAndroid Build Coastguard Worker     return -1;
337*77c1e3ccSAndroid Build Coastguard Worker   }
338*77c1e3ccSAndroid Build Coastguard Worker   if (*end_tile >= num_tiles) {
339*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
340*77c1e3ccSAndroid Build Coastguard Worker                        "tg_end (%d) must be less than NumTiles (%d)", *end_tile,
341*77c1e3ccSAndroid Build Coastguard Worker                        num_tiles);
342*77c1e3ccSAndroid Build Coastguard Worker     return -1;
343*77c1e3ccSAndroid Build Coastguard Worker   }
344*77c1e3ccSAndroid Build Coastguard Worker   pbi->next_start_tile = (*end_tile == num_tiles - 1) ? 0 : *end_tile + 1;
345*77c1e3ccSAndroid Build Coastguard Worker 
346*77c1e3ccSAndroid Build Coastguard Worker   return ((rb->bit_offset - saved_bit_offset + 7) >> 3);
347*77c1e3ccSAndroid Build Coastguard Worker }
348*77c1e3ccSAndroid Build Coastguard Worker 
349*77c1e3ccSAndroid Build Coastguard Worker // On success, returns the tile group OBU size. On failure, sets
350*77c1e3ccSAndroid Build Coastguard Worker // pbi->common.error.error_code and returns 0.
read_one_tile_group_obu(AV1Decoder * pbi,struct aom_read_bit_buffer * rb,int is_first_tg,const uint8_t * data,const uint8_t * data_end,const uint8_t ** p_data_end,int * is_last_tg,int tile_start_implicit)351*77c1e3ccSAndroid Build Coastguard Worker static uint32_t read_one_tile_group_obu(
352*77c1e3ccSAndroid Build Coastguard Worker     AV1Decoder *pbi, struct aom_read_bit_buffer *rb, int is_first_tg,
353*77c1e3ccSAndroid Build Coastguard Worker     const uint8_t *data, const uint8_t *data_end, const uint8_t **p_data_end,
354*77c1e3ccSAndroid Build Coastguard Worker     int *is_last_tg, int tile_start_implicit) {
355*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &pbi->common;
356*77c1e3ccSAndroid Build Coastguard Worker   int start_tile, end_tile;
357*77c1e3ccSAndroid Build Coastguard Worker   int32_t header_size, tg_payload_size;
358*77c1e3ccSAndroid Build Coastguard Worker 
359*77c1e3ccSAndroid Build Coastguard Worker   assert((rb->bit_offset & 7) == 0);
360*77c1e3ccSAndroid Build Coastguard Worker   assert(rb->bit_buffer + aom_rb_bytes_read(rb) == data);
361*77c1e3ccSAndroid Build Coastguard Worker 
362*77c1e3ccSAndroid Build Coastguard Worker   header_size = read_tile_group_header(pbi, rb, &start_tile, &end_tile,
363*77c1e3ccSAndroid Build Coastguard Worker                                        tile_start_implicit);
364*77c1e3ccSAndroid Build Coastguard Worker   if (header_size == -1 || byte_alignment(cm, rb)) return 0;
365*77c1e3ccSAndroid Build Coastguard Worker   data += header_size;
366*77c1e3ccSAndroid Build Coastguard Worker   av1_decode_tg_tiles_and_wrapup(pbi, data, data_end, p_data_end, start_tile,
367*77c1e3ccSAndroid Build Coastguard Worker                                  end_tile, is_first_tg);
368*77c1e3ccSAndroid Build Coastguard Worker 
369*77c1e3ccSAndroid Build Coastguard Worker   tg_payload_size = (uint32_t)(*p_data_end - data);
370*77c1e3ccSAndroid Build Coastguard Worker 
371*77c1e3ccSAndroid Build Coastguard Worker   *is_last_tg = end_tile == cm->tiles.rows * cm->tiles.cols - 1;
372*77c1e3ccSAndroid Build Coastguard Worker   return header_size + tg_payload_size;
373*77c1e3ccSAndroid Build Coastguard Worker }
374*77c1e3ccSAndroid Build Coastguard Worker 
alloc_tile_list_buffer(AV1Decoder * pbi,int tile_width_in_pixels,int tile_height_in_pixels)375*77c1e3ccSAndroid Build Coastguard Worker static void alloc_tile_list_buffer(AV1Decoder *pbi, int tile_width_in_pixels,
376*77c1e3ccSAndroid Build Coastguard Worker                                    int tile_height_in_pixels) {
377*77c1e3ccSAndroid Build Coastguard Worker   // The resolution of the output frame is read out from the bitstream. The data
378*77c1e3ccSAndroid Build Coastguard Worker   // are stored in the order of Y plane, U plane and V plane. As an example, for
379*77c1e3ccSAndroid Build Coastguard Worker   // image format 4:2:0, the output frame of U plane and V plane is 1/4 of the
380*77c1e3ccSAndroid Build Coastguard Worker   // output frame.
381*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &pbi->common;
382*77c1e3ccSAndroid Build Coastguard Worker   const int output_frame_width =
383*77c1e3ccSAndroid Build Coastguard Worker       (pbi->output_frame_width_in_tiles_minus_1 + 1) * tile_width_in_pixels;
384*77c1e3ccSAndroid Build Coastguard Worker   const int output_frame_height =
385*77c1e3ccSAndroid Build Coastguard Worker       (pbi->output_frame_height_in_tiles_minus_1 + 1) * tile_height_in_pixels;
386*77c1e3ccSAndroid Build Coastguard Worker   // The output frame is used to store the decoded tile list. The decoded tile
387*77c1e3ccSAndroid Build Coastguard Worker   // list has to fit into 1 output frame.
388*77c1e3ccSAndroid Build Coastguard Worker   assert((pbi->tile_count_minus_1 + 1) <=
389*77c1e3ccSAndroid Build Coastguard Worker          (pbi->output_frame_width_in_tiles_minus_1 + 1) *
390*77c1e3ccSAndroid Build Coastguard Worker              (pbi->output_frame_height_in_tiles_minus_1 + 1));
391*77c1e3ccSAndroid Build Coastguard Worker 
392*77c1e3ccSAndroid Build Coastguard Worker   // Allocate the tile list output buffer.
393*77c1e3ccSAndroid Build Coastguard Worker   // Note: if cm->seq_params->use_highbitdepth is 1 and
394*77c1e3ccSAndroid Build Coastguard Worker   // cm->seq_params->bit_depth is 8, we could allocate less memory, namely, 8
395*77c1e3ccSAndroid Build Coastguard Worker   // bits/pixel.
396*77c1e3ccSAndroid Build Coastguard Worker   if (aom_alloc_frame_buffer(&pbi->tile_list_outbuf, output_frame_width,
397*77c1e3ccSAndroid Build Coastguard Worker                              output_frame_height, cm->seq_params->subsampling_x,
398*77c1e3ccSAndroid Build Coastguard Worker                              cm->seq_params->subsampling_y,
399*77c1e3ccSAndroid Build Coastguard Worker                              (cm->seq_params->use_highbitdepth &&
400*77c1e3ccSAndroid Build Coastguard Worker                               (cm->seq_params->bit_depth > AOM_BITS_8)),
401*77c1e3ccSAndroid Build Coastguard Worker                              0, cm->features.byte_alignment, false, 0))
402*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
403*77c1e3ccSAndroid Build Coastguard Worker                        "Failed to allocate the tile list output buffer");
404*77c1e3ccSAndroid Build Coastguard Worker }
405*77c1e3ccSAndroid Build Coastguard Worker 
yv12_tile_copy(const YV12_BUFFER_CONFIG * src,int hstart1,int hend1,int vstart1,int vend1,YV12_BUFFER_CONFIG * dst,int hstart2,int vstart2,int plane)406*77c1e3ccSAndroid Build Coastguard Worker static void yv12_tile_copy(const YV12_BUFFER_CONFIG *src, int hstart1,
407*77c1e3ccSAndroid Build Coastguard Worker                            int hend1, int vstart1, int vend1,
408*77c1e3ccSAndroid Build Coastguard Worker                            YV12_BUFFER_CONFIG *dst, int hstart2, int vstart2,
409*77c1e3ccSAndroid Build Coastguard Worker                            int plane) {
410*77c1e3ccSAndroid Build Coastguard Worker   const int src_stride = (plane > 0) ? src->strides[1] : src->strides[0];
411*77c1e3ccSAndroid Build Coastguard Worker   const int dst_stride = (plane > 0) ? dst->strides[1] : dst->strides[0];
412*77c1e3ccSAndroid Build Coastguard Worker   int row, col;
413*77c1e3ccSAndroid Build Coastguard Worker 
414*77c1e3ccSAndroid Build Coastguard Worker   assert(src->flags & YV12_FLAG_HIGHBITDEPTH);
415*77c1e3ccSAndroid Build Coastguard Worker   assert(!(dst->flags & YV12_FLAG_HIGHBITDEPTH));
416*77c1e3ccSAndroid Build Coastguard Worker 
417*77c1e3ccSAndroid Build Coastguard Worker   const uint16_t *src16 =
418*77c1e3ccSAndroid Build Coastguard Worker       CONVERT_TO_SHORTPTR(src->buffers[plane] + vstart1 * src_stride + hstart1);
419*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *dst8 = dst->buffers[plane] + vstart2 * dst_stride + hstart2;
420*77c1e3ccSAndroid Build Coastguard Worker 
421*77c1e3ccSAndroid Build Coastguard Worker   for (row = vstart1; row < vend1; ++row) {
422*77c1e3ccSAndroid Build Coastguard Worker     for (col = 0; col < (hend1 - hstart1); ++col) *dst8++ = (uint8_t)(*src16++);
423*77c1e3ccSAndroid Build Coastguard Worker     src16 += src_stride - (hend1 - hstart1);
424*77c1e3ccSAndroid Build Coastguard Worker     dst8 += dst_stride - (hend1 - hstart1);
425*77c1e3ccSAndroid Build Coastguard Worker   }
426*77c1e3ccSAndroid Build Coastguard Worker   return;
427*77c1e3ccSAndroid Build Coastguard Worker }
428*77c1e3ccSAndroid Build Coastguard Worker 
copy_decoded_tile_to_tile_list_buffer(AV1Decoder * pbi,int tile_idx,int tile_width_in_pixels,int tile_height_in_pixels)429*77c1e3ccSAndroid Build Coastguard Worker static void copy_decoded_tile_to_tile_list_buffer(AV1Decoder *pbi, int tile_idx,
430*77c1e3ccSAndroid Build Coastguard Worker                                                   int tile_width_in_pixels,
431*77c1e3ccSAndroid Build Coastguard Worker                                                   int tile_height_in_pixels) {
432*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &pbi->common;
433*77c1e3ccSAndroid Build Coastguard Worker   const int ssy = cm->seq_params->subsampling_y;
434*77c1e3ccSAndroid Build Coastguard Worker   const int ssx = cm->seq_params->subsampling_x;
435*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
436*77c1e3ccSAndroid Build Coastguard Worker 
437*77c1e3ccSAndroid Build Coastguard Worker   YV12_BUFFER_CONFIG *cur_frame = &cm->cur_frame->buf;
438*77c1e3ccSAndroid Build Coastguard Worker   const int tr = tile_idx / (pbi->output_frame_width_in_tiles_minus_1 + 1);
439*77c1e3ccSAndroid Build Coastguard Worker   const int tc = tile_idx % (pbi->output_frame_width_in_tiles_minus_1 + 1);
440*77c1e3ccSAndroid Build Coastguard Worker   int plane;
441*77c1e3ccSAndroid Build Coastguard Worker 
442*77c1e3ccSAndroid Build Coastguard Worker   // Copy decoded tile to the tile list output buffer.
443*77c1e3ccSAndroid Build Coastguard Worker   for (plane = 0; plane < num_planes; ++plane) {
444*77c1e3ccSAndroid Build Coastguard Worker     const int shift_x = plane > 0 ? ssx : 0;
445*77c1e3ccSAndroid Build Coastguard Worker     const int shift_y = plane > 0 ? ssy : 0;
446*77c1e3ccSAndroid Build Coastguard Worker     const int h = tile_height_in_pixels >> shift_y;
447*77c1e3ccSAndroid Build Coastguard Worker     const int w = tile_width_in_pixels >> shift_x;
448*77c1e3ccSAndroid Build Coastguard Worker 
449*77c1e3ccSAndroid Build Coastguard Worker     // src offset
450*77c1e3ccSAndroid Build Coastguard Worker     int vstart1 = pbi->dec_tile_row * h;
451*77c1e3ccSAndroid Build Coastguard Worker     int vend1 = vstart1 + h;
452*77c1e3ccSAndroid Build Coastguard Worker     int hstart1 = pbi->dec_tile_col * w;
453*77c1e3ccSAndroid Build Coastguard Worker     int hend1 = hstart1 + w;
454*77c1e3ccSAndroid Build Coastguard Worker     // dst offset
455*77c1e3ccSAndroid Build Coastguard Worker     int vstart2 = tr * h;
456*77c1e3ccSAndroid Build Coastguard Worker     int hstart2 = tc * w;
457*77c1e3ccSAndroid Build Coastguard Worker 
458*77c1e3ccSAndroid Build Coastguard Worker     if (cm->seq_params->use_highbitdepth &&
459*77c1e3ccSAndroid Build Coastguard Worker         cm->seq_params->bit_depth == AOM_BITS_8) {
460*77c1e3ccSAndroid Build Coastguard Worker       yv12_tile_copy(cur_frame, hstart1, hend1, vstart1, vend1,
461*77c1e3ccSAndroid Build Coastguard Worker                      &pbi->tile_list_outbuf, hstart2, vstart2, plane);
462*77c1e3ccSAndroid Build Coastguard Worker     } else {
463*77c1e3ccSAndroid Build Coastguard Worker       switch (plane) {
464*77c1e3ccSAndroid Build Coastguard Worker         case 0:
465*77c1e3ccSAndroid Build Coastguard Worker           aom_yv12_partial_copy_y(cur_frame, hstart1, hend1, vstart1, vend1,
466*77c1e3ccSAndroid Build Coastguard Worker                                   &pbi->tile_list_outbuf, hstart2, vstart2);
467*77c1e3ccSAndroid Build Coastguard Worker           break;
468*77c1e3ccSAndroid Build Coastguard Worker         case 1:
469*77c1e3ccSAndroid Build Coastguard Worker           aom_yv12_partial_copy_u(cur_frame, hstart1, hend1, vstart1, vend1,
470*77c1e3ccSAndroid Build Coastguard Worker                                   &pbi->tile_list_outbuf, hstart2, vstart2);
471*77c1e3ccSAndroid Build Coastguard Worker           break;
472*77c1e3ccSAndroid Build Coastguard Worker         case 2:
473*77c1e3ccSAndroid Build Coastguard Worker           aom_yv12_partial_copy_v(cur_frame, hstart1, hend1, vstart1, vend1,
474*77c1e3ccSAndroid Build Coastguard Worker                                   &pbi->tile_list_outbuf, hstart2, vstart2);
475*77c1e3ccSAndroid Build Coastguard Worker           break;
476*77c1e3ccSAndroid Build Coastguard Worker         default: assert(0);
477*77c1e3ccSAndroid Build Coastguard Worker       }
478*77c1e3ccSAndroid Build Coastguard Worker     }
479*77c1e3ccSAndroid Build Coastguard Worker   }
480*77c1e3ccSAndroid Build Coastguard Worker }
481*77c1e3ccSAndroid Build Coastguard Worker 
482*77c1e3ccSAndroid Build Coastguard Worker // Only called while large_scale_tile = 1.
483*77c1e3ccSAndroid Build Coastguard Worker //
484*77c1e3ccSAndroid Build Coastguard Worker // On success, returns the tile list OBU size. On failure, sets
485*77c1e3ccSAndroid Build Coastguard Worker // pbi->common.error.error_code and returns 0.
read_and_decode_one_tile_list(AV1Decoder * pbi,struct aom_read_bit_buffer * rb,const uint8_t * data,const uint8_t * data_end,const uint8_t ** p_data_end,int * frame_decoding_finished)486*77c1e3ccSAndroid Build Coastguard Worker static uint32_t read_and_decode_one_tile_list(AV1Decoder *pbi,
487*77c1e3ccSAndroid Build Coastguard Worker                                               struct aom_read_bit_buffer *rb,
488*77c1e3ccSAndroid Build Coastguard Worker                                               const uint8_t *data,
489*77c1e3ccSAndroid Build Coastguard Worker                                               const uint8_t *data_end,
490*77c1e3ccSAndroid Build Coastguard Worker                                               const uint8_t **p_data_end,
491*77c1e3ccSAndroid Build Coastguard Worker                                               int *frame_decoding_finished) {
492*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &pbi->common;
493*77c1e3ccSAndroid Build Coastguard Worker   uint32_t tile_list_payload_size = 0;
494*77c1e3ccSAndroid Build Coastguard Worker   const int num_tiles = cm->tiles.cols * cm->tiles.rows;
495*77c1e3ccSAndroid Build Coastguard Worker   const int start_tile = 0;
496*77c1e3ccSAndroid Build Coastguard Worker   const int end_tile = num_tiles - 1;
497*77c1e3ccSAndroid Build Coastguard Worker   int i = 0;
498*77c1e3ccSAndroid Build Coastguard Worker 
499*77c1e3ccSAndroid Build Coastguard Worker   // Process the tile list info.
500*77c1e3ccSAndroid Build Coastguard Worker   pbi->output_frame_width_in_tiles_minus_1 = aom_rb_read_literal(rb, 8);
501*77c1e3ccSAndroid Build Coastguard Worker   pbi->output_frame_height_in_tiles_minus_1 = aom_rb_read_literal(rb, 8);
502*77c1e3ccSAndroid Build Coastguard Worker   pbi->tile_count_minus_1 = aom_rb_read_literal(rb, 16);
503*77c1e3ccSAndroid Build Coastguard Worker 
504*77c1e3ccSAndroid Build Coastguard Worker   // The output frame is used to store the decoded tile list. The decoded tile
505*77c1e3ccSAndroid Build Coastguard Worker   // list has to fit into 1 output frame.
506*77c1e3ccSAndroid Build Coastguard Worker   if ((pbi->tile_count_minus_1 + 1) >
507*77c1e3ccSAndroid Build Coastguard Worker       (pbi->output_frame_width_in_tiles_minus_1 + 1) *
508*77c1e3ccSAndroid Build Coastguard Worker           (pbi->output_frame_height_in_tiles_minus_1 + 1)) {
509*77c1e3ccSAndroid Build Coastguard Worker     pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
510*77c1e3ccSAndroid Build Coastguard Worker     return 0;
511*77c1e3ccSAndroid Build Coastguard Worker   }
512*77c1e3ccSAndroid Build Coastguard Worker 
513*77c1e3ccSAndroid Build Coastguard Worker   if (pbi->tile_count_minus_1 > MAX_TILES - 1) {
514*77c1e3ccSAndroid Build Coastguard Worker     pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
515*77c1e3ccSAndroid Build Coastguard Worker     return 0;
516*77c1e3ccSAndroid Build Coastguard Worker   }
517*77c1e3ccSAndroid Build Coastguard Worker 
518*77c1e3ccSAndroid Build Coastguard Worker   int tile_width, tile_height;
519*77c1e3ccSAndroid Build Coastguard Worker   if (!av1_get_uniform_tile_size(cm, &tile_width, &tile_height)) {
520*77c1e3ccSAndroid Build Coastguard Worker     pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
521*77c1e3ccSAndroid Build Coastguard Worker     return 0;
522*77c1e3ccSAndroid Build Coastguard Worker   }
523*77c1e3ccSAndroid Build Coastguard Worker   const int tile_width_in_pixels = tile_width * MI_SIZE;
524*77c1e3ccSAndroid Build Coastguard Worker   const int tile_height_in_pixels = tile_height * MI_SIZE;
525*77c1e3ccSAndroid Build Coastguard Worker 
526*77c1e3ccSAndroid Build Coastguard Worker   // Allocate output frame buffer for the tile list.
527*77c1e3ccSAndroid Build Coastguard Worker   alloc_tile_list_buffer(pbi, tile_width_in_pixels, tile_height_in_pixels);
528*77c1e3ccSAndroid Build Coastguard Worker 
529*77c1e3ccSAndroid Build Coastguard Worker   uint32_t tile_list_info_bytes = 4;
530*77c1e3ccSAndroid Build Coastguard Worker   tile_list_payload_size += tile_list_info_bytes;
531*77c1e3ccSAndroid Build Coastguard Worker   data += tile_list_info_bytes;
532*77c1e3ccSAndroid Build Coastguard Worker 
533*77c1e3ccSAndroid Build Coastguard Worker   int tile_idx = 0;
534*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i <= pbi->tile_count_minus_1; i++) {
535*77c1e3ccSAndroid Build Coastguard Worker     // Process 1 tile.
536*77c1e3ccSAndroid Build Coastguard Worker     // Reset the bit reader.
537*77c1e3ccSAndroid Build Coastguard Worker     rb->bit_offset = 0;
538*77c1e3ccSAndroid Build Coastguard Worker     rb->bit_buffer = data;
539*77c1e3ccSAndroid Build Coastguard Worker 
540*77c1e3ccSAndroid Build Coastguard Worker     // Read out the tile info.
541*77c1e3ccSAndroid Build Coastguard Worker     uint32_t tile_info_bytes = 5;
542*77c1e3ccSAndroid Build Coastguard Worker     // Set reference for each tile.
543*77c1e3ccSAndroid Build Coastguard Worker     int ref_idx = aom_rb_read_literal(rb, 8);
544*77c1e3ccSAndroid Build Coastguard Worker     if (ref_idx >= MAX_EXTERNAL_REFERENCES) {
545*77c1e3ccSAndroid Build Coastguard Worker       pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
546*77c1e3ccSAndroid Build Coastguard Worker       return 0;
547*77c1e3ccSAndroid Build Coastguard Worker     }
548*77c1e3ccSAndroid Build Coastguard Worker     av1_set_reference_dec(cm, cm->remapped_ref_idx[0], 1,
549*77c1e3ccSAndroid Build Coastguard Worker                           &pbi->ext_refs.refs[ref_idx]);
550*77c1e3ccSAndroid Build Coastguard Worker 
551*77c1e3ccSAndroid Build Coastguard Worker     pbi->dec_tile_row = aom_rb_read_literal(rb, 8);
552*77c1e3ccSAndroid Build Coastguard Worker     pbi->dec_tile_col = aom_rb_read_literal(rb, 8);
553*77c1e3ccSAndroid Build Coastguard Worker     if (pbi->dec_tile_row < 0 || pbi->dec_tile_col < 0 ||
554*77c1e3ccSAndroid Build Coastguard Worker         pbi->dec_tile_row >= cm->tiles.rows ||
555*77c1e3ccSAndroid Build Coastguard Worker         pbi->dec_tile_col >= cm->tiles.cols) {
556*77c1e3ccSAndroid Build Coastguard Worker       pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
557*77c1e3ccSAndroid Build Coastguard Worker       return 0;
558*77c1e3ccSAndroid Build Coastguard Worker     }
559*77c1e3ccSAndroid Build Coastguard Worker 
560*77c1e3ccSAndroid Build Coastguard Worker     pbi->coded_tile_data_size = aom_rb_read_literal(rb, 16) + 1;
561*77c1e3ccSAndroid Build Coastguard Worker     data += tile_info_bytes;
562*77c1e3ccSAndroid Build Coastguard Worker     if ((size_t)(data_end - data) < pbi->coded_tile_data_size) {
563*77c1e3ccSAndroid Build Coastguard Worker       pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
564*77c1e3ccSAndroid Build Coastguard Worker       return 0;
565*77c1e3ccSAndroid Build Coastguard Worker     }
566*77c1e3ccSAndroid Build Coastguard Worker 
567*77c1e3ccSAndroid Build Coastguard Worker     av1_decode_tg_tiles_and_wrapup(pbi, data, data + pbi->coded_tile_data_size,
568*77c1e3ccSAndroid Build Coastguard Worker                                    p_data_end, start_tile, end_tile, 0);
569*77c1e3ccSAndroid Build Coastguard Worker     uint32_t tile_payload_size = (uint32_t)(*p_data_end - data);
570*77c1e3ccSAndroid Build Coastguard Worker 
571*77c1e3ccSAndroid Build Coastguard Worker     tile_list_payload_size += tile_info_bytes + tile_payload_size;
572*77c1e3ccSAndroid Build Coastguard Worker 
573*77c1e3ccSAndroid Build Coastguard Worker     // Update data ptr for next tile decoding.
574*77c1e3ccSAndroid Build Coastguard Worker     data = *p_data_end;
575*77c1e3ccSAndroid Build Coastguard Worker     assert(data <= data_end);
576*77c1e3ccSAndroid Build Coastguard Worker 
577*77c1e3ccSAndroid Build Coastguard Worker     // Copy the decoded tile to the tile list output buffer.
578*77c1e3ccSAndroid Build Coastguard Worker     copy_decoded_tile_to_tile_list_buffer(pbi, tile_idx, tile_width_in_pixels,
579*77c1e3ccSAndroid Build Coastguard Worker                                           tile_height_in_pixels);
580*77c1e3ccSAndroid Build Coastguard Worker     tile_idx++;
581*77c1e3ccSAndroid Build Coastguard Worker   }
582*77c1e3ccSAndroid Build Coastguard Worker 
583*77c1e3ccSAndroid Build Coastguard Worker   *frame_decoding_finished = 1;
584*77c1e3ccSAndroid Build Coastguard Worker   return tile_list_payload_size;
585*77c1e3ccSAndroid Build Coastguard Worker }
586*77c1e3ccSAndroid Build Coastguard Worker 
587*77c1e3ccSAndroid Build Coastguard Worker // Returns the last nonzero byte index in 'data'. If there is no nonzero byte in
588*77c1e3ccSAndroid Build Coastguard Worker // 'data', returns -1.
get_last_nonzero_byte_index(const uint8_t * data,size_t sz)589*77c1e3ccSAndroid Build Coastguard Worker static int get_last_nonzero_byte_index(const uint8_t *data, size_t sz) {
590*77c1e3ccSAndroid Build Coastguard Worker   // Scan backward and return on the first nonzero byte.
591*77c1e3ccSAndroid Build Coastguard Worker   int i = (int)sz - 1;
592*77c1e3ccSAndroid Build Coastguard Worker   while (i >= 0 && data[i] == 0) {
593*77c1e3ccSAndroid Build Coastguard Worker     --i;
594*77c1e3ccSAndroid Build Coastguard Worker   }
595*77c1e3ccSAndroid Build Coastguard Worker   return i;
596*77c1e3ccSAndroid Build Coastguard Worker }
597*77c1e3ccSAndroid Build Coastguard Worker 
598*77c1e3ccSAndroid Build Coastguard Worker // Allocates metadata that was read and adds it to the decoders metadata array.
alloc_read_metadata(AV1Decoder * const pbi,OBU_METADATA_TYPE metadata_type,const uint8_t * data,size_t sz,aom_metadata_insert_flags_t insert_flag)599*77c1e3ccSAndroid Build Coastguard Worker static void alloc_read_metadata(AV1Decoder *const pbi,
600*77c1e3ccSAndroid Build Coastguard Worker                                 OBU_METADATA_TYPE metadata_type,
601*77c1e3ccSAndroid Build Coastguard Worker                                 const uint8_t *data, size_t sz,
602*77c1e3ccSAndroid Build Coastguard Worker                                 aom_metadata_insert_flags_t insert_flag) {
603*77c1e3ccSAndroid Build Coastguard Worker   if (!pbi->metadata) {
604*77c1e3ccSAndroid Build Coastguard Worker     pbi->metadata = aom_img_metadata_array_alloc(0);
605*77c1e3ccSAndroid Build Coastguard Worker     if (!pbi->metadata) {
606*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
607*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate metadata array");
608*77c1e3ccSAndroid Build Coastguard Worker     }
609*77c1e3ccSAndroid Build Coastguard Worker   }
610*77c1e3ccSAndroid Build Coastguard Worker   aom_metadata_t *metadata =
611*77c1e3ccSAndroid Build Coastguard Worker       aom_img_metadata_alloc(metadata_type, data, sz, insert_flag);
612*77c1e3ccSAndroid Build Coastguard Worker   if (!metadata) {
613*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
614*77c1e3ccSAndroid Build Coastguard Worker                        "Error allocating metadata");
615*77c1e3ccSAndroid Build Coastguard Worker   }
616*77c1e3ccSAndroid Build Coastguard Worker   aom_metadata_t **metadata_array =
617*77c1e3ccSAndroid Build Coastguard Worker       (aom_metadata_t **)realloc(pbi->metadata->metadata_array,
618*77c1e3ccSAndroid Build Coastguard Worker                                  (pbi->metadata->sz + 1) * sizeof(metadata));
619*77c1e3ccSAndroid Build Coastguard Worker   if (!metadata_array) {
620*77c1e3ccSAndroid Build Coastguard Worker     aom_img_metadata_free(metadata);
621*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
622*77c1e3ccSAndroid Build Coastguard Worker                        "Error growing metadata array");
623*77c1e3ccSAndroid Build Coastguard Worker   }
624*77c1e3ccSAndroid Build Coastguard Worker   pbi->metadata->metadata_array = metadata_array;
625*77c1e3ccSAndroid Build Coastguard Worker   pbi->metadata->metadata_array[pbi->metadata->sz] = metadata;
626*77c1e3ccSAndroid Build Coastguard Worker   pbi->metadata->sz++;
627*77c1e3ccSAndroid Build Coastguard Worker }
628*77c1e3ccSAndroid Build Coastguard Worker 
629*77c1e3ccSAndroid Build Coastguard Worker // On failure, calls aom_internal_error() and does not return.
read_metadata_itut_t35(AV1Decoder * const pbi,const uint8_t * data,size_t sz)630*77c1e3ccSAndroid Build Coastguard Worker static void read_metadata_itut_t35(AV1Decoder *const pbi, const uint8_t *data,
631*77c1e3ccSAndroid Build Coastguard Worker                                    size_t sz) {
632*77c1e3ccSAndroid Build Coastguard Worker   if (sz == 0) {
633*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
634*77c1e3ccSAndroid Build Coastguard Worker                        "itu_t_t35_country_code is missing");
635*77c1e3ccSAndroid Build Coastguard Worker   }
636*77c1e3ccSAndroid Build Coastguard Worker   int country_code_size = 1;
637*77c1e3ccSAndroid Build Coastguard Worker   if (*data == 0xFF) {
638*77c1e3ccSAndroid Build Coastguard Worker     if (sz == 1) {
639*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
640*77c1e3ccSAndroid Build Coastguard Worker                          "itu_t_t35_country_code_extension_byte is missing");
641*77c1e3ccSAndroid Build Coastguard Worker     }
642*77c1e3ccSAndroid Build Coastguard Worker     ++country_code_size;
643*77c1e3ccSAndroid Build Coastguard Worker   }
644*77c1e3ccSAndroid Build Coastguard Worker   int end_index = get_last_nonzero_byte_index(data, sz);
645*77c1e3ccSAndroid Build Coastguard Worker   if (end_index < country_code_size) {
646*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
647*77c1e3ccSAndroid Build Coastguard Worker                        "No trailing bits found in ITU-T T.35 metadata OBU");
648*77c1e3ccSAndroid Build Coastguard Worker   }
649*77c1e3ccSAndroid Build Coastguard Worker   // itu_t_t35_payload_bytes is byte aligned. Section 6.7.2 of the spec says:
650*77c1e3ccSAndroid Build Coastguard Worker   //   itu_t_t35_payload_bytes shall be bytes containing data registered as
651*77c1e3ccSAndroid Build Coastguard Worker   //   specified in Recommendation ITU-T T.35.
652*77c1e3ccSAndroid Build Coastguard Worker   // Therefore the first trailing byte should be 0x80.
653*77c1e3ccSAndroid Build Coastguard Worker   if (data[end_index] != 0x80) {
654*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
655*77c1e3ccSAndroid Build Coastguard Worker                        "The last nonzero byte of the ITU-T T.35 metadata OBU "
656*77c1e3ccSAndroid Build Coastguard Worker                        "is 0x%02x, should be 0x80.",
657*77c1e3ccSAndroid Build Coastguard Worker                        data[end_index]);
658*77c1e3ccSAndroid Build Coastguard Worker   }
659*77c1e3ccSAndroid Build Coastguard Worker   alloc_read_metadata(pbi, OBU_METADATA_TYPE_ITUT_T35, data, end_index,
660*77c1e3ccSAndroid Build Coastguard Worker                       AOM_MIF_ANY_FRAME);
661*77c1e3ccSAndroid Build Coastguard Worker }
662*77c1e3ccSAndroid Build Coastguard Worker 
663*77c1e3ccSAndroid Build Coastguard Worker // On success, returns the number of bytes read from 'data'. On failure, calls
664*77c1e3ccSAndroid Build Coastguard Worker // aom_internal_error() and does not return.
read_metadata_hdr_cll(AV1Decoder * const pbi,const uint8_t * data,size_t sz)665*77c1e3ccSAndroid Build Coastguard Worker static size_t read_metadata_hdr_cll(AV1Decoder *const pbi, const uint8_t *data,
666*77c1e3ccSAndroid Build Coastguard Worker                                     size_t sz) {
667*77c1e3ccSAndroid Build Coastguard Worker   const size_t kHdrCllPayloadSize = 4;
668*77c1e3ccSAndroid Build Coastguard Worker   if (sz < kHdrCllPayloadSize) {
669*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
670*77c1e3ccSAndroid Build Coastguard Worker                        "Incorrect HDR CLL metadata payload size");
671*77c1e3ccSAndroid Build Coastguard Worker   }
672*77c1e3ccSAndroid Build Coastguard Worker   alloc_read_metadata(pbi, OBU_METADATA_TYPE_HDR_CLL, data, kHdrCllPayloadSize,
673*77c1e3ccSAndroid Build Coastguard Worker                       AOM_MIF_ANY_FRAME);
674*77c1e3ccSAndroid Build Coastguard Worker   return kHdrCllPayloadSize;
675*77c1e3ccSAndroid Build Coastguard Worker }
676*77c1e3ccSAndroid Build Coastguard Worker 
677*77c1e3ccSAndroid Build Coastguard Worker // On success, returns the number of bytes read from 'data'. On failure, calls
678*77c1e3ccSAndroid Build Coastguard Worker // aom_internal_error() and does not return.
read_metadata_hdr_mdcv(AV1Decoder * const pbi,const uint8_t * data,size_t sz)679*77c1e3ccSAndroid Build Coastguard Worker static size_t read_metadata_hdr_mdcv(AV1Decoder *const pbi, const uint8_t *data,
680*77c1e3ccSAndroid Build Coastguard Worker                                      size_t sz) {
681*77c1e3ccSAndroid Build Coastguard Worker   const size_t kMdcvPayloadSize = 24;
682*77c1e3ccSAndroid Build Coastguard Worker   if (sz < kMdcvPayloadSize) {
683*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
684*77c1e3ccSAndroid Build Coastguard Worker                        "Incorrect HDR MDCV metadata payload size");
685*77c1e3ccSAndroid Build Coastguard Worker   }
686*77c1e3ccSAndroid Build Coastguard Worker   alloc_read_metadata(pbi, OBU_METADATA_TYPE_HDR_MDCV, data, kMdcvPayloadSize,
687*77c1e3ccSAndroid Build Coastguard Worker                       AOM_MIF_ANY_FRAME);
688*77c1e3ccSAndroid Build Coastguard Worker   return kMdcvPayloadSize;
689*77c1e3ccSAndroid Build Coastguard Worker }
690*77c1e3ccSAndroid Build Coastguard Worker 
scalability_structure(struct aom_read_bit_buffer * rb)691*77c1e3ccSAndroid Build Coastguard Worker static void scalability_structure(struct aom_read_bit_buffer *rb) {
692*77c1e3ccSAndroid Build Coastguard Worker   const int spatial_layers_cnt_minus_1 = aom_rb_read_literal(rb, 2);
693*77c1e3ccSAndroid Build Coastguard Worker   const int spatial_layer_dimensions_present_flag = aom_rb_read_bit(rb);
694*77c1e3ccSAndroid Build Coastguard Worker   const int spatial_layer_description_present_flag = aom_rb_read_bit(rb);
695*77c1e3ccSAndroid Build Coastguard Worker   const int temporal_group_description_present_flag = aom_rb_read_bit(rb);
696*77c1e3ccSAndroid Build Coastguard Worker   // scalability_structure_reserved_3bits must be set to zero and be ignored by
697*77c1e3ccSAndroid Build Coastguard Worker   // decoders.
698*77c1e3ccSAndroid Build Coastguard Worker   aom_rb_read_literal(rb, 3);
699*77c1e3ccSAndroid Build Coastguard Worker 
700*77c1e3ccSAndroid Build Coastguard Worker   if (spatial_layer_dimensions_present_flag) {
701*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i <= spatial_layers_cnt_minus_1; i++) {
702*77c1e3ccSAndroid Build Coastguard Worker       aom_rb_read_literal(rb, 16);
703*77c1e3ccSAndroid Build Coastguard Worker       aom_rb_read_literal(rb, 16);
704*77c1e3ccSAndroid Build Coastguard Worker     }
705*77c1e3ccSAndroid Build Coastguard Worker   }
706*77c1e3ccSAndroid Build Coastguard Worker   if (spatial_layer_description_present_flag) {
707*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i <= spatial_layers_cnt_minus_1; i++) {
708*77c1e3ccSAndroid Build Coastguard Worker       aom_rb_read_literal(rb, 8);
709*77c1e3ccSAndroid Build Coastguard Worker     }
710*77c1e3ccSAndroid Build Coastguard Worker   }
711*77c1e3ccSAndroid Build Coastguard Worker   if (temporal_group_description_present_flag) {
712*77c1e3ccSAndroid Build Coastguard Worker     const int temporal_group_size = aom_rb_read_literal(rb, 8);
713*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < temporal_group_size; i++) {
714*77c1e3ccSAndroid Build Coastguard Worker       aom_rb_read_literal(rb, 3);
715*77c1e3ccSAndroid Build Coastguard Worker       aom_rb_read_bit(rb);
716*77c1e3ccSAndroid Build Coastguard Worker       aom_rb_read_bit(rb);
717*77c1e3ccSAndroid Build Coastguard Worker       const int temporal_group_ref_cnt = aom_rb_read_literal(rb, 3);
718*77c1e3ccSAndroid Build Coastguard Worker       for (int j = 0; j < temporal_group_ref_cnt; j++) {
719*77c1e3ccSAndroid Build Coastguard Worker         aom_rb_read_literal(rb, 8);
720*77c1e3ccSAndroid Build Coastguard Worker       }
721*77c1e3ccSAndroid Build Coastguard Worker     }
722*77c1e3ccSAndroid Build Coastguard Worker   }
723*77c1e3ccSAndroid Build Coastguard Worker }
724*77c1e3ccSAndroid Build Coastguard Worker 
read_metadata_scalability(struct aom_read_bit_buffer * rb)725*77c1e3ccSAndroid Build Coastguard Worker static void read_metadata_scalability(struct aom_read_bit_buffer *rb) {
726*77c1e3ccSAndroid Build Coastguard Worker   const int scalability_mode_idc = aom_rb_read_literal(rb, 8);
727*77c1e3ccSAndroid Build Coastguard Worker   if (scalability_mode_idc == SCALABILITY_SS) {
728*77c1e3ccSAndroid Build Coastguard Worker     scalability_structure(rb);
729*77c1e3ccSAndroid Build Coastguard Worker   }
730*77c1e3ccSAndroid Build Coastguard Worker }
731*77c1e3ccSAndroid Build Coastguard Worker 
read_metadata_timecode(struct aom_read_bit_buffer * rb)732*77c1e3ccSAndroid Build Coastguard Worker static void read_metadata_timecode(struct aom_read_bit_buffer *rb) {
733*77c1e3ccSAndroid Build Coastguard Worker   aom_rb_read_literal(rb, 5);  // counting_type f(5)
734*77c1e3ccSAndroid Build Coastguard Worker   const int full_timestamp_flag =
735*77c1e3ccSAndroid Build Coastguard Worker       aom_rb_read_bit(rb);     // full_timestamp_flag f(1)
736*77c1e3ccSAndroid Build Coastguard Worker   aom_rb_read_bit(rb);         // discontinuity_flag (f1)
737*77c1e3ccSAndroid Build Coastguard Worker   aom_rb_read_bit(rb);         // cnt_dropped_flag f(1)
738*77c1e3ccSAndroid Build Coastguard Worker   aom_rb_read_literal(rb, 9);  // n_frames f(9)
739*77c1e3ccSAndroid Build Coastguard Worker   if (full_timestamp_flag) {
740*77c1e3ccSAndroid Build Coastguard Worker     aom_rb_read_literal(rb, 6);  // seconds_value f(6)
741*77c1e3ccSAndroid Build Coastguard Worker     aom_rb_read_literal(rb, 6);  // minutes_value f(6)
742*77c1e3ccSAndroid Build Coastguard Worker     aom_rb_read_literal(rb, 5);  // hours_value f(5)
743*77c1e3ccSAndroid Build Coastguard Worker   } else {
744*77c1e3ccSAndroid Build Coastguard Worker     const int seconds_flag = aom_rb_read_bit(rb);  // seconds_flag f(1)
745*77c1e3ccSAndroid Build Coastguard Worker     if (seconds_flag) {
746*77c1e3ccSAndroid Build Coastguard Worker       aom_rb_read_literal(rb, 6);                    // seconds_value f(6)
747*77c1e3ccSAndroid Build Coastguard Worker       const int minutes_flag = aom_rb_read_bit(rb);  // minutes_flag f(1)
748*77c1e3ccSAndroid Build Coastguard Worker       if (minutes_flag) {
749*77c1e3ccSAndroid Build Coastguard Worker         aom_rb_read_literal(rb, 6);                  // minutes_value f(6)
750*77c1e3ccSAndroid Build Coastguard Worker         const int hours_flag = aom_rb_read_bit(rb);  // hours_flag f(1)
751*77c1e3ccSAndroid Build Coastguard Worker         if (hours_flag) {
752*77c1e3ccSAndroid Build Coastguard Worker           aom_rb_read_literal(rb, 5);  // hours_value f(5)
753*77c1e3ccSAndroid Build Coastguard Worker         }
754*77c1e3ccSAndroid Build Coastguard Worker       }
755*77c1e3ccSAndroid Build Coastguard Worker     }
756*77c1e3ccSAndroid Build Coastguard Worker   }
757*77c1e3ccSAndroid Build Coastguard Worker   // time_offset_length f(5)
758*77c1e3ccSAndroid Build Coastguard Worker   const int time_offset_length = aom_rb_read_literal(rb, 5);
759*77c1e3ccSAndroid Build Coastguard Worker   if (time_offset_length) {
760*77c1e3ccSAndroid Build Coastguard Worker     // time_offset_value f(time_offset_length)
761*77c1e3ccSAndroid Build Coastguard Worker     aom_rb_read_literal(rb, time_offset_length);
762*77c1e3ccSAndroid Build Coastguard Worker   }
763*77c1e3ccSAndroid Build Coastguard Worker }
764*77c1e3ccSAndroid Build Coastguard Worker 
765*77c1e3ccSAndroid Build Coastguard Worker // Returns the last nonzero byte in 'data'. If there is no nonzero byte in
766*77c1e3ccSAndroid Build Coastguard Worker // 'data', returns 0.
767*77c1e3ccSAndroid Build Coastguard Worker //
768*77c1e3ccSAndroid Build Coastguard Worker // Call this function to check the following requirement in the spec:
769*77c1e3ccSAndroid Build Coastguard Worker //   This implies that when any payload data is present for this OBU type, at
770*77c1e3ccSAndroid Build Coastguard Worker //   least one byte of the payload data (including the trailing bit) shall not
771*77c1e3ccSAndroid Build Coastguard Worker //   be equal to 0.
get_last_nonzero_byte(const uint8_t * data,size_t sz)772*77c1e3ccSAndroid Build Coastguard Worker static uint8_t get_last_nonzero_byte(const uint8_t *data, size_t sz) {
773*77c1e3ccSAndroid Build Coastguard Worker   // Scan backward and return on the first nonzero byte.
774*77c1e3ccSAndroid Build Coastguard Worker   size_t i = sz;
775*77c1e3ccSAndroid Build Coastguard Worker   while (i != 0) {
776*77c1e3ccSAndroid Build Coastguard Worker     --i;
777*77c1e3ccSAndroid Build Coastguard Worker     if (data[i] != 0) return data[i];
778*77c1e3ccSAndroid Build Coastguard Worker   }
779*77c1e3ccSAndroid Build Coastguard Worker   return 0;
780*77c1e3ccSAndroid Build Coastguard Worker }
781*77c1e3ccSAndroid Build Coastguard Worker 
782*77c1e3ccSAndroid Build Coastguard Worker // Checks the metadata for correct syntax but ignores the parsed metadata.
783*77c1e3ccSAndroid Build Coastguard Worker //
784*77c1e3ccSAndroid Build Coastguard Worker // On success, returns the number of bytes read from 'data'. On failure, sets
785*77c1e3ccSAndroid Build Coastguard Worker // pbi->common.error.error_code and returns 0, or calls aom_internal_error()
786*77c1e3ccSAndroid Build Coastguard Worker // and does not return.
read_metadata(AV1Decoder * pbi,const uint8_t * data,size_t sz)787*77c1e3ccSAndroid Build Coastguard Worker static size_t read_metadata(AV1Decoder *pbi, const uint8_t *data, size_t sz) {
788*77c1e3ccSAndroid Build Coastguard Worker   size_t type_length;
789*77c1e3ccSAndroid Build Coastguard Worker   uint64_t type_value;
790*77c1e3ccSAndroid Build Coastguard Worker   if (aom_uleb_decode(data, sz, &type_value, &type_length) < 0) {
791*77c1e3ccSAndroid Build Coastguard Worker     pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
792*77c1e3ccSAndroid Build Coastguard Worker     return 0;
793*77c1e3ccSAndroid Build Coastguard Worker   }
794*77c1e3ccSAndroid Build Coastguard Worker   const OBU_METADATA_TYPE metadata_type = (OBU_METADATA_TYPE)type_value;
795*77c1e3ccSAndroid Build Coastguard Worker   if (metadata_type == 0 || metadata_type >= 6) {
796*77c1e3ccSAndroid Build Coastguard Worker     // If metadata_type is reserved for future use or a user private value,
797*77c1e3ccSAndroid Build Coastguard Worker     // ignore the entire OBU and just check trailing bits.
798*77c1e3ccSAndroid Build Coastguard Worker     if (get_last_nonzero_byte(data + type_length, sz - type_length) == 0) {
799*77c1e3ccSAndroid Build Coastguard Worker       pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
800*77c1e3ccSAndroid Build Coastguard Worker       return 0;
801*77c1e3ccSAndroid Build Coastguard Worker     }
802*77c1e3ccSAndroid Build Coastguard Worker     return sz;
803*77c1e3ccSAndroid Build Coastguard Worker   }
804*77c1e3ccSAndroid Build Coastguard Worker   if (metadata_type == OBU_METADATA_TYPE_ITUT_T35) {
805*77c1e3ccSAndroid Build Coastguard Worker     // read_metadata_itut_t35() checks trailing bits.
806*77c1e3ccSAndroid Build Coastguard Worker     read_metadata_itut_t35(pbi, data + type_length, sz - type_length);
807*77c1e3ccSAndroid Build Coastguard Worker     return sz;
808*77c1e3ccSAndroid Build Coastguard Worker   } else if (metadata_type == OBU_METADATA_TYPE_HDR_CLL) {
809*77c1e3ccSAndroid Build Coastguard Worker     size_t bytes_read =
810*77c1e3ccSAndroid Build Coastguard Worker         type_length +
811*77c1e3ccSAndroid Build Coastguard Worker         read_metadata_hdr_cll(pbi, data + type_length, sz - type_length);
812*77c1e3ccSAndroid Build Coastguard Worker     if (get_last_nonzero_byte(data + bytes_read, sz - bytes_read) != 0x80) {
813*77c1e3ccSAndroid Build Coastguard Worker       pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
814*77c1e3ccSAndroid Build Coastguard Worker       return 0;
815*77c1e3ccSAndroid Build Coastguard Worker     }
816*77c1e3ccSAndroid Build Coastguard Worker     return sz;
817*77c1e3ccSAndroid Build Coastguard Worker   } else if (metadata_type == OBU_METADATA_TYPE_HDR_MDCV) {
818*77c1e3ccSAndroid Build Coastguard Worker     size_t bytes_read =
819*77c1e3ccSAndroid Build Coastguard Worker         type_length +
820*77c1e3ccSAndroid Build Coastguard Worker         read_metadata_hdr_mdcv(pbi, data + type_length, sz - type_length);
821*77c1e3ccSAndroid Build Coastguard Worker     if (get_last_nonzero_byte(data + bytes_read, sz - bytes_read) != 0x80) {
822*77c1e3ccSAndroid Build Coastguard Worker       pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
823*77c1e3ccSAndroid Build Coastguard Worker       return 0;
824*77c1e3ccSAndroid Build Coastguard Worker     }
825*77c1e3ccSAndroid Build Coastguard Worker     return sz;
826*77c1e3ccSAndroid Build Coastguard Worker   }
827*77c1e3ccSAndroid Build Coastguard Worker 
828*77c1e3ccSAndroid Build Coastguard Worker   struct aom_read_bit_buffer rb;
829*77c1e3ccSAndroid Build Coastguard Worker   av1_init_read_bit_buffer(pbi, &rb, data + type_length, data + sz);
830*77c1e3ccSAndroid Build Coastguard Worker   if (metadata_type == OBU_METADATA_TYPE_SCALABILITY) {
831*77c1e3ccSAndroid Build Coastguard Worker     read_metadata_scalability(&rb);
832*77c1e3ccSAndroid Build Coastguard Worker   } else {
833*77c1e3ccSAndroid Build Coastguard Worker     assert(metadata_type == OBU_METADATA_TYPE_TIMECODE);
834*77c1e3ccSAndroid Build Coastguard Worker     read_metadata_timecode(&rb);
835*77c1e3ccSAndroid Build Coastguard Worker   }
836*77c1e3ccSAndroid Build Coastguard Worker   if (av1_check_trailing_bits(pbi, &rb) != 0) {
837*77c1e3ccSAndroid Build Coastguard Worker     // pbi->error.error_code is already set.
838*77c1e3ccSAndroid Build Coastguard Worker     return 0;
839*77c1e3ccSAndroid Build Coastguard Worker   }
840*77c1e3ccSAndroid Build Coastguard Worker   assert((rb.bit_offset & 7) == 0);
841*77c1e3ccSAndroid Build Coastguard Worker   return type_length + (rb.bit_offset >> 3);
842*77c1e3ccSAndroid Build Coastguard Worker }
843*77c1e3ccSAndroid Build Coastguard Worker 
844*77c1e3ccSAndroid Build Coastguard Worker // On success, returns 'sz'. On failure, sets pbi->common.error.error_code and
845*77c1e3ccSAndroid Build Coastguard Worker // returns 0.
read_padding(AV1_COMMON * const cm,const uint8_t * data,size_t sz)846*77c1e3ccSAndroid Build Coastguard Worker static size_t read_padding(AV1_COMMON *const cm, const uint8_t *data,
847*77c1e3ccSAndroid Build Coastguard Worker                            size_t sz) {
848*77c1e3ccSAndroid Build Coastguard Worker   // The spec allows a padding OBU to be header-only (i.e., obu_size = 0). So
849*77c1e3ccSAndroid Build Coastguard Worker   // check trailing bits only if sz > 0.
850*77c1e3ccSAndroid Build Coastguard Worker   if (sz > 0) {
851*77c1e3ccSAndroid Build Coastguard Worker     // The payload of a padding OBU is byte aligned. Therefore the first
852*77c1e3ccSAndroid Build Coastguard Worker     // trailing byte should be 0x80. See https://crbug.com/aomedia/2393.
853*77c1e3ccSAndroid Build Coastguard Worker     const uint8_t last_nonzero_byte = get_last_nonzero_byte(data, sz);
854*77c1e3ccSAndroid Build Coastguard Worker     if (last_nonzero_byte != 0x80) {
855*77c1e3ccSAndroid Build Coastguard Worker       cm->error->error_code = AOM_CODEC_CORRUPT_FRAME;
856*77c1e3ccSAndroid Build Coastguard Worker       return 0;
857*77c1e3ccSAndroid Build Coastguard Worker     }
858*77c1e3ccSAndroid Build Coastguard Worker   }
859*77c1e3ccSAndroid Build Coastguard Worker   return sz;
860*77c1e3ccSAndroid Build Coastguard Worker }
861*77c1e3ccSAndroid Build Coastguard Worker 
862*77c1e3ccSAndroid Build Coastguard Worker // On success, returns a boolean that indicates whether the decoding of the
863*77c1e3ccSAndroid Build Coastguard Worker // current frame is finished. On failure, sets pbi->error.error_code and
864*77c1e3ccSAndroid Build Coastguard Worker // returns -1.
aom_decode_frame_from_obus(struct AV1Decoder * pbi,const uint8_t * data,const uint8_t * data_end,const uint8_t ** p_data_end)865*77c1e3ccSAndroid Build Coastguard Worker int aom_decode_frame_from_obus(struct AV1Decoder *pbi, const uint8_t *data,
866*77c1e3ccSAndroid Build Coastguard Worker                                const uint8_t *data_end,
867*77c1e3ccSAndroid Build Coastguard Worker                                const uint8_t **p_data_end) {
868*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &pbi->common;
869*77c1e3ccSAndroid Build Coastguard Worker   int frame_decoding_finished = 0;
870*77c1e3ccSAndroid Build Coastguard Worker   int is_first_tg_obu_received = 1;
871*77c1e3ccSAndroid Build Coastguard Worker   // Whenever pbi->seen_frame_header is set to 1, frame_header is set to the
872*77c1e3ccSAndroid Build Coastguard Worker   // beginning of the frame_header_obu and frame_header_size is set to its
873*77c1e3ccSAndroid Build Coastguard Worker   // size. This allows us to check if a redundant frame_header_obu is a copy
874*77c1e3ccSAndroid Build Coastguard Worker   // of the previous frame_header_obu.
875*77c1e3ccSAndroid Build Coastguard Worker   //
876*77c1e3ccSAndroid Build Coastguard Worker   // Initialize frame_header to a dummy nonnull pointer, otherwise the Clang
877*77c1e3ccSAndroid Build Coastguard Worker   // Static Analyzer in clang 7.0.1 will falsely warn that a null pointer is
878*77c1e3ccSAndroid Build Coastguard Worker   // passed as an argument to a 'nonnull' parameter of memcmp(). The initial
879*77c1e3ccSAndroid Build Coastguard Worker   // value will not be used.
880*77c1e3ccSAndroid Build Coastguard Worker   const uint8_t *frame_header = data;
881*77c1e3ccSAndroid Build Coastguard Worker   uint32_t frame_header_size = 0;
882*77c1e3ccSAndroid Build Coastguard Worker   ObuHeader obu_header;
883*77c1e3ccSAndroid Build Coastguard Worker   memset(&obu_header, 0, sizeof(obu_header));
884*77c1e3ccSAndroid Build Coastguard Worker   pbi->seen_frame_header = 0;
885*77c1e3ccSAndroid Build Coastguard Worker   pbi->next_start_tile = 0;
886*77c1e3ccSAndroid Build Coastguard Worker   pbi->num_tile_groups = 0;
887*77c1e3ccSAndroid Build Coastguard Worker 
888*77c1e3ccSAndroid Build Coastguard Worker   if (data_end < data) {
889*77c1e3ccSAndroid Build Coastguard Worker     pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
890*77c1e3ccSAndroid Build Coastguard Worker     return -1;
891*77c1e3ccSAndroid Build Coastguard Worker   }
892*77c1e3ccSAndroid Build Coastguard Worker 
893*77c1e3ccSAndroid Build Coastguard Worker   // Reset pbi->camera_frame_header_ready to 0 if cm->tiles.large_scale = 0.
894*77c1e3ccSAndroid Build Coastguard Worker   if (!cm->tiles.large_scale) pbi->camera_frame_header_ready = 0;
895*77c1e3ccSAndroid Build Coastguard Worker 
896*77c1e3ccSAndroid Build Coastguard Worker   // decode frame as a series of OBUs
897*77c1e3ccSAndroid Build Coastguard Worker   while (!frame_decoding_finished && pbi->error.error_code == AOM_CODEC_OK) {
898*77c1e3ccSAndroid Build Coastguard Worker     struct aom_read_bit_buffer rb;
899*77c1e3ccSAndroid Build Coastguard Worker     size_t payload_size = 0;
900*77c1e3ccSAndroid Build Coastguard Worker     size_t decoded_payload_size = 0;
901*77c1e3ccSAndroid Build Coastguard Worker     size_t obu_payload_offset = 0;
902*77c1e3ccSAndroid Build Coastguard Worker     size_t bytes_read = 0;
903*77c1e3ccSAndroid Build Coastguard Worker     const size_t bytes_available = data_end - data;
904*77c1e3ccSAndroid Build Coastguard Worker 
905*77c1e3ccSAndroid Build Coastguard Worker     if (bytes_available == 0 && !pbi->seen_frame_header) {
906*77c1e3ccSAndroid Build Coastguard Worker       *p_data_end = data;
907*77c1e3ccSAndroid Build Coastguard Worker       pbi->error.error_code = AOM_CODEC_OK;
908*77c1e3ccSAndroid Build Coastguard Worker       break;
909*77c1e3ccSAndroid Build Coastguard Worker     }
910*77c1e3ccSAndroid Build Coastguard Worker 
911*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_err_t status =
912*77c1e3ccSAndroid Build Coastguard Worker         aom_read_obu_header_and_size(data, bytes_available, pbi->is_annexb,
913*77c1e3ccSAndroid Build Coastguard Worker                                      &obu_header, &payload_size, &bytes_read);
914*77c1e3ccSAndroid Build Coastguard Worker 
915*77c1e3ccSAndroid Build Coastguard Worker     if (status != AOM_CODEC_OK) {
916*77c1e3ccSAndroid Build Coastguard Worker       pbi->error.error_code = status;
917*77c1e3ccSAndroid Build Coastguard Worker       return -1;
918*77c1e3ccSAndroid Build Coastguard Worker     }
919*77c1e3ccSAndroid Build Coastguard Worker 
920*77c1e3ccSAndroid Build Coastguard Worker     // Record obu size header information.
921*77c1e3ccSAndroid Build Coastguard Worker     pbi->obu_size_hdr.data = data + obu_header.size;
922*77c1e3ccSAndroid Build Coastguard Worker     pbi->obu_size_hdr.size = bytes_read - obu_header.size;
923*77c1e3ccSAndroid Build Coastguard Worker 
924*77c1e3ccSAndroid Build Coastguard Worker     // Note: aom_read_obu_header_and_size() takes care of checking that this
925*77c1e3ccSAndroid Build Coastguard Worker     // doesn't cause 'data' to advance past 'data_end'.
926*77c1e3ccSAndroid Build Coastguard Worker     data += bytes_read;
927*77c1e3ccSAndroid Build Coastguard Worker 
928*77c1e3ccSAndroid Build Coastguard Worker     if ((size_t)(data_end - data) < payload_size) {
929*77c1e3ccSAndroid Build Coastguard Worker       pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
930*77c1e3ccSAndroid Build Coastguard Worker       return -1;
931*77c1e3ccSAndroid Build Coastguard Worker     }
932*77c1e3ccSAndroid Build Coastguard Worker 
933*77c1e3ccSAndroid Build Coastguard Worker     cm->temporal_layer_id = obu_header.temporal_layer_id;
934*77c1e3ccSAndroid Build Coastguard Worker     cm->spatial_layer_id = obu_header.spatial_layer_id;
935*77c1e3ccSAndroid Build Coastguard Worker 
936*77c1e3ccSAndroid Build Coastguard Worker     if (obu_header.type != OBU_TEMPORAL_DELIMITER &&
937*77c1e3ccSAndroid Build Coastguard Worker         obu_header.type != OBU_SEQUENCE_HEADER) {
938*77c1e3ccSAndroid Build Coastguard Worker       // don't decode obu if it's not in current operating mode
939*77c1e3ccSAndroid Build Coastguard Worker       if (!is_obu_in_current_operating_point(pbi, &obu_header)) {
940*77c1e3ccSAndroid Build Coastguard Worker         data += payload_size;
941*77c1e3ccSAndroid Build Coastguard Worker         continue;
942*77c1e3ccSAndroid Build Coastguard Worker       }
943*77c1e3ccSAndroid Build Coastguard Worker     }
944*77c1e3ccSAndroid Build Coastguard Worker 
945*77c1e3ccSAndroid Build Coastguard Worker     av1_init_read_bit_buffer(pbi, &rb, data, data + payload_size);
946*77c1e3ccSAndroid Build Coastguard Worker 
947*77c1e3ccSAndroid Build Coastguard Worker     switch (obu_header.type) {
948*77c1e3ccSAndroid Build Coastguard Worker       case OBU_TEMPORAL_DELIMITER:
949*77c1e3ccSAndroid Build Coastguard Worker         decoded_payload_size = read_temporal_delimiter_obu();
950*77c1e3ccSAndroid Build Coastguard Worker         if (pbi->seen_frame_header) {
951*77c1e3ccSAndroid Build Coastguard Worker           // A new temporal unit has started, but the frame in the previous
952*77c1e3ccSAndroid Build Coastguard Worker           // temporal unit is incomplete.
953*77c1e3ccSAndroid Build Coastguard Worker           pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
954*77c1e3ccSAndroid Build Coastguard Worker           return -1;
955*77c1e3ccSAndroid Build Coastguard Worker         }
956*77c1e3ccSAndroid Build Coastguard Worker         break;
957*77c1e3ccSAndroid Build Coastguard Worker       case OBU_SEQUENCE_HEADER:
958*77c1e3ccSAndroid Build Coastguard Worker         decoded_payload_size = read_sequence_header_obu(pbi, &rb);
959*77c1e3ccSAndroid Build Coastguard Worker         if (pbi->error.error_code != AOM_CODEC_OK) return -1;
960*77c1e3ccSAndroid Build Coastguard Worker         // The sequence header should not change in the middle of a frame.
961*77c1e3ccSAndroid Build Coastguard Worker         if (pbi->sequence_header_changed && pbi->seen_frame_header) {
962*77c1e3ccSAndroid Build Coastguard Worker           pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
963*77c1e3ccSAndroid Build Coastguard Worker           return -1;
964*77c1e3ccSAndroid Build Coastguard Worker         }
965*77c1e3ccSAndroid Build Coastguard Worker         break;
966*77c1e3ccSAndroid Build Coastguard Worker       case OBU_FRAME_HEADER:
967*77c1e3ccSAndroid Build Coastguard Worker       case OBU_REDUNDANT_FRAME_HEADER:
968*77c1e3ccSAndroid Build Coastguard Worker       case OBU_FRAME:
969*77c1e3ccSAndroid Build Coastguard Worker         if (obu_header.type == OBU_REDUNDANT_FRAME_HEADER) {
970*77c1e3ccSAndroid Build Coastguard Worker           if (!pbi->seen_frame_header) {
971*77c1e3ccSAndroid Build Coastguard Worker             pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
972*77c1e3ccSAndroid Build Coastguard Worker             return -1;
973*77c1e3ccSAndroid Build Coastguard Worker           }
974*77c1e3ccSAndroid Build Coastguard Worker         } else {
975*77c1e3ccSAndroid Build Coastguard Worker           // OBU_FRAME_HEADER or OBU_FRAME.
976*77c1e3ccSAndroid Build Coastguard Worker           if (pbi->seen_frame_header) {
977*77c1e3ccSAndroid Build Coastguard Worker             pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
978*77c1e3ccSAndroid Build Coastguard Worker             return -1;
979*77c1e3ccSAndroid Build Coastguard Worker           }
980*77c1e3ccSAndroid Build Coastguard Worker         }
981*77c1e3ccSAndroid Build Coastguard Worker         // Only decode first frame header received
982*77c1e3ccSAndroid Build Coastguard Worker         if (!pbi->seen_frame_header ||
983*77c1e3ccSAndroid Build Coastguard Worker             (cm->tiles.large_scale && !pbi->camera_frame_header_ready)) {
984*77c1e3ccSAndroid Build Coastguard Worker           frame_header_size = read_frame_header_obu(
985*77c1e3ccSAndroid Build Coastguard Worker               pbi, &rb, data, p_data_end, obu_header.type != OBU_FRAME);
986*77c1e3ccSAndroid Build Coastguard Worker           frame_header = data;
987*77c1e3ccSAndroid Build Coastguard Worker           pbi->seen_frame_header = 1;
988*77c1e3ccSAndroid Build Coastguard Worker           if (!pbi->ext_tile_debug && cm->tiles.large_scale)
989*77c1e3ccSAndroid Build Coastguard Worker             pbi->camera_frame_header_ready = 1;
990*77c1e3ccSAndroid Build Coastguard Worker         } else {
991*77c1e3ccSAndroid Build Coastguard Worker           // Verify that the frame_header_obu is identical to the original
992*77c1e3ccSAndroid Build Coastguard Worker           // frame_header_obu.
993*77c1e3ccSAndroid Build Coastguard Worker           if (frame_header_size > payload_size ||
994*77c1e3ccSAndroid Build Coastguard Worker               memcmp(data, frame_header, frame_header_size) != 0) {
995*77c1e3ccSAndroid Build Coastguard Worker             pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
996*77c1e3ccSAndroid Build Coastguard Worker             return -1;
997*77c1e3ccSAndroid Build Coastguard Worker           }
998*77c1e3ccSAndroid Build Coastguard Worker           assert(rb.bit_offset == 0);
999*77c1e3ccSAndroid Build Coastguard Worker           rb.bit_offset = 8 * frame_header_size;
1000*77c1e3ccSAndroid Build Coastguard Worker         }
1001*77c1e3ccSAndroid Build Coastguard Worker 
1002*77c1e3ccSAndroid Build Coastguard Worker         decoded_payload_size = frame_header_size;
1003*77c1e3ccSAndroid Build Coastguard Worker         pbi->frame_header_size = frame_header_size;
1004*77c1e3ccSAndroid Build Coastguard Worker         cm->cur_frame->temporal_id = obu_header.temporal_layer_id;
1005*77c1e3ccSAndroid Build Coastguard Worker         cm->cur_frame->spatial_id = obu_header.spatial_layer_id;
1006*77c1e3ccSAndroid Build Coastguard Worker 
1007*77c1e3ccSAndroid Build Coastguard Worker         if (cm->show_existing_frame) {
1008*77c1e3ccSAndroid Build Coastguard Worker           if (obu_header.type == OBU_FRAME) {
1009*77c1e3ccSAndroid Build Coastguard Worker             pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
1010*77c1e3ccSAndroid Build Coastguard Worker             return -1;
1011*77c1e3ccSAndroid Build Coastguard Worker           }
1012*77c1e3ccSAndroid Build Coastguard Worker           frame_decoding_finished = 1;
1013*77c1e3ccSAndroid Build Coastguard Worker           pbi->seen_frame_header = 0;
1014*77c1e3ccSAndroid Build Coastguard Worker 
1015*77c1e3ccSAndroid Build Coastguard Worker           if (cm->show_frame &&
1016*77c1e3ccSAndroid Build Coastguard Worker               !cm->seq_params->order_hint_info.enable_order_hint) {
1017*77c1e3ccSAndroid Build Coastguard Worker             ++cm->current_frame.frame_number;
1018*77c1e3ccSAndroid Build Coastguard Worker           }
1019*77c1e3ccSAndroid Build Coastguard Worker           break;
1020*77c1e3ccSAndroid Build Coastguard Worker         }
1021*77c1e3ccSAndroid Build Coastguard Worker 
1022*77c1e3ccSAndroid Build Coastguard Worker         // In large scale tile coding, decode the common camera frame header
1023*77c1e3ccSAndroid Build Coastguard Worker         // before any tile list OBU.
1024*77c1e3ccSAndroid Build Coastguard Worker         if (!pbi->ext_tile_debug && pbi->camera_frame_header_ready) {
1025*77c1e3ccSAndroid Build Coastguard Worker           frame_decoding_finished = 1;
1026*77c1e3ccSAndroid Build Coastguard Worker           // Skip the rest of the frame data.
1027*77c1e3ccSAndroid Build Coastguard Worker           decoded_payload_size = payload_size;
1028*77c1e3ccSAndroid Build Coastguard Worker           // Update data_end.
1029*77c1e3ccSAndroid Build Coastguard Worker           *p_data_end = data_end;
1030*77c1e3ccSAndroid Build Coastguard Worker           break;
1031*77c1e3ccSAndroid Build Coastguard Worker         }
1032*77c1e3ccSAndroid Build Coastguard Worker 
1033*77c1e3ccSAndroid Build Coastguard Worker         if (obu_header.type != OBU_FRAME) break;
1034*77c1e3ccSAndroid Build Coastguard Worker         obu_payload_offset = frame_header_size;
1035*77c1e3ccSAndroid Build Coastguard Worker         // Byte align the reader before reading the tile group.
1036*77c1e3ccSAndroid Build Coastguard Worker         // byte_alignment() has set pbi->error.error_code if it returns -1.
1037*77c1e3ccSAndroid Build Coastguard Worker         if (byte_alignment(cm, &rb)) return -1;
1038*77c1e3ccSAndroid Build Coastguard Worker         AOM_FALLTHROUGH_INTENDED;  // fall through to read tile group.
1039*77c1e3ccSAndroid Build Coastguard Worker       case OBU_TILE_GROUP:
1040*77c1e3ccSAndroid Build Coastguard Worker         if (!pbi->seen_frame_header) {
1041*77c1e3ccSAndroid Build Coastguard Worker           pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1042*77c1e3ccSAndroid Build Coastguard Worker           return -1;
1043*77c1e3ccSAndroid Build Coastguard Worker         }
1044*77c1e3ccSAndroid Build Coastguard Worker         if (obu_payload_offset > payload_size) {
1045*77c1e3ccSAndroid Build Coastguard Worker           pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1046*77c1e3ccSAndroid Build Coastguard Worker           return -1;
1047*77c1e3ccSAndroid Build Coastguard Worker         }
1048*77c1e3ccSAndroid Build Coastguard Worker         decoded_payload_size += read_one_tile_group_obu(
1049*77c1e3ccSAndroid Build Coastguard Worker             pbi, &rb, is_first_tg_obu_received, data + obu_payload_offset,
1050*77c1e3ccSAndroid Build Coastguard Worker             data + payload_size, p_data_end, &frame_decoding_finished,
1051*77c1e3ccSAndroid Build Coastguard Worker             obu_header.type == OBU_FRAME);
1052*77c1e3ccSAndroid Build Coastguard Worker         if (pbi->error.error_code != AOM_CODEC_OK) return -1;
1053*77c1e3ccSAndroid Build Coastguard Worker         is_first_tg_obu_received = 0;
1054*77c1e3ccSAndroid Build Coastguard Worker         if (frame_decoding_finished) {
1055*77c1e3ccSAndroid Build Coastguard Worker           pbi->seen_frame_header = 0;
1056*77c1e3ccSAndroid Build Coastguard Worker           pbi->next_start_tile = 0;
1057*77c1e3ccSAndroid Build Coastguard Worker         }
1058*77c1e3ccSAndroid Build Coastguard Worker         pbi->num_tile_groups++;
1059*77c1e3ccSAndroid Build Coastguard Worker         break;
1060*77c1e3ccSAndroid Build Coastguard Worker       case OBU_METADATA:
1061*77c1e3ccSAndroid Build Coastguard Worker         decoded_payload_size = read_metadata(pbi, data, payload_size);
1062*77c1e3ccSAndroid Build Coastguard Worker         if (pbi->error.error_code != AOM_CODEC_OK) return -1;
1063*77c1e3ccSAndroid Build Coastguard Worker         break;
1064*77c1e3ccSAndroid Build Coastguard Worker       case OBU_TILE_LIST:
1065*77c1e3ccSAndroid Build Coastguard Worker         if (CONFIG_NORMAL_TILE_MODE) {
1066*77c1e3ccSAndroid Build Coastguard Worker           pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
1067*77c1e3ccSAndroid Build Coastguard Worker           return -1;
1068*77c1e3ccSAndroid Build Coastguard Worker         }
1069*77c1e3ccSAndroid Build Coastguard Worker 
1070*77c1e3ccSAndroid Build Coastguard Worker         // This OBU type is purely for the large scale tile coding mode.
1071*77c1e3ccSAndroid Build Coastguard Worker         // The common camera frame header has to be already decoded.
1072*77c1e3ccSAndroid Build Coastguard Worker         if (!pbi->camera_frame_header_ready) {
1073*77c1e3ccSAndroid Build Coastguard Worker           pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1074*77c1e3ccSAndroid Build Coastguard Worker           return -1;
1075*77c1e3ccSAndroid Build Coastguard Worker         }
1076*77c1e3ccSAndroid Build Coastguard Worker 
1077*77c1e3ccSAndroid Build Coastguard Worker         cm->tiles.large_scale = 1;
1078*77c1e3ccSAndroid Build Coastguard Worker         av1_set_single_tile_decoding_mode(cm);
1079*77c1e3ccSAndroid Build Coastguard Worker         decoded_payload_size =
1080*77c1e3ccSAndroid Build Coastguard Worker             read_and_decode_one_tile_list(pbi, &rb, data, data + payload_size,
1081*77c1e3ccSAndroid Build Coastguard Worker                                           p_data_end, &frame_decoding_finished);
1082*77c1e3ccSAndroid Build Coastguard Worker         if (pbi->error.error_code != AOM_CODEC_OK) return -1;
1083*77c1e3ccSAndroid Build Coastguard Worker         break;
1084*77c1e3ccSAndroid Build Coastguard Worker       case OBU_PADDING:
1085*77c1e3ccSAndroid Build Coastguard Worker         decoded_payload_size = read_padding(cm, data, payload_size);
1086*77c1e3ccSAndroid Build Coastguard Worker         if (pbi->error.error_code != AOM_CODEC_OK) return -1;
1087*77c1e3ccSAndroid Build Coastguard Worker         break;
1088*77c1e3ccSAndroid Build Coastguard Worker       default:
1089*77c1e3ccSAndroid Build Coastguard Worker         // Skip unrecognized OBUs
1090*77c1e3ccSAndroid Build Coastguard Worker         if (payload_size > 0 &&
1091*77c1e3ccSAndroid Build Coastguard Worker             get_last_nonzero_byte(data, payload_size) == 0) {
1092*77c1e3ccSAndroid Build Coastguard Worker           pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1093*77c1e3ccSAndroid Build Coastguard Worker           return -1;
1094*77c1e3ccSAndroid Build Coastguard Worker         }
1095*77c1e3ccSAndroid Build Coastguard Worker         decoded_payload_size = payload_size;
1096*77c1e3ccSAndroid Build Coastguard Worker         break;
1097*77c1e3ccSAndroid Build Coastguard Worker     }
1098*77c1e3ccSAndroid Build Coastguard Worker 
1099*77c1e3ccSAndroid Build Coastguard Worker     // Check that the signalled OBU size matches the actual amount of data read
1100*77c1e3ccSAndroid Build Coastguard Worker     if (decoded_payload_size > payload_size) {
1101*77c1e3ccSAndroid Build Coastguard Worker       pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1102*77c1e3ccSAndroid Build Coastguard Worker       return -1;
1103*77c1e3ccSAndroid Build Coastguard Worker     }
1104*77c1e3ccSAndroid Build Coastguard Worker 
1105*77c1e3ccSAndroid Build Coastguard Worker     // If there are extra padding bytes, they should all be zero
1106*77c1e3ccSAndroid Build Coastguard Worker     while (decoded_payload_size < payload_size) {
1107*77c1e3ccSAndroid Build Coastguard Worker       uint8_t padding_byte = data[decoded_payload_size++];
1108*77c1e3ccSAndroid Build Coastguard Worker       if (padding_byte != 0) {
1109*77c1e3ccSAndroid Build Coastguard Worker         pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1110*77c1e3ccSAndroid Build Coastguard Worker         return -1;
1111*77c1e3ccSAndroid Build Coastguard Worker       }
1112*77c1e3ccSAndroid Build Coastguard Worker     }
1113*77c1e3ccSAndroid Build Coastguard Worker 
1114*77c1e3ccSAndroid Build Coastguard Worker     data += payload_size;
1115*77c1e3ccSAndroid Build Coastguard Worker   }
1116*77c1e3ccSAndroid Build Coastguard Worker 
1117*77c1e3ccSAndroid Build Coastguard Worker   if (pbi->error.error_code != AOM_CODEC_OK) return -1;
1118*77c1e3ccSAndroid Build Coastguard Worker   return frame_decoding_finished;
1119*77c1e3ccSAndroid Build Coastguard Worker }
1120