2 * Micrsoft RLE Video 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 * MS RLE Video Decoder by Mike Melanson (melanson@pcisys.net)
25 * For more information about the MS RLE format, visit:
26 * http://www.pcisys.net/~melanson/codecs/
28 * The MS RLE decoder outputs PAL8 colorspace data.
30 * Note that this decoder expects the palette colors from the end of the
31 * BITMAPINFO header passed through palctrl.
43 typedef struct MsrleContext {
44 AVCodecContext *avctx;
52 #define FETCH_NEXT_STREAM_BYTE() \
53 if (stream_ptr >= s->size) \
55 av_log(s->avctx, AV_LOG_ERROR, " MS RLE: stream ptr just went out of bounds (1)\n"); \
58 stream_byte = s->buf[stream_ptr++];
60 static void msrle_decode_pal4(MsrleContext *s)
63 unsigned char rle_code;
64 unsigned char extra_byte, odd_pixel;
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;
72 /* make the palette available */
73 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
74 if (s->avctx->palctrl->palette_changed) {
75 s->frame.palette_has_changed = 1;
76 s->avctx->palctrl->palette_changed = 0;
79 while (row_ptr >= 0) {
80 FETCH_NEXT_STREAM_BYTE();
81 rle_code = stream_byte;
83 /* fetch the next byte to see how to handle escape code */
84 FETCH_NEXT_STREAM_BYTE();
85 if (stream_byte == 0) {
86 /* line is done, goto the next one */
89 } else if (stream_byte == 1) {
92 } else if (stream_byte == 2) {
93 /* reposition frame decode coordinates */
94 FETCH_NEXT_STREAM_BYTE();
95 pixel_ptr += stream_byte;
96 FETCH_NEXT_STREAM_BYTE();
97 row_ptr -= stream_byte * row_dec;
99 // copy pixels from encoded stream
100 odd_pixel = stream_byte & 1;
101 rle_code = (stream_byte + 1) / 2;
102 extra_byte = rle_code & 0x01;
103 if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
105 av_log(s->avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
109 for (i = 0; i < rle_code; i++) {
110 if (pixel_ptr >= s->avctx->width)
112 FETCH_NEXT_STREAM_BYTE();
113 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
115 if (i + 1 == rle_code && odd_pixel)
117 if (pixel_ptr >= s->avctx->width)
119 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
123 // if the RLE code is odd, skip a byte in the stream
128 // decode a run of data
129 if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
131 av_log(s->avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
134 FETCH_NEXT_STREAM_BYTE();
135 for (i = 0; i < rle_code; i++) {
136 if (pixel_ptr >= s->avctx->width)
139 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
141 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
147 /* one last sanity check on the way out */
148 if (stream_ptr < s->size)
149 av_log(s->avctx, AV_LOG_ERROR, " MS RLE: ended frame decode with bytes left over (%d < %d)\n",
150 stream_ptr, s->size);
155 static void msrle_decode_pal8(MsrleContext *s)
158 unsigned char rle_code;
159 unsigned char extra_byte;
160 unsigned char stream_byte;
162 int row_dec = s->frame.linesize[0];
163 int row_ptr = (s->avctx->height - 1) * row_dec;
164 int frame_size = row_dec * s->avctx->height;
166 /* make the palette available */
167 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
168 if (s->avctx->palctrl->palette_changed) {
169 s->frame.palette_has_changed = 1;
170 s->avctx->palctrl->palette_changed = 0;
173 while (row_ptr >= 0) {
174 FETCH_NEXT_STREAM_BYTE();
175 rle_code = stream_byte;
177 /* fetch the next byte to see how to handle escape code */
178 FETCH_NEXT_STREAM_BYTE();
179 if (stream_byte == 0) {
180 /* line is done, goto the next one */
183 } else if (stream_byte == 1) {
186 } else if (stream_byte == 2) {
187 /* reposition frame decode coordinates */
188 FETCH_NEXT_STREAM_BYTE();
189 pixel_ptr += stream_byte;
190 FETCH_NEXT_STREAM_BYTE();
191 row_ptr -= stream_byte * row_dec;
193 /* copy pixels from encoded stream */
194 if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
196 av_log(s->avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
200 rle_code = stream_byte;
201 extra_byte = stream_byte & 0x01;
202 if (stream_ptr + rle_code + extra_byte > s->size) {
203 av_log(s->avctx, AV_LOG_ERROR, " MS RLE: stream ptr just went out of bounds (2)\n");
208 FETCH_NEXT_STREAM_BYTE();
209 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
213 /* if the RLE code is odd, skip a byte in the stream */
218 /* decode a run of data */
219 if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
221 av_log(s->avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (2)\n");
225 FETCH_NEXT_STREAM_BYTE();
228 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
234 /* one last sanity check on the way out */
235 if (stream_ptr < s->size)
236 av_log(s->avctx, AV_LOG_ERROR, " MS RLE: ended frame decode with bytes left over (%d < %d)\n",
237 stream_ptr, s->size);
240 static int msrle_decode_init(AVCodecContext *avctx)
242 MsrleContext *s = (MsrleContext *)avctx->priv_data;
246 avctx->pix_fmt = PIX_FMT_PAL8;
247 avctx->has_b_frames = 0;
248 s->frame.data[0] = NULL;
253 static int msrle_decode_frame(AVCodecContext *avctx,
254 void *data, int *data_size,
255 uint8_t *buf, int buf_size)
257 MsrleContext *s = (MsrleContext *)avctx->priv_data;
262 s->frame.reference = 1;
263 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
264 if (avctx->reget_buffer(avctx, &s->frame)) {
265 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
269 switch (avctx->bits_per_sample) {
271 msrle_decode_pal8(s);
274 msrle_decode_pal4(s);
277 av_log(avctx, AV_LOG_ERROR, "Don't know how to decode depth %u.\n",
278 avctx->bits_per_sample);
281 *data_size = sizeof(AVFrame);
282 *(AVFrame*)data = s->frame;
284 /* report that the buffer was completely consumed */
288 static int msrle_decode_end(AVCodecContext *avctx)
290 MsrleContext *s = (MsrleContext *)avctx->priv_data;
292 /* release the last frame */
293 if (s->frame.data[0])
294 avctx->release_buffer(avctx, &s->frame);
299 AVCodec msrle_decoder = {
303 sizeof(MsrleContext),