]> git.sesse.net Git - ffmpeg/blob - libavcodec/sunrast.c
asfdec: ignore stored duration for truncated files
[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/common.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/imgutils.h"
25 #include "avcodec.h"
26 #include "sunrast.h"
27
28 typedef struct SUNRASTContext {
29     AVFrame picture;
30 } SUNRASTContext;
31
32 static av_cold int sunrast_init(AVCodecContext *avctx) {
33     SUNRASTContext *s = avctx->priv_data;
34
35     avcodec_get_frame_defaults(&s->picture);
36     avctx->coded_frame = &s->picture;
37
38     return 0;
39 }
40
41 static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
42                                 int *data_size, AVPacket *avpkt) {
43     const uint8_t *buf       = avpkt->data;
44     const uint8_t *buf_end   = avpkt->data + avpkt->size;
45     SUNRASTContext * const s = avctx->priv_data;
46     AVFrame *picture         = data;
47     AVFrame * const p        = &s->picture;
48     unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
49     uint8_t *ptr, *ptr2 = NULL;
50     const uint8_t *bufstart = buf;
51     int ret;
52
53     if (avpkt->size < 32)
54         return AVERROR_INVALIDDATA;
55
56     if (AV_RB32(buf) != RAS_MAGIC) {
57         av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
58         return AVERROR_INVALIDDATA;
59     }
60
61     w         = AV_RB32(buf + 4);
62     h         = AV_RB32(buf + 8);
63     depth     = AV_RB32(buf + 12);
64     type      = AV_RB32(buf + 20);
65     maptype   = AV_RB32(buf + 24);
66     maplength = AV_RB32(buf + 28);
67     buf      += 32;
68
69     if (type == RT_EXPERIMENTAL) {
70         av_log_ask_for_sample(avctx, "unsupported (compression) type\n");
71         return AVERROR_PATCHWELCOME;
72     }
73     if (type > RT_FORMAT_IFF) {
74         av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
75         return AVERROR_INVALIDDATA;
76     }
77     if (av_image_check_size(w, h, 0, avctx)) {
78         av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
79         return AVERROR_INVALIDDATA;
80     }
81     if (maptype == RMT_RAW) {
82         av_log_ask_for_sample(avctx, "unsupported colormap type\n");
83         return AVERROR_PATCHWELCOME;
84     }
85     if (maptype > RMT_RAW) {
86         av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
87         return AVERROR_INVALIDDATA;
88     }
89
90     if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
91         av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
92         return -1;
93     }
94
95     switch (depth) {
96         case 1:
97             avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_MONOWHITE;
98             break;
99         case 4:
100             avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_NONE;
101             break;
102         case 8:
103             avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
104             break;
105         case 24:
106             avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
107             break;
108         case 32:
109             avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_0RGB : AV_PIX_FMT_0BGR;
110             break;
111         default:
112             av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
113             return AVERROR_INVALIDDATA;
114     }
115
116     if (p->data[0])
117         avctx->release_buffer(avctx, p);
118
119     if (w != avctx->width || h != avctx->height)
120         avcodec_set_dimensions(avctx, w, h);
121     if ((ret = avctx->get_buffer(avctx, p)) < 0) {
122         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
123         return ret;
124     }
125
126     p->pict_type = AV_PICTURE_TYPE_I;
127
128     if (buf_end - buf < maplength)
129         return AVERROR_INVALIDDATA;
130
131     if (depth > 8 && maplength) {
132         av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
133
134     } else if (maplength) {
135         unsigned int len = maplength / 3;
136
137         if (maplength % 3 || maplength > 768) {
138             av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
139             return AVERROR_INVALIDDATA;
140         }
141
142         ptr = p->data[1];
143         for (x = 0; x < len; x++, ptr += 4)
144             *(uint32_t *)ptr = (0xFFU<<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++) == RLE_TRIGGER) {
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 == AV_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             = AV_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 };