Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/mathematics.h"
25 #include "avformat.h"
26 #include "internal.h"
27 
28 typedef struct R3DContext {
30  unsigned *video_offsets;
31  unsigned rdvo_offset;
32 
34 } R3DContext;
35 
36 typedef struct Atom {
37  unsigned size;
38  uint32_t tag;
39  uint64_t offset;
40 } Atom;
41 
42 static 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 av_unused tmp2;
61  AVRational framerate;
62 
63  if (!st)
64  return AVERROR(ENOMEM);
67 
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);
71 
72  tmp = avio_rb16(s->pb); // unknown
73  av_log(s, AV_LOG_TRACE, "unknown1 %d\n", tmp);
74 
75  tmp = avio_rb32(s->pb);
76  avpriv_set_pts_info(st, 32, 1, tmp);
77 
78  tmp = avio_rb32(s->pb); // filenum
79  av_log(s, AV_LOG_TRACE, "filenum %d\n", tmp);
80 
81  avio_skip(s->pb, 32); // unknown
82 
83  st->codecpar->width = avio_rb32(s->pb);
84  st->codecpar->height = avio_rb32(s->pb);
85 
86  tmp = avio_rb16(s->pb); // unknown
87  av_log(s, AV_LOG_TRACE, "unknown2 %d\n", tmp);
88 
89  framerate.num = avio_rb16(s->pb);
90  framerate.den = avio_rb16(s->pb);
91  if (framerate.num > 0 && framerate.den > 0) {
92  st->avg_frame_rate = framerate;
93  }
94 
95  r3d->audio_channels = avio_r8(s->pb); // audio channels
96  av_log(s, AV_LOG_TRACE, "audio channels %d\n", tmp);
97 
98  avio_read(s->pb, filename, 257);
99  filename[sizeof(filename)-1] = 0;
100  av_dict_set(&st->metadata, "filename", filename, 0);
101 
102  av_log(s, AV_LOG_TRACE, "filename %s\n", filename);
103  av_log(s, AV_LOG_TRACE, "resolution %dx%d\n", st->codecpar->width, st->codecpar->height);
104  av_log(s, AV_LOG_TRACE, "timescale %d\n", st->time_base.den);
105  av_log(s, AV_LOG_TRACE, "frame rate %d/%d\n",
106  framerate.num, framerate.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] = avio_rb32(s->pb);
124  if (!r3d->video_offsets[i]) {
125  r3d->video_offsets_count = i;
126  break;
127  }
128  av_log(s, AV_LOG_TRACE, "video offset %d: %#x\n", i, r3d->video_offsets[i]);
129  }
130 
131  if (st->avg_frame_rate.num)
133  (AVRational){st->avg_frame_rate.den,
134  st->avg_frame_rate.num},
135  st->time_base);
136  av_log(s, AV_LOG_TRACE, "duration %"PRId64"\n", st->duration);
137 
138  return 0;
139 }
140 
142 {
143  R3DContext *r3d = s->priv_data;
144  int av_unused tmp;
145 
146  r3d->rdvo_offset = avio_rb32(s->pb);
147  avio_rb32(s->pb); // rdvs offset
148  avio_rb32(s->pb); // rdao offset
149  avio_rb32(s->pb); // rdas offset
150 
151  tmp = avio_rb32(s->pb);
152  av_log(s, AV_LOG_TRACE, "num video chunks %d\n", tmp);
153 
154  tmp = avio_rb32(s->pb);
155  av_log(s, AV_LOG_TRACE, "num audio chunks %d\n", tmp);
156 
157  avio_skip(s->pb, 6*4);
158 }
159 
161 {
162  R3DContext *r3d = s->priv_data;
163  Atom atom;
164  int ret;
165 
166  if (read_atom(s, &atom) < 0) {
167  av_log(s, AV_LOG_ERROR, "error reading atom\n");
168  return -1;
169  }
170  if (atom.tag == MKTAG('R','E','D','1')) {
171  if ((ret = r3d_read_red1(s)) < 0) {
172  av_log(s, AV_LOG_ERROR, "error parsing 'red1' atom\n");
173  return ret;
174  }
175  } else {
176  av_log(s, AV_LOG_ERROR, "could not find 'red1' atom\n");
177  return -1;
178  }
179 
180  /* we cannot create the audio stream now because we do not know the
181  * sample rate */
182  if (r3d->audio_channels)
184 
185  s->internal->data_offset = avio_tell(s->pb);
186  av_log(s, AV_LOG_TRACE, "data offset %#"PRIx64"\n", s->internal->data_offset);
187  if (!s->pb->seekable)
188  return 0;
189  // find REOB/REOF/REOS to load index
190  avio_seek(s->pb, avio_size(s->pb)-48-8, SEEK_SET);
191  if (read_atom(s, &atom) < 0)
192  av_log(s, AV_LOG_ERROR, "error reading end atom\n");
193 
194  if (atom.tag != MKTAG('R','E','O','B') &&
195  atom.tag != MKTAG('R','E','O','F') &&
196  atom.tag != MKTAG('R','E','O','S'))
197  goto out;
198 
199  r3d_read_reos(s);
200 
201  if (r3d->rdvo_offset) {
202  avio_seek(s->pb, r3d->rdvo_offset, SEEK_SET);
203  if (read_atom(s, &atom) < 0)
204  av_log(s, AV_LOG_ERROR, "error reading 'rdvo' atom\n");
205  if (atom.tag == MKTAG('R','D','V','O')) {
206  if (r3d_read_rdvo(s, &atom) < 0)
207  av_log(s, AV_LOG_ERROR, "error parsing 'rdvo' atom\n");
208  }
209  }
210 
211  out:
212  avio_seek(s->pb, s->internal->data_offset, SEEK_SET);
213  return 0;
214 }
215 
216 static int r3d_read_redv(AVFormatContext *s, AVPacket *pkt, Atom *atom)
217 {
218  AVStream *st = s->streams[0];
219  int tmp;
220  int av_unused tmp2;
221  int64_t pos = avio_tell(s->pb);
222  unsigned dts;
223  int ret;
224 
225  dts = avio_rb32(s->pb);
226 
227  tmp = avio_rb32(s->pb);
228  av_log(s, AV_LOG_TRACE, "frame num %d\n", tmp);
229 
230  tmp = avio_r8(s->pb); // major version
231  tmp2 = avio_r8(s->pb); // minor version
232  av_log(s, AV_LOG_TRACE, "version %d.%d\n", tmp, tmp2);
233 
234  tmp = avio_rb16(s->pb); // unknown
235  av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
236 
237  if (tmp > 4) {
238  tmp = avio_rb16(s->pb); // unknown
239  av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
240 
241  tmp = avio_rb16(s->pb); // unknown
242  av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
243 
244  tmp = avio_rb32(s->pb);
245  av_log(s, AV_LOG_TRACE, "width %d\n", tmp);
246  tmp = avio_rb32(s->pb);
247  av_log(s, AV_LOG_TRACE, "height %d\n", tmp);
248 
249  tmp = avio_rb32(s->pb);
250  av_log(s, AV_LOG_TRACE, "metadata len %d\n", tmp);
251  }
252  tmp = atom->size - 8 - (avio_tell(s->pb) - pos);
253  if (tmp < 0)
254  return -1;
255  ret = av_get_packet(s->pb, pkt, tmp);
256  if (ret < 0) {
257  av_log(s, AV_LOG_ERROR, "error reading video packet\n");
258  return -1;
259  }
260 
261  pkt->stream_index = 0;
262  pkt->dts = dts;
263  if (st->avg_frame_rate.num)
264  pkt->duration = (uint64_t)st->time_base.den*
266  av_log(s, AV_LOG_TRACE, "pkt dts %"PRId64" duration %"PRId64"\n", pkt->dts, pkt->duration);
267 
268  return 0;
269 }
270 
271 static int r3d_read_reda(AVFormatContext *s, AVPacket *pkt, Atom *atom)
272 {
273  R3DContext *r3d = s->priv_data;
274  AVStream *st;
275  int av_unused tmp, tmp2;
276  int samples, size;
277  int64_t pos = avio_tell(s->pb);
278  unsigned dts;
279  int ret;
280 
281  if (s->nb_streams < 2) {
282  st = avformat_new_stream(s, NULL);
283  if (!st)
284  return AVERROR(ENOMEM);
287  st->codecpar->channels = r3d->audio_channels;
288  avpriv_set_pts_info(st, 32, 1, s->streams[0]->time_base.den);
289  } else {
290  st = s->streams[1];
291  }
292 
293  dts = avio_rb32(s->pb);
294 
295  st->codecpar->sample_rate = avio_rb32(s->pb);
296  if (st->codecpar->sample_rate <= 0) {
297  av_log(s, AV_LOG_ERROR, "Bad sample rate\n");
298  return AVERROR_INVALIDDATA;
299  }
300 
301  samples = avio_rb32(s->pb);
302 
303  tmp = avio_rb32(s->pb);
304  av_log(s, AV_LOG_TRACE, "packet num %d\n", tmp);
305 
306  tmp = avio_rb16(s->pb); // unknown
307  av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
308 
309  tmp = avio_r8(s->pb); // major version
310  tmp2 = avio_r8(s->pb); // minor version
311  av_log(s, AV_LOG_TRACE, "version %d.%d\n", tmp, tmp2);
312 
313  tmp = avio_rb32(s->pb); // unknown
314  av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
315 
316  size = atom->size - 8 - (avio_tell(s->pb) - pos);
317  if (size < 0)
318  return -1;
319  ret = av_get_packet(s->pb, pkt, size);
320  if (ret < 0) {
321  av_log(s, AV_LOG_ERROR, "error reading audio packet\n");
322  return ret;
323  }
324 
325  pkt->stream_index = 1;
326  pkt->dts = dts;
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);
330 
331  return 0;
332 }
333 
335 {
336  R3DContext *r3d = s->priv_data;
337  Atom atom;
338  int err = 0;
339 
340  while (!err) {
341  if (read_atom(s, &atom) < 0) {
342  err = -1;
343  break;
344  }
345  switch (atom.tag) {
346  case MKTAG('R','E','D','V'):
347  if (s->streams[0]->discard == AVDISCARD_ALL)
348  goto skip;
349  if (!(err = r3d_read_redv(s, pkt, &atom)))
350  return 0;
351  break;
352  case MKTAG('R','E','D','A'):
353  if (!r3d->audio_channels)
354  return -1;
355  if (s->nb_streams >= 2 && s->streams[1]->discard == AVDISCARD_ALL)
356  goto skip;
357  if (!(err = r3d_read_reda(s, pkt, &atom)))
358  return 0;
359  break;
360  default:
361  skip:
362  avio_skip(s->pb, atom.size-8);
363  }
364  }
365  return err;
366 }
367 
368 static int r3d_probe(AVProbeData *p)
369 {
370  if (AV_RL32(p->buf + 4) == MKTAG('R','E','D','1'))
371  return AVPROBE_SCORE_MAX;
372  return 0;
373 }
374 
375 static int r3d_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
376 {
377  AVStream *st = s->streams[0]; // video stream
378  R3DContext *r3d = s->priv_data;
379  int frame_num;
380 
381  if (!st->avg_frame_rate.num)
382  return -1;
383 
384  frame_num = av_rescale_q(sample_time, st->time_base,
385  (AVRational){st->avg_frame_rate.den, st->avg_frame_rate.num});
386  av_log(s, AV_LOG_TRACE, "seek frame num %d timestamp %"PRId64"\n",
387  frame_num, sample_time);
388 
389  if (frame_num < r3d->video_offsets_count) {
390  if (avio_seek(s->pb, r3d->video_offsets_count, SEEK_SET) < 0)
391  return -1;
392  } else {
393  av_log(s, AV_LOG_ERROR, "could not seek to frame %d\n", frame_num);
394  return -1;
395  }
396 
397  return 0;
398 }
399 
401 {
402  R3DContext *r3d = s->priv_data;
403 
404  av_freep(&r3d->video_offsets);
405 
406  return 0;
407 }
408 
410  .name = "r3d",
411  .long_name = NULL_IF_CONFIG_SMALL("REDCODE R3D"),
412  .priv_data_size = sizeof(R3DContext),
417  .read_seek = r3d_seek,
418 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
uint64_t offset
Definition: r3d.c:39
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:297
int size
uint32_t tag
Definition: r3d.c:38
unsigned video_offsets_count
Definition: r3d.c:29
static int read_atom(AVFormatContext *s, Atom *atom)
Definition: r3d.c:42
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:2986
int64_t data_offset
offset of the first packet
Definition: internal.h:62
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
static int r3d_close(AVFormatContext *s)
Definition: r3d.c:400
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
int num
numerator
Definition: rational.h:44
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:242
unsigned size
Definition: r3d.c:37
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1243
discard all
Definition: avcodec.h:689
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:681
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:989
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
Format I/O context.
Definition: avformat.h:940
Public dictionary API.
int width
Video only.
Definition: avcodec.h:3525
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:919
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:150
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:696
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1364
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
static int r3d_read_reda(AVFormatContext *s, AVPacket *pkt, Atom *atom)
Definition: r3d.c:271
static int flags
Definition: log.c:50
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
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:117
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:295
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:99
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:665
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
static void r3d_read_reos(AVFormatContext *s)
Definition: r3d.c:141
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
Definition: r3d.c:36
int audio_channels
Definition: r3d.c:33
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:781
Definition: r3d.c:28
unsigned rdvo_offset
Definition: r3d.c:31
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:536
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:400
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:996
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:153
AVInputFormat ff_r3d_demuxer
Definition: r3d.c:409
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:86
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:286
#define AV_RL32
Definition: intreadwrite.h:146
AVDictionary * metadata
Definition: avformat.h:772
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:546
Stream structure.
Definition: avformat.h:705
NULL
Definition: eval.c:55
AVIOContext * pb
I/O context.
Definition: avformat.h:982
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
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:68
static int r3d_probe(AVProbeData *p)
Definition: r3d.c:368
rational number numerator/denominator
Definition: rational.h:43
static int r3d_read_rdvo(AVFormatContext *s, Atom *atom)
Definition: r3d.c:111
This structure contains the data a format has to probe a file.
Definition: avformat.h:398
unsigned * video_offsets
Definition: r3d.c:30
static int r3d_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
Definition: r3d.c:375
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:757
int sample_rate
Audio only.
Definition: avcodec.h:3564
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:407
Main libavformat public API header.
static int r3d_read_header(AVFormatContext *s)
Definition: r3d.c:160
static int r3d_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: r3d.c:334
int den
denominator
Definition: rational.h:45
static uint8_t tmp[8]
Definition: des.c:38
void * priv_data
Format private data.
Definition: avformat.h:968
static int r3d_read_redv(AVFormatContext *s, AVPacket *pkt, Atom *atom)
Definition: r3d.c:216
int channels
Audio only.
Definition: avcodec.h:3560
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1345
FILE * out
Definition: movenc.c:54
static int r3d_read_red1(AVFormatContext *s)
Definition: r3d.c:54
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:529
AVCodecParameters * codecpar
Definition: avformat.h:831
int stream_index
Definition: avcodec.h:1348
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:742
#define MKTAG(a, b, c, d)
Definition: common.h:256
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:763
This structure stores compressed data.
Definition: avcodec.h:1323
#define av_unused
Definition: attributes.h:86