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/bswap.h"
28 #include "libavutil/common.h"
29 
30 #include "gif.h"
31 #include "parser.h"
32 
33 typedef enum GIFParseStates {
39 } gif_states;
40 
41 typedef struct GIFParseContext {
43  unsigned found_sig;
45  int found_end;
46  int index;
47  int state;
48  int gct_flag;
49  int gct_size;
51  int etype;
52  int delay;
54 
55 static int gif_find_frame_end(GIFParseContext *g, const uint8_t *buf,
56  int buf_size, void *logctx)
57 {
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  } else if (buf[index] == GIF_EXTENSION_INTRODUCER) {
67  g->state = GIF_EXTENSION;
68  g->found_start = 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 = 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 == 8) {
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  next = gif_find_frame_end(g, buf, buf_size, avctx);
169  if (ff_combine_frame(&g->pc, next, &buf, &buf_size) < 0) {
170  *poutbuf = NULL;
171  *poutbuf_size = 0;
172  return buf_size;
173  }
174 
175  s->duration = g->delay;
176 
177  *poutbuf = buf;
178  *poutbuf_size = buf_size;
179  return next;
180 }
181 
183  .codec_ids = { AV_CODEC_ID_GIF },
184  .priv_data_size = sizeof(GIFParseContext),
185  .parser_parse = gif_parse,
186  .parser_close = ff_parse_close,
187 };
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:55
GIFParseStates
GIFParseStates
Definition: gif_parser.c:33
GIFParseContext::index
int index
Definition: gif_parser.c:46
GIF_IMAGE
@ GIF_IMAGE
Definition: gif_parser.c:37
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:284
GIFParseContext::found_start
int found_start
Definition: gif_parser.c:44
GIF_GCE_EXT_LABEL
#define GIF_GCE_EXT_LABEL
Definition: gif.h:45
GIFParseContext::gct_flag
int gct_flag
Definition: gif_parser.c:48
ParseContext
Definition: parser.h:28
GIFParseContext
Definition: gif_parser.c:41
gif89a_sig
static const uint8_t gif89a_sig[6]
Definition: gif.h:35
GIFParseContext::found_end
int found_end
Definition: gif_parser.c:45
GIF_EXTENSION_BLOCK
@ GIF_EXTENSION_BLOCK
Definition: gif_parser.c:36
s
#define s(width, name)
Definition: cbs_vp9.c:256
GIFParseContext::gct_size
int gct_size
Definition: gif_parser.c:49
g
const char * g
Definition: vf_curves.c:127
GIFParseContext::state
int state
Definition: gif_parser.c:47
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:34
index
int index
Definition: gxfenc.c:89
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2972
gif.h
GIFParseContext::etype
int etype
Definition: gif_parser.c:51
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:199
AV_CODEC_ID_GIF
@ AV_CODEC_ID_GIF
Definition: codec_id.h:149
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:50
common.h
parser.h
AVCodecParserContext
Definition: avcodec.h:2812
bswap.h
AVCodecContext
main external API structure.
Definition: avcodec.h:426
GIF_IMAGE_BLOCK
@ GIF_IMAGE_BLOCK
Definition: gif_parser.c:38
GIF_EXTENSION
@ GIF_EXTENSION
Definition: gif_parser.c:35
GIFParseContext::found_sig
unsigned found_sig
Definition: gif_parser.c:43
GIFParseContext::pc
ParseContext pc
Definition: gif_parser.c:42
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
AVCodecParser
Definition: avcodec.h:2971
GIFParseContext::delay
int delay
Definition: gif_parser.c:52
ff_gif_parser
const AVCodecParser ff_gif_parser
Definition: gif_parser.c:182