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 mlt_position position = mlt_transition_get_position( transition, a_frame );
65 mlt_position length = mlt_transition_get_length( transition );
66 int reverse = mlt_properties_get_int( properties, "reverse" );
67 double mix = mlt_properties_get( properties, "mix" ) ?
68 mlt_properties_anim_get_double( properties, "mix", position, length ) :
69 mlt_transition_get_progress( transition, a_frame );
70 double inverse = 1.0 - mix;
72 // Set the movit parameters
73 bool ok = effect->set_float( "strength_first", reverse ? mix : inverse );
74 ok |= effect->set_float( "strength_second", reverse ? inverse : mix );
77 // Get the frames' textures
78 GLuint* texture_id[2] = {0, 0};
79 *format = mlt_image_glsl_texture;
80 mlt_frame_get_image( a_frame, (uint8_t**) &texture_id[0], format, width, height, 0 );
81 a_input->useFBOInput( chain, *texture_id[0] );
82 *format = mlt_image_glsl_texture;
83 mlt_frame_get_image( b_frame, (uint8_t**) &texture_id[1], format, width, height, 0 );
84 b_input->useFBOInput( chain, *texture_id[1] );
86 // Set resolution to that of the a_frame
87 *width = mlt_properties_get_int( a_props, "width" );
88 *height = mlt_properties_get_int( a_props, "height" );
90 // Setup rendering to an FBO
91 GlslManager* glsl = GlslManager::get_instance();
92 if ( output_format == mlt_image_glsl_texture ) {
93 error = glsl->render_frame_texture( service, a_frame, *width, *height, image );
94 *format = output_format;
97 error = glsl->render_frame_rgba( service, a_frame, *width, *height, image );
98 *format = mlt_image_rgb24a;
100 mlt_service_unlock( service );
105 static mlt_frame process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
107 mlt_service service = MLT_TRANSITION_SERVICE(transition);
109 if ( !GlslManager::init_chain( service ) ) {
110 // Create the Movit effect chain
111 EffectChain* chain = GlslManager::get_chain( service );
112 mlt_profile profile = mlt_service_profile( service );
113 Input* b_input = new MltInput( profile->width, profile->height );
114 ImageFormat output_format;
115 output_format.color_space = COLORSPACE_sRGB;
116 output_format.gamma_curve = GAMMA_sRGB;
117 chain->add_input( b_input );
118 chain->add_output( output_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED );
119 chain->set_dither_bits( 8 );
121 Effect* effect = chain->add_effect( new MixEffect(),
122 GlslManager::get_input( service ), b_input );
124 // Save these new effects on properties for get_image
125 mlt_properties properties = MLT_TRANSITION_PROPERTIES(transition);
126 mlt_properties_set_data( properties, "movit effect", effect, 0, NULL, NULL );
127 mlt_properties_set_data( properties, "movit input B", b_input, 0, NULL, NULL );
130 // Push the transition on to the frame
131 mlt_frame_push_service( a_frame, transition );
133 // Push the b_frame on to the stack
134 mlt_frame_push_frame( a_frame, b_frame );
136 // Push the transition method
137 mlt_frame_push_get_image( a_frame, get_image );
143 mlt_transition transition_movit_mix_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
145 mlt_transition transition = NULL;
146 GlslManager* glsl = GlslManager::get_instance();
147 if ( glsl && ( transition = mlt_transition_new() ) ) {
148 transition->process = process;
149 mlt_properties_set( MLT_TRANSITION_PROPERTIES( transition ), "mix", arg );
151 // Inform apps and framework that this is a video only transition
152 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "_transition_type", 1 );