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
23 #include "bytestream.h"
24 #include "codec_internal.h"
29 * Expand an RLE row into a channel.
30 * @param logctx a logcontext
31 * @param out_buf Points to one line after the output buffer.
32 * @param g GetByteContext used to read input from
33 * @param width length of out_buf in nb of elements
34 * @return nb of elements written, else return error code.
36 static int expand_rle_row8(void *logctx, uint8_t *out_buf,
37 GetByteContext *g, unsigned width)
39 unsigned char pixel, count;
40 unsigned char *orig = out_buf;
41 uint8_t *out_end = out_buf + width;
43 while (out_buf < out_end) {
44 if (bytestream2_get_bytes_left(g) < 1)
45 return AVERROR_INVALIDDATA;
46 pixel = bytestream2_get_byteu(g);
47 if (!(count = (pixel & 0x7f))) {
51 /* Check for buffer overflow. */
52 if (out_end - out_buf < count) {
53 av_log(logctx, AV_LOG_ERROR, "Invalid pixel count.\n");
54 return AVERROR_INVALIDDATA;
59 *out_buf++ = bytestream2_get_byte(g);
61 pixel = bytestream2_get_byte(g);
67 return out_buf - orig;
70 static int expand_rle_row16(void *logctx, uint16_t *out_buf,
71 GetByteContext *g, unsigned width)
75 unsigned short *orig = out_buf;
76 uint16_t *out_end = out_buf + width;
78 while (out_buf < out_end) {
79 if (bytestream2_get_bytes_left(g) < 2)
80 return AVERROR_INVALIDDATA;
81 pixel = bytestream2_get_be16u(g);
82 if (!(count = (pixel & 0x7f)))
85 /* Check for buffer overflow. */
86 if (out_end - out_buf < count) {
87 av_log(logctx, AV_LOG_ERROR, "Invalid pixel count.\n");
88 return AVERROR_INVALIDDATA;
93 pixel = bytestream2_get_ne16(g);
94 AV_WN16A(out_buf, pixel);
98 pixel = bytestream2_get_ne16(g);
101 AV_WN16A(out_buf, pixel);
106 return out_buf - orig;
111 * Read a run length encoded SGI image.
112 * @param out_buf output buffer
113 * @param s the current image state
114 * @return 0 if no error, else return error code.
116 static int read_rle_sgi(void *logctx, uint8_t *out[4], ptrdiff_t stride[4],
117 GetByteContext *g, unsigned width, int height,
118 unsigned nb_components, unsigned bytes_per_channel)
120 unsigned int len = height * nb_components * 4;
121 GetByteContext g_table = *g;
122 unsigned int start_offset;
125 /* size of RLE offset and length tables */
126 if (len * 2 > bytestream2_get_bytes_left(g)) {
127 return AVERROR_INVALIDDATA;
130 for (unsigned z = 0; z < nb_components; z++) {
131 uint8_t *dest_row = out[z] + (height - 1) * stride[z];
133 start_offset = bytestream2_get_be32(&g_table);
134 bytestream2_seek(g, start_offset, SEEK_SET);
135 if (bytes_per_channel == 1)
136 ret = expand_rle_row8(logctx, dest_row, g, width);
138 ret = expand_rle_row16(logctx, (uint16_t *)dest_row, g, width);
140 return AVERROR_INVALIDDATA;
141 if (dest_row == out[z])
143 dest_row -= stride[z];
150 * Read an uncompressed SGI image.
151 * @param out_buf output buffer
152 * @param s the current image state
153 * @return 0 if read success, else return error code.
155 static int read_uncompressed_sgi(uint8_t *const out[4], const ptrdiff_t stride[4],
156 GetByteContext *g, unsigned width, int height,
157 unsigned nb_components, unsigned bytes_per_channel)
159 unsigned rowsize = width * bytes_per_channel;
161 /* Test buffer size. */
162 if (rowsize * (int64_t)height * nb_components > bytestream2_get_bytes_left(g))
163 return AVERROR_INVALIDDATA;
165 for (unsigned z = 0; z < nb_components; z++) {
166 uint8_t *cur_row = out[z] + (height - 1) * stride[z];
168 bytestream2_get_bufferu(g, cur_row, rowsize);
169 if (cur_row == out[z])
171 cur_row -= stride[z];
177 static int decode_frame(AVCodecContext *avctx, AVFrame *p,
178 int *got_frame, AVPacket *avpkt)
181 unsigned int bytes_per_channel, nb_components, dimension, rle, width;
183 ptrdiff_t linesize[4];
187 bytestream2_init(&g, avpkt->data, avpkt->size);
188 if (bytestream2_get_bytes_left(&g) < SGI_HEADER_SIZE) {
189 av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
190 return AVERROR_INVALIDDATA;
193 /* Test for SGI magic. */
194 if (bytestream2_get_be16u(&g) != SGI_MAGIC) {
195 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
196 return AVERROR_INVALIDDATA;
199 rle = bytestream2_get_byteu(&g);
200 bytes_per_channel = bytestream2_get_byteu(&g);
201 dimension = bytestream2_get_be16u(&g);
202 width = bytestream2_get_be16u(&g);
203 height = bytestream2_get_be16u(&g);
204 nb_components = bytestream2_get_be16u(&g);
206 if (bytes_per_channel != 1 && bytes_per_channel != 2) {
207 av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
208 return AVERROR_INVALIDDATA;
211 /* Check for supported image dimensions. */
212 if (dimension != 2 && dimension != 3) {
213 av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
214 return AVERROR_INVALIDDATA;
217 if (nb_components == SGI_GRAYSCALE) {
218 avctx->pix_fmt = bytes_per_channel == 2 ? AV_PIX_FMT_GRAY16BE : AV_PIX_FMT_GRAY8;
219 } else if (nb_components == SGI_RGB) {
220 avctx->pix_fmt = bytes_per_channel == 2 ? AV_PIX_FMT_GBRP16BE : AV_PIX_FMT_GBRP;
221 } else if (nb_components == SGI_RGBA) {
222 avctx->pix_fmt = bytes_per_channel == 2 ? AV_PIX_FMT_GBRAP16BE : AV_PIX_FMT_GBRAP;
224 av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
225 return AVERROR_INVALIDDATA;
228 ret = ff_set_dimensions(avctx, width, height);
232 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
235 switch (nb_components) {
236 #define MAP(in_idx, out_idx) \
237 out[(in_idx)] = p->data[(out_idx)]; \
238 linesize[(in_idx)] = p->linesize[(out_idx)]
251 p->pict_type = AV_PICTURE_TYPE_I;
252 p->flags |= AV_FRAME_FLAG_KEY;
255 bytestream2_seek(&g, SGI_HEADER_SIZE, SEEK_SET);
257 ret = read_rle_sgi(avctx, out, linesize, &g,
258 width, height, nb_components, bytes_per_channel);
260 ret = read_uncompressed_sgi(out, linesize, &g,
261 width, height, nb_components, bytes_per_channel);
270 const FFCodec ff_sgi_decoder = {
272 CODEC_LONG_NAME("SGI image"),
273 .p.type = AVMEDIA_TYPE_VIDEO,
274 .p.id = AV_CODEC_ID_SGI,
275 FF_CODEC_DECODE_CB(decode_frame),
276 .p.capabilities = AV_CODEC_CAP_DR1,