Mercurial > libavformat.hg
annotate r3d.c @ 5185:0c742c9c4aa7 libavformat
Try harder to avoid false positives for DV probe.
Require at least one signature match per provided 1MB of probe data,
and if there is only a single match, return at most MAX/4.
Fixes issue1382 but could/should probably still be improved.
author | reimar |
---|---|
date | Mon, 14 Sep 2009 22:03:07 +0000 |
parents | 74d599c373f9 |
children | f41a84612842 |
rev | line source |
---|---|
4257 | 1 /* |
2 * R3D REDCODE demuxer | |
3 * Copyright (c) 2008 Baptiste Coudurier <baptiste dot coudurier at gmail dot com> | |
4 * | |
5 * This file is part of FFmpeg. | |
6 * | |
7 * FFmpeg is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU Lesser General Public | |
9 * License as published by the Free Software Foundation; either | |
10 * version 2.1 of the License, or (at your option) any later version. | |
11 * | |
12 * FFmpeg is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
18 * License along with FFmpeg; if not, write to the Free Software | |
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 */ | |
21 | |
22 //#define DEBUG | |
23 | |
24 #include "libavutil/intreadwrite.h" | |
25 #include "avformat.h" | |
26 | |
27 typedef struct { | |
28 unsigned video_offsets_count; | |
29 unsigned *video_offsets; | |
30 unsigned rdvo_offset; | |
31 } R3DContext; | |
32 | |
33 typedef struct { | |
34 unsigned size; | |
35 uint32_t tag; | |
36 uint64_t offset; | |
37 } Atom; | |
38 | |
39 static int read_atom(AVFormatContext *s, Atom *atom) | |
40 { | |
41 atom->offset = url_ftell(s->pb); | |
42 atom->size = get_be32(s->pb); | |
43 if (atom->size < 8) | |
44 return -1; | |
45 atom->tag = get_le32(s->pb); | |
46 dprintf(s, "atom %d %.4s offset %#llx\n", | |
47 atom->size, (char*)&atom->tag, atom->offset); | |
48 return atom->size; | |
49 } | |
50 | |
51 static int r3d_read_red1(AVFormatContext *s) | |
52 { | |
53 AVStream *st = av_new_stream(s, 0); | |
4623 | 54 char filename[258]; |
4257 | 55 int tmp, tmp2; |
56 | |
57 if (!st) | |
5148
74d599c373f9
return AVERROR(ENOMEM) if stream could not be allocated
bcoudurier
parents:
5147
diff
changeset
|
58 return AVERROR(ENOMEM); |
4257 | 59 st->codec->codec_type = CODEC_TYPE_VIDEO; |
60 st->codec->codec_id = CODEC_ID_JPEG2000; | |
61 | |
62 tmp = get_byte(s->pb); // major version | |
63 tmp2 = get_byte(s->pb); // minor version | |
64 dprintf(s, "version %d.%d\n", tmp, tmp2); | |
65 | |
66 tmp = get_be16(s->pb); // unknown | |
67 dprintf(s, "unknown1 %d\n", tmp); | |
68 | |
69 tmp = get_be32(s->pb); | |
70 av_set_pts_info(st, 32, 1, tmp); | |
71 | |
72 tmp = get_be32(s->pb); // filenum | |
73 dprintf(s, "filenum %d\n", tmp); | |
74 | |
75 url_fskip(s->pb, 32); // unknown | |
76 | |
77 st->codec->width = get_be32(s->pb); | |
78 st->codec->height = get_be32(s->pb); | |
79 | |
80 tmp = get_be16(s->pb); // unknown | |
81 dprintf(s, "unknown2 %d\n", tmp); | |
82 | |
83 st->codec->time_base.den = get_be16(s->pb); | |
84 st->codec->time_base.num = get_be16(s->pb); | |
85 | |
86 tmp = get_byte(s->pb); // audio channels | |
87 dprintf(s, "audio channels %d\n", tmp); | |
88 if (tmp > 0) { | |
89 AVStream *ast = av_new_stream(s, 1); | |
5147
64ad0b0e120d
check av_new_stream return value, patch by Kent Mein, mein at cs dot umn dot edu
bcoudurier
parents:
4623
diff
changeset
|
90 if (!ast) |
64ad0b0e120d
check av_new_stream return value, patch by Kent Mein, mein at cs dot umn dot edu
bcoudurier
parents:
4623
diff
changeset
|
91 return AVERROR(ENOMEM); |
4257 | 92 ast->codec->codec_type = CODEC_TYPE_AUDIO; |
93 ast->codec->codec_id = CODEC_ID_PCM_S32BE; | |
94 ast->codec->channels = tmp; | |
95 av_set_pts_info(ast, 32, 1, st->time_base.den); | |
96 } | |
97 | |
4623 | 98 get_buffer(s->pb, filename, 257); |
99 filename[sizeof(filename)-1] = 0; | |
100 av_metadata_set(&st->metadata, "filename", filename); | |
4257 | 101 |
4623 | 102 dprintf(s, "filename %s\n", filename); |
4257 | 103 dprintf(s, "resolution %dx%d\n", st->codec->width, st->codec->height); |
104 dprintf(s, "timescale %d\n", st->time_base.den); | |
105 dprintf(s, "frame rate %d/%d\n", | |
106 st->codec->time_base.num, st->codec->time_base.den); | |
107 | |
108 return 0; | |
109 } | |
110 | |
111 static int r3d_read_rdvo(AVFormatContext *s, Atom *atom) | |
112 { | |
113 R3DContext *r3d = s->priv_data; | |
114 AVStream *st = s->streams[0]; | |
115 int i; | |
116 | |
117 r3d->video_offsets_count = (atom->size - 8) / 4; | |
118 r3d->video_offsets = av_malloc(atom->size); | |
119 if (!r3d->video_offsets) | |
120 return AVERROR(ENOMEM); | |
121 | |
122 for (i = 0; i < r3d->video_offsets_count; i++) { | |
123 r3d->video_offsets[i] = get_be32(s->pb); | |
124 if (!r3d->video_offsets[i]) { | |
125 r3d->video_offsets_count = i; | |
126 break; | |
127 } | |
128 dprintf(s, "video offset %d: %#x\n", i, r3d->video_offsets[i]); | |
129 } | |
130 | |
131 if (st->codec->time_base.den) | |
132 st->duration = (uint64_t)r3d->video_offsets_count* | |
133 st->time_base.den*st->codec->time_base.num/st->codec->time_base.den; | |
134 dprintf(s, "duration %lld\n", st->duration); | |
135 | |
136 return 0; | |
137 } | |
138 | |
139 static void r3d_read_reos(AVFormatContext *s) | |
140 { | |
141 R3DContext *r3d = s->priv_data; | |
142 int tmp; | |
143 | |
144 r3d->rdvo_offset = get_be32(s->pb); | |
145 get_be32(s->pb); // rdvs offset | |
146 get_be32(s->pb); // rdao offset | |
147 get_be32(s->pb); // rdas offset | |
148 | |
149 tmp = get_be32(s->pb); | |
150 dprintf(s, "num video chunks %d\n", tmp); | |
151 | |
152 tmp = get_be32(s->pb); | |
153 dprintf(s, "num audio chunks %d\n", tmp); | |
154 | |
155 url_fskip(s->pb, 6*4); | |
156 } | |
157 | |
158 static int r3d_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
159 { | |
160 R3DContext *r3d = s->priv_data; | |
161 Atom atom; | |
162 int ret; | |
163 | |
164 if (read_atom(s, &atom) < 0) { | |
165 av_log(s, AV_LOG_ERROR, "error reading atom\n"); | |
166 return -1; | |
167 } | |
168 if (atom.tag == MKTAG('R','E','D','1')) { | |
169 if ((ret = r3d_read_red1(s)) < 0) { | |
170 av_log(s, AV_LOG_ERROR, "error parsing 'red1' atom\n"); | |
171 return ret; | |
172 } | |
173 } else { | |
174 av_log(s, AV_LOG_ERROR, "could not find 'red1' atom\n"); | |
175 return -1; | |
176 } | |
177 | |
178 s->data_offset = url_ftell(s->pb); | |
179 dprintf(s, "data offset %#llx\n", s->data_offset); | |
180 if (url_is_streamed(s->pb)) | |
181 return 0; | |
182 // find REOB/REOF/REOS to load index | |
183 url_fseek(s->pb, url_fsize(s->pb)-48-8, SEEK_SET); | |
184 if (read_atom(s, &atom) < 0) | |
185 av_log(s, AV_LOG_ERROR, "error reading end atom\n"); | |
186 | |
187 if (atom.tag != MKTAG('R','E','O','B') && | |
188 atom.tag != MKTAG('R','E','O','F') && | |
189 atom.tag != MKTAG('R','E','O','S')) | |
190 goto out; | |
191 | |
192 r3d_read_reos(s); | |
193 | |
194 if (r3d->rdvo_offset) { | |
195 url_fseek(s->pb, r3d->rdvo_offset, SEEK_SET); | |
196 if (read_atom(s, &atom) < 0) | |
197 av_log(s, AV_LOG_ERROR, "error reading 'rdvo' atom\n"); | |
198 if (atom.tag == MKTAG('R','D','V','O')) { | |
199 if (r3d_read_rdvo(s, &atom) < 0) | |
200 av_log(s, AV_LOG_ERROR, "error parsing 'rdvo' atom\n"); | |
201 } | |
202 } | |
203 | |
204 out: | |
205 url_fseek(s->pb, s->data_offset, SEEK_SET); | |
206 return 0; | |
207 } | |
208 | |
209 static int r3d_read_redv(AVFormatContext *s, AVPacket *pkt, Atom *atom) | |
210 { | |
211 AVStream *st = s->streams[0]; | |
212 int tmp, tmp2; | |
213 uint64_t pos = url_ftell(s->pb); | |
214 unsigned dts; | |
215 | |
216 dts = get_be32(s->pb); | |
217 | |
218 tmp = get_be32(s->pb); | |
219 dprintf(s, "frame num %d\n", tmp); | |
220 | |
221 tmp = get_byte(s->pb); // major version | |
222 tmp2 = get_byte(s->pb); // minor version | |
223 dprintf(s, "version %d.%d\n", tmp, tmp2); | |
224 | |
225 tmp = get_be16(s->pb); // unknown | |
226 dprintf(s, "unknown %d\n", tmp); | |
227 | |
228 if (tmp > 4) { | |
229 tmp = get_be16(s->pb); // unknown | |
230 dprintf(s, "unknown %d\n", tmp); | |
231 | |
232 tmp = get_be16(s->pb); // unknown | |
233 dprintf(s, "unknown %d\n", tmp); | |
234 | |
235 tmp = get_be32(s->pb); | |
236 dprintf(s, "width %d\n", tmp); | |
237 tmp = get_be32(s->pb); | |
238 dprintf(s, "height %d\n", tmp); | |
239 | |
240 tmp = get_be32(s->pb); | |
241 dprintf(s, "metadata len %d\n", tmp); | |
242 } | |
243 tmp = atom->size - 8 - (url_ftell(s->pb) - pos); | |
244 if (tmp < 0) | |
245 return -1; | |
246 | |
247 if (av_get_packet(s->pb, pkt, tmp) != tmp) { | |
248 av_log(s, AV_LOG_ERROR, "error reading video packet\n"); | |
249 return -1; | |
250 } | |
251 | |
252 pkt->stream_index = 0; | |
253 pkt->dts = dts; | |
254 if (st->codec->time_base.den) | |
255 pkt->duration = (uint64_t)st->time_base.den* | |
256 st->codec->time_base.num/st->codec->time_base.den; | |
257 dprintf(s, "pkt dts %lld duration %d\n", pkt->dts, pkt->duration); | |
258 | |
259 return 0; | |
260 } | |
261 | |
262 static int r3d_read_reda(AVFormatContext *s, AVPacket *pkt, Atom *atom) | |
263 { | |
264 AVStream *st = s->streams[1]; | |
265 int tmp, tmp2, samples, size; | |
266 uint64_t pos = url_ftell(s->pb); | |
267 unsigned dts; | |
268 | |
269 dts = get_be32(s->pb); | |
270 | |
271 st->codec->sample_rate = get_be32(s->pb); | |
272 | |
273 samples = get_be32(s->pb); | |
274 | |
275 tmp = get_be32(s->pb); | |
276 dprintf(s, "packet num %d\n", tmp); | |
277 | |
278 tmp = get_be16(s->pb); // unkown | |
279 dprintf(s, "unknown %d\n", tmp); | |
280 | |
281 tmp = get_byte(s->pb); // major version | |
282 tmp2 = get_byte(s->pb); // minor version | |
283 dprintf(s, "version %d.%d\n", tmp, tmp2); | |
284 | |
285 tmp = get_be32(s->pb); // unknown | |
286 dprintf(s, "unknown %d\n", tmp); | |
287 | |
288 size = atom->size - 8 - (url_ftell(s->pb) - pos); | |
289 if (size < 0) | |
290 return -1; | |
291 if (av_get_packet(s->pb, pkt, size) != size) { | |
292 av_log(s, AV_LOG_ERROR, "error reading video packet\n"); | |
293 return -1; | |
294 } | |
295 | |
296 pkt->stream_index = 1; | |
297 pkt->dts = dts; | |
298 pkt->duration = av_rescale(samples, st->time_base.den, st->codec->sample_rate); | |
299 dprintf(s, "pkt dts %lld duration %d samples %d sample rate %d\n", | |
300 pkt->dts, pkt->duration, samples, st->codec->sample_rate); | |
301 | |
302 return 0; | |
303 } | |
304 | |
305 static int r3d_read_packet(AVFormatContext *s, AVPacket *pkt) | |
306 { | |
307 Atom atom; | |
308 int err = 0; | |
309 | |
310 while (!err) { | |
311 if (read_atom(s, &atom) < 0) { | |
312 err = -1; | |
313 break; | |
314 } | |
315 switch (atom.tag) { | |
316 case MKTAG('R','E','D','V'): | |
317 if (s->streams[0]->discard == AVDISCARD_ALL) | |
318 goto skip; | |
319 if (!(err = r3d_read_redv(s, pkt, &atom))) | |
320 return 0; | |
321 break; | |
322 case MKTAG('R','E','D','A'): | |
323 if (s->nb_streams < 2) | |
324 return -1; | |
325 if (s->streams[1]->discard == AVDISCARD_ALL) | |
326 goto skip; | |
327 if (!(err = r3d_read_reda(s, pkt, &atom))) | |
328 return 0; | |
329 break; | |
330 default: | |
331 skip: | |
332 url_fskip(s->pb, atom.size-8); | |
333 } | |
334 } | |
335 return err; | |
336 } | |
337 | |
338 static int r3d_probe(AVProbeData *p) | |
339 { | |
340 if (AV_RL32(p->buf + 4) == MKTAG('R','E','D','1')) | |
341 return AVPROBE_SCORE_MAX; | |
342 return 0; | |
343 } | |
344 | |
345 static int r3d_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags) | |
346 { | |
347 AVStream *st = s->streams[0]; // video stream | |
348 R3DContext *r3d = s->priv_data; | |
349 int frame_num; | |
350 | |
351 if (!st->codec->time_base.num || !st->time_base.den) | |
352 return -1; | |
353 | |
354 frame_num = sample_time*st->codec->time_base.den/ | |
355 ((int64_t)st->codec->time_base.num*st->time_base.den); | |
356 dprintf(s, "seek frame num %d timestamp %lld\n", frame_num, sample_time); | |
357 | |
358 if (frame_num < r3d->video_offsets_count) { | |
359 url_fseek(s->pb, r3d->video_offsets_count, SEEK_SET); | |
360 } else { | |
361 av_log(s, AV_LOG_ERROR, "could not seek to frame %d\n", frame_num); | |
362 return -1; | |
363 } | |
364 | |
365 return 0; | |
366 } | |
367 | |
368 static int r3d_close(AVFormatContext *s) | |
369 { | |
370 R3DContext *r3d = s->priv_data; | |
371 | |
372 av_freep(&r3d->video_offsets); | |
373 | |
374 return 0; | |
375 } | |
376 | |
377 AVInputFormat r3d_demuxer = { | |
378 "r3d", | |
379 NULL_IF_CONFIG_SMALL("REDCODE R3D format"), | |
380 sizeof(R3DContext), | |
381 r3d_probe, | |
382 r3d_read_header, | |
383 r3d_read_packet, | |
384 r3d_close, | |
385 r3d_seek, | |
386 }; |