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 static int msrle_decode_pal4(AVCodecContext *avctx, AVFrame *pic,
36 unsigned char rle_code;
37 unsigned char extra_byte, odd_pixel;
38 unsigned char stream_byte;
40 int line = avctx->height - 1;
43 while (line >= 0 && pixel_ptr <= avctx->width) {
44 if (bytestream2_get_bytes_left(gb) <= 0) {
45 av_log(avctx, AV_LOG_ERROR,
46 "MS RLE: bytestream overrun, %dx%d left\n",
47 avctx->width - pixel_ptr, line);
48 return AVERROR_INVALIDDATA;
50 rle_code = stream_byte = bytestream2_get_byteu(gb);
52 /* fetch the next byte to see how to handle escape code */
53 stream_byte = bytestream2_get_byte(gb);
54 if (stream_byte == 0) {
55 /* line is done, goto the next one */
58 } else if (stream_byte == 1) {
61 } else if (stream_byte == 2) {
62 /* reposition frame decode coordinates */
63 stream_byte = bytestream2_get_byte(gb);
64 pixel_ptr += stream_byte;
65 stream_byte = bytestream2_get_byte(gb);
68 // copy pixels from encoded stream
69 odd_pixel = stream_byte & 1;
70 rle_code = (stream_byte + 1) / 2;
71 extra_byte = rle_code & 0x01;
72 if (pixel_ptr + 2*rle_code - odd_pixel > avctx->width ||
73 bytestream2_get_bytes_left(gb) < rle_code) {
74 av_log(avctx, AV_LOG_ERROR,
75 "MS RLE: frame/stream ptr just went out of bounds (copy)\n");
76 return AVERROR_INVALIDDATA;
79 for (i = 0; i < rle_code; i++) {
80 if (pixel_ptr >= avctx->width)
82 stream_byte = bytestream2_get_byteu(gb);
83 pic->data[0][line * pic->linesize[0] + pixel_ptr] = stream_byte >> 4;
85 if (i + 1 == rle_code && odd_pixel)
87 if (pixel_ptr >= avctx->width)
89 pic->data[0][line * pic->linesize[0] + pixel_ptr] = stream_byte & 0x0F;
93 // if the RLE code is odd, skip a byte in the stream
95 bytestream2_skip(gb, 1);
98 // decode a run of data
99 if (pixel_ptr + rle_code > avctx->width + 1) {
100 av_log(avctx, AV_LOG_ERROR,
101 "MS RLE: frame ptr just went out of bounds (run) %d %d %d\n", pixel_ptr, rle_code, avctx->width);
102 return AVERROR_INVALIDDATA;
104 stream_byte = bytestream2_get_byte(gb);
105 for (i = 0; i < rle_code; i++) {
106 if (pixel_ptr >= avctx->width)
109 pic->data[0][line * pic->linesize[0] + pixel_ptr] = stream_byte >> 4;
111 pic->data[0][line * pic->linesize[0] + pixel_ptr] = stream_byte & 0x0F;
117 /* one last sanity check on the way out */
118 if (bytestream2_get_bytes_left(gb)) {
119 av_log(avctx, AV_LOG_ERROR,
120 "MS RLE: ended frame decode with %d bytes left over\n",
121 bytestream2_get_bytes_left(gb));
122 return AVERROR_INVALIDDATA;
129 static int msrle_decode_8_16_24_32(AVCodecContext *avctx, AVFrame *pic,
130 int depth, GetByteContext *gb)
132 uint8_t *output, *output_end;
133 int p1, p2, line=avctx->height - 1, pos=0, i;
136 unsigned int width= FFABS(pic->linesize[0]) / (depth >> 3);
138 output = pic->data[0] + (avctx->height - 1) * pic->linesize[0];
139 output_end = output + FFABS(pic->linesize[0]);
141 while (bytestream2_get_bytes_left(gb) > 0) {
142 p1 = bytestream2_get_byteu(gb);
143 if(p1 == 0) { //Escape code
144 p2 = bytestream2_get_byte(gb);
145 if(p2 == 0) { //End-of-line
147 if (bytestream2_get_be16(gb) == 1) { // end-of-picture
150 av_log(avctx, AV_LOG_ERROR,
151 "Next line is beyond picture bounds (%d bytes left)\n",
152 bytestream2_get_bytes_left(gb));
153 return AVERROR_INVALIDDATA;
156 output = pic->data[0] + line * pic->linesize[0];
157 output_end = output + FFABS(pic->linesize[0]);
160 } else if(p2 == 1) { //End-of-picture
162 } else if(p2 == 2) { //Skip
163 p1 = bytestream2_get_byte(gb);
164 p2 = bytestream2_get_byte(gb);
167 if (line < 0 || pos >= width){
168 av_log(avctx, AV_LOG_ERROR, "Skip beyond picture bounds\n");
171 output = pic->data[0] + line * pic->linesize[0] + pos * (depth >> 3);
172 output_end = pic->data[0] + line * pic->linesize[0] + FFABS(pic->linesize[0]);
176 if (output + p2 * (depth >> 3) > output_end) {
177 bytestream2_skip(gb, 2 * (depth >> 3));
179 } else if (bytestream2_get_bytes_left(gb) < p2 * (depth >> 3)) {
180 av_log(avctx, AV_LOG_ERROR, "bytestream overrun\n");
181 return AVERROR_INVALIDDATA;
184 if ((depth == 8) || (depth == 24)) {
185 bytestream2_get_bufferu(gb, output, p2 * (depth >> 3));
186 output += p2 * (depth >> 3);
188 // RLE8 copy is actually padded - and runs are not!
189 if(depth == 8 && (p2 & 1)) {
190 bytestream2_skip(gb, 1);
192 } else if (depth == 16) {
193 for(i = 0; i < p2; i++) {
194 *(uint16_t*)output = bytestream2_get_le16u(gb);
197 } else if (depth == 32) {
198 for(i = 0; i < p2; i++) {
199 *(uint32_t*)output = bytestream2_get_le32u(gb);
204 } else { //run of pixels
205 uint8_t pix[3]; //original pixel
206 if (output + p1 * (depth >> 3) > output_end)
211 pix[0] = bytestream2_get_byte(gb);
212 memset(output, pix[0], p1);
216 pix16 = bytestream2_get_le16(gb);
217 for(i = 0; i < p1; i++) {
218 *(uint16_t*)output = pix16;
223 pix[0] = bytestream2_get_byte(gb);
224 pix[1] = bytestream2_get_byte(gb);
225 pix[2] = bytestream2_get_byte(gb);
226 for(i = 0; i < p1; i++) {
233 pix32 = bytestream2_get_le32(gb);
234 for(i = 0; i < p1; i++) {
235 *(uint32_t*)output = pix32;
244 av_log(avctx, AV_LOG_WARNING, "MS RLE warning: no end-of-picture code\n");
249 int ff_msrle_decode(AVCodecContext *avctx, AVFrame *pic,
250 int depth, GetByteContext *gb)
254 return msrle_decode_pal4(avctx, pic, gb);
259 return msrle_decode_8_16_24_32(avctx, pic, depth, gb);
261 av_log(avctx, AV_LOG_ERROR, "Unknown depth %d\n", depth);