Coverage Report

Created: 2026-08-14 06:18

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