3 * Copyright (c) 2008 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/mathematics.h"
28 typedef struct R3DContext {
29 unsigned video_offsets_count;
30 unsigned *video_offsets;
42 static int read_atom(AVFormatContext *s, Atom *atom)
44 atom->offset = avio_tell(s->pb);
45 atom->size = avio_rb32(s->pb);
48 atom->tag = avio_rl32(s->pb);
49 av_log(s, AV_LOG_TRACE, "atom %u %.4s offset %#"PRIx64"\n",
50 atom->size, (char*)&atom->tag, atom->offset);
54 static int r3d_read_red1(AVFormatContext *s)
56 AVStream *st = avformat_new_stream(s, NULL);
57 R3DContext *r3d = s->priv_data;
64 return AVERROR(ENOMEM);
65 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
66 st->codecpar->codec_id = AV_CODEC_ID_JPEG2000;
68 tmp = avio_r8(s->pb); // major version
69 tmp2 = avio_r8(s->pb); // minor version
70 av_log(s, AV_LOG_TRACE, "version %d.%d\n", tmp, tmp2);
72 tmp = avio_rb16(s->pb); // unknown
73 av_log(s, AV_LOG_TRACE, "unknown1 %d\n", tmp);
75 tmp = avio_rb32(s->pb);
76 avpriv_set_pts_info(st, 32, 1, tmp);
78 tmp = avio_rb32(s->pb); // filenum
79 av_log(s, AV_LOG_TRACE, "filenum %d\n", tmp);
81 avio_skip(s->pb, 32); // unknown
83 st->codecpar->width = avio_rb32(s->pb);
84 st->codecpar->height = avio_rb32(s->pb);
86 tmp = avio_rb16(s->pb); // unknown
87 av_log(s, AV_LOG_TRACE, "unknown2 %d\n", tmp);
89 framerate.num = avio_rb16(s->pb);
90 framerate.den = avio_rb16(s->pb);
91 if (framerate.num > 0 && framerate.den > 0) {
92 #if FF_API_R_FRAME_RATE
95 st->avg_frame_rate = framerate;
98 r3d->audio_channels = avio_r8(s->pb); // audio channels
99 av_log(s, AV_LOG_TRACE, "audio channels %d\n", tmp);
101 avio_read(s->pb, filename, 257);
102 filename[sizeof(filename)-1] = 0;
103 av_dict_set(&st->metadata, "filename", filename, 0);
105 av_log(s, AV_LOG_TRACE, "filename %s\n", filename);
106 av_log(s, AV_LOG_TRACE, "resolution %dx%d\n", st->codecpar->width, st->codecpar->height);
107 av_log(s, AV_LOG_TRACE, "timescale %d\n", st->time_base.den);
108 av_log(s, AV_LOG_TRACE, "frame rate %d/%d\n",
109 framerate.num, framerate.den);
114 static int r3d_read_rdvo(AVFormatContext *s, Atom *atom)
116 R3DContext *r3d = s->priv_data;
117 AVStream *st = s->streams[0];
120 r3d->video_offsets_count = (atom->size - 8) / 4;
121 r3d->video_offsets = av_malloc(atom->size);
122 if (!r3d->video_offsets)
123 return AVERROR(ENOMEM);
125 for (i = 0; i < r3d->video_offsets_count; i++) {
126 r3d->video_offsets[i] = avio_rb32(s->pb);
127 if (!r3d->video_offsets[i]) {
128 r3d->video_offsets_count = i;
131 av_log(s, AV_LOG_TRACE, "video offset %d: %#x\n", i, r3d->video_offsets[i]);
134 if (st->avg_frame_rate.num)
135 st->duration = av_rescale_q(r3d->video_offsets_count,
136 av_inv_q(st->avg_frame_rate),
138 av_log(s, AV_LOG_TRACE, "duration %"PRId64"\n", st->duration);
143 static void r3d_read_reos(AVFormatContext *s)
145 R3DContext *r3d = s->priv_data;
148 r3d->rdvo_offset = avio_rb32(s->pb);
149 avio_rb32(s->pb); // rdvs offset
150 avio_rb32(s->pb); // rdao offset
151 avio_rb32(s->pb); // rdas offset
153 tmp = avio_rb32(s->pb);
154 av_log(s, AV_LOG_TRACE, "num video chunks %d\n", tmp);
156 tmp = avio_rb32(s->pb);
157 av_log(s, AV_LOG_TRACE, "num audio chunks %d\n", tmp);
159 avio_skip(s->pb, 6*4);
162 static int r3d_read_header(AVFormatContext *s)
164 R3DContext *r3d = s->priv_data;
168 if (read_atom(s, &atom) < 0) {
169 av_log(s, AV_LOG_ERROR, "error reading atom\n");
172 if (atom.tag == MKTAG('R','E','D','1')) {
173 if ((ret = r3d_read_red1(s)) < 0) {
174 av_log(s, AV_LOG_ERROR, "error parsing 'red1' atom\n");
178 av_log(s, AV_LOG_ERROR, "could not find 'red1' atom\n");
182 /* we cannot create the audio stream now because we do not know the
184 if (r3d->audio_channels)
185 s->ctx_flags |= AVFMTCTX_NOHEADER;
187 s->internal->data_offset = avio_tell(s->pb);
188 av_log(s, AV_LOG_TRACE, "data offset %#"PRIx64"\n", s->internal->data_offset);
189 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
191 // find REOB/REOF/REOS to load index
192 avio_seek(s->pb, avio_size(s->pb)-48-8, SEEK_SET);
193 if (read_atom(s, &atom) < 0)
194 av_log(s, AV_LOG_ERROR, "error reading end atom\n");
196 if (atom.tag != MKTAG('R','E','O','B') &&
197 atom.tag != MKTAG('R','E','O','F') &&
198 atom.tag != MKTAG('R','E','O','S'))
203 if (r3d->rdvo_offset) {
204 avio_seek(s->pb, r3d->rdvo_offset, SEEK_SET);
205 if (read_atom(s, &atom) < 0)
206 av_log(s, AV_LOG_ERROR, "error reading 'rdvo' atom\n");
207 if (atom.tag == MKTAG('R','D','V','O')) {
208 if (r3d_read_rdvo(s, &atom) < 0)
209 av_log(s, AV_LOG_ERROR, "error parsing 'rdvo' atom\n");
214 avio_seek(s->pb, s->internal->data_offset, SEEK_SET);
218 static int r3d_read_redv(AVFormatContext *s, AVPacket *pkt, Atom *atom)
220 AVStream *st = s->streams[0];
223 int64_t pos = avio_tell(s->pb);
227 dts = avio_rb32(s->pb);
229 tmp = avio_rb32(s->pb);
230 av_log(s, AV_LOG_TRACE, "frame num %d\n", tmp);
232 tmp = avio_r8(s->pb); // major version
233 tmp2 = avio_r8(s->pb); // minor version
234 av_log(s, AV_LOG_TRACE, "version %d.%d\n", tmp, tmp2);
236 tmp = avio_rb16(s->pb); // unknown
237 av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
240 tmp = avio_rb16(s->pb); // unknown
241 av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
243 tmp = avio_rb16(s->pb); // unknown
244 av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
246 tmp = avio_rb32(s->pb);
247 av_log(s, AV_LOG_TRACE, "width %d\n", tmp);
248 tmp = avio_rb32(s->pb);
249 av_log(s, AV_LOG_TRACE, "height %d\n", tmp);
251 tmp = avio_rb32(s->pb);
252 av_log(s, AV_LOG_TRACE, "metadata len %d\n", tmp);
254 tmp = atom->size - 8 - (avio_tell(s->pb) - pos);
257 ret = av_get_packet(s->pb, pkt, tmp);
259 av_log(s, AV_LOG_ERROR, "error reading video packet\n");
263 pkt->stream_index = 0;
265 if (st->avg_frame_rate.num)
266 pkt->duration = (uint64_t)st->time_base.den*
267 st->avg_frame_rate.den/st->avg_frame_rate.num;
268 av_log(s, AV_LOG_TRACE, "pkt dts %"PRId64" duration %"PRId64"\n", pkt->dts, pkt->duration);
273 static int r3d_read_reda(AVFormatContext *s, AVPacket *pkt, Atom *atom)
275 R3DContext *r3d = s->priv_data;
277 int av_unused tmp, tmp2;
279 int64_t pos = avio_tell(s->pb);
283 if (s->nb_streams < 2) {
284 st = avformat_new_stream(s, NULL);
286 return AVERROR(ENOMEM);
287 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
288 st->codecpar->codec_id = AV_CODEC_ID_PCM_S32BE;
289 st->codecpar->channels = r3d->audio_channels;
290 avpriv_set_pts_info(st, 32, 1, s->streams[0]->time_base.den);
295 dts = avio_rb32(s->pb);
297 st->codecpar->sample_rate = avio_rb32(s->pb);
298 if (st->codecpar->sample_rate <= 0) {
299 av_log(s, AV_LOG_ERROR, "Bad sample rate\n");
300 return AVERROR_INVALIDDATA;
303 samples = avio_rb32(s->pb);
305 tmp = avio_rb32(s->pb);
306 av_log(s, AV_LOG_TRACE, "packet num %d\n", tmp);
308 tmp = avio_rb16(s->pb); // unknown
309 av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
311 tmp = avio_r8(s->pb); // major version
312 tmp2 = avio_r8(s->pb); // minor version
313 av_log(s, AV_LOG_TRACE, "version %d.%d\n", tmp, tmp2);
315 tmp = avio_rb32(s->pb); // unknown
316 av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
318 size = atom->size - 8 - (avio_tell(s->pb) - pos);
321 ret = av_get_packet(s->pb, pkt, size);
323 av_log(s, AV_LOG_ERROR, "error reading audio packet\n");
327 pkt->stream_index = 1;
329 if (st->codecpar->sample_rate)
330 pkt->duration = av_rescale(samples, st->time_base.den, st->codecpar->sample_rate);
331 av_log(s, AV_LOG_TRACE, "pkt dts %"PRId64" duration %"PRId64" samples %d sample rate %d\n",
332 pkt->dts, pkt->duration, samples, st->codecpar->sample_rate);
337 static int r3d_read_packet(AVFormatContext *s, AVPacket *pkt)
339 R3DContext *r3d = s->priv_data;
344 if (read_atom(s, &atom) < 0) {
349 case MKTAG('R','E','D','V'):
350 if (s->streams[0]->discard == AVDISCARD_ALL)
352 if (!(err = r3d_read_redv(s, pkt, &atom)))
355 case MKTAG('R','E','D','A'):
356 if (!r3d->audio_channels)
358 if (s->nb_streams >= 2 && s->streams[1]->discard == AVDISCARD_ALL)
360 if (!(err = r3d_read_reda(s, pkt, &atom)))
365 avio_skip(s->pb, atom.size-8);
371 static int r3d_probe(AVProbeData *p)
373 if (AV_RL32(p->buf + 4) == MKTAG('R','E','D','1'))
374 return AVPROBE_SCORE_MAX;
378 static int r3d_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
380 AVStream *st = s->streams[0]; // video stream
381 R3DContext *r3d = s->priv_data;
384 if (!st->avg_frame_rate.num)
387 frame_num = av_rescale_q(sample_time, st->time_base,
388 av_inv_q(st->avg_frame_rate));
389 av_log(s, AV_LOG_TRACE, "seek frame num %d timestamp %"PRId64"\n",
390 frame_num, sample_time);
392 if (frame_num < r3d->video_offsets_count) {
393 if (avio_seek(s->pb, r3d->video_offsets_count, SEEK_SET) < 0)
396 av_log(s, AV_LOG_ERROR, "could not seek to frame %d\n", frame_num);
403 static int r3d_close(AVFormatContext *s)
405 R3DContext *r3d = s->priv_data;
407 av_freep(&r3d->video_offsets);
412 AVInputFormat ff_r3d_demuxer = {
414 .long_name = NULL_IF_CONFIG_SMALL("REDCODE R3D"),
415 .priv_data_size = sizeof(R3DContext),
416 .read_probe = r3d_probe,
417 .read_header = r3d_read_header,
418 .read_packet = r3d_read_packet,
419 .read_close = r3d_close,
420 .read_seek = r3d_seek,