Help with ThreeInputFilter
I'm trying to write an extension of the two input filter to support three inputs. The reason is I have three separate sources that need to be synced up before processing. In this case, it's three videos that i need to make sure are not getting ahead of or behind each other.
I believe I've done most of the changes correctly but the "renderToTextureWithVertices" is giving me problems, most likely due to a naive attempt at extending. There are EXC_BAD_ACCESS errors on glDrawArrays as a result. Code is below.
Any idea of what I'm doing wrong? Is this how a three input filter could be synced up
- (void)renderToTextureWithVertices:(const GLfloat *)vertices textureCoordinates:(const GLfloat *)textureCoordinates sourceTexture:(GLuint)sourceTexture;
{
if (self.preventRendering)
{
return;
}
[GPUImageOpenGLESContext useImageProcessingContext];
[self setFilterFBO];
[filterProgram use];
glClearColor(backgroundColorRed, backgroundColorGreen, backgroundColorBlue, backgroundColorAlpha);
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, sourceTexture);
glUniform1i(filterInputTextureUniform, 2);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, filterSourceTexture2);
glUniform1i(filterInputTextureUniform2, 3);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, filterSourceTexture3);
glUniform1i(filterInputTextureUniform3, 4);
glVertexAttribPointer(filterPositionAttribute, 2, GL_FLOAT, 0, 0, vertices);
glVertexAttribPointer(filterTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, textureCoordinates);
glVertexAttribPointer(filterSecondTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, [[self class] textureCoordinatesForRotation:inputRotation3]);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
If you comment out the lines
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, filterSourceTexture3);
glUniform1i(filterInputTextureUniform3, 4);
does it still crash?
Also, why are you using inputRotation3 instead of inputRotation2 in the second to last line?
Turns out, I was actually missing another line at the end (needed to add a third texture coordinate attribute);
- (void)renderToTextureWithVertices:(const GLfloat *)vertices textureCoordinates:(const GLfloat *)textureCoordinates sourceTexture:(GLuint)sourceTexture;
{
if (self.preventRendering)
{
return;
}
[GPUImageOpenGLESContext useImageProcessingContext];
[self setFilterFBO];
[filterProgram use];
glClearColor(backgroundColorRed, backgroundColorGreen, backgroundColorBlue, backgroundColorAlpha);
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, sourceTexture);
glUniform1i(filterInputTextureUniform, 2);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, filterSourceTexture2);
glUniform1i(filterInputTextureUniform2, 3);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, filterSourceTexture3);
glUniform1i(filterInputTextureUniform3, 4);
glVertexAttribPointer(filterPositionAttribute, 2, GL_FLOAT, 0, 0, vertices);
glVertexAttribPointer(filterTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, textureCoordinates);
glVertexAttribPointer(filterSecondTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, [[self class] textureCoordinatesForRotation:inputRotation2]);
glVertexAttribPointer(filterThirdTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, [[self class] textureCoordinatesForRotation:inputRotation3]);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
Now this is crashing elsewhere but it's not necessarily a glDraw problem. The REAL problem, though, is that after finally getting this going, it's not syncing the videos. They still stutter between one another (which throws off some effects), and their timing in the output is affected by the render speed of the input.
Is there some easy way to "gate" inputs so that we make sure we get them all before moving on, and that as we do so, we're adding exactly one frame to the output? Maybe a hackish timer somewhere in the movie writer?
Would you mind posting this class? I am doing something similar to reduce my filter chain and would like to see if this is a good way of handling it.
here it is, but unfortunately it's not really working for me. It's not really doing a good job of keeping all three inputs in sync. Ideally, each input should be supplying one frame at a time, then all three frames get processed together. But that's not what's happening; they get out of sync.
Anyway, hope you have better luck with it than I did. The name is "CustomChromaFilter" but could be anything, of course.
#import "GPUImageTwoInputFilter.h"
/** Selectively replaces a color in the first image with the second image
*/
@interface CustomChromaFilter : GPUImageTwoInputFilter
{
GLint colorToReplaceUniform, thresholdSensitivityUniform, smoothingUniform;
}
/** The threshold sensitivity controls how similar pixels need to be colored to be replaced
The default value is 0.3
*/
@property(readwrite, nonatomic) GLfloat thresholdSensitivity;
/** The degree of smoothing controls how gradually similar colors are replaced in the image
The default value is 0.1
*/
@property(readwrite, nonatomic) GLfloat smoothing;
/** The color to be replaced is specified using individual red, green, and blue components (normalized to 1.0).
The default is green: (0.0, 1.0, 0.0).
@param redComponent Red component of color to be replaced
@param greenComponent Green component of color to be replaced
@param blueComponent Blue component of color to be replaced
*/
- (void)setColorToReplaceRed:(GLfloat)redComponent green:(GLfloat)greenComponent blue:(GLfloat)blueComponent;
@end
//
// CustomChromaFilter.m
// SimpleVideoFileFilter
//
// Created by Drew McAuliffe on 8/4/12.
// Copyright (c) 2012 Cell Phone. All rights reserved.
//
#import "CustomChromaFilter.h"
// Shader code based on Apple's CIChromaKeyFilter example: https://developer.apple.com/library/mac/#samplecode/CIChromaKeyFilter/Introduction/Intro.html
NSString *const kCustomChromaKeyBlendFragmentShaderString = SHADER_STRING
(
precision highp float;
varying highp vec2 textureCoordinate;
varying highp vec2 textureCoordinate2;
uniform float thresholdSensitivity;
uniform float smoothing;
uniform vec3 colorToReplace;
uniform sampler2D inputImageTexture;
uniform sampler2D inputImageTexture2;
const highp vec3 W = vec3(0.2125, 0.7154, 0.0721);
void main()
{
//vec3 textureColor = texture2D(inputImageTexture, textureCoordinate).rgb;
//vec3 textureColor2 = texture2D(inputImageTexture2, textureCoordinate2).rgb;
vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
vec4 textureColor2 = texture2D(inputImageTexture2, textureCoordinate2);
//float maskY = 0.2989 * colorToReplace.r + 0.5866 * colorToReplace.g + 0.1145 * colorToReplace.b;
float maskY = 0.2989 * colorToReplace.r + 0.5866 * colorToReplace.g + 0.1145 * colorToReplace.b;
float maskCr = 0.7132 * (colorToReplace.r - maskY);
float maskCb = 0.5647 * (colorToReplace.b - maskY);
//float Y = 0.2989 * textureColor.r + 0.5866 * textureColor.g + 0.1145 * textureColor.b;
float Y = 0.2989 * textureColor.r + 0.5866 * textureColor.g + 0.1145 * textureColor.b;
float Cr = 0.7132 * (textureColor.r - Y);
float Cb = 0.5647 * (textureColor.b - Y);
// float blendValue = 1.0 - smoothstep(thresholdSensitivity - smoothing, thresholdSensitivity , abs(Cr - maskCr) + abs(Cb - maskCb));
//float blendValue = 1.0 - smoothstep(thresholdSensitivity, thresholdSensitivity + smoothing, distance(vec2(Cr, Cb), vec2(maskCr, maskCb)));
float blendValue = (1.0 - smoothstep(thresholdSensitivity, thresholdSensitivity + smoothing, distance(vec2(Cr, Cb), vec2(maskCr, maskCb)))) * textureColor.g;
//gl_FragColor = vec4(mix(textureColor, textureColor2, blendValue), 1.0);
vec4 result = mix(textureColor, textureColor2, blendValue);
gl_FragColor = result;
/*
float d = abs(length(abs(colorToReplace.rgb - textureColor2.rgb)));
float edge0 = thresholdSensitivity * (1.0 - smoothing);
float alpha = smoothstep(edge0, thresholdSensitivity, d);
gl_FragColor = mix(textureColor, textureColor2, alpha);
*/
}
);
@implementation CustomChromaFilter
@synthesize thresholdSensitivity = _thresholdSensitivity;
@synthesize smoothing = _smoothing;
- (id)init;
{
if (!(self = [super initWithFragmentShaderFromString:kCustomChromaKeyBlendFragmentShaderString]))
{
return nil;
}
thresholdSensitivityUniform = [filterProgram uniformIndex:@"thresholdSensitivity"];
smoothingUniform = [filterProgram uniformIndex:@"smoothing"];
colorToReplaceUniform = [filterProgram uniformIndex:@"colorToReplace"];
self.thresholdSensitivity = 0.4;
self.smoothing = 0.1;
[self setColorToReplaceRed:0.0 green:1.0 blue:0.0];
return self;
}
#pragma mark -
#pragma mark Accessors
- (void)setColorToReplaceRed:(GLfloat)redComponent green:(GLfloat)greenComponent blue:(GLfloat)blueComponent;
{
GLfloat colorToReplace[3];
colorToReplace[0] = redComponent;
colorToReplace[1] = greenComponent;
colorToReplace[2] = blueComponent;
[GPUImageOpenGLESContext useImageProcessingContext];
[filterProgram use];
glUniform3fv(colorToReplaceUniform, 1, colorToReplace);
}
- (void)setThresholdSensitivity:(CGFloat)newValue;
{
_thresholdSensitivity = newValue;
[GPUImageOpenGLESContext useImageProcessingContext];
[filterProgram use];
glUniform1f(thresholdSensitivityUniform, _thresholdSensitivity);
}
- (void)setSmoothing:(CGFloat)newValue;
{
_smoothing = newValue;
[GPUImageOpenGLESContext useImageProcessingContext];
[filterProgram use];
glUniform1f(smoothingUniform, _smoothing);
}
@end