4 #include "automation.h"
10 #include "edlsession.h"
13 #include "filesystem.h"
14 #include "localsession.h"
16 #include "strategies.inc"
18 #include "transition.h"
19 #include "transportque.inc"
23 Edits::Edits(EDL *edl, Track *track, Edit *default_edit)
29 List<Edit>::List<Edit>();
30 default_edit->track = track;
31 default_edit->startproject = 0;
32 default_edit->length = LAST_VIRTUAL_LENGTH;
33 insert_after(0, default_edit);
42 void Edits::equivalent_output(Edits *edits, int64_t *result)
44 // For the case of plugin sets, a new plugin set may be created with
45 // plugins only starting after 0. We only want to restart brender at
46 // the first plugin in this case.
47 for(Edit *current = first, *that_current = edits->first;
48 current || that_current;
50 that_current = that_current->next)
52 //printf("Edits::equivalent_output 1 %d\n", *result);
53 if(!current && that_current)
55 int64_t position1 = length();
56 int64_t position2 = that_current->startproject;
57 if(*result < 0 || *result > MIN(position1, position2))
58 *result = MIN(position1, position2);
62 if(current && !that_current)
64 int64_t position1 = edits->length();
65 int64_t position2 = current->startproject;
66 if(*result < 0 || *result > MIN(position1, position2))
67 *result = MIN(position1, position2);
72 //printf("Edits::equivalent_output 2 %d\n", *result);
73 current->equivalent_output(that_current, result);
74 //printf("Edits::equivalent_output 3 %d\n", *result);
79 void Edits::copy_from(Edits *edits)
81 while(last) delete last;
82 for(Edit *current = edits->first; current; current = NEXT)
84 Edit *new_edit = append(create_edit());
85 new_edit->copy_from(current);
87 loaded_length = edits->loaded_length;
91 Edits& Edits::operator=(Edits& edits)
93 printf("Edits::operator= 1\n");
99 void Edits::insert_asset(Asset *asset,
104 Edit *new_edit = insert_new_edit(position);
106 new_edit->asset = asset;
107 new_edit->startsource = 0;
108 new_edit->startproject = position;
109 new_edit->length = length;
111 if(asset->audio_data)
112 new_edit->channel = track_number % asset->channels;
114 if(asset->video_data)
115 new_edit->channel = track_number % asset->layers;
117 //printf("Edits::insert_asset %d %d\n", new_edit->channel, new_edit->length);
118 for(Edit *current = new_edit->next; current; current = NEXT)
120 current->startproject += length;
124 void Edits::insert_edits(Edits *source_edits, int64_t position)
126 int64_t clipboard_length =
127 track->to_units(source_edits->edl->local_session->clipboard_length, 1);
128 int64_t clipboard_end = position + clipboard_length;
130 int64_t total_length = 0;
131 for(Edit *source_edit = source_edits->first;
133 source_edit = source_edit->next)
136 Asset *dest_asset = edl->assets->update(source_edit->asset);
137 // Open destination area
138 Edit *dest_edit = insert_new_edit(position + source_edit->startproject);
140 dest_edit->copy_from(source_edit);
141 dest_edit->asset = dest_asset;
142 dest_edit->startproject = position + source_edit->startproject;
146 // Shift keyframes in source edit to their position in the
147 // destination edit for plugin case
148 dest_edit->shift_keyframes(position);
151 total_length += dest_edit->length;
152 if (source_edits->loaded_length && total_length > source_edits->loaded_length)
154 dest_edit->length -= (total_length - source_edits->loaded_length);
157 // Shift following edits and keyframes in following edits by length
158 // in current source edit.
159 for(Edit *future_edit = dest_edit->next;
161 future_edit = future_edit->next)
163 future_edit->startproject += dest_edit->length;
164 future_edit->shift_keyframes(dest_edit->length);
167 // Fill clipboard length with silence
168 if(!source_edit->next &&
169 dest_edit->startproject + dest_edit->length < clipboard_end)
171 paste_silence(dest_edit->startproject + dest_edit->length,
179 // Can't paste silence in here because it's used by paste_silence.
180 Edit* Edits::insert_new_edit(int64_t position)
183 //printf("Edits::insert_new_edit 1\n");
185 current = split_edit(position);
187 //printf("Edits::insert_new_edit 1\n");
188 if (current->length == 0) // when creating a split we got 0-length edit, just use it!
190 else // we need to insert
193 new_edit = create_edit();
194 insert_after(current, new_edit);
196 //printf("Edits::insert_new_edit 1\n");
197 new_edit->startproject = position;
198 //printf("Edits::insert_new_edit 2\n");
203 Edit* Edits::split_edit(int64_t position)
205 // Get edit containing position
206 Edit *edit = editof(position, PLAY_FORWARD, 0);
207 // No edit found, make one - except when we are at zero position!
208 if(!edit && position != 0)
209 if (length() == position)
211 edit = last; // we do not need any edit to extend past the last one
213 if (!last || length() < position)
216 // Even when track is completely empty or split is beyond last edit, return correct edit
217 Edit *empty = create_edit();
219 empty->startproject = length(); // end of last edit
221 empty->startproject = 0; // empty track
222 empty->length = position - empty->startproject;
223 insert_after(last, empty);
226 { // now we are now surely in situation where we have a) broken edit list or b) negative position... report error!
228 printf("Trying to insert edit at position, but failed: %lli\n", position);
229 printf("Dump is here:\n");
233 // Split would have created a 0 length
234 // if(edit->startproject == position) return edit;
235 // Create anyway so the return value comes before position
237 Edit *new_edit = create_edit();
238 insert_after(edit, new_edit);
239 if (edit) // if we have actually split the edit, do the funky stuff!
241 new_edit->copy_from(edit);
242 new_edit->length = new_edit->startproject + new_edit->length - position;
243 edit->length = position - edit->startproject;
244 new_edit->startsource += edit->length;
247 // Decide what to do with the transition
248 if(edit->length && edit->transition)
250 delete new_edit->transition;
251 new_edit->transition = 0;
254 if(edit->transition && edit->transition->length > edit->length)
255 edit->transition->length = edit->length;
256 if(new_edit->transition && new_edit->transition->length > new_edit->length)
257 new_edit->transition->length = new_edit->length;
259 new_edit->length = 0;
260 new_edit->startproject = position;
264 int Edits::save(FileXML *xml, char *output_path)
266 copy(0, length(), xml, output_path);
270 void Edits::resample(double old_rate, double new_rate)
272 for(Edit *current = first; current; current = NEXT)
274 current->startproject = Units::to_int64((double)current->startproject /
277 if(PREVIOUS) PREVIOUS->length = current->startproject - PREVIOUS->startproject;
278 current->startsource = Units::to_int64((double)current->startsource /
281 if(!NEXT) current->length = Units::to_int64((double)current->length /
284 if(current->transition)
286 current->transition->length = Units::to_int64(
287 (double)current->transition->length /
291 current->resample(old_rate, new_rate);
308 int Edits::optimize()
314 // Sort edits by starting point
319 for(current = first; current; current = NEXT)
321 Edit *next_edit = NEXT;
323 if(next_edit && next_edit->startproject < current->startproject)
325 swap(next_edit, current);
331 // Insert silence between edits which aren't consecutive
332 for(current = last; current; current = current->previous)
334 if(current->previous)
336 Edit *previous_edit = current->previous;
337 if(current->startproject -
338 previous_edit->startproject -
339 previous_edit->length > 0)
341 Edit *new_edit = create_edit();
342 insert_before(current, new_edit);
343 new_edit->startproject = previous_edit->startproject + previous_edit->length;
344 new_edit->length = current->startproject -
345 previous_edit->startproject -
346 previous_edit->length;
350 if(current->startproject > 0)
352 Edit *new_edit = create_edit();
353 insert_before(current, new_edit);
354 new_edit->length = current->startproject;
364 // delete 0 length edits
366 current != last && !result; )
368 if(current->length == 0)
370 Edit* next = current->next;
371 // Be smart with transitions!
372 if (next && current->transition && !next->transition)
374 next->transition = current->transition;
375 next->transition->edit = next;
376 current->transition = 0;
383 current = current->next;
386 // merge same files or transitions
388 current && current->next && !result; )
391 Edit *next_edit = current->next;
392 if(current->asset == next_edit->asset &&
394 (current->startsource + current->length == next_edit->startsource &&
395 current->channel == next_edit->channel)
399 // source positions are consecutive
400 current->length += next_edit->length;
405 current = (Plugin*)current->next;
408 // delete last edit of 0 length or silence
410 if (!last || !last->silence())
412 // No last empty edit available... create one
413 Edit *empty_edit = create_edit();
415 empty_edit->startproject = 0;
417 empty_edit->startproject = last->startproject + last->length;
418 empty_edit->length = LAST_VIRTUAL_LENGTH;
419 insert_after(last, empty_edit);
422 last->length = LAST_VIRTUAL_LENGTH;
448 // ===================================== file operations
450 int Edits::load(FileXML *file, int track_offset)
453 int64_t startproject = 0;
457 result = file->read_tag();
459 //printf("Edits::load 1 %s\n", file->tag.get_title());
462 if(!strcmp(file->tag.get_title(), "EDIT"))
464 load_edit(file, startproject, track_offset);
467 if(!strcmp(file->tag.get_title(), "/EDITS"))
475 loaded_length = last->startproject + last->length;
482 int Edits::load_edit(FileXML *file, int64_t &startproject, int track_offset)
486 //printf("Edits::load_edit 1 %d\n", total());
487 current = append_new_edit();
488 //printf("Edits::load_edit 2 %d\n", total());
490 current->load_properties(file, startproject);
492 startproject += current->length;
495 //printf("Edits::load_edit 1\n");
498 //printf("Edits::load_edit 2\n");
499 result = file->read_tag();
500 //printf("Edits::load_edit 3 %s\n", file->tag.get_title());
504 if(file->tag.title_is("FILE"))
507 sprintf(filename, SILENCE);
508 file->tag.get_property("SRC", filename);
510 //printf("Edits::load_edit 4 %s\n", filename);
511 if(strcmp(filename, SILENCE))
513 char directory[BCTEXTLEN], edl_directory[BCTEXTLEN];
515 fs.set_current_dir("");
516 fs.extract_dir(directory, filename);
517 if(!strlen(directory))
519 fs.extract_dir(edl_directory, file->filename);
520 fs.join_names(directory, edl_directory, filename);
521 strcpy(filename, directory);
523 current->asset = edl->assets->get_asset(filename);
527 current->asset = edl->assets->get_asset(SILENCE);
529 //printf("Edits::load_edit 5\n");
532 if(file->tag.title_is("TRANSITION"))
534 current->transition = new Transition(edl,
537 track->to_units(edl->session->default_transition_length, 1));
538 current->transition->load_xml(file);
541 if(file->tag.title_is(SILENCE))
543 //printf("Edits::load_edit 6\n");
544 current->asset = edl->assets->get_asset(SILENCE);
545 //printf("Edits::load_edit 7\n");
548 if(file->tag.title_is("/EDIT"))
553 //printf("Edits::load_edit 8\n");
556 //printf("Edits::load_edit 5\n");
557 // in case of incomplete edit tag
558 if(!current->asset) current->asset = edl->assets->get_asset(SILENCE);
562 // ============================================= accounting
564 int64_t Edits::length()
567 return last->startproject + last->length;
572 Edit* Edits::editof(int64_t position, int direction, int use_nudge)
575 if(use_nudge && track) position += track->nudge;
577 if(direction == PLAY_FORWARD)
579 for(current = last; current; current = PREVIOUS)
581 if(current->startproject <= position && current->startproject + current->length > position)
586 if(direction == PLAY_REVERSE)
588 for(current = first; current; current = NEXT)
590 if(current->startproject < position && current->startproject + current->length >= position)
595 return 0; // return 0 on failure
598 Edit* Edits::get_playable_edit(int64_t position, int use_nudge)
601 if(track && use_nudge) position += track->nudge;
603 // Get the current edit
604 for(current = first; current; current = NEXT)
606 if(current->startproject <= position &&
607 current->startproject + current->length > position)
611 // Get the edit's asset
618 return current; // return 0 on failure
621 // ================================================ editing
625 int Edits::copy(int64_t start, int64_t end, FileXML *file, char *output_path)
629 file->tag.set_title("EDITS");
631 file->append_newline();
633 for(current_edit = first; current_edit; current_edit = current_edit->next)
635 current_edit->copy(start, end, file, output_path);
638 file->tag.set_title("/EDITS");
640 file->append_newline();
645 void Edits::clear(int64_t start, int64_t end)
647 Edit* edit1 = editof(start, PLAY_FORWARD, 0);
648 Edit* edit2 = editof(end, PLAY_FORWARD, 0);
651 if(end == start) return; // nothing selected
652 if(!edit1 && !edit2) return; // nothing selected
656 { // edit2 beyond end of track
658 end = this->length();
663 // in different edits
665 //printf("Edits::clear 3.5 %d %d %d %d\n", edit1->startproject, edit1->length, edit2->startproject, edit2->length);
666 edit1->length = start - edit1->startproject;
667 edit2->length -= end - edit2->startproject;
668 edit2->startsource += end - edit2->startproject;
669 edit2->startproject += end - edit2->startproject;
672 for(current_edit = edit1->next; current_edit && current_edit != edit2;)
674 Edit* next = current_edit->next;
675 remove(current_edit);
679 for(current_edit = edit2; current_edit; current_edit = current_edit->next)
681 current_edit->startproject -= end - start;
686 // in same edit. paste_edit depends on this
688 current_edit = split_edit(start);
690 current_edit->length -= end - start;
691 current_edit->startsource += end - start;
694 for(current_edit = current_edit->next;
696 current_edit = current_edit->next)
698 current_edit->startproject -= end - start;
705 // Used by edit handle and plugin handle movement but plugin handle movement
706 // can only effect other plugins.
707 void Edits::clear_recursive(int64_t start,
714 //printf("Edits::clear_recursive 1\n");
725 int Edits::clear_handle(double start,
732 distance = 0.0; // if nothing is found, distance is 0!
733 for(current_edit = first;
734 current_edit && current_edit->next;
735 current_edit = current_edit->next)
740 if(current_edit->asset &&
741 current_edit->next->asset)
744 if(current_edit->asset->equivalent(*current_edit->next->asset,
749 // Got two consecutive edits in same source
750 if(edl->equivalent(track->from_units(current_edit->next->startproject),
754 int length = -current_edit->length;
755 current_edit->length = current_edit->next->startsource - current_edit->startsource;
756 length += current_edit->length;
758 // Lengthen automation
759 track->automation->paste_silence(current_edit->next->startproject,
760 current_edit->next->startproject + length);
764 track->shift_effects(current_edit->next->startproject,
768 for(current_edit = current_edit->next; current_edit; current_edit = current_edit->next)
770 current_edit->startproject += length;
773 distance = track->from_units(length);
784 int Edits::modify_handles(double oldposition,
796 //printf("Edits::modify_handles 1 %d %f %f\n", currentend, newposition, oldposition);
800 for(current_edit = first; current_edit && !result;)
802 if(edl->equivalent(track->from_units(current_edit->startproject),
805 // edit matches selection
806 //printf("Edits::modify_handles 3 %f %f\n", newposition, oldposition);
807 oldposition = track->from_units(current_edit->startproject);
810 if(newposition >= oldposition)
812 //printf("Edits::modify_handle 1 %s %f %f\n", track->title, oldposition, newposition);
813 // shift start of edit in
814 current_edit->shift_start_in(edit_mode,
815 track->to_units(newposition, 0),
816 track->to_units(oldposition, 0),
824 //printf("Edits::modify_handle 2 %s\n", track->title);
825 // move start of edit out
826 current_edit->shift_start_out(edit_mode,
827 track->to_units(newposition, 0),
828 track->to_units(oldposition, 0),
836 if(!result) current_edit = current_edit->next;
841 // right handle selected
842 for(current_edit = first; current_edit && !result;)
844 if(edl->equivalent(track->from_units(current_edit->startproject) +
845 track->from_units(current_edit->length), oldposition))
847 oldposition = track->from_units(current_edit->startproject) +
848 track->from_units(current_edit->length);
851 //printf("Edits::modify_handle 3\n");
852 if(newposition <= oldposition)
854 // shift end of edit in
855 //printf("Edits::modify_handle 4\n");
856 current_edit->shift_end_in(edit_mode,
857 track->to_units(newposition, 0),
858 track->to_units(oldposition, 0),
863 //printf("Edits::modify_handle 5\n");
867 // move end of edit out
868 //printf("Edits::modify_handle 6\n");
869 current_edit->shift_end_out(edit_mode,
870 track->to_units(newposition, 0),
871 track->to_units(oldposition, 0),
876 //printf("Edits::modify_handle 7\n");
880 if(!result) current_edit = current_edit->next;
881 //printf("Edits::modify_handle 8\n");
890 // Paste silence should not return anything - since pasting silence to an empty track should produce no edits
891 // If we need rutine to create new edit by pushing others forward, write new rutine and call it properly
892 // This are two distinctive functions
893 // This rutine leaves edits in optimized state!
894 void Edits::paste_silence(int64_t start, int64_t end)
896 // paste silence does not do anything if
897 // a) paste silence is on empty track
898 // b) paste silence is after last edit
899 // in both cases editof returns NULL
900 Edit *new_edit = editof(start, PLAY_FORWARD, 0);
901 if (!new_edit) return;
903 if (!new_edit->asset)
904 { // in this case we extend already existing edit
905 new_edit->length += end - start;
907 { // we are in fact creating a new edit
908 new_edit = insert_new_edit(start);
909 new_edit->length = end - start;
911 for(Edit *current = new_edit->next; current; current = NEXT)
913 current->startproject += end - start;
918 // Used by other editing commands so don't optimize
919 // This is separate from paste_silence, since it has to wrok also on empty tracks/beyond end of track
920 Edit *Edits::create_and_insert_edit(int64_t start, int64_t end)
922 Edit *new_edit = insert_new_edit(start);
923 new_edit->length = end - start;
924 for(Edit *current = new_edit->next; current; current = NEXT)
926 current->startproject += end - start;
931 Edit* Edits::shift(int64_t position, int64_t difference)
933 Edit *new_edit = split_edit(position);
935 for(Edit *current = first;
939 if(current->startproject >= position)
941 current->shift(difference);
948 void Edits::shift_keyframes_recursive(int64_t position, int64_t length)
950 track->shift_keyframes(position, length, 0);
953 void Edits::shift_effects_recursive(int64_t position, int64_t length)
955 track->shift_effects(position, length, 0);