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