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 {
34     GIF_HEADER = 1,
35     GIF_EXTENSION,
36     GIF_EXTENSION_BLOCK,
37     GIF_IMAGE,
38     GIF_IMAGE_BLOCK,
39 } gif_states;
40 
41 typedef struct GIFParseContext {
42     ParseContext pc;
43     unsigned found_sig;
44     int found_start;
45     int found_end;
46     int index;
47     int state;
48     int gct_flag;
49     int gct_size;
50     int block_size;
51     int etype;
52     int delay;
53 } GIFParseContext;
54 
gif_find_frame_end(GIFParseContext *g, const uint8_t *buf, int buf_size, void *logctx)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 
gif_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)161 static int gif_parse(AVCodecParserContext *s, AVCodecContext *avctx,
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 
182 const AVCodecParser ff_gif_parser = {
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 };
188