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 "internal.h"
29 #include "lzw.h"
30 
31 #define GCE_DISPOSAL_NONE 0
32 #define GCE_DISPOSAL_INPLACE 1
33 #define GCE_DISPOSAL_BACKGROUND 2
34 #define GCE_DISPOSAL_RESTORE 3
35 
36 typedef struct GifState {
44  uint32_t *image_palette;
45 
46  /* after the frame is displayed, the disposal method is used */
48  /* delay during which the frame is shown */
49  int gce_delay;
50 
51  /* LZW compatible decoder */
54 
55  /* aux buffers */
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 = bytestream2_get_le16(&s->gb);
72  top = bytestream2_get_le16(&s->gb);
73  width = bytestream2_get_le16(&s->gb);
74  height = bytestream2_get_le16(&s->gb);
75  flags = bytestream2_get_byte(&s->gb);
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  bytestream2_get_buffer(&s->gb, 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  !width || !height) {
94  av_log(s->avctx, AV_LOG_ERROR, "Invalid image dimensions.\n");
95  return AVERROR_INVALIDDATA;
96  }
97 
98  /* build the palette */
99  n = (1 << bits_per_pixel);
100  spal = palette;
101  for(i = 0; i < n; i++) {
102  s->image_palette[i] = (0xffu << 24) | AV_RB24(spal);
103  spal += 3;
104  }
105  for(; i < 256; i++)
106  s->image_palette[i] = (0xffu << 24);
107  /* handle transparency */
108  if (s->transparent_color_index >= 0)
110 
111  /* now get the image data */
112  code_size = bytestream2_get_byte(&s->gb);
113  ff_lzw_decode_init(s->lzw, code_size, s->gb.buffer,
115 
116  /* read all the image */
117  linesize = s->picture.linesize[0];
118  ptr1 = s->picture.data[0] + top * linesize + left;
119  ptr = ptr1;
120  pass = 0;
121  y1 = 0;
122  for (y = 0; y < height; y++) {
123  ff_lzw_decode(s->lzw, ptr, width);
124  if (is_interleaved) {
125  switch(pass) {
126  default:
127  case 0:
128  case 1:
129  y1 += 8;
130  ptr += linesize * 8;
131  if (y1 >= height) {
132  y1 = pass ? 2 : 4;
133  ptr = ptr1 + linesize * y1;
134  pass++;
135  }
136  break;
137  case 2:
138  y1 += 4;
139  ptr += linesize * 4;
140  if (y1 >= height) {
141  y1 = 1;
142  ptr = ptr1 + linesize;
143  pass++;
144  }
145  break;
146  case 3:
147  y1 += 2;
148  ptr += linesize * 2;
149  break;
150  }
151  } else {
152  ptr += linesize;
153  }
154  }
155  /* read the garbage data until end marker is found */
157 
159  return 0;
160 }
161 
163 {
164  int ext_code, ext_len, i, gce_flags, gce_transparent_index;
165 
166  /* extension */
167  ext_code = bytestream2_get_byte(&s->gb);
168  ext_len = bytestream2_get_byte(&s->gb);
169 
170  av_dlog(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
171 
172  switch(ext_code) {
173  case 0xf9:
174  if (ext_len != 4)
175  goto discard_ext;
176  s->transparent_color_index = -1;
177  gce_flags = bytestream2_get_byte(&s->gb);
178  s->gce_delay = bytestream2_get_le16(&s->gb);
179  gce_transparent_index = bytestream2_get_byte(&s->gb);
180  if (gce_flags & 0x01)
181  s->transparent_color_index = gce_transparent_index;
182  else
183  s->transparent_color_index = -1;
184  s->gce_disposal = (gce_flags >> 2) & 0x7;
185 
186  av_dlog(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
187  gce_flags, s->gce_delay,
189 
190  ext_len = bytestream2_get_byte(&s->gb);
191  break;
192  }
193 
194  /* NOTE: many extension blocks can come after */
195  discard_ext:
196  while (ext_len != 0) {
197  for (i = 0; i < ext_len; i++)
198  bytestream2_get_byte(&s->gb);
199  ext_len = bytestream2_get_byte(&s->gb);
200 
201  av_dlog(s->avctx, "gif: ext_len1=%d\n", ext_len);
202  }
203  return 0;
204 }
205 
207 {
208  uint8_t sig[6];
209  int v, n;
210  int has_global_palette;
211 
212  if (bytestream2_get_bytes_left(&s->gb) < 13)
213  return AVERROR_INVALIDDATA;
214 
215  /* read gif signature */
216  bytestream2_get_buffer(&s->gb, sig, 6);
217  if (memcmp(sig, gif87a_sig, 6) != 0 &&
218  memcmp(sig, gif89a_sig, 6) != 0)
219  return AVERROR_INVALIDDATA;
220 
221  /* read screen header */
222  s->transparent_color_index = -1;
223  s->screen_width = bytestream2_get_le16(&s->gb);
224  s->screen_height = bytestream2_get_le16(&s->gb);
225  if( (unsigned)s->screen_width > 32767
226  || (unsigned)s->screen_height > 32767){
227  av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
228  return AVERROR_INVALIDDATA;
229  }
230 
231  v = bytestream2_get_byte(&s->gb);
232  s->color_resolution = ((v & 0x70) >> 4) + 1;
233  has_global_palette = (v & 0x80);
234  s->bits_per_pixel = (v & 0x07) + 1;
235  s->background_color_index = bytestream2_get_byte(&s->gb);
236  bytestream2_get_byte(&s->gb); /* ignored */
237 
238  av_dlog(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
240  has_global_palette);
241 
242  if (has_global_palette) {
243  n = 1 << s->bits_per_pixel;
244  if (bytestream2_get_bytes_left(&s->gb) < n * 3)
245  return AVERROR_INVALIDDATA;
246  bytestream2_get_buffer(&s->gb, s->global_palette, n * 3);
247  }
248  return 0;
249 }
250 
252 {
253  while (bytestream2_get_bytes_left(&s->gb) > 0) {
254  int code = bytestream2_get_byte(&s->gb);
255  int ret;
256 
257  av_dlog(s->avctx, "gif: code=%02x '%c'\n", code, code);
258 
259  switch (code) {
260  case ',':
261  return gif_read_image(s);
262  case '!':
263  if ((ret = gif_read_extension(s)) < 0)
264  return ret;
265  break;
266  case ';':
267  /* end of image */
268  default:
269  /* error or erroneous EOF */
270  return AVERROR_INVALIDDATA;
271  }
272  }
273  return AVERROR_INVALIDDATA;
274 }
275 
277 {
278  GifState *s = avctx->priv_data;
279 
280  s->avctx = avctx;
281 
283  avctx->coded_frame= &s->picture;
284  s->picture.data[0] = NULL;
285  ff_lzw_decode_open(&s->lzw);
286  return 0;
287 }
288 
289 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
290  AVPacket *avpkt)
291 {
292  const uint8_t *buf = avpkt->data;
293  int buf_size = avpkt->size;
294  GifState *s = avctx->priv_data;
295  AVFrame *picture = data;
296  int ret;
297 
298  bytestream2_init(&s->gb, buf, buf_size);
299  if ((ret = gif_read_header1(s)) < 0)
300  return ret;
301 
302  avctx->pix_fmt = AV_PIX_FMT_PAL8;
303  if ((ret = av_image_check_size(s->screen_width, s->screen_height, 0, avctx)) < 0)
304  return ret;
306 
307  if (s->picture.data[0])
308  avctx->release_buffer(avctx, &s->picture);
309  if ((ret = ff_get_buffer(avctx, &s->picture)) < 0) {
310  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
311  return ret;
312  }
313  s->image_palette = (uint32_t *)s->picture.data[1];
314  ret = gif_parse_next_image(s);
315  if (ret < 0)
316  return ret;
317 
318  *picture = s->picture;
319  *got_frame = 1;
320  return bytestream2_tell(&s->gb);
321 }
322 
324 {
325  GifState *s = avctx->priv_data;
326 
328  if(s->picture.data[0])
329  avctx->release_buffer(avctx, &s->picture);
330  return 0;
331 }
332 
334  .name = "gif",
335  .type = AVMEDIA_TYPE_VIDEO,
336  .id = AV_CODEC_ID_GIF,
337  .priv_data_size = sizeof(GifState),
341  .capabilities = CODEC_CAP_DR1,
342  .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
343 };