From ae0c27b8a81eebb8739ea7cb0cef4d558e2e0b89 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Mon, 12 Sep 2016 17:31:51 +0200 Subject: [PATCH] Some work on PCM. --- MP42/FFmpegUtils.m | 18 +++++++++++++++++- MP42/MP42AVFImporter.h | 2 -- MP42/MP42AVFImporter.m | 25 ++++++++++++++++++++----- MP42/MP42AudioDecoder.m | 31 ++++++++++++++++++++++++------- 4 files changed, 61 insertions(+), 15 deletions(-) diff --git a/MP42/FFmpegUtils.m b/MP42/FFmpegUtils.m index eeff13f..7c24655 100644 --- a/MP42/FFmpegUtils.m +++ b/MP42/FFmpegUtils.m @@ -65,6 +65,22 @@ void FFInitFFmpeg() REGISTER_DECODER(dca); REGISTER_DECODER(truehd); REGISTER_DECODER(mlp); + REGISTER_DECODER(pcm_f32be); + REGISTER_DECODER(pcm_f32le); + REGISTER_DECODER(pcm_f64be); + REGISTER_DECODER(pcm_f64le); + REGISTER_DECODER(pcm_s16be); + REGISTER_DECODER(pcm_s16le); + REGISTER_DECODER(pcm_s16le_planar); + REGISTER_DECODER(pcm_s24be); + REGISTER_DECODER(pcm_s24le); + REGISTER_DECODER(pcm_s32be); + REGISTER_DECODER(pcm_s32le); + REGISTER_DECODER(pcm_s8); + REGISTER_DECODER(pcm_u16be); + REGISTER_DECODER(pcm_u16le); + REGISTER_DECODER(pcm_u24be); + REGISTER_DECODER(pcm_u24le); }); } @@ -88,7 +104,7 @@ static const struct { { 'DTS ', AV_CODEC_ID_DTS }, { 'trhd', AV_CODEC_ID_TRUEHD }, { 'mlp ', AV_CODEC_ID_MLP }, - { kAudioFormatLinearPCM, AV_CODEC_ID_PCM_S16LE }, + { kAudioFormatLinearPCM, AV_CODEC_ID_PCM_S16BE }, { kAudioFormatLinearPCM, AV_CODEC_ID_PCM_U8 }, { kAudioFormatALaw, AV_CODEC_ID_PCM_ALAW }, { kAudioFormatULaw, AV_CODEC_ID_PCM_MULAW }, diff --git a/MP42/MP42AVFImporter.h b/MP42/MP42AVFImporter.h index a07e273..cc7e6ad 100644 --- a/MP42/MP42AVFImporter.h +++ b/MP42/MP42AVFImporter.h @@ -9,8 +9,6 @@ #import #import "MP42FileImporter.h" -@class AVAsset; - @interface MP42AVFImporter : MP42FileImporter @end diff --git a/MP42/MP42AVFImporter.m b/MP42/MP42AVFImporter.m index d60fd57..28598a4 100644 --- a/MP42/MP42AVFImporter.m +++ b/MP42/MP42AVFImporter.m @@ -25,8 +25,8 @@ @interface AVFDemuxHelper : NSObject { @public - int64_t currentTime; - int64_t minDisplayOffset; + int64_t currentTime; + int64_t minDisplayOffset; AVAssetReaderOutput *assetReaderOutput; MP42EditListsReconstructor *editsConstructor; @@ -49,7 +49,7 @@ } + (NSArray *)supportedFileFormats { - return @[@"mov", @"mp4", @"m4v", @"m4a", @"m2ts", @"ts", @"mts", @"ac3", @"eac3", @"ec3", @"webvtt", @"vtt"]; + return @[@"mov", @"mp4", @"m4v", @"m4a", @"m2ts", @"ts", @"mts", @"ac3", @"eac3", @"ec3", @"webvtt", @"vtt", @"caf", @"aif", @"aiff"]; } - (FourCharCode)formatForTrack:(AVAssetTrack *)track { @@ -725,6 +725,22 @@ return nil; } +- (AudioStreamBasicDescription)audioDescriptionForTrack:(MP42AudioTrack *)track +{ + AudioStreamBasicDescription result; + bzero(&result, sizeof(AudioStreamBasicDescription)); + + AVAssetTrack *assetTrack = [_localAsset trackWithTrackID:track.sourceId]; + CMFormatDescriptionRef formatDescription = (CMFormatDescriptionRef)assetTrack.formatDescriptions.firstObject; + + if (formatDescription) { + const AudioStreamBasicDescription *asbd = CMAudioFormatDescriptionGetStreamBasicDescription(formatDescription); + memcpy(&result, asbd, sizeof(AudioStreamBasicDescription)); + } + + return result; +} + - (void)demux { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; @@ -754,7 +770,7 @@ demuxHelper->assetReaderOutput = assetReaderOutput; demuxHelper->editsConstructor = [[MP42EditListsReconstructor alloc] initWithMediaFormat:track.format]; - totalDataLength += [track dataLength]; + totalDataLength += track.dataLength; } } @@ -936,7 +952,6 @@ if ([dict valueForKey:(NSString*)kCMSampleAttachmentKey_DoNotDisplay]) { doNotDisplay = YES; } - } } diff --git a/MP42/MP42AudioDecoder.m b/MP42/MP42AudioDecoder.m index 2c0de3d..e61591d 100644 --- a/MP42/MP42AudioDecoder.m +++ b/MP42/MP42AudioDecoder.m @@ -131,6 +131,12 @@ typedef struct MP42DecodeContext MP42DecodeContext; av_dict_set(&av_opts, "drc_scale", drc_scale, 0); } + if (asbd.mFormatID == kAudioFormatLinearPCM) + { + _avctx->channels = asbd.mChannelsPerFrame; + _avctx->sample_rate = asbd.mSampleRate; + } + if (avcodec_open2(_avctx, _codec, &av_opts)) { NSLog(@"Error opening audio decoder"); av_freep(&_avctx); @@ -271,6 +277,11 @@ static void configureDescriptors(MP42DecodeContext *context, AVFrame *frame) int nb_channels = av_get_channel_layout_nb_channels(frame->channel_layout); int sample_rate = frame->sample_rate; + // FIXME PCM + if (nb_channels == 0) { + nb_channels = context->inputFormat->mChannelsPerFrame; + } + // Reset the channels per frame and sample rate if (context->inputFormat->mChannelsPerFrame == context->outputFormat->mChannelsPerFrame) { context->outputFormat->mChannelsPerFrame = nb_channels; @@ -296,7 +307,7 @@ static void configureDescriptors(MP42DecodeContext *context, AVFrame *frame) context->out_layout = AV_CH_LAYOUT_MONO; } } - else { + else if (frame->channel_layout) { context->out_layout = frame->channel_layout; } @@ -352,12 +363,18 @@ static int resample(MP42DecodeContext *context, AVFrame *frame, uint8_t **output center_mix_level, downmix_info->lfe_mix_level); } - hb_audio_resample_set_channel_layout(context->resampler, - frame->channel_layout); - hb_audio_resample_set_sample_rate(context->resampler, - frame->sample_rate); - hb_audio_resample_set_sample_fmt(context->resampler, - frame->format); + if (frame->channel_layout) { + hb_audio_resample_set_channel_layout(context->resampler, + frame->channel_layout); + } + if (frame->sample_rate) { + hb_audio_resample_set_sample_rate(context->resampler, + frame->sample_rate); + } + if (frame->format) { + hb_audio_resample_set_sample_fmt(context->resampler, + frame->format); + } if (hb_audio_resample_update(context->resampler)) { NSLog(@"decavcodec: hb_audio_resample_update() failed");