FFmpeg  2.1.1
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 {
30  unsigned int width;
31  unsigned int height;
32  unsigned int depth;
33  unsigned int bytes_per_channel;
34  int linesize;
36 } SgiState;
37 
38 /**
39  * Expand an RLE row into a channel.
40  * @param s the current image state
41  * @param out_buf Points to one line after the output buffer.
42  * @param out_end end of line in output buffer
43  * @param pixelstride pixel stride of input buffer
44  * @return size of output in bytes, else return error code.
45  */
46 static int expand_rle_row(SgiState *s, uint8_t *out_buf,
47  uint8_t *out_end, int pixelstride)
48 {
49  unsigned char pixel, count;
50  unsigned char *orig = out_buf;
51 
52  while (out_buf < out_end) {
53  if (bytestream2_get_bytes_left(&s->g) < 1)
54  return AVERROR_INVALIDDATA;
55  pixel = bytestream2_get_byteu(&s->g);
56  if (!(count = (pixel & 0x7f))) {
57  break;
58  }
59 
60  /* Check for buffer overflow. */
61  if (out_end - out_buf <= pixelstride * (count - 1))
62  return AVERROR_INVALIDDATA;
63 
64  if (pixel & 0x80) {
65  while (count--) {
66  *out_buf = bytestream2_get_byte(&s->g);
67  out_buf += pixelstride;
68  }
69  } else {
70  pixel = bytestream2_get_byte(&s->g);
71 
72  while (count--) {
73  *out_buf = pixel;
74  out_buf += pixelstride;
75  }
76  }
77  }
78  return (out_buf - orig) / pixelstride;
79 }
80 
81 /**
82  * Read a run length encoded SGI image.
83  * @param out_buf output buffer
84  * @param s the current image state
85  * @return 0 if no error, else return error code.
86  */
87 static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
88 {
89  uint8_t *dest_row;
90  unsigned int len = s->height * s->depth * 4;
91  GetByteContext g_table = s->g;
92  unsigned int y, z;
93  unsigned int start_offset;
94 
95  /* size of RLE offset and length tables */
96  if (len * 2 > bytestream2_get_bytes_left(&s->g)) {
97  return AVERROR_INVALIDDATA;
98  }
99 
100  for (z = 0; z < s->depth; z++) {
101  dest_row = out_buf;
102  for (y = 0; y < s->height; y++) {
103  dest_row -= s->linesize;
104  start_offset = bytestream2_get_be32(&g_table);
105  bytestream2_seek(&s->g, start_offset, SEEK_SET);
106  if (expand_rle_row(s, dest_row + z, dest_row + s->width*s->depth,
107  s->depth) != s->width) {
108  return AVERROR_INVALIDDATA;
109  }
110  }
111  }
112  return 0;
113 }
114 
115 /**
116  * Read an uncompressed SGI image.
117  * @param out_buf output buffer
118  * @param s the current image state
119  * @return 0 if read success, else return error code.
120  */
121 static int read_uncompressed_sgi(unsigned char* out_buf, SgiState *s)
122 {
123  int x, y, z;
124  unsigned int offset = s->height * s->width * s->bytes_per_channel;
125  GetByteContext gp[4];
126  uint8_t *out_end;
127 
128  /* Test buffer size. */
129  if (offset * s->depth > bytestream2_get_bytes_left(&s->g))
130  return AVERROR_INVALIDDATA;
131 
132  /* Create a reader for each plane */
133  for (z = 0; z < s->depth; z++) {
134  gp[z] = s->g;
135  bytestream2_skip(&gp[z], z * offset);
136  }
137 
138  for (y = s->height - 1; y >= 0; y--) {
139  out_end = out_buf + (y * s->linesize);
140  if (s->bytes_per_channel == 1) {
141  for (x = s->width; x > 0; x--)
142  for (z = 0; z < s->depth; z++)
143  *out_end++ = bytestream2_get_byteu(&gp[z]);
144  } else {
145  uint16_t *out16 = (uint16_t *)out_end;
146  for (x = s->width; x > 0; x--)
147  for (z = 0; z < s->depth; z++)
148  *out16++ = bytestream2_get_ne16u(&gp[z]);
149  }
150  }
151  return 0;
152 }
153 
154 static int decode_frame(AVCodecContext *avctx,
155  void *data, int *got_frame,
156  AVPacket *avpkt)
157 {
158  SgiState *s = avctx->priv_data;
159  AVFrame *p = data;
160  unsigned int dimension, rle;
161  int ret = 0;
162  uint8_t *out_buf, *out_end;
163 
164  bytestream2_init(&s->g, avpkt->data, avpkt->size);
166  av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
167  return AVERROR_INVALIDDATA;
168  }
169 
170  /* Test for SGI magic. */
171  if (bytestream2_get_be16u(&s->g) != SGI_MAGIC) {
172  av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
173  return AVERROR_INVALIDDATA;
174  }
175 
176  rle = bytestream2_get_byteu(&s->g);
177  s->bytes_per_channel = bytestream2_get_byteu(&s->g);
178  dimension = bytestream2_get_be16u(&s->g);
179  s->width = bytestream2_get_be16u(&s->g);
180  s->height = bytestream2_get_be16u(&s->g);
181  s->depth = bytestream2_get_be16u(&s->g);
182 
183  if (s->bytes_per_channel != 1 && (s->bytes_per_channel != 2 || rle)) {
184  av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
185  return AVERROR_INVALIDDATA;
186  }
187 
188  /* Check for supported image dimensions. */
189  if (dimension != 2 && dimension != 3) {
190  av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
191  return AVERROR_INVALIDDATA;
192  }
193 
194  if (s->depth == SGI_GRAYSCALE) {
196  } else if (s->depth == SGI_RGB) {
198  } else if (s->depth == SGI_RGBA) {
200  } else {
201  av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
202  return AVERROR_INVALIDDATA;
203  }
204 
205  if (av_image_check_size(s->width, s->height, 0, avctx))
206  return AVERROR_INVALIDDATA;
207  avcodec_set_dimensions(avctx, s->width, s->height);
208 
209  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
210  return ret;
211 
213  p->key_frame = 1;
214  out_buf = p->data[0];
215 
216  out_end = out_buf + p->linesize[0] * s->height;
217 
218  s->linesize = p->linesize[0];
219 
220  /* Skip header. */
221  bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET);
222  if (rle) {
223  ret = read_rle_sgi(out_end, s);
224  } else {
225  ret = read_uncompressed_sgi(out_buf, s);
226  }
227  if (ret)
228  return ret;
229 
230  *got_frame = 1;
231  return avpkt->size;
232 }
233 
235  .name = "sgi",
236  .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
237  .type = AVMEDIA_TYPE_VIDEO,
238  .id = AV_CODEC_ID_SGI,
239  .priv_data_size = sizeof(SgiState),
240  .decode = decode_frame,
241  .capabilities = CODEC_CAP_DR1,
242 };
static int expand_rle_row(SgiState *s, uint8_t *out_buf, uint8_t *out_end, int pixelstride)
Expand an RLE row into a channel.
Definition: sgidec.c:46
const char * s
Definition: avisynth_c.h:668
int linesize
Definition: sgidec.c:34
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
unsigned int width
Definition: sgidec.c:30
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: avcodec.h:4536
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:233
int size
Definition: avcodec.h:1064
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:131
static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
Read a run length encoded SGI image.
Definition: sgidec.c:87
unsigned int height
Definition: sgidec.c:31
AVCodec.
Definition: avcodec.h:2922
Y , 8bpp.
Definition: avcodec.h:4542
unsigned int bytes_per_channel
Definition: sgidec.c:33
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition: avcodec.h:4579
uint8_t
static const uint8_t offset[511][2]
Definition: vf_uspp.c:58
const char * name
Name of the codec implementation.
Definition: avcodec.h:2929
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:742
const char data[16]
Definition: mxf.c:68
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:162
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:152
Libavcodec external API header.
#define SGI_RGBA
Definition: sgi.h:34
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:231
unsigned int depth
Definition: sgidec.c:32
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:167
float y
ret
Definition: avfilter.c:961
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: sgidec.c:154
main external API structure.
Definition: avcodec.h:1146
#define SGI_HEADER_SIZE
Definition: sgi.h:30
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:941
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: avcodec.h:4684
uint8_t * data
Definition: avcodec.h:1063
#define SGI_GRAYSCALE
Definition: sgi.h:32
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
#define SGI_RGB
Definition: sgi.h:33
uint8_t pixel
Definition: tiny_ssim.c:40
void * priv_data
Definition: avcodec.h:1182
GetByteContext g
Definition: sgidec.c:35
static int read_uncompressed_sgi(unsigned char *out_buf, SgiState *s)
Read an uncompressed SGI image.
Definition: sgidec.c:121
#define SGI_MAGIC
SGI image file signature.
Definition: sgi.h:28
#define bytestream2_get_ne16u
Definition: bytestream.h:117
common internal api header.
#define AVERROR_INVALIDDATA
int len
AVCodec ff_sgi_decoder
Definition: sgidec.c:234
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: avcodec.h:4563
int key_frame
1 -&gt; keyframe, 0-&gt; not
Definition: frame.h:162
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:206
void INT64 INT64 count
Definition: avisynth_c.h:594
#define gp
Definition: regdef.h:62
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
This structure stores compressed data.
Definition: avcodec.h:1040
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
Y , 16bpp, big-endian.
Definition: avcodec.h:4567