Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle deprecated Matroska Elements #133

Merged
merged 4 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ circular = "0.3"
log = "0.4"
crc = "3.0.1"
uuid = "1.3.0"
phf = { version = "0.11.1", features = ["macros"] }

[dev-dependencies]
quickcheck = "1"
Expand Down
38 changes: 1 addition & 37 deletions src/demuxer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,9 @@ fn track_entry_audio_kind(audio: &Audio) -> Option<MediaKind> {
let rate = audio
.output_sampling_frequency
.unwrap_or(audio.sampling_frequency);
// TODO: complete it
let map = if audio.channel_positions.is_none() {
Some(ChannelMap::default_map(audio.channels as usize))
} else {
unimplemented!("Convert matroska map to rust-av map")
};
let a = AudioInfo {
rate: rate as usize,
map,
map: Some(ChannelMap::default_map(audio.channels as usize)),
format: None,
};
Some(MediaKind::Audio(a))
Expand Down Expand Up @@ -406,12 +400,9 @@ mod tests {
flag_default: 1,
flag_forced: 0,
flag_lacing: 0,
min_cache: None,
max_cache: None,
default_duration: None,
default_decoded_field_duration: None,
track_timestamp_scale: 1.0,
track_offset: None,
max_block_addition_id: 0,
name: None,
language: String::from("eng"),
Expand All @@ -420,18 +411,8 @@ mod tests {
codec_private: None,
codec_name: None,
attachment_link: None,
codec_settings: None,
codec_info_url: None,
codec_download_url: None,
codec_decode_all: None,
track_overlay: None,
codec_delay: 0,
seek_pre_roll: 0,
trick_track_uid: None,
trick_track_segment_uid: None,
trick_track_flag: None,
trick_master_track_uid: None,
trick_master_track_segment_uid: None,
video: Some(Video {
flag_interlaced: 0,
field_order: 2,
Expand All @@ -447,10 +428,7 @@ mod tests {
display_width: Some(640),
display_height: Some(360),
display_unit: 3,
aspect_ratio_type: None,
colour_space: None,
gamma_value: None,
frame_rate: None,
colour: None,
projection: None
}),
Expand All @@ -468,12 +446,9 @@ mod tests {
flag_default: 1,
flag_forced: 0,
flag_lacing: 0,
min_cache: None,
max_cache: None,
default_duration: None,
default_decoded_field_duration: None,
track_timestamp_scale: 1.0,
track_offset: None,
max_block_addition_id: 0,
name: None,
language: String::from("eng"),
Expand All @@ -485,24 +460,13 @@ mod tests {
]),
codec_name: None,
attachment_link: None,
codec_settings: None,
codec_info_url: None,
codec_download_url: None,
codec_decode_all: None,
track_overlay: None,
codec_delay: 6500000,
seek_pre_roll: 0,
trick_track_uid: None,
trick_track_segment_uid: None,
trick_track_flag: None,
trick_master_track_uid: None,
trick_master_track_segment_uid: None,
video: None,
audio: Some(Audio {
sampling_frequency: 48000.0,
output_sampling_frequency: None,
channels: 1,
channel_positions: None,
bit_depth: Some(32),
}),
track_translate: vec![],
Expand Down
47 changes: 47 additions & 0 deletions src/ebml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,50 @@ self::macros::impl_ebml_master! {
pub fn ebml_header(input: &[u8]) -> EbmlResult<EbmlHeader> {
ebml_element(0x1A45DFA3)(input)
}

/// Map of known deprecated Element IDs and their EBML Paths
pub static DEPRECATED: phf::Map<u32, &'static str> = phf::phf_map! {
0x8E_u32 => r"\Segment\Cluster\BlockGroup\Slices",
0x97_u32 => r"\Segment\Cues\CuePoint\CueTrackPositions\CueReference\CueRefCluster",
0xA2_u32 => r"\Segment\Cluster\BlockGroup\BlockVirtual",
0xAA_u32 => r"\Segment\Tracks\TrackEntry\CodecDecodeAll",
0xAF_u32 => r"\Segment\Cluster\EncryptedBlock",
0xC0_u32 => r"\Segment\Tracks\TrackEntry\TrickTrackUID",
0xC1_u32 => r"\Segment\Tracks\TrackEntry\TrickTrackSegmentUID",
0xC4_u32 => r"\Segment\Tracks\TrackEntry\TrickMasterTrackSegmentUID",
0xC6_u32 => r"\Segment\Tracks\TrackEntry\TrickTrackFlag",
0xC7_u32 => r"\Segment\Tracks\TrackEntry\TrickMasterTrackUID",
0xC8_u32 => r"\Segment\Cluster\BlockGroup\ReferenceFrame",
0xC9_u32 => r"\Segment\Cluster\BlockGroup\ReferenceFrame\ReferenceOffset",
0xCA_u32 => r"\Segment\Cluster\BlockGroup\ReferenceFrame\ReferenceTimestamp",
0xCB_u32 => r"\Segment\Cluster\BlockGroup\Slices\TimeSlice\BlockAdditionID",
0xCC_u32 => r"\Segment\Cluster\BlockGroup\Slices\TimeSlice\LaceNumber",
0xCD_u32 => r"\Segment\Cluster\BlockGroup\Slices\TimeSlice\FrameNumber",
0xCE_u32 => r"\Segment\Cluster\BlockGroup\Slices\TimeSlice\Delay",
0xCF_u32 => r"\Segment\Cluster\BlockGroup\Slices\TimeSlice\SliceDuration",
0xE8_u32 => r"\Segment\Cluster\BlockGroup\Slices\TimeSlice",
0xEB_u32 => r"\Segment\Cues\CuePoint\CueTrackPositions\CueReference\CueRefCodecState",
0xFD_u32 => r"\Segment\Cluster\BlockGroup\ReferenceVirtual",
0x44B4_u32 => r"\Segment\Tags\Tag\+SimpleTag\TagDefaultBogus",
0x4661_u32 => r"\Segment\Attachments\AttachedFile\FileUsedStartTime",
0x4662_u32 => r"\Segment\Attachments\AttachedFile\FileUsedEndTime",
0x4675_u32 => r"\Segment\Attachments\AttachedFile\FileReferral",
0x47E3_u32 => r"\Segment\Tracks\TrackEntry\ContentEncodings\ContentEncoding\ContentEncryption\ContentSignature",
0x47E4_u32 => r"\Segment\Tracks\TrackEntry\ContentEncodings\ContentEncoding\ContentEncryption\ContentSigKeyID",
0x47E5_u32 => r"\Segment\Tracks\TrackEntry\ContentEncodings\ContentEncoding\ContentEncryption\ContentSigAlgo",
0x47E6_u32 => r"\Segment\Tracks\TrackEntry\ContentEncodings\ContentEncoding\ContentEncryption\ContentSigHashAlgo",
0x535F_u32 => r"\Segment\Cues\CuePoint\CueTrackPositions\CueReference\CueRefNumber",
0x537F_u32 => r"\Segment\Tracks\TrackEntry\TrackOffset",
0x54B3_u32 => r"\Segment\Tracks\TrackEntry\Video\AspectRatioType",
0x5854_u32 => r"\Segment\Cluster\SilentTracks",
0x58D7_u32 => r"\Segment\Cluster\SilentTracks\SilentTrackNumber",
0x6DE7_u32 => r"\Segment\Tracks\TrackEntry\MinCache",
0x6DF8_u32 => r"\Segment\Tracks\TrackEntry\MaxCache",
0x6FAB_u32 => r"\Segment\Tracks\TrackEntry\TrackOverlay",
0x7D7B_u32 => r"\Segment\Tracks\TrackEntry\Audio\ChannelPositions",
0x2383E3_u32 => r"\Segment\Tracks\TrackEntry\Video\FrameRate",
0x26B240_u32 => r"\Segment\Tracks\TrackEntry\CodecDownloadURL",
0x2FB523_u32 => r"\Segment\Tracks\TrackEntry\Video\GammaValue",
0x3A9697_u32 => r"\Segment\Tracks\TrackEntry\CodecSettings",
0x3B4040_u32 => r"\Segment\Tracks\TrackEntry\CodecInfoURL",
};
6 changes: 3 additions & 3 deletions src/ebml/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ pub fn void(input: &[u8]) -> EbmlResult<&[u8]> {
ebml_element(0xEC)(input)
}

/// Consumes an entire Master Element, and returns the ID if successful.
pub fn skip_master(input: &[u8]) -> EbmlResult<u32> {
/// Consumes an entire EBML Element, and returns the ID if successful.
pub fn skip_element(input: &[u8]) -> EbmlResult<u32> {
let (i, (id, size, crc)) = tuple((vid, elem_size, crc))(input)?;
let size = if crc.is_some() { size - 6 } else { size };
checksum(crc, take(size))(i)?;
let (i, _) = checksum(crc, take(size))(i)?;
Ok((i, id))
}

Expand Down
35 changes: 0 additions & 35 deletions src/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,10 @@ impl_ebml_master! {
#[derive(Debug, Clone, PartialEq)]
struct Cluster<'a> {
[0xE7] timestamp: (u64),
[0x5854] silent_tracks: (Option<SilentTracks>),
[0xA7] position: (Option<u64>),
[0xAB] prev_size: (Option<u64>),
[0xA3] simple_block: (Vec<&'a [u8]>) [0..],
[0xA0] block_group: (Vec<BlockGroup<'a>>) [0..],
[0xAF] encrypted_block: (Option<&'a [u8]>),
}
}

impl_ebml_master! {
// Element ID 0x5854
#[derive(Debug, Clone, PartialEq, Eq)]
struct SilentTracks {
[0x58D7] numbers: (Vec<u64>) [0..],
}
}

Expand All @@ -142,16 +132,12 @@ impl_ebml_master! {
#[derive(Debug, Clone, PartialEq, Eq)]
struct BlockGroup<'a> {
[0xA1] block: (&'a [u8]),
[0xA2] block_virtual: (Option<Vec<u8>>),
// [0x75A1] block_additions: (Option<BlockAdditions>),
[0x9B] block_duration: (Option<u64>),
[0xFA] reference_priority: (u64) = 0,
[0xFB] reference_block: (Option<u64>),
[0xFD] reference_virtual: (Option<i64>),
[0xA4] codec_state: (Option<Vec<u8>>),
[0x75A2] discard_padding: (Option<i64>),
// [0x8E] slices: (Option<Slices>),
// [0xC8] reference_frame: (Option<ReferenceFrame>),
}
}

Expand Down Expand Up @@ -300,13 +286,10 @@ impl_ebml_master! {
[0x88] flag_default: (u64) = 1,
[0x55AA] flag_forced: (u64) = 0,
[0x9C] flag_lacing: (u64) = 1,
[0x6DE7] min_cache: (Option<u64>),
[0x6DF8] max_cache: (Option<u64>),
[0x23E383] default_duration: (Option<u64>),
[0x234E7A] default_decoded_field_duration: (Option<u64>),
// FIXME: reimplement float_or handling
[0x23314F] track_timestamp_scale: (f64) = 1.0,
[0x537F] track_offset: (Option<i64>),
[0x55EE] max_block_addition_id: (u64) = 0,
[0x536E] name: (Option<String>),
[0x22B59C] language: (String) = String::from("eng"),
Expand All @@ -315,18 +298,8 @@ impl_ebml_master! {
[0x63A2] codec_private: (Option<Vec<u8>>),
[0x258688] codec_name: (Option<String>),
[0x7446] attachment_link: (Option<u64>),
[0x3A9697] codec_settings: (Option<String>),
[0x3B4040] codec_info_url: (Option<String>),
[0x26B240] codec_download_url: (Option<String>),
[0xAA] codec_decode_all: (Option<u64>),
[0x6FAB] track_overlay: (Option<u64>),
[0x56AA] codec_delay: (u64) = 0,
[0x56BB] seek_pre_roll: (u64) = 0,
[0xC0] trick_track_uid: (Option<u64>),
[0xC1] trick_track_segment_uid: (Option<Uuid>),
[0xC6] trick_track_flag: (Option<u64>),
[0xC7] trick_master_track_uid: (Option<u64>),
[0xC4] trick_master_track_segment_uid: (Option<Uuid>),
[0xE0] video: (Option<Video>),
[0xE1] audio: (Option<Audio>),
[0x6624] track_translate: (Vec<TrackTranslate>) [0..],
Expand Down Expand Up @@ -419,10 +392,6 @@ impl_ebml_master! {
struct ContentEncryption {
[0x47E1] enc_algo: (u64) = 0,
[0x47E2] enc_key_id: (Option<Vec<u8>>),
[0x47E3] signature: (Option<Vec<u8>>),
[0x47E4] sig_key_id: (Option<Vec<u8>>),
[0x47E5] sig_algo: (Option<u64>),
[0x47E6] sig_hash_algo: (Option<u64>),
}
}

Expand All @@ -434,7 +403,6 @@ impl_ebml_master! {
[0xB5] sampling_frequency: (f64) = 5360.0,
[0x7885] output_sampling_frequency: (Option<f64>),
[0x9F] channels: (u64),
[0x7D7B] channel_positions: (Option<Vec<u8>>),
[0x6264] bit_depth: (Option<u64>),
}
}
Expand All @@ -457,10 +425,7 @@ impl_ebml_master! {
[0x54B0] display_width: (Option<u64>),
[0x54BA] display_height: (Option<u64>),
[0x54B2] display_unit: (u64) = 0,
[0x54B3] aspect_ratio_type: (Option<u64>),
[0x2EB524] colour_space: (Option<Vec<u8>>),
[0x2FB523] gamma_value: (Option<f64>),
[0x2383E3] frame_rate: (Option<f64>),
[0x55B0] colour: (Option<Colour>),
[0x55D0] projection: (Option<Projection>),
}
Expand Down
4 changes: 0 additions & 4 deletions src/muxer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,10 @@ impl Muxer for MkvMuxer {

let cluster = Cluster {
timestamp: self.timestamp.take().unwrap(),
silent_tracks: None,
position: None,
prev_size: None,
simple_block: simple_blocks,
block_group: Vec::new(),
encrypted_block: None,
};

let mut buf: Vec<u8> = vec![0; cluster.size(0x1F43B675)];
Expand Down Expand Up @@ -398,12 +396,10 @@ impl Muxer for MkvMuxer {

let cluster = Cluster {
timestamp: self.timestamp.take().unwrap(),
silent_tracks: None,
position: None,
prev_size: None,
simple_block: simple_blocks,
block_group: Vec::new(),
encrypted_block: None,
};

let mut buf: Vec<u8> = vec![0; cluster.size(0x1F43B675)];
Expand Down
11 changes: 8 additions & 3 deletions src/permutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,14 @@ macro_rules! permutation_trait_impl(
// Have all parsers (including void) failed?
if l == input.len() {
// Skip unknown Element if possible.
if let Error { id: _, kind: crate::ebml::ErrorKind::MissingElement } = err {
if let Ok((i, id)) = crate::ebml::skip_master(input) {
log::warn!("Skipped unknown Element 0x{id:X}");
if let Error { id: _, kind: $crate::ebml::ErrorKind::MissingElement } = err {
if let Ok((i, id)) = $crate::ebml::skip_element(input) {
Luni-4 marked this conversation as resolved.
Show resolved Hide resolved
if let Some(name) = $crate::ebml::DEPRECATED.get(&id) {
log::warn!("Skipped deprecated Element '{name}' ({id:#0X})");
} else {
log::warn!("Skipped unknown Element {id:#0X}");
}

input = i;
continue;
}
Expand Down
7 changes: 1 addition & 6 deletions src/serializer/cookie_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cookie_factory::bytes::{be_f64, be_i16, be_i8};
use cookie_factory::bytes::{be_f64, be_i16};
use cookie_factory::combinator::{skip, slice};
use cookie_factory::gen::legacy_wrap;
use cookie_factory::GenError;
Expand Down Expand Up @@ -89,11 +89,6 @@ pub(crate) fn gen_slice<'a, 'b>(
move |input| legacy_wrap(slice(v), input)
}

#[inline]
pub(crate) fn set_be_i8(x: (&mut [u8], usize), v: i8) -> Result<(&mut [u8], usize), GenError> {
legacy_wrap(be_i8(v), x)
}

#[inline]
pub(crate) fn set_be_i16(x: (&mut [u8], usize), v: i16) -> Result<(&mut [u8], usize), GenError> {
legacy_wrap(be_i16(v), x)
Expand Down
Loading