Statistics
| Branch: | Revision:

ffmpeg / libavformat / mm.c @ 3143f133

History | View | Annotate | Download (5.45 KB)

1 4e114829 Mike Melanson
/*
2
 * American Laser Games MM Format Demuxer
3
 * Copyright (c) 2006 Peter Ross
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 4e114829 Mike Melanson
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 4e114829 Mike Melanson
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 4e114829 Mike Melanson
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 e5a389a1 Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 4e114829 Mike Melanson
 */
21
22
/**
23
 * @file mm.c
24
 * American Laser Games MM Format Demuxer
25
 * by Peter Ross (suxen_drol at hotmail dot com)
26
 *
27
 * The MM format was used by IBM-PC ports of ALG's "arcade shooter" games,
28
 * including Mad Dog McCree and Crime Patrol.
29
 *
30
 * Technical details here:
31
 *  http://wiki.multimedia.cx/index.php?title=American_Laser_Games_MM
32
 */
33
34
#include "avformat.h"
35
36
#define MM_PREAMBLE_SIZE    6
37
38
#define MM_TYPE_HEADER      0x0
39
#define MM_TYPE_INTER       0x5
40
#define MM_TYPE_INTRA       0x8
41
#define MM_TYPE_INTRA_HH    0xc
42
#define MM_TYPE_INTER_HH    0xd
43
#define MM_TYPE_INTRA_HHV   0xe
44
#define MM_TYPE_INTER_HHV   0xf
45
#define MM_TYPE_AUDIO       0x15
46
#define MM_TYPE_PALETTE     0x31
47
48
#define MM_HEADER_LEN_V     0x16    /* video only */
49
#define MM_HEADER_LEN_AV    0x18    /* video + audio */
50
51
#define MM_PALETTE_COUNT    128
52
#define MM_PALETTE_SIZE     (MM_PALETTE_COUNT*3)
53
54
typedef struct {
55
  unsigned int audio_pts, video_pts;
56
} MmDemuxContext;
57
58
static int mm_probe(AVProbeData *p)
59
{
60
    /* the first chunk is always the header */
61 fead30d4 Alex Beregszaszi
    if (AV_RL16(&p->buf[0]) != MM_TYPE_HEADER)
62 4e114829 Mike Melanson
        return 0;
63 fead30d4 Alex Beregszaszi
    if (AV_RL32(&p->buf[2]) != MM_HEADER_LEN_V && AV_RL32(&p->buf[2]) != MM_HEADER_LEN_AV)
64 4e114829 Mike Melanson
        return 0;
65
66
    /* only return half certainty since this check is a bit sketchy */
67
    return AVPROBE_SCORE_MAX / 2;
68
}
69
70
static int mm_read_header(AVFormatContext *s,
71
                           AVFormatParameters *ap)
72
{
73 e4141433 Nicholas Tung
    MmDemuxContext *mm = s->priv_data;
74 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
75 4e114829 Mike Melanson
    AVStream *st;
76
77
    unsigned int type, length;
78
    unsigned int frame_rate, width, height;
79
80
    type = get_le16(pb);
81
    length = get_le32(pb);
82
83
    if (type != MM_TYPE_HEADER)
84
        return AVERROR_INVALIDDATA;
85
86
    /* read header */
87
    get_le16(pb);   /* total number of chunks */
88
    frame_rate = get_le16(pb);
89
    get_le16(pb);   /* ibm-pc video bios mode */
90
    width = get_le16(pb);
91
    height = get_le16(pb);
92
    url_fseek(pb, length - 10, SEEK_CUR);  /* unknown data */
93
94
    /* video stream */
95
    st = av_new_stream(s, 0);
96
    if (!st)
97 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
98 4e114829 Mike Melanson
    st->codec->codec_type = CODEC_TYPE_VIDEO;
99
    st->codec->codec_id = CODEC_ID_MMVIDEO;
100
    st->codec->codec_tag = 0;  /* no fourcc */
101
    st->codec->width = width;
102
    st->codec->height = height;
103
    av_set_pts_info(st, 64, 1, frame_rate);
104
105
    /* audio stream */
106
    if (length == MM_HEADER_LEN_AV) {
107
        st = av_new_stream(s, 0);
108
        if (!st)
109 769e10f0 Panagiotis Issaris
            return AVERROR(ENOMEM);
110 4e114829 Mike Melanson
        st->codec->codec_type = CODEC_TYPE_AUDIO;
111
        st->codec->codec_tag = 0; /* no fourcc */
112
        st->codec->codec_id = CODEC_ID_PCM_U8;
113
        st->codec->channels = 1;
114
        st->codec->sample_rate = 8000;
115
        av_set_pts_info(st, 64, 1, 8000); /* 8000 hz */
116
    }
117
118
    mm->audio_pts = 0;
119
    mm->video_pts = 0;
120
    return 0;
121
}
122
123
static int mm_read_packet(AVFormatContext *s,
124
                           AVPacket *pkt)
