util/frame/audio: add samples accessor
This commit is contained in:
parent
fd7ff33f68
commit
72895052e6
@ -1,4 +1,4 @@
|
|||||||
#![feature(convert)]
|
#![feature(convert, core)]
|
||||||
#![allow(raw_pointer_derive, non_camel_case_types)]
|
#![allow(raw_pointer_derive, non_camel_case_types)]
|
||||||
|
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
@ -15,6 +15,7 @@ pub use util::color;
|
|||||||
pub use util::chroma;
|
pub use util::chroma;
|
||||||
pub use util::time;
|
pub use util::time;
|
||||||
pub use util::frame::{self, Frame};
|
pub use util::frame::{self, Frame};
|
||||||
|
pub use util::samples::Samples;
|
||||||
|
|
||||||
#[cfg(feature = "format")]
|
#[cfg(feature = "format")]
|
||||||
pub mod format;
|
pub mod format;
|
||||||
|
@ -49,6 +49,10 @@ impl Sample {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_packed(&self) -> bool {
|
||||||
|
!self.is_planar()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn bytes(&self) -> usize {
|
pub fn bytes(&self) -> usize {
|
||||||
unsafe {
|
unsafe {
|
||||||
av_get_bytes_per_sample((*self).into()) as usize
|
av_get_bytes_per_sample((*self).into()) as usize
|
||||||
|
@ -3,6 +3,7 @@ use std::mem;
|
|||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
use ffi::*;
|
use ffi::*;
|
||||||
|
use ::Samples;
|
||||||
use ::util::format;
|
use ::util::format;
|
||||||
use super::Frame;
|
use super::Frame;
|
||||||
|
|
||||||
@ -21,7 +22,7 @@ impl Audio {
|
|||||||
let mut frame = Audio::empty();
|
let mut frame = Audio::empty();
|
||||||
|
|
||||||
frame.set_format(format);
|
frame.set_format(format);
|
||||||
frame.set_samples(samples);
|
frame.set_sample_number(samples);
|
||||||
frame.set_channel_layout(layout);
|
frame.set_channel_layout(layout);
|
||||||
|
|
||||||
av_frame_get_buffer(frame.ptr, 1);
|
av_frame_get_buffer(frame.ptr, 1);
|
||||||
@ -83,17 +84,25 @@ impl Audio {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn samples(&self) -> usize {
|
pub fn sample_number(&self) -> usize {
|
||||||
unsafe {
|
unsafe {
|
||||||
(*self.ptr).nb_samples as usize
|
(*self.ptr).nb_samples as usize
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_samples(&mut self, value: usize) {
|
pub fn set_sample_number(&mut self, value: usize) {
|
||||||
unsafe {
|
unsafe {
|
||||||
(*self.ptr).nb_samples = value as c_int;
|
(*self.ptr).nb_samples = value as c_int;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn samples(&self) -> Samples {
|
||||||
|
Samples::wrap(self.ptr as *mut AVPicture, self.format(), self.sample_number(), self.channels())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn samples_mut(&mut self) -> Samples {
|
||||||
|
Samples::wrap(self.ptr as *mut AVPicture, self.format(), self.sample_number(), self.channels())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Send for Audio { }
|
unsafe impl Send for Audio { }
|
||||||
@ -114,7 +123,7 @@ impl DerefMut for Audio {
|
|||||||
|
|
||||||
impl Clone for Audio {
|
impl Clone for Audio {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
let mut cloned = Audio::new(self.format(), self.samples(), self.channel_layout());
|
let mut cloned = Audio::new(self.format(), self.sample_number(), self.channel_layout());
|
||||||
cloned.clone_from(self);
|
cloned.clone_from(self);
|
||||||
|
|
||||||
cloned
|
cloned
|
||||||
|
@ -8,6 +8,7 @@ pub mod format;
|
|||||||
pub mod frame;
|
pub mod frame;
|
||||||
pub mod chroma;
|
pub mod chroma;
|
||||||
pub mod time;
|
pub mod time;
|
||||||
|
pub mod samples;
|
||||||
|
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use std::str::from_utf8_unchecked;
|
use std::str::from_utf8_unchecked;
|
||||||
|
130
src/util/samples.rs
Normal file
130
src/util/samples.rs
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
use std::slice;
|
||||||
|
use std::mem;
|
||||||
|
use std::any::TypeId;
|
||||||
|
use std::marker::{Reflect, PhantomData};
|
||||||
|
|
||||||
|
use ffi::*;
|
||||||
|
use ::util::format::Sample;
|
||||||
|
use ::Error;
|
||||||
|
|
||||||
|
pub struct Samples<'a> {
|
||||||
|
pub ptr: *mut AVPicture,
|
||||||
|
|
||||||
|
format: Sample,
|
||||||
|
number: usize,
|
||||||
|
channels: u16,
|
||||||
|
|
||||||
|
_marker: PhantomData<&'a ()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Samples<'a> {
|
||||||
|
pub fn wrap(ptr: *mut AVPicture, format: Sample, number: usize, channels: u16) -> Self {
|
||||||
|
Samples {
|
||||||
|
ptr: ptr,
|
||||||
|
|
||||||
|
format: format,
|
||||||
|
number: number,
|
||||||
|
channels: channels,
|
||||||
|
|
||||||
|
_marker: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn format(&self) -> Sample {
|
||||||
|
self.format
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn number(&self) -> usize {
|
||||||
|
self.number
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn channels(&self) -> u16 {
|
||||||
|
self.channels
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_planar(&self) -> bool {
|
||||||
|
self.format.is_planar()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_packed(&self) -> bool {
|
||||||
|
self.format.is_planar()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn data<T: Reflect + 'static>(&self) -> Result<Vec<&[T]>, Error> {
|
||||||
|
if !valid::<T>(self.format) {
|
||||||
|
return Err(Error::InvalidData)
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut result = Vec::new();
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
let size = (*self.ptr).linesize[0];
|
||||||
|
|
||||||
|
if self.is_planar() {
|
||||||
|
for i in 0 .. self.channels {
|
||||||
|
result.push(slice::from_raw_parts(
|
||||||
|
mem::transmute((*self.ptr).data[i as usize]),
|
||||||
|
size as usize / mem::size_of::<T>()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result.push(slice::from_raw_parts(
|
||||||
|
mem::transmute((*self.ptr).data[0]),
|
||||||
|
size as usize / mem::size_of::<T>()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn data_mut<T: Reflect + 'static>(&mut self) -> Result<Vec<&mut [T]>, Error> {
|
||||||
|
if !valid::<T>(self.format) {
|
||||||
|
return Err(Error::InvalidData)
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut result = Vec::new();
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
let size = (*self.ptr).linesize[0];
|
||||||
|
|
||||||
|
if self.is_planar() {
|
||||||
|
for i in 0 .. self.channels {
|
||||||
|
result.push(slice::from_raw_parts_mut(
|
||||||
|
mem::transmute((*self.ptr).data[i as usize]),
|
||||||
|
size as usize / mem::size_of::<T>()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result.push(slice::from_raw_parts_mut(
|
||||||
|
mem::transmute((*self.ptr).data[0]),
|
||||||
|
size as usize / mem::size_of::<T>()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn valid<T: Reflect + 'static>(format: Sample) -> bool {
|
||||||
|
match format {
|
||||||
|
Sample::None =>
|
||||||
|
false,
|
||||||
|
|
||||||
|
Sample::U8 | Sample::U8P if TypeId::of::<T>() != TypeId::of::<u8>() =>
|
||||||
|
false,
|
||||||
|
|
||||||
|
Sample::S16 | Sample::S16P if TypeId::of::<T>() != TypeId::of::<i16>() =>
|
||||||
|
false,
|
||||||
|
|
||||||
|
Sample::S32 | Sample::S32P if TypeId::of::<T>() != TypeId::of::<i32>() =>
|
||||||
|
false,
|
||||||
|
|
||||||
|
Sample::FLT | Sample::FLTP if TypeId::of::<T>() != TypeId::of::<f32>() =>
|
||||||
|
false,
|
||||||
|
|
||||||
|
Sample::DBL | Sample::DBLP if TypeId::of::<T>() != TypeId::of::<f64>() =>
|
||||||
|
false,
|
||||||
|
|
||||||
|
_ => true
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user