diff mbox series

[FFmpeg-devel,11/31] fftools/ffmpeg_filter: stop accessing encoder AVCodecContext

Message ID 20240405161212.26167-11-anton@khirnov.net
State Accepted
Commit bc206ed1b3631801869b1bd4ddb3d3e4dd5f7aef
Headers show
Series [FFmpeg-devel,01/31] lavfi/vf_scale: fix AVOption flags for "size"/"s" | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Anton Khirnov April 5, 2024, 4:11 p.m. UTC
Pass all the necessary value through OutputFilterOptions.

Will allow decoupling filtering from encoding in future commits.
---
 fftools/ffmpeg.h          |  7 +++++++
 fftools/ffmpeg_filter.c   | 22 +++++++++++-----------
 fftools/ffmpeg_mux_init.c |  6 ++++++
 3 files changed, 24 insertions(+), 11 deletions(-)

Comments

Dennis Mungai April 5, 2024, 4:50 p.m. UTC | #1
On Fri, 5 Apr 2024, 19:14 Anton Khirnov, <anton@khirnov.net> wrote:

> Pass all the necessary value through OutputFilterOptions.
>
> Will allow decoupling filtering from encoding in future commits.
> ---
>  fftools/ffmpeg.h          |  7 +++++++
>  fftools/ffmpeg_filter.c   | 22 +++++++++++-----------
>  fftools/ffmpeg_mux_init.c |  6 ++++++
>  3 files changed, 24 insertions(+), 11 deletions(-)
>
> diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
> index d0e896dbe7..598ca2fa96 100644
> --- a/fftools/ffmpeg.h
> +++ b/fftools/ffmpeg.h
> @@ -282,6 +282,13 @@ typedef struct OutputFilterOptions {
>
>      // A combination of OFilterFlags.
>      unsigned            flags;
> +
> +    int                 format;
> +    int                 width;
> +    int                 height;
> +
> +    int                 sample_rate;
> +    AVChannelLayout     ch_layout;
>  } OutputFilterOptions;
>
>  typedef struct InputFilter {
> diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
> index 5661dc960a..3c25d2ed65 100644
> --- a/fftools/ffmpeg_filter.c
> +++ b/fftools/ffmpeg_filter.c
> @@ -782,12 +782,12 @@ int ofilter_bind_ost(OutputFilter *ofilter,
> OutputStream *ost,
>      ofp->ts_offset    = opts->ts_offset;
>      ofp->enc_timebase = opts->output_tb;
>
> -    switch (ost->enc_ctx->codec_type) {
> +    switch (ofilter->type) {
>      case AVMEDIA_TYPE_VIDEO:
> -        ofp->width      = ost->enc_ctx->width;
> -        ofp->height     = ost->enc_ctx->height;
> -        if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
> -            ofp->format = ost->enc_ctx->pix_fmt;
> +        ofp->width      = opts->width;
> +        ofp->height     = opts->height;
> +        if (opts->format != AV_PIX_FMT_NONE) {
> +            ofp->format = opts->format;
>          } else if (opts->pix_fmts)
>              ofp->formats = opts->pix_fmts;
>          else if (opts->enc)
> @@ -812,19 +812,19 @@ int ofilter_bind_ost(OutputFilter *ofilter,
> OutputStream *ost,
>
>          break;
>      case AVMEDIA_TYPE_AUDIO:
> -        if (ost->enc_ctx->sample_fmt != AV_SAMPLE_FMT_NONE) {
> -            ofp->format = ost->enc_ctx->sample_fmt;
> +        if (opts->format != AV_SAMPLE_FMT_NONE) {
> +            ofp->format = opts->format;
>          } else if (opts->enc) {
>              ofp->formats = opts->enc->sample_fmts;
>          }
> -        if (ost->enc_ctx->sample_rate) {
> -            ofp->sample_rate = ost->enc_ctx->sample_rate;
> +        if (opts->sample_rate) {
> +            ofp->sample_rate = opts->sample_rate;
>          } else if (opts->enc) {
>              ofp->sample_rates = opts->enc->supported_samplerates;
>          }
> -        if (ost->enc_ctx->ch_layout.nb_channels) {
> +        if (opts->ch_layout.nb_channels) {
>              int ret = set_channel_layout(ofp, opts->enc ?
> opts->enc->ch_layouts : NULL,
> -                                         &ost->enc_ctx->ch_layout);
> +                                         &opts->ch_layout);
>              if (ret < 0)
>                  return ret;
>          } else if (opts->enc) {
> diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
> index 04642f5c8b..b031cc59d2 100644
> --- a/fftools/ffmpeg_mux_init.c
> +++ b/fftools/ffmpeg_mux_init.c
> @@ -1378,6 +1378,12 @@ static int ost_add(Muxer *mux, const OptionsContext
> *o, enum AVMediaType type,
>          (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
>          OutputFilterOptions opts = {
>              .enc = enc,
> +            .format      = (type == AVMEDIA_TYPE_VIDEO) ?
> +                           ost->enc_ctx->pix_fmt :
> ost->enc_ctx->sample_fmt,
> +            .width       = ost->enc_ctx->width,
> +            .height      = ost->enc_ctx->height,
> +            .sample_rate = ost->enc_ctx->sample_rate,
> +            .ch_layout   = ost->enc_ctx->ch_layout,
>              .output_tb = enc_tb,
>              .ts_offset = mux->of.start_time == AV_NOPTS_VALUE ?
>                           0 : mux->of.start_time,
> --
> 2.43.0
>


Does this imply that down the line, with this and additional patchsets,
that FFmpeg can handle tasks such as stream copy and filtering in the same
invocation?

>
Gyan Doshi April 5, 2024, 4:54 p.m. UTC | #2
On 2024-04-05 10:20 pm, Dennis Mungai wrote:
>
> Does this imply that down the line, with this and additional patchsets,
> that FFmpeg can handle tasks such as stream copy and filtering in the same
> invocation?

What do you mean? If you map a stream twice, you already can do that now.

Regards,
Gyan
Dennis Mungai April 5, 2024, 5:07 p.m. UTC | #3
On Fri, 5 Apr 2024 at 19:54, Gyan Doshi <ffmpeg@gyani.pro> wrote:

>
>
> On 2024-04-05 10:20 pm, Dennis Mungai wrote:
> >
> > Does this imply that down the line, with this and additional patchsets,
> > that FFmpeg can handle tasks such as stream copy and filtering in the
> same
> > invocation?
>
> What do you mean? If you map a stream twice, you already can do that now.
>
> Regards,
> Gyan
>

What of doing the same *without* mapping the stream twice?
A complex filtergraph could have a stream mapped, split and then passed to
a copy codec operation, for example, without needing the double re-map
implied above.
Anton Khirnov April 5, 2024, 5:09 p.m. UTC | #4
Quoting Dennis Mungai (2024-04-05 19:07:19)
> On Fri, 5 Apr 2024 at 19:54, Gyan Doshi <ffmpeg@gyani.pro> wrote:
> 
> >
> >
> > On 2024-04-05 10:20 pm, Dennis Mungai wrote:
> > >
> > > Does this imply that down the line, with this and additional patchsets,
> > > that FFmpeg can handle tasks such as stream copy and filtering in the
> > same
> > > invocation?
> >
> > What do you mean? If you map a stream twice, you already can do that now.
> >
> > Regards,
> > Gyan
> >
> 
> What of doing the same *without* mapping the stream twice?

What's the issue with mapping a stream twice?

> A complex filtergraph could have a stream mapped, split and then passed to
> a copy codec operation, for example, without needing the double re-map
> implied above.

Filtergraphs process decoded frames, streamcopy bypasses them entirely.
None of this affects streamcopy.
diff mbox series

Patch

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index d0e896dbe7..598ca2fa96 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -282,6 +282,13 @@  typedef struct OutputFilterOptions {
 
     // A combination of OFilterFlags.
     unsigned            flags;
+
+    int                 format;
+    int                 width;
+    int                 height;
+
+    int                 sample_rate;
+    AVChannelLayout     ch_layout;
 } OutputFilterOptions;
 
 typedef struct InputFilter {
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 5661dc960a..3c25d2ed65 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -782,12 +782,12 @@  int ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost,
     ofp->ts_offset    = opts->ts_offset;
     ofp->enc_timebase = opts->output_tb;
 
-    switch (ost->enc_ctx->codec_type) {
+    switch (ofilter->type) {
     case AVMEDIA_TYPE_VIDEO:
-        ofp->width      = ost->enc_ctx->width;
-        ofp->height     = ost->enc_ctx->height;
-        if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
-            ofp->format = ost->enc_ctx->pix_fmt;
+        ofp->width      = opts->width;
+        ofp->height     = opts->height;
+        if (opts->format != AV_PIX_FMT_NONE) {
+            ofp->format = opts->format;
         } else if (opts->pix_fmts)
             ofp->formats = opts->pix_fmts;
         else if (opts->enc)
@@ -812,19 +812,19 @@  int ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost,
 
         break;
     case AVMEDIA_TYPE_AUDIO:
-        if (ost->enc_ctx->sample_fmt != AV_SAMPLE_FMT_NONE) {
-            ofp->format = ost->enc_ctx->sample_fmt;
+        if (opts->format != AV_SAMPLE_FMT_NONE) {
+            ofp->format = opts->format;
         } else if (opts->enc) {
             ofp->formats = opts->enc->sample_fmts;
         }
-        if (ost->enc_ctx->sample_rate) {
-            ofp->sample_rate = ost->enc_ctx->sample_rate;
+        if (opts->sample_rate) {
+            ofp->sample_rate = opts->sample_rate;
         } else if (opts->enc) {
             ofp->sample_rates = opts->enc->supported_samplerates;
         }
-        if (ost->enc_ctx->ch_layout.nb_channels) {
+        if (opts->ch_layout.nb_channels) {
             int ret = set_channel_layout(ofp, opts->enc ? opts->enc->ch_layouts : NULL,
-                                         &ost->enc_ctx->ch_layout);
+                                         &opts->ch_layout);
             if (ret < 0)
                 return ret;
         } else if (opts->enc) {
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 04642f5c8b..b031cc59d2 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -1378,6 +1378,12 @@  static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
         (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
         OutputFilterOptions opts = {
             .enc = enc,
+            .format      = (type == AVMEDIA_TYPE_VIDEO) ?
+                           ost->enc_ctx->pix_fmt : ost->enc_ctx->sample_fmt,
+            .width       = ost->enc_ctx->width,
+            .height      = ost->enc_ctx->height,
+            .sample_rate = ost->enc_ctx->sample_rate,
+            .ch_layout   = ost->enc_ctx->ch_layout,
             .output_tb = enc_tb,
             .ts_offset = mux->of.start_time == AV_NOPTS_VALUE ?
                          0 : mux->of.start_time,