../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
193 / 295
65.4%
Branches:
114 / 222
51.4%
Functions:
8 / 11
72.7%

libavcodec/gif.c
Line Branch Exec Source
1 /*
2 * Copyright (c) 2000 Fabrice Bellard
3 * Copyright (c) 2002 Francois Revol
4 * Copyright (c) 2006 Baptiste Coudurier
5 * Copyright (c) 2018 Bjorn Roche
6 * Copyright (c) 2018 Paul B Mahol
7 *
8 * first version by Francois Revol <revol@free.fr>
9 *
10 * This file is part of FFmpeg.
11 *
12 * FFmpeg is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * FFmpeg is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with FFmpeg; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27 /**
28 * @file
29 * GIF encoder
30 * @see http://www.w3.org/Graphics/GIF/spec-gif89a.txt
31 */
32
33 #include "libavutil/attributes.h"
34 #include "libavutil/imgutils_internal.h"
35 #include "libavutil/mem.h"
36 #include "libavutil/opt.h"
37 #include "avcodec.h"
38 #include "bytestream.h"
39 #include "codec_internal.h"
40 #include "encode.h"
41 #include "lzw.h"
42 #include "gif.h"
43
44 #define DEFAULT_TRANSPARENCY_INDEX 0x1f
45
46 typedef struct GIFContext {
47 const AVClass *class;
48 LZWState *lzw;
49 uint8_t *buf;
50 uint8_t *shrunk_buf;
51 int buf_size;
52 AVFrame *last_frame;
53 int flags;
54 int image;
55 int use_global_palette;
56 uint32_t palette[AVPALETTE_COUNT]; ///< local reference palette for !pal8
57 int palette_loaded;
58 int transparent_index;
59 uint8_t *tmpl; ///< temporary line buffer
60 } GIFContext;
61
62 enum {
63 GF_OFFSETTING = 1<<0,
64 GF_TRANSDIFF = 1<<1,
65 };
66
67 ✗ static void shrink_palette(const uint32_t *src, uint8_t *map,
68 uint32_t *dst, size_t *palette_count)
69 {
70 ✗ size_t colors_seen = 0;
71
72
0/2
✗ Branch 11 → 3 not taken.
✗ Branch 11 → 12 not taken.
✗ for (size_t i = 0; i < AVPALETTE_COUNT; i++) {
73 ✗ int seen = 0;
74
0/2
✗ Branch 7 → 4 not taken.
✗ Branch 7 → 8 not taken.
✗ for (size_t c = 0; c < colors_seen; c++) {
75
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ if (src[i] == dst[c]) {
76 ✗ seen = 1;
77 ✗ break;
78 }
79 }
80
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ if (!seen) {
81 ✗ dst[colors_seen] = src[i];
82 ✗ map[i] = colors_seen;
83 ✗ colors_seen++;
84 }
85 }
86
87 ✗ *palette_count = colors_seen;
88 ✗ }
89
90 ✗ static void remap_frame_to_palette(const uint8_t *src, int src_linesize,
91 uint8_t *dst, int dst_linesize,
92 int w, int h, uint8_t *map)
93 {
94
0/2
✗ Branch 7 → 3 not taken.
✗ Branch 7 → 8 not taken.
✗ for (int i = 0; i < h; i++)
95
0/2
✗ Branch 5 → 4 not taken.
✗ Branch 5 → 6 not taken.
✗ for (int j = 0; j < w; j++)
96 ✗ dst[i * dst_linesize + j] = map[src[i * src_linesize + j]];
97 ✗ }
98
99 1063 static int is_image_translucent(AVCodecContext *avctx,
100 const uint8_t *buf, const int linesize)
101 {
102 1063 GIFContext *s = avctx->priv_data;
103 1063 int trans = s->transparent_index;
104
105
1/2
✓ Branch 2 → 3 taken 1063 times.
✗ Branch 2 → 4 not taken.
1063 if (trans < 0)
106 1063 return 0;
107
108
0/2
✗ Branch 11 → 5 not taken.
✗ Branch 11 → 12 not taken.
✗ for (int y = 0; y < avctx->height; y++) {
109
0/2
✗ Branch 9 → 6 not taken.
✗ Branch 9 → 10 not taken.
✗ for (int x = 0; x < avctx->width; x++) {
110
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (buf[x] == trans) {
111 ✗ return 1;
112 }
113 }
114 ✗ buf += linesize;
115 }
116
117 ✗ return 0;
118 }
119
120 39 static int get_palette_transparency_index(const uint32_t *palette)
121 {
122 39 int transparent_color_index = -1;
123 39 unsigned i, smallest_alpha = 0xff;
124
125
2/2
✓ Branch 2 → 3 taken 31 times.
✓ Branch 2 → 4 taken 8 times.
39 if (!palette)
126 31 return -1;
127
128
2/2
✓ Branch 8 → 5 taken 2048 times.
✓ Branch 8 → 9 taken 8 times.
2056 for (i = 0; i < AVPALETTE_COUNT; i++) {
129 2048 const uint32_t v = palette[i];
130
2/2
✓ Branch 5 → 6 taken 2 times.
✓ Branch 5 → 7 taken 2046 times.
2048 if (v >> 24 < smallest_alpha) {
131 2 smallest_alpha = v >> 24;
132 2 transparent_color_index = i;
133 }
134 }
135
2/2
✓ Branch 9 → 10 taken 2 times.
✓ Branch 9 → 11 taken 6 times.
8 return smallest_alpha < 128 ? transparent_color_index : -1;
136 }
137
138 1056 static int pick_palette_entry(const uint8_t *buf, int linesize, int w, int h)
139 {
140 1056 int histogram[AVPALETTE_COUNT] = {0};
141 int x, y, i;
142
143
2/2
✓ Branch 7 → 3 taken 32487 times.
✓ Branch 7 → 8 taken 1056 times.
33543 for (y = 0; y < h; y++) {
144
2/2
✓ Branch 5 → 4 taken 3157775 times.
✓ Branch 5 → 6 taken 32487 times.
3190262 for (x = 0; x < w; x++)
145 3157775 histogram[buf[x]]++;
146 32487 buf += linesize;
147 }
148
2/2
✓ Branch 12 → 9 taken 7288 times.
✓ Branch 12 → 13 taken 24 times.
7312 for (i = 0; i < FF_ARRAY_ELEMS(histogram); i++)
149
2/2
✓ Branch 9 → 10 taken 1032 times.
✓ Branch 9 → 11 taken 6256 times.
7288 if (!histogram[i])
150 1032 return i;
151 24 return -1;
152 }
153
154 ✗ static void gif_crop_translucent(AVCodecContext *avctx,
155 const uint8_t *buf, const int linesize,
156 int *width, int *height,
157 int *x_start, int *y_start)
158 {
159 ✗ GIFContext *s = avctx->priv_data;
160 ✗ int trans = s->transparent_index;
161
162 /* Crop image */
163
0/4
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 45 not taken.
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 45 not taken.
✗ if ((s->flags & GF_OFFSETTING) && trans >= 0) {
164 ✗ const int w = avctx->width;
165 ✗ const int h = avctx->height;
166 ✗ int x_end = w - 1,
167 ✗ y_end = h - 1;
168
169 // crop top
170
0/2
✗ Branch 13 → 5 not taken.
✗ Branch 13 → 14 not taken.
✗ while (*y_start < y_end) {
171 ✗ int is_trans = 1;
172
0/2
✗ Branch 9 → 6 not taken.
✗ Branch 9 → 10 not taken.
✗ for (int i = 0; i < w; i++) {
173
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (buf[linesize * *y_start + i] != trans) {
174 ✗ is_trans = 0;
175 ✗ break;
176 }
177 }
178
179
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ if (!is_trans)
180 ✗ break;
181 ✗ (*y_start)++;
182 }
183
184 // crop bottom
185
0/2
✗ Branch 23 → 15 not taken.
✗ Branch 23 → 24 not taken.
✗ while (y_end > *y_start) {
186 ✗ int is_trans = 1;
187
0/2
✗ Branch 19 → 16 not taken.
✗ Branch 19 → 20 not taken.
✗ for (int i = 0; i < w; i++) {
188
0/2
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
✗ if (buf[linesize * y_end + i] != trans) {
189 ✗ is_trans = 0;
190 ✗ break;
191 }
192 }
193
0/2
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 22 not taken.
✗ if (!is_trans)
194 ✗ break;
195 ✗ y_end--;
196 }
197
198 // crop left
199
0/2
✗ Branch 33 → 25 not taken.
✗ Branch 33 → 34 not taken.
✗ while (*x_start < x_end) {
200 ✗ int is_trans = 1;
201
0/2
✗ Branch 29 → 26 not taken.
✗ Branch 29 → 30 not taken.
✗ for (int i = *y_start; i < y_end; i++) {
202
0/2
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 28 not taken.
✗ if (buf[linesize * i + *x_start] != trans) {
203 ✗ is_trans = 0;
204 ✗ break;
205 }
206 }
207
0/2
✗ Branch 30 → 31 not taken.
✗ Branch 30 → 32 not taken.
✗ if (!is_trans)
208 ✗ break;
209 ✗ (*x_start)++;
210 }
211
212 // crop right
213
0/2
✗ Branch 43 → 35 not taken.
✗ Branch 43 → 44 not taken.
✗ while (x_end > *x_start) {
214 ✗ int is_trans = 1;
215
0/2
✗ Branch 39 → 36 not taken.
✗ Branch 39 → 40 not taken.
✗ for (int i = *y_start; i < y_end; i++) {
216
0/2
✗ Branch 36 → 37 not taken.
✗ Branch 36 → 38 not taken.
✗ if (buf[linesize * i + x_end] != trans) {
217 ✗ is_trans = 0;
218 ✗ break;
219 }
220 }
221
0/2
✗ Branch 40 → 41 not taken.
✗ Branch 40 → 42 not taken.
✗ if (!is_trans)
222 ✗ break;
223 ✗ x_end--;
224 }
225
226 ✗ *height = y_end + 1 - *y_start;
227 ✗ *width = x_end + 1 - *x_start;
228 ✗ av_log(avctx, AV_LOG_DEBUG,"%dx%d image at pos (%d;%d) [area:%dx%d]\n",
229 *width, *height, *x_start, *y_start, avctx->width, avctx->height);
230 }
231 ✗ }
232
233 1063 static void gif_crop_opaque(AVCodecContext *avctx,
234 const uint32_t *palette,
235 const uint8_t *buf, const int linesize,
236 int *width, int *height, int *x_start, int *y_start)
237 {
238 1063 GIFContext *s = avctx->priv_data;
239
240 /* Crop image */
241
4/6
✓ Branch 2 → 3 taken 1063 times.
✗ Branch 2 → 36 not taken.
✓ Branch 3 → 4 taken 1056 times.
✓ Branch 3 → 36 taken 7 times.
✓ Branch 4 → 5 taken 1056 times.
✗ Branch 4 → 36 not taken.
1063 if ((s->flags & GF_OFFSETTING) && s->last_frame && !palette) {
242 1056 const uint8_t *ref = s->last_frame->data[0];
243 1056 const int ref_linesize = s->last_frame->linesize[0];
244 1056 int x_end = avctx->width - 1,
245 1056 y_end = avctx->height - 1;
246
247 /* skip common lines */
248
1/2
✓ Branch 9 → 6 taken 100704 times.
✗ Branch 9 → 10 not taken.
100704 while (*y_start < y_end) {
249
2/2
✓ Branch 6 → 7 taken 1056 times.
✓ Branch 6 → 8 taken 99648 times.
100704 if (memcmp(ref + *y_start*ref_linesize, buf + *y_start*linesize, *width))
250 1056 break;
251 99648 (*y_start)++;
252 }
253
1/2
✓ Branch 14 → 11 taken 99777 times.
✗ Branch 14 → 15 not taken.
99777 while (y_end > *y_start) {
254
2/2
✓ Branch 11 → 12 taken 1056 times.
✓ Branch 11 → 13 taken 98721 times.
99777 if (memcmp(ref + y_end*ref_linesize, buf + y_end*linesize, *width))
255 1056 break;
256 98721 y_end--;
257 }
258 1056 *height = y_end + 1 - *y_start;
259
260 /* skip common columns */
261
1/2
✓ Branch 24 → 16 taken 96923 times.
✗ Branch 24 → 25 not taken.
96923 while (*x_start < x_end) {
262 96923 int same_column = 1;
263
2/2
✓ Branch 20 → 17 taken 2402634 times.
✓ Branch 20 → 21 taken 95867 times.
2498501 for (int y = *y_start; y <= y_end; y++) {
264
2/2
✓ Branch 17 → 18 taken 1056 times.
✓ Branch 17 → 19 taken 2401578 times.
2402634 if (ref[y*ref_linesize + *x_start] != buf[y*linesize + *x_start]) {
265 1056 same_column = 0;
266 1056 break;
267 }
268 }
269
2/2
✓ Branch 21 → 22 taken 1056 times.
✓ Branch 21 → 23 taken 95867 times.
96923 if (!same_column)
270 1056 break;
271 95867 (*x_start)++;
272 }
273
1/2
✓ Branch 34 → 26 taken 104460 times.
✗ Branch 34 → 35 not taken.
104460 while (x_end > *x_start) {
274 104460 int same_column = 1;
275
2/2
✓ Branch 30 → 27 taken 2442709 times.
✓ Branch 30 → 31 taken 103404 times.
2546113 for (int y = *y_start; y <= y_end; y++) {
276
2/2
✓ Branch 27 → 28 taken 1056 times.
✓ Branch 27 → 29 taken 2441653 times.
2442709 if (ref[y*ref_linesize + x_end] != buf[y*linesize + x_end]) {
277 1056 same_column = 0;
278 1056 break;
279 }
280 }
281
2/2
✓ Branch 31 → 32 taken 1056 times.
✓ Branch 31 → 33 taken 103404 times.
104460 if (!same_column)
282 1056 break;
283 103404 x_end--;
284 }
285 1056 *width = x_end + 1 - *x_start;
286
287 1056 av_log(avctx, AV_LOG_DEBUG,"%dx%d image at pos (%d;%d) [area:%dx%d]\n",
288 *width, *height, *x_start, *y_start, avctx->width, avctx->height);
289 }
290 1063 }
291
292 1063 static int gif_image_write_image(AVCodecContext *avctx,
293 uint8_t **bytestream, uint8_t *end,
294 const uint32_t *palette,
295 const uint8_t *buf, const int linesize,
296 AVPacket *pkt)
297 {
298 1063 GIFContext *s = avctx->priv_data;
299 1063 int disposal, len = 0, height = avctx->height, width = avctx->width, x, y;
300 1063 int x_start = 0, y_start = 0, trans = s->transparent_index;
301
4/6
✓ Branch 2 → 3 taken 1063 times.
✗ Branch 2 → 6 not taken.
✓ Branch 3 → 4 taken 1056 times.
✓ Branch 3 → 6 taken 7 times.
✓ Branch 4 → 5 taken 1056 times.
✗ Branch 4 → 6 not taken.
1063 int bcid = -1, honor_transparency = (s->flags & GF_TRANSDIFF) && s->last_frame && !palette;
302 const uint8_t *ptr;
303 1063 int ptr_linesize = linesize;
304 uint32_t shrunk_palette[AVPALETTE_COUNT];
305 1063 uint8_t map[AVPALETTE_COUNT] = { 0 };
306 1063 size_t shrunk_palette_count = 0;
307
308 /*
309 * We memset to 0xff instead of 0x00 so that the transparency detection
310 * doesn't pick anything after the palette entries as the transparency
311 * index, and because GIF89a requires us to always write a power-of-2
312 * number of palette entries.
313 */
314 1063 memset(shrunk_palette, 0xff, AVPALETTE_SIZE);
315
316
2/4
✓ Branch 7 → 8 taken 1063 times.
✗ Branch 7 → 12 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 1063 times.
1063 if (!s->image && is_image_translucent(avctx, buf, linesize)) {
317 ✗ gif_crop_translucent(avctx, buf, linesize, &width, &height, &x_start, &y_start);
318 ✗ honor_transparency = 0;
319 ✗ disposal = GCE_DISPOSAL_BACKGROUND;
320 } else {
321 1063 gif_crop_opaque(avctx, palette, buf, linesize, &width, &height, &x_start, &y_start);
322 1063 disposal = GCE_DISPOSAL_INPLACE;
323 }
324
325
3/4
✓ Branch 14 → 15 taken 1063 times.
✗ Branch 14 → 16 not taken.
✓ Branch 15 → 16 taken 7 times.
✓ Branch 15 → 43 taken 1056 times.
1063 if (s->image || !avctx->frame_num) { /* GIF header */
326
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 7 times.
7 const uint32_t *global_palette = palette ? palette : s->palette;
327 7 const AVRational sar = avctx->sample_aspect_ratio;
328 7 int64_t aspect = 0;
329
330
1/4
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 24 taken 7 times.
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 24 not taken.
7 if (sar.num > 0 && sar.den > 0) {
331 ✗ aspect = sar.num * 64LL / sar.den - 15;
332
0/4
✗ Branch 21 → 22 not taken.
✗ Branch 21 → 23 not taken.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
✗ if (aspect < 0 || aspect > 255)
333 ✗ aspect = 0;
334 }
335
336 7 bytestream_put_buffer(bytestream, gif89a_sig, sizeof(gif89a_sig));
337 7 bytestream_put_le16(bytestream, avctx->width);
338 7 bytestream_put_le16(bytestream, avctx->height);
339
340 7 bcid = get_palette_transparency_index(global_palette);
341
342
1/2
✓ Branch 28 → 29 taken 7 times.
✗ Branch 28 → 30 not taken.
7 bytestream_put_byte(bytestream, ((uint8_t) s->use_global_palette << 7) | 0x70 | (s->use_global_palette ? 7 : 0)); /* flags: global clut, 256 entries */
343
2/2
✓ Branch 32 → 33 taken 2 times.
✓ Branch 32 → 34 taken 5 times.
7 bytestream_put_byte(bytestream, bcid < 0 ? DEFAULT_TRANSPARENCY_INDEX : bcid); /* background color index */
344 7 bytestream_put_byte(bytestream, aspect);
345
1/2
✓ Branch 37 → 38 taken 7 times.
✗ Branch 37 → 42 not taken.
7 if (s->use_global_palette) {
346
2/2
✓ Branch 41 → 39 taken 1792 times.
✓ Branch 41 → 42 taken 7 times.
1799 for (int i = 0; i < 256; i++) {
347 1792 const uint32_t v = global_palette[i] & 0xffffff;
348 1792 bytestream_put_be24(bytestream, v);
349 }
350 }
351 }
352
353
3/4
✓ Branch 43 → 44 taken 1056 times.
✓ Branch 43 → 48 taken 7 times.
✓ Branch 44 → 45 taken 1056 times.
✗ Branch 44 → 48 not taken.
1063 if (honor_transparency && trans < 0) {
354 1056 trans = pick_palette_entry(buf + y_start*linesize + x_start,
355 linesize, width, height);
356
2/2
✓ Branch 46 → 47 taken 24 times.
✓ Branch 46 → 48 taken 1032 times.
1056 if (trans < 0) // TODO, patch welcome
357 24 av_log(avctx, AV_LOG_DEBUG, "No available color, can not use transparency\n");
358 }
359
360
2/2
✓ Branch 48 → 49 taken 31 times.
✓ Branch 48 → 50 taken 1032 times.
1063 if (trans < 0)
361 31 honor_transparency = 0;
362
363
2/4
✓ Branch 50 → 51 taken 1063 times.
✗ Branch 50 → 52 not taken.
✗ Branch 51 → 52 not taken.
✓ Branch 51 → 56 taken 1063 times.
1063 if (palette || !s->use_global_palette) {
364
0/2
✗ Branch 52 → 53 not taken.
✗ Branch 52 → 54 not taken.
✗ const uint32_t *pal = palette ? palette : s->palette;
365 ✗ shrink_palette(pal, map, shrunk_palette, &shrunk_palette_count);
366 }
367
368
3/4
✓ Branch 56 → 57 taken 31 times.
✓ Branch 56 → 59 taken 1032 times.
✓ Branch 57 → 58 taken 31 times.
✗ Branch 57 → 59 not taken.
1063 bcid = honor_transparency || disposal == GCE_DISPOSAL_BACKGROUND ? trans : get_palette_transparency_index(palette);
369
370 /* graphic control extension */
371 1063 bytestream_put_byte(bytestream, GIF_EXTENSION_INTRODUCER);
372 1063 bytestream_put_byte(bytestream, GIF_GCE_EXT_LABEL);
373 1063 bytestream_put_byte(bytestream, 0x04); /* block size */
374 1063 bytestream_put_byte(bytestream, disposal<<2 | (bcid >= 0));
375 1063 bytestream_put_le16(bytestream, 5); // default delay
376
3/4
✓ Branch 65 → 66 taken 1032 times.
✓ Branch 65 → 69 taken 31 times.
✗ Branch 66 → 67 not taken.
✓ Branch 66 → 68 taken 1032 times.
1063 bytestream_put_byte(bytestream, bcid < 0 ? DEFAULT_TRANSPARENCY_INDEX : (shrunk_palette_count ? map[bcid] : bcid));
377 1063 bytestream_put_byte(bytestream, 0x00);
378
379 /* image block */
380 1063 bytestream_put_byte(bytestream, GIF_IMAGE_SEPARATOR);
381 1063 bytestream_put_le16(bytestream, x_start);
382 1063 bytestream_put_le16(bytestream, y_start);
383 1063 bytestream_put_le16(bytestream, width);
384 1063 bytestream_put_le16(bytestream, height);
385
386
2/4
✓ Branch 77 → 78 taken 1063 times.
✗ Branch 77 → 79 not taken.
✗ Branch 78 → 79 not taken.
✓ Branch 78 → 85 taken 1063 times.
1063 if (palette || !s->use_global_palette) {
387 ✗ unsigned pow2_count = av_log2(shrunk_palette_count - 1);
388 unsigned i;
389
390 ✗ bytestream_put_byte(bytestream, 1<<7 | pow2_count); /* flags */
391
0/2
✗ Branch 83 → 81 not taken.
✗ Branch 83 → 84 not taken.
✗ for (i = 0; i < 1 << (pow2_count + 1); i++) {
392 ✗ const uint32_t v = shrunk_palette[i];
393 ✗ bytestream_put_be24(bytestream, v);
394 }
395 } else {
396 1063 bytestream_put_byte(bytestream, 0x00); /* flags */
397 }
398
399 1063 bytestream_put_byte(bytestream, 0x08);
400
401 1063 ff_lzw_encode_init(s->lzw, s->buf, s->buf_size,
402 12, FF_LZW_GIF, 1);
403
404
1/2
✗ Branch 88 → 89 not taken.
✓ Branch 88 → 96 taken 1063 times.
1063 if (shrunk_palette_count) {
405
0/2
✗ Branch 89 → 90 not taken.
✗ Branch 89 → 94 not taken.
✗ if (!s->shrunk_buf) {
406 ✗ s->shrunk_buf = av_malloc(avctx->height * avctx->width);
407
0/2
✗ Branch 91 → 92 not taken.
✗ Branch 91 → 94 not taken.
✗ if (!s->shrunk_buf) {
408 ✗ av_log(avctx, AV_LOG_ERROR, "Could not allocated remapped frame buffer.\n");
409 ✗ return AVERROR(ENOMEM);
410 }
411 }
412 ✗ remap_frame_to_palette(buf, linesize, s->shrunk_buf, avctx->width, avctx->width, avctx->height, map);
413 ✗ ptr = s->shrunk_buf + y_start*avctx->width + x_start;
414 ✗ ptr_linesize = avctx->width;
415 } else {
416 1063 ptr = buf + y_start*linesize + x_start;
417 }
418
2/2
✓ Branch 97 → 98 taken 1032 times.
✓ Branch 97 → 107 taken 31 times.
1063 if (honor_transparency) {
419 1032 const int ref_linesize = s->last_frame->linesize[0];
420 1032 const uint8_t *ref = s->last_frame->data[0] + y_start*ref_linesize + x_start;
421
422
2/2
✓ Branch 106 → 99 taken 25575 times.
✓ Branch 106 → 111 taken 1032 times.
26607 for (y = 0; y < height; y++) {
423 25575 memcpy(s->tmpl, ptr, width);
424
2/2
✓ Branch 103 → 100 taken 724751 times.
✓ Branch 103 → 104 taken 25575 times.
750326 for (x = 0; x < width; x++)
425
2/2
✓ Branch 100 → 101 taken 367271 times.
✓ Branch 100 → 102 taken 357480 times.
724751 if (ref[x] == ptr[x])
426 367271 s->tmpl[x] = trans;
427 25575 len += ff_lzw_encode(s->lzw, s->tmpl, width);
428 25575 ptr += ptr_linesize;
429 25575 ref += ref_linesize;
430 }
431 } else {
432
2/2
✓ Branch 110 → 108 taken 8502 times.
✓ Branch 110 → 111 taken 31 times.
8533 for (y = 0; y < height; y++) {
433 8502 len += ff_lzw_encode(s->lzw, ptr, width);
434 8502 ptr += ptr_linesize;
435 }
436 }
437 1063 len += ff_lzw_encode_flush(s->lzw);
438
439 1063 ptr = s->buf;
440
2/2
✓ Branch 118 → 113 taken 9251 times.
✓ Branch 118 → 119 taken 1063 times.
10314 while (len > 0) {
441 9251 int size = FFMIN(255, len);
442 9251 bytestream_put_byte(bytestream, size);
443
1/2
✗ Branch 114 → 115 not taken.
✓ Branch 114 → 116 taken 9251 times.
9251 if (end - *bytestream < size)
444 ✗ return -1;
445 9251 bytestream_put_buffer(bytestream, ptr, size);
446 9251 ptr += size;
447 9251 len -= size;
448 }
449 1063 bytestream_put_byte(bytestream, 0x00); /* end of image block */
450 1063 return 0;
451 }
452
453 7 static av_cold int gif_encode_init(AVCodecContext *avctx)
454 {
455 7 GIFContext *s = avctx->priv_data;
456
457
2/4
✓ Branch 2 → 3 taken 7 times.
✗ Branch 2 → 4 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 7 times.
7 if (avctx->width > 65535 || avctx->height > 65535) {
458 ✗ av_log(avctx, AV_LOG_ERROR, "GIF does not support resolutions above 65535x65535\n");
459 ✗ return AVERROR(EINVAL);
460 }
461
462 7 s->transparent_index = -1;
463
464 7 s->lzw = av_mallocz(ff_lzw_encode_state_size);
465 7 s->buf_size = avctx->width*avctx->height*2 + 1000;
466 7 s->buf = av_malloc(s->buf_size);
467 7 s->tmpl = av_malloc(avctx->width);
468
3/6
✓ Branch 9 → 10 taken 7 times.
✗ Branch 9 → 12 not taken.
✓ Branch 10 → 11 taken 7 times.
✗ Branch 10 → 12 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 7 times.
7 if (!s->tmpl || !s->buf || !s->lzw)
469 ✗ return AVERROR(ENOMEM);
470
471
2/2
✓ Branch 14 → 15 taken 1 time.
✓ Branch 14 → 18 taken 6 times.
7 if (avpriv_set_systematic_pal2(s->palette, avctx->pix_fmt) < 0)
472
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 1 time.
1 av_assert0(avctx->pix_fmt == AV_PIX_FMT_PAL8);
473
474 7 return 0;
475 }
476
477 1063 static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
478 const AVFrame *pict, int *got_packet)
479 {
480 1063 GIFContext *s = avctx->priv_data;
481 uint8_t *outbuf_ptr, *end;
482 1063 const uint32_t *palette = NULL;
483 int ret;
484
485
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 1063 times.
1063 if ((ret = ff_alloc_packet(avctx, pkt, avctx->width*avctx->height*7/5 + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
486 ✗ return ret;
487 1063 outbuf_ptr = pkt->data;
488 1063 end = pkt->data + pkt->size;
489
490
2/2
✓ Branch 5 → 6 taken 173 times.
✓ Branch 5 → 12 taken 890 times.
1063 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
491 173 palette = (uint32_t*)pict->data[1];
492
493
2/2
✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 10 taken 172 times.
173 if (!s->palette_loaded) {
494 1 memcpy(s->palette, palette, AVPALETTE_SIZE);
495 1 s->transparent_index = get_palette_transparency_index(palette);
496 1 s->palette_loaded = 1;
497
1/2
✓ Branch 8 → 9 taken 1 time.
✗ Branch 8 → 12 not taken.
1 if (s->use_global_palette)
498 1 palette = NULL;
499
1/2
✓ Branch 10 → 11 taken 172 times.
✗ Branch 10 → 12 not taken.
172 } else if (!memcmp(s->palette, palette, AVPALETTE_SIZE)) {
500 172 palette = NULL;
501 }
502 }
503
504 1063 ret = gif_image_write_image(avctx, &outbuf_ptr, end, palette,
505 1063 pict->data[0], pict->linesize[0], pkt);
506
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 1063 times.
1063 if (ret < 0)
507 ✗ return ret;
508
3/4
✓ Branch 15 → 16 taken 7 times.
✓ Branch 15 → 20 taken 1056 times.
✓ Branch 16 → 17 taken 7 times.
✗ Branch 16 → 20 not taken.
1063 if (!s->last_frame && !s->image) {
509 7 s->last_frame = av_frame_alloc();
510
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 7 times.
7 if (!s->last_frame)
511 ✗ return AVERROR(ENOMEM);
512 }
513
514
1/2
✓ Branch 20 → 21 taken 1063 times.
✗ Branch 20 → 24 not taken.
1063 if (!s->image) {
515 1063 ret = av_frame_replace(s->last_frame, pict);
516
1/2
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 1063 times.
1063 if (ret < 0)
517 ✗ return ret;
518 }
519
520 1063 pkt->size = outbuf_ptr - pkt->data;
521
3/4
✓ Branch 24 → 25 taken 1063 times.
✗ Branch 24 → 26 not taken.
✓ Branch 25 → 26 taken 7 times.
✓ Branch 25 → 27 taken 1056 times.
1063 if (s->image || !avctx->frame_num)
522 7 pkt->flags |= AV_PKT_FLAG_KEY;
523 1063 *got_packet = 1;
524
525 1063 return 0;
526 }
527
528 7 static av_cold int gif_encode_close(AVCodecContext *avctx)
529 {
530 7 GIFContext *s = avctx->priv_data;
531
532 7 av_freep(&s->lzw);
533 7 av_freep(&s->buf);
534 7 av_freep(&s->shrunk_buf);
535 7 s->buf_size = 0;
536 7 av_frame_free(&s->last_frame);
537 7 av_freep(&s->tmpl);
538 7 return 0;
539 }
540
541 #define OFFSET(x) offsetof(GIFContext, x)
542 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
543 static const AVOption gif_options[] = {
544 { "gifflags", "set GIF flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = GF_OFFSETTING|GF_TRANSDIFF}, 0, INT_MAX, FLAGS, .unit = "flags" },
545 { "offsetting", "enable picture offsetting", 0, AV_OPT_TYPE_CONST, {.i64=GF_OFFSETTING}, INT_MIN, INT_MAX, FLAGS, .unit = "flags" },
546 { "transdiff", "enable transparency detection between frames", 0, AV_OPT_TYPE_CONST, {.i64=GF_TRANSDIFF}, INT_MIN, INT_MAX, FLAGS, .unit = "flags" },
547 { "gifimage", "enable encoding only images per frame", OFFSET(image), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
548 { "global_palette", "write a palette to the global gif header where feasible", OFFSET(use_global_palette), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
549 { NULL }
550 };
551
552 static const AVClass gif_class = {
553 .class_name = "GIF encoder",
554 .item_name = av_default_item_name,
555 .option = gif_options,
556 .version = LIBAVUTIL_VERSION_INT,
557 };
558
559 const FFCodec ff_gif_encoder = {
560 .p.name = "gif",
561 CODEC_LONG_NAME("GIF (Graphics Interchange Format)"),
562 .p.type = AVMEDIA_TYPE_VIDEO,
563 .p.id = AV_CODEC_ID_GIF,
564 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
565 .priv_data_size = sizeof(GIFContext),
566 .init = gif_encode_init,
567 FF_CODEC_ENCODE_CB(gif_encode_frame),
568 .close = gif_encode_close,
569 CODEC_PIXFMTS(AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8, AV_PIX_FMT_RGB4_BYTE,
570 AV_PIX_FMT_BGR4_BYTE, AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8),
571 .p.priv_class = &gif_class,
572 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
573 };
574