static Effect* build_movit_chain( mlt_service service, mlt_frame frame, GlslChain *chain ) { if ( service == (mlt_service) -1 ) { mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ); MltInput* input = GlslManager::get_input( producer, frame ); GlslManager::set_input( producer, frame, NULL ); chain->effect_chain->add_input( input->get_input() ); chain->inputs.insert(std::make_pair( producer, input ) ); return input->get_input(); } Effect* effect = GlslManager::get_effect( service, frame ); assert( effect ); GlslManager::set_effect( service, frame, NULL ); mlt_service input_a = GlslManager::get_effect_input( service, frame ); mlt_service input_b, input_c; mlt_frame frame_b, frame_c; GlslManager::get_effect_secondary_input( service, frame, &input_b, &frame_b ); GlslManager::get_effect_third_input( service, frame, &input_c, &frame_c ); Effect *effect_a = build_movit_chain( input_a, frame, chain ); if ( input_c && input_b ) { Effect *effect_b = build_movit_chain( input_b, frame_b, chain ); Effect *effect_c = build_movit_chain( input_c, frame_c, chain ); chain->effect_chain->add_effect( effect, effect_a, effect_b, effect_c ); } else if ( input_b ) { Effect *effect_b = build_movit_chain( input_b, frame_b, chain ); chain->effect_chain->add_effect( effect, effect_a, effect_b ); } else { chain->effect_chain->add_effect( effect, effect_a ); } chain->effects.insert(std::make_pair( service, effect ) ); return effect; }
static int convert_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, mlt_image_format output_format ) { // Nothing to do! if ( *format == output_format ) return 0; mlt_properties properties = MLT_FRAME_PROPERTIES( frame ); mlt_log_debug( NULL, "filter_movit_convert: %s -> %s (%d)\n", mlt_image_format_name( *format ), mlt_image_format_name( output_format ), mlt_frame_get_position( frame ) ); // Use CPU if glsl not initialized or not supported. GlslManager* glsl = GlslManager::get_instance(); if ( !glsl || !glsl->get_int("glsl_supported" ) ) return convert_on_cpu( frame, image, format, output_format ); // Do non-GL image conversions on a CPU-based image converter. if ( *format != mlt_image_glsl && output_format != mlt_image_glsl && output_format != mlt_image_glsl_texture ) return convert_on_cpu( frame, image, format, output_format ); int error = 0; int width = mlt_properties_get_int( properties, "width" ); int height = mlt_properties_get_int( properties, "height" ); GlslManager::get_instance()->lock_service( frame ); // If we're at the beginning of a series of Movit effects, store the input // sent into the chain. if ( output_format == mlt_image_glsl ) { mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ); mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ); MltInput *input = create_input( properties, *format, profile->width, profile->height, width, height ); GlslManager::set_input( producer, frame, input ); uint8_t *img_copy = make_input_copy( *format, *image, width, height ); GlslManager::set_input_pixel_pointer( producer, frame, img_copy ); *image = (uint8_t *) -1; mlt_frame_set_image( frame, *image, 0, NULL ); } // If we're at the _end_ of a series of Movit effects, render the chain. if ( *format == mlt_image_glsl ) { mlt_service leaf_service = (mlt_service) *image; if ( leaf_service == (mlt_service) -1 ) { // Something on the way requested conversion to mlt_glsl, // but never added an effect. Don't build a Movit chain; // just do the conversion and we're done. mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ); MltInput *input = GlslManager::get_input( producer, frame ); *image = GlslManager::get_input_pixel_pointer( producer, frame ); *format = input->get_format(); delete input; GlslManager::get_instance()->unlock_service( frame ); return convert_on_cpu( frame, image, format, output_format ); } // Construct the chain unless we already have a good one. finalize_movit_chain( leaf_service, frame ); // Set per-frame parameters now that we know which Effect instances to set them on. // (finalize_movit_chain may already have done this, though, but twice doesn't hurt.) GlslChain *chain = GlslManager::get_chain( leaf_service ); set_movit_parameters( chain, leaf_service, frame ); error = movit_render( chain->effect_chain, frame, format, output_format, width, height, image ); dispose_pixel_pointers( chain, leaf_service, frame ); } // If we've been asked to render some frame directly to a texture (without any // effects in-between), we create a new mini-chain to do so. if ( *format != mlt_image_glsl && output_format == mlt_image_glsl_texture ) { // We might already have a texture from a previous conversion from mlt_image_glsl. glsl_texture texture = (glsl_texture) mlt_properties_get_data( properties, "movit.convert.texture", NULL ); // XXX: requires a special property set on the frame by the app for now // because we do not have reliable way to clear the texture property // when a downstream filter has changed image. if ( texture && mlt_properties_get_int( properties, "movit.convert.use_texture") ) { *image = (uint8_t*) &texture->texture; mlt_frame_set_image( frame, *image, 0, NULL ); } else { // Use a separate chain to convert image in RAM to OpenGL texture. // Use cached chain if available and compatible. Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) ); EffectChain *chain = (EffectChain*) producer.get_data( "movit.convert.chain" ); MltInput *input = (MltInput*) producer.get_data( "movit.convert.input" ); int w = producer.get_int( "movit.convert.width" ); int h = producer.get_int( "movit.convert.height" ); mlt_image_format f = (mlt_image_format) producer.get_int( "movit.convert.format" ); if ( !chain || !input || width != w || height != h || *format != f ) { chain = new EffectChain( width, height, GlslManager::get_instance()->get_resource_pool() ); input = create_input( properties, *format, width, height, width, height ); chain->add_input( input->get_input() ); chain->add_effect( new Mlt::VerticalFlip() ); ImageFormat movit_output_format; movit_output_format.color_space = COLORSPACE_sRGB; movit_output_format.gamma_curve = GAMMA_sRGB; chain->add_output(movit_output_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED); chain->set_dither_bits(8); chain->finalize(); producer.set( "movit.convert.chain", chain, 0, (mlt_destructor) delete_chain ); producer.set( "movit.convert.input", input, 0, NULL ); producer.set( "movit.convert.width", width ); producer.set( "movit.convert.height", height ); producer.set( "movit.convert.format", *format ); } if ( *format == mlt_image_yuv422 ) { // We need to convert to planar, which make_input_copy() will do for us. uint8_t *planar = make_input_copy( *format, *image, width, height ); input->set_pixel_data( planar ); error = movit_render( chain, frame, format, output_format, width, height, image ); mlt_pool_release( planar ); } else { input->set_pixel_data( *image ); error = movit_render( chain, frame, format, output_format, width, height, image ); } } } GlslManager::get_instance()->unlock_service( frame ); mlt_properties_set_int( properties, "format", output_format ); *format = output_format; return error; }