forked from sbooth/SFBAudioEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSFBDSFDecoder.m
More file actions
448 lines (355 loc) · 14.9 KB
/
SFBDSFDecoder.m
File metadata and controls
448 lines (355 loc) · 14.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//
// Copyright (c) 2014-2025 Stephen F. Booth <me@sbooth.org>
// Part of https://github.com/sbooth/SFBAudioEngine
// MIT license
//
#import <stdint.h>
@import os.log;
#import "SFBDSFDecoder.h"
#import "NSData+SFBExtensions.h"
#import "NSError+SFBURLPresentation.h"
#import "SFBCStringForOSType.h"
SFBDSDDecoderName const SFBDSDDecoderNameDSF = @"org.sbooth.AudioEngine.DSDDecoder.DSF";
#define DSF_BLOCK_SIZE_BYTES_PER_CHANNEL 4096
// Read a four byte chunk ID as a uint32_t
static BOOL ReadChunkID(SFBInputSource *inputSource, uint32_t *chunkID)
{
NSCParameterAssert(chunkID != NULL);
char chunkIDBytes [4];
NSInteger bytesRead;
if(![inputSource readBytes:chunkIDBytes length:4 bytesRead:&bytesRead error:nil] || bytesRead != 4) {
os_log_error(gSFBDSDDecoderLog, "Unable to read chunk ID");
return NO;
}
*chunkID = (uint32_t)((chunkIDBytes[0] << 24u) | (chunkIDBytes[1] << 16u) | (chunkIDBytes[2] << 8u) | chunkIDBytes[3]);
return YES;
}
static NSError * CreateInvalidDSFFileError(NSURL * url)
{
return [NSError SFB_errorWithDomain:SFBDSDDecoderErrorDomain
code:SFBDSDDecoderErrorCodeInvalidFormat
descriptionFormatStringForURL:NSLocalizedString(@"The file “%@” is not a valid DSF file.", @"")
url:url
failureReason:NSLocalizedString(@"Not a DSF file", @"")
recoverySuggestion:NSLocalizedString(@"The file's extension may not match the file's type.", @"")];
}
// For the size of matrices this class deals with the naive approach is adequate
static void MatrixTransposeNaive(const uint8_t * restrict A, uint8_t * restrict B, NSInteger rows, NSInteger columns)
{
for(NSInteger i = 0; i < rows; ++i) {
for(NSInteger j = 0; j < columns; ++j)
B[j * rows + i] = A[i * columns + j];
}
}
@interface SFBDSFDecoder ()
{
@private
AVAudioFramePosition _packetPosition;
AVAudioFramePosition _packetCount;
int64_t _audioOffset;
AVAudioCompressedBuffer *_buffer;
}
- (BOOL)readAndInterleaveDSFBlockReturningError:(NSError **)error;
@end
@implementation SFBDSFDecoder
+ (void)load
{
[SFBDSDDecoder registerSubclass:[self class]];
}
+ (NSSet *)supportedPathExtensions
{
return [NSSet setWithObject:@"dsf"];
}
+ (NSSet *)supportedMIMETypes
{
return [NSSet setWithObject:@"audio/dsf"];
}
+ (SFBDSDDecoderName)decoderName
{
return SFBDSDDecoderNameDSF;
}
+ (BOOL)testInputSource:(SFBInputSource *)inputSource formatIsSupported:(SFBTernaryTruthValue *)formatIsSupported error:(NSError **)error
{
NSParameterAssert(inputSource != nil);
NSParameterAssert(formatIsSupported != NULL);
NSData *header = [inputSource readHeaderOfLength:SFBDSFDetectionSize skipID3v2Tag:NO error:error];
if(!header)
return NO;
if([header isDSFHeader])
*formatIsSupported = SFBTernaryTruthValueTrue;
else
*formatIsSupported = SFBTernaryTruthValueFalse;
return YES;
}
- (BOOL)decodingIsLossless
{
return YES;
}
- (BOOL)openReturningError:(NSError **)error
{
if(![super openReturningError:error])
return NO;
// Read the 'DSD ' chunk
uint32_t chunkID;
if(!ReadChunkID(_inputSource, &chunkID) || chunkID != 'DSD ') {
os_log_error(gSFBDSDDecoderLog, "Unable to read 'DSD ' chunk");
if(error)
*error = CreateInvalidDSFFileError(_inputSource.url);
return NO;
}
uint64_t chunkSize, fileSize, metadataOffset;
// Unlike normal IFF, the chunkSize includes the size of the chunk ID and size
if(![_inputSource readUInt64LittleEndian:&chunkSize error:nil] || chunkSize != 28) {
os_log_error(gSFBDSDDecoderLog, "Unexpected 'DSD ' chunk size: %llu", chunkSize);
if(error)
*error = CreateInvalidDSFFileError(_inputSource.url);
return NO;
}
if(![_inputSource readUInt64LittleEndian:&fileSize error:nil]) {
os_log_error(gSFBDSDDecoderLog, "Unable to read file size in 'DSD ' chunk");
if(error)
*error = CreateInvalidDSFFileError(_inputSource.url);
return NO;
}
if(![_inputSource readUInt64LittleEndian:&metadataOffset error:nil]) {
os_log_error(gSFBDSDDecoderLog, "Unable to read metadata offset in 'DSD ' chunk");
if(error)
*error = CreateInvalidDSFFileError(_inputSource.url);
return NO;
}
// Read the 'fmt ' chunk
if(!ReadChunkID(_inputSource, &chunkID) || chunkID != 'fmt ') {
os_log_error(gSFBDSDDecoderLog, "Unable to read 'fmt ' chunk");
if(error)
*error = CreateInvalidDSFFileError(_inputSource.url);
return NO;
}
if(![_inputSource readUInt64LittleEndian:&chunkSize error:nil]) {
os_log_error(gSFBDSDDecoderLog, "Unexpected 'fmt ' chunk size: %llu", chunkSize);
if(error)
*error = CreateInvalidDSFFileError(_inputSource.url);
return NO;
}
uint32_t formatVersion, formatID, channelType, channelNum, samplingFrequency, bitsPerSample;
uint64_t sampleCount;
uint32_t blockSizePerChannel, reserved;
if(![_inputSource readUInt32LittleEndian:&formatVersion error:nil] || formatVersion != 1) {
os_log_error(gSFBDSDDecoderLog, "Unexpected format version in 'fmt ': %u", formatVersion);
if(error)
*error = CreateInvalidDSFFileError(_inputSource.url);
return NO;
}
if(![_inputSource readUInt32LittleEndian:&formatID error:nil] || formatID != 0) {
os_log_error(gSFBDSDDecoderLog, "Unexpected format ID in 'fmt ': %{public}.4s", SFBCStringForOSType(formatID));
if(error)
*error = CreateInvalidDSFFileError(_inputSource.url);
return NO;
}
if(![_inputSource readUInt32LittleEndian:&channelType error:nil] || (channelType < 1 || channelType > 7)) {
os_log_error(gSFBDSDDecoderLog, "Unexpected channel type in 'fmt ': %u", channelType);
if(error)
*error = CreateInvalidDSFFileError(_inputSource.url);
return NO;
}
if(![_inputSource readUInt32LittleEndian:&channelNum error:nil] || (channelNum < 1 || channelNum > 6)) {
os_log_error(gSFBDSDDecoderLog, "Unexpected channel count in 'fmt ': %u", channelNum);
if(error)
*error = CreateInvalidDSFFileError(_inputSource.url);
return NO;
}
if(![_inputSource readUInt32LittleEndian:&samplingFrequency error:nil] || (samplingFrequency != kSFBSampleRateDSD64 && samplingFrequency != kSFBSampleRateDSD128)) {
os_log_error(gSFBDSDDecoderLog, "Unexpected sample rate in 'fmt ': %u", samplingFrequency);
if(error)
*error = CreateInvalidDSFFileError(_inputSource.url);
return NO;
}
if(![_inputSource readUInt32LittleEndian:&bitsPerSample error:nil] || (bitsPerSample != 1 && bitsPerSample != 8)) {
os_log_error(gSFBDSDDecoderLog, "Unexpected bits per sample in 'fmt ': %u", bitsPerSample);
if(error)
*error = CreateInvalidDSFFileError(_inputSource.url);
return NO;
}
if(![_inputSource readUInt64LittleEndian:&sampleCount error:nil]) {
os_log_error(gSFBDSDDecoderLog, "Unable to read sample count in 'fmt ' chunk");
if(error)
*error = CreateInvalidDSFFileError(_inputSource.url);
return NO;
}
if(![_inputSource readUInt32LittleEndian:&blockSizePerChannel error:nil] || blockSizePerChannel != DSF_BLOCK_SIZE_BYTES_PER_CHANNEL) {
os_log_error(gSFBDSDDecoderLog, "Unexpected block size per channel in 'fmt ': %u", blockSizePerChannel);
if(error)
*error = CreateInvalidDSFFileError(_inputSource.url);
return NO;
}
if(![_inputSource readUInt32LittleEndian:&reserved error:nil] || reserved != 0) {
os_log_error(gSFBDSDDecoderLog, "Unexpected non-zero value for reserved in 'fmt ': %u", reserved);
if(error)
*error = CreateInvalidDSFFileError(_inputSource.url);
return NO;
}
// Read the 'data' chunk
if(!ReadChunkID(_inputSource, &chunkID) || chunkID != 'data') {
os_log_error(gSFBDSDDecoderLog, "Unable to read 'data' chunk");
if(error)
*error = CreateInvalidDSFFileError(_inputSource.url);
return NO;
}
if(![_inputSource readUInt64LittleEndian:&chunkSize error:nil]) {
os_log_error(gSFBDSDDecoderLog, "Unexpected 'data' chunk size: %llu", chunkSize);
if(error)
*error = CreateInvalidDSFFileError(_inputSource.url);
return NO;
}
_packetCount = sampleCount / kSFBPCMFramesPerDSDPacket;
NSInteger offset;
if(![_inputSource getOffset:&offset error:nil]) {
os_log_error(gSFBDSDDecoderLog, "Error getting audio offset");
return NO;
}
_audioOffset = offset;
// Channel layouts are defined in the DSF file format specification
AVAudioChannelLayout *channelLayout = nil;
switch(channelType) {
case 1: channelLayout = [AVAudioChannelLayout layoutWithLayoutTag:kAudioChannelLayoutTag_Mono]; break;
case 2: channelLayout = [AVAudioChannelLayout layoutWithLayoutTag:kAudioChannelLayoutTag_Stereo]; break;
case 3: channelLayout = [AVAudioChannelLayout layoutWithLayoutTag:kAudioChannelLayoutTag_MPEG_3_0_A]; break;
case 4: channelLayout = [AVAudioChannelLayout layoutWithLayoutTag:kAudioChannelLayoutTag_WAVE_4_0_B]; break;
case 5: channelLayout = [AVAudioChannelLayout layoutWithLayoutTag:kAudioChannelLayoutTag_DVD_10]; break;
case 6: channelLayout = [AVAudioChannelLayout layoutWithLayoutTag:kAudioChannelLayoutTag_WAVE_5_0_B]; break;
case 7: channelLayout = [AVAudioChannelLayout layoutWithLayoutTag:kAudioChannelLayoutTag_WAVE_5_1_B]; break;
}
AudioStreamBasicDescription processingStreamDescription = {0};
// The output format is raw DSD
processingStreamDescription.mFormatID = kSFBAudioFormatDSD;
processingStreamDescription.mFormatFlags = bitsPerSample == 8 ? kAudioFormatFlagIsBigEndian : 0;
processingStreamDescription.mSampleRate = (Float64)samplingFrequency;
processingStreamDescription.mChannelsPerFrame = (UInt32)channelNum;
processingStreamDescription.mBitsPerChannel = 1;
processingStreamDescription.mBytesPerPacket = kSFBBytesPerDSDPacketPerChannel * channelNum;
processingStreamDescription.mFramesPerPacket = kSFBPCMFramesPerDSDPacket;
_processingFormat = [[AVAudioFormat alloc] initWithStreamDescription:&processingStreamDescription channelLayout:channelLayout];
// Set up the source format
AudioStreamBasicDescription sourceStreamDescription = {0};
sourceStreamDescription.mFormatID = kSFBAudioFormatDSD;
sourceStreamDescription.mSampleRate = (Float64)samplingFrequency;
sourceStreamDescription.mChannelsPerFrame = (UInt32)channelNum;
sourceStreamDescription.mBitsPerChannel = 1;
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription channelLayout:channelLayout];
// Metadata chunk is ignored
_buffer = [[AVAudioCompressedBuffer alloc] initWithFormat:_processingFormat packetCapacity:(DSF_BLOCK_SIZE_BYTES_PER_CHANNEL / kSFBBytesPerDSDPacketPerChannel) maximumPacketSize:(kSFBBytesPerDSDPacketPerChannel * channelNum)];
_buffer.packetCount = 0;
return YES;
}
- (BOOL)closeReturningError:(NSError **)error
{
_buffer = nil;
return [super closeReturningError:error];
}
- (BOOL)isOpen
{
return _buffer != nil;
}
- (AVAudioFramePosition)packetPosition
{
return _packetPosition;
}
- (AVAudioFramePosition)packetCount
{
return _packetCount;
}
- (BOOL)decodeIntoBuffer:(AVAudioCompressedBuffer *)buffer packetCount:(AVAudioPacketCount)packetCount error:(NSError **)error
{
NSParameterAssert(buffer != nil);
NSParameterAssert([buffer.format isEqual:_processingFormat]);
// Reset output buffer data size
buffer.packetCount = 0;
buffer.byteLength = 0;
if(packetCount > buffer.packetCapacity)
packetCount = buffer.packetCapacity;
if(packetCount == 0)
return YES;
AVAudioPacketCount packetsProcessed = 0;
uint32_t packetSize = kSFBBytesPerDSDPacketPerChannel * _processingFormat.channelCount;
for(;;) {
AVAudioPacketCount packetsRemaining = packetCount - packetsProcessed;
AVAudioPacketCount packetsToSkip = buffer.packetCount;
AVAudioPacketCount packetsInBuffer = _buffer.packetCount;
AVAudioPacketCount packetsToCopy = MIN(packetsInBuffer, packetsRemaining);
// Copy data from the internal buffer to output
uint32_t copySize = packetsToCopy * packetSize;
memcpy((void *)((uintptr_t)buffer.data + (packetsToSkip * packetSize)), _buffer.data, copySize);
buffer.packetCount += packetsToCopy;
buffer.byteLength += copySize;
// Move remaining data in buffer to beginning
if(packetsToCopy != packetsInBuffer) {
uint8_t *dst = (uint8_t *)_buffer.data;
memmove(dst, dst + copySize, (packetsInBuffer - packetsToCopy) * packetSize);
}
_buffer.packetCount -= packetsToCopy;
_buffer.byteLength -= copySize;
packetsProcessed += packetsToCopy;
// All requested packets were read
if(packetsProcessed == packetCount)
break;
// Read the next block
if(![self readAndInterleaveDSFBlockReturningError:error])
return NO;
}
_packetPosition += packetsProcessed;
return YES;
}
- (BOOL)seekToPacket:(AVAudioFramePosition)packet error:(NSError **)error
{
NSParameterAssert(packet >= 0);
AVAudioChannelCount channelCount = _processingFormat.channelCount;
// A DSF version 1 block is 4096 bytes per channel
// This equates to 4096 packets or 32768 frames per block
// Seek to the start of the block containing packet
NSInteger blockNumber = packet / DSF_BLOCK_SIZE_BYTES_PER_CHANNEL;
NSInteger blockOffset = blockNumber * DSF_BLOCK_SIZE_BYTES_PER_CHANNEL * channelCount;
if(![_inputSource seekToOffset:(_audioOffset + blockOffset) error:error]) {
os_log_debug(gSFBDSDDecoderLog, "-seekToPacket:error: failed seeking to input offset: %lld", _audioOffset + blockOffset);
return NO;
}
if(![self readAndInterleaveDSFBlockReturningError:error])
return NO;
// Skip ahead in the interleaved audio to the specified packet
AVAudioPacketCount packetsInBuffer = _buffer.packetCount;
AVAudioPacketCount packetsToSkip = (AVAudioPacketCount)(packet % packetsInBuffer);
AVAudioPacketCount packetsToMove = packetsInBuffer - packetsToSkip;
// Move data
uint32_t packetSize = kSFBBytesPerDSDPacketPerChannel * _processingFormat.channelCount;
const void *src = (const void *)((uintptr_t)_buffer.data + (packetsToSkip * packetSize));
memmove(_buffer.data, src, packetsToMove * packetSize);
_buffer.packetCount = packetsToMove;
_buffer.byteLength = packetsToMove * packetSize;
_packetPosition = packet;
return YES;
}
// Read input, grouped in DSF as 8 one-bit samples per frame (a single channel byte) in a block
// of the specified block size (4096 bytes per channel for DSF version 1) for each channel,
// then interleave the channel bytes into clustered frames.
// The DSF blocks form a matrix with one row per channel and one column per channel byte.
// For stereo, the data is arranged as 4096 L channel bytes followed by 4096 R channel bytes,
// a 2 x 4096 matrix.
// Interleaving is accomplished by matrix transposition.
- (BOOL)readAndInterleaveDSFBlockReturningError:(NSError **)error
{
uint8_t *buf = (uint8_t *)_buffer.data;
uint32_t bufsize = _buffer.byteCapacity;
NSInteger bytesRead;
if(![_inputSource readBytes:buf length:bufsize bytesRead:&bytesRead error:error] || bytesRead != bufsize) {
os_log_error(gSFBDSDDecoderLog, "Error reading audio block: requested %u bytes, got %ld", bufsize, bytesRead);
return NO;
}
// Deinterleave the blocks and interleave the samples into clustered frames
AVAudioChannelCount channelCount = _processingFormat.channelCount;
assert(channelCount != 0);
uint8_t tmp [bufsize];
MatrixTransposeNaive(buf, tmp, channelCount, DSF_BLOCK_SIZE_BYTES_PER_CHANNEL);
memcpy(buf, tmp, bufsize);
_buffer.packetCount = (AVAudioPacketCount)(bytesRead / (kSFBBytesPerDSDPacketPerChannel * channelCount));
_buffer.byteLength = (uint32_t)bytesRead;
return YES;
}
@end