-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
starting point for implementing hwaccel
- Loading branch information
Showing
5 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
extern crate ffmpeg_next as ffmpeg; | ||
|
||
use crate::hwaccel::HardwareAccelerationDeviceType; | ||
|
||
pub struct HardwareDeviceContext { | ||
ptr: *mut ffmpeg::ffi::AVBufferRef, | ||
} | ||
|
||
impl HardwareDeviceContext { | ||
pub fn new( | ||
device_type: HardwareAccelerationDeviceType, | ||
) -> Result<HardwareDeviceContext, ffmpeg::error::Error> { | ||
let mut ptr: *mut ffmpeg::ffi::AVBufferRef = std::ptr::null_mut(); | ||
|
||
unsafe { | ||
match ffmpeg::ffi::av_hwdevice_ctx_create( | ||
(&mut ptr) as *mut *mut ffmpeg::ffi::AVBufferRef, | ||
device_type.into(), | ||
std::ptr::null(), | ||
std::ptr::null_mut(), | ||
0, | ||
) { | ||
0 => Ok(HardwareDeviceContext { ptr }), | ||
e => Err(ffmpeg::error::Error::from(e)), | ||
} | ||
} | ||
} | ||
|
||
unsafe fn ref_raw(&self) -> *mut ffmpeg::ffi::AVBufferRef { | ||
ffmpeg::ffi::av_buffer_ref(self.ptr) | ||
} | ||
} | ||
|
||
impl Drop for HardwareDeviceContext { | ||
fn drop(&mut self) { | ||
unsafe { | ||
ffmpeg::ffi::av_buffer_unref(&mut self.ptr); | ||
} | ||
} | ||
} | ||
|
||
pub fn hwdevice_list_available_device_types() -> Vec<HardwareAccelerationDeviceType> { | ||
let mut hwdevice_types = Vec::new(); | ||
let mut hwdevice_type = unsafe { | ||
ffmpeg::ffi::av_hwdevice_iterate_types(ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_NONE) | ||
}; | ||
while hwdevice_type != ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_NONE { | ||
hwdevice_types.push(HardwareAccelerationDeviceType::from(hwdevice_type).unwrap()); | ||
hwdevice_type = unsafe { ffmpeg::ffi::av_hwdevice_iterate_types(hwdevice_type) }; | ||
} | ||
hwdevice_types | ||
} | ||
|
||
pub fn codec_find_corresponding_hwaccel_pixfmt( | ||
codec: &ffmpeg::codec::codec::Codec, | ||
hwaccel_type: HardwareAccelerationDeviceType, | ||
) -> Option<ffmpeg::format::pixel::Pixel> { | ||
let mut i = 0; | ||
loop { | ||
unsafe { | ||
let hw_config = ffmpeg::ffi::avcodec_get_hw_config(codec.as_ptr(), i); | ||
if !hw_config.is_null() { | ||
let hw_config_supports_codec = (((*hw_config).methods) as i32 | ||
& ffmpeg::ffi::AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX as i32) | ||
!= 0; | ||
if hw_config_supports_codec && (*hw_config).device_type == hwaccel_type.into() { | ||
break Some((*hw_config).pix_fmt.into()); | ||
} | ||
} else { | ||
break None; | ||
} | ||
} | ||
i += 1; | ||
} | ||
} | ||
|
||
pub fn codec_context_hwaccel_set_get_format( | ||
codec_context: &mut ffmpeg::codec::context::Context, | ||
hw_pixfmt: ffmpeg::format::pixel::Pixel, | ||
) { | ||
unsafe { | ||
(*codec_context.as_mut_ptr()).opaque = | ||
ffmpeg::ffi::AVPixelFormat::from(hw_pixfmt) as i32 as _; | ||
(*codec_context.as_mut_ptr()).get_format = Some(hwaccel_get_format); | ||
} | ||
} | ||
|
||
pub fn codec_context_hwaccel_set_hw_device_ctx( | ||
codec_context: &mut ffmpeg::codec::context::Context, | ||
hardware_device_context: &HardwareDeviceContext, | ||
) { | ||
unsafe { | ||
(*codec_context.as_mut_ptr()).hw_device_ctx = hardware_device_context.ref_raw(); | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
unsafe extern "C" fn hwaccel_get_format( | ||
ctx: *mut ffmpeg::ffi::AVCodecContext, | ||
pix_fmts: *const ffmpeg::ffi::AVPixelFormat, | ||
) -> ffmpeg::ffi::AVPixelFormat { | ||
let mut p = pix_fmts; | ||
while *p != ffmpeg::ffi::AVPixelFormat::AV_PIX_FMT_NONE { | ||
if *p == std::mem::transmute((*ctx).opaque as i32) { | ||
return *p; | ||
} | ||
p = p.add(1); | ||
} | ||
ffmpeg::ffi::AVPixelFormat::AV_PIX_FMT_NONE | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
extern crate ffmpeg_next as ffmpeg; | ||
|
||
use crate::error::Error; | ||
use crate::ffi_hwaccel; | ||
|
||
type Result<T> = std::result::Result<T, Error>; | ||
|
||
pub struct HardwareAccelerationContext { | ||
hardware_device_context: ffi_hwaccel::HardwareDeviceContext, | ||
pixel_format: ffmpeg::util::format::Pixel, | ||
} | ||
|
||
impl HardwareAccelerationContext { | ||
pub fn new( | ||
decoder: &mut ffmpeg::codec::Context, | ||
device_type: HardwareAccelerationDeviceType, | ||
) -> Result<Self> { | ||
let codec = decoder.codec().ok_or(Error::UninitializedCodec)?; | ||
let pixel_format = | ||
ffi_hwaccel::codec_find_corresponding_hwaccel_pixfmt(&codec, device_type) | ||
.ok_or(Error::UnsupportedCodecHardwareAccelerationDeviceType)?; | ||
|
||
ffi_hwaccel::codec_context_hwaccel_set_get_format(decoder, pixel_format); | ||
|
||
let hardware_device_context = ffi_hwaccel::HardwareDeviceContext::new(device_type)?; | ||
ffi_hwaccel::codec_context_hwaccel_set_hw_device_ctx(decoder, &hardware_device_context); | ||
|
||
Ok(HardwareAccelerationContext { | ||
hardware_device_context, | ||
pixel_format, | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Debug, Copy, Clone, PartialEq, Eq)] | ||
pub enum HardwareAccelerationDeviceType { | ||
/// Video Decode and Presentation API for Unix (VDPAU) | ||
Vdpau, | ||
/// NVIDIA CUDA | ||
Cuda, | ||
/// Video Acceleration API (VA-API) | ||
VaApi, | ||
/// DirectX Video Acceleration 2.0 | ||
Dxva2, | ||
/// Quick Sync Video | ||
Qsv, | ||
/// VideoToolbox | ||
VideoToolbox, | ||
/// Direct3D 11 Video Acceleration | ||
D3D11Va, | ||
/// Linux Direct Rendering Manager | ||
Drm, | ||
/// OpenCL | ||
OpenCl, | ||
/// MediaCodec | ||
MeiaCodec, | ||
/// Vulkan | ||
Vulkan, | ||
/// Direct3D 12 Video Acceleration | ||
D3D12Va, | ||
} | ||
|
||
impl HardwareAccelerationDeviceType { | ||
/// Whether or not the device type is available on this system. | ||
pub fn is_available(self) -> bool { | ||
Self::list_available().contains(&self) | ||
} | ||
|
||
/// List available hardware acceleration device types on this system. | ||
/// | ||
/// Uses `av_hwdevice_iterate_types` internally. | ||
pub fn list_available() -> Vec<HardwareAccelerationDeviceType> { | ||
ffi_hwaccel::hwdevice_list_available_device_types() | ||
} | ||
} | ||
|
||
impl HardwareAccelerationDeviceType { | ||
pub fn from(value: ffmpeg::ffi::AVHWDeviceType) -> Option<HardwareAccelerationDeviceType> { | ||
match value { | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_VDPAU => Some(Self::Vdpau), | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_CUDA => Some(Self::Cuda), | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_VAAPI => Some(Self::VaApi), | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_DXVA2 => Some(Self::Dxva2), | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_QSV => Some(Self::Qsv), | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_VIDEOTOOLBOX => Some(Self::VideoToolbox), | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_D3D11VA => Some(Self::D3D11Va), | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_DRM => Some(Self::Drm), | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_OPENCL => Some(Self::OpenCl), | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_MEDIACODEC => Some(Self::MeiaCodec), | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_VULKAN => Some(Self::Vulkan), | ||
// ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_D3D12VA => Self::D3D12Va, | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_NONE => None, | ||
} | ||
} | ||
} | ||
|
||
impl From<HardwareAccelerationDeviceType> for ffmpeg::ffi::AVHWDeviceType { | ||
fn from(value: HardwareAccelerationDeviceType) -> Self { | ||
match value { | ||
HardwareAccelerationDeviceType::Vdpau => { | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_VDPAU | ||
} | ||
HardwareAccelerationDeviceType::Cuda => { | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_CUDA | ||
} | ||
HardwareAccelerationDeviceType::VaApi => { | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_VAAPI | ||
} | ||
HardwareAccelerationDeviceType::Dxva2 => { | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_DXVA2 | ||
} | ||
HardwareAccelerationDeviceType::Qsv => { | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_QSV | ||
} | ||
HardwareAccelerationDeviceType::VideoToolbox => { | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_VIDEOTOOLBOX | ||
} | ||
HardwareAccelerationDeviceType::D3D11Va => { | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_D3D11VA | ||
} | ||
HardwareAccelerationDeviceType::Drm => { | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_DRM | ||
} | ||
HardwareAccelerationDeviceType::OpenCl => { | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_OPENCL | ||
} | ||
HardwareAccelerationDeviceType::MeiaCodec => { | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_MEDIACODEC | ||
} | ||
HardwareAccelerationDeviceType::Vulkan => { | ||
ffmpeg::ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_VULKAN | ||
} | ||
HardwareAccelerationDeviceType::D3D12Va => { | ||
unimplemented!() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters