xref: /aosp_15_r20/external/libaom/av1/common/obu_util.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2018, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
12*77c1e3ccSAndroid Build Coastguard Worker 
13*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/obu_util.h"
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/bitreader_buffer.h"
16*77c1e3ccSAndroid Build Coastguard Worker 
read_obu_size(const uint8_t * data,size_t bytes_available,size_t * const obu_size,size_t * const length_field_size)17*77c1e3ccSAndroid Build Coastguard Worker static aom_codec_err_t read_obu_size(const uint8_t *data,
18*77c1e3ccSAndroid Build Coastguard Worker                                      size_t bytes_available,
19*77c1e3ccSAndroid Build Coastguard Worker                                      size_t *const obu_size,
20*77c1e3ccSAndroid Build Coastguard Worker                                      size_t *const length_field_size) {
21*77c1e3ccSAndroid Build Coastguard Worker   uint64_t u_obu_size = 0;
22*77c1e3ccSAndroid Build Coastguard Worker   if (aom_uleb_decode(data, bytes_available, &u_obu_size, length_field_size) !=
23*77c1e3ccSAndroid Build Coastguard Worker       0) {
24*77c1e3ccSAndroid Build Coastguard Worker     return AOM_CODEC_CORRUPT_FRAME;
25*77c1e3ccSAndroid Build Coastguard Worker   }
26*77c1e3ccSAndroid Build Coastguard Worker 
27*77c1e3ccSAndroid Build Coastguard Worker   if (u_obu_size > UINT32_MAX) return AOM_CODEC_CORRUPT_FRAME;
28*77c1e3ccSAndroid Build Coastguard Worker   *obu_size = (size_t)u_obu_size;
29*77c1e3ccSAndroid Build Coastguard Worker   return AOM_CODEC_OK;
30*77c1e3ccSAndroid Build Coastguard Worker }
31*77c1e3ccSAndroid Build Coastguard Worker 
32*77c1e3ccSAndroid Build Coastguard Worker // Parses OBU header and stores values in 'header'.
read_obu_header(struct aom_read_bit_buffer * rb,int is_annexb,ObuHeader * header)33*77c1e3ccSAndroid Build Coastguard Worker static aom_codec_err_t read_obu_header(struct aom_read_bit_buffer *rb,
34*77c1e3ccSAndroid Build Coastguard Worker                                        int is_annexb, ObuHeader *header) {
35*77c1e3ccSAndroid Build Coastguard Worker   if (!rb || !header) return AOM_CODEC_INVALID_PARAM;
36*77c1e3ccSAndroid Build Coastguard Worker 
37*77c1e3ccSAndroid Build Coastguard Worker   const ptrdiff_t bit_buffer_byte_length = rb->bit_buffer_end - rb->bit_buffer;
38*77c1e3ccSAndroid Build Coastguard Worker   if (bit_buffer_byte_length < 1) return AOM_CODEC_CORRUPT_FRAME;
39*77c1e3ccSAndroid Build Coastguard Worker 
40*77c1e3ccSAndroid Build Coastguard Worker   header->size = 1;
41*77c1e3ccSAndroid Build Coastguard Worker 
42*77c1e3ccSAndroid Build Coastguard Worker   if (aom_rb_read_bit(rb) != 0) {
43*77c1e3ccSAndroid Build Coastguard Worker     // Forbidden bit. Must not be set.
44*77c1e3ccSAndroid Build Coastguard Worker     return AOM_CODEC_CORRUPT_FRAME;
45*77c1e3ccSAndroid Build Coastguard Worker   }
46*77c1e3ccSAndroid Build Coastguard Worker 
47*77c1e3ccSAndroid Build Coastguard Worker   header->type = (OBU_TYPE)aom_rb_read_literal(rb, 4);
48*77c1e3ccSAndroid Build Coastguard Worker   header->has_extension = aom_rb_read_bit(rb);
49*77c1e3ccSAndroid Build Coastguard Worker   header->has_size_field = aom_rb_read_bit(rb);
50*77c1e3ccSAndroid Build Coastguard Worker 
51*77c1e3ccSAndroid Build Coastguard Worker   if (!header->has_size_field && !is_annexb) {
52*77c1e3ccSAndroid Build Coastguard Worker     // section 5 obu streams must have obu_size field set.
53*77c1e3ccSAndroid Build Coastguard Worker     return AOM_CODEC_UNSUP_BITSTREAM;
54*77c1e3ccSAndroid Build Coastguard Worker   }
55*77c1e3ccSAndroid Build Coastguard Worker 
56*77c1e3ccSAndroid Build Coastguard Worker   // obu_reserved_1bit must be set to 0. The value is ignored by a decoder.
57*77c1e3ccSAndroid Build Coastguard Worker   aom_rb_read_bit(rb);
58*77c1e3ccSAndroid Build Coastguard Worker 
59*77c1e3ccSAndroid Build Coastguard Worker   if (header->has_extension) {
60*77c1e3ccSAndroid Build Coastguard Worker     if (bit_buffer_byte_length == 1) return AOM_CODEC_CORRUPT_FRAME;
61*77c1e3ccSAndroid Build Coastguard Worker 
62*77c1e3ccSAndroid Build Coastguard Worker     header->size += 1;
63*77c1e3ccSAndroid Build Coastguard Worker     header->temporal_layer_id = aom_rb_read_literal(rb, 3);
64*77c1e3ccSAndroid Build Coastguard Worker     header->spatial_layer_id = aom_rb_read_literal(rb, 2);
65*77c1e3ccSAndroid Build Coastguard Worker     // extension_header_reserved_3bits must be set to 0. The value is ignored by
66*77c1e3ccSAndroid Build Coastguard Worker     // a decoder.
67*77c1e3ccSAndroid Build Coastguard Worker     aom_rb_read_literal(rb, 3);
68*77c1e3ccSAndroid Build Coastguard Worker   } else {
69*77c1e3ccSAndroid Build Coastguard Worker     header->temporal_layer_id = 0;
70*77c1e3ccSAndroid Build Coastguard Worker     header->spatial_layer_id = 0;
71*77c1e3ccSAndroid Build Coastguard Worker   }
72*77c1e3ccSAndroid Build Coastguard Worker 
73*77c1e3ccSAndroid Build Coastguard Worker   return AOM_CODEC_OK;
74*77c1e3ccSAndroid Build Coastguard Worker }
75*77c1e3ccSAndroid Build Coastguard Worker 
aom_read_obu_header(uint8_t * buffer,size_t buffer_length,size_t * consumed,ObuHeader * header,int is_annexb)76*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t aom_read_obu_header(uint8_t *buffer, size_t buffer_length,
77*77c1e3ccSAndroid Build Coastguard Worker                                     size_t *consumed, ObuHeader *header,
78*77c1e3ccSAndroid Build Coastguard Worker                                     int is_annexb) {
79*77c1e3ccSAndroid Build Coastguard Worker   if (buffer_length < 1 || !consumed || !header) return AOM_CODEC_INVALID_PARAM;
80*77c1e3ccSAndroid Build Coastguard Worker 
81*77c1e3ccSAndroid Build Coastguard Worker   // TODO(tomfinegan): Set the error handler here and throughout this file, and
82*77c1e3ccSAndroid Build Coastguard Worker   // confirm parsing work done via aom_read_bit_buffer is successful.
83*77c1e3ccSAndroid Build Coastguard Worker   struct aom_read_bit_buffer rb = { buffer, buffer + buffer_length, 0, NULL,
84*77c1e3ccSAndroid Build Coastguard Worker                                     NULL };
85*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_err_t parse_result = read_obu_header(&rb, is_annexb, header);
86*77c1e3ccSAndroid Build Coastguard Worker   if (parse_result == AOM_CODEC_OK) *consumed = header->size;
87*77c1e3ccSAndroid Build Coastguard Worker   return parse_result;
88*77c1e3ccSAndroid Build Coastguard Worker }
89*77c1e3ccSAndroid Build Coastguard Worker 
aom_read_obu_header_and_size(const uint8_t * data,size_t bytes_available,int is_annexb,ObuHeader * obu_header,size_t * const payload_size,size_t * const bytes_read)90*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t aom_read_obu_header_and_size(const uint8_t *data,
91*77c1e3ccSAndroid Build Coastguard Worker                                              size_t bytes_available,
92*77c1e3ccSAndroid Build Coastguard Worker                                              int is_annexb,
93*77c1e3ccSAndroid Build Coastguard Worker                                              ObuHeader *obu_header,
94*77c1e3ccSAndroid Build Coastguard Worker                                              size_t *const payload_size,
95*77c1e3ccSAndroid Build Coastguard Worker                                              size_t *const bytes_read) {
96*77c1e3ccSAndroid Build Coastguard Worker   size_t length_field_size_obu = 0;
97*77c1e3ccSAndroid Build Coastguard Worker   size_t length_field_size_payload = 0;
98*77c1e3ccSAndroid Build Coastguard Worker   size_t obu_size = 0;
99*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_err_t status;
100*77c1e3ccSAndroid Build Coastguard Worker 
101*77c1e3ccSAndroid Build Coastguard Worker   if (is_annexb) {
102*77c1e3ccSAndroid Build Coastguard Worker     // Size field comes before the OBU header, and includes the OBU header
103*77c1e3ccSAndroid Build Coastguard Worker     status =
104*77c1e3ccSAndroid Build Coastguard Worker         read_obu_size(data, bytes_available, &obu_size, &length_field_size_obu);
105*77c1e3ccSAndroid Build Coastguard Worker 
106*77c1e3ccSAndroid Build Coastguard Worker     if (status != AOM_CODEC_OK) return status;
107*77c1e3ccSAndroid Build Coastguard Worker   }
108*77c1e3ccSAndroid Build Coastguard Worker 
109*77c1e3ccSAndroid Build Coastguard Worker   struct aom_read_bit_buffer rb = { data + length_field_size_obu,
110*77c1e3ccSAndroid Build Coastguard Worker                                     data + bytes_available, 0, NULL, NULL };
111*77c1e3ccSAndroid Build Coastguard Worker 
112*77c1e3ccSAndroid Build Coastguard Worker   status = read_obu_header(&rb, is_annexb, obu_header);
113*77c1e3ccSAndroid Build Coastguard Worker   if (status != AOM_CODEC_OK) return status;
114*77c1e3ccSAndroid Build Coastguard Worker 
115*77c1e3ccSAndroid Build Coastguard Worker   if (!obu_header->has_size_field) {
116*77c1e3ccSAndroid Build Coastguard Worker     assert(is_annexb);
117*77c1e3ccSAndroid Build Coastguard Worker     // Derive the payload size from the data we've already read
118*77c1e3ccSAndroid Build Coastguard Worker     if (obu_size < obu_header->size) return AOM_CODEC_CORRUPT_FRAME;
119*77c1e3ccSAndroid Build Coastguard Worker 
120*77c1e3ccSAndroid Build Coastguard Worker     *payload_size = obu_size - obu_header->size;
121*77c1e3ccSAndroid Build Coastguard Worker   } else {
122*77c1e3ccSAndroid Build Coastguard Worker     // Size field comes after the OBU header, and is just the payload size
123*77c1e3ccSAndroid Build Coastguard Worker     status = read_obu_size(
124*77c1e3ccSAndroid Build Coastguard Worker         data + length_field_size_obu + obu_header->size,
125*77c1e3ccSAndroid Build Coastguard Worker         bytes_available - length_field_size_obu - obu_header->size,
126*77c1e3ccSAndroid Build Coastguard Worker         payload_size, &length_field_size_payload);
127*77c1e3ccSAndroid Build Coastguard Worker     if (status != AOM_CODEC_OK) return status;
128*77c1e3ccSAndroid Build Coastguard Worker   }
129*77c1e3ccSAndroid Build Coastguard Worker 
130*77c1e3ccSAndroid Build Coastguard Worker   *bytes_read =
131*77c1e3ccSAndroid Build Coastguard Worker       length_field_size_obu + obu_header->size + length_field_size_payload;
132*77c1e3ccSAndroid Build Coastguard Worker   return AOM_CODEC_OK;
133*77c1e3ccSAndroid Build Coastguard Worker }
134