FFmpeg
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 
30 typedef enum GIFParseStates {
36 } gif_states;
37 
38 typedef struct GIFParseContext {
40  unsigned found_sig;
42  int found_end;
43  int index;
44  int state;
45  int gct_flag;
46  int gct_size;
48  int etype;
49  int delay;
50  int keyframe;
52 
53 static int gif_find_frame_end(GIFParseContext *g, const uint8_t *buf,
54  int buf_size, void *logctx)
55 {
56  ParseContext *pc = &g->pc;
57  int index, next = END_NOT_FOUND;
58 
59  for (index = 0; index < buf_size; index++) {
60  if (!g->state) {
61  if (!memcmp(buf + index, gif87a_sig, 6) ||
62  !memcmp(buf + index, gif89a_sig, 6)) {
63  g->state = GIF_HEADER;
64  g->found_sig++;
65  g->keyframe = 1;
66  } else if (buf[index] == GIF_EXTENSION_INTRODUCER) {
67  g->state = GIF_EXTENSION;
68  g->found_start = pc->frame_start_found = 1;
69  } else if (buf[index] == GIF_IMAGE_SEPARATOR) {
70  g->state = GIF_IMAGE;
71  } else if (buf[index] == GIF_TRAILER) {
72  g->state = 0;
73  g->found_end = 1;
74  g->found_sig = 0;
75  } else {
76  g->found_sig = 0;
77  }
78  }
79 
80  if (g->state == GIF_HEADER) {
81  if (g->index == 10) {
82  g->gct_flag = !!(buf[index] & 0x80);
83  g->gct_size = 3 * (1 << ((buf[index] & 0x07) + 1));
84  }
85  if (g->index >= 12 + g->gct_flag * g->gct_size) {
86  g->state = 0;
87  g->index = 0;
88  g->gct_flag = 0;
89  g->gct_size = 0;
90  continue;
91  }
92  g->index++;
93  } else if (g->state == GIF_EXTENSION) {
94  if (g->found_start && g->found_end && g->found_sig) {
95  next = index;
96  g->found_start = pc->frame_start_found = 0;
97  g->found_end = 0;
98  g->index = 0;
99  g->gct_flag = 0;
100  g->gct_size = 0;
101  g->state = 0;
102  break;
103  }
104  if (g->index == 1) {
105  g->etype = buf[index];
106  }
107  if (g->index >= 2) {
108  g->block_size = buf[index];
109  g->index = 0;
110  g->state = GIF_EXTENSION_BLOCK;
111  continue;
112  }
113  g->index++;
114  } else if (g->state == GIF_IMAGE_BLOCK) {
115  if (!g->index)
116  g->block_size = buf[index];
117  if (g->index >= g->block_size) {
118  g->index = 0;
119  if (!g->block_size) {
120  g->state = 0;
121  g->found_end = 1;
122  }
123  continue;
124  }
125  g->index++;
126  } else if (g->state == GIF_EXTENSION_BLOCK) {
127  if (g->etype == GIF_GCE_EXT_LABEL) {
128  if (g->index == 0)
129  g->delay = 0;
130  if (g->index >= 1 && g->index <= 2) {
131  g->delay |= buf[index] << (8 * (g->index - 1));
132  }
133  }
134  if (g->index >= g->block_size) {
135  g->block_size = buf[index];
136  g->index = 0;
137  if (!g->block_size)
138  g->state = 0;
139  continue;
140  }
141  g->index++;
142  } else if (g->state == GIF_IMAGE) {
143  if (g->index == 9) {
144  g->gct_flag = !!(buf[index] & 0x80);
145  g->gct_size = 3 * (1 << ((buf[index] & 0x07) + 1));
146  }
147  if (g->index >= 10 + g->gct_flag * g->gct_size) {
148  g->state = GIF_IMAGE_BLOCK;
149  g->index = 0;
150  g->gct_flag = 0;
151  g->gct_size = 0;
152  continue;
153  }
154  g->index++;
155  }
156  }
157 
158  return next;
159 }
160 
162  const uint8_t **poutbuf, int *poutbuf_size,
163  const uint8_t *buf, int buf_size)
164 {
165  GIFParseContext *g = s->priv_data;
166  int next;
167 
168  *poutbuf_size = 0;
169  *poutbuf = NULL;
170 
171  if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
172  next = buf_size;
173  } else {
174  next = gif_find_frame_end(g, buf, buf_size, avctx);
175  if (ff_combine_frame(&g->pc, next, &buf, &buf_size) < 0) {
176  *poutbuf = NULL;
177  *poutbuf_size = 0;
178  return buf_size;
179  }
180  }
181 
182  s->duration = g->delay ? g->delay : 10;
183  s->key_frame = g->keyframe;
184  s->pict_type = g->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
185  g->keyframe = 0;
186 
187  *poutbuf = buf;
188  *poutbuf_size = buf_size;
189  return next;
190 }
191 
193  .codec_ids = { AV_CODEC_ID_GIF },
194  .priv_data_size = sizeof(GIFParseContext),
195  .parser_parse = gif_parse,
196  .parser_close = ff_parse_close,
197 };
gif_find_frame_end
static int gif_find_frame_end(GIFParseContext *g, const uint8_t *buf, int buf_size, void *logctx)
Definition: gif_parser.c:53
GIFParseStates
GIFParseStates
Definition: gif_parser.c:30
GIFParseContext::index
int index
Definition: gif_parser.c:43
GIF_IMAGE
@ GIF_IMAGE
Definition: gif_parser.c:34
gif_parse
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:161
GIF_TRAILER
#define GIF_TRAILER
Definition: gif.h:42
ff_parse_close
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:288
GIFParseContext::found_start
int found_start
Definition: gif_parser.c:41
GIF_GCE_EXT_LABEL
#define GIF_GCE_EXT_LABEL
Definition: gif.h:45
GIFParseContext::gct_flag
int gct_flag
Definition: gif_parser.c:45
ParseContext
Definition: parser.h:28
GIFParseContext
Definition: gif_parser.c:38
gif89a_sig
static const uint8_t gif89a_sig[6]
Definition: gif.h:35
GIFParseContext::found_end
int found_end
Definition: gif_parser.c:42
GIF_EXTENSION_BLOCK
@ GIF_EXTENSION_BLOCK
Definition: gif_parser.c:33
s
#define s(width, name)
Definition: cbs_vp9.c:198
GIFParseContext::gct_size
int gct_size
Definition: gif_parser.c:46
g
const char * g
Definition: vf_curves.c:127
GIFParseContext::state
int state
Definition: gif_parser.c:44
GIF_IMAGE_SEPARATOR
#define GIF_IMAGE_SEPARATOR
Definition: gif.h:44
NULL
#define NULL
Definition: coverity.c:32
GIF_HEADER
@ GIF_HEADER
Definition: gif_parser.c:31
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
ParseContext::frame_start_found
int frame_start_found
Definition: parser.h:34
index
int index
Definition: gxfenc.c:89
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2934
gif.h
GIFParseContext::etype
int etype
Definition: gif_parser.c:48
ff_combine_frame
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:203
AV_CODEC_ID_GIF
@ AV_CODEC_ID_GIF
Definition: codec_id.h:149
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2808
gif87a_sig
static const uint8_t gif87a_sig[6]
Definition: gif.h:34
GIF_EXTENSION_INTRODUCER
#define GIF_EXTENSION_INTRODUCER
Definition: gif.h:43
GIFParseContext::block_size
int block_size
Definition: gif_parser.c:47
parser.h
AVCodecParserContext
Definition: avcodec.h:2774
AVCodecContext
main external API structure.
Definition: avcodec.h:441
GIFParseContext::keyframe
int keyframe
Definition: gif_parser.c:50
GIF_IMAGE_BLOCK
@ GIF_IMAGE_BLOCK
Definition: gif_parser.c:35
GIF_EXTENSION
@ GIF_EXTENSION
Definition: gif_parser.c:32
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
GIFParseContext::found_sig
unsigned found_sig
Definition: gif_parser.c:40
GIFParseContext::pc
ParseContext pc
Definition: gif_parser.c:39
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
AVCodecParser
Definition: avcodec.h:2933
GIFParseContext::delay
int delay
Definition: gif_parser.c:49
ff_gif_parser
const AVCodecParser ff_gif_parser
Definition: gif_parser.c:192