Coverage Report

Created: 2026-03-08 06:51

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