*: format code with rustfmt and fix clippy suggestions

* Add avformat_close_input call to clean up AVFormantContext
* Format code with rustfmt
* Fix clippy lint double_parens
* Fix clippy lint deref_addrof
* Fix clippy lint identity_conversion
* Fix clippy lint match_ref_pats
* Fix clippy lint cast_lossless
* Fix clippy lint cmp_null
* Fix clippy lint clone_on_ref_ptr
* Fix clippy lint map_clone
* Fix clippy lint needless_borrow
* Fix clippy lint needless_pass_by_value
* Fix clippy lints for examples
* Fix clippy lint unused_io_amount
* Fix clippy lint new_without_default
* Ignore inline_always clippy lint
* Add vim temp files to .gitignore
This commit is contained in:
Tadas Barzdžius
2018-04-10 17:06:42 +03:00
committed by meh
parent 20c7ef399a
commit 0bcd4550b8
128 changed files with 10643 additions and 10233 deletions

View File

@ -1,139 +1,153 @@
use std::ptr;
use libc::{c_int, int64_t};
use ffi::*;
use ::util::format;
use ::{Error, ChannelLayout, frame};
use super::Delay;
use ffi::*;
use libc::{c_int, int64_t};
use util::format;
use {frame, ChannelLayout, Error};
#[derive(Eq, PartialEq, Copy, Clone)]
pub struct Definition {
pub format: format::Sample,
pub channel_layout: ChannelLayout,
pub rate: u32,
pub format: format::Sample,
pub channel_layout: ChannelLayout,
pub rate: u32,
}
pub struct Context {
ptr: *mut SwrContext,
ptr: *mut SwrContext,
input: Definition,
output: Definition,
input: Definition,
output: Definition,
}
impl Context {
#[doc(hidden)]
pub unsafe fn as_ptr(&self) -> *const SwrContext {
self.ptr as *const _
}
#[doc(hidden)]
pub unsafe fn as_ptr(&self) -> *const SwrContext {
self.ptr as *const _
}
#[doc(hidden)]
pub unsafe fn as_mut_ptr(&mut self) -> *mut SwrContext {
self.ptr
}
#[doc(hidden)]
pub unsafe fn as_mut_ptr(&mut self) -> *mut SwrContext {
self.ptr
}
}
impl Context {
/// Create a resampler with the given definitions.
pub fn get(src_format: format::Sample, src_channel_layout: ChannelLayout, src_rate: u32,
dst_format: format::Sample, dst_channel_layout: ChannelLayout, dst_rate: u32) -> Result<Self, Error> {
unsafe {
let ptr = swr_alloc_set_opts(ptr::null_mut(),
dst_channel_layout.bits() as int64_t, dst_format.into(), dst_rate as c_int,
src_channel_layout.bits() as int64_t, src_format.into(), src_rate as c_int,
0, ptr::null_mut());
/// Create a resampler with the given definitions.
pub fn get(
src_format: format::Sample,
src_channel_layout: ChannelLayout,
src_rate: u32,
dst_format: format::Sample,
dst_channel_layout: ChannelLayout,
dst_rate: u32,
) -> Result<Self, Error> {
unsafe {
let ptr = swr_alloc_set_opts(
ptr::null_mut(),
dst_channel_layout.bits() as int64_t,
dst_format.into(),
dst_rate as c_int,
src_channel_layout.bits() as int64_t,
src_format.into(),
src_rate as c_int,
0,
ptr::null_mut(),
);
if ptr != ptr::null_mut() {
match swr_init(ptr) {
e if e < 0 =>
Err(Error::from(e)),
if ptr.is_null() {
match swr_init(ptr) {
e if e < 0 => Err(Error::from(e)),
_ =>
Ok(Context {
ptr: ptr,
_ => Ok(Context {
ptr: ptr,
input: Definition {
format: src_format,
channel_layout: src_channel_layout,
rate: src_rate,
},
input: Definition {
format: src_format,
channel_layout: src_channel_layout,
rate: src_rate,
},
output: Definition {
format: dst_format,
channel_layout: dst_channel_layout,
rate: dst_rate,
}
})
}
}
else {
Err(Error::InvalidData)
}
}
}
output: Definition {
format: dst_format,
channel_layout: dst_channel_layout,
rate: dst_rate,
},
}),
}
} else {
Err(Error::InvalidData)
}
}
}
/// Get the input definition.
pub fn input(&self) -> &Definition {
&self.input
}
/// Get the input definition.
pub fn input(&self) -> &Definition {
&self.input
}
/// Get the output definition.
pub fn output(&self) -> &Definition {
&self.output
}
/// Get the output definition.
pub fn output(&self) -> &Definition {
&self.output
}
/// Get the remaining delay.
pub fn delay(&self) -> Option<Delay> {
unsafe {
match swr_get_delay(self.as_ptr() as *mut _, 1) {
0 => None,
_ => Some(Delay::from(self))
}
}
}
/// Get the remaining delay.
pub fn delay(&self) -> Option<Delay> {
unsafe {
match swr_get_delay(self.as_ptr() as *mut _, 1) {
0 => None,
_ => Some(Delay::from(self)),
}
}
}
/// Run the resampler from the given input to the given output.
///
/// When there are internal frames to process it will return `Ok(Some(Delay { .. }))`.
pub fn run(&mut self, input: &frame::Audio, output: &mut frame::Audio) -> Result<Option<Delay>, Error> {
output.set_rate(self.output.rate);
/// Run the resampler from the given input to the given output.
///
/// When there are internal frames to process it will return `Ok(Some(Delay { .. }))`.
pub fn run(
&mut self,
input: &frame::Audio,
output: &mut frame::Audio,
) -> Result<Option<Delay>, Error> {
output.set_rate(self.output.rate);
unsafe {
if output.is_empty() {
output.alloc(self.output.format, input.samples(), self.output.channel_layout);
}
unsafe {
if output.is_empty() {
output.alloc(
self.output.format,
input.samples(),
self.output.channel_layout,
);
}
match swr_convert_frame(self.as_mut_ptr(), output.as_mut_ptr(), input.as_ptr()) {
0 =>
Ok(self.delay()),
match swr_convert_frame(self.as_mut_ptr(), output.as_mut_ptr(), input.as_ptr()) {
0 => Ok(self.delay()),
e =>
Err(Error::from(e))
}
}
}
e => Err(Error::from(e)),
}
}
}
/// Convert one of the remaining internal frames.
///
/// When there are no more internal frames `Ok(None)` will be returned.
pub fn flush(&mut self, output: &mut frame::Audio) -> Result<Option<Delay>, Error> {
output.set_rate(self.output.rate);
/// Convert one of the remaining internal frames.
///
/// When there are no more internal frames `Ok(None)` will be returned.
pub fn flush(&mut self, output: &mut frame::Audio) -> Result<Option<Delay>, Error> {
output.set_rate(self.output.rate);
unsafe {
match swr_convert_frame(self.as_mut_ptr(), output.as_mut_ptr(), ptr::null()) {
0 =>
Ok(self.delay()),
unsafe {
match swr_convert_frame(self.as_mut_ptr(), output.as_mut_ptr(), ptr::null()) {
0 => Ok(self.delay()),
e =>
Err(Error::from(e))
}
}
}
e => Err(Error::from(e)),
}
}
}
}
impl Drop for Context {
fn drop(&mut self) {
unsafe {
swr_free(&mut self.as_mut_ptr());
}
}
fn drop(&mut self) {
unsafe {
swr_free(&mut self.as_mut_ptr());
}
}
}

