2 * id RoQ (.roq) File Demuxer
3 * Copyright (c) 2003 The ffmpeg Project
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 "libavutil/intreadwrite.h"
34 #define RoQ_MAGIC_NUMBER 0x1084
35 #define RoQ_CHUNK_PREAMBLE_SIZE 8
36 #define RoQ_AUDIO_SAMPLE_RATE 22050
37 #define RoQ_CHUNKS_TO_SCAN 30
39 #define RoQ_INFO 0x1001
40 #define RoQ_QUAD_CODEBOOK 0x1002
41 #define RoQ_QUAD_VQ 0x1011
42 #define RoQ_SOUND_MONO 0x1020
43 #define RoQ_SOUND_STEREO 0x1021
45 typedef struct RoqDemuxContext {
52 int video_stream_index;
53 int audio_stream_index;
56 unsigned int audio_frame_count;
60 static int roq_probe(AVProbeData *p)
62 if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
63 (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
66 return AVPROBE_SCORE_MAX;
69 static int roq_read_header(AVFormatContext *s,
70 AVFormatParameters *ap)
72 RoqDemuxContext *roq = s->priv_data;
73 AVIOContext *pb = s->pb;
74 unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
76 /* get the main header */
77 if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
78 RoQ_CHUNK_PREAMBLE_SIZE)
80 roq->frame_rate = AV_RL16(&preamble[6]);
82 /* init private context parameters */
83 roq->width = roq->height = roq->audio_channels = roq->video_pts =
84 roq->audio_frame_count = 0;
85 roq->audio_stream_index = -1;
86 roq->video_stream_index = -1;
88 s->ctx_flags |= AVFMTCTX_NOHEADER;
93 static int roq_read_packet(AVFormatContext *s,
96 RoqDemuxContext *roq = s->priv_data;
97 AVIOContext *pb = s->pb;
99 unsigned int chunk_size;
100 unsigned int chunk_type;
101 unsigned int codebook_size;
102 unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
104 int64_t codebook_offset;
106 while (!packet_read) {
108 if (s->pb->eof_reached)
111 /* get the next chunk preamble */
112 if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
113 RoQ_CHUNK_PREAMBLE_SIZE)
116 chunk_type = AV_RL16(&preamble[0]);
117 chunk_size = AV_RL32(&preamble[2]);
118 if(chunk_size > INT_MAX)
119 return AVERROR_INVALIDDATA;
121 switch (chunk_type) {
124 if (roq->video_stream_index == -1) {
125 AVStream *st = avformat_new_stream(s, NULL);
127 return AVERROR(ENOMEM);
128 avpriv_set_pts_info(st, 63, 1, roq->frame_rate);
129 roq->video_stream_index = st->index;
130 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
131 st->codec->codec_id = CODEC_ID_ROQ;
132 st->codec->codec_tag = 0; /* no fourcc */
134 if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != RoQ_CHUNK_PREAMBLE_SIZE)
136 st->codec->width = roq->width = AV_RL16(preamble);
137 st->codec->height = roq->height = AV_RL16(preamble + 2);
140 /* don't care about this chunk anymore */
141 avio_skip(pb, RoQ_CHUNK_PREAMBLE_SIZE);
144 case RoQ_QUAD_CODEBOOK:
145 /* packet needs to contain both this codebook and next VQ chunk */
146 codebook_offset = avio_tell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
147 codebook_size = chunk_size;
148 avio_skip(pb, codebook_size);
149 if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
150 RoQ_CHUNK_PREAMBLE_SIZE)
152 chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
156 avio_seek(pb, codebook_offset, SEEK_SET);
158 /* load up the packet */
159 ret= av_get_packet(pb, pkt, chunk_size);
160 if (ret != chunk_size)
162 pkt->stream_index = roq->video_stream_index;
163 pkt->pts = roq->video_pts++;
169 case RoQ_SOUND_STEREO:
170 if (roq->audio_stream_index == -1) {
171 AVStream *st = avformat_new_stream(s, NULL);
173 return AVERROR(ENOMEM);
174 avpriv_set_pts_info(st, 32, 1, RoQ_AUDIO_SAMPLE_RATE);
175 roq->audio_stream_index = st->index;
176 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
177 st->codec->codec_id = CODEC_ID_ROQ_DPCM;
178 st->codec->codec_tag = 0; /* no tag */
179 st->codec->channels = roq->audio_channels = chunk_type == RoQ_SOUND_STEREO ? 2 : 1;
180 st->codec->sample_rate = RoQ_AUDIO_SAMPLE_RATE;
181 st->codec->bits_per_coded_sample = 16;
182 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
183 st->codec->bits_per_coded_sample;
184 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
187 /* load up the packet */
188 if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
190 /* copy over preamble */
191 memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
193 if (chunk_type == RoQ_QUAD_VQ) {
194 pkt->stream_index = roq->video_stream_index;
195 pkt->pts = roq->video_pts++;
197 pkt->stream_index = roq->audio_stream_index;
198 pkt->pts = roq->audio_frame_count;
199 roq->audio_frame_count += (chunk_size / roq->audio_channels);
202 pkt->pos= avio_tell(pb);
203 ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
205 if (ret != chunk_size)
212 av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type);
213 return AVERROR_INVALIDDATA;
220 AVInputFormat ff_roq_demuxer = {
222 .long_name = NULL_IF_CONFIG_SMALL("id RoQ format"),
223 .priv_data_size = sizeof(RoqDemuxContext),
224 .read_probe = roq_probe,
225 .read_header = roq_read_header,
226 .read_packet = roq_read_packet,