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