sunrast.c 7.54 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/intreadwrite.h"
23
#include "libavutil/imgutils.h"
Ivo van Poorten's avatar
Ivo van Poorten committed
24
#include "avcodec.h"
25
#include "sunrast.h"
Ivo van Poorten's avatar
Ivo van Poorten committed
26 27 28 29 30

typedef struct SUNRASTContext {
    AVFrame picture;
} SUNRASTContext;

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

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

    return 0;
}

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

52 53 54
    if (avpkt->size < 32)
        return AVERROR_INVALIDDATA;

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

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

68
    if (type == RT_EXPERIMENTAL) {
69 70
        av_log_ask_for_sample(avctx, "unsupported (compression) type\n");
        return AVERROR_PATCHWELCOME;
Ivo van Poorten's avatar
Ivo van Poorten committed
71
    }
72
    if (type > RT_FORMAT_IFF) {
73
        av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
74
        return AVERROR_INVALIDDATA;
Ivo van Poorten's avatar
Ivo van Poorten committed
75
    }
76 77
    if (av_image_check_size(w, h, 0, avctx)) {
        av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
78
        return AVERROR_INVALIDDATA;
Ivo van Poorten's avatar
Ivo van Poorten committed
79
    }
80 81 82 83 84
    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
85
        av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
86
        return AVERROR_INVALIDDATA;
Ivo van Poorten's avatar
Ivo van Poorten committed
87 88
    }

89 90 91 92
    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
93 94 95

    switch (depth) {
        case 1:
96 97 98 99
            avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_MONOWHITE;
            break;
        case 4:
            avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_NONE;
Ivo van Poorten's avatar
Ivo van Poorten committed
100 101
            break;
        case 8:
102
            avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_GRAY8;
Ivo van Poorten's avatar
Ivo van Poorten committed
103 104
            break;
        case 24:
Peter Ross's avatar
Peter Ross committed
105
            avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
Ivo van Poorten's avatar
Ivo van Poorten committed
106
            break;
107
        case 32:
108
            avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_0RGB : PIX_FMT_0BGR;
109
            break;
Ivo van Poorten's avatar
Ivo van Poorten committed
110 111
        default:
            av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
112
            return AVERROR_INVALIDDATA;
Ivo van Poorten's avatar
Ivo van Poorten committed
113 114 115 116 117 118 119
    }

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

    if (w != avctx->width || h != avctx->height)
        avcodec_set_dimensions(avctx, w, h);
120
    if ((ret = avctx->get_buffer(avctx, p)) < 0) {
Ivo van Poorten's avatar
Ivo van Poorten committed
121
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
122
        return ret;
Ivo van Poorten's avatar
Ivo van Poorten committed
123 124
    }

125
    p->pict_type = AV_PICTURE_TYPE_I;
Ivo van Poorten's avatar
Ivo van Poorten committed
126

127 128 129
    if (buf_end - buf < maplength)
        return AVERROR_INVALIDDATA;

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

133
    } else if (maplength) {
Ivo van Poorten's avatar
Ivo van Poorten committed
134 135 136 137
        unsigned int len = maplength / 3;

        if (maplength % 3 || maplength > 768) {
            av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
138
            return AVERROR_INVALIDDATA;
Ivo van Poorten's avatar
Ivo van Poorten committed
139 140 141
        }

        ptr = p->data[1];
Aneesh Dogra's avatar
Aneesh Dogra committed
142
        for (x = 0; x < len; x++, ptr += 4)
143
            *(uint32_t *)ptr = (0xFF<<24) + (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
Ivo van Poorten's avatar
Ivo van Poorten committed
144 145 146 147
    }

    buf += maplength;

148 149 150 151 152 153
    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
154 155
    ptr    = p->data[0];
    stride = p->linesize[0];
156
    }
Ivo van Poorten's avatar
Ivo van Poorten committed
157 158 159

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

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

        x = 0;
167
        while (ptr != end && buf < buf_end) {
Ivo van Poorten's avatar
Ivo van Poorten committed
168
            run = 1;
169 170 171
            if (buf_end - buf < 1)
                return AVERROR_INVALIDDATA;

172
            if ((value = *buf++) == RLE_TRIGGER) {
Ivo van Poorten's avatar
Ivo van Poorten committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
                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
189
        for (y = 0; y < h; y++) {
190 191
            if (buf_end - buf < len)
                break;
Ivo van Poorten's avatar
Ivo van Poorten committed
192 193 194 195 196
            memcpy(ptr, buf, len);
            ptr += stride;
            buf += alen;
        }
    }
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    if (avctx->pix_fmt == PIX_FMT_PAL8 && depth < 8) {
        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
221

Aneesh Dogra's avatar
Aneesh Dogra committed
222
    *picture   = s->picture;
Ivo van Poorten's avatar
Ivo van Poorten committed
223 224 225 226 227
    *data_size = sizeof(AVFrame);

    return buf - bufstart;
}

228
static av_cold int sunrast_end(AVCodecContext *avctx) {
Ivo van Poorten's avatar
Ivo van Poorten committed
229 230 231 232 233 234 235 236
    SUNRASTContext *s = avctx->priv_data;

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

    return 0;
}

237
AVCodec ff_sunrast_decoder = {
238 239 240 241 242 243 244 245
    .name           = "sunrast",
    .type           = AVMEDIA_TYPE_VIDEO,
    .id             = CODEC_ID_SUNRAST,
    .priv_data_size = sizeof(SUNRASTContext),
    .init           = sunrast_init,
    .close          = sunrast_end,
    .decode         = sunrast_decode_frame,
    .capabilities   = CODEC_CAP_DR1,
246
    .long_name      = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
Ivo van Poorten's avatar
Ivo van Poorten committed
247
};