Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mmvideo.c @ 2912e87a

History | View | Annotate | Download (6.14 KB)

1
/*
2
 * American Laser Games MM Video Decoder
3
 * Copyright (c) 2006,2008 Peter Ross
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav 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
 * Libav 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 Libav; 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
24
 * American Laser Games MM Video Decoder
25
 * by Peter Ross (pross@xvid.org)
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 "libavutil/intreadwrite.h"
35
#include "avcodec.h"
36

    
37
#define MM_PREAMBLE_SIZE    6
38

    
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
46

    
47
typedef struct MmContext {
48
    AVCodecContext *avctx;
49
    AVFrame frame;
50
    int palette[AVPALETTE_COUNT];
51
} MmContext;
52

    
53
static av_cold int mm_decode_init(AVCodecContext *avctx)
54
{
55
    MmContext *s = avctx->priv_data;
56

    
57
    s->avctx = avctx;
58

    
59
    avctx->pix_fmt = PIX_FMT_PAL8;
60

    
61
    s->frame.reference = 1;
62

    
63
    return 0;
64
}
65

    
66
static void mm_decode_pal(MmContext *s, const uint8_t *buf, const uint8_t *buf_end)
67
{
68
    int i;
69
    buf += 4;
70
    for (i=0; i<128 && buf+2<buf_end; i++) {
71
        s->palette[i] = AV_RB24(buf);
72
        s->palette[i+128] = s->palette[i]<<2;
73
        buf += 3;
74
    }
75
}
76

    
77
/**
78
 * @param half_horiz Half horizontal resolution (0 or 1)
79
 * @param half_vert Half vertical resolution (0 or 1)
80
 */
81
static void mm_decode_intra(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
82
{
83
    int i, x, y;
84
    i=0; x=0; y=0;
85

    
86
    while(i<buf_size) {
87
        int run_length, color;
88

    
89
        if (y >= s->avctx->height)
90
            return;
91

    
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;
100
        }
101

    
102
        if (half_horiz)
103
            run_length *=2;
104

    
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);
109
        }
110
        x+= run_length;
111

    
112
        if (x >= s->avctx->width) {
113
            x=0;
114
            y += 1 + half_vert;
115
        }
116
    }
117
}
118

    
119
/*
120
 * @param half_horiz Half horizontal resolution (0 or 1)
121
 * @param half_vert Half vertical resolution (0 or 1)
122
 */
123
static void mm_decode_inter(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
124
{
125
    const int data_ptr = 2 + AV_RL16(&buf[0]);
126
    int d, r, y;
127
    d = data_ptr; r = 2; y = 0;
128

    
129
    while(r < data_ptr) {
130
        int i, j;
131
        int length = buf[r] & 0x7f;
132
        int x = buf[r+1] + ((buf[r] & 0x80) << 1);
133
        r += 2;
134

    
135
        if (length==0) {
136
            y += x;
137
            continue;
138
        }
139

    
140
        if (y + half_vert >= s->avctx->height)
141
            return;
142

    
143
        for(i=0; i<length; i++) {
144
            for(j=0; j<8; j++) {
145
                int replace = (buf[r+i] >> (7-j)) & 1;
146
                if (replace) {
147
                    int color = buf[d];
148
                    s->frame.data[0][y*s->frame.linesize[0] + x] = color;
149
                    if (half_horiz)
150
                        s->frame.data[0][y*s->frame.linesize[0] + x + 1] = color;
151
                    if (half_vert) {
152
                        s->frame.data[0][(y+1)*s->frame.linesize[0] + x] = color;
153
                        if (half_horiz)
154
                            s->frame.data[0][(y+1)*s->frame.linesize[0] + x + 1] = color;
155
                    }
156
                    d++;
157
                }
158
                x += 1 + half_horiz;
159
            }
160
        }
161

    
162
        r += length;
163
        y += 1 + half_vert;
164
    }
165
}
166

    
167
static int mm_decode_frame(AVCodecContext *avctx,
168
                            void *data, int *data_size,
169
                            AVPacket *avpkt)
170
{
171
    const uint8_t *buf = avpkt->data;
172
    int buf_size = avpkt->size;
173
    MmContext *s = avctx->priv_data;
174
    const uint8_t *buf_end = buf+buf_size;
175
    int type;
176

    
177
    type = AV_RL16(&buf[0]);
178
    buf += MM_PREAMBLE_SIZE;
179
    buf_size -= MM_PREAMBLE_SIZE;
180

    
181
    if (avctx->reget_buffer(avctx, &s->frame) < 0) {
182
        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
183
        return -1;
184
    }
185

    
186
    switch(type) {
187
    case MM_TYPE_PALETTE   : mm_decode_pal(s, buf, buf_end); return buf_size;
188
    case MM_TYPE_INTRA     : mm_decode_intra(s, 0, 0, buf, buf_size); break;
189
    case MM_TYPE_INTRA_HH  : mm_decode_intra(s, 1, 0, buf, buf_size); break;
190
    case MM_TYPE_INTRA_HHV : mm_decode_intra(s, 1, 1, buf, buf_size); break;
191
    case MM_TYPE_INTER     : mm_decode_inter(s, 0, 0, buf, buf_size); break;
192
    case MM_TYPE_INTER_HH  : mm_decode_inter(s, 1, 0, buf, buf_size); break;
193
    case MM_TYPE_INTER_HHV : mm_decode_inter(s, 1, 1, buf, buf_size); break;
194
    default :
195
        return -1;
196
    }
197

    
198
    memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
199

    
200
    *data_size = sizeof(AVFrame);
201
    *(AVFrame*)data = s->frame;
202

    
203
    return buf_size;
204
}
205

    
206
static av_cold int mm_decode_end(AVCodecContext *avctx)
207
{
208
    MmContext *s = avctx->priv_data;
209

    
210
    if(s->frame.data[0])
211
        avctx->release_buffer(avctx, &s->frame);
212

    
213
    return 0;
214
}
215

    
216
AVCodec ff_mmvideo_decoder = {
217
    "mmvideo",
218
    AVMEDIA_TYPE_VIDEO,
219
    CODEC_ID_MMVIDEO,
220
    sizeof(MmContext),
221
    mm_decode_init,
222
    NULL,
223
    mm_decode_end,
224
    mm_decode_frame,
225
    CODEC_CAP_DR1,
226
    .long_name = NULL_IF_CONFIG_SMALL("American Laser Games MM Video"),
227
};