Set video width/height and create audio stream in read_packet instead of
[ffmpeg.git] / 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 libavformat/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 "libavutil/intreadwrite.h"
31 #include "avformat.h"
32
33 #define RoQ_MAGIC_NUMBER 0x1084
34 #define RoQ_CHUNK_PREAMBLE_SIZE 8
35 #define RoQ_AUDIO_SAMPLE_RATE 22050
36 #define RoQ_CHUNKS_TO_SCAN 30
37
38 #define RoQ_INFO           0x1001
39 #define RoQ_QUAD_CODEBOOK  0x1002
40 #define RoQ_QUAD_VQ        0x1011
41 #define RoQ_SOUND_MONO     0x1020
42 #define RoQ_SOUND_STEREO   0x1021
43
44 typedef struct RoqDemuxContext {
45
46     int width;
47     int height;
48     int audio_channels;
49     int framerate;
50     int frame_pts_inc;
51
52     int video_stream_index;
53     int audio_stream_index;
54
55     int64_t video_pts;
56     unsigned int audio_frame_count;
57
58 } RoqDemuxContext;
59
60 static int roq_probe(AVProbeData *p)
61 {
62     if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
63         (AV_RL32(&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
77     /* get the main header */
78     if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
79         RoQ_CHUNK_PREAMBLE_SIZE)
80         return AVERROR(EIO);
81     roq->framerate = AV_RL16(&preamble[6]);
82     roq->frame_pts_inc = 90000 / roq->framerate;
83
84     /* init private context parameters */
85     roq->width = roq->height = roq->audio_channels = roq->video_pts =
86     roq->audio_frame_count = 0;
87     roq->audio_stream_index = -1;
88
89     st = av_new_stream(s, 0);
90     if (!st)
91         return AVERROR(ENOMEM);
92     /* set the pts reference (1 pts = 1/90000) */
93     av_set_pts_info(st, 33, 1, 90000);
94     roq->video_stream_index = st->index;
95     st->codec->codec_type = CODEC_TYPE_VIDEO;
96     st->codec->codec_id = CODEC_ID_ROQ;
97     st->codec->codec_tag = 0;  /* no fourcc */
98
99     return 0;
100 }
101
102 static int roq_read_packet(AVFormatContext *s,
103                            AVPacket *pkt)
104 {
105     RoqDemuxContext *roq = s->priv_data;
106     ByteIOContext *pb = s->pb;
107     int ret = 0;
108     unsigned int chunk_size;
109     unsigned int chunk_type;
110     unsigned int codebook_size;
111     unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
112     int packet_read = 0;
113     int64_t codebook_offset;
114
115     while (!packet_read) {
116
117         if (url_feof(s->pb))
118             return AVERROR(EIO);
119
120         /* get the next chunk preamble */
121         if ((ret = get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
122             RoQ_CHUNK_PREAMBLE_SIZE)
123             return AVERROR(EIO);
124
125         chunk_type = AV_RL16(&preamble[0]);
126         chunk_size = AV_RL32(&preamble[2]);
127         if(chunk_size > INT_MAX)
128             return AVERROR_INVALIDDATA;
129
130         switch (chunk_type) {
131
132         case RoQ_INFO:
133             if (!roq->width || !roq->height) {
134                 AVStream *st = s->streams[roq->video_stream_index];
135                 if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != RoQ_CHUNK_PREAMBLE_SIZE)
136                     return AVERROR(EIO);
137                 st->codec->width  = roq->width  = AV_RL16(preamble);
138                 st->codec->height = roq->height = AV_RL16(preamble + 2);
139                 break;
140             }
141             /* don't care about this chunk anymore */
142             url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_CUR);
143             break;
144
145         case RoQ_QUAD_CODEBOOK:
146             /* packet needs to contain both this codebook and next VQ chunk */
147             codebook_offset = url_ftell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
148             codebook_size = chunk_size;
149             url_fseek(pb, codebook_size, SEEK_CUR);
150             if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
151                 RoQ_CHUNK_PREAMBLE_SIZE)
152                 return AVERROR(EIO);
153             chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
154                 codebook_size;
155
156             /* rewind */
157             url_fseek(pb, codebook_offset, SEEK_SET);
158
159             /* load up the packet */
160             ret= av_get_packet(pb, pkt, chunk_size);
161             if (ret != chunk_size)
162                 return AVERROR(EIO);
163             pkt->stream_index = roq->video_stream_index;
164             pkt->pts = roq->video_pts;
165
166             roq->video_pts += roq->frame_pts_inc;
167             packet_read = 1;
168             break;
169
170         case RoQ_SOUND_MONO:
171         case RoQ_SOUND_STEREO:
172             if (roq->audio_stream_index == -1) {
173                 AVStream *st = av_new_stream(s, 1);
174                 if (!st)
175                     return AVERROR(ENOMEM);
176                 av_set_pts_info(st, 33, 1, 90000);
177                 roq->audio_stream_index = st->index;
178                 st->codec->codec_type = CODEC_TYPE_AUDIO;
179                 st->codec->codec_id = CODEC_ID_ROQ_DPCM;
180                 st->codec->codec_tag = 0;  /* no tag */
181                 st->codec->channels = roq->audio_channels = chunk_type == RoQ_SOUND_STEREO ? 2 : 1;
182                 st->codec->sample_rate = RoQ_AUDIO_SAMPLE_RATE;
183                 st->codec->bits_per_coded_sample = 16;
184                 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
185                     st->codec->bits_per_coded_sample;
186                 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
187             }
188         case RoQ_QUAD_VQ:
189             /* load up the packet */
190             if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
191                 return AVERROR(EIO);
192             /* copy over preamble */
193             memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
194
195             if (chunk_type == RoQ_QUAD_VQ) {
196                 pkt->stream_index = roq->video_stream_index;
197                 pkt->pts = roq->video_pts;
198                 roq->video_pts += roq->frame_pts_inc;
199             } else {
200                 pkt->stream_index = roq->audio_stream_index;
201                 pkt->pts = roq->audio_frame_count;
202                 pkt->pts *= 90000;
203                 pkt->pts /= RoQ_AUDIO_SAMPLE_RATE;
204                 roq->audio_frame_count += (chunk_size / roq->audio_channels);
205             }
206
207             pkt->pos= url_ftell(pb);
208             ret = get_buffer(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
209                 chunk_size);
210             if (ret != chunk_size)
211                 ret = AVERROR(EIO);
212
213             packet_read = 1;
214             break;
215
216         default:
217             av_log(s, AV_LOG_ERROR, "  unknown RoQ chunk (%04X)\n", chunk_type);
218             return AVERROR_INVALIDDATA;
219             break;
220         }
221     }
222
223     return ret;
224 }
225
226 AVInputFormat roq_demuxer = {
227     "RoQ",
228     NULL_IF_CONFIG_SMALL("id RoQ format"),
229     sizeof(RoqDemuxContext),
230     roq_probe,
231     roq_read_header,
232     roq_read_packet,
233 };