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