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 "glsl_manager.h"
25 #include <movit/padding_effect.h>
27 static int get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
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;
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 )
40 *width = mlt_properties_get_int( properties, "meta.media.width" );
41 *height = mlt_properties_get_int( properties, "meta.media.height" );
43 if ( *width == 0 || *height == 0 )
45 *width = profile->width;
46 *height = profile->height;
48 mlt_properties_set_int( properties, "rescale_width", *width );
49 mlt_properties_set_int( properties, "rescale_height", *height );
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 );
56 // Skip processing if requested.
57 if ( requested_format == mlt_image_none )
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 );
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;
75 mlt_log_debug( MLT_FILTER_SERVICE(filter), "%dx%d -> %dx%d\n", *width, *height, owidth, oheight);
77 GlslManager::get_instance()->lock_service( frame );
78 Effect* effect = GlslManager::get_effect( filter, frame );
80 bool ok = effect->set_int( "width", owidth );
81 ok |= effect->set_int( "height", oheight );
82 ok |= effect->set_float( "left", -left );
83 ok |= effect->set_float( "top", -top );
88 GlslManager::get_instance()->unlock_service( frame );
94 static mlt_frame process( mlt_filter filter, mlt_frame frame )
96 mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
97 if ( !GlslManager::init_chain( MLT_PRODUCER_SERVICE(producer) ) ) {
98 Effect* effect = GlslManager::add_effect( filter, frame, new PaddingEffect );
99 RGBATuple border_color( 0.0f, 0.0f, 0.0f, 1.0f );
100 bool ok = effect->set_vec4( "border_color", (float*) &border_color );
103 mlt_frame_push_service( frame, filter );
104 mlt_frame_push_get_image( frame, get_image );
109 mlt_filter filter_movit_crop_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
111 mlt_filter filter = NULL;
112 GlslManager* glsl = GlslManager::get_instance();
114 if ( glsl && ( filter = mlt_filter_new() ) ) {
115 filter->process = process;