2006-02-12 06:49:40 +00:00
|
|
|
/*
|
|
|
|
* American Laser Games MM Format Demuxer
|
|
|
|
* Copyright (c) 2006 Peter Ross
|
|
|
|
*
|
2006-10-07 15:30:46 +00:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2006-02-12 06:49:40 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 15:30:46 +00:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2006-02-12 06:49:40 +00:00
|
|
|
*
|
2006-10-07 15:30:46 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2006-02-12 06:49:40 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 15:30:46 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2007-07-05 10:40:25 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-02-12 06:49:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 14:45:34 +00:00
|
|
|
* @file
|
2006-02-12 06:49:40 +00:00
|
|
|
* American Laser Games MM Format Demuxer
|
2010-01-09 03:31:13 +00:00
|
|
|
* by Peter Ross (pross@xvid.org)
|
2006-02-12 06:49:40 +00:00
|
|
|
*
|
|
|
|
* The MM format was used by IBM-PC ports of ALG's "arcade shooter" games,
|
|
|
|
* including Mad Dog McCree and Crime Patrol.
|
|
|
|
*
|
|
|
|
* Technical details here:
|
|
|
|
* http://wiki.multimedia.cx/index.php?title=American_Laser_Games_MM
|
|
|
|
*/
|
|
|
|
|
2012-04-07 20:52:57 +00:00
|
|
|
#include "libavutil/channel_layout.h"
|
2009-01-11 22:19:48 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2006-02-12 06:49:40 +00:00
|
|
|
#include "avformat.h"
|
2011-11-29 18:28:15 +00:00
|
|
|
#include "internal.h"
|
2006-02-12 06:49:40 +00:00
|
|
|
|
|
|
|
#define MM_PREAMBLE_SIZE 6
|
|
|
|
|
|
|
|
#define MM_TYPE_HEADER 0x0
|
|
|
|
#define MM_TYPE_INTER 0x5
|
|
|
|
#define MM_TYPE_INTRA 0x8
|
|
|
|
#define MM_TYPE_INTRA_HH 0xc
|
|
|
|
#define MM_TYPE_INTER_HH 0xd
|
|
|
|
#define MM_TYPE_INTRA_HHV 0xe
|
|
|
|
#define MM_TYPE_INTER_HHV 0xf
|
|
|
|
#define MM_TYPE_AUDIO 0x15
|
|
|
|
#define MM_TYPE_PALETTE 0x31
|
|
|
|
|
|
|
|
#define MM_HEADER_LEN_V 0x16 /* video only */
|
|
|
|
#define MM_HEADER_LEN_AV 0x18 /* video + audio */
|
|
|
|
|
|
|
|
#define MM_PALETTE_COUNT 128
|
|
|
|
#define MM_PALETTE_SIZE (MM_PALETTE_COUNT*3)
|
|
|
|
|
2014-09-22 07:19:33 +00:00
|
|
|
typedef struct MmDemuxContext {
|
2006-02-12 06:49:40 +00:00
|
|
|
unsigned int audio_pts, video_pts;
|
|
|
|
} MmDemuxContext;
|
|
|
|
|
2019-03-21 00:18:37 +00:00
|
|
|
static int probe(const AVProbeData *p)
|
2006-02-12 06:49:40 +00:00
|
|
|
{
|
2009-09-15 09:21:29 +00:00
|
|
|
int len, type, fps, w, h;
|
|
|
|
if (p->buf_size < MM_HEADER_LEN_AV + MM_PREAMBLE_SIZE)
|
|
|
|
return 0;
|
2006-02-12 06:49:40 +00:00
|
|
|
/* the first chunk is always the header */
|
2007-01-19 22:12:59 +00:00
|
|
|
if (AV_RL16(&p->buf[0]) != MM_TYPE_HEADER)
|
2006-02-12 06:49:40 +00:00
|
|
|
return 0;
|
2009-09-15 09:21:29 +00:00
|
|
|
len = AV_RL32(&p->buf[2]);
|
|
|
|
if (len != MM_HEADER_LEN_V && len != MM_HEADER_LEN_AV)
|
|
|
|
return 0;
|
|
|
|
fps = AV_RL16(&p->buf[8]);
|
|
|
|
w = AV_RL16(&p->buf[12]);
|
|
|
|
h = AV_RL16(&p->buf[14]);
|
|
|
|
if (!fps || fps > 60 || !w || w > 2048 || !h || h > 2048)
|
|
|
|
return 0;
|
|
|
|
type = AV_RL16(&p->buf[len]);
|
|
|
|
if (!type || type > 0x31)
|
2006-02-12 06:49:40 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* only return half certainty since this check is a bit sketchy */
|
2013-03-25 15:12:51 +00:00
|
|
|
return AVPROBE_SCORE_EXTENSION;
|
2006-02-12 06:49:40 +00:00
|
|
|
}
|
|
|
|
|
2012-01-12 12:20:36 +00:00
|
|
|
static int read_header(AVFormatContext *s)
|
2006-02-12 06:49:40 +00:00
|
|
|
{
|
2007-04-08 20:24:16 +00:00
|
|
|
MmDemuxContext *mm = s->priv_data;
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2006-02-12 06:49:40 +00:00
|
|
|
AVStream *st;
|
|
|
|
|
|
|
|
unsigned int type, length;
|
|
|
|
unsigned int frame_rate, width, height;
|
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
type = avio_rl16(pb);
|
|
|
|
length = avio_rl32(pb);
|
2006-02-12 06:49:40 +00:00
|
|
|
|
|
|
|
if (type != MM_TYPE_HEADER)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
/* read header */
|
2011-02-21 15:43:01 +00:00
|
|
|
avio_rl16(pb); /* total number of chunks */
|
|
|
|
frame_rate = avio_rl16(pb);
|
|
|
|
avio_rl16(pb); /* ibm-pc video bios mode */
|
|
|
|
width = avio_rl16(pb);
|
|
|
|
height = avio_rl16(pb);
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, length - 10); /* unknown data */
|
2006-02-12 06:49:40 +00:00
|
|
|
|
|
|
|
/* video stream */
|
2011-06-18 09:43:24 +00:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2006-02-12 06:49:40 +00:00
|
|
|
if (!st)
|
2007-07-19 15:21:30 +00:00
|
|
|
return AVERROR(ENOMEM);
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_MMVIDEO;
|
|
|
|
st->codecpar->codec_tag = 0; /* no fourcc */
|
|
|
|
st->codecpar->width = width;
|
|
|
|
st->codecpar->height = height;
|
2011-11-29 18:28:15 +00:00
|
|
|
avpriv_set_pts_info(st, 64, 1, frame_rate);
|
2006-02-12 06:49:40 +00:00
|
|
|
|
|
|
|
/* audio stream */
|
|
|
|
if (length == MM_HEADER_LEN_AV) {
|
2011-06-18 09:43:24 +00:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2006-02-12 06:49:40 +00:00
|
|
|
if (!st)
|
2007-07-19 15:21:30 +00:00
|
|
|
return AVERROR(ENOMEM);
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
st->codecpar->codec_tag = 0; /* no fourcc */
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
|
|
|
|
st->codecpar->channels = 1;
|
|
|
|
st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
|
|
|
|
st->codecpar->sample_rate = 8000;
|
2011-11-29 18:28:15 +00:00
|
|
|
avpriv_set_pts_info(st, 64, 1, 8000); /* 8000 hz */
|
2006-02-12 06:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mm->audio_pts = 0;
|
|
|
|
mm->video_pts = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-09 12:37:13 +00:00
|
|
|
static int read_packet(AVFormatContext *s,
|
2006-02-12 06:49:40 +00:00
|
|
|
AVPacket *pkt)
|
|
|
|
{
|
2007-04-08 20:24:16 +00:00
|
|
|
MmDemuxContext *mm = s->priv_data;
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2006-02-12 06:49:40 +00:00
|
|
|
unsigned char preamble[MM_PREAMBLE_SIZE];
|
|
|
|
unsigned int type, length;
|
2019-12-10 21:59:53 +00:00
|
|
|
int ret;
|
2006-02-12 06:49:40 +00:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_read(pb, preamble, MM_PREAMBLE_SIZE) != MM_PREAMBLE_SIZE) {
|
2007-07-19 15:23:32 +00:00
|
|
|
return AVERROR(EIO);
|
2006-02-12 06:49:40 +00:00
|
|
|
}
|
|
|
|
|
2007-01-19 22:12:59 +00:00
|
|
|
type = AV_RL16(&preamble[0]);
|
|
|
|
length = AV_RL16(&preamble[2]);
|
2006-02-12 06:49:40 +00:00
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case MM_TYPE_PALETTE :
|
|
|
|
case MM_TYPE_INTER :
|
|
|
|
case MM_TYPE_INTRA :
|
|
|
|
case MM_TYPE_INTRA_HH :
|
|
|
|
case MM_TYPE_INTER_HH :
|
|
|
|
case MM_TYPE_INTRA_HHV :
|
|
|
|
case MM_TYPE_INTER_HHV :
|
|
|
|
/* output preamble + data */
|
2019-12-10 21:59:53 +00:00
|
|
|
if ((ret = av_new_packet(pkt, length + MM_PREAMBLE_SIZE)) < 0)
|
|
|
|
return ret;
|
2006-02-12 06:49:40 +00:00
|
|
|
memcpy(pkt->data, preamble, MM_PREAMBLE_SIZE);
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_read(pb, pkt->data + MM_PREAMBLE_SIZE, length) != length)
|
2007-07-19 15:23:32 +00:00
|
|
|
return AVERROR(EIO);
|
2006-02-12 06:49:40 +00:00
|
|
|
pkt->size = length + MM_PREAMBLE_SIZE;
|
|
|
|
pkt->stream_index = 0;
|
2008-07-08 12:44:08 +00:00
|
|
|
pkt->pts = mm->video_pts;
|
|
|
|
if (type!=MM_TYPE_PALETTE)
|
|
|
|
mm->video_pts++;
|
2006-02-12 06:49:40 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case MM_TYPE_AUDIO :
|
2020-07-09 10:07:28 +00:00
|
|
|
if (s->nb_streams < 2)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2019-12-10 21:59:53 +00:00
|
|
|
if ((ret = av_get_packet(s->pb, pkt, length)) < 0)
|
|
|
|
return ret;
|
2006-02-12 06:49:40 +00:00
|
|
|
pkt->stream_index = 1;
|
|
|
|
pkt->pts = mm->audio_pts++;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default :
|
2009-02-16 16:27:08 +00:00
|
|
|
av_log(s, AV_LOG_INFO, "unknown chunk type 0x%x\n", type);
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, length);
|
2006-02-12 06:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:03:28 +00:00
|
|
|
AVInputFormat ff_mm_demuxer = {
|
2011-07-16 20:18:12 +00:00
|
|
|
.name = "mm",
|
2012-07-24 21:51:41 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("American Laser Games MM"),
|
2011-07-16 20:18:12 +00:00
|
|
|
.priv_data_size = sizeof(MmDemuxContext),
|
|
|
|
.read_probe = probe,
|
|
|
|
.read_header = read_header,
|
|
|
|
.read_packet = read_packet,
|
2006-02-12 06:49:40 +00:00
|
|
|
};
|