msrledec.c 9.67 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 141
    output     = pic->data[0] + (avctx->height - 1) * pic->linesize[0];
    output_end = pic->data[0] +  avctx->height      * pic->linesize[0];
142 143
    while (bytestream2_get_bytes_left(gb) > 0) {
        p1 = bytestream2_get_byteu(gb);
144
        if(p1 == 0) { //Escape code
145
            p2 = bytestream2_get_byte(gb);
146 147
            if(p2 == 0) { //End-of-line
                output = pic->data[0] + (--line) * pic->linesize[0];
148 149 150 151 152 153 154 155 156
                if (line < 0) {
                    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 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 172 173 174
                output = pic->data[0] + line * pic->linesize[0] + pos * (depth >> 3);
                continue;
            }
            // Copy data
175 176 177
            if ((pic->linesize[0] > 0 && output + p2 * (depth >> 3) > output_end) ||
                (pic->linesize[0] < 0 && output + p2 * (depth >> 3) < output_end)) {
                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 185
            if ((depth == 8) || (depth == 24)) {
                for(i = 0; i < p2 * (depth >> 3); i++) {
186
                    *output++ = bytestream2_get_byteu(gb);
187 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
            switch(depth){
207
            case  8: pix[0] = bytestream2_get_byte(gb);
208
                     break;
209
            case 16: pix16  = bytestream2_get_le16(gb);
210
                     break;
211 212 213
            case 24: pix[0] = bytestream2_get_byte(gb);
                     pix[1] = bytestream2_get_byte(gb);
                     pix[2] = bytestream2_get_byte(gb);
214
                     break;
215
            case 32: pix32  = bytestream2_get_le32(gb);
216 217
                     break;
            }
218 219
            if ((pic->linesize[0] > 0 && output + p1 * (depth >> 3) > output_end) ||
                (pic->linesize[0] < 0 && output + p1 * (depth >> 3) < output_end))
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
                continue;
            for(i = 0; i < p1; i++) {
                switch(depth){
                case  8: *output++ = pix[0];
                         break;
                case 16: *(uint16_t*)output = pix16;
                         output += 2;
                         break;
                case 24: *output++ = pix[0];
                         *output++ = pix[1];
                         *output++ = pix[2];
                         break;
                case 32: *(uint32_t*)output = pix32;
                         output += 4;
                         break;
                }
            }
            pos += p1;
        }
    }

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


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