forked from Blendman974/faceclaw
iOS TTF support; voice input
This commit is contained in:
1 parent
ae14edcb59
commit
b07ae0b479
67 files changed
+15717
-55
No files matched your search
@@ -0,0 +1,15 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
/** Core Text rasterizer. All sizes are glasses pixels, independent of screen scale.
|
||||
* Each operation owns its layout/context, so main and worker isolates may call it.
|
||||
* Packets match Android FontFileRenderer: grayscale image or 10-byte glyph header.
|
||||
*/
|
||||
@interface FaceclawFontRenderer : NSObject
|
||||
+ (BOOL)canLoadFont:(NSString *)path;
|
||||
+ (NSString *)getFontName:(NSString *)path;
|
||||
+ (NSString *)getFontMetrics:(NSString *)path size:(double)size;
|
||||
+ (double)measureTextExact:(NSString *)path text:(NSString *)text size:(double)size;
|
||||
+ (NSData *)renderGlyphCell:(NSString *)path size:(double)size codePoint:(NSInteger)codePoint gamma:(double)gamma;
|
||||
+ (NSData *)renderText:(NSString *)path text:(NSString *)text size:(double)size gamma:(double)gamma;
|
||||
+ (NSData *)renderWrapped:(NSString *)path text:(NSString *)text size:(double)size width:(NSInteger)width gamma:(double)gamma maxLines:(NSInteger)maxLines;
|
||||
@end
|
||||
@@ -0,0 +1,189 @@
|
||||
#import "FaceclawFontRenderer.h"
|
||||
#import <CoreText/CoreText.h>
|
||||
#import <CoreGraphics/CoreGraphics.h>
|
||||
#import <math.h>
|
||||
|
||||
// Immutable font descriptors may be shared; layout objects never cross threads.
|
||||
static CTFontRef CreateFont(NSString *path, double size) CF_RETURNS_RETAINED {
|
||||
if (!path.length || !isfinite(size) || size <= 0 || size > 512) return NULL;
|
||||
static NSCache *cache;
|
||||
static dispatch_once_t once;
|
||||
dispatch_once(&once, ^{ cache = [NSCache new]; cache.countLimit = 32; });
|
||||
NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
|
||||
if (![attrs[NSFileType] isEqual:NSFileTypeRegular]) return NULL;
|
||||
NSString *key = [NSString stringWithFormat:@"%@:%@:%@", path, attrs[NSFileSize], attrs[NSFileModificationDate]];
|
||||
id descriptor = [cache objectForKey:key];
|
||||
if (!descriptor) {
|
||||
NSArray *descriptors = CFBridgingRelease(CTFontManagerCreateFontDescriptorsFromURL((__bridge CFURLRef)[NSURL fileURLWithPath:path]));
|
||||
descriptor = descriptors.firstObject; // TTC: first face, matching Android.
|
||||
if (!descriptor) return NULL; // Never silently substitute a system font for an invalid file.
|
||||
[cache setObject:descriptor forKey:key];
|
||||
}
|
||||
return CTFontCreateWithFontDescriptor((__bridge CTFontDescriptorRef)descriptor, size, NULL);
|
||||
}
|
||||
static NSAttributedString *Attributed(CTFontRef font, NSString *text, BOOL glyphLayout) {
|
||||
NSMutableDictionary *attrs = [@{(__bridge id)kCTFontAttributeName: (__bridge id)font,
|
||||
(__bridge id)kCTForegroundColorFromContextAttributeName: @YES} mutableCopy];
|
||||
// Shared UI lays out codepoints with pair kerning. Whole preview paragraphs
|
||||
// can use normal ligatures/shaping; UI measurements must match glyph cells.
|
||||
if (glyphLayout) attrs[(__bridge id)kCTLigatureAttributeName] = @0;
|
||||
return [[NSAttributedString alloc] initWithString:text attributes:attrs];
|
||||
}
|
||||
static BOOL ValidText(NSString *text) { return text != nil && text.length <= 100000; }
|
||||
static BOOL ValidRaster(NSInteger width, NSInteger height) {
|
||||
return width > 0 && height > 0 && width <= 65535 && height <= 65535 && width * height <= 4000000;
|
||||
}
|
||||
static CGContextRef CreateContext(NSMutableData *pixels, NSInteger width, NSInteger height) CF_RETURNS_RETAINED {
|
||||
CGContextRef ctx = CGBitmapContextCreate(pixels.mutableBytes, width, height, 8, width, NULL, (CGBitmapInfo)kCGImageAlphaOnly);
|
||||
if (ctx) {
|
||||
CGContextSetShouldAntialias(ctx, true);
|
||||
CGContextSetShouldSmoothFonts(ctx, false); // Grayscale coverage, never LCD subpixel color.
|
||||
CGContextSetShouldSubpixelPositionFonts(ctx, true);
|
||||
CGContextSetShouldSubpixelQuantizeFonts(ctx, false);
|
||||
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
|
||||
CGContextSetGrayFillColor(ctx, 1, 1);
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
static void U16(uint8_t *out, NSInteger value) { out[0] = value & 255; out[1] = (value >> 8) & 255; }
|
||||
static void GammaCopy(uint8_t *dst, const uint8_t *src, NSInteger count, double gamma) {
|
||||
if (!isfinite(gamma) || gamma <= 0) gamma = 1;
|
||||
uint8_t lut[256];
|
||||
for (int i = 0; i < 256; i++) lut[i] = (uint8_t)lround(255 * pow(i / 255.0, gamma));
|
||||
for (NSInteger i = 0; i < count; i++) dst[i] = lut[src[i]];
|
||||
}
|
||||
static NSData *ImagePacket(NSData *pixels, NSInteger width, NSInteger height, double gamma) {
|
||||
NSMutableData *out = [NSMutableData dataWithLength:4 + pixels.length];
|
||||
uint8_t *dst = out.mutableBytes;
|
||||
U16(dst, width); U16(dst + 2, height);
|
||||
GammaCopy(dst + 4, pixels.bytes, pixels.length, gamma);
|
||||
return out;
|
||||
}
|
||||
|
||||
@implementation FaceclawFontRenderer
|
||||
+ (BOOL)canLoadFont:(NSString *)path {
|
||||
CTFontRef font = CreateFont(path, 16);
|
||||
if (!font) return NO;
|
||||
CFRelease(font); return YES;
|
||||
}
|
||||
+ (NSString *)getFontName:(NSString *)path {
|
||||
CTFontRef font = CreateFont(path, 16);
|
||||
if (!font) return @"";
|
||||
NSString *family = CFBridgingRelease(CTFontCopyFamilyName(font));
|
||||
NSString *style = CFBridgingRelease(CTFontCopyName(font, kCTFontStyleNameKey));
|
||||
CFRelease(font);
|
||||
return [NSString stringWithFormat:@"%@\n%@", family ?: @"", style ?: @""];
|
||||
}
|
||||
+ (NSString *)getFontMetrics:(NSString *)path size:(double)size {
|
||||
CTFontRef font = CreateFont(path, size);
|
||||
if (!font) return @"";
|
||||
NSString *metrics = [NSString stringWithFormat:@"%ld %ld %ld", (long)ceil(CTFontGetAscent(font)),
|
||||
(long)ceil(CTFontGetDescent(font)), (long)ceil(MAX(0, CTFontGetLeading(font)))];
|
||||
CFRelease(font); return metrics;
|
||||
}
|
||||
+ (double)measureTextExact:(NSString *)path text:(NSString *)text size:(double)size {
|
||||
if (!ValidText(text)) return 0;
|
||||
CTFontRef font = CreateFont(path, size);
|
||||
if (!font) return 0;
|
||||
CTLineRef line = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)Attributed(font, text, YES));
|
||||
double width = CTLineGetTypographicBounds(line, NULL, NULL, NULL);
|
||||
CFRelease(line); CFRelease(font); return width;
|
||||
}
|
||||
+ (NSData *)renderGlyphCell:(NSString *)path size:(double)size codePoint:(NSInteger)cp gamma:(double)gamma {
|
||||
if (cp < 0 || cp > 0x10ffff || (cp >= 0xd800 && cp <= 0xdfff)) return [NSData data];
|
||||
CTFontRef font = CreateFont(path, size);
|
||||
if (!font) return [NSData data];
|
||||
uint32_t scalar = (uint32_t)cp;
|
||||
NSString *text = [[NSString alloc] initWithBytes:&scalar length:4 encoding:NSUTF32LittleEndianStringEncoding];
|
||||
CTLineRef line = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)Attributed(font, text, YES));
|
||||
double advance = CTLineGetTypographicBounds(line, NULL, NULL, NULL);
|
||||
NSMutableData *out = [NSMutableData dataWithLength:10];
|
||||
U16(out.mutableBytes, MAX(0, MIN(65535, lround(advance * 64))));
|
||||
CGRect bounds = CTLineGetImageBounds(line, NULL);
|
||||
if (!CGRectIsEmpty(bounds) && !CGRectIsNull(bounds)) {
|
||||
NSInteger left = floor(CGRectGetMinX(bounds)) - 2, top = ceil(CGRectGetMaxY(bounds)) + 2;
|
||||
NSInteger bottom = floor(CGRectGetMinY(bounds)) - 2;
|
||||
NSInteger width = ceil(CGRectGetMaxX(bounds)) + 2 - left, height = top - bottom;
|
||||
if (!ValidRaster(width, height)) { CFRelease(line); CFRelease(font); return [NSData data]; }
|
||||
NSMutableData *pixels = [NSMutableData dataWithLength:width * height];
|
||||
CGContextRef ctx = CreateContext(pixels, width, height);
|
||||
if (!ctx) { CFRelease(line); CFRelease(font); return [NSData data]; }
|
||||
CGContextSetTextPosition(ctx, -left, -bottom); CTLineDraw(line, ctx); CGContextRelease(ctx);
|
||||
const uint8_t *src = pixels.bytes;
|
||||
NSInteger x0 = width, y0 = height, x1 = -1, y1 = -1;
|
||||
for (NSInteger y = 0; y < height; y++) for (NSInteger x = 0; x < width; x++) if (src[y * width + x]) {
|
||||
x0 = MIN(x0, x); x1 = MAX(x1, x); y0 = MIN(y0, y); y1 = MAX(y1, y);
|
||||
}
|
||||
if (x1 >= x0) {
|
||||
NSInteger w = x1 - x0 + 1, h = y1 - y0 + 1;
|
||||
[out setLength:10 + w * h]; uint8_t *dst = out.mutableBytes;
|
||||
U16(dst + 2, left + x0); U16(dst + 4, ceil(CTFontGetAscent(font)) - top + y0);
|
||||
U16(dst + 6, w); U16(dst + 8, h);
|
||||
for (NSInteger y = 0; y < h; y++) GammaCopy(dst + 10 + y * w, src + (y0 + y) * width + x0, w, gamma);
|
||||
}
|
||||
}
|
||||
CFRelease(line); CFRelease(font); return out;
|
||||
}
|
||||
+ (NSData *)renderText:(NSString *)path text:(NSString *)text size:(double)size gamma:(double)gamma {
|
||||
if (!ValidText(text)) return [NSData data];
|
||||
CTFontRef font = CreateFont(path, size);
|
||||
if (!font) return [NSData data];
|
||||
CTLineRef line = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)Attributed(font, text, NO));
|
||||
CGRect bounds = CTLineGetImageBounds(line, NULL);
|
||||
BOOL ink = !CGRectIsNull(bounds) && !CGRectIsEmpty(bounds);
|
||||
NSInteger left = ink ? MIN(0, floor(CGRectGetMinX(bounds))) : 0;
|
||||
NSInteger right = MAX(1, ceil(CTLineGetTypographicBounds(line, NULL, NULL, NULL)));
|
||||
NSInteger top = ceil(CTFontGetAscent(font)), bottom = -ceil(CTFontGetDescent(font));
|
||||
if (ink) { right = MAX(right, ceil(CGRectGetMaxX(bounds))); top = MAX(top, ceil(CGRectGetMaxY(bounds))); bottom = MIN(bottom, floor(CGRectGetMinY(bounds))); }
|
||||
NSInteger width = right - left, height = top - bottom;
|
||||
NSData *result = [NSData data];
|
||||
if (ValidRaster(width, height)) {
|
||||
NSMutableData *pixels = [NSMutableData dataWithLength:width * height];
|
||||
CGContextRef ctx = CreateContext(pixels, width, height);
|
||||
if (ctx) {
|
||||
CGContextSetTextPosition(ctx, -left, -bottom); CTLineDraw(line, ctx); CGContextRelease(ctx);
|
||||
result = ImagePacket(pixels, width, height, gamma);
|
||||
}
|
||||
}
|
||||
CFRelease(line); CFRelease(font); return result;
|
||||
}
|
||||
+ (NSData *)renderWrapped:(NSString *)path text:(NSString *)text size:(double)size width:(NSInteger)width gamma:(double)gamma maxLines:(NSInteger)maxLines {
|
||||
if (!ValidText(text) || width <= 0 || width > 65535 || maxLines <= 0) return [NSData data];
|
||||
CTFontRef font = CreateFont(path, size);
|
||||
if (!font) return [NSData data];
|
||||
NSInteger ascent = ceil(CTFontGetAscent(font)), descent = ceil(CTFontGetDescent(font));
|
||||
NSInteger step = MAX(1, ascent + descent + (NSInteger)ceil(MAX(0, CTFontGetLeading(font))));
|
||||
maxLines = MIN(maxLines, MIN(65535 / step, 4000000 / width / step));
|
||||
if (maxLines <= 0) { CFRelease(font); return [NSData data]; }
|
||||
NSAttributedString *attributed = Attributed(font, text, NO);
|
||||
CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attributed);
|
||||
NSMutableArray *lines = [NSMutableArray new];
|
||||
CFIndex start = 0;
|
||||
while (start < (CFIndex)text.length && lines.count < (NSUInteger)maxLines) {
|
||||
CFIndex count = CTTypesetterSuggestLineBreak(typesetter, start, width);
|
||||
if (count <= 0) count = [text rangeOfComposedCharacterSequenceAtIndex:start].length;
|
||||
CTLineRef line;
|
||||
if (lines.count == (NSUInteger)maxLines - 1 && start + count < (CFIndex)text.length) {
|
||||
NSString *rest = [[text substringFromIndex:start] stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
|
||||
CTLineRef full = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)Attributed(font, rest, NO));
|
||||
CTLineRef token = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)Attributed(font, @"…", NO));
|
||||
line = CTLineCreateTruncatedLine(full, width, kCTLineTruncationEnd, token);
|
||||
if (!line) line = CFRetain(token);
|
||||
CFRelease(full); CFRelease(token);
|
||||
} else line = CTTypesetterCreateLine(typesetter, CFRangeMake(start, count));
|
||||
[lines addObject:CFBridgingRelease(line)]; start += count;
|
||||
}
|
||||
NSInteger height = MAX(1, lines.count) * step;
|
||||
NSMutableData *pixels = [NSMutableData dataWithLength:width * height];
|
||||
CGContextRef ctx = CreateContext(pixels, width, height);
|
||||
NSData *result = [NSData data];
|
||||
if (ctx) {
|
||||
for (NSUInteger i = 0; i < lines.count; i++) {
|
||||
CGContextSetTextPosition(ctx, 0, height - ascent - i * step);
|
||||
CTLineDraw((__bridge CTLineRef)lines[i], ctx);
|
||||
}
|
||||
CGContextRelease(ctx); result = ImagePacket(pixels, width, height, gamma);
|
||||
}
|
||||
CFRelease(typesetter); CFRelease(font); return result;
|
||||
}
|
||||
@end
|
||||
@@ -0,0 +1,11 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
/** One decoder per capture. Stock G2 packets: 5x40 LC3 bytes, DSP trailer,
|
||||
* counter. Output: 800 mono PCM16LE samples at 16 kHz (50 ms).
|
||||
*/
|
||||
@interface FaceclawLc3Decoder : NSObject
|
||||
- (NSData *)decodePacket:(NSData *)packet;
|
||||
@property(nonatomic, readonly) NSUInteger packets;
|
||||
@property(nonatomic, readonly) NSUInteger duplicates;
|
||||
@property(nonatomic, readonly) NSUInteger missing;
|
||||
@property(nonatomic, readonly) NSUInteger errors;
|
||||
@end
|
||||
@@ -0,0 +1,42 @@
|
||||
#import "FaceclawLc3Decoder.h"
|
||||
#include "lc3/lc3.h"
|
||||
#include <stdlib.h>
|
||||
@interface FaceclawLc3Decoder () {
|
||||
void *_memory;
|
||||
lc3_decoder_t _decoder;
|
||||
int _lastCounter;
|
||||
}
|
||||
@property(nonatomic) NSUInteger packets, duplicates, missing, errors;
|
||||
@end
|
||||
@implementation FaceclawLc3Decoder
|
||||
- (instancetype)init {
|
||||
if ((self = [super init])) {
|
||||
_lastCounter = -1;
|
||||
_memory = calloc(1, lc3_decoder_size(10000, 16000));
|
||||
if (!_memory) return nil;
|
||||
_decoder = lc3_setup_decoder(10000, 16000, 0, _memory);
|
||||
if (!_decoder) return nil;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
- (void)dealloc { free(_memory); }
|
||||
- (NSData *)decodePacket:(NSData *)packet {
|
||||
@synchronized(self) {
|
||||
if (packet.length != 205) { _errors++; return nil; }
|
||||
const uint8_t *bytes = packet.bytes;
|
||||
int counter = bytes[204], gap = _lastCounter < 0 ? 1 : (counter - _lastCounter) & 255;
|
||||
// Both arms may relay the same packet. Drop duplicates/late copies,
|
||||
// including across the 255 -> 0 wrap, before touching decoder history.
|
||||
if (gap == 0 || gap >= 128) { _duplicates++; return nil; }
|
||||
_missing += gap - 1;
|
||||
_lastCounter = counter;
|
||||
NSMutableData *out = [NSMutableData dataWithLength:800 * sizeof(int16_t)];
|
||||
int16_t *pcm = out.mutableBytes;
|
||||
for (int frame = 0; frame < 5; frame++) {
|
||||
int status = lc3_decode(_decoder, bytes + frame * 40, 40, LC3_PCM_FORMAT_S16, pcm + frame * 160, 1);
|
||||
if (status < 0) { _errors++; return nil; }
|
||||
}
|
||||
_packets++; return out;
|
||||
}
|
||||
}
|
||||
@end
|
||||
@@ -0,0 +1,12 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
/** On-device speech recognition fed ONLY by decoded G2 BLE microphone packets. */
|
||||
@interface FaceclawSpeech : NSObject
|
||||
@property(nonatomic, copy) void (^eventHandler)(NSString *json);
|
||||
+ (NSInteger)authorizationStatus;
|
||||
+ (void)requestAuthorization:(void (^)(NSInteger status))completion;
|
||||
/** Returns an actionable error, or an empty string when capture starts. */
|
||||
- (NSString *)start;
|
||||
- (void)acceptPacket:(NSData *)packet;
|
||||
- (void)finish;
|
||||
- (void)cancel;
|
||||
@end
|
||||
@@ -0,0 +1,136 @@
|
||||
#import "FaceclawSpeech.h"
|
||||
#import "FaceclawLc3Decoder.h"
|
||||
#import <Speech/Speech.h>
|
||||
#import <AVFoundation/AVFoundation.h>
|
||||
#import <math.h>
|
||||
|
||||
@interface FaceclawSpeech ()
|
||||
@property(nonatomic, strong) SFSpeechRecognizer *recognizer;
|
||||
@property(nonatomic, strong) SFSpeechAudioBufferRecognitionRequest *request;
|
||||
@property(nonatomic, strong) SFSpeechRecognitionTask *task;
|
||||
@property(nonatomic, strong) FaceclawLc3Decoder *decoder;
|
||||
@property(nonatomic, strong) dispatch_queue_t audioQueue;
|
||||
@property(atomic) NSUInteger generation;
|
||||
@property(nonatomic) NSUInteger pendingPackets;
|
||||
@property(nonatomic) BOOL accepting;
|
||||
@property(nonatomic, copy) NSString *bestText;
|
||||
@end
|
||||
@implementation FaceclawSpeech
|
||||
- (instancetype)init {
|
||||
if ((self = [super init])) _audioQueue = dispatch_queue_create("com.faceclaw.glasses-speech", DISPATCH_QUEUE_SERIAL);
|
||||
return self;
|
||||
}
|
||||
+ (NSInteger)authorizationStatus { return SFSpeechRecognizer.authorizationStatus; }
|
||||
+ (void)requestAuthorization:(void (^)(NSInteger))completion {
|
||||
[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{ completion(status); });
|
||||
}];
|
||||
}
|
||||
- (void)emit:(NSDictionary *)event {
|
||||
if (!self.eventHandler) return;
|
||||
NSData *json = [NSJSONSerialization dataWithJSONObject:event options:0 error:nil];
|
||||
if (json) self.eventHandler([[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding]);
|
||||
}
|
||||
- (NSString *)start {
|
||||
[self cancel];
|
||||
if (SFSpeechRecognizer.authorizationStatus != SFSpeechRecognizerAuthorizationStatusAuthorized)
|
||||
return @"Allow Speech Recognition for Faceclaw in iPhone Settings.";
|
||||
NSString *language = NSLocale.preferredLanguages.firstObject ?: @"en-US";
|
||||
self.recognizer = [[SFSpeechRecognizer alloc] initWithLocale:[NSLocale localeWithLocaleIdentifier:language]];
|
||||
if (!self.recognizer.supportsOnDeviceRecognition)
|
||||
return [NSString stringWithFormat:@"On-device speech is unavailable for %@. Enable Dictation for that language in iPhone Settings → General → Keyboard, then retry.", language];
|
||||
if (!self.recognizer.available) return @"Speech recognition is temporarily unavailable. Try again shortly.";
|
||||
self.decoder = [FaceclawLc3Decoder new];
|
||||
if (!self.decoder) return @"Could not initialize the glasses audio decoder.";
|
||||
self.recognizer.queue = NSOperationQueue.mainQueue;
|
||||
self.request = [SFSpeechAudioBufferRecognitionRequest new];
|
||||
self.request.requiresOnDeviceRecognition = YES;
|
||||
self.request.shouldReportPartialResults = YES;
|
||||
self.request.taskHint = SFSpeechRecognitionTaskHintDictation;
|
||||
if (@available(iOS 16.0, *)) self.request.addsPunctuation = YES;
|
||||
self.bestText = @""; self.accepting = YES;
|
||||
NSUInteger generation = self.generation;
|
||||
__weak FaceclawSpeech *weakSelf = self;
|
||||
self.task = [self.recognizer recognitionTaskWithRequest:self.request resultHandler:^(SFSpeechRecognitionResult *result, NSError *error) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
FaceclawSpeech *owner = weakSelf;
|
||||
if (!owner || owner.generation != generation) return;
|
||||
if (result) {
|
||||
owner.bestText = result.bestTranscription.formattedString ?: @"";
|
||||
if (!result.final) [owner emit:@{@"kind":@"transcript", @"text":owner.bestText, @"final":@NO}];
|
||||
}
|
||||
if (result.final || error) {
|
||||
NSString *message = error ? [NSString stringWithFormat:@"Speech recognition: %@", error.localizedDescription] : @"Ready to send";
|
||||
[owner complete:message];
|
||||
}
|
||||
});
|
||||
}];
|
||||
[self emit:@{@"kind":@"status", @"message":[NSString stringWithFormat:@"Listening on glasses (%@, on-device)…", language]}];
|
||||
// Bound a dictation to the recognizer's supported short-session workflow.
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 55 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
|
||||
FaceclawSpeech *owner = weakSelf;
|
||||
if (owner && owner.generation == generation && owner.accepting) [owner finish];
|
||||
});
|
||||
return @"";
|
||||
}
|
||||
- (void)acceptPacket:(NSData *)packet {
|
||||
if (!self.accepting) return;
|
||||
if (self.pendingPackets >= 40) { [self complete:@"Audio processing fell behind. Please try again."]; return; }
|
||||
self.pendingPackets++;
|
||||
NSUInteger generation = self.generation;
|
||||
FaceclawLc3Decoder *decoder = self.decoder;
|
||||
SFSpeechAudioBufferRecognitionRequest *request = self.request;
|
||||
NSData *copy = [packet copy];
|
||||
dispatch_async(self.audioQueue, ^{ @autoreleasepool {
|
||||
if (self.generation != generation) return;
|
||||
NSData *pcm = [decoder decodePacket:copy];
|
||||
double rms = 0;
|
||||
if (pcm.length) {
|
||||
AVAudioFormat *format = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32 sampleRate:16000 channels:1 interleaved:NO];
|
||||
AVAudioPCMBuffer *buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:format frameCapacity:(AVAudioFrameCount)(pcm.length / 2)];
|
||||
buffer.frameLength = buffer.frameCapacity;
|
||||
const int16_t *samples = pcm.bytes;
|
||||
for (NSUInteger i = 0; i < buffer.frameLength; i++) {
|
||||
float value = samples[i] / 32768.0f;
|
||||
buffer.floatChannelData[0][i] = value; rms += value * value;
|
||||
}
|
||||
rms = sqrt(rms / buffer.frameLength);
|
||||
if (self.generation == generation) [request appendAudioPCMBuffer:buffer];
|
||||
}
|
||||
NSUInteger packets = decoder.packets, errors = decoder.errors, missing = decoder.missing;
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if (self.generation != generation) return;
|
||||
self.pendingPackets--;
|
||||
if (pcm.length && packets % 20 == 0) [self emit:@{@"kind":@"audio", @"packets":@(packets), @"seconds":@(packets * 0.05), @"rms":@(rms), @"missing":@(missing), @"errors":@(errors)}];
|
||||
if (errors > 10 && !packets) [self complete:@"Could not decode glasses audio. Reconnect the glasses and retry."];
|
||||
});
|
||||
}});
|
||||
}
|
||||
- (void)finish {
|
||||
if (!self.accepting) return;
|
||||
self.accepting = NO;
|
||||
NSUInteger generation = self.generation;
|
||||
SFSpeechAudioBufferRecognitionRequest *request = self.request;
|
||||
[self emit:@{@"kind":@"finishing"}];
|
||||
// Drain already received audio before marking the utterance complete.
|
||||
dispatch_async(self.audioQueue, ^{ if (self.generation == generation) [request endAudio]; });
|
||||
__weak FaceclawSpeech *weakSelf = self;
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 8 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
|
||||
FaceclawSpeech *owner = weakSelf;
|
||||
if (owner && owner.generation == generation) [owner complete:@"Ready to send"];
|
||||
});
|
||||
}
|
||||
- (void)complete:(NSString *)status {
|
||||
NSString *text = self.bestText ?: @"";
|
||||
[self cancel];
|
||||
NSUInteger generation = self.generation;
|
||||
if (text.length) [self emit:@{@"kind":@"transcript", @"text":text, @"final":@YES}];
|
||||
if (self.generation == generation) [self emit:@{@"kind":@"ended", @"message":status}];
|
||||
}
|
||||
- (void)cancel {
|
||||
self.generation++;
|
||||
self.accepting = NO; self.pendingPackets = 0;
|
||||
[self.task cancel]; self.task = nil; self.request = nil; self.decoder = nil; self.recognizer = nil;
|
||||
self.bestText = @"";
|
||||
}
|
||||
@end
|
||||
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -0,0 +1,7 @@
|
||||
Google liblc3 v1.1.3 (same release as Android app.gradle).
|
||||
Source: https://github.com/google/liblc3/releases/tag/v1.1.3
|
||||
Archive SHA-256: 276752ff54ce6a77d54ec133397b9d7e71f90caf3d9afa32d8b0e891b8ecb8af
|
||||
|
||||
Unmodified include/*.h, src/*.h and the 12 translation units compiled by
|
||||
Android. Licensed under Apache-2.0; see LICENSE. NativeScript compiles the
|
||||
C files into the iOS app. No generated binaries are checked in.
|
||||
@@ -0,0 +1,92 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#include "attdet.h"
|
||||
|
||||
|
||||
/**
|
||||
* Time domain attack detector
|
||||
*/
|
||||
bool lc3_attdet_run(enum lc3_dt dt, enum lc3_srate sr,
|
||||
int nbytes, struct lc3_attdet_analysis *attdet, const int16_t *x)
|
||||
{
|
||||
/* --- Check enabling --- */
|
||||
|
||||
const int nbytes_ranges[][LC3_NUM_SRATE - LC3_SRATE_32K][2] = {
|
||||
[LC3_DT_7M5 - LC3_DT_7M5] = { { 61, 149 }, { 75, 149 } },
|
||||
[LC3_DT_10M - LC3_DT_7M5] = { { 81, INT_MAX }, { 100, INT_MAX } },
|
||||
};
|
||||
|
||||
if (dt < LC3_DT_7M5 || sr < LC3_SRATE_32K || lc3_hr(sr) ||
|
||||
nbytes < nbytes_ranges[dt - LC3_DT_7M5][sr - LC3_SRATE_32K][0] ||
|
||||
nbytes > nbytes_ranges[dt - LC3_DT_7M5][sr - LC3_SRATE_32K][1] )
|
||||
return 0;
|
||||
|
||||
/* --- Filtering & Energy calculation --- */
|
||||
|
||||
int nblk = 4 - (dt == LC3_DT_7M5);
|
||||
int32_t e[4];
|
||||
|
||||
for (int i = 0; i < nblk; i++) {
|
||||
e[i] = 0;
|
||||
|
||||
if (sr == LC3_SRATE_32K) {
|
||||
int16_t xn2 = (x[-4] + x[-3]) >> 1;
|
||||
int16_t xn1 = (x[-2] + x[-1]) >> 1;
|
||||
int16_t xn, xf;
|
||||
|
||||
for (int j = 0; j < 40; j++, x += 2, xn2 = xn1, xn1 = xn) {
|
||||
xn = (x[0] + x[1]) >> 1;
|
||||
xf = (3 * xn - 4 * xn1 + 1 * xn2) >> 3;
|
||||
e[i] += (xf * xf) >> 5;
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
int16_t xn2 = (x[-6] + x[-5] + x[-4]) >> 2;
|
||||
int16_t xn1 = (x[-3] + x[-2] + x[-1]) >> 2;
|
||||
int16_t xn, xf;
|
||||
|
||||
for (int j = 0; j < 40; j++, x += 3, xn2 = xn1, xn1 = xn) {
|
||||
xn = (x[0] + x[1] + x[2]) >> 2;
|
||||
xf = (3 * xn - 4 * xn1 + 1 * xn2) >> 3;
|
||||
e[i] += (xf * xf) >> 5;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* --- Attack detection ---
|
||||
* The attack block `p_att` is defined as the normative value + 1,
|
||||
* in such way, it will be initialized to 0 */
|
||||
|
||||
int p_att = 0;
|
||||
int32_t a[4];
|
||||
|
||||
for (int i = 0; i < nblk; i++) {
|
||||
a[i] = LC3_MAX(attdet->an1 >> 2, attdet->en1);
|
||||
attdet->en1 = e[i], attdet->an1 = a[i];
|
||||
|
||||
if ((e[i] >> 3) > a[i] + (a[i] >> 4))
|
||||
p_att = i + 1;
|
||||
}
|
||||
|
||||
int att = attdet->p_att >= 1 + (nblk >> 1) || p_att > 0;
|
||||
attdet->p_att = p_att;
|
||||
|
||||
return att;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __LC3_ATTDET_H
|
||||
#define __LC3_ATTDET_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
||||
/**
|
||||
* Time domain attack detector
|
||||
* dt, sr Duration and samplerate of the frame
|
||||
* nbytes Size in bytes of the frame
|
||||
* attdet Context of the Attack Detector
|
||||
* x [-6..-1] Previous, [0..ns-1] Current samples
|
||||
* return 1: Attack detected 0: Otherwise
|
||||
*/
|
||||
bool lc3_attdet_run(enum lc3_dt dt, enum lc3_srate sr,
|
||||
int nbytes, lc3_attdet_analysis_t *attdet, const int16_t *x);
|
||||
|
||||
|
||||
#endif /* __LC3_ATTDET_H */
|
||||
@@ -0,0 +1,375 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#include "bits.h"
|
||||
#include "common.h"
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Common
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
static inline int ac_get(struct lc3_bits_buffer *);
|
||||
static inline void accu_load(struct lc3_bits_accu *, struct lc3_bits_buffer *);
|
||||
|
||||
/**
|
||||
* Arithmetic coder return range bits
|
||||
* ac Arithmetic coder
|
||||
* return 1 + log2(ac->range)
|
||||
*/
|
||||
static int ac_get_range_bits(const struct lc3_bits_ac *ac)
|
||||
{
|
||||
int nbits = 0;
|
||||
|
||||
for (unsigned r = ac->range; r; r >>= 1, nbits++);
|
||||
|
||||
return nbits;
|
||||
}
|
||||
|
||||
/**
|
||||
* Arithmetic coder return pending bits
|
||||
* ac Arithmetic coder
|
||||
* return Pending bits
|
||||
*/
|
||||
static int ac_get_pending_bits(const struct lc3_bits_ac *ac)
|
||||
{
|
||||
return 26 - ac_get_range_bits(ac) +
|
||||
((ac->cache >= 0) + ac->carry_count) * 8;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return number of bits left in the bitstream
|
||||
* bits Bitstream context
|
||||
* return >= 0: Number of bits left < 0: Overflow
|
||||
*/
|
||||
static int get_bits_left(const struct lc3_bits *bits)
|
||||
{
|
||||
const struct lc3_bits_buffer *buffer = &bits->buffer;
|
||||
const struct lc3_bits_accu *accu = &bits->accu;
|
||||
const struct lc3_bits_ac *ac = &bits->ac;
|
||||
|
||||
uintptr_t end = (uintptr_t)buffer->p_bw +
|
||||
(bits->mode == LC3_BITS_MODE_READ ? LC3_ACCU_BITS/8 : 0);
|
||||
|
||||
uintptr_t start = (uintptr_t)buffer->p_fw -
|
||||
(bits->mode == LC3_BITS_MODE_READ ? LC3_AC_BITS/8 : 0);
|
||||
|
||||
int n = end > start ? (int)(end - start) : -(int)(start - end);
|
||||
|
||||
return 8 * n - (accu->n + accu->nover + ac_get_pending_bits(ac));
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup bitstream writing
|
||||
*/
|
||||
void lc3_setup_bits(struct lc3_bits *bits,
|
||||
enum lc3_bits_mode mode, void *buffer, int len)
|
||||
{
|
||||
*bits = (struct lc3_bits){
|
||||
.mode = mode,
|
||||
.accu = {
|
||||
.n = mode == LC3_BITS_MODE_READ ? LC3_ACCU_BITS : 0,
|
||||
},
|
||||
.ac = {
|
||||
.range = 0xffffff,
|
||||
.cache = -1
|
||||
},
|
||||
.buffer = {
|
||||
.start = (uint8_t *)buffer, .end = (uint8_t *)buffer + len,
|
||||
.p_fw = (uint8_t *)buffer, .p_bw = (uint8_t *)buffer + len,
|
||||
}
|
||||
};
|
||||
|
||||
if (mode == LC3_BITS_MODE_READ) {
|
||||
struct lc3_bits_ac *ac = &bits->ac;
|
||||
struct lc3_bits_accu *accu = &bits->accu;
|
||||
struct lc3_bits_buffer *buffer = &bits->buffer;
|
||||
|
||||
ac->low = ac_get(buffer) << 16;
|
||||
ac->low |= ac_get(buffer) << 8;
|
||||
ac->low |= ac_get(buffer);
|
||||
|
||||
accu_load(accu, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return number of bits left in the bitstream
|
||||
*/
|
||||
int lc3_get_bits_left(const struct lc3_bits *bits)
|
||||
{
|
||||
return LC3_MAX(get_bits_left(bits), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return number of bits left in the bitstream
|
||||
*/
|
||||
int lc3_check_bits(const struct lc3_bits *bits)
|
||||
{
|
||||
const struct lc3_bits_ac *ac = &bits->ac;
|
||||
|
||||
return -(get_bits_left(bits) < 0 || ac->error);
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Writing
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Flush the bits accumulator
|
||||
* accu Bitstream accumulator
|
||||
* buffer Bitstream buffer
|
||||
*/
|
||||
static inline void accu_flush(
|
||||
struct lc3_bits_accu *accu, struct lc3_bits_buffer *buffer)
|
||||
{
|
||||
int nbytes = LC3_MIN(accu->n >> 3,
|
||||
LC3_MAX(buffer->p_bw - buffer->p_fw, 0));
|
||||
|
||||
accu->n -= 8 * nbytes;
|
||||
|
||||
for ( ; nbytes; accu->v >>= 8, nbytes--)
|
||||
*(--buffer->p_bw) = accu->v & 0xff;
|
||||
|
||||
if (accu->n >= 8)
|
||||
accu->n = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Arithmetic coder put byte
|
||||
* buffer Bitstream buffer
|
||||
* byte Byte to output
|
||||
*/
|
||||
static inline void ac_put(struct lc3_bits_buffer *buffer, int byte)
|
||||
{
|
||||
if (buffer->p_fw < buffer->end)
|
||||
*(buffer->p_fw++) = byte;
|
||||
}
|
||||
|
||||
/**
|
||||
* Arithmetic coder range shift
|
||||
* ac Arithmetic coder
|
||||
* buffer Bitstream buffer
|
||||
*/
|
||||
LC3_HOT static inline void ac_shift(
|
||||
struct lc3_bits_ac *ac, struct lc3_bits_buffer *buffer)
|
||||
{
|
||||
if (ac->low < 0xff0000 || ac->carry)
|
||||
{
|
||||
if (ac->cache >= 0)
|
||||
ac_put(buffer, ac->cache + ac->carry);
|
||||
|
||||
for ( ; ac->carry_count > 0; ac->carry_count--)
|
||||
ac_put(buffer, ac->carry ? 0x00 : 0xff);
|
||||
|
||||
ac->cache = ac->low >> 16;
|
||||
ac->carry = 0;
|
||||
}
|
||||
else
|
||||
ac->carry_count++;
|
||||
|
||||
ac->low = (ac->low << 8) & 0xffffff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Arithmetic coder termination
|
||||
* ac Arithmetic coder
|
||||
* buffer Bitstream buffer
|
||||
* end_val/nbits End value and count of bits to terminate (1 to 8)
|
||||
*/
|
||||
static void ac_terminate(struct lc3_bits_ac *ac,
|
||||
struct lc3_bits_buffer *buffer)
|
||||
{
|
||||
int nbits = 25 - ac_get_range_bits(ac);
|
||||
unsigned mask = 0xffffff >> nbits;
|
||||
unsigned val = ac->low + mask;
|
||||
unsigned high = ac->low + ac->range;
|
||||
|
||||
bool over_val = val >> 24;
|
||||
bool over_high = high >> 24;
|
||||
|
||||
val = (val & 0xffffff) & ~mask;
|
||||
high = (high & 0xffffff);
|
||||
|
||||
if (over_val == over_high) {
|
||||
|
||||
if (val + mask >= high) {
|
||||
nbits++;
|
||||
mask >>= 1;
|
||||
val = ((ac->low + mask) & 0xffffff) & ~mask;
|
||||
}
|
||||
|
||||
ac->carry |= val < ac->low;
|
||||
}
|
||||
|
||||
ac->low = val;
|
||||
|
||||
for (; nbits > 8; nbits -= 8)
|
||||
ac_shift(ac, buffer);
|
||||
ac_shift(ac, buffer);
|
||||
|
||||
int end_val = ac->cache >> (8 - nbits);
|
||||
|
||||
if (ac->carry_count) {
|
||||
ac_put(buffer, ac->cache);
|
||||
for ( ; ac->carry_count > 1; ac->carry_count--)
|
||||
ac_put(buffer, 0xff);
|
||||
|
||||
end_val = nbits < 8 ? 0 : 0xff;
|
||||
}
|
||||
|
||||
if (buffer->p_fw < buffer->end) {
|
||||
*buffer->p_fw &= 0xff >> nbits;
|
||||
*buffer->p_fw |= end_val << (8 - nbits);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush and terminate bitstream
|
||||
*/
|
||||
void lc3_flush_bits(struct lc3_bits *bits)
|
||||
{
|
||||
struct lc3_bits_ac *ac = &bits->ac;
|
||||
struct lc3_bits_accu *accu = &bits->accu;
|
||||
struct lc3_bits_buffer *buffer = &bits->buffer;
|
||||
|
||||
int nleft = buffer->p_bw - buffer->p_fw;
|
||||
for (int n = 8 * nleft - accu->n; n > 0; n -= 32)
|
||||
lc3_put_bits(bits, 0, LC3_MIN(n, 32));
|
||||
|
||||
accu_flush(accu, buffer);
|
||||
|
||||
ac_terminate(ac, buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write from 1 to 32 bits,
|
||||
* exceeding the capacity of the accumulator
|
||||
*/
|
||||
LC3_HOT void lc3_put_bits_generic(struct lc3_bits *bits, unsigned v, int n)
|
||||
{
|
||||
struct lc3_bits_accu *accu = &bits->accu;
|
||||
|
||||
/* --- Fulfill accumulator and flush -- */
|
||||
|
||||
int n1 = LC3_MIN(LC3_ACCU_BITS - accu->n, n);
|
||||
if (n1) {
|
||||
accu->v |= v << accu->n;
|
||||
accu->n = LC3_ACCU_BITS;
|
||||
}
|
||||
|
||||
accu_flush(accu, &bits->buffer);
|
||||
|
||||
/* --- Accumulate remaining bits -- */
|
||||
|
||||
accu->v = v >> n1;
|
||||
accu->n = n - n1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Arithmetic coder renormalization
|
||||
*/
|
||||
LC3_HOT void lc3_ac_write_renorm(struct lc3_bits *bits)
|
||||
{
|
||||
struct lc3_bits_ac *ac = &bits->ac;
|
||||
|
||||
for ( ; ac->range < 0x10000; ac->range <<= 8)
|
||||
ac_shift(ac, &bits->buffer);
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Reading
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Arithmetic coder get byte
|
||||
* buffer Bitstream buffer
|
||||
* return Byte read, 0 on overflow
|
||||
*/
|
||||
static inline int ac_get(struct lc3_bits_buffer *buffer)
|
||||
{
|
||||
return buffer->p_fw < buffer->end ? *(buffer->p_fw++) : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the accumulator
|
||||
* accu Bitstream accumulator
|
||||
* buffer Bitstream buffer
|
||||
*/
|
||||
static inline void accu_load(struct lc3_bits_accu *accu,
|
||||
struct lc3_bits_buffer *buffer)
|
||||
{
|
||||
int nbytes = LC3_MIN(accu->n >> 3, buffer->p_bw - buffer->start);
|
||||
|
||||
accu->n -= 8 * nbytes;
|
||||
|
||||
for ( ; nbytes; nbytes--) {
|
||||
accu->v >>= 8;
|
||||
accu->v |= (unsigned)*(--buffer->p_bw) << (LC3_ACCU_BITS - 8);
|
||||
}
|
||||
|
||||
if (accu->n >= 8) {
|
||||
accu->nover = LC3_MIN(accu->nover + accu->n, LC3_ACCU_BITS);
|
||||
accu->v >>= accu->n;
|
||||
accu->n = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read from 1 to 32 bits,
|
||||
* exceeding the capacity of the accumulator
|
||||
*/
|
||||
LC3_HOT unsigned lc3_get_bits_generic(struct lc3_bits *bits, int n)
|
||||
{
|
||||
struct lc3_bits_accu *accu = &bits->accu;
|
||||
struct lc3_bits_buffer *buffer = &bits->buffer;
|
||||
|
||||
/* --- Fulfill accumulator and read -- */
|
||||
|
||||
accu_load(accu, buffer);
|
||||
|
||||
int n1 = LC3_MIN(LC3_ACCU_BITS - accu->n, n);
|
||||
unsigned v = (accu->v >> accu->n) & ((1u << n1) - 1);
|
||||
accu->n += n1;
|
||||
|
||||
/* --- Second round --- */
|
||||
|
||||
int n2 = n - n1;
|
||||
|
||||
if (n2) {
|
||||
accu_load(accu, buffer);
|
||||
|
||||
v |= ((accu->v >> accu->n) & ((1u << n2) - 1)) << n1;
|
||||
accu->n += n2;
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
/**
|
||||
* Arithmetic coder renormalization
|
||||
*/
|
||||
LC3_HOT void lc3_ac_read_renorm(struct lc3_bits *bits)
|
||||
{
|
||||
struct lc3_bits_ac *ac = &bits->ac;
|
||||
|
||||
for ( ; ac->range < 0x10000; ac->range <<= 8)
|
||||
ac->low = ((ac->low << 8) | ac_get(&bits->buffer)) & 0xffffff;
|
||||
}
|
||||
@@ -0,0 +1,309 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
/**
|
||||
* The bitstream is written by the 2 ends of the buffer :
|
||||
*
|
||||
* - Arthmetic coder put bits while increasing memory addresses
|
||||
* in the buffer (forward)
|
||||
*
|
||||
* - Plain bits are puts starting the end of the buffer, with memeory
|
||||
* addresses decreasing (backward)
|
||||
*
|
||||
* .---------------------------------------------------.
|
||||
* | > > > > > > > > > > : : < < < < < < < < < |
|
||||
* '---------------------------------------------------'
|
||||
* |---------------------> - - - - - - - - - - - - - ->|
|
||||
* |< - - - <-------------------|
|
||||
* Arithmetic coding Plain bits
|
||||
* `lc3_put_symbol()` `lc3_put_bits()`
|
||||
*
|
||||
* - The forward writing is protected against buffer overflow, it cannot
|
||||
* write after the buffer, but can overwrite plain bits previously
|
||||
* written in the buffer.
|
||||
*
|
||||
* - The backward writing is protected against overwrite of the arithmetic
|
||||
* coder bitstream. In such way, the backward bitstream is always limited
|
||||
* by the aritmetic coder bitstream, and can be overwritten by him.
|
||||
*
|
||||
* .---------------------------------------------------.
|
||||
* | > > > > > > > > > > : : < < < < < < < < < |
|
||||
* '---------------------------------------------------'
|
||||
* |---------------------> - - - - - - - - - - - - - ->|
|
||||
* |< - - - - - - - - - - - - - - <-------------------|
|
||||
* Arithmetic coding Plain bits
|
||||
* `lc3_get_symbol()` `lc3_get_bits()`
|
||||
*
|
||||
* - Reading is limited to read of the complementary end of the buffer.
|
||||
*
|
||||
* - The procedure `lc3_check_bits()` returns indication that read has been
|
||||
* made crossing the other bit plane.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __LC3_BITS_H
|
||||
#define __LC3_BITS_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
||||
/**
|
||||
* Bitstream mode
|
||||
*/
|
||||
|
||||
enum lc3_bits_mode {
|
||||
LC3_BITS_MODE_READ,
|
||||
LC3_BITS_MODE_WRITE,
|
||||
};
|
||||
|
||||
/**
|
||||
* Arithmetic coder symbol interval
|
||||
* The model split the interval in 17 symbols
|
||||
*/
|
||||
|
||||
struct lc3_ac_symbol {
|
||||
uint16_t low : 16;
|
||||
uint16_t range : 16;
|
||||
};
|
||||
|
||||
struct lc3_ac_model {
|
||||
struct lc3_ac_symbol s[17];
|
||||
};
|
||||
|
||||
/**
|
||||
* Bitstream context
|
||||
*/
|
||||
|
||||
#define LC3_ACCU_BITS (int)(8 * sizeof(unsigned))
|
||||
|
||||
struct lc3_bits_accu {
|
||||
unsigned v;
|
||||
int n, nover;
|
||||
};
|
||||
|
||||
#define LC3_AC_BITS (int)(24)
|
||||
|
||||
struct lc3_bits_ac {
|
||||
unsigned low, range;
|
||||
int cache, carry, carry_count;
|
||||
bool error;
|
||||
};
|
||||
|
||||
struct lc3_bits_buffer {
|
||||
const uint8_t *start, *end;
|
||||
uint8_t *p_fw, *p_bw;
|
||||
};
|
||||
|
||||
typedef struct lc3_bits {
|
||||
enum lc3_bits_mode mode;
|
||||
struct lc3_bits_ac ac;
|
||||
struct lc3_bits_accu accu;
|
||||
struct lc3_bits_buffer buffer;
|
||||
} lc3_bits_t;
|
||||
|
||||
|
||||
/**
|
||||
* Setup bitstream reading/writing
|
||||
* bits Bitstream context
|
||||
* mode Either READ or WRITE mode
|
||||
* buffer, len Output buffer and length (in bytes)
|
||||
*/
|
||||
void lc3_setup_bits(lc3_bits_t *bits,
|
||||
enum lc3_bits_mode mode, void *buffer, int len);
|
||||
|
||||
/**
|
||||
* Return number of bits left in the bitstream
|
||||
* bits Bitstream context
|
||||
* return Number of bits left
|
||||
*/
|
||||
int lc3_get_bits_left(const lc3_bits_t *bits);
|
||||
|
||||
/**
|
||||
* Check if error occured on bitstream reading/writing
|
||||
* bits Bitstream context
|
||||
* return 0: Ok -1: Bitstream overflow or AC reading error
|
||||
*/
|
||||
int lc3_check_bits(const lc3_bits_t *bits);
|
||||
|
||||
/**
|
||||
* Put a bit
|
||||
* bits Bitstream context
|
||||
* v Bit value, 0 or 1
|
||||
*/
|
||||
static inline void lc3_put_bit(lc3_bits_t *bits, int v);
|
||||
|
||||
/**
|
||||
* Put from 1 to 32 bits
|
||||
* bits Bitstream context
|
||||
* v, n Value, in range 0 to 2^n - 1, and bits count (1 to 32)
|
||||
*/
|
||||
static inline void lc3_put_bits(lc3_bits_t *bits, unsigned v, int n);
|
||||
|
||||
/**
|
||||
* Put arithmetic coder symbol
|
||||
* bits Bitstream context
|
||||
* model, s Model distribution and symbol value
|
||||
*/
|
||||
static inline void lc3_put_symbol(lc3_bits_t *bits,
|
||||
const struct lc3_ac_model *model, unsigned s);
|
||||
|
||||
/**
|
||||
* Flush and terminate bitstream writing
|
||||
* bits Bitstream context
|
||||
*/
|
||||
void lc3_flush_bits(lc3_bits_t *bits);
|
||||
|
||||
/**
|
||||
* Get a bit
|
||||
* bits Bitstream context
|
||||
*/
|
||||
static inline int lc3_get_bit(lc3_bits_t *bits);
|
||||
|
||||
/**
|
||||
* Get from 1 to 32 bits
|
||||
* bits Bitstream context
|
||||
* n Number of bits to read (1 to 32)
|
||||
* return The value read
|
||||
*/
|
||||
static inline unsigned lc3_get_bits(lc3_bits_t *bits, int n);
|
||||
|
||||
/**
|
||||
* Get arithmetic coder symbol
|
||||
* bits Bitstream context
|
||||
* model Model distribution
|
||||
* return The value read
|
||||
*/
|
||||
static inline unsigned lc3_get_symbol(lc3_bits_t *bits,
|
||||
const struct lc3_ac_model *model);
|
||||
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Inline implementations
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
void lc3_put_bits_generic(lc3_bits_t *bits, unsigned v, int n);
|
||||
unsigned lc3_get_bits_generic(struct lc3_bits *bits, int n);
|
||||
|
||||
void lc3_ac_read_renorm(lc3_bits_t *bits);
|
||||
void lc3_ac_write_renorm(lc3_bits_t *bits);
|
||||
|
||||
|
||||
/**
|
||||
* Put a bit
|
||||
*/
|
||||
LC3_HOT static inline void lc3_put_bit(lc3_bits_t *bits, int v)
|
||||
{
|
||||
lc3_put_bits(bits, v, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put from 1 to 32 bits
|
||||
*/
|
||||
LC3_HOT static inline void lc3_put_bits(
|
||||
struct lc3_bits *bits, unsigned v, int n)
|
||||
{
|
||||
struct lc3_bits_accu *accu = &bits->accu;
|
||||
|
||||
if (accu->n + n <= LC3_ACCU_BITS) {
|
||||
accu->v |= v << accu->n;
|
||||
accu->n += n;
|
||||
} else {
|
||||
lc3_put_bits_generic(bits, v, n);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a bit
|
||||
*/
|
||||
LC3_HOT static inline int lc3_get_bit(lc3_bits_t *bits)
|
||||
{
|
||||
return lc3_get_bits(bits, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get from 1 to 32 bits
|
||||
*/
|
||||
LC3_HOT static inline unsigned lc3_get_bits(struct lc3_bits *bits, int n)
|
||||
{
|
||||
struct lc3_bits_accu *accu = &bits->accu;
|
||||
|
||||
if (accu->n + n <= LC3_ACCU_BITS) {
|
||||
int v = (accu->v >> accu->n) & ((1u << n) - 1);
|
||||
return (accu->n += n), v;
|
||||
}
|
||||
else {
|
||||
return lc3_get_bits_generic(bits, n);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Put arithmetic coder symbol
|
||||
*/
|
||||
LC3_HOT static inline void lc3_put_symbol(
|
||||
struct lc3_bits *bits, const struct lc3_ac_model *model, unsigned s)
|
||||
{
|
||||
const struct lc3_ac_symbol *symbols = model->s;
|
||||
struct lc3_bits_ac *ac = &bits->ac;
|
||||
unsigned range = ac->range >> 10;
|
||||
|
||||
ac->low += range * symbols[s].low;
|
||||
ac->range = range * symbols[s].range;
|
||||
|
||||
ac->carry |= ac->low >> 24;
|
||||
ac->low &= 0xffffff;
|
||||
|
||||
if (ac->range < 0x10000)
|
||||
lc3_ac_write_renorm(bits);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get arithmetic coder symbol
|
||||
*/
|
||||
LC3_HOT static inline unsigned lc3_get_symbol(
|
||||
lc3_bits_t *bits, const struct lc3_ac_model *model)
|
||||
{
|
||||
const struct lc3_ac_symbol *symbols = model->s;
|
||||
struct lc3_bits_ac *ac = &bits->ac;
|
||||
|
||||
unsigned range = (ac->range >> 10) & 0xffff;
|
||||
|
||||
ac->error |= (ac->low >= (range << 10));
|
||||
if (ac->error)
|
||||
ac->low = 0;
|
||||
|
||||
int s = 16;
|
||||
|
||||
if (ac->low < range * symbols[s].low) {
|
||||
s >>= 1;
|
||||
s -= ac->low < range * symbols[s].low ? 4 : -4;
|
||||
s -= ac->low < range * symbols[s].low ? 2 : -2;
|
||||
s -= ac->low < range * symbols[s].low ? 1 : -1;
|
||||
s -= ac->low < range * symbols[s].low;
|
||||
}
|
||||
|
||||
ac->low -= range * symbols[s].low;
|
||||
ac->range = range * symbols[s].range;
|
||||
|
||||
if (ac->range < 0x10000)
|
||||
lc3_ac_read_renorm(bits);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
#endif /* __LC3_BITS_H */
|
||||
@@ -0,0 +1,154 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#include "bwdet.h"
|
||||
|
||||
|
||||
/**
|
||||
* Bandwidth detector
|
||||
*/
|
||||
enum lc3_bandwidth lc3_bwdet_run(
|
||||
enum lc3_dt dt, enum lc3_srate sr, const float *e)
|
||||
{
|
||||
/* Bandwidth regions */
|
||||
|
||||
struct region { int is : 8; int ie : 8; };
|
||||
|
||||
#if LC3_PLUS
|
||||
|
||||
static const struct region bws_table_2m5[][LC3_BANDWIDTH_SWB+1] = {
|
||||
{ { 24, 34+1 } },
|
||||
{ { 24, 32+1 }, { 35, 39+1 } },
|
||||
{ { 24, 31+1 }, { 33, 38+1 }, { 39, 42+1 } },
|
||||
{ { 22, 29+1 }, { 31, 35+1 }, { 37, 40+1 }, { 41, 43+1 } },
|
||||
};
|
||||
|
||||
static const struct region bws_table_5m[][LC3_BANDWIDTH_SWB+1] = {
|
||||
{ { 39, 49+1 } },
|
||||
{ { 35, 44+1 }, { 47, 51+1 } },
|
||||
{ { 34, 42+1 }, { 44, 49+1 }, { 50, 53+1 } },
|
||||
{ { 32, 40+1 }, { 42, 46+1 }, { 48, 51+1 }, { 52, 54+1 } },
|
||||
};
|
||||
|
||||
#endif /* LC3_PLUS */
|
||||
|
||||
static const struct region bws_table_7m5[][LC3_BANDWIDTH_SWB+1] = {
|
||||
{ { 51, 63+1 } },
|
||||
{ { 45, 55+1 }, { 58, 63+1 } },
|
||||
{ { 42, 51+1 }, { 53, 58+1 }, { 60, 63+1 } },
|
||||
{ { 40, 48+1 }, { 51, 55+1 }, { 57, 60+1 }, { 61, 63+1 } },
|
||||
};
|
||||
|
||||
static const struct region bws_table_10m[][LC3_BANDWIDTH_SWB+1] = {
|
||||
{ { 53, 63+1 } },
|
||||
{ { 47, 56+1 }, { 59, 63+1 } },
|
||||
{ { 44, 52+1 }, { 54, 59+1 }, { 60, 63+1 } },
|
||||
{ { 41, 49+1 }, { 51, 55+1 }, { 57, 60+1 }, { 61, 63+1 } },
|
||||
};
|
||||
|
||||
static const struct region (*bws_table[])[LC3_BANDWIDTH_SWB+1] = {
|
||||
[LC3_DT_2M5] = LC3_IF_PLUS(bws_table_2m5, NULL),
|
||||
[LC3_DT_5M ] = LC3_IF_PLUS(bws_table_5m , NULL),
|
||||
[LC3_DT_7M5] = bws_table_7m5,
|
||||
[LC3_DT_10M] = bws_table_10m,
|
||||
};
|
||||
|
||||
static const int l_table[LC3_NUM_DT][LC3_BANDWIDTH_SWB+1] = {
|
||||
[LC3_DT_2M5] = { 4, 4, 3, 1 },
|
||||
[LC3_DT_5M ] = { 4, 4, 3, 1 },
|
||||
[LC3_DT_7M5] = { 4, 4, 3, 2 },
|
||||
[LC3_DT_10M] = { 4, 4, 3, 1 },
|
||||
};
|
||||
|
||||
/* --- Stage 1 ---
|
||||
* Determine bw0 candidate */
|
||||
|
||||
enum lc3_bandwidth bw0 = LC3_BANDWIDTH_NB;
|
||||
enum lc3_bandwidth bwn = (enum lc3_bandwidth)sr;
|
||||
|
||||
if (bwn <= bw0 || lc3_hr(sr))
|
||||
return bwn;
|
||||
|
||||
const struct region *bwr = bws_table[dt][bwn-1];
|
||||
|
||||
for (enum lc3_bandwidth bw = bw0; bw < bwn; bw++) {
|
||||
int i = bwr[bw].is, ie = bwr[bw].ie;
|
||||
int n = ie - i;
|
||||
|
||||
float se = e[i];
|
||||
for (i++; i < ie; i++)
|
||||
se += e[i];
|
||||
|
||||
if (se >= (10 << (bw == LC3_BANDWIDTH_NB)) * n)
|
||||
bw0 = bw + 1;
|
||||
}
|
||||
|
||||
/* --- Stage 2 ---
|
||||
* Detect drop above cut-off frequency.
|
||||
* The Tc condition (13) is precalculated, as
|
||||
* Tc[] = 10 ^ (n / 10) , n = { 15, 23, 20, 20 } */
|
||||
|
||||
int hold = bw0 >= bwn;
|
||||
|
||||
if (!hold) {
|
||||
int i0 = bwr[bw0].is, l = l_table[dt][bw0];
|
||||
float tc = (const float []){
|
||||
31.62277660, 199.52623150, 100, 100 }[bw0];
|
||||
|
||||
for (int i = i0 - l + 1; !hold && i <= i0 + 1; i++) {
|
||||
hold = e[i-l] > tc * e[i];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return hold ? bw0 : bwn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return number of bits coding the bandwidth value
|
||||
*/
|
||||
int lc3_bwdet_get_nbits(enum lc3_srate sr)
|
||||
{
|
||||
return lc3_hr(sr) ? 0 : (sr > 0) + (sr > 1) + (sr > 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put bandwidth indication
|
||||
*/
|
||||
void lc3_bwdet_put_bw(lc3_bits_t *bits,
|
||||
enum lc3_srate sr, enum lc3_bandwidth bw)
|
||||
{
|
||||
int nbits_bw = lc3_bwdet_get_nbits(sr);
|
||||
if (nbits_bw > 0)
|
||||
lc3_put_bits(bits, bw, nbits_bw);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get bandwidth indication
|
||||
*/
|
||||
int lc3_bwdet_get_bw(lc3_bits_t *bits,
|
||||
enum lc3_srate sr, enum lc3_bandwidth *bw)
|
||||
{
|
||||
enum lc3_bandwidth max_bw = (enum lc3_bandwidth)sr;
|
||||
int nbits_bw = lc3_bwdet_get_nbits(sr);
|
||||
|
||||
*bw = nbits_bw <= 0 ? max_bw :
|
||||
(enum lc3_bandwidth)lc3_get_bits(bits, nbits_bw);
|
||||
|
||||
return *bw > max_bw ? (*bw = max_bw), -1 : 0;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __LC3_BWDET_H
|
||||
#define __LC3_BWDET_H
|
||||
|
||||
#include "common.h"
|
||||
#include "bits.h"
|
||||
|
||||
|
||||
/**
|
||||
* Bandwidth detector
|
||||
* dt, sr Duration and samplerate of the frame
|
||||
* e Energy estimation per bands
|
||||
* return Return detected bandwitdth
|
||||
*/
|
||||
enum lc3_bandwidth lc3_bwdet_run(
|
||||
enum lc3_dt dt, enum lc3_srate sr, const float *e);
|
||||
|
||||
/**
|
||||
* Return number of bits coding the bandwidth value
|
||||
* sr Samplerate of the frame
|
||||
* return Number of bits coding the bandwidth value
|
||||
*/
|
||||
int lc3_bwdet_get_nbits(enum lc3_srate sr);
|
||||
|
||||
/**
|
||||
* Put bandwidth indication
|
||||
* bits Bitstream context
|
||||
* sr Samplerate of the frame
|
||||
* bw Bandwidth detected
|
||||
*/
|
||||
void lc3_bwdet_put_bw(lc3_bits_t *bits,
|
||||
enum lc3_srate sr, enum lc3_bandwidth bw);
|
||||
|
||||
/**
|
||||
* Get bandwidth indication
|
||||
* bits Bitstream context
|
||||
* sr Samplerate of the frame
|
||||
* bw Return bandwidth indication
|
||||
* return 0: Ok -1: Invalid bandwidth indication
|
||||
*/
|
||||
int lc3_bwdet_get_bw(lc3_bits_t *bits,
|
||||
enum lc3_srate sr, enum lc3_bandwidth *bw);
|
||||
|
||||
|
||||
#endif /* __LC3_BWDET_H */
|
||||
@@ -0,0 +1,142 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __LC3_COMMON_H
|
||||
#define __LC3_COMMON_H
|
||||
|
||||
#include <lc3.h>
|
||||
#include "fastmath.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdalign.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __ARM_ARCH
|
||||
#include <arm_acle.h>
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* Acivation flags for LC3-Plus and LC3-Plus HR features
|
||||
*/
|
||||
|
||||
#ifndef LC3_PLUS
|
||||
#define LC3_PLUS 1
|
||||
#endif
|
||||
|
||||
#ifndef LC3_PLUS_HR
|
||||
#define LC3_PLUS_HR 1
|
||||
#endif
|
||||
|
||||
#if LC3_PLUS
|
||||
#define LC3_IF_PLUS(a, b) (a)
|
||||
#else
|
||||
#define LC3_IF_PLUS(a, b) (b)
|
||||
#endif
|
||||
|
||||
#if LC3_PLUS_HR
|
||||
#define LC3_IF_PLUS_HR(a, b) (a)
|
||||
#else
|
||||
#define LC3_IF_PLUS_HR(a, b) (b)
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* Hot Function attribute
|
||||
* Selectively disable sanitizer
|
||||
*/
|
||||
|
||||
#ifdef __clang__
|
||||
|
||||
#define LC3_HOT \
|
||||
__attribute__((no_sanitize("bounds"))) \
|
||||
__attribute__((no_sanitize("integer")))
|
||||
|
||||
#else /* __clang__ */
|
||||
|
||||
#define LC3_HOT
|
||||
|
||||
#endif /* __clang__ */
|
||||
|
||||
|
||||
/**
|
||||
* Macros
|
||||
* MIN/MAX Minimum and maximum between 2 values
|
||||
* CLIP Clip a value between low and high limits
|
||||
* SATXX Signed saturation on 'xx' bits
|
||||
* ABS Return absolute value
|
||||
*/
|
||||
|
||||
#define LC3_MIN(a, b) ( (a) < (b) ? (a) : (b) )
|
||||
#define LC3_MAX(a, b) ( (a) > (b) ? (a) : (b) )
|
||||
|
||||
#define LC3_CLIP(v, min, max) LC3_MIN(LC3_MAX(v, min), max)
|
||||
#define LC3_SAT16(v) LC3_CLIP(v, -(1 << 15), (1 << 15) - 1)
|
||||
#define LC3_SAT24(v) LC3_CLIP(v, -(1 << 23), (1 << 23) - 1)
|
||||
|
||||
#define LC3_ABS(v) ( (v) < 0 ? -(v) : (v) )
|
||||
|
||||
|
||||
#if defined(__ARM_FEATURE_SAT) && !(__GNUC__ < 10)
|
||||
|
||||
#undef LC3_SAT16
|
||||
#define LC3_SAT16(v) __ssat(v, 16)
|
||||
|
||||
#undef LC3_SAT24
|
||||
#define LC3_SAT24(v) __ssat(v, 24)
|
||||
|
||||
#endif /* __ARM_FEATURE_SAT */
|
||||
|
||||
|
||||
/**
|
||||
* Return `true` when high-resolution mode
|
||||
*/
|
||||
static inline bool lc3_hr(enum lc3_srate sr) {
|
||||
return LC3_PLUS_HR && (sr >= LC3_SRATE_48K_HR);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Bandwidth, mapped to Nyquist frequency of samplerates
|
||||
*/
|
||||
|
||||
enum lc3_bandwidth {
|
||||
LC3_BANDWIDTH_NB = LC3_SRATE_8K,
|
||||
LC3_BANDWIDTH_WB = LC3_SRATE_16K,
|
||||
LC3_BANDWIDTH_SSWB = LC3_SRATE_24K,
|
||||
LC3_BANDWIDTH_SWB = LC3_SRATE_32K,
|
||||
LC3_BANDWIDTH_FB = LC3_SRATE_48K,
|
||||
|
||||
LC3_BANDWIDTH_FB_HR = LC3_SRATE_48K_HR,
|
||||
LC3_BANDWIDTH_UB_HR = LC3_SRATE_96K_HR,
|
||||
|
||||
LC3_NUM_BANDWIDTH,
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Complex floating point number
|
||||
*/
|
||||
|
||||
struct lc3_complex
|
||||
{
|
||||
float re, im;
|
||||
};
|
||||
|
||||
|
||||
#endif /* __LC3_COMMON_H */
|
||||
@@ -0,0 +1,54 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#include "energy.h"
|
||||
#include "tables.h"
|
||||
|
||||
|
||||
/**
|
||||
* Energy estimation per band
|
||||
*/
|
||||
bool lc3_energy_compute(
|
||||
enum lc3_dt dt, enum lc3_srate sr, const float *x, float *e)
|
||||
{
|
||||
int nb = lc3_num_bands[dt][sr];
|
||||
const int *lim = lc3_band_lim[dt][sr];
|
||||
|
||||
/* Mean the square of coefficients within each band */
|
||||
|
||||
float e_sum[2] = { 0, 0 };
|
||||
int iband_h = nb - (const int []){
|
||||
[LC3_DT_2M5] = 2, [LC3_DT_5M ] = 3,
|
||||
[LC3_DT_7M5] = 4, [LC3_DT_10M] = 2 }[dt];
|
||||
|
||||
for (int iband = 0, i = lim[iband]; iband < nb; iband++) {
|
||||
int ie = lim[iband+1];
|
||||
int n = ie - i;
|
||||
|
||||
float sx2 = x[i] * x[i];
|
||||
for (i++; i < ie; i++)
|
||||
sx2 += x[i] * x[i];
|
||||
|
||||
*e = sx2 / n;
|
||||
e_sum[iband >= iband_h] += *(e++);
|
||||
}
|
||||
|
||||
/* Return the near nyquist flag */
|
||||
|
||||
return e_sum[1] > 30 * e_sum[0];
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __LC3_ENERGY_H
|
||||
#define __LC3_ENERGY_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
||||
/**
|
||||
* Energy estimation per band
|
||||
* dt, sr Duration and samplerate of the frame
|
||||
* x Input MDCT coefficient
|
||||
* e Energy estimation per bands
|
||||
* return True when high energy detected near Nyquist frequency
|
||||
*/
|
||||
bool lc3_energy_compute(
|
||||
enum lc3_dt dt, enum lc3_srate sr, const float *x, float *e);
|
||||
|
||||
|
||||
#endif /* __LC3_ENERGY_H */
|
||||
@@ -0,0 +1,219 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __LC3_FASTMATH_H
|
||||
#define __LC3_FASTMATH_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
|
||||
|
||||
/**
|
||||
* IEEE 754 Floating point representation
|
||||
*/
|
||||
|
||||
#define LC3_IEEE754_SIGN_SHL (31)
|
||||
#define LC3_IEEE754_SIGN_MASK (1 << LC3_IEEE754_SIGN_SHL)
|
||||
|
||||
#define LC3_IEEE754_EXP_SHL (23)
|
||||
#define LC3_IEEE754_EXP_MASK (0xff << LC3_IEEE754_EXP_SHL)
|
||||
#define LC3_IEEE754_EXP_BIAS (127)
|
||||
|
||||
|
||||
/**
|
||||
* Fast multiply floating-point number by integral power of 2
|
||||
* x Operand, finite number
|
||||
* exp Exponent such that 2^x is finite
|
||||
* return 2^exp
|
||||
*/
|
||||
static inline float lc3_ldexpf(float _x, int exp) {
|
||||
union { float f; int32_t s; } x = { .f = _x };
|
||||
|
||||
if (x.s & LC3_IEEE754_EXP_MASK)
|
||||
x.s += exp << LC3_IEEE754_EXP_SHL;
|
||||
|
||||
return x.f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fast convert floating-point number to fractional and integral components
|
||||
* x Operand, finite number
|
||||
* exp Return the exponent part
|
||||
* return The normalized fraction in [0.5:1[
|
||||
*/
|
||||
static inline float lc3_frexpf(float _x, int *exp) {
|
||||
union { float f; uint32_t u; } x = { .f = _x };
|
||||
|
||||
int e = (x.u & LC3_IEEE754_EXP_MASK) >> LC3_IEEE754_EXP_SHL;
|
||||
*exp = e - (LC3_IEEE754_EXP_BIAS - 1);
|
||||
|
||||
x.u = (x.u & ~LC3_IEEE754_EXP_MASK) |
|
||||
((LC3_IEEE754_EXP_BIAS - 1) << LC3_IEEE754_EXP_SHL);
|
||||
|
||||
return x.f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fast 2^n approximation
|
||||
* x Operand, range -100 to 100
|
||||
* return 2^x approximation (max relative error ~ 4e-7)
|
||||
*/
|
||||
static inline float lc3_exp2f(float x)
|
||||
{
|
||||
/* --- 2^(i/8) for i from 0 to 7 --- */
|
||||
|
||||
static const float e[] = {
|
||||
1.00000000e+00, 1.09050773e+00, 1.18920712e+00, 1.29683955e+00,
|
||||
1.41421356e+00, 1.54221083e+00, 1.68179283e+00, 1.83400809e+00 };
|
||||
|
||||
/* --- Polynomial approx in range 0 to 1/8 --- */
|
||||
|
||||
static const float p[] = {
|
||||
1.00448128e-02, 5.54563260e-02, 2.40228756e-01, 6.93147140e-01 };
|
||||
|
||||
/* --- Split the operand ---
|
||||
*
|
||||
* Such as x = k/8 + y, with k an integer, and |y| < 0.5/8
|
||||
*
|
||||
* Note that `fast-math` compiler option leads to rounding error,
|
||||
* disable optimisation with `volatile`. */
|
||||
|
||||
volatile union { float f; int32_t s; } v;
|
||||
|
||||
v.f = x + 0x1.8p20f;
|
||||
int k = v.s;
|
||||
x -= v.f - 0x1.8p20f;
|
||||
|
||||
/* --- Compute 2^x, with |x| < 1 ---
|
||||
* Perform polynomial approximation in range -0.5/8 to 0.5/8,
|
||||
* and muplity by precomputed value of 2^(i/8), i in [0:7] */
|
||||
|
||||
union { float f; int32_t s; } y;
|
||||
|
||||
y.f = ( p[0]) * x;
|
||||
y.f = (y.f + p[1]) * x;
|
||||
y.f = (y.f + p[2]) * x;
|
||||
y.f = (y.f + p[3]) * x;
|
||||
y.f = (y.f + 1.f) * e[k & 7];
|
||||
|
||||
/* --- Add the exponent --- */
|
||||
|
||||
y.s += (k >> 3) << LC3_IEEE754_EXP_SHL;
|
||||
|
||||
return y.f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fast log2(x) approximation
|
||||
* x Operand, greater than 0
|
||||
* return log2(x) approximation (max absolute error ~ 1e-4)
|
||||
*/
|
||||
static inline float lc3_log2f(float x)
|
||||
{
|
||||
float y;
|
||||
int e;
|
||||
|
||||
/* --- Polynomial approx in range 0.5 to 1 --- */
|
||||
|
||||
static const float c[] = {
|
||||
-1.29479677, 5.11769018, -8.42295281, 8.10557963, -3.50567360 };
|
||||
|
||||
x = lc3_frexpf(x, &e);
|
||||
|
||||
y = ( c[0]) * x;
|
||||
y = (y + c[1]) * x;
|
||||
y = (y + c[2]) * x;
|
||||
y = (y + c[3]) * x;
|
||||
y = (y + c[4]);
|
||||
|
||||
/* --- Add log2f(2^e) and return --- */
|
||||
|
||||
return e + y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fast log10(x) approximation
|
||||
* x Operand, greater than 0
|
||||
* return log10(x) approximation (max absolute error ~ 1e-4)
|
||||
*/
|
||||
static inline float lc3_log10f(float x)
|
||||
{
|
||||
return log10f(2) * lc3_log2f(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fast `10 * log10(x)` (or dB) approximation in fixed Q16
|
||||
* x Operand, in range 2^-63 to 2^63 (1e-19 to 1e19)
|
||||
* return 10 * log10(x) in fixed Q16 (-190 to 192 dB)
|
||||
*
|
||||
* - The 0 value is accepted and return the minimum value ~ -191dB
|
||||
* - This function assumed that float 32 bits is coded IEEE 754
|
||||
*/
|
||||
static inline int32_t lc3_db_q16(float x)
|
||||
{
|
||||
/* --- Table in Q15 --- */
|
||||
|
||||
static const uint16_t t[][2] = {
|
||||
|
||||
/* [n][0] = 10 * log10(2) * log2(1 + n/32), with n = [0..15] */
|
||||
/* [n][1] = [n+1][0] - [n][0] (while defining [16][0]) */
|
||||
|
||||
{ 0, 4379 }, { 4379, 4248 }, { 8627, 4125 }, { 12753, 4009 },
|
||||
{ 16762, 3899 }, { 20661, 3795 }, { 24456, 3697 }, { 28153, 3603 },
|
||||
{ 31755, 3514 }, { 35269, 3429 }, { 38699, 3349 }, { 42047, 3272 },
|
||||
{ 45319, 3198 }, { 48517, 3128 }, { 51645, 3061 }, { 54705, 2996 },
|
||||
|
||||
/* [n][0] = 10 * log10(2) * log2(1 + n/32) - 10 * log10(2) / 2, */
|
||||
/* with n = [16..31] */
|
||||
/* [n][1] = [n+1][0] - [n][0] (while defining [32][0]) */
|
||||
|
||||
{ 8381, 2934 }, { 11315, 2875 }, { 14190, 2818 }, { 17008, 2763 },
|
||||
{ 19772, 2711 }, { 22482, 2660 }, { 25142, 2611 }, { 27754, 2564 },
|
||||
{ 30318, 2519 }, { 32837, 2475 }, { 35312, 2433 }, { 37744, 2392 },
|
||||
{ 40136, 2352 }, { 42489, 2314 }, { 44803, 2277 }, { 47080, 2241 },
|
||||
|
||||
};
|
||||
|
||||
/* --- Approximation ---
|
||||
*
|
||||
* 10 * log10(x^2) = 10 * log10(2) * log2(x^2)
|
||||
*
|
||||
* And log2(x^2) = 2 * log2( (1 + m) * 2^e )
|
||||
* = 2 * (e + log2(1 + m)) , with m in range [0..1]
|
||||
*
|
||||
* Split the float values in :
|
||||
* e2 Double value of the exponent (2 * e + k)
|
||||
* hi High 5 bits of mantissa, for precalculated result `t[hi][0]`
|
||||
* lo Low 16 bits of mantissa, for linear interpolation `t[hi][1]`
|
||||
*
|
||||
* Two cases, from the range of the mantissa :
|
||||
* 0 to 0.5 `k = 0`, use 1st part of the table
|
||||
* 0.5 to 1 `k = 1`, use 2nd part of the table */
|
||||
|
||||
union { float f; uint32_t u; } x2 = { .f = x*x };
|
||||
|
||||
int e2 = (int)(x2.u >> 22) - 2*127;
|
||||
int hi = (x2.u >> 18) & 0x1f;
|
||||
int lo = (x2.u >> 2) & 0xffff;
|
||||
|
||||
return e2 * 49321 + t[hi][0] + ((t[hi][1] * lo) >> 16);
|
||||
}
|
||||
|
||||
|
||||
#endif /* __LC3_FASTMATH_H */
|
||||
@@ -0,0 +1,766 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#include <lc3.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "bits.h"
|
||||
|
||||
#include "attdet.h"
|
||||
#include "bwdet.h"
|
||||
#include "ltpf.h"
|
||||
#include "mdct.h"
|
||||
#include "energy.h"
|
||||
#include "sns.h"
|
||||
#include "tns.h"
|
||||
#include "spec.h"
|
||||
#include "plc.h"
|
||||
|
||||
|
||||
/**
|
||||
* Frame side data
|
||||
*/
|
||||
|
||||
struct side_data {
|
||||
enum lc3_bandwidth bw;
|
||||
bool pitch_present;
|
||||
lc3_ltpf_data_t ltpf;
|
||||
lc3_sns_data_t sns;
|
||||
lc3_tns_data_t tns;
|
||||
lc3_spec_side_t spec;
|
||||
};
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* General
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Resolve frame duration in us
|
||||
* us Frame duration in us
|
||||
* hrmode High-resolution mode indication
|
||||
* return Frame duration identifier, or LC3_NUM_DT
|
||||
*/
|
||||
static enum lc3_dt resolve_dt(int us, bool hrmode)
|
||||
{
|
||||
return LC3_PLUS && us == 2500 ? LC3_DT_2M5 :
|
||||
LC3_PLUS && us == 5000 ? LC3_DT_5M :
|
||||
!hrmode && us == 7500 ? LC3_DT_7M5 :
|
||||
us == 10000 ? LC3_DT_10M : LC3_NUM_DT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve samplerate in Hz
|
||||
* hz Samplerate in Hz
|
||||
* hrmode High-resolution mode indication
|
||||
* return Sample rate identifier, or LC3_NUM_SRATE
|
||||
*/
|
||||
static enum lc3_srate resolve_srate(int hz, bool hrmode)
|
||||
{
|
||||
hrmode = LC3_PLUS_HR && hrmode;
|
||||
|
||||
return !hrmode && hz == 8000 ? LC3_SRATE_8K :
|
||||
!hrmode && hz == 16000 ? LC3_SRATE_16K :
|
||||
!hrmode && hz == 24000 ? LC3_SRATE_24K :
|
||||
!hrmode && hz == 32000 ? LC3_SRATE_32K :
|
||||
!hrmode && hz == 48000 ? LC3_SRATE_48K :
|
||||
hrmode && hz == 48000 ? LC3_SRATE_48K_HR :
|
||||
hrmode && hz == 96000 ? LC3_SRATE_96K_HR : LC3_NUM_SRATE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of PCM samples in a frame
|
||||
*/
|
||||
LC3_EXPORT int lc3_hr_frame_samples(bool hrmode, int dt_us, int sr_hz)
|
||||
{
|
||||
enum lc3_dt dt = resolve_dt(dt_us, hrmode);
|
||||
enum lc3_srate sr = resolve_srate(sr_hz, hrmode);
|
||||
|
||||
if (dt >= LC3_NUM_DT || sr >= LC3_NUM_SRATE)
|
||||
return -1;
|
||||
|
||||
return lc3_ns(dt, sr);
|
||||
}
|
||||
|
||||
LC3_EXPORT int lc3_frame_samples(int dt_us, int sr_hz)
|
||||
{
|
||||
return lc3_hr_frame_samples(false, dt_us, sr_hz);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the size of frames or frame blocks, from bitrate
|
||||
*/
|
||||
LC3_EXPORT int lc3_hr_frame_block_bytes(
|
||||
bool hrmode, int dt_us, int sr_hz, int nchannels, int bitrate)
|
||||
{
|
||||
enum lc3_dt dt = resolve_dt(dt_us, hrmode);
|
||||
enum lc3_srate sr = resolve_srate(sr_hz, hrmode);
|
||||
|
||||
if (dt >= LC3_NUM_DT || sr >= LC3_NUM_SRATE
|
||||
|| nchannels < 1 || nchannels > 8 || bitrate < 0)
|
||||
return -1;
|
||||
|
||||
bitrate = LC3_CLIP(bitrate, 0, 8*LC3_HR_MAX_BITRATE);
|
||||
|
||||
return LC3_CLIP((bitrate * (int)(1 + dt)) / 3200,
|
||||
nchannels * lc3_min_frame_bytes(dt, sr),
|
||||
nchannels * lc3_max_frame_bytes(dt, sr) );
|
||||
}
|
||||
|
||||
LC3_EXPORT int lc3_frame_bock_bytes(int dt_us, int nchannels, int bitrate)
|
||||
{
|
||||
return lc3_hr_frame_block_bytes(false, dt_us, 8000, nchannels, bitrate);
|
||||
}
|
||||
|
||||
LC3_EXPORT int lc3_hr_frame_bytes(
|
||||
bool hrmode, int dt_us, int sr_hz, int bitrate)
|
||||
{
|
||||
return lc3_hr_frame_block_bytes(hrmode, dt_us, sr_hz, 1, bitrate);
|
||||
}
|
||||
|
||||
LC3_EXPORT int lc3_frame_bytes(int dt_us, int bitrate)
|
||||
{
|
||||
return lc3_hr_frame_bytes(false, dt_us, 8000, bitrate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the bitrate, from the size of frames or frame blocks
|
||||
*/
|
||||
LC3_EXPORT int lc3_hr_resolve_bitrate(
|
||||
bool hrmode, int dt_us, int sr_hz, int nbytes)
|
||||
{
|
||||
enum lc3_dt dt = resolve_dt(dt_us, hrmode);
|
||||
enum lc3_srate sr = resolve_srate(sr_hz, hrmode);
|
||||
|
||||
if (dt >= LC3_NUM_DT || sr >= LC3_NUM_SRATE || nbytes < 0)
|
||||
return -1;
|
||||
|
||||
return LC3_MIN(((int64_t)nbytes * 3200 + dt) / (1 + dt), INT_MAX);
|
||||
}
|
||||
|
||||
LC3_EXPORT int lc3_resolve_bitrate(int dt_us, int nbytes)
|
||||
{
|
||||
return lc3_hr_resolve_bitrate(false, dt_us, 8000, nbytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return algorithmic delay, as a number of samples
|
||||
*/
|
||||
LC3_EXPORT int lc3_hr_delay_samples(bool hrmode, int dt_us, int sr_hz)
|
||||
{
|
||||
enum lc3_dt dt = resolve_dt(dt_us, hrmode);
|
||||
enum lc3_srate sr = resolve_srate(sr_hz, hrmode);
|
||||
|
||||
if (dt >= LC3_NUM_DT || sr >= LC3_NUM_SRATE)
|
||||
return -1;
|
||||
|
||||
return 2 * lc3_nd(dt, sr) - lc3_ns(dt, sr);
|
||||
}
|
||||
|
||||
LC3_EXPORT int lc3_delay_samples(int dt_us, int sr_hz)
|
||||
{
|
||||
return lc3_hr_delay_samples(false, dt_us, sr_hz);
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Encoder
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Input PCM Samples from signed 16 bits
|
||||
* encoder Encoder state
|
||||
* pcm, stride Input PCM samples, and count between two consecutives
|
||||
*/
|
||||
static void load_s16(
|
||||
struct lc3_encoder *encoder, const void *_pcm, int stride)
|
||||
{
|
||||
const int16_t *pcm = _pcm;
|
||||
|
||||
enum lc3_dt dt = encoder->dt;
|
||||
enum lc3_srate sr = encoder->sr_pcm;
|
||||
|
||||
int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
|
||||
float *xs = encoder->x + encoder->xs_off;
|
||||
int ns = lc3_ns(dt, sr);
|
||||
|
||||
for (int i = 0; i < ns; i++, pcm += stride)
|
||||
xt[i] = *pcm, xs[i] = *pcm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Input PCM Samples from signed 24 bits
|
||||
* encoder Encoder state
|
||||
* pcm, stride Input PCM samples, and count between two consecutives
|
||||
*/
|
||||
static void load_s24(
|
||||
struct lc3_encoder *encoder, const void *_pcm, int stride)
|
||||
{
|
||||
const int32_t *pcm = _pcm;
|
||||
|
||||
enum lc3_dt dt = encoder->dt;
|
||||
enum lc3_srate sr = encoder->sr_pcm;
|
||||
|
||||
int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
|
||||
float *xs = encoder->x + encoder->xs_off;
|
||||
int ns = lc3_ns(dt, sr);
|
||||
|
||||
for (int i = 0; i < ns; i++, pcm += stride) {
|
||||
xt[i] = *pcm >> 8;
|
||||
xs[i] = lc3_ldexpf(*pcm, -8);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Input PCM Samples from signed 24 bits packed
|
||||
* encoder Encoder state
|
||||
* pcm, stride Input PCM samples, and count between two consecutives
|
||||
*/
|
||||
static void load_s24_3le(
|
||||
struct lc3_encoder *encoder, const void *_pcm, int stride)
|
||||
{
|
||||
const uint8_t *pcm = _pcm;
|
||||
|
||||
enum lc3_dt dt = encoder->dt;
|
||||
enum lc3_srate sr = encoder->sr_pcm;
|
||||
|
||||
int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
|
||||
float *xs = encoder->x + encoder->xs_off;
|
||||
int ns = lc3_ns(dt, sr);
|
||||
|
||||
for (int i = 0; i < ns; i++, pcm += 3*stride) {
|
||||
int32_t in = ((uint32_t)pcm[0] << 8) |
|
||||
((uint32_t)pcm[1] << 16) |
|
||||
((uint32_t)pcm[2] << 24) ;
|
||||
|
||||
xt[i] = in >> 16;
|
||||
xs[i] = lc3_ldexpf(in, -16);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Input PCM Samples from float 32 bits
|
||||
* encoder Encoder state
|
||||
* pcm, stride Input PCM samples, and count between two consecutives
|
||||
*/
|
||||
static void load_float(
|
||||
struct lc3_encoder *encoder, const void *_pcm, int stride)
|
||||
{
|
||||
const float *pcm = _pcm;
|
||||
|
||||
enum lc3_dt dt = encoder->dt;
|
||||
enum lc3_srate sr = encoder->sr_pcm;
|
||||
|
||||
int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
|
||||
float *xs = encoder->x + encoder->xs_off;
|
||||
int ns = lc3_ns(dt, sr);
|
||||
|
||||
for (int i = 0; i < ns; i++, pcm += stride) {
|
||||
xs[i] = lc3_ldexpf(*pcm, 15);
|
||||
xt[i] = LC3_SAT16((int32_t)xs[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Frame Analysis
|
||||
* encoder Encoder state
|
||||
* nbytes Size in bytes of the frame
|
||||
* side Return frame data
|
||||
*/
|
||||
static void analyze(struct lc3_encoder *encoder,
|
||||
int nbytes, struct side_data *side)
|
||||
{
|
||||
enum lc3_dt dt = encoder->dt;
|
||||
enum lc3_srate sr = encoder->sr;
|
||||
enum lc3_srate sr_pcm = encoder->sr_pcm;
|
||||
|
||||
int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
|
||||
float *xs = encoder->x + encoder->xs_off;
|
||||
int ns = lc3_ns(dt, sr_pcm);
|
||||
int nt = lc3_nt(sr_pcm);
|
||||
|
||||
float *xd = encoder->x + encoder->xd_off;
|
||||
float *xf = xs;
|
||||
|
||||
/* --- Temporal --- */
|
||||
|
||||
bool att = lc3_attdet_run(dt, sr_pcm, nbytes, &encoder->attdet, xt);
|
||||
|
||||
side->pitch_present =
|
||||
lc3_ltpf_analyse(dt, sr_pcm, &encoder->ltpf, xt, &side->ltpf);
|
||||
|
||||
memmove(xt - nt, xt + (ns-nt), nt * sizeof(*xt));
|
||||
|
||||
/* --- Spectral --- */
|
||||
|
||||
float e[LC3_MAX_BANDS];
|
||||
|
||||
lc3_mdct_forward(dt, sr_pcm, sr, xs, xd, xf);
|
||||
|
||||
bool nn_flag = lc3_energy_compute(dt, sr, xf, e);
|
||||
if (nn_flag)
|
||||
lc3_ltpf_disable(&side->ltpf);
|
||||
|
||||
side->bw = lc3_bwdet_run(dt, sr, e);
|
||||
|
||||
lc3_sns_analyze(dt, sr, nbytes, e, att, &side->sns, xf, xf);
|
||||
|
||||
lc3_tns_analyze(dt, side->bw, nn_flag, nbytes, &side->tns, xf);
|
||||
|
||||
lc3_spec_analyze(dt, sr,
|
||||
nbytes, side->pitch_present, &side->tns,
|
||||
&encoder->spec, xf, &side->spec);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode bitstream
|
||||
* encoder Encoder state
|
||||
* side The frame data
|
||||
* nbytes Target size of the frame (20 to 400)
|
||||
* buffer Output bitstream buffer of `nbytes` size
|
||||
*/
|
||||
static void encode(struct lc3_encoder *encoder,
|
||||
const struct side_data *side, int nbytes, void *buffer)
|
||||
{
|
||||
enum lc3_dt dt = encoder->dt;
|
||||
enum lc3_srate sr = encoder->sr;
|
||||
|
||||
float *xf = encoder->x + encoder->xs_off;
|
||||
enum lc3_bandwidth bw = side->bw;
|
||||
|
||||
lc3_bits_t bits;
|
||||
|
||||
lc3_setup_bits(&bits, LC3_BITS_MODE_WRITE, buffer, nbytes);
|
||||
|
||||
lc3_bwdet_put_bw(&bits, sr, bw);
|
||||
|
||||
lc3_spec_put_side(&bits, dt, sr, &side->spec);
|
||||
|
||||
lc3_tns_put_data(&bits, &side->tns);
|
||||
|
||||
lc3_put_bit(&bits, side->pitch_present);
|
||||
|
||||
lc3_sns_put_data(&bits, &side->sns);
|
||||
|
||||
if (side->pitch_present)
|
||||
lc3_ltpf_put_data(&bits, &side->ltpf);
|
||||
|
||||
lc3_spec_encode(&bits, dt, sr, bw, nbytes, &side->spec, xf);
|
||||
|
||||
lc3_flush_bits(&bits);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return size needed for an encoder
|
||||
*/
|
||||
LC3_EXPORT unsigned lc3_hr_encoder_size(bool hrmode, int dt_us, int sr_hz)
|
||||
{
|
||||
if (resolve_dt(dt_us, hrmode) >= LC3_NUM_DT ||
|
||||
resolve_srate(sr_hz, hrmode) >= LC3_NUM_SRATE)
|
||||
return 0;
|
||||
|
||||
return sizeof(struct lc3_encoder) +
|
||||
(LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz)-1) * sizeof(float);
|
||||
}
|
||||
|
||||
LC3_EXPORT unsigned lc3_encoder_size(int dt_us, int sr_hz)
|
||||
{
|
||||
return lc3_hr_encoder_size(false, dt_us, sr_hz);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup encoder
|
||||
*/
|
||||
LC3_EXPORT struct lc3_encoder *lc3_hr_setup_encoder(
|
||||
bool hrmode, int dt_us, int sr_hz, int sr_pcm_hz, void *mem)
|
||||
{
|
||||
if (sr_pcm_hz <= 0)
|
||||
sr_pcm_hz = sr_hz;
|
||||
|
||||
enum lc3_dt dt = resolve_dt(dt_us, hrmode);
|
||||
enum lc3_srate sr = resolve_srate(sr_hz, hrmode);
|
||||
enum lc3_srate sr_pcm = resolve_srate(sr_pcm_hz, hrmode);
|
||||
|
||||
if (dt >= LC3_NUM_DT || sr_pcm >= LC3_NUM_SRATE || sr > sr_pcm || !mem)
|
||||
return NULL;
|
||||
|
||||
struct lc3_encoder *encoder = mem;
|
||||
int ns = lc3_ns(dt, sr_pcm);
|
||||
int nt = lc3_nt(sr_pcm);
|
||||
|
||||
*encoder = (struct lc3_encoder){
|
||||
.dt = dt, .sr = sr,
|
||||
.sr_pcm = sr_pcm,
|
||||
|
||||
.xt_off = nt,
|
||||
.xs_off = (nt + ns) / 2,
|
||||
.xd_off = (nt + ns) / 2 + ns,
|
||||
};
|
||||
|
||||
memset(encoder->x, 0,
|
||||
LC3_ENCODER_BUFFER_COUNT(dt_us, sr_pcm_hz) * sizeof(float));
|
||||
|
||||
return encoder;
|
||||
}
|
||||
|
||||
LC3_EXPORT struct lc3_encoder *lc3_setup_encoder(
|
||||
int dt_us, int sr_hz, int sr_pcm_hz, void *mem)
|
||||
{
|
||||
return lc3_hr_setup_encoder(false, dt_us, sr_hz, sr_pcm_hz, mem);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode a frame
|
||||
*/
|
||||
LC3_EXPORT int lc3_encode(struct lc3_encoder *encoder,
|
||||
enum lc3_pcm_format fmt, const void *pcm, int stride, int nbytes, void *out)
|
||||
{
|
||||
static void (* const load[])(struct lc3_encoder *, const void *, int) = {
|
||||
[LC3_PCM_FORMAT_S16 ] = load_s16,
|
||||
[LC3_PCM_FORMAT_S24 ] = load_s24,
|
||||
[LC3_PCM_FORMAT_S24_3LE] = load_s24_3le,
|
||||
[LC3_PCM_FORMAT_FLOAT ] = load_float,
|
||||
};
|
||||
|
||||
/* --- Check parameters --- */
|
||||
|
||||
if (!encoder || nbytes < lc3_min_frame_bytes(encoder->dt, encoder->sr)
|
||||
|| nbytes > lc3_max_frame_bytes(encoder->dt, encoder->sr))
|
||||
return -1;
|
||||
|
||||
/* --- Processing --- */
|
||||
|
||||
struct side_data side;
|
||||
|
||||
load[fmt](encoder, pcm, stride);
|
||||
|
||||
analyze(encoder, nbytes, &side);
|
||||
|
||||
encode(encoder, &side, nbytes, out);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Decoder
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Output PCM Samples to signed 16 bits
|
||||
* decoder Decoder state
|
||||
* pcm, stride Output PCM samples, and count between two consecutives
|
||||
*/
|
||||
static void store_s16(
|
||||
struct lc3_decoder *decoder, void *_pcm, int stride)
|
||||
{
|
||||
int16_t *pcm = _pcm;
|
||||
|
||||
enum lc3_dt dt = decoder->dt;
|
||||
enum lc3_srate sr = decoder->sr_pcm;
|
||||
|
||||
float *xs = decoder->x + decoder->xs_off;
|
||||
int ns = lc3_ns(dt, sr);
|
||||
|
||||
for ( ; ns > 0; ns--, xs++, pcm += stride) {
|
||||
int32_t s = *xs >= 0 ? (int)(*xs + 0.5f) : (int)(*xs - 0.5f);
|
||||
*pcm = LC3_SAT16(s);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Output PCM Samples to signed 24 bits
|
||||
* decoder Decoder state
|
||||
* pcm, stride Output PCM samples, and count between two consecutives
|
||||
*/
|
||||
static void store_s24(
|
||||
struct lc3_decoder *decoder, void *_pcm, int stride)
|
||||
{
|
||||
int32_t *pcm = _pcm;
|
||||
|
||||
enum lc3_dt dt = decoder->dt;
|
||||
enum lc3_srate sr = decoder->sr_pcm;
|
||||
|
||||
float *xs = decoder->x + decoder->xs_off;
|
||||
int ns = lc3_ns(dt, sr);
|
||||
|
||||
for ( ; ns > 0; ns--, xs++, pcm += stride) {
|
||||
int32_t s = *xs >= 0 ? (int32_t)(lc3_ldexpf(*xs, 8) + 0.5f)
|
||||
: (int32_t)(lc3_ldexpf(*xs, 8) - 0.5f);
|
||||
*pcm = LC3_SAT24(s);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Output PCM Samples to signed 24 bits packed
|
||||
* decoder Decoder state
|
||||
* pcm, stride Output PCM samples, and count between two consecutives
|
||||
*/
|
||||
static void store_s24_3le(
|
||||
struct lc3_decoder *decoder, void *_pcm, int stride)
|
||||
{
|
||||
uint8_t *pcm = _pcm;
|
||||
|
||||
enum lc3_dt dt = decoder->dt;
|
||||
enum lc3_srate sr = decoder->sr_pcm;
|
||||
|
||||
float *xs = decoder->x + decoder->xs_off;
|
||||
int ns = lc3_ns(dt, sr);
|
||||
|
||||
for ( ; ns > 0; ns--, xs++, pcm += 3*stride) {
|
||||
int32_t s = *xs >= 0 ? (int32_t)(lc3_ldexpf(*xs, 8) + 0.5f)
|
||||
: (int32_t)(lc3_ldexpf(*xs, 8) - 0.5f);
|
||||
|
||||
s = LC3_SAT24(s);
|
||||
pcm[0] = (s >> 0) & 0xff;
|
||||
pcm[1] = (s >> 8) & 0xff;
|
||||
pcm[2] = (s >> 16) & 0xff;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Output PCM Samples to float 32 bits
|
||||
* decoder Decoder state
|
||||
* pcm, stride Output PCM samples, and count between two consecutives
|
||||
*/
|
||||
static void store_float(
|
||||
struct lc3_decoder *decoder, void *_pcm, int stride)
|
||||
{
|
||||
float *pcm = _pcm;
|
||||
|
||||
enum lc3_dt dt = decoder->dt;
|
||||
enum lc3_srate sr = decoder->sr_pcm;
|
||||
|
||||
float *xs = decoder->x + decoder->xs_off;
|
||||
int ns = lc3_ns(dt, sr);
|
||||
|
||||
for ( ; ns > 0; ns--, xs++, pcm += stride) {
|
||||
float s = lc3_ldexpf(*xs, -15);
|
||||
*pcm = fminf(fmaxf(s, -1.f), 1.f);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode bitstream
|
||||
* decoder Decoder state
|
||||
* data, nbytes Input bitstream buffer
|
||||
* side Return the side data
|
||||
* return 0: Ok < 0: Bitsream error detected
|
||||
*/
|
||||
static int decode(struct lc3_decoder *decoder,
|
||||
const void *data, int nbytes, struct side_data *side)
|
||||
{
|
||||
enum lc3_dt dt = decoder->dt;
|
||||
enum lc3_srate sr = decoder->sr;
|
||||
|
||||
float *xf = decoder->x + decoder->xs_off;
|
||||
int ns = lc3_ns(dt, sr);
|
||||
int ne = lc3_ne(dt, sr);
|
||||
|
||||
lc3_bits_t bits;
|
||||
int ret = 0;
|
||||
|
||||
lc3_setup_bits(&bits, LC3_BITS_MODE_READ, (void *)data, nbytes);
|
||||
|
||||
if ((ret = lc3_bwdet_get_bw(&bits, sr, &side->bw)) < 0)
|
||||
return ret;
|
||||
|
||||
if ((ret = lc3_spec_get_side(&bits, dt, sr, &side->spec)) < 0)
|
||||
return ret;
|
||||
|
||||
if ((ret = lc3_tns_get_data(&bits, dt, side->bw, nbytes, &side->tns)) < 0)
|
||||
return ret;
|
||||
|
||||
side->pitch_present = lc3_get_bit(&bits);
|
||||
|
||||
if ((ret = lc3_sns_get_data(&bits, &side->sns)) < 0)
|
||||
return ret;
|
||||
|
||||
if (side->pitch_present)
|
||||
lc3_ltpf_get_data(&bits, &side->ltpf);
|
||||
|
||||
if ((ret = lc3_spec_decode(&bits, dt, sr,
|
||||
side->bw, nbytes, &side->spec, xf)) < 0)
|
||||
return ret;
|
||||
|
||||
memset(xf + ne, 0, (ns - ne) * sizeof(float));
|
||||
|
||||
return lc3_check_bits(&bits);
|
||||
}
|
||||
|
||||
/**
|
||||
* Frame synthesis
|
||||
* decoder Decoder state
|
||||
* side Frame data, NULL performs PLC
|
||||
* nbytes Size in bytes of the frame
|
||||
*/
|
||||
static void synthesize(struct lc3_decoder *decoder,
|
||||
const struct side_data *side, int nbytes)
|
||||
{
|
||||
enum lc3_dt dt = decoder->dt;
|
||||
enum lc3_srate sr = decoder->sr;
|
||||
enum lc3_srate sr_pcm = decoder->sr_pcm;
|
||||
|
||||
float *xf = decoder->x + decoder->xs_off;
|
||||
int ns = lc3_ns(dt, sr_pcm);
|
||||
int ne = lc3_ne(dt, sr);
|
||||
|
||||
float *xg = decoder->x + decoder->xg_off;
|
||||
float *xs = xf;
|
||||
|
||||
float *xd = decoder->x + decoder->xd_off;
|
||||
float *xh = decoder->x + decoder->xh_off;
|
||||
|
||||
if (side) {
|
||||
enum lc3_bandwidth bw = side->bw;
|
||||
|
||||
lc3_plc_suspend(&decoder->plc);
|
||||
|
||||
lc3_tns_synthesize(dt, bw, &side->tns, xf);
|
||||
|
||||
lc3_sns_synthesize(dt, sr, &side->sns, xf, xg);
|
||||
|
||||
lc3_mdct_inverse(dt, sr_pcm, sr, xg, xd, xs);
|
||||
|
||||
} else {
|
||||
lc3_plc_synthesize(dt, sr, &decoder->plc, xg, xf);
|
||||
|
||||
memset(xf + ne, 0, (ns - ne) * sizeof(float));
|
||||
|
||||
lc3_mdct_inverse(dt, sr_pcm, sr, xf, xd, xs);
|
||||
}
|
||||
|
||||
if (!lc3_hr(sr))
|
||||
lc3_ltpf_synthesize(dt, sr_pcm, nbytes, &decoder->ltpf,
|
||||
side && side->pitch_present ? &side->ltpf : NULL, xh, xs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update decoder state on decoding completion
|
||||
* decoder Decoder state
|
||||
*/
|
||||
static void complete(struct lc3_decoder *decoder)
|
||||
{
|
||||
enum lc3_dt dt = decoder->dt;
|
||||
enum lc3_srate sr_pcm = decoder->sr_pcm;
|
||||
int nh = lc3_nh(dt, sr_pcm);
|
||||
int ns = lc3_ns(dt, sr_pcm);
|
||||
|
||||
decoder->xs_off = decoder->xs_off - decoder->xh_off < nh ?
|
||||
decoder->xs_off + ns : decoder->xh_off;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return size needed for a decoder
|
||||
*/
|
||||
LC3_EXPORT unsigned lc3_hr_decoder_size(bool hrmode, int dt_us, int sr_hz)
|
||||
{
|
||||
if (resolve_dt(dt_us, hrmode) >= LC3_NUM_DT ||
|
||||
resolve_srate(sr_hz, hrmode) >= LC3_NUM_SRATE)
|
||||
return 0;
|
||||
|
||||
return sizeof(struct lc3_decoder) +
|
||||
(LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz)-1) * sizeof(float);
|
||||
}
|
||||
|
||||
LC3_EXPORT unsigned lc3_decoder_size(int dt_us, int sr_hz)
|
||||
{
|
||||
return lc3_hr_decoder_size(false, dt_us, sr_hz);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup decoder
|
||||
*/
|
||||
LC3_EXPORT struct lc3_decoder *lc3_hr_setup_decoder(
|
||||
bool hrmode, int dt_us, int sr_hz, int sr_pcm_hz, void *mem)
|
||||
{
|
||||
if (sr_pcm_hz <= 0)
|
||||
sr_pcm_hz = sr_hz;
|
||||
|
||||
enum lc3_dt dt = resolve_dt(dt_us, hrmode);
|
||||
enum lc3_srate sr = resolve_srate(sr_hz, hrmode);
|
||||
enum lc3_srate sr_pcm = resolve_srate(sr_pcm_hz, hrmode);
|
||||
|
||||
if (dt >= LC3_NUM_DT || sr_pcm >= LC3_NUM_SRATE || sr > sr_pcm || !mem)
|
||||
return NULL;
|
||||
|
||||
struct lc3_decoder *decoder = mem;
|
||||
int nh = lc3_nh(dt, sr_pcm);
|
||||
int ns = lc3_ns(dt, sr_pcm);
|
||||
int nd = lc3_nd(dt, sr_pcm);
|
||||
|
||||
*decoder = (struct lc3_decoder){
|
||||
.dt = dt, .sr = sr,
|
||||
.sr_pcm = sr_pcm,
|
||||
|
||||
.xh_off = 0,
|
||||
.xs_off = nh,
|
||||
.xd_off = nh + ns,
|
||||
.xg_off = nh + ns + nd,
|
||||
};
|
||||
|
||||
lc3_plc_reset(&decoder->plc);
|
||||
|
||||
memset(decoder->x, 0,
|
||||
LC3_DECODER_BUFFER_COUNT(dt_us, sr_pcm_hz) * sizeof(float));
|
||||
|
||||
return decoder;
|
||||
}
|
||||
|
||||
LC3_EXPORT struct lc3_decoder *lc3_setup_decoder(
|
||||
int dt_us, int sr_hz, int sr_pcm_hz, void *mem)
|
||||
{
|
||||
return lc3_hr_setup_decoder(false, dt_us, sr_hz, sr_pcm_hz, mem);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode a frame
|
||||
*/
|
||||
LC3_EXPORT int lc3_decode(struct lc3_decoder *decoder,
|
||||
const void *in, int nbytes, enum lc3_pcm_format fmt, void *pcm, int stride)
|
||||
{
|
||||
static void (* const store[])(struct lc3_decoder *, void *, int) = {
|
||||
[LC3_PCM_FORMAT_S16 ] = store_s16,
|
||||
[LC3_PCM_FORMAT_S24 ] = store_s24,
|
||||
[LC3_PCM_FORMAT_S24_3LE] = store_s24_3le,
|
||||
[LC3_PCM_FORMAT_FLOAT ] = store_float,
|
||||
};
|
||||
|
||||
/* --- Check parameters --- */
|
||||
|
||||
if (!decoder)
|
||||
return -1;
|
||||
|
||||
if (in && (nbytes < LC3_MIN_FRAME_BYTES ||
|
||||
nbytes > lc3_max_frame_bytes(decoder->dt, decoder->sr) ))
|
||||
return -1;
|
||||
|
||||
/* --- Processing --- */
|
||||
|
||||
struct side_data side;
|
||||
|
||||
int ret = !in || (decode(decoder, in, nbytes, &side) < 0);
|
||||
|
||||
synthesize(decoder, ret ? NULL : &side, nbytes);
|
||||
|
||||
store[fmt](decoder, pcm, stride);
|
||||
|
||||
complete(decoder);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,423 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
/**
|
||||
* Low Complexity Communication Codec (LC3)
|
||||
*
|
||||
* This implementation conforms to :
|
||||
*
|
||||
* - Low Complexity Communication Codec (LC3)
|
||||
* Bluetooth Specification v1.0
|
||||
*
|
||||
* - ETSI TS 103 634 v1.4.1
|
||||
* Digital Enhanced Cordless Telecommunications (DECT)
|
||||
* Low Complexity Communication Codec plus (LC3plus)
|
||||
*
|
||||
* LC3 and LC3 Plus are audio codecs designed for low-latency audio transport.
|
||||
*
|
||||
* - Unlike most other codecs, the LC3 codec is focused on audio streaming
|
||||
* in constrained (on packet sizes and interval) tranport layer.
|
||||
* In this way, the LC3 does not handle :
|
||||
* VBR (Variable Bitrate), based on input signal complexity
|
||||
* ABR (Adaptative Bitrate). It does not rely on any bit reservoir,
|
||||
* a frame will be strictly encoded in the bytes budget given by
|
||||
* the user (or transport layer).
|
||||
*
|
||||
* However, the bitrate (bytes budget for encoding a frame) can be
|
||||
* freely changed at any time. But will not rely on signal complexity,
|
||||
* it can follow a temporary bandwidth increase or reduction.
|
||||
*
|
||||
* - Unlike classic codecs, the LC3 codecs does not run on fixed amount
|
||||
* of samples as input. It operates only on fixed frame duration, for
|
||||
* any supported sample rates (8 to 48 KHz). Two frames duration are
|
||||
* available 7.5ms and 10ms.
|
||||
*
|
||||
*
|
||||
* --- LC3 Plus features ---
|
||||
*
|
||||
* In addition to LC3, following features of LC3 Plus are proposed:
|
||||
* - Frame duration of 2.5 and 5ms.
|
||||
* - High-Resolution mode, 48 KHz, and 96 kHz sampling rates.
|
||||
*
|
||||
* The distinction between LC3 and LC3 plus is made according to :
|
||||
*
|
||||
* Frame Duration | 2.5ms | 5ms | 7.5ms | 10 ms |
|
||||
* ---------------- | ----- | ----- | ----- | ----- |
|
||||
* LC3 | | | X | X |
|
||||
* LC3 Plus | X | X | | X |
|
||||
*
|
||||
* The 10 ms frame duration is available in LC3 and LC3 plus standard.
|
||||
* In this mode, the produced bitstream can be referenced either
|
||||
* as LC3 or LC3 plus.
|
||||
*
|
||||
* The LC3 Plus high-resolution mode should be preferred at high bitrates
|
||||
* and larger audio bandwidth. In this mode, the audio bandwidth is always
|
||||
* up to the Nyquist frequency, compared to LC3 at 48 KHz, which limits
|
||||
* the bandwidth to 20 KHz.
|
||||
*
|
||||
*
|
||||
* --- Bit rate ---
|
||||
*
|
||||
* The proposed implementation accepts any frame sizes between 20 and 400 Bytes
|
||||
* for non-high-resolution mode. Mind that the LC3 Plus standard defines
|
||||
* smaller sizes for frame durations shorter than 10 ms and/or sampling rates
|
||||
* less than 48 kHz.
|
||||
*
|
||||
* In High-Resolution mode, the frame sizes (and bitrates) are restricted
|
||||
* as follows:
|
||||
*
|
||||
* HR Configuration | Frame sizes | Bitrate (kbps) |
|
||||
* ------------------ | ------------- | -------------- |
|
||||
* 10 ms - 48 KHz | 156 to 625 | 124.8 - 500 |
|
||||
* 10 ms - 96 KHz | 187 to 625 | 149.6 - 500 |
|
||||
* ------------------ | ------------- | -------------- |
|
||||
* 5 ms - 48 KHz | 93 to 375 | 148.8 - 600 |
|
||||
* 5 ms - 96 KHz | 109 to 375 | 174.4 - 600 |
|
||||
* ------------------ | ------------- | -------------- |
|
||||
* 2.5 ms - 48 KHz | 54 to 210 | 172.8 - 672 |
|
||||
* 2.5 ms - 96 KHz | 62 to 210 | 198.4 - 672 |
|
||||
*
|
||||
*
|
||||
* --- About 44.1 KHz sample rate ---
|
||||
*
|
||||
* The Bluetooth specification and the ETSI TS 103 634 standard references
|
||||
* the 44.1 KHz sample rate, although there is no support in the core algorithm
|
||||
* of the codec.
|
||||
* We can summarize the 44.1 KHz support by "You can put any sample rate around
|
||||
* the defined base sample rates." Please mind the following items :
|
||||
*
|
||||
* 1. The frame size will not be 2.5ms, 5ms, 7.5 ms or 10 ms, but is scaled
|
||||
* by 'supported sample rate' / 'input sample rate'
|
||||
*
|
||||
* 2. The bandwidth will be hard limited (to 20 KHz) if you select 48 KHz.
|
||||
* The encoded bandwidth will also be affected by the above inverse
|
||||
* factor of 20 KHz.
|
||||
*
|
||||
* Applied to 44.1 KHz, we get :
|
||||
*
|
||||
* 1. About 8.16 ms frame duration, instead of 7.5 ms
|
||||
* About 10.88 ms frame duration, instead of 10 ms
|
||||
*
|
||||
* 2. The bandwidth becomes limited to 18.375 KHz
|
||||
*
|
||||
*
|
||||
* --- How to encode / decode ---
|
||||
*
|
||||
* An encoder / decoder context needs to be setup. This context keeps states
|
||||
* on the current stream to proceed, and samples that overlapped across
|
||||
* frames.
|
||||
*
|
||||
* You have two ways to setup the encoder / decoder :
|
||||
*
|
||||
* - Using static memory allocation (this module does not rely on
|
||||
* any dynamic memory allocation). The types `lc3_xxcoder_mem_16k_t`,
|
||||
* and `lc3_xxcoder_mem_48k_t` have size of the memory needed for
|
||||
* encoding up to 16 KHz or 48 KHz.
|
||||
*
|
||||
* - Using dynamic memory allocation. The `lc3_xxcoder_size()` procedure
|
||||
* returns the needed memory size, for a given configuration. The memory
|
||||
* space must be aligned to a pointer size. As an example, you can setup
|
||||
* encoder like this :
|
||||
*
|
||||
* | enc = lc3_setup_encoder(frame_us, sample rate,
|
||||
* | malloc(lc3_encoder_size(frame_us, sample rate)));
|
||||
* | ...
|
||||
* | free(enc);
|
||||
*
|
||||
* Note :
|
||||
* - A NULL memory adress as input, will return a NULL encoder context.
|
||||
* - The returned encoder handle is set at the address of the allocated
|
||||
* memory space, you can directly free the handle.
|
||||
*
|
||||
* Next, call the `lc3_encode()` encoding procedure, for each frames.
|
||||
* To handle multichannel streams (Stereo or more), you can proceed with
|
||||
* interleaved channels PCM stream like this :
|
||||
*
|
||||
* | for(int ich = 0; ich < nch: ich++)
|
||||
* | lc3_encode(encoder[ich], pcm + ich, nch, ...);
|
||||
*
|
||||
* with `nch` as the number of channels in the PCM stream
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* Antoine SOULIER, Tempow / Google LLC
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __LC3_H
|
||||
#define __LC3_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "lc3_private.h"
|
||||
|
||||
|
||||
/**
|
||||
* Limitations
|
||||
* - On the bitrate, in bps
|
||||
* - On the size of the frames in bytes
|
||||
* - On the number of samples by frames
|
||||
*/
|
||||
|
||||
#define LC3_MIN_BITRATE 16000
|
||||
#define LC3_MAX_BITRATE 320000
|
||||
#define LC3_HR_MAX_BITRATE 672000
|
||||
|
||||
#define LC3_MIN_FRAME_BYTES 20
|
||||
#define LC3_MAX_FRAME_BYTES 400
|
||||
#define LC3_HR_MAX_FRAME_BYTES 625
|
||||
|
||||
#define LC3_MIN_FRAME_SAMPLES LC3_NS( 2500, 8000)
|
||||
#define LC3_MAX_FRAME_SAMPLES LC3_NS(10000, 48000)
|
||||
#define LC3_HR_MAX_FRAME_SAMPLES LC3_NS(10000, 96000)
|
||||
|
||||
|
||||
/**
|
||||
* Parameters check
|
||||
* LC3_CHECK_DT_US(us) True when frame duration in us is suitable
|
||||
* LC3_CHECK_SR_HZ(sr) True when sample rate in Hz is suitable
|
||||
*
|
||||
* LC3_HR_CHECK_SR_HZ(hrmode, sr)
|
||||
* True when sample rate in Hz is suitable, according to the
|
||||
* selection of the high-resolution mode `hrmode`.
|
||||
*/
|
||||
|
||||
#define LC3_CHECK_DT_US(us) \
|
||||
( ((us) == 2500) || ((us) == 5000) || \
|
||||
((us) == 7500) || ((us) == 10000) )
|
||||
|
||||
#define LC3_CHECK_SR_HZ(sr) \
|
||||
( ((sr) == 8000) || ((sr) == 16000) || ((sr) == 24000) || \
|
||||
((sr) == 32000) || ((sr) == 48000) )
|
||||
|
||||
#define LC3_HR_CHECK_SR_HZ(hrmode, sr) \
|
||||
( (hrmode) ? ((sr) == 48000) || ((sr) == 96000) : LC3_CHECK_SR_HZ(sr) )
|
||||
|
||||
|
||||
/**
|
||||
* PCM Sample Format
|
||||
* S16 Signed 16 bits, in 16 bits words (int16_t)
|
||||
* S24 Signed 24 bits, using low three bytes of 32 bits words (int32_t).
|
||||
* The high byte sign extends (bits 31..24 set to b23).
|
||||
* S24_3LE Signed 24 bits packed in 3 bytes little endian
|
||||
* FLOAT Floating point 32 bits (float type), in range -1 to 1
|
||||
*/
|
||||
|
||||
enum lc3_pcm_format {
|
||||
LC3_PCM_FORMAT_S16,
|
||||
LC3_PCM_FORMAT_S24,
|
||||
LC3_PCM_FORMAT_S24_3LE,
|
||||
LC3_PCM_FORMAT_FLOAT,
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Handle
|
||||
*/
|
||||
|
||||
typedef struct lc3_encoder *lc3_encoder_t;
|
||||
typedef struct lc3_decoder *lc3_decoder_t;
|
||||
|
||||
|
||||
/**
|
||||
* Static memory of encoder/decoder contexts
|
||||
*
|
||||
* Propose types suitable for static memory allocation, supporting
|
||||
* any frame duration, and maximum sample rates 16k and 48k respectively
|
||||
* You can customize your type using the `LC3_ENCODER_MEM_T` or
|
||||
* `LC3_DECODER_MEM_T` macro.
|
||||
*/
|
||||
|
||||
typedef LC3_ENCODER_MEM_T(10000, 16000) lc3_encoder_mem_16k_t;
|
||||
typedef LC3_ENCODER_MEM_T(10000, 48000) lc3_encoder_mem_48k_t;
|
||||
|
||||
typedef LC3_DECODER_MEM_T(10000, 16000) lc3_decoder_mem_16k_t;
|
||||
typedef LC3_DECODER_MEM_T(10000, 48000) lc3_decoder_mem_48k_t;
|
||||
|
||||
|
||||
/**
|
||||
* Return the number of PCM samples in a frame
|
||||
* hrmode Enable High-Resolution mode (48000 and 96000 sample rates)
|
||||
* dt_us Frame duration in us, 2500, 5000, 7500 or 10000
|
||||
* sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
|
||||
* return Number of PCM samples, -1 on bad parameters
|
||||
*/
|
||||
LC3_EXPORT int lc3_hr_frame_samples(bool hrmode, int dt_us, int sr_hz);
|
||||
|
||||
LC3_EXPORT int lc3_frame_samples(int dt_us, int sr_hz);
|
||||
|
||||
/**
|
||||
* Return the size of frames, from bitrate
|
||||
* hrmode Enable High-Resolution mode (48000 and 96000 sample rates)
|
||||
* dt_us Frame duration in us, 2500, 5000, 7500 or 10000
|
||||
* sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
|
||||
* bitrate Target bitrate in bit per second, 0 or `INT_MAX` returns
|
||||
* respectively the minimum and maximum allowed size.
|
||||
* return The floor size in bytes of the frames, -1 on bad parameters
|
||||
*/
|
||||
LC3_EXPORT int lc3_hr_frame_bytes(
|
||||
bool hrmode, int dt_us, int sr_hz, int bitrate);
|
||||
|
||||
LC3_EXPORT int lc3_frame_bytes(int dt_us, int bitrate);
|
||||
|
||||
/**
|
||||
* Return the size of frame blocks, from bitrate
|
||||
* A frame block contains the frame data from all channels.
|
||||
* hrmode Enable High-Resolution mode (48000 and 96000 sample rates)
|
||||
* dt_us Frame duration in us, 2500, 5000, 7500 or 10000
|
||||
* sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
|
||||
* nchannels The number of channels (or frames) in the block (<= 8)
|
||||
* bitrate Target bitrate in bit per second, 0 or `INT_MAX` returns
|
||||
* respectively the minimum and maximum allowed size.
|
||||
* return The floor size in bytes of the frames, -1 on bad parameters
|
||||
*/
|
||||
LC3_EXPORT int lc3_hr_frame_block_bytes(
|
||||
bool hrmode, int dt_us, int sr_hz, int nchannels, int bitrate);
|
||||
|
||||
LC3_EXPORT int lc3_frame_block_bytes(int dt_us, int nframes, int bitrate);
|
||||
|
||||
/**
|
||||
* Resolve the bitrate, from the size of frames
|
||||
* hrmode Enable High-Resolution mode (48000 and 96000 sample rates)
|
||||
* dt_us Frame duration in us, 2500, 5000, 7500 or 10000
|
||||
* sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
|
||||
* nbytes Size in bytes of the frames or frame blocks
|
||||
* return The ceiled bitrate in bps, -1 on bad parameters
|
||||
*/
|
||||
LC3_EXPORT int lc3_hr_resolve_bitrate(
|
||||
bool hrmode, int dt_us, int sr_hz, int nbytes);
|
||||
|
||||
LC3_EXPORT int lc3_resolve_bitrate(int dt_us, int nbytes);
|
||||
|
||||
/**
|
||||
* Return algorithmic delay, as a number of samples
|
||||
* hrmode Enable High-Resolution mode (48000 and 96000 sample rates)
|
||||
* dt_us Frame duration in us, 2500, 5000, 7500 or 10000
|
||||
* sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
|
||||
* return Number of algorithmic delay samples, -1 on bad parameters
|
||||
*/
|
||||
LC3_EXPORT int lc3_hr_delay_samples(bool hrmode, int dt_us, int sr_hz);
|
||||
|
||||
LC3_EXPORT int lc3_delay_samples(int dt_us, int sr_hz);
|
||||
|
||||
/**
|
||||
* Return size needed for an encoder
|
||||
* hrmode Enable High-Resolution mode (48000 and 96000 sample rates)
|
||||
* dt_us Frame duration in us, 2500, 5000, 7500 or 10000
|
||||
* sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
|
||||
* return Size of then encoder in bytes, 0 on bad parameters
|
||||
*
|
||||
* The `sr_hz` parameter is the sample rate of the PCM input stream,
|
||||
* and will match `sr_pcm_hz` of `lc3_hr_setup_encoder()`.
|
||||
*/
|
||||
LC3_EXPORT unsigned lc3_hr_encoder_size(bool hrmode, int dt_us, int sr_hz);
|
||||
|
||||
LC3_EXPORT unsigned lc3_encoder_size(int dt_us, int sr_hz);
|
||||
|
||||
/**
|
||||
* Setup encoder
|
||||
* hrmode Enable High-Resolution mode (48000 and 96000 sample rates)
|
||||
* dt_us Frame duration in us, 2500, 5000, 7500 or 10000
|
||||
* sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
|
||||
* sr_pcm_hz Input sample rate, downsampling option of input, or 0
|
||||
* mem Encoder memory space, aligned to pointer type
|
||||
* return Encoder as an handle, NULL on bad parameters
|
||||
*
|
||||
* The `sr_pcm_hz` parameter is a downsampling option of PCM input,
|
||||
* the value `0` fallback to the sample rate of the encoded stream `sr_hz`.
|
||||
* When used, `sr_pcm_hz` is intended to be higher or equal to the encoder
|
||||
* sample rate `sr_hz`. The size of the context needed, given by
|
||||
* `lc3_hr_encoder_size()` will be set accordingly to `sr_pcm_hz`.
|
||||
*/
|
||||
LC3_EXPORT lc3_encoder_t lc3_hr_setup_encoder(
|
||||
bool hrmode, int dt_us, int sr_hz, int sr_pcm_hz, void *mem);
|
||||
|
||||
LC3_EXPORT lc3_encoder_t lc3_setup_encoder(
|
||||
int dt_us, int sr_hz, int sr_pcm_hz, void *mem);
|
||||
|
||||
/**
|
||||
* Encode a frame
|
||||
* encoder Handle of the encoder
|
||||
* fmt PCM input format
|
||||
* pcm, stride Input PCM samples, and count between two consecutives
|
||||
* nbytes Target size, in bytes, of the frame
|
||||
* out Output buffer of `nbytes` size
|
||||
* return 0: On success -1: Wrong parameters
|
||||
*/
|
||||
LC3_EXPORT int lc3_encode(
|
||||
lc3_encoder_t encoder, enum lc3_pcm_format fmt,
|
||||
const void *pcm, int stride, int nbytes, void *out);
|
||||
|
||||
/**
|
||||
* Return size needed for an decoder
|
||||
* hrmode Enable High-Resolution mode (48000 and 96000 sample rates)
|
||||
* dt_us Frame duration in us, 2500, 5000, 7500 or 10000
|
||||
* sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
|
||||
* return Size of then decoder in bytes, 0 on bad parameters
|
||||
*
|
||||
* The `sr_hz` parameter is the sample rate of the PCM output stream,
|
||||
* and will match `sr_pcm_hz` of `lc3_hr_setup_decoder()`.
|
||||
*/
|
||||
LC3_EXPORT unsigned lc3_hr_decoder_size(bool hrmode, int dt_us, int sr_hz);
|
||||
|
||||
LC3_EXPORT unsigned lc3_decoder_size(int dt_us, int sr_hz);
|
||||
|
||||
/**
|
||||
* Setup decoder
|
||||
* hrmode Enable High-Resolution mode (48000 and 96000 sample rates)
|
||||
* dt_us Frame duration in us, 2500, 5000, 7500 or 10000
|
||||
* sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
|
||||
* sr_pcm_hz Output sample rate, upsampling option of output (or 0)
|
||||
* mem Decoder memory space, aligned to pointer type
|
||||
* return Decoder as an handle, NULL on bad parameters
|
||||
*
|
||||
* The `sr_pcm_hz` parameter is an upsampling option of PCM output,
|
||||
* the value `0` fallback to the sample rate of the decoded stream `sr_hz`.
|
||||
* When used, `sr_pcm_hz` is intended to be higher or equal to the decoder
|
||||
* sample rate `sr_hz`. The size of the context needed, given by
|
||||
* `lc3_hr_decoder_size()` will be set accordingly to `sr_pcm_hz`.
|
||||
*/
|
||||
LC3_EXPORT lc3_decoder_t lc3_hr_setup_decoder(
|
||||
bool hrmode, int dt_us, int sr_hz, int sr_pcm_hz, void *mem);
|
||||
|
||||
LC3_EXPORT lc3_decoder_t lc3_setup_decoder(
|
||||
int dt_us, int sr_hz, int sr_pcm_hz, void *mem);
|
||||
|
||||
/**
|
||||
* Decode a frame
|
||||
* decoder Handle of the decoder
|
||||
* in, nbytes Input bitstream, and size in bytes, NULL performs PLC
|
||||
* fmt PCM output format
|
||||
* pcm, stride Output PCM samples, and count between two consecutives
|
||||
* return 0: On success 1: PLC operated -1: Wrong parameters
|
||||
*/
|
||||
LC3_EXPORT int lc3_decode(
|
||||
lc3_decoder_t decoder, const void *in, int nbytes,
|
||||
enum lc3_pcm_format fmt, void *pcm, int stride);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __LC3_H */
|
||||
@@ -0,0 +1,310 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
/**
|
||||
* Low Complexity Communication Codec (LC3) - C++ interface
|
||||
*/
|
||||
|
||||
#ifndef __LC3_CPP_H
|
||||
#define __LC3_CPP_H
|
||||
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lc3.h"
|
||||
|
||||
namespace lc3 {
|
||||
|
||||
// PCM Sample Format
|
||||
// - Signed 16 bits, in 16 bits words (int16_t)
|
||||
// - Signed 24 bits, using low three bytes of 32 bits words (int32_t)
|
||||
// The high byte sign extends (bits 31..24 set to b23)
|
||||
// - Signed 24 bits packed in 3 bytes little endian
|
||||
// - Floating point 32 bits (float type), in range -1 to 1
|
||||
|
||||
enum class PcmFormat {
|
||||
kS16 = LC3_PCM_FORMAT_S16,
|
||||
kS24 = LC3_PCM_FORMAT_S24,
|
||||
kS24In3Le = LC3_PCM_FORMAT_S24_3LE,
|
||||
kF32 = LC3_PCM_FORMAT_FLOAT
|
||||
};
|
||||
|
||||
// Base Encoder/Decoder Class
|
||||
template <typename T>
|
||||
class Base {
|
||||
protected:
|
||||
Base(int dt_us, int sr_hz, int sr_pcm_hz, size_t nchannels, bool hrmode)
|
||||
: dt_us_(dt_us),
|
||||
sr_hz_(sr_hz),
|
||||
sr_pcm_hz_(sr_pcm_hz == 0 ? sr_hz : sr_pcm_hz),
|
||||
nchannels_(nchannels),
|
||||
hrmode_(hrmode) {
|
||||
states.reserve(nchannels_);
|
||||
}
|
||||
|
||||
virtual ~Base() = default;
|
||||
|
||||
int dt_us_, sr_hz_;
|
||||
int sr_pcm_hz_;
|
||||
size_t nchannels_;
|
||||
bool hrmode_;
|
||||
|
||||
using state_ptr = std::unique_ptr<T, decltype(&free)>;
|
||||
std::vector<state_ptr> states;
|
||||
|
||||
public:
|
||||
// Return the number of PCM samples in a frame
|
||||
int GetFrameSamples() {
|
||||
return lc3_hr_frame_samples(hrmode_, dt_us_, sr_pcm_hz_); }
|
||||
|
||||
// Return the size of a frame block, from bitrate
|
||||
int GetFrameBytes(int bitrate) {
|
||||
return lc3_hr_frame_block_bytes(
|
||||
hrmode_, dt_us_, sr_hz_, nchannels_, bitrate); }
|
||||
|
||||
// Resolve the bitrate, from the size of frame blocks
|
||||
int ResolveBitrate(int nbytes) {
|
||||
return lc3_hr_resolve_bitrate(hrmode_, dt_us_, sr_hz_, nbytes); }
|
||||
|
||||
// Return algorithmic delay, as a number of samples
|
||||
int GetDelaySamples() {
|
||||
return lc3_hr_delay_samples(hrmode_, dt_us_, sr_pcm_hz_); }
|
||||
|
||||
}; // class Base
|
||||
|
||||
// Encoder Class
|
||||
class Encoder : public Base<struct lc3_encoder> {
|
||||
template <typename T>
|
||||
int EncodeImpl(PcmFormat fmt, const T *pcm, int block_size, uint8_t *out) {
|
||||
if (states.size() != nchannels_) return -1;
|
||||
|
||||
enum lc3_pcm_format cfmt = static_cast<lc3_pcm_format>(fmt);
|
||||
int ret = 0;
|
||||
|
||||
uint8_t *out_ptr = out;
|
||||
for (size_t ich = 0; ich < nchannels_; ich++) {
|
||||
int frame_size = block_size / nchannels_
|
||||
+ (ich < block_size % nchannels_);
|
||||
|
||||
ret |= lc3_encode(states[ich].get(), cfmt, pcm + ich, nchannels_,
|
||||
frame_size, out_ptr);
|
||||
|
||||
out_ptr += frame_size;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public:
|
||||
// Encoder construction / destruction
|
||||
//
|
||||
// The frame duration `dt_us` is 2500, 5000, 7500 or 10000 us.
|
||||
// The sample rate `sr_hz` is 8000, 16000, 24000, 32000 or 48000 Hz.
|
||||
// The `hrmode` flag enables the high-resolution mode, in which case
|
||||
// the sample rate is 48000 or 96000 Hz.
|
||||
//
|
||||
// The `sr_pcm_hz` parameter is a downsampling option of PCM input,
|
||||
// the value 0 fallback to the sample rate of the encoded stream `sr_hz`.
|
||||
// When used, `sr_pcm_hz` is intended to be higher or equal to the encoder
|
||||
// sample rate `sr_hz`.
|
||||
|
||||
Encoder(int dt_us, int sr_hz, int sr_pcm_hz = 0,
|
||||
size_t nchannels = 1, bool hrmode = false)
|
||||
: Base(dt_us, sr_hz, sr_pcm_hz, nchannels, hrmode) {
|
||||
for (size_t ich = 0; ich < nchannels_; ich++) {
|
||||
auto s = state_ptr((lc3_encoder_t)
|
||||
malloc(lc3_hr_encoder_size(hrmode_, dt_us_, sr_pcm_hz_)), free);
|
||||
|
||||
if (lc3_hr_setup_encoder(hrmode_, dt_us_, sr_hz_, sr_pcm_hz_, s.get()))
|
||||
states.push_back(std::move(s));
|
||||
}
|
||||
}
|
||||
|
||||
~Encoder() override = default;
|
||||
|
||||
// Reset encoder state
|
||||
|
||||
void Reset() {
|
||||
for (auto &s : states)
|
||||
lc3_hr_setup_encoder(hrmode_, dt_us_, sr_hz_, sr_pcm_hz_, s.get());
|
||||
}
|
||||
|
||||
// Encode
|
||||
//
|
||||
// The input PCM samples are given in signed 16 bits, 24 bits, float,
|
||||
// according the type of `pcm` input buffer, or by selecting a format.
|
||||
//
|
||||
// The PCM samples are read in interleaved way, and consecutive
|
||||
// `nchannels` frames, are output in `out` buffer, of size `buffer_size`.
|
||||
//
|
||||
// The value returned is 0 on successs, -1 otherwise.
|
||||
|
||||
int Encode(const int16_t *pcm, int block_size, uint8_t *out) {
|
||||
return EncodeImpl(PcmFormat::kS16, pcm, block_size, out);
|
||||
}
|
||||
|
||||
int Encode(const int32_t *pcm, int block_size, uint8_t *out) {
|
||||
return EncodeImpl(PcmFormat::kS24, pcm, block_size, out);
|
||||
}
|
||||
|
||||
int Encode(const float *pcm, int block_size, uint8_t *out) {
|
||||
return EncodeImpl(PcmFormat::kF32, pcm, block_size, out);
|
||||
}
|
||||
|
||||
int Encode(PcmFormat fmt, const void *pcm, int block_size, uint8_t *out) {
|
||||
uintptr_t pcm_ptr = reinterpret_cast<uintptr_t>(pcm);
|
||||
|
||||
switch (fmt) {
|
||||
case PcmFormat::kS16:
|
||||
assert(pcm_ptr % alignof(int16_t) == 0);
|
||||
return EncodeImpl(fmt, reinterpret_cast<const int16_t *>(pcm),
|
||||
block_size, out);
|
||||
|
||||
case PcmFormat::kS24:
|
||||
assert(pcm_ptr % alignof(int32_t) == 0);
|
||||
return EncodeImpl(fmt, reinterpret_cast<const int32_t *>(pcm),
|
||||
block_size, out);
|
||||
|
||||
case PcmFormat::kS24In3Le:
|
||||
return EncodeImpl(fmt, reinterpret_cast<const int8_t(*)[3]>(pcm),
|
||||
block_size, out);
|
||||
|
||||
case PcmFormat::kF32:
|
||||
assert(pcm_ptr % alignof(float) == 0);
|
||||
return EncodeImpl(fmt, reinterpret_cast<const float *>(pcm), block_size,
|
||||
out);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
}; // class Encoder
|
||||
|
||||
// Decoder Class
|
||||
class Decoder : public Base<struct lc3_decoder> {
|
||||
template <typename T>
|
||||
int DecodeImpl(const uint8_t *in, int block_size, PcmFormat fmt, T *pcm) {
|
||||
if (states.size() != nchannels_) return -1;
|
||||
|
||||
enum lc3_pcm_format cfmt = static_cast<enum lc3_pcm_format>(fmt);
|
||||
int ret = 0;
|
||||
|
||||
const uint8_t *in_ptr = in;
|
||||
for (size_t ich = 0; ich < nchannels_; ich++) {
|
||||
int frame_size = block_size / nchannels_
|
||||
+ (ich < block_size % nchannels_);
|
||||
|
||||
ret |= lc3_decode(states[ich].get(), in_ptr, frame_size,
|
||||
cfmt, pcm + ich, nchannels_);
|
||||
|
||||
in_ptr += frame_size;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public:
|
||||
// Decoder construction / destruction
|
||||
//
|
||||
// The frame duration `dt_us` is 2500, 5000, 7500 or 10000 us.
|
||||
// The sample rate `sr_hz` is 8000, 16000, 24000, 32000 or 48000 Hz.
|
||||
// The `hrmode` flag enables the high-resolution mode, in which case
|
||||
// the sample rate is 48000 or 96000 Hz.
|
||||
//
|
||||
// The `sr_pcm_hz` parameter is an downsampling option of PCM output,
|
||||
// the value 0 fallback to the sample rate of the decoded stream `sr_hz`.
|
||||
// When used, `sr_pcm_hz` is intended to be higher or equal to the decoder
|
||||
// sample rate `sr_hz`.
|
||||
|
||||
Decoder(int dt_us, int sr_hz, int sr_pcm_hz = 0,
|
||||
size_t nchannels = 1, bool hrmode = false)
|
||||
: Base(dt_us, sr_hz, sr_pcm_hz, nchannels, hrmode) {
|
||||
for (size_t i = 0; i < nchannels_; i++) {
|
||||
auto s = state_ptr((lc3_decoder_t)
|
||||
malloc(lc3_hr_decoder_size(hrmode_, dt_us_, sr_pcm_hz_)), free);
|
||||
|
||||
if (lc3_hr_setup_decoder(hrmode_, dt_us_, sr_hz_, sr_pcm_hz_, s.get()))
|
||||
states.push_back(std::move(s));
|
||||
}
|
||||
}
|
||||
|
||||
~Decoder() override = default;
|
||||
|
||||
// Reset decoder state
|
||||
|
||||
void Reset() {
|
||||
for (auto &s : states)
|
||||
lc3_hr_setup_decoder(hrmode_, dt_us_, sr_hz_, sr_pcm_hz_, s.get());
|
||||
}
|
||||
|
||||
// Decode
|
||||
//
|
||||
// Decode a frame block of size `block_size`,
|
||||
// in the `pcm` buffer in interleaved way.
|
||||
//
|
||||
// The PCM samples are output in signed 16 bits, 24 bits, float,
|
||||
// according the type of `pcm` output buffer, or by selecting a format.
|
||||
//
|
||||
// The value returned is 0 on successs, 1 when PLC has been performed,
|
||||
// and -1 otherwise.
|
||||
|
||||
int Decode(const uint8_t *in, int block_size, int16_t *pcm) {
|
||||
return DecodeImpl(in, block_size, PcmFormat::kS16, pcm);
|
||||
}
|
||||
|
||||
int Decode(const uint8_t *in, int block_size, int32_t *pcm) {
|
||||
return DecodeImpl(in, block_size, PcmFormat::kS24In3Le, pcm);
|
||||
}
|
||||
|
||||
int Decode(const uint8_t *in, int block_size, float *pcm) {
|
||||
return DecodeImpl(in, block_size, PcmFormat::kF32, pcm);
|
||||
}
|
||||
|
||||
int Decode(const uint8_t *in, int block_size, PcmFormat fmt, void *pcm) {
|
||||
uintptr_t pcm_ptr = reinterpret_cast<uintptr_t>(pcm);
|
||||
|
||||
switch (fmt) {
|
||||
case PcmFormat::kS16:
|
||||
assert(pcm_ptr % alignof(int16_t) == 0);
|
||||
return DecodeImpl(in, block_size, fmt,
|
||||
reinterpret_cast<int16_t *>(pcm));
|
||||
|
||||
case PcmFormat::kS24:
|
||||
assert(pcm_ptr % alignof(int32_t) == 0);
|
||||
return DecodeImpl(in, block_size, fmt,
|
||||
reinterpret_cast<int32_t *>(pcm));
|
||||
|
||||
case PcmFormat::kS24In3Le:
|
||||
return DecodeImpl(in, block_size, fmt,
|
||||
reinterpret_cast<int8_t(*)[3]>(pcm));
|
||||
|
||||
case PcmFormat::kF32:
|
||||
assert(pcm_ptr % alignof(float) == 0);
|
||||
return DecodeImpl(in, block_size, fmt, reinterpret_cast<float *>(pcm));
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
}; // class Decoder
|
||||
|
||||
} // namespace lc3
|
||||
|
||||
#endif /* __LC3_CPP_H */
|
||||
@@ -0,0 +1,187 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __LC3_PRIVATE_H
|
||||
#define __LC3_PRIVATE_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
/**
|
||||
* Characteristics
|
||||
*
|
||||
* - The number of samples within a frame
|
||||
*
|
||||
* - The number of MDCT delayed samples, sum of half a frame and
|
||||
* an ovelap of future by 1.25 ms (2.5ms, 5ms and 10ms frame durations)
|
||||
* or 2 ms (7.5ms frame duration).
|
||||
*
|
||||
* - For decoding, keep 18 ms of history, aligned on a frame
|
||||
*
|
||||
* - For encoding, keep 1.25 ms of temporal previous samples
|
||||
*/
|
||||
|
||||
#define LC3_NS(dt_us, sr_hz) \
|
||||
( (dt_us) * (sr_hz) / 1000 / 1000 )
|
||||
|
||||
#define LC3_ND(dt_us, sr_hz) \
|
||||
( LC3_NS(dt_us, sr_hz) / 2 + \
|
||||
LC3_NS((dt_us) == 7500 ? 2000 : 1250, sr_hz) )
|
||||
|
||||
#define LC3_NH(dt_us, sr_hz) \
|
||||
( (sr_hz) > 48000 ? 0 : ( LC3_NS(18000, sr_hz) + \
|
||||
LC3_NS(dt_us, sr_hz) - (LC3_NS(18000, sr_hz) % LC3_NS(dt_us, sr_hz)) ) )
|
||||
|
||||
#define LC3_NT(sr_hz) \
|
||||
( LC3_NS(1250, sr_hz) )
|
||||
|
||||
|
||||
/**
|
||||
* Frame duration
|
||||
*/
|
||||
|
||||
enum lc3_dt {
|
||||
LC3_DT_2M5 = 0,
|
||||
LC3_DT_5M = 1,
|
||||
LC3_DT_7M5 = 2,
|
||||
LC3_DT_10M = 3,
|
||||
|
||||
LC3_NUM_DT
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Sampling frequency and high-resolution mode
|
||||
*/
|
||||
|
||||
enum lc3_srate {
|
||||
LC3_SRATE_8K,
|
||||
LC3_SRATE_16K,
|
||||
LC3_SRATE_24K,
|
||||
LC3_SRATE_32K,
|
||||
LC3_SRATE_48K,
|
||||
LC3_SRATE_48K_HR,
|
||||
LC3_SRATE_96K_HR,
|
||||
|
||||
LC3_NUM_SRATE
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Encoder state and memory
|
||||
*/
|
||||
|
||||
typedef struct lc3_attdet_analysis {
|
||||
int32_t en1, an1;
|
||||
int p_att;
|
||||
} lc3_attdet_analysis_t;
|
||||
|
||||
struct lc3_ltpf_hp50_state {
|
||||
int64_t s1, s2;
|
||||
};
|
||||
|
||||
typedef struct lc3_ltpf_analysis {
|
||||
bool active;
|
||||
int pitch;
|
||||
float nc[2];
|
||||
|
||||
struct lc3_ltpf_hp50_state hp50;
|
||||
int16_t x_12k8[384];
|
||||
int16_t x_6k4[178];
|
||||
int tc;
|
||||
} lc3_ltpf_analysis_t;
|
||||
|
||||
typedef struct lc3_spec_analysis {
|
||||
float nbits_off;
|
||||
int nbits_spare;
|
||||
} lc3_spec_analysis_t;
|
||||
|
||||
struct lc3_encoder {
|
||||
enum lc3_dt dt;
|
||||
enum lc3_srate sr, sr_pcm;
|
||||
|
||||
lc3_attdet_analysis_t attdet;
|
||||
lc3_ltpf_analysis_t ltpf;
|
||||
lc3_spec_analysis_t spec;
|
||||
|
||||
int xt_off, xs_off, xd_off;
|
||||
float x[1];
|
||||
};
|
||||
|
||||
#define LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz) \
|
||||
( ( LC3_NS(dt_us, sr_hz) + LC3_NT(sr_hz) ) / 2 + \
|
||||
LC3_NS(dt_us, sr_hz) + LC3_ND(dt_us, sr_hz) )
|
||||
|
||||
#define LC3_ENCODER_MEM_T(dt_us, sr_hz) \
|
||||
struct { \
|
||||
struct lc3_encoder __e; \
|
||||
float __x[LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz)-1]; \
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decoder state and memory
|
||||
*/
|
||||
|
||||
typedef struct lc3_ltpf_synthesis {
|
||||
bool active;
|
||||
int pitch;
|
||||
float c[2*12], x[12];
|
||||
} lc3_ltpf_synthesis_t;
|
||||
|
||||
typedef struct lc3_plc_state {
|
||||
uint16_t seed;
|
||||
int count;
|
||||
float alpha;
|
||||
} lc3_plc_state_t;
|
||||
|
||||
struct lc3_decoder {
|
||||
enum lc3_dt dt;
|
||||
enum lc3_srate sr, sr_pcm;
|
||||
|
||||
lc3_ltpf_synthesis_t ltpf;
|
||||
lc3_plc_state_t plc;
|
||||
|
||||
int xh_off, xs_off, xd_off, xg_off;
|
||||
float x[1];
|
||||
};
|
||||
|
||||
#define LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz) \
|
||||
( LC3_NH(dt_us, sr_hz) + LC3_NS(dt_us, sr_hz) + \
|
||||
LC3_ND(dt_us, sr_hz) + LC3_NS(dt_us, sr_hz) )
|
||||
|
||||
#define LC3_DECODER_MEM_T(dt_us, sr_hz) \
|
||||
struct { \
|
||||
struct lc3_decoder __d; \
|
||||
float __x[LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz)-1]; \
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Change the visibility of interface functions
|
||||
*/
|
||||
|
||||
#ifdef _WIN32
|
||||
#define LC3_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define LC3_EXPORT __attribute__((visibility ("default")))
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* __LC3_PRIVATE_H */
|
||||
@@ -0,0 +1,964 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#include "ltpf.h"
|
||||
#include "tables.h"
|
||||
|
||||
#include "ltpf_neon.h"
|
||||
#include "ltpf_arm.h"
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Resampling
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Resampling coefficients
|
||||
* The coefficients, in fixed Q15, are reordered by phase for each source
|
||||
* samplerate (coefficient matrix transposed)
|
||||
*/
|
||||
|
||||
#ifndef resample_8k_12k8
|
||||
static const int16_t h_8k_12k8_q15[8*10] = {
|
||||
214, 417, -1052, -4529, 26233, -4529, -1052, 417, 214, 0,
|
||||
180, 0, -1522, -2427, 24506, -5289, 0, 763, 156, -28,
|
||||
92, -323, -1361, 0, 19741, -3885, 1317, 861, 0, -61,
|
||||
0, -457, -752, 1873, 13068, 0, 2389, 598, -213, -79,
|
||||
-61, -398, 0, 2686, 5997, 5997, 2686, 0, -398, -61,
|
||||
-79, -213, 598, 2389, 0, 13068, 1873, -752, -457, 0,
|
||||
-61, 0, 861, 1317, -3885, 19741, 0, -1361, -323, 92,
|
||||
-28, 156, 763, 0, -5289, 24506, -2427, -1522, 0, 180,
|
||||
};
|
||||
#endif /* resample_8k_12k8 */
|
||||
|
||||
#ifndef resample_16k_12k8
|
||||
static const int16_t h_16k_12k8_q15[4*20] = {
|
||||
-61, 214, -398, 417, 0, -1052, 2686, -4529, 5997, 26233,
|
||||
5997, -4529, 2686, -1052, 0, 417, -398, 214, -61, 0,
|
||||
|
||||
-79, 180, -213, 0, 598, -1522, 2389, -2427, 0, 24506,
|
||||
13068, -5289, 1873, 0, -752, 763, -457, 156, 0, -28,
|
||||
|
||||
-61, 92, 0, -323, 861, -1361, 1317, 0, -3885, 19741,
|
||||
19741, -3885, 0, 1317, -1361, 861, -323, 0, 92, -61,
|
||||
|
||||
-28, 0, 156, -457, 763, -752, 0, 1873, -5289, 13068,
|
||||
24506, 0, -2427, 2389, -1522, 598, 0, -213, 180, -79,
|
||||
};
|
||||
#endif /* resample_16k_12k8 */
|
||||
|
||||
#ifndef resample_32k_12k8
|
||||
static const int16_t h_32k_12k8_q15[2*40] = {
|
||||
-30, -31, 46, 107, 0, -199, -162, 209, 430, 0,
|
||||
-681, -526, 658, 1343, 0, -2264, -1943, 2999, 9871, 13116,
|
||||
9871, 2999, -1943, -2264, 0, 1343, 658, -526, -681, 0,
|
||||
430, 209, -162, -199, 0, 107, 46, -31, -30, 0,
|
||||
|
||||
-14, -39, 0, 90, 78, -106, -229, 0, 382, 299,
|
||||
-376, -761, 0, 1194, 937, -1214, -2644, 0, 6534, 12253,
|
||||
12253, 6534, 0, -2644, -1214, 937, 1194, 0, -761, -376,
|
||||
299, 382, 0, -229, -106, 78, 90, 0, -39, -14,
|
||||
};
|
||||
#endif /* resample_32k_12k8 */
|
||||
|
||||
#ifndef resample_24k_12k8
|
||||
static const int16_t h_24k_12k8_q15[8*30] = {
|
||||
-50, 19, 143, -93, -290, 278, 485, -658, -701, 1396,
|
||||
901, -3019, -1042, 10276, 17488, 10276, -1042, -3019, 901, 1396,
|
||||
-701, -658, 485, 278, -290, -93, 143, 19, -50, 0,
|
||||
|
||||
-46, 0, 141, -45, -305, 185, 543, -501, -854, 1153,
|
||||
1249, -2619, -1908, 8712, 17358, 11772, 0, -3319, 480, 1593,
|
||||
-504, -796, 399, 367, -261, -142, 138, 40, -52, -5,
|
||||
|
||||
-41, -17, 133, 0, -304, 91, 574, -334, -959, 878,
|
||||
1516, -2143, -2590, 7118, 16971, 13161, 1202, -3495, 0, 1731,
|
||||
-267, -908, 287, 445, -215, -188, 125, 62, -52, -12,
|
||||
|
||||
-34, -30, 120, 41, -291, 0, 577, -164, -1015, 585,
|
||||
1697, -1618, -3084, 5534, 16337, 14406, 2544, -3526, -523, 1800,
|
||||
0, -985, 152, 509, -156, -230, 104, 83, -48, -19,
|
||||
|
||||
-26, -41, 103, 76, -265, -83, 554, 0, -1023, 288,
|
||||
1791, -1070, -3393, 3998, 15474, 15474, 3998, -3393, -1070, 1791,
|
||||
288, -1023, 0, 554, -83, -265, 76, 103, -41, -26,
|
||||
|
||||
-19, -48, 83, 104, -230, -156, 509, 152, -985, 0,
|
||||
1800, -523, -3526, 2544, 14406, 16337, 5534, -3084, -1618, 1697,
|
||||
585, -1015, -164, 577, 0, -291, 41, 120, -30, -34,
|
||||
|
||||
-12, -52, 62, 125, -188, -215, 445, 287, -908, -267,
|
||||
1731, 0, -3495, 1202, 13161, 16971, 7118, -2590, -2143, 1516,
|
||||
878, -959, -334, 574, 91, -304, 0, 133, -17, -41,
|
||||
|
||||
-5, -52, 40, 138, -142, -261, 367, 399, -796, -504,
|
||||
1593, 480, -3319, 0, 11772, 17358, 8712, -1908, -2619, 1249,
|
||||
1153, -854, -501, 543, 185, -305, -45, 141, 0, -46,
|
||||
};
|
||||
#endif /* resample_24k_12k8 */
|
||||
|
||||
#ifndef resample_48k_12k8
|
||||
static const int16_t h_48k_12k8_q15[4*60] = {
|
||||
-13, -25, -20, 10, 51, 71, 38, -47, -133, -145,
|
||||
-42, 139, 277, 242, 0, -329, -511, -351, 144, 698,
|
||||
895, 450, -535, -1510, -1697, -521, 1999, 5138, 7737, 8744,
|
||||
7737, 5138, 1999, -521, -1697, -1510, -535, 450, 895, 698,
|
||||
144, -351, -511, -329, 0, 242, 277, 139, -42, -145,
|
||||
-133, -47, 38, 71, 51, 10, -20, -25, -13, 0,
|
||||
|
||||
-9, -23, -24, 0, 41, 71, 52, -23, -115, -152,
|
||||
-78, 92, 254, 272, 76, -251, -493, -427, 0, 576,
|
||||
900, 624, -262, -1309, -1763, -954, 1272, 4356, 7203, 8679,
|
||||
8169, 5886, 2767, 0, -1542, -1660, -809, 240, 848, 796,
|
||||
292, -252, -507, -398, -82, 199, 288, 183, 0, -130,
|
||||
-145, -71, 20, 69, 60, 20, -15, -26, -17, -3,
|
||||
|
||||
-6, -20, -26, -8, 31, 67, 62, 0, -94, -152,
|
||||
-108, 45, 223, 287, 143, -167, -454, -480, -134, 439,
|
||||
866, 758, 0, -1071, -1748, -1295, 601, 3559, 6580, 8485,
|
||||
8485, 6580, 3559, 601, -1295, -1748, -1071, 0, 758, 866,
|
||||
439, -134, -480, -454, -167, 143, 287, 223, 45, -108,
|
||||
-152, -94, 0, 62, 67, 31, -8, -26, -20, -6,
|
||||
|
||||
-3, -17, -26, -15, 20, 60, 69, 20, -71, -145,
|
||||
-130, 0, 183, 288, 199, -82, -398, -507, -252, 292,
|
||||
796, 848, 240, -809, -1660, -1542, 0, 2767, 5886, 8169,
|
||||
8679, 7203, 4356, 1272, -954, -1763, -1309, -262, 624, 900,
|
||||
576, 0, -427, -493, -251, 76, 272, 254, 92, -78,
|
||||
-152, -115, -23, 52, 71, 41, 0, -24, -23, -9,
|
||||
};
|
||||
#endif /* resample_48k_12k8 */
|
||||
|
||||
#ifndef resample_96k_12k8
|
||||
static const int16_t h_96k_12k8_q15[2*120] = {
|
||||
-3, -7, -10, -13, -13, -10, -4, 5, 15, 26,
|
||||
33, 36, 31, 19, 0, -23, -47, -66, -76, -73,
|
||||
-54, -21, 23, 70, 111, 139, 143, 121, 72, 0,
|
||||
-84, -165, -227, -256, -240, -175, -67, 72, 219, 349,
|
||||
433, 448, 379, 225, 0, -268, -536, -755, -874, -848,
|
||||
-648, -260, 301, 1000, 1780, 2569, 3290, 3869, 4243, 4372,
|
||||
4243, 3869, 3290, 2569, 1780, 1000, 301, -260, -648, -848,
|
||||
-874, -755, -536, -268, 0, 225, 379, 448, 433, 349,
|
||||
219, 72, -67, -175, -240, -256, -227, -165, -84, 0,
|
||||
72, 121, 143, 139, 111, 70, 23, -21, -54, -73,
|
||||
-76, -66, -47, -23, 0, 19, 31, 36, 33, 26,
|
||||
15, 5, -4, -10, -13, -13, -10, -7, -3, 0,
|
||||
|
||||
-1, -5, -8, -12, -13, -12, -8, 0, 10, 21,
|
||||
30, 35, 34, 26, 10, -11, -35, -58, -73, -76,
|
||||
-65, -39, 0, 46, 92, 127, 144, 136, 100, 38,
|
||||
-41, -125, -199, -246, -254, -214, -126, 0, 146, 288,
|
||||
398, 450, 424, 312, 120, -131, -405, -655, -830, -881,
|
||||
-771, -477, 0, 636, 1384, 2178, 2943, 3601, 4084, 4340,
|
||||
4340, 4084, 3601, 2943, 2178, 1384, 636, 0, -477, -771,
|
||||
-881, -830, -655, -405, -131, 120, 312, 424, 450, 398,
|
||||
288, 146, 0, -126, -214, -254, -246, -199, -125, -41,
|
||||
38, 100, 136, 144, 127, 92, 46, 0, -39, -65,
|
||||
-76, -73, -58, -35, -11, 10, 26, 34, 35, 30,
|
||||
21, 10, 0, -8, -12, -13, -12, -8, -5, -1,
|
||||
};
|
||||
#endif /* resample_96k_12k8 */
|
||||
|
||||
|
||||
/**
|
||||
* High-pass 50Hz filtering, at 12.8 KHz samplerate
|
||||
* hp50 Biquad filter state
|
||||
* xn Input sample, in fixed Q30
|
||||
* return Filtered sample, in fixed Q30
|
||||
*/
|
||||
LC3_HOT static inline int32_t filter_hp50(
|
||||
struct lc3_ltpf_hp50_state *hp50, int32_t xn)
|
||||
{
|
||||
int32_t yn;
|
||||
|
||||
const int32_t a1 = -2110217691, a2 = 1037111617;
|
||||
const int32_t b1 = -2110535566, b2 = 1055267782;
|
||||
|
||||
yn = (hp50->s1 + (int64_t)xn * b2) >> 30;
|
||||
hp50->s1 = (hp50->s2 + (int64_t)xn * b1 - (int64_t)yn * a1);
|
||||
hp50->s2 = ( (int64_t)xn * b2 - (int64_t)yn * a2);
|
||||
|
||||
return yn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resample from 8 / 16 / 32 KHz to 12.8 KHz Template
|
||||
* p Resampling factor with compared to 192 KHz (8, 4 or 2)
|
||||
* h Arrange by phase coefficients table
|
||||
* hp50 High-Pass biquad filter state
|
||||
* x [-d..-1] Previous, [0..ns-1] Current samples, Q15
|
||||
* y, n [0..n-1] Output `n` processed samples, Q14
|
||||
*
|
||||
* The `x` vector is aligned on 32 bits
|
||||
* The number of previous samples `d` accessed on `x` is :
|
||||
* d: { 10, 20, 40 } - 1 for resampling factors 8, 4 and 2.
|
||||
*/
|
||||
#if !defined(resample_8k_12k8) || !defined(resample_16k_12k8) \
|
||||
|| !defined(resample_32k_12k8)
|
||||
LC3_HOT static inline void resample_x64k_12k8(const int p, const int16_t *h,
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
|
||||
{
|
||||
const int w = 2*(40 / p);
|
||||
|
||||
x -= w - 1;
|
||||
|
||||
for (int i = 0; i < 5*n; i += 5) {
|
||||
const int16_t *hn = h + (i % p) * w;
|
||||
const int16_t *xn = x + (i / p);
|
||||
int32_t un = 0;
|
||||
|
||||
for (int k = 0; k < w; k += 10) {
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
}
|
||||
|
||||
int32_t yn = filter_hp50(hp50, un);
|
||||
*(y++) = (yn + (1 << 15)) >> 16;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Resample from 24 / 48 KHz to 12.8 KHz Template
|
||||
* p Resampling factor with compared to 192 KHz (8 or 4)
|
||||
* h Arrange by phase coefficients table
|
||||
* hp50 High-Pass biquad filter state
|
||||
* x [-d..-1] Previous, [0..ns-1] Current samples, Q15
|
||||
* y, n [0..n-1] Output `n` processed samples, Q14
|
||||
*
|
||||
* The `x` vector is aligned on 32 bits
|
||||
* The number of previous samples `d` accessed on `x` is :
|
||||
* d: { 30, 60 } - 1 for resampling factors 8 and 4.
|
||||
*/
|
||||
#if !defined(resample_24k_12k8) || !defined(resample_48k_12k8) \
|
||||
|| !defined(resample_96k_12k8)
|
||||
LC3_HOT static inline void resample_x192k_12k8(const int p, const int16_t *h,
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
|
||||
{
|
||||
const int w = 2*(120 / p);
|
||||
|
||||
x -= w - 1;
|
||||
|
||||
for (int i = 0; i < 15*n; i += 15) {
|
||||
const int16_t *hn = h + (i % p) * w;
|
||||
const int16_t *xn = x + (i / p);
|
||||
int32_t un = 0;
|
||||
|
||||
for (int k = 0; k < w; k += 15) {
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
un += *(xn++) * *(hn++);
|
||||
}
|
||||
|
||||
int32_t yn = filter_hp50(hp50, un);
|
||||
*(y++) = (yn + (1 << 15)) >> 16;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Resample from 8 Khz to 12.8 KHz
|
||||
* hp50 High-Pass biquad filter state
|
||||
* x [-10..-1] Previous, [0..ns-1] Current samples, Q15
|
||||
* y, n [0..n-1] Output `n` processed samples, Q14
|
||||
*
|
||||
* The `x` vector is aligned on 32 bits
|
||||
*/
|
||||
#ifndef resample_8k_12k8
|
||||
LC3_HOT static void resample_8k_12k8(
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
|
||||
{
|
||||
resample_x64k_12k8(8, h_8k_12k8_q15, hp50, x, y, n);
|
||||
}
|
||||
#endif /* resample_8k_12k8 */
|
||||
|
||||
/**
|
||||
* Resample from 16 Khz to 12.8 KHz
|
||||
* hp50 High-Pass biquad filter state
|
||||
* x [-20..-1] Previous, [0..ns-1] Current samples, in fixed Q15
|
||||
* y, n [0..n-1] Output `n` processed samples, in fixed Q14
|
||||
*
|
||||
* The `x` vector is aligned on 32 bits
|
||||
*/
|
||||
#ifndef resample_16k_12k8
|
||||
LC3_HOT static void resample_16k_12k8(
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
|
||||
{
|
||||
resample_x64k_12k8(4, h_16k_12k8_q15, hp50, x, y, n);
|
||||
}
|
||||
#endif /* resample_16k_12k8 */
|
||||
|
||||
/**
|
||||
* Resample from 32 Khz to 12.8 KHz
|
||||
* hp50 High-Pass biquad filter state
|
||||
* x [-30..-1] Previous, [0..ns-1] Current samples, in fixed Q15
|
||||
* y, n [0..n-1] Output `n` processed samples, in fixed Q14
|
||||
*
|
||||
* The `x` vector is aligned on 32 bits
|
||||
*/
|
||||
#ifndef resample_32k_12k8
|
||||
LC3_HOT static void resample_32k_12k8(
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
|
||||
{
|
||||
resample_x64k_12k8(2, h_32k_12k8_q15, hp50, x, y, n);
|
||||
}
|
||||
#endif /* resample_32k_12k8 */
|
||||
|
||||
/**
|
||||
* Resample from 24 Khz to 12.8 KHz
|
||||
* hp50 High-Pass biquad filter state
|
||||
* x [-30..-1] Previous, [0..ns-1] Current samples, in fixed Q15
|
||||
* y, n [0..n-1] Output `n` processed samples, in fixed Q14
|
||||
*
|
||||
* The `x` vector is aligned on 32 bits
|
||||
*/
|
||||
#ifndef resample_24k_12k8
|
||||
LC3_HOT static void resample_24k_12k8(
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
|
||||
{
|
||||
resample_x192k_12k8(8, h_24k_12k8_q15, hp50, x, y, n);
|
||||
}
|
||||
#endif /* resample_24k_12k8 */
|
||||
|
||||
/**
|
||||
* Resample from 48 Khz to 12.8 KHz
|
||||
* hp50 High-Pass biquad filter state
|
||||
* x [-60..-1] Previous, [0..ns-1] Current samples, in fixed Q15
|
||||
* y, n [0..n-1] Output `n` processed samples, in fixed Q14
|
||||
*
|
||||
* The `x` vector is aligned on 32 bits
|
||||
*/
|
||||
#ifndef resample_48k_12k8
|
||||
LC3_HOT static void resample_48k_12k8(
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
|
||||
{
|
||||
resample_x192k_12k8(4, h_48k_12k8_q15, hp50, x, y, n);
|
||||
}
|
||||
#endif /* resample_48k_12k8 */
|
||||
|
||||
/**
|
||||
* Resample from 96 Khz to 12.8 KHz
|
||||
* hp50 High-Pass biquad filter state
|
||||
* x [-120..-1] Previous, [0..ns-1] Current samples, in fixed Q15
|
||||
* y, n [0..n-1] Output `n` processed samples, in fixed Q14
|
||||
*
|
||||
* The `x` vector is aligned on 32 bits
|
||||
*/
|
||||
#ifndef resample_96k_12k8
|
||||
LC3_HOT static void resample_96k_12k8(
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
|
||||
{
|
||||
resample_x192k_12k8(2, h_96k_12k8_q15, hp50, x, y, n);
|
||||
}
|
||||
#endif /* resample_96k_12k8 */
|
||||
|
||||
/**
|
||||
* Resample to 6.4 KHz
|
||||
* x [-3..-1] Previous, [0..n-1] Current samples
|
||||
* y, n [0..n-1] Output `n` processed samples
|
||||
*
|
||||
* The `x` vector is aligned on 32 bits
|
||||
*/
|
||||
#ifndef resample_6k4
|
||||
LC3_HOT static void resample_6k4(const int16_t *x, int16_t *y, int n)
|
||||
{
|
||||
static const int16_t h[] = { 18477, 15424, 8105 };
|
||||
const int16_t *ye = y + n;
|
||||
|
||||
for (x--; y < ye; x += 2)
|
||||
*(y++) = (x[0] * h[0] + (x[-1] + x[1]) * h[1]
|
||||
+ (x[-2] + x[2]) * h[2]) >> 16;
|
||||
}
|
||||
#endif /* resample_6k4 */
|
||||
|
||||
/**
|
||||
* LTPF Resample to 12.8 KHz implementations for each samplerates
|
||||
*/
|
||||
|
||||
static void (* const resample_12k8[])
|
||||
(struct lc3_ltpf_hp50_state *, const int16_t *, int16_t *, int ) =
|
||||
{
|
||||
[LC3_SRATE_8K ] = resample_8k_12k8,
|
||||
[LC3_SRATE_16K ] = resample_16k_12k8,
|
||||
[LC3_SRATE_24K ] = resample_24k_12k8,
|
||||
[LC3_SRATE_32K ] = resample_32k_12k8,
|
||||
[LC3_SRATE_48K ] = resample_48k_12k8,
|
||||
[LC3_SRATE_48K_HR] = resample_48k_12k8,
|
||||
[LC3_SRATE_96K_HR] = resample_96k_12k8,
|
||||
};
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Analysis
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Return dot product of 2 vectors
|
||||
* a, b, n The 2 vectors of size `n` (> 0 and <= 128)
|
||||
* return sum( a[i] * b[i] ), i = [0..n-1]
|
||||
*
|
||||
* The size `n` of vectors must be multiple of 16, and less or equal to 128
|
||||
*/
|
||||
#ifndef dot
|
||||
LC3_HOT static inline float dot(const int16_t *a, const int16_t *b, int n)
|
||||
{
|
||||
int64_t v = 0;
|
||||
|
||||
for (int i = 0; i < (n >> 4); i++)
|
||||
for (int j = 0; j < 16; j++)
|
||||
v += *(a++) * *(b++);
|
||||
|
||||
int32_t v32 = (v + (1 << 5)) >> 6;
|
||||
return (float)v32;
|
||||
}
|
||||
#endif /* dot */
|
||||
|
||||
/**
|
||||
* Return vector of correlations
|
||||
* a, b, n The 2 vector of size `n` (> 0 and <= 128)
|
||||
* y, nc Output the correlation vector of size `nc`
|
||||
*
|
||||
* The first vector `a` is aligned of 32 bits
|
||||
* The size `n` of vectors is multiple of 16, and less or equal to 128
|
||||
*/
|
||||
#ifndef correlate
|
||||
LC3_HOT static void correlate(
|
||||
const int16_t *a, const int16_t *b, int n, float *y, int nc)
|
||||
{
|
||||
for (const float *ye = y + nc; y < ye; )
|
||||
*(y++) = dot(a, b--, n);
|
||||
}
|
||||
#endif /* correlate */
|
||||
|
||||
/**
|
||||
* Search the maximum value and returns its argument
|
||||
* x, n The input vector of size `n`
|
||||
* x_max Return the maximum value
|
||||
* return Return the argument of the maximum
|
||||
*/
|
||||
LC3_HOT static int argmax(const float *x, int n, float *x_max)
|
||||
{
|
||||
int arg = 0;
|
||||
|
||||
*x_max = x[arg = 0];
|
||||
for (int i = 1; i < n; i++)
|
||||
if (*x_max < x[i])
|
||||
*x_max = x[arg = i];
|
||||
|
||||
return arg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the maximum weithed value and returns its argument
|
||||
* x, n The input vector of size `n`
|
||||
* w_incr Increment of the weight
|
||||
* x_max, xw_max Return the maximum not weighted value
|
||||
* return Return the argument of the weigthed maximum
|
||||
*/
|
||||
LC3_HOT static int argmax_weighted(
|
||||
const float *x, int n, float w_incr, float *x_max)
|
||||
{
|
||||
int arg;
|
||||
|
||||
float xw_max = (*x_max = x[arg = 0]);
|
||||
float w = 1 + w_incr;
|
||||
|
||||
for (int i = 1; i < n; i++, w += w_incr)
|
||||
if (xw_max < x[i] * w)
|
||||
xw_max = (*x_max = x[arg = i]) * w;
|
||||
|
||||
return arg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interpolate from pitch detected value
|
||||
* x, n [-2..-1] Previous, [0..n] Current input
|
||||
* d The phase of interpolation (0 to 3)
|
||||
* return The interpolated vector
|
||||
*
|
||||
* The size `n` of vectors must be multiple of 4
|
||||
*/
|
||||
LC3_HOT static void interpolate(const int16_t *x, int n, int d, int16_t *y)
|
||||
{
|
||||
static const int16_t h4_q15[][4] = {
|
||||
{ 6877, 19121, 6877, 0 }, { 3506, 18025, 11000, 220 },
|
||||
{ 1300, 15048, 15048, 1300 }, { 220, 11000, 18025, 3506 } };
|
||||
|
||||
const int16_t *h = h4_q15[d];
|
||||
int16_t x3 = x[-2], x2 = x[-1], x1, x0;
|
||||
|
||||
x1 = (*x++);
|
||||
for (const int16_t *ye = y + n; y < ye; ) {
|
||||
int32_t yn;
|
||||
|
||||
yn = (x0 = *(x++)) * h[0] + x1 * h[1] + x2 * h[2] + x3 * h[3];
|
||||
*(y++) = yn >> 15;
|
||||
|
||||
yn = (x3 = *(x++)) * h[0] + x0 * h[1] + x1 * h[2] + x2 * h[3];
|
||||
*(y++) = yn >> 15;
|
||||
|
||||
yn = (x2 = *(x++)) * h[0] + x3 * h[1] + x0 * h[2] + x1 * h[3];
|
||||
*(y++) = yn >> 15;
|
||||
|
||||
yn = (x1 = *(x++)) * h[0] + x2 * h[1] + x3 * h[2] + x0 * h[3];
|
||||
*(y++) = yn >> 15;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Interpolate autocorrelation
|
||||
* x [-4..-1] Previous, [0..4] Current input
|
||||
* d The phase of interpolation (-3 to 3)
|
||||
* return The interpolated value
|
||||
*/
|
||||
LC3_HOT static float interpolate_corr(const float *x, int d)
|
||||
{
|
||||
static const float h4[][8] = {
|
||||
{ 1.53572770e-02, -4.72963246e-02, 8.35788573e-02, 8.98638285e-01,
|
||||
8.35788573e-02, -4.72963246e-02, 1.53572770e-02, },
|
||||
{ 2.74547165e-03, 4.59833449e-03, -7.54404636e-02, 8.17488686e-01,
|
||||
3.30182571e-01, -1.05835916e-01, 2.86823405e-02, -2.87456116e-03 },
|
||||
{ -3.00125103e-03, 2.95038503e-02, -1.30305021e-01, 6.03297008e-01,
|
||||
6.03297008e-01, -1.30305021e-01, 2.95038503e-02, -3.00125103e-03 },
|
||||
{ -2.87456116e-03, 2.86823405e-02, -1.05835916e-01, 3.30182571e-01,
|
||||
8.17488686e-01, -7.54404636e-02, 4.59833449e-03, 2.74547165e-03 },
|
||||
};
|
||||
|
||||
const float *h = h4[(4+d) % 4];
|
||||
|
||||
float y = d < 0 ? x[-4] * *(h++) :
|
||||
d > 0 ? x[ 4] * *(h+7) : 0;
|
||||
|
||||
y += x[-3] * h[0] + x[-2] * h[1] + x[-1] * h[2] + x[0] * h[3] +
|
||||
x[ 1] * h[4] + x[ 2] * h[5] + x[ 3] * h[6];
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pitch detection algorithm
|
||||
* ltpf Context of analysis
|
||||
* x, n [-114..-17] Previous, [0..n-1] Current 6.4KHz samples
|
||||
* tc Return the pitch-lag estimation
|
||||
* return True when pitch present
|
||||
*
|
||||
* The `x` vector is aligned on 32 bits
|
||||
*/
|
||||
static bool detect_pitch(struct lc3_ltpf_analysis *ltpf,
|
||||
const int16_t *x, int n, int *tc)
|
||||
{
|
||||
float rm1, rm2;
|
||||
float r[98];
|
||||
|
||||
const int r0 = 17, nr = 98;
|
||||
int k0 = LC3_MAX( 0, ltpf->tc-4);
|
||||
int nk = LC3_MIN(nr-1, ltpf->tc+4) - k0 + 1;
|
||||
|
||||
correlate(x, x - r0, n, r, nr);
|
||||
|
||||
int t1 = argmax_weighted(r, nr, -.5f/(nr-1), &rm1);
|
||||
int t2 = k0 + argmax(r + k0, nk, &rm2);
|
||||
|
||||
const int16_t *x1 = x - (r0 + t1);
|
||||
const int16_t *x2 = x - (r0 + t2);
|
||||
|
||||
float nc1 = rm1 <= 0 ? 0 :
|
||||
rm1 / sqrtf(dot(x, x, n) * dot(x1, x1, n));
|
||||
|
||||
float nc2 = rm2 <= 0 ? 0 :
|
||||
rm2 / sqrtf(dot(x, x, n) * dot(x2, x2, n));
|
||||
|
||||
int t1sel = nc2 <= 0.85f * nc1;
|
||||
ltpf->tc = (t1sel ? t1 : t2);
|
||||
|
||||
*tc = r0 + ltpf->tc;
|
||||
return (t1sel ? nc1 : nc2) > 0.6f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pitch-lag parameter
|
||||
* x, n [-232..-28] Previous, [0..n-1] Current 12.8KHz samples, Q14
|
||||
* tc Pitch-lag estimation
|
||||
* pitch The pitch value, in fixed .4
|
||||
* return The bitstream pitch index value
|
||||
*
|
||||
* The `x` vector is aligned on 32 bits
|
||||
*/
|
||||
static int refine_pitch(const int16_t *x, int n, int tc, int *pitch)
|
||||
{
|
||||
float r[17], rm;
|
||||
int e, f;
|
||||
|
||||
int r0 = LC3_MAX( 32, 2*tc - 4);
|
||||
int nr = LC3_MIN(228, 2*tc + 4) - r0 + 1;
|
||||
|
||||
correlate(x, x - (r0 - 4), n, r, nr + 8);
|
||||
|
||||
e = r0 + argmax(r + 4, nr, &rm);
|
||||
const float *re = r + (e - (r0 - 4));
|
||||
|
||||
float dm = interpolate_corr(re, f = 0);
|
||||
for (int i = 1; i <= 3; i++) {
|
||||
float d;
|
||||
|
||||
if (e >= 127 && ((i & 1) | (e >= 157)))
|
||||
continue;
|
||||
|
||||
if ((d = interpolate_corr(re, i)) > dm)
|
||||
dm = d, f = i;
|
||||
|
||||
if (e > 32 && (d = interpolate_corr(re, -i)) > dm)
|
||||
dm = d, f = -i;
|
||||
}
|
||||
|
||||
e -= (f < 0);
|
||||
f += 4*(f < 0);
|
||||
|
||||
*pitch = 4*e + f;
|
||||
return e < 127 ? 4*e + f - 128 :
|
||||
e < 157 ? 2*e + (f >> 1) + 126 : e + 283;
|
||||
}
|
||||
|
||||
/**
|
||||
* LTPF Analysis
|
||||
*/
|
||||
bool lc3_ltpf_analyse(
|
||||
enum lc3_dt dt, enum lc3_srate sr, struct lc3_ltpf_analysis *ltpf,
|
||||
const int16_t *x, struct lc3_ltpf_data *data)
|
||||
{
|
||||
/* --- Resampling to 12.8 KHz --- */
|
||||
|
||||
int z_12k8 = sizeof(ltpf->x_12k8) / sizeof(*ltpf->x_12k8);
|
||||
int n_12k8 = (1 + dt) * 32;
|
||||
|
||||
memmove(ltpf->x_12k8, ltpf->x_12k8 + n_12k8,
|
||||
(z_12k8 - n_12k8) * sizeof(*ltpf->x_12k8));
|
||||
|
||||
int16_t *x_12k8 = ltpf->x_12k8 + (z_12k8 - n_12k8);
|
||||
|
||||
resample_12k8[sr](<pf->hp50, x, x_12k8, n_12k8);
|
||||
|
||||
x_12k8 -= (dt == LC3_DT_7M5 ? 44 : 24);
|
||||
|
||||
/* --- Resampling to 6.4 KHz --- */
|
||||
|
||||
int z_6k4 = sizeof(ltpf->x_6k4) / sizeof(*ltpf->x_6k4);
|
||||
int n_6k4 = n_12k8 >> 1;
|
||||
|
||||
memmove(ltpf->x_6k4, ltpf->x_6k4 + n_6k4,
|
||||
(z_6k4 - n_6k4) * sizeof(*ltpf->x_6k4));
|
||||
|
||||
int16_t *x_6k4 = ltpf->x_6k4 + (z_6k4 - n_6k4);
|
||||
|
||||
resample_6k4(x_12k8, x_6k4, n_6k4);
|
||||
|
||||
/* --- Enlarge for small frame size --- */
|
||||
|
||||
if (dt == LC3_DT_2M5) {
|
||||
x_12k8 -= n_12k8, x_6k4 -= n_6k4;
|
||||
n_12k8 += n_12k8; n_6k4 += n_6k4;
|
||||
}
|
||||
|
||||
/* --- Pitch detection --- */
|
||||
|
||||
int tc, pitch = 0;
|
||||
float nc = 0;
|
||||
|
||||
bool pitch_present = detect_pitch(ltpf, x_6k4, n_6k4, &tc);
|
||||
|
||||
if (pitch_present) {
|
||||
int16_t u[128], v[128];
|
||||
|
||||
data->pitch_index = refine_pitch(x_12k8, n_12k8, tc, &pitch);
|
||||
|
||||
interpolate(x_12k8, n_12k8, 0, u);
|
||||
interpolate(x_12k8 - (pitch >> 2), n_12k8, pitch & 3, v);
|
||||
|
||||
nc = dot(u, v, n_12k8) / sqrtf(dot(u, u, n_12k8) * dot(v, v, n_12k8));
|
||||
}
|
||||
|
||||
/* --- Activation --- */
|
||||
|
||||
if (ltpf->active) {
|
||||
int pitch_diff =
|
||||
LC3_MAX(pitch, ltpf->pitch) - LC3_MIN(pitch, ltpf->pitch);
|
||||
float nc_diff = nc - ltpf->nc[0];
|
||||
|
||||
data->active = !lc3_hr(sr) && pitch_present &&
|
||||
((nc > 0.9f) || (nc > 0.84f && pitch_diff < 8 && nc_diff > -0.1f));
|
||||
|
||||
} else {
|
||||
data->active = !lc3_hr(sr) && pitch_present &&
|
||||
( (dt == LC3_DT_10M || ltpf->nc[1] > 0.94f) &&
|
||||
(ltpf->nc[0] > 0.94f && nc > 0.94f) );
|
||||
}
|
||||
|
||||
ltpf->active = data->active;
|
||||
ltpf->pitch = pitch;
|
||||
ltpf->nc[1] = ltpf->nc[0];
|
||||
ltpf->nc[0] = nc;
|
||||
|
||||
return pitch_present;
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Synthesis
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Width of synthesis filter
|
||||
*/
|
||||
|
||||
#define MAX_FILTER_WIDTH \
|
||||
(LC3_MAX_SRATE_HZ / 4000)
|
||||
|
||||
|
||||
/**
|
||||
* Synthesis filter template
|
||||
* xh, nh History ring buffer of filtered samples
|
||||
* lag Lag parameter in the ring buffer
|
||||
* x0 w-1 previous input samples
|
||||
* x, n Current samples as input, filtered as output
|
||||
* c, w Coefficients `den` then `num`, and width of filter
|
||||
* fade Fading mode of filter -1: Out 1: In 0: None
|
||||
*/
|
||||
LC3_HOT static inline void synthesize_template(
|
||||
const float *xh, int nh, int lag,
|
||||
const float *x0, float *x, int n,
|
||||
const float *c, const int w, int fade)
|
||||
{
|
||||
float g = (float)(fade <= 0);
|
||||
float g_incr = (float)((fade > 0) - (fade < 0)) / n;
|
||||
float u[MAX_FILTER_WIDTH];
|
||||
|
||||
/* --- Load previous samples --- */
|
||||
|
||||
lag += (w >> 1);
|
||||
|
||||
const float *y = x - xh < lag ? x + (nh - lag) : x - lag;
|
||||
const float *y_end = xh + nh - 1;
|
||||
|
||||
for (int j = 0; j < w-1; j++) {
|
||||
|
||||
u[j] = 0;
|
||||
|
||||
float yi = *y, xi = *(x0++);
|
||||
y = y < y_end ? y + 1 : xh;
|
||||
|
||||
for (int k = 0; k <= j; k++)
|
||||
u[j-k] -= yi * c[k];
|
||||
|
||||
for (int k = 0; k <= j; k++)
|
||||
u[j-k] += xi * c[w+k];
|
||||
}
|
||||
|
||||
u[w-1] = 0;
|
||||
|
||||
/* --- Process by filter length --- */
|
||||
|
||||
for (int i = 0; i < n; i += w)
|
||||
for (int j = 0; j < w; j++, g += g_incr) {
|
||||
|
||||
float yi = *y, xi = *x;
|
||||
y = y < y_end ? y + 1 : xh;
|
||||
|
||||
for (int k = 0; k < w; k++)
|
||||
u[(j+(w-1)-k)%w] -= yi * c[k];
|
||||
|
||||
for (int k = 0; k < w; k++)
|
||||
u[(j+(w-1)-k)%w] += xi * c[w+k];
|
||||
|
||||
*(x++) = xi - g * u[j];
|
||||
u[j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Synthesis filter for each samplerates (width of filter)
|
||||
*/
|
||||
|
||||
|
||||
LC3_HOT static void synthesize_4(const float *xh, int nh, int lag,
|
||||
const float *x0, float *x, int n, const float *c, int fade)
|
||||
{
|
||||
synthesize_template(xh, nh, lag, x0, x, n, c, 4, fade);
|
||||
}
|
||||
|
||||
LC3_HOT static void synthesize_6(const float *xh, int nh, int lag,
|
||||
const float *x0, float *x, int n, const float *c, int fade)
|
||||
{
|
||||
synthesize_template(xh, nh, lag, x0, x, n, c, 6, fade);
|
||||
}
|
||||
|
||||
LC3_HOT static void synthesize_8(const float *xh, int nh, int lag,
|
||||
const float *x0, float *x, int n, const float *c, int fade)
|
||||
{
|
||||
synthesize_template(xh, nh, lag, x0, x, n, c, 8, fade);
|
||||
}
|
||||
|
||||
LC3_HOT static void synthesize_12(const float *xh, int nh, int lag,
|
||||
const float *x0, float *x, int n, const float *c, int fade)
|
||||
{
|
||||
synthesize_template(xh, nh, lag, x0, x, n, c, 12, fade);
|
||||
}
|
||||
|
||||
static void (* const synthesize[])(const float *, int, int,
|
||||
const float *, float *, int, const float *, int) =
|
||||
{
|
||||
[LC3_SRATE_8K ] = synthesize_4,
|
||||
[LC3_SRATE_16K] = synthesize_4,
|
||||
[LC3_SRATE_24K] = synthesize_6,
|
||||
[LC3_SRATE_32K] = synthesize_8,
|
||||
[LC3_SRATE_48K] = synthesize_12,
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* LTPF Synthesis
|
||||
*/
|
||||
void lc3_ltpf_synthesize(enum lc3_dt dt, enum lc3_srate sr, int nbytes,
|
||||
lc3_ltpf_synthesis_t *ltpf, const lc3_ltpf_data_t *data,
|
||||
const float *xh, float *x)
|
||||
{
|
||||
int nh = lc3_ns(dt, sr) + lc3_nh(dt, sr);
|
||||
|
||||
/* --- Filter parameters --- */
|
||||
|
||||
int p_idx = data ? data->pitch_index : 0;
|
||||
int pitch =
|
||||
p_idx >= 440 ? (((p_idx ) - 283) << 2) :
|
||||
p_idx >= 380 ? (((p_idx >> 1) - 63) << 2) + (((p_idx & 1)) << 1) :
|
||||
(((p_idx >> 2) + 32) << 2) + (((p_idx & 3)) << 0) ;
|
||||
|
||||
pitch = (pitch * lc3_ns(LC3_DT_10M, sr) + 64) / 128;
|
||||
|
||||
int nbits = (nbytes*8 * (1 + LC3_DT_10M)) / (1 + dt);
|
||||
if (dt == LC3_DT_2M5)
|
||||
nbits = (6 * nbits + 5) / 10;
|
||||
if (dt == LC3_DT_5M)
|
||||
nbits -= 160;
|
||||
|
||||
int g_idx = LC3_MAX(nbits / 80, (int)(3 + sr)) - (3 + sr);
|
||||
bool active = data && data->active && g_idx < 4;
|
||||
|
||||
int w = LC3_MAX(4, lc3_ns_4m[sr] >> 4);
|
||||
float c[2 * MAX_FILTER_WIDTH];
|
||||
|
||||
for (int i = 0; i < w; i++) {
|
||||
float g = active ? 0.4f - 0.05f * g_idx : 0;
|
||||
c[ i] = g * lc3_ltpf_cden[sr][pitch & 3][(w-1)-i];
|
||||
c[w+i] = 0.85f * g * lc3_ltpf_cnum[sr][LC3_MIN(g_idx, 3)][(w-1)-i];
|
||||
}
|
||||
|
||||
/* --- Transition handling --- */
|
||||
|
||||
int ns = lc3_ns(dt, sr);
|
||||
int nt = ns / (1 + dt);
|
||||
float x0[2][MAX_FILTER_WIDTH];
|
||||
|
||||
memcpy(x0[0], ltpf->x, (w-1) * sizeof(float));
|
||||
memcpy(ltpf->x, x + ns - (w-1), (w-1) * sizeof(float));
|
||||
|
||||
if (active)
|
||||
memcpy(x0[1], x + nt-(w-1), (w-1) * sizeof(float));
|
||||
|
||||
if (!ltpf->active && active)
|
||||
synthesize[sr](xh, nh, pitch/4, x0[0], x, nt, c, 1);
|
||||
else if (ltpf->active && !active)
|
||||
synthesize[sr](xh, nh, ltpf->pitch/4, x0[0], x, nt, ltpf->c, -1);
|
||||
else if (ltpf->active && active && ltpf->pitch == pitch)
|
||||
synthesize[sr](xh, nh, pitch/4, x0[0], x, nt, c, 0);
|
||||
else if (ltpf->active && active) {
|
||||
synthesize[sr](xh, nh, ltpf->pitch/4, x0[0], x, nt, ltpf->c, -1);
|
||||
synthesize[sr](xh, nh, pitch/4,
|
||||
(x <= xh ? x + nh : x) - (w-1), x, nt, c, 1);
|
||||
}
|
||||
|
||||
/* --- Remainder --- */
|
||||
|
||||
if (active && ns > nt)
|
||||
synthesize[sr](xh, nh, pitch/4, x0[1], x + nt, ns-nt, c, 0);
|
||||
|
||||
/* --- Update state --- */
|
||||
|
||||
ltpf->active = active;
|
||||
ltpf->pitch = pitch;
|
||||
memcpy(ltpf->c, c, 2*w * sizeof(*ltpf->c));
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Bitstream data
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* LTPF disable
|
||||
*/
|
||||
void lc3_ltpf_disable(struct lc3_ltpf_data *data)
|
||||
{
|
||||
data->active = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return number of bits coding the bitstream data
|
||||
*/
|
||||
int lc3_ltpf_get_nbits(bool pitch)
|
||||
{
|
||||
return 1 + 10 * pitch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put bitstream data
|
||||
*/
|
||||
void lc3_ltpf_put_data(lc3_bits_t *bits,
|
||||
const struct lc3_ltpf_data *data)
|
||||
{
|
||||
lc3_put_bit(bits, data->active);
|
||||
lc3_put_bits(bits, data->pitch_index, 9);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get bitstream data
|
||||
*/
|
||||
void lc3_ltpf_get_data(lc3_bits_t *bits,
|
||||
struct lc3_ltpf_data *data)
|
||||
{
|
||||
data->active = lc3_get_bit(bits);
|
||||
data->pitch_index = lc3_get_bits(bits, 9);
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __LC3_LTPF_H
|
||||
#define __LC3_LTPF_H
|
||||
|
||||
#include "common.h"
|
||||
#include "bits.h"
|
||||
|
||||
|
||||
/**
|
||||
* LTPF data
|
||||
*/
|
||||
|
||||
typedef struct lc3_ltpf_data {
|
||||
bool active;
|
||||
int pitch_index;
|
||||
} lc3_ltpf_data_t;
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Encoding
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* LTPF analysis
|
||||
* dt, sr Duration and samplerate of the frame
|
||||
* ltpf Context of analysis
|
||||
* allowed True when activation of LTPF is allowed
|
||||
* x [-d..-1] Previous, [0..ns-1] Current samples
|
||||
* data Return bitstream data
|
||||
* return True when pitch present, False otherwise
|
||||
*
|
||||
* The `x` vector is aligned on 32 bits
|
||||
* The number of previous samples `d` accessed on `x` is :
|
||||
* d: { 10, 20, 30, 40, 60 } - 1 for samplerates from 8KHz to 48KHz
|
||||
*/
|
||||
bool lc3_ltpf_analyse(enum lc3_dt dt, enum lc3_srate sr,
|
||||
lc3_ltpf_analysis_t *ltpf, const int16_t *x, lc3_ltpf_data_t *data);
|
||||
|
||||
/**
|
||||
* LTPF disable
|
||||
* data LTPF data, disabled activation on return
|
||||
*/
|
||||
void lc3_ltpf_disable(lc3_ltpf_data_t *data);
|
||||
|
||||
/**
|
||||
* Return number of bits coding the bitstream data
|
||||
* pitch True when pitch present, False otherwise
|
||||
* return Bit consumption, including the pitch present flag
|
||||
*/
|
||||
int lc3_ltpf_get_nbits(bool pitch);
|
||||
|
||||
/**
|
||||
* Put bitstream data
|
||||
* bits Bitstream context
|
||||
* data LTPF data
|
||||
*/
|
||||
void lc3_ltpf_put_data(lc3_bits_t *bits, const lc3_ltpf_data_t *data);
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Decoding
|
||||
* -------------------------------------------------------------------------- */
|
||||
/**
|
||||
* Get bitstream data
|
||||
* bits Bitstream context
|
||||
* data Return bitstream data
|
||||
*/
|
||||
void lc3_ltpf_get_data(lc3_bits_t *bits, lc3_ltpf_data_t *data);
|
||||
|
||||
/**
|
||||
* LTPF synthesis
|
||||
* dt, sr Duration and samplerate of the frame
|
||||
* nbytes Size in bytes of the frame
|
||||
* ltpf Context of synthesis
|
||||
* data Bitstream data, NULL when pitch not present
|
||||
* xr Base address of ring buffer of decoded samples
|
||||
* x Samples to proceed in the ring buffer, filtered as output
|
||||
*
|
||||
* The size of the ring buffer is `nh + ns`.
|
||||
* The filtering needs an history of at least 18 ms.
|
||||
*/
|
||||
void lc3_ltpf_synthesize(enum lc3_dt dt, enum lc3_srate sr, int nbytes,
|
||||
lc3_ltpf_synthesis_t *ltpf, const lc3_ltpf_data_t *data,
|
||||
const float *xr, float *x);
|
||||
|
||||
|
||||
#endif /* __LC3_LTPF_H */
|
||||
@@ -0,0 +1,506 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#if (__ARM_FEATURE_SIMD32 && !(__GNUC__ < 10) || defined(TEST_ARM))
|
||||
|
||||
#ifndef TEST_ARM
|
||||
|
||||
#include <arm_acle.h>
|
||||
|
||||
static inline int16x2_t __pkhbt(int16x2_t a, int16x2_t b)
|
||||
{
|
||||
int16x2_t r;
|
||||
__asm("pkhbt %0, %1, %2" : "=r" (r) : "r" (a), "r" (b));
|
||||
return r;
|
||||
}
|
||||
|
||||
#endif /* TEST_ARM */
|
||||
|
||||
|
||||
/**
|
||||
* Import
|
||||
*/
|
||||
|
||||
static inline int32_t filter_hp50(struct lc3_ltpf_hp50_state *, int32_t);
|
||||
static inline float dot(const int16_t *, const int16_t *, int);
|
||||
|
||||
|
||||
/**
|
||||
* Resample from 8 / 16 / 32 KHz to 12.8 KHz Template
|
||||
*/
|
||||
#if !defined(resample_8k_12k8) || !defined(resample_16k_12k8) \
|
||||
|| !defined(resample_32k_12k8)
|
||||
static inline void arm_resample_x64k_12k8(const int p, const int16x2_t *h,
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16x2_t *x, int16_t *y, int n)
|
||||
{
|
||||
const int w = 40 / p;
|
||||
|
||||
x -= w;
|
||||
|
||||
for (int i = 0; i < 5*n; i += 5) {
|
||||
const int16x2_t *hn = h + (i % (2*p)) * (48 / p);
|
||||
const int16x2_t *xn = x + (i / (2*p));
|
||||
|
||||
int32_t un = __smlad(*(xn++), *(hn++), 0);
|
||||
|
||||
for (int k = 0; k < w; k += 5) {
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
}
|
||||
|
||||
int32_t yn = filter_hp50(hp50, un);
|
||||
*(y++) = (yn + (1 << 15)) >> 16;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Resample from 24 / 48 KHz to 12.8 KHz Template
|
||||
*/
|
||||
#if !defined(resample_24k_12k8) || !defined(resample_48k_12k8)
|
||||
static inline void arm_resample_x192k_12k8(const int p, const int16x2_t *h,
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16x2_t *x, int16_t *y, int n)
|
||||
{
|
||||
const int w = 120 / p;
|
||||
|
||||
x -= w;
|
||||
|
||||
for (int i = 0; i < 15*n; i += 15) {
|
||||
const int16x2_t *hn = h + (i % (2*p)) * (128 / p);
|
||||
const int16x2_t *xn = x + (i / (2*p));
|
||||
|
||||
int32_t un = __smlad(*(xn++), *(hn++), 0);
|
||||
|
||||
for (int k = 0; k < w; k += 15) {
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
un = __smlad(*(xn++), *(hn++), un);
|
||||
}
|
||||
|
||||
int32_t yn = filter_hp50(hp50, un);
|
||||
*(y++) = (yn + (1 << 15)) >> 16;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Resample from 8 Khz to 12.8 KHz
|
||||
*/
|
||||
#ifndef resample_8k_12k8
|
||||
|
||||
static void arm_resample_8k_12k8(
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
|
||||
{
|
||||
alignas(int32_t) static const int16_t h[2*8*12] = {
|
||||
0, 214, 417, -1052, -4529, 26233, -4529, -1052, 417, 214, 0, 0,
|
||||
0, 180, 0, -1522, -2427, 24506, -5289, 0, 763, 156, -28, 0,
|
||||
0, 92, -323, -1361, 0, 19741, -3885, 1317, 861, 0, -61, 0,
|
||||
0, 0, -457, -752, 1873, 13068, 0, 2389, 598, -213, -79, 0,
|
||||
0, -61, -398, 0, 2686, 5997, 5997, 2686, 0, -398, -61, 0,
|
||||
0, -79, -213, 598, 2389, 0, 13068, 1873, -752, -457, 0, 0,
|
||||
0, -61, 0, 861, 1317, -3885, 19741, 0, -1361, -323, 92, 0,
|
||||
0, -28, 156, 763, 0, -5289, 24506, -2427, -1522, 0, 180, 0,
|
||||
0, 0, 214, 417, -1052, -4529, 26233, -4529, -1052, 417, 214, 0,
|
||||
0, 0, 180, 0, -1522, -2427, 24506, -5289, 0, 763, 156, -28,
|
||||
0, 0, 92, -323, -1361, 0, 19741, -3885, 1317, 861, 0, -61,
|
||||
0, 0, 0, -457, -752, 1873, 13068, 0, 2389, 598, -213, -79,
|
||||
0, 0, -61, -398, 0, 2686, 5997, 5997, 2686, 0, -398, -61,
|
||||
0, 0, -79, -213, 598, 2389, 0, 13068, 1873, -752, -457, 0,
|
||||
0, 0, -61, 0, 861, 1317, -3885, 19741, 0, -1361, -323, 92,
|
||||
0, 0, -28, 156, 763, 0, -5289, 24506, -2427, -1522, 0, 180,
|
||||
};
|
||||
|
||||
arm_resample_x64k_12k8(
|
||||
8, (const int16x2_t *)h, hp50, (int16x2_t *)x, y, n);
|
||||
}
|
||||
|
||||
#ifndef TEST_ARM
|
||||
#define resample_8k_12k8 arm_resample_8k_12k8
|
||||
#endif
|
||||
|
||||
#endif /* resample_8k_12k8 */
|
||||
|
||||
/**
|
||||
* Resample from 16 Khz to 12.8 KHz
|
||||
*/
|
||||
#ifndef resample_16k_12k8
|
||||
|
||||
static void arm_resample_16k_12k8(
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
|
||||
{
|
||||
alignas(int32_t) static const int16_t h[2*4*24] = {
|
||||
|
||||
0, -61, 214, -398, 417, 0, -1052, 2686,
|
||||
-4529, 5997, 26233, 5997, -4529, 2686, -1052, 0,
|
||||
417, -398, 214, -61, 0, 0, 0, 0,
|
||||
|
||||
|
||||
0, -79, 180, -213, 0, 598, -1522, 2389,
|
||||
-2427, 0, 24506, 13068, -5289, 1873, 0, -752,
|
||||
763, -457, 156, 0, -28, 0, 0, 0,
|
||||
|
||||
|
||||
0, -61, 92, 0, -323, 861, -1361, 1317,
|
||||
0, -3885, 19741, 19741, -3885, 0, 1317, -1361,
|
||||
861, -323, 0, 92, -61, 0, 0, 0,
|
||||
|
||||
0, -28, 0, 156, -457, 763, -752, 0,
|
||||
1873, -5289, 13068, 24506, 0, -2427, 2389, -1522,
|
||||
598, 0, -213, 180, -79, 0, 0, 0,
|
||||
|
||||
|
||||
0, 0, -61, 214, -398, 417, 0, -1052,
|
||||
2686, -4529, 5997, 26233, 5997, -4529, 2686, -1052,
|
||||
0, 417, -398, 214, -61, 0, 0, 0,
|
||||
|
||||
|
||||
0, 0, -79, 180, -213, 0, 598, -1522,
|
||||
2389, -2427, 0, 24506, 13068, -5289, 1873, 0,
|
||||
-752, 763, -457, 156, 0, -28, 0, 0,
|
||||
|
||||
|
||||
0, 0, -61, 92, 0, -323, 861, -1361,
|
||||
1317, 0, -3885, 19741, 19741, -3885, 0, 1317,
|
||||
-1361, 861, -323, 0, 92, -61, 0, 0,
|
||||
|
||||
0, 0, -28, 0, 156, -457, 763, -752,
|
||||
0, 1873, -5289, 13068, 24506, 0, -2427, 2389,
|
||||
-1522, 598, 0, -213, 180, -79, 0, 0,
|
||||
};
|
||||
|
||||
arm_resample_x64k_12k8(
|
||||
4, (const int16x2_t *)h, hp50, (int16x2_t *)x, y, n);
|
||||
}
|
||||
|
||||
#ifndef TEST_ARM
|
||||
#define resample_16k_12k8 arm_resample_16k_12k8
|
||||
#endif
|
||||
|
||||
#endif /* resample_16k_12k8 */
|
||||
|
||||
/**
|
||||
* Resample from 32 Khz to 12.8 KHz
|
||||
*/
|
||||
#ifndef resample_32k_12k8
|
||||
|
||||
static void arm_resample_32k_12k8(
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
|
||||
{
|
||||
alignas(int32_t) static const int16_t h[2*2*48] = {
|
||||
|
||||
0, -30, -31, 46, 107, 0, -199, -162,
|
||||
209, 430, 0, -681, -526, 658, 1343, 0,
|
||||
-2264, -1943, 2999, 9871, 13116, 9871, 2999, -1943,
|
||||
-2264, 0, 1343, 658, -526, -681, 0, 430,
|
||||
209, -162, -199, 0, 107, 46, -31, -30,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
|
||||
0, -14, -39, 0, 90, 78, -106, -229,
|
||||
0, 382, 299, -376, -761, 0, 1194, 937,
|
||||
-1214, -2644, 0, 6534, 12253, 12253, 6534, 0,
|
||||
-2644, -1214, 937, 1194, 0, -761, -376, 299,
|
||||
382, 0, -229, -106, 78, 90, 0, -39,
|
||||
-14, 0, 0, 0, 0, 0, 0, 0,
|
||||
|
||||
0, 0, -30, -31, 46, 107, 0, -199,
|
||||
-162, 209, 430, 0, -681, -526, 658, 1343,
|
||||
0, -2264, -1943, 2999, 9871, 13116, 9871, 2999,
|
||||
-1943, -2264, 0, 1343, 658, -526, -681, 0,
|
||||
430, 209, -162, -199, 0, 107, 46, -31,
|
||||
-30, 0, 0, 0, 0, 0, 0, 0,
|
||||
|
||||
0, 0, -14, -39, 0, 90, 78, -106,
|
||||
-229, 0, 382, 299, -376, -761, 0, 1194,
|
||||
937, -1214, -2644, 0, 6534, 12253, 12253, 6534,
|
||||
0, -2644, -1214, 937, 1194, 0, -761, -376,
|
||||
299, 382, 0, -229, -106, 78, 90, 0,
|
||||
-39, -14, 0, 0, 0, 0, 0, 0,
|
||||
};
|
||||
|
||||
arm_resample_x64k_12k8(
|
||||
2, (const int16x2_t *)h, hp50, (int16x2_t *)x, y, n);
|
||||
}
|
||||
|
||||
#ifndef TEST_ARM
|
||||
#define resample_32k_12k8 arm_resample_32k_12k8
|
||||
#endif
|
||||
|
||||
#endif /* resample_32k_12k8 */
|
||||
|
||||
/**
|
||||
* Resample from 24 Khz to 12.8 KHz
|
||||
*/
|
||||
#ifndef resample_24k_12k8
|
||||
|
||||
static void arm_resample_24k_12k8(
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
|
||||
{
|
||||
alignas(int32_t) static const int16_t h[2*8*32] = {
|
||||
|
||||
0, -50, 19, 143, -93, -290, 278, 485,
|
||||
-658, -701, 1396, 901, -3019, -1042, 10276, 17488,
|
||||
10276, -1042, -3019, 901, 1396, -701, -658, 485,
|
||||
278, -290, -93, 143, 19, -50, 0, 0,
|
||||
|
||||
0, -46, 0, 141, -45, -305, 185, 543,
|
||||
-501, -854, 1153, 1249, -2619, -1908, 8712, 17358,
|
||||
11772, 0, -3319, 480, 1593, -504, -796, 399,
|
||||
367, -261, -142, 138, 40, -52, -5, 0,
|
||||
|
||||
0, -41, -17, 133, 0, -304, 91, 574,
|
||||
-334, -959, 878, 1516, -2143, -2590, 7118, 16971,
|
||||
13161, 1202, -3495, 0, 1731, -267, -908, 287,
|
||||
445, -215, -188, 125, 62, -52, -12, 0,
|
||||
|
||||
0, -34, -30, 120, 41, -291, 0, 577,
|
||||
-164, -1015, 585, 1697, -1618, -3084, 5534, 16337,
|
||||
14406, 2544, -3526, -523, 1800, 0, -985, 152,
|
||||
509, -156, -230, 104, 83, -48, -19, 0,
|
||||
|
||||
0, -26, -41, 103, 76, -265, -83, 554,
|
||||
0, -1023, 288, 1791, -1070, -3393, 3998, 15474,
|
||||
15474, 3998, -3393, -1070, 1791, 288, -1023, 0,
|
||||
554, -83, -265, 76, 103, -41, -26, 0,
|
||||
|
||||
0, -19, -48, 83, 104, -230, -156, 509,
|
||||
152, -985, 0, 1800, -523, -3526, 2544, 14406,
|
||||
16337, 5534, -3084, -1618, 1697, 585, -1015, -164,
|
||||
577, 0, -291, 41, 120, -30, -34, 0,
|
||||
|
||||
0, -12, -52, 62, 125, -188, -215, 445,
|
||||
287, -908, -267, 1731, 0, -3495, 1202, 13161,
|
||||
16971, 7118, -2590, -2143, 1516, 878, -959, -334,
|
||||
574, 91, -304, 0, 133, -17, -41, 0,
|
||||
|
||||
0, -5, -52, 40, 138, -142, -261, 367,
|
||||
399, -796, -504, 1593, 480, -3319, 0, 11772,
|
||||
17358, 8712, -1908, -2619, 1249, 1153, -854, -501,
|
||||
543, 185, -305, -45, 141, 0, -46, 0,
|
||||
|
||||
0, 0, -50, 19, 143, -93, -290, 278,
|
||||
485, -658, -701, 1396, 901, -3019, -1042, 10276,
|
||||
17488, 10276, -1042, -3019, 901, 1396, -701, -658,
|
||||
485, 278, -290, -93, 143, 19, -50, 0,
|
||||
|
||||
0, 0, -46, 0, 141, -45, -305, 185,
|
||||
543, -501, -854, 1153, 1249, -2619, -1908, 8712,
|
||||
17358, 11772, 0, -3319, 480, 1593, -504, -796,
|
||||
399, 367, -261, -142, 138, 40, -52, -5,
|
||||
|
||||
0, 0, -41, -17, 133, 0, -304, 91,
|
||||
574, -334, -959, 878, 1516, -2143, -2590, 7118,
|
||||
16971, 13161, 1202, -3495, 0, 1731, -267, -908,
|
||||
287, 445, -215, -188, 125, 62, -52, -12,
|
||||
|
||||
0, 0, -34, -30, 120, 41, -291, 0,
|
||||
577, -164, -1015, 585, 1697, -1618, -3084, 5534,
|
||||
16337, 14406, 2544, -3526, -523, 1800, 0, -985,
|
||||
152, 509, -156, -230, 104, 83, -48, -19,
|
||||
|
||||
0, 0, -26, -41, 103, 76, -265, -83,
|
||||
554, 0, -1023, 288, 1791, -1070, -3393, 3998,
|
||||
15474, 15474, 3998, -3393, -1070, 1791, 288, -1023,
|
||||
0, 554, -83, -265, 76, 103, -41, -26,
|
||||
|
||||
0, 0, -19, -48, 83, 104, -230, -156,
|
||||
509, 152, -985, 0, 1800, -523, -3526, 2544,
|
||||
14406, 16337, 5534, -3084, -1618, 1697, 585, -1015,
|
||||
-164, 577, 0, -291, 41, 120, -30, -34,
|
||||
|
||||
0, 0, -12, -52, 62, 125, -188, -215,
|
||||
445, 287, -908, -267, 1731, 0, -3495, 1202,
|
||||
13161, 16971, 7118, -2590, -2143, 1516, 878, -959,
|
||||
-334, 574, 91, -304, 0, 133, -17, -41,
|
||||
|
||||
0, 0, -5, -52, 40, 138, -142, -261,
|
||||
367, 399, -796, -504, 1593, 480, -3319, 0,
|
||||
11772, 17358, 8712, -1908, -2619, 1249, 1153, -854,
|
||||
-501, 543, 185, -305, -45, 141, 0, -46,
|
||||
};
|
||||
|
||||
arm_resample_x192k_12k8(
|
||||
8, (const int16x2_t *)h, hp50, (int16x2_t *)x, y, n);
|
||||
}
|
||||
|
||||
#ifndef TEST_ARM
|
||||
#define resample_24k_12k8 arm_resample_24k_12k8
|
||||
#endif
|
||||
|
||||
#endif /* resample_24k_12k8 */
|
||||
|
||||
/**
|
||||
* Resample from 48 Khz to 12.8 KHz
|
||||
*/
|
||||
#ifndef resample_48k_12k8
|
||||
|
||||
static void arm_resample_48k_12k8(
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
|
||||
{
|
||||
alignas(int32_t) static const int16_t h[2*4*64] = {
|
||||
|
||||
0, -13, -25, -20, 10, 51, 71, 38,
|
||||
-47, -133, -145, -42, 139, 277, 242, 0,
|
||||
-329, -511, -351, 144, 698, 895, 450, -535,
|
||||
-1510, -1697, -521, 1999, 5138, 7737, 8744, 7737,
|
||||
5138, 1999, -521, -1697, -1510, -535, 450, 895,
|
||||
698, 144, -351, -511, -329, 0, 242, 277,
|
||||
139, -42, -145, -133, -47, 38, 71, 51,
|
||||
10, -20, -25, -13, 0, 0, 0, 0,
|
||||
|
||||
0, -9, -23, -24, 0, 41, 71, 52,
|
||||
-23, -115, -152, -78, 92, 254, 272, 76,
|
||||
-251, -493, -427, 0, 576, 900, 624, -262,
|
||||
-1309, -1763, -954, 1272, 4356, 7203, 8679, 8169,
|
||||
5886, 2767, 0, -1542, -1660, -809, 240, 848,
|
||||
796, 292, -252, -507, -398, -82, 199, 288,
|
||||
183, 0, -130, -145, -71, 20, 69, 60,
|
||||
20, -15, -26, -17, -3, 0, 0, 0,
|
||||
|
||||
0, -6, -20, -26, -8, 31, 67, 62,
|
||||
0, -94, -152, -108, 45, 223, 287, 143,
|
||||
-167, -454, -480, -134, 439, 866, 758, 0,
|
||||
-1071, -1748, -1295, 601, 3559, 6580, 8485, 8485,
|
||||
6580, 3559, 601, -1295, -1748, -1071, 0, 758,
|
||||
866, 439, -134, -480, -454, -167, 143, 287,
|
||||
223, 45, -108, -152, -94, 0, 62, 67,
|
||||
31, -8, -26, -20, -6, 0, 0, 0,
|
||||
|
||||
0, -3, -17, -26, -15, 20, 60, 69,
|
||||
20, -71, -145, -130, 0, 183, 288, 199,
|
||||
-82, -398, -507, -252, 292, 796, 848, 240,
|
||||
-809, -1660, -1542, 0, 2767, 5886, 8169, 8679,
|
||||
7203, 4356, 1272, -954, -1763, -1309, -262, 624,
|
||||
900, 576, 0, -427, -493, -251, 76, 272,
|
||||
254, 92, -78, -152, -115, -23, 52, 71,
|
||||
41, 0, -24, -23, -9, 0, 0, 0,
|
||||
|
||||
0, 0, -13, -25, -20, 10, 51, 71,
|
||||
38, -47, -133, -145, -42, 139, 277, 242,
|
||||
0, -329, -511, -351, 144, 698, 895, 450,
|
||||
-535, -1510, -1697, -521, 1999, 5138, 7737, 8744,
|
||||
7737, 5138, 1999, -521, -1697, -1510, -535, 450,
|
||||
895, 698, 144, -351, -511, -329, 0, 242,
|
||||
277, 139, -42, -145, -133, -47, 38, 71,
|
||||
51, 10, -20, -25, -13, 0, 0, 0,
|
||||
|
||||
0, 0, -9, -23, -24, 0, 41, 71,
|
||||
52, -23, -115, -152, -78, 92, 254, 272,
|
||||
76, -251, -493, -427, 0, 576, 900, 624,
|
||||
-262, -1309, -1763, -954, 1272, 4356, 7203, 8679,
|
||||
8169, 5886, 2767, 0, -1542, -1660, -809, 240,
|
||||
848, 796, 292, -252, -507, -398, -82, 199,
|
||||
288, 183, 0, -130, -145, -71, 20, 69,
|
||||
60, 20, -15, -26, -17, -3, 0, 0,
|
||||
|
||||
0, 0, -6, -20, -26, -8, 31, 67,
|
||||
62, 0, -94, -152, -108, 45, 223, 287,
|
||||
143, -167, -454, -480, -134, 439, 866, 758,
|
||||
0, -1071, -1748, -1295, 601, 3559, 6580, 8485,
|
||||
8485, 6580, 3559, 601, -1295, -1748, -1071, 0,
|
||||
758, 866, 439, -134, -480, -454, -167, 143,
|
||||
287, 223, 45, -108, -152, -94, 0, 62,
|
||||
67, 31, -8, -26, -20, -6, 0, 0,
|
||||
|
||||
0, 0, -3, -17, -26, -15, 20, 60,
|
||||
69, 20, -71, -145, -130, 0, 183, 288,
|
||||
199, -82, -398, -507, -252, 292, 796, 848,
|
||||
240, -809, -1660, -1542, 0, 2767, 5886, 8169,
|
||||
8679, 7203, 4356, 1272, -954, -1763, -1309, -262,
|
||||
624, 900, 576, 0, -427, -493, -251, 76,
|
||||
272, 254, 92, -78, -152, -115, -23, 52,
|
||||
71, 41, 0, -24, -23, -9, 0, 0,
|
||||
};
|
||||
|
||||
arm_resample_x192k_12k8(
|
||||
4, (const int16x2_t *)h, hp50, (int16x2_t *)x, y, n);
|
||||
}
|
||||
|
||||
#ifndef TEST_ARM
|
||||
#define resample_48k_12k8 arm_resample_48k_12k8
|
||||
#endif
|
||||
|
||||
#endif /* resample_48k_12k8 */
|
||||
|
||||
/**
|
||||
* Return vector of correlations
|
||||
*/
|
||||
#ifndef correlate
|
||||
|
||||
static void arm_correlate(
|
||||
const int16_t *a, const int16_t *b, int n, float *y, int nc)
|
||||
{
|
||||
/* --- Check alignment of `b` --- */
|
||||
|
||||
if ((uintptr_t)b & 3)
|
||||
*(y++) = dot(a, b--, n), nc--;
|
||||
|
||||
/* --- Processing by pair --- */
|
||||
|
||||
for ( ; nc >= 2; nc -= 2) {
|
||||
const int16x2_t *an = (const int16x2_t *)(a );
|
||||
const int16x2_t *bn = (const int16x2_t *)(b--);
|
||||
|
||||
int16x2_t ax, b0, b1;
|
||||
int64_t v0 = 0, v1 = 0;
|
||||
|
||||
b1 = (int16x2_t)*(b--) << 16;
|
||||
|
||||
for (int i = 0; i < (n >> 4); i++ )
|
||||
for (int j = 0; j < 4; j++) {
|
||||
|
||||
ax = *(an++), b0 = *(bn++);
|
||||
v0 = __smlald (ax, b0, v0);
|
||||
v1 = __smlaldx(ax, __pkhbt(b0, b1), v1);
|
||||
|
||||
ax = *(an++), b1 = *(bn++);
|
||||
v0 = __smlald (ax, b1, v0);
|
||||
v1 = __smlaldx(ax, __pkhbt(b1, b0), v1);
|
||||
}
|
||||
|
||||
*(y++) = (float)((int32_t)((v0 + (1 << 5)) >> 6));
|
||||
*(y++) = (float)((int32_t)((v1 + (1 << 5)) >> 6));
|
||||
}
|
||||
|
||||
/* --- Odd element count --- */
|
||||
|
||||
if (nc > 0)
|
||||
*(y++) = dot(a, b, n);
|
||||
}
|
||||
|
||||
#ifndef TEST_ARM
|
||||
#define correlate arm_correlate
|
||||
#endif
|
||||
|
||||
#endif /* correlate */
|
||||
|
||||
#endif /* __ARM_FEATURE_SIMD32 */
|
||||
@@ -0,0 +1,281 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#if __ARM_NEON && __ARM_ARCH_ISA_A64 && \
|
||||
!defined(TEST_ARM) || defined(TEST_NEON)
|
||||
|
||||
#ifndef TEST_NEON
|
||||
#include <arm_neon.h>
|
||||
#endif /* TEST_NEON */
|
||||
|
||||
|
||||
/**
|
||||
* Import
|
||||
*/
|
||||
|
||||
static inline int32_t filter_hp50(struct lc3_ltpf_hp50_state *, int32_t);
|
||||
|
||||
|
||||
/**
|
||||
* Resample from 16 Khz to 12.8 KHz
|
||||
*/
|
||||
#ifndef resample_16k_12k8
|
||||
|
||||
LC3_HOT static void neon_resample_16k_12k8(
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
|
||||
{
|
||||
static const int16_t h[4][20] = {
|
||||
|
||||
{ -61, 214, -398, 417, 0, -1052, 2686, -4529, 5997, 26233,
|
||||
5997, -4529, 2686, -1052, 0, 417, -398, 214, -61, 0 },
|
||||
|
||||
{ -79, 180, -213, 0, 598, -1522, 2389, -2427, 0, 24506,
|
||||
13068, -5289, 1873, 0, -752, 763, -457, 156, 0, -28 },
|
||||
|
||||
{ -61, 92, 0, -323, 861, -1361, 1317, 0, -3885, 19741,
|
||||
19741, -3885, 0, 1317, -1361, 861, -323, 0, 92, -61 },
|
||||
|
||||
{ -28, 0, 156, -457, 763, -752, 0, 1873, -5289, 13068,
|
||||
24506, 0, -2427, 2389, -1522, 598, 0, -213, 180, -79 },
|
||||
|
||||
};
|
||||
|
||||
x -= 20 - 1;
|
||||
|
||||
for (int i = 0; i < 5*n; i += 5) {
|
||||
const int16_t *hn = h[i & 3];
|
||||
const int16_t *xn = x + (i >> 2);
|
||||
int32x4_t un;
|
||||
|
||||
un = vmull_s16( vld1_s16(xn), vld1_s16(hn)), xn += 4, hn += 4;
|
||||
un = vmlal_s16(un, vld1_s16(xn), vld1_s16(hn)), xn += 4, hn += 4;
|
||||
un = vmlal_s16(un, vld1_s16(xn), vld1_s16(hn)), xn += 4, hn += 4;
|
||||
un = vmlal_s16(un, vld1_s16(xn), vld1_s16(hn)), xn += 4, hn += 4;
|
||||
un = vmlal_s16(un, vld1_s16(xn), vld1_s16(hn)), xn += 4, hn += 4;
|
||||
|
||||
int32_t yn = filter_hp50(hp50, vaddvq_s32(un));
|
||||
*(y++) = (yn + (1 << 15)) >> 16;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef TEST_NEON
|
||||
#define resample_16k_12k8 neon_resample_16k_12k8
|
||||
#endif
|
||||
|
||||
#endif /* resample_16k_12k8 */
|
||||
|
||||
/**
|
||||
* Resample from 32 Khz to 12.8 KHz
|
||||
*/
|
||||
#ifndef resample_32k_12k8
|
||||
|
||||
LC3_HOT static void neon_resample_32k_12k8(
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
|
||||
{
|
||||
x -= 40 - 1;
|
||||
|
||||
static const int16_t h[2][40] = {
|
||||
|
||||
{ -30, -31, 46, 107, 0, -199, -162, 209, 430, 0,
|
||||
-681, -526, 658, 1343, 0, -2264, -1943, 2999, 9871, 13116,
|
||||
9871, 2999, -1943, -2264, 0, 1343, 658, -526, -681, 0,
|
||||
430, 209, -162, -199, 0, 107, 46, -31, -30, 0 },
|
||||
|
||||
{ -14, -39, 0, 90, 78, -106, -229, 0, 382, 299,
|
||||
-376, -761, 0, 1194, 937, -1214, -2644, 0, 6534, 12253,
|
||||
12253, 6534, 0, -2644, -1214, 937, 1194, 0, -761, -376,
|
||||
299, 382, 0, -229, -106, 78, 90, 0, -39, -14 },
|
||||
|
||||
};
|
||||
|
||||
for (int i = 0; i < 5*n; i += 5) {
|
||||
const int16_t *hn = h[i & 1];
|
||||
const int16_t *xn = x + (i >> 1);
|
||||
|
||||
int32x4_t un = vmull_s16(vld1_s16(xn), vld1_s16(hn));
|
||||
xn += 4, hn += 4;
|
||||
|
||||
for (int i = 1; i < 10; i++)
|
||||
un = vmlal_s16(un, vld1_s16(xn), vld1_s16(hn)), xn += 4, hn += 4;
|
||||
|
||||
int32_t yn = filter_hp50(hp50, vaddvq_s32(un));
|
||||
*(y++) = (yn + (1 << 15)) >> 16;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef TEST_NEON
|
||||
#define resample_32k_12k8 neon_resample_32k_12k8
|
||||
#endif
|
||||
|
||||
#endif /* resample_32k_12k8 */
|
||||
|
||||
/**
|
||||
* Resample from 48 Khz to 12.8 KHz
|
||||
*/
|
||||
#ifndef resample_48k_12k8
|
||||
|
||||
LC3_HOT static void neon_resample_48k_12k8(
|
||||
struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
|
||||
{
|
||||
alignas(16) static const int16_t h[4][64] = {
|
||||
|
||||
{ -13, -25, -20, 10, 51, 71, 38, -47, -133, -145,
|
||||
-42, 139, 277, 242, 0, -329, -511, -351, 144, 698,
|
||||
895, 450, -535, -1510, -1697, -521, 1999, 5138, 7737, 8744,
|
||||
7737, 5138, 1999, -521, -1697, -1510, -535, 450, 895, 698,
|
||||
144, -351, -511, -329, 0, 242, 277, 139, -42, -145,
|
||||
-133, -47, 38, 71, 51, 10, -20, -25, -13, 0 },
|
||||
|
||||
{ -9, -23, -24, 0, 41, 71, 52, -23, -115, -152,
|
||||
-78, 92, 254, 272, 76, -251, -493, -427, 0, 576,
|
||||
900, 624, -262, -1309, -1763, -954, 1272, 4356, 7203, 8679,
|
||||
8169, 5886, 2767, 0, -1542, -1660, -809, 240, 848, 796,
|
||||
292, -252, -507, -398, -82, 199, 288, 183, 0, -130,
|
||||
-145, -71, 20, 69, 60, 20, -15, -26, -17, -3 },
|
||||
|
||||
{ -6, -20, -26, -8, 31, 67, 62, 0, -94, -152,
|
||||
-108, 45, 223, 287, 143, -167, -454, -480, -134, 439,
|
||||
866, 758, 0, -1071, -1748, -1295, 601, 3559, 6580, 8485,
|
||||
8485, 6580, 3559, 601, -1295, -1748, -1071, 0, 758, 866,
|
||||
439, -134, -480, -454, -167, 143, 287, 223, 45, -108,
|
||||
-152, -94, 0, 62, 67, 31, -8, -26, -20, -6 },
|
||||
|
||||
{ -3, -17, -26, -15, 20, 60, 69, 20, -71, -145,
|
||||
-130, 0, 183, 288, 199, -82, -398, -507, -252, 292,
|
||||
796, 848, 240, -809, -1660, -1542, 0, 2767, 5886, 8169,
|
||||
8679, 7203, 4356, 1272, -954, -1763, -1309, -262, 624, 900,
|
||||
576, 0, -427, -493, -251, 76, 272, 254, 92, -78,
|
||||
-152, -115, -23, 52, 71, 41, 0, -24, -23, -9 },
|
||||
|
||||
};
|
||||
|
||||
x -= 60 - 1;
|
||||
|
||||
for (int i = 0; i < 15*n; i += 15) {
|
||||
const int16_t *hn = h[i & 3];
|
||||
const int16_t *xn = x + (i >> 2);
|
||||
|
||||
int32x4_t un = vmull_s16(vld1_s16(xn), vld1_s16(hn));
|
||||
xn += 4, hn += 4;
|
||||
|
||||
for (int i = 1; i < 15; i++)
|
||||
un = vmlal_s16(un, vld1_s16(xn), vld1_s16(hn)), xn += 4, hn += 4;
|
||||
|
||||
int32_t yn = filter_hp50(hp50, vaddvq_s32(un));
|
||||
*(y++) = (yn + (1 << 15)) >> 16;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef TEST_NEON
|
||||
#define resample_48k_12k8 neon_resample_48k_12k8
|
||||
#endif
|
||||
|
||||
#endif /* resample_48k_12k8 */
|
||||
|
||||
/**
|
||||
* Return dot product of 2 vectors
|
||||
*/
|
||||
#ifndef dot
|
||||
|
||||
LC3_HOT static inline float neon_dot(const int16_t *a, const int16_t *b, int n)
|
||||
{
|
||||
int64x2_t v = vmovq_n_s64(0);
|
||||
|
||||
for (int i = 0; i < (n >> 4); i++) {
|
||||
int32x4_t u;
|
||||
|
||||
u = vmull_s16( vld1_s16(a), vld1_s16(b)), a += 4, b += 4;
|
||||
u = vmlal_s16(u, vld1_s16(a), vld1_s16(b)), a += 4, b += 4;
|
||||
v = vpadalq_s32(v, u);
|
||||
|
||||
u = vmull_s16( vld1_s16(a), vld1_s16(b)), a += 4, b += 4;
|
||||
u = vmlal_s16(u, vld1_s16(a), vld1_s16(b)), a += 4, b += 4;
|
||||
v = vpadalq_s32(v, u);
|
||||
}
|
||||
|
||||
int32_t v32 = (vaddvq_s64(v) + (1 << 5)) >> 6;
|
||||
return (float)v32;
|
||||
}
|
||||
|
||||
#ifndef TEST_NEON
|
||||
#define dot neon_dot
|
||||
#endif
|
||||
|
||||
#endif /* dot */
|
||||
|
||||
/**
|
||||
* Return vector of correlations
|
||||
*/
|
||||
#ifndef correlate
|
||||
|
||||
LC3_HOT static void neon_correlate(
|
||||
const int16_t *a, const int16_t *b, int n, float *y, int nc)
|
||||
{
|
||||
for ( ; nc >= 4; nc -= 4, b -= 4) {
|
||||
const int16_t *an = (const int16_t *)a;
|
||||
const int16_t *bn = (const int16_t *)b;
|
||||
|
||||
int64x2_t v0 = vmovq_n_s64(0), v1 = v0, v2 = v0, v3 = v0;
|
||||
int16x4_t ax, b0, b1;
|
||||
|
||||
b0 = vld1_s16(bn-4);
|
||||
|
||||
for (int i=0; i < (n >> 4); i++ )
|
||||
for (int j = 0; j < 2; j++) {
|
||||
int32x4_t u0, u1, u2, u3;
|
||||
|
||||
b1 = b0;
|
||||
b0 = vld1_s16(bn), bn += 4;
|
||||
ax = vld1_s16(an), an += 4;
|
||||
|
||||
u0 = vmull_s16(ax, b0);
|
||||
u1 = vmull_s16(ax, vext_s16(b1, b0, 3));
|
||||
u2 = vmull_s16(ax, vext_s16(b1, b0, 2));
|
||||
u3 = vmull_s16(ax, vext_s16(b1, b0, 1));
|
||||
|
||||
b1 = b0;
|
||||
b0 = vld1_s16(bn), bn += 4;
|
||||
ax = vld1_s16(an), an += 4;
|
||||
|
||||
u0 = vmlal_s16(u0, ax, b0);
|
||||
u1 = vmlal_s16(u1, ax, vext_s16(b1, b0, 3));
|
||||
u2 = vmlal_s16(u2, ax, vext_s16(b1, b0, 2));
|
||||
u3 = vmlal_s16(u3, ax, vext_s16(b1, b0, 1));
|
||||
|
||||
v0 = vpadalq_s32(v0, u0);
|
||||
v1 = vpadalq_s32(v1, u1);
|
||||
v2 = vpadalq_s32(v2, u2);
|
||||
v3 = vpadalq_s32(v3, u3);
|
||||
}
|
||||
|
||||
*(y++) = (float)((int32_t)((vaddvq_s64(v0) + (1 << 5)) >> 6));
|
||||
*(y++) = (float)((int32_t)((vaddvq_s64(v1) + (1 << 5)) >> 6));
|
||||
*(y++) = (float)((int32_t)((vaddvq_s64(v2) + (1 << 5)) >> 6));
|
||||
*(y++) = (float)((int32_t)((vaddvq_s64(v3) + (1 << 5)) >> 6));
|
||||
}
|
||||
|
||||
for ( ; nc > 0; nc--)
|
||||
*(y++) = neon_dot(a, b--, n);
|
||||
}
|
||||
#endif /* correlate */
|
||||
|
||||
#ifndef TEST_NEON
|
||||
#define correlate neon_correlate
|
||||
#endif
|
||||
|
||||
#endif /* __ARM_NEON && __ARM_ARCH_ISA_A64 */
|
||||
@@ -0,0 +1,475 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#include "mdct.h"
|
||||
#include "tables.h"
|
||||
|
||||
#include "mdct_neon.h"
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* FFT processing
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* FFT 5 Points
|
||||
* x, y Input and output coefficients, of size 5xn
|
||||
* n Number of interleaved transform to perform (n % 2 = 0)
|
||||
*/
|
||||
#ifndef fft_5
|
||||
LC3_HOT static inline void fft_5(
|
||||
const struct lc3_complex *x, struct lc3_complex *y, int n)
|
||||
{
|
||||
static const float cos1 = 0.3090169944; /* cos(-2Pi 1/5) */
|
||||
static const float cos2 = -0.8090169944; /* cos(-2Pi 2/5) */
|
||||
|
||||
static const float sin1 = -0.9510565163; /* sin(-2Pi 1/5) */
|
||||
static const float sin2 = -0.5877852523; /* sin(-2Pi 2/5) */
|
||||
|
||||
for (int i = 0; i < n; i++, x++, y+= 5) {
|
||||
|
||||
struct lc3_complex s14 =
|
||||
{ x[1*n].re + x[4*n].re, x[1*n].im + x[4*n].im };
|
||||
struct lc3_complex d14 =
|
||||
{ x[1*n].re - x[4*n].re, x[1*n].im - x[4*n].im };
|
||||
|
||||
struct lc3_complex s23 =
|
||||
{ x[2*n].re + x[3*n].re, x[2*n].im + x[3*n].im };
|
||||
struct lc3_complex d23 =
|
||||
{ x[2*n].re - x[3*n].re, x[2*n].im - x[3*n].im };
|
||||
|
||||
y[0].re = x[0].re + s14.re + s23.re;
|
||||
|
||||
y[0].im = x[0].im + s14.im + s23.im;
|
||||
|
||||
y[1].re = x[0].re + s14.re * cos1 - d14.im * sin1
|
||||
+ s23.re * cos2 - d23.im * sin2;
|
||||
|
||||
y[1].im = x[0].im + s14.im * cos1 + d14.re * sin1
|
||||
+ s23.im * cos2 + d23.re * sin2;
|
||||
|
||||
y[2].re = x[0].re + s14.re * cos2 - d14.im * sin2
|
||||
+ s23.re * cos1 + d23.im * sin1;
|
||||
|
||||
y[2].im = x[0].im + s14.im * cos2 + d14.re * sin2
|
||||
+ s23.im * cos1 - d23.re * sin1;
|
||||
|
||||
y[3].re = x[0].re + s14.re * cos2 + d14.im * sin2
|
||||
+ s23.re * cos1 - d23.im * sin1;
|
||||
|
||||
y[3].im = x[0].im + s14.im * cos2 - d14.re * sin2
|
||||
+ s23.im * cos1 + d23.re * sin1;
|
||||
|
||||
y[4].re = x[0].re + s14.re * cos1 + d14.im * sin1
|
||||
+ s23.re * cos2 + d23.im * sin2;
|
||||
|
||||
y[4].im = x[0].im + s14.im * cos1 - d14.re * sin1
|
||||
+ s23.im * cos2 - d23.re * sin2;
|
||||
}
|
||||
}
|
||||
#endif /* fft_5 */
|
||||
|
||||
/**
|
||||
* FFT Butterfly 3 Points
|
||||
* x, y Input and output coefficients
|
||||
* twiddles Twiddles factors, determine size of transform
|
||||
* n Number of interleaved transforms
|
||||
*/
|
||||
#ifndef fft_bf3
|
||||
LC3_HOT static inline void fft_bf3(
|
||||
const struct lc3_fft_bf3_twiddles *twiddles,
|
||||
const struct lc3_complex *x, struct lc3_complex *y, int n)
|
||||
{
|
||||
int n3 = twiddles->n3;
|
||||
const struct lc3_complex (*w0)[2] = twiddles->t;
|
||||
const struct lc3_complex (*w1)[2] = w0 + n3, (*w2)[2] = w1 + n3;
|
||||
|
||||
const struct lc3_complex *x0 = x, *x1 = x0 + n*n3, *x2 = x1 + n*n3;
|
||||
struct lc3_complex *y0 = y, *y1 = y0 + n3, *y2 = y1 + n3;
|
||||
|
||||
for (int i = 0; i < n; i++, y0 += 3*n3, y1 += 3*n3, y2 += 3*n3)
|
||||
for (int j = 0; j < n3; j++, x0++, x1++, x2++) {
|
||||
|
||||
y0[j].re = x0->re + x1->re * w0[j][0].re - x1->im * w0[j][0].im
|
||||
+ x2->re * w0[j][1].re - x2->im * w0[j][1].im;
|
||||
|
||||
y0[j].im = x0->im + x1->im * w0[j][0].re + x1->re * w0[j][0].im
|
||||
+ x2->im * w0[j][1].re + x2->re * w0[j][1].im;
|
||||
|
||||
y1[j].re = x0->re + x1->re * w1[j][0].re - x1->im * w1[j][0].im
|
||||
+ x2->re * w1[j][1].re - x2->im * w1[j][1].im;
|
||||
|
||||
y1[j].im = x0->im + x1->im * w1[j][0].re + x1->re * w1[j][0].im
|
||||
+ x2->im * w1[j][1].re + x2->re * w1[j][1].im;
|
||||
|
||||
y2[j].re = x0->re + x1->re * w2[j][0].re - x1->im * w2[j][0].im
|
||||
+ x2->re * w2[j][1].re - x2->im * w2[j][1].im;
|
||||
|
||||
y2[j].im = x0->im + x1->im * w2[j][0].re + x1->re * w2[j][0].im
|
||||
+ x2->im * w2[j][1].re + x2->re * w2[j][1].im;
|
||||
}
|
||||
}
|
||||
#endif /* fft_bf3 */
|
||||
|
||||
/**
|
||||
* FFT Butterfly 2 Points
|
||||
* twiddles Twiddles factors, determine size of transform
|
||||
* x, y Input and output coefficients
|
||||
* n Number of interleaved transforms
|
||||
*/
|
||||
#ifndef fft_bf2
|
||||
LC3_HOT static inline void fft_bf2(
|
||||
const struct lc3_fft_bf2_twiddles *twiddles,
|
||||
const struct lc3_complex *x, struct lc3_complex *y, int n)
|
||||
{
|
||||
int n2 = twiddles->n2;
|
||||
const struct lc3_complex *w = twiddles->t;
|
||||
|
||||
const struct lc3_complex *x0 = x, *x1 = x0 + n*n2;
|
||||
struct lc3_complex *y0 = y, *y1 = y0 + n2;
|
||||
|
||||
for (int i = 0; i < n; i++, y0 += 2*n2, y1 += 2*n2) {
|
||||
|
||||
for (int j = 0; j < n2; j++, x0++, x1++) {
|
||||
|
||||
y0[j].re = x0->re + x1->re * w[j].re - x1->im * w[j].im;
|
||||
y0[j].im = x0->im + x1->im * w[j].re + x1->re * w[j].im;
|
||||
|
||||
y1[j].re = x0->re - x1->re * w[j].re + x1->im * w[j].im;
|
||||
y1[j].im = x0->im - x1->im * w[j].re - x1->re * w[j].im;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* fft_bf2 */
|
||||
|
||||
/**
|
||||
* Perform FFT
|
||||
* x, y0, y1 Input, and 2 scratch buffers of size `n`
|
||||
* n Number of points 30, 40, 60, 80, 90, 120, 160, 180, 240, 480
|
||||
* return The buffer `y0` or `y1` that hold the result
|
||||
*
|
||||
* Input `x` can be the same as the `y0` second scratch buffer
|
||||
*/
|
||||
static struct lc3_complex *fft(const struct lc3_complex *x, int n,
|
||||
struct lc3_complex *y0, struct lc3_complex *y1)
|
||||
{
|
||||
struct lc3_complex *y[2] = { y1, y0 };
|
||||
int i2, i3, is = 0;
|
||||
|
||||
/* The number of points `n` can be decomposed as :
|
||||
*
|
||||
* n = 5^1 * 3^n3 * 2^n2
|
||||
*
|
||||
* for n = 10, 20, 40, 80, 160 n3 = 0, n2 = [1..5]
|
||||
* n = 30, 60, 120, 240, 480 n3 = 1, n2 = [1..5]
|
||||
* n = 90, 180 n3 = 2, n2 = [1..2]
|
||||
*
|
||||
* Note that the expression `n & (n-1) == 0` is equivalent
|
||||
* to the check that `n` is a power of 2. */
|
||||
|
||||
fft_5(x, y[is], n /= 5);
|
||||
|
||||
for (i3 = 0; n & (n-1); i3++, is ^= 1)
|
||||
fft_bf3(lc3_fft_twiddles_bf3[i3], y[is], y[is ^ 1], n /= 3);
|
||||
|
||||
for (i2 = 0; n > 1; i2++, is ^= 1)
|
||||
fft_bf2(lc3_fft_twiddles_bf2[i2][i3], y[is], y[is ^ 1], n >>= 1);
|
||||
|
||||
return y[is];
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* MDCT processing
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Windowing of samples before MDCT
|
||||
* dt, sr Duration and samplerate
|
||||
* x, y Input current and delayed samples
|
||||
* y, d Output windowed samples, and delayed ones
|
||||
*/
|
||||
LC3_HOT static void mdct_window(
|
||||
enum lc3_dt dt, enum lc3_srate sr,
|
||||
const float *x, float *d, float *y)
|
||||
{
|
||||
const float *win = lc3_mdct_win[dt][sr];
|
||||
int ns = lc3_ns(dt, sr), nd = lc3_nd(dt, sr);
|
||||
|
||||
const float *w0 = win, *w1 = w0 + ns;
|
||||
const float *w2 = w1, *w3 = w2 + nd;
|
||||
|
||||
const float *x0 = x + ns-nd, *x1 = x0;
|
||||
float *y0 = y + ns/2, *y1 = y0;
|
||||
float *d0 = d, *d1 = d + nd;
|
||||
|
||||
while (x1 > x) {
|
||||
*(--y0) = *d0 * *(w0++) - *(--x1) * *(--w1);
|
||||
*(y1++) = (*(d0++) = *(x0++)) * *(w2++);
|
||||
|
||||
*(--y0) = *d0 * *(w0++) - *(--x1) * *(--w1);
|
||||
*(y1++) = (*(d0++) = *(x0++)) * *(w2++);
|
||||
}
|
||||
|
||||
for (x1 += ns; x0 < x1; ) {
|
||||
*(--y0) = *d0 * *(w0++) - *(--d1) * *(--w1);
|
||||
*(y1++) = (*(d0++) = *(x0++)) * *(w2++) + (*d1 = *(--x1)) * *(--w3);
|
||||
|
||||
*(--y0) = *d0 * *(w0++) - *(--d1) * *(--w1);
|
||||
*(y1++) = (*(d0++) = *(x0++)) * *(w2++) + (*d1 = *(--x1)) * *(--w3);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pre-rotate MDCT coefficients of N/2 points, before FFT N/4 points FFT
|
||||
* def Size and twiddles factors
|
||||
* x, y Input and output coefficients
|
||||
*
|
||||
* `x` and y` can be the same buffer
|
||||
*/
|
||||
LC3_HOT static void mdct_pre_fft(const struct lc3_mdct_rot_def *def,
|
||||
const float *x, struct lc3_complex *y)
|
||||
{
|
||||
int n4 = def->n4;
|
||||
|
||||
const float *x0 = x, *x1 = x0 + 2*n4;
|
||||
const struct lc3_complex *w0 = def->w, *w1 = w0 + n4;
|
||||
struct lc3_complex *y0 = y, *y1 = y0 + n4;
|
||||
|
||||
while (x0 < x1) {
|
||||
struct lc3_complex u, uw = *(w0++);
|
||||
u.re = - *(--x1) * uw.re + *x0 * uw.im;
|
||||
u.im = *(x0++) * uw.re + *x1 * uw.im;
|
||||
|
||||
struct lc3_complex v, vw = *(--w1);
|
||||
v.re = - *(--x1) * vw.im + *x0 * vw.re;
|
||||
v.im = - *(x0++) * vw.im - *x1 * vw.re;
|
||||
|
||||
*(y0++) = u;
|
||||
*(--y1) = v;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Post-rotate FFT N/4 points coefficients, resulting MDCT N points
|
||||
* def Size and twiddles factors
|
||||
* x, y Input and output coefficients
|
||||
*
|
||||
* `x` and y` can be the same buffer
|
||||
*/
|
||||
LC3_HOT static void mdct_post_fft(const struct lc3_mdct_rot_def *def,
|
||||
const struct lc3_complex *x, float *y)
|
||||
{
|
||||
int n4 = def->n4, n8 = n4 >> 1;
|
||||
|
||||
const struct lc3_complex *w0 = def->w + n8, *w1 = w0 - 1;
|
||||
const struct lc3_complex *x0 = x + n8, *x1 = x0 - 1;
|
||||
|
||||
float *y0 = y + n4, *y1 = y0;
|
||||
|
||||
for ( ; y1 > y; x0++, x1--, w0++, w1--) {
|
||||
|
||||
float u0 = x0->im * w0->im + x0->re * w0->re;
|
||||
float u1 = x1->re * w1->im - x1->im * w1->re;
|
||||
|
||||
float v0 = x0->re * w0->im - x0->im * w0->re;
|
||||
float v1 = x1->im * w1->im + x1->re * w1->re;
|
||||
|
||||
*(y0++) = u0; *(y0++) = u1;
|
||||
*(--y1) = v0; *(--y1) = v1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pre-rotate IMDCT coefficients of N points, before FFT N/4 points FFT
|
||||
* def Size and twiddles factors
|
||||
* x, y Input and output coefficients
|
||||
*
|
||||
* `x` and `y` can be the same buffer
|
||||
* The real and imaginary parts of `y` are swapped,
|
||||
* to operate on FFT instead of IFFT
|
||||
*/
|
||||
LC3_HOT static void imdct_pre_fft(const struct lc3_mdct_rot_def *def,
|
||||
const float *x, struct lc3_complex *y)
|
||||
{
|
||||
int n4 = def->n4;
|
||||
|
||||
const float *x0 = x, *x1 = x0 + 2*n4;
|
||||
|
||||
const struct lc3_complex *w0 = def->w, *w1 = w0 + n4;
|
||||
struct lc3_complex *y0 = y, *y1 = y0 + n4;
|
||||
|
||||
while (x0 < x1) {
|
||||
float u0 = *(x0++), u1 = *(--x1);
|
||||
float v0 = *(x0++), v1 = *(--x1);
|
||||
struct lc3_complex uw = *(w0++), vw = *(--w1);
|
||||
|
||||
(y0 )->re = - u0 * uw.re - u1 * uw.im;
|
||||
(y0++)->im = - u1 * uw.re + u0 * uw.im;
|
||||
|
||||
(--y1)->re = - v1 * vw.re - v0 * vw.im;
|
||||
( y1)->im = - v0 * vw.re + v1 * vw.im;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Post-rotate FFT N/4 points coefficients, resulting IMDCT N points
|
||||
* def Size and twiddles factors
|
||||
* x, y Input and output coefficients
|
||||
*
|
||||
* `x` and y` can be the same buffer
|
||||
* The real and imaginary parts of `x` are swapped,
|
||||
* to operate on FFT instead of IFFT
|
||||
*/
|
||||
LC3_HOT static void imdct_post_fft(const struct lc3_mdct_rot_def *def,
|
||||
const struct lc3_complex *x, float *y)
|
||||
{
|
||||
int n4 = def->n4;
|
||||
|
||||
const struct lc3_complex *w0 = def->w, *w1 = w0 + n4;
|
||||
const struct lc3_complex *x0 = x, *x1 = x0 + n4;
|
||||
|
||||
float *y0 = y, *y1 = y0 + 2*n4;
|
||||
|
||||
while (x0 < x1) {
|
||||
struct lc3_complex uz = *(x0++), vz = *(--x1);
|
||||
struct lc3_complex uw = *(w0++), vw = *(--w1);
|
||||
|
||||
*(y0++) = uz.re * uw.im - uz.im * uw.re;
|
||||
*(--y1) = uz.re * uw.re + uz.im * uw.im;
|
||||
|
||||
*(--y1) = vz.re * vw.im - vz.im * vw.re;
|
||||
*(y0++) = vz.re * vw.re + vz.im * vw.im;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply windowing of samples
|
||||
* dt, sr Duration and samplerate
|
||||
* x, d Middle half of IMDCT coefficients and delayed samples
|
||||
* y, d Output samples and delayed ones
|
||||
*/
|
||||
LC3_HOT static void imdct_window(
|
||||
enum lc3_dt dt, enum lc3_srate sr,
|
||||
const float *x, float *d, float *y)
|
||||
{
|
||||
/* The full MDCT coefficients is given by symmetry :
|
||||
* T[ 0 .. n/4-1] = -half[n/4-1 .. 0 ]
|
||||
* T[ n/4 .. n/2-1] = half[0 .. n/4-1]
|
||||
* T[ n/2 .. 3n/4-1] = half[n/4 .. n/2-1]
|
||||
* T[3n/4 .. n-1] = half[n/2-1 .. n/4 ] */
|
||||
|
||||
const float *win = lc3_mdct_win[dt][sr];
|
||||
int n4 = lc3_ns(dt, sr) >> 1, nd = lc3_nd(dt, sr);
|
||||
const float *w2 = win, *w0 = w2 + 3*n4, *w1 = w0;
|
||||
|
||||
const float *x0 = d + nd-n4, *x1 = x0;
|
||||
float *y0 = y + nd-n4, *y1 = y0, *y2 = d + nd, *y3 = d;
|
||||
|
||||
while (y0 > y) {
|
||||
*(--y0) = *(--x0) - *(x ) * *(w1++);
|
||||
*(y1++) = *(x1++) + *(x++) * *(--w0);
|
||||
|
||||
*(--y0) = *(--x0) - *(x ) * *(w1++);
|
||||
*(y1++) = *(x1++) + *(x++) * *(--w0);
|
||||
}
|
||||
|
||||
while (y1 < y + nd) {
|
||||
*(y1++) = *(x1++) + *(x++) * *(--w0);
|
||||
*(y1++) = *(x1++) + *(x++) * *(--w0);
|
||||
}
|
||||
|
||||
while (y1 < y + 2*n4) {
|
||||
*(y1++) = *(x ) * *(--w0);
|
||||
*(--y2) = *(x++) * *(w2++);
|
||||
|
||||
*(y1++) = *(x ) * *(--w0);
|
||||
*(--y2) = *(x++) * *(w2++);
|
||||
}
|
||||
|
||||
while (y2 > y3) {
|
||||
*(y3++) = *(x ) * *(--w0);
|
||||
*(--y2) = *(x++) * *(w2++);
|
||||
|
||||
*(y3++) = *(x ) * *(--w0);
|
||||
*(--y2) = *(x++) * *(w2++);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rescale samples
|
||||
* x, n Input and count of samples, scaled as output
|
||||
* scale Scale factor
|
||||
*/
|
||||
LC3_HOT static void rescale(float *x, int n, float f)
|
||||
{
|
||||
for (int i = 0; i < (n >> 2); i++) {
|
||||
*(x++) *= f; *(x++) *= f;
|
||||
*(x++) *= f; *(x++) *= f;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Forward MDCT transformation
|
||||
*/
|
||||
void lc3_mdct_forward(
|
||||
enum lc3_dt dt, enum lc3_srate sr, enum lc3_srate sr_dst,
|
||||
const float *x, float *d, float *y)
|
||||
{
|
||||
const struct lc3_mdct_rot_def *rot = lc3_mdct_rot[dt][sr];
|
||||
int ns_dst = lc3_ns(dt, sr_dst);
|
||||
int ns = lc3_ns(dt, sr);
|
||||
|
||||
struct lc3_complex buffer[LC3_MAX_NS / 2];
|
||||
struct lc3_complex *z = (struct lc3_complex *)y;
|
||||
union { float *f; struct lc3_complex *z; } u = { .z = buffer };
|
||||
|
||||
mdct_window(dt, sr, x, d, u.f);
|
||||
|
||||
mdct_pre_fft(rot, u.f, u.z);
|
||||
u.z = fft(u.z, ns/2, u.z, z);
|
||||
mdct_post_fft(rot, u.z, y);
|
||||
|
||||
if (ns != ns_dst)
|
||||
rescale(y, ns_dst, sqrtf((float)ns_dst / ns));
|
||||
}
|
||||
|
||||
/**
|
||||
* Inverse MDCT transformation
|
||||
*/
|
||||
void lc3_mdct_inverse(
|
||||
enum lc3_dt dt, enum lc3_srate sr, enum lc3_srate sr_src,
|
||||
const float *x, float *d, float *y)
|
||||
{
|
||||
const struct lc3_mdct_rot_def *rot = lc3_mdct_rot[dt][sr];
|
||||
int ns_src = lc3_ns(dt, sr_src);
|
||||
int ns = lc3_ns(dt, sr);
|
||||
|
||||
struct lc3_complex buffer[LC3_MAX_NS / 2];
|
||||
struct lc3_complex *z = (struct lc3_complex *)y;
|
||||
union { float *f; struct lc3_complex *z; } u = { .z = buffer };
|
||||
|
||||
imdct_pre_fft(rot, x, z);
|
||||
z = fft(z, ns/2, z, u.z);
|
||||
imdct_post_fft(rot, z, u.f);
|
||||
|
||||
if (ns != ns_src)
|
||||
rescale(u.f, ns, sqrtf((float)ns / ns_src));
|
||||
|
||||
imdct_window(dt, sr, u.f, d, y);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __LC3_MDCT_H
|
||||
#define __LC3_MDCT_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
||||
/**
|
||||
* Forward MDCT transformation
|
||||
* dt, sr Duration and samplerate (size of the transform)
|
||||
* sr_dst Samplerate destination, scale transforam accordingly
|
||||
* x, d Temporal samples and delayed buffer
|
||||
* y, d Output `ns` coefficients and `nd` delayed samples
|
||||
*
|
||||
* `x` and `y` can be the same buffer
|
||||
*/
|
||||
void lc3_mdct_forward(
|
||||
enum lc3_dt dt, enum lc3_srate sr, enum lc3_srate sr_dst,
|
||||
const float *x, float *d, float *y);
|
||||
|
||||
/**
|
||||
* Inverse MDCT transformation
|
||||
* dt, sr Duration and samplerate (size of the transform)
|
||||
* sr_src Samplerate source, scale transforam accordingly
|
||||
* x, d Frequency coefficients and delayed buffer
|
||||
* y, d Output `ns` samples and `nd` delayed ones
|
||||
*
|
||||
* `x` and `y` can be the same buffer
|
||||
*/
|
||||
void lc3_mdct_inverse(
|
||||
enum lc3_dt dt, enum lc3_srate sr, enum lc3_srate sr_src,
|
||||
const float *x, float *d, float *y);
|
||||
|
||||
|
||||
#endif /* __LC3_MDCT_H */
|
||||
@@ -0,0 +1,296 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#if __ARM_NEON && __ARM_ARCH_ISA_A64 && \
|
||||
!defined(TEST_ARM) || defined(TEST_NEON)
|
||||
|
||||
#ifndef TEST_NEON
|
||||
#include <arm_neon.h>
|
||||
#endif /* TEST_NEON */
|
||||
|
||||
|
||||
/**
|
||||
* FFT 5 Points
|
||||
* The number of interleaved transform `n` assumed to be even
|
||||
*/
|
||||
#ifndef fft_5
|
||||
|
||||
LC3_HOT static inline void neon_fft_5(
|
||||
const struct lc3_complex *x, struct lc3_complex *y, int n)
|
||||
{
|
||||
static const union { float f[2]; uint64_t u64; }
|
||||
__cos1 = { { 0.3090169944, 0.3090169944 } },
|
||||
__cos2 = { { -0.8090169944, -0.8090169944 } },
|
||||
__sin1 = { { 0.9510565163, -0.9510565163 } },
|
||||
__sin2 = { { 0.5877852523, -0.5877852523 } };
|
||||
|
||||
float32x2_t sin1 = vcreate_f32(__sin1.u64);
|
||||
float32x2_t sin2 = vcreate_f32(__sin2.u64);
|
||||
float32x2_t cos1 = vcreate_f32(__cos1.u64);
|
||||
float32x2_t cos2 = vcreate_f32(__cos2.u64);
|
||||
|
||||
float32x4_t sin1q = vcombine_f32(sin1, sin1);
|
||||
float32x4_t sin2q = vcombine_f32(sin2, sin2);
|
||||
float32x4_t cos1q = vcombine_f32(cos1, cos1);
|
||||
float32x4_t cos2q = vcombine_f32(cos2, cos2);
|
||||
|
||||
for (int i = 0; i < n; i += 2, x += 2, y += 10) {
|
||||
|
||||
float32x4_t y0, y1, y2, y3, y4;
|
||||
|
||||
float32x4_t x0 = vld1q_f32( (float *)(x + 0*n) );
|
||||
float32x4_t x1 = vld1q_f32( (float *)(x + 1*n) );
|
||||
float32x4_t x2 = vld1q_f32( (float *)(x + 2*n) );
|
||||
float32x4_t x3 = vld1q_f32( (float *)(x + 3*n) );
|
||||
float32x4_t x4 = vld1q_f32( (float *)(x + 4*n) );
|
||||
|
||||
float32x4_t s14 = vaddq_f32(x1, x4);
|
||||
float32x4_t s23 = vaddq_f32(x2, x3);
|
||||
|
||||
float32x4_t d14 = vrev64q_f32( vsubq_f32(x1, x4) );
|
||||
float32x4_t d23 = vrev64q_f32( vsubq_f32(x2, x3) );
|
||||
|
||||
y0 = vaddq_f32( x0, vaddq_f32(s14, s23) );
|
||||
|
||||
y4 = vfmaq_f32( x0, s14, cos1q );
|
||||
y4 = vfmaq_f32( y4, s23, cos2q );
|
||||
|
||||
y1 = vfmaq_f32( y4, d14, sin1q );
|
||||
y1 = vfmaq_f32( y1, d23, sin2q );
|
||||
|
||||
y4 = vfmsq_f32( y4, d14, sin1q );
|
||||
y4 = vfmsq_f32( y4, d23, sin2q );
|
||||
|
||||
y3 = vfmaq_f32( x0, s14, cos2q );
|
||||
y3 = vfmaq_f32( y3, s23, cos1q );
|
||||
|
||||
y2 = vfmaq_f32( y3, d14, sin2q );
|
||||
y2 = vfmsq_f32( y2, d23, sin1q );
|
||||
|
||||
y3 = vfmsq_f32( y3, d14, sin2q );
|
||||
y3 = vfmaq_f32( y3, d23, sin1q );
|
||||
|
||||
vst1_f32( (float *)(y + 0), vget_low_f32(y0) );
|
||||
vst1_f32( (float *)(y + 1), vget_low_f32(y1) );
|
||||
vst1_f32( (float *)(y + 2), vget_low_f32(y2) );
|
||||
vst1_f32( (float *)(y + 3), vget_low_f32(y3) );
|
||||
vst1_f32( (float *)(y + 4), vget_low_f32(y4) );
|
||||
|
||||
vst1_f32( (float *)(y + 5), vget_high_f32(y0) );
|
||||
vst1_f32( (float *)(y + 6), vget_high_f32(y1) );
|
||||
vst1_f32( (float *)(y + 7), vget_high_f32(y2) );
|
||||
vst1_f32( (float *)(y + 8), vget_high_f32(y3) );
|
||||
vst1_f32( (float *)(y + 9), vget_high_f32(y4) );
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef TEST_NEON
|
||||
#define fft_5 neon_fft_5
|
||||
#endif
|
||||
|
||||
#endif /* fft_5 */
|
||||
|
||||
/**
|
||||
* FFT Butterfly 3 Points
|
||||
*/
|
||||
#ifndef fft_bf3
|
||||
|
||||
LC3_HOT static inline void neon_fft_bf3(
|
||||
const struct lc3_fft_bf3_twiddles *twiddles,
|
||||
const struct lc3_complex *x, struct lc3_complex *y, int n)
|
||||
{
|
||||
int n3 = twiddles->n3;
|
||||
const struct lc3_complex (*w0_ptr)[2] = twiddles->t;
|
||||
const struct lc3_complex (*w1_ptr)[2] = w0_ptr + n3;
|
||||
const struct lc3_complex (*w2_ptr)[2] = w1_ptr + n3;
|
||||
|
||||
const struct lc3_complex *x0_ptr = x;
|
||||
const struct lc3_complex *x1_ptr = x0_ptr + n*n3;
|
||||
const struct lc3_complex *x2_ptr = x1_ptr + n*n3;
|
||||
|
||||
struct lc3_complex *y0_ptr = y;
|
||||
struct lc3_complex *y1_ptr = y0_ptr + n3;
|
||||
struct lc3_complex *y2_ptr = y1_ptr + n3;
|
||||
|
||||
for (int j, i = 0; i < n; i++,
|
||||
y0_ptr += 3*n3, y1_ptr += 3*n3, y2_ptr += 3*n3) {
|
||||
|
||||
/* --- Process by pair --- */
|
||||
|
||||
for (j = 0; j < (n3 >> 1); j++,
|
||||
x0_ptr += 2, x1_ptr += 2, x2_ptr += 2) {
|
||||
|
||||
float32x4_t x0 = vld1q_f32( (float *)x0_ptr );
|
||||
float32x4_t x1 = vld1q_f32( (float *)x1_ptr );
|
||||
float32x4_t x2 = vld1q_f32( (float *)x2_ptr );
|
||||
|
||||
float32x4_t x1r = vtrn1q_f32( vrev64q_f32(vnegq_f32(x1)), x1 );
|
||||
float32x4_t x2r = vtrn1q_f32( vrev64q_f32(vnegq_f32(x2)), x2 );
|
||||
|
||||
float32x4x2_t wn;
|
||||
float32x4_t yn;
|
||||
|
||||
wn = vld2q_f32( (float *)(w0_ptr + 2*j) );
|
||||
|
||||
yn = vfmaq_f32( x0, x1 , vtrn1q_f32(wn.val[0], wn.val[0]) );
|
||||
yn = vfmaq_f32( yn, x1r, vtrn1q_f32(wn.val[1], wn.val[1]) );
|
||||
yn = vfmaq_f32( yn, x2 , vtrn2q_f32(wn.val[0], wn.val[0]) );
|
||||
yn = vfmaq_f32( yn, x2r, vtrn2q_f32(wn.val[1], wn.val[1]) );
|
||||
vst1q_f32( (float *)(y0_ptr + 2*j), yn );
|
||||
|
||||
wn = vld2q_f32( (float *)(w1_ptr + 2*j) );
|
||||
|
||||
yn = vfmaq_f32( x0, x1 , vtrn1q_f32(wn.val[0], wn.val[0]) );
|
||||
yn = vfmaq_f32( yn, x1r, vtrn1q_f32(wn.val[1], wn.val[1]) );
|
||||
yn = vfmaq_f32( yn, x2 , vtrn2q_f32(wn.val[0], wn.val[0]) );
|
||||
yn = vfmaq_f32( yn, x2r, vtrn2q_f32(wn.val[1], wn.val[1]) );
|
||||
vst1q_f32( (float *)(y1_ptr + 2*j), yn );
|
||||
|
||||
wn = vld2q_f32( (float *)(w2_ptr + 2*j) );
|
||||
|
||||
yn = vfmaq_f32( x0, x1 , vtrn1q_f32(wn.val[0], wn.val[0]) );
|
||||
yn = vfmaq_f32( yn, x1r, vtrn1q_f32(wn.val[1], wn.val[1]) );
|
||||
yn = vfmaq_f32( yn, x2 , vtrn2q_f32(wn.val[0], wn.val[0]) );
|
||||
yn = vfmaq_f32( yn, x2r, vtrn2q_f32(wn.val[1], wn.val[1]) );
|
||||
vst1q_f32( (float *)(y2_ptr + 2*j), yn );
|
||||
|
||||
}
|
||||
|
||||
/* --- Last iteration --- */
|
||||
|
||||
if (n3 & 1) {
|
||||
|
||||
float32x2x2_t wn;
|
||||
float32x2_t yn;
|
||||
|
||||
float32x2_t x0 = vld1_f32( (float *)(x0_ptr++) );
|
||||
float32x2_t x1 = vld1_f32( (float *)(x1_ptr++) );
|
||||
float32x2_t x2 = vld1_f32( (float *)(x2_ptr++) );
|
||||
|
||||
float32x2_t x1r = vtrn1_f32( vrev64_f32(vneg_f32(x1)), x1 );
|
||||
float32x2_t x2r = vtrn1_f32( vrev64_f32(vneg_f32(x2)), x2 );
|
||||
|
||||
wn = vld2_f32( (float *)(w0_ptr + 2*j) );
|
||||
|
||||
yn = vfma_f32( x0, x1 , vtrn1_f32(wn.val[0], wn.val[0]) );
|
||||
yn = vfma_f32( yn, x1r, vtrn1_f32(wn.val[1], wn.val[1]) );
|
||||
yn = vfma_f32( yn, x2 , vtrn2_f32(wn.val[0], wn.val[0]) );
|
||||
yn = vfma_f32( yn, x2r, vtrn2_f32(wn.val[1], wn.val[1]) );
|
||||
vst1_f32( (float *)(y0_ptr + 2*j), yn );
|
||||
|
||||
wn = vld2_f32( (float *)(w1_ptr + 2*j) );
|
||||
|
||||
yn = vfma_f32( x0, x1 , vtrn1_f32(wn.val[0], wn.val[0]) );
|
||||
yn = vfma_f32( yn, x1r, vtrn1_f32(wn.val[1], wn.val[1]) );
|
||||
yn = vfma_f32( yn, x2 , vtrn2_f32(wn.val[0], wn.val[0]) );
|
||||
yn = vfma_f32( yn, x2r, vtrn2_f32(wn.val[1], wn.val[1]) );
|
||||
vst1_f32( (float *)(y1_ptr + 2*j), yn );
|
||||
|
||||
wn = vld2_f32( (float *)(w2_ptr + 2*j) );
|
||||
|
||||
yn = vfma_f32( x0, x1 , vtrn1_f32(wn.val[0], wn.val[0]) );
|
||||
yn = vfma_f32( yn, x1r, vtrn1_f32(wn.val[1], wn.val[1]) );
|
||||
yn = vfma_f32( yn, x2 , vtrn2_f32(wn.val[0], wn.val[0]) );
|
||||
yn = vfma_f32( yn, x2r, vtrn2_f32(wn.val[1], wn.val[1]) );
|
||||
vst1_f32( (float *)(y2_ptr + 2*j), yn );
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef TEST_NEON
|
||||
#define fft_bf3 neon_fft_bf3
|
||||
#endif
|
||||
|
||||
#endif /* fft_bf3 */
|
||||
|
||||
/**
|
||||
* FFT Butterfly 2 Points
|
||||
*/
|
||||
#ifndef fft_bf2
|
||||
|
||||
LC3_HOT static inline void neon_fft_bf2(
|
||||
const struct lc3_fft_bf2_twiddles *twiddles,
|
||||
const struct lc3_complex *x, struct lc3_complex *y, int n)
|
||||
{
|
||||
int n2 = twiddles->n2;
|
||||
const struct lc3_complex *w_ptr = twiddles->t;
|
||||
|
||||
const struct lc3_complex *x0_ptr = x;
|
||||
const struct lc3_complex *x1_ptr = x0_ptr + n*n2;
|
||||
|
||||
struct lc3_complex *y0_ptr = y;
|
||||
struct lc3_complex *y1_ptr = y0_ptr + n2;
|
||||
|
||||
for (int j, i = 0; i < n; i++, y0_ptr += 2*n2, y1_ptr += 2*n2) {
|
||||
|
||||
/* --- Process by pair --- */
|
||||
|
||||
for (j = 0; j < (n2 >> 1); j++, x0_ptr += 2, x1_ptr += 2) {
|
||||
|
||||
float32x4_t x0 = vld1q_f32( (float *)x0_ptr );
|
||||
float32x4_t x1 = vld1q_f32( (float *)x1_ptr );
|
||||
float32x4_t y0, y1;
|
||||
|
||||
float32x4_t x1r = vtrn1q_f32( vrev64q_f32(vnegq_f32(x1)), x1 );
|
||||
|
||||
float32x4_t w = vld1q_f32( (float *)(w_ptr + 2*j) );
|
||||
float32x4_t w_re = vtrn1q_f32(w, w);
|
||||
float32x4_t w_im = vtrn2q_f32(w, w);
|
||||
|
||||
y0 = vfmaq_f32( x0, x1 , w_re );
|
||||
y0 = vfmaq_f32( y0, x1r, w_im );
|
||||
vst1q_f32( (float *)(y0_ptr + 2*j), y0 );
|
||||
|
||||
y1 = vfmsq_f32( x0, x1 , w_re );
|
||||
y1 = vfmsq_f32( y1, x1r, w_im );
|
||||
vst1q_f32( (float *)(y1_ptr + 2*j), y1 );
|
||||
}
|
||||
|
||||
/* --- Last iteration --- */
|
||||
|
||||
if (n2 & 1) {
|
||||
|
||||
float32x2_t x0 = vld1_f32( (float *)(x0_ptr++) );
|
||||
float32x2_t x1 = vld1_f32( (float *)(x1_ptr++) );
|
||||
float32x2_t y0, y1;
|
||||
|
||||
float32x2_t x1r = vtrn1_f32( vrev64_f32(vneg_f32(x1)), x1 );
|
||||
|
||||
float32x2_t w = vld1_f32( (float *)(w_ptr + 2*j) );
|
||||
float32x2_t w_re = vtrn1_f32(w, w);
|
||||
float32x2_t w_im = vtrn2_f32(w, w);
|
||||
|
||||
y0 = vfma_f32( x0, x1 , w_re );
|
||||
y0 = vfma_f32( y0, x1r, w_im );
|
||||
vst1_f32( (float *)(y0_ptr + 2*j), y0 );
|
||||
|
||||
y1 = vfms_f32( x0, x1 , w_re );
|
||||
y1 = vfms_f32( y1, x1r, w_im );
|
||||
vst1_f32( (float *)(y1_ptr + 2*j), y1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef TEST_NEON
|
||||
#define fft_bf2 neon_fft_bf2
|
||||
#endif
|
||||
|
||||
#endif /* fft_bf2 */
|
||||
|
||||
#endif /* __ARM_NEON && __ARM_ARCH_ISA_A64 */
|
||||
@@ -0,0 +1,62 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#include "plc.h"
|
||||
#include "tables.h"
|
||||
|
||||
|
||||
/**
|
||||
* Reset Packet Loss Concealment state
|
||||
*/
|
||||
void lc3_plc_reset(struct lc3_plc_state *plc)
|
||||
{
|
||||
plc->seed = 24607;
|
||||
lc3_plc_suspend(plc);
|
||||
}
|
||||
|
||||
/**
|
||||
* Suspend PLC execution (Good frame received)
|
||||
*/
|
||||
void lc3_plc_suspend(struct lc3_plc_state *plc)
|
||||
{
|
||||
plc->count = 1;
|
||||
plc->alpha = 1.0f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Synthesis of a PLC frame
|
||||
*/
|
||||
void lc3_plc_synthesize(enum lc3_dt dt, enum lc3_srate sr,
|
||||
struct lc3_plc_state *plc, const float *x, float *y)
|
||||
{
|
||||
uint16_t seed = plc->seed;
|
||||
float alpha = plc->alpha;
|
||||
int ne = lc3_ne(dt, sr);
|
||||
|
||||
alpha *= (plc->count < 4 ? 1.0f :
|
||||
plc->count < 8 ? 0.9f : 0.85f);
|
||||
|
||||
for (int i = 0; i < ne; i++) {
|
||||
seed = (16831 + seed * 12821) & 0xffff;
|
||||
y[i] = alpha * (seed & 0x8000 ? -x[i] : x[i]);
|
||||
}
|
||||
|
||||
plc->seed = seed;
|
||||
plc->alpha = alpha;
|
||||
plc->count++;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __LC3_PLC_H
|
||||
#define __LC3_PLC_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
||||
/**
|
||||
* Reset PLC state
|
||||
* plc PLC State to reset
|
||||
*/
|
||||
void lc3_plc_reset(lc3_plc_state_t *plc);
|
||||
|
||||
/**
|
||||
* Suspend PLC synthesis (Error-free frame decoded)
|
||||
* plc PLC State
|
||||
*/
|
||||
void lc3_plc_suspend(lc3_plc_state_t *plc);
|
||||
|
||||
/**
|
||||
* Synthesis of a PLC frame
|
||||
* dt, sr Duration and samplerate of the frame
|
||||
* plc PLC State
|
||||
* x Last good spectral coefficients
|
||||
* y Return emulated ones
|
||||
*
|
||||
* `x` and `y` can be the same buffer
|
||||
*/
|
||||
void lc3_plc_synthesize(enum lc3_dt dt, enum lc3_srate sr,
|
||||
lc3_plc_state_t *plc, const float *x, float *y);
|
||||
|
||||
|
||||
#endif /* __LC3_PLC_H */
|
||||
@@ -0,0 +1,929 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#include "sns.h"
|
||||
#include "tables.h"
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* DCT-16
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Matrix of DCT-16 coefficients
|
||||
*
|
||||
* M[n][k] = 2f cos( Pi k (2n + 1) / 2N )
|
||||
*
|
||||
* k = [0..N-1], n = [0..N-1], N = 16
|
||||
* f = sqrt(1/4N) for k=0, sqrt(1/2N) otherwise
|
||||
*/
|
||||
static const float dct16_m[16][16] = {
|
||||
|
||||
{ 2.50000000e-01, 3.51850934e-01, 3.46759961e-01, 3.38329500e-01,
|
||||
3.26640741e-01, 3.11806253e-01, 2.93968901e-01, 2.73300467e-01,
|
||||
2.50000000e-01, 2.24291897e-01, 1.96423740e-01, 1.66663915e-01,
|
||||
1.35299025e-01, 1.02631132e-01, 6.89748448e-02, 3.46542923e-02 },
|
||||
|
||||
{ 2.50000000e-01, 3.38329500e-01, 2.93968901e-01, 2.24291897e-01,
|
||||
1.35299025e-01, 3.46542923e-02, -6.89748448e-02, -1.66663915e-01,
|
||||
-2.50000000e-01, -3.11806253e-01, -3.46759961e-01, -3.51850934e-01,
|
||||
-3.26640741e-01, -2.73300467e-01, -1.96423740e-01, -1.02631132e-01 },
|
||||
|
||||
{ 2.50000000e-01, 3.11806253e-01, 1.96423740e-01, 3.46542923e-02,
|
||||
-1.35299025e-01, -2.73300467e-01, -3.46759961e-01, -3.38329500e-01,
|
||||
-2.50000000e-01, -1.02631132e-01, 6.89748448e-02, 2.24291897e-01,
|
||||
3.26640741e-01, 3.51850934e-01, 2.93968901e-01, 1.66663915e-01 },
|
||||
|
||||
{ 2.50000000e-01, 2.73300467e-01, 6.89748448e-02, -1.66663915e-01,
|
||||
-3.26640741e-01, -3.38329500e-01, -1.96423740e-01, 3.46542923e-02,
|
||||
2.50000000e-01, 3.51850934e-01, 2.93968901e-01, 1.02631132e-01,
|
||||
-1.35299025e-01, -3.11806253e-01, -3.46759961e-01, -2.24291897e-01 },
|
||||
|
||||
{ 2.50000000e-01, 2.24291897e-01, -6.89748448e-02, -3.11806253e-01,
|
||||
-3.26640741e-01, -1.02631132e-01, 1.96423740e-01, 3.51850934e-01,
|
||||
2.50000000e-01, -3.46542923e-02, -2.93968901e-01, -3.38329500e-01,
|
||||
-1.35299025e-01, 1.66663915e-01, 3.46759961e-01, 2.73300467e-01 },
|
||||
|
||||
{ 2.50000000e-01, 1.66663915e-01, -1.96423740e-01, -3.51850934e-01,
|
||||
-1.35299025e-01, 2.24291897e-01, 3.46759961e-01, 1.02631132e-01,
|
||||
-2.50000000e-01, -3.38329500e-01, -6.89748448e-02, 2.73300467e-01,
|
||||
3.26640741e-01, 3.46542923e-02, -2.93968901e-01, -3.11806253e-01 },
|
||||
|
||||
{ 2.50000000e-01, 1.02631132e-01, -2.93968901e-01, -2.73300467e-01,
|
||||
1.35299025e-01, 3.51850934e-01, 6.89748448e-02, -3.11806253e-01,
|
||||
-2.50000000e-01, 1.66663915e-01, 3.46759961e-01, 3.46542923e-02,
|
||||
-3.26640741e-01, -2.24291897e-01, 1.96423740e-01, 3.38329500e-01 },
|
||||
|
||||
{ 2.50000000e-01, 3.46542923e-02, -3.46759961e-01, -1.02631132e-01,
|
||||
3.26640741e-01, 1.66663915e-01, -2.93968901e-01, -2.24291897e-01,
|
||||
2.50000000e-01, 2.73300467e-01, -1.96423740e-01, -3.11806253e-01,
|
||||
1.35299025e-01, 3.38329500e-01, -6.89748448e-02, -3.51850934e-01 },
|
||||
|
||||
{ 2.50000000e-01, -3.46542923e-02, -3.46759961e-01, 1.02631132e-01,
|
||||
3.26640741e-01, -1.66663915e-01, -2.93968901e-01, 2.24291897e-01,
|
||||
2.50000000e-01, -2.73300467e-01, -1.96423740e-01, 3.11806253e-01,
|
||||
1.35299025e-01, -3.38329500e-01, -6.89748448e-02, 3.51850934e-01 },
|
||||
|
||||
{ 2.50000000e-01, -1.02631132e-01, -2.93968901e-01, 2.73300467e-01,
|
||||
1.35299025e-01, -3.51850934e-01, 6.89748448e-02, 3.11806253e-01,
|
||||
-2.50000000e-01, -1.66663915e-01, 3.46759961e-01, -3.46542923e-02,
|
||||
-3.26640741e-01, 2.24291897e-01, 1.96423740e-01, -3.38329500e-01 },
|
||||
|
||||
{ 2.50000000e-01, -1.66663915e-01, -1.96423740e-01, 3.51850934e-01,
|
||||
-1.35299025e-01, -2.24291897e-01, 3.46759961e-01, -1.02631132e-01,
|
||||
-2.50000000e-01, 3.38329500e-01, -6.89748448e-02, -2.73300467e-01,
|
||||
3.26640741e-01, -3.46542923e-02, -2.93968901e-01, 3.11806253e-01 },
|
||||
|
||||
{ 2.50000000e-01, -2.24291897e-01, -6.89748448e-02, 3.11806253e-01,
|
||||
-3.26640741e-01, 1.02631132e-01, 1.96423740e-01, -3.51850934e-01,
|
||||
2.50000000e-01, 3.46542923e-02, -2.93968901e-01, 3.38329500e-01,
|
||||
-1.35299025e-01, -1.66663915e-01, 3.46759961e-01, -2.73300467e-01 },
|
||||
|
||||
{ 2.50000000e-01, -2.73300467e-01, 6.89748448e-02, 1.66663915e-01,
|
||||
-3.26640741e-01, 3.38329500e-01, -1.96423740e-01, -3.46542923e-02,
|
||||
2.50000000e-01, -3.51850934e-01, 2.93968901e-01, -1.02631132e-01,
|
||||
-1.35299025e-01, 3.11806253e-01, -3.46759961e-01, 2.24291897e-01 },
|
||||
|
||||
{ 2.50000000e-01, -3.11806253e-01, 1.96423740e-01, -3.46542923e-02,
|
||||
-1.35299025e-01, 2.73300467e-01, -3.46759961e-01, 3.38329500e-01,
|
||||
-2.50000000e-01, 1.02631132e-01, 6.89748448e-02, -2.24291897e-01,
|
||||
3.26640741e-01, -3.51850934e-01, 2.93968901e-01, -1.66663915e-01 },
|
||||
|
||||
{ 2.50000000e-01, -3.38329500e-01, 2.93968901e-01, -2.24291897e-01,
|
||||
1.35299025e-01, -3.46542923e-02, -6.89748448e-02, 1.66663915e-01,
|
||||
-2.50000000e-01, 3.11806253e-01, -3.46759961e-01, 3.51850934e-01,
|
||||
-3.26640741e-01, 2.73300467e-01, -1.96423740e-01, 1.02631132e-01 },
|
||||
|
||||
{ 2.50000000e-01, -3.51850934e-01, 3.46759961e-01, -3.38329500e-01,
|
||||
3.26640741e-01, -3.11806253e-01, 2.93968901e-01, -2.73300467e-01,
|
||||
2.50000000e-01, -2.24291897e-01, 1.96423740e-01, -1.66663915e-01,
|
||||
1.35299025e-01, -1.02631132e-01, 6.89748448e-02, -3.46542923e-02 },
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Forward DCT-16 transformation
|
||||
* x, y Input and output 16 values
|
||||
*/
|
||||
LC3_HOT static void dct16_forward(const float *x, float *y)
|
||||
{
|
||||
for (int i = 0, j; i < 16; i++)
|
||||
for (y[i] = 0, j = 0; j < 16; j++)
|
||||
y[i] += x[j] * dct16_m[j][i];
|
||||
}
|
||||
|
||||
/**
|
||||
* Inverse DCT-16 transformation
|
||||
* x, y Input and output 16 values
|
||||
*/
|
||||
LC3_HOT static void dct16_inverse(const float *x, float *y)
|
||||
{
|
||||
for (int i = 0, j; i < 16; i++)
|
||||
for (y[i] = 0, j = 0; j < 16; j++)
|
||||
y[i] += x[j] * dct16_m[i][j];
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Scale factors
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Scale factors
|
||||
* dt, sr Duration and samplerate of the frame
|
||||
* nbytes Size in bytes of the frame
|
||||
* eb Energy estimation per bands
|
||||
* att 1: Attack detected 0: Otherwise
|
||||
* scf Output 16 scale factors
|
||||
*/
|
||||
LC3_HOT static void compute_scale_factors(
|
||||
enum lc3_dt dt, enum lc3_srate sr, int nbytes,
|
||||
const float *eb, bool att, float *scf)
|
||||
{
|
||||
/* Pre-emphasis gain table :
|
||||
* Ge[b] = 10 ^ (b * g_tilt) / 630 , b = [0..63] */
|
||||
|
||||
static const float ge_14[LC3_MAX_BANDS] = { /* g_tilt = 14 */
|
||||
1.00000000e+00, 1.05250029e+00, 1.10775685e+00, 1.16591440e+00,
|
||||
1.22712524e+00, 1.29154967e+00, 1.35935639e+00, 1.43072299e+00,
|
||||
1.50583635e+00, 1.58489319e+00, 1.66810054e+00, 1.75567629e+00,
|
||||
1.84784980e+00, 1.94486244e+00, 2.04696827e+00, 2.15443469e+00,
|
||||
2.26754313e+00, 2.38658979e+00, 2.51188643e+00, 2.64376119e+00,
|
||||
2.78255940e+00, 2.92864456e+00, 3.08239924e+00, 3.24422608e+00,
|
||||
3.41454887e+00, 3.59381366e+00, 3.78248991e+00, 3.98107171e+00,
|
||||
4.19007911e+00, 4.41005945e+00, 4.64158883e+00, 4.88527357e+00,
|
||||
5.14175183e+00, 5.41169527e+00, 5.69581081e+00, 5.99484250e+00,
|
||||
6.30957344e+00, 6.64082785e+00, 6.98947321e+00, 7.35642254e+00,
|
||||
7.74263683e+00, 8.14912747e+00, 8.57695899e+00, 9.02725178e+00,
|
||||
9.50118507e+00, 1.00000000e+01, 1.05250029e+01, 1.10775685e+01,
|
||||
1.16591440e+01, 1.22712524e+01, 1.29154967e+01, 1.35935639e+01,
|
||||
1.43072299e+01, 1.50583635e+01, 1.58489319e+01, 1.66810054e+01,
|
||||
1.75567629e+01, 1.84784980e+01, 1.94486244e+01, 2.04696827e+01,
|
||||
2.15443469e+01, 2.26754313e+01, 2.38658979e+01, 2.51188643e+01 };
|
||||
|
||||
static const float ge_18[LC3_MAX_BANDS] = { /* g_tilt = 18 */
|
||||
1.00000000e+00, 1.06800043e+00, 1.14062492e+00, 1.21818791e+00,
|
||||
1.30102522e+00, 1.38949549e+00, 1.48398179e+00, 1.58489319e+00,
|
||||
1.69266662e+00, 1.80776868e+00, 1.93069773e+00, 2.06198601e+00,
|
||||
2.20220195e+00, 2.35195264e+00, 2.51188643e+00, 2.68269580e+00,
|
||||
2.86512027e+00, 3.05994969e+00, 3.26802759e+00, 3.49025488e+00,
|
||||
3.72759372e+00, 3.98107171e+00, 4.25178630e+00, 4.54090961e+00,
|
||||
4.84969343e+00, 5.17947468e+00, 5.53168120e+00, 5.90783791e+00,
|
||||
6.30957344e+00, 6.73862717e+00, 7.19685673e+00, 7.68624610e+00,
|
||||
8.20891416e+00, 8.76712387e+00, 9.36329209e+00, 1.00000000e+01,
|
||||
1.06800043e+01, 1.14062492e+01, 1.21818791e+01, 1.30102522e+01,
|
||||
1.38949549e+01, 1.48398179e+01, 1.58489319e+01, 1.69266662e+01,
|
||||
1.80776868e+01, 1.93069773e+01, 2.06198601e+01, 2.20220195e+01,
|
||||
2.35195264e+01, 2.51188643e+01, 2.68269580e+01, 2.86512027e+01,
|
||||
3.05994969e+01, 3.26802759e+01, 3.49025488e+01, 3.72759372e+01,
|
||||
3.98107171e+01, 4.25178630e+01, 4.54090961e+01, 4.84969343e+01,
|
||||
5.17947468e+01, 5.53168120e+01, 5.90783791e+01, 6.30957344e+01 };
|
||||
|
||||
static const float ge_22[LC3_MAX_BANDS] = { /* g_tilt = 22 */
|
||||
1.00000000e+00, 1.08372885e+00, 1.17446822e+00, 1.27280509e+00,
|
||||
1.37937560e+00, 1.49486913e+00, 1.62003281e+00, 1.75567629e+00,
|
||||
1.90267705e+00, 2.06198601e+00, 2.23463373e+00, 2.42173704e+00,
|
||||
2.62450630e+00, 2.84425319e+00, 3.08239924e+00, 3.34048498e+00,
|
||||
3.62017995e+00, 3.92329345e+00, 4.25178630e+00, 4.60778348e+00,
|
||||
4.99358789e+00, 5.41169527e+00, 5.86481029e+00, 6.35586411e+00,
|
||||
6.88803330e+00, 7.46476041e+00, 8.08977621e+00, 8.76712387e+00,
|
||||
9.50118507e+00, 1.02967084e+01, 1.11588399e+01, 1.20931568e+01,
|
||||
1.31057029e+01, 1.42030283e+01, 1.53922315e+01, 1.66810054e+01,
|
||||
1.80776868e+01, 1.95913107e+01, 2.12316686e+01, 2.30093718e+01,
|
||||
2.49359200e+01, 2.70237760e+01, 2.92864456e+01, 3.17385661e+01,
|
||||
3.43959997e+01, 3.72759372e+01, 4.03970086e+01, 4.37794036e+01,
|
||||
4.74450028e+01, 5.14175183e+01, 5.57226480e+01, 6.03882412e+01,
|
||||
6.54444792e+01, 7.09240702e+01, 7.68624610e+01, 8.32980665e+01,
|
||||
9.02725178e+01, 9.78309319e+01, 1.06022203e+02, 1.14899320e+02,
|
||||
1.24519708e+02, 1.34945600e+02, 1.46244440e+02, 1.58489319e+02 };
|
||||
|
||||
static const float ge_26[LC3_MAX_BANDS] = { /* g_tilt = 26 */
|
||||
1.00000000e+00, 1.09968890e+00, 1.20931568e+00, 1.32987103e+00,
|
||||
1.46244440e+00, 1.60823388e+00, 1.76855694e+00, 1.94486244e+00,
|
||||
2.13874364e+00, 2.35195264e+00, 2.58641621e+00, 2.84425319e+00,
|
||||
3.12779366e+00, 3.43959997e+00, 3.78248991e+00, 4.15956216e+00,
|
||||
4.57422434e+00, 5.03022373e+00, 5.53168120e+00, 6.08312841e+00,
|
||||
6.68954879e+00, 7.35642254e+00, 8.08977621e+00, 8.89623710e+00,
|
||||
9.78309319e+00, 1.07583590e+01, 1.18308480e+01, 1.30102522e+01,
|
||||
1.43072299e+01, 1.57335019e+01, 1.73019574e+01, 1.90267705e+01,
|
||||
2.09235283e+01, 2.30093718e+01, 2.53031508e+01, 2.78255940e+01,
|
||||
3.05994969e+01, 3.36499270e+01, 3.70044512e+01, 4.06933843e+01,
|
||||
4.47500630e+01, 4.92111475e+01, 5.41169527e+01, 5.95118121e+01,
|
||||
6.54444792e+01, 7.19685673e+01, 7.91430346e+01, 8.70327166e+01,
|
||||
9.57089124e+01, 1.05250029e+02, 1.15742288e+02, 1.27280509e+02,
|
||||
1.39968963e+02, 1.53922315e+02, 1.69266662e+02, 1.86140669e+02,
|
||||
2.04696827e+02, 2.25102829e+02, 2.47543082e+02, 2.72220379e+02,
|
||||
2.99357729e+02, 3.29200372e+02, 3.62017995e+02, 3.98107171e+02 };
|
||||
|
||||
static const float ge_30[LC3_MAX_BANDS] = { /* g_tilt = 30 */
|
||||
1.00000000e+00, 1.11588399e+00, 1.24519708e+00, 1.38949549e+00,
|
||||
1.55051578e+00, 1.73019574e+00, 1.93069773e+00, 2.15443469e+00,
|
||||
2.40409918e+00, 2.68269580e+00, 2.99357729e+00, 3.34048498e+00,
|
||||
3.72759372e+00, 4.15956216e+00, 4.64158883e+00, 5.17947468e+00,
|
||||
5.77969288e+00, 6.44946677e+00, 7.19685673e+00, 8.03085722e+00,
|
||||
8.96150502e+00, 1.00000000e+01, 1.11588399e+01, 1.24519708e+01,
|
||||
1.38949549e+01, 1.55051578e+01, 1.73019574e+01, 1.93069773e+01,
|
||||
2.15443469e+01, 2.40409918e+01, 2.68269580e+01, 2.99357729e+01,
|
||||
3.34048498e+01, 3.72759372e+01, 4.15956216e+01, 4.64158883e+01,
|
||||
5.17947468e+01, 5.77969288e+01, 6.44946677e+01, 7.19685673e+01,
|
||||
8.03085722e+01, 8.96150502e+01, 1.00000000e+02, 1.11588399e+02,
|
||||
1.24519708e+02, 1.38949549e+02, 1.55051578e+02, 1.73019574e+02,
|
||||
1.93069773e+02, 2.15443469e+02, 2.40409918e+02, 2.68269580e+02,
|
||||
2.99357729e+02, 3.34048498e+02, 3.72759372e+02, 4.15956216e+02,
|
||||
4.64158883e+02, 5.17947468e+02, 5.77969288e+02, 6.44946677e+02,
|
||||
7.19685673e+02, 8.03085722e+02, 8.96150502e+02, 1.00000000e+03 };
|
||||
|
||||
#if LC3_PLUS_HR
|
||||
|
||||
static const float ge_34[LC3_MAX_BANDS] = { /* g_tilt = 34 */
|
||||
1.00000000e+00, 1.13231759e+00, 1.28214312e+00, 1.45179321e+00,
|
||||
1.64389099e+00, 1.86140669e+00, 2.10770353e+00, 2.38658979e+00,
|
||||
2.70237760e+00, 3.05994969e+00, 3.46483486e+00, 3.92329345e+00,
|
||||
4.44241419e+00, 5.03022373e+00, 5.69581081e+00, 6.44946677e+00,
|
||||
7.30284467e+00, 8.26913948e+00, 9.36329209e+00, 1.06022203e+01,
|
||||
1.20050806e+01, 1.35935639e+01, 1.53922315e+01, 1.74288945e+01,
|
||||
1.97350438e+01, 2.23463373e+01, 2.53031508e+01, 2.86512027e+01,
|
||||
3.24422608e+01, 3.67349426e+01, 4.15956216e+01, 4.70994540e+01,
|
||||
5.33315403e+01, 6.03882412e+01, 6.83786677e+01, 7.74263683e+01,
|
||||
8.76712387e+01, 9.92716858e+01, 1.12407076e+02, 1.27280509e+02,
|
||||
1.44121960e+02, 1.63191830e+02, 1.84784980e+02, 2.09235283e+02,
|
||||
2.36920791e+02, 2.68269580e+02, 3.03766364e+02, 3.43959997e+02,
|
||||
3.89471955e+02, 4.41005945e+02, 4.99358789e+02, 5.65432741e+02,
|
||||
6.40249439e+02, 7.24965701e+02, 8.20891416e+02, 9.29509790e+02,
|
||||
1.05250029e+03, 1.19176459e+03, 1.34945600e+03, 1.52801277e+03,
|
||||
1.73019574e+03, 1.95913107e+03, 2.21835857e+03, 2.51188643e+03 };
|
||||
|
||||
#endif /* LC3_PLUS_HR */
|
||||
|
||||
static const float *ge_table[LC3_NUM_SRATE] = {
|
||||
[LC3_SRATE_8K ] = ge_14, [LC3_SRATE_16K ] = ge_18,
|
||||
[LC3_SRATE_24K ] = ge_22, [LC3_SRATE_32K ] = ge_26,
|
||||
[LC3_SRATE_48K ] = ge_30,
|
||||
|
||||
#if LC3_PLUS_HR
|
||||
[LC3_SRATE_48K_HR] = ge_30, [LC3_SRATE_96K_HR] = ge_34,
|
||||
#endif /* LC3_PLUS_HR */
|
||||
|
||||
};
|
||||
|
||||
float e[LC3_MAX_BANDS];
|
||||
|
||||
/* --- Copy and padding --- */
|
||||
|
||||
int nb = lc3_num_bands[dt][sr];
|
||||
int n4 = nb < 32 ? 32 % nb : 0;
|
||||
int n2 = nb < 32 ? nb - n4 : LC3_MAX_BANDS - nb;
|
||||
|
||||
for (int i4 = 0; i4 < n4; i4++)
|
||||
e[4*i4 + 0] = e[4*i4 + 1] =
|
||||
e[4*i4 + 2] = e[4*i4 + 3] = eb[i4];
|
||||
|
||||
for (int i2 = n4; i2 < n4+n2; i2++)
|
||||
e[2*(n4+i2) + 0] = e[2*(n4+i2) + 1] = eb[i2];
|
||||
|
||||
memcpy(e + 4*n4 + 2*n2, eb + n4 + n2, (nb - n4 - n2) * sizeof(float));
|
||||
|
||||
/* --- Smoothing, pre-emphasis and logarithm --- */
|
||||
|
||||
const float *ge = ge_table[sr];
|
||||
|
||||
float e0 = e[0], e1 = e[0], e2;
|
||||
float e_sum = 0;
|
||||
|
||||
for (int i = 0; i < LC3_MAX_BANDS-1; ) {
|
||||
e[i] = (e0 * 0.25f + e1 * 0.5f + (e2 = e[i+1]) * 0.25f) * ge[i];
|
||||
e_sum += e[i++];
|
||||
|
||||
e[i] = (e1 * 0.25f + e2 * 0.5f + (e0 = e[i+1]) * 0.25f) * ge[i];
|
||||
e_sum += e[i++];
|
||||
|
||||
e[i] = (e2 * 0.25f + e0 * 0.5f + (e1 = e[i+1]) * 0.25f) * ge[i];
|
||||
e_sum += e[i++];
|
||||
}
|
||||
|
||||
e[LC3_MAX_BANDS-1] = (e0 * 0.25f + e1 * 0.75f) * ge[LC3_MAX_BANDS-1];
|
||||
e_sum += e[LC3_MAX_BANDS-1];
|
||||
|
||||
float noise_floor = fmaxf(e_sum * (1e-4f / 64), 0x1p-32f);
|
||||
|
||||
for (int i = 0; i < LC3_MAX_BANDS; i++)
|
||||
e[i] = lc3_log2f(fmaxf(e[i], noise_floor)) * 0.5f;
|
||||
|
||||
/* --- Grouping & scaling --- */
|
||||
|
||||
float scf_sum;
|
||||
|
||||
scf[0] = (e[0] + e[4]) * 1.f/12 +
|
||||
(e[0] + e[3]) * 2.f/12 +
|
||||
(e[1] + e[2]) * 3.f/12 ;
|
||||
scf_sum = scf[0];
|
||||
|
||||
for (int i = 1; i < 15; i++) {
|
||||
scf[i] = (e[4*i-1] + e[4*i+4]) * 1.f/12 +
|
||||
(e[4*i ] + e[4*i+3]) * 2.f/12 +
|
||||
(e[4*i+1] + e[4*i+2]) * 3.f/12 ;
|
||||
scf_sum += scf[i];
|
||||
}
|
||||
|
||||
scf[15] = (e[59] + e[63]) * 1.f/12 +
|
||||
(e[60] + e[63]) * 2.f/12 +
|
||||
(e[61] + e[62]) * 3.f/12 ;
|
||||
scf_sum += scf[15];
|
||||
|
||||
float cf = lc3_hr(sr) ? 0.6f : 0.85f;
|
||||
if (lc3_hr(sr) && 8 * nbytes >
|
||||
(dt < LC3_DT_10M ? 1150 * (int)(1 + dt) : 4400))
|
||||
cf *= dt < LC3_DT_10M ? 0.25f : 0.35f;
|
||||
|
||||
for (int i = 0; i < 16; i++)
|
||||
scf[i] = cf * (scf[i] - scf_sum * 1.f/16);
|
||||
|
||||
/* --- Attack handling --- */
|
||||
|
||||
if (!att)
|
||||
return;
|
||||
|
||||
float s0, s1 = scf[0], s2 = scf[1], s3 = scf[2], s4 = scf[3];
|
||||
float sn = s1 + s2;
|
||||
|
||||
scf[0] = (sn += s3) * 1.f/3;
|
||||
scf[1] = (sn += s4) * 1.f/4;
|
||||
scf_sum = scf[0] + scf[1];
|
||||
|
||||
for (int i = 2; i < 14; i++, sn -= s0) {
|
||||
s0 = s1, s1 = s2, s2 = s3, s3 = s4, s4 = scf[i+2];
|
||||
scf[i] = (sn += s4) * 1.f/5;
|
||||
scf_sum += scf[i];
|
||||
}
|
||||
|
||||
scf[14] = (sn ) * 1.f/4;
|
||||
scf[15] = (sn -= s1) * 1.f/3;
|
||||
scf_sum += scf[14] + scf[15];
|
||||
|
||||
for (int i = 0; i < 16; i++)
|
||||
scf[i] = (dt == LC3_DT_7M5 ? 0.3f : 0.5f) *
|
||||
(scf[i] - scf_sum * 1.f/16);
|
||||
}
|
||||
|
||||
/**
|
||||
* Codebooks
|
||||
* scf Input 16 scale factors
|
||||
* lf/hfcb_idx Output the low and high frequency codebooks index
|
||||
*/
|
||||
LC3_HOT static void resolve_codebooks(
|
||||
const float *scf, int *lfcb_idx, int *hfcb_idx)
|
||||
{
|
||||
float dlfcb_max = 0, dhfcb_max = 0;
|
||||
*lfcb_idx = *hfcb_idx = 0;
|
||||
|
||||
for (int icb = 0; icb < 32; icb++) {
|
||||
const float *lfcb = lc3_sns_lfcb[icb];
|
||||
const float *hfcb = lc3_sns_hfcb[icb];
|
||||
float dlfcb = 0, dhfcb = 0;
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
dlfcb += (scf[ i] - lfcb[i]) * (scf[ i] - lfcb[i]);
|
||||
dhfcb += (scf[8+i] - hfcb[i]) * (scf[8+i] - hfcb[i]);
|
||||
}
|
||||
|
||||
if (icb == 0 || dlfcb < dlfcb_max)
|
||||
*lfcb_idx = icb, dlfcb_max = dlfcb;
|
||||
|
||||
if (icb == 0 || dhfcb < dhfcb_max)
|
||||
*hfcb_idx = icb, dhfcb_max = dhfcb;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unit energy normalize pulse configuration
|
||||
* c Pulse configuration
|
||||
* cn Normalized pulse configuration
|
||||
*/
|
||||
LC3_HOT static void normalize(const int *c, float *cn)
|
||||
{
|
||||
int c2_sum = 0;
|
||||
for (int i = 0; i < 16; i++)
|
||||
c2_sum += c[i] * c[i];
|
||||
|
||||
float c_norm = 1.f / sqrtf(c2_sum);
|
||||
|
||||
for (int i = 0; i < 16; i++)
|
||||
cn[i] = c[i] * c_norm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sub-procedure of `quantize()`, add unit pulse
|
||||
* x, y, n Transformed residual, and vector of pulses with length
|
||||
* start, end Current number of pulses, limit to reach
|
||||
* corr, energy Correlation (x,y) and y energy, updated at output
|
||||
*/
|
||||
LC3_HOT static void add_pulse(const float *x, int *y, int n,
|
||||
int start, int end, float *corr, float *energy)
|
||||
{
|
||||
for (int k = start; k < end; k++) {
|
||||
float best_c2 = (*corr + x[0]) * (*corr + x[0]);
|
||||
float best_e = *energy + 2*y[0] + 1;
|
||||
int nbest = 0;
|
||||
|
||||
for (int i = 1; i < n; i++) {
|
||||
float c2 = (*corr + x[i]) * (*corr + x[i]);
|
||||
float e = *energy + 2*y[i] + 1;
|
||||
|
||||
if (c2 * best_e > e * best_c2)
|
||||
best_c2 = c2, best_e = e, nbest = i;
|
||||
}
|
||||
|
||||
*corr += x[nbest];
|
||||
*energy += 2*y[nbest] + 1;
|
||||
y[nbest]++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Quantization of codebooks residual
|
||||
* scf Input 16 scale factors, output quantized version
|
||||
* lf/hfcb_idx Codebooks index
|
||||
* c, cn Output 4 pulse configurations candidates, normalized
|
||||
* shape/gain_idx Output selected shape/gain indexes
|
||||
*/
|
||||
LC3_HOT static void quantize(const float *scf, int lfcb_idx, int hfcb_idx,
|
||||
int (*c)[16], float (*cn)[16], int *shape_idx, int *gain_idx)
|
||||
{
|
||||
/* --- Residual --- */
|
||||
|
||||
const float *lfcb = lc3_sns_lfcb[lfcb_idx];
|
||||
const float *hfcb = lc3_sns_hfcb[hfcb_idx];
|
||||
float r[16], x[16];
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
r[ i] = scf[ i] - lfcb[i];
|
||||
r[8+i] = scf[8+i] - hfcb[i];
|
||||
}
|
||||
|
||||
dct16_forward(r, x);
|
||||
|
||||
/* --- Shape 3 candidate ---
|
||||
* Project to or below pyramid N = 16, K = 6,
|
||||
* then add unit pulses until you reach K = 6, over N = 16 */
|
||||
|
||||
float xm[16];
|
||||
float xm_sum = 0;
|
||||
|
||||
for (int i = 0; i < 16; i++) {
|
||||
xm[i] = fabsf(x[i]);
|
||||
xm_sum += xm[i];
|
||||
}
|
||||
|
||||
float proj_factor = (6 - 1) / fmaxf(xm_sum, 1e-31f);
|
||||
float corr = 0, energy = 0;
|
||||
int npulses = 0;
|
||||
|
||||
for (int i = 0; i < 16; i++) {
|
||||
c[3][i] = floorf(xm[i] * proj_factor);
|
||||
npulses += c[3][i];
|
||||
corr += c[3][i] * xm[i];
|
||||
energy += c[3][i] * c[3][i];
|
||||
}
|
||||
|
||||
add_pulse(xm, c[3], 16, npulses, 6, &corr, &energy);
|
||||
npulses = 6;
|
||||
|
||||
/* --- Shape 2 candidate ---
|
||||
* Add unit pulses until you reach K = 8 on shape 3 */
|
||||
|
||||
memcpy(c[2], c[3], sizeof(c[2]));
|
||||
|
||||
add_pulse(xm, c[2], 16, npulses, 8, &corr, &energy);
|
||||
npulses = 8;
|
||||
|
||||
/* --- Shape 1 candidate ---
|
||||
* Remove any unit pulses from shape 2 that are not part of 0 to 9
|
||||
* Update energy and correlation terms accordingly
|
||||
* Add unit pulses until you reach K = 10, over N = 10 */
|
||||
|
||||
memcpy(c[1], c[2], sizeof(c[1]));
|
||||
|
||||
for (int i = 10; i < 16; i++) {
|
||||
c[1][i] = 0;
|
||||
npulses -= c[2][i];
|
||||
corr -= c[2][i] * xm[i];
|
||||
energy -= c[2][i] * c[2][i];
|
||||
}
|
||||
|
||||
add_pulse(xm, c[1], 10, npulses, 10, &corr, &energy);
|
||||
npulses = 10;
|
||||
|
||||
/* --- Shape 0 candidate ---
|
||||
* Add unit pulses until you reach K = 1, on shape 1 */
|
||||
|
||||
memcpy(c[0], c[1], sizeof(c[0]));
|
||||
|
||||
add_pulse(xm + 10, c[0] + 10, 6, 0, 1, &corr, &energy);
|
||||
|
||||
/* --- Add sign and unit energy normalize --- */
|
||||
|
||||
for (int j = 0; j < 16; j++)
|
||||
for (int i = 0; i < 4; i++)
|
||||
c[i][j] = x[j] < 0 ? -c[i][j] : c[i][j];
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
normalize(c[i], cn[i]);
|
||||
|
||||
/* --- Determe shape & gain index ---
|
||||
* Search the Mean Square Error, within (shape, gain) combinations */
|
||||
|
||||
float mse_min = FLT_MAX;
|
||||
*shape_idx = *gain_idx = 0;
|
||||
|
||||
for (int ic = 0; ic < 4; ic++) {
|
||||
const struct lc3_sns_vq_gains *cgains = lc3_sns_vq_gains + ic;
|
||||
float cmse_min = FLT_MAX;
|
||||
int cgain_idx = 0;
|
||||
|
||||
for (int ig = 0; ig < cgains->count; ig++) {
|
||||
float g = cgains->v[ig];
|
||||
|
||||
float mse = 0;
|
||||
for (int i = 0; i < 16; i++)
|
||||
mse += (x[i] - g * cn[ic][i]) * (x[i] - g * cn[ic][i]);
|
||||
|
||||
if (mse < cmse_min) {
|
||||
cgain_idx = ig,
|
||||
cmse_min = mse;
|
||||
}
|
||||
}
|
||||
|
||||
if (cmse_min < mse_min) {
|
||||
*shape_idx = ic, *gain_idx = cgain_idx;
|
||||
mse_min = cmse_min;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unquantization of codebooks residual
|
||||
* lf/hfcb_idx Low and high frequency codebooks index
|
||||
* c Table of normalized pulse configuration
|
||||
* shape/gain Selected shape/gain indexes
|
||||
* scf Return unquantized scale factors
|
||||
*/
|
||||
LC3_HOT static void unquantize(int lfcb_idx, int hfcb_idx,
|
||||
const float *c, int shape, int gain, float *scf)
|
||||
{
|
||||
const float *lfcb = lc3_sns_lfcb[lfcb_idx];
|
||||
const float *hfcb = lc3_sns_hfcb[hfcb_idx];
|
||||
float g = lc3_sns_vq_gains[shape].v[gain];
|
||||
|
||||
dct16_inverse(c, scf);
|
||||
|
||||
for (int i = 0; i < 8; i++)
|
||||
scf[i] = lfcb[i] + g * scf[i];
|
||||
|
||||
for (int i = 8; i < 16; i++)
|
||||
scf[i] = hfcb[i-8] + g * scf[i];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sub-procedure of `sns_enumerate()`, enumeration of a vector
|
||||
* c, n Table of pulse configuration, and length
|
||||
* idx, ls Return enumeration set
|
||||
*/
|
||||
static void enum_mvpq(const int *c, int n, int *idx, bool *ls)
|
||||
{
|
||||
int ci, i;
|
||||
|
||||
/* --- Scan for 1st significant coeff --- */
|
||||
|
||||
for (i = 0, c += n; (ci = *(--c)) == 0 && i < 15; i++);
|
||||
|
||||
*idx = 0;
|
||||
*ls = ci < 0;
|
||||
|
||||
/* --- Scan remaining coefficients --- */
|
||||
|
||||
unsigned j = LC3_ABS(ci);
|
||||
|
||||
for (i++; i < n; i++, j += LC3_ABS(ci)) {
|
||||
|
||||
if ((ci = *(--c)) != 0) {
|
||||
*idx = (*idx << 1) | *ls;
|
||||
*ls = ci < 0;
|
||||
}
|
||||
|
||||
*idx += lc3_sns_mpvq_offsets[i][LC3_MIN(j, 10)];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sub-procedure of `sns_deenumerate()`, deenumeration of a vector
|
||||
* idx, ls Enumeration set
|
||||
* npulses Number of pulses in the set
|
||||
* c, n Table of pulses configuration, and length
|
||||
*/
|
||||
static void deenum_mvpq(int idx, bool ls, int npulses, int *c, int n)
|
||||
{
|
||||
int i;
|
||||
|
||||
/* --- Scan for coefficients --- */
|
||||
|
||||
for (i = n-1; i >= 0 && idx; i--) {
|
||||
|
||||
int ci = 0;
|
||||
|
||||
for (ci = 0; idx < lc3_sns_mpvq_offsets[i][npulses - ci]; ci++);
|
||||
idx -= lc3_sns_mpvq_offsets[i][npulses - ci];
|
||||
|
||||
*(c++) = ls ? -ci : ci;
|
||||
npulses -= ci;
|
||||
if (ci > 0) {
|
||||
ls = idx & 1;
|
||||
idx >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* --- Set last significant --- */
|
||||
|
||||
int ci = npulses;
|
||||
|
||||
if (i-- >= 0)
|
||||
*(c++) = ls ? -ci : ci;
|
||||
|
||||
while (i-- >= 0)
|
||||
*(c++) = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* SNS Enumeration of PVQ configuration
|
||||
* shape Selected shape index
|
||||
* c Selected pulse configuration
|
||||
* idx_a, ls_a Return enumeration set A
|
||||
* idx_b, ls_b Return enumeration set B (shape = 0)
|
||||
*/
|
||||
static void enumerate(int shape, const int *c,
|
||||
int *idx_a, bool *ls_a, int *idx_b, bool *ls_b)
|
||||
{
|
||||
enum_mvpq(c, shape < 2 ? 10 : 16, idx_a, ls_a);
|
||||
|
||||
if (shape == 0)
|
||||
enum_mvpq(c + 10, 6, idx_b, ls_b);
|
||||
}
|
||||
|
||||
/**
|
||||
* SNS Deenumeration of PVQ configuration
|
||||
* shape Selected shape index
|
||||
* idx_a, ls_a enumeration set A
|
||||
* idx_b, ls_b enumeration set B (shape = 0)
|
||||
* c Return pulse configuration
|
||||
*/
|
||||
static void deenumerate(int shape,
|
||||
int idx_a, bool ls_a, int idx_b, bool ls_b, int *c)
|
||||
{
|
||||
int npulses_a = (const int []){ 10, 10, 8, 6 }[shape];
|
||||
|
||||
deenum_mvpq(idx_a, ls_a, npulses_a, c, shape < 2 ? 10 : 16);
|
||||
|
||||
if (shape == 0)
|
||||
deenum_mvpq(idx_b, ls_b, 1, c + 10, 6);
|
||||
else if (shape == 1)
|
||||
memset(c + 10, 0, 6 * sizeof(*c));
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Filtering
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Spectral shaping
|
||||
* dt, sr Duration and samplerate of the frame
|
||||
* scf_q Quantized scale factors
|
||||
* inv True on inverse shaping, False otherwise
|
||||
* x Spectral coefficients
|
||||
* y Return shapped coefficients
|
||||
*
|
||||
* `x` and `y` can be the same buffer
|
||||
*/
|
||||
LC3_HOT static void spectral_shaping(enum lc3_dt dt, enum lc3_srate sr,
|
||||
const float *scf_q, bool inv, const float *x, float *y)
|
||||
{
|
||||
/* --- Interpolate scale factors --- */
|
||||
|
||||
float scf[LC3_MAX_BANDS];
|
||||
float s0, s1 = inv ? -scf_q[0] : scf_q[0];
|
||||
|
||||
scf[0] = scf[1] = s1;
|
||||
for (int i = 0; i < 15; i++) {
|
||||
s0 = s1, s1 = inv ? -scf_q[i+1] : scf_q[i+1];
|
||||
scf[4*i+2] = s0 + 0.125f * (s1 - s0);
|
||||
scf[4*i+3] = s0 + 0.375f * (s1 - s0);
|
||||
scf[4*i+4] = s0 + 0.625f * (s1 - s0);
|
||||
scf[4*i+5] = s0 + 0.875f * (s1 - s0);
|
||||
}
|
||||
scf[62] = s1 + 0.125f * (s1 - s0);
|
||||
scf[63] = s1 + 0.375f * (s1 - s0);
|
||||
|
||||
int nb = lc3_num_bands[dt][sr];
|
||||
int n4 = nb < 32 ? 32 % nb : 0;
|
||||
int n2 = nb < 32 ? nb - n4 : LC3_MAX_BANDS - nb;
|
||||
|
||||
for (int i4 = 0; i4 < n4; i4++)
|
||||
scf[i4] = 0.25f * (scf[4*i4+0] + scf[4*i4+1] +
|
||||
scf[4*i4+2] + scf[4*i4+3]);
|
||||
|
||||
for (int i2 = n4; i2 < n4+n2; i2++)
|
||||
scf[i2] = 0.5f * (scf[2*(n4+i2)] + scf[2*(n4+i2)+1]);
|
||||
|
||||
memmove(scf + n4 + n2, scf + 4*n4 + 2*n2, (nb - n4 - n2) * sizeof(float));
|
||||
|
||||
/* --- Spectral shaping --- */
|
||||
|
||||
const int *lim = lc3_band_lim[dt][sr];
|
||||
|
||||
for (int i = 0, ib = 0; ib < nb; ib++) {
|
||||
float g_sns = lc3_exp2f(-scf[ib]);
|
||||
|
||||
for ( ; i < lim[ib+1]; i++)
|
||||
y[i] = x[i] * g_sns;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Interface
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* SNS analysis
|
||||
*/
|
||||
void lc3_sns_analyze(
|
||||
enum lc3_dt dt, enum lc3_srate sr, int nbytes,
|
||||
const float *eb, bool att, struct lc3_sns_data *data,
|
||||
const float *x, float *y)
|
||||
{
|
||||
/* Processing steps :
|
||||
* - Determine 16 scale factors from bands energy estimation
|
||||
* - Get codebooks indexes that match thoses scale factors
|
||||
* - Quantize the residual with the selected codebook
|
||||
* - The pulse configuration `c[]` is enumerated
|
||||
* - Finally shape the spectrum coefficients accordingly */
|
||||
|
||||
float scf[16], cn[4][16];
|
||||
int c[4][16];
|
||||
|
||||
compute_scale_factors(dt, sr, nbytes, eb, att, scf);
|
||||
|
||||
resolve_codebooks(scf, &data->lfcb, &data->hfcb);
|
||||
|
||||
quantize(scf, data->lfcb, data->hfcb,
|
||||
c, cn, &data->shape, &data->gain);
|
||||
|
||||
unquantize(data->lfcb, data->hfcb,
|
||||
cn[data->shape], data->shape, data->gain, scf);
|
||||
|
||||
enumerate(data->shape, c[data->shape],
|
||||
&data->idx_a, &data->ls_a, &data->idx_b, &data->ls_b);
|
||||
|
||||
spectral_shaping(dt, sr, scf, false, x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* SNS synthesis
|
||||
*/
|
||||
void lc3_sns_synthesize(
|
||||
enum lc3_dt dt, enum lc3_srate sr,
|
||||
const lc3_sns_data_t *data, const float *x, float *y)
|
||||
{
|
||||
float scf[16], cn[16];
|
||||
int c[16];
|
||||
|
||||
deenumerate(data->shape,
|
||||
data->idx_a, data->ls_a, data->idx_b, data->ls_b, c);
|
||||
|
||||
normalize(c, cn);
|
||||
|
||||
unquantize(data->lfcb, data->hfcb, cn, data->shape, data->gain, scf);
|
||||
|
||||
spectral_shaping(dt, sr, scf, true, x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return number of bits coding the bitstream data
|
||||
*/
|
||||
int lc3_sns_get_nbits(void)
|
||||
{
|
||||
return 38;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put bitstream data
|
||||
*/
|
||||
void lc3_sns_put_data(lc3_bits_t *bits, const struct lc3_sns_data *data)
|
||||
{
|
||||
/* --- Codebooks --- */
|
||||
|
||||
lc3_put_bits(bits, data->lfcb, 5);
|
||||
lc3_put_bits(bits, data->hfcb, 5);
|
||||
|
||||
/* --- Shape, gain and vectors --- *
|
||||
* Write MSB bit of shape index, next LSB bits of shape and gain,
|
||||
* and MVPQ vectors indexes are muxed */
|
||||
|
||||
int shape_msb = data->shape >> 1;
|
||||
lc3_put_bit(bits, shape_msb);
|
||||
|
||||
if (shape_msb == 0) {
|
||||
const int size_a = 2390004;
|
||||
int submode = data->shape & 1;
|
||||
|
||||
int mux_high = submode == 0 ?
|
||||
2 * (data->idx_b + 1) + data->ls_b : data->gain & 1;
|
||||
int mux_code = mux_high * size_a + data->idx_a;
|
||||
|
||||
lc3_put_bits(bits, data->gain >> submode, 1);
|
||||
lc3_put_bits(bits, data->ls_a, 1);
|
||||
lc3_put_bits(bits, mux_code, 25);
|
||||
|
||||
} else {
|
||||
const int size_a = 15158272;
|
||||
int submode = data->shape & 1;
|
||||
|
||||
int mux_code = submode == 0 ?
|
||||
data->idx_a : size_a + 2 * data->idx_a + (data->gain & 1);
|
||||
|
||||
lc3_put_bits(bits, data->gain >> submode, 2);
|
||||
lc3_put_bits(bits, data->ls_a, 1);
|
||||
lc3_put_bits(bits, mux_code, 24);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get bitstream data
|
||||
*/
|
||||
int lc3_sns_get_data(lc3_bits_t *bits, struct lc3_sns_data *data)
|
||||
{
|
||||
/* --- Codebooks --- */
|
||||
|
||||
*data = (struct lc3_sns_data){
|
||||
.lfcb = lc3_get_bits(bits, 5),
|
||||
.hfcb = lc3_get_bits(bits, 5)
|
||||
};
|
||||
|
||||
/* --- Shape, gain and vectors --- */
|
||||
|
||||
int shape_msb = lc3_get_bit(bits);
|
||||
data->gain = lc3_get_bits(bits, 1 + shape_msb);
|
||||
data->ls_a = lc3_get_bit(bits);
|
||||
|
||||
int mux_code = lc3_get_bits(bits, 25 - shape_msb);
|
||||
|
||||
if (shape_msb == 0) {
|
||||
const int size_a = 2390004;
|
||||
|
||||
if (mux_code >= size_a * 14)
|
||||
return -1;
|
||||
|
||||
data->idx_a = mux_code % size_a;
|
||||
mux_code = mux_code / size_a;
|
||||
|
||||
data->shape = (mux_code < 2);
|
||||
|
||||
if (data->shape == 0) {
|
||||
data->idx_b = (mux_code - 2) / 2;
|
||||
data->ls_b = (mux_code - 2) % 2;
|
||||
} else {
|
||||
data->gain = (data->gain << 1) + (mux_code % 2);
|
||||
}
|
||||
|
||||
} else {
|
||||
const int size_a = 15158272;
|
||||
|
||||
if (mux_code >= size_a + 1549824)
|
||||
return -1;
|
||||
|
||||
data->shape = 2 + (mux_code >= size_a);
|
||||
if (data->shape == 2) {
|
||||
data->idx_a = mux_code;
|
||||
} else {
|
||||
mux_code -= size_a;
|
||||
data->idx_a = mux_code / 2;
|
||||
data->gain = (data->gain << 1) + (mux_code % 2);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __LC3_SNS_H
|
||||
#define __LC3_SNS_H
|
||||
|
||||
#include "common.h"
|
||||
#include "bits.h"
|
||||
|
||||
|
||||
/**
|
||||
* Bitstream data
|
||||
*/
|
||||
|
||||
typedef struct lc3_sns_data {
|
||||
int lfcb, hfcb;
|
||||
int shape, gain;
|
||||
int idx_a, idx_b;
|
||||
bool ls_a, ls_b;
|
||||
} lc3_sns_data_t;
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Encoding
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* SNS analysis
|
||||
* dt, sr Duration and samplerate of the frame
|
||||
* nbytes Size in bytes of the frame
|
||||
* eb Energy estimation per bands, and count of bands
|
||||
* att 1: Attack detected 0: Otherwise
|
||||
* data Return bitstream data
|
||||
* x Spectral coefficients
|
||||
* y Return shapped coefficients
|
||||
*
|
||||
* `x` and `y` can be the same buffer
|
||||
*/
|
||||
void lc3_sns_analyze(
|
||||
enum lc3_dt dt, enum lc3_srate sr, int nbytes,
|
||||
const float *eb, bool att, lc3_sns_data_t *data,
|
||||
const float *x, float *y);
|
||||
|
||||
/**
|
||||
* Return number of bits coding the bitstream data
|
||||
* return Bit consumption
|
||||
*/
|
||||
int lc3_sns_get_nbits(void);
|
||||
|
||||
/**
|
||||
* Put bitstream data
|
||||
* bits Bitstream context
|
||||
* data Bitstream data
|
||||
*/
|
||||
void lc3_sns_put_data(lc3_bits_t *bits, const lc3_sns_data_t *data);
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Decoding
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get bitstream data
|
||||
* bits Bitstream context
|
||||
* data Return SNS data
|
||||
* return 0: Ok -1: Invalid SNS data
|
||||
*/
|
||||
int lc3_sns_get_data(lc3_bits_t *bits, lc3_sns_data_t *data);
|
||||
|
||||
/**
|
||||
* SNS synthesis
|
||||
* dt, sr Duration and samplerate of the frame
|
||||
* data Bitstream data
|
||||
* x Spectral coefficients
|
||||
* y Return shapped coefficients
|
||||
*
|
||||
* `x` and `y` can be the same buffer
|
||||
*/
|
||||
void lc3_sns_synthesize(enum lc3_dt dt, enum lc3_srate sr,
|
||||
const lc3_sns_data_t *data, const float *x, float *y);
|
||||
|
||||
|
||||
#endif /* __LC3_SNS_H */
|
||||
@@ -0,0 +1,959 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#include "spec.h"
|
||||
#include "bits.h"
|
||||
#include "tables.h"
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Global Gain / Quantization
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Resolve quantized gain index offset
|
||||
* sr, nbytes Samplerate and size of the frame
|
||||
* return Gain index offset
|
||||
*/
|
||||
static int resolve_gain_offset(enum lc3_srate sr, int nbytes)
|
||||
{
|
||||
int sr_ind = lc3_hr(sr) ? 4 + (sr - LC3_SRATE_48K_HR) : sr;
|
||||
|
||||
int g_off = (nbytes * 8) / (10 * (1 + sr_ind));
|
||||
return LC3_MIN(sr >= LC3_SRATE_96K_HR ? 181 : 255,
|
||||
105 + 5*(1 + sr_ind) + LC3_MIN(g_off, 115));
|
||||
}
|
||||
|
||||
/**
|
||||
* Unquantize gain
|
||||
* g_int Quantization gain value
|
||||
* return Unquantized gain value
|
||||
*/
|
||||
static float unquantize_gain(int g_int)
|
||||
{
|
||||
/* Unquantization gain table :
|
||||
* G[i] = 10 ^ (i / 28) , i = [0..27] */
|
||||
|
||||
static const float iq_table[] = {
|
||||
1.00000000e+00, 1.08571112e+00, 1.17876863e+00, 1.27980221e+00,
|
||||
1.38949549e+00, 1.50859071e+00, 1.63789371e+00, 1.77827941e+00,
|
||||
1.93069773e+00, 2.09617999e+00, 2.27584593e+00, 2.47091123e+00,
|
||||
2.68269580e+00, 2.91263265e+00, 3.16227766e+00, 3.43332002e+00,
|
||||
3.72759372e+00, 4.04708995e+00, 4.39397056e+00, 4.77058270e+00,
|
||||
5.17947468e+00, 5.62341325e+00, 6.10540230e+00, 6.62870316e+00,
|
||||
7.19685673e+00, 7.81370738e+00, 8.48342898e+00, 9.21055318e+00
|
||||
};
|
||||
|
||||
float g = 1.f;
|
||||
|
||||
for ( ; g_int < 0; g_int += 28, g *= 0.1f);
|
||||
for ( ; g_int >= 28; g_int -= 28, g *= 10.f);
|
||||
|
||||
return g * iq_table[g_int];
|
||||
}
|
||||
|
||||
/**
|
||||
* Global Gain Estimation
|
||||
* dt, sr Duration and samplerate of the frame
|
||||
* x Spectral coefficients
|
||||
* nbytes Size of the frame
|
||||
* nbits_budget Number of bits available coding the spectrum
|
||||
* nbits_off Offset on the available bits, temporarily smoothed
|
||||
* g_off Gain index offset
|
||||
* reset_off Return True when the nbits_off must be reset
|
||||
* g_min Return lower bound of quantized gain value
|
||||
* return The quantized gain value
|
||||
*/
|
||||
LC3_HOT static int estimate_gain(
|
||||
enum lc3_dt dt, enum lc3_srate sr, const float *x,
|
||||
int nbytes, int nbits_budget, float nbits_off, int g_off,
|
||||
bool *reset_off, int *g_min)
|
||||
{
|
||||
int n4 = lc3_ne(dt, sr) / 4;
|
||||
union { float f; int32_t q16; } e[LC3_MAX_NE / 4];
|
||||
|
||||
/* --- Signal adaptative noise floor --- */
|
||||
|
||||
int reg_bits = 0;
|
||||
float low_bits = 0;
|
||||
|
||||
if (lc3_hr(sr)) {
|
||||
int reg_c = (const int [LC3_NUM_DT][LC3_NUM_SRATE - LC3_SRATE_48K_HR]){
|
||||
[LC3_DT_2M5] = { -6, -6 },
|
||||
[LC3_DT_5M ] = { 0, 0 },
|
||||
[LC3_DT_10M] = { 2, 5 }
|
||||
}[dt][sr - LC3_SRATE_48K_HR];
|
||||
|
||||
reg_bits = (8*nbytes * 4) / (125 * (1 + dt));
|
||||
reg_bits = LC3_CLIP(reg_bits + reg_c, 6, 23);
|
||||
|
||||
float m0 = 1e-5f, m1 = 1e-5f, k = 0;
|
||||
|
||||
for (int i = 0; i < n4; i++) {
|
||||
m0 += fabsf(x[4*i + 0]), m1 += fabsf(x[4*i + 0]) * k++;
|
||||
m0 += fabsf(x[4*i + 1]), m1 += fabsf(x[4*i + 1]) * k++;
|
||||
m0 += fabsf(x[4*i + 2]), m1 += fabsf(x[4*i + 2]) * k++;
|
||||
m0 += fabsf(x[4*i + 3]), m1 += fabsf(x[4*i + 3]) * k++;
|
||||
}
|
||||
|
||||
int m = roundf((1.6f * m0) / ((1 + dt) * m1));
|
||||
low_bits = 8 - LC3_MIN(m, 8);
|
||||
}
|
||||
|
||||
/* --- Energy (dB) by 4 MDCT blocks --- */
|
||||
|
||||
float x2_max = 0;
|
||||
|
||||
for (int i = 0; i < n4; i++) {
|
||||
float x0 = x[4*i + 0] * x[4*i + 0];
|
||||
float x1 = x[4*i + 1] * x[4*i + 1];
|
||||
float x2 = x[4*i + 2] * x[4*i + 2];
|
||||
float x3 = x[4*i + 3] * x[4*i + 3];
|
||||
|
||||
x2_max = fmaxf(x2_max, x0);
|
||||
x2_max = fmaxf(x2_max, x1);
|
||||
x2_max = fmaxf(x2_max, x2);
|
||||
x2_max = fmaxf(x2_max, x3);
|
||||
|
||||
e[i].f = x0 + x1 + x2 + x3;
|
||||
}
|
||||
|
||||
float x_max = sqrtf(x2_max);
|
||||
float nf = lc3_hr(sr) ?
|
||||
lc3_ldexpf(x_max, -reg_bits) * lc3_exp2f(-low_bits) : 0;
|
||||
|
||||
for (int i = 0; i < n4; i++)
|
||||
e[i].q16 = lc3_db_q16(fmaxf(e[i].f + nf, 1e-10f));
|
||||
|
||||
/* --- Determine gain index --- */
|
||||
|
||||
int nbits = nbits_budget + nbits_off + 0.5f;
|
||||
int g_int = 255 - g_off;
|
||||
|
||||
const int k_20_28 = 20.f/28 * 0x1p16f + 0.5f;
|
||||
const int k_2u7 = 2.7f * 0x1p16f + 0.5f;
|
||||
const int k_1u4 = 1.4f * 0x1p16f + 0.5f;
|
||||
|
||||
for (int i = 128, j, j0 = n4-1, j1 ; i > 0; i >>= 1) {
|
||||
int gn = (g_int - i) * k_20_28;
|
||||
int v = 0;
|
||||
|
||||
for (j = j0; j >= 0 && e[j].q16 < gn; j--);
|
||||
|
||||
for (j1 = j; j >= 0; j--) {
|
||||
int e_diff = e[j].q16 - gn;
|
||||
|
||||
v += e_diff < 0 ? k_2u7 :
|
||||
e_diff < 43 << 16 ? e_diff + ( 7 << 16)
|
||||
: 2*e_diff - (36 << 16);
|
||||
}
|
||||
|
||||
if (v > nbits * k_1u4)
|
||||
j0 = j1;
|
||||
else
|
||||
g_int = g_int - i;
|
||||
}
|
||||
|
||||
/* --- Limit gain index --- */
|
||||
|
||||
float x_lim = lc3_hr(sr) ? 0x7fffp8f : 0x7fffp0f;
|
||||
|
||||
*g_min = 255 - g_off;
|
||||
for (int i = 128 ; i > 0; i >>= 1)
|
||||
if (x_lim * unquantize_gain(*g_min - i) > x_max)
|
||||
*g_min -= i;
|
||||
|
||||
*reset_off = g_int < *g_min || x_max == 0;
|
||||
if (*reset_off)
|
||||
g_int = *g_min;
|
||||
|
||||
return g_int;
|
||||
}
|
||||
|
||||
/**
|
||||
* Global Gain Adjustment
|
||||
* dt, sr Duration and samplerate of the frame
|
||||
* g_idx The estimated quantized gain index
|
||||
* nbits Computed number of bits coding the spectrum
|
||||
* nbits_budget Number of bits available for coding the spectrum
|
||||
* g_idx_min Minimum gain index value
|
||||
* return Gain adjust value (-1 to 2)
|
||||
*/
|
||||
LC3_HOT static int adjust_gain(
|
||||
enum lc3_dt dt, enum lc3_srate sr,
|
||||
int g_idx, int nbits, int nbits_budget, int g_idx_min)
|
||||
{
|
||||
/* --- Compute delta threshold --- */
|
||||
|
||||
const int *t = (const int [LC3_NUM_SRATE][3]){
|
||||
{ 80, 500, 850 }, { 230, 1025, 1700 }, { 380, 1550, 2550 },
|
||||
{ 530, 2075, 3400 }, { 680, 2600, 4250 },
|
||||
{ 680, 2600, 4250 }, { 830, 3125, 5100 }
|
||||
}[sr];
|
||||
|
||||
int delta, den = 48;
|
||||
|
||||
if (nbits < t[0]) {
|
||||
delta = 3*(nbits + 48);
|
||||
|
||||
} else if (nbits < t[1]) {
|
||||
int n0 = 3*(t[0] + 48), range = t[1] - t[0];
|
||||
delta = n0 * range + (nbits - t[0]) * (t[1] - n0);
|
||||
den *= range;
|
||||
|
||||
} else {
|
||||
delta = LC3_MIN(nbits, t[2]);
|
||||
}
|
||||
|
||||
delta = (delta + den/2) / den;
|
||||
|
||||
/* --- Adjust gain --- */
|
||||
|
||||
if (lc3_hr(sr) && nbits > nbits_budget) {
|
||||
int factor = 1 + (dt <= LC3_DT_5M) +
|
||||
(dt <= LC3_DT_2M5) * (1 + (nbits >= 520));
|
||||
|
||||
int g_incr = factor + (factor * (nbits - nbits_budget)) / delta;
|
||||
return LC3_MIN(g_idx + g_incr, 255) - g_idx;
|
||||
}
|
||||
|
||||
if (!lc3_hr(sr) && nbits < nbits_budget - (delta + 2))
|
||||
return -(g_idx > g_idx_min);
|
||||
|
||||
if (!lc3_hr(sr) && nbits > nbits_budget)
|
||||
return (g_idx < 255) + (g_idx < 254 && nbits >= nbits_budget + delta);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Spectrum quantization
|
||||
* dt, sr Duration and samplerate of the frame
|
||||
* g_int Quantization gain value
|
||||
* x Spectral coefficients, scaled as output
|
||||
* n Return count of significants
|
||||
*/
|
||||
LC3_HOT static void quantize(
|
||||
enum lc3_dt dt, enum lc3_srate sr, int g_int, float *x, int *n)
|
||||
{
|
||||
float g_inv = unquantize_gain(-g_int);
|
||||
int ne = lc3_ne(dt, sr);
|
||||
|
||||
*n = ne;
|
||||
|
||||
for (int i = 0; i < ne; i += 2) {
|
||||
float xq_min = lc3_hr(sr) ? 0.5f : 10.f/16;
|
||||
|
||||
x[i+0] *= g_inv;
|
||||
x[i+1] *= g_inv;
|
||||
|
||||
*n = fabsf(x[i+0]) >= xq_min ||
|
||||
fabsf(x[i+1]) >= xq_min ? ne : *n - 2;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Spectrum quantization inverse
|
||||
* dt, sr Duration and samplerate of the frame
|
||||
* g_int Quantization gain value
|
||||
* x, nq Spectral quantized, and count of significants
|
||||
* return Unquantized gain value
|
||||
*/
|
||||
LC3_HOT static float unquantize(
|
||||
enum lc3_dt dt, enum lc3_srate sr,
|
||||
int g_int, float *x, int nq)
|
||||
{
|
||||
float g = unquantize_gain(g_int);
|
||||
int i, ne = lc3_ne(dt, sr);
|
||||
|
||||
for (i = 0; i < nq; i++)
|
||||
x[i] = x[i] * g;
|
||||
|
||||
for ( ; i < ne; i++)
|
||||
x[i] = 0;
|
||||
|
||||
return g;
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Spectrum coding
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Resolve High-bitrate and LSB modes according size of the frame
|
||||
* sr, nbytes Samplerate and size of the frame
|
||||
* p_lsb_mode True when LSB mode allowed, when not NULL
|
||||
* return True when High-Rate mode enabled
|
||||
*/
|
||||
static bool resolve_modes(enum lc3_srate sr, int nbytes, bool *p_lsb_mode)
|
||||
{
|
||||
int sr_ind = lc3_hr(sr) ? 4 + (sr - LC3_SRATE_48K_HR) : sr;
|
||||
|
||||
if (p_lsb_mode)
|
||||
*p_lsb_mode = (nbytes >= 20 * (3 + sr_ind)) && (sr < LC3_SRATE_96K_HR);
|
||||
|
||||
return (nbytes > 20 * (1 + sr_ind)) && (sr < LC3_SRATE_96K_HR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bit consumption
|
||||
* dt, sr, nbytes Duration, samplerate and size of the frame
|
||||
* x Spectral quantized coefficients
|
||||
* n Count of significant coefficients, updated on truncation
|
||||
* nbits_budget Truncate to stay in budget, when not zero
|
||||
* p_lsb_mode Return True when LSB's are not AC coded, or NULL
|
||||
* return The number of bits coding the spectrum
|
||||
*/
|
||||
LC3_HOT static int compute_nbits(
|
||||
enum lc3_dt dt, enum lc3_srate sr, int nbytes,
|
||||
const float *x, int *n, int nbits_budget, bool *p_lsb_mode)
|
||||
{
|
||||
bool lsb_mode, high_rate = resolve_modes(sr, nbytes, &lsb_mode);
|
||||
int ne = lc3_ne(dt, sr);
|
||||
|
||||
/* --- Loop on quantized coefficients --- */
|
||||
|
||||
int nbits = 0, nbits_lsb = 0;
|
||||
uint8_t state = 0;
|
||||
|
||||
int nbits_end = 0;
|
||||
int n_end = 0;
|
||||
|
||||
nbits_budget = nbits_budget ? nbits_budget * 2048 : INT_MAX;
|
||||
|
||||
for (int i = 0, h = 0; h < 2; h++) {
|
||||
const uint8_t (*lut_coeff)[4] = lc3_spectrum_lookup[high_rate][h];
|
||||
|
||||
for ( ; i < LC3_MIN(*n, (ne + 2) >> (1 - h))
|
||||
&& nbits <= nbits_budget; i += 2) {
|
||||
|
||||
float xq_off = lc3_hr(sr) ? 0.5f : 6.f/16;
|
||||
uint32_t a = fabsf(x[i+0]) + xq_off;
|
||||
uint32_t b = fabsf(x[i+1]) + xq_off;
|
||||
|
||||
const uint8_t *lut = lut_coeff[state];
|
||||
|
||||
/* --- Sign values --- */
|
||||
|
||||
int s = (a != 0) + (b != 0);
|
||||
nbits += s * 2048;
|
||||
|
||||
/* --- LSB values Reduce to 2*2 bits MSB values ---
|
||||
* Reduce to 2x2 bits MSB values. The LSB's pair are arithmetic
|
||||
* coded with an escape code followed by 1 bit for each values.
|
||||
* The LSB mode does not arthmetic code the first LSB,
|
||||
* add the sign of the LSB when one of pair was at value 1 */
|
||||
|
||||
uint32_t m = (a | b) >> 2;
|
||||
unsigned k = 0;
|
||||
|
||||
if (m) {
|
||||
|
||||
if (lsb_mode) {
|
||||
nbits += lc3_spectrum_bits[lut[k++]][16] - 2*2048;
|
||||
nbits_lsb += 2 + (a == 1) + (b == 1);
|
||||
}
|
||||
|
||||
for (m >>= lsb_mode; m; m >>= 1, k++)
|
||||
nbits += lc3_spectrum_bits[lut[LC3_MIN(k, 3)]][16];
|
||||
|
||||
nbits += k * 2*2048;
|
||||
a >>= k;
|
||||
b >>= k;
|
||||
|
||||
k = LC3_MIN(k, 3);
|
||||
}
|
||||
|
||||
/* --- MSB values --- */
|
||||
|
||||
nbits += lc3_spectrum_bits[lut[k]][a + 4*b];
|
||||
|
||||
/* --- Update state --- */
|
||||
|
||||
if (s && nbits <= nbits_budget) {
|
||||
n_end = i + 2;
|
||||
nbits_end = nbits;
|
||||
}
|
||||
|
||||
state = (state << 4) + (k > 1 ? 12 + k : 1 + (a + b) * (k + 1));
|
||||
}
|
||||
}
|
||||
|
||||
/* --- Return --- */
|
||||
|
||||
*n = n_end;
|
||||
|
||||
if (p_lsb_mode)
|
||||
*p_lsb_mode = lsb_mode &&
|
||||
nbits_end + nbits_lsb * 2048 > nbits_budget;
|
||||
|
||||
if (nbits_budget >= INT_MAX)
|
||||
nbits_end += nbits_lsb * 2048;
|
||||
|
||||
return (nbits_end + 2047) / 2048;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put quantized spectrum
|
||||
* bits Bitstream context
|
||||
* dt, sr, nbytes Duration, samplerate and size of the frame
|
||||
* x Spectral quantized coefficients
|
||||
* nq, lsb_mode Count of significants, and LSB discard indication
|
||||
*/
|
||||
LC3_HOT static void put_quantized(lc3_bits_t *bits,
|
||||
enum lc3_dt dt, enum lc3_srate sr, int nbytes,
|
||||
const float *x, int nq, bool lsb_mode)
|
||||
{
|
||||
bool high_rate = resolve_modes(sr, nbytes, NULL);
|
||||
int ne = lc3_ne(dt, sr);
|
||||
|
||||
/* --- Loop on quantized coefficients --- */
|
||||
|
||||
uint8_t state = 0;
|
||||
|
||||
for (int i = 0, h = 0; h < 2; h++) {
|
||||
const uint8_t (*lut_coeff)[4] = lc3_spectrum_lookup[high_rate][h];
|
||||
|
||||
for ( ; i < LC3_MIN(nq, (ne + 2) >> (1 - h)); i += 2) {
|
||||
|
||||
float xq_off = lc3_hr(sr) ? 0.5f : 6.f/16;
|
||||
uint32_t a = fabsf(x[i+0]) + xq_off;
|
||||
uint32_t b = fabsf(x[i+1]) + xq_off;
|
||||
|
||||
const uint8_t *lut = lut_coeff[state];
|
||||
|
||||
/* --- LSB values Reduce to 2*2 bits MSB values ---
|
||||
* Reduce to 2x2 bits MSB values. The LSB's pair are arithmetic
|
||||
* coded with an escape code and 1 bits for each values.
|
||||
* The LSB mode discard the first LSB (at this step) */
|
||||
|
||||
uint32_t m = (a | b) >> 2;
|
||||
unsigned k = 0, shr = 0;
|
||||
|
||||
if (m) {
|
||||
|
||||
if (lsb_mode)
|
||||
lc3_put_symbol(bits,
|
||||
lc3_spectrum_models + lut[k++], 16);
|
||||
|
||||
for (m >>= lsb_mode; m; m >>= 1, k++) {
|
||||
lc3_put_bit(bits, (a >> k) & 1);
|
||||
lc3_put_bit(bits, (b >> k) & 1);
|
||||
lc3_put_symbol(bits,
|
||||
lc3_spectrum_models + lut[LC3_MIN(k, 3)], 16);
|
||||
}
|
||||
|
||||
a >>= lsb_mode;
|
||||
b >>= lsb_mode;
|
||||
|
||||
shr = k - lsb_mode;
|
||||
k = LC3_MIN(k, 3);
|
||||
}
|
||||
|
||||
/* --- Sign values --- */
|
||||
|
||||
if (a) lc3_put_bit(bits, x[i+0] < 0);
|
||||
if (b) lc3_put_bit(bits, x[i+1] < 0);
|
||||
|
||||
/* --- MSB values --- */
|
||||
|
||||
a >>= shr;
|
||||
b >>= shr;
|
||||
|
||||
lc3_put_symbol(bits, lc3_spectrum_models + lut[k], a + 4*b);
|
||||
|
||||
/* --- Update state --- */
|
||||
|
||||
state = (state << 4) + (k > 1 ? 12 + k : 1 + (a + b) * (k + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get quantized spectrum
|
||||
* bits Bitstream context
|
||||
* dt, sr, nbytes Duration, samplerate and size of the frame
|
||||
* nq, lsb_mode Count of significants, and LSB discard indication
|
||||
* x Return `nq` spectral quantized coefficients
|
||||
* nf_seed Return the noise factor seed associated
|
||||
* return 0: Ok -1: Invalid bitstream data
|
||||
*/
|
||||
LC3_HOT static int get_quantized(lc3_bits_t *bits,
|
||||
enum lc3_dt dt, enum lc3_srate sr, int nbytes,
|
||||
int nq, bool lsb_mode, float *x, uint16_t *nf_seed)
|
||||
{
|
||||
bool high_rate = resolve_modes(sr, nbytes, NULL);
|
||||
int ne = lc3_ne(dt, sr);
|
||||
|
||||
*nf_seed = 0;
|
||||
|
||||
/* --- Loop on quantized coefficients --- */
|
||||
|
||||
uint8_t state = 0;
|
||||
|
||||
for (int i = 0, h = 0; h < 2; h++) {
|
||||
const uint8_t (*lut_coeff)[4] = lc3_spectrum_lookup[high_rate][h];
|
||||
|
||||
for ( ; i < LC3_MIN(nq, (ne + 2) >> (1 - h)); i += 2) {
|
||||
|
||||
const uint8_t *lut = lut_coeff[state];
|
||||
int max_shl = lc3_hr(sr) ? 22 : 14;
|
||||
|
||||
/* --- LSB values ---
|
||||
* Until the symbol read indicates the escape value 16,
|
||||
* read an LSB bit for each values.
|
||||
* The LSB mode discard the first LSB (at this step) */
|
||||
|
||||
int u = 0, v = 0;
|
||||
int k = 0, shl = 0;
|
||||
|
||||
unsigned s = lc3_get_symbol(bits, lc3_spectrum_models + lut[k]);
|
||||
|
||||
if (lsb_mode && s >= 16) {
|
||||
s = lc3_get_symbol(bits, lc3_spectrum_models + lut[++k]);
|
||||
shl++;
|
||||
}
|
||||
|
||||
for ( ; s >= 16 && shl < max_shl; shl++) {
|
||||
u |= lc3_get_bit(bits) << shl;
|
||||
v |= lc3_get_bit(bits) << shl;
|
||||
|
||||
k += (k < 3);
|
||||
s = lc3_get_symbol(bits, lc3_spectrum_models + lut[k]);
|
||||
}
|
||||
|
||||
if (s >= 16)
|
||||
return -1;
|
||||
|
||||
/* --- MSB & sign values --- */
|
||||
|
||||
int a = s % 4;
|
||||
int b = s / 4;
|
||||
|
||||
u |= a << shl;
|
||||
v |= b << shl;
|
||||
|
||||
x[i+0] = u && lc3_get_bit(bits) ? -u : u;
|
||||
x[i+1] = v && lc3_get_bit(bits) ? -v : v;
|
||||
|
||||
*nf_seed = (*nf_seed + (u & 0x7fff) * (i )
|
||||
+ (v & 0x7fff) * (i+1)) & 0xffff;
|
||||
|
||||
/* --- Update state --- */
|
||||
|
||||
state = (state << 4) + (k > 1 ? 12 + k : 1 + (a + b) * (k + 1));
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put residual bits of quantization
|
||||
* bits Bitstream context
|
||||
* nbits Maximum number of bits to output
|
||||
* hrmode High-Resolution mode
|
||||
* x, n Spectral quantized, and count of significants
|
||||
*/
|
||||
LC3_HOT static void put_residual(lc3_bits_t *bits,
|
||||
int nbits, bool hrmode, float *x, int n)
|
||||
{
|
||||
float xq_lim = hrmode ? 0.5f : 10.f/16;
|
||||
float xq_off = xq_lim / 2;
|
||||
|
||||
for (int iter = 0; iter < (hrmode ? 20 : 1) && nbits > 0; iter++) {
|
||||
for (int i = 0; i < n && nbits > 0; i++) {
|
||||
|
||||
float xr = fabsf(x[i]);
|
||||
if (xr < xq_lim)
|
||||
continue;
|
||||
|
||||
bool b = (xr - truncf(xr) < xq_lim) ^ (x[i] < 0);
|
||||
lc3_put_bit(bits, b);
|
||||
nbits--;
|
||||
|
||||
x[i] += b ? -xq_off : xq_off;
|
||||
}
|
||||
|
||||
xq_off *= xq_lim;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get residual bits of quantization
|
||||
* bits Bitstream context
|
||||
* nbits Maximum number of bits to output
|
||||
* hrmode High-Resolution mode
|
||||
* x, nq Spectral quantized, and count of significants
|
||||
*/
|
||||
LC3_HOT static void get_residual(lc3_bits_t *bits,
|
||||
int nbits, bool hrmode, float *x, int n)
|
||||
{
|
||||
float xq_off_1 = hrmode ? 0.25f : 5.f/16;
|
||||
float xq_off_2 = hrmode ? 0.25f : 3.f/16;
|
||||
|
||||
for (int iter = 0; iter < (hrmode ? 20 : 1) && nbits > 0; iter++) {
|
||||
for (int i = 0; i < n && nbits > 0; i++) {
|
||||
|
||||
if (x[i] == 0)
|
||||
continue;
|
||||
|
||||
if (lc3_get_bit(bits) == 0)
|
||||
x[i] -= x[i] < 0 ? xq_off_1 : xq_off_2;
|
||||
else
|
||||
x[i] += x[i] > 0 ? xq_off_1 : xq_off_2;
|
||||
|
||||
nbits--;
|
||||
}
|
||||
|
||||
xq_off_1 *= 0.5f;
|
||||
xq_off_2 *= 0.5f;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Put LSB values of quantized spectrum values
|
||||
* bits Bitstream context
|
||||
* nbits Maximum number of bits to output
|
||||
* hrmode High-Resolution mode
|
||||
* x, n Spectral quantized, and count of significants
|
||||
*/
|
||||
LC3_HOT static void put_lsb(lc3_bits_t *bits,
|
||||
int nbits, bool hrmode, const float *x, int n)
|
||||
{
|
||||
for (int i = 0; i < n && nbits > 0; i += 2) {
|
||||
|
||||
float xq_off = hrmode ? 0.5f : 6.f/16;
|
||||
uint32_t a = fabsf(x[i+0]) + xq_off;
|
||||
uint32_t b = fabsf(x[i+1]) + xq_off;
|
||||
|
||||
if ((a | b) >> 2 == 0)
|
||||
continue;
|
||||
|
||||
if (nbits-- > 0)
|
||||
lc3_put_bit(bits, a & 1);
|
||||
|
||||
if (a == 1 && nbits-- > 0)
|
||||
lc3_put_bit(bits, x[i+0] < 0);
|
||||
|
||||
if (nbits-- > 0)
|
||||
lc3_put_bit(bits, b & 1);
|
||||
|
||||
if (b == 1 && nbits-- > 0)
|
||||
lc3_put_bit(bits, x[i+1] < 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get LSB values of quantized spectrum values
|
||||
* bits Bitstream context
|
||||
* nbits Maximum number of bits to output
|
||||
* x, nq Spectral quantized, and count of significants
|
||||
* nf_seed Update the noise factor seed according
|
||||
*/
|
||||
LC3_HOT static void get_lsb(lc3_bits_t *bits,
|
||||
int nbits, float *x, int nq, uint16_t *nf_seed)
|
||||
{
|
||||
for (int i = 0; i < nq && nbits > 0; i += 2) {
|
||||
|
||||
float a = fabsf(x[i]), b = fabsf(x[i+1]);
|
||||
|
||||
if (fmaxf(a, b) < 4)
|
||||
continue;
|
||||
|
||||
if (nbits-- > 0 && lc3_get_bit(bits)) {
|
||||
if (a) {
|
||||
x[i] += x[i] < 0 ? -1 : 1;
|
||||
*nf_seed = (*nf_seed + i) & 0xffff;
|
||||
} else if (nbits-- > 0) {
|
||||
x[i] = lc3_get_bit(bits) ? -1 : 1;
|
||||
*nf_seed = (*nf_seed + i) & 0xffff;
|
||||
}
|
||||
}
|
||||
|
||||
if (nbits-- > 0 && lc3_get_bit(bits)) {
|
||||
if (b) {
|
||||
x[i+1] += x[i+1] < 0 ? -1 : 1;
|
||||
*nf_seed = (*nf_seed + i+1) & 0xffff;
|
||||
} else if (nbits-- > 0) {
|
||||
x[i+1] = lc3_get_bit(bits) ? -1 : 1;
|
||||
*nf_seed = (*nf_seed + i+1) & 0xffff;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Noise coding
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Estimate noise level
|
||||
* dt, bw Duration and bandwidth of the frame
|
||||
* hrmode High-Resolution mode
|
||||
* x, n Spectral quantized, and count of significants
|
||||
* return Noise factor (0 to 7)
|
||||
*/
|
||||
LC3_HOT static int estimate_noise(
|
||||
enum lc3_dt dt, enum lc3_bandwidth bw, bool hrmode, const float *x, int n)
|
||||
{
|
||||
int bw_stop = lc3_ne(dt, (enum lc3_srate)LC3_MIN(bw, LC3_BANDWIDTH_FB));
|
||||
int w = 1 + (dt >= LC3_DT_7M5) + (dt>= LC3_DT_10M);
|
||||
|
||||
float xq_lim = hrmode ? 0.5f : 10.f/16;
|
||||
float sum = 0;
|
||||
int i, ns = 0, z = 0;
|
||||
|
||||
for (i = 6 * (1 + dt) - w; i < LC3_MIN(n, bw_stop); i++) {
|
||||
z = fabsf(x[i]) < xq_lim ? z + 1 : 0;
|
||||
if (z > 2*w)
|
||||
sum += fabsf(x[i - w]), ns++;
|
||||
}
|
||||
|
||||
for ( ; i < bw_stop + w; i++)
|
||||
if (++z > 2*w)
|
||||
sum += fabsf(x[i - w]), ns++;
|
||||
|
||||
int nf = ns ? 8 - (int)((16 * sum) / ns + 0.5f) : 8;
|
||||
|
||||
return LC3_CLIP(nf, 0, 7);
|
||||
}
|
||||
|
||||
/**
|
||||
* Noise filling
|
||||
* dt, bw Duration and bandwidth of the frame
|
||||
* nf, nf_seed The noise factor and pseudo-random seed
|
||||
* g Quantization gain
|
||||
* x, nq Spectral quantized, and count of significants
|
||||
*/
|
||||
LC3_HOT static void fill_noise(enum lc3_dt dt, enum lc3_bandwidth bw,
|
||||
int nf, uint16_t nf_seed, float g, float *x, int nq)
|
||||
{
|
||||
int bw_stop = lc3_ne(dt, (enum lc3_srate)LC3_MIN(bw, LC3_BANDWIDTH_FB));
|
||||
int w = 1 + (dt >= LC3_DT_7M5) + (dt>= LC3_DT_10M);
|
||||
|
||||
float s = g * (float)(8 - nf) / 16;
|
||||
int i, z = 0;
|
||||
|
||||
for (i = 6 * (1 + dt) - w; i < LC3_MIN(nq, bw_stop); i++) {
|
||||
z = x[i] ? 0 : z + 1;
|
||||
if (z > 2*w) {
|
||||
nf_seed = (13849 + nf_seed*31821) & 0xffff;
|
||||
x[i - w] = nf_seed & 0x8000 ? -s : s;
|
||||
}
|
||||
}
|
||||
|
||||
for ( ; i < bw_stop + w; i++)
|
||||
if (++z > 2*w) {
|
||||
nf_seed = (13849 + nf_seed*31821) & 0xffff;
|
||||
x[i - w] = nf_seed & 0x8000 ? -s : s;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Put noise factor
|
||||
* bits Bitstream context
|
||||
* nf Noise factor (0 to 7)
|
||||
*/
|
||||
static void put_noise_factor(lc3_bits_t *bits, int nf)
|
||||
{
|
||||
lc3_put_bits(bits, nf, 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get noise factor
|
||||
* bits Bitstream context
|
||||
* return Noise factor (0 to 7)
|
||||
*/
|
||||
static int get_noise_factor(lc3_bits_t *bits)
|
||||
{
|
||||
return lc3_get_bits(bits, 3);
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Encoding
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Bit consumption of the number of coded coefficients
|
||||
* dt, sr, nbytes Duration, samplerate and size of the frame
|
||||
* return Bit consumpution of the number of coded coefficients
|
||||
*/
|
||||
static int get_nbits_nq(enum lc3_dt dt, enum lc3_srate sr)
|
||||
{
|
||||
int ne = lc3_ne(dt, sr);
|
||||
return 4 + (ne > 32) + (ne > 64) + (ne > 128) + (ne > 256) + (ne > 512);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bit consumption of the arithmetic coder
|
||||
* dt, sr, nbytes Duration, samplerate and size of the frame
|
||||
* return Bit consumption of bitstream data
|
||||
*/
|
||||
static int get_nbits_ac(enum lc3_dt dt, enum lc3_srate sr, int nbytes)
|
||||
{
|
||||
return get_nbits_nq(dt, sr) +
|
||||
3 + lc3_hr(sr) + LC3_MIN((nbytes-1) / 160, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Spectrum analysis
|
||||
*/
|
||||
void lc3_spec_analyze(
|
||||
enum lc3_dt dt, enum lc3_srate sr, int nbytes,
|
||||
bool pitch, const lc3_tns_data_t *tns,
|
||||
struct lc3_spec_analysis *spec,
|
||||
float *x, struct lc3_spec_side *side)
|
||||
{
|
||||
bool reset_off;
|
||||
|
||||
/* --- Bit budget --- */
|
||||
|
||||
const int nbits_gain = 8;
|
||||
const int nbits_nf = 3;
|
||||
|
||||
int nbits_budget = 8*nbytes - get_nbits_ac(dt, sr, nbytes) -
|
||||
lc3_bwdet_get_nbits(sr) - lc3_ltpf_get_nbits(pitch) -
|
||||
lc3_sns_get_nbits() - lc3_tns_get_nbits(tns) - nbits_gain - nbits_nf;
|
||||
|
||||
/* --- Global gain --- */
|
||||
|
||||
float nbits_off = spec->nbits_off + spec->nbits_spare;
|
||||
nbits_off = fminf(fmaxf(nbits_off, -40), 40);
|
||||
nbits_off = 0.8f * spec->nbits_off + 0.2f * nbits_off;
|
||||
|
||||
int g_off = resolve_gain_offset(sr, nbytes);
|
||||
|
||||
int g_min, g_int = estimate_gain(dt, sr,
|
||||
x, nbytes, nbits_budget, nbits_off, g_off, &reset_off, &g_min);
|
||||
|
||||
/* --- Quantization --- */
|
||||
|
||||
quantize(dt, sr, g_int, x, &side->nq);
|
||||
|
||||
int nbits = compute_nbits(dt, sr, nbytes, x, &side->nq, 0, NULL);
|
||||
|
||||
spec->nbits_off = reset_off ? 0 : nbits_off;
|
||||
spec->nbits_spare = reset_off ? 0 : nbits_budget - nbits;
|
||||
|
||||
/* --- Adjust gain and requantize --- */
|
||||
|
||||
int g_adj = adjust_gain(dt, sr,
|
||||
g_off + g_int, nbits, nbits_budget, g_off + g_min);
|
||||
|
||||
if (g_adj)
|
||||
quantize(dt, sr, g_adj, x, &side->nq);
|
||||
|
||||
side->g_idx = g_int + g_adj + g_off;
|
||||
nbits = compute_nbits(dt, sr, nbytes,
|
||||
x, &side->nq, nbits_budget, &side->lsb_mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put spectral quantization side data
|
||||
*/
|
||||
void lc3_spec_put_side(lc3_bits_t *bits,
|
||||
enum lc3_dt dt, enum lc3_srate sr,
|
||||
const struct lc3_spec_side *side)
|
||||
{
|
||||
int nbits_nq = get_nbits_nq(dt, sr);
|
||||
|
||||
lc3_put_bits(bits, LC3_MAX(side->nq >> 1, 1) - 1, nbits_nq);
|
||||
lc3_put_bits(bits, side->lsb_mode, 1);
|
||||
lc3_put_bits(bits, side->g_idx, 8);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode spectral coefficients
|
||||
*/
|
||||
void lc3_spec_encode(lc3_bits_t *bits,
|
||||
enum lc3_dt dt, enum lc3_srate sr, enum lc3_bandwidth bw,
|
||||
int nbytes, const lc3_spec_side_t *side, float *x)
|
||||
{
|
||||
bool lsb_mode = side->lsb_mode;
|
||||
int nq = side->nq;
|
||||
|
||||
put_noise_factor(bits, estimate_noise(dt, bw, lc3_hr(sr), x, nq));
|
||||
|
||||
put_quantized(bits, dt, sr, nbytes, x, nq, lsb_mode);
|
||||
|
||||
int nbits_left = lc3_get_bits_left(bits);
|
||||
|
||||
if (lsb_mode)
|
||||
put_lsb(bits, nbits_left, lc3_hr(sr), x, nq);
|
||||
else
|
||||
put_residual(bits, nbits_left, lc3_hr(sr), x, nq);
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Decoding
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get spectral quantization side data
|
||||
*/
|
||||
int lc3_spec_get_side(lc3_bits_t *bits,
|
||||
enum lc3_dt dt, enum lc3_srate sr, struct lc3_spec_side *side)
|
||||
{
|
||||
int nbits_nq = get_nbits_nq(dt, sr);
|
||||
int ne = lc3_ne(dt, sr);
|
||||
|
||||
side->nq = (lc3_get_bits(bits, nbits_nq) + 1) << 1;
|
||||
side->lsb_mode = lc3_get_bit(bits);
|
||||
side->g_idx = lc3_get_bits(bits, 8);
|
||||
|
||||
return side->nq > ne ? (side->nq = ne), -1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode spectral coefficients
|
||||
*/
|
||||
int lc3_spec_decode(lc3_bits_t *bits,
|
||||
enum lc3_dt dt, enum lc3_srate sr, enum lc3_bandwidth bw,
|
||||
int nbytes, const lc3_spec_side_t *side, float *x)
|
||||
{
|
||||
bool lsb_mode = side->lsb_mode;
|
||||
int nq = side->nq;
|
||||
int ret = 0;
|
||||
|
||||
int nf = get_noise_factor(bits);
|
||||
uint16_t nf_seed;
|
||||
|
||||
if ((ret = get_quantized(bits, dt, sr, nbytes,
|
||||
nq, lsb_mode, x, &nf_seed)) < 0)
|
||||
return ret;
|
||||
|
||||
int nbits_left = lc3_get_bits_left(bits);
|
||||
|
||||
if (lsb_mode)
|
||||
get_lsb(bits, nbits_left, x, nq, &nf_seed);
|
||||
else
|
||||
get_residual(bits, nbits_left, lc3_hr(sr), x, nq);
|
||||
|
||||
int g_int = side->g_idx - resolve_gain_offset(sr, nbytes);
|
||||
float g = unquantize(dt, sr, g_int, x, nq);
|
||||
|
||||
if (nq > 2 || x[0] || x[1] || side->g_idx > 0 || nf < 7)
|
||||
fill_noise(dt, bw, nf, nf_seed, g, x, nq);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __LC3_SPEC_H
|
||||
#define __LC3_SPEC_H
|
||||
|
||||
#include "common.h"
|
||||
#include "tables.h"
|
||||
#include "bwdet.h"
|
||||
#include "ltpf.h"
|
||||
#include "tns.h"
|
||||
#include "sns.h"
|
||||
|
||||
|
||||
/**
|
||||
* Spectral quantization side data
|
||||
*/
|
||||
typedef struct lc3_spec_side {
|
||||
int g_idx, nq;
|
||||
bool lsb_mode;
|
||||
} lc3_spec_side_t;
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Encoding
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Spectrum analysis
|
||||
* dt, sr, nbytes Duration, samplerate and size of the frame
|
||||
* pitch, tns Pitch present indication and TNS bistream data
|
||||
* spec Context of analysis
|
||||
* x Spectral coefficients, scaled as output
|
||||
* side Return quantization data
|
||||
*/
|
||||
void lc3_spec_analyze(
|
||||
enum lc3_dt dt, enum lc3_srate sr, int nbytes,
|
||||
bool pitch, const lc3_tns_data_t *tns, lc3_spec_analysis_t *spec,
|
||||
float *x, lc3_spec_side_t *side);
|
||||
|
||||
/**
|
||||
* Put spectral quantization side data
|
||||
* bits Bitstream context
|
||||
* dt, sr Duration and samplerate of the frame
|
||||
* side Spectral quantization side data
|
||||
*/
|
||||
void lc3_spec_put_side(lc3_bits_t *bits,
|
||||
enum lc3_dt dt, enum lc3_srate sr, const lc3_spec_side_t *side);
|
||||
|
||||
/**
|
||||
* Encode spectral coefficients
|
||||
* bits Bitstream context
|
||||
* dt, sr, bw Duration, samplerate, bandwidth
|
||||
* nbytes and size of the frame
|
||||
* side, x Quantization data, and scaled coefficients
|
||||
*/
|
||||
void lc3_spec_encode(lc3_bits_t *bits,
|
||||
enum lc3_dt dt, enum lc3_srate sr, enum lc3_bandwidth bw,
|
||||
int nbytes, const lc3_spec_side_t *side, float *x);
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Decoding
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get spectral quantization side data
|
||||
* bits Bitstream context
|
||||
* dt, sr Duration and samplerate of the frame
|
||||
* side Return quantization side data
|
||||
* return 0: Ok -1: Invalid bandwidth indication
|
||||
*/
|
||||
int lc3_spec_get_side(lc3_bits_t *bits,
|
||||
enum lc3_dt dt, enum lc3_srate sr, lc3_spec_side_t *side);
|
||||
|
||||
/**
|
||||
* Decode spectral coefficients
|
||||
* bits Bitstream context
|
||||
* dt, sr, bw Duration, samplerate, bandwidth
|
||||
* nbytes and size of the frame
|
||||
* side Quantization side data
|
||||
* x Spectral coefficients
|
||||
* return 0: Ok -1: Invalid bitstream data
|
||||
*/
|
||||
int lc3_spec_decode(lc3_bits_t *bits,
|
||||
enum lc3_dt dt, enum lc3_srate sr, enum lc3_bandwidth bw,
|
||||
int nbytes, const lc3_spec_side_t *side, float *x);
|
||||
|
||||
|
||||
#endif /* __LC3_SPEC_H */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,161 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __LC3_TABLES_H
|
||||
#define __LC3_TABLES_H
|
||||
|
||||
#include "common.h"
|
||||
#include "bits.h"
|
||||
|
||||
|
||||
/**
|
||||
* Characteristics
|
||||
*
|
||||
* ns Number of temporal samples / frequency coefficients within a frame
|
||||
*
|
||||
* ne Number of encoded frequency coefficients
|
||||
*
|
||||
* nd Number of MDCT delayed samples, sum of half a frame and an ovelap
|
||||
* of future by 1.25 ms (2.5ms, 5ms and 10ms frame durations),
|
||||
* or 2 ms (7.5ms frame duration).
|
||||
*
|
||||
* nh Number of 18 ms samples of the history buffer, aligned on a frame
|
||||
*
|
||||
* nt Number of 1.25 ms previous samples
|
||||
*/
|
||||
|
||||
extern const int lc3_ns_2m5[LC3_NUM_SRATE];
|
||||
extern const int lc3_ne_2m5[LC3_NUM_SRATE];
|
||||
extern const int lc3_ns_4m [LC3_NUM_SRATE];
|
||||
|
||||
static inline int lc3_ns(enum lc3_dt dt, enum lc3_srate sr) {
|
||||
return lc3_ns_2m5[sr] * (1 + dt);
|
||||
}
|
||||
|
||||
static inline int lc3_ne(enum lc3_dt dt, enum lc3_srate sr) {
|
||||
return lc3_ne_2m5[sr] * (1 + dt);
|
||||
}
|
||||
|
||||
static inline int lc3_nd(enum lc3_dt dt, enum lc3_srate sr) {
|
||||
return ( lc3_ns(dt, sr) +
|
||||
(dt == LC3_DT_7M5 ? lc3_ns_4m[sr] : lc3_ns_2m5[sr]) ) >> 1;
|
||||
}
|
||||
|
||||
static inline int lc3_nh(enum lc3_dt dt, enum lc3_srate sr) {
|
||||
return sr > LC3_SRATE_48K_HR ? 0 :
|
||||
(8 + (dt == LC3_DT_7M5)) * lc3_ns_2m5[sr];
|
||||
}
|
||||
|
||||
static inline int lc3_nt(enum lc3_srate sr) {
|
||||
return lc3_ns_2m5[sr] >> 1;
|
||||
}
|
||||
|
||||
#define LC3_MAX_SRATE_HZ ( LC3_PLUS_HR ? 96000 : 48000 )
|
||||
|
||||
#define LC3_MAX_NS ( LC3_NS(10000, LC3_MAX_SRATE_HZ) )
|
||||
#define LC3_MAX_NE ( LC3_PLUS_HR ? LC3_MAX_NS : LC3_NS(10000, 40000) )
|
||||
|
||||
|
||||
/**
|
||||
* Limits on size of frame
|
||||
*/
|
||||
|
||||
extern const int lc3_frame_bytes_hr_lim
|
||||
[LC3_NUM_DT][LC3_NUM_SRATE - LC3_SRATE_48K_HR][2];
|
||||
|
||||
static inline int lc3_min_frame_bytes(enum lc3_dt dt, enum lc3_srate sr) {
|
||||
return !lc3_hr(sr) ? LC3_MIN_FRAME_BYTES :
|
||||
lc3_frame_bytes_hr_lim[dt][sr - LC3_SRATE_48K_HR][0];
|
||||
}
|
||||
|
||||
static inline int lc3_max_frame_bytes(enum lc3_dt dt, enum lc3_srate sr) {
|
||||
return !lc3_hr(sr) ? LC3_MAX_FRAME_BYTES :
|
||||
lc3_frame_bytes_hr_lim[dt][sr - LC3_SRATE_48K_HR][1];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* MDCT Twiddles and window coefficients
|
||||
*/
|
||||
|
||||
struct lc3_fft_bf3_twiddles { int n3; const struct lc3_complex (*t)[2]; };
|
||||
struct lc3_fft_bf2_twiddles { int n2; const struct lc3_complex *t; };
|
||||
struct lc3_mdct_rot_def { int n4; const struct lc3_complex *w; };
|
||||
|
||||
extern const struct lc3_fft_bf3_twiddles *lc3_fft_twiddles_bf3[];
|
||||
extern const struct lc3_fft_bf2_twiddles *lc3_fft_twiddles_bf2[][3];
|
||||
extern const struct lc3_mdct_rot_def *lc3_mdct_rot[LC3_NUM_DT][LC3_NUM_SRATE];
|
||||
|
||||
extern const float *lc3_mdct_win[LC3_NUM_DT][LC3_NUM_SRATE];
|
||||
|
||||
|
||||
/**
|
||||
* Limits of bands
|
||||
*/
|
||||
|
||||
#define LC3_MAX_BANDS 64
|
||||
|
||||
extern const int lc3_num_bands[LC3_NUM_DT][LC3_NUM_SRATE];
|
||||
extern const int *lc3_band_lim[LC3_NUM_DT][LC3_NUM_SRATE];
|
||||
|
||||
|
||||
/**
|
||||
* SNS Quantization
|
||||
*/
|
||||
|
||||
extern const float lc3_sns_lfcb[32][8];
|
||||
extern const float lc3_sns_hfcb[32][8];
|
||||
|
||||
struct lc3_sns_vq_gains {
|
||||
int count; const float *v;
|
||||
};
|
||||
|
||||
extern const struct lc3_sns_vq_gains lc3_sns_vq_gains[4];
|
||||
|
||||
extern const int32_t lc3_sns_mpvq_offsets[][11];
|
||||
|
||||
|
||||
/**
|
||||
* TNS Arithmetic Coding
|
||||
*/
|
||||
|
||||
extern const struct lc3_ac_model lc3_tns_order_models[];
|
||||
extern const uint16_t lc3_tns_order_bits[][8];
|
||||
|
||||
extern const struct lc3_ac_model lc3_tns_coeffs_models[];
|
||||
extern const uint16_t lc3_tns_coeffs_bits[][17];
|
||||
|
||||
|
||||
/**
|
||||
* Long Term Postfilter
|
||||
*/
|
||||
|
||||
extern const float *lc3_ltpf_cnum[LC3_NUM_SRATE][4];
|
||||
extern const float *lc3_ltpf_cden[LC3_NUM_SRATE][4];
|
||||
|
||||
|
||||
/**
|
||||
* Spectral Data Arithmetic Coding
|
||||
*/
|
||||
|
||||
extern const uint8_t lc3_spectrum_lookup[2][2][256][4];
|
||||
extern const struct lc3_ac_model lc3_spectrum_models[];
|
||||
extern const uint16_t lc3_spectrum_bits[][17];
|
||||
|
||||
|
||||
#endif /* __LC3_TABLES_H */
|
||||
@@ -0,0 +1,510 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#include "tns.h"
|
||||
#include "tables.h"
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Filter Coefficients
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Resolve LPC Weighting indication according bitrate
|
||||
* dt, nbytes Duration and size of the frame
|
||||
* return True when LPC Weighting enabled
|
||||
*/
|
||||
static bool resolve_lpc_weighting(enum lc3_dt dt, int nbytes)
|
||||
{
|
||||
return nbytes * 8 < 120 * (int)(1 + dt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return dot product of 2 vectors
|
||||
* a, b, n The 2 vectors of size `n`
|
||||
* return sum( a[i] * b[i] ), i = [0..n-1]
|
||||
*/
|
||||
LC3_HOT static inline float dot(const float *a, const float *b, int n)
|
||||
{
|
||||
float v = 0;
|
||||
|
||||
while (n--)
|
||||
v += *(a++) * *(b++);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
/**
|
||||
* LPC Coefficients
|
||||
* dt, bw Duration and bandwidth of the frame
|
||||
* maxorder Maximum order of filter
|
||||
* x Spectral coefficients
|
||||
* gain, a Output the prediction gains and LPC coefficients
|
||||
*/
|
||||
LC3_HOT static void compute_lpc_coeffs(
|
||||
enum lc3_dt dt, enum lc3_bandwidth bw, int maxorder,
|
||||
const float *x, float *gain, float (*a)[9])
|
||||
{
|
||||
|
||||
#if LC3_PLUS
|
||||
|
||||
static const int sub_2m5_nb[] = { 3, 10, 20 };
|
||||
static const int sub_2m5_wb[] = { 3, 20, 40 };
|
||||
static const int sub_2m5_sswb[] = { 3, 30, 60 };
|
||||
static const int sub_2m5_swb[] = { 3, 40, 80 };
|
||||
static const int sub_2m5_fb[] = { 3, 51, 100 };
|
||||
|
||||
static const int sub_5m_nb[] = { 6, 23, 40 };
|
||||
static const int sub_5m_wb[] = { 6, 43, 80 };
|
||||
static const int sub_5m_sswb[] = { 6, 63, 120 };
|
||||
static const int sub_5m_swb[] = { 6, 43, 80, 120, 160 };
|
||||
static const int sub_5m_fb[] = { 6, 53, 100, 150, 200 };
|
||||
|
||||
#endif /* LC3_PLUS */
|
||||
|
||||
static const int sub_7m5_nb[] = { 9, 26, 43, 60 };
|
||||
static const int sub_7m5_wb[] = { 9, 46, 83, 120 };
|
||||
static const int sub_7m5_sswb[] = { 9, 66, 123, 180 };
|
||||
static const int sub_7m5_swb[] = { 9, 46, 82, 120, 159, 200, 240 };
|
||||
static const int sub_7m5_fb[] = { 9, 56, 103, 150, 200, 250, 300 };
|
||||
|
||||
static const int sub_10m_nb[] = { 12, 34, 57, 80 };
|
||||
static const int sub_10m_wb[] = { 12, 61, 110, 160 };
|
||||
static const int sub_10m_sswb[] = { 12, 88, 164, 240 };
|
||||
static const int sub_10m_swb[] = { 12, 61, 110, 160, 213, 266, 320 };
|
||||
static const int sub_10m_fb[] = { 12, 74, 137, 200, 266, 333, 400 };
|
||||
|
||||
static const float lag_window[] = {
|
||||
1.00000000e+00, 9.98028026e-01, 9.92135406e-01, 9.82391584e-01,
|
||||
9.68910791e-01, 9.51849807e-01, 9.31404933e-01, 9.07808230e-01,
|
||||
8.81323137e-01
|
||||
};
|
||||
|
||||
const int *sub = (const int * const [LC3_NUM_DT][LC3_NUM_BANDWIDTH]){
|
||||
|
||||
#if LC3_PLUS
|
||||
|
||||
[LC3_DT_2M5] = {
|
||||
sub_2m5_nb, sub_2m5_wb, sub_2m5_sswb, sub_2m5_swb,
|
||||
sub_2m5_fb, sub_2m5_fb, sub_2m5_fb },
|
||||
|
||||
[LC3_DT_5M] = {
|
||||
sub_5m_nb , sub_5m_wb , sub_5m_sswb , sub_5m_swb ,
|
||||
sub_5m_fb , sub_5m_fb , sub_5m_fb },
|
||||
|
||||
#endif /* LC3_PLUS */
|
||||
|
||||
[LC3_DT_7M5] = {
|
||||
sub_7m5_nb, sub_7m5_wb, sub_7m5_sswb, sub_7m5_swb,
|
||||
sub_7m5_fb },
|
||||
|
||||
[LC3_DT_10M] = {
|
||||
sub_10m_nb, sub_10m_wb, sub_10m_sswb, sub_10m_swb,
|
||||
sub_10m_fb, sub_10m_fb, sub_10m_fb },
|
||||
|
||||
}[dt][bw];
|
||||
|
||||
/* --- Normalized autocorrelation --- */
|
||||
|
||||
int nfilters = 1 + (dt >= LC3_DT_5M && bw >= LC3_BANDWIDTH_SWB);
|
||||
int nsubdivisions = 2 + (dt >= LC3_DT_7M5);
|
||||
|
||||
const float *xs, *xe = x + *sub;
|
||||
float r[2][9];
|
||||
|
||||
for (int f = 0; f < nfilters; f++) {
|
||||
float c[9][3] = { 0 };
|
||||
|
||||
for (int s = 0; s < nsubdivisions; s++) {
|
||||
xs = xe, xe = x + *(++sub);
|
||||
|
||||
for (int k = 0; k <= maxorder; k++)
|
||||
c[k][s] = dot(xs, xs + k, (xe - xs) - k);
|
||||
}
|
||||
|
||||
r[f][0] = nsubdivisions;
|
||||
if (nsubdivisions == 2) {
|
||||
float e0 = c[0][0], e1 = c[0][1];
|
||||
for (int k = 1; k <= maxorder; k++)
|
||||
r[f][k] = e0 == 0 || e1 == 0 ? 0 :
|
||||
(c[k][0]/e0 + c[k][1]/e1) * lag_window[k];
|
||||
|
||||
} else {
|
||||
float e0 = c[0][0], e1 = c[0][1], e2 = c[0][2];
|
||||
for (int k = 1; k <= maxorder; k++)
|
||||
r[f][k] = e0 == 0 || e1 == 0 || e2 == 0 ? 0 :
|
||||
(c[k][0]/e0 + c[k][1]/e1 + c[k][2]/e2) * lag_window[k];
|
||||
}
|
||||
}
|
||||
|
||||
/* --- Levinson-Durbin recursion --- */
|
||||
|
||||
for (int f = 0; f < nfilters; f++) {
|
||||
float *a0 = a[f], a1[9];
|
||||
float err = r[f][0], rc;
|
||||
|
||||
gain[f] = err;
|
||||
|
||||
a0[0] = 1;
|
||||
for (int k = 1; k <= maxorder; ) {
|
||||
|
||||
rc = -r[f][k];
|
||||
for (int i = 1; i < k; i++)
|
||||
rc -= a0[i] * r[f][k-i];
|
||||
|
||||
rc /= err;
|
||||
err *= 1 - rc * rc;
|
||||
|
||||
for (int i = 1; i < k; i++)
|
||||
a1[i] = a0[i] + rc * a0[k-i];
|
||||
a1[k++] = rc;
|
||||
|
||||
rc = -r[f][k];
|
||||
for (int i = 1; i < k; i++)
|
||||
rc -= a1[i] * r[f][k-i];
|
||||
|
||||
rc /= err;
|
||||
err *= 1 - rc * rc;
|
||||
|
||||
for (int i = 1; i < k; i++)
|
||||
a0[i] = a1[i] + rc * a1[k-i];
|
||||
a0[k++] = rc;
|
||||
}
|
||||
|
||||
gain[f] /= err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* LPC Weighting
|
||||
* gain, a Prediction gain and LPC coefficients, weighted as output
|
||||
*/
|
||||
LC3_HOT static void lpc_weighting(float pred_gain, float *a)
|
||||
{
|
||||
float gamma = 1.f - (1.f - 0.85f) * (2.f - pred_gain) / (2.f - 1.5f);
|
||||
float g = 1.f;
|
||||
|
||||
for (int i = 1; i < 9; i++)
|
||||
a[i] *= (g *= gamma);
|
||||
}
|
||||
|
||||
/**
|
||||
* LPC reflection
|
||||
* a, maxorder LPC coefficients, and maximum order (4 or 8)
|
||||
* rc Output refelection coefficients
|
||||
*/
|
||||
LC3_HOT static void lpc_reflection(
|
||||
const float *a, int maxorder, float *rc)
|
||||
{
|
||||
float e, b[2][7], *b0, *b1;
|
||||
|
||||
rc[maxorder-1] = a[maxorder];
|
||||
e = 1 - rc[maxorder-1] * rc[maxorder-1];
|
||||
|
||||
b1 = b[1];
|
||||
for (int i = 0; i < maxorder-1; i++)
|
||||
b1[i] = (a[1+i] - rc[maxorder-1] * a[(maxorder-1)-i]) / e;
|
||||
|
||||
for (int k = maxorder-2; k > 0; k--) {
|
||||
b0 = b1, b1 = b[k & 1];
|
||||
|
||||
rc[k] = b0[k];
|
||||
e = 1 - rc[k] * rc[k];
|
||||
|
||||
for (int i = 0; i < k; i++)
|
||||
b1[i] = (b0[i] - rc[k] * b0[k-1-i]) / e;
|
||||
}
|
||||
|
||||
rc[0] = b1[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Quantization of RC coefficients
|
||||
* rc, maxorder Refelection coefficients, and maximum order (4 or 8)
|
||||
* order Return order of coefficients
|
||||
* rc_i Return quantized coefficients
|
||||
*/
|
||||
static void quantize_rc(const float *rc, int maxorder, int *order, int *rc_q)
|
||||
{
|
||||
/* Quantization table, sin(delta * (i + 0.5)), delta = Pi / 17,
|
||||
* rounded to fixed point Q15 value (LC3-Plus HR requirements). */
|
||||
|
||||
static float q_thr[] = {
|
||||
0x0bcfp-15, 0x2307p-15, 0x390ep-15, 0x4d23p-15,
|
||||
0x5e98p-15, 0x6cd4p-15, 0x775bp-15, 0x7dd2p-15,
|
||||
};
|
||||
|
||||
*order = maxorder;
|
||||
|
||||
for (int i = 0; i < maxorder; i++) {
|
||||
float rc_m = fabsf(rc[i]);
|
||||
|
||||
rc_q[i] = 4 * (rc_m >= q_thr[4]);
|
||||
for (int j = 0; j < 4 && rc_m >= q_thr[rc_q[i]]; j++, rc_q[i]++);
|
||||
|
||||
if (rc[i] < 0)
|
||||
rc_q[i] = -rc_q[i];
|
||||
|
||||
*order = rc_q[i] != 0 ? maxorder : *order - 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unquantization of RC coefficients
|
||||
* rc_q, order Quantized coefficients, and order
|
||||
* rc Return refelection coefficients
|
||||
*/
|
||||
static void unquantize_rc(const int *rc_q, int order, float rc[8])
|
||||
{
|
||||
/* Quantization table, sin(delta * i), delta = Pi / 17,
|
||||
* rounded to fixed point Q15 value (LC3-Plus HR requirements). */
|
||||
|
||||
static float q_inv[] = {
|
||||
0x0000p-15, 0x1785p-15, 0x2e3dp-15, 0x4362p-15,
|
||||
0x563cp-15, 0x6625p-15, 0x7295p-15, 0x7b1dp-15, 0x7f74p-15,
|
||||
};
|
||||
|
||||
int i;
|
||||
|
||||
for (i = 0; i < order; i++) {
|
||||
float rc_m = q_inv[LC3_ABS(rc_q[i])];
|
||||
rc[i] = rc_q[i] < 0 ? -rc_m : rc_m;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Filtering
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Forward filtering
|
||||
* dt, bw Duration and bandwidth of the frame
|
||||
* rc_order, rc Order of coefficients, and coefficients
|
||||
* x Spectral coefficients, filtered as output
|
||||
*/
|
||||
LC3_HOT static void forward_filtering(
|
||||
enum lc3_dt dt, enum lc3_bandwidth bw,
|
||||
const int rc_order[2], float (* const rc)[8], float *x)
|
||||
{
|
||||
int nfilters = 1 + (dt >= LC3_DT_5M && bw >= LC3_BANDWIDTH_SWB);
|
||||
int nf = lc3_ne(dt, (enum lc3_srate)LC3_MIN(bw, LC3_BANDWIDTH_FB))
|
||||
>> (nfilters - 1);
|
||||
int i0, ie = 3*(1 + dt);
|
||||
|
||||
float s[8] = { 0 };
|
||||
|
||||
for (int f = 0; f < nfilters; f++) {
|
||||
|
||||
i0 = ie;
|
||||
ie = nf * (1 + f);
|
||||
|
||||
if (!rc_order[f])
|
||||
continue;
|
||||
|
||||
for (int i = i0; i < ie; i++) {
|
||||
float xi = x[i];
|
||||
float s0, s1 = xi;
|
||||
|
||||
for (int k = 0; k < rc_order[f]; k++) {
|
||||
s0 = s[k];
|
||||
s[k] = s1;
|
||||
|
||||
s1 = rc[f][k] * xi + s0;
|
||||
xi += rc[f][k] * s0;
|
||||
}
|
||||
|
||||
x[i] = xi;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inverse filtering
|
||||
* dt, bw Duration and bandwidth of the frame
|
||||
* rc_order, rc Order of coefficients, and unquantized coefficients
|
||||
* x Spectral coefficients, filtered as output
|
||||
*/
|
||||
LC3_HOT static void inverse_filtering(
|
||||
enum lc3_dt dt, enum lc3_bandwidth bw,
|
||||
const int rc_order[2], float (* const rc)[8], float *x)
|
||||
{
|
||||
int nfilters = 1 + (dt >= LC3_DT_5M && bw >= LC3_BANDWIDTH_SWB);
|
||||
int nf = lc3_ne(dt, (enum lc3_srate)LC3_MIN(bw, LC3_BANDWIDTH_FB))
|
||||
>> (nfilters - 1);
|
||||
int i0, ie = 3*(1 + dt);
|
||||
|
||||
float s[8] = { 0 };
|
||||
|
||||
for (int f = 0; f < nfilters; f++) {
|
||||
|
||||
i0 = ie;
|
||||
ie = nf * (1 + f);
|
||||
|
||||
if (!rc_order[f])
|
||||
continue;
|
||||
|
||||
for (int i = i0; i < ie; i++) {
|
||||
float xi = x[i];
|
||||
|
||||
xi -= s[7] * rc[f][7];
|
||||
for (int k = 6; k >= 0; k--) {
|
||||
xi -= s[k] * rc[f][k];
|
||||
s[k+1] = s[k] + rc[f][k] * xi;
|
||||
}
|
||||
s[0] = xi;
|
||||
x[i] = xi;
|
||||
}
|
||||
|
||||
for (int k = 7; k >= rc_order[f]; k--)
|
||||
s[k] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Interface
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* TNS analysis
|
||||
*/
|
||||
void lc3_tns_analyze(enum lc3_dt dt, enum lc3_bandwidth bw,
|
||||
bool nn_flag, int nbytes, struct lc3_tns_data *data, float *x)
|
||||
{
|
||||
/* Processing steps :
|
||||
* - Determine the LPC (Linear Predictive Coding) Coefficients
|
||||
* - Check is the filtering is disabled
|
||||
* - The coefficients are weighted on low bitrates and predicition gain
|
||||
* - Convert to reflection coefficients and quantize
|
||||
* - Finally filter the spectral coefficients */
|
||||
|
||||
float pred_gain[2], a[2][9];
|
||||
float rc[2][8];
|
||||
|
||||
data->lpc_weighting = resolve_lpc_weighting(dt, nbytes);
|
||||
data->nfilters = 1 + (dt >= LC3_DT_5M && bw >= LC3_BANDWIDTH_SWB);
|
||||
int maxorder = dt <= LC3_DT_5M ? 4 : 8;
|
||||
|
||||
compute_lpc_coeffs(dt, bw, maxorder, x, pred_gain, a);
|
||||
|
||||
for (int f = 0; f < data->nfilters; f++) {
|
||||
|
||||
data->rc_order[f] = 0;
|
||||
if (nn_flag || pred_gain[f] <= 1.5f)
|
||||
continue;
|
||||
|
||||
if (data->lpc_weighting && pred_gain[f] < 2.f)
|
||||
lpc_weighting(pred_gain[f], a[f]);
|
||||
|
||||
lpc_reflection(a[f], maxorder, rc[f]);
|
||||
|
||||
quantize_rc(rc[f], maxorder, &data->rc_order[f], data->rc[f]);
|
||||
unquantize_rc(data->rc[f], data->rc_order[f], rc[f]);
|
||||
}
|
||||
|
||||
forward_filtering(dt, bw, data->rc_order, rc, x);
|
||||
}
|
||||
|
||||
/**
|
||||
* TNS synthesis
|
||||
*/
|
||||
void lc3_tns_synthesize(enum lc3_dt dt, enum lc3_bandwidth bw,
|
||||
const struct lc3_tns_data *data, float *x)
|
||||
{
|
||||
float rc[2][8] = { 0 };
|
||||
|
||||
for (int f = 0; f < data->nfilters; f++)
|
||||
if (data->rc_order[f])
|
||||
unquantize_rc(data->rc[f], data->rc_order[f], rc[f]);
|
||||
|
||||
inverse_filtering(dt, bw, data->rc_order, rc, x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bit consumption of bitstream data
|
||||
*/
|
||||
int lc3_tns_get_nbits(const struct lc3_tns_data *data)
|
||||
{
|
||||
int nbits = 0;
|
||||
|
||||
for (int f = 0; f < data->nfilters; f++) {
|
||||
|
||||
int nbits_2048 = 2048;
|
||||
int rc_order = data->rc_order[f];
|
||||
|
||||
nbits_2048 += rc_order > 0 ? lc3_tns_order_bits
|
||||
[data->lpc_weighting][rc_order-1] : 0;
|
||||
|
||||
for (int i = 0; i < rc_order; i++)
|
||||
nbits_2048 += lc3_tns_coeffs_bits[i][8 + data->rc[f][i]];
|
||||
|
||||
nbits += (nbits_2048 + (1 << 11) - 1) >> 11;
|
||||
}
|
||||
|
||||
return nbits;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put bitstream data
|
||||
*/
|
||||
void lc3_tns_put_data(lc3_bits_t *bits, const struct lc3_tns_data *data)
|
||||
{
|
||||
for (int f = 0; f < data->nfilters; f++) {
|
||||
int rc_order = data->rc_order[f];
|
||||
|
||||
lc3_put_bits(bits, rc_order > 0, 1);
|
||||
if (rc_order <= 0)
|
||||
continue;
|
||||
|
||||
lc3_put_symbol(bits,
|
||||
lc3_tns_order_models + data->lpc_weighting, rc_order-1);
|
||||
|
||||
for (int i = 0; i < rc_order; i++)
|
||||
lc3_put_symbol(bits,
|
||||
lc3_tns_coeffs_models + i, 8 + data->rc[f][i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get bitstream data
|
||||
*/
|
||||
int lc3_tns_get_data(lc3_bits_t *bits,
|
||||
enum lc3_dt dt, enum lc3_bandwidth bw, int nbytes, lc3_tns_data_t *data)
|
||||
{
|
||||
data->nfilters = 1 + (dt >= LC3_DT_5M && bw >= LC3_BANDWIDTH_SWB);
|
||||
data->lpc_weighting = resolve_lpc_weighting(dt, nbytes);
|
||||
|
||||
for (int f = 0; f < data->nfilters; f++) {
|
||||
|
||||
data->rc_order[f] = lc3_get_bit(bits);
|
||||
if (!data->rc_order[f])
|
||||
continue;
|
||||
|
||||
data->rc_order[f] += lc3_get_symbol(bits,
|
||||
lc3_tns_order_models + data->lpc_weighting);
|
||||
if (dt <= LC3_DT_5M && data->rc_order[f] > 4)
|
||||
return -1;
|
||||
|
||||
for (int i = 0; i < data->rc_order[f]; i++)
|
||||
data->rc[f][i] = (int)lc3_get_symbol(bits,
|
||||
lc3_tns_coeffs_models + i) - 8;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __LC3_TNS_H
|
||||
#define __LC3_TNS_H
|
||||
|
||||
#include "common.h"
|
||||
#include "bits.h"
|
||||
|
||||
|
||||
/**
|
||||
* Bitstream data
|
||||
*/
|
||||
|
||||
typedef struct lc3_tns_data {
|
||||
int nfilters;
|
||||
bool lpc_weighting;
|
||||
int rc_order[2];
|
||||
int rc[2][8];
|
||||
} lc3_tns_data_t;
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Encoding
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* TNS analysis
|
||||
* dt, bw Duration and bandwidth of the frame
|
||||
* nn_flag True when high energy detected near Nyquist frequency
|
||||
* nbytes Size in bytes of the frame
|
||||
* data Return bitstream data
|
||||
* x Spectral coefficients, filtered as output
|
||||
*/
|
||||
void lc3_tns_analyze(enum lc3_dt dt, enum lc3_bandwidth bw,
|
||||
bool nn_flag, int nbytes, lc3_tns_data_t *data, float *x);
|
||||
|
||||
/**
|
||||
* Return number of bits coding the data
|
||||
* data Bitstream data
|
||||
* return Bit consumption
|
||||
*/
|
||||
int lc3_tns_get_nbits(const lc3_tns_data_t *data);
|
||||
|
||||
/**
|
||||
* Put bitstream data
|
||||
* bits Bitstream context
|
||||
* data Bitstream data
|
||||
*/
|
||||
void lc3_tns_put_data(lc3_bits_t *bits, const lc3_tns_data_t *data);
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Decoding
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get bitstream data
|
||||
* bits Bitstream context
|
||||
* dt, bw Duration and bandwidth of the frame
|
||||
* nbytes Size in bytes of the frame
|
||||
* data Bitstream data
|
||||
* return 0: Ok -1: Invalid bitstream data
|
||||
*/
|
||||
int lc3_tns_get_data(lc3_bits_t *bits,
|
||||
enum lc3_dt dt, enum lc3_bandwidth bw, int nbytes, lc3_tns_data_t *data);
|
||||
|
||||
/**
|
||||
* TNS synthesis
|
||||
* dt, bw Duration and bandwidth of the frame
|
||||
* data Bitstream data
|
||||
* x Spectral coefficients, filtered as output
|
||||
*/
|
||||
void lc3_tns_synthesize(enum lc3_dt dt, enum lc3_bandwidth bw,
|
||||
const lc3_tns_data_t *data, float *x);
|
||||
|
||||
|
||||
#endif /* __LC3_TNS_H */
|
||||
@@ -1,6 +1,9 @@
|
||||
module FaceclawGraphics {
|
||||
header "FaceclawGraphics.h"
|
||||
header "FaceclawFontRenderer.h"
|
||||
header "FaceclawBluetooth.h"
|
||||
header "FaceclawSocket.h"
|
||||
header "FaceclawLc3Decoder.h"
|
||||
header "FaceclawSpeech.h"
|
||||
export *
|
||||
}
|
||||
Reference in new issue
Block a user