diff mbox series

[FFmpeg-devel,2/2] ffmpeg_opt: consider HW acceleration method when selecting decoder

Message ID 20210922074207.19451-2-haihao.xiang@intel.com
State New
Headers show
Series [FFmpeg-devel,1/2] ffmpeg_opt: select a decoder after getting values for per-stream hwdec options | expand

Commit Message

Xiang, Haihao Sept. 22, 2021, 7:42 a.m. UTC
Usually a HW decoder is expected when user specifies a HW acceleration
method via -hwaccel option, however the current implementation doesn't
take HW acceleration method into account, it is possible to select a SW
decoder.

For example:

$> ffmpeg -hwaccel vaapi -i av1.ivf -f null -
...
Stream #0:0 -> #0:0 (av1 (libdav1d) -> wrapped_avframe (native))

libdav1d is selected in this case even if vaapi is specified.

After applying this patch, the native av1 decoder (with vaapi support)
is selected.
...
Stream #0:0 -> #0:0 (av1 (native) -> wrapped_avframe (native))
---
 fftools/ffmpeg_opt.c | 44 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 43 insertions(+), 1 deletion(-)

Comments

Xiang, Haihao Oct. 8, 2021, 6:06 a.m. UTC | #1
On Wed, 2021-09-22 at 15:42 +0800, Haihao Xiang wrote:
> Usually a HW decoder is expected when user specifies a HW acceleration
> method via -hwaccel option, however the current implementation doesn't
> take HW acceleration method into account, it is possible to select a SW
> decoder.
> 
> For example:
> 
> $> ffmpeg -hwaccel vaapi -i av1.ivf -f null -
> ...
> Stream #0:0 -> #0:0 (av1 (libdav1d) -> wrapped_avframe (native))
> 
> libdav1d is selected in this case even if vaapi is specified.
> 
> After applying this patch, the native av1 decoder (with vaapi support)
> is selected.
> ...
> Stream #0:0 -> #0:0 (av1 (native) -> wrapped_avframe (native))

Any comment for this patchset? We may simplify the command line using HW
acceleration method after applying this patchset.

Thanks
Haihao


> ---
>  fftools/ffmpeg_opt.c | 44 +++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 43 insertions(+), 1 deletion(-)
> 
> diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
> index 2d9a8a2ea0..914078add9 100644
> --- a/fftools/ffmpeg_opt.c
> +++ b/fftools/ffmpeg_opt.c
> @@ -804,6 +804,48 @@ static const AVCodec *choose_decoder(OptionsContext *o,
> AVFormatContext *s, AVSt
>          return avcodec_find_decoder(st->codecpar->codec_id);
>  }
>  
> +static const AVCodec *choose_decoder2(InputStream *ist, OptionsContext *o,
> AVFormatContext *s, AVStream *st)
> +{
> +    char *codec_name = NULL;
> +
> +    MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
> +    if (codec_name) {
> +        const AVCodec *codec = find_codec_or_die(codec_name, st->codecpar-
> >codec_type, 0);
> +        st->codecpar->codec_id = codec->id;
> +        if (recast_media && st->codecpar->codec_type != codec->type)
> +            st->codecpar->codec_type = codec->type;
> +        return codec;
> +    } else {
> +        if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
> +            ist->hwaccel_id == HWACCEL_GENERIC &&
> +            ist->hwaccel_device_type != AV_HWDEVICE_TYPE_NONE) {
> +            const AVCodec *p;
> +            void *i = 0;
> +
> +            while ((p = av_codec_iterate(&i))) {
> +                int j;
> +
> +                if (p->id != st->codecpar->codec_id ||
> +                    !av_codec_is_decoder(p) ||
> +                    !avcodec_get_hw_config(p, 0))
> +                    continue;
> +
> +                for (j = 0; ;j++) {
> +                    const AVCodecHWConfig *config = avcodec_get_hw_config(p,
> j);
> +
> +                    if (!config)
> +                        break;
> +
> +                    if (config->device_type == ist->hwaccel_device_type)
> +                        return p;
> +                }
> +            }
> +        }
> +
> +        return avcodec_find_decoder(st->codecpar->codec_id);
> +    }
> +}
> +
>  /* Add all the streams from the given input file to the global
>   * list of input streams. */
>  static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
> @@ -932,7 +974,7 @@ static void add_input_streams(OptionsContext *o,
> AVFormatContext *ic)
>              ist->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
>          }
>  
> -        ist->dec = choose_decoder(o, ic, st);
> +        ist->dec = choose_decoder2(ist, o, ic, st);
>          ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st-
> >codecpar->codec_id, ic, st, ist->dec);
>  
>          ist->reinit_filters = -1;
Xiang, Haihao Nov. 16, 2021, 2:21 a.m. UTC | #2
> On Wed, 2021-09-22 at 15:42 +0800, Haihao Xiang wrote:
> > Usually a HW decoder is expected when user specifies a HW acceleration
> > method via -hwaccel option, however the current implementation doesn't
> > take HW acceleration method into account, it is possible to select a SW
> > decoder.
> > 
> > For example:
> > 
> > $> ffmpeg -hwaccel vaapi -i av1.ivf -f null -
> > ...
> > Stream #0:0 -> #0:0 (av1 (libdav1d) -> wrapped_avframe (native))
> > 
> > libdav1d is selected in this case even if vaapi is specified.
> > 
> > After applying this patch, the native av1 decoder (with vaapi support)
> > is selected.
> > ...
> > Stream #0:0 -> #0:0 (av1 (native) -> wrapped_avframe (native))
> 
> Any comment for this patchset? We may simplify the command line using HW
> acceleration method after applying this patchset.

