Statistics
| Branch: | Revision:

ffmpeg / libavformat / idroq.c @ b973f9ad

History | View | Annotate | Download (8.65 KB)

1 3ef8be2b Mike Melanson
/*
2
 * Id RoQ (.roq) File Demuxer
3
 * Copyright (c) 2003 The ffmpeg Project
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 3ef8be2b Mike Melanson
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 3ef8be2b Mike Melanson
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 3ef8be2b Mike Melanson
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 3ef8be2b Mike Melanson
 */
21
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/
28
 */
29
30
#include "avformat.h"
31
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
36
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
42
43
typedef struct RoqDemuxContext {
44
45
    int width;
46
    int height;
47
    int audio_channels;
48
    int framerate;
49
    int frame_pts_inc;
50
51
    int video_stream_index;
52
    int audio_stream_index;
53
54
    int64_t video_pts;
55
    unsigned int audio_frame_count;
56
57
} RoqDemuxContext;
58
59
static int roq_probe(AVProbeData *p)
60
{
61 fead30d4 Alex Beregszaszi
    if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
62
        (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
63 3ef8be2b Mike Melanson
        return 0;
64
65
    return AVPROBE_SCORE_MAX;
66
}
67
68
static int roq_read_header(AVFormatContext *s,
69
                           AVFormatParameters *ap)
70
{
71
    RoqDemuxContext *roq = s->priv_data;
72 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
73 3ef8be2b Mike Melanson
    AVStream *st;
74
    unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
75
    int i;
76
    unsigned int chunk_size;
77
    unsigned int chunk_type;
78
79
    /* get the main header */
80 115329f1 Diego Biurrun
    if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
81 3ef8be2b Mike Melanson
        RoQ_CHUNK_PREAMBLE_SIZE)
82 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
83 fead30d4 Alex Beregszaszi
    roq->framerate = AV_RL16(&preamble[6]);
84 3ef8be2b Mike Melanson
    roq->frame_pts_inc = 90000 / roq->framerate;
85
86
    /* init private context parameters */
87 115329f1 Diego Biurrun
    roq->width = roq->height = roq->audio_channels = roq->video_pts =
88 3ef8be2b Mike Melanson
    roq->audio_frame_count = 0;
89
90
    /* scan the first n chunks searching for A/V parameters */
91
    for (i = 0; i < RoQ_CHUNKS_TO_SCAN; i++) {
92 115329f1 Diego Biurrun
        if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
93 3ef8be2b Mike Melanson
            RoQ_CHUNK_PREAMBLE_SIZE)
94 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
95 3ef8be2b Mike Melanson
96 fead30d4 Alex Beregszaszi
        chunk_type = AV_RL16(&preamble[0]);
97
        chunk_size = AV_RL32(&preamble[2]);
98 3ef8be2b Mike Melanson
99
        switch (chunk_type) {
100
101
        case RoQ_INFO:
102
            /* fetch the width and height; reuse the preamble bytes */
103 115329f1 Diego Biurrun
            if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
104 3ef8be2b Mike Melanson
                RoQ_CHUNK_PREAMBLE_SIZE)
105 6f3e0b21 Panagiotis Issaris
                return AVERROR(EIO);
106 fead30d4 Alex Beregszaszi
            roq->width = AV_RL16(&preamble[0]);
107
            roq->height = AV_RL16(&preamble[2]);
108 3ef8be2b Mike Melanson
            break;
109
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;
115
116
        case RoQ_SOUND_MONO:
117
            roq->audio_channels = 1;
118
            url_fseek(pb, chunk_size, SEEK_CUR);
119
            break;
120
121
        case RoQ_SOUND_STEREO:
122
            roq->audio_channels = 2;
123
            url_fseek(pb, chunk_size, SEEK_CUR);
124
            break;
125
126
        default:
127 fead30d4 Alex Beregszaszi
            av_log(s, AV_LOG_ERROR, " unknown RoQ chunk type (%04X)\n", AV_RL16(&preamble[0]));
128 3ef8be2b Mike Melanson
            return AVERROR_INVALIDDATA;
129
            break;
130
        }
131
132
        /* if all necessary parameters have been gathered, exit early */
133
        if ((roq->width && roq->height) && roq->audio_channels)
134
            break;
135
    }
136
137
    /* seek back to the first chunk */
138
    url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_SET);
139
140
    /* initialize the decoders */
141
    st = av_new_stream(s, 0);
142
    if (!st)
143 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
144 9ee91c2f Michael Niedermayer
    /* set the pts reference (1 pts = 1/90000) */
145
    av_set_pts_info(st, 33, 1, 90000);
