Skip to content

Commit 5cffa03

Browse files
committed
Fix FFmpeg hwdevice setup for Vulkan.
1 parent a0e21a6 commit 5cffa03

5 files changed

+37
-18
lines changed

tests/video_encode_test.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int main()
2121
options.width = 640;
2222
options.height = 480;
2323
options.frame_timebase = { 1, 60 };
24-
options.encoder = "h264_pyro";
24+
options.encoder = "hevc_vulkan";
2525
options.low_latency = true;
2626
options.realtime = true;
2727

@@ -33,7 +33,8 @@ int main()
3333
ctx.set_system_handles(handles);
3434
if (!ctx.init_instance_and_device(nullptr, 0, nullptr, 0,
3535
Vulkan::CONTEXT_CREATION_ENABLE_VIDEO_ENCODE_BIT |
36-
Vulkan::CONTEXT_CREATION_ENABLE_VIDEO_H264_BIT))
36+
Vulkan::CONTEXT_CREATION_ENABLE_VIDEO_H264_BIT |
37+
Vulkan::CONTEXT_CREATION_ENABLE_VIDEO_H265_BIT))
3738
{
3839
return 1;
3940
}

video/ffmpeg_decode.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ void VideoDecoder::Impl::begin_audio_stream()
10561056

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

10621062
if (avcodec_open2(video.av_ctx, video.av_codec, nullptr) < 0)

video/ffmpeg_encode.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ bool VideoEncoder::Impl::init_video_codec_av(const AVCodec *codec)
912912
{
913913
if (avcodec_get_hw_config(codec, 0) != nullptr)
914914
{
915-
if (!hw.init_codec_context(codec, device, nullptr, nullptr))
915+
if (!hw.init_codec_context(codec, device, nullptr, nullptr, true))
916916
{
917917
LOGW("Failed to init HW encoder context, falling back to software.\n");
918918
return false;

video/ffmpeg_hw_device.cpp

+31-13
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,36 @@ struct FFmpegHWDevice::Impl
157157
}
158158
}
159159

160-
bool init_hw_device(const AVCodec *av_codec, const char *type)
160+
bool init_hw_device(const AVCodec *av_codec, const char *type, bool encode)
161161
{
162162
#ifdef HAVE_FFMPEG_VULKAN
163-
bool use_vulkan = device->get_device_features().supports_video_decode_queue;
164-
if (use_vulkan)
163+
bool use_vulkan = false;
164+
165+
if (encode)
165166
{
166-
if (av_codec->id == AV_CODEC_ID_H264)
167-
use_vulkan = device->get_device_features().supports_video_decode_h264;
168-
else if (av_codec->id == AV_CODEC_ID_HEVC)
169-
use_vulkan = device->get_device_features().supports_video_decode_h265;
170-
else
171-
use_vulkan = false;
167+
use_vulkan = device->get_device_features().supports_video_encode_queue;
168+
if (use_vulkan)
169+
{
170+
if (av_codec->id == AV_CODEC_ID_H264)
171+
use_vulkan = device->get_device_features().supports_video_encode_h264;
172+
else if (av_codec->id == AV_CODEC_ID_HEVC)
173+
use_vulkan = device->get_device_features().supports_video_encode_h265;
174+
else
175+
use_vulkan = false;
176+
}
177+
}
178+
else
179+
{
180+
use_vulkan = device->get_device_features().supports_video_decode_queue;
181+
if (use_vulkan)
182+
{
183+
if (av_codec->id == AV_CODEC_ID_H264)
184+
use_vulkan = device->get_device_features().supports_video_decode_h264;
185+
else if (av_codec->id == AV_CODEC_ID_HEVC)
186+
use_vulkan = device->get_device_features().supports_video_decode_h265;
187+
else
188+
use_vulkan = false;
189+
}
172190
}
173191
#endif
174192

@@ -263,7 +281,7 @@ struct FFmpegHWDevice::Impl
263281
}
264282

265283
bool init_codec_context(const AVCodec *av_codec, Vulkan::Device *device_,
266-
AVCodecContext *av_ctx, const char *type)
284+
AVCodecContext *av_ctx, const char *type, bool encode)
267285
{
268286
if (device && (device != device_ || av_codec != cached_av_codec))
269287
{
@@ -277,7 +295,7 @@ struct FFmpegHWDevice::Impl
277295
device = device_;
278296
cached_av_codec = av_codec;
279297

280-
if (!init_hw_device(av_codec, type))
298+
if (!init_hw_device(av_codec, type, encode))
281299
return false;
282300

283301
if (av_ctx && (hw_config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) != 0)
@@ -337,11 +355,11 @@ FFmpegHWDevice::~FFmpegHWDevice()
337355
}
338356

339357
bool FFmpegHWDevice::init_codec_context(const AVCodec *codec, Vulkan::Device *device,
340-
AVCodecContext *ctx, const char *type)
358+
AVCodecContext *ctx, const char *type, bool encode)
341359
{
342360
if (!impl)
343361
impl.reset(new Impl);
344-
return impl->init_codec_context(codec, device, ctx, type);
362+
return impl->init_codec_context(codec, device, ctx, type, encode);
345363
}
346364

347365
bool FFmpegHWDevice::init_frame_context(AVCodecContext *ctx,

video/ffmpeg_hw_device.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct FFmpegHWDevice
4040
FFmpegHWDevice();
4141
~FFmpegHWDevice();
4242

43-
bool init_codec_context(const AVCodec *codec, Vulkan::Device *device, AVCodecContext *ctx, const char *type);
43+
bool init_codec_context(const AVCodec *codec, Vulkan::Device *device, AVCodecContext *ctx, const char *type, bool encode);
4444
bool init_frame_context(AVCodecContext *ctx, unsigned width, unsigned height, int sw_pixel_format);
4545
int get_hw_device_type() const;
4646
int get_pix_fmt() const;

0 commit comments

Comments
 (0)