]> git.sesse.net Git - mlt/blob - src/modules/opengl/filter_movit_crop.cpp
24ae8702d298bc2e8d1c34b0b952a914326aba69
[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         *format = mlt_image_none;
57         if ( mlt_properties_get_int( properties, "test_image" ) )
58                 *format = mlt_image_yuv422;
59         error = mlt_frame_get_image( frame, image, format, width, height, writable );
60
61         // Skip processing if requested.
62         if ( requested_format == mlt_image_none )
63                 return error;
64
65         if ( !error && *format != mlt_image_glsl && frame->convert_image ) {
66         // Pin the requested format to the first one returned.
67 //              mlt_properties_set_int( MLT_PRODUCER_PROPERTIES(producer), "_movit image_format", *format );
68                 error = frame->convert_image( frame, image, format, mlt_image_glsl );
69         }
70         if ( !error ) {
71                 double left    = mlt_properties_get_double( properties, "crop.left" );
72                 double right   = mlt_properties_get_double( properties, "crop.right" );
73                 double top     = mlt_properties_get_double( properties, "crop.top" );
74                 double bottom  = mlt_properties_get_double( properties, "crop.bottom" );
75                 int owidth  = *width - left - right;
76                 int oheight = *height - top - bottom;
77                 owidth = owidth < 0 ? 0 : owidth;
78                 oheight = oheight < 0 ? 0 : oheight;
79
80                 mlt_log_debug( MLT_FILTER_SERVICE(filter), "%dx%d -> %dx%d\n", *width, *height, owidth, oheight);
81
82                 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
83                 GlslManager::get_instance()->lock_service( frame );
84                 mlt_properties_set_int( properties, "movit.parms.int.width", owidth );
85                 mlt_properties_set_int( properties, "movit.parms.int.height", oheight );
86                 mlt_properties_set_double( properties, "movit.parms.float.left", -left );
87                 mlt_properties_set_double( properties, "movit.parms.float.top", -top );
88
89                 bool disable = ( *width == owidth && *height == oheight );
90                 mlt_properties_set_int( properties, "movit.parms.int.disable", disable );
91
92                 GlslManager::get_instance()->unlock_service( frame );
93         }
94
95         GlslManager::set_effect_input( MLT_FILTER_SERVICE( filter ), frame, (mlt_service) *image );
96         Effect* effect = GlslManager::set_effect( MLT_FILTER_SERVICE( filter ), frame, new OptionalEffect<PaddingEffect> );
97         assert(effect);
98         *image = (uint8_t *) MLT_FILTER_SERVICE( filter );
99         RGBATuple border_color( 0.0f, 0.0f, 0.0f, 1.0f );
100         bool ok = effect->set_vec4( "border_color", (float*) &border_color );
101         assert(ok);
102         return error;
103 }
104
105 static mlt_frame process( mlt_filter filter, mlt_frame frame )
106 {
107         mlt_frame_push_service( frame, filter );
108         mlt_frame_push_get_image( frame, get_image );
109         return frame;
110 }
111
112 extern "C"
113 mlt_filter filter_movit_crop_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
114 {
115         mlt_filter filter = NULL;
116         GlslManager* glsl = GlslManager::get_instance();
117
118         if ( glsl && ( filter = mlt_filter_new() ) ) {
119                 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
120                 glsl->add_ref( properties );
121                 filter->process = process;
122         }
123         return filter;
124 }