Statistics
| Branch: | Revision:

ffmpeg / libavformat / idroq.c @ 7be806f3

History | View | Annotate | Download (8.78 KB)

1 3ef8be2b Mike Melanson
/*
2
 * Id RoQ (.roq) File Demuxer
3
 * Copyright (c) 2003 The ffmpeg Project
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
20
/**
21
 * @file idroq.c
22
 * Id RoQ format file demuxer
23
 * by Mike Melanson (melanson@pcisys.net)
24
 * for more information on the .roq file format, visit:
25
 *   http://www.csse.monash.edu.au/~timf/
26
 */
27
28
#include "avformat.h"
29
30
#define RoQ_MAGIC_NUMBER 0x1084
31
#define RoQ_CHUNK_PREAMBLE_SIZE 8
32
#define RoQ_AUDIO_SAMPLE_RATE 22050
33
#define RoQ_CHUNKS_TO_SCAN 30
34
35
#define RoQ_INFO           0x1001
36
#define RoQ_QUAD_CODEBOOK  0x1002
37
#define RoQ_QUAD_VQ        0x1011
38
#define RoQ_SOUND_MONO     0x1020
39
#define RoQ_SOUND_STEREO   0x1021
40
41
typedef struct RoqDemuxContext {
42
43
    int width;
44
    int height;
45
    int audio_channels;
46
    int framerate;
47
    int frame_pts_inc;
48
49
    int video_stream_index;
50
    int audio_stream_index;
51
52
    int64_t video_pts;
53
    unsigned int audio_frame_count;
54
55
} RoqDemuxContext;
56
57
static int roq_probe(AVProbeData *p)
58
{
59 e7106d5b Mike Melanson
    if (p->buf_size < 6)
60
        return 0;
61
62 3ef8be2b Mike Melanson
    if ((LE_16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
63
        (LE_32(&p->buf[2]) != 0xFFFFFFFF))
64
        return 0;
65
66
    return AVPROBE_SCORE_MAX;
67
}
68
69
static int roq_read_header(AVFormatContext *s,
70
                           AVFormatParameters *ap)
71
{
72
    RoqDemuxContext *roq = s->priv_data;
73
    ByteIOContext *pb = &s->pb;
74
    AVStream *st;
75
    unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
76
    int i;
77
    unsigned int chunk_size;
78
    unsigned int chunk_type;
79
80
    /* get the main header */
81
    if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != 
82
        RoQ_CHUNK_PREAMBLE_SIZE)
83
        return AVERROR_IO;
84
    roq->framerate = LE_16(&preamble[6]);
85
    roq->frame_pts_inc = 90000 / roq->framerate;
86
87
    /* init private context parameters */
88
    roq->width = roq->height = roq->audio_channels = roq->video_pts = 
89
    roq->audio_frame_count = 0;
90
91
    /* scan the first n chunks searching for A/V parameters */
92
    for (i = 0; i < RoQ_CHUNKS_TO_SCAN; i++) {
93
        if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != 
94
            RoQ_CHUNK_PREAMBLE_SIZE)
95
            return AVERROR_IO;
96
97
        chunk_type = LE_16(&preamble[0]);
98
        chunk_size = LE_32(&preamble[2]);
99
100
        switch (chunk_type) {
101
102
        case RoQ_INFO:
103
            /* fetch the width and height; reuse the preamble bytes */
104
            if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != 
105
                RoQ_CHUNK_PREAMBLE_SIZE)
106
                return AVERROR_IO;
107
            roq->width = LE_16(&preamble[0]);
108
            roq->height = LE_16(&preamble[2]);
109
            break;
110
111
        case RoQ_QUAD_CODEBOOK:
112
        case RoQ_QUAD_VQ:
113
            /* ignore during this scan */
114
            url_fseek(pb, chunk_size, SEEK_CUR);
115
            break;
116
117
        case RoQ_SOUND_MONO:
118
            roq->audio_channels = 1;
119
            url_fseek(pb, chunk_size, SEEK_CUR);
120
            break;
121
122
        case RoQ_SOUND_STEREO:
123
            roq->audio_channels = 2;
124
            url_fseek(pb, chunk_size, SEEK_CUR);
125
            break;
126
127
        default:
128 bc874dae Michel Bardiaux
            av_log(s, AV_LOG_ERROR, " unknown RoQ chunk type (%04X)\n", LE_16(&preamble[0]));
129 3ef8be2b Mike Melanson
            return AVERROR_INVALIDDATA;
130
            break;
131
        }
132
133
        /* if all necessary parameters have been gathered, exit early */
134
        if ((roq->width && roq->height) && roq->audio_channels)
135
            break;
136
    }
137
138
    /* seek back to the first chunk */
139
    url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_SET);
140
141
    /* initialize the decoders */
142
    st = av_new_stream(s, 0);
143
    if (!st)
144
        return AVERROR_NOMEM;
145 9ee91c2f Michael Niedermayer
    /* set the pts reference (1 pts = 1/90000) */
146
    av_set_pts_info(st, 33, 1, 90000);
147 3ef8be2b Mike Melanson
    roq->video_stream_index = st->index;
148
    st->codec.codec_type = CODEC_TYPE_VIDEO;
149
    st->codec.codec_id = CODEC_ID_ROQ;
150
    st->codec.codec_tag = 0;  /* no fourcc */
151
    st->codec.width = roq->width;
152
    st->codec.height = roq->height;
