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