]> git.sesse.net Git - mlt/blob - src/modules/opengl/filter_movit_convert.cpp
Cache the Movit chain used for RAM->GL texture conversion.
[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: disabled for now because we do not have reliable way to clear the texture property
109                         // when a downstream filter has changed image.
110                         if ( 0 && texture ) {
111                                 *image = (uint8_t*) &texture->texture;
112                                 mlt_frame_set_image( frame, *image, 0, NULL );
113                                 mlt_properties_set_int( properties, "format", output_format );
114                                 *format = output_format;
115                                 GlslManager::get_instance()->unlock_service( frame );
116                                 return error;
117                         } else {
118                                 // Use a separate chain to convert image in RAM to OpenGL texture.
119                                 // Use cached chain if available and compatible.
120                                 Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) );
121                                 chain = (EffectChain*) producer.get_data( "movit.convert.chain" );
122                                 input = (MltInput*) producer.get_data( "movit.convert.input" );
123                                 int w = producer.get_int( "movit.convert.width" );
124                                 int h = producer.get_int( "movit.convert.height" );
125                                 mlt_image_format f = (mlt_image_format) producer.get_int( "movit.convert.format" );
126                                 if ( !chain || width != w || height != h || output_format != f ) {
127                                         chain = new EffectChain( width, height );
128                                         input = new MltInput( width, height );
129                                         chain->add_input( input );
130                                         chain->add_effect( new Mlt::VerticalFlip() );
131                                         finalize_chain = true;
132                                         producer.set( "movit.convert.chain", chain, 0, (mlt_destructor) delete_chain );
133                                         producer.set( "movit.convert.width", width );
134                                         producer.set( "movit.convert.height", height );
135                                         producer.set( "movit.convert.width", output_format );
136                                 }
137                         }
138                 }
139                 if ( *format == mlt_image_rgb24a || *format == mlt_image_opengl ) { 
140                         input->useFlatInput( chain, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, width, height );
141                         input->set_pixel_data( *image );
142                 }
143                 else if ( *format == mlt_image_rgb24 ) {
144                         input->useFlatInput( chain, FORMAT_RGB, width, height );
145                         input->set_pixel_data( *image );
146                 }
147                 else if ( *format == mlt_image_yuv420p ) {
148                         ImageFormat image_format;
149                         YCbCrFormat ycbcr_format;
150                         if ( 709 == mlt_properties_get_int( properties, "colorspace" ) ) {
151                                 image_format.color_space = COLORSPACE_REC_709;
152                                 image_format.gamma_curve = GAMMA_REC_709;
153                                 ycbcr_format.luma_coefficients = YCBCR_REC_709;
154                         } else if ( 576 == mlt_properties_get_int( properties, "height" ) ) {
155                                 image_format.color_space = COLORSPACE_REC_601_625;
156                                 image_format.gamma_curve = GAMMA_REC_601;
157                                 ycbcr_format.luma_coefficients = YCBCR_REC_601;
158                         } else {
159                                 image_format.color_space = COLORSPACE_REC_601_525;
160                                 image_format.gamma_curve = GAMMA_REC_601;
161                                 ycbcr_format.luma_coefficients = YCBCR_REC_601;
162                         }
163                         ycbcr_format.full_range = mlt_properties_get_int( properties, "force_full_luma" );
164                         ycbcr_format.chroma_subsampling_x = ycbcr_format.chroma_subsampling_y = 2;
165                         // TODO: make new frame properties set by producers
166                         ycbcr_format.cb_x_position = ycbcr_format.cr_x_position = 0.0f;
167                         ycbcr_format.cb_y_position = ycbcr_format.cr_y_position = 0.5f;
168                         input->useYCbCrInput( chain, image_format, ycbcr_format, width, height );
169                         input->set_pixel_data( *image );
170                 }
171                 else if ( *format == mlt_image_yuv422 ) {
172                         ImageFormat image_format;
173                         YCbCrFormat ycbcr_format;
174                         if ( 709 == mlt_properties_get_int( properties, "colorspace" ) ) {
175                                 image_format.color_space = COLORSPACE_REC_709;
176                                 image_format.gamma_curve = GAMMA_REC_709;
177                                 ycbcr_format.luma_coefficients = YCBCR_REC_709;
178                         } else if ( 576 == height ) {
179                                 image_format.color_space = COLORSPACE_REC_601_625;
180                                 image_format.gamma_curve = GAMMA_REC_601;
181                                 ycbcr_format.luma_coefficients = YCBCR_REC_601;
182                         } else {
183                                 image_format.color_space = COLORSPACE_REC_601_525;
184                                 image_format.gamma_curve = GAMMA_REC_601;
185                                 ycbcr_format.luma_coefficients = YCBCR_REC_601;
186                         }
187                         ycbcr_format.full_range = mlt_properties_get_int( properties, "force_full_luma" );
188                         ycbcr_format.chroma_subsampling_x = 2;
189                         ycbcr_format.chroma_subsampling_y = 1;
190                         // TODO: make new frame properties set by producers
191                         ycbcr_format.cb_x_position = ycbcr_format.cr_x_position = 0.0f;
192                         ycbcr_format.cb_y_position = ycbcr_format.cr_y_position = 0.5f;
193                         input->useYCbCrInput( chain, image_format, ycbcr_format, width, height );
194                         
195                         // convert chunky to planar
196                         uint8_t* planar = (uint8_t*) mlt_pool_alloc( img_size );
197                         yuv422_to_yuv422p( *image, planar, width, height );
198                         input->set_pixel_data( planar );
199                         mlt_frame_set_image( frame, planar, img_size, mlt_pool_release );
200                 }
201                 // Finalize the separate conversion chain if needed.
202                 if ( finalize_chain )
203                         chain->finalize();
204         }
205
206         if ( output_format != mlt_image_glsl ) {
207                 glsl_fbo fbo = glsl->get_fbo( width, height );
208
209                 if ( output_format == mlt_image_glsl_texture ) {
210                         glsl_texture texture = glsl->get_texture( width, height, GL_RGBA );
211
212                         glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
213                         check_error();
214                         glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
215                         check_error();
216                         glBindFramebuffer( GL_FRAMEBUFFER, 0 );
217                         check_error();
218
219                         GlslManager::render( service, chain, fbo->fbo, width, height );
220
221                         glFinish();
222                         check_error();
223                         glBindFramebuffer( GL_FRAMEBUFFER, 0 );
224                         check_error();
225
226                         *image = (uint8_t*) &texture->texture;
227                         mlt_frame_set_image( frame, *image, 0, NULL );
228                         mlt_properties_set_data( properties, "movit.convert.texture", texture, 0,
229                                 (mlt_destructor) GlslManager::release_texture, NULL );
230                         mlt_properties_set_int( properties, "format", output_format );
231                         *format = output_format;
232                 }
233                 else {
234                         // Use a PBO to hold the data we read back with glReadPixels()
235                         // (Intel/DRI goes into a slow path if we don't read to PBO)
236                         GLenum gl_format = ( output_format == mlt_image_rgb24a || output_format == mlt_image_opengl )?
237                                 GL_RGBA : GL_RGB;
238                         img_size = width * height * ( gl_format == GL_RGB? 3 : 4 );
239                         glsl_pbo pbo = glsl->get_pbo( img_size );
240                         glsl_texture texture = glsl->get_texture( width, height, gl_format );
241
242                         if ( fbo && pbo && texture ) {
243                                 // Set the FBO
244                                 glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
245                                 check_error();
246                                 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
247                                 check_error();
248                                 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
249                                 check_error();
250
251                                 GlslManager::render( service, chain, fbo->fbo, width, height );
252         
253                                 // Read FBO into PBO
254                                 glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, pbo->pbo );
255                                 check_error();
256                                 glBufferData( GL_PIXEL_PACK_BUFFER_ARB, img_size, NULL, GL_STREAM_READ );
257                                 check_error();
258                                 glReadPixels( 0, 0, width, height, gl_format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0) );
259                                 check_error();
260         
261                                 // Copy from PBO
262                                 uint8_t* buf = (uint8_t*) glMapBuffer( GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY );
263                                 check_error();
264                                 *image = (uint8_t*) mlt_pool_alloc( img_size );
265                                 mlt_frame_set_image( frame, *image, img_size, mlt_pool_release );
266                                 memcpy( *image, buf, img_size );
267
268                                 if ( output_format == mlt_image_yuv422 || output_format == mlt_image_yuv420p ) {
269                                         *format = mlt_image_rgb24;
270                                         error = convert_on_cpu( frame, image, format, output_format );
271                                 }
272         
273                                 // Release PBO and FBO
274                                 glUnmapBuffer( GL_PIXEL_PACK_BUFFER_ARB );
275                                 check_error();
276                                 glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, 0 );
277                                 check_error();
278                                 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
279                                 check_error();
280                                 glBindTexture( GL_TEXTURE_2D, 0 );
281                                 check_error();
282                                 mlt_properties_set_data( properties, "movit.convert.texture", texture, 0,
283                                         (mlt_destructor) GlslManager::release_texture, NULL);
284         
285                                 mlt_properties_set_int( properties, "format", output_format );
286                                 *format = output_format;
287                         }
288                         else {
289                                 error = 1;
290                         }
291                 }
292                 if ( fbo ) GlslManager::release_fbo( fbo );
293         }
294         else {
295                 mlt_properties_set_int( properties, "format", output_format );
296                 *format = output_format;
297         }
298         GlslManager::get_instance()->unlock_service( frame );
299
300         return error;
301 }
302
303 static mlt_frame process( mlt_filter filter, mlt_frame frame )
304 {
305         // Set a default colorspace on the frame if not yet set by the producer.
306         // The producer may still change it during get_image.
307         // This way we do not have to modify each producer to set a valid colorspace.
308         mlt_properties properties = MLT_FRAME_PROPERTIES(frame);
309         if ( mlt_properties_get_int( properties, "colorspace" ) <= 0 )
310                 mlt_properties_set_int( properties, "colorspace", mlt_service_profile( MLT_FILTER_SERVICE(filter) )->colorspace );
311
312         frame->convert_image = convert_image;
313
314         mlt_filter cpu_csc = (mlt_filter) mlt_properties_get_data( MLT_FILTER_PROPERTIES( filter ), "cpu_csc", NULL );
315         mlt_properties_inc_ref( MLT_FILTER_PROPERTIES(cpu_csc) );
316         mlt_properties_set_data( properties, "cpu_csc", cpu_csc, 0,
317                 (mlt_destructor) mlt_filter_close, NULL );
318
319         return frame;
320 }
321
322 static mlt_filter create_filter( mlt_profile profile, char *effect )
323 {
324         mlt_filter filter = NULL;
325         char *id = strdup( effect );
326         char *arg = strchr( id, ':' );
327         if ( arg != NULL )
328                 *arg ++ = '\0';
329
330         // The swscale and avcolor_space filters require resolution as arg to test compatibility
331         if ( !strcmp( effect, "avcolor_space" ) )
332                 arg = (char*) profile->width;
333
334         filter = mlt_factory_filter( profile, id, arg );
335         if ( filter )
336                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
337         free( id );
338         return filter;
339 }
340
341 extern "C" {
342
343 mlt_filter filter_movit_convert_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
344 {
345         mlt_filter filter = NULL;
346         GlslManager* glsl = GlslManager::get_instance();
347
348         if ( glsl && ( filter = mlt_filter_new() ) )
349         {
350                 mlt_filter cpu_csc = create_filter( profile, "avcolor_space" );
351                 if ( !cpu_csc )
352                         cpu_csc = create_filter( profile, "imageconvert" );
353                 if ( cpu_csc )
354                         mlt_properties_set_data( MLT_FILTER_PROPERTIES( filter ), "cpu_csc", cpu_csc, 0,
355                                 (mlt_destructor) mlt_filter_close, NULL );
356                 filter->process = process;
357         }
358         return filter;
359 }
360
361 }