sunrast.c 5.24 KB
Newer Older
Ivo van Poorten's avatar
Ivo van Poorten committed
1 2 3 4
/*
 * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image decoder
 * Copyright (c) 2007, 2008 Ivo van Poorten
 *
5
 * This file is part of Libav.
Ivo van Poorten's avatar
Ivo van Poorten committed
6
 *
7
 * Libav is free software; you can redistribute it and/or
Ivo van Poorten's avatar
Ivo van Poorten committed
8 9 10 11
 * 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.
 *
12
 * Libav is distributed in the hope that it will be useful,
Ivo van Poorten's avatar
Ivo van Poorten committed
13 14 15 16 17
 * 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
18
 * License along with Libav; if not, write to the Free Software
Ivo van Poorten's avatar
Ivo van Poorten committed
19 20 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22
#include "libavutil/common.h"
23
#include "libavutil/intreadwrite.h"
24
#include "libavutil/imgutils.h"
Ivo van Poorten's avatar
Ivo van Poorten committed
25
#include "avcodec.h"
26
#include "internal.h"
27
#include "sunrast.h"
Ivo van Poorten's avatar
Ivo van Poorten committed
28 29

static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
30 31
                                int *got_frame, AVPacket *avpkt)
{
Aneesh Dogra's avatar
Aneesh Dogra committed
32 33
    const uint8_t *buf       = avpkt->data;
    const uint8_t *buf_end   = avpkt->data + avpkt->size;
34
    AVFrame * const p        = data;
Ivo van Poorten's avatar
Ivo van Poorten committed
35
    unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
Michael Niedermayer's avatar
Michael Niedermayer committed
36 37
    uint8_t *ptr;
    const uint8_t *bufstart = buf;
38
    int ret;
Ivo van Poorten's avatar
Ivo van Poorten committed
39

40 41 42
    if (avpkt->size < 32)
        return AVERROR_INVALIDDATA;

43
    if (AV_RB32(buf) != RAS_MAGIC) {
Ivo van Poorten's avatar
Ivo van Poorten committed
44
        av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
45
        return AVERROR_INVALIDDATA;
Ivo van Poorten's avatar
Ivo van Poorten committed
46 47
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
48 49 50 51 52 53
    w         = AV_RB32(buf + 4);
    h         = AV_RB32(buf + 8);
    depth     = AV_RB32(buf + 12);
    type      = AV_RB32(buf + 20);
    maptype   = AV_RB32(buf + 24);
    maplength = AV_RB32(buf + 28);
54
    buf      += 32;
Ivo van Poorten's avatar
Ivo van Poorten committed
55

56
    if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF || type == RT_EXPERIMENTAL) {
57
        avpriv_request_sample(avctx, "TIFF/IFF/EXPERIMENTAL (compression) type");
58
        return AVERROR_PATCHWELCOME;
Ivo van Poorten's avatar
Ivo van Poorten committed
59
    }
60
    if (type > RT_FORMAT_IFF) {
Ivo van Poorten's avatar
Ivo van Poorten committed
61
        av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
62
        return AVERROR_INVALIDDATA;
Ivo van Poorten's avatar
Ivo van Poorten committed
63
    }
64
    if (maptype == RMT_RAW) {
65
        avpriv_request_sample(avctx, "Unknown colormap type");
66 67 68
        return AVERROR_PATCHWELCOME;
    }
    if (maptype > RMT_RAW) {
Ivo van Poorten's avatar
Ivo van Poorten committed
69
        av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
70
        return AVERROR_INVALIDDATA;
Ivo van Poorten's avatar
Ivo van Poorten committed
71 72 73 74 75
    }


    switch (depth) {
        case 1:
76
            avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
Ivo van Poorten's avatar
Ivo van Poorten committed
77 78
            break;
        case 8:
79
            avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
Ivo van Poorten's avatar
Ivo van Poorten committed
80 81
            break;
        case 24:
82
            avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
Ivo van Poorten's avatar
Ivo van Poorten committed
83 84 85
            break;
        default:
            av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
86
            return AVERROR_INVALIDDATA;
Ivo van Poorten's avatar
Ivo van Poorten committed
87 88
    }

89 90 91 92
    ret = ff_set_dimensions(avctx, w, h);
    if (ret < 0)
        return ret;

93
    if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
Ivo van Poorten's avatar
Ivo van Poorten committed
94
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
95
        return ret;
Ivo van Poorten's avatar
Ivo van Poorten committed
96 97
    }

98
    p->pict_type = AV_PICTURE_TYPE_I;
Ivo van Poorten's avatar
Ivo van Poorten committed
99

100 101 102
    if (buf_end - buf < maplength)
        return AVERROR_INVALIDDATA;

Ivo van Poorten's avatar
Ivo van Poorten committed
103 104 105
    if (depth != 8 && maplength) {
        av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");

106
    } else if (maplength) {
Ivo van Poorten's avatar
Ivo van Poorten committed
107 108 109 110
        unsigned int len = maplength / 3;

        if (maplength % 3 || maplength > 768) {
            av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
111
            return AVERROR_INVALIDDATA;
Ivo van Poorten's avatar
Ivo van Poorten committed
112 113 114
        }

        ptr = p->data[1];
Aneesh Dogra's avatar
Aneesh Dogra committed
115 116
        for (x = 0; x < len; x++, ptr += 4)
            *(uint32_t *)ptr = (buf[x] << 16) + (buf[len + x] << 8) + buf[len + len + x];
Ivo van Poorten's avatar
Ivo van Poorten committed
117 118 119 120 121 122 123 124 125
    }

    buf += maplength;

    ptr    = p->data[0];
    stride = p->linesize[0];

    /* scanlines are aligned on 16 bit boundaries */
    len  = (depth * w + 7) >> 3;
