GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/gif_parser.c Lines: 0 93 0.0 %
Date: 2019-11-18 18:00:01 Branches: 0 62 0.0 %

Line Branch Exec Source
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 {
35
    GIF_HEADER = 1,
36
    GIF_EXTENSION,
37
    GIF_EXTENSION_BLOCK,
38
    GIF_IMAGE,
39
    GIF_IMAGE_BLOCK,
40
} gif_states;
41
42
typedef struct GIFParseContext {
43
    ParseContext pc;
44
    unsigned found_sig;
45
    int found_start;
46
    int found_end;
47
    int index;
48
    int state;
49
    int gct_flag;
50
    int gct_size;
51
    int block_size;
52
    int etype;
53
    int delay;
54
} GIFParseContext;
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
162
static int gif_parse(AVCodecParserContext *s, AVCodecContext *avctx,
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
183
AVCodecParser ff_gif_parser = {
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
};