]> git.sesse.net Git - ffmpeg/blob - libavformat/idroqdec.c
pcmdec: remove dependency from rawdec
[ffmpeg] / libavformat / idroqdec.c
1 /*
2  * id RoQ (.roq) File Demuxer
3  * Copyright (c) 2003 The ffmpeg Project
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
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 #include "internal.h"
33
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
38
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
44
45 typedef struct RoqDemuxContext {
46
47     int frame_rate;
48     int width;
49     int height;
50     int audio_channels;
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 {
71     RoqDemuxContext *roq = s->priv_data;
72     AVIOContext *pb = s->pb;
73     unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
74
75     /* get the main header */
76     if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
77         RoQ_CHUNK_PREAMBLE_SIZE)
78         return AVERROR(EIO);
79     roq->frame_rate = AV_RL16(&preamble[6]);
80
81     /* init private context parameters */
82     roq->width = roq->height = roq->audio_channels = roq->video_pts =
83     roq->audio_frame_count = 0;
84     roq->audio_stream_index = -1;
85     roq->video_stream_index = -1;
86
87     s->ctx_flags |= AVFMTCTX_NOHEADER;
88
89     return 0;
90 }
91
92 static int roq_read_packet(AVFormatContext *s,
93                            AVPacket *pkt)
94 {
95     RoqDemuxContext *roq = s->priv_data;
96     AVIOContext *pb = s->pb;
97     int ret = 0;
98     unsigned int chunk_size;
99     unsigned int chunk_type;
100     unsigned int codebook_size;
101     unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
102     int packet_read = 0;
103     int64_t codebook_offset;
104
105     while (!packet_read) {
106
107         if (s->pb->eof_reached)
108             return AVERROR(EIO);
109
110         /* get the next chunk preamble */
111         if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
112             RoQ_CHUNK_PREAMBLE_SIZE)
113             return AVERROR(EIO);
114
115         chunk_type = AV_RL16(&preamble[0]);
116         chunk_size = AV_RL32(&preamble[2]);
117         if(chunk_size > INT_MAX)
118             return AVERROR_INVALIDDATA;
119
120         switch (chunk_type) {
121
122         case RoQ_INFO:
123             if (roq->video_stream_index == -1) {
124                 AVStream *st = avformat_new_stream(s, NULL);
125                 if (!st)
126                     return AVERROR(ENOMEM);
127                 avpriv_set_pts_info(st, 63, 1, roq->frame_rate);
128                 roq->video_stream_index = st->index;
129                 st->codec->codec_type   = AVMEDIA_TYPE_VIDEO;
130                 st->codec->codec_id     = AV_CODEC_ID_ROQ;
131                 st->codec->codec_tag    = 0;  /* no fourcc */
132
133                 if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != RoQ_CHUNK_PREAMBLE_SIZE)
134                     return AVERROR(EIO);
135                 st->codec->width  = roq->width  = AV_RL16(preamble);
136                 st->codec->height = roq->height = AV_RL16(preamble + 2);
137                 break;
138             }
139             /* don't care about this chunk anymore */
140             avio_skip(pb, RoQ_CHUNK_PREAMBLE_SIZE);
141             break;
142
143         case RoQ_QUAD_CODEBOOK:
144             /* packet needs to contain both this codebook and next VQ chunk */
145             codebook_offset = avio_tell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
146             codebook_size = chunk_size;
147             avio_skip(pb, codebook_size);
148             if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
149                 RoQ_CHUNK_PREAMBLE_SIZE)
150                 return AVERROR(EIO);
151             chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
152                 codebook_size;
153
154             /* rewind */
155             avio_seek(pb, codebook_offset, SEEK_SET);
156
157             /* load up the packet */
158             ret= av_get_packet(pb, pkt, chunk_size);
159             if (ret != chunk_size)
160                 return AVERROR(EIO);
161             pkt->stream_index = roq->video_stream_index;
162             pkt->pts = roq->video_pts++;
163
164             packet_read = 1;
165             break;
166
167         case RoQ_SOUND_MONO:
168         case RoQ_SOUND_STEREO:
169             if (roq->audio_stream_index == -1) {
170                 AVStream *st = avformat_new_stream(s, NULL);
171                 if (!st)
172                     return AVERROR(ENOMEM);
173                 avpriv_set_pts_info(st, 32, 1, RoQ_AUDIO_SAMPLE_RATE);
174                 roq->audio_stream_index = st->index;
175                 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
176                 st->codec->codec_id = AV_CODEC_ID_ROQ_DPCM;
177                 st->codec->codec_tag = 0;  /* no tag */
178                 st->codec->channels = roq->audio_channels = chunk_type == RoQ_SOUND_STEREO ? 2 : 1;
179                 st->codec->sample_rate = RoQ_AUDIO_SAMPLE_RATE;
180                 st->codec->bits_per_coded_sample = 16;
181                 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
182                     st->codec->bits_per_coded_sample;
183                 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
184             }
185         case RoQ_QUAD_VQ:
186             /* load up the packet */
187             if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
188                 return AVERROR(EIO);
189             /* copy over preamble */
190             memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
191
192             if (chunk_type == RoQ_QUAD_VQ) {
193                 pkt->stream_index = roq->video_stream_index;
194                 pkt->pts = roq->video_pts++;
195             } else {
196                 pkt->stream_index = roq->audio_stream_index;
197                 pkt->pts = roq->audio_frame_count;
198                 roq->audio_frame_count += (chunk_size / roq->audio_channels);
199             }
200
201             pkt->pos= avio_tell(pb);
202             ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
203                 chunk_size);
204             if (ret != chunk_size)
205                 ret = AVERROR(EIO);
206
207             packet_read = 1;
208             break;
209
210         default:
211             av_log(s, AV_LOG_ERROR, "  unknown RoQ chunk (%04X)\n", chunk_type);
212             return AVERROR_INVALIDDATA;
213         }
214     }
215
216     return ret;
217 }
218
219 AVInputFormat ff_roq_demuxer = {
220     .name           = "roq",
221     .long_name      = NULL_IF_CONFIG_SMALL("id RoQ"),
222     .priv_data_size = sizeof(RoqDemuxContext),
223     .read_probe     = roq_probe,
224     .read_header    = roq_read_header,
225     .read_packet    = roq_read_packet,
226 };