From: Anton Khirnov <anton@khirnov.net> To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH v4 08/12] lavc/frame_thread_encoder: avoid assigning a whole AVCodecContext Date: Wed, 27 Mar 2024 11:27:02 +0100 Message-ID: <20240327102712.20569-1-anton@khirnov.net> (raw) In-Reply-To: <20240322202841.31730-8-anton@khirnov.net> 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. --- libavcodec/frame_thread_encoder.c | 52 +++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/libavcodec/frame_thread_encoder.c b/libavcodec/frame_thread_encoder.c index cda5158117..17712e68ee 100644 --- a/libavcodec/frame_thread_encoder.c +++ b/libavcodec/frame_thread_encoder.c @@ -28,6 +28,7 @@ #include "libavutil/thread.h" #include "avcodec.h" #include "avcodec_internal.h" +#include "codec_par.h" #include "encode.h" #include "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); } end: - ff_codec_close(avctx); - av_freep(&avctx); + avcodec_free_context(&avctx); return NULL; } @@ -121,6 +121,7 @@ av_cold int ff_frame_thread_encoder_init(AVCodecContext *avctx) int i=0; ThreadContext *c; AVCodecContext *thread_avctx = NULL; + AVCodecParameters *par = NULL; int ret; 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++){ - void *tmpv; thread_avctx = avcodec_alloc_context3(avctx->codec); if (!thread_avctx) { ret = AVERROR(ENOMEM); goto fail; } - tmpv = thread_avctx->priv_data; - *thread_avctx = *avctx; - thread_avctx->priv_data = tmpv; - thread_avctx->internal = NULL; - thread_avctx->hw_frames_ctx = NULL; + + ret = avcodec_parameters_to_context(thread_avctx, par); + if (ret < 0) + goto fail; + ret = av_opt_copy(thread_avctx, avctx); if (ret < 0) goto fail; @@ -217,6 +227,26 @@ av_cold int ff_frame_thread_encoder_init(AVCodecContext *avctx) thread_avctx->thread_count = 1; 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) goto fail; 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; return 0; fail: - ff_codec_close(thread_avctx); - av_freep(&thread_avctx); + avcodec_parameters_free(&par); + avcodec_free_context(&thread_avctx); avctx->thread_count = i; av_log(avctx, AV_LOG_ERROR, "ff_frame_thread_encoder_init failed\n"); ff_frame_thread_encoder_free(avctx); -- 2.43.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2024-03-27 10:27 UTC|newest] Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top 2024-03-22 20:28 [FFmpeg-devel] [PATCH 01/12] tests/fate/ffmpeg: evaluate thread count in fate-run.sh rather than make Anton Khirnov 2024-03-22 20:28 ` [FFmpeg-devel] [PATCH 02/12] fftools/ffmpeg_demux: only call filter_codec_opts() when we have a decoder Anton Khirnov 2024-03-22 20:28 ` [FFmpeg-devel] [PATCH 03/12] fftools/cmdutils: do not use a random codec's private options Anton Khirnov 2024-03-22 20:28 ` [FFmpeg-devel] [PATCH 04/12] fftools/ffmpeg_dec: apply decoder options manually Anton Khirnov 2024-03-22 20:28 ` [FFmpeg-devel] [PATCH 05/12] fftools/ffmpeg_{demux, dec}: pass -bitexact through DecoderFlags Anton Khirnov 2024-03-22 20:28 ` [FFmpeg-devel] [PATCH 06/12] fftools/ffmpeg_dec: apply cropping manually Anton Khirnov 2024-03-23 2:53 ` James Almer 2024-03-23 7:20 ` Anton Khirnov 2024-03-22 20:28 ` [FFmpeg-devel] [PATCH 07/12] fftools/ffmpeg_filter: remove display matrix if we have applied it Anton Khirnov 2024-03-22 20:28 ` [FFmpeg-devel] [PATCH 08/12] lavc/frame_thread_encoder: avoid assigning a whole AVCodecContext Anton Khirnov 2024-03-22 20:42 ` Andreas Rheinhardt 2024-03-23 14:00 ` [FFmpeg-devel] [PATCH v2 " Anton Khirnov 2024-03-23 14:11 ` Andreas Rheinhardt 2024-03-24 9:27 ` Anton Khirnov 2024-03-24 10:19 ` Andreas Rheinhardt 2024-03-24 10:33 ` Anton Khirnov 2024-03-24 11:10 ` Andreas Rheinhardt 2024-03-24 11:23 ` Anton Khirnov 2024-03-24 0:11 ` [FFmpeg-devel] [PATCH " Michael Niedermayer 2024-03-24 9:29 ` [FFmpeg-devel] [PATCH v3 " Anton Khirnov 2024-03-24 11:25 ` Andreas Rheinhardt 2024-03-24 11:36 ` Anton Khirnov 2024-03-27 10:27 ` Anton Khirnov [this message] 2024-03-22 20:28 ` [FFmpeg-devel] [PATCH 09/12] lavc/decode: move sd_global_map to avcodec Anton Khirnov 2024-03-22 20:28 ` [FFmpeg-devel] [PATCH 10/12] lavc/encode: map AVCodecContext.decoded_side_data to coded_side_data Anton Khirnov 2024-03-23 2:33 ` James Almer 2024-03-23 7:20 ` Anton Khirnov 2024-03-22 20:28 ` [FFmpeg-devel] [PATCH 11/12] fftools/ffmpeg_enc: stop copying demuxer side data to the muxer Anton Khirnov 2024-03-22 20:28 ` [FFmpeg-devel] [PATCH 12/12] fftools/ffmpeg_demux: make InputStream.autorotate private Anton Khirnov
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20240327102712.20569-1-anton@khirnov.net \ --to=anton@khirnov.net \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git