AV Foundation Layer Instruction
AV Foundation Layer Instruction
AV Foundation Layer Instruction
with AV Foundation
Session 612
Scott G. Johnston
AV Foundation Engineer
These are confidential sessions—please refrain from streaming, blogging, or taking pictures
Agenda
■ Tweening
■ Performance
• Debugging compositions
■ Common pitfalls
Agenda
■ Tweening
■ Performance
• Debugging compositions
■ Common pitfalls
Existing Architecture
AV Foundation editing today
• Available since iOS 4.0
and OS X Lion
• Used in video editing apps
from Apple and in the store
• Video editing
■ Temporal composition
■ Video composition
■ Audio mixing
Possible Today
Wipes, Dissolves, Transforms,…
New Opportunities
OpenGL and Everything Else
Custom Video Compositor
What Is a Video Compositor?
A
B
Composition Model
AVComposition
AVVideoComposition
A B
A B
B A
A
Composition Model
AVComposition
AVVideoComposition
A B
A B
B A
A
Composition Model
AVComposition
AVVideoComposition
A B
A B
B A
A
Composition Model
AVComposition
AVVideoComposition
A B
A B
B A
A
Video Instructions
AVVideoComposition
A B
A B
B A
A
Simple mix
1 source track
Video Instructions
AVVideoComposition
A B
A B
B A
A
Complex mix
>1 source tracks
Video Instructions
AVVideoComposition
A B
A B
B A
A
Compositor
Video Instructions
AVVideoComposition
A B
A B
B A
A
Compositor
Video Instructions
AVVideoComposition
A B
A B
B A
A
Compositor
Video Instructions
AVVideoComposition
A B
A B
B A
A
Compositor
Video Instructions
AVVideoComposition
A B
A B
B A
A
Compositor
Video Instructions
AVVideoComposition
A B
A B
B A
A
Compositor
Video Instructions
AVVideoComposition
A B
A B
B A
A
Compositor
Video Instructions
AVVideoComposition
A B
A B
B A
A
Compositor
Compositor
Compositor
Video Compositor
Compositor
Video Compositor
Instruction
Compositor
Video Compositor
Instruction
Compositor
Video Compositor
Instruction
Compositor
Video Compositor
Instruction
Instruction
Opacity Ramp 1 to 0
Compositor
Agenda
■ Tweening
■ Performance
• Debugging compositions
■ Common pitfalls
Video Compositor
Instruction
Opacity Ramp 1 to 0
Built-In
Compositor
Video Compositor
Instruction
Opacity Ramp 1 to 0
Built-In
Compositor
Video Compositor
Instruction
Opacity Ramp 1 to 0
Built-in
Compositor
Custom Video Compositor
Instruction
Opacity Ramp 1 to 0
Your
Built-in
Code
Compositor
Here
Custom Video Compositor
Instruction
Opacity Ramp 1 to 0
Your
Code
Here
Custom Video Compositor
Instruction
Opacity
Your Ramp
Mixing 1 to 0
Parameters
Your
Code
Here
Custom Video Compositor
Instruction
Request
Instruction
Request
@protocol AVVideoCompositing
Instruction
Request
@protocol AVVideoCompositing
Instruction
Request
@protocol AVVideoCompositionInstruction
@protocol AVVideoCompositing
Instruction
Request
Instruction
Request
startVideoCompositionRequest:
Instruction
Request
startVideoCompositionRequest: finishWithComposedVideoFrame:
Instruction
Request finishWithError:
finishCancelledRequest:
startVideoCompositionRequest: finishWithComposedVideoFrame:
Instruction
Request finishWithError:
finishCancelledRequest:
startVideoCompositionRequest: finishWithComposedVideoFrame:
Instruction
Mixing Parameters
Your
Code
Here
Custom Video Compositor
finishWithError:
Request
finishCancelledRequest:
startVideoCompositionRequest: finishWithComposedVideoFrame:
Instruction
Mixing Parameters
Your
Code
Here
finishWithError:
Request
finishCancelledRequest:
startVideoCompositionRequest: finishWithComposedVideoFrame:
Instruction
Mixing Parameters
Your
Code
Here
Choosing Pixel Formats
finishWithError:
Request
finishCancelledRequest:
startVideoCompositionRequest: finishWithComposedVideoFrame:
Instruction
Mixing Parameters
Your
Code
Here
Choosing Pixel Formats
finishWithError:
Request
finishCancelledRequest:
startVideoCompositionRequest: finishWithComposedVideoFrame:
Instruction
Mixing Parameters
Your
Code
Here
Mixing Parameters
Your
Code
Here
Mixing Parameters
Your
Code
Here
(NSDictionary *) sourcePixelBufferAttributes
Choosing Pixel Formats
finishWithError:
Request
finishCancelledRequest:
startVideoCompositionRequest: finishWithComposedVideoFrame:
Instruction
Mixing Parameters
Your
Code
Here
(NSDictionary *) sourcePixelBufferAttributes
kCVPixelFormatType_32BGRA,...
Choosing Pixel Formats
finishWithError:
Request
finishCancelledRequest:
startVideoCompositionRequest: finishWithComposedVideoFrame:
Instruction
Mixing Parameters
Your
Code
Here
(NSDictionary *) sourcePixelBufferAttributes
Choosing Pixel Formats
finishWithError:
Request
finishCancelledRequest:
startVideoCompositionRequest: finishWithComposedVideoFrame:
Instruction
Mixing Parameters
Your
Code
Here
Mixing Parameters
Your
Code
Here
Mixing Parameters
Your
Code
Here
Mixing Parameters
Your
Code
Here
Mixing Parameters
Your
Code
Here
Mixing Parameters
Your
Code
Here
(NSDictionary *) requiredPixelBufferAttributesForRenderContext
Choosing Pixel Formats
finishWithError:
Request
finishCancelledRequest:
startVideoCompositionRequest: finishWithComposedVideoFrame:
Instruction
Mixing Parameters
Your
Code
Here
(NSDictionary *) requiredPixelBufferAttributesForRenderContext
kCVPixelFormatType_32BGRA,...
Choosing Pixel Formats
finishWithError:
Request
finishCancelledRequest:
startVideoCompositionRequest: finishWithComposedVideoFrame:
Instruction
Mixing Parameters
Your
Code
Here
(NSDictionary *) requiredPixelBufferAttributesForRenderContext
Choosing Pixel Formats
finishWithError:
Request
finishCancelledRequest:
startVideoCompositionRequest: finishWithComposedVideoFrame:
Instruction
Mixing Parameters
Your
Code
Here
Mixing Parameters
Your
Code
Here
Mixing Parameters
Your
Code
Here
[request.renderContext newPixelBuffer]
Choosing Pixel Formats
finishWithError:
Request
finishCancelledRequest:
startVideoCompositionRequest: finishWithComposedVideoFrame:
Instruction
Mixing Parameters
Your
Code
Here
[request.renderContext newPixelBuffer]
Choosing Pixel Formats
finishWithError:
Request
finishCancelledRequest:
startVideoCompositionRequest: finishWithComposedVideoFrame:
Instruction
Mixing Parameters
Your
Code
Here
[request.renderContext newPixelBuffer]
Choosing Pixel Formats
finishWithError:
Request
finishCancelledRequest:
startVideoCompositionRequest: finishWithComposedVideoFrame:
Instruction
Mixing Parameters
Your
Code
Here
Mixing Parameters
Your
Code
Here
Mixing Parameters
Your
Code
Here
Mixing Parameters
Your
Code
Here
Demo
CPU and GPU custom compositors
Agenda
■ Tweening
■ Performance
• Debugging compositions
■ Common pitfalls
Tweening
AVVideoComposition
A B
A B
B A
A
Compositor
Tweening
AVVideoComposition
A B
A B
B A
A
Compositor
Tweening
AVVideoComposition
A B
A B
B A
A
Compositor
Tweening
Tweening
AVVideoComposition
A
B
Instruction
Compositor
Tweening
AVVideoComposition
A
B
Compositor
Tweening
AVVideoComposition
A
B
Compositor
Tweening
AVVideoComposition
A
B
Compositor
Tweening
elapsed = 0 secs
AVVideoComposition
A
B
opacity = 100%
Opacity Ramp 100% to 0%
Compositor
Tweening
elapsed = 0 secs
AVVideoComposition
A
B
opacity = 100%
Opacity Ramp 100% to 0%
■ Tweening
■ Performance
• Debugging compositions
■ Common pitfalls
iOS Apps
Foreground Background
CPU CPU
—
Performance
Instruction properties
@protocol AVVideoCompositionInstruction<NSObject>
{
@property CMPersistentTrackID passthroughTrackID;
@property NSArray *requiredSourceTrackIDs;
@property BOOL containsTweening;
}
Performance
Instruction properties
@protocol AVVideoCompositionInstruction<NSObject>
{
@property CMPersistentTrackID passthroughTrackID;
@property NSArray *requiredSourceTrackIDs;
@property BOOL containsTweening;
}
Performance
passthroughTrackID
AVVideoComposition
A B
A B
B A
A
2 sources 2 sources
Performance
passthroughTrackID
AVVideoComposition
A B
A B
B A
A
AVVideoComposition
A B
A B
B A
A
AVVideoComposition
A B
A B
B A
A
requiredSourceTrackIDs = @[ A ];
Performance
requiredSourceTrackIDs
AVVideoComposition
A B
A B
B A
A
requiredSourceTrackIDs = @[ A ];
Performance
requiredSourceTrackIDs
AVVideoComposition
A B
A B
B A
A
requiredSourceTrackIDs = nil
Performance
requiredSourceTrackIDs
AVVideoComposition
A B
A B
B A
A
Moving picture-in-picture,
same source frames every time
Performance
containsTweening
containsTweening = YES;
Moving picture-in-picture,
same source frames every time
Performance
containsTweening
containsTweening = YES;
Performance
containsTweening
containsTweening = YES;
Static picture-in-picture,
same source frames every time
Performance
containsTweening
containsTweening = YES;
Frame #0
Custom Compositor
Static picture-in-picture,
same source frames every time
Performance
containsTweening
containsTweening = YES;
Frame #0 Frame #1
Static picture-in-picture,
same source frames every time
Performance
containsTweening
containsTweening = YES;
Static picture-in-picture,
same source frames every time
Performance
containsTweening
containsTweening = YES;
Static picture-in-picture,
same source frames every time
Performance
containsTweening
Re-render identical output!
containsTweening = YES;
Static picture-in-picture,
same source frames every time
Performance
containsTweening
Static picture-in-picture,
same source frames every time
Performance
containsTweening
I’m not animating
containsTweening = NO;
Static picture-in-picture,
same source frames every time
Performance
containsTweening
containsTweening = NO;
Static picture-in-picture,
same source frames every time
Performance
containsTweening
containsTweening = NO;
Frame #0
Custom Compositor
Static picture-in-picture,
same source frames every time
Performance
containsTweening
containsTweening = NO;
Frame #0 Frame #1
Custom Compositor
Static picture-in-picture,
same source frames every time
Performance
containsTweening
containsTweening = NO;
Custom Compositor
Static picture-in-picture,
same source frames every time
Performance
containsTweening
Reuse identical output
containsTweening = NO;
Custom Compositor
Static picture-in-picture,
same source frames every time
Performance
Pixel buffer formats
• Performance hit converting sources
■ H.264 decodes to YUV 4:2:0
■ Best performance, work in YUV 4:2:0
■ Tweening
■ Performance
• Debugging compositions
■ Common pitfalls
Debugging Compositions
Debugging Compositions
Common pitfalls
• Gaps between segments
• Misaligned track segments
• Misaligned layer instructions
• Misaligned opacity/audio ramps
• Bogus layer transforms
Debugging Compositions
Common pitfalls
• Gaps between segments
• Misaligned track segments
• Misaligned layer instructions
• Misaligned opacity/audio ramps Oops!
• Bogus layer transforms
A B
B A
B A
Debugging Compositions
Common pitfalls
• Gaps between segments
• Misaligned track segments
• Misaligned layer instructions
So close :-(
• Misaligned opacity/audio ramps
• Bogus layer transforms Whale.m4v
Otters.m4v
Sea.m4v Waves.m4v
Debugging Compositions
Common pitfalls
• Gaps between segments
Not the alignment
• Misaligned track segments we’re looking for!
• Misaligned layer instructions
• Misaligned opacity/audio ramps
• Bogus layer transforms
Sea.m4v
Waves.m4v
B
A
A
Debugging Compositions
Common pitfalls
• Gaps between segments
• Misaligned track segments
• Misaligned layer instructions Overshoot!
• Misaligned opacity/audio ramps
• Bogus layer transforms
B
B
A
Debugging Compositions
Common pitfalls
• Gaps between segments
• Misaligned track segments
• Misaligned layer instructions
• Misaligned opacity/audio ramps
• Bogus layer transforms
Debugging Compositions
Common pitfalls
• Gaps between segments
• Misaligned track segments
• Misaligned layer instructions
• Misaligned opacity/audio ramps
• Bogus layer transforms ?*#!
Demo
AVCompositionDebugViewer
Sample Code
Debugging compositions
• AVCompositionDebugViewer
• Materials available at:
https://developer.apple.com/wwdc/schedule/details.php?id=612
Debugging Compositions
■ Tweening
■ Performance
• Debugging compositions
■ Common pitfalls
Summary
■ Tweening
■ Performance
• Debugging compositions
■ Common pitfalls
Summary
■ Tweening
■ Performance
■ Tweening
■ Performance
• Debugging compositions
■ Common pitfalls
Summary
■ Tweening
■ Performance
• Debugging compositions
■ Common pitfalls
Summary
■ Tweening
■ Performance
• Debugging compositions
■ Common pitfalls
Summary
■ Tweening
■ Performance
• Debugging compositions
■ Common pitfalls
Custom Compositors
Effects, Transitions, Generators
Custom Compositors
Effects, Transitions, Generators
More Information
John Geleynse
Director, Technology Evangelist
[email protected]
Documentation
AVFoundation
http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG
Pacific Heights
Moving to AV Kit and AV Foundation Tuesday 4:30PM
Nob Hill
Preparing and Presenting Media for Accessibility Wednesday 10:15AM
Nob Hill
What's New in Camera Capture Wednesday 11:30AM
Labs
Media Lab B
OS X and iOS Capture Lab Thursday 9:00AM
Media Lab B
AV Foundation Lab Thursday 2:00PM
Media Lab B
AV Foundation Lab Friday 9:00AM