sunrast.c 6.64 KB
Newer Older
Ivo van Poorten's avatar
Ivo van Poorten committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image decoder
 * Copyright (c) 2007, 2008 Ivo van Poorten
 *
 * 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
 */

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;
36
    uint8_t *ptr, *ptr2 = NULL;
Michael Niedermayer's avatar
Michael Niedermayer committed
37
    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_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) {
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 76
    if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
        av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
        return -1;
    }
Ivo van Poorten's avatar
Ivo van Poorten committed
77 78 79

    switch (depth) {
        case 1:
80
            avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_MONOWHITE;
81 82
            break;
        case 4:
83
            avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_NONE;
Ivo van Poorten's avatar
Ivo van Poorten committed
84 85
            break;
        case 8:
86
            avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
Ivo van Poorten's avatar
Ivo van Poorten committed
87 88
            break;
        case 24:
89
            avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
Ivo van Poorten's avatar
Ivo van Poorten committed
90
            break;
91
        case 32:
92
            avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_0RGB : AV_PIX_FMT_0BGR;
93
            break;
Ivo van Poorten's avatar
Ivo van Poorten committed
94 95
        default:
            av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
96
            return AVERROR_INVALIDDATA;
Ivo van Poorten's avatar
Ivo van Poorten committed
97 98
    }

99 100 101 102
    ret = ff_set_dimensions(avctx, w, h);
    if (ret < 0)
        return ret;

103
    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
104
        return ret;
Ivo van Poorten's avatar
Ivo van Poorten committed
105

106
    p->pict_type = AV_PICTURE_TYPE_I;
Ivo van Poorten's avatar
Ivo van Poorten committed
107

108 109 110
    if (buf_end - buf < maplength)
        return AVERROR_INVALIDDATA;

111
    if (depth > 8 && maplength) {
Ivo van Poorten's avatar
Ivo van Poorten committed
112 113
        av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");

114
    } else if (maplength) {
Ivo van Poorten's avatar
Ivo van Poorten committed
115 116 117 118
        unsigned int len = maplength / 3;

        if (maplength % 3 || maplength > 768) {
            av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
119
            return AVERROR_INVALIDDATA;
Ivo van Poorten's avatar
Ivo van Poorten committed
120 121 122
        }

        ptr = p->data[1];
Aneesh Dogra's avatar
Aneesh Dogra committed
123
        for (x = 0; x < len; x++, ptr += 4)
124
            *(uint32_t *)ptr = (0xFFU<<24) + (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
Ivo van Poorten's avatar
Ivo van Poorten committed
125 126 127 128
    }

    buf += maplength;

129 130 131 132 133 134
    if (maplength && depth < 8) {
        ptr = ptr2 = av_malloc((w + 15) * h);
        if (!ptr)
            return AVERROR(ENOMEM);
        stride = (w + 15 >> 3) * depth;
    } else {
Ivo van Poorten's avatar
Ivo van Poorten committed
135 136
    ptr    = p->data[0];
    stride = p->linesize[0];
137
    }
Ivo van Poorten's avatar
Ivo van Poorten committed
138 139 140

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

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

        x = 0;
148
        while (ptr != end && buf < buf_end) {
Ivo van Poorten's avatar
Ivo van Poorten committed
149
            run = 1;
150 151 152
            if (buf_end - buf < 1)
                return AVERROR_INVALIDDATA;

153
            if ((value = *buf++) == RLE_TRIGGER) {
Ivo van Poorten's avatar
Ivo van Poorten committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
                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
170
        for (y = 0; y < h; y++) {
171 172
            if (buf_end - buf < len)
                break;
Ivo van Poorten's avatar
Ivo van Poorten committed
173 174 175 176 177
            memcpy(ptr, buf, len);
            ptr += stride;
            buf += alen;
        }
    }
178
    if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && depth < 8) {
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        uint8_t *ptr_free = ptr2;
        ptr = p->data[0];
        for (y=0; y<h; y++) {
            for (x = 0; x < (w + 7 >> 3) * depth; x++) {
                if (depth == 1) {
                    ptr[8*x]   = ptr2[x] >> 7;
                    ptr[8*x+1] = ptr2[x] >> 6 & 1;
                    ptr[8*x+2] = ptr2[x] >> 5 & 1;
                    ptr[8*x+3] = ptr2[x] >> 4 & 1;
                    ptr[8*x+4] = ptr2[x] >> 3 & 1;
                    ptr[8*x+5] = ptr2[x] >> 2 & 1;
                    ptr[8*x+6] = ptr2[x] >> 1 & 1;
                    ptr[8*x+7] = ptr2[x]      & 1;
                } else {
                    ptr[2*x]   = ptr2[x] >> 4;
                    ptr[2*x+1] = ptr2[x] & 0xF;
                }
            }
            ptr  += p->linesize[0];
            ptr2 += (w + 15 >> 3) * depth;
        }
        av_freep(&ptr_free);
    }
Ivo van Poorten's avatar
Ivo van Poorten committed
202

203
    *got_frame = 1;
Ivo van Poorten's avatar
Ivo van Poorten committed
204 205 206 207

    return buf - bufstart;
}

208
AVCodec ff_sunrast_decoder = {
209
    .name           = "sunrast",
210
    .long_name      = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
211
    .type           = AVMEDIA_TYPE_VIDEO,
212
    .id             = AV_CODEC_ID_SUNRAST,
213 214
    .decode         = sunrast_decode_frame,
    .capabilities   = CODEC_CAP_DR1,
Ivo van Poorten's avatar
Ivo van Poorten committed
215
};