Coverage Report

Created: 2025-07-23 06:32

/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
193k
    unsigned int *number_temporal_layers) {
32
  // derive number of spatial/temporal layers from operating_point_idc
33
34
193k
  if (!number_spatial_layers || !number_temporal_layers)
35
0
    return AOM_CODEC_INVALID_PARAM;
36
37
193k
  if (operating_point_idc == 0) {
38
137k
    *number_temporal_layers = 1;
39
137k
    *number_spatial_layers = 1;
40
137k
  } else {
41
56.5k
    *number_spatial_layers = 0;
42
56.5k
    *number_temporal_layers = 0;
43
282k
    for (int j = 0; j < MAX_NUM_SPATIAL_LAYERS; j++) {
44
226k
      *number_spatial_layers +=
45
226k
          (operating_point_idc >> (j + MAX_NUM_TEMPORAL_LAYERS)) & 0x1;
46
226k
    }
47
508k
    for (int j = 0; j < MAX_NUM_TEMPORAL_LAYERS; j++) {
48
452k
      *number_temporal_layers += (operating_point_idc >> j) & 0x1;
49
452k
    }
50
56.5k
  }
51
52
193k
  return AOM_CODEC_OK;
53
193k
}
54
55
static int is_obu_in_current_operating_point(AV1Decoder *pbi,
56
236k
                                             const ObuHeader *obu_header) {
57
236k
  if (!pbi->current_operating_point || !obu_header->has_extension) {
58
235k
    return 1;
59
235k
  }
60
61
1.38k
  if ((pbi->current_operating_point >> obu_header->temporal_layer_id) & 0x1 &&
62
1.38k
      (pbi->current_operating_point >> (obu_header->spatial_layer_id + 8)) &
63
535
          0x1) {
64
204
    return 1;
65
204
  }
66
1.18k
  return 0;
67
1.38k
}
68
69
static int byte_alignment(AV1_COMMON *const cm,
70
303k
                          struct aom_read_bit_buffer *const rb) {
71
755k
  while (rb->bit_offset & 7) {
72
457k
    if (aom_rb_read_bit(rb)) {
73
5.42k
      cm->error->error_code = AOM_CODEC_CORRUPT_FRAME;
74
5.42k
      return -1;
75
5.42k
    }
76
457k
  }
77
297k
  return 0;
78
303k
}
79
80
99.7k
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
94.4k
                                struct aom_read_bit_buffer *rb) {
85
94.4k
  *seq_level_idx = aom_rb_read_literal(rb, LEVEL_BITS);
86
94.4k
  if (!is_valid_seq_level_idx(*seq_level_idx)) return 0;
87
88.5k
  return 1;
88
94.4k
}
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
56.9k
                                      const SequenceHeader *seq_params_new) {
97
56.9k
  return !memcmp(seq_params_old, seq_params_new,
98
56.9k
                 offsetof(SequenceHeader, op_params));
99
56.9k
}
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
86.2k
                                         struct aom_read_bit_buffer *rb) {
106
86.2k
  AV1_COMMON *const cm = &pbi->common;
107
86.2k
  const uint32_t saved_bit_offset = rb->bit_offset;
108
109
  // Verify rb has been configured to report errors.
110
86.2k
  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
86.2k
  SequenceHeader sh = *cm->seq_params;
115
86.2k
  SequenceHeader *const seq_params = &sh;
116
117
86.2k
  seq_params->profile = av1_read_profile(rb);
118
86.2k
  if (seq_params->profile > CONFIG_MAX_DECODE_PROFILE) {
119
252
    pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
120
252
    return 0;
121
252
  }
122
123
  // Still picture or not
124
85.9k
  seq_params->still_picture = aom_rb_read_bit(rb);
125
85.9k
  seq_params->reduced_still_picture_hdr = aom_rb_read_bit(rb);
126
  // Video must have reduced_still_picture_hdr = 0
127
85.9k
  if (!seq_params->still_picture && seq_params->reduced_still_picture_hdr) {
128
25
    pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
129
25
    return 0;
130
25
  }
131
132
85.9k
  if (seq_params->reduced_still_picture_hdr) {
133
17.3k
    seq_params->timing_info_present = 0;
134
17.3k
    seq_params->decoder_model_info_present_flag = 0;
135
17.3k
    seq_params->display_model_info_present_flag = 0;
136
17.3k
    seq_params->operating_points_cnt_minus_1 = 0;
137
17.3k
    seq_params->operating_point_idc[0] = 0;
138
17.3k
    seq_params->has_nonzero_operating_point_idc = false;
139
17.3k
    if (!read_bitstream_level(&seq_params->seq_level_idx[0], rb)) {
140
656
      pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
141
656
      return 0;
142
656
    }
143
16.6k
    seq_params->tier[0] = 0;
144
16.6k
    seq_params->op_params[0].decoder_model_param_present_flag = 0;
145
16.6k
    seq_params->op_params[0].display_model_param_present_flag = 0;
146
68.6k
  } else {
147
68.6k
    seq_params->timing_info_present = aom_rb_read_bit(rb);
148
68.6k
    if (seq_params->timing_info_present) {
149
5.50k
      av1_read_timing_info_header(&seq_params->timing_info, &pbi->error, rb);
150
151
5.50k
      seq_params->decoder_model_info_present_flag = aom_rb_read_bit(rb);
152
5.50k
      if (seq_params->decoder_model_info_present_flag)
153
1.15k
        av1_read_decoder_model_info(&seq_params->decoder_model_info, rb);
154
63.1k
    } else {
155
63.1k
      seq_params->decoder_model_info_present_flag = 0;
156
63.1k
    }
157
68.6k
    seq_params->display_model_info_present_flag = aom_rb_read_bit(rb);
158
68.6k
    seq_params->operating_points_cnt_minus_1 =
159
68.6k
        aom_rb_read_literal(rb, OP_POINTS_CNT_MINUS_1_BITS);
160
68.6k
    seq_params->has_nonzero_operating_point_idc = false;
161
140k
    for (int i = 0; i < seq_params->operating_points_cnt_minus_1 + 1; i++) {
162
77.2k
      seq_params->operating_point_idc[i] =
163
77.2k
          aom_rb_read_literal(rb, OP_POINTS_IDC_BITS);
164
77.2k
      if (seq_params->operating_point_idc[i] != 0)
165
26.3k
        seq_params->has_nonzero_operating_point_idc = true;
166
77.2k
      if (!read_bitstream_level(&seq_params->seq_level_idx[i], rb)) {
167
5.25k
        pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
168
5.25k
        return 0;
169
5.25k
      }
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
71.9k
      if (seq_params->seq_level_idx[i] >= SEQ_LEVEL_4_0)
173
5.92k
        seq_params->tier[i] = aom_rb_read_bit(rb);
174
66.0k
      else
175
66.0k
        seq_params->tier[i] = 0;
176
71.9k
      if (seq_params->decoder_model_info_present_flag) {
177
3.14k
        seq_params->op_params[i].decoder_model_param_present_flag =
178
3.14k
            aom_rb_read_bit(rb);
179
3.14k
        if (seq_params->op_params[i].decoder_model_param_present_flag)
180
2.00k
          av1_read_op_parameters_info(&seq_params->op_params[i],
181
2.00k
                                      seq_params->decoder_model_info
182
2.00k
                                          .encoder_decoder_buffer_delay_length,
183
2.00k
                                      rb);
184
68.8k
      } else {
185
68.8k
        seq_params->op_params[i].decoder_model_param_present_flag = 0;
186
68.8k
      }
187
71.9k
      if (seq_params->timing_info_present &&
188
71.9k
          (seq_params->timing_info.equal_picture_interval ||
189
7.97k
           seq_params->op_params[i].decoder_model_param_present_flag)) {
190
4.87k
        seq_params->op_params[i].bitrate = av1_max_level_bitrate(
191
4.87k
            seq_params->profile, seq_params->seq_level_idx[i],
192
4.87k
            seq_params->tier[i]);
193
        // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass
194
        // the check
195
4.87k
        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
4.87k
        seq_params->op_params[i].buffer_size = seq_params->op_params[i].bitrate;
201
4.87k
      }
202
71.9k
      if (seq_params->timing_info_present &&
203
71.9k
          seq_params->timing_info.equal_picture_interval &&
204
71.9k
          !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
2.94k
        seq_params->op_params[i].decoder_buffer_delay = 70000;
208
2.94k
        seq_params->op_params[i].encoder_buffer_delay = 20000;
209
2.94k
        seq_params->op_params[i].low_delay_mode_flag = 0;
210
2.94k
      }
211
212
71.9k
      if (seq_params->display_model_info_present_flag) {
213
6.80k
        seq_params->op_params[i].display_model_param_present_flag =
214
6.80k
            aom_rb_read_bit(rb);
215
6.80k
        if (seq_params->op_params[i].display_model_param_present_flag) {
216
2.13k
          seq_params->op_params[i].initial_display_delay =
217
2.13k
              aom_rb_read_literal(rb, 4) + 1;
218
2.13k
          if (seq_params->op_params[i].initial_display_delay > 10)
219
706
            aom_internal_error(
220
706
                &pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
221
706
                "AV1 does not support more than 10 decoded frames delay");
222
4.67k
        } else {
223
4.67k
          seq_params->op_params[i].initial_display_delay = 10;
224
4.67k
        }
225
65.1k
      } else {
226
65.1k
        seq_params->op_params[i].display_model_param_present_flag = 0;
227
65.1k
        seq_params->op_params[i].initial_display_delay = 10;
228
65.1k
      }
229
71.9k
    }
230
68.6k
  }
231
  // This decoder supports all levels.  Choose operating point provided by
232
  // external means
233
80.0k
  int operating_point = pbi->operating_point;
234
80.0k
  if (operating_point < 0 ||
235
80.0k
      operating_point > seq_params->operating_points_cnt_minus_1)
236
23.6k
    operating_point = 0;
237
80.0k
  pbi->current_operating_point =
238
80.0k
      seq_params->operating_point_idc[operating_point];
239
80.0k
  if (aom_get_num_layers_from_operating_point_idc(
240
80.0k
          pbi->current_operating_point, &pbi->number_spatial_layers,
241
80.0k
          &pbi->number_temporal_layers) != AOM_CODEC_OK) {
242
0
    pbi->error.error_code = AOM_CODEC_ERROR;
243
0
    return 0;
244
0
  }
245
246
80.0k
  av1_read_sequence_header(cm, rb, seq_params);
247
248
80.0k
  av1_read_color_config(rb, pbi->allow_lowbitdepth, seq_params, &pbi->error);
249
80.0k
  if (!(seq_params->subsampling_x == 0 && seq_params->subsampling_y == 0) &&
250
80.0k
      !(seq_params->subsampling_x == 1 && seq_params->subsampling_y == 1) &&
251
80.0k
      !(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
80.0k
  seq_params->film_grain_params_present = aom_rb_read_bit(rb);
259
260
80.0k
  if (av1_check_trailing_bits(pbi, rb) != 0) {
261
    // pbi->error.error_code is already set.
262
2.55k
    return 0;
263
2.55k
  }
264
265
  // If a sequence header has been decoded before, we check if the new
266
  // one is consistent with the old one.
267
77.4k
  if (pbi->sequence_header_ready) {
268
56.9k
    if (!are_seq_headers_consistent(cm->seq_params, seq_params))
269
28.6k
      pbi->sequence_header_changed = 1;
270
56.9k
  }
271
272
77.4k
  *cm->seq_params = *seq_params;
273
77.4k
  pbi->sequence_header_ready = 1;
274
275
77.4k
  return ((rb->bit_offset - saved_bit_offset + 7) >> 3);
276
80.0k
}
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
218k
                                      int trailing_bits_present) {
286
218k
  const uint32_t hdr_size =
287
218k
      av1_decode_frame_headers_and_setup(pbi, rb, trailing_bits_present);
288
218k
  const AV1_COMMON *cm = &pbi->common;
289
218k
  if (cm->show_existing_frame) {
290
333
    *p_data_end = data + hdr_size;
291
333
  }
292
218k
  return hdr_size;
293
218k
}
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
150k
                                      int tile_start_implicit) {
301
150k
  AV1_COMMON *const cm = &pbi->common;
302
150k
  CommonTileParams *const tiles = &cm->tiles;
303
150k
  uint32_t saved_bit_offset = rb->bit_offset;
304
150k
  int tile_start_and_end_present_flag = 0;
305
150k
  const int num_tiles = tiles->rows * tiles->cols;
306
307
150k
  if (!tiles->large_scale && num_tiles > 1) {
308
6.68k
    tile_start_and_end_present_flag = aom_rb_read_bit(rb);
309
6.68k
    if (tile_start_implicit && tile_start_and_end_present_flag) {
310
923
      aom_internal_error(
311
923
          &pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
312
923
          "For OBU_FRAME type obu tile_start_and_end_present_flag must be 0");
313
923
      return -1;
314
923
    }
315
6.68k
  }
316
149k
  if (tiles->large_scale || num_tiles == 1 ||
317
149k
      !tile_start_and_end_present_flag) {
318
149k
    *start_tile = 0;
319
149k
    *end_tile = num_tiles - 1;
320
149k
  } else {
321
85
    int tile_bits = tiles->log2_rows + tiles->log2_cols;
322
85
    *start_tile = aom_rb_read_literal(rb, tile_bits);
323
85
    *end_tile = aom_rb_read_literal(rb, tile_bits);
324
85
  }
325
149k
  if (*start_tile != pbi->next_start_tile) {
326
2
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
327
2
                       "tg_start (%d) must be equal to %d", *start_tile,
328
2
                       pbi->next_start_tile);
329
2
    return -1;
330
2
  }
