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