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 "internal.h"
26 #include "sgi.h"
27 
28 typedef struct SgiState {
31  unsigned int width;
32  unsigned int height;
33  unsigned int depth;
34  unsigned int bytes_per_channel;
35  int linesize;
37 } SgiState;
38 
47 static int expand_rle_row(SgiState *s, uint8_t *out_buf,
48  int len, int pixelstride)
49 {
50  unsigned char pixel, count;
51  unsigned char *orig = out_buf;
52 
53  while (1) {
54  if (bytestream2_get_bytes_left(&s->g) < 1)
55  return AVERROR_INVALIDDATA;
56  pixel = bytestream2_get_byteu(&s->g);
57  if (!(count = (pixel & 0x7f))) {
58  return (out_buf - orig) / pixelstride;
59  }
60 
61  /* Check for buffer overflow. */
62  if (pixelstride * (count - 1) >= len) {
63  av_log(s->avctx, AV_LOG_ERROR, "Invalid pixel count.\n");
64  return AVERROR_INVALIDDATA;
65  }
66 
67  if (pixel & 0x80) {
68  while (count--) {
69  *out_buf = bytestream2_get_byte(&s->g);
70  out_buf += pixelstride;
71  }
72  } else {
73  pixel = bytestream2_get_byte(&s->g);
74 
75  while (count--) {
76  *out_buf = pixel;
77  out_buf += pixelstride;
78  }
79  }
80  }
81 }
82 
89 static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
90 {
91  uint8_t *dest_row;
92  unsigned int len = s->height * s->depth * 4;
93  GetByteContext g_table = s->g;
94  unsigned int y, z;
95  unsigned int start_offset;
96 
97  /* size of RLE offset and length tables */
98  if (len * 2 > bytestream2_get_bytes_left(&s->g)) {
99  return AVERROR_INVALIDDATA;
100  }
101 
102  for (z = 0; z < s->depth; z++) {
103  dest_row = out_buf;
104  for (y = 0; y < s->height; y++) {
105  dest_row -= s->linesize;
106  start_offset = bytestream2_get_be32(&g_table);
107  bytestream2_seek(&s->g, start_offset, SEEK_SET);
108  if (expand_rle_row(s, dest_row + z, FFABS(s->linesize) - z,
109  s->depth) != s->width) {
110  return AVERROR_INVALIDDATA;
111  }
112  }
113  }
114  return 0;
115 }
116 
124 static int read_uncompressed_sgi(unsigned char* out_buf, uint8_t* out_end,
125  SgiState *s)
126 {
127  int x, y, z;
128  unsigned int offset = s->height * s->width * s->bytes_per_channel;
129  GetByteContext gp[4];
130 
131  /* Test buffer size. */
132  if (offset * s->depth > bytestream2_get_bytes_left(&s->g))
133  return AVERROR_INVALIDDATA;
134 
135  /* Create a reader for each plane */
136  for (z = 0; z < s->depth; z++) {
137  gp[z] = s->g;
138  bytestream2_skip(&gp[z], z * offset);
139  }
140 
141  for (y = s->height - 1; y >= 0; y--) {
142  out_end = out_buf + (y * s->linesize);
143  if (s->bytes_per_channel == 1) {
144  for (x = s->width; x > 0; x--)
145  for (z = 0; z < s->depth; z++)
146  *out_end++ = bytestream2_get_byteu(&gp[z]);
147  } else {
148  uint16_t *out16 = (uint16_t *)out_end;
149  for (x = s->width; x > 0; x--)
150  for (z = 0; z < s->depth; z++)
151  *out16++ = bytestream2_get_ne16u(&gp[z]);
152  }
153  }
154  return 0;
155 }
156 
157 static int decode_frame(AVCodecContext *avctx,
158  void *data, int *got_frame,
159  AVPacket *avpkt)
160 {
161  SgiState *s = avctx->priv_data;
162  AVFrame *picture = data;
163  AVFrame *p = &s->picture;
164  unsigned int dimension, rle;
165  int ret = 0;
166  uint8_t *out_buf, *out_end;
167 
168  bytestream2_init(&s->g, avpkt->data, avpkt->size);
170  av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
171  return AVERROR_INVALIDDATA;
172  }
173 
174  /* Test for SGI magic. */
175  if (bytestream2_get_be16(&s->g) != SGI_MAGIC) {
176  av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
177  return AVERROR_INVALIDDATA;
178  }
179 
180  rle = bytestream2_get_byte(&s->g);
181  s->bytes_per_channel = bytestream2_get_byte(&s->g);
182  dimension = bytestream2_get_be16(&s->g);
183  s->width = bytestream2_get_be16(&s->g);
184  s->height = bytestream2_get_be16(&s->g);
185  s->depth = bytestream2_get_be16(&s->g);
186 
187  if (s->bytes_per_channel != 1 && (s->bytes_per_channel != 2 || rle)) {
188  av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
189  return -1;
190  }
191 
192  /* Check for supported image dimensions. */
193  if (dimension != 2 && dimension != 3) {
194  av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
195  return -1;
196  }
197 
198  if (s->depth == SGI_GRAYSCALE) {
200  } else if (s->depth == SGI_RGB) {
202  } else if (s->depth == SGI_RGBA && s->bytes_per_channel == 1) {
203  avctx->pix_fmt = AV_PIX_FMT_RGBA;
204  } else {
205  av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
206  return -1;
207  }
208 
209  if (av_image_check_size(s->width, s->height, 0, avctx))
210  return -1;
211  avcodec_set_dimensions(avctx, s->width, s->height);
212 
213  if (p->data[0])
214  avctx->release_buffer(avctx, p);
215 
216  p->reference = 0;
217  if (ff_get_buffer(avctx, p) < 0) {
218  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed.\n");
219  return -1;
220  }
221 
223  p->key_frame = 1;
224  out_buf = p->data[0];
225 
226  out_end = out_buf + p->linesize[0] * s->height;
227 
228  s->linesize = p->linesize[0];
229 
230  /* Skip header. */
231  bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET);
232  if (rle) {
233  ret = read_rle_sgi(out_end, s);
234  } else {
235  ret = read_uncompressed_sgi(out_buf, out_end, s);
236  }
237 
238  if (ret == 0) {
239  *picture = s->picture;
240  *got_frame = 1;
241  return avpkt->size;
242  } else {
243  return ret;
244  }
245 }
246 
247 static av_cold int sgi_init(AVCodecContext *avctx){
248  SgiState *s = avctx->priv_data;
249 
250  s->avctx = avctx;
251 
253  avctx->coded_frame = &s->picture;
254 
255  return 0;
256 }
257 
258 static av_cold int sgi_end(AVCodecContext *avctx)
259 {
260  SgiState * const s = avctx->priv_data;
261 
262  if (s->picture.data[0])
263  avctx->release_buffer(avctx, &s->picture);
264 
265  return 0;
266 }
267 
269  .name = "sgi",
270  .type = AVMEDIA_TYPE_VIDEO,
271  .id = AV_CODEC_ID_SGI,
272  .priv_data_size = sizeof(SgiState),
273  .init = sgi_init,
274  .close = sgi_end,
275  .decode = decode_frame,
276  .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
277  .capabilities = CODEC_CAP_DR1,
278 };
int linesize
Definition: sgidec.c:35
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
unsigned int width
Definition: sgidec.c:31
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
misc image utilities
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:149
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
Read a run length encoded SGI image.
Definition: sgidec.c:89
unsigned int height
Definition: sgidec.c:32
AVCodec.
Definition: avcodec.h:2960
unsigned int bytes_per_channel
Definition: sgidec.c:34
struct SgiState SgiState
static av_cold int sgi_init(AVCodecContext *avctx)
Definition: sgidec.c:247
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
AVFrame picture
Definition: sgidec.c:30
uint8_t
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
static int read_uncompressed_sgi(unsigned char *out_buf, uint8_t *out_end, SgiState *s)
Read an uncompressed SGI image.
Definition: sgidec.c:124
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:1132
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:159
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:149
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
#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:220
unsigned int depth
Definition: sgidec.c:33
static AVFrame * picture
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
#define pixel
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: sgidec.c:157
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:1339
#define SGI_HEADER_SIZE
Definition: sgi.h:30
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:604
Y , 16bpp, big-endian.
Definition: pixfmt.h:98
AVCodecContext * avctx
Definition: sgidec.c:29
#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:47
GetByteContext g
Definition: sgidec.c:36
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:116
Y , 8bpp.
Definition: pixfmt.h:73
common internal api header.
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:108
void * priv_data
Definition: avcodec.h:1382
int len
AVCodec ff_sgi_decoder
Definition: sgidec.c:268
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:203
static av_cold int sgi_end(AVCodecContext *avctx)
Definition: sgidec.c:258
#define gp
Definition: regdef.h:62
This structure stores compressed data.
Definition: avcodec.h:898