1 /*
2 * Copyright © 2018, VideoLAN and dav1d authors
3 * Copyright © 2018, Two Orioles, LLC
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "config.h"
29
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "input/demuxer.h"
37
38 typedef struct DemuxerPriv {
39 FILE *f;
40 } IvfInputContext;
41
42 static const uint8_t probe_data[] = {
43 'D', 'K', 'I', 'F',
44 0, 0, 0x20, 0,
45 'A', 'V', '0', '1',
46 };
47
ivf_probe(const uint8_t * const data)48 static int ivf_probe(const uint8_t *const data) {
49 return !memcmp(data, probe_data, sizeof(probe_data));
50 }
51
rl32(const uint8_t * const p)52 static unsigned rl32(const uint8_t *const p) {
53 return ((uint32_t)p[3] << 24U) | (p[2] << 16U) | (p[1] << 8U) | p[0];
54 }
55
rl64(const uint8_t * const p)56 static int64_t rl64(const uint8_t *const p) {
57 return (((uint64_t) rl32(&p[4])) << 32) | rl32(p);
58 }
59
ivf_open(IvfInputContext * const c,const char * const file,unsigned fps[2],unsigned * const num_frames,unsigned timebase[2])60 static int ivf_open(IvfInputContext *const c, const char *const file,
61 unsigned fps[2], unsigned *const num_frames, unsigned timebase[2])
62 {
63 size_t res;
64 uint8_t hdr[32];
65
66 if (!(c->f = fopen(file, "rb"))) {
67 fprintf(stderr, "Failed to open %s: %s\n", file, strerror(errno));
68 return -1;
69 } else if ((res = fread(hdr, 32, 1, c->f)) != 1) {
70 fprintf(stderr, "Failed to read stream header: %s\n", strerror(errno));
71 fclose(c->f);
72 return -1;
73 } else if (memcmp(hdr, "DKIF", 4)) {
74 fprintf(stderr, "%s is not an IVF file [tag=%.4s|0x%02x%02x%02x%02x]\n",
75 file, hdr, hdr[0], hdr[1], hdr[2], hdr[3]);
76 fclose(c->f);
77 return -1;
78 } else if (memcmp(&hdr[8], "AV01", 4)) {
79 fprintf(stderr, "%s is not an AV1 file [tag=%.4s|0x%02x%02x%02x%02x]\n",
80 file, &hdr[8], hdr[8], hdr[9], hdr[10], hdr[11]);
81 fclose(c->f);
82 return -1;
83 }
84
85 timebase[0] = rl32(&hdr[16]);
86 timebase[1] = rl32(&hdr[20]);
87 const unsigned duration = rl32(&hdr[24]);
88
89 uint8_t data[4];
90 for (*num_frames = 0;; (*num_frames)++) {
91 if ((res = fread(data, 4, 1, c->f)) != 1)
92 break; // EOF
93 fseeko(c->f, rl32(data) + 8, SEEK_CUR);
94 }
95 fps[0] = timebase[0] * *num_frames;
96 fps[1] = timebase[1] * duration;
97 fseeko(c->f, 32, SEEK_SET);
98
99 return 0;
100 }
101
ivf_read(IvfInputContext * const c,Dav1dData * const buf)102 static int ivf_read(IvfInputContext *const c, Dav1dData *const buf) {
103 uint8_t data[8];
104 uint8_t *ptr;
105 size_t res;
106
107 const int64_t off = ftello(c->f);
108 if ((res = fread(data, 4, 1, c->f)) != 1)
109 return -1; // EOF
110 const ptrdiff_t sz = rl32(data);
111 if ((res = fread(data, 8, 1, c->f)) != 1)
112 return -1; // EOF
113 ptr = dav1d_data_create(buf, sz);
114 if (!ptr) return -1;
115 buf->m.offset = off;
116 buf->m.timestamp = rl64(data);
117 if ((res = fread(ptr, sz, 1, c->f)) != 1) {
118 fprintf(stderr, "Failed to read frame data: %s\n", strerror(errno));
119 dav1d_data_unref(buf);
120 return -1;
121 }
122
123 return 0;
124 }
125
ivf_close(IvfInputContext * const c)126 static void ivf_close(IvfInputContext *const c) {
127 fclose(c->f);
128 }
129
130 const Demuxer ivf_demuxer = {
131 .priv_data_size = sizeof(IvfInputContext),
132 .name = "ivf",
133 .probe = ivf_probe,
134 .probe_sz = sizeof(probe_data),
135 .open = ivf_open,
136 .read = ivf_read,
137 .close = ivf_close,
138 };
139