2
* American Laser Games MM Format Demuxer
3
* Copyright (c) 2006 Peter Ross
5
* This file is part of FFmpeg.
7
* FFmpeg is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* FFmpeg is distributed in the hope that it will be useful,
13
* 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.
17
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
24
* American Laser Games MM Format Demuxer
25
* by Peter Ross (suxen_drol at hotmail dot com)
27
* The MM format was used by IBM-PC ports of ALG's "arcade shooter" games,
28
* including Mad Dog McCree and Crime Patrol.
30
* Technical details here:
31
* http://wiki.multimedia.cx/index.php?title=American_Laser_Games_MM
36
#define MM_PREAMBLE_SIZE 6
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
48
#define MM_HEADER_LEN_V 0x16 /* video only */
49
#define MM_HEADER_LEN_AV 0x18 /* video + audio */
51
#define MM_PALETTE_COUNT 128
52
#define MM_PALETTE_SIZE (MM_PALETTE_COUNT*3)
55
AVPaletteControl palette_control;
56
unsigned int audio_pts, video_pts;
59
static int mm_probe(AVProbeData *p)
61
/* the first chunk is always the header */
62
if (p->buf_size < MM_PREAMBLE_SIZE)
64
if (AV_RL16(&p->buf[0]) != MM_TYPE_HEADER)
66
if (AV_RL32(&p->buf[2]) != MM_HEADER_LEN_V && AV_RL32(&p->buf[2]) != MM_HEADER_LEN_AV)
69
/* only return half certainty since this check is a bit sketchy */
70
return AVPROBE_SCORE_MAX / 2;
73
static int mm_read_header(AVFormatContext *s,
74
AVFormatParameters *ap)
76
MmDemuxContext *mm = (MmDemuxContext *)s->priv_data;
77
ByteIOContext *pb = &s->pb;
80
unsigned int type, length;
81
unsigned int frame_rate, width, height;
84
length = get_le32(pb);
86
if (type != MM_TYPE_HEADER)
87
return AVERROR_INVALIDDATA;
90
get_le16(pb); /* total number of chunks */
91
frame_rate = get_le16(pb);
92
get_le16(pb); /* ibm-pc video bios mode */
94
height = get_le16(pb);
95
url_fseek(pb, length - 10, SEEK_CUR); /* unknown data */
98
st = av_new_stream(s, 0);
100
return AVERROR_NOMEM;
101
st->codec->codec_type = CODEC_TYPE_VIDEO;
102
st->codec->codec_id = CODEC_ID_MMVIDEO;
103
st->codec->codec_tag = 0; /* no fourcc */
104
st->codec->width = width;
105
st->codec->height = height;
106
st->codec->palctrl = &mm->palette_control;
107
av_set_pts_info(st, 64, 1, frame_rate);
110
if (length == MM_HEADER_LEN_AV) {
111
st = av_new_stream(s, 0);
113
return AVERROR_NOMEM;
114
st->codec->codec_type = CODEC_TYPE_AUDIO;
115
st->codec->codec_tag = 0; /* no fourcc */
116
st->codec->codec_id = CODEC_ID_PCM_U8;
117
st->codec->channels = 1;
118
st->codec->sample_rate = 8000;
119
av_set_pts_info(st, 64, 1, 8000); /* 8000 hz */
122
mm->palette_control.palette_changed = 0;
128
static int mm_read_packet(AVFormatContext *s,
131
MmDemuxContext *mm = (MmDemuxContext *)s->priv_data;
132
ByteIOContext *pb = &s->pb;
133
unsigned char preamble[MM_PREAMBLE_SIZE];
134
unsigned char pal[MM_PALETTE_SIZE];
135
unsigned int type, length;
140
if (get_buffer(pb, preamble, MM_PREAMBLE_SIZE) != MM_PREAMBLE_SIZE) {
144
type = AV_RL16(&preamble[0]);
145
length = AV_RL16(&preamble[2]);
148
case MM_TYPE_PALETTE :
149
url_fseek(pb, 4, SEEK_CUR); /* unknown data */
150
if (get_buffer(pb, pal, MM_PALETTE_SIZE) != MM_PALETTE_SIZE)
152
url_fseek(pb, length - (4 + MM_PALETTE_SIZE), SEEK_CUR);
154
for (i=0; i<MM_PALETTE_COUNT; i++) {
155
int r = pal[i*3 + 0];
156
int g = pal[i*3 + 1];
157
int b = pal[i*3 + 2];
158
mm->palette_control.palette[i] = (r << 16) | (g << 8) | (b);
159
/* repeat palette, where each components is multiplied by four */
160
mm->palette_control.palette[i+128] = (r << 18) | (g << 10) | (b<<2);
162
mm->palette_control.palette_changed = 1;
167
case MM_TYPE_INTRA_HH :
168
case MM_TYPE_INTER_HH :
169
case MM_TYPE_INTRA_HHV :
170
case MM_TYPE_INTER_HHV :
171
/* output preamble + data */
172
if (av_new_packet(pkt, length + MM_PREAMBLE_SIZE))
173
return AVERROR_NOMEM;
174
memcpy(pkt->data, preamble, MM_PREAMBLE_SIZE);
175
if (get_buffer(pb, pkt->data + MM_PREAMBLE_SIZE, length) != length)
177
pkt->size = length + MM_PREAMBLE_SIZE;
178
pkt->stream_index = 0;
179
pkt->pts = mm->video_pts++;
183
if (av_get_packet(&s->pb, pkt, length)<0)
184
return AVERROR_NOMEM;
186
pkt->stream_index = 1;
187
pkt->pts = mm->audio_pts++;
191
av_log(NULL, AV_LOG_INFO, "mm: unknown chunk type 0x%x\n", type);
192
url_fseek(pb, length, SEEK_CUR);
199
static int mm_read_close(AVFormatContext *s)
204
AVInputFormat mm_demuxer = {
206
"American Laser Games MM format",
207
sizeof(MmDemuxContext),