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 palctrl.
41 typedef struct MsrleContext
{
42 AVCodecContext
*avctx
;
51 #define FETCH_NEXT_STREAM_BYTE() \
52 if (stream_ptr >= s->size) \
54 printf(" MS RLE: stream ptr just went out of bounds (1)\n"); \
57 stream_byte = s->buf[stream_ptr++];
59 static void msrle_decode_pal8(MsrleContext
*s
)
62 unsigned char rle_code
;
63 unsigned char extra_byte
;
64 unsigned char stream_byte
;
66 int row_dec
= s
->frame
.linesize
[0];
67 int row_ptr
= (s
->avctx
->height
- 1) * row_dec
;
68 int frame_size
= row_dec
* s
->avctx
->height
;
70 while (row_ptr
>= 0) {
71 FETCH_NEXT_STREAM_BYTE();
72 rle_code
= stream_byte
;
74 /* fetch the next byte to see how to handle escape code */
75 FETCH_NEXT_STREAM_BYTE();
76 if (stream_byte
== 0) {
77 /* line is done, goto the next one */
80 } else if (stream_byte
== 1) {
83 } else if (stream_byte
== 2) {
84 /* reposition frame decode coordinates */
85 FETCH_NEXT_STREAM_BYTE();
86 pixel_ptr
+= stream_byte
;
87 FETCH_NEXT_STREAM_BYTE();
88 row_ptr
-= stream_byte
* row_dec
;
90 /* copy pixels from encoded stream */
91 if ((row_ptr
+ pixel_ptr
+ stream_byte
> frame_size
) ||
93 printf(" MS RLE: frame ptr just went out of bounds (1)\n");
97 rle_code
= stream_byte
;
98 extra_byte
= stream_byte
& 0x01;
99 if (stream_ptr
+ rle_code
+ extra_byte
> s
->size
) {
100 printf(" MS RLE: stream ptr just went out of bounds (2)\n");
105 FETCH_NEXT_STREAM_BYTE();
106 s
->frame
.data
[0][row_ptr
+ pixel_ptr
] = stream_byte
;
110 /* if the RLE code is odd, skip a byte in the stream */
115 /* decode a run of data */
116 if ((row_ptr
+ pixel_ptr
+ stream_byte
> frame_size
) ||
118 printf(" MS RLE: frame ptr just went out of bounds (2)\n");
122 FETCH_NEXT_STREAM_BYTE();
125 s
->frame
.data
[0][row_ptr
+ pixel_ptr
] = stream_byte
;
131 /* make the palette available */
132 memcpy(s
->frame
.data
[1], s
->avctx
->palctrl
->palette
, AVPALETTE_SIZE
);
133 if (s
->avctx
->palctrl
->palette_changed
) {
134 s
->frame
.palette_has_changed
= 1;
135 s
->avctx
->palctrl
->palette_changed
= 0;
138 /* one last sanity check on the way out */
139 if (stream_ptr
< s
->size
)
140 printf(" MS RLE: ended frame decode with bytes left over (%d < %d)\n",
141 stream_ptr
, s
->size
);
144 static int msrle_decode_init(AVCodecContext
*avctx
)
146 MsrleContext
*s
= (MsrleContext
*)avctx
->priv_data
;
150 avctx
->pix_fmt
= PIX_FMT_PAL8
;
151 avctx
->has_b_frames
= 0;
152 s
->frame
.data
[0] = s
->prev_frame
.data
[0] = NULL
;
157 static int msrle_decode_frame(AVCodecContext
*avctx
,
158 void *data
, int *data_size
,
159 uint8_t *buf
, int buf_size
)
161 MsrleContext
*s
= (MsrleContext
*)avctx
->priv_data
;
166 s
->frame
.reference
= 1;
167 if (avctx
->get_buffer(avctx
, &s
->frame
)) {
168 printf (" MS RLE: get_buffer() failed\n");
172 if (s
->prev_frame
.data
[0] && (s
->frame
.linesize
[0] != s
->prev_frame
.linesize
[0]))
173 printf (" MS RLE: Buffer linesize changed: current %u, previous %u.\n"
174 " Expect wrong image and/or crash!\n",
175 s
->frame
.linesize
[0], s
->prev_frame
.linesize
[0]);
177 /* grossly inefficient, but...oh well */
178 if (s
->prev_frame
.data
[0] != NULL
)
179 memcpy(s
->frame
.data
[0], s
->prev_frame
.data
[0],
180 s
->frame
.linesize
[0] * s
->avctx
->height
);
182 msrle_decode_pal8(s
);
184 if (s
->prev_frame
.data
[0])
185 avctx
->release_buffer(avctx
, &s
->prev_frame
);
188 s
->prev_frame
= s
->frame
;
190 *data_size
= sizeof(AVFrame
);
191 *(AVFrame
*)data
= s
->frame
;
193 /* report that the buffer was completely consumed */
197 static int msrle_decode_end(AVCodecContext
*avctx
)
199 MsrleContext
*s
= (MsrleContext
*)avctx
->priv_data
;
201 /* release the last frame */
202 if (s
->prev_frame
.data
[0])
203 avctx
->release_buffer(avctx
, &s
->prev_frame
);
208 AVCodec msrle_decoder
= {
212 sizeof(MsrleContext
),