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