153
154
    if (roq->audio_channels) {
155
        st = av_new_stream(s, 0);
156
        if (!st)
157
            return AVERROR_NOMEM;
158 9ee91c2f Michael Niedermayer
        av_set_pts_info(st, 33, 1, 90000);
159 3ef8be2b Mike Melanson
        roq->audio_stream_index = st->index;
160
        st->codec.codec_type = CODEC_TYPE_AUDIO;
161
        st->codec.codec_id = CODEC_ID_ROQ_DPCM;
162
        st->codec.codec_tag = 0;  /* no tag */
163
        st->codec.channels = roq->audio_channels;
164
        st->codec.sample_rate = RoQ_AUDIO_SAMPLE_RATE;
165
        st->codec.bits_per_sample = 16;
166
        st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
167
            st->codec.bits_per_sample;
168
        st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
169
    }
170
171
    return 0;
172
}
173
174
static int roq_read_packet(AVFormatContext *s,
175
                           AVPacket *pkt)
176
{
177
    RoqDemuxContext *roq = s->priv_data;
178
    ByteIOContext *pb = &s->pb;
179
    int ret = 0;
180
    unsigned int chunk_size;
181
    unsigned int chunk_type;
182
    unsigned int codebook_size;
183
    unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
184
    int packet_read = 0;
185
    offset_t codebook_offset;
186
187
    while (!packet_read) {
188
189
        if (url_feof(&s->pb))
190 0bd586c5 Mike Melanson
            return AVERROR_IO;
191 3ef8be2b Mike Melanson
192
        /* get the next chunk preamble */
193
        if ((ret = get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) != 
194
            RoQ_CHUNK_PREAMBLE_SIZE)
195 0bd586c5 Mike Melanson
            return AVERROR_IO;
196 3ef8be2b Mike Melanson
197
        chunk_type = LE_16(&preamble[0]);
198
        chunk_size = LE_32(&preamble[2]);
199 0ecca7a4 Michael Niedermayer
        if(chunk_size > INT_MAX)
200
            return AVERROR_INVALIDDATA;
201 3ef8be2b Mike Melanson
202
        switch (chunk_type) {
203
204
        case RoQ_INFO:
205
            /* don't care about this chunk anymore */
206
            url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_CUR);
207
            break;
208
209
        case RoQ_QUAD_CODEBOOK:
210
            /* packet needs to contain both this codebook and next VQ chunk */
211
            codebook_offset = url_ftell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
212
            codebook_size = chunk_size;
213
            url_fseek(pb, codebook_size, SEEK_CUR);
214
            if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != 
215
                RoQ_CHUNK_PREAMBLE_SIZE)
216 0bd586c5 Mike Melanson
                return AVERROR_IO;
217 3ef8be2b Mike Melanson
            chunk_size = LE_32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 + 
218
                codebook_size;
219
220
            /* rewind */
221
            url_fseek(pb, codebook_offset, SEEK_SET);
222
223
            /* load up the packet */
224
            if (av_new_packet(pkt, chunk_size))
225 0bd586c5 Mike Melanson
                return AVERROR_IO;
226 3ef8be2b Mike Melanson
            pkt->stream_index = roq->video_stream_index;
227
            pkt->pts = roq->video_pts;
228
            ret = get_buffer(pb, pkt->data, chunk_size);
229
            if (ret != chunk_size)
230 0bd586c5 Mike Melanson
                ret = AVERROR_IO;
231 3ef8be2b Mike Melanson
232
            roq->video_pts += roq->frame_pts_inc;
233
            packet_read = 1;
234
            break;
235
236
        case RoQ_SOUND_MONO:
237
        case RoQ_SOUND_STEREO:
238
        case RoQ_QUAD_VQ:
239
            /* load up the packet */
240
            if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
241 0bd586c5 Mike Melanson
                return AVERROR_IO;
242 3ef8be2b Mike Melanson
            /* copy over preamble */
243
            memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
244
245
            if (chunk_type == RoQ_QUAD_VQ) {
246
                pkt->stream_index = roq->video_stream_index;
247
                pkt->pts = roq->video_pts;
248
                roq->video_pts += roq->frame_pts_inc;
249
            } else {
250
                pkt->stream_index = roq->audio_stream_index;
251
                pkt->pts = roq->audio_frame_count;
252
                pkt->pts *= 90000;
253
                pkt->pts /= RoQ_AUDIO_SAMPLE_RATE;
254
                roq->audio_frame_count += (chunk_size / roq->audio_channels);
255
            }
256
257 d42f74b8 Mike Melanson
            ret = get_buffer(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
258
                chunk_size);
259 3ef8be2b Mike Melanson
            if (ret != chunk_size)
260 0bd586c5 Mike Melanson
                ret = AVERROR_IO;
261 3ef8be2b Mike Melanson
262
            packet_read = 1;
263
            break;
264
265
        default:
266 bc874dae Michel Bardiaux
            av_log(s, AV_LOG_ERROR, "  unknown RoQ chunk (%04X)\n", chunk_type);
267 3ef8be2b Mike Melanson
            return AVERROR_INVALIDDATA;
268
            break;
269
        }
270
    }
271
272
    return ret;
273
}
274
275
static int roq_read_close(AVFormatContext *s)
276
{
277
//    RoqDemuxContext *roq = (RoqDemuxContext *)s->priv_data;
278
279
    return 0;
280
}
281
282
static AVInputFormat roq_iformat = {
283
    "RoQ",
284
    "Id RoQ format",
285
    sizeof(RoqDemuxContext),
286
    roq_probe,
287
    roq_read_header,
288
    roq_read_packet,
289
    roq_read_close,
290
};
291
292
int roq_init(void)
293
{
294
    av_register_input_format(&roq_iformat);
295
    return 0;
296
}