From bc6b5cef25cb2c66cb4e0e96df6d2639a61a197a Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Mon, 8 Nov 2021 14:23:02 -0800 Subject: [PATCH] GUACAMOLE-1330: Dynamically allocate AVPacket when supported (static allocation deprecated as of libavcodec 58.133.100). --- src/guacenc/ffmpeg-compat.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/guacenc/ffmpeg-compat.c b/src/guacenc/ffmpeg-compat.c index db6bec63..54e4b749 100644 --- a/src/guacenc/ffmpeg-compat.c +++ b/src/guacenc/ffmpeg-compat.c @@ -135,6 +135,15 @@ int guacenc_avcodec_encode_video(guacenc_video* video, AVFrame* frame) { #else +/* For libavcodec < 57.37.100: input/output was not decoupled and static + * allocation of AVPacket was supported. + * + * NOTE: Since dynamic allocation of AVPacket was added before this point (in + * 57.12.100) and static allocation was deprecated later (in 58.133.100), it is + * convenient to tie static vs. dynamic allocation to the old vs. new I/O + * mechanism and avoid further complicating the version comparison logic. */ +#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 37, 100) + /* Init video packet */ AVPacket packet; av_init_packet(&packet); @@ -143,9 +152,6 @@ int guacenc_avcodec_encode_video(guacenc_video* video, AVFrame* frame) { packet.data = NULL; packet.size = 0; -/* For libavcodec < 57.37.100: input/output was not decoupled */ -#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57,37,100) - /* Write frame to video */ int got_data; if (avcodec_encode_video2(video->context, &packet, frame, &got_data) < 0) { @@ -176,19 +182,25 @@ int guacenc_avcodec_encode_video(guacenc_video* video, AVFrame* frame) { return -1; } + AVPacket* packet = av_packet_alloc(); + if (packet == NULL) + return -1; + /* Flush all available packets */ int got_data = 0; - while (avcodec_receive_packet(video->context, &packet) == 0) { + while (avcodec_receive_packet(video->context, packet) == 0) { /* Data was received */ got_data = 1; /* Attempt to write data to output file */ - guacenc_write_packet(video, (void*) &packet, packet.size); - av_packet_unref(&packet); + guacenc_write_packet(video, (void*) packet, packet->size); + av_packet_unref(packet); } + av_packet_free(&packet); + #endif /* Frame may have been queued for later writing / reordering */