FFmpeg
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  * Copyright (c) 2012 Vitaliy E Sugrobov
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include "libavutil/opt.h"
25 #include "avcodec.h"
26 #include "bytestream.h"
27 #include "codec_internal.h"
28 #include "decode.h"
29 #include "lzw.h"
30 #include "gif.h"
31 
32 /* This value is intentionally set to "transparent white" color.
33  * It is much better to have white background instead of black
34  * when gif image converted to format which not support transparency.
35  */
36 #define GIF_TRANSPARENT_COLOR 0x00ffffff
37 
38 typedef struct GifState {
39  const AVClass *class;
45  uint32_t bg_color;
49  /* intermediate buffer for storing color indices
50  * obtained from lzw-encoded data stream */
51  uint8_t *idx_line;
53 
54  /* after the frame is displayed, the disposal method is used */
57  /* rectangle describing area that must be disposed */
59  /* depending on disposal method we store either part of the image
60  * drawn on the canvas or background color that
61  * should be used upon disposal */
62  uint8_t *stored_img;
65 
68 
69  /* aux buffers */
70  uint32_t global_palette[256];
71  uint32_t local_palette[256];
72 
74  int keyframe;
76  int trans_color; /**< color value that is used instead of transparent color */
77 } GifState;
78 
79 static void gif_read_palette(GifState *s, uint32_t *pal, int nb)
80 {
81  int i;
82 
83  for (i = 0; i < nb; i++, pal++)
84  *pal = (0xffu << 24) | bytestream2_get_be24u(&s->gb);
85 }
86 
87 static void gif_fill(AVFrame *picture, uint32_t color)
88 {
89  const ptrdiff_t linesize = picture->linesize[0];
90  uint8_t *py = picture->data[0];
91  const int w = picture->width;
92  const int h = picture->height;
93 
94  for (int y = 0; y < h; y++) {
95  uint32_t *px = (uint32_t *)py;
96  for (int x = 0; x < w; x++)
97  px[x] = color;
98  py += linesize;
99  }
100 }
101 
102 static void gif_fill_rect(AVFrame *picture, uint32_t color, int l, int t, int w, int h)
103 {
104  const ptrdiff_t linesize = picture->linesize[0];
105  uint8_t *py = picture->data[0] + t * linesize;
106 
107  for (int y = 0; y < h; y++) {
108  uint32_t *px = ((uint32_t *)py) + l;
109  for (int x = 0; x < w; x++)
110  px[x] = color;
111  py += linesize;
112  }
113 }
114 
115 static void gif_copy_img_rect(const uint8_t *src, uint8_t *dst,
116  ptrdiff_t src_linesize,
117  ptrdiff_t dst_linesize,
118  int l, int t, int w, int h)
119 {
120  const uint8_t *src_py = src;
121  uint8_t *dst_py = dst;
122 
123  src_py += t * src_linesize;
124  dst_py += t * dst_linesize;
125  for (int y = 0; y < h; y++) {
126  memcpy(dst_py + l * 4, src_py + l * 4, w * 4);
127  src_py += src_linesize;
128  dst_py += dst_linesize;
129  }
130 }
131 
133 {
134  int left, top, width, height, bits_per_pixel, code_size, flags, pw;
135  int is_interleaved, has_local_palette, y, pass, y1, pal_size, lzwed_len;
136  uint32_t *ptr, *pal, *px, *pr, *ptr1;
137  ptrdiff_t linesize;
138  int ret;
139  uint8_t *idx;
140 
141  /* At least 9 bytes of Image Descriptor. */
142  if (bytestream2_get_bytes_left(&s->gb) < 9)
143  return AVERROR_INVALIDDATA;
144 
145  left = bytestream2_get_le16u(&s->gb);
146  top = bytestream2_get_le16u(&s->gb);
147  width = bytestream2_get_le16u(&s->gb);
148  height = bytestream2_get_le16u(&s->gb);
149  flags = bytestream2_get_byteu(&s->gb);
150  is_interleaved = flags & 0x40;
151  has_local_palette = flags & 0x80;
152  bits_per_pixel = (flags & 0x07) + 1;
153 
154  ff_dlog(s->avctx, "image x=%d y=%d w=%d h=%d\n", left, top, width, height);
155 
156  if (has_local_palette) {
157  pal_size = 1 << bits_per_pixel;
158 
159  if (bytestream2_get_bytes_left(&s->gb) < pal_size * 3)
160  return AVERROR_INVALIDDATA;
161 
162  gif_read_palette(s, s->local_palette, pal_size);
163  pal = s->local_palette;
164  } else {
165  if (!s->has_global_palette) {
166  av_log(s->avctx, AV_LOG_ERROR, "picture doesn't have either global or local palette.\n");
167  return AVERROR_INVALIDDATA;
168  }
169 
170  pal = s->global_palette;
171  }
172 
173  if (s->keyframe) {
174  if (s->transparent_color_index == -1 && s->has_global_palette) {
175  /* transparency wasn't set before the first frame, fill with background color */
176  gif_fill(frame, s->bg_color);
177  } else {
178  /* otherwise fill with transparent color.
179  * this is necessary since by default picture filled with 0x80808080. */
180  gif_fill(frame, s->trans_color);
181  }
182  }
183 
184  /* verify that all the image is inside the screen dimensions */
185  if (!width || width > s->screen_width) {
186  av_log(s->avctx, AV_LOG_WARNING, "Invalid image width: %d, truncating.\n", width);
187  width = s->screen_width;
188  }
189  if (left >= s->screen_width) {
190  av_log(s->avctx, AV_LOG_ERROR, "Invalid left position: %d.\n", left);
191  return AVERROR_INVALIDDATA;
192  }
193  if (!height || height > s->screen_height) {
194  av_log(s->avctx, AV_LOG_WARNING, "Invalid image height: %d, truncating.\n", height);
195  height = s->screen_height;
196  }
197  if (top >= s->screen_height) {
198  av_log(s->avctx, AV_LOG_ERROR, "Invalid top position: %d.\n", top);
199  return AVERROR_INVALIDDATA;
200  }
201  if (left + width > s->screen_width) {
202  /* width must be kept around to avoid lzw vs line desync */
203  pw = s->screen_width - left;
204  av_log(s->avctx, AV_LOG_WARNING, "Image too wide by %d, truncating.\n",
205  left + width - s->screen_width);
206  } else {
207  pw = width;
208  }
209  if (top + height > s->screen_height) {
210  /* we don't care about the extra invisible lines */
211  av_log(s->avctx, AV_LOG_WARNING, "Image too high by %d, truncating.\n",
212  top + height - s->screen_height);
213  height = s->screen_height - top;
214  }
215 
216  /* process disposal method */
217  if (s->gce_prev_disposal == GCE_DISPOSAL_BACKGROUND) {
218  gif_fill_rect(frame, s->stored_bg_color, s->gce_l, s->gce_t, s->gce_w, s->gce_h);
219  } else if (s->gce_prev_disposal == GCE_DISPOSAL_RESTORE) {
220  gif_copy_img_rect(s->stored_img, frame->data[0],
221  FFABS(frame->linesize[0]), frame->linesize[0], s->gce_l, s->gce_t, s->gce_w, s->gce_h);
222  }
223 
224  s->gce_prev_disposal = s->gce_disposal;
225 
226  if (s->gce_disposal != GCE_DISPOSAL_NONE) {
227  s->gce_l = left; s->gce_t = top;
228  s->gce_w = pw; s->gce_h = height;
229 
230  if (s->gce_disposal == GCE_DISPOSAL_BACKGROUND) {
231  if (s->transparent_color_index >= 0)
232  s->stored_bg_color = s->trans_color;
233  else
234  s->stored_bg_color = s->bg_color;
235  } else if (s->gce_disposal == GCE_DISPOSAL_RESTORE) {
236  av_fast_malloc(&s->stored_img, &s->stored_img_size, FFABS(frame->linesize[0]) * frame->height);
237  if (!s->stored_img)
238  return AVERROR(ENOMEM);
239 
240  gif_copy_img_rect(frame->data[0], s->stored_img,
241  frame->linesize[0], FFABS(frame->linesize[0]), left, top, pw, height);
242  }
243  }
244 
245  /* Expect at least 2 bytes: 1 for lzw code size and 1 for block size. */
246  if (bytestream2_get_bytes_left(&s->gb) < 2)
247  return AVERROR_INVALIDDATA;
248 
249  /* now get the image data */
250  code_size = bytestream2_get_byteu(&s->gb);
251  if ((ret = ff_lzw_decode_init(s->lzw, code_size, s->gb.buffer,
252  bytestream2_get_bytes_left(&s->gb), FF_LZW_GIF)) < 0) {
253  av_log(s->avctx, AV_LOG_ERROR, "LZW init failed\n");
254  return ret;
255  }
256 
257  /* read all the image */
258  linesize = frame->linesize[0];
259  ptr1 = (uint32_t *)(frame->data[0] + top * linesize) + left;
260  ptr = ptr1;
261  pass = 0;
262  y1 = 0;
263  for (y = 0; y < height; y++) {
264  int count = ff_lzw_decode(s->lzw, s->idx_line, width);
265  if (count != width) {
266  if (count)
267  av_log(s->avctx, AV_LOG_ERROR, "LZW decode failed\n");
268  goto decode_tail;
269  }
270 
271  pr = ptr + pw;
272 
273  for (px = ptr, idx = s->idx_line; px < pr; px++, idx++) {
274  if (*idx != s->transparent_color_index)
275  *px = pal[*idx];
276  }
277 
278  if (is_interleaved) {
279  switch(pass) {
280  default:
281  case 0:
282  case 1:
283  y1 += 8;
284  ptr += linesize * 2;
285  break;
286  case 2:
287  y1 += 4;
288  ptr += linesize * 1;
289  break;
290  case 3:
291  y1 += 2;
292  ptr += linesize / 2;
293  break;
294  }
295  while (y1 >= height) {
296  y1 = 4 >> pass;
297  ptr = ptr1 + linesize / 4 * y1;
298  pass++;
299  }
300  } else {
301  ptr += linesize / 4;
302  }
303  }
304 
305  decode_tail:
306  /* read the garbage data until end marker is found */
307  lzwed_len = ff_lzw_decode_tail(s->lzw);
308  bytestream2_skipu(&s->gb, lzwed_len);
309 
310  /* Graphic Control Extension's scope is single frame.
311  * Remove its influence. */
312  s->transparent_color_index = -1;
313  s->gce_disposal = GCE_DISPOSAL_NONE;
314 
315  return 0;
316 }
317 
319 {
320  int ext_code, ext_len, gce_flags, gce_transparent_index;
321 
322  /* There must be at least 2 bytes:
323  * 1 for extension label and 1 for extension length. */
324  if (bytestream2_get_bytes_left(&s->gb) < 2)
325  return AVERROR_INVALIDDATA;
326 
327  ext_code = bytestream2_get_byteu(&s->gb);
328  ext_len = bytestream2_get_byteu(&s->gb);
329 
330  ff_dlog(s->avctx, "ext_code=0x%x len=%d\n", ext_code, ext_len);
331 
332  switch(ext_code) {
333  case GIF_GCE_EXT_LABEL:
334  if (ext_len != 4)
335  goto discard_ext;
336 
337  /* We need at least 5 bytes more: 4 is for extension body
338  * and 1 for next block size. */
339  if (bytestream2_get_bytes_left(&s->gb) < 5)
340  return AVERROR_INVALIDDATA;
341 
342  gce_flags = bytestream2_get_byteu(&s->gb);
343  bytestream2_skipu(&s->gb, 2); // delay during which the frame is shown
344  gce_transparent_index = bytestream2_get_byteu(&s->gb);
345  if (gce_flags & 0x01)
346  s->transparent_color_index = gce_transparent_index;
347  else
348  s->transparent_color_index = -1;
349  s->gce_disposal = (gce_flags >> 2) & 0x7;
350 
351  ff_dlog(s->avctx, "gce_flags=%x tcolor=%d disposal=%d\n",
352  gce_flags,
353  s->transparent_color_index, s->gce_disposal);
354 
355  if (s->gce_disposal > 3) {
356  s->gce_disposal = GCE_DISPOSAL_NONE;
357  ff_dlog(s->avctx, "invalid value in gce_disposal (%d). Using default value of 0.\n", ext_len);
358  }
359 
360  ext_len = bytestream2_get_byteu(&s->gb);
361  break;
362  }
363 
364  /* NOTE: many extension blocks can come after */
365  discard_ext:
366  while (ext_len) {
367  /* There must be at least ext_len bytes and 1 for next block size byte. */
368  if (bytestream2_get_bytes_left(&s->gb) < ext_len + 1)
369  return AVERROR_INVALIDDATA;
370 
371  bytestream2_skipu(&s->gb, ext_len);
372  ext_len = bytestream2_get_byteu(&s->gb);
373 
374  ff_dlog(s->avctx, "ext_len1=%d\n", ext_len);
375  }
376  return 0;
377 }
378 
380 {
381  uint8_t sig[6];
382  int v, n;
383  int background_color_index;
384 
385  if (bytestream2_get_bytes_left(&s->gb) < 13)
386  return AVERROR_INVALIDDATA;
387 
388  /* read gif signature */
389  bytestream2_get_bufferu(&s->gb, sig, 6);
390  if (memcmp(sig, gif87a_sig, 6) &&
391  memcmp(sig, gif89a_sig, 6))
392  return AVERROR_INVALIDDATA;
393 
394  /* read screen header */
395  s->transparent_color_index = -1;
396  s->screen_width = bytestream2_get_le16u(&s->gb);
397  s->screen_height = bytestream2_get_le16u(&s->gb);
398 
399  v = bytestream2_get_byteu(&s->gb);
400  s->color_resolution = ((v & 0x70) >> 4) + 1;
401  s->has_global_palette = (v & 0x80);
402  s->bits_per_pixel = (v & 0x07) + 1;
403  background_color_index = bytestream2_get_byteu(&s->gb);
404  n = bytestream2_get_byteu(&s->gb);
405  if (n) {
406  s->avctx->sample_aspect_ratio.num = n + 15;
407  s->avctx->sample_aspect_ratio.den = 64;
408  }
409 
410  ff_dlog(s->avctx, "screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
411  s->screen_width, s->screen_height, s->bits_per_pixel,
412  s->has_global_palette);
413 
414  if (s->has_global_palette) {
415  s->background_color_index = background_color_index;
416  n = 1 << s->bits_per_pixel;
417  if (bytestream2_get_bytes_left(&s->gb) < n * 3)
418  return AVERROR_INVALIDDATA;
419 
420  gif_read_palette(s, s->global_palette, n);
421  s->bg_color = s->global_palette[s->background_color_index];
422  } else
423  s->background_color_index = -1;
424 
425  return 0;
426 }
427 
429 {
430  while (bytestream2_get_bytes_left(&s->gb) > 0) {
431  int code = bytestream2_get_byte(&s->gb);
432  int ret;
433 
434  av_log(s->avctx, AV_LOG_DEBUG, "code=%02x '%c'\n", code, code);
435 
436  switch (code) {
437  case GIF_IMAGE_SEPARATOR:
438  return gif_read_image(s, frame);
440  if ((ret = gif_read_extension(s)) < 0)
441  return ret;
442  break;
443  case GIF_TRAILER:
444  /* end of image */
445  return AVERROR_EOF;
446  default:
447  /* erroneous block label */
448  return AVERROR_INVALIDDATA;
449  }
450  }
451  return AVERROR_EOF;
452 }
453 
455 {
456  GifState *s = avctx->priv_data;
457 
458  s->avctx = avctx;
459 
460  avctx->pix_fmt = AV_PIX_FMT_RGB32;
461  s->frame = av_frame_alloc();
462  if (!s->frame)
463  return AVERROR(ENOMEM);
464  ff_lzw_decode_open(&s->lzw);
465  if (!s->lzw)
466  return AVERROR(ENOMEM);
467  return 0;
468 }
469 
470 static int gif_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
471  int *got_frame, AVPacket *avpkt)
472 {
473  GifState *s = avctx->priv_data;
474  int ret;
475 
476  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
477 
478  if (avpkt->size >= 6) {
479  s->keyframe = memcmp(avpkt->data, gif87a_sig, 6) == 0 ||
480  memcmp(avpkt->data, gif89a_sig, 6) == 0;
481  } else {
482  s->keyframe = 0;
483  }
484 
485  if (s->keyframe) {
486  s->keyframe_ok = 0;
487  s->gce_prev_disposal = GCE_DISPOSAL_NONE;
488  if ((ret = gif_read_header1(s)) < 0)
489  return ret;
490 
491  if ((ret = ff_set_dimensions(avctx, s->screen_width, s->screen_height)) < 0)
492  return ret;
493 
494  av_frame_unref(s->frame);
495  av_fast_malloc(&s->idx_line, &s->idx_line_size, s->screen_width);
496  if (!s->idx_line)
497  return AVERROR(ENOMEM);
498  } else if (!s->keyframe_ok) {
499  av_log(avctx, AV_LOG_ERROR, "cannot decode frame without keyframe\n");
500  return AVERROR_INVALIDDATA;
501  }
502 
503  ret = ff_reget_buffer(avctx, s->frame, 0);
504  if (ret < 0)
505  return ret;
506 
507  ret = gif_parse_next_image(s, s->frame);
508  if (ret < 0)
509  return ret;
510 
511  if ((ret = av_frame_ref(rframe, s->frame)) < 0)
512  return ret;
513 
514  rframe->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
515  rframe->flags = AV_FRAME_FLAG_KEY * s->keyframe;
516  s->keyframe_ok |= !!s->keyframe;
517 
518  *got_frame = 1;
519 
520  return bytestream2_tell(&s->gb);
521 }
522 
524 {
525  GifState *s = avctx->priv_data;
526 
527  ff_lzw_decode_close(&s->lzw);
528  av_frame_free(&s->frame);
529  av_freep(&s->idx_line);
530  av_freep(&s->stored_img);
531 
532  return 0;
533 }
534 
535 static const AVOption options[] = {
536  { "trans_color", "color value (ARGB) that is used instead of transparent color",
537  offsetof(GifState, trans_color), AV_OPT_TYPE_INT,
538  {.i64 = GIF_TRANSPARENT_COLOR}, 0, 0xffffffff,
540  { NULL },
541 };
542 
543 static const AVClass decoder_class = {
544  .class_name = "gif decoder",
545  .item_name = av_default_item_name,
546  .option = options,
547  .version = LIBAVUTIL_VERSION_INT,
548  .category = AV_CLASS_CATEGORY_DECODER,
549 };
550 
552  .p.name = "gif",
553  CODEC_LONG_NAME("GIF (Graphics Interchange Format)"),
554  .p.type = AVMEDIA_TYPE_VIDEO,
555  .p.id = AV_CODEC_ID_GIF,
556  .priv_data_size = sizeof(GifState),
558  .close = gif_decode_close,
560  .p.capabilities = AV_CODEC_CAP_DR1,
561  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
562  .p.priv_class = &decoder_class,
563 };
GIF_TRANSPARENT_COLOR
#define GIF_TRANSPARENT_COLOR
Definition: gifdec.c:36
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AV_CLASS_CATEGORY_DECODER
@ AV_CLASS_CATEGORY_DECODER
Definition: log.h:35
AV_OPT_FLAG_VIDEO_PARAM
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:284
color
Definition: vf_paletteuse.c:511
GetByteContext
Definition: bytestream.h:33
options
static const AVOption options[]
Definition: gifdec.c:535
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
GifState::idx_line_size
int idx_line_size
Definition: gifdec.c:52
GifState::background_color_index
int background_color_index
Definition: gifdec.c:46
bytestream2_skipu
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:174
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
GIF_TRAILER
#define GIF_TRAILER
Definition: gif.h:42
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVFrame::width
int width
Definition: frame.h:412
w
uint8_t w
Definition: llviddspenc.c:38
AVPacket::data
uint8_t * data
Definition: packet.h:522
AVOption
AVOption.
Definition: opt.h:251
FFCodec
Definition: codec_internal.h:127
gif_decode_frame
static int gif_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition: gifdec.c:470
ff_lzw_decode
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:169
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:649
GIF_GCE_EXT_LABEL
#define GIF_GCE_EXT_LABEL
Definition: gif.h:45
ff_lzw_decode_close
av_cold void ff_lzw_decode_close(LZWState **p)
Definition: lzw.c:118
ff_set_dimensions
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:94
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
GifState::screen_height
int screen_height
Definition: gifdec.c:42
gif_parse_next_image
static int gif_parse_next_image(GifState *s, AVFrame *frame)
Definition: gifdec.c:428
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
gif_decode_init
static av_cold int gif_decode_init(AVCodecContext *avctx)
Definition: gifdec.c:454
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:88
gif89a_sig
static const uint8_t gif89a_sig[6]
Definition: gif.h:35
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:628
GifState::stored_bg_color
int stored_bg_color
Definition: gifdec.c:64
GifState::color_resolution
int color_resolution
Definition: gifdec.c:48
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
s
#define s(width, name)
Definition: cbs_vp9.c:198
ff_lzw_decode_open
av_cold void ff_lzw_decode_open(LZWState **p)
Definition: lzw.c:113
GIF_IMAGE_SEPARATOR
#define GIF_IMAGE_SEPARATOR
Definition: gif.h:44
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
decoder_class
static const AVClass decoder_class
Definition: gifdec.c:543
gif_copy_img_rect
static void gif_copy_img_rect(const uint8_t *src, uint8_t *dst, ptrdiff_t src_linesize, ptrdiff_t dst_linesize, int l, int t, int w, int h)
Definition: gifdec.c:115
decode.h
LZWState
Definition: lzw.c:46
GifState::trans_color
int trans_color
color value that is used instead of transparent color
Definition: gifdec.c:76
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
GifState::gce_l
int gce_l
Definition: gifdec.c:58
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
gif_read_extension
static int gif_read_extension(GifState *s)
Definition: gifdec.c:318
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
GifState::keyframe_ok
int keyframe_ok
Definition: gifdec.c:75
gif_read_header1
static int gif_read_header1(GifState *s)
Definition: gifdec.c:379
GifState::stored_img_size
int stored_img_size
Definition: gifdec.c:63
FF_LZW_GIF
@ FF_LZW_GIF
Definition: lzw.h:38
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
bytestream2_tell
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:192
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:28
gif_read_image
static int gif_read_image(GifState *s, AVFrame *frame)
Definition: gifdec.c:132
GifState::screen_width
int screen_width
Definition: gifdec.c:41
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:442
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:523
av_frame_ref
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:361
codec_internal.h
lzw.h
LZW decoding routines.
gif_fill_rect
static void gif_fill_rect(AVFrame *picture, uint32_t color, int l, int t, int w, int h)
Definition: gifdec.c:102
gif.h
GifState::frame
AVFrame * frame
Definition: gifdec.c:40
GifState::bits_per_pixel
int bits_per_pixel
Definition: gifdec.c:44
GCE_DISPOSAL_NONE
#define GCE_DISPOSAL_NONE
Definition: gif.h:37
GCE_DISPOSAL_RESTORE
#define GCE_DISPOSAL_RESTORE
Definition: gif.h:40
height
#define height
AV_PIX_FMT_RGB32
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:455
GifState::gce_disposal
int gce_disposal
Definition: gifdec.c:56
GifState::gce_prev_disposal
int gce_prev_disposal
Definition: gifdec.c:55
GifState::gce_h
int gce_h
Definition: gifdec.c:58
GifState::lzw
LZWState * lzw
Definition: gifdec.c:67
AV_CODEC_ID_GIF
@ AV_CODEC_ID_GIF
Definition: codec_id.h:149
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:245
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
gif87a_sig
static const uint8_t gif87a_sig[6]
Definition: gif.h:34
ff_lzw_decode_init
int ff_lzw_decode_init(LZWState *p, int csize, const uint8_t *buf, int buf_size, int mode)
Initialize LZW decoder.
Definition: lzw.c:131
GIF_EXTENSION_INTRODUCER
#define GIF_EXTENSION_INTRODUCER
Definition: gif.h:43
AV_OPT_FLAG_DECODING_PARAM
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:282
GifState::bg_color
uint32_t bg_color
Definition: gifdec.c:45
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:622
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
gif_fill
static void gif_fill(AVFrame *picture, uint32_t color)
Definition: gifdec.c:87
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:662
avcodec.h
GifState::idx_line
uint8_t * idx_line
Definition: gifdec.c:51
ff_reget_buffer
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition: decode.c:1736
ret
ret
Definition: filter_design.txt:187
GCE_DISPOSAL_BACKGROUND
#define GCE_DISPOSAL_BACKGROUND
Definition: gif.h:39
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
left
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:386
GifState::keyframe
int keyframe
Definition: gifdec.c:74
GifState::transparent_color_index
int transparent_color_index
Definition: gifdec.c:47
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AVFrame::height
int height
Definition: frame.h:412
GifState::local_palette
uint32_t local_palette[256]
Definition: gifdec.c:71
GifState
Definition: gifdec.c:38
ff_gif_decoder
const FFCodec ff_gif_decoder
Definition: gifdec.c:551
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
GifState::has_global_palette
int has_global_palette
Definition: gifdec.c:43
gif_read_palette
static void gif_read_palette(GifState *s, uint32_t *pal, int nb)
Definition: gifdec.c:79
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
bytestream2_get_bufferu
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:277
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_fast_malloc
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:555
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
GifState::gb
GetByteContext gb
Definition: gifdec.c:66
GifState::stored_img
uint8_t * stored_img
Definition: gifdec.c:62
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
gif_decode_close
static av_cold int gif_decode_close(AVCodecContext *avctx)
Definition: gifdec.c:523
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:385
GifState::gce_w
int gce_w
Definition: gifdec.c:58
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ff_lzw_decode_tail
int ff_lzw_decode_tail(LZWState *p)
Definition: lzw.c:99
h
h
Definition: vp9dsp_template.c:2038
GifState::gce_t
int gce_t
Definition: gifdec.c:58
GifState::global_palette
uint32_t global_palette[256]
Definition: gifdec.c:70
GifState::avctx
AVCodecContext * avctx
Definition: gifdec.c:73