331
149k
  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
149k
  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
    return -1;
343
1
  }
344
149k
  pbi->next_start_tile = (*end_tile == num_tiles - 1) ? 0 : *end_tile + 1;
345
346
149k
  return ((rb->bit_offset - saved_bit_offset + 7) >> 3);
347
149k
}
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
150k
    int *is_last_tg, int tile_start_implicit) {
355
150k
  AV1_COMMON *const cm = &pbi->common;
356
150k
  int start_tile, end_tile;
357
150k
  int32_t header_size, tg_payload_size;
358
359
150k
  assert((rb->bit_offset & 7) == 0);
360
150k
  assert(rb->bit_buffer + aom_rb_bytes_read(rb) == data);
361
362
150k
  header_size = read_tile_group_header(pbi, rb, &start_tile, &end_tile,
363
150k
                                       tile_start_implicit);
364
150k
  if (header_size == -1 || byte_alignment(cm, rb)) return 0;
365
148k
  data += header_size;
366
148k
  av1_decode_tg_tiles_and_wrapup(pbi, data, data_end, p_data_end, start_tile,
367
148k
                                 end_tile, is_first_tg);
368
369
148k
  tg_payload_size = (uint32_t)(*p_data_end - data);
370
371
148k
  *is_last_tg = end_tile == cm->tiles.rows * cm->tiles.cols - 1;
372
148k
  return header_size + tg_payload_size;
373
150k
}
374
375
static void alloc_tile_list_buffer(AV1Decoder *pbi, int tile_width_in_pixels,
376
1.14k
                                   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
1.14k
  AV1_COMMON *const cm = &pbi->common;
382
1.14k
  const int output_frame_width =
383
1.14k
      (pbi->output_frame_width_in_tiles_minus_1 + 1) * tile_width_in_pixels;
384
1.14k
  const int output_frame_height =
385
1.14k
      (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
1.14k
  assert((pbi->tile_count_minus_1 + 1) <=
389
1.14k
         (pbi->output_frame_width_in_tiles_minus_1 + 1) *
390
1.14k
             (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
1.14k
  if (aom_alloc_frame_buffer(&pbi->tile_list_outbuf, output_frame_width,
397
1.14k
                             output_frame_height, cm->seq_params->subsampling_x,
398
1.14k
                             cm->seq_params->subsampling_y,
399
1.14k
                             (cm->seq_params->use_highbitdepth &&
400
1.14k
                              (cm->seq_params->bit_depth > AOM_BITS_8)),
401
1.14k
                             0, cm->features.byte_alignment, false, 0))
402
434
    aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
403
434
                       "Failed to allocate the tile list output buffer");
404
1.14k
}
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
1.43k
                                              int *frame_decoding_finished) {
492
1.43k
  AV1_COMMON *const cm = &pbi->common;
493
1.43k
  uint32_t tile_list_payload_size = 0;
494
1.43k
  const int num_tiles = cm->tiles.cols * cm->tiles.rows;
495
1.43k
  const int start_tile = 0;
496
1.43k
  const int end_tile = num_tiles - 1;
497
1.43k
  int i = 0;
498
499
  // Process the tile list info.
500
1.43k
  pbi->output_frame_width_in_tiles_minus_1 = aom_rb_read_literal(rb, 8);
501
1.43k
  pbi->output_frame_height_in_tiles_minus_1 = aom_rb_read_literal(rb, 8);
502
1.43k
  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
1.43k
  if ((pbi->tile_count_minus_1 + 1) >
507
1.43k
      (pbi->output_frame_width_in_tiles_minus_1 + 1) *
508
1.43k
          (pbi->output_frame_height_in_tiles_minus_1 + 1)) {
509
44
    pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
510
44
    return 0;
511
44
  }
512
513
1.38k
  if (pbi->tile_count_minus_1 > MAX_TILES - 1) {
514
14
    pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
515
14
    return 0;
516
14
  }
517
518
1.37k
  int tile_width, tile_height;
519
1.37k
  if (!av1_get_uniform_tile_size(cm, &tile_width, &tile_height)) {
520
225
    pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
521
225
    return 0;
522
225
  }
523
1.14k
  const int tile_width_in_pixels = tile_width * MI_SIZE;
524
1.14k
  const int tile_height_in_pixels = tile_height * MI_SIZE;
525
526
  // Allocate output frame buffer for the tile list.
527
1.14k
  alloc_tile_list_buffer(pbi, tile_width_in_pixels, tile_height_in_pixels);
528
529
1.14k
  uint32_t tile_list_info_bytes = 4;
530
1.14k
  tile_list_payload_size += tile_list_info_bytes;
531
1.14k
  data += tile_list_info_bytes;
532
533
1.14k
  int tile_idx = 0;
534
1.14k
  for (i = 0; i <= pbi->tile_count_minus_1; i++) {
535
    // Process 1 tile.
536
    // Reset the bit reader.
537
712
    rb->bit_offset = 0;
538
712
    rb->bit_buffer = data;
539
540
    // Read out the tile info.
541
712
    uint32_t tile_info_bytes = 5;
542
    // Set reference for each tile.
543
712
    int ref_idx = aom_rb_read_literal(rb, 8);
544
712
    if (ref_idx >= MAX_EXTERNAL_REFERENCES) {
545
67
      pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
546
67
      return 0;
547
67
    }
548
645
    av1_set_reference_dec(cm, cm->remapped_ref_idx[0], 1,
549
645
                          &pbi->ext_refs.refs[ref_idx]);
550
551
645
    pbi->dec_tile_row = aom_rb_read_literal(rb, 8);
552
645
    pbi->dec_tile_col = aom_rb_read_literal(rb, 8);
553
645
    if (pbi->dec_tile_row < 0 || pbi->dec_tile_col < 0 ||
554
645
        pbi->dec_tile_row >= cm->tiles.rows ||
555
645
        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
645
    pbi->coded_tile_data_size = aom_rb_read_literal(rb, 16) + 1;
561
645
    data += tile_info_bytes;
562
645
    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
645
    av1_decode_tg_tiles_and_wrapup(pbi, data, data + pbi->coded_tile_data_size,
568
645
                                   p_data_end, start_tile, end_tile, 0);
569
645
    uint32_t tile_payload_size = (uint32_t)(*p_data_end - data);
570
571
645
    tile_list_payload_size += tile_info_bytes + tile_payload_size;
572
573
    // Update data ptr for next tile decoding.
574
645
    data = *p_data_end;
575
645
    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
435
  *frame_decoding_finished = 1;
584
435
  return tile_list_payload_size;
585
1.14k
}
586
587
// Returns the last nonzero byte index in 'data'. If there is no nonzero byte in
588
// 'data', returns -1.
589
447
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
447
  int i = (int)sz - 1;
592
1.16k
  while (i >= 0 && data[i] == 0) {
593
722
    --i;
594
722
  }
595
447
  return i;
596
447
}
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
1.04k
                                aom_metadata_insert_flags_t insert_flag) {
603
1.04k
  if (!pbi->metadata) {
604
125
    pbi->metadata = aom_img_metadata_array_alloc(0);
605
125
    if (!pbi->metadata) {
606
0
      aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
607
0
                         "Failed to allocate metadata array");
608
0
    }
609
125
  }
610
1.04k
  aom_metadata_t *metadata =
611
1.04k
      aom_img_metadata_alloc(metadata_type, data, sz, insert_flag);
612
1.04k
  if (!metadata) {
613
0
    aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
614
0
                       "Error allocating metadata");
615
0
  }
