-
Notifications
You must be signed in to change notification settings - Fork 1
/
DVGStackableVideoCompositor.m
451 lines (420 loc) · 21.3 KB
/
DVGStackableVideoCompositor.m
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#import "DVGStackableCompositionInstruction.h"
#import "DVGStackableVideoCompositor.h"
#import "DVGOglEffectKeyframedAnimation.h"
#import "DVGOglEffectBase.h"
#import <CoreVideo/CoreVideo.h>
#import "DVGEasing.h"
static int ddLogLevel = DDLogLevelVerbose;
NSString* kCompEffectOptionExportSize = @"kCompEffectOptionExportSize";
NSString* kCompEffectOptionProgressBlock = @"kCompEffectOptionProgressBlock";
dispatch_queue_t renderingQueue;
dispatch_queue_t renderContextQueue;
@interface DVGStackableVideoCompositor()
{
BOOL _shouldCancelAllRequests;
BOOL _renderContextDidChange;
CVPixelBufferRef _previousBuffer;
AVVideoCompositionRenderContext* _renderContext;
}
@end
static __weak DVGStackableVideoCompositor* g_activeCompositor;
static BOOL g_IsCompositingActive = YES;
@implementation DVGStackableVideoCompositor
#pragma mark - AVVideoCompositing protocol
+ (void) initialize
{
renderingQueue = dispatch_queue_create("com.denivip.DVGStackableVideoCompositor.renderingqueue", DISPATCH_QUEUE_SERIAL);
renderContextQueue = dispatch_queue_create("com.denivip.DVGStackableVideoCompositor.rendercontextqueue", DISPATCH_QUEUE_SERIAL);
}
- (id)init
{
self = [super init];
if (self)
{
_previousBuffer = nil;
_renderContextDidChange = NO;
g_activeCompositor = self;
}
return self;
}
+ (DVGStackableVideoCompositor*)getActiveVideoProcessingCompositor {
return g_activeCompositor;
}
+ (void)enableCompositingGlobally:(BOOL)bEnable {
g_IsCompositingActive = bEnable;
}
- (NSDictionary *)sourcePixelBufferAttributes
{
return @{(NSString *)kCVPixelBufferPixelFormatTypeKey : @[@(kCVPixelFormatType_32BGRA)],
(NSString *)kCVPixelBufferOpenGLCompatibilityKey : @YES};
}
- (NSDictionary *)requiredPixelBufferAttributesForRenderContext
{
return @{(NSString *)kCVPixelBufferPixelFormatTypeKey : @[@(kCVPixelFormatType_32BGRA)],
(NSString *)kCVPixelBufferOpenGLCompatibilityKey : @YES
//(NSString *)kCVPixelBufferWidthKey: @(100),
//(NSString *)kCVPixelBufferHeightKey: @(100)
};
}
- (void)renderContextChanged:(AVVideoCompositionRenderContext *)newRenderContext
{
dispatch_sync(renderContextQueue, ^() {
_renderContext = newRenderContext;
_renderContextDidChange = YES;
});
}
- (void)startVideoCompositionRequest:(AVAsynchronousVideoCompositionRequest *)request
{
@autoreleasepool {
dispatch_async(renderingQueue,^() {
// Check if all pending requests have been cancelled
if (_shouldCancelAllRequests || !g_IsCompositingActive) {
[request finishCancelledRequest];
} else {
NSError *err = nil;
// Get the next rendererd pixel buffer
CVPixelBufferRef resultPixels = [self newRenderedPixelBufferForRequest:request error:&err];
if (resultPixels) {
// The resulting pixelbuffer from OpenGL renderer is passed along to the request
[request finishWithComposedVideoFrame:resultPixels];
CVPixelBufferRelease(resultPixels);
} else {
[request finishWithError:err];
}
}
});
}
}
- (void)cancelAllPendingVideoCompositionRequests
{
// pending requests will call finishCancelledRequest, those already rendering will call finishWithComposedVideoFrame
_shouldCancelAllRequests = YES;
dispatch_barrier_async(renderingQueue, ^() {
// start accepting requests again
_shouldCancelAllRequests = NO;
});
}
- (CVPixelBufferRef)newRenderedPixelBufferForRequest:(AVAsynchronousVideoCompositionRequest *)request error:(NSError **)errOut
{
DVGStackableCompositionInstruction *currentInstruction = request.videoCompositionInstruction;
// tweenFactor indicates how far within that timeRange are we rendering this frame. This is normalized to vary between 0.0 and 1.0.
// 0.0 indicates the time at first frame in that videoComposition timeRange
// 1.0 indicates the time at last frame in that videoComposition timeRange
CGFloat time = CMTimeGetSeconds(request.compositionTime);
CMTime elapsed = CMTimeSubtract(request.compositionTime, request.videoCompositionInstruction.timeRange.start);
float tweenFactor = CMTimeGetSeconds(elapsed) / CMTimeGetSeconds(request.videoCompositionInstruction.timeRange.duration);
currentInstruction.actualRenderSize = _renderContext.size;
currentInstruction.actualRenderTime = time;
currentInstruction.actualRenderProgress = tweenFactor;
currentInstruction.actualRenderTransform = nil;
if(currentInstruction.onBeforeRenderingFrame){
currentInstruction.onBeforeRenderingFrame(currentInstruction);
time = currentInstruction.actualRenderTime;
tweenFactor = currentInstruction.actualRenderProgress;
}
if(_renderContextDidChange){
currentInstruction.actualRenderTransform = [NSValue valueWithCGAffineTransform:_renderContext.renderTransform];
}
DVGStackableCompositionInstructionFrameBufferFabricator ibf = currentInstruction.onTrackFrameNeeded;
if (ibf == nil){
ibf = ^(CMPersistentTrackID effectTrackID){
return [request sourceFrameByTrackID:effectTrackID];
};
}
_renderContextDidChange = NO;
return [DVGStackableVideoCompositor renderSingleFrameWithInstruction:currentInstruction trackFrameFabricator:ibf];
}
+ (CVPixelBufferRef)renderSingleFrameWithInstruction:(DVGStackableCompositionInstruction*)currentInstruction
trackFrameFabricator:(DVGStackableCompositionInstructionFrameBufferFabricator)ibf
{
BOOL isOkRendered = YES;
CVPixelBufferRef prevBuffer = nil;
CVPixelBufferRef dstPixels = nil;
//if(currentInstruction.lastOkRenderedPixels){isOkRendered = NO;}else{// DBG
for(DVGOglEffectBase* renderer in currentInstruction.renderersStack){
CGSize renderSize = currentInstruction.actualRenderSize;
// Destination pixel buffer into which we render the output
//if(renderer.effectRenderingUpscale != 1.0f){
int videoWidth = renderSize.width*renderer.effectRenderingUpscale;
int videoHeight = renderSize.height*renderer.effectRenderingUpscale;
CVPixelBufferPoolRef bufferPool = (__bridge CVPixelBufferPoolRef)[currentInstruction getPixelBufferPoolForWidth:videoWidth andHeight:videoHeight];
CVPixelBufferPoolCreatePixelBuffer(NULL, bufferPool, &dstPixels);
//}else
//{
// dstPixels = [rc newPixelBuffer];
//}
CGSize destinationSize = CGSizeMake(CVPixelBufferGetWidth(dstPixels), CVPixelBufferGetHeight(dstPixels));
// Recompute normalized render transform everytime the render context changes
if (currentInstruction.actualRenderTransform != nil) {
// The renderTransform returned by the renderContext is in X: [0, w] and Y: [0, h] coordinate system
// But since in this sample we render using OpenGLES which has its coordinate system between [-1, 1] we compute a normalized transform
CGAffineTransform renderContextTransform = {renderSize.width/2, 0, 0, renderSize.height/2, renderSize.width/2, renderSize.height/2};
CGAffineTransform destinationTransform = {2/destinationSize.width, 0, 0, 2/destinationSize.height, -1, -1};
CGAffineTransform normalizedRenderTransform = CGAffineTransformConcat(CGAffineTransformConcat(renderContextTransform, [currentInstruction.actualRenderTransform CGAffineTransformValue]), destinationTransform);
[renderer prepareTransform:normalizedRenderTransform];
}
CVPixelBufferRef trackBuffer = nil;
DVGGLRotationMode trackOrientation = kDVGGLNoRotation;
if(ibf && renderer.effectTrackID != kCMPersistentTrackID_Invalid){
trackBuffer = ibf(renderer.effectTrackID);//[request sourceFrameByTrackID:renderer.effectTrackID];
trackOrientation = renderer.effectTrackOrientation;
if(trackBuffer == nil){
isOkRendered = NO;
NSLog(@"No frame for track %i, time: %0.02f. Falling back to last Ok frame", renderer.effectTrackID, currentInstruction.actualRenderTime);
break;
}
}
if(g_IsCompositingActive){
[renderer renderIntoPixelBuffer:dstPixels
prevBuffer:prevBuffer
trackBuffer:trackBuffer
trackOrient:trackOrientation
atTime:currentInstruction.actualRenderTime
withTween:currentInstruction.actualRenderProgress];
}
if(prevBuffer){
CVPixelBufferRelease(prevBuffer);
prevBuffer = nil;
}
prevBuffer = dstPixels;
}
//}
// Do NOT releasing prevBuffer - it is == dstPixels, which will be freed on upper levels
if(isOkRendered){
if(currentInstruction.lastOkRenderedPixels){
CVPixelBufferRelease(currentInstruction.lastOkRenderedPixels);
}
currentInstruction.lastOkRenderedPixels = dstPixels;
CVPixelBufferRetain(currentInstruction.lastOkRenderedPixels);
}else{
if(dstPixels){
CVPixelBufferRelease(dstPixels);
}
dstPixels = currentInstruction.lastOkRenderedPixels;
CVPixelBufferRetain(dstPixels);
}
return dstPixels;
}
+ (AVAssetExportSession*)createExportSessionWithAsset:(AVAsset*)asset andAnimationScene:(DVGKeyframedAnimationScene*)animscene
{
DVGOglEffectKeyframedAnimation* kar = [[DVGOglEffectKeyframedAnimation alloc] init];
kar.animationScene = animscene;
kar.adjustScaleForAspectRatio = YES;
return [DVGStackableVideoCompositor createExportSessionWithAssets:@[asset] andEffectsStack:@[kar] options:nil];
}
+ (AVAssetExportSession*)createExportSessionWithAssets:(NSArray<AVAsset*>*)assets andEffectsStack:(NSArray<DVGOglEffectBase*>*)effstack options:(NSDictionary*)svcOptions
{
if([assets count] == 0){
return nil;
}
AVAsset* asset = [assets objectAtIndex:0];
NSArray* videoTracks = [asset tracksWithMediaType:AVMediaTypeVideo];
if([videoTracks count] == 0){
return nil;
}
CGSize outsize = CGSizeMake(0, 0);
if(svcOptions && [svcOptions valueForKey:kCompEffectOptionExportSize] != nil){
outsize = [[svcOptions valueForKey:kCompEffectOptionExportSize] CGSizeValue];
}
AVAssetTrack* videoTrack = [videoTracks objectAtIndex:0];
CGSize videoSize = [videoTrack naturalSize];
CGAffineTransform videoTransform = [videoTrack preferredTransform];
AVMutableComposition *composition = [AVMutableComposition composition];
DVGGLRotationMode inputRotation = [DVGOglEffectBase orientationForPrefferedTransform:videoTransform andSize:videoSize];
videoSize = [DVGOglEffectBase landscapeSizeForOrientation:inputRotation andSize:videoSize];
if(outsize.width > 0 && outsize.height > 0){
videoSize = outsize;
}
composition.naturalSize = videoSize;
AVMutableVideoComposition *videoComposition = nil;
videoComposition = [AVMutableVideoComposition videoComposition];
[DVGStackableVideoCompositor prepareComposition:composition andVideoComposition:videoComposition andEffectsStack:effstack
forAssets:assets
withSize:videoSize
withOrientation:inputRotation
withOptions:svcOptions];
if (videoComposition) {
AVAssetExportSession* exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
CMTime frameDur = videoTrack.minFrameDuration;// Preserving ORIGINAL fps!
if(CMTIME_IS_INVALID(frameDur)){
frameDur = CMTimeMake(1, 30); // 30 fps
}
// Every videoComposition needs these properties to be set:
videoComposition.frameDuration = frameDur;
videoComposition.renderSize = videoSize;
exportSession.videoComposition = videoComposition;
return exportSession;
}
return nil;
}
+ (AVPlayerItem*)createPlayerItemWithAsset:(AVAsset*)asset andAnimationScene:(DVGKeyframedAnimationScene*)animscene
{
DVGOglEffectKeyframedAnimation* kar = [[DVGOglEffectKeyframedAnimation alloc] init];
kar.animationScene = animscene;
kar.adjustScaleForAspectRatio = YES;
return [DVGStackableVideoCompositor createPlayerItemWithAssets:@[asset] andEffectsStack:@[kar] options:nil];
}
+ (AVPlayerItem*)createPlayerItemWithAssets:(NSArray<AVAsset*>*)assets andEffectsStack:(NSArray<DVGOglEffectBase*>*)effstack options:(NSDictionary*)svcOptions
{
if([assets count] == 0){
return nil;
}
AVAsset* asset = [assets objectAtIndex:0];
NSArray* videoTracks = [asset tracksWithMediaType:AVMediaTypeVideo];
if([videoTracks count] == 0){
return nil;
}
AVAssetTrack* videoTrack = [videoTracks objectAtIndex:0];
CGSize videoSize = [videoTrack naturalSize];
if(videoSize.width < 1 || videoSize.height < 1){
// wtf ??? ??? ??? ???
videoSize = CGSizeMake(1360,768);
}
CGAffineTransform videoTransform = [videoTrack preferredTransform];
AVMutableComposition *composition = [AVMutableComposition composition];
DVGGLRotationMode inputRotation = [DVGOglEffectBase orientationForPrefferedTransform:videoTransform andSize:videoSize];
videoSize = [DVGOglEffectBase landscapeSizeForOrientation:inputRotation andSize:videoSize];
composition.naturalSize = videoSize;
AVMutableVideoComposition *videoComposition = nil;
videoComposition = [AVMutableVideoComposition videoComposition];
[DVGStackableVideoCompositor prepareComposition:composition andVideoComposition:videoComposition andEffectsStack:effstack
forAssets:assets
withSize:videoSize
withOrientation:inputRotation
withOptions:svcOptions];
if (videoComposition) {
// Every videoComposition needs these properties to be set:
videoComposition.frameDuration = CMTimeMake(1, 30); // 30 fps
videoComposition.renderSize = videoSize;
}
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:composition];
playerItem.videoComposition = videoComposition;
return playerItem;
}
+ (BOOL)prepareComposition:(AVMutableComposition *)composition
andVideoComposition:(AVMutableVideoComposition *)videoComposition
andEffectsStack:(NSArray<DVGOglEffectBase*>*)effstack_raw
forAssets:(NSArray<AVAsset*>*)assets
withSize:(CGSize)videoSize
withOrientation:(DVGGLRotationMode)orientation
withOptions:(NSDictionary*)svcOptions
{
if([assets count] == 0){
return NO;
}
AVAsset* asset = [assets objectAtIndex:0];
NSArray* videoTracks = [asset tracksWithMediaType:AVMediaTypeVideo];
if([videoTracks count] == 0){
return NO;
}
AVAssetTrack* videoTrack = [videoTracks objectAtIndex:0];
AVMutableCompositionTrack *compositionVideoTrack;
compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
CMTimeRange timeRangeInAsset = CMTimeRangeMake(kCMTimeZero, [asset duration]);
[compositionVideoTrack insertTimeRange:timeRangeInAsset ofTrack:videoTrack atTime:kCMTimeZero error:nil];
CGSize videoOrigSize = [videoTrack naturalSize];
DDLogWarn(@"prepareComposition: Adding base track, origsize: %0.02f:%0.02f, duration: %0.02fs", videoOrigSize.width, videoOrigSize.height, CMTimeGetSeconds([asset duration]));
NSArray* audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];
if([audioTracks count] > 0){
AVMutableCompositionTrack *compositionAudioTrack;
compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *audioTrack = [audioTracks objectAtIndex:0];
[compositionAudioTrack insertTimeRange:timeRangeInAsset ofTrack:audioTrack atTime:kCMTimeZero error:nil];
}
NSMutableArray *instructions = [NSMutableArray array];
NSArray *effstack = [effstack_raw filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id effect, NSDictionary *bindings) {
return [effect isKindOfClass:[DVGOglEffectBase class]];
}]];
NSMutableArray* compositionTracks = @[@(compositionVideoTrack.trackID)].mutableCopy;
if([assets count] > 1){
for(int ti = 1;ti<[assets count];ti++){
AVAsset* asset2 = [assets objectAtIndex:ti];
NSArray* videoTracks2 = [asset2 tracksWithMediaType:AVMediaTypeVideo];
if([videoTracks2 count] == 0){
continue;
}
AVAssetTrack* videoTrack2 = [videoTracks2 objectAtIndex:0];
AVMutableCompositionTrack *compositionVideoTrack2;
compositionVideoTrack2 = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
CMTimeRange timeRangeInAsset2 = CMTimeRangeMake(kCMTimeZero, [asset2 duration]);
DDLogWarn(@"prepareComposition: Adding track %i, duration: %0.02fs", compositionVideoTrack2.trackID, CMTimeGetSeconds([asset2 duration]));
NSError* insertErr = nil;
[compositionVideoTrack2 insertTimeRange:timeRangeInAsset2 ofTrack:videoTrack2 atTime:kCMTimeZero error:&insertErr];
if(insertErr){
DDLogError(@"prepareComposition: Can`t insert track %@", videoTrack2);
}else{
[compositionTracks addObject:@(compositionVideoTrack2.trackID)];
}
}
}
if([effstack count]>0){
videoComposition.customVideoCompositorClass = [DVGStackableVideoCompositor class];
DVGStackableCompositionInstruction *videoInstruction = [[DVGStackableCompositionInstruction alloc] initProcessingWithSourceTrackIDs:compositionTracks
forTimeRange:timeRangeInAsset];
if(svcOptions != nil){
videoInstruction.onBeforeRenderingFrame = [svcOptions objectForKey:kCompEffectOptionProgressBlock];
}
int renderers = 0;
for(DVGOglEffectBase* renderer in effstack){
NSInteger trackIdx = renderer.effectTrackIndex;
if(renderers == 0 && trackIdx == kCMPersistentTrackID_Invalid)
{
trackIdx = 1;
}
if(trackIdx <= 0 || trackIdx-1 >= [compositionTracks count]){
trackIdx = 0;
}
renderer.effectTrackID = (trackIdx>0)?[[compositionTracks objectAtIndex:trackIdx-1] intValue]:kCMPersistentTrackID_Invalid;
renderer.effectTrackOrientation = orientation;
renderers++;
}
videoInstruction.renderersStack = effstack;
[instructions addObject:videoInstruction];
}else{
AVMutableVideoCompositionInstruction *passThroughInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
passThroughInstruction.timeRange = timeRangeInAsset;
AVMutableVideoCompositionLayerInstruction *passThroughLayer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
passThroughInstruction.layerInstructions = [NSArray arrayWithObject:passThroughLayer];
[instructions addObject:passThroughInstruction];
}
videoComposition.instructions = instructions;
return YES;
}
+ (UIImage*)renderSingleFrameWithImage:(UIImage*)frame andEffectsStack:(NSArray<DVGOglEffectBase*>*)effstack options:(NSDictionary*)svcOptions {
__block UIImage* res = nil;
dispatch_sync(renderingQueue,^() {
if(g_IsCompositingActive){
DVGStackableCompositionInstruction *videoInstruction = [[DVGStackableCompositionInstruction alloc] initProcessingZero];
videoInstruction.actualRenderSize = frame.size;
videoInstruction.actualRenderTime = 0;
videoInstruction.actualRenderProgress = 0;
videoInstruction.actualRenderTransform = nil;
videoInstruction.renderersStack = effstack;
videoInstruction.actualRenderTransform = [NSValue valueWithCGAffineTransform:CGAffineTransformIdentity];
UIImage* frameFlipped = frame;//[DVGOglEffectBase imageWithFlippedRGBOfImage:frame];
CGImageRef frameFlippedCG = [frameFlipped CGImage];
CVPixelBufferRef frameBuffer = [DVGOglEffectBase createPixelBufferFromCGImage:frameFlippedCG];
DVGStackableCompositionInstructionFrameBufferFabricator ibf = ^(CMPersistentTrackID effectTrackID){
return frameBuffer;
};
//CVPixelBufferRef buff = ibf(0);CFRetain(buff);
CVPixelBufferRef buff = [DVGStackableVideoCompositor renderSingleFrameWithInstruction:videoInstruction trackFrameFabricator:ibf];
CGImageRef cgImage = [DVGOglEffectBase createCGImageFromPixelBuffer:buff];
if(cgImage != nil){
res = [UIImage imageWithCGImage:cgImage scale:1.0 orientation:UIImageOrientationUp];
}
if(buff != nil){
CVPixelBufferRelease(buff);
}
if(cgImage != nil){
CGImageRelease(cgImage);
}
if(frameBuffer != nil){
CVPixelBufferRelease(frameBuffer);
}
}
});
return res;
}
@end