msrledec.c 9.59 KB
Newer Older
1
/*
2
 * Microsoft RLE decoder
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Copyright (C) 2008 Konstantin Shishkov
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23
 * @file
24
 * MS RLE decoder based on decoder by Mike Melanson and my own for TSCC
25 26 27 28
 * For more information about the MS RLE format, visit:
 *   http://www.multimedia.cx/msrle.txt
 */

29
#include "libavutil/intreadwrite.h"
30
#include "avcodec.h"
31
#include "msrledec.h"
32

33
static int msrle_decode_pal4(AVCodecContext *avctx, AVFrame *pic,
34
                             GetByteContext *gb)
35 36 37 38
{
    unsigned char rle_code;
    unsigned char extra_byte, odd_pixel;
    unsigned char stream_byte;
39
    int pixel_ptr = 0;
40
    int line = avctx->height - 1;
41 42
    int i;

43
    while (line >= 0 && pixel_ptr <= avctx->width) {
44 45
        if (bytestream2_get_bytes_left(gb) <= 0) {
            av_log(avctx, AV_LOG_ERROR,
46 47
                   "MS RLE: bytestream overrun, %dx%d left\n",
                   avctx->width - pixel_ptr, line);
48 49 50
            return AVERROR_INVALIDDATA;
        }
        rle_code = stream_byte = bytestream2_get_byteu(gb);
51 52
        if (rle_code == 0) {
            /* fetch the next byte to see how to handle escape code */
53
            stream_byte = bytestream2_get_byte(gb);
54 55
            if (stream_byte == 0) {
                /* line is done, goto the next one */
56
                line--;
57 58 59 60 61 62
                pixel_ptr = 0;
            } else if (stream_byte == 1) {
                /* decode is done */
                return 0;
            } else if (stream_byte == 2) {
                /* reposition frame decode coordinates */
63
                stream_byte = bytestream2_get_byte(gb);
64
                pixel_ptr += stream_byte;
65
                stream_byte = bytestream2_get_byte(gb);
66
                line -= stream_byte;
Michael Niedermayer's avatar
Michael Niedermayer committed
67 68 69 70 71
            } else {
                // copy pixels from encoded stream
                odd_pixel =  stream_byte & 1;
                rle_code = (stream_byte + 1) / 2;
                extra_byte = rle_code & 0x01;
72
                if (pixel_ptr + 2*rle_code - odd_pixel > avctx->width ||
73 74 75 76
                    bytestream2_get_bytes_left(gb) < rle_code) {
                    av_log(avctx, AV_LOG_ERROR,
                           "MS RLE: frame/stream ptr just went out of bounds (copy)\n");
                    return AVERROR_INVALIDDATA;
Michael Niedermayer's avatar
Michael Niedermayer committed
77
                }
78

Michael Niedermayer's avatar
Michael Niedermayer committed
79 80 81
                for (i = 0; i < rle_code; i++) {
                    if (pixel_ptr >= avctx->width)
                        break;
82
                    stream_byte = bytestream2_get_byteu(gb);
83
                    pic->data[0][line * pic->linesize[0] + pixel_ptr] = stream_byte >> 4;
Michael Niedermayer's avatar
Michael Niedermayer committed
84 85 86 87 88
                    pixel_ptr++;
                    if (i + 1 == rle_code && odd_pixel)
                        break;
                    if (pixel_ptr >= avctx->width)
                        break;
89
                    pic->data[0][line * pic->linesize[0] + pixel_ptr] = stream_byte & 0x0F;
Michael Niedermayer's avatar
Michael Niedermayer committed
90 91
                    pixel_ptr++;
                }
92

Michael Niedermayer's avatar
Michael Niedermayer committed
93 94
                // if the RLE code is odd, skip a byte in the stream
                if (extra_byte)
95
                    bytestream2_skip(gb, 1);
96 97 98
            }
        } else {
            // 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;
103
            }
104
            stream_byte = bytestream2_get_byte(gb);
105 106 107 108
            for (i = 0; i < rle_code; i++) {
                if (pixel_ptr >= avctx->width)
                    break;
                if ((i & 1) == 0)
109
                    pic->data[0][line * pic->linesize[0] + pixel_ptr] = stream_byte >> 4;
110
                else
111
                    pic->data[0][line * pic->linesize[0] + pixel_ptr] = stream_byte & 0x0F;
112 113 114 115 116 117
                pixel_ptr++;
            }
        }
    }

    /* one last sanity check on the way out */
118 119 120 121 122
    if (bytestream2_get_bytes_left(gb)) {
        av_log(avctx, AV_LOG_ERROR,
               "MS RLE: ended frame decode with %d bytes left over\n",
               bytestream2_get_bytes_left(gb));
        return AVERROR_INVALIDDATA;
123 124 125 126 127 128
    }

    return 0;
}


129
static int msrle_decode_8_16_24_32(AVCodecContext *avctx, AVFrame *pic,
130
                                   int depth, GetByteContext *gb)