616
1.04k
  aom_metadata_t **metadata_array =
617
1.04k
      (aom_metadata_t **)realloc(pbi->metadata->metadata_array,
618
1.04k
                                 (pbi->metadata->sz + 1) * sizeof(metadata));
619
1.04k
  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
1.04k
  pbi->metadata->metadata_array = metadata_array;
625
1.04k
  pbi->metadata->metadata_array[pbi->metadata->sz] = metadata;
626
1.04k
  pbi->metadata->sz++;
627
1.04k
}
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
635
                                   size_t sz, bool has_obu_extension_header) {
632
635
  if (sz == 0) {
633
178
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
634
178
                       "itu_t_t35_country_code is missing");
635
178
  }
636
635
  int country_code_size = 1;
637
635
  if (*data == 0xFF) {
638
194
    if (sz == 1) {
639
10
      aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
640
10
                         "itu_t_t35_country_code_extension_byte is missing");
641
10
    }
642
194
    ++country_code_size;
643
194
  }
644
635
  int end_index = get_last_nonzero_byte_index(data, sz);
645
635
  if (end_index < country_code_size) {
646
29
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
647
29
                       "No trailing bits found in ITU-T T.35 metadata OBU");
648
29
  }
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
635
  if (data[end_index] != 0x80) {
654
246
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
655
246
                       "The last nonzero byte of the ITU-T T.35 metadata OBU "
656
246
                       "is 0x%02x, should be 0x80.",
657
246
                       data[end_index]);
