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 input = new MltInput( width, height );
94 chain = new EffectChain( width, height );
95 chain->add_input( input );
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 );
103 else if ( *format == mlt_image_rgb24 ) {
104 input->useFlatInput( chain, FORMAT_RGB, width, height );
105 input->set_pixel_data( *image );
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;
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;
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 );
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;
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;
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 );
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 );
163 if ( output_format != mlt_image_glsl ) {
164 glsl_fbo fbo = glsl->get_fbo( width, height );
166 if ( output_format == mlt_image_glsl_texture ) {
167 glsl_texture texture = glsl->get_texture( width, height, GL_RGBA );
169 glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
171 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
173 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
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 );
183 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
185 // Using a temporary chain to convert image in RAM to OpenGL texture.
186 if ( *format != mlt_image_glsl )
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;
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 )?
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 );
205 if ( fbo && pbo && texture ) {
207 glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
209 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
211 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
214 GlslManager::render( service, chain, fbo->fbo, width, height );
217 glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, pbo->pbo );
219 glBufferData( GL_PIXEL_PACK_BUFFER_ARB, img_size, NULL, GL_STREAM_READ );
221 glReadPixels( 0, 0, width, height, gl_format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0) );
225 uint8_t* buf = (uint8_t*) glMapBuffer( GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY );
228 if ( output_format == mlt_image_yuv422 || output_format == mlt_image_yuv420p ) {
230 *format = mlt_image_rgb24;
231 error = convert_on_cpu( frame, image, format, output_format );
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 );
239 // Release PBO and FBO
240 glUnmapBuffer( GL_PIXEL_PACK_BUFFER_ARB );
242 glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, 0 );
244 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
246 glBindTexture( GL_TEXTURE_2D, 0 );
248 GlslManager::release_texture( texture );
250 mlt_properties_set_int( properties, "format", output_format );
251 *format = output_format;
257 if ( fbo ) GlslManager::release_fbo( fbo );
260 mlt_properties_set_int( properties, "format", output_format );
261 *format = output_format;
267 static mlt_frame process( mlt_filter filter, mlt_frame frame )
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 );
276 frame->convert_image = convert_image;
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 );
286 static mlt_filter create_filter( mlt_profile profile, char *effect )
288 mlt_filter filter = NULL;
289 char *id = strdup( effect );
290 char *arg = strchr( id, ':' );
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;
298 filter = mlt_factory_filter( profile, id, arg );
300 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
307 mlt_filter filter_movit_convert_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
309 mlt_filter filter = NULL;
310 GlslManager* glsl = GlslManager::get_instance();
312 if ( glsl && ( filter = mlt_filter_new() ) )
314 mlt_filter cpu_csc = create_filter( profile, "avcolor_space" );
316 cpu_csc = create_filter( profile, "imageconvert" );
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;