2 * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image decoder
3 * Copyright (c) 2007, 2008 Ivo van Poorten
5 * This file is part of Libav.
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.
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.
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
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/imgutils.h"
26 /* The Old and Standard format types indicate that the image data is
27 * uncompressed. There is no difference between the two formats. */
31 /* The Byte-Encoded format type indicates that the image data is compressed
32 * using a run-length encoding scheme. */
33 #define RT_BYTE_ENCODED 2
35 /* The RGB format type indicates that the image is uncompressed with reverse
36 * component order from Old and Standard (RGB vs BGR). */
37 #define RT_FORMAT_RGB 3
39 /* The TIFF and IFF format types indicate that the raster file was originally
40 * converted from either of these file formats. We do not have any samples or
41 * documentation of the format details. */
42 #define RT_FORMAT_TIFF 4
43 #define RT_FORMAT_IFF 5
45 /* The Experimental format type is implementation-specific and is generally an
46 * indication that the image file does not conform to the Sun Raster file
47 * format specification. */
48 #define RT_EXPERIMENTAL 0xffff
50 typedef struct SUNRASTContext {
54 static av_cold int sunrast_init(AVCodecContext *avctx) {
55 SUNRASTContext *s = avctx->priv_data;
57 avcodec_get_frame_defaults(&s->picture);
58 avctx->coded_frame= &s->picture;
63 static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
64 int *data_size, AVPacket *avpkt) {
65 const uint8_t *buf = avpkt->data;
66 const uint8_t *buf_end = avpkt->data + avpkt->size;
67 SUNRASTContext * const s = avctx->priv_data;
68 AVFrame *picture = data;
69 AVFrame * const p = &s->picture;
70 unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
72 const uint8_t *bufstart = buf;
75 return AVERROR_INVALIDDATA;
77 if (AV_RB32(buf) != 0x59a66a95) {
78 av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
84 depth = AV_RB32(buf+12);
85 type = AV_RB32(buf+20);
86 maptype = AV_RB32(buf+24);
87 maplength = AV_RB32(buf+28);
90 if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF || type == RT_EXPERIMENTAL) {
91 av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
94 if (type < RT_OLD || type > RT_FORMAT_IFF) {
95 av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
98 if (av_image_check_size(w, h, 0, avctx)) {
99 av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
103 av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
110 avctx->pix_fmt = PIX_FMT_MONOWHITE;
113 avctx->pix_fmt = PIX_FMT_PAL8;
116 avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
119 av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
124 avctx->release_buffer(avctx, p);
126 if (w != avctx->width || h != avctx->height)
127 avcodec_set_dimensions(avctx, w, h);
128 if (avctx->get_buffer(avctx, p) < 0) {
129 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
133 p->pict_type = AV_PICTURE_TYPE_I;
135 if (buf_end - buf < maplength)
136 return AVERROR_INVALIDDATA;
138 if (depth != 8 && maplength) {
139 av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
141 } else if (depth == 8) {
142 unsigned int len = maplength / 3;
145 av_log(avctx, AV_LOG_ERROR, "colormap expected\n");
148 if (maplength % 3 || maplength > 768) {
149 av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
154 for (x=0; x<len; x++, ptr+=4)
155 *(uint32_t *)ptr = (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
161 stride = p->linesize[0];
163 /* scanlines are aligned on 16 bit boundaries */
164 len = (depth * w + 7) >> 3;
165 alen = len + (len&1);
167 if (type == RT_BYTE_ENCODED) {
169 uint8_t *end = ptr + h*stride;
172 while (ptr != end && buf < buf_end) {
174 if (buf_end - buf < 1)
175 return AVERROR_INVALIDDATA;
177 if ((value = *buf++) == 0x80) {
194 for (y=0; y<h; y++) {
195 if (buf_end - buf < len)
197 memcpy(ptr, buf, len);
203 *picture = s->picture;
204 *data_size = sizeof(AVFrame);
206 return buf - bufstart;
209 static av_cold int sunrast_end(AVCodecContext *avctx) {
210 SUNRASTContext *s = avctx->priv_data;
212 if(s->picture.data[0])
213 avctx->release_buffer(avctx, &s->picture);
218 AVCodec ff_sunrast_decoder = {
220 .type = AVMEDIA_TYPE_VIDEO,
221 .id = CODEC_ID_SUNRAST,
222 .priv_data_size = sizeof(SUNRASTContext),
223 .init = sunrast_init,
224 .close = sunrast_end,
225 .decode = sunrast_decode_frame,
226 .capabilities = CODEC_CAP_DR1,
227 .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),