Skip to content

Commit

Permalink
Make hwdevice configurable for decoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
Themaister committed Nov 18, 2023
1 parent f3c8569 commit 4f609a5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion video/ffmpeg_decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ void VideoDecoder::Impl::begin_audio_stream()

bool VideoDecoder::Impl::init_video_decoder_post_device()
{
if (!hw.init_codec_context(video.av_codec, device, video.av_ctx))
if (!hw.init_codec_context(video.av_codec, device, video.av_ctx, opts.hwdevice))
LOGW("Failed to init hardware decode context. Falling back to software.\n");

if (avcodec_open2(video.av_ctx, video.av_codec, nullptr) < 0)
Expand Down
1 change: 1 addition & 0 deletions video/ffmpeg_decode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class VideoDecoder
bool blocking = false;
float target_video_buffer_time = 0.2f;
float target_realtime_audio_buffer_time = 0.5f;
const char *hwdevice = nullptr;
};

void set_io_interface(DemuxerIOInterface *iface);
Expand Down
2 changes: 1 addition & 1 deletion video/ffmpeg_encode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ bool VideoEncoder::Impl::init_video_codec()

if (avcodec_get_hw_config(codec, 0) != nullptr)
{
if (!hw.init_codec_context(codec, device, nullptr))
if (!hw.init_codec_context(codec, device, nullptr, nullptr))
{
LOGW("Failed to init HW encoder context, falling back to software.\n");
return false;
Expand Down
21 changes: 14 additions & 7 deletions video/ffmpeg_hw_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ struct FFmpegHWDevice::Impl
}
}

bool init_hw_device(const AVCodec *av_codec)
bool init_hw_device(const AVCodec *av_codec, const char *type)
{
#ifdef HAVE_FFMPEG_VULKAN
bool use_vulkan = false;
Expand All @@ -159,10 +159,15 @@ struct FFmpegHWDevice::Impl
#ifdef HAVE_FFMPEG_VULKAN
if (config->device_type == AV_HWDEVICE_TYPE_VULKAN && !use_vulkan)
continue;
if (config->device_type != AV_HWDEVICE_TYPE_VULKAN && use_vulkan)
continue;
#endif

if (type)
{
const char *hwdevice_name = av_hwdevice_get_type_name(config->device_type);
if (strcmp(type, hwdevice_name) != 0)
continue;
}

if ((config->methods & (AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX | AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX)) != 0)
init_hw_device_ctx(config);
}
Expand Down Expand Up @@ -221,7 +226,8 @@ struct FFmpegHWDevice::Impl
return true;
}

bool init_codec_context(const AVCodec *av_codec, Vulkan::Device *device_, AVCodecContext *av_ctx)
bool init_codec_context(const AVCodec *av_codec, Vulkan::Device *device_,
AVCodecContext *av_ctx, const char *type)
{
if (device && (device != device_ || av_codec != cached_av_codec))
{
Expand All @@ -235,7 +241,7 @@ struct FFmpegHWDevice::Impl
device = device_;
cached_av_codec = av_codec;

if (!init_hw_device(av_codec))
if (!init_hw_device(av_codec, type))
return false;

if (av_ctx && (hw_config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) != 0)
Expand Down Expand Up @@ -294,11 +300,12 @@ FFmpegHWDevice::~FFmpegHWDevice()
{
}

bool FFmpegHWDevice::init_codec_context(const AVCodec *codec, Vulkan::Device *device, AVCodecContext *ctx)
bool FFmpegHWDevice::init_codec_context(const AVCodec *codec, Vulkan::Device *device,
AVCodecContext *ctx, const char *type)
{
if (!impl)
impl.reset(new Impl);
return impl->init_codec_context(codec, device, ctx);
return impl->init_codec_context(codec, device, ctx, type);
}

bool FFmpegHWDevice::init_frame_context(AVCodecContext *ctx,
Expand Down
2 changes: 1 addition & 1 deletion video/ffmpeg_hw_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct FFmpegHWDevice
FFmpegHWDevice();
~FFmpegHWDevice();

bool init_codec_context(const AVCodec *codec, Vulkan::Device *device, AVCodecContext *ctx);
bool init_codec_context(const AVCodec *codec, Vulkan::Device *device, AVCodecContext *ctx, const char *type);
bool init_frame_context(AVCodecContext *ctx, unsigned width, unsigned height, int sw_pixel_format);
int get_hw_device_type() const;
int get_pix_fmt() const;
Expand Down

0 comments on commit 4f609a5

Please sign in to comment.