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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//! AIFF/AIFC audio file format reader.
//!
//! Parses IFF chunks: COMM (audio info), NAME, AUTH, comments.
//! Mirrors ExifTool's AIFF.pm.
use crate::error::{Error, Result};
use crate::tag::{Tag, TagGroup, TagId};
use crate::value::Value;
pub fn read_aiff(data: &[u8]) -> Result<Vec<Tag>> {
if data.len() < 12
|| !data.starts_with(b"FORM")
|| (&data[8..12] != b"AIFF" && &data[8..12] != b"AIFC")
{
return Err(Error::InvalidData("not an AIFF file".into()));
}
let mut tags = Vec::new();
let is_compressed = &data[8..12] == b"AIFC";
let mut pos = 12;
while pos + 8 <= data.len() {
let chunk_id = &data[pos..pos + 4];
let chunk_size =
u32::from_be_bytes([data[pos + 4], data[pos + 5], data[pos + 6], data[pos + 7]])
as usize;
pos += 8;
if pos + chunk_size > data.len() {
break;
}
let cd = &data[pos..pos + chunk_size];
match chunk_id {
// Common chunk
b"COMM" => {
if cd.len() >= 18 {
let channels = i16::from_be_bytes([cd[0], cd[1]]);
let num_frames = u32::from_be_bytes([cd[2], cd[3], cd[4], cd[5]]);
let bits_per_sample = i16::from_be_bytes([cd[6], cd[7]]);
let sample_rate = decode_ieee_extended(&cd[8..18]);
tags.push(mk(
"NumChannels",
"Number of Channels",
Value::I16(channels),
));
tags.push(mk(
"NumSampleFrames",
"Number of Sample Frames",
Value::U32(num_frames),
));
tags.push(mk("SampleSize", "Sample Size", Value::I16(bits_per_sample)));
tags.push(mk(
"SampleRate",
"Sample Rate",
Value::U32(sample_rate as u32),
));
if sample_rate > 0.0 && num_frames > 0 {
let duration = num_frames as f64 / sample_rate;
// Perl ConvertDuration: < 30s → "{:.2} s", else "h:mm:ss"
let dur_str = if duration < 30.0 {
format!("{:.2} s", duration)
} else {
let dur_u = (duration + 0.5) as u64;
let h = dur_u / 3600;
let m = (dur_u % 3600) / 60;
let s = dur_u % 60;
format!("{}:{:02}:{:02}", h, m, s)
};
// `%Image::ExifTool::AIFF::Composite` (AIFF.pm line
// 136) declares no GROUPS, so this takes the Composite
// table's own — Composite/Composite (ExifTool.pm line
// 2303) with family 2 defaulting to Other.
let mut tag = mk("Duration", "Duration", Value::String(dur_str));
tag.group.family0 = "Composite".into();
tag.group.family1 = "Composite".into();
tag.group.family2 = "Other".into();
tags.push(tag);
}
// AIFC compression type
if is_compressed && cd.len() >= 22 {
let comp_type = crate::encoding::decode_utf8_or_latin1(&cd[18..22])
.trim()
.to_string();
let comp_name = match comp_type.as_str() {
"NONE" | "none" => "None",
"sowt" => "Little-endian PCM",
"fl32" | "FL32" => "32-bit Float",
"fl64" | "FL64" => "64-bit Float",
"alaw" | "ALAW" => "A-Law",
"ulaw" | "ULAW" => "mu-Law",
"ima4" | "IMA4" => "IMA ADPCM",
_ => &comp_type,
};
tags.push(mk(
"Compression",
"Compression",
Value::String(comp_name.to_string()),
));
}
}
}
b"NAME" => {
let name = crate::encoding::decode_utf8_or_latin1(cd)
.trim_end_matches('\0')
.to_string();
if !name.is_empty() {
tags.push(mk("Name", "Name", Value::String(name)));
}
}
b"AUTH" => {
let author = crate::encoding::decode_utf8_or_latin1(cd)
.trim_end_matches('\0')
.to_string();
if !author.is_empty() {
tags.push(mk("Author", "Author", Value::String(author)));
}
}
b"(c) " => {
let copyright = crate::encoding::decode_utf8_or_latin1(cd)
.trim_end_matches('\0')
.to_string();
if !copyright.is_empty() {
tags.push(mk("Copyright", "Copyright", Value::String(copyright)));
}
}
b"ANNO" => {
let annotation = crate::encoding::decode_utf8_or_latin1(cd)
.trim_end_matches('\0')
.to_string();
if !annotation.is_empty() {
tags.push(mk("Annotation", "Annotation", Value::String(annotation)));
}
}
// COMT: Comment chunk with timestamp
b"COMT" => {
if cd.len() >= 2 {
let num_comments = u16::from_be_bytes([cd[0], cd[1]]) as usize;
let mut p = 2;
for _ in 0..num_comments {
if p + 8 > cd.len() {
break;
}
let ts = u32::from_be_bytes([cd[p], cd[p + 1], cd[p + 2], cd[p + 3]]);
// marker ID at p+4..p+6 (skipped)
let size = u16::from_be_bytes([cd[p + 6], cd[p + 7]]) as usize;
p += 8;
// CommentTime: Mac epoch (seconds since 1904-01-01)
// ValueConv: ConvertUnixTime($val - ((66 * 365 + 17) * 24 * 3600))
let mac_offset: u64 = (66 * 365 + 17) * 24 * 3600;
if ts as u64 >= mac_offset {
let unix_ts = ts as u64 - mac_offset;
let dt = aiff_unix_to_datetime(unix_ts as i64);
tags.push(mk("CommentTime", "Comment Time", Value::String(dt)));
}
if p + size <= cd.len() && size > 0 {
let comment = crate::encoding::decode_utf8_or_latin1(&cd[p..p + size])
.trim_end_matches('\0')
.to_string();
if !comment.is_empty() {
tags.push(mk("Comment", "Comment", Value::String(comment)));
}
}
let size_padded = size + (size & 1);
p += size_padded;
}
}
}
// ID3 tags embedded in AIFF
b"ID3 " if cd.starts_with(b"ID3") => {
if let Ok(id3_tags) = crate::formats::id3::read_mp3(cd) {
tags.extend(id3_tags);
}
}
_ => {}
}
pos += chunk_size;
// Pad to even boundary
if chunk_size % 2 != 0 {
pos += 1;
}
}
Ok(tags)
}
/// Decode 80-bit IEEE 754 extended precision float (10 bytes, big-endian).
fn decode_ieee_extended(data: &[u8]) -> f64 {
if data.len() < 10 {
return 0.0;
}
let exponent = (((data[0] as u16) & 0x7F) << 8) | data[1] as u16;
let sign = if data[0] & 0x80 != 0 { -1.0 } else { 1.0 };
let mantissa = ((data[2] as u64) << 56)
| ((data[3] as u64) << 48)
| ((data[4] as u64) << 40)
| ((data[5] as u64) << 32)
| ((data[6] as u64) << 24)
| ((data[7] as u64) << 16)
| ((data[8] as u64) << 8)
| data[9] as u64;
if exponent == 0 && mantissa == 0 {
return 0.0;
}
let f = mantissa as f64 / (1u64 << 63) as f64;
sign * f * 2.0_f64.powi(exponent as i32 - 16383)
}
/// Convert Unix timestamp to "YYYY:MM:DD HH:MM:SS" (UTC, without timezone suffix).
/// Mirrors Perl's ConvertUnixTime($val) for AIFF CommentTime.
pub(crate) fn aiff_unix_to_datetime(secs: i64) -> String {
let days = secs / 86400;
let time = secs % 86400;
let h = time / 3600;
let m = (time % 3600) / 60;
let s = time % 60;
let mut y = 1970i32;
let mut rem = days;
loop {
let dy: i64 = if (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 {
366
} else {
365
};
if rem < dy {
break;
}
rem -= dy;
y += 1;
}
let leap = (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
let months: [i64; 12] = [
31,
if leap { 29 } else { 28 },
31,
30,
31,
30,
31,
31,
30,
31,
30,
31,
];
let mut mo = 1i32;
for &dm in &months {
if rem < dm {
break;
}
rem -= dm;
mo += 1;
}
format!(
"{:04}:{:02}:{:02} {:02}:{:02}:{:02}",
y,
mo,
rem + 1,
h,
m,
s
)
}
fn mk(name: &str, description: &str, value: Value) -> Tag {
let print_value = value.to_display_string();
Tag {
id: TagId::Text(name.to_string()),
name: name.to_string(),
description: description.to_string(),
group: TagGroup {
family0: "AIFF".into(),
family1: "AIFF".into(),
family2: "Audio".into(),
family3: "Main".into(),
},
raw_value: value,
print_value,
priority: 0,
}
}