2 * Copyright (c) 2013 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
15 #include "vpx_ports/mem_ops.h"
19 static const char *IVF_SIGNATURE
= "DKIF";
21 static void fix_framerate(int *num
, int *den
) {
22 // Some versions of vpxenc used 1/(2*fps) for the timebase, so
23 // we can guess the framerate using only the timebase in this
24 // case. Other files would require reading ahead to guess the
25 // timebase, like we do for webm.
27 // Correct for the factor of 2 applied to the timebase in the encoder.
33 // Don't know FPS for sure, and don't have readahead code
34 // (yet?), so just default to 30fps.
40 int file_is_ivf(struct VpxInputContext
*input_ctx
) {
44 if (fread(raw_hdr
, 1, 32, input_ctx
->file
) == 32) {
45 if (memcmp(IVF_SIGNATURE
, raw_hdr
, 4) == 0) {
48 if (mem_get_le16(raw_hdr
+ 4) != 0) {
49 fprintf(stderr
, "Error: Unrecognized IVF version! This file may not"
53 input_ctx
->fourcc
= mem_get_le32(raw_hdr
+ 8);
54 input_ctx
->width
= mem_get_le16(raw_hdr
+ 12);
55 input_ctx
->height
= mem_get_le16(raw_hdr
+ 14);
56 input_ctx
->framerate
.numerator
= mem_get_le32(raw_hdr
+ 16);
57 input_ctx
->framerate
.denominator
= mem_get_le32(raw_hdr
+ 20);
58 fix_framerate(&input_ctx
->framerate
.numerator
,
59 &input_ctx
->framerate
.denominator
);
64 rewind(input_ctx
->file
);
65 input_ctx
->detect
.buf_read
= 0;
67 input_ctx
->detect
.position
= 4;
72 int ivf_read_frame(FILE *infile
, uint8_t **buffer
,
73 size_t *bytes_read
, size_t *buffer_size
) {
74 char raw_header
[IVF_FRAME_HDR_SZ
] = {0};
75 size_t frame_size
= 0;
77 if (fread(raw_header
, IVF_FRAME_HDR_SZ
, 1, infile
) != 1) {
79 warn("Failed to read frame size\n");
81 frame_size
= mem_get_le32(raw_header
);
83 if (frame_size
> 256 * 1024 * 1024) {
84 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size
);
88 if (frame_size
> *buffer_size
) {
89 uint8_t *new_buffer
= realloc(*buffer
, 2 * frame_size
);
93 *buffer_size
= 2 * frame_size
;
95 warn("Failed to allocate compressed data buffer\n");
102 if (fread(*buffer
, 1, frame_size
, infile
) != frame_size
) {
103 warn("Failed to read full frame\n");
107 *bytes_read
= frame_size
;