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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_profile.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <math.h>
28 #include <string.h>
29
30 // this is a utility function used by DoWave below
getPoint(uint8_t * src,int w,int h,int x,int y,int z)31 static uint8_t getPoint(uint8_t *src, int w, int h, int x, int y, int z)
32 {
33 if (x<0) x+=-((-x)%w)+w; else if (x>=w) x=x%w;
34 if (y<0) y+=-((-y)%h)+h; else if (y>=h) y=y%h;
35 return src[CLAMP(x+y*w, 0, w*h-1) * 4 + z];
36 }
37
38 // the main meat of the algorithm lies here
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 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)
40 {
41 register int x, y;
42 int decalY, decalX, z;
43 float amplitude, phase, pulsation;
44 register int uneven = src_w % 2;
45 int w = (src_w - uneven ) / 2;
46 amplitude = factor;
47 pulsation = 0.5 / factor; // smaller means bigger period
48 phase = position * pulsation * speed / 10; // smaller means longer
49 for (y=0;y<src_h;y++) {
50 decalX = deformX ? sin(pulsation * y + phase) * amplitude : 0;
51 for (x=0;x<w;x++) {
52 decalY = deformY ? sin(pulsation * x * 2 + phase) * amplitude : 0;
53 for (z=0; z<4; z++)
54 *dst++ = getPoint(src, w, src_h, (x+decalX), (y+decalY), z);
55 }
56 if (uneven) {
57 decalY = sin(pulsation * x * 2 + phase) * amplitude;
58 for (z=0; z<2; z++)
59 *dst++ = getPoint(src, w, src_h, (x+decalX), (y+decalY), z);
60 }
61 }
62 }
63
filter_get_image(mlt_frame frame,uint8_t ** image,mlt_image_format * format,int * width,int * height,int writable)64 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
65 {
66 mlt_filter filter = (mlt_filter) mlt_frame_pop_service( frame );
67 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
68 mlt_position position = mlt_frame_get_position( frame );
69
70 // Get the image
71 *format = mlt_image_yuv422;
72 int error = mlt_frame_get_image( frame, image, format, width, height, 0 );
73
74 // Only process if we have no error and a valid colour space
75 if ( error == 0 )
76 {
77 double factor = mlt_properties_get_double( properties, "start" );
78
79 mlt_position f_pos = mlt_filter_get_position( filter, frame );
80 mlt_position f_len = mlt_filter_get_length2( filter, frame );
81 int speed = mlt_properties_anim_get_int( properties, "speed", f_pos, f_len );
82 int deformX = mlt_properties_anim_get_int( properties, "deformX", f_pos, f_len );
83 int deformY = mlt_properties_anim_get_int( properties, "deformY", f_pos, f_len );
84
85 if ( mlt_properties_get( properties, "end" ) )
86 {
87 // Determine the time position of this frame in the transition duration
88 double end = fabs( mlt_properties_get_double( MLT_FILTER_PROPERTIES( filter ), "end" ) );
89 factor += ( end - factor ) * mlt_filter_get_progress( filter, frame );
90 }
91
92 // If animated property "wave" is set, use its value.
93 char* wave_property = mlt_properties_get( properties, "wave" );
94 if ( wave_property )
95 {
96 factor = mlt_properties_anim_get_double( properties, "wave", f_pos, f_len );
97 }
98
99 mlt_profile profile = mlt_service_profile(MLT_FILTER_SERVICE(filter));
100 factor *= mlt_profile_scale_width(profile, *width);
101
102 if (factor > 0.0)
103 {
104 int image_size = *width * (*height) * 2;
105 uint8_t *dst = mlt_pool_alloc (image_size);
106 DoWave(*image, *width, (*height), dst, position, speed, factor, deformX, deformY);
107 *image = dst;
108 mlt_frame_set_image( frame, *image, image_size, mlt_pool_release );
109 }
110 }
111
112 return error;
113 }
114
115 /** Filter processing.
116 */
117
filter_process(mlt_filter filter,mlt_frame frame)118 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
119 {
120 mlt_frame_push_service( frame, filter );
121 mlt_frame_push_get_image( frame, filter_get_image );
122
123 return frame;
124 }
125
126 /** Constructor for the filter.
127 */
128
filter_wave_init(mlt_profile profile,mlt_service_type type,const char * id,char * arg)129 mlt_filter filter_wave_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
130 {
131 mlt_filter filter = mlt_filter_new( );
132 if ( filter )
133 {
134 filter->process = filter_process;
135 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "start", arg == NULL ? "10" : arg);
136 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "speed", "5");
137 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "deformX", "1");
138 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "deformY", "1");
139 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "wave", NULL);
140 }
141 return filter;
142 }
143
144