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"
31 static void yuv422_to_yuv422p( uint8_t *yuv422, uint8_t *yuv422p, int width, int height )
34 uint8_t *U = Y + width * height;
35 uint8_t *V = U + width * height / 2;
36 int n = width * height / 2 + 1;
45 static int convert_on_cpu( mlt_frame frame, uint8_t **image, mlt_image_format *format, mlt_image_format output_format )
48 mlt_filter cpu_csc = (mlt_filter) mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "cpu_csc", NULL );
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;
62 static int convert_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, mlt_image_format output_format )
65 if ( *format == output_format )
68 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
70 mlt_log_debug( NULL, "filter_movit_convert: %s -> %s\n",
71 mlt_image_format_name( *format ), mlt_image_format_name( output_format ) );
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 );
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 );
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 );
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 );
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;
102 input = new MltInput( width, height );
103 chain = new EffectChain( width, height );
104 chain->add_input( input );
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 );
113 else if ( *format == mlt_image_rgb24 ) {
114 input->useFlatInput( chain, FORMAT_RGB, width, height );
115 input->set_pixel_data( *image );
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;
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;
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 );
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;
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;
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 );
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 );
173 if ( output_format != mlt_image_glsl ) {
174 glsl_fbo fbo = glsl->get_fbo( width, height );
176 if ( output_format == mlt_image_glsl_texture ) {
177 glsl_texture texture = glsl->get_texture( width, height, GL_RGBA );
179 glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
181 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
183 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
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 );
193 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
195 // Using a temporary chain to convert image in RAM to OpenGL texture.
196 if ( *format != mlt_image_glsl )
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;
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 )?
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 );
215 if ( fbo && pbo && texture ) {
217 glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
219 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
221 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
224 GlslManager::render( service, chain, fbo->fbo, width, height );
227 glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, pbo->pbo );
229 glBufferData( GL_PIXEL_PACK_BUFFER_ARB, img_size, NULL, GL_STREAM_READ );
231 glReadPixels( 0, 0, width, height, gl_format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0) );
235 uint8_t* buf = (uint8_t*) glMapBuffer( GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY );
238 if ( output_format == mlt_image_yuv422 || output_format == mlt_image_yuv420p ) {
240 *format = mlt_image_rgb24;
241 error = convert_on_cpu( frame, image, format, output_format );
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 );
249 // Release PBO and FBO
250 glUnmapBuffer( GL_PIXEL_PACK_BUFFER_ARB );
252 glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, 0 );
254 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
256 glBindTexture( GL_TEXTURE_2D, 0 );
258 mlt_properties_set_data( properties, "movit.convert", texture, 0,
259 (mlt_destructor) GlslManager::release_texture, NULL);
261 mlt_properties_set_int( properties, "format", output_format );
262 *format = output_format;
268 if ( fbo ) GlslManager::release_fbo( fbo );
271 mlt_properties_set_int( properties, "format", output_format );
272 *format = output_format;
278 static mlt_frame process( mlt_filter filter, mlt_frame frame )
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 );
287 frame->convert_image = convert_image;
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 );
297 static mlt_filter create_filter( mlt_profile profile, char *effect )
299 mlt_filter filter = NULL;
300 char *id = strdup( effect );
301 char *arg = strchr( id, ':' );
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;
309 filter = mlt_factory_filter( profile, id, arg );
311 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
318 mlt_filter filter_movit_convert_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
320 mlt_filter filter = NULL;
321 GlslManager* glsl = GlslManager::get_instance();
323 if ( glsl && ( filter = mlt_filter_new() ) )
325 mlt_filter cpu_csc = create_filter( profile, "avcolor_space" );
327 cpu_csc = create_filter( profile, "imageconvert" );
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;