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