wc3movie.c 9.05 KB
Newer Older
1 2 3 4
/*
 * Wing Commander III Movie (.mve) File Demuxer
 * 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 25 26 27 28 29
 * Wing Commander III Movie file demuxer
 * by Mike Melanson (melanson@pcisys.net)
 * for more information on the WC3 .mve file format, visit:
 *   http://www.pcisys.net/~melanson/codecs/
 */

30
#include "libavutil/intreadwrite.h"
31
#include "libavutil/dict.h"
32
#include "avformat.h"
33
#include "internal.h"
34

35 36
#define FORM_TAG MKTAG('F', 'O', 'R', 'M')
#define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
37
#define  PC__TAG MKTAG('_', 'P', 'C', '_')
38 39 40 41 42 43 44 45 46 47
#define SOND_TAG MKTAG('S', 'O', 'N', 'D')
#define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
#define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
#define PALT_TAG MKTAG('P', 'A', 'L', 'T')
#define INDX_TAG MKTAG('I', 'N', 'D', 'X')
#define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
#define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
#define VGA__TAG MKTAG('V', 'G', 'A', ' ')
#define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
#define AUDI_TAG MKTAG('A', 'U', 'D', 'I')
48 49 50 51 52 53 54 55 56 57 58

/* video resolution unless otherwise specified */
#define WC3_DEFAULT_WIDTH 320
#define WC3_DEFAULT_HEIGHT 165

/* always use the same PCM audio parameters */
#define WC3_SAMPLE_RATE 22050
#define WC3_AUDIO_CHANNELS 1
#define WC3_AUDIO_BITS 16

/* nice, constant framerate */
59
#define WC3_FRAME_FPS 15
60 61 62 63 64 65 66 67 68 69

#define PALETTE_SIZE (256 * 3)

typedef struct Wc3DemuxContext {
    int width;
    int height;
    int64_t pts;
    int video_stream_index;
    int audio_stream_index;

70
    AVPacket vpkt;
71 72 73 74 75 76 77 78

} Wc3DemuxContext;

static int wc3_probe(AVProbeData *p)
{
    if (p->buf_size < 12)
        return 0;

79 80
    if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
        (AV_RL32(&p->buf[8]) != MOVE_TAG))
81 82 83 84 85
        return 0;

    return AVPROBE_SCORE_MAX;
}

