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
{
30 AVCodecContext
*avctx
;
34 unsigned int bytes_per_channel
;
40 * Expand an RLE row into a channel.
41 * @param s the current image state
42 * @param out_buf Points to one line after the output buffer.
43 * @param len length of out_buf in bytes
44 * @param pixelstride pixel stride of input buffer
45 * @return size of output in bytes, else return error code.
47 static int expand_rle_row8(SgiState
*s
, uint8_t *out_buf
,
48 int len
, int pixelstride
)
50 unsigned char pixel
, count
;
51 unsigned char *orig
= out_buf
;
52 uint8_t *out_end
= out_buf
+ len
;
54 while (out_buf
< out_end
) {
55 if (bytestream2_get_bytes_left(&s
->g
) < 1)
56 return AVERROR_INVALIDDATA
;
57 pixel
= bytestream2_get_byteu(&s
->g
);
58 if (!(count
= (pixel
& 0x7f))) {
62 /* Check for buffer overflow. */
63 if (out_end
- out_buf
<= pixelstride
* (count
- 1)) {
64 av_log(s
->avctx
, AV_LOG_ERROR
, "Invalid pixel count.\n");
65 return AVERROR_INVALIDDATA
;
70 *out_buf
= bytestream2_get_byte(&s
->g
);
71 out_buf
+= pixelstride
;
74 pixel
= bytestream2_get_byte(&s
->g
);
78 out_buf
+= pixelstride
;
82 return (out_buf
- orig
) / pixelstride
;
85 static int expand_rle_row16(SgiState
*s
, uint16_t *out_buf
,
86 int len
, int pixelstride
)
90 unsigned short *orig
= out_buf
;
91 uint16_t *out_end
= out_buf
+ len
;
93 while (out_buf
< out_end
) {
94 if (bytestream2_get_bytes_left(&s
->g
) < 2)
95 return AVERROR_INVALIDDATA
;
96 pixel
= bytestream2_get_be16u(&s
->g
);
97 if (!(count
= (pixel
& 0x7f)))
100 /* Check for buffer overflow. */
101 if (out_end
- out_buf
<= pixelstride
* (count
- 1)) {
102 av_log(s
->avctx
, AV_LOG_ERROR
, "Invalid pixel count.\n");
103 return AVERROR_INVALIDDATA
;
108 pixel
= bytestream2_get_ne16(&s
->g
);
109 AV_WN16A(out_buf
, pixel
);
110 out_buf
+= pixelstride
;
113 pixel
= bytestream2_get_ne16(&s
->g
);
116 AV_WN16A(out_buf
, pixel
);
117 out_buf
+= pixelstride
;
121 return (out_buf
- orig
) / pixelstride
;
126 * Read a run length encoded SGI image.
127 * @param out_buf output buffer
128 * @param s the current image state
129 * @return 0 if no error, else return error code.
131 static int read_rle_sgi(uint8_t *out_buf
, SgiState
*s
)
134 unsigned int len
= s
->height
* s
->depth
* 4;
135 GetByteContext g_table
= s
->g
;
137 unsigned int start_offset
;
140 /* size of RLE offset and length tables */
141 if (len
* 2 > bytestream2_get_bytes_left(&s
->g
)) {
142 return AVERROR_INVALIDDATA
;
145 for (z
= 0; z
< s
->depth
; z
++) {
147 for (y
= 0; y
< s
->height
; y
++) {
148 linesize
= s
->width
* s
->depth
;
149 dest_row
-= s
->linesize
;
150 start_offset
= bytestream2_get_be32(&g_table
);
151 bytestream2_seek(&s
->g
, start_offset
, SEEK_SET
);
152 if (s
->bytes_per_channel
== 1)
153 ret
= expand_rle_row8(s
, dest_row
+ z
, linesize
, s
->depth
);
155 ret
= expand_rle_row16(s
, (uint16_t *)dest_row
+ z
, linesize
, s
->depth
);
157 return AVERROR_INVALIDDATA
;
164 * Read an uncompressed SGI image.
165 * @param out_buf output buffer
166 * @param s the current image state
167 * @return 0 if read success, else return error code.
169 static int read_uncompressed_sgi(unsigned char *out_buf
, SgiState
*s
)
172 unsigned int offset
= s
->height
* s
->width
* s
->bytes_per_channel
;
173 GetByteContext gp
[4];
176 /* Test buffer size. */
177 if (offset
* s
->depth
> bytestream2_get_bytes_left(&s
->g
))
178 return AVERROR_INVALIDDATA
;
180 /* Create a reader for each plane */
181 for (z
= 0; z
< s
->depth
; z
++) {
183 bytestream2_skip(&gp
[z
], z
* offset
);
186 for (y
= s
->height
- 1; y
>= 0; y
--) {
187 out_end
= out_buf
+ (y
* s
->linesize
);
188 if (s
->bytes_per_channel
== 1) {
189 for (x
= s
->width
; x
> 0; x
--)
190 for (z
= 0; z
< s
->depth
; z
++)
191 *out_end
++ = bytestream2_get_byteu(&gp
[z
]);
193 uint16_t *out16
= (uint16_t *)out_end
;
194 for (x
= s
->width
; x
> 0; x
--)
195 for (z
= 0; z
< s
->depth
; z
++)
196 *out16
++ = bytestream2_get_ne16u(&gp
[z
]);
202 static int decode_frame(AVCodecContext
*avctx
,
203 void *data
, int *got_frame
,
206 SgiState
*s
= avctx
->priv_data
;
208 unsigned int dimension
, rle
;
210 uint8_t *out_buf
, *out_end
;
212 bytestream2_init(&s
->g
, avpkt
->data
, avpkt
->size
);
213 if (bytestream2_get_bytes_left(&s
->g
) < SGI_HEADER_SIZE
) {
214 av_log(avctx
, AV_LOG_ERROR
, "buf_size too small (%d)\n", avpkt
->size
);
215 return AVERROR_INVALIDDATA
;
218 /* Test for SGI magic. */
219 if (bytestream2_get_be16u(&s
->g
) != SGI_MAGIC
) {
220 av_log(avctx
, AV_LOG_ERROR
, "bad magic number\n");
221 return AVERROR_INVALIDDATA
;
224 rle
= bytestream2_get_byteu(&s
->g
);
225 s
->bytes_per_channel
= bytestream2_get_byteu(&s
->g
);
226 dimension
= bytestream2_get_be16u(&s
->g
);
227 s
->width
= bytestream2_get_be16u(&s
->g
);
228 s
->height
= bytestream2_get_be16u(&s
->g
);
229 s
->depth
= bytestream2_get_be16u(&s
->g
);
231 if (s
->bytes_per_channel
!= 1 && s
->bytes_per_channel
!= 2) {
232 av_log(avctx
, AV_LOG_ERROR
, "wrong channel number\n");
233 return AVERROR_INVALIDDATA
;
236 /* Check for supported image dimensions. */
237 if (dimension
!= 2 && dimension
!= 3) {
238 av_log(avctx
, AV_LOG_ERROR
, "wrong dimension number\n");
239 return AVERROR_INVALIDDATA
;
242 if (s
->depth
== SGI_GRAYSCALE
) {
243 avctx
->pix_fmt
= s
->bytes_per_channel
== 2 ? AV_PIX_FMT_GRAY16BE
: AV_PIX_FMT_GRAY8
;
244 } else if (s
->depth
== SGI_RGB
) {
245 avctx
->pix_fmt
= s
->bytes_per_channel
== 2 ? AV_PIX_FMT_RGB48BE
: AV_PIX_FMT_RGB24
;
246 } else if (s
->depth
== SGI_RGBA
) {
247 avctx
->pix_fmt
= s
->bytes_per_channel
== 2 ? AV_PIX_FMT_RGBA64BE
: AV_PIX_FMT_RGBA
;
249 av_log(avctx
, AV_LOG_ERROR
, "wrong picture format\n");
250 return AVERROR_INVALIDDATA
;
253 ret
= ff_set_dimensions(avctx
, s
->width
, s
->height
);
257 if ((ret
= ff_get_buffer(avctx
, p
, 0)) < 0)
260 p
->pict_type
= AV_PICTURE_TYPE_I
;
262 out_buf
= p
->data
[0];
264 out_end
= out_buf
+ p
->linesize
[0] * s
->height
;
266 s
->linesize
= p
->linesize
[0];
269 bytestream2_seek(&s
->g
, SGI_HEADER_SIZE
, SEEK_SET
);
271 ret
= read_rle_sgi(out_end
, s
);
273 ret
= read_uncompressed_sgi(out_buf
, s
);
282 static av_cold
int sgi_decode_init(AVCodecContext
*avctx
)
284 SgiState
*s
= avctx
->priv_data
;
291 AVCodec ff_sgi_decoder
= {
293 .long_name
= NULL_IF_CONFIG_SMALL("SGI image"),
294 .type
= AVMEDIA_TYPE_VIDEO
,
295 .id
= AV_CODEC_ID_SGI
,
296 .priv_data_size
= sizeof(SgiState
),
297 .decode
= decode_frame
,
298 .init
= sgi_decode_init
,
299 .capabilities
= CODEC_CAP_DR1
,