2013-10-05 09:01:33 +00:00
|
|
|
//
|
|
|
|
// OpusDecoder.m
|
|
|
|
// Opus
|
|
|
|
//
|
|
|
|
// Created by Christopher Snowhill on 10/4/13.
|
|
|
|
// Copyright 2013 __NoWork, Inc__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2013-10-05 21:15:09 +00:00
|
|
|
#import "Plugin.h"
|
|
|
|
|
2013-10-05 09:01:33 +00:00
|
|
|
#import "OpusDecoder.h"
|
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
2013-10-05 09:01:33 +00:00
|
|
|
|
|
|
|
@implementation OpusFile
|
|
|
|
|
|
|
|
int sourceRead(void *_stream, unsigned char *_ptr, int _nbytes)
|
|
|
|
{
|
2016-06-19 19:57:18 +00:00
|
|
|
id source = (__bridge id)_stream;
|
2013-10-05 09:01:33 +00:00
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
return (int) [source read:_ptr amount:_nbytes];
|
2013-10-05 09:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int sourceSeek(void *_stream, opus_int64 _offset, int _whence)
|
|
|
|
{
|
2016-06-19 19:57:18 +00:00
|
|
|
id source = (__bridge id)_stream;
|
2013-10-05 09:01:33 +00:00
|
|
|
return ([source seek:_offset whence:_whence] ? 0 : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sourceClose(void *_stream)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
opus_int64 sourceTell(void *_stream)
|
|
|
|
{
|
2016-06-19 19:57:18 +00:00
|
|
|
id source = (__bridge id)_stream;
|
2013-10-05 09:01:33 +00:00
|
|
|
|
|
|
|
return [source tell];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
opusRef = NULL;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)open:(id<CogSource>)s
|
|
|
|
{
|
2016-06-19 19:57:18 +00:00
|
|
|
source = s;
|
2013-10-05 09:01:33 +00:00
|
|
|
|
|
|
|
OpusFileCallbacks callbacks = {
|
|
|
|
.read = sourceRead,
|
|
|
|
.seek = sourceSeek,
|
|
|
|
.close = sourceClose,
|
|
|
|
.tell = sourceTell
|
|
|
|
};
|
|
|
|
|
|
|
|
int error;
|
2016-06-19 19:57:18 +00:00
|
|
|
opusRef = op_open_callbacks((__bridge void *)source, &callbacks, NULL, 0, &error);
|
2013-10-05 09:01:33 +00:00
|
|
|
|
|
|
|
if (!opusRef)
|
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"FAILED TO OPEN OPUS FILE");
|
2013-10-05 09:01:33 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentSection = lastSection = op_current_link( opusRef );
|
|
|
|
|
|
|
|
bitrate = (op_bitrate(opusRef, currentSection )/1000.0);
|
|
|
|
channels = op_channel_count( opusRef, currentSection );
|
|
|
|
|
|
|
|
seekable = op_seekable(opusRef);
|
|
|
|
|
|
|
|
totalFrames = op_pcm_total(opusRef, -1);
|
|
|
|
|
|
|
|
[self willChangeValueForKey:@"properties"];
|
|
|
|
[self didChangeValueForKey:@"properties"];
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int)readAudio:(void *)buf frames:(UInt32)frames
|
|
|
|
{
|
|
|
|
int numread;
|
|
|
|
int total = 0;
|
|
|
|
|
|
|
|
if (currentSection != lastSection) {
|
|
|
|
bitrate = (op_bitrate(opusRef, currentSection)/1000.0);
|
|
|
|
channels = op_channel_count(opusRef, currentSection);
|
|
|
|
|
|
|
|
[self willChangeValueForKey:@"properties"];
|
|
|
|
[self didChangeValueForKey:@"properties"];
|
|
|
|
}
|
|
|
|
|
2013-10-05 21:15:09 +00:00
|
|
|
int size = frames * channels;
|
2013-10-05 09:01:33 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
lastSection = currentSection;
|
2015-12-22 08:39:00 +00:00
|
|
|
numread = op_read_float( opusRef, &((float *)buf)[total], size - total, NULL );
|
2013-10-05 09:01:33 +00:00
|
|
|
currentSection = op_current_link( opusRef );
|
|
|
|
if (numread > 0) {
|
2013-10-05 21:15:09 +00:00
|
|
|
total += numread * channels;
|
2013-10-05 09:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (currentSection != lastSection) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (total != frames && numread != 0);
|
|
|
|
|
2013-10-05 21:15:09 +00:00
|
|
|
return total/channels;
|
2013-10-05 09:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)close
|
|
|
|
{
|
|
|
|
op_free(opusRef);
|
|
|
|
opusRef = NULL;
|
2016-06-19 19:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[self close];
|
2013-10-05 09:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (long)seek:(long)frame
|
|
|
|
{
|
|
|
|
op_pcm_seek(opusRef, frame);
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *)properties
|
|
|
|
{
|
|
|
|
return [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
[NSNumber numberWithInt:channels], @"channels",
|
2013-10-05 21:15:09 +00:00
|
|
|
[NSNumber numberWithInt:32], @"bitsPerSample",
|
|
|
|
[NSNumber numberWithBool:YES], @"floatingPoint",
|
2013-10-05 09:01:33 +00:00
|
|
|
[NSNumber numberWithFloat:48000], @"sampleRate",
|
|
|
|
[NSNumber numberWithDouble:totalFrames], @"totalFrames",
|
|
|
|
[NSNumber numberWithInt:bitrate], @"bitrate",
|
|
|
|
[NSNumber numberWithBool:([source seekable] && seekable)], @"seekable",
|
2021-10-02 02:18:42 +00:00
|
|
|
@"Opus", @"codec",
|
2013-10-05 21:15:09 +00:00
|
|
|
@"host", @"endian",
|
2013-10-05 09:01:33 +00:00
|
|
|
nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (NSArray *)fileTypes
|
|
|
|
{
|
2020-11-19 09:37:50 +00:00
|
|
|
return [NSArray arrayWithObjects:@"opus",@"ogg",nil];
|
2013-10-05 09:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSArray *)mimeTypes
|
|
|
|
{
|
|
|
|
return [NSArray arrayWithObjects:@"audio/x-opus+ogg", nil];
|
|
|
|
}
|
|
|
|
|
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 17:54:11 +00:00
|
|
|
+ (float)priority
|
|
|
|
{
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
2013-10-05 09:01:33 +00:00
|
|
|
@end
|