[FFmpeg-devel] [PATCH] fftools/ffmpeg: use an int for nb_threads

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Sat Feb 15 03:28:06 EET 2025


James Almer:
> Removes unnecessary allocations.
> 
> Signed-off-by: James Almer <jamrial at gmail.com>
> ---
>  fftools/ffmpeg.h          |  2 +-
>  fftools/ffmpeg_filter.c   | 16 ++++++----------
>  fftools/ffmpeg_mux_init.c |  5 ++---
>  3 files changed, 9 insertions(+), 14 deletions(-)
> 
> diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
> index 9439be0f41..6cc0da05a0 100644
> --- a/fftools/ffmpeg.h
> +++ b/fftools/ffmpeg.h
> @@ -316,7 +316,7 @@ typedef struct OutputFilterOptions {
>      AVDictionary       *sws_opts;
>      AVDictionary       *swr_opts;
>  
> -    const char         *nb_threads;
> +    int64_t             nb_threads;
>  
>      // A combination of OFilterFlags.
>      unsigned            flags;
> diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
> index a0d04fd76f..800e2a3f06 100644
> --- a/fftools/ffmpeg_filter.c
> +++ b/fftools/ffmpeg_filter.c
> @@ -60,7 +60,7 @@ typedef struct FilterGraphPriv {
>  
>      const char      *graph_desc;
>  
> -    char            *nb_threads;
> +    int              nb_threads;
>  
>      // frame for temporarily holding output from the filtergraph
>      AVFrame         *frame;
> @@ -1042,7 +1042,6 @@ void fg_free(FilterGraph **pfg)
>      }
>      av_freep(&fg->outputs);
>      av_freep(&fgp->graph_desc);
> -    av_freep(&fgp->nb_threads);
>  
>      av_frame_free(&fgp->frame);
>      av_frame_free(&fgp->frame_enc);
> @@ -1097,6 +1096,7 @@ int fg_create(FilterGraph **pfg, char *graph_desc, Scheduler *sch)
>      fg->class       = &fg_class;
>      fgp->graph_desc = graph_desc;
>      fgp->disable_conversions = !auto_conversion_filters;
> +    fgp->nb_threads          = -1;
>      fgp->sch                 = sch;
>  
>      snprintf(fgp->log_name, sizeof(fgp->log_name), "fc#%d", fg->index);
> @@ -1247,12 +1247,8 @@ int fg_create_simple(FilterGraph **pfg,
>      if (ret < 0)
>          return ret;
>  
> -    if (opts->nb_threads) {
> -        av_freep(&fgp->nb_threads);
> -        fgp->nb_threads = av_strdup(opts->nb_threads);
> -        if (!fgp->nb_threads)
> -            return AVERROR(ENOMEM);
> -    }
> +    if (opts->nb_threads >= 0)
> +        fgp->nb_threads = opts->nb_threads;
>  
>      return 0;
>  }
> @@ -1936,8 +1932,8 @@ static int configure_filtergraph(FilterGraph *fg, FilterGraphThread *fgt)
>              ret = av_opt_set(fgt->graph, "threads", filter_nbthreads, 0);
>              if (ret < 0)
>                  goto fail;
> -        } else if (fgp->nb_threads) {
> -            ret = av_opt_set(fgt->graph, "threads", fgp->nb_threads, 0);
> +        } else if (fgp->nb_threads >= 0) {
> +            ret = av_opt_set_int(fgt->graph, "threads", fgp->nb_threads, 0);
>              if (ret < 0)
>                  return ret;
>          }
> diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
> index 944176ca5d..d212a1f326 100644
> --- a/fftools/ffmpeg_mux_init.c
> +++ b/fftools/ffmpeg_mux_init.c
> @@ -930,7 +930,7 @@ ost_bind_filter(const Muxer *mux, MuxStream *ms, OutputFilter *ofilter,
>          .ts_offset        = mux->of.start_time == AV_NOPTS_VALUE ?
>                              0 : mux->of.start_time,
>          .vs               = vs,
> -
> +        .nb_threads       = -1,
>          .flags = OFILTER_FLAG_DISABLE_CONVERT * !!keep_pix_fmt |
>                   OFILTER_FLAG_AUTOSCALE       * !!autoscale    |
>                   OFILTER_FLAG_AUDIO_24BIT * !!(av_get_exact_bits_per_sample(enc_ctx->codec_id) == 24),
> @@ -982,7 +982,7 @@ ost_bind_filter(const Muxer *mux, MuxStream *ms, OutputFilter *ofilter,
>      }
>  
>      if (threads_manual) {
> -        ret = av_opt_get(enc_ctx, "threads", 0, (uint8_t**)&opts.nb_threads);
> +        ret = av_opt_get_int(enc_ctx, "threads", 0, &opts.nb_threads);
>          if (ret < 0)
>              return ret;
>      }
> @@ -1002,7 +1002,6 @@ ost_bind_filter(const Muxer *mux, MuxStream *ms, OutputFilter *ofilter,
>              ost->filter = ost->fg_simple->outputs[0];
>  
>      }
> -    av_freep(&opts.nb_threads);
>      if (ret < 0)
>          return ret;
>  

What about "auto"? I thought that was a thing. Or do I misremember?

- Andreas



More information about the ffmpeg-devel mailing list