blob: 93b1f27e7a53aafedab90a02b9609a414502e5cc [file] [log] [blame]
Tom Finegan00a35aa2013-11-14 12:37:42 -08001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Tom Finegan00a35aa2013-11-14 12:37:42 -08003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * 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.
Tom Finegan00a35aa2013-11-14 12:37:42 -080010 */
11
Tom Finegan00a35aa2013-11-14 12:37:42 -080012#include <stdio.h>
13#include <stdlib.h>
Dmitry Kovalevc2b33682014-01-24 11:20:09 -080014#include <string.h>
15
Yaowu Xuc27fc142016-08-22 16:08:15 -070016#include "aom_ports/mem_ops.h"
Dmitry Kovalev4334c072014-02-26 16:32:09 -080017
Dmitry Kovalevc2b33682014-01-24 11:20:09 -080018#include "./ivfdec.h"
19
20static const char *IVF_SIGNATURE = "DKIF";
Tom Finegan00a35aa2013-11-14 12:37:42 -080021
Dmitry Kovalevdf463722014-01-15 15:10:12 -080022static void fix_framerate(int *num, int *den) {
Sebastien Alaiwandeaf05e2016-11-25 10:39:39 +010023 if (*den <= 0 || *den >= 1000000000 || *num <= 0 || *num >= 1000) {
24 // framerate seems to be invalid, just default to 30fps.
Dmitry Kovalevdf463722014-01-15 15:10:12 -080025 *num = 30;
26 *den = 1;
27 }
28}
29
Yaowu Xuf883b422016-08-30 14:01:10 -070030int file_is_ivf(struct AvxInputContext *input_ctx) {
Tom Finegan00a35aa2013-11-14 12:37:42 -080031 char raw_hdr[32];
32 int is_ivf = 0;
33
Tom Finegan00a35aa2013-11-14 12:37:42 -080034 if (fread(raw_hdr, 1, 32, input_ctx->file) == 32) {
Dmitry Kovalevc2b33682014-01-24 11:20:09 -080035 if (memcmp(IVF_SIGNATURE, raw_hdr, 4) == 0) {
Tom Finegan00a35aa2013-11-14 12:37:42 -080036 is_ivf = 1;
37
38 if (mem_get_le16(raw_hdr + 4) != 0) {
clang-format6c4d83e2016-08-08 19:03:30 -070039 fprintf(stderr,
40 "Error: Unrecognized IVF version! This file may not"
Tom Finegan00a35aa2013-11-14 12:37:42 -080041 " decode properly.");
42 }
43
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);
Dmitry Kovalevdf463722014-01-15 15:10:12 -080049 fix_framerate(&input_ctx->framerate.numerator,
50 &input_ctx->framerate.denominator);
Tom Finegan00a35aa2013-11-14 12:37:42 -080051 }
52 }
53
Yaowu Xua7c7e782013-11-19 10:17:04 -080054 if (!is_ivf) {
Tom Finegan00a35aa2013-11-14 12:37:42 -080055 rewind(input_ctx->file);
Yaowu Xua7c7e782013-11-19 10:17:04 -080056 input_ctx->detect.buf_read = 0;
57 } else {
Tom Finegan00a35aa2013-11-14 12:37:42 -080058 input_ctx->detect.position = 4;
Yaowu Xua7c7e782013-11-19 10:17:04 -080059 }
Tom Finegan00a35aa2013-11-14 12:37:42 -080060 return is_ivf;
61}
62
clang-format6c4d83e2016-08-08 19:03:30 -070063int 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 };
Tom Finegan00a35aa2013-11-14 12:37:42 -080066 size_t frame_size = 0;
Tom Finegan00a35aa2013-11-14 12:37:42 -080067
68 if (fread(raw_header, IVF_FRAME_HDR_SZ, 1, infile) != 1) {
clang-format6c4d83e2016-08-08 19:03:30 -070069 if (!feof(infile)) warn("Failed to read frame size\n");
Tom Finegan00a35aa2013-11-14 12:37:42 -080070 } else {
71 frame_size = mem_get_le32(raw_header);
72
73 if (frame_size > 256 * 1024 * 1024) {
74 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
75 frame_size = 0;
76 }
77
78 if (frame_size > *buffer_size) {
79 uint8_t *new_buffer = realloc(*buffer, 2 * frame_size);
80
81 if (new_buffer) {
82 *buffer = new_buffer;
83 *buffer_size = 2 * frame_size;
84 } else {
85 warn("Failed to allocate compressed data buffer\n");
86 frame_size = 0;
87 }
88 }
89 }
90
91 if (!feof(infile)) {
92 if (fread(*buffer, 1, frame_size, infile) != frame_size) {
93 warn("Failed to read full frame\n");
94 return 1;
95 }
96
97 *bytes_read = frame_size;
98 return 0;
99 }
100
101 return 1;
102}