]> git.sesse.net Git - mlt/blob - src/modules/plus/filter_affine.c
Add lift_gamma_gain filter.
[mlt] / src / modules / plus / filter_affine.c
1 /*
2  * filter_affine.c -- affine 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.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
27
28 /** Do it :-).
29 */
30
31 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
32 {
33         // Get the filter
34         mlt_filter filter = mlt_frame_pop_service( this );
35
36         // Get the properties
37         mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
38
39         // Get the image
40         int error = 0;
41         *format = mlt_image_rgb24a;
42         //mlt_frame_get_image( this, image, format, width, height, 0 );
43
44         // Only process if we have no error and a valid colour space
45         if ( error == 0 )
46         {
47                 mlt_service_lock( MLT_FILTER_SERVICE( filter ) );
48                 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
49                 mlt_transition transition = mlt_properties_get_data( properties, "transition", NULL );
50                 mlt_frame a_frame = NULL;
51                 mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( filter ) );
52
53                 if ( producer == NULL )
54                 {
55                         char *background = mlt_properties_get( properties, "background" );
56                         producer = mlt_factory_producer( profile, NULL, background );
57                         mlt_properties_set_data( properties, "producer", producer, 0, (mlt_destructor)mlt_producer_close, NULL );
58                 }
59
60                 if ( transition == NULL )
61                 {
62                         transition = mlt_factory_transition( profile, "affine", NULL );
63                         mlt_properties_set_data( properties, "transition", transition, 0, (mlt_destructor)mlt_transition_close, NULL );
64                         if ( transition )
65                                 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "b_alpha", 1 );
66                 }
67
68                 if ( producer != NULL && transition != NULL )
69                 {
70                         mlt_position position = mlt_filter_get_position( filter, this );
71                         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( this );
72                         mlt_position in = mlt_filter_get_in( filter );
73                         mlt_position out = mlt_filter_get_out( filter );
74                         double consumer_ar = mlt_profile_sar( profile );
75                         mlt_transition_set_in_and_out( transition, in, out );
76                         if ( out > 0 ) {
77                                 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( producer ), "length", out - in + 1 );
78                                 mlt_producer_set_in_and_out( producer, in, out );
79                         }
80                         mlt_producer_seek( producer, in + position );
81                         mlt_frame_set_position( this, position );
82                         mlt_properties_pass( MLT_PRODUCER_PROPERTIES( producer ), properties, "producer." );
83                         mlt_properties_pass( MLT_TRANSITION_PROPERTIES( transition ), properties, "transition." );
84                         mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &a_frame, 0 );
85                         mlt_frame_set_position( a_frame, in + position );
86 //                      mlt_properties_set_int( MLT_FRAME_PROPERTIES( a_frame ), "distort", 1 );
87
88                         // Special case - aspect_ratio = 0
89                         if ( mlt_frame_get_aspect_ratio( this ) == 0 )
90                                 mlt_frame_set_aspect_ratio( this, consumer_ar );
91                         if ( mlt_frame_get_aspect_ratio( a_frame ) == 0 )
92                                 mlt_frame_set_aspect_ratio( a_frame, consumer_ar );
93
94                         // Add the affine transition onto the frame stack
95                         mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
96                         mlt_transition_process( transition, a_frame, this );
97
98                         if ( mlt_properties_get_int( properties, "use_normalised" ) )
99                         {
100                                 // Use the normalised width & height
101                                 mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( filter ) );
102                                 *width = profile->width;
103                                 *height = profile->height;
104                         }
105                         
106                         mlt_frame_get_image( a_frame, image, format, width, height, writable );
107                         mlt_properties_set_data( frame_properties, "affine_frame", a_frame, 0, (mlt_destructor)mlt_frame_close, NULL );
108                         mlt_frame_set_image( this, *image, *width * *height * 4, NULL );
109                         mlt_frame_set_alpha( this, mlt_frame_get_alpha_mask( a_frame ), *width * *height, NULL );
110                 }
111                 else
112                 {
113                         mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
114                 }
115         }
116
117         return error;
118 }
119
120 /** Filter processing.
121 */
122
123 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
124 {
125         // Push the frame filter
126         mlt_frame_push_service( frame, this );
127         mlt_frame_push_get_image( frame, filter_get_image );
128
129         return frame;
130 }
131
132 /** Constructor for the filter.
133 */
134
135 mlt_filter filter_affine_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
136 {
137         mlt_filter this = mlt_filter_new( );
138         if ( this != NULL )
139         {
140                 this->process = filter_process;
141                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "background", arg ? arg : "colour:0" );
142         }
143         return this;
144 }
145
146