-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAiffParser.java
More file actions
341 lines (290 loc) · 12.9 KB
/
AiffParser.java
File metadata and controls
341 lines (290 loc) · 12.9 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package me.tamkungz.codecmedia.internal.audio.aiff;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import me.tamkungz.codecmedia.CodecMediaException;
import me.tamkungz.codecmedia.internal.audio.BitrateMode;
public final class AiffParser {
private static final String AIFC_COMPRESSION_NONE = "NONE";
private static final String AIFC_COMPRESSION_SOWT = "sowt";
private static final Map<String, String> TEXT_CHUNK_TO_METADATA = Map.of(
"NAME", "title",
"AUTH", "artist",
"(c) ", "copyright",
"ANNO", "comment"
);
private static final String[] METADATA_WRITE_ORDER = {"title", "artist", "copyright", "comment"};
private AiffParser() {
}
public static AiffProbeInfo parse(byte[] bytes) throws CodecMediaException {
if (!isLikelyAiff(bytes)) {
throw new CodecMediaException("Not an AIFF file");
}
boolean aifc = bytes[11] == 'C';
int offset = 12;
Integer channels = null;
Integer bitsPerSample = null;
Integer sampleRate = null;
Long frameCount = null;
while (offset + 8 <= bytes.length) {
String chunkId = readAscii(bytes, offset, 4);
int chunkSize = readBeInt(bytes, offset + 4);
if (chunkSize < 0) {
throw new CodecMediaException("Invalid AIFF chunk size: " + chunkSize);
}
int chunkDataStart = offset + 8;
if (chunkDataStart + chunkSize > bytes.length) {
throw new CodecMediaException("AIFF chunk exceeds file bounds: " + chunkId);
}
if ("COMM".equals(chunkId)) {
if (chunkSize < 18) {
throw new CodecMediaException("AIFF COMM chunk too small");
}
channels = readBeShort(bytes, chunkDataStart);
frameCount = readBeUInt32(bytes, chunkDataStart + 2);
bitsPerSample = readBeShort(bytes, chunkDataStart + 6);
sampleRate = decodeExtended80ToIntHz(bytes, chunkDataStart + 8);
if (aifc) {
if (chunkSize < 22) {
throw new CodecMediaException("AIFC COMM chunk missing compression type");
}
String compressionType = readAscii(bytes, chunkDataStart + 18, 4);
validateAifcCompressionType(compressionType);
}
}
int padded = (chunkSize % 2 == 0) ? chunkSize : chunkSize + 1;
offset = chunkDataStart + padded;
}
if (channels == null || bitsPerSample == null || sampleRate == null || frameCount == null) {
throw new CodecMediaException("AIFF missing required COMM chunk fields");
}
if (channels <= 0 || bitsPerSample <= 0 || sampleRate <= 0 || frameCount < 0) {
throw new CodecMediaException("Invalid AIFF format values");
}
long durationMillis = (frameCount * 1000L) / sampleRate;
long byteRate = (long) sampleRate * channels * bitsPerSample / 8L;
int bitrateKbps = (int) ((byteRate * 8L) / 1000L);
return new AiffProbeInfo(durationMillis, bitrateKbps, sampleRate, channels, BitrateMode.CBR);
}
private static void validateAifcCompressionType(String compressionType) throws CodecMediaException {
if (AIFC_COMPRESSION_NONE.equals(compressionType) || AIFC_COMPRESSION_SOWT.equals(compressionType)) {
return;
}
throw new CodecMediaException("Unsupported AIFC compression type: " + compressionType);
}
public static boolean isLikelyAiff(byte[] bytes) {
return bytes != null
&& bytes.length >= 12
&& bytes[0] == 'F'
&& bytes[1] == 'O'
&& bytes[2] == 'R'
&& bytes[3] == 'M'
&& bytes[8] == 'A'
&& bytes[9] == 'I'
&& bytes[10] == 'F'
&& (bytes[11] == 'F' || bytes[11] == 'C');
}
public static Map<String, String> readTextMetadata(byte[] bytes) throws CodecMediaException {
if (!isLikelyAiff(bytes)) {
throw new CodecMediaException("Not an AIFF file");
}
Map<String, String> out = new LinkedHashMap<>();
int offset = 12;
while (offset + 8 <= bytes.length) {
String chunkId = readAscii(bytes, offset, 4);
int chunkSize = readBeInt(bytes, offset + 4);
if (chunkSize < 0) {
throw new CodecMediaException("Invalid AIFF chunk size: " + chunkSize);
}
int chunkDataStart = offset + 8;
long chunkDataEndLong = (long) chunkDataStart + chunkSize;
if (chunkDataEndLong < chunkDataStart || chunkDataEndLong > bytes.length) {
throw new CodecMediaException("AIFF chunk exceeds file bounds: " + chunkId);
}
String key = TEXT_CHUNK_TO_METADATA.get(chunkId);
if (key != null) {
int chunkDataEnd = (int) chunkDataEndLong;
while (chunkDataEnd > chunkDataStart
&& (bytes[chunkDataEnd - 1] == 0 || bytes[chunkDataEnd - 1] == ' ' || bytes[chunkDataEnd - 1] == '\n' || bytes[chunkDataEnd - 1] == '\r')) {
chunkDataEnd--;
}
String value = new String(bytes, chunkDataStart, chunkDataEnd - chunkDataStart, StandardCharsets.UTF_8).trim();
if (!value.isEmpty()) {
out.put(key, value);
}
}
int padded = (chunkSize & 1) == 0 ? chunkSize : chunkSize + 1;
long nextOffsetLong = (long) chunkDataStart + padded;
if (nextOffsetLong < chunkDataStart || nextOffsetLong > bytes.length || nextOffsetLong > Integer.MAX_VALUE) {
throw new CodecMediaException("AIFF chunk exceeds file bounds: " + chunkId);
}
offset = (int) nextOffsetLong;
}
return out;
}
public static byte[] writeTextMetadata(byte[] bytes, Map<String, String> metadataEntries) throws CodecMediaException {
if (!isLikelyAiff(bytes)) {
throw new CodecMediaException("Not an AIFF file");
}
List<byte[]> keptChunks = new ArrayList<>();
int offset = 12;
while (offset + 8 <= bytes.length) {
String chunkId = readAscii(bytes, offset, 4);
int chunkSize = readBeInt(bytes, offset + 4);
if (chunkSize < 0) {
throw new CodecMediaException("Invalid AIFF chunk size: " + chunkSize);
}
int chunkDataStart = offset + 8;
long chunkDataEndLong = (long) chunkDataStart + chunkSize;
if (chunkDataEndLong < chunkDataStart || chunkDataEndLong > bytes.length) {
throw new CodecMediaException("AIFF chunk exceeds file bounds: " + chunkId);
}
int padded = (chunkSize & 1) == 0 ? chunkSize : chunkSize + 1;
long nextOffsetLong = (long) chunkDataStart + padded;
if (nextOffsetLong < chunkDataStart || nextOffsetLong > bytes.length || nextOffsetLong > Integer.MAX_VALUE) {
throw new CodecMediaException("AIFF chunk exceeds file bounds: " + chunkId);
}
if (!isManagedTextChunk(chunkId)) {
int len = (int) (nextOffsetLong - offset);
byte[] rawChunk = new byte[len];
System.arraycopy(bytes, offset, rawChunk, 0, len);
keptChunks.add(rawChunk);
}
offset = (int) nextOffsetLong;
}
List<byte[]> textChunks = buildTextChunks(metadataEntries);
long total = 12L;
for (byte[] chunk : keptChunks) {
total += chunk.length;
}
for (byte[] chunk : textChunks) {
total += chunk.length;
}
if (total > Integer.MAX_VALUE) {
throw new CodecMediaException("AIFF file is too large after metadata write");
}
byte[] out = new byte[(int) total];
out[0] = 'F';
out[1] = 'O';
out[2] = 'R';
out[3] = 'M';
writeBeInt(out, 4, (int) (total - 8));
out[8] = 'A';
out[9] = 'I';
out[10] = 'F';
out[11] = bytes[11];
int outOffset = 12;
for (byte[] chunk : keptChunks) {
System.arraycopy(chunk, 0, out, outOffset, chunk.length);
outOffset += chunk.length;
}
for (byte[] chunk : textChunks) {
System.arraycopy(chunk, 0, out, outOffset, chunk.length);
outOffset += chunk.length;
}
return out;
}
private static int decodeExtended80ToIntHz(byte[] bytes, int offset) throws CodecMediaException {
if (offset + 10 > bytes.length) {
throw new CodecMediaException("Unexpected end of AIFF data");
}
int exp = ((bytes[offset] & 0x7F) << 8) | (bytes[offset + 1] & 0xFF);
long mantissa = 0;
for (int i = 0; i < 8; i++) {
mantissa = (mantissa << 8) | (bytes[offset + 2 + i] & 0xFFL);
}
if (exp == 0 || mantissa == 0) {
return 0;
}
int shift = exp - 16383 - 63;
long value;
if (shift >= 0) {
value = mantissa << Math.min(shift, 30);
} else {
value = mantissa >>> Math.min(-shift, 63);
}
if (value <= 0 || value > Integer.MAX_VALUE) {
throw new CodecMediaException("Unsupported AIFF sample rate encoding");
}
return (int) value;
}
private static String readAscii(byte[] bytes, int offset, int len) throws CodecMediaException {
if (offset + len > bytes.length) {
throw new CodecMediaException("Unexpected end of AIFF data");
}
return new String(bytes, offset, len, StandardCharsets.US_ASCII);
}
private static boolean isManagedTextChunk(String chunkId) {
return TEXT_CHUNK_TO_METADATA.containsKey(chunkId);
}
private static List<byte[]> buildTextChunks(Map<String, String> metadataEntries) throws CodecMediaException {
List<byte[]> chunks = new ArrayList<>();
if (metadataEntries == null || metadataEntries.isEmpty()) {
return chunks;
}
for (String metadataKey : METADATA_WRITE_ORDER) {
String value = metadataEntries.get(metadataKey);
if (value == null || value.isBlank()) {
continue;
}
String chunkId = metadataToChunkId(metadataKey);
if (chunkId == null) {
continue;
}
byte[] data = value.getBytes(StandardCharsets.UTF_8);
if (data.length > Integer.MAX_VALUE - 8) {
throw new CodecMediaException("AIFF metadata entry too large: " + metadataKey);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.writeBytes(chunkId.getBytes(StandardCharsets.US_ASCII));
writeBeInt(out, data.length);
out.writeBytes(data);
if ((data.length & 1) != 0) {
out.write(0);
}
chunks.add(out.toByteArray());
}
return chunks;
}
private static String metadataToChunkId(String metadataKey) {
for (Map.Entry<String, String> entry : TEXT_CHUNK_TO_METADATA.entrySet()) {
if (entry.getValue().equals(metadataKey)) {
return entry.getKey();
}
}
return null;
}
private static int readBeShort(byte[] bytes, int offset) throws CodecMediaException {
if (offset + 2 > bytes.length) {
throw new CodecMediaException("Unexpected end of AIFF data");
}
return ((bytes[offset] & 0xFF) << 8) | (bytes[offset + 1] & 0xFF);
}
private static int readBeInt(byte[] bytes, int offset) throws CodecMediaException {
if (offset + 4 > bytes.length) {
throw new CodecMediaException("Unexpected end of AIFF data");
}
return ((bytes[offset] & 0xFF) << 24)
| ((bytes[offset + 1] & 0xFF) << 16)
| ((bytes[offset + 2] & 0xFF) << 8)
| (bytes[offset + 3] & 0xFF);
}
private static long readBeUInt32(byte[] bytes, int offset) throws CodecMediaException {
return readBeInt(bytes, offset) & 0xFFFFFFFFL;
}
private static void writeBeInt(byte[] out, int offset, int value) {
out[offset] = (byte) ((value >>> 24) & 0xFF);
out[offset + 1] = (byte) ((value >>> 16) & 0xFF);
out[offset + 2] = (byte) ((value >>> 8) & 0xFF);
out[offset + 3] = (byte) (value & 0xFF);
}
private static void writeBeInt(ByteArrayOutputStream out, int value) {
out.write((value >>> 24) & 0xFF);
out.write((value >>> 16) & 0xFF);
out.write((value >>> 8) & 0xFF);
out.write(value & 0xFF);
}
}