2006-10-22 18:05:03 +03:00
|
|
|
/*
|
|
|
|
* GIF decoder
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2003 Fabrice Bellard
|
|
|
|
* Copyright (c) 2006 Baptiste Coudurier
|
2012-11-30 11:58:53 +03:00
|
|
|
* Copyright (c) 2012 Vitaliy E Sugrobov
|
2006-10-22 18:05:03 +03:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
//#define DEBUG
|
|
|
|
|
2011-02-07 15:37:08 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
2012-11-30 11:58:53 +03:00
|
|
|
#include "libavutil/opt.h"
|
2006-10-22 18:05:03 +03:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "bytestream.h"
|
2012-11-10 15:22:56 +03:00
|
|
|
#include "internal.h"
|
2006-10-26 07:15:48 +03:00
|
|
|
#include "lzw.h"
|
2012-11-30 11:58:55 +03:00
|
|
|
#include "gif.h"
|
2006-10-22 18:05:03 +03:00
|
|
|
|
2012-11-30 11:58:53 +03:00
|
|
|
/* This value is intentionally set to "transparent white" color.
|
|
|
|
* It is much better to have white background instead of black
|
|
|
|
* when gif image converted to format which not support transparency.
|
|
|
|
*/
|
|
|
|
#define GIF_TRANSPARENT_COLOR 0x00ffffff
|
2006-10-22 18:05:03 +03:00
|
|
|
|
|
|
|
typedef struct GifState {
|
2012-11-30 11:58:53 +03:00
|
|
|
const AVClass *class;
|
2006-10-22 19:10:04 +03:00
|
|
|
AVFrame picture;
|
2006-10-22 18:05:03 +03:00
|
|
|
int screen_width;
|
|
|
|
int screen_height;
|
2012-11-30 11:58:53 +03:00
|
|
|
int has_global_palette;
|
2006-10-22 18:05:03 +03:00
|
|
|
int bits_per_pixel;
|
2012-11-30 11:58:53 +03:00
|
|
|
uint32_t bg_color;
|
2006-10-22 18:05:03 +03:00
|
|
|
int background_color_index;
|
|
|
|
int transparent_color_index;
|
|
|
|
int color_resolution;
|
2012-11-30 11:58:53 +03:00
|
|
|
/* intermediate buffer for storing color indices
|
|
|
|
* obtained from lzw-encoded data stream */
|
|
|
|
uint8_t *idx_line;
|
2012-12-01 13:53:22 +03:00
|
|
|
int idx_line_size;
|
2006-10-22 18:05:03 +03:00
|
|
|
|
|
|
|
/* after the frame is displayed, the disposal method is used */
|
2012-11-30 11:58:53 +03:00
|
|
|
int gce_prev_disposal;
|
2006-10-22 18:05:03 +03:00
|
|
|
int gce_disposal;
|
2012-11-30 11:58:53 +03:00
|
|
|
/* rectangle describing area that must be disposed */
|
|
|
|
int gce_l, gce_t, gce_w, gce_h;
|
|
|
|
/* depending on disposal method we store either part of the image
|
|
|
|
* drawn on the canvas or background color that
|
|
|
|
* should be used upon disposal */
|
|
|
|
uint32_t * stored_img;
|
2012-12-01 13:53:22 +03:00
|
|
|
int stored_img_size;
|
2012-11-30 11:58:53 +03:00
|
|
|
int stored_bg_color;
|
2006-10-22 18:05:03 +03:00
|
|
|
|
2012-12-05 19:06:54 +03:00
|
|
|
GetByteContext gb;
|
2006-10-22 18:05:03 +03:00
|
|
|
/* LZW compatible decoder */
|
2006-10-26 07:15:48 +03:00
|
|
|
LZWState *lzw;
|
2006-10-22 18:05:03 +03:00
|
|
|
|
|
|
|
/* aux buffers */
|
2012-11-30 11:58:53 +03:00
|
|
|
uint32_t global_palette[256];
|
|
|
|
uint32_t local_palette[256];
|
2007-03-12 14:36:41 +02:00
|
|
|
|
2012-08-07 06:43:55 +03:00
|
|
|
AVCodecContext *avctx;
|
2012-11-30 11:58:53 +03:00
|
|
|
int keyframe;
|
2013-01-24 06:17:58 +03:00
|
|
|
int keyframe_ok;
|
2012-11-30 11:58:53 +03:00
|
|
|
int trans_color; /**< color value that is used instead of transparent color */
|
2006-10-22 18:05:03 +03:00
|
|
|
} GifState;
|
|
|
|
|
2012-12-05 19:06:54 +03:00
|
|
|
static void gif_read_palette(GifState *s, uint32_t *pal, int nb)
|
2012-11-30 11:58:53 +03:00
|
|
|
{
|
2012-12-05 19:06:54 +03:00
|
|
|
int i;
|
2012-11-30 11:58:53 +03:00
|
|
|
|
2012-12-05 19:06:54 +03:00
|
|
|
for (i = 0; i < nb; i++, pal++)
|
|
|
|
*pal = (0xffu << 24) | bytestream2_get_be24u(&s->gb);
|
2012-11-30 11:58:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gif_fill(AVFrame *picture, uint32_t color)
|
|
|
|
{
|
|
|
|
uint32_t *p = (uint32_t *)picture->data[0];
|
|
|
|
uint32_t *p_end = p + (picture->linesize[0] / sizeof(uint32_t)) * picture->height;
|
|
|
|
|
|
|
|
for (; p < p_end; p++)
|
|
|
|
*p = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gif_fill_rect(AVFrame *picture, uint32_t color, int l, int t, int w, int h)
|
|
|
|
{
|
|
|
|
const int linesize = picture->linesize[0] / sizeof(uint32_t);
|
|
|
|
const uint32_t *py = (uint32_t *)picture->data[0] + t * linesize;
|
2012-12-20 20:45:04 +03:00
|
|
|
const uint32_t *pr, *pb = py + h * linesize;
|
2012-11-30 11:58:53 +03:00
|
|
|
uint32_t *px;
|
|
|
|
|
|
|
|
for (; py < pb; py += linesize) {
|
|
|
|
px = (uint32_t *)py + l;
|
|
|
|
pr = px + w;
|
|
|
|
|
|
|
|
for (; px < pr; px++)
|
|
|
|
*px = color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gif_copy_img_rect(const uint32_t *src, uint32_t *dst,
|
|
|
|
int linesize, int l, int t, int w, int h)
|
|
|
|
{
|
|
|
|
const int y_start = t * linesize;
|
2013-01-25 14:51:16 +03:00
|
|
|
const uint32_t *src_px,
|
2012-11-30 11:58:53 +03:00
|
|
|
*src_py = src + y_start,
|
|
|
|
*dst_py = dst + y_start;
|
2013-01-25 01:11:10 +03:00
|
|
|
const uint32_t *src_pb = src_py + h * linesize;
|
2012-11-30 11:58:53 +03:00
|
|
|
uint32_t *dst_px;
|
|
|
|
|
|
|
|
for (; src_py < src_pb; src_py += linesize, dst_py += linesize) {
|
|
|
|
src_px = src_py + l;
|
|
|
|
dst_px = (uint32_t *)dst_py + l;
|
|
|
|
|
2013-01-25 14:51:16 +03:00
|
|
|
memcpy(dst_px, src_px, w * sizeof(uint32_t));
|
2012-11-30 11:58:53 +03:00
|
|
|
}
|
|
|
|
}
|
2006-10-22 18:05:03 +03:00
|
|
|
|
|
|
|
static int gif_read_image(GifState *s)
|
|
|
|
{
|
|
|
|
int left, top, width, height, bits_per_pixel, code_size, flags;
|
2012-11-30 11:58:53 +03:00
|
|
|
int is_interleaved, has_local_palette, y, pass, y1, linesize, pal_size;
|
|
|
|
uint32_t *ptr, *pal, *px, *pr, *ptr1;
|
2012-11-15 00:59:22 +03:00
|
|
|
int ret;
|
2012-11-30 11:58:53 +03:00
|
|
|
uint8_t *idx;
|
2006-10-22 18:05:03 +03:00
|
|
|
|
2012-11-30 11:58:54 +03:00
|
|
|
/* At least 9 bytes of Image Descriptor. */
|
2012-12-05 19:06:54 +03:00
|
|
|
if (bytestream2_get_bytes_left(&s->gb) < 9)
|
2012-11-30 11:58:54 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-12-05 19:06:54 +03:00
|
|
|
left = bytestream2_get_le16u(&s->gb);
|
|
|
|
top = bytestream2_get_le16u(&s->gb);
|
|
|
|
width = bytestream2_get_le16u(&s->gb);
|
|
|
|
height = bytestream2_get_le16u(&s->gb);
|
|
|
|
flags = bytestream2_get_byteu(&s->gb);
|
2006-10-22 18:05:03 +03:00
|
|
|
is_interleaved = flags & 0x40;
|
|
|
|
has_local_palette = flags & 0x80;
|
|
|
|
bits_per_pixel = (flags & 0x07) + 1;
|
2011-01-25 02:16:36 +02:00
|
|
|
|
2012-08-07 06:46:33 +03:00
|
|
|
av_dlog(s->avctx, "image x=%d y=%d w=%d h=%d\n", left, top, width, height);
|
2006-10-22 18:05:03 +03:00
|
|
|
|
|
|
|
if (has_local_palette) {
|
2012-11-30 11:58:53 +03:00
|
|
|
pal_size = 1 << bits_per_pixel;
|
|
|
|
|
2012-12-05 19:06:54 +03:00
|
|
|
if (bytestream2_get_bytes_left(&s->gb) < pal_size * 3)
|
2012-11-30 11:58:53 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-12-05 19:06:54 +03:00
|
|
|
gif_read_palette(s, s->local_palette, pal_size);
|
2012-11-30 11:58:53 +03:00
|
|
|
pal = s->local_palette;
|
2006-10-22 18:05:03 +03:00
|
|
|
} else {
|
2012-11-30 11:58:53 +03:00
|
|
|
if (!s->has_global_palette) {
|
2013-01-03 22:14:40 +03:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "picture doesn't have either global or local palette.\n");
|
2012-11-30 11:58:53 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
pal = s->global_palette;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->keyframe) {
|
|
|
|
if (s->transparent_color_index == -1 && s->has_global_palette) {
|
|
|
|
/* transparency wasn't set before the first frame, fill with background color */
|
|
|
|
gif_fill(&s->picture, s->bg_color);
|
|
|
|
} else {
|
|
|
|
/* otherwise fill with transparent color.
|
|
|
|
* this is necessary since by default picture filled with 0x80808080. */
|
|
|
|
gif_fill(&s->picture, s->trans_color);
|
|
|
|
}
|
2006-10-22 18:05:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* verify that all the image is inside the screen dimensions */
|
|
|
|
if (left + width > s->screen_width ||
|
|
|
|
top + height > s->screen_height)
|
2013-01-03 22:14:40 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2013-01-25 02:03:59 +03:00
|
|
|
if (width <= 0 || height <= 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2006-10-22 18:05:03 +03:00
|
|
|
|
2012-11-30 11:58:53 +03:00
|
|
|
/* process disposal method */
|
|
|
|
if (s->gce_prev_disposal == GCE_DISPOSAL_BACKGROUND) {
|
|
|
|
gif_fill_rect(&s->picture, s->stored_bg_color, s->gce_l, s->gce_t, s->gce_w, s->gce_h);
|
|
|
|
} else if (s->gce_prev_disposal == GCE_DISPOSAL_RESTORE) {
|
|
|
|
gif_copy_img_rect(s->stored_img, (uint32_t *)s->picture.data[0],
|
|
|
|
s->picture.linesize[0] / sizeof(uint32_t), s->gce_l, s->gce_t, s->gce_w, s->gce_h);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->gce_prev_disposal = s->gce_disposal;
|
|
|
|
|
|
|
|
if (s->gce_disposal != GCE_DISPOSAL_NONE) {
|
|
|
|
s->gce_l = left; s->gce_t = top;
|
|
|
|
s->gce_w = width; s->gce_h = height;
|
|
|
|
|
|
|
|
if (s->gce_disposal == GCE_DISPOSAL_BACKGROUND) {
|
2012-12-30 20:21:25 +03:00
|
|
|
if (s->transparent_color_index >= 0)
|
2012-11-30 11:58:53 +03:00
|
|
|
s->stored_bg_color = s->trans_color;
|
|
|
|
else
|
|
|
|
s->stored_bg_color = s->bg_color;
|
|
|
|
} else if (s->gce_disposal == GCE_DISPOSAL_RESTORE) {
|
2012-12-01 13:53:22 +03:00
|
|
|
av_fast_malloc(&s->stored_img, &s->stored_img_size, s->picture.linesize[0] * s->picture.height);
|
|
|
|
if (!s->stored_img)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-11-30 11:58:53 +03:00
|
|
|
|
|
|
|
gif_copy_img_rect((uint32_t *)s->picture.data[0], s->stored_img,
|
|
|
|
s->picture.linesize[0] / sizeof(uint32_t), left, top, width, height);
|
|
|
|
}
|
2006-10-22 20:25:16 +03:00
|
|
|
}
|
2006-10-22 18:05:03 +03:00
|
|
|
|
2012-11-30 11:58:54 +03:00
|
|
|
/* Expect at least 2 bytes: 1 for lzw code size and 1 for block size. */
|
2012-12-05 19:06:54 +03:00
|
|
|
if (bytestream2_get_bytes_left(&s->gb) < 2)
|
2012-11-30 11:58:54 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2006-10-22 18:05:03 +03:00
|
|
|
/* now get the image data */
|
2012-12-05 19:06:54 +03:00
|
|
|
code_size = bytestream2_get_byteu(&s->gb);
|
|
|
|
if ((ret = ff_lzw_decode_init(s->lzw, code_size, s->gb.buffer,
|
|
|
|
bytestream2_get_bytes_left(&s->gb), FF_LZW_GIF)) < 0) {
|
2012-11-15 00:59:22 +03:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "LZW init failed\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2006-10-22 18:05:03 +03:00
|
|
|
|
|
|
|
/* read all the image */
|
2012-11-30 11:58:53 +03:00
|
|
|
linesize = s->picture.linesize[0] / sizeof(uint32_t);
|
|
|
|
ptr1 = (uint32_t *)s->picture.data[0] + top * linesize + left;
|
2006-10-22 18:05:03 +03:00
|
|
|
ptr = ptr1;
|
|
|
|
pass = 0;
|
|
|
|
y1 = 0;
|
|
|
|
for (y = 0; y < height; y++) {
|
2012-11-30 11:58:53 +03:00
|
|
|
if (ff_lzw_decode(s->lzw, s->idx_line, width) == 0)
|
|
|
|
goto decode_tail;
|
|
|
|
|
|
|
|
pr = ptr + width;
|
|
|
|
|
|
|
|
for (px = ptr, idx = s->idx_line; px < pr; px++, idx++) {
|
2012-12-21 20:18:39 +03:00
|
|
|
if (*idx != s->transparent_color_index)
|
|
|
|
*px = pal[*idx];
|
2012-11-30 11:58:53 +03:00
|
|
|
}
|
|
|
|
|
2006-10-22 18:05:03 +03:00
|
|
|
if (is_interleaved) {
|
|
|
|
switch(pass) {
|
|
|
|
default:
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
y1 += 8;
|
|
|
|
ptr += linesize * 8;
|
|
|
|
if (y1 >= height) {
|
2008-03-06 11:45:59 +02:00
|
|
|
y1 = pass ? 2 : 4;
|
|
|
|
ptr = ptr1 + linesize * y1;
|
2006-10-22 18:05:03 +03:00
|
|
|
pass++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
y1 += 4;
|
|
|
|
ptr += linesize * 4;
|
|
|
|
if (y1 >= height) {
|
|
|
|
y1 = 1;
|
|
|
|
ptr = ptr1 + linesize;
|
|
|
|
pass++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
y1 += 2;
|
|
|
|
ptr += linesize * 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ptr += linesize;
|
|
|
|
}
|
|
|
|
}
|
2012-11-30 11:58:53 +03:00
|
|
|
|
|
|
|
decode_tail:
|
2006-10-22 18:05:03 +03:00
|
|
|
/* read the garbage data until end marker is found */
|
2006-10-26 07:15:48 +03:00
|
|
|
ff_lzw_decode_tail(s->lzw);
|
2012-11-30 11:58:53 +03:00
|
|
|
|
|
|
|
/* Graphic Control Extension's scope is single frame.
|
|
|
|
* Remove its influence. */
|
|
|
|
s->transparent_color_index = -1;
|
|
|
|
s->gce_disposal = GCE_DISPOSAL_NONE;
|
|
|
|
|
2006-10-22 18:05:03 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gif_read_extension(GifState *s)
|
|
|
|
{
|
2012-12-05 19:06:54 +03:00
|
|
|
int ext_code, ext_len, gce_flags, gce_transparent_index;
|
2006-10-22 18:05:03 +03:00
|
|
|
|
2012-11-30 11:58:54 +03:00
|
|
|
/* There must be at least 2 bytes:
|
|
|
|
* 1 for extension label and 1 for extension length. */
|
2012-12-05 19:06:54 +03:00
|
|
|
if (bytestream2_get_bytes_left(&s->gb) < 2)
|
2012-11-30 11:58:54 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-12-05 19:06:54 +03:00
|
|
|
ext_code = bytestream2_get_byteu(&s->gb);
|
|
|
|
ext_len = bytestream2_get_byteu(&s->gb);
|
2011-01-25 02:16:36 +02:00
|
|
|
|
2012-08-07 06:46:33 +03:00
|
|
|
av_dlog(s->avctx, "ext_code=0x%x len=%d\n", ext_code, ext_len);
|
2011-01-25 02:16:36 +02:00
|
|
|
|
2006-10-22 18:05:03 +03:00
|
|
|
switch(ext_code) {
|
2012-11-30 11:58:52 +03:00
|
|
|
case GIF_GCE_EXT_LABEL:
|
2006-10-22 18:05:03 +03:00
|
|
|
if (ext_len != 4)
|
|
|
|
goto discard_ext;
|
2012-11-30 11:58:54 +03:00
|
|
|
|
|
|
|
/* We need at least 5 bytes more: 4 is for extension body
|
|
|
|
* and 1 for next block size. */
|
2012-12-05 19:06:54 +03:00
|
|
|
if (bytestream2_get_bytes_left(&s->gb) < 5)
|
2012-11-30 11:58:54 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-12-05 19:06:54 +03:00
|
|
|
gce_flags = bytestream2_get_byteu(&s->gb);
|
|
|
|
bytestream2_skipu(&s->gb, 2); // delay during which the frame is shown
|
|
|
|
gce_transparent_index = bytestream2_get_byteu(&s->gb);
|
2006-10-22 18:05:03 +03:00
|
|
|
if (gce_flags & 0x01)
|
|
|
|
s->transparent_color_index = gce_transparent_index;
|
|
|
|
else
|
|
|
|
s->transparent_color_index = -1;
|
|
|
|
s->gce_disposal = (gce_flags >> 2) & 0x7;
|
2011-01-25 02:16:36 +02:00
|
|
|
|
2012-11-30 11:58:53 +03:00
|
|
|
av_dlog(s->avctx, "gce_flags=%x tcolor=%d disposal=%d\n",
|
|
|
|
gce_flags,
|
2006-10-22 18:05:03 +03:00
|
|
|
s->transparent_color_index, s->gce_disposal);
|
2011-01-25 02:16:36 +02:00
|
|
|
|
2012-11-30 11:58:53 +03:00
|
|
|
if (s->gce_disposal > 3) {
|
|
|
|
s->gce_disposal = GCE_DISPOSAL_NONE;
|
|
|
|
av_dlog(s->avctx, "invalid value in gce_disposal (%d). Using default value of 0.\n", ext_len);
|
|
|
|
}
|
|
|
|
|
2012-12-05 19:06:54 +03:00
|
|
|
ext_len = bytestream2_get_byteu(&s->gb);
|
2006-10-22 18:05:03 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: many extension blocks can come after */
|
|
|
|
discard_ext:
|
2013-01-25 15:13:17 +03:00
|
|
|
while (ext_len) {
|
2012-11-30 11:58:54 +03:00
|
|
|
/* There must be at least ext_len bytes and 1 for next block size byte. */
|
2012-12-05 19:06:54 +03:00
|
|
|
if (bytestream2_get_bytes_left(&s->gb) < ext_len + 1)
|
2012-11-30 11:58:54 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-12-05 19:06:54 +03:00
|
|
|
bytestream2_skipu(&s->gb, ext_len);
|
|
|
|
ext_len = bytestream2_get_byteu(&s->gb);
|
2011-01-25 02:16:36 +02:00
|
|
|
|
2012-08-07 06:46:33 +03:00
|
|
|
av_dlog(s->avctx, "ext_len1=%d\n", ext_len);
|
2006-10-22 18:05:03 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gif_read_header1(GifState *s)
|
|
|
|
{
|
|
|
|
uint8_t sig[6];
|
2006-10-22 18:15:15 +03:00
|
|
|
int v, n;
|
2012-11-30 11:58:53 +03:00
|
|
|
int background_color_index;
|
2006-10-22 18:05:03 +03:00
|
|
|
|
2012-12-05 19:06:54 +03:00
|
|
|
if (bytestream2_get_bytes_left(&s->gb) < 13)
|
2012-08-07 06:55:28 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-03-25 01:57:42 +02:00
|
|
|
|
2006-10-22 18:05:03 +03:00
|
|
|
/* read gif signature */
|
2012-12-05 19:06:54 +03:00
|
|
|
bytestream2_get_bufferu(&s->gb, sig, 6);
|
2013-01-25 15:13:17 +03:00
|
|
|
if (memcmp(sig, gif87a_sig, 6) &&
|
|
|
|
memcmp(sig, gif89a_sig, 6))
|
2012-08-07 06:55:28 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-10-22 18:05:03 +03:00
|
|
|
|
|
|
|
/* read screen header */
|
|
|
|
s->transparent_color_index = -1;
|
2012-12-05 19:06:54 +03:00
|
|
|
s->screen_width = bytestream2_get_le16u(&s->gb);
|
|
|
|
s->screen_height = bytestream2_get_le16u(&s->gb);
|
2006-10-22 18:05:03 +03:00
|
|
|
|
2012-12-05 19:06:54 +03:00
|
|
|
v = bytestream2_get_byteu(&s->gb);
|
2006-10-22 18:05:03 +03:00
|
|
|
s->color_resolution = ((v & 0x70) >> 4) + 1;
|
2012-11-30 11:58:53 +03:00
|
|
|
s->has_global_palette = (v & 0x80);
|
2006-10-22 18:05:03 +03:00
|
|
|
s->bits_per_pixel = (v & 0x07) + 1;
|
2012-12-05 19:06:54 +03:00
|
|
|
background_color_index = bytestream2_get_byteu(&s->gb);
|
|
|
|
n = bytestream2_get_byteu(&s->gb);
|
2012-12-01 15:36:16 +03:00
|
|
|
if (n) {
|
|
|
|
s->avctx->sample_aspect_ratio.num = n + 15;
|
|
|
|
s->avctx->sample_aspect_ratio.den = 64;
|
|
|
|
}
|
2011-01-25 02:16:36 +02:00
|
|
|
|
2012-08-07 06:46:33 +03:00
|
|
|
av_dlog(s->avctx, "screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
|
2006-10-22 18:05:03 +03:00
|
|
|
s->screen_width, s->screen_height, s->bits_per_pixel,
|
2012-11-30 11:58:53 +03:00
|
|
|
s->has_global_palette);
|
2011-01-25 02:16:36 +02:00
|
|
|
|
2012-11-30 11:58:53 +03:00
|
|
|
if (s->has_global_palette) {
|
|
|
|
s->background_color_index = background_color_index;
|
2006-10-22 18:05:03 +03:00
|
|
|
n = 1 << s->bits_per_pixel;
|
2012-12-05 19:06:54 +03:00
|
|
|
if (bytestream2_get_bytes_left(&s->gb) < n * 3)
|
2012-08-07 06:55:28 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2012-11-30 11:58:53 +03:00
|
|
|
|
2012-12-05 19:06:54 +03:00
|
|
|
gif_read_palette(s, s->global_palette, n);
|
2012-11-30 11:58:53 +03:00
|
|
|
s->bg_color = s->global_palette[s->background_color_index];
|
|
|
|
} else
|
|
|
|
s->background_color_index = -1;
|
|
|
|
|
2006-10-22 18:05:03 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-25 15:15:13 +03:00
|
|
|
static int gif_parse_next_image(GifState *s)
|
2006-10-22 18:05:03 +03:00
|
|
|
{
|
2012-12-05 19:06:54 +03:00
|
|
|
while (bytestream2_get_bytes_left(&s->gb)) {
|
|
|
|
int code = bytestream2_get_byte(&s->gb);
|
2012-11-19 12:30:01 +03:00
|
|
|
int ret;
|
2011-01-25 02:16:36 +02:00
|
|
|
|
2012-08-07 06:46:33 +03:00
|
|
|
av_dlog(s->avctx, "code=%02x '%c'\n", code, code);
|
2011-01-25 02:16:36 +02:00
|
|
|
|
2006-10-22 18:05:03 +03:00
|
|
|
switch (code) {
|
2012-11-30 11:58:52 +03:00
|
|
|
case GIF_IMAGE_SEPARATOR:
|
2007-04-21 21:47:30 +03:00
|
|
|
return gif_read_image(s);
|
2012-11-30 11:58:52 +03:00
|
|
|
case GIF_EXTENSION_INTRODUCER:
|
|
|
|
if ((ret = gif_read_extension(s)) < 0)
|
|
|
|
return ret;
|
2006-10-22 18:05:03 +03:00
|
|
|
break;
|
2012-11-30 11:58:52 +03:00
|
|
|
case GIF_TRAILER:
|
2007-04-21 21:47:30 +03:00
|
|
|
/* end of image */
|
2013-01-25 15:15:13 +03:00
|
|
|
return AVERROR_EOF;
|
2006-10-22 18:05:03 +03:00
|
|
|
default:
|
2012-11-30 11:58:52 +03:00
|
|
|
/* erroneous block label */
|
|
|
|
return AVERROR_INVALIDDATA;
|
2006-10-22 18:05:03 +03:00
|
|
|
}
|
|
|
|
}
|
2012-11-30 11:58:52 +03:00
|
|
|
return AVERROR_EOF;
|
2006-10-22 18:05:03 +03:00
|
|
|
}
|
|
|
|
|
2008-03-21 05:11:20 +02:00
|
|
|
static av_cold int gif_decode_init(AVCodecContext *avctx)
|
2006-10-22 18:05:03 +03:00
|
|
|
{
|
|
|
|
GifState *s = avctx->priv_data;
|
|
|
|
|
2007-03-12 14:36:41 +02:00
|
|
|
s->avctx = avctx;
|
|
|
|
|
2012-11-30 11:58:53 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB32;
|
2006-10-22 19:10:04 +03:00
|
|
|
avcodec_get_frame_defaults(&s->picture);
|
|
|
|
avctx->coded_frame= &s->picture;
|
|
|
|
s->picture.data[0] = NULL;
|
2006-10-26 07:15:48 +03:00
|
|
|
ff_lzw_decode_open(&s->lzw);
|
2006-10-22 18:05:03 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-05 19:15:57 +03:00
|
|
|
static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
|
2006-10-22 18:05:03 +03:00
|
|
|
{
|
|
|
|
GifState *s = avctx->priv_data;
|
|
|
|
AVFrame *picture = data;
|
|
|
|
int ret;
|
|
|
|
|
2012-12-05 19:06:54 +03:00
|
|
|
bytestream2_init(&s->gb, avpkt->data, avpkt->size);
|
|
|
|
|
2012-11-30 11:58:53 +03:00
|
|
|
s->picture.pts = avpkt->pts;
|
|
|
|
s->picture.pkt_pts = avpkt->pts;
|
|
|
|
s->picture.pkt_dts = avpkt->dts;
|
|
|
|
s->picture.pkt_duration = avpkt->duration;
|
|
|
|
|
2012-12-05 19:06:54 +03:00
|
|
|
if (avpkt->size >= 6) {
|
|
|
|
s->keyframe = memcmp(avpkt->data, gif87a_sig, 6) == 0 ||
|
|
|
|
memcmp(avpkt->data, gif89a_sig, 6) == 0;
|
2012-11-30 11:58:53 +03:00
|
|
|
} else {
|
|
|
|
s->keyframe = 0;
|
|
|
|
}
|
2006-10-22 18:05:03 +03:00
|
|
|
|
2012-11-30 11:58:53 +03:00
|
|
|
if (s->keyframe) {
|
2013-01-24 06:17:58 +03:00
|
|
|
s->keyframe_ok = 0;
|
2012-11-30 11:58:53 +03:00
|
|
|
if ((ret = gif_read_header1(s)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if ((ret = av_image_check_size(s->screen_width, s->screen_height, 0, avctx)) < 0)
|
|
|
|
return ret;
|
|
|
|
avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
|
|
|
|
|
|
|
|
if (s->picture.data[0])
|
|
|
|
avctx->release_buffer(avctx, &s->picture);
|
|
|
|
|
2012-12-05 17:18:12 +03:00
|
|
|
if ((ret = ff_get_buffer(avctx, &s->picture)) < 0) {
|
2012-11-30 11:58:53 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-25 15:46:57 +03:00
|
|
|
av_fast_malloc(&s->idx_line, &s->idx_line_size, s->screen_width);
|
|
|
|
if (!s->idx_line)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2012-11-30 11:58:53 +03:00
|
|
|
s->picture.pict_type = AV_PICTURE_TYPE_I;
|
|
|
|
s->picture.key_frame = 1;
|
2013-01-24 06:17:58 +03:00
|
|
|
s->keyframe_ok = 1;
|
2012-11-30 11:58:53 +03:00
|
|
|
} else {
|
2013-01-24 06:17:58 +03:00
|
|
|
if (!s->keyframe_ok) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "cannot decode frame without keyframe\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2012-11-30 11:58:53 +03:00
|
|
|
if ((ret = avctx->reget_buffer(avctx, &s->picture)) < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->picture.pict_type = AV_PICTURE_TYPE_P;
|
|
|
|
s->picture.key_frame = 0;
|
2006-10-22 19:10:04 +03:00
|
|
|
}
|
2012-11-30 11:58:53 +03:00
|
|
|
|
2013-01-25 15:15:13 +03:00
|
|
|
ret = gif_parse_next_image(s);
|
2006-10-22 18:05:03 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-01-25 15:15:13 +03:00
|
|
|
|
|
|
|
*picture = s->picture;
|
|
|
|
*got_frame = 1;
|
2006-10-22 18:05:03 +03:00
|
|
|
|
2012-12-05 19:06:54 +03:00
|
|
|
return avpkt->size;
|
2006-10-22 18:05:03 +03:00
|
|
|
}
|
|
|
|
|
2008-03-21 05:11:20 +02:00
|
|
|
static av_cold int gif_decode_close(AVCodecContext *avctx)
|
2006-10-22 19:10:04 +03:00
|
|
|
{
|
|
|
|
GifState *s = avctx->priv_data;
|
|
|
|
|
2006-10-26 07:15:48 +03:00
|
|
|
ff_lzw_decode_close(&s->lzw);
|
2006-10-22 19:10:04 +03:00
|
|
|
if(s->picture.data[0])
|
|
|
|
avctx->release_buffer(avctx, &s->picture);
|
2012-11-30 11:58:53 +03:00
|
|
|
|
|
|
|
av_freep(&s->idx_line);
|
|
|
|
av_freep(&s->stored_img);
|
|
|
|
|
2006-10-22 19:10:04 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-30 11:58:53 +03:00
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "trans_color", "color value (ARGB) that is used instead of transparent color",
|
|
|
|
offsetof(GifState, trans_color), AV_OPT_TYPE_INT,
|
|
|
|
{.i64 = GIF_TRANSPARENT_COLOR}, 0, 0xffffffff,
|
|
|
|
AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_VIDEO_PARAM },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass decoder_class = {
|
|
|
|
.class_name = "gif decoder",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
.category = AV_CLASS_CATEGORY_DECODER,
|
|
|
|
};
|
|
|
|
|
2011-01-25 23:40:11 +02:00
|
|
|
AVCodec ff_gif_decoder = {
|
2011-07-17 13:54:31 +03:00
|
|
|
.name = "gif",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 12:11:04 +03:00
|
|
|
.id = AV_CODEC_ID_GIF,
|
2011-07-17 13:54:31 +03:00
|
|
|
.priv_data_size = sizeof(GifState),
|
|
|
|
.init = gif_decode_init,
|
|
|
|
.close = gif_decode_close,
|
|
|
|
.decode = gif_decode_frame,
|
|
|
|
.capabilities = CODEC_CAP_DR1,
|
2012-04-06 19:19:39 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
|
2012-11-30 11:58:53 +03:00
|
|
|
.priv_class = &decoder_class,
|
2006-10-22 18:05:03 +03:00
|
|
|
};
|