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
;
41 static int read_atom(AVFormatContext
*s
, Atom
*atom
)
43 atom
->offset
= avio_tell(s
->pb
);
44 atom
->size
= avio_rb32(s
->pb
);
47 atom
->tag
= avio_rl32(s
->pb
);
48 av_log(s
, AV_LOG_TRACE
, "atom %u %.4s offset %#"PRIx64
"\n",
49 atom
->size
, (char*)&atom
->tag
, atom
->offset
);
53 static int r3d_read_red1(AVFormatContext
*s
)
55 AVStream
*st
= avformat_new_stream(s
, NULL
);
56 R3DContext
*r3d
= s
->priv_data
;
63 return AVERROR(ENOMEM
);
64 st
->codecpar
->codec_type
= AVMEDIA_TYPE_VIDEO
;
65 st
->codecpar
->codec_id
= AV_CODEC_ID_JPEG2000
;
67 tmp
= avio_r8(s
->pb
); // major version
68 tmp2
= avio_r8(s
->pb
); // minor version
69 av_log(s
, AV_LOG_TRACE
, "version %d.%d\n", tmp
, tmp2
);
71 tmp
= avio_rb16(s
->pb
); // unknown
72 av_log(s
, AV_LOG_TRACE
, "unknown1 %d\n", tmp
);
74 tmp
= avio_rb32(s
->pb
);
75 avpriv_set_pts_info(st
, 32, 1, tmp
);
77 tmp
= avio_rb32(s
->pb
); // filenum
78 av_log(s
, AV_LOG_TRACE
, "filenum %d\n", tmp
);
80 avio_skip(s
->pb
, 32); // unknown
82 st
->codecpar
->width
= avio_rb32(s
->pb
);
83 st
->codecpar
->height
= avio_rb32(s
->pb
);
85 tmp
= avio_rb16(s
->pb
); // unknown
86 av_log(s
, AV_LOG_TRACE
, "unknown2 %d\n", tmp
);
88 framerate
.num
= avio_rb16(s
->pb
);
89 framerate
.den
= avio_rb16(s
->pb
);
90 if (framerate
.num
> 0 && framerate
.den
> 0) {
91 #if FF_API_R_FRAME_RATE
94 st
->avg_frame_rate
= framerate
;
97 r3d
->audio_channels
= avio_r8(s
->pb
); // audio channels
98 av_log(s
, AV_LOG_TRACE
, "audio channels %d\n", tmp
);
100 avio_read(s
->pb
, filename
, 257);
101 filename
[sizeof(filename
)-1] = 0;
102 av_dict_set(&st
->metadata
, "filename", filename
, 0);
104 av_log(s
, AV_LOG_TRACE
, "filename %s\n", filename
);
105 av_log(s
, AV_LOG_TRACE
, "resolution %dx%d\n", st
->codecpar
->width
, st
->codecpar
->height
);
106 av_log(s
, AV_LOG_TRACE
, "timescale %d\n", st
->time_base
.den
);
107 av_log(s
, AV_LOG_TRACE
, "frame rate %d/%d\n",
108 framerate
.num
, framerate
.den
);
113 static int r3d_read_rdvo(AVFormatContext
*s
, Atom
*atom
)
115 R3DContext
*r3d
= s
->priv_data
;
116 AVStream
*st
= s
->streams
[0];
119 r3d
->video_offsets_count
= (atom
->size
- 8) / 4;
121 for (i
= 0; i
< r3d
->video_offsets_count
; i
++) {
122 unsigned video_offset
= avio_rb32(s
->pb
);
124 r3d
->video_offsets_count
= i
;
127 av_log(s
, AV_LOG_TRACE
, "video offset %d: %#x\n", i
, video_offset
);
130 if (st
->avg_frame_rate
.num
)
131 st
->duration
= av_rescale_q(r3d
->video_offsets_count
,
132 av_inv_q(st
->avg_frame_rate
),
134 av_log(s
, AV_LOG_TRACE
, "duration %"PRId64
"\n", st
->duration
);
139 static void r3d_read_reos(AVFormatContext
*s
)
141 R3DContext
*r3d
= s
->priv_data
;
144 r3d
->rdvo_offset
= avio_rb32(s
->pb
);
145 avio_rb32(s
->pb
); // rdvs offset
146 avio_rb32(s
->pb
); // rdao offset
147 avio_rb32(s
->pb
); // rdas offset
149 tmp
= avio_rb32(s
->pb
);
150 av_log(s
, AV_LOG_TRACE
, "num video chunks %d\n", tmp
);
152 tmp
= avio_rb32(s
->pb
);
153 av_log(s
, AV_LOG_TRACE
, "num audio chunks %d\n", tmp
);
155 avio_skip(s
->pb
, 6*4);
158 static int r3d_read_header(AVFormatContext
*s
)
160 R3DContext
*r3d
= s
->priv_data
;
164 if (read_atom(s
, &atom
) < 0) {
165 av_log(s
, AV_LOG_ERROR
, "error reading atom\n");
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");
174 av_log(s
, AV_LOG_ERROR
, "could not find 'red1' atom\n");
178 /* we cannot create the audio stream now because we do not know the
180 if (r3d
->audio_channels
)
181 s
->ctx_flags
|= AVFMTCTX_NOHEADER
;
183 s
->internal
->data_offset
= avio_tell(s
->pb
);
184 av_log(s
, AV_LOG_TRACE
, "data offset %#"PRIx64
"\n", s
->internal
->data_offset
);
185 if (!(s
->pb
->seekable
& AVIO_SEEKABLE_NORMAL
))
187 // find REOB/REOF/REOS to load index
188 avio_seek(s
->pb
, avio_size(s
->pb
)-48-8, SEEK_SET
);
189 if (read_atom(s
, &atom
) < 0)
190 av_log(s
, AV_LOG_ERROR
, "error reading end atom\n");
192 if (atom
.tag
!= MKTAG('R','E','O','B') &&
193 atom
.tag
!= MKTAG('R','E','O','F') &&
194 atom
.tag
!= MKTAG('R','E','O','S'))
199 if (r3d
->rdvo_offset
) {
200 avio_seek(s
->pb
, r3d
->rdvo_offset
, SEEK_SET
);
201 if (read_atom(s
, &atom
) < 0)
202 av_log(s
, AV_LOG_ERROR
, "error reading 'rdvo' atom\n");
203 if (atom
.tag
== MKTAG('R','D','V','O')) {
204 if (r3d_read_rdvo(s
, &atom
) < 0)
205 av_log(s
, AV_LOG_ERROR
, "error parsing 'rdvo' atom\n");
210 avio_seek(s
->pb
, s
->internal
->data_offset
, SEEK_SET
);
214 static int r3d_read_redv(AVFormatContext
*s
, AVPacket
*pkt
, Atom
*atom
)
216 AVStream
*st
= s
->streams
[0];
219 int64_t pos
= avio_tell(s
->pb
);
223 dts
= avio_rb32(s
->pb
);
225 tmp
= avio_rb32(s
->pb
);
226 av_log(s
, AV_LOG_TRACE
, "frame num %d\n", tmp
);
228 tmp
= avio_r8(s
->pb
); // major version
229 tmp2
= avio_r8(s
->pb
); // minor version
230 av_log(s
, AV_LOG_TRACE
, "version %d.%d\n", tmp
, tmp2
);
232 tmp
= avio_rb16(s
->pb
); // unknown
233 av_log(s
, AV_LOG_TRACE
, "unknown %d\n", tmp
);
236 tmp
= avio_rb16(s
->pb
); // unknown
237 av_log(s
, AV_LOG_TRACE
, "unknown %d\n", tmp
);
239 tmp
= avio_rb16(s
->pb
); // unknown
240 av_log(s
, AV_LOG_TRACE
, "unknown %d\n", tmp
);
242 tmp
= avio_rb32(s
->pb
);
243 av_log(s
, AV_LOG_TRACE
, "width %d\n", tmp
);
244 tmp
= avio_rb32(s
->pb
);
245 av_log(s
, AV_LOG_TRACE
, "height %d\n", tmp
);
247 tmp
= avio_rb32(s
->pb
);
248 av_log(s
, AV_LOG_TRACE
, "metadata len %d\n", tmp
);
250 tmp
= atom
->size
- 8 - (avio_tell(s
->pb
) - pos
);
253 ret
= av_get_packet(s
->pb
, pkt
, tmp
);
255 av_log(s
, AV_LOG_ERROR
, "error reading video packet\n");
259 pkt
->stream_index
= 0;
261 if (st
->avg_frame_rate
.num
)
262 pkt
->duration
= (uint64_t)st
->time_base
.den
*
263 st
->avg_frame_rate
.den
/st
->avg_frame_rate
.num
;
264 av_log(s
, AV_LOG_TRACE
, "pkt dts %"PRId64
" duration %"PRId64
"\n", pkt
->dts
, pkt
->duration
);
269 static int r3d_read_reda(AVFormatContext
*s
, AVPacket
*pkt
, Atom
*atom
)
271 R3DContext
*r3d
= s
->priv_data
;
273 int av_unused tmp
, tmp2
;
275 int64_t pos
= avio_tell(s
->pb
);
279 if (s
->nb_streams
< 2) {
280 st
= avformat_new_stream(s
, NULL
);
282 return AVERROR(ENOMEM
);
283 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
284 st
->codecpar
->codec_id
= AV_CODEC_ID_PCM_S32BE
;
285 st
->codecpar
->channels
= r3d
->audio_channels
;
286 avpriv_set_pts_info(st
, 32, 1, s
->streams
[0]->time_base
.den
);
291 dts
= avio_rb32(s
->pb
);
293 st
->codecpar
->sample_rate
= avio_rb32(s
->pb
);
294 if (st
->codecpar
->sample_rate
<= 0) {
295 av_log(s
, AV_LOG_ERROR
, "Bad sample rate\n");
296 return AVERROR_INVALIDDATA
;
299 samples
= avio_rb32(s
->pb
);
301 tmp
= avio_rb32(s
->pb
);
302 av_log(s
, AV_LOG_TRACE
, "packet num %d\n", tmp
);
304 tmp
= avio_rb16(s
->pb
); // unknown
305 av_log(s
, AV_LOG_TRACE
, "unknown %d\n", tmp
);
307 tmp
= avio_r8(s
->pb
); // major version
308 tmp2
= avio_r8(s
->pb
); // minor version
309 av_log(s
, AV_LOG_TRACE
, "version %d.%d\n", tmp
, tmp2
);
311 tmp
= avio_rb32(s
->pb
); // unknown
312 av_log(s
, AV_LOG_TRACE
, "unknown %d\n", tmp
);
314 size
= atom
->size
- 8 - (avio_tell(s
->pb
) - pos
);
317 ret
= av_get_packet(s
->pb
, pkt
, size
);
319 av_log(s
, AV_LOG_ERROR
, "error reading audio packet\n");
323 pkt
->stream_index
= 1;
326 if (st
->codecpar
->sample_rate
&& samples
> 0)
327 pkt
->duration
= av_rescale(samples
, st
->time_base
.den
, st
->codecpar
->sample_rate
);
328 av_log(s
, AV_LOG_TRACE
, "pkt dts %"PRId64
" duration %"PRId64
" samples %d sample rate %d\n",
329 pkt
->dts
, pkt
->duration
, samples
, st
->codecpar
->sample_rate
);
334 static int r3d_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
336 R3DContext
*r3d
= s
->priv_data
;
341 if (read_atom(s
, &atom
) < 0) {
346 case MKTAG('R','E','D','V'):
347 if (s
->streams
[0]->discard
== AVDISCARD_ALL
)
349 if (!(err
= r3d_read_redv(s
, pkt
, &atom
)))
352 case MKTAG('R','E','D','A'):
353 if (!r3d
->audio_channels
)
355 if (s
->nb_streams
>= 2 && s
->streams
[1]->discard
== AVDISCARD_ALL
)
357 if (!(err
= r3d_read_reda(s
, pkt
, &atom
)))
362 avio_skip(s
->pb
, atom
.size
-8);
368 static int r3d_probe(const AVProbeData
*p
)
370 if (AV_RL32(p
->buf
+ 4) == MKTAG('R','E','D','1'))
371 return AVPROBE_SCORE_MAX
;
375 static int r3d_seek(AVFormatContext
*s
, int stream_index
, int64_t sample_time
, int flags
)
377 AVStream
*st
= s
->streams
[0]; // video stream
378 R3DContext
*r3d
= s
->priv_data
;
381 if (!st
->avg_frame_rate
.num
)
384 frame_num
= av_rescale_q(sample_time
, st
->time_base
,
385 av_inv_q(st
->avg_frame_rate
));
386 av_log(s
, AV_LOG_TRACE
, "seek frame num %d timestamp %"PRId64
"\n",
387 frame_num
, sample_time
);
389 if (frame_num
< r3d
->video_offsets_count
) {
390 if (avio_seek(s
->pb
, r3d
->video_offsets_count
, SEEK_SET
) < 0)
393 av_log(s
, AV_LOG_ERROR
, "could not seek to frame %d\n", frame_num
);
400 AVInputFormat ff_r3d_demuxer
= {
402 .long_name
= NULL_IF_CONFIG_SMALL("REDCODE R3D"),
403 .priv_data_size
= sizeof(R3DContext
),
404 .read_probe
= r3d_probe
,
405 .read_header
= r3d_read_header
,
406 .read_packet
= r3d_read_packet
,
407 .read_seek
= r3d_seek
,