]> git.sesse.net Git - mlt/blob - src/modules/opengl/transition_movit_luma.cpp
Add movit.luma transition.
[mlt] / src / modules / opengl / transition_movit_luma.cpp
1 /*
2  * transition_movit_luma.cpp
3  * Copyright (C) 2014 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 <math.h>
24
25 #include "filter_glsl_manager.h"
26 #include <movit/init.h>
27 #include <movit/effect_chain.h>
28 #include <movit/util.h>
29 #include <movit/luma_mix_effect.h>
30 #include <movit/mix_effect.h>
31 #include "mlt_movit_input.h"
32
33 using namespace movit;
34
35 static int get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
36 {
37         int error;
38
39         // Get the transition object
40         mlt_transition transition = (mlt_transition) mlt_frame_pop_service( a_frame );
41         mlt_service service = MLT_TRANSITION_SERVICE( transition );
42
43         // Get the b frame from the stack
44         mlt_frame b_frame = (mlt_frame) mlt_frame_pop_frame( a_frame );
45         mlt_frame c_frame = (mlt_frame) mlt_frame_pop_frame( a_frame );
46
47         // Get the properties of the transition
48         mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
49
50         mlt_service_lock( service );
51
52         // Get the transition parameters
53         mlt_position position = mlt_transition_get_position( transition, a_frame );
54         mlt_position length = mlt_transition_get_length( transition );
55         int reverse = mlt_properties_get_int( properties, "reverse" );
56         double mix = mlt_transition_get_progress( transition, a_frame );
57         double inverse = 1.0 - mix;
58         double softness = mlt_properties_anim_get_double( properties, "softness", position, length );
59
60         if ( c_frame )
61         {
62                 // Set the Movit parameters.
63                 mlt_properties_set( properties, "movit.parms.float.strength_first", NULL );
64                 mlt_properties_set( properties, "movit.parms.float.strength_second", NULL );
65                 mlt_properties_set_double( properties, "movit.parms.float.progress", reverse ? inverse : mix );
66                 mlt_properties_set_double( properties, "movit.parms.float.transition_width", 1.0 / (softness + 1.0e-4) );
67         
68                 uint8_t *a_image, *b_image, *c_image;
69         
70                 // Get the images.
71                 *format = mlt_image_glsl;
72                 error = mlt_frame_get_image( a_frame, &a_image, format, width, height, writable );
73                 error = mlt_frame_get_image( b_frame, &b_image, format, width, height, writable );
74                 error = mlt_frame_get_image( c_frame, &c_image, format, width, height, writable );
75         
76                 GlslManager::set_effect_input( service, a_frame, (mlt_service) a_image );
77                 GlslManager::set_effect_secondary_input( service, a_frame, (mlt_service) b_image, b_frame );
78                 GlslManager::set_effect_third_input( service, a_frame, (mlt_service) c_image, c_frame );
79                 GlslManager::set_effect( service, a_frame, new LumaMixEffect() );
80         }
81         else
82         {
83                 // Set the Movit parameters.
84                 mlt_properties_set( properties, "movit.parms.float.progress", NULL );
85                 mlt_properties_set( properties, "movit.parms.float.transition_width", NULL );
86                 mlt_properties_set_double( properties, "movit.parms.float.strength_first", reverse ? mix : inverse );
87                 mlt_properties_set_double( properties, "movit.parms.float.strength_second", reverse ? inverse : mix );
88         
89                 uint8_t *a_image, *b_image;
90         
91                 // Get the two images.
92                 *format = mlt_image_glsl;
93                 error = mlt_frame_get_image( a_frame, &a_image, format, width, height, writable );
94                 error = mlt_frame_get_image( b_frame, &b_image, format, width, height, writable );
95         
96                 GlslManager::set_effect_input( service, a_frame, (mlt_service) a_image );
97                 GlslManager::set_effect_secondary_input( service, a_frame, (mlt_service) b_image, b_frame );
98                 GlslManager::set_effect( service, a_frame, new MixEffect() );
99         }
100         *image = (uint8_t *) service;
101         
102         mlt_service_unlock( service );
103         return error;
104 }
105
106 static mlt_frame process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
107 {
108         mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
109
110         // Obtain the wipe producer.
111         char *resource = mlt_properties_get( properties, "resource" );
112         char *last_resource = mlt_properties_get( properties, "_resource" );
113         int invert = mlt_properties_get_int( properties, "invert" );
114         int last_invert = mlt_properties_get_int( properties, "_invert" );
115         mlt_producer producer = (mlt_producer) mlt_properties_get_data( properties, "instance", NULL );
116
117         // If we haven't created the wipe producer or it has changed
118         if ( resource )
119         if ( ( !producer || strcmp( resource, last_resource ) ) || ( invert != last_invert ) ) {
120                 char temp[ 512 ];
121                 char *extension = strrchr( resource, '.' );
122
123                 // Store the last resource now
124                 mlt_properties_set( properties, "_resource", resource );
125                 mlt_properties_set_int( properties, "_invert", invert );
126
127                 // This is a hack - the idea is that we can indirectly reference the
128                 // luma modules pgm or png images by a short cut like %luma01.pgm - we then replace
129                 // the % with the full path to the image and use it if it exists, if not, check for
130                 // the file ending in a .png, and failing that, default to a fade in
131                 if ( strchr( resource, '%' ) ) {
132                         FILE *test;
133                         sprintf( temp, "%s/lumas/%s/%s", mlt_environment( "MLT_DATA" ), mlt_environment( "MLT_NORMALISATION" ), strchr( resource, '%' ) + 1 );
134                         test = fopen( temp, "r" );
135
136                         if ( test == NULL )
137                         {
138                                 strcat( temp, ".png" );
139                                 test = fopen( temp, "r" );
140                         }
141
142                         if ( test )
143                                 fclose( test ); 
144                         else
145                                 strcpy( temp, "colour:0x00000080" );
146
147                         resource = temp;
148                         extension = strrchr( resource, '.' );
149                 }
150
151                 mlt_profile profile = mlt_service_profile( MLT_TRANSITION_SERVICE( transition ) );
152                 producer = mlt_factory_producer( profile, NULL, resource );
153                 if ( producer != NULL ) {
154                         mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
155                         if ( invert ) {
156                                 mlt_filter filter = mlt_factory_filter( profile, "invert", NULL );
157                                 if ( filter )
158                                         mlt_producer_attach( producer, filter );
159                         }
160                 }
161                 mlt_properties_set_data( properties, "instance", producer, 0, (mlt_destructor) mlt_producer_close, NULL );
162         }
163         // We may still not have a producer in which case, we do nothing
164         if ( producer ) {
165                 mlt_frame wipe = NULL;
166                 mlt_position position = mlt_transition_get_position( transition, a_frame );
167                 mlt_properties_pass( MLT_PRODUCER_PROPERTIES( producer ), properties, "producer." );
168                 mlt_producer_seek( producer, position );
169                 if ( mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &wipe, 0 ) == 0 ) {
170                         char *name = mlt_properties_get( properties, "_unique_id" );
171                         mlt_properties_set_data( MLT_FRAME_PROPERTIES(a_frame), name, wipe, 0, (mlt_destructor) mlt_frame_close, NULL );
172                         mlt_properties_set_int( MLT_FRAME_PROPERTIES(wipe), "distort", 1 );
173                         mlt_frame_push_frame( a_frame, wipe );
174                 } else {
175                         mlt_frame_push_frame( a_frame, NULL );
176                 }
177         } else {
178                 mlt_frame_push_frame( a_frame, NULL );
179         }
180         mlt_frame_push_frame( a_frame, b_frame );
181         mlt_frame_push_service( a_frame, transition );
182         mlt_frame_push_get_image( a_frame, get_image );
183
184         return a_frame;
185 }
186
187 extern "C"
188 mlt_transition transition_movit_luma_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
189 {
190         mlt_transition transition = NULL;
191         GlslManager* glsl = GlslManager::get_instance();
192         if ( glsl && ( transition = mlt_transition_new() ) ) {
193                 transition->process = process;
194                 mlt_properties_set( MLT_TRANSITION_PROPERTIES( transition ), "resource", arg );
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 }