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/
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
;
71 avctx
->pix_fmt
= AV_PIX_FMT_RGB555
;
74 s
->frame
= av_frame_alloc();
76 return AVERROR(ENOMEM
);
81 static void msvideo1_decode_8bit(Msvideo1Context
*s
)
83 int block_ptr
, pixel_ptr
;
85 int pixel_x
, pixel_y
; /* pixel width and height iterators */
86 int block_x
, block_y
; /* block width and height iterators */
87 int blocks_wide
, blocks_high
; /* width and height in 4x4 blocks */
91 /* decoding parameters */
93 unsigned char byte_a
, byte_b
;
96 unsigned char colors
[8];
97 unsigned char *pixels
= s
->frame
->data
[0];
98 int stride
= s
->frame
->linesize
[0];
102 blocks_wide
= s
->avctx
->width
/ 4;
103 blocks_high
= s
->avctx
->height
/ 4;
104 total_blocks
= blocks_wide
* blocks_high
;
106 row_dec
= stride
+ 4;
108 for (block_y
= blocks_high
; block_y
> 0; block_y
--) {
109 block_ptr
= ((block_y
* 4) - 1) * stride
;
110 for (block_x
= blocks_wide
; block_x
> 0; block_x
--) {
111 /* check if this block should be skipped */
113 block_ptr
+= block_inc
;
119 pixel_ptr
= block_ptr
;
121 /* get the next two bytes in the encoded data stream */
123 byte_a
= s
->buf
[stream_ptr
++];
124 byte_b
= s
->buf
[stream_ptr
++];
126 /* check if the decode is finished */
127 if ((byte_a
== 0) && (byte_b
== 0) && (total_blocks
== 0))
129 else if ((byte_b
& 0xFC) == 0x84) {
130 /* skip code, but don't count the current block */
131 skip_blocks
= ((byte_b
- 0x84) << 8) + byte_a
- 1;
132 } else if (byte_b
< 0x80) {
133 /* 2-color encoding */
134 flags
= (byte_b
<< 8) | byte_a
;
137 colors
[0] = s
->buf
[stream_ptr
++];
138 colors
[1] = s
->buf
[stream_ptr
++];
140 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
141 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++, flags
>>= 1)
142 pixels
[pixel_ptr
++] = colors
[(flags
& 0x1) ^ 1];
143 pixel_ptr
-= row_dec
;
145 } else if (byte_b
>= 0x90) {
146 /* 8-color encoding */
147 flags
= (byte_b
<< 8) | byte_a
;
150 memcpy(colors
, &s
->buf
[stream_ptr
], 8);
153 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
154 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++, flags
>>= 1)
155 pixels
[pixel_ptr
++] =
156 colors
[((pixel_y
& 0x2) << 1) +
157 (pixel_x
& 0x2) + ((flags
& 0x1) ^ 1)];
158 pixel_ptr
-= row_dec
;
161 /* 1-color encoding */
164 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
165 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++)
166 pixels
[pixel_ptr
++] = colors
[0];
167 pixel_ptr
-= row_dec
;
171 block_ptr
+= block_inc
;
176 /* make the palette available on the way out */
177 if (s
->avctx
->pix_fmt
== AV_PIX_FMT_PAL8
)
178 memcpy(s
->frame
->data
[1], s
->pal
, AVPALETTE_SIZE
);
181 static void msvideo1_decode_16bit(Msvideo1Context
*s
)
183 int block_ptr
, pixel_ptr
;
185 int pixel_x
, pixel_y
; /* pixel width and height iterators */
186 int block_x
, block_y
; /* block width and height iterators */
187 int blocks_wide
, blocks_high
; /* width and height in 4x4 blocks */
191 /* decoding parameters */
193 unsigned char byte_a
, byte_b
;
194 unsigned short flags
;
196 unsigned short colors
[8];
197 unsigned short *pixels
= (unsigned short *)s
->frame
->data
[0];
198 int stride
= s
->frame
->linesize
[0] / 2;
202 blocks_wide
= s
->avctx
->width
/ 4;
203 blocks_high
= s
->avctx
->height
/ 4;
204 total_blocks
= blocks_wide
* blocks_high
;
206 row_dec
= stride
+ 4;
208 for (block_y
= blocks_high
; block_y
> 0; block_y
--) {
209 block_ptr
= ((block_y
* 4) - 1) * stride
;
210 for (block_x
= blocks_wide
; block_x
> 0; block_x
--) {
211 /* check if this block should be skipped */
213 block_ptr
+= block_inc
;
219 pixel_ptr
= block_ptr
;
221 /* get the next two bytes in the encoded data stream */
223 byte_a
= s
->buf
[stream_ptr
++];
224 byte_b
= s
->buf
[stream_ptr
++];
226 /* check if the decode is finished */
227 if ((byte_a
== 0) && (byte_b
== 0) && (total_blocks
== 0)) {
229 } else if ((byte_b
& 0xFC) == 0x84) {
230 /* skip code, but don't count the current block */
231 skip_blocks
= ((byte_b
- 0x84) << 8) + byte_a
- 1;
232 } else if (byte_b
< 0x80) {
233 /* 2- or 8-color encoding modes */
234 flags
= (byte_b
<< 8) | byte_a
;
237 colors
[0] = AV_RL16(&s
->buf
[stream_ptr
]);
239 colors
[1] = AV_RL16(&s
->buf
[stream_ptr
]);
242 if (colors
[0] & 0x8000) {
243 /* 8-color encoding */
244 CHECK_STREAM_PTR(12);
245 colors
[2] = AV_RL16(&s
->buf
[stream_ptr
]);
247 colors
[3] = AV_RL16(&s
->buf
[stream_ptr
]);
249 colors
[4] = AV_RL16(&s
->buf
[stream_ptr
]);
251 colors
[5] = AV_RL16(&s
->buf
[stream_ptr
]);
253 colors
[6] = AV_RL16(&s
->buf
[stream_ptr
]);
255 colors
[7] = AV_RL16(&s
->buf
[stream_ptr
]);
258 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
259 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++, flags
>>= 1)
260 pixels
[pixel_ptr
++] =
261 colors
[((pixel_y
& 0x2) << 1) +
262 (pixel_x
& 0x2) + ((flags
& 0x1) ^ 1)];
263 pixel_ptr
-= row_dec
;
266 /* 2-color encoding */
267 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
268 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++, flags
>>= 1)
269 pixels
[pixel_ptr
++] = colors
[(flags
& 0x1) ^ 1];
270 pixel_ptr
-= row_dec
;
274 /* otherwise, it's a 1-color block */
275 colors
[0] = (byte_b
<< 8) | byte_a
;
277 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
278 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++)
279 pixels
[pixel_ptr
++] = colors
[0];
280 pixel_ptr
-= row_dec
;
284 block_ptr
+= block_inc
;
290 static int msvideo1_decode_frame(AVCodecContext
*avctx
,
291 void *data
, int *got_frame
,
294 const uint8_t *buf
= avpkt
->data
;
295 int buf_size
= avpkt
->size
;
296 Msvideo1Context
*s
= avctx
->priv_data
;
302 if ((ret
= ff_reget_buffer(avctx
, s
->frame
)) < 0) {
303 av_log(s
->avctx
, AV_LOG_ERROR
, "reget_buffer() failed\n");
308 const uint8_t *pal
= av_packet_get_side_data(avpkt
, AV_PKT_DATA_PALETTE
, NULL
);
311 memcpy(s
->pal
, pal
, AVPALETTE_SIZE
);
312 s
->frame
->palette_has_changed
= 1;
317 msvideo1_decode_8bit(s
);
319 msvideo1_decode_16bit(s
);
321 if ((ret
= av_frame_ref(data
, s
->frame
)) < 0)
326 /* report that the buffer was completely consumed */
330 static av_cold
int msvideo1_decode_end(AVCodecContext
*avctx
)
332 Msvideo1Context
*s
= avctx
->priv_data
;
334 av_frame_free(&s
->frame
);
339 AVCodec ff_msvideo1_decoder
= {
341 .long_name
= NULL_IF_CONFIG_SMALL("Microsoft Video 1"),
342 .type
= AVMEDIA_TYPE_VIDEO
,
343 .id
= AV_CODEC_ID_MSVIDEO1
,
344 .priv_data_size
= sizeof(Msvideo1Context
),
345 .init
= msvideo1_decode_init
,
346 .close
= msvideo1_decode_end
,
347 .decode
= msvideo1_decode_frame
,
348 .capabilities
= AV_CODEC_CAP_DR1
,