2013-10-05 06:01:33 -03:00
|
|
|
//
|
|
|
|
// OpusDecoder.m
|
|
|
|
// Opus
|
|
|
|
//
|
|
|
|
// Created by Christopher Snowhill on 10/4/13.
|
|
|
|
// Copyright 2013 __NoWork, Inc__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2013-10-05 18:15:09 -03:00
|
|
|
#import "Plugin.h"
|
|
|
|
|
2013-10-05 06:01:33 -03:00
|
|
|
#import "OpusDecoder.h"
|
|
|
|
|
2013-10-11 09:03:55 -03:00
|
|
|
#import "Logging.h"
|
2013-10-05 06:01:33 -03:00
|
|
|
|
2022-02-09 18:44:50 -03:00
|
|
|
#import "HTTPSource.h"
|
|
|
|
|
2013-10-05 06:01:33 -03:00
|
|
|
@implementation OpusFile
|
|
|
|
|
2021-12-28 05:54:28 -03:00
|
|
|
static const int MAXCHANNELS = 8;
|
|
|
|
static const int chmap[MAXCHANNELS][MAXCHANNELS] = {
|
2022-02-07 02:49:27 -03:00
|
|
|
{
|
|
|
|
0,
|
|
|
|
}, // mono
|
|
|
|
{
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
}, // l, r
|
|
|
|
{
|
|
|
|
0,
|
|
|
|
2,
|
|
|
|
1,
|
|
|
|
}, // l, c, r -> l, r, c
|
|
|
|
{
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
}, // l, r, bl, br
|
|
|
|
{
|
|
|
|
0,
|
|
|
|
2,
|
|
|
|
1,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
}, // l, c, r, bl, br -> l, r, c, bl, br
|
|
|
|
{ 0, 2, 1, 5, 3, 4 }, // l, c, r, bl, br, lfe -> l, r, c, lfe, bl, br
|
|
|
|
{ 0, 2, 1, 6, 5, 3, 4 }, // l, c, r, sl, sr, bc, lfe -> l, r, c, lfe, bc, sl, sr
|
|
|
|
{ 0, 2, 1, 7, 5, 6, 3, 4 } // l, c, r, sl, sr, bl, br, lfe -> l, r, c, lfe, bl, br, sl, sr
|
2021-12-28 05:54:28 -03:00
|
|
|
};
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
int sourceRead(void *_stream, unsigned char *_ptr, int _nbytes) {
|
2016-06-19 15:57:18 -04:00
|
|
|
id source = (__bridge id)_stream;
|
2013-10-05 06:01:33 -03:00
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
return (int)[source read:_ptr amount:_nbytes];
|
2013-10-05 06:01:33 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
int sourceSeek(void *_stream, opus_int64 _offset, int _whence) {
|
2016-06-19 15:57:18 -04:00
|
|
|
id source = (__bridge id)_stream;
|
2013-10-05 06:01:33 -03:00
|
|
|
return ([source seek:_offset whence:_whence] ? 0 : -1);
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
int sourceClose(void *_stream) {
|
2013-10-05 06:01:33 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
opus_int64 sourceTell(void *_stream) {
|
2016-06-19 15:57:18 -04:00
|
|
|
id source = (__bridge id)_stream;
|
2013-10-05 06:01:33 -03:00
|
|
|
|
|
|
|
return [source tell];
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (id)init {
|
|
|
|
self = [super init];
|
|
|
|
if(self) {
|
|
|
|
opusRef = NULL;
|
|
|
|
}
|
|
|
|
return self;
|
2013-10-05 06:01:33 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (BOOL)open:(id<CogSource>)s {
|
2016-06-19 15:57:18 -04:00
|
|
|
source = s;
|
2022-02-07 02:49:27 -03:00
|
|
|
|
2013-10-05 06:01:33 -03:00
|
|
|
OpusFileCallbacks callbacks = {
|
2022-02-07 02:49:27 -03:00
|
|
|
.read = sourceRead,
|
|
|
|
.seek = sourceSeek,
|
|
|
|
.close = sourceClose,
|
|
|
|
.tell = sourceTell
|
2013-10-05 06:01:33 -03:00
|
|
|
};
|
2022-02-07 02:49:27 -03:00
|
|
|
|
|
|
|
int error;
|
|
|
|
opusRef = op_open_callbacks((__bridge void *)source, &callbacks, NULL, 0, &error);
|
|
|
|
|
|
|
|
if(!opusRef) {
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"FAILED TO OPEN OPUS FILE");
|
2013-10-05 06:01:33 -03:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
currentSection = lastSection = op_current_link(opusRef);
|
|
|
|
|
|
|
|
bitrate = (op_bitrate(opusRef, currentSection) / 1000.0);
|
|
|
|
channels = op_channel_count(opusRef, currentSection);
|
|
|
|
|
2013-10-05 06:01:33 -03:00
|
|
|
seekable = op_seekable(opusRef);
|
2022-02-07 02:49:27 -03:00
|
|
|
|
2013-10-05 06:01:33 -03:00
|
|
|
totalFrames = op_pcm_total(opusRef, -1);
|
2022-02-07 02:49:27 -03:00
|
|
|
|
|
|
|
NSString *gainMode = [[NSUserDefaults standardUserDefaults] stringForKey:@"volumeScaling"];
|
|
|
|
BOOL isAlbum = [gainMode hasPrefix:@"albumGain"];
|
|
|
|
BOOL isTrack = [gainMode hasPrefix:@"trackGain"];
|
|
|
|
|
|
|
|
if(isAlbum || isTrack) {
|
|
|
|
op_set_gain_offset(opusRef, isAlbum ? OP_HEADER_GAIN : OP_TRACK_GAIN, 0);
|
|
|
|
} else {
|
|
|
|
op_set_gain_offset(opusRef, OP_ABSOLUTE_GAIN, 0);
|
|
|
|
}
|
2013-10-05 06:01:33 -03:00
|
|
|
|
|
|
|
[self willChangeValueForKey:@"properties"];
|
|
|
|
[self didChangeValueForKey:@"properties"];
|
2022-02-07 02:49:27 -03:00
|
|
|
|
2022-02-09 18:44:50 -03:00
|
|
|
genre = @"";
|
|
|
|
album = @"";
|
|
|
|
artist = @"";
|
|
|
|
title = @"";
|
|
|
|
[self updateMetadata];
|
|
|
|
|
2013-10-05 06:01:33 -03:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2022-02-09 18:44:50 -03:00
|
|
|
- (void)updateMetadata {
|
|
|
|
const OpusTags *comment = op_tags(opusRef, -1);
|
|
|
|
|
|
|
|
if(comment) {
|
|
|
|
uint8_t nullByte = '\0';
|
|
|
|
NSString *_genre = genre;
|
|
|
|
NSString *_album = album;
|
|
|
|
NSString *_artist = artist;
|
|
|
|
NSString *_title = title;
|
|
|
|
for(int i = 0; i < comment->comments; ++i) {
|
|
|
|
NSMutableData *commentField = [NSMutableData dataWithBytes:comment->user_comments[i] length:comment->comment_lengths[i]];
|
|
|
|
[commentField appendBytes:&nullByte length:1];
|
|
|
|
NSString *commentString = [NSString stringWithUTF8String:[commentField bytes]];
|
|
|
|
NSArray *splitFields = [commentString componentsSeparatedByString:@"="];
|
|
|
|
if([splitFields count] == 2) {
|
|
|
|
NSString *name = [splitFields objectAtIndex:0];
|
|
|
|
NSString *value = [splitFields objectAtIndex:1];
|
|
|
|
name = [name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
|
|
|
value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
|
|
|
name = [name lowercaseString];
|
|
|
|
if([name isEqualToString:@"genre"]) {
|
|
|
|
_genre = value;
|
|
|
|
} else if([name isEqualToString:@"album"]) {
|
|
|
|
_album = value;
|
|
|
|
} else if([name isEqualToString:@"artist"]) {
|
|
|
|
_artist = value;
|
|
|
|
} else if([name isEqualToString:@"title"]) {
|
|
|
|
_title = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 20:04:49 -03:00
|
|
|
if(![source seekable] &&
|
|
|
|
(![_genre isEqual:genre] ||
|
|
|
|
![_album isEqual:album] ||
|
|
|
|
![_artist isEqual:artist] ||
|
|
|
|
![_title isEqual:title])) {
|
2022-02-09 18:44:50 -03:00
|
|
|
genre = _genre;
|
|
|
|
album = _album;
|
|
|
|
artist = _artist;
|
|
|
|
title = _title;
|
|
|
|
[self willChangeValueForKey:@"metadata"];
|
|
|
|
[self didChangeValueForKey:@"metadata"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateIcyMetadata {
|
2022-02-09 20:04:49 -03:00
|
|
|
if([source seekable]) return;
|
|
|
|
|
2022-02-09 18:44:50 -03:00
|
|
|
NSString *_genre = genre;
|
|
|
|
NSString *_album = album;
|
|
|
|
NSString *_artist = artist;
|
|
|
|
NSString *_title = title;
|
|
|
|
|
|
|
|
Class sourceClass = [source class];
|
|
|
|
if([sourceClass isEqual:NSClassFromString(@"HTTPSource")]) {
|
|
|
|
HTTPSource *httpSource = (HTTPSource *)source;
|
|
|
|
if([httpSource hasMetadata]) {
|
|
|
|
NSDictionary *metadata = [httpSource metadata];
|
|
|
|
_genre = [metadata valueForKey:@"genre"];
|
|
|
|
_album = [metadata valueForKey:@"album"];
|
|
|
|
_artist = [metadata valueForKey:@"artist"];
|
|
|
|
_title = [metadata valueForKey:@"title"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(![_genre isEqual:genre] ||
|
|
|
|
![_album isEqual:album] ||
|
|
|
|
![_artist isEqual:artist] ||
|
|
|
|
![_title isEqual:title]) {
|
|
|
|
genre = _genre;
|
|
|
|
album = _album;
|
|
|
|
artist = _artist;
|
|
|
|
title = _title;
|
|
|
|
[self willChangeValueForKey:@"metadata"];
|
|
|
|
[self didChangeValueForKey:@"metadata"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (int)readAudio:(void *)buf frames:(UInt32)frames {
|
2013-10-05 06:01:33 -03:00
|
|
|
int numread;
|
|
|
|
int total = 0;
|
2022-02-07 02:49:27 -03:00
|
|
|
|
|
|
|
if(currentSection != lastSection) {
|
|
|
|
bitrate = (op_bitrate(opusRef, currentSection) / 1000.0);
|
2013-10-05 06:01:33 -03:00
|
|
|
channels = op_channel_count(opusRef, currentSection);
|
2022-02-07 02:49:27 -03:00
|
|
|
|
2013-10-05 06:01:33 -03:00
|
|
|
[self willChangeValueForKey:@"properties"];
|
|
|
|
[self didChangeValueForKey:@"properties"];
|
2022-02-09 18:44:50 -03:00
|
|
|
|
|
|
|
[self updateMetadata];
|
2013-10-05 06:01:33 -03:00
|
|
|
}
|
2022-02-07 02:49:27 -03:00
|
|
|
|
|
|
|
int size = frames * channels;
|
|
|
|
|
|
|
|
do {
|
|
|
|
float *out = ((float *)buf) + total;
|
|
|
|
float tempbuf[512 * channels];
|
2013-10-05 06:01:33 -03:00
|
|
|
lastSection = currentSection;
|
2022-02-07 02:49:27 -03:00
|
|
|
int toread = size - total;
|
|
|
|
if(toread > 512) toread = 512;
|
|
|
|
numread = op_read_float(opusRef, (channels < MAXCHANNELS) ? tempbuf : out, toread, NULL);
|
|
|
|
if(numread > 0 && channels <= MAXCHANNELS) {
|
|
|
|
for(int i = 0; i < numread; ++i) {
|
|
|
|
for(int j = 0; j < channels; ++j) {
|
|
|
|
out[i * channels + j] = tempbuf[i * channels + chmap[channels - 1][j]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
currentSection = op_current_link(opusRef);
|
|
|
|
if(numread > 0) {
|
2013-10-05 18:15:09 -03:00
|
|
|
total += numread * channels;
|
2013-10-05 06:01:33 -03:00
|
|
|
}
|
2022-02-07 02:49:27 -03:00
|
|
|
|
|
|
|
if(currentSection != lastSection) {
|
2013-10-05 06:01:33 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
} while(total != size && numread != 0);
|
|
|
|
|
2022-02-09 18:44:50 -03:00
|
|
|
[self updateIcyMetadata];
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
return total / channels;
|
2013-10-05 06:01:33 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)close {
|
|
|
|
op_free(opusRef);
|
|
|
|
opusRef = NULL;
|
2016-06-19 15:57:18 -04:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)dealloc {
|
|
|
|
[self close];
|
2013-10-05 06:01:33 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (long)seek:(long)frame {
|
|
|
|
op_pcm_seek(opusRef, frame);
|
|
|
|
|
2013-10-05 06:01:33 -03:00
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (NSDictionary *)properties {
|
2022-02-09 00:42:03 -03:00
|
|
|
return @{@"channels": [NSNumber numberWithInt:channels],
|
|
|
|
@"bitsPerSample": [NSNumber numberWithInt:32],
|
|
|
|
@"floatingPoint": [NSNumber numberWithBool:YES],
|
|
|
|
@"sampleRate": [NSNumber numberWithFloat:48000],
|
|
|
|
@"totalFrames": [NSNumber numberWithDouble:totalFrames],
|
|
|
|
@"bitrate": [NSNumber numberWithInt:bitrate],
|
|
|
|
@"seekable": [NSNumber numberWithBool:([source seekable] && seekable)],
|
|
|
|
@"codec": @"Opus",
|
|
|
|
@"endian": @"host",
|
|
|
|
@"encoding": @"lossy"};
|
2013-10-05 06:01:33 -03:00
|
|
|
}
|
|
|
|
|
2022-02-09 00:56:39 -03:00
|
|
|
- (NSDictionary *)metadata {
|
2022-02-09 18:44:50 -03:00
|
|
|
return @{ @"genre": genre, @"album": album, @"artist": artist, @"title": title };
|
2022-02-09 00:56:39 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
+ (NSArray *)fileTypes {
|
|
|
|
return @[@"opus", @"ogg"];
|
2013-10-05 06:01:33 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
+ (NSArray *)mimeTypes {
|
2022-01-18 23:12:57 -03:00
|
|
|
return @[@"audio/x-opus+ogg", @"application/ogg"];
|
2013-10-05 06:01:33 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
+ (float)priority {
|
|
|
|
return 1.0;
|
Implemented support for multiple decoders per file name extension, with a floating point priority control per interface. In the event that more than one input is registered to a given extension, and we match that extension, it will be passed off to an instance of the multi-decoder wrapper, which will try opening the file with all of the decoders in order of priority, until either one of them accepts it, or all of them have failed. This paves the way for adding a VGMSTREAM input, so I can give it a very low priority, since it has several formats that are verified by file name extension only. All current inputs have been given a priority of 1.0, except for CoreAudio, which was given a priority of 0.5, because it contains an MP3 and AC3 decoders that I'd rather not use if I don't have to.
2013-10-21 14:54:11 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
+ (NSArray *)fileTypeAssociations {
|
|
|
|
return @[
|
|
|
|
@[@"Opus Audio File", @"ogg.icns", @"opus"],
|
|
|
|
@[@"Ogg Audio File", @"ogg.icns", @"ogg"]
|
|
|
|
];
|
2022-01-18 08:06:03 -03:00
|
|
|
}
|
|
|
|
|
2013-10-05 06:01:33 -03:00
|
|
|
@end
|