From 5abb5c04155b536f26fc88311ac3132890111360 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Sun, 25 Apr 2021 01:43:26 +0200 Subject: [PATCH] avcodec/flacenc: Avoid copying packet data, allow user-supplied buffers The FLAC encoder calculates the size in advance, so 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. Reviewed-by: James Almer Signed-off-by: Andreas Rheinhardt --- libavcodec/flacenc.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libavcodec/flacenc.c b/libavcodec/flacenc.c index 37ed1e4cce..de36d33333 100644 --- a/libavcodec/flacenc.c +++ b/libavcodec/flacenc.c @@ -27,6 +27,7 @@ #include "avcodec.h" #include "bswapdsp.h" +#include "encode.h" #include "put_bits.h" #include "golomb.h" #include "internal.h" @@ -1378,7 +1379,7 @@ static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, } } - if ((ret = ff_alloc_packet2(avctx, avpkt, frame_bytes, 0)) < 0) + if ((ret = ff_get_encode_buffer(avctx, avpkt, frame_bytes, 0)) < 0) return ret; out_bytes = write_frame(s, avpkt); @@ -1396,10 +1397,11 @@ static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, avpkt->pts = frame->pts; avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples); - avpkt->size = out_bytes; s->next_pts = avpkt->pts + avpkt->duration; + av_shrink_packet(avpkt, out_bytes); + *got_packet_ptr = 1; return 0; } @@ -1459,11 +1461,12 @@ const AVCodec ff_flac_encoder = { .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"), .type = AVMEDIA_TYPE_AUDIO, .id = AV_CODEC_ID_FLAC, + .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | + AV_CODEC_CAP_SMALL_LAST_FRAME, .priv_data_size = sizeof(FlacEncodeContext), .init = flac_encode_init, .encode2 = flac_encode_frame, .close = flac_encode_close, - .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY, .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE },