avcodec/libcodec2: Avoid copying packet data, allow user-supplied buffers

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.

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 044daa7c45
commit 398822a70e

View File

@ -22,6 +22,7 @@
#include <codec2/codec2.h>
#include "avcodec.h"
#include "libavutil/opt.h"
#include "encode.h"
#include "internal.h"
#include "codec2utils.h"
@ -169,7 +170,7 @@ static int libcodec2_encode(AVCodecContext *avctx, AVPacket *avpkt,
LibCodec2Context *c2 = avctx->priv_data;
int16_t *samples = (int16_t *)frame->data[0];
int ret = ff_alloc_packet2(avctx, avpkt, avctx->block_align, 0);
int ret = ff_get_encode_buffer(avctx, avpkt, avctx->block_align, 0);
if (ret < 0) {
return ret;
}
@ -201,11 +202,11 @@ const AVCodec ff_libcodec2_encoder = {
.long_name = NULL_IF_CONFIG_SMALL("codec2 encoder using libcodec2"),
.type = AVMEDIA_TYPE_AUDIO,
.id = AV_CODEC_ID_CODEC2,
.capabilities = AV_CODEC_CAP_DR1,
.priv_data_size = sizeof(LibCodec2Context),
.init = libcodec2_init_encoder,
.close = libcodec2_close,
.encode2 = libcodec2_encode,
.capabilities = 0,
.supported_samplerates = (const int[]){ 8000, 0 },
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
.channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO, 0 },