refactor: always require pix_fmt in Scale

This commit is contained in:
kieran 2024-11-06 13:41:42 +00:00
parent dddddf7eba
commit 69a8e56573
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941
2 changed files with 25 additions and 13 deletions

View File

@ -1,5 +1,9 @@
use anyhow::Error; use anyhow::Error;
use ffmpeg_sys_the_third::{av_dict_set, av_frame_alloc, av_frame_copy_props, 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 ffmpeg_sys_the_third::{
av_dict_set, av_frame_alloc, av_frame_copy_props, 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::collections::HashMap;
use std::ffi::CStr; use std::ffi::CStr;
use std::ptr; use std::ptr;

View File

@ -2,10 +2,10 @@ use std::mem::transmute;
use std::ptr; use std::ptr;
use crate::return_ffmpeg_error; use crate::return_ffmpeg_error;
use anyhow::Error; use anyhow::{bail, Error};
use ffmpeg_sys_the_third::{ use ffmpeg_sys_the_third::{
av_frame_alloc, av_frame_copy_props, sws_freeContext, sws_getContext, sws_scale_frame, AVFrame, av_frame_alloc, av_frame_copy_props, sws_freeContext, sws_getContext, sws_init_context,
AVPixelFormat, SwsContext, SWS_BILINEAR, sws_scale_frame, AVFrame, AVPixelFormat, SwsContext, SWS_BILINEAR,
}; };
pub struct Scaler { pub struct Scaler {
@ -29,10 +29,10 @@ impl Drop for Scaler {
} }
impl Scaler { impl Scaler {
pub fn new(format: AVPixelFormat) -> Self { pub fn new(width: u16, height: u16, format: AVPixelFormat) -> Self {
Self { Self {
width: 0, width,
height: 0, height,
format, format,
ctx: ptr::null_mut(), ctx: ptr::null_mut(),
} }
@ -43,8 +43,13 @@ impl Scaler {
frame: *const AVFrame, frame: *const AVFrame,
width: u16, width: u16,
height: u16, height: u16,
format: AVPixelFormat,
) -> Result<(), Error> { ) -> Result<(), Error> {
if !self.ctx.is_null() && self.width == width && self.height == height { if !self.ctx.is_null()
&& self.width == width
&& self.height == height
&& self.format == format
{
return Ok(()); return Ok(());
} }
@ -54,8 +59,6 @@ impl Scaler {
self.ctx = ptr::null_mut(); self.ctx = ptr::null_mut();
} }
self.width = width;
self.height = height;
self.ctx = sws_getContext( self.ctx = sws_getContext(
(*frame).width, (*frame).width,
(*frame).height, (*frame).height,
@ -69,8 +72,12 @@ impl Scaler {
ptr::null_mut(), ptr::null_mut(),
); );
if self.ctx.is_null() { if self.ctx.is_null() {
return Err(Error::msg("Failed to create scalar context")); bail!("Failed to create scalar context");
} }
self.width = width;
self.height = height;
self.format = format;
Ok(()) Ok(())
} }
@ -79,12 +86,13 @@ impl Scaler {
frame: *mut AVFrame, frame: *mut AVFrame,
width: u16, width: u16,
height: u16, height: u16,
format: AVPixelFormat,
) -> Result<*mut AVFrame, Error> { ) -> Result<*mut AVFrame, Error> {
if !(*frame).hw_frames_ctx.is_null() { if !(*frame).hw_frames_ctx.is_null() {
anyhow::bail!("Hardware frames are not supported in this software scalar"); bail!("Hardware frames are not supported in this software scalar");
} }
self.setup_scaler(frame, width, height)?; self.setup_scaler(frame, width, height, format)?;
let dst_frame = av_frame_alloc(); let dst_frame = av_frame_alloc();
let ret = av_frame_copy_props(dst_frame, frame); let ret = av_frame_copy_props(dst_frame, frame);