]> git.sesse.net Git - ffmpeg/blob - libavcodec/sunrast.c
png: check bit depth for PAL8/Y400A pixel formats.
[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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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;
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_FORMAT_TIFF || type == RT_FORMAT_IFF || 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
90     switch (depth) {
91         case 1:
92             avctx->pix_fmt = PIX_FMT_MONOWHITE;
93             break;
94         case 8:
95             avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_GRAY8;
96             break;
97         case 24:
98             avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
99             break;
100         default:
101             av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
102             return AVERROR_INVALIDDATA;
103     }
104
105     if (p->data[0])
106         avctx->release_buffer(avctx, p);
107
108     if (w != avctx->width || h != avctx->height)
109         avcodec_set_dimensions(avctx, w, h);
110     if ((ret = avctx->get_buffer(avctx, p)) < 0) {
111         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
112         return ret;
113     }
114
115     p->pict_type = AV_PICTURE_TYPE_I;
116
117     if (buf_end - buf < maplength)
118         return AVERROR_INVALIDDATA;
119
120     if (depth != 8 && maplength) {
121         av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
122
123     } else if (maplength) {
124         unsigned int len = maplength / 3;
125
126         if (maplength % 3 || maplength > 768) {
127             av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
128             return AVERROR_INVALIDDATA;
129         }
130
131         ptr = p->data[1];
132         for (x = 0; x < len; x++, ptr += 4)
133             *(uint32_t *)ptr = (buf[x] << 16) + (buf[len + x] << 8) + buf[len + len + x];
134     }
135
136     buf += maplength;
137
138     ptr    = p->data[0];
139     stride = p->linesize[0];
140
141     /* scanlines are aligned on 16 bit boundaries */
142     len  = (depth * w + 7) >> 3;
143     alen = len + (len & 1);
144
145     if (type == RT_BYTE_ENCODED) {
146         int value, run;
147         uint8_t *end = ptr + h * stride;
148
149         x = 0;
150         while (ptr != end && buf < buf_end) {
151             run = 1;
152             if (buf_end - buf < 1)
153                 return AVERROR_INVALIDDATA;
154
155             if ((value = *buf++) == RLE_TRIGGER) {
156                 run = *buf++ + 1;
157                 if (run != 1)
158                     value = *buf++;
159             }
160             while (run--) {
161                 if (x < len)
162                     ptr[x] = value;
163                 if (++x >= alen) {
164                     x = 0;
165                     ptr += stride;
166                     if (ptr == end)
167                         break;
168                 }
169             }
170         }
171     } else {
172         for (y = 0; y < h; y++) {
173             if (buf_end - buf < len)
174                 break;
175             memcpy(ptr, buf, len);
176             ptr += stride;
177             buf += alen;
178         }
179     }
180
181     *picture   = s->picture;
182     *data_size = sizeof(AVFrame);
183
184     return buf - bufstart;
185 }
186
187 static av_cold int sunrast_end(AVCodecContext *avctx) {
188     SUNRASTContext *s = avctx->priv_data;
189
190     if(s->picture.data[0])
191         avctx->release_buffer(avctx, &s->picture);
192
193     return 0;
194 }
195
196 AVCodec ff_sunrast_decoder = {
197     .name           = "sunrast",
198     .type           = AVMEDIA_TYPE_VIDEO,
199     .id             = CODEC_ID_SUNRAST,
200     .priv_data_size = sizeof(SUNRASTContext),
201     .init           = sunrast_init,
202     .close          = sunrast_end,
203     .decode         = sunrast_decode_frame,
204     .capabilities   = CODEC_CAP_DR1,
205     .long_name      = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
206 };