1 /*
2 * Copyright © 2018, VideoLAN and dav1d authors
3 * Copyright © 2018, Two Orioles, LLC
4 * Copyright © 2019, James Almer <[email protected]>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "config.h"
30
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "common/intops.h"
37
38 #include "dav1d/headers.h"
39
40 #include "input/demuxer.h"
41 #include "input/parse.h"
42
43 // these functions are based on an implementation from FFmpeg, and relicensed
44 // with author's permission
45
46 #define PROBE_SIZE 2048
47
annexb_probe(const uint8_t * data)48 static int annexb_probe(const uint8_t *data) {
49 int ret, cnt = 0;
50
51 size_t temporal_unit_size;
52 ret = leb(data + cnt, PROBE_SIZE - cnt, &temporal_unit_size);
53 if (ret < 0)
54 return 0;
55 cnt += ret;
56
57 size_t frame_unit_size;
58 ret = leb(data + cnt, PROBE_SIZE - cnt, &frame_unit_size);
59 if (ret < 0 || ((uint64_t)frame_unit_size + ret) > temporal_unit_size)
60 return 0;
61 cnt += ret;
62
63 temporal_unit_size -= ret;
64
65 size_t obu_unit_size;
66 ret = leb(data + cnt, PROBE_SIZE - cnt, &obu_unit_size);
67 if (ret < 0 || ((uint64_t)obu_unit_size + ret) >= frame_unit_size)
68 return 0;
69 cnt += ret;
70
71 temporal_unit_size -= obu_unit_size + ret;
72 frame_unit_size -= obu_unit_size + ret;
73
74 // Check that the first OBU is a Temporal Delimiter.
75 size_t obu_size;
76 enum Dav1dObuType type;
77 ret = parse_obu_header(data + cnt, imin(PROBE_SIZE - cnt, (int) obu_unit_size),
78 &obu_size, &type, 1);
79 if (ret < 0 || type != DAV1D_OBU_TD || obu_size > 0)
80 return 0;
81 cnt += (int)obu_unit_size;
82
83 // look for first frame and accompanying sequence header
84 int seq = 0;
85 while (cnt < PROBE_SIZE) {
86 ret = leb(data + cnt, PROBE_SIZE - cnt, &obu_unit_size);
87 if (ret < 0 || ((uint64_t)obu_unit_size + ret) > frame_unit_size)
88 return 0;
89 cnt += ret;
90 temporal_unit_size -= ret;
91 frame_unit_size -= ret;
92
93 ret = parse_obu_header(data + cnt, imin(PROBE_SIZE - cnt, (int) obu_unit_size),
94 &obu_size, &type, 1);
95 if (ret < 0)
96 return 0;
97 cnt += (int)obu_unit_size;
98
99 switch (type) {
100 case DAV1D_OBU_SEQ_HDR:
101 seq = 1;
102 break;
103 case DAV1D_OBU_FRAME:
104 case DAV1D_OBU_FRAME_HDR:
105 return seq;
106 case DAV1D_OBU_TD:
107 case DAV1D_OBU_TILE_GRP:
108 return 0;
109 default:
110 break;
111 }
112
113 temporal_unit_size -= obu_unit_size;
114 frame_unit_size -= obu_unit_size;
115 if (frame_unit_size <= 0)
116 return 0;
117 }
118
119 return seq;
120 }
121
122 typedef struct DemuxerPriv {
123 FILE *f;
124 size_t temporal_unit_size;
125 size_t frame_unit_size;
126 } AnnexbInputContext;
127
annexb_open(AnnexbInputContext * const c,const char * const file,unsigned fps[2],unsigned * const num_frames,unsigned timebase[2])128 static int annexb_open(AnnexbInputContext *const c, const char *const file,
129 unsigned fps[2], unsigned *const num_frames, unsigned timebase[2])
130 {
131 int res;
132 size_t len;
133
134 if (!(c->f = fopen(file, "rb"))) {
135 fprintf(stderr, "Failed to open %s: %s\n", file, strerror(errno));
136 return -1;
137 }
138
139 // TODO: Parse sequence header and read timing info if any.
140 fps[0] = 25;
141 fps[1] = 1;
142 timebase[0] = 25;
143 timebase[1] = 1;
144 for (*num_frames = 0;; (*num_frames)++) {
145 res = leb128(c->f, &len);
146 if (res < 0)
147 break;
148 fseeko(c->f, len, SEEK_CUR);
149 }
150 fseeko(c->f, 0, SEEK_SET);
151
152 return 0;
153 }
154
annexb_read(AnnexbInputContext * const c,Dav1dData * const data)155 static int annexb_read(AnnexbInputContext *const c, Dav1dData *const data) {
156 size_t len;
157 int res;
158
159 if (!c->temporal_unit_size) {
160 res = leb128(c->f, &c->temporal_unit_size);
161 if (res < 0) return -1;
162 }
163 if (!c->frame_unit_size) {
164 res = leb128(c->f, &c->frame_unit_size);
165 if (res < 0 || (c->frame_unit_size + res) > c->temporal_unit_size) return -1;
166 c->temporal_unit_size -= res;
167 }
168 res = leb128(c->f, &len);
169 if (res < 0 || (len + res) > c->frame_unit_size) return -1;
170 uint8_t *ptr = dav1d_data_create(data, len);
171 if (!ptr) return -1;
172 c->temporal_unit_size -= len + res;
173 c->frame_unit_size -= len + res;
174 if (fread(ptr, len, 1, c->f) != 1) {
175 fprintf(stderr, "Failed to read frame data: %s\n", strerror(errno));
176 dav1d_data_unref(data);
177 return -1;
178 }
179
180 return 0;
181 }
182
annexb_close(AnnexbInputContext * const c)183 static void annexb_close(AnnexbInputContext *const c) {
184 fclose(c->f);
185 }
186
187 const Demuxer annexb_demuxer = {
188 .priv_data_size = sizeof(AnnexbInputContext),
189 .name = "annexb",
190 .probe = annexb_probe,
191 .probe_sz = PROBE_SIZE,
192 .open = annexb_open,
193 .read = annexb_read,
194 .seek = NULL,
195 .close = annexb_close,
196 };
197