2 * American Laser Games MM Format Demuxer
3 * Copyright (c) 2006 Peter Ross
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * American Laser Games MM Format Demuxer
23 * by Peter Ross (suxen_drol at hotmail dot com)
25 * The MM format was used by IBM-PC ports of ALG's "arcade shooter" games,
26 * including Mad Dog McCree and Crime Patrol.
28 * Technical details here:
29 * http://wiki.multimedia.cx/index.php?title=American_Laser_Games_MM
34 #define MM_PREAMBLE_SIZE 6
36 #define MM_TYPE_HEADER 0x0
37 #define MM_TYPE_INTER 0x5
38 #define MM_TYPE_INTRA 0x8
39 #define MM_TYPE_INTRA_HH 0xc
40 #define MM_TYPE_INTER_HH 0xd
41 #define MM_TYPE_INTRA_HHV 0xe
42 #define MM_TYPE_INTER_HHV 0xf
43 #define MM_TYPE_AUDIO 0x15
44 #define MM_TYPE_PALETTE 0x31
46 #define MM_HEADER_LEN_V 0x16 /* video only */
47 #define MM_HEADER_LEN_AV 0x18 /* video + audio */
49 #define MM_PALETTE_COUNT 128
50 #define MM_PALETTE_SIZE (MM_PALETTE_COUNT*3)
53 AVPaletteControl palette_control;
54 unsigned int audio_pts, video_pts;
57 static int mm_probe(AVProbeData *p)
59 /* the first chunk is always the header */
60 if (p->buf_size < MM_PREAMBLE_SIZE)
62 if (LE_16(&p->buf[0]) != MM_TYPE_HEADER)
64 if (LE_32(&p->buf[2]) != MM_HEADER_LEN_V && LE_32(&p->buf[2]) != MM_HEADER_LEN_AV)
67 /* only return half certainty since this check is a bit sketchy */
68 return AVPROBE_SCORE_MAX / 2;
71 static int mm_read_header(AVFormatContext *s,
72 AVFormatParameters *ap)
74 MmDemuxContext *mm = (MmDemuxContext *)s->priv_data;
75 ByteIOContext *pb = &s->pb;
78 unsigned int type, length;
79 unsigned int frame_rate, width, height;
82 length = get_le32(pb);
84 if (type != MM_TYPE_HEADER)
85 return AVERROR_INVALIDDATA;
88 get_le16(pb); /* total number of chunks */
89 frame_rate = get_le16(pb);
90 get_le16(pb); /* ibm-pc video bios mode */
92 height = get_le16(pb);
93 url_fseek(pb, length - 10, SEEK_CUR); /* unknown data */
96 st = av_new_stream(s, 0);
99 st->codec->codec_type = CODEC_TYPE_VIDEO;
100 st->codec->codec_id = CODEC_ID_MMVIDEO;
101 st->codec->codec_tag = 0; /* no fourcc */
102 st->codec->width = width;
103 st->codec->height = height;
104 st->codec->palctrl = &mm->palette_control;
105 av_set_pts_info(st, 64, 1, frame_rate);
108 if (length == MM_HEADER_LEN_AV) {
109 st = av_new_stream(s, 0);
111 return AVERROR_NOMEM;
112 st->codec->codec_type = CODEC_TYPE_AUDIO;
113 st->codec->codec_tag = 0; /* no fourcc */
114 st->codec->codec_id = CODEC_ID_PCM_U8;
115 st->codec->channels = 1;
116 st->codec->sample_rate = 8000;
117 av_set_pts_info(st, 64, 1, 8000); /* 8000 hz */
120 mm->palette_control.palette_changed = 0;
126 static int mm_read_packet(AVFormatContext *s,
129 MmDemuxContext *mm = (MmDemuxContext *)s->priv_data;
130 ByteIOContext *pb = &s->pb;
131 unsigned char preamble[MM_PREAMBLE_SIZE];
132 unsigned char pal[MM_PALETTE_SIZE];
133 unsigned int type, length;
138 if (get_buffer(pb, preamble, MM_PREAMBLE_SIZE) != MM_PREAMBLE_SIZE) {
142 type = LE_16(&preamble[0]);
143 length = LE_16(&preamble[2]);
146 case MM_TYPE_PALETTE :
147 url_fseek(pb, 4, SEEK_CUR); /* unknown data */
148 if (get_buffer(pb, pal, MM_PALETTE_SIZE) != MM_PALETTE_SIZE)
150 url_fseek(pb, length - (4 + MM_PALETTE_SIZE), SEEK_CUR);
152 for (i=0; i<MM_PALETTE_COUNT; i++) {
153 int r = pal[i*3 + 0];
154 int g = pal[i*3 + 1];
155 int b = pal[i*3 + 2];
156 mm->palette_control.palette[i] = (r << 16) | (g << 8) | (b);
157 /* repeat palette, where each components is multiplied by four */
158 mm->palette_control.palette[i+128] = (r << 18) | (g << 10) | (b<<2);
160 mm->palette_control.palette_changed = 1;
165 case MM_TYPE_INTRA_HH :
166 case MM_TYPE_INTER_HH :
167 case MM_TYPE_INTRA_HHV :
168 case MM_TYPE_INTER_HHV :
169 /* output preamble + data */
170 if (av_new_packet(pkt, length + MM_PREAMBLE_SIZE))
171 return AVERROR_NOMEM;
172 memcpy(pkt->data, preamble, MM_PREAMBLE_SIZE);
173 if (get_buffer(pb, pkt->data + MM_PREAMBLE_SIZE, length) != length)
175 pkt->size = length + MM_PREAMBLE_SIZE;
176 pkt->stream_index = 0;
177 pkt->pts = mm->video_pts++;
181 if (av_get_packet(&s->pb, pkt, length)<0)
182 return AVERROR_NOMEM;
184 pkt->stream_index = 1;
185 pkt->pts = mm->audio_pts++;
189 av_log(NULL, AV_LOG_INFO, "mm: unknown chunk type 0x%x\n", type);
190 url_fseek(pb, length, SEEK_CUR);
197 static int mm_read_close(AVFormatContext *s)
202 static AVInputFormat mm_iformat = {
204 "American Laser Games MM format",
205 sizeof(MmDemuxContext),
214 av_register_input_format(&mm_iformat);