86
static int wc3_read_header(AVFormatContext *s)
87
{
88
    Wc3DemuxContext *wc3 = s->priv_data;
89
    AVIOContext *pb = s->pb;
90 91 92 93
    unsigned int fourcc_tag;
    unsigned int size;
    AVStream *st;
    int ret = 0;
94
    char *buffer;
95 96 97 98 99 100

    /* default context members */
    wc3->width = WC3_DEFAULT_WIDTH;
    wc3->height = WC3_DEFAULT_HEIGHT;
    wc3->pts = 0;
    wc3->video_stream_index = wc3->audio_stream_index = 0;
101 102
    av_init_packet(&wc3->vpkt);
    wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
103 104

    /* skip the first 3 32-bit numbers */
105
    avio_skip(pb, 12);
106 107 108

    /* traverse through the chunks and load the header information before
     * the first BRCH tag */
109 110
    fourcc_tag = avio_rl32(pb);
    size = (avio_rb32(pb) + 1) & (~1);
111 112 113 114 115 116 117

    do {
        switch (fourcc_tag) {

        case SOND_TAG:
        case INDX_TAG:
            /* SOND unknown, INDX unnecessary; ignore both */
118
            avio_skip(pb, size);
119 120
            break;

121
        case PC__TAG:
122
            /* number of palettes, unneeded */
123
            avio_skip(pb, 12);
124 125 126 127
            break;

        case BNAM_TAG:
            /* load up the name */
128 129
            buffer = av_malloc(size+1);
            if (!buffer)
130
                return AVERROR(ENOMEM);
131
            if ((ret = avio_read(pb, buffer, size)) != size)
132
                return AVERROR(EIO);
133
            buffer[size] = 0;
134 135
            av_dict_set(&s->metadata, "title", buffer,
                                   AV_DICT_DONT_STRDUP_VAL);
136 137 138 139
            break;

        case SIZE_TAG:
            /* video resolution override */
140 141
            wc3->width  = avio_rl32(pb);
            wc3->height = avio_rl32(pb);
142 143 144 145
            break;

        case PALT_TAG:
            /* one of several palettes */
146
            avio_seek(pb, -8, SEEK_CUR);
147
            av_append_packet(pb, &wc3->vpkt, 8 + PALETTE_SIZE);
148 149 150
            break;

        default:
151
            av_log(s, AV_LOG_ERROR, "  unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
152 153
                (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
                (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
154 155 156
            return AVERROR_INVALIDDATA;
        }

157
        fourcc_tag = avio_rl32(pb);
158
        /* chunk sizes are 16-bit aligned */
159
        size = (avio_rb32(pb) + 1) & (~1);
160 161
        if (url_feof(pb))
            return AVERROR(EIO);
162 163 164 165

    } while (fourcc_tag != BRCH_TAG);

    /* initialize the decoder streams */
166
    st = avformat_new_stream(s, NULL);
167
    if (!st)
168
        return AVERROR(ENOMEM);
169
    avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
170
    wc3->video_stream_index = st->index;
171
    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
172 173 174 175
    st->codec->codec_id = CODEC_ID_XAN_WC3;
    st->codec->codec_tag = 0;  /* no fourcc */
    st->codec->width = wc3->width;
    st->codec->height = wc3->height;
176

177
    st = avformat_new_stream(s, NULL);
178
    if (!st)
179
        return AVERROR(ENOMEM);
180
    avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
181
    wc3->audio_stream_index = st->index;
182
    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
183 184 185
    st->codec->codec_id = CODEC_ID_PCM_S16LE;
    st->codec->codec_tag = 1;
    st->codec->channels = WC3_AUDIO_CHANNELS;
186
    st->codec->bits_per_coded_sample = WC3_AUDIO_BITS;
187 188
    st->codec->sample_rate = WC3_SAMPLE_RATE;
    st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
189
        st->codec->bits_per_coded_sample;
190
    st->codec->block_align = WC3_AUDIO_BITS * WC3_AUDIO_CHANNELS;
191 192 193 194 195 196 197

    return 0;
}

static int wc3_read_packet(AVFormatContext *s,
                           AVPacket *pkt)
{
198
    Wc3DemuxContext *wc3 = s->priv_data;
199
    AVIOContext *pb = s->pb;
200 201 202 203 204 205 206 207
    unsigned int fourcc_tag;
    unsigned int size;
    int packet_read = 0;
    int ret = 0;
    unsigned char text[1024];

    while (!packet_read) {

208
        fourcc_tag = avio_rl32(pb);
209
        /* chunk sizes are 16-bit aligned */
210
        size = (avio_rb32(pb) + 1) & (~1);
211 212
        if (url_feof(pb))
            return AVERROR(EIO);
213 214 215 216 217 218 219 220 221

        switch (fourcc_tag) {

        case BRCH_TAG:
            /* no-op */
            break;

        case SHOT_TAG:
            /* load up new palette */
222
            avio_seek(pb, -8, SEEK_CUR);
223
            av_append_packet(pb, &wc3->vpkt, 8 + 4);
224 225 226 227
            break;

        case VGA__TAG:
            /* send out video chunk */
228
            avio_seek(pb, -8, SEEK_CUR);
229 230 231 232 233 234
            ret= av_append_packet(pb, &wc3->vpkt, 8 + size);
            // ignore error if we have some data
            if (wc3->vpkt.size > 0)
                ret = 0;
            *pkt = wc3->vpkt;
            wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
235 236 237 238 239 240 241 242
            pkt->stream_index = wc3->video_stream_index;
            pkt->pts = wc3->pts;
            packet_read = 1;
            break;

        case TEXT_TAG:
            /* subtitle chunk */
#if 0
243
            avio_skip(pb, size);
244
#else
245
            if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size)
246
                ret = AVERROR(EIO);
247 248
            else {
                int i = 0;
249 250
                av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
                av_log (s, AV_LOG_DEBUG, "  inglish: %s\n", &text[i + 1]);
251
                i += text[i] + 1;
252
                av_log (s, AV_LOG_DEBUG, "  doytsch: %s\n", &text[i + 1]);
253
                i += text[i] + 1;
254
                av_log (s, AV_LOG_DEBUG, "  fronsay: %s\n", &text[i + 1]);
255 256 257 258 259 260
            }
#endif
            break;

        case AUDI_TAG:
            /* send out audio chunk */
Michael Niedermayer's avatar
Michael Niedermayer committed
261
            ret= av_get_packet(pb, pkt, size);
262 263 264 265
            pkt->stream_index = wc3->audio_stream_index;
            pkt->pts = wc3->pts;

            /* time to advance pts */
266
            wc3->pts++;
267 268 269 270 271

            packet_read = 1;
            break;

        default:
272
            av_log (s, AV_LOG_ERROR, "  unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
273 274
                (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
                (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
275 276 277 278 279 280 281 282 283 284 285
            ret = AVERROR_INVALIDDATA;
            packet_read = 1;
            break;
        }
    }

    return ret;
}

static int wc3_read_close(AVFormatContext *s)
{
286
    Wc3DemuxContext *wc3 = s->priv_data;
287

288 289
    if (wc3->vpkt.size > 0)
        av_free_packet(&wc3->vpkt);
290 291 292 293

    return 0;
}

294
AVInputFormat ff_wc3_demuxer = {
295 296 297 298 299 300 301
    .name           = "wc3movie",
    .long_name      = NULL_IF_CONFIG_SMALL("Wing Commander III movie format"),
    .priv_data_size = sizeof(Wc3DemuxContext),
    .read_probe     = wc3_probe,
    .read_header    = wc3_read_header,
    .read_packet    = wc3_read_packet,
    .read_close     = wc3_read_close,
302
};