This is an automated email from the git hooks/post-receive script.

Git pushed a commit to branch master
in repository ffmpeg.

commit 5ca18fdde49d97b7d4c194fab1c9550bb606689d
Author:     Niklas Haas <[email protected]>
AuthorDate: Tue Aug 4 13:22:31 2026 +0200
Commit:     Niklas Haas <[email protected]>
CommitDate: Sun Aug 9 21:03:33 2026 +0200

    swscale/graph: internally ref copied intermediate buffers
    
    If the pass we are allocating output buffers for has a plane copy map,
    we can optimize away the internal buffer by setting it to a ref of its
    input buffer, if one exists.
    
    In theory, we can also do this if there is not an input buffer, by
    directly referencing a sentinel or placeholder SwsPassBuffer
    corresponding to the original input image, but this will require a bit
    more work so I have decided to hold off on it for now.
    
    Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/graph.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/libswscale/graph.c b/libswscale/graph.c
index 012bb53388..0f444199f7 100644
--- a/libswscale/graph.c
+++ b/libswscale/graph.c
@@ -54,8 +54,9 @@ int ff_sws_pass_aligned_width(const SwsPass *pass, int width)
     return aligned_w <= INT_MAX ? aligned_w : width;
 }
 
-/* Allocates one buffer per plane */
-static int frame_alloc_planes(AVFrame *dst)
+/* Allocates (or refs) one buffer per plane */
+static int frame_alloc_planes_ref(AVFrame *dst, const AVFrame *src,
+                                  const int plane_copy[4])
 {
     int ret = av_image_check_size2(dst->width, dst->height, INT64_MAX,
                                    dst->format, 0, NULL);
@@ -80,6 +81,17 @@ static int frame_alloc_planes(AVFrame *dst)
     for (int i = 0; i < 4; i++) {
         if (!sizes[i])
             break;
+        int src_idx = plane_copy[i];
+        if (src_idx >= 0 && src) {
+            /* Ref the source plane instead of allocating a new buffer */
+            dst->buf[i] = av_buffer_ref(src->buf[src_idx]);
+            if (!dst->buf[i])
+                return AVERROR(ENOMEM);
+            dst->data[i]     = src->data[src_idx];
+            dst->linesize[i] = src->linesize[src_idx];
+            continue;
+        }
+
         AVBufferRef *buf = av_buffer_alloc(sizes[i]);
         if (!buf)
             return AVERROR(ENOMEM);
@@ -143,8 +155,12 @@ static int pass_alloc_output(SwsPass *pass)
     }
 #endif
 
+    const AVFrame *src = NULL;
+    if (pass->input)
+        src = pass->input->output->avframe;
+
     avframe->format = pass->format;
-    ret = frame_alloc_planes(avframe);
+    ret = frame_alloc_planes_ref(avframe, src, buffer->plane_copy);
     if (ret < 0) {
         av_frame_free(&avframe);
         return ret;

_______________________________________________
ffmpeg-cvslog mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to