* Support for DTS to AC3 + AAC conversion.
If you prefer to recompile FFmpeg from scratch, make sure you include the --enable-encoder=ac3 and --disable-iconv parameters. My config: ./configure --disable-programs --disable-filters --disable-everything --disable-avdevice --disable-swscale --enable-avresample --enable-decoder=dvdsub --enable-decoder=pgssub --enable-decoder=aac --enable-decoder=mp3 --enable-decoder=mp2 --enable-decoder=mp1 --enable-decoder=ac3 --enable-decoder=eac3 --enable-decoder=truehd --enable-decoder=alac --enable-decoder=opus --enable-decoder=vorbis --enable-decoder=flac --enable-decoder=mlp --enable-decoder=dca --enable-decoder=pcm --enable-filter=aresample --enable-parser=opus --enable-muxer=opus --prefix=/Users/damians/Desktop/iffmpeg --extra-cxxflags='-mmacosx-version-min=10.9' --extra-cflags='-mmacosx-version-min=10.9' --extra-ldflags='-mmacosx-version-min=10.9' --enable-decoder=pcm_f32be --enable-decoder=pcm_f32le --enable-decoder=pcm_f64be --enable-decoder=pcm_f64le --enable-decoder=pcm_s16be --enable-decoder=pcm_s16le --enable-decoder=pcm_s16le_planar --enable-decoder=pcm_s24be --enable-decoder=pcm_daud --enable-decoder=pcm_s24le --enable-decoder=pcm_s32be --enable-decoder=pcm_s32le --enable-decoder=pcm_s8 --enable-decoder=pcm_u8 --enable-decoder=pcm_u16be --enable-decoder=pcm_u16le --enable-decoder=pcm_u24be --enable-decoder=pcm_u24le --enable-encoder=ac3 --disable-iconv
This commit is contained in:
1 parent
2a75f36676
commit
5e821d87f6
12 files changed
+617
-6
No files matched your search
@@ -22,4 +22,6 @@ OSType CodecIDToFourCC(enum AVCodecID codecID);
|
||||
int remap_layout(AudioChannelLayout *layout, uint64_t in_layout, int count);
|
||||
int convert_layout_to_av(AudioChannelLayout *layout, UInt32 layoutSize);
|
||||
|
||||
int channel_layout_for_channels(AVCodec *codec, int channels);
|
||||
|
||||
#endif /* FFmpegUtils_h */
|
||||
@@ -17,6 +17,10 @@
|
||||
extern AVCodec ff_##x##_decoder; \
|
||||
avcodec_register(&ff_##x##_decoder); }
|
||||
|
||||
#define REGISTER_ENCODER(x) { \
|
||||
extern AVCodec ff_##x##_encoder; \
|
||||
avcodec_register(&ff_##x##_encoder); }
|
||||
|
||||
static int ff_lockmgr_cb(void **mutex, enum AVLockOp op)
|
||||
{
|
||||
switch (op) {
|
||||
@@ -83,6 +87,7 @@ void FFInitFFmpeg()
|
||||
REGISTER_DECODER(pcm_u16le);
|
||||
REGISTER_DECODER(pcm_u24be);
|
||||
REGISTER_DECODER(pcm_u24le);
|
||||
REGISTER_ENCODER(ac3);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -327,3 +332,27 @@ done:
|
||||
free(layout_copy);
|
||||
return layout_mask;
|
||||
}
|
||||
|
||||
int channel_layout_for_channels(AVCodec *codec, int channels)
|
||||
{
|
||||
const uint64_t *p;
|
||||
uint64_t best_ch_layout = 0;
|
||||
int best_nb_channells = 0;
|
||||
|
||||
if (!codec->channel_layouts)
|
||||
return AV_CH_LAYOUT_STEREO;
|
||||
|
||||
p = codec->channel_layouts;
|
||||
while (*p) {
|
||||
int nb_channels = av_get_channel_layout_nb_channels(*p);
|
||||
|
||||
if (nb_channels == channels) return *p;
|
||||
|
||||
if (nb_channels > best_nb_channells) {
|
||||
best_ch_layout = *p;
|
||||
best_nb_channells = nb_channels;
|
||||
}
|
||||
p++;
|
||||
}
|
||||
return best_ch_layout;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// MP42AC3AudioEncoder.h
|
||||
// MP42Foundation
|
||||
//
|
||||
// Created by Damiano Galassi on 23/07/2016.
|
||||
// Copyright © 2016 Damiano Galassi. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#import "MP42AudioUnit.h"
|
||||
|
||||
@class MP42SampleBuffer;
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MP42AC3AudioEncoder : NSObject<MP42AudioUnit>
|
||||
|
||||
- (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, nullable) AudioChannelLayout *outputLayout;
|
||||
@property (nonatomic, readonly) UInt32 outputLayoutSize;
|
||||
@property (nonatomic, readonly) AudioStreamBasicDescription outputFormat;
|
||||
|
||||
@property (nonatomic, readonly) NSData *magicCookie;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,408 @@
|
||||
//
|
||||
// MP42AC3AudioEncoder.m
|
||||
// MP42Foundation
|
||||
//
|
||||
// Created by Damiano Galassi on 23/07/2016.
|
||||
// Copyright © 2016 Damiano Galassi. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MP42AC3AudioEncoder.h"
|
||||
#import "MP42Fifo.h"
|
||||
#import "MP42Sample.h"
|
||||
#import "MP42PrivateUtilities.h"
|
||||
#include "sfifo.h"
|
||||
#include "FFmpegUtils.h"
|
||||
|
||||
#define FIFO_DURATION (2.5f)
|
||||
|
||||
// A struct to hold info for the data proc
|
||||
typedef struct AudioFileIO
|
||||
{
|
||||
sfifo_t *ringBuffer;
|
||||
|
||||
float *inBuffer;
|
||||
UInt32 inBufferSize;
|
||||
|
||||
UInt32 outputMaxSize;
|
||||
|
||||
UInt32 inSizePerPacket;
|
||||
UInt32 inSamples;
|
||||
UInt32 channelsPerFrame;
|
||||
UInt32 numPacketsPerRead;
|
||||
|
||||
AVFrame *frame;
|
||||
float *samples;
|
||||
|
||||
UInt64 outputPos;
|
||||
|
||||
bool done;
|
||||
|
||||
AudioStreamPacketDescription * _Nullable pktDescs;
|
||||
} AudioFileIO;
|
||||
|
||||
@interface MP42AC3AudioEncoder ()
|
||||
{
|
||||
AVCodecContext *_avctx;
|
||||
|
||||
__unsafe_unretained id<MP42AudioUnit> _outputUnit;
|
||||
MP42AudioUnitOutput _outputType;
|
||||
|
||||
NSData *_magicCookie;
|
||||
}
|
||||
|
||||
@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, unsafe_unretained) id<MP42AudioUnit> inputUnit;
|
||||
|
||||
@end
|
||||
|
||||
@implementation MP42AC3AudioEncoder
|
||||
|
||||
@synthesize outputUnit = _outputUnit;
|
||||
@synthesize outputType = _outputType;
|
||||
|
||||
@synthesize magicCookie = _magicCookie;
|
||||
|
||||
- (instancetype)initWithInputUnit:(id<MP42AudioUnit>)unit bitRate:(NSUInteger)bitRate error:(NSError **)error
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_inputUnit = unit;
|
||||
_inputUnit.outputUnit = self;
|
||||
_inputFormat = unit.outputFormat;
|
||||
|
||||
_bitrate = bitRate;
|
||||
|
||||
_inputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
|
||||
_outputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
|
||||
|
||||
[self start];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
[self disposeConverter];
|
||||
}
|
||||
|
||||
#pragma mark - Encoder Init
|
||||
|
||||
- (BOOL)initConverterWithBitRate:(NSUInteger)bitrate
|
||||
{
|
||||
AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_AC3);
|
||||
if (!codec) {
|
||||
if ([[NSString stringWithFormat:@"%s", avcodec_configuration()] rangeOfString:@"--enable-encoder=ac3"].location == NSNotFound) {
|
||||
NSLog(@"Codec not found. Compile ffmpeg with configuration: %s --enable-encoder=ac3", avcodec_configuration());
|
||||
}
|
||||
else {
|
||||
NSLog(@"Codec not found. Have you registered the encoder with avcodec_register() or avcodec_register_all()?");
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
_avctx = avcodec_alloc_context3(codec);
|
||||
if (!_avctx) {
|
||||
NSLog(@"Could not allocate audio codec context");
|
||||
return NO;
|
||||
}
|
||||
|
||||
_avctx->bit_rate = (!bitrate) ? 640000 : bitrate * 1000;
|
||||
_avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
|
||||
_avctx->sample_rate = 48000;
|
||||
|
||||
_avctx->channel_layout = channel_layout_for_channels(codec, _inputFormat.mChannelsPerFrame);
|
||||
_avctx->channels = av_get_channel_layout_nb_channels(_avctx->channel_layout);
|
||||
|
||||
AudioStreamBasicDescription outputFormat;
|
||||
bzero(&outputFormat, sizeof(AudioStreamBasicDescription));
|
||||
outputFormat.mFormatID = kAudioFormatAC3;
|
||||
outputFormat.mSampleRate = _avctx->sample_rate;
|
||||
outputFormat.mChannelsPerFrame = _avctx->channels;
|
||||
outputFormat.mFramesPerPacket = 1536;
|
||||
outputFormat.mBytesPerPacket = 4 * outputFormat.mChannelsPerFrame;
|
||||
outputFormat.mBytesPerFrame = outputFormat.mBytesPerPacket * outputFormat.mFramesPerPacket;
|
||||
outputFormat.mBitsPerChannel = 16;
|
||||
outputFormat.mFormatFlags = kAudioFormatFlagIsFloat;
|
||||
_outputFormat = outputFormat;
|
||||
|
||||
if (avcodec_open2(_avctx, codec, NULL) < 0) {
|
||||
NSLog(@"Could not open codec");
|
||||
return NO;
|
||||
}
|
||||
|
||||
_ringBuffer = (sfifo_t *) malloc(sizeof(sfifo_t));
|
||||
int ringbuffer_len = _inputFormat.mSampleRate * FIFO_DURATION * 4 * 23;
|
||||
sfifo_init(_ringBuffer, ringbuffer_len);
|
||||
|
||||
_afio = malloc(sizeof(AudioFileIO));
|
||||
bzero(_afio, sizeof(AudioFileIO));
|
||||
|
||||
_afio->inBufferSize = 1024 * 64;
|
||||
_afio->inBuffer = malloc(_afio->inBufferSize);
|
||||
|
||||
_afio->outputMaxSize = _afio->inBufferSize;
|
||||
|
||||
_afio->inSizePerPacket = _inputFormat.mBytesPerPacket;
|
||||
_afio->inSamples = _outputFormat.mFramesPerPacket;
|
||||
_afio->channelsPerFrame = _inputFormat.mChannelsPerFrame;
|
||||
_afio->numPacketsPerRead = _afio->inBufferSize / _afio->inSizePerPacket;
|
||||
|
||||
_afio->ringBuffer = _ringBuffer;
|
||||
|
||||
_afio->frame = av_frame_alloc();
|
||||
if (!_afio->frame) {
|
||||
NSLog(@"Could not allocate audio frame");
|
||||
return NO;
|
||||
}
|
||||
|
||||
_afio->frame->nb_samples = _avctx->frame_size;
|
||||
_afio->frame->format = _avctx->sample_fmt;
|
||||
_afio->frame->channel_layout = _avctx->channel_layout;
|
||||
_afio->frame->channels = _avctx->channels;
|
||||
|
||||
int buffer_size = av_samples_get_buffer_size(NULL, _avctx->channels, _avctx->frame_size,
|
||||
_avctx->sample_fmt, 0);
|
||||
_afio->samples = av_malloc(buffer_size);
|
||||
if (!_afio->samples) {
|
||||
NSLog(@"Could not allocate %d bytes for samples buffer", buffer_size);
|
||||
return NO;
|
||||
}
|
||||
int ret = avcodec_fill_audio_frame(_afio->frame, _avctx->channels, _avctx->sample_fmt,
|
||||
(const uint8_t*)_afio->samples, buffer_size, 0);
|
||||
if (ret < 0) {
|
||||
NSLog(@"Could not setup audio frame");
|
||||
return NO;
|
||||
}
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (void)disposeConverter
|
||||
{
|
||||
if (_afio)
|
||||
{
|
||||
if (_afio->samples)
|
||||
{
|
||||
av_freep(&_afio->samples);
|
||||
}
|
||||
if (_afio->frame)
|
||||
{
|
||||
av_frame_free(&_afio->frame);
|
||||
}
|
||||
}
|
||||
if (_avctx)
|
||||
{
|
||||
avcodec_close(_avctx);
|
||||
av_free(_avctx);
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)createMagicCookie
|
||||
{
|
||||
uint64_t fscod = 0;
|
||||
uint64_t bsid = 8;
|
||||
uint64_t bsmod = 0;
|
||||
uint64_t acmod = 7;
|
||||
uint64_t lfeon = (_avctx->channel_layout & AV_CH_LOW_FREQUENCY) ? 1 : 0;
|
||||
uint64_t bit_rate_code = 15;
|
||||
|
||||
switch (_avctx->channels - lfeon)
|
||||
{
|
||||
case 1:
|
||||
acmod = 1;
|
||||
break;
|
||||
case 2:
|
||||
acmod = 2;
|
||||
break;
|
||||
case 3:
|
||||
if (_avctx->channel_layout & AV_CH_BACK_CENTER) acmod = 3;
|
||||
else acmod = 4;
|
||||
break;
|
||||
case 4:
|
||||
if (_avctx->channel_layout & AV_CH_BACK_CENTER) acmod = 5;
|
||||
else acmod = 6;
|
||||
break;
|
||||
case 5:
|
||||
acmod = 7;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (_avctx->sample_rate == 48000) fscod = 0;
|
||||
else if (_avctx->sample_rate == 44100) fscod = 1;
|
||||
else if (_avctx->sample_rate == 32000) fscod = 2;
|
||||
else fscod = 3;
|
||||
|
||||
NSMutableData *ac3Info = [[NSMutableData alloc] init];
|
||||
[ac3Info appendBytes:&fscod length:sizeof(uint64_t)];
|
||||
[ac3Info appendBytes:&bsid length:sizeof(uint64_t)];
|
||||
[ac3Info appendBytes:&bsmod length:sizeof(uint64_t)];
|
||||
[ac3Info appendBytes:&acmod length:sizeof(uint64_t)];
|
||||
[ac3Info appendBytes:&lfeon length:sizeof(uint64_t)];
|
||||
[ac3Info appendBytes:&bit_rate_code length:sizeof(uint64_t)];
|
||||
|
||||
_magicCookie = ac3Info;
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (void)start
|
||||
{
|
||||
_decoderThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadMainRoutine) object:nil];
|
||||
[_decoderThread setName:@"AudioToolbox Audio Encoder"];
|
||||
[_decoderThread start];
|
||||
}
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
- (void)addSample:(MP42SampleBuffer *)sample
|
||||
{
|
||||
[_inputSamplesBuffer enqueue:sample];
|
||||
}
|
||||
|
||||
- (nullable MP42SampleBuffer *)copyEncodedSample
|
||||
{
|
||||
return [_outputSamplesBuffer dequeue];
|
||||
}
|
||||
|
||||
#pragma mark - Encoder
|
||||
|
||||
static MP42SampleBuffer *encode(AVCodecContext *context, AudioFileIO *afio)
|
||||
{
|
||||
UInt32 availableBytes = sfifo_used(afio->ringBuffer);
|
||||
if (!afio->done &&
|
||||
availableBytes < afio->inSamples * afio->inSizePerPacket) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
UInt32 wanted = MIN(afio->inSamples * afio->inSizePerPacket, availableBytes);
|
||||
UInt32 outNumBytes = sfifo_read(afio->ringBuffer, afio->inBuffer, wanted);
|
||||
if (outNumBytes == 0)
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
|
||||
// Populate the AVFrame with the samples
|
||||
float *pEnc = afio->samples;
|
||||
float *pIn = afio->inBuffer;
|
||||
int channels = afio->channelsPerFrame;
|
||||
int samples = context->frame_size;
|
||||
int outputChannels = context->channels;
|
||||
for (int ch = 0; ch < channels; ++ch) {
|
||||
for (int i = 0; i < samples; ++i) {
|
||||
if (ch < outputChannels) {
|
||||
*pEnc++ = pIn[channels * i + ch];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Encode
|
||||
MP42SampleBuffer *sample = nil;
|
||||
if (avcodec_send_frame(context, afio->frame) == 0)
|
||||
{
|
||||
AVPacket *pkt = av_packet_alloc();
|
||||
if (avcodec_receive_packet(context, pkt) == 0)
|
||||
{
|
||||
sample = [[MP42SampleBuffer alloc] init];
|
||||
sample->size = pkt->size;
|
||||
sample->duration = pkt->duration;
|
||||
sample->decodeTimestamp = afio->outputPos * pkt->duration;
|
||||
sample->presentationTimestamp = sample->decodeTimestamp;
|
||||
sample->presentationOutputTimestamp = sample->decodeTimestamp;
|
||||
sample->data = malloc(pkt->size);
|
||||
memcpy(sample->data, pkt->data, pkt->size);
|
||||
sample->flags |= MP42SampleBufferFlagIsSync;
|
||||
afio->outputPos += 1;
|
||||
}
|
||||
av_packet_free(&pkt);
|
||||
}
|
||||
|
||||
return sample;
|
||||
}
|
||||
|
||||
static MP42SampleBuffer *flush(AVCodecContext *context, AudioFileIO *afio)
|
||||
{
|
||||
MP42SampleBuffer *outSample = encode(context, afio);
|
||||
return outSample;
|
||||
}
|
||||
|
||||
static inline void enqueue(MP42AC3AudioEncoder *self, MP42SampleBuffer *outSample)
|
||||
{
|
||||
if (outSample) {
|
||||
if (self->_outputType == MP42AudioUnitOutputPush) {
|
||||
[self->_outputUnit addSample:outSample];
|
||||
}
|
||||
else {
|
||||
[self->_outputSamplesBuffer enqueue:outSample];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)threadMainRoutine
|
||||
{
|
||||
@autoreleasepool {
|
||||
MP42SampleBuffer *sampleBuffer = nil;
|
||||
|
||||
while ((sampleBuffer = [_inputSamplesBuffer dequeueAndWait])) {
|
||||
@autoreleasepool {
|
||||
|
||||
MP42SampleBuffer *outSample = nil;
|
||||
|
||||
if (sampleBuffer->flags & MP42SampleBufferFlagEndOfFile) {
|
||||
if (_avctx) {
|
||||
_afio->done = true;
|
||||
while ((outSample = flush(_avctx, _afio))) {
|
||||
enqueue(self, outSample);
|
||||
}
|
||||
}
|
||||
|
||||
enqueue(self, sampleBuffer);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
if (_avctx) {
|
||||
sfifo_write(_ringBuffer, sampleBuffer->data, sampleBuffer->size);
|
||||
while ((outSample = encode(_avctx, _afio))) {
|
||||
enqueue(self, outSample);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
#import "MP42AudioDecoder.h"
|
||||
#import "MP42AudioEncoder.h"
|
||||
#import "MP42AC3AudioEncoder.h"
|
||||
|
||||
#import "MP42Track+Muxer.h"
|
||||
#import "MP42FileImporter+Private.h"
|
||||
@@ -22,7 +23,7 @@
|
||||
@interface MP42AudioConverter ()
|
||||
|
||||
@property (nonatomic, readonly) MP42AudioDecoder *decoder;
|
||||
@property (nonatomic, readonly) MP42AudioEncoder *encoder;
|
||||
@property (nonatomic, readonly) id<MP42AudioUnit> encoder;
|
||||
|
||||
@end
|
||||
|
||||
@@ -53,10 +54,17 @@
|
||||
if (!_decoder) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
_encoder = [[MP42AudioEncoder alloc] initWithInputUnit:_decoder
|
||||
bitRate:settings.bitRate
|
||||
error:error];
|
||||
|
||||
if (settings.format == kMP42AudioCodecType_AC3 || settings.format == kMP42AudioCodecType_EnhancedAC3) {
|
||||
_encoder = [[MP42AC3AudioEncoder alloc] initWithInputUnit:_decoder
|
||||
bitRate:settings.bitRate
|
||||
error:error];
|
||||
}
|
||||
else {
|
||||
_encoder = [[MP42AudioEncoder alloc] initWithInputUnit:_decoder
|
||||
bitRate:settings.bitRate
|
||||
error:error];
|
||||
}
|
||||
_encoder.outputUnit = self;
|
||||
_encoder.outputType = MP42AudioUnitOutputPull;
|
||||
|
||||
|
||||
@@ -192,6 +192,23 @@
|
||||
return (self.trackId > 0);
|
||||
}
|
||||
|
||||
- (void)setConversionSettings:(MP42AudioConversionSettings *)conversionSettings
|
||||
{
|
||||
[super setConversionSettings:conversionSettings];
|
||||
|
||||
if (conversionSettings == nil) return;
|
||||
|
||||
if ([self.name rangeOfString:@"surround" options:NSCaseInsensitiveSearch].location == NSNotFound) return;
|
||||
|
||||
if ([conversionSettings.mixDown isEqualToString:SBNoneMixdown]) {
|
||||
// FFmpeg encoding currently doesn't go beyond 5.1
|
||||
self.name = (self.channels > 6) ? @"Surround 5.1" : nameForChannelLayoutTag(self.channelLayoutTag);
|
||||
}
|
||||
else {
|
||||
self.name = (self.channels == 1 || [conversionSettings.mixDown isEqualToString:SBMonoMixdown]) ? @"Mono" : @"Stereo";
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setVolume:(float)newVolume
|
||||
{
|
||||
_volume = newVolume;
|
||||
|
||||
@@ -22,6 +22,8 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
+ (instancetype)audioConversionWithBitRate:(NSUInteger)bitrate mixDown:(NSString *)mixDown drc:(float)drc;
|
||||
|
||||
- (instancetype)initWitFormat:(FourCharCode)format bitRate:(NSUInteger)bitRate mixDown:(NSString *)mixDown drc:(float)drc;
|
||||
|
||||
@property (nonatomic, readonly) NSUInteger bitRate;
|
||||
|
||||
@property (nonatomic, readonly) NSString *mixDown;
|
||||
|
||||
+2
-1
@@ -1046,7 +1046,8 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) {
|
||||
|
||||
for (MP42AudioTrack *track in [self tracksWithMediaType:kMP42MediaType_Audio] ) {
|
||||
if ((track.targetFormat == kMP42AudioCodecType_AC3 ||
|
||||
track.targetFormat == kMP42AudioCodecType_EnhancedAC3) &&
|
||||
track.targetFormat == kMP42AudioCodecType_EnhancedAC3 ||
|
||||
track.targetFormat == kMP42AudioCodecType_DTS) &&
|
||||
track.fallbackTrack == nil) {
|
||||
[needFallbackTracks addObject:track];
|
||||
}
|
||||
|
||||
@@ -119,6 +119,72 @@
|
||||
// so update the track timescale here.
|
||||
timeScale = audioConverter.sampleRate;
|
||||
format = track.conversionSettings.format;
|
||||
|
||||
if (track.format == kMP42AudioCodecType_DTS && format == kMP42AudioCodecType_AC3) {
|
||||
AudioStreamBasicDescription asbd;
|
||||
bzero(&asbd, sizeof(AudioStreamBasicDescription));
|
||||
asbd.mSampleRate = [track.muxer_helper->importer timescaleForTrack:track];
|
||||
asbd.mChannelsPerFrame = [(MP42AudioTrack *)track channels];
|
||||
asbd.mFormatID = track.format;
|
||||
|
||||
UInt32 channelLayoutSize = sizeof(AudioChannelLayout);
|
||||
AudioChannelLayout *channelLayout = calloc(channelLayoutSize, 1);
|
||||
channelLayout->mChannelLayoutTag = [(MP42AudioTrack *)track channelLayoutTag];
|
||||
|
||||
UInt32 bitmapSize = sizeof(UInt32);
|
||||
UInt32 channelBitmap;
|
||||
OSStatus err = AudioFormatGetProperty(kAudioFormatProperty_BitmapForLayoutTag,
|
||||
sizeof(AudioChannelLayoutTag), &channelLayout->mChannelLayoutTag,
|
||||
&bitmapSize, &channelBitmap);
|
||||
if (err && AudioChannelLayoutTag_GetNumberOfChannels(channelLayout->mChannelLayoutTag) == 6) {
|
||||
channelBitmap = 0x3F;
|
||||
}
|
||||
free(channelLayout);
|
||||
|
||||
uint64_t fscod = 0;
|
||||
uint64_t bsid = 8;
|
||||
uint64_t bsmod = 0;
|
||||
uint64_t acmod = 7;
|
||||
uint64_t lfeon = (channelBitmap & kAudioChannelBit_LFEScreen) ? 1 : 0;
|
||||
uint64_t bit_rate_code = 15;
|
||||
|
||||
switch (asbd.mChannelsPerFrame - lfeon) {
|
||||
case 1:
|
||||
acmod = 1;
|
||||
break;
|
||||
case 2:
|
||||
acmod = 2;
|
||||
break;
|
||||
case 3:
|
||||
if (channelBitmap & kAudioChannelBit_CenterSurround) acmod = 3;
|
||||
else acmod = 4;
|
||||
break;
|
||||
case 4:
|
||||
if (channelBitmap & kAudioChannelBit_CenterSurround) acmod = 5;
|
||||
else acmod = 6;
|
||||
break;
|
||||
case 5:
|
||||
acmod = 7;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (asbd.mSampleRate == 48000) fscod = 0;
|
||||
else if (asbd.mSampleRate == 44100) fscod = 1;
|
||||
else if (asbd.mSampleRate == 32000) fscod = 2;
|
||||
else fscod = 3;
|
||||
|
||||
NSMutableData *ac3Info = [NSMutableData dataWithCapacity:sizeof(uint64_t) * 6];
|
||||
[ac3Info appendBytes:&fscod length:sizeof(uint64_t)];
|
||||
[ac3Info appendBytes:&bsid length:sizeof(uint64_t)];
|
||||
[ac3Info appendBytes:&bsmod length:sizeof(uint64_t)];
|
||||
[ac3Info appendBytes:&acmod length:sizeof(uint64_t)];
|
||||
[ac3Info appendBytes:&lfeon length:sizeof(uint64_t)];
|
||||
[ac3Info appendBytes:&bit_rate_code length:sizeof(uint64_t)];
|
||||
|
||||
magicCookie = ac3Info;
|
||||
}
|
||||
}
|
||||
if ([track isMemberOfClass:[MP42SubtitleTrack class]] && track.conversionSettings &&
|
||||
(track.format == kMP42SubtitleCodecType_VobSub || track.format == kMP42SubtitleCodecType_PGS)) {
|
||||
|
||||
@@ -18,4 +18,6 @@ BOOL trackNeedConversion(FourCharCode formatName);
|
||||
|
||||
int isHdVideo(uint64_t width, uint64_t height);
|
||||
|
||||
NSString *nameForChannelLayoutTag(UInt32 channelLayoutTag);
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#import "MP42Utilities.h"
|
||||
#import "MP42SubUtilities.h"
|
||||
#import <AudioToolbox/AudioToolbox.h>
|
||||
|
||||
NSString * StringFromTime(long long time, long timeScale)
|
||||
{
|
||||
@@ -99,3 +100,37 @@ int isHdVideo(uint64_t width, uint64_t height)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
NSString *nameForChannelLayoutTag(UInt32 channelLayoutTag)
|
||||
{
|
||||
UInt32 channelLayoutSize = sizeof(AudioChannelLayout);
|
||||
AudioChannelLayout *channelLayout = calloc(channelLayoutSize, 1);
|
||||
channelLayout->mChannelLayoutTag = channelLayoutTag;
|
||||
|
||||
UInt32 bitmapSize = sizeof(UInt32);
|
||||
UInt32 channelBitmap;
|
||||
OSStatus err = AudioFormatGetProperty(kAudioFormatProperty_BitmapForLayoutTag,
|
||||
sizeof(AudioChannelLayoutTag), &channelLayout->mChannelLayoutTag,
|
||||
&bitmapSize, &channelBitmap);
|
||||
UInt32 channels = AudioChannelLayoutTag_GetNumberOfChannels(channelLayout->mChannelLayoutTag);
|
||||
if (err && channels == 6) {
|
||||
channelBitmap = 0x3F;
|
||||
}
|
||||
free(channelLayout);
|
||||
BOOL lfe = (channelBitmap & kAudioChannelBit_LFEScreen);
|
||||
int mainChannels = (lfe) ? channels - 1 : channels;
|
||||
NSString *desc;
|
||||
switch (mainChannels) {
|
||||
case 1:
|
||||
desc = @"Mono";
|
||||
break;
|
||||
case 2:
|
||||
case 3:
|
||||
desc = @"Stereo";
|
||||
break;
|
||||
default:
|
||||
desc = @"Surround";
|
||||
break;
|
||||
}
|
||||
return [NSString stringWithFormat:@"%@ %d.%d", desc, mainChannels, (lfe) ? 1 : 0];
|
||||
}
|
||||
Reference in new issue
Block a user