2019-10-13 10:21:09 +00:00
|
|
|
// Copyright (C) 2019 Ruben Gonzalez <rgonzalez@fluendo.com>
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
2022-01-15 19:18:47 +00:00
|
|
|
//
|
2022-03-14 08:22:53 +00:00
|
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
2019-10-13 10:21:09 +00:00
|
|
|
|
2021-06-03 18:20:54 +00:00
|
|
|
use gst::glib;
|
2019-10-13 10:21:09 +00:00
|
|
|
use gst::subclass::prelude::*;
|
|
|
|
use gst_audio::prelude::*;
|
|
|
|
use gst_audio::subclass::prelude::*;
|
|
|
|
|
|
|
|
use std::io::Cursor;
|
|
|
|
|
|
|
|
use atomic_refcell::AtomicRefCell;
|
|
|
|
|
|
|
|
use byte_slice_cast::*;
|
|
|
|
|
2021-01-21 18:21:29 +00:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
|
|
|
static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
|
|
|
|
gst::DebugCategory::new(
|
|
|
|
"claxondec",
|
|
|
|
gst::DebugColorFlags::empty(),
|
|
|
|
Some("Claxon FLAC decoder"),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2019-10-13 10:21:09 +00:00
|
|
|
struct State {
|
|
|
|
audio_info: Option<gst_audio::AudioInfo>,
|
|
|
|
}
|
|
|
|
|
2021-03-07 16:22:24 +00:00
|
|
|
#[derive(Default)]
|
2020-11-14 16:52:56 +00:00
|
|
|
pub struct ClaxonDec {
|
2019-10-13 10:21:09 +00:00
|
|
|
state: AtomicRefCell<Option<State>>,
|
|
|
|
}
|
|
|
|
|
2021-03-07 16:22:24 +00:00
|
|
|
#[glib::object_subclass]
|
2019-10-13 10:21:09 +00:00
|
|
|
impl ObjectSubclass for ClaxonDec {
|
|
|
|
const NAME: &'static str = "ClaxonDec";
|
2020-11-14 16:52:56 +00:00
|
|
|
type Type = super::ClaxonDec;
|
2019-10-13 10:21:09 +00:00
|
|
|
type ParentType = gst_audio::AudioDecoder;
|
2021-01-21 18:21:29 +00:00
|
|
|
}
|
2019-10-13 10:21:09 +00:00
|
|
|
|
2021-01-21 18:21:29 +00:00
|
|
|
impl ObjectImpl for ClaxonDec {}
|
2019-10-13 10:21:09 +00:00
|
|
|
|
2021-10-23 08:57:31 +00:00
|
|
|
impl GstObjectImpl for ClaxonDec {}
|
|
|
|
|
2021-01-21 18:21:29 +00:00
|
|
|
impl ElementImpl for ClaxonDec {
|
|
|
|
fn metadata() -> Option<&'static gst::subclass::ElementMetadata> {
|
|
|
|
static ELEMENT_METADATA: Lazy<gst::subclass::ElementMetadata> = Lazy::new(|| {
|
|
|
|
gst::subclass::ElementMetadata::new(
|
|
|
|
"Claxon FLAC decoder",
|
|
|
|
"Decoder/Audio",
|
|
|
|
"Claxon FLAC decoder",
|
|
|
|
"Ruben Gonzalez <rgonzalez@fluendo.com>",
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
Some(&*ELEMENT_METADATA)
|
2019-10-13 10:21:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 18:21:29 +00:00
|
|
|
fn pad_templates() -> &'static [gst::PadTemplate] {
|
|
|
|
static PAD_TEMPLATES: Lazy<Vec<gst::PadTemplate>> = Lazy::new(|| {
|
2021-11-06 07:34:10 +00:00
|
|
|
let sink_caps = gst::Caps::builder("audio/x-flac")
|
|
|
|
.field("framed", true)
|
|
|
|
.build();
|
2021-01-21 18:21:29 +00:00
|
|
|
let sink_pad_template = gst::PadTemplate::new(
|
|
|
|
"sink",
|
|
|
|
gst::PadDirection::Sink,
|
|
|
|
gst::PadPresence::Always,
|
|
|
|
&sink_caps,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2022-08-10 10:45:52 +00:00
|
|
|
let src_caps = gst_audio::AudioCapsBuilder::new_interleaved()
|
2022-08-09 11:18:12 +00:00
|
|
|
.format_list([
|
|
|
|
gst_audio::AudioFormat::S8,
|
|
|
|
gst_audio::AUDIO_FORMAT_S16,
|
|
|
|
gst_audio::AUDIO_FORMAT_S2432,
|
|
|
|
gst_audio::AUDIO_FORMAT_S32,
|
|
|
|
])
|
|
|
|
.rate_range(1..655_350)
|
|
|
|
.channels_range(1..8)
|
2021-11-06 07:34:10 +00:00
|
|
|
.build();
|
2021-01-21 18:21:29 +00:00
|
|
|
let src_pad_template = gst::PadTemplate::new(
|
|
|
|
"src",
|
|
|
|
gst::PadDirection::Src,
|
|
|
|
gst::PadPresence::Always,
|
|
|
|
&src_caps,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
vec![sink_pad_template, src_pad_template]
|
|
|
|
});
|
2019-10-13 10:21:09 +00:00
|
|
|
|
2021-01-21 18:21:29 +00:00
|
|
|
PAD_TEMPLATES.as_ref()
|
|
|
|
}
|
|
|
|
}
|
2019-10-13 10:21:09 +00:00
|
|
|
|
|
|
|
impl AudioDecoderImpl for ClaxonDec {
|
2020-11-14 16:52:56 +00:00
|
|
|
fn stop(&self, _element: &Self::Type) -> Result<(), gst::ErrorMessage> {
|
2019-10-13 10:21:09 +00:00
|
|
|
*self.state.borrow_mut() = None;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-11-14 16:52:56 +00:00
|
|
|
fn start(&self, _element: &Self::Type) -> Result<(), gst::ErrorMessage> {
|
2021-05-05 07:37:30 +00:00
|
|
|
*self.state.borrow_mut() = Some(State { audio_info: None });
|
2019-10-13 10:21:09 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-11-14 16:52:56 +00:00
|
|
|
fn set_format(&self, element: &Self::Type, caps: &gst::Caps) -> Result<(), gst::LoggableError> {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, obj: element, "Setting format {:?}", caps);
|
2019-10-13 10:21:09 +00:00
|
|
|
|
|
|
|
let mut audio_info: Option<gst_audio::AudioInfo> = None;
|
|
|
|
|
2021-04-20 12:58:11 +00:00
|
|
|
let s = caps.structure(0).unwrap();
|
2021-11-06 07:34:10 +00:00
|
|
|
if let Ok(Some(streamheaders)) = s.get_optional::<gst::ArrayRef>("streamheader") {
|
2019-10-13 10:21:09 +00:00
|
|
|
let streamheaders = streamheaders.as_slice();
|
|
|
|
|
|
|
|
if streamheaders.len() < 2 {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2021-01-21 18:21:29 +00:00
|
|
|
CAT,
|
2019-10-13 10:21:09 +00:00
|
|
|
obj: element,
|
|
|
|
"Not enough streamheaders, trying in-band"
|
|
|
|
);
|
|
|
|
} else {
|
2021-04-25 12:41:22 +00:00
|
|
|
let ident_buf = streamheaders[0].get::<Option<gst::Buffer>>();
|
2019-10-13 10:21:09 +00:00
|
|
|
if let Ok(Some(ident_buf)) = ident_buf {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, obj: element, "Got streamheader buffers");
|
2019-10-13 10:21:09 +00:00
|
|
|
let inmap = ident_buf.map_readable().unwrap();
|
|
|
|
|
|
|
|
if inmap[0..7] != [0x7f, b'F', b'L', b'A', b'C', 0x01, 0x00] {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, obj: element, "Unknown streamheader format");
|
2021-04-20 12:58:11 +00:00
|
|
|
} else if let Ok(tstreaminfo) = claxon_streaminfo(&inmap[13..]) {
|
2021-05-05 07:37:30 +00:00
|
|
|
if let Ok(taudio_info) = gstaudioinfo(&tstreaminfo) {
|
2020-01-20 06:13:43 +00:00
|
|
|
// To speed up negotiation
|
|
|
|
if element.set_output_format(&taudio_info).is_err()
|
|
|
|
|| element.negotiate().is_err()
|
|
|
|
{
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2021-01-21 18:21:29 +00:00
|
|
|
CAT,
|
2020-01-20 06:13:43 +00:00
|
|
|
obj: element,
|
|
|
|
"Error to negotiate output from based on in-caps streaminfo"
|
|
|
|
);
|
2019-10-13 10:21:09 +00:00
|
|
|
}
|
2020-01-20 06:13:43 +00:00
|
|
|
|
|
|
|
audio_info = Some(taudio_info);
|
2019-10-13 10:21:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut state_guard = self.state.borrow_mut();
|
2021-05-05 07:37:30 +00:00
|
|
|
*state_guard = Some(State { audio_info });
|
2019-10-13 10:21:09 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-20 06:13:43 +00:00
|
|
|
#[allow(clippy::verbose_bit_mask)]
|
2019-10-13 10:21:09 +00:00
|
|
|
fn handle_frame(
|
|
|
|
&self,
|
2020-11-14 16:52:56 +00:00
|
|
|
element: &Self::Type,
|
2019-10-13 10:21:09 +00:00
|
|
|
inbuf: Option<&gst::Buffer>,
|
|
|
|
) -> Result<gst::FlowSuccess, gst::FlowError> {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, obj: element, "Handling buffer {:?}", inbuf);
|
2019-10-13 10:21:09 +00:00
|
|
|
|
|
|
|
let inbuf = match inbuf {
|
|
|
|
None => return Ok(gst::FlowSuccess::Ok),
|
|
|
|
Some(inbuf) => inbuf,
|
|
|
|
};
|
|
|
|
|
|
|
|
let inmap = inbuf.map_readable().map_err(|_| {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::error!(CAT, obj: element, "Failed to buffer readable");
|
2019-10-13 10:21:09 +00:00
|
|
|
gst::FlowError::Error
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let mut state_guard = self.state.borrow_mut();
|
|
|
|
let state = state_guard.as_mut().ok_or(gst::FlowError::NotNegotiated)?;
|
|
|
|
|
|
|
|
if inmap.as_slice() == b"fLaC" {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, obj: element, "fLaC buffer received");
|
2019-10-13 10:21:09 +00:00
|
|
|
} else if inmap[0] & 0x7F == 0x00 {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, obj: element, "Streaminfo header buffer received");
|
2019-10-13 10:21:09 +00:00
|
|
|
return self.handle_streaminfo_header(element, state, inmap.as_ref());
|
|
|
|
} else if inmap[0] == 0b1111_1111 && inmap[1] & 0b1111_1100 == 0b1111_1000 {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, obj: element, "Data buffer received");
|
2019-10-13 10:21:09 +00:00
|
|
|
return self.handle_data(element, state, inmap.as_ref());
|
|
|
|
} else {
|
|
|
|
// info about other headers in flacparse and https://xiph.org/flac/format.html
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2021-01-21 18:21:29 +00:00
|
|
|
CAT,
|
2019-10-13 10:21:09 +00:00
|
|
|
obj: element,
|
|
|
|
"Other header buffer received {:?}",
|
|
|
|
inmap[0] & 0x7F
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
element.finish_frame(None, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ClaxonDec {
|
|
|
|
fn handle_streaminfo_header(
|
|
|
|
&self,
|
2020-11-14 16:52:56 +00:00
|
|
|
element: &super::ClaxonDec,
|
2019-10-13 10:21:09 +00:00
|
|
|
state: &mut State,
|
|
|
|
indata: &[u8],
|
|
|
|
) -> Result<gst::FlowSuccess, gst::FlowError> {
|
2021-04-20 12:58:11 +00:00
|
|
|
let streaminfo = claxon_streaminfo(indata).map_err(|e| {
|
2020-12-20 18:43:45 +00:00
|
|
|
gst::element_error!(element, gst::StreamError::Decode, [e]);
|
2020-04-11 09:30:35 +00:00
|
|
|
gst::FlowError::Error
|
|
|
|
})?;
|
2019-10-13 10:21:09 +00:00
|
|
|
|
2021-05-05 07:37:30 +00:00
|
|
|
let audio_info = gstaudioinfo(&streaminfo).map_err(|e| {
|
2020-12-20 18:43:45 +00:00
|
|
|
gst::element_error!(element, gst::StreamError::Decode, [&e]);
|
2020-04-11 09:30:35 +00:00
|
|
|
gst::FlowError::Error
|
|
|
|
})?;
|
2019-10-13 10:21:09 +00:00
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2021-01-21 18:21:29 +00:00
|
|
|
CAT,
|
2019-10-13 10:21:09 +00:00
|
|
|
obj: element,
|
|
|
|
"Successfully parsed headers: {:?}",
|
|
|
|
audio_info
|
|
|
|
);
|
|
|
|
|
|
|
|
element.set_output_format(&audio_info)?;
|
|
|
|
element.negotiate()?;
|
|
|
|
|
|
|
|
state.audio_info = Some(audio_info);
|
|
|
|
|
|
|
|
element.finish_frame(None, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_data(
|
|
|
|
&self,
|
2020-11-14 16:52:56 +00:00
|
|
|
element: &super::ClaxonDec,
|
2019-10-13 10:21:09 +00:00
|
|
|
state: &mut State,
|
|
|
|
indata: &[u8],
|
|
|
|
) -> Result<gst::FlowSuccess, gst::FlowError> {
|
|
|
|
// TODO It's valid for FLAC to not have any streaminfo header at all, for a small subset
|
|
|
|
// of possible FLAC configurations. (claxon does not actually support that)
|
|
|
|
let audio_info = state
|
|
|
|
.audio_info
|
|
|
|
.as_ref()
|
|
|
|
.ok_or(gst::FlowError::NotNegotiated)?;
|
2020-04-11 10:25:11 +00:00
|
|
|
let depth = AudioDepth::validate(audio_info.depth())?;
|
2019-10-13 10:21:09 +00:00
|
|
|
|
2020-04-11 10:25:11 +00:00
|
|
|
let channels = audio_info.channels() as usize;
|
2019-10-13 10:21:09 +00:00
|
|
|
if channels > 8 {
|
|
|
|
unreachable!(
|
|
|
|
"FLAC only supports from 1 to 8 channels (audio contains {} channels)",
|
|
|
|
channels
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let buffer = Vec::new();
|
|
|
|
let mut cursor = Cursor::new(indata);
|
|
|
|
let mut reader = claxon::frame::FrameReader::new(&mut cursor);
|
|
|
|
let result = match reader.read_next_or_eof(buffer) {
|
|
|
|
Ok(Some(result)) => result,
|
|
|
|
Ok(None) => return element.finish_frame(None, 1),
|
|
|
|
Err(err) => {
|
2020-12-20 18:43:45 +00:00
|
|
|
return gst_audio::audio_decoder_error!(
|
2019-10-13 10:21:09 +00:00
|
|
|
element,
|
|
|
|
1,
|
|
|
|
gst::StreamError::Decode,
|
|
|
|
["Failed to decode packet: {:?}", err]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(cursor.position(), indata.len() as u64);
|
|
|
|
|
|
|
|
let v = if channels != 1 {
|
|
|
|
let mut v: Vec<i32> = vec![0; result.len() as usize];
|
|
|
|
|
|
|
|
for (o, i) in v.chunks_exact_mut(channels).enumerate() {
|
2020-01-20 06:13:43 +00:00
|
|
|
for (c, s) in i.iter_mut().enumerate() {
|
|
|
|
*s = result.sample(c as u32, o as u32);
|
2019-10-13 10:21:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
v
|
|
|
|
} else {
|
|
|
|
result.into_buffer()
|
|
|
|
};
|
|
|
|
|
2020-04-11 10:25:11 +00:00
|
|
|
let depth_adjusted = depth.adjust_samples(v);
|
2020-10-13 09:38:02 +00:00
|
|
|
let outbuf = gst::Buffer::from_mut_slice(depth_adjusted);
|
2020-04-11 10:25:11 +00:00
|
|
|
element.finish_frame(Some(outbuf), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Depth of audio samples
|
|
|
|
enum AudioDepth {
|
|
|
|
/// 8bits.
|
|
|
|
I8,
|
|
|
|
/// 16bits.
|
|
|
|
I16,
|
|
|
|
/// 24bits.
|
|
|
|
I24,
|
|
|
|
/// 32bits.
|
|
|
|
I32,
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:38:02 +00:00
|
|
|
enum ByteVec {
|
|
|
|
I8(Vec<i8>),
|
|
|
|
I16(Vec<i16>),
|
|
|
|
I32(Vec<i32>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<[u8]> for ByteVec {
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
match self {
|
|
|
|
ByteVec::I8(ref vec) => vec.as_byte_slice(),
|
|
|
|
ByteVec::I16(ref vec) => vec.as_byte_slice(),
|
|
|
|
ByteVec::I32(ref vec) => vec.as_byte_slice(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsMut<[u8]> for ByteVec {
|
|
|
|
fn as_mut(&mut self) -> &mut [u8] {
|
|
|
|
match self {
|
|
|
|
ByteVec::I8(ref mut vec) => vec.as_mut_byte_slice(),
|
|
|
|
ByteVec::I16(ref mut vec) => vec.as_mut_byte_slice(),
|
|
|
|
ByteVec::I32(ref mut vec) => vec.as_mut_byte_slice(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 10:25:11 +00:00
|
|
|
impl AudioDepth {
|
|
|
|
/// Validate input audio depth.
|
|
|
|
fn validate(input: u32) -> Result<Self, gst::FlowError> {
|
|
|
|
let depth = match input {
|
|
|
|
8 => AudioDepth::I8,
|
|
|
|
16 => AudioDepth::I16,
|
|
|
|
24 => AudioDepth::I24,
|
|
|
|
32 => AudioDepth::I32,
|
|
|
|
_ => return Err(gst::FlowError::NotSupported),
|
2019-10-13 10:21:09 +00:00
|
|
|
};
|
2020-04-11 10:25:11 +00:00
|
|
|
Ok(depth)
|
|
|
|
}
|
2019-10-13 10:21:09 +00:00
|
|
|
|
2020-04-11 10:25:11 +00:00
|
|
|
/// Adjust samples depth.
|
|
|
|
///
|
|
|
|
/// This takes a vector of 32bits samples, adjusts the depth of each,
|
|
|
|
/// and returns the adjusted bytes stream.
|
2020-10-13 09:38:02 +00:00
|
|
|
fn adjust_samples(&self, input: Vec<i32>) -> ByteVec {
|
2020-04-11 10:25:11 +00:00
|
|
|
match *self {
|
2020-10-13 09:38:02 +00:00
|
|
|
AudioDepth::I8 => ByteVec::I8(input.into_iter().map(|x| x as i8).collect::<Vec<_>>()),
|
|
|
|
AudioDepth::I16 => {
|
|
|
|
ByteVec::I16(input.into_iter().map(|x| x as i16).collect::<Vec<_>>())
|
|
|
|
}
|
|
|
|
AudioDepth::I24 | AudioDepth::I32 => ByteVec::I32(input),
|
2020-04-11 10:25:11 +00:00
|
|
|
}
|
2019-10-13 10:21:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 12:57:40 +00:00
|
|
|
fn claxon_streaminfo(indata: &[u8]) -> Result<claxon::metadata::StreamInfo, &'static str> {
|
2019-10-13 10:21:09 +00:00
|
|
|
let mut cursor = Cursor::new(indata);
|
|
|
|
let mut metadata_iter = claxon::metadata::MetadataBlockReader::new(&mut cursor);
|
|
|
|
let streaminfo = match metadata_iter.next() {
|
|
|
|
Some(Ok(claxon::metadata::MetadataBlock::StreamInfo(info))) => info,
|
|
|
|
_ => return Err("Failed to decode STREAMINFO"),
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(cursor.position(), indata.len() as u64);
|
|
|
|
|
|
|
|
Ok(streaminfo)
|
|
|
|
}
|
|
|
|
|
2021-05-05 07:37:30 +00:00
|
|
|
fn gstaudioinfo(streaminfo: &claxon::metadata::StreamInfo) -> Result<gst_audio::AudioInfo, String> {
|
2019-10-13 10:21:09 +00:00
|
|
|
let format = match streaminfo.bits_per_sample {
|
|
|
|
8 => gst_audio::AudioFormat::S8,
|
|
|
|
16 => gst_audio::AUDIO_FORMAT_S16,
|
|
|
|
24 => gst_audio::AUDIO_FORMAT_S2432,
|
|
|
|
32 => gst_audio::AUDIO_FORMAT_S32,
|
2020-04-11 09:30:35 +00:00
|
|
|
_ => return Err("format not supported".to_string()),
|
2019-10-13 10:21:09 +00:00
|
|
|
};
|
|
|
|
|
2020-04-11 09:30:35 +00:00
|
|
|
let index = match streaminfo.channels as usize {
|
|
|
|
0 => return Err("no channels".to_string()),
|
|
|
|
n if n > 8 => return Err("more than 8 channels, not supported yet".to_string()),
|
|
|
|
n => n,
|
|
|
|
};
|
2019-10-13 10:21:09 +00:00
|
|
|
let to = &FLAC_CHANNEL_POSITIONS[index - 1][..index];
|
2020-04-11 09:30:35 +00:00
|
|
|
let info_builder =
|
2020-06-25 16:49:07 +00:00
|
|
|
gst_audio::AudioInfo::builder(format, streaminfo.sample_rate, streaminfo.channels)
|
2020-04-11 09:30:35 +00:00
|
|
|
.positions(to);
|
|
|
|
|
|
|
|
let audio_info = info_builder
|
|
|
|
.build()
|
|
|
|
.map_err(|e| format!("failed to build audio info: {}", e))?;
|
2019-10-13 10:21:09 +00:00
|
|
|
|
2020-04-11 09:30:35 +00:00
|
|
|
Ok(audio_info)
|
2019-10-13 10:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9
|
|
|
|
// http://flac.sourceforge.net/format.html#frame_header
|
|
|
|
const FLAC_CHANNEL_POSITIONS: [[gst_audio::AudioChannelPosition; 8]; 8] = [
|
|
|
|
[
|
|
|
|
gst_audio::AudioChannelPosition::Mono,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
gst_audio::AudioChannelPosition::FrontLeft,
|
|
|
|
gst_audio::AudioChannelPosition::FrontRight,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
gst_audio::AudioChannelPosition::FrontLeft,
|
|
|
|
gst_audio::AudioChannelPosition::FrontCenter,
|
|
|
|
gst_audio::AudioChannelPosition::FrontRight,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
gst_audio::AudioChannelPosition::FrontLeft,
|
|
|
|
gst_audio::AudioChannelPosition::FrontRight,
|
|
|
|
gst_audio::AudioChannelPosition::RearLeft,
|
|
|
|
gst_audio::AudioChannelPosition::RearRight,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
gst_audio::AudioChannelPosition::FrontLeft,
|
|
|
|
gst_audio::AudioChannelPosition::FrontCenter,
|
|
|
|
gst_audio::AudioChannelPosition::FrontRight,
|
|
|
|
gst_audio::AudioChannelPosition::RearLeft,
|
|
|
|
gst_audio::AudioChannelPosition::RearRight,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
gst_audio::AudioChannelPosition::FrontLeft,
|
|
|
|
gst_audio::AudioChannelPosition::FrontCenter,
|
|
|
|
gst_audio::AudioChannelPosition::FrontRight,
|
|
|
|
gst_audio::AudioChannelPosition::RearLeft,
|
|
|
|
gst_audio::AudioChannelPosition::RearRight,
|
|
|
|
gst_audio::AudioChannelPosition::Lfe1,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
],
|
|
|
|
// FIXME: 7/8 channel layouts are not defined in the FLAC specs
|
|
|
|
[
|
|
|
|
gst_audio::AudioChannelPosition::FrontLeft,
|
|
|
|
gst_audio::AudioChannelPosition::FrontCenter,
|
|
|
|
gst_audio::AudioChannelPosition::FrontRight,
|
|
|
|
gst_audio::AudioChannelPosition::SideLeft,
|
|
|
|
gst_audio::AudioChannelPosition::SideRight,
|
|
|
|
gst_audio::AudioChannelPosition::RearCenter,
|
|
|
|
gst_audio::AudioChannelPosition::Lfe1,
|
|
|
|
gst_audio::AudioChannelPosition::Invalid,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
gst_audio::AudioChannelPosition::FrontLeft,
|
|
|
|
gst_audio::AudioChannelPosition::FrontCenter,
|
|
|
|
gst_audio::AudioChannelPosition::FrontRight,
|
|
|
|
gst_audio::AudioChannelPosition::SideLeft,
|
|
|
|
gst_audio::AudioChannelPosition::SideRight,
|
|
|
|
gst_audio::AudioChannelPosition::RearLeft,
|
|
|
|
gst_audio::AudioChannelPosition::RearRight,
|
|
|
|
gst_audio::AudioChannelPosition::Lfe1,
|
|
|
|
],
|
|
|
|
];
|