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