658
246
  }
659
635
  alloc_read_metadata(pbi, OBU_METADATA_TYPE_ITUT_T35, data, end_index,
660
635
                      has_obu_extension_header
661
635
                          ? AOM_MIF_ANY_FRAME_LAYER_SPECIFIC
662
635
                          : AOM_MIF_ANY_FRAME);
663
635
}
664
665
// On success, returns the number of bytes read from 'data'. On failure, calls
666
// aom_internal_error() and does not return.
667
static size_t read_metadata_hdr_cll(AV1Decoder *const pbi, const uint8_t *data,
668
684
                                    size_t sz) {
669
684
  const size_t kHdrCllPayloadSize = 4;
670
684
  if (sz < kHdrCllPayloadSize) {
671
44
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
672
44
                       "Incorrect HDR CLL metadata payload size");
673
44
  }
674
684
  alloc_read_metadata(pbi, OBU_METADATA_TYPE_HDR_CLL, data, kHdrCllPayloadSize,
675
684
                      AOM_MIF_ANY_FRAME);
676
684
  return kHdrCllPayloadSize;
677
684
}
678
679
// On success, returns the number of bytes read from 'data'. On failure, calls
680
// aom_internal_error() and does not return.
681
static size_t read_metadata_hdr_mdcv(AV1Decoder *const pbi, const uint8_t *data,
682
584
                                     size_t sz) {
683
584
  const size_t kMdcvPayloadSize = 24;
684
584
  if (sz < kMdcvPayloadSize) {
685
356
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
686
356
                       "Incorrect HDR MDCV metadata payload size");
687
356
  }
