]> git.sesse.net Git - mlt/blob - src/modules/plus/filter_affine.c
Massive refactoring of image conversion.
[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_yuv422;
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_producer producer = mlt_properties_get_data( properties, "producer", NULL );
48                 mlt_transition transition = mlt_properties_get_data( properties, "transition", NULL );
49                 mlt_frame a_frame = NULL;
50
51                 if ( producer == NULL )
52                 {
53                         char *background = mlt_properties_get( properties, "background" );
54                         mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( filter ) );
55                         producer = mlt_factory_producer( profile, NULL, background );
56                         mlt_properties_set_data( properties, "producer", producer, 0, (mlt_destructor)mlt_producer_close, NULL );
57                 }
58
59                 if ( transition == NULL )
60                 {
61                         mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( filter ) );
62                         transition = mlt_factory_transition( profile, "affine", NULL );
63                         mlt_properties_set_data( properties, "transition", transition, 0, (mlt_destructor)mlt_transition_close, NULL );
64                 }
65
66                 if ( producer != NULL && transition != NULL )
67                 {
68                         char *name = mlt_properties_get( properties, "_unique_id" );
69                         mlt_position position = mlt_properties_get_position( MLT_FRAME_PROPERTIES( this ), name );
70                         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( this );
71                         double consumer_ar = mlt_properties_get_double( frame_properties, "consumer_aspect_ratio" );
72                         mlt_properties_set_position( MLT_TRANSITION_PROPERTIES( transition ), "in", mlt_filter_get_in( filter ) );
73                         mlt_properties_set_position( MLT_TRANSITION_PROPERTIES( transition ), "out", mlt_filter_get_out( filter ) );
74                         mlt_producer_seek( producer, position );
75                         mlt_frame_set_position( this, position );
76                         mlt_properties_pass( MLT_PRODUCER_PROPERTIES( producer ), properties, "producer." );
77                         mlt_properties_pass( MLT_TRANSITION_PROPERTIES( transition ), properties, "transition." );
78                         mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &a_frame, 0 );
79                         mlt_properties_set( MLT_FRAME_PROPERTIES( a_frame ), "rescale.interp", "nearest" );
80                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( a_frame ), "distort", 1 );
81
82                         // Special case - aspect_ratio = 0
83                         if ( mlt_properties_get_double( frame_properties, "aspect_ratio" ) == 0 )
84                                 mlt_properties_set_double( frame_properties, "aspect_ratio", consumer_ar );
85                         if ( mlt_properties_get_double( MLT_FRAME_PROPERTIES( a_frame ), "aspect_ratio" ) == 0 )
86                                 mlt_properties_set_double( MLT_FRAME_PROPERTIES( a_frame ), "aspect_ratio", consumer_ar );
87                         mlt_properties_set_double( MLT_FRAME_PROPERTIES( a_frame ), "consumer_aspect_ratio", consumer_ar );
88
89                         mlt_transition_process( transition, a_frame, this );
90                         mlt_frame_get_image( a_frame, image, format, width, height, writable );
91                         mlt_properties_set_data( frame_properties, "affine_frame", a_frame, 0, (mlt_destructor)mlt_frame_close, NULL );
92                         mlt_properties_set_data( frame_properties, "image", *image, *width * *height * 2, NULL, NULL );
93                         mlt_properties_set_data( frame_properties, "alpha", mlt_frame_get_alpha_mask( a_frame ), *width * *height, NULL, NULL );
94                 }
95         }
96
97         return error;
98 }
99
100 /** Filter processing.
101 */
102
103 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
104 {
105         // Get the properties of the frame
106         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
107
108         // Get a unique name to store the frame position
109         char *name = mlt_properties_get( MLT_FILTER_PROPERTIES( this ), "_unique_id" );
110
111         // Assign the current position to the name
112         mlt_properties_set_position( properties, name, mlt_frame_get_position( frame ) - mlt_filter_get_in( this ) );
113
114         // Push the frame filter
115         mlt_frame_push_service( frame, this );
116         mlt_frame_push_get_image( frame, filter_get_image );
117
118         return frame;
119 }
120
121 /** Constructor for the filter.
122 */
123
124 mlt_filter filter_affine_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
125 {
126         mlt_filter this = mlt_filter_new( );
127         if ( this != NULL )
128         {
129                 this->process = filter_process;
130                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "background", "colour:black" );
131         }
132         return this;
133 }
134
135