2 * Micrsoft RLE Video Decoder
3 * Copyright (C) 2003 the ffmpeg project
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * MS RLE Video Decoder by Mike Melanson (melanson@pcisys.net)
23 * For more information about the MS RLE format, visit:
24 * http://www.pcisys.net/~melanson/codecs/
26 * The MS RLE decoder outputs PAL8 colorspace data.
28 * Note that this decoder expects the palette colors from the end of the
29 * BITMAPINFO header passed through extradata.
41 typedef struct MsrleContext
{
42 AVCodecContext
*avctx
;
49 unsigned int palette
[256];
52 #define FETCH_NEXT_STREAM_BYTE() \
53 if (stream_ptr >= s->size) \
55 printf(" MS RLE: stream ptr just went out of bounds (1)\n"); \
58 stream_byte = s->buf[stream_ptr++];
60 static void msrle_decode_pal8(MsrleContext
*s
)
63 unsigned char rle_code
;
64 unsigned char extra_byte
;
65 unsigned char stream_byte
;
67 int row_dec
= s
->frame
.linesize
[0];
68 int row_ptr
= (s
->avctx
->height
- 1) * row_dec
;
69 int frame_size
= row_dec
* s
->avctx
->height
;
71 while (row_ptr
>= 0) {
72 FETCH_NEXT_STREAM_BYTE();
73 rle_code
= stream_byte
;
75 /* fetch the next byte to see how to handle escape code */
76 FETCH_NEXT_STREAM_BYTE();
77 if (stream_byte
== 0) {
78 /* line is done, goto the next one */
81 } else if (stream_byte
== 1) {
84 } else if (stream_byte
== 2) {
85 /* reposition frame decode coordinates */
86 FETCH_NEXT_STREAM_BYTE();
87 pixel_ptr
+= stream_byte
;
88 FETCH_NEXT_STREAM_BYTE();
89 row_ptr
-= stream_byte
* row_dec
;
91 /* copy pixels from encoded stream */
92 if ((row_ptr
+ pixel_ptr
+ stream_byte
> frame_size
) ||
94 printf(" MS RLE: frame ptr just went out of bounds (1)\n");
98 rle_code
= stream_byte
;
99 extra_byte
= stream_byte
& 0x01;
100 if (stream_ptr
+ rle_code
+ extra_byte
> s
->size
) {
101 printf(" MS RLE: stream ptr just went out of bounds (2)\n");
106 FETCH_NEXT_STREAM_BYTE();
107 s
->frame
.data
[0][row_ptr
+ pixel_ptr
] = stream_byte
;
111 /* if the RLE code is odd, skip a byte in the stream */
116 /* decode a run of data */
117 if ((row_ptr
+ pixel_ptr
+ stream_byte
> frame_size
) ||
119 printf(" MS RLE: frame ptr just went out of bounds (2)\n");
123 FETCH_NEXT_STREAM_BYTE();
126 s
->frame
.data
[0][row_ptr
+ pixel_ptr
] = stream_byte
;
132 /* make the palette available */
133 memcpy(s
->frame
.data
[1], s
->palette
, 256 * 4);
135 /* one last sanity check on the way out */
136 if (stream_ptr
< s
->size
)
137 printf(" MS RLE: ended frame decode with bytes left over (%d < %d)\n",
138 stream_ptr
, s
->size
);
141 static int msrle_decode_init(AVCodecContext
*avctx
)
143 MsrleContext
*s
= (MsrleContext
*)avctx
->priv_data
;
145 unsigned char *palette
;
149 avctx
->pix_fmt
= PIX_FMT_PAL8
;
150 avctx
->has_b_frames
= 0;
151 s
->frame
.data
[0] = s
->prev_frame
.data
[0] = NULL
;
153 /* convert palette */
154 palette
= (unsigned char *)s
->avctx
->extradata
;
155 memset (s
->palette
, 0, 256 * 4);
156 for (i
= 0, j
= 0; i
< s
->avctx
->extradata_size
/ 4; i
++, j
+= 4)
158 (palette
[j
+ 2] << 16) |
159 (palette
[j
+ 1] << 8) |
160 (palette
[j
+ 0] << 0);
165 static int msrle_decode_frame(AVCodecContext
*avctx
,
166 void *data
, int *data_size
,
167 uint8_t *buf
, int buf_size
)
169 MsrleContext
*s
= (MsrleContext
*)avctx
->priv_data
;
174 if (avctx
->get_buffer(avctx
, &s
->frame
)) {
175 printf (" MS RLE: get_buffer() failed\n");
179 /* grossly inefficient, but...oh well */
180 if (s
->prev_frame
.data
[0] != NULL
)
181 memcpy(s
->frame
.data
[0], s
->prev_frame
.data
[0],
182 s
->frame
.linesize
[0] * s
->avctx
->height
);
184 msrle_decode_pal8(s
);
186 if (s
->frame
.data
[0])
187 avctx
->release_buffer(avctx
, &s
->frame
);
190 s
->prev_frame
= s
->frame
;
192 *data_size
= sizeof(AVFrame
);
193 *(AVFrame
*)data
= s
->frame
;
195 /* report that the buffer was completely consumed */
199 static int msrle_decode_end(AVCodecContext
*avctx
)
201 MsrleContext
*s
= (MsrleContext
*)avctx
->priv_data
;
203 /* release the last frame */
204 if (s
->prev_frame
.data
[0])
205 avctx
->release_buffer(avctx
, &s
->prev_frame
);
210 AVCodec msrle_decoder
= {
214 sizeof(MsrleContext
),