-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathAudioConverterXX.cpp
183 lines (169 loc) · 6.03 KB
/
AudioConverterXX.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "AudioConverterXX.h"
#include <limits>
#include <cmath>
#include "CoreAudio/AudioCodec.h"
#include "CoreAudio/AudioComponent.h"
#include "cautil.h"
#include "chanmap.h"
const UInt32 AAC_7_1_Rear_Tag = kAudioChannelLayoutTag_AAC_7_1_B;
std::vector<uint8_t> AudioConverterXX::getCompressionMagicCookie()
{
auto cookie = BaseT::getCompressionMagicCookie();
if (isOutputAAC() && m_OutputChannelLayoutTag == AAC_7_1_Rear_Tag) {
auto asc = cautil::parseMagicCookieAAC(cookie);
cautil::insert71RearPCEToASC(&asc);
cautil::replaceASCInMagicCookie(&cookie, asc);
}
return cookie;
}
std::shared_ptr<AudioChannelLayout> AudioConverterXX::getInputChannelLayout()
{
if (!isOutputAAC() || m_InputChannelLayoutTag != AAC_7_1_Rear_Tag)
return BaseT::getInputChannelLayout();
else {
auto acl =
std::shared_ptr<AudioChannelLayout>(new AudioChannelLayout);
memset(acl.get(), 0, sizeof(AudioChannelLayout));
acl->mChannelLayoutTag = m_InputChannelLayoutTag;
return acl;
}
}
void AudioConverterXX::setInputChannelLayout(const AudioChannelLayout &acl)
{
if (!isOutputAAC() || acl.mChannelLayoutTag != AAC_7_1_Rear_Tag)
BaseT::setInputChannelLayout(acl);
else {
m_InputChannelLayoutTag = acl.mChannelLayoutTag;
AudioChannelLayout acl2 = { 0 };
acl2.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_7_1_B;
BaseT::setInputChannelLayout(acl2);
}
}
std::shared_ptr<AudioChannelLayout> AudioConverterXX::getOutputChannelLayout()
{
if (!isOutputAAC() || m_OutputChannelLayoutTag != AAC_7_1_Rear_Tag)
return BaseT::getOutputChannelLayout();
else {
auto acl =
std::shared_ptr<AudioChannelLayout>(new AudioChannelLayout);
memset(acl.get(), 0, sizeof(AudioChannelLayout));
acl->mChannelLayoutTag = m_OutputChannelLayoutTag;
return acl;
}
}
void AudioConverterXX::setOutputChannelLayout(const AudioChannelLayout &acl)
{
if (!isOutputAAC() || acl.mChannelLayoutTag != AAC_7_1_Rear_Tag)
BaseT::setOutputChannelLayout(acl);
else {
m_OutputChannelLayoutTag = acl.mChannelLayoutTag;
AudioChannelLayout acl2 = { 0 };
acl2.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_7_1_B;
BaseT::setOutputChannelLayout(acl2);
}
}
bool AudioConverterXX::isOutputAAC()
{
auto asbd = getOutputStreamDescription();
return (asbd.mFormatID == 'aac ' || asbd.mFormatID == 'aach');
}
double AudioConverterXX::getClosestAvailableBitRate(double value)
{
auto rates = getApplicableEncodeBitRates();
double distance = std::numeric_limits<double>::max();
double pick = 0;
for (auto it = rates.begin(); it != rates.end(); ++it) {
if (!it->mMinimum) continue;
double diff = std::abs(value - it->mMinimum);
if (distance > diff) {
distance = diff;
pick = it->mMinimum;
}
}
return pick;
}
void AudioConverterXX::configAACCodec(UInt32 bitrateControlMode, double bitrate,
UInt32 codecQuality)
{
const UInt32 qmax = kAudioConverterQuality_Max;
setBitRateControlMode(bitrateControlMode);
setCodecQuality(std::min(codecQuality, qmax));
if (bitrateControlMode == kAudioCodecBitRateControlMode_Variable)
setSoundQualityForVBR(std::min(UInt32(bitrate + .5), qmax));
else {
if (bitrate == 0.0) // request maximum available bitrate
bitrate = 1000.0 * 1000.0 * 1000.0; // set big enough value
else if (bitrate >= 8000) // in bps
;
else if (bitrate >= 8.0) // in kbps
bitrate *= 1000.0;
else { // bits per sample
auto iasbd = getInputStreamDescription();
auto oasbd = getOutputStreamDescription();
auto layout = getOutputChannelLayout();
double srate = oasbd.mSampleRate ? oasbd.mSampleRate
: iasbd.mSampleRate;
unsigned nchannels = oasbd.mChannelsPerFrame;
switch (layout->mChannelLayoutTag) {
case kAudioChannelLayoutTag_MPEG_5_1_D:
case kAudioChannelLayoutTag_AAC_6_1:
case kAudioChannelLayoutTag_MPEG_7_1_B:
--nchannels;
}
bitrate *= nchannels * srate;
}
setEncodeBitRate(getClosestAvailableBitRate(bitrate));
}
}
std::string AudioConverterXX::getConfigAsString()
{
std::string s;
auto asbd = getOutputStreamDescription();
UInt32 codec = asbd.mFormatID;
if (codec == 'aac ')
s = "AAC-LC Encoder";
else if (codec == 'aach')
s = "AAC-HE Encoder";
else
s = "Apple Lossless Encoder";
if (codec != 'aac ' && codec != 'aach')
return s;
UInt32 value = getBitRateControlMode();
const char * strategies[] = { "CBR", "ABR", "CVBR", "TVBR" };
s += strutil::format(", %s", strategies[value]);
if (value == kAudioCodecBitRateControlMode_Variable) {
value = getSoundQualityForVBR();
s += strutil::format(" q%d", value);
} else {
value = getEncodeBitRate();
s += strutil::format(" %gkbps", value / 1000.0);
}
value = getCodecQuality();
s += strutil::format(", Quality %d", value);
return s;
}
std::string AudioConverterXX::getEncodingParamsTag()
{
UInt32 mode = getBitRateControlMode();
UInt32 bitrate = getEncodeBitRate();
auto asbd = getOutputStreamDescription();
UInt32 codec = asbd.mFormatID;
AudioComponentDescription cd = { 'aenc', codec, 'appl', 0, 0 };
auto ac = AudioComponentFindNext(nullptr, &cd);
UInt32 vers = 0;
AudioComponentGetVersion(ac, &vers);
char buf[32] = "vers\0\0\0\1acbf\0\0\0\0brat\0\0\0\0cdcv\0\0\0";
buf[12] = mode >> 24;
buf[13] = mode >> 16;
buf[14] = mode >> 8;
buf[15] = mode;
buf[20] = bitrate >> 24;
buf[21] = bitrate >> 16;
buf[22] = bitrate >> 8;
buf[23] = bitrate;
buf[28] = vers >> 24;
buf[29] = vers >> 16;
buf[30] = vers >> 8;
buf[31] = vers;
return std::string(buf, buf + 32);
}