Add support for dropping the encoder delay samples in the audio decoder.
This commit is contained in:
1 parent
f47229d1a6
commit
d336067aee
3 files changed
+52
-1
No files matched your search
@@ -38,15 +38,19 @@
|
|||||||
if (self) {
|
if (self) {
|
||||||
NSData *magicCookie = [track.muxer_helper->importer magicCookieForTrack:track];
|
NSData *magicCookie = [track.muxer_helper->importer magicCookieForTrack:track];
|
||||||
AudioStreamBasicDescription asbd = [self basicDescriptorForTrack:track];
|
AudioStreamBasicDescription asbd = [self basicDescriptorForTrack:track];
|
||||||
|
UInt32 initialPadding = [self initialPaddingForTrack:track];
|
||||||
UInt32 channelLayoutSize = sizeof(AudioChannelLayout);
|
UInt32 channelLayoutSize = sizeof(AudioChannelLayout);
|
||||||
AudioChannelLayout *channelLayout = calloc(channelLayoutSize, 1);
|
AudioChannelLayout *channelLayout = calloc(channelLayoutSize, 1);
|
||||||
channelLayout->mChannelLayoutTag = track.channelLayoutTag;
|
channelLayout->mChannelLayoutTag = track.channelLayoutTag;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
_decoder = [[MP42AudioDecoder alloc] initWithAudioFormat:asbd
|
_decoder = [[MP42AudioDecoder alloc] initWithAudioFormat:asbd
|
||||||
channelLayout:channelLayout
|
channelLayout:channelLayout
|
||||||
channelLayoutSize:channelLayoutSize
|
channelLayoutSize:channelLayoutSize
|
||||||
mixdownType:settings.mixDown
|
mixdownType:settings.mixDown
|
||||||
drc:settings.drc
|
drc:settings.drc
|
||||||
|
initialPadding:initialPadding
|
||||||
magicCookie:magicCookie error:error];
|
magicCookie:magicCookie error:error];
|
||||||
|
|
||||||
free(channelLayout);
|
free(channelLayout);
|
||||||
@@ -76,6 +80,19 @@
|
|||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (UInt32)initialPaddingForTrack:(MP42AudioTrack *)track
|
||||||
|
{
|
||||||
|
UInt32 initialPadding = 0;
|
||||||
|
if (track.format == kMP42AudioCodecType_MPEG4AAC) {
|
||||||
|
initialPadding = 2112;
|
||||||
|
}
|
||||||
|
else if (track.format == kMP42AudioCodecType_AC3) {
|
||||||
|
initialPadding = 256;
|
||||||
|
}
|
||||||
|
|
||||||
|
return initialPadding;
|
||||||
|
}
|
||||||
|
|
||||||
- (AudioStreamBasicDescription)basicDescriptorForTrack:(MP42AudioTrack *)track
|
- (AudioStreamBasicDescription)basicDescriptorForTrack:(MP42AudioTrack *)track
|
||||||
{
|
{
|
||||||
AudioStreamBasicDescription asbd;
|
AudioStreamBasicDescription asbd;
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
channelLayoutSize:(UInt32)channelLayoutSize
|
channelLayoutSize:(UInt32)channelLayoutSize
|
||||||
mixdownType:(NSString *)mixdownType
|
mixdownType:(NSString *)mixdownType
|
||||||
drc:(float)drc
|
drc:(float)drc
|
||||||
|
initialPadding:(UInt32)initialPadding
|
||||||
magicCookie:(NSData *)magicCookie
|
magicCookie:(NSData *)magicCookie
|
||||||
error:(NSError **)error;
|
error:(NSError **)error;
|
||||||
|
|
||||||
|
|||||||
+34
-1
@@ -35,6 +35,8 @@ struct MP42DecodeContext {
|
|||||||
enum AVMatrixEncoding matrix_encoding;
|
enum AVMatrixEncoding matrix_encoding;
|
||||||
int out_layout;
|
int out_layout;
|
||||||
|
|
||||||
|
int drop_samples;
|
||||||
|
|
||||||
BOOL configured;
|
BOOL configured;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -67,6 +69,7 @@ typedef struct MP42DecodeContext MP42DecodeContext;
|
|||||||
channelLayoutSize:(UInt32)channelLayoutSize
|
channelLayoutSize:(UInt32)channelLayoutSize
|
||||||
mixdownType:(NSString *)mixdownType
|
mixdownType:(NSString *)mixdownType
|
||||||
drc:(float)drc
|
drc:(float)drc
|
||||||
|
initialPadding:(UInt32)initialPadding
|
||||||
magicCookie:(NSData *)magicCookie
|
magicCookie:(NSData *)magicCookie
|
||||||
error:(NSError **)error
|
error:(NSError **)error
|
||||||
{
|
{
|
||||||
@@ -178,6 +181,11 @@ typedef struct MP42DecodeContext MP42DecodeContext;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int drop_samples = 0;
|
||||||
|
if (initialPadding > 0) {
|
||||||
|
drop_samples = initialPadding;
|
||||||
|
}
|
||||||
|
|
||||||
// Creates the output audio stream basic description.
|
// Creates the output audio stream basic description.
|
||||||
// It will be used to configure the next audio unit in the chain.
|
// It will be used to configure the next audio unit in the chain.
|
||||||
AudioStreamBasicDescription outputFormat;
|
AudioStreamBasicDescription outputFormat;
|
||||||
@@ -205,6 +213,7 @@ typedef struct MP42DecodeContext MP42DecodeContext;
|
|||||||
_context->outputLayout = &_outputLayout;
|
_context->outputLayout = &_outputLayout;
|
||||||
_context->outputLayoutSize = &_outputLayoutSize;
|
_context->outputLayoutSize = &_outputLayoutSize;
|
||||||
_context->matrix_encoding = matrix_encoding;
|
_context->matrix_encoding = matrix_encoding;
|
||||||
|
_context->drop_samples = drop_samples;
|
||||||
_context->configured = NO;
|
_context->configured = NO;
|
||||||
|
|
||||||
// Init the FIFOs
|
// Init the FIFOs
|
||||||
@@ -433,6 +442,27 @@ static int resample(MP42DecodeContext *context, AVFrame *frame, uint8_t **output
|
|||||||
output_data, output_data_size);
|
output_data, output_data_size);
|
||||||
|
|
||||||
|
|
||||||
|
if (ret == 0 && context->drop_samples > 0)
|
||||||
|
{
|
||||||
|
// drop audio samples that are part of the encoder delay
|
||||||
|
int channels = context->outputFormat->mChannelsPerFrame;
|
||||||
|
int sample_size = channels * sizeof(float);
|
||||||
|
int samples = *output_data_size / sample_size;
|
||||||
|
if (samples <= context->drop_samples)
|
||||||
|
{
|
||||||
|
*output_data_size = 0;
|
||||||
|
free(*output_data);
|
||||||
|
context->drop_samples -= samples;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int size = context->drop_samples * sample_size;
|
||||||
|
memmove(*output_data, *output_data + size, *output_data_size - size);
|
||||||
|
*output_data_size -= size;
|
||||||
|
context->drop_samples = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -463,9 +493,12 @@ static int receive_frame(MP42DecodeContext *context, MP42SampleBuffer **outSampl
|
|||||||
uint8_t *output_data = NULL;
|
uint8_t *output_data = NULL;
|
||||||
int output_data_size = 0;
|
int output_data_size = 0;
|
||||||
ret = resample(context, frame, &output_data, &output_data_size);
|
ret = resample(context, frame, &output_data, &output_data_size);
|
||||||
if (!ret) {
|
if (!ret && output_data_size) {
|
||||||
*outSample = sampleBufferFromFrame(output_data, output_data_size);
|
*outSample = sampleBufferFromFrame(output_data, output_data_size);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
*outSample = nil;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
av_frame_free(&frame);
|
av_frame_free(&frame);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user