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 FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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;
71 uint8_t *ptr, *ptr2 = NULL;
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_EXPERIMENTAL) {
91 av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
94 if (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");
107 if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
108 av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
114 avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_MONOWHITE;
117 avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_NONE;
120 avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_GRAY8;
123 avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
126 avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB0 : PIX_FMT_BGR0;
129 av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
134 avctx->release_buffer(avctx, p);
136 if (w != avctx->width || h != avctx->height)
137 avcodec_set_dimensions(avctx, w, h);
138 if (avctx->get_buffer(avctx, p) < 0) {
139 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
143 p->pict_type = AV_PICTURE_TYPE_I;
145 if (buf_end - buf < maplength)
146 return AVERROR_INVALIDDATA;
148 if (depth > 8 && maplength) {
149 av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
151 } else if (maplength) {
152 unsigned int len = maplength / 3;
155 av_log(avctx, AV_LOG_ERROR, "colormap expected\n");
158 if (maplength % 3 || maplength > 768) {
159 av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
164 for (x=0; x<len; x++, ptr+=4)
165 *(uint32_t *)ptr = (0xFF<<24) + (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
170 if (maplength && depth < 8) {
171 ptr = ptr2 = av_malloc((w + 15) * h);
173 return AVERROR(ENOMEM);
174 stride = (w + 15 >> 3) * depth;
177 stride = p->linesize[0];
180 /* scanlines are aligned on 16 bit boundaries */
181 len = (depth * w + 7) >> 3;
182 alen = len + (len&1);
184 if (type == RT_BYTE_ENCODED) {
186 uint8_t *end = ptr + h*stride;
189 while (ptr != end && buf < buf_end) {
191 if (buf_end - buf < 1)
192 return AVERROR_INVALIDDATA;
194 if ((value = *buf++) == 0x80) {
211 for (y=0; y<h; y++) {
212 if (buf_end - buf < len)
214 memcpy(ptr, buf, len);
219 if (avctx->pix_fmt == PIX_FMT_PAL8 && depth < 8) {
220 uint8_t *ptr_free = ptr2;
222 for (y=0; y<h; y++) {
223 for (x = 0; x < (w + 7 >> 3) * depth; x++) {
225 ptr[8*x] = ptr2[x] >> 7;
226 ptr[8*x+1] = ptr2[x] >> 6 & 1;
227 ptr[8*x+2] = ptr2[x] >> 5 & 1;
228 ptr[8*x+3] = ptr2[x] >> 4 & 1;
229 ptr[8*x+4] = ptr2[x] >> 3 & 1;
230 ptr[8*x+5] = ptr2[x] >> 2 & 1;
231 ptr[8*x+6] = ptr2[x] >> 1 & 1;
232 ptr[8*x+7] = ptr2[x] & 1;
234 ptr[2*x] = ptr2[x] >> 4;
235 ptr[2*x+1] = ptr2[x] & 0xF;
238 ptr += p->linesize[0];
239 ptr2 += (w + 15 >> 3) * depth;
244 *picture = s->picture;
245 *data_size = sizeof(AVFrame);
247 return buf - bufstart;
250 static av_cold int sunrast_end(AVCodecContext *avctx) {
251 SUNRASTContext *s = avctx->priv_data;
253 if(s->picture.data[0])
254 avctx->release_buffer(avctx, &s->picture);
259 AVCodec ff_sunrast_decoder = {
261 .type = AVMEDIA_TYPE_VIDEO,
262 .id = CODEC_ID_SUNRAST,
263 .priv_data_size = sizeof(SUNRASTContext),
264 .init = sunrast_init,
265 .close = sunrast_end,
266 .decode = sunrast_decode_frame,
267 .capabilities = CODEC_CAP_DR1,
268 .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),