FFmpeg  4.4.9
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 
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 rdvo_offset;
31 
33 } R3DContext;
34 
35 typedef struct Atom {
36  unsigned size;
37  uint32_t tag;
38  uint64_t offset;
39 } Atom;
40 
41 static int read_atom(AVFormatContext *s, Atom *atom)
42 {
43  atom->offset = avio_tell(s->pb);
44  atom->size = avio_rb32(s->pb);
45  if (atom->size < 8)
46  return -1;
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);
50  return atom->size;
51 }
52 
54 {
56  R3DContext *r3d = s->priv_data;
57  char filename[258];
58  int tmp;
59  int ret;
60  int av_unused tmp2;
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 #if FF_API_R_FRAME_RATE
93  st->r_frame_rate =
94 #endif
96  }
97 
98  r3d->audio_channels = avio_r8(s->pb); // audio channels
99  av_log(s, AV_LOG_TRACE, "audio channels %d\n", tmp);
100 
101  ret = avio_read(s->pb, filename, 257);
102  if (ret < 257)
103  return ret < 0 ? ret : AVERROR_EOF;
104  filename[sizeof(filename)-1] = 0;
105  av_dict_set(&st->metadata, "filename", filename, 0);
106 
107  av_log(s, AV_LOG_TRACE, "filename %s\n", filename);
108  av_log(s, AV_LOG_TRACE, "resolution %dx%d\n", st->codecpar->width, st->codecpar->height);
109  av_log(s, AV_LOG_TRACE, "timescale %d\n", st->time_base.den);
110  av_log(s, AV_LOG_TRACE, "frame rate %d/%d\n",
111  framerate.num, framerate.den);
112 
113  return 0;
114 }
115 
116 static int r3d_read_rdvo(AVFormatContext *s, Atom *atom)
117 {
118  R3DContext *r3d = s->priv_data;
119  AVStream *st = s->streams[0];
120  int i;
121 
122  r3d->video_offsets_count = (atom->size - 8) / 4;
123 
124  for (i = 0; i < r3d->video_offsets_count; i++) {
125  unsigned video_offset = avio_rb32(s->pb);
126  if (!video_offset) {
127  r3d->video_offsets_count = i;
128  break;
129  }
130  av_log(s, AV_LOG_TRACE, "video offset %d: %#x\n", i, video_offset);
131  if (st->avg_frame_rate.num) {
132  int ret = av_add_index_entry(st, video_offset,
134  0, 0, AVINDEX_KEYFRAME);
135  if (ret < 0)
136  return ret;
137  }
138  }
139 
140  if (st->avg_frame_rate.num)
143  st->time_base);
144  av_log(s, AV_LOG_TRACE, "duration %"PRId64"\n", st->duration);
145 
146  return 0;
147 }
148 
150 {
151  R3DContext *r3d = s->priv_data;
152  int av_unused tmp;
153 
154  r3d->rdvo_offset = avio_rb32(s->pb);
155  avio_rb32(s->pb); // rdvs offset
156  avio_rb32(s->pb); // rdao offset
157  avio_rb32(s->pb); // rdas offset
158 
159  tmp = avio_rb32(s->pb);
160  av_log(s, AV_LOG_TRACE, "num video chunks %d\n", tmp);
161 
162  tmp = avio_rb32(s->pb);
163  av_log(s, AV_LOG_TRACE, "num audio chunks %d\n", tmp);
164 
165  avio_skip(s->pb, 6*4);
166 }
167 
169 {
170  R3DContext *r3d = s->priv_data;
171  Atom atom;
172  int ret;
173 
174  if (read_atom(s, &atom) < 0) {
175  av_log(s, AV_LOG_ERROR, "error reading atom\n");
176  return -1;
177  }
178  if (atom.tag == MKTAG('R','E','D','1')) {
179  if ((ret = r3d_read_red1(s)) < 0) {
180  av_log(s, AV_LOG_ERROR, "error parsing 'red1' atom\n");
181  return ret;
182  }
183  } else {
184  av_log(s, AV_LOG_ERROR, "could not find 'red1' atom\n");
185  return -1;
186  }
187 
188  /* we cannot create the audio stream now because we do not know the
189  * sample rate */
190  if (r3d->audio_channels)
191  s->ctx_flags |= AVFMTCTX_NOHEADER;
192 
193  s->internal->data_offset = avio_tell(s->pb);
194  av_log(s, AV_LOG_TRACE, "data offset %#"PRIx64"\n", s->internal->data_offset);
195  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
196  return 0;
197  // find REOB/REOF/REOS to load index
198  avio_seek(s->pb, avio_size(s->pb)-48-8, SEEK_SET);
199  if (read_atom(s, &atom) < 0)
200  av_log(s, AV_LOG_ERROR, "error reading end atom\n");
201 
202  if (atom.tag != MKTAG('R','E','O','B') &&
203  atom.tag != MKTAG('R','E','O','F') &&
204  atom.tag != MKTAG('R','E','O','S'))
205  goto out;
206 
207  r3d_read_reos(s);
208 
209  if (r3d->rdvo_offset) {
210  avio_seek(s->pb, r3d->rdvo_offset, SEEK_SET);
211  if (read_atom(s, &atom) < 0)
212  av_log(s, AV_LOG_ERROR, "error reading 'rdvo' atom\n");
213  if (atom.tag == MKTAG('R','D','V','O')) {
214  if ((ret = r3d_read_rdvo(s, &atom)) < 0) {
215  av_log(s, AV_LOG_ERROR, "error parsing 'rdvo' atom\n");
216  return ret;
217  }
218  }
219  }
220 
221  out:
222  avio_seek(s->pb, s->internal->data_offset, SEEK_SET);
223  return 0;
224 }
225 
227 {
228  AVStream *st = s->streams[0];
229  int tmp;
230  int av_unused tmp2;
231  int64_t pos = avio_tell(s->pb);
232  unsigned dts;
233  int ret;
234 
235  dts = avio_rb32(s->pb);
236 
237  tmp = avio_rb32(s->pb);
238  av_log(s, AV_LOG_TRACE, "frame num %d\n", tmp);
239 
240  tmp = avio_r8(s->pb); // major version
241  tmp2 = avio_r8(s->pb); // minor version
242  av_log(s, AV_LOG_TRACE, "version %d.%d\n", tmp, tmp2);
243 
244  tmp = avio_rb16(s->pb); // unknown
245  av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
246 
247  if (tmp > 4) {
248  tmp = avio_rb16(s->pb); // unknown
249  av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
250 
251  tmp = avio_rb16(s->pb); // unknown
252  av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
253 
254  tmp = avio_rb32(s->pb);
255  av_log(s, AV_LOG_TRACE, "width %d\n", tmp);
256  tmp = avio_rb32(s->pb);
257  av_log(s, AV_LOG_TRACE, "height %d\n", tmp);
258 
259  tmp = avio_rb32(s->pb);
260  av_log(s, AV_LOG_TRACE, "metadata len %d\n", tmp);
261  }
262  tmp = atom->size - 8 - (avio_tell(s->pb) - pos);
263  if (tmp < 0)
264  return -1;
265  ret = av_get_packet(s->pb, pkt, tmp);
266  if (ret < 0) {
267  av_log(s, AV_LOG_ERROR, "error reading video packet\n");
268  return -1;
269  }
270 
271  pkt->stream_index = 0;
272  pkt->dts = dts;
273  if (st->avg_frame_rate.num)
274  pkt->duration = (uint64_t)st->time_base.den*
276  av_log(s, AV_LOG_TRACE, "pkt dts %"PRId64" duration %"PRId64"\n", pkt->dts, pkt->duration);
277 
278  return 0;
279 }
280 
282 {
283  R3DContext *r3d = s->priv_data;
284  AVStream *st;
285  int av_unused tmp, tmp2;
286  int samples, size;
287  int64_t pos = avio_tell(s->pb);
288  unsigned dts;
289  int ret;
290 
291  if (s->nb_streams < 2) {
292  st = avformat_new_stream(s, NULL);
293  if (!st)
294  return AVERROR(ENOMEM);
297  st->codecpar->channels = r3d->audio_channels;
298  avpriv_set_pts_info(st, 32, 1, s->streams[0]->time_base.den);
299  } else {
300  st = s->streams[1];
301  }
302 
303  dts = avio_rb32(s->pb);
304 
305  st->codecpar->sample_rate = avio_rb32(s->pb);
306  if (st->codecpar->sample_rate <= 0) {
307  av_log(s, AV_LOG_ERROR, "Bad sample rate\n");
308  return AVERROR_INVALIDDATA;
309  }
310 
311  samples = avio_rb32(s->pb);
312 
313  tmp = avio_rb32(s->pb);
314  av_log(s, AV_LOG_TRACE, "packet num %d\n", tmp);
315 
316  tmp = avio_rb16(s->pb); // unknown
317  av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
318 
319  tmp = avio_r8(s->pb); // major version
320  tmp2 = avio_r8(s->pb); // minor version
321  av_log(s, AV_LOG_TRACE, "version %d.%d\n", tmp, tmp2);
322 
323  tmp = avio_rb32(s->pb); // unknown
324  av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
325 
326  size = atom->size - 8 - (avio_tell(s->pb) - pos);
327  if (size < 0)
328  return -1;
329  ret = av_get_packet(s->pb, pkt, size);
330  if (ret < 0) {
331  av_log(s, AV_LOG_ERROR, "error reading audio packet\n");
332  return ret;
333  }
334 
335  pkt->stream_index = 1;
336  pkt->dts = dts;
337 
338  if (st->codecpar->sample_rate && samples > 0)
339  pkt->duration = av_rescale(samples, st->time_base.den, st->codecpar->sample_rate);
340  av_log(s, AV_LOG_TRACE, "pkt dts %"PRId64" duration %"PRId64" samples %d sample rate %d\n",
341  pkt->dts, pkt->duration, samples, st->codecpar->sample_rate);
342 
343  return 0;
344 }
345 
347 {
348  R3DContext *r3d = s->priv_data;
349  Atom atom;
350  int err = 0;
351 
352  while (!err) {
353  if (read_atom(s, &atom) < 0) {
354  err = -1;
355  break;
356  }
357  switch (atom.tag) {
358  case MKTAG('R','E','D','V'):
359  if (s->streams[0]->discard == AVDISCARD_ALL)
360  goto skip;
361  if (!(err = r3d_read_redv(s, pkt, &atom)))
362  return 0;
363  break;
364  case MKTAG('R','E','D','A'):
365  if (!r3d->audio_channels)
366  return -1;
367  if (s->nb_streams >= 2 && s->streams[1]->discard == AVDISCARD_ALL)
368  goto skip;
369  if (!(err = r3d_read_reda(s, pkt, &atom)))
370  return 0;
371  break;
372  default:
373  skip:
374  avio_skip(s->pb, atom.size-8);
375  }
376  }
377  return err;
378 }
379 
380 static int r3d_probe(const AVProbeData *p)
381 {
382  if (AV_RL32(p->buf + 4) == MKTAG('R','E','D','1'))
383  return AVPROBE_SCORE_MAX;
384  return 0;
385 }
386 
388  .name = "r3d",
389  .long_name = NULL_IF_CONFIG_SMALL("REDCODE R3D"),
390  .priv_data_size = sizeof(R3DContext),
394 };
#define av_unused
Definition: attributes.h:131
Main libavformat public API header.
#define AVINDEX_KEYFRAME
Definition: avformat.h:811
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1177
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:310
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:349
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:782
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:344
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:645
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:766
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:797
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:636
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define AV_RL32
Definition: intreadwrite.h:146
#define s(width, name)
Definition: cbs_vp9.c:257
#define MKTAG(a, b, c, d)
Definition: common.h:478
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
Public dictionary API.
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:555
@ AV_CODEC_ID_JPEG2000
Definition: codec_id.h:137
@ AV_CODEC_ID_PCM_S32BE
Definition: codec_id.h:322
@ AVDISCARD_ALL
discard all
Definition: avcodec.h:236
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4508
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: utils.c:2013
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:70
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:220
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
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.
Definition: mathematics.c:129
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
int i
Definition: input.c:407
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:4944
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static int r3d_read_red1(AVFormatContext *s)
Definition: r3d.c:53
static int r3d_read_rdvo(AVFormatContext *s, Atom *atom)
Definition: r3d.c:116
static int r3d_read_redv(AVFormatContext *s, AVPacket *pkt, Atom *atom)
Definition: r3d.c:226
static int read_atom(AVFormatContext *s, Atom *atom)
Definition: r3d.c:41
AVInputFormat ff_r3d_demuxer
Definition: r3d.c:387
static int r3d_read_reda(AVFormatContext *s, AVPacket *pkt, Atom *atom)
Definition: r3d.c:281
static void r3d_read_reos(AVFormatContext *s)
Definition: r3d.c:149
static int r3d_read_header(AVFormatContext *s)
Definition: r3d.c:168
static int r3d_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: r3d.c:346
static int r3d_probe(const AVProbeData *p)
Definition: r3d.c:380
unsigned int pos
Definition: spdifenc.c:412
int channels
Audio only.
Definition: codec_par.h:166
int width
Video only.
Definition: codec_par.h:126
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int sample_rate
Audio only.
Definition: codec_par.h:170
Format I/O context.
Definition: avformat.h:1232
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:368
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
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:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:922
AVDictionary * metadata
Definition: avformat.h:937
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:946
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:902
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:1015
Definition: r3d.c:35
unsigned size
Definition: r3d.c:36
uint64_t offset
Definition: r3d.c:38
uint32_t tag
Definition: r3d.c:37
Definition: r3d.c:28
unsigned video_offsets_count
Definition: r3d.c:29
int audio_channels
Definition: r3d.c:32
unsigned rdvo_offset
Definition: r3d.c:30
#define av_log(a,...)
static uint8_t tmp[11]
Definition: aes_ctr.c:27
int framerate
Definition: h264_levels.c:65
FILE * out
Definition: movenc.c:54
AVPacket * pkt
Definition: movenc.c:59
int size