4 * Copyright (C) 2008-2017 Adam Williams <broadcast at earthling dot net>
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.
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.
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
25 #include "automation.h"
26 #include "bcsignals.h"
33 #include "edlsession.h"
37 #include "filesystem.h"
38 #include "localsession.h"
40 #include "strategies.inc"
42 #include "transition.h"
43 #include "transportque.inc"
47 Edits::Edits(EDL *edl, Track *track)
59 void Edits::equivalent_output(Edits *edits, int64_t *result)
61 // For the case of plugin sets, a new plugin set may be created with
62 // plugins only starting after 0. We only want to restart brender at
63 // the first plugin in this case.
64 for(Edit *current = first, *that_current = edits->first;
65 current || that_current;
67 that_current = that_current->next)
69 //printf("Edits::equivalent_output 1 %d\n", *result);
70 if(!current && that_current)
72 int64_t position1 = length();
73 int64_t position2 = that_current->startproject;
74 if(*result < 0 || *result > MIN(position1, position2))
75 *result = MIN(position1, position2);
79 if(current && !that_current)
81 int64_t position1 = edits->length();
82 int64_t position2 = current->startproject;
83 if(*result < 0 || *result > MIN(position1, position2))
84 *result = MIN(position1, position2);
89 //printf("Edits::equivalent_output 2 %d\n", *result);
90 current->equivalent_output(that_current, result);
91 //printf("Edits::equivalent_output 3 %d\n", *result);
96 void Edits::copy_from(Edits *edits)
98 while(last) delete last;
99 for(Edit *current = edits->first; current; current = NEXT)
101 Edit *new_edit = append(create_edit());
102 new_edit->copy_from(current);
107 Edits& Edits::operator=(Edits& edits)
109 printf("Edits::operator= 1\n");
115 void Edits::insert_asset(Asset *asset, EDL *nested_edl,
116 int64_t length, int64_t position, int track_number)
118 Edit *new_edit = insert_new_edit(position);
120 new_edit->nested_edl = nested_edl;
121 new_edit->asset = asset;
122 new_edit->startsource = 0;
123 new_edit->startproject = position;
124 new_edit->length = length;
128 if(track->data_type == TRACK_AUDIO)
129 new_edit->channel = track_number % nested_edl->session->audio_channels;
131 new_edit->channel = 0;
134 if(asset && !nested_edl)
136 if(asset->audio_data)
137 new_edit->channel = track_number % asset->channels;
139 if(asset->video_data)
140 new_edit->channel = track_number % asset->layers;
143 //printf("Edits::insert_asset %d %d\n", new_edit->channel, new_edit->length);
144 for(Edit *current = new_edit->next; current; current = NEXT)
146 current->startproject += length;
150 void Edits::insert_edits(Edits *source_edits,
155 //int64_t clipboard_end = position + min_length;
156 // Length pasted so far
157 int64_t source_len = 0;
159 // Fill region between end of edit table and beginning of pasted segment
160 // with silence. Can't call from insert_new_edit because it's recursive.
161 if(position > length())
163 paste_silence(length(), position);
167 for(Edit *source_edit = source_edits->first;
169 source_edit = source_edit->next)
171 EDL *dest_nested_edl = 0;
172 if(source_edit->nested_edl)
173 dest_nested_edl = edl->nested_edls.get_nested(source_edit->nested_edl);
176 Asset *dest_asset = 0;
177 if(source_edit->asset)
178 dest_asset = edl->assets->update(source_edit->asset);
179 // Open destination area
180 Edit *dest_edit = insert_new_edit(position + source_edit->startproject);
182 dest_edit->clone_from(source_edit);
183 dest_edit->asset = dest_asset;
184 dest_edit->nested_edl = dest_nested_edl;
185 dest_edit->startproject = position + source_edit->startproject;
189 // Shift keyframes in source edit to their position in the
190 // destination edit for plugin case
191 if(edit_autos) dest_edit->shift_keyframes(position);
193 // Shift following edits and keyframes in following edits by length
194 // in current source edit.
195 for(Edit *future_edit = dest_edit->next;
197 future_edit = future_edit->next)
199 future_edit->startproject += dest_edit->length;
200 future_edit->shift_keyframes(dest_edit->length);
203 source_len += source_edit->length;
209 // Fill remaining clipboard length with silence
210 if(source_len < min_length)
212 //printf("Edits::insert_edits %d\n", __LINE__);
213 paste_silence(position + source_len, position + min_length);
219 // Can't paste silence in here because it's used by paste_silence.
220 Edit* Edits::insert_new_edit(int64_t position)
222 //printf("Edits::insert_new_edit 1\n");
223 Edit *current = split_edit(position);
225 //printf("Edits::insert_new_edit 1\n");
226 Edit *new_edit = create_edit();
227 if( current ) current = PREVIOUS;
228 //printf("Edits::insert_new_edit 1\n");
229 insert_after(current, new_edit);
230 new_edit->startproject = position;
231 //printf("Edits::insert_new_edit 2\n");
235 Edit* Edits::split_edit(int64_t position)
237 // Get edit containing position
238 Edit *edit = editof(position, PLAY_FORWARD, 0);
240 // Split would have created a 0 length
241 // if(edit->startproject == position) return edit;
242 // Create anyway so the return value comes before position
244 Edit *new_edit = create_edit();
245 insert_after(edit, new_edit);
246 new_edit->clone_from(edit);
247 new_edit->length = new_edit->startproject + new_edit->length - position;
248 edit->length = position - edit->startproject;
249 if( !new_edit->length || edit->silence() )
250 new_edit->hard_left = new_edit->hard_right = 0;
251 else if( !edit->length )
252 edit->hard_left = edit->hard_right = 0;
254 new_edit->hard_right = edit->hard_right;
255 new_edit->hard_left = edit->hard_right = 0;
257 new_edit->startproject = position;
258 new_edit->startsource += edit->length;
260 // Decide what to do with the transition
261 if(edit->length && edit->transition) {
262 delete new_edit->transition;
263 new_edit->transition = 0;
266 if(edit->transition && edit->transition->length > edit->length)
267 edit->transition->length = edit->length;
268 if(new_edit->transition && new_edit->transition->length > new_edit->length)
269 new_edit->transition->length = new_edit->length;
273 int Edits::save(FileXML *xml, const char *output_path)
275 copy(0, length(), xml, output_path);
279 void Edits::resample(double old_rate, double new_rate)
281 for(Edit *current = first; current; current = NEXT)
283 current->startproject = Units::to_int64((double)current->startproject /
286 if(PREVIOUS) PREVIOUS->length = current->startproject - PREVIOUS->startproject;
287 current->startsource = Units::to_int64((double)current->startsource /
290 if(!NEXT) current->length = Units::to_int64((double)current->length /
293 if(current->transition)
295 current->transition->length = Units::to_int64(
296 (double)current->transition->length /
300 current->resample(old_rate, new_rate);
304 int Edits::is_glitch(Edit *edit)
306 if( track->data_type != TRACK_AUDIO ) return 0;
307 int64_t threshold = edl->session->frame_rate > 0 ?
308 0.5 * edl->session->sample_rate / edl->session->frame_rate : 0;
309 // audio edit shorter than .5 frames is a glitch
310 return edit->length < threshold ? 1 : 0;
313 int Edits::optimize()
318 //printf("Edits::optimize %d\n", __LINE__);
319 // Sort edits by starting point
324 for(current = first; current; current = NEXT)
326 Edit *next_edit = NEXT;
328 if(next_edit && next_edit->startproject < current->startproject)
330 swap(next_edit, current);
336 // trim edits before position 0
337 while( first && first->startproject+first->length < 0 )
339 if( first && first->startproject < 0 ) {
340 first->length += first->startproject;
341 first->startproject = 0;
344 // Insert silence between edits which aren't consecutive
345 for(current = last; current; current = current->previous)
347 if(current->previous)
349 Edit *previous_edit = current->previous;
350 if(current->startproject -
351 previous_edit->startproject -
352 previous_edit->length > 0)
354 Edit *new_edit = create_edit();
355 insert_before(current, new_edit);
356 new_edit->startproject = previous_edit->startproject + previous_edit->length;
357 new_edit->length = current->startproject -
358 previous_edit->startproject -
359 previous_edit->length;
363 if(current->startproject > 0)
365 Edit *new_edit = create_edit();
366 insert_before(current, new_edit);
367 new_edit->length = current->startproject;
375 // delete 0 length edits
376 for( current = first; !result && current; ) {
377 Edit* prev = current->previous, *next = current->next;
378 if( current->length == 0 ) {
379 if( next && current->transition && !next->transition) {
380 next->transition = current->transition;
381 next->transition->edit = next;
382 current->transition = 0;
384 if( !current->silence() ) {
385 if( current->hard_left && next && !next->silence() )
387 if( current->hard_right && prev && !prev->silence())
388 prev->hard_right = 1;
397 // merge same files or transitions, and deglitch
398 if( !result && track->data_type != TRACK_SUBTITLE ) {
400 if( current && !current->hard_right &&
401 current->next && !current->next->hard_left &&
402 is_glitch(current) ) {
403 // if the first edit is a glitch, change it to silence
405 current->nested_edl = 0;
408 for( ; current && (next_edit=current->next); current=NEXT ) {
409 // both edges are not hard edges
410 if( current->hard_right || next_edit->hard_left )
412 // next edit is a glitch
413 if( is_glitch(next_edit) )
415 // both edits are silence & not a plugin
416 if( !current->is_plugin() && current->silence() &&
417 !next_edit->is_plugin() && next_edit->silence() )
419 // source channels are identical & assets are identical
420 if( !result && current->channel == next_edit->channel &&
421 current->asset == next_edit->asset &&
422 current->nested_edl == next_edit->nested_edl ) {
423 // and stop and start in the same frame
424 int64_t current_end = current->startsource + current->length;
425 int64_t next_start = next_edit->startsource;
426 if( current_end == next_start ||
427 EQUIV(edl->frame_align(track->from_units(current_end), 1),
428 edl->frame_align(track->from_units(next_start), 1)) )
433 int64_t current_start = current->startproject;
434 int64_t next_end = next_edit->startproject + next_edit->length;
435 current->length = next_end - current_start;
436 current->hard_right = next_edit->hard_right;
442 if( last && last->silence() &&
443 !last->transition && !last->hard_left && !last->hard_right ) {
453 // ===================================== file operations
455 void Edits::load(FileXML *file, int track_offset)
457 int64_t startproject = 0;
459 while( last ) delete last;
461 while( !file->read_tag() ) {
462 //printf("Edits::load 1 %s\n", file->tag.get_title());
463 if(!strcmp(file->tag.get_title(), "EDIT")) {
464 load_edit(file, startproject, track_offset);
466 else if(!strcmp(file->tag.get_title(), "/EDITS"))
474 int Edits::load_edit(FileXML *file, int64_t &startproject, int track_offset)
476 Edit* current = append_new_edit();
477 current->load_properties(file, startproject);
479 startproject += current->length;
481 while( !file->read_tag() ) {
482 if(file->tag.title_is("NESTED_EDL")) {
483 char path[BCTEXTLEN];
485 file->tag.get_property("SRC", path);
486 //printf("Edits::load_edit %d path=%s\n", __LINE__, path);
488 current->nested_edl = edl->nested_edls.load(path);
490 // printf("Edits::load_edit %d nested_edl->path=%s\n",
491 // __LINE__, current->nested_edl->path);
493 else if(file->tag.title_is("FILE")) {
494 char filename[BCTEXTLEN];
496 file->tag.get_property("SRC", filename);
498 if(filename[0] != 0) {
499 char directory[BCTEXTLEN], edl_directory[BCTEXTLEN];
501 fs.set_current_dir("");
502 fs.extract_dir(directory, filename);
503 if(!strlen(directory)) {
504 fs.extract_dir(edl_directory, file->filename);
505 fs.join_names(directory, edl_directory, filename);
506 strcpy(filename, directory);
508 current->asset = edl->assets->get_asset(filename);
513 //printf("Edits::load_edit 5\n");
515 else if(file->tag.title_is("TRANSITION")) {
516 current->transition = new Transition(edl, current, "",
517 track->to_units(edl->session->default_transition_length, 1));
518 current->transition->load_xml(file);
520 else if(file->tag.title_is("/EDIT"))
524 //printf("Edits::load_edit %d\n", __LINE__);
526 //printf("Edits::load_edit %d\n", __LINE__);
530 // ============================================= accounting
532 int64_t Edits::length()
534 return last ? last->startproject + last->length : 0;
539 Edit* Edits::editof(int64_t position, int direction, int use_nudge)
542 if(use_nudge && track) position += track->nudge;
544 if(direction == PLAY_FORWARD) {
545 for(current = last; current; current = PREVIOUS) {
546 if(current->startproject <= position && current->startproject + current->length > position)
551 if(direction == PLAY_REVERSE) {
552 for(current = first; current; current = NEXT) {
553 if(current->startproject < position && current->startproject + current->length >= position)
558 return 0; // return 0 on failure
561 Edit* Edits::get_playable_edit(int64_t position, int use_nudge)
564 if(track && use_nudge) position += track->nudge;
566 // Get the current edit
567 for(current = first; current; current = NEXT) {
568 if(current->startproject <= position &&
569 current->startproject + current->length > position)
573 // Get the edit's asset
574 // TODO: descend into nested EDLs
580 return current; // return 0 on failure
583 // ================================================ editing
587 int Edits::copy(int64_t start, int64_t end, FileXML *file, const char *output_path)
591 file->tag.set_title("EDITS");
593 file->append_newline();
595 for(current_edit = first; current_edit; current_edit = current_edit->next)
597 current_edit->copy(start, end, file, output_path);
600 file->tag.set_title("/EDITS");
602 file->append_newline();
608 void Edits::clear(int64_t start, int64_t end)
610 if( start >= end ) return;
612 Edit* edit1 = editof(start, PLAY_FORWARD, 0);
613 Edit* edit2 = editof(end, PLAY_FORWARD, 0);
616 if(end == start) return; // nothing selected
617 if(!edit1 && !edit2) return; // nothing selected
620 if(!edit2) { // edit2 beyond end of track
622 end = this->length();
625 if( edit1 && edit2 && edit1 != edit2)
627 // in different edits
629 //printf("Edits::clear 3.5 %d %d %d %d\n", edit1->startproject, edit1->length, edit2->startproject, edit2->length);
630 edit1->length = start - edit1->startproject;
631 edit2->length -= end - edit2->startproject;
632 edit2->startsource += end - edit2->startproject;
633 edit2->startproject += end - edit2->startproject;
636 for(current_edit = edit1->next; current_edit && current_edit != edit2;) {
637 Edit* next = current_edit->next;
638 remove(current_edit);
642 for(current_edit = edit2; current_edit; current_edit = current_edit->next) {
643 current_edit->startproject -= end - start;
647 // in same edit. paste_edit depends on this
649 current_edit = split_edit(start);
651 current_edit->length -= end - start;
652 current_edit->startsource += end - start;
654 while( (current_edit=current_edit->next) != 0 ) {
655 current_edit->startproject -= end - start;
663 // Used by edit handle and plugin handle movement but plugin handle movement
664 // can only effect other plugins.
665 void Edits::clear_recursive(int64_t start, int64_t end,
666 int edit_edits, int edit_labels, int edit_plugins, int edit_autos,
669 //printf("Edits::clear_recursive 1\n");
670 track->clear(start, end,
671 edit_edits, edit_labels, edit_plugins, edit_autos, trim_edits);
675 int Edits::clear_handle(double start, double end,
676 int edit_plugins, int edit_autos, double &distance)
680 distance = 0.0; // if nothing is found, distance is 0!
681 for(current_edit = first;
682 current_edit && current_edit->next;
683 current_edit = current_edit->next) {
687 if(current_edit->asset && current_edit->next->asset) {
689 if(current_edit->asset->equivalent(*current_edit->next->asset, 0, 0, edl)) {
691 // Got two consecutive edits in same source
692 if(edl->equivalent(track->from_units(current_edit->next->startproject),
695 int length = -current_edit->length;
696 current_edit->length = current_edit->next->startsource - current_edit->startsource;
697 length += current_edit->length;
699 // Lengthen automation
701 track->automation->paste_silence(current_edit->next->startproject,
702 current_edit->next->startproject + length);
706 track->shift_effects(current_edit->next->startproject,
707 length, edit_autos, 0);
709 for(current_edit = current_edit->next; current_edit; current_edit = current_edit->next)
711 current_edit->startproject += length;
714 distance = track->from_units(length);
725 int Edits::modify_handles(double oldposition, double newposition, int currentend,
726 int edit_mode, int edit_edits, int edit_labels, int edit_plugins, int edit_autos,
727 Edits *trim_edits, int group_id)
731 Edit *left = 0, *right = 0;
733 double start = DBL_MAX, end = DBL_MIN;
734 for( Edit *edit=first; edit; edit=edit->next ) {
735 if( edit->group_id != group_id ) continue;
736 double edit_start = edit->track->from_units(edit->startproject);
737 if( edit_start < start ) { start = edit_start; left = edit; }
738 double edit_end = edit->track->from_units(edit->startproject+edit->length);
739 if( edit_end > end ) { end = edit_end; right = edit; }
743 //printf("Edits::modify_handles 1 %d %f %f\n", currentend, newposition, oldposition);
744 if(currentend == 0) {
746 for(current_edit = first; current_edit && !result;) {
747 if( group_id > 0 ? current_edit == left :
748 edl->equivalent(track->from_units(current_edit->startproject),
750 // edit matches selection
751 //printf("Edits::modify_handles 3 %f %f\n", newposition, oldposition);
752 double delta = newposition - oldposition;
753 oldposition = track->from_units(current_edit->startproject);
754 if( group_id > 0 ) newposition = oldposition + delta;
755 current_edit->shift_start(edit_mode,
756 track->to_units(newposition, 0), track->to_units(oldposition, 0),
757 edit_labels, edit_autos, edit_plugins, trim_edits);
761 if(!result) current_edit = current_edit->next;
765 // right handle selected
766 for(current_edit = first; current_edit && !result;) {
767 if( group_id > 0 ? current_edit == right :
768 edl->equivalent(track->from_units(current_edit->startproject) +
769 track->from_units(current_edit->length), oldposition) ) {
770 double delta = newposition - oldposition;
771 oldposition = track->from_units(current_edit->startproject) +
772 track->from_units(current_edit->length);
773 if( group_id > 0 ) newposition = oldposition + delta;
776 current_edit->shift_end(edit_mode,
777 track->to_units(newposition, 0), track->to_units(oldposition, 0),
778 edit_labels, edit_autos, edit_plugins, trim_edits);
781 if(!result) current_edit = current_edit->next;
782 //printf("Edits::modify_handle 8\n");
790 void Edits::paste_silence(int64_t start, int64_t end)
792 Edit *new_edit = editof(start, PLAY_FORWARD, 0);
793 if (!new_edit) return;
795 if( !new_edit->silence() || new_edit->hard_right ) {
796 new_edit = insert_new_edit(start);
797 new_edit->length = end - start;
800 new_edit->length += end - start;
802 for(Edit *current = new_edit->next; current; current = NEXT) {
803 current->startproject += end - start;
809 Edit *Edits::create_silence(int64_t start, int64_t end)
811 Edit *new_edit = insert_new_edit(start);
812 new_edit->length = end - start;
813 for(Edit *current = new_edit->next; current; current = NEXT) {
814 current->startproject += end - start;
819 Edit* Edits::shift(int64_t position, int64_t difference)
821 Edit *new_edit = split_edit(position);
823 for(Edit *current = first; current; current = NEXT) {
824 if(current->startproject >= position) {
825 current->shift(difference);
832 void Edits::shift_keyframes_recursive(int64_t position, int64_t length)
834 track->shift_keyframes(position, length);
837 void Edits::shift_effects_recursive(int64_t position, int64_t length, int edit_autos)
839 track->shift_effects(position, length, edit_autos, 0);
842 double Edits::early_timecode()
845 for( Edit *edit=first; edit; edit=edit->next ) {
846 Asset *asset = edit->asset;
847 if( !asset ) continue;
848 if( asset->timecode < -1 )
849 asset->timecode = FFMPEG::get_timecode(asset->path,
850 track->data_type, edit->channel,
851 edl->session->frame_rate);
852 if( asset->timecode < 0 ) continue;
853 if( result < 0 || result > asset->timecode )
854 result = asset->timecode;
859 void Edits::align_timecodes(double offset)
861 for( Edit *edit=first, *next=0; edit; edit=next ) {
863 if( edit->silence() ) delete edit;
865 for( Edit *edit=first, *next=0; edit; edit=next ) {
867 Asset *asset = edit->asset;
868 if( !asset && asset->timecode < 0 ) continue;
869 double position = asset->timecode - offset;
870 edit->startproject = track->to_units(position, 1) + edit->startsource;
875 for( Edit *edit=first, *next=0; edit; edit=next ) {
877 if( !next || next->startproject >= edit->startproject ) continue;
883 int64_t startproject = 0;
884 for( Edit *edit=first, *next=0; edit; edit=next ) {
885 if( (next = edit->next) != 0 ) {
886 int64_t length = next->startproject - startproject;
887 if( length > edit->length ) edit->length = length;
889 int64_t length = edit->startproject - startproject;
891 Edit *new_edit = create_edit();
892 insert_before(edit, new_edit);
893 new_edit->startproject = startproject;
894 new_edit->length = length;
895 startproject = edit->startproject;
897 startproject += edit->length;
901 void Edits::update_idxbl_length(int id, int64_t du)
903 for( Edit *edit=first; edit; edit=edit->next ) {
904 Indexable *idxbl = edit->asset ? (Indexable *)edit->asset :
905 edit->nested_edl ? (Indexable *)edit->nested_edl : 0;
906 if( !idxbl || idxbl->id != id ) continue;
908 if( edit->length > 0 && edit->next ) {
909 int64_t next_start = edit->next->startproject;
910 int64_t edit_end = edit->startproject + edit->length;
911 if( edit_end > next_start )
912 edit->length = next_start - edit->startproject;
914 if( edit->length < 0 ) edit->length = 0;