FFmpeg  2.1.1
idroqdec.c
Go to the documentation of this file.
1 /*
2  * id RoQ (.roq) File Demuxer
3  * Copyright (c) 2003 The ffmpeg Project
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 /**
23  * @file
24  * id RoQ format file demuxer
25  * by Mike Melanson (melanson@pcisys.net)
26  * for more information on the .roq file format, visit:
27  * http://www.csse.monash.edu.au/~timf/
28  */
29 
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/intreadwrite.h"
32 #include "avformat.h"
33 #include "internal.h"
34 #include "avio_internal.h"
35 
36 #define RoQ_MAGIC_NUMBER 0x1084
37 #define RoQ_CHUNK_PREAMBLE_SIZE 8
38 #define RoQ_AUDIO_SAMPLE_RATE 22050
39 #define RoQ_CHUNKS_TO_SCAN 30
40 
41 #define RoQ_INFO 0x1001
42 #define RoQ_QUAD_CODEBOOK 0x1002
43 #define RoQ_QUAD_VQ 0x1011
44 #define RoQ_SOUND_MONO 0x1020
45 #define RoQ_SOUND_STEREO 0x1021
46 
47 typedef struct RoqDemuxContext {
48 
50  int width;
51  int height;
53 
56 
57  int64_t video_pts;
58  unsigned int audio_frame_count;
59 
61 
62 static int roq_probe(AVProbeData *p)
63 {
64  if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
65  (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
66  return 0;
67 
68  return AVPROBE_SCORE_MAX;
69 }
70 
72 {
73  RoqDemuxContext *roq = s->priv_data;
74  AVIOContext *pb = s->pb;
75  unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
76 
77  /* get the main header */
78  if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
80  return AVERROR(EIO);
81  roq->frame_rate = AV_RL16(&preamble[6]);
82 
83  /* init private context parameters */
84  roq->width = roq->height = roq->audio_channels = roq->video_pts =
85  roq->audio_frame_count = 0;
86  roq->audio_stream_index = -1;
87  roq->video_stream_index = -1;
88 
90 
91  return 0;
92 }
93 
95  AVPacket *pkt)
96 {
97  RoqDemuxContext *roq = s->priv_data;
98  AVIOContext *pb = s->pb;
99  int ret = 0;
100  unsigned int chunk_size;
101  unsigned int chunk_type;
102  unsigned int codebook_size;
103  unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
104  int packet_read = 0;
105  int64_t codebook_offset;
106 
107  while (!packet_read) {
108 
109  if (url_feof(s->pb))
110  return AVERROR(EIO);
111 
112  /* get the next chunk preamble */
113  if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
115  return AVERROR(EIO);
116 
117  chunk_type = AV_RL16(&preamble[0]);
118  chunk_size = AV_RL32(&preamble[2]);
119  if(chunk_size > INT_MAX)
120  return AVERROR_INVALIDDATA;
121 
122  chunk_size = ffio_limit(pb, chunk_size);
123 
124  switch (chunk_type) {
125 
126  case RoQ_INFO:
127  if (roq->video_stream_index == -1) {
128  AVStream *st = avformat_new_stream(s, NULL);
129  if (!st)
130  return AVERROR(ENOMEM);
131  avpriv_set_pts_info(st, 63, 1, roq->frame_rate);
132  roq->video_stream_index = st->index;
135  st->codec->codec_tag = 0; /* no fourcc */
136 
138  return AVERROR(EIO);
139  st->codec->width = roq->width = AV_RL16(preamble);
140  st->codec->height = roq->height = AV_RL16(preamble + 2);
141  break;
142  }
143  /* don't care about this chunk anymore */
145  break;
146 
147  case RoQ_QUAD_CODEBOOK:
148  if (roq->video_stream_index < 0)
149  return AVERROR_INVALIDDATA;
150  /* packet needs to contain both this codebook and next VQ chunk */
151  codebook_offset = avio_tell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
152  codebook_size = chunk_size;
153  avio_skip(pb, codebook_size);
154  if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
156  return AVERROR(EIO);
157  chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
158  codebook_size;
159 
160  /* rewind */
161  avio_seek(pb, codebook_offset, SEEK_SET);
162 
163  /* load up the packet */
164  ret= av_get_packet(pb, pkt, chunk_size);
165  if (ret != chunk_size)
166  return AVERROR(EIO);
167  pkt->stream_index = roq->video_stream_index;
168  pkt->pts = roq->video_pts++;
169 
170  packet_read = 1;
171  break;
172 
173  case RoQ_SOUND_MONO:
174  case RoQ_SOUND_STEREO:
175  if (roq->audio_stream_index == -1) {
176  AVStream *st = avformat_new_stream(s, NULL);
177  if (!st)
178  return AVERROR(ENOMEM);
180  roq->audio_stream_index = st->index;
183  st->codec->codec_tag = 0; /* no tag */
184  if (chunk_type == RoQ_SOUND_STEREO) {
185  st->codec->channels = 2;
187  } else {
188  st->codec->channels = 1;
190  }
191  roq->audio_channels = st->codec->channels;
193  st->codec->bits_per_coded_sample = 16;
194  st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
197  }
198  case RoQ_QUAD_VQ:
199  if (chunk_type == RoQ_QUAD_VQ) {
200  if (roq->video_stream_index < 0)
201  return AVERROR_INVALIDDATA;
202  }
203 
204  /* load up the packet */
205  if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
206  return AVERROR(EIO);
207  /* copy over preamble */
208  memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
209 
210  if (chunk_type == RoQ_QUAD_VQ) {
211  pkt->stream_index = roq->video_stream_index;
212  pkt->pts = roq->video_pts++;
213  } else {
214  pkt->stream_index = roq->audio_stream_index;
215  pkt->pts = roq->audio_frame_count;
216  roq->audio_frame_count += (chunk_size / roq->audio_channels);
217  }
218 
219  pkt->pos= avio_tell(pb);
220  ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
221  chunk_size);
222  if (ret != chunk_size)
223  ret = AVERROR(EIO);
224 
225  packet_read = 1;
226  break;
227 
228  default:
229  av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type);
230  return AVERROR_INVALIDDATA;
231  }
232  }
233 
234  return ret;
235 }
236 
238  .name = "roq",
239  .long_name = NULL_IF_CONFIG_SMALL("id RoQ"),
240  .priv_data_size = sizeof(RoqDemuxContext),
244 };
int audio_channels
Definition: idroqdec.c:52
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:478
unsigned int audio_frame_count
Definition: idroqdec.c:58
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1092
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:3922
#define AV_RL16(x)
Definition: intreadwrite.h:245
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
int index
stream index in AVFormatContext
Definition: avformat.h:668
static int roq_probe(AVProbeData *p)
Definition: idroqdec.c:62
int64_t video_pts
Definition: idroqdec.c:57
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
#define AV_CH_LAYOUT_STEREO
int ctx_flags
Format-specific flags, see AVFMTCTX_xx.
Definition: avformat.h:1004
#define RoQ_SOUND_STEREO
Definition: idroqdec.c:45
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1910
#define RoQ_MAGIC_NUMBER
Definition: idroqdec.c:36
Format I/O context.
Definition: avformat.h:968
int ffio_limit(AVIOContext *s, int size)
Definition: utils.c:133
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
static int roq_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: idroqdec.c:94
#define RoQ_INFO
Definition: idroqdec.c:41
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2563
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3348
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:200
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:83
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
void * priv_data
Format private data.
Definition: avformat.h:988
int audio_stream_index
Definition: idroqdec.c:55
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
#define RoQ_SOUND_MONO
Definition: idroqdec.c:44
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1934
int bit_rate
the average bitrate
Definition: avcodec.h:1204
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:54
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:592
Stream structure.
Definition: avformat.h:667
enum AVMediaType codec_type
Definition: avcodec.h:1154
enum AVCodecID codec_id
Definition: avcodec.h:1157
int sample_rate
samples per second
Definition: avcodec.h:1873
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:938
unsigned int codec_tag
fourcc (LSB first, so &quot;ABCD&quot; -&gt; (&#39;D&#39;&lt;&lt;24) + (&#39;C&#39;&lt;&lt;16) + (&#39;B&#39;&lt;&lt;8) + &#39;A&#39;).
Definition: avcodec.h:1172
AVIOContext * pb
I/O context.
Definition: avformat.h:1001
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
#define RoQ_CHUNK_PREAMBLE_SIZE
Definition: idroqdec.c:37
uint8_t * data
Definition: avcodec.h:1063
#define RoQ_AUDIO_SAMPLE_RATE
Definition: idroqdec.c:38
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:342
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:480
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
Main libavformat public API header.
#define AVERROR_INVALIDDATA
#define AV_RL32(x)
Definition: intreadwrite.h:275
int channels
number of audio channels
Definition: avcodec.h:1874
int video_stream_index
Definition: idroqdec.c:54
#define AVERROR(e)
#define RoQ_QUAD_CODEBOOK
Definition: idroqdec.c:42
static AVPacket pkt
Definition: demuxing.c:52
int stream_index
Definition: avcodec.h:1065
#define AV_CH_LAYOUT_MONO
AVInputFormat ff_roq_demuxer
Definition: idroqdec.c:237
This structure stores compressed data.
Definition: avcodec.h:1040
int64_t pts
Presentation timestamp in AVStream-&gt;time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1056
static int roq_read_header(AVFormatContext *s)
Definition: idroqdec.c:71
#define RoQ_QUAD_VQ
Definition: idroqdec.c:43