FFmpeg
Loading...
Searching...
No Matches
mm.c
Go to the documentation of this file.
1/*
2 * American Laser Games MM Format Demuxer
3 * Copyright (c) 2006 Peter Ross
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22/**
23 * @file
24 * American Laser Games MM Format Demuxer
25 * by Peter Ross (pross@xvid.org)
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
37#include "avformat.h"
38#include "avio_internal.h"
39#include "demux.h"
40#include "internal.h"
41
42#define MM_PREAMBLE_SIZE 6
43
44#define MM_TYPE_HEADER 0x0
45#define MM_TYPE_RAW 0x2
46#define MM_TYPE_INTER 0x5
47#define MM_TYPE_INTRA 0x8
48#define MM_TYPE_INTRA_HH 0xc
49#define MM_TYPE_INTER_HH 0xd
50#define MM_TYPE_INTRA_HHV 0xe
51#define MM_TYPE_INTER_HHV 0xf
52#define MM_TYPE_AUDIO2 0x14
53#define MM_TYPE_AUDIO 0x15
54#define MM_TYPE_PALETTE 0x31
55
56#define MM_HEADER_LEN_V 0x16 /* video only */
57#define MM_HEADER_LEN_AV 0x18 /* video + audio */
58#define MM_HEADER_LEN_AV2 0x1a
59
60#define MM_PALETTE_COUNT 128
61#define MM_PALETTE_SIZE (MM_PALETTE_COUNT*3)
62
63static int probe(const AVProbeData *p)
64{
65 int len, type, fps, w, h;
66 if (p->buf_size < MM_HEADER_LEN_AV + MM_PREAMBLE_SIZE)
67 return 0;
68 /* the first chunk is always the header */
69 if (AV_RL16(&p->buf[0]) != MM_TYPE_HEADER)
70 return 0;
71 len = AV_RL32(&p->buf[2]);
73 return 0;
74 fps = AV_RL16(&p->buf[8]);
75 w = AV_RL16(&p->buf[12]);
76 h = AV_RL16(&p->buf[14]);
77 if (!fps || fps > 60 || !w || w > 2048 || !h || h > 2048)
78 return 0;
79 type = AV_RL16(&p->buf[len]);
80 if (!type || type > 0x31)
81 return 0;
82
83 /* only return half certainty since this check is a bit sketchy */
85}
86
88{
89 AVIOContext *pb = s->pb;
90 AVStream *st;
91
92 unsigned int type, length;
93 unsigned int frame_rate, width, height;
94
95 type = avio_rl16(pb);
96 length = avio_rl32(pb);
97
98 if (type != MM_TYPE_HEADER || length < 10)
100
101 /* read header */
102 avio_rl16(pb); /* total number of chunks */
103 frame_rate = avio_rl16(pb);
104 avio_rl16(pb); /* ibm-pc video bios mode */
105 width = avio_rl16(pb);
106 height = avio_rl16(pb);
107 avio_skip(pb, length - 10); /* unknown data */
108
109 /* video stream */
111 if (!st)
112 return AVERROR(ENOMEM);
115 st->codecpar->codec_tag = 0; /* no fourcc */
116 st->codecpar->width = width;
117 st->codecpar->height = height;
118 avpriv_set_pts_info(st, 64, 1, frame_rate);
119
120 /* audio stream */
121 if (length >= MM_HEADER_LEN_AV) {
123 if (!st)
124 return AVERROR(ENOMEM);
126 st->codecpar->codec_tag = 0; /* no fourcc */
129 st->codecpar->sample_rate = 8000;
130 avpriv_set_pts_info(st, 64, 1, 8000); /* 8000 hz */
131 }
132
133 return 0;
134}
135
137 AVPacket *pkt)
138{
139 AVIOContext *pb = s->pb;
140 unsigned char preamble[MM_PREAMBLE_SIZE];
141 unsigned int type, length;
142 int64_t pos = avio_tell(pb);
143 int ret;
144
145 while (1) {
146 if (avio_feof(pb))
147 return AVERROR_EOF;
148
149 ret = ffio_read_size(pb, preamble, MM_PREAMBLE_SIZE);
150 if (ret < 0)
151 return ret;
152
153 type = AV_RL16(&preamble[0]);
154 length = AV_RL16(&preamble[2]);
155
156 switch (type) {
157 case MM_TYPE_RAW :
158 case MM_TYPE_PALETTE :
159 case MM_TYPE_INTER :
160 case MM_TYPE_INTRA :
161 case MM_TYPE_INTRA_HH :
162 case MM_TYPE_INTER_HH :
163 case MM_TYPE_INTRA_HHV :
164 case MM_TYPE_INTER_HHV :
165 /* output preamble + data */
166 if ((ret = av_new_packet(pkt, length + MM_PREAMBLE_SIZE)) < 0)
167 return ret;
168 memcpy(pkt->data, preamble, MM_PREAMBLE_SIZE);
169 ret = ffio_read_size(pb, pkt->data + MM_PREAMBLE_SIZE, length);
170 if (ret < 0)
171 return ret;
172 pkt->size = length + MM_PREAMBLE_SIZE;
173 pkt->stream_index = 0;
175 pkt->duration = 1;
176 if (type == MM_TYPE_RAW ||
178 pkt->flags |= AV_PKT_FLAG_KEY;
179 pkt->pos = pos;
180 return 0;
181
182 case MM_TYPE_AUDIO :
183 if (s->nb_streams < 2)
184 return AVERROR_INVALIDDATA;
185 if ((ret = av_get_packet(s->pb, pkt, length)) < 0)
186 return ret;
187 pkt->stream_index = 1;
188 pkt->duration = length;
189 pkt->pos = pos;
190 return 0;
191
192 default :
193 av_log(s, AV_LOG_INFO, "unknown chunk type 0x%x\n", type);
195 case MM_TYPE_AUDIO2 :
196 avio_skip(pb, length);
197 }
198 }
199}
200
202 .p.name = "mm",
203 .p.long_name = NULL_IF_CONFIG_SMALL("American Laser Games MM"),
204 .p.flags = AVFMT_GENERIC_INDEX,
205 .read_probe = probe,
206 .read_header = read_header,
207 .read_packet = read_packet,
208};
static int probe(const AVProbeData *p)
Definition act.c:39
const FFInputFormat ff_mm_demuxer
Definition mm.c:201
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_PCM_U8
Definition codec_id.h:335
@ AV_CODEC_ID_MMVIDEO
Definition codec_id.h:130
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_INFO
Standard information.
Definition log.h:221
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
cl_device_type type
#define AV_RL32(p)
#define AV_RL16(p)
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
uint8_t w
Definition llvidencdsp.c:39
#define MM_HEADER_LEN_V
Definition mm.c:56
#define MM_TYPE_AUDIO
Definition mm.c:53
#define MM_TYPE_HEADER
Definition mm.c:44
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition mm.c:136
#define MM_HEADER_LEN_AV2
Definition mm.c:58
#define MM_TYPE_AUDIO2
Definition mm.c:52
static int read_header(AVFormatContext *s)
Definition mm.c:87
static int probe(const AVProbeData *p)
Definition mm.c:63
#define MM_HEADER_LEN_AV
Definition mm.c:57
#define MM_TYPE_INTRA_HHV
Definition mmvideo.c:47
#define MM_PREAMBLE_SIZE
Definition mmvideo.c:40
#define MM_TYPE_INTER
Definition mmvideo.c:43
#define MM_TYPE_PALETTE
Definition mmvideo.c:49
#define MM_TYPE_INTRA
Definition mmvideo.c:44
#define MM_TYPE_INTRA_HH
Definition mmvideo.c:45
#define MM_TYPE_INTER_HHV
Definition mmvideo.c:48
#define MM_TYPE_RAW
Definition mmvideo.c:42
#define MM_TYPE_INTER_HH
Definition mmvideo.c:46
unsigned int pos
Definition spdifenc.c:431
An AVChannelLayout holds information about the channel layout of audio data.
int height
The height of the video frame in pixels.
Definition codec_par.h:150
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
#define av_log(a,...)
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
int len