Statistics
| Branch: | Revision:

ffmpeg / libavformat / idroqdec.c @ 45a8a02a

History | View | Annotate | Download (7.19 KB)

1 3ef8be2b Mike Melanson
/*
2 ffa5ed24 Stefano Sabatini
 * id RoQ (.roq) File Demuxer
3 3ef8be2b Mike Melanson
 * 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 ba87f080 Diego Biurrun
 * @file
24 ffa5ed24 Stefano Sabatini
 * id RoQ format file demuxer
25 3ef8be2b Mike Melanson
 * 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 6a5d31ac Diego Biurrun
#include "libavutil/intreadwrite.h"
31 3ef8be2b Mike Melanson
#include "avformat.h"
32
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
37
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
43
44
typedef struct RoqDemuxContext {
45
46
    int width;
47
    int height;
48
    int audio_channels;
49
50
    int video_stream_index;
51
    int audio_stream_index;
52
53
    int64_t video_pts;
54
    unsigned int audio_frame_count;
55
56
} RoqDemuxContext;
57
58
static int roq_probe(AVProbeData *p)
59
{
60 fead30d4 Alex Beregszaszi
    if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
61
        (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
62 3ef8be2b Mike Melanson
        return 0;
63
64
    return AVPROBE_SCORE_MAX;
65
}
66
67
static int roq_read_header(AVFormatContext *s,
68
                           AVFormatParameters *ap)
69
{
70
    RoqDemuxContext *roq = s->priv_data;
71 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
72 abb785f1 Reimar Döffinger
    int framerate;
73 3ef8be2b Mike Melanson
    AVStream *st;
74
    unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
75
76
    /* get the main header */
77 b7effd4e Anton Khirnov
    if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
78 3ef8be2b Mike Melanson
        RoQ_CHUNK_PREAMBLE_SIZE)
79 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
80 abb785f1 Reimar Döffinger
    framerate = AV_RL16(&preamble[6]);
81 3ef8be2b Mike Melanson
82
    /* init private context parameters */
83 115329f1 Diego Biurrun
    roq->width = roq->height = roq->audio_channels = roq->video_pts =
84 3ef8be2b Mike Melanson
    roq->audio_frame_count = 0;
85 15969b55 Reimar Döffinger
    roq->audio_stream_index = -1;
86 3ef8be2b Mike Melanson
87
    st = av_new_stream(s, 0);
88
    if (!st)
89 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
90 abb785f1 Reimar Döffinger
    av_set_pts_info(st, 63, 1, framerate);
91 3ef8be2b Mike Melanson
    roq->video_stream_index = st->index;
92 72415b2a Stefano Sabatini
    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
93 01f4895c Michael Niedermayer
    st->codec->codec_id = CODEC_ID_ROQ;
94
    st->codec->codec_tag = 0;  /* no fourcc */
95 3ef8be2b Mike Melanson
96
    return 0;
97
}
98
99
static int roq_read_packet(AVFormatContext *s,
100
                           AVPacket *pkt)
101
{
102
    RoqDemuxContext *roq = s->priv_data;
103 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
104 3ef8be2b Mike Melanson
    int ret = 0;
105
    unsigned int chunk_size;
106
    unsigned int chunk_type;
107
    unsigned int codebook_size;
108
    unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
109
    int packet_read = 0;
110 bc5c918e Diego Biurrun
    int64_t codebook_offset;
111 3ef8be2b Mike Melanson
112
    while (!packet_read) {
113
114 66e5b1df Anton Khirnov
        if (s->pb->eof_reached)
115 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
116 3ef8be2b Mike Melanson
117
        /* get the next chunk preamble */
118 b7effd4e Anton Khirnov
        if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
119 3ef8be2b Mike Melanson
            RoQ_CHUNK_PREAMBLE_SIZE)
120 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
121 3ef8be2b Mike Melanson
122 fead30d4 Alex Beregszaszi
        chunk_type = AV_RL16(&preamble[0]);
123
        chunk_size = AV_RL32(&preamble[2]);
124 0ecca7a4 Michael Niedermayer
        if(chunk_size > INT_MAX)
125
            return AVERROR_INVALIDDATA;
126 3ef8be2b Mike Melanson
127
        switch (chunk_type) {
128
129
        case RoQ_INFO:
130 15969b55 Reimar Döffinger
            if (!roq->width || !roq->height) {
131
                AVStream *st = s->streams[roq->video_stream_index];
132 b7effd4e Anton Khirnov
                if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != RoQ_CHUNK_PREAMBLE_SIZE)
133 15969b55 Reimar Döffinger
                    return AVERROR(EIO);
134
                st->codec->width  = roq->width  = AV_RL16(preamble);
135
                st->codec->height = roq->height = AV_RL16(preamble + 2);
136
                break;
137
            }
138 3ef8be2b Mike Melanson
            /* don't care about this chunk anymore */
139 45a8a02a Anton Khirnov
            avio_skip(pb, RoQ_CHUNK_PREAMBLE_SIZE);
140 3ef8be2b Mike Melanson
            break;
141
142
        case RoQ_QUAD_CODEBOOK:
143
            /* packet needs to contain both this codebook and next VQ chunk */
144 a2704c97 Anton Khirnov
            codebook_offset = avio_tell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
145 3ef8be2b Mike Melanson
            codebook_size = chunk_size;
146 45a8a02a Anton Khirnov
            avio_skip(pb, codebook_size);
147 b7effd4e Anton Khirnov
            if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
148 3ef8be2b Mike Melanson
                RoQ_CHUNK_PREAMBLE_SIZE)
