merged hv7 mod
[goodguy/history.git] / cinelerra-5.1 / cinelerra / keyframe.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2010 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 "bcsignals.h"
24 #include "clip.h"
25 #include "cstrdup.h"
26 #include "filexml.h"
27 #include "keyframe.h"
28
29 #include <stdio.h>
30 #include <string.h>
31
32
33
34 KeyFrame::KeyFrame()
35  : Auto()
36 {
37         data[0] = 0;
38 }
39
40 KeyFrame::KeyFrame(EDL *edl, KeyFrames *autos)
41  : Auto(edl, (Autos*)autos)
42 {
43         data[0] = 0;
44 }
45
46 KeyFrame::~KeyFrame()
47 {
48 }
49
50 void KeyFrame::load(FileXML *file)
51 {
52 //printf("KeyFrame::load 1\n");
53 // Shouldn't be necessary
54 //      position = file->tag.get_property((char*)"POSITION", position);
55 //printf("KeyFrame::load 1\n");
56
57         int len = file->read_data_until((char*)"/KEYFRAME", data, MESSAGESIZE-1);
58         data[len] = 0;
59 //printf("KeyFrame::load 2 data=\n%s\nend of data\n", data);
60 }
61
62 void KeyFrame::copy(int64_t start, int64_t end, FileXML *file, int default_auto)
63 {
64 //printf("KeyFrame::copy 1 %d\n%s\n", position - start, data);
65         file->tag.set_title((char*)"KEYFRAME");
66         if(default_auto)
67                 file->tag.set_property((char*)"POSITION", 0);
68         else
69                 file->tag.set_property((char*)"POSITION", position - start);
70 // default_auto converts a default auto to a normal auto
71         if(is_default && !default_auto)
72                 file->tag.set_property((char*)"DEFAULT", 1);
73         file->append_tag();
74 // Can't put newlines in because these would be reimported and resaved along
75 // with new newlines.
76 //      file->append_newline();
77
78         file->append_data(data, strlen(data));
79 //      file->append_newline();
80
81         file->tag.set_title((char*)"/KEYFRAME");
82         file->append_tag();
83         file->append_newline();
84 }
85
86
87 void KeyFrame::copy_from(Auto *that)
88 {
89         copy_from((KeyFrame*)that);
90 }
91
92 void KeyFrame::copy_from(KeyFrame *that)
93 {
94         Auto::copy_from(that);
95         KeyFrame *keyframe = (KeyFrame*)that;
96         strcpy(data, keyframe->data);
97         position = keyframe->position;
98 }
99
100 void KeyFrame::copy_data(KeyFrame *src)
101 {
102         strcpy(data, src->data);
103 }
104
105 int KeyFrame::identical(KeyFrame *src)
106 {
107         return !strcasecmp(src->data, data);
108 }
109
110 void KeyFrame::get_contents(BC_Hash *ptr, char **text, char **extra)
111 {
112         FileXML input;
113         input.set_shared_input(data, strlen(data));
114         int result = 0;
115         char *this_text = 0;
116         char *this_extra = 0;
117         while(!result)
118         {
119                 result = input.read_tag();
120                 if(!result)
121                 {
122                         for(int i = 0; i < input.tag.properties.size(); i++)
123                         {
124                                 const char *key = input.tag.get_property_text(i);
125                                 const char *value = input.tag.get_property(key);
126                                 ptr->update(key, value);
127                         }
128
129 // Read any text after tag
130                         this_text = input.read_text();
131                         (*text) = cstrdup(this_text);
132
133 // Read remaining data
134                         this_extra = input.get_data();
135                         (*extra) = cstrdup(this_extra);
136                         break;
137                 }
138         }
139 }
140
141 void KeyFrame::update_parameter(BC_Hash *params,
142         const char *text, const char *extra)
143 {
144         FileXML output;
145         FileXML input;
146         input.set_shared_input(get_data(), strlen(get_data()));
147         int result = 0;
148         BC_Hash this_params;
149         char *this_text = 0;
150         char *this_extra = 0;
151         int got_it = 0;
152
153 // printf("KeyFrame::update_parameter %d %p %p %p \n",
154 // __LINE__,
155 // params,
156 // text,
157 // extra);
158
159
160         get_contents(&this_params, &this_text, &this_extra);
161
162
163 // printf("KeyFrame::update_parameter %d params=%p\n", __LINE__, params);
164 // if(params) params->dump();
165 // printf("KeyFrame::update_parameter %d\n", __LINE__);
166 // this_params.dump();
167
168 // Get first tag
169         while(!result)
170         {
171                 result = input.read_tag();
172                 if(!result)
173                 {
174 // Replicate first tag
175                         output.tag.set_title(input.tag.get_title());
176
177 // Get each parameter from this keyframe
178                         for(int i = 0; i < this_params.size(); i++)
179                         {
180                                 const char *key = this_params.get_key(i);
181                                 const char *value = this_params.get_value(i);
182
183 // Get new value from the params argument
184                                 got_it = 1;
185                                 if(params)
186                                 {
187                                         got_it = 0;
188                                         for(int j = 0; j < params->size(); j++)
189                                         {
190                                                 if(!strcmp(params->get_key(j), key))
191                                                 {
192                                                         got_it = 1;
193                                                         value = params->get_value(j);
194                                                         break;
195                                                 }
196                                         }
197                                 }
198
199 // Set parameter in output.
200                                 output.tag.set_property(key, value);
201                         }
202
203 // Get each parameter from params argument
204                         if(params)
205                         {
206                                 for(int i = 0; i < params->size(); i++)
207                                 {
208                                         const char *key = params->get_key(i);
209 //printf("KeyFrame::update_parameter %d %s\n", __LINE__, key);
210
211                                         got_it = 0;
212                                         for(int j = 0; j < this_params.size(); j++)
213                                         {
214                                                 if(!strcmp(this_params.get_key(j), key))
215                                                 {
216                                                         got_it = 1;
217                                                         break;
218                                                 }
219                                         }
220 //printf("KeyFrame::update_parameter %d %s\n", __LINE__, key);
221
222 // If it wasn't found in output, set new parameter in output.
223                                         if(!got_it)
224                                         {
225                                                 output.tag.set_property(key, params->get_value(i));
226 //printf("KeyFrame::update_parameter %d %s\n", __LINE__, key);
227                                         }
228                                 }
229                         }
230
231 // Append parameters to output
232                         output.append_tag();
233 // Write anonymous text & duplicate the rest
234                         output.append_text(text ? text : this_text);
235 // Append remaining previous data
236                         output.append_data(extra ? extra : this_extra);
237 // Move output to input
238                         output.terminate_string();
239                         strcpy(this->data, output.string());
240                         break;
241                 }
242         }
243
244         delete [] this_text;
245         delete [] this_extra;
246 }
247
248
249 void KeyFrame::get_diff(KeyFrame *src,
250         BC_Hash **params,
251         char **text,
252         char **extra)
253 {
254         const int debug = 0;
255         FileXML input;
256         input.set_shared_input(data, strlen(data));
257         BC_Hash this_params;
258         char *this_text = 0;
259         char *this_extra = 0;
260         BC_Hash src_parameters;
261         char *src_text = 0;
262         char *src_extra = 0;
263
264         get_contents(&this_params, &this_text, &this_extra);
265         src->get_contents(&src_parameters, &src_text, &src_extra);
266
267 if(debug) printf("KeyFrame::get_diff %d %d %d\n",
268 __LINE__,
269 this_params.size(),
270 src_parameters.size());
271
272 // Capture changed parameters
273         char this_value[BCTEXTLEN];
274         for(int i = 0; i < MIN(this_params.size(), src_parameters.size()); i++)
275         {
276                 const char *src_key = src_parameters.get_key(i);
277                 const char *src_value = src_parameters.get_value(i);
278                 this_value[0] = 0;
279                 this_params.get(src_key, this_value);
280 if(debug) printf("KeyFrame::get_diff %d %s %s %s\n",
281 __LINE__,
282 src_key,
283 src_value,
284 this_value);
285 // Capture values which differ
286                 if(strcmp(src_value, this_value))
287                 {
288                         if(!(*params)) (*params) = new BC_Hash;
289                         (*params)->update(src_key, src_value);
290                 }
291         }
292
293
294 // Capture text which differs
295         if( !this_text || strcmp(this_text, src_text))
296                 (*text) = cstrdup(src_text);
297
298         if( !this_extra || strcmp(this_extra, src_extra))
299                 (*extra) = cstrdup(src_extra);
300
301
302         delete [] this_text;
303         delete [] this_extra;
304         delete [] src_text;
305         delete [] src_extra;
306 }
307
308 int KeyFrame::operator==(Auto &that)
309 {
310         return identical((KeyFrame*)&that);
311 }
312
313 int KeyFrame::operator==(KeyFrame &that)
314 {
315         return identical(&that);
316 }
317
318 char* KeyFrame::get_data()
319 {
320         return data;
321 }
322
323 void KeyFrame::set_data(char *data)
324 {
325         strcpy(this->data, data);
326 }
327
328 void KeyFrame::dump(FILE *fp)
329 {
330         fprintf(fp,"     position: %jd\n", position);
331         fprintf(fp,"     data: %s\n", data);
332 }
333