add libdav1d codec, add remap_a/v_codec option keywords
[goodguy/cinelerra.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         int64_t pos = track->to_units(edl->local_session->get_selectionstart(1), 0);
84 // Search for keyframe on or before selection
85         KeyFrame *result = get_prev_keyframe(pos, PLAY_FORWARD);
86         if( edl->session->auto_keyframes ) {
87                 if( !result || result->position != pos ||
88                     result == (KeyFrame*)default_auto )
89 // generate keyframes while tweeking, and no keyframe found at pos
90                         result = (KeyFrame*)insert_auto(pos);
91         }
92         return result;
93 }
94
95
96 void KeyFrames::update_parameter(KeyFrame *src)
97 {
98 // Create new keyframe if auto keyframes or replace entire keyframe.
99         double selection_start = edl->local_session->get_selectionstart(0);
100         double selection_end = edl->local_session->get_selectionend(0);
101         selection_start = edl->align_to_frame(selection_start, 0);
102         selection_end = edl->align_to_frame(selection_end, 0);
103
104         if( !edl->session->span_keyframes ||
105             EQUIV(selection_start, selection_end) ) {
106 // Search for keyframe to write
107                 KeyFrame *dst = get_keyframe();
108                 dst->copy_data(src);
109         }
110         else {
111 // Replace changed parameter in all selected keyframes.
112                 BC_Hash *params = 0;
113                 char *text = 0, *extra = 0;
114 // Search all keyframes in selection but don't create a new one.
115                 int64_t start = track->to_units(selection_start, 0);
116                 int64_t end = track->to_units(selection_end, 0);
117                 KeyFrame *current = get_prev_keyframe(start, PLAY_FORWARD);
118 // The first one determines the changed parameters since it is the one displayed
119                 current->get_diff(src, &params, &text, &extra);
120 // Always update the first one
121                 current->update_parameter(params, text, extra);
122
123                 for( current = (KeyFrame*)NEXT; current;  current = (KeyFrame*)NEXT ) {
124                         if( current->position >= end ) break;
125                         current->update_parameter(params, text, extra);
126                 }
127                 delete params;
128                 delete [] text,
129                 delete [] extra;
130         }
131 }
132
133 Auto* KeyFrames::new_auto()
134 {
135         return new KeyFrame(edl, this);
136 }
137
138
139 void KeyFrames::dump(FILE *fp)
140 {
141         fprintf(fp,"    DEFAULT_KEYFRAME\n");
142         ((KeyFrame*)default_auto)->dump(fp);
143         fprintf(fp,"    KEYFRAMES total=%d\n", total());
144         for(KeyFrame *current = (KeyFrame*)first;
145                 current;
146                 current = (KeyFrame*)NEXT)
147         {
148                 current->dump(fp);
149         }
150 }
151