Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1 | /* |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 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. |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
Dmitry Kovalev | c2b3368 | 2014-01-24 11:20:09 -0800 | [diff] [blame] | 14 | #include <string.h> |
| 15 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 16 | #include "aom_ports/mem_ops.h" |
Dmitry Kovalev | 4334c07 | 2014-02-26 16:32:09 -0800 | [diff] [blame] | 17 | |
Dmitry Kovalev | c2b3368 | 2014-01-24 11:20:09 -0800 | [diff] [blame] | 18 | #include "./ivfdec.h" |
| 19 | |
| 20 | static const char *IVF_SIGNATURE = "DKIF"; |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 21 | |
Dmitry Kovalev | df46372 | 2014-01-15 15:10:12 -0800 | [diff] [blame] | 22 | static void fix_framerate(int *num, int *den) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 23 | // Some versions of aomenc used 1/(2*fps) for the timebase, so |
Dmitry Kovalev | df46372 | 2014-01-15 15:10:12 -0800 | [diff] [blame] | 24 | // we can guess the framerate using only the timebase in this |
| 25 | // case. Other files would require reading ahead to guess the |
| 26 | // timebase, like we do for webm. |
James Zern | 7982914 | 2016-06-03 18:04:54 -0700 | [diff] [blame] | 27 | if (*den > 0 && *den < 1000000000 && *num > 0 && *num < 1000) { |
Dmitry Kovalev | df46372 | 2014-01-15 15:10:12 -0800 | [diff] [blame] | 28 | // Correct for the factor of 2 applied to the timebase in the encoder. |
| 29 | if (*num & 1) |
| 30 | *den *= 2; |
| 31 | else |
| 32 | *num /= 2; |
| 33 | } else { |
| 34 | // Don't know FPS for sure, and don't have readahead code |
| 35 | // (yet?), so just default to 30fps. |
| 36 | *num = 30; |
| 37 | *den = 1; |
| 38 | } |
| 39 | } |
| 40 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 41 | int file_is_ivf(struct AvxInputContext *input_ctx) { |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 42 | char raw_hdr[32]; |
| 43 | int is_ivf = 0; |
| 44 | |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 45 | if (fread(raw_hdr, 1, 32, input_ctx->file) == 32) { |
Dmitry Kovalev | c2b3368 | 2014-01-24 11:20:09 -0800 | [diff] [blame] | 46 | if (memcmp(IVF_SIGNATURE, raw_hdr, 4) == 0) { |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 47 | is_ivf = 1; |
| 48 | |
| 49 | if (mem_get_le16(raw_hdr + 4) != 0) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 50 | fprintf(stderr, |
| 51 | "Error: Unrecognized IVF version! This file may not" |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 52 | " decode properly."); |
| 53 | } |
| 54 | |
| 55 | input_ctx->fourcc = mem_get_le32(raw_hdr + 8); |
| 56 | input_ctx->width = mem_get_le16(raw_hdr + 12); |
| 57 | input_ctx->height = mem_get_le16(raw_hdr + 14); |
| 58 | input_ctx->framerate.numerator = mem_get_le32(raw_hdr + 16); |
| 59 | input_ctx->framerate.denominator = mem_get_le32(raw_hdr + 20); |
Dmitry Kovalev | df46372 | 2014-01-15 15:10:12 -0800 | [diff] [blame] | 60 | fix_framerate(&input_ctx->framerate.numerator, |
| 61 | &input_ctx->framerate.denominator); |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Yaowu Xu | a7c7e78 | 2013-11-19 10:17:04 -0800 | [diff] [blame] | 65 | if (!is_ivf) { |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 66 | rewind(input_ctx->file); |
Yaowu Xu | a7c7e78 | 2013-11-19 10:17:04 -0800 | [diff] [blame] | 67 | input_ctx->detect.buf_read = 0; |
| 68 | } else { |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 69 | input_ctx->detect.position = 4; |
Yaowu Xu | a7c7e78 | 2013-11-19 10:17:04 -0800 | [diff] [blame] | 70 | } |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 71 | return is_ivf; |
| 72 | } |
| 73 | |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 74 | int ivf_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read, |
| 75 | size_t *buffer_size) { |
| 76 | char raw_header[IVF_FRAME_HDR_SZ] = { 0 }; |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 77 | size_t frame_size = 0; |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 78 | |
| 79 | if (fread(raw_header, IVF_FRAME_HDR_SZ, 1, infile) != 1) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 80 | if (!feof(infile)) warn("Failed to read frame size\n"); |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 81 | } else { |
| 82 | frame_size = mem_get_le32(raw_header); |
| 83 | |
| 84 | if (frame_size > 256 * 1024 * 1024) { |
| 85 | warn("Read invalid frame size (%u)\n", (unsigned int)frame_size); |
| 86 | frame_size = 0; |
| 87 | } |
| 88 | |
| 89 | if (frame_size > *buffer_size) { |
| 90 | uint8_t *new_buffer = realloc(*buffer, 2 * frame_size); |
| 91 | |
| 92 | if (new_buffer) { |
| 93 | *buffer = new_buffer; |
| 94 | *buffer_size = 2 * frame_size; |
| 95 | } else { |
| 96 | warn("Failed to allocate compressed data buffer\n"); |
| 97 | frame_size = 0; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (!feof(infile)) { |
| 103 | if (fread(*buffer, 1, frame_size, infile) != frame_size) { |
| 104 | warn("Failed to read full frame\n"); |
| 105 | return 1; |
| 106 | } |
| 107 | |
| 108 | *bytes_read = frame_size; |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | return 1; |
| 113 | } |