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;
38 * Expand an RLE row into a channel.
39 * @param in_buf input buffer
40 * @param in_end end of input buffer
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(const uint8_t *in_buf, const uint8_t* in_end,
47 unsigned char *out_buf, uint8_t* out_end, int pixelstride)
49 unsigned char pixel, count;
50 unsigned char *orig = out_buf;
53 if(in_buf + 1 > in_end) return -1;
54 pixel = bytestream_get_byte(&in_buf);
55 if (!(count = (pixel & 0x7f))) {
56 return (out_buf - orig) / pixelstride;
59 /* Check for buffer overflow. */
60 if(out_buf + pixelstride * count >= out_end) return -1;
64 *out_buf = bytestream_get_byte(&in_buf);
65 out_buf += pixelstride;
68 pixel = bytestream_get_byte(&in_buf);
72 out_buf += pixelstride;
79 * Read a run length encoded SGI image.
80 * @param out_buf output buffer
81 * @param in_buf input buffer
82 * @param in_end end of input buffer
83 * @param s the current image state
84 * @return 0 if no error, else return error number.
86 static int read_rle_sgi(unsigned char* out_buf, const uint8_t *in_buf,
87 const uint8_t *in_end, SgiState* s)
90 unsigned int len = s->height * s->depth * 4;
91 const uint8_t *start_table = in_buf;
93 unsigned int start_offset;
95 /* size of RLE offset and length tables */
96 if(len * 2 > in_end - in_buf) {
97 return AVERROR_INVALIDDATA;
100 in_buf -= SGI_HEADER_SIZE;
101 for (z = 0; z < s->depth; z++) {
103 for (y = 0; y < s->height; y++) {
104 dest_row -= s->linesize;
105 start_offset = bytestream_get_be32(&start_table);
106 if(start_offset > in_end - in_buf) {
107 return AVERROR_INVALIDDATA;
109 if (expand_rle_row(in_buf + start_offset, in_end, dest_row + z,
110 dest_row + FFABS(s->linesize), s->depth) != s->width)
111 return AVERROR_INVALIDDATA;
117 static av_always_inline void copy_loop(uint8_t *out_buf, const uint8_t *in_buf,
118 unsigned offset, unsigned bytes_per_channel,
122 for (y = s->height - 1; y >= 0; y--) {
123 uint8_t *line = out_buf + (y * s->linesize);
124 for (x = s->width; x > 0; x--) {
125 const uint8_t *ptr = in_buf;
126 in_buf += bytes_per_channel;
127 for(z = 0; z < s->depth; z ++) {
128 memcpy(line, ptr, bytes_per_channel);
129 line += bytes_per_channel;
137 * Read an uncompressed SGI image.
138 * @param out_buf output buffer
139 * @param out_end end ofoutput buffer
140 * @param in_buf input buffer
141 * @param in_end end of input buffer
142 * @param s the current image state
143 * @return 0 if read success, otherwise return -1.
145 static int read_uncompressed_sgi(unsigned char* out_buf, uint8_t* out_end,
146 const uint8_t *in_buf, const uint8_t *in_end, SgiState* s)
148 unsigned int offset = s->height * s->width * s->bytes_per_channel;
150 /* Test buffer size. */
151 if (offset * s->depth > in_end - in_buf) {
155 if (s->bytes_per_channel == 2) {
156 copy_loop(out_buf, in_buf, offset, 2, s);
158 av_assert1(s->bytes_per_channel == 1);
159 copy_loop(out_buf, in_buf, offset, 1, s);
164 static int decode_frame(AVCodecContext *avctx,
165 void *data, int *data_size,
168 const uint8_t *in_buf = avpkt->data;
169 int buf_size = avpkt->size;
170 SgiState *s = avctx->priv_data;
171 AVFrame *picture = data;
172 AVFrame *p = &s->picture;
173 const uint8_t *in_end = in_buf + buf_size;
174 unsigned int dimension, rle;
176 uint8_t *out_buf, *out_end;
178 if (buf_size < SGI_HEADER_SIZE){
179 av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", buf_size);
183 /* Test for SGI magic. */
184 if (bytestream_get_be16(&in_buf) != SGI_MAGIC) {
185 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
189 rle = bytestream_get_byte(&in_buf);
190 s->bytes_per_channel = bytestream_get_byte(&in_buf);
191 dimension = bytestream_get_be16(&in_buf);
192 s->width = bytestream_get_be16(&in_buf);
193 s->height = bytestream_get_be16(&in_buf);
194 s->depth = bytestream_get_be16(&in_buf);
196 if (s->bytes_per_channel != 1 && (s->bytes_per_channel != 2 || rle)) {
197 av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
201 /* Check for supported image dimensions. */
202 if (dimension != 2 && dimension != 3) {
203 av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
207 if (s->depth == SGI_GRAYSCALE) {
208 avctx->pix_fmt = s->bytes_per_channel == 2 ? PIX_FMT_GRAY16BE : PIX_FMT_GRAY8;
209 } else if (s->depth == SGI_RGB) {
210 avctx->pix_fmt = s->bytes_per_channel == 2 ? PIX_FMT_RGB48BE : PIX_FMT_RGB24;
211 } else if (s->depth == SGI_RGBA && s->bytes_per_channel == 1) {
212 avctx->pix_fmt = PIX_FMT_RGBA;
214 av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
218 if (av_image_check_size(s->width, s->height, 0, avctx))
220 avcodec_set_dimensions(avctx, s->width, s->height);
223 avctx->release_buffer(avctx, p);
226 if (avctx->get_buffer(avctx, p) < 0) {
227 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed.\n");
231 p->pict_type = AV_PICTURE_TYPE_I;
233 out_buf = p->data[0];
235 out_end = out_buf + p->linesize[0] * s->height;
237 s->linesize = p->linesize[0];
240 in_buf += SGI_HEADER_SIZE - 12;
242 ret = read_rle_sgi(out_end, in_buf, in_end, s);
244 ret = read_uncompressed_sgi(out_buf, out_end, in_buf, in_end, s);
248 *picture = s->picture;
249 *data_size = sizeof(AVPicture);
256 static av_cold int sgi_init(AVCodecContext *avctx){
257 SgiState *s = avctx->priv_data;
259 avcodec_get_frame_defaults(&s->picture);
260 avctx->coded_frame = &s->picture;
265 static av_cold int sgi_end(AVCodecContext *avctx)
267 SgiState * const s = avctx->priv_data;
269 if (s->picture.data[0])
270 avctx->release_buffer(avctx, &s->picture);
275 AVCodec ff_sgi_decoder = {
277 .type = AVMEDIA_TYPE_VIDEO,
279 .priv_data_size = sizeof(SgiState),
282 .decode = decode_frame,
283 .long_name = NULL_IF_CONFIG_SMALL("SGI image"),