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
/
CompressVideoCodec.c
291 lines (240 loc) · 8.74 KB
/
CompressVideoCodec.c
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* CompressVideoCodec.c
* 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 <Carbon/Carbon.h>
#include <QuickTime/QuickTime.h>
#include <libavutil/avutil.h>
#include "PerianResourceIDs.h"
#include "Codecprintf.h"
#include "CommonUtils.h"
#include "CompressCodecUtils.h"
typedef struct {
ComponentInstance self;
ComponentInstance delegateComponent;
ComponentInstance actualCodec;
int strippedHeaderSize;
uint8_t *strippedHeader;
int userDecompressRecordSize;
OSType originalFourCC;
} CompressCodecGlobalRecord, *CompressCodecGlobals;
typedef struct {
void *userDecompressRecord;
Ptr codecData;
} CompressDecompressRecord;
// Setup required for ComponentDispatchHelper.c
#define IMAGECODEC_BASENAME() CompressCodec
#define IMAGECODEC_GLOBALS() CompressCodecGlobals storage
#define CALLCOMPONENT_BASENAME() IMAGECODEC_BASENAME()
#define CALLCOMPONENT_GLOBALS() IMAGECODEC_GLOBALS()
#define COMPONENT_UPP_PREFIX() uppImageCodec
#define COMPONENT_DISPATCH_FILE "CompressVideoCodecDispatch.h"
#define COMPONENT_SELECT_PREFIX() kImageCodec
#define GET_DELEGATE_COMPONENT() (storage->delegateComponent)
#include <CoreServices/Components.k.h>
#include <QuickTime/ImageCodec.k.h>
#pragma GCC visibility push(default)
#include <QuickTime/ComponentDispatchHelper.c>
#pragma GCC visibility pop
static int componentSkipCount = 0;
ComponentResult CompressCodecOpen(CompressCodecGlobals glob, ComponentInstance self)
{
ComponentResult err;
glob = (CompressCodecGlobals)NewPtrClear(sizeof(CompressCodecGlobalRecord));
if((err = MemError())) goto bail;
SetComponentInstanceStorage(self, (Handle)glob);
glob->self = self;
err = OpenADefaultComponent(decompressorComponentType, kBaseCodecType, &glob->delegateComponent);
if (err) goto bail;
ComponentSetTarget(glob->delegateComponent, self);
ComponentDescription desc;
GetComponentInfo((Component)self, &desc, 0, 0, 0);
OSType original = originalStreamFourCC(desc.componentSubType);
if(original != 0)
{
glob->originalFourCC = original;
memset(&desc, 0, sizeof(ComponentDescription));
desc.componentType = decompressorComponentType;
desc.componentSubType = original;
Component component = FindNextComponent(0, &desc);
int currentSkip = 0;
while(currentSkip != componentSkipCount)
{
component = FindNextComponent(component, &desc);
currentSkip++;
}
if(component)
glob->actualCodec = OpenComponent(component);
}
bail:
return err;
}
ComponentResult CompressCodecClose(CompressCodecGlobals glob, ComponentInstance self)
{
if(glob) {
if(glob->actualCodec)
CloseComponent(glob->actualCodec);
if(glob->strippedHeader)
av_freep(&glob->strippedHeader);
DisposePtr((Ptr)glob);
}
return noErr;
}
ComponentResult CompressCodecVersion(CompressCodecGlobals glob)
{
if(glob->actualCodec)
return CallComponentVersion(glob->actualCodec);
return kCompressCodecVersion;
}
ComponentResult CompressCodecTarget(CompressCodecGlobals glob, ComponentInstance target)
{
if(glob->actualCodec)
return CallComponentTarget(glob->actualCodec, target);
return noErr;
}
ComponentResult CompressCodecGetMPWorkFunction(CompressCodecGlobals glob, ComponentMPWorkFunctionUPP *workFunction, void **refCon)
{
if(glob->actualCodec)
return CallComponentGetMPWorkFunction(glob->actualCodec, workFunction, refCon);
return noErr;
}
ComponentResult CompressCodecInitialize(CompressCodecGlobals glob, ImageSubCodecDecompressCapabilities *cap)
{
ComponentResult err = noErr;
cap->decompressRecordSize = sizeof(CompressDecompressRecord);
if(glob->actualCodec)
{
err = ImageCodecInitialize(glob->actualCodec, cap);
if(err == noErr)
glob->userDecompressRecordSize = cap->decompressRecordSize;
}
return err;
}
ComponentResult CompressCodecPreflight(CompressCodecGlobals glob, CodecDecompressParams *p)
{
ImageDescriptionHandle imageDesc = p->imageDescription;
if(isImageDescriptionExtensionPresent(imageDesc, kCompressionSettingsExtension))
{
Handle header = NewHandle(0);
GetImageDescriptionExtension(imageDesc, &header, kCompressionSettingsExtension, 1);
int size = GetHandleSize(header);
if(size > 0)
{
glob->strippedHeaderSize = size;
if (glob->strippedHeader)
av_freep(&glob->strippedHeader);
glob->strippedHeader = malloc(size);
memcpy(glob->strippedHeader, *header, size);
}
DisposeHandle(header);
}
OSType myType = (*imageDesc)->cType;
(*imageDesc)->cType = glob->originalFourCC;
Ptr oldData = p->data;
long oldBufferSize = p->bufferSize;
if(glob->strippedHeaderSize)
{
long newSize = oldBufferSize + glob->strippedHeaderSize;
Ptr newData = NewPtrClear(newSize);
memcpy(newData, glob->strippedHeader, glob->strippedHeaderSize);
memcpy(newData + glob->strippedHeaderSize, oldData, oldBufferSize);
p->data = newData;
p->bufferSize += glob->strippedHeaderSize;
}
ComponentResult err = ImageCodecPreflight(glob->actualCodec, p);
if(err == featureUnsupported)
componentSkipCount++;
else
componentSkipCount=0;
(*imageDesc)->cType = myType;
if(glob->strippedHeaderSize)
{
DisposePtr(p->data);
p->data = oldData;
p->bufferSize = oldBufferSize;
}
return err;
}
ComponentResult CompressCodecBeginBand(CompressCodecGlobals glob, CodecDecompressParams *p, ImageSubCodecDecompressRecord *drp, long flags)
{
CompressDecompressRecord *myDrp = (CompressDecompressRecord *)drp->userDecompressRecord;
myDrp->userDecompressRecord = malloc(glob->userDecompressRecordSize);
long oldBufferSize = p->bufferSize;
long newBufferSize = oldBufferSize + glob->strippedHeaderSize;
Ptr oldCodecData = drp->codecData;
Ptr codecData = NewPtrClear(newBufferSize);
memcpy(codecData, glob->strippedHeader, glob->strippedHeaderSize);
memcpy(codecData + glob->strippedHeaderSize, oldCodecData, p->bufferSize);
myDrp->codecData = codecData;
drp->userDecompressRecord = myDrp->userDecompressRecord;
drp->codecData = codecData;
p->bufferSize = newBufferSize;
ComponentResult err = ImageCodecBeginBand(glob->actualCodec, p, drp, flags);
drp->userDecompressRecord = myDrp;
drp->codecData = oldCodecData;
p->bufferSize = oldBufferSize;
return err;
}
ComponentResult CompressCodecDecodeBand(CompressCodecGlobals glob, ImageSubCodecDecompressRecord *drp, unsigned long flags)
{
CompressDecompressRecord *myDrp = (CompressDecompressRecord *)drp->userDecompressRecord;
Ptr oldCodecData = drp->codecData;
drp->userDecompressRecord = myDrp->userDecompressRecord;
drp->codecData = myDrp->codecData;
ComponentResult err = ImageCodecDecodeBand(glob->actualCodec, drp, flags);
drp->userDecompressRecord = myDrp;
drp->codecData = oldCodecData;
return err;
}
ComponentResult CompressCodecDrawBand(CompressCodecGlobals glob, ImageSubCodecDecompressRecord *drp)
{
CompressDecompressRecord *myDrp = (CompressDecompressRecord *)drp->userDecompressRecord;
Ptr oldCodecData = drp->codecData;
drp->userDecompressRecord = myDrp->userDecompressRecord;
drp->codecData = myDrp->codecData;
ComponentResult err = ImageCodecDrawBand(glob->actualCodec, drp);
drp->userDecompressRecord = myDrp;
drp->codecData = oldCodecData;
return err;
}
ComponentResult CompressCodecEndBand(CompressCodecGlobals glob, ImageSubCodecDecompressRecord *drp, OSErr result, long flags)
{
CompressDecompressRecord *myDrp = (CompressDecompressRecord *)drp->userDecompressRecord;
Ptr oldCodecData = drp->codecData;
drp->userDecompressRecord = myDrp->userDecompressRecord;
drp->codecData = myDrp->codecData;
ComponentResult err = ImageCodecEndBand(glob->actualCodec, drp, result, flags);
av_freep(&myDrp->userDecompressRecord);
av_freep(&myDrp->codecData);
drp->userDecompressRecord = myDrp;
drp->codecData = oldCodecData;
return err;
}
ComponentResult CompressCodecGetCodecInfo(CompressCodecGlobals glob, CodecInfo *info)
{
if(glob->actualCodec)
return ImageCodecGetCodecInfo(glob->actualCodec, info);
return noErr;
}
ComponentResult CompressCodecGetSourceDataGammaLevel(CompressCodecGlobals glob, Fixed *sourceDataGammaLevel)
{
if(glob->actualCodec)
return ImageCodecGetSourceDataGammaLevel(glob->actualCodec, sourceDataGammaLevel);
return noErr;
}