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