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/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;
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_FORMAT_TIFF || type == RT_FORMAT_IFF || 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;
92 avctx->pix_fmt = PIX_FMT_MONOWHITE;
95 avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_GRAY8;
98 avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
101 av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
102 return AVERROR_INVALIDDATA;
106 avctx->release_buffer(avctx, p);
108 if (w != avctx->width || h != avctx->height)
109 avcodec_set_dimensions(avctx, w, h);
110 if ((ret = avctx->get_buffer(avctx, p)) < 0) {
111 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
115 p->pict_type = AV_PICTURE_TYPE_I;
117 if (buf_end - buf < maplength)
118 return AVERROR_INVALIDDATA;
120 if (depth != 8 && maplength) {
121 av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
123 } else if (maplength) {
124 unsigned int len = maplength / 3;
126 if (maplength % 3 || maplength > 768) {
127 av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
128 return AVERROR_INVALIDDATA;
132 for (x = 0; x < len; x++, ptr += 4)
133 *(uint32_t *)ptr = (buf[x] << 16) + (buf[len + x] << 8) + buf[len + len + x];
139 stride = p->linesize[0];
141 /* scanlines are aligned on 16 bit boundaries */
142 len = (depth * w + 7) >> 3;
143 alen = len + (len & 1);
145 if (type == RT_BYTE_ENCODED) {
147 uint8_t *end = ptr + h * stride;
150 while (ptr != end && buf < buf_end) {
152 if (buf_end - buf < 1)
153 return AVERROR_INVALIDDATA;
155 if ((value = *buf++) == RLE_TRIGGER) {
172 for (y = 0; y < h; y++) {
173 if (buf_end - buf < len)
175 memcpy(ptr, buf, len);
181 *picture = s->picture;
182 *data_size = sizeof(AVFrame);
184 return buf - bufstart;
187 static av_cold int sunrast_end(AVCodecContext *avctx) {
188 SUNRASTContext *s = avctx->priv_data;
190 if(s->picture.data[0])
191 avctx->release_buffer(avctx, &s->picture);
196 AVCodec ff_sunrast_decoder = {
198 .type = AVMEDIA_TYPE_VIDEO,
199 .id = CODEC_ID_SUNRAST,
200 .priv_data_size = sizeof(SUNRASTContext),
201 .init = sunrast_init,
202 .close = sunrast_end,
203 .decode = sunrast_decode_frame,
204 .capabilities = CODEC_CAP_DR1,
205 .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),