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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "avcodec.h"
24 #include "bytestream.h"
25 #include "sgi.h"
26 
27 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 
46 static int expand_rle_row(SgiState *s, uint8_t *out_buf,
47  int len, int pixelstride)
48 {
49  unsigned char pixel, count;
50  unsigned char *orig = out_buf;
51 
52  while (1) {
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  return (out_buf - orig) / pixelstride;
58  }
59 
60  /* Check for buffer overflow. */
61  if (pixelstride * (count - 1) >= len) {
62  av_log(s->avctx, AV_LOG_ERROR, "Invalid pixel count.\n");
63  return AVERROR_INVALIDDATA;
64  }
65 
66  if (pixel & 0x80) {
67  while (count--) {
68  *out_buf = bytestream2_get_byte(&s->g);
69  out_buf += pixelstride;
70  }
71  } else {
72  pixel = bytestream2_get_byte(&s->g);
73 
74  while (count--) {
75  *out_buf = pixel;
76  out_buf += pixelstride;
77  }
78  }
79  }
80 }
81 
88 static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
89 {
90  uint8_t *dest_row;
91  unsigned int len = s->height * s->depth * 4;
92  GetByteContext g_table = s->g;
93  unsigned int y, z;
94  unsigned int start_offset;
95 
96  /* size of RLE offset and length tables */
97  if (len * 2 > bytestream2_get_bytes_left(&s->g)) {
98  return AVERROR_INVALIDDATA;
99  }
100 
101  for (z = 0; z < s->depth; z++) {
102  dest_row = out_buf;
103  for (y = 0; y < s->height; y++) {
104  dest_row -= s->linesize;
105  start_offset = bytestream2_get_be32(&g_table);
106  bytestream2_seek(&s->g, start_offset, SEEK_SET);
107  if (expand_rle_row(s, dest_row + z, FFABS(s->linesize) - z,
108  s->depth) != s->width) {
109  return AVERROR_INVALIDDATA;
110  }
111  }
112  }
113  return 0;
114 }
115 
123 static int read_uncompressed_sgi(unsigned char* out_buf, uint8_t* out_end,
124  SgiState *s)
125 {
126  int x, y, z;
127  unsigned int offset = s->height * s->width * s->bytes_per_channel;
128  GetByteContext gp[4];
129 
130  /* Test buffer size. */
131  if (offset * s->depth > bytestream2_get_bytes_left(&s->g))
132  return AVERROR_INVALIDDATA;
133 
134  /* Create a reader for each plane */
135  for (z = 0; z < s->depth; z++) {
136  gp[z] = s->g;
137  bytestream2_skip(&gp[z], z * offset);
138  }
139 
140  for (y = s->height - 1; y >= 0; y--) {
141  out_end = out_buf + (y * s->linesize);
142  if (s->bytes_per_channel == 1) {
143  for (x = s->width; x > 0; x--)
144  for (z = 0; z < s->depth; z++)
145  *out_end++ = bytestream2_get_byteu(&gp[z]);
146  } else {
147  uint16_t *out16 = (uint16_t *)out_end;
148  for (x = s->width; x > 0; x--)
149  for (z = 0; z < s->depth; z++)
150  *out16++ = bytestream2_get_ne16u(&gp[z]);
151  }
152  }
153  return 0;
154 }
155 
156 static int decode_frame(AVCodecContext *avctx,
157  void *data, int *data_size,
158  AVPacket *avpkt)
159 {
160  SgiState *s = avctx->priv_data;
161  AVFrame *picture = data;
162  AVFrame *p = &s->picture;
163  unsigned int dimension, rle;
164  int ret = 0;
165  uint8_t *out_buf, *out_end;
166 
167  bytestream2_init(&s->g, avpkt->data, avpkt->size);
169  av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
170  return AVERROR_INVALIDDATA;
171  }
172 
173  /* Test for SGI magic. */
174  if (bytestream2_get_be16(&s->g) != SGI_MAGIC) {
175  av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
176  return AVERROR_INVALIDDATA;
177  }
178 
179  rle = bytestream2_get_byte(&s->g);
180  s->bytes_per_channel = bytestream2_get_byte(&s->g);
181  dimension = bytestream2_get_be16(&s->g);
182  s->width = bytestream2_get_be16(&s->g);
183  s->height = bytestream2_get_be16(&s->g);
184  s->depth = bytestream2_get_be16(&s->g);
185 
186  if (s->bytes_per_channel != 1 && (s->bytes_per_channel != 2 || rle)) {
187  av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
188  return -1;
189  }
190 
191  /* Check for supported image dimensions. */
192  if (dimension != 2 && dimension != 3) {
193  av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
194  return -1;
195  }
196 
197  if (s->depth == SGI_GRAYSCALE) {
199  } else if (s->depth == SGI_RGB) {
201  } else if (s->depth == SGI_RGBA && s->bytes_per_channel == 1) {
202  avctx->pix_fmt = PIX_FMT_RGBA;
203  } else {
204  av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
205  return -1;
206  }
207 
208  if (av_image_check_size(s->width, s->height, 0, avctx))
209  return -1;
210  avcodec_set_dimensions(avctx, s->width, s->height);
211 
212  if (p->data[0])
213  avctx->release_buffer(avctx, p);
214 
215  p->reference = 0;
216  if (avctx->get_buffer(avctx, p) < 0) {
217  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed.\n");
218  return -1;
219  }
220 
222  p->key_frame = 1;
223  out_buf = p->data[0];
224 
225  out_end = out_buf + p->linesize[0] * s->height;
226 
227  s->linesize = p->linesize[0];
228 
229  /* Skip header. */
230  bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET);
231  if (rle) {
232  ret = read_rle_sgi(out_end, s);
233  } else {
234  ret = read_uncompressed_sgi(out_buf, out_end, s);
235  }
236 
237  if (ret == 0) {
238  *picture = s->picture;
239  *data_size = sizeof(AVPicture);
240  return avpkt->size;
241  } else {
242  return ret;
243  }
244 }
245 
246 static av_cold int sgi_init(AVCodecContext *avctx){
247  SgiState *s = avctx->priv_data;
248 
249  s->avctx = avctx;
250 
252  avctx->coded_frame = &s->picture;
253 
254  return 0;
255 }
256 
257 static av_cold int sgi_end(AVCodecContext *avctx)
258 {
259  SgiState * const s = avctx->priv_data;
260 
261  if (s->picture.data[0])
262  avctx->release_buffer(avctx, &s->picture);
263 
264  return 0;
265 }
266 
268  .name = "sgi",
269  .type = AVMEDIA_TYPE_VIDEO,
270  .id = CODEC_ID_SGI,
271  .priv_data_size = sizeof(SgiState),
272  .init = sgi_init,
273  .close = sgi_end,
274  .decode = decode_frame,
275  .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
276 };
277 
int(* get_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of each frame to get a buffer for it.
Definition: avcodec.h:1726
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int linesize
Definition: sgidec.c:34
Audio Video Frame.
Definition: avcodec.h:985
unsigned int width
Definition: sgidec.c:30
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:1737
misc image utilities
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2000
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
int size
Definition: avcodec.h:909
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:138
static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
Read a run length encoded SGI image.
Definition: sgidec.c:88
unsigned int height
Definition: sgidec.c:31
AVCodec.
Definition: avcodec.h:3189
unsigned int bytes_per_channel
Definition: sgidec.c:33
struct SgiState SgiState
static av_cold int sgi_init(AVCodecContext *avctx)
Definition: sgidec.c:246
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
AVFrame picture
Definition: sgidec.c:29
#define av_cold
Definition: attributes.h:71
const char data[16]
Definition: mxf.c:60
uint8_t * data
Definition: avcodec.h:908
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
Definition: sgidec.c:156
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:133
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
static int read_uncompressed_sgi(unsigned char *out_buf, uint8_t *out_end, SgiState *s)
Read an uncompressed SGI image.
Definition: sgidec.c:123
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1074
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:167
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:157
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:107
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
#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:213
unsigned int depth
Definition: sgidec.c:32
static AVFrame * picture
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1029
#define FFABS(a)
Definition: common.h:50
#define pixel
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1329
#define SGI_HEADER_SIZE
Definition: sgi.h:30
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
struct AVPicture AVPicture
four components are given, that's all.
AVCodecContext * avctx
Definition: sgidec.c:28
#define SGI_GRAYSCALE
Definition: sgi.h:32
#define SGI_RGB
Definition: sgi.h:33
static int expand_rle_row(SgiState *s, uint8_t *out_buf, int len, int pixelstride)
Expand an RLE row into a channel.
Definition: sgidec.c:46
GetByteContext g
Definition: sgidec.c:35
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
#define SGI_MAGIC
SGI image file signature.
Definition: sgi.h:28
#define bytestream2_get_ne16u
Definition: bytestream.h:124
void * priv_data
Definition: avcodec.h:1531
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:66
int len
AVCodec ff_sgi_decoder
Definition: sgidec.c:267
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1022
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:211
static av_cold int sgi_end(AVCodecContext *avctx)
Definition: sgidec.c:257
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
#define gp
Definition: regdef.h:62
void avcodec_get_frame_defaults(AVFrame *pic)
Set the fields of the given AVFrame to default values.
Definition: utils.c:609
Y , 8bpp.
Definition: pixfmt.h:72