avcodec: Make ff_alloc_packet() based encoders accept user buffers

Up until now, these encoders received non-refcounted packets
(whose data was owned by the corresponding AVCodecContext)
from ff_alloc_packet(); these packets were made refcounted lateron
by av_packet_make_refcounted() generically.
This commit makes these encoders accept user-supplied buffers by
replacing av_packet_make_refcounted() with an equivalent function
that is based upon get_encode_buffer().

(I am pretty certain that one can also set the flag for mpegvideo-
based encoders, but I want to double-check this later. What is certain
is that it reallocates the buffer owned by the AVCodecContext
which should maybe be moved to encode.c, so that proresenc_kostya.c
and ttaenc.c can make use of it, too.)

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt
2021-05-11 20:52:13 +02:00
parent 7360e97e4b
commit a499b4345b
50 changed files with 83 additions and 30 deletions

View File

@@ -107,6 +107,23 @@ fail:
return ret;
}
static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
{
uint8_t *data = avpkt->data;
int ret;
if (avpkt->buf)
return 0;
avpkt->data = NULL;
ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0);
if (ret < 0)
return ret;
memcpy(avpkt->data, data, avpkt->size);
return 0;
}
/**
* Pad last frame with silence.
*/
@@ -184,7 +201,7 @@ int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt,
if (!ret && *got_packet) {
if (avpkt->data) {
ret = av_packet_make_refcounted(avpkt);
ret = encode_make_refcounted(avctx, avpkt);
if (ret < 0)
goto unref;
// Date returned by encoders must always be ref-counted