688
584
  alloc_read_metadata(pbi, OBU_METADATA_TYPE_HDR_MDCV, data, kMdcvPayloadSize,
689
584
                      AOM_MIF_ANY_FRAME);
690
584
  return kMdcvPayloadSize;
691
584
}
692
693
3.25k
static void scalability_structure(struct aom_read_bit_buffer *rb) {
694
3.25k
  const int spatial_layers_cnt_minus_1 = aom_rb_read_literal(rb, 2);
695
3.25k
  const int spatial_layer_dimensions_present_flag = aom_rb_read_bit(rb);
696
3.25k
  const int spatial_layer_description_present_flag = aom_rb_read_bit(rb);
697
3.25k
  const int temporal_group_description_present_flag = aom_rb_read_bit(rb);
698
  // scalability_structure_reserved_3bits must be set to zero and be ignored by
699
  // decoders.
700
3.25k
  aom_rb_read_literal(rb, 3);
701
702
3.25k
  if (spatial_layer_dimensions_present_flag) {
703
4.32k
    for (int i = 0; i <= spatial_layers_cnt_minus_1; i++) {
704
2.36k
      aom_rb_read_literal(rb, 16);
705
2.36k
      aom_rb_read_literal(rb, 16);
706
2.36k
    }
707
1.96k
  }
708
3.25k
  if (spatial_layer_description_present_flag) {
709
594
    for (int i = 0; i <= spatial_layers_cnt_minus_1; i++) {
710
400
      aom_rb_read_literal(rb, 8);
711
400
    }
712
194
  }
713
3.25k
  if (temporal_group_description_present_flag) {
714
1.33k
    const int temporal_group_size = aom_rb_read_literal(rb, 8);
715
3.16k
    for (int i = 0; i < temporal_group_size; i++) {
716
1.83k
      aom_rb_read_literal(rb, 3);
717
1.83k
      aom_rb_read_bit(rb);
718
1.83k
      aom_rb_read_bit(rb);
719
1.83k
      const int temporal_group_ref_cnt = aom_rb_read_literal(rb, 3);
720
2.67k
      for (int j = 0; j < temporal_group_ref_cnt; j++) {
721
842
        aom_rb_read_literal(rb, 8);
722
842
      }
723
1.83k
    }
724
1.33k
  }
725
3.25k
}
726
727
3.29k
static void read_metadata_scalability(struct aom_read_bit_buffer *rb) {
728
3.29k
  const int scalability_mode_idc = aom_rb_read_literal(rb, 8);
729
3.29k
  if (scalability_mode_idc == SCALABILITY_SS) {
730
3.25k
    scalability_structure(rb);
731
3.25k
  }
732
3.29k
}
733
734
637
static void read_metadata_timecode(struct aom_read_bit_buffer *rb) {
735
637
  aom_rb_read_literal(rb, 5);  // counting_type f(5)
736
637
  const int full_timestamp_flag =
737
637
      aom_rb_read_bit(rb);     // full_timestamp_flag f(1)
738
637
  aom_rb_read_bit(rb);         // discontinuity_flag (f1)
739
637
  aom_rb_read_bit(rb);         // cnt_dropped_flag f(1)
740
637
  aom_rb_read_literal(rb, 9);  // n_frames f(9)
741
637
  if (full_timestamp_flag) {
742
293
    aom_rb_read_literal(rb, 6);  // seconds_value f(6)
743
293
    aom_rb_read_literal(rb, 6);  // minutes_value f(6)
744
293
    aom_rb_read_literal(rb, 5);  // hours_value f(5)
745
344
  } else {
746
344
    const int seconds_flag = aom_rb_read_bit(rb);  // seconds_flag f(1)
747
344
    if (seconds_flag) {
748
171
      aom_rb_read_literal(rb, 6);                    // seconds_value f(6)
749
171
      const int minutes_flag = aom_rb_read_bit(rb);  // minutes_flag f(1)
750
171
      if (minutes_flag) {
751
131
        aom_rb_read_literal(rb, 6);                  // minutes_value f(6)
752
131
        const int hours_flag = aom_rb_read_bit(rb);  // hours_flag f(1)
753
131
        if (hours_flag) {
754
105
          aom_rb_read_literal(rb, 5);  // hours_value f(5)
755
105
        }
756
131
      }
757
171
    }
758
344
  }
759
  // time_offset_length f(5)
760
637
  const int time_offset_length = aom_rb_read_literal(rb, 5);
761
637
  if (time_offset_length) {
762
    // time_offset_value f(time_offset_length)
763
419
    aom_rb_read_literal(rb, time_offset_length);
764
419
  }
765
637
}
766
767
// Returns the last nonzero byte in 'data'. If there is no nonzero byte in
768
// 'data', returns 0.
769
//
770
// Call this function to check the following requirement in the spec:
771
//   This implies that when any payload data is present for this OBU type, at
772
//   least one byte of the payload data (including the trailing bit) shall not
773
//   be equal to 0.
774
6.24k
static uint8_t get_last_nonzero_byte(const uint8_t *data, size_t sz) {
775
  // Scan backward and return on the first nonzero byte.
776
6.24k
  size_t i = sz;
777
24.3k
  while (i != 0) {
778
23.4k
    --i;
779
23.4k
    if (data[i] != 0) return data[i];
780
23.4k
  }
781
838
  return 0;
782
6.24k
}
783
784
// Checks the metadata for correct syntax but ignores the parsed metadata.
785
//
786
// On success, returns the number of bytes read from 'data'. On failure, sets
787
// pbi->common.error.error_code and returns 0, or calls aom_internal_error()
788
// and does not return.
789
static size_t read_metadata(AV1Decoder *pbi, const uint8_t *data, size_t sz,
790
9.95k
                            bool has_obu_extension_header) {
791
9.95k
  size_t type_length;
792
9.95k
  uint64_t type_value;
793
9.95k
  if (aom_uleb_decode(data, sz, &type_value, &type_length) < 0) {
794
169
    pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
795
169
    return 0;
796
169
  }
797
9.78k
  const OBU_METADATA_TYPE metadata_type = (OBU_METADATA_TYPE)type_value;
798
9.78k
  if (metadata_type == 0 || metadata_type >= 6) {
799
    // If metadata_type is reserved for future use or a user private value,
800
    // ignore the entire OBU and just check trailing bits.
801
3.95k
    if (get_last_nonzero_byte(data + type_length, sz - type_length) == 0) {
802
415
      pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
803
415
      return 0;
804
415
    }
805
3.53k
    return sz;
806
3.95k
  }
807
5.83k
  if (metadata_type == OBU_METADATA_TYPE_ITUT_T35) {
808
    // read_metadata_itut_t35() checks trailing bits.
809
635
    read_metadata_itut_t35(pbi, data + type_length, sz - type_length,
810
635
                           has_obu_extension_header);
811
635
    return sz;
812
5.19k
  } else if (metadata_type == OBU_METADATA_TYPE_HDR_CLL) {
813
684
    size_t bytes_read =
814
684
        type_length +
815
684
        read_metadata_hdr_cll(pbi, data + type_length, sz - type_length);
816
684
    if (get_last_nonzero_byte(data + bytes_read, sz - bytes_read) != 0x80) {
817
584
      pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
818
584
      return 0;
819
584
    }
820
100
    return sz;
821
4.51k
  } else if (metadata_type == OBU_METADATA_TYPE_HDR_MDCV) {
822
584
    size_t bytes_read =
823
584
        type_length +
824
584
        read_metadata_hdr_mdcv(pbi, data + type_length, sz - type_length);
825
584
    if (get_last_nonzero_byte(data + bytes_read, sz - bytes_read) != 0x80) {
826
210
      pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
827
210
      return 0;
828
210
    }
829
374
    return sz;
830
584
  }
831
832
3.93k
  struct aom_read_bit_buffer rb;
833
3.93k
  av1_init_read_bit_buffer(pbi, &rb, data + type_length, data + sz);
834
3.93k
  if (metadata_type == OBU_METADATA_TYPE_SCALABILITY) {
835
3.29k
    read_metadata_scalability(&rb);
836
3.29k
  } else {
837
637
    assert(metadata_type == OBU_METADATA_TYPE_TIMECODE);
838
637
    read_metadata_timecode(&rb);
839
637
  }
840
3.93k
  if (av1_check_trailing_bits(pbi, &rb) != 0) {
841
    // pbi->error.error_code is already set.
842
2.68k
    return 0;
843
2.68k
  }
844
1.24k
  assert((rb.bit_offset & 7) == 0);
845
199
  return type_length + (rb.bit_offset >> 3);
846
1.24k
}
847
848
// On success, returns 'sz'. On failure, sets pbi->common.error.error_code and
849
// returns 0.
850
static size_t read_padding(AV1_COMMON *const cm, const uint8_t *data,
851
2.79k
                           size_t sz) {
852
  // The spec allows a padding OBU to be header-only (i.e., obu_size = 0). So
853
  // check trailing bits only if sz > 0.
854
2.79k
  if (sz > 0) {
855
    // The payload of a padding OBU is byte aligned. Therefore the first
856
    // trailing byte should be 0x80. See https://crbug.com/aomedia/2393.
857
63
    const uint8_t last_nonzero_byte = get_last_nonzero_byte(data, sz);
858
63
    if (last_nonzero_byte != 0x80) {
859
34
      cm->error->error_code = AOM_CODEC_CORRUPT_FRAME;
860
34
      return 0;
861
34
    }
862
63
  }
863
2.76k
  return sz;
864
2.79k
}
865
866
// On success, returns a boolean that indicates whether the decoding of the
867
// current frame is finished. On failure, sets pbi->error.error_code and
868
// returns -1.
869
int aom_decode_frame_from_obus(struct AV1Decoder *pbi, const uint8_t *data,
870
                               const uint8_t *data_end,
871
269k
                               const uint8_t **p_data_end) {
872
269k
  AV1_COMMON *const cm = &pbi->common;
873
269k
  int frame_decoding_finished = 0;
874
269k
  int is_first_tg_obu_received = 1;
875
  // Whenever pbi->seen_frame_header is set to 1, frame_header is set to the
876
  // beginning of the frame_header_obu and frame_header_size is set to its
877
  // size. This allows us to check if a redundant frame_header_obu is a copy
878
  // of the previous frame_header_obu.
879
  //
880
  // Initialize frame_header to a dummy nonnull pointer, otherwise the Clang
881
  // Static Analyzer in clang 7.0.1 will falsely warn that a null pointer is
882
  // passed as an argument to a 'nonnull' parameter of memcmp(). The initial
883
  // value will not be used.
884
269k
  const uint8_t *frame_header = data;
885
269k
  uint32_t frame_header_size = 0;
886
269k
  ObuHeader obu_header;
887
269k
  memset(&obu_header, 0, sizeof(obu_header));
888
269k
  pbi->seen_frame_header = 0;
889
269k
  pbi->next_start_tile = 0;
890
269k
  pbi->num_tile_groups = 0;
891
892
269k
  if (data_end < data) {
893
0
    pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
894
0
    return -1;
895
0
  }
896
897
  // Reset pbi->camera_frame_header_ready to 0 if cm->tiles.large_scale = 0.
898
269k
  if (!cm->tiles.large_scale) pbi->camera_frame_header_ready = 0;
899
900
  // decode frame as a series of OBUs
901
552k
  while (!frame_decoding_finished && pbi->error.error_code == AOM_CODEC_OK) {
902
451k
    struct aom_read_bit_buffer rb;
903
451k
    size_t payload_size = 0;
904
451k
    size_t decoded_payload_size = 0;
905
451k
    size_t obu_payload_offset = 0;
906
451k
    size_t bytes_read = 0;
907
451k
    const size_t bytes_available = data_end - data;
908
909
451k
    if (bytes_available == 0 && !pbi->seen_frame_header) {
910
644
      *p_data_end = data;
911
644
      pbi->error.error_code = AOM_CODEC_OK;
912
644
      break;
913
644
    }
914
915
450k
    aom_codec_err_t status =
916
450k
        aom_read_obu_header_and_size(data, bytes_available, pbi->is_annexb,
917
450k
                                     &obu_header, &payload_size, &bytes_read);
918
919
450k
    if (status != AOM_CODEC_OK) {
920
22.2k
      pbi->error.error_code = status;
921
22.2k
      return -1;
922
22.2k
    }
923
924
    // Record obu size header information.
925
428k
    pbi->obu_size_hdr.data = data + obu_header.size;
926
428k
    pbi->obu_size_hdr.size = bytes_read - obu_header.size;
927
928
    // Note: aom_read_obu_header_and_size() takes care of checking that this
929
    // doesn't cause 'data' to advance past 'data_end'.
930
428k
    data += bytes_read;
931
932
428k
    if ((size_t)(data_end - data) < payload_size) {
933
5.30k
      pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
934
5.30k
      return -1;
935
5.30k
    }
936
937
422k
    cm->temporal_layer_id = obu_header.temporal_layer_id;
938
422k
    cm->spatial_layer_id = obu_header.spatial_layer_id;
939
940
422k
    if (obu_header.type != OBU_TEMPORAL_DELIMITER &&
941
422k
        obu_header.type != OBU_SEQUENCE_HEADER) {
942
      // don't decode obu if it's not in current operating mode
943
236k
      if (!is_obu_in_current_operating_point(pbi, &obu_header)) {
944
1.18k
        data += payload_size;
945
1.18k
        continue;
946
1.18k
      }
947
236k
    }
948
949
421k
    av1_init_read_bit_buffer(pbi, &rb, data, data + payload_size);
950
951
421k
    switch (obu_header.type) {
952
99.7k
      case OBU_TEMPORAL_DELIMITER:
953
99.7k
        decoded_payload_size = read_temporal_delimiter_obu();
954
99.7k
        if (pbi->seen_frame_header) {
955
          // A new temporal unit has started, but the frame in the previous
956
          // temporal unit is incomplete.
957
3
          pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
958
3
          return -1;
959
3
        }
960
99.6k
        break;
961
99.6k
      case OBU_SEQUENCE_HEADER:
962
86.2k
        decoded_payload_size = read_sequence_header_obu(pbi, &rb);
963
86.2k
        if (pbi->error.error_code != AOM_CODEC_OK) return -1;
964
        // The sequence header should not change in the middle of a frame.
965
77.4k
        if (pbi->sequence_header_changed && pbi->seen_frame_header) {
966
1
          pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
967
1
          return -1;
968
1
        }
969
77.4k
        break;
970
77.4k
      case OBU_FRAME_HEADER:
971
2.02k
      case OBU_REDUNDANT_FRAME_HEADER:
972
219k
      case OBU_FRAME:
973
219k
        if (obu_header.type == OBU_REDUNDANT_FRAME_HEADER) {
974
789
          if (!pbi->seen_frame_header) {
975
59
            pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
976
59
            return -1;
977
59
          }
978
218k
        } else {
979
          // OBU_FRAME_HEADER or OBU_FRAME.
980
218k
          if (pbi->seen_frame_header) {
981
26
            pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
982
26
            return -1;
983
26
          }
984
218k
        }
985
        // Only decode first frame header received
986
219k
        if (!pbi->seen_frame_header ||
987
219k
            (cm->tiles.large_scale && !pbi->camera_frame_header_ready)) {
988
218k
          frame_header_size = read_frame_header_obu(
989
218k
              pbi, &rb, data, p_data_end, obu_header.type != OBU_FRAME);
990
218k
          frame_header = data;
991
218k
          pbi->seen_frame_header = 1;
992
218k
          if (!pbi->ext_tile_debug && cm->tiles.large_scale)
993
7.96k
            pbi->camera_frame_header_ready = 1;
994
218k
        } else {
995
          // Verify that the frame_header_obu is identical to the original
996
          // frame_header_obu.
997
730
          if (frame_header_size > payload_size ||
998
730
              memcmp(data, frame_header, frame_header_size) != 0) {
999
428
            pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1000
428
            return -1;
1001
428
          }
1002
302
          assert(rb.bit_offset == 0);
1003
302
          rb.bit_offset = 8 * frame_header_size;
1004
302
        }
1005
1006
218k
        decoded_payload_size = frame_header_size;
1007
218k
        pbi->frame_header_size = frame_header_size;
1008
218k
        cm->cur_frame->temporal_id = obu_header.temporal_layer_id;
1009
218k
        cm->cur_frame->spatial_id = obu_header.spatial_layer_id;
1010
1011
218k
        if (cm->show_existing_frame) {
1012
333
          if (obu_header.type == OBU_FRAME) {
1013
197
            pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
1014
197
            return -1;
1015
197
          }
1016
136
          frame_decoding_finished = 1;
1017
136
          pbi->seen_frame_header = 0;
1018
1019
136
          if (cm->show_frame &&
1020
136
              !cm->seq_params->order_hint_info.enable_order_hint) {
1021
2
            ++cm->current_frame.frame_number;
1022
2
          }
1023
136
          break;
1024
333
        }
1025
1026
        // In large scale tile coding, decode the common camera frame header
1027
        // before any tile list OBU.
1028
218k
        if (!pbi->ext_tile_debug && pbi->camera_frame_header_ready) {
1029
7.96k
          frame_decoding_finished = 1;
1030
          // Skip the rest of the frame data.
1031
7.96k
          decoded_payload_size = payload_size;
1032
          // Update data_end.
1033
7.96k
          *p_data_end = data_end;
1034
7.96k
          break;
1035
7.96k
        }
1036
1037
210k
        if (obu_header.type != OBU_FRAME) break;
1038
209k
        obu_payload_offset = frame_header_size;
1039
        // Byte align the reader before reading the tile group.
1040
        // byte_alignment() has set pbi->error.error_code if it returns -1.
1041
209k
        if (byte_alignment(cm, &rb)) return -1;
1042
204k
        AOM_FALLTHROUGH_INTENDED;  // fall through to read tile group.
1043
204k
      case OBU_TILE_GROUP:
1044
204k
        if (!pbi->seen_frame_header) {
1045
51
          pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1046
51
          return -1;
1047
51
        }
1048
204k
        if (obu_payload_offset > payload_size) {
1049
0
          pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1050
0
          return -1;
1051
0
        }
1052
204k
        decoded_payload_size += read_one_tile_group_obu(
1053
204k
            pbi, &rb, is_first_tg_obu_received, data + obu_payload_offset,
1054
204k
            data + payload_size, p_data_end, &frame_decoding_finished,
1055
204k
            obu_header.type == OBU_FRAME);
1056
204k
        if (pbi->error.error_code != AOM_CODEC_OK) return -1;
1057
203k
        is_first_tg_obu_received = 0;
1058
203k
        if (frame_decoding_finished) {
1059
92.7k
          pbi->seen_frame_header = 0;
1060
92.7k
          pbi->next_start_tile = 0;
1061
92.7k
        }
1062
203k
        pbi->num_tile_groups++;
1063
203k
        break;
1064
9.95k
      case OBU_METADATA: {
1065
9.95k
        decoded_payload_size =
1066
9.95k
            read_metadata(pbi, data, payload_size, obu_header.has_extension);
1067
9.95k
        if (pbi->error.error_code != AOM_CODEC_OK) return -1;
1068
5.89k
        break;
1069
9.95k
      }
1070
5.89k
      case OBU_TILE_LIST:
1071
1.85k
        if (CONFIG_NORMAL_TILE_MODE) {
1072
0
          pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
1073
0
          return -1;
1074
0
        }
1075
1076
        // This OBU type is purely for the large scale tile coding mode.
1077
        // The common camera frame header has to be already decoded.
1078
1.85k
        if (!pbi->camera_frame_header_ready) {
1079
424
          pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1080
424
          return -1;
1081
424
        }
1082
1083
1.43k
        cm->tiles.large_scale = 1;
1084
1.43k
        av1_set_single_tile_decoding_mode(cm);
1085
1.43k
        decoded_payload_size =
1086
1.43k
            read_and_decode_one_tile_list(pbi, &rb, data, data + payload_size,
1087
1.43k
                                          p_data_end, &frame_decoding_finished);
1088
1.43k
        if (pbi->error.error_code != AOM_CODEC_OK) return -1;
1089
1.08k
        break;
1090
2.79k
      case OBU_PADDING:
1091
2.79k
        decoded_payload_size = read_padding(cm, data, payload_size);
1092
2.79k
        if (pbi->error.error_code != AOM_CODEC_OK) return -1;
1093
2.76k
        break;
1094
2.76k
      default:
1095
        // Skip unrecognized OBUs
1096
1.96k
        if (payload_size > 0 &&
1097
1.96k
            get_last_nonzero_byte(data, payload_size) == 0) {
1098
37
          pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1099
37
          return -1;
1100
37
        }
1101
1.93k
        decoded_payload_size = payload_size;
1102
1.93k
        break;
1103
421k
    }
1104
1105
    // Check that the signalled OBU size matches the actual amount of data read
1106
283k
    if (decoded_payload_size > payload_size) {
1107
0
      pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1108
0
      return -1;
1109
0
    }
1110
1111
    // If there are extra padding bytes, they should all be zero
1112
300k
    while (decoded_payload_size < payload_size) {
1113
19.4k
      uint8_t padding_byte = data[decoded_payload_size++];
1114
19.4k
      if (padding_byte != 0) {
1115
2.06k
        pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
1116
2.06k
        return -1;
1117
2.06k
      }
1118
19.4k
    }
1119
1120
281k
    data += payload_size;
1121
281k
  }
1122
1123
101k
  if (pbi->error.error_code != AOM_CODEC_OK) return -1;
1124
101k
  return frame_decoding_finished;
1125
101k
}