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