idroqdec.c 7.95 KB
Newer Older
1
/*
2
 * id RoQ (.roq) File Demuxer
3 4
 * Copyright (c) 2003 The ffmpeg Project
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * 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 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 26 27 28 29
 * by Mike Melanson (melanson@pcisys.net)
 * for more information on the .roq file format, visit:
 *   http://www.csse.monash.edu.au/~timf/
 */

30
#include "libavutil/channel_layout.h"
31
#include "libavutil/intreadwrite.h"
32
#include "avformat.h"
33
#include "internal.h"
34 35 36 37 38 39 40 41 42 43 44 45 46 47

#define RoQ_MAGIC_NUMBER 0x1084
#define RoQ_CHUNK_PREAMBLE_SIZE 8
#define RoQ_AUDIO_SAMPLE_RATE 22050
#define RoQ_CHUNKS_TO_SCAN 30

#define RoQ_INFO           0x1001
#define RoQ_QUAD_CODEBOOK  0x1002
#define RoQ_QUAD_VQ        0x1011
#define RoQ_SOUND_MONO     0x1020
#define RoQ_SOUND_STEREO   0x1021

typedef struct RoqDemuxContext {

48
    int frame_rate;
49 50 51 52 53 54 55 56 57 58 59 60 61 62
    int width;
    int height;
    int audio_channels;

    int video_stream_index;
    int audio_stream_index;

    int64_t video_pts;
    unsigned int audio_frame_count;

} RoqDemuxContext;

static int roq_probe(AVProbeData *p)
{
63 64
    if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
        (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
65 66 67 68 69
        return 0;

    return AVPROBE_SCORE_MAX;
}

70
static int roq_read_header(AVFormatContext *s)
71 72
{
    RoqDemuxContext *roq = s->priv_data;
73
    AVIOContext *pb = s->pb;
74 75 76
    unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];

    /* get the main header */
77
    if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
78
        RoQ_CHUNK_PREAMBLE_SIZE)
79
        return AVERROR(EIO);
80
    roq->frame_rate = AV_RL16(&preamble[6]);
81 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;
86
    roq->video_stream_index = -1;
87

88
    s->ctx_flags |= AVFMTCTX_NOHEADER;
89 90 91 92 93 94 95 96

    return 0;
}

static int roq_read_packet(AVFormatContext *s,
                           AVPacket *pkt)
{
    RoqDemuxContext *roq = s->priv_data;
97
    AVIOContext *pb = s->pb;
98 99 100 101 102 103
    int ret = 0;
    unsigned int chunk_size;
    unsigned int chunk_type;
    unsigned int codebook_size;
    unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
    int packet_read = 0;
104
    int64_t codebook_offset;
105 106 107

    while (!packet_read) {

Anton Khirnov's avatar
Anton Khirnov committed
108
        if (s->pb->eof_reached)
109
            return AVERROR(EIO);
110 111

        /* get the next chunk preamble */
112
        if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
113
            RoQ_CHUNK_PREAMBLE_SIZE)
114
            return AVERROR(EIO);
115

116 117
        chunk_type = AV_RL16(&preamble[0]);
        chunk_size = AV_RL32(&preamble[2]);
118 119
        if(chunk_size > INT_MAX)
            return AVERROR_INVALIDDATA;
120 121 122 123

        switch (chunk_type) {

        case RoQ_INFO:
124 125 126 127 128 129 130
            if (roq->video_stream_index == -1) {
                AVStream *st = avformat_new_stream(s, NULL);
                if (!st)
                    return AVERROR(ENOMEM);
                avpriv_set_pts_info(st, 63, 1, roq->frame_rate);
                roq->video_stream_index = st->index;
                st->codec->codec_type   = AVMEDIA_TYPE_VIDEO;
131
                st->codec->codec_id     = AV_CODEC_ID_ROQ;
132 133
                st->codec->codec_tag    = 0;  /* no fourcc */

134
                if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != RoQ_CHUNK_PREAMBLE_SIZE)
135 136 137 138 139
                    return AVERROR(EIO);
                st->codec->width  = roq->width  = AV_RL16(preamble);
                st->codec->height = roq->height = AV_RL16(preamble + 2);
                break;
            }
140
            /* don't care about this chunk anymore */
141
            avio_skip(pb, RoQ_CHUNK_PREAMBLE_SIZE);
142 143 144
            break;

        case RoQ_QUAD_CODEBOOK:
145 146
            if (roq->video_stream_index < 0)
                return AVERROR_INVALIDDATA;
147
            /* packet needs to contain both this codebook and next VQ chunk */
148
            codebook_offset = avio_tell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
149
            codebook_size = chunk_size;
150
            avio_skip(pb, codebook_size);
151
            if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
152
                RoQ_CHUNK_PREAMBLE_SIZE)
