]> git.sesse.net Git - mlt/blob - src/modules/opengl/transition_movit_luma.cpp
f4b99bca238066f93d708f87376a8298afa80320
[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                 mlt_properties_set_int( properties, "movit.parms.int.inverse",
68                         !mlt_properties_get_int( properties, "invert" ) );
69
70                 uint8_t *a_image, *b_image, *c_image;
71         
72                 // Get the images.
73                 *format = mlt_image_glsl;
74                 error = mlt_frame_get_image( a_frame, &a_image, format, width, height, writable );
75                 error = mlt_frame_get_image( b_frame, &b_image, format, width, height, writable );
76                 error = mlt_frame_get_image( c_frame, &c_image, format, width, height, writable );
77         
78                 GlslManager::set_effect_input( service, a_frame, (mlt_service) a_image );
79                 GlslManager::set_effect_secondary_input( service, a_frame, (mlt_service) b_image, b_frame );
80                 GlslManager::set_effect_third_input( service, a_frame, (mlt_service) c_image, c_frame );
81                 GlslManager::set_effect( service, a_frame, new LumaMixEffect() );
82         }
83         else
84         {
85                 // Set the Movit parameters.
86                 mlt_properties_set( properties, "movit.parms.int.inverse", NULL );
87                 mlt_properties_set( properties, "movit.parms.float.progress", NULL );
88                 mlt_properties_set( properties, "movit.parms.float.transition_width", NULL );
89                 mlt_properties_set_double( properties, "movit.parms.float.strength_first", reverse ? mix : inverse );
90                 mlt_properties_set_double( properties, "movit.parms.float.strength_second", reverse ? inverse : mix );
91         
92                 uint8_t *a_image, *b_image;
93         
94                 // Get the two images.
95                 *format = mlt_image_glsl;
96                 error = mlt_frame_get_image( a_frame, &a_image, format, width, height, writable );
97                 error = mlt_frame_get_image( b_frame, &b_image, format, width, height, writable );
98         
99                 GlslManager::set_effect_input( service, a_frame, (mlt_service) a_image );
100                 GlslManager::set_effect_secondary_input( service, a_frame, (mlt_service) b_image, b_frame );
101                 GlslManager::set_effect( service, a_frame, new MixEffect() );
102         }
103         *image = (uint8_t *) service;
104         
105         mlt_service_unlock( service );
106         return error;
107 }
108
109 static mlt_frame process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
110 {
111         mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
112
113         // Obtain the wipe producer.
114         char *resource = mlt_properties_get( properties, "resource" );
115         char *last_resource = mlt_properties_get( properties, "_resource" );
116         mlt_producer producer = (mlt_producer) mlt_properties_get_data( properties, "instance", NULL );
117
118         // If we haven't created the wipe producer or it has changed
119         if ( resource )
120         if ( !producer || strcmp( resource, last_resource ) ) {
121                 char temp[ 512 ];
122                 char *extension = strrchr( resource, '.' );
123
124                 // Store the last resource now
125                 mlt_properties_set( properties, "_resource", resource );
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                 }
156                 mlt_properties_set_data( properties, "instance", producer, 0, (mlt_destructor) mlt_producer_close, NULL );
157         }
158         // We may still not have a producer in which case, we do nothing
159         if ( producer ) {
160                 mlt_frame wipe = NULL;
161                 mlt_position position = mlt_transition_get_position( transition, a_frame );
162                 mlt_properties_pass( MLT_PRODUCER_PROPERTIES( producer ), properties, "producer." );
163                 mlt_producer_seek( producer, position );
164                 if ( mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &wipe, 0 ) == 0 ) {
165                         char *name = mlt_properties_get( properties, "_unique_id" );
166                         mlt_properties_set_data( MLT_FRAME_PROPERTIES(a_frame), name, wipe, 0, (mlt_destructor) mlt_frame_close, NULL );
167                         mlt_properties_set_int( MLT_FRAME_PROPERTIES(wipe), "distort", 1 );
168                         mlt_frame_push_frame( a_frame, wipe );
169                 } else {
170                         mlt_frame_push_frame( a_frame, NULL );
171                 }
172         } else {
173                 mlt_frame_push_frame( a_frame, NULL );
174         }
175         mlt_frame_push_frame( a_frame, b_frame );
176         mlt_frame_push_service( a_frame, transition );
177         mlt_frame_push_get_image( a_frame, get_image );
178
179         return a_frame;
180 }
181
182 extern "C"
183 mlt_transition transition_movit_luma_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
184 {
185         mlt_transition transition = NULL;
186         GlslManager* glsl = GlslManager::get_instance();
187         if ( glsl && ( transition = mlt_transition_new() ) ) {
188                 transition->process = process;
189                 mlt_properties_set( MLT_TRANSITION_PROPERTIES( transition ), "resource", arg );
190                 
191                 // Inform apps and framework that this is a video only transition
192                 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "_transition_type", 1 );
193         }
194         return transition;
195 }