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>
25 #include "glsl_manager.h"
26 #include <movit/effect_chain.h>
27 #include <movit/util.h>
28 #include "mlt_movit_input.h"
29 #include <mlt++/MltProducer.h>
30 #include "mlt_flip_effect.h"
32 static void yuv422_to_yuv422p( uint8_t *yuv422, uint8_t *yuv422p, int width, int height )
35 uint8_t *U = Y + width * height;
36 uint8_t *V = U + width * height / 2;
37 int n = width * height / 2 + 1;
46 static int convert_on_cpu( mlt_frame frame, uint8_t **image, mlt_image_format *format, mlt_image_format output_format )
49 mlt_filter cpu_csc = (mlt_filter) mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "cpu_csc", NULL );
51 int (* save_fp )( mlt_frame self, uint8_t **image, mlt_image_format *input, mlt_image_format output )
52 = frame->convert_image;
53 frame->convert_image = NULL;
54 mlt_filter_process( cpu_csc, frame );
55 error = frame->convert_image( frame, image, format, output_format );
56 frame->convert_image = save_fp;
63 static void delete_chain( EffectChain* chain )
68 static int convert_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, mlt_image_format output_format )
71 if ( *format == output_format )
74 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
76 mlt_log_debug( NULL, "filter_movit_convert: %s -> %s (%d)\n",
77 mlt_image_format_name( *format ), mlt_image_format_name( output_format ),
78 mlt_frame_get_position( frame ) );
80 // Use CPU if glsl not initialized or not supported.
81 GlslManager* glsl = GlslManager::get_instance();
82 if ( !glsl || !glsl->get_int("glsl_supported" ) )
83 return convert_on_cpu( frame, image, format, output_format );
85 // Do non-GL image conversions on a CPU-based image converter.
86 if ( *format != mlt_image_glsl && output_format != mlt_image_glsl && output_format != mlt_image_glsl_texture )
87 return convert_on_cpu( frame, image, format, output_format );
90 int width = mlt_properties_get_int( properties, "width" );
91 int height = mlt_properties_get_int( properties, "height" );
92 int img_size = mlt_image_format_size( *format, width, height, NULL );
93 mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
94 mlt_service service = MLT_PRODUCER_SERVICE(producer);
95 GlslManager::get_instance()->lock_service( frame );
96 EffectChain* chain = GlslManager::get_chain( service );
97 MltInput* input = GlslManager::get_input( service );
99 if ( !chain || !input ) {
100 GlslManager::get_instance()->unlock_service( frame );
104 if ( *format != mlt_image_glsl ) {
105 bool finalize_chain = false;
106 if ( output_format == mlt_image_glsl_texture ) {
107 // We might already have a texture from a previous conversion from mlt_image_glsl.
108 glsl_texture texture = (glsl_texture) mlt_properties_get_data( properties, "movit.convert.texture", NULL );
109 // XXX: requires a special property set on the frame by the app for now
110 // because we do not have reliable way to clear the texture property
111 // when a downstream filter has changed image.
112 if ( texture && mlt_properties_get_int( properties, "movit.convert.use_texture") ) {
113 *image = (uint8_t*) &texture->texture;
114 mlt_frame_set_image( frame, *image, 0, NULL );
115 mlt_properties_set_int( properties, "format", output_format );
116 *format = output_format;
117 GlslManager::get_instance()->unlock_service( frame );
120 // Use a separate chain to convert image in RAM to OpenGL texture.
121 // Use cached chain if available and compatible.
122 Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) );
123 chain = (EffectChain*) producer.get_data( "movit.convert.chain" );
124 input = (MltInput*) producer.get_data( "movit.convert.input" );
125 int w = producer.get_int( "movit.convert.width" );
126 int h = producer.get_int( "movit.convert.height" );
127 mlt_image_format f = (mlt_image_format) producer.get_int( "movit.convert.format" );
128 if ( !chain || width != w || height != h || output_format != f ) {
129 chain = new EffectChain( width, height );
130 input = new MltInput( width, height );
131 chain->add_input( input );
132 chain->add_effect( new Mlt::VerticalFlip() );
133 finalize_chain = true;
134 producer.set( "movit.convert.chain", chain, 0, (mlt_destructor) delete_chain );
135 producer.set( "movit.convert.input", input, 0 );
136 producer.set( "movit.convert.width", width );
137 producer.set( "movit.convert.height", height );
138 producer.set( "movit.convert.format", output_format );
142 if ( *format == mlt_image_rgb24a || *format == mlt_image_opengl ) {
143 input->useFlatInput( chain, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, width, height );
144 input->set_pixel_data( *image );
146 else if ( *format == mlt_image_rgb24 ) {
147 input->useFlatInput( chain, FORMAT_RGB, width, height );
148 input->set_pixel_data( *image );
150 else if ( *format == mlt_image_yuv420p ) {
151 ImageFormat image_format;
152 YCbCrFormat ycbcr_format;
153 if ( 709 == mlt_properties_get_int( properties, "colorspace" ) ) {
154 image_format.color_space = COLORSPACE_REC_709;
155 image_format.gamma_curve = GAMMA_REC_709;
156 ycbcr_format.luma_coefficients = YCBCR_REC_709;
157 } else if ( 576 == mlt_properties_get_int( properties, "height" ) ) {
158 image_format.color_space = COLORSPACE_REC_601_625;
159 image_format.gamma_curve = GAMMA_REC_601;
160 ycbcr_format.luma_coefficients = YCBCR_REC_601;
162 image_format.color_space = COLORSPACE_REC_601_525;
163 image_format.gamma_curve = GAMMA_REC_601;
164 ycbcr_format.luma_coefficients = YCBCR_REC_601;
166 ycbcr_format.full_range = mlt_properties_get_int( properties, "force_full_luma" );
167 ycbcr_format.chroma_subsampling_x = ycbcr_format.chroma_subsampling_y = 2;
168 // TODO: make new frame properties set by producers
169 ycbcr_format.cb_x_position = ycbcr_format.cr_x_position = 0.0f;
170 ycbcr_format.cb_y_position = ycbcr_format.cr_y_position = 0.5f;
171 input->useYCbCrInput( chain, image_format, ycbcr_format, width, height );
172 input->set_pixel_data( *image );
174 else if ( *format == mlt_image_yuv422 ) {
175 ImageFormat image_format;
176 YCbCrFormat ycbcr_format;
177 if ( 709 == mlt_properties_get_int( properties, "colorspace" ) ) {
178 image_format.color_space = COLORSPACE_REC_709;
179 image_format.gamma_curve = GAMMA_REC_709;
180 ycbcr_format.luma_coefficients = YCBCR_REC_709;
181 } else if ( 576 == height ) {
182 image_format.color_space = COLORSPACE_REC_601_625;
183 image_format.gamma_curve = GAMMA_REC_601;
184 ycbcr_format.luma_coefficients = YCBCR_REC_601;
186 image_format.color_space = COLORSPACE_REC_601_525;
187 image_format.gamma_curve = GAMMA_REC_601;
188 ycbcr_format.luma_coefficients = YCBCR_REC_601;
190 ycbcr_format.full_range = mlt_properties_get_int( properties, "force_full_luma" );
191 ycbcr_format.chroma_subsampling_x = 2;
192 ycbcr_format.chroma_subsampling_y = 1;
193 // TODO: make new frame properties set by producers
194 ycbcr_format.cb_x_position = ycbcr_format.cr_x_position = 0.0f;
195 ycbcr_format.cb_y_position = ycbcr_format.cr_y_position = 0.5f;
196 input->useYCbCrInput( chain, image_format, ycbcr_format, width, height );
198 // convert chunky to planar
199 uint8_t* planar = (uint8_t*) mlt_pool_alloc( img_size );
200 yuv422_to_yuv422p( *image, planar, width, height );
201 input->set_pixel_data( planar );
202 mlt_frame_set_image( frame, planar, img_size, mlt_pool_release );
204 // Finalize the separate conversion chain if needed.
205 if ( finalize_chain )
209 if ( output_format != mlt_image_glsl ) {
211 if ( output_format == mlt_image_glsl_texture ) {
212 error = glsl->render_frame_texture( service, frame, width, height, image );
215 error = glsl->render_frame_rgba( service, frame, width, height, image );
216 if ( !error && output_format != mlt_image_rgb24a ) {
217 *format = mlt_image_rgb24a;
218 error = convert_on_cpu( frame, image, format, output_format );
221 mlt_properties_set_int( properties, "format", output_format );
222 *format = output_format;
225 mlt_properties_set_int( properties, "format", output_format );
226 *format = output_format;
228 GlslManager::get_instance()->unlock_service( frame );
233 static mlt_frame process( mlt_filter filter, mlt_frame frame )
235 // Set a default colorspace on the frame if not yet set by the producer.
236 // The producer may still change it during get_image.
237 // This way we do not have to modify each producer to set a valid colorspace.
238 mlt_properties properties = MLT_FRAME_PROPERTIES(frame);
239 if ( mlt_properties_get_int( properties, "colorspace" ) <= 0 )
240 mlt_properties_set_int( properties, "colorspace", mlt_service_profile( MLT_FILTER_SERVICE(filter) )->colorspace );
242 frame->convert_image = convert_image;
244 mlt_filter cpu_csc = (mlt_filter) mlt_properties_get_data( MLT_FILTER_PROPERTIES( filter ), "cpu_csc", NULL );
245 mlt_properties_inc_ref( MLT_FILTER_PROPERTIES(cpu_csc) );
246 mlt_properties_set_data( properties, "cpu_csc", cpu_csc, 0,
247 (mlt_destructor) mlt_filter_close, NULL );
252 static mlt_filter create_filter( mlt_profile profile, const char *effect )
255 char *id = strdup( effect );
256 char *arg = strchr( id, ':' );
260 // The swscale and avcolor_space filters require resolution as arg to test compatibility
261 if ( !strcmp( effect, "avcolor_space" ) )
262 filter = mlt_factory_filter( profile, id, &profile->width );
264 filter = mlt_factory_filter( profile, id, arg );
266 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
273 mlt_filter filter_movit_convert_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
275 mlt_filter filter = NULL;
276 GlslManager* glsl = GlslManager::get_instance();
278 if ( glsl && ( filter = mlt_filter_new() ) )
281 // XXX avcolor_space is crashing on Windows in this context!
282 mlt_filter cpu_csc = NULL;
284 mlt_filter cpu_csc = create_filter( profile, "avcolor_space" );
287 cpu_csc = create_filter( profile, "imageconvert" );
289 mlt_properties_set_data( MLT_FILTER_PROPERTIES( filter ), "cpu_csc", cpu_csc, 0,
290 (mlt_destructor) mlt_filter_close, NULL );
291 filter->process = process;