2 * Microsoft Video-1 Decoder
3 * Copyright (C) 2003 the ffmpeg project
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
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/
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.
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); \
51 typedef struct Msvideo1Context {
53 AVCodecContext *avctx;
60 int mode_8bit; /* if it's not 8-bit, it's 16-bit */
64 static int msvideo1_decode_init(AVCodecContext *avctx)
66 Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data;
70 /* figure out the colorspace based on the presence of a palette */
71 if (s->avctx->palctrl) {
73 avctx->pix_fmt = PIX_FMT_PAL8;
76 avctx->pix_fmt = PIX_FMT_RGB555;
79 avctx->has_b_frames = 0;
80 dsputil_init(&s->dsp, avctx);
82 s->frame.data[0] = NULL;
87 static void msvideo1_decode_8bit(Msvideo1Context *s)
89 int block_ptr, pixel_ptr;
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 */
97 /* decoding parameters */
99 unsigned char byte_a, byte_b;
100 unsigned short flags;
102 unsigned char colors[8];
103 unsigned char *pixels = s->frame.data[0];
104 int stride = s->frame.linesize[0];
108 blocks_wide = s->avctx->width / 4;
109 blocks_high = s->avctx->height / 4;
110 total_blocks = blocks_wide * blocks_high;
112 row_dec = stride + 4;
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 */
119 block_ptr += block_inc;
125 pixel_ptr = block_ptr;
127 /* get the next two bytes in the encoded data stream */
129 byte_a = s->buf[stream_ptr++];
130 byte_b = s->buf[stream_ptr++];
132 /* check if the decode is finished */
133 if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
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;
143 colors[0] = s->buf[stream_ptr++];
144 colors[1] = s->buf[stream_ptr++];
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;
151 } else if (byte_b >= 0x90) {
152 /* 8-color encoding */
153 flags = (byte_b << 8) | byte_a;
156 memcpy(colors, &s->buf[stream_ptr], 8);
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;
167 /* 1-color encoding */
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;
177 block_ptr += block_inc;
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;
192 static void msvideo1_decode_16bit(Msvideo1Context *s)
194 int block_ptr, pixel_ptr;
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 */
202 /* decoding parameters */
204 unsigned char byte_a, byte_b;
205 unsigned short flags;
207 unsigned short colors[8];
208 unsigned short *pixels = (unsigned short *)s->frame.data[0];
209 int stride = s->frame.linesize[0] / 2;
213 blocks_wide = s->avctx->width / 4;
214 blocks_high = s->avctx->height / 4;
215 total_blocks = blocks_wide * blocks_high;
217 row_dec = stride + 4;
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 */
224 block_ptr += block_inc;
230 pixel_ptr = block_ptr;
232 /* get the next two bytes in the encoded data stream */
234 byte_a = s->buf[stream_ptr++];
235 byte_b = s->buf[stream_ptr++];
237 /* check if the decode is finished */
238 if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
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;
248 colors[0] = LE_16(&s->buf[stream_ptr]);
250 colors[1] = LE_16(&s->buf[stream_ptr]);
253 if (colors[0] & 0x8000) {
254 /* 8-color encoding */
255 CHECK_STREAM_PTR(12);
256 colors[2] = LE_16(&s->buf[stream_ptr]);
258 colors[3] = LE_16(&s->buf[stream_ptr]);
260 colors[4] = LE_16(&s->buf[stream_ptr]);
262 colors[5] = LE_16(&s->buf[stream_ptr]);
264 colors[6] = LE_16(&s->buf[stream_ptr]);
266 colors[7] = LE_16(&s->buf[stream_ptr]);
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;
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;
285 /* otherwise, it's a 1-color block */
286 colors[0] = (byte_b << 8) | byte_a;
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;
295 block_ptr += block_inc;
301 static int msvideo1_decode_frame(AVCodecContext *avctx,
302 void *data, int *data_size,
303 uint8_t *buf, int buf_size)
305 Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data;
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");
318 msvideo1_decode_8bit(s);
320 msvideo1_decode_16bit(s);
322 *data_size = sizeof(AVFrame);
323 *(AVFrame*)data = s->frame;
325 /* report that the buffer was completely consumed */
329 static int msvideo1_decode_end(AVCodecContext *avctx)
331 Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data;
333 if (s->frame.data[0])
334 avctx->release_buffer(avctx, &s->frame);
339 AVCodec msvideo1_decoder = {
343 sizeof(Msvideo1Context),
344 msvideo1_decode_init,
347 msvideo1_decode_frame,