]> git.sesse.net Git - ffmpeg/blob - libavformat/idroq.c
cleanup frame_rate code, this may also fix some large file bugs
[ffmpeg] / libavformat / idroq.c
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 RoQ_MAGIC_NUMBER 0x1084
31 #define RoQ_CHUNK_PREAMBLE_SIZE 8
32 #define RoQ_AUDIO_SAMPLE_RATE 22050
33 #define RoQ_CHUNKS_TO_SCAN 30
34
35 #define RoQ_INFO           0x1001
36 #define RoQ_QUAD_CODEBOOK  0x1002
37 #define RoQ_QUAD_VQ        0x1011
38 #define RoQ_SOUND_MONO     0x1020
39 #define RoQ_SOUND_STEREO   0x1021
40
41 typedef struct RoqDemuxContext {
42
43     int width;
44     int height;
45     int audio_channels;
46     int framerate;
47     int frame_pts_inc;
48
49     int video_stream_index;
50     int audio_stream_index;
51
52     int64_t video_pts;
53     unsigned int audio_frame_count;
54
55 } RoqDemuxContext;
56
57 static int roq_probe(AVProbeData *p)
58 {
59     if (p->buf_size < 6)
60         return 0;
61
62     if ((LE_16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
63         (LE_32(&p->buf[2]) != 0xFFFFFFFF))
64         return 0;
65
66     return AVPROBE_SCORE_MAX;
67 }
68
69 static int roq_read_header(AVFormatContext *s,
70                            AVFormatParameters *ap)
71 {
72     RoqDemuxContext *roq = s->priv_data;
73     ByteIOContext *pb = &s->pb;
74     AVStream *st;
75     unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
76     int i;
77     unsigned int chunk_size;
78     unsigned int chunk_type;
79
80     /* get the main header */
81     if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != 
82         RoQ_CHUNK_PREAMBLE_SIZE)
83         return AVERROR_IO;
84     roq->framerate = LE_16(&preamble[6]);
85     roq->frame_pts_inc = 90000 / roq->framerate;
86
87     /* set the pts reference (1 pts = 1/90000) */
88     s->pts_num = 1;
89     s->pts_den = 90000;
90
91     /* init private context parameters */
92     roq->width = roq->height = roq->audio_channels = roq->video_pts = 
93     roq->audio_frame_count = 0;
94
95     /* scan the first n chunks searching for A/V parameters */
96     for (i = 0; i < RoQ_CHUNKS_TO_SCAN; i++) {
97         if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != 
98             RoQ_CHUNK_PREAMBLE_SIZE)
99             return AVERROR_IO;
100
101         chunk_type = LE_16(&preamble[0]);
102         chunk_size = LE_32(&preamble[2]);
103
104         switch (chunk_type) {
105
106         case RoQ_INFO:
107             /* fetch the width and height; reuse the preamble bytes */
108             if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != 
109                 RoQ_CHUNK_PREAMBLE_SIZE)
110                 return AVERROR_IO;
111             roq->width = LE_16(&preamble[0]);
112             roq->height = LE_16(&preamble[2]);
113             break;
114
115         case RoQ_QUAD_CODEBOOK:
116         case RoQ_QUAD_VQ:
117             /* ignore during this scan */
118             url_fseek(pb, chunk_size, SEEK_CUR);
119             break;
120
121         case RoQ_SOUND_MONO:
122             roq->audio_channels = 1;
123             url_fseek(pb, chunk_size, SEEK_CUR);
124             break;
125
126         case RoQ_SOUND_STEREO:
127             roq->audio_channels = 2;
128             url_fseek(pb, chunk_size, SEEK_CUR);
129             break;
130
131         default:
132             av_log(s, AV_LOG_ERROR, " unknown RoQ chunk type (%04X)\n", LE_16(&preamble[0]));
133             return AVERROR_INVALIDDATA;
134             break;
135         }
136
137         /* if all necessary parameters have been gathered, exit early */
138         if ((roq->width && roq->height) && roq->audio_channels)
139             break;
140     }
141
142     /* seek back to the first chunk */
143     url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_SET);
144
145     /* initialize the decoders */
146     st = av_new_stream(s, 0);
147     if (!st)
148         return AVERROR_NOMEM;
149     roq->video_stream_index = st->index;
150     st->codec.codec_type = CODEC_TYPE_VIDEO;
151     st->codec.codec_id = CODEC_ID_ROQ;
152     st->codec.codec_tag = 0;  /* no fourcc */
153     st->codec.width = roq->width;
154     st->codec.height = roq->height;
155
156     if (roq->audio_channels) {
157         st = av_new_stream(s, 0);
158         if (!st)
159             return AVERROR_NOMEM;
160         roq->audio_stream_index = st->index;
161         st->codec.codec_type = CODEC_TYPE_AUDIO;
162         st->codec.codec_id = CODEC_ID_ROQ_DPCM;
163         st->codec.codec_tag = 0;  /* no tag */
164         st->codec.channels = roq->audio_channels;
165         st->codec.sample_rate = RoQ_AUDIO_SAMPLE_RATE;
166         st->codec.bits_per_sample = 16;
167         st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
168             st->codec.bits_per_sample;
169         st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
170     }
171
172     return 0;
173 }
174
175 static int roq_read_packet(AVFormatContext *s,
176                            AVPacket *pkt)
177 {
178     RoqDemuxContext *roq = s->priv_data;
179     ByteIOContext *pb = &s->pb;
180     int ret = 0;
181     unsigned int chunk_size;
182     unsigned int chunk_type;
183     unsigned int codebook_size;
184     unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
185     int packet_read = 0;
186     offset_t codebook_offset;
187
188     while (!packet_read) {
189
190         if (url_feof(&s->pb))
191             return -EIO;
192
193         /* get the next chunk preamble */
194         if ((ret = get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) != 
195             RoQ_CHUNK_PREAMBLE_SIZE)
196             return -EIO;
197
198         chunk_type = LE_16(&preamble[0]);
199         chunk_size = LE_32(&preamble[2]);
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 -EIO;
216             chunk_size = LE_32(&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             if (av_new_packet(pkt, chunk_size))
224                 return -EIO;
225             pkt->stream_index = roq->video_stream_index;
226             pkt->pts = roq->video_pts;
227             ret = get_buffer(pb, pkt->data, chunk_size);
228             if (ret != chunk_size)
229                 ret = -EIO;
230
231             roq->video_pts += roq->frame_pts_inc;
232             packet_read = 1;
233             break;
234
235         case RoQ_SOUND_MONO:
236         case RoQ_SOUND_STEREO:
237         case RoQ_QUAD_VQ:
238             /* load up the packet */
239             if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
240                 return -EIO;
241             /* copy over preamble */
242             memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
243
244             if (chunk_type == RoQ_QUAD_VQ) {
245                 pkt->stream_index = roq->video_stream_index;
246                 pkt->pts = roq->video_pts;
247                 roq->video_pts += roq->frame_pts_inc;
248             } else {
249                 pkt->stream_index = roq->audio_stream_index;
250                 pkt->pts = roq->audio_frame_count;
251                 pkt->pts *= 90000;
252                 pkt->pts /= RoQ_AUDIO_SAMPLE_RATE;
253                 roq->audio_frame_count += (chunk_size / roq->audio_channels);
254             }
255
256             ret = get_buffer(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
257                 chunk_size);
258             if (ret != chunk_size)
259                 ret = -EIO;
260
261             packet_read = 1;
262             break;
263
264         default:
265             av_log(s, AV_LOG_ERROR, "  unknown RoQ chunk (%04X)\n", chunk_type);
266             return AVERROR_INVALIDDATA;
267             break;
268         }
269     }
270
271     return ret;
272 }
273
274 static int roq_read_close(AVFormatContext *s)
275 {
276 //    RoqDemuxContext *roq = (RoqDemuxContext *)s->priv_data;
277
278     return 0;
279 }
280
281 static AVInputFormat roq_iformat = {
282     "RoQ",
283     "Id RoQ format",
284     sizeof(RoqDemuxContext),
285     roq_probe,
286     roq_read_header,
287     roq_read_packet,
288     roq_read_close,
289 };
290
291 int roq_init(void)
292 {
293     av_register_input_format(&roq_iformat);
294     return 0;
295 }