[PATCH] pngdec: frame multithreading support
Signed-off-by: Paul B Mahol <onemda@gmail.com> --- libavcodec/pngdec.c | 71 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 18 deletions(-) diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c index 1358ac5..dcdbb2f 100644 --- a/libavcodec/pngdec.c +++ b/libavcodec/pngdec.c @@ -28,6 +28,7 @@ #include "internal.h" #include "png.h" #include "pngdsp.h" +#include "thread.h" /* TODO: * - add 16 bit depth support @@ -40,7 +41,8 @@ typedef struct PNGDecContext { AVCodecContext *avctx; GetByteContext gb; - AVFrame *prev; + ThreadFrame last_picture; + ThreadFrame picture; int state; int width, height; @@ -505,13 +507,18 @@ static int decode_frame(AVCodecContext *avctx, PNGDecContext * const s = avctx->priv_data; const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; - AVFrame *p = data; + AVFrame *p; AVDictionary *metadata = NULL; uint8_t *crow_buf_base = NULL; uint32_t tag, length; int64_t sig; int ret; + if (s->last_picture.f) + ff_thread_release_buffer(avctx, &s->last_picture); + FFSWAP(ThreadFrame, s->picture, s->last_picture); + p = s->picture.f; + bytestream2_init(&s->gb, buf, buf_size); /* check signature */ @@ -635,8 +642,10 @@ static int decode_frame(AVCodecContext *avctx, goto fail; } - if (ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF) < 0) + if (ff_thread_get_buffer(avctx, &s->picture, AV_GET_BUFFER_FLAG_REF) < 0) goto fail; + ff_thread_finish_setup(avctx); + p->pict_type = AV_PICTURE_TYPE_I; p->key_frame = 1; p->interlaced_frame = !!s->interlace_type; @@ -820,16 +829,17 @@ static int decode_frame(AVCodecContext *avctx, } /* handle p-frames only if a predecessor frame is available */ - if (s->prev->data[0]) { + if (s->last_picture.f->data[0]) { if ( !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG") - && s->prev->width == p->width - && s->prev->height== p->height - && s->prev->format== p->format + && s->last_picture.f->width == p->width + && s->last_picture.f->height== p->height + && s->last_picture.f->format== p->format ) { int i, j; uint8_t *pd = p->data[0]; - uint8_t *pd_last = s->prev->data[0]; + uint8_t *pd_last = s->last_picture.f->data[0]; + ff_thread_await_progress(&s->last_picture, INT_MAX, 0); for (j = 0; j < s->height; j++) { for (i = 0; i < s->width * s->bpp; i++) { pd[i] += pd_last[i]; @@ -839,13 +849,13 @@ static int decode_frame(AVCodecContext *avctx, } } } + ff_thread_report_progress(&s->picture, INT_MAX, 0); av_frame_set_metadata(p, metadata); metadata = NULL; - av_frame_unref(s->prev); - if ((ret = av_frame_ref(s->prev, p)) < 0) - goto fail; + if ((ret = av_frame_ref(data, s->picture.f)) < 0) + return ret; *got_frame = 1; @@ -863,17 +873,35 @@ static int decode_frame(AVCodecContext *avctx, goto the_end; } +static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src) +{ + PNGDecContext *psrc = src->priv_data; + PNGDecContext *pdst = dst->priv_data; + + if (dst == src) + return 0; + + ff_thread_release_buffer(dst, &pdst->picture); + if (psrc->picture.f->data[0]) + return ff_thread_ref_frame(&pdst->picture, &psrc->picture); + + return 0; +} + static av_cold int png_dec_init(AVCodecContext *avctx) { PNGDecContext *s = avctx->priv_data; - s->prev = av_frame_alloc(); - if (!s->prev) + s->avctx = avctx; + s->last_picture.f = av_frame_alloc(); + s->picture.f = av_frame_alloc(); + if (!s->last_picture.f || !s->picture.f) return AVERROR(ENOMEM); - ff_pngdsp_init(&s->dsp); - - s->avctx = avctx; + if (!avctx->internal->is_copy) { + avctx->internal->allocate_progress = 1; + ff_pngdsp_init(&s->dsp); + } return 0; } @@ -882,7 +910,12 @@ static av_cold int png_dec_end(AVCodecContext *avctx) { PNGDecContext *s = avctx->priv_data; - av_frame_free(&s->prev); + if (s->last_picture.f) + ff_thread_release_buffer(avctx, &s->last_picture); + av_frame_free(&s->last_picture.f); + if (s->picture.f) + ff_thread_release_buffer(avctx, &s->picture); + av_frame_free(&s->picture.f); return 0; } @@ -895,6 +928,8 @@ AVCodec ff_png_decoder = { .init = png_dec_init, .close = png_dec_end, .decode = decode_frame, - .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/, + .init_thread_copy = ONLY_IF_THREADS_ENABLED(png_dec_init), + .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context), + .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS /*| CODEC_CAP_DRAW_HORIZ_BAND*/, .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"), }; -- 1.7.11.2
On Tue, Aug 20, 2013 at 11:04:58PM +0000, Paul B Mahol wrote:
Signed-off-by: Paul B Mahol <onemda@gmail.com> --- libavcodec/pngdec.c | 71 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 18 deletions(-)
deadlocks with some damaged files Id Target Id Frame 16 Thread 0x7fffe598a700 (LWP 31556) "ffmpeg_g" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 15 Thread 0x7fffe618b700 (LWP 31555) "ffmpeg_g" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 14 Thread 0x7fffe698c700 (LWP 31554) "ffmpeg_g" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 13 Thread 0x7fffe718d700 (LWP 31553) "ffmpeg_g" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 12 Thread 0x7fffe798e700 (LWP 31552) "ffmpeg_g" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 11 Thread 0x7fffe818f700 (LWP 31551) "ffmpeg_g" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 10 Thread 0x7fffe8990700 (LWP 31550) "ffmpeg_g" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 9 Thread 0x7fffe9191700 (LWP 31549) "ffmpeg_g" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 8 Thread 0x7fffe9992700 (LWP 31548) "ffmpeg_g" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 7 Thread 0x7fffea193700 (LWP 31547) "ffmpeg_g" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 6 Thread 0x7fffea994700 (LWP 31546) "ffmpeg_g" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 5 Thread 0x7fffeb195700 (LWP 31545) "ffmpeg_g" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 4 Thread 0x7fffeb996700 (LWP 31544) "ffmpeg_g" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 3 Thread 0x7fffec197700 (LWP 31543) "ffmpeg_g" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 2 Thread 0x7fffec998700 (LWP 31542) "ffmpeg_g" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 * 1 Thread 0x7ffff7faa780 (LWP 31538) "ffmpeg_g" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 (gdb) bt #0 pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 #1 0x00000000008e0d6b in ff_thread_decode_frame (avctx=0x174cfe0, picture=0x173eea0, got_picture_ptr=0x7fffffffdcd8, avpkt=0x7fffffffda00) at libavcodec/pthread.c:631 #2 0x0000000000980b84 in avcodec_decode_video2 (avctx=0x174cfe0, picture=0x173eea0, got_picture_ptr=0x7fffffffdcd8, avpkt=0x7fffffffdc20) at libavcodec/utils.c:1979 #3 0x0000000000424086 in decode_video (ist=0x174ecc0, pkt=0x7fffffffdc20, got_output=0x7fffffffdcd8) at ffmpeg.c:1668 #4 0x0000000000424f13 in output_packet (ist=0x174ecc0, pkt=0x7fffffffdda0) at ffmpeg.c:1866 #5 0x000000000042aae3 in process_input (file_index=0) at ffmpeg.c:3085 #6 0x000000000042ae62 in transcode_step () at ffmpeg.c:3181 #7 0x000000000042af6f in transcode () at ffmpeg.c:3233 #8 0x000000000042b49a in main (argc=8, argv=0x7fffffffe3c8) at ffmpeg.c:3411 #0 pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 #1 0x00000000008e106b in ff_thread_await_progress (f=0x1752330, n=2147483647, field=0) at libavcodec/pthread.c:692 #2 0x00000000008d26f3 in decode_frame (avctx=0x1753120, data=0x17529f0, got_frame=0x1752c60, avpkt=0x1752980) at libavcodec/pngdec.c:842 #3 0x00000000008e0103 in frame_worker_thread (arg=0x1752880) at libavcodec/pthread.c:339 #4 0x00007ffff0467e9a in start_thread (arg=0x7fffe618b700) at pthread_create.c:308 #5 0x00007ffff0194ccd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:112 #6 0x0000000000000000 in ?? () the rest have backtraces like this: #0 pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162 #1 0x00000000008e003b in frame_worker_thread (arg=0x1752ca0) at libavcodec/pthread.c:330 #2 0x00007ffff0467e9a in start_thread (arg=0x7fffe598a700) at pthread_create.c:308 #3 0x00007ffff0194ccd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:112 #4 0x0000000000000000 in ?? () [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB it is not once nor twice but times without number that the same ideas make their appearance in the world. -- Aristotle
Signed-off-by: Paul B Mahol <onemda@gmail.com> --- libavcodec/pngdec.c | 69 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 18 deletions(-) diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c index 1358ac5..16225db 100644 --- a/libavcodec/pngdec.c +++ b/libavcodec/pngdec.c @@ -28,6 +28,7 @@ #include "internal.h" #include "png.h" #include "pngdsp.h" +#include "thread.h" /* TODO: * - add 16 bit depth support @@ -40,7 +41,8 @@ typedef struct PNGDecContext { AVCodecContext *avctx; GetByteContext gb; - AVFrame *prev; + ThreadFrame last_picture; + ThreadFrame picture; int state; int width, height; @@ -505,13 +507,17 @@ static int decode_frame(AVCodecContext *avctx, PNGDecContext * const s = avctx->priv_data; const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; - AVFrame *p = data; + AVFrame *p; AVDictionary *metadata = NULL; uint8_t *crow_buf_base = NULL; uint32_t tag, length; int64_t sig; int ret; + ff_thread_release_buffer(avctx, &s->last_picture); + FFSWAP(ThreadFrame, s->picture, s->last_picture); + p = s->picture.f; + bytestream2_init(&s->gb, buf, buf_size); /* check signature */ @@ -635,8 +641,10 @@ static int decode_frame(AVCodecContext *avctx, goto fail; } - if (ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF) < 0) + if (ff_thread_get_buffer(avctx, &s->picture, AV_GET_BUFFER_FLAG_REF) < 0) goto fail; + ff_thread_finish_setup(avctx); + p->pict_type = AV_PICTURE_TYPE_I; p->key_frame = 1; p->interlaced_frame = !!s->interlace_type; @@ -820,16 +828,17 @@ static int decode_frame(AVCodecContext *avctx, } /* handle p-frames only if a predecessor frame is available */ - if (s->prev->data[0]) { + if (s->last_picture.f->data[0]) { if ( !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG") - && s->prev->width == p->width - && s->prev->height== p->height - && s->prev->format== p->format + && s->last_picture.f->width == p->width + && s->last_picture.f->height== p->height + && s->last_picture.f->format== p->format ) { int i, j; uint8_t *pd = p->data[0]; - uint8_t *pd_last = s->prev->data[0]; + uint8_t *pd_last = s->last_picture.f->data[0]; + ff_thread_await_progress(&s->last_picture, INT_MAX, 0); for (j = 0; j < s->height; j++) { for (i = 0; i < s->width * s->bpp; i++) { pd[i] += pd_last[i]; @@ -839,13 +848,13 @@ static int decode_frame(AVCodecContext *avctx, } } } + ff_thread_report_progress(&s->picture, INT_MAX, 0); av_frame_set_metadata(p, metadata); metadata = NULL; - av_frame_unref(s->prev); - if ((ret = av_frame_ref(s->prev, p)) < 0) - goto fail; + if ((ret = av_frame_ref(data, s->picture.f)) < 0) + return ret; *got_frame = 1; @@ -860,20 +869,39 @@ static int decode_frame(AVCodecContext *avctx, fail: av_dict_free(&metadata); ret = AVERROR_INVALIDDATA; + ff_thread_release_buffer(avctx, &s->picture); goto the_end; } +static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src) +{ + PNGDecContext *psrc = src->priv_data; + PNGDecContext *pdst = dst->priv_data; + + if (dst == src) + return 0; + + ff_thread_release_buffer(dst, &pdst->picture); + if (psrc->picture.f->data[0]) + return ff_thread_ref_frame(&pdst->picture, &psrc->picture); + + return 0; +} + static av_cold int png_dec_init(AVCodecContext *avctx) { PNGDecContext *s = avctx->priv_data; - s->prev = av_frame_alloc(); - if (!s->prev) + s->avctx = avctx; + s->last_picture.f = av_frame_alloc(); + s->picture.f = av_frame_alloc(); + if (!s->last_picture.f || !s->picture.f) return AVERROR(ENOMEM); - ff_pngdsp_init(&s->dsp); - - s->avctx = avctx; + if (!avctx->internal->is_copy) { + avctx->internal->allocate_progress = 1; + ff_pngdsp_init(&s->dsp); + } return 0; } @@ -882,7 +910,10 @@ static av_cold int png_dec_end(AVCodecContext *avctx) { PNGDecContext *s = avctx->priv_data; - av_frame_free(&s->prev); + ff_thread_release_buffer(avctx, &s->last_picture); + av_frame_free(&s->last_picture.f); + ff_thread_release_buffer(avctx, &s->picture); + av_frame_free(&s->picture.f); return 0; } @@ -895,6 +926,8 @@ AVCodec ff_png_decoder = { .init = png_dec_init, .close = png_dec_end, .decode = decode_frame, - .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/, + .init_thread_copy = ONLY_IF_THREADS_ENABLED(png_dec_init), + .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context), + .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS /*| CODEC_CAP_DRAW_HORIZ_BAND*/, .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"), }; -- 1.7.11.2
participants (2)
-
Michael Niedermayer -
Paul B Mahol