]> git.sesse.net Git - ffmpeg/blob - libavcodec/msvideo1.c
Conditionally compile some of the AltiVec optimizations.
[ffmpeg] / libavcodec / msvideo1.c
1 /*
2  * Microsoft Video-1 Decoder
3  * Copyright (C) 2003 the ffmpeg project
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 /**
24  * @file msvideo1.c
25  * Microsoft Video-1 Decoder by Mike Melanson (melanson@pcisys.net)
26  * For more information about the MS Video-1 format, visit:
27  *   http://www.pcisys.net/~melanson/codecs/
28  *
29  * This decoder outputs either PAL8 or RGB555 data, depending on the
30  * whether a RGB palette was passed through palctrl;
31  * if it's present, then the data is PAL8; RGB555 otherwise.
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38
39 #include "common.h"
40 #include "avcodec.h"
41 #include "dsputil.h"
42
43 #define PALETTE_COUNT 256
44 #define CHECK_STREAM_PTR(n) \
45   if ((stream_ptr + n) > s->size ) { \
46     av_log(s->avctx, AV_LOG_ERROR, " MS Video-1 warning: stream_ptr out of bounds (%d >= %d)\n", \
47       stream_ptr + n, s->size); \
48     return; \
49   }
50
51 typedef struct Msvideo1Context {
52
53     AVCodecContext *avctx;
54     DSPContext dsp;
55     AVFrame frame;
56
57     unsigned char *buf;
58     int size;
59
60     int mode_8bit;  /* if it's not 8-bit, it's 16-bit */
61
62 } Msvideo1Context;
63
64 static int msvideo1_decode_init(AVCodecContext *avctx)
65 {
66     Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data;
67
68     s->avctx = avctx;
69
70     /* figure out the colorspace based on the presence of a palette */
71     if (s->avctx->palctrl) {
72         s->mode_8bit = 1;
73         avctx->pix_fmt = PIX_FMT_PAL8;
74     } else {
75         s->mode_8bit = 0;
76         avctx->pix_fmt = PIX_FMT_RGB555;
77     }
78
79     avctx->has_b_frames = 0;
80     dsputil_init(&s->dsp, avctx);
81
82     s->frame.data[0] = NULL;
83
84     return 0;
85 }
86
87 static void msvideo1_decode_8bit(Msvideo1Context *s)
88 {
89     int block_ptr, pixel_ptr;
90     int total_blocks;
91     int pixel_x, pixel_y;  /* pixel width and height iterators */
92     int block_x, block_y;  /* block width and height iterators */
93     int blocks_wide, blocks_high;  /* width and height in 4x4 blocks */
94     int block_inc;
95     int row_dec;
96
97     /* decoding parameters */
98     int stream_ptr;
99     unsigned char byte_a, byte_b;
100     unsigned short flags;
101     int skip_blocks;
102     unsigned char colors[8];
103     unsigned char *pixels = s->frame.data[0];
104     int stride = s->frame.linesize[0];
105
106     stream_ptr = 0;
107     skip_blocks = 0;
108     blocks_wide = s->avctx->width / 4;
109     blocks_high = s->avctx->height / 4;
110     total_blocks = blocks_wide * blocks_high;
111     block_inc = 4;
112     row_dec = stride + 4;
113
114     for (block_y = blocks_high; block_y > 0; block_y--) {
115         block_ptr = ((block_y * 4) - 1) * stride;
116         for (block_x = blocks_wide; block_x > 0; block_x--) {
117             /* check if this block should be skipped */
118             if (skip_blocks) {
119                 block_ptr += block_inc;
120                 skip_blocks--;
121                 total_blocks--;
122                 continue;
123             }
124
125             pixel_ptr = block_ptr;
126
127             /* get the next two bytes in the encoded data stream */
128             CHECK_STREAM_PTR(2);
129             byte_a = s->buf[stream_ptr++];
130             byte_b = s->buf[stream_ptr++];
131
132             /* check if the decode is finished */
133             if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
134                 return;
135             else if ((byte_b & 0xFC) == 0x84) {
136                 /* skip code, but don't count the current block */
137                 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
138             } else if (byte_b < 0x80) {
139                 /* 2-color encoding */
140                 flags = (byte_b << 8) | byte_a;
141
142                 CHECK_STREAM_PTR(2);
143                 colors[0] = s->buf[stream_ptr++];
144                 colors[1] = s->buf[stream_ptr++];
145
146                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
147                     for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
148                         pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
149                     pixel_ptr -= row_dec;
150                 }
151             } else if (byte_b >= 0x90) {
152                 /* 8-color encoding */
153                 flags = (byte_b << 8) | byte_a;
154
155                 CHECK_STREAM_PTR(8);
156                 memcpy(colors, &s->buf[stream_ptr], 8);
157                 stream_ptr += 8;
158
159                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
160                     for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
161                         pixels[pixel_ptr++] =
162                             colors[((pixel_y & 0x2) << 1) +
163                                 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
164                     pixel_ptr -= row_dec;
165                 }
166             } else {
167                 /* 1-color encoding */
168                 colors[0] = byte_a;
169
170                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
171                     for (pixel_x = 0; pixel_x < 4; pixel_x++)
172                         pixels[pixel_ptr++] = colors[0];
173                     pixel_ptr -= row_dec;
174                 }
175             }
176
177             block_ptr += block_inc;
178             total_blocks--;
179         }
180     }
181
182     /* make the palette available on the way out */
183     if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
184         memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
185         if (s->avctx->palctrl->palette_changed) {
186             s->frame.palette_has_changed = 1;
187             s->avctx->palctrl->palette_changed = 0;
188         }
189     }
190 }
191
192 static void msvideo1_decode_16bit(Msvideo1Context *s)
193 {
194     int block_ptr, pixel_ptr;
195     int total_blocks;
196     int pixel_x, pixel_y;  /* pixel width and height iterators */
197     int block_x, block_y;  /* block width and height iterators */
198     int blocks_wide, blocks_high;  /* width and height in 4x4 blocks */
199     int block_inc;
200     int row_dec;
201
202     /* decoding parameters */
203     int stream_ptr;
204     unsigned char byte_a, byte_b;
205     unsigned short flags;
206     int skip_blocks;
207     unsigned short colors[8];
208     unsigned short *pixels = (unsigned short *)s->frame.data[0];
209     int stride = s->frame.linesize[0] / 2;
210
211     stream_ptr = 0;
212     skip_blocks = 0;
213     blocks_wide = s->avctx->width / 4;
214     blocks_high = s->avctx->height / 4;
215     total_blocks = blocks_wide * blocks_high;
216     block_inc = 4;
217     row_dec = stride + 4;
218
219     for (block_y = blocks_high; block_y > 0; block_y--) {
220         block_ptr = ((block_y * 4) - 1) * stride;
221         for (block_x = blocks_wide; block_x > 0; block_x--) {
222             /* check if this block should be skipped */
223             if (skip_blocks) {
224                 block_ptr += block_inc;
225                 skip_blocks--;
226                 total_blocks--;
227                 continue;
228             }
229
230             pixel_ptr = block_ptr;
231
232             /* get the next two bytes in the encoded data stream */
233             CHECK_STREAM_PTR(2);
234             byte_a = s->buf[stream_ptr++];
235             byte_b = s->buf[stream_ptr++];
236
237             /* check if the decode is finished */
238             if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
239                 return;
240             } else if ((byte_b & 0xFC) == 0x84) {
241                 /* skip code, but don't count the current block */
242                 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
243             } else if (byte_b < 0x80) {
244                 /* 2- or 8-color encoding modes */
245                 flags = (byte_b << 8) | byte_a;
246
247                 CHECK_STREAM_PTR(4);
248                 colors[0] = LE_16(&s->buf[stream_ptr]);
249                 stream_ptr += 2;
250                 colors[1] = LE_16(&s->buf[stream_ptr]);
251                 stream_ptr += 2;
252
253                 if (colors[0] & 0x8000) {
254                     /* 8-color encoding */
255                     CHECK_STREAM_PTR(12);
256                     colors[2] = LE_16(&s->buf[stream_ptr]);
257                     stream_ptr += 2;
258                     colors[3] = LE_16(&s->buf[stream_ptr]);
259                     stream_ptr += 2;
260                     colors[4] = LE_16(&s->buf[stream_ptr]);
261                     stream_ptr += 2;
262                     colors[5] = LE_16(&s->buf[stream_ptr]);
263                     stream_ptr += 2;
264                     colors[6] = LE_16(&s->buf[stream_ptr]);
265                     stream_ptr += 2;
266                     colors[7] = LE_16(&s->buf[stream_ptr]);
267                     stream_ptr += 2;
268
269                     for (pixel_y = 0; pixel_y < 4; pixel_y++) {
270                         for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
271                             pixels[pixel_ptr++] =
272                                 colors[((pixel_y & 0x2) << 1) +
273                                     (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
274                         pixel_ptr -= row_dec;
275                     }
276                 } else {
277                     /* 2-color encoding */
278                     for (pixel_y = 0; pixel_y < 4; pixel_y++) {
279                         for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
280                             pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
281                         pixel_ptr -= row_dec;
282                     }
283                 }
284             } else {
285                 /* otherwise, it's a 1-color block */
286                 colors[0] = (byte_b << 8) | byte_a;
287
288                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
289                     for (pixel_x = 0; pixel_x < 4; pixel_x++)
290                         pixels[pixel_ptr++] = colors[0];
291                     pixel_ptr -= row_dec;
292                 }
293             }
294
295             block_ptr += block_inc;
296             total_blocks--;
297         }
298     }
299 }
300
301 static int msvideo1_decode_frame(AVCodecContext *avctx,
302                                 void *data, int *data_size,
303                                 uint8_t *buf, int buf_size)
304 {
305     Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data;
306
307     s->buf = buf;
308     s->size = buf_size;
309
310     s->frame.reference = 1;
311     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
312     if (avctx->reget_buffer(avctx, &s->frame)) {
313         av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
314         return -1;
315     }
316
317     if (s->mode_8bit)
318         msvideo1_decode_8bit(s);
319     else
320         msvideo1_decode_16bit(s);
321
322     *data_size = sizeof(AVFrame);
323     *(AVFrame*)data = s->frame;
324
325     /* report that the buffer was completely consumed */
326     return buf_size;
327 }
328
329 static int msvideo1_decode_end(AVCodecContext *avctx)
330 {
331     Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data;
332
333     if (s->frame.data[0])
334         avctx->release_buffer(avctx, &s->frame);
335
336     return 0;
337 }
338
339 AVCodec msvideo1_decoder = {
340     "msvideo1",
341     CODEC_TYPE_VIDEO,
342     CODEC_ID_MSVIDEO1,
343     sizeof(Msvideo1Context),
344     msvideo1_decode_init,
345     NULL,
346     msvideo1_decode_end,
347     msvideo1_decode_frame,
348     CODEC_CAP_DR1,
349 };