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) );
67 mlt_properties_set_int( properties, "movit.parms.int.inverse",
68 !mlt_properties_get_int( properties, "invert" ) );
70 uint8_t *a_image, *b_image, *c_image;
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 );
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() );
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 );
92 uint8_t *a_image, *b_image;
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 );
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() );
103 *image = (uint8_t *) service;
105 mlt_service_unlock( service );
109 static mlt_frame process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
111 mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
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 );
118 // If we haven't created the wipe producer or it has changed
120 if ( !producer || strcmp( resource, last_resource ) ) {
122 char *extension = strrchr( resource, '.' );
124 // Store the last resource now
125 mlt_properties_set( properties, "_resource", resource );
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_properties_set_data( properties, "instance", producer, 0, (mlt_destructor) mlt_producer_close, NULL );
158 // We may still not have a producer in which case, we do nothing
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 );
170 mlt_frame_push_frame( a_frame, NULL );
173 mlt_frame_push_frame( a_frame, NULL );
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 );
183 mlt_transition transition_movit_luma_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
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 );
191 // Inform apps and framework that this is a video only transition
192 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "_transition_type", 1 );