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/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;
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_FORMAT_TIFF || type == RT_FORMAT_IFF || 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;
93 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
96 avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
99 avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
102 av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
103 return AVERROR_INVALIDDATA;
107 avctx->release_buffer(avctx, p);
109 if (w != avctx->width || h != avctx->height)
110 avcodec_set_dimensions(avctx, w, h);
111 if ((ret = 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 (maplength) {
125 unsigned int len = maplength / 3;
127 if (maplength % 3 || maplength > 768) {
128 av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
129 return AVERROR_INVALIDDATA;
133 for (x = 0; x < len; x++, ptr += 4)
134 *(uint32_t *)ptr = (buf[x] << 16) + (buf[len + x] << 8) + buf[len + len + x];
140 stride = p->linesize[0];
142 /* scanlines are aligned on 16 bit boundaries */
143 len = (depth * w + 7) >> 3;
144 alen = len + (len & 1);
146 if (type == RT_BYTE_ENCODED) {
148 uint8_t *end = ptr + h * stride;
151 while (ptr != end && buf < buf_end) {
153 if (buf_end - buf < 1)
154 return AVERROR_INVALIDDATA;
156 if ((value = *buf++) == RLE_TRIGGER) {
173 for (y = 0; y < h; y++) {
174 if (buf_end - buf < len)
176 memcpy(ptr, buf, len);
182 *picture = s->picture;
183 *data_size = sizeof(AVFrame);
185 return buf - bufstart;
188 static av_cold int sunrast_end(AVCodecContext *avctx) {
189 SUNRASTContext *s = avctx->priv_data;
191 if(s->picture.data[0])
192 avctx->release_buffer(avctx, &s->picture);
197 AVCodec ff_sunrast_decoder = {
199 .type = AVMEDIA_TYPE_VIDEO,
200 .id = AV_CODEC_ID_SUNRAST,
201 .priv_data_size = sizeof(SUNRASTContext),
202 .init = sunrast_init,
203 .close = sunrast_end,
204 .decode = sunrast_decode_frame,
205 .capabilities = CODEC_CAP_DR1,
206 .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),