[FFmpeg-devel] [PATCH 1/4] avfilter/trim: consume all available frames and avoid activate reschedule

Marton Balint cus at passwd.hu
Thu Jul 24 02:35:57 EEST 2025


There is no benefit in delaying processing all available frames.

Signed-off-by: Marton Balint <cus at passwd.hu>
---
 libavfilter/trim.c | 42 +++++++++++++++++-------------------------
 1 file changed, 17 insertions(+), 25 deletions(-)

diff --git a/libavfilter/trim.c b/libavfilter/trim.c
index 7200680716..6d1016ac81 100644
--- a/libavfilter/trim.c
+++ b/libavfilter/trim.c
@@ -90,10 +90,8 @@ static int trim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
     int drop;
 
     /* drop everything if EOF has already been returned */
-    if (s->eof) {
-        av_frame_free(&frame);
+    if (s->eof)
         return 0;
-    }
 
     if (s->start_frame >= 0 || s->start_pts != AV_NOPTS_VALUE) {
         drop = 1;
@@ -131,13 +129,10 @@ static int trim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
 
     s->nb_frames++;
 
-    return ff_filter_frame(ctx->outputs[0], frame);
+    return 1;
 
 drop:
-    if (!s->eof)
-        ff_filter_set_ready(ctx, 100);
     s->nb_frames++;
-    av_frame_free(&frame);
     return 0;
 }
 #endif // CONFIG_TRIM_FILTER
@@ -152,10 +147,8 @@ static int atrim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
     int drop;
 
     /* drop everything if EOF has already been returned */
-    if (s->eof) {
-        av_frame_free(&frame);
+    if (s->eof)
         return 0;
-    }
 
     if (frame->pts != AV_NOPTS_VALUE)
         pts = av_rescale_q(frame->pts, inlink->time_base,
@@ -230,10 +223,8 @@ static int atrim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
 
     if (start_sample) {
         AVFrame *out = ff_get_audio_buffer(ctx->outputs[0], end_sample - start_sample);
-        if (!out) {
-            av_frame_free(&frame);
+        if (!out)
             return AVERROR(ENOMEM);
-        }
 
         av_frame_copy_props(out, frame);
         av_samples_copy(out->extended_data, frame->extended_data, 0, start_sample,
@@ -243,18 +234,16 @@ static int atrim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
             out->pts += av_rescale_q(start_sample, (AVRational){ 1, out->sample_rate },
                                      inlink->time_base);
 
-        av_frame_free(&frame);
-        frame = out;
+        av_frame_unref(frame);
+        av_frame_move_ref(frame, out);
+        av_frame_free(&out);
     } else
         frame->nb_samples = end_sample;
 
-    return ff_filter_frame(ctx->outputs[0], frame);
+    return 1;
 
 drop:
-    if (!s->eof)
-        ff_filter_set_ready(ctx, 100);
     s->nb_samples += frame->nb_samples;
-    av_frame_free(&frame);
     return 0;
 }
 #endif // CONFIG_ATRIM_FILTER
@@ -295,18 +284,21 @@ static int activate(AVFilterContext *ctx)
     TrimContext *s = ctx->priv;
     AVFilterLink *inlink = ctx->inputs[0];
     AVFilterLink *outlink = ctx->outputs[0];
+    AVFrame *frame = NULL;
+    int ret;
 
     FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
 
-    if (!s->eof && ff_inlink_queued_frames(inlink)) {
-        AVFrame *frame = NULL;
-        int ret;
-
-        ret = ff_inlink_consume_frame(inlink, &frame);
+    while ((ret = ff_inlink_consume_frame(inlink, &frame))) {
         if (ret < 0)
             return ret;
+
+        ret = s->filter_frame(inlink, frame);
         if (ret > 0)
-            return s->filter_frame(inlink, frame);
+            return ff_filter_frame(outlink, frame);
+        av_frame_free(&frame);
+        if (ret < 0)
+            return ret;
     }
 
     FF_FILTER_FORWARD_STATUS(inlink, outlink);
-- 
2.43.0



More information about the ffmpeg-devel mailing list