This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
CompressAudioCodec.cpp
257 lines (231 loc) · 8.51 KB
/
CompressAudioCodec.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* CompressAudioCodec.cpp
* Created by Graham Booker on 8/14/10.
* This file is part of Perian.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "CompressAudioCodec.h"
#include "CodecIDs.h"
#include "CompressCodecUtils.h"
static const OSType kAllInputFormats[] =
{
kCompressedAC3,
kCompressedMP1,
kCompressedMP2,
kCompressedMP3,
kCompressedDTS,
0,
};
static const UInt32 kIntPCMOutFormatFlag = kLinearPCMFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kLinearPCMFormatFlagIsPacked;
CompressAudioCodec::CompressAudioCodec(AudioComponentInstance inInstance) : FFissionCodec(inInstance)
{
for (int i = 0; kAllInputFormats[i] != CODEC_ID_NONE; i++) {
CAStreamBasicDescription theInputFormat(kAudioStreamAnyRate, kAllInputFormats[i], 0, 1, 0, 0, 0, 0);
AddInputFormat(theInputFormat);
}
// libavcodec outputs 16-bit native-endian integer pcm, so why do conversions ourselves?
CAStreamBasicDescription theOutputFormat(kAudioStreamAnyRate, kAudioFormatLinearPCM, 0, 1, 0, 0, 16, kIntPCMOutFormatFlag);
AddOutputFormat(theOutputFormat);
strippedHeader = NULL;
strippedHeaderSize = 0;
innerCookie = NULL;
innerCookieSize = 0;
actualUnit = NULL;
}
CompressAudioCodec::~CompressAudioCodec()
{
if(strippedHeader)
delete[] strippedHeader;
if(innerCookie)
delete[] innerCookie;
if(actualUnit)
CloseComponent(actualUnit);
}
UInt32 CompressAudioCodec::ParseCookieAtom(const uint8_t* inAtom, UInt32 inAtomMaxSize)
{
if(inAtomMaxSize < 8)
//Invalid; atom must be at least 8 bytes.
return inAtomMaxSize;
const UInt32 *atomElements = reinterpret_cast<const UInt32 *>(inAtom);
UInt32 atomSize = EndianU32_BtoN(atomElements[0]);
UInt32 atomType = EndianU32_BtoN(atomElements[1]);
if(atomSize > inAtomMaxSize)
return inAtomMaxSize;
switch (atomType) {
case 'CpSt': {
//Stripped header
UInt32 headerSize = atomSize - 8;
if(headerSize == 0)
break;
strippedHeaderSize = headerSize;
strippedHeader = new Byte[headerSize];
memcpy(strippedHeader, inAtom+8, headerSize);
}
break;
case 'CpCk': {
//Real cookie
UInt32 cookieSize = atomSize - 8;
if(cookieSize == 0)
break;
innerCookieSize = cookieSize;
innerCookie = new Byte[cookieSize];
memcpy(innerCookie, inAtom+8, cookieSize);
}
break;
default:
break;
}
return atomSize;
}
void CompressAudioCodec::ParseCookie(const uint8_t* inMagicCookie, UInt32 inMagicCookieByteSize)
{
UInt32 offset = 0;
while(offset < inMagicCookieByteSize) {
offset += ParseCookieAtom(inMagicCookie + offset, inMagicCookieByteSize - offset);
}
}
void CompressAudioCodec::Initialize(const AudioStreamBasicDescription* inInputFormat, const AudioStreamBasicDescription* inOutputFormat, const void* inMagicCookie, UInt32 inMagicCookieByteSize)
{
FFissionCodec::Initialize(inInputFormat, inOutputFormat, inMagicCookie, inMagicCookieByteSize);
if(inMagicCookie)
SetMagicCookie(inMagicCookie, inMagicCookieByteSize);
if(mInputFormat.mFormatID != 0 && actualUnit == NULL)
{
OSType original = originalStreamFourCC(mInputFormat.mFormatID);
if(original == 0)
CODEC_THROW(kAudioCodecUnsupportedFormatError);
ComponentDescription desc;
memset(&desc, 0, sizeof(ComponentDescription));
desc.componentType = kAudioDecoderComponentType;
desc.componentSubType = original;
Component component = NULL;
while((component = FindNextComponent(component, &desc)) != NULL)
{
ComponentResult err = OpenAComponent(component, &actualUnit);
if (err)
continue;
AudioStreamBasicDescription input = mInputFormat;
input.mFormatID = original;
err = AudioCodecInitialize(actualUnit, &input, inOutputFormat, innerCookie, innerCookieSize);
if(err == noErr)
break;
}
if (!actualUnit)
CODEC_THROW(kAudioCodecUnsupportedFormatError);
}
else if(actualUnit != NULL)
{
OSType original = originalStreamFourCC(mInputFormat.mFormatID);
AudioStreamBasicDescription input = mInputFormat;
input.mFormatID = original;
AudioCodecInitialize(actualUnit, &input, inOutputFormat, innerCookie, innerCookieSize);
}
}
void CompressAudioCodec::Uninitialize()
{
FFissionCodec::Uninitialize();
if(actualUnit)
AudioCodecUninitialize(actualUnit);
}
void CompressAudioCodec::Reset()
{
if(actualUnit)
AudioCodecReset(actualUnit);
}
void CompressAudioCodec::GetProperty(AudioCodecPropertyID inPropertyID, UInt32& ioPropertyDataSize, void* outPropertyData)
{
if(actualUnit)
{
AudioCodecGetProperty(actualUnit, inPropertyID, &ioPropertyDataSize, outPropertyData);
switch (inPropertyID) {
case kAudioCodecPropertyCurrentInputFormat:
case kAudioCodecPropertySupportedInputFormats:
case kAudioCodecInputFormatsForOutputFormat:
{
int formatCount = ioPropertyDataSize / sizeof(AudioStreamBasicDescription);
AudioStreamBasicDescription *formats = reinterpret_cast<AudioStreamBasicDescription*>(outPropertyData);
for(int i=0; i<formatCount; i++)
{
formats[i].mFormatID = mInputFormat.mFormatID;
}
}
break;
default:
break;
}
}
else
FFissionCodec::GetProperty(inPropertyID, ioPropertyDataSize, outPropertyData);
}
void CompressAudioCodec::SetMagicCookie(const void* inMagicCookieData, UInt32 inMagicCookieDataByteSize)
{
ParseCookie(static_cast<const uint8_t *> (inMagicCookieData), inMagicCookieDataByteSize);
FFissionCodec::SetMagicCookie(inMagicCookieData, inMagicCookieDataByteSize);
if(actualUnit)
{
OSType original = originalStreamFourCC(mInputFormat.mFormatID);
AudioStreamBasicDescription input = mInputFormat;
input.mFormatID = original;
AudioStreamBasicDescription output = mOutputFormat;
AudioCodecInitialize(actualUnit, &input, &output, innerCookie, innerCookieSize);
}
}
void CompressAudioCodec::SetCurrentInputFormat(const AudioStreamBasicDescription& inInputFormat)
{
FFissionCodec::SetCurrentInputFormat(inInputFormat);
}
void CompressAudioCodec::SetCurrentOutputFormat(const AudioStreamBasicDescription& inOutputFormat)
{
FFissionCodec::SetCurrentOutputFormat(inOutputFormat);
}
void CompressAudioCodec::AppendInputData(const void* inInputData, UInt32& ioInputDataByteSize, UInt32& ioNumberPackets, const AudioStreamPacketDescription* inPacketDescription)
{
if(inPacketDescription && ioNumberPackets && actualUnit)
{
UInt32 totalSize = 0;
for(int i=0; i<ioNumberPackets; i++)
totalSize += inPacketDescription[i].mDataByteSize;
Byte *newInputData = new Byte[totalSize + strippedHeaderSize*ioNumberPackets];
AudioStreamPacketDescription *newPackets = new AudioStreamPacketDescription[ioNumberPackets];
UInt32 offset = 0;
for(int i=0; i<ioNumberPackets; i++)
{
memcpy(newInputData + offset, strippedHeader, strippedHeaderSize);
UInt32 packetSize = inPacketDescription[i].mDataByteSize;
memcpy(newInputData + offset + strippedHeaderSize, static_cast<const Byte *>(inInputData) + inPacketDescription[i].mStartOffset, packetSize);
newPackets[i].mDataByteSize = packetSize + strippedHeaderSize;
newPackets[i].mStartOffset = offset;
newPackets[i].mVariableFramesInPacket = inPacketDescription[i].mVariableFramesInPacket;
offset += packetSize + strippedHeaderSize;
}
AudioCodecAppendInputData(actualUnit, newInputData, &offset, &ioNumberPackets, newPackets);
AudioStreamPacketDescription lastPacket = inPacketDescription[ioNumberPackets-1];
ioInputDataByteSize = lastPacket.mStartOffset + lastPacket.mDataByteSize;
delete[] newInputData;
delete[] newPackets;
}
}
UInt32 CompressAudioCodec::ProduceOutputPackets(void* outOutputData, UInt32& ioOutputDataByteSize, UInt32& ioNumberPackets, AudioStreamPacketDescription* outPacketDescription)
{
UInt32 status = noErr;
if(actualUnit)
AudioCodecProduceOutputPackets(actualUnit, outOutputData, &ioOutputDataByteSize, &ioNumberPackets, outPacketDescription, &status);
return status;
}
#pragma GCC visibility push(default)
#include "ACPlugInDispatch.h"
#define CompressAudioCodecEntry CompressAudioDecoderEntry
AUDIOCOMPONENT_ENTRY(AudioCodecFactory, CompressAudioCodec)