idroqdec.c 8.23 KB
Newer Older
1
/*
2
 * id RoQ (.roq) File Demuxer
3
 * Copyright (c) 2003 The FFmpeg project
4
 *
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 (avio_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
            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;
133 134 135
                st->codecpar->codec_type   = AVMEDIA_TYPE_VIDEO;
                st->codecpar->codec_id     = AV_CODEC_ID_ROQ;
                st->codecpar->codec_tag    = 0;  /* no fourcc */
136

137
                if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != RoQ_CHUNK_PREAMBLE_SIZE)
138
                    return AVERROR(EIO);
139 140
                st->codecpar->width  = roq->width  = AV_RL16(preamble);
                st->codecpar->height = roq->height = AV_RL16(preamble + 2);
141 142
                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
                codebook_size;

160 161 162
            if (chunk_size > INT_MAX)
                return AVERROR_INVALIDDATA;

163
            /* rewind */
164
            avio_seek(pb, codebook_offset, SEEK_SET);
165 166

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

            packet_read = 1;
            break;

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

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

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

222
            pkt->pos= avio_tell(pb);
223
            ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
224
                chunk_size);
225 226
            if (ret != chunk_size) {
                av_packet_unref(pkt);
227
                ret = AVERROR(EIO);
228
            }
229 230 231 232 233

            packet_read = 1;
            break;

        default:
234
            av_log(s, AV_LOG_ERROR, "  unknown RoQ chunk (%04X)\n", chunk_type);
235 236 237 238 239 240 241
            return AVERROR_INVALIDDATA;
        }
    }

    return ret;
}

242
AVInputFormat ff_roq_demuxer = {
243
    .name           = "roq",
244
    .long_name      = NULL_IF_CONFIG_SMALL("id RoQ"),
245 246 247 248
    .priv_data_size = sizeof(RoqDemuxContext),
    .read_probe     = roq_probe,
    .read_header    = roq_read_header,
    .read_packet    = roq_read_packet,
249
};