3 * Copyright (c) 2003 Fabrice Bellard
4 * Copyright (c) 2006 Baptiste Coudurier
5 * Copyright (c) 2012 Vitaliy E Sugrobov
7 * This file is part of FFmpeg.
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.
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.
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
26 #include "libavutil/imgutils.h"
27 #include "libavutil/opt.h"
29 #include "bytestream.h"
34 /* This value is intentionally set to "transparent white" color.
35 * It is much better to have white background instead of black
36 * when gif image converted to format which not support transparency.
38 #define GIF_TRANSPARENT_COLOR 0x00ffffff
40 typedef struct GifState {
45 int has_global_palette;
48 int background_color_index;
49 int transparent_color_index;
51 /* intermediate buffer for storing color indices
52 * obtained from lzw-encoded data stream */
56 /* after the frame is displayed, the disposal method is used */
57 int gce_prev_disposal;
59 /* rectangle describing area that must be disposed */
60 int gce_l, gce_t, gce_w, gce_h;
61 /* depending on disposal method we store either part of the image
62 * drawn on the canvas or background color that
63 * should be used upon disposal */
64 uint32_t * stored_img;
69 /* LZW compatible decoder */
73 uint32_t global_palette[256];
74 uint32_t local_palette[256];
76 AVCodecContext *avctx;
79 int trans_color; /**< color value that is used instead of transparent color */
82 static void gif_read_palette(GifState *s, uint32_t *pal, int nb)
86 for (i = 0; i < nb; i++, pal++)
87 *pal = (0xffu << 24) | bytestream2_get_be24u(&s->gb);
90 static void gif_fill(AVFrame *picture, uint32_t color)
92 uint32_t *p = (uint32_t *)picture->data[0];
93 uint32_t *p_end = p + (picture->linesize[0] / sizeof(uint32_t)) * picture->height;
95 for (; p < p_end; p++)
99 static void gif_fill_rect(AVFrame *picture, uint32_t color, int l, int t, int w, int h)
101 const int linesize = picture->linesize[0] / sizeof(uint32_t);
102 const uint32_t *py = (uint32_t *)picture->data[0] + t * linesize;
103 const uint32_t *pr, *pb = py + h * linesize;
106 for (; py < pb; py += linesize) {
107 px = (uint32_t *)py + l;
110 for (; px < pr; px++)
115 static void gif_copy_img_rect(const uint32_t *src, uint32_t *dst,
116 int linesize, int l, int t, int w, int h)
118 const int y_start = t * linesize;
119 const uint32_t *src_px,
120 *src_py = src + y_start,
121 *dst_py = dst + y_start;
122 const uint32_t *src_pb = src_py + h * linesize;
125 for (; src_py < src_pb; src_py += linesize, dst_py += linesize) {
127 dst_px = (uint32_t *)dst_py + l;
129 memcpy(dst_px, src_px, w * sizeof(uint32_t));
133 static int gif_read_image(GifState *s)
135 int left, top, width, height, bits_per_pixel, code_size, flags;
136 int is_interleaved, has_local_palette, y, pass, y1, linesize, pal_size;
137 uint32_t *ptr, *pal, *px, *pr, *ptr1;
141 /* At least 9 bytes of Image Descriptor. */
142 if (bytestream2_get_bytes_left(&s->gb) < 9)
143 return AVERROR_INVALIDDATA;
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;
154 av_dlog(s->avctx, "image x=%d y=%d w=%d h=%d\n", left, top, width, height);
156 if (has_local_palette) {
157 pal_size = 1 << bits_per_pixel;
159 if (bytestream2_get_bytes_left(&s->gb) < pal_size * 3)
160 return AVERROR_INVALIDDATA;
162 gif_read_palette(s, s->local_palette, pal_size);
163 pal = s->local_palette;
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;
170 pal = s->global_palette;
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(&s->picture, s->bg_color);
178 /* otherwise fill with transparent color.
179 * this is necessary since by default picture filled with 0x80808080. */
180 gif_fill(&s->picture, s->trans_color);
184 /* verify that all the image is inside the screen dimensions */
185 if (left + width > s->screen_width ||
186 top + height > s->screen_height)
187 return AVERROR_INVALIDDATA;
188 if (width <= 0 || height <= 0)
189 return AVERROR_INVALIDDATA;
191 /* process disposal method */
192 if (s->gce_prev_disposal == GCE_DISPOSAL_BACKGROUND) {
193 gif_fill_rect(&s->picture, s->stored_bg_color, s->gce_l, s->gce_t, s->gce_w, s->gce_h);
194 } else if (s->gce_prev_disposal == GCE_DISPOSAL_RESTORE) {
195 gif_copy_img_rect(s->stored_img, (uint32_t *)s->picture.data[0],
196 s->picture.linesize[0] / sizeof(uint32_t), s->gce_l, s->gce_t, s->gce_w, s->gce_h);
199 s->gce_prev_disposal = s->gce_disposal;
201 if (s->gce_disposal != GCE_DISPOSAL_NONE) {
202 s->gce_l = left; s->gce_t = top;
203 s->gce_w = width; s->gce_h = height;
205 if (s->gce_disposal == GCE_DISPOSAL_BACKGROUND) {
206 if (s->transparent_color_index >= 0)
207 s->stored_bg_color = s->trans_color;
209 s->stored_bg_color = s->bg_color;
210 } else if (s->gce_disposal == GCE_DISPOSAL_RESTORE) {
211 av_fast_malloc(&s->stored_img, &s->stored_img_size, s->picture.linesize[0] * s->picture.height);
213 return AVERROR(ENOMEM);
215 gif_copy_img_rect((uint32_t *)s->picture.data[0], s->stored_img,
216 s->picture.linesize[0] / sizeof(uint32_t), left, top, width, height);
220 /* Expect at least 2 bytes: 1 for lzw code size and 1 for block size. */
221 if (bytestream2_get_bytes_left(&s->gb) < 2)
222 return AVERROR_INVALIDDATA;
224 /* now get the image data */
225 code_size = bytestream2_get_byteu(&s->gb);
226 if ((ret = ff_lzw_decode_init(s->lzw, code_size, s->gb.buffer,
227 bytestream2_get_bytes_left(&s->gb), FF_LZW_GIF)) < 0) {
228 av_log(s->avctx, AV_LOG_ERROR, "LZW init failed\n");
232 /* read all the image */
233 linesize = s->picture.linesize[0] / sizeof(uint32_t);
234 ptr1 = (uint32_t *)s->picture.data[0] + top * linesize + left;
238 for (y = 0; y < height; y++) {
239 if (ff_lzw_decode(s->lzw, s->idx_line, width) == 0)
244 for (px = ptr, idx = s->idx_line; px < pr; px++, idx++) {
245 if (*idx != s->transparent_color_index)
249 if (is_interleaved) {
258 ptr = ptr1 + linesize * y1;
267 ptr = ptr1 + linesize;
282 /* read the garbage data until end marker is found */
283 ff_lzw_decode_tail(s->lzw);
285 /* Graphic Control Extension's scope is single frame.
286 * Remove its influence. */
287 s->transparent_color_index = -1;
288 s->gce_disposal = GCE_DISPOSAL_NONE;
293 static int gif_read_extension(GifState *s)
295 int ext_code, ext_len, gce_flags, gce_transparent_index;
297 /* There must be at least 2 bytes:
298 * 1 for extension label and 1 for extension length. */
299 if (bytestream2_get_bytes_left(&s->gb) < 2)
300 return AVERROR_INVALIDDATA;
302 ext_code = bytestream2_get_byteu(&s->gb);
303 ext_len = bytestream2_get_byteu(&s->gb);
305 av_dlog(s->avctx, "ext_code=0x%x len=%d\n", ext_code, ext_len);
308 case GIF_GCE_EXT_LABEL:
312 /* We need at least 5 bytes more: 4 is for extension body
313 * and 1 for next block size. */
314 if (bytestream2_get_bytes_left(&s->gb) < 5)
315 return AVERROR_INVALIDDATA;
317 gce_flags = bytestream2_get_byteu(&s->gb);
318 bytestream2_skipu(&s->gb, 2); // delay during which the frame is shown
319 gce_transparent_index = bytestream2_get_byteu(&s->gb);
320 if (gce_flags & 0x01)
321 s->transparent_color_index = gce_transparent_index;
323 s->transparent_color_index = -1;
324 s->gce_disposal = (gce_flags >> 2) & 0x7;
326 av_dlog(s->avctx, "gce_flags=%x tcolor=%d disposal=%d\n",
328 s->transparent_color_index, s->gce_disposal);
330 if (s->gce_disposal > 3) {
331 s->gce_disposal = GCE_DISPOSAL_NONE;
332 av_dlog(s->avctx, "invalid value in gce_disposal (%d). Using default value of 0.\n", ext_len);
335 ext_len = bytestream2_get_byteu(&s->gb);
339 /* NOTE: many extension blocks can come after */
342 /* There must be at least ext_len bytes and 1 for next block size byte. */
343 if (bytestream2_get_bytes_left(&s->gb) < ext_len + 1)
344 return AVERROR_INVALIDDATA;
346 bytestream2_skipu(&s->gb, ext_len);
347 ext_len = bytestream2_get_byteu(&s->gb);
349 av_dlog(s->avctx, "ext_len1=%d\n", ext_len);
354 static int gif_read_header1(GifState *s)
358 int background_color_index;
360 if (bytestream2_get_bytes_left(&s->gb) < 13)
361 return AVERROR_INVALIDDATA;
363 /* read gif signature */
364 bytestream2_get_bufferu(&s->gb, sig, 6);
365 if (memcmp(sig, gif87a_sig, 6) &&
366 memcmp(sig, gif89a_sig, 6))
367 return AVERROR_INVALIDDATA;
369 /* read screen header */
370 s->transparent_color_index = -1;
371 s->screen_width = bytestream2_get_le16u(&s->gb);
372 s->screen_height = bytestream2_get_le16u(&s->gb);
374 v = bytestream2_get_byteu(&s->gb);
375 s->color_resolution = ((v & 0x70) >> 4) + 1;
376 s->has_global_palette = (v & 0x80);
377 s->bits_per_pixel = (v & 0x07) + 1;
378 background_color_index = bytestream2_get_byteu(&s->gb);
379 n = bytestream2_get_byteu(&s->gb);
381 s->avctx->sample_aspect_ratio.num = n + 15;
382 s->avctx->sample_aspect_ratio.den = 64;
385 av_dlog(s->avctx, "screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
386 s->screen_width, s->screen_height, s->bits_per_pixel,
387 s->has_global_palette);
389 if (s->has_global_palette) {
390 s->background_color_index = background_color_index;
391 n = 1 << s->bits_per_pixel;
392 if (bytestream2_get_bytes_left(&s->gb) < n * 3)
393 return AVERROR_INVALIDDATA;
395 gif_read_palette(s, s->global_palette, n);
396 s->bg_color = s->global_palette[s->background_color_index];
398 s->background_color_index = -1;
403 static int gif_parse_next_image(GifState *s)
405 while (bytestream2_get_bytes_left(&s->gb)) {
406 int code = bytestream2_get_byte(&s->gb);
409 av_dlog(s->avctx, "code=%02x '%c'\n", code, code);
412 case GIF_IMAGE_SEPARATOR:
413 return gif_read_image(s);
414 case GIF_EXTENSION_INTRODUCER:
415 if ((ret = gif_read_extension(s)) < 0)
422 /* erroneous block label */
423 return AVERROR_INVALIDDATA;
429 static av_cold int gif_decode_init(AVCodecContext *avctx)
431 GifState *s = avctx->priv_data;
435 avctx->pix_fmt = AV_PIX_FMT_RGB32;
436 avcodec_get_frame_defaults(&s->picture);
437 avctx->coded_frame= &s->picture;
438 s->picture.data[0] = NULL;
439 ff_lzw_decode_open(&s->lzw);
443 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
445 GifState *s = avctx->priv_data;
446 AVFrame *picture = data;
449 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
451 s->picture.pts = avpkt->pts;
452 s->picture.pkt_pts = avpkt->pts;
453 s->picture.pkt_dts = avpkt->dts;
454 s->picture.pkt_duration = avpkt->duration;
456 if (avpkt->size >= 6) {
457 s->keyframe = memcmp(avpkt->data, gif87a_sig, 6) == 0 ||
458 memcmp(avpkt->data, gif89a_sig, 6) == 0;
465 if ((ret = gif_read_header1(s)) < 0)
468 if ((ret = av_image_check_size(s->screen_width, s->screen_height, 0, avctx)) < 0)
470 avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
472 if (s->picture.data[0])
473 avctx->release_buffer(avctx, &s->picture);
475 if ((ret = ff_get_buffer(avctx, &s->picture)) < 0) {
476 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
480 av_fast_malloc(&s->idx_line, &s->idx_line_size, s->screen_width);
482 return AVERROR(ENOMEM);
484 s->picture.pict_type = AV_PICTURE_TYPE_I;
485 s->picture.key_frame = 1;
488 if (!s->keyframe_ok) {
489 av_log(avctx, AV_LOG_ERROR, "cannot decode frame without keyframe\n");
490 return AVERROR_INVALIDDATA;
493 if ((ret = avctx->reget_buffer(avctx, &s->picture)) < 0) {
494 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
498 s->picture.pict_type = AV_PICTURE_TYPE_P;
499 s->picture.key_frame = 0;
502 ret = gif_parse_next_image(s);
506 *picture = s->picture;
512 static av_cold int gif_decode_close(AVCodecContext *avctx)
514 GifState *s = avctx->priv_data;
516 ff_lzw_decode_close(&s->lzw);
517 if(s->picture.data[0])
518 avctx->release_buffer(avctx, &s->picture);
520 av_freep(&s->idx_line);
521 av_freep(&s->stored_img);
526 static const AVOption options[] = {
527 { "trans_color", "color value (ARGB) that is used instead of transparent color",
528 offsetof(GifState, trans_color), AV_OPT_TYPE_INT,
529 {.i64 = GIF_TRANSPARENT_COLOR}, 0, 0xffffffff,
530 AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_VIDEO_PARAM },
534 static const AVClass decoder_class = {
535 .class_name = "gif decoder",
536 .item_name = av_default_item_name,
538 .version = LIBAVUTIL_VERSION_INT,
539 .category = AV_CLASS_CATEGORY_DECODER,
542 AVCodec ff_gif_decoder = {
544 .type = AVMEDIA_TYPE_VIDEO,
545 .id = AV_CODEC_ID_GIF,
546 .priv_data_size = sizeof(GifState),
547 .init = gif_decode_init,
548 .close = gif_decode_close,
549 .decode = gif_decode_frame,
550 .capabilities = CODEC_CAP_DR1,
551 .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
552 .priv_class = &decoder_class,