2 * filter_watermark.c -- watermark filter
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_factory.h>
23 #include <framework/mlt_frame.h>
24 #include <framework/mlt_producer.h>
25 #include <framework/mlt_transition.h>
34 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
36 // Error we will return
39 // Get the watermark filter object
40 mlt_filter this = mlt_frame_pop_service( frame );
42 // Get the properties of the filter
43 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
45 mlt_service_lock( MLT_FILTER_SERVICE( this ) );
47 // Get the producer from the filter
48 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
50 // Get the composite from the filter
51 mlt_transition composite = mlt_properties_get_data( properties, "composite", NULL );
53 // Get the resource to use
54 char *resource = mlt_properties_get( properties, "resource" );
56 // Get the old resource
57 char *old_resource = mlt_properties_get( properties, "_old_resource" );
59 // Create a composite if we don't have one
60 if ( composite == NULL )
62 // Create composite via the factory
63 mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( this ) );
64 composite = mlt_factory_transition( profile, "composite", NULL );
66 // Register the composite for reuse/destruction
67 if ( composite != NULL )
68 mlt_properties_set_data( properties, "composite", composite, 0, ( mlt_destructor )mlt_transition_close, NULL );
72 if ( composite != NULL )
75 mlt_properties composite_properties = MLT_TRANSITION_PROPERTIES( composite );
77 // Pass all the composite. properties on the filter down
78 mlt_properties_pass( composite_properties, properties, "composite." );
80 if ( mlt_properties_get( properties, "composite.out" ) == NULL )
81 mlt_properties_set_int( composite_properties, "out", mlt_properties_get_int( properties, "_out" ) );
84 mlt_properties_set_int( composite_properties, "refresh", 1 );
87 // Create a producer if don't have one
88 if ( producer == NULL || ( old_resource != NULL && strcmp( resource, old_resource ) ) )
90 // Get the factory producer service
91 char *factory = mlt_properties_get( properties, "factory" );
93 // Create the producer
94 mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( this ) );
95 producer = mlt_factory_producer( profile, factory, resource );
98 if ( producer != NULL )
100 // Register the producer for reuse/destruction
101 mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
103 // Ensure that we loop
104 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
106 // Set the old resource
107 mlt_properties_set( properties, "_old_resource", resource );
111 if ( producer != NULL )
113 // Get the producer properties
114 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
116 // Now pass all producer. properties on the filter down
117 mlt_properties_pass( producer_properties, properties, "producer." );
120 mlt_service_unlock( MLT_FILTER_SERVICE( this ) );
122 // Only continue if we have both producer and composite
123 if ( composite != NULL && producer != NULL )
125 // Get the service of the producer
126 mlt_service service = MLT_PRODUCER_SERVICE( producer );
128 // We will get the 'b frame' from the producer
129 mlt_frame b_frame = NULL;
131 // Get the unique id of the filter (used to reacquire the producer position)
132 char *name = mlt_properties_get( properties, "_unique_id" );
134 // Get the original producer position
135 mlt_position position = mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), name );
137 // Make sure the producer is in the correct position
138 mlt_producer_seek( producer, position );
140 // Resetting position to appease the composite transition
141 mlt_frame_set_position( frame, position );
143 // Get the b frame and process with composite if successful
144 if ( mlt_service_get_frame( service, &b_frame, 0 ) == 0 )
146 // Get the a and b frame properties
147 mlt_properties a_props = MLT_FRAME_PROPERTIES( frame );
148 mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
150 // Set the b frame to be in the same position and have same consumer requirements
151 mlt_frame_set_position( b_frame, position );
152 mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
153 mlt_properties_set_int( b_props, "consumer_deinterlace", mlt_properties_get_int( a_props, "consumer_deinterlace" ) || mlt_properties_get_int( properties, "deinterlace" ) );
154 mlt_properties_set_double( b_props, "output_ratio", mlt_properties_get_double( a_props, "output_ratio" ) );
156 // Check for the special case - no aspect ratio means no problem :-)
157 if ( mlt_frame_get_aspect_ratio( b_frame ) == 0 )
158 mlt_properties_set_double( b_props, "aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
159 if ( mlt_frame_get_aspect_ratio( frame ) == 0 )
160 mlt_properties_set_double( a_props, "aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
162 mlt_properties_set_int( b_props, "normalised_width", mlt_properties_get_int( a_props, "normalised_width" ) );
163 mlt_properties_set_int( b_props, "normalised_height", mlt_properties_get_int( a_props, "normalised_height" ) );
165 if ( mlt_properties_get_int( properties, "distort" ) )
167 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( composite ), "distort", 1 );
168 mlt_properties_set_int( a_props, "distort", 1 );
169 mlt_properties_set_int( b_props, "distort", 1 );
172 *format = mlt_image_yuv422;
173 if ( mlt_properties_get_int( properties, "reverse" ) == 0 )
175 // Apply all filters that are attached to this filter to the b frame
176 mlt_service_apply_filters( MLT_FILTER_SERVICE( this ), b_frame, 0 );
179 mlt_transition_process( composite, frame, b_frame );
182 error = mlt_frame_get_image( frame, image, format, width, height, 1 );
188 uint8_t *alpha = NULL;
189 const char *rescale = mlt_properties_get( a_props, "rescale.interp" );
190 if ( rescale == NULL || !strcmp( rescale, "none" ) )
192 mlt_transition_process( composite, b_frame, frame );
193 mlt_properties_set_int( a_props, "consumer_deinterlace", 1 );
194 mlt_properties_set_int( b_props, "consumer_deinterlace", 1 );
195 mlt_properties_set( a_props, "rescale.interp", rescale );
196 mlt_properties_set( b_props, "rescale.interp", rescale );
197 mlt_service_apply_filters( MLT_FILTER_SERVICE( this ), b_frame, 0 );
198 error = mlt_frame_get_image( b_frame, image, format, width, height, 1 );
199 alpha = mlt_frame_get_alpha_mask( b_frame );
200 mlt_properties_set_data( a_props, "image", *image, *width * *height * 2, NULL, NULL );
201 mlt_properties_set_data( a_props, "alpha", alpha, *width * *height, NULL, NULL );
202 mlt_properties_set_int( a_props, "width", *width );
203 mlt_properties_set_int( a_props, "height", *height );
204 mlt_properties_set_int( a_props, "progressive", 1 );
205 mlt_properties_inc_ref( b_props );
206 strcpy( temp, "_b_frame" );
207 while( mlt_properties_get_data( a_props, temp, NULL ) != NULL )
208 sprintf( temp, "_b_frame%d", count ++ );
209 mlt_properties_set_data( a_props, temp, b_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
214 mlt_frame_close( b_frame );
218 // Get the image from the frame without running fx
219 error = mlt_frame_get_image( frame, image, format, width, height, 1 );
225 /** Filter processing.
228 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
230 // Get the properties of the frame
231 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
233 // Get a unique name to store the frame position
234 char *name = mlt_properties_get( MLT_FILTER_PROPERTIES( this ), "_unique_id" );
236 // Assign the frame out point to the filter (just in case we need it later)
237 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "_out", mlt_properties_get_int( properties, "out" ) );
239 // Assign the current position to the name
240 mlt_properties_set_position( properties, name, mlt_frame_get_position( frame ) - mlt_filter_get_in( this ) );
242 // Push the filter on to the stack
243 mlt_frame_push_service( frame, this );
245 // Push the get_image on to the stack
246 mlt_frame_push_get_image( frame, filter_get_image );
251 /** Constructor for the filter.
254 mlt_filter filter_watermark_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
256 mlt_filter this = mlt_filter_new( );
259 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
260 this->process = filter_process;
261 mlt_properties_set( properties, "factory", mlt_environment( "MLT_PRODUCER" ) );
263 mlt_properties_set( properties, "resource", arg );
264 // Ensure that attached filters are handled privately
265 mlt_properties_set_int( properties, "_filter_private", 1 );