1/*
2 * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image decoder
3 * Copyright (c) 2007, 2008 Ivo van Poorten
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22#include "libavutil/intreadwrite.h"
23#include "avcodec.h"
24
25#define RT_OLD          0
26#define RT_STANDARD     1
27#define RT_BYTE_ENCODED 2
28#define RT_FORMAT_RGB   3
29#define RT_FORMAT_TIFF  4
30#define RT_FORMAT_IFF   5
31
32typedef struct SUNRASTContext {
33    AVFrame picture;
34} SUNRASTContext;
35
36static av_cold int sunrast_init(AVCodecContext *avctx) {
37    SUNRASTContext *s = avctx->priv_data;
38
39    avcodec_get_frame_defaults(&s->picture);
40    avctx->coded_frame= &s->picture;
41
42    return 0;
43}
44
45static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
46                                int *data_size, AVPacket *avpkt) {
47    const uint8_t *buf = avpkt->data;
48    SUNRASTContext * const s = avctx->priv_data;
49    AVFrame *picture = data;
50    AVFrame * const p = &s->picture;
51    unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
52    uint8_t *ptr;
53    const uint8_t *bufstart = buf;
54
55    if (AV_RB32(buf) != 0x59a66a95) {
56        av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
57        return -1;
58    }
59
60    w         = AV_RB32(buf+4);
61    h         = AV_RB32(buf+8);
62    depth     = AV_RB32(buf+12);
63    type      = AV_RB32(buf+20);
64    maptype   = AV_RB32(buf+24);
65    maplength = AV_RB32(buf+28);
66
67    if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
68        av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
69        return -1;
70    }
71    if (type > RT_FORMAT_IFF) {
72        av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
73        return -1;
74    }
75    if (maptype & ~1) {
76        av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
77        return -1;
78    }
79
80    buf += 32;
81
82    switch (depth) {
83        case 1:
84            avctx->pix_fmt = PIX_FMT_MONOWHITE;
85            break;
86        case 8:
87            avctx->pix_fmt = PIX_FMT_PAL8;
88            break;
89        case 24:
90            avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
91            break;
92        default:
93            av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
94            return -1;
95    }
96
97    if (p->data[0])
98        avctx->release_buffer(avctx, p);
99
100    if (avcodec_check_dimensions(avctx, w, h))
101        return -1;
102    if (w != avctx->width || h != avctx->height)
103        avcodec_set_dimensions(avctx, w, h);
104    if (avctx->get_buffer(avctx, p) < 0) {
105        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
106        return -1;
107    }
108
109    p->pict_type = FF_I_TYPE;
110
111    if (depth != 8 && maplength) {
112        av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
113
114    } else if (depth == 8) {
115        unsigned int len = maplength / 3;
116
117        if (!maplength) {
118            av_log(avctx, AV_LOG_ERROR, "colormap expected\n");
119            return -1;
120        }
121        if (maplength % 3 || maplength > 768) {
122            av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
123            return -1;
124        }
125
126        ptr = p->data[1];
127        for (x=0; x<len; x++, ptr+=4)
128            *(uint32_t *)ptr = (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
129    }
130
131    buf += maplength;
132
133    ptr    = p->data[0];
134    stride = p->linesize[0];
135
136    /* scanlines are aligned on 16 bit boundaries */
137    len  = (depth * w + 7) >> 3;
138    alen = len + (len&1);
139
140    if (type == RT_BYTE_ENCODED) {
141        int value, run;
142        uint8_t *end = ptr + h*stride;
143
144        x = 0;
145        while (ptr != end) {
146            run = 1;
147            if ((value = *buf++) == 0x80) {
148                run = *buf++ + 1;
149                if (run != 1)
150                    value = *buf++;
151            }
152            while (run--) {
153                if (x < len)
154                    ptr[x] = value;
155                if (++x >= alen) {
156                    x = 0;
157                    ptr += stride;
158                    if (ptr == end)
159                        break;
160                }
161            }
162        }
163    } else {
164        for (y=0; y<h; y++) {
165            memcpy(ptr, buf, len);
166            ptr += stride;
167            buf += alen;
168        }
169    }
170
171    *picture = s->picture;
172    *data_size = sizeof(AVFrame);
173
174    return buf - bufstart;
175}
176
177static av_cold int sunrast_end(AVCodecContext *avctx) {
178    SUNRASTContext *s = avctx->priv_data;
179
180    if(s->picture.data[0])
181        avctx->release_buffer(avctx, &s->picture);
182
183    return 0;
184}
185
186AVCodec sunrast_decoder = {
187    "sunrast",
188    AVMEDIA_TYPE_VIDEO,
189    CODEC_ID_SUNRAST,
190    sizeof(SUNRASTContext),
191    sunrast_init,
192    NULL,
193    sunrast_end,
194    sunrast_decode_frame,
195    CODEC_CAP_DR1,
196    NULL,
197    .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
198};
199