[FFmpeg-devel] [PATCH] lavu/get_video_buffer: also align data pointers (v2)

Pavel Koshevoy pkoshevoy at gmail.com
Fri Nov 15 21:32:21 EET 2024


This avoids unpleasant surprises to av_frame_get_buffer callers
that explicitly specified 64-byte alignment and didn't get
AVFrame.data pointers that are 64-byte aligned.

For example, see https://github.com/sekrit-twc/zimg/issues/212

Although the zscale issue has already been resolved by other means
it would still be prudent to improve the behavior of av_frame_get_buffer
to fix any unknown and future instances of similar issues.
---
 libavutil/frame.c | 4 +++-
 libavutil/frame.h | 7 ++++---
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/libavutil/frame.c b/libavutil/frame.c
index f0a0dba018..7faf7aeae8 100644
--- a/libavutil/frame.c
+++ b/libavutil/frame.c
@@ -216,6 +216,7 @@ static int get_video_buffer(AVFrame *frame, int align)
         total_size += sizes[i];
     }
 
+    total_size += align - 1;
     frame->buf[0] = av_buffer_alloc(total_size);
     if (!frame->buf[0]) {
         ret = AVERROR(ENOMEM);
@@ -223,7 +224,8 @@ static int get_video_buffer(AVFrame *frame, int align)
     }
 
     if ((ret = av_image_fill_pointers(frame->data, frame->format, padded_height,
-                                      frame->buf[0]->data, frame->linesize)) < 0)
+                                      (uint8_t *)FFALIGN((uintptr_t)frame->buf[0]->data, align),
+                                      frame->linesize)) < 0)
         goto fail;
 
     for (int i = 1; i < 4; i++) {
diff --git a/libavutil/frame.h b/libavutil/frame.h
index f7806566d5..c107f43bc0 100644
--- a/libavutil/frame.h
+++ b/libavutil/frame.h
@@ -887,9 +887,10 @@ void av_frame_move_ref(AVFrame *dst, AVFrame *src);
  *           cases.
  *
  * @param frame frame in which to store the new buffers.
- * @param align Required buffer size alignment. If equal to 0, alignment will be
- *              chosen automatically for the current CPU. It is highly
- *              recommended to pass 0 here unless you know what you are doing.
+ * @param align Required buffer size and data pointer alignment. If equal to 0,
+ *              alignment will be chosen automatically for the current CPU.
+ *              It is highly recommended to pass 0 here unless you know what
+ *              you are doing.
  *
  * @return 0 on success, a negative AVERROR on error.
  */
-- 
2.43.0



More information about the ffmpeg-devel mailing list