1 /*
2 * Copyright (c) 2014 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
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "./ivfdec.h"
15 #include "./video_reader.h"
16
17 #include "vpx_ports/mem_ops.h"
18
19 static const char *const kIVFSignature = "DKIF";
20
21 struct VpxVideoReaderStruct {
22 VpxVideoInfo info;
23 FILE *file;
24 uint8_t *buffer;
25 size_t buffer_size;
26 size_t frame_size;
27 };
28
vpx_video_reader_open(const char * filename)29 VpxVideoReader *vpx_video_reader_open(const char *filename) {
30 char header[32];
31 VpxVideoReader *reader = NULL;
32 FILE *const file = fopen(filename, "rb");
33 if (!file) {
34 fprintf(stderr, "%s can't be opened.\n", filename); // Can't open file
35 return NULL;
36 }
37
38 if (fread(header, 1, 32, file) != 32) {
39 fprintf(stderr, "File header on %s can't be read.\n",
40 filename); // Can't read file header
41 return NULL;
42 }
43 if (memcmp(kIVFSignature, header, 4) != 0) {
44 fprintf(stderr, "The IVF signature on %s is wrong.\n",
45 filename); // Wrong IVF signature
46
47 return NULL;
48 }
49 if (mem_get_le16(header + 4) != 0) {
50 fprintf(stderr, "%s uses the wrong IVF version.\n",
51 filename); // Wrong IVF version
52
53 return NULL;
54 }
55
56 reader = calloc(1, sizeof(*reader));
57 if (!reader) {
58 fprintf(
59 stderr,
60 "Can't allocate VpxVideoReader\n"); // Can't allocate VpxVideoReader
61
62 return NULL;
63 }
64
65 reader->file = file;
66 reader->info.codec_fourcc = mem_get_le32(header + 8);
67 reader->info.frame_width = mem_get_le16(header + 12);
68 reader->info.frame_height = mem_get_le16(header + 14);
69 reader->info.time_base.numerator = mem_get_le32(header + 16);
70 reader->info.time_base.denominator = mem_get_le32(header + 20);
71
72 return reader;
73 }
74
vpx_video_reader_close(VpxVideoReader * reader)75 void vpx_video_reader_close(VpxVideoReader *reader) {
76 if (reader) {
77 fclose(reader->file);
78 free(reader->buffer);
79 free(reader);
80 }
81 }
82
vpx_video_reader_read_frame(VpxVideoReader * reader)83 int vpx_video_reader_read_frame(VpxVideoReader *reader) {
84 return !ivf_read_frame(reader->file, &reader->buffer, &reader->frame_size,
85 &reader->buffer_size);
86 }
87
vpx_video_reader_get_frame(VpxVideoReader * reader,size_t * size)88 const uint8_t *vpx_video_reader_get_frame(VpxVideoReader *reader,
89 size_t *size) {
90 if (size) *size = reader->frame_size;
91
92 return reader->buffer;
93 }
94
vpx_video_reader_get_info(VpxVideoReader * reader)95 const VpxVideoInfo *vpx_video_reader_get_info(VpxVideoReader *reader) {
96 return &reader->info;
97 }
98