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"
27 typedef struct SUNRASTContext {
31 static av_cold int sunrast_init(AVCodecContext *avctx) {
32 SUNRASTContext *s = avctx->priv_data;
34 avcodec_get_frame_defaults(&s->picture);
35 avctx->coded_frame = &s->picture;
40 static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
41 int *data_size, AVPacket *avpkt) {
42 const uint8_t *buf = avpkt->data;
43 const uint8_t *buf_end = avpkt->data + avpkt->size;
44 SUNRASTContext * const s = avctx->priv_data;
45 AVFrame *picture = data;
46 AVFrame * const p = &s->picture;
47 unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
48 uint8_t *ptr, *ptr2 = NULL;
49 const uint8_t *bufstart = buf;
53 return AVERROR_INVALIDDATA;
55 if (AV_RB32(buf) != RAS_MAGIC) {
56 av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
57 return AVERROR_INVALIDDATA;
62 depth = AV_RB32(buf + 12);
63 type = AV_RB32(buf + 20);
64 maptype = AV_RB32(buf + 24);
65 maplength = AV_RB32(buf + 28);
68 if (type == RT_EXPERIMENTAL) {
69 av_log_ask_for_sample(avctx, "unsupported (compression) type\n");
70 return AVERROR_PATCHWELCOME;
72 if (type > RT_FORMAT_IFF) {
73 av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
74 return AVERROR_INVALIDDATA;
76 if (av_image_check_size(w, h, 0, avctx)) {
77 av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
78 return AVERROR_INVALIDDATA;
80 if (maptype == RMT_RAW) {
81 av_log_ask_for_sample(avctx, "unsupported colormap type\n");
82 return AVERROR_PATCHWELCOME;
84 if (maptype > RMT_RAW) {
85 av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
86 return AVERROR_INVALIDDATA;
89 if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
90 av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
96 avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_MONOWHITE;
99 avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_NONE;
102 avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_GRAY8;
105 avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
108 avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_0RGB : PIX_FMT_0BGR;
111 av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
112 return AVERROR_INVALIDDATA;
116 avctx->release_buffer(avctx, p);
118 if (w != avctx->width || h != avctx->height)
119 avcodec_set_dimensions(avctx, w, h);
120 if ((ret = avctx->get_buffer(avctx, p)) < 0) {
121 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
125 p->pict_type = AV_PICTURE_TYPE_I;
127 if (buf_end - buf < maplength)
128 return AVERROR_INVALIDDATA;
130 if (depth > 8 && maplength) {
131 av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
133 } else if (maplength) {
134 unsigned int len = maplength / 3;
136 if (maplength % 3 || maplength > 768) {
137 av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
138 return AVERROR_INVALIDDATA;
142 for (x = 0; x < len; x++, ptr += 4)
143 *(uint32_t *)ptr = (0xFF<<24) + (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
148 if (maplength && depth < 8) {
149 ptr = ptr2 = av_malloc((w + 15) * h);
151 return AVERROR(ENOMEM);
152 stride = (w + 15 >> 3) * depth;
155 stride = p->linesize[0];
158 /* scanlines are aligned on 16 bit boundaries */
159 len = (depth * w + 7) >> 3;
160 alen = len + (len & 1);
162 if (type == RT_BYTE_ENCODED) {
164 uint8_t *end = ptr + h * stride;
167 while (ptr != end && buf < buf_end) {
169 if (buf_end - buf < 1)
170 return AVERROR_INVALIDDATA;
172 if ((value = *buf++) == RLE_TRIGGER) {
189 for (y = 0; y < h; y++) {
190 if (buf_end - buf < len)
192 memcpy(ptr, buf, len);
197 if (avctx->pix_fmt == PIX_FMT_PAL8 && depth < 8) {
198 uint8_t *ptr_free = ptr2;
200 for (y=0; y<h; y++) {
201 for (x = 0; x < (w + 7 >> 3) * depth; x++) {
203 ptr[8*x] = ptr2[x] >> 7;
204 ptr[8*x+1] = ptr2[x] >> 6 & 1;
205 ptr[8*x+2] = ptr2[x] >> 5 & 1;
206 ptr[8*x+3] = ptr2[x] >> 4 & 1;
207 ptr[8*x+4] = ptr2[x] >> 3 & 1;
208 ptr[8*x+5] = ptr2[x] >> 2 & 1;
209 ptr[8*x+6] = ptr2[x] >> 1 & 1;
210 ptr[8*x+7] = ptr2[x] & 1;
212 ptr[2*x] = ptr2[x] >> 4;
213 ptr[2*x+1] = ptr2[x] & 0xF;
216 ptr += p->linesize[0];
217 ptr2 += (w + 15 >> 3) * depth;
222 *picture = s->picture;
223 *data_size = sizeof(AVFrame);
225 return buf - bufstart;
228 static av_cold int sunrast_end(AVCodecContext *avctx) {
229 SUNRASTContext *s = avctx->priv_data;
231 if(s->picture.data[0])
232 avctx->release_buffer(avctx, &s->picture);
237 AVCodec ff_sunrast_decoder = {
239 .type = AVMEDIA_TYPE_VIDEO,
240 .id = CODEC_ID_SUNRAST,
241 .priv_data_size = sizeof(SUNRASTContext),
242 .init = sunrast_init,
243 .close = sunrast_end,
244 .decode = sunrast_decode_frame,
245 .capabilities = CODEC_CAP_DR1,
246 .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),