FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mm.c
Date: 2026-05-02 03:33:10
Exec Total Coverage
Lines: 73 88 83.0%
Functions: 3 3 100.0%
Branches: 34 58 58.6%

Line Branch Exec Source
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
34 #include "libavutil/attributes.h"
35 #include "libavutil/channel_layout.h"
36 #include "libavutil/intreadwrite.h"
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
63 7480 static int probe(const AVProbeData *p)
64 {
65 int len, type, fps, w, h;
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7480 times.
7480 if (p->buf_size < MM_HEADER_LEN_AV + MM_PREAMBLE_SIZE)
67 return 0;
68 /* the first chunk is always the header */
69
2/2
✓ Branch 0 taken 6037 times.
✓ Branch 1 taken 1443 times.
7480 if (AV_RL16(&p->buf[0]) != MM_TYPE_HEADER)
70 6037 return 0;
71 1443 len = AV_RL32(&p->buf[2]);
72
4/6
✓ Branch 0 taken 1443 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1442 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1442 times.
✗ Branch 5 not taken.
1443 if (len != MM_HEADER_LEN_V && len != MM_HEADER_LEN_AV && len != MM_HEADER_LEN_AV2)
73 1442 return 0;
74 1 fps = AV_RL16(&p->buf[8]);
75 1 w = AV_RL16(&p->buf[12]);
76 1 h = AV_RL16(&p->buf[14]);
77
6/12
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
1 if (!fps || fps > 60 || !w || w > 2048 || !h || h > 2048)
78 return 0;
79 1 type = AV_RL16(&p->buf[len]);
80
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!type || type > 0x31)
81 return 0;
82
83 /* only return half certainty since this check is a bit sketchy */
84 1 return AVPROBE_SCORE_EXTENSION;
85 }
86
87 1 static int read_header(AVFormatContext *s)
88 {
89 1 AVIOContext *pb = s->pb;
90 AVStream *st;
91
92 unsigned int type, length;
93 unsigned int frame_rate, width, height;
94
95 1 type = avio_rl16(pb);
96 1 length = avio_rl32(pb);
97
98
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (type != MM_TYPE_HEADER || length < 10)
99 return AVERROR_INVALIDDATA;
100
101 /* read header */
102 1 avio_rl16(pb); /* total number of chunks */
103 1 frame_rate = avio_rl16(pb);
104 1 avio_rl16(pb); /* ibm-pc video bios mode */
105 1 width = avio_rl16(pb);
106 1 height = avio_rl16(pb);
107 1 avio_skip(pb, length - 10); /* unknown data */
108
109 /* video stream */
110 1 st = avformat_new_stream(s, NULL);
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!st)
112 return AVERROR(ENOMEM);
113 1 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
114 1 st->codecpar->codec_id = AV_CODEC_ID_MMVIDEO;
115 1 st->codecpar->codec_tag = 0; /* no fourcc */
116 1 st->codecpar->width = width;
117 1 st->codecpar->height = height;
118 1 avpriv_set_pts_info(st, 64, 1, frame_rate);
119
120 /* audio stream */
121
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (length >= MM_HEADER_LEN_AV) {
122 1 st = avformat_new_stream(s, NULL);
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!st)
124 return AVERROR(ENOMEM);
125 1 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
126 1 st->codecpar->codec_tag = 0; /* no fourcc */
127 1 st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
128 1 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
129 1 st->codecpar->sample_rate = 8000;
130 1 avpriv_set_pts_info(st, 64, 1, 8000); /* 8000 hz */
131 }
132
133 1 return 0;
134 }
135
136 100 static int read_packet(AVFormatContext *s,
137 AVPacket *pkt)
138 {
139 100 AVIOContext *pb = s->pb;
140 unsigned char preamble[MM_PREAMBLE_SIZE];
141 unsigned int type, length;
142 100 int64_t pos = avio_tell(pb);
143 int ret;
144
145 while (1) {
146
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 100 times.
100 if (avio_feof(pb))
147 return AVERROR_EOF;
148
149 100 ret = ffio_read_size(pb, preamble, MM_PREAMBLE_SIZE);
150
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 99 times.
100 if (ret < 0)
151 1 return ret;
152
153 99 type = AV_RL16(&preamble[0]);
154 99 length = AV_RL16(&preamble[2]);
155
156
2/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 67 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
99 switch (type) {
157 32 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
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
32 if ((ret = av_new_packet(pkt, length + MM_PREAMBLE_SIZE)) < 0)
167 return ret;
168 32 memcpy(pkt->data, preamble, MM_PREAMBLE_SIZE);
169 32 ret = ffio_read_size(pb, pkt->data + MM_PREAMBLE_SIZE, length);
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
171 return ret;
172 32 pkt->size = length + MM_PREAMBLE_SIZE;
173 32 pkt->stream_index = 0;
174
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 1 times.
32 if (type!=MM_TYPE_PALETTE)
175 31 pkt->duration = 1;
176
3/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 1 times.
32 if (type == MM_TYPE_RAW ||
177 type == MM_TYPE_INTRA)
178 31 pkt->flags |= AV_PKT_FLAG_KEY;
179 32 pkt->pos = pos;
180 32 return 0;
181
182 67 case MM_TYPE_AUDIO :
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if (s->nb_streams < 2)
184 return AVERROR_INVALIDDATA;
185
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 67 times.
67 if ((ret = av_get_packet(s->pb, pkt, length)) < 0)
186 return ret;
187 67 pkt->stream_index = 1;
188 67 pkt->duration = length;
189 67 pkt->pos = pos;
190 67 return 0;
191
192 default :
193 av_log(s, AV_LOG_INFO, "unknown chunk type 0x%x\n", type);
194 av_fallthrough;
195 case MM_TYPE_AUDIO2 :
196 avio_skip(pb, length);
197 }
198 }
199 }
200
201 const FFInputFormat ff_mm_demuxer = {
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 };
209