[FFmpeg-devel] [PATCH 19/46] avcodec/g722enc: Avoid copying packet data, allow user-supplied buffers

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Fri Apr 30 02:56:50 EEST 2021


When the packet size is known in advance like here, one can avoid
an intermediate buffer for the packet data by using
ff_get_encode_buffer() and also set AV_CODEC_CAP_DR1 at the same time.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/g722enc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libavcodec/g722enc.c b/libavcodec/g722enc.c
index 24880f9550..fadadd5cdf 100644
--- a/libavcodec/g722enc.c
+++ b/libavcodec/g722enc.c
@@ -29,6 +29,7 @@
 
 #include "libavutil/avassert.h"
 #include "avcodec.h"
+#include "encode.h"
 #include "internal.h"
 #include "g722.h"
 #include "libavutil/common.h"
@@ -346,7 +347,7 @@ static int g722_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     int nb_samples, out_size, ret;
 
     out_size = (frame->nb_samples + 1) / 2;
-    if ((ret = ff_alloc_packet2(avctx, avpkt, out_size, 0)) < 0)
+    if ((ret = ff_get_encode_buffer(avctx, avpkt, out_size, 0)) < 0)
         return ret;
 
     nb_samples = frame->nb_samples - (frame->nb_samples & 1);
@@ -373,11 +374,11 @@ const AVCodec ff_adpcm_g722_encoder = {
     .long_name       = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
     .type            = AVMEDIA_TYPE_AUDIO,
     .id              = AV_CODEC_ID_ADPCM_G722,
+    .capabilities    = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SMALL_LAST_FRAME,
     .priv_data_size  = sizeof(G722Context),
     .init            = g722_encode_init,
     .close           = g722_encode_close,
     .encode2         = g722_encode_frame,
-    .capabilities    = AV_CODEC_CAP_SMALL_LAST_FRAME,
     .sample_fmts     = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
     .channel_layouts = (const uint64_t[]){ AV_CH_LAYOUT_MONO, 0 },
     .caps_internal   = FF_CODEC_CAP_INIT_CLEANUP,
-- 
2.27.0



More information about the ffmpeg-devel mailing list