mm.c 5.89 KB
Newer Older
1 2 3 4
/*
 * American Laser Games MM Format Demuxer
 * Copyright (c) 2006 Peter Ross
 *
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
 * American Laser Games MM Format Demuxer
Peter Ross's avatar
Peter Ross committed
25
 * by Peter Ross (pross@xvid.org)
26 27 28 29 30 31 32 33
 *
 * The MM format was used by IBM-PC ports of ALG's "arcade shooter" games,
 * including Mad Dog McCree and Crime Patrol.
 *
 * Technical details here:
 *  http://wiki.multimedia.cx/index.php?title=American_Laser_Games_MM
 */

Justin Ruggles's avatar
Justin Ruggles committed
34
#include "libavutil/channel_layout.h"
35
#include "libavutil/intreadwrite.h"
36
#include "avformat.h"
37
#include "internal.h"
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

#define MM_PREAMBLE_SIZE    6

#define MM_TYPE_HEADER      0x0
#define MM_TYPE_INTER       0x5
#define MM_TYPE_INTRA       0x8
#define MM_TYPE_INTRA_HH    0xc
#define MM_TYPE_INTER_HH    0xd
#define MM_TYPE_INTRA_HHV   0xe
#define MM_TYPE_INTER_HHV   0xf
#define MM_TYPE_AUDIO       0x15
#define MM_TYPE_PALETTE     0x31

#define MM_HEADER_LEN_V     0x16    /* video only */
#define MM_HEADER_LEN_AV    0x18    /* video + audio */

#define MM_PALETTE_COUNT    128
#define MM_PALETTE_SIZE     (MM_PALETTE_COUNT*3)

57
typedef struct MmDemuxContext {
58 59 60
  unsigned int audio_pts, video_pts;
} MmDemuxContext;

61
static int probe(AVProbeData *p)
62
{
63 64 65
    int len, type, fps, w, h;
    if (p->buf_size < MM_HEADER_LEN_AV + MM_PREAMBLE_SIZE)
        return 0;
66
    /* the first chunk is always the header */
67
    if (AV_RL16(&p->buf[0]) != MM_TYPE_HEADER)
68
        return 0;
69 70 71 72 73 74 75 76 77 78
    len = AV_RL32(&p->buf[2]);
    if (len != MM_HEADER_LEN_V && len != MM_HEADER_LEN_AV)
        return 0;
    fps = AV_RL16(&p->buf[8]);
    w = AV_RL16(&p->buf[12]);
    h = AV_RL16(&p->buf[14]);
    if (!fps || fps > 60 || !w || w > 2048 || !h || h > 2048)
        return 0;
    type = AV_RL16(&p->buf[len]);
    if (!type || type > 0x31)
79 80 81
        return 0;

    /* only return half certainty since this check is a bit sketchy */
82
    return AVPROBE_SCORE_EXTENSION;
83 84
}

85
static int read_header(AVFormatContext *s)
86
{
87
    MmDemuxContext *mm = s->priv_data;
88
    AVIOContext *pb = s->pb;
89 90 91 92 93
    AVStream *st;

    unsigned int type, length;
    unsigned int frame_rate, width, height;

94 95
    type = avio_rl16(pb);
    length = avio_rl32(pb);
96 97 98 99 100

    if (type != MM_TYPE_HEADER)
        return AVERROR_INVALIDDATA;

    /* read header */
101 102 103 104 105
    avio_rl16(pb);   /* total number of chunks */
    frame_rate = avio_rl16(pb);
    avio_rl16(pb);   /* ibm-pc video bios mode */
    width = avio_rl16(pb);
    height = avio_rl16(pb);
106
    avio_skip(pb, length - 10);  /* unknown data */
107 108

    /* video stream */
109
    st = avformat_new_stream(s, NULL);
110
    if (!st)
111
        return AVERROR(ENOMEM);
112 113 114 115 116
    st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
    st->codecpar->codec_id = AV_CODEC_ID_MMVIDEO;
    st->codecpar->codec_tag = 0;  /* no fourcc */
    st->codecpar->width = width;
    st->codecpar->height = height;
117
    avpriv_set_pts_info(st, 64, 1, frame_rate);
118 119 120

    /* audio stream */
    if (length == MM_HEADER_LEN_AV) {
121
        st = avformat_new_stream(s, NULL);
122
        if (!st)
123
            return AVERROR(ENOMEM);
124 125 126 127 128 129
        st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
        st->codecpar->codec_tag = 0; /* no fourcc */
        st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
        st->codecpar->channels = 1;
        st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
        st->codecpar->sample_rate = 8000;
130
        avpriv_set_pts_info(st, 64, 1, 8000); /* 8000 hz */
131 132 133 134 135 136 137
    }

    mm->audio_pts = 0;
    mm->video_pts = 0;
    return 0;
}

138
static int read_packet(AVFormatContext *s,
139 140
                           AVPacket *pkt)
{
141
    MmDemuxContext *mm = s->priv_data;
142
    AVIOContext *pb = s->pb;
143 144 145 146 147
    unsigned char preamble[MM_PREAMBLE_SIZE];
    unsigned int type, length;

    while(1) {

148
        if (avio_read(pb, preamble, MM_PREAMBLE_SIZE) != MM_PREAMBLE_SIZE) {
149
            return AVERROR(EIO);
150 151
        }

152 153
        type = AV_RL16(&preamble[0]);
        length = AV_RL16(&preamble[2]);
154 155 156 157 158 159 160 161 162 163 164

        switch(type) {
        case MM_TYPE_PALETTE :
        case MM_TYPE_INTER :
        case MM_TYPE_INTRA :
        case MM_TYPE_INTRA_HH :
        case MM_TYPE_INTER_HH :
        case MM_TYPE_INTRA_HHV :
        case MM_TYPE_INTER_HHV :
            /* output preamble + data */
            if (av_new_packet(pkt, length + MM_PREAMBLE_SIZE))
165
                return AVERROR(ENOMEM);
166
            memcpy(pkt->data, preamble, MM_PREAMBLE_SIZE);
167
            if (avio_read(pb, pkt->data + MM_PREAMBLE_SIZE, length) != length)
168
                return AVERROR(EIO);
169 170
            pkt->size = length + MM_PREAMBLE_SIZE;
            pkt->stream_index = 0;
171 172 173
            pkt->pts = mm->video_pts;
            if (type!=MM_TYPE_PALETTE)
                mm->video_pts++;
174 175 176
            return 0;

        case MM_TYPE_AUDIO :
177
            if (av_get_packet(s->pb, pkt, length)<0)
178
                return AVERROR(ENOMEM);
179 180 181 182 183
            pkt->stream_index = 1;
            pkt->pts = mm->audio_pts++;
            return 0;

        default :
184
            av_log(s, AV_LOG_INFO, "unknown chunk type 0x%x\n", type);
185
            avio_skip(pb, length);
186 187 188 189
        }
    }
}

190
AVInputFormat ff_mm_demuxer = {
191
    .name           = "mm",
192
    .long_name      = NULL_IF_CONFIG_SMALL("American Laser Games MM"),
193 194 195 196
    .priv_data_size = sizeof(MmDemuxContext),
    .read_probe     = probe,
    .read_header    = read_header,
    .read_packet    = read_packet,
197
};