blob: d11dfff56556bff643a51f8d2d397d4efbffb675 [file] [log] [blame]
Tom Finegan00a35aa2013-11-14 12:37:42 -08001/*
2 * Copyright (c) 2013 The WebM project authors. All Rights Reserved.
3 *
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.
9 */
10
Tom Finegan00a35aa2013-11-14 12:37:42 -080011#include <stdio.h>
12#include <stdlib.h>
Dmitry Kovalevc2b33682014-01-24 11:20:09 -080013#include <string.h>
14
Yaowu Xuc27fc142016-08-22 16:08:15 -070015#include "aom_ports/mem_ops.h"
Dmitry Kovalev4334c072014-02-26 16:32:09 -080016
Dmitry Kovalevc2b33682014-01-24 11:20:09 -080017#include "./ivfdec.h"
18
19static const char *IVF_SIGNATURE = "DKIF";
Tom Finegan00a35aa2013-11-14 12:37:42 -080020
Dmitry Kovalevdf463722014-01-15 15:10:12 -080021static void fix_framerate(int *num, int *den) {
Yaowu Xuf883b422016-08-30 14:01:10 -070022 // Some versions of aomenc used 1/(2*fps) for the timebase, so
Dmitry Kovalevdf463722014-01-15 15:10:12 -080023 // 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.
James Zern79829142016-06-03 18:04:54 -070026 if (*den > 0 && *den < 1000000000 && *num > 0 && *num < 1000) {
Dmitry Kovalevdf463722014-01-15 15:10:12 -080027 // Correct for the factor of 2 applied to the timebase in the encoder.
28 if (*num & 1)
29 *den *= 2;
30 else
31 *num /= 2;
32 } else {
33 // Don't know FPS for sure, and don't have readahead code
34 // (yet?), so just default to 30fps.
35 *num = 30;
36 *den = 1;
37 }
38}
39
Yaowu Xuf883b422016-08-30 14:01:10 -070040int file_is_ivf(struct AvxInputContext *input_ctx) {
Tom Finegan00a35aa2013-11-14 12:37:42 -080041 char raw_hdr[32];
42 int is_ivf = 0;
43
Tom Finegan00a35aa2013-11-14 12:37:42 -080044 if (fread(raw_hdr, 1, 32, input_ctx->file) == 32) {
Dmitry Kovalevc2b33682014-01-24 11:20:09 -080045 if (memcmp(IVF_SIGNATURE, raw_hdr, 4) == 0) {
Tom Finegan00a35aa2013-11-14 12:37:42 -080046 is_ivf = 1;
47
48 if (mem_get_le16(raw_hdr + 4) != 0) {
clang-format6c4d83e2016-08-08 19:03:30 -070049 fprintf(stderr,
50 "Error: Unrecognized IVF version! This file may not"
Tom Finegan00a35aa2013-11-14 12:37:42 -080051 " decode properly.");
52 }
53
54 input_ctx->fourcc = mem_get_le32(raw_hdr + 8);
55 input_ctx->width = mem_get_le16(raw_hdr + 12);
56 input_ctx->height = mem_get_le16(raw_hdr + 14);
57 input_ctx->framerate.numerator = mem_get_le32(raw_hdr + 16);
58 input_ctx->framerate.denominator = mem_get_le32(raw_hdr + 20);
Dmitry Kovalevdf463722014-01-15 15:10:12 -080059 fix_framerate(&input_ctx->framerate.numerator,
60 &input_ctx->framerate.denominator);
Tom Finegan00a35aa2013-11-14 12:37:42 -080061 }
62 }
63
Yaowu Xua7c7e782013-11-19 10:17:04 -080064 if (!is_ivf) {
Tom Finegan00a35aa2013-11-14 12:37:42 -080065 rewind(input_ctx->file);
Yaowu Xua7c7e782013-11-19 10:17:04 -080066 input_ctx->detect.buf_read = 0;
67 } else {
Tom Finegan00a35aa2013-11-14 12:37:42 -080068 input_ctx->detect.position = 4;
Yaowu Xua7c7e782013-11-19 10:17:04 -080069 }
Tom Finegan00a35aa2013-11-14 12:37:42 -080070 return is_ivf;
71}
72
clang-format6c4d83e2016-08-08 19:03:30 -070073int ivf_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
74 size_t *buffer_size) {
75 char raw_header[IVF_FRAME_HDR_SZ] = { 0 };
Tom Finegan00a35aa2013-11-14 12:37:42 -080076 size_t frame_size = 0;
Tom Finegan00a35aa2013-11-14 12:37:42 -080077
78 if (fread(raw_header, IVF_FRAME_HDR_SZ, 1, infile) != 1) {
clang-format6c4d83e2016-08-08 19:03:30 -070079 if (!feof(infile)) warn("Failed to read frame size\n");
Tom Finegan00a35aa2013-11-14 12:37:42 -080080 } else {
81 frame_size = mem_get_le32(raw_header);
82
83 if (frame_size > 256 * 1024 * 1024) {
84 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
85 frame_size = 0;
86 }
87
88 if (frame_size > *buffer_size) {
89 uint8_t *new_buffer = realloc(*buffer, 2 * frame_size);
90
91 if (new_buffer) {
92 *buffer = new_buffer;
93 *buffer_size = 2 * frame_size;
94 } else {
95 warn("Failed to allocate compressed data buffer\n");
96 frame_size = 0;
97 }
98 }
99 }
100
101 if (!feof(infile)) {
102 if (fread(*buffer, 1, frame_size, infile) != frame_size) {
103 warn("Failed to read full frame\n");
104 return 1;
105 }
106
107 *bytes_read = frame_size;
108 return 0;
109 }
110
111 return 1;
112}