125
{
126 e4141433 Nicholas Tung
    MmDemuxContext *mm = s->priv_data;
127 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
128 4e114829 Mike Melanson
    unsigned char preamble[MM_PREAMBLE_SIZE];
129
    unsigned int type, length;
130
131
    while(1) {
132
133
        if (get_buffer(pb, preamble, MM_PREAMBLE_SIZE) != MM_PREAMBLE_SIZE) {
134 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
135 4e114829 Mike Melanson
        }
136
137 fead30d4 Alex Beregszaszi
        type = AV_RL16(&preamble[0]);
138
        length = AV_RL16(&preamble[2]);
139 4e114829 Mike Melanson
140
        switch(type) {
141
        case MM_TYPE_PALETTE :
142
        case MM_TYPE_INTER :
143
        case MM_TYPE_INTRA :
144
        case MM_TYPE_INTRA_HH :
145
        case MM_TYPE_INTER_HH :
146
        case MM_TYPE_INTRA_HHV :
147
        case MM_TYPE_INTER_HHV :
148
            /* output preamble + data */
149
            if (av_new_packet(pkt, length + MM_PREAMBLE_SIZE))
150 769e10f0 Panagiotis Issaris
                return AVERROR(ENOMEM);
151 4e114829 Mike Melanson
            memcpy(pkt->data, preamble, MM_PREAMBLE_SIZE);
152
            if (get_buffer(pb, pkt->data + MM_PREAMBLE_SIZE, length) != length)
153 6f3e0b21 Panagiotis Issaris
                return AVERROR(EIO);
154 4e114829 Mike Melanson
            pkt->size = length + MM_PREAMBLE_SIZE;
155
            pkt->stream_index = 0;
156 60f451b6 Peter Ross
            pkt->pts = mm->video_pts;
157
            if (type!=MM_TYPE_PALETTE)
158
                mm->video_pts++;
159 4e114829 Mike Melanson
            return 0;
160
161
        case MM_TYPE_AUDIO :
162 899681cd Björn Axelsson
            if (av_get_packet(s->pb, pkt, length)<0)
163 769e10f0 Panagiotis Issaris
                return AVERROR(ENOMEM);
164 4e114829 Mike Melanson
            pkt->size = length;
165
            pkt->stream_index = 1;
166
            pkt->pts = mm->audio_pts++;
167
            return 0;
168
169
        default :
170
            av_log(NULL, AV_LOG_INFO, "mm: unknown chunk type 0x%x\n", type);
171
            url_fseek(pb, length, SEEK_CUR);
172
        }
173
    }
174
175
    return 0;
176
}
177
178
static int mm_read_close(AVFormatContext *s)
179
{
180
    return 0;
181
}
182
183 ff70e601 Måns Rullgård
AVInputFormat mm_demuxer = {
184 4e114829 Mike Melanson
    "mm",
185 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("American Laser Games MM format"),
186 4e114829 Mike Melanson
    sizeof(MmDemuxContext),
187
    mm_probe,
188
    mm_read_header,
189
    mm_read_packet,
190
    mm_read_close,
191
};