1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-08-10 06:10:52 +02:00

avcodec/libshine: Avoid copying data, allow user-supplied buffer

The libshine 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.

Reviewed-by: James Almer <jamrial@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt
2021-04-25 01:43:26 +02:00
parent 0812a60fee
commit 6564ab0e01

View File

@@ -24,6 +24,7 @@
#include "libavutil/intreadwrite.h" #include "libavutil/intreadwrite.h"
#include "audio_frame_queue.h" #include "audio_frame_queue.h"
#include "avcodec.h" #include "avcodec.h"
#include "encode.h"
#include "internal.h" #include "internal.h"
#include "mpegaudio.h" #include "mpegaudio.h"
#include "mpegaudiodecheader.h" #include "mpegaudiodecheader.h"
@@ -102,7 +103,7 @@ static int libshine_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
len = hdr.frame_size; len = hdr.frame_size;
if (len <= s->buffer_index) { if (len <= s->buffer_index) {
if ((ret = ff_alloc_packet2(avctx, avpkt, len, 0))) if ((ret = ff_get_encode_buffer(avctx, avpkt, len, 0)))
return ret; return ret;
memcpy(avpkt->data, s->buffer, len); memcpy(avpkt->data, s->buffer, len);
s->buffer_index -= len; s->buffer_index -= len;
@@ -111,7 +112,6 @@ static int libshine_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts, ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
&avpkt->duration); &avpkt->duration);
avpkt->size = len;
*got_packet_ptr = 1; *got_packet_ptr = 1;
} }
return 0; return 0;
@@ -135,11 +135,11 @@ const AVCodec ff_libshine_encoder = {
.long_name = NULL_IF_CONFIG_SMALL("libshine MP3 (MPEG audio layer 3)"), .long_name = NULL_IF_CONFIG_SMALL("libshine MP3 (MPEG audio layer 3)"),
.type = AVMEDIA_TYPE_AUDIO, .type = AVMEDIA_TYPE_AUDIO,
.id = AV_CODEC_ID_MP3, .id = AV_CODEC_ID_MP3,
.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
.priv_data_size = sizeof(SHINEContext), .priv_data_size = sizeof(SHINEContext),
.init = libshine_encode_init, .init = libshine_encode_init,
.encode2 = libshine_encode_frame, .encode2 = libshine_encode_frame,
.close = libshine_encode_close, .close = libshine_encode_close,
.capabilities = AV_CODEC_CAP_DELAY,
.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16P, .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16P,
AV_SAMPLE_FMT_NONE }, AV_SAMPLE_FMT_NONE },
.supported_samplerates = libshine_sample_rates, .supported_samplerates = libshine_sample_rates,