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