]> git.sesse.net Git - ffmpeg/blob - libavcodec/sunrast.c
Support reading 64bit sgi images.
[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 #define RT_OLD          0
27 #define RT_STANDARD     1
28 #define RT_BYTE_ENCODED 2
29 #define RT_FORMAT_RGB   3
30 #define RT_FORMAT_TIFF  4
31 #define RT_FORMAT_IFF   5
32
33 typedef struct SUNRASTContext {
34     AVFrame picture;
35 } SUNRASTContext;
36
37 static av_cold int sunrast_init(AVCodecContext *avctx) {
38     SUNRASTContext *s = avctx->priv_data;
39
40     avcodec_get_frame_defaults(&s->picture);
41     avctx->coded_frame= &s->picture;
42
43     return 0;
44 }
45
46 static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
47                                 int *data_size, AVPacket *avpkt) {
48     const uint8_t *buf = avpkt->data;
49     const uint8_t *buf_end = avpkt->data + avpkt->size;
50     SUNRASTContext * const s = avctx->priv_data;
51     AVFrame *picture = data;
52     AVFrame * const p = &s->picture;
53     unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
54     uint8_t *ptr, *ptr2 = NULL;
55     const uint8_t *bufstart = buf;
56
57     if (avpkt->size < 32)
58         return AVERROR_INVALIDDATA;
59
60     if (AV_RB32(buf) != 0x59a66a95) {
61         av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
62         return -1;
63     }
64
65     w         = AV_RB32(buf+4);
66     h         = AV_RB32(buf+8);
67     depth     = AV_RB32(buf+12);
68     type      = AV_RB32(buf+20);
69     maptype   = AV_RB32(buf+24);
70     maplength = AV_RB32(buf+28);
71     buf      += 32;
72
73     if (type > RT_FORMAT_IFF) {
74         av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
75         return -1;
76     }
77     if (av_image_check_size(w, h, 0, avctx)) {
78         av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
79         return -1;
80     }
81     if (maptype & ~1) {
82         av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
83         return -1;
84     }
85
86     if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
87         av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
88         return -1;
89     }
90
91     switch (depth) {
92         case 1:
93             avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_MONOWHITE;
94             break;
95         case 4:
96             avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_NONE;
97             break;
98         case 8:
99             avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_GRAY8;
100             break;
101         case 24:
102             avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
103             break;
104         case 32:
105             avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB0 : PIX_FMT_BGR0;
106             break;
107         default:
108             av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
109             return -1;
110     }
111
112     if (p->data[0])
113         avctx->release_buffer(avctx, p);
114
115     if (w != avctx->width || h != avctx->height)
116         avcodec_set_dimensions(avctx, w, h);
117     if (avctx->get_buffer(avctx, p) < 0) {
118         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
119         return -1;
120     }
121
122     p->pict_type = AV_PICTURE_TYPE_I;
123
124     if (buf_end - buf < maplength)
125         return AVERROR_INVALIDDATA;
126
127     if (depth > 8 && maplength) {
128         av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
129
130     } else if (maplength) {
131         unsigned int len = maplength / 3;
132
133         if (!maplength) {
134             av_log(avctx, AV_LOG_ERROR, "colormap expected\n");
135             return -1;
136         }
137         if (maplength % 3 || maplength > 768) {
138             av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
139             return -1;
140         }
141
142         ptr = p->data[1];
143         for (x=0; x<len; x++, ptr+=4)
144             *(uint32_t *)ptr = (0xFF<<24) + (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
145     }
146
147     buf += maplength;
148
149     if (maplength && depth < 8) {
150         ptr = ptr2 = av_malloc((w + 15) * h);
151         if (!ptr)
152             return AVERROR(ENOMEM);
153         stride = (w + 15 >> 3) * depth;
154     } else {
155     ptr    = p->data[0];
156     stride = p->linesize[0];
157     }
158
159     /* scanlines are aligned on 16 bit boundaries */
160     len  = (depth * w + 7) >> 3;
161     alen = len + (len&1);
162
163     if (type == RT_BYTE_ENCODED) {
164         int value, run;
165         uint8_t *end = ptr + h*stride;
166
167         x = 0;
168         while (ptr != end && buf < buf_end) {
169             run = 1;
170             if (buf_end - buf < 1)
171                 return AVERROR_INVALIDDATA;
172
173             if ((value = *buf++) == 0x80) {
174                 run = *buf++ + 1;
175                 if (run != 1)
176                     value = *buf++;
177             }
178             while (run--) {
179                 if (x < len)
180                     ptr[x] = value;
181                 if (++x >= alen) {
182                     x = 0;
183                     ptr += stride;
184                     if (ptr == end)
185                         break;
186                 }
187             }
188         }
189     } else {
190         for (y=0; y<h; y++) {
191             if (buf_end - buf < len)
192                 break;
193             memcpy(ptr, buf, len);
194             ptr += stride;
195             buf += alen;
196         }
197     }
198     if (avctx->pix_fmt == PIX_FMT_PAL8 && depth < 8) {
199         uint8_t *ptr_free = ptr2;
200         ptr = p->data[0];
201         for (y=0; y<h; y++) {
202             for (x = 0; x < (w + 7 >> 3) * depth; x++) {
203                 if (depth == 1) {
204                     ptr[8*x]   = ptr2[x] >> 7;
205                     ptr[8*x+1] = ptr2[x] >> 6 & 1;
206                     ptr[8*x+2] = ptr2[x] >> 5 & 1;
207                     ptr[8*x+3] = ptr2[x] >> 4 & 1;
208                     ptr[8*x+4] = ptr2[x] >> 3 & 1;
209                     ptr[8*x+5] = ptr2[x] >> 2 & 1;
210                     ptr[8*x+6] = ptr2[x] >> 1 & 1;
211                     ptr[8*x+7] = ptr2[x]      & 1;
212                 } else {
213                     ptr[2*x]   = ptr2[x] >> 4;
214                     ptr[2*x+1] = ptr2[x] & 0xF;
215                 }
216             }
217             ptr  += p->linesize[0];
218             ptr2 += (w + 15 >> 3) * depth;
219         }
220         av_freep(&ptr_free);
221     }
222
223     *picture = s->picture;
224     *data_size = sizeof(AVFrame);
225
226     return buf - bufstart;
227 }
228
229 static av_cold int sunrast_end(AVCodecContext *avctx) {
230     SUNRASTContext *s = avctx->priv_data;
231
232     if(s->picture.data[0])
233         avctx->release_buffer(avctx, &s->picture);
234
235     return 0;
236 }
237
238 AVCodec ff_sunrast_decoder = {
239     .name           = "sunrast",
240     .type           = AVMEDIA_TYPE_VIDEO,
241     .id             = CODEC_ID_SUNRAST,
242     .priv_data_size = sizeof(SUNRASTContext),
243     .init           = sunrast_init,
244     .close          = sunrast_end,
245     .decode         = sunrast_decode_frame,
246     .capabilities   = CODEC_CAP_DR1,
247     .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
248 };