]> git.sesse.net Git - ffmpeg/blob - libavformat/idroq.c
ddda86fec8d06ecdee1b8a8e35bc2a251650a84c
[ffmpeg] / libavformat / idroq.c
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 idroq.c
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 "avformat.h"
31
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
36
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
42
43 typedef struct RoqDemuxContext {
44
45     int width;
46     int height;
47     int audio_channels;
48     int framerate;
49     int frame_pts_inc;
50
51     int video_stream_index;
52     int audio_stream_index;
53
54     int64_t video_pts;
55     unsigned int audio_frame_count;
56
57 } RoqDemuxContext;
58
59 static int roq_probe(AVProbeData *p)
60 {
61     if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
62         (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
63         return 0;
64
65     return AVPROBE_SCORE_MAX;
66 }
67
68 static int roq_read_header(AVFormatContext *s,
69                            AVFormatParameters *ap)
70 {
71     RoqDemuxContext *roq = s->priv_data;
72     ByteIOContext *pb = &s->pb;
73     AVStream *st;
74     unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
75     int i;
76     unsigned int chunk_size;
77     unsigned int chunk_type;
78
79     /* get the main header */
80     if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
81         RoQ_CHUNK_PREAMBLE_SIZE)
82         return AVERROR_IO;
83     roq->framerate = AV_RL16(&preamble[6]);
84     roq->frame_pts_inc = 90000 / roq->framerate;
85
86     /* init private context parameters */
87     roq->width = roq->height = roq->audio_channels = roq->video_pts =
88     roq->audio_frame_count = 0;
89
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)
94             return AVERROR_IO;
95
96         chunk_type = AV_RL16(&preamble[0]);
97         chunk_size = AV_RL32(&preamble[2]);
98
99         switch (chunk_type) {
100
101         case RoQ_INFO:
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)
105                 return AVERROR_IO;
106             roq->width = AV_RL16(&preamble[0]);
107             roq->height = AV_RL16(&preamble[2]);
108             break;
109
110         case RoQ_QUAD_CODEBOOK:
111         case RoQ_QUAD_VQ:
112             /* ignore during this scan */
113             url_fseek(pb, chunk_size, SEEK_CUR);
114             break;
115
116         case RoQ_SOUND_MONO:
117             roq->audio_channels = 1;
118             url_fseek(pb, chunk_size, SEEK_CUR);
119             break;
120
121         case RoQ_SOUND_STEREO:
122             roq->audio_channels = 2;
123             url_fseek(pb, chunk_size, SEEK_CUR);
124             break;
125
126         default:
127             av_log(s, AV_LOG_ERROR, " unknown RoQ chunk type (%04X)\n", AV_RL16(&preamble[0]));
128             return AVERROR_INVALIDDATA;
129             break;
130         }
131
132         /* if all necessary parameters have been gathered, exit early */
133         if ((roq->width && roq->height) && roq->audio_channels)
134             break;
135     }
136
137     /* seek back to the first chunk */
138     url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_SET);
139
140     /* initialize the decoders */
141     st = av_new_stream(s, 0);
142     if (!st)
143         return AVERROR_NOMEM;
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;
152
153     if (roq->audio_channels) {
154         st = av_new_stream(s, 0);
155         if (!st)
156             return AVERROR_NOMEM;
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;
168     }
169
170     return 0;
171 }
172
173 static int roq_read_packet(AVFormatContext *s,
174                            AVPacket *pkt)
175 {
176     RoqDemuxContext *roq = s->priv_data;
177     ByteIOContext *pb = &s->pb;
178     int ret = 0;
179     unsigned int chunk_size;
180     unsigned int chunk_type;
181     unsigned int codebook_size;
182     unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
183     int packet_read = 0;
184     offset_t codebook_offset;
185
186     while (!packet_read) {
187
188         if (url_feof(&s->pb))
189             return AVERROR_IO;
190
191         /* get the next chunk preamble */
192         if ((ret = get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
193             RoQ_CHUNK_PREAMBLE_SIZE)
194             return AVERROR_IO;
195
196         chunk_type = AV_RL16(&preamble[0]);
197         chunk_size = AV_RL32(&preamble[2]);
198         if(chunk_size > INT_MAX)
199             return AVERROR_INVALIDDATA;
200
201         switch (chunk_type) {
202
203         case RoQ_INFO:
204             /* don't care about this chunk anymore */
205             url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_CUR);
206             break;
207
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)
215                 return AVERROR_IO;
216             chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
217                 codebook_size;
218
219             /* rewind */
220             url_fseek(pb, codebook_offset, SEEK_SET);
221
222             /* load up the packet */
223             ret= av_get_packet(pb, pkt, chunk_size);
224             if (ret != chunk_size)
225                 return AVERROR_IO;
226             pkt->stream_index = roq->video_stream_index;
227             pkt->pts = roq->video_pts;
228
229             roq->video_pts += roq->frame_pts_inc;
230             packet_read = 1;
231             break;
232
233         case RoQ_SOUND_MONO:
234         case RoQ_SOUND_STEREO:
235         case RoQ_QUAD_VQ:
236             /* load up the packet */
237             if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
238                 return AVERROR_IO;
239             /* copy over preamble */
240             memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
241
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;
246             } else {
247                 pkt->stream_index = roq->audio_stream_index;
248                 pkt->pts = roq->audio_frame_count;
249                 pkt->pts *= 90000;
250                 pkt->pts /= RoQ_AUDIO_SAMPLE_RATE;
251                 roq->audio_frame_count += (chunk_size / roq->audio_channels);
252             }
253
254             pkt->pos= url_ftell(pb);
255             ret = get_buffer(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
256                 chunk_size);
257             if (ret != chunk_size)
258                 ret = AVERROR_IO;
259
260             packet_read = 1;
261             break;
262
263         default:
264             av_log(s, AV_LOG_ERROR, "  unknown RoQ chunk (%04X)\n", chunk_type);
265             return AVERROR_INVALIDDATA;
266             break;
267         }
268     }
269
270     return ret;
271 }
272
273 static int roq_read_close(AVFormatContext *s)
274 {
275 //    RoqDemuxContext *roq = (RoqDemuxContext *)s->priv_data;
276
277     return 0;
278 }
279
280 AVInputFormat roq_demuxer = {
281     "RoQ",
282     "Id RoQ format",
283     sizeof(RoqDemuxContext),
284     roq_probe,
285     roq_read_header,
286     roq_read_packet,
287     roq_read_close,
288 };