[FFmpeg-devel] [PATCH 2/2] ffmpeg_hw: reuse filter_frame in hwaccel_retrieve_data()

James Almer jamrial at gmail.com
Fri Nov 19 17:48:56 EET 2021


This avoids an AVFrame allocation per decoded frame.

Signed-off-by: James Almer <jamrial at gmail.com>
---
ist->filter_frame is safe to use here since send_frame_to_filters() also unrefs
it immediately after using it. But if this is deemed ugly/evil, I'll just add a
new AVFrame to InputStream.

 fftools/ffmpeg_hw.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/fftools/ffmpeg_hw.c b/fftools/ffmpeg_hw.c
index 14e702bd92..4201e44c43 100644
--- a/fftools/ffmpeg_hw.c
+++ b/fftools/ffmpeg_hw.c
@@ -500,7 +500,7 @@ int hw_device_setup_for_encode(OutputStream *ost)
 static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
 {
     InputStream *ist = avctx->opaque;
-    AVFrame *output = NULL;
+    AVFrame *output = ist->filter_frame;
     enum AVPixelFormat output_format = ist->hwaccel_output_format;
     int err;
 
@@ -509,10 +509,6 @@ static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
         return 0;
     }
 
-    output = av_frame_alloc();
-    if (!output)
-        return AVERROR(ENOMEM);
-
     output->format = output_format;
 
     err = av_hwframe_transfer_data(output, input, 0);
@@ -524,18 +520,16 @@ static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
 
     err = av_frame_copy_props(output, input);
     if (err < 0) {
-        av_frame_unref(output);
         goto fail;
     }
 
     av_frame_unref(input);
     av_frame_move_ref(input, output);
-    av_frame_free(&output);
 
     return 0;
 
 fail:
-    av_frame_free(&output);
+    av_frame_unref(output);
     return err;
 }
 
-- 
2.33.0



More information about the ffmpeg-devel mailing list