1 /*
2 * Copyright © 2019, VideoLAN and dav1d authors
3 * Copyright © 2019, 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 #if HAVE_SYS_TYPES_H
36 #include <sys/types.h>
37 #endif
38
39 #include "dav1d/headers.h"
40
41 #include "input/demuxer.h"
42 #include "input/parse.h"
43
44 #define PROBE_SIZE 2048
45
section5_probe(const uint8_t * data)46 static int section5_probe(const uint8_t *data) {
47 int ret, cnt = 0;
48
49 // Check that the first OBU is a Temporal Delimiter.
50 size_t obu_size;
51 enum Dav1dObuType type;
52 ret = parse_obu_header(data + cnt, PROBE_SIZE - cnt,
53 &obu_size, &type, 0);
54 if (ret < 0 || type != DAV1D_OBU_TD || obu_size > 0)
55 return 0;
56 cnt += ret;
57
58 // look for first frame and accompanying sequence header
59 int seq = 0;
60 while (cnt < PROBE_SIZE) {
61 ret = parse_obu_header(data + cnt, PROBE_SIZE - cnt,
62 &obu_size, &type, 0);
63 if (ret < 0)
64 return 0;
65 cnt += ret;
66
67 switch (type) {
68 case DAV1D_OBU_SEQ_HDR:
69 seq = 1;
70 break;
71 case DAV1D_OBU_FRAME:
72 case DAV1D_OBU_FRAME_HDR:
73 return seq;
74 case DAV1D_OBU_TD:
75 case DAV1D_OBU_TILE_GRP:
76 return 0;
77 default:
78 break;
79 }
80 }
81
82 return seq;
83 }
84
85 typedef struct DemuxerPriv {
86 FILE *f;
87 } Section5InputContext;
88
section5_open(Section5InputContext * const c,const char * const file,unsigned fps[2],unsigned * const num_frames,unsigned timebase[2])89 static int section5_open(Section5InputContext *const c, const char *const file,
90 unsigned fps[2], unsigned *const num_frames, unsigned timebase[2])
91 {
92 if (!(c->f = fopen(file, "rb"))) {
93 fprintf(stderr, "Failed to open %s: %s\n", file, strerror(errno));
94 return -1;
95 }
96
97 // TODO: Parse sequence header and read timing info if any.
98 fps[0] = 25;
99 fps[1] = 1;
100 timebase[0] = 25;
101 timebase[1] = 1;
102 *num_frames = 0;
103 for (;;) {
104 uint8_t byte[2];
105
106 if (fread(&byte[0], 1, 1, c->f) < 1)
107 break;
108 const enum Dav1dObuType obu_type = (byte[0] >> 3) & 0xf;
109 if (obu_type == DAV1D_OBU_TD)
110 (*num_frames)++;
111 const int has_length_field = byte[0] & 0x2;
112 if (!has_length_field)
113 return -1;
114 const int has_extension = byte[0] & 0x4;
115 if (has_extension && fread(&byte[1], 1, 1, c->f) < 1)
116 return -1;
117 size_t len;
118 const int res = leb128(c->f, &len);
119 if (res < 0)
120 return -1;
121 fseeko(c->f, len, SEEK_CUR); // skip packet
122 }
123 fseeko(c->f, 0, SEEK_SET);
124
125 return 0;
126 }
127
section5_read(Section5InputContext * const c,Dav1dData * const data)128 static int section5_read(Section5InputContext *const c, Dav1dData *const data) {
129 size_t total_bytes = 0;
130
131 for (int first = 1;; first = 0) {
132 uint8_t byte[2];
133
134 if (fread(&byte[0], 1, 1, c->f) < 1) {
135 if (!first && feof(c->f)) break;
136 return -1;
137 }
138 const enum Dav1dObuType obu_type = (byte[0] >> 3) & 0xf;
139 if (first) {
140 if (obu_type != DAV1D_OBU_TD)
141 return -1;
142 } else {
143 if (obu_type == DAV1D_OBU_TD) {
144 // include TD in next packet
145 fseeko(c->f, -1, SEEK_CUR);
146 break;
147 }
148 }
149 const int has_length_field = byte[0] & 0x2;
150 if (!has_length_field)
151 return -1;
152 const int has_extension = !!(byte[0] & 0x4);
153 if (has_extension && fread(&byte[1], 1, 1, c->f) < 1)
154 return -1;
155 size_t len;
156 const int res = leb128(c->f, &len);
157 if (res < 0)
158 return -1;
159 total_bytes += 1 + has_extension + res + len;
160 fseeko(c->f, len, SEEK_CUR); // skip packet, we'll read it below
161 }
162
163 fseeko(c->f, -(off_t)total_bytes, SEEK_CUR);
164 uint8_t *ptr = dav1d_data_create(data, total_bytes);
165 if (!ptr) return -1;
166 if (fread(ptr, total_bytes, 1, c->f) != 1) {
167 fprintf(stderr, "Failed to read frame data: %s\n", strerror(errno));
168 dav1d_data_unref(data);
169 return -1;
170 }
171
172 return 0;
173 }
174
section5_close(Section5InputContext * const c)175 static void section5_close(Section5InputContext *const c) {
176 fclose(c->f);
177 }
178
179 const Demuxer section5_demuxer = {
180 .priv_data_size = sizeof(Section5InputContext),
181 .name = "section5",
182 .probe = section5_probe,
183 .probe_sz = PROBE_SIZE,
184 .open = section5_open,
185 .read = section5_read,
186 .seek = NULL,
187 .close = section5_close,
188 };
189