]> git.sesse.net Git - mlt/blob - src/modules/opengl/filter_movit_resize.cpp
Propertly refcount the GlslManager.
[mlt] / src / modules / opengl / filter_movit_resize.cpp
1 /*
2  * filter_movit_resize.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 <math.h>
23 #include <stdlib.h>
24 #include <assert.h>
25
26 #include "filter_glsl_manager.h"
27 #include <movit/init.h>
28 #include <movit/padding_effect.h>
29 #include "optional_effect.h"
30
31 static float alignment_parse( char* align )
32 {
33         int ret = 0.0f;
34
35         if ( align == NULL );
36         else if ( isdigit( align[ 0 ] ) )
37                 ret = atoi( align );
38         else if ( align[ 0 ] == 'c' || align[ 0 ] == 'm' )
39                 ret = 1.0f;
40         else if ( align[ 0 ] == 'r' || align[ 0 ] == 'b' )
41                 ret = 2.0f;
42
43         return ret;
44 }
45
46 static int get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
47 {
48         int error = 0;
49         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
50         mlt_filter filter = (mlt_filter) mlt_frame_pop_service( frame );
51         mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( filter ) );
52
53         // Retrieve the aspect ratio
54         double aspect_ratio = mlt_frame_get_aspect_ratio( frame );
55         double consumer_aspect = mlt_profile_sar( profile );
56
57         // Correct Width/height if necessary
58         if ( *width == 0 || *height == 0 )
59         {
60                 *width = profile->width;
61                 *height = profile->height;
62         }
63
64         // Assign requested width/height from our subordinate
65         int owidth = *width;
66         int oheight = *height;
67
68         // Use a mlt_rect to compute position and size
69         mlt_rect rect;
70         rect.x = rect.y = 0.0;
71         if ( mlt_properties_get( properties, "resize.rect" ) ) {
72                 mlt_position position = mlt_filter_get_position( filter, frame );
73                 mlt_position length = mlt_filter_get_length2( filter, frame );
74                 rect = mlt_properties_anim_get_rect( properties, "resize.rect", position, length );
75                 if ( strchr( mlt_properties_get( properties, "resize.rect" ), '%' ) ) {
76                         rect.x *= profile->width;
77                         rect.w *= profile->width;
78                         rect.y *= profile->height;
79                         rect.h *= profile->height;
80                 }
81                 if ( !mlt_properties_get_int( properties, "resize.fill" ) ) {
82                         int x = mlt_properties_get_int( properties, "meta.media.width" );
83                         rect.w = rect.w > x ? x : rect.w;
84                         x = mlt_properties_get_int( properties, "meta.media.height" );
85                         rect.h = rect.h > x ? x : rect.h;
86                 }
87                 owidth = lrintf( rect.w );
88                 oheight = lrintf( rect.h );
89         }
90
91         // Check for the special case - no aspect ratio means no problem :-)
92         if ( aspect_ratio == 0.0 )
93                 aspect_ratio = consumer_aspect;
94
95         // Reset the aspect ratio
96         mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
97
98         // Skip processing if requested.
99         char *rescale = mlt_properties_get( properties, "rescale.interp" );
100         if ( *format == mlt_image_none || ( rescale && !strcmp( rescale, "none" ) ) )
101                 return mlt_frame_get_image( frame, image, format, width, height, writable );
102
103         if ( mlt_properties_get_int( properties, "distort" ) == 0 )
104         {
105                 // Normalise the input and out display aspect
106                 int normalised_width = profile->width;
107                 int normalised_height = profile->height;
108                 int real_width = mlt_properties_get_int( properties, "meta.media.width" );
109                 int real_height = mlt_properties_get_int( properties, "meta.media.height" );
110                 if ( real_width == 0 )
111                         real_width = mlt_properties_get_int( properties, "width" );
112                 if ( real_height == 0 )
113                         real_height = mlt_properties_get_int( properties, "height" );
114                 double input_ar = aspect_ratio * real_width / real_height;
115                 double output_ar = consumer_aspect * owidth / oheight;
116                 
117                 // Optimised for the input_ar > output_ar case (e.g. widescreen on standard)
118                 int scaled_width = lrint( ( input_ar * normalised_width ) / output_ar );
119                 int scaled_height = normalised_height;
120
121                 // Now ensure that our images fit in the output frame
122                 if ( scaled_width > normalised_width )
123                 {
124                         scaled_width = normalised_width;
125                         scaled_height = lrint( ( output_ar * normalised_height ) / input_ar );
126                 }
127
128                 // Now calculate the actual image size that we want
129                 owidth = lrint( scaled_width * owidth / normalised_width );
130                 oheight = lrint( scaled_height * oheight / normalised_height );
131
132                 mlt_log_debug( MLT_FILTER_SERVICE(filter),
133                         "real %dx%d normalised %dx%d output %dx%d sar %f in-dar %f out-dar %f\n",
134                         real_width, real_height, normalised_width, normalised_height, owidth, oheight, aspect_ratio, input_ar, output_ar);
135
136                 // Tell frame we have conformed the aspect to the consumer
137                 mlt_frame_set_aspect_ratio( frame, consumer_aspect );
138         }
139
140         mlt_properties_set_int( properties, "distort", 0 );
141
142         // Now get the image
143         *format = mlt_image_glsl;
144         error = mlt_frame_get_image( frame, image, format, &owidth, &oheight, writable );
145
146         // Offset the position according to alignment
147         float w = float( *width - owidth );
148         float h = float( *height - oheight );
149         if ( mlt_properties_get( properties, "resize.rect" ) ) {
150                 // default left if rect supplied
151                 rect.x += w * alignment_parse( mlt_properties_get( properties, "resize.halign" ) ) / 2.0f;
152                 rect.y += h * alignment_parse( mlt_properties_get( properties, "resize.valign" ) ) / 2.0f;
153         } else {
154                 // default center if no rect
155                 rect.x = w * 0.5f;
156                 rect.y = h * 0.5f;
157         }
158
159         if ( !error ) {
160                 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
161                 GlslManager::get_instance()->lock_service( frame );
162                 mlt_properties_set_int( filter_properties, "movit.parms.int.width", *width );
163                 mlt_properties_set_int( filter_properties, "movit.parms.int.height", *height );
164                 mlt_properties_set_double( filter_properties, "movit.parms.float.left", rect.x );
165                 mlt_properties_set_double( filter_properties, "movit.parms.float.top", rect.y );
166
167                 bool disable = ( *width == owidth && *height == oheight );
168                 mlt_properties_set_int( filter_properties, "movit.parms.int.disable", disable );
169
170                 GlslManager::get_instance()->unlock_service( frame );
171
172                 GlslManager::set_effect_input( MLT_FILTER_SERVICE( filter ), frame, (mlt_service) *image );
173                 GlslManager::set_effect( MLT_FILTER_SERVICE( filter ), frame, new OptionalEffect<PaddingEffect> );
174                 *image = (uint8_t *) MLT_FILTER_SERVICE( filter );
175                 return error;
176         }
177
178         return error;
179 }
180
181 static mlt_frame process( mlt_filter filter, mlt_frame frame )
182 {
183         mlt_frame_push_service( frame, filter );
184         mlt_frame_push_get_image( frame, get_image );
185         return frame;
186 }
187
188 extern "C"
189 mlt_filter filter_movit_resize_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
190 {
191         mlt_filter filter = NULL;
192         GlslManager* glsl = GlslManager::get_instance();
193
194         if ( glsl && ( filter = mlt_filter_new() ) )
195         {
196                 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
197                 glsl->add_ref( properties );
198                 filter->process = process;
199         }
200         return filter;
201 }