-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathproperties.rs
211 lines (182 loc) · 5.33 KB
/
properties.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use super::header::{ChannelMode, Emphasis, Header, Layer, MpegVersion, VbrHeader};
use crate::config::ParsingMode;
use crate::error::Result;
use crate::mpeg::header::rev_search_for_frame_header;
use crate::properties::{ChannelMask, FileProperties};
use crate::util::math::RoundedDivision;
use std::io::{Read, Seek, SeekFrom};
use std::time::Duration;
/// An MPEG file's audio properties
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct MpegProperties {
pub(crate) version: MpegVersion,
pub(crate) layer: Layer,
pub(crate) duration: Duration,
pub(crate) overall_bitrate: u32,
pub(crate) audio_bitrate: u32,
pub(crate) sample_rate: u32,
pub(crate) channels: u8,
pub(crate) channel_mode: ChannelMode,
pub(crate) mode_extension: Option<u8>,
pub(crate) copyright: bool,
pub(crate) original: bool,
pub(crate) emphasis: Option<Emphasis>,
}
impl From<MpegProperties> for FileProperties {
fn from(input: MpegProperties) -> Self {
let MpegProperties {
duration,
overall_bitrate,
audio_bitrate,
sample_rate,
channels,
channel_mode,
version: _,
layer: _,
copyright: _,
emphasis: _,
mode_extension: _,
original: _,
} = input;
let channel_mask = match channel_mode {
ChannelMode::SingleChannel => Some(ChannelMask::mono()),
ChannelMode::Stereo | ChannelMode::JointStereo => Some(ChannelMask::stereo()),
ChannelMode::DualChannel => None, // Cannot be represented by ChannelMask
};
Self {
duration,
overall_bitrate: Some(overall_bitrate),
audio_bitrate: Some(audio_bitrate),
sample_rate: Some(sample_rate),
bit_depth: None,
channels: Some(channels),
channel_mask,
}
}
}
impl MpegProperties {
/// Duration of the audio
pub fn duration(&self) -> Duration {
self.duration
}
/// Overall bitrate (kbps)
pub fn overall_bitrate(&self) -> u32 {
self.overall_bitrate
}
/// Audio bitrate (kbps)
pub fn audio_bitrate(&self) -> u32 {
self.audio_bitrate
}
/// Sample rate (Hz)
pub fn sample_rate(&self) -> u32 {
self.sample_rate
}
/// Channel count
pub fn channels(&self) -> u8 {
self.channels
}
/// MPEG version
pub fn version(&self) -> &MpegVersion {
&self.version
}
/// MPEG layer
pub fn layer(&self) -> &Layer {
&self.layer
}
/// MPEG channel mode
pub fn channel_mode(&self) -> &ChannelMode {
&self.channel_mode
}
/// A channel mode extension specifically for [`ChannelMode::JointStereo`]
pub fn mode_extension(&self) -> Option<u8> {
self.mode_extension
}
/// Whether the audio is copyrighted
pub fn is_copyright(&self) -> bool {
self.copyright
}
/// Whether the media is original or a copy
pub fn is_original(&self) -> bool {
self.original
}
/// See [`Emphasis`]
pub fn emphasis(&self) -> Option<Emphasis> {
self.emphasis
}
}
pub(super) fn read_properties<R>(
properties: &mut MpegProperties,
reader: &mut R,
first_frame: (Header, u64),
mut last_frame_offset: u64,
xing_header: Option<VbrHeader>,
file_length: u64,
parse_mode: ParsingMode,
) -> Result<()>
where
R: Read + Seek,
{
let first_frame_header = first_frame.0;
let first_frame_offset = first_frame.1;
properties.version = first_frame_header.version;
properties.layer = first_frame_header.layer;
properties.channel_mode = first_frame_header.channel_mode;
properties.mode_extension = first_frame_header.mode_extension;
properties.copyright = first_frame_header.copyright;
properties.original = first_frame_header.original;
properties.emphasis = first_frame_header.emphasis;
properties.sample_rate = first_frame_header.sample_rate;
properties.channels = if first_frame_header.channel_mode == ChannelMode::SingleChannel {
1
} else {
2
};
if let Some(xing_header) = xing_header {
if first_frame_header.sample_rate > 0 && xing_header.is_valid() {
let frame_time = (u32::from(first_frame_header.samples) * 1000)
.div_round(first_frame_header.sample_rate);
let length = u64::from(frame_time) * u64::from(xing_header.frames);
properties.duration = Duration::from_millis(length);
properties.overall_bitrate = ((file_length * 8) / length) as u32;
properties.audio_bitrate = ((u64::from(xing_header.size) * 8) / length) as u32;
return Ok(());
}
}
// Nothing more we can do
if first_frame_header.bitrate == 0 {
return Ok(());
}
log::warn!("MPEG: Using bitrate to estimate duration");
properties.audio_bitrate = first_frame_header.bitrate;
// Search for the last frame, starting at the end of the frames
reader.seek(SeekFrom::Start(last_frame_offset))?;
let mut last_frame = None;
let mut pos = last_frame_offset;
while pos > 0 {
match rev_search_for_frame_header(reader, &mut pos, parse_mode) {
// Found a frame header
Ok(Some(header)) => {
// Move `last_frame_offset` back to the actual position
last_frame_offset = pos;
if header.cmp(&first_frame_header) {
last_frame = Some(header);
break;
}
},
// Encountered some IO error, just break
Err(_) => break,
// No frame sync found, continue further back in the file
_ => {},
}
}
if let Some(last_frame_header) = last_frame {
let stream_len = last_frame_offset - first_frame_offset + u64::from(last_frame_header.len);
let length = (stream_len * 8).div_round(u64::from(properties.audio_bitrate));
if length > 0 {
properties.overall_bitrate = ((file_length * 8) / length) as u32;
properties.duration = Duration::from_millis(length);
}
}
Ok(())
}