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 "sgi.h"
27 
28 /**
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.
35  */
36 static int expand_rle_row8(void *logctx, uint8_t *out_buf,
37  GetByteContext *g, unsigned width)
38 {
39  unsigned char pixel, count;
40  unsigned char *orig = out_buf;
41  uint8_t *out_end = out_buf + width;
42 
43  while (out_buf < out_end) {
45  return AVERROR_INVALIDDATA;
46  pixel = bytestream2_get_byteu(g);
47  if (!(count = (pixel & 0x7f))) {
48  break;
49  }
50 
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;
55  }
56 
57  if (pixel & 0x80) {
58  while (count--)
59  *out_buf++ = bytestream2_get_byte(g);
60  } else {
61  pixel = bytestream2_get_byte(g);
62 
63  while (count--)
64  *out_buf++ = pixel;
65  }
66  }
67  return out_buf - orig;
68 }
69 
70 static int expand_rle_row16(void *logctx, uint16_t *out_buf,
71  GetByteContext *g, unsigned width)
72 {
73  unsigned short pixel;
74  unsigned char count;
75  unsigned short *orig = out_buf;
76  uint16_t *out_end = out_buf + width;
77 
78  while (out_buf < out_end) {
80  return AVERROR_INVALIDDATA;
81  pixel = bytestream2_get_be16u(g);
82  if (!(count = (pixel & 0x7f)))
83  break;
84 
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;
89  }
90 
91  if (pixel & 0x80) {
92  while (count--) {
94  AV_WN16A(out_buf, pixel);
95  out_buf++;
96  }
97  } else {
99 
100  while (count--) {
101  AV_WN16A(out_buf, pixel);
102  out_buf++;
103  }
104  }
105  }
106  return out_buf - orig;
107 }
108 
109 
110 /**
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.
115  */
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)
119 {
120  unsigned int len = height * nb_components * 4;
121  GetByteContext g_table = *g;
122  unsigned int start_offset;
123  int ret;
124 
125  /* size of RLE offset and length tables */
126  if (len * 2 > bytestream2_get_bytes_left(g)) {
127  return AVERROR_INVALIDDATA;
128  }
129 
130  for (unsigned z = 0; z < nb_components; z++) {
131  uint8_t *dest_row = out[z] + (height - 1) * stride[z];
132  while (1) {
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);
137  else
138  ret = expand_rle_row16(logctx, (uint16_t *)dest_row, g, width);
139  if (ret != width)
140  return AVERROR_INVALIDDATA;
141  if (dest_row == out[z])
142  break;
143  dest_row -= stride[z];
144  }
145  }
146  return 0;
147 }
148 
149 /**
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.
154  */
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)
158 {
159  unsigned rowsize = width * bytes_per_channel;
160 
161  /* Test buffer size. */
162  if (rowsize * (int64_t)height > bytestream2_get_bytes_left(g))
163  return AVERROR_INVALIDDATA;
164 
165  for (unsigned z = 0; z < nb_components; z++) {
166  uint8_t *cur_row = out[z] + (height - 1) * stride[z];
167  while (1) {
168  bytestream2_get_bufferu(g, cur_row, rowsize);
169  if (cur_row == out[z])
170  break;
171  cur_row -= stride[z];
172  }
173  }
174  return 0;
175 }
176 
177 static int decode_frame(AVCodecContext *avctx, AVFrame *p,
178  int *got_frame, AVPacket *avpkt)
179 {
181  unsigned int bytes_per_channel, nb_components, dimension, rle, width;
182  uint8_t *out[4];
183  ptrdiff_t linesize[4];
184  int height;
185  int ret = 0;
186 
187  bytestream2_init(&g, avpkt->data, avpkt->size);
189  av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
190  return AVERROR_INVALIDDATA;
191  }
192 
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;
197  }
198 
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);
205 
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;
209  }
210 
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;
215  }
216 
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;
223  } else {
224  av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
225  return AVERROR_INVALIDDATA;
226  }
227 
228  ret = ff_set_dimensions(avctx, width, height);
229  if (ret < 0)
230  return ret;
231 
232  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
233  return ret;
234 
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)]
239  case SGI_GRAYSCALE:
240  MAP(0, 0);
241  break;
242  case SGI_RGBA:
243  MAP(3, 3);
244  /* fallthrough */
245  case SGI_RGB:
246  MAP(0, 2);
247  MAP(1, 0);
248  MAP(2, 1);
249  break;
250  }
252  p->key_frame = 1;
253 
254  /* Skip header. */
255  bytestream2_seek(&g, SGI_HEADER_SIZE, SEEK_SET);
256  if (rle) {
257  ret = read_rle_sgi(avctx, out, linesize, &g,
258  width, height, nb_components, bytes_per_channel);
259  } else {
260  ret = read_uncompressed_sgi(out, linesize, &g,
261  width, height, nb_components, bytes_per_channel);
262  }
263  if (ret)
264  return ret;
265 
266  *got_frame = 1;
267  return avpkt->size;
268 }
269 
271  .p.name = "sgi",
272  CODEC_LONG_NAME("SGI image"),
273  .p.type = AVMEDIA_TYPE_VIDEO,
274  .p.id = AV_CODEC_ID_SGI,
276  .p.capabilities = AV_CODEC_CAP_DR1,
277 };
out
FILE * out
Definition: movenc.c:54
GetByteContext
Definition: bytestream.h:33
AV_PIX_FMT_GBRP16BE
@ AV_PIX_FMT_GBRP16BE
planar GBR 4:4:4 48bpp, big-endian
Definition: pixfmt.h:164
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
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:116
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:36
AVPacket::data
uint8_t * data
Definition: packet.h:374
FFCodec
Definition: codec_internal.h:119
ff_set_dimensions
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:91
rle
static int rle(uint8_t *dst, const uint8_t *src, int compressed_size, int uncompressed_size)
Definition: exr.c:216
AV_PIX_FMT_GRAY16BE
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:123
AV_PIX_FMT_GBRAP
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:205
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:417
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AV_PIX_FMT_GBRAP16BE
@ AV_PIX_FMT_GBRAP16BE
planar GBRA 4:4:4:4 64bpp, big-endian
Definition: pixfmt.h:206
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:298
g
const char * g
Definition: vf_curves.c:127
ff_sgi_decoder
const FFCodec ff_sgi_decoder
Definition: sgidec.c:270
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:534
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:264
SGI_RGBA
#define SGI_RGBA
Definition: sgi.h:34
pixel
uint8_t pixel
Definition: tiny_ssim.c:41
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:422
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1420
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:375
codec_internal.h
bytestream2_get_ne16
#define bytestream2_get_ne16
Definition: bytestream.h:119
height
#define height
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:70
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:211
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:608
avcodec.h
stride
#define stride
Definition: h264pred_template.c:537
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:398
MAP
#define MAP(in_idx, out_idx)
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:158
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
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:351
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
dimension
The official guide to swscale for confused that consecutive non overlapping rectangles of dimension(0, slice_top) -(picture_width
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:177
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:155