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"
28 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, -1 if buffer overflows
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;
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))) {
57 return (out_buf - orig) / pixelstride;
60 /* Check for buffer overflow. */
61 if(out_buf + pixelstride * (count-1) >= out_end) return -1;
65 *out_buf = bytestream2_get_byte(&s->g);
66 out_buf += pixelstride;
69 pixel = bytestream2_get_byte(&s->g);
73 out_buf += pixelstride;
80 * Read a run length encoded SGI image.
81 * @param out_buf output buffer
82 * @param s the current image state
83 * @return 0 if no error, else return error number.
85 static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
88 unsigned int len = s->height * s->depth * 4;
89 GetByteContext g_table = s->g;
91 unsigned int start_offset;
93 /* size of RLE offset and length tables */
94 if (len * 2 > bytestream2_get_bytes_left(&s->g)) {
95 return AVERROR_INVALIDDATA;
98 for (z = 0; z < s->depth; z++) {
100 for (y = 0; y < s->height; y++) {
101 dest_row -= s->linesize;
102 start_offset = bytestream2_get_be32(&g_table);
103 bytestream2_seek(&s->g, start_offset, SEEK_SET);
104 if (expand_rle_row(s, dest_row + z, dest_row + FFABS(s->linesize),
105 s->depth) != s->width) {
106 return AVERROR_INVALIDDATA;
114 * Read an uncompressed SGI image.
115 * @param out_buf output buffer
116 * @param s the current image state
117 * @return 0 if read success, otherwise return -1.
119 static int read_uncompressed_sgi(unsigned char* out_buf, SgiState *s)
122 unsigned int offset = s->height * s->width * s->bytes_per_channel;
123 GetByteContext gp[4];
126 /* Test buffer size. */
127 if (offset * s->depth > bytestream2_get_bytes_left(&s->g))
128 return AVERROR_INVALIDDATA;
130 /* Create a reader for each plane */
131 for (z = 0; z < s->depth; z++) {
133 bytestream2_skip(&gp[z], z * offset);
136 for (y = s->height - 1; y >= 0; y--) {
137 out_end = out_buf + (y * s->linesize);
138 if (s->bytes_per_channel == 1) {
139 for (x = s->width; x > 0; x--)
140 for (z = 0; z < s->depth; z++)
141 *out_end++ = bytestream2_get_byteu(&gp[z]);
143 uint16_t *out16 = (uint16_t *)out_end;
144 for (x = s->width; x > 0; x--)
145 for (z = 0; z < s->depth; z++)
146 *out16++ = bytestream2_get_ne16u(&gp[z]);
152 static int decode_frame(AVCodecContext *avctx,
153 void *data, int *data_size,
156 SgiState *s = avctx->priv_data;
157 AVFrame *picture = data;
158 AVFrame *p = &s->picture;
159 unsigned int dimension, rle;
161 uint8_t *out_buf, *out_end;
163 bytestream2_init(&s->g, avpkt->data, avpkt->size);
164 if (bytestream2_get_bytes_left(&s->g) < SGI_HEADER_SIZE) {
165 av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
166 return AVERROR_INVALIDDATA;
169 /* Test for SGI magic. */
170 if (bytestream2_get_be16(&s->g) != SGI_MAGIC) {
171 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
172 return AVERROR_INVALIDDATA;
175 rle = bytestream2_get_byte(&s->g);
176 s->bytes_per_channel = bytestream2_get_byte(&s->g);
177 dimension = bytestream2_get_be16(&s->g);
178 s->width = bytestream2_get_be16(&s->g);
179 s->height = bytestream2_get_be16(&s->g);
180 s->depth = bytestream2_get_be16(&s->g);
182 if (s->bytes_per_channel != 1 && (s->bytes_per_channel != 2 || rle)) {
183 av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
187 /* Check for supported image dimensions. */
188 if (dimension != 2 && dimension != 3) {
189 av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
193 if (s->depth == SGI_GRAYSCALE) {
194 avctx->pix_fmt = s->bytes_per_channel == 2 ? PIX_FMT_GRAY16BE : PIX_FMT_GRAY8;
195 } else if (s->depth == SGI_RGB) {
196 avctx->pix_fmt = s->bytes_per_channel == 2 ? PIX_FMT_RGB48BE : PIX_FMT_RGB24;
197 } else if (s->depth == SGI_RGBA) {
198 avctx->pix_fmt = s->bytes_per_channel == 2 ? PIX_FMT_RGBA64BE : PIX_FMT_RGBA;
200 av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
204 if (av_image_check_size(s->width, s->height, 0, avctx))
206 avcodec_set_dimensions(avctx, s->width, s->height);
209 avctx->release_buffer(avctx, p);
212 if (avctx->get_buffer(avctx, p) < 0) {
213 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed.\n");
217 p->pict_type = AV_PICTURE_TYPE_I;
219 out_buf = p->data[0];
221 out_end = out_buf + p->linesize[0] * s->height;
223 s->linesize = p->linesize[0];
226 bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET);
228 ret = read_rle_sgi(out_end, s);
230 ret = read_uncompressed_sgi(out_buf, s);
234 *picture = s->picture;
235 *data_size = sizeof(AVPicture);
242 static av_cold int sgi_init(AVCodecContext *avctx){
243 SgiState *s = avctx->priv_data;
245 avcodec_get_frame_defaults(&s->picture);
246 avctx->coded_frame = &s->picture;
251 static av_cold int sgi_end(AVCodecContext *avctx)
253 SgiState * const s = avctx->priv_data;
255 if (s->picture.data[0])
256 avctx->release_buffer(avctx, &s->picture);
261 AVCodec ff_sgi_decoder = {
263 .type = AVMEDIA_TYPE_VIDEO,
265 .priv_data_size = sizeof(SgiState),
268 .decode = decode_frame,
269 .capabilities = CODEC_CAP_DR1,
270 .long_name = NULL_IF_CONFIG_SMALL("SGI image"),