• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,ObuHeader obu_header)54 static int is_obu_in_current_operating_point(AV1Decoder *pbi,
55                                              ObuHeader obu_header) {
56   if (!pbi->current_operating_point) {
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()79 static uint32_t read_temporal_delimiter_obu() { 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     cm->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     cm->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       cm->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, &cm->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         cm->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(&cm->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                 &cm->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, &cm->number_spatial_layers,
236           &cm->number_temporal_layers) != AOM_CODEC_OK) {
237     cm->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, &cm->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(&cm->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     // cm->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.
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)275 static uint32_t read_frame_header_obu(AV1Decoder *pbi,
276                                       struct aom_read_bit_buffer *rb,
277                                       const uint8_t *data,
278                                       const uint8_t **p_data_end,
279                                       int trailing_bits_present) {
280   return av1_decode_frame_headers_and_setup(pbi, rb, data, p_data_end,
281                                             trailing_bits_present);
282 }
283 
284 // On success, returns the tile group header size. On failure, calls
285 // 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)286 static int32_t read_tile_group_header(AV1Decoder *pbi,
287                                       struct aom_read_bit_buffer *rb,
288                                       int *start_tile, int *end_tile,
289                                       int tile_start_implicit) {
290   AV1_COMMON *const cm = &pbi->common;
291   CommonTileParams *const tiles = &cm->tiles;
292   uint32_t saved_bit_offset = rb->bit_offset;
293   int tile_start_and_end_present_flag = 0;
294   const int num_tiles = tiles->rows * tiles->cols;
295 
296   if (!tiles->large_scale && num_tiles > 1) {
297     tile_start_and_end_present_flag = aom_rb_read_bit(rb);
298     if (tile_start_implicit && tile_start_and_end_present_flag) {
299       aom_internal_error(
300           &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
301           "For OBU_FRAME type obu tile_start_and_end_present_flag must be 0");
302       return -1;
303     }
304   }
305   if (tiles->large_scale || num_tiles == 1 ||
306       !tile_start_and_end_present_flag) {
307     *start_tile = 0;
308     *end_tile = num_tiles - 1;
309   } else {
310     int tile_bits = tiles->log2_rows + tiles->log2_cols;
311     *start_tile = aom_rb_read_literal(rb, tile_bits);
312     *end_tile = aom_rb_read_literal(rb, tile_bits);
313   }
314   if (*start_tile != pbi->next_start_tile) {
315     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
316                        "tg_start (%d) must be equal to %d", *start_tile,
317                        pbi->next_start_tile);
318     return -1;
319   }
320   if (*start_tile > *end_tile) {
321     aom_internal_error(
322         &cm->error, AOM_CODEC_CORRUPT_FRAME,
323         "tg_end (%d) must be greater than or equal to tg_start (%d)", *end_tile,
324         *start_tile);
325     return -1;
326   }
327   if (*end_tile >= num_tiles) {
328     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
329                        "tg_end (%d) must be less than NumTiles (%d)", *end_tile,
330                        num_tiles);
331     return -1;
332   }
333   pbi->next_start_tile = (*end_tile == num_tiles - 1) ? 0 : *end_tile + 1;
334 
335   return ((rb->bit_offset - saved_bit_offset + 7) >> 3);
336 }
337 
338 // On success, returns the tile group OBU size. On failure, sets
339 // 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)340 static uint32_t read_one_tile_group_obu(
341     AV1Decoder *pbi, struct aom_read_bit_buffer *rb, int is_first_tg,
342     const uint8_t *data, const uint8_t *data_end, const uint8_t **p_data_end,
343     int *is_last_tg, int tile_start_implicit) {
344   AV1_COMMON *const cm = &pbi->common;
345   int start_tile, end_tile;
346   int32_t header_size, tg_payload_size;
347 
348   assert((rb->bit_offset & 7) == 0);
349   assert(rb->bit_buffer + aom_rb_bytes_read(rb) == data);
350 
351   header_size = read_tile_group_header(pbi, rb, &start_tile, &end_tile,
352                                        tile_start_implicit);
353   if (header_size == -1 || byte_alignment(cm, rb)) return 0;
354   data += header_size;
355   av1_decode_tg_tiles_and_wrapup(pbi, data, data_end, p_data_end, start_tile,
356                                  end_tile, is_first_tg);
357 
358   tg_payload_size = (uint32_t)(*p_data_end - data);
359 
360   *is_last_tg = end_tile == cm->tiles.rows * cm->tiles.cols - 1;
361   return header_size + tg_payload_size;
362 }
363 
alloc_tile_list_buffer(AV1Decoder * pbi)364 static void alloc_tile_list_buffer(AV1Decoder *pbi) {
365   // The resolution of the output frame is read out from the bitstream. The data
366   // are stored in the order of Y plane, U plane and V plane. As an example, for
367   // image format 4:2:0, the output frame of U plane and V plane is 1/4 of the
368   // output frame.
369   AV1_COMMON *const cm = &pbi->common;
370   int tile_width, tile_height;
371   av1_get_uniform_tile_size(cm, &tile_width, &tile_height);
372   const int tile_width_in_pixels = tile_width * MI_SIZE;
373   const int tile_height_in_pixels = tile_height * MI_SIZE;
374   const int output_frame_width =
375       (pbi->output_frame_width_in_tiles_minus_1 + 1) * tile_width_in_pixels;
376   const int output_frame_height =
377       (pbi->output_frame_height_in_tiles_minus_1 + 1) * tile_height_in_pixels;
378   // The output frame is used to store the decoded tile list. The decoded tile
379   // list has to fit into 1 output frame.
380   assert((pbi->tile_count_minus_1 + 1) <=
381          (pbi->output_frame_width_in_tiles_minus_1 + 1) *
382              (pbi->output_frame_height_in_tiles_minus_1 + 1));
383 
384   // Allocate the tile list output buffer.
385   // Note: if cm->seq_params.use_highbitdepth is 1 and cm->seq_params.bit_depth
386   // is 8, we could allocate less memory, namely, 8 bits/pixel.
387   if (aom_alloc_frame_buffer(&pbi->tile_list_outbuf, output_frame_width,
388                              output_frame_height, cm->seq_params.subsampling_x,
389                              cm->seq_params.subsampling_y,
390                              (cm->seq_params.use_highbitdepth &&
391                               (cm->seq_params.bit_depth > AOM_BITS_8)),
392                              0, cm->features.byte_alignment))
393     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
394                        "Failed to allocate the tile list output buffer");
395 }
396 
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)397 static void yv12_tile_copy(const YV12_BUFFER_CONFIG *src, int hstart1,
398                            int hend1, int vstart1, int vend1,
399                            YV12_BUFFER_CONFIG *dst, int hstart2, int vstart2,
400                            int plane) {
401   const int src_stride = (plane > 0) ? src->strides[1] : src->strides[0];
402   const int dst_stride = (plane > 0) ? dst->strides[1] : dst->strides[0];
403   int row, col;
404 
405   assert(src->flags & YV12_FLAG_HIGHBITDEPTH);
406   assert(!(dst->flags & YV12_FLAG_HIGHBITDEPTH));
407 
408   const uint16_t *src16 =
409       CONVERT_TO_SHORTPTR(src->buffers[plane] + vstart1 * src_stride + hstart1);
410   uint8_t *dst8 = dst->buffers[plane] + vstart2 * dst_stride + hstart2;
411 
412   for (row = vstart1; row < vend1; ++row) {
413     for (col = 0; col < (hend1 - hstart1); ++col) *dst8++ = (uint8_t)(*src16++);
414     src16 += src_stride - (hend1 - hstart1);
415     dst8 += dst_stride - (hend1 - hstart1);
416   }
417   return;
418 }
419 
copy_decoded_tile_to_tile_list_buffer(AV1Decoder * pbi,int tile_idx)420 static void copy_decoded_tile_to_tile_list_buffer(AV1Decoder *pbi,
421                                                   int tile_idx) {
422   AV1_COMMON *const cm = &pbi->common;
423   int tile_width, tile_height;
424   av1_get_uniform_tile_size(cm, &tile_width, &tile_height);
425   const int tile_width_in_pixels = tile_width * MI_SIZE;
426   const int tile_height_in_pixels = tile_height * MI_SIZE;
427   const int ssy = cm->seq_params.subsampling_y;
428   const int ssx = cm->seq_params.subsampling_x;
429   const int num_planes = av1_num_planes(cm);
430 
431   YV12_BUFFER_CONFIG *cur_frame = &cm->cur_frame->buf;
432   const int tr = tile_idx / (pbi->output_frame_width_in_tiles_minus_1 + 1);
433   const int tc = tile_idx % (pbi->output_frame_width_in_tiles_minus_1 + 1);
434   int plane;
435 
436   // Copy decoded tile to the tile list output buffer.
437   for (plane = 0; plane < num_planes; ++plane) {
438     const int shift_x = plane > 0 ? ssx : 0;
439     const int shift_y = plane > 0 ? ssy : 0;
440     const int h = tile_height_in_pixels >> shift_y;
441     const int w = tile_width_in_pixels >> shift_x;
442 
443     // src offset
444     int vstart1 = pbi->dec_tile_row * h;
445     int vend1 = vstart1 + h;
446     int hstart1 = pbi->dec_tile_col * w;
447     int hend1 = hstart1 + w;
448     // dst offset
449     int vstart2 = tr * h;
450     int hstart2 = tc * w;
451 
452     if (cm->seq_params.use_highbitdepth &&
453         cm->seq_params.bit_depth == AOM_BITS_8) {
454       yv12_tile_copy(cur_frame, hstart1, hend1, vstart1, vend1,
455                      &pbi->tile_list_outbuf, hstart2, vstart2, plane);
456     } else {
457       switch (plane) {
458         case 0:
459           aom_yv12_partial_copy_y(cur_frame, hstart1, hend1, vstart1, vend1,
460                                   &pbi->tile_list_outbuf, hstart2, vstart2);
461           break;
462         case 1:
463           aom_yv12_partial_copy_u(cur_frame, hstart1, hend1, vstart1, vend1,
464                                   &pbi->tile_list_outbuf, hstart2, vstart2);
465           break;
466         case 2:
467           aom_yv12_partial_copy_v(cur_frame, hstart1, hend1, vstart1, vend1,
468                                   &pbi->tile_list_outbuf, hstart2, vstart2);
469           break;
470         default: assert(0);
471       }
472     }
473   }
474 }
475 
476 // Only called while large_scale_tile = 1.
477 //
478 // On success, returns the tile list OBU size. On failure, sets
479 // 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)480 static uint32_t read_and_decode_one_tile_list(AV1Decoder *pbi,
481                                               struct aom_read_bit_buffer *rb,
482                                               const uint8_t *data,
483                                               const uint8_t *data_end,
484                                               const uint8_t **p_data_end,
485                                               int *frame_decoding_finished) {
486   AV1_COMMON *const cm = &pbi->common;
487   uint32_t tile_list_payload_size = 0;
488   const int num_tiles = cm->tiles.cols * cm->tiles.rows;
489   const int start_tile = 0;
490   const int end_tile = num_tiles - 1;
491   int i = 0;
492 
493   // Process the tile list info.
494   pbi->output_frame_width_in_tiles_minus_1 = aom_rb_read_literal(rb, 8);
495   pbi->output_frame_height_in_tiles_minus_1 = aom_rb_read_literal(rb, 8);
496   pbi->tile_count_minus_1 = aom_rb_read_literal(rb, 16);
497   if (pbi->tile_count_minus_1 > MAX_TILES - 1) {
498     cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
499     return 0;
500   }
501 
502   // Allocate output frame buffer for the tile list.
503   alloc_tile_list_buffer(pbi);
504 
505   uint32_t tile_list_info_bytes = 4;
506   tile_list_payload_size += tile_list_info_bytes;
507   data += tile_list_info_bytes;
508 
509   int tile_idx = 0;
510   for (i = 0; i <= pbi->tile_count_minus_1; i++) {
511     // Process 1 tile.
512     // Reset the bit reader.
513     rb->bit_offset = 0;
514     rb->bit_buffer = data;
515 
516     // Read out the tile info.
517     uint32_t tile_info_bytes = 5;
518     // Set reference for each tile.
519     int ref_idx = aom_rb_read_literal(rb, 8);
520     if (ref_idx >= MAX_EXTERNAL_REFERENCES) {
521       cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
522       return 0;
523     }
524     av1_set_reference_dec(cm, cm->remapped_ref_idx[0], 1,
525                           &pbi->ext_refs.refs[ref_idx]);
526 
527     pbi->dec_tile_row = aom_rb_read_literal(rb, 8);
528     pbi->dec_tile_col = aom_rb_read_literal(rb, 8);
529     if (pbi->dec_tile_row < 0 || pbi->dec_tile_col < 0 ||
530         pbi->dec_tile_row >= cm->tiles.rows ||
531         pbi->dec_tile_col >= cm->tiles.cols) {
532       cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
533       return 0;
534     }
535 
536     pbi->coded_tile_data_size = aom_rb_read_literal(rb, 16) + 1;
537     data += tile_info_bytes;
538     if ((size_t)(data_end - data) < pbi->coded_tile_data_size) {
539       cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
540       return 0;
541     }
542 
543     av1_decode_tg_tiles_and_wrapup(pbi, data, data + pbi->coded_tile_data_size,
544                                    p_data_end, start_tile, end_tile, 0);
545     uint32_t tile_payload_size = (uint32_t)(*p_data_end - data);
546 
547     tile_list_payload_size += tile_info_bytes + tile_payload_size;
548 
549     // Update data ptr for next tile decoding.
550     data = *p_data_end;
551     assert(data <= data_end);
552 
553     // Copy the decoded tile to the tile list output buffer.
554     copy_decoded_tile_to_tile_list_buffer(pbi, tile_idx);
555     tile_idx++;
556   }
557 
558   *frame_decoding_finished = 1;
559   return tile_list_payload_size;
560 }
561 
562 // Returns the last nonzero byte index in 'data'. If there is no nonzero byte in
563 // 'data', returns -1.
get_last_nonzero_byte_index(const uint8_t * data,size_t sz)564 static int get_last_nonzero_byte_index(const uint8_t *data, size_t sz) {
565   // Scan backward and return on the first nonzero byte.
566   int i = (int)sz - 1;
567   while (i >= 0 && data[i] == 0) {
568     --i;
569   }
570   return i;
571 }
572 
573 // 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)574 static void alloc_read_metadata(AV1Decoder *const pbi,
575                                 OBU_METADATA_TYPE metadata_type,
576                                 const uint8_t *data, size_t sz,
577                                 aom_metadata_insert_flags_t insert_flag) {
578   AV1_COMMON *const cm = &pbi->common;
579   aom_metadata_t *metadata =
580       aom_img_metadata_alloc(metadata_type, data, sz, insert_flag);
581   if (!metadata) {
582     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
583                        "Error allocating metadata");
584   }
585   if (!pbi->metadata) {
586     pbi->metadata = aom_img_metadata_array_alloc(1);
587     if (!pbi->metadata) {
588       aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
589                          "Failed to allocate metadata array");
590     }
591   } else {
592     aom_metadata_t **metadata_array =
593         (aom_metadata_t **)realloc(pbi->metadata->metadata_array,
594                                    (pbi->metadata->sz + 1) * sizeof(metadata));
595     if (!metadata_array) {
596       aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
597                          "Error allocating metadata");
598     }
599     pbi->metadata->metadata_array = metadata_array;
600     pbi->metadata->sz++;
601   }
602   pbi->metadata->metadata_array[pbi->metadata->sz - 1] = metadata;
603 }
604 
605 // On success, returns the number of bytes read from 'data'. On failure, calls
606 // aom_internal_error() and does not return.
read_metadata_itut_t35(AV1Decoder * const pbi,const uint8_t * data,size_t sz)607 static size_t read_metadata_itut_t35(AV1Decoder *const pbi, const uint8_t *data,
608                                      size_t sz) {
609   const int kMinItuT35PayloadSize = 2;
610   AV1_COMMON *const cm = &pbi->common;
611   if (sz == 0) {
612     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
613                        "itu_t_t35_country_code is missing");
614   }
615   int bytes_read = get_last_nonzero_byte_index(data, sz);
616   if (bytes_read < 0) {
617     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
618                        "No trailing bits found on metadata");
619   }
620   if (*data == 0xFF && bytes_read < kMinItuT35PayloadSize) {
621     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
622                        "itu_t_t35_country_code_extension_byte is missing");
623   }
624   alloc_read_metadata(pbi, OBU_METADATA_TYPE_ITUT_T35, data, (size_t)bytes_read,
625                       AOM_MIF_ANY_FRAME);
626   return (size_t)bytes_read;
627 }
628 
629 // On success, returns the number of bytes read from 'data'. On failure, calls
630 // aom_internal_error() and does not return.
read_metadata_hdr_cll(AV1Decoder * const pbi,const uint8_t * data,size_t sz)631 static size_t read_metadata_hdr_cll(AV1Decoder *const pbi, const uint8_t *data,
632                                     size_t sz) {
633   const int kHdrCllPayloadSize = 4;
634   AV1_COMMON *const cm = &pbi->common;
635   if (sz == 0) {
636     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
637                        "HDR CLL metadata payload is missing");
638   }
639   int bytes_read = get_last_nonzero_byte_index(data, sz);
640   if (bytes_read < 0) {
641     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
642                        "No trailing bits found on metadata");
643   }
644   if (bytes_read != kHdrCllPayloadSize) {
645     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
646                        "Incorrect HDR CLL metadata payload size");
647   }
648   alloc_read_metadata(pbi, OBU_METADATA_TYPE_HDR_CLL, data, (size_t)bytes_read,
649                       AOM_MIF_ANY_FRAME);
650   return (size_t)bytes_read;
651 }
652 
653 // On success, returns the number of bytes read from 'data'. On failure, calls
654 // aom_internal_error() and does not return.
read_metadata_hdr_mdcv(AV1Decoder * const pbi,const uint8_t * data,size_t sz)655 static size_t read_metadata_hdr_mdcv(AV1Decoder *const pbi, const uint8_t *data,
656                                      size_t sz) {
657   const int kMdcvPayloadSize = 24;
658   AV1_COMMON *const cm = &pbi->common;
659   if (sz == 0) {
660     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
661                        "HDR MDCV metadata payload is missing");
662   }
663   int bytes_read = get_last_nonzero_byte_index(data, sz);
664   if (bytes_read < 0) {
665     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
666                        "No trailing bits found on HDR MDCV metadata");
667   }
668   if (bytes_read != kMdcvPayloadSize) {
669     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
670                        "Incorrect HDR MDCV metadata payload size");
671   }
672   alloc_read_metadata(pbi, OBU_METADATA_TYPE_HDR_MDCV, data, (size_t)bytes_read,
673                       AOM_MIF_ANY_FRAME);
674   return (size_t)bytes_read;
675 }
676 
scalability_structure(struct aom_read_bit_buffer * rb)677 static void scalability_structure(struct aom_read_bit_buffer *rb) {
678   const int spatial_layers_cnt_minus_1 = aom_rb_read_literal(rb, 2);
679   const int spatial_layer_dimensions_present_flag = aom_rb_read_bit(rb);
680   const int spatial_layer_description_present_flag = aom_rb_read_bit(rb);
681   const int temporal_group_description_present_flag = aom_rb_read_bit(rb);
682   aom_rb_read_literal(rb, 3);  // reserved
683 
684   if (spatial_layer_dimensions_present_flag) {
685     for (int i = 0; i <= spatial_layers_cnt_minus_1; i++) {
686       aom_rb_read_literal(rb, 16);
687       aom_rb_read_literal(rb, 16);
688     }
689   }
690   if (spatial_layer_description_present_flag) {
691     for (int i = 0; i <= spatial_layers_cnt_minus_1; i++) {
692       aom_rb_read_literal(rb, 8);
693     }
694   }
695   if (temporal_group_description_present_flag) {
696     const int temporal_group_size = aom_rb_read_literal(rb, 8);
697     for (int i = 0; i < temporal_group_size; i++) {
698       aom_rb_read_literal(rb, 3);
699       aom_rb_read_bit(rb);
700       aom_rb_read_bit(rb);
701       const int temporal_group_ref_cnt = aom_rb_read_literal(rb, 3);
702       for (int j = 0; j < temporal_group_ref_cnt; j++) {
703         aom_rb_read_literal(rb, 8);
704       }
705     }
706   }
707 }
708 
read_metadata_scalability(struct aom_read_bit_buffer * rb)709 static void read_metadata_scalability(struct aom_read_bit_buffer *rb) {
710   const int scalability_mode_idc = aom_rb_read_literal(rb, 8);
711   if (scalability_mode_idc == SCALABILITY_SS) {
712     scalability_structure(rb);
713   }
714 }
715 
read_metadata_timecode(struct aom_read_bit_buffer * rb)716 static void read_metadata_timecode(struct aom_read_bit_buffer *rb) {
717   aom_rb_read_literal(rb, 5);  // counting_type f(5)
718   const int full_timestamp_flag =
719       aom_rb_read_bit(rb);     // full_timestamp_flag f(1)
720   aom_rb_read_bit(rb);         // discontinuity_flag (f1)
721   aom_rb_read_bit(rb);         // cnt_dropped_flag f(1)
722   aom_rb_read_literal(rb, 9);  // n_frames f(9)
723   if (full_timestamp_flag) {
724     aom_rb_read_literal(rb, 6);  // seconds_value f(6)
725     aom_rb_read_literal(rb, 6);  // minutes_value f(6)
726     aom_rb_read_literal(rb, 5);  // hours_value f(5)
727   } else {
728     const int seconds_flag = aom_rb_read_bit(rb);  // seconds_flag f(1)
729     if (seconds_flag) {
730       aom_rb_read_literal(rb, 6);                    // seconds_value f(6)
731       const int minutes_flag = aom_rb_read_bit(rb);  // minutes_flag f(1)
732       if (minutes_flag) {
733         aom_rb_read_literal(rb, 6);                  // minutes_value f(6)
734         const int hours_flag = aom_rb_read_bit(rb);  // hours_flag f(1)
735         if (hours_flag) {
736           aom_rb_read_literal(rb, 5);  // hours_value f(5)
737         }
738       }
739     }
740   }
741   // time_offset_length f(5)
742   const int time_offset_length = aom_rb_read_literal(rb, 5);
743   if (time_offset_length) {
744     // time_offset_value f(time_offset_length)
745     aom_rb_read_literal(rb, time_offset_length);
746   }
747 }
748 
749 // Returns the last nonzero byte in 'data'. If there is no nonzero byte in
750 // 'data', returns 0.
751 //
752 // Call this function to check the following requirement in the spec:
753 //   This implies that when any payload data is present for this OBU type, at
754 //   least one byte of the payload data (including the trailing bit) shall not
755 //   be equal to 0.
get_last_nonzero_byte(const uint8_t * data,size_t sz)756 static uint8_t get_last_nonzero_byte(const uint8_t *data, size_t sz) {
757   // Scan backward and return on the first nonzero byte.
758   size_t i = sz;
759   while (i != 0) {
760     --i;
761     if (data[i] != 0) return data[i];
762   }
763   return 0;
764 }
765 
766 // Checks the metadata for correct syntax but ignores the parsed metadata.
767 //
768 // On success, returns the number of bytes read from 'data'. On failure, sets
769 // pbi->common.error.error_code and returns 0, or calls aom_internal_error()
770 // and does not return.
read_metadata(AV1Decoder * pbi,const uint8_t * data,size_t sz)771 static size_t read_metadata(AV1Decoder *pbi, const uint8_t *data, size_t sz) {
772   AV1_COMMON *const cm = &pbi->common;
773   size_t type_length;
774   uint64_t type_value;
775   if (aom_uleb_decode(data, sz, &type_value, &type_length) < 0) {
776     cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
777     return 0;
778   }
779   const OBU_METADATA_TYPE metadata_type = (OBU_METADATA_TYPE)type_value;
780   if (metadata_type == 0 || metadata_type >= 6) {
781     // If metadata_type is reserved for future use or a user private value,
782     // ignore the entire OBU and just check trailing bits.
783     if (get_last_nonzero_byte(data + type_length, sz - type_length) == 0) {
784       pbi->common.error.error_code = AOM_CODEC_CORRUPT_FRAME;
785       return 0;
786     }
787     return sz;
788   }
789   if (metadata_type == OBU_METADATA_TYPE_ITUT_T35) {
790     size_t bytes_read =
791         type_length +
792         read_metadata_itut_t35(pbi, data + type_length, sz - type_length);
793     // itu_t_t35_payload_bytes is byte aligned and the first
794     // trailing byte should be 0x80.
795     if (get_last_nonzero_byte(data + bytes_read, sz - bytes_read) != 0x80) {
796       pbi->common.error.error_code = AOM_CODEC_CORRUPT_FRAME;
797       return 0;
798     }
799     return sz;
800   } else if (metadata_type == OBU_METADATA_TYPE_HDR_CLL) {
801     size_t bytes_read =
802         type_length +
803         read_metadata_hdr_cll(pbi, data + type_length, sz - type_length);
804     if (get_last_nonzero_byte(data + bytes_read, sz - bytes_read) != 0x80) {
805       pbi->common.error.error_code = AOM_CODEC_CORRUPT_FRAME;
806       return 0;
807     }
808     return sz;
809   } else if (metadata_type == OBU_METADATA_TYPE_HDR_MDCV) {
810     size_t bytes_read =
811         type_length +
812         read_metadata_hdr_mdcv(pbi, data + type_length, sz - type_length);
813     if (get_last_nonzero_byte(data + bytes_read, sz - bytes_read) != 0x80) {
814       pbi->common.error.error_code = AOM_CODEC_CORRUPT_FRAME;
815       return 0;
816     }
817     return sz;
818   }
819 
820   struct aom_read_bit_buffer rb;
821   av1_init_read_bit_buffer(pbi, &rb, data + type_length, data + sz);
822   if (metadata_type == OBU_METADATA_TYPE_SCALABILITY) {
823     read_metadata_scalability(&rb);
824   } else {
825     assert(metadata_type == OBU_METADATA_TYPE_TIMECODE);
826     read_metadata_timecode(&rb);
827   }
828   if (av1_check_trailing_bits(pbi, &rb) != 0) {
829     // cm->error.error_code is already set.
830     return 0;
831   }
832   assert((rb.bit_offset & 7) == 0);
833   return type_length + (rb.bit_offset >> 3);
834 }
835 
836 // On success, returns 'sz'. On failure, sets pbi->common.error.error_code and
837 // returns 0.
read_padding(AV1_COMMON * const cm,const uint8_t * data,size_t sz)838 static size_t read_padding(AV1_COMMON *const cm, const uint8_t *data,
839                            size_t sz) {
840   // The spec allows a padding OBU to be header-only (i.e., obu_size = 0). So
841   // check trailing bits only if sz > 0.
842   if (sz > 0) {
843     // The payload of a padding OBU is byte aligned. Therefore the first
844     // trailing byte should be 0x80. See https://crbug.com/aomedia/2393.
845     const uint8_t last_nonzero_byte = get_last_nonzero_byte(data, sz);
846     if (last_nonzero_byte != 0x80) {
847       cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
848       return 0;
849     }
850   }
851   return sz;
852 }
853 
854 // On success, returns a boolean that indicates whether the decoding of the
855 // current frame is finished. On failure, sets cm->error.error_code and
856 // 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)857 int aom_decode_frame_from_obus(struct AV1Decoder *pbi, const uint8_t *data,
858                                const uint8_t *data_end,
859                                const uint8_t **p_data_end) {
860   AV1_COMMON *const cm = &pbi->common;
861   int frame_decoding_finished = 0;
862   int is_first_tg_obu_received = 1;
863   uint32_t frame_header_size = 0;
864   ObuHeader obu_header;
865   memset(&obu_header, 0, sizeof(obu_header));
866   pbi->seen_frame_header = 0;
867   pbi->next_start_tile = 0;
868 
869   if (data_end < data) {
870     cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
871     return -1;
872   }
873 
874   // Reset pbi->camera_frame_header_ready to 0 if cm->tiles.large_scale = 0.
875   if (!cm->tiles.large_scale) pbi->camera_frame_header_ready = 0;
876 
877   // decode frame as a series of OBUs
878   while (!frame_decoding_finished && cm->error.error_code == AOM_CODEC_OK) {
879     struct aom_read_bit_buffer rb;
880     size_t payload_size = 0;
881     size_t decoded_payload_size = 0;
882     size_t obu_payload_offset = 0;
883     size_t bytes_read = 0;
884     const size_t bytes_available = data_end - data;
885 
886     if (bytes_available == 0 && !pbi->seen_frame_header) {
887       *p_data_end = data;
888       cm->error.error_code = AOM_CODEC_OK;
889       break;
890     }
891 
892     aom_codec_err_t status =
893         aom_read_obu_header_and_size(data, bytes_available, pbi->is_annexb,
894                                      &obu_header, &payload_size, &bytes_read);
895 
896     if (status != AOM_CODEC_OK) {
897       cm->error.error_code = status;
898       return -1;
899     }
900 
901     // Record obu size header information.
902     pbi->obu_size_hdr.data = data + obu_header.size;
903     pbi->obu_size_hdr.size = bytes_read - obu_header.size;
904 
905     // Note: aom_read_obu_header_and_size() takes care of checking that this
906     // doesn't cause 'data' to advance past 'data_end'.
907     data += bytes_read;
908 
909     if ((size_t)(data_end - data) < payload_size) {
910       cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
911       return -1;
912     }
913 
914     cm->temporal_layer_id = obu_header.temporal_layer_id;
915     cm->spatial_layer_id = obu_header.spatial_layer_id;
916 
917     if (obu_header.type != OBU_TEMPORAL_DELIMITER &&
918         obu_header.type != OBU_SEQUENCE_HEADER &&
919         obu_header.type != OBU_PADDING) {
920       // don't decode obu if it's not in current operating mode
921       if (!is_obu_in_current_operating_point(pbi, obu_header)) {
922         data += payload_size;
923         continue;
924       }
925     }
926 
927     av1_init_read_bit_buffer(pbi, &rb, data, data + payload_size);
928 
929     switch (obu_header.type) {
930       case OBU_TEMPORAL_DELIMITER:
931         decoded_payload_size = read_temporal_delimiter_obu();
932         pbi->seen_frame_header = 0;
933         pbi->next_start_tile = 0;
934         break;
935       case OBU_SEQUENCE_HEADER:
936         decoded_payload_size = read_sequence_header_obu(pbi, &rb);
937         if (cm->error.error_code != AOM_CODEC_OK) return -1;
938         // The sequence header should not change in the middle of a frame.
939         if (pbi->sequence_header_changed && pbi->seen_frame_header) {
940           cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
941           return -1;
942         }
943         break;
944       case OBU_FRAME_HEADER:
945       case OBU_REDUNDANT_FRAME_HEADER:
946       case OBU_FRAME:
947         if (obu_header.type == OBU_REDUNDANT_FRAME_HEADER) {
948           if (!pbi->seen_frame_header) {
949             cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
950             return -1;
951           }
952         } else {
953           // OBU_FRAME_HEADER or OBU_FRAME.
954           if (pbi->seen_frame_header) {
955             cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
956             return -1;
957           }
958         }
959         // Only decode first frame header received
960         if (!pbi->seen_frame_header ||
961             (cm->tiles.large_scale && !pbi->camera_frame_header_ready)) {
962           frame_header_size = read_frame_header_obu(
963               pbi, &rb, data, p_data_end, obu_header.type != OBU_FRAME);
964           pbi->seen_frame_header = 1;
965           if (!pbi->ext_tile_debug && cm->tiles.large_scale)
966             pbi->camera_frame_header_ready = 1;
967         } else {
968           // TODO(wtc): Verify that the frame_header_obu is identical to the
969           // original frame_header_obu. For now just skip frame_header_size
970           // bytes in the bit buffer.
971           if (frame_header_size > payload_size) {
972             cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
973             return -1;
974           }
975           assert(rb.bit_offset == 0);
976           rb.bit_offset = 8 * frame_header_size;
977         }
978 
979         decoded_payload_size = frame_header_size;
980         pbi->frame_header_size = frame_header_size;
981 
982         if (cm->show_existing_frame) {
983           if (obu_header.type == OBU_FRAME) {
984             cm->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
985             return -1;
986           }
987           frame_decoding_finished = 1;
988           pbi->seen_frame_header = 0;
989           break;
990         }
991 
992         // In large scale tile coding, decode the common camera frame header
993         // before any tile list OBU.
994         if (!pbi->ext_tile_debug && pbi->camera_frame_header_ready) {
995           frame_decoding_finished = 1;
996           // Skip the rest of the frame data.
997           decoded_payload_size = payload_size;
998           // Update data_end.
999           *p_data_end = data_end;
1000           break;
1001         }
1002 
1003         if (obu_header.type != OBU_FRAME) break;
1004         obu_payload_offset = frame_header_size;
1005         // Byte align the reader before reading the tile group.
1006         // byte_alignment() has set cm->error.error_code if it returns -1.
1007         if (byte_alignment(cm, &rb)) return -1;
1008         AOM_FALLTHROUGH_INTENDED;  // fall through to read tile group.
1009       case OBU_TILE_GROUP:
1010         if (!pbi->seen_frame_header) {
1011           cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1012           return -1;
1013         }
1014         if (obu_payload_offset > payload_size) {
1015           cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1016           return -1;
1017         }
1018         decoded_payload_size += read_one_tile_group_obu(
1019             pbi, &rb, is_first_tg_obu_received, data + obu_payload_offset,
1020             data + payload_size, p_data_end, &frame_decoding_finished,
1021             obu_header.type == OBU_FRAME);
1022         if (cm->error.error_code != AOM_CODEC_OK) return -1;
1023         is_first_tg_obu_received = 0;
1024         if (frame_decoding_finished) pbi->seen_frame_header = 0;
1025         break;
1026       case OBU_METADATA:
1027         decoded_payload_size = read_metadata(pbi, data, payload_size);
1028         if (cm->error.error_code != AOM_CODEC_OK) return -1;
1029         break;
1030       case OBU_TILE_LIST:
1031         if (CONFIG_NORMAL_TILE_MODE) {
1032           cm->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
1033           return -1;
1034         }
1035 
1036         // This OBU type is purely for the large scale tile coding mode.
1037         // The common camera frame header has to be already decoded.
1038         if (!pbi->camera_frame_header_ready) {
1039           cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1040           return -1;
1041         }
1042 
1043         cm->tiles.large_scale = 1;
1044         av1_set_single_tile_decoding_mode(cm);
1045         decoded_payload_size =
1046             read_and_decode_one_tile_list(pbi, &rb, data, data + payload_size,
1047                                           p_data_end, &frame_decoding_finished);
1048         if (cm->error.error_code != AOM_CODEC_OK) return -1;
1049         break;
1050       case OBU_PADDING:
1051         decoded_payload_size = read_padding(&pbi->common, data, payload_size);
1052         if (cm->error.error_code != AOM_CODEC_OK) return -1;
1053         break;
1054       default:
1055         // Skip unrecognized OBUs
1056         if (payload_size > 0 &&
1057             get_last_nonzero_byte(data, payload_size) == 0) {
1058           cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1059           return -1;
1060         }
1061         decoded_payload_size = payload_size;
1062         break;
1063     }
1064 
1065     // Check that the signalled OBU size matches the actual amount of data read
1066     if (decoded_payload_size > payload_size) {
1067       cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1068       return -1;
1069     }
1070 
1071     // If there are extra padding bytes, they should all be zero
1072     while (decoded_payload_size < payload_size) {
1073       uint8_t padding_byte = data[decoded_payload_size++];
1074       if (padding_byte != 0) {
1075         cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1076         return -1;
1077       }
1078     }
1079 
1080     data += payload_size;
1081   }
1082 
1083   if (cm->error.error_code != AOM_CODEC_OK) return -1;
1084   return frame_decoding_finished;
1085 }
1086