FFmpeg
Loading...
Searching...
No Matches
r3d.c
Go to the documentation of this file.
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
23#include "libavutil/dict.h"
25#include "avformat.h"
26#include "demux.h"
27#include "internal.h"
28
29typedef struct R3DContext {
31 unsigned rdvo_offset;
32
35
36typedef struct Atom {
37 unsigned size;
38 uint32_t tag;
39 uint64_t offset;
40} Atom;
41
42static int read_atom(AVFormatContext *s, Atom *atom)
43{
44 atom->offset = avio_tell(s->pb);
45 atom->size = avio_rb32(s->pb);
46 if (atom->size < 8)
47 return -1;
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);
51 return atom->size;
52}
53
55{
57 R3DContext *r3d = s->priv_data;
58 char filename[258];
59 int tmp;
60 int ret;
61 av_unused int tmp2;
63
64 if (!st)
65 return AVERROR(ENOMEM);
68
69 tmp = avio_r8(s->pb); // major version
70 tmp2 = avio_r8(s->pb); // minor version
71 av_log(s, AV_LOG_TRACE, "version %d.%d\n", tmp, tmp2);
72
73 tmp = avio_rb16(s->pb); // unknown
74 av_log(s, AV_LOG_TRACE, "unknown1 %d\n", tmp);
75
76 tmp = avio_rb32(s->pb);
77 avpriv_set_pts_info(st, 32, 1, tmp);
78
79 tmp = avio_rb32(s->pb); // filenum
80 av_log(s, AV_LOG_TRACE, "filenum %d\n", tmp);
81
82 avio_skip(s->pb, 32); // unknown
83
84 st->codecpar->width = avio_rb32(s->pb);
85 st->codecpar->height = avio_rb32(s->pb);
86
87 tmp = avio_rb16(s->pb); // unknown
88 av_log(s, AV_LOG_TRACE, "unknown2 %d\n", tmp);
89
90 framerate.num = avio_rb16(s->pb);
91 framerate.den = avio_rb16(s->pb);
92 if (framerate.num > 0 && framerate.den > 0) {
93#if FF_API_R_FRAME_RATE
94 st->r_frame_rate =
95#endif
97 }
98
99 r3d->audio_channels = avio_r8(s->pb); // audio channels
100 av_log(s, AV_LOG_TRACE, "audio channels %d\n", tmp);
101
102 ret = avio_read(s->pb, filename, 257);
103 if (ret < 257)
104 return ret < 0 ? ret : AVERROR_EOF;
105 filename[sizeof(filename)-1] = 0;
106 av_dict_set(&st->metadata, "filename", filename, 0);
107
108 av_log(s, AV_LOG_TRACE, "filename %s\n", filename);
109 av_log(s, AV_LOG_TRACE, "resolution %dx%d\n", st->codecpar->width, st->codecpar->height);
110 av_log(s, AV_LOG_TRACE, "timescale %d\n", st->time_base.den);
111 av_log(s, AV_LOG_TRACE, "frame rate %d/%d\n",
112 framerate.num, framerate.den);
113
114 return 0;
115}
116
118{
119 R3DContext *r3d = s->priv_data;
120 AVStream *st = s->streams[0];
121 int i;
122
123 r3d->video_offsets_count = (atom->size - 8) / 4;
124
125 for (i = 0; i < r3d->video_offsets_count; i++) {
126 unsigned video_offset = avio_rb32(s->pb);
127 if (!video_offset) {
128 r3d->video_offsets_count = i;
129 break;
130 }
131 av_log(s, AV_LOG_TRACE, "video offset %d: %#x\n", i, video_offset);
132 if (st->avg_frame_rate.num) {
133 int ret = av_add_index_entry(st, video_offset,
135 0, 0, AVINDEX_KEYFRAME);
136 if (ret < 0)
137 return ret;
138 }
139 }
140
141 if (st->avg_frame_rate.num)
144 st->time_base);
145 av_log(s, AV_LOG_TRACE, "duration %"PRId64"\n", st->duration);
146
147 return 0;
148}
149
151{
152 R3DContext *r3d = s->priv_data;
153 av_unused int tmp;
154
155 r3d->rdvo_offset = avio_rb32(s->pb);
156 avio_rb32(s->pb); // rdvs offset
157 avio_rb32(s->pb); // rdao offset
158 avio_rb32(s->pb); // rdas offset
159
160 tmp = avio_rb32(s->pb);
161 av_log(s, AV_LOG_TRACE, "num video chunks %d\n", tmp);
162
163 tmp = avio_rb32(s->pb);
164 av_log(s, AV_LOG_TRACE, "num audio chunks %d\n", tmp);
165
166 avio_skip(s->pb, 6*4);
167}
168
170{
171 FFFormatContext *const si = ffformatcontext(s);
172 R3DContext *r3d = s->priv_data;
173 Atom atom;
174 int ret;
175
176 if (read_atom(s, &atom) < 0) {
177 av_log(s, AV_LOG_ERROR, "error reading atom\n");
178 return -1;
179 }
180 if (atom.tag == MKTAG('R','E','D','1')) {
181 if ((ret = r3d_read_red1(s)) < 0) {
182 av_log(s, AV_LOG_ERROR, "error parsing 'red1' atom\n");
183 return ret;
184 }
185 } else {
186 av_log(s, AV_LOG_ERROR, "could not find 'red1' atom\n");
187 return -1;
188 }
189
190 /* we cannot create the audio stream now because we do not know the
191 * sample rate */
192 if (r3d->audio_channels)
193 s->ctx_flags |= AVFMTCTX_NOHEADER;
194
195 si->data_offset = avio_tell(s->pb);
196 av_log(s, AV_LOG_TRACE, "data offset %#"PRIx64"\n", si->data_offset);
197 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
198 return 0;
199 // find REOB/REOF/REOS to load index
200 avio_seek(s->pb, avio_size(s->pb)-48-8, SEEK_SET);
201 if (read_atom(s, &atom) < 0)
202 av_log(s, AV_LOG_ERROR, "error reading end atom\n");
203
204 if (atom.tag != MKTAG('R','E','O','B') &&
205 atom.tag != MKTAG('R','E','O','F') &&
206 atom.tag != MKTAG('R','E','O','S'))
207 goto out;
208
210
211 if (r3d->rdvo_offset) {
212 avio_seek(s->pb, r3d->rdvo_offset, SEEK_SET);
213 if (read_atom(s, &atom) < 0)
214 av_log(s, AV_LOG_ERROR, "error reading 'rdvo' atom\n");
215 if (atom.tag == MKTAG('R','D','V','O')) {
216 if ((ret = r3d_read_rdvo(s, &atom)) < 0) {
217 av_log(s, AV_LOG_ERROR, "error parsing 'rdvo' atom\n");
218 return ret;
219 }
220 }
221 }
222
223 out:
224 avio_seek(s->pb, si->data_offset, SEEK_SET);
225 return 0;
226}
227
229{
230 AVStream *st = s->streams[0];
231 int tmp;
232 av_unused int tmp2;
233 int64_t pos = avio_tell(s->pb);
234 unsigned dts;
235 int ret;
236
237 dts = avio_rb32(s->pb);
238
239 tmp = avio_rb32(s->pb);
240 av_log(s, AV_LOG_TRACE, "frame num %d\n", tmp);
241
242 tmp = avio_r8(s->pb); // major version
243 tmp2 = avio_r8(s->pb); // minor version
244 av_log(s, AV_LOG_TRACE, "version %d.%d\n", tmp, tmp2);
245
246 tmp = avio_rb16(s->pb); // unknown
247 av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
248
249 if (tmp > 4) {
250 tmp = avio_rb16(s->pb); // unknown
251 av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
252
253 tmp = avio_rb16(s->pb); // unknown
254 av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
255
256 tmp = avio_rb32(s->pb);
257 av_log(s, AV_LOG_TRACE, "width %d\n", tmp);
258 tmp = avio_rb32(s->pb);
259 av_log(s, AV_LOG_TRACE, "height %d\n", tmp);
260
261 tmp = avio_rb32(s->pb);
262 av_log(s, AV_LOG_TRACE, "metadata len %d\n", tmp);
263 }
264 tmp = atom->size - 8 - (avio_tell(s->pb) - pos);
265 if (tmp < 0)
266 return -1;
267 ret = av_get_packet(s->pb, pkt, tmp);
268 if (ret < 0) {
269 av_log(s, AV_LOG_ERROR, "error reading video packet\n");
270 return -1;
271 }
272
273 pkt->stream_index = 0;
274 pkt->dts = dts;
275 if (st->avg_frame_rate.num)
276 pkt->duration = (uint64_t)st->time_base.den*
278 av_log(s, AV_LOG_TRACE, "pkt dts %"PRId64" duration %"PRId64"\n", pkt->dts, pkt->duration);
279
280 return 0;
281}
282
284{
285 R3DContext *r3d = s->priv_data;
286 AVStream *st;
287 av_unused int tmp, tmp2;
288 int samples, size;
289 int64_t pos = avio_tell(s->pb);
290 unsigned dts;
291 int ret;
292
293 if (s->nb_streams < 2) {
295 if (!st)
296 return AVERROR(ENOMEM);
300 avpriv_set_pts_info(st, 32, 1, s->streams[0]->time_base.den);
301 } else {
302 st = s->streams[1];
303 }
304
305 dts = avio_rb32(s->pb);
306
307 st->codecpar->sample_rate = avio_rb32(s->pb);
308 if (st->codecpar->sample_rate <= 0) {
309 av_log(s, AV_LOG_ERROR, "Bad sample rate\n");
310 return AVERROR_INVALIDDATA;
311 }
312
313 samples = avio_rb32(s->pb);
314
315 tmp = avio_rb32(s->pb);
316 av_log(s, AV_LOG_TRACE, "packet num %d\n", tmp);
317
318 tmp = avio_rb16(s->pb); // unknown
319 av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
320
321 tmp = avio_r8(s->pb); // major version
322 tmp2 = avio_r8(s->pb); // minor version
323 av_log(s, AV_LOG_TRACE, "version %d.%d\n", tmp, tmp2);
324
325 tmp = avio_rb32(s->pb); // unknown
326 av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
327
328 size = atom->size - 8 - (avio_tell(s->pb) - pos);
329 if (size < 0)
330 return -1;
331 ret = av_get_packet(s->pb, pkt, size);
332 if (ret < 0) {
333 av_log(s, AV_LOG_ERROR, "error reading audio packet\n");
334 return ret;
335 }
336
337 pkt->stream_index = 1;
338 pkt->dts = dts;
339
340 if (st->codecpar->sample_rate && samples > 0)
341 pkt->duration = av_rescale(samples, st->time_base.den, st->codecpar->sample_rate);
342 av_log(s, AV_LOG_TRACE, "pkt dts %"PRId64" duration %"PRId64" samples %d sample rate %d\n",
343 pkt->dts, pkt->duration, samples, st->codecpar->sample_rate);
344
345 return 0;
346}
347
349{
350 R3DContext *r3d = s->priv_data;
351 Atom atom;
352 int err = 0;
353
354 while (!err) {
355 if (read_atom(s, &atom) < 0) {
356 err = -1;
357 break;
358 }
359 switch (atom.tag) {
360 case MKTAG('R','E','D','V'):
361 if (s->streams[0]->discard == AVDISCARD_ALL)
362 goto skip;
363 if (!(err = r3d_read_redv(s, pkt, &atom)))
364 return 0;
365 break;
366 case MKTAG('R','E','D','A'):
367 if (!r3d->audio_channels)
368 return -1;
369 if (s->nb_streams >= 2 && s->streams[1]->discard == AVDISCARD_ALL)
370 goto skip;
371 if (!(err = r3d_read_reda(s, pkt, &atom)))
372 return 0;
373 break;
374 default:
375 skip:
376 avio_skip(s->pb, atom.size-8);
377 }
378 }
379 return err;
380}
381
382static int r3d_probe(const AVProbeData *p)
383{
384 if (AV_RL32(p->buf + 4) == MKTAG('R','E','D','1'))
385 return AVPROBE_SCORE_MAX;
386 return 0;
387}
388
390 .p.name = "r3d",
391 .p.long_name = NULL_IF_CONFIG_SMALL("REDCODE R3D"),
392 .priv_data_size = sizeof(R3DContext),
396};
const FFInputFormat ff_r3d_demuxer
Definition r3d.c:389
static FILE * out
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:865
Main libavformat public API header.
#define AVINDEX_KEYFRAME
Definition avformat.h:630
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:485
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition avformat.h:1352
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
unsigned int avio_rb16(AVIOContext *s)
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:507
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:620
unsigned int avio_rl32(AVIOContext *s)
unsigned int avio_rb32(AVIOContext *s)
int avio_r8(AVIOContext *s)
Definition aviobuf.c:611
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
Public dictionary API.
int ret
Definition ffplay.c:391
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:580
@ AV_CODEC_ID_JPEG2000
Definition codec_id.h:138
@ AV_CODEC_ID_PCM_S32BE
Definition codec_id.h:342
@ AVDISCARD_ALL
discard all
Definition defs.h:248
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition seek.c:122
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:87
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition rational.h:159
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_RL32(p)
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition internal.h:135
#define av_unused
Definition attributes.h:164
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
#define MKTAG(a, b, c, d)
Definition macros.h:55
static int r3d_read_red1(AVFormatContext *s)
Definition r3d.c:54
static int r3d_read_rdvo(AVFormatContext *s, Atom *atom)
Definition r3d.c:117
static int r3d_read_redv(AVFormatContext *s, AVPacket *pkt, Atom *atom)
Definition r3d.c:228
static int read_atom(AVFormatContext *s, Atom *atom)
Definition r3d.c:42
static int r3d_read_reda(AVFormatContext *s, AVPacket *pkt, Atom *atom)
Definition r3d.c:283
static void r3d_read_reos(AVFormatContext *s)
Definition r3d.c:150
static int r3d_read_header(AVFormatContext *s)
Definition r3d.c:169
static int r3d_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition r3d.c:348
static int r3d_probe(const AVProbeData *p)
Definition r3d.c:382
unsigned int pos
Definition spdifenc.c:431
int nb_channels
Number of channels in this layout.
int height
The height of the video frame in pixels.
Definition codec_par.h:150
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1401
This structure stores compressed data.
Definition packet.h:587
This structure contains the data a format has to probe a file.
Definition avformat.h:473
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:827
AVDictionary * metadata
Definition avformat.h:848
AVRational avg_frame_rate
Average framerate.
Definition avformat.h:857
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:807
AVRational r_frame_rate
Real base framerate of the stream.
Definition avformat.h:902
Definition r3d.c:36
unsigned size
Definition r3d.c:37
uint64_t offset
Definition r3d.c:39
uint32_t tag
Definition r3d.c:38
int64_t data_offset
offset of the first packet
Definition internal.h:89
unsigned video_offsets_count
Definition r3d.c:30
int audio_channels
Definition r3d.c:33
unsigned rdvo_offset
Definition r3d.c:31
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
float framerate
Definition av1_levels.c:29
int size