3 * Copyright (c) 2018 Paul B Mahol
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/avassert.h"
28 #include "libavutil/bswap.h"
29 #include "libavutil/common.h"
34 typedef enum GIFParseStates {
42 typedef struct GIFParseContext {
56 static int gif_find_frame_end(GIFParseContext *g, const uint8_t *buf,
57 int buf_size, void *logctx)
59 int index, next = END_NOT_FOUND;
61 for (index = 0; index < buf_size; index++) {
63 if (!memcmp(buf + index, gif87a_sig, 6) ||
64 !memcmp(buf + index, gif89a_sig, 6)) {
65 g->state = GIF_HEADER;
67 } else if (buf[index] == GIF_EXTENSION_INTRODUCER) {
68 g->state = GIF_EXTENSION;
70 } else if (buf[index] == GIF_IMAGE_SEPARATOR) {
72 } else if (buf[index] == GIF_TRAILER) {
81 if (g->state == GIF_HEADER) {
83 g->gct_flag = !!(buf[index] & 0x80);
84 g->gct_size = 3 * (1 << ((buf[index] & 0x07) + 1));
86 if (g->index >= 12 + g->gct_flag * g->gct_size) {
94 } else if (g->state == GIF_EXTENSION) {
95 if (g->found_start && g->found_end && g->found_sig) {
106 g->etype = buf[index];
109 g->block_size = buf[index];
111 g->state = GIF_EXTENSION_BLOCK;
115 } else if (g->state == GIF_IMAGE_BLOCK) {
117 g->block_size = buf[index];
118 if (g->index >= g->block_size) {
120 if (!g->block_size) {
127 } else if (g->state == GIF_EXTENSION_BLOCK) {
128 if (g->etype == GIF_GCE_EXT_LABEL) {
131 if (g->index >= 1 && g->index <= 2) {
132 g->delay |= buf[index] << (8 * (g->index - 1));
135 if (g->index >= g->block_size) {
136 g->block_size = buf[index];
143 } else if (g->state == GIF_IMAGE) {
145 g->gct_flag = !!(buf[index] & 0x80);
146 g->gct_size = 3 * (1 << ((buf[index] & 0x07) + 1));
148 if (g->index >= 10 + g->gct_flag * g->gct_size) {
149 g->state = GIF_IMAGE_BLOCK;
162 static int gif_parse(AVCodecParserContext *s, AVCodecContext *avctx,
163 const uint8_t **poutbuf, int *poutbuf_size,
164 const uint8_t *buf, int buf_size)
166 GIFParseContext *g = s->priv_data;
169 next = gif_find_frame_end(g, buf, buf_size, avctx);
170 if (ff_combine_frame(&g->pc, next, &buf, &buf_size) < 0) {
176 s->duration = g->delay;
179 *poutbuf_size = buf_size;
183 AVCodecParser ff_gif_parser = {
184 .codec_ids = { AV_CODEC_ID_GIF },
185 .priv_data_size = sizeof(GIFParseContext),
186 .parser_parse = gif_parse,
187 .parser_close = ff_parse_close,