]> git.sesse.net Git - ffmpeg/blob - libavcodec/sgidec.c
Merge commit 'ed61f3ca8a0664a697782253b354055136c5d303'
[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 "internal.h"
27 #include "sgi.h"
28
29 typedef struct SgiState {
30     AVCodecContext *avctx;
31     unsigned int width;
32     unsigned int height;
33     unsigned int depth;
34     unsigned int bytes_per_channel;
35     int linesize;
36     GetByteContext g;
37 } SgiState;
38
39 /**
40  * Expand an RLE row into a channel.
41  * @param s the current image state
42  * @param out_buf Points to one line after the output buffer.
43  * @param len length of out_buf in bytes
44  * @param pixelstride pixel stride of input buffer
45  * @return size of output in bytes, else return error code.
46  */
47 static int expand_rle_row(SgiState *s, uint8_t *out_buf,
48                           int len, int pixelstride)
49 {
50     unsigned char pixel, count;
51     unsigned char *orig = out_buf;
52     uint8_t *out_end = out_buf + len;
53
54     while (out_buf < out_end) {
55         if (bytestream2_get_bytes_left(&s->g) < 1)
56             return AVERROR_INVALIDDATA;
57         pixel = bytestream2_get_byteu(&s->g);
58         if (!(count = (pixel & 0x7f))) {
59             break;
60         }
61
62         /* Check for buffer overflow. */
63         if (out_end - out_buf <= pixelstride * (count - 1)) {
64             av_log(s->avctx, AV_LOG_ERROR, "Invalid pixel count.\n");
65             return AVERROR_INVALIDDATA;
66         }
67
68         if (pixel & 0x80) {
69             while (count--) {
70                 *out_buf = bytestream2_get_byte(&s->g);
71                 out_buf += pixelstride;
72             }
73         } else {
74             pixel = bytestream2_get_byte(&s->g);
75
76             while (count--) {
77                 *out_buf = pixel;
78                 out_buf += pixelstride;
79             }
80         }
81     }
82     return (out_buf - orig) / pixelstride;
83 }
84
85 /**
86  * Read a run length encoded SGI image.
87  * @param out_buf output buffer
88  * @param s the current image state
89  * @return 0 if no error, else return error code.
90  */
91 static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
92 {
93     uint8_t *dest_row;
94     unsigned int len = s->height * s->depth * 4;
95     GetByteContext g_table = s->g;
96     unsigned int y, z;
97     unsigned int start_offset;
98
99     /* size of  RLE offset and length tables */
100     if (len * 2  > bytestream2_get_bytes_left(&s->g)) {
101         return AVERROR_INVALIDDATA;
102     }
103
104     for (z = 0; z < s->depth; z++) {
105         dest_row = out_buf;
106         for (y = 0; y < s->height; y++) {
107             dest_row -= s->linesize;
108             start_offset = bytestream2_get_be32(&g_table);
109             bytestream2_seek(&s->g, start_offset, SEEK_SET);
110             if (expand_rle_row(s, dest_row + z, s->width*s->depth,
111                                s->depth) != s->width) {
112                 return AVERROR_INVALIDDATA;
113             }
114         }
115     }
116     return 0;
117 }
118
119 /**
120  * Read an uncompressed SGI image.
121  * @param out_buf output buffer
122  * @param s the current image state
123  * @return 0 if read success, else return error code.
124  */
125 static int read_uncompressed_sgi(unsigned char* out_buf, SgiState *s)
126 {
127     int x, y, z;
128     unsigned int offset = s->height * s->width * s->bytes_per_channel;
129     GetByteContext gp[4];
130     uint8_t *out_end;
131
132     /* Test buffer size. */
133     if (offset * s->depth > bytestream2_get_bytes_left(&s->g))
134         return AVERROR_INVALIDDATA;
135
136     /* Create a reader for each plane */
137     for (z = 0; z < s->depth; z++) {
138         gp[z] = s->g;
139         bytestream2_skip(&gp[z], z * offset);
140     }
141
142     for (y = s->height - 1; y >= 0; y--) {
143         out_end = out_buf + (y * s->linesize);
144         if (s->bytes_per_channel == 1) {
145             for (x = s->width; x > 0; x--)
146                 for (z = 0; z < s->depth; z++)
147                     *out_end++ = bytestream2_get_byteu(&gp[z]);
148         } else {
149             uint16_t *out16 = (uint16_t *)out_end;
150             for (x = s->width; x > 0; x--)
151                 for (z = 0; z < s->depth; z++)
152                     *out16++ = bytestream2_get_ne16u(&gp[z]);
153         }
154     }
155     return 0;
156 }
157
158 static int decode_frame(AVCodecContext *avctx,
159                         void *data, int *got_frame,
160                         AVPacket *avpkt)
161 {
162     SgiState *s = avctx->priv_data;
163     AVFrame *p = data;
164     unsigned int dimension, rle;
165     int ret = 0;
166     uint8_t *out_buf, *out_end;
167
168     bytestream2_init(&s->g, avpkt->data, avpkt->size);
169     if (bytestream2_get_bytes_left(&s->g) < SGI_HEADER_SIZE) {
170         av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
171         return AVERROR_INVALIDDATA;
172     }
173
174     /* Test for SGI magic. */
175     if (bytestream2_get_be16u(&s->g) != SGI_MAGIC) {
176         av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
177         return AVERROR_INVALIDDATA;
178     }
179
180     rle                  = bytestream2_get_byteu(&s->g);
181     s->bytes_per_channel = bytestream2_get_byteu(&s->g);
182     dimension            = bytestream2_get_be16u(&s->g);
183     s->width             = bytestream2_get_be16u(&s->g);
184     s->height            = bytestream2_get_be16u(&s->g);
185     s->depth             = bytestream2_get_be16u(&s->g);
186
187     if (s->bytes_per_channel != 1 && (s->bytes_per_channel != 2 || rle)) {
188         av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
189         return AVERROR_INVALIDDATA;
190     }
191
192     /* Check for supported image dimensions. */
193     if (dimension != 2 && dimension != 3) {
194         av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
195         return AVERROR_INVALIDDATA;
196     }
197
198     if (s->depth == SGI_GRAYSCALE) {
199         avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_GRAY16BE : AV_PIX_FMT_GRAY8;
200     } else if (s->depth == SGI_RGB) {
201         avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_RGB48BE : AV_PIX_FMT_RGB24;
202     } else if (s->depth == SGI_RGBA) {
203         avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_RGBA64BE : AV_PIX_FMT_RGBA;
204     } else {
205         av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
206         return AVERROR_INVALIDDATA;
207     }
208
209     ret = ff_set_dimensions(avctx, s->width, s->height);
210     if (ret < 0)
211         return ret;
212
213     if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
214         return ret;
215
216     p->pict_type = AV_PICTURE_TYPE_I;
217     p->key_frame = 1;
218     out_buf = p->data[0];
219
220     out_end = out_buf + p->linesize[0] * s->height;
221
222     s->linesize = p->linesize[0];
223
224     /* Skip header. */
225     bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET);
226     if (rle) {
227         ret = read_rle_sgi(out_end, s);
228     } else {
229         ret = read_uncompressed_sgi(out_buf, s);
230     }
231     if (ret)
232         return ret;
233
234     *got_frame = 1;
235     return avpkt->size;
236 }
237
238 static av_cold int sgi_decode_init(AVCodecContext *avctx)
239 {
240     SgiState *s = avctx->priv_data;
241
242     s->avctx = avctx;
243
244     return 0;
245 }
246
247 AVCodec ff_sgi_decoder = {
248     .name           = "sgi",
249     .long_name      = NULL_IF_CONFIG_SMALL("SGI image"),
250     .type           = AVMEDIA_TYPE_VIDEO,
251     .id             = AV_CODEC_ID_SGI,
252     .priv_data_size = sizeof(SgiState),
253     .decode         = decode_frame,
254     .init           = sgi_decode_init,
255     .capabilities   = CODEC_CAP_DR1,
256 };