Libav
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 #include "libavutil/imgutils.h"
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27 #include "lzw.h"
28 
29 #define GCE_DISPOSAL_NONE 0
30 #define GCE_DISPOSAL_INPLACE 1
31 #define GCE_DISPOSAL_BACKGROUND 2
32 #define GCE_DISPOSAL_RESTORE 3
33 
34 typedef struct GifState {
41  uint32_t *image_palette;
42 
43  /* after the frame is displayed, the disposal method is used */
45  /* delay during which the frame is shown */
46  int gce_delay;
47 
48  /* LZW compatible decoder */
51 
52  /* aux buffers */
55 
57 } GifState;
58 
59 static const uint8_t gif87a_sig[6] = "GIF87a";
60 static const uint8_t gif89a_sig[6] = "GIF89a";
61 
62 static int gif_read_image(GifState *s, AVFrame *frame)
63 {
64  int left, top, width, height, bits_per_pixel, code_size, flags;
65  int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
66  uint8_t *ptr, *spal, *palette, *ptr1;
67 
68  left = bytestream2_get_le16(&s->gb);
69  top = bytestream2_get_le16(&s->gb);
70  width = bytestream2_get_le16(&s->gb);
71  height = bytestream2_get_le16(&s->gb);
72  flags = bytestream2_get_byte(&s->gb);
73  is_interleaved = flags & 0x40;
74  has_local_palette = flags & 0x80;
75  bits_per_pixel = (flags & 0x07) + 1;
76 
77  av_dlog(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
78 
79  if (has_local_palette) {
80  bytestream2_get_buffer(&s->gb, s->local_palette, 3 * (1 << bits_per_pixel));
81  palette = s->local_palette;
82  } else {
83  palette = s->global_palette;
84  bits_per_pixel = s->bits_per_pixel;
85  }
86 
87  /* verify that all the image is inside the screen dimensions */
88  if (left + width > s->screen_width ||
89  top + height > s->screen_height ||
90  !width || !height) {
91  av_log(s->avctx, AV_LOG_ERROR, "Invalid image dimensions.\n");
92  return AVERROR_INVALIDDATA;
93  }
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 = bytestream2_get_byte(&s->gb);
110  ff_lzw_decode_init(s->lzw, code_size, s->gb.buffer,
112 
113  /* read all the image */
114  linesize = frame->linesize[0];
115  ptr1 = frame->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  if (y1 >= height) {
129  y1 = pass ? 2 : 4;
130  ptr = ptr1 + linesize * y1;
131  pass++;
132  }
133  break;
134  case 2:
135  y1 += 4;
136  ptr += linesize * 4;
137  if (y1 >= height) {
138  y1 = 1;
139  ptr = ptr1 + linesize;
140  pass++;
141  }
142  break;
143  case 3:
144  y1 += 2;
145  ptr += linesize * 2;
146  break;
147  }
148  } else {
149  ptr += linesize;
150  }
151  }
152  /* read the garbage data until end marker is found */
154 
156  return 0;
157 }
158 
160 {
161  int ext_code, ext_len, i, gce_flags, gce_transparent_index;
162 
163  /* extension */
164  ext_code = bytestream2_get_byte(&s->gb);
165  ext_len = bytestream2_get_byte(&s->gb);
166 
167  av_dlog(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
168 
169  switch(ext_code) {
170  case 0xf9:
171  if (ext_len != 4)
172  goto discard_ext;
173  s->transparent_color_index = -1;
174  gce_flags = bytestream2_get_byte(&s->gb);
175  s->gce_delay = bytestream2_get_le16(&s->gb);
176  gce_transparent_index = bytestream2_get_byte(&s->gb);
177  if (gce_flags & 0x01)
178  s->transparent_color_index = gce_transparent_index;
179  else
180  s->transparent_color_index = -1;
181  s->gce_disposal = (gce_flags >> 2) & 0x7;
182 
183  av_dlog(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
184  gce_flags, s->gce_delay,
186 
187  ext_len = bytestream2_get_byte(&s->gb);
188  break;
189  }
190 
191  /* NOTE: many extension blocks can come after */
192  discard_ext:
193  while (ext_len != 0) {
194  for (i = 0; i < ext_len; i++)
195  bytestream2_get_byte(&s->gb);
196  ext_len = bytestream2_get_byte(&s->gb);
197 
198  av_dlog(s->avctx, "gif: ext_len1=%d\n", ext_len);
199  }
200  return 0;
201 }
202 
204 {
205  uint8_t sig[6];
206  int v, n;
207  int has_global_palette;
208 
209  if (bytestream2_get_bytes_left(&s->gb) < 13)
210  return AVERROR_INVALIDDATA;
211 
212  /* read gif signature */
213  bytestream2_get_buffer(&s->gb, sig, 6);
214  if (memcmp(sig, gif87a_sig, 6) != 0 &&
215  memcmp(sig, gif89a_sig, 6) != 0)
216  return AVERROR_INVALIDDATA;
217 
218  /* read screen header */
219  s->transparent_color_index = -1;
220  s->screen_width = bytestream2_get_le16(&s->gb);
221  s->screen_height = bytestream2_get_le16(&s->gb);
222  if( (unsigned)s->screen_width > 32767
223  || (unsigned)s->screen_height > 32767){
224  av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
225  return AVERROR_INVALIDDATA;
226  }
227 
228  v = bytestream2_get_byte(&s->gb);
229  s->color_resolution = ((v & 0x70) >> 4) + 1;
230  has_global_palette = (v & 0x80);
231  s->bits_per_pixel = (v & 0x07) + 1;
232  s->background_color_index = bytestream2_get_byte(&s->gb);
233  bytestream2_get_byte(&s->gb); /* ignored */
234 
235  av_dlog(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
237  has_global_palette);
238 
239  if (has_global_palette) {
240  n = 1 << s->bits_per_pixel;
241  if (bytestream2_get_bytes_left(&s->gb) < n * 3)
242  return AVERROR_INVALIDDATA;
243  bytestream2_get_buffer(&s->gb, s->global_palette, n * 3);
244  }
245  return 0;
246 }
247 
248 static int gif_parse_next_image(GifState *s, AVFrame *frame)
249 {
250  while (bytestream2_get_bytes_left(&s->gb) > 0) {
251  int code = bytestream2_get_byte(&s->gb);
252  int ret;
253 
254  av_dlog(s->avctx, "gif: code=%02x '%c'\n", code, code);
255 
256  switch (code) {
257  case ',':
258  return gif_read_image(s, frame);
259  case '!':
260  if ((ret = gif_read_extension(s)) < 0)
261  return ret;
262  break;
263  case ';':
264  /* end of image */
265  default:
266  /* error or erroneous EOF */
267  return AVERROR_INVALIDDATA;
268  }
269  }
270  return AVERROR_INVALIDDATA;
271 }
272 
274 {
275  GifState *s = avctx->priv_data;
276 
277  s->avctx = avctx;
278 
279  ff_lzw_decode_open(&s->lzw);
280  return 0;
281 }
282 
283 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
284  AVPacket *avpkt)
285 {
286  const uint8_t *buf = avpkt->data;
287  int buf_size = avpkt->size;
288  GifState *s = avctx->priv_data;
289  AVFrame *picture = data;
290  int ret;
291 
292  bytestream2_init(&s->gb, buf, buf_size);
293  if ((ret = gif_read_header1(s)) < 0)
294  return ret;
295 
296  avctx->pix_fmt = AV_PIX_FMT_PAL8;
297 
298  if ((ret = ff_set_dimensions(avctx, s->screen_width, s->screen_height)) < 0)
299  return ret;
300 
301  if ((ret = ff_get_buffer(avctx, picture, 0)) < 0) {
302  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
303  return ret;
304  }
305  s->image_palette = (uint32_t *)picture->data[1];
306  ret = gif_parse_next_image(s, picture);
307  if (ret < 0)
308  return ret;
309 
310  *got_frame = 1;
311  return bytestream2_tell(&s->gb);
312 }
313 
315 {
316  GifState *s = avctx->priv_data;
317 
319  return 0;
320 }
321 
323  .name = "gif",
324  .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
325  .type = AVMEDIA_TYPE_VIDEO,
326  .id = AV_CODEC_ID_GIF,
327  .priv_data_size = sizeof(GifState),
331  .capabilities = CODEC_CAP_DR1,
332 };
int transparent_color_index
Definition: gifdec.c:39
static int gif_read_image(GifState *s, AVFrame *frame)
Definition: gifdec.c:62
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
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
static int gif_parse_next_image(GifState *s, AVFrame *frame)
Definition: gifdec.c:248
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
misc image utilities
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:133
int size
Definition: avcodec.h:968
av_cold void ff_lzw_decode_close(LZWState **p)
Definition: lzw.c:120
#define AV_RB24
Definition: intreadwrite.h:64
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:1248
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:56
AVCodec.
Definition: avcodec.h:2790
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
AVCodec ff_gif_decoder
Definition: gifdec.c:322
uint8_t
#define av_cold
Definition: attributes.h:66
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
int screen_height
Definition: gifdec.c:36
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:967
const uint8_t * buffer
Definition: bytestream.h:33
static int flags
Definition: log.c:44
static int gif_read_extension(GifState *s)
Definition: gifdec.c:159
static av_cold int gif_decode_close(AVCodecContext *avctx)
Definition: gifdec.c:314
int screen_width
Definition: gifdec.c:35
Definition: lzw.c:46
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
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:150
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:168
const char * name
Name of the codec implementation.
Definition: avcodec.h:2797
int ff_lzw_size_read(LZWState *p)
Definition: lzw.c:96
#define pass
Definition: fft_template.c:335
int gce_disposal
Definition: gifdec.c:44
static av_cold int gif_decode_init(AVCodecContext *avctx)
Definition: gifdec.c:273
Definition: lzw.h:38
static const uint8_t gif89a_sig[6]
Definition: gifdec.c:60
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:183
int gce_delay
Definition: gifdec.c:46
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
main external API structure.
Definition: avcodec.h:1044
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:603
int background_color_index
Definition: gifdec.c:38
GetByteContext gb
Definition: gifdec.c:49
uint32_t * image_palette
Definition: gifdec.c:41
uint8_t local_palette[256 *3]
Definition: gifdec.c:54
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
int height
Definition: gxfenc.c:72
LZW decoding routines.
static const uint8_t gif87a_sig[6]
Definition: gifdec.c:59
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
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1086
LZWState * lzw
Definition: gifdec.c:50
static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: gifdec.c:283
static int gif_read_header1(GifState *s)
Definition: gifdec.c:203
int color_resolution
Definition: gifdec.c:40
int bits_per_pixel
Definition: gifdec.c:37
uint8_t global_palette[256 *3]
Definition: gifdec.c:53
This structure stores compressed data.
Definition: avcodec.h:944