149 6f3e0b21 Panagiotis Issaris
                return AVERROR(EIO);
150 fead30d4 Alex Beregszaszi
            chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
151 3ef8be2b Mike Melanson
                codebook_size;
152
153
            /* rewind */
154 6b4aa5da Anton Khirnov
            avio_seek(pb, codebook_offset, SEEK_SET);
155 3ef8be2b Mike Melanson
156
            /* load up the packet */
157 2692067a Michael Niedermayer
            ret= av_get_packet(pb, pkt, chunk_size);
158
            if (ret != chunk_size)
159 6f3e0b21 Panagiotis Issaris
                return AVERROR(EIO);
160 3ef8be2b Mike Melanson
            pkt->stream_index = roq->video_stream_index;
161 abb785f1 Reimar Döffinger
            pkt->pts = roq->video_pts++;
162 3ef8be2b Mike Melanson
163
            packet_read = 1;
164
            break;
165
166
        case RoQ_SOUND_MONO:
167
        case RoQ_SOUND_STEREO:
168 15969b55 Reimar Döffinger
            if (roq->audio_stream_index == -1) {
169
                AVStream *st = av_new_stream(s, 1);
170
                if (!st)
171
                    return AVERROR(ENOMEM);
172 abb785f1 Reimar Döffinger
                av_set_pts_info(st, 32, 1, RoQ_AUDIO_SAMPLE_RATE);
173 15969b55 Reimar Döffinger
                roq->audio_stream_index = st->index;
174 72415b2a Stefano Sabatini
                st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
175 15969b55 Reimar Döffinger
                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;
183
            }
184 3ef8be2b Mike Melanson
        case RoQ_QUAD_VQ:
185
            /* load up the packet */
186
            if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
187 6f3e0b21 Panagiotis Issaris
                return AVERROR(EIO);
188 3ef8be2b Mike Melanson
            /* copy over preamble */
189
            memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
190
191
            if (chunk_type == RoQ_QUAD_VQ) {
192
                pkt->stream_index = roq->video_stream_index;
193 abb785f1 Reimar Döffinger
                pkt->pts = roq->video_pts++;
194 3ef8be2b Mike Melanson
            } else {
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);
198
            }
199
200 a2704c97 Anton Khirnov
            pkt->pos= avio_tell(pb);
201 b7effd4e Anton Khirnov
            ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
202 d42f74b8 Mike Melanson
                chunk_size);
203 3ef8be2b Mike Melanson
            if (ret != chunk_size)
204 6f3e0b21 Panagiotis Issaris
                ret = AVERROR(EIO);
205 3ef8be2b Mike Melanson
206
            packet_read = 1;
207
            break;
208
209
        default:
210 bc874dae Michel Bardiaux
            av_log(s, AV_LOG_ERROR, "  unknown RoQ chunk (%04X)\n", chunk_type);
211 3ef8be2b Mike Melanson
            return AVERROR_INVALIDDATA;
212
            break;
213
        }
214
    }
215
216
    return ret;
217
}
218
219 c6610a21 Diego Elio Pettenò
AVInputFormat ff_roq_demuxer = {
220 3ef8be2b Mike Melanson
    "RoQ",
221 ffa5ed24 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("id RoQ format"),
222 3ef8be2b Mike Melanson
    sizeof(RoqDemuxContext),
223
    roq_probe,
224
    roq_read_header,
225
    roq_read_packet,
226
};