2 * filter_movit_convert.cpp
3 * Copyright (C) 2013 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.
20 #include <framework/mlt.h>
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"
33 static void set_movit_parameters( GlslChain *chain, mlt_service service, mlt_frame frame );
35 static void yuv422_to_yuv422p( uint8_t *yuv422, uint8_t *yuv422p, int width, int height )
38 uint8_t *U = Y + width * height;
39 uint8_t *V = U + width * height / 2;
40 int n = width * height / 2 + 1;
49 static int convert_on_cpu( mlt_frame frame, uint8_t **image, mlt_image_format *format, mlt_image_format output_format )
52 mlt_filter cpu_csc = (mlt_filter) mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "cpu_csc", NULL );
54 int (* save_fp )( mlt_frame self, uint8_t **image, mlt_image_format *input, mlt_image_format output )
55 = frame->convert_image;
56 frame->convert_image = NULL;
57 mlt_filter_process( cpu_csc, frame );
58 error = frame->convert_image( frame, image, format, output_format );
59 frame->convert_image = save_fp;
66 static void delete_chain( EffectChain* chain )
71 static void get_format_from_properties( mlt_properties properties, ImageFormat* image_format, YCbCrFormat* ycbcr_format )
73 switch ( mlt_properties_get_int( properties, "colorspace" ) ) {
75 ycbcr_format->luma_coefficients = YCBCR_REC_601;
79 ycbcr_format->luma_coefficients = YCBCR_REC_709;
83 switch ( mlt_properties_get_int( properties, "color_primaries" ) ) {
85 image_format->color_space = COLORSPACE_REC_601_625;
88 image_format->color_space = COLORSPACE_REC_601_525;
92 image_format->color_space = COLORSPACE_REC_709;
96 image_format->gamma_curve = GAMMA_REC_709;
98 if ( mlt_properties_get_int( properties, "force_full_luma" ) ) {
99 ycbcr_format->full_range = true;
101 ycbcr_format->full_range = ( mlt_properties_get_int( properties, "full_luma" ) == 1 );
104 // TODO: make new frame properties set by producers
105 ycbcr_format->cb_x_position = ycbcr_format->cr_x_position = 0.0f;
106 ycbcr_format->cb_y_position = ycbcr_format->cr_y_position = 0.5f;
109 static void build_fingerprint( mlt_service service, mlt_frame frame, std::string *fingerprint )
111 if ( service == (mlt_service) -1 ) {
112 fingerprint->append( "input" );
116 Effect* effect = GlslManager::get_effect( service, frame );
118 mlt_service input_a = GlslManager::get_effect_input( service, frame );
119 fingerprint->push_back( '(' );
120 build_fingerprint( input_a, frame, fingerprint );
121 fingerprint->push_back( ')' );
125 GlslManager::get_effect_secondary_input( service, frame, &input_b, &frame_b );
127 fingerprint->push_back( '(' );
128 build_fingerprint( input_b, frame_b, fingerprint );
129 fingerprint->push_back( ')' );
132 fingerprint->push_back( '(' );
133 fingerprint->append( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "_unique_id" ) );
135 const char* effect_fingerprint = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "_movit fingerprint" );
136 if ( effect_fingerprint ) {
137 fingerprint->push_back( '[' );
138 fingerprint->append( effect_fingerprint );
139 fingerprint->push_back( ']' );
142 bool disable = mlt_properties_get_int( MLT_SERVICE_PROPERTIES( service ), "movit.parms.int.disable" );
144 fingerprint->push_back( 'd' );
146 fingerprint->push_back( ')' );
149 static Effect* build_movit_chain( mlt_service service, mlt_frame frame, GlslChain *chain )
151 if ( service == (mlt_service) -1 ) {
152 mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
153 MltInput* input = GlslManager::get_input( producer, frame );
154 GlslManager::set_input( producer, frame, NULL );
155 chain->effect_chain->add_input( input );
156 chain->effects.insert(std::make_pair( MLT_SERVICE( producer ), input ) );
160 Effect* effect = GlslManager::get_effect( service, frame );
162 GlslManager::set_effect( service, frame, NULL );
164 mlt_service input_a = GlslManager::get_effect_input( service, frame );
167 GlslManager::get_effect_secondary_input( service, frame, &input_b, &frame_b );
168 Effect *effect_a = build_movit_chain( input_a, frame, chain );
171 Effect *effect_b = build_movit_chain( input_b, frame_b, chain );
172 chain->effect_chain->add_effect( effect, effect_a, effect_b );
174 chain->effect_chain->add_effect( effect, effect_a );
177 chain->effects.insert(std::make_pair( service, effect ) );
181 static void dispose_movit_effects( mlt_service service, mlt_frame frame )
183 if ( service == (mlt_service) -1 ) {
184 mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
185 delete GlslManager::get_input( producer, frame );
186 GlslManager::set_input( producer, frame, NULL );
190 delete GlslManager::get_effect( service, frame );
191 GlslManager::set_effect( service, frame, NULL );
193 mlt_service input_a = GlslManager::get_effect_input( service, frame );
196 GlslManager::get_effect_secondary_input( service, frame, &input_b, &frame_b );
197 dispose_movit_effects( input_a, frame );
200 dispose_movit_effects( input_b, frame_b );
204 static void finalize_movit_chain( mlt_service leaf_service, mlt_frame frame )
206 GlslChain* chain = GlslManager::get_chain( leaf_service );
208 std::string new_fingerprint;
209 build_fingerprint( leaf_service, frame, &new_fingerprint );
211 // Build the chain if needed.
212 if ( !chain || new_fingerprint != chain->fingerprint ) {
213 mlt_log_debug( leaf_service, "=== CREATING NEW CHAIN (old chain=%p, leaf=%p, fingerprint=%s) ===\n", chain, leaf_service, new_fingerprint.c_str() );
214 mlt_profile profile = mlt_service_profile( leaf_service );
215 chain = new GlslChain;
216 chain->effect_chain = new EffectChain(
217 profile->display_aspect_num,
218 profile->display_aspect_den,
219 GlslManager::get_instance()->get_resource_pool()
221 chain->fingerprint = new_fingerprint;
223 build_movit_chain( leaf_service, frame, chain );
224 set_movit_parameters( chain, leaf_service, frame );
225 chain->effect_chain->add_effect( new Mlt::VerticalFlip );
227 ImageFormat output_format;
228 output_format.color_space = COLORSPACE_sRGB;
229 output_format.gamma_curve = GAMMA_sRGB;
230 chain->effect_chain->add_output(output_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
231 chain->effect_chain->set_dither_bits(8);
232 chain->effect_chain->finalize();
234 GlslManager::set_chain( leaf_service, chain );
236 // Delete all the created Effect instances to avoid memory leaks.
237 dispose_movit_effects( leaf_service, frame );
241 static void set_movit_parameters( GlslChain *chain, mlt_service service, mlt_frame frame )
243 if ( service == (mlt_service) -1 ) {
244 mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
245 MltInput* input = (MltInput *) chain->effects[ MLT_PRODUCER_SERVICE( producer ) ];
246 input->set_pixel_data( GlslManager::get_input_pixel_pointer( producer, frame ) );
250 Effect* effect = chain->effects[ service ];
251 mlt_service input_a = GlslManager::get_effect_input( service, frame );
252 set_movit_parameters( chain, input_a, frame );
256 GlslManager::get_effect_secondary_input( service, frame, &input_b, &frame_b );
258 set_movit_parameters( chain, input_b, frame_b );
261 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
262 int count = mlt_properties_count( properties );
263 for (int i = 0; i < count; ++i) {
264 const char *name = mlt_properties_get_name( properties, i );
265 if (strncmp(name, "movit.parms.float.", strlen("movit.parms.float.")) == 0) {
266 bool ok = effect->set_float(name + strlen("movit.parms.float."),
267 mlt_properties_get_double( properties, name ));
270 if (strncmp(name, "movit.parms.int.", strlen("movit.parms.int.")) == 0) {
271 bool ok = effect->set_int(name + strlen("movit.parms.int."),
272 mlt_properties_get_int( properties, name ));
275 if (strncmp(name, "movit.parms.vec3.", strlen("movit.parms.vec3.")) == 0 &&
276 strcmp(name + strlen(name) - 3, "[0]") == 0) {
278 char *name_copy = strdup(name);
279 char *index_char = name_copy + strlen(name_copy) - 2;
280 val[0] = mlt_properties_get_double( properties, name_copy );
282 val[1] = mlt_properties_get_double( properties, name_copy );
284 val[2] = mlt_properties_get_double( properties, name_copy );
285 index_char[-1] = '\0';
286 bool ok = effect->set_vec3(name_copy + strlen("movit.parms.vec3."), val);
290 if (strncmp(name, "movit.parms.vec4.", strlen("movit.parms.vec4.")) == 0 &&
291 strcmp(name + strlen(name) - 3, "[0]") == 0) {
293 char *name_copy = strdup(name);
294 char *index_char = name_copy + strlen(name_copy) - 2;
295 val[0] = mlt_properties_get_double( properties, name_copy );
297 val[1] = mlt_properties_get_double( properties, name_copy );
299 val[2] = mlt_properties_get_double( properties, name_copy );
301 val[3] = mlt_properties_get_double( properties, name_copy );
302 index_char[-1] = '\0';
303 bool ok = effect->set_vec4(name_copy + strlen("movit.parms.vec4."), val);
310 static void dispose_pixel_pointers( GlslChain *chain, mlt_service service, mlt_frame frame )
312 if ( service == (mlt_service) -1 ) {
313 mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
314 MltInput* input = (MltInput *) chain->effects[ MLT_PRODUCER_SERVICE( producer ) ];
315 input->invalidate_pixel_data();
316 mlt_pool_release( GlslManager::get_input_pixel_pointer( producer, frame ) );
320 mlt_service input_a = GlslManager::get_effect_input( service, frame );
321 dispose_pixel_pointers( chain, input_a, frame );
325 GlslManager::get_effect_secondary_input( service, frame, &input_b, &frame_b );
327 dispose_pixel_pointers( chain, input_b, frame_b );
331 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 )
333 GlslManager* glsl = GlslManager::get_instance();
335 if ( output_format == mlt_image_glsl_texture ) {
336 error = glsl->render_frame_texture( chain, frame, width, height, image );
339 error = glsl->render_frame_rgba( chain, frame, width, height, image );
340 if ( !error && output_format != mlt_image_rgb24a ) {
341 *format = mlt_image_rgb24a;
342 error = convert_on_cpu( frame, image, format, output_format );
348 // Create an MltInput for an image with the given format and dimensions.
349 static MltInput* create_input( mlt_properties properties, mlt_image_format format, int aspect_width, int aspect_height, int width, int height )
351 MltInput* input = new MltInput( aspect_width, aspect_height );
352 if ( format == mlt_image_rgb24a || format == mlt_image_opengl ) {
353 // TODO: Get the color space if available.
354 input->useFlatInput( FORMAT_RGBA_POSTMULTIPLIED_ALPHA, width, height );
356 else if ( format == mlt_image_rgb24 ) {
357 // TODO: Get the color space if available.
358 input->useFlatInput( FORMAT_RGB, width, height );
360 else if ( format == mlt_image_yuv420p ) {
361 ImageFormat image_format;
362 YCbCrFormat ycbcr_format;
363 get_format_from_properties( properties, &image_format, &ycbcr_format );
364 ycbcr_format.chroma_subsampling_x = ycbcr_format.chroma_subsampling_y = 2;
365 input->useYCbCrInput( image_format, ycbcr_format, width, height );
367 else if ( format == mlt_image_yuv422 ) {
368 ImageFormat image_format;
369 YCbCrFormat ycbcr_format;
370 get_format_from_properties( properties, &image_format, &ycbcr_format );
371 ycbcr_format.chroma_subsampling_x = 2;
372 ycbcr_format.chroma_subsampling_y = 1;
373 input->useYCbCrInput( image_format, ycbcr_format, width, height );
378 // Make a copy of the given image (allocated using mlt_pool_alloc) suitable
379 // to pass as pixel pointer to an MltInput (created using create_input
380 // with the same parameters), and return that pointer.
381 static uint8_t* make_input_copy( mlt_image_format format, uint8_t *image, int width, int height )
383 int img_size = mlt_image_format_size( format, width, height, NULL );
384 uint8_t* img_copy = (uint8_t*) mlt_pool_alloc( img_size );
385 if ( format == mlt_image_yuv422 ) {
386 yuv422_to_yuv422p( image, img_copy, width, height );
388 memcpy( img_copy, image, img_size );
393 static int convert_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, mlt_image_format output_format )
396 if ( *format == output_format )
399 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
401 mlt_log_debug( NULL, "filter_movit_convert: %s -> %s (%d)\n",
402 mlt_image_format_name( *format ), mlt_image_format_name( output_format ),
403 mlt_frame_get_position( frame ) );
405 // Use CPU if glsl not initialized or not supported.
406 GlslManager* glsl = GlslManager::get_instance();
407 if ( !glsl || !glsl->get_int("glsl_supported" ) )
408 return convert_on_cpu( frame, image, format, output_format );
410 // Do non-GL image conversions on a CPU-based image converter.
411 if ( *format != mlt_image_glsl && output_format != mlt_image_glsl && output_format != mlt_image_glsl_texture )
412 return convert_on_cpu( frame, image, format, output_format );
415 int width = mlt_properties_get_int( properties, "width" );
416 int height = mlt_properties_get_int( properties, "height" );
417 GlslManager::get_instance()->lock_service( frame );
419 // If we're at the beginning of a series of Movit effects, store the input
420 // sent into the chain.
421 if ( output_format == mlt_image_glsl ) {
422 mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
423 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) );
424 MltInput *input = create_input( properties, *format, profile->width, profile->height, width, height );
425 GlslManager::set_input( producer, frame, input );
426 uint8_t *img_copy = make_input_copy( *format, *image, width, height );
427 GlslManager::set_input_pixel_pointer( producer, frame, img_copy );
429 *image = (uint8_t *) -1;
430 mlt_frame_set_image( frame, *image, 0, NULL );
433 // If we're at the _end_ of a series of Movit effects, render the chain.
434 if ( *format == mlt_image_glsl ) {
435 mlt_service leaf_service = (mlt_service) *image;
437 // Construct the chain unless we already have a good one.
438 finalize_movit_chain( leaf_service, frame );
440 // Set per-frame parameters now that we know which Effect instances to set them on.
441 // (finalize_movit_chain may already have done this, though, but twice doesn't hurt.)
442 GlslChain *chain = GlslManager::get_chain( leaf_service );
443 set_movit_parameters( chain, leaf_service, frame );
445 error = movit_render( chain->effect_chain, frame, format, output_format, width, height, image );
447 dispose_pixel_pointers( chain, leaf_service, frame );
450 // If we've been asked to render some frame directly to a texture (without any
451 // effects in-between), we create a new mini-chain to do so.
452 if ( *format != mlt_image_glsl && output_format == mlt_image_glsl_texture ) {
453 // We might already have a texture from a previous conversion from mlt_image_glsl.
454 glsl_texture texture = (glsl_texture) mlt_properties_get_data( properties, "movit.convert.texture", NULL );
455 // XXX: requires a special property set on the frame by the app for now
456 // because we do not have reliable way to clear the texture property
457 // when a downstream filter has changed image.
458 if ( texture && mlt_properties_get_int( properties, "movit.convert.use_texture") ) {
459 *image = (uint8_t*) &texture->texture;
460 mlt_frame_set_image( frame, *image, 0, NULL );
462 // Use a separate chain to convert image in RAM to OpenGL texture.
463 // Use cached chain if available and compatible.
464 Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) );
465 EffectChain *chain = (EffectChain*) producer.get_data( "movit.convert.chain" );
466 MltInput *input = (MltInput*) producer.get_data( "movit.convert.input" );
467 int w = producer.get_int( "movit.convert.width" );
468 int h = producer.get_int( "movit.convert.height" );
469 mlt_image_format f = (mlt_image_format) producer.get_int( "movit.convert.format" );
470 if ( !chain || !input || width != w || height != h || *format != f ) {
471 chain = new EffectChain( width, height );
472 input = create_input( properties, *format, width, height, width, height );
473 chain->add_input( input );
474 chain->add_effect( new Mlt::VerticalFlip() );
475 ImageFormat movit_output_format;
476 movit_output_format.color_space = COLORSPACE_sRGB;
477 movit_output_format.gamma_curve = GAMMA_sRGB;
478 chain->add_output(movit_output_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
479 chain->set_dither_bits(8);
481 producer.set( "movit.convert.chain", chain, 0, (mlt_destructor) delete_chain );
482 producer.set( "movit.convert.input", input, 0, NULL );
483 producer.set( "movit.convert.width", width );
484 producer.set( "movit.convert.height", height );
485 producer.set( "movit.convert.format", *format );
488 if ( *format == mlt_image_yuv422 ) {
489 // We need to convert to planar, which make_input_copy() will do for us.
490 uint8_t *planar = make_input_copy( *format, *image, width, height );
491 input->set_pixel_data( planar );
492 error = movit_render( chain, frame, format, output_format, width, height, image );
493 mlt_pool_release( planar );
495 input->set_pixel_data( *image );
496 error = movit_render( chain, frame, format, output_format, width, height, image );
501 GlslManager::get_instance()->unlock_service( frame );
503 mlt_properties_set_int( properties, "format", output_format );
504 *format = output_format;
509 static mlt_frame process( mlt_filter filter, mlt_frame frame )
511 // Set a default colorspace on the frame if not yet set by the producer.
512 // The producer may still change it during get_image.
513 // This way we do not have to modify each producer to set a valid colorspace.
514 mlt_properties properties = MLT_FRAME_PROPERTIES(frame);
515 if ( mlt_properties_get_int( properties, "colorspace" ) <= 0 )
516 mlt_properties_set_int( properties, "colorspace", mlt_service_profile( MLT_FILTER_SERVICE(filter) )->colorspace );
518 frame->convert_image = convert_image;
520 mlt_filter cpu_csc = (mlt_filter) mlt_properties_get_data( MLT_FILTER_PROPERTIES( filter ), "cpu_csc", NULL );
521 mlt_properties_inc_ref( MLT_FILTER_PROPERTIES(cpu_csc) );
522 mlt_properties_set_data( properties, "cpu_csc", cpu_csc, 0,
523 (mlt_destructor) mlt_filter_close, NULL );
528 static mlt_filter create_filter( mlt_profile profile, const char *effect )
531 char *id = strdup( effect );
532 char *arg = strchr( id, ':' );
536 // The swscale and avcolor_space filters require resolution as arg to test compatibility
537 if ( !strcmp( effect, "avcolor_space" ) )
538 filter = mlt_factory_filter( profile, id, &profile->width );
540 filter = mlt_factory_filter( profile, id, arg );
542 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
549 mlt_filter filter_movit_convert_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
551 mlt_filter filter = NULL;
552 GlslManager* glsl = GlslManager::get_instance();
554 if ( glsl && ( filter = mlt_filter_new() ) )
556 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
557 glsl->add_ref( properties );
559 // XXX avcolor_space is crashing on Windows in this context!
560 mlt_filter cpu_csc = NULL;
562 mlt_filter cpu_csc = create_filter( profile, "avcolor_space" );
565 cpu_csc = create_filter( profile, "imageconvert" );
567 mlt_properties_set_data( MLT_FILTER_PROPERTIES( filter ), "cpu_csc", cpu_csc, 0,
568 (mlt_destructor) mlt_filter_close, NULL );
569 filter->process = process;