]> git.sesse.net Git - mlt/blob - src/modules/opengl/filter_movit_convert.cpp
Make the Movit converter use the correct color primaries.
[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 "filter_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 void get_format_from_properties( mlt_properties properties, ImageFormat* image_format, YCbCrFormat* ycbcr_format )
69 {
70         switch ( mlt_properties_get_int( properties, "colorspace" ) ) {
71         case 601:
72                 ycbcr_format->luma_coefficients = YCBCR_REC_601;
73                 break;
74         case 709:
75         default:
76                 ycbcr_format->luma_coefficients = YCBCR_REC_709;
77                 break;
78         }
79
80         switch ( mlt_properties_get_int( properties, "color_primaries" ) ) {
81         case 601625:
82                 image_format->color_space = COLORSPACE_REC_601_625;
83                 break;
84         case 601525:
85                 image_format->color_space = COLORSPACE_REC_601_525;
86                 break;
87         case 709:
88         default:
89                 image_format->color_space = COLORSPACE_REC_709;
90                 break;
91         }
92
93         image_format->gamma_curve = GAMMA_REC_709;
94
95         if ( mlt_properties_get_int( properties, "force_full_luma" ) ) {
96                 ycbcr_format->full_range = true;
97         } else {
98                 ycbcr_format->full_range = ( mlt_properties_get_int( properties, "full_luma" ) == 1 );
99         }
100
101         // TODO: make new frame properties set by producers
102         ycbcr_format->cb_x_position = ycbcr_format->cr_x_position = 0.0f;
103         ycbcr_format->cb_y_position = ycbcr_format->cr_y_position = 0.5f;
104 }
105
106 static int convert_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, mlt_image_format output_format )
107 {
108         // Nothing to do!
109         if ( *format == output_format )
110                 return 0;
111
112         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
113
114         mlt_log_debug( NULL, "filter_movit_convert: %s -> %s (%d)\n",
115                 mlt_image_format_name( *format ), mlt_image_format_name( output_format ),
116                 mlt_frame_get_position( frame ) );
117
118         // Use CPU if glsl not initialized or not supported.
119         GlslManager* glsl = GlslManager::get_instance();
120         if ( !glsl || !glsl->get_int("glsl_supported" ) )
121                 return convert_on_cpu( frame, image, format, output_format );
122
123         // Do non-GL image conversions on a CPU-based image converter.
124         if ( *format != mlt_image_glsl && output_format != mlt_image_glsl && output_format != mlt_image_glsl_texture )
125                 return convert_on_cpu( frame, image, format, output_format );
126
127         int error = 0;
128         int width = mlt_properties_get_int( properties, "width" );
129         int height = mlt_properties_get_int( properties, "height" );
130         int img_size = mlt_image_format_size( *format, width, height, NULL );
131         mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
132         mlt_service service = MLT_PRODUCER_SERVICE(producer);
133         GlslManager::get_instance()->lock_service( frame );
134         EffectChain* chain = GlslManager::get_chain( service );
135         MltInput* input = GlslManager::get_input( service );
136
137         if ( !chain || !input ) {
138                 GlslManager::get_instance()->unlock_service( frame );
139                 return 2;
140         }
141
142         if ( *format != mlt_image_glsl ) {
143                 bool finalize_chain = false;
144                 if ( output_format == mlt_image_glsl_texture ) {
145                         // We might already have a texture from a previous conversion from mlt_image_glsl.
146                         glsl_texture texture = (glsl_texture) mlt_properties_get_data( properties, "movit.convert.texture", NULL );
147                         // XXX: requires a special property set on the frame by the app for now
148                         // because we do not have reliable way to clear the texture property
149                         // when a downstream filter has changed image.
150                         if ( texture && mlt_properties_get_int( properties, "movit.convert.use_texture") ) {
151                                 *image = (uint8_t*) &texture->texture;
152                                 mlt_frame_set_image( frame, *image, 0, NULL );
153                                 mlt_properties_set_int( properties, "format", output_format );
154                                 *format = output_format;
155                                 GlslManager::get_instance()->unlock_service( frame );
156                                 return error;
157                         } else {
158                                 // Use a separate chain to convert image in RAM to OpenGL texture.
159                                 // Use cached chain if available and compatible.
160                                 Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) );
161                                 chain = (EffectChain*) producer.get_data( "movit.convert.chain" );
162                                 input = (MltInput*) producer.get_data( "movit.convert.input" );
163                                 int w = producer.get_int( "movit.convert.width" );
164                                 int h = producer.get_int( "movit.convert.height" );
165                                 mlt_image_format f = (mlt_image_format) producer.get_int( "movit.convert.format" );
166                                 if ( !chain || width != w || height != h || output_format != f ) {
167                                         chain = new EffectChain( width, height );
168                                         input = new MltInput( width, height );
169                                         chain->add_input( input );
170                                         chain->add_effect( new Mlt::VerticalFlip() );
171                                         finalize_chain = true;
172                                         producer.set( "movit.convert.chain", chain, 0, (mlt_destructor) delete_chain );
173                                         producer.set( "movit.convert.input", input, 0 );
174                                         producer.set( "movit.convert.width", width );
175                                         producer.set( "movit.convert.height", height );
176                                         producer.set( "movit.convert.format", output_format );
177                                 }
178                         }
179                 }
180                 if ( *format == mlt_image_rgb24a || *format == mlt_image_opengl ) { 
181                         // TODO: Get the color space if available.
182                         input->useFlatInput( chain, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, width, height );
183                         input->set_pixel_data( *image );
184                 }
185                 else if ( *format == mlt_image_rgb24 ) {
186                         // TODO: Get the color space if available.
187                         input->useFlatInput( chain, FORMAT_RGB, width, height );
188                         input->set_pixel_data( *image );
189                 }
190                 else if ( *format == mlt_image_yuv420p ) {
191                         ImageFormat image_format;
192                         YCbCrFormat ycbcr_format;
193                         get_format_from_properties( properties, &image_format, &ycbcr_format );
194                         ycbcr_format.chroma_subsampling_x = ycbcr_format.chroma_subsampling_y = 2;
195                         input->useYCbCrInput( chain, image_format, ycbcr_format, width, height );
196                         input->set_pixel_data( *image );
197                 }
198                 else if ( *format == mlt_image_yuv422 ) {
199                         ImageFormat image_format;
200                         YCbCrFormat ycbcr_format;
201                         get_format_from_properties( properties, &image_format, &ycbcr_format );
202                         ycbcr_format.chroma_subsampling_x = 2;
203                         ycbcr_format.chroma_subsampling_y = 1;
204                         input->useYCbCrInput( chain, image_format, ycbcr_format, width, height );
205                         
206                         // convert chunky to planar
207                         uint8_t* planar = (uint8_t*) mlt_pool_alloc( img_size );
208                         yuv422_to_yuv422p( *image, planar, width, height );
209                         input->set_pixel_data( planar );
210                         mlt_frame_set_image( frame, planar, img_size, mlt_pool_release );
211                 }
212                 // Finalize the separate conversion chain if needed.
213                 if ( finalize_chain )
214                         chain->finalize();
215         }
216
217         if ( output_format != mlt_image_glsl ) {
218
219                 if ( output_format == mlt_image_glsl_texture ) {
220                         error = glsl->render_frame_texture( service, frame, width, height, image );
221                 }
222                 else {
223                         error = glsl->render_frame_rgba( service, frame, width, height, image );
224                         if ( !error && output_format != mlt_image_rgb24a ) {
225                                 *format = mlt_image_rgb24a;
226                                 error = convert_on_cpu( frame, image, format, output_format );
227                         }
228                 }
229                 mlt_properties_set_int( properties, "format", output_format );
230                 *format = output_format;
231         }
232         else {
233                 mlt_properties_set_int( properties, "format", output_format );
234                 *format = output_format;
235         }
236         GlslManager::get_instance()->unlock_service( frame );
237
238         return error;
239 }
240
241 static mlt_frame process( mlt_filter filter, mlt_frame frame )
242 {
243         // Set a default colorspace on the frame if not yet set by the producer.
244         // The producer may still change it during get_image.
245         // This way we do not have to modify each producer to set a valid colorspace.
246         mlt_properties properties = MLT_FRAME_PROPERTIES(frame);
247         if ( mlt_properties_get_int( properties, "colorspace" ) <= 0 )
248                 mlt_properties_set_int( properties, "colorspace", mlt_service_profile( MLT_FILTER_SERVICE(filter) )->colorspace );
249
250         frame->convert_image = convert_image;
251
252         mlt_filter cpu_csc = (mlt_filter) mlt_properties_get_data( MLT_FILTER_PROPERTIES( filter ), "cpu_csc", NULL );
253         mlt_properties_inc_ref( MLT_FILTER_PROPERTIES(cpu_csc) );
254         mlt_properties_set_data( properties, "cpu_csc", cpu_csc, 0,
255                 (mlt_destructor) mlt_filter_close, NULL );
256
257         return frame;
258 }
259
260 static mlt_filter create_filter( mlt_profile profile, const char *effect )
261 {
262         mlt_filter filter;
263         char *id = strdup( effect );
264         char *arg = strchr( id, ':' );
265         if ( arg != NULL )
266                 *arg ++ = '\0';
267
268         // The swscale and avcolor_space filters require resolution as arg to test compatibility
269         if ( !strcmp( effect, "avcolor_space" ) )
270                 filter = mlt_factory_filter( profile, id, &profile->width );
271         else
272                 filter = mlt_factory_filter( profile, id, arg );
273         if ( filter )
274                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
275         free( id );
276         return filter;
277 }
278
279 extern "C" {
280
281 mlt_filter filter_movit_convert_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
282 {
283         mlt_filter filter = NULL;
284         GlslManager* glsl = GlslManager::get_instance();
285
286         if ( glsl && ( filter = mlt_filter_new() ) )
287         {
288 #ifdef WIN32
289                 // XXX avcolor_space is crashing on Windows in this context!
290                 mlt_filter cpu_csc = NULL;
291 #else
292                 mlt_filter cpu_csc = create_filter( profile, "avcolor_space" );
293 #endif
294                 if ( !cpu_csc )
295                         cpu_csc = create_filter( profile, "imageconvert" );
296                 if ( cpu_csc )
297                         mlt_properties_set_data( MLT_FILTER_PROPERTIES( filter ), "cpu_csc", cpu_csc, 0,
298                                 (mlt_destructor) mlt_filter_close, NULL );
299                 filter->process = process;
300         }
301         return filter;
302 }
303
304 }