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"
28 #define RT_BYTE_ENCODED 2
29 #define RT_FORMAT_RGB 3
30 #define RT_FORMAT_TIFF 4
31 #define RT_FORMAT_IFF 5
33 typedef struct SUNRASTContext {
37 static av_cold int sunrast_init(AVCodecContext *avctx) {
38 SUNRASTContext *s = avctx->priv_data;
40 avcodec_get_frame_defaults(&s->picture);
41 avctx->coded_frame= &s->picture;
46 static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
47 int *data_size, AVPacket *avpkt) {
48 const uint8_t *buf = avpkt->data;
49 const uint8_t *buf_end = avpkt->data + avpkt->size;
50 SUNRASTContext * const s = avctx->priv_data;
51 AVFrame *picture = data;
52 AVFrame * const p = &s->picture;
53 unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
55 const uint8_t *bufstart = buf;
58 return AVERROR_INVALIDDATA;
60 if (AV_RB32(buf) != 0x59a66a95) {
61 av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
67 depth = AV_RB32(buf+12);
68 type = AV_RB32(buf+20);
69 maptype = AV_RB32(buf+24);
70 maplength = AV_RB32(buf+28);
73 if (type < RT_OLD || type > RT_FORMAT_IFF) {
74 av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
77 if (av_image_check_size(w, h, 0, avctx)) {
78 av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
82 av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
86 if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
87 av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
93 avctx->pix_fmt = PIX_FMT_MONOWHITE;
96 avctx->pix_fmt = PIX_FMT_PAL8;
99 avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
102 av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
107 avctx->release_buffer(avctx, p);
109 if (w != avctx->width || h != avctx->height)
110 avcodec_set_dimensions(avctx, w, h);
111 if (avctx->get_buffer(avctx, p) < 0) {
112 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
116 p->pict_type = AV_PICTURE_TYPE_I;
118 if (buf_end - buf < maplength)
119 return AVERROR_INVALIDDATA;
121 if (depth != 8 && maplength) {
122 av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
124 } else if (depth == 8) {
125 unsigned int len = maplength / 3;
128 av_log(avctx, AV_LOG_ERROR, "colormap expected\n");
131 if (maplength % 3 || maplength > 768) {
132 av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
137 for (x=0; x<len; x++, ptr+=4)
138 *(uint32_t *)ptr = (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
144 stride = p->linesize[0];
146 /* scanlines are aligned on 16 bit boundaries */
147 len = (depth * w + 7) >> 3;
148 alen = len + (len&1);
150 if (type == RT_BYTE_ENCODED) {
152 uint8_t *end = ptr + h*stride;
155 while (ptr != end && buf < buf_end) {
157 if (buf_end - buf < 1)
158 return AVERROR_INVALIDDATA;
160 if ((value = *buf++) == 0x80) {
177 for (y=0; y<h; y++) {
178 if (buf_end - buf < len)
180 memcpy(ptr, buf, len);
186 *picture = s->picture;
187 *data_size = sizeof(AVFrame);
189 return buf - bufstart;
192 static av_cold int sunrast_end(AVCodecContext *avctx) {
193 SUNRASTContext *s = avctx->priv_data;
195 if(s->picture.data[0])
196 avctx->release_buffer(avctx, &s->picture);
201 AVCodec ff_sunrast_decoder = {
203 .type = AVMEDIA_TYPE_VIDEO,
204 .id = CODEC_ID_SUNRAST,
205 .priv_data_size = sizeof(SUNRASTContext),
206 .init = sunrast_init,
207 .close = sunrast_end,
208 .decode = sunrast_decode_frame,
209 .capabilities = CODEC_CAP_DR1,
210 .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),