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
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/
30 #include "libavutil/intreadwrite.h"
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
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
44 typedef struct RoqDemuxContext
{
50 int video_stream_index
;
51 int audio_stream_index
;
54 unsigned int audio_frame_count
;
58 static int roq_probe(AVProbeData
*p
)
60 if ((AV_RL16(&p
->buf
[0]) != RoQ_MAGIC_NUMBER
) ||
61 (AV_RL32(&p
->buf
[2]) != 0xFFFFFFFF))
64 return AVPROBE_SCORE_MAX
;
67 static int roq_read_header(AVFormatContext
*s
,
68 AVFormatParameters
*ap
)
70 RoqDemuxContext
*roq
= s
->priv_data
;
71 ByteIOContext
*pb
= s
->pb
;
74 unsigned char preamble
[RoQ_CHUNK_PREAMBLE_SIZE
];
76 /* get the main header */
77 if (get_buffer(pb
, preamble
, RoQ_CHUNK_PREAMBLE_SIZE
) !=
78 RoQ_CHUNK_PREAMBLE_SIZE
)
80 framerate
= 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;
87 st
= av_new_stream(s
, 0);
89 return AVERROR(ENOMEM
);
90 av_set_pts_info(st
, 63, 1, framerate
);
91 roq
->video_stream_index
= st
->index
;
92 st
->codec
->codec_type
= CODEC_TYPE_VIDEO
;
93 st
->codec
->codec_id
= CODEC_ID_ROQ
;
94 st
->codec
->codec_tag
= 0; /* no fourcc */
99 static int roq_read_packet(AVFormatContext
*s
,
102 RoqDemuxContext
*roq
= s
->priv_data
;
103 ByteIOContext
*pb
= s
->pb
;
105 unsigned int chunk_size
;
106 unsigned int chunk_type
;
107 unsigned int codebook_size
;
108 unsigned char preamble
[RoQ_CHUNK_PREAMBLE_SIZE
];
110 int64_t codebook_offset
;
112 while (!packet_read
) {
117 /* get the next chunk preamble */
118 if ((ret
= get_buffer(pb
, preamble
, RoQ_CHUNK_PREAMBLE_SIZE
)) !=
119 RoQ_CHUNK_PREAMBLE_SIZE
)
122 chunk_type
= AV_RL16(&preamble
[0]);
123 chunk_size
= AV_RL32(&preamble
[2]);
124 if(chunk_size
> INT_MAX
)
125 return AVERROR_INVALIDDATA
;
127 switch (chunk_type
) {
130 if (!roq
->width
|| !roq
->height
) {
131 AVStream
*st
= s
->streams
[roq
->video_stream_index
];
132 if (get_buffer(pb
, preamble
, RoQ_CHUNK_PREAMBLE_SIZE
) != RoQ_CHUNK_PREAMBLE_SIZE
)
134 st
->codec
->width
= roq
->width
= AV_RL16(preamble
);
135 st
->codec
->height
= roq
->height
= AV_RL16(preamble
+ 2);
138 /* don't care about this chunk anymore */
139 url_fseek(pb
, RoQ_CHUNK_PREAMBLE_SIZE
, SEEK_CUR
);
142 case RoQ_QUAD_CODEBOOK
:
143 /* packet needs to contain both this codebook and next VQ chunk */
144 codebook_offset
= url_ftell(pb
) - RoQ_CHUNK_PREAMBLE_SIZE
;
145 codebook_size
= chunk_size
;
146 url_fseek(pb
, codebook_size
, SEEK_CUR
);
147 if (get_buffer(pb
, preamble
, RoQ_CHUNK_PREAMBLE_SIZE
) !=
148 RoQ_CHUNK_PREAMBLE_SIZE
)
150 chunk_size
= AV_RL32(&preamble
[2]) + RoQ_CHUNK_PREAMBLE_SIZE
* 2 +
154 url_fseek(pb
, codebook_offset
, SEEK_SET
);
156 /* load up the packet */
157 ret
= av_get_packet(pb
, pkt
, chunk_size
);
158 if (ret
!= chunk_size
)
160 pkt
->stream_index
= roq
->video_stream_index
;
161 pkt
->pts
= roq
->video_pts
++;
167 case RoQ_SOUND_STEREO
:
168 if (roq
->audio_stream_index
== -1) {
169 AVStream
*st
= av_new_stream(s
, 1);
171 return AVERROR(ENOMEM
);
172 av_set_pts_info(st
, 32, 1, RoQ_AUDIO_SAMPLE_RATE
);
173 roq
->audio_stream_index
= st
->index
;
174 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
175 st
->codec
->codec_id
= CODEC_ID_ROQ_DPCM
;
176 st
->codec
->codec_tag
= 0; /* no tag */
177 st
->codec
->channels
= roq
->audio_channels
= chunk_type
== RoQ_SOUND_STEREO
? 2 : 1;
178 st
->codec
->sample_rate
= RoQ_AUDIO_SAMPLE_RATE
;
179 st
->codec
->bits_per_coded_sample
= 16;
180 st
->codec
->bit_rate
= st
->codec
->channels
* st
->codec
->sample_rate
*
181 st
->codec
->bits_per_coded_sample
;
182 st
->codec
->block_align
= st
->codec
->channels
* st
->codec
->bits_per_coded_sample
;
185 /* load up the packet */
186 if (av_new_packet(pkt
, chunk_size
+ RoQ_CHUNK_PREAMBLE_SIZE
))
188 /* copy over preamble */
189 memcpy(pkt
->data
, preamble
, RoQ_CHUNK_PREAMBLE_SIZE
);
191 if (chunk_type
== RoQ_QUAD_VQ
) {
192 pkt
->stream_index
= roq
->video_stream_index
;
193 pkt
->pts
= roq
->video_pts
++;
195 pkt
->stream_index
= roq
->audio_stream_index
;
196 pkt
->pts
= roq
->audio_frame_count
;
197 roq
->audio_frame_count
+= (chunk_size
/ roq
->audio_channels
);
200 pkt
->pos
= url_ftell(pb
);
201 ret
= get_buffer(pb
, pkt
->data
+ RoQ_CHUNK_PREAMBLE_SIZE
,
203 if (ret
!= chunk_size
)
210 av_log(s
, AV_LOG_ERROR
, " unknown RoQ chunk (%04X)\n", chunk_type
);
211 return AVERROR_INVALIDDATA
;
219 AVInputFormat roq_demuxer
= {
221 NULL_IF_CONFIG_SMALL("id RoQ format"),
222 sizeof(RoqDemuxContext
),