Commit e923a359 authored by Anton Khirnov's avatar Anton Khirnov

pthread_frame: make sure ff_thread_release_buffer always cleans the frame

parent 917d28d5
...@@ -982,10 +982,11 @@ void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f) ...@@ -982,10 +982,11 @@ void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
PerThreadContext *p = avctx->internal->thread_ctx; PerThreadContext *p = avctx->internal->thread_ctx;
FrameThreadContext *fctx; FrameThreadContext *fctx;
AVFrame *dst, *tmp; AVFrame *dst, *tmp;
int ret = 0;
int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) || int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
THREAD_SAFE_CALLBACKS(avctx); THREAD_SAFE_CALLBACKS(avctx);
if (!f->f || !f->f->buf[0]) if (!f->f)
return; return;
if (avctx->debug & FF_DEBUG_BUFFERS) if (avctx->debug & FF_DEBUG_BUFFERS)
...@@ -994,7 +995,8 @@ void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f) ...@@ -994,7 +995,8 @@ void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
av_buffer_unref(&f->progress); av_buffer_unref(&f->progress);
f->owner[0] = f->owner[1] = NULL; f->owner[0] = f->owner[1] = NULL;
if (can_direct_free) { // when the frame buffers are not allocated, just reset it to clean state
if (can_direct_free || !f->f->buf[0]) {
av_frame_unref(f->f); av_frame_unref(f->f);
return; return;
} }
...@@ -1002,13 +1004,17 @@ void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f) ...@@ -1002,13 +1004,17 @@ void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
fctx = p->parent; fctx = p->parent;
pthread_mutex_lock(&fctx->buffer_mutex); pthread_mutex_lock(&fctx->buffer_mutex);
if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers)) if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers)) {
ret = AVERROR(ENOMEM);
goto fail; goto fail;
}
tmp = av_fast_realloc(p->released_buffers, &p->released_buffers_allocated, tmp = av_fast_realloc(p->released_buffers, &p->released_buffers_allocated,
(p->num_released_buffers + 1) * (p->num_released_buffers + 1) *
sizeof(*p->released_buffers)); sizeof(*p->released_buffers));
if (!tmp) if (!tmp) {
ret = AVERROR(ENOMEM);
goto fail; goto fail;
}
p->released_buffers = tmp; p->released_buffers = tmp;
dst = &p->released_buffers[p->num_released_buffers]; dst = &p->released_buffers[p->num_released_buffers];
...@@ -1018,4 +1024,14 @@ void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f) ...@@ -1018,4 +1024,14 @@ void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
fail: fail:
pthread_mutex_unlock(&fctx->buffer_mutex); pthread_mutex_unlock(&fctx->buffer_mutex);
// make sure the frame is clean even if we fail to free it
// this leaks, but it is better than crashing
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Could not queue a frame for freeing, this will leak\n");
memset(f->f->buf, 0, sizeof(f->f->buf));
if (f->f->extended_buf)
memset(f->f->extended_buf, 0, f->f->nb_extended_buf * sizeof(*f->f->extended_buf));
av_frame_unref(f->f);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment