See this thread on libav-user. Basically after listing the hw accelerated codecs you can try to lookup the appropriate decoder with avcodec_find_decoder_by_name (since the AVHWAccel struct has the name field) and then use that for decoding. But then you need to know the codec upfront. If you use avformat_open_input
then you may simply try to find a matching hw accelerated decoder by the codec id from the stream info, then open the hw accelerated codec by name and use that.
Update, since I got downvoted
To provide a working example of this, I have an ffmpeg installation from homebrew, that lists videotoolbox (which is a HW accelerated codec) via ffmpeg -encoders | grep h264
:
V..... h264_videotoolbox VideoToolbox H.264 Encoder (codec h264)
And the following snippet also finds it:
extern "C"
{
#include <libavcodec/avcodec.h>
}
int main(int argc, char** argv)
{
auto *codec = avcodec_find_encoder_by_name("h264_videotoolbox");
if (codec)
{
return 0;
}
return 1;
}
Moreover, if you check what avcodec_find_encoder_by_name
/avcodec_find_encoder_by_name
does, it is visible that it basically iterates the whole codec list just by applying a filter to distinguish encoders/decoders:
static AVCodec *find_codec_by_name(const char *name, int (*x)(const AVCodec *))
{
void *i = 0;
const AVCodec *p;
if (!name)
return NULL;
while ((p = av_codec_iterate(&i))) {
if (!x(p))
continue;
if (strcmp(name, p->name) == 0)
return (AVCodec*)p;
}
return NULL;
}
AVCodec *avcodec_find_encoder_by_name(const char *name)
{
return find_codec_by_name(name, av_codec_is_encoder);
}
AVCodec *avcodec_find_decoder_by_name(const char *name)
{
return find_codec_by_name(name, av_codec_is_decoder);
}
The av_codec_iterate
will iteratre the codec_list
variable, which is a pregenerated list of supported codecs (populated by the features enabled when configuring the build). So if any hw accelerated codecs were enable during configuration, they will be there.