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  break;
132  case 2:
133  y1 += 4;
134  ptr += linesize * 4;
135  break;
136  case 3:
137  y1 += 2;
138  ptr += linesize * 2;
139  break;
140  }
141  while (y1 >= height) {
142  y1 = 4 >> pass;
143  ptr = ptr1 + linesize * y1;
144  pass++;
145  }
146  } else {
147  ptr += linesize;
148  }
149  }
150  /* read the garbage data until end marker is found */
152 
154  return 0;
155 }
156 
158 {
159  int ext_code, ext_len, i, gce_flags, gce_transparent_index;
160 
161  /* extension */
162  ext_code = bytestream2_get_byte(&s->gb);
163  ext_len = bytestream2_get_byte(&s->gb);
164 
165  av_dlog(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
166 
167  switch(ext_code) {
168  case 0xf9:
169  if (ext_len != 4)
170  goto discard_ext;
171  s->transparent_color_index = -1;
172  gce_flags = bytestream2_get_byte(&s->gb);
173  s->gce_delay = bytestream2_get_le16(&s->gb);
174  gce_transparent_index = bytestream2_get_byte(&s->gb);
175  if (gce_flags & 0x01)
176  s->transparent_color_index = gce_transparent_index;
177  else
178  s->transparent_color_index = -1;
179  s->gce_disposal = (gce_flags >> 2) & 0x7;
180 
181  av_dlog(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
182  gce_flags, s->gce_delay,
184 
185  ext_len = bytestream2_get_byte(&s->gb);
186  break;
187  }
188 
189  /* NOTE: many extension blocks can come after */
190  discard_ext:
191  while (ext_len != 0) {
192  for (i = 0; i < ext_len; i++)
193  bytestream2_get_byte(&s->gb);
194  ext_len = bytestream2_get_byte(&s->gb);
195 
196  av_dlog(s->avctx, "gif: ext_len1=%d\n", ext_len);
197  }
198  return 0;
199 }
200 
202 {
203  uint8_t sig[6];
204  int v, n;
205  int has_global_palette;
206 
207  if (bytestream2_get_bytes_left(&s->gb) < 13)
208  return AVERROR_INVALIDDATA;
209 
210  /* read gif signature */
211  bytestream2_get_buffer(&s->gb, sig, 6);
212  if (memcmp(sig, gif87a_sig, 6) != 0 &&
213  memcmp(sig, gif89a_sig, 6) != 0)
214  return AVERROR_INVALIDDATA;
215 
216  /* read screen header */
217  s->transparent_color_index = -1;
218  s->screen_width = bytestream2_get_le16(&s->gb);
219  s->screen_height = bytestream2_get_le16(&s->gb);
220  if( (unsigned)s->screen_width > 32767
221  || (unsigned)s->screen_height > 32767){
222  av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
223  return AVERROR_INVALIDDATA;
224  }
225 
226  v = bytestream2_get_byte(&s->gb);
227  s->color_resolution = ((v & 0x70) >> 4) + 1;
228  has_global_palette = (v & 0x80);
229  s->bits_per_pixel = (v & 0x07) + 1;
230  s->background_color_index = bytestream2_get_byte(&s->gb);
231  bytestream2_get_byte(&s->gb); /* ignored */
232 
233  av_dlog(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
235  has_global_palette);
236 
237  if (has_global_palette) {
238  n = 1 << s->bits_per_pixel;
239  if (bytestream2_get_bytes_left(&s->gb) < n * 3)
240  return AVERROR_INVALIDDATA;
241  bytestream2_get_buffer(&s->gb, s->global_palette, n * 3);
242  }
243  return 0;
244 }
245 
247 {
248  while (bytestream2_get_bytes_left(&s->gb) > 0) {
249  int code = bytestream2_get_byte(&s->gb);
250  int ret;
251 
252  av_dlog(s->avctx, "gif: code=%02x '%c'\n", code, code);
253 
254  switch (code) {
255  case ',':
256  return gif_read_image(s);
257  case '!':
258  if ((ret = gif_read_extension(s)) < 0)
259  return ret;
260  break;
261  case ';':
262  /* end of image */
263  default:
264  /* error or erroneous EOF */
265  return AVERROR_INVALIDDATA;
266  }
267  }
268  return AVERROR_INVALIDDATA;
269 }
270 
272 {
273  GifState *s = avctx->priv_data;
274 
275  s->avctx = avctx;
276 
278  avctx->coded_frame= &s->picture;
279  s->picture.data[0] = NULL;
280  ff_lzw_decode_open(&s->lzw);
281  return 0;
282 }
283 
284 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
285  AVPacket *avpkt)
286 {
287  const uint8_t *buf = avpkt->data;
288  int buf_size = avpkt->size;
289  GifState *s = avctx->priv_data;
290  AVFrame *picture = data;
291  int ret;
292 
293  bytestream2_init(&s->gb, buf, buf_size);
294  if ((ret = gif_read_header1(s)) < 0)
295  return ret;
296 
297  avctx->pix_fmt = AV_PIX_FMT_PAL8;
298  if ((ret = av_image_check_size(s->screen_width, s->screen_height, 0, avctx)) < 0)
299  return ret;
301 
302  if (s->picture.data[0])
303  avctx->release_buffer(avctx, &s->picture);
304  if ((ret = ff_get_buffer(avctx, &s->picture)) < 0) {
305  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
306  return ret;
307  }
308  s->image_palette = (uint32_t *)s->picture.data[1];
309  ret = gif_parse_next_image(s);
310  if (ret < 0)
311  return ret;
312 
313  *picture = s->picture;
314  *got_frame = 1;
315  return bytestream2_tell(&s->gb);
316 }
317 
319 {
320  GifState *s = avctx->priv_data;
321 
323  if(s->picture.data[0])
324  avctx->release_buffer(avctx, &s->picture);
325  return 0;
326 }
327 
329  .name = "gif",
330  .type = AVMEDIA_TYPE_VIDEO,
331  .id = AV_CODEC_ID_GIF,
332  .priv_data_size = sizeof(GifState),
336  .capabilities = CODEC_CAP_DR1,
337  .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
338 };
int transparent_color_index
Definition: gifdec.c:42
int ff_lzw_decode(LZWState *p, uint8_t *buf, int len)
Decode given number of bytes NOTE: the algorithm here is inspired from the LZW GIF decoder written by...
Definition: lzw.c:171
void ff_lzw_decode_tail(LZWState *p)
Definition: lzw.c:102
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
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
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
static int gif_parse_next_image(GifState *s)
Definition: gifdec.c:246
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:149
int size
Definition: avcodec.h:916
av_cold void ff_lzw_decode_close(LZWState **p)
Definition: lzw.c:120
#define AV_RB24
Definition: intreadwrite.h:64
#define pass
Definition: fft.c:334
av_cold void ff_lzw_decode_open(LZWState **p)
Definition: lzw.c:115
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
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
AVCodecContext * avctx
Definition: gifdec.c:59
AVCodec.
Definition: avcodec.h:2960
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
AVCodec ff_gif_decoder
Definition: gifdec.c:328
uint8_t
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
int screen_height
Definition: gifdec.c:39
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
const uint8_t * buffer
Definition: bytestream.h:33
static int flags
Definition: log.c:42
static int gif_read_extension(GifState *s)
Definition: gifdec.c:157
static av_cold int gif_decode_close(AVCodecContext *avctx)
Definition: gifdec.c:318
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
AVFrame picture
Definition: gifdec.c:37
int screen_width
Definition: gifdec.c:38
Definition: lzw.c:46
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_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:258
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
int ff_lzw_size_read(LZWState *p)
Definition: lzw.c:96
static int gif_read_image(GifState *s)
Definition: gifdec.c:65
int gce_disposal
Definition: gifdec.c:47
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
static AVFrame * picture
static av_cold int gif_decode_init(AVCodecContext *avctx)
Definition: gifdec.c:271
Definition: lzw.h:38
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
static const uint8_t gif89a_sig[6]
Definition: gifdec.c:63
struct GifState GifState
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:183
int gce_delay
Definition: gifdec.c:49
NULL
Definition: eval.c:52
static int width
Definition: utils.c:156
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
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
int background_color_index
Definition: gifdec.c:41
GetByteContext gb
Definition: gifdec.c:52
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:604
uint32_t * image_palette
Definition: gifdec.c:44
uint8_t local_palette[256 *3]
Definition: gifdec.c:57
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int height
Definition: gxfenc.c:72
LZW decoding routines.
static const uint8_t gif87a_sig[6]
Definition: gifdec.c:62
common internal api header.
int ff_lzw_decode_init(LZWState *p, int csize, const uint8_t *buf, int buf_size, int mode)
Initialize LZW decoder.
Definition: lzw.c:133
void * priv_data
Definition: avcodec.h:1382
LZWState * lzw
Definition: gifdec.c:53
static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: gifdec.c:284
static int gif_read_header1(GifState *s)
Definition: gifdec.c:201
int color_resolution
Definition: gifdec.c:43
int bits_per_pixel
Definition: gifdec.c:40
uint8_t global_palette[256 *3]
Definition: gifdec.c:56
This structure stores compressed data.
Definition: avcodec.h:898