ca8dc6bf2f95e42f31d101ffb85b7b654daa1cd4
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / floatautos.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "automation.inc"
23 #include "clip.h"
24 #include "edl.h"
25 #include "edlsession.h"
26 #include "filexml.h"
27 #include "floatauto.h"
28 #include "floatautos.h"
29 #include "track.h"
30 #include "localsession.h"
31 #include "transportque.inc"
32
33 FloatAutos::FloatAutos(EDL *edl,
34                                 Track *track,
35                                 float default_)
36  : Autos(edl, track)
37 {
38         this->default_ = default_;
39         type = AUTOMATION_TYPE_FLOAT;
40         float_min = -FLT_MAX;
41         float_max = FLT_MAX;
42 }
43
44 FloatAutos::~FloatAutos()
45 {
46 }
47
48 void FloatAutos::set_automation_mode(int64_t start, int64_t end, int mode)
49 {
50         FloatAuto *current = (FloatAuto*)first;
51         while(current)
52         {
53 // Is current auto in range?
54                 if(current->position >= start && current->position < end)
55                 {
56                         current->change_curve_mode((FloatAuto::t_mode)mode);
57                 }
58                 current = (FloatAuto*)NEXT;
59         }
60 }
61
62 void FloatAutos::draw_joining_line(BC_SubWindow *canvas, int vertical, int center_pixel, int x1, int y1, int x2, int y2)
63 {
64         if(vertical)
65                 canvas->draw_line(center_pixel - y1, x1, center_pixel - y2, x2);
66         else
67                 canvas->draw_line(x1, center_pixel + y1, x2, center_pixel + y2);
68 }
69
70 Auto* FloatAutos::new_auto()
71 {
72         FloatAuto *result = new FloatAuto(edl, this);
73         result->set_value(default_);
74         return result;
75 }
76
77 int FloatAutos::get_testy(float slope, int cursor_x, int ax, int ay)
78 {
79         return (int)(slope * (cursor_x - ax)) + ay;
80 }
81
82 int FloatAutos::automation_is_constant(int64_t start,
83         int64_t length,
84         int direction,
85         double &constant)
86 {
87         int total_autos = total();
88         int64_t end;
89         if(direction == PLAY_FORWARD)
90         {
91                 end = start + length;
92         }
93         else
94         {
95                 end = start + 1;
96                 start -= length;
97         }
98
99
100 // No keyframes on track
101         if(total_autos == 0)
102         {
103                 constant = ((FloatAuto*)default_auto)->get_value();
104                 return 1;
105         }
106         else
107 // Only one keyframe on track.
108         if(total_autos == 1)
109         {
110                 constant = ((FloatAuto*)first)->get_value();
111                 return 1;
112         }
113         else
114 // Last keyframe is before region
115         if(last->position <= start)
116         {
117                 constant = ((FloatAuto*)last)->get_value();
118                 return 1;
119         }
120         else
121 // First keyframe is after region
122         if(first->position > end)
123         {
124                 constant = ((FloatAuto*)first)->get_value();
125                 return 1;
126         }
127
128 // Scan sequentially
129         int64_t prev_position = -1;
130         for(Auto *current = first; current; current = NEXT)
131         {
132                 int test_current_next = 0;
133                 int test_previous_current = 0;
134                 FloatAuto *float_current = (FloatAuto*)current;
135
136 // keyframes before and after region but not in region
137                 if(prev_position >= 0 &&
138                         prev_position < start &&
139                         current->position >= end)
140                 {
141 // Get value now in case change doesn't occur
142                         constant = float_current->get_value();
143                         test_previous_current = 1;
144                 }
145                 prev_position = current->position;
146
147 // Keyframe occurs in the region
148                 if(!test_previous_current &&
149                         current->position < end &&
150                         current->position >= start)
151                 {
152
153 // Get value now in case change doesn't occur
154                         constant = float_current->get_value();
155
156 // Keyframe has neighbor
157                         if(current->previous)
158                         {
159                                 test_previous_current = 1;
160                         }
161
162                         if(current->next)
163                         {
164                                 test_current_next = 1;
165                         }
166                 }
167
168                 if(test_current_next)
169                 {
170 //printf("FloatAutos::automation_is_constant 1 %d\n", start);
171                         FloatAuto *float_next = (FloatAuto*)current->next;
172
173 // Change occurs between keyframes
174                         if( !EQUIV(float_current->get_value(), float_next->get_value()) ||
175                                 !EQUIV(float_current->get_control_out_value(), 0) ||
176                                 !EQUIV(float_next->get_control_in_value(), 0))
177                         {
178                                 return 0;
179                         }
180                 }
181
182                 if(test_previous_current)
183                 {
184                         FloatAuto *float_previous = (FloatAuto*)current->previous;
185
186 // Change occurs between keyframes
187                         if(!EQUIV(float_current->get_value(), float_previous->get_value()) ||
188                                 !EQUIV(float_current->get_control_in_value(), 0) ||
189                                 !EQUIV(float_previous->get_control_out_value(), 0))
190                         {
191 // printf("FloatAutos::automation_is_constant %d %d %d %f %f %f %f\n",
192 // start,
193 // float_previous->position,
194 // float_current->position,
195 // float_previous->get_value(),
196 // float_current->get_value(),
197 // float_previous->get_control_out_value(),
198 // float_current->get_control_in_value());
199                                 return 0;
200                         }
201                 }
202         }
203
204 // Got nothing that changes in the region.
205         return 1;
206 }
207
208 double FloatAutos::get_automation_constant(int64_t start, int64_t end)
209 {
210         Auto *current_auto, *before = 0, *after = 0;
211
212 // quickly get autos just outside range
213         get_neighbors(start, end, &before, &after);
214
215 // no auto before range so use first
216         if(before)
217                 current_auto = before;
218         else
219                 current_auto = first;
220
221 // no autos at all so use default value
222         if(!current_auto) current_auto = default_auto;
223
224         return ((FloatAuto*)current_auto)->get_value();
225 }
226
227
228 float FloatAutos::get_value(int64_t position,
229         int direction,
230         FloatAuto* &previous,
231         FloatAuto* &next)
232 {
233 // Calculate bezier equation at position
234         previous = (FloatAuto*)get_prev_auto(position, direction, (Auto* &)previous, 0);
235         next = (FloatAuto*)get_next_auto(position, direction, (Auto* &)next, 0);
236
237 // Constant
238         if(!next && !previous) return ((FloatAuto*)default_auto)->get_value();
239         if(!previous) return next->get_value();
240         if(!next) return previous->get_value();
241         if(next == previous) return previous->get_value();
242
243         if(direction == PLAY_FORWARD)
244         {
245                 if(EQUIV(previous->get_value(), next->get_value())) {
246                         if( (previous->curve_mode == FloatAuto::LINEAR &&
247                              next->curve_mode == FloatAuto::LINEAR) ||
248                             (EQUIV(previous->get_control_out_value(), 0) &&
249                              EQUIV(next->get_control_in_value(), 0))) {
250                                 return previous->get_value();
251                         }
252                 }
253         }
254         else if(direction == PLAY_REVERSE) {
255                 if(EQUIV(previous->get_value(), next->get_value())) {
256                         if( (previous->curve_mode == FloatAuto::LINEAR &&
257                              next->curve_mode == FloatAuto::LINEAR) ||
258                             (EQUIV(previous->get_control_in_value(), 0) &&
259                              EQUIV(next->get_control_out_value(), 0))) {
260                                 return previous->get_value();
261                         }
262                 }
263         }
264 // at this point: previous and next not NULL, positions differ, value not constant.
265
266         return calculate_bezier(previous, next, position);
267 }
268
269
270 float FloatAutos::calculate_bezier(FloatAuto *previous, FloatAuto *next, int64_t position)
271 {
272         if(next->position - previous->position == 0) return previous->get_value();
273
274         float y0 = previous->get_value();
275         float y3 = next->get_value();
276
277 // control points
278         float y1 = previous->get_value() + previous->get_control_out_value();
279         float y2 = next->get_value() + next->get_control_in_value();
280         float t = (float)(position - previous->position) /
281                         (next->position - previous->position);
282
283         float tpow2 = t * t;
284         float tpow3 = t * t * t;
285         float invt = 1 - t;
286         float invtpow2 = invt * invt;
287         float invtpow3 = invt * invt * invt;
288
289         float result = (  invtpow3 * y0
290                 + 3 * t     * invtpow2 * y1
291                 + 3 * tpow2 * invt     * y2
292                 +     tpow3            * y3);
293 //printf("FloatAutos::get_value(t=%5.3f)->%6.2f   (prev,pos,next)=(%d,%d,%d)\n", t, result, previous->position, position, next->position);
294
295         return result;
296 }
297
298
299 float FloatAutos::calculate_bezier_derivation(FloatAuto *previous, FloatAuto *next, int64_t position)
300 // calculate the slope of the interpolating bezier function at given position.
301 // computed slope is based on the actual position scale (in frames or samples)
302 {
303         float scale = next->position - previous->position;
304         if( scale == 0 ) {
305                 if( !previous->get_control_out_position() )
306                         return 0;
307                 return previous->get_control_out_value() / previous->get_control_out_position();
308         }
309         float y0 = previous->get_value();
310         float y3 = next->get_value();
311
312 // control points
313         float y1 = previous->get_value() + previous->get_control_out_value();
314         float y2 = next->get_value() + next->get_control_in_value();
315 // normalized scale
316         float t = (float)(position - previous->position) / scale;
317
318         float tpow2 = t * t;
319         float invt = 1 - t;
320         float invtpow2 = invt * invt;
321
322         float slope = 3 * (
323                 - invtpow2              * y0
324                 - invt * ( 2*t - invt ) * y1
325                 + t    * ( 2*invt - t ) * y2
326                 + tpow2                 * y3
327                 );
328
329         return slope / scale;
330 }
331
332
333
334 void FloatAutos::get_extents(float *min,
335         float *max,
336         int *coords_undefined,
337         int64_t unit_start,
338         int64_t unit_end)
339 {
340         if(!edl)
341         {
342                 printf("FloatAutos::get_extents edl == NULL\n");
343                 return;
344         }
345
346         if(!track)
347         {
348                 printf("FloatAutos::get_extents track == NULL\n");
349                 return;
350         }
351
352 // Use default auto
353         if(!first)
354         {
355                 FloatAuto *current = (FloatAuto*)default_auto;
356                 if(*coords_undefined)
357                 {
358                         *min = *max = current->get_value();
359                         *coords_undefined = 0;
360                 }
361
362                 *min = MIN(current->get_value(), *min);
363                 *max = MAX(current->get_value(), *max);
364         }
365
366 // Test all handles
367         for(FloatAuto *current = (FloatAuto*)first; current; current = (FloatAuto*)NEXT)
368         {
369                 if(current->position >= unit_start && current->position < unit_end)
370                 {
371                         if(*coords_undefined)
372                         {
373                                 *min = *max = current->get_value();
374                                 *coords_undefined = 0;
375                         }
376
377                         *min = MIN(current->get_value(), *min);
378                         *min = MIN(current->get_value() + current->get_control_in_value(), *min);
379                         *min = MIN(current->get_value() + current->get_control_out_value(), *min);
380
381                         *max = MAX(current->get_value(), *max);
382                         *max = MAX(current->get_value() + current->get_control_in_value(), *max);
383                         *max = MAX(current->get_value() + current->get_control_out_value(), *max);
384                 }
385         }
386
387 // Test joining regions
388         FloatAuto *prev = 0;
389         FloatAuto *next = 0;
390         int64_t unit_step = edl->local_session->zoom_sample;
391         if(track->data_type == TRACK_VIDEO)
392                 unit_step = (int64_t)(unit_step *
393                         edl->session->frame_rate /
394                         edl->session->sample_rate);
395         unit_step = MAX(unit_step, 1);
396         for(int64_t position = unit_start;
397                 position < unit_end;
398                 position += unit_step)
399         {
400                 float value = get_value(position,PLAY_FORWARD,prev,next);
401                 if(*coords_undefined)
402                 {
403                         *min = *max = value;
404                         *coords_undefined = 0;
405                 }
406                 else
407                 {
408                         *min = MIN(value, *min);
409                         *max = MAX(value, *max);
410                 }
411         }
412 }
413
414 void FloatAutos::set_proxy(int orig_scale, int new_scale)
415 {
416         float orig_value;
417         orig_value = ((FloatAuto*)default_auto)->value * orig_scale;
418         ((FloatAuto*)default_auto)->value = orig_value / new_scale;
419
420         for( FloatAuto *current= (FloatAuto*)first; current; current=(FloatAuto*)NEXT ) {
421                 orig_value = current->value * orig_scale;
422                 current->value = orig_value / new_scale;
423                 orig_value = current->control_in_value * orig_scale;
424                 current->control_in_value = orig_value / new_scale;
425                 orig_value = current->control_out_value * orig_scale;
426                 current->control_out_value = orig_value / new_scale;
427         }
428 }
429
430 void FloatAutos::dump()
431 {
432         printf("        FloatAutos::dump %p\n", this);
433         printf("        Default: position %jd value=%f\n",
434                 default_auto->position, ((FloatAuto*)default_auto)->get_value());
435         for(Auto* current = first; current; current = NEXT)
436         {
437                 printf("        position %jd value=%7.3f invalue=%7.3f outvalue=%7.3f %s\n",
438                         current->position,
439                         ((FloatAuto*)current)->get_value(),
440                         ((FloatAuto*)current)->get_control_in_value(),
441                         ((FloatAuto*)current)->get_control_out_value(),
442                         FloatAuto::curve_name(((FloatAuto*)current)->curve_mode));
443         }
444 }
445
446 double FloatAutos::automation_integral(int64_t start, int64_t length, int direction)
447 {
448         if( direction == PLAY_REVERSE )
449                 start -= length;
450         if( start < 0 ) {
451                 length += start;
452                 start = 0;
453         }
454         double value = 0;
455         int64_t pos = start, len = length, end = pos + len;
456         while( pos < end ) {
457                 int64_t prev_pos = 0, next_pos = end;
458                 Auto *zprev = 0, *znext = 0;
459                 FloatAuto *prev = (FloatAuto*)get_prev_auto(pos, direction, zprev, 0);
460                 if( prev ) prev_pos = prev->position;
461                 FloatAuto *next = (FloatAuto*)get_next_auto(pos, direction, znext, 0);
462                 if( next ) next_pos = next->position;
463                 if( !prev && !next ) prev = next = (FloatAuto*)default_auto;
464                 else if( !prev ) prev = next;
465                 else if( !next ) next = prev;
466
467                 double dt = next_pos - prev_pos;
468                 double t0 = (pos - prev_pos) / dt;
469                 if( (pos = next_pos) > end ) pos = end;
470                 double t1 = (pos - prev_pos) / dt;
471
472                 double y0 = prev->get_value(), y1 = y0 + prev->get_control_out_value();
473                 double y3 = next->get_value(), y2 = y3 + next->get_control_in_value();
474                 if( y0 != y1 || y1 != y2 || y2 != y3 ) {
475 // bezier definite integral t0..t1
476                         double f4 = -y0/4 + 3*y1/4 - 3*y2/4 + y3/4;
477                         double f3 = y0 - 2*y1 + y2;
478                         double f2 = -3*y0/2 + 3*y1/2;
479                         double f1 = y0, t = t0;
480                         double t2 = t*t, t3 = t2*t, t4 = t3*t;
481                         t0 = t4*f4 + t3*f3 + t2*f2 + t*f1;
482                         t = t1;  t2 = t*t;  t3 = t2*t;  t4 = t3*t;
483                         t1 = t4*f4 + t3*f3 + t2*f2 + t*f1;
484                 }
485                 else {
486                         t0 *= y0;  t1 *= y0;
487                 }
488                 value += dt * (t1 - t0);
489         }
490
491         return value + 1e-6;
492 }
493
494 int64_t FloatAutos::speed_position(double pos)
495 {
496         double length = track->get_length();
497         int64_t l = -1, r = track->to_units(length, 1);
498         if( r < 1 ) r = 1;
499         for( int i=32; --i >= 0 && automation_integral(0,r,PLAY_FORWARD) <= pos; r*=2 );
500         for( int i=64; --i >= 0 && (r-l)>1; ) {
501                 int64_t m = (l + r) / 2;
502                 double t = automation_integral(0,m,PLAY_FORWARD) - pos;
503                 *(t >= 0 ? &r : &l) = m;
504         }
505         return r;
506 }
507