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