3 * Todd Kirby <doubleshot@pacbell.net>
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/imgutils.h"
23 #include "libavutil/avassert.h"
25 #include "bytestream.h"
29 typedef struct SgiState {
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 out_end end of line in output buffer
43 * @param pixelstride pixel stride of input buffer
44 * @return size of output in bytes, else return error code.
46 static int expand_rle_row(SgiState *s, uint8_t *out_buf,
47 uint8_t *out_end, int pixelstride)
49 unsigned char pixel, count;
50 unsigned char *orig = out_buf;
52 while (out_buf < out_end) {
53 if (bytestream2_get_bytes_left(&s->g) < 1)
54 return AVERROR_INVALIDDATA;
55 pixel = bytestream2_get_byteu(&s->g);
56 if (!(count = (pixel & 0x7f))) {
60 /* Check for buffer overflow. */
61 if (out_end - out_buf <= pixelstride * (count - 1))
62 return AVERROR_INVALIDDATA;
66 *out_buf = bytestream2_get_byte(&s->g);
67 out_buf += pixelstride;
70 pixel = bytestream2_get_byte(&s->g);
74 out_buf += pixelstride;
78 return (out_buf - orig) / pixelstride;
82 * Read a run length encoded SGI image.
83 * @param out_buf output buffer
84 * @param s the current image state
85 * @return 0 if no error, else return error code.
87 static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
90 unsigned int len = s->height * s->depth * 4;
91 GetByteContext g_table = s->g;
93 unsigned int start_offset;
95 /* size of RLE offset and length tables */
96 if (len * 2 > bytestream2_get_bytes_left(&s->g)) {
97 return AVERROR_INVALIDDATA;
100 for (z = 0; z < s->depth; z++) {
102 for (y = 0; y < s->height; y++) {
103 dest_row -= s->linesize;
104 start_offset = bytestream2_get_be32(&g_table);
105 bytestream2_seek(&s->g, start_offset, SEEK_SET);
106 if (expand_rle_row(s, dest_row + z, dest_row + s->width*s->depth,
107 s->depth) != s->width) {
108 return AVERROR_INVALIDDATA;
116 * Read an uncompressed SGI image.
117 * @param out_buf output buffer
118 * @param s the current image state
119 * @return 0 if read success, else return error code.
121 static int read_uncompressed_sgi(unsigned char* out_buf, SgiState *s)
124 unsigned int offset = s->height * s->width * s->bytes_per_channel;
125 GetByteContext gp[4];
128 /* Test buffer size. */
129 if (offset * s->depth > bytestream2_get_bytes_left(&s->g))
130 return AVERROR_INVALIDDATA;
132 /* Create a reader for each plane */
133 for (z = 0; z < s->depth; z++) {
135 bytestream2_skip(&gp[z], z * offset);
138 for (y = s->height - 1; y >= 0; y--) {
139 out_end = out_buf + (y * s->linesize);
140 if (s->bytes_per_channel == 1) {
141 for (x = s->width; x > 0; x--) {
142 bytestream2_get_bufferu(&gp[z], out_end, s->depth);
146 uint16_t *out16 = (uint16_t *)out_end;
147 for (x = s->width; x > 0; x--)
148 for (z = 0; z < s->depth; z++)
149 *out16++ = bytestream2_get_ne16u(&gp[z]);
155 static int decode_frame(AVCodecContext *avctx,
156 void *data, int *got_frame,
159 SgiState *s = avctx->priv_data;
161 unsigned int dimension, rle;
163 uint8_t *out_buf, *out_end;
165 bytestream2_init(&s->g, avpkt->data, avpkt->size);
166 if (bytestream2_get_bytes_left(&s->g) < SGI_HEADER_SIZE) {
167 av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
168 return AVERROR_INVALIDDATA;
171 /* Test for SGI magic. */
172 if (bytestream2_get_be16u(&s->g) != SGI_MAGIC) {
173 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
174 return AVERROR_INVALIDDATA;
177 rle = bytestream2_get_byteu(&s->g);
178 s->bytes_per_channel = bytestream2_get_byteu(&s->g);
179 dimension = bytestream2_get_be16u(&s->g);
180 s->width = bytestream2_get_be16u(&s->g);
181 s->height = bytestream2_get_be16u(&s->g);
182 s->depth = bytestream2_get_be16u(&s->g);
184 if (s->bytes_per_channel != 1 && (s->bytes_per_channel != 2 || rle)) {
185 av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
186 return AVERROR_INVALIDDATA;
189 /* Check for supported image dimensions. */
190 if (dimension != 2 && dimension != 3) {
191 av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
192 return AVERROR_INVALIDDATA;
195 if (s->depth == SGI_GRAYSCALE) {
196 avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_GRAY16BE : AV_PIX_FMT_GRAY8;
197 } else if (s->depth == SGI_RGB) {
198 avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_RGB48BE : AV_PIX_FMT_RGB24;
199 } else if (s->depth == SGI_RGBA) {
200 avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_RGBA64BE : AV_PIX_FMT_RGBA;
202 av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
203 return AVERROR_INVALIDDATA;
206 if (av_image_check_size(s->width, s->height, 0, avctx))
207 return AVERROR_INVALIDDATA;
208 avcodec_set_dimensions(avctx, s->width, s->height);
210 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
213 p->pict_type = AV_PICTURE_TYPE_I;
215 out_buf = p->data[0];
217 out_end = out_buf + p->linesize[0] * s->height;
219 s->linesize = p->linesize[0];
222 bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET);
224 ret = read_rle_sgi(out_end, s);
226 ret = read_uncompressed_sgi(out_buf, s);
235 AVCodec ff_sgi_decoder = {
237 .type = AVMEDIA_TYPE_VIDEO,
238 .id = AV_CODEC_ID_SGI,
239 .priv_data_size = sizeof(SgiState),
240 .decode = decode_frame,
241 .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
242 .capabilities = CODEC_CAP_DR1,