frame/audio: add inline attributes

This commit is contained in:
meh 2015-08-05 17:08:15 +02:00
parent 3c138fe189
commit 19b0351574

View File

@ -12,10 +12,12 @@ use super::Frame;
pub struct Audio(Frame); pub struct Audio(Frame);
impl Audio { impl Audio {
#[inline]
pub unsafe fn wrap(ptr: *mut AVFrame) -> Self { pub unsafe fn wrap(ptr: *mut AVFrame) -> Self {
Audio(Frame::wrap(ptr)) Audio(Frame::wrap(ptr))
} }
#[inline]
pub unsafe fn alloc(&mut self, format: format::Sample, samples: usize, layout: ChannelLayout) { pub unsafe fn alloc(&mut self, format: format::Sample, samples: usize, layout: ChannelLayout) {
self.set_format(format); self.set_format(format);
self.set_samples(samples); self.set_samples(samples);
@ -26,12 +28,14 @@ impl Audio {
} }
impl Audio { impl Audio {
#[inline]
pub fn empty() -> Self { pub fn empty() -> Self {
unsafe { unsafe {
Audio(Frame::empty()) Audio(Frame::empty())
} }
} }
#[inline]
pub fn new(format: format::Sample, samples: usize, layout: ChannelLayout) -> Self { pub fn new(format: format::Sample, samples: usize, layout: ChannelLayout) -> Self {
unsafe { unsafe {
let mut frame = Audio::empty(); let mut frame = Audio::empty();
@ -41,6 +45,7 @@ impl Audio {
} }
} }
#[inline]
pub fn format(&self) -> format::Sample { pub fn format(&self) -> format::Sample {
unsafe { unsafe {
if (*self.as_ptr()).format == -1 { if (*self.as_ptr()).format == -1 {
@ -52,68 +57,80 @@ impl Audio {
} }
} }
#[inline]
pub fn set_format(&mut self, value: format::Sample) { pub fn set_format(&mut self, value: format::Sample) {
unsafe { unsafe {
(*self.as_mut_ptr()).format = mem::transmute::<AVSampleFormat, c_int>(value.into()); (*self.as_mut_ptr()).format = mem::transmute::<AVSampleFormat, c_int>(value.into());
} }
} }
#[inline]
pub fn channel_layout(&self) -> ChannelLayout { pub fn channel_layout(&self) -> ChannelLayout {
unsafe { unsafe {
ChannelLayout::from_bits_truncate(av_frame_get_channel_layout(self.as_ptr()) as c_ulonglong) ChannelLayout::from_bits_truncate(av_frame_get_channel_layout(self.as_ptr()) as c_ulonglong)
} }
} }
#[inline]
pub fn set_channel_layout(&mut self, value: ChannelLayout) { pub fn set_channel_layout(&mut self, value: ChannelLayout) {
unsafe { unsafe {
av_frame_set_channel_layout(self.as_mut_ptr(), value.bits() as int64_t); av_frame_set_channel_layout(self.as_mut_ptr(), value.bits() as int64_t);
} }
} }
#[inline]
pub fn channels(&self) -> u16 { pub fn channels(&self) -> u16 {
unsafe { unsafe {
av_frame_get_channels(self.as_ptr()) as u16 av_frame_get_channels(self.as_ptr()) as u16
} }
} }
#[inline]
pub fn set_channels(&mut self, value: u16) { pub fn set_channels(&mut self, value: u16) {
unsafe { unsafe {
av_frame_set_channels(self.as_mut_ptr(), value as c_int); av_frame_set_channels(self.as_mut_ptr(), value as c_int);
} }
} }
#[inline]
pub fn rate(&self) -> u32 { pub fn rate(&self) -> u32 {
unsafe { unsafe {
av_frame_get_sample_rate(self.as_ptr()) as u32 av_frame_get_sample_rate(self.as_ptr()) as u32
} }
} }
#[inline]
pub fn set_rate(&mut self, value: u32) { pub fn set_rate(&mut self, value: u32) {
unsafe { unsafe {
av_frame_set_sample_rate(self.as_mut_ptr(), value as c_int); av_frame_set_sample_rate(self.as_mut_ptr(), value as c_int);
} }
} }
#[inline]
pub fn samples(&self) -> usize { pub fn samples(&self) -> usize {
unsafe { unsafe {
(*self.as_ptr()).nb_samples as usize (*self.as_ptr()).nb_samples as usize
} }
} }
#[inline]
pub fn set_samples(&mut self, value: usize) { pub fn set_samples(&mut self, value: usize) {
unsafe { unsafe {
(*self.as_mut_ptr()).nb_samples = value as c_int; (*self.as_mut_ptr()).nb_samples = value as c_int;
} }
} }
#[inline]
pub fn is_planar(&self) -> bool { pub fn is_planar(&self) -> bool {
self.format().is_planar() self.format().is_planar()
} }
#[inline]
pub fn is_packed(&self) -> bool { pub fn is_packed(&self) -> bool {
self.format().is_packed() self.format().is_packed()
} }
#[inline]
pub fn planes(&self) -> usize { pub fn planes(&self) -> usize {
unsafe { unsafe {
if (*self.as_ptr()).linesize[0] == 0 { if (*self.as_ptr()).linesize[0] == 0 {
@ -129,6 +146,7 @@ impl Audio {
} }
} }
#[inline]
pub fn plane<T: Sample>(&self, index: usize) -> &[T] { pub fn plane<T: Sample>(&self, index: usize) -> &[T] {
if index >= self.planes() { if index >= self.planes() {
panic!("out of bounds"); panic!("out of bounds");
@ -145,6 +163,7 @@ impl Audio {
} }
} }
#[inline]
pub fn plane_mut<T: Sample>(&mut self, index: usize) -> &[T] { pub fn plane_mut<T: Sample>(&mut self, index: usize) -> &[T] {
if index >= self.planes() { if index >= self.planes() {
panic!("out of bounds"); panic!("out of bounds");
@ -161,6 +180,7 @@ impl Audio {
} }
} }
#[inline]
pub fn data(&self) -> Vec<&[u8]> { pub fn data(&self) -> Vec<&[u8]> {
let mut result = Vec::new(); let mut result = Vec::new();
@ -175,6 +195,7 @@ impl Audio {
result result
} }
#[inline]
pub fn data_mut(&mut self) -> Vec<&mut [u8]> { pub fn data_mut(&mut self) -> Vec<&mut [u8]> {
let mut result = Vec::new(); let mut result = Vec::new();
@ -236,6 +257,7 @@ pub unsafe trait Sample {
} }
unsafe impl Sample for u8 { unsafe impl Sample for u8 {
#[inline]
fn is_valid(format: format::Sample, _channels: u16) -> bool { fn is_valid(format: format::Sample, _channels: u16) -> bool {
if let format::Sample::U8(..) = format { if let format::Sample::U8(..) = format {
true true
@ -247,42 +269,49 @@ unsafe impl Sample for u8 {
} }
unsafe impl Sample for (u8, u8) { unsafe impl Sample for (u8, u8) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 2 && format == format::Sample::U8(format::sample::Type::Packed) channels == 2 && format == format::Sample::U8(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (u8, u8, u8) { unsafe impl Sample for (u8, u8, u8) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 3 && format == format::Sample::U8(format::sample::Type::Packed) channels == 3 && format == format::Sample::U8(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (u8, u8, u8, u8) { unsafe impl Sample for (u8, u8, u8, u8) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 4 && format == format::Sample::U8(format::sample::Type::Packed) channels == 4 && format == format::Sample::U8(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (u8, u8, u8, u8, u8) { unsafe impl Sample for (u8, u8, u8, u8, u8) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 5 && format == format::Sample::U8(format::sample::Type::Packed) channels == 5 && format == format::Sample::U8(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (u8, u8, u8, u8, u8, u8) { unsafe impl Sample for (u8, u8, u8, u8, u8, u8) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 6 && format == format::Sample::U8(format::sample::Type::Packed) channels == 6 && format == format::Sample::U8(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (u8, u8, u8, u8, u8, u8, u8) { unsafe impl Sample for (u8, u8, u8, u8, u8, u8, u8) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 7 && format == format::Sample::U8(format::sample::Type::Packed) channels == 7 && format == format::Sample::U8(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for i16 { unsafe impl Sample for i16 {
#[inline]
fn is_valid(format: format::Sample, _channels: u16) -> bool { fn is_valid(format: format::Sample, _channels: u16) -> bool {
if let format::Sample::I16(..) = format { if let format::Sample::I16(..) = format {
true true
@ -294,42 +323,49 @@ unsafe impl Sample for i16 {
} }
unsafe impl Sample for (i16, i16) { unsafe impl Sample for (i16, i16) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 2 && format == format::Sample::I16(format::sample::Type::Packed) channels == 2 && format == format::Sample::I16(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (i16, i16, i16) { unsafe impl Sample for (i16, i16, i16) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 3 && format == format::Sample::I16(format::sample::Type::Packed) channels == 3 && format == format::Sample::I16(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (i16, i16, i16, i16) { unsafe impl Sample for (i16, i16, i16, i16) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 4 && format == format::Sample::I16(format::sample::Type::Packed) channels == 4 && format == format::Sample::I16(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (i16, i16, i16, i16, i16) { unsafe impl Sample for (i16, i16, i16, i16, i16) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 5 && format == format::Sample::I16(format::sample::Type::Packed) channels == 5 && format == format::Sample::I16(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (i16, i16, i16, i16, i16, i16) { unsafe impl Sample for (i16, i16, i16, i16, i16, i16) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 6 && format == format::Sample::I16(format::sample::Type::Packed) channels == 6 && format == format::Sample::I16(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (i16, i16, i16, i16, i16, i16, i16) { unsafe impl Sample for (i16, i16, i16, i16, i16, i16, i16) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 7 && format == format::Sample::I16(format::sample::Type::Packed) channels == 7 && format == format::Sample::I16(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for i32 { unsafe impl Sample for i32 {
#[inline]
fn is_valid(format: format::Sample, _channels: u16) -> bool { fn is_valid(format: format::Sample, _channels: u16) -> bool {
if let format::Sample::I32(..) = format { if let format::Sample::I32(..) = format {
true true
@ -341,42 +377,49 @@ unsafe impl Sample for i32 {
} }
unsafe impl Sample for (i32, i32) { unsafe impl Sample for (i32, i32) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 2 && format == format::Sample::I32(format::sample::Type::Packed) channels == 2 && format == format::Sample::I32(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (i32, i32, i32) { unsafe impl Sample for (i32, i32, i32) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 3 && format == format::Sample::I32(format::sample::Type::Packed) channels == 3 && format == format::Sample::I32(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (i32, i32, i32, i32) { unsafe impl Sample for (i32, i32, i32, i32) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 4 && format == format::Sample::I32(format::sample::Type::Packed) channels == 4 && format == format::Sample::I32(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (i32, i32, i32, i32, i32) { unsafe impl Sample for (i32, i32, i32, i32, i32) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 5 && format == format::Sample::I32(format::sample::Type::Packed) channels == 5 && format == format::Sample::I32(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (i32, i32, i32, i32, i32, i32) { unsafe impl Sample for (i32, i32, i32, i32, i32, i32) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 6 && format == format::Sample::I32(format::sample::Type::Packed) channels == 6 && format == format::Sample::I32(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (i32, i32, i32, i32, i32, i32, i32) { unsafe impl Sample for (i32, i32, i32, i32, i32, i32, i32) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 7 && format == format::Sample::I32(format::sample::Type::Packed) channels == 7 && format == format::Sample::I32(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for f32 { unsafe impl Sample for f32 {
#[inline]
fn is_valid(format: format::Sample, _channels: u16) -> bool { fn is_valid(format: format::Sample, _channels: u16) -> bool {
if let format::Sample::F32(..) = format { if let format::Sample::F32(..) = format {
true true
@ -388,42 +431,49 @@ unsafe impl Sample for f32 {
} }
unsafe impl Sample for (f32, f32) { unsafe impl Sample for (f32, f32) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 2 && format == format::Sample::F32(format::sample::Type::Packed) channels == 2 && format == format::Sample::F32(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (f32, f32, f32) { unsafe impl Sample for (f32, f32, f32) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 3 && format == format::Sample::F32(format::sample::Type::Packed) channels == 3 && format == format::Sample::F32(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (f32, f32, f32, f32) { unsafe impl Sample for (f32, f32, f32, f32) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 4 && format == format::Sample::F32(format::sample::Type::Packed) channels == 4 && format == format::Sample::F32(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (f32, f32, f32, f32, f32) { unsafe impl Sample for (f32, f32, f32, f32, f32) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 5 && format == format::Sample::F32(format::sample::Type::Packed) channels == 5 && format == format::Sample::F32(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (f32, f32, f32, f32, f32, f32) { unsafe impl Sample for (f32, f32, f32, f32, f32, f32) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 6 && format == format::Sample::F32(format::sample::Type::Packed) channels == 6 && format == format::Sample::F32(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (f32, f32, f32, f32, f32, f32, f32) { unsafe impl Sample for (f32, f32, f32, f32, f32, f32, f32) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 7 && format == format::Sample::F32(format::sample::Type::Packed) channels == 7 && format == format::Sample::F32(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for f64 { unsafe impl Sample for f64 {
#[inline]
fn is_valid(format: format::Sample, _channels: u16) -> bool { fn is_valid(format: format::Sample, _channels: u16) -> bool {
if let format::Sample::F64(..) = format { if let format::Sample::F64(..) = format {
true true
@ -435,36 +485,42 @@ unsafe impl Sample for f64 {
} }
unsafe impl Sample for (f64, f64) { unsafe impl Sample for (f64, f64) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 2 && format == format::Sample::F64(format::sample::Type::Packed) channels == 2 && format == format::Sample::F64(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (f64, f64, f64) { unsafe impl Sample for (f64, f64, f64) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 3 && format == format::Sample::F64(format::sample::Type::Packed) channels == 3 && format == format::Sample::F64(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (f64, f64, f64, f64) { unsafe impl Sample for (f64, f64, f64, f64) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 4 && format == format::Sample::F64(format::sample::Type::Packed) channels == 4 && format == format::Sample::F64(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (f64, f64, f64, f64, f64) { unsafe impl Sample for (f64, f64, f64, f64, f64) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 5 && format == format::Sample::F64(format::sample::Type::Packed) channels == 5 && format == format::Sample::F64(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (f64, f64, f64, f64, f64, f64) { unsafe impl Sample for (f64, f64, f64, f64, f64, f64) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 6 && format == format::Sample::F64(format::sample::Type::Packed) channels == 6 && format == format::Sample::F64(format::sample::Type::Packed)
} }
} }
unsafe impl Sample for (f64, f64, f64, f64, f64, f64, f64) { unsafe impl Sample for (f64, f64, f64, f64, f64, f64, f64) {
#[inline]
fn is_valid(format: format::Sample, channels: u16) -> bool { fn is_valid(format: format::Sample, channels: u16) -> bool {
channels == 7 && format == format::Sample::F64(format::sample::Type::Packed) channels == 7 && format == format::Sample::F64(format::sample::Type::Packed)
} }