]> git.sesse.net Git - mlt/blob - src/modules/opengl/filter_movit_crop.cpp
2927f6bd9eab3b2a83a291ef6594ba03e9f902f9
[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         if ( mlt_properties_get_int( properties, "test_image" ) )
55                 *format = mlt_image_yuv422;
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                 GlslManager::get_instance()->lock_service( frame );
80                 Effect* effect = GlslManager::get_effect( filter, frame );
81                 if ( effect ) {
82                         bool ok = effect->set_int( "width", owidth );
83                         ok |= effect->set_int( "height", oheight );
84                         ok |= effect->set_float( "left", -left );
85                         ok |= effect->set_float( "top", -top );
86                         assert(ok);
87                         *width = owidth;
88                         *height = oheight;
89                 }
90                 GlslManager::get_instance()->unlock_service( frame );
91         }
92
93         return error;
94 }
95
96 static mlt_frame process( mlt_filter filter, mlt_frame frame )
97 {
98         mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
99         if ( !GlslManager::init_chain( MLT_PRODUCER_SERVICE(producer) ) ) {
100                 Effect* effect = GlslManager::add_effect( filter, frame, new PaddingEffect );
101                 RGBATuple border_color( 0.0f, 0.0f, 0.0f, 1.0f );
102                 bool ok = effect->set_vec4( "border_color", (float*) &border_color );
103                 assert(ok);
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                 filter->process = process;
118         }
119         return filter;
120 }