bsd lang segv fix, enable bsd lv2, lv2 gui enable fix, proxy/ffmpeg toggle resize...
[goodguy/history.git] / cinelerra-5.1 / cinelerra / keyframes.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 "bchash.h"
23 #include "clip.h"
24 #include "edl.h"
25 #include "edlsession.h"
26 #include "filexml.h"
27 #include "keyframe.h"
28 #include "keyframes.h"
29 #include "localsession.h"
30 #include "track.h"
31 #include "transportque.inc"
32
33 KeyFrames::KeyFrames(EDL *edl, Track *track)
34  : Autos(edl, track)
35 {
36         type = Autos::AUTOMATION_TYPE_PLUGIN;
37 }
38
39 KeyFrames::~KeyFrames()
40 {
41 }
42
43
44 KeyFrame* KeyFrames::get_prev_keyframe(int64_t position,
45         int direction)
46 {
47         KeyFrame *current = 0;
48
49 // This doesn't work because edl->selectionstart doesn't change during
50 // playback at the same rate as PluginClient::source_position.
51         if(position < 0)
52         {
53                 position = track->to_units(edl->local_session->get_selectionstart(1), 0);
54         }
55
56 // Get keyframe on or before current position
57         for(current = (KeyFrame*)last;
58                 current;
59                 current = (KeyFrame*)PREVIOUS)
60         {
61                 if(direction == PLAY_FORWARD && current->position <= position) break;
62                 else
63                 if(direction == PLAY_REVERSE && current->position < position) break;
64         }
65
66 // Nothing before current position
67         if(!current && first)
68         {
69                 current = (KeyFrame*)first;
70         }
71         else
72 // No keyframes
73         if(!current)
74         {
75                 current = (KeyFrame*)default_auto;
76         }
77
78         return current;
79 }
80
81 KeyFrame* KeyFrames::get_keyframe()
82 {
83 // Search for keyframe on or before selection
84         KeyFrame *result =
85                 get_prev_keyframe(
86                         track->to_units(edl->local_session->get_selectionstart(1), 0),
87                         PLAY_FORWARD);
88
89 // Return nearest keyframe if not in automatic keyframe generation
90         if(!edl->session->auto_keyframes)
91         {
92                 return result;
93         }
94         else
95 // Return new keyframe
96         if(result == (KeyFrame*)default_auto ||
97                 result->position != track->to_units(edl->local_session->get_selectionstart(1), 0))
98         {
99                 return (KeyFrame*)insert_auto(track->to_units(edl->local_session->get_selectionstart(1), 0));
100         }
101         else
102 // Return existing keyframe
103         {
104                 return result;
105         }
106
107         return 0;
108 }
109
110
111 void KeyFrames::update_parameter(KeyFrame *src)
112 {
113         const int debug = 0;
114 // Create new keyframe if auto keyframes or replace entire keyframe.
115         double selection_start = edl->local_session->get_selectionstart(0);
116         double selection_end = edl->local_session->get_selectionend(0);
117         selection_start = edl->align_to_frame(selection_start, 0);
118         selection_end = edl->align_to_frame(selection_end, 0);
119
120         if(EQUIV(selection_start, selection_end))
121         {
122 // Search for keyframe to write to
123                 KeyFrame *dst = get_keyframe();
124
125                 dst->copy_data(src);
126         }
127         else
128 // Replace changed parameter in all selected keyframes.
129         {
130                 BC_Hash *params = 0;
131                 char *text = 0;
132                 char *extra = 0;
133
134
135 // Search all keyframes in selection but don't create a new one.
136                 int64_t start = track->to_units(selection_start, 0);
137                 int64_t end = track->to_units(selection_end, 0);
138                 KeyFrame *current = get_prev_keyframe(
139                                 start,
140                                 PLAY_FORWARD);
141
142 // The first one determines the changed parameters since it is the one displayed
143 // in the GUI.
144                 current->get_diff(src,
145                         &params,
146                         &text,
147                         &extra);
148
149
150 if(debug) printf("KeyFrames::update_parameter %d params=%p position=%jd start=%jd\n",
151   __LINE__,  params, current->position, track->to_units(start, 0));
152
153 if(debug && params)
154 {
155 for(int i = 0; i < params->size(); i++)
156 printf("KeyFrames::update_parameter %d changed=%s %s\n",
157 __LINE__,
158 params->get_key(i),
159 params->get_value(i));
160 }
161
162 // Always update the first one
163                 current->update_parameter(params,
164                         text,
165                         extra);
166                 for(current = (KeyFrame*)NEXT ; current && current->position < end; current = (KeyFrame*)NEXT)
167                 {
168 if(debug) printf("KeyFrames::update_parameter %d position=%jd\n",
169   __LINE__, current->position);
170                         current->update_parameter(params,
171                                 text,
172                                 extra);
173                 }
174
175                 delete params;
176                 delete [] text,
177                 delete [] extra;
178         }
179 }
180
181 Auto* KeyFrames::new_auto()
182 {
183         return new KeyFrame(edl, this);
184 }
185
186
187 void KeyFrames::dump(FILE *fp)
188 {
189         fprintf(fp,"    DEFAULT_KEYFRAME\n");
190         ((KeyFrame*)default_auto)->dump(fp);
191         fprintf(fp,"    KEYFRAMES total=%d\n", total());
192         for(KeyFrame *current = (KeyFrame*)first;
193                 current;
194                 current = (KeyFrame*)NEXT)
195         {
196                 current->dump(fp);
197         }
198 }
199