]> git.sesse.net Git - mlt/blob - src/modules/opengl/filter_movit_convert.cpp
Adjust for Movit moving into namespace movit.
[mlt] / src / modules / opengl / filter_movit_convert.cpp
1 /*
2  * filter_movit_convert.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 #include <framework/mlt.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <assert.h>
24 #include <string>
25
26 #include "filter_glsl_manager.h"
27 #include <movit/effect_chain.h>
28 #include <movit/util.h>
29 #include "mlt_movit_input.h"
30 #include <mlt++/MltProducer.h>
31 #include "mlt_flip_effect.h"
32
33 using namespace movit;
34
35 static void set_movit_parameters( GlslChain *chain, mlt_service service, mlt_frame frame );
36
37 static void yuv422_to_yuv422p( uint8_t *yuv422, uint8_t *yuv422p, int width, int height )
38 {
39         uint8_t *Y = yuv422p;
40         uint8_t *U = Y + width * height;
41         uint8_t *V = U + width * height / 2;
42         int n = width * height / 2 + 1;
43         while ( --n ) {
44                 *Y++ = *yuv422++;
45                 *U++ = *yuv422++;
46                 *Y++ = *yuv422++;
47                 *V++ = *yuv422++;
48         }
49 }
50
51 static int convert_on_cpu( mlt_frame frame, uint8_t **image, mlt_image_format *format, mlt_image_format output_format )
52 {
53         int error = 0;
54         mlt_filter cpu_csc = (mlt_filter) mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "cpu_csc", NULL );
55         if ( cpu_csc ) {
56                 int (* save_fp )( mlt_frame self, uint8_t **image, mlt_image_format *input, mlt_image_format output )
57                         = frame->convert_image;
58                 frame->convert_image = NULL;
59                 mlt_filter_process( cpu_csc, frame );
60                 error = frame->convert_image( frame, image, format, output_format );
61                 frame->convert_image = save_fp;
62         } else {
63                 error = 1;
64         }
65         return error;
66 }
67
68 static void delete_chain( EffectChain* chain )
69 {
70         delete chain;
71 }
72
73 static void get_format_from_properties( mlt_properties properties, ImageFormat* image_format, YCbCrFormat* ycbcr_format )
74 {
75         switch ( mlt_properties_get_int( properties, "colorspace" ) ) {
76         case 601:
77                 ycbcr_format->luma_coefficients = YCBCR_REC_601;
78                 break;
79         case 709:
80         default:
81                 ycbcr_format->luma_coefficients = YCBCR_REC_709;
82                 break;
83         }
84
85         switch ( mlt_properties_get_int( properties, "color_primaries" ) ) {
86         case 601625:
87                 image_format->color_space = COLORSPACE_REC_601_625;
88                 break;
89         case 601525:
90                 image_format->color_space = COLORSPACE_REC_601_525;
91                 break;
92         case 709:
93         default:
94                 image_format->color_space = COLORSPACE_REC_709;
95                 break;
96         }
97
98         image_format->gamma_curve = GAMMA_REC_709;
99
100         if ( mlt_properties_get_int( properties, "force_full_luma" ) ) {
101                 ycbcr_format->full_range = true;
102         } else {
103                 ycbcr_format->full_range = ( mlt_properties_get_int( properties, "full_luma" ) == 1 );
104         }
105
106         // TODO: make new frame properties set by producers
107         ycbcr_format->cb_x_position = ycbcr_format->cr_x_position = 0.0f;
108         ycbcr_format->cb_y_position = ycbcr_format->cr_y_position = 0.5f;
109 }
110
111 static void build_fingerprint( mlt_service service, mlt_frame frame, std::string *fingerprint )
112 {
113         if ( service == (mlt_service) -1 ) {
114                 fingerprint->append( "input" );
115                 return;
116         }
117
118         Effect* effect = GlslManager::get_effect( service, frame );
119         assert( effect );
120         mlt_service input_a = GlslManager::get_effect_input( service, frame );
121         fingerprint->push_back( '(' );
122         build_fingerprint( input_a, frame, fingerprint );
123         fingerprint->push_back( ')' );
124
125         mlt_frame frame_b;
126         mlt_service input_b;
127         GlslManager::get_effect_secondary_input( service, frame, &input_b, &frame_b );
128         if ( input_b ) {
129                 fingerprint->push_back( '(' );
130                 build_fingerprint( input_b, frame_b, fingerprint );
131                 fingerprint->push_back( ')' );
132         }
133
134         fingerprint->push_back( '(' );
135         fingerprint->append( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "_unique_id" ) );
136
137         const char* effect_fingerprint = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "_movit fingerprint" );
138         if ( effect_fingerprint ) {
139                 fingerprint->push_back( '[' );
140                 fingerprint->append( effect_fingerprint );
141                 fingerprint->push_back( ']' );
142         }
143
144         bool disable = mlt_properties_get_int( MLT_SERVICE_PROPERTIES( service ), "movit.parms.int.disable" );
145         if ( disable ) {
146                 fingerprint->push_back( 'd' );
147         }
148         fingerprint->push_back( ')' );
149 }
150
151 static Effect* build_movit_chain( mlt_service service, mlt_frame frame, GlslChain *chain )
152 {
153         if ( service == (mlt_service) -1 ) {
154                 mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
155                 MltInput* input = GlslManager::get_input( producer, frame );
156                 GlslManager::set_input( producer, frame, NULL );
157                 chain->effect_chain->add_input( input->get_input() );
158                 chain->inputs.insert(std::make_pair( producer, input ) );
159                 return input->get_input();
160         }
161
162         Effect* effect = GlslManager::get_effect( service, frame );
163         assert( effect );
164         GlslManager::set_effect( service, frame, NULL );
165
166         mlt_service input_a = GlslManager::get_effect_input( service, frame );
167         mlt_service input_b;
168         mlt_frame frame_b;
169         GlslManager::get_effect_secondary_input( service, frame, &input_b, &frame_b );
170         Effect *effect_a = build_movit_chain( input_a, frame, chain );
171
172         if ( input_b ) {
173                 Effect *effect_b = build_movit_chain( input_b, frame_b, chain );
174                 chain->effect_chain->add_effect( effect, effect_a, effect_b );
175         } else {
176                 chain->effect_chain->add_effect( effect, effect_a );
177         }
178                 
179         chain->effects.insert(std::make_pair( service, effect ) );
180         return effect;
181 }
182
183 static void dispose_movit_effects( mlt_service service, mlt_frame frame )
184 {
185         if ( service == (mlt_service) -1 ) {
186                 mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
187                 delete GlslManager::get_input( producer, frame );
188                 GlslManager::set_input( producer, frame, NULL );
189                 return;
190         }
191
192         delete GlslManager::get_effect( service, frame );
193         GlslManager::set_effect( service, frame, NULL );
194
195         mlt_service input_a = GlslManager::get_effect_input( service, frame );
196         mlt_service input_b;
197         mlt_frame frame_b;
198         GlslManager::get_effect_secondary_input( service, frame, &input_b, &frame_b );
199         dispose_movit_effects( input_a, frame );
200
201         if ( input_b ) {
202                 dispose_movit_effects( input_b, frame_b );
203         }
204 }
205
206 static void finalize_movit_chain( mlt_service leaf_service, mlt_frame frame )
207 {
208         GlslChain* chain = GlslManager::get_chain( leaf_service );
209
210         std::string new_fingerprint;
211         build_fingerprint( leaf_service, frame, &new_fingerprint );
212
213         // Build the chain if needed.
214         if ( !chain || new_fingerprint != chain->fingerprint ) {
215                 mlt_log_debug( leaf_service, "=== CREATING NEW CHAIN (old chain=%p, leaf=%p, fingerprint=%s) ===\n", chain, leaf_service, new_fingerprint.c_str() );
216                 mlt_profile profile = mlt_service_profile( leaf_service );
217                 chain = new GlslChain;
218                 chain->effect_chain = new EffectChain(
219                         profile->display_aspect_num,
220                         profile->display_aspect_den,
221                         GlslManager::get_instance()->get_resource_pool()
222                 );
223                 chain->fingerprint = new_fingerprint;
224
225                 build_movit_chain( leaf_service, frame, chain );
226                 set_movit_parameters( chain, leaf_service, frame );
227                 chain->effect_chain->add_effect( new Mlt::VerticalFlip );
228
229                 ImageFormat output_format;
230                 output_format.color_space = COLORSPACE_sRGB;
231                 output_format.gamma_curve = GAMMA_sRGB;
232                 chain->effect_chain->add_output(output_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
233                 chain->effect_chain->set_dither_bits(8);
234                 chain->effect_chain->finalize();
235
236                 GlslManager::set_chain( leaf_service, chain );
237         } else {
238                 // Delete all the created Effect instances to avoid memory leaks.
239                 dispose_movit_effects( leaf_service, frame );
240         }
241 }
242
243 static void set_movit_parameters( GlslChain *chain, mlt_service service, mlt_frame frame )
244 {
245         if ( service == (mlt_service) -1 ) {
246                 mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
247                 MltInput* input = chain->inputs[ producer ];
248                 input->set_pixel_data( GlslManager::get_input_pixel_pointer( producer, frame ) );
249                 return;
250         }
251
252         Effect* effect = chain->effects[ service ];
253         mlt_service input_a = GlslManager::get_effect_input( service, frame );
254         set_movit_parameters( chain, input_a, frame );
255
256         mlt_service input_b;
257         mlt_frame frame_b;
258         GlslManager::get_effect_secondary_input( service, frame, &input_b, &frame_b );
259         if ( input_b ) {
260                 set_movit_parameters( chain, input_b, frame_b );
261         }
262                 
263         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
264         int count = mlt_properties_count( properties );
265         for (int i = 0; i < count; ++i) {
266                 const char *name = mlt_properties_get_name( properties, i );
267                 if (strncmp(name, "movit.parms.float.", strlen("movit.parms.float.")) == 0) {
268                         bool ok = effect->set_float(name + strlen("movit.parms.float."),
269                                 mlt_properties_get_double( properties, name ));
270                         assert(ok);
271                 }
272                 if (strncmp(name, "movit.parms.int.", strlen("movit.parms.int.")) == 0) {
273                         bool ok = effect->set_int(name + strlen("movit.parms.int."),
274                                 mlt_properties_get_int( properties, name ));
275                         assert(ok);
276                 }
277                 if (strncmp(name, "movit.parms.vec3.", strlen("movit.parms.vec3.")) == 0 &&
278                     strcmp(name + strlen(name) - 3, "[0]") == 0) {
279                         float val[3];
280                         char *name_copy = strdup(name);
281                         char *index_char = name_copy + strlen(name_copy) - 2;
282                         val[0] = mlt_properties_get_double( properties, name_copy );
283                         *index_char = '1';
284                         val[1] = mlt_properties_get_double( properties, name_copy );
285                         *index_char = '2';
286                         val[2] = mlt_properties_get_double( properties, name_copy );
287                         index_char[-1] = '\0';
288                         bool ok = effect->set_vec3(name_copy + strlen("movit.parms.vec3."), val);
289                         assert(ok);
290                         free(name_copy);
291                 }
292                 if (strncmp(name, "movit.parms.vec4.", strlen("movit.parms.vec4.")) == 0 &&
293                     strcmp(name + strlen(name) - 3, "[0]") == 0) {
294                         float val[4];
295                         char *name_copy = strdup(name);
296                         char *index_char = name_copy + strlen(name_copy) - 2;
297                         val[0] = mlt_properties_get_double( properties, name_copy );
298                         *index_char = '1';
299                         val[1] = mlt_properties_get_double( properties, name_copy );
300                         *index_char = '2';
301                         val[2] = mlt_properties_get_double( properties, name_copy );
302                         *index_char = '3';
303                         val[3] = mlt_properties_get_double( properties, name_copy );
304                         index_char[-1] = '\0';
305                         bool ok = effect->set_vec4(name_copy + strlen("movit.parms.vec4."), val);
306                         assert(ok);
307                         free(name_copy);
308                 }
309         }
310 }
311
312 static void dispose_pixel_pointers( GlslChain *chain, mlt_service service, mlt_frame frame )
313 {
314         if ( service == (mlt_service) -1 ) {
315                 mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
316                 MltInput* input = chain->inputs[ producer ];
317                 input->invalidate_pixel_data();
318                 mlt_pool_release( GlslManager::get_input_pixel_pointer( producer, frame ) );
319                 return;
320         }
321
322         mlt_service input_a = GlslManager::get_effect_input( service, frame );
323         dispose_pixel_pointers( chain, input_a, frame );
324
325         mlt_service input_b;
326         mlt_frame frame_b;
327         GlslManager::get_effect_secondary_input( service, frame, &input_b, &frame_b );
328         if ( input_b ) {
329                 dispose_pixel_pointers( chain, input_b, frame_b );
330         }
331 }
332
333 static int movit_render( EffectChain *chain, mlt_frame frame, mlt_image_format *format, mlt_image_format output_format, int width, int height, uint8_t **image )
334 {
335         GlslManager* glsl = GlslManager::get_instance();
336         int error;
337         if ( output_format == mlt_image_glsl_texture ) {
338                 error = glsl->render_frame_texture( chain, frame, width, height, image );
339         }
340         else {
341                 error = glsl->render_frame_rgba( chain, frame, width, height, image );
342                 if ( !error && output_format != mlt_image_rgb24a ) {
343                         *format = mlt_image_rgb24a;
344                         error = convert_on_cpu( frame, image, format, output_format );
345                 }
346         }
347         return error;
348 }
349
350 // Create an MltInput for an image with the given format and dimensions.
351 static MltInput* create_input( mlt_properties properties, mlt_image_format format, int aspect_width, int aspect_height, int width, int height )
352 {
353         MltInput* input = new MltInput();
354         if ( format == mlt_image_rgb24a || format == mlt_image_opengl ) {
355                 // TODO: Get the color space if available.
356                 input->useFlatInput( FORMAT_RGBA_POSTMULTIPLIED_ALPHA, width, height );
357         }
358         else if ( format == mlt_image_rgb24 ) {
359                 // TODO: Get the color space if available.
360                 input->useFlatInput( FORMAT_RGB, width, height );
361         }
362         else if ( format == mlt_image_yuv420p ) {
363                 ImageFormat image_format;
364                 YCbCrFormat ycbcr_format;
365                 get_format_from_properties( properties, &image_format, &ycbcr_format );
366                 ycbcr_format.chroma_subsampling_x = ycbcr_format.chroma_subsampling_y = 2;
367                 input->useYCbCrInput( image_format, ycbcr_format, width, height );
368         }
369         else if ( format == mlt_image_yuv422 ) {
370                 ImageFormat image_format;
371                 YCbCrFormat ycbcr_format;
372                 get_format_from_properties( properties, &image_format, &ycbcr_format );
373                 ycbcr_format.chroma_subsampling_x = 2;
374                 ycbcr_format.chroma_subsampling_y = 1;
375                 input->useYCbCrInput( image_format, ycbcr_format, width, height );
376         }
377         return input;
378 }
379
380 // Make a copy of the given image (allocated using mlt_pool_alloc) suitable
381 // to pass as pixel pointer to an MltInput (created using create_input
382 // with the same parameters), and return that pointer.
383 static uint8_t* make_input_copy( mlt_image_format format, uint8_t *image, int width, int height )
384 {
385         int img_size = mlt_image_format_size( format, width, height, NULL );
386         uint8_t* img_copy = (uint8_t*) mlt_pool_alloc( img_size );
387         if ( format == mlt_image_yuv422 ) {
388                 yuv422_to_yuv422p( image, img_copy, width, height );
389         } else {
390                 memcpy( img_copy, image, img_size );
391         }
392         return img_copy;
393 }
394
395 static int convert_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, mlt_image_format output_format )
396 {
397         // Nothing to do!
398         if ( *format == output_format )
399                 return 0;
400
401         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
402
403         mlt_log_debug( NULL, "filter_movit_convert: %s -> %s (%d)\n",
404                 mlt_image_format_name( *format ), mlt_image_format_name( output_format ),
405                 mlt_frame_get_position( frame ) );
406
407         // Use CPU if glsl not initialized or not supported.
408         GlslManager* glsl = GlslManager::get_instance();
409         if ( !glsl || !glsl->get_int("glsl_supported" ) )
410                 return convert_on_cpu( frame, image, format, output_format );
411
412         // Do non-GL image conversions on a CPU-based image converter.
413         if ( *format != mlt_image_glsl && output_format != mlt_image_glsl && output_format != mlt_image_glsl_texture )
414                 return convert_on_cpu( frame, image, format, output_format );
415
416         int error = 0;
417         int width = mlt_properties_get_int( properties, "width" );
418         int height = mlt_properties_get_int( properties, "height" );
419         GlslManager::get_instance()->lock_service( frame );
420         
421         // If we're at the beginning of a series of Movit effects, store the input
422         // sent into the chain.
423         if ( output_format == mlt_image_glsl ) {
424                 mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
425                 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) );
426                 MltInput *input = create_input( properties, *format, profile->width, profile->height, width, height );
427                 GlslManager::set_input( producer, frame, input );
428                 uint8_t *img_copy = make_input_copy( *format, *image, width, height );
429                 GlslManager::set_input_pixel_pointer( producer, frame, img_copy );
430
431                 *image = (uint8_t *) -1;
432                 mlt_frame_set_image( frame, *image, 0, NULL );
433         }
434
435         // If we're at the _end_ of a series of Movit effects, render the chain.
436         if ( *format == mlt_image_glsl ) {
437                 mlt_service leaf_service = (mlt_service) *image;
438
439                 // Construct the chain unless we already have a good one.
440                 finalize_movit_chain( leaf_service, frame );
441
442                 // Set per-frame parameters now that we know which Effect instances to set them on.
443                 // (finalize_movit_chain may already have done this, though, but twice doesn't hurt.)
444                 GlslChain *chain = GlslManager::get_chain( leaf_service );
445                 set_movit_parameters( chain, leaf_service, frame );
446
447                 error = movit_render( chain->effect_chain, frame, format, output_format, width, height, image );
448
449                 dispose_pixel_pointers( chain, leaf_service, frame );
450         }
451
452         // If we've been asked to render some frame directly to a texture (without any
453         // effects in-between), we create a new mini-chain to do so.
454         if ( *format != mlt_image_glsl && output_format == mlt_image_glsl_texture ) {
455                 // We might already have a texture from a previous conversion from mlt_image_glsl.
456                 glsl_texture texture = (glsl_texture) mlt_properties_get_data( properties, "movit.convert.texture", NULL );
457                 // XXX: requires a special property set on the frame by the app for now
458                 // because we do not have reliable way to clear the texture property
459                 // when a downstream filter has changed image.
460                 if ( texture && mlt_properties_get_int( properties, "movit.convert.use_texture") ) {
461                         *image = (uint8_t*) &texture->texture;
462                         mlt_frame_set_image( frame, *image, 0, NULL );
463                 } else {
464                         // Use a separate chain to convert image in RAM to OpenGL texture.
465                         // Use cached chain if available and compatible.
466                         Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) );
467                         EffectChain *chain = (EffectChain*) producer.get_data( "movit.convert.chain" );
468                         MltInput *input = (MltInput*) producer.get_data( "movit.convert.input" );
469                         int w = producer.get_int( "movit.convert.width" );
470                         int h = producer.get_int( "movit.convert.height" );
471                         mlt_image_format f = (mlt_image_format) producer.get_int( "movit.convert.format" );
472                         if ( !chain || !input || width != w || height != h || *format != f ) {
473                                 chain = new EffectChain( width, height );
474                                 input = create_input( properties, *format, width, height, width, height );
475                                 chain->add_input( input->get_input() );
476                                 chain->add_effect( new Mlt::VerticalFlip() );
477                                 ImageFormat movit_output_format;
478                                 movit_output_format.color_space = COLORSPACE_sRGB;
479                                 movit_output_format.gamma_curve = GAMMA_sRGB;
480                                 chain->add_output(movit_output_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
481                                 chain->set_dither_bits(8);
482                                 chain->finalize();
483                                 producer.set( "movit.convert.chain", chain, 0, (mlt_destructor) delete_chain );
484                                 producer.set( "movit.convert.input", input, 0, NULL );
485                                 producer.set( "movit.convert.width", width );
486                                 producer.set( "movit.convert.height", height );
487                                 producer.set( "movit.convert.format", *format );
488                         }
489
490                         if ( *format == mlt_image_yuv422 ) {
491                                 // We need to convert to planar, which make_input_copy() will do for us.
492                                 uint8_t *planar = make_input_copy( *format, *image, width, height );
493                                 input->set_pixel_data( planar );
494                                 error = movit_render( chain, frame, format, output_format, width, height, image );
495                                 mlt_pool_release( planar );
496                         } else {
497                                 input->set_pixel_data( *image );
498                                 error = movit_render( chain, frame, format, output_format, width, height, image );
499                         }
500                 }
501         }
502
503         GlslManager::get_instance()->unlock_service( frame );
504
505         mlt_properties_set_int( properties, "format", output_format );
506         *format = output_format;
507
508         return error;
509 }
510
511 static mlt_frame process( mlt_filter filter, mlt_frame frame )
512 {
513         // Set a default colorspace on the frame if not yet set by the producer.
514         // The producer may still change it during get_image.
515         // This way we do not have to modify each producer to set a valid colorspace.
516         mlt_properties properties = MLT_FRAME_PROPERTIES(frame);
517         if ( mlt_properties_get_int( properties, "colorspace" ) <= 0 )
518                 mlt_properties_set_int( properties, "colorspace", mlt_service_profile( MLT_FILTER_SERVICE(filter) )->colorspace );
519
520         frame->convert_image = convert_image;
521
522         mlt_filter cpu_csc = (mlt_filter) mlt_properties_get_data( MLT_FILTER_PROPERTIES( filter ), "cpu_csc", NULL );
523         mlt_properties_inc_ref( MLT_FILTER_PROPERTIES(cpu_csc) );
524         mlt_properties_set_data( properties, "cpu_csc", cpu_csc, 0,
525                 (mlt_destructor) mlt_filter_close, NULL );
526
527         return frame;
528 }
529
530 static mlt_filter create_filter( mlt_profile profile, const char *effect )
531 {
532         mlt_filter filter;
533         char *id = strdup( effect );
534         char *arg = strchr( id, ':' );
535         if ( arg != NULL )
536                 *arg ++ = '\0';
537
538         // The swscale and avcolor_space filters require resolution as arg to test compatibility
539         if ( !strcmp( effect, "avcolor_space" ) )
540                 filter = mlt_factory_filter( profile, id, &profile->width );
541         else
542                 filter = mlt_factory_filter( profile, id, arg );
543         if ( filter )
544                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
545         free( id );
546         return filter;
547 }
548
549 extern "C" {
550
551 mlt_filter filter_movit_convert_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
552 {
553         mlt_filter filter = NULL;
554         GlslManager* glsl = GlslManager::get_instance();
555
556         if ( glsl && ( filter = mlt_filter_new() ) )
557         {
558                 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
559                 glsl->add_ref( properties );
560 #ifdef WIN32
561                 // XXX avcolor_space is crashing on Windows in this context!
562                 mlt_filter cpu_csc = NULL;
563 #else
564                 mlt_filter cpu_csc = create_filter( profile, "avcolor_space" );
565 #endif
566                 if ( !cpu_csc )
567                         cpu_csc = create_filter( profile, "imageconvert" );
568                 if ( cpu_csc )
569                         mlt_properties_set_data( MLT_FILTER_PROPERTIES( filter ), "cpu_csc", cpu_csc, 0,
570                                 (mlt_destructor) mlt_filter_close, NULL );
571                 filter->process = process;
572         }
573         return filter;
574 }
575
576 }