FFmpeg
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"
26 #include "libavutil/attributes.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  */
37 static 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) {
46  return AVERROR_INVALIDDATA;
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");
55  return AVERROR_INVALIDDATA;
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 
71 static 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) {
81  return AVERROR_INVALIDDATA;
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");
89  return AVERROR_INVALIDDATA;
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  */
117 static 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  */
156 static 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 
178 static 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. */
254  bytestream2_seek(&g, SGI_HEADER_SIZE, SEEK_SET);
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 };
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition: bytestream.h:158
out
static FILE * out
Definition: movenc.c:55
GetByteContext
Definition: bytestream.h:33
AV_PIX_FMT_GBRP16BE
@ AV_PIX_FMT_GBRP16BE
planar GBR 4:4:4 48bpp, big-endian
Definition: pixfmt.h:171
int64_t
long long int64_t
Definition: coverity.c:34
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:435
bytestream2_seek
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:212
read_rle_sgi
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
expand_rle_row8
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
AVPacket::data
uint8_t * data
Definition: packet.h:595
FFCodec
Definition: codec_internal.h:127
ff_set_dimensions
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:91
rle
static int rle(uint8_t *dst, const uint8_t *src, int compressed_size, int uncompressed_size)
Definition: exr.c:225
AV_PIX_FMT_GRAY16BE
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:104
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AV_PIX_FMT_GBRAP
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:212
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
AV_PIX_FMT_GBRAP16BE
@ AV_PIX_FMT_GBRAP16BE
planar GBRA 4:4:4:4 64bpp, big-endian
Definition: pixfmt.h:213
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:347
g
const char * g
Definition: vf_curves.c:128
ff_sgi_decoder
const FFCodec ff_sgi_decoder
Definition: sgidec.c:269
SGI_MAGIC
#define SGI_MAGIC
SGI image file signature.
Definition: sgi.h:28
decode.h
AV_WN16A
#define AV_WN16A(p, v)
Definition: intreadwrite.h:530
av_fallthrough
#define av_fallthrough
Definition: attributes.h:67
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:332
SGI_RGBA
#define SGI_RGBA
Definition: sgi.h:34
pixel
uint8_t pixel
Definition: tiny_ssim.c:41
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1765
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:596
height
#define height
Definition: dsp.h:89
codec_internal.h
bytestream2_get_ne16
#define bytestream2_get_ne16
Definition: bytestream.h:119
attributes.h
sgi.h
SGI_RGB
#define SGI_RGB
Definition: sgi.h:33
expand_rle_row16
static int expand_rle_row16(void *logctx, uint16_t *out_buf, GetByteContext *g, unsigned width)
Definition: sgidec.c:71
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
len
int len
Definition: vorbis_enc_data.h:426
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:639
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:439
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
MAP
#define MAP(in_idx, out_idx)
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:165
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
bytestream2_get_bufferu
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:277
SGI_HEADER_SIZE
#define SGI_HEADER_SIZE
Definition: sgi.h:30
AVPacket
This structure stores compressed data.
Definition: packet.h:572
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_CODEC_ID_SGI
@ AV_CODEC_ID_SGI
Definition: codec_id.h:153
stride
#define stride
Definition: h264pred_template.c:536
dimension
The official guide to swscale for confused that consecutive non overlapping rectangles of dimension(0, slice_top) -(picture_width
width
#define width
Definition: dsp.h:89
SGI_GRAYSCALE
#define SGI_GRAYSCALE
Definition: sgi.h:32
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition: sgidec.c:178
read_uncompressed_sgi
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