Skip to content

Commit

Permalink
AVFrame ptr is now privale
Browse files Browse the repository at this point in the history
Signed-off-by: Fabrice Desclaux <[email protected]>
  • Loading branch information
serpilliere committed Dec 16, 2024
1 parent 091b9b1 commit 02ef563
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
8 changes: 4 additions & 4 deletions sanzu/src/ffmpeg_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl Drop for AVPacket {
#[derive(Debug)]
pub struct AVFrame {
/// Raw pointer on the AVFrame
pub ptr: *mut ffmpeg::AVFrame,
ptr: *mut ffmpeg::AVFrame,
}

impl AVFrame {
Expand All @@ -143,19 +143,19 @@ impl AVFrame {
}
}

pub fn as_mut_ptr(&self) -> *mut ffmpeg::AVFrame {
pub fn get_ptr(&self) -> *mut ffmpeg::AVFrame {
self.ptr
}

pub fn make_writable(&mut self) -> Result<()> {
pub fn make_writable(&self) -> Result<()> {
let retval: i32 = unsafe { ffmpeg::av_frame_make_writable(self.ptr) };
if retval < 0 {
return Err(averror("av_frame_make", retval));
}
Ok(())
}

pub fn plane(&mut self, indx: usize, len: usize) -> &mut [u8] {
pub fn plane(&self, indx: usize, len: usize) -> &mut [u8] {
unsafe {
let data_ptr = (*self.ptr).data[indx];
std::slice::from_raw_parts_mut(data_ptr, len)
Expand Down
2 changes: 1 addition & 1 deletion sanzu/src/video_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn decode(
let mut duration_yuv = Duration::new(0, 0);
let context_ptr = decoder.context.as_mut_ptr();
let packet_ptr = decoder.packet.as_mut_ptr();
let frame_ptr = decoder.frame.as_mut_ptr();
let frame_ptr = decoder.frame.get_ptr();

let mut ret = unsafe { ffmpeg::avcodec_send_packet(context_ptr, packet_ptr) };

Expand Down
29 changes: 14 additions & 15 deletions sanzu/src/video_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl EncoderBuilder {
return Err(averror("avcodec_open2", retval));
}
let frame = AVFrame::new()?;
let frame_ptr = frame.as_mut_ptr();
let frame_ptr = frame.get_ptr();
unsafe {
(*frame_ptr).format = (*context_ptr).pix_fmt as i32;
(*frame_ptr).width = (*context_ptr).width;
Expand Down Expand Up @@ -224,15 +224,15 @@ impl Encoder for EncoderFFmpeg {
) -> Result<(Vec<u8>, EncoderTimings)> {
let time_start = Instant::now();

let pixel_format = unsafe { (*self.frame.ptr).format };
let pixel_format = unsafe { (*self.frame.get_ptr()).format };

match pixel_format {
x if x == AVPixelFormat::AV_PIX_FMT_YUV420P as i32 => {
// yuv420
let (y_lane, u_lane, v_lane) = unsafe {
let y_lane = (*self.frame.ptr).linesize[0] as u32;
let u_lane = (*self.frame.ptr).linesize[1] as u32;
let v_lane = (*self.frame.ptr).linesize[2] as u32;
let y_lane = (*self.frame.get_ptr()).linesize[0] as u32;
let u_lane = (*self.frame.get_ptr()).linesize[1] as u32;
let v_lane = (*self.frame.get_ptr()).linesize[2] as u32;
(y_lane, u_lane, v_lane)
};

Expand Down Expand Up @@ -305,9 +305,9 @@ impl Encoder for EncoderFFmpeg {
x if x == AVPixelFormat::AV_PIX_FMT_YUV444P as i32 => {
// yuv444
let (y_lane, u_lane, v_lane) = unsafe {
let y_lane = (*self.frame.ptr).linesize[0] as u32;
let u_lane = (*self.frame.ptr).linesize[1] as u32;
let v_lane = (*self.frame.ptr).linesize[2] as u32;
let y_lane = (*self.frame.get_ptr()).linesize[0] as u32;
let u_lane = (*self.frame.get_ptr()).linesize[1] as u32;
let v_lane = (*self.frame.get_ptr()).linesize[2] as u32;
(y_lane, u_lane, v_lane)
};

Expand Down Expand Up @@ -380,8 +380,8 @@ impl Encoder for EncoderFFmpeg {
x if x == AVPixelFormat::AV_PIX_FMT_NV12 as i32 => {
// nv12
let (y_lane, uv_lane) = unsafe {
let y_lane = (*self.frame.ptr).linesize[0] as u32;
let uv_lane = (*self.frame.ptr).linesize[1] as u32;
let y_lane = (*self.frame.get_ptr()).linesize[0] as u32;
let uv_lane = (*self.frame.get_ptr()).linesize[1] as u32;
(y_lane, uv_lane)
};

Expand Down Expand Up @@ -447,7 +447,7 @@ impl Encoder for EncoderFFmpeg {
let image_bpl = bytes_per_line as usize;
let height = height as usize;

let plane_bpl = unsafe { (*self.frame.ptr).linesize[0] } as usize;
let plane_bpl = unsafe { (*self.frame.get_ptr()).linesize[0] } as usize;
let size = plane_bpl * height;
self.frame
.make_writable()
Expand Down Expand Up @@ -481,13 +481,12 @@ impl Encoder for EncoderFFmpeg {
};

unsafe {
(*self.frame.as_mut_ptr()).pts = count;
(*self.frame.get_ptr()).pts = count;
}
let time_yuv = Instant::now();

let mut retval = unsafe {
ffmpeg::avcodec_send_frame(self.context.as_mut_ptr(), self.frame.as_mut_ptr())
};
let mut retval =
unsafe { ffmpeg::avcodec_send_frame(self.context.as_mut_ptr(), self.frame.get_ptr()) };
if retval < 0 {
return Err(averror("avcodec_send_frame", retval));
}
Expand Down

0 comments on commit 02ef563

Please sign in to comment.