]> git.sesse.net Git - mlt/blob - src/modules/opengl/transition_movit_overlay.cpp
5273ab838edc82d202154031ef182c336b475542
[mlt] / src / modules / opengl / transition_movit_overlay.cpp
1 /*
2  * transition_movit_overlay.cpp
3  * Copyright (C) 2013 Dan Dennedy <dan@dennedy.org>
4  *
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.
9  *
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.
14  *
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.
18  */
19
20
21 #include <framework/mlt.h>
22 #include <string.h>
23 #include <assert.h>
24
25 #include "glsl_manager.h"
26 #include <movit/init.h>
27 #include <movit/effect_chain.h>
28 #include <movit/util.h>
29 #include <movit/overlay_effect.h>
30 #include "mlt_movit_input.h"
31 #include "mlt_flip_effect.h"
32
33 static int get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
34 {
35         int error = 0;
36
37         // Get the b frame from the stack
38         mlt_frame b_frame = (mlt_frame) mlt_frame_pop_frame( a_frame );
39
40         // Get the transition object
41         mlt_transition transition = (mlt_transition) mlt_frame_pop_service( a_frame );
42
43         // Get the properties of the transition
44         mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
45
46         // Get the properties of the a frame
47         mlt_properties a_props = MLT_FRAME_PROPERTIES( a_frame );
48
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         MltInput* a_input = GlslManager::get_input( service );
54         MltInput* b_input = (MltInput*) mlt_properties_get_data( properties, "movit input B", NULL );
55         mlt_image_format output_format = *format;
56
57         if ( !chain || !a_input ) {
58                 mlt_service_unlock( service );
59                 return 2;
60         }
61
62         // Get the frames' textures
63         GLuint* texture_id[2] = {0, 0};
64         *format = mlt_image_glsl_texture;
65         mlt_frame_get_image( a_frame, (uint8_t**) &texture_id[0], format, width, height, 0 );
66         a_input->useFBOInput( chain, *texture_id[0] );
67         *format = mlt_image_glsl_texture;
68         mlt_frame_get_image( b_frame, (uint8_t**) &texture_id[1], format, width, height, 0 );
69         b_input->useFBOInput( chain, *texture_id[1] );
70
71         // Set resolution to that of the a_frame
72         *width = mlt_properties_get_int( a_props, "width" );
73         *height = mlt_properties_get_int( a_props, "height" );
74
75         // Setup rendering to an FBO
76         GlslManager* glsl = GlslManager::get_instance();
77         if ( output_format == mlt_image_glsl_texture ) {
78                 error = glsl->render_frame_texture( service, a_frame, *width, *height, image );
79                 *format = output_format;
80         }
81         else {
82                 error = glsl->render_frame_rgba( service, a_frame, *width, *height, image );
83                 *format = mlt_image_rgb24a;
84         }
85         mlt_service_unlock( service );
86
87         return error;
88 }
89
90 static mlt_frame process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
91 {
92         mlt_service service = MLT_TRANSITION_SERVICE(transition);
93
94         if ( !GlslManager::init_chain( service ) ) {
95                 // Create the Movit effect chain
96                 EffectChain* chain = GlslManager::get_chain( service );
97                 mlt_profile profile = mlt_service_profile( service );
98                 Input* b_input = new MltInput( profile->width, profile->height );
99                 ImageFormat output_format;
100                 output_format.color_space = COLORSPACE_sRGB;
101                 output_format.gamma_curve = GAMMA_sRGB;
102                 chain->add_input( b_input );
103                 chain->add_output( output_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED );
104                 chain->set_dither_bits( 8 );
105
106                 chain->add_effect( new OverlayEffect(), GlslManager::get_input( service ), b_input );
107
108                 // Save these new input on properties for get_image
109                 mlt_properties_set_data( MLT_TRANSITION_PROPERTIES(transition),
110                         "movit input B", b_input, 0, NULL, NULL );
111         }
112
113         // Push the transition on to the frame
114         mlt_frame_push_service( a_frame, transition );
115
116         // Push the b_frame on to the stack
117         mlt_frame_push_frame( a_frame, b_frame );
118
119         // Push the transition method
120         mlt_frame_push_get_image( a_frame, get_image );
121
122         return a_frame;
123 }
124
125 extern "C"
126 mlt_transition transition_movit_overlay_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
127 {
128         mlt_transition transition = NULL;
129         GlslManager* glsl = GlslManager::get_instance();
130         if ( glsl && ( transition = mlt_transition_new() ) ) {
131                 transition->process = process;
132                 
133                 // Inform apps and framework that this is a video only transition
134                 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "_transition_type", 1 );
135         }
136         return transition;
137 }