Coverage Report

Created: 2025-12-31 06:49

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