idroqdec.c 8.03 KB
Newer Older
1
/*
2
 * id RoQ (.roq) File Demuxer
3 4
 * Copyright (c) 2003 The ffmpeg Project
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg 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
 * FFmpeg 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 FFmpeg; 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
#include "avio_internal.h"
35 36 37 38 39 40 41 42 43 44 45 46 47 48

#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 {

49
    int frame_rate;
50 51 52 53 54 55 56 57 58 59 60 61 62 63
    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)
{
64 65
    if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
        (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
66 67 68 69 70
        return 0;

    return AVPROBE_SCORE_MAX;
}

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

    /* get the main header */
78
    if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
79
        RoQ_CHUNK_PREAMBLE_SIZE)
80
        return AVERROR(EIO);
81
    roq->frame_rate = AV_RL16(&preamble[6]);
82 83

    /* init private context parameters */
84
    roq->width = roq->height = roq->audio_channels = roq->video_pts =
85
    roq->audio_frame_count = 0;
86
    roq->audio_stream_index = -1;
87
    roq->video_stream_index = -1;
88

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

    return 0;
}

static int roq_read_packet(AVFormatContext *s,
                           AVPacket *pkt)
{
    RoqDemuxContext *roq = s->priv_data;
98
    AVIOContext *pb = s->pb;
99 100 101 102 103 104
    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;
105
    int64_t codebook_offset;
106 107 108

    while (!packet_read) {

109
        if (url_feof(s->pb))
110
            return AVERROR(EIO);
111 112

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

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

122 123
        chunk_size = ffio_limit(pb, chunk_size);

124 125 126
        switch (chunk_type) {

        case RoQ_INFO:
127 128 129 130 131 132 133
            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;
134
                st->codec->codec_id     = AV_CODEC_ID_ROQ;
135 136
                st->codec->codec_tag    = 0;  /* no fourcc */

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

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

            /* rewind */
161
            avio_seek(pb, codebook_offset, SEEK_SET);
162 163

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

            packet_read = 1;
            break;

        case RoQ_SOUND_MONO:
        case RoQ_SOUND_STEREO:
175
            if (roq->audio_stream_index == -1) {
176
                AVStream *st = avformat_new_stream(s, NULL);
177 178
                if (!st)
                    return AVERROR(ENOMEM);
179
                avpriv_set_pts_info(st, 32, 1, RoQ_AUDIO_SAMPLE_RATE);
180
                roq->audio_stream_index = st->index;
181
                st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
182
                st->codec->codec_id = AV_CODEC_ID_ROQ_DPCM;
183
                st->codec->codec_tag = 0;  /* no tag */
184 185 186 187 188 189 190 191
                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;
192 193 194 195 196 197
                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;
            }
198
        case RoQ_QUAD_VQ:
199 200 201 202 203
            if (chunk_type == RoQ_QUAD_VQ) {
                if (roq->video_stream_index < 0)
                    return AVERROR_INVALIDDATA;
            }

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

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

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

            packet_read = 1;
            break;

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

    return ret;
}

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