FFmpeg
 All Data Structures 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 {
31  unsigned int width;
32  unsigned int height;
33  unsigned int depth;
34  unsigned int bytes_per_channel;
35  int linesize;
37 } SgiState;
38 
39 /**
40  * Expand an RLE row into a channel.
41  * @param s the current image state
42  * @param out_buf Points to one line after the output buffer.
43  * @param out_end end of line in output buffer
44  * @param pixelstride pixel stride of input buffer
45  * @return size of output in bytes, -1 if buffer overflows
46  */
47 static int expand_rle_row(SgiState *s, uint8_t *out_buf,
48  uint8_t *out_end, int pixelstride)
49 {
50  unsigned char pixel, count;
51  unsigned char *orig = out_buf;
52 
53  while (out_buf < out_end) {
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  break;
59  }
60 
61  /* Check for buffer overflow. */
62  if(out_buf + pixelstride * (count-1) >= out_end) return -1;
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 number.
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, otherwise return -1.
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 *picture = data;
160  AVFrame *p = &s->picture;
161  unsigned int dimension, rle;
162  int ret = 0;
163  uint8_t *out_buf, *out_end;
164 
165  bytestream2_init(&s->g, avpkt->data, avpkt->size);
167  av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
168  return AVERROR_INVALIDDATA;
169  }
170 
171  /* Test for SGI magic. */
172  if (bytestream2_get_be16u(&s->g) != SGI_MAGIC) {
173  av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
174  return AVERROR_INVALIDDATA;
175  }
176 
177  rle = bytestream2_get_byteu(&s->g);
178  s->bytes_per_channel = bytestream2_get_byteu(&s->g);
179  dimension = bytestream2_get_be16u(&s->g);
180  s->width = bytestream2_get_be16u(&s->g);
181  s->height = bytestream2_get_be16u(&s->g);
182  s->depth = bytestream2_get_be16u(&s->g);
183 
184  if (s->bytes_per_channel != 1 && (s->bytes_per_channel != 2 || rle)) {
185  av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
186  return -1;
187  }
188 
189  /* Check for supported image dimensions. */
190  if (dimension != 2 && dimension != 3) {
191  av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
192  return -1;
193  }
194 
195  if (s->depth == SGI_GRAYSCALE) {
197  } else if (s->depth == SGI_RGB) {
199  } else if (s->depth == SGI_RGBA) {
201  } else {
202  av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
203  return -1;
204  }
205 
206  if (av_image_check_size(s->width, s->height, 0, avctx))
207  return -1;
208  avcodec_set_dimensions(avctx, s->width, s->height);
209 
210  if (p->data[0])
211  avctx->release_buffer(avctx, p);
212 
213  p->reference = 0;
214  if (ff_get_buffer(avctx, p) < 0) {
215  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed.\n");
216  return -1;
217  }
218 
220  p->key_frame = 1;
221  out_buf = p->data[0];
222 
223  out_end = out_buf + p->linesize[0] * s->height;
224 
225  s->linesize = p->linesize[0];
226 
227  /* Skip header. */
228  bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET);
229  if (rle) {
230  ret = read_rle_sgi(out_end, s);
231  } else {
232  ret = read_uncompressed_sgi(out_buf, s);
233  }
234 
235  if (ret == 0) {
236  *picture = s->picture;
237  *got_frame = 1;
238  return avpkt->size;
239  } else {
240  return ret;
241  }
242 }
243 
244 static av_cold int sgi_init(AVCodecContext *avctx){
245  SgiState *s = avctx->priv_data;
246 
248  avctx->coded_frame = &s->picture;
249 
250  return 0;
251 }
252 
253 static av_cold int sgi_end(AVCodecContext *avctx)
254 {
255  SgiState * const s = avctx->priv_data;
256 
257  if (s->picture.data[0])
258  avctx->release_buffer(avctx, &s->picture);
259 
260  return 0;
261 }
262 
264  .name = "sgi",
265  .type = AVMEDIA_TYPE_VIDEO,
266  .id = AV_CODEC_ID_SGI,
267  .priv_data_size = sizeof(SgiState),
268  .init = sgi_init,
269  .close = sgi_end,
270  .decode = decode_frame,
271  .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
272  .capabilities = CODEC_CAP_DR1,
273 };