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;
77 return AVERROR_INVALIDDATA;
79 if (AV_RB32(buf) != RAS_MAGIC) {
80 av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
86 depth = AV_RB32(buf + 12);
87 type = AV_RB32(buf + 20);
88 maptype = AV_RB32(buf + 24);
89 maplength = AV_RB32(buf + 28);
92 if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF || type == RT_EXPERIMENTAL) {
93 av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
96 if (type > RT_FORMAT_IFF) {
97 av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
100 if (av_image_check_size(w, h, 0, avctx)) {
101 av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
105 av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
112 avctx->pix_fmt = PIX_FMT_MONOWHITE;
115 avctx->pix_fmt = PIX_FMT_PAL8;
118 avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
121 av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
126 avctx->release_buffer(avctx, p);
128 if (w != avctx->width || h != avctx->height)
129 avcodec_set_dimensions(avctx, w, h);
130 if (avctx->get_buffer(avctx, p) < 0) {
131 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
135 p->pict_type = AV_PICTURE_TYPE_I;
137 if (buf_end - buf < maplength)
138 return AVERROR_INVALIDDATA;
140 if (depth != 8 && maplength) {
141 av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
143 } else if (depth == 8) {
144 unsigned int len = maplength / 3;
147 av_log(avctx, AV_LOG_ERROR, "colormap expected\n");
150 if (maplength % 3 || maplength > 768) {
151 av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
156 for (x = 0; x < len; x++, ptr += 4)
157 *(uint32_t *)ptr = (buf[x] << 16) + (buf[len + x] << 8) + buf[len + len + x];
163 stride = p->linesize[0];
165 /* scanlines are aligned on 16 bit boundaries */
166 len = (depth * w + 7) >> 3;
167 alen = len + (len & 1);
169 if (type == RT_BYTE_ENCODED) {
171 uint8_t *end = ptr + h * stride;
174 while (ptr != end && buf < buf_end) {
176 if (buf_end - buf < 1)
177 return AVERROR_INVALIDDATA;
179 if ((value = *buf++) == 0x80) {
196 for (y = 0; y < h; y++) {
197 if (buf_end - buf < len)
199 memcpy(ptr, buf, len);
205 *picture = s->picture;
206 *data_size = sizeof(AVFrame);
208 return buf - bufstart;
211 static av_cold int sunrast_end(AVCodecContext *avctx) {
212 SUNRASTContext *s = avctx->priv_data;
214 if(s->picture.data[0])
215 avctx->release_buffer(avctx, &s->picture);
220 AVCodec ff_sunrast_decoder = {
222 .type = AVMEDIA_TYPE_VIDEO,
223 .id = CODEC_ID_SUNRAST,
224 .priv_data_size = sizeof(SUNRASTContext),
225 .init = sunrast_init,
226 .close = sunrast_end,
227 .decode = sunrast_decode_frame,
228 .capabilities = CODEC_CAP_DR1,
229 .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),