2014-02-06 06:34:46 +04:00
|
|
|
/*
|
2016-03-24 21:38:32 +03:00
|
|
|
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
|
2014-02-06 06:34:46 +04:00
|
|
|
*
|
2016-03-24 21:38:32 +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.
|
2014-02-06 06:34:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "./ivfdec.h"
|
|
|
|
#include "./video_reader.h"
|
|
|
|
|
2016-03-22 01:15:19 +03:00
|
|
|
#include "aom_ports/mem_ops.h"
|
2014-02-27 04:32:09 +04:00
|
|
|
|
2014-02-06 06:34:46 +04:00
|
|
|
static const char *const kIVFSignature = "DKIF";
|
|
|
|
|
|
|
|
struct VpxVideoReaderStruct {
|
|
|
|
VpxVideoInfo info;
|
|
|
|
FILE *file;
|
|
|
|
uint8_t *buffer;
|
|
|
|
size_t buffer_size;
|
|
|
|
size_t frame_size;
|
|
|
|
};
|
|
|
|
|
2016-03-25 22:11:05 +03:00
|
|
|
VpxVideoReader *aom_video_reader_open(const char *filename) {
|
2014-02-06 06:34:46 +04:00
|
|
|
char header[32];
|
|
|
|
VpxVideoReader *reader = NULL;
|
|
|
|
FILE *const file = fopen(filename, "rb");
|
2016-01-27 23:42:45 +03:00
|
|
|
if (!file) return NULL; // Can't open file
|
2014-02-06 06:34:46 +04:00
|
|
|
|
2016-01-27 23:42:45 +03:00
|
|
|
if (fread(header, 1, 32, file) != 32) return NULL; // Can't read file header
|
2014-02-06 06:34:46 +04:00
|
|
|
|
|
|
|
if (memcmp(kIVFSignature, header, 4) != 0)
|
|
|
|
return NULL; // Wrong IVF signature
|
|
|
|
|
2016-01-27 23:42:45 +03:00
|
|
|
if (mem_get_le16(header + 4) != 0) return NULL; // Wrong IVF version
|
2014-02-06 06:34:46 +04:00
|
|
|
|
|
|
|
reader = calloc(1, sizeof(*reader));
|
2016-01-27 23:42:45 +03:00
|
|
|
if (!reader) return NULL; // Can't allocate VpxVideoReader
|
2014-02-06 06:34:46 +04:00
|
|
|
|
|
|
|
reader->file = file;
|
|
|
|
reader->info.codec_fourcc = mem_get_le32(header + 8);
|
|
|
|
reader->info.frame_width = mem_get_le16(header + 12);
|
|
|
|
reader->info.frame_height = mem_get_le16(header + 14);
|
|
|
|
reader->info.time_base.numerator = mem_get_le32(header + 16);
|
|
|
|
reader->info.time_base.denominator = mem_get_le32(header + 20);
|
|
|
|
|
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
2016-03-25 22:11:05 +03:00
|
|
|
void aom_video_reader_close(VpxVideoReader *reader) {
|
2014-02-06 06:34:46 +04:00
|
|
|
if (reader) {
|
|
|
|
fclose(reader->file);
|
|
|
|
free(reader->buffer);
|
|
|
|
free(reader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 22:11:05 +03:00
|
|
|
int aom_video_reader_read_frame(VpxVideoReader *reader) {
|
2014-02-06 06:34:46 +04:00
|
|
|
return !ivf_read_frame(reader->file, &reader->buffer, &reader->frame_size,
|
|
|
|
&reader->buffer_size);
|
|
|
|
}
|
|
|
|
|
2016-03-25 22:11:05 +03:00
|
|
|
const uint8_t *aom_video_reader_get_frame(VpxVideoReader *reader,
|
2014-02-06 06:34:46 +04:00
|
|
|
size_t *size) {
|
2016-01-27 23:42:45 +03:00
|
|
|
if (size) *size = reader->frame_size;
|
2014-02-06 06:34:46 +04:00
|
|
|
|
|
|
|
return reader->buffer;
|
|
|
|
}
|
|
|
|
|
2016-03-25 22:11:05 +03:00
|
|
|
const VpxVideoInfo *aom_video_reader_get_info(VpxVideoReader *reader) {
|
2014-02-06 06:34:46 +04:00
|
|
|
return &reader->info;
|
|
|
|
}
|