2 * Microsoft Video-1 Decoder
3 * Copyright (C) 2003 the ffmpeg project
5 * This file is part of Libav.
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.
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.
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
24 * Microsoft Video-1 Decoder by Mike Melanson (melanson@pcisys.net)
25 * For more information about the MS Video-1 format, visit:
26 * http://www.pcisys.net/~melanson/codecs/
34 #include "libavutil/intreadwrite.h"
37 #define PALETTE_COUNT 256
38 #define CHECK_STREAM_PTR(n) \
39 if ((stream_ptr + n) > s->size ) { \
40 av_log(s->avctx, AV_LOG_ERROR, " MS Video-1 warning: stream_ptr out of bounds (%d >= %d)\n", \
41 stream_ptr + n, s->size); \
45 typedef struct Msvideo1Context {
47 AVCodecContext *avctx;
50 const unsigned char *buf;
53 int mode_8bit; /* if it's not 8-bit, it's 16-bit */
58 static av_cold int msvideo1_decode_init(AVCodecContext *avctx)
60 Msvideo1Context *s = avctx->priv_data;
64 /* figure out the colorspace based on the presence of a palette */
65 if (s->avctx->bits_per_coded_sample == 8) {
67 avctx->pix_fmt = PIX_FMT_PAL8;
70 avctx->pix_fmt = PIX_FMT_RGB555;
73 s->frame.data[0] = NULL;
78 static void msvideo1_decode_8bit(Msvideo1Context *s)
80 int block_ptr, pixel_ptr;
82 int pixel_x, pixel_y; /* pixel width and height iterators */
83 int block_x, block_y; /* block width and height iterators */
84 int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
88 /* decoding parameters */
90 unsigned char byte_a, byte_b;
93 unsigned char colors[8];
94 unsigned char *pixels = s->frame.data[0];
95 int stride = s->frame.linesize[0];
99 blocks_wide = s->avctx->width / 4;
100 blocks_high = s->avctx->height / 4;
101 total_blocks = blocks_wide * blocks_high;
103 row_dec = stride + 4;
105 for (block_y = blocks_high; block_y > 0; block_y--) {
106 block_ptr = ((block_y * 4) - 1) * stride;
107 for (block_x = blocks_wide; block_x > 0; block_x--) {
108 /* check if this block should be skipped */
110 block_ptr += block_inc;
116 pixel_ptr = block_ptr;
118 /* get the next two bytes in the encoded data stream */
120 byte_a = s->buf[stream_ptr++];
121 byte_b = s->buf[stream_ptr++];
123 /* check if the decode is finished */
124 if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
126 else if ((byte_b & 0xFC) == 0x84) {
127 /* skip code, but don't count the current block */
128 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
129 } else if (byte_b < 0x80) {
130 /* 2-color encoding */
131 flags = (byte_b << 8) | byte_a;
134 colors[0] = s->buf[stream_ptr++];
135 colors[1] = s->buf[stream_ptr++];
137 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
138 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
139 pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
140 pixel_ptr -= row_dec;
142 } else if (byte_b >= 0x90) {
143 /* 8-color encoding */
144 flags = (byte_b << 8) | byte_a;
147 memcpy(colors, &s->buf[stream_ptr], 8);
150 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
151 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
152 pixels[pixel_ptr++] =
153 colors[((pixel_y & 0x2) << 1) +
154 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
155 pixel_ptr -= row_dec;
158 /* 1-color encoding */
161 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
162 for (pixel_x = 0; pixel_x < 4; pixel_x++)
163 pixels[pixel_ptr++] = colors[0];
164 pixel_ptr -= row_dec;
168 block_ptr += block_inc;
173 /* make the palette available on the way out */
174 if (s->avctx->pix_fmt == PIX_FMT_PAL8)
175 memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
178 static void msvideo1_decode_16bit(Msvideo1Context *s)
180 int block_ptr, pixel_ptr;
182 int pixel_x, pixel_y; /* pixel width and height iterators */
183 int block_x, block_y; /* block width and height iterators */
184 int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
188 /* decoding parameters */
190 unsigned char byte_a, byte_b;
191 unsigned short flags;
193 unsigned short colors[8];
194 unsigned short *pixels = (unsigned short *)s->frame.data[0];
195 int stride = s->frame.linesize[0] / 2;
199 blocks_wide = s->avctx->width / 4;
200 blocks_high = s->avctx->height / 4;
201 total_blocks = blocks_wide * blocks_high;
203 row_dec = stride + 4;
205 for (block_y = blocks_high; block_y > 0; block_y--) {
206 block_ptr = ((block_y * 4) - 1) * stride;
207 for (block_x = blocks_wide; block_x > 0; block_x--) {
208 /* check if this block should be skipped */
210 block_ptr += block_inc;
216 pixel_ptr = block_ptr;
218 /* get the next two bytes in the encoded data stream */
220 byte_a = s->buf[stream_ptr++];
221 byte_b = s->buf[stream_ptr++];
223 /* check if the decode is finished */
224 if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
226 } else if ((byte_b & 0xFC) == 0x84) {
227 /* skip code, but don't count the current block */
228 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
229 } else if (byte_b < 0x80) {
230 /* 2- or 8-color encoding modes */
231 flags = (byte_b << 8) | byte_a;
234 colors[0] = AV_RL16(&s->buf[stream_ptr]);
236 colors[1] = AV_RL16(&s->buf[stream_ptr]);
239 if (colors[0] & 0x8000) {
240 /* 8-color encoding */
241 CHECK_STREAM_PTR(12);
242 colors[2] = AV_RL16(&s->buf[stream_ptr]);
244 colors[3] = AV_RL16(&s->buf[stream_ptr]);
246 colors[4] = AV_RL16(&s->buf[stream_ptr]);
248 colors[5] = AV_RL16(&s->buf[stream_ptr]);
250 colors[6] = AV_RL16(&s->buf[stream_ptr]);
252 colors[7] = AV_RL16(&s->buf[stream_ptr]);
255 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
256 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
257 pixels[pixel_ptr++] =
258 colors[((pixel_y & 0x2) << 1) +
259 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
260 pixel_ptr -= row_dec;
263 /* 2-color encoding */
264 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
265 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
266 pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
267 pixel_ptr -= row_dec;
271 /* otherwise, it's a 1-color block */
272 colors[0] = (byte_b << 8) | byte_a;
274 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
275 for (pixel_x = 0; pixel_x < 4; pixel_x++)
276 pixels[pixel_ptr++] = colors[0];
277 pixel_ptr -= row_dec;
281 block_ptr += block_inc;
287 static int msvideo1_decode_frame(AVCodecContext *avctx,
288 void *data, int *data_size,
291 const uint8_t *buf = avpkt->data;
292 int buf_size = avpkt->size;
293 Msvideo1Context *s = avctx->priv_data;
298 s->frame.reference = 1;
299 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
300 if (avctx->reget_buffer(avctx, &s->frame)) {
301 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
306 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
309 memcpy(s->pal, pal, AVPALETTE_SIZE);
310 s->frame.palette_has_changed = 1;
315 msvideo1_decode_8bit(s);
317 msvideo1_decode_16bit(s);
319 *data_size = sizeof(AVFrame);
320 *(AVFrame*)data = s->frame;
322 /* report that the buffer was completely consumed */
326 static av_cold int msvideo1_decode_end(AVCodecContext *avctx)
328 Msvideo1Context *s = avctx->priv_data;
330 if (s->frame.data[0])
331 avctx->release_buffer(avctx, &s->frame);
336 AVCodec ff_msvideo1_decoder = {
340 sizeof(Msvideo1Context),
341 msvideo1_decode_init,
344 msvideo1_decode_frame,
346 .long_name= NULL_IF_CONFIG_SMALL("Microsoft Video 1"),