3 * Todd Kirby <doubleshot@pacbell.net>
5 * This file is part of Libav.
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.
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.
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
22 #include "libavutil/imgutils.h"
24 #include "bytestream.h"
28 typedef struct SgiState {
29 AVCodecContext *avctx;
33 unsigned int bytes_per_channel;
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
46 static int expand_rle_row8(SgiState *s, uint8_t *out_buf,
47 int len, int pixelstride)
49 unsigned char pixel, count;
50 unsigned char *orig = out_buf;
51 uint8_t *out_end = out_buf + len;
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))) {
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;
69 *out_buf = bytestream2_get_byte(&s->g);
70 out_buf += pixelstride;
73 pixel = bytestream2_get_byte(&s->g);
77 out_buf += pixelstride;
81 return (out_buf - orig) / pixelstride;
84 static int expand_rle_row16(SgiState *s, uint16_t *out_buf,
85 int len, int pixelstride)
89 unsigned short *orig = out_buf;
90 uint16_t *out_end = out_buf + len;
92 while (out_buf < out_end) {
93 if (bytestream2_get_bytes_left(&s->g) < 2)
94 return AVERROR_INVALIDDATA;
95 pixel = bytestream2_get_be16u(&s->g);
96 if (!(count = (pixel & 0x7f)))
99 /* Check for buffer overflow. */
100 if (pixelstride * (count - 1) >= len) {
101 av_log(s->avctx, AV_LOG_ERROR, "Invalid pixel count.\n");
102 return AVERROR_INVALIDDATA;
107 pixel = bytestream2_get_ne16(&s->g);
108 AV_WN16A(out_buf, pixel);
109 out_buf += pixelstride;
112 pixel = bytestream2_get_ne16(&s->g);
115 AV_WN16A(out_buf, pixel);
116 out_buf += pixelstride;
120 return (out_buf - orig) / pixelstride;
125 * Read a run length encoded SGI image.
126 * @param out_buf output buffer
127 * @param s the current image state
128 * @return 0 if no error, else return error number.
130 static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
133 unsigned int len = s->height * s->depth * 4;
134 GetByteContext g_table = s->g;
136 unsigned int start_offset;
139 /* size of RLE offset and length tables */
140 if (len * 2 > bytestream2_get_bytes_left(&s->g)) {
141 return AVERROR_INVALIDDATA;
144 for (z = 0; z < s->depth; z++) {
146 for (y = 0; y < s->height; y++) {
147 linesize = s->width * s->depth * s->bytes_per_channel;
148 dest_row -= s->linesize;
149 start_offset = bytestream2_get_be32(&g_table);
150 bytestream2_seek(&s->g, start_offset, SEEK_SET);
151 if (s->bytes_per_channel == 1)
152 ret = expand_rle_row8(s, dest_row + z, linesize, s->depth);
154 ret = expand_rle_row16(s, (uint16_t *)dest_row + z, linesize, s->depth);
156 return AVERROR_INVALIDDATA;
163 * Read an uncompressed SGI image.
164 * @param out_buf output buffer
165 * @param s the current image state
166 * @return 0 if read success, otherwise return -1.
168 static int read_uncompressed_sgi(unsigned char *out_buf, SgiState *s)
171 unsigned int offset = s->height * s->width * s->bytes_per_channel;
172 GetByteContext gp[4];
175 /* Test buffer size. */
176 if (offset * s->depth > bytestream2_get_bytes_left(&s->g))
177 return AVERROR_INVALIDDATA;
179 /* Create a reader for each plane */
180 for (z = 0; z < s->depth; z++) {
182 bytestream2_skip(&gp[z], z * offset);
185 for (y = s->height - 1; y >= 0; y--) {
186 out_end = out_buf + (y * s->linesize);
187 if (s->bytes_per_channel == 1) {
188 for (x = s->width; x > 0; x--)
189 for (z = 0; z < s->depth; z++)
190 *out_end++ = bytestream2_get_byteu(&gp[z]);
192 uint16_t *out16 = (uint16_t *)out_end;
193 for (x = s->width; x > 0; x--)
194 for (z = 0; z < s->depth; z++)
195 *out16++ = bytestream2_get_ne16u(&gp[z]);
201 static int decode_frame(AVCodecContext *avctx,
202 void *data, int *got_frame,
205 SgiState *s = avctx->priv_data;
207 unsigned int dimension, rle;
209 uint8_t *out_buf, *out_end;
211 bytestream2_init(&s->g, avpkt->data, avpkt->size);
212 if (bytestream2_get_bytes_left(&s->g) < SGI_HEADER_SIZE) {
213 av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
214 return AVERROR_INVALIDDATA;
217 /* Test for SGI magic. */
218 if (bytestream2_get_be16(&s->g) != SGI_MAGIC) {
219 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
220 return AVERROR_INVALIDDATA;
223 rle = bytestream2_get_byte(&s->g);
224 s->bytes_per_channel = bytestream2_get_byte(&s->g);
225 dimension = bytestream2_get_be16(&s->g);
226 s->width = bytestream2_get_be16(&s->g);
227 s->height = bytestream2_get_be16(&s->g);
228 s->depth = bytestream2_get_be16(&s->g);
230 if (s->bytes_per_channel != 1 && s->bytes_per_channel != 2) {
231 av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
232 return AVERROR(EINVAL);
235 /* Check for supported image dimensions. */
236 if (dimension != 2 && dimension != 3) {
237 av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
238 return AVERROR(EINVAL);
241 if (s->depth == SGI_GRAYSCALE) {
242 avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_GRAY16BE : AV_PIX_FMT_GRAY8;
243 } else if (s->depth == SGI_RGB) {
244 avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_RGB48BE : AV_PIX_FMT_RGB24;
245 } else if (s->depth == SGI_RGBA) {
246 avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_RGBA64BE : AV_PIX_FMT_RGBA;
248 av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
249 return AVERROR(EINVAL);
252 ret = ff_set_dimensions(avctx, s->width, s->height);
256 ret = ff_get_buffer(avctx, p, 0);
258 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed.\n");
262 p->pict_type = AV_PICTURE_TYPE_I;
264 out_buf = p->data[0];
266 out_end = out_buf + p->linesize[0] * s->height;
268 s->linesize = p->linesize[0];
271 bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET);
273 ret = read_rle_sgi(out_end, s);
275 ret = read_uncompressed_sgi(out_buf, s);
286 static av_cold int sgi_decode_init(AVCodecContext *avctx)
288 SgiState *s = avctx->priv_data;
295 AVCodec ff_sgi_decoder = {
297 .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
298 .type = AVMEDIA_TYPE_VIDEO,
299 .id = AV_CODEC_ID_SGI,
300 .priv_data_size = sizeof(SgiState),
301 .decode = decode_frame,
302 .init = sgi_decode_init,
303 .capabilities = AV_CODEC_CAP_DR1,