AVCustomEdit/APLCrossDissolveRenderer.m
/* |
Copyright (C) 2017 Apple Inc. All Rights Reserved. |
See LICENSE.txt for this sample’s licensing information |
Abstract: |
APLCrossDissolveRenderer subclass of APLOpenGLRenderer, renders the given source buffers to perform a cross dissolve over the time range of the transition. |
*/ |
#import "APLCrossDissolveRenderer.h" |
@implementation APLCrossDissolveRenderer |
- (void)renderPixelBuffer:(CVPixelBufferRef)destinationPixelBuffer usingForegroundSourceBuffer:(CVPixelBufferRef)foregroundPixelBuffer andBackgroundSourceBuffer:(CVPixelBufferRef)backgroundPixelBuffer forTweenFactor:(float)tween |
{ |
[EAGLContext setCurrentContext:self.currentContext]; |
if (foregroundPixelBuffer != NULL || backgroundPixelBuffer != NULL) { |
CVOpenGLESTextureRef foregroundLumaTexture = [self lumaTextureForPixelBuffer:foregroundPixelBuffer]; |
CVOpenGLESTextureRef foregroundChromaTexture = [self chromaTextureForPixelBuffer:foregroundPixelBuffer]; |
CVOpenGLESTextureRef backgroundLumaTexture = [self lumaTextureForPixelBuffer:backgroundPixelBuffer]; |
CVOpenGLESTextureRef backgroundChromaTexture = [self chromaTextureForPixelBuffer:backgroundPixelBuffer]; |
CVOpenGLESTextureRef destLumaTexture = [self lumaTextureForPixelBuffer:destinationPixelBuffer]; |
CVOpenGLESTextureRef destChromaTexture = [self chromaTextureForPixelBuffer:destinationPixelBuffer]; |
glUseProgram(self.programY); |
// Set the render transform |
GLfloat preferredRenderTransform [] = { |
self.renderTransform.a, self.renderTransform.b, self.renderTransform.tx, 0.0, |
self.renderTransform.c, self.renderTransform.d, self.renderTransform.ty, 0.0, |
0.0, 0.0, 1.0, 0.0, |
0.0, 0.0, 0.0, 1.0, |
}; |
glUniformMatrix4fv(uniforms[UNIFORM_RENDER_TRANSFORM_Y], 1, GL_FALSE, preferredRenderTransform); |
glBindFramebuffer(GL_FRAMEBUFFER, self.offscreenBufferHandle); |
glViewport(0, 0, (int)CVPixelBufferGetWidthOfPlane(destinationPixelBuffer, 0), (int)CVPixelBufferGetHeightOfPlane(destinationPixelBuffer, 0)); |
// Y planes of foreground and background frame are used to render the Y plane of the destination frame |
glActiveTexture(GL_TEXTURE0); |
glBindTexture(CVOpenGLESTextureGetTarget(foregroundLumaTexture), CVOpenGLESTextureGetName(foregroundLumaTexture)); |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
glActiveTexture(GL_TEXTURE1); |
glBindTexture(CVOpenGLESTextureGetTarget(backgroundLumaTexture), CVOpenGLESTextureGetName(backgroundLumaTexture)); |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
// Attach the destination texture as a color attachment to the off screen frame buffer |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, CVOpenGLESTextureGetTarget(destLumaTexture), CVOpenGLESTextureGetName(destLumaTexture), 0); |
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { |
NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER)); |
goto bail; |
} |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
glClear(GL_COLOR_BUFFER_BIT); |
GLfloat quadVertexData1 [] = { |
-1.0, 1.0, |
1.0, 1.0, |
-1.0, -1.0, |
1.0, -1.0, |
}; |
// texture data varies from 0 -> 1, whereas vertex data varies from -1 -> 1 |
GLfloat quadTextureData1 [] = { |
0.5 + quadVertexData1[0]/2, 0.5 + quadVertexData1[1]/2, |
0.5 + quadVertexData1[2]/2, 0.5 + quadVertexData1[3]/2, |
0.5 + quadVertexData1[4]/2, 0.5 + quadVertexData1[5]/2, |
0.5 + quadVertexData1[6]/2, 0.5 + quadVertexData1[7]/2, |
}; |
glUniform1i(uniforms[UNIFORM_Y], 0); |
glVertexAttribPointer(ATTRIB_VERTEX_Y, 2, GL_FLOAT, 0, 0, quadVertexData1); |
glEnableVertexAttribArray(ATTRIB_VERTEX_Y); |
glVertexAttribPointer(ATTRIB_TEXCOORD_Y, 2, GL_FLOAT, 0, 0, quadTextureData1); |
glEnableVertexAttribArray(ATTRIB_TEXCOORD_Y); |
// Blend function to draw the foreground frame |
glEnable(GL_BLEND); |
glBlendFunc(GL_ONE, GL_ZERO); |
// Draw the foreground frame |
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
glUniform1i(uniforms[UNIFORM_Y], 1); |
glVertexAttribPointer(ATTRIB_VERTEX_Y, 2, GL_FLOAT, 0, 0, quadVertexData1); |
glEnableVertexAttribArray(ATTRIB_VERTEX_Y); |
glVertexAttribPointer(ATTRIB_TEXCOORD_Y, 2, GL_FLOAT, 0, 0, quadTextureData1); |
glEnableVertexAttribArray(ATTRIB_TEXCOORD_Y); |
// Blend function to draw the background frame |
glBlendColor(0, 0, 0, tween); |
glBlendFunc(GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA); |
// Draw the background frame |
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
// Perform similar operations as above for the UV plane |
glUseProgram(self.programUV); |
glUniformMatrix4fv(uniforms[UNIFORM_RENDER_TRANSFORM_UV], 1, GL_FALSE, preferredRenderTransform); |
// UV planes of foreground and background frame are used to render the UV plane of the destination frame |
glActiveTexture(GL_TEXTURE2); |
glBindTexture(CVOpenGLESTextureGetTarget(foregroundChromaTexture), CVOpenGLESTextureGetName(foregroundChromaTexture)); |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
glActiveTexture(GL_TEXTURE3); |
glBindTexture(CVOpenGLESTextureGetTarget(backgroundChromaTexture), CVOpenGLESTextureGetName(backgroundChromaTexture)); |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
glViewport(0, 0, (int)CVPixelBufferGetWidthOfPlane(destinationPixelBuffer, 1), (int)CVPixelBufferGetHeightOfPlane(destinationPixelBuffer, 1)); |
// Attach the destination texture as a color attachment to the off screen frame buffer |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, CVOpenGLESTextureGetTarget(destChromaTexture), CVOpenGLESTextureGetName(destChromaTexture), 0); |
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { |
NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER)); |
goto bail; |
} |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
glClear(GL_COLOR_BUFFER_BIT); |
glUniform1i(uniforms[UNIFORM_UV], 2); |
glVertexAttribPointer(ATTRIB_VERTEX_UV, 2, GL_FLOAT, 0, 0, quadVertexData1); |
glEnableVertexAttribArray(ATTRIB_VERTEX_UV); |
glVertexAttribPointer(ATTRIB_TEXCOORD_UV, 2, GL_FLOAT, 0, 0, quadTextureData1); |
glEnableVertexAttribArray(ATTRIB_TEXCOORD_UV); |
glBlendFunc(GL_ONE, GL_ZERO); |
// Draw the foreground frame |
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
glUniform1i(uniforms[UNIFORM_UV], 3); |
glVertexAttribPointer(ATTRIB_VERTEX_UV, 2, GL_FLOAT, 0, 0, quadVertexData1); |
glEnableVertexAttribArray(ATTRIB_VERTEX_UV); |
glVertexAttribPointer(ATTRIB_TEXCOORD_UV, 2, GL_FLOAT, 0, 0, quadTextureData1); |
glEnableVertexAttribArray(ATTRIB_TEXCOORD_UV); |
glBlendColor(0, 0, 0, tween); |
glBlendFunc(GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA); |
// Draw the background frame |
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
glFlush(); |
bail: |
CFRelease(foregroundLumaTexture); |
CFRelease(foregroundChromaTexture); |
CFRelease(backgroundLumaTexture); |
CFRelease(backgroundChromaTexture); |
CFRelease(destLumaTexture); |
CFRelease(destChromaTexture); |
// Periodic texture cache flush every frame |
CVOpenGLESTextureCacheFlush(self.videoTextureCache, 0); |
[EAGLContext setCurrentContext:nil]; |
} |
} |
@end |
Copyright © 2017 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2017-08-17