Replace 5 with AOT_SBR when referring to the MPEG-4 audio object type.
[FFMpeg-mirror/lagarith.git] / libavcodec / mmvideo.c
blob6dc95589c6df3a79bc80f0aa45dfc4708cda677f
1 /*
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
22 /**
23 * @file libavcodec/mmvideo.c
24 * American Laser Games MM Video Decoder
25 * by Peter Ross (suxen_drol at hotmail dot com)
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"
35 #include "avcodec.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;
49 AVFrame frame;
50 int palette[AVPALETTE_COUNT];
51 } MmContext;
53 static av_cold int mm_decode_init(AVCodecContext *avctx)
55 MmContext *s = avctx->priv_data;
57 s->avctx = avctx;
59 avctx->pix_fmt = PIX_FMT_PAL8;
61 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height))
62 return -1;
64 s->frame.reference = 1;
65 if (avctx->get_buffer(avctx, &s->frame)) {
66 av_log(s->avctx, AV_LOG_ERROR, "mmvideo: get_buffer() failed\n");
67 return -1;
70 return 0;
73 static void mm_decode_pal(MmContext *s, const uint8_t *buf, const uint8_t *buf_end)
75 int i;
76 buf += 4;
77 for (i=0; i<128 && buf+2<buf_end; i++) {
78 s->palette[i] = AV_RB24(buf);
79 s->palette[i+128] = s->palette[i]<<2;
80 buf += 3;
84 static void mm_decode_intra(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
86 int i, x, y;
87 i=0; x=0; y=0;
89 while(i<buf_size) {
90 int run_length, color;
92 if (buf[i] & 0x80) {
93 run_length = 1;
94 color = buf[i];
95 i++;
96 }else{
97 run_length = (buf[i] & 0x7f) + 2;
98 color = buf[i+1];
99 i+=2;
102 if (half_horiz)
103 run_length *=2;
105 if (color) {
106 memset(s->frame.data[0] + y*s->frame.linesize[0] + x, color, run_length);
107 if (half_vert)
108 memset(s->frame.data[0] + (y+1)*s->frame.linesize[0] + x, color, run_length);
110 x+= run_length;
112 if (x >= s->avctx->width) {
113 x=0;
114 y += half_vert ? 2 : 1;
119 static void mm_decode_inter(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
121 const int data_ptr = 2 + AV_RL16(&buf[0]);
122 int d, r, y;
123 d = data_ptr; r = 2; y = 0;
125 while(r < data_ptr) {
126 int i, j;
127 int length = buf[r] & 0x7f;
128 int x = buf[r+1] + ((buf[r] & 0x80) << 1);
129 r += 2;
131 if (length==0) {
132 y += x;
133 continue;
136 for(i=0; i<length; i++) {
137 for(j=0; j<8; j++) {
138 int replace = (buf[r+i] >> (7-j)) & 1;
139 if (replace) {
140 int color = buf[d];
141 s->frame.data[0][y*s->frame.linesize[0] + x] = color;
142 if (half_horiz)
143 s->frame.data[0][y*s->frame.linesize[0] + x + 1] = color;
144 if (half_vert) {
145 s->frame.data[0][(y+1)*s->frame.linesize[0] + x] = color;
146 if (half_horiz)
147 s->frame.data[0][(y+1)*s->frame.linesize[0] + x + 1] = color;
149 d++;
151 x += half_horiz ? 2 : 1;
155 r += length;
156 y += half_vert ? 2 : 1;
160 static int mm_decode_frame(AVCodecContext *avctx,
161 void *data, int *data_size,
162 AVPacket *avpkt)
164 const uint8_t *buf = avpkt->data;
165 int buf_size = avpkt->size;
166 MmContext *s = avctx->priv_data;
167 const uint8_t *buf_end = buf+buf_size;
168 int type;
170 type = AV_RL16(&buf[0]);
171 buf += MM_PREAMBLE_SIZE;
172 buf_size -= MM_PREAMBLE_SIZE;
174 switch(type) {
175 case MM_TYPE_PALETTE : mm_decode_pal(s, buf, buf_end); return buf_size;
176 case MM_TYPE_INTRA : mm_decode_intra(s, 0, 0, buf, buf_size); break;
177 case MM_TYPE_INTRA_HH : mm_decode_intra(s, 1, 0, buf, buf_size); break;
178 case MM_TYPE_INTRA_HHV : mm_decode_intra(s, 1, 1, buf, buf_size); break;
179 case MM_TYPE_INTER : mm_decode_inter(s, 0, 0, buf, buf_size); break;
180 case MM_TYPE_INTER_HH : mm_decode_inter(s, 1, 0, buf, buf_size); break;
181 case MM_TYPE_INTER_HHV : mm_decode_inter(s, 1, 1, buf, buf_size); break;
182 default :
183 return -1;
186 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
188 *data_size = sizeof(AVFrame);
189 *(AVFrame*)data = s->frame;
191 return buf_size;
194 static av_cold int mm_decode_end(AVCodecContext *avctx)
196 MmContext *s = avctx->priv_data;
198 if(s->frame.data[0])
199 avctx->release_buffer(avctx, &s->frame);
201 return 0;
204 AVCodec mmvideo_decoder = {
205 "mmvideo",
206 CODEC_TYPE_VIDEO,
207 CODEC_ID_MMVIDEO,
208 sizeof(MmContext),
209 mm_decode_init,
210 NULL,
211 mm_decode_end,
212 mm_decode_frame,
213 CODEC_CAP_DR1,
214 .long_name = NULL_IF_CONFIG_SMALL("American Laser Games MM Video"),