From patchwork Sun Dec 9 11:16:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 11348 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 58EE944D86E for ; Sun, 9 Dec 2018 13:24:34 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A872068A9E7; Sun, 9 Dec 2018 13:24:24 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5122168A9EA for ; Sun, 9 Dec 2018 13:24:18 +0200 (EET) Received: by mail-wr1-f65.google.com with SMTP id x10so7710479wrs.8 for ; Sun, 09 Dec 2018 03:24:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=TYRGzonvFFhb8fmU3e6HoyF6lo5EYFaD13jWz2Onlww=; b=MG3FCxTZGRMTP2ZHPjMQ/tizydqWjAjTk4/V01FpHbZ1DZZ9kSe30AJ0Hg5VML/U/f 2IIkTHsOB8HGIh2/PCGSA7ePCVrYsX8DohK/2aTrHRLw1/9YwrtGj8CtmzK3cVsbi77h rfBivftWjF8f4YwkZItfGyPYNkgehE9CmRPixV1mNi5EUTGOOVRfhymRahfAv99rIZV0 UZruvvyQ/wNlvy27ZzIvO9+n0mGIS2s1MQc9Ke3+Zyw+FM3mGX8iou4J7Z/sTWTXgubt If2fFlEGWnyeacDD/KOjvUS4SALR0j2NVP3BEXpy6z35vCsSFwR7X/nHZxFoKZkLotU2 LFyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=TYRGzonvFFhb8fmU3e6HoyF6lo5EYFaD13jWz2Onlww=; b=LulvzDS95Rb0kb2ssfF1QDrx9loJ+bOh/egF0l0rTdXsTuOEAQKUVBjSvC4Y8oS1n2 ugw1dv9dnPvquMoQbjIZWAgl0i7/6c9ulMUg4KeoDMjRj5f6XgDJHJIbyzlkrOyhfevB kRaSHK+2gQimwKc29iTJBoKsFmn5lzzZ9DZWQb2/GrRQOQLr/DB3CCmSXxz0IaAR3bxx YVD0/EBTELVuGPMv15mxBvPZ/MjHNE8yjLSW1dH6ZKiHVs3ss6h58Sb9hloaRFdTgS2B MwXAfrTc4t+xIM6hsDDgiq1W6bSD8C6WLiBBC+c7UDsKjKRITOm8ktvx2/WmJV7EIP85 Md9w== X-Gm-Message-State: AA+aEWZ6l0UAapWYvwTiqQs7HrriU2DJfoaVZk+cW3Uxzin1amcmJukt GFSsrsOHsvLIBIKLmUVQ+OmDEjA6 X-Google-Smtp-Source: AFSGD/WD8iYQi/UGix+H/8TYX+wOAveUS7aPo1YD9PwnHn6o7TdhMIG76B1+jzfXRUNkF9+77f3gbQ== X-Received: by 2002:adf:a1d2:: with SMTP id v18mr6372159wrv.87.1544354192820; Sun, 09 Dec 2018 03:16:32 -0800 (PST) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id j33sm16899466wre.91.2018.12.09.03.16.31 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 09 Dec 2018 03:16:31 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 9 Dec 2018 12:16:21 +0100 Message-Id: <20181209111621.4974-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 1/2] avcodec: add gif parser X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- libavcodec/Makefile | 1 + libavcodec/gif_parser.c | 188 ++++++++++++++++++++++++++++++++++++++++ libavcodec/parsers.c | 1 + 3 files changed, 190 insertions(+) create mode 100644 libavcodec/gif_parser.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 5feadac729..d53b8ff330 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1026,6 +1026,7 @@ OBJS-$(CONFIG_DVDSUB_PARSER) += dvdsub_parser.o OBJS-$(CONFIG_FLAC_PARSER) += flac_parser.o flacdata.o flac.o \ vorbis_data.o OBJS-$(CONFIG_G729_PARSER) += g729_parser.o +OBJS-$(CONFIG_GIF_PARSER) += gif_parser.o OBJS-$(CONFIG_GSM_PARSER) += gsm_parser.o OBJS-$(CONFIG_H261_PARSER) += h261_parser.o OBJS-$(CONFIG_H263_PARSER) += h263_parser.o diff --git a/libavcodec/gif_parser.c b/libavcodec/gif_parser.c new file mode 100644 index 0000000000..9d31dc7e48 --- /dev/null +++ b/libavcodec/gif_parser.c @@ -0,0 +1,188 @@ +/* + * GIF parser + * Copyright (c) 2018 Paul B Mahol + * + * 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 + */ + +/** + * @file + * GIF parser + */ + +#include "libavutil/avassert.h" +#include "libavutil/bswap.h" +#include "libavutil/common.h" + +#include "gif.h" +#include "parser.h" + +typedef enum GIFParseStates { + GIF_HEADER = 1, + GIF_EXTENSION, + GIF_EXTENSION_BLOCK, + GIF_IMAGE, + GIF_IMAGE_BLOCK, +} gif_states; + +typedef struct GIFParseContext { + ParseContext pc; + unsigned found_sig; + int found_start; + int found_end; + int index; + int state; + int gct_flag; + int gct_size; + int block_size; + int etype; + int delay; +} GIFParseContext; + +static int gif_find_frame_end(GIFParseContext *g, const uint8_t *buf, + int buf_size, void *logctx) +{ + int index, next = END_NOT_FOUND; + + for (index = 0; index < buf_size; index++) { + if (!g->state) { + if (!memcmp(buf + index, gif87a_sig, 6) || + !memcmp(buf + index, gif89a_sig, 6)) { + g->state = GIF_HEADER; + g->found_sig++; + } else if (buf[index] == GIF_EXTENSION_INTRODUCER) { + g->state = GIF_EXTENSION; + g->found_start = 1; + } else if (buf[index] == GIF_IMAGE_SEPARATOR) { + g->state = GIF_IMAGE; + } else if (buf[index] == GIF_TRAILER) { + g->state = 0; + g->found_end = 1; + g->found_sig = 0; + } else { + av_assert0(0); + } + } + + if (g->state == GIF_HEADER) { + if (g->index == 10) { + g->gct_flag = !!(buf[index] & 0x80); + g->gct_size = 3 * (1 << ((buf[index] & 0x07) + 1)); + } + if (g->index >= 12 + g->gct_flag * g->gct_size) { + g->state = 0; + g->index = 0; + g->gct_flag = 0; + g->gct_size = 0; + continue; + } + g->index++; + } else if (g->state == GIF_EXTENSION) { + if (g->found_start && g->found_end && g->found_sig) { + next = index; + g->found_start = 0; + g->found_end = 0; + g->index = 0; + g->gct_flag = 0; + g->gct_size = 0; + g->state = 0; + break; + } + if (g->index == 1) { + g->etype = buf[index]; + } + if (g->index >= 2) { + g->block_size = buf[index]; + g->index = 0; + g->state = GIF_EXTENSION_BLOCK; + continue; + } + g->index++; + } else if (g->state == GIF_IMAGE_BLOCK) { + if (!g->index) + g->block_size = buf[index]; + if (g->index >= g->block_size) { + g->index = 0; + if (!g->block_size) { + g->state = 0; + g->found_end = 1; + } + continue; + } + g->index++; + } else if (g->state == GIF_EXTENSION_BLOCK) { + if (g->etype == GIF_GCE_EXT_LABEL) { + if (g->index == 0) + g->delay = 0; + if (g->index >= 1 && g->index <= 2) { + g->delay |= buf[index] << (8 * (g->index - 1)); + } + } + if (g->index >= g->block_size) { + g->block_size = buf[index]; + g->index = 0; + if (!g->block_size) + g->state = 0; + continue; + } + g->index++; + } else if (g->state == GIF_IMAGE) { + if (g->index == 8) { + g->gct_flag = !!(buf[index] & 0x80); + g->gct_size = 3 * (1 << ((buf[index] & 0x07) + 1)); + } + if (g->index >= 10 + g->gct_flag * g->gct_size) { + g->state = GIF_IMAGE_BLOCK; + g->index = 0; + g->gct_flag = 0; + g->gct_size = 0; + continue; + } + g->index++; + } + } + + return next; +} + +static int gif_parse(AVCodecParserContext *s, AVCodecContext *avctx, + const uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size) +{ + GIFParseContext *g = s->priv_data; + int next; + + next = gif_find_frame_end(g, buf, buf_size, avctx); + if (ff_combine_frame(&g->pc, next, &buf, &buf_size) < 0) { + *poutbuf = NULL; + *poutbuf_size = 0; + return buf_size; + } + + s->duration = g->delay; + + *poutbuf = buf; + *poutbuf_size = buf_size; + return next; +} + +AVCodecParser ff_gif_parser = { + .codec_ids = { AV_CODEC_ID_GIF }, + .priv_data_size = sizeof(GIFParseContext), + .parser_parse = gif_parse, + .parser_close = ff_parse_close, +}; diff --git a/libavcodec/parsers.c b/libavcodec/parsers.c index f01cad4c84..eca95787ae 100644 --- a/libavcodec/parsers.c +++ b/libavcodec/parsers.c @@ -41,6 +41,7 @@ extern AVCodecParser ff_dvdsub_parser; extern AVCodecParser ff_dvd_nav_parser; extern AVCodecParser ff_flac_parser; extern AVCodecParser ff_g729_parser; +extern AVCodecParser ff_gif_parser; extern AVCodecParser ff_gsm_parser; extern AVCodecParser ff_h261_parser; extern AVCodecParser ff_h263_parser;