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"
29 typedef struct SUNRASTContext {
33 static av_cold int sunrast_init(AVCodecContext *avctx) {
34 SUNRASTContext *s = avctx->priv_data;
36 avcodec_get_frame_defaults(&s->picture);
37 avctx->coded_frame = &s->picture;
42 static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
43 int *got_frame, AVPacket *avpkt)
45 const uint8_t *buf = avpkt->data;
46 const uint8_t *buf_end = avpkt->data + avpkt->size;
47 SUNRASTContext * const s = avctx->priv_data;
48 AVFrame *picture = data;
49 AVFrame * const p = &s->picture;
50 unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
52 const uint8_t *bufstart = buf;
56 return AVERROR_INVALIDDATA;
58 if (AV_RB32(buf) != RAS_MAGIC) {
59 av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
60 return AVERROR_INVALIDDATA;
65 depth = AV_RB32(buf + 12);
66 type = AV_RB32(buf + 20);
67 maptype = AV_RB32(buf + 24);
68 maplength = AV_RB32(buf + 28);
71 if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF || type == RT_EXPERIMENTAL) {
72 av_log_ask_for_sample(avctx, "unsupported (compression) type\n");
73 return AVERROR_PATCHWELCOME;
75 if (type > RT_FORMAT_IFF) {
76 av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
77 return AVERROR_INVALIDDATA;
79 if (av_image_check_size(w, h, 0, avctx)) {
80 av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
81 return AVERROR_INVALIDDATA;
83 if (maptype == RMT_RAW) {
84 av_log_ask_for_sample(avctx, "unsupported colormap type\n");
85 return AVERROR_PATCHWELCOME;
87 if (maptype > RMT_RAW) {
88 av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
89 return AVERROR_INVALIDDATA;
95 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
98 avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
101 avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
104 av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
105 return AVERROR_INVALIDDATA;
109 avctx->release_buffer(avctx, p);
111 if (w != avctx->width || h != avctx->height)
112 avcodec_set_dimensions(avctx, w, h);
113 if ((ret = ff_get_buffer(avctx, p)) < 0) {
114 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
118 p->pict_type = AV_PICTURE_TYPE_I;
120 if (buf_end - buf < maplength)
121 return AVERROR_INVALIDDATA;
123 if (depth != 8 && maplength) {
124 av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
126 } else if (maplength) {
127 unsigned int len = maplength / 3;
129 if (maplength % 3 || maplength > 768) {
130 av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
131 return AVERROR_INVALIDDATA;
135 for (x = 0; x < len; x++, ptr += 4)
136 *(uint32_t *)ptr = (buf[x] << 16) + (buf[len + x] << 8) + buf[len + len + x];
142 stride = p->linesize[0];
144 /* scanlines are aligned on 16 bit boundaries */
145 len = (depth * w + 7) >> 3;
146 alen = len + (len & 1);
148 if (type == RT_BYTE_ENCODED) {
150 uint8_t *end = ptr + h * stride;
153 while (ptr != end && buf < buf_end) {
155 if (buf_end - buf < 1)
156 return AVERROR_INVALIDDATA;
158 if ((value = *buf++) == RLE_TRIGGER) {
175 for (y = 0; y < h; y++) {
176 if (buf_end - buf < len)
178 memcpy(ptr, buf, len);
184 *picture = s->picture;
187 return buf - bufstart;
190 static av_cold int sunrast_end(AVCodecContext *avctx) {
191 SUNRASTContext *s = avctx->priv_data;
193 if(s->picture.data[0])
194 avctx->release_buffer(avctx, &s->picture);
199 AVCodec ff_sunrast_decoder = {
201 .type = AVMEDIA_TYPE_VIDEO,
202 .id = AV_CODEC_ID_SUNRAST,
203 .priv_data_size = sizeof(SUNRASTContext),
204 .init = sunrast_init,
205 .close = sunrast_end,
206 .decode = sunrast_decode_frame,
207 .capabilities = CODEC_CAP_DR1,
208 .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),