👕 refactor: hide ffmpeg audio frame from public decoder API

main
_ 2021-11-13 17:37:55 +00:00
parent 68461866d8
commit be3c76d9d8
2 changed files with 40 additions and 24 deletions

View File

@ -24,12 +24,39 @@ use ffmpeg_next::{
self, self,
Sample, Sample,
}, },
frame::Audio as AudioFrame, frame::Audio as FfAudioFrame,
}, },
}; };
pub const SAMPLE_RATE: u32 = 48000; pub const SAMPLE_RATE: u32 = 48000;
pub struct AudioFrame {
frame: FfAudioFrame,
}
impl AudioFrame {
pub fn data (&self) -> &[u8] {
// Hard-coded because I'm only support f32 interleaved stereo
&self.frame.data (0) [0..self.frame.samples () * 4 * 2]
}
pub fn rate (&self) -> u32 {
self.frame.rate ()
}
pub fn samples (&self) -> usize {
self.frame.samples ()
}
}
impl From <FfAudioFrame> for AudioFrame {
fn from (frame: FfAudioFrame) -> Self {
Self {
frame,
}
}
}
#[derive (Default)] #[derive (Default)]
pub struct PcmBuffers { pub struct PcmBuffers {
buffers: Vec <Vec <f32>>, buffers: Vec <Vec <f32>>,
@ -70,6 +97,8 @@ impl PcmBuffers {
let mut b = vec! [0.0f32; new_buffer.len () / 4]; let mut b = vec! [0.0f32; new_buffer.len () / 4];
let mut rdr = Cursor::new (new_buffer); let mut rdr = Cursor::new (new_buffer);
// Pointless memcpy. I can get rid of it later by changing PcmBuffers'
// internal format
rdr.read_f32_into::<LittleEndian> (&mut b).unwrap (); rdr.read_f32_into::<LittleEndian> (&mut b).unwrap ();
self.produce (b); self.produce (b);
@ -88,7 +117,7 @@ pub struct Decoder {
pub resampler: ResamplingContext, pub resampler: ResamplingContext,
best_stream_idx: usize, best_stream_idx: usize,
dummy_frame: Option <AudioFrame>, dummy_frame: Option <FfAudioFrame>,
} }
impl Decoder { impl Decoder {
@ -117,8 +146,8 @@ impl Decoder {
}) })
} }
fn new_frame () -> AudioFrame { fn new_frame () -> FfAudioFrame {
let mut x = AudioFrame::empty (); let mut x = FfAudioFrame::empty ();
x.set_channel_layout (ChannelLayout::STEREO); x.set_channel_layout (ChannelLayout::STEREO);
x.set_format (Sample::F32 (sample::Type::Packed)); x.set_format (Sample::F32 (sample::Type::Packed));
x x
@ -129,18 +158,6 @@ impl Decoder {
assert_eq! (frame.rate (), 48000); assert_eq! (frame.rate (), 48000);
assert! (frame.samples () > 0); assert! (frame.samples () > 0);
let actual_bytes = frame.data (0).len ();
let expected_bytes = frame.samples () * 4 * 2;
assert! (actual_bytes >= expected_bytes);
if actual_bytes > expected_bytes {
let extra_bytes = actual_bytes - expected_bytes;
let extra_samples = extra_bytes / 4 / 2;
// tracing::debug! ("Extra bytes: {}", extra_bytes);
// tracing::debug! ("Extra samples: {}", extra_samples);
}
Some (frame) Some (frame)
} }
else { else {
@ -191,7 +208,7 @@ impl Decoder {
Ok (if frame_resampled.samples () > 0 { Ok (if frame_resampled.samples () > 0 {
// tracing::trace! ("Pulled from resampler FIFO"); // tracing::trace! ("Pulled from resampler FIFO");
Some (frame_resampled) Some (frame_resampled.into ())
} }
else { else {
None None
@ -199,13 +216,13 @@ impl Decoder {
} }
pub fn pump_decoder (&mut self) -> Result <Option <AudioFrame>> { pub fn pump_decoder (&mut self) -> Result <Option <AudioFrame>> {
let mut frame_src = AudioFrame::empty (); let mut frame_src = FfAudioFrame::empty ();
if let Err (_) = self.decoder.receive_frame (&mut frame_src) { if let Err (_) = self.decoder.receive_frame (&mut frame_src) {
return Ok (None); return Ok (None);
}; };
if self.dummy_frame.is_none () { if self.dummy_frame.is_none () {
let mut dummy_frame = AudioFrame::new ( let mut dummy_frame = FfAudioFrame::new (
frame_src.format (), frame_src.format (),
0, 0,
frame_src.channel_layout (), frame_src.channel_layout (),
@ -216,14 +233,14 @@ impl Decoder {
let nb_output_samples = frame_src.samples (); let nb_output_samples = frame_src.samples ();
let mut frame_resampled = AudioFrame::new ( let mut frame_resampled = FfAudioFrame::new (
Sample::F32 (sample::Type::Packed), Sample::F32 (sample::Type::Packed),
nb_output_samples, nb_output_samples,
ChannelLayout::STEREO ChannelLayout::STEREO
); );
self.resampler.run (&frame_src, &mut frame_resampled)?; self.resampler.run (&frame_src, &mut frame_resampled)?;
Ok (Some (frame_resampled)) Ok (Some (frame_resampled.into ()))
} }
pub fn pump_demuxer (&mut self) -> Result <bool> { pub fn pump_demuxer (&mut self) -> Result <bool> {

View File

@ -52,8 +52,7 @@ fn cmd_debug_dump (args: &[String]) -> Result <()> {
let mut f = File::create ("pcm-dump.data")?; let mut f = File::create ("pcm-dump.data")?;
while let Some (frame) = decoder.next ()? { while let Some (frame) = decoder.next ()? {
f.write_all (&frame.data (0) [0..frame.samples () * 4 * 2])?; f.write_all (frame.data ())?;
// f.write_all (frame.data (0))?;
} }
Ok (()) Ok (())
@ -116,7 +115,7 @@ fn cmd_play (args: &[String]) -> Result <()> {
while pcm_buffers.samples_available () < 12_000 { while pcm_buffers.samples_available () < 12_000 {
// tracing::trace! ("Decoder is trying to work..."); // tracing::trace! ("Decoder is trying to work...");
match decoder.next ()? { match decoder.next ()? {
Some (frame) => pcm_buffers.produce_bytes (&frame.data (0) [0..frame.samples () * 4 * 2]), Some (frame) => pcm_buffers.produce_bytes (frame.data ()),
None => { None => {
tracing::info! ("Finished decoding file"); tracing::info! ("Finished decoding file");
break 'one_file; break 'one_file;