Aneesh Dogra's avatar
Aneesh Dogra committed
126
    alen = len + (len & 1);
Ivo van Poorten's avatar
Ivo van Poorten committed
127 128 129

    if (type == RT_BYTE_ENCODED) {
        int value, run;
Aneesh Dogra's avatar
Aneesh Dogra committed
130
        uint8_t *end = ptr + h * stride;
Ivo van Poorten's avatar
Ivo van Poorten committed
131 132

        x = 0;
133
        while (ptr != end && buf < buf_end) {
Ivo van Poorten's avatar
Ivo van Poorten committed
134
            run = 1;
135 136 137
            if (buf_end - buf < 1)
                return AVERROR_INVALIDDATA;

138
            if ((value = *buf++) == RLE_TRIGGER) {
Ivo van Poorten's avatar
Ivo van Poorten committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
                run = *buf++ + 1;
                if (run != 1)
                    value = *buf++;
            }
            while (run--) {
                if (x < len)
                    ptr[x] = value;
                if (++x >= alen) {
                    x = 0;
                    ptr += stride;
                    if (ptr == end)
                        break;
                }
            }
        }
    } else {
Aneesh Dogra's avatar
Aneesh Dogra committed
155
        for (y = 0; y < h; y++) {
156 157
            if (buf_end - buf < len)
                break;
Ivo van Poorten's avatar
Ivo van Poorten committed
158 159 160 161 162 163
            memcpy(ptr, buf, len);
            ptr += stride;
            buf += alen;
        }
    }

164
    *got_frame = 1;
Ivo van Poorten's avatar
Ivo van Poorten committed
165 166 167 168

    return buf - bufstart;
}

169
AVCodec ff_sunrast_decoder = {
170
    .name           = "sunrast",
171
    .long_name      = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
172
    .type           = AVMEDIA_TYPE_VIDEO,
173
    .id             = AV_CODEC_ID_SUNRAST,
174
    .decode         = sunrast_decode_frame,
175
    .capabilities   = AV_CODEC_CAP_DR1,
Ivo van Poorten's avatar
Ivo van Poorten committed
176
};