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 using namespace movit;
35 static void set_movit_parameters( GlslChain *chain, mlt_service service, mlt_frame frame );
37 static void yuv422_to_yuv422p( uint8_t *yuv422, uint8_t *yuv422p, int width, int height )
40 uint8_t *U = Y + width * height;
41 uint8_t *V = U + width * height / 2;
42 int n = width * height / 2 + 1;
51 static int convert_on_cpu( mlt_frame frame, uint8_t **image, mlt_image_format *format, mlt_image_format output_format )
54 mlt_filter cpu_csc = (mlt_filter) mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "cpu_csc", NULL );
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;
68 static void delete_chain( EffectChain* chain )
73 static void get_format_from_properties( mlt_properties properties, ImageFormat* image_format, YCbCrFormat* ycbcr_format )
75 switch ( mlt_properties_get_int( properties, "colorspace" ) ) {
77 ycbcr_format->luma_coefficients = YCBCR_REC_601;
81 ycbcr_format->luma_coefficients = YCBCR_REC_709;
85 switch ( mlt_properties_get_int( properties, "color_primaries" ) ) {
87 image_format->color_space = COLORSPACE_REC_601_625;
90 image_format->color_space = COLORSPACE_REC_601_525;
94 image_format->color_space = COLORSPACE_REC_709;
98 image_format->gamma_curve = GAMMA_REC_709;
100 if ( mlt_properties_get_int( properties, "force_full_luma" ) ) {
101 ycbcr_format->full_range = true;
103 ycbcr_format->full_range = ( mlt_properties_get_int( properties, "full_luma" ) == 1 );
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;
111 static void build_fingerprint( mlt_service service, mlt_frame frame, std::string *fingerprint )
113 if ( service == (mlt_service) -1 ) {
114 fingerprint->append( "input" );
118 Effect* effect = GlslManager::get_effect( service, frame );
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( ')' );
127 GlslManager::get_effect_secondary_input( service, frame, &input_b, &frame_b );
129 fingerprint->push_back( '(' );
130 build_fingerprint( input_b, frame_b, fingerprint );
131 fingerprint->push_back( ')' );
134 fingerprint->push_back( '(' );
135 fingerprint->append( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "_unique_id" ) );
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( ']' );
144 bool disable = mlt_properties_get_int( MLT_SERVICE_PROPERTIES( service ), "movit.parms.int.disable" );
146 fingerprint->push_back( 'd' );
148 fingerprint->push_back( ')' );
151 static Effect* build_movit_chain( mlt_service service, mlt_frame frame, GlslChain *chain )
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();
162 Effect* effect = GlslManager::get_effect( service, frame );
164 GlslManager::set_effect( service, frame, NULL );
166 mlt_service input_a = GlslManager::get_effect_input( service, frame );
169 GlslManager::get_effect_secondary_input( service, frame, &input_b, &frame_b );
170 Effect *effect_a = build_movit_chain( input_a, frame, chain );
173 Effect *effect_b = build_movit_chain( input_b, frame_b, chain );
174 chain->effect_chain->add_effect( effect, effect_a, effect_b );
176 chain->effect_chain->add_effect( effect, effect_a );
179 chain->effects.insert(std::make_pair( service, effect ) );
183 static void dispose_movit_effects( mlt_service service, mlt_frame frame )
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 );
192 delete GlslManager::get_effect( service, frame );
193 GlslManager::set_effect( service, frame, NULL );
195 mlt_service input_a = GlslManager::get_effect_input( service, frame );
198 GlslManager::get_effect_secondary_input( service, frame, &input_b, &frame_b );
199 dispose_movit_effects( input_a, frame );
202 dispose_movit_effects( input_b, frame_b );
206 static void finalize_movit_chain( mlt_service leaf_service, mlt_frame frame )
208 GlslChain* chain = GlslManager::get_chain( leaf_service );
210 std::string new_fingerprint;
211 build_fingerprint( leaf_service, frame, &new_fingerprint );
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()
223 chain->fingerprint = new_fingerprint;
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 );
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();
236 GlslManager::set_chain( leaf_service, chain );
238 // Delete all the created Effect instances to avoid memory leaks.
239 dispose_movit_effects( leaf_service, frame );
243 static void set_movit_parameters( GlslChain *chain, mlt_service service, mlt_frame frame )
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 ) );
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 );
258 GlslManager::get_effect_secondary_input( service, frame, &input_b, &frame_b );
260 set_movit_parameters( chain, input_b, frame_b );
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 ));
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 ));
277 if (strncmp(name, "movit.parms.vec3.", strlen("movit.parms.vec3.")) == 0 &&
278 strcmp(name + strlen(name) - 3, "[0]") == 0) {
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 );
284 val[1] = mlt_properties_get_double( properties, name_copy );
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);
292 if (strncmp(name, "movit.parms.vec4.", strlen("movit.parms.vec4.")) == 0 &&
293 strcmp(name + strlen(name) - 3, "[0]") == 0) {
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 );
299 val[1] = mlt_properties_get_double( properties, name_copy );
301 val[2] = mlt_properties_get_double( properties, name_copy );
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);
312 static void dispose_pixel_pointers( GlslChain *chain, mlt_service service, mlt_frame frame )
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 ) );
322 mlt_service input_a = GlslManager::get_effect_input( service, frame );
323 dispose_pixel_pointers( chain, input_a, frame );
327 GlslManager::get_effect_secondary_input( service, frame, &input_b, &frame_b );
329 dispose_pixel_pointers( chain, input_b, frame_b );
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 )
335 GlslManager* glsl = GlslManager::get_instance();
337 if ( output_format == mlt_image_glsl_texture ) {
338 error = glsl->render_frame_texture( chain, frame, width, height, image );
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 );
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 )
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 );
358 else if ( format == mlt_image_rgb24 ) {
359 // TODO: Get the color space if available.
360 input->useFlatInput( FORMAT_RGB, width, height );
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 );
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 );
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 )
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 );
390 memcpy( img_copy, image, img_size );
395 static int convert_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, mlt_image_format output_format )
398 if ( *format == output_format )
401 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
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 ) );
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 );
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 );
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 );
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 );
431 *image = (uint8_t *) -1;
432 mlt_frame_set_image( frame, *image, 0, NULL );
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;
439 // Construct the chain unless we already have a good one.
440 finalize_movit_chain( leaf_service, frame );
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 );
447 error = movit_render( chain->effect_chain, frame, format, output_format, width, height, image );
449 dispose_pixel_pointers( chain, leaf_service, frame );
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 );
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);
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 );
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 );
497 input->set_pixel_data( *image );
498 error = movit_render( chain, frame, format, output_format, width, height, image );
503 GlslManager::get_instance()->unlock_service( frame );
505 mlt_properties_set_int( properties, "format", output_format );
506 *format = output_format;
511 static mlt_frame process( mlt_filter filter, mlt_frame frame )
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 );
520 frame->convert_image = convert_image;
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 );
530 static mlt_filter create_filter( mlt_profile profile, const char *effect )
533 char *id = strdup( effect );
534 char *arg = strchr( id, ':' );
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 );
542 filter = mlt_factory_filter( profile, id, arg );
544 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
551 mlt_filter filter_movit_convert_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
553 mlt_filter filter = NULL;
554 GlslManager* glsl = GlslManager::get_instance();
556 if ( glsl && ( filter = mlt_filter_new() ) )
558 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
559 glsl->add_ref( properties );
561 // XXX avcolor_space is crashing on Windows in this context!
562 mlt_filter cpu_csc = NULL;
564 mlt_filter cpu_csc = create_filter( profile, "avcolor_space" );
567 cpu_csc = create_filter( profile, "imageconvert" );
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;