FFmpeg  4.4.9
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 "libavutil/imgutils.h"
23 #include "libavutil/avassert.h"
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27 #include "sgi.h"
28 
29 typedef struct SgiState {
31  unsigned int width;
32  int height;
33  unsigned int depth;
34  unsigned int bytes_per_channel;
35  int linesize;
37 } SgiState;
38 
39 /**
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.
46  */
47 static int expand_rle_row8(SgiState *s, uint8_t *out_buf,
48  int len, int pixelstride)
49 {
50  unsigned char pixel, count;
51  unsigned char *orig = out_buf;
52  uint8_t *out_end = out_buf + len;
53 
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))) {
59  break;
60  }
61 
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;
66  }
67 
68  if (pixel & 0x80) {
69  while (count--) {
70  *out_buf = bytestream2_get_byte(&s->g);
71  out_buf += pixelstride;
72  }
73  } else {
74  pixel = bytestream2_get_byte(&s->g);
75 
76  while (count--) {
77  *out_buf = pixel;
78  out_buf += pixelstride;
79  }
80  }
81  }
82  return (out_buf - orig) / pixelstride;
83 }
84 
85 static int expand_rle_row16(SgiState *s, uint16_t *out_buf,
86  int len, int pixelstride)
87 {
88  unsigned short pixel;
89  unsigned char count;
90  unsigned short *orig = out_buf;
91  uint16_t *out_end = out_buf + len;
92 
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)))
98  break;
99 
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;
104  }
105 
106  if (pixel & 0x80) {
107  while (count--) {
109  AV_WN16A(out_buf, pixel);
110  out_buf += pixelstride;
111  }
112  } else {
114 
115  while (count--) {
116  AV_WN16A(out_buf, pixel);
117  out_buf += pixelstride;
118  }
119  }
120  }
121  return (out_buf - orig) / pixelstride;
122 }
123 
124 
125 /**
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.
130  */
131 static int read_rle_sgi(uint8_t *last_line, SgiState *s)
132 {
133  uint8_t *dest_row;
134  unsigned int len = s->height * s->depth * 4;
135  GetByteContext g_table = s->g;
136  unsigned int start_offset;
137  int linesize, ret;
138 
139  /* size of RLE offset and length tables */
140  if (len * 2 > bytestream2_get_bytes_left(&s->g)) {
141  return AVERROR_INVALIDDATA;
142  }
143 
144  for (unsigned z = 0; z < s->depth; z++) {
145  dest_row = last_line;
146  for (int remaining_lines = s->height;;) {
147  linesize = s->width * s->depth;
148  start_offset = bytestream2_get_be32(&g_table);
149  bytestream2_seek(&s->g, start_offset, SEEK_SET);
150  if (s->bytes_per_channel == 1)
151  ret = expand_rle_row8(s, dest_row + z, linesize, s->depth);
152  else
153  ret = expand_rle_row16(s, (uint16_t *)dest_row + z, linesize, s->depth);
154  if (ret != s->width)
155  return AVERROR_INVALIDDATA;
156  if (--remaining_lines == 0)
157  break;
158  dest_row -= s->linesize;
159  }
160  }
161  return 0;
162 }
163 
164 /**
165  * Read an uncompressed SGI image.
166  * @param out_buf output buffer
167  * @param s the current image state
168  * @return 0 if read success, else return error code.
169  */
170 static int read_uncompressed_sgi(unsigned char *out_buf, SgiState *s)
171 {
172  int x, y, z;
173  unsigned int offset = s->height * s->width * s->bytes_per_channel;
174  GetByteContext gp[4];
175  uint8_t *out_end;
176 
177  /* Test buffer size. */
178  if (offset * s->depth > bytestream2_get_bytes_left(&s->g))
179  return AVERROR_INVALIDDATA;
180 
181  /* Create a reader for each plane */
182  for (z = 0; z < s->depth; z++) {
183  gp[z] = s->g;
184  bytestream2_skip(&gp[z], z * offset);
185  }
186 
187  for (y = s->height - 1; y >= 0; y--) {
188  out_end = out_buf + (y * s->linesize);
189  if (s->bytes_per_channel == 1) {
190  for (x = s->width; x > 0; x--)
191  for (z = 0; z < s->depth; z++)
192  *out_end++ = bytestream2_get_byteu(&gp[z]);
193  } else {
194  uint16_t *out16 = (uint16_t *)out_end;
195  for (x = s->width; x > 0; x--)
196  for (z = 0; z < s->depth; z++)
197  *out16++ = bytestream2_get_ne16u(&gp[z]);
198  }
199  }
200  return 0;
201 }
202 
203 static int decode_frame(AVCodecContext *avctx,
204  void *data, int *got_frame,
205  AVPacket *avpkt)
206 {
207  SgiState *s = avctx->priv_data;
208  AVFrame *p = data;
209  unsigned int dimension, rle;
210  int ret = 0;
211  uint8_t *out_buf, *last_line;
212 
213  bytestream2_init(&s->g, avpkt->data, avpkt->size);
215  av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
216  return AVERROR_INVALIDDATA;
217  }
218 
219  /* Test for SGI magic. */
220  if (bytestream2_get_be16u(&s->g) != SGI_MAGIC) {
221  av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
222  return AVERROR_INVALIDDATA;
223  }
224 
225  rle = bytestream2_get_byteu(&s->g);
226  s->bytes_per_channel = bytestream2_get_byteu(&s->g);
227  dimension = bytestream2_get_be16u(&s->g);
228  s->width = bytestream2_get_be16u(&s->g);
229  s->height = bytestream2_get_be16u(&s->g);
230  s->depth = bytestream2_get_be16u(&s->g);
231 
232  if (s->bytes_per_channel != 1 && s->bytes_per_channel != 2) {
233  av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
234  return AVERROR_INVALIDDATA;
235  }
236 
237  /* Check for supported image dimensions. */
238  if (dimension != 2 && dimension != 3) {
239  av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
240  return AVERROR_INVALIDDATA;
241  }
242 
243  if (s->depth == SGI_GRAYSCALE) {
244  avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_GRAY16BE : AV_PIX_FMT_GRAY8;
245  } else if (s->depth == SGI_RGB) {
246  avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_RGB48BE : AV_PIX_FMT_RGB24;
247  } else if (s->depth == SGI_RGBA) {
248  avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_RGBA64BE : AV_PIX_FMT_RGBA;
249  } else {
250  av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
251  return AVERROR_INVALIDDATA;
252  }
253 
254  ret = ff_set_dimensions(avctx, s->width, s->height);
255  if (ret < 0)
256  return ret;
257 
258  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
259  return ret;
260 
262  p->key_frame = 1;
263  out_buf = p->data[0];
264 
265  last_line = out_buf + p->linesize[0] * (s->height - 1);
266 
267  s->linesize = p->linesize[0];
268 
269  /* Skip header. */
270  bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET);
271  if (rle) {
272  ret = read_rle_sgi(last_line, s);
273  } else {
274  ret = read_uncompressed_sgi(out_buf, s);
275  }
276  if (ret)
277  return ret;
278 
279  *got_frame = 1;
280  return avpkt->size;
281 }
282 
284 {
285  SgiState *s = avctx->priv_data;
286 
287  s->avctx = avctx;
288 
289  return 0;
290 }
291 
293  .name = "sgi",
294  .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
295  .type = AVMEDIA_TYPE_VIDEO,
296  .id = AV_CODEC_ID_SGI,
297  .priv_data_size = sizeof(SgiState),
298  .decode = decode_frame,
300  .capabilities = AV_CODEC_CAP_DR1,
301  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
302 };
#define av_cold
Definition: attributes.h:88
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
static av_always_inline int bytestream2_get_bytes_left(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 void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
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 bytestream2_get_ne16u
Definition: bytestream.h:123
#define s(width, name)
Definition: cbs_vp9.c:257
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1911
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static int rle(uint8_t *dst, const uint8_t *src, int compressed_size, int uncompressed_size)
Definition: exr.c:218
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
@ AV_CODEC_ID_SGI
Definition: codec_id.h:150
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
misc image utilities
#define AV_WN16A(p, v)
Definition: intreadwrite.h:534
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:84
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
const char data[16]
Definition: mxf.c:142
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_RGB48BE
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition: pixfmt.h:102
@ AV_PIX_FMT_RGBA64BE
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
Definition: pixfmt.h:205
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
#define gp
Definition: regdef.h:62
#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_uncompressed_sgi(unsigned char *out_buf, SgiState *s)
Read an uncompressed SGI image.
Definition: sgidec.c:170
static int read_rle_sgi(uint8_t *last_line, SgiState *s)
Read a run length encoded SGI image.
Definition: sgidec.c:131
static int expand_rle_row16(SgiState *s, uint16_t *out_buf, int len, int pixelstride)
Definition: sgidec.c:85
static int expand_rle_row8(SgiState *s, uint8_t *out_buf, int len, int pixelstride)
Expand an RLE row into a channel.
Definition: sgidec.c:47
AVCodec ff_sgi_decoder
Definition: sgidec.c:292
static av_cold int sgi_decode_init(AVCodecContext *avctx)
Definition: sgidec.c:283
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: sgidec.c:203
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:396
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
unsigned int width
Definition: sgidec.c:31
GetByteContext g
Definition: sgidec.c:36
unsigned int bytes_per_channel
Definition: sgidec.c:34
int height
Definition: sgidec.c:32
unsigned int depth
Definition: sgidec.c:33
int linesize
Definition: sgidec.c:35
AVCodecContext * avctx
Definition: sgidec.c:30
#define av_log(a,...)
uint8_t pixel
Definition: tiny_ssim.c:42
static const uint8_t offset[127][2]
Definition: vf_spp.c:107
int len