153
                return AVERROR(EIO);
154
            chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
155 156 157
                codebook_size;

            /* rewind */
158
            avio_seek(pb, codebook_offset, SEEK_SET);
159 160

            /* load up the packet */
Michael Niedermayer's avatar
Michael Niedermayer committed
161 162
            ret= av_get_packet(pb, pkt, chunk_size);
            if (ret != chunk_size)
163
                return AVERROR(EIO);
164
            pkt->stream_index = roq->video_stream_index;
165
            pkt->pts = roq->video_pts++;
166 167 168 169 170 171

            packet_read = 1;
            break;

        case RoQ_SOUND_MONO:
        case RoQ_SOUND_STEREO:
172
            if (roq->audio_stream_index == -1) {
173
                AVStream *st = avformat_new_stream(s, NULL);
174 175
                if (!st)
                    return AVERROR(ENOMEM);
176
                avpriv_set_pts_info(st, 32, 1, RoQ_AUDIO_SAMPLE_RATE);
177
                roq->audio_stream_index = st->index;
178
                st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
179
                st->codec->codec_id = AV_CODEC_ID_ROQ_DPCM;
180
                st->codec->codec_tag = 0;  /* no tag */
181 182 183 184 185 186 187 188
                if (chunk_type == RoQ_SOUND_STEREO) {
                    st->codec->channels       = 2;
                    st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
                } else {
                    st->codec->channels       = 1;
                    st->codec->channel_layout = AV_CH_LAYOUT_MONO;
                }
                roq->audio_channels    = st->codec->channels;
189 190 191 192 193 194
                st->codec->sample_rate = RoQ_AUDIO_SAMPLE_RATE;
                st->codec->bits_per_coded_sample = 16;
                st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
                    st->codec->bits_per_coded_sample;
                st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
            }
195
        case RoQ_QUAD_VQ:
196 197 198 199 200
            if (chunk_type == RoQ_QUAD_VQ) {
                if (roq->video_stream_index < 0)
                    return AVERROR_INVALIDDATA;
            }

201 202
            /* load up the packet */
            if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
203
                return AVERROR(EIO);
204 205 206 207 208
            /* copy over preamble */
            memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);

            if (chunk_type == RoQ_QUAD_VQ) {
                pkt->stream_index = roq->video_stream_index;
209
                pkt->pts = roq->video_pts++;
210 211 212 213 214 215
            } else {
                pkt->stream_index = roq->audio_stream_index;
                pkt->pts = roq->audio_frame_count;
                roq->audio_frame_count += (chunk_size / roq->audio_channels);
            }

216
            pkt->pos= avio_tell(pb);
217
            ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
218
                chunk_size);
219
            if (ret != chunk_size)
220
                ret = AVERROR(EIO);
221 222 223 224 225

            packet_read = 1;
            break;

        default:
226
            av_log(s, AV_LOG_ERROR, "  unknown RoQ chunk (%04X)\n", chunk_type);
227 228 229 230 231 232 233
            return AVERROR_INVALIDDATA;
        }
    }

    return ret;
}

234
AVInputFormat ff_roq_demuxer = {
235
    .name           = "roq",
236
    .long_name      = NULL_IF_CONFIG_SMALL("id RoQ"),
237 238 239 240
    .priv_data_size = sizeof(RoqDemuxContext),
    .read_probe     = roq_probe,
    .read_header    = roq_read_header,
    .read_packet    = roq_read_packet,
241
};