]> git.sesse.net Git - mlt/blob - src/modules/opengl/filter_movit_crop.cpp
Propertly refcount the GlslManager.
[mlt] / src / modules / opengl / filter_movit_crop.cpp
1 /*
2  * filter_movit_crop.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 <assert.h>
23
24 #include "filter_glsl_manager.h"
25 #include <movit/padding_effect.h>
26 #include "optional_effect.h"
27
28 static int get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
29 {
30         int error = 0;
31         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
32         mlt_filter filter = (mlt_filter) mlt_frame_pop_service( frame );
33         mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( filter ) );
34         mlt_image_format requested_format = *format;
35
36         // Correct width/height if necessary
37         *width = mlt_properties_get_int( properties, "crop.original_width" );
38         *height = mlt_properties_get_int( properties, "crop.original_height" );
39         if ( *width == 0 || *height == 0 )
40         {
41                 *width = mlt_properties_get_int( properties, "meta.media.width" );
42                 *height = mlt_properties_get_int( properties, "meta.media.height" );
43         }
44         if ( *width == 0 || *height == 0 )
45         {
46                 *width = profile->width;
47                 *height = profile->height;
48         }
49         mlt_properties_set_int( properties, "rescale_width", *width );
50         mlt_properties_set_int( properties, "rescale_height", *height );
51
52         // Get the image as requested
53 //      *format = (mlt_image_format) mlt_properties_get_int( MLT_PRODUCER_PROPERTIES(producer), "_movit image_format" );
54         *format = mlt_image_none;
55         if ( mlt_properties_get_int( properties, "test_image" ) )
56                 *format = mlt_image_yuv422;
57         error = mlt_frame_get_image( frame, image, format, width, height, writable );
58
59         // Skip processing if requested.
60         if ( requested_format == mlt_image_none )
61                 return error;
62
63         if ( !error && *format != mlt_image_glsl && frame->convert_image ) {
64         // Pin the requested format to the first one returned.
65 //              mlt_properties_set_int( MLT_PRODUCER_PROPERTIES(producer), "_movit image_format", *format );
66                 error = frame->convert_image( frame, image, format, mlt_image_glsl );
67         }
68         if ( !error ) {
69                 double left    = mlt_properties_get_double( properties, "crop.left" );
70                 double right   = mlt_properties_get_double( properties, "crop.right" );
71                 double top     = mlt_properties_get_double( properties, "crop.top" );
72                 double bottom  = mlt_properties_get_double( properties, "crop.bottom" );
73                 int owidth  = *width - left - right;
74                 int oheight = *height - top - bottom;
75                 owidth = owidth < 0 ? 0 : owidth;
76                 oheight = oheight < 0 ? 0 : oheight;
77
78                 mlt_log_debug( MLT_FILTER_SERVICE(filter), "%dx%d -> %dx%d\n", *width, *height, owidth, oheight);
79
80                 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
81                 GlslManager::get_instance()->lock_service( frame );
82                 mlt_properties_set_int( properties, "movit.parms.int.width", owidth );
83                 mlt_properties_set_int( properties, "movit.parms.int.height", oheight );
84                 mlt_properties_set_double( properties, "movit.parms.float.left", -left );
85                 mlt_properties_set_double( properties, "movit.parms.float.top", -top );
86
87                 bool disable = ( *width == owidth && *height == oheight );
88                 mlt_properties_set_int( properties, "movit.parms.int.disable", disable );
89
90                 GlslManager::get_instance()->unlock_service( frame );
91         }
92
93         GlslManager::set_effect_input( MLT_FILTER_SERVICE( filter ), frame, (mlt_service) *image );
94         Effect* effect = GlslManager::set_effect( MLT_FILTER_SERVICE( filter ), frame, new OptionalEffect<PaddingEffect> );
95         assert(effect);
96         *image = (uint8_t *) MLT_FILTER_SERVICE( filter );
97         RGBATuple border_color( 0.0f, 0.0f, 0.0f, 1.0f );
98         bool ok = effect->set_vec4( "border_color", (float*) &border_color );
99         assert(ok);
100         return error;
101 }
102
103 static mlt_frame process( mlt_filter filter, mlt_frame frame )
104 {
105         mlt_frame_push_service( frame, filter );
106         mlt_frame_push_get_image( frame, get_image );
107         return frame;
108 }
109
110 extern "C"
111 mlt_filter filter_movit_crop_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
112 {
113         mlt_filter filter = NULL;
114         GlslManager* glsl = GlslManager::get_instance();
115
116         if ( glsl && ( filter = mlt_filter_new() ) ) {
117                 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
118                 glsl->add_ref( properties );
119                 filter->process = process;
120         }
121         return filter;
122 }