FFmpeg
Loading...
Searching...
No Matches
gif.c
Go to the documentation of this file.
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
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
46typedef struct GIFContext {
47 const AVClass *class;
49 uint8_t *buf;
50 uint8_t *shrunk_buf;
53 int flags;
54 int image;
56 uint32_t palette[AVPALETTE_COUNT]; ///< local reference palette for !pal8
59 uint8_t *tmpl; ///< temporary line buffer
61
62enum {
65};
66
67static 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 for (size_t i = 0; i < AVPALETTE_COUNT; i++) {
73 int seen = 0;
74 for (size_t c = 0; c < colors_seen; c++) {
75 if (src[i] == dst[c]) {
76 seen = 1;
77 break;
78 }
79 }
80 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
90static 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 for (int i = 0; i < h; i++)
95 for (int j = 0; j < w; j++)
96 dst[i * dst_linesize + j] = map[src[i * src_linesize + j]];
97}
98
100 const uint8_t *buf, const int linesize)
101{
102 GIFContext *s = avctx->priv_data;
103 int trans = s->transparent_index;
104
105 if (trans < 0)
106 return 0;
107
108 for (int y = 0; y < avctx->height; y++) {
109 for (int x = 0; x < avctx->width; x++) {
110 if (buf[x] == trans) {
111 return 1;
112 }
113 }
114 buf += linesize;
115 }
116
117 return 0;
118}
119
120static int get_palette_transparency_index(const uint32_t *palette)
121{
122 int transparent_color_index = -1;
123 unsigned i, smallest_alpha = 0xff;
124
125 if (!palette)
126 return -1;
127
128 for (i = 0; i < AVPALETTE_COUNT; i++) {
129 const uint32_t v = palette[i];
130 if (v >> 24 < smallest_alpha) {
131 smallest_alpha = v >> 24;
132 transparent_color_index = i;
133 }
134 }
135 return smallest_alpha < 128 ? transparent_color_index : -1;
136}
137
138static int pick_palette_entry(const uint8_t *buf, int linesize, int w, int h)
139{
140 int histogram[AVPALETTE_COUNT] = {0};
141 int x, y, i;
142
143 for (y = 0; y < h; y++) {
144 for (x = 0; x < w; x++)
145 histogram[buf[x]]++;
146 buf += linesize;
147 }
148 for (i = 0; i < FF_ARRAY_ELEMS(histogram); i++)
149 if (!histogram[i])
150 return i;
151 return -1;
152}
153
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 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 while (*y_start < y_end) {
171 int is_trans = 1;
172 for (int i = 0; i < w; i++) {
173 if (buf[linesize * *y_start + i] != trans) {
174 is_trans = 0;
175 break;
176 }
177 }
178
179 if (!is_trans)
180 break;
181 (*y_start)++;
182 }
183
184 // crop bottom
185 while (y_end > *y_start) {
186 int is_trans = 1;
187 for (int i = 0; i < w; i++) {
188 if (buf[linesize * y_end + i] != trans) {
189 is_trans = 0;
190 break;
191 }
192 }
193 if (!is_trans)
194 break;
195 y_end--;
196 }
197
198 // crop left
199 while (*x_start < x_end) {
200 int is_trans = 1;
201 for (int i = *y_start; i < y_end; i++) {
202 if (buf[linesize * i + *x_start] != trans) {
203 is_trans = 0;
204 break;
205 }
206 }
207 if (!is_trans)
208 break;
209 (*x_start)++;
210 }
211
212 // crop right
213 while (x_end > *x_start) {
214 int is_trans = 1;
215 for (int i = *y_start; i < y_end; i++) {
216 if (buf[linesize * i + x_end] != trans) {
217 is_trans = 0;
218 break;
219 }
220 }
221 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
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 GIFContext *s = avctx->priv_data;
239
240 /* Crop image */
241 if ((s->flags & GF_OFFSETTING) && s->last_frame && !palette) {
242 const uint8_t *ref = s->last_frame->data[0];
243 const int ref_linesize = s->last_frame->linesize[0];
244 int x_end = avctx->width - 1,
245 y_end = avctx->height - 1;
246
247 /* skip common lines */
248 while (*y_start < y_end) {
249 if (memcmp(ref + *y_start*ref_linesize, buf + *y_start*linesize, *width))
250 break;
251 (*y_start)++;
252 }
253 while (y_end > *y_start) {
254 if (memcmp(ref + y_end*ref_linesize, buf + y_end*linesize, *width))
255 break;
256 y_end--;
257 }
258 *height = y_end + 1 - *y_start;
259
260 /* skip common columns */
261 while (*x_start < x_end) {
262 int same_column = 1;
263 for (int y = *y_start; y <= y_end; y++) {
264 if (ref[y*ref_linesize + *x_start] != buf[y*linesize + *x_start]) {
265 same_column = 0;
266 break;
267 }
268 }
269 if (!same_column)
270 break;
271 (*x_start)++;
272 }
273 while (x_end > *x_start) {
274 int same_column = 1;
275 for (int y = *y_start; y <= y_end; y++) {
276 if (ref[y*ref_linesize + x_end] != buf[y*linesize + x_end]) {
277 same_column = 0;
278 break;
279 }
280 }
281 if (!same_column)
282 break;
283 x_end--;
284 }
285 *width = x_end + 1 - *x_start;
286
287 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}
291
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 GIFContext *s = avctx->priv_data;
299 int disposal, len = 0, height = avctx->height, width = avctx->width, x, y;
300 int x_start = 0, y_start = 0, trans = s->transparent_index;
301 int bcid = -1, honor_transparency = (s->flags & GF_TRANSDIFF) && s->last_frame && !palette;
302 const uint8_t *ptr;
303 int ptr_linesize = linesize;
304 uint32_t shrunk_palette[AVPALETTE_COUNT];
305 uint8_t map[AVPALETTE_COUNT] = { 0 };
306 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 memset(shrunk_palette, 0xff, AVPALETTE_SIZE);
315
316 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 gif_crop_opaque(avctx, palette, buf, linesize, &width, &height, &x_start, &y_start);
322 disposal = GCE_DISPOSAL_INPLACE;
323 }
324
325 if (s->image || !avctx->frame_num) { /* GIF header */
326 const uint32_t *global_palette = palette ? palette : s->palette;
327 const AVRational sar = avctx->sample_aspect_ratio;
328 int64_t aspect = 0;
329
330 if (sar.num > 0 && sar.den > 0) {
331 aspect = sar.num * 64LL / sar.den - 15;
332 if (aspect < 0 || aspect > 255)
333 aspect = 0;
334 }
335
336 bytestream_put_buffer(bytestream, gif89a_sig, sizeof(gif89a_sig));
337 bytestream_put_le16(bytestream, avctx->width);
338 bytestream_put_le16(bytestream, avctx->height);
339
340 bcid = get_palette_transparency_index(global_palette);
341
342 bytestream_put_byte(bytestream, ((uint8_t) s->use_global_palette << 7) | 0x70 | (s->use_global_palette ? 7 : 0)); /* flags: global clut, 256 entries */
343 bytestream_put_byte(bytestream, bcid < 0 ? DEFAULT_TRANSPARENCY_INDEX : bcid); /* background color index */
344 bytestream_put_byte(bytestream, aspect);
345 if (s->use_global_palette) {
346 for (int i = 0; i < 256; i++) {
347 const uint32_t v = global_palette[i] & 0xffffff;
348 bytestream_put_be24(bytestream, v);
349 }
350 }
351 }
352
353 if (honor_transparency && trans < 0) {
354 trans = pick_palette_entry(buf + y_start*linesize + x_start,
355 linesize, width, height);
356 if (trans < 0) // TODO, patch welcome
357 av_log(avctx, AV_LOG_DEBUG, "No available color, can not use transparency\n");
358 }
359
360 if (trans < 0)
361 honor_transparency = 0;
362
363 if (palette || !s->use_global_palette) {
364 const uint32_t *pal = palette ? palette : s->palette;
365 shrink_palette(pal, map, shrunk_palette, &shrunk_palette_count);
366 }
367
368 bcid = honor_transparency || disposal == GCE_DISPOSAL_BACKGROUND ? trans : get_palette_transparency_index(palette);
369
370 /* graphic control extension */
371 bytestream_put_byte(bytestream, GIF_EXTENSION_INTRODUCER);
372 bytestream_put_byte(bytestream, GIF_GCE_EXT_LABEL);
373 bytestream_put_byte(bytestream, 0x04); /* block size */
374 bytestream_put_byte(bytestream, disposal<<2 | (bcid >= 0));
375 bytestream_put_le16(bytestream, 5); // default delay
376 bytestream_put_byte(bytestream, bcid < 0 ? DEFAULT_TRANSPARENCY_INDEX : (shrunk_palette_count ? map[bcid] : bcid));
377 bytestream_put_byte(bytestream, 0x00);
378
379 /* image block */
380 bytestream_put_byte(bytestream, GIF_IMAGE_SEPARATOR);
381 bytestream_put_le16(bytestream, x_start);
382 bytestream_put_le16(bytestream, y_start);
383 bytestream_put_le16(bytestream, width);
384 bytestream_put_le16(bytestream, height);
385
386 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 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 bytestream_put_byte(bytestream, 0x00); /* flags */
397 }
398
399 bytestream_put_byte(bytestream, 0x08);
400
401 ff_lzw_encode_init(s->lzw, s->buf, s->buf_size,
402 12, FF_LZW_GIF, 1);
403
404 if (shrunk_palette_count) {
405 if (!s->shrunk_buf) {
406 s->shrunk_buf = av_malloc(avctx->height * avctx->width);
407 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 ptr = buf + y_start*linesize + x_start;
417 }
418 if (honor_transparency) {
419 const int ref_linesize = s->last_frame->linesize[0];
420 const uint8_t *ref = s->last_frame->data[0] + y_start*ref_linesize + x_start;
421
422 for (y = 0; y < height; y++) {
423 memcpy(s->tmpl, ptr, width);
424 for (x = 0; x < width; x++)
425 if (ref[x] == ptr[x])
426 s->tmpl[x] = trans;
427 len += ff_lzw_encode(s->lzw, s->tmpl, width);
428 ptr += ptr_linesize;
429 ref += ref_linesize;
430 }
431 } else {
432 for (y = 0; y < height; y++) {
433 len += ff_lzw_encode(s->lzw, ptr, width);
434 ptr += ptr_linesize;
435 }
436 }
437 len += ff_lzw_encode_flush(s->lzw);
438
439 ptr = s->buf;
440 while (len > 0) {
441 int size = FFMIN(255, len);
442 bytestream_put_byte(bytestream, size);
443 if (end - *bytestream < size)
444 return -1;
445 bytestream_put_buffer(bytestream, ptr, size);
446 ptr += size;
447 len -= size;
448 }
449 bytestream_put_byte(bytestream, 0x00); /* end of image block */
450 return 0;
451}
452
454{
455 GIFContext *s = avctx->priv_data;
456
457 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 s->transparent_index = -1;
463
465 s->buf_size = avctx->width*avctx->height*2 + 1000;
466 s->buf = av_malloc(s->buf_size);
467 s->tmpl = av_malloc(avctx->width);
468 if (!s->tmpl || !s->buf || !s->lzw)
469 return AVERROR(ENOMEM);
470
471 if (avpriv_set_systematic_pal2(s->palette, avctx->pix_fmt) < 0)
473
474 return 0;
475}
476
478 const AVFrame *pict, int *got_packet)
479{
480 GIFContext *s = avctx->priv_data;
481 uint8_t *outbuf_ptr, *end;
482 const uint32_t *palette = NULL;
483 int ret;
484
485 if ((ret = ff_alloc_packet(avctx, pkt, avctx->width*avctx->height*7/5 + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
486 return ret;
487 outbuf_ptr = pkt->data;
488 end = pkt->data + pkt->size;
489
490 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
491 palette = (uint32_t*)pict->data[1];
492
493 if (!s->palette_loaded) {
494 memcpy(s->palette, palette, AVPALETTE_SIZE);
495 s->transparent_index = get_palette_transparency_index(palette);
496 s->palette_loaded = 1;
497 if (s->use_global_palette)
498 palette = NULL;
499 } else if (!memcmp(s->palette, palette, AVPALETTE_SIZE)) {
500 palette = NULL;
501 }
502 }
503
504 ret = gif_image_write_image(avctx, &outbuf_ptr, end, palette,
505 pict->data[0], pict->linesize[0], pkt);
506 if (ret < 0)
507 return ret;
508 if (!s->last_frame && !s->image) {
509 s->last_frame = av_frame_alloc();
510 if (!s->last_frame)
511 return AVERROR(ENOMEM);
512 }
513
514 if (!s->image) {
515 ret = av_frame_replace(s->last_frame, pict);
516 if (ret < 0)
517 return ret;
518 }
519
520 pkt->size = outbuf_ptr - pkt->data;
521 if (s->image || !avctx->frame_num)
522 pkt->flags |= AV_PKT_FLAG_KEY;
523 *got_packet = 1;
524
525 return 0;
526}
527
529{
530 GIFContext *s = avctx->priv_data;
531
532 av_freep(&s->lzw);
533 av_freep(&s->buf);
534 av_freep(&s->shrunk_buf);
535 s->buf_size = 0;
536 av_frame_free(&s->last_frame);
537 av_freep(&s->tmpl);
538 return 0;
539}
540
541#define OFFSET(x) offsetof(GIFContext, x)
542#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
543static 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
552static 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
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,
565 .priv_data_size = sizeof(GIFContext),
568 .close = gif_encode_close,
571 .p.priv_class = &gif_class,
572 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
573};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
const FFCodec ff_gif_encoder
Definition gif.c:559
static const uint8_t palette[16][3]
Definition apnggen.c:50
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition bytestream.h:372
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:596
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition encode.c:63
#define FF_INPUT_BUFFER_MIN_SIZE
Used by some encoders as upper bound for the length of headers.
Definition encode.h:34
int ret
Definition ffplay.c:391
GIF format definitions.
#define GIF_IMAGE_SEPARATOR
Definition gif.h:46
static attribute_nonstring const uint8_t gif89a_sig[6]
Definition gif.h:37
#define GCE_DISPOSAL_INPLACE
Definition gif.h:40
#define GIF_EXTENSION_INTRODUCER
Definition gif.h:45
#define GIF_GCE_EXT_LABEL
Definition gif.h:47
#define GCE_DISPOSAL_BACKGROUND
Definition gif.h:41
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
Definition opt.h:254
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_GIF
Definition codec_id.h:147
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:657
#define AVERROR(e)
Definition error.h:45
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
Definition frame.c:379
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:247
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
const VDPAUPixFmtMap * map
int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
Definition imgutils.c:178
#define av_log2
Definition intmath.h:84
static const AVOption gif_options[]
Definition gif.c:543
static int is_image_translucent(AVCodecContext *avctx, const uint8_t *buf, const int linesize)
Definition gif.c:99
static void remap_frame_to_palette(const uint8_t *src, int src_linesize, uint8_t *dst, int dst_linesize, int w, int h, uint8_t *map)
Definition gif.c:90
static av_cold int gif_encode_close(AVCodecContext *avctx)
Definition gif.c:528
static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition gif.c:477
static void gif_crop_opaque(AVCodecContext *avctx, const uint32_t *palette, const uint8_t *buf, const int linesize, int *width, int *height, int *x_start, int *y_start)
Definition gif.c:233
static av_cold int gif_encode_init(AVCodecContext *avctx)
Definition gif.c:453
static void shrink_palette(const uint32_t *src, uint8_t *map, uint32_t *dst, size_t *palette_count)
Definition gif.c:67
@ GF_OFFSETTING
Definition gif.c:63
@ GF_TRANSDIFF
Definition gif.c:64
static const AVClass gif_class
Definition gif.c:552
static int get_palette_transparency_index(const uint32_t *palette)
Definition gif.c:120
static void gif_crop_translucent(AVCodecContext *avctx, const uint8_t *buf, const int linesize, int *width, int *height, int *x_start, int *y_start)
Definition gif.c:154
static int pick_palette_entry(const uint8_t *buf, int linesize, int w, int h)
Definition gif.c:138
#define OFFSET(x)
Definition gif.c:541
static int gif_image_write_image(AVCodecContext *avctx, uint8_t **bytestream, uint8_t *end, const uint32_t *palette, const uint8_t *buf, const int linesize, AVPacket *pkt)
Definition gif.c:292
#define DEFAULT_TRANSPARENCY_INDEX
Definition gif.c:44
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
uint8_t w
Definition llvidencdsp.c:39
LZW decoding routines.
void ff_lzw_encode_init(struct LZWEncodeState *s, uint8_t *outbuf, int outsize, int maxbits, enum FF_LZW_MODES mode, int little_endian)
Initialize LZW encoder.
Definition lzwenc.c:206
const int ff_lzw_encode_state_size
Definition lzwenc.c:68
int ff_lzw_encode(struct LZWEncodeState *s, const uint8_t *inbuf, int insize)
LZW main compress function.
Definition lzwenc.c:230
@ FF_LZW_GIF
Definition lzw.h:38
int ff_lzw_encode_flush(struct LZWEncodeState *s)
Write end code and flush bitstream.
Definition lzwenc.c:263
#define FFMIN(a, b)
Definition macros.h:49
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_RGB8
packed RGB 3:3:2, 8bpp, (msb)3R 3G 2B(lsb)
Definition pixfmt.h:93
@ AV_PIX_FMT_BGR8
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition pixfmt.h:90
@ AV_PIX_FMT_RGB4_BYTE
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
Definition pixfmt.h:95
@ AV_PIX_FMT_BGR4_BYTE
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
Definition pixfmt.h:92
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
#define AVPALETTE_COUNT
Definition pixfmt.h:33
#define AVPALETTE_SIZE
Definition pixfmt.h:32
#define FF_ARRAY_ELEMS(a)
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:644
int width
picture width / height.
Definition avcodec.h:605
int64_t frame_num
Frame counter, set by libavcodec.
Definition avcodec.h:1893
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition avcodec.h:629
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:500
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:524
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:587
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
int image
Definition gif.c:54
int palette_loaded
Definition gif.c:57
uint32_t palette[AVPALETTE_COUNT]
local reference palette for !pal8
Definition gif.c:56
int buf_size
Definition gif.c:51
uint8_t * tmpl
temporary line buffer
Definition gif.c:59
int use_global_palette
Definition gif.c:55
uint8_t * shrunk_buf
Definition gif.c:50
int flags
Definition gif.c:53
int transparent_index
Definition gif.c:58
AVFrame * last_frame
Definition gif.c:52
LZWState * lzw
Definition gif.c:48
uint8_t * buf
Definition gif.c:49
Definition lzw.c:46
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static int ref[MAX_W *MAX_W]
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99
int size
int len
static double c[64]