FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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, -1 if buffer overflows
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_buf + pixelstride * (count-1) >= out_end) return -1;
62 
63  if (pixel & 0x80) {
64  while (count--) {
65  *out_buf = bytestream2_get_byte(&s->g);
66  out_buf += pixelstride;
67  }
68  } else {
69  pixel = bytestream2_get_byte(&s->g);
70 
71  while (count--) {
72  *out_buf = pixel;
73  out_buf += pixelstride;
74  }
75  }
76  }
77  return (out_buf - orig) / pixelstride;
78 }
79 
80 /**
81  * Read a run length encoded SGI image.
82  * @param out_buf output buffer
83  * @param s the current image state
84  * @return 0 if no error, else return error number.
85  */
86 static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
87 {
88  uint8_t *dest_row;
89  unsigned int len = s->height * s->depth * 4;
90  GetByteContext g_table = s->g;
91  unsigned int y, z;
92  unsigned int start_offset;
93 
94  /* size of RLE offset and length tables */
95  if (len * 2 > bytestream2_get_bytes_left(&s->g)) {
96  return AVERROR_INVALIDDATA;
97  }
98 
99  for (z = 0; z < s->depth; z++) {
100  dest_row = out_buf;
101  for (y = 0; y < s->height; y++) {
102  dest_row -= s->linesize;
103  start_offset = bytestream2_get_be32(&g_table);
104  bytestream2_seek(&s->g, start_offset, SEEK_SET);
105  if (expand_rle_row(s, dest_row + z, dest_row + s->width*s->depth,
106  s->depth) != s->width) {
107  return AVERROR_INVALIDDATA;
108  }
109  }
110  }
111  return 0;
112 }
113 
114 /**
115  * Read an uncompressed SGI image.
116  * @param out_buf output buffer
117  * @param s the current image state
118  * @return 0 if read success, otherwise return -1.
119  */
120 static int read_uncompressed_sgi(unsigned char* out_buf, SgiState *s)
121 {
122  int x, y, z;
123  unsigned int offset = s->height * s->width * s->bytes_per_channel;
124  GetByteContext gp[4];
125  uint8_t *out_end;
126 
127  /* Test buffer size. */
128  if (offset * s->depth > bytestream2_get_bytes_left(&s->g))
129  return AVERROR_INVALIDDATA;
130 
131  /* Create a reader for each plane */
132  for (z = 0; z < s->depth; z++) {
133  gp[z] = s->g;
134  bytestream2_skip(&gp[z], z * offset);
135  }
136 
137  for (y = s->height - 1; y >= 0; y--) {
138  out_end = out_buf + (y * s->linesize);
139  if (s->bytes_per_channel == 1) {
140  for (x = s->width; x > 0; x--)
141  for (z = 0; z < s->depth; z++)
142  *out_end++ = bytestream2_get_byteu(&gp[z]);
143  } else {
144  uint16_t *out16 = (uint16_t *)out_end;
145  for (x = s->width; x > 0; x--)
146  for (z = 0; z < s->depth; z++)
147  *out16++ = bytestream2_get_ne16u(&gp[z]);
148  }
149  }
150  return 0;
151 }
152 
153 static int decode_frame(AVCodecContext *avctx,
154  void *data, int *got_frame,
155  AVPacket *avpkt)
156 {
157  SgiState *s = avctx->priv_data;
158  AVFrame *p = data;
159  unsigned int dimension, rle;
160  int ret = 0;
161  uint8_t *out_buf, *out_end;
162 
163  bytestream2_init(&s->g, avpkt->data, avpkt->size);
165  av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
166  return AVERROR_INVALIDDATA;
167  }
168 
169  /* Test for SGI magic. */
170  if (bytestream2_get_be16u(&s->g) != SGI_MAGIC) {
171  av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
172  return AVERROR_INVALIDDATA;
173  }
174 
175  rle = bytestream2_get_byteu(&s->g);
176  s->bytes_per_channel = bytestream2_get_byteu(&s->g);
177  dimension = bytestream2_get_be16u(&s->g);
178  s->width = bytestream2_get_be16u(&s->g);
179  s->height = bytestream2_get_be16u(&s->g);
180  s->depth = bytestream2_get_be16u(&s->g);
181 
182  if (s->bytes_per_channel != 1 && (s->bytes_per_channel != 2 || rle)) {
183  av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
184  return -1;
185  }
186 
187  /* Check for supported image dimensions. */
188  if (dimension != 2 && dimension != 3) {
189  av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
190  return -1;
191  }
192 
193  if (s->depth == SGI_GRAYSCALE) {
195  } else if (s->depth == SGI_RGB) {
197  } else if (s->depth == SGI_RGBA) {
199  } else {
200  av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
201  return -1;
202  }
203 
204  if (av_image_check_size(s->width, s->height, 0, avctx))
205  return -1;
206  avcodec_set_dimensions(avctx, s->width, s->height);
207 
208  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
209  return ret;
210 
212  p->key_frame = 1;
213  out_buf = p->data[0];
214 
215  out_end = out_buf + p->linesize[0] * s->height;
216 
217  s->linesize = p->linesize[0];
218 
219  /* Skip header. */
220  bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET);
221  if (rle) {
222  ret = read_rle_sgi(out_end, s);
223  } else {
224  ret = read_uncompressed_sgi(out_buf, s);
225  }
226 
227  if (ret == 0) {
228  *got_frame = 1;
229  return avpkt->size;
230  } else {
231  return ret;
232  }
233 }
234 
236  .name = "sgi",
237  .type = AVMEDIA_TYPE_VIDEO,
238  .id = AV_CODEC_ID_SGI,
239  .priv_data_size = sizeof(SgiState),
240  .decode = decode_frame,
241  .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
242  .capabilities = CODEC_CAP_DR1,
243 };