]> git.sesse.net Git - mlt/blob - src/modules/opengl/filter_movit_convert.cpp
Fix memory leak converting image in RAM to GL texture.
[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
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"
31
32 static void yuv422_to_yuv422p( uint8_t *yuv422, uint8_t *yuv422p, int width, int height )
33 {
34         uint8_t *Y = yuv422p;
35         uint8_t *U = Y + width * height;
36         uint8_t *V = U + width * height / 2;
37         int n = width * height / 2 + 1;
38         while ( --n ) {
39                 *Y++ = *yuv422++;
40                 *U++ = *yuv422++;
41                 *Y++ = *yuv422++;
42                 *V++ = *yuv422++;
43         }
44 }
45
46 static int convert_on_cpu( mlt_frame frame, uint8_t **image, mlt_image_format *format, mlt_image_format output_format )
47 {
48         int error = 0;
49         mlt_filter cpu_csc = (mlt_filter) mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "cpu_csc", NULL );
50         if ( cpu_csc ) {
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;
57         } else {
58                 error = 1;
59         }
60         return error;
61 }
62
63 static void delete_chain( EffectChain* chain )
64 {
65         delete chain;
66 }
67
68 static int convert_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, mlt_image_format output_format )
69 {
70         // Nothing to do!
71         if ( *format == output_format )
72                 return 0;
73
74         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
75
76         mlt_log_debug( NULL, "filter_movit_convert: %s -> %s\n",
77                 mlt_image_format_name( *format ), mlt_image_format_name( output_format ) );
78
79         // Use CPU if glsl not initialized or not supported.
80         GlslManager* glsl = GlslManager::get_instance();
81         if ( !glsl || !glsl->get_int("glsl_supported" ) )
82                 return convert_on_cpu( frame, image, format, output_format );
83
84         // Do non-GL image conversions on a CPU-based image converter.
85         if ( *format != mlt_image_glsl && output_format != mlt_image_glsl && output_format != mlt_image_glsl_texture )
86                 return convert_on_cpu( frame, image, format, output_format );
87
88         int error = 0;
89         int width = mlt_properties_get_int( properties, "width" );
90         int height = mlt_properties_get_int( properties, "height" );
91         int img_size = mlt_image_format_size( *format, width, height, NULL );
92         mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
93         mlt_service service = MLT_PRODUCER_SERVICE(producer);
94         GlslManager::get_instance()->lock_service( frame );
95         EffectChain* chain = GlslManager::get_chain( service );
96         MltInput* input = GlslManager::get_input( service );
97
98         if ( !chain || !input ) {
99                 GlslManager::get_instance()->unlock_service( frame );
100                 return 2;
101         }
102
103         if ( *format != mlt_image_glsl ) {
104                 bool finalize_chain = false;
105                 if ( output_format == mlt_image_glsl_texture ) {
106                         // We might already have a texture from a previous conversion from mlt_image_glsl.
107                         glsl_texture texture = (glsl_texture) mlt_properties_get_data( properties, "movit.convert.texture", NULL );
108                         // XXX: requires a special property set on the frame by the app for now
109                         // because we do not have reliable way to clear the texture property
110                         // when a downstream filter has changed image.
111                         if ( texture && mlt_properties_get_int( properties, "movit.convert.use_texture") ) {
112                                 *image = (uint8_t*) &texture->texture;
113                                 mlt_frame_set_image( frame, *image, 0, NULL );
114                                 mlt_properties_set_int( properties, "format", output_format );
115                                 *format = output_format;
116                                 GlslManager::get_instance()->unlock_service( frame );
117                                 return error;
118                         } else {
119                                 // Use a separate chain to convert image in RAM to OpenGL texture.
120                                 // Use cached chain if available and compatible.
121                                 Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) );
122                                 chain = (EffectChain*) producer.get_data( "movit.convert.chain" );
123                                 input = (MltInput*) producer.get_data( "movit.convert.input" );
124                                 int w = producer.get_int( "movit.convert.width" );
125                                 int h = producer.get_int( "movit.convert.height" );
126                                 mlt_image_format f = (mlt_image_format) producer.get_int( "movit.convert.format" );
127                                 if ( !chain || width != w || height != h || output_format != f ) {
128                                         chain = new EffectChain( width, height );
129                                         input = new MltInput( width, height );
130                                         chain->add_input( input );
131                                         chain->add_effect( new Mlt::VerticalFlip() );
132                                         finalize_chain = true;
133                                         producer.set( "movit.convert.chain", chain, 0, (mlt_destructor) delete_chain );
134                                         producer.set( "movit.convert.input", input, 0 );
135                                         producer.set( "movit.convert.width", width );
136                                         producer.set( "movit.convert.height", height );
137                                         producer.set( "movit.convert.format", output_format );
138                                 }
139                         }
140                 }
141                 if ( *format == mlt_image_rgb24a || *format == mlt_image_opengl ) { 
142                         input->useFlatInput( chain, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, width, height );
143                         input->set_pixel_data( *image );
144                 }
145                 else if ( *format == mlt_image_rgb24 ) {
146                         input->useFlatInput( chain, FORMAT_RGB, width, height );
147                         input->set_pixel_data( *image );
148                 }
149                 else if ( *format == mlt_image_yuv420p ) {
150                         ImageFormat image_format;
151                         YCbCrFormat ycbcr_format;
152                         if ( 709 == mlt_properties_get_int( properties, "colorspace" ) ) {
153                                 image_format.color_space = COLORSPACE_REC_709;
154                                 image_format.gamma_curve = GAMMA_REC_709;
155                                 ycbcr_format.luma_coefficients = YCBCR_REC_709;
156                         } else if ( 576 == mlt_properties_get_int( properties, "height" ) ) {
157                                 image_format.color_space = COLORSPACE_REC_601_625;
158                                 image_format.gamma_curve = GAMMA_REC_601;
159                                 ycbcr_format.luma_coefficients = YCBCR_REC_601;
160                         } else {
161                                 image_format.color_space = COLORSPACE_REC_601_525;
162                                 image_format.gamma_curve = GAMMA_REC_601;
163                                 ycbcr_format.luma_coefficients = YCBCR_REC_601;
164                         }
165                         ycbcr_format.full_range = mlt_properties_get_int( properties, "force_full_luma" );
166                         ycbcr_format.chroma_subsampling_x = ycbcr_format.chroma_subsampling_y = 2;
167                         // TODO: make new frame properties set by producers
168                         ycbcr_format.cb_x_position = ycbcr_format.cr_x_position = 0.0f;
169                         ycbcr_format.cb_y_position = ycbcr_format.cr_y_position = 0.5f;
170                         input->useYCbCrInput( chain, image_format, ycbcr_format, width, height );
171                         input->set_pixel_data( *image );
172                 }
173                 else if ( *format == mlt_image_yuv422 ) {
174                         ImageFormat image_format;
175                         YCbCrFormat ycbcr_format;
176                         if ( 709 == mlt_properties_get_int( properties, "colorspace" ) ) {
177                                 image_format.color_space = COLORSPACE_REC_709;
178                                 image_format.gamma_curve = GAMMA_REC_709;
179                                 ycbcr_format.luma_coefficients = YCBCR_REC_709;
180                         } else if ( 576 == height ) {
181                                 image_format.color_space = COLORSPACE_REC_601_625;
182                                 image_format.gamma_curve = GAMMA_REC_601;
183                                 ycbcr_format.luma_coefficients = YCBCR_REC_601;
184                         } else {
185                                 image_format.color_space = COLORSPACE_REC_601_525;
186                                 image_format.gamma_curve = GAMMA_REC_601;
187                                 ycbcr_format.luma_coefficients = YCBCR_REC_601;
188                         }
189                         ycbcr_format.full_range = mlt_properties_get_int( properties, "force_full_luma" );
190                         ycbcr_format.chroma_subsampling_x = 2;
191                         ycbcr_format.chroma_subsampling_y = 1;
192                         // TODO: make new frame properties set by producers
193                         ycbcr_format.cb_x_position = ycbcr_format.cr_x_position = 0.0f;
194                         ycbcr_format.cb_y_position = ycbcr_format.cr_y_position = 0.5f;
195                         input->useYCbCrInput( chain, image_format, ycbcr_format, width, height );
196                         
197                         // convert chunky to planar
198                         uint8_t* planar = (uint8_t*) mlt_pool_alloc( img_size );
199                         yuv422_to_yuv422p( *image, planar, width, height );
200                         input->set_pixel_data( planar );
201                         mlt_frame_set_image( frame, planar, img_size, mlt_pool_release );
202                 }
203                 // Finalize the separate conversion chain if needed.
204                 if ( finalize_chain )
205                         chain->finalize();
206         }
207
208         if ( output_format != mlt_image_glsl ) {
209                 glsl_fbo fbo = glsl->get_fbo( width, height );
210
211                 if ( output_format == mlt_image_glsl_texture ) {
212                         glsl_texture texture = glsl->get_texture( width, height, GL_RGBA );
213
214                         glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
215                         check_error();
216                         glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
217                         check_error();
218                         glBindFramebuffer( GL_FRAMEBUFFER, 0 );
219                         check_error();
220
221                         GlslManager::render( service, chain, fbo->fbo, width, height );
222
223                         glFinish();
224                         check_error();
225                         glBindFramebuffer( GL_FRAMEBUFFER, 0 );
226                         check_error();
227
228                         *image = (uint8_t*) &texture->texture;
229                         mlt_frame_set_image( frame, *image, 0, NULL );
230                         mlt_properties_set_data( properties, "movit.convert.texture", texture, 0,
231                                 (mlt_destructor) GlslManager::release_texture, NULL );
232                         mlt_properties_set_int( properties, "format", output_format );
233                         *format = output_format;
234                 }
235                 else {
236                         // Use a PBO to hold the data we read back with glReadPixels()
237                         // (Intel/DRI goes into a slow path if we don't read to PBO)
238                         GLenum gl_format = ( output_format == mlt_image_rgb24a || output_format == mlt_image_opengl )?
239                                 GL_RGBA : GL_RGB;
240                         img_size = width * height * ( gl_format == GL_RGB? 3 : 4 );
241                         glsl_pbo pbo = glsl->get_pbo( img_size );
242                         glsl_texture texture = glsl->get_texture( width, height, gl_format );
243
244                         if ( fbo && pbo && texture ) {
245                                 // Set the FBO
246                                 glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
247                                 check_error();
248                                 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
249                                 check_error();
250                                 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
251                                 check_error();
252
253                                 GlslManager::render( service, chain, fbo->fbo, width, height );
254         
255                                 // Read FBO into PBO
256                                 glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, pbo->pbo );
257                                 check_error();
258                                 glBufferData( GL_PIXEL_PACK_BUFFER_ARB, img_size, NULL, GL_STREAM_READ );
259                                 check_error();
260                                 glReadPixels( 0, 0, width, height, gl_format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0) );
261                                 check_error();
262         
263                                 // Copy from PBO
264                                 uint8_t* buf = (uint8_t*) glMapBuffer( GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY );
265                                 check_error();
266                                 *image = (uint8_t*) mlt_pool_alloc( img_size );
267                                 mlt_frame_set_image( frame, *image, img_size, mlt_pool_release );
268                                 memcpy( *image, buf, img_size );
269
270                                 if ( output_format == mlt_image_yuv422 || output_format == mlt_image_yuv420p ) {
271                                         *format = mlt_image_rgb24;
272                                         error = convert_on_cpu( frame, image, format, output_format );
273                                 }
274         
275                                 // Release PBO and FBO
276                                 glUnmapBuffer( GL_PIXEL_PACK_BUFFER_ARB );
277                                 check_error();
278                                 glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, 0 );
279                                 check_error();
280                                 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
281                                 check_error();
282                                 glBindTexture( GL_TEXTURE_2D, 0 );
283                                 check_error();
284                                 mlt_properties_set_data( properties, "movit.convert.texture", texture, 0,
285                                         (mlt_destructor) GlslManager::release_texture, NULL);
286         
287                                 mlt_properties_set_int( properties, "format", output_format );
288                                 *format = output_format;
289                         }
290                         else {
291                                 error = 1;
292                         }
293                 }
294                 if ( fbo ) GlslManager::release_fbo( fbo );
295         }
296         else {
297                 mlt_properties_set_int( properties, "format", output_format );
298                 *format = output_format;
299         }
300         GlslManager::get_instance()->unlock_service( frame );
301
302         return error;
303 }
304
305 static mlt_frame process( mlt_filter filter, mlt_frame frame )
306 {
307         // Set a default colorspace on the frame if not yet set by the producer.
308         // The producer may still change it during get_image.
309         // This way we do not have to modify each producer to set a valid colorspace.
310         mlt_properties properties = MLT_FRAME_PROPERTIES(frame);
311         if ( mlt_properties_get_int( properties, "colorspace" ) <= 0 )
312                 mlt_properties_set_int( properties, "colorspace", mlt_service_profile( MLT_FILTER_SERVICE(filter) )->colorspace );
313
314         frame->convert_image = convert_image;
315
316         mlt_filter cpu_csc = (mlt_filter) mlt_properties_get_data( MLT_FILTER_PROPERTIES( filter ), "cpu_csc", NULL );
317         mlt_properties_inc_ref( MLT_FILTER_PROPERTIES(cpu_csc) );
318         mlt_properties_set_data( properties, "cpu_csc", cpu_csc, 0,
319                 (mlt_destructor) mlt_filter_close, NULL );
320
321         return frame;
322 }
323
324 static mlt_filter create_filter( mlt_profile profile, char *effect )
325 {
326         mlt_filter filter;
327         char *id = strdup( effect );
328         char *arg = strchr( id, ':' );
329         if ( arg != NULL )
330                 *arg ++ = '\0';
331
332         // The swscale and avcolor_space filters require resolution as arg to test compatibility
333         if ( !strcmp( effect, "avcolor_space" ) )
334                 filter = mlt_factory_filter( profile, id, &profile->width );
335         else
336                 filter = mlt_factory_filter( profile, id, arg );
337         if ( filter )
338                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
339         free( id );
340         return filter;
341 }
342
343 extern "C" {
344
345 mlt_filter filter_movit_convert_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
346 {
347         mlt_filter filter = NULL;
348         GlslManager* glsl = GlslManager::get_instance();
349
350         if ( glsl && ( filter = mlt_filter_new() ) )
351         {
352 #ifdef WIN32
353                 // XXX avcolor_space is crashing on Windows in this context!
354                 mlt_filter cpu_csc = NULL;
355 #else
356                 mlt_filter cpu_csc = create_filter( profile, "avcolor_space" );
357 #endif
358                 if ( !cpu_csc )
359                         cpu_csc = create_filter( profile, "imageconvert" );
360                 if ( cpu_csc )
361                         mlt_properties_set_data( MLT_FILTER_PROPERTIES( filter ), "cpu_csc", cpu_csc, 0,
362                                 (mlt_destructor) mlt_filter_close, NULL );
363                 filter->process = process;
364         }
365         return filter;
366 }
367
368 }