-
Notifications
You must be signed in to change notification settings - Fork 86
/
SFBWavPackDecoder.m
529 lines (423 loc) · 18 KB
/
SFBWavPackDecoder.m
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//
// Copyright (c) 2011-2024 Stephen F. Booth <[email protected]>
// Part of https://github.com/sbooth/SFBAudioEngine
// MIT license
//
@import os.log;
@import AudioToolbox;
@import wavpack;
#import "SFBWavPackDecoder.h"
#import "AVAudioChannelLayout+SFBChannelLabels.h"
#import "NSData+SFBExtensions.h"
#import "NSError+SFBURLPresentation.h"
SFBAudioDecoderName const SFBAudioDecoderNameWavPack = @"org.sbooth.AudioEngine.Decoder.WavPack";
SFBAudioDecodingPropertiesKey const SFBAudioDecodingPropertiesKeyWavPackMode = @"WavpackGetMode";
SFBAudioDecodingPropertiesKey const SFBAudioDecodingPropertiesKeyWavPackQualifyMode = @"WavpackGetQualifyMode";
SFBAudioDecodingPropertiesKey const SFBAudioDecodingPropertiesKeyWavPackVersion = @"WavpackGetVersion";
SFBAudioDecodingPropertiesKey const SFBAudioDecodingPropertiesKeyWavPackFileFormat = @"WavpackGetFileFormat";
SFBAudioDecodingPropertiesKey const SFBAudioDecodingPropertiesKeyWavPackNumberSamples = @"WavpackGetNumSamples64";
SFBAudioDecodingPropertiesKey const SFBAudioDecodingPropertiesKeyWavPackNumberSamplesInFrame = @"WavpackGetNumSamplesInFrame";
SFBAudioDecodingPropertiesKey const SFBAudioDecodingPropertiesKeyWavPackSampleRate = @"WavpackGetSampleRate";
SFBAudioDecodingPropertiesKey const SFBAudioDecodingPropertiesKeyWavPackNativeSampleRate = @"WavpackGetNativeSampleRate";
SFBAudioDecodingPropertiesKey const SFBAudioDecodingPropertiesKeyWavPackBitsPerSample = @"WavpackGetBitsPerSample";
SFBAudioDecodingPropertiesKey const SFBAudioDecodingPropertiesKeyWavPackBytesPerSample = @"WavpackGetBytesPerSample";
SFBAudioDecodingPropertiesKey const SFBAudioDecodingPropertiesKeyWavPackNumberChannels = @"WavpackGetNumChannels";
SFBAudioDecodingPropertiesKey const SFBAudioDecodingPropertiesKeyWavPackChannelMask = @"WavpackGetChannelMask";
SFBAudioDecodingPropertiesKey const SFBAudioDecodingPropertiesKeyWavPackReducedChannels = @"WavpackGetReducedChannels";
SFBAudioDecodingPropertiesKey const SFBAudioDecodingPropertiesKeyWavPackFloatNormExponent = @"WavpackGetFloatNormExp";
SFBAudioDecodingPropertiesKey const SFBAudioDecodingPropertiesKeyWavPackRatio = @"WavpackGetRatio";
#define BUFFER_SIZE_FRAMES 2048
static int32_t read_bytes_callback(void *id, void *data, int32_t bcount)
{
NSCParameterAssert(id != NULL);
SFBWavPackDecoder *decoder = (__bridge SFBWavPackDecoder *)id;
NSInteger bytesRead;
if(![decoder->_inputSource readBytes:data length:bcount bytesRead:&bytesRead error:nil])
return -1;
return (int32_t)bytesRead;
}
static int64_t get_pos_callback(void *id)
{
NSCParameterAssert(id != NULL);
SFBWavPackDecoder *decoder = (__bridge SFBWavPackDecoder *)id;
NSInteger offset;
if(![decoder->_inputSource getOffset:&offset error:nil])
return 0;
return offset;
}
static int set_pos_abs_callback(void *id, int64_t pos)
{
NSCParameterAssert(id != NULL);
SFBWavPackDecoder *decoder = (__bridge SFBWavPackDecoder *)id;
return ![decoder->_inputSource seekToOffset:pos error:nil];
}
static int set_pos_rel_callback(void *id, int64_t delta, int mode)
{
NSCParameterAssert(id != NULL);
SFBWavPackDecoder *decoder = (__bridge SFBWavPackDecoder *)id;
if(!decoder->_inputSource.supportsSeeking)
return -1;
// Adjust offset as required
NSInteger offset = delta;
switch(mode) {
case SEEK_SET:
// offset remains unchanged
break;
case SEEK_CUR: {
NSInteger inputSourceOffset;
if([decoder->_inputSource getOffset:&inputSourceOffset error:nil])
offset += inputSourceOffset;
break;
}
case SEEK_END: {
NSInteger inputSourceLength;
if([decoder->_inputSource getLength:&inputSourceLength error:nil])
offset += inputSourceLength;
break;
}
}
return ![decoder->_inputSource seekToOffset:offset error:nil];
}
// FIXME: How does one emulate ungetc when the data is non-seekable?
// A small read buffer in SFBWavPackDecoder would work but this function
// only seems to be called once per file (when opening) so it may not be worthwhile
static int push_back_byte_callback(void *id, int c)
{
NSCParameterAssert(id != NULL);
SFBWavPackDecoder *decoder = (__bridge SFBWavPackDecoder *)id;
if(!decoder->_inputSource.supportsSeeking)
return EOF;
NSInteger offset;
if(![decoder->_inputSource getOffset:&offset error:nil] || offset < 1)
return EOF;
if(![decoder->_inputSource seekToOffset:(offset - 1) error:nil])
return EOF;
return c;
}
static int64_t get_length_callback(void *id)
{
NSCParameterAssert(id != NULL);
SFBWavPackDecoder *decoder = (__bridge SFBWavPackDecoder *)id;
NSInteger length;
if(![decoder->_inputSource getLength:&length error:nil])
return -1;
return length;
}
static int can_seek_callback(void *id)
{
NSCParameterAssert(id != NULL);
SFBWavPackDecoder *decoder = (__bridge SFBWavPackDecoder *)id;
return (int)decoder->_inputSource.supportsSeeking;
}
@interface SFBWavPackDecoder ()
{
@private
WavpackStreamReader64 _streamReader;
WavpackContext *_wpc;
int32_t *_buffer;
AVAudioFramePosition _framePosition;
AVAudioFramePosition _frameLength;
}
@end
@implementation SFBWavPackDecoder
+ (void)load
{
[SFBAudioDecoder registerSubclass:[self class]];
}
+ (NSSet *)supportedPathExtensions
{
return [NSSet setWithObject:@"wv"];
}
+ (NSSet *)supportedMIMETypes
{
return [NSSet setWithArray:@[@"audio/wavpack", @"audio/x-wavpack"]];
}
+ (SFBAudioDecoderName)decoderName
{
return SFBAudioDecoderNameWavPack;
}
+ (BOOL)testInputSource:(SFBInputSource *)inputSource formatIsSupported:(SFBTernaryTruthValue *)formatIsSupported error:(NSError **)error
{
NSParameterAssert(inputSource != nil);
NSParameterAssert(formatIsSupported != NULL);
NSData *header = [inputSource readHeaderOfLength:SFBWavPackDetectionSize skipID3v2Tag:NO error:error];
if(!header)
return NO;
if([header isWavPackHeader])
*formatIsSupported = SFBTernaryTruthValueTrue;
else
*formatIsSupported = SFBTernaryTruthValueFalse;
return YES;
}
- (BOOL)decodingIsLossless
{
return (WavpackGetMode(_wpc) & MODE_LOSSLESS) == MODE_LOSSLESS;
}
- (BOOL)openReturningError:(NSError **)error
{
if(![super openReturningError:error])
return NO;
_streamReader.read_bytes = read_bytes_callback;
_streamReader.get_pos = get_pos_callback;
_streamReader.set_pos_abs = set_pos_abs_callback;
_streamReader.set_pos_rel = set_pos_rel_callback;
_streamReader.push_back_byte = push_back_byte_callback;
_streamReader.get_length = get_length_callback;
_streamReader.can_seek = can_seek_callback;
char errorBuf [80];
// Setup converter
_wpc = WavpackOpenFileInputEx64(&_streamReader, (__bridge void *)self, NULL, errorBuf, OPEN_WVC | OPEN_NORMALIZE/* | OPEN_DSD_NATIVE*/, 0);
if(!_wpc) {
os_log_error(gSFBAudioDecoderLog, "Error opening WavPack file: %s", errorBuf);
if(error)
*error = [NSError SFB_errorWithDomain:SFBAudioDecoderErrorDomain
code:SFBAudioDecoderErrorCodeInvalidFormat
descriptionFormatStringForURL:NSLocalizedString(@"The file “%@” is not a valid WavPack file.", @"")
url:_inputSource.url
failureReason:NSLocalizedString(@"Not a WavPack file", @"")
recoverySuggestion:NSLocalizedString(@"The file's extension may not match the file's type.", @"")];
return NO;
}
AVAudioChannelLayout *channelLayout = nil;
// Attempt to use the standard WAVE channel mask
int channelMask = WavpackGetChannelMask(_wpc);
if(channelMask) {
AudioChannelLayout layout = {
.mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelBitmap,
.mChannelBitmap = channelMask,
.mNumberChannelDescriptions = 0,
};
AudioChannelLayoutTag tag = 0;
UInt32 propertySize = sizeof(tag);
OSStatus result = AudioFormatGetProperty(kAudioFormatProperty_TagForChannelLayout, sizeof(layout), &layout, &propertySize, &tag);
if(result == noErr)
channelLayout = [[AVAudioChannelLayout alloc] initWithLayoutTag:tag];
else
channelLayout = [AVAudioChannelLayout layoutWithLayout:&layout];
}
// Fall back on the WavPack channel identities
if(!channelLayout) {
int channelCount = WavpackGetNumChannels(_wpc);
unsigned char identities [channelCount + 1];
WavpackGetChannelIdentities(_wpc, identities);
// Convert from WavPack channel identity to Core Audio channel label
AudioChannelLabel labels [channelCount];
// from pack_utils.c:
//
// The channel IDs so far reserved are listed here:
//
// 0: not allowed / terminator
// 1 - 18: Microsoft standard channels
// 30, 31: Stereo mix from RF64 (not really recommended, but RF64 specifies this)
// 33 - 44: Core Audio channels (see Core Audio specification)
// 127 - 128: Amio LeftHeight, Amio RightHeight
// 138 - 142: Amio BottomFrontLeft/Center/Right, Amio ProximityLeft/Right
// 200 - 207: Core Audio channels (see Core Audio specification)
// 221 - 224: Core Audio channels 301 - 305 (offset by 80)
// 255: Present but unknown or unused channel
for(int i = 0; i < channelCount; ++i) {
switch(identities[i]) {
case 1 ... 18: labels[i] = identities[i]; break;
case 30: labels[i] = kAudioChannelLabel_Left; break;
case 31: labels[i] = kAudioChannelLabel_Right; break;
case 33 ... 44: labels[i] = identities[i]; break;
// FIXME: amio mappings are approximate (or possible incorrect)
case 127: labels[i] = kAudioChannelLabel_VerticalHeightLeft; break;
case 128: labels[i] = kAudioChannelLabel_VerticalHeightRight; break;
case 138: labels[i] = kAudioChannelLabel_LeftBottom; break;
case 139: labels[i] = kAudioChannelLabel_CenterBottom; break;
case 140: labels[i] = kAudioChannelLabel_RightBottom; break;
case 141: labels[i] = kAudioChannelLabel_LeftEdgeOfScreen; break;
case 142: labels[i] = kAudioChannelLabel_RightEdgeOfScreen; break;
case 200 ... 207: labels[i] = identities[i]; break;
case 221 ... 224: labels[i] = identities[i] + 80; break;
case 255: labels[i] = kAudioChannelLabel_Unknown; break;
default:
os_log_error(gSFBAudioDecoderLog, "Invalid WavPack channel ID: %d", identities[i]);
labels[i] = kAudioChannelLabel_Unused;
break;
}
}
channelLayout = [AVAudioChannelLayout layoutWithChannelLabels:labels count:channelCount];
}
// Floating-point and lossy files will be handed off in the canonical Core Audio format
int mode = WavpackGetMode(_wpc);
// int qmode = WavpackGetQualifyMode(_wpc);
if(MODE_FLOAT & mode || !(MODE_LOSSLESS & mode)) {
_processingFormat = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32 sampleRate:WavpackGetSampleRate(_wpc) interleaved:NO channelLayout:channelLayout];
}
// else if(qmode & QMODE_DSD_AUDIO) {
// }
else {
AudioStreamBasicDescription processingStreamDescription = {0};
processingStreamDescription.mFormatID = kAudioFormatLinearPCM;
processingStreamDescription.mFormatFlags = kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsNonInterleaved;
// Align high because Apple's AudioConverter doesn't handle low alignment
if(WavpackGetBitsPerSample(_wpc) != 32)
processingStreamDescription.mFormatFlags |= kAudioFormatFlagIsAlignedHigh;
processingStreamDescription.mSampleRate = WavpackGetSampleRate(_wpc);
processingStreamDescription.mChannelsPerFrame = (UInt32)WavpackGetNumChannels(_wpc);
processingStreamDescription.mBitsPerChannel = (UInt32)WavpackGetBitsPerSample(_wpc);
processingStreamDescription.mBytesPerPacket = 4;
processingStreamDescription.mFramesPerPacket = 1;
processingStreamDescription.mBytesPerFrame = processingStreamDescription.mBytesPerPacket / processingStreamDescription.mFramesPerPacket;
_processingFormat = [[AVAudioFormat alloc] initWithStreamDescription:&processingStreamDescription channelLayout:channelLayout];
}
_frameLength = WavpackGetNumSamples64(_wpc);
// Set up the source format
AudioStreamBasicDescription sourceStreamDescription = {0};
sourceStreamDescription.mFormatID = kSFBAudioFormatWavPack;
sourceStreamDescription.mSampleRate = WavpackGetSampleRate(_wpc);
sourceStreamDescription.mChannelsPerFrame = (UInt32)WavpackGetNumChannels(_wpc);
sourceStreamDescription.mBitsPerChannel = (UInt32)WavpackGetBitsPerSample(_wpc);
sourceStreamDescription.mBytesPerPacket = (UInt32)WavpackGetBytesPerSample(_wpc);
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription];
// Populate codec properties
_properties = @{
SFBAudioDecodingPropertiesKeyWavPackMode: @(WavpackGetMode(_wpc)),
SFBAudioDecodingPropertiesKeyWavPackQualifyMode: @(WavpackGetQualifyMode(_wpc)),
SFBAudioDecodingPropertiesKeyWavPackVersion: @(WavpackGetVersion(_wpc)),
SFBAudioDecodingPropertiesKeyWavPackFileFormat: @(WavpackGetFileFormat(_wpc)),
SFBAudioDecodingPropertiesKeyWavPackNumberSamples: @(WavpackGetNumSamples64(_wpc)),
SFBAudioDecodingPropertiesKeyWavPackNumberSamplesInFrame: @(WavpackGetNumSamplesInFrame(_wpc)),
SFBAudioDecodingPropertiesKeyWavPackSampleRate: @(WavpackGetSampleRate(_wpc)),
SFBAudioDecodingPropertiesKeyWavPackNativeSampleRate: @(WavpackGetNativeSampleRate(_wpc)),
SFBAudioDecodingPropertiesKeyWavPackBitsPerSample: @(WavpackGetBitsPerSample(_wpc)),
SFBAudioDecodingPropertiesKeyWavPackBytesPerSample: @(WavpackGetBytesPerSample(_wpc)),
SFBAudioDecodingPropertiesKeyWavPackNumberChannels: @(WavpackGetNumChannels(_wpc)),
SFBAudioDecodingPropertiesKeyWavPackChannelMask: @(WavpackGetChannelMask(_wpc)),
SFBAudioDecodingPropertiesKeyWavPackReducedChannels: @(WavpackGetReducedChannels(_wpc)),
SFBAudioDecodingPropertiesKeyWavPackFloatNormExponent: @(WavpackGetFloatNormExp(_wpc)),
SFBAudioDecodingPropertiesKeyWavPackRatio: @(WavpackGetRatio(_wpc)),
};
_buffer = malloc(sizeof(int32_t) * (size_t)BUFFER_SIZE_FRAMES * (size_t)WavpackGetNumChannels(_wpc));
if(!_buffer) {
WavpackCloseFile(_wpc);
_wpc = NULL;
if(error)
*error = [NSError errorWithDomain:NSPOSIXErrorDomain code:ENOMEM userInfo:nil];
return NO;
}
return YES;
}
- (BOOL)closeReturningError:(NSError **)error
{
if(_buffer) {
free(_buffer);
_buffer = NULL;
}
if(_wpc) {
WavpackCloseFile(_wpc);
_wpc = NULL;
}
return [super closeReturningError:error];
}
- (BOOL)isOpen
{
return _wpc != NULL;
}
- (AVAudioFramePosition)framePosition
{
return _framePosition;
}
- (AVAudioFramePosition)frameLength
{
return _frameLength;
}
- (BOOL)decodeIntoBuffer:(AVAudioPCMBuffer *)buffer frameLength:(AVAudioFrameCount)frameLength error:(NSError **)error
{
NSParameterAssert(buffer != nil);
NSParameterAssert([buffer.format isEqual:_processingFormat]);
// Reset output buffer data size
buffer.frameLength = 0;
if(frameLength > buffer.frameCapacity)
frameLength = buffer.frameCapacity;
if(frameLength == 0)
return YES;
AVAudioFrameCount framesRemaining = frameLength;
while(framesRemaining > 0) {
uint32_t framesToRead = MIN(framesRemaining, BUFFER_SIZE_FRAMES);
// Wavpack uses "complete" samples (one sample across all channels), i.e. a Core Audio frame
uint32_t samplesRead = WavpackUnpackSamples(_wpc, _buffer, framesToRead);
if(samplesRead == 0)
break;
// FIXME: What is the best way to detect a decoding error here?
// The documentation states:
// The actual number of samples unpacked is returned, which should be equal to the number requested unless the end of file is encountered or an error occurs. If all samples have been unpacked then 0 will be returned.
// The samples returned are handled differently based on the file's mode
int mode = WavpackGetMode(_wpc);
// int qmode = WavpackGetQualifyMode(_wpc);
// Floating point files require no special handling other than deinterleaving
if(mode & MODE_FLOAT) {
float * const *floatChannelData = buffer.floatChannelData;
AVAudioChannelCount channelCount = buffer.format.channelCount;
for(AVAudioChannelCount channel = 0; channel < channelCount; ++channel) {
const float *input = (float *)_buffer + channel;
float *output = floatChannelData[channel] + buffer.frameLength;
for(uint32_t sample = 0; sample < samplesRead; ++sample) {
*output++ = *input;
input += channelCount;
}
}
buffer.frameLength += samplesRead;
}
// Lossless files will be handed off as integers
else if(mode & MODE_LOSSLESS) {
// WavPack hands us 32-bit signed integers with the samples low-aligned
int shift = 8 * (4 - WavpackGetBytesPerSample(_wpc));
int32_t * const *int32ChannelData = buffer.int32ChannelData;
AVAudioChannelCount channelCount = buffer.format.channelCount;
// Deinterleave the 32-bit samples, shifting to high alignment
if(shift) {
for(AVAudioChannelCount channel = 0; channel < channelCount; ++channel) {
const int32_t *input = _buffer + channel;
int32_t *output = int32ChannelData[channel] + buffer.frameLength;
for(uint32_t sample = 0; sample < samplesRead; ++sample) {
*output++ = (int32_t)((uint32_t)*input << shift);
input += channelCount;
}
}
}
// Just deinterleave the 32-bit samples
else {
for(AVAudioChannelCount channel = 0; channel < channelCount; ++channel) {
const int32_t *input = _buffer + channel;
int32_t *output = int32ChannelData[channel] + buffer.frameLength;
for(uint32_t sample = 0; sample < samplesRead; ++sample) {
*output++ = *input;
input += channelCount;
}
}
}
buffer.frameLength += samplesRead;
}
// Convert lossy files to float
else {
float scaleFactor = ((uint32_t)1 << ((WavpackGetBytesPerSample(_wpc) * 8) - 1));
// Deinterleave the 32-bit samples and convert to float
float * const *floatChannelData = buffer.floatChannelData;
AVAudioChannelCount channelCount = buffer.format.channelCount;
for(AVAudioChannelCount channel = 0; channel < channelCount; ++channel) {
const int32_t *input = _buffer + channel;
float *output = floatChannelData[channel] + buffer.frameLength;
for(uint32_t sample = 0; sample < samplesRead; ++sample) {
*output++ = *input / scaleFactor;
input += channelCount;
}
}
buffer.frameLength += samplesRead;
}
framesRemaining -= samplesRead;
_framePosition += samplesRead;
}
return YES;
}
- (BOOL)seekToFrame:(AVAudioFramePosition)frame error:(NSError **)error
{
NSParameterAssert(frame >= 0);
if(!WavpackSeekSample64(_wpc, frame))
return NO;
_framePosition = frame;
return YES;
}
@end