2006-02-12 07:49:40 +01:00
|
|
|
/*
|
|
|
|
* American Laser Games MM Format Demuxer
|
|
|
|
* Copyright (c) 2006 Peter Ross
|
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2006-02-12 07:49:40 +01:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 17:30:46 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2006-02-12 07:49:40 +01:00
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2006-02-12 07:49:40 +01: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 17:30:46 +02:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2007-07-05 12:40:25 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-02-12 07:49:40 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 16:45:34 +02:00
|
|
|
* @file
|
2006-02-12 07:49:40 +01:00
|
|
|
* American Laser Games MM Format Demuxer
|
2010-01-09 04:31:13 +01:00
|
|
|
* by Peter Ross (pross@xvid.org)
|
2006-02-12 07:49:40 +01: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
|
|
|
|
*/
|
|
|
|
|
2009-01-11 23:19:48 +01:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2006-02-12 07:49:40 +01:00
|
|
|
#include "avformat.h"
|
|
|
|
|
|
|
|
#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)
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned int audio_pts, video_pts;
|
|
|
|
} MmDemuxContext;
|
|
|
|
|
2010-03-09 13:37:13 +01:00
|
|
|
static int probe(AVProbeData *p)
|
2006-02-12 07:49:40 +01:00
|
|
|
{
|
2009-09-15 11:21:29 +02:00
|
|
|
int len, type, fps, w, h;
|
|
|
|
if (p->buf_size < MM_HEADER_LEN_AV + MM_PREAMBLE_SIZE)
|
|
|
|
return 0;
|
2006-02-12 07:49:40 +01:00
|
|
|
/* the first chunk is always the header */
|
2007-01-19 23:12:59 +01:00
|
|
|
if (AV_RL16(&p->buf[0]) != MM_TYPE_HEADER)
|
2006-02-12 07:49:40 +01:00
|
|
|
return 0;
|
2009-09-15 11:21:29 +02: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 07:49:40 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* only return half certainty since this check is a bit sketchy */
|
|
|
|
return AVPROBE_SCORE_MAX / 2;
|
|
|
|
}
|
|
|
|
|
2010-03-09 13:37:13 +01:00
|
|
|
static int read_header(AVFormatContext *s,
|
2006-02-12 07:49:40 +01:00
|
|
|
AVFormatParameters *ap)
|
|
|
|
{
|
2007-04-08 22:24:16 +02:00
|
|
|
MmDemuxContext *mm = s->priv_data;
|
2007-11-21 08:41:00 +01:00
|
|
|
ByteIOContext *pb = s->pb;
|
2006-02-12 07:49:40 +01:00
|
|
|
AVStream *st;
|
|
|
|
|
|
|
|
unsigned int type, length;
|
|
|
|
unsigned int frame_rate, width, height;
|
|
|
|
|
|
|
|
type = get_le16(pb);
|
|
|
|
length = get_le32(pb);
|
|
|
|
|
|
|
|
if (type != MM_TYPE_HEADER)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
/* read header */
|
|
|
|
get_le16(pb); /* total number of chunks */
|
|
|
|
frame_rate = get_le16(pb);
|
|
|
|
get_le16(pb); /* ibm-pc video bios mode */
|
|
|
|
width = get_le16(pb);
|
|
|
|
height = get_le16(pb);
|
|
|
|
url_fseek(pb, length - 10, SEEK_CUR); /* unknown data */
|
|
|
|
|
|
|
|
/* video stream */
|
|
|
|
st = av_new_stream(s, 0);
|
|
|
|
if (!st)
|
2007-07-19 17:21:30 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2010-03-31 01:30:55 +02:00
|
|
|
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
|
2006-02-12 07:49:40 +01:00
|
|
|
st->codec->codec_id = CODEC_ID_MMVIDEO;
|
|
|
|
st->codec->codec_tag = 0; /* no fourcc */
|
|
|
|
st->codec->width = width;
|
|
|
|
st->codec->height = height;
|
|
|
|
av_set_pts_info(st, 64, 1, frame_rate);
|
|
|
|
|
|
|
|
/* audio stream */
|
|
|
|
if (length == MM_HEADER_LEN_AV) {
|
|
|
|
st = av_new_stream(s, 0);
|
|
|
|
if (!st)
|
2007-07-19 17:21:30 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2010-03-31 01:30:55 +02:00
|
|
|
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
|
2006-02-12 07:49:40 +01:00
|
|
|
st->codec->codec_tag = 0; /* no fourcc */
|
|
|
|
st->codec->codec_id = CODEC_ID_PCM_U8;
|
|
|
|
st->codec->channels = 1;
|
|
|
|
st->codec->sample_rate = 8000;
|
|
|
|
av_set_pts_info(st, 64, 1, 8000); /* 8000 hz */
|
|
|
|
}
|
|
|
|
|
|
|
|
mm->audio_pts = 0;
|
|
|
|
mm->video_pts = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-09 13:37:13 +01:00
|
|
|
static int read_packet(AVFormatContext *s,
|
2006-02-12 07:49:40 +01:00
|
|
|
AVPacket *pkt)
|
|
|
|
{
|
2007-04-08 22:24:16 +02:00
|
|
|
MmDemuxContext *mm = s->priv_data;
|
2007-11-21 08:41:00 +01:00
|
|
|
ByteIOContext *pb = s->pb;
|
2006-02-12 07:49:40 +01:00
|
|
|
unsigned char preamble[MM_PREAMBLE_SIZE];
|
|
|
|
unsigned int type, length;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
|
|
|
|
if (get_buffer(pb, preamble, MM_PREAMBLE_SIZE) != MM_PREAMBLE_SIZE) {
|
2007-07-19 17:23:32 +02:00
|
|
|
return AVERROR(EIO);
|
2006-02-12 07:49:40 +01:00
|
|
|
}
|
|
|
|
|
2007-01-19 23:12:59 +01:00
|
|
|
type = AV_RL16(&preamble[0]);
|
|
|
|
length = AV_RL16(&preamble[2]);
|
2006-02-12 07:49:40 +01: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 */
|
|
|
|
if (av_new_packet(pkt, length + MM_PREAMBLE_SIZE))
|
2007-07-19 17:21:30 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2006-02-12 07:49:40 +01:00
|
|
|
memcpy(pkt->data, preamble, MM_PREAMBLE_SIZE);
|
|
|
|
if (get_buffer(pb, pkt->data + MM_PREAMBLE_SIZE, length) != length)
|
2007-07-19 17:23:32 +02:00
|
|
|
return AVERROR(EIO);
|
2006-02-12 07:49:40 +01:00
|
|
|
pkt->size = length + MM_PREAMBLE_SIZE;
|
|
|
|
pkt->stream_index = 0;
|
2008-07-08 14:44:08 +02:00
|
|
|
pkt->pts = mm->video_pts;
|
|
|
|
if (type!=MM_TYPE_PALETTE)
|
|
|
|
mm->video_pts++;
|
2006-02-12 07:49:40 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case MM_TYPE_AUDIO :
|
2007-11-21 08:41:00 +01:00
|
|
|
if (av_get_packet(s->pb, pkt, length)<0)
|
2007-07-19 17:21:30 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2006-02-12 07:49:40 +01:00
|
|
|
pkt->size = length;
|
|
|
|
pkt->stream_index = 1;
|
|
|
|
pkt->pts = mm->audio_pts++;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default :
|
2009-02-16 17:27:08 +01:00
|
|
|
av_log(s, AV_LOG_INFO, "unknown chunk type 0x%x\n", type);
|
2006-02-12 07:49:40 +01:00
|
|
|
url_fseek(pb, length, SEEK_CUR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 23:03:28 +01:00
|
|
|
AVInputFormat ff_mm_demuxer = {
|
2006-02-12 07:49:40 +01:00
|
|
|
"mm",
|
2008-06-03 18:20:54 +02:00
|
|
|
NULL_IF_CONFIG_SMALL("American Laser Games MM format"),
|
2006-02-12 07:49:40 +01:00
|
|
|
sizeof(MmDemuxContext),
|
2010-03-09 13:37:13 +01:00
|
|
|
probe,
|
|
|
|
read_header,
|
|
|
|
read_packet,
|
2006-02-12 07:49:40 +01:00
|
|
|
};
|