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