gifdec.c
Go to the documentation of this file.
1 /*
2  * GIF decoder
3  * Copyright (c) 2003 Fabrice Bellard
4  * Copyright (c) 2006 Baptiste Coudurier
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 //#define DEBUG
24 
25 #include "libavutil/imgutils.h"
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "lzw.h"
29 
30 #define GCE_DISPOSAL_NONE 0
31 #define GCE_DISPOSAL_INPLACE 1
32 #define GCE_DISPOSAL_BACKGROUND 2
33 #define GCE_DISPOSAL_RESTORE 3
34 
35 typedef struct GifState {
43  uint32_t *image_palette;
44 
45  /* after the frame is displayed, the disposal method is used */
47  /* delay during which the frame is shown */
48  int gce_delay;
49 
50  /* LZW compatible decoder */
51  const uint8_t *bytestream;
52  const uint8_t *bytestream_end;
54 
55  /* aux buffers */
56  uint8_t global_palette[256 * 3];
57  uint8_t local_palette[256 * 3];
58 
60 } GifState;
61 
62 static const uint8_t gif87a_sig[6] = "GIF87a";
63 static const uint8_t gif89a_sig[6] = "GIF89a";
64 
65 static int gif_read_image(GifState *s)
66 {
67  int left, top, width, height, bits_per_pixel, code_size, flags;
68  int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
69  uint8_t *ptr, *spal, *palette, *ptr1;
70 
71  left = bytestream_get_le16(&s->bytestream);
72  top = bytestream_get_le16(&s->bytestream);
73  width = bytestream_get_le16(&s->bytestream);
74  height = bytestream_get_le16(&s->bytestream);
75  flags = bytestream_get_byte(&s->bytestream);
76  is_interleaved = flags & 0x40;
77  has_local_palette = flags & 0x80;
78  bits_per_pixel = (flags & 0x07) + 1;
79 
80  av_dlog(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
81 
82  if (has_local_palette) {
83  bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
84  palette = s->local_palette;
85  } else {
86  palette = s->global_palette;
87  bits_per_pixel = s->bits_per_pixel;
88  }
89 
90  /* verify that all the image is inside the screen dimensions */
91  if (left + width > s->screen_width ||
92  top + height > s->screen_height)
93  return AVERROR(EINVAL);
94 
95  /* build the palette */
96  n = (1 << bits_per_pixel);
97  spal = palette;
98  for(i = 0; i < n; i++) {
99  s->image_palette[i] = (0xffu << 24) | AV_RB24(spal);
100  spal += 3;
101  }
102  for(; i < 256; i++)
103  s->image_palette[i] = (0xffu << 24);
104  /* handle transparency */
105  if (s->transparent_color_index >= 0)
107 
108  /* now get the image data */
109  code_size = bytestream_get_byte(&s->bytestream);
110  ff_lzw_decode_init(s->lzw, code_size, s->bytestream,
112 
113  /* read all the image */
114  linesize = s->picture.linesize[0];
115  ptr1 = s->picture.data[0] + top * linesize + left;
116  ptr = ptr1;
117  pass = 0;
118  y1 = 0;
119  for (y = 0; y < height; y++) {
120  ff_lzw_decode(s->lzw, ptr, width);
121  if (is_interleaved) {
122  switch(pass) {
123  default:
124  case 0:
125  case 1:
126  y1 += 8;
127  ptr += linesize * 8;
128  break;
129  case 2:
130  y1 += 4;
131  ptr += linesize * 4;
132  break;
133  case 3:
134  y1 += 2;
135  ptr += linesize * 2;
136  break;
137  }
138  while (y1 >= height) {
139  y1 = 4 >> pass;
140  ptr = ptr1 + linesize * y1;
141  pass++;
142  }
143  } else {
144  ptr += linesize;
145  }
146  }
147  /* read the garbage data until end marker is found */
149  s->bytestream = ff_lzw_cur_ptr(s->lzw);
150  return 0;
151 }
152 
154 {
155  int ext_code, ext_len, i, gce_flags, gce_transparent_index;
156 
157  /* extension */
158  ext_code = bytestream_get_byte(&s->bytestream);
159  ext_len = bytestream_get_byte(&s->bytestream);
160 
161  av_dlog(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
162 
163  switch(ext_code) {
164  case 0xf9:
165  if (ext_len != 4)
166  goto discard_ext;
167  s->transparent_color_index = -1;
168  gce_flags = bytestream_get_byte(&s->bytestream);
169  s->gce_delay = bytestream_get_le16(&s->bytestream);
170  gce_transparent_index = bytestream_get_byte(&s->bytestream);
171  if (gce_flags & 0x01)
172  s->transparent_color_index = gce_transparent_index;
173  else
174  s->transparent_color_index = -1;
175  s->gce_disposal = (gce_flags >> 2) & 0x7;
176 
177  av_dlog(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
178  gce_flags, s->gce_delay,
180 
181  ext_len = bytestream_get_byte(&s->bytestream);
182  break;
183  }
184 
185  /* NOTE: many extension blocks can come after */
186  discard_ext:
187  while (ext_len != 0) {
188  for (i = 0; i < ext_len; i++)
189  bytestream_get_byte(&s->bytestream);
190  ext_len = bytestream_get_byte(&s->bytestream);
191 
192  av_dlog(s->avctx, "gif: ext_len1=%d\n", ext_len);
193  }
194  return 0;
195 }
196 
198 {
199  uint8_t sig[6];
200  int v, n;
201  int has_global_palette;
202 
203  if (s->bytestream_end < s->bytestream + 13)
204  return -1;
205 
206  /* read gif signature */
207  bytestream_get_buffer(&s->bytestream, sig, 6);
208  if (memcmp(sig, gif87a_sig, 6) != 0 &&
209  memcmp(sig, gif89a_sig, 6) != 0)
210  return -1;
211 
212  /* read screen header */
213  s->transparent_color_index = -1;
214  s->screen_width = bytestream_get_le16(&s->bytestream);
215  s->screen_height = bytestream_get_le16(&s->bytestream);
216  if( (unsigned)s->screen_width > 32767
217  || (unsigned)s->screen_height > 32767){
218  av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
219  return -1;
220  }
221 
222  v = bytestream_get_byte(&s->bytestream);
223  s->color_resolution = ((v & 0x70) >> 4) + 1;
224  has_global_palette = (v & 0x80);
225  s->bits_per_pixel = (v & 0x07) + 1;
226  s->background_color_index = bytestream_get_byte(&s->bytestream);
227  bytestream_get_byte(&s->bytestream); /* ignored */
228 
229  av_dlog(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
231  has_global_palette);
232 
233  if (has_global_palette) {
234  n = 1 << s->bits_per_pixel;
235  if (s->bytestream_end < s->bytestream + n * 3)
236  return -1;
238  }
239  return 0;
240 }
241 
243 {
244  while (s->bytestream < s->bytestream_end) {
245  int code = bytestream_get_byte(&s->bytestream);
246 
247  av_dlog(s->avctx, "gif: code=%02x '%c'\n", code, code);
248 
249  switch (code) {
250  case ',':
251  return gif_read_image(s);
252  case '!':
253  if (gif_read_extension(s) < 0)
254  return -1;
255  break;
256  case ';':
257  /* end of image */
258  default:
259  /* error or erroneous EOF */
260  return -1;
261  }
262  }
263  return -1;
264 }
265 
267 {
268  GifState *s = avctx->priv_data;
269 
270  s->avctx = avctx;
271 
273  avctx->coded_frame= &s->picture;
274  s->picture.data[0] = NULL;
275  ff_lzw_decode_open(&s->lzw);
276  return 0;
277 }
278 
279 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
280 {
281  const uint8_t *buf = avpkt->data;
282  int buf_size = avpkt->size;
283  GifState *s = avctx->priv_data;
284  AVFrame *picture = data;
285  int ret;
286 
287  s->bytestream = buf;
288  s->bytestream_end = buf + buf_size;
289  if (gif_read_header1(s) < 0)
290  return -1;
291 
292  avctx->pix_fmt = PIX_FMT_PAL8;
293  if (av_image_check_size(s->screen_width, s->screen_height, 0, avctx))
294  return -1;
296 
297  if (s->picture.data[0])
298  avctx->release_buffer(avctx, &s->picture);
299  if (avctx->get_buffer(avctx, &s->picture) < 0) {
300  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
301  return -1;
302  }
303  s->image_palette = (uint32_t *)s->picture.data[1];
304  ret = gif_parse_next_image(s);
305  if (ret < 0)
306  return ret;
307 
308  *picture = s->picture;
309  *data_size = sizeof(AVPicture);
310  return s->bytestream - buf;
311 }
312 
314 {
315  GifState *s = avctx->priv_data;
316 
318  if(s->picture.data[0])
319  avctx->release_buffer(avctx, &s->picture);
320  return 0;
321 }
322 
324  .name = "gif",
325  .type = AVMEDIA_TYPE_VIDEO,
326  .id = CODEC_ID_GIF,
327  .priv_data_size = sizeof(GifState),
331  .capabilities = CODEC_CAP_DR1,
332  .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
333 };