Move #defines that are mostly used in h264.c out of h264data.h and into h264.h.
[FFMpeg-mirror/ffmpeg-vdpau.git] / libavformat / idroq.c
blobacf2db6c1cf73d4fcce67647451a2db353b109b4
1 /*
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
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/
30 #include "avformat.h"
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 {
45 int width;
46 int height;
47 int audio_channels;
48 int framerate;
49 int frame_pts_inc;
51 int video_stream_index;
52 int audio_stream_index;
54 int64_t video_pts;
55 unsigned int audio_frame_count;
57 } RoqDemuxContext;
59 static int roq_probe(AVProbeData *p)
61 if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
62 (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
63 return 0;
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;
73 AVStream *st;
74 unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
75 int i;
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)
82 return AVERROR(EIO);
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)
94 return AVERROR(EIO);
96 chunk_type = AV_RL16(&preamble[0]);
97 chunk_size = AV_RL32(&preamble[2]);
99 switch (chunk_type) {
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(EIO);
106 roq->width = AV_RL16(&preamble[0]);
107 roq->height = AV_RL16(&preamble[2]);
108 break;
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;
116 case RoQ_SOUND_MONO:
117 roq->audio_channels = 1;
118 url_fseek(pb, chunk_size, SEEK_CUR);
119 break;
121 case RoQ_SOUND_STEREO:
122 roq->audio_channels = 2;
123 url_fseek(pb, chunk_size, SEEK_CUR);
124 break;
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;
132 /* if all necessary parameters have been gathered, exit early */
133 if ((roq->width && roq->height) && roq->audio_channels)
134 break;
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);
142 if (!st)
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);
155 if (!st)
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_coded_sample = 16;
165 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
166 st->codec->bits_per_coded_sample;
167 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
170 return 0;
173 static int roq_read_packet(AVFormatContext *s,
174 AVPacket *pkt)
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 int64_t codebook_offset;
186 while (!packet_read) {
188 if (url_feof(s->pb))
189 return AVERROR(EIO);
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(EIO);
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) {
203 case RoQ_INFO:
204 /* don't care about this chunk anymore */
205 url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_CUR);
206 break;
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(EIO);
216 chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
217 codebook_size;
219 /* rewind */
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)
225 return AVERROR(EIO);
226 pkt->stream_index = roq->video_stream_index;
227 pkt->pts = roq->video_pts;
229 roq->video_pts += roq->frame_pts_inc;
230 packet_read = 1;
231 break;
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(EIO);
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;
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);
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(EIO);
260 packet_read = 1;
261 break;
263 default:
264 av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type);
265 return AVERROR_INVALIDDATA;
266 break;
270 return ret;
273 AVInputFormat roq_demuxer = {
274 "RoQ",
275 NULL_IF_CONFIG_SMALL("id RoQ format"),
276 sizeof(RoqDemuxContext),
277 roq_probe,
278 roq_read_header,
279 roq_read_packet,