131 132
{
    uint8_t *output, *output_end;
133
    int p1, p2, line=avctx->height - 1, pos=0, i;
134 135
    uint16_t pix16;
    uint32_t pix32;
136
    unsigned int width= FFABS(pic->linesize[0]) / (depth >> 3);
137

138
    output     = pic->data[0] + (avctx->height - 1) * pic->linesize[0];
139 140
    output_end = output + FFABS(pic->linesize[0]);

141 142
    while (bytestream2_get_bytes_left(gb) > 0) {
        p1 = bytestream2_get_byteu(gb);
143
        if(p1 == 0) { //Escape code
144
            p2 = bytestream2_get_byte(gb);
145
            if(p2 == 0) { //End-of-line
146
                if (--line < 0) {
147 148 149 150 151 152 153 154
                    if (bytestream2_get_be16(gb) == 1) { // end-of-picture
                        return 0;
                    } else {
                        av_log(avctx, AV_LOG_ERROR,
                               "Next line is beyond picture bounds (%d bytes left)\n",
                               bytestream2_get_bytes_left(gb));
                        return AVERROR_INVALIDDATA;
                    }
155
                }
156
                output = pic->data[0] + line * pic->linesize[0];
157
                output_end = output + FFABS(pic->linesize[0]);
158 159 160 161 162
                pos = 0;
                continue;
            } else if(p2 == 1) { //End-of-picture
                return 0;
            } else if(p2 == 2) { //Skip
163 164
                p1 = bytestream2_get_byte(gb);
                p2 = bytestream2_get_byte(gb);
165
                line -= p2;
166 167
                pos += p1;
                if (line < 0 || pos >= width){
168
                    av_log(avctx, AV_LOG_ERROR, "Skip beyond picture bounds\n");
169
                    return -1;
170
                }
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]);
173 174 175
                continue;
            }
            // Copy data
176
            if (output + p2 * (depth >> 3) > output_end) {
177
                bytestream2_skip(gb, 2 * (depth >> 3));
178
                continue;
179 180 181
            } else if (bytestream2_get_bytes_left(gb) < p2 * (depth >> 3)) {
                av_log(avctx, AV_LOG_ERROR, "bytestream overrun\n");
                return AVERROR_INVALIDDATA;
182
            }
183

184
            if ((depth == 8) || (depth == 24)) {
185 186 187
                bytestream2_get_bufferu(gb, output, p2 * (depth >> 3));
                output += p2 * (depth >> 3);

188 189
                // RLE8 copy is actually padded - and runs are not!
                if(depth == 8 && (p2 & 1)) {
190
                    bytestream2_skip(gb, 1);
191 192 193
                }
            } else if (depth == 16) {
                for(i = 0; i < p2; i++) {
194
                    *(uint16_t*)output = bytestream2_get_le16u(gb);
195 196 197 198
                    output += 2;
                }
            } else if (depth == 32) {
                for(i = 0; i < p2; i++) {
199
                    *(uint32_t*)output = bytestream2_get_le32u(gb);
200 201 202 203
                    output += 4;
                }
            }
            pos += p2;
204
        } else { //run of pixels
205
            uint8_t pix[3]; //original pixel
206
            if (output + p1 * (depth >> 3) > output_end)
207
                continue;
208

209 210
            switch(depth){
            case  8:
211
                pix[0] = bytestream2_get_byte(gb);
Paul B Mahol's avatar
Paul B Mahol committed
212 213
                memset(output, pix[0], p1);
                output += p1;
214 215
                break;
            case 16:
216
                pix16  = bytestream2_get_le16(gb);
217 218 219 220 221 222
                for(i = 0; i < p1; i++) {
                        *(uint16_t*)output = pix16;
                        output += 2;
                }
                break;
            case 24:
223 224 225
                pix[0] = bytestream2_get_byte(gb);
                pix[1] = bytestream2_get_byte(gb);
                pix[2] = bytestream2_get_byte(gb);
226 227 228 229 230 231 232
                for(i = 0; i < p1; i++) {
                        *output++ = pix[0];
                        *output++ = pix[1];
                        *output++ = pix[2];
                }
                break;
            case 32:
233
                pix32  = bytestream2_get_le32(gb);
234 235 236
                for(i = 0; i < p1; i++) {
                        *(uint32_t*)output = pix32;
                        output += 4;
237
                }
238
                break;
239 240 241 242 243
            }
            pos += p1;
        }
    }

244
    av_log(avctx, AV_LOG_WARNING, "MS RLE warning: no end-of-picture code\n");
245 246 247 248
    return 0;
}


249
int ff_msrle_decode(AVCodecContext *avctx, AVFrame *pic,
250
                    int depth, GetByteContext *gb)
251 252 253
{
    switch(depth){
    case  4:
254
        return msrle_decode_pal4(avctx, pic, gb);
255 256 257 258
    case  8:
    case 16:
    case 24:
    case 32:
259
        return msrle_decode_8_16_24_32(avctx, pic, depth, gb);
260 261 262 263 264
    default:
        av_log(avctx, AV_LOG_ERROR, "Unknown depth %d\n", depth);
        return -1;
    }
}