2 * Copyright (c) 2017, Alliance for Open Media. All rights reserved
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.
17 #include "common/obudec.h"
19 #include "aom_dsp/aom_dsp_common.h"
20 #include "aom_ports/mem_ops.h"
21 #include "av1/common/common.h"
22 #include "av1/common/obu_util.h"
24 #define OBU_BUFFER_SIZE (500 * 1024)
26 #define OBU_HEADER_SIZE 1
27 #define OBU_EXTENSION_SIZE 1
28 #define OBU_MAX_LENGTH_FIELD_SIZE 8
30 #define OBU_MAX_HEADER_SIZE \
31 (OBU_HEADER_SIZE + OBU_EXTENSION_SIZE + 2 * OBU_MAX_LENGTH_FIELD_SIZE)
33 #define OBU_DETECTION_SIZE \
34 (OBU_HEADER_SIZE + OBU_EXTENSION_SIZE + 4 * OBU_MAX_LENGTH_FIELD_SIZE)
36 // Reads unsigned LEB128 integer and returns 0 upon successful read and decode.
37 // Stores raw bytes in 'value_buffer', length of the number in 'value_length',
38 // and decoded value in 'value'.
39 static int obudec_read_leb128(FILE *f
, uint8_t *value_buffer
,
40 size_t *value_length
, uint64_t *value
) {
41 if (!f
|| !value_buffer
|| !value_length
|| !value
) return -1;
43 for (len
= 0; len
< OBU_MAX_LENGTH_FIELD_SIZE
; ++len
) {
44 const size_t num_read
= fread(&value_buffer
[len
], 1, 1, f
);
46 if (len
== 0 && feof(f
)) {
50 // Ran out of data before completing read of value.
53 if ((value_buffer
[len
] >> 7) == 0) {
60 return aom_uleb_decode(value_buffer
, len
, value
, NULL
);
63 // Reads OBU header from 'f'. The 'buffer_capacity' passed in must be large
64 // enough to store an OBU header with extension (2 bytes). Raw OBU data is
65 // written to 'obu_data', parsed OBU header values are written to 'obu_header',
66 // and total bytes read from file are written to 'bytes_read'. Returns 0 for
67 // success, and non-zero on failure. When end of file is reached, the return
68 // value is 0 and the 'bytes_read' value is set to 0.
69 static int obudec_read_obu_header(FILE *f
, size_t buffer_capacity
,
70 int is_annexb
, uint8_t *obu_data
,
71 ObuHeader
*obu_header
, size_t *bytes_read
) {
72 if (!f
|| buffer_capacity
< (OBU_HEADER_SIZE
+ OBU_EXTENSION_SIZE
) ||
73 !obu_data
|| !obu_header
|| !bytes_read
) {
76 *bytes_read
= fread(obu_data
, 1, 1, f
);
78 if (feof(f
) && *bytes_read
== 0) {
80 } else if (*bytes_read
!= 1) {
81 fprintf(stderr
, "obudec: Failure reading OBU header.\n");
85 const int has_extension
= (obu_data
[0] >> 2) & 0x1;
87 if (fread(&obu_data
[1], 1, 1, f
) != 1) {
88 fprintf(stderr
, "obudec: Failure reading OBU extension.");
94 size_t obu_bytes_parsed
= 0;
95 const aom_codec_err_t parse_result
= aom_read_obu_header(
96 obu_data
, *bytes_read
, &obu_bytes_parsed
, obu_header
, is_annexb
);
97 if (parse_result
!= AOM_CODEC_OK
|| *bytes_read
!= obu_bytes_parsed
) {
98 fprintf(stderr
, "obudec: Error parsing OBU header.\n");
105 // Reads OBU payload from 'f' and returns 0 for success when all payload bytes
106 // are read from the file. Payload data is written to 'obu_data', and actual
107 // bytes read added to 'bytes_read'.
108 static int obudec_read_obu_payload(FILE *f
, size_t payload_length
,
109 uint8_t *obu_data
, size_t *bytes_read
) {
110 if (!f
|| payload_length
== 0 || !obu_data
|| !bytes_read
) return -1;
112 if (fread(obu_data
, 1, payload_length
, f
) != payload_length
) {
113 fprintf(stderr
, "obudec: Failure reading OBU payload.\n");
117 *bytes_read
+= payload_length
;
121 static int obudec_read_obu_header_and_size(FILE *f
, size_t buffer_capacity
,
122 int is_annexb
, uint8_t *buffer
,
124 size_t *payload_length
,
125 ObuHeader
*obu_header
) {
126 const size_t kMinimumBufferSize
= OBU_MAX_HEADER_SIZE
;
127 if (!f
|| !buffer
|| !bytes_read
|| !payload_length
|| !obu_header
||
128 buffer_capacity
< kMinimumBufferSize
) {
132 size_t leb128_length_obu
= 0;
133 size_t leb128_length_payload
= 0;
134 uint64_t obu_size
= 0;
136 if (obudec_read_leb128(f
, &buffer
[0], &leb128_length_obu
, &obu_size
) != 0) {
137 fprintf(stderr
, "obudec: Failure reading OBU size length.\n");
139 } else if (leb128_length_obu
== 0) {
143 if (obu_size
> UINT32_MAX
) {
144 fprintf(stderr
, "obudec: OBU payload length too large.\n");
149 size_t header_size
= 0;
150 if (obudec_read_obu_header(f
, buffer_capacity
- leb128_length_obu
, is_annexb
,
151 buffer
+ leb128_length_obu
, obu_header
,
152 &header_size
) != 0) {
154 } else if (header_size
== 0) {
159 if (!obu_header
->has_size_field
) {
161 if (obu_size
< header_size
) {
162 fprintf(stderr
, "obudec: OBU size is too small.\n");
165 *payload_length
= (size_t)obu_size
- header_size
;
167 uint64_t u64_payload_length
= 0;
168 if (obudec_read_leb128(f
, &buffer
[leb128_length_obu
+ header_size
],
169 &leb128_length_payload
, &u64_payload_length
) != 0) {
170 fprintf(stderr
, "obudec: Failure reading OBU payload length.\n");
173 if (u64_payload_length
> UINT32_MAX
) {
174 fprintf(stderr
, "obudec: OBU payload length too large.\n");
178 *payload_length
= (size_t)u64_payload_length
;
181 *bytes_read
= leb128_length_obu
+ header_size
+ leb128_length_payload
;
185 static int obudec_grow_buffer(size_t growth_amount
, uint8_t **obu_buffer
,
186 size_t *obu_buffer_capacity
) {
187 if (!*obu_buffer
|| !obu_buffer_capacity
|| growth_amount
== 0) {
191 const size_t capacity
= *obu_buffer_capacity
;
192 if (SIZE_MAX
- growth_amount
< capacity
) {
193 fprintf(stderr
, "obudec: cannot grow buffer, capacity will roll over.\n");
197 const size_t new_capacity
= capacity
+ growth_amount
;
199 #if defined AOM_MAX_ALLOCABLE_MEMORY
200 if (new_capacity
> AOM_MAX_ALLOCABLE_MEMORY
) {
201 fprintf(stderr
, "obudec: OBU size exceeds max alloc size.\n");
206 uint8_t *new_buffer
= (uint8_t *)realloc(*obu_buffer
, new_capacity
);
208 fprintf(stderr
, "obudec: Failed to allocate compressed data buffer.\n");
212 *obu_buffer
= new_buffer
;
213 *obu_buffer_capacity
= new_capacity
;
217 static int obudec_read_one_obu(FILE *f
, uint8_t **obu_buffer
,
218 size_t obu_bytes_buffered
,
219 size_t *obu_buffer_capacity
, size_t *obu_length
,
220 ObuHeader
*obu_header
, int is_annexb
) {
221 if (!f
|| !(*obu_buffer
) || !obu_buffer_capacity
|| !obu_length
||
226 size_t bytes_read
= 0;
227 size_t obu_payload_length
= 0;
228 size_t available_buffer_capacity
= *obu_buffer_capacity
- obu_bytes_buffered
;
230 if (available_buffer_capacity
< OBU_MAX_HEADER_SIZE
) {
231 if (obudec_grow_buffer(AOMMAX(*obu_buffer_capacity
, OBU_MAX_HEADER_SIZE
),
232 obu_buffer
, obu_buffer_capacity
) != 0) {
233 *obu_length
= bytes_read
;
236 available_buffer_capacity
+=
237 AOMMAX(*obu_buffer_capacity
, OBU_MAX_HEADER_SIZE
);
240 const int status
= obudec_read_obu_header_and_size(
241 f
, available_buffer_capacity
, is_annexb
, *obu_buffer
+ obu_bytes_buffered
,
242 &bytes_read
, &obu_payload_length
, obu_header
);
243 if (status
< 0) return status
;
245 if (obu_payload_length
> SIZE_MAX
- bytes_read
) return -1;
247 if (obu_payload_length
> 256 * 1024 * 1024) {
248 fprintf(stderr
, "obudec: Read invalid OBU size (%u)\n",
249 (unsigned int)obu_payload_length
);
250 *obu_length
= bytes_read
+ obu_payload_length
;
254 if (bytes_read
+ obu_payload_length
> available_buffer_capacity
&&
255 obudec_grow_buffer(AOMMAX(*obu_buffer_capacity
, obu_payload_length
),
256 obu_buffer
, obu_buffer_capacity
) != 0) {
257 *obu_length
= bytes_read
+ obu_payload_length
;
261 if (obu_payload_length
> 0 &&
262 obudec_read_obu_payload(f
, obu_payload_length
,
263 *obu_buffer
+ obu_bytes_buffered
+ bytes_read
,
268 *obu_length
= bytes_read
;
272 int file_is_obu(struct ObuDecInputContext
*obu_ctx
) {
273 if (!obu_ctx
|| !obu_ctx
->avx_ctx
) return 0;
275 struct AvxInputContext
*avx_ctx
= obu_ctx
->avx_ctx
;
276 uint8_t detect_buf
[OBU_DETECTION_SIZE
] = { 0 };
277 const int is_annexb
= obu_ctx
->is_annexb
;
278 FILE *f
= avx_ctx
->file
;
279 size_t payload_length
= 0;
280 ObuHeader obu_header
;
281 memset(&obu_header
, 0, sizeof(obu_header
));
282 size_t length_of_unit_size
= 0;
283 size_t annexb_header_length
= 0;
284 uint64_t unit_size
= 0;
287 // read the size of first temporal unit
288 if (obudec_read_leb128(f
, &detect_buf
[0], &length_of_unit_size
,
290 fprintf(stderr
, "obudec: Failure reading temporal unit header\n");
294 // read the size of first frame unit
295 if (obudec_read_leb128(f
, &detect_buf
[length_of_unit_size
],
296 &annexb_header_length
, &unit_size
) != 0) {
297 fprintf(stderr
, "obudec: Failure reading frame unit header\n");
300 annexb_header_length
+= length_of_unit_size
;
303 size_t bytes_read
= 0;
304 if (obudec_read_obu_header_and_size(
305 f
, OBU_DETECTION_SIZE
- annexb_header_length
, is_annexb
,
306 &detect_buf
[annexb_header_length
], &bytes_read
, &payload_length
,
308 fprintf(stderr
, "obudec: Failure reading first OBU.\n");
314 bytes_read
+= annexb_header_length
;
317 if (obu_header
.type
!= OBU_TEMPORAL_DELIMITER
&&
318 obu_header
.type
!= OBU_SEQUENCE_HEADER
) {
322 if (obu_header
.has_size_field
) {
323 if (obu_header
.type
== OBU_TEMPORAL_DELIMITER
&& payload_length
!= 0) {
326 "obudec: Invalid OBU_TEMPORAL_DELIMITER payload length (non-zero).");
330 } else if (!is_annexb
) {
331 fprintf(stderr
, "obudec: OBU size fields required, cannot decode input.\n");
336 // Appears that input is valid Section 5 AV1 stream.
337 obu_ctx
->buffer
= (uint8_t *)malloc(OBU_BUFFER_SIZE
);
338 if (!obu_ctx
->buffer
) {
339 fprintf(stderr
, "Out of memory.\n");
343 obu_ctx
->buffer_capacity
= OBU_BUFFER_SIZE
;
345 memcpy(obu_ctx
->buffer
, &detect_buf
[0], bytes_read
);
346 obu_ctx
->bytes_buffered
= bytes_read
;
347 // If the first OBU is a SEQUENCE_HEADER, then it will have a payload.
348 // We need to read this in so that our buffer only contains complete OBUs.
349 if (payload_length
> 0) {
350 if (payload_length
> (obu_ctx
->buffer_capacity
- bytes_read
)) {
351 fprintf(stderr
, "obudec: First OBU's payload is too large\n");
356 size_t payload_bytes
= 0;
357 const int status
= obudec_read_obu_payload(
358 f
, payload_length
, &obu_ctx
->buffer
[bytes_read
], &payload_bytes
);
363 obu_ctx
->bytes_buffered
+= payload_bytes
;
368 int obudec_read_temporal_unit(struct ObuDecInputContext
*obu_ctx
,
369 uint8_t **buffer
, size_t *bytes_read
,
370 size_t *buffer_size
) {
371 FILE *f
= obu_ctx
->avx_ctx
->file
;
383 size_t length_of_temporal_unit_size
= 0;
384 uint8_t tuheader
[OBU_MAX_LENGTH_FIELD_SIZE
] = { 0 };
386 if (obu_ctx
->is_annexb
) {
389 if (obu_ctx
->bytes_buffered
== 0) {
390 if (obudec_read_leb128(f
, &tuheader
[0], &length_of_temporal_unit_size
,
392 fprintf(stderr
, "obudec: Failure reading temporal unit header\n");
395 if (size
== 0 && feof(f
)) {
399 // temporal unit size was already stored in buffer
400 if (aom_uleb_decode(obu_ctx
->buffer
, obu_ctx
->bytes_buffered
, &size
,
401 &length_of_temporal_unit_size
) != 0) {
402 fprintf(stderr
, "obudec: Failure reading temporal unit header\n");
407 if (size
> UINT32_MAX
|| size
+ length_of_temporal_unit_size
> UINT32_MAX
) {
408 fprintf(stderr
, "obudec: TU too large.\n");
412 size
+= length_of_temporal_unit_size
;
413 tu_size
= (size_t)size
;
416 ObuHeader obu_header
;
417 memset(&obu_header
, 0, sizeof(obu_header
));
419 if (obudec_read_one_obu(f
, &obu_ctx
->buffer
, obu_ctx
->bytes_buffered
,
420 &obu_ctx
->buffer_capacity
, &obu_size
, &obu_header
,
422 fprintf(stderr
, "obudec: read_one_obu failed in TU loop\n");
426 if (obu_header
.type
== OBU_TEMPORAL_DELIMITER
|| obu_size
== 0) {
427 tu_size
= obu_ctx
->bytes_buffered
;
430 obu_ctx
->bytes_buffered
+= obu_size
;
435 #if defined AOM_MAX_ALLOCABLE_MEMORY
436 if (tu_size
> AOM_MAX_ALLOCABLE_MEMORY
) {
437 fprintf(stderr
, "obudec: Temporal Unit size exceeds max alloc size.\n");
442 uint8_t *new_buffer
= (uint8_t *)realloc(*buffer
, tu_size
);
445 fprintf(stderr
, "obudec: Out of memory.\n");
448 *buffer
= new_buffer
;
450 *bytes_read
= tu_size
;
451 *buffer_size
= tu_size
;
453 if (!obu_ctx
->is_annexb
) {
454 memcpy(*buffer
, obu_ctx
->buffer
, tu_size
);
456 // At this point, (obu_ctx->buffer + obu_ctx->bytes_buffered + obu_size)
457 // points to the end of the buffer.
458 memmove(obu_ctx
->buffer
, obu_ctx
->buffer
+ obu_ctx
->bytes_buffered
,
460 obu_ctx
->bytes_buffered
= obu_size
;
465 if (!obu_ctx
->bytes_buffered
) {
466 data_size
= tu_size
- length_of_temporal_unit_size
;
467 memcpy(*buffer
, &tuheader
[0], length_of_temporal_unit_size
);
468 offset
= length_of_temporal_unit_size
;
470 const size_t copy_size
= AOMMIN(obu_ctx
->bytes_buffered
, tu_size
);
471 memcpy(*buffer
, obu_ctx
->buffer
, copy_size
);
473 data_size
= tu_size
- copy_size
;
474 obu_ctx
->bytes_buffered
-= copy_size
;
477 if (fread(*buffer
+ offset
, 1, data_size
, f
) != data_size
) {
478 fprintf(stderr
, "obudec: Failed to read full temporal unit\n");
486 void obudec_free(struct ObuDecInputContext
*obu_ctx
) { free(obu_ctx
->buffer
); }