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
use crate::aac::header::ADTSHeader;
use crate::mp4::AudioObjectType;
use crate::mpeg::header::MpegVersion;
use crate::properties::FileProperties;

use std::time::Duration;

/// An AAC file's audio properties
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct AACProperties {
	pub(crate) version: MpegVersion,
	pub(crate) audio_object_type: AudioObjectType,
	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) copyright: bool,
	pub(crate) original: bool,
}

impl AACProperties {
	/// MPEG version
	///
	/// The only possible variants are:
	///
	/// * [MpegVersion::V2]
	/// * [MpegVersion::V4]
	pub fn version(&self) -> MpegVersion {
		self.version
	}

	/// Audio object type
	///
	/// The only possible variants are:
	///
	/// * [AudioObjectType::AacMain]
	/// * [AudioObjectType::AacLowComplexity]
	/// * [AudioObjectType::AacScalableSampleRate]
	/// * [AudioObjectType::AacLongTermPrediction]
	pub fn audio_object_type(&self) -> AudioObjectType {
		self.audio_object_type
	}

	/// 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
	}

	/// Whether the audio is copyrighted
	pub fn copyright(&self) -> bool {
		self.copyright
	}

	/// Whether the media is original or a copy
	pub fn original(&self) -> bool {
		self.original
	}
}

impl From<AACProperties> for FileProperties {
	fn from(input: AACProperties) -> Self {
		FileProperties {
			duration: input.duration,
			overall_bitrate: Some(input.overall_bitrate),
			audio_bitrate: Some(input.audio_bitrate),
			sample_rate: Some(input.sample_rate),
			bit_depth: None,
			channels: Some(input.channels),
			channel_mask: None,
		}
	}
}

pub(super) fn read_properties(
	properties: &mut AACProperties,
	first_frame: ADTSHeader,
	stream_len: u64,
) {
	properties.version = first_frame.version;
	properties.audio_object_type = first_frame.audio_object_ty;
	properties.sample_rate = first_frame.sample_rate;
	properties.channels = first_frame.channels;
	properties.copyright = first_frame.copyright;
	properties.original = first_frame.original;

	let bitrate = first_frame.bitrate;

	if bitrate > 0 {
		properties.audio_bitrate = bitrate;
		properties.overall_bitrate = bitrate;
		properties.duration = Duration::from_millis((stream_len * 8) / u64::from(bitrate));
	}
}