diff mbox series

[FFmpeg-devel,3/3] avcodec/libx264: Simplify copying packet data

Message ID AM7PR03MB6660F7501500A68446CCDABC8F909@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Commit b59138b6819b97544fd2fc40a27fdac0d75b2287
Headers show
Series [FFmpeg-devel,1/3] avcodec/libx264: Check for overflow if necessary
Related show

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Andreas Rheinhardt Nov. 7, 2021, 2:36 p.m. UTC
x264.h: "the payloads of all output NALs are guaranteed to be
sequential in memory." Therefore we can omit the loop.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/libx264.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

Comments

James Almer Nov. 7, 2021, 4:46 p.m. UTC | #1
On 11/7/2021 11:36 AM, Andreas Rheinhardt wrote:
> x264.h: "the payloads of all output NALs are guaranteed to be
> sequential in memory." Therefore we can omit the loop.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>   libavcodec/libx264.c | 9 ++++-----
>   1 file changed, 4 insertions(+), 5 deletions(-)
> 
> diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
> index 4fe02dd11c..5f62c7b1d8 100644
> --- a/libavcodec/libx264.c
> +++ b/libavcodec/libx264.c
> @@ -139,7 +139,6 @@ static int encode_nals(AVCodecContext *ctx, AVPacket *pkt,
>       X264Context *x4 = ctx->priv_data;
>       uint8_t *p;
>       uint64_t size = x4->sei_size;
> -    int i;
>       int ret;
>   
>       if (!nnal)
> @@ -165,14 +164,14 @@ static int encode_nals(AVCodecContext *ctx, AVPacket *pkt,
>       if (x4->sei_size > 0) {
>           memcpy(p, x4->sei, x4->sei_size);
>           p += x4->sei_size;
> +        size -= x4->sei_size;
>           x4->sei_size = 0;
>           av_freep(&x4->sei);
>       }
>   
> -    for (i = 0; i < nnal; i++){
> -        memcpy(p, nals[i].p_payload, nals[i].i_payload);
> -        p += nals[i].i_payload;
> -    }
> +    /* x264 guarantees the payloads of the NALs
> +     * to be sequential in memory. */
> +    memcpy(p, nals[0].p_payload, size);
>   
>       return 1;
>   }

LGTM.
diff mbox series

Patch

diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index 4fe02dd11c..5f62c7b1d8 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -139,7 +139,6 @@  static int encode_nals(AVCodecContext *ctx, AVPacket *pkt,
     X264Context *x4 = ctx->priv_data;
     uint8_t *p;
     uint64_t size = x4->sei_size;
-    int i;
     int ret;
 
     if (!nnal)
@@ -165,14 +164,14 @@  static int encode_nals(AVCodecContext *ctx, AVPacket *pkt,
     if (x4->sei_size > 0) {
         memcpy(p, x4->sei, x4->sei_size);
         p += x4->sei_size;
+        size -= x4->sei_size;
         x4->sei_size = 0;
         av_freep(&x4->sei);
     }
 
-    for (i = 0; i < nnal; i++){
-        memcpy(p, nals[i].p_payload, nals[i].i_payload);
-        p += nals[i].i_payload;
-    }
+    /* x264 guarantees the payloads of the NALs
+     * to be sequential in memory. */
+    memcpy(p, nals[0].p_payload, size);
 
     return 1;
 }