]> git.sesse.net Git - mlt/blob - src/modules/opengl/transition_movit_overlay.cpp
Add the new opengl module (opengl branch).
[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         EffectChain* chain = GlslManager::get_chain( service );
52         MltInput* a_input = GlslManager::get_input( service );
53         MltInput* b_input = (MltInput*) mlt_properties_get_data( properties, "movit input B", NULL );
54         mlt_image_format output_format = *format;
55
56         // Get the frames' textures
57         GLuint* texture_id[2] = {0, 0};
58         *format = mlt_image_glsl_texture;
59         mlt_frame_get_image( a_frame, (uint8_t**) &texture_id[0], format, width, height, 0 );
60         a_input->useFBOInput( chain, *texture_id[0] );
61         *format = mlt_image_glsl_texture;
62         mlt_frame_get_image( b_frame, (uint8_t**) &texture_id[1], format, width, height, 0 );
63         b_input->useFBOInput( chain, *texture_id[1] );
64
65         // Set resolution to that of the a_frame
66         *width = mlt_properties_get_int( a_props, "width" );
67         *height = mlt_properties_get_int( a_props, "height" );
68
69         // Setup rendering to an FBO
70         GlslManager* glsl = GlslManager::get_instance();
71         glsl_fbo fbo = glsl->get_fbo( *width, *height );
72         if ( output_format == mlt_image_glsl_texture ) {
73                 glsl_texture texture = glsl->get_texture( *width, *height, GL_RGBA );
74
75                 glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
76                 check_error();
77                 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
78                 check_error();
79                 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
80                 check_error();
81
82                 GlslManager::render( service, chain, fbo->fbo, *width, *height );
83
84                 glFinish();
85                 check_error();
86                 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
87                 check_error();
88
89                 *image = (uint8_t*) &texture->texture;
90                 mlt_frame_set_image( a_frame, *image, 0, NULL );
91                 mlt_properties_set_data( properties, "movit.convert", texture, 0,
92                         (mlt_destructor) GlslManager::release_texture, NULL );
93                 *format = output_format;
94         }
95         else {
96                 // Use a PBO to hold the data we read back with glReadPixels()
97                 // (Intel/DRI goes into a slow path if we don't read to PBO)
98                 GLenum gl_format = ( output_format == mlt_image_rgb24a || output_format == mlt_image_opengl )?
99                         GL_RGBA : GL_RGB;
100                 int img_size = *width * *height * ( gl_format == GL_RGB? 3 : 4 );
101                 glsl_pbo pbo = glsl->get_pbo( img_size );
102                 glsl_texture texture = glsl->get_texture( *width, *height, gl_format );
103
104                 if ( fbo && pbo && texture ) {
105                         // Set the FBO
106                         glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
107                         check_error();
108                         glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
109                         check_error();
110                         glBindFramebuffer( GL_FRAMEBUFFER, 0 );
111                         check_error();
112
113                         GlslManager::render( service, chain, fbo->fbo, *width, *height );
114
115                         // Read FBO into PBO
116                         glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, pbo->pbo );
117                         check_error();
118                         glBufferData( GL_PIXEL_PACK_BUFFER_ARB, img_size, NULL, GL_STREAM_READ );
119                         check_error();
120                         glReadPixels( 0, 0, *width, *height, gl_format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0) );
121                         check_error();
122
123                         // Copy from PBO
124                         uint8_t* buf = (uint8_t*) glMapBuffer( GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY );
125                         check_error();
126
127                         *format = gl_format == GL_RGBA ? mlt_image_rgb24a : mlt_image_rgb24;
128                         *image = (uint8_t*) mlt_pool_alloc( img_size );
129                         mlt_frame_set_image( a_frame, *image, img_size, mlt_pool_release );
130                         memcpy( *image, buf, img_size );
131
132                         // Release PBO and FBO
133                         glUnmapBuffer( GL_PIXEL_PACK_BUFFER_ARB );
134                         check_error();
135                         glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, 0 );
136                         check_error();
137                         glBindFramebuffer( GL_FRAMEBUFFER, 0 );
138                         check_error();
139                         glBindTexture( GL_TEXTURE_2D, 0 );
140                         check_error();
141                         GlslManager::release_texture( texture );
142                 }
143                 else {
144                         error = 1;
145                 }
146         }
147         if ( fbo ) GlslManager::release_fbo( fbo );
148
149         return error;
150 }
151
152 static mlt_frame process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
153 {
154         mlt_service service = MLT_TRANSITION_SERVICE(transition);
155
156         if ( !GlslManager::init_chain( service ) ) {
157                 // Create the Movit effect chain
158                 EffectChain* chain = GlslManager::get_chain( service );
159                 mlt_profile profile = mlt_service_profile( service );
160                 Input* b_input = new MltInput( profile->width, profile->height );
161                 ImageFormat output_format;
162                 output_format.color_space = COLORSPACE_sRGB;
163                 output_format.gamma_curve = GAMMA_sRGB;
164                 chain->add_input( b_input );
165                 chain->add_output( output_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED );
166                 chain->set_dither_bits( 8 );
167
168                 Effect* effect = chain->add_effect( new OverlayEffect(),
169                         GlslManager::get_input( service ), b_input );
170
171                 // Save these new input on properties for get_image
172                 mlt_properties_set_data( MLT_TRANSITION_PROPERTIES(transition),
173                         "movit input B", b_input, 0, NULL, NULL );
174         }
175
176         // Push the transition on to the frame
177         mlt_frame_push_service( a_frame, transition );
178
179         // Push the b_frame on to the stack
180         mlt_frame_push_frame( a_frame, b_frame );
181
182         // Push the transition method
183         mlt_frame_push_get_image( a_frame, get_image );
184
185         return a_frame;
186 }
187
188 extern "C"
189 mlt_transition transition_movit_overlay_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
190 {
191         mlt_transition transition = NULL;
192         GlslManager* glsl = GlslManager::get_instance();
193         if ( glsl && ( transition = mlt_transition_new() ) ) {
194                 transition->process = process;
195                 
196                 // Inform apps and framework that this is a video only transition
197                 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "_transition_type", 1 );
198         }
199         return transition;
200 }