Set the right channels number and layout in the encoder.

This commit is contained in:
Damiano Galassi committed 2016-07-26 13:00:42 +02:00
1 parent 1b0316237c
commit ffe8abd0b6
8 files changed
+248 -73

No files matched your search

+5
View File
@@ -10,10 +10,15 @@
#define FFmpegUtils_h
#include <Foundation/Foundation.h>
#include <CoreAudio/CoreAudio.h>
#include <avcodec.h>
void FFInitFFmpeg();
enum AVCodecID FourCCToCodecID(OSType formatID);
OSType CodecIDToFourCC(enum AVCodecID codecID);
int remap_layout(AudioChannelLayout *layout, uint64_t in_layout, int count);
int get_aac_tag(uint64_t in_layout);
#endif /* FFmpegUtils_h */
+46
View File
@@ -112,3 +112,49 @@ OSType OSTypeFCodecIDToFourCC(enum AVCodecID codecID)
}
return AV_CODEC_ID_NONE;
}
static av_cold int get_channel_label(int channel)
{
uint64_t map = 1 << channel;
if (map <= AV_CH_LOW_FREQUENCY)
return channel + 1;
else if (map <= AV_CH_BACK_RIGHT)
return channel + 29;
else if (map <= AV_CH_BACK_CENTER)
return channel - 1;
else if (map <= AV_CH_SIDE_RIGHT)
return channel - 4;
else if (map <= AV_CH_TOP_BACK_RIGHT)
return channel + 1;
else if (map <= AV_CH_STEREO_RIGHT)
return -1;
else if (map <= AV_CH_WIDE_RIGHT)
return channel + 4;
else if (map <= AV_CH_SURROUND_DIRECT_RIGHT)
return channel - 23;
else if (map == AV_CH_LOW_FREQUENCY_2)
return kAudioChannelLabel_LFE2;
else
return -1;
}
int remap_layout(AudioChannelLayout *layout, uint64_t in_layout, int count)
{
int i;
int c = 0;
layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
layout->mNumberChannelDescriptions = count;
for (i = 0; i < count; i++) {
int label;
while (!(in_layout & (1 << c)) && c < 64)
c++;
if (c == 64)
return AVERROR(EINVAL); // This should never happen
label = get_channel_label(c);
layout->mChannelDescriptions[i].mChannelLabel = label;
if (label < 0)
return AVERROR(EINVAL);
c++;
}
return 0;
}
+5 -2
View File
@@ -19,10 +19,13 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithAudioFormat:(AudioStreamBasicDescription)asbd mixdownType:(NSString *)mixdownType magicCookie:(NSData *)magicCookie error:(NSError **)error;
@property (nonatomic, readonly, nullable) AudioChannelLayout *inputLayout;
@property (nonatomic, readonly) UInt32 inputLayoutSize;
@property (nonatomic, readonly) AudioStreamBasicDescription inputFormat;
@property (nonatomic, readonly) AudioStreamBasicDescription outputFormat;
- (void)addSample:(MP42SampleBuffer *)sample;
@property (nonatomic, readonly, nullable) AudioChannelLayout *outputLayout;
@property (nonatomic, readonly) UInt32 outputLayoutSize;
@property (nonatomic, readonly) AudioStreamBasicDescription outputFormat;
@end
+87 -39
View File
@@ -23,11 +23,18 @@ struct MP42DecodeContext {
AVCodecContext *avctx;
AVAudioResampleContext *resampler;
AudioStreamBasicDescription inputFormat;
AudioStreamBasicDescription outputFormat;
AudioChannelLayout **inputLayout;
UInt32 *inputLayoutSize;
AudioStreamBasicDescription *inputFormat;
AudioChannelLayout **outputLayout;
UInt32 *outputLayoutSize;
AudioStreamBasicDescription *outputFormat;
enum AVMatrixEncoding matrix_encoding;
enum AVSampleFormat out_sample_format;
BOOL reconfigured;
};
typedef struct MP42DecodeContext MP42DecodeContext;
@@ -133,11 +140,18 @@ typedef struct MP42DecodeContext MP42DecodeContext;
_context = malloc(sizeof(MP42DecodeContext));
_context->avctx = _avctx;
_context->resampler = NULL;
_context->inputFormat = _inputFormat;
_context->outputFormat = _outputFormat;
_context->inputFormat = &_inputFormat;
_context->outputFormat = &_outputFormat;
_context->outputLayout = &_outputLayout;
_context->outputLayoutSize = &_outputLayoutSize;
_context->matrix_encoding = matrix_encoding;
_context->out_sample_format = AV_SAMPLE_FMT_FLT;
_context->reconfigured = NO;
// Init the FIFOs
_inputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
_outputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
@@ -161,6 +175,9 @@ typedef struct MP42DecodeContext MP42DecodeContext;
avresample_free(&_context->resampler);
}
}
free(_inputLayout);
free(_outputLayout);
}
- (void)start
@@ -172,6 +189,11 @@ typedef struct MP42DecodeContext MP42DecodeContext;
#pragma mark - Public methods
- (void)reconfigure
{
return;
}
- (void)addSample:(MP42SampleBuffer *)sample
{
[_inputSamplesBuffer enqueue:sample];
@@ -210,48 +232,69 @@ static int initResampler(MP42DecodeContext *context, AVFrame *frame)
int ret = 0;
AVAudioResampleContext *resampler;
// Get real channels number and layout
int nb_channels = av_get_channel_layout_nb_channels(frame->channel_layout);
if (context->inputFormat->mChannelsPerFrame == context->outputFormat->mChannelsPerFrame) {
context->outputFormat->mChannelsPerFrame = nb_channels;
context->inputFormat->mChannelsPerFrame = nb_channels;
}
context->outputFormat->mChannelsPerFrame = context->outputFormat->mChannelsPerFrame;
context->outputFormat->mBytesPerPacket = 4 * context->outputFormat->mChannelsPerFrame;
context->outputFormat->mBytesPerFrame = context->outputFormat->mBytesPerPacket * context->outputFormat->mFramesPerPacket;
// Set the output channel layout
int layout = AV_CH_LAYOUT_STEREO;
if (context->outputFormat->mChannelsPerFrame < context->inputFormat->mChannelsPerFrame) {
if (context->outputFormat->mChannelsPerFrame == 2) {
layout = AV_CH_LAYOUT_STEREO;
}
else if (context->outputFormat->mChannelsPerFrame == 1) {
layout = AV_CH_LAYOUT_MONO;
}
}
else {
layout = frame->channel_layout;
}
UInt32 layout_size = sizeof(AudioChannelLayout) +
sizeof(AudioChannelDescription) * nb_channels;
AudioChannelLayout *outputLayout = malloc(layout_size);
bzero(outputLayout, layout_size);
remap_layout(outputLayout, frame->channel_layout, nb_channels);
*context->outputLayoutSize = layout_size;
*context->outputLayout = outputLayout;
// Tell the audio unit loop we need to reconfigure the chain.
context->reconfigured = YES;
// We want float interleaved, FFmpeg usually prefers
// float planar, so we need to convert it.
resampler = avresample_alloc_context();
if (!resampler) {
return 1;
}
if (context->avctx->sample_fmt != context->out_sample_format ||
context->outputFormat.mChannelsPerFrame != context->inputFormat.mChannelsPerFrame) {
av_opt_set_int(resampler, "in_channel_layout", frame->channel_layout, 0);
av_opt_set_int(resampler, "out_channel_layout", layout, 0);
resampler = avresample_alloc_context();
if (!resampler) {
return 1;
}
av_opt_set_int(resampler, "in_sample_rate", context->inputFormat->mSampleRate, 0);
av_opt_set_int(resampler, "out_sample_rate", context->inputFormat->mSampleRate, 0);
av_opt_set_int(resampler, "in_channel_layout", frame->channel_layout, 0);
if (context->outputFormat.mChannelsPerFrame < context->inputFormat.mChannelsPerFrame) {
if (context->outputFormat.mChannelsPerFrame == 2) {
av_opt_set_int(resampler, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
}
else if (context->outputFormat.mChannelsPerFrame == 1) {
av_opt_set_int(resampler, "out_channel_layout", AV_CH_LAYOUT_MONO, 0);
}
}
else {
av_opt_set_int(resampler, "out_channel_layout", frame->channel_layout, 0);
}
av_opt_set_int(resampler, "in_sample_fmt", context->avctx->sample_fmt, 0);
av_opt_set_int(resampler, "out_sample_fmt", context->out_sample_format, 0);
av_opt_set_int(resampler, "in_sample_rate", context->inputFormat.mSampleRate, 0);
av_opt_set_int(resampler, "out_sample_rate", context->inputFormat.mSampleRate, 0);
if (context->matrix_encoding != AV_MATRIX_ENCODING_NONE) {
av_opt_set_int(resampler, "matrix_encoding", context->matrix_encoding, 0);
}
av_opt_set_int(resampler, "in_sample_fmt", context->avctx->sample_fmt, 0);
av_opt_set_int(resampler, "out_sample_fmt", context->out_sample_format, 0);
ret = avresample_open(resampler);
if (context->matrix_encoding != AV_MATRIX_ENCODING_NONE) {
av_opt_set_int(resampler, "matrix_encoding", context->matrix_encoding, 0);
}
ret = avresample_open(resampler);
if (ret) {
avresample_free(&resampler);
}
else {
context->resampler = resampler;
}
if (ret) {
avresample_free(&resampler);
}
else {
context->resampler = resampler;
}
return ret;
@@ -268,7 +311,7 @@ static int resample(MP42DecodeContext *context, AVFrame *frame, uint8_t **output
}
int out_samples = avresample_get_out_samples(context->resampler, frame->nb_samples);
int out_nb_channels = context->outputFormat.mChannelsPerFrame;
int out_nb_channels = context->outputFormat->mChannelsPerFrame;
int in_plane_size, out_plane_size = 0;
@@ -343,6 +386,11 @@ static int decode(MP42DecodeContext *context, MP42SampleBuffer *inSample, MP42Sa
decode(_context, sampleBuffer, &outSample);
}
if (_context->reconfigured) {
[_outputUnit reconfigure];
_context->reconfigured = NO;
}
if (outSample) {
if (_outputType == MP42AudioUnitOutputPush) {
[_outputUnit addSample:outSample];
+7 -3
View File
@@ -18,11 +18,15 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithInputUnit:(id<MP42AudioUnit>)unit bitRate:(NSUInteger)bitRate error:(NSError **)error;
@property (nonatomic, readonly, nullable) AudioChannelLayout *inputLayout;
@property (nonatomic, readonly) UInt32 inputLayoutSize;
@property (nonatomic, readonly) AudioStreamBasicDescription inputFormat;
@property (nonatomic, readonly) AudioStreamBasicDescription outputFormat;
@property (nonatomic, readonly) NSData *magicCookie;
- (void)addSample:(MP42SampleBuffer *)sample;
@property (nonatomic, readonly, nullable) AudioChannelLayout *outputLayout;
@property (nonatomic, readonly) UInt32 outputLayoutSize;
@property (nonatomic, readonly) AudioStreamBasicDescription outputFormat;
@property (nonatomic, readonly) NSData *magicCookie;
@end
+86 -29
View File
@@ -41,13 +41,14 @@ typedef struct AudioFileIO
}
@property (nonatomic, readonly) AudioConverterRef encoder;
@property (nonatomic, readonly) NSUInteger bitrate;
@property (nonatomic, readonly) NSThread *decoderThread;
@property (nonatomic, readonly) MP42Fifo<MP42SampleBuffer *> *inputSamplesBuffer;
@property (nonatomic, readonly) MP42Fifo<MP42SampleBuffer *> *outputSamplesBuffer;
@property (nonatomic, readonly) sfifo_t *ringBuffer;
@property (nonatomic, readonly) AudioFileIO afio;
@property (nonatomic, readonly) AudioFileIO *afio;
@property (nonatomic, readonly, unsafe_unretained) id<MP42AudioUnit> inputUnit;
@@ -68,13 +69,7 @@ typedef struct AudioFileIO
_inputUnit.outputUnit = self;
_inputFormat = unit.outputFormat;
if (![self initConverterWithBitRate:bitRate]) {
return nil;
}
if (![self createMagicCookie]) {
return nil;
}
_bitrate = bitRate;
_inputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
_outputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
@@ -86,13 +81,7 @@ typedef struct AudioFileIO
- (void)dealloc
{
if (_encoder) {
AudioConverterDispose(_encoder);
}
if (_ringBuffer) {
sfifo_close(_ringBuffer);
free(_ringBuffer);
}
[self disposeConverter];
}
#pragma mark - Encoder Init
@@ -144,7 +133,7 @@ typedef struct AudioFileIO
if (err) {
NSLog(@"err: kAudioConverterApplicableEncodeBitRates From AudioConverter");
}
bitrateCounts = tmpsiz / sizeof( AudioValueRange );
bitrateCounts = tmpsiz / sizeof(AudioValueRange);
// Set bitrate.
tmp = bitrate * outputFormat.mChannelsPerFrame * 1000;
@@ -157,7 +146,31 @@ typedef struct AudioFileIO
free(bitrates);
AudioConverterSetProperty(_encoder, kAudioConverterEncodeBitRate,
sizeof( tmp ), &tmp);
sizeof(tmp), &tmp);
// Set the input channel layout.
if (_inputLayout) {
err = AudioConverterSetProperty(_encoder, kAudioConverterInputChannelLayout, _inputLayoutSize, _inputLayout);
if (err) {
NSLog(@"err: kAudioConverterInputChannelLayout From AudioConverter");
}
}
// Get the output channel layout.
/*err = AudioConverterGetPropertyInfo(_encoder,
kAudioConverterOutputChannelLayout,
&_outputLayoutSize, NULL);
if (err) {
NSLog(@"err: kAudioConverterOutputChannelLayout From AudioConverter");
}
_outputLayout = malloc(_outputLayoutSize);
err = AudioConverterGetProperty(_encoder,
kAudioConverterOutputChannelLayout,
&_outputLayoutSize, &_outputLayout);
if (err) {
NSLog(@"err: kAudioConverterOutputChannelLayout From AudioConverter");
}*/
// Get real input.
tmpsiz = sizeof(_inputFormat);
@@ -186,24 +199,50 @@ typedef struct AudioFileIO
sfifo_init(_ringBuffer, ringbuffer_len);
// Set up buffers and data proc info struct
struct AudioFileIO encoderData;
encoderData.srcBufferSize = 32768;
encoderData.srcBuffer = (char *) malloc(encoderData.srcBufferSize);
_afio = malloc(sizeof(AudioFileIO));
_afio->srcBufferSize = 32768;
_afio->srcBuffer = (char *) malloc(_afio->srcBufferSize);
encoderData.outputMaxSize = outputSizePerPacket;
_afio->outputMaxSize = outputSizePerPacket;
encoderData.srcSizePerPacket = _inputFormat.mBytesPerPacket;
encoderData.channelsPerFrame = _inputFormat.mChannelsPerFrame;
encoderData.numPacketsPerRead = encoderData.srcBufferSize / encoderData.srcSizePerPacket;
_afio->srcSizePerPacket = _inputFormat.mBytesPerPacket;
_afio->channelsPerFrame = _inputFormat.mChannelsPerFrame;
_afio->numPacketsPerRead = _afio->srcBufferSize / _afio->srcSizePerPacket;
encoderData.pktDescs = NULL;
encoderData.ringBuffer = _ringBuffer;
_afio = encoderData;
_afio->pktDescs = NULL;
_afio->ringBuffer = _ringBuffer;
return YES;
}
- (void)disposeConverter
{
if (_encoder) {
AudioConverterDispose(_encoder);
_encoder = NULL;
}
if (_ringBuffer) {
sfifo_close(_ringBuffer);
free(_ringBuffer);
_ringBuffer = NULL;
}
if (_afio) {
free(_afio);
_afio = NULL;
}
if (_outputLayout) {
free(_outputLayout);
_outputLayout = NULL;
}
if (_inputLayout) {
free(_inputLayout);
_inputLayout = NULL;
}
}
- (BOOL)createMagicCookie
{
OSStatus err;
@@ -244,6 +283,24 @@ typedef struct AudioFileIO
#pragma mark - Public methods
- (void)reconfigure
{
[self disposeConverter];
_inputFormat = _inputUnit.outputFormat;
_inputLayoutSize = _inputUnit.outputLayoutSize;
_inputLayout = malloc(_inputLayoutSize);
memcpy(_inputLayout, _inputUnit.outputLayout, _inputLayoutSize);
if (![self initConverterWithBitRate:_bitrate]) {
return;
}
if (![self createMagicCookie]) {
return;
}
}
- (NSData *)magicCookie
{
return _magicCookie;
@@ -360,7 +417,7 @@ static MP42SampleBuffer *encode(AudioConverterRef encoder, AudioFileIO *afio)
sfifo_write(_ringBuffer, sampleBuffer->data, sampleBuffer->size);
}
while ((outSample = encode(_encoder, &_afio))) {
while ((outSample = encode(_encoder, _afio))) {
if (_outputType == MP42AudioUnitOutputPush) {
[_outputUnit addSample:outSample];
}
+1
View File
@@ -14,6 +14,7 @@
@property(nonatomic, readwrite) float volume;
@property(nonatomic, readwrite) UInt32 channels;
@property(nonatomic, readwrite) UInt32 sourceChannels;
@property(nonatomic, readwrite) UInt32 channelLayoutTag;
@property(nonatomic, readonly) MP42TrackId fallbackTrackId;
+11
View File
@@ -11,6 +11,8 @@
#import "MP42ConverterProtocol.h"
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSUInteger, MP42AudioUnitOutput) {
MP42AudioUnitOutputPush,
MP42AudioUnitOutputPull,
@@ -18,10 +20,19 @@ typedef NS_ENUM(NSUInteger, MP42AudioUnitOutput) {
@protocol MP42AudioUnit <MP42ConverterProtocol>
- (void)reconfigure;
@property (nonatomic, readwrite) MP42AudioUnitOutput outputType;
@property (nonatomic, readwrite, unsafe_unretained) id<MP42ConverterProtocol> outputUnit;
@property (nonatomic, readonly, nullable) AudioChannelLayout *inputLayout;
@property (nonatomic, readonly) UInt32 inputLayoutSize;
@property (nonatomic, readonly) AudioStreamBasicDescription inputFormat;
@property (nonatomic, readonly, nullable) AudioChannelLayout *outputLayout;
@property (nonatomic, readonly) UInt32 outputLayoutSize;
@property (nonatomic, readonly) AudioStreamBasicDescription outputFormat;
@end
NS_ASSUME_NONNULL_END