2 * Copyright (c) 2016, 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.
16 #include "aom_ports/mem_ops.h"
20 static const char *IVF_SIGNATURE
= "DKIF";
22 static void fix_framerate(int *num
, int *den
) {
23 if (*den
<= 0 || *den
>= 1000000000 || *num
<= 0 || *num
>= 1000) {
24 // framerate seems to be invalid, just default to 30fps.
30 int file_is_ivf(struct AvxInputContext
*input_ctx
) {
34 if (fread(raw_hdr
, 1, 32, input_ctx
->file
) == 32) {
35 if (memcmp(IVF_SIGNATURE
, raw_hdr
, 4) == 0) {
38 if (mem_get_le16(raw_hdr
+ 4) != 0) {
40 "Error: Unrecognized IVF version! This file may not"
44 input_ctx
->fourcc
= mem_get_le32(raw_hdr
+ 8);
45 input_ctx
->width
= mem_get_le16(raw_hdr
+ 12);
46 input_ctx
->height
= mem_get_le16(raw_hdr
+ 14);
47 input_ctx
->framerate
.numerator
= mem_get_le32(raw_hdr
+ 16);
48 input_ctx
->framerate
.denominator
= mem_get_le32(raw_hdr
+ 20);
49 fix_framerate(&input_ctx
->framerate
.numerator
,
50 &input_ctx
->framerate
.denominator
);
55 rewind(input_ctx
->file
);
56 input_ctx
->detect
.buf_read
= 0;
58 input_ctx
->detect
.position
= 4;
63 int ivf_read_frame(FILE *infile
, uint8_t **buffer
, size_t *bytes_read
,
64 size_t *buffer_size
) {
65 char raw_header
[IVF_FRAME_HDR_SZ
] = { 0 };
66 size_t frame_size
= 0;
68 if (fread(raw_header
, IVF_FRAME_HDR_SZ
, 1, infile
) != 1) {
69 if (!feof(infile
)) warn("Failed to read frame size\n");
71 frame_size
= mem_get_le32(raw_header
);
73 if (frame_size
> 256 * 1024 * 1024) {
74 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size
);
78 if (frame_size
> *buffer_size
) {
79 uint8_t *new_buffer
= (uint8_t *)realloc(*buffer
, 2 * frame_size
);
83 *buffer_size
= 2 * frame_size
;
85 warn("Failed to allocate compressed data buffer\n");
92 if (fread(*buffer
, 1, frame_size
, infile
) != frame_size
) {
93 warn("Failed to read full frame\n");
97 *bytes_read
= frame_size
;