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 // Create a producer if don't have one
80 if ( producer
== NULL
|| ( old_resource
!= NULL
&& strcmp( resource
, old_resource
) ) )
82 // Get the factory producer service
83 char *factory
= mlt_properties_get( properties
, "factory" );
85 // Create the producer
86 producer
= mlt_factory_producer( factory
, resource
);
89 if ( producer
!= NULL
)
91 // Register the producer for reuse/destruction
92 mlt_properties_set_data( properties
, "producer", producer
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
94 // Ensure that we loop
95 mlt_properties_set( mlt_producer_properties( producer
), "eof", "loop" );
97 // Set the old resource
98 mlt_properties_set( properties
, "_old_resource", resource
);
102 if ( producer
!= NULL
)
104 // Get the producer properties
105 mlt_properties producer_properties
= mlt_producer_properties( producer
);
107 // Now pass all producer. properties on the filter down
108 mlt_properties_pass( producer_properties
, properties
, "producer." );
111 // Only continue if we have both producer and composite
112 if ( composite
!= NULL
&& producer
!= NULL
)
114 // Get the service of the producer
115 mlt_service service
= mlt_producer_service( producer
);
117 // We will get the 'b frame' from the producer
118 mlt_frame b_frame
= NULL
;
120 // Get the unique id of the filter (used to reacquire the producer position)
121 char *name
= mlt_properties_get( properties
, "_unique_id" );
123 // Get the original producer position
124 mlt_position position
= mlt_properties_get_position( mlt_frame_properties( frame
), name
);
126 // Make sure the producer is in the correct position
127 mlt_producer_seek( producer
, position
);
129 // Resetting position to appease the composite transition
130 mlt_frame_set_position( frame
, position
);
132 // Get the b frame and process with composite if successful
133 if ( mlt_service_get_frame( service
, &b_frame
, 0 ) == 0 )
135 // Get the a and b frame properties
136 mlt_properties a_props
= mlt_frame_properties( frame
);
137 mlt_properties b_props
= mlt_frame_properties( b_frame
);
139 // Set the b frame to be in the same position and have same consumer requirements
140 mlt_frame_set_position( b_frame
, position
);
141 mlt_properties_set_double( b_props
, "consumer_aspect_ratio", mlt_properties_get_double( a_props
, "consumer_aspect_ratio" ) );
142 mlt_properties_set_int( b_props
, "consumer_progressive", mlt_properties_get_double( a_props
, "consumer_progressive" ) );
144 if ( mlt_properties_get_int( properties
, "reverse" ) == 0 )
146 // Apply all filters that are attached to this filter to the b frame
147 mlt_service_apply_filters( mlt_filter_service( this ), b_frame
, 0 );
150 mlt_transition_process( composite
, frame
, b_frame
);
153 error
= mlt_frame_get_image( frame
, image
, format
, width
, height
, 1 );
157 mlt_transition_process( composite
, b_frame
, frame
);
158 mlt_properties_set( a_props
, "rescale.interp", "nearest" );
159 mlt_properties_set( b_props
, "rescale.interp", "nearest" );
160 mlt_service_apply_filters( mlt_filter_service( this ), frame
, 0 );
161 error
= mlt_frame_get_image( b_frame
, image
, format
, width
, height
, 1 );
162 mlt_properties_set_data( b_props
, "image", *image
, 0, NULL
, NULL
);
163 mlt_properties_set_data( a_props
, "image", *image
, *width
* *height
* 2, mlt_pool_release
, NULL
);
164 mlt_properties_set_int( a_props
, "width", *width
);
165 mlt_properties_set_int( a_props
, "height", *height
);
170 mlt_frame_close( b_frame
);
174 // Get the image from the frame without running fx
175 error
= mlt_frame_get_image( frame
, image
, format
, width
, height
, 1 );
181 /** Filter processing.
184 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
186 // Get the properties of the frame
187 mlt_properties properties
= mlt_frame_properties( frame
);
189 // Get a unique name to store the frame position
190 char *name
= mlt_properties_get( mlt_filter_properties( this ), "_unique_id" );
192 // Assign the current position to the name
193 mlt_properties_set_position( properties
, name
, mlt_frame_get_position( frame
) - mlt_filter_get_in( this ) );
195 // Push the filter on to the stack
196 mlt_frame_push_service( frame
, this );
198 // Push the get_image on to the stack
199 mlt_frame_push_get_image( frame
, filter_get_image
);
204 /** Constructor for the filter.
207 mlt_filter
filter_watermark_init( void *arg
)
209 mlt_filter
this = mlt_filter_new( );
212 mlt_properties properties
= mlt_filter_properties( this );
213 this->process
= filter_process
;
214 mlt_properties_set( properties
, "factory", "fezzik" );
216 mlt_properties_set( properties
, "resource", arg
);
217 // Ensure that attached filters are handled privately
218 mlt_properties_set_int( properties
, "_filter_private", 1 );