]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/mmvideo.c
Add long names to many AVCodec declarations.
[frescor/ffmpeg.git] / libavcodec / mmvideo.c
1 /*
2  * American Laser Games MM Video Decoder
3  * Copyright (c) 2006 Peter Ross
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file mm.c
24  * American Laser Games MM Video Decoder
25  * by Peter Ross (suxen_drol at hotmail dot com)
26  *
27  * The MM format was used by IBM-PC ports of ALG's "arcade shooter" games,
28  * including Mad Dog McCree and Crime Patrol.
29  *
30  * Technical details here:
31  *  http://wiki.multimedia.cx/index.php?title=American_Laser_Games_MM
32  */
33
34 #include "avcodec.h"
35
36 #define MM_PREAMBLE_SIZE    6
37
38 #define MM_TYPE_INTER       0x5
39 #define MM_TYPE_INTRA       0x8
40 #define MM_TYPE_INTRA_HH    0xc
41 #define MM_TYPE_INTER_HH    0xd
42 #define MM_TYPE_INTRA_HHV   0xe
43 #define MM_TYPE_INTER_HHV   0xf
44
45 typedef struct MmContext {
46     AVCodecContext *avctx;
47     AVFrame frame;
48 } MmContext;
49
50 static av_cold int mm_decode_init(AVCodecContext *avctx)
51 {
52     MmContext *s = avctx->priv_data;
53
54     s->avctx = avctx;
55
56     if (s->avctx->palctrl == NULL) {
57         av_log(avctx, AV_LOG_ERROR, "mmvideo: palette expected.\n");
58         return -1;
59     }
60
61     avctx->pix_fmt = PIX_FMT_PAL8;
62
63     if (avcodec_check_dimensions(avctx, avctx->width, avctx->height))
64         return -1;
65
66     s->frame.reference = 1;
67     if (avctx->get_buffer(avctx, &s->frame)) {
68         av_log(s->avctx, AV_LOG_ERROR, "mmvideo: get_buffer() failed\n");
69         return -1;
70     }
71
72     return 0;
73 }
74
75 static void mm_decode_intra(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
76 {
77     int i, x, y;
78     i=0; x=0; y=0;
79
80     while(i<buf_size) {
81         int run_length, color;
82
83         if (buf[i] & 0x80) {
84             run_length = 1;
85             color = buf[i];
86             i++;
87         }else{
88             run_length = (buf[i] & 0x7f) + 2;
89             color = buf[i+1];
90             i+=2;
91         }
92
93         if (half_horiz)
94             run_length *=2;
95
96         if (color) {
97             memset(s->frame.data[0] + y*s->frame.linesize[0] + x, color, run_length);
98             if (half_vert)
99                 memset(s->frame.data[0] + (y+1)*s->frame.linesize[0] + x, color, run_length);
100         }
101         x+= run_length;
102
103         if (x >= s->avctx->width) {
104             x=0;
105             y += half_vert ? 2 : 1;
106         }
107     }
108 }
109
110 static void mm_decode_inter(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
111 {
112     const int data_ptr = 2 + AV_RL16(&buf[0]);
113     int d, r, y;
114     d = data_ptr; r = 2; y = 0;
115
116     while(r < data_ptr) {
117         int i, j;
118         int length = buf[r] & 0x7f;
119         int x = buf[r+1] + ((buf[r] & 0x80) << 1);
120         r += 2;
121
122         if (length==0) {
123             y += x;
124             continue;
125         }
126
127         for(i=0; i<length; i++) {
128             for(j=0; j<8; j++) {
129                 int replace = (buf[r+i] >> (7-j)) & 1;
130                 if (replace) {
131                     int color = buf[d];
132                     s->frame.data[0][y*s->frame.linesize[0] + x] = color;
133                     if (half_horiz)
134                         s->frame.data[0][y*s->frame.linesize[0] + x + 1] = color;
135                     if (half_vert) {
136                         s->frame.data[0][(y+1)*s->frame.linesize[0] + x] = color;
137                         if (half_horiz)
138                             s->frame.data[0][(y+1)*s->frame.linesize[0] + x + 1] = color;
139                     }
140                     d++;
141                 }
142                 x += half_horiz ? 2 : 1;
143             }
144         }
145
146         r += length;
147         y += half_vert ? 2 : 1;
148     }
149 }
150
151 static int mm_decode_frame(AVCodecContext *avctx,
152                             void *data, int *data_size,
153                             const uint8_t *buf, int buf_size)
154 {
155     MmContext *s = avctx->priv_data;
156     AVPaletteControl *palette_control = avctx->palctrl;
157     int type;
158
159     if (palette_control->palette_changed) {
160         memcpy(s->frame.data[1], palette_control->palette, AVPALETTE_SIZE);
161         palette_control->palette_changed = 0;
162     }
163
164     type = AV_RL16(&buf[0]);
165     buf += MM_PREAMBLE_SIZE;
166     buf_size -= MM_PREAMBLE_SIZE;
167
168     switch(type) {
169     case MM_TYPE_INTRA     : mm_decode_intra(s, 0, 0, buf, buf_size); break;
170     case MM_TYPE_INTRA_HH  : mm_decode_intra(s, 1, 0, buf, buf_size); break;
171     case MM_TYPE_INTRA_HHV : mm_decode_intra(s, 1, 1, buf, buf_size); break;
172     case MM_TYPE_INTER     : mm_decode_inter(s, 0, 0, buf, buf_size); break;
173     case MM_TYPE_INTER_HH  : mm_decode_inter(s, 1, 0, buf, buf_size); break;
174     case MM_TYPE_INTER_HHV : mm_decode_inter(s, 1, 1, buf, buf_size); break;
175     default :
176         return -1;
177     }
178
179     *data_size = sizeof(AVFrame);
180     *(AVFrame*)data = s->frame;
181
182     return buf_size;
183 }
184
185 static av_cold int mm_decode_end(AVCodecContext *avctx)
186 {
187     MmContext *s = avctx->priv_data;
188
189     if(s->frame.data[0])
190         avctx->release_buffer(avctx, &s->frame);
191
192     return 0;
193 }
194
195 AVCodec mmvideo_decoder = {
196     "mmvideo",
197     CODEC_TYPE_VIDEO,
198     CODEC_ID_MMVIDEO,
199     sizeof(MmContext),
200     mm_decode_init,
201     NULL,
202     mm_decode_end,
203     mm_decode_frame,
204     CODEC_CAP_DR1,
205     .long_name = "American Laser Games MM Video",
206 };