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_row(SgiState
*s
, uint8_t *out_buf
,
47 int len
, 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 (pixelstride
* (count
- 1) >= len
) {
62 av_log(s
->avctx
, AV_LOG_ERROR
, "Invalid pixel count.\n");
63 return AVERROR_INVALIDDATA
;
68 *out_buf
= bytestream2_get_byte(&s
->g
);
69 out_buf
+= pixelstride
;
72 pixel
= bytestream2_get_byte(&s
->g
);
76 out_buf
+= pixelstride
;
83 * Read a run length encoded SGI image.
84 * @param out_buf output buffer
85 * @param s the current image state
86 * @return 0 if no error, else return error number.
88 static int read_rle_sgi(uint8_t *out_buf
, SgiState
*s
)
91 unsigned int len
= s
->height
* s
->depth
* 4;
92 GetByteContext g_table
= s
->g
;
94 unsigned int start_offset
;
96 /* size of RLE offset and length tables */
97 if (len
* 2 > bytestream2_get_bytes_left(&s
->g
)) {
98 return AVERROR_INVALIDDATA
;
101 for (z
= 0; z
< s
->depth
; z
++) {
103 for (y
= 0; y
< s
->height
; y
++) {
104 dest_row
-= s
->linesize
;
105 start_offset
= bytestream2_get_be32(&g_table
);
106 bytestream2_seek(&s
->g
, start_offset
, SEEK_SET
);
107 if (expand_rle_row(s
, dest_row
+ z
, FFABS(s
->linesize
) - z
,
108 s
->depth
) != s
->width
) {
109 return AVERROR_INVALIDDATA
;
117 * Read an uncompressed SGI image.
118 * @param out_buf output buffer
119 * @param out_end end ofoutput buffer
120 * @param s the current image state
121 * @return 0 if read success, otherwise return -1.
123 static int read_uncompressed_sgi(unsigned char* out_buf
, uint8_t* out_end
,
127 unsigned int offset
= s
->height
* s
->width
* s
->bytes_per_channel
;
128 GetByteContext gp
[4];
130 /* Test buffer size. */
131 if (offset
* s
->depth
> bytestream2_get_bytes_left(&s
->g
))
132 return AVERROR_INVALIDDATA
;
134 /* Create a reader for each plane */
135 for (z
= 0; z
< s
->depth
; z
++) {
137 bytestream2_skip(&gp
[z
], z
* offset
);
140 for (y
= s
->height
- 1; y
>= 0; y
--) {
141 out_end
= out_buf
+ (y
* s
->linesize
);
142 if (s
->bytes_per_channel
== 1) {
143 for (x
= s
->width
; x
> 0; x
--)
144 for (z
= 0; z
< s
->depth
; z
++)
145 *out_end
++ = bytestream2_get_byteu(&gp
[z
]);
147 uint16_t *out16
= (uint16_t *)out_end
;
148 for (x
= s
->width
; x
> 0; x
--)
149 for (z
= 0; z
< s
->depth
; z
++)
150 *out16
++ = bytestream2_get_ne16u(&gp
[z
]);
156 static int decode_frame(AVCodecContext
*avctx
,
157 void *data
, int *got_frame
,
160 SgiState
*s
= avctx
->priv_data
;
162 unsigned int dimension
, rle
;
164 uint8_t *out_buf
, *out_end
;
166 bytestream2_init(&s
->g
, avpkt
->data
, avpkt
->size
);
167 if (bytestream2_get_bytes_left(&s
->g
) < SGI_HEADER_SIZE
) {
168 av_log(avctx
, AV_LOG_ERROR
, "buf_size too small (%d)\n", avpkt
->size
);
169 return AVERROR_INVALIDDATA
;
172 /* Test for SGI magic. */
173 if (bytestream2_get_be16(&s
->g
) != SGI_MAGIC
) {
174 av_log(avctx
, AV_LOG_ERROR
, "bad magic number\n");
175 return AVERROR_INVALIDDATA
;
178 rle
= bytestream2_get_byte(&s
->g
);
179 s
->bytes_per_channel
= bytestream2_get_byte(&s
->g
);
180 dimension
= bytestream2_get_be16(&s
->g
);
181 s
->width
= bytestream2_get_be16(&s
->g
);
182 s
->height
= bytestream2_get_be16(&s
->g
);
183 s
->depth
= bytestream2_get_be16(&s
->g
);
185 if (s
->bytes_per_channel
!= 1 && (s
->bytes_per_channel
!= 2 || rle
)) {
186 av_log(avctx
, AV_LOG_ERROR
, "wrong channel number\n");
190 /* Check for supported image dimensions. */
191 if (dimension
!= 2 && dimension
!= 3) {
192 av_log(avctx
, AV_LOG_ERROR
, "wrong dimension number\n");
196 if (s
->depth
== SGI_GRAYSCALE
) {
197 avctx
->pix_fmt
= s
->bytes_per_channel
== 2 ? AV_PIX_FMT_GRAY16BE
: AV_PIX_FMT_GRAY8
;
198 } else if (s
->depth
== SGI_RGB
) {
199 avctx
->pix_fmt
= s
->bytes_per_channel
== 2 ? AV_PIX_FMT_RGB48BE
: AV_PIX_FMT_RGB24
;
200 } else if (s
->depth
== SGI_RGBA
&& s
->bytes_per_channel
== 1) {
201 avctx
->pix_fmt
= AV_PIX_FMT_RGBA
;
203 av_log(avctx
, AV_LOG_ERROR
, "wrong picture format\n");
207 ret
= ff_set_dimensions(avctx
, s
->width
, s
->height
);
211 if (ff_get_buffer(avctx
, p
, 0) < 0) {
212 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed.\n");
216 p
->pict_type
= AV_PICTURE_TYPE_I
;
218 out_buf
= p
->data
[0];
220 out_end
= out_buf
+ p
->linesize
[0] * s
->height
;
222 s
->linesize
= p
->linesize
[0];
225 bytestream2_seek(&s
->g
, SGI_HEADER_SIZE
, SEEK_SET
);
227 ret
= read_rle_sgi(out_end
, s
);
229 ret
= read_uncompressed_sgi(out_buf
, out_end
, s
);
240 static av_cold
int sgi_decode_init(AVCodecContext
*avctx
)
242 SgiState
*s
= avctx
->priv_data
;
249 AVCodec ff_sgi_decoder
= {
251 .long_name
= NULL_IF_CONFIG_SMALL("SGI image"),
252 .type
= AVMEDIA_TYPE_VIDEO
,
253 .id
= AV_CODEC_ID_SGI
,
254 .priv_data_size
= sizeof(SgiState
),
255 .decode
= decode_frame
,
256 .init
= sgi_decode_init
,
257 .capabilities
= CODEC_CAP_DR1
,