• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Returns 1 when OBU type is valid, and 0 otherwise.
valid_obu_type(int obu_type)18 static int valid_obu_type(int obu_type) {
19   int valid_type = 0;
20   switch (obu_type) {
21     case OBU_SEQUENCE_HEADER:
22     case OBU_TEMPORAL_DELIMITER:
23     case OBU_FRAME_HEADER:
24     case OBU_TILE_GROUP:
25     case OBU_METADATA:
26     case OBU_FRAME:
27     case OBU_REDUNDANT_FRAME_HEADER:
28     case OBU_TILE_LIST:
29     case OBU_PADDING: valid_type = 1; break;
30     default: break;
31   }
32   return valid_type;
33 }
34 
read_obu_size(const uint8_t * data,size_t bytes_available,size_t * const obu_size,size_t * const length_field_size)35 static aom_codec_err_t read_obu_size(const uint8_t *data,
36                                      size_t bytes_available,
37                                      size_t *const obu_size,
38                                      size_t *const length_field_size) {
39   uint64_t u_obu_size = 0;
40   if (aom_uleb_decode(data, bytes_available, &u_obu_size, length_field_size) !=
41       0) {
42     return AOM_CODEC_CORRUPT_FRAME;
43   }
44 
45   if (u_obu_size > UINT32_MAX) return AOM_CODEC_CORRUPT_FRAME;
46   *obu_size = (size_t)u_obu_size;
47   return AOM_CODEC_OK;
48 }
49 
50 // Parses OBU header and stores values in 'header'.
read_obu_header(struct aom_read_bit_buffer * rb,int is_annexb,ObuHeader * header)51 static aom_codec_err_t read_obu_header(struct aom_read_bit_buffer *rb,
52                                        int is_annexb, ObuHeader *header) {
53   if (!rb || !header) return AOM_CODEC_INVALID_PARAM;
54 
55   const ptrdiff_t bit_buffer_byte_length = rb->bit_buffer_end - rb->bit_buffer;
56   if (bit_buffer_byte_length < 1) return AOM_CODEC_CORRUPT_FRAME;
57 
58   header->size = 1;
59 
60   if (aom_rb_read_bit(rb) != 0) {
61     // Forbidden bit. Must not be set.
62     return AOM_CODEC_CORRUPT_FRAME;
63   }
64 
65   header->type = (OBU_TYPE)aom_rb_read_literal(rb, 4);
66 
67   if (!valid_obu_type(header->type)) return AOM_CODEC_CORRUPT_FRAME;
68 
69   header->has_extension = aom_rb_read_bit(rb);
70   header->has_size_field = aom_rb_read_bit(rb);
71 
72   if (!header->has_size_field && !is_annexb) {
73     // section 5 obu streams must have obu_size field set.
74     return AOM_CODEC_UNSUP_BITSTREAM;
75   }
76 
77   if (aom_rb_read_bit(rb) != 0) {
78     // obu_reserved_1bit must be set to 0.
79     return AOM_CODEC_CORRUPT_FRAME;
80   }
81 
82   if (header->has_extension) {
83     if (bit_buffer_byte_length == 1) return AOM_CODEC_CORRUPT_FRAME;
84 
85     header->size += 1;
86     header->temporal_layer_id = aom_rb_read_literal(rb, 3);
87     header->spatial_layer_id = aom_rb_read_literal(rb, 2);
88     if (aom_rb_read_literal(rb, 3) != 0) {
89       // extension_header_reserved_3bits must be set to 0.
90       return AOM_CODEC_CORRUPT_FRAME;
91     }
92   }
93 
94   return AOM_CODEC_OK;
95 }
96 
aom_read_obu_header(uint8_t * buffer,size_t buffer_length,size_t * consumed,ObuHeader * header,int is_annexb)97 aom_codec_err_t aom_read_obu_header(uint8_t *buffer, size_t buffer_length,
98                                     size_t *consumed, ObuHeader *header,
99                                     int is_annexb) {
100   if (buffer_length < 1 || !consumed || !header) return AOM_CODEC_INVALID_PARAM;
101 
102   // TODO(tomfinegan): Set the error handler here and throughout this file, and
103   // confirm parsing work done via aom_read_bit_buffer is successful.
104   struct aom_read_bit_buffer rb = { buffer, buffer + buffer_length, 0, NULL,
105                                     NULL };
106   aom_codec_err_t parse_result = read_obu_header(&rb, is_annexb, header);
107   if (parse_result == AOM_CODEC_OK) *consumed = header->size;
108   return parse_result;
109 }
110 
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)111 aom_codec_err_t aom_read_obu_header_and_size(const uint8_t *data,
112                                              size_t bytes_available,
113                                              int is_annexb,
114                                              ObuHeader *obu_header,
115                                              size_t *const payload_size,
116                                              size_t *const bytes_read) {
117   size_t length_field_size_obu = 0;
118   size_t length_field_size_payload = 0;
119   size_t obu_size = 0;
120   aom_codec_err_t status;
121 
122   if (is_annexb) {
123     // Size field comes before the OBU header, and includes the OBU header
124     status =
125         read_obu_size(data, bytes_available, &obu_size, &length_field_size_obu);
126 
127     if (status != AOM_CODEC_OK) return status;
128   }
129 
130   struct aom_read_bit_buffer rb = { data + length_field_size_obu,
131                                     data + bytes_available, 0, NULL, NULL };
132 
133   status = read_obu_header(&rb, is_annexb, obu_header);
134   if (status != AOM_CODEC_OK) return status;
135 
136   if (!obu_header->has_size_field) {
137     assert(is_annexb);
138     // Derive the payload size from the data we've already read
139     if (obu_size < obu_header->size) return AOM_CODEC_CORRUPT_FRAME;
140 
141     *payload_size = obu_size - obu_header->size;
142   } else {
143     // Size field comes after the OBU header, and is just the payload size
144     status = read_obu_size(
145         data + length_field_size_obu + obu_header->size,
146         bytes_available - length_field_size_obu - obu_header->size,
147         payload_size, &length_field_size_payload);
148     if (status != AOM_CODEC_OK) return status;
149   }
150 
151   *bytes_read =
152       length_field_size_obu + obu_header->size + length_field_size_payload;
153   return AOM_CODEC_OK;
154 }
155