Coverage Report

Created: 2026-02-14 07:00

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