diff mbox series

[FFmpeg-devel] avutil/imgutils: optimize image copy efficiency

Message ID tencent_E931D38C21E051D99E7927CB2E4268D9E908@qq.com
State New
Headers show
Series [FFmpeg-devel] avutil/imgutils: optimize image copy efficiency | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

徐福隆 May 4, 2023, 7:54 a.m. UTC
It makes sense when copying 4K/8K video, if linesize
equals to aligned linesize.

Signed-off-by: xufuji456 <839789740@qq.com>
---
 libavutil/imgutils.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

Comments

Martin Storsjö May 4, 2023, 8:22 a.m. UTC | #1
On Thu, 4 May 2023, xufuji456 wrote:

> It makes sense when copying 4K/8K video, if linesize
> equals to aligned linesize.
>
> Signed-off-by: xufuji456 <839789740@qq.com>
> ---
> libavutil/imgutils.c | 15 +++++++++++----
> 1 file changed, 11 insertions(+), 4 deletions(-)
>
> diff --git a/libavutil/imgutils.c b/libavutil/imgutils.c
> index 9ab5757cf6..1d432e7a57 100644
> --- a/libavutil/imgutils.c
> +++ b/libavutil/imgutils.c
> @@ -525,10 +525,17 @@ int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
>         const uint8_t *src = src_data[i];
>         h = (height + (1 << shift) - 1) >> shift;
>
> -        for (j = 0; j < h; j++) {
> -            memcpy(dst, src, linesize[i]);
> -            dst += FFALIGN(linesize[i], align);
> -            src += src_linesize[i];
> +        if (FFALIGN(linesize[i], align) == linesize[i] && src_linesize[i] == linesize[i]) {
> +            int size = linesize[i] * h;

For cases like these, I would prefer to use "linesize[i] * (h - 1) + w" 
instead. For cases if copying e.g. into an offsetted position within a 
buffer, writing the last trailing padding would end up writing out of 
bounds.

That said, I'm unsure about how much gain you get from this optimization 
here - some numbers to back it up would be useful.

// Martin
徐福隆 May 4, 2023, 1:18 p.m. UTC | #2
Thank you for your review, Martin.
I try add some data, and submit again.




------------------&nbsp;Original&nbsp;------------------
From:                                                                                                                        "FFmpeg development discussions and patches"                                                                                    <martin@martin.st&gt;;
Date:&nbsp;Thu, May 4, 2023 04:22 PM
To:&nbsp;"FFmpeg development discussions and patches"<ffmpeg-devel@ffmpeg.org&gt;;
Cc:&nbsp;"徐福隆"<839789740@qq.com&gt;;
Subject:&nbsp;Re: [FFmpeg-devel] [PATCH] avutil/imgutils: optimize image copy efficiency



On Thu, 4 May 2023, xufuji456 wrote:

&gt; It makes sense when copying 4K/8K video, if linesize
&gt; equals to aligned linesize.
&gt;
&gt; Signed-off-by: xufuji456 <839789740@qq.com&gt;
&gt; ---
&gt; libavutil/imgutils.c | 15 +++++++++++----
&gt; 1 file changed, 11 insertions(+), 4 deletions(-)
&gt;
&gt; diff --git a/libavutil/imgutils.c b/libavutil/imgutils.c
&gt; index 9ab5757cf6..1d432e7a57 100644
&gt; --- a/libavutil/imgutils.c
&gt; +++ b/libavutil/imgutils.c
&gt; @@ -525,10 +525,17 @@ int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const uint8_t *src = src_data[i];
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; h = (height + (1 << shift) - 1) &gt;&gt; shift;
&gt;
&gt; -&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (j = 0; j < h; j++) {
&gt; -&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; memcpy(dst, src, linesize[i]);
&gt; -&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dst += FFALIGN(linesize[i], align);
&gt; -&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; src += src_linesize[i];
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (FFALIGN(linesize[i], align) == linesize[i] &amp;&amp; src_linesize[i] == linesize[i]) {
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int size = linesize[i] * h;

For cases like these, I would prefer to use "linesize[i] * (h - 1) + w" 
instead. For cases if copying e.g. into an offsetted position within a 
buffer, writing the last trailing padding would end up writing out of 
bounds.

That said, I'm unsure about how much gain you get from this optimization 
here - some numbers to back it up would be useful.

// Martin

_______________________________________________
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".
徐福隆 May 5, 2023, 9:51 a.m. UTC | #3
There is an if condition "FFALIGN(linesize[i], align) == linesize[i] &amp;&amp; src_linesize[i] == linesize[i]".
When src's linesize equals to dst, it has no padding in this case I think. If has padding, it will execute
previous process. As a result, we could ignore the situation of padding in this case.


Thank you


//frank
&nbsp;




------------------&nbsp;Original&nbsp;------------------
From:                                                                                                                        "FFmpeg development discussions and patches"                                                                                    <martin@martin.st&gt;;
Date:&nbsp;Thu, May 4, 2023 04:22 PM
To:&nbsp;"FFmpeg development discussions and patches"<ffmpeg-devel@ffmpeg.org&gt;;
Cc:&nbsp;"徐福隆"<839789740@qq.com&gt;;
Subject:&nbsp;Re: [FFmpeg-devel] [PATCH] avutil/imgutils: optimize image copy efficiency



On Thu, 4 May 2023, xufuji456 wrote:

&gt; It makes sense when copying 4K/8K video, if linesize
&gt; equals to aligned linesize.
&gt;
&gt; Signed-off-by: xufuji456 <839789740@qq.com&gt;
&gt; ---
&gt; libavutil/imgutils.c | 15 +++++++++++----
&gt; 1 file changed, 11 insertions(+), 4 deletions(-)
&gt;
&gt; diff --git a/libavutil/imgutils.c b/libavutil/imgutils.c
&gt; index 9ab5757cf6..1d432e7a57 100644
&gt; --- a/libavutil/imgutils.c
&gt; +++ b/libavutil/imgutils.c
&gt; @@ -525,10 +525,17 @@ int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const uint8_t *src = src_data[i];
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; h = (height + (1 << shift) - 1) &gt;&gt; shift;
&gt;
&gt; -&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (j = 0; j < h; j++) {
&gt; -&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; memcpy(dst, src, linesize[i]);
&gt; -&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dst += FFALIGN(linesize[i], align);
&gt; -&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; src += src_linesize[i];
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (FFALIGN(linesize[i], align) == linesize[i] &amp;&amp; src_linesize[i] == linesize[i]) {
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int size = linesize[i] * h;

For cases like these, I would prefer to use "linesize[i] * (h - 1) + w" 
instead. For cases if copying e.g. into an offsetted position within a 
buffer, writing the last trailing padding would end up writing out of 
bounds.

That said, I'm unsure about how much gain you get from this optimization 
here - some numbers to back it up would be useful.

// Martin

_______________________________________________
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".
diff mbox series

Patch

diff --git a/libavutil/imgutils.c b/libavutil/imgutils.c
index 9ab5757cf6..1d432e7a57 100644
--- a/libavutil/imgutils.c
+++ b/libavutil/imgutils.c
@@ -525,10 +525,17 @@  int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
         const uint8_t *src = src_data[i];
         h = (height + (1 << shift) - 1) >> shift;
 
-        for (j = 0; j < h; j++) {
-            memcpy(dst, src, linesize[i]);
-            dst += FFALIGN(linesize[i], align);
-            src += src_linesize[i];
+        if (FFALIGN(linesize[i], align) == linesize[i] && src_linesize[i] == linesize[i]) {
+            int size = linesize[i] * h;
+            memcpy(dst, src, size);
+            dst += size;
+            src += size;
+        } else {
+            for (j = 0; j < h; j++) {
+                memcpy(dst, src, linesize[i]);
+                dst += FFALIGN(linesize[i], align);
+                src += src_linesize[i];
+            }
         }
     }