View File

@ -1,24 +1,23 @@
use libc::int64_t;
use ffi::*;
use super::Context;
use ffi::*;
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct Delay {
pub seconds: i64,
pub milliseconds: i64,
pub input: i64,
pub output: i64,
pub seconds: i64,
pub milliseconds: i64,
pub input: i64,
pub output: i64,
}
impl Delay {
pub fn from(context: &Context) -> Self {
unsafe {
Delay {
seconds: swr_get_delay(context.as_ptr() as *mut _, 1),
milliseconds: swr_get_delay(context.as_ptr() as *mut _, 1000),
input: swr_get_delay(context.as_ptr() as *mut _, context.input().rate as int64_t),
output: swr_get_delay(context.as_ptr() as *mut _, context.output().rate as int64_t),
}
}
}
pub fn from(context: &Context) -> Self {
unsafe {
Delay {
seconds: swr_get_delay(context.as_ptr() as *mut _, 1),
milliseconds: swr_get_delay(context.as_ptr() as *mut _, 1000),
input: swr_get_delay(context.as_ptr() as *mut _, i64::from(context.input().rate)),
output: swr_get_delay(context.as_ptr() as *mut _, i64::from(context.output().rate)),
}
}
}
}

View File

@ -1,58 +1,58 @@
use ffi::*;
use ffi::SwrDitherType::*;
use ffi::*;
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum Dither {
None,
Rectangular,
Triangular,
TriangularHighPass,
None,
Rectangular,
Triangular,
TriangularHighPass,
NoiseShapingLipshitz,
NoiseShapingFWeighted,
NoiseShapingModifiedEWeighted,
NoiseShapingImprovedEWeighted,
NoiseShapingShibata,
NoiseShapingLowShibata,
NoiseShapingHighShibata,
NoiseShapingLipshitz,
NoiseShapingFWeighted,
NoiseShapingModifiedEWeighted,
NoiseShapingImprovedEWeighted,
NoiseShapingShibata,
NoiseShapingLowShibata,
NoiseShapingHighShibata,
}
impl From<SwrDitherType> for Dither {
fn from(value: SwrDitherType) -> Dither {
match value {
SWR_DITHER_NONE => Dither::None,
SWR_DITHER_RECTANGULAR => Dither::Rectangular,
SWR_DITHER_TRIANGULAR => Dither::Triangular,
SWR_DITHER_TRIANGULAR_HIGHPASS => Dither::TriangularHighPass,
fn from(value: SwrDitherType) -> Dither {
match value {
SWR_DITHER_NONE => Dither::None,
SWR_DITHER_RECTANGULAR => Dither::Rectangular,
SWR_DITHER_TRIANGULAR => Dither::Triangular,
SWR_DITHER_TRIANGULAR_HIGHPASS => Dither::TriangularHighPass,
SWR_DITHER_NS => Dither::None,
SWR_DITHER_NS_LIPSHITZ => Dither::NoiseShapingLipshitz,
SWR_DITHER_NS_F_WEIGHTED => Dither::NoiseShapingFWeighted,
SWR_DITHER_NS_MODIFIED_E_WEIGHTED => Dither::NoiseShapingModifiedEWeighted,
SWR_DITHER_NS_IMPROVED_E_WEIGHTED => Dither::NoiseShapingImprovedEWeighted,
SWR_DITHER_NS_SHIBATA => Dither::NoiseShapingShibata,
SWR_DITHER_NS_LOW_SHIBATA => Dither::NoiseShapingLowShibata,
SWR_DITHER_NS_HIGH_SHIBATA => Dither::NoiseShapingHighShibata,
SWR_DITHER_NB => Dither::None,
}
}
SWR_DITHER_NS => Dither::None,
SWR_DITHER_NS_LIPSHITZ => Dither::NoiseShapingLipshitz,
SWR_DITHER_NS_F_WEIGHTED => Dither::NoiseShapingFWeighted,
SWR_DITHER_NS_MODIFIED_E_WEIGHTED => Dither::NoiseShapingModifiedEWeighted,
SWR_DITHER_NS_IMPROVED_E_WEIGHTED => Dither::NoiseShapingImprovedEWeighted,
SWR_DITHER_NS_SHIBATA => Dither::NoiseShapingShibata,
SWR_DITHER_NS_LOW_SHIBATA => Dither::NoiseShapingLowShibata,
SWR_DITHER_NS_HIGH_SHIBATA => Dither::NoiseShapingHighShibata,
SWR_DITHER_NB => Dither::None,
}
}
}
impl Into<SwrDitherType> for Dither {
fn into(self) -> SwrDitherType {
match self {
Dither::None => SWR_DITHER_NONE,
Dither::Rectangular => SWR_DITHER_RECTANGULAR,
Dither::Triangular => SWR_DITHER_TRIANGULAR,
Dither::TriangularHighPass => SWR_DITHER_TRIANGULAR_HIGHPASS,
fn into(self) -> SwrDitherType {
match self {
Dither::None => SWR_DITHER_NONE,
Dither::Rectangular => SWR_DITHER_RECTANGULAR,
Dither::Triangular => SWR_DITHER_TRIANGULAR,
Dither::TriangularHighPass => SWR_DITHER_TRIANGULAR_HIGHPASS,
Dither::NoiseShapingLipshitz => SWR_DITHER_NS_LIPSHITZ,
Dither::NoiseShapingFWeighted => SWR_DITHER_NS_F_WEIGHTED,
Dither::NoiseShapingModifiedEWeighted => SWR_DITHER_NS_MODIFIED_E_WEIGHTED,
Dither::NoiseShapingImprovedEWeighted => SWR_DITHER_NS_IMPROVED_E_WEIGHTED,
Dither::NoiseShapingShibata => SWR_DITHER_NS_SHIBATA,
Dither::NoiseShapingLowShibata => SWR_DITHER_NS_LOW_SHIBATA,
Dither::NoiseShapingHighShibata => SWR_DITHER_NS_HIGH_SHIBATA,
}
}
Dither::NoiseShapingLipshitz => SWR_DITHER_NS_LIPSHITZ,
Dither::NoiseShapingFWeighted => SWR_DITHER_NS_F_WEIGHTED,
Dither::NoiseShapingModifiedEWeighted => SWR_DITHER_NS_MODIFIED_E_WEIGHTED,
Dither::NoiseShapingImprovedEWeighted => SWR_DITHER_NS_IMPROVED_E_WEIGHTED,
Dither::NoiseShapingShibata => SWR_DITHER_NS_SHIBATA,
Dither::NoiseShapingLowShibata => SWR_DITHER_NS_LOW_SHIBATA,
Dither::NoiseShapingHighShibata => SWR_DITHER_NS_HIGH_SHIBATA,
}
}
}

