2 * American Laser Games MM Video Decoder
3 * Copyright (c) 2006,2008 Peter Ross
5 * This file is part of FFmpeg.
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.
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.
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
24 * American Laser Games MM Video Decoder
25 * by Peter Ross (pross@xvid.org)
27 * The MM format was used by IBM-PC ports of ALG's "arcade shooter" games,
28 * including Mad Dog McCree and Crime Patrol.
30 * Technical details here:
31 * http://wiki.multimedia.cx/index.php?title=American_Laser_Games_MM
34 #include "libavutil/intreadwrite.h"
37 #define MM_PREAMBLE_SIZE 6
39 #define MM_TYPE_INTER 0x5
40 #define MM_TYPE_INTRA 0x8
41 #define MM_TYPE_INTRA_HH 0xc
42 #define MM_TYPE_INTER_HH 0xd
43 #define MM_TYPE_INTRA_HHV 0xe
44 #define MM_TYPE_INTER_HHV 0xf
45 #define MM_TYPE_PALETTE 0x31
47 typedef struct MmContext {
48 AVCodecContext *avctx;
50 int palette[AVPALETTE_COUNT];
53 static av_cold int mm_decode_init(AVCodecContext *avctx)
55 MmContext *s = avctx->priv_data;
59 avctx->pix_fmt = PIX_FMT_PAL8;
61 avcodec_get_frame_defaults(&s->frame);
62 s->frame.reference = 1;
67 static void mm_decode_pal(MmContext *s, const uint8_t *buf, const uint8_t *buf_end)
71 for (i=0; i<128 && buf+2<buf_end; i++) {
72 s->palette[i] = AV_RB24(buf);
73 s->palette[i+128] = s->palette[i]<<2;
79 * @param half_horiz Half horizontal resolution (0 or 1)
80 * @param half_vert Half vertical resolution (0 or 1)
82 static void mm_decode_intra(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
88 int run_length, color;
90 if (y >= s->avctx->height)
98 run_length = (buf[i] & 0x7f) + 2;
107 memset(s->frame.data[0] + y*s->frame.linesize[0] + x, color, run_length);
109 memset(s->frame.data[0] + (y+1)*s->frame.linesize[0] + x, color, run_length);
113 if (x >= s->avctx->width) {
121 * @param half_horiz Half horizontal resolution (0 or 1)
122 * @param half_vert Half vertical resolution (0 or 1)
124 static void mm_decode_inter(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
126 const int data_ptr = 2 + AV_RL16(&buf[0]);
128 d = data_ptr; r = 2; y = 0;
130 while(r < data_ptr) {
132 int length = buf[r] & 0x7f;
133 int x = buf[r+1] + ((buf[r] & 0x80) << 1);
141 if (y + half_vert >= s->avctx->height)
144 for(i=0; i<length; i++) {
146 int replace = (buf[r+i] >> (7-j)) & 1;
149 s->frame.data[0][y*s->frame.linesize[0] + x] = color;
151 s->frame.data[0][y*s->frame.linesize[0] + x + 1] = color;
153 s->frame.data[0][(y+1)*s->frame.linesize[0] + x] = color;
155 s->frame.data[0][(y+1)*s->frame.linesize[0] + x + 1] = color;
168 static int mm_decode_frame(AVCodecContext *avctx,
169 void *data, int *data_size,
172 const uint8_t *buf = avpkt->data;
173 int buf_size = avpkt->size;
174 MmContext *s = avctx->priv_data;
175 const uint8_t *buf_end = buf+buf_size;
178 type = AV_RL16(&buf[0]);
179 buf += MM_PREAMBLE_SIZE;
180 buf_size -= MM_PREAMBLE_SIZE;
182 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
183 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
188 case MM_TYPE_PALETTE : mm_decode_pal(s, buf, buf_end); return buf_size;
189 case MM_TYPE_INTRA : mm_decode_intra(s, 0, 0, buf, buf_size); break;
190 case MM_TYPE_INTRA_HH : mm_decode_intra(s, 1, 0, buf, buf_size); break;
191 case MM_TYPE_INTRA_HHV : mm_decode_intra(s, 1, 1, buf, buf_size); break;
192 case MM_TYPE_INTER : mm_decode_inter(s, 0, 0, buf, buf_size); break;
193 case MM_TYPE_INTER_HH : mm_decode_inter(s, 1, 0, buf, buf_size); break;
194 case MM_TYPE_INTER_HHV : mm_decode_inter(s, 1, 1, buf, buf_size); break;
199 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
201 *data_size = sizeof(AVFrame);
202 *(AVFrame*)data = s->frame;
207 static av_cold int mm_decode_end(AVCodecContext *avctx)
209 MmContext *s = avctx->priv_data;
212 avctx->release_buffer(avctx, &s->frame);
217 AVCodec ff_mmvideo_decoder = {
219 .type = AVMEDIA_TYPE_VIDEO,
220 .id = CODEC_ID_MMVIDEO,
221 .priv_data_size = sizeof(MmContext),
222 .init = mm_decode_init,
223 .close = mm_decode_end,
224 .decode = mm_decode_frame,
225 .capabilities = CODEC_CAP_DR1,
226 .long_name = NULL_IF_CONFIG_SMALL("American Laser Games MM Video"),