]> git.sesse.net Git - mlt/blob - src/modules/kdenlive/filter_wave.c
Refactor to mlt_filter_get_progress().
[mlt] / src / modules / kdenlive / filter_wave.c
1 /*
2  * wave.c -- wave filter
3  * Copyright (C) ?-2007 Leny Grisel <leny.grisel@laposte.net>
4  * Copyright (C) 2007 Jean-Baptiste Mardelle <jb@ader.ch>
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_frame.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
27 #include <string.h>
28
29 // this is a utility function used by DoWave below
30 static uint8_t getPoint(uint8_t *src, int w, int h, int x, int y, int z)
31 {
32         if (x<0) x+=-((-x)%w)+w; else if (x>=w) x=x%w;
33         if (y<0) y+=-((-y)%h)+h; else if (y>=h) y=y%h;
34         return src[(x+y*w)*4+z];
35 }
36
37 // the main meat of the algorithm lies here
38 static void DoWave(uint8_t *src, int src_w, int src_h, uint8_t *dst, mlt_position position, int speed, int factor, int deformX, int deformY)
39 {
40         register int x, y;
41         int decalY, decalX, z;
42         float amplitude, phase, pulsation;
43         register int uneven = src_w % 2;
44         int w = (src_w - uneven ) / 2;
45         amplitude = factor;
46         pulsation = 0.5 / factor;   // smaller means bigger period
47         phase = position * pulsation * speed / 10; // smaller means longer
48         for (y=0;y<src_h;y++) {
49                 decalX = deformX ? sin(pulsation * y + phase) * amplitude : 0;
50                 for (x=0;x<w;x++) {
51                         decalY = deformY ? sin(pulsation * x * 2 + phase) * amplitude : 0;
52                         for (z=0; z<4; z++)
53                                 *dst++ = getPoint(src, w, src_h, (x+decalX), (y+decalY), z);
54                 }
55                 if (uneven) {
56                         decalY = sin(pulsation * x * 2 + phase) * amplitude;
57                         for (z=0; z<2; z++)
58                                 *dst++ = getPoint(src, w, src_h, (x+decalX), (y+decalY), z);
59                 }
60         }
61 }
62
63 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
64 {
65         mlt_properties unique = mlt_frame_pop_service( frame );
66         mlt_position position = mlt_frame_get_position( frame );
67
68         // Get the image
69         *format = mlt_image_yuv422;
70         int error = mlt_frame_get_image( frame, image, format, width, height, 0 );
71
72         // Only process if we have no error and a valid colour space
73         if ( error == 0 )
74         {
75                 double factor = mlt_properties_get_int( unique, "wave" );
76                 int speed = mlt_properties_get_int( unique, "speed" );
77                 int deformX = mlt_properties_get_int( unique, "deformX" );
78                 int deformY = mlt_properties_get_int( unique, "deformY" );
79                 if (factor != 0) {
80                         int image_size = *width * (*height) * 2;
81                         uint8_t *dst = mlt_pool_alloc (image_size);
82                         DoWave(*image, *width, (*height), dst, position, speed, factor, deformX, deformY);
83                         *image = dst;
84                         mlt_frame_set_image( frame, *image, image_size, mlt_pool_release );
85                 }
86         }
87
88         return error;
89 }
90
91 /** Filter processing.
92 */
93
94 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
95 {
96         // Get the starting wave level
97         double wave = mlt_properties_get_double( MLT_FILTER_PROPERTIES( filter ), "start" );
98         int speed = mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "speed" );
99         int deformX = mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "deformX" );
100         int deformY = mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "deformY" );
101
102         // If there is an end adjust gain to the range
103         if ( mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "end" ) != NULL )
104         {
105                 // Determine the time position of this frame in the transition duration
106                 double end = fabs( mlt_properties_get_double( MLT_FILTER_PROPERTIES( filter ), "end" ) );
107                 wave += ( end - wave ) * mlt_filter_get_progress( filter, frame );
108         }
109
110         // Push the frame filter
111         mlt_properties unique = mlt_frame_unique_properties( frame, MLT_FILTER_SERVICE( filter ) );
112         mlt_properties_set_double( unique, "wave", wave );
113         mlt_properties_set_int( unique, "speed", speed );
114         mlt_properties_set_int( unique, "deformX", deformX );
115         mlt_properties_set_int( unique, "deformY", deformY );
116         mlt_frame_push_service( frame, unique );
117         mlt_frame_push_get_image( frame, filter_get_image );
118
119         return frame;
120 }
121
122 /** Constructor for the filter.
123 */
124
125 mlt_filter filter_wave_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
126 {
127         mlt_filter filter = mlt_filter_new( );
128         if ( filter )
129         {
130                 filter->process = filter_process;
131                 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "start", arg == NULL ? "10" : arg);
132                 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "speed", arg == NULL ? "5" : arg);
133                 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "deformX", arg == NULL ? "1" : arg);
134                 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "deformY", arg == NULL ? "1" : arg);
135         }
136         return filter;
137 }
138
139
140