Ping again. 

According to the help string for -hwaccel option, HW accelerated decoding is
used if -hwaccel arg is specified. However in the current code, hwaccel is not
taken into account when choosing a codec for the video stream, (see 
https://github.com/FFmpeg/FFmpeg/blob/master/fftools/ffmpeg_opt.c#L792-L805).

For example, libdav1d is selected (if libdav1d is available) when running the
command below because libdav1d has higher priority than the native av1 decoder
in the current FFmpeg. 

$> ffmpeg -hwaccel vaapi -i av1.ivf -f null -

(Note `ffmpeg -hwaccel vaapi -i input.h264 -f null -` works as expected, the
native h264 decoder is selected)

This patch enabled the automatic codec selection for hw accelerated decoding,
-hwaccel option can work as expected after applying this patch. 

Thanks
Haihao


> > ---
> >  fftools/ffmpeg_opt.c | 44 +++++++++++++++++++++++++++++++++++++++++++-
> >  1 file changed, 43 insertions(+), 1 deletion(-)
> > 
> > diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
> > index 2d9a8a2ea0..914078add9 100644
> > --- a/fftools/ffmpeg_opt.c
> > +++ b/fftools/ffmpeg_opt.c
> > @@ -804,6 +804,48 @@ static const AVCodec *choose_decoder(OptionsContext *o,
> > AVFormatContext *s, AVSt
> >          return avcodec_find_decoder(st->codecpar->codec_id);
> >  }
> >  
> > +static const AVCodec *choose_decoder2(InputStream *ist, OptionsContext *o,
> > AVFormatContext *s, AVStream *st)
> > +{
> > +    char *codec_name = NULL;
> > +
> > +    MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
> > +    if (codec_name) {
> > +        const AVCodec *codec = find_codec_or_die(codec_name, st->codecpar-
> > > codec_type, 0);
> > 
> > +        st->codecpar->codec_id = codec->id;
> > +        if (recast_media && st->codecpar->codec_type != codec->type)
> > +            st->codecpar->codec_type = codec->type;
> > +        return codec;
> > +    } else {
> > +        if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
> > +            ist->hwaccel_id == HWACCEL_GENERIC &&
> > +            ist->hwaccel_device_type != AV_HWDEVICE_TYPE_NONE) {
> > +            const AVCodec *p;
> > +            void *i = 0;
> > +
> > +            while ((p = av_codec_iterate(&i))) {
> > +                int j;
> > +
> > +                if (p->id != st->codecpar->codec_id ||
> > +                    !av_codec_is_decoder(p) ||
> > +                    !avcodec_get_hw_config(p, 0))
> > +                    continue;
> > +
> > +                for (j = 0; ;j++) {
> > +                    const AVCodecHWConfig *config =
> > avcodec_get_hw_config(p,
> > j);
> > +
> > +                    if (!config)
> > +                        break;
> > +
> > +                    if (config->device_type == ist->hwaccel_device_type)
> > +                        return p;
> > +                }
> > +            }
> > +        }
> > +
> > +        return avcodec_find_decoder(st->codecpar->codec_id);
> > +    }
> > +}
> > +
> >  /* Add all the streams from the given input file to the global
> >   * list of input streams. */
> >  static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
> > @@ -932,7 +974,7 @@ static void add_input_streams(OptionsContext *o,
> > AVFormatContext *ic)
> >              ist->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
> >          }
> >  
> > -        ist->dec = choose_decoder(o, ic, st);
> > +        ist->dec = choose_decoder2(ist, o, ic, st);
> >          ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st-
> > > codecpar->codec_id, ic, st, ist->dec);
> > 
> >  
> >          ist->reinit_filters = -1;
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
Xiang, Haihao Jan. 5, 2022, 5:50 a.m. UTC | #3
On Tue, 2021-11-16 at 02:21 +0000, Xiang, Haihao wrote:
> > On Wed, 2021-09-22 at 15:42 +0800, Haihao Xiang wrote:
> > > Usually a HW decoder is expected when user specifies a HW acceleration
> > > method via -hwaccel option, however the current implementation doesn't
> > > take HW acceleration method into account, it is possible to select a SW
> > > decoder.
> > > 
> > > For example:
> > > 
> > > $> ffmpeg -hwaccel vaapi -i av1.ivf -f null -
> > > ...
> > > Stream #0:0 -> #0:0 (av1 (libdav1d) -> wrapped_avframe (native))
> > > 
> > > libdav1d is selected in this case even if vaapi is specified.
> > > 
> > > After applying this patch, the native av1 decoder (with vaapi support)
> > > is selected.
> > > ...
> > > Stream #0:0 -> #0:0 (av1 (native) -> wrapped_avframe (native))
> > 
> > Any comment for this patchset? We may simplify the command line using HW
> > acceleration method after applying this patchset.
> 
> Ping again. 
> 
> According to the help string for -hwaccel option, HW accelerated decoding is
> used if -hwaccel arg is specified. However in the current code, hwaccel is not
> taken into account when choosing a codec for the video stream, (see 
> https://github.com/FFmpeg/FFmpeg/blob/master/fftools/ffmpeg_opt.c#L792-L805).
> 
> For example, libdav1d is selected (if libdav1d is available) when running the
> command below because libdav1d has higher priority than the native av1 decoder
> in the current FFmpeg. 
> 
> $> ffmpeg -hwaccel vaapi -i av1.ivf -f null -
> 
> (Note `ffmpeg -hwaccel vaapi -i input.h264 -f null -` works as expected, the
> native h264 decoder is selected)
> 
> This patch enabled the automatic codec selection for hw accelerated decoding,
> -hwaccel option can work as expected after applying this patch. 
> 

Hi Mark

Do you have any comment for automatically codec selection for hw accelerated
decoding ?

Thanks
Haihao


> > > ---
> > >  fftools/ffmpeg_opt.c | 44 +++++++++++++++++++++++++++++++++++++++++++-
> > >  1 file changed, 43 insertions(+), 1 deletion(-)
> > > 
> > > diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
> > > index 2d9a8a2ea0..914078add9 100644
> > > --- a/fftools/ffmpeg_opt.c
> > > +++ b/fftools/ffmpeg_opt.c
> > > @@ -804,6 +804,48 @@ static const AVCodec *choose_decoder(OptionsContext
> > > *o,
> > > AVFormatContext *s, AVSt
> > >          return avcodec_find_decoder(st->codecpar->codec_id);
> > >  }
> > >  
> > > +static const AVCodec *choose_decoder2(InputStream *ist, OptionsContext
> > > *o,
> > > AVFormatContext *s, AVStream *st)
> > > +{
> > > +    char *codec_name = NULL;
> > > +
> > > +    MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
> > > +    if (codec_name) {
> > > +        const AVCodec *codec = find_codec_or_die(codec_name, st-
> > > >codecpar-
> > > > codec_type, 0);
> > > 
> > > +        st->codecpar->codec_id = codec->id;
> > > +        if (recast_media && st->codecpar->codec_type != codec->type)
> > > +            st->codecpar->codec_type = codec->type;
> > > +        return codec;
> > > +    } else {
> > > +        if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
> > > +            ist->hwaccel_id == HWACCEL_GENERIC &&
> > > +            ist->hwaccel_device_type != AV_HWDEVICE_TYPE_NONE) {
> > > +            const AVCodec *p;
> > > +            void *i = 0;
> > > +
> > > +            while ((p = av_codec_iterate(&i))) {
> > > +                int j;
> > > +
> > > +                if (p->id != st->codecpar->codec_id ||
> > > +                    !av_codec_is_decoder(p) ||
> > > +                    !avcodec_get_hw_config(p, 0))
> > > +                    continue;
> > > +
> > > +                for (j = 0; ;j++) {
> > > +                    const AVCodecHWConfig *config =
> > > avcodec_get_hw_config(p,
> > > j);
> > > +
> > > +                    if (!config)
> > > +                        break;
> > > +
> > > +                    if (config->device_type == ist->hwaccel_device_type)
> > > +                        return p;
> > > +                }
> > > +            }
> > > +        }
> > > +
> > > +        return avcodec_find_decoder(st->codecpar->codec_id);
> > > +    }
> > > +}
> > > +
> > >  /* Add all the streams from the given input file to the global
> > >   * list of input streams. */
> > >  static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
> > > @@ -932,7 +974,7 @@ static void add_input_streams(OptionsContext *o,
> > > AVFormatContext *ic)
> > >              ist->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
> > >          }
> > >  
> > > -        ist->dec = choose_decoder(o, ic, st);
> > > +        ist->dec = choose_decoder2(ist, o, ic, st);
> > >          ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st-
> > > > codecpar->codec_id, ic, st, ist->dec);
> > > 
> > >  
> > >          ist->reinit_filters = -1;
> > 
> > _______________________________________________
> > ffmpeg-devel mailing list
> > ffmpeg-devel@ffmpeg.org
> > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> > 
> > To unsubscribe, visit link above, or email
> > ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
diff mbox series

