2 * transition_movit_luma.cpp
3 * Copyright (C) 2014 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 "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"
33 using namespace movit;
35 static int get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
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 );
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 );
47 // Get the properties of the transition
48 mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
50 mlt_service_lock( service );
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 );
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) );
68 uint8_t *a_image, *b_image, *c_image;
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 );
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() );
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 );
89 uint8_t *a_image, *b_image;
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 );
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() );
100 *image = (uint8_t *) service;
102 mlt_service_unlock( service );
106 static mlt_frame process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
108 mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
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 );
117 // If we haven't created the wipe producer or it has changed
119 if ( ( !producer || strcmp( resource, last_resource ) ) || ( invert != last_invert ) ) {
121 char *extension = strrchr( resource, '.' );
123 // Store the last resource now
124 mlt_properties_set( properties, "_resource", resource );
125 mlt_properties_set_int( properties, "_invert", invert );
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, '%' ) ) {
133 sprintf( temp, "%s/lumas/%s/%s", mlt_environment( "MLT_DATA" ), mlt_environment( "MLT_NORMALISATION" ), strchr( resource, '%' ) + 1 );
134 test = fopen( temp, "r" );
138 strcat( temp, ".png" );
139 test = fopen( temp, "r" );
145 strcpy( temp, "colour:0x00000080" );
148 extension = strrchr( resource, '.' );
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" );
156 mlt_filter filter = mlt_factory_filter( profile, "invert", NULL );
158 mlt_producer_attach( producer, filter );
161 mlt_properties_set_data( properties, "instance", producer, 0, (mlt_destructor) mlt_producer_close, NULL );
163 // We may still not have a producer in which case, we do nothing
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 );
175 mlt_frame_push_frame( a_frame, NULL );
178 mlt_frame_push_frame( a_frame, NULL );
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 );
188 mlt_transition transition_movit_luma_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
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 );
196 // Inform apps and framework that this is a video only transition
197 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "_transition_type", 1 );