diff mbox series

[FFmpeg-devel] avfilter/af_aresample: switch to activate

Message ID CAPYw7P5FPXdT2dmxvRJ1sqaRJEcQ_U4DYSnAXgC46Zt-pEhAuQ@mail.gmail.com
State New
Headers show
Series [FFmpeg-devel] avfilter/af_aresample: switch to activate | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished

Commit Message

Paul B Mahol May 18, 2023, 4:27 p.m. UTC
With proper EOF reporting support. Attached.

Comments

Michael Niedermayer May 18, 2023, 8:54 p.m. UTC | #1
On Thu, May 18, 2023 at 06:27:00PM +0200, Paul B Mahol wrote:
> With proper EOF reporting support. Attached.

>  af_aresample.c |   54 ++++++++++++++++++++++++++++++++++++++++++++++--------
>  1 file changed, 46 insertions(+), 8 deletions(-)
> 338cf669de11643ba2c601061f210984b526bbbf  0022-avfilter-af_aresample-switch-to-activate.patch
> From ed6be8443c29ce2246eec572b7342cee00e61925 Mon Sep 17 00:00:00 2001
> From: Paul B Mahol <onemda@gmail.com>
> Date: Wed, 17 May 2023 09:15:51 +0200
> Subject: [PATCH 22/27] avfilter/af_aresample: switch to activate
> 
> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> ---
>  libavfilter/af_aresample.c | 54 ++++++++++++++++++++++++++++++++------
>  1 file changed, 46 insertions(+), 8 deletions(-)
5fc6947a0355c4a155dd969174f9d0ab83e3b755

This seems to cause a infinite loop with:

./ffmpeg -i 1sec.ogg -af asetnsamples=1:p=0,atrim=end_sample=10240,aresample=8000,asetnsamples=1:p=0,aresample=44100 -bitexact -y smpl12.wav

https://samples.ffmpeg.org/ogg/Vorbis/1sec.ogg

thx

[...]
Paul B Mahol May 18, 2023, 10:19 p.m. UTC | #2
Fixed patch.
Paul B Mahol May 27, 2023, 3:56 p.m. UTC | #3
Will apply soon.
diff mbox series

Patch

From ed6be8443c29ce2246eec572b7342cee00e61925 Mon Sep 17 00:00:00 2001
From: Paul B Mahol <onemda@gmail.com>
Date: Wed, 17 May 2023 09:15:51 +0200
Subject: [PATCH 22/27] avfilter/af_aresample: switch to activate

Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavfilter/af_aresample.c | 54 ++++++++++++++++++++++++++++++++------
 1 file changed, 46 insertions(+), 8 deletions(-)

diff --git a/libavfilter/af_aresample.c b/libavfilter/af_aresample.c
index 971c861d0e..a9fc74b7a3 100644
--- a/libavfilter/af_aresample.c
+++ b/libavfilter/af_aresample.c
@@ -32,6 +32,7 @@ 
 #include "libswresample/swresample.h"
 #include "avfilter.h"
 #include "audio.h"
+#include "filters.h"
 #include "internal.h"
 
 typedef struct AResampleContext {
@@ -41,6 +42,7 @@  typedef struct AResampleContext {
     struct SwrContext *swr;
     int64_t next_pts;
     int more_data;
+    int eof;
 } AResampleContext;
 
 static av_cold int preinit(AVFilterContext *ctx)
@@ -169,7 +171,8 @@  static int config_output(AVFilterLink *outlink)
 
 static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref)
 {
-    AResampleContext *aresample = inlink->dst->priv;
+    AVFilterContext *ctx = inlink->dst;
+    AResampleContext *aresample = ctx->priv;
     const int n_in  = insamplesref->nb_samples;
     int64_t delay;
     int n_out       = n_in * aresample->ratio + 32;
@@ -214,6 +217,7 @@  FF_ENABLE_DEPRECATION_WARNINGS
     if (n_out <= 0) {
         av_frame_free(&outsamplesref);
         av_frame_free(&insamplesref);
+        ff_filter_set_ready(ctx, 100);
         return 0;
     }
 
@@ -260,8 +264,10 @@  static int flush_frame(AVFilterLink *outlink, int final, AVFrame **outsamplesref
 static int request_frame(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
+    AVFilterLink *inlink = ctx->inputs[0];
     AResampleContext *aresample = ctx->priv;
-    int ret;
+    int ret = 0, status;
+    int64_t pts;
 
     // First try to get data from the internal buffers
     if (aresample->more_data) {
@@ -273,19 +279,52 @@  static int request_frame(AVFilterLink *outlink)
     }
     aresample->more_data = 0;
 
+    if (!aresample->eof && ff_inlink_acknowledge_status(inlink, &status, &pts))
+        aresample->eof = 1;
+
     // Second request more data from the input
-    ret = ff_request_frame(ctx->inputs[0]);
+    if (!aresample->eof)
+        FF_FILTER_FORWARD_WANTED(outlink, inlink);
 
     // Third if we hit the end flush
-    if (ret == AVERROR_EOF) {
+    if (aresample->eof) {
         AVFrame *outsamplesref;
 
-        if ((ret = flush_frame(outlink, 1, &outsamplesref)) < 0)
+        if ((ret = flush_frame(outlink, 1, &outsamplesref)) < 0) {
+            if (ret == AVERROR_EOF) {
+                ff_outlink_set_status(outlink, AVERROR_EOF, aresample->next_pts);
+                return 0;
+            }
             return ret;
+        }
 
         return ff_filter_frame(outlink, outsamplesref);
     }
-    return ret;
+
+    ff_filter_set_ready(ctx, 100);
+    return 0;
+}
+
+static int activate(AVFilterContext *ctx)
+{
+    AResampleContext *aresample = ctx->priv;
+    AVFilterLink *inlink = ctx->inputs[0];
+    AVFilterLink *outlink = ctx->outputs[0];
+
+    FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
+
+    if (!aresample->eof && ff_inlink_queued_frames(inlink)) {
+        AVFrame *frame = NULL;
+        int ret;
+
+        ret = ff_inlink_consume_frame(inlink, &frame);
+        if (ret < 0)
+            return ret;
+        if (ret > 0)
+            return filter_frame(inlink, frame);
+    }
+
+    return request_frame(outlink);
 }
 
 static const AVClass *resample_child_class_iterate(void **iter)
@@ -322,7 +361,6 @@  static const AVFilterPad aresample_inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_AUDIO,
-        .filter_frame = filter_frame,
     },
 };
 
@@ -330,7 +368,6 @@  static const AVFilterPad aresample_outputs[] = {
     {
         .name          = "default",
         .config_props  = config_output,
-        .request_frame = request_frame,
         .type          = AVMEDIA_TYPE_AUDIO,
     },
 };
@@ -339,6 +376,7 @@  const AVFilter ff_af_aresample = {
     .name          = "aresample",
     .description   = NULL_IF_CONFIG_SMALL("Resample audio data."),
     .preinit       = preinit,
+    .activate      = activate,
     .uninit        = uninit,
     .priv_size     = sizeof(AResampleContext),
     .priv_class    = &aresample_class,
-- 
2.39.1