sunrast.c 6.14 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 30 31 32

typedef struct SUNRASTContext {
    AVFrame picture;
} SUNRASTContext;

33
static av_cold int sunrast_init(AVCodecContext *avctx) {
Ivo van Poorten's avatar
Ivo van Poorten committed
34 35 36
    SUNRASTContext *s = avctx->priv_data;

    avcodec_get_frame_defaults(&s->picture);
Aneesh Dogra's avatar
Aneesh Dogra committed
37
    avctx->coded_frame = &s->picture;
Ivo van Poorten's avatar
Ivo van Poorten committed
38 39 40 41 42

    return 0;
}

static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
43 44
                                int *got_frame, AVPacket *avpkt)
{
Aneesh Dogra's avatar
Aneesh Dogra committed
45 46
    const uint8_t *buf       = avpkt->data;
    const uint8_t *buf_end   = avpkt->data + avpkt->size;
Ivo van Poorten's avatar
Ivo van Poorten committed
47
    SUNRASTContext * const s = avctx->priv_data;
Aneesh Dogra's avatar
Aneesh Dogra committed
48 49
    AVFrame *picture         = data;
    AVFrame * const p        = &s->picture;
Ivo van Poorten's avatar
Ivo van Poorten committed
50
    unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
Michael Niedermayer's avatar
Michael Niedermayer committed
51 52
    uint8_t *ptr;
    const uint8_t *bufstart = buf;
53
    int ret;
Ivo van Poorten's avatar
Ivo van Poorten committed
54

55 56 57
    if (avpkt->size < 32)
        return AVERROR_INVALIDDATA;

58
    if (AV_RB32(buf) != RAS_MAGIC) {
Ivo van Poorten's avatar
Ivo van Poorten committed
59
        av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
60
        return AVERROR_INVALIDDATA;
Ivo van Poorten's avatar
Ivo van Poorten committed
61 62
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
63 64 65 66 67 68
    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);
69
    buf      += 32;
Ivo van Poorten's avatar
Ivo van Poorten committed
70

71
    if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF || type == RT_EXPERIMENTAL) {
72 73
        av_log_ask_for_sample(avctx, "unsupported (compression) type\n");
        return AVERROR_PATCHWELCOME;
Ivo van Poorten's avatar
Ivo van Poorten committed
74
    }
75
    if (type > RT_FORMAT_IFF) {
Ivo van Poorten's avatar
Ivo van Poorten committed
76
        av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
77
        return AVERROR_INVALIDDATA;
Ivo van Poorten's avatar
Ivo van Poorten committed
78
    }
79 80
    if (av_image_check_size(w, h, 0, avctx)) {
        av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
81
        return AVERROR_INVALIDDATA;
82
    }
83 84 85 86 87
    if (maptype == RMT_RAW) {
        av_log_ask_for_sample(avctx, "unsupported colormap type\n");
        return AVERROR_PATCHWELCOME;
    }
    if (maptype > RMT_RAW) {
Ivo van Poorten's avatar
Ivo van Poorten committed
88
        av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
89
        return AVERROR_INVALIDDATA;
Ivo van Poorten's avatar
Ivo van Poorten committed
90 91 92 93 94
    }


    switch (depth) {
        case 1:
95
            avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
Ivo van Poorten's avatar
Ivo van Poorten committed
96 97
            break;
        case 8:
98
            avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
Ivo van Poorten's avatar
Ivo van Poorten committed
99 100
            break;
        case 24:
101
            avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
Ivo van Poorten's avatar
Ivo van Poorten committed
102 103 104
            break;
        default:
            av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
105
            return AVERROR_INVALIDDATA;
Ivo van Poorten's avatar
Ivo van Poorten committed
106 107 108 109 110 111 112
    }

    if (p->data[0])
        avctx->release_buffer(avctx, p);

    if (w != avctx->width || h != avctx->height)
        avcodec_set_dimensions(avctx, w, h);
113
    if ((ret = ff_get_buffer(avctx, p)) < 0) {
Ivo van Poorten's avatar
Ivo van Poorten committed
114
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
115
        return ret;
Ivo van Poorten's avatar
Ivo van Poorten committed
116 117
    }

118
    p->pict_type = AV_PICTURE_TYPE_I;
Ivo van Poorten's avatar
Ivo van Poorten committed
119

120 121 122
    if (buf_end - buf < maplength)
        return AVERROR_INVALIDDATA;

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

126
    } else if (maplength) {
Ivo van Poorten's avatar
Ivo van Poorten committed
127 128 129 130
        unsigned int len = maplength / 3;

        if (maplength % 3 || maplength > 768) {
            av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
131
            return AVERROR_INVALIDDATA;
Ivo van Poorten's avatar
Ivo van Poorten committed
132 133 134
        }

        ptr = p->data[1];
Aneesh Dogra's avatar
Aneesh Dogra committed
135 136
        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
137 138 139 140 141 142 143 144 145
    }

    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
146
    alen = len + (len & 1);
Ivo van Poorten's avatar
Ivo van Poorten committed
147 148 149

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

        x = 0;
153
        while (ptr != end && buf < buf_end) {
Ivo van Poorten's avatar
Ivo van Poorten committed
154
            run = 1;
155 156 157
            if (buf_end - buf < 1)
                return AVERROR_INVALIDDATA;

158
            if ((value = *buf++) == RLE_TRIGGER) {
Ivo van Poorten's avatar
Ivo van Poorten committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
                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
175
        for (y = 0; y < h; y++) {
176 177
            if (buf_end - buf < len)
                break;
Ivo van Poorten's avatar
Ivo van Poorten committed
178 179 180 181 182 183
            memcpy(ptr, buf, len);
            ptr += stride;
            buf += alen;
        }
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
184
    *picture   = s->picture;
185
    *got_frame = 1;
Ivo van Poorten's avatar
Ivo van Poorten committed
186 187 188 189

    return buf - bufstart;
}

190
static av_cold int sunrast_end(AVCodecContext *avctx) {
Ivo van Poorten's avatar
Ivo van Poorten committed
191 192 193 194 195 196 197 198
    SUNRASTContext *s = avctx->priv_data;

    if(s->picture.data[0])
        avctx->release_buffer(avctx, &s->picture);

    return 0;
}

199
AVCodec ff_sunrast_decoder = {
200 201
    .name           = "sunrast",
    .type           = AVMEDIA_TYPE_VIDEO,
202
    .id             = AV_CODEC_ID_SUNRAST,
203 204 205 206 207
    .priv_data_size = sizeof(SUNRASTContext),
    .init           = sunrast_init,
    .close          = sunrast_end,
    .decode         = sunrast_decode_frame,
    .capabilities   = CODEC_CAP_DR1,
208
    .long_name      = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
Ivo van Poorten's avatar
Ivo van Poorten committed
209
};