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"
28 #define RT_BYTE_ENCODED 2
29 #define RT_FORMAT_RGB 3
30 #define RT_FORMAT_TIFF 4
31 #define RT_FORMAT_IFF 5
33 typedef struct SUNRASTContext {
37 static av_cold int sunrast_init(AVCodecContext *avctx) {
38 SUNRASTContext *s = avctx->priv_data;
40 avcodec_get_frame_defaults(&s->picture);
41 avctx->coded_frame= &s->picture;
46 static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
47 int *data_size, AVPacket *avpkt) {
48 const uint8_t *buf = avpkt->data;
49 SUNRASTContext * const s = avctx->priv_data;
50 AVFrame *picture = data;
51 AVFrame * const p = &s->picture;
52 unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
54 const uint8_t *bufstart = buf;
56 if (AV_RB32(buf) != 0x59a66a95) {
57 av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
63 depth = AV_RB32(buf+12);
64 type = AV_RB32(buf+20);
65 maptype = AV_RB32(buf+24);
66 maplength = AV_RB32(buf+28);
68 if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
69 av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
72 if (type > RT_FORMAT_IFF) {
73 av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
77 av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
85 avctx->pix_fmt = PIX_FMT_MONOWHITE;
88 avctx->pix_fmt = PIX_FMT_PAL8;
91 avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
94 av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
99 avctx->release_buffer(avctx, p);
101 if (av_image_check_size(w, h, 0, avctx))
103 if (w != avctx->width || h != avctx->height)
104 avcodec_set_dimensions(avctx, w, h);
105 if (avctx->get_buffer(avctx, p) < 0) {
106 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
110 p->pict_type = AV_PICTURE_TYPE_I;
112 if (depth != 8 && maplength) {
113 av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
115 } else if (depth == 8) {
116 unsigned int len = maplength / 3;
119 av_log(avctx, AV_LOG_ERROR, "colormap expected\n");
122 if (maplength % 3 || maplength > 768) {
123 av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
128 for (x=0; x<len; x++, ptr+=4)
129 *(uint32_t *)ptr = (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
135 stride = p->linesize[0];
137 /* scanlines are aligned on 16 bit boundaries */
138 len = (depth * w + 7) >> 3;
139 alen = len + (len&1);
141 if (type == RT_BYTE_ENCODED) {
143 uint8_t *end = ptr + h*stride;
148 if ((value = *buf++) == 0x80) {
165 for (y=0; y<h; y++) {
166 memcpy(ptr, buf, len);
172 *picture = s->picture;
173 *data_size = sizeof(AVFrame);
175 return buf - bufstart;
178 static av_cold int sunrast_end(AVCodecContext *avctx) {
179 SUNRASTContext *s = avctx->priv_data;
181 if(s->picture.data[0])
182 avctx->release_buffer(avctx, &s->picture);
187 AVCodec ff_sunrast_decoder = {
189 .type = AVMEDIA_TYPE_VIDEO,
190 .id = CODEC_ID_SUNRAST,
191 .priv_data_size = sizeof(SUNRASTContext),
192 .init = sunrast_init,
193 .close = sunrast_end,
194 .decode = sunrast_decode_frame,
195 .capabilities = CODEC_CAP_DR1,
196 .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),