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 Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * American Laser Games MM Format Demuxer
25 * by Peter Ross (pross@xvid.org)
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
34 #include "libavutil/channel_layout.h"
35 #include "libavutil/intreadwrite.h"
37 #include "avio_internal.h"
41 #define MM_PREAMBLE_SIZE 6
43 #define MM_TYPE_HEADER 0x0
44 #define MM_TYPE_RAW 0x2
45 #define MM_TYPE_INTER 0x5
46 #define MM_TYPE_INTRA 0x8
47 #define MM_TYPE_INTRA_HH 0xc
48 #define MM_TYPE_INTER_HH 0xd
49 #define MM_TYPE_INTRA_HHV 0xe
50 #define MM_TYPE_INTER_HHV 0xf
51 #define MM_TYPE_AUDIO2 0x14
52 #define MM_TYPE_AUDIO 0x15
53 #define MM_TYPE_PALETTE 0x31
55 #define MM_HEADER_LEN_V 0x16 /* video only */
56 #define MM_HEADER_LEN_AV 0x18 /* video + audio */
57 #define MM_HEADER_LEN_AV2 0x1a
59 #define MM_PALETTE_COUNT 128
60 #define MM_PALETTE_SIZE (MM_PALETTE_COUNT*3)
62 static int probe(const AVProbeData
*p
)
64 int len
, type
, fps
, w
, h
;
65 if (p
->buf_size
< MM_HEADER_LEN_AV
+ MM_PREAMBLE_SIZE
)
67 /* the first chunk is always the header */
68 if (AV_RL16(&p
->buf
[0]) != MM_TYPE_HEADER
)
70 len
= AV_RL32(&p
->buf
[2]);
71 if (len
!= MM_HEADER_LEN_V
&& len
!= MM_HEADER_LEN_AV
&& len
!= MM_HEADER_LEN_AV2
)
73 fps
= AV_RL16(&p
->buf
[8]);
74 w
= AV_RL16(&p
->buf
[12]);
75 h
= AV_RL16(&p
->buf
[14]);
76 if (!fps
|| fps
> 60 || !w
|| w
> 2048 || !h
|| h
> 2048)
78 type
= AV_RL16(&p
->buf
[len
]);
79 if (!type
|| type
> 0x31)
82 /* only return half certainty since this check is a bit sketchy */
83 return AVPROBE_SCORE_EXTENSION
;
86 static int read_header(AVFormatContext
*s
)
88 AVIOContext
*pb
= s
->pb
;
91 unsigned int type
, length
;
92 unsigned int frame_rate
, width
, height
;
95 length
= avio_rl32(pb
);
97 if (type
!= MM_TYPE_HEADER
|| length
< 10)
98 return AVERROR_INVALIDDATA
;
101 avio_rl16(pb
); /* total number of chunks */
102 frame_rate
= avio_rl16(pb
);
103 avio_rl16(pb
); /* ibm-pc video bios mode */
104 width
= avio_rl16(pb
);
105 height
= avio_rl16(pb
);
106 avio_skip(pb
, length
- 10); /* unknown data */
109 st
= avformat_new_stream(s
, NULL
);
111 return AVERROR(ENOMEM
);
112 st
->codecpar
->codec_type
= AVMEDIA_TYPE_VIDEO
;
113 st
->codecpar
->codec_id
= AV_CODEC_ID_MMVIDEO
;
114 st
->codecpar
->codec_tag
= 0; /* no fourcc */
115 st
->codecpar
->width
= width
;
116 st
->codecpar
->height
= height
;
117 avpriv_set_pts_info(st
, 64, 1, frame_rate
);
120 if (length
>= MM_HEADER_LEN_AV
) {
121 st
= avformat_new_stream(s
, NULL
);
123 return AVERROR(ENOMEM
);
124 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
125 st
->codecpar
->codec_tag
= 0; /* no fourcc */
126 st
->codecpar
->codec_id
= AV_CODEC_ID_PCM_U8
;
127 st
->codecpar
->ch_layout
= (AVChannelLayout
)AV_CHANNEL_LAYOUT_MONO
;
128 st
->codecpar
->sample_rate
= 8000;
129 avpriv_set_pts_info(st
, 64, 1, 8000); /* 8000 hz */
135 static int read_packet(AVFormatContext
*s
,
138 AVIOContext
*pb
= s
->pb
;
139 unsigned char preamble
[MM_PREAMBLE_SIZE
];
140 unsigned int type
, length
;
141 int64_t pos
= avio_tell(pb
);
148 ret
= ffio_read_size(pb
, preamble
, MM_PREAMBLE_SIZE
);
152 type
= AV_RL16(&preamble
[0]);
153 length
= AV_RL16(&preamble
[2]);
157 case MM_TYPE_PALETTE
:
160 case MM_TYPE_INTRA_HH
:
161 case MM_TYPE_INTER_HH
:
162 case MM_TYPE_INTRA_HHV
:
163 case MM_TYPE_INTER_HHV
:
164 /* output preamble + data */
165 if ((ret
= av_new_packet(pkt
, length
+ MM_PREAMBLE_SIZE
)) < 0)
167 memcpy(pkt
->data
, preamble
, MM_PREAMBLE_SIZE
);
168 ret
= ffio_read_size(pb
, pkt
->data
+ MM_PREAMBLE_SIZE
, length
);
171 pkt
->size
= length
+ MM_PREAMBLE_SIZE
;
172 pkt
->stream_index
= 0;
173 if (type
!=MM_TYPE_PALETTE
)
175 if (type
== MM_TYPE_RAW
||
176 type
== MM_TYPE_INTRA
)
177 pkt
->flags
|= AV_PKT_FLAG_KEY
;
182 if (s
->nb_streams
< 2)
183 return AVERROR_INVALIDDATA
;
184 if ((ret
= av_get_packet(s
->pb
, pkt
, length
)) < 0)
186 pkt
->stream_index
= 1;
187 pkt
->duration
= length
;
192 av_log(s
, AV_LOG_INFO
, "unknown chunk type 0x%x\n", type
);
194 case MM_TYPE_AUDIO2
:
195 avio_skip(pb
, length
);
200 const FFInputFormat ff_mm_demuxer
= {
202 .p
.long_name
= NULL_IF_CONFIG_SMALL("American Laser Games MM"),
203 .p
.flags
= AVFMT_GENERIC_INDEX
,
205 .read_header
= read_header
,
206 .read_packet
= read_packet
,