2 * id RoQ (.roq) File Demuxer
3 * Copyright (c) 2003 The ffmpeg Project
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
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/
32 #define RoQ_MAGIC_NUMBER 0x1084
33 #define RoQ_CHUNK_PREAMBLE_SIZE 8
34 #define RoQ_AUDIO_SAMPLE_RATE 22050
35 #define RoQ_CHUNKS_TO_SCAN 30
37 #define RoQ_INFO 0x1001
38 #define RoQ_QUAD_CODEBOOK 0x1002
39 #define RoQ_QUAD_VQ 0x1011
40 #define RoQ_SOUND_MONO 0x1020
41 #define RoQ_SOUND_STEREO 0x1021
43 typedef struct RoqDemuxContext
{
51 int video_stream_index
;
52 int audio_stream_index
;
55 unsigned int audio_frame_count
;
59 static int roq_probe(AVProbeData
*p
)
61 if ((AV_RL16(&p
->buf
[0]) != RoQ_MAGIC_NUMBER
) ||
62 (AV_RL32(&p
->buf
[2]) != 0xFFFFFFFF))
65 return AVPROBE_SCORE_MAX
;
68 static int roq_read_header(AVFormatContext
*s
,
69 AVFormatParameters
*ap
)
71 RoqDemuxContext
*roq
= s
->priv_data
;
72 ByteIOContext
*pb
= s
->pb
;
74 unsigned char preamble
[RoQ_CHUNK_PREAMBLE_SIZE
];
76 unsigned int chunk_size
;
77 unsigned int chunk_type
;
79 /* get the main header */
80 if (get_buffer(pb
, preamble
, RoQ_CHUNK_PREAMBLE_SIZE
) !=
81 RoQ_CHUNK_PREAMBLE_SIZE
)
83 roq
->framerate
= AV_RL16(&preamble
[6]);
84 roq
->frame_pts_inc
= 90000 / roq
->framerate
;
86 /* init private context parameters */
87 roq
->width
= roq
->height
= roq
->audio_channels
= roq
->video_pts
=
88 roq
->audio_frame_count
= 0;
90 /* scan the first n chunks searching for A/V parameters */
91 for (i
= 0; i
< RoQ_CHUNKS_TO_SCAN
; i
++) {
92 if (get_buffer(pb
, preamble
, RoQ_CHUNK_PREAMBLE_SIZE
) !=
93 RoQ_CHUNK_PREAMBLE_SIZE
)
96 chunk_type
= AV_RL16(&preamble
[0]);
97 chunk_size
= AV_RL32(&preamble
[2]);
102 /* fetch the width and height; reuse the preamble bytes */
103 if (get_buffer(pb
, preamble
, RoQ_CHUNK_PREAMBLE_SIZE
) !=
104 RoQ_CHUNK_PREAMBLE_SIZE
)
106 roq
->width
= AV_RL16(&preamble
[0]);
107 roq
->height
= AV_RL16(&preamble
[2]);
110 case RoQ_QUAD_CODEBOOK
:
112 /* ignore during this scan */
113 url_fseek(pb
, chunk_size
, SEEK_CUR
);
117 roq
->audio_channels
= 1;
118 url_fseek(pb
, chunk_size
, SEEK_CUR
);
121 case RoQ_SOUND_STEREO
:
122 roq
->audio_channels
= 2;
123 url_fseek(pb
, chunk_size
, SEEK_CUR
);
127 av_log(s
, AV_LOG_ERROR
, " unknown RoQ chunk type (%04X)\n", AV_RL16(&preamble
[0]));
128 return AVERROR_INVALIDDATA
;
132 /* if all necessary parameters have been gathered, exit early */
133 if ((roq
->width
&& roq
->height
) && roq
->audio_channels
)
137 /* seek back to the first chunk */
138 url_fseek(pb
, RoQ_CHUNK_PREAMBLE_SIZE
, SEEK_SET
);
140 /* initialize the decoders */
141 st
= av_new_stream(s
, 0);
143 return AVERROR(ENOMEM
);
144 /* set the pts reference (1 pts = 1/90000) */
145 av_set_pts_info(st
, 33, 1, 90000);
146 roq
->video_stream_index
= st
->index
;
147 st
->codec
->codec_type
= CODEC_TYPE_VIDEO
;
148 st
->codec
->codec_id
= CODEC_ID_ROQ
;
149 st
->codec
->codec_tag
= 0; /* no fourcc */
150 st
->codec
->width
= roq
->width
;
151 st
->codec
->height
= roq
->height
;
153 if (roq
->audio_channels
) {
154 st
= av_new_stream(s
, 0);
156 return AVERROR(ENOMEM
);
157 av_set_pts_info(st
, 33, 1, 90000);
158 roq
->audio_stream_index
= st
->index
;
159 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
160 st
->codec
->codec_id
= CODEC_ID_ROQ_DPCM
;
161 st
->codec
->codec_tag
= 0; /* no tag */
162 st
->codec
->channels
= roq
->audio_channels
;
163 st
->codec
->sample_rate
= RoQ_AUDIO_SAMPLE_RATE
;
164 st
->codec
->bits_per_sample
= 16;
165 st
->codec
->bit_rate
= st
->codec
->channels
* st
->codec
->sample_rate
*
166 st
->codec
->bits_per_sample
;
167 st
->codec
->block_align
= st
->codec
->channels
* st
->codec
->bits_per_sample
;
173 static int roq_read_packet(AVFormatContext
*s
,
176 RoqDemuxContext
*roq
= s
->priv_data
;
177 ByteIOContext
*pb
= s
->pb
;
179 unsigned int chunk_size
;
180 unsigned int chunk_type
;
181 unsigned int codebook_size
;
182 unsigned char preamble
[RoQ_CHUNK_PREAMBLE_SIZE
];
184 offset_t codebook_offset
;
186 while (!packet_read
) {
191 /* get the next chunk preamble */
192 if ((ret
= get_buffer(pb
, preamble
, RoQ_CHUNK_PREAMBLE_SIZE
)) !=
193 RoQ_CHUNK_PREAMBLE_SIZE
)
196 chunk_type
= AV_RL16(&preamble
[0]);
197 chunk_size
= AV_RL32(&preamble
[2]);
198 if(chunk_size
> INT_MAX
)
199 return AVERROR_INVALIDDATA
;
201 switch (chunk_type
) {
204 /* don't care about this chunk anymore */
205 url_fseek(pb
, RoQ_CHUNK_PREAMBLE_SIZE
, SEEK_CUR
);
208 case RoQ_QUAD_CODEBOOK
:
209 /* packet needs to contain both this codebook and next VQ chunk */
210 codebook_offset
= url_ftell(pb
) - RoQ_CHUNK_PREAMBLE_SIZE
;
211 codebook_size
= chunk_size
;
212 url_fseek(pb
, codebook_size
, SEEK_CUR
);
213 if (get_buffer(pb
, preamble
, RoQ_CHUNK_PREAMBLE_SIZE
) !=
214 RoQ_CHUNK_PREAMBLE_SIZE
)
216 chunk_size
= AV_RL32(&preamble
[2]) + RoQ_CHUNK_PREAMBLE_SIZE
* 2 +
220 url_fseek(pb
, codebook_offset
, SEEK_SET
);
222 /* load up the packet */
223 ret
= av_get_packet(pb
, pkt
, chunk_size
);
224 if (ret
!= chunk_size
)
226 pkt
->stream_index
= roq
->video_stream_index
;
227 pkt
->pts
= roq
->video_pts
;
229 roq
->video_pts
+= roq
->frame_pts_inc
;
234 case RoQ_SOUND_STEREO
:
236 /* load up the packet */
237 if (av_new_packet(pkt
, chunk_size
+ RoQ_CHUNK_PREAMBLE_SIZE
))
239 /* copy over preamble */
240 memcpy(pkt
->data
, preamble
, RoQ_CHUNK_PREAMBLE_SIZE
);
242 if (chunk_type
== RoQ_QUAD_VQ
) {
243 pkt
->stream_index
= roq
->video_stream_index
;
244 pkt
->pts
= roq
->video_pts
;
245 roq
->video_pts
+= roq
->frame_pts_inc
;
247 pkt
->stream_index
= roq
->audio_stream_index
;
248 pkt
->pts
= roq
->audio_frame_count
;
250 pkt
->pts
/= RoQ_AUDIO_SAMPLE_RATE
;
251 roq
->audio_frame_count
+= (chunk_size
/ roq
->audio_channels
);
254 pkt
->pos
= url_ftell(pb
);
255 ret
= get_buffer(pb
, pkt
->data
+ RoQ_CHUNK_PREAMBLE_SIZE
,
257 if (ret
!= chunk_size
)
264 av_log(s
, AV_LOG_ERROR
, " unknown RoQ chunk (%04X)\n", chunk_type
);
265 return AVERROR_INVALIDDATA
;
273 AVInputFormat roq_demuxer
= {
275 NULL_IF_CONFIG_SMALL("id RoQ format"),
276 sizeof(RoqDemuxContext
),