diff mbox series

[FFmpeg-devel,06/39] avcodec/libmp3lame: Avoid copying data, allow user-supplied buffer

Message ID HE1PR0301MB21544CF9C254344F80904FD58F299@HE1PR0301MB2154.eurprd03.prod.outlook.com
State Accepted
Commit c59398970bff5ee6c0305a0228eb562ec5901b08
Headers show
Series [FFmpeg-devel,01/39] avcodec/audiotoolboxenc: Remove AV_CODEC_CAP_DR1 | expand

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 May 21, 2021, 9:17 a.m. UTC
The libmp3lame encoder already uses an internal buffer, so that the
packet size is already known before allocating the packet; therefore
one can avoid another (implicit) intermediate buffer by switching
to ff_get_encode_buffer(), thereby also supporting user-supplied
buffers..

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

Comments

James Almer May 21, 2021, 1 p.m. UTC | #1
On 5/21/2021 6:17 AM, Andreas Rheinhardt wrote:
> The libmp3lame encoder already uses an internal buffer, so that the
> packet size is already known before allocating the packet; therefore
> one can avoid another (implicit) intermediate buffer by switching
> to ff_get_encode_buffer(), thereby also supporting user-supplied
> buffers..

Extra dot.

> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>   libavcodec/libmp3lame.c | 7 ++++---
>   1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/libavcodec/libmp3lame.c b/libavcodec/libmp3lame.c
> index fe4b50257f..5675864bb2 100644
> --- a/libavcodec/libmp3lame.c
> +++ b/libavcodec/libmp3lame.c
> @@ -34,6 +34,7 @@
>   #include "libavutil/opt.h"
>   #include "avcodec.h"
>   #include "audio_frame_queue.h"
> +#include "encode.h"
>   #include "internal.h"
>   #include "mpegaudio.h"
>   #include "mpegaudiodecheader.h"
> @@ -264,7 +265,7 @@ static int mp3lame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
>       ff_dlog(avctx, "in:%d packet-len:%d index:%d\n", avctx->frame_size, len,
>               s->buffer_index);
>       if (len <= s->buffer_index) {
> -        if ((ret = ff_alloc_packet2(avctx, avpkt, len, 0)) < 0)
> +        if ((ret = ff_get_encode_buffer(avctx, avpkt, len, 0)) < 0)
>               return ret;
>           memcpy(avpkt->data, s->buffer, len);
>           s->buffer_index -= len;
> @@ -296,7 +297,6 @@ static int mp3lame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
>               AV_WL32(side_data + 4, discard_padding);
>           }
>   
> -        avpkt->size = len;
>           *got_packet_ptr = 1;
>       }
>       return 0;
> @@ -332,11 +332,12 @@ const AVCodec ff_libmp3lame_encoder = {
>       .long_name             = NULL_IF_CONFIG_SMALL("libmp3lame MP3 (MPEG audio layer 3)"),
>       .type                  = AVMEDIA_TYPE_AUDIO,
>       .id                    = AV_CODEC_ID_MP3,
> +    .capabilities          = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
> +                             AV_CODEC_CAP_SMALL_LAST_FRAME,
>       .priv_data_size        = sizeof(LAMEContext),
>       .init                  = mp3lame_encode_init,
>       .encode2               = mp3lame_encode_frame,
>       .close                 = mp3lame_encode_close,
> -    .capabilities          = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SMALL_LAST_FRAME,
>       .sample_fmts           = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P,
>                                                                AV_SAMPLE_FMT_FLTP,
>                                                                AV_SAMPLE_FMT_S16P,
>
diff mbox series

Patch

diff --git a/libavcodec/libmp3lame.c b/libavcodec/libmp3lame.c
index fe4b50257f..5675864bb2 100644
--- a/libavcodec/libmp3lame.c
+++ b/libavcodec/libmp3lame.c
@@ -34,6 +34,7 @@ 
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "audio_frame_queue.h"
+#include "encode.h"
 #include "internal.h"
 #include "mpegaudio.h"
 #include "mpegaudiodecheader.h"
@@ -264,7 +265,7 @@  static int mp3lame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     ff_dlog(avctx, "in:%d packet-len:%d index:%d\n", avctx->frame_size, len,
             s->buffer_index);
     if (len <= s->buffer_index) {
-        if ((ret = ff_alloc_packet2(avctx, avpkt, len, 0)) < 0)
+        if ((ret = ff_get_encode_buffer(avctx, avpkt, len, 0)) < 0)
             return ret;
         memcpy(avpkt->data, s->buffer, len);
         s->buffer_index -= len;
@@ -296,7 +297,6 @@  static int mp3lame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
             AV_WL32(side_data + 4, discard_padding);
         }
 
-        avpkt->size = len;
         *got_packet_ptr = 1;
     }
     return 0;
@@ -332,11 +332,12 @@  const AVCodec ff_libmp3lame_encoder = {
     .long_name             = NULL_IF_CONFIG_SMALL("libmp3lame MP3 (MPEG audio layer 3)"),
     .type                  = AVMEDIA_TYPE_AUDIO,
     .id                    = AV_CODEC_ID_MP3,
+    .capabilities          = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
+                             AV_CODEC_CAP_SMALL_LAST_FRAME,
     .priv_data_size        = sizeof(LAMEContext),
     .init                  = mp3lame_encode_init,
     .encode2               = mp3lame_encode_frame,
     .close                 = mp3lame_encode_close,
-    .capabilities          = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SMALL_LAST_FRAME,
     .sample_fmts           = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P,
                                                              AV_SAMPLE_FMT_FLTP,
                                                              AV_SAMPLE_FMT_S16P,