View File

@ -3,25 +3,25 @@ use sys::SwrEngine::*;
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum Engine {
Software,
SoundExchange,
Software,
SoundExchange,
}
impl From<SwrEngine> for Engine {
fn from(value: SwrEngine) -> Engine {
match value {
SWR_ENGINE_SWR => Engine::Software,
SWR_ENGINE_SOXR => Engine::SoundExchange,
SWR_ENGINE_NB => Engine::Software,
}
}
fn from(value: SwrEngine) -> Engine {
match value {
SWR_ENGINE_SWR => Engine::Software,
SWR_ENGINE_SOXR => Engine::SoundExchange,
SWR_ENGINE_NB => Engine::Software,
}
}
}
impl Into<SwrEngine> for Engine {
fn into(self) -> SwrEngine {
match self {
Engine::Software => SWR_ENGINE_SWR,
Engine::SoundExchange => SWR_ENGINE_SOXR,
}
}
fn into(self) -> SwrEngine {
match self {
Engine::Software => SWR_ENGINE_SWR,
Engine::SoundExchange => SWR_ENGINE_SOXR,
}
}
}

View File

@ -1,19 +1,41 @@
use util::format;
use ::{decoder, Error, ChannelLayout, frame};
use super::Context;
use util::format;
use {decoder, frame, ChannelLayout, Error};
impl frame::Audio {
#[inline]
pub fn resampler(&self, format: format::Sample, channel_layout: ChannelLayout, rate: u32) -> Result<Context, Error> {
Context::get(self.format(), self.channel_layout(), self.rate(),
format, channel_layout, rate)
}
#[inline]
pub fn resampler(
&self,
format: format::Sample,
channel_layout: ChannelLayout,
rate: u32,
) -> Result<Context, Error> {
Context::get(
self.format(),
self.channel_layout(),
self.rate(),
format,
channel_layout,
rate,
)
}
}
impl decoder::Audio {
#[inline]
pub fn resampler(&self, format: format::Sample, channel_layout: ChannelLayout, rate: u32) -> Result<Context, Error> {
Context::get(self.format(), self.channel_layout(), self.rate(),
format, channel_layout, rate)
}
#[inline]
pub fn resampler(
&self,
format: format::Sample,
channel_layout: ChannelLayout,
rate: u32,
) -> Result<Context, Error> {
Context::get(
self.format(),
self.channel_layout(),
self.rate(),
format,
channel_layout,
rate,
)
}
}

