From: Andreas Rheinhardt <ffmpegagent-at-gmail.com@ffmpeg.org> To: ffmpeg-devel@ffmpeg.org Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> Subject: [FFmpeg-devel] [PATCH 26/47] avcodec/mpegvideo_enc: Simplify allocating non-slice buffers Date: Sun, 15 Jun 2025 16:54:17 +0000 Message-ID: <28a872c5c2ee09d81d3115e9d4d54b16daa49803.1750006479.git.ffmpegagent@gmail.com> (raw) In-Reply-To: <pull.98.ffstaging.FFmpeg.1750006478.ffmpegagent@gmail.com> From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> Allocate them before the slice contexts, so that they are automatically copied to the slice contexts. This avoids having to set them in a loop. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> --- libavcodec/mpegvideo_enc.c | 148 ++++++++++++++++++------------------- 1 file changed, 73 insertions(+), 75 deletions(-) diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c index 1fae5fbeb0..60abe08d7a 100644 --- a/libavcodec/mpegvideo_enc.c +++ b/libavcodec/mpegvideo_enc.c @@ -444,50 +444,33 @@ static av_cold int init_matrices(MPVMainEncContext *const m, AVCodecContext *avc return 0; } -static av_cold int init_buffers(MPVMainEncContext *const m, AVCodecContext *avctx) +static av_cold int init_buffers(MPVMainEncContext *const m) { MPVEncContext *const s = &m->s; - // Align the following per-thread buffers to avoid false sharing. - enum { -#ifndef _MSC_VER - /// The number is supposed to match/exceed the cache-line size. - ALIGN = FFMAX(128, _Alignof(max_align_t)), -#else - ALIGN = 128, -#endif - DCT_ERROR_SIZE = FFALIGN(2 * sizeof(*s->dct_error_sum), ALIGN), - }; - static_assert(DCT_ERROR_SIZE * MAX_THREADS + ALIGN - 1 <= SIZE_MAX, - "Need checks for potential overflow."); - unsigned nb_slices = s->c.slice_context_count, mv_table_size, mb_array_size; - char *dct_error = NULL; - int has_b_frames = !!m->max_b_frames, nb_mv_tables = 1 + 5 * has_b_frames; + int has_b_frames = !!m->max_b_frames; int16_t (*mv_table)[2]; - if (m->noise_reduction) { - if (!FF_ALLOCZ_TYPED_ARRAY(s->dct_offset, 2)) - return AVERROR(ENOMEM); - dct_error = av_mallocz(ALIGN - 1 + nb_slices * DCT_ERROR_SIZE); - if (!dct_error) - return AVERROR(ENOMEM); - m->dct_error_sum_base = dct_error; - dct_error += FFALIGN((uintptr_t)dct_error, ALIGN) - (uintptr_t)dct_error; - } - /* Allocate MB type table */ - mb_array_size = s->c.mb_stride * s->c.mb_height; + unsigned mb_array_size = s->c.mb_stride * s->c.mb_height; s->mb_type = av_calloc(mb_array_size, 3 * sizeof(*s->mb_type) + sizeof(*s->mb_mean)); if (!s->mb_type) return AVERROR(ENOMEM); + s->mc_mb_var = s->mb_type + mb_array_size; + s->mb_var = s->mc_mb_var + mb_array_size; + s->mb_mean = (uint8_t*)(s->mb_var + mb_array_size); + if (!FF_ALLOCZ_TYPED_ARRAY(s->lambda_table, mb_array_size)) return AVERROR(ENOMEM); - mv_table_size = (s->c.mb_height + 2) * s->c.mb_stride + 1; + unsigned mv_table_size = (s->c.mb_height + 2) * s->c.mb_stride + 1; + unsigned nb_mv_tables = 1 + 5 * has_b_frames; if (s->c.codec_id == AV_CODEC_ID_MPEG4 || (s->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) { nb_mv_tables += 8 * has_b_frames; - if (!ALLOCZ_ARRAYS(s->p_field_select_table[0], 2 * (2 + 4 * has_b_frames), mv_table_size)) + s->p_field_select_table[0] = av_calloc(mv_table_size, 2 * (2 + 4 * has_b_frames)); + if (!s->p_field_select_table[0]) return AVERROR(ENOMEM); + s->p_field_select_table[1] = s->p_field_select_table[0] + 2 * mv_table_size; } mv_table = av_calloc(mv_table_size, nb_mv_tables * sizeof(*mv_table)); @@ -496,43 +479,21 @@ static av_cold int init_buffers(MPVMainEncContext *const m, AVCodecContext *avct m->mv_table_base = mv_table; mv_table += s->c.mb_stride + 1; - for (unsigned i = 0; i < nb_slices; ++i) { - MPVEncContext *const s2 = s->c.enc_contexts[i]; - int16_t (*tmp_mv_table)[2] = mv_table; - - if (dct_error) { - s2->dct_offset = s->dct_offset; - s2->dct_error_sum = (void*)dct_error; - dct_error += DCT_ERROR_SIZE; - } - - s2->mb_type = s->mb_type; - s2->mc_mb_var = s2->mb_type + mb_array_size; - s2->mb_var = s2->mc_mb_var + mb_array_size; - s2->mb_mean = (uint8_t*)(s2->mb_var + mb_array_size); - s2->lambda_table = s->lambda_table; - - s2->p_mv_table = tmp_mv_table; - if (has_b_frames) { - s2->b_forw_mv_table = tmp_mv_table += mv_table_size; - s2->b_back_mv_table = tmp_mv_table += mv_table_size; - s2->b_bidir_forw_mv_table = tmp_mv_table += mv_table_size; - s2->b_bidir_back_mv_table = tmp_mv_table += mv_table_size; - s2->b_direct_mv_table = tmp_mv_table += mv_table_size; - } - - if (s->p_field_select_table[0]) { // MPEG-4 or INTERLACED_ME above - uint8_t *field_select = s->p_field_select_table[0]; - s2->p_field_select_table[0] = field_select; - s2->p_field_select_table[1] = field_select += 2 * mv_table_size; - - if (has_b_frames) { - for (int j = 0; j < 2; j++) { - for (int k = 0; k < 2; k++) { - for (int l = 0; l < 2; l++) - s2->b_field_mv_table[j][k][l] = tmp_mv_table += mv_table_size; - s2->b_field_select_table[j][k] = field_select += 2 * mv_table_size; - } + s->p_mv_table = mv_table; + if (has_b_frames) { + s->b_forw_mv_table = mv_table += mv_table_size; + s->b_back_mv_table = mv_table += mv_table_size; + s->b_bidir_forw_mv_table = mv_table += mv_table_size; + s->b_bidir_back_mv_table = mv_table += mv_table_size; + s->b_direct_mv_table = mv_table += mv_table_size; + + if (s->p_field_select_table[1]) { // MPEG-4 or INTERLACED_ME above + uint8_t *field_select = s->p_field_select_table[1]; + for (int j = 0; j < 2; j++) { + for (int k = 0; k < 2; k++) { + for (int l = 0; l < 2; l++) + s->b_field_mv_table[j][k][l] = mv_table += mv_table_size; + s->b_field_select_table[j][k] = field_select += 2 * mv_table_size; } } } @@ -541,6 +502,44 @@ static av_cold int init_buffers(MPVMainEncContext *const m, AVCodecContext *avct return 0; } +static av_cold int init_slice_buffers(MPVMainEncContext *const m) +{ + MPVEncContext *const s = &m->s; + // Align the following per-thread buffers to avoid false sharing. + enum { +#ifndef _MSC_VER + /// The number is supposed to match/exceed the cache-line size. + ALIGN = FFMAX(128, _Alignof(max_align_t)), +#else + ALIGN = 128, +#endif + DCT_ERROR_SIZE = FFALIGN(2 * sizeof(*s->dct_error_sum), ALIGN), + }; + static_assert(DCT_ERROR_SIZE * MAX_THREADS + ALIGN - 1 <= SIZE_MAX, + "Need checks for potential overflow."); + unsigned nb_slices = s->c.slice_context_count; + + if (!m->noise_reduction) + return 0; + + if (!FF_ALLOCZ_TYPED_ARRAY(s->dct_offset, 2)) + return AVERROR(ENOMEM); + char *dct_error = av_mallocz(ALIGN - 1 + nb_slices * DCT_ERROR_SIZE); + if (!dct_error) + return AVERROR(ENOMEM); + m->dct_error_sum_base = dct_error; + dct_error += FFALIGN((uintptr_t)dct_error, ALIGN) - (uintptr_t)dct_error; + + for (unsigned i = 0; i < nb_slices; ++i) { + MPVEncContext *const s2 = s->c.enc_contexts[i]; + + s2->dct_offset = s->dct_offset; + s2->dct_error_sum = (void*)dct_error; + dct_error += DCT_ERROR_SIZE; + } + return 0; +} + /* init video encoder */ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) { @@ -1059,20 +1058,19 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) ret = ff_mpv_common_init(&s->c); if (ret < 0) return ret; - ret = ff_mpv_init_duplicate_contexts(&s->c); + ret = init_buffers(m); if (ret < 0) return ret; - if (s->c.slice_context_count > 1) { - for (int i = 0; i < s->c.slice_context_count; ++i) { - s->c.enc_contexts[i]->rtp_mode = 1; - - if (avctx->codec_id == AV_CODEC_ID_H263P) - s->c.enc_contexts[i]->c.h263_slice_structured = 1; - } + s->rtp_mode = 1; + if (avctx->codec_id == AV_CODEC_ID_H263P) + s->c.h263_slice_structured = 1; } + ret = ff_mpv_init_duplicate_contexts(&s->c); + if (ret < 0) + return ret; - ret = init_buffers(m, avctx); + ret = init_slice_buffers(m); if (ret < 0) return ret; -- ffmpeg-codebot _______________________________________________ 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:[~2025-06-15 17:00 UTC|newest] Thread overview: 48+ messages / expand[flat|nested] mbox.gz Atom feed top 2025-06-15 16:53 [FFmpeg-devel] [PATCH 00/47] avcodec/mpeg4videodec: Don't zero blocks twice ffmpegagent 2025-06-15 16:53 ` [FFmpeg-devel] [PATCH 01/47] " Andreas Rheinhardt 2025-06-15 16:53 ` [FFmpeg-devel] [PATCH 02/47] avcodec/rv10: Perform RV20 initialization during init Andreas Rheinhardt 2025-06-15 16:53 ` [FFmpeg-devel] [PATCH 03/47] avcodec/rv10: Perform RV20 check only for RV20 Andreas Rheinhardt 2025-06-15 16:53 ` [FFmpeg-devel] [PATCH 04/47] avcodec/ituh263dec: Don't process unnecessarily many coefficients Andreas Rheinhardt 2025-06-15 16:53 ` [FFmpeg-devel] [PATCH 05/47] avcodec/ituh263dec: Remove redundant store Andreas Rheinhardt 2025-06-15 16:53 ` [FFmpeg-devel] [PATCH 06/47] avcodec/ituh263enc: Don't use array unnecessarily Andreas Rheinhardt 2025-06-15 16:53 ` [FFmpeg-devel] [PATCH 07/47] avcodec/ituh263enc: Fix indentation Andreas Rheinhardt 2025-06-15 16:53 ` [FFmpeg-devel] [PATCH 08/47] avcodec/mpeg12dec: Don't store block_last_index unnecessarily Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 09/47] avcodec/rv10: Avoid indirection when reading VLC codes Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 10/47] avcodec/mpeg_er: Mark ff_mpeg_er_init() as av_cold Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 11/47] avcodec/mpegvideo_enc: Remove always-true branch Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 12/47] avcodec/motion_est: Fix indentation Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 13/47] avcodec/svq1enc: Don't free scratchbuf upon error Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 14/47] avcodec/svq1enc: Allocate motion_val{8, 16} during init Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 15/47] avcodec/svq1enc: Stop calling ff_mpv_common_init() Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 16/47] avcodec/svq1enc: Remove write-only c_block_{width, height} Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 17/47] avcodec/svq1enc: Set MpegEncContext.avctx only once Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 18/47] avcodec/svq1enc: Don't initialize unneeded block_index Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 19/47] configure: Factor mpegvideoencdsp out of mpegvideoenc Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 20/47] configure: Relax svq1enc->mpegvideoenc dependency Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 21/47] configure: Relax diracdec->mpegvideoenc dependency Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 22/47] configure: Relax snowenc->mpegvideoenc dependency Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 23/47] avcodec/mpegvideo: Don't reset thread_context ptrs unnecessarily Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 24/47] avcodec/mpegvideo: Don't zero unnecessarily Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 25/47] avcodec/mpegvideo: Defer init of enc slice ctxs in ff_mpv_common_init() Andreas Rheinhardt 2025-06-15 16:54 ` Andreas Rheinhardt [this message] 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 27/47] avcodec/ituh263dec: Simplify AIC handling Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 28/47] avcodec/ituh263enc: " Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 29/47] avcodec/flvenc: Combine writing bits Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 30/47] avcodec/flvenc: Move ff_flv2_encode_ac_esc() to ituh263enc.c Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 31/47] avcodec/mpegvideo: Redo resetting intra table entry Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 32/47] avcodec/mpegvideo: Avoid {a, d}c_val[{1, 2}] Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 33/47] avcodec/mpegvideo: Provide alignment hint to compiler Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 34/47] avcodec/mpegvideo: Don't reset AC values of upper-left luma block Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 35/47] avcodec/mpegvideo: Combine stores Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 36/47] avcodec/mpeg4video: Don't use ac_val[1], ac_val[2] when cleaning buffers Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 37/47] avcodec/mpegvideo: Only use a single ac_val pointer Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 38/47] avcodec/mpegvideo: Allocate ac_val jointly Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 39/47] avcodec/mpegvideo: Zero-init mbintra_table Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 40/47] avcodec/mpegvideo: Only keep MpegEncContext.dc_val[0] Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 41/47] avcodec/vc1: Only keep mb_type[0] Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 42/47] avcodec/mpegvideo: Allocate dc_val for each encoder slice Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 43/47] avcodec/{ituh263, mpeg4video}enc: Simplify out-of-slice DC prediction Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 44/47] avcodec/mpeg4video: Move ff_mpeg4_pred_dc() to decoder Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 45/47] avcodec/ituh263enc: Simplify encoding umotion vectors Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 46/47] avcodec/mpegvideo: Reduce stack usage when copying MpegEncContext Andreas Rheinhardt 2025-06-15 16:54 ` [FFmpeg-devel] [PATCH 47/47] avcodec/mpeg4videoenc: Fix data race when using AC prediction Andreas Rheinhardt
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=28a872c5c2ee09d81d3115e9d4d54b16daa49803.1750006479.git.ffmpegagent@gmail.com \ --to=ffmpegagent-at-gmail.com@ffmpeg.org \ --cc=andreas.rheinhardt@outlook.com \ --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