From c487aa43895ba646866e64c679b73b91cc7e6e49 Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Wed, 4 Jun 2025 18:37:46 -0500 Subject: [PATCH] Fixing the alignment of our FFmpegReader video buffers to be 32 / AVX2. This increases performance by a factor of 3X. --- src/FFmpegReader.cpp | 8 ++++++-- src/FFmpegReader.h | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/FFmpegReader.cpp b/src/FFmpegReader.cpp index afdff878..21d847aa 100644 --- a/src/FFmpegReader.cpp +++ b/src/FFmpegReader.cpp @@ -1568,8 +1568,12 @@ void FFmpegReader::ProcessVideoPacket(int64_t requested_frame) { // Determine required buffer size and allocate buffer const int bytes_per_pixel = 4; - int buffer_size = (width * height * bytes_per_pixel) + 128; - buffer = new unsigned char[buffer_size](); + int raw_buffer_size = (width * height * bytes_per_pixel) + 128; + + // Aligned memory allocation (for speed) + constexpr size_t ALIGNMENT = 32; // AVX2 + int buffer_size = ((raw_buffer_size + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT; + buffer = (unsigned char*) aligned_alloc(ALIGNMENT, buffer_size); // Copy picture data from one AVFrame (or AVPicture) to another one. AV_COPY_PICTURE_DATA(pFrameRGB, buffer, PIX_FMT_RGBA, width, height); diff --git a/src/FFmpegReader.h b/src/FFmpegReader.h index 4ccd9554..295cdcf3 100644 --- a/src/FFmpegReader.h +++ b/src/FFmpegReader.h @@ -31,6 +31,7 @@ #include "Clip.h" #include "OpenMPUtilities.h" #include "Settings.h" +#include namespace openshot {