]> git.sesse.net Git - ffmpeg/blob - libavcodec/mmvideo.c
Chinese AVS decoder
[ffmpeg] / libavcodec / mmvideo.c
1 /*
2  * American Laser Games MM Video Decoder
3  * Copyright (c) 2006 Peter Ross
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 /**
21  * @file mm.c
22  * American Laser Games MM Video Decoder
23  * by Peter Ross (suxen_drol at hotmail dot com)
24  *
25  * The MM format was used by IBM-PC ports of ALG's "arcade shooter" games,
26  * including Mad Dog McCree and Crime Patrol.
27  *
28  * Technical details here:
29  *  http://wiki.multimedia.cx/index.php?title=American_Laser_Games_MM
30  */
31
32 #include "avcodec.h"
33
34 #define MM_PREAMBLE_SIZE    6
35
36 #define MM_TYPE_INTER       0x5
37 #define MM_TYPE_INTRA       0x8
38 #define MM_TYPE_INTRA_HH    0xc
39 #define MM_TYPE_INTER_HH    0xd
40 #define MM_TYPE_INTRA_HHV   0xe
41 #define MM_TYPE_INTER_HHV   0xf
42
43 typedef struct MmContext {
44     AVCodecContext *avctx;
45     AVFrame frame;
46 } MmContext;
47
48 static int mm_decode_init(AVCodecContext *avctx)
49 {
50     MmContext *s = avctx->priv_data;
51
52     s->avctx = avctx;
53
54     if (s->avctx->palctrl == NULL) {
55         av_log(avctx, AV_LOG_ERROR, "mmvideo: palette expected.\n");
56         return -1;
57     }
58
59     avctx->pix_fmt = PIX_FMT_PAL8;
60     avctx->has_b_frames = 0;
61
62     if (avcodec_check_dimensions(avctx, avctx->width, avctx->height))
63         return -1;
64
65     s->frame.reference = 1;
66     if (avctx->get_buffer(avctx, &s->frame)) {
67         av_log(s->avctx, AV_LOG_ERROR, "mmvideo: get_buffer() failed\n");
68         return -1;
69     }
70
71     return 0;
72 }
73
74 static void mm_decode_intra(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
75 {
76     int i, x, y;
77     i=0; x=0; y=0;
78
79     while(i<buf_size) {
80         int run_length, color;
81
82         if (buf[i] & 0x80) {
83             run_length = 1;
84             color = buf[i];
85             i++;
86         }else{
87             run_length = (buf[i] & 0x7f) + 2;
88             color = buf[i+1];
89             i+=2;
90         }
91
92         if (half_horiz)
93             run_length *=2;
94
95         if (color) {
96             memset(s->frame.data[0] + y*s->frame.linesize[0] + x, color, run_length);
97             if (half_vert)
98                 memset(s->frame.data[0] + (y+1)*s->frame.linesize[0] + x, color, run_length);
99         }
100         x+= run_length;
101
102         if (x >= s->avctx->width) {
103             x=0;
104             y += half_vert ? 2 : 1;
105         }
106     }
107 }
108
109 static void mm_decode_inter(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
110 {
111     const int data_ptr = 2 + LE_16(&buf[0]);
112     int d, r, y;
113     d = data_ptr; r = 2; y = 0;
114
115     while(r < data_ptr) {
116         int i, j;
117         int length = buf[r] & 0x7f;
118         int x = buf[r+1] + ((buf[r] & 0x80) << 1);
119         r += 2;
120
121         if (length==0) {
122             y += x;
123             continue;
124         }
125
126         for(i=0; i<length; i++) {
127             for(j=0; j<8; j++) {
128                 int replace = (buf[r+i] >> (7-j)) & 1;
129                 if (replace) {
130                     int color = buf[d];
131                     s->frame.data[0][y*s->frame.linesize[0] + x] = color;
132                     if (half_horiz)
133                         s->frame.data[0][y*s->frame.linesize[0] + x + 1] = color;
134                     if (half_vert) {
135                         s->frame.data[0][(y+1)*s->frame.linesize[0] + x] = color;
136                         if (half_horiz)
137                             s->frame.data[0][(y+1)*s->frame.linesize[0] + x + 1] = color;
138                     }
139                     d++;
140                 }
141                 x += half_horiz ? 2 : 1;
142             }
143         }
144
145         r += length;
146         y += half_vert ? 2 : 1;
147     }
148 }
149
150 static int mm_decode_frame(AVCodecContext *avctx,
151                             void *data, int *data_size,
152                             uint8_t *buf, int buf_size)
153 {
154     MmContext *s = avctx->priv_data;
155     AVPaletteControl *palette_control = avctx->palctrl;
156     int type;
157
158     if (palette_control->palette_changed) {
159         memcpy(s->frame.data[1], palette_control->palette, AVPALETTE_SIZE);
160         palette_control->palette_changed = 0;
161     }
162
163     type = LE_16(&buf[0]);
164     buf += MM_PREAMBLE_SIZE;
165     buf_size -= MM_PREAMBLE_SIZE;
166
167     switch(type) {
168     case MM_TYPE_INTRA     : mm_decode_intra(s, 0, 0, buf, buf_size); break;
169     case MM_TYPE_INTRA_HH  : mm_decode_intra(s, 1, 0, buf, buf_size); break;
170     case MM_TYPE_INTRA_HHV : mm_decode_intra(s, 1, 1, buf, buf_size); break;
171     case MM_TYPE_INTER     : mm_decode_inter(s, 0, 0, buf, buf_size); break;
172     case MM_TYPE_INTER_HH  : mm_decode_inter(s, 1, 0, buf, buf_size); break;
173     case MM_TYPE_INTER_HHV : mm_decode_inter(s, 1, 1, buf, buf_size); break;
174     default :
175         return -1;
176     }
177
178     *data_size = sizeof(AVFrame);
179     *(AVFrame*)data = s->frame;
180
181     return buf_size;
182 }
183
184 static int mm_decode_end(AVCodecContext *avctx)
185 {
186     MmContext *s = avctx->priv_data;
187
188     if(s->frame.data[0])
189         avctx->release_buffer(avctx, &s->frame);
190
191     return 0;
192 }
193
194 AVCodec mmvideo_decoder = {
195     "mmvideo",
196     CODEC_TYPE_VIDEO,
197     CODEC_ID_MMVIDEO,
198     sizeof(MmContext),
199     mm_decode_init,
200     NULL,
201     mm_decode_end,
202     mm_decode_frame,
203     CODEC_CAP_DR1,
204 };