00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00034 #include "libavutil/intreadwrite.h"
00035 #include "avformat.h"
00036 #include "internal.h"
00037
00038 #define MM_PREAMBLE_SIZE 6
00039
00040 #define MM_TYPE_HEADER 0x0
00041 #define MM_TYPE_INTER 0x5
00042 #define MM_TYPE_INTRA 0x8
00043 #define MM_TYPE_INTRA_HH 0xc
00044 #define MM_TYPE_INTER_HH 0xd
00045 #define MM_TYPE_INTRA_HHV 0xe
00046 #define MM_TYPE_INTER_HHV 0xf
00047 #define MM_TYPE_AUDIO 0x15
00048 #define MM_TYPE_PALETTE 0x31
00049
00050 #define MM_HEADER_LEN_V 0x16
00051 #define MM_HEADER_LEN_AV 0x18
00052
00053 #define MM_PALETTE_COUNT 128
00054 #define MM_PALETTE_SIZE (MM_PALETTE_COUNT*3)
00055
00056 typedef struct {
00057 unsigned int audio_pts, video_pts;
00058 } MmDemuxContext;
00059
00060 static int probe(AVProbeData *p)
00061 {
00062 int len, type, fps, w, h;
00063 if (p->buf_size < MM_HEADER_LEN_AV + MM_PREAMBLE_SIZE)
00064 return 0;
00065
00066 if (AV_RL16(&p->buf[0]) != MM_TYPE_HEADER)
00067 return 0;
00068 len = AV_RL32(&p->buf[2]);
00069 if (len != MM_HEADER_LEN_V && len != MM_HEADER_LEN_AV)
00070 return 0;
00071 fps = AV_RL16(&p->buf[8]);
00072 w = AV_RL16(&p->buf[12]);
00073 h = AV_RL16(&p->buf[14]);
00074 if (!fps || fps > 60 || !w || w > 2048 || !h || h > 2048)
00075 return 0;
00076 type = AV_RL16(&p->buf[len]);
00077 if (!type || type > 0x31)
00078 return 0;
00079
00080
00081 return AVPROBE_SCORE_MAX / 2;
00082 }
00083
00084 static int read_header(AVFormatContext *s)
00085 {
00086 MmDemuxContext *mm = s->priv_data;
00087 AVIOContext *pb = s->pb;
00088 AVStream *st;
00089
00090 unsigned int type, length;
00091 unsigned int frame_rate, width, height;
00092
00093 type = avio_rl16(pb);
00094 length = avio_rl32(pb);
00095
00096 if (type != MM_TYPE_HEADER)
00097 return AVERROR_INVALIDDATA;
00098
00099
00100 avio_rl16(pb);
00101 frame_rate = avio_rl16(pb);
00102 avio_rl16(pb);
00103 width = avio_rl16(pb);
00104 height = avio_rl16(pb);
00105 avio_skip(pb, length - 10);
00106
00107
00108 st = avformat_new_stream(s, NULL);
00109 if (!st)
00110 return AVERROR(ENOMEM);
00111 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00112 st->codec->codec_id = AV_CODEC_ID_MMVIDEO;
00113 st->codec->codec_tag = 0;
00114 st->codec->width = width;
00115 st->codec->height = height;
00116 avpriv_set_pts_info(st, 64, 1, frame_rate);
00117
00118
00119 if (length == MM_HEADER_LEN_AV) {
00120 st = avformat_new_stream(s, NULL);
00121 if (!st)
00122 return AVERROR(ENOMEM);
00123 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00124 st->codec->codec_tag = 0;
00125 st->codec->codec_id = AV_CODEC_ID_PCM_U8;
00126 st->codec->channels = 1;
00127 st->codec->sample_rate = 8000;
00128 avpriv_set_pts_info(st, 64, 1, 8000);
00129 }
00130
00131 mm->audio_pts = 0;
00132 mm->video_pts = 0;
00133 return 0;
00134 }
00135
00136 static int read_packet(AVFormatContext *s,
00137 AVPacket *pkt)
00138 {
00139 MmDemuxContext *mm = s->priv_data;
00140 AVIOContext *pb = s->pb;
00141 unsigned char preamble[MM_PREAMBLE_SIZE];
00142 unsigned int type, length;
00143
00144 while(1) {
00145
00146 if (avio_read(pb, preamble, MM_PREAMBLE_SIZE) != MM_PREAMBLE_SIZE) {
00147 return AVERROR(EIO);
00148 }
00149
00150 type = AV_RL16(&preamble[0]);
00151 length = AV_RL16(&preamble[2]);
00152
00153 switch(type) {
00154 case MM_TYPE_PALETTE :
00155 case MM_TYPE_INTER :
00156 case MM_TYPE_INTRA :
00157 case MM_TYPE_INTRA_HH :
00158 case MM_TYPE_INTER_HH :
00159 case MM_TYPE_INTRA_HHV :
00160 case MM_TYPE_INTER_HHV :
00161
00162 if (av_new_packet(pkt, length + MM_PREAMBLE_SIZE))
00163 return AVERROR(ENOMEM);
00164 memcpy(pkt->data, preamble, MM_PREAMBLE_SIZE);
00165 if (avio_read(pb, pkt->data + MM_PREAMBLE_SIZE, length) != length)
00166 return AVERROR(EIO);
00167 pkt->size = length + MM_PREAMBLE_SIZE;
00168 pkt->stream_index = 0;
00169 pkt->pts = mm->video_pts;
00170 if (type!=MM_TYPE_PALETTE)
00171 mm->video_pts++;
00172 return 0;
00173
00174 case MM_TYPE_AUDIO :
00175 if (av_get_packet(s->pb, pkt, length)<0)
00176 return AVERROR(ENOMEM);
00177 pkt->stream_index = 1;
00178 pkt->pts = mm->audio_pts++;
00179 return 0;
00180
00181 default :
00182 av_log(s, AV_LOG_INFO, "unknown chunk type 0x%x\n", type);
00183 avio_skip(pb, length);
00184 }
00185 }
00186 }
00187
00188 AVInputFormat ff_mm_demuxer = {
00189 .name = "mm",
00190 .long_name = NULL_IF_CONFIG_SMALL("American Laser Games MM"),
00191 .priv_data_size = sizeof(MmDemuxContext),
00192 .read_probe = probe,
00193 .read_header = read_header,
00194 .read_packet = read_packet,
00195 };