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/common.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/imgutils.h"
28 typedef struct SUNRASTContext {
32 static av_cold int sunrast_init(AVCodecContext *avctx) {
33 SUNRASTContext *s = avctx->priv_data;
35 avcodec_get_frame_defaults(&s->picture);
36 avctx->coded_frame = &s->picture;
41 static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
42 int *data_size, AVPacket *avpkt) {
43 const uint8_t *buf = avpkt->data;
44 const uint8_t *buf_end = avpkt->data + avpkt->size;
45 SUNRASTContext * const s = avctx->priv_data;
46 AVFrame *picture = data;
47 AVFrame * const p = &s->picture;
48 unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
49 uint8_t *ptr, *ptr2 = NULL;
50 const uint8_t *bufstart = buf;
54 return AVERROR_INVALIDDATA;
56 if (AV_RB32(buf) != RAS_MAGIC) {
57 av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
58 return AVERROR_INVALIDDATA;
63 depth = AV_RB32(buf + 12);
64 type = AV_RB32(buf + 20);
65 maptype = AV_RB32(buf + 24);
66 maplength = AV_RB32(buf + 28);
69 if (type == RT_EXPERIMENTAL) {
70 av_log_ask_for_sample(avctx, "unsupported (compression) type\n");
71 return AVERROR_PATCHWELCOME;
73 if (type > RT_FORMAT_IFF) {
74 av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
75 return AVERROR_INVALIDDATA;
77 if (av_image_check_size(w, h, 0, avctx)) {
78 av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
79 return AVERROR_INVALIDDATA;
81 if (maptype == RMT_RAW) {
82 av_log_ask_for_sample(avctx, "unsupported colormap type\n");
83 return AVERROR_PATCHWELCOME;
85 if (maptype > RMT_RAW) {
86 av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
87 return AVERROR_INVALIDDATA;
90 if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
91 av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
97 avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_MONOWHITE;
100 avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_NONE;
103 avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_GRAY8;
106 avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
109 avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_0RGB : PIX_FMT_0BGR;
112 av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
113 return AVERROR_INVALIDDATA;
117 avctx->release_buffer(avctx, p);
119 if (w != avctx->width || h != avctx->height)
120 avcodec_set_dimensions(avctx, w, h);
121 if ((ret = avctx->get_buffer(avctx, p)) < 0) {
122 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
126 p->pict_type = AV_PICTURE_TYPE_I;
128 if (buf_end - buf < maplength)
129 return AVERROR_INVALIDDATA;
131 if (depth > 8 && maplength) {
132 av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
134 } else if (maplength) {
135 unsigned int len = maplength / 3;
137 if (maplength % 3 || maplength > 768) {
138 av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
139 return AVERROR_INVALIDDATA;
143 for (x = 0; x < len; x++, ptr += 4)
144 *(uint32_t *)ptr = (0xFF<<24) + (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
149 if (maplength && depth < 8) {
150 ptr = ptr2 = av_malloc((w + 15) * h);
152 return AVERROR(ENOMEM);
153 stride = (w + 15 >> 3) * depth;
156 stride = p->linesize[0];
159 /* scanlines are aligned on 16 bit boundaries */
160 len = (depth * w + 7) >> 3;
161 alen = len + (len & 1);
163 if (type == RT_BYTE_ENCODED) {
165 uint8_t *end = ptr + h * stride;
168 while (ptr != end && buf < buf_end) {
170 if (buf_end - buf < 1)
171 return AVERROR_INVALIDDATA;
173 if ((value = *buf++) == RLE_TRIGGER) {
190 for (y = 0; y < h; y++) {
191 if (buf_end - buf < len)
193 memcpy(ptr, buf, len);
198 if (avctx->pix_fmt == PIX_FMT_PAL8 && depth < 8) {
199 uint8_t *ptr_free = ptr2;
201 for (y=0; y<h; y++) {
202 for (x = 0; x < (w + 7 >> 3) * depth; x++) {
204 ptr[8*x] = ptr2[x] >> 7;
205 ptr[8*x+1] = ptr2[x] >> 6 & 1;
206 ptr[8*x+2] = ptr2[x] >> 5 & 1;
207 ptr[8*x+3] = ptr2[x] >> 4 & 1;
208 ptr[8*x+4] = ptr2[x] >> 3 & 1;
209 ptr[8*x+5] = ptr2[x] >> 2 & 1;
210 ptr[8*x+6] = ptr2[x] >> 1 & 1;
211 ptr[8*x+7] = ptr2[x] & 1;
213 ptr[2*x] = ptr2[x] >> 4;
214 ptr[2*x+1] = ptr2[x] & 0xF;
217 ptr += p->linesize[0];
218 ptr2 += (w + 15 >> 3) * depth;
223 *picture = s->picture;
224 *data_size = sizeof(AVFrame);
226 return buf - bufstart;
229 static av_cold int sunrast_end(AVCodecContext *avctx) {
230 SUNRASTContext *s = avctx->priv_data;
232 if(s->picture.data[0])
233 avctx->release_buffer(avctx, &s->picture);
238 AVCodec ff_sunrast_decoder = {
240 .type = AVMEDIA_TYPE_VIDEO,
241 .id = AV_CODEC_ID_SUNRAST,
242 .priv_data_size = sizeof(SUNRASTContext),
243 .init = sunrast_init,
244 .close = sunrast_end,
245 .decode = sunrast_decode_frame,
246 .capabilities = CODEC_CAP_DR1,
247 .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),