tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

obu.c (45377B)


      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 
     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 
     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 
     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 
     80 static uint32_t read_temporal_delimiter_obu(void) { return 0; }
     81 
     82 // Returns a boolean that indicates success.
     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.
     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.
    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.
    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.
    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.
    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 
    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 
    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 
    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.
    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.
    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.
    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 = (aom_metadata_t **)realloc(
    617      pbi->metadata->metadata_array,
    618      (pbi->metadata->sz + 1) * sizeof(*metadata_array));
    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.
    630 static void read_metadata_itut_t35(AV1Decoder *const pbi, const uint8_t *data,
    631                                   size_t sz, bool has_obu_extension_header) {
    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                      has_obu_extension_header
    661                          ? AOM_MIF_ANY_FRAME_LAYER_SPECIFIC
    662                          : AOM_MIF_ANY_FRAME);
    663 }
    664 
    665 // On success, returns the number of bytes read from 'data'. On failure, calls
    666 // aom_internal_error() and does not return.
    667 static size_t read_metadata_hdr_cll(AV1Decoder *const pbi, const uint8_t *data,
    668                                    size_t sz) {
    669  const size_t kHdrCllPayloadSize = 4;
    670  if (sz < kHdrCllPayloadSize) {
    671    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
    672                       "Incorrect HDR CLL metadata payload size");
    673  }
    674  alloc_read_metadata(pbi, OBU_METADATA_TYPE_HDR_CLL, data, kHdrCllPayloadSize,
    675                      AOM_MIF_ANY_FRAME);
    676  return kHdrCllPayloadSize;
    677 }
    678 
    679 // On success, returns the number of bytes read from 'data'. On failure, calls
    680 // aom_internal_error() and does not return.
    681 static size_t read_metadata_hdr_mdcv(AV1Decoder *const pbi, const uint8_t *data,
    682                                     size_t sz) {
    683  const size_t kMdcvPayloadSize = 24;
    684  if (sz < kMdcvPayloadSize) {
    685    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
    686                       "Incorrect HDR MDCV metadata payload size");
    687  }
    688  alloc_read_metadata(pbi, OBU_METADATA_TYPE_HDR_MDCV, data, kMdcvPayloadSize,
    689                      AOM_MIF_ANY_FRAME);
    690  return kMdcvPayloadSize;
    691 }
    692 
    693 static void scalability_structure(struct aom_read_bit_buffer *rb) {
    694  const int spatial_layers_cnt_minus_1 = aom_rb_read_literal(rb, 2);
    695  const int spatial_layer_dimensions_present_flag = aom_rb_read_bit(rb);
    696  const int spatial_layer_description_present_flag = aom_rb_read_bit(rb);
    697  const int temporal_group_description_present_flag = aom_rb_read_bit(rb);
    698  // scalability_structure_reserved_3bits must be set to zero and be ignored by
    699  // decoders.
    700  aom_rb_read_literal(rb, 3);
    701 
    702  if (spatial_layer_dimensions_present_flag) {
    703    for (int i = 0; i <= spatial_layers_cnt_minus_1; i++) {
    704      aom_rb_read_literal(rb, 16);
    705      aom_rb_read_literal(rb, 16);
    706    }
    707  }
    708  if (spatial_layer_description_present_flag) {
    709    for (int i = 0; i <= spatial_layers_cnt_minus_1; i++) {
    710      aom_rb_read_literal(rb, 8);
    711    }
    712  }
    713  if (temporal_group_description_present_flag) {
    714    const int temporal_group_size = aom_rb_read_literal(rb, 8);
    715    for (int i = 0; i < temporal_group_size; i++) {
    716      aom_rb_read_literal(rb, 3);
    717      aom_rb_read_bit(rb);
    718      aom_rb_read_bit(rb);
    719      const int temporal_group_ref_cnt = aom_rb_read_literal(rb, 3);
    720      for (int j = 0; j < temporal_group_ref_cnt; j++) {
    721        aom_rb_read_literal(rb, 8);
    722      }
    723    }
    724  }
    725 }
    726 
    727 static void read_metadata_scalability(struct aom_read_bit_buffer *rb) {
    728  const int scalability_mode_idc = aom_rb_read_literal(rb, 8);
    729  if (scalability_mode_idc == SCALABILITY_SS) {
    730    scalability_structure(rb);
    731  }
    732 }
    733 
    734 static void read_metadata_timecode(struct aom_read_bit_buffer *rb) {
    735  aom_rb_read_literal(rb, 5);  // counting_type f(5)
    736  const int full_timestamp_flag =
    737      aom_rb_read_bit(rb);     // full_timestamp_flag f(1)
    738  aom_rb_read_bit(rb);         // discontinuity_flag (f1)
    739  aom_rb_read_bit(rb);         // cnt_dropped_flag f(1)
    740  aom_rb_read_literal(rb, 9);  // n_frames f(9)
    741  if (full_timestamp_flag) {
    742    aom_rb_read_literal(rb, 6);  // seconds_value f(6)
    743    aom_rb_read_literal(rb, 6);  // minutes_value f(6)
    744    aom_rb_read_literal(rb, 5);  // hours_value f(5)
    745  } else {
    746    const int seconds_flag = aom_rb_read_bit(rb);  // seconds_flag f(1)
    747    if (seconds_flag) {
    748      aom_rb_read_literal(rb, 6);                    // seconds_value f(6)
    749      const int minutes_flag = aom_rb_read_bit(rb);  // minutes_flag f(1)
    750      if (minutes_flag) {
    751        aom_rb_read_literal(rb, 6);                  // minutes_value f(6)
    752        const int hours_flag = aom_rb_read_bit(rb);  // hours_flag f(1)
    753        if (hours_flag) {
    754          aom_rb_read_literal(rb, 5);  // hours_value f(5)
    755        }
    756      }
    757    }
    758  }
    759  // time_offset_length f(5)
    760  const int time_offset_length = aom_rb_read_literal(rb, 5);
    761  if (time_offset_length) {
    762    // time_offset_value f(time_offset_length)
    763    aom_rb_read_literal(rb, time_offset_length);
    764  }
    765 }
    766 
    767 // Returns the last nonzero byte in 'data'. If there is no nonzero byte in
    768 // 'data', returns 0.
    769 //
    770 // Call this function to check the following requirement in the spec:
    771 //   This implies that when any payload data is present for this OBU type, at
    772 //   least one byte of the payload data (including the trailing bit) shall not
    773 //   be equal to 0.
    774 static uint8_t get_last_nonzero_byte(const uint8_t *data, size_t sz) {
    775  // Scan backward and return on the first nonzero byte.
    776  size_t i = sz;
    777  while (i != 0) {
    778    --i;
    779    if (data[i] != 0) return data[i];
    780  }
    781  return 0;
    782 }
    783 
    784 // Checks the metadata for correct syntax but ignores the parsed metadata.
    785 //
    786 // On success, returns the number of bytes read from 'data'. On failure, sets
    787 // pbi->common.error.error_code and returns 0, or calls aom_internal_error()
    788 // and does not return.
    789 static size_t read_metadata(AV1Decoder *pbi, const uint8_t *data, size_t sz,
    790                            bool has_obu_extension_header) {
    791  size_t type_length;
    792  uint64_t type_value;
    793  if (aom_uleb_decode(data, sz, &type_value, &type_length) < 0) {
    794    pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
    795    return 0;
    796  }
    797  const OBU_METADATA_TYPE metadata_type = (OBU_METADATA_TYPE)type_value;
    798  if (metadata_type == 0 || metadata_type >= 6) {
    799    // If metadata_type is reserved for future use or a user private value,
    800    // ignore the entire OBU and just check trailing bits.
    801    if (get_last_nonzero_byte(data + type_length, sz - type_length) == 0) {
    802      pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
    803      return 0;
    804    }
    805    return sz;
    806  }
    807  if (metadata_type == OBU_METADATA_TYPE_ITUT_T35) {
    808    // read_metadata_itut_t35() checks trailing bits.
    809    read_metadata_itut_t35(pbi, data + type_length, sz - type_length,
    810                           has_obu_extension_header);
    811    return sz;
    812  } else if (metadata_type == OBU_METADATA_TYPE_HDR_CLL) {
    813    size_t bytes_read =
    814        type_length +
    815        read_metadata_hdr_cll(pbi, data + type_length, sz - type_length);
    816    if (get_last_nonzero_byte(data + bytes_read, sz - bytes_read) != 0x80) {
    817      pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
    818      return 0;
    819    }
    820    return sz;
    821  } else if (metadata_type == OBU_METADATA_TYPE_HDR_MDCV) {
    822    size_t bytes_read =
    823        type_length +
    824        read_metadata_hdr_mdcv(pbi, data + type_length, sz - type_length);
    825    if (get_last_nonzero_byte(data + bytes_read, sz - bytes_read) != 0x80) {
    826      pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
    827      return 0;
    828    }
    829    return sz;
    830  }
    831 
    832  struct aom_read_bit_buffer rb;
    833  av1_init_read_bit_buffer(pbi, &rb, data + type_length, data + sz);
    834  if (metadata_type == OBU_METADATA_TYPE_SCALABILITY) {
    835    read_metadata_scalability(&rb);
    836  } else {
    837    assert(metadata_type == OBU_METADATA_TYPE_TIMECODE);
    838    read_metadata_timecode(&rb);
    839  }
    840  if (av1_check_trailing_bits(pbi, &rb) != 0) {
    841    // pbi->error.error_code is already set.
    842    return 0;
    843  }
    844  assert((rb.bit_offset & 7) == 0);
    845  return type_length + (rb.bit_offset >> 3);
    846 }
    847 
    848 // On success, returns 'sz'. On failure, sets pbi->common.error.error_code and
    849 // returns 0.
    850 static size_t read_padding(AV1_COMMON *const cm, const uint8_t *data,
    851                           size_t sz) {
    852  // The spec allows a padding OBU to be header-only (i.e., obu_size = 0). So
    853  // check trailing bits only if sz > 0.
    854  if (sz > 0) {
    855    // The payload of a padding OBU is byte aligned. Therefore the first
    856    // trailing byte should be 0x80. See https://crbug.com/aomedia/2393.
    857    const uint8_t last_nonzero_byte = get_last_nonzero_byte(data, sz);
    858    if (last_nonzero_byte != 0x80) {
    859      cm->error->error_code = AOM_CODEC_CORRUPT_FRAME;
    860      return 0;
    861    }
    862  }
    863  return sz;
    864 }
    865 
    866 // On success, returns a boolean that indicates whether the decoding of the
    867 // current frame is finished. On failure, sets pbi->error.error_code and
    868 // returns -1.
    869 int aom_decode_frame_from_obus(struct AV1Decoder *pbi, const uint8_t *data,
    870                               const uint8_t *data_end,
    871                               const uint8_t **p_data_end) {
    872 #if CONFIG_COLLECT_COMPONENT_TIMING
    873  start_timing(pbi, aom_decode_frame_from_obus_time);
    874 #endif
    875  AV1_COMMON *const cm = &pbi->common;
    876  int frame_decoding_finished = 0;
    877  int is_first_tg_obu_received = 1;
    878  // Whenever pbi->seen_frame_header is set to 1, frame_header is set to the
    879  // beginning of the frame_header_obu and frame_header_size is set to its
    880  // size. This allows us to check if a redundant frame_header_obu is a copy
    881  // of the previous frame_header_obu.
    882  //
    883  // Initialize frame_header to a dummy nonnull pointer, otherwise the Clang
    884  // Static Analyzer in clang 7.0.1 will falsely warn that a null pointer is
    885  // passed as an argument to a 'nonnull' parameter of memcmp(). The initial
    886  // value will not be used.
    887  const uint8_t *frame_header = data;
    888  uint32_t frame_header_size = 0;
    889  ObuHeader obu_header;
    890  memset(&obu_header, 0, sizeof(obu_header));
    891  pbi->seen_frame_header = 0;
    892  pbi->next_start_tile = 0;
    893  pbi->num_tile_groups = 0;
    894 
    895  if (data_end < data) {
    896    pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
    897    return -1;
    898  }
    899 
    900  // Reset pbi->camera_frame_header_ready to 0 if cm->tiles.large_scale = 0.
    901  if (!cm->tiles.large_scale) pbi->camera_frame_header_ready = 0;
    902 
    903  // decode frame as a series of OBUs
    904  while (!frame_decoding_finished && pbi->error.error_code == AOM_CODEC_OK) {
    905    struct aom_read_bit_buffer rb;
    906    size_t payload_size = 0;
    907    size_t decoded_payload_size = 0;
    908    size_t obu_payload_offset = 0;
    909    size_t bytes_read = 0;
    910    const size_t bytes_available = data_end - data;
    911 
    912    if (bytes_available == 0 && !pbi->seen_frame_header) {
    913      *p_data_end = data;
    914      pbi->error.error_code = AOM_CODEC_OK;
    915      break;
    916    }
    917 
    918    aom_codec_err_t status =
    919        aom_read_obu_header_and_size(data, bytes_available, pbi->is_annexb,
    920                                     &obu_header, &payload_size, &bytes_read);
    921 
    922    if (status != AOM_CODEC_OK) {
    923      pbi->error.error_code = status;
    924      return -1;
    925    }
    926 
    927    // Record obu size header information.
    928    pbi->obu_size_hdr.data = data + obu_header.size;
    929    pbi->obu_size_hdr.size = bytes_read - obu_header.size;
    930 
    931    // Note: aom_read_obu_header_and_size() takes care of checking that this
    932    // doesn't cause 'data' to advance past 'data_end'.
    933    data += bytes_read;
    934 
    935    if ((size_t)(data_end - data) < payload_size) {
    936      pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
    937      return -1;
    938    }
    939 
    940    cm->temporal_layer_id = obu_header.temporal_layer_id;
    941    cm->spatial_layer_id = obu_header.spatial_layer_id;
    942 
    943    if (obu_header.type != OBU_TEMPORAL_DELIMITER &&
    944        obu_header.type != OBU_SEQUENCE_HEADER) {
    945      // don't decode obu if it's not in current operating mode
    946      if (!is_obu_in_current_operating_point(pbi, &obu_header)) {
    947        data += payload_size;
    948        continue;
    949      }
    950    }
    951 
    952    av1_init_read_bit_buffer(pbi, &rb, data, data + payload_size);
    953 
    954    switch (obu_header.type) {
    955      case OBU_TEMPORAL_DELIMITER:
    956        decoded_payload_size = read_temporal_delimiter_obu();
    957        if (pbi->seen_frame_header) {
    958          // A new temporal unit has started, but the frame in the previous
    959          // temporal unit is incomplete.
    960          pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
    961          return -1;
    962        }
    963        break;
    964      case OBU_SEQUENCE_HEADER:
    965        decoded_payload_size = read_sequence_header_obu(pbi, &rb);
    966        if (pbi->error.error_code != AOM_CODEC_OK) return -1;
    967        // The sequence header should not change in the middle of a frame.
    968        if (pbi->sequence_header_changed && pbi->seen_frame_header) {
    969          pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
    970          return -1;
    971        }
    972        break;
    973      case OBU_FRAME_HEADER:
    974      case OBU_REDUNDANT_FRAME_HEADER:
    975      case OBU_FRAME:
    976        if (obu_header.type == OBU_REDUNDANT_FRAME_HEADER) {
    977          if (!pbi->seen_frame_header) {
    978            pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
    979            return -1;
    980          }
    981        } else {
    982          // OBU_FRAME_HEADER or OBU_FRAME.
    983          if (pbi->seen_frame_header) {
    984            pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
    985            return -1;
    986          }
    987        }
    988        // Only decode first frame header received
    989        if (!pbi->seen_frame_header ||
    990            (cm->tiles.large_scale && !pbi->camera_frame_header_ready)) {
    991          frame_header_size = read_frame_header_obu(
    992              pbi, &rb, data, p_data_end, obu_header.type != OBU_FRAME);
    993          frame_header = data;
    994          pbi->seen_frame_header = 1;
    995          if (!pbi->ext_tile_debug && cm->tiles.large_scale)
    996            pbi->camera_frame_header_ready = 1;
    997        } else {
    998          // Verify that the frame_header_obu is identical to the original
    999          // frame_header_obu.
   1000          if (frame_header_size > payload_size ||
   1001              memcmp(data, frame_header, frame_header_size) != 0) {
   1002            pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
   1003            return -1;
   1004          }
   1005          assert(rb.bit_offset == 0);
   1006          rb.bit_offset = 8 * frame_header_size;
   1007        }
   1008 
   1009        decoded_payload_size = frame_header_size;
   1010        pbi->frame_header_size = frame_header_size;
   1011        cm->cur_frame->temporal_id = obu_header.temporal_layer_id;
   1012        cm->cur_frame->spatial_id = obu_header.spatial_layer_id;
   1013 
   1014        if (cm->show_existing_frame) {
   1015          if (obu_header.type == OBU_FRAME) {
   1016            pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
   1017            return -1;
   1018          }
   1019          frame_decoding_finished = 1;
   1020          pbi->seen_frame_header = 0;
   1021 
   1022          if (cm->show_frame &&
   1023              !cm->seq_params->order_hint_info.enable_order_hint) {
   1024            ++cm->current_frame.frame_number;
   1025          }
   1026          break;
   1027        }
   1028 
   1029        // In large scale tile coding, decode the common camera frame header
   1030        // before any tile list OBU.
   1031        if (!pbi->ext_tile_debug && pbi->camera_frame_header_ready) {
   1032          frame_decoding_finished = 1;
   1033          // Skip the rest of the frame data.
   1034          decoded_payload_size = payload_size;
   1035          // Update data_end.
   1036          *p_data_end = data_end;
   1037          break;
   1038        }
   1039 
   1040        if (obu_header.type != OBU_FRAME) break;
   1041        obu_payload_offset = frame_header_size;
   1042        // Byte align the reader before reading the tile group.
   1043        // byte_alignment() has set pbi->error.error_code if it returns -1.
   1044        if (byte_alignment(cm, &rb)) return -1;
   1045        AOM_FALLTHROUGH_INTENDED;  // fall through to read tile group.
   1046      case OBU_TILE_GROUP:
   1047        if (!pbi->seen_frame_header) {
   1048          pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
   1049          return -1;
   1050        }
   1051        if (obu_payload_offset > payload_size) {
   1052          pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
   1053          return -1;
   1054        }
   1055        decoded_payload_size += read_one_tile_group_obu(
   1056            pbi, &rb, is_first_tg_obu_received, data + obu_payload_offset,
   1057            data + payload_size, p_data_end, &frame_decoding_finished,
   1058            obu_header.type == OBU_FRAME);
   1059        if (pbi->error.error_code != AOM_CODEC_OK) return -1;
   1060        is_first_tg_obu_received = 0;
   1061        if (frame_decoding_finished) {
   1062          pbi->seen_frame_header = 0;
   1063          pbi->next_start_tile = 0;
   1064        }
   1065        pbi->num_tile_groups++;
   1066        break;
   1067      case OBU_METADATA: {
   1068        decoded_payload_size =
   1069            read_metadata(pbi, data, payload_size, obu_header.has_extension);
   1070        if (pbi->error.error_code != AOM_CODEC_OK) return -1;
   1071        break;
   1072      }
   1073      case OBU_TILE_LIST:
   1074        if (CONFIG_NORMAL_TILE_MODE) {
   1075          pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
   1076          return -1;
   1077        }
   1078 
   1079        // This OBU type is purely for the large scale tile coding mode.
   1080        // The common camera frame header has to be already decoded.
   1081        if (!pbi->camera_frame_header_ready) {
   1082          pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
   1083          return -1;
   1084        }
   1085 
   1086        cm->tiles.large_scale = 1;
   1087        av1_set_single_tile_decoding_mode(cm);
   1088        decoded_payload_size =
   1089            read_and_decode_one_tile_list(pbi, &rb, data, data + payload_size,
   1090                                          p_data_end, &frame_decoding_finished);
   1091        if (pbi->error.error_code != AOM_CODEC_OK) return -1;
   1092        break;
   1093      case OBU_PADDING:
   1094        decoded_payload_size = read_padding(cm, data, payload_size);
   1095        if (pbi->error.error_code != AOM_CODEC_OK) return -1;
   1096        break;
   1097      default:
   1098        // Skip unrecognized OBUs
   1099        if (payload_size > 0 &&
   1100            get_last_nonzero_byte(data, payload_size) == 0) {
   1101          pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
   1102          return -1;
   1103        }
   1104        decoded_payload_size = payload_size;
   1105        break;
   1106    }
   1107 
   1108    // Check that the signalled OBU size matches the actual amount of data read
   1109    if (decoded_payload_size > payload_size) {
   1110      pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
   1111      return -1;
   1112    }
   1113 
   1114    // If there are extra padding bytes, they should all be zero
   1115    while (decoded_payload_size < payload_size) {
   1116      uint8_t padding_byte = data[decoded_payload_size++];
   1117      if (padding_byte != 0) {
   1118        pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
   1119        return -1;
   1120      }
   1121    }
   1122 
   1123    data += payload_size;
   1124  }
   1125 
   1126  if (pbi->error.error_code != AOM_CODEC_OK) return -1;
   1127 
   1128 #if CONFIG_COLLECT_COMPONENT_TIMING
   1129  end_timing(pbi, aom_decode_frame_from_obus_time);
   1130 
   1131  // Print out timing information.
   1132  int i;
   1133  fprintf(stderr,
   1134          "\n Frame number: %d, Frame type: %s, Show Frame: %d, Show existing "
   1135          "frame: %d\n",
   1136          cm->current_frame.frame_number,
   1137          get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame,
   1138          cm->show_existing_frame);
   1139  // Exclude show_existing_frame since it doesn't take much time.
   1140  if (!cm->show_existing_frame) {
   1141    for (i = 0; i < kTimingComponents; i++) {
   1142      pbi->component_time[i] += pbi->frame_component_time[i];
   1143      fprintf(stderr, " %s:  %" PRId64 " us (total: %" PRId64 " us)\n",
   1144              get_component_name(i), pbi->frame_component_time[i],
   1145              pbi->component_time[i]);
   1146      pbi->frame_component_time[i] = 0;
   1147    }
   1148  }
   1149 #endif
   1150 
   1151  return frame_decoding_finished;
   1152 }