Go to the documentation of this file.
43 #define DEFAULT_TRANSPARENCY_INDEX 0x1f
67 uint32_t *
dst,
size_t *palette_count)
69 size_t colors_seen = 0;
73 for (
size_t c = 0;
c < colors_seen;
c++) {
86 *palette_count = colors_seen;
90 uint8_t *
dst,
int dst_linesize,
91 int w,
int h, uint8_t *
map)
93 for (
int i = 0;
i <
h;
i++)
94 for (
int j = 0; j <
w; j++)
95 dst[
i * dst_linesize + j] =
map[
src[
i * src_linesize + j]];
99 const uint8_t *buf,
const int linesize)
102 int trans =
s->transparent_index;
107 for (
int y = 0; y < avctx->
height; y++) {
108 for (
int x = 0; x < avctx->
width; x++) {
109 if (buf[x] == trans) {
121 int transparent_color_index = -1;
122 unsigned i, smallest_alpha = 0xff;
128 const uint32_t v = palette[
i];
129 if (v >> 24 < smallest_alpha) {
130 smallest_alpha = v >> 24;
131 transparent_color_index =
i;
134 return smallest_alpha < 128 ? transparent_color_index : -1;
142 for (y = 0; y <
h; y++) {
143 for (x = 0; x <
w; x++)
154 const uint8_t *buf,
const int linesize,
156 int *x_start,
int *y_start)
159 int trans =
s->transparent_index;
163 const int w = avctx->
width;
169 while (*y_start < y_end) {
171 for (
int i = 0;
i <
w;
i++) {
172 if (buf[linesize * *y_start +
i] != trans) {
184 while (y_end > *y_start) {
186 for (
int i = 0;
i <
w;
i++) {
187 if (buf[linesize * y_end +
i] != trans) {
198 while (*x_start < x_end) {
200 for (
int i = *y_start;
i < y_end;
i++) {
201 if (buf[linesize *
i + *x_start] != trans) {
212 while (x_end > *x_start) {
214 for (
int i = *y_start;
i < y_end;
i++) {
215 if (buf[linesize *
i + x_end] != trans) {
225 *
height = y_end + 1 - *y_start;
226 *
width = x_end + 1 - *x_start;
233 const uint32_t *palette,
234 const uint8_t *buf,
const int linesize,
235 int *
width,
int *
height,
int *x_start,
int *y_start)
241 const uint8_t *
ref =
s->last_frame->data[0];
242 const int ref_linesize =
s->last_frame->linesize[0];
243 int x_end = avctx->
width - 1,
244 y_end = avctx->
height - 1;
247 while (*y_start < y_end) {
248 if (memcmp(
ref + *y_start*ref_linesize, buf + *y_start*linesize, *
width))
252 while (y_end > *y_start) {
253 if (memcmp(
ref + y_end*ref_linesize, buf + y_end*linesize, *
width))
257 *
height = y_end + 1 - *y_start;
260 while (*x_start < x_end) {
262 for (
int y = *y_start; y <= y_end; y++) {
263 if (
ref[y*ref_linesize + *x_start] != buf[y*linesize + *x_start]) {
272 while (x_end > *x_start) {
274 for (
int y = *y_start; y <= y_end; y++) {
275 if (
ref[y*ref_linesize + x_end] != buf[y*linesize + x_end]) {
284 *
width = x_end + 1 - *x_start;
292 uint8_t **bytestream, uint8_t *end,
293 const uint32_t *palette,
294 const uint8_t *buf,
const int linesize,
299 int x_start = 0, y_start = 0, trans =
s->transparent_index;
300 int bcid = -1, honor_transparency = (
s->flags &
GF_TRANSDIFF) &&
s->last_frame && !palette;
304 size_t shrunk_palette_count = 0;
316 honor_transparency = 0;
323 if (
s->image || !avctx->frame_num) {
324 const uint32_t *global_palette = palette ? palette :
s->palette;
325 const AVRational sar = avctx->sample_aspect_ratio;
328 if (sar.
num > 0 && sar.
den > 0) {
329 aspect = sar.
num * 64LL / sar.
den - 15;
330 if (aspect < 0 || aspect > 255)
335 bytestream_put_le16(bytestream, avctx->width);
336 bytestream_put_le16(bytestream, avctx->height);
340 bytestream_put_byte(bytestream, ((uint8_t)
s->use_global_palette << 7) | 0x70 | (
s->use_global_palette ? 7 : 0));
342 bytestream_put_byte(bytestream, aspect);
343 if (
s->use_global_palette) {
344 for (
int i = 0;
i < 256;
i++) {
345 const uint32_t v = global_palette[
i] & 0xffffff;
346 bytestream_put_be24(bytestream, v);
351 if (honor_transparency && trans < 0) {
359 honor_transparency = 0;
361 if (palette || !
s->use_global_palette) {
362 const uint32_t *pal = palette ? palette :
s->palette;
371 bytestream_put_byte(bytestream, 0x04);
372 bytestream_put_byte(bytestream, disposal<<2 | (bcid >= 0));
373 bytestream_put_le16(bytestream, 5);
375 bytestream_put_byte(bytestream, 0x00);
379 bytestream_put_le16(bytestream, x_start);
380 bytestream_put_le16(bytestream, y_start);
381 bytestream_put_le16(bytestream,
width);
382 bytestream_put_le16(bytestream,
height);
384 if (palette || !
s->use_global_palette) {
385 unsigned pow2_count =
av_log2(shrunk_palette_count - 1);
388 bytestream_put_byte(bytestream, 1<<7 | pow2_count);
389 for (
i = 0;
i < 1 << (pow2_count + 1);
i++) {
390 const uint32_t v = shrunk_palette[
i];
391 bytestream_put_be24(bytestream, v);
394 bytestream_put_byte(bytestream, 0x00);
397 bytestream_put_byte(bytestream, 0x08);
402 if (shrunk_palette_count) {
403 if (!
s->shrunk_buf) {
404 s->shrunk_buf =
av_malloc(avctx->height * linesize);
405 if (!
s->shrunk_buf) {
411 ptr =
s->shrunk_buf + y_start*linesize + x_start;
413 ptr = buf + y_start*linesize + x_start;
415 if (honor_transparency) {
416 const int ref_linesize =
s->last_frame->linesize[0];
417 const uint8_t *
ref =
s->last_frame->data[0] + y_start*ref_linesize + x_start;
419 for (y = 0; y <
height; y++) {
420 memcpy(
s->tmpl, ptr,
width);
421 for (x = 0; x <
width; x++)
422 if (
ref[x] == ptr[x])
429 for (y = 0; y <
height; y++) {
439 bytestream_put_byte(bytestream,
size);
440 if (end - *bytestream <
size)
446 bytestream_put_byte(bytestream, 0x00);
454 if (avctx->
width > 65535 || avctx->
height > 65535) {
459 s->transparent_index = -1;
465 if (!
s->tmpl || !
s->buf || !
s->lzw)
475 const AVFrame *pict,
int *got_packet)
478 uint8_t *outbuf_ptr, *end;
479 const uint32_t *palette =
NULL;
488 palette = (uint32_t*)pict->
data[1];
490 if (!
s->palette_loaded) {
493 s->palette_loaded = 1;
501 if (!
s->last_frame && !
s->image) {
534 #define OFFSET(x) offsetof(GIFContext, x)
535 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
541 {
"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 },
AVPixelFormat
Pixel format.
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
static int is_image_translucent(AVCodecContext *avctx, const uint8_t *buf, const int linesize)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
This structure describes decoded (raw) audio or video data.
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)
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.
#define GIF_GCE_EXT_LABEL
static int gif_encode_close(AVCodecContext *avctx)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define FF_INPUT_BUFFER_MIN_SIZE
Used by some encoders as upper bound for the length of headers.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static void gif_crop_translucent(AVCodecContext *avctx, const uint8_t *buf, const int linesize, int *width, int *height, int *x_start, int *y_start)
AVCodec p
The public AVCodec.
const FFCodec ff_gif_encoder
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)
uint32_t palette[AVPALETTE_COUNT]
local reference palette for !pal8
#define DEFAULT_TRANSPARENCY_INDEX
static void shrink_palette(const uint32_t *src, uint8_t *map, uint32_t *dst, size_t *palette_count)
#define FF_CODEC_ENCODE_CB(func)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
static const uint8_t gif89a_sig[6]
@ AV_PIX_FMT_BGR8
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define FF_ARRAY_ELEMS(a)
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define GIF_IMAGE_SEPARATOR
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
#define CODEC_LONG_NAME(str)
#define LIBAVUTIL_VERSION_INT
Describe the class of an AVClass context structure.
uint8_t * tmpl
temporary line buffer
Rational number (pair of numerator and denominator).
const char * av_default_item_name(void *ptr)
Return the context name.
@ AV_PIX_FMT_RGB8
packed RGB 3:3:2, 8bpp, (msb)3R 3G 2B(lsb)
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
@ AV_PIX_FMT_BGR4_BYTE
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
int(* init)(AVBSFContext *ctx)
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
static int get_palette_transparency_index(const uint32_t *palette)
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static av_cold int gif_encode_init(AVCodecContext *avctx)
int flags
A combination of AV_PKT_FLAG values.
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
#define i(width, name, range_min, range_max)
#define GIF_EXTENSION_INTRODUCER
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
const char * name
Name of the codec implementation.
int ff_lzw_encode_flush(struct LZWEncodeState *s)
Write end code and flush bitstream.
static const AVClass gif_class
@ AV_PIX_FMT_RGB4_BYTE
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
int64_t frame_num
Frame counter, set by libavcodec.
#define GCE_DISPOSAL_BACKGROUND
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static const AVOption gif_options[]
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)
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
main external API structure.
static int ref[MAX_W *MAX_W]
const VDPAUPixFmtMap * map
This structure stores compressed data.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
const int ff_lzw_encode_state_size
int width
picture width / height.
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
#define flags(name, subs,...)
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
#define GCE_DISPOSAL_INPLACE
static int pick_palette_entry(const uint8_t *buf, int linesize, int w, int h)
int ff_lzw_encode(struct LZWEncodeState *s, const uint8_t *inbuf, int insize)
LZW main compress function.
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.