More mutable properties
[melted] / 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 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.
10 *
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.
15 *
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.
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
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 // Create a composite if we don't have one
52 if ( composite == NULL )
53 {
54 // Create composite via the factory
55 composite = mlt_factory_transition( "composite", NULL );
56
57 // Register the composite for reuse/destruction
58 if ( composite != NULL )
59 mlt_properties_set_data( properties, "composite", composite, 0, ( mlt_destructor )mlt_transition_close, NULL );
60 }
61
62 // If we have one
63 if ( composite != NULL )
64 {
65 // Get the properties
66 mlt_properties composite_properties = mlt_transition_properties( composite );
67
68 // Pass all the composite. properties on the filter down
69 mlt_properties_pass( composite_properties, properties, "composite." );
70 }
71
72 // Create a producer if don't have one
73 if ( producer == NULL )
74 {
75 // Get the resource to use
76 char *resource = mlt_properties_get( properties, "resource" );
77
78 // Get the factory producer service
79 char *factory = mlt_properties_get( properties, "factory" );
80
81 // Create the producer
82 producer = mlt_factory_producer( factory, resource );
83
84 // If we have one
85 if ( producer != NULL )
86 {
87 // Register the producer for reuse/destruction
88 mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
89
90 // Ensure that we loop
91 mlt_properties_set( mlt_producer_properties( producer ), "eof", "loop" );
92 }
93 }
94
95 if ( producer != NULL )
96 {
97 // Get the producer properties
98 mlt_properties producer_properties = mlt_producer_properties( producer );
99
100 // Now pass all producer. properties on the filter down
101 mlt_properties_pass( producer_properties, properties, "producer." );
102 }
103
104 // Only continue if we have both producer and composite
105 if ( composite != NULL && producer != NULL )
106 {
107 // Get the service of the producer
108 mlt_service service = mlt_producer_service( producer );
109
110 // We will get the 'b frame' from the producer
111 mlt_frame b_frame = NULL;
112
113 // Get the unique id of the filter (used to reacquire the producer position)
114 char *name = mlt_properties_get( properties, "_unique_id" );
115
116 // Get the original producer position
117 mlt_position position = mlt_properties_get_position( mlt_frame_properties( frame ), name );
118
119 // Make sure the producer is in the correct position
120 mlt_producer_seek( producer, position );
121
122 // Resetting position to appease the composite transition
123 mlt_frame_set_position( frame, position );
124
125 // Get the b frame and process with composite if successful
126 if ( mlt_service_get_frame( service, &b_frame, 0 ) == 0 )
127 mlt_transition_process( composite, frame, b_frame );
128
129 // Get the image
130 error = mlt_frame_get_image( frame, image, format, width, height, 1 );
131
132 // Close the b frame
133 mlt_frame_close( b_frame );
134 }
135 else
136 {
137 // Get the image from the frame without running fx
138 error = mlt_frame_get_image( frame, image, format, width, height, 1 );
139 }
140
141 return error;
142 }
143
144 /** Filter processing.
145 */
146
147 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
148 {
149 // Get the properties of the frame
150 mlt_properties properties = mlt_frame_properties( frame );
151
152 // Get a unique name to store the frame position
153 char *name = mlt_properties_get( mlt_filter_properties( this ), "_unique_id" );
154
155 // Assign the current position to the name
156 mlt_properties_set_position( properties, name, mlt_frame_get_position( frame ) );
157
158 // Push the filter on to the stack
159 mlt_frame_push_service( frame, this );
160
161 // Push the get_image on to the stack
162 mlt_frame_push_get_image( frame, filter_get_image );
163
164 return frame;
165 }
166
167 /** Constructor for the filter.
168 */
169
170 mlt_filter filter_watermark_init( void *arg )
171 {
172 mlt_filter this = mlt_filter_new( );
173 if ( this != NULL )
174 {
175 mlt_properties properties = mlt_filter_properties( this );
176 this->process = filter_process;
177 mlt_properties_set( properties, "factory", "fezzik" );
178 if ( arg != NULL )
179 mlt_properties_set( properties, "resource", arg );
180 }
181 return this;
182 }
183