mirror of
https://github.com/Serial-ATA/lofty-rs
synced 2025-03-04 06:47:12 +00:00
EBML: Get MKA properties test passing
This commit is contained in:
parent
ef5ab65c4e
commit
0cf31014b4
4 changed files with 172 additions and 53 deletions
|
@ -146,13 +146,17 @@ ebml_master_elements! {
|
|||
TrackEntry: {
|
||||
id: 0xAE,
|
||||
children: [
|
||||
TrackNumber: { 0xD7, UnsignedInt },
|
||||
TrackUid: { 0x73C5, UnsignedInt },
|
||||
TrackType: { 0x83, UnsignedInt },
|
||||
FlagEnabled: { 0xB9, UnsignedInt },
|
||||
FlagDefault: { 0x88, UnsignedInt },
|
||||
DefaultDuration: { 0x23E3_83, UnsignedInt },
|
||||
TrackTimecodeScale: { 0x2331_59, Float },
|
||||
Language: { 0x22B5_9C, String },
|
||||
LanguageBCP47: { 0x22B59D, String },
|
||||
CodecID: { 0x86, String },
|
||||
CodecPrivate: { 0x63A2, Binary },
|
||||
CodecName: { 0x258688, Utf8 },
|
||||
CodecDelay: { 0x56AA, UnsignedInt },
|
||||
SeekPreRoll: { 0x56BB, UnsignedInt },
|
||||
|
@ -168,6 +172,7 @@ ebml_master_elements! {
|
|||
OutputSamplingFrequency: { 0x78B5, Float },
|
||||
Channels: { 0x9F, UnsignedInt },
|
||||
BitDepth: { 0x6264, UnsignedInt },
|
||||
Emphasis: { 0x52F1, UnsignedInt },
|
||||
],
|
||||
},
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use super::Language;
|
||||
use crate::properties::FileProperties;
|
||||
|
||||
/// Properties from the EBML header
|
||||
|
@ -113,17 +114,41 @@ impl Default for SegmentInfo {
|
|||
}
|
||||
|
||||
/// A full descriptor for an audio track
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct AudioTrackDescriptor {
|
||||
pub(crate) number: u64,
|
||||
pub(crate) uid: u64,
|
||||
pub(crate) language: String,
|
||||
pub(crate) enabled: bool,
|
||||
pub(crate) default: bool,
|
||||
pub(crate) language: Language,
|
||||
pub(crate) default_duration: u64,
|
||||
pub(crate) codec_id: String,
|
||||
pub(crate) codec_private: Vec<u8>,
|
||||
pub(crate) codec_private: Option<Vec<u8>>,
|
||||
pub(crate) codec_name: Option<String>,
|
||||
pub(crate) settings: AudioTrackSettings,
|
||||
}
|
||||
|
||||
impl Default for AudioTrackDescriptor {
|
||||
fn default() -> Self {
|
||||
AudioTrackDescriptor {
|
||||
// Note, these values are not spec compliant and will hopefully be overwritten when
|
||||
// parsing. It doesn't really matter though, since we aren't an encoder.
|
||||
number: 0,
|
||||
uid: 0,
|
||||
default_duration: 0,
|
||||
codec_id: String::new(),
|
||||
|
||||
// Spec-compliant defaults
|
||||
enabled: true,
|
||||
default: true,
|
||||
language: Language::Iso639_2(String::from("eng")),
|
||||
codec_private: None,
|
||||
codec_name: None,
|
||||
settings: AudioTrackSettings::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AudioTrackDescriptor {
|
||||
/// The track number
|
||||
pub fn number(&self) -> u64 {
|
||||
|
@ -135,10 +160,20 @@ impl AudioTrackDescriptor {
|
|||
self.uid
|
||||
}
|
||||
|
||||
/// Whether the track is usable
|
||||
pub fn is_enabled(&self) -> bool {
|
||||
self.enabled
|
||||
}
|
||||
|
||||
/// Whether the track is eligible for automatic selection
|
||||
pub fn is_default(&self) -> bool {
|
||||
self.default
|
||||
}
|
||||
|
||||
/// The language of the track, in the Matroska languages form
|
||||
///
|
||||
/// NOTE: See [basics](https://matroska.org/technical/basics.html#language-codes) on language codes.
|
||||
pub fn language(&self) -> &str {
|
||||
pub fn language(&self) -> &Language {
|
||||
&self.language
|
||||
}
|
||||
|
||||
|
@ -157,8 +192,13 @@ impl AudioTrackDescriptor {
|
|||
}
|
||||
|
||||
/// Private data only known to the codec
|
||||
pub fn codec_private(&self) -> &[u8] {
|
||||
&self.codec_private
|
||||
pub fn codec_private(&self) -> Option<&[u8]> {
|
||||
self.codec_private.as_deref()
|
||||
}
|
||||
|
||||
/// A human-readable string for the [codec_id](AudioTrackDescriptor::codec_id)
|
||||
pub fn codec_name(&self) -> Option<&str> {
|
||||
self.codec_name.as_deref()
|
||||
}
|
||||
|
||||
/// The audio settings of the track
|
||||
|
@ -170,8 +210,8 @@ impl AudioTrackDescriptor {
|
|||
/// Settings for an audio track
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct AudioTrackSettings {
|
||||
pub(crate) sampling_frequency: u32,
|
||||
pub(crate) output_sampling_frequency: u32,
|
||||
pub(crate) sampling_frequency: f64,
|
||||
pub(crate) output_sampling_frequency: f64,
|
||||
pub(crate) channels: u8,
|
||||
pub(crate) bit_depth: Option<u8>,
|
||||
pub(crate) emphasis: Option<EbmlAudioTrackEmphasis>,
|
||||
|
@ -179,14 +219,14 @@ pub struct AudioTrackSettings {
|
|||
|
||||
impl AudioTrackSettings {
|
||||
/// The sampling frequency of the track
|
||||
pub fn sampling_frequency(&self) -> u32 {
|
||||
pub fn sampling_frequency(&self) -> f64 {
|
||||
self.sampling_frequency
|
||||
}
|
||||
|
||||
/// Real output sampling frequency in Hz (used for SBR techniques).
|
||||
///
|
||||
/// The default value for `output_sampling_frequency` of the same TrackEntry is equal to the [`Self::sampling_frequency`].
|
||||
pub fn output_sampling_frequency(&self) -> u32 {
|
||||
pub fn output_sampling_frequency(&self) -> f64 {
|
||||
self.output_sampling_frequency
|
||||
}
|
||||
|
||||
|
@ -210,7 +250,6 @@ impl AudioTrackSettings {
|
|||
#[allow(missing_docs)]
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||
pub enum EbmlAudioTrackEmphasis {
|
||||
None = 0,
|
||||
CdAudio = 1,
|
||||
Reserved = 2,
|
||||
CcitJ17 = 3,
|
||||
|
@ -225,6 +264,27 @@ pub enum EbmlAudioTrackEmphasis {
|
|||
PhonoNartb = 16,
|
||||
}
|
||||
|
||||
impl EbmlAudioTrackEmphasis {
|
||||
/// Get the audio emphasis from a `u8`
|
||||
pub fn from_u8(value: u8) -> Option<Self> {
|
||||
match value {
|
||||
1 => Some(Self::CdAudio),
|
||||
2 => Some(Self::Reserved),
|
||||
3 => Some(Self::CcitJ17),
|
||||
4 => Some(Self::Fm50),
|
||||
5 => Some(Self::Fm75),
|
||||
10 => Some(Self::PhonoRiaa),
|
||||
11 => Some(Self::PhonoIecN78),
|
||||
12 => Some(Self::PhonoTeldec),
|
||||
13 => Some(Self::PhonoEmi),
|
||||
14 => Some(Self::PhonoColumbiaLp),
|
||||
15 => Some(Self::PhonoLondon),
|
||||
16 => Some(Self::PhonoNartb),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// EBML audio properties
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct EbmlProperties {
|
||||
|
@ -248,16 +308,14 @@ impl EbmlProperties {
|
|||
&self.extensions
|
||||
}
|
||||
|
||||
/// Information from the `\EBML\Segment\Info` element
|
||||
/// Information from the `\Segment\Info` element
|
||||
pub fn segment_info(&self) -> &SegmentInfo {
|
||||
&self.segment_info
|
||||
}
|
||||
|
||||
/// All audio tracks in the file
|
||||
///
|
||||
/// This includes all audio tracks in the Matroska `\EBML\Segment\Tracks` element.
|
||||
///
|
||||
/// NOTE: The first audio track is **always** the default audio track.
|
||||
/// This includes all audio tracks in the Matroska `\Segment\Tracks` element.
|
||||
pub fn audio_tracks(&self) -> &[AudioTrackDescriptor] {
|
||||
&self.audio_tracks
|
||||
}
|
||||
|
@ -265,11 +323,9 @@ impl EbmlProperties {
|
|||
/// Information about the default audio track
|
||||
///
|
||||
/// The information is extracted from the first audio track with its default flag set
|
||||
/// in the `\EBML\Segment\Tracks` element.
|
||||
///
|
||||
/// NOTE: This will always return `Some` unless [`ParseOptions::read_properties`](crate::config::ParseOptions::read_properties) is set to `false`.
|
||||
/// in the `\Segment\Tracks` element.
|
||||
pub fn default_audio_track(&self) -> Option<&AudioTrackDescriptor> {
|
||||
self.audio_tracks.first()
|
||||
self.audio_tracks.iter().find(|track| track.default)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -283,7 +339,7 @@ impl From<EbmlProperties> for FileProperties {
|
|||
duration: todo!("Support duration"),
|
||||
overall_bitrate: todo!("Support bitrate"),
|
||||
audio_bitrate: todo!("Support bitrate"),
|
||||
sample_rate: Some(default_audio_track.settings.sampling_frequency),
|
||||
sample_rate: Some(default_audio_track.settings.sampling_frequency as u32),
|
||||
bit_depth: default_audio_track.settings.bit_depth,
|
||||
channels: Some(default_audio_track.settings.channels),
|
||||
channel_mask: todo!("Channel mask"),
|
||||
|
|
|
@ -3,6 +3,7 @@ use crate::ebml::element_reader::{
|
|||
ChildElementDescriptor, ElementChildIterator, ElementIdent, ElementReaderYield,
|
||||
};
|
||||
use crate::ebml::properties::EbmlProperties;
|
||||
use crate::ebml::{AudioTrackDescriptor, EbmlAudioTrackEmphasis, Language};
|
||||
use crate::error::Result;
|
||||
|
||||
use std::io::{Read, Seek};
|
||||
|
@ -10,17 +11,15 @@ use std::io::{Read, Seek};
|
|||
pub(super) fn read_from<R>(
|
||||
children_reader: &mut ElementChildIterator<'_, R>,
|
||||
parse_options: ParseOptions,
|
||||
_properties: &mut EbmlProperties,
|
||||
properties: &mut EbmlProperties,
|
||||
) -> Result<()>
|
||||
where
|
||||
R: Read + Seek,
|
||||
{
|
||||
let mut audio_tracks = Vec::new();
|
||||
|
||||
while let Some(child) = children_reader.next()? {
|
||||
match child {
|
||||
ElementReaderYield::Master((ElementIdent::TrackEntry, _size)) => {
|
||||
read_track_entry(children_reader, parse_options, &mut audio_tracks)?;
|
||||
read_track_entry(children_reader, parse_options, &mut properties.audio_tracks)?;
|
||||
},
|
||||
ElementReaderYield::Eof => break,
|
||||
_ => {
|
||||
|
@ -32,30 +31,30 @@ where
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct AudioTrack {
|
||||
default: bool,
|
||||
enabled: bool,
|
||||
codec_id: String,
|
||||
codec_name: String,
|
||||
}
|
||||
|
||||
const AUDIO_TRACK_TYPE: u64 = 2;
|
||||
|
||||
fn read_track_entry<R>(
|
||||
children_reader: &mut ElementChildIterator<'_, R>,
|
||||
_parse_options: ParseOptions,
|
||||
audio_tracks: &mut Vec<AudioTrack>,
|
||||
parse_options: ParseOptions,
|
||||
audio_tracks: &mut Vec<AudioTrackDescriptor>,
|
||||
) -> Result<()>
|
||||
where
|
||||
R: Read + Seek,
|
||||
{
|
||||
let mut track = AudioTrack::default();
|
||||
let mut track = AudioTrackDescriptor::default();
|
||||
|
||||
while let Some(child) = children_reader.next()? {
|
||||
match child {
|
||||
ElementReaderYield::Child((ChildElementDescriptor { ident, .. }, size)) => {
|
||||
match ident {
|
||||
ElementIdent::TrackNumber => {
|
||||
let track_number = children_reader.read_unsigned_int(size.value())?;
|
||||
track.number = track_number;
|
||||
},
|
||||
ElementIdent::TrackUid => {
|
||||
let track_uid = children_reader.read_unsigned_int(size.value())?;
|
||||
track.uid = track_uid;
|
||||
},
|
||||
ElementIdent::TrackType => {
|
||||
let track_type = children_reader.read_unsigned_int(size.value())?;
|
||||
log::trace!("Encountered new track of type: {}", track_type);
|
||||
|
@ -80,18 +79,27 @@ where
|
|||
let _timecode_scale = children_reader.read_float(size.value())?;
|
||||
},
|
||||
ElementIdent::Language => {
|
||||
let _language = children_reader.read_string(size.value())?;
|
||||
let language = children_reader.read_string(size.value())?;
|
||||
track.language = Language::Iso639_2(language);
|
||||
},
|
||||
ElementIdent::LanguageBCP47 => {
|
||||
let language = children_reader.read_string(size.value())?;
|
||||
track.language = Language::Bcp47(language);
|
||||
},
|
||||
ElementIdent::CodecID => {
|
||||
let codec_id = children_reader.read_string(size.value())?;
|
||||
track.codec_id = codec_id;
|
||||
},
|
||||
ElementIdent::CodecPrivate => {
|
||||
let codec_private = children_reader.read_binary(size.value())?;
|
||||
track.codec_private = Some(codec_private);
|
||||
},
|
||||
ElementIdent::CodecDelay => {
|
||||
let _codec_delay = children_reader.read_unsigned_int(size.value())?;
|
||||
},
|
||||
ElementIdent::CodecName => {
|
||||
let codec_name = children_reader.read_utf8(size.value())?;
|
||||
track.codec_name = codec_name;
|
||||
track.codec_name = Some(codec_name);
|
||||
},
|
||||
ElementIdent::SeekPreRoll => {
|
||||
let _seek_pre_roll = children_reader.read_unsigned_int(size.value())?;
|
||||
|
@ -101,7 +109,7 @@ where
|
|||
},
|
||||
ElementReaderYield::Master((id, size)) => match id {
|
||||
ElementIdent::Audio => {
|
||||
children_reader.skip(size.value())?;
|
||||
read_audio_settings(&mut children_reader.children(), parse_options, &mut track)?
|
||||
},
|
||||
_ => {
|
||||
unreachable!("Unhandled master element in TrackEntry: {:?}", id);
|
||||
|
@ -114,12 +122,59 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
if !track.enabled {
|
||||
log::debug!("Skipping disabled track");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
audio_tracks.push(track);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_audio_settings<R>(
|
||||
children_reader: &mut ElementChildIterator<'_, R>,
|
||||
_parse_options: ParseOptions,
|
||||
audio_track: &mut AudioTrackDescriptor,
|
||||
) -> Result<()>
|
||||
where
|
||||
R: Read + Seek,
|
||||
{
|
||||
while let Some(child) = children_reader.next()? {
|
||||
match child {
|
||||
ElementReaderYield::Child((ChildElementDescriptor { ident, .. }, size)) => {
|
||||
match ident {
|
||||
ElementIdent::SamplingFrequency => {
|
||||
let sampling_frequency = children_reader.read_float(size.value())?;
|
||||
audio_track.settings.sampling_frequency = sampling_frequency;
|
||||
},
|
||||
ElementIdent::OutputSamplingFrequency => {
|
||||
let output_sampling_frequency = children_reader.read_float(size.value())?;
|
||||
audio_track.settings.output_sampling_frequency = output_sampling_frequency;
|
||||
},
|
||||
ElementIdent::Channels => {
|
||||
let channels = children_reader.read_unsigned_int(size.value())? as u8;
|
||||
audio_track.settings.channels = channels;
|
||||
},
|
||||
ElementIdent::BitDepth => {
|
||||
let bit_depth = children_reader.read_unsigned_int(size.value())? as u8;
|
||||
audio_track.settings.bit_depth = Some(bit_depth);
|
||||
},
|
||||
ElementIdent::Emphasis => {
|
||||
let emphasis = children_reader.read_unsigned_int(size.value())?;
|
||||
if emphasis == 0 {
|
||||
continue; // No emphasis
|
||||
}
|
||||
|
||||
audio_track.settings.emphasis =
|
||||
EbmlAudioTrackEmphasis::from_u8(emphasis as u8);
|
||||
},
|
||||
_ => {
|
||||
unreachable!("Unhandled child element in Audio: {child:?}");
|
||||
},
|
||||
}
|
||||
},
|
||||
ElementReaderYield::Eof => break,
|
||||
_ => {
|
||||
unreachable!("Unhandled child element in Audio: {child:?}");
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ use crate::ape::{ApeFile, ApeProperties};
|
|||
use crate::config::ParseOptions;
|
||||
use crate::ebml::{
|
||||
AudioTrackDescriptor, AudioTrackSettings, EbmlFile, EbmlHeaderProperties, EbmlProperties,
|
||||
SegmentInfo,
|
||||
Language, SegmentInfo,
|
||||
};
|
||||
use crate::file::AudioFile;
|
||||
use crate::flac::{FlacFile, FlacProperties};
|
||||
|
@ -90,17 +90,20 @@ fn MKA_PROPERTIES() -> EbmlProperties {
|
|||
writing_app: String::from("Lavf60.3.100"),
|
||||
},
|
||||
audio_tracks: vec![AudioTrackDescriptor {
|
||||
number: 0,
|
||||
uid: 0,
|
||||
language: String::new(),
|
||||
number: 1,
|
||||
uid: 18181673715630629642,
|
||||
enabled: true,
|
||||
default: false,
|
||||
language: Language::Iso639_2(String::from("und")),
|
||||
default_duration: 0,
|
||||
codec_id: String::new(),
|
||||
codec_private: vec![],
|
||||
codec_id: String::from("A_VORBIS"),
|
||||
codec_private: None,
|
||||
codec_name: None,
|
||||
settings: AudioTrackSettings {
|
||||
sampling_frequency: 0,
|
||||
output_sampling_frequency: 0,
|
||||
channels: 0,
|
||||
bit_depth: None,
|
||||
sampling_frequency: 48000.0,
|
||||
output_sampling_frequency: 0.0,
|
||||
channels: 2,
|
||||
bit_depth: Some(32),
|
||||
emphasis: None,
|
||||
},
|
||||
}],
|
||||
|
|
Loading…
Add table
Reference in a new issue