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
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/
33 #include "libavutil/internal.h"
34 #include "libavutil/intreadwrite.h"
38 #define PALETTE_COUNT 256
39 #define CHECK_STREAM_PTR(n) \
40 if ((stream_ptr + n) > s->size ) { \
41 av_log(s->avctx, AV_LOG_ERROR, " MS Video-1 warning: stream_ptr out of bounds (%d >= %d)\n", \
42 stream_ptr + n, s->size); \
46 typedef struct Msvideo1Context {
48 AVCodecContext *avctx;
51 const unsigned char *buf;
54 int mode_8bit; /* if it's not 8-bit, it's 16-bit */
59 static av_cold int msvideo1_decode_init(AVCodecContext *avctx)
61 Msvideo1Context *s = avctx->priv_data;
65 /* figure out the colorspace based on the presence of a palette */
66 if (s->avctx->bits_per_coded_sample == 8) {
68 avctx->pix_fmt = AV_PIX_FMT_PAL8;
69 if (avctx->extradata_size >= AVPALETTE_SIZE)
70 memcpy(s->pal, avctx->extradata, AVPALETTE_SIZE);
73 avctx->pix_fmt = AV_PIX_FMT_RGB555;
76 s->frame = av_frame_alloc();
78 return AVERROR(ENOMEM);
83 static void msvideo1_decode_8bit(Msvideo1Context *s)
85 int block_ptr, pixel_ptr;
87 int pixel_x, pixel_y; /* pixel width and height iterators */
88 int block_x, block_y; /* block width and height iterators */
89 int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
93 /* decoding parameters */
95 unsigned char byte_a, byte_b;
98 unsigned char colors[8];
99 unsigned char *pixels = s->frame->data[0];
100 int stride = s->frame->linesize[0];
104 blocks_wide = s->avctx->width / 4;
105 blocks_high = s->avctx->height / 4;
106 total_blocks = blocks_wide * blocks_high;
108 row_dec = stride + 4;
110 for (block_y = blocks_high; block_y > 0; block_y--) {
111 block_ptr = ((block_y * 4) - 1) * stride;
112 for (block_x = blocks_wide; block_x > 0; block_x--) {
113 /* check if this block should be skipped */
115 block_ptr += block_inc;
121 pixel_ptr = block_ptr;
123 /* get the next two bytes in the encoded data stream */
125 byte_a = s->buf[stream_ptr++];
126 byte_b = s->buf[stream_ptr++];
128 /* check if the decode is finished */
129 if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
131 else if ((byte_b & 0xFC) == 0x84) {
132 /* skip code, but don't count the current block */
133 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
134 } else if (byte_b < 0x80) {
135 /* 2-color encoding */
136 flags = (byte_b << 8) | byte_a;
139 colors[0] = s->buf[stream_ptr++];
140 colors[1] = s->buf[stream_ptr++];
142 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
143 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
144 pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
145 pixel_ptr -= row_dec;
147 } else if (byte_b >= 0x90) {
148 /* 8-color encoding */
149 flags = (byte_b << 8) | byte_a;
152 memcpy(colors, &s->buf[stream_ptr], 8);
155 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
156 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
157 pixels[pixel_ptr++] =
158 colors[((pixel_y & 0x2) << 1) +
159 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
160 pixel_ptr -= row_dec;
163 /* 1-color encoding */
166 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
167 for (pixel_x = 0; pixel_x < 4; pixel_x++)
168 pixels[pixel_ptr++] = colors[0];
169 pixel_ptr -= row_dec;
173 block_ptr += block_inc;
178 /* make the palette available on the way out */
179 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
180 memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
183 static void msvideo1_decode_16bit(Msvideo1Context *s)
185 int block_ptr, pixel_ptr;
187 int pixel_x, pixel_y; /* pixel width and height iterators */
188 int block_x, block_y; /* block width and height iterators */
189 int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
193 /* decoding parameters */
195 unsigned char byte_a, byte_b;
196 unsigned short flags;
198 unsigned short colors[8];
199 unsigned short *pixels = (unsigned short *)s->frame->data[0];
200 int stride = s->frame->linesize[0] / 2;
204 blocks_wide = s->avctx->width / 4;
205 blocks_high = s->avctx->height / 4;
206 total_blocks = blocks_wide * blocks_high;
208 row_dec = stride + 4;
210 for (block_y = blocks_high; block_y > 0; block_y--) {
211 block_ptr = ((block_y * 4) - 1) * stride;
212 for (block_x = blocks_wide; block_x > 0; block_x--) {
213 /* check if this block should be skipped */
215 block_ptr += block_inc;
221 pixel_ptr = block_ptr;
223 /* get the next two bytes in the encoded data stream */
225 byte_a = s->buf[stream_ptr++];
226 byte_b = s->buf[stream_ptr++];
228 /* check if the decode is finished */
229 if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
231 } else if ((byte_b & 0xFC) == 0x84) {
232 /* skip code, but don't count the current block */
233 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
234 } else if (byte_b < 0x80) {
235 /* 2- or 8-color encoding modes */
236 flags = (byte_b << 8) | byte_a;
239 colors[0] = AV_RL16(&s->buf[stream_ptr]);
241 colors[1] = AV_RL16(&s->buf[stream_ptr]);
244 if (colors[0] & 0x8000) {
245 /* 8-color encoding */
246 CHECK_STREAM_PTR(12);
247 colors[2] = AV_RL16(&s->buf[stream_ptr]);
249 colors[3] = AV_RL16(&s->buf[stream_ptr]);
251 colors[4] = AV_RL16(&s->buf[stream_ptr]);
253 colors[5] = AV_RL16(&s->buf[stream_ptr]);
255 colors[6] = AV_RL16(&s->buf[stream_ptr]);
257 colors[7] = AV_RL16(&s->buf[stream_ptr]);
260 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
261 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
262 pixels[pixel_ptr++] =
263 colors[((pixel_y & 0x2) << 1) +
264 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
265 pixel_ptr -= row_dec;
268 /* 2-color encoding */
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++] = colors[(flags & 0x1) ^ 1];
272 pixel_ptr -= row_dec;
276 /* otherwise, it's a 1-color block */
277 colors[0] = (byte_b << 8) | byte_a;
279 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
280 for (pixel_x = 0; pixel_x < 4; pixel_x++)
281 pixels[pixel_ptr++] = colors[0];
282 pixel_ptr -= row_dec;
286 block_ptr += block_inc;
292 static int msvideo1_decode_frame(AVCodecContext *avctx,
293 void *data, int *got_frame,
296 const uint8_t *buf = avpkt->data;
297 int buf_size = avpkt->size;
298 Msvideo1Context *s = avctx->priv_data;
304 // Discard frame if its smaller than the minimum frame size
305 if (buf_size < (avctx->width/4) * (avctx->height/4) / 512) {
306 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
307 return AVERROR_INVALIDDATA;
310 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
315 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
317 if (pal && size == AVPALETTE_SIZE) {
318 memcpy(s->pal, pal, AVPALETTE_SIZE);
319 s->frame->palette_has_changed = 1;
321 av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
326 msvideo1_decode_8bit(s);
328 msvideo1_decode_16bit(s);
330 if ((ret = av_frame_ref(data, s->frame)) < 0)
335 /* report that the buffer was completely consumed */
339 static av_cold int msvideo1_decode_end(AVCodecContext *avctx)
341 Msvideo1Context *s = avctx->priv_data;
343 av_frame_free(&s->frame);
348 AVCodec ff_msvideo1_decoder = {
350 .long_name = NULL_IF_CONFIG_SMALL("Microsoft Video 1"),
351 .type = AVMEDIA_TYPE_VIDEO,
352 .id = AV_CODEC_ID_MSVIDEO1,
353 .priv_data_size = sizeof(Msvideo1Context),
354 .init = msvideo1_decode_init,
355 .close = msvideo1_decode_end,
356 .decode = msvideo1_decode_frame,
357 .capabilities = AV_CODEC_CAP_DR1,