262 lines
5.5 KiB
Rust
262 lines
5.5 KiB
Rust
use std::{
|
|
io::Cursor,
|
|
};
|
|
|
|
use anyhow::{
|
|
anyhow,
|
|
Result,
|
|
};
|
|
|
|
use byteorder::{
|
|
LittleEndian,
|
|
ReadBytesExt,
|
|
};
|
|
|
|
// This crate flitters between being very convenient and being a type labyrinth.
|
|
|
|
use ffmpeg_next::{
|
|
decoder::Audio as DecodeContext,
|
|
format::context::Input as DemuxContext,
|
|
software::resampling::Context as ResamplingContext,
|
|
util::{
|
|
channel_layout::ChannelLayout,
|
|
format::sample::{
|
|
self,
|
|
Sample,
|
|
},
|
|
frame::Audio as AudioFrame,
|
|
},
|
|
};
|
|
|
|
pub const SAMPLE_RATE: u32 = 48000;
|
|
|
|
#[derive (Default)]
|
|
pub struct PcmBuffers {
|
|
buffers: Vec <Vec <f32>>,
|
|
|
|
// Always points into the first buffer, if any
|
|
consumer_cursor: usize,
|
|
}
|
|
|
|
impl PcmBuffers {
|
|
pub fn samples_available (&self) -> usize {
|
|
self.buffers.iter ().map (|b| b.len ()).sum::<usize> () - self.consumer_cursor
|
|
}
|
|
|
|
#[warn(unused_must_use)]
|
|
pub fn consume_exact (&mut self, data: &mut [f32]) -> bool {
|
|
if data.len () > self.samples_available () {
|
|
return false;
|
|
}
|
|
|
|
for x in data {
|
|
if self.consumer_cursor >= self.buffers [0].len () {
|
|
self.buffers.remove (0);
|
|
self.consumer_cursor = 0;
|
|
}
|
|
|
|
*x = self.buffers [0][self.consumer_cursor];
|
|
self.consumer_cursor += 1;
|
|
}
|
|
|
|
true
|
|
}
|
|
|
|
pub fn produce (&mut self, new_buffer: Vec <f32>) {
|
|
self.buffers.push (new_buffer);
|
|
}
|
|
|
|
pub fn produce_bytes (&mut self, new_buffer: &[u8]) {
|
|
let mut b = vec! [0.0f32; new_buffer.len () / 4];
|
|
let mut rdr = Cursor::new (new_buffer);
|
|
|
|
rdr.read_f32_into::<LittleEndian> (&mut b).unwrap ();
|
|
|
|
self.produce (b);
|
|
}
|
|
}
|
|
|
|
#[derive (Default)]
|
|
pub struct SharedState {
|
|
pub pcm_buffers: PcmBuffers,
|
|
pub quit: bool,
|
|
}
|
|
|
|
pub struct Decoder {
|
|
pub input_ctx: DemuxContext,
|
|
pub decoder: DecodeContext,
|
|
pub resampler: ResamplingContext,
|
|
|
|
best_stream_idx: usize,
|
|
dummy_frame: Option <AudioFrame>,
|
|
}
|
|
|
|
impl Decoder {
|
|
pub fn new (filename: &str) -> Result <Self> {
|
|
let input_ctx = ffmpeg_next::format::input (&filename)?;
|
|
let stream = input_ctx
|
|
.streams ()
|
|
.best (ffmpeg_next::media::Type::Audio)
|
|
.ok_or_else (|| anyhow! ("can't find good audio stream"))?;
|
|
let best_stream_idx = stream.index ();
|
|
|
|
let decoder = stream.codec ().decoder ().audio ()?;
|
|
let resampler = decoder.resampler (
|
|
Sample::F32 (sample::Type::Packed),
|
|
ChannelLayout::STEREO,
|
|
48000,
|
|
)?;
|
|
|
|
Ok (Self {
|
|
input_ctx,
|
|
decoder,
|
|
resampler,
|
|
|
|
best_stream_idx,
|
|
dummy_frame: None,
|
|
})
|
|
}
|
|
|
|
fn new_frame () -> AudioFrame {
|
|
let mut x = AudioFrame::empty ();
|
|
x.set_channel_layout (ChannelLayout::STEREO);
|
|
x.set_format (Sample::F32 (sample::Type::Packed));
|
|
x
|
|
}
|
|
|
|
pub fn next (&mut self) -> Result <Option <AudioFrame>> {
|
|
Ok (if let Some (frame) = self.pump ()? {
|
|
assert_eq! (frame.rate (), 48000);
|
|
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)
|
|
}
|
|
else {
|
|
None
|
|
})
|
|
}
|
|
|
|
fn pump (&mut self) -> Result <Option <AudioFrame>> {
|
|
loop {
|
|
match self.pump_resampler ()? {
|
|
Some (x) => {
|
|
// tracing::trace! ("Pumped resampler");
|
|
return Ok (Some (x));
|
|
},
|
|
None => (),
|
|
}
|
|
|
|
match self.pump_decoder ()? {
|
|
Some (x) => {
|
|
// tracing::trace! ("Pumped decoder");
|
|
return Ok (Some (x));
|
|
},
|
|
None => (),
|
|
}
|
|
|
|
if self.pump_demuxer ()? {
|
|
// tracing::trace! ("Pumped demuxer");
|
|
continue;
|
|
}
|
|
else {
|
|
break;
|
|
}
|
|
}
|
|
|
|
Ok (None)
|
|
}
|
|
|
|
pub fn pump_resampler (&mut self) -> Result <Option <AudioFrame>> {
|
|
let frame_src = match self.dummy_frame.as_ref () {
|
|
None => return Ok (None),
|
|
Some (x) => x,
|
|
};
|
|
|
|
let mut frame_resampled = Self::new_frame ();
|
|
|
|
let _rc = self.resampler.run (&frame_src, &mut frame_resampled)?;
|
|
// dbg! (&frame_resampled, rc);
|
|
|
|
Ok (if frame_resampled.samples () > 0 {
|
|
// tracing::trace! ("Pulled from resampler FIFO");
|
|
Some (frame_resampled)
|
|
}
|
|
else {
|
|
None
|
|
})
|
|
}
|
|
|
|
pub fn pump_decoder (&mut self) -> Result <Option <AudioFrame>> {
|
|
let mut frame_src = AudioFrame::empty ();
|
|
if let Err (_) = self.decoder.receive_frame (&mut frame_src) {
|
|
return Ok (None);
|
|
};
|
|
|
|
if self.dummy_frame.is_none () {
|
|
let mut dummy_frame = AudioFrame::new (
|
|
frame_src.format (),
|
|
0,
|
|
frame_src.channel_layout (),
|
|
);
|
|
dummy_frame.set_rate (frame_src.rate ());
|
|
self.dummy_frame = Some (dummy_frame);
|
|
}
|
|
|
|
let nb_output_samples = frame_src.samples ();
|
|
|
|
let mut frame_resampled = AudioFrame::new (
|
|
Sample::F32 (sample::Type::Packed),
|
|
nb_output_samples,
|
|
ChannelLayout::STEREO
|
|
);
|
|
|
|
self.resampler.run (&frame_src, &mut frame_resampled)?;
|
|
Ok (Some (frame_resampled))
|
|
}
|
|
|
|
pub fn pump_demuxer (&mut self) -> Result <bool> {
|
|
while let Some ((stream, packet)) = self.input_ctx.packets ().next () {
|
|
if stream.index () == self.best_stream_idx {
|
|
// tracing::trace! ("demuxed packet");
|
|
self.decoder.send_packet (&packet)?;
|
|
return Ok (true);
|
|
}
|
|
}
|
|
|
|
Ok (false)
|
|
}
|
|
}
|
|
|
|
#[cfg (test)]
|
|
mod test {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn pcm_buffer () {
|
|
let mut x = PcmBuffers::default ();
|
|
|
|
x.produce (vec! [0.0, 1.0, 2.0, 3.0]);
|
|
x.produce (vec! [4.0, 5.0, 6.0, 7.0]);
|
|
|
|
let mut data = vec! [0.0; 3];
|
|
|
|
assert! (x.consume_exact (&mut data));
|
|
assert_eq! (&data, &[0.0, 1.0, 2.0]);
|
|
|
|
assert! (x.consume_exact (&mut data));
|
|
assert_eq! (&data, &[3.0, 4.0, 5.0]);
|
|
}
|
|
}
|