2 * filter_watermark.c -- watermark filter
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "filter_watermark.h"
23 #include <framework/mlt_factory.h>
24 #include <framework/mlt_frame.h>
25 #include <framework/mlt_producer.h>
26 #include <framework/mlt_transition.h>
35 static int filter_get_image( mlt_frame frame
, uint8_t **image
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
37 // Error we will return
40 // Get the watermark filter object
41 mlt_filter
this = mlt_frame_pop_service( frame
);
43 // Get the properties of the filter
44 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
46 // Get the producer from the filter
47 mlt_producer producer
= mlt_properties_get_data( properties
, "producer", NULL
);
49 // Get the composite from the filter
50 mlt_transition composite
= mlt_properties_get_data( properties
, "composite", NULL
);
52 // Get the resource to use
53 char *resource
= mlt_properties_get( properties
, "resource" );
55 // Get the old resource
56 char *old_resource
= mlt_properties_get( properties
, "_old_resource" );
58 // Create a composite if we don't have one
59 if ( composite
== NULL
)
61 // Create composite via the factory
62 composite
= mlt_factory_transition( "composite", NULL
);
64 // Register the composite for reuse/destruction
65 if ( composite
!= NULL
)
66 mlt_properties_set_data( properties
, "composite", composite
, 0, ( mlt_destructor
)mlt_transition_close
, NULL
);
70 if ( composite
!= NULL
)
73 mlt_properties composite_properties
= MLT_TRANSITION_PROPERTIES( composite
);
75 // Pass all the composite. properties on the filter down
76 mlt_properties_pass( composite_properties
, properties
, "composite." );
79 mlt_properties_set_int( composite_properties
, "refresh", 1 );
82 // Create a producer if don't have one
83 if ( producer
== NULL
|| ( old_resource
!= NULL
&& strcmp( resource
, old_resource
) ) )
85 // Get the factory producer service
86 char *factory
= mlt_properties_get( properties
, "factory" );
88 // Create the producer
89 producer
= mlt_factory_producer( factory
, resource
);
92 if ( producer
!= NULL
)
94 // Register the producer for reuse/destruction
95 mlt_properties_set_data( properties
, "producer", producer
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
97 // Ensure that we loop
98 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer
), "eof", "loop" );
100 // Set the old resource
101 mlt_properties_set( properties
, "_old_resource", resource
);
105 if ( producer
!= NULL
)
107 // Get the producer properties
108 mlt_properties producer_properties
= MLT_PRODUCER_PROPERTIES( producer
);
110 // Now pass all producer. properties on the filter down
111 mlt_properties_pass( producer_properties
, properties
, "producer." );
114 // Only continue if we have both producer and composite
115 if ( composite
!= NULL
&& producer
!= NULL
)
117 // Get the service of the producer
118 mlt_service service
= MLT_PRODUCER_SERVICE( producer
);
120 // We will get the 'b frame' from the producer
121 mlt_frame b_frame
= NULL
;
123 // Get the unique id of the filter (used to reacquire the producer position)
124 char *name
= mlt_properties_get( properties
, "_unique_id" );
126 // Get the original producer position
127 mlt_position position
= mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame
), name
);
129 // Make sure the producer is in the correct position
130 mlt_producer_seek( producer
, position
);
132 // Resetting position to appease the composite transition
133 mlt_frame_set_position( frame
, position
);
135 // Get the b frame and process with composite if successful
136 if ( mlt_service_get_frame( service
, &b_frame
, 0 ) == 0 )
138 // Get the a and b frame properties
139 mlt_properties a_props
= MLT_FRAME_PROPERTIES( frame
);
140 mlt_properties b_props
= MLT_FRAME_PROPERTIES( b_frame
);
142 // Set the b frame to be in the same position and have same consumer requirements
143 mlt_frame_set_position( b_frame
, position
);
144 mlt_properties_set_double( b_props
, "consumer_aspect_ratio", mlt_properties_get_double( a_props
, "consumer_aspect_ratio" ) );
145 mlt_properties_set_int( b_props
, "consumer_deinterlace", mlt_properties_get_double( a_props
, "consumer_deinterlace" ) );
147 // Check for the special case - no aspect ratio means no problem :-)
148 if ( mlt_frame_get_aspect_ratio( b_frame
) == 0 )
149 mlt_properties_set_double( b_props
, "aspect_ratio", mlt_properties_get_double( a_props
, "consumer_aspect_ratio" ) );
150 if ( mlt_frame_get_aspect_ratio( frame
) == 0 )
151 mlt_properties_set_double( a_props
, "aspect_ratio", mlt_properties_get_double( a_props
, "consumer_aspect_ratio" ) );
153 mlt_properties_set_int( b_props
, "normalised_width", mlt_properties_get_int( a_props
, "normalised_width" ) );
154 mlt_properties_set_int( b_props
, "normalised_height", mlt_properties_get_int( a_props
, "normalised_height" ) );
156 if ( mlt_properties_get_int( properties
, "distort" ) )
158 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( composite
), "distort", 1 );
159 mlt_properties_set_int( a_props
, "distort", 1 );
160 mlt_properties_set_int( b_props
, "distort", 1 );
163 if ( mlt_properties_get_int( properties
, "reverse" ) == 0 )
165 // Apply all filters that are attached to this filter to the b frame
166 mlt_service_apply_filters( MLT_FILTER_SERVICE( this ), b_frame
, 0 );
169 mlt_transition_process( composite
, frame
, b_frame
);
172 error
= mlt_frame_get_image( frame
, image
, format
, width
, height
, 1 );
178 uint8_t *alpha
= NULL
;
179 char *rescale
= mlt_properties_get( a_props
, "rescale.interp" );
180 if ( rescale
== NULL
|| !strcmp( rescale
, "none" ) )
182 mlt_transition_process( composite
, b_frame
, frame
);
183 mlt_properties_set_int( a_props
, "consumer_deinterlace", 1 );
184 mlt_properties_set_int( b_props
, "consumer_deinterlace", 1 );
185 mlt_properties_set( a_props
, "rescale.interp", rescale
);
186 mlt_properties_set( b_props
, "rescale.interp", rescale
);
187 mlt_service_apply_filters( MLT_FILTER_SERVICE( this ), b_frame
, 0 );
188 error
= mlt_frame_get_image( b_frame
, image
, format
, width
, height
, 1 );
189 alpha
= mlt_frame_get_alpha_mask( b_frame
);
190 mlt_properties_set_data( a_props
, "image", *image
, *width
* *height
* 2, NULL
, NULL
);
191 mlt_properties_set_data( a_props
, "alpha", alpha
, *width
* *height
, NULL
, NULL
);
192 mlt_properties_set_int( a_props
, "width", *width
);
193 mlt_properties_set_int( a_props
, "height", *height
);
194 mlt_properties_set_int( a_props
, "progressive", 1 );
195 mlt_properties_inc_ref( b_props
);
196 strcpy( temp
, "_b_frame" );
197 while( mlt_properties_get_data( a_props
, temp
, NULL
) != NULL
)
198 sprintf( temp
, "_b_frame%d", count
++ );
199 mlt_properties_set_data( a_props
, temp
, b_frame
, 0, ( mlt_destructor
)mlt_frame_close
, NULL
);
204 mlt_frame_close( b_frame
);
208 // Get the image from the frame without running fx
209 error
= mlt_frame_get_image( frame
, image
, format
, width
, height
, 1 );
215 /** Filter processing.
218 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
220 // Get the properties of the frame
221 mlt_properties properties
= MLT_FRAME_PROPERTIES( frame
);
223 // Get a unique name to store the frame position
224 char *name
= mlt_properties_get( MLT_FILTER_PROPERTIES( this ), "_unique_id" );
226 // Assign the current position to the name
227 mlt_properties_set_position( properties
, name
, mlt_frame_get_position( frame
) - mlt_filter_get_in( this ) );
229 // Push the filter on to the stack
230 mlt_frame_push_service( frame
, this );
232 // Push the get_image on to the stack
233 mlt_frame_push_get_image( frame
, filter_get_image
);
238 /** Constructor for the filter.
241 mlt_filter
filter_watermark_init( void *arg
)
243 mlt_filter
this = mlt_filter_new( );
246 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
247 this->process
= filter_process
;
248 mlt_properties_set( properties
, "factory", "fezzik" );
250 mlt_properties_set( properties
, "resource", arg
);
251 // Ensure that attached filters are handled privately
252 mlt_properties_set_int( properties
, "_filter_private", 1 );