]> git.sesse.net Git - ffmpeg/blob - libavcodec/sunrast.c
Merge remote-tracking branch 'qatar/master'
[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 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 "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, *ptr2 = NULL;
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_EXPERIMENTAL) {
91         av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
92         return -1;
93     }
94     if (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     if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
108         av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
109         return -1;
110     }
111
112     switch (depth) {
113         case 1:
114             avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_MONOWHITE;
115             break;
116         case 4:
117             avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_NONE;
118             break;
119         case 8:
120             avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_GRAY8;
121             break;
122         case 24:
123             avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
124             break;
125         case 32:
126             avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB0 : PIX_FMT_BGR0;
127             break;
128         default:
129             av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
130             return -1;
131     }
132
133     if (p->data[0])
134         avctx->release_buffer(avctx, p);
135
136     if (w != avctx->width || h != avctx->height)
137         avcodec_set_dimensions(avctx, w, h);
138     if (avctx->get_buffer(avctx, p) < 0) {
139         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
140         return -1;
141     }
142
143     p->pict_type = AV_PICTURE_TYPE_I;
144
145     if (buf_end - buf < maplength)
146         return AVERROR_INVALIDDATA;
147
148     if (depth > 8 && maplength) {
149         av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
150
151     } else if (maplength) {
152         unsigned int len = maplength / 3;
153
154         if (!maplength) {
155             av_log(avctx, AV_LOG_ERROR, "colormap expected\n");
156             return -1;
157         }
158         if (maplength % 3 || maplength > 768) {
159             av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
160             return -1;
161         }
162
163         ptr = p->data[1];
164         for (x=0; x<len; x++, ptr+=4)
165             *(uint32_t *)ptr = (0xFF<<24) + (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
166     }
167
168     buf += maplength;
169
170     if (maplength && depth < 8) {
171         ptr = ptr2 = av_malloc((w + 15) * h);
172         if (!ptr)
173             return AVERROR(ENOMEM);
174         stride = (w + 15 >> 3) * depth;
175     } else {
176     ptr    = p->data[0];
177     stride = p->linesize[0];
178     }
179
180     /* scanlines are aligned on 16 bit boundaries */
181     len  = (depth * w + 7) >> 3;
182     alen = len + (len&1);
183
184     if (type == RT_BYTE_ENCODED) {
185         int value, run;
186         uint8_t *end = ptr + h*stride;
187
188         x = 0;
189         while (ptr != end && buf < buf_end) {
190             run = 1;
191             if (buf_end - buf < 1)
192                 return AVERROR_INVALIDDATA;
193
194             if ((value = *buf++) == 0x80) {
195                 run = *buf++ + 1;
196                 if (run != 1)
197                     value = *buf++;
198             }
199             while (run--) {
200                 if (x < len)
201                     ptr[x] = value;
202                 if (++x >= alen) {
203                     x = 0;
204                     ptr += stride;
205                     if (ptr == end)
206                         break;
207                 }
208             }
209         }
210     } else {
211         for (y=0; y<h; y++) {
212             if (buf_end - buf < len)
213                 break;
214             memcpy(ptr, buf, len);
215             ptr += stride;
216             buf += alen;
217         }
218     }
219     if (avctx->pix_fmt == PIX_FMT_PAL8 && depth < 8) {
220         uint8_t *ptr_free = ptr2;
221         ptr = p->data[0];
222         for (y=0; y<h; y++) {
223             for (x = 0; x < (w + 7 >> 3) * depth; x++) {
224                 if (depth == 1) {
225                     ptr[8*x]   = ptr2[x] >> 7;
226                     ptr[8*x+1] = ptr2[x] >> 6 & 1;
227                     ptr[8*x+2] = ptr2[x] >> 5 & 1;
228                     ptr[8*x+3] = ptr2[x] >> 4 & 1;
229                     ptr[8*x+4] = ptr2[x] >> 3 & 1;
230                     ptr[8*x+5] = ptr2[x] >> 2 & 1;
231                     ptr[8*x+6] = ptr2[x] >> 1 & 1;
232                     ptr[8*x+7] = ptr2[x]      & 1;
233                 } else {
234                     ptr[2*x]   = ptr2[x] >> 4;
235                     ptr[2*x+1] = ptr2[x] & 0xF;
236                 }
237             }
238             ptr  += p->linesize[0];
239             ptr2 += (w + 15 >> 3) * depth;
240         }
241         av_freep(&ptr_free);
242     }
243
244     *picture = s->picture;
245     *data_size = sizeof(AVFrame);
246
247     return buf - bufstart;
248 }
249
250 static av_cold int sunrast_end(AVCodecContext *avctx) {
251     SUNRASTContext *s = avctx->priv_data;
252
253     if(s->picture.data[0])
254         avctx->release_buffer(avctx, &s->picture);
255
256     return 0;
257 }
258
259 AVCodec ff_sunrast_decoder = {
260     .name           = "sunrast",
261     .type           = AVMEDIA_TYPE_VIDEO,
262     .id             = CODEC_ID_SUNRAST,
263     .priv_data_size = sizeof(SUNRASTContext),
264     .init           = sunrast_init,
265     .close          = sunrast_end,
266     .decode         = sunrast_decode_frame,
267     .capabilities   = CODEC_CAP_DR1,
268     .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
269 };