Some work on encoding PCM audio.
This commit is contained in:
1 parent
acf14c20f5
commit
c9b59d96f0
5 files changed
+239
-33
No files matched your search
+2
-1
@@ -15,10 +15,11 @@
|
||||
|
||||
void FFInitFFmpeg();
|
||||
|
||||
enum AVCodecID ASBDToCodecID(AudioStreamBasicDescription asbd);
|
||||
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);
|
||||
int convert_layout_to_av(AudioChannelLayout *layout, UInt32 layoutSize);
|
||||
|
||||
#endif /* FFmpegUtils_h */
|
||||
+152
-2
@@ -77,6 +77,7 @@ void FFInitFFmpeg()
|
||||
REGISTER_DECODER(pcm_s32be);
|
||||
REGISTER_DECODER(pcm_s32le);
|
||||
REGISTER_DECODER(pcm_s8);
|
||||
REGISTER_DECODER(pcm_u8);
|
||||
REGISTER_DECODER(pcm_u16be);
|
||||
REGISTER_DECODER(pcm_u16le);
|
||||
REGISTER_DECODER(pcm_u24be);
|
||||
@@ -104,13 +105,58 @@ static const struct {
|
||||
{ 'DTS ', AV_CODEC_ID_DTS },
|
||||
{ 'trhd', AV_CODEC_ID_TRUEHD },
|
||||
{ 'mlp ', AV_CODEC_ID_MLP },
|
||||
{ kAudioFormatLinearPCM, AV_CODEC_ID_PCM_S16BE },
|
||||
{ kAudioFormatLinearPCM, AV_CODEC_ID_PCM_U8 },
|
||||
{ kAudioFormatALaw, AV_CODEC_ID_PCM_ALAW },
|
||||
{ kAudioFormatULaw, AV_CODEC_ID_PCM_MULAW },
|
||||
{0, AV_CODEC_ID_NONE }
|
||||
};
|
||||
|
||||
// List of codec IDs we know about and that map to audio fourccs
|
||||
static const struct {
|
||||
AudioFormatFlags mFormatFlags;
|
||||
UInt32 mBitsPerChannel;
|
||||
enum AVCodecID codecID;
|
||||
} kAudioPCMCodecMap[] =
|
||||
{
|
||||
{ kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked, 16, AV_CODEC_ID_PCM_S16LE },
|
||||
{ kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked | kAudioFormatFlagIsBigEndian, 16, AV_CODEC_ID_PCM_S16BE },
|
||||
{ kLinearPCMFormatFlagIsPacked, 16, AV_CODEC_ID_PCM_U16LE },
|
||||
{ kLinearPCMFormatFlagIsPacked | kAudioFormatFlagIsBigEndian, 16, AV_CODEC_ID_PCM_U16BE },
|
||||
{ kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked | kAudioFormatFlagIsBigEndian, 8, AV_CODEC_ID_PCM_S8 },
|
||||
{ kLinearPCMFormatFlagIsPacked | kAudioFormatFlagIsBigEndian, 8, AV_CODEC_ID_PCM_U8 },
|
||||
{ kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked, 32, AV_CODEC_ID_PCM_S32LE },
|
||||
{ kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked | kAudioFormatFlagIsBigEndian, 32, AV_CODEC_ID_PCM_S32BE },
|
||||
{ kLinearPCMFormatFlagIsPacked, 32, AV_CODEC_ID_PCM_U32LE },
|
||||
{ kLinearPCMFormatFlagIsPacked | kAudioFormatFlagIsBigEndian, 32, AV_CODEC_ID_PCM_U32BE },
|
||||
{ kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked, 24, AV_CODEC_ID_PCM_S24LE },
|
||||
{ kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked | kAudioFormatFlagIsBigEndian, 24, AV_CODEC_ID_PCM_S24BE },
|
||||
{ kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked | kAudioFormatFlagIsNonInterleaved, 16, AV_CODEC_ID_PCM_S16LE_PLANAR },
|
||||
{ kLinearPCMFormatFlagIsFloat | kLinearPCMFormatFlagIsPacked | kAudioFormatFlagIsBigEndian, 32, AV_CODEC_ID_PCM_F32BE },
|
||||
{ kLinearPCMFormatFlagIsFloat | kLinearPCMFormatFlagIsPacked, 32, AV_CODEC_ID_PCM_F32LE },
|
||||
{ kLinearPCMFormatFlagIsFloat | kLinearPCMFormatFlagIsPacked | kAudioFormatFlagIsBigEndian, 64, AV_CODEC_ID_PCM_F64BE },
|
||||
{ kLinearPCMFormatFlagIsFloat | kLinearPCMFormatFlagIsPacked, 64, AV_CODEC_ID_PCM_F64LE },
|
||||
{0, AV_CODEC_ID_NONE }
|
||||
};
|
||||
|
||||
enum AVCodecID ASBDToCodecID(AudioStreamBasicDescription asbd)
|
||||
{
|
||||
if (asbd.mFormatID != kAudioFormatLinearPCM) {
|
||||
for (int i = 0; kAudioCodecMap[i].codecID != AV_CODEC_ID_NONE; i++) {
|
||||
if (kAudioCodecMap[i].mFormatID == asbd.mFormatID) {
|
||||
return kAudioCodecMap[i].codecID;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (int i = 0; kAudioPCMCodecMap[i].codecID != AV_CODEC_ID_NONE; i++) {
|
||||
if (kAudioPCMCodecMap[i].mFormatFlags == asbd.mFormatFlags &&
|
||||
kAudioPCMCodecMap[i].mBitsPerChannel == asbd.mBitsPerChannel) {
|
||||
return kAudioPCMCodecMap[i].codecID;
|
||||
}
|
||||
}
|
||||
}
|
||||
return AV_CODEC_ID_NONE;
|
||||
}
|
||||
|
||||
enum AVCodecID FourCCToCodecID(OSType formatID)
|
||||
{
|
||||
for (int i = 0; kAudioCodecMap[i].codecID != AV_CODEC_ID_NONE; i++) {
|
||||
@@ -176,3 +222,107 @@ int remap_layout(AudioChannelLayout *layout, uint64_t in_layout, int count)
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef ff_ctzll
|
||||
#define ff_ctzll ff_ctzll_c
|
||||
/* We use the De-Bruijn method outlined in:
|
||||
* http://supertech.csail.mit.edu/papers/debruijn.pdf. */
|
||||
static av_always_inline av_const int ff_ctzll_c(long long v)
|
||||
{
|
||||
static const uint8_t debruijn_ctz64[64] = {
|
||||
0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28,
|
||||
62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11,
|
||||
63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10,
|
||||
51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12
|
||||
};
|
||||
return debruijn_ctz64[(uint64_t)((v & -v) * 0x022FDD63CC95386DU) >> 58];
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
static int get_channel_id(AudioChannelLabel label)
|
||||
{
|
||||
if (label == 0)
|
||||
return -1;
|
||||
else if (label <= kAudioChannelLabel_LFEScreen)
|
||||
return label - 1;
|
||||
else if (label <= kAudioChannelLabel_RightSurround)
|
||||
return label + 4;
|
||||
else if (label <= kAudioChannelLabel_CenterSurround)
|
||||
return label + 1;
|
||||
else if (label <= kAudioChannelLabel_RightSurroundDirect)
|
||||
return label + 23;
|
||||
else if (label <= kAudioChannelLabel_TopBackRight)
|
||||
return label - 1;
|
||||
else if (label < kAudioChannelLabel_RearSurroundLeft)
|
||||
return -1;
|
||||
else if (label <= kAudioChannelLabel_RearSurroundRight)
|
||||
return label - 29;
|
||||
else if (label <= kAudioChannelLabel_RightWide)
|
||||
return label - 4;
|
||||
else if (label == kAudioChannelLabel_LFE2)
|
||||
return ff_ctzll(AV_CH_LOW_FREQUENCY_2);
|
||||
else if (label == kAudioChannelLabel_Mono)
|
||||
return ff_ctzll(AV_CH_FRONT_CENTER);
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int compare_channel_descriptions(const void* a, const void* b)
|
||||
{
|
||||
const AudioChannelDescription* da = a;
|
||||
const AudioChannelDescription* db = b;
|
||||
return get_channel_id(da->mChannelLabel) - get_channel_id(db->mChannelLabel);
|
||||
}
|
||||
|
||||
static AudioChannelLayout *convert_layout(AudioChannelLayout *layout, UInt32* size)
|
||||
{
|
||||
AudioChannelLayoutTag tag = layout->mChannelLayoutTag;
|
||||
AudioChannelLayout *new_layout;
|
||||
if (tag == kAudioChannelLayoutTag_UseChannelDescriptions)
|
||||
return layout;
|
||||
else if (tag == kAudioChannelLayoutTag_UseChannelBitmap)
|
||||
AudioFormatGetPropertyInfo(kAudioFormatProperty_ChannelLayoutForBitmap,
|
||||
sizeof(UInt32), &layout->mChannelBitmap, size);
|
||||
else
|
||||
AudioFormatGetPropertyInfo(kAudioFormatProperty_ChannelLayoutForTag,
|
||||
sizeof(AudioChannelLayoutTag), &tag, size);
|
||||
new_layout = av_malloc(*size);
|
||||
if (!new_layout) {
|
||||
av_free(layout);
|
||||
return NULL;
|
||||
}
|
||||
if (tag == kAudioChannelLayoutTag_UseChannelBitmap)
|
||||
AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForBitmap,
|
||||
sizeof(UInt32), &layout->mChannelBitmap, size, new_layout);
|
||||
else
|
||||
AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForTag,
|
||||
sizeof(AudioChannelLayoutTag), &tag, size, new_layout);
|
||||
new_layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
|
||||
av_free(layout);
|
||||
return new_layout;
|
||||
}
|
||||
|
||||
int convert_layout_to_av(AudioChannelLayout *layout, UInt32 layoutSize)
|
||||
{
|
||||
AudioChannelLayout *layout_copy = av_malloc(layoutSize);
|
||||
memcpy(layout_copy, layout, layoutSize);
|
||||
uint64_t layout_mask = 0;
|
||||
int i;
|
||||
if (!layout_copy)
|
||||
return AVERROR(ENOMEM);
|
||||
if (!(layout_copy = convert_layout(layout_copy, &layoutSize)))
|
||||
return AVERROR(ENOMEM);
|
||||
for (i = 0; i < layout_copy->mNumberChannelDescriptions; i++) {
|
||||
int channel_id = get_channel_id(layout_copy->mChannelDescriptions[i].mChannelLabel);
|
||||
if (channel_id < 0)
|
||||
goto done;
|
||||
if (layout_mask & (1 << channel_id))
|
||||
goto done;
|
||||
layout_mask |= 1 << channel_id;
|
||||
layout_copy->mChannelDescriptions[i].mChannelFlags = i; // Abusing flags as index
|
||||
}
|
||||
done:
|
||||
free(layout_copy);
|
||||
return layout_mask;
|
||||
}
|
||||
@@ -37,8 +37,13 @@
|
||||
if (self) {
|
||||
NSData *magicCookie = [track.muxer_helper->importer magicCookieForTrack:track];
|
||||
AudioStreamBasicDescription asbd = [self basicDescriptorForTrack:track];
|
||||
UInt32 channelLayoutSize = sizeof(AudioChannelLayout);
|
||||
AudioChannelLayout *channelLayout = calloc(channelLayoutSize, 1);
|
||||
channelLayout->mChannelLayoutTag = track.channelLayoutTag;
|
||||
|
||||
_decoder = [[MP42AudioDecoder alloc] initWithAudioFormat:asbd
|
||||
channelLayout:channelLayout
|
||||
channelLayoutSize:channelLayoutSize
|
||||
mixdownType:settings.mixDown
|
||||
drc:settings.drc
|
||||
magicCookie:magicCookie error:error];
|
||||
|
||||
@@ -18,6 +18,8 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
@interface MP42AudioDecoder : NSObject<MP42AudioUnit>
|
||||
|
||||
- (instancetype)initWithAudioFormat:(AudioStreamBasicDescription)asbd
|
||||
channelLayout:(AudioChannelLayout *)channelLayout
|
||||
channelLayoutSize:(UInt32)channelLayoutSize
|
||||
mixdownType:(NSString *)mixdownType
|
||||
drc:(float)drc
|
||||
magicCookie:(NSData *)magicCookie
|
||||
|
||||
+78
-30
@@ -63,10 +63,12 @@ typedef struct MP42DecodeContext MP42DecodeContext;
|
||||
@synthesize outputType = _outputType;
|
||||
|
||||
- (instancetype)initWithAudioFormat:(AudioStreamBasicDescription)asbd
|
||||
channelLayout:(AudioChannelLayout *)channelLayout
|
||||
channelLayoutSize:(UInt32)channelLayoutSize
|
||||
mixdownType:(NSString *)mixdownType
|
||||
drc:(float)drc
|
||||
magicCookie:(NSData *)magicCookie
|
||||
error:(NSError **)error;
|
||||
error:(NSError **)error
|
||||
{
|
||||
self = [super init];
|
||||
|
||||
@@ -75,7 +77,7 @@ typedef struct MP42DecodeContext MP42DecodeContext;
|
||||
|
||||
_inputFormat = asbd;
|
||||
|
||||
enum AVCodecID codecID = FourCCToCodecID(asbd.mFormatID);
|
||||
enum AVCodecID codecID = ASBDToCodecID(asbd);
|
||||
_codec = avcodec_find_decoder(codecID);
|
||||
|
||||
if (!_codec) {
|
||||
@@ -131,9 +133,17 @@ typedef struct MP42DecodeContext MP42DecodeContext;
|
||||
av_dict_set(&av_opts, "drc_scale", drc_scale, 0);
|
||||
}
|
||||
|
||||
// Copy the input channels layout
|
||||
if (channelLayout && channelLayoutSize) {
|
||||
_inputLayoutSize = channelLayoutSize;
|
||||
_inputLayout = malloc(_inputLayoutSize);
|
||||
memcpy(_inputLayout, channelLayout, _inputLayoutSize);
|
||||
}
|
||||
|
||||
if (asbd.mFormatID == kAudioFormatLinearPCM)
|
||||
{
|
||||
_avctx->channels = asbd.mChannelsPerFrame;
|
||||
_avctx->channel_layout = convert_layout_to_av(_inputLayout, _inputLayoutSize);
|
||||
_avctx->sample_rate = asbd.mSampleRate;
|
||||
}
|
||||
|
||||
@@ -189,6 +199,8 @@ typedef struct MP42DecodeContext MP42DecodeContext;
|
||||
|
||||
_context->avctx = _avctx;
|
||||
_context->inputFormat = &_inputFormat;
|
||||
_context->inputLayout = &_inputLayout;
|
||||
_context->inputLayoutSize = &_inputLayoutSize;
|
||||
_context->outputFormat = &_outputFormat;
|
||||
_context->outputLayout = &_outputLayout;
|
||||
_context->outputLayoutSize = &_outputLayoutSize;
|
||||
@@ -271,17 +283,60 @@ static MP42SampleBuffer * sampleBufferFromFrame(uint8_t *output_data, int output
|
||||
return sample;
|
||||
}
|
||||
|
||||
static void convertChannelLayout(MP42DecodeContext *context)
|
||||
{
|
||||
// Create the AudioChannelLayout from the FFmpeg channel layout
|
||||
UInt32 layout_size = sizeof(AudioChannelLayout) +
|
||||
sizeof(AudioChannelDescription) * context->outputFormat->mChannelsPerFrame;
|
||||
AudioChannelLayout *outputLayout = malloc(layout_size);
|
||||
bzero(outputLayout, layout_size);
|
||||
remap_layout(outputLayout, context->out_layout, context->outputFormat->mChannelsPerFrame);
|
||||
|
||||
*context->outputLayoutSize = layout_size;
|
||||
*context->outputLayout = outputLayout;
|
||||
}
|
||||
|
||||
static void configurePCMDescriptors(MP42DecodeContext *context, AVFrame *frame)
|
||||
{
|
||||
AudioChannelLayout *inputLayout = *context->inputLayout;
|
||||
|
||||
int nb_channels = AudioChannelLayoutTag_GetNumberOfChannels(inputLayout->mChannelLayoutTag);
|
||||
|
||||
if (nb_channels == 1) {
|
||||
context->out_layout = AV_CH_LAYOUT_MONO;
|
||||
}
|
||||
else {
|
||||
context->out_layout = AV_CH_LAYOUT_STEREO;
|
||||
}
|
||||
|
||||
// Set the output channel layout
|
||||
context->out_layout = AV_CH_LAYOUT_STEREO;
|
||||
if (context->outputFormat->mChannelsPerFrame < context->inputFormat->mChannelsPerFrame) {
|
||||
if (context->outputFormat->mChannelsPerFrame == 2) {
|
||||
context->out_layout = AV_CH_LAYOUT_STEREO;
|
||||
}
|
||||
else if (context->outputFormat->mChannelsPerFrame == 1) {
|
||||
context->out_layout = AV_CH_LAYOUT_MONO;
|
||||
}
|
||||
}
|
||||
else if (frame->channel_layout) {
|
||||
context->out_layout = convert_layout_to_av(*context->inputLayout, *context->inputLayoutSize);;
|
||||
}
|
||||
|
||||
// Create the AudioChannelLayout from the FFmpeg channel layout
|
||||
convertChannelLayout(context);
|
||||
|
||||
if (context->inputFormat->mSampleRate > 48000) {
|
||||
context->outputFormat->mSampleRate = 48000;
|
||||
}
|
||||
}
|
||||
|
||||
static void configureDescriptors(MP42DecodeContext *context, AVFrame *frame)
|
||||
{
|
||||
// Get real channels number and layout
|
||||
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;
|
||||
@@ -312,21 +367,19 @@ static void configureDescriptors(MP42DecodeContext *context, AVFrame *frame)
|
||||
}
|
||||
|
||||
// Create the AudioChannelLayout from the FFmpeg channel layout
|
||||
UInt32 layout_size = sizeof(AudioChannelLayout) +
|
||||
sizeof(AudioChannelDescription) * context->outputFormat->mChannelsPerFrame;
|
||||
AudioChannelLayout *outputLayout = malloc(layout_size);
|
||||
bzero(outputLayout, layout_size);
|
||||
remap_layout(outputLayout, context->out_layout, context->outputFormat->mChannelsPerFrame);
|
||||
|
||||
*context->outputLayoutSize = layout_size;
|
||||
*context->outputLayout = outputLayout;
|
||||
convertChannelLayout(context);
|
||||
}
|
||||
|
||||
static int resample(MP42DecodeContext *context, AVFrame *frame, uint8_t **output_data, int *output_data_size)
|
||||
{
|
||||
int ret = 0;
|
||||
if (!context->configured) {
|
||||
configureDescriptors(context, frame);
|
||||
if (context->inputFormat->mFormatID == kAudioFormatLinearPCM) {
|
||||
configurePCMDescriptors(context, frame);
|
||||
}
|
||||
else {
|
||||
configureDescriptors(context, frame);
|
||||
}
|
||||
// We want float interleaved.
|
||||
context->resampler = hb_audio_resample_init(AV_SAMPLE_FMT_FLT,
|
||||
context->out_layout,
|
||||
@@ -363,20 +416,15 @@ static int resample(MP42DecodeContext *context, AVFrame *frame, uint8_t **output
|
||||
center_mix_level,
|
||||
downmix_info->lfe_mix_level);
|
||||
}
|
||||
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))
|
||||
{
|
||||
|
||||
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 (hb_audio_resample_update(context->resampler)) {
|
||||
NSLog(@"decavcodec: hb_audio_resample_update() failed");
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user