mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2025-01-08 13:22:53 +02:00
lavc/frame_thread_encoder: avoid assigning a whole AVCodecContext
It is highly unsafe, as AVCodecContext contains many allocated fields. Almost everything needed by worker threads should be covered by routing through AVCodecParameters and av_opt_copy(), except for a few fields that are copied manually. avcodec_free_context() can now be used for per-thread contexts.
This commit is contained in:
parent
198a7788e7
commit
e1f384adbf
@ -28,6 +28,7 @@
|
|||||||
#include "libavutil/thread.h"
|
#include "libavutil/thread.h"
|
||||||
#include "avcodec.h"
|
#include "avcodec.h"
|
||||||
#include "avcodec_internal.h"
|
#include "avcodec_internal.h"
|
||||||
|
#include "codec_par.h"
|
||||||
#include "encode.h"
|
#include "encode.h"
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
#include "pthread_internal.h"
|
#include "pthread_internal.h"
|
||||||
@ -111,8 +112,7 @@ static void * attribute_align_arg worker(void *v){
|
|||||||
pthread_mutex_unlock(&c->finished_task_mutex);
|
pthread_mutex_unlock(&c->finished_task_mutex);
|
||||||
}
|
}
|
||||||
end:
|
end:
|
||||||
ff_codec_close(avctx);
|
avcodec_free_context(&avctx);
|
||||||
av_freep(&avctx);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,6 +121,7 @@ av_cold int ff_frame_thread_encoder_init(AVCodecContext *avctx)
|
|||||||
int i=0;
|
int i=0;
|
||||||
ThreadContext *c;
|
ThreadContext *c;
|
||||||
AVCodecContext *thread_avctx = NULL;
|
AVCodecContext *thread_avctx = NULL;
|
||||||
|
AVCodecParameters *par = NULL;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if( !(avctx->thread_type & FF_THREAD_FRAME)
|
if( !(avctx->thread_type & FF_THREAD_FRAME)
|
||||||
@ -194,18 +195,27 @@ av_cold int ff_frame_thread_encoder_init(AVCodecContext *avctx)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
par = avcodec_parameters_alloc();
|
||||||
|
if (!par) {
|
||||||
|
ret = AVERROR(ENOMEM);
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = avcodec_parameters_from_context(par, avctx);
|
||||||
|
if (ret < 0)
|
||||||
|
goto fail;
|
||||||
|
|
||||||
for(i=0; i<avctx->thread_count ; i++){
|
for(i=0; i<avctx->thread_count ; i++){
|
||||||
void *tmpv;
|
|
||||||
thread_avctx = avcodec_alloc_context3(avctx->codec);
|
thread_avctx = avcodec_alloc_context3(avctx->codec);
|
||||||
if (!thread_avctx) {
|
if (!thread_avctx) {
|
||||||
ret = AVERROR(ENOMEM);
|
ret = AVERROR(ENOMEM);
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
tmpv = thread_avctx->priv_data;
|
|
||||||
*thread_avctx = *avctx;
|
ret = avcodec_parameters_to_context(thread_avctx, par);
|
||||||
thread_avctx->priv_data = tmpv;
|
if (ret < 0)
|
||||||
thread_avctx->internal = NULL;
|
goto fail;
|
||||||
thread_avctx->hw_frames_ctx = NULL;
|
|
||||||
ret = av_opt_copy(thread_avctx, avctx);
|
ret = av_opt_copy(thread_avctx, avctx);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
@ -217,6 +227,26 @@ av_cold int ff_frame_thread_encoder_init(AVCodecContext *avctx)
|
|||||||
thread_avctx->thread_count = 1;
|
thread_avctx->thread_count = 1;
|
||||||
thread_avctx->active_thread_type &= ~FF_THREAD_FRAME;
|
thread_avctx->active_thread_type &= ~FF_THREAD_FRAME;
|
||||||
|
|
||||||
|
#define DUP_MATRIX(m) \
|
||||||
|
if (avctx->m) { \
|
||||||
|
thread_avctx->m = av_memdup(avctx->m, 64 * sizeof(*avctx->m)); \
|
||||||
|
if (!thread_avctx->m) { \
|
||||||
|
ret = AVERROR(ENOMEM); \
|
||||||
|
goto fail; \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
DUP_MATRIX(intra_matrix);
|
||||||
|
DUP_MATRIX(chroma_intra_matrix);
|
||||||
|
DUP_MATRIX(inter_matrix);
|
||||||
|
|
||||||
|
#undef DUP_MATRIX
|
||||||
|
|
||||||
|
thread_avctx->opaque = avctx->opaque;
|
||||||
|
thread_avctx->get_encode_buffer = avctx->get_encode_buffer;
|
||||||
|
thread_avctx->execute = avctx->execute;
|
||||||
|
thread_avctx->execute2 = avctx->execute2;
|
||||||
|
thread_avctx->stats_in = avctx->stats_in;
|
||||||
|
|
||||||
if ((ret = avcodec_open2(thread_avctx, avctx->codec, NULL)) < 0)
|
if ((ret = avcodec_open2(thread_avctx, avctx->codec, NULL)) < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
av_assert0(!thread_avctx->internal->frame_thread_encoder);
|
av_assert0(!thread_avctx->internal->frame_thread_encoder);
|
||||||
@ -227,12 +257,14 @@ av_cold int ff_frame_thread_encoder_init(AVCodecContext *avctx)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
avcodec_parameters_free(&par);
|
||||||
|
|
||||||
avctx->active_thread_type = FF_THREAD_FRAME;
|
avctx->active_thread_type = FF_THREAD_FRAME;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
fail:
|
fail:
|
||||||
ff_codec_close(thread_avctx);
|
avcodec_parameters_free(&par);
|
||||||
av_freep(&thread_avctx);
|
avcodec_free_context(&thread_avctx);
|
||||||
avctx->thread_count = i;
|
avctx->thread_count = i;
|
||||||
av_log(avctx, AV_LOG_ERROR, "ff_frame_thread_encoder_init failed\n");
|
av_log(avctx, AV_LOG_ERROR, "ff_frame_thread_encoder_init failed\n");
|
||||||
ff_frame_thread_encoder_free(avctx);
|
ff_frame_thread_encoder_free(avctx);
|
||||||
|
Loading…
Reference in New Issue
Block a user