146 3ef8be2b Mike Melanson
    roq->video_stream_index = st->index;
147 01f4895c Michael Niedermayer
    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;
152 3ef8be2b Mike Melanson
153
    if (roq->audio_channels) {
154
        st = av_new_stream(s, 0);
155
        if (!st)
156 769e10f0 Panagiotis Issaris
            return AVERROR(ENOMEM);
157 9ee91c2f Michael Niedermayer
        av_set_pts_info(st, 33, 1, 90000);
158 3ef8be2b Mike Melanson
        roq->audio_stream_index = st->index;
159 01f4895c Michael Niedermayer
        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_sample = 16;
165
        st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
166
            st->codec->bits_per_sample;
167
        st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
168 3ef8be2b Mike Melanson
    }
169
170
    return 0;
171
}
172
173
static int roq_read_packet(AVFormatContext *s,
174
                           AVPacket *pkt)
175
{
176
    RoqDemuxContext *roq = s->priv_data;
177 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
178 3ef8be2b Mike Melanson
    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
    offset_t codebook_offset;
185
186
    while (!packet_read) {
187
188 899681cd Björn Axelsson
        if (url_feof(s->pb))
189 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
190 3ef8be2b Mike Melanson
191
        /* get the next chunk preamble */
192 115329f1 Diego Biurrun
        if ((ret = get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
193 3ef8be2b Mike Melanson
            RoQ_CHUNK_PREAMBLE_SIZE)
194 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
195 3ef8be2b Mike Melanson
196 fead30d4 Alex Beregszaszi
        chunk_type = AV_RL16(&preamble[0]);
197
        chunk_size = AV_RL32(&preamble[2]);
198 0ecca7a4 Michael Niedermayer
        if(chunk_size > INT_MAX)
199
            return AVERROR_INVALIDDATA;
200 3ef8be2b Mike Melanson
201
        switch (chunk_type) {
202
203
        case RoQ_INFO:
204
            /* don't care about this chunk anymore */
205
            url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_CUR);
206
            break;
207
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 115329f1 Diego Biurrun
            if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
214 3ef8be2b Mike Melanson
                RoQ_CHUNK_PREAMBLE_SIZE)
215 6f3e0b21 Panagiotis Issaris
                return AVERROR(EIO);
216 fead30d4 Alex Beregszaszi
            chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
217 3ef8be2b Mike Melanson
                codebook_size;
218
219
            /* rewind */
220
            url_fseek(pb, codebook_offset, SEEK_SET);
221
222
            /* load up the packet */
223 2692067a Michael Niedermayer
            ret= av_get_packet(pb, pkt, chunk_size);
224
            if (ret != chunk_size)
225 6f3e0b21 Panagiotis Issaris
                return AVERROR(EIO);
226 3ef8be2b Mike Melanson
            pkt->stream_index = roq->video_stream_index;
227
            pkt->pts = roq->video_pts;
228
229
            roq->video_pts += roq->frame_pts_inc;
230
            packet_read = 1;
231
            break;
232
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 6f3e0b21 Panagiotis Issaris
                return AVERROR(EIO);
239 3ef8be2b Mike Melanson
            /* copy over preamble */
240
            memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
241
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);
252
            }
253
254 2692067a Michael Niedermayer
            pkt->pos= url_ftell(pb);
255 d42f74b8 Mike Melanson
            ret = get_buffer(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
256
                chunk_size);
257 3ef8be2b Mike Melanson
            if (ret != chunk_size)
258 6f3e0b21 Panagiotis Issaris
                ret = AVERROR(EIO);
259 3ef8be2b Mike Melanson
260
            packet_read = 1;
261
            break;
262
263
        default:
264 bc874dae Michel Bardiaux
            av_log(s, AV_LOG_ERROR, "  unknown RoQ chunk (%04X)\n", chunk_type);
265 3ef8be2b Mike Melanson
            return AVERROR_INVALIDDATA;
266
            break;
267
        }
268
    }
269
270
    return ret;
271
}
272
273
static int roq_read_close(AVFormatContext *s)
274
{
275 e4141433 Nicholas Tung
//    RoqDemuxContext *roq = s->priv_data;
276 3ef8be2b Mike Melanson
277
    return 0;
278
}
279
280 ff70e601 Måns Rullgård
AVInputFormat roq_demuxer = {
281 3ef8be2b Mike Melanson
    "RoQ",
282
    "Id RoQ format",
283
    sizeof(RoqDemuxContext),
284
    roq_probe,
285
    roq_read_header,
286
    roq_read_packet,
287
    roq_read_close,
288
};