Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gif transparency fix #3

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ada564c
fix for transparencies in animated gifs
bejayoharen Jan 16, 2018
88a7c75
bugfixes for edge and 2nd frame artifacts
bejayoharen Feb 5, 2018
77cd1d0
fixes for corrupt gifs
bejayoharen Mar 16, 2018
e8f1d7d
Merge remote-tracking branch 'upstream/master'
cyburgee Mar 19, 2018
9e5c00c
Merge remote-tracking branch 'upstream/master'
cyburgee Apr 4, 2018
6e06057
actually fix conflict
cyburgee Apr 4, 2018
e6bd543
Merge remote-tracking branch 'upstream/master'
cyburgee Apr 18, 2018
2e5e052
Merge remote-tracking branch 'upstream/master'
cyburgee May 22, 2018
8f5485b
Merge remote-tracking branch 'upstream/master'
cyburgee Oct 18, 2018
5eaaf8e
CR-1266: Fix infinite loop bug on transparent optimization
jagraff Nov 26, 2018
a35311a
Merge pull request #1 from Giphy/CR-1266
jagraff Nov 29, 2018
6e3b807
Merge remote-tracking branch 'upstream/master'
cyburgee Mar 14, 2019
5cfb22c
fix conflict res issue
cyburgee Mar 14, 2019
2fc69e5
Merge pull request #2 from Giphy/upstream-merge-03-14-19-v2
cyburgee Mar 14, 2019
c6180f3
fix transparent crop
cyburgee Apr 19, 2019
d69c4cf
Update gif.c
cyburgee Apr 19, 2019
8376342
Fix bug with transparent gif cropping
jagraff May 6, 2019
561517b
Optimize
jagraff May 6, 2019
d199ea7
Remove extra logging
jagraff May 6, 2019
9f697ed
Fix disposal method issue
jagraff May 10, 2019
9ecadad
Fix for duration bug
jagraff Jun 11, 2019
1647406
Don't need to specify prop
jagraff Jun 11, 2019
f837eeb
Change get delay function
jagraff Jun 11, 2019
b3f31a3
Change get delay function
jagraff Jun 11, 2019
4f1587a
Spacing
jagraff Jun 11, 2019
f2b8cce
Remove incorrect AV_NOPTS_VALUE check
jagraff Jun 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions libavcodec/avcodec.h
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,12 @@ enum AVPacketSideDataType {
*/
AV_PKT_DATA_A53_CC,

/**
* The disposal method that should be used with the frame. If missing,
* the frame will not be disposed. This contains exactly one byte.
*/
AV_PKT_DATA_GIF_FRAME_DISPOSAL,

/**
* This side data is encryption initialization data.
* The format is not part of ABI, use av_encryption_init_info_* methods to
Expand Down
31 changes: 24 additions & 7 deletions libavcodec/gif.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ static void gif_crop_translucent(AVCodecContext *avctx,
GIFContext *s = avctx->priv_data;
int trans = s->transparent_index;

av_log(avctx, AV_LOG_DEBUG,"avctx frame: %d\n", avctx->frame_number);
jagraff marked this conversation as resolved.
Show resolved Hide resolved

/* Crop image */
if ((s->flags & GF_OFFSETTING) && trans >= 0) {
const int w = avctx->width;
Expand All @@ -136,61 +138,70 @@ static void gif_crop_translucent(AVCodecContext *avctx,
while (*y_start < y_end) {
int is_trans = 1;
for (int i = 0; i < w; i++) {
if (buf[w * *y_start + i] != trans) {
if (buf[linesize * *y_start + i] != trans) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(ignoring this file as it's already mostly been accepted in mainline)

is_trans = 0;
break;
}
}

if (!is_trans)
break;
// av_log(avctx, AV_LOG_DEBUG,"crop top: %dx%d image at pos (%d;%d) [area:%dx%d]\n",
// *width, *height, *x_start, *y_start, avctx->width, avctx->height);
(*y_start)++;
}

// crop bottom
while (y_end < h) {
while (y_end > *y_start) {
int is_trans = 1;
for (int i = 0; i < w; i++) {
if (buf[w * y_end + i] != trans) {
if (buf[linesize * y_end + i] != trans) {
is_trans = 0;
break;
}
}
if (!is_trans)
break;
// av_log(avctx, AV_LOG_DEBUG,"crop bottom: %dx%d image at pos (%d;%d) [area:%dx%d]\n",
// *width, *height, *x_start, *y_start, avctx->width, avctx->height);
y_end--;
}

// crop left
while (*x_start < x_end) {
int is_trans = 1;
for (int i = *y_start; i < y_end; i++) {
if (buf[w * i + *x_start] != trans) {
if (buf[linesize * i + *x_start] != trans) {
is_trans = 0;
break;
}
}
if (!is_trans)
break;
// av_log(avctx, AV_LOG_DEBUG,"crop left: %dx%d image at pos (%d;%d) [area:%dx%d]\n",
// *width, *height, *x_start, *y_start, avctx->width, avctx->height);
(*x_start)++;
}

// crop right
while (x_end < w) {
while (x_end > *x_start) {
int is_trans = 1;
for (int i = *y_start; i < y_end; i++) {
if (buf[w * i + x_end] != trans) {
if (buf[linesize * i + x_end] != trans) {
is_trans = 0;
break;
}
}
if (!is_trans)
break;
// av_log(avctx, AV_LOG_DEBUG,"crop right: %dx%d image at pos (%d;%d) [area:%dx%d]\n",
// *width, *height, *x_start, *y_start, avctx->width, avctx->height);
x_end--;
}

*height = y_end + 1 - *y_start;
*width = x_end + 1 - *x_start;

av_log(avctx, AV_LOG_DEBUG,"%dx%d image at pos (%d;%d) [area:%dx%d]\n",
*width, *height, *x_start, *y_start, avctx->width, avctx->height);
}
Expand Down Expand Up @@ -343,11 +354,13 @@ static int gif_image_write_image(AVCodecContext *avctx,

bytestream_put_byte(bytestream, 0x08);

// av_log(avctx, AV_LOG_DEBUG, "frame: %d buf_size: %d\n", avctx->frame_number, s->buf_size);
ff_lzw_encode_init(s->lzw, s->buf, s->buf_size,
12, FF_LZW_GIF, put_bits);

ptr = buf + y_start*linesize + x_start;
if (honor_transparency) {
av_log(avctx, AV_LOG_DEBUG, "frame: %d honoring transparency\n", avctx->frame_number);
const int ref_linesize = s->last_frame->linesize[0];
const uint8_t *ref = s->last_frame->data[0] + y_start*ref_linesize + x_start;

Expand All @@ -361,9 +374,13 @@ static int gif_image_write_image(AVCodecContext *avctx,
ref += ref_linesize;
}
} else {
av_log(avctx, AV_LOG_DEBUG, "frame: %d not honoring transparency\n", avctx->frame_number);
av_log(avctx, AV_LOG_DEBUG, "frame: %d lzw encoding image of height: %d\n", avctx->frame_number, height);
for (y = 0; y < height; y++) {
len += ff_lzw_encode(s->lzw, ptr, width);
av_log(avctx, AV_LOG_DEBUG, "frame: %d lzw encoding image of width: %d\n", avctx->frame_number, width);
ptr += linesize;
av_log(avctx, AV_LOG_DEBUG, "frame: %d wrote linesize: %d\n", avctx->frame_number, linesize);
}
}
len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
Expand Down Expand Up @@ -497,4 +514,4 @@ AVCodec ff_gif_encoder = {
AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE
},
.priv_class = &gif_class,
};
};
4 changes: 4 additions & 0 deletions libavdevice/indev_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
static const AVInputFormat * const indev_list[] = {
&ff_avfoundation_demuxer,
&ff_lavfi_demuxer,
NULL };
2 changes: 2 additions & 0 deletions libavdevice/outdev_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
static const AVOutputFormat * const outdev_list[] = {
NULL };