2013-11-15 00:37:42 +04:00
|
|
|
/*
|
2016-09-02 00:32:49 +03:00
|
|
|
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
|
2013-11-15 00:37:42 +04:00
|
|
|
*
|
2016-09-02 00:32:49 +03:00
|
|
|
* This source code is subject to the terms of the BSD 2 Clause License and
|
|
|
|
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
|
|
|
|
* was not distributed with this source code in the LICENSE file, you can
|
|
|
|
* obtain it at www.aomedia.org/license/software. If the Alliance for Open
|
|
|
|
* Media Patent License 1.0 was not distributed with this source code in the
|
|
|
|
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
|
2013-11-15 00:37:42 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-01-24 23:20:09 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-08-23 02:08:15 +03:00
|
|
|
#include "aom_ports/mem_ops.h"
|
2014-02-27 04:32:09 +04:00
|
|
|
|
2014-01-24 23:20:09 +04:00
|
|
|
#include "./ivfdec.h"
|
|
|
|
|
|
|
|
static const char *IVF_SIGNATURE = "DKIF";
|
2013-11-15 00:37:42 +04:00
|
|
|
|
2014-01-16 03:10:12 +04:00
|
|
|
static void fix_framerate(int *num, int *den) {
|
2016-08-31 00:01:10 +03:00
|
|
|
// Some versions of aomenc used 1/(2*fps) for the timebase, so
|
2014-01-16 03:10:12 +04:00
|
|
|
// we can guess the framerate using only the timebase in this
|
|
|
|
// case. Other files would require reading ahead to guess the
|
|
|
|
// timebase, like we do for webm.
|
2016-06-04 04:04:54 +03:00
|
|
|
if (*den > 0 && *den < 1000000000 && *num > 0 && *num < 1000) {
|
2014-01-16 03:10:12 +04:00
|
|
|
// Correct for the factor of 2 applied to the timebase in the encoder.
|
|
|
|
if (*num & 1)
|
|
|
|
*den *= 2;
|
|
|
|
else
|
|
|
|
*num /= 2;
|
|
|
|
} else {
|
|
|
|
// Don't know FPS for sure, and don't have readahead code
|
|
|
|
// (yet?), so just default to 30fps.
|
|
|
|
*num = 30;
|
|
|
|
*den = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
int file_is_ivf(struct AvxInputContext *input_ctx) {
|
2013-11-15 00:37:42 +04:00
|
|
|
char raw_hdr[32];
|
|
|
|
int is_ivf = 0;
|
|
|
|
|
|
|
|
if (fread(raw_hdr, 1, 32, input_ctx->file) == 32) {
|
2014-01-24 23:20:09 +04:00
|
|
|
if (memcmp(IVF_SIGNATURE, raw_hdr, 4) == 0) {
|
2013-11-15 00:37:42 +04:00
|
|
|
is_ivf = 1;
|
|
|
|
|
|
|
|
if (mem_get_le16(raw_hdr + 4) != 0) {
|
2016-08-09 05:03:30 +03:00
|
|
|
fprintf(stderr,
|
|
|
|
"Error: Unrecognized IVF version! This file may not"
|
2013-11-15 00:37:42 +04:00
|
|
|
" decode properly.");
|
|
|
|
}
|
|
|
|
|
|
|
|
input_ctx->fourcc = mem_get_le32(raw_hdr + 8);
|
|
|
|
input_ctx->width = mem_get_le16(raw_hdr + 12);
|
|
|
|
input_ctx->height = mem_get_le16(raw_hdr + 14);
|
|
|
|
input_ctx->framerate.numerator = mem_get_le32(raw_hdr + 16);
|
|
|
|
input_ctx->framerate.denominator = mem_get_le32(raw_hdr + 20);
|
2014-01-16 03:10:12 +04:00
|
|
|
fix_framerate(&input_ctx->framerate.numerator,
|
|
|
|
&input_ctx->framerate.denominator);
|
2013-11-15 00:37:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-19 22:17:04 +04:00
|
|
|
if (!is_ivf) {
|
2013-11-15 00:37:42 +04:00
|
|
|
rewind(input_ctx->file);
|
2013-11-19 22:17:04 +04:00
|
|
|
input_ctx->detect.buf_read = 0;
|
|
|
|
} else {
|
2013-11-15 00:37:42 +04:00
|
|
|
input_ctx->detect.position = 4;
|
2013-11-19 22:17:04 +04:00
|
|
|
}
|
2013-11-15 00:37:42 +04:00
|
|
|
return is_ivf;
|
|
|
|
}
|
|
|
|
|
2016-08-09 05:03:30 +03:00
|
|
|
int ivf_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
|
|
|
|
size_t *buffer_size) {
|
|
|
|
char raw_header[IVF_FRAME_HDR_SZ] = { 0 };
|
2013-11-15 00:37:42 +04:00
|
|
|
size_t frame_size = 0;
|
|
|
|
|
|
|
|
if (fread(raw_header, IVF_FRAME_HDR_SZ, 1, infile) != 1) {
|
2016-08-09 05:03:30 +03:00
|
|
|
if (!feof(infile)) warn("Failed to read frame size\n");
|
2013-11-15 00:37:42 +04:00
|
|
|
} else {
|
|
|
|
frame_size = mem_get_le32(raw_header);
|
|
|
|
|
|
|
|
if (frame_size > 256 * 1024 * 1024) {
|
|
|
|
warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
|
|
|
|
frame_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame_size > *buffer_size) {
|
|
|
|
uint8_t *new_buffer = realloc(*buffer, 2 * frame_size);
|
|
|
|
|
|
|
|
if (new_buffer) {
|
|
|
|
*buffer = new_buffer;
|
|
|
|
*buffer_size = 2 * frame_size;
|
|
|
|
} else {
|
|
|
|
warn("Failed to allocate compressed data buffer\n");
|
|
|
|
frame_size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!feof(infile)) {
|
|
|
|
if (fread(*buffer, 1, frame_size, infile) != frame_size) {
|
|
|
|
warn("Failed to read full frame\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*bytes_read = frame_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|