]> git.sesse.net Git - mlt/blob - src/modules/core/filter_watermark.c
f1ab0e41ee9180c3631940beef76d91be5eefab1
[mlt] / src / modules / core / filter_watermark.c
1 /*
2  * filter_watermark.c -- watermark filter
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
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.
10  *
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.
15  *
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
19  */
20
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>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 /** Do it :-).
32 */
33
34 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
35 {
36         // Error we will return
37         int error = 0;
38
39         // Get the watermark filter object
40         mlt_filter this = mlt_frame_pop_service( frame );
41
42         // Get the properties of the filter
43         mlt_properties properties = MLT_FILTER_PROPERTIES( this );
44
45         // Get the producer from the filter
46         mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
47
48         // Get the composite from the filter
49         mlt_transition composite = mlt_properties_get_data( properties, "composite", NULL );
50
51         // Get the resource to use
52         char *resource = mlt_properties_get( properties, "resource" );
53
54         // Get the old resource
55         char *old_resource = mlt_properties_get( properties, "_old_resource" );
56
57         // Create a composite if we don't have one
58         if ( composite == NULL )
59         {
60                 // Create composite via the factory
61                 mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( this ) );
62                 composite = mlt_factory_transition( profile, "composite", NULL );
63
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 );
67         }
68
69         // If we have one
70         if ( composite != NULL )
71         {
72                 // Get the properties
73                 mlt_properties composite_properties = MLT_TRANSITION_PROPERTIES( composite );
74
75                 // Pass all the composite. properties on the filter down
76                 mlt_properties_pass( composite_properties, properties, "composite." );
77
78                 if ( mlt_properties_get( properties, "composite.out" ) == NULL )
79                         mlt_properties_set_int( composite_properties, "out", mlt_properties_get_int( properties, "_out" ) );
80
81                 // Force a refresh
82                 mlt_properties_set_int( composite_properties, "refresh", 1 );
83         }
84
85         // Create a producer if don't have one
86         if ( producer == NULL || ( old_resource != NULL && strcmp( resource, old_resource ) ) )
87         {
88                 // Get the factory producer service
89                 char *factory = mlt_properties_get( properties, "factory" );
90
91                 // Create the producer
92                 mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( this ) );
93                 producer = mlt_factory_producer( profile, factory, resource );
94
95                 // If we have one
96                 if ( producer != NULL )
97                 {
98                         // Register the producer for reuse/destruction
99                         mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
100
101                         // Ensure that we loop
102                         mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
103
104                         // Set the old resource
105                         mlt_properties_set( properties, "_old_resource", resource );
106                 }
107         }
108
109         if ( producer != NULL )
110         {
111                 // Get the producer properties
112                 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
113
114                 // Now pass all producer. properties on the filter down
115                 mlt_properties_pass( producer_properties, properties, "producer." );
116         }
117
118         // Only continue if we have both producer and composite
119         if ( composite != NULL && producer != NULL )
120         {
121                 // Get the service of the producer
122                 mlt_service service = MLT_PRODUCER_SERVICE( producer );
123
124                 // We will get the 'b frame' from the producer
125                 mlt_frame b_frame = NULL;
126
127                 // Get the unique id of the filter (used to reacquire the producer position)
128                 char *name = mlt_properties_get( properties, "_unique_id" );
129
130                 // Get the original producer position
131                 mlt_position position = mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), name );
132
133                 // Make sure the producer is in the correct position
134                 mlt_producer_seek( producer, position );
135
136                 // Resetting position to appease the composite transition
137                 mlt_frame_set_position( frame, position );
138
139                 // Get the b frame and process with composite if successful
140                 if ( mlt_service_get_frame( service, &b_frame, 0 ) == 0 )
141                 {
142                         // Get the a and b frame properties
143                         mlt_properties a_props = MLT_FRAME_PROPERTIES( frame );
144                         mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
145
146                         // Set the b frame to be in the same position and have same consumer requirements
147                         mlt_frame_set_position( b_frame, position );
148                         mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
149                         mlt_properties_set_int( b_props, "consumer_deinterlace", mlt_properties_get_int( a_props, "consumer_deinterlace" ) || mlt_properties_get_int( properties, "deinterlace" ) );
150                         mlt_properties_set_double( b_props, "output_ratio", mlt_properties_get_double( a_props, "output_ratio" ) );
151
152                         // Check for the special case - no aspect ratio means no problem :-)
153                         if ( mlt_frame_get_aspect_ratio( b_frame ) == 0 )
154                                 mlt_properties_set_double( b_props, "aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
155                         if ( mlt_frame_get_aspect_ratio( frame ) == 0 )
156                                 mlt_properties_set_double( a_props, "aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
157
158                         mlt_properties_set_int( b_props, "normalised_width", mlt_properties_get_int( a_props, "normalised_width" ) );
159                         mlt_properties_set_int( b_props, "normalised_height", mlt_properties_get_int( a_props, "normalised_height" ) );
160
161                         if ( mlt_properties_get_int( properties, "distort" ) )
162                         {
163                                 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( composite ), "distort", 1 );
164                                 mlt_properties_set_int( a_props, "distort", 1 );
165                                 mlt_properties_set_int( b_props, "distort", 1 );
166                         }
167
168                         if ( mlt_properties_get_int( properties, "reverse" ) == 0 )
169                         {
170                                 // Apply all filters that are attached to this filter to the b frame
171                                 mlt_service_apply_filters( MLT_FILTER_SERVICE( this ), b_frame, 0 );
172
173                                 // Process the frame
174                                 mlt_transition_process( composite, frame, b_frame );
175
176                                 // Get the image
177                                 error = mlt_frame_get_image( frame, image, format, width, height, 1 );
178                         }
179                         else
180                         {
181                                 char temp[ 132 ];
182                                 int count = 0;
183                                 uint8_t *alpha = NULL;
184                                 const char *rescale = mlt_properties_get( a_props, "rescale.interp" );
185                                 if ( rescale == NULL || !strcmp( rescale, "none" ) )
186                                         rescale = "hyper";
187                                 mlt_transition_process( composite, b_frame, frame );
188                                 mlt_properties_set_int( a_props, "consumer_deinterlace", 1 );
189                                 mlt_properties_set_int( b_props, "consumer_deinterlace", 1 );
190                                 mlt_properties_set( a_props, "rescale.interp", rescale );
191                                 mlt_properties_set( b_props, "rescale.interp", rescale );
192                                 mlt_service_apply_filters( MLT_FILTER_SERVICE( this ), b_frame, 0 );
193                                 error = mlt_frame_get_image( b_frame, image, format, width, height, 1 );
194                                 alpha = mlt_frame_get_alpha_mask( b_frame );
195                                 mlt_properties_set_data( a_props, "image", *image, *width * *height * 2, NULL, NULL );
196                                 mlt_properties_set_data( a_props, "alpha", alpha, *width * *height, NULL, NULL );
197                                 mlt_properties_set_int( a_props, "width", *width );
198                                 mlt_properties_set_int( a_props, "height", *height );
199                                 mlt_properties_set_int( a_props, "progressive", 1 );
200                                 mlt_properties_inc_ref( b_props );
201                                 strcpy( temp, "_b_frame" );
202                                 while( mlt_properties_get_data( a_props, temp, NULL ) != NULL )
203                                         sprintf( temp, "_b_frame%d", count ++ );
204                                 mlt_properties_set_data( a_props, temp, b_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
205                         }
206                 }
207
208                 // Close the b frame
209                 mlt_frame_close( b_frame );
210         }
211         else
212         {
213                 // Get the image from the frame without running fx
214                 error = mlt_frame_get_image( frame, image, format, width, height, 1 );
215         }
216
217         return error;
218 }
219
220 /** Filter processing.
221 */
222
223 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
224 {
225         // Get the properties of the frame
226         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
227
228         // Get a unique name to store the frame position
229         char *name = mlt_properties_get( MLT_FILTER_PROPERTIES( this ), "_unique_id" );
230
231         // Assign the frame out point to the filter (just in case we need it later)
232         mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "_out", mlt_properties_get_int( properties, "out" ) );
233
234         // Assign the current position to the name
235         mlt_properties_set_position( properties, name, mlt_frame_get_position( frame ) - mlt_filter_get_in( this ) );
236
237         // Push the filter on to the stack
238         mlt_frame_push_service( frame, this );
239
240         // Push the get_image on to the stack
241         mlt_frame_push_get_image( frame, filter_get_image );
242
243         return frame;
244 }
245
246 /** Constructor for the filter.
247 */
248
249 mlt_filter filter_watermark_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
250 {
251         mlt_filter this = mlt_filter_new( );
252         if ( this != NULL )
253         {
254                 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
255                 this->process = filter_process;
256                 mlt_properties_set( properties, "factory", mlt_environment( "MLT_PRODUCER" ) );
257                 if ( arg != NULL )
258                         mlt_properties_set( properties, "resource", arg );
259                 // Ensure that attached filters are handled privately
260                 mlt_properties_set_int( properties, "_filter_private", 1 );
261         }
262         return this;
263 }
264