33 #define BITSTREAM_WRITER_LE 44 #define DEFAULT_TRANSPARENCY_INDEX 0x1f 74 for (
int y = 0; y < avctx->
height; y++) {
75 for (
int x = 0; x < avctx->
width; x++) {
76 if (buf[x] == trans) {
88 int transparent_color_index = -1;
89 unsigned i, smallest_alpha = 0xff;
95 const uint32_t v = palette[
i];
96 if (v >> 24 < smallest_alpha) {
97 smallest_alpha = v >> 24;
98 transparent_color_index =
i;
101 return smallest_alpha < 128 ? transparent_color_index : -1;
109 for (y = 0; y <
h; y++) {
110 for (x = 0; x <
w; x++)
123 int *x_start,
int *y_start)
130 const int w = avctx->
width;
136 while (*y_start < y_end) {
138 for (
int i = 0;
i <
w;
i++) {
139 if (buf[linesize * *y_start +
i] != trans) {
151 while (y_end > *y_start) {
153 for (
int i = 0;
i <
w;
i++) {
154 if (buf[linesize * y_end +
i] != trans) {
165 while (*x_start < x_end) {
167 for (
int i = *y_start;
i < y_end;
i++) {
168 if (buf[linesize *
i + *x_start] != trans) {
179 while (x_end > *x_start) {
181 for (
int i = *y_start;
i < y_end;
i++) {
182 if (buf[linesize *
i + x_end] != trans) {
192 *height = y_end + 1 - *y_start;
193 *width = x_end + 1 - *x_start;
195 *width, *height, *x_start, *y_start, avctx->
width, avctx->
height);
202 int *
width,
int *
height,
int *x_start,
int *y_start)
210 int x_end = avctx->
width - 1,
211 y_end = avctx->
height - 1;
214 while (*y_start < y_end) {
215 if (memcmp(ref + *y_start*ref_linesize, buf + *y_start*linesize, *width))
219 while (y_end > *y_start) {
220 if (memcmp(ref + y_end*ref_linesize, buf + y_end*linesize, *width))
224 *height = y_end + 1 - *y_start;
227 while (*x_start < x_end) {
229 for (
int y = *y_start; y <= y_end; y++) {
230 if (ref[y*ref_linesize + *x_start] != buf[y*linesize + *x_start]) {
239 while (x_end > *x_start) {
241 for (
int y = *y_start; y <= y_end; y++) {
242 if (ref[y*ref_linesize + x_end] != buf[y*linesize + x_end]) {
251 *width = x_end + 1 - *x_start;
254 *width, *height, *x_start, *y_start, avctx->
width, avctx->
height);
272 honor_transparency = 0;
284 if (sar.
num > 0 && sar.
den > 0) {
285 aspect = sar.
num * 64LL / sar.
den - 15;
286 if (aspect < 0 || aspect > 255)
291 bytestream_put_le16(bytestream, avctx->
width);
292 bytestream_put_le16(bytestream, avctx->
height);
296 bytestream_put_byte(bytestream, 0xf7);
298 bytestream_put_byte(bytestream, aspect);
299 for (
int i = 0;
i < 256;
i++) {
300 const uint32_t v = global_palette[
i] & 0xffffff;
301 bytestream_put_be24(bytestream, v);
305 if (honor_transparency && trans < 0) {
313 honor_transparency = 0;
320 bytestream_put_byte(bytestream, 0x04);
321 bytestream_put_byte(bytestream, disposal<<2 | (bcid >= 0));
322 bytestream_put_le16(bytestream, 5);
324 bytestream_put_byte(bytestream, 0x00);
328 bytestream_put_le16(bytestream, x_start);
329 bytestream_put_le16(bytestream, y_start);
330 bytestream_put_le16(bytestream,
width);
331 bytestream_put_le16(bytestream,
height);
334 bytestream_put_byte(bytestream, 0x00);
337 bytestream_put_byte(bytestream, 1<<7 | 0x7);
339 const uint32_t v = palette[
i];
340 bytestream_put_be24(bytestream, v);
344 bytestream_put_byte(bytestream, 0x08);
349 ptr = buf + y_start*linesize + x_start;
350 if (honor_transparency) {
354 for (y = 0; y <
height; y++) {
356 for (x = 0; x <
width; x++)
357 if (ref[x] == ptr[x])
364 for (y = 0; y <
height; y++) {
374 bytestream_put_byte(bytestream, size);
375 if (end - *bytestream < size)
381 bytestream_put_byte(bytestream, 0x00);
389 if (avctx->
width > 65535 || avctx->
height > 65535) {
410 const AVFrame *pict,
int *got_packet)
419 outbuf_ptr = pkt->
data;
423 palette = (uint32_t*)pict->
data[1];
469 #define OFFSET(x) offsetof(GIFContext, x) 470 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM #define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
This structure describes decoded (raw) audio or video data.
uint32_t palette[AVPALETTE_COUNT]
local reference palette for !pal8
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
#define LIBAVUTIL_VERSION_INT
static av_cold int init(AVCodecContext *avctx)
#define GCE_DISPOSAL_BACKGROUND
static int get_palette_transparency_index(const uint32_t *palette)
static int gif_encode_close(AVCodecContext *avctx)
static const AVClass gif_class
const char * av_default_item_name(void *ptr)
Return the context name.
int ff_lzw_encode(struct LZWEncodeState *s, const uint8_t *inbuf, int insize)
LZW main compress function.
#define GIF_GCE_EXT_LABEL
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
8 bits with AV_PIX_FMT_RGB32 palette
int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
static av_cold int end(AVCodecContext *avctx)
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
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)
uint8_t * tmpl
temporary line buffer
#define AV_INPUT_BUFFER_MIN_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int ff_lzw_encode_flush(struct LZWEncodeState *s, void(*lzw_flush_put_bits)(struct PutBitContext *))
#define i(width, name, range_min, range_max)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
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 flags
A combination of AV_PKT_FLAG values.
#define GIF_IMAGE_SEPARATOR
static int pick_palette_entry(const uint8_t *buf, int linesize, int w, int h)
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
static void gif_crop_translucent(AVCodecContext *avctx, const uint8_t *buf, const int linesize, int *width, int *height, int *x_start, int *y_start)
int width
picture width / height.
#define GIF_EXTENSION_INTRODUCER
#define FF_ARRAY_ELEMS(a)
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
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)
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Describe the class of an AVClass context structure.
Rational number (pair of numerator and denominator).
static enum AVPixelFormat pix_fmts[]
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
#define GCE_DISPOSAL_INPLACE
static int is_image_translucent(AVCodecContext *avctx, const uint8_t *buf, const int linesize)
void ff_lzw_encode_init(struct LZWEncodeState *s, uint8_t *outbuf, int outsize, int maxbits, enum FF_LZW_MODES mode, void(*lzw_put_bits)(struct PutBitContext *, int, unsigned int))
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static int ref[MAX_W *MAX_W]
static const uint8_t gif89a_sig[6]
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
static av_cold int gif_encode_init(AVCodecContext *avctx)
int frame_number
Frame counter, set by libavcodec.
const int ff_lzw_encode_state_size
static const AVOption gif_options[]
AVPixelFormat
Pixel format.
This structure stores compressed data.
#define DEFAULT_TRANSPARENCY_INDEX