fix transition keyframe update when autogenerate keyframes set, revert copy operators...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / edit.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 "asset.h"
23 #include "assets.h"
24 #include "bcsignals.h"
25 #include "clip.h"
26 #include "edit.h"
27 #include "edits.h"
28 #include "edl.h"
29 #include "edlsession.h"
30 #include "filexml.h"
31 #include "filesystem.h"
32 #include "labels.h"
33 #include "localsession.h"
34 #include "plugin.h"
35 #include "mainsession.h"
36 #include "strack.h"
37 #include "trackcanvas.h"
38 #include "tracks.h"
39 #include "transition.h"
40 #include <string.h>
41
42
43 Edit::Edit()
44 {
45         reset();
46 }
47
48 Edit::Edit(EDL *edl, Track *track)
49 {
50         reset();
51         this->edl = edl;
52         this->track = track;
53         if(track) this->edits = track->edits;
54         id = EDL::next_id();
55         orig_id = id;
56 }
57
58 Edit::Edit(EDL *edl, Edits *edits)
59 {
60         reset();
61         this->edl = edl;
62         this->edits = edits;
63         if(edits) this->track = edits->track;
64         id = EDL::next_id();
65         orig_id = id;
66 }
67
68 Edit::~Edit()
69 {
70 //printf("Edit::~Edit 1\n");
71         if(transition) delete transition;
72 //printf("Edit::~Edit 2\n");
73 }
74
75 void Edit::reset()
76 {
77         edl = 0;
78         track = 0;
79         edits = 0;
80         startsource = 0;
81         startproject = 0;
82         length = 0;
83         asset = 0;
84         transition = 0;
85         channel = 0;
86         user_title[0] = 0;
87         nested_edl = 0;
88         is_selected = 0;
89         hard_left = 0;
90         hard_right = 0;
91         color = 0;
92         group_id = 0;
93 }
94
95 Indexable* Edit::get_source()
96 {
97         if(asset) return asset;
98         if(nested_edl) return nested_edl;
99         return 0;
100 }
101
102 int Edit::copy(int64_t start,
103         int64_t end,
104         FileXML *file,
105         const char *output_path)
106 {
107 // variables
108 //printf("Edit::copy 1\n");
109
110         int64_t endproject = startproject + length;
111         int result;
112
113         if((startproject >= start && startproject <= end) ||  // startproject in range
114                  (endproject <= end && endproject >= start) ||     // endproject in range
115                  (startproject <= start && endproject >= end))    // range in project
116         {
117 // edit is in range
118                 int64_t startproject_in_selection = startproject; // start of edit in selection in project
119                 int64_t startsource_in_selection = startsource; // start of source in selection in source
120                 //int64_t endsource_in_selection = startsource + length; // end of source in selection
121                 int64_t length_in_selection = length;             // length of edit in selection
122 //printf("Edit::copy 2\n");
123
124                 if(startproject < start)
125                 {         // start is after start of edit in project
126                         int64_t length_difference = start - startproject;
127
128                         startsource_in_selection += length_difference;
129                         startproject_in_selection += length_difference;
130                         length_in_selection -= length_difference;
131                 }
132 //printf("Edit::copy 3\n");
133
134                 if(endproject > end)
135                 {         // end is before end of edit in project
136                         length_in_selection = end - startproject_in_selection;
137                 }
138
139 //printf("Edit::copy 4\n");
140                 if(file)    // only if not counting
141                 {
142                         file->tag.set_title("EDIT");
143                         file->tag.set_property("STARTSOURCE", startsource_in_selection);
144                         file->tag.set_property("CHANNEL", (int64_t)channel);
145                         file->tag.set_property("LENGTH", length_in_selection);
146                         file->tag.set_property("HARD_LEFT", hard_left);
147                         file->tag.set_property("HARD_RIGHT", hard_right);
148                         file->tag.set_property("COLOR", color);
149                         file->tag.set_property("GROUP_ID", group_id);
150                         if(user_title[0]) file->tag.set_property("USER_TITLE", user_title);
151 //printf("Edit::copy 5\n");
152
153                         copy_properties_derived(file, length_in_selection);
154
155                         file->append_tag();
156 //                      file->append_newline();
157 //printf("Edit::copy 6\n");
158
159                         if(nested_edl)
160                         {
161                                 file->tag.set_title("NESTED_EDL");
162                                 file->tag.set_property("SRC", nested_edl->path);
163                                 file->append_tag();
164                                 file->tag.set_title("/NESTED_EDL");
165                                 file->append_tag();
166                                 file->append_newline();
167                         }
168
169                         if(asset)
170                         {
171 //printf("Edit::copy 6 %s\n", asset->path);
172                                 char stored_path[BCTEXTLEN];
173                                 char asset_directory[BCTEXTLEN];
174                                 char output_directory[BCTEXTLEN];
175                                 FileSystem fs;
176
177 //printf("Edit::copy %d %s\n", __LINE__, asset->path);
178                                 fs.extract_dir(asset_directory, asset->path);
179 //printf("Edit::copy %d %s\n", __LINE__, asset->path);
180
181                                 if(output_path)
182                                         fs.extract_dir(output_directory, output_path);
183                                 else
184                                         output_directory[0] = 0;
185 //printf("Edit::copy %s, %s %s, %s\n", asset->path, asset_directory, output_path, output_directory);
186
187                                 if(output_path && !strcmp(asset_directory, output_directory))
188                                         fs.extract_name(stored_path, asset->path);
189                                 else
190                                         strcpy(stored_path, asset->path);
191
192                                 file->tag.set_title("FILE");
193                                 file->tag.set_property("SRC", stored_path);
194                                 file->append_tag();
195                                 file->tag.set_title("/FILE");
196                                 file->append_tag();
197                         }
198
199                         if(transition && startsource_in_selection == startsource)
200                         {
201                                 transition->save_xml(file);
202                         }
203
204 //printf("Edit::copy 7\n");
205                         file->tag.set_title("/EDIT");
206                         file->append_tag();
207                         file->append_newline();
208 //printf("Edit::copy 8\n");
209                 }
210 //printf("Edit::copy 9\n");
211                 result = 1;
212         }
213         else
214         {
215                 result = 0;
216         }
217 //printf("Edit::copy 10\n");
218         return result;
219 }
220
221
222 int64_t Edit::get_source_end(int64_t default_)
223 {
224         return default_;
225 }
226
227 void Edit::insert_transition(char *title)
228 {
229 //printf("Edit::insert_transition this=%p title=%p title=%s\n", this, title, title);
230         delete transition;
231         transition = new Transition(edl, this, title,
232                 track->to_units(edl->session->default_transition_length, 1));
233 }
234
235 void Edit::detach_transition()
236 {
237         if(transition) delete transition;
238         transition = 0;
239 }
240
241 int Edit::silence()
242 {
243         return (track->data_type != TRACK_SUBTITLE ?
244                 asset || nested_edl :
245                 *((SEdit *)this)->get_text()) ? 0 : 1;
246 }
247
248 void Edit::set_selected(int v)
249 {
250         if( group_id )
251                 edl->tracks->set_group_selected(group_id, v);
252         else
253                 is_selected = v >= 0 ? v : !is_selected ? 1 : 0;
254 }
255
256 void Edit::copy_from(Edit *edit)
257 {
258         this->orig_id = edit->orig_id;
259         this->nested_edl = edl->nested_edls.get_nested(edit->nested_edl);
260         this->asset = edl->assets->update(edit->asset);
261         this->startsource = edit->startsource;
262         this->startproject = edit->startproject;
263         this->length = edit->length;
264         this->hard_left = edit->hard_left;
265         this->hard_right = edit->hard_right;
266         this->color = edit->color;
267         this->group_id = edit->group_id;
268         strcpy (this->user_title, edit->user_title);
269
270         if(edit->transition)
271         {
272                 if(!transition) transition = new Transition(edl,
273                         this,
274                         edit->transition->title,
275                         edit->transition->length);
276                 *this->transition = *edit->transition;
277         }
278         this->channel = edit->channel;
279 }
280
281 void Edit::clone_from(Edit *edit)
282 {
283         copy_from(edit);
284         edit->orig_id = edit->id;
285 }
286
287 void Edit::equivalent_output(Edit *edit, int64_t *result)
288 {
289 // End of edit changed
290         if(startproject + length != edit->startproject + edit->length)
291         {
292                 int64_t new_length = MIN(startproject + length,
293                         edit->startproject + edit->length);
294                 if(*result < 0 || new_length < *result)
295                         *result = new_length;
296         }
297
298         if(
299 // Different nested EDLs
300                 (edit->nested_edl && !nested_edl) ||
301                 (!edit->nested_edl && nested_edl) ||
302 // Different assets
303                 (edit->asset == 0 && asset != 0) ||
304                 (edit->asset != 0 && asset == 0) ||
305 // different transitions
306                 (edit->transition == 0 && transition != 0) ||
307                 (edit->transition != 0 && transition == 0) ||
308 // Position changed
309                 (startproject != edit->startproject) ||
310                 (startsource != edit->startsource) ||
311 // Transition changed
312                 (transition && edit->transition &&
313                         !transition->identical(edit->transition)) ||
314 // Asset changed
315                 (asset && edit->asset &&
316                         !asset->equivalent(*edit->asset, 1, 1, edl)) ||
317 // Nested EDL changed
318                 (nested_edl && edit->nested_edl &&
319                         strcmp(nested_edl->path, edit->nested_edl->path))
320                 )
321         {
322 // Start of edit changed
323                 if(*result < 0 || startproject < *result) *result = startproject;
324         }
325 }
326
327
328 Edit& Edit::operator=(Edit& edit)
329 {
330 //printf("Edit::operator= called\n");
331         copy_from(&edit);
332         return *this;
333 }
334
335 void Edit::synchronize_params(Edit *edit)
336 {
337         copy_from(edit);
338 }
339
340
341 // Comparison for ResourcePixmap drawing
342 int Edit::identical(Edit &edit)
343 {
344         int result = (this->nested_edl == edit.nested_edl &&
345                 this->asset == edit.asset &&
346                 this->startsource == edit.startsource &&
347                 this->startproject == edit.startproject &&
348                 this->length == edit.length &&
349                 this->transition == edit.transition &&
350                 this->channel == edit.channel);
351         return result;
352 }
353
354 int Edit::operator==(Edit &edit)
355 {
356         return identical(edit);
357 }
358
359 double Edit::frames_per_picon()
360 {
361         return Units::round(picon_w()) / frame_w();
362 }
363
364 double Edit::frame_w()
365 {
366         return track->from_units(1) *
367                 edl->session->sample_rate /
368                 edl->local_session->zoom_sample;
369 }
370
371 double Edit::picon_w()
372 {
373         int w = 0, h = 0;
374         if(asset) {
375                 w = asset->width;
376                 h = asset->height;
377         }
378         else if(nested_edl) {
379                 w = nested_edl->session->output_w;
380                 h = nested_edl->session->output_h;
381         }
382         return w>0 && h>0 ? ((double)edl->local_session->zoom_track*w)/h : 0;
383 }
384
385 int Edit::picon_h()
386 {
387         return edl->local_session->zoom_track;
388 }
389
390
391 int Edit::dump(FILE *fp)
392 {
393         fprintf(fp,"     EDIT %p\n", this); fflush(fp);
394         fprintf(fp,"      id %d, orig_id %d, nested_edl=%p %s asset=%p %s\n",
395                 id, orig_id, nested_edl, nested_edl ? nested_edl->path : "",
396                 asset, asset ? asset->path : "");
397         fflush(fp);
398         fprintf(fp,"      channel %d, color %08x, hard lt/rt %d/%d"
399                 " group_id %d, is_selected %d\n",
400                 channel, color, hard_left, hard_right, group_id, is_selected);
401         if( transition ) {
402                 fprintf(fp,"      TRANSITION %p\n", transition);
403                 transition->dump(fp);
404         }
405         fprintf(fp,"      startsource %jd startproject %jd length %jd\n",
406                 startsource, startproject, length);
407         fflush(fp);
408         return 0;
409 }
410
411 int Edit::load_properties(FileXML *file, int64_t &startproject)
412 {
413         startsource = file->tag.get_property("STARTSOURCE", (int64_t)0);
414         length = file->tag.get_property("LENGTH", (int64_t)0);
415         hard_left = file->tag.get_property("HARD_LEFT", (int64_t)0);
416         hard_right = file->tag.get_property("HARD_RIGHT", (int64_t)0);
417         color = file->tag.get_property("COLOR", 0);
418         group_id = file->tag.get_property("GROUP_ID", group_id);
419         user_title[0] = 0;
420         file->tag.get_property("USER_TITLE", user_title);
421         this->startproject = startproject;
422         load_properties_derived(file);
423         return 0;
424 }
425
426 void Edit::shift(int64_t difference)
427 {
428         startproject += difference;
429 }
430
431 void Edit::trim(int64_t difference)
432 {
433         length += difference;
434         if( startproject < 0 ) {
435                 if( (startsource+=startproject) < 0 ) startsource = 0;
436                 if( (length+=startproject) < 0 ) length = 0;
437                 startproject = 0;
438         }
439         if( startsource < 0 )
440                 startsource = 0;
441         int64_t src_len = get_source_end(INT64_MAX);
442         if( startsource + length > src_len ) {
443                 length = src_len - startsource;
444                 if( length < 0 ) length = 0;
445         }
446         if( length < 0 )
447                 length = 0;
448 }
449
450 int Edit::shift_start(int edit_mode, int64_t newposition, int64_t oldposition,
451         int edit_labels, int edit_autos, int edit_plugins, Edits *trim_edits)
452 {
453         int64_t cut_length = newposition - oldposition;
454         int rest_moved = edit_mode == MOVE_RIPPLE || edit_mode == MOVE_EDGE ? 1 : 0;
455         if( cut_length > length )
456                 cut_length = length;
457         else if( cut_length < -length )
458                 cut_length = -length;
459
460         int64_t start = startproject, end = start + length;
461         Edit *prev = this->previous, *next = this->next;
462         int edits_moved = 0;
463
464         switch( edit_mode ) {
465         case MOVE_RIPPLE:
466                 edits_moved = 1;
467                 startsource += cut_length;
468                 cut_length = -cut_length;
469                 length += cut_length;
470                 for( Edit *edit=next; edit; edit=edit->next )
471                         edit->startproject += cut_length;
472                 break;
473         case MOVE_ROLL:
474                 if( prev && prev->length + cut_length < 0 )
475                         cut_length = -prev->length;
476                 if( prev ) prev->trim(cut_length);
477                 startproject += cut_length;
478                 startsource += cut_length;
479                 length -= cut_length;
480                 break;
481         case MOVE_SLIP:
482                 edits_moved = 1;
483                 startsource -= cut_length;
484                 break;
485         case MOVE_SLIDE:
486                 edits_moved = 1;
487                 if( prev && prev->length + cut_length < 0 )
488                         cut_length = -prev->length;
489                 if( next && next->length - cut_length < 0 )
490                         cut_length = next->length;
491                 if( prev ) prev->trim(cut_length);
492                 startproject += cut_length;
493                 if( next ) {
494                         next->startproject += cut_length;
495                         next->startsource += cut_length;
496                         next->trim(-cut_length);
497                 }
498                 break;
499         case MOVE_EDGE:
500                 edits_moved = 1;
501                 startsource -= cut_length;
502                 length += cut_length;
503                 for( Edit *edit=next; edit; edit=edit->next )
504                         edit->startproject += cut_length;
505                 break;
506         }
507         trim(0);
508         return follow_edits(start, end, cut_length, edits_moved, rest_moved,
509                 edit_labels, edit_autos, edit_plugins, trim_edits);
510 }
511
512 int Edit::shift_end(int edit_mode, int64_t newposition, int64_t oldposition,
513         int edit_labels, int edit_autos, int edit_plugins, Edits *trim_edits)
514 {
515         int64_t cut_length = newposition - oldposition;
516         int rest_moved = edit_mode == MOVE_RIPPLE || edit_mode == MOVE_EDGE ? 1 : 0;
517         if( cut_length > length ) {
518                  if( !rest_moved ) cut_length = length;
519         }
520         else if( cut_length < -length )
521                 cut_length = -length;
522         int64_t start = startproject, end = start + length;
523         Edit *prev = this->previous, *next = this->next;
524         int edits_moved = 0;
525
526         switch( edit_mode ) {
527         case MOVE_RIPPLE:
528         case MOVE_EDGE:
529                 length += cut_length;
530                 for( Edit *edit=next; edit; edit=edit->next )
531                         edit->startproject += cut_length;
532                 break;
533         case MOVE_ROLL:
534                 if( next && next->length - cut_length < 0 )
535                         cut_length = next->length;
536                 length += cut_length;
537                 if( next ) {
538                         next->startproject += cut_length;
539                         next->startsource += cut_length;
540                         next->trim(-cut_length);
541                 }
542                 break;
543         case MOVE_SLIP:
544                 edits_moved = 1;
545                 startsource -= cut_length;
546                 break;
547         case MOVE_SLIDE:
548                 edits_moved = 1;
549                 if( prev && prev->length + cut_length < 0 )
550                         cut_length = -prev->length;
551                 if( next && next->length - cut_length < 0 )
552                         cut_length = next->length;
553                 if( prev ) prev->trim(cut_length);
554                 startproject += cut_length;
555                 if( next ) {
556                         next->startproject += cut_length;
557                         next->startsource += cut_length;
558                         next->trim(-cut_length);
559                 }
560                 break;
561         }
562         trim(0);
563         return follow_edits(start, end, cut_length, edits_moved, rest_moved,
564                 edit_labels, edit_autos, edit_plugins, trim_edits);
565 }
566
567 int Edit::follow_edits(int64_t start, int64_t end, int64_t cut_length,
568                 int edits_moved, int rest_moved, int edit_labels, int edit_autos,
569                 int edit_plugins, Edits *trim_edits)
570 {
571         if( edits_moved && rest_moved ) {
572                 if( edit_labels ) {
573                         double cut_len = track->from_units(cut_length);
574                         double start_pos = edits->track->from_units(start);
575                         if( cut_len < 0 )
576                                 edits->edl->labels->clear(start_pos + cut_len, start_pos, 1);
577                         else if( cut_len > 0 )
578                                 edits->edl->labels->insert(start_pos, cut_len);
579                 }
580                 if( cut_length < 0 )
581                         track->clear(start + cut_length, start,
582                                 0, 0, edit_autos, edit_plugins, trim_edits);
583                 else if( cut_length > 0 ) {
584                         if( edit_autos )
585                                 track->shift_keyframes(start, cut_length);
586                         if( edit_plugins )
587                                 track->shift_effects(start, cut_length, 1, trim_edits);
588                 }
589         }
590         else if( edits_moved ) {
591                 if( edit_labels ) {
592                         double cut_len = track->from_units(cut_length);
593                         double start_pos = edits->track->from_units(start);
594                         edits->edl->labels->insert(start_pos, cut_len);
595                         double end_pos = edits->track->from_units(end);
596                         edits->edl->labels->insert(end_pos + cut_len, -cut_len);
597                 }
598                 if( edit_autos ) {
599                         if( cut_length > 0 ) {
600                                 track->clear(end, end+cut_length, 0, 0, 0, 1, 0);
601                                 track->shift_keyframes(start, cut_length);
602                         }
603                         else if( cut_length < 0 ) {
604                                 track->clear(start+cut_length, start, 0, 0, 0, 1, 0);
605                                 track->shift_keyframes(end+cut_length, -cut_length);
606                         }
607                 }
608                 if( edit_plugins ) {
609                         if( cut_length > 0 ) {
610                                 track->clear(end, end+cut_length, 0, 0, -1, 0, 0);
611                                 track->shift_effects(start, cut_length, 1, 0);
612                         }
613                         else if( cut_length < 0 ) {
614                                 track->clear(start+cut_length, start, 0, 0, -1, 0, 0);
615                                 track->shift_effects(end+cut_length, -cut_length, 1, 0);
616                         }
617                 }
618         }
619         else if( rest_moved ) {
620                 if( edit_labels ) {
621                         double cut_len = track->from_units(cut_length);
622                         double end_pos = edits->track->from_units(end);
623                         if( cut_len < 0 )
624                                 edits->edl->labels->clear(end_pos + cut_len, end_pos, 1);
625                         else if( cut_len > 0 )
626                                 edits->edl->labels->insert(end_pos, cut_len);
627                 }
628                 if( cut_length < 0 )
629                         track->clear(end + cut_length, end,
630                                 0, 0, edit_autos, edit_plugins, trim_edits);
631                 else if( cut_length > 0 ) {
632                         if( edit_autos )
633                                 track->shift_keyframes(end, cut_length);
634                         if( edit_plugins )
635                                 track->shift_effects(end, cut_length, 1, trim_edits);
636                 }
637         }
638         return 0;
639 }
640
641
642
643 int Edit::popup_transition(float view_start, float zoom_units, int cursor_x, int cursor_y)
644 {
645         int64_t left, right, left_unit, right_unit;
646         if(!transition) return 0;
647         get_handle_parameters(left, right, left_unit, right_unit, view_start, zoom_units);
648
649         if(cursor_x > left && cursor_x < right)
650         {
651 //              transition->popup_transition(cursor_x, cursor_y);
652                 return 1;
653         }
654         return 0;
655 }
656
657 int Edit::select_handle(float view_start, float zoom_units, int cursor_x, int cursor_y, int64_t &selection)
658 {
659         int64_t left, right, left_unit, right_unit;
660         get_handle_parameters(left, right, left_unit, right_unit, view_start, zoom_units);
661
662         int64_t pixel1, pixel2;
663         pixel1 = left;
664         pixel2 = pixel1 + xS(10);
665
666 // test left edit
667 // cursor_x is faked in acanvas
668         if(cursor_x >= pixel1 && cursor_x <= pixel2)
669         {
670                 selection = left_unit;
671                 return 1;     // left handle
672         }
673
674         //int64_t endproject = startproject + length;
675         pixel2 = right;
676         pixel1 = pixel2 - xS(10);
677
678 // test right edit
679         if(cursor_x >= pixel1 && cursor_x <= pixel2)
680         {
681                 selection = right_unit;
682                 return 2;     // right handle
683         }
684         return 0;
685 }
686
687 void Edit::get_title(char *title)
688 {
689         if( user_title[0] ) {
690                 strcpy(title, user_title);
691                 return;
692         }
693         Indexable *idxbl = asset ? (Indexable*)asset : (Indexable*)nested_edl;
694         if( !idxbl ) {
695                 title[0] = 0;
696                 return;
697         }
698         FileSystem fs;
699         fs.extract_name(title, idxbl->path);
700         if( asset || track->data_type == TRACK_AUDIO ) {
701                 char number[BCSTRLEN];
702                 sprintf(number, " #%d", channel + 1);
703                 strcat(title, number);
704         }
705 }
706