Skip to content

Commit 2a7f1bc

Browse files
elenrilmichaelni
authored andcommitted
lavc/pngdec: always create a copy for APNG_DISPOSE_OP_BACKGROUND
Calling av_frame_make_writable() from decoders is tricky, especially when frame threading is used. It is much simpler and safer to just make a private copy of the frame. This is not expected to have a major performance impact, since APNG_DISPOSE_OP_BACKGROUND is not used often and av_frame_make_writable() would typically make a copy anyway. Found-by: James Almer <jamrial@gmail.com> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc> (cherry picked from commit b593abd) Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
1 parent 25e794a commit 2a7f1bc

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

libavcodec/pngdec.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ typedef struct PNGDecContext {
8989
int has_trns;
9090
uint8_t transparent_color_be[6];
9191

92+
uint8_t *background_buf;
93+
unsigned background_buf_allocated;
9294
uint32_t palette[256];
9395
uint8_t *crow_buf;
9496
uint8_t *last_row;
@@ -1091,19 +1093,20 @@ static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s,
10911093
ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
10921094

10931095
// need to reset a rectangle to background:
1094-
// create a new writable copy
10951096
if (s->last_dispose_op == APNG_DISPOSE_OP_BACKGROUND) {
1096-
int ret = av_frame_make_writable(s->last_picture.f);
1097-
if (ret < 0)
1098-
return ret;
1097+
av_fast_malloc(&s->background_buf, &s->background_buf_allocated,
1098+
src_stride * p->height);
1099+
if (!s->background_buf)
1100+
return AVERROR(ENOMEM);
10991101

1100-
src = s->last_picture.f->data[0];
1101-
src_stride = s->last_picture.f->linesize[0];
1102+
memcpy(s->background_buf, src, src_stride * p->height);
11021103

11031104
for (y = s->last_y_offset; y < s->last_y_offset + s->last_h; y++) {
1104-
memset(s->last_picture.f->data[0] + src_stride * y +
1105+
memset(s->background_buf + src_stride * y +
11051106
s->bpp * s->last_x_offset, 0, s->bpp * s->last_w);
11061107
}
1108+
1109+
src = s->background_buf;
11071110
}
11081111

11091112
// copy unchanged rectangles from the last frame
@@ -1738,6 +1741,7 @@ static av_cold int png_dec_end(AVCodecContext *avctx)
17381741
s->last_row_size = 0;
17391742
av_freep(&s->tmp_row);
17401743
s->tmp_row_size = 0;
1744+
av_freep(&s->background_buf);
17411745

17421746
av_freep(&s->iccp_data);
17431747
av_dict_free(&s->frame_metadata);

0 commit comments

Comments
 (0)