FFmpeg
Loading...
Searching...
No Matches
gif_parser.c
Go to the documentation of this file.
1/*
2 * GIF parser
3 * Copyright (c) 2018 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22/**
23 * @file
24 * GIF parser
25 */
26
27#include "gif.h"
28#include "parser.h"
29#include "parser_internal.h"
30
38
53
54static int gif_find_frame_end(GIFParseContext *g, const uint8_t *buf,
55 int buf_size, void *logctx)
56{
57 ParseContext *pc = &g->pc;
58 int index, next = END_NOT_FOUND;
59
60 for (index = 0; index < buf_size; index++) {
61 if (!g->state) {
62 if (!memcmp(buf + index, gif87a_sig, 6) ||
63 !memcmp(buf + index, gif89a_sig, 6)) {
64 g->state = GIF_HEADER;
65 g->found_sig++;
66 g->keyframe = 1;
67 } else if (buf[index] == GIF_EXTENSION_INTRODUCER) {
68 g->state = GIF_EXTENSION;
69 g->found_start = pc->frame_start_found = 1;
70 } else if (buf[index] == GIF_IMAGE_SEPARATOR) {
71 if (g->state != GIF_EXTENSION_BLOCK && g->found_start &&
72 g->found_end && g->found_sig) {
73 next = index;
74 g->found_start = pc->frame_start_found = 1;
75 g->found_end = 0;
76 g->index = 0;
77 g->gct_flag = 0;
78 g->gct_size = 0;
79 g->state = GIF_IMAGE;
80 break;
81 }
82 g->state = GIF_IMAGE;
83 } else if (buf[index] == GIF_TRAILER) {
84 g->state = 0;
85 g->found_end = 1;
86 g->found_sig = 0;
87 } else {
88 g->found_sig = 0;
89 }
90 }
91
92 if (g->state == GIF_HEADER) {
93 if (g->index == 10) {
94 g->gct_flag = !!(buf[index] & 0x80);
95 g->gct_size = 3 * (1 << ((buf[index] & 0x07) + 1));
96 }
97 if (g->index >= 12 + g->gct_flag * g->gct_size) {
98 g->state = 0;
99 g->index = 0;
100 g->gct_flag = 0;
101 g->gct_size = 0;
102 continue;
103 }
104 g->index++;
105 } else if (g->state == GIF_EXTENSION) {
106 if (g->found_start && g->found_end && g->found_sig) {
107 next = index;
108 g->found_start = pc->frame_start_found = 0;
109 g->found_end = 0;
110 g->index = 0;
111 g->gct_flag = 0;
112 g->gct_size = 0;
113 g->state = 0;
114 break;
115 }
116 if (g->index == 1) {
117 g->etype = buf[index];
118 }
119 if (g->index >= 2) {
120 g->block_size = buf[index];
121 g->index = 0;
122 g->state = GIF_EXTENSION_BLOCK;
123 continue;
124 }
125 g->index++;
126 } else if (g->state == GIF_IMAGE_BLOCK) {
127 if (!g->index)
128 g->block_size = buf[index];
129 if (g->index >= g->block_size) {
130 g->index = 0;
131 if (!g->block_size) {
132 g->state = 0;
133 g->found_end = 1;
134 }
135 continue;
136 }
137 g->index++;
138 } else if (g->state == GIF_EXTENSION_BLOCK) {
139 if (g->etype == GIF_GCE_EXT_LABEL) {
140 if (g->index == 0)
141 g->delay = 0;
142 if (g->index >= 1 && g->index <= 2) {
143 g->delay |= buf[index] << (8 * (g->index - 1));
144 }
145 }
146 if (g->index >= g->block_size) {
147 g->block_size = buf[index];
148 g->index = 0;
149 if (!g->block_size)
150 g->state = 0;
151 continue;
152 }
153 g->index++;
154 } else if (g->state == GIF_IMAGE) {
155 if (g->index == 9) {
156 g->gct_flag = !!(buf[index] & 0x80);
157 g->gct_size = 3 * (1 << ((buf[index] & 0x07) + 1));
158 }
159 if (g->index >= 10 + g->gct_flag * g->gct_size) {
160 g->state = GIF_IMAGE_BLOCK;
161 g->index = 0;
162 g->gct_flag = 0;
163 g->gct_size = 0;
164 continue;
165 }
166 g->index++;
167 }
168 }
169
170 return next;
171}
172
174 const uint8_t **poutbuf, int *poutbuf_size,
175 const uint8_t *buf, int buf_size)
176{
177 GIFParseContext *g = s->priv_data;
178 int next;
179
180 *poutbuf_size = 0;
181 *poutbuf = NULL;
182
183 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
184 next = buf_size;
185 } else {
186 next = gif_find_frame_end(g, buf, buf_size, avctx);
187 if (ff_combine_frame(&g->pc, next, &buf, &buf_size) < 0) {
188 *poutbuf = NULL;
189 *poutbuf_size = 0;
190 return buf_size;
191 }
192 }
193
194 s->duration = g->delay ? g->delay : 10;
195 s->key_frame = g->keyframe;
196 s->pict_type = g->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
197 g->keyframe = 0;
198
199 *poutbuf = buf;
200 *poutbuf_size = buf_size;
201 return next;
202}
203
206 .priv_data_size = sizeof(GIFParseContext),
207 .parse = gif_parse,
209};
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition apv_parser.c:92
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2651
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
GIF format definitions.
#define GIF_IMAGE_SEPARATOR
Definition gif.h:46
static attribute_nonstring const uint8_t gif89a_sig[6]
Definition gif.h:37
#define GIF_TRAILER
Definition gif.h:44
#define GIF_EXTENSION_INTRODUCER
Definition gif.h:45
#define GIF_GCE_EXT_LABEL
Definition gif.h:47
static attribute_nonstring const uint8_t gif87a_sig[6]
Definition gif.h:36
gif_states
Definition gif_parser.c:31
@ GIF_EXTENSION_BLOCK
Definition gif_parser.c:34
@ GIF_EXTENSION
Definition gif_parser.c:33
@ GIF_IMAGE_BLOCK
Definition gif_parser.c:36
@ GIF_HEADER
Definition gif_parser.c:32
@ GIF_IMAGE
Definition gif_parser.c:35
const FFCodecParser ff_gif_parser
Definition gif_parser.c:204
static int gif_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition gif_parser.c:173
static int gif_find_frame_end(GIFParseContext *g, const uint8_t *buf, int buf_size, void *logctx)
Definition gif_parser.c:54
@ AV_CODEC_ID_GIF
Definition codec_id.h:147
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
int index
Definition gxfenc.c:90
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition parser.c:300
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition parser.c:213
#define END_NOT_FOUND
Definition parser.h:40
#define PARSER_CODEC_LIST(...)
main external API structure.
Definition avcodec.h:443
unsigned found_sig
Definition gif_parser.c:41
ParseContext pc
Definition gif_parser.c:40
int frame_start_found
Definition parser.h:34
const char * g
Definition vf_curves.c:128