av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
[libav.git] / libavformat / idroq.c
CommitLineData
3ef8be2b
MM
1/*
2 * Id RoQ (.roq) File Demuxer
3 * Copyright (c) 2003 The ffmpeg Project
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/**
21 * @file idroq.c
22 * Id RoQ format file demuxer
23 * by Mike Melanson (melanson@pcisys.net)
24 * for more information on the .roq file format, visit:
25 * http://www.csse.monash.edu.au/~timf/
26 */
27
28#include "avformat.h"
29
30#define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
31#define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
32 (((uint8_t*)(x))[2] << 16) | \
33 (((uint8_t*)(x))[1] << 8) | \
34 ((uint8_t*)(x))[0])
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
47typedef struct RoqDemuxContext {
48
49 int width;
50 int height;
51 int audio_channels;
52 int framerate;
53 int frame_pts_inc;
54
55 int video_stream_index;
56 int audio_stream_index;
57
58 int64_t video_pts;
59 unsigned int audio_frame_count;
60
61} RoqDemuxContext;
62
63static int roq_probe(AVProbeData *p)
64{
e7106d5b
MM
65 if (p->buf_size < 6)
66 return 0;
67
3ef8be2b
MM
68 if ((LE_16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
69 (LE_32(&p->buf[2]) != 0xFFFFFFFF))
70 return 0;
71
72 return AVPROBE_SCORE_MAX;
73}
74
75static int roq_read_header(AVFormatContext *s,
76 AVFormatParameters *ap)
77{
78 RoqDemuxContext *roq = s->priv_data;
79 ByteIOContext *pb = &s->pb;
80 AVStream *st;
81 unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
82 int i;
83 unsigned int chunk_size;
84 unsigned int chunk_type;
85
86 /* get the main header */
87 if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
88 RoQ_CHUNK_PREAMBLE_SIZE)
89 return AVERROR_IO;
90 roq->framerate = LE_16(&preamble[6]);
91 roq->frame_pts_inc = 90000 / roq->framerate;
92
93 /* set the pts reference (1 pts = 1/90000) */
94 s->pts_num = 1;
95 s->pts_den = 90000;
96
97 /* init private context parameters */
98 roq->width = roq->height = roq->audio_channels = roq->video_pts =
99 roq->audio_frame_count = 0;
100
101 /* scan the first n chunks searching for A/V parameters */
102 for (i = 0; i < RoQ_CHUNKS_TO_SCAN; i++) {
103 if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
104 RoQ_CHUNK_PREAMBLE_SIZE)
105 return AVERROR_IO;
106
107 chunk_type = LE_16(&preamble[0]);
108 chunk_size = LE_32(&preamble[2]);
109
110 switch (chunk_type) {
111
112 case RoQ_INFO:
113 /* fetch the width and height; reuse the preamble bytes */
114 if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
115 RoQ_CHUNK_PREAMBLE_SIZE)
116 return AVERROR_IO;
117 roq->width = LE_16(&preamble[0]);
118 roq->height = LE_16(&preamble[2]);
119 break;
120
121 case RoQ_QUAD_CODEBOOK:
122 case RoQ_QUAD_VQ:
123 /* ignore during this scan */
124 url_fseek(pb, chunk_size, SEEK_CUR);
125 break;
126
127 case RoQ_SOUND_MONO:
128 roq->audio_channels = 1;
129 url_fseek(pb, chunk_size, SEEK_CUR);
130 break;
131
132 case RoQ_SOUND_STEREO:
133 roq->audio_channels = 2;
134 url_fseek(pb, chunk_size, SEEK_CUR);
135 break;
136
137 default:
bc874dae 138 av_log(s, AV_LOG_ERROR, " unknown RoQ chunk type (%04X)\n", LE_16(&preamble[0]));
3ef8be2b
MM
139 return AVERROR_INVALIDDATA;
140 break;
141 }
142
143 /* if all necessary parameters have been gathered, exit early */
144 if ((roq->width && roq->height) && roq->audio_channels)
145 break;
146 }
147
148 /* seek back to the first chunk */
149 url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_SET);
150
151 /* initialize the decoders */
152 st = av_new_stream(s, 0);
153 if (!st)
154 return AVERROR_NOMEM;
155 roq->video_stream_index = st->index;
156 st->codec.codec_type = CODEC_TYPE_VIDEO;
157 st->codec.codec_id = CODEC_ID_ROQ;
158 st->codec.codec_tag = 0; /* no fourcc */
159 st->codec.width = roq->width;
160 st->codec.height = roq->height;
161
162 if (roq->audio_channels) {
163 st = av_new_stream(s, 0);
164 if (!st)
165 return AVERROR_NOMEM;
166 roq->audio_stream_index = st->index;
167 st->codec.codec_type = CODEC_TYPE_AUDIO;
168 st->codec.codec_id = CODEC_ID_ROQ_DPCM;
169 st->codec.codec_tag = 0; /* no tag */
170 st->codec.channels = roq->audio_channels;
171 st->codec.sample_rate = RoQ_AUDIO_SAMPLE_RATE;
172 st->codec.bits_per_sample = 16;
173 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
174 st->codec.bits_per_sample;
175 st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
176 }
3ef8be2b
MM
177
178 return 0;
179}
180
181static int roq_read_packet(AVFormatContext *s,
182 AVPacket *pkt)
183{
184 RoqDemuxContext *roq = s->priv_data;
185 ByteIOContext *pb = &s->pb;
186 int ret = 0;
187 unsigned int chunk_size;
188 unsigned int chunk_type;
189 unsigned int codebook_size;
190 unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
191 int packet_read = 0;
192 offset_t codebook_offset;
193
194 while (!packet_read) {
195
196 if (url_feof(&s->pb))
197 return -EIO;
198
199 /* get the next chunk preamble */
200 if ((ret = get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
201 RoQ_CHUNK_PREAMBLE_SIZE)
202 return -EIO;
203
204 chunk_type = LE_16(&preamble[0]);
205 chunk_size = LE_32(&preamble[2]);
206
207 switch (chunk_type) {
208
209 case RoQ_INFO:
210 /* don't care about this chunk anymore */
211 url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_CUR);
212 break;
213
214 case RoQ_QUAD_CODEBOOK:
215 /* packet needs to contain both this codebook and next VQ chunk */
216 codebook_offset = url_ftell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
217 codebook_size = chunk_size;
218 url_fseek(pb, codebook_size, SEEK_CUR);
219 if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
220 RoQ_CHUNK_PREAMBLE_SIZE)
221 return -EIO;
222 chunk_size = LE_32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
223 codebook_size;
224
225 /* rewind */
226 url_fseek(pb, codebook_offset, SEEK_SET);
227
228 /* load up the packet */
229 if (av_new_packet(pkt, chunk_size))
230 return -EIO;
231 pkt->stream_index = roq->video_stream_index;
232 pkt->pts = roq->video_pts;
233 ret = get_buffer(pb, pkt->data, chunk_size);
234 if (ret != chunk_size)
235 ret = -EIO;
236
237 roq->video_pts += roq->frame_pts_inc;
238 packet_read = 1;
239 break;
240
241 case RoQ_SOUND_MONO:
242 case RoQ_SOUND_STEREO:
243 case RoQ_QUAD_VQ:
244 /* load up the packet */
245 if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
246 return -EIO;
247 /* copy over preamble */
248 memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
249
250 if (chunk_type == RoQ_QUAD_VQ) {
251 pkt->stream_index = roq->video_stream_index;
252 pkt->pts = roq->video_pts;
253 roq->video_pts += roq->frame_pts_inc;
254 } else {
255 pkt->stream_index = roq->audio_stream_index;
256 pkt->pts = roq->audio_frame_count;
257 pkt->pts *= 90000;
258 pkt->pts /= RoQ_AUDIO_SAMPLE_RATE;
259 roq->audio_frame_count += (chunk_size / roq->audio_channels);
260 }
261
d42f74b8
MM
262 ret = get_buffer(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
263 chunk_size);
3ef8be2b
MM
264 if (ret != chunk_size)
265 ret = -EIO;
266
267 packet_read = 1;
268 break;
269
270 default:
bc874dae 271 av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type);
3ef8be2b
MM
272 return AVERROR_INVALIDDATA;
273 break;
274 }
275 }
276
277 return ret;
278}
279
280static int roq_read_close(AVFormatContext *s)
281{
282// RoqDemuxContext *roq = (RoqDemuxContext *)s->priv_data;
283
284 return 0;
285}
286
287static AVInputFormat roq_iformat = {
288 "RoQ",
289 "Id RoQ format",
290 sizeof(RoqDemuxContext),
291 roq_probe,
292 roq_read_header,
293 roq_read_packet,
294 roq_read_close,
295};
296
297int roq_init(void)
298{
299 av_register_input_format(&roq_iformat);
300 return 0;
301}