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  if (g->state != GIF_EXTENSION_BLOCK && g->found_start &&
71  g->found_end && g->found_sig) {
72  next = index;
73  g->found_start = pc->frame_start_found = 1;
74  g->found_end = 0;
75  g->index = 0;
76  g->gct_flag = 0;
77  g->gct_size = 0;
78  g->state = GIF_IMAGE;
79  break;
80  }
81  g->state = GIF_IMAGE;
82  } else if (buf[index] == GIF_TRAILER) {
83  g->state = 0;
84  g->found_end = 1;
85  g->found_sig = 0;
86  } else {
87  g->found_sig = 0;
88  }
89  }
90 
91  if (g->state == GIF_HEADER) {
92  if (g->index == 10) {
93  g->gct_flag = !!(buf[index] & 0x80);
94  g->gct_size = 3 * (1 << ((buf[index] & 0x07) + 1));
95  }
96  if (g->index >= 12 + g->gct_flag * g->gct_size) {
97  g->state = 0;
98  g->index = 0;
99  g->gct_flag = 0;
100  g->gct_size = 0;
101  continue;
102  }
103  g->index++;
104  } else if (g->state == GIF_EXTENSION) {
105  if (g->found_start && g->found_end && g->found_sig) {
106  next = index;
107  g->found_start = pc->frame_start_found = 0;
108  g->found_end = 0;
109  g->index = 0;
110  g->gct_flag = 0;
111  g->gct_size = 0;
112  g->state = 0;
113  break;
114  }
115  if (g->index == 1) {
116  g->etype = buf[index];
117  }
118  if (g->index >= 2) {
119  g->block_size = buf[index];
120  g->index = 0;
121  g->state = GIF_EXTENSION_BLOCK;
122  continue;
123  }
124  g->index++;
125  } else if (g->state == GIF_IMAGE_BLOCK) {
126  if (!g->index)
127  g->block_size = buf[index];
128  if (g->index >= g->block_size) {
129  g->index = 0;
130  if (!g->block_size) {
131  g->state = 0;
132  g->found_end = 1;
133  }
134  continue;
135  }
136  g->index++;
137  } else if (g->state == GIF_EXTENSION_BLOCK) {
138  if (g->etype == GIF_GCE_EXT_LABEL) {
139  if (g->index == 0)
140  g->delay = 0;
141  if (g->index >= 1 && g->index <= 2) {
142  g->delay |= buf[index] << (8 * (g->index - 1));
143  }
144  }
145  if (g->index >= g->block_size) {
146  g->block_size = buf[index];
147  g->index = 0;
148  if (!g->block_size)
149  g->state = 0;
150  continue;
151  }
152  g->index++;
153  } else if (g->state == GIF_IMAGE) {
154  if (g->index == 9) {
155  g->gct_flag = !!(buf[index] & 0x80);
156  g->gct_size = 3 * (1 << ((buf[index] & 0x07) + 1));
157  }
158  if (g->index >= 10 + g->gct_flag * g->gct_size) {
159  g->state = GIF_IMAGE_BLOCK;
160  g->index = 0;
161  g->gct_flag = 0;
162  g->gct_size = 0;
163  continue;
164  }
165  g->index++;
166  }
167  }
168 
169  return next;
170 }
171 
173  const uint8_t **poutbuf, int *poutbuf_size,
174  const uint8_t *buf, int buf_size)
175 {
176  GIFParseContext *g = s->priv_data;
177  int next;
178 
179  *poutbuf_size = 0;
180  *poutbuf = NULL;
181 
182  if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
183  next = buf_size;
184  } else {
185  next = gif_find_frame_end(g, buf, buf_size, avctx);
186  if (ff_combine_frame(&g->pc, next, &buf, &buf_size) < 0) {
187  *poutbuf = NULL;
188  *poutbuf_size = 0;
189  return buf_size;
190  }
191  }
192 
193  s->duration = g->delay ? g->delay : 10;
194  s->key_frame = g->keyframe;
195  s->pict_type = g->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
196  g->keyframe = 0;
197 
198  *poutbuf = buf;
199  *poutbuf_size = buf_size;
200  return next;
201 }
202 
204  .codec_ids = { AV_CODEC_ID_GIF },
205  .priv_data_size = sizeof(GIFParseContext),
206  .parser_parse = gif_parse,
207  .parser_close = ff_parse_close,
208 };
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:172
GIF_TRAILER
#define GIF_TRAILER
Definition: gif.h:42
ff_parse_close
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:289
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:2867
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:2741
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:2707
AVCodecContext
main external API structure.
Definition: avcodec.h:445
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:2866
GIFParseContext::delay
int delay
Definition: gif_parser.c:49
ff_gif_parser
const AVCodecParser ff_gif_parser
Definition: gif_parser.c:203