Coverage Report

Created: 2025-06-13 07:07

/src/aom/av1/common/obu_util.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2018, 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
#include <assert.h>
12
13
#include "av1/common/obu_util.h"
14
15
#include "aom_dsp/bitreader_buffer.h"
16
17
static aom_codec_err_t read_obu_size(const uint8_t *data,
18
                                     size_t bytes_available,
19
                                     size_t *const obu_size,
20
1.29M
                                     size_t *const length_field_size) {
21
1.29M
  uint64_t u_obu_size = 0;
22
1.29M
  if (aom_uleb_decode(data, bytes_available, &u_obu_size, length_field_size) !=
23
1.29M
      0) {
24
3.65k
    return AOM_CODEC_CORRUPT_FRAME;
25
3.65k
  }
26
27
1.29M
  if (u_obu_size > UINT32_MAX) return AOM_CODEC_CORRUPT_FRAME;
28
1.29M
  *obu_size = (size_t)u_obu_size;
29
1.29M
  return AOM_CODEC_OK;
30
1.29M
}
31
32
// Parses OBU header and stores values in 'header'.
33
static aom_codec_err_t read_obu_header(struct aom_read_bit_buffer *rb,
34
1.49M
                                       int is_annexb, ObuHeader *header) {
35
1.49M
  if (!rb || !header) return AOM_CODEC_INVALID_PARAM;
36
37
1.49M
  const ptrdiff_t bit_buffer_byte_length = rb->bit_buffer_end - rb->bit_buffer;
38
1.49M
  if (bit_buffer_byte_length < 1) return AOM_CODEC_CORRUPT_FRAME;
39
40
1.49M
  header->size = 1;
41
42
1.49M
  if (aom_rb_read_bit(rb) != 0) {
43
    // Forbidden bit. Must not be set.
44
16.2k
    return AOM_CODEC_CORRUPT_FRAME;
45
16.2k
  }
46
47
1.47M
  header->type = (OBU_TYPE)aom_rb_read_literal(rb, 4);
48
1.47M
  header->has_extension = aom_rb_read_bit(rb);
49
1.47M
  header->has_size_field = aom_rb_read_bit(rb);
50
51
1.47M
  if (!header->has_size_field && !is_annexb) {
52
    // section 5 obu streams must have obu_size field set.
53
209k
    return AOM_CODEC_UNSUP_BITSTREAM;
54
209k
  }
55
56
  // obu_reserved_1bit must be set to 0. The value is ignored by a decoder.
57
1.26M
  aom_rb_read_bit(rb);
58
59
1.26M
  if (header->has_extension) {
60
185k
    if (bit_buffer_byte_length == 1) return AOM_CODEC_CORRUPT_FRAME;
61
62
185k
    header->size += 1;
63
185k
    header->temporal_layer_id = aom_rb_read_literal(rb, 3);
64
185k
    header->spatial_layer_id = aom_rb_read_literal(rb, 2);
65
    // extension_header_reserved_3bits must be set to 0. The value is ignored by
66
    // a decoder.
67
185k
    aom_rb_read_literal(rb, 3);
68
1.08M
  } else {
69
1.08M
    header->temporal_layer_id = 0;
70
1.08M
    header->spatial_layer_id = 0;
71
1.08M
  }
72
73
1.26M
  return AOM_CODEC_OK;
74
1.26M
}
75
76
aom_codec_err_t aom_read_obu_header(uint8_t *buffer, size_t buffer_length,
77
                                    size_t *consumed, ObuHeader *header,
78
0
                                    int is_annexb) {
79
0
  if (buffer_length < 1 || !consumed || !header) return AOM_CODEC_INVALID_PARAM;
80
81
  // TODO(tomfinegan): Set the error handler here and throughout this file, and
82
  // confirm parsing work done via aom_read_bit_buffer is successful.
83
0
  struct aom_read_bit_buffer rb = { buffer, buffer + buffer_length, 0, NULL,
84
0
                                    NULL };
85
0
  aom_codec_err_t parse_result = read_obu_header(&rb, is_annexb, header);
86
0
  if (parse_result == AOM_CODEC_OK) *consumed = header->size;
87
0
  return parse_result;
88
0
}
89
90
aom_codec_err_t aom_read_obu_header_and_size(const uint8_t *data,
91
                                             size_t bytes_available,
92
                                             int is_annexb,
93
                                             ObuHeader *obu_header,
94
                                             size_t *const payload_size,
95
1.49M
                                             size_t *const bytes_read) {
96
1.49M
  size_t length_field_size_obu = 0;
97
1.49M
  size_t length_field_size_payload = 0;
98
1.49M
  size_t obu_size = 0;
99
1.49M
  aom_codec_err_t status;
100
101
1.49M
  if (is_annexb) {
102
    // Size field comes before the OBU header, and includes the OBU header
103
98.0k
    status =
104
98.0k
        read_obu_size(data, bytes_available, &obu_size, &length_field_size_obu);
105
106
98.0k
    if (status != AOM_CODEC_OK) return status;
107
98.0k
  }
108
109
1.49M
  struct aom_read_bit_buffer rb = { data + length_field_size_obu,
110
1.49M
                                    data + bytes_available, 0, NULL, NULL };
111
112
1.49M
  status = read_obu_header(&rb, is_annexb, obu_header);
113
1.49M
  if (status != AOM_CODEC_OK) return status;
114
115
1.26M
  if (!obu_header->has_size_field) {
116
65.9k
    assert(is_annexb);
117
    // Derive the payload size from the data we've already read
118
65.9k
    if (obu_size < obu_header->size) return AOM_CODEC_CORRUPT_FRAME;
119
120
64.3k
    *payload_size = obu_size - obu_header->size;
121
1.20M
  } else {
122
    // Size field comes after the OBU header, and is just the payload size
123
1.20M
    status = read_obu_size(
124
1.20M
        data + length_field_size_obu + obu_header->size,
125
1.20M
        bytes_available - length_field_size_obu - obu_header->size,
126
1.20M
        payload_size, &length_field_size_payload);
127
1.20M
    if (status != AOM_CODEC_OK) return status;
128
1.20M
  }
129
130
1.26M
  *bytes_read =
131
1.26M
      length_field_size_obu + obu_header->size + length_field_size_payload;
132
1.26M
  return AOM_CODEC_OK;
133
1.26M
}