]> git.sesse.net Git - ffmpeg/blob - libavcodec/sunrast.c
sunrast: Document the different Sun Raster file format types.
[ffmpeg] / libavcodec / sunrast.c
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 Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/imgutils.h"
24 #include "avcodec.h"
25
26 /* The Old and Standard format types indicate that the image data is
27  * uncompressed. There is no difference between the two formats. */
28 #define RT_OLD          0
29 #define RT_STANDARD     1
30
31 /* The Byte-Encoded format type indicates that the image data is compressed
32  * using a run-length encoding scheme. */
33 #define RT_BYTE_ENCODED 2
34
35 /* The RGB format type indicates that the image is uncompressed with reverse
36  * component order from Old and Standard (RGB vs BGR). */
37 #define RT_FORMAT_RGB   3
38
39 /* The TIFF and IFF format types indicate that the raster file was originally
40  * converted from either of these file formats. We do not have any samples or
41  * documentation of the format details. */
42 #define RT_FORMAT_TIFF  4
43 #define RT_FORMAT_IFF   5
44
45 /* The Experimental format type is implementation-specific and is generally an
46  * indication that the image file does not conform to the Sun Raster file
47  * format specification. */
48 #define RT_EXPERIMENTAL 0xffff
49
50 typedef struct SUNRASTContext {
51     AVFrame picture;
52 } SUNRASTContext;
53
54 static av_cold int sunrast_init(AVCodecContext *avctx) {
55     SUNRASTContext *s = avctx->priv_data;
56
57     avcodec_get_frame_defaults(&s->picture);
58     avctx->coded_frame= &s->picture;
59
60     return 0;
61 }
62
63 static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
64                                 int *data_size, AVPacket *avpkt) {
65     const uint8_t *buf = avpkt->data;
66     const uint8_t *buf_end = avpkt->data + avpkt->size;
67     SUNRASTContext * const s = avctx->priv_data;
68     AVFrame *picture = data;
69     AVFrame * const p = &s->picture;
70     unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
71     uint8_t *ptr;
72     const uint8_t *bufstart = buf;
73
74     if (avpkt->size < 32)
75         return AVERROR_INVALIDDATA;
76
77     if (AV_RB32(buf) != 0x59a66a95) {
78         av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
79         return -1;
80     }
81
82     w         = AV_RB32(buf+4);
83     h         = AV_RB32(buf+8);
84     depth     = AV_RB32(buf+12);
85     type      = AV_RB32(buf+20);
86     maptype   = AV_RB32(buf+24);
87     maplength = AV_RB32(buf+28);
88     buf      += 32;
89
90     if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF || type == RT_EXPERIMENTAL) {
91         av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
92         return -1;
93     }
94     if (type < RT_OLD || type > RT_FORMAT_IFF) {
95         av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
96         return -1;
97     }
98     if (av_image_check_size(w, h, 0, avctx)) {
99         av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
100         return -1;
101     }
102     if (maptype & ~1) {
103         av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
104         return -1;
105     }
106
107
108     switch (depth) {
109         case 1:
110             avctx->pix_fmt = PIX_FMT_MONOWHITE;
111             break;
112         case 8:
113             avctx->pix_fmt = PIX_FMT_PAL8;
114             break;
115         case 24:
116             avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
117             break;
118         default:
119             av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
120             return -1;
121     }
122
123     if (p->data[0])
124         avctx->release_buffer(avctx, p);
125
126     if (w != avctx->width || h != avctx->height)
127         avcodec_set_dimensions(avctx, w, h);
128     if (avctx->get_buffer(avctx, p) < 0) {
129         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
130         return -1;
131     }
132
133     p->pict_type = AV_PICTURE_TYPE_I;
134
135     if (buf_end - buf < maplength)
136         return AVERROR_INVALIDDATA;
137
138     if (depth != 8 && maplength) {
139         av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
140
141     } else if (depth == 8) {
142         unsigned int len = maplength / 3;
143
144         if (!maplength) {
145             av_log(avctx, AV_LOG_ERROR, "colormap expected\n");
146             return -1;
147         }
148         if (maplength % 3 || maplength > 768) {
149             av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
150             return -1;
151         }
152
153         ptr = p->data[1];
154         for (x=0; x<len; x++, ptr+=4)
155             *(uint32_t *)ptr = (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
156     }
157
158     buf += maplength;
159
160     ptr    = p->data[0];
161     stride = p->linesize[0];
162
163     /* scanlines are aligned on 16 bit boundaries */
164     len  = (depth * w + 7) >> 3;
165     alen = len + (len&1);
166
167     if (type == RT_BYTE_ENCODED) {
168         int value, run;
169         uint8_t *end = ptr + h*stride;
170
171         x = 0;
172         while (ptr != end && buf < buf_end) {
173             run = 1;
174             if (buf_end - buf < 1)
175                 return AVERROR_INVALIDDATA;
176
177             if ((value = *buf++) == 0x80) {
178                 run = *buf++ + 1;
179                 if (run != 1)
180                     value = *buf++;
181             }
182             while (run--) {
183                 if (x < len)
184                     ptr[x] = value;
185                 if (++x >= alen) {
186                     x = 0;
187                     ptr += stride;
188                     if (ptr == end)
189                         break;
190                 }
191             }
192         }
193     } else {
194         for (y=0; y<h; y++) {
195             if (buf_end - buf < len)
196                 break;
197             memcpy(ptr, buf, len);
198             ptr += stride;
199             buf += alen;
200         }
201     }
202
203     *picture = s->picture;
204     *data_size = sizeof(AVFrame);
205
206     return buf - bufstart;
207 }
208
209 static av_cold int sunrast_end(AVCodecContext *avctx) {
210     SUNRASTContext *s = avctx->priv_data;
211
212     if(s->picture.data[0])
213         avctx->release_buffer(avctx, &s->picture);
214
215     return 0;
216 }
217
218 AVCodec ff_sunrast_decoder = {
219     .name           = "sunrast",
220     .type           = AVMEDIA_TYPE_VIDEO,
221     .id             = CODEC_ID_SUNRAST,
222     .priv_data_size = sizeof(SUNRASTContext),
223     .init           = sunrast_init,
224     .close          = sunrast_end,
225     .decode         = sunrast_decode_frame,
226     .capabilities   = CODEC_CAP_DR1,
227     .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
228 };