Patch

diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 2d9a8a2ea0..914078add9 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -804,6 +804,48 @@  static const AVCodec *choose_decoder(OptionsContext *o, AVFormatContext *s, AVSt
         return avcodec_find_decoder(st->codecpar->codec_id);
 }
 
+static const AVCodec *choose_decoder2(InputStream *ist, OptionsContext *o, AVFormatContext *s, AVStream *st)
+{
+    char *codec_name = NULL;
+
+    MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
+    if (codec_name) {
+        const AVCodec *codec = find_codec_or_die(codec_name, st->codecpar->codec_type, 0);
+        st->codecpar->codec_id = codec->id;
+        if (recast_media && st->codecpar->codec_type != codec->type)
+            st->codecpar->codec_type = codec->type;
+        return codec;
+    } else {
+        if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
+            ist->hwaccel_id == HWACCEL_GENERIC &&
+            ist->hwaccel_device_type != AV_HWDEVICE_TYPE_NONE) {
+            const AVCodec *p;
+            void *i = 0;
+
+            while ((p = av_codec_iterate(&i))) {
+                int j;
+
+                if (p->id != st->codecpar->codec_id ||
+                    !av_codec_is_decoder(p) ||
+                    !avcodec_get_hw_config(p, 0))
+                    continue;
+
+                for (j = 0; ;j++) {
+                    const AVCodecHWConfig *config = avcodec_get_hw_config(p, j);
+
+                    if (!config)
+                        break;
+
+                    if (config->device_type == ist->hwaccel_device_type)
+                        return p;
+                }
+            }
+        }
+
+        return avcodec_find_decoder(st->codecpar->codec_id);
+    }
+}
+
 /* Add all the streams from the given input file to the global
  * list of input streams. */
 static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
@@ -932,7 +974,7 @@  static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
             ist->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
         }
 
-        ist->dec = choose_decoder(o, ic, st);
+        ist->dec = choose_decoder2(ist, o, ic, st);
         ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id, ic, st, ist->dec);
 
         ist->reinit_filters = -1;