msrledec.c 9.6 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, AVPicture *pic,
34
                             GetByteContext *gb)
35 36 37 38
{
    unsigned char rle_code;
    unsigned char extra_byte, odd_pixel;
    unsigned char stream_byte;
39
    unsigned int pixel_ptr = 0;
40 41 42 43 44 45
    int row_dec = pic->linesize[0];
    int row_ptr = (avctx->height - 1) * row_dec;
    int frame_size = row_dec * avctx->height;
    int i;

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

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

Michael Niedermayer's avatar
Michael Niedermayer committed
95 96
                // if the RLE code is odd, skip a byte in the stream
                if (extra_byte)
97
                    bytestream2_skip(gb, 1);
98 99 100
            }
        } else {
            // decode a run of data
101
            if (row_ptr + pixel_ptr + stream_byte > frame_size) {
102 103 104
                av_log(avctx, AV_LOG_ERROR,
                       "MS RLE: frame ptr just went out of bounds (run)\n");
                return AVERROR_INVALIDDATA;
105
            }
106
            stream_byte = bytestream2_get_byte(gb);
107 108 109 110 111 112 113 114 115 116 117 118 119
            for (i = 0; i < rle_code; i++) {
                if (pixel_ptr >= avctx->width)
                    break;
                if ((i & 1) == 0)
                    pic->data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
                else
                    pic->data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
                pixel_ptr++;
            }
        }
    }

    /* one last sanity check on the way out */
120 121 122 123 124
    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;
125 126 127 128 129 130
    }

    return 0;
}


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

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

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

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

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

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


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