diff mbox series

[FFmpeg-devel,42/46] avcodec/videotoolboxenc: Avoid copying data, allow user-supplied buffers

Message ID HE1PR0301MB21540B1D6ADCFAC37E30A8B08F5E9@HE1PR0301MB2154.eurprd03.prod.outlook.com
State Accepted
Commit 7539a01bb0ea24998e1aa87706edf9785ddd3ac6
Headers show
Series [FFmpeg-devel,01/46] avcodec/a64multienc: Avoid intermediate buffer
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Andreas Rheinhardt April 29, 2021, 11:57 p.m. UTC
Here the packet size is known before allocating the packet because
the encoder provides said information (and works with internal buffers
itself), so one use this information to avoid the implicit use of another
intermediate buffer for the packet data; and by switching to
ff_get_encode_buffer() one can also allow user-supplied buffers.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
This is completely untested (but actually also trivial). I hope someone
can actually test it.

 libavcodec/videotoolboxenc.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Comments

Rick Kern May 6, 2021, 4:32 a.m. UTC | #1
On Thu, Apr 29, 2021 at 8:04 PM Andreas Rheinhardt <
andreas.rheinhardt@outlook.com> wrote:

> Here the packet size is known before allocating the packet because
> the encoder provides said information (and works with internal buffers
> itself), so one use this information to avoid the implicit use of another
> intermediate buffer for the packet data; and by switching to
> ff_get_encode_buffer() one can also allow user-supplied buffers.
>
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
> This is completely untested (but actually also trivial). I hope someone
> can actually test it.
>
This works fine - applied.


>
>  libavcodec/videotoolboxenc.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index e576ff7d7e..f3e66c9e06 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -32,6 +32,7 @@
>  #include "internal.h"
>  #include <pthread.h>
>  #include "atsc_a53.h"
> +#include "encode.h"
>  #include "h264.h"
>  #include "h264_sei.h"
>  #include <dlfcn.h>
> @@ -1974,7 +1975,7 @@ static int vtenc_cm_to_avpacket(
>                     sei_nalu_size +
>                     nalu_count * ((int)sizeof(start_code) -
> (int)length_code_size);
>
> -    status = ff_alloc_packet2(avctx, pkt, out_buf_size, out_buf_size);
> +    status = ff_get_encode_buffer(avctx, pkt, out_buf_size, 0);
>      if (status < 0)
>          return status;
>
> @@ -2017,7 +2018,6 @@ static int vtenc_cm_to_avpacket(
>      time_base_num = avctx->time_base.num;
>      pkt->pts = pts.value / time_base_num;
>      pkt->dts = dts.value / time_base_num - dts_delta;
> -    pkt->size = out_buf_size;
>
>      return 0;
>  }
> @@ -2664,12 +2664,12 @@ const AVCodec ff_h264_videotoolbox_encoder = {
>      .long_name        = NULL_IF_CONFIG_SMALL("VideoToolbox H.264
> Encoder"),
>      .type             = AVMEDIA_TYPE_VIDEO,
>      .id               = AV_CODEC_ID_H264,
> +    .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
>      .priv_data_size   = sizeof(VTEncContext),
>      .pix_fmts         = avc_pix_fmts,
>      .init             = vtenc_init,
>      .encode2          = vtenc_frame,
>      .close            = vtenc_close,
> -    .capabilities     = AV_CODEC_CAP_DELAY,
>      .priv_class       = &h264_videotoolbox_class,
>      .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE |
>                          FF_CODEC_CAP_INIT_CLEANUP,
> @@ -2698,12 +2698,13 @@ const AVCodec ff_hevc_videotoolbox_encoder = {
>      .long_name        = NULL_IF_CONFIG_SMALL("VideoToolbox H.265
> Encoder"),
>      .type             = AVMEDIA_TYPE_VIDEO,
>      .id               = AV_CODEC_ID_HEVC,
> +    .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
> +                        AV_CODEC_CAP_HARDWARE,
>      .priv_data_size   = sizeof(VTEncContext),
>      .pix_fmts         = hevc_pix_fmts,
>      .init             = vtenc_init,
>      .encode2          = vtenc_frame,
>      .close            = vtenc_close,
> -    .capabilities     = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
>      .priv_class       = &hevc_videotoolbox_class,
>      .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE |
>                          FF_CODEC_CAP_INIT_CLEANUP,
> --
> 2.27.0
>
> _______________________________________________
> 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/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index e576ff7d7e..f3e66c9e06 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -32,6 +32,7 @@ 
 #include "internal.h"
 #include <pthread.h>
 #include "atsc_a53.h"
+#include "encode.h"
 #include "h264.h"
 #include "h264_sei.h"
 #include <dlfcn.h>
@@ -1974,7 +1975,7 @@  static int vtenc_cm_to_avpacket(
                    sei_nalu_size +
                    nalu_count * ((int)sizeof(start_code) - (int)length_code_size);
 
-    status = ff_alloc_packet2(avctx, pkt, out_buf_size, out_buf_size);
+    status = ff_get_encode_buffer(avctx, pkt, out_buf_size, 0);
     if (status < 0)
         return status;
 
@@ -2017,7 +2018,6 @@  static int vtenc_cm_to_avpacket(
     time_base_num = avctx->time_base.num;
     pkt->pts = pts.value / time_base_num;
     pkt->dts = dts.value / time_base_num - dts_delta;
-    pkt->size = out_buf_size;
 
     return 0;
 }
@@ -2664,12 +2664,12 @@  const AVCodec ff_h264_videotoolbox_encoder = {
     .long_name        = NULL_IF_CONFIG_SMALL("VideoToolbox H.264 Encoder"),
     .type             = AVMEDIA_TYPE_VIDEO,
     .id               = AV_CODEC_ID_H264,
+    .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
     .priv_data_size   = sizeof(VTEncContext),
     .pix_fmts         = avc_pix_fmts,
     .init             = vtenc_init,
     .encode2          = vtenc_frame,
     .close            = vtenc_close,
-    .capabilities     = AV_CODEC_CAP_DELAY,
     .priv_class       = &h264_videotoolbox_class,
     .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE |
                         FF_CODEC_CAP_INIT_CLEANUP,
@@ -2698,12 +2698,13 @@  const AVCodec ff_hevc_videotoolbox_encoder = {
     .long_name        = NULL_IF_CONFIG_SMALL("VideoToolbox H.265 Encoder"),
     .type             = AVMEDIA_TYPE_VIDEO,
     .id               = AV_CODEC_ID_HEVC,
+    .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
+                        AV_CODEC_CAP_HARDWARE,
     .priv_data_size   = sizeof(VTEncContext),
     .pix_fmts         = hevc_pix_fmts,
     .init             = vtenc_init,
     .encode2          = vtenc_frame,
     .close            = vtenc_close,
-    .capabilities     = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
     .priv_class       = &hevc_videotoolbox_class,
     .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE |
                         FF_CODEC_CAP_INIT_CLEANUP,