View File

@ -1,29 +1,29 @@
use ffi::*;
use ffi::SwrFilterType::*;
use ffi::*;
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum Filter {
Cubic,
BlackmanNuttall,
Kaiser,
Cubic,
BlackmanNuttall,
Kaiser,
}
impl From<SwrFilterType> for Filter {
fn from(value: SwrFilterType) -> Filter {
match value {
SWR_FILTER_TYPE_CUBIC => Filter::Cubic,
SWR_FILTER_TYPE_BLACKMAN_NUTTALL => Filter::BlackmanNuttall,
SWR_FILTER_TYPE_KAISER => Filter::Kaiser,
}
}
fn from(value: SwrFilterType) -> Filter {
match value {
SWR_FILTER_TYPE_CUBIC => Filter::Cubic,
SWR_FILTER_TYPE_BLACKMAN_NUTTALL => Filter::BlackmanNuttall,
SWR_FILTER_TYPE_KAISER => Filter::Kaiser,
}
}
}
impl Into<SwrFilterType> for Filter {
fn into(self) -> SwrFilterType {
match self {
Filter::Cubic => SWR_FILTER_TYPE_CUBIC,
Filter::BlackmanNuttall => SWR_FILTER_TYPE_BLACKMAN_NUTTALL,
Filter::Kaiser => SWR_FILTER_TYPE_KAISER,
}
}
fn into(self) -> SwrFilterType {
match self {
Filter::Cubic => SWR_FILTER_TYPE_CUBIC,
Filter::BlackmanNuttall => SWR_FILTER_TYPE_BLACKMAN_NUTTALL,
Filter::Kaiser => SWR_FILTER_TYPE_KAISER,
}
}
}

View File

@ -1,8 +1,8 @@
use libc::c_int;
use ffi::*;
use libc::c_int;
bitflags! {
pub struct Flags: c_int {
const FORCE = SWR_FLAG_RESAMPLE;
}
pub struct Flags: c_int {
const FORCE = SWR_FLAG_RESAMPLE;
}
}

View File

@ -24,19 +24,13 @@ use std::str::from_utf8_unchecked;
use ffi::*;
pub fn version() -> u32 {
unsafe {
swresample_version()
}
unsafe { swresample_version() }
}
pub fn configuration() -> &'static str {
unsafe {
from_utf8_unchecked(CStr::from_ptr(swresample_configuration()).to_bytes())
}
unsafe { from_utf8_unchecked(CStr::from_ptr(swresample_configuration()).to_bytes()) }
}
pub fn license() -> &'static str {
unsafe {
from_utf8_unchecked(CStr::from_ptr(swresample_license()).to_bytes())
}
unsafe { from_utf8_unchecked(CStr::from_ptr(swresample_license()).to_bytes()) }
}