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 #include "parser_internal.h"
30 
31 typedef enum GIFParseStates {
37 } gif_states;
38 
39 typedef struct GIFParseContext {
41  unsigned found_sig;
43  int found_end;
44  int index;
45  int state;
46  int gct_flag;
47  int gct_size;
49  int etype;
50  int delay;
51  int keyframe;
53 
54 static 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 };
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:54
GIFParseStates
GIFParseStates
Definition: gif_parser.c:31
GIFParseContext::index
int index
Definition: gif_parser.c:44
GIF_IMAGE
@ GIF_IMAGE
Definition: gif_parser.c:35
ff_parse_close
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:298
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:173
GIF_TRAILER
#define GIF_TRAILER
Definition: gif.h:42
parser_internal.h
GIFParseContext::found_start
int found_start
Definition: gif_parser.c:42
GIF_GCE_EXT_LABEL
#define GIF_GCE_EXT_LABEL
Definition: gif.h:45
GIFParseContext::gct_flag
int gct_flag
Definition: gif_parser.c:46
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:136
ParseContext
Definition: parser.h:28
ff_gif_parser
const FFCodecParser ff_gif_parser
Definition: gif_parser.c:204
GIFParseContext
Definition: gif_parser.c:39
gif89a_sig
static const uint8_t gif89a_sig[6]
Definition: gif.h:35
GIFParseContext::found_end
int found_end
Definition: gif_parser.c:43
GIF_EXTENSION_BLOCK
@ GIF_EXTENSION_BLOCK
Definition: gif_parser.c:34
s
#define s(width, name)
Definition: cbs_vp9.c:198
GIFParseContext::gct_size
int gct_size
Definition: gif_parser.c:47
g
const char * g
Definition: vf_curves.c:128
GIFParseContext::state
int state
Definition: gif_parser.c:45
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:32
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:278
ParseContext::frame_start_found
int frame_start_found
Definition: parser.h:34
parse
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:46
index
int index
Definition: gxfenc.c:90
gif.h
GIFParseContext::etype
int etype
Definition: gif_parser.c:49
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:211
FFCodecParser
Definition: parser_internal.h:29
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:2623
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:48
parser.h
PARSER_CODEC_LIST
#define PARSER_CODEC_LIST(...)
Definition: parser_internal.h:76
AVCodecParserContext
Definition: avcodec.h:2589
AVCodecContext
main external API structure.
Definition: avcodec.h:439
GIFParseContext::keyframe
int keyframe
Definition: gif_parser.c:51
GIF_IMAGE_BLOCK
@ GIF_IMAGE_BLOCK
Definition: gif_parser.c:36
GIF_EXTENSION
@ GIF_EXTENSION
Definition: gif_parser.c:33
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:279
GIFParseContext::found_sig
unsigned found_sig
Definition: gif_parser.c:41
GIFParseContext::pc
ParseContext pc
Definition: gif_parser.c:40
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
GIFParseContext::delay
int delay
Definition: gif_parser.c:50