From 3f6e7153361b09e12cd6e97ce5a9175048653296 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Tue, 11 May 2021 21:47:53 +0200 Subject: [PATCH] avcodec/frame_thread_encoder: Avoid dictionaries avcodec_open2() allows to provide options via an AVDictionary; but it is also allowed to set options by simply setting the value of the AVCodecContext or via the AVOptions API if the codec has a private class. Any options provided via an AVDictionary have already been applied before ff_frame_thread_init(), so in order to copy all the options from the main AVCodecContext and its private context, it is enough to av_opt_copy() these options. The current code does this, but it does more: It also copies the user-provided AVDictionary and uses it for the initialization of each of the worker-AVCodecContexts. This is completely unnecessary, because said options have already been copied from the main context. Furthermore, these options were also examined to decide if frame threading should be used for huffman encoding in case this would incur nondeterminism. This is wrong, because options not set via an AVDictionary are ignored. Instead inspect the values stored in the contexts directly. (In order to maintain the current behaviour, the default value of the "non_deterministic" option has been changed to false, because the absence of an entry with said key in the AVDictionary had the consequence of disallowing nondeterminism.) Finally, the AVDictionary has been removed from the signature of ff_frame_thread_encoder_init(). Signed-off-by: Andreas Rheinhardt --- libavcodec/avcodec.c | 2 +- libavcodec/frame_thread_encoder.c | 25 ++++++++----------------- libavcodec/frame_thread_encoder.h | 2 +- libavcodec/huffyuvenc.c | 2 +- 4 files changed, 11 insertions(+), 20 deletions(-) diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c index 882700f357..6f61ae246d 100644 --- a/libavcodec/avcodec.c +++ b/libavcodec/avcodec.c @@ -303,7 +303,7 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) { unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem - ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL); + ret = ff_frame_thread_encoder_init(avctx); lock_avcodec(codec); if (ret < 0) goto free_and_end; diff --git a/libavcodec/frame_thread_encoder.c b/libavcodec/frame_thread_encoder.c index 778317d60b..5d14442c01 100644 --- a/libavcodec/frame_thread_encoder.c +++ b/libavcodec/frame_thread_encoder.c @@ -121,7 +121,8 @@ end: return NULL; } -int ff_frame_thread_encoder_init(AVCodecContext *avctx, AVDictionary *options){ +int ff_frame_thread_encoder_init(AVCodecContext *avctx) +{ int i=0; ThreadContext *c; @@ -148,18 +149,14 @@ int ff_frame_thread_encoder_init(AVCodecContext *avctx, AVDictionary *options){ if (avctx->codec_id == AV_CODEC_ID_HUFFYUV || avctx->codec_id == AV_CODEC_ID_FFVHUFF) { int warn = 0; - int context_model = 0; - AVDictionaryEntry *con = av_dict_get(options, "context", NULL, AV_DICT_MATCH_CASE); - - if (con && con->value) - context_model = atoi(con->value); + int64_t tmp; if (avctx->flags & AV_CODEC_FLAG_PASS1) warn = 1; - else if(context_model > 0) { - AVDictionaryEntry *t = av_dict_get(options, "non_deterministic", - NULL, AV_DICT_MATCH_CASE); - warn = !t || !t->value || !atoi(t->value) ? 1 : 0; + else if (av_opt_get_int(avctx->priv_data, "context", 0, &tmp) >= 0 && + tmp > 0) { + warn = av_opt_get_int(avctx->priv_data, "non_deterministic", 0, &tmp) < 0 + || !tmp; } // huffyuv does not support these with multiple frame threads currently if (warn) { @@ -202,7 +199,6 @@ int ff_frame_thread_encoder_init(AVCodecContext *avctx, AVDictionary *options){ } for(i=0; ithread_count ; i++){ - AVDictionary *tmp = NULL; int ret; void *tmpv; AVCodecContext *thread_avctx = avcodec_alloc_context3(avctx->codec); @@ -225,13 +221,8 @@ int ff_frame_thread_encoder_init(AVCodecContext *avctx, AVDictionary *options){ thread_avctx->thread_count = 1; thread_avctx->active_thread_type &= ~FF_THREAD_FRAME; - av_dict_copy(&tmp, options, 0); - av_dict_set(&tmp, "threads", "1", 0); - if(avcodec_open2(thread_avctx, avctx->codec, &tmp) < 0) { - av_dict_free(&tmp); + if (avcodec_open2(thread_avctx, avctx->codec, NULL) < 0) goto fail; - } - av_dict_free(&tmp); av_assert0(!thread_avctx->internal->frame_thread_encoder); thread_avctx->internal->frame_thread_encoder = c; if(pthread_create(&c->worker[i], NULL, worker, thread_avctx)) { diff --git a/libavcodec/frame_thread_encoder.h b/libavcodec/frame_thread_encoder.h index c400d6b32c..2cdc40a830 100644 --- a/libavcodec/frame_thread_encoder.h +++ b/libavcodec/frame_thread_encoder.h @@ -23,7 +23,7 @@ #include "avcodec.h" -int ff_frame_thread_encoder_init(AVCodecContext *avctx, AVDictionary *options); +int ff_frame_thread_encoder_init(AVCodecContext *avctx); void ff_frame_thread_encoder_free(AVCodecContext *avctx); int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, AVFrame *frame, int *got_packet_ptr); diff --git a/libavcodec/huffyuvenc.c b/libavcodec/huffyuvenc.c index b35f5c3342..266984c026 100644 --- a/libavcodec/huffyuvenc.c +++ b/libavcodec/huffyuvenc.c @@ -1016,7 +1016,7 @@ static av_cold int encode_end(AVCodecContext *avctx) #define COMMON_OPTIONS \ { "non_deterministic", "Allow multithreading for e.g. context=1 at the expense of determinism", \ - OFFSET(non_determ), AV_OPT_TYPE_BOOL, { .i64 = 1 }, \ + OFFSET(non_determ), AV_OPT_TYPE_BOOL, { .i64 = 0 }, \ 0, 1, VE }, \ { "pred", "Prediction method", OFFSET(predictor), AV_OPT_TYPE_INT, { .i64 = LEFT }, LEFT, MEDIAN, VE, "pred" }, \ { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LEFT }, INT_MIN, INT_MAX, VE, "pred" }, \