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 #define RAS_MAGIC 0x59a66a95
28 /* The Old and Standard format types indicate that the image data is
29 * uncompressed. There is no difference between the two formats. */
33 /* The Byte-Encoded format type indicates that the image data is compressed
34 * using a run-length encoding scheme. */
35 #define RT_BYTE_ENCODED 2
37 /* The RGB format type indicates that the image is uncompressed with reverse
38 * component order from Old and Standard (RGB vs BGR). */
39 #define RT_FORMAT_RGB 3
41 /* The TIFF and IFF format types indicate that the raster file was originally
42 * converted from either of these file formats. We do not have any samples or
43 * documentation of the format details. */
44 #define RT_FORMAT_TIFF 4
45 #define RT_FORMAT_IFF 5
47 /* The Experimental format type is implementation-specific and is generally an
48 * indication that the image file does not conform to the Sun Raster file
49 * format specification. */
50 #define RT_EXPERIMENTAL 0xffff
52 typedef struct SUNRASTContext {
56 static av_cold int sunrast_init(AVCodecContext *avctx) {
57 SUNRASTContext *s = avctx->priv_data;
59 avcodec_get_frame_defaults(&s->picture);
60 avctx->coded_frame = &s->picture;
65 static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
66 int *data_size, AVPacket *avpkt) {
67 const uint8_t *buf = avpkt->data;
68 const uint8_t *buf_end = avpkt->data + avpkt->size;
69 SUNRASTContext * const s = avctx->priv_data;
70 AVFrame *picture = data;
71 AVFrame * const p = &s->picture;
72 unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
74 const uint8_t *bufstart = buf;
78 return AVERROR_INVALIDDATA;
80 if (AV_RB32(buf) != RAS_MAGIC) {
81 av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
82 return AVERROR_INVALIDDATA;
87 depth = AV_RB32(buf + 12);
88 type = AV_RB32(buf + 20);
89 maptype = AV_RB32(buf + 24);
90 maplength = AV_RB32(buf + 28);
93 if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF || type == RT_EXPERIMENTAL) {
94 av_log_ask_for_sample(avctx, "unsupported (compression) type\n");
95 return AVERROR_PATCHWELCOME;
97 if (type > RT_FORMAT_IFF) {
98 av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
99 return AVERROR_INVALIDDATA;
101 if (av_image_check_size(w, h, 0, avctx)) {
102 av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
103 return AVERROR_INVALIDDATA;
106 av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
107 return AVERROR_INVALIDDATA;
113 avctx->pix_fmt = PIX_FMT_MONOWHITE;
116 avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_GRAY8;
119 avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
122 av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
123 return AVERROR_INVALIDDATA;
127 avctx->release_buffer(avctx, p);
129 if (w != avctx->width || h != avctx->height)
130 avcodec_set_dimensions(avctx, w, h);
131 if ((ret = avctx->get_buffer(avctx, p)) < 0) {
132 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
136 p->pict_type = AV_PICTURE_TYPE_I;
138 if (buf_end - buf < maplength)
139 return AVERROR_INVALIDDATA;
141 if (depth != 8 && maplength) {
142 av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
144 } else if (maplength) {
145 unsigned int len = maplength / 3;
147 if (maplength % 3 || maplength > 768) {
148 av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
149 return AVERROR_INVALIDDATA;
153 for (x = 0; x < len; x++, ptr += 4)
154 *(uint32_t *)ptr = (buf[x] << 16) + (buf[len + x] << 8) + buf[len + len + x];
160 stride = p->linesize[0];
162 /* scanlines are aligned on 16 bit boundaries */
163 len = (depth * w + 7) >> 3;
164 alen = len + (len & 1);
166 if (type == RT_BYTE_ENCODED) {
168 uint8_t *end = ptr + h * stride;
171 while (ptr != end && buf < buf_end) {
173 if (buf_end - buf < 1)
174 return AVERROR_INVALIDDATA;
176 if ((value = *buf++) == 0x80) {
193 for (y = 0; y < h; y++) {
194 if (buf_end - buf < len)
196 memcpy(ptr, buf, len);
202 *picture = s->picture;
203 *data_size = sizeof(AVFrame);
205 return buf - bufstart;
208 static av_cold int sunrast_end(AVCodecContext *avctx) {
209 SUNRASTContext *s = avctx->priv_data;
211 if(s->picture.data[0])
212 avctx->release_buffer(avctx, &s->picture);
217 AVCodec ff_sunrast_decoder = {
219 .type = AVMEDIA_TYPE_VIDEO,
220 .id = CODEC_ID_SUNRAST,
221 .priv_data_size = sizeof(SUNRASTContext),
222 .init = sunrast_init,
223 .close = sunrast_end,
224 .decode = sunrast_decode_frame,
225 .capabilities = CODEC_CAP_DR1,
226 .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),