1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

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 <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2021-05-11 21:47:53 +02:00
parent 56e9e0273a
commit 3f6e715336
4 changed files with 11 additions and 20 deletions

View File

@ -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;

View File

@ -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; i<avctx->thread_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)) {

View File

@ -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);

View File

@ -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" }, \