Statistics
| Branch: | Revision:

ffmpeg / libavcodec / sunrast.c @ 2912e87a

History | View | Annotate | Download (5.4 KB)

1
/*
2
 * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image decoder
3
 * Copyright (c) 2007, 2008 Ivo van Poorten
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * Libav is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#include "libavutil/intreadwrite.h"
23
#include "libavutil/imgutils.h"
24
#include "avcodec.h"
25

    
26
#define RT_OLD          0
27
#define RT_STANDARD     1
28
#define RT_BYTE_ENCODED 2
29
#define RT_FORMAT_RGB   3
30
#define RT_FORMAT_TIFF  4
31
#define RT_FORMAT_IFF   5
32

    
33
typedef struct SUNRASTContext {
34
    AVFrame picture;
35
} SUNRASTContext;
36

    
37
static av_cold int sunrast_init(AVCodecContext *avctx) {
38
    SUNRASTContext *s = avctx->priv_data;
39

    
40
    avcodec_get_frame_defaults(&s->picture);
41
    avctx->coded_frame= &s->picture;
42

    
43
    return 0;
44
}
45

    
46
static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
47
                                int *data_size, AVPacket *avpkt) {
48
    const uint8_t *buf = avpkt->data;
49
    SUNRASTContext * const s = avctx->priv_data;
50
    AVFrame *picture = data;
51
    AVFrame * const p = &s->picture;
52
    unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
53
    uint8_t *ptr;
54
    const uint8_t *bufstart = buf;
55

    
56
    if (AV_RB32(buf) != 0x59a66a95) {
57
        av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
58
        return -1;
59
    }
60

    
61
    w         = AV_RB32(buf+4);
62
    h         = AV_RB32(buf+8);
63
    depth     = AV_RB32(buf+12);
64
    type      = AV_RB32(buf+20);
65
    maptype   = AV_RB32(buf+24);
66
    maplength = AV_RB32(buf+28);
67

    
68
    if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
69
        av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
70
        return -1;
71
    }
72
    if (type > RT_FORMAT_IFF) {
73
        av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
74
        return -1;
75
    }
76
    if (maptype & ~1) {
77
        av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
78
        return -1;
79
    }
80

    
81
    buf += 32;
82

    
83
    switch (depth) {
84
        case 1:
85
            avctx->pix_fmt = PIX_FMT_MONOWHITE;
86
            break;
87
        case 8:
88
            avctx->pix_fmt = PIX_FMT_PAL8;
89
            break;
90
        case 24:
91
            avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
92
            break;
93
        default:
94
            av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
95
            return -1;
96
    }
97

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

    
101
    if (av_image_check_size(w, h, 0, avctx))
102
        return -1;
103
    if (w != avctx->width || h != avctx->height)
104
        avcodec_set_dimensions(avctx, w, h);
105
    if (avctx->get_buffer(avctx, p) < 0) {
106
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
107
        return -1;
108
    }
109

    
110
    p->pict_type = FF_I_TYPE;
111

    
112
    if (depth != 8 && maplength) {
113
        av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
114

    
115
    } else if (depth == 8) {
116
        unsigned int len = maplength / 3;
117

    
118
        if (!maplength) {
119
            av_log(avctx, AV_LOG_ERROR, "colormap expected\n");
120
            return -1;
121
        }
122
        if (maplength % 3 || maplength > 768) {
123
            av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
124
            return -1;
125
        }
126

    
127
        ptr = p->data[1];
128
        for (x=0; x<len; x++, ptr+=4)
129
            *(uint32_t *)ptr = (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
130
    }
131

    
132
    buf += maplength;
133

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

    
137
    /* scanlines are aligned on 16 bit boundaries */
138
    len  = (depth * w + 7) >> 3;
139
    alen = len + (len&1);
140

    
141
    if (type == RT_BYTE_ENCODED) {
142
        int value, run;
143
        uint8_t *end = ptr + h*stride;
144

    
145
        x = 0;
146
        while (ptr != end) {
147
            run = 1;
148
            if ((value = *buf++) == 0x80) {
149
                run = *buf++ + 1;
150
                if (run != 1)
151
                    value = *buf++;
152
            }
153
            while (run--) {
154
                if (x < len)
155
                    ptr[x] = value;
156
                if (++x >= alen) {
157
                    x = 0;
158
                    ptr += stride;
159
                    if (ptr == end)
160
                        break;
161
                }
162
            }
163
        }
164
    } else {
165
        for (y=0; y<h; y++) {
166
            memcpy(ptr, buf, len);
167
            ptr += stride;
168
            buf += alen;
169
        }
170
    }
171

    
172
    *picture = s->picture;
173
    *data_size = sizeof(AVFrame);
174

    
175
    return buf - bufstart;
176
}
177

    
178
static av_cold int sunrast_end(AVCodecContext *avctx) {
179
    SUNRASTContext *s = avctx->priv_data;
180

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

    
184
    return 0;
185
}
186

    
187
AVCodec ff_sunrast_decoder = {
188
    "sunrast",
189
    AVMEDIA_TYPE_VIDEO,
190
    CODEC_ID_SUNRAST,
191
    sizeof(SUNRASTContext),
192
    sunrast_init,
193
    NULL,
194
    sunrast_end,
195
    sunrast_decode_frame,
196
    CODEC_CAP_DR1,
197
    NULL,
198
    .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
199
};