1use std::{error::Error, ffi::CStr, fmt, ptr, slice};
5
6#[cfg(all(target_os = "linux", feature = "hwdevice"))]
7use std::ffi::CString;
8
9#[cfg(all(
10 any(target_os = "linux", target_os = "macos", target_os = "windows"),
11 feature = "hwdevice"
12))]
13use std::sync::Mutex;
14#[cfg(all(target_os = "linux", feature = "hwdevice"))]
15use std::sync::MutexGuard;
16
17pub use alpha2_media::GpuFault;
18use rsmpeg::{
19 avcodec::{AVCodecContext, AVCodecParameters},
20 avutil::AVFrame,
21 error::RsmpegError,
22 ffi,
23 swscale::SwsContext,
24};
25
26#[cfg(all(any(target_os = "linux", target_os = "windows"), feature = "hwdevice"))]
27use rsmpeg::avutil::AVHWDeviceContext;
28
29mod container;
30mod log;
31
32pub use container::{
33 CreateOutputError, DemuxInput, OpenInputError, OutputFile, PathError, file_url, open_input,
34};
35pub use log::{LogLine, LogMark, install_log_capture, log_detail, log_mark, log_since};
36
37#[allow(
39 dead_code,
40 non_camel_case_types,
41 unreachable_pub,
42 clippy::all,
43 clippy::pedantic,
44 clippy::undocumented_unsafe_blocks
45)]
46mod interop {
47 include!(concat!(env!("OUT_DIR"), "/interop.rs"));
48}
49
50const _: () = {
53 use interop::{Alpha2D3D12FrameInfo as D3D12, Alpha2VideoToolboxFrameInfo as VideoToolbox};
54 use std::mem::{align_of, offset_of, size_of};
55 assert!(size_of::<D3D12>() == 64 && align_of::<D3D12>() == 8);
56 assert!(offset_of!(D3D12, resource) == 0);
57 assert!(offset_of!(D3D12, fence) == 8);
58 assert!(offset_of!(D3D12, fence_value) == 16);
59 assert!(offset_of!(D3D12, device) == 24);
60 assert!(offset_of!(D3D12, dxgi_format) == 32);
61 assert!(offset_of!(D3D12, width) == 36);
62 assert!(offset_of!(D3D12, height) == 40);
63 assert!(offset_of!(D3D12, hardware_format) == 44);
64 assert!(offset_of!(D3D12, software_format) == 48);
65 assert!(offset_of!(D3D12, surface_width) == 52);
66 assert!(offset_of!(D3D12, surface_height) == 56);
67 assert!(size_of::<VideoToolbox>() == 24 && align_of::<VideoToolbox>() == 8);
68 assert!(offset_of!(VideoToolbox, pixel_buffer) == 0);
69 assert!(offset_of!(VideoToolbox, width) == 8);
70 assert!(offset_of!(VideoToolbox, height) == 12);
71 assert!(offset_of!(VideoToolbox, hardware_format) == 16);
72 assert!(offset_of!(VideoToolbox, software_format) == 20);
73};
74
75#[cfg(all(
78 any(target_os = "linux", target_os = "macos", target_os = "windows"),
79 feature = "hwdevice"
80))]
81#[allow(clippy::cast_ptr_alignment)]
83fn download_hardware_frame(frame: &AVFrame) -> Result<AVFrame, BridgeError> {
84 if frame.hw_frames_ctx.is_null() {
85 return Err(BridgeError::InvalidInput(
86 "hardware frame has no frames context",
87 ));
88 }
89 let written = unsafe {
92 let frames = (*frame.hw_frames_ctx).data.cast::<ffi::AVHWFramesContext>();
93 ((*frames).width, (*frames).height)
94 };
95 let mut software = AVFrame::new();
96 software
97 .hwframe_transfer_data(frame)
98 .map_err(BridgeError::Ffmpeg)?;
99 zero_row_padding(&mut software, written.0, written.1)?;
102 let copied = unsafe { ffi::av_frame_copy_props(software.as_mut_ptr(), frame.as_ptr()) };
105 if copied < 0 {
106 return Err(BridgeError::FfmpegCode(copied));
107 }
108 Ok(software)
109}
110
111#[cfg_attr(not(feature = "hwdevice"), allow(dead_code))]
119fn zero_row_padding(frame: &mut AVFrame, width: i32, height: i32) -> Result<(), BridgeError> {
120 let format = frame.format;
121 let descriptor = unsafe { ffi::av_pix_fmt_desc_get(format).as_ref() }
123 .ok_or(BridgeError::InvalidInput("pixel format has no descriptor"))?;
124 if width < 0 || height < 0 {
125 return Err(BridgeError::InvalidInput("negative frame size"));
126 }
127 let planes = unsafe { ffi::av_pix_fmt_count_planes(format) };
129 for plane in 0..usize::try_from(planes).unwrap_or(0).min(4) {
130 let plane_index = i32::try_from(plane).unwrap_or(0);
131 let written = unsafe { ffi::av_image_get_linesize(format, width, plane_index) };
133 let (Ok(written), Ok(stride)) = (
134 usize::try_from(written),
135 usize::try_from(frame.linesize[plane]),
136 ) else {
137 continue;
138 };
139 let data = frame.data[plane];
140 if data.is_null() || stride <= written {
141 continue;
142 }
143 let shift = if plane == 1 || plane == 2 {
144 descriptor.log2_chroma_h
145 } else {
146 0
147 };
148 let rows = usize::try_from(-((-height) >> shift)).unwrap_or(0);
149 let start = data as usize;
150 let end = stride
151 .checked_mul(rows)
152 .and_then(|len| start.checked_add(len))
153 .ok_or(BridgeError::InvalidInput("frame plane size overflow"))?;
154 let inside = frame.buf.iter().any(|buffer| {
155 unsafe { buffer.as_ref() }.is_some_and(|buffer| {
157 let buffer_start = buffer.data as usize;
158 buffer_start <= start && buffer_start.saturating_add(buffer.size) >= end
159 })
160 });
161 if !inside {
162 return Err(BridgeError::InvalidInput(
163 "frame plane lies outside its buffers",
164 ));
165 }
166 for row in 0..rows {
167 unsafe { ptr::write_bytes(data.add(row * stride + written), 0, stride - written) };
169 }
170 }
171 Ok(())
172}
173
174#[cfg(all(target_os = "windows", feature = "hwdevice"))]
175use interop::Alpha2D3D12FrameInfo as D3D12FrameDescriptor;
176
177#[cfg(all(target_os = "windows", feature = "hwdevice"))]
178unsafe extern "C" {
179 fn alpha2_d3d12_device_init(
180 context: *mut ffi::AVHWDeviceContext,
181 device: *mut std::ffi::c_void,
182 ) -> i32;
183 fn alpha2_d3d12_get_format(
184 context: *mut ffi::AVCodecContext,
185 formats: *const ffi::AVPixelFormat,
186 ) -> ffi::AVPixelFormat;
187 fn alpha2_d3d12_frame_info(
188 frame: *const ffi::AVFrame,
189 output: *mut D3D12FrameDescriptor,
190 ) -> i32;
191 fn alpha2_d3d12_frame_set_fence_value(frame: *mut ffi::AVFrame, value: u64) -> i32;
192 fn alpha2_d3d12_frame_fence_value(frame: *const ffi::AVFrame, output: *mut u64) -> i32;
193 fn alpha2_d3d12_frames_configure(
194 frames: *mut ffi::AVBufferRef,
195 dxgi_format: u32,
196 resource_flags: u32,
197 ) -> i32;
198}
199
200#[cfg(all(target_os = "windows", feature = "hwdevice"))]
202pub const DXGI_FORMAT_NV12: u32 = 103;
203
204#[cfg(all(target_os = "windows", feature = "hwdevice"))]
213pub fn d3d12_frames_configure(
214 frames: &mut rsmpeg::avutil::AVHWFramesContext,
215 dxgi_format: u32,
216 resource_flags: u32,
217) -> Result<(), BridgeError> {
218 let status =
220 unsafe { alpha2_d3d12_frames_configure(frames.as_mut_ptr(), dxgi_format, resource_flags) };
221 if status < 0 {
222 return Err(BridgeError::FfmpegCode(status));
223 }
224 Ok(())
225}
226
227#[derive(Clone, Copy, Debug, Eq, PartialEq)]
231pub struct D3D12FrameInfo {
232 pub resource: u64,
234 pub fence: u64,
236 pub fence_value: u64,
238 pub device: u64,
240 pub dxgi_format: u32,
242 pub width: i32,
244 pub height: i32,
246 pub software_format: i32,
248 pub surface_width: u32,
250 pub surface_height: u32,
252}
253
254impl D3D12FrameInfo {
255 #[cfg_attr(
256 not(all(target_os = "windows", feature = "hwdevice")),
257 allow(dead_code)
258 )]
259 const fn from_descriptor(descriptor: &interop::Alpha2D3D12FrameInfo) -> Self {
260 Self {
261 resource: descriptor.resource,
262 fence: descriptor.fence,
263 fence_value: descriptor.fence_value,
264 device: descriptor.device,
265 dxgi_format: descriptor.dxgi_format,
266 width: descriptor.width,
267 height: descriptor.height,
268 software_format: descriptor.software_format,
269 surface_width: descriptor.surface_width,
270 surface_height: descriptor.surface_height,
271 }
272 }
273}
274
275#[cfg(all(target_os = "windows", feature = "hwdevice"))]
277pub struct D3D12FrameLease {
278 frame: Mutex<AVFrame>,
279}
280
281#[cfg(all(target_os = "windows", feature = "hwdevice"))]
282impl fmt::Debug for D3D12FrameLease {
283 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
284 formatter
285 .debug_struct("D3D12FrameLease")
286 .finish_non_exhaustive()
287 }
288}
289
290#[cfg(all(target_os = "windows", feature = "hwdevice"))]
291impl D3D12FrameLease {
292 #[must_use]
294 pub fn new(frame: AVFrame) -> Self {
295 Self {
296 frame: Mutex::new(frame),
297 }
298 }
299
300 pub fn download(&self) -> Result<DecodedFrame, BridgeError> {
305 let frame = self
306 .frame
307 .lock()
308 .unwrap_or_else(std::sync::PoisonError::into_inner);
309 download_hardware_frame(&frame).map(|frame| DecodedFrame {
310 frame,
311 written: true,
312 })
313 }
314
315 pub fn info(&self) -> Result<D3D12FrameInfo, BridgeError> {
320 let frame = self
321 .frame
322 .lock()
323 .unwrap_or_else(std::sync::PoisonError::into_inner);
324 d3d12_frame_info(&frame)
325 }
326}
327
328#[cfg(all(target_os = "windows", feature = "hwdevice"))]
336#[allow(clippy::cast_ptr_alignment)]
337pub unsafe fn create_d3d12_device_context(device: u64) -> Result<AVHWDeviceContext, BridgeError> {
338 let device = usize::try_from(device)
339 .map_err(|_| BridgeError::InvalidInput("invalid D3D12 device pointer"))?
340 as *mut std::ffi::c_void;
341 if device.is_null() {
342 return Err(BridgeError::InvalidInput("D3D12 device is null"));
343 }
344 let mut context = alloc_hwdevice(ffi::AV_HWDEVICE_TYPE_D3D12VA, "D3D12")?;
345 let device_context = context.data.cast::<ffi::AVHWDeviceContext>();
346 let result = unsafe { alpha2_d3d12_device_init(device_context, device) };
349 if result < 0 {
350 return Err(BridgeError::FfmpegCode(result));
351 }
352 context.init().map_err(BridgeError::Ffmpeg)?;
353 Ok(context)
354}
355
356#[cfg(all(target_os = "windows", feature = "hwdevice"))]
358#[must_use]
359pub fn d3d12_get_format_callback()
360-> unsafe extern "C" fn(*mut ffi::AVCodecContext, *const ffi::AVPixelFormat) -> ffi::AVPixelFormat {
361 alpha2_d3d12_get_format
362}
363
364#[cfg(all(target_os = "windows", feature = "hwdevice"))]
369pub fn d3d12_frame_info(frame: &AVFrame) -> Result<D3D12FrameInfo, BridgeError> {
370 let mut descriptor = D3D12FrameDescriptor::default();
371 let result = unsafe { alpha2_d3d12_frame_info(frame.as_ptr(), &raw mut descriptor) };
373 if result != 0 {
374 return Err(BridgeError::FfmpegCode(result));
375 }
376 Ok(D3D12FrameInfo::from_descriptor(&descriptor))
377}
378
379#[cfg(all(target_os = "windows", feature = "hwdevice"))]
384pub fn d3d12_frame_fence_value(frame: &AVFrame) -> Result<u64, BridgeError> {
385 let mut value = 0;
386 let result = unsafe { alpha2_d3d12_frame_fence_value(frame.as_ptr(), &raw mut value) };
388 if result != 0 {
389 return Err(BridgeError::FfmpegCode(result));
390 }
391 Ok(value)
392}
393
394#[cfg(all(target_os = "windows", feature = "hwdevice"))]
400pub struct D3D12EncodeFrameAccess<'a> {
401 frame: &'a mut AVFrame,
402 info: D3D12FrameInfo,
403}
404
405#[cfg(all(target_os = "windows", feature = "hwdevice"))]
406impl fmt::Debug for D3D12EncodeFrameAccess<'_> {
407 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
408 formatter
409 .debug_struct("D3D12EncodeFrameAccess")
410 .field("info", &self.info)
411 .finish_non_exhaustive()
412 }
413}
414
415#[cfg(all(target_os = "windows", feature = "hwdevice"))]
416impl<'a> D3D12EncodeFrameAccess<'a> {
417 pub fn begin(frame: &'a mut AVFrame) -> Result<Self, BridgeError> {
422 let info = d3d12_frame_info(frame)?;
423 Ok(Self { frame, info })
424 }
425
426 #[must_use]
428 pub const fn info(&self) -> &D3D12FrameInfo {
429 &self.info
430 }
431
432 pub fn retain_frame(&self) -> Result<AVFrame, BridgeError> {
437 let raw = unsafe { ffi::av_frame_clone(self.frame.as_ptr()) };
439 let raw = std::ptr::NonNull::new(raw)
440 .ok_or(BridgeError::InvalidInput("cannot retain encode frame"))?;
441 Ok(unsafe { AVFrame::from_raw(raw) })
443 }
444
445 pub fn commit(&mut self, fence_value: u64) -> Result<(), BridgeError> {
450 let result =
452 unsafe { alpha2_d3d12_frame_set_fence_value(self.frame.as_mut_ptr(), fence_value) };
453 if result != 0 {
454 return Err(BridgeError::FfmpegCode(result));
455 }
456 self.info.fence_value = fence_value;
457 Ok(())
458 }
459}
460
461#[cfg(all(any(target_os = "linux", target_os = "macos"), feature = "hwdevice"))]
462use interop::Alpha2VideoToolboxFrameInfo as VideoToolboxFrameDescriptor;
463
464#[cfg(all(any(target_os = "linux", target_os = "macos"), feature = "hwdevice"))]
466#[cfg_attr(target_os = "linux", allow(dead_code))]
467unsafe extern "C" {
468 fn alpha2_videotoolbox_get_format(
469 context: *mut ffi::AVCodecContext,
470 formats: *const ffi::AVPixelFormat,
471 ) -> ffi::AVPixelFormat;
472 fn alpha2_videotoolbox_frame_info(
473 frame: *const ffi::AVFrame,
474 output: *mut VideoToolboxFrameDescriptor,
475 ) -> i32;
476}
477
478#[derive(Clone, Copy, Debug, Eq, PartialEq)]
482pub struct VideoToolboxFrameInfo {
483 pub pixel_buffer: u64,
485 pub width: i32,
487 pub height: i32,
489 pub software_format: i32,
491}
492
493impl VideoToolboxFrameInfo {
494 #[cfg_attr(
495 not(all(any(target_os = "linux", target_os = "macos"), feature = "hwdevice")),
496 allow(dead_code)
497 )]
498 const fn from_descriptor(descriptor: &interop::Alpha2VideoToolboxFrameInfo) -> Self {
499 Self {
500 pixel_buffer: descriptor.pixel_buffer,
501 width: descriptor.width,
502 height: descriptor.height,
503 software_format: descriptor.software_format,
504 }
505 }
506}
507
508#[cfg(all(any(target_os = "linux", target_os = "macos"), feature = "hwdevice"))]
510#[cfg_attr(target_os = "linux", allow(dead_code))]
511fn read_videotoolbox_frame_info(frame: &AVFrame) -> Result<VideoToolboxFrameInfo, BridgeError> {
512 let mut descriptor = VideoToolboxFrameDescriptor::default();
513 let result = unsafe { alpha2_videotoolbox_frame_info(frame.as_ptr(), &raw mut descriptor) };
515 if result != 0 {
516 return Err(BridgeError::InvalidInput(
517 "frame is not a VideoToolbox frame with a pixel buffer",
518 ));
519 }
520 Ok(VideoToolboxFrameInfo::from_descriptor(&descriptor))
521}
522
523#[cfg(all(target_os = "macos", feature = "hwdevice"))]
525pub struct VideoToolboxFrameLease {
526 frame: Mutex<AVFrame>,
527}
528
529#[cfg(all(target_os = "macos", feature = "hwdevice"))]
530impl fmt::Debug for VideoToolboxFrameLease {
531 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
532 formatter
533 .debug_struct("VideoToolboxFrameLease")
534 .finish_non_exhaustive()
535 }
536}
537
538#[cfg(all(target_os = "macos", feature = "hwdevice"))]
539impl VideoToolboxFrameLease {
540 #[must_use]
542 pub fn new(frame: AVFrame) -> Self {
543 Self {
544 frame: Mutex::new(frame),
545 }
546 }
547
548 pub fn download(&self) -> Result<DecodedFrame, BridgeError> {
553 let frame = self
554 .frame
555 .lock()
556 .unwrap_or_else(std::sync::PoisonError::into_inner);
557 download_hardware_frame(&frame).map(|frame| DecodedFrame {
558 frame,
559 written: true,
560 })
561 }
562
563 pub fn info(&self) -> Result<VideoToolboxFrameInfo, BridgeError> {
568 let frame = self
569 .frame
570 .lock()
571 .unwrap_or_else(std::sync::PoisonError::into_inner);
572 read_videotoolbox_frame_info(&frame)
573 }
574}
575
576#[cfg(all(target_os = "macos", feature = "hwdevice"))]
578#[must_use]
579pub fn videotoolbox_get_format_callback()
580-> unsafe extern "C" fn(*mut ffi::AVCodecContext, *const ffi::AVPixelFormat) -> ffi::AVPixelFormat {
581 alpha2_videotoolbox_get_format
582}
583
584#[cfg(all(target_os = "macos", feature = "hwdevice"))]
589pub fn videotoolbox_frame_info(frame: &AVFrame) -> Result<VideoToolboxFrameInfo, BridgeError> {
590 read_videotoolbox_frame_info(frame)
591}
592
593#[cfg(all(target_os = "linux", feature = "hwdevice"))]
595pub const MAX_VULKAN_PLANES: usize = interop::ALPHA2_MAX_VULKAN_PLANES as usize;
596
597#[cfg(all(target_os = "linux", feature = "hwdevice"))]
599#[derive(Clone, Debug)]
600pub struct VulkanDeviceInfo {
601 pub instance: u64,
603 pub physical_device: u64,
605 pub device: u64,
607 pub graphics_queue_family: u32,
609 pub graphics_queue_count: u32,
611 pub decode_queue_family: u32,
613 pub decode_queue_count: u32,
615 pub decode_video_caps: u32,
617 pub encode_queue_family: u32,
619 pub encode_queue_count: u32,
621 pub encode_video_caps: u32,
623 pub core_features: [u32; 4],
625 pub timeline_semaphore: u32,
627 pub synchronization2: u32,
629 pub sampler_ycbcr_conversion: u32,
631 pub video_maintenance1: u32,
633 pub video_decode_vp9: u32,
635 pub instance_extensions: Vec<String>,
637 pub device_extensions: Vec<String>,
639}
640
641#[cfg(all(target_os = "linux", feature = "hwdevice"))]
642use interop::Alpha2VulkanDeviceInfo as VulkanDeviceDescriptor;
643
644#[cfg(all(target_os = "linux", feature = "hwdevice"))]
649pub struct VulkanQueueLock {
650 device: AVHWDeviceContext,
651 queue_family: u32,
652}
653
654#[cfg(all(target_os = "linux", feature = "hwdevice"))]
657unsafe impl Sync for VulkanQueueLock {}
658
659#[cfg(all(target_os = "linux", feature = "hwdevice"))]
660impl fmt::Debug for VulkanQueueLock {
661 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
662 formatter
663 .debug_struct("VulkanQueueLock")
664 .field("queue_family", &self.queue_family)
665 .finish_non_exhaustive()
666 }
667}
668
669#[cfg(all(target_os = "linux", feature = "hwdevice"))]
670impl VulkanQueueLock {
671 #[allow(clippy::cast_ptr_alignment)]
677 pub fn new(device: AVHWDeviceContext, queue_family: u32) -> Result<Self, BridgeError> {
678 let lockable = unsafe {
681 alpha2_vulkan_queue_lockable(
682 device.data.cast::<ffi::AVHWDeviceContext>().cast_const(),
683 queue_family,
684 )
685 };
686 if lockable == 0 {
687 return Err(BridgeError::InvalidInput(
688 "Vulkan queue lock needs a family of a device the bridge initialized",
689 ));
690 }
691 Ok(Self {
692 device,
693 queue_family,
694 })
695 }
696
697 #[must_use]
699 pub fn lock(&self) -> VulkanQueueGuard<'_> {
700 unsafe { self.raw_lock() };
702 VulkanQueueGuard {
703 lock: self,
704 _owning_thread: std::marker::PhantomData,
705 }
706 }
707
708 #[allow(clippy::cast_ptr_alignment)]
714 pub unsafe fn raw_lock(&self) {
715 unsafe {
718 alpha2_vulkan_queue_lock(
719 self.device.data.cast::<ffi::AVHWDeviceContext>(),
720 self.queue_family,
721 );
722 }
723 }
724
725 #[allow(clippy::cast_ptr_alignment)]
731 pub unsafe fn raw_unlock(&self) {
732 unsafe {
734 alpha2_vulkan_queue_unlock(
735 self.device.data.cast::<ffi::AVHWDeviceContext>(),
736 self.queue_family,
737 );
738 }
739 }
740}
741
742#[cfg(all(target_os = "linux", feature = "hwdevice"))]
744#[derive(Debug)]
745pub struct VulkanQueueGuard<'a> {
746 lock: &'a VulkanQueueLock,
747 _owning_thread: std::marker::PhantomData<*const ()>,
748}
749
750#[cfg(all(target_os = "linux", feature = "hwdevice"))]
751impl Drop for VulkanQueueGuard<'_> {
752 fn drop(&mut self) {
753 unsafe { self.lock.raw_unlock() };
756 }
757}
758
759#[cfg(all(target_os = "linux", feature = "hwdevice"))]
760unsafe extern "C" {
761 fn alpha2_vulkan_queue_lockable(
762 context: *const ffi::AVHWDeviceContext,
763 queue_family: u32,
764 ) -> i32;
765 fn alpha2_vulkan_queue_lock(context: *mut ffi::AVHWDeviceContext, queue_family: u32);
766 fn alpha2_vulkan_queue_unlock(context: *mut ffi::AVHWDeviceContext, queue_family: u32);
767 fn alpha2_vulkan_device_init(
768 context: *mut ffi::AVHWDeviceContext,
769 info: *const VulkanDeviceDescriptor,
770 ) -> i32;
771}
772
773#[cfg(all(target_os = "linux", feature = "hwdevice"))]
783#[allow(clippy::cast_ptr_alignment)]
784pub unsafe fn create_vulkan_device_context(
785 info: &VulkanDeviceInfo,
786) -> Result<AVHWDeviceContext, BridgeError> {
787 let instance = info
788 .instance_extensions
789 .iter()
790 .map(|s| CString::new(s.as_str()))
791 .collect::<Result<Vec<_>, _>>()
792 .map_err(|_| BridgeError::InvalidInput("Vulkan extension contains NUL"))?;
793 let device = info
794 .device_extensions
795 .iter()
796 .map(|s| CString::new(s.as_str()))
797 .collect::<Result<Vec<_>, _>>()
798 .map_err(|_| BridgeError::InvalidInput("Vulkan extension contains NUL"))?;
799 let instance_ptrs = instance.iter().map(|s| s.as_ptr()).collect::<Vec<_>>();
800 let device_ptrs = device.iter().map(|s| s.as_ptr()).collect::<Vec<_>>();
801 let raw = VulkanDeviceDescriptor {
802 instance: info.instance,
803 physical_device: info.physical_device,
804 device: info.device,
805 graphics_queue_family: info.graphics_queue_family,
806 graphics_queue_count: info.graphics_queue_count,
807 decode_queue_family: info.decode_queue_family,
808 decode_queue_count: info.decode_queue_count,
809 decode_video_caps: info.decode_video_caps,
810 encode_queue_family: info.encode_queue_family,
811 encode_queue_count: info.encode_queue_count,
812 encode_video_caps: info.encode_video_caps,
813 shader_image_gather_extended: info.core_features[0],
814 fragment_stores_and_atomics: info.core_features[1],
815 shader_int64: info.core_features[2],
816 vertex_pipeline_stores_and_atomics: info.core_features[3],
817 timeline_semaphore: info.timeline_semaphore,
818 synchronization2: info.synchronization2,
819 sampler_ycbcr_conversion: info.sampler_ycbcr_conversion,
820 video_maintenance1: info.video_maintenance1,
821 video_decode_vp9: info.video_decode_vp9,
822 instance_extensions: instance_ptrs.as_ptr(),
823 instance_extension_count: u32::try_from(instance_ptrs.len())
824 .map_err(|_| BridgeError::InvalidInput("too many Vulkan extensions"))?,
825 device_extensions: device_ptrs.as_ptr(),
826 device_extension_count: u32::try_from(device_ptrs.len())
827 .map_err(|_| BridgeError::InvalidInput("too many Vulkan extensions"))?,
828 };
829 let mut context = alloc_hwdevice(ffi::AV_HWDEVICE_TYPE_VULKAN, "Vulkan")?;
830 let device_context = context.data.cast::<ffi::AVHWDeviceContext>();
831 let result = unsafe { alpha2_vulkan_device_init(device_context, &raw const raw) };
834 if result < 0 {
835 return Err(BridgeError::FfmpegCode(result));
836 }
837 context.init().map_err(BridgeError::Ffmpeg)?;
838 Ok(context)
839}
840
841#[cfg(all(target_os = "linux", feature = "hwdevice"))]
845#[derive(Clone, Debug, Eq, PartialEq)]
846pub struct VulkanFrameInfo {
847 pub hardware_format: i32,
849 pub software_format: i32,
851 pub width: i32,
853 pub height: i32,
855 pub coded_width: i32,
857 pub coded_height: i32,
859 pub plane_count: u32,
861 pub sampled: bool,
863 pub instance: u64,
865 pub physical_device: u64,
867 pub device: u64,
869 pub images: [u64; MAX_VULKAN_PLANES],
871 pub formats: [u32; MAX_VULKAN_PLANES],
873 pub layouts: [u32; MAX_VULKAN_PLANES],
875 pub access: [u32; MAX_VULKAN_PLANES],
877 pub semaphores: [u64; MAX_VULKAN_PLANES],
879 pub semaphore_values: [u64; MAX_VULKAN_PLANES],
881 pub queue_families: [u32; MAX_VULKAN_PLANES],
883}
884
885#[cfg(all(target_os = "linux", feature = "hwdevice"))]
887pub type VulkanFrameDescriptor = interop::Alpha2VulkanFrameInfo;
888
889#[cfg(all(target_os = "linux", feature = "hwdevice"))]
894#[repr(transparent)]
895#[derive(Default)]
896pub struct VulkanFrameAccess(interop::Alpha2VulkanFrameAccess);
897
898#[cfg(all(target_os = "linux", feature = "hwdevice"))]
899impl VulkanFrameAccess {
900 #[must_use]
902 pub const fn frame(&self) -> &VulkanFrameDescriptor {
903 &self.0.frame
904 }
905
906 pub const fn frame_mut(&mut self) -> &mut VulkanFrameDescriptor {
908 &mut self.0.frame
909 }
910
911 #[must_use]
913 pub const fn allocator(&self) -> u64 {
914 self.0.allocator
915 }
916
917 #[must_use]
919 pub const fn queue_family(&self) -> u32 {
920 self.0.queue_family
921 }
922
923 #[must_use]
925 pub const fn layer_count(&self) -> u32 {
926 self.0.layer_count
927 }
928}
929
930#[cfg(all(target_os = "linux", feature = "hwdevice"))]
931unsafe extern "C" {
932 fn alpha2_vulkan_get_format(
933 context: *mut ffi::AVCodecContext,
934 formats: *const ffi::AVPixelFormat,
935 ) -> ffi::AVPixelFormat;
936 fn alpha2_vulkan_frame_info(
937 frame: *const ffi::AVFrame,
938 output: *mut VulkanFrameDescriptor,
939 ) -> i32;
940 fn alpha2_vulkan_frames_add_usage(frames: *mut ffi::AVBufferRef, usage: u32) -> i32;
941 fn alpha2_vulkan_frames_usage(frames: *const ffi::AVBufferRef) -> u32;
942 fn alpha2_vulkan_frame_access_begin(
943 frame: *mut ffi::AVFrame,
944 output: *mut VulkanFrameAccess,
945 ) -> i32;
946 fn alpha2_vulkan_frame_access_end(
947 begun: *const interop::Alpha2VulkanFrameAccess,
948 updated: *const VulkanFrameAccess,
949 );
950}
951
952#[cfg(all(target_os = "linux", feature = "hwdevice"))]
954const fn vulkan_frame_refusal(status: i32) -> BridgeError {
955 BridgeError::InvalidInput(match status {
956 -1 => "frame is not a Vulkan hardware frame",
957 -2 => "Vulkan frame has no hardware frames or device context",
958 -3 => "Vulkan frame has no images",
959 -4 => "Vulkan frames context has no frame or queue locks, or no layers",
960 -5 => "Vulkan device has no graphics or compute queue",
961 _ => "Vulkan frame could not be read",
962 })
963}
964
965#[cfg(all(target_os = "linux", feature = "hwdevice"))]
967pub struct VulkanFrameLease {
968 frame: Mutex<AVFrame>,
969}
970
971#[cfg(all(target_os = "linux", feature = "hwdevice"))]
972impl fmt::Debug for VulkanFrameLease {
973 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
974 formatter
975 .debug_struct("VulkanFrameLease")
976 .finish_non_exhaustive()
977 }
978}
979
980#[cfg(all(target_os = "linux", feature = "hwdevice"))]
981impl VulkanFrameLease {
982 #[must_use]
984 pub fn new(frame: AVFrame) -> Self {
985 Self {
986 frame: Mutex::new(frame),
987 }
988 }
989
990 pub fn download(&self) -> Result<DecodedFrame, BridgeError> {
995 let frame = self
996 .frame
997 .lock()
998 .unwrap_or_else(std::sync::PoisonError::into_inner);
999 download_hardware_frame(&frame).map(|frame| DecodedFrame {
1000 frame,
1001 written: true,
1002 })
1003 }
1004
1005 pub fn info(&self) -> Result<VulkanFrameInfo, BridgeError> {
1010 let frame = self
1011 .frame
1012 .lock()
1013 .unwrap_or_else(std::sync::PoisonError::into_inner);
1014 vulkan_frame_info(&frame)
1015 }
1016
1017 pub fn lock_access(&self) -> Result<VulkanFrameAccessGuard<'_>, BridgeError> {
1022 let mut frame = self
1023 .frame
1024 .lock()
1025 .unwrap_or_else(std::sync::PoisonError::into_inner);
1026 let mut access = VulkanFrameAccess::default();
1027 let result =
1029 unsafe { alpha2_vulkan_frame_access_begin(frame.as_mut_ptr(), &raw mut access) };
1030 if result != 0 {
1031 return Err(vulkan_frame_refusal(result));
1032 }
1033 Ok(VulkanFrameAccessGuard {
1034 _frame: frame,
1035 begun: access.0,
1036 access,
1037 committed: false,
1038 })
1039 }
1040}
1041
1042#[cfg(all(target_os = "linux", feature = "hwdevice"))]
1055pub struct VulkanFrameAccessGuard<'a> {
1056 _frame: MutexGuard<'a, AVFrame>,
1057 begun: interop::Alpha2VulkanFrameAccess,
1059 access: VulkanFrameAccess,
1060 committed: bool,
1061}
1062
1063#[cfg(all(target_os = "linux", feature = "hwdevice"))]
1064impl VulkanFrameAccessGuard<'_> {
1065 #[must_use]
1067 pub const fn access(&self) -> &VulkanFrameAccess {
1068 &self.access
1069 }
1070
1071 #[must_use]
1078 pub const unsafe fn access_mut(&mut self) -> &mut VulkanFrameAccess {
1079 &mut self.access
1080 }
1081
1082 pub fn commit(&mut self) {
1084 self.committed = true;
1085 }
1086}
1087
1088#[cfg(all(target_os = "linux", feature = "hwdevice"))]
1089impl Drop for VulkanFrameAccessGuard<'_> {
1090 fn drop(&mut self) {
1091 let updated = if self.committed {
1092 &raw const self.access
1093 } else {
1094 ptr::null()
1095 };
1096 unsafe { alpha2_vulkan_frame_access_end(&raw const self.begun, updated) };
1099 }
1100}
1101
1102#[cfg(all(target_os = "linux", feature = "hwdevice"))]
1107pub struct VulkanEncodeFrameAccess<'a> {
1108 frame: &'a mut AVFrame,
1109 begun: interop::Alpha2VulkanFrameAccess,
1111 access: VulkanFrameAccess,
1112 committed: bool,
1113 _owning_thread: std::marker::PhantomData<*const ()>,
1114}
1115
1116#[cfg(all(target_os = "linux", feature = "hwdevice"))]
1117impl<'a> VulkanEncodeFrameAccess<'a> {
1118 pub fn retain_frame(&self) -> Result<AVFrame, BridgeError> {
1123 let raw = unsafe { ffi::av_frame_clone(self.frame.as_ptr()) };
1125 let raw = std::ptr::NonNull::new(raw)
1126 .ok_or(BridgeError::InvalidInput("cannot retain encode frame"))?;
1127 Ok(unsafe { AVFrame::from_raw(raw) })
1129 }
1130
1131 pub fn begin(frame: &'a mut AVFrame) -> Result<Self, BridgeError> {
1136 let mut access = VulkanFrameAccess::default();
1137 let result =
1139 unsafe { alpha2_vulkan_frame_access_begin(frame.as_mut_ptr(), &raw mut access) };
1140 if result != 0 {
1141 return Err(vulkan_frame_refusal(result));
1142 }
1143 Ok(Self {
1144 frame,
1145 begun: access.0,
1146 access,
1147 committed: false,
1148 _owning_thread: std::marker::PhantomData,
1149 })
1150 }
1151
1152 #[must_use]
1154 pub const fn access(&self) -> &VulkanFrameAccess {
1155 &self.access
1156 }
1157
1158 #[must_use]
1164 pub const unsafe fn access_mut(&mut self) -> &mut VulkanFrameAccess {
1165 &mut self.access
1166 }
1167
1168 pub fn commit(&mut self) {
1170 self.committed = true;
1171 }
1172}
1173
1174#[cfg(all(target_os = "linux", feature = "hwdevice"))]
1175impl Drop for VulkanEncodeFrameAccess<'_> {
1176 fn drop(&mut self) {
1177 let updated = if self.committed {
1178 &raw const self.access
1179 } else {
1180 ptr::null()
1181 };
1182 unsafe { alpha2_vulkan_frame_access_end(&raw const self.begun, updated) };
1185 }
1186}
1187
1188#[cfg(all(target_os = "linux", feature = "hwdevice"))]
1190#[must_use]
1191pub fn vulkan_get_format_callback()
1192-> unsafe extern "C" fn(*mut ffi::AVCodecContext, *const ffi::AVPixelFormat) -> ffi::AVPixelFormat {
1193 alpha2_vulkan_get_format
1194}
1195
1196#[cfg(all(target_os = "linux", feature = "hwdevice"))]
1203pub fn vulkan_frame_info(frame: &AVFrame) -> Result<VulkanFrameInfo, BridgeError> {
1204 let mut raw = VulkanFrameDescriptor::default();
1205 let result = unsafe { alpha2_vulkan_frame_info(frame.as_ptr(), &raw mut raw) };
1208 if result != 0 {
1209 return Err(vulkan_frame_refusal(result));
1210 }
1211 Ok(VulkanFrameInfo {
1212 hardware_format: raw.hardware_format,
1213 software_format: raw.software_format,
1214 width: raw.width,
1215 height: raw.height,
1216 coded_width: raw.coded_width,
1217 coded_height: raw.coded_height,
1218 plane_count: raw.plane_count,
1219 sampled: raw.sampled != 0,
1220 instance: raw.instance,
1221 physical_device: raw.physical_device,
1222 device: raw.device,
1223 images: raw.images,
1224 formats: raw.formats,
1225 layouts: raw.layouts,
1226 access: raw.access,
1227 semaphores: raw.semaphores,
1228 semaphore_values: raw.semaphore_values,
1229 queue_families: raw.queue_families,
1230 })
1231}
1232
1233#[cfg(all(any(target_os = "linux", target_os = "windows"), feature = "hwdevice"))]
1235fn hwdevice_available(kind: ffi::AVHWDeviceType) -> bool {
1236 let mut each = ffi::AV_HWDEVICE_TYPE_NONE;
1237 loop {
1238 each = unsafe { ffi::av_hwdevice_iterate_types(each) };
1240 if each == ffi::AV_HWDEVICE_TYPE_NONE {
1241 return false;
1242 }
1243 if each == kind {
1244 return true;
1245 }
1246 }
1247}
1248
1249#[cfg(all(any(target_os = "linux", target_os = "windows"), feature = "hwdevice"))]
1251fn alloc_hwdevice(
1252 kind: ffi::AVHWDeviceType,
1253 name: &'static str,
1254) -> Result<AVHWDeviceContext, BridgeError> {
1255 check_hardware_interop()?;
1256 if !hwdevice_available(kind) {
1257 return Err(BridgeError::MissingHwDevice(name));
1258 }
1259 Ok(AVHWDeviceContext::alloc(kind))
1260}
1261
1262#[derive(Debug)]
1264#[non_exhaustive]
1265pub enum BridgeError {
1266 InvalidInput(&'static str),
1268 Ffmpeg(RsmpegError),
1270 FfmpegCode(i32),
1273 NativeGpu(NativeGpuFailure),
1275 MissingHwDevice(&'static str),
1277 RuntimeMismatch(&'static str),
1280 UnsupportedSurface(String),
1282}
1283
1284#[derive(Copy, Clone, Debug, Eq, PartialEq)]
1286pub struct YuvPlaneLayout {
1287 pub chroma_shift_x: u8,
1289 pub chroma_shift_y: u8,
1291 pub bit_depth: u8,
1293 pub interleaved_chroma: bool,
1295 pub msb_aligned: bool,
1297}
1298
1299impl YuvPlaneLayout {
1300 #[must_use]
1302 pub const fn bytes_per_sample(self) -> u32 {
1303 if self.bit_depth > 8 { 2 } else { 1 }
1304 }
1305}
1306
1307#[derive(Clone, Debug, Eq, PartialEq)]
1309pub struct FramePlane {
1310 pub bytes: bytes::Bytes,
1312 pub stride: u32,
1314 pub width: u32,
1316 pub height: u32,
1318}
1319
1320#[derive(Clone, Debug, Eq, PartialEq)]
1322pub struct YuvPlanes {
1323 pub layout: YuvPlaneLayout,
1325 pub width: u32,
1327 pub height: u32,
1329 pub planes: Vec<FramePlane>,
1331}
1332
1333impl fmt::Display for BridgeError {
1334 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1335 match self {
1336 Self::InvalidInput(context) => {
1337 write!(formatter, "invalid media buffer input: {context}")
1338 }
1339 Self::Ffmpeg(error) => write!(formatter, "{error}"),
1340 Self::FfmpegCode(code) => write!(formatter, "{}", RsmpegError::AVError(*code)),
1341 Self::NativeGpu(failure) => write!(formatter, "{failure}"),
1342 Self::MissingHwDevice(kind) => {
1343 write!(formatter, "the loaded FFmpeg has no {kind} hardware device")
1344 }
1345 Self::UnsupportedSurface(description) => formatter.write_str(description),
1346 Self::RuntimeMismatch(reason) => formatter.write_str(reason),
1347 }
1348 }
1349}
1350
1351fn configure_colorspace(
1356 context: &mut SwsContext,
1357 source_coefficients: i32,
1358 source_full_range: bool,
1359 destination_coefficients: i32,
1360 destination_full_range: bool,
1361) -> Result<(), BridgeError> {
1362 let result = unsafe {
1365 ffi::sws_setColorspaceDetails(
1366 context.as_mut_ptr(),
1367 ffi::sws_getCoefficients(source_coefficients),
1368 i32::from(source_full_range),
1369 ffi::sws_getCoefficients(destination_coefficients),
1370 i32::from(destination_full_range),
1371 0,
1372 1 << 16,
1373 1 << 16,
1374 )
1375 };
1376 if result < 0 {
1377 Err(BridgeError::FfmpegCode(result))
1378 } else {
1379 Ok(())
1380 }
1381}
1382
1383const SCALER_DESTINATION_ALIGN: i32 = 64;
1385
1386pub struct PackedScaler {
1392 context: SwsContext,
1393 width: i32,
1394 height: i32,
1395 source: ffi::AVPixelFormat,
1396 target: ffi::AVPixelFormat,
1397 destination: AVFrame,
1398}
1399
1400impl PackedScaler {
1401 pub fn new(
1407 width: i32,
1408 height: i32,
1409 source: ffi::AVPixelFormat,
1410 target: ffi::AVPixelFormat,
1411 ) -> Result<Self, BridgeError> {
1412 if width <= 0 || height <= 0 {
1413 return Err(BridgeError::InvalidInput("scaler size is not positive"));
1414 }
1415 let context = SwsContext::get_context(
1416 width,
1417 height,
1418 source,
1419 width,
1420 height,
1421 target,
1422 scaler_flags(width, height, source, target),
1423 None,
1424 None,
1425 None,
1426 )
1427 .ok_or(BridgeError::InvalidInput(
1428 "libswscale has no conversion between these pixel formats",
1429 ))?;
1430 let destination = padded_zeroed_frame(width, height, target)?;
1431 Ok(Self {
1432 context,
1433 width,
1434 height,
1435 source,
1436 target,
1437 destination,
1438 })
1439 }
1440
1441 pub fn configure_colorspace(
1447 &mut self,
1448 source_coefficients: i32,
1449 source_full_range: bool,
1450 destination_coefficients: i32,
1451 destination_full_range: bool,
1452 ) -> Result<(), BridgeError> {
1453 configure_colorspace(
1454 &mut self.context,
1455 source_coefficients,
1456 source_full_range,
1457 destination_coefficients,
1458 destination_full_range,
1459 )
1460 }
1461
1462 pub fn convert(&mut self, frame: &AVFrame) -> Result<Vec<u8>, BridgeError> {
1469 self.check_source(frame)?;
1470 scale_into(&mut self.context, frame, &mut self.destination, self.height)?;
1471 let size =
1473 unsafe { ffi::av_image_get_buffer_size(self.target, self.width, self.height, 1) };
1474 let len = usize::try_from(size).map_err(|_| BridgeError::FfmpegCode(size))?;
1475 let mut packed = vec![0_u8; len];
1476 let copied = unsafe {
1479 ffi::av_image_copy_to_buffer(
1480 packed.as_mut_ptr(),
1481 size,
1482 self.destination.data.as_ptr().cast(),
1483 self.destination.linesize.as_ptr(),
1484 self.target,
1485 self.width,
1486 self.height,
1487 1,
1488 )
1489 };
1490 if copied < 0 {
1491 return Err(BridgeError::FfmpegCode(copied));
1492 }
1493 Ok(packed)
1494 }
1495
1496 pub fn convert_to_frame(&mut self, frame: &AVFrame) -> Result<AVFrame, BridgeError> {
1502 self.check_source(frame)?;
1503 let mut output = padded_zeroed_frame(self.width, self.height, self.target)?;
1504 scale_into(&mut self.context, frame, &mut output, self.height)?;
1505 Ok(output)
1506 }
1507
1508 fn check_source(&self, frame: &AVFrame) -> Result<(), BridgeError> {
1509 if frame.width != self.width || frame.height != self.height || frame.format != self.source {
1510 return Err(BridgeError::InvalidInput(
1511 "frame size or format differs from the scaler's",
1512 ));
1513 }
1514 Ok(())
1515 }
1516}
1517
1518impl fmt::Debug for PackedScaler {
1519 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1520 formatter
1521 .debug_struct("PackedScaler")
1522 .field("width", &self.width)
1523 .field("height", &self.height)
1524 .field("source", &self.source)
1525 .field("target", &self.target)
1526 .finish_non_exhaustive()
1527 }
1528}
1529
1530fn scaler_flags(
1533 width: i32,
1534 height: i32,
1535 source: ffi::AVPixelFormat,
1536 target: ffi::AVPixelFormat,
1537) -> ffi::SwsFlags {
1538 let unscaled_rgb_source = matches!(
1539 source,
1540 ffi::AV_PIX_FMT_YUV420P | ffi::AV_PIX_FMT_YUV422P | ffi::AV_PIX_FMT_YUVA420P
1541 );
1542 let rgb_target = unsafe { ffi::av_pix_fmt_desc_get(target).as_ref() }
1544 .is_some_and(|descriptor| descriptor.flags & u64::from(ffi::AV_PIX_FMT_FLAG_RGB) != 0);
1545 if unscaled_rgb_source && rgb_target && height % 2 == 0 && width % 2 == 1 {
1546 ffi::SWS_BILINEAR | ffi::SWS_ACCURATE_RND
1547 } else {
1548 ffi::SWS_BILINEAR
1549 }
1550}
1551
1552fn padded_zeroed_frame(
1554 width: i32,
1555 height: i32,
1556 format: ffi::AVPixelFormat,
1557) -> Result<AVFrame, BridgeError> {
1558 let mut frame = AVFrame::new();
1559 frame.set_format(format);
1560 frame.set_width(width);
1561 frame.set_height(height);
1562 frame.get_buffer(SCALER_DESTINATION_ALIGN)?;
1563 for buffer in frame.buf {
1564 if let Some(buffer) = unsafe { buffer.as_ref() } {
1566 unsafe { ptr::write_bytes(buffer.data, 0, buffer.size) };
1568 }
1569 }
1570 Ok(frame)
1571}
1572
1573fn scale_into(
1576 context: &mut SwsContext,
1577 source: &AVFrame,
1578 destination: &mut AVFrame,
1579 height: i32,
1580) -> Result<(), BridgeError> {
1581 let rows = unsafe {
1585 ffi::sws_scale(
1586 context.as_mut_ptr(),
1587 source.data.as_ptr().cast(),
1588 source.linesize.as_ptr(),
1589 0,
1590 height,
1591 destination.data.as_ptr().cast(),
1592 destination.linesize.as_ptr(),
1593 )
1594 };
1595 if rows < 0 {
1596 return Err(BridgeError::FfmpegCode(rows));
1597 }
1598 if rows != height {
1599 return Err(BridgeError::InvalidInput(
1600 "libswscale wrote fewer rows than the frame has",
1601 ));
1602 }
1603 Ok(())
1604}
1605
1606impl Error for BridgeError {}
1607
1608impl BridgeError {
1609 #[must_use]
1611 pub const fn gpu_fault(&self) -> Option<GpuFault> {
1612 match self {
1613 Self::NativeGpu(failure) => failure.fault,
1614 _ => None,
1615 }
1616 }
1617}
1618
1619#[derive(Copy, Clone, Debug, Eq, PartialEq)]
1621pub enum NativeGpuBackend {
1622 Vulkan,
1624 D3D12,
1626 Metal,
1628}
1629
1630impl NativeGpuBackend {
1631 #[must_use]
1633 pub const fn as_str(self) -> &'static str {
1634 match self {
1635 Self::Vulkan => "Vulkan",
1636 Self::D3D12 => "D3D12",
1637 Self::Metal => "Metal",
1638 }
1639 }
1640}
1641
1642#[derive(Copy, Clone, Debug, Eq, PartialEq)]
1646pub struct NativeGpuFailure {
1647 pub backend: NativeGpuBackend,
1649 pub operation: &'static str,
1651 pub step: &'static str,
1653 pub status: i32,
1655 pub fault: Option<GpuFault>,
1657}
1658
1659impl fmt::Display for NativeGpuFailure {
1660 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1661 let Self {
1662 backend,
1663 operation,
1664 step,
1665 status,
1666 ..
1667 } = *self;
1668 let backend = backend.as_str();
1669 if self.fault == Some(GpuFault::Timeout) {
1671 write!(
1672 formatter,
1673 "{backend} {operation} ({step}): {}",
1674 alpha2_media::GPU_TIMEOUT_MESSAGE
1675 )?;
1676 } else {
1677 write!(formatter, "{backend} {operation} failed at {step}")?;
1678 }
1679 if status == 0 {
1680 return Ok(());
1681 }
1682 match self.backend {
1683 NativeGpuBackend::Vulkan => write!(formatter, " (VkResult {status})"),
1684 NativeGpuBackend::D3D12 => write!(formatter, " (HRESULT {status:#010X})"),
1685 NativeGpuBackend::Metal => write!(formatter, " (CVReturn {status})"),
1686 }
1687 }
1688}
1689
1690impl From<RsmpegError> for BridgeError {
1691 fn from(error: RsmpegError) -> Self {
1692 Self::Ffmpeg(error)
1693 }
1694}
1695
1696unsafe extern "C" {
1697 fn alpha2_ffmpeg_shim_avutil_version() -> std::ffi::c_uint;
1698}
1699
1700#[must_use]
1706pub fn hardware_interop_refusal() -> Option<&'static str> {
1707 static REFUSAL: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
1708 REFUSAL
1709 .get_or_init(|| {
1710 let compiled = unsafe { alpha2_ffmpeg_shim_avutil_version() };
1712 let loaded = unsafe { ffi::avutil_version() };
1714 interop_version_refusal(compiled, loaded)
1715 })
1716 .as_deref()
1717}
1718
1719fn interop_version_refusal(compiled: u32, loaded: u32) -> Option<String> {
1721 let version = |value: u32| format!("{}.{}.{}", value >> 16, (value >> 8) & 0xff, value & 0xff);
1722 (compiled >> 8 != loaded >> 8).then(|| {
1723 format!(
1724 "hardware interop is off: the FFmpeg shims were compiled for libavutil {} and the \
1725 loaded libavutil is {}",
1726 version(compiled),
1727 version(loaded)
1728 )
1729 })
1730}
1731
1732pub fn check_hardware_interop() -> Result<(), BridgeError> {
1737 hardware_interop_refusal().map_or(Ok(()), |reason| Err(BridgeError::RuntimeMismatch(reason)))
1738}
1739
1740#[must_use]
1742pub fn runtime_version() -> String {
1743 unsafe { CStr::from_ptr(ffi::av_version_info()) }
1745 .to_string_lossy()
1746 .into_owned()
1747}
1748
1749#[must_use]
1751pub fn runtime_configuration() -> String {
1752 unsafe { CStr::from_ptr(ffi::avcodec_configuration()) }
1754 .to_string_lossy()
1755 .into_owned()
1756}
1757
1758#[must_use]
1760pub fn muxer_accepts_codec(format: &CStr, codec: ffi::AVCodecID) -> Option<bool> {
1761 let muxer = unsafe { ffi::av_guess_format(format.as_ptr(), ptr::null(), ptr::null()) };
1764 if muxer.is_null() {
1765 return None;
1766 }
1767 let accepted = unsafe {
1769 ffi::avformat_query_codec(
1770 muxer,
1771 codec,
1772 i32::try_from(ffi::FF_COMPLIANCE_NORMAL).unwrap_or(0),
1773 )
1774 };
1775 Some(accepted == 1)
1776}
1777
1778#[derive(Copy, Clone, Debug, Eq, PartialEq)]
1780pub struct SampleProfile {
1781 pub is_yuv_420: bool,
1783 pub bit_depth: u8,
1785 pub has_alpha: bool,
1787 pub is_rgb: bool,
1789}
1790
1791#[must_use]
1795pub fn sample_profile(format: ffi::AVPixelFormat) -> Option<SampleProfile> {
1796 if format == ffi::AV_PIX_FMT_NONE {
1797 return None;
1798 }
1799 let descriptor = unsafe { ffi::av_pix_fmt_desc_get(format).as_ref() }?;
1801 let components = usize::from(descriptor.nb_components.min(4));
1802 Some(SampleProfile {
1803 is_yuv_420: descriptor.log2_chroma_w == 1
1804 && descriptor.log2_chroma_h == 1
1805 && descriptor.nb_components >= 3
1806 && descriptor.flags & u64::from(ffi::AV_PIX_FMT_FLAG_RGB) == 0,
1807 bit_depth: descriptor.comp[..components]
1808 .iter()
1809 .map(|component| u8::try_from(component.depth).unwrap_or(u8::MAX))
1810 .max()
1811 .unwrap_or(8),
1812 has_alpha: descriptor.flags & u64::from(ffi::AV_PIX_FMT_FLAG_ALPHA) != 0,
1813 is_rgb: descriptor.flags & u64::from(ffi::AV_PIX_FMT_FLAG_RGB | ffi::AV_PIX_FMT_FLAG_PAL)
1814 != 0,
1815 })
1816}
1817
1818#[must_use]
1820pub fn pixel_format_name(format: ffi::AVPixelFormat) -> Option<String> {
1821 let name = unsafe { ffi::av_get_pix_fmt_name(format) };
1823 if name.is_null() {
1824 None
1825 } else {
1826 Some(
1828 unsafe { CStr::from_ptr(name) }
1829 .to_string_lossy()
1830 .into_owned(),
1831 )
1832 }
1833}
1834
1835pub fn open_codec_context(
1844 context: &mut rsmpeg::avcodec::AVCodecContext,
1845 options: rsmpeg::avutil::AVDictionary,
1846) -> Result<Option<rsmpeg::avutil::AVDictionary>, RsmpegError> {
1847 let _media_foundation = media_foundation_lock(context);
1848 let mut dictionary = options.into_raw().as_ptr();
1849 let status = unsafe {
1852 ffi::avcodec_open2(
1853 context.as_mut_ptr(),
1854 ptr::null_mut(),
1855 std::ptr::addr_of_mut!(dictionary),
1856 )
1857 };
1858 let unused = ptr::NonNull::new(dictionary)
1860 .map(|dictionary| unsafe { rsmpeg::avutil::AVDictionary::from_raw(dictionary) });
1861 if status < 0 {
1862 return Err(RsmpegError::from(status));
1863 }
1864 Ok(unused)
1865}
1866
1867fn media_foundation_lock(
1872 context: &rsmpeg::avcodec::AVCodecContext,
1873) -> Option<std::sync::MutexGuard<'static, ()>> {
1874 static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
1875 if !cfg!(target_os = "windows") || context.codec.is_null() {
1876 return None;
1877 }
1878 let name = unsafe { CStr::from_ptr((*context.codec).name) };
1880 name.to_bytes().ends_with(b"_mf").then(|| {
1881 LOCK.lock()
1882 .unwrap_or_else(std::sync::PoisonError::into_inner)
1883 })
1884}
1885
1886pub struct EncoderContext(Option<rsmpeg::avcodec::AVCodecContext>);
1890
1891impl From<rsmpeg::avcodec::AVCodecContext> for EncoderContext {
1892 fn from(context: rsmpeg::avcodec::AVCodecContext) -> Self {
1893 Self(Some(context))
1894 }
1895}
1896
1897impl std::ops::Deref for EncoderContext {
1898 type Target = rsmpeg::avcodec::AVCodecContext;
1899
1900 fn deref(&self) -> &Self::Target {
1901 self.0.as_ref().expect("taken only when dropped")
1902 }
1903}
1904
1905impl std::ops::DerefMut for EncoderContext {
1906 fn deref_mut(&mut self) -> &mut Self::Target {
1907 self.0.as_mut().expect("taken only when dropped")
1908 }
1909}
1910
1911impl Drop for EncoderContext {
1912 fn drop(&mut self) {
1913 if let Some(context) = self.0.take() {
1914 let _media_foundation = media_foundation_lock(&context);
1915 drop(context);
1916 }
1917 }
1918}
1919
1920#[cfg(all(target_os = "linux", feature = "hwdevice"))]
1925pub const VULKAN_ENCODE_INPUT_USAGE: u32 = 0x0000_0001 | 0x0000_0002 | 0x0000_0004 | 0x0000_4000;
1926
1927#[cfg(all(target_os = "linux", feature = "hwdevice"))]
1937pub fn vulkan_frames_add_usage(
1938 frames: &mut rsmpeg::avutil::AVHWFramesContext,
1939 usage: u32,
1940) -> Result<(), BridgeError> {
1941 let status = unsafe { alpha2_vulkan_frames_add_usage(frames.as_mut_ptr(), usage) };
1943 if status < 0 {
1944 return Err(BridgeError::FfmpegCode(status));
1945 }
1946 Ok(())
1947}
1948
1949#[cfg(all(target_os = "linux", feature = "hwdevice"))]
1951#[must_use]
1952pub fn vulkan_frames_usage(frames: &rsmpeg::avutil::AVHWFramesContext) -> u32 {
1953 unsafe { alpha2_vulkan_frames_usage(frames.as_ptr()) }
1955}
1956
1957#[must_use]
1959pub fn packet_bytes(packet: &rsmpeg::avcodec::AVPacket) -> Vec<u8> {
1960 let size = usize::try_from(packet.size).unwrap_or(0);
1961 if packet.data.is_null() || size == 0 {
1962 return Vec::new();
1963 }
1964 unsafe { slice::from_raw_parts(packet.data.cast_const(), size) }.to_vec()
1966}
1967
1968#[must_use]
1979pub fn codec_parameters_extradata(parameters: &rsmpeg::avcodec::AVCodecParameters) -> Vec<u8> {
1980 let size = usize::try_from(parameters.extradata_size).unwrap_or(0);
1981 if parameters.extradata.is_null() || size == 0 {
1982 return Vec::new();
1983 }
1984 unsafe { slice::from_raw_parts(parameters.extradata.cast_const(), size) }.to_vec()
1986}
1987
1988#[must_use]
1990pub fn packet_has_side_data(
1991 packet: &rsmpeg::avcodec::AVPacket,
1992 kind: ffi::AVPacketSideDataType,
1993) -> bool {
1994 let mut size = 0_usize;
1995 !unsafe { ffi::av_packet_get_side_data(packet.as_ptr(), kind, &raw mut size) }.is_null()
1997}
1998
1999#[must_use]
2004pub fn take_packet_side_data(
2005 packet: &mut rsmpeg::avcodec::AVPacket,
2006 kind: ffi::AVPacketSideDataType,
2007) -> Option<Vec<u8>> {
2008 let mut size = 0_usize;
2009 let data = unsafe { ffi::av_packet_get_side_data(packet.as_ptr(), kind, &raw mut size) };
2011 if data.is_null() {
2012 return None;
2013 }
2014 let bytes = unsafe { slice::from_raw_parts(data, size) }.to_vec();
2016 let packet = packet.as_mut_ptr();
2017 unsafe {
2020 ffi::av_packet_side_data_remove(
2021 (*packet).side_data,
2022 &raw mut (*packet).side_data_elems,
2023 kind,
2024 );
2025 }
2026 Some(bytes)
2027}
2028
2029pub fn set_frame_color(
2032 frame: &mut rsmpeg::avutil::AVFrame,
2033 (primaries, transfer, space, range): (
2034 ffi::AVColorPrimaries,
2035 ffi::AVColorTransferCharacteristic,
2036 ffi::AVColorSpace,
2037 ffi::AVColorRange,
2038 ),
2039) {
2040 let raw = frame.as_mut_ptr();
2041 unsafe {
2043 (*raw).color_primaries = primaries;
2044 (*raw).color_trc = transfer;
2045 (*raw).colorspace = space;
2046 (*raw).color_range = range;
2047 }
2048}
2049
2050pub fn set_packet_side_data(
2055 packet: &mut rsmpeg::avcodec::AVPacket,
2056 kind: ffi::AVPacketSideDataType,
2057 data: &[u8],
2058) -> Result<(), BridgeError> {
2059 let _ = take_packet_side_data(packet, kind);
2060 let entry = unsafe { ffi::av_packet_new_side_data(packet.as_mut_ptr(), kind, data.len()) };
2063 if entry.is_null() {
2064 return Err(BridgeError::FfmpegCode(ffi::AVERROR(ffi::ENOMEM)));
2065 }
2066 unsafe { ptr::copy_nonoverlapping(data.as_ptr(), entry, data.len()) };
2068 Ok(())
2069}
2070
2071pub fn set_output_stream_extradata(
2079 output: &mut rsmpeg::avformat::AVFormatContextOutput,
2080 index: usize,
2081 data: &[u8],
2082) -> Result<(), BridgeError> {
2083 let parameters = output
2084 .streams()
2085 .get(index)
2086 .ok_or(BridgeError::InvalidInput("the output has no such stream"))?
2087 .codecpar;
2088 let size = i32::try_from(data.len())
2089 .map_err(|_| BridgeError::InvalidInput("extradata is larger than FFmpeg holds"))?;
2090 let padding = usize::try_from(ffi::AV_INPUT_BUFFER_PADDING_SIZE).unwrap_or(64);
2091 let buffer = unsafe { ffi::av_mallocz(data.len() + padding) }.cast::<u8>();
2093 if buffer.is_null() {
2094 return Err(BridgeError::FfmpegCode(ffi::AVERROR(ffi::ENOMEM)));
2095 }
2096 unsafe { ptr::copy_nonoverlapping(data.as_ptr(), buffer, data.len()) };
2098 unsafe {
2101 ffi::av_freep((&raw mut (*parameters).extradata).cast());
2102 (*parameters).extradata = buffer;
2103 (*parameters).extradata_size = size;
2104 }
2105 Ok(())
2106}
2107
2108pub fn packet_from_bytes(data: &[u8]) -> Result<rsmpeg::avcodec::AVPacket, BridgeError> {
2113 let size = i32::try_from(data.len())
2114 .map_err(|_| BridgeError::InvalidInput("a packet is larger than FFmpeg holds"))?;
2115 let mut packet = rsmpeg::avcodec::AVPacket::new();
2116 let status = unsafe { ffi::av_new_packet(packet.as_mut_ptr(), size) };
2118 if status < 0 {
2119 return Err(BridgeError::FfmpegCode(status));
2120 }
2121 if !data.is_empty() {
2122 unsafe { ptr::copy_nonoverlapping(data.as_ptr(), packet.data, data.len()) };
2124 }
2125 Ok(packet)
2126}
2127
2128pub fn shrink_packet_to(
2133 packet: &mut rsmpeg::avcodec::AVPacket,
2134 data: &[u8],
2135) -> Result<(), BridgeError> {
2136 let size = i32::try_from(data.len())
2137 .ok()
2138 .filter(|size| *size <= packet.size)
2139 .ok_or(BridgeError::InvalidInput("a packet cannot grow in place"))?;
2140 let status = unsafe { ffi::av_packet_make_writable(packet.as_mut_ptr()) };
2142 if status < 0 {
2143 return Err(BridgeError::FfmpegCode(status));
2144 }
2145 if !data.is_empty() {
2146 unsafe { ptr::copy_nonoverlapping(data.as_ptr(), packet.data, data.len()) };
2148 }
2149 unsafe { ffi::av_shrink_packet(packet.as_mut_ptr(), size) };
2151 Ok(())
2152}
2153
2154#[derive(Clone, Copy, Debug)]
2156pub struct VideoStreamDescription<'a> {
2157 pub codec_id: ffi::AVCodecID,
2159 pub width: i32,
2161 pub height: i32,
2163 pub pixel_format: ffi::AVPixelFormat,
2165 pub profile: i32,
2167 pub level: i32,
2169 pub color_primaries: ffi::AVColorPrimaries,
2171 pub color_trc: ffi::AVColorTransferCharacteristic,
2173 pub color_space: ffi::AVColorSpace,
2175 pub color_range: ffi::AVColorRange,
2177 pub sample_aspect_ratio: ffi::AVRational,
2179 pub frame_rate: ffi::AVRational,
2181 pub bit_rate: i64,
2183 pub extradata: &'a [u8],
2185}
2186
2187pub fn video_codec_parameters(
2192 description: &VideoStreamDescription<'_>,
2193) -> Result<rsmpeg::avcodec::AVCodecParameters, BridgeError> {
2194 let extradata_size = i32::try_from(description.extradata.len())
2195 .map_err(|_| BridgeError::InvalidInput("extradata is larger than FFmpeg holds"))?;
2196 let mut parameters = rsmpeg::avcodec::AVCodecParameters::new();
2197 let raw = parameters.as_mut_ptr();
2198 unsafe {
2200 (*raw).codec_type = ffi::AVMEDIA_TYPE_VIDEO;
2201 (*raw).codec_id = description.codec_id;
2202 (*raw).width = description.width;
2203 (*raw).height = description.height;
2204 (*raw).format = description.pixel_format;
2205 (*raw).profile = description.profile;
2206 (*raw).level = description.level;
2207 (*raw).color_primaries = description.color_primaries;
2208 (*raw).color_trc = description.color_trc;
2209 (*raw).color_space = description.color_space;
2210 (*raw).color_range = description.color_range;
2211 (*raw).sample_aspect_ratio = description.sample_aspect_ratio;
2212 (*raw).framerate = description.frame_rate;
2213 (*raw).bit_rate = description.bit_rate;
2214 (*raw).field_order = ffi::AV_FIELD_PROGRESSIVE;
2215 }
2216 if !description.extradata.is_empty() {
2217 let padding = usize::try_from(ffi::AV_INPUT_BUFFER_PADDING_SIZE).unwrap_or(64);
2218 let buffer = unsafe { ffi::av_mallocz(description.extradata.len() + padding) }.cast::<u8>();
2220 if buffer.is_null() {
2221 return Err(BridgeError::FfmpegCode(ffi::AVERROR(ffi::ENOMEM)));
2222 }
2223 unsafe {
2226 ptr::copy_nonoverlapping(
2227 description.extradata.as_ptr(),
2228 buffer,
2229 description.extradata.len(),
2230 );
2231 (*raw).extradata = buffer;
2232 (*raw).extradata_size = extradata_size;
2233 }
2234 }
2235 Ok(parameters)
2236}
2237
2238pub fn bitstream_filter_list(
2247 spec: &CStr,
2248 parameters: &AVCodecParameters,
2249 time_base: rsmpeg::avutil::AVRational,
2250) -> Result<rsmpeg::avcodec::AVBSFContext, BridgeError> {
2251 let mut raw = ptr::null_mut();
2252 let status = unsafe { ffi::av_bsf_list_parse_str(spec.as_ptr(), &raw mut raw) };
2254 if status < 0 {
2255 return Err(BridgeError::FfmpegCode(status));
2256 }
2257 let raw = ptr::NonNull::new(raw).ok_or(BridgeError::FfmpegCode(ffi::AVERROR(ffi::ENOMEM)))?;
2258 let mut context = unsafe { rsmpeg::avcodec::AVBSFContextUninit::from_raw(raw) };
2260 let status = unsafe { ffi::avcodec_parameters_copy(context.par_in, parameters.as_ptr()) };
2264 if status < 0 {
2265 return Err(BridgeError::FfmpegCode(status));
2266 }
2267 context.set_time_base_in(time_base);
2268 context.init().map_err(BridgeError::Ffmpeg)
2269}
2270
2271pub fn set_stream_codec_tag(stream: &mut rsmpeg::avformat::AVStreamMut<'_>, tag: u32) {
2273 let mut parameters = stream.codecpar_mut();
2274 unsafe { (*parameters.as_mut_ptr()).codec_tag = tag };
2276}
2277
2278pub fn set_stream_display_matrix(
2284 stream: &mut rsmpeg::avformat::AVStreamMut<'_>,
2285 display: DisplayRotation,
2286) -> Result<(), BridgeError> {
2287 let mut parameters = stream.codecpar_mut();
2288 let parameters = parameters.as_mut_ptr();
2289 let entry = unsafe {
2292 ffi::av_packet_side_data_new(
2293 &raw mut (*parameters).coded_side_data,
2294 &raw mut (*parameters).nb_coded_side_data,
2295 ffi::AV_PKT_DATA_DISPLAYMATRIX,
2296 size_of::<[i32; 9]>(),
2297 0,
2298 )
2299 };
2300 if entry.is_null() {
2301 return Err(BridgeError::FfmpegCode(ffi::AVERROR(ffi::ENOMEM)));
2302 }
2303 let mut matrix = [0_i32; 9];
2304 unsafe {
2307 ffi::av_display_rotation_set(matrix.as_mut_ptr(), -display.counter_clockwise_degrees);
2308 if display.flipped {
2309 ffi::av_display_matrix_flip(matrix.as_mut_ptr(), 1, 0);
2310 }
2311 }
2312 unsafe {
2315 ptr::copy_nonoverlapping(
2316 matrix.as_ptr().cast::<u8>(),
2317 (*entry).data,
2318 size_of::<[i32; 9]>(),
2319 );
2320 }
2321 Ok(())
2322}
2323
2324#[must_use]
2326pub fn hardware_device_type_name(device_type: ffi::AVHWDeviceType) -> Option<String> {
2327 let name = unsafe { ffi::av_hwdevice_get_type_name(device_type) };
2329 if name.is_null() {
2330 None
2331 } else {
2332 Some(
2334 unsafe { CStr::from_ptr(name) }
2335 .to_string_lossy()
2336 .into_owned(),
2337 )
2338 }
2339}
2340
2341#[must_use]
2348pub fn yuv_layout(format: ffi::AVPixelFormat) -> Option<YuvPlaneLayout> {
2349 if format == ffi::AV_PIX_FMT_NONE {
2350 return None;
2351 }
2352 let descriptor = unsafe { ffi::av_pix_fmt_desc_get(format).as_ref() }?;
2354 let foreign_endian = if cfg!(target_endian = "little") {
2355 ffi::AV_PIX_FMT_FLAG_BE
2356 } else {
2357 0
2358 };
2359 let rejected = foreign_endian
2360 | ffi::AV_PIX_FMT_FLAG_PAL
2361 | ffi::AV_PIX_FMT_FLAG_BITSTREAM
2362 | ffi::AV_PIX_FMT_FLAG_HWACCEL
2363 | ffi::AV_PIX_FMT_FLAG_RGB
2364 | ffi::AV_PIX_FMT_FLAG_ALPHA
2365 | ffi::AV_PIX_FMT_FLAG_BAYER
2366 | ffi::AV_PIX_FMT_FLAG_FLOAT
2367 | ffi::AV_PIX_FMT_FLAG_XYZ;
2368 if descriptor.flags & u64::from(rejected) != 0 || descriptor.nb_components != 3 {
2369 return None;
2370 }
2371 let [luma, u, v, _] = descriptor.comp;
2372 let depth = luma.depth;
2373 if !(8..=16).contains(&depth) || u.depth != depth || v.depth != depth {
2374 return None;
2375 }
2376 if descriptor.log2_chroma_w > 2 || descriptor.log2_chroma_h > 2 {
2377 return None;
2378 }
2379 let bytes = if depth > 8 { 2 } else { 1 };
2380 let shift = luma.shift;
2381 let msb_aligned = match shift {
2382 0 => false,
2383 _ if depth > 8 && shift == 16 - depth => true,
2384 _ => return None,
2385 };
2386 let planar = u.plane == 1
2387 && v.plane == 2
2388 && u.step == bytes
2389 && v.step == bytes
2390 && u.offset == 0
2391 && v.offset == 0;
2392 let interleaved = u.plane == 1
2393 && v.plane == 1
2394 && u.step == 2 * bytes
2395 && v.step == 2 * bytes
2396 && u.offset == 0
2397 && v.offset == bytes;
2398 if luma.plane != 0
2399 || luma.step != bytes
2400 || luma.offset != 0
2401 || u.shift != shift
2402 || v.shift != shift
2403 || !(planar || interleaved)
2404 {
2405 return None;
2406 }
2407 Some(YuvPlaneLayout {
2408 chroma_shift_x: descriptor.log2_chroma_w,
2409 chroma_shift_y: descriptor.log2_chroma_h,
2410 bit_depth: u8::try_from(depth).ok()?,
2411 interleaved_chroma: interleaved,
2412 msb_aligned,
2413 })
2414}
2415
2416pub fn yuv_planes(frame: &AVFrame) -> Result<Option<YuvPlanes>, BridgeError> {
2425 frame_yuv_planes(frame, false)
2426}
2427
2428pub struct DecodedFrame {
2432 frame: AVFrame,
2433 written: bool,
2434}
2435
2436impl DecodedFrame {
2437 pub fn receive(decoder: &mut AVCodecContext) -> Result<Self, RsmpegError> {
2442 let written = decoder_frames_are_initialized(decoder);
2443 let frame = decoder.receive_frame()?;
2444 Ok(Self { frame, written })
2445 }
2446
2447 pub fn yuv_planes(&self) -> Result<Option<YuvPlanes>, BridgeError> {
2454 frame_yuv_planes(&self.frame, self.written)
2455 }
2456
2457 pub fn set_pts(&mut self, pts: i64) {
2459 self.frame.set_pts(pts);
2460 }
2461
2462 #[must_use]
2464 pub fn into_frame(self) -> AVFrame {
2465 self.frame
2466 }
2467}
2468
2469impl std::ops::Deref for DecodedFrame {
2470 type Target = AVFrame;
2471
2472 fn deref(&self) -> &AVFrame {
2473 &self.frame
2474 }
2475}
2476
2477impl fmt::Debug for DecodedFrame {
2478 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2479 formatter
2480 .debug_struct("DecodedFrame")
2481 .field("format", &self.frame.format)
2482 .field("width", &self.frame.width)
2483 .field("height", &self.frame.height)
2484 .field("pts", &self.frame.pts)
2485 .field("written", &self.written)
2486 .finish_non_exhaustive()
2487 }
2488}
2489
2490fn decoder_frames_are_initialized(decoder: &AVCodecContext) -> bool {
2495 static DEFAULT_GET_BUFFER2: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
2496 let default = *DEFAULT_GET_BUFFER2.get_or_init(|| {
2498 unsafe {
2500 let mut context = ffi::avcodec_alloc_context3(ptr::null());
2501 let get_buffer2 = context
2502 .as_ref()
2503 .and_then(|context| context.get_buffer2)
2504 .map_or(0, |function| function as usize);
2505 ffi::avcodec_free_context(&raw mut context);
2506 get_buffer2
2507 }
2508 });
2509 let wrapped =
2511 unsafe { decoder.codec.as_ref() }.is_none_or(|codec| !codec.wrapper_name.is_null());
2512 let get_buffer2 = decoder.get_buffer2.map_or(0, |function| function as usize);
2513 !wrapped && default != 0 && get_buffer2 == default
2514}
2515
2516fn frame_yuv_planes(frame: &AVFrame, share: bool) -> Result<Option<YuvPlanes>, BridgeError> {
2518 let Some(layout) = yuv_layout(frame.format) else {
2519 return Ok(None);
2520 };
2521 let width =
2522 u32::try_from(frame.width).map_err(|_| BridgeError::InvalidInput("negative YUV width"))?;
2523 let height = u32::try_from(frame.height)
2524 .map_err(|_| BridgeError::InvalidInput("negative YUV height"))?;
2525 if width == 0 || height == 0 {
2526 return Err(BridgeError::InvalidInput("empty YUV frame"));
2527 }
2528 let bytes = layout.bytes_per_sample();
2529 let chroma_width = width.div_ceil(1 << layout.chroma_shift_x);
2530 let chroma_height = height.div_ceil(1 << layout.chroma_shift_y);
2531 let shared =
2533 (share && !frame.buf[0].is_null()).then(|| std::sync::Arc::new(SharedFrame(frame.clone())));
2534 let copy = |index: usize,
2535 samples: u32,
2536 pairs: u32,
2537 rows: u32,
2538 context: &'static str|
2539 -> Result<FramePlane, BridgeError> {
2540 let row_bytes = samples
2541 .checked_mul(bytes)
2542 .and_then(|row| row.checked_mul(pairs))
2543 .ok_or(BridgeError::InvalidInput(context))?;
2544 if let Some(view) = shared
2545 .as_ref()
2546 .and_then(|frame| PlaneView::new(frame, index, row_bytes, rows))
2547 {
2548 let stride =
2549 u32::try_from(view.stride).map_err(|_| BridgeError::InvalidInput(context))?;
2550 return Ok(FramePlane {
2551 bytes: bytes::Bytes::from_owner(view),
2552 stride,
2553 width: samples,
2554 height: rows,
2555 });
2556 }
2557 Ok(FramePlane {
2558 bytes: copy_video_plane(frame, index, row_bytes, rows, context)?.into(),
2559 stride: row_bytes,
2560 width: samples,
2561 height: rows,
2562 })
2563 };
2564 let mut planes = vec![copy(0, width, 1, height, "invalid Y plane")?];
2565 if layout.interleaved_chroma {
2566 planes.push(copy(1, chroma_width, 2, chroma_height, "invalid UV plane")?);
2567 } else {
2568 planes.push(copy(1, chroma_width, 1, chroma_height, "invalid U plane")?);
2569 planes.push(copy(2, chroma_width, 1, chroma_height, "invalid V plane")?);
2570 }
2571 Ok(Some(YuvPlanes {
2572 layout,
2573 width,
2574 height,
2575 planes,
2576 }))
2577}
2578
2579struct SharedFrame(AVFrame);
2581
2582unsafe impl Sync for SharedFrame {}
2585
2586struct PlaneView {
2588 frame: std::sync::Arc<SharedFrame>,
2589 data: *const u8,
2590 len: usize,
2591 stride: usize,
2592}
2593
2594unsafe impl Send for PlaneView {}
2597
2598impl PlaneView {
2599 fn new(
2601 frame: &std::sync::Arc<SharedFrame>,
2602 index: usize,
2603 row_bytes: u32,
2604 rows: u32,
2605 ) -> Option<Self> {
2606 let inner = &frame.0;
2607 let data = inner.data[index].cast_const();
2608 let stride = usize::try_from(inner.linesize[index]).ok()?;
2609 let row_bytes = usize::try_from(row_bytes).ok()?;
2610 if data.is_null() || stride < row_bytes || rows == 0 {
2611 return None;
2612 }
2613 let len = stride.checked_mul(usize::try_from(rows).ok()?)?;
2614 in_frame_buffer(inner, data as usize, len).then(|| Self {
2615 frame: std::sync::Arc::clone(frame),
2616 data,
2617 len,
2618 stride,
2619 })
2620 }
2621}
2622
2623impl AsRef<[u8]> for PlaneView {
2624 fn as_ref(&self) -> &[u8] {
2625 let _ = &self.frame;
2626 unsafe { slice::from_raw_parts(self.data, self.len) }
2628 }
2629}
2630
2631fn copy_video_plane(
2633 frame: &AVFrame,
2634 index: usize,
2635 width: u32,
2636 height: u32,
2637 context: &'static str,
2638) -> Result<Vec<u8>, BridgeError> {
2639 let (data, linesize) = (frame.data[index].cast_const(), frame.linesize[index]);
2640 let has_buffers = frame.buf.iter().any(|buffer| !buffer.is_null());
2641 if has_buffers && !plane_rows_in_frame_buffer(frame, data, linesize, width, height) {
2642 return Err(BridgeError::InvalidInput(context));
2643 }
2644 unsafe { copy_plane_rows(data, linesize, width, height, context) }
2647}
2648
2649fn plane_rows_in_frame_buffer(
2651 frame: &AVFrame,
2652 data: *const u8,
2653 linesize: i32,
2654 width: u32,
2655 height: u32,
2656) -> bool {
2657 let (Ok(row_bytes), Ok(rows)) = (usize::try_from(width), usize::try_from(height)) else {
2658 return false;
2659 };
2660 let Ok(stride) = usize::try_from(linesize.unsigned_abs()) else {
2661 return false;
2662 };
2663 let Some(span) = stride
2664 .checked_mul(rows.saturating_sub(1))
2665 .and_then(|before| before.checked_add(row_bytes))
2666 else {
2667 return false;
2668 };
2669 let start = if linesize < 0 {
2671 (data as usize).checked_sub(span - row_bytes)
2672 } else {
2673 Some(data as usize)
2674 };
2675 !data.is_null() && rows > 0 && start.is_some_and(|start| in_frame_buffer(frame, start, span))
2676}
2677
2678unsafe fn copy_plane_rows(
2681 data: *const u8,
2682 linesize: i32,
2683 width: u32,
2684 height: u32,
2685 context: &'static str,
2686) -> Result<Vec<u8>, BridgeError> {
2687 let row_bytes = usize::try_from(width).map_err(|_| BridgeError::InvalidInput(context))?;
2688 let rows = usize::try_from(height).map_err(|_| BridgeError::InvalidInput(context))?;
2689 let stride =
2690 usize::try_from(linesize.unsigned_abs()).map_err(|_| BridgeError::InvalidInput(context))?;
2691 if data.is_null() || linesize == 0 || stride < row_bytes {
2692 return Err(BridgeError::InvalidInput(context));
2693 }
2694 let byte_len = row_bytes
2695 .checked_mul(rows)
2696 .ok_or(BridgeError::InvalidInput(context))?;
2697 let mut output = Vec::with_capacity(byte_len);
2699 let signed_stride =
2700 isize::try_from(linesize).map_err(|_| BridgeError::InvalidInput(context))?;
2701 for row in 0..rows {
2702 let row = isize::try_from(row).map_err(|_| BridgeError::InvalidInput(context))?;
2703 let offset = row
2704 .checked_mul(signed_stride)
2705 .ok_or(BridgeError::InvalidInput(context))?;
2706 let source = unsafe { slice::from_raw_parts(data.offset(offset), row_bytes) };
2708 output.extend_from_slice(source);
2709 }
2710 debug_assert_eq!(output.len(), byte_len);
2711 Ok(output)
2712}
2713
2714pub fn rgba_frame(bytes: &[u8], width: i32, height: i32) -> Result<AVFrame, BridgeError> {
2719 let row_bytes = usize::try_from(width)
2720 .ok()
2721 .and_then(|value| value.checked_mul(4))
2722 .ok_or(BridgeError::InvalidInput("RGBA row width overflow"))?;
2723 let rows =
2724 usize::try_from(height).map_err(|_| BridgeError::InvalidInput("negative RGBA height"))?;
2725 let expected = row_bytes
2726 .checked_mul(rows)
2727 .ok_or(BridgeError::InvalidInput("RGBA frame size overflow"))?;
2728 if bytes.len() != expected {
2729 return Err(BridgeError::InvalidInput("RGBA byte length mismatch"));
2730 }
2731
2732 let mut frame = AVFrame::new();
2733 frame.set_format(ffi::AV_PIX_FMT_RGBA);
2734 frame.set_width(width);
2735 frame.set_height(height);
2736 frame.get_buffer(1)?;
2737 frame.make_writable()?;
2738 let linesize = usize::try_from(frame.linesize[0])
2739 .map_err(|_| BridgeError::InvalidInput("negative RGBA linesize"))?;
2740 if frame.data[0].is_null() || linesize < row_bytes {
2741 return Err(BridgeError::InvalidInput(
2742 "RGBA destination plane is too small",
2743 ));
2744 }
2745
2746 unsafe {
2749 for row in 0..rows {
2750 let destination_offset = row.checked_mul(linesize).ok_or(BridgeError::InvalidInput(
2751 "RGBA destination offset overflow",
2752 ))?;
2753 let source_offset = row
2754 .checked_mul(row_bytes)
2755 .ok_or(BridgeError::InvalidInput("RGBA source offset overflow"))?;
2756 let destination =
2757 slice::from_raw_parts_mut(frame.data[0].add(destination_offset), row_bytes);
2758 destination.copy_from_slice(&bytes[source_offset..source_offset + row_bytes]);
2759 }
2760 }
2761 Ok(frame)
2762}
2763
2764#[allow(clippy::too_many_arguments)]
2769pub fn yuv420p_frame(
2770 y: &[u8],
2771 y_stride: u32,
2772 u: &[u8],
2773 u_stride: u32,
2774 v: &[u8],
2775 v_stride: u32,
2776 width: i32,
2777 height: i32,
2778) -> Result<AVFrame, BridgeError> {
2779 let width =
2780 u32::try_from(width).map_err(|_| BridgeError::InvalidInput("negative YUV width"))?;
2781 let height =
2782 u32::try_from(height).map_err(|_| BridgeError::InvalidInput("negative YUV height"))?;
2783 if width == 0 || height == 0 {
2784 return Err(BridgeError::InvalidInput("empty YUV frame"));
2785 }
2786 let chroma_width = width.div_ceil(2);
2787 let chroma_height = height.div_ceil(2);
2788 validate_source_plane(y, y_stride, width, height, "invalid Y source plane")?;
2789 validate_source_plane(
2790 u,
2791 u_stride,
2792 chroma_width,
2793 chroma_height,
2794 "invalid U source plane",
2795 )?;
2796 validate_source_plane(
2797 v,
2798 v_stride,
2799 chroma_width,
2800 chroma_height,
2801 "invalid V source plane",
2802 )?;
2803
2804 let mut frame = AVFrame::new();
2805 frame.set_format(ffi::AV_PIX_FMT_YUV420P);
2806 frame.set_width(i32::try_from(width).map_err(|_| BridgeError::InvalidInput("YUV width"))?);
2807 frame.set_height(i32::try_from(height).map_err(|_| BridgeError::InvalidInput("YUV height"))?);
2808 frame.get_buffer(1)?;
2809 frame.make_writable()?;
2810 copy_plane_to_frame(y, y_stride, width, height, frame.data[0], frame.linesize[0])?;
2811 copy_plane_to_frame(
2812 u,
2813 u_stride,
2814 chroma_width,
2815 chroma_height,
2816 frame.data[1],
2817 frame.linesize[1],
2818 )?;
2819 copy_plane_to_frame(
2820 v,
2821 v_stride,
2822 chroma_width,
2823 chroma_height,
2824 frame.data[2],
2825 frame.linesize[2],
2826 )?;
2827 Ok(frame)
2828}
2829
2830#[must_use]
2833pub fn yuv_pixel_format(layout: YuvPlaneLayout) -> Option<ffi::AVPixelFormat> {
2834 let mut descriptor = ptr::null();
2835 loop {
2836 descriptor = unsafe { ffi::av_pix_fmt_desc_next(descriptor) };
2838 if descriptor.is_null() {
2839 return None;
2840 }
2841 let format = unsafe { ffi::av_pix_fmt_desc_get_id(descriptor) };
2843 if yuv_layout(format) == Some(layout) {
2844 return Some(format);
2845 }
2846 }
2847}
2848
2849pub fn planar_frame(
2854 format: ffi::AVPixelFormat,
2855 planes: &[(&[u8], u32)],
2856 width: u32,
2857 height: u32,
2858) -> Result<AVFrame, BridgeError> {
2859 let descriptor = unsafe { ffi::av_pix_fmt_desc_get(format).as_ref() }
2861 .ok_or(BridgeError::InvalidInput("pixel format has no descriptor"))?;
2862 let count = unsafe { ffi::av_pix_fmt_count_planes(format) };
2864 if usize::try_from(count).ok() != Some(planes.len()) || width == 0 || height == 0 {
2865 return Err(BridgeError::InvalidInput(
2866 "planes do not match the pixel format",
2867 ));
2868 }
2869 let signed = |value: u32| {
2870 i32::try_from(value).map_err(|_| BridgeError::InvalidInput("frame size overflow"))
2871 };
2872 let mut frame = AVFrame::new();
2873 frame.set_format(format);
2874 frame.set_width(signed(width)?);
2875 frame.set_height(signed(height)?);
2876 frame.get_buffer(1)?;
2877 frame.make_writable()?;
2878 for (index, (bytes, stride)) in planes.iter().enumerate() {
2879 let plane = i32::try_from(index).unwrap_or(i32::MAX);
2880 let row_bytes = unsafe { ffi::av_image_get_linesize(format, signed(width)?, plane) };
2882 let row_bytes = u32::try_from(row_bytes).map_err(|_| BridgeError::FfmpegCode(row_bytes))?;
2883 let rows = if index == 0 || index == 3 {
2884 height
2885 } else {
2886 height.div_ceil(1 << descriptor.log2_chroma_h)
2887 };
2888 validate_source_plane(bytes, *stride, row_bytes, rows, "invalid source plane")?;
2889 copy_plane_to_frame(
2890 bytes,
2891 *stride,
2892 row_bytes,
2893 rows,
2894 frame.data[index],
2895 frame.linesize[index],
2896 )?;
2897 }
2898 Ok(frame)
2899}
2900
2901fn validate_source_plane(
2902 bytes: &[u8],
2903 stride: u32,
2904 width: u32,
2905 height: u32,
2906 context: &'static str,
2907) -> Result<(), BridgeError> {
2908 let required = usize::try_from(stride).ok().and_then(|stride| {
2909 usize::try_from(height)
2910 .ok()
2911 .and_then(|height| stride.checked_mul(height))
2912 });
2913 if stride < width || required != Some(bytes.len()) {
2914 Err(BridgeError::InvalidInput(context))
2915 } else {
2916 Ok(())
2917 }
2918}
2919
2920fn copy_plane_to_frame(
2921 source: &[u8],
2922 source_stride: u32,
2923 width: u32,
2924 height: u32,
2925 destination: *mut u8,
2926 destination_stride: i32,
2927) -> Result<(), BridgeError> {
2928 let row_bytes =
2929 usize::try_from(width).map_err(|_| BridgeError::InvalidInput("YUV row width overflow"))?;
2930 let rows =
2931 usize::try_from(height).map_err(|_| BridgeError::InvalidInput("YUV row count overflow"))?;
2932 let source_stride = usize::try_from(source_stride)
2933 .map_err(|_| BridgeError::InvalidInput("YUV source stride overflow"))?;
2934 let destination_stride = usize::try_from(destination_stride)
2935 .map_err(|_| BridgeError::InvalidInput("negative YUV destination stride"))?;
2936 if destination.is_null() || destination_stride < row_bytes {
2937 return Err(BridgeError::InvalidInput(
2938 "YUV destination plane is too small",
2939 ));
2940 }
2941 for row in 0..rows {
2942 let source_offset = row
2943 .checked_mul(source_stride)
2944 .ok_or(BridgeError::InvalidInput("YUV source offset overflow"))?;
2945 let destination_offset = row
2946 .checked_mul(destination_stride)
2947 .ok_or(BridgeError::InvalidInput("YUV destination offset overflow"))?;
2948 unsafe {
2951 ptr::copy_nonoverlapping(
2952 source.as_ptr().add(source_offset),
2953 destination.add(destination_offset),
2954 row_bytes,
2955 );
2956 }
2957 }
2958 Ok(())
2959}
2960
2961pub fn audio_frame(
2968 channels: &[Vec<f32>],
2969 format: ffi::AVSampleFormat,
2970 sample_rate: i32,
2971 pts: i64,
2972) -> Result<AVFrame, BridgeError> {
2973 let Some(samples) = channels.first().map(Vec::len) else {
2974 return Err(BridgeError::InvalidInput("invalid planar audio shape"));
2975 };
2976 if sample_rate <= 0
2977 || samples == 0
2978 || channels.len() > ffi::AV_NUM_DATA_POINTERS as usize
2979 || channels.iter().any(|channel| channel.len() != samples)
2980 {
2981 return Err(BridgeError::InvalidInput(
2982 "planar audio channel lengths differ",
2983 ));
2984 }
2985 let sample_bytes = match format {
2986 ffi::AV_SAMPLE_FMT_FLTP | ffi::AV_SAMPLE_FMT_FLT => size_of::<f32>(),
2987 ffi::AV_SAMPLE_FMT_S16 => size_of::<i16>(),
2988 _ => return Err(BridgeError::InvalidInput("unsupported audio sample format")),
2989 };
2990 let planar = format == ffi::AV_SAMPLE_FMT_FLTP;
2991 let channel_count = i32::try_from(channels.len())
2992 .map_err(|_| BridgeError::InvalidInput("audio channel count overflow"))?;
2993 let sample_count = i32::try_from(samples)
2994 .map_err(|_| BridgeError::InvalidInput("audio sample count overflow"))?;
2995 let mut frame = AVFrame::new();
2996 frame.set_format(format);
2997 frame.set_ch_layout(
2998 rsmpeg::avutil::AVChannelLayout::from_nb_channels(channel_count).into_inner(),
2999 );
3000 frame.set_sample_rate(sample_rate);
3001 frame.set_nb_samples(sample_count);
3002 frame.set_pts(pts);
3003 frame.set_time_base(ffi::AVRational {
3004 num: 1,
3005 den: sample_rate,
3006 });
3007 frame.get_buffer(0)?;
3008 frame.make_writable()?;
3009 let plane_bytes = samples
3010 .checked_mul(sample_bytes)
3011 .and_then(|bytes| bytes.checked_mul(if planar { 1 } else { channels.len() }))
3012 .ok_or(BridgeError::InvalidInput("audio plane byte count overflow"))?;
3013 let plane_capacity = usize::try_from(frame.linesize[0]).unwrap_or(0);
3014 if plane_capacity < plane_bytes {
3015 return Err(BridgeError::InvalidInput(
3016 "audio plane capacity is too small",
3017 ));
3018 }
3019 let planes = if planar { channels.len() } else { 1 };
3020 if frame.data[..planes].iter().any(|plane| plane.is_null()) {
3021 return Err(BridgeError::InvalidInput("audio plane pointer is null"));
3022 }
3023 if planar {
3024 for (index, source) in channels.iter().enumerate() {
3025 unsafe {
3028 ptr::copy_nonoverlapping(
3029 source.as_ptr().cast::<u8>(),
3030 frame.data[index],
3031 plane_bytes,
3032 );
3033 }
3034 }
3035 return Ok(frame);
3036 }
3037 let plane = unsafe { std::slice::from_raw_parts_mut(frame.data[0], plane_bytes) };
3039 let interleaved = (0..samples).flat_map(|sample| channels.iter().map(move |c| c[sample]));
3040 if format == ffi::AV_SAMPLE_FMT_FLT {
3041 for (bytes, value) in plane.as_chunks_mut::<4>().0.iter_mut().zip(interleaved) {
3042 *bytes = value.to_ne_bytes();
3043 }
3044 } else {
3045 for (bytes, value) in plane.as_chunks_mut::<2>().0.iter_mut().zip(interleaved) {
3046 #[allow(clippy::cast_possible_truncation)]
3048 let value = (value.clamp(-1.0, 1.0) * 32_767.0).round() as i16;
3049 *bytes = value.to_ne_bytes();
3050 }
3051 }
3052 Ok(frame)
3053}
3054
3055#[must_use]
3070pub fn channel_layout_channels(layout: &rsmpeg::avutil::AVChannelLayout) -> Vec<ffi::AVChannel> {
3071 if layout.order == ffi::AV_CHANNEL_ORDER_UNSPEC {
3072 return Vec::new();
3073 }
3074 let channels = u32::try_from(layout.nb_channels).unwrap_or(0);
3075 (0..channels)
3076 .map(|index| {
3077 layout
3078 .channel_from_index(index)
3079 .unwrap_or(ffi::AV_CHAN_NONE)
3080 })
3081 .collect()
3082}
3083
3084pub fn channel_layout_of(
3089 channels: &[ffi::AVChannel],
3090) -> Result<rsmpeg::avutil::AVChannelLayout, BridgeError> {
3091 let count = i32::try_from(channels.len())
3092 .ok()
3093 .filter(|count| *count > 0)
3094 .ok_or(BridgeError::InvalidInput("a channel layout needs channels"))?;
3095 let mut raw = std::mem::MaybeUninit::<ffi::AVChannelLayout>::zeroed();
3096 let initialized = unsafe { ffi::av_channel_layout_custom_init(raw.as_mut_ptr(), count) };
3098 if initialized < 0 {
3099 return Err(BridgeError::FfmpegCode(initialized));
3100 }
3101 let mut layout = unsafe { rsmpeg::avutil::AVChannelLayout::new(raw.assume_init()) };
3103 for (index, channel) in channels.iter().enumerate() {
3104 unsafe { (*layout.u.map.add(index)).id = *channel };
3106 }
3107 let flags = i32::try_from(ffi::AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL).unwrap_or(0);
3108 let retyped = unsafe {
3110 ffi::av_channel_layout_retype(layout.as_mut_ptr(), ffi::AV_CHANNEL_ORDER_UNSPEC, flags)
3111 };
3112 if retyped < 0 {
3113 return Err(BridgeError::FfmpegCode(retyped));
3114 }
3115 Ok(layout)
3116}
3117
3118pub fn audio_f32_interleaved(frame: &AVFrame) -> Result<Vec<f32>, BridgeError> {
3123 if frame.format != ffi::AV_SAMPLE_FMT_FLT || frame.nb_samples < 0 {
3124 return Err(BridgeError::InvalidInput(
3125 "packed audio format or sample count is invalid",
3126 ));
3127 }
3128 let channels = usize::try_from(frame.ch_layout().nb_channels)
3129 .map_err(|_| BridgeError::InvalidInput("packed audio channel count is negative"))?;
3130 let frames = usize::try_from(frame.nb_samples)
3131 .map_err(|_| BridgeError::InvalidInput("packed audio sample count is negative"))?;
3132 let samples = frames
3133 .checked_mul(channels)
3134 .ok_or(BridgeError::InvalidInput(
3135 "packed audio sample count overflow",
3136 ))?;
3137 let bytes = samples
3138 .checked_mul(size_of::<f32>())
3139 .ok_or(BridgeError::InvalidInput(
3140 "packed audio byte count overflow",
3141 ))?;
3142 if channels == 0
3143 || frame.data[0].is_null()
3144 || usize::try_from(frame.linesize[0]).unwrap_or(0) < bytes
3145 {
3146 return Err(BridgeError::InvalidInput(
3147 "packed audio plane is missing or too small",
3148 ));
3149 }
3150 let mut output = vec![0.0_f32; samples];
3151 unsafe {
3153 ptr::copy_nonoverlapping(frame.data[0], output.as_mut_ptr().cast::<u8>(), bytes);
3154 }
3155 Ok(output)
3156}
3157
3158#[derive(Copy, Clone, Debug, PartialEq)]
3160pub struct DisplayRotation {
3161 pub counter_clockwise_degrees: f64,
3163 pub flipped: bool,
3165}
3166
3167#[must_use]
3169pub fn display_rotation(parameters: &AVCodecParameters) -> Option<DisplayRotation> {
3170 let count = usize::try_from(parameters.nb_coded_side_data).ok()?;
3171 if count == 0 || count > 64 || parameters.coded_side_data.is_null() {
3172 return None;
3173 }
3174 let entries = unsafe { slice::from_raw_parts(parameters.coded_side_data, count) };
3176 let matrix = entries.iter().find(|entry| {
3177 entry.type_ == ffi::AV_PKT_DATA_DISPLAYMATRIX
3178 && entry.size >= 9 * size_of::<i32>()
3179 && !entry.data.is_null()
3180 })?;
3181 unsafe { matrix_rotation(matrix.data) }
3183}
3184
3185#[must_use]
3187pub fn frame_display_rotation(frame: &AVFrame) -> Option<DisplayRotation> {
3188 let entry =
3190 unsafe { ffi::av_frame_get_side_data(frame.as_ptr(), ffi::AV_FRAME_DATA_DISPLAYMATRIX) };
3191 if entry.is_null() {
3192 return None;
3193 }
3194 let entry = unsafe { &*entry };
3196 if entry.size < 9 * size_of::<i32>() || entry.data.is_null() {
3197 return None;
3198 }
3199 unsafe { matrix_rotation(entry.data) }
3201}
3202
3203unsafe fn matrix_rotation(data: *const u8) -> Option<DisplayRotation> {
3206 let mut aligned = [0_i32; 9];
3207 unsafe {
3209 ptr::copy_nonoverlapping(
3210 data,
3211 aligned.as_mut_ptr().cast::<u8>(),
3212 size_of::<[i32; 9]>(),
3213 );
3214 }
3215 let determinant = i64::from(aligned[0]) * i64::from(aligned[4])
3218 - i64::from(aligned[1]) * i64::from(aligned[3]);
3219 let flipped = determinant < 0;
3220 if flipped {
3221 unsafe { ffi::av_display_matrix_flip(aligned.as_mut_ptr(), 1, 0) };
3223 }
3224 let angle = unsafe { ffi::av_display_rotation_get(aligned.as_ptr()) };
3226 angle.is_finite().then_some(DisplayRotation {
3227 counter_clockwise_degrees: angle,
3228 flipped,
3229 })
3230}
3231
3232const fn size_of<T>() -> usize {
3233 std::mem::size_of::<T>()
3234}
3235
3236pub struct Yuv420pFrameBuffer {
3242 frame: AVFrame,
3243 width: usize,
3244 height: usize,
3245 planes: Vec<(usize, usize)>,
3247}
3248
3249pub struct Yuv420pFramePlanes<'a> {
3251 pub y: &'a mut [u8],
3253 pub y_stride: usize,
3255 pub u: &'a mut [u8],
3257 pub v: &'a mut [u8],
3259 pub chroma_stride: usize,
3261}
3262
3263impl Yuv420pFrameBuffer {
3264 pub fn new(width: i32, height: i32) -> Result<Self, BridgeError> {
3269 Self::with_format(width, height, ffi::AV_PIX_FMT_YUV420P)
3270 }
3271
3272 pub fn with_format(
3278 width: i32,
3279 height: i32,
3280 format: ffi::AVPixelFormat,
3281 ) -> Result<Self, BridgeError> {
3282 let coded_width = usize::try_from(width)
3283 .map_err(|_| BridgeError::InvalidInput("negative frame width"))?;
3284 let coded_height = usize::try_from(height)
3285 .map_err(|_| BridgeError::InvalidInput("negative frame height"))?;
3286 if coded_width == 0 || coded_height == 0 {
3287 return Err(BridgeError::InvalidInput("empty frame"));
3288 }
3289 let planes = plane_geometry(format, width, height)?;
3290 let frame = zeroed_frame(width, height, format)?;
3291 for (index, &(row_bytes, _)) in planes.iter().enumerate() {
3292 if frame.data[index].is_null()
3293 || plane_stride(frame.linesize[index], row_bytes).is_err()
3294 {
3295 return Err(BridgeError::InvalidInput(
3296 "the frame's planes do not match its pixel format",
3297 ));
3298 }
3299 }
3300 Ok(Self {
3301 frame,
3302 width: coded_width,
3303 height: coded_height,
3304 planes,
3305 })
3306 }
3307
3308 #[must_use]
3310 pub fn planes(&self) -> &[(usize, usize)] {
3311 &self.planes
3312 }
3313
3314 pub fn begin_write_planes(&mut self) -> Result<Vec<(&mut [u8], usize)>, BridgeError> {
3319 self.make_writable()?;
3320 let mut written = Vec::with_capacity(self.planes.len());
3321 for (index, &(row_bytes, rows)) in self.planes.iter().enumerate() {
3322 let stride = plane_stride(self.frame.linesize[index], row_bytes)?;
3323 let plane = unsafe { writable_plane(&self.frame, index, stride, row_bytes, rows)? };
3326 written.push((plane, stride));
3327 }
3328 Ok(written)
3329 }
3330
3331 pub fn is_shared(&self) -> Result<bool, BridgeError> {
3336 Ok(!self.frame.is_writable().map_err(BridgeError::Ffmpeg)?)
3337 }
3338
3339 fn make_writable(&mut self) -> Result<(), BridgeError> {
3340 if self.is_shared()? {
3341 let mut replacement =
3342 zeroed_frame(self.frame.width, self.frame.height, self.frame.format)?;
3343 let status =
3345 unsafe { ffi::av_frame_copy_props(replacement.as_mut_ptr(), self.frame.as_ptr()) };
3346 if status < 0 {
3347 return Err(BridgeError::FfmpegCode(status));
3348 }
3349 self.frame = replacement;
3350 }
3351 Ok(())
3352 }
3353
3354 #[must_use]
3356 pub const fn dimensions(&self) -> (usize, usize) {
3357 (self.width, self.height)
3358 }
3359
3360 pub fn begin_write(&mut self) -> Result<Yuv420pFramePlanes<'_>, BridgeError> {
3365 if self.frame.format != ffi::AV_PIX_FMT_YUV420P {
3366 return Err(BridgeError::InvalidInput("the frame is not YUV420P"));
3367 }
3368 let mut planes = self.begin_write_planes()?.into_iter();
3369 let (Some((y, y_stride)), Some((u, u_stride)), Some((v, v_stride))) =
3370 (planes.next(), planes.next(), planes.next())
3371 else {
3372 return Err(BridgeError::InvalidInput("the frame is not YUV420P"));
3373 };
3374 if u_stride != v_stride {
3375 return Err(BridgeError::InvalidInput("mismatched chroma strides"));
3376 }
3377 Ok(Yuv420pFramePlanes {
3378 y,
3379 y_stride,
3380 u,
3381 v,
3382 chroma_stride: u_stride,
3383 })
3384 }
3385
3386 pub fn set_presentation(&mut self, pts: i64, time_base: ffi::AVRational) {
3388 self.frame.set_pts(pts);
3389 self.frame.set_time_base(time_base);
3390 }
3391
3392 #[must_use]
3394 pub const fn frame(&self) -> &AVFrame {
3395 &self.frame
3396 }
3397
3398 #[must_use]
3400 pub fn visible_bytes(&self) -> usize {
3401 if self.planes.is_empty() {
3402 let chroma = self.width.div_ceil(2) * self.height.div_ceil(2);
3403 return self.width * self.height + 2 * chroma;
3404 }
3405 self.planes
3406 .iter()
3407 .map(|&(row_bytes, rows)| row_bytes * rows)
3408 .sum()
3409 }
3410
3411 #[must_use]
3413 pub const fn size(&self) -> (usize, usize) {
3414 (self.width, self.height)
3415 }
3416}
3417
3418fn zeroed_frame(
3420 width: i32,
3421 height: i32,
3422 format: ffi::AVPixelFormat,
3423) -> Result<AVFrame, BridgeError> {
3424 let mut frame = AVFrame::new();
3425 frame.set_format(format);
3426 frame.set_width(width);
3427 frame.set_height(height);
3428 frame.get_buffer(1)?;
3429 for buffer in frame.buf {
3430 if let Some(buffer) = unsafe { buffer.as_ref() } {
3432 unsafe { ptr::write_bytes(buffer.data, 0, buffer.size) };
3434 }
3435 }
3436 Ok(frame)
3437}
3438
3439impl fmt::Debug for Yuv420pFrameBuffer {
3440 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
3441 formatter
3442 .debug_struct("Yuv420pFrameBuffer")
3443 .field("width", &self.width)
3444 .field("height", &self.height)
3445 .finish_non_exhaustive()
3446 }
3447}
3448
3449fn plane_stride(linesize: i32, width: usize) -> Result<usize, BridgeError> {
3450 let stride = usize::try_from(linesize)
3451 .map_err(|_| BridgeError::InvalidInput("negative frame linesize"))?;
3452 if stride < width {
3453 return Err(BridgeError::InvalidInput(
3454 "frame linesize is below its width",
3455 ));
3456 }
3457 Ok(stride)
3458}
3459
3460unsafe fn writable_plane<'a>(
3465 frame: &AVFrame,
3466 index: usize,
3467 stride: usize,
3468 row_bytes: usize,
3469 rows: usize,
3470) -> Result<&'a mut [u8], BridgeError> {
3471 let data = frame.data[index];
3472 if data.is_null() {
3473 return Err(BridgeError::InvalidInput("frame plane is null"));
3474 }
3475 let len = stride
3476 .checked_mul(rows.saturating_sub(1))
3477 .and_then(|before| before.checked_add(row_bytes))
3478 .ok_or(BridgeError::InvalidInput("frame plane size overflow"))?;
3479 if !in_frame_buffer(frame, data as usize, len) {
3480 return Err(BridgeError::InvalidInput(
3481 "frame plane extends past its buffer",
3482 ));
3483 }
3484 Ok(unsafe { slice::from_raw_parts_mut(data, len) })
3486}
3487
3488fn in_frame_buffer(frame: &AVFrame, start: usize, len: usize) -> bool {
3490 let Some(end) = start.checked_add(len) else {
3491 return false;
3492 };
3493 frame.buf.iter().any(|buffer| {
3494 let Some(buffer) = (unsafe { buffer.as_ref() }) else {
3496 return false;
3497 };
3498 let buffer_start = buffer.data as usize;
3499 buffer_start
3500 .checked_add(buffer.size)
3501 .is_some_and(|buffer_end| buffer_start <= start && end <= buffer_end)
3502 })
3503}
3504
3505fn plane_geometry(
3507 format: ffi::AVPixelFormat,
3508 width: i32,
3509 height: i32,
3510) -> Result<Vec<(usize, usize)>, BridgeError> {
3511 let descriptor = unsafe { ffi::av_pix_fmt_desc_get(format).as_ref() }
3513 .ok_or(BridgeError::InvalidInput("pixel format has no descriptor"))?;
3514 let count = unsafe { ffi::av_pix_fmt_count_planes(format) };
3516 let count = usize::try_from(count)
3517 .ok()
3518 .filter(|count| (1..=4).contains(count))
3519 .ok_or(BridgeError::InvalidInput("pixel format has no planes"))?;
3520 let rows = usize::try_from(height).map_err(|_| BridgeError::InvalidInput("negative height"))?;
3521 (0..count)
3522 .map(|index| {
3523 let plane = i32::try_from(index).unwrap_or(i32::MAX);
3524 let row_bytes = unsafe { ffi::av_image_get_linesize(format, width, plane) };
3526 let row_bytes =
3527 usize::try_from(row_bytes).map_err(|_| BridgeError::FfmpegCode(row_bytes))?;
3528 let rows = if index == 1 || index == 2 {
3529 rows.div_ceil(1 << descriptor.log2_chroma_h)
3530 } else {
3531 rows
3532 };
3533 Ok((row_bytes, rows))
3534 })
3535 .collect()
3536}
3537
3538#[cfg(all(test, target_os = "linux", feature = "hwdevice"))]
3539mod vulkan_shim_tests;
3540
3541#[cfg(all(test, target_os = "linux", feature = "hwdevice"))]
3542mod vulkan_access_tests;
3543
3544#[cfg(test)]
3545mod tests;