]> git.sesse.net Git - ffmpeg/blob - libavcodec/sgidec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / sgidec.c
1 /*
2  * SGI image decoder
3  * Todd Kirby <doubleshot@pacbell.net>
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/imgutils.h"
23 #include "libavutil/avassert.h"
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "sgi.h"
27
28 typedef struct SgiState {
29     AVFrame picture;
30     unsigned int width;
31     unsigned int height;
32     unsigned int depth;
33     unsigned int bytes_per_channel;
34     int linesize;
35 } SgiState;
36
37 /**
38  * Expand an RLE row into a channel.
39  * @param in_buf input buffer
40  * @param in_end end of input buffer
41  * @param out_buf Points to one line after the output buffer.
42  * @param out_end end of line in output buffer
43  * @param pixelstride pixel stride of input buffer
44  * @return size of output in bytes, -1 if buffer overflows
45  */
46 static int expand_rle_row(const uint8_t *in_buf, const uint8_t* in_end,
47             unsigned char *out_buf, uint8_t* out_end, int pixelstride)
48 {
49     unsigned char pixel, count;
50     unsigned char *orig = out_buf;
51
52     while (1) {
53         if(in_buf + 1 > in_end) return -1;
54         pixel = bytestream_get_byte(&in_buf);
55         if (!(count = (pixel & 0x7f))) {
56             return (out_buf - orig) / pixelstride;
57         }
58
59         /* Check for buffer overflow. */
60         if(out_buf + pixelstride * count >= out_end) return -1;
61
62         if (pixel & 0x80) {
63             while (count--) {
64                 *out_buf = bytestream_get_byte(&in_buf);
65                 out_buf += pixelstride;
66             }
67         } else {
68             pixel = bytestream_get_byte(&in_buf);
69
70             while (count--) {
71                 *out_buf = pixel;
72                 out_buf += pixelstride;
73             }
74         }
75     }
76 }
77
78 /**
79  * Read a run length encoded SGI image.
80  * @param out_buf output buffer
81  * @param in_buf input buffer
82  * @param in_end end of input buffer
83  * @param s the current image state
84  * @return 0 if no error, else return error number.
85  */
86 static int read_rle_sgi(unsigned char* out_buf, const uint8_t *in_buf,
87                         const uint8_t *in_end, SgiState* s)
88 {
89     uint8_t *dest_row;
90     unsigned int len = s->height * s->depth * 4;
91     const uint8_t *start_table = in_buf;
92     unsigned int y, z;
93     unsigned int start_offset;
94
95     /* size of  RLE offset and length tables */
96     if(len * 2  > in_end - in_buf) {
97         return AVERROR_INVALIDDATA;
98     }
99
100     in_buf -= SGI_HEADER_SIZE;
101     for (z = 0; z < s->depth; z++) {
102         dest_row = out_buf;
103         for (y = 0; y < s->height; y++) {
104             dest_row -= s->linesize;
105             start_offset = bytestream_get_be32(&start_table);
106             if(start_offset > in_end - in_buf) {
107                 return AVERROR_INVALIDDATA;
108             }
109             if (expand_rle_row(in_buf + start_offset, in_end, dest_row + z,
110                 dest_row + FFABS(s->linesize), s->depth) != s->width)
111                 return AVERROR_INVALIDDATA;
112         }
113     }
114     return 0;
115 }
116
117 static av_always_inline void copy_loop(uint8_t *out_buf, const uint8_t *in_buf,
118                                        unsigned offset, unsigned bytes_per_channel,
119                                        SgiState *s)
120 {
121     int x, y, z;
122     for (y = s->height - 1; y >= 0; y--) {
123         uint8_t *line = out_buf + (y * s->linesize);
124         for (x = s->width; x > 0; x--) {
125             const uint8_t *ptr = in_buf;
126             in_buf += bytes_per_channel;
127             for(z = 0; z < s->depth; z ++) {
128                 memcpy(line, ptr, bytes_per_channel);
129                 line += bytes_per_channel;
130                 ptr += offset;
131             }
132         }
133     }
134 }
135
136 /**
137  * Read an uncompressed SGI image.
138  * @param out_buf output buffer
139  * @param out_end end ofoutput buffer
140  * @param in_buf input buffer
141  * @param in_end end of input buffer
142  * @param s the current image state
143  * @return 0 if read success, otherwise return -1.
144  */
145 static int read_uncompressed_sgi(unsigned char* out_buf, uint8_t* out_end,
146                 const uint8_t *in_buf, const uint8_t *in_end, SgiState* s)
147 {
148     unsigned int offset = s->height * s->width * s->bytes_per_channel;
149
150     /* Test buffer size. */
151     if (offset * s->depth > in_end - in_buf) {
152        return -1;
153     }
154
155     if (s->bytes_per_channel == 2) {
156         copy_loop(out_buf, in_buf, offset, 2, s);
157     } else {
158         av_assert1(s->bytes_per_channel == 1);
159         copy_loop(out_buf, in_buf, offset, 1, s);
160     }
161     return 0;
162 }
163
164 static int decode_frame(AVCodecContext *avctx,
165                         void *data, int *data_size,
166                         AVPacket *avpkt)
167 {
168     const uint8_t *in_buf = avpkt->data;
169     int buf_size = avpkt->size;
170     SgiState *s = avctx->priv_data;
171     AVFrame *picture = data;
172     AVFrame *p = &s->picture;
173     const uint8_t *in_end = in_buf + buf_size;
174     unsigned int dimension, rle;
175     int ret = 0;
176     uint8_t *out_buf, *out_end;
177
178     if (buf_size < SGI_HEADER_SIZE){
179         av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", buf_size);
180         return -1;
181     }
182
183     /* Test for SGI magic. */
184     if (bytestream_get_be16(&in_buf) != SGI_MAGIC) {
185         av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
186         return -1;
187     }
188
189     rle = bytestream_get_byte(&in_buf);
190     s->bytes_per_channel = bytestream_get_byte(&in_buf);
191     dimension = bytestream_get_be16(&in_buf);
192     s->width  = bytestream_get_be16(&in_buf);
193     s->height = bytestream_get_be16(&in_buf);
194     s->depth  = bytestream_get_be16(&in_buf);
195
196     if (s->bytes_per_channel != 1 && (s->bytes_per_channel != 2 || rle)) {
197         av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
198         return -1;
199     }
200
201     /* Check for supported image dimensions. */
202     if (dimension != 2 && dimension != 3) {
203         av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
204         return -1;
205     }
206
207     if (s->depth == SGI_GRAYSCALE) {
208         avctx->pix_fmt = s->bytes_per_channel == 2 ? PIX_FMT_GRAY16BE : PIX_FMT_GRAY8;
209     } else if (s->depth == SGI_RGB) {
210         avctx->pix_fmt = s->bytes_per_channel == 2 ? PIX_FMT_RGB48BE : PIX_FMT_RGB24;
211     } else if (s->depth == SGI_RGBA && s->bytes_per_channel == 1) {
212         avctx->pix_fmt = PIX_FMT_RGBA;
213     } else {
214         av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
215         return -1;
216     }
217
218     if (av_image_check_size(s->width, s->height, 0, avctx))
219         return -1;
220     avcodec_set_dimensions(avctx, s->width, s->height);
221
222     if (p->data[0])
223         avctx->release_buffer(avctx, p);
224
225     p->reference = 0;
226     if (avctx->get_buffer(avctx, p) < 0) {
227         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed.\n");
228         return -1;
229     }
230
231     p->pict_type = AV_PICTURE_TYPE_I;
232     p->key_frame = 1;
233     out_buf = p->data[0];
234
235     out_end = out_buf + p->linesize[0] * s->height;
236
237     s->linesize = p->linesize[0];
238
239     /* Skip header. */
240     in_buf += SGI_HEADER_SIZE - 12;
241     if (rle) {
242         ret = read_rle_sgi(out_end, in_buf, in_end, s);
243     } else {
244         ret = read_uncompressed_sgi(out_buf, out_end, in_buf, in_end, s);
245     }
246
247     if (ret == 0) {
248         *picture   = s->picture;
249         *data_size = sizeof(AVPicture);
250         return buf_size;
251     } else {
252         return -1;
253     }
254 }
255
256 static av_cold int sgi_init(AVCodecContext *avctx){
257     SgiState *s = avctx->priv_data;
258
259     avcodec_get_frame_defaults(&s->picture);
260     avctx->coded_frame = &s->picture;
261
262     return 0;
263 }
264
265 static av_cold int sgi_end(AVCodecContext *avctx)
266 {
267     SgiState * const s = avctx->priv_data;
268
269     if (s->picture.data[0])
270         avctx->release_buffer(avctx, &s->picture);
271
272     return 0;
273 }
274
275 AVCodec ff_sgi_decoder = {
276     .name           = "sgi",
277     .type           = AVMEDIA_TYPE_VIDEO,
278     .id             = CODEC_ID_SGI,
279     .priv_data_size = sizeof(SgiState),
280     .init           = sgi_init,
281     .close          = sgi_end,
282     .decode         = decode_frame,
283     .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
284 };
285