diff --git a/examples/main.rs b/examples/main.rs index a81b472..f11cb43 100644 --- a/examples/main.rs +++ b/examples/main.rs @@ -3,7 +3,6 @@ use ffmpeg_sys_the_third::AVHWDeviceType::{ AV_HWDEVICE_TYPE_CUDA, AV_HWDEVICE_TYPE_D3D12VA, AV_HWDEVICE_TYPE_MEDIACODEC, AV_HWDEVICE_TYPE_OPENCL, AV_HWDEVICE_TYPE_VDPAU, AV_HWDEVICE_TYPE_VULKAN, }; -use ffmpeg_sys_the_third::AVPixelFormat::AV_PIX_FMT_BGR24; use ffmpeg_sys_the_third::{av_frame_free, av_packet_free, AVMediaType}; use log::{error, info}; use std::env::args; diff --git a/src/lib.rs b/src/lib.rs index 0802428..1451747 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,5 @@ use anyhow::Error; -use ffmpeg_sys_the_third::{ - av_dict_set, av_make_error_string, av_opt_next, av_opt_set, AVDictionary, AVOption, - AV_OPT_SEARCH_CHILDREN, -}; +use ffmpeg_sys_the_third::{av_dict_set, av_frame_alloc, av_frame_free, av_hwframe_transfer_data, av_make_error_string, av_opt_next, av_opt_set, AVDictionary, AVFrame, AVOption, AV_OPT_SEARCH_CHILDREN}; use std::collections::HashMap; use std::ffi::CStr; use std::ptr; @@ -117,6 +114,19 @@ fn set_opts(ctx: *mut libc::c_void, options: HashMap) -> Result< Ok(()) } +/// Get the frame as CPU frame +pub unsafe fn get_frame_from_hw(mut frame: *mut AVFrame) -> Result<*mut AVFrame, Error> { + if (*frame).hw_frames_ctx.is_null() { + Ok(frame) + } else { + let new_frame = av_frame_alloc(); + let ret = av_hwframe_transfer_data(new_frame, frame, 0); + return_ffmpeg_error!(ret); + av_frame_free(&mut frame); + Ok(new_frame) + } +} + pub use decode::*; pub use demux::*; pub use ffmpeg_sys_the_third; diff --git a/src/resample.rs b/src/resample.rs index 47181cd..21be9f8 100644 --- a/src/resample.rs +++ b/src/resample.rs @@ -54,6 +54,9 @@ impl Resample { } pub unsafe fn process_frame(&mut self, frame: *mut AVFrame) -> Result<*mut AVFrame, Error> { + if !(*frame).hw_frames_ctx.is_null() { + anyhow::bail!("Hardware frames are not supported in this software re-sampler"); + } self.setup_swr(frame)?; let mut out_frame = av_frame_alloc(); diff --git a/src/scale.rs b/src/scale.rs index 8e3defe..4f3231d 100644 --- a/src/scale.rs +++ b/src/scale.rs @@ -80,6 +80,10 @@ impl Scaler { width: u16, height: u16, ) -> Result<*mut AVFrame, Error> { + if !(*frame).hw_frames_ctx.is_null() { + anyhow::bail!("Hardware frames are not supported in this software scalar"); + } + self.setup_scaler(frame, width, height)?; let dst_frame = av_frame_alloc();