FFmpeg
Loading...
Searching...
No Matches
sgidec.c
Go to the documentation of this file.
1/*
2 * SGI image decoder
3 * Todd Kirby <doubleshot@pacbell.net>
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22#include "avcodec.h"
23#include "bytestream.h"
24#include "codec_internal.h"
25#include "decode.h"
27#include "sgi.h"
28
29/**
30 * Expand an RLE row into a channel.
31 * @param logctx a logcontext
32 * @param out_buf Points to one line after the output buffer.
33 * @param g GetByteContext used to read input from
34 * @param width length of out_buf in nb of elements
35 * @return nb of elements written, else return error code.
36 */
37static int expand_rle_row8(void *logctx, uint8_t *out_buf,
38 GetByteContext *g, unsigned width)
39{
40 unsigned char pixel, count;
41 unsigned char *orig = out_buf;
42 uint8_t *out_end = out_buf + width;
43
44 while (out_buf < out_end) {
47 pixel = bytestream2_get_byteu(g);
48 if (!(count = (pixel & 0x7f))) {
49 break;
50 }
51
52 /* Check for buffer overflow. */
53 if (out_end - out_buf < count) {
54 av_log(logctx, AV_LOG_ERROR, "Invalid pixel count.\n");
56 }
57
58 if (pixel & 0x80) {
59 while (count--)
60 *out_buf++ = bytestream2_get_byte(g);
61 } else {
62 pixel = bytestream2_get_byte(g);
63
64 while (count--)
65 *out_buf++ = pixel;
66 }
67 }
68 return out_buf - orig;
69}
70
71static int expand_rle_row16(void *logctx, uint16_t *out_buf,
72 GetByteContext *g, unsigned width)
73{
74 unsigned short pixel;
75 unsigned char count;
76 unsigned short *orig = out_buf;
77 uint16_t *out_end = out_buf + width;
78
79 while (out_buf < out_end) {
82 pixel = bytestream2_get_be16u(g);
83 if (!(count = (pixel & 0x7f)))
84 break;
85
86 /* Check for buffer overflow. */
87 if (out_end - out_buf < count) {
88 av_log(logctx, AV_LOG_ERROR, "Invalid pixel count.\n");
90 }
91
92 if (pixel & 0x80) {
93 while (count--) {
95 AV_WN16A(out_buf, pixel);
96 out_buf++;
97 }
98 } else {
100
101 while (count--) {
102 AV_WN16A(out_buf, pixel);
103 out_buf++;
104 }
105 }
106 }
107 return out_buf - orig;
108}
109
110
111/**
112 * Read a run length encoded SGI image.
113 * @param out_buf output buffer
114 * @param s the current image state
115 * @return 0 if no error, else return error code.
116 */
117static int read_rle_sgi(void *logctx, uint8_t *out[4], ptrdiff_t stride[4],
118 GetByteContext *g, unsigned width, int height,
119 unsigned nb_components, unsigned bytes_per_channel)
120{
121 unsigned int len = height * nb_components * 4;
122 GetByteContext g_table = *g;
123 unsigned int start_offset;
124 int ret;
125
126 /* size of RLE offset and length tables */
127 if (len * 2 > bytestream2_get_bytes_left(g)) {
128 return AVERROR_INVALIDDATA;
129 }
130
131 for (unsigned z = 0; z < nb_components; z++) {
132 uint8_t *dest_row = out[z] + (height - 1) * stride[z];
133 while (1) {
134 start_offset = bytestream2_get_be32(&g_table);
135 bytestream2_seek(g, start_offset, SEEK_SET);
136 if (bytes_per_channel == 1)
137 ret = expand_rle_row8(logctx, dest_row, g, width);
138 else
139 ret = expand_rle_row16(logctx, (uint16_t *)dest_row, g, width);
140 if (ret != width)
141 return AVERROR_INVALIDDATA;
142 if (dest_row == out[z])
143 break;
144 dest_row -= stride[z];
145 }
146 }
147 return 0;
148}
149
150/**
151 * Read an uncompressed SGI image.
152 * @param out_buf output buffer
153 * @param s the current image state
154 * @return 0 if read success, else return error code.
155 */
156static int read_uncompressed_sgi(uint8_t *const out[4], const ptrdiff_t stride[4],
157 GetByteContext *g, unsigned width, int height,
158 unsigned nb_components, unsigned bytes_per_channel)
159{
160 unsigned rowsize = width * bytes_per_channel;
161
162 /* Test buffer size. */
163 if (rowsize * (int64_t)height * nb_components > bytestream2_get_bytes_left(g))
164 return AVERROR_INVALIDDATA;
165
166 for (unsigned z = 0; z < nb_components; z++) {
167 uint8_t *cur_row = out[z] + (height - 1) * stride[z];
168 while (1) {
169 bytestream2_get_bufferu(g, cur_row, rowsize);
170 if (cur_row == out[z])
171 break;
172 cur_row -= stride[z];
173 }
174 }
175 return 0;
176}
177
178static int decode_frame(AVCodecContext *avctx, AVFrame *p,
179 int *got_frame, AVPacket *avpkt)
180{
182 unsigned int bytes_per_channel, nb_components, dimension, rle, width;
183 uint8_t *out[4];
184 ptrdiff_t linesize[4];
185 int height;
186 int ret = 0;
187
188 bytestream2_init(&g, avpkt->data, avpkt->size);
190 av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
191 return AVERROR_INVALIDDATA;
192 }
193
194 /* Test for SGI magic. */
195 if (bytestream2_get_be16u(&g) != SGI_MAGIC) {
196 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
197 return AVERROR_INVALIDDATA;
198 }
199
200 rle = bytestream2_get_byteu(&g);
201 bytes_per_channel = bytestream2_get_byteu(&g);
202 dimension = bytestream2_get_be16u(&g);
203 width = bytestream2_get_be16u(&g);
204 height = bytestream2_get_be16u(&g);
205 nb_components = bytestream2_get_be16u(&g);
206
207 if (bytes_per_channel != 1 && bytes_per_channel != 2) {
208 av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
209 return AVERROR_INVALIDDATA;
210 }
211
212 /* Check for supported image dimensions. */
213 if (dimension != 2 && dimension != 3) {
214 av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
215 return AVERROR_INVALIDDATA;
216 }
217
218 if (nb_components == SGI_GRAYSCALE) {
219 avctx->pix_fmt = bytes_per_channel == 2 ? AV_PIX_FMT_GRAY16BE : AV_PIX_FMT_GRAY8;
220 } else if (nb_components == SGI_RGB) {
221 avctx->pix_fmt = bytes_per_channel == 2 ? AV_PIX_FMT_GBRP16BE : AV_PIX_FMT_GBRP;
222 } else if (nb_components == SGI_RGBA) {
223 avctx->pix_fmt = bytes_per_channel == 2 ? AV_PIX_FMT_GBRAP16BE : AV_PIX_FMT_GBRAP;
224 } else {
225 av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
226 return AVERROR_INVALIDDATA;
227 }
228
229 ret = ff_set_dimensions(avctx, width, height);
230 if (ret < 0)
231 return ret;
232
233 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
234 return ret;
235
236 switch (nb_components) {
237#define MAP(in_idx, out_idx) \
238 out[(in_idx)] = p->data[(out_idx)]; \
239 linesize[(in_idx)] = p->linesize[(out_idx)]
240 case SGI_GRAYSCALE:
241 MAP(0, 0);
242 break;
243 case SGI_RGBA:
244 MAP(3, 3);
246 case SGI_RGB:
247 MAP(0, 2);
248 MAP(1, 0);
249 MAP(2, 1);
250 break;
251 }
252
253 /* Skip header. */
255 if (rle) {
256 ret = read_rle_sgi(avctx, out, linesize, &g,
257 width, height, nb_components, bytes_per_channel);
258 } else {
259 ret = read_uncompressed_sgi(out, linesize, &g,
260 width, height, nb_components, bytes_per_channel);
261 }
262 if (ret)
263 return ret;
264
265 *got_frame = 1;
266 return avpkt->size;
267}
268
270 .p.name = "sgi",
271 CODEC_LONG_NAME("SGI image"),
272 .p.type = AVMEDIA_TYPE_VIDEO,
273 .p.id = AV_CODEC_ID_SGI,
275 .p.capabilities = AV_CODEC_CAP_DR1,
276};
const FFCodec ff_sgi_decoder
Definition sgidec.c:269
Libavcodec external API header.
#define pixel
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition bytestream.h:277
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition bytestream.h:212
#define bytestream2_get_ne16
Definition bytestream.h:119
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
long long int64_t
Definition coverity.c:34
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
static int rle(uint8_t *dst, const uint8_t *src, int compressed_size, int uncompressed_size)
Definition exr.c:217
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_SGI
Definition codec_id.h:151
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_WN16A(p, v)
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition 4xm.c:837
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition pixfmt.h:104
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_GBRAP16BE
planar GBRA 4:4:4:4 64bpp, big-endian
Definition pixfmt.h:213
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition pixfmt.h:212
@ AV_PIX_FMT_GBRP16BE
planar GBR 4:4:4 48bpp, big-endian
Definition pixfmt.h:171
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
#define SGI_MAGIC
SGI image file signature.
Definition sgi.h:28
#define SGI_RGB
Definition sgi.h:33
#define SGI_GRAYSCALE
Definition sgi.h:32
#define SGI_HEADER_SIZE
Definition sgi.h:30
#define SGI_RGBA
Definition sgi.h:34
static int read_rle_sgi(void *logctx, uint8_t *out[4], ptrdiff_t stride[4], GetByteContext *g, unsigned width, int height, unsigned nb_components, unsigned bytes_per_channel)
Read a run length encoded SGI image.
Definition sgidec.c:117
#define MAP(in_idx, out_idx)
static int read_uncompressed_sgi(uint8_t *const out[4], const ptrdiff_t stride[4], GetByteContext *g, unsigned width, int height, unsigned nb_components, unsigned bytes_per_channel)
Read an uncompressed SGI image.
Definition sgidec.c:156
static int expand_rle_row16(void *logctx, uint16_t *out_buf, GetByteContext *g, unsigned width)
Definition sgidec.c:71
static int expand_rle_row8(void *logctx, uint8_t *out_buf, GetByteContext *g, unsigned width)
Expand an RLE row into a channel.
Definition sgidec.c:37
static int decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition sgidec.c:178
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
#define stride
#define av_log(a,...)
static FILE * out
Definition movenc.c:55
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
const char * g
Definition vf_curves.c:128
int len