2 * filter_movit_crop.cpp
3 * Copyright (C) 2013 Dan Dennedy <dan@dennedy.org>
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.
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.
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.
20 #include <framework/mlt.h>
24 #include "filter_glsl_manager.h"
25 #include <movit/padding_effect.h>
26 #include "optional_effect.h"
28 using namespace movit;
30 static int get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
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;
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 )
43 *width = mlt_properties_get_int( properties, "meta.media.width" );
44 *height = mlt_properties_get_int( properties, "meta.media.height" );
46 if ( *width == 0 || *height == 0 )
48 *width = profile->width;
49 *height = profile->height;
51 mlt_properties_set_int( properties, "rescale_width", *width );
52 mlt_properties_set_int( properties, "rescale_height", *height );
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 );
58 // Skip processing if requested.
59 if ( requested_format == mlt_image_none )
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 );
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;
77 mlt_log_debug( MLT_FILTER_SERVICE(filter), "%dx%d -> %dx%d\n", *width, *height, owidth, oheight);
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 );
86 bool disable = ( *width == owidth && *height == oheight );
87 mlt_properties_set_int( properties, "_movit.parms.int.disable", disable );
89 GlslManager::get_instance()->unlock_service( frame );
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> );
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 );
102 static mlt_frame process( mlt_filter filter, mlt_frame frame )
104 mlt_frame_push_service( frame, filter );
105 mlt_frame_push_get_image( frame, get_image );
110 mlt_filter filter_movit_crop_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
112 mlt_filter filter = NULL;
113 GlslManager* glsl = GlslManager::get_instance();
115 if ( glsl && ( filter = mlt_filter_new() ) ) {
116 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
117 glsl->add_ref( properties );
118 filter->process = process;