History

avcodec/mpegvideo: Move allocating encoder buffers to mpegvideoenc.c

dct_error_sum and me.map are allocated per slice-context and therefore their allocation has not been moved to mpegvideoenc.c in 0154fb43e328b13da8943e66b38dc1b5ab9315af. This commit changes this by allocating them jointly and moving said allocations to mpegvideo_enc.c like the other encoder-only buffers. The buffers are suitably aligned to ensure that no false sharing occurs.

Andreas Rheinhardt committed 4 years ago

Commit a5a2fc01 parent 99642123 files · +57 −17Browse files

Unified · Split

3 files
  1. libavcodec/mpegvideo.c +0 −17
  2. libavcodec/mpegvideo_enc.c +54 −0
  3. libavcodec/mpegvideoenc.h +3 −0
libavcodec/mpegvideo.c+0−17
@@ -351,17 +351,6 @@ av_cold void ff_mpv_idct_init(MpegEncContext *s) · 350 unmodified lines
351351
352352 static av_cold int init_duplicate_context(MpegEncContext *s)
353353 {
354− if (s->encoding) {
355− s->me.map = av_mallocz(2 * ME_MAP_SIZE * sizeof(*s->me.map));
356− if (!s->me.map)
357− return AVERROR(ENOMEM);
358− s->me.score_map = s->me.map + ME_MAP_SIZE;
359−
360− if (s->noise_reduction) {
361− if (!FF_ALLOCZ_TYPED_ARRAY(s->dct_error_sum, 2))
362− return AVERROR(ENOMEM);
363− }
364− }
365354 if (!FF_ALLOCZ_TYPED_ARRAY(s->blocks, 1 + s->encoding))
366355 return AVERROR(ENOMEM);
367356 s->block = s->blocks[0];
@@ -420,9 +409,6 @@ static av_cold void free_duplicate_context(MpegEncContext *s) · 52 unmodified lines
420409 s->sc.obmc_scratchpad = NULL;
421410 s->sc.linesize = 0;
422411
423− av_freep(&s->dct_error_sum);
424− av_freep(&s->me.map);
425− s->me.score_map = NULL;
426412 av_freep(&s->blocks);
427413 av_freep(&s->ac_val_base);
428414 s->block = NULL;
@@ -646,9 +632,6 @@ static void clear_context(MpegEncContext *s) · 217 unmodified lines
646632
647633 memset(s->thread_context, 0, sizeof(s->thread_context));
648634
649− s->me.map = NULL;
650− s->me.score_map = NULL;
651− s->dct_error_sum = NULL;
652635 s->block = NULL;
653636 s->blocks = NULL;
654637 s->ac_val_base = NULL;
libavcodec/mpegvideo_enc.c+54−0
@@ -33,6 +33,7 @@ · 32 unmodified lines
3333
3434 #include "config_components.h"
3535
36+#include <assert.h>
3637 #include <stdint.h>
3738
3839 #include "libavutil/emms.h"
@@ -420,6 +421,53 @@ static av_cold int init_matrices(MPVMainEncContext *const m, AVCodecContext *avc · 381 unmodified lines
420421 return 0;
421422 }
422423
424+static av_cold int init_buffers(MPVMainEncContext *const m, AVCodecContext *avctx)
425+{
426+ MpegEncContext *const s = &m->s;
427+ // Align the following per-thread buffers to avoid false sharing.
428+ enum {
429+#ifndef _MSC_VER
430+ /// The number is supposed to match/exceed the cache-line size.
431+ ALIGN = FFMAX(128, _Alignof(max_align_t)),
432+#else
433+ ALIGN = 128,
434+#endif
435+ ME_MAP_ALLOC_SIZE = FFALIGN(2 * ME_MAP_SIZE * sizeof(*s->me.map), ALIGN),
436+ DCT_ERROR_SIZE = FFALIGN(2 * sizeof(*s->dct_error_sum), ALIGN),
437+ };
438+ static_assert(FFMAX(ME_MAP_ALLOC_SIZE, DCT_ERROR_SIZE) * MAX_THREADS + ALIGN - 1 <= SIZE_MAX,
439+ "Need checks for potential overflow.");
440+ unsigned nb_slices = s->slice_context_count;
441+ char *dct_error = NULL, *me_map;
442+
443+ if (s->noise_reduction) {
444+ dct_error = av_mallocz(ALIGN - 1 + nb_slices * DCT_ERROR_SIZE);
445+ if (!dct_error)
446+ return AVERROR(ENOMEM);
447+ m->dct_error_sum_base = dct_error;
448+ dct_error += FFALIGN((uintptr_t)dct_error, ALIGN) - (uintptr_t)dct_error;
449+ }
450+ me_map = av_mallocz(ALIGN - 1 + nb_slices * ME_MAP_ALLOC_SIZE);
451+ if (!me_map)
452+ return AVERROR(ENOMEM);
453+ m->me_map_base = me_map;
454+ me_map += FFALIGN((uintptr_t)me_map, ALIGN) - (uintptr_t)me_map;
455+
456+ for (unsigned i = 0; i < nb_slices; ++i) {
457+ MpegEncContext *const s2 = s->thread_context[i];
458+
459+ if (dct_error) {
460+ s2->dct_error_sum = (void*)dct_error;
461+ dct_error += DCT_ERROR_SIZE;
462+ }
463+ s2->me.map = (uint32_t*)me_map;
464+ s2->me.score_map = s2->me.map + ME_MAP_SIZE;
465+ me_map += ME_MAP_ALLOC_SIZE;
466+ }
467+
468+ return 0;
469+}
470+
423471 /* init video encoder */
424472 av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
425473 {
@@ -923,6 +971,10 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 497 unmodified lines
923971 if (ret < 0)
924972 return ret;
925973
974+ ret = init_buffers(m, avctx);
975+ if (ret < 0)
976+ return ret;
977+
926978 /* Allocate MV tables; the MV and MB tables will be copied
927979 * to slice contexts by ff_update_duplicate_context(). */
928980 mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
@@ -1067,6 +1119,8 @@ av_cold int ff_mpv_encode_end(AVCodecContext *avctx) · 138 unmodified lines
10671119 av_freep(&s->b_field_mv_table_base);
10681120 av_freep(&s->b_field_select_table[0][0]);
10691121 av_freep(&s->p_field_select_table[0]);
1122+ av_freep(&m->dct_error_sum_base);
1123+ av_freep(&m->me_map_base);
10701124
10711125 av_freep(&s->mb_type);
10721126 av_freep(&s->lambda_table);
libavcodec/mpegvideoenc.h+3−0
@@ -98,6 +98,9 @@ typedef struct MPVMainEncContext { · 97 unmodified lines
9898
9999 int64_t mb_var_sum; ///< sum of MB variance for current frame
100100 int64_t mc_mb_var_sum; ///< motion compensated MB variance for current frame
101+
102+ char *me_map_base; ///< backs MotionEstContext.(map|score_map)
103+ char *dct_error_sum_base; ///< backs dct_error_sum
101104 } MPVMainEncContext;
102105
103106 static inline const MPVMainEncContext *slice_to_mainenc(const MpegEncContext *s)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t