Coverage Report

Created: 2025-06-22 08:04

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