diff mbox series

[FFmpeg-devel,1/2] avfilter/src_movie: switch to activate()

Message ID 20220402120623.142534-1-onemda@gmail.com
State New
Headers show
Series [FFmpeg-devel,1/2] avfilter/src_movie: switch to activate() | expand

Commit Message

Paul B Mahol April 2, 2022, 12:06 p.m. UTC
Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavfilter/src_movie.c | 90 +++++++++++++++++++++++++----------------
 1 file changed, 56 insertions(+), 34 deletions(-)

Comments

Paul B Mahol Dec. 12, 2022, 8:08 p.m. UTC | #1
Can I ask some knowledgeable people to comment why this patch
have issues?

On 4/2/22, Paul B Mahol <onemda@gmail.com> wrote:
> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> ---
>  libavfilter/src_movie.c | 90 +++++++++++++++++++++++++----------------
>  1 file changed, 56 insertions(+), 34 deletions(-)
>
> diff --git a/libavfilter/src_movie.c b/libavfilter/src_movie.c
> index 711854c23c..bc7b0d37af 100644
> --- a/libavfilter/src_movie.c
> +++ b/libavfilter/src_movie.c
> @@ -46,6 +46,7 @@
>
>  #include "audio.h"
>  #include "avfilter.h"
> +#include "filters.h"
>  #include "formats.h"
>  #include "internal.h"
>  #include "video.h"
> @@ -55,6 +56,7 @@ typedef struct MovieStream {
>      AVCodecContext *codec_ctx;
>      int64_t discontinuity_threshold;
>      int64_t last_pts;
> +    int got_eof;
>  } MovieStream;
>
>  typedef struct MovieContext {
> @@ -70,6 +72,8 @@ typedef struct MovieContext {
>      int64_t discontinuity_threshold;
>      int64_t ts_offset;
>      int dec_threads;
> +    int got_eagain;
> +    int got_wanted;
>
>      AVFormatContext *format_ctx;
>
> @@ -100,7 +104,6 @@ static const AVOption movie_options[]= {
>  };
>
>  static int movie_config_output_props(AVFilterLink *outlink);
> -static int movie_request_frame(AVFilterLink *outlink);
>
>  static AVStream *find_stream(void *log, AVFormatContext *avf, const char
> *spec)
>  {
> @@ -314,7 +317,6 @@ static av_cold int movie_common_init(AVFilterContext
> *ctx)
>          if (!pad.name)
>              return AVERROR(ENOMEM);
>          pad.config_props  = movie_config_output_props;
> -        pad.request_frame = movie_request_frame;
>          if ((ret = ff_append_outpad_free_name(ctx, &pad)) < 0)
>              return ret;
>          if ( movie->st[i].st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO
> &&
> @@ -554,47 +556,67 @@ static int movie_push_frame(AVFilterContext *ctx,
> unsigned out_id)
>      return 0;
>  }
>
> -static int movie_request_frame(AVFilterLink *outlink)
> +static int activate(AVFilterContext *ctx)
>  {
> -    AVFilterContext *ctx = outlink->src;
>      MovieContext  *movie = ctx->priv;
> -    unsigned out_id = FF_OUTLINK_IDX(outlink);
> +    int ret = 0;
> +
> +    /* check all decoders for available output */
> +    for (int i = 0; i < ctx->nb_outputs; i++) {
> +        if (movie->st[i].got_eof)
> +            continue;
> +
> +        if (ff_outlink_frame_wanted(ctx->outputs[i]))
> +            movie->got_wanted++;
>
> -    while (1) {
> -        int got_eagain = 0, got_eof = 0;
> -        int ret = 0;
> +        ret = movie_push_frame(ctx, i);
> +        if (ret == AVERROR(EAGAIN))
> +            movie->got_eagain++;
> +        else if (ret == AVERROR_EOF)
> +            movie->st[i].got_eof = 1;
> +        else if (ret < 0)
> +            return ret;
> +    }
> +
> +    if (movie->got_eagain || movie->got_wanted) {
> +        /* all decoders require more input -> read a new packet */
> +        movie->got_eagain = 0;
> +        movie->got_wanted = 0;
> +        ret = movie_decode_packet(ctx);
> +        if (ret < 0 && ret != AVERROR(EAGAIN))
> +            return ret;
> +        ff_filter_set_ready(ctx, 100);
> +        return 0;
> +    } else {
> +        int nb_eofs = 0;
>
> -        /* check all decoders for available output */
>          for (int i = 0; i < ctx->nb_outputs; i++) {
> -            ret = movie_push_frame(ctx, i);
> -            if (ret == AVERROR(EAGAIN))
> -                got_eagain++;
> -            else if (ret == AVERROR_EOF)
> -                got_eof++;
> -            else if (ret < 0)
> -                return ret;
> -            else if (i == out_id)
> -                return 0;
> +            if (movie->st[i].got_eof) {
> +                if (movie->loop_count == 1)
> +                    ff_outlink_set_status(ctx->outputs[i], AVERROR_EOF,
> movie->st[i].last_pts);
> +                nb_eofs++;
> +            }
> +        }
> +
> +        if (nb_eofs != ctx->nb_outputs) {
> +            ff_filter_set_ready(ctx, 100);
> +            return 0;
>          }
>
> -        if (got_eagain) {
> -            /* all decoders require more input -> read a new packet */
> -            ret = movie_decode_packet(ctx);
> +        if (movie->loop_count != 1) {
> +            ret = rewind_file(ctx);
>              if (ret < 0)
>                  return ret;
> -        } else if (got_eof) {
> -            /* all decoders flushed */
> -            if (movie->loop_count != 1) {
> -                ret = rewind_file(ctx);
> -                if (ret < 0)
> -                    return ret;
> -                movie->loop_count -= movie->loop_count > 1;
> -                av_log(ctx, AV_LOG_VERBOSE, "Stream finished,
> looping.\n");
> -                continue;
> -            }
> -            return AVERROR_EOF;
> +            movie->loop_count -= movie->loop_count > 1;
> +            av_log(ctx, AV_LOG_VERBOSE, "Stream finished, looping.\n");
> +            for (int i = 0; i < ctx->nb_outputs; i++)
> +                movie->st[i].got_eof = 0;
> +            ff_filter_set_ready(ctx, 100);
>          }
> +        return 0;
>      }
> +
> +    return FFERROR_NOT_READY;
>  }
>
>  static int process_command(AVFilterContext *ctx, const char *cmd, const
> char *args,
> @@ -651,7 +673,7 @@ const AVFilter ff_avsrc_movie = {
>      .init          = movie_common_init,
>      .uninit        = movie_uninit,
>      FILTER_QUERY_FUNC(movie_query_formats),
> -
> +    .activate      = activate,
>      .inputs    = NULL,
>      .outputs   = NULL,
>      .flags     = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
> @@ -670,7 +692,7 @@ const AVFilter ff_avsrc_amovie = {
>      .init          = movie_common_init,
>      .uninit        = movie_uninit,
>      FILTER_QUERY_FUNC(movie_query_formats),
> -
> +    .activate      = activate,
>      .inputs     = NULL,
>      .outputs    = NULL,
>      .flags      = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
> --
> 2.35.1
>
>
Paul B Mahol May 10, 2023, 2:47 p.m. UTC | #2
Will apply as it now works.
Paul B Mahol May 12, 2023, 10:55 a.m. UTC | #3
On Wed, May 10, 2023 at 4:47 PM Paul B Mahol <onemda@gmail.com> wrote:

>
> Will apply as it now works.
>

last time for comments
diff mbox series

Patch

diff --git a/libavfilter/src_movie.c b/libavfilter/src_movie.c
index 711854c23c..bc7b0d37af 100644
--- a/libavfilter/src_movie.c
+++ b/libavfilter/src_movie.c
@@ -46,6 +46,7 @@ 
 
 #include "audio.h"
 #include "avfilter.h"
+#include "filters.h"
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
@@ -55,6 +56,7 @@  typedef struct MovieStream {
     AVCodecContext *codec_ctx;
     int64_t discontinuity_threshold;
     int64_t last_pts;
+    int got_eof;
 } MovieStream;
 
 typedef struct MovieContext {
@@ -70,6 +72,8 @@  typedef struct MovieContext {
     int64_t discontinuity_threshold;
     int64_t ts_offset;
     int dec_threads;
+    int got_eagain;
+    int got_wanted;
 
     AVFormatContext *format_ctx;
 
@@ -100,7 +104,6 @@  static const AVOption movie_options[]= {
 };
 
 static int movie_config_output_props(AVFilterLink *outlink);
-static int movie_request_frame(AVFilterLink *outlink);
 
 static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
 {
@@ -314,7 +317,6 @@  static av_cold int movie_common_init(AVFilterContext *ctx)
         if (!pad.name)
             return AVERROR(ENOMEM);
         pad.config_props  = movie_config_output_props;
-        pad.request_frame = movie_request_frame;
         if ((ret = ff_append_outpad_free_name(ctx, &pad)) < 0)
             return ret;
         if ( movie->st[i].st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
@@ -554,47 +556,67 @@  static int movie_push_frame(AVFilterContext *ctx, unsigned out_id)
     return 0;
 }
 
-static int movie_request_frame(AVFilterLink *outlink)
+static int activate(AVFilterContext *ctx)
 {
-    AVFilterContext *ctx = outlink->src;
     MovieContext  *movie = ctx->priv;
-    unsigned out_id = FF_OUTLINK_IDX(outlink);
+    int ret = 0;
+
+    /* check all decoders for available output */
+    for (int i = 0; i < ctx->nb_outputs; i++) {
+        if (movie->st[i].got_eof)
+            continue;
+
+        if (ff_outlink_frame_wanted(ctx->outputs[i]))
+            movie->got_wanted++;
 
-    while (1) {
-        int got_eagain = 0, got_eof = 0;
-        int ret = 0;
+        ret = movie_push_frame(ctx, i);
+        if (ret == AVERROR(EAGAIN))
+            movie->got_eagain++;
+        else if (ret == AVERROR_EOF)
+            movie->st[i].got_eof = 1;
+        else if (ret < 0)
+            return ret;
+    }
+
+    if (movie->got_eagain || movie->got_wanted) {
+        /* all decoders require more input -> read a new packet */
+        movie->got_eagain = 0;
+        movie->got_wanted = 0;
+        ret = movie_decode_packet(ctx);
+        if (ret < 0 && ret != AVERROR(EAGAIN))
+            return ret;
+        ff_filter_set_ready(ctx, 100);
+        return 0;
+    } else {
+        int nb_eofs = 0;
 
-        /* check all decoders for available output */
         for (int i = 0; i < ctx->nb_outputs; i++) {
-            ret = movie_push_frame(ctx, i);
-            if (ret == AVERROR(EAGAIN))
-                got_eagain++;
-            else if (ret == AVERROR_EOF)
-                got_eof++;
-            else if (ret < 0)
-                return ret;
-            else if (i == out_id)
-                return 0;
+            if (movie->st[i].got_eof) {
+                if (movie->loop_count == 1)
+                    ff_outlink_set_status(ctx->outputs[i], AVERROR_EOF, movie->st[i].last_pts);
+                nb_eofs++;
+            }
+        }
+
+        if (nb_eofs != ctx->nb_outputs) {
+            ff_filter_set_ready(ctx, 100);
+            return 0;
         }
 
-        if (got_eagain) {
-            /* all decoders require more input -> read a new packet */
-            ret = movie_decode_packet(ctx);
+        if (movie->loop_count != 1) {
+            ret = rewind_file(ctx);
             if (ret < 0)
                 return ret;
-        } else if (got_eof) {
-            /* all decoders flushed */
-            if (movie->loop_count != 1) {
-                ret = rewind_file(ctx);
-                if (ret < 0)
-                    return ret;
-                movie->loop_count -= movie->loop_count > 1;
-                av_log(ctx, AV_LOG_VERBOSE, "Stream finished, looping.\n");
-                continue;
-            }
-            return AVERROR_EOF;
+            movie->loop_count -= movie->loop_count > 1;
+            av_log(ctx, AV_LOG_VERBOSE, "Stream finished, looping.\n");
+            for (int i = 0; i < ctx->nb_outputs; i++)
+                movie->st[i].got_eof = 0;
+            ff_filter_set_ready(ctx, 100);
         }
+        return 0;
     }
+
+    return FFERROR_NOT_READY;
 }
 
 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
@@ -651,7 +673,7 @@  const AVFilter ff_avsrc_movie = {
     .init          = movie_common_init,
     .uninit        = movie_uninit,
     FILTER_QUERY_FUNC(movie_query_formats),
-
+    .activate      = activate,
     .inputs    = NULL,
     .outputs   = NULL,
     .flags     = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
@@ -670,7 +692,7 @@  const AVFilter ff_avsrc_amovie = {
     .init          = movie_common_init,
     .uninit        = movie_uninit,
     FILTER_QUERY_FUNC(movie_query_formats),
-
+    .activate      = activate,
     .inputs     = NULL,
     .outputs    = NULL,
     .flags      = AVFILTER_FLAG_DYNAMIC_OUTPUTS,