2 * transition_movit_mix.cpp
3 * Copyright (C) 2013 Dan Dennedy <dan@dennedy.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <framework/mlt.h>
25 #include "glsl_manager.h"
26 #include <movit/init.h>
27 #include <movit/effect_chain.h>
28 #include <movit/util.h>
29 #include <movit/mix_effect.h>
30 #include "mlt_movit_input.h"
31 #include "mlt_flip_effect.h"
33 static int get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
37 // Get the b frame from the stack
38 mlt_frame b_frame = (mlt_frame) mlt_frame_pop_frame( a_frame );
40 // Get the transition object
41 mlt_transition transition = (mlt_transition) mlt_frame_pop_service( a_frame );
43 // Get the properties of the transition
44 mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
46 // Get the properties of the a frame
47 mlt_properties a_props = MLT_FRAME_PROPERTIES( a_frame );
49 // Get the movit objects
50 mlt_service service = MLT_TRANSITION_SERVICE( transition );
51 mlt_service_lock( service );
52 EffectChain* chain = GlslManager::get_chain( service );
53 Effect* effect = (Effect*) mlt_properties_get_data( properties, "movit effect", NULL );
54 MltInput* a_input = GlslManager::get_input( service );
55 MltInput* b_input = (MltInput*) mlt_properties_get_data( properties, "movit input B", NULL );
56 mlt_image_format output_format = *format;
58 if ( !chain || !a_input ) {
59 mlt_service_unlock( service );
63 // Get the transition parameters
64 int reverse = mlt_properties_get_int( properties, "reverse" );
65 double mix = mlt_properties_get( properties, "mix" ) ?
66 mlt_properties_get_double( properties, "mix" ) :
67 mlt_transition_get_progress( transition, a_frame );
68 double inverse = 1.0 - mix;
70 // Set the movit parameters
71 bool ok = effect->set_float( "strength_first", reverse ? mix : inverse );
72 ok |= effect->set_float( "strength_second", reverse ? inverse : mix );
75 // Get the frames' textures
76 GLuint* texture_id[2] = {0, 0};
77 *format = mlt_image_glsl_texture;
78 mlt_frame_get_image( a_frame, (uint8_t**) &texture_id[0], format, width, height, 0 );
79 a_input->useFBOInput( chain, *texture_id[0] );
80 *format = mlt_image_glsl_texture;
81 mlt_frame_get_image( b_frame, (uint8_t**) &texture_id[1], format, width, height, 0 );
82 b_input->useFBOInput( chain, *texture_id[1] );
84 // Set resolution to that of the a_frame
85 *width = mlt_properties_get_int( a_props, "width" );
86 *height = mlt_properties_get_int( a_props, "height" );
88 // Setup rendering to an FBO
89 GlslManager* glsl = GlslManager::get_instance();
90 glsl_fbo fbo = glsl->get_fbo( *width, *height );
91 if ( output_format == mlt_image_glsl_texture ) {
92 glsl_texture texture = glsl->get_texture( *width, *height, GL_RGBA );
94 glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
96 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
98 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
101 GlslManager::render( service, chain, fbo->fbo, *width, *height );
105 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
108 *image = (uint8_t*) &texture->texture;
109 mlt_frame_set_image( a_frame, *image, 0, NULL );
110 mlt_properties_set_data( properties, "movit.convert", texture, 0,
111 (mlt_destructor) GlslManager::release_texture, NULL );
112 *format = output_format;
115 // Use a PBO to hold the data we read back with glReadPixels()
116 // (Intel/DRI goes into a slow path if we don't read to PBO)
117 GLenum gl_format = ( output_format == mlt_image_rgb24a || output_format == mlt_image_opengl )?
119 int img_size = *width * *height * ( gl_format == GL_RGB? 3 : 4 );
120 glsl_pbo pbo = glsl->get_pbo( img_size );
121 glsl_texture texture = glsl->get_texture( *width, *height, gl_format );
123 if ( fbo && pbo && texture ) {
125 glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
127 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
129 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
132 GlslManager::render( service, chain, fbo->fbo, *width, *height );
135 glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, pbo->pbo );
137 glBufferData( GL_PIXEL_PACK_BUFFER_ARB, img_size, NULL, GL_STREAM_READ );
139 glReadPixels( 0, 0, *width, *height, gl_format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0) );
143 uint8_t* buf = (uint8_t*) glMapBuffer( GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY );
146 *format = gl_format == GL_RGBA ? mlt_image_rgb24a : mlt_image_rgb24;
147 *image = (uint8_t*) mlt_pool_alloc( img_size );
148 mlt_frame_set_image( a_frame, *image, img_size, mlt_pool_release );
149 memcpy( *image, buf, img_size );
151 // Release PBO and FBO
152 glUnmapBuffer( GL_PIXEL_PACK_BUFFER_ARB );
154 glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, 0 );
156 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
158 glBindTexture( GL_TEXTURE_2D, 0 );
160 GlslManager::release_texture( texture );
166 if ( fbo ) GlslManager::release_fbo( fbo );
167 mlt_service_unlock( service );
172 static mlt_frame process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
174 mlt_service service = MLT_TRANSITION_SERVICE(transition);
176 if ( !GlslManager::init_chain( service ) ) {
177 // Create the Movit effect chain
178 EffectChain* chain = GlslManager::get_chain( service );
179 mlt_profile profile = mlt_service_profile( service );
180 Input* b_input = new MltInput( profile->width, profile->height );
181 ImageFormat output_format;
182 output_format.color_space = COLORSPACE_sRGB;
183 output_format.gamma_curve = GAMMA_sRGB;
184 chain->add_input( b_input );
185 chain->add_output( output_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED );
186 chain->set_dither_bits( 8 );
188 Effect* effect = chain->add_effect( new MixEffect(),
189 GlslManager::get_input( service ), b_input );
191 // Save these new effects on properties for get_image
192 mlt_properties properties = MLT_TRANSITION_PROPERTIES(transition);
193 mlt_properties_set_data( properties, "movit effect", effect, 0, NULL, NULL );
194 mlt_properties_set_data( properties, "movit input B", b_input, 0, NULL, NULL );
197 // Push the transition on to the frame
198 mlt_frame_push_service( a_frame, transition );
200 // Push the b_frame on to the stack
201 mlt_frame_push_frame( a_frame, b_frame );
203 // Push the transition method
204 mlt_frame_push_get_image( a_frame, get_image );
210 mlt_transition transition_movit_mix_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
212 mlt_transition transition = NULL;
213 GlslManager* glsl = GlslManager::get_instance();
214 if ( glsl && ( transition = mlt_transition_new() ) ) {
215 transition->process = process;
216 mlt_properties_set( MLT_TRANSITION_PROPERTIES( transition ), "mix", arg );
218 // Inform apps and framework that this is a video only transition
219 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "_transition_type", 1 );