2 * Microsoft RLE decoder
3 * Copyright (C) 2008 Konstantin Shishkov
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 decoder based on decoder by Mike Melanson and my own for TSCC
25 * For more information about the MS RLE format, visit:
26 * http://www.multimedia.cx/msrle.txt
29 #include "libavutil/intreadwrite.h"
33 #define FETCH_NEXT_STREAM_BYTE() \
34 if (stream_ptr >= data_size) \
36 av_log(avctx, AV_LOG_ERROR, " MS RLE: stream ptr just went out of bounds (1)\n"); \
39 stream_byte = data[stream_ptr++];
41 static int msrle_decode_pal4(AVCodecContext *avctx, AVPicture *pic,
42 const uint8_t *data, int data_size)
45 unsigned char rle_code;
46 unsigned char extra_byte, odd_pixel;
47 unsigned char stream_byte;
48 unsigned int pixel_ptr = 0;
49 int row_dec = pic->linesize[0];
50 int row_ptr = (avctx->height - 1) * row_dec;
51 int frame_size = row_dec * avctx->height;
54 while (row_ptr >= 0) {
55 FETCH_NEXT_STREAM_BYTE();
56 rle_code = stream_byte;
58 /* fetch the next byte to see how to handle escape code */
59 FETCH_NEXT_STREAM_BYTE();
60 if (stream_byte == 0) {
61 /* line is done, goto the next one */
64 } else if (stream_byte == 1) {
67 } else if (stream_byte == 2) {
68 /* reposition frame decode coordinates */
69 FETCH_NEXT_STREAM_BYTE();
70 pixel_ptr += stream_byte;
71 FETCH_NEXT_STREAM_BYTE();
72 row_ptr -= stream_byte * row_dec;
74 // copy pixels from encoded stream
75 odd_pixel = stream_byte & 1;
76 rle_code = (stream_byte + 1) / 2;
77 extra_byte = rle_code & 0x01;
78 if (row_ptr + pixel_ptr + stream_byte > frame_size) {
79 av_log(avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
83 for (i = 0; i < rle_code; i++) {
84 if (pixel_ptr >= avctx->width)
86 FETCH_NEXT_STREAM_BYTE();
87 pic->data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
89 if (i + 1 == rle_code && odd_pixel)
91 if (pixel_ptr >= avctx->width)
93 pic->data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
97 // if the RLE code is odd, skip a byte in the stream
102 // decode a run of data
103 if (row_ptr + pixel_ptr + stream_byte > frame_size) {
104 av_log(avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
107 FETCH_NEXT_STREAM_BYTE();
108 for (i = 0; i < rle_code; i++) {
109 if (pixel_ptr >= avctx->width)
112 pic->data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
114 pic->data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
120 /* one last sanity check on the way out */
121 if (stream_ptr < data_size) {
122 av_log(avctx, AV_LOG_ERROR, " MS RLE: ended frame decode with bytes left over (%d < %d)\n",
123 stream_ptr, data_size);
131 static int msrle_decode_8_16_24_32(AVCodecContext *avctx, AVPicture *pic, int depth,
132 const uint8_t *data, int srcsize)
134 uint8_t *output, *output_end;
135 const uint8_t* src = data;
136 int p1, p2, line=avctx->height - 1, pos=0, i;
137 uint16_t av_uninit(pix16);
138 uint32_t av_uninit(pix32);
139 unsigned int width= FFABS(pic->linesize[0]) / (depth >> 3);
141 output = pic->data[0] + (avctx->height - 1) * pic->linesize[0];
142 output_end = pic->data[0] + avctx->height * pic->linesize[0];
143 while(src + 1 < data + srcsize) {
145 if(p1 == 0) { //Escape code
147 if(p2 == 0) { //End-of-line
148 output = pic->data[0] + (--line) * pic->linesize[0];
149 if (line < 0 && !(src+1 < data + srcsize && AV_RB16(src) == 1)) {
150 av_log(avctx, AV_LOG_ERROR, "Next line is beyond picture bounds\n");
155 } else if(p2 == 1) { //End-of-picture
157 } else if(p2 == 2) { //Skip
162 if (line < 0 || pos >= width){
163 av_log(avctx, AV_LOG_ERROR, "Skip beyond picture bounds\n");
166 output = pic->data[0] + line * pic->linesize[0] + pos * (depth >> 3);
170 if ((pic->linesize[0] > 0 && output + p2 * (depth >> 3) > output_end)
171 ||(pic->linesize[0] < 0 && output + p2 * (depth >> 3) < output_end)) {
172 src += p2 * (depth >> 3);
175 if(data + srcsize - src < p2 * (depth >> 3)){
176 av_log(avctx, AV_LOG_ERROR, "Copy beyond input buffer\n");
179 if ((depth == 8) || (depth == 24)) {
180 for(i = 0; i < p2 * (depth >> 3); i++) {
183 // RLE8 copy is actually padded - and runs are not!
184 if(depth == 8 && (p2 & 1)) {
187 } else if (depth == 16) {
188 for(i = 0; i < p2; i++) {
189 pix16 = AV_RL16(src);
191 *(uint16_t*)output = pix16;
194 } else if (depth == 32) {
195 for(i = 0; i < p2; i++) {
196 pix32 = AV_RL32(src);
198 *(uint32_t*)output = pix32;
203 } else { //run of pixels
204 uint8_t pix[3]; //original pixel
206 case 8: pix[0] = *src++;
208 case 16: pix16 = AV_RL16(src);
211 case 24: pix[0] = *src++;
215 case 32: pix32 = AV_RL32(src);
219 if ((pic->linesize[0] > 0 && output + p1 * (depth >> 3) > output_end)
220 ||(pic->linesize[0] < 0 && output + p1 * (depth >> 3) < output_end))
222 for(i = 0; i < p1; i++) {
224 case 8: *output++ = pix[0];
226 case 16: *(uint16_t*)output = pix16;
229 case 24: *output++ = pix[0];
233 case 32: *(uint32_t*)output = pix32;
242 av_log(avctx, AV_LOG_WARNING, "MS RLE warning: no end-of-picture code\n");
247 int ff_msrle_decode(AVCodecContext *avctx, AVPicture *pic, int depth,
248 const uint8_t* data, int data_size)
252 return msrle_decode_pal4(avctx, pic, data, data_size);
257 return msrle_decode_8_16_24_32(avctx, pic, depth, data, data_size);
259 av_log(avctx, AV_LOG_ERROR, "Unknown depth %d\n", depth);