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