Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

allow iterating hw config

Brendan Allanc202b6b8fadf
diff --git a/crates/ffmpeg-hw-device/src/lib.rs b/crates/ffmpeg-hw-device/src/lib.rs
index f77f48017..cb8ee41bd 100644
--- a/crates/ffmpeg-hw-device/src/lib.rs
+++ b/crates/ffmpeg-hw-device/src/lib.rs
@@ -10,8 +10,8 @@ use ffmpeg::{
};
use ffmpeg_sys_next::{
av_buffer_ref, av_buffer_unref, av_hwdevice_ctx_create, av_hwframe_transfer_data,
- avcodec_get_hw_config, AVBufferRef, AVCodecContext, AVHWDeviceType, AVPixelFormat,
- AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
+ avcodec_get_hw_config, AVBufferRef, AVCodecContext, AVCodecHWConfig, AVHWDeviceType,
+ AVPixelFormat, AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
};
thread_local! {
@@ -122,6 +122,25 @@ impl CodecContextExt for codec::decoder::decoder::Decoder {
}
}
+pub trait CodecExt {
+ fn hw_configs(&self) -> impl Iterator<Item = *const AVCodecHWConfig>;
+}
+
+impl CodecExt for codec::codec::Codec {
+ fn hw_configs(&self) -> impl Iterator<Item = *const AVCodecHWConfig> {
+ let mut i = 0;
+
+ std::iter::from_fn(move || {
+ let config = unsafe { avcodec_get_hw_config(self.as_ptr(), i) };
+ if config.is_null() {
+ return None;
+ }
+ i += 1;
+ Some(config)
+ })
+ }
+}
+
// impl CodecContextExt for codec::encoder::video::Video {
// fn try_use_hw_device(
// &mut self,
diff --git a/crates/rendering/src/decoder/ffmpeg.rs b/crates/rendering/src/decoder/ffmpeg.rs
index c928d5218..826a27ec3 100644
--- a/crates/rendering/src/decoder/ffmpeg.rs
+++ b/crates/rendering/src/decoder/ffmpeg.rs
@@ -9,7 +9,7 @@ use ffmpeg::{
codec::{self, Capabilities},
format, frame, rescale, software, Codec, Rescale,
};
-use ffmpeg_hw_device::{CodecContextExt, HwDevice};
+use ffmpeg_hw_device::{CodecContextExt, CodecExt, HwDevice};
use ffmpeg_sys_next::{avcodec_find_decoder, AVHWDeviceType};
use tokio::sync::oneshot;
@@ -117,6 +117,12 @@ impl FfmpegDecoder {
}
}
+ for config in decoder.codec().unwrap().hw_configs() {
+ unsafe {
+ dbg!((*config).device_type);
+ }
+ }
+
let hw_device: Option<HwDevice> = if cfg!(target_os = "macos") {
decoder
.try_use_hw_device(AVHWDeviceType::AV_HWDEVICE_TYPE_VIDEOTOOLBOX, Pixel::NV12)
@@ -125,14 +131,14 @@ impl FfmpegDecoder {
// For now, disable hardware acceleration on Windows as it's causing issues
// This ensures reliable playback while we debug the hardware acceleration issues
None
-
+
// Uncomment the below code once hardware acceleration issues are resolved
/*
// Try D3D11VA first - most widely supported on modern Windows
let d3d11va = decoder
.try_use_hw_device(AVHWDeviceType::AV_HWDEVICE_TYPE_D3D11VA, Pixel::D3D11)
.ok();
-
+
if d3d11va.is_some() {
println!("Using D3D11VA hardware acceleration");
d3d11va
@@ -141,7 +147,7 @@ impl FfmpegDecoder {
let cuda = decoder
.try_use_hw_device(AVHWDeviceType::AV_HWDEVICE_TYPE_CUDA, Pixel::CUDA)
.ok();
-
+
if cuda.is_some() {
println!("Using CUDA hardware acceleration");
cuda
@@ -150,7 +156,7 @@ impl FfmpegDecoder {
let qsv = decoder
.try_use_hw_device(AVHWDeviceType::AV_HWDEVICE_TYPE_QSV, Pixel::QSV)
.ok();
-
+
if qsv.is_some() {
println!("Using QSV hardware acceleration");
qsv