Set the right channels number and layout in the encoder.
This commit is contained in:
1 parent
1b0316237c
commit
ffe8abd0b6
8 files changed
+232
-57
No files matched your search
@@ -10,10 +10,15 @@
|
|||||||
#define FFmpegUtils_h
|
#define FFmpegUtils_h
|
||||||
|
|
||||||
#include <Foundation/Foundation.h>
|
#include <Foundation/Foundation.h>
|
||||||
|
#include <CoreAudio/CoreAudio.h>
|
||||||
#include <avcodec.h>
|
#include <avcodec.h>
|
||||||
|
|
||||||
void FFInitFFmpeg();
|
void FFInitFFmpeg();
|
||||||
|
|
||||||
enum AVCodecID FourCCToCodecID(OSType formatID);
|
enum AVCodecID FourCCToCodecID(OSType formatID);
|
||||||
OSType CodecIDToFourCC(enum AVCodecID codecID);
|
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 */
|
#endif /* FFmpegUtils_h */
|
||||||
@@ -112,3 +112,49 @@ OSType OSTypeFCodecIDToFourCC(enum AVCodecID codecID)
|
|||||||
}
|
}
|
||||||
return AV_CODEC_ID_NONE;
|
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;
|
||||||
|
}
|
||||||
@@ -19,10 +19,13 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
|
|
||||||
- (instancetype)initWithAudioFormat:(AudioStreamBasicDescription)asbd mixdownType:(NSString *)mixdownType magicCookie:(NSData *)magicCookie error:(NSError **)error;
|
- (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 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
|
@end
|
||||||
|
|
||||||
|
|||||||
+71
-23
@@ -23,11 +23,18 @@ struct MP42DecodeContext {
|
|||||||
AVCodecContext *avctx;
|
AVCodecContext *avctx;
|
||||||
AVAudioResampleContext *resampler;
|
AVAudioResampleContext *resampler;
|
||||||
|
|
||||||
AudioStreamBasicDescription inputFormat;
|
AudioChannelLayout **inputLayout;
|
||||||
AudioStreamBasicDescription outputFormat;
|
UInt32 *inputLayoutSize;
|
||||||
|
AudioStreamBasicDescription *inputFormat;
|
||||||
|
|
||||||
|
AudioChannelLayout **outputLayout;
|
||||||
|
UInt32 *outputLayoutSize;
|
||||||
|
AudioStreamBasicDescription *outputFormat;
|
||||||
|
|
||||||
enum AVMatrixEncoding matrix_encoding;
|
enum AVMatrixEncoding matrix_encoding;
|
||||||
enum AVSampleFormat out_sample_format;
|
enum AVSampleFormat out_sample_format;
|
||||||
|
|
||||||
|
BOOL reconfigured;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct MP42DecodeContext MP42DecodeContext;
|
typedef struct MP42DecodeContext MP42DecodeContext;
|
||||||
@@ -133,11 +140,18 @@ typedef struct MP42DecodeContext MP42DecodeContext;
|
|||||||
_context = malloc(sizeof(MP42DecodeContext));
|
_context = malloc(sizeof(MP42DecodeContext));
|
||||||
_context->avctx = _avctx;
|
_context->avctx = _avctx;
|
||||||
_context->resampler = NULL;
|
_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->matrix_encoding = matrix_encoding;
|
||||||
_context->out_sample_format = AV_SAMPLE_FMT_FLT;
|
_context->out_sample_format = AV_SAMPLE_FMT_FLT;
|
||||||
|
|
||||||
|
_context->reconfigured = NO;
|
||||||
|
|
||||||
// Init the FIFOs
|
// Init the FIFOs
|
||||||
_inputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
|
_inputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
|
||||||
_outputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
|
_outputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
|
||||||
@@ -161,6 +175,9 @@ typedef struct MP42DecodeContext MP42DecodeContext;
|
|||||||
avresample_free(&_context->resampler);
|
avresample_free(&_context->resampler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(_inputLayout);
|
||||||
|
free(_outputLayout);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)start
|
- (void)start
|
||||||
@@ -172,6 +189,11 @@ typedef struct MP42DecodeContext MP42DecodeContext;
|
|||||||
|
|
||||||
#pragma mark - Public methods
|
#pragma mark - Public methods
|
||||||
|
|
||||||
|
- (void)reconfigure
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)addSample:(MP42SampleBuffer *)sample
|
- (void)addSample:(MP42SampleBuffer *)sample
|
||||||
{
|
{
|
||||||
[_inputSamplesBuffer enqueue:sample];
|
[_inputSamplesBuffer enqueue:sample];
|
||||||
@@ -210,32 +232,54 @@ static int initResampler(MP42DecodeContext *context, AVFrame *frame)
|
|||||||
int ret = 0;
|
int ret = 0;
|
||||||
AVAudioResampleContext *resampler;
|
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
|
// We want float interleaved, FFmpeg usually prefers
|
||||||
// float planar, so we need to convert it.
|
// float planar, so we need to convert it.
|
||||||
|
|
||||||
if (context->avctx->sample_fmt != context->out_sample_format ||
|
|
||||||
context->outputFormat.mChannelsPerFrame != context->inputFormat.mChannelsPerFrame) {
|
|
||||||
|
|
||||||
resampler = avresample_alloc_context();
|
resampler = avresample_alloc_context();
|
||||||
if (!resampler) {
|
if (!resampler) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
av_opt_set_int(resampler, "in_channel_layout", frame->channel_layout, 0);
|
av_opt_set_int(resampler, "in_channel_layout", frame->channel_layout, 0);
|
||||||
if (context->outputFormat.mChannelsPerFrame < context->inputFormat.mChannelsPerFrame) {
|
av_opt_set_int(resampler, "out_channel_layout", layout, 0);
|
||||||
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_rate", context->inputFormat.mSampleRate, 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);
|
av_opt_set_int(resampler, "out_sample_rate", context->inputFormat->mSampleRate, 0);
|
||||||
|
|
||||||
av_opt_set_int(resampler, "in_sample_fmt", context->avctx->sample_fmt, 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, "out_sample_fmt", context->out_sample_format, 0);
|
||||||
@@ -252,7 +296,6 @@ static int initResampler(MP42DecodeContext *context, AVFrame *frame)
|
|||||||
else {
|
else {
|
||||||
context->resampler = resampler;
|
context->resampler = resampler;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
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_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;
|
int in_plane_size, out_plane_size = 0;
|
||||||
|
|
||||||
@@ -343,6 +386,11 @@ static int decode(MP42DecodeContext *context, MP42SampleBuffer *inSample, MP42Sa
|
|||||||
decode(_context, sampleBuffer, &outSample);
|
decode(_context, sampleBuffer, &outSample);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_context->reconfigured) {
|
||||||
|
[_outputUnit reconfigure];
|
||||||
|
_context->reconfigured = NO;
|
||||||
|
}
|
||||||
|
|
||||||
if (outSample) {
|
if (outSample) {
|
||||||
if (_outputType == MP42AudioUnitOutputPush) {
|
if (_outputType == MP42AudioUnitOutputPush) {
|
||||||
[_outputUnit addSample:outSample];
|
[_outputUnit addSample:outSample];
|
||||||
|
|||||||
@@ -18,11 +18,15 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
|
|
||||||
- (instancetype)initWithInputUnit:(id<MP42AudioUnit>)unit bitRate:(NSUInteger)bitRate error:(NSError **)error;
|
- (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 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
|
@end
|
||||||
|
|
||||||
|
|||||||
+86
-29
@@ -41,13 +41,14 @@ typedef struct AudioFileIO
|
|||||||
}
|
}
|
||||||
|
|
||||||
@property (nonatomic, readonly) AudioConverterRef encoder;
|
@property (nonatomic, readonly) AudioConverterRef encoder;
|
||||||
|
@property (nonatomic, readonly) NSUInteger bitrate;
|
||||||
|
|
||||||
@property (nonatomic, readonly) NSThread *decoderThread;
|
@property (nonatomic, readonly) NSThread *decoderThread;
|
||||||
@property (nonatomic, readonly) MP42Fifo<MP42SampleBuffer *> *inputSamplesBuffer;
|
@property (nonatomic, readonly) MP42Fifo<MP42SampleBuffer *> *inputSamplesBuffer;
|
||||||
@property (nonatomic, readonly) MP42Fifo<MP42SampleBuffer *> *outputSamplesBuffer;
|
@property (nonatomic, readonly) MP42Fifo<MP42SampleBuffer *> *outputSamplesBuffer;
|
||||||
|
|
||||||
@property (nonatomic, readonly) sfifo_t *ringBuffer;
|
@property (nonatomic, readonly) sfifo_t *ringBuffer;
|
||||||
@property (nonatomic, readonly) AudioFileIO afio;
|
@property (nonatomic, readonly) AudioFileIO *afio;
|
||||||
|
|
||||||
@property (nonatomic, readonly, unsafe_unretained) id<MP42AudioUnit> inputUnit;
|
@property (nonatomic, readonly, unsafe_unretained) id<MP42AudioUnit> inputUnit;
|
||||||
|
|
||||||
@@ -68,13 +69,7 @@ typedef struct AudioFileIO
|
|||||||
_inputUnit.outputUnit = self;
|
_inputUnit.outputUnit = self;
|
||||||
_inputFormat = unit.outputFormat;
|
_inputFormat = unit.outputFormat;
|
||||||
|
|
||||||
if (![self initConverterWithBitRate:bitRate]) {
|
_bitrate = bitRate;
|
||||||
return nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (![self createMagicCookie]) {
|
|
||||||
return nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
_inputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
|
_inputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
|
||||||
_outputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
|
_outputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
|
||||||
@@ -86,13 +81,7 @@ typedef struct AudioFileIO
|
|||||||
|
|
||||||
- (void)dealloc
|
- (void)dealloc
|
||||||
{
|
{
|
||||||
if (_encoder) {
|
[self disposeConverter];
|
||||||
AudioConverterDispose(_encoder);
|
|
||||||
}
|
|
||||||
if (_ringBuffer) {
|
|
||||||
sfifo_close(_ringBuffer);
|
|
||||||
free(_ringBuffer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - Encoder Init
|
#pragma mark - Encoder Init
|
||||||
@@ -144,7 +133,7 @@ typedef struct AudioFileIO
|
|||||||
if (err) {
|
if (err) {
|
||||||
NSLog(@"err: kAudioConverterApplicableEncodeBitRates From AudioConverter");
|
NSLog(@"err: kAudioConverterApplicableEncodeBitRates From AudioConverter");
|
||||||
}
|
}
|
||||||
bitrateCounts = tmpsiz / sizeof( AudioValueRange );
|
bitrateCounts = tmpsiz / sizeof(AudioValueRange);
|
||||||
|
|
||||||
// Set bitrate.
|
// Set bitrate.
|
||||||
tmp = bitrate * outputFormat.mChannelsPerFrame * 1000;
|
tmp = bitrate * outputFormat.mChannelsPerFrame * 1000;
|
||||||
@@ -157,7 +146,31 @@ typedef struct AudioFileIO
|
|||||||
free(bitrates);
|
free(bitrates);
|
||||||
|
|
||||||
AudioConverterSetProperty(_encoder, kAudioConverterEncodeBitRate,
|
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.
|
// Get real input.
|
||||||
tmpsiz = sizeof(_inputFormat);
|
tmpsiz = sizeof(_inputFormat);
|
||||||
@@ -186,24 +199,50 @@ typedef struct AudioFileIO
|
|||||||
sfifo_init(_ringBuffer, ringbuffer_len);
|
sfifo_init(_ringBuffer, ringbuffer_len);
|
||||||
|
|
||||||
// Set up buffers and data proc info struct
|
// Set up buffers and data proc info struct
|
||||||
struct AudioFileIO encoderData;
|
_afio = malloc(sizeof(AudioFileIO));
|
||||||
encoderData.srcBufferSize = 32768;
|
_afio->srcBufferSize = 32768;
|
||||||
encoderData.srcBuffer = (char *) malloc(encoderData.srcBufferSize);
|
_afio->srcBuffer = (char *) malloc(_afio->srcBufferSize);
|
||||||
|
|
||||||
encoderData.outputMaxSize = outputSizePerPacket;
|
_afio->outputMaxSize = outputSizePerPacket;
|
||||||
|
|
||||||
encoderData.srcSizePerPacket = _inputFormat.mBytesPerPacket;
|
_afio->srcSizePerPacket = _inputFormat.mBytesPerPacket;
|
||||||
encoderData.channelsPerFrame = _inputFormat.mChannelsPerFrame;
|
_afio->channelsPerFrame = _inputFormat.mChannelsPerFrame;
|
||||||
encoderData.numPacketsPerRead = encoderData.srcBufferSize / encoderData.srcSizePerPacket;
|
_afio->numPacketsPerRead = _afio->srcBufferSize / _afio->srcSizePerPacket;
|
||||||
|
|
||||||
encoderData.pktDescs = NULL;
|
_afio->pktDescs = NULL;
|
||||||
encoderData.ringBuffer = _ringBuffer;
|
_afio->ringBuffer = _ringBuffer;
|
||||||
|
|
||||||
_afio = encoderData;
|
|
||||||
|
|
||||||
return YES;
|
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
|
- (BOOL)createMagicCookie
|
||||||
{
|
{
|
||||||
OSStatus err;
|
OSStatus err;
|
||||||
@@ -244,6 +283,24 @@ typedef struct AudioFileIO
|
|||||||
|
|
||||||
#pragma mark - Public methods
|
#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
|
- (NSData *)magicCookie
|
||||||
{
|
{
|
||||||
return _magicCookie;
|
return _magicCookie;
|
||||||
@@ -360,7 +417,7 @@ static MP42SampleBuffer *encode(AudioConverterRef encoder, AudioFileIO *afio)
|
|||||||
sfifo_write(_ringBuffer, sampleBuffer->data, sampleBuffer->size);
|
sfifo_write(_ringBuffer, sampleBuffer->data, sampleBuffer->size);
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((outSample = encode(_encoder, &_afio))) {
|
while ((outSample = encode(_encoder, _afio))) {
|
||||||
if (_outputType == MP42AudioUnitOutputPush) {
|
if (_outputType == MP42AudioUnitOutputPush) {
|
||||||
[_outputUnit addSample:outSample];
|
[_outputUnit addSample:outSample];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
@property(nonatomic, readwrite) float volume;
|
@property(nonatomic, readwrite) float volume;
|
||||||
@property(nonatomic, readwrite) UInt32 channels;
|
@property(nonatomic, readwrite) UInt32 channels;
|
||||||
@property(nonatomic, readwrite) UInt32 sourceChannels;
|
@property(nonatomic, readwrite) UInt32 sourceChannels;
|
||||||
|
|
||||||
@property(nonatomic, readwrite) UInt32 channelLayoutTag;
|
@property(nonatomic, readwrite) UInt32 channelLayoutTag;
|
||||||
|
|
||||||
@property(nonatomic, readonly) MP42TrackId fallbackTrackId;
|
@property(nonatomic, readonly) MP42TrackId fallbackTrackId;
|
||||||
|
|||||||
@@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
#import "MP42ConverterProtocol.h"
|
#import "MP42ConverterProtocol.h"
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
typedef NS_ENUM(NSUInteger, MP42AudioUnitOutput) {
|
typedef NS_ENUM(NSUInteger, MP42AudioUnitOutput) {
|
||||||
MP42AudioUnitOutputPush,
|
MP42AudioUnitOutputPush,
|
||||||
MP42AudioUnitOutputPull,
|
MP42AudioUnitOutputPull,
|
||||||
@@ -18,10 +20,19 @@ typedef NS_ENUM(NSUInteger, MP42AudioUnitOutput) {
|
|||||||
|
|
||||||
@protocol MP42AudioUnit <MP42ConverterProtocol>
|
@protocol MP42AudioUnit <MP42ConverterProtocol>
|
||||||
|
|
||||||
|
- (void)reconfigure;
|
||||||
|
|
||||||
@property (nonatomic, readwrite) MP42AudioUnitOutput outputType;
|
@property (nonatomic, readwrite) MP42AudioUnitOutput outputType;
|
||||||
@property (nonatomic, readwrite, unsafe_unretained) id<MP42ConverterProtocol> outputUnit;
|
@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) AudioStreamBasicDescription inputFormat;
|
||||||
|
|
||||||
|
@property (nonatomic, readonly, nullable) AudioChannelLayout *outputLayout;
|
||||||
|
@property (nonatomic, readonly) UInt32 outputLayoutSize;
|
||||||
@property (nonatomic, readonly) AudioStreamBasicDescription outputFormat;
|
@property (nonatomic, readonly) AudioStreamBasicDescription outputFormat;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
||||||
Reference in new issue
Block a user