FFmpeg
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 
35 #include "libavutil/intreadwrite.h"
36 #include "avformat.h"
37 #include "demux.h"
38 #include "internal.h"
39 
40 #define MM_PREAMBLE_SIZE 6
41 
42 #define MM_TYPE_HEADER 0x0
43 #define MM_TYPE_RAW 0x2
44 #define MM_TYPE_INTER 0x5
45 #define MM_TYPE_INTRA 0x8
46 #define MM_TYPE_INTRA_HH 0xc
47 #define MM_TYPE_INTER_HH 0xd
48 #define MM_TYPE_INTRA_HHV 0xe
49 #define MM_TYPE_INTER_HHV 0xf
50 #define MM_TYPE_AUDIO2 0x14
51 #define MM_TYPE_AUDIO 0x15
52 #define MM_TYPE_PALETTE 0x31
53 
54 #define MM_HEADER_LEN_V 0x16 /* video only */
55 #define MM_HEADER_LEN_AV 0x18 /* video + audio */
56 #define MM_HEADER_LEN_AV2 0x1a
57 
58 #define MM_PALETTE_COUNT 128
59 #define MM_PALETTE_SIZE (MM_PALETTE_COUNT*3)
60 
61 static int probe(const AVProbeData *p)
62 {
63  int len, type, fps, w, h;
65  return 0;
66  /* the first chunk is always the header */
67  if (AV_RL16(&p->buf[0]) != MM_TYPE_HEADER)
68  return 0;
69  len = AV_RL32(&p->buf[2]);
71  return 0;
72  fps = AV_RL16(&p->buf[8]);
73  w = AV_RL16(&p->buf[12]);
74  h = AV_RL16(&p->buf[14]);
75  if (!fps || fps > 60 || !w || w > 2048 || !h || h > 2048)
76  return 0;
77  type = AV_RL16(&p->buf[len]);
78  if (!type || type > 0x31)
79  return 0;
80 
81  /* only return half certainty since this check is a bit sketchy */
83 }
84 
86 {
87  AVIOContext *pb = s->pb;
88  AVStream *st;
89 
90  unsigned int type, length;
91  unsigned int frame_rate, width, height;
92 
93  type = avio_rl16(pb);
94  length = avio_rl32(pb);
95 
96  if (type != MM_TYPE_HEADER || length < 10)
97  return AVERROR_INVALIDDATA;
98 
99  /* read header */
100  avio_rl16(pb); /* total number of chunks */
101  frame_rate = avio_rl16(pb);
102  avio_rl16(pb); /* ibm-pc video bios mode */
103  width = avio_rl16(pb);
104  height = avio_rl16(pb);
105  avio_skip(pb, length - 10); /* unknown data */
106 
107  /* video stream */
108  st = avformat_new_stream(s, NULL);
109  if (!st)
110  return AVERROR(ENOMEM);
113  st->codecpar->codec_tag = 0; /* no fourcc */
114  st->codecpar->width = width;
115  st->codecpar->height = height;
116  avpriv_set_pts_info(st, 64, 1, frame_rate);
117 
118  /* audio stream */
119  if (length >= MM_HEADER_LEN_AV) {
120  st = avformat_new_stream(s, NULL);
121  if (!st)
122  return AVERROR(ENOMEM);
124  st->codecpar->codec_tag = 0; /* no fourcc */
127  st->codecpar->sample_rate = 8000;
128  avpriv_set_pts_info(st, 64, 1, 8000); /* 8000 hz */
129  }
130 
131  return 0;
132 }
133 
135  AVPacket *pkt)
136 {
137  AVIOContext *pb = s->pb;
138  unsigned char preamble[MM_PREAMBLE_SIZE];
139  unsigned int type, length;
140  int64_t pos = avio_tell(pb);
141  int ret;
142 
143  while (1) {
144  if (avio_feof(pb))
145  return AVERROR_EOF;
146 
147  if (avio_read(pb, preamble, MM_PREAMBLE_SIZE) != MM_PREAMBLE_SIZE)
148  return AVERROR(EIO);
149 
150  type = AV_RL16(&preamble[0]);
151  length = AV_RL16(&preamble[2]);
152 
153  switch (type) {
154  case MM_TYPE_RAW :
155  case MM_TYPE_PALETTE :
156  case MM_TYPE_INTER :
157  case MM_TYPE_INTRA :
158  case MM_TYPE_INTRA_HH :
159  case MM_TYPE_INTER_HH :
160  case MM_TYPE_INTRA_HHV :
161  case MM_TYPE_INTER_HHV :
162  /* output preamble + data */
163  if ((ret = av_new_packet(pkt, length + MM_PREAMBLE_SIZE)) < 0)
164  return ret;
165  memcpy(pkt->data, preamble, MM_PREAMBLE_SIZE);
166  if (avio_read(pb, pkt->data + MM_PREAMBLE_SIZE, length) != length)
167  return AVERROR(EIO);
168  pkt->size = length + MM_PREAMBLE_SIZE;
169  pkt->stream_index = 0;
170  if (type!=MM_TYPE_PALETTE)
171  pkt->duration = 1;
172  if (type == MM_TYPE_RAW ||
173  type == MM_TYPE_INTRA)
175  pkt->pos = pos;
176  return 0;
177 
178  case MM_TYPE_AUDIO :
179  if (s->nb_streams < 2)
180  return AVERROR_INVALIDDATA;
181  if ((ret = av_get_packet(s->pb, pkt, length)) < 0)
182  return ret;
183  pkt->stream_index = 1;
184  pkt->duration = length;
185  pkt->pos = pos;
186  return 0;
187 
188  default :
189  av_log(s, AV_LOG_INFO, "unknown chunk type 0x%x\n", type);
190  /* fallthrough */
191  case MM_TYPE_AUDIO2 :
192  avio_skip(pb, length);
193  }
194  }
195 }
196 
198  .p.name = "mm",
199  .p.long_name = NULL_IF_CONFIG_SMALL("American Laser Games MM"),
200  .p.flags = AVFMT_GENERIC_INDEX,
201  .read_probe = probe,
202  .read_header = read_header,
203  .read_packet = read_packet,
204 };
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
read_packet
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mm.c:134
MM_TYPE_INTER_HHV
#define MM_TYPE_INTER_HHV
Definition: mm.c:49
int64_t
long long int64_t
Definition: coverity.c:34
MM_TYPE_INTER
#define MM_TYPE_INTER
Definition: mm.c:44
w
uint8_t w
Definition: llviddspenc.c:38
AVPacket::data
uint8_t * data
Definition: packet.h:539
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:557
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:594
avpriv_set_pts_info
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:867
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:714
MM_TYPE_INTRA_HHV
#define MM_TYPE_INTRA_HHV
Definition: mm.c:48
pkt
AVPacket * pkt
Definition: movenc.c:60
MM_TYPE_INTRA_HH
#define MM_TYPE_INTRA_HH
Definition: mm.c:46
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: packet.c:99
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
MM_TYPE_HEADER
#define MM_TYPE_HEADER
Definition: mm.c:42
probe
static int probe(const AVProbeData *p)
Definition: mm.c:61
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
NULL
#define NULL
Definition: coverity.c:32
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
MM_PREAMBLE_SIZE
#define MM_PREAMBLE_SIZE
Definition: mm.c:40
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:461
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:730
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AVPacket::size
int size
Definition: packet.h:540
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
height
#define height
Definition: dsp.h:85
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:318
MM_TYPE_AUDIO2
#define MM_TYPE_AUDIO2
Definition: mm.c:50
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
AV_CODEC_ID_MMVIDEO
@ AV_CODEC_ID_MMVIDEO
Definition: codec_id.h:132
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:545
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:220
MM_TYPE_PALETTE
#define MM_TYPE_PALETTE
Definition: mm.c:52
MM_HEADER_LEN_AV
#define MM_HEADER_LEN_AV
Definition: mm.c:55
MM_TYPE_RAW
#define MM_TYPE_RAW
Definition: mm.c:43
AVCodecParameters::height
int height
Definition: codec_par.h:135
MM_TYPE_INTER_HH
#define MM_TYPE_INTER_HH
Definition: mm.c:47
demux.h
len
int len
Definition: vorbis_enc_data.h:426
av_get_packet
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:91
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
channel_layout.h
MM_TYPE_INTRA
#define MM_TYPE_INTRA
Definition: mm.c:45
ff_mm_demuxer
const FFInputFormat ff_mm_demuxer
Definition: mm.c:197
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
AVPacket::stream_index
int stream_index
Definition: packet.h:541
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
MM_TYPE_AUDIO
#define MM_TYPE_AUDIO
Definition: mm.c:51
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:339
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:393
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:559
FFInputFormat
Definition: demux.h:42
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
MM_HEADER_LEN_V
#define MM_HEADER_LEN_V
Definition: mm.c:54
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
h
h
Definition: vp9dsp_template.c:2070
width
#define width
Definition: dsp.h:85
read_header
static int read_header(AVFormatContext *s)
Definition: mm.c:85
MM_HEADER_LEN_AV2
#define MM_HEADER_LEN_AV2
Definition: mm.c:56
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346