a64024ada45ad77505b3b53905e823a04608a9ed
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / edits.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008-2017 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 "aedit.h"
23 #include "asset.h"
24 #include "assets.h"
25 #include "automation.h"
26 #include "bcsignals.h"
27 #include "cache.h"
28 #include "clip.h"
29 #include "clipedls.h"
30 #include "edit.h"
31 #include "edits.h"
32 #include "edl.h"
33 #include "edlsession.h"
34 #include "ffmpeg.h"
35 #include "file.h"
36 #include "filexml.h"
37 #include "filesystem.h"
38 #include "localsession.h"
39 #include "plugin.h"
40 #include "strategies.inc"
41 #include "track.h"
42 #include "transition.h"
43 #include "transportque.inc"
44
45 #include <string.h>
46
47 Edits::Edits(EDL *edl, Track *track)
48  : List<Edit>()
49 {
50         this->edl = edl;
51         this->track = track;
52 }
53
54 Edits::~Edits()
55 {
56 }
57
58
59 void Edits::equivalent_output(Edits *edits, int64_t *result)
60 {
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;
66                 current = NEXT,
67                 that_current = that_current->next)
68         {
69 //printf("Edits::equivalent_output 1 %d\n", *result);
70                 if(!current && that_current)
71                 {
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);
76                         break;
77                 }
78                 else
79                 if(current && !that_current)
80                 {
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);
85                         break;
86                 }
87                 else
88                 {
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);
92                 }
93         }
94 }
95
96 void Edits::copy_from(Edits *edits)
97 {
98         while(last) delete last;
99         for(Edit *current = edits->first; current; current = NEXT)
100         {
101                 Edit *new_edit = append(create_edit());
102                 new_edit->copy_from(current);
103         }
104 }
105
106
107 Edits& Edits::operator=(Edits& edits)
108 {
109 printf("Edits::operator= 1\n");
110         copy_from(&edits);
111         return *this;
112 }
113
114
115 void Edits::insert_asset(Asset *asset, EDL *nested_edl,
116         int64_t length, int64_t position, int track_number)
117 {
118         Edit *new_edit = insert_new_edit(position);
119
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;
125
126         if(nested_edl)
127         {
128                 if(track->data_type == TRACK_AUDIO)
129                         new_edit->channel = track_number % nested_edl->session->audio_channels;
130                 else
131                         new_edit->channel = 0;
132         }
133
134         if(asset && !nested_edl)
135         {
136                 if(asset->audio_data)
137                         new_edit->channel = track_number % asset->channels;
138                 else
139                 if(asset->video_data)
140                         new_edit->channel = track_number % asset->layers;
141         }
142
143 //printf("Edits::insert_asset %d %d\n", new_edit->channel, new_edit->length);
144         for(Edit *current = new_edit->next; current; current = NEXT)
145         {
146                 current->startproject += length;
147         }
148 }
149
150 void Edits::insert_edits(Edits *source_edits,
151         int64_t position,
152         int64_t min_length,
153         int edit_autos)
154 {
155         //int64_t clipboard_end = position + min_length;
156 // Length pasted so far
157         int64_t source_len = 0;
158
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())
162         {
163                 paste_silence(length(), position);
164         }
165
166
167         for(Edit *source_edit = source_edits->first;
168                 source_edit;
169                 source_edit = source_edit->next)
170         {
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);
174
175 // Update Assets
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);
181
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;
186
187
188
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);
192
193 // Shift following edits and keyframes in following edits by length
194 // in current source edit.
195                 for(Edit *future_edit = dest_edit->next;
196                         future_edit;
197                         future_edit = future_edit->next)
198                 {
199                         future_edit->startproject += dest_edit->length;
200                         future_edit->shift_keyframes(dest_edit->length);
201                 }
202
203                 source_len += source_edit->length;
204         }
205
206
207
208
209 // Fill remaining clipboard length with silence
210         if(source_len < min_length)
211         {
212 //printf("Edits::insert_edits %d\n", __LINE__);
213                 paste_silence(position + source_len, position + min_length);
214         }
215 }
216
217
218 // Native units
219 // Can't paste silence in here because it's used by paste_silence.
220 Edit* Edits::insert_new_edit(int64_t position)
221 {
222 //printf("Edits::insert_new_edit 1\n");
223         Edit *current = split_edit(position);
224
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");
232         return new_edit;
233 }
234
235 Edit* Edits::split_edit(int64_t position)
236 {
237 // Get edit containing position
238         Edit *edit = editof(position, PLAY_FORWARD, 0);
239         if(!edit) return 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
243
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;
253         else {
254                 new_edit->hard_right = edit->hard_right;
255                 new_edit->hard_left = edit->hard_right = 0;
256         }
257         new_edit->startproject = position;
258         new_edit->startsource += edit->length;
259
260 // Decide what to do with the transition
261         if(edit->length && edit->transition) {
262                 delete new_edit->transition;
263                 new_edit->transition = 0;
264         }
265
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;
270         return new_edit;
271 }
272
273 int Edits::save(FileXML *xml, const char *output_path)
274 {
275         copy(0, length(), xml, output_path);
276         return 0;
277 }
278
279 void Edits::resample(double old_rate, double new_rate)
280 {
281         for(Edit *current = first; current; current = NEXT)
282         {
283                 current->startproject = Units::to_int64((double)current->startproject /
284                         old_rate *
285                         new_rate);
286                 if(PREVIOUS) PREVIOUS->length = current->startproject - PREVIOUS->startproject;
287                 current->startsource = Units::to_int64((double)current->startsource /
288                         old_rate *
289                         new_rate);
290                 if(!NEXT) current->length = Units::to_int64((double)current->length /
291                         old_rate *
292                         new_rate);
293                 if(current->transition)
294                 {
295                         current->transition->length = Units::to_int64(
296                                 (double)current->transition->length /
297                                 old_rate *
298                                 new_rate);
299                 }
300                 current->resample(old_rate, new_rate);
301         }
302 }
303
304 int Edits::is_glitch(Edit *edit)
305 {
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;
311 }
312
313 int Edits::optimize()
314 {
315         int result = 1;
316         Edit *current;
317
318 //printf("Edits::optimize %d\n", __LINE__);
319 // Sort edits by starting point
320         while(result)
321         {
322                 result = 0;
323
324                 for(current = first; current; current = NEXT)
325                 {
326                         Edit *next_edit = NEXT;
327
328                         if(next_edit && next_edit->startproject < current->startproject)
329                         {
330                                 swap(next_edit, current);
331                                 result = 1;
332                         }
333                 }
334         }
335
336 // trim edits before position 0
337         while( first && first->startproject+first->length < 0 )
338                 delete first;
339         if( first && first->startproject < 0 ) {
340                 first->length += first->startproject;
341                 first->startproject = 0;
342         }
343
344 // Insert silence between edits which aren't consecutive
345         for(current = last; current; current = current->previous)
346         {
347                 if(current->previous)
348                 {
349                         Edit *previous_edit = current->previous;
350                         if(current->startproject -
351                                 previous_edit->startproject -
352                                 previous_edit->length > 0)
353                         {
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;
360                         }
361                 }
362                 else
363                 if(current->startproject > 0)
364                 {
365                         Edit *new_edit = create_edit();
366                         insert_before(current, new_edit);
367                         new_edit->length = current->startproject;
368                 }
369         }
370
371         result = 1;
372         while(result) {
373                 result = 0;
374
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;
383                                 }
384                                 if( !current->silence() ) {
385                                         if( current->hard_left && next && !next->silence() )
386                                                 next->hard_left = 1;
387                                         if( current->hard_right && prev && !prev->silence())
388                                                 prev->hard_right = 1;
389                                 }
390                                 delete current;
391                                 result = 1;
392                                 break;
393                         }
394                         current = next;
395                 }
396
397 // merge same files or transitions, and deglitch
398                 if( !result && track->data_type != TRACK_SUBTITLE ) {
399                         current = first;
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
404                                 current->asset = 0;
405                                 current->nested_edl = 0;
406                         }
407                         Edit *next_edit = 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 )
411                                         continue;
412 // next edit is a glitch
413                                 if( is_glitch(next_edit) )
414                                         break;
415 // both edits are silence & not a plugin
416                                 if( !current->is_plugin() && current->silence() &&
417                                     !next_edit->is_plugin() && next_edit->silence() )
418                                         break;
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)) )
429                                                 break;
430                                 }
431                         }
432                         if( next_edit ) {
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;
437                                 remove(next_edit);
438                                 result = 1;
439                         }
440                 }
441
442                 if( last && last->silence() &&
443                     !last->transition && !last->hard_left && !last->hard_right ) {
444                         delete last;
445                         result = 1;
446                 }
447         }
448
449         return 0;
450 }
451
452
453 // ===================================== file operations
454
455 void Edits::load(FileXML *file, int track_offset)
456 {
457         int64_t startproject = 0;
458
459         while( last ) delete last;
460
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);
465                 }
466                 else if(!strcmp(file->tag.get_title(), "/EDITS"))
467                         break;
468         }
469
470 //track->dump();
471         optimize();
472 }
473
474 int Edits::load_edit(FileXML *file, int64_t &startproject, int track_offset)
475 {
476         Edit* current = append_new_edit();
477         current->load_properties(file, startproject);
478
479         startproject += current->length;
480
481         while( !file->read_tag() ) {
482                 if(file->tag.title_is("NESTED_EDL")) {
483                         char path[BCTEXTLEN];
484                         path[0] = 0;
485                         file->tag.get_property("SRC", path);
486 //printf("Edits::load_edit %d path=%s\n", __LINE__, path);
487                         if(path[0] != 0) {
488                                 current->nested_edl = edl->nested_edls.load(path);
489                         }
490 // printf("Edits::load_edit %d nested_edl->path=%s\n",
491 // __LINE__, current->nested_edl->path);
492                 }
493                 else if(file->tag.title_is("FILE")) {
494                         char filename[BCTEXTLEN];
495                         filename[0] = 0;
496                         file->tag.get_property("SRC", filename);
497 // Extend path
498                         if(filename[0] != 0) {
499                                 char directory[BCTEXTLEN], edl_directory[BCTEXTLEN];
500                                 FileSystem fs;
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);
507                                 }
508                                 current->asset = edl->assets->get_asset(filename);
509                         }
510                         else {
511                                 current->asset = 0;
512                         }
513 //printf("Edits::load_edit 5\n");
514                 }
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);
519                 }
520                 else if(file->tag.title_is("/EDIT"))
521                         break;
522         }
523
524 //printf("Edits::load_edit %d\n", __LINE__);
525 //track->dump();
526 //printf("Edits::load_edit %d\n", __LINE__);
527         return 0;
528 }
529
530 // ============================================= accounting
531
532 int64_t Edits::length()
533 {
534         return last ? last->startproject + last->length : 0;
535 }
536
537
538
539 Edit* Edits::editof(int64_t position, int direction, int use_nudge)
540 {
541         Edit *current = 0;
542         if(use_nudge && track) position += track->nudge;
543
544         if(direction == PLAY_FORWARD) {
545                 for(current = last; current; current = PREVIOUS) {
546                         if(current->startproject <= position && current->startproject + current->length > position)
547                                 return current;
548                 }
549         }
550         else
551         if(direction == PLAY_REVERSE) {
552                 for(current = first; current; current = NEXT) {
553                         if(current->startproject < position && current->startproject + current->length >= position)
554                                 return current;
555                 }
556         }
557
558         return 0;     // return 0 on failure
559 }
560
561 Edit* Edits::get_playable_edit(int64_t position, int use_nudge)
562 {
563         Edit *current;
564         if(track && use_nudge) position += track->nudge;
565
566 // Get the current edit
567         for(current = first; current; current = NEXT) {
568                 if(current->startproject <= position &&
569                         current->startproject + current->length > position)
570                         break;
571         }
572
573 // Get the edit's asset
574 // TODO: descend into nested EDLs
575         if(current) {
576                 if(!current->asset)
577                         current = 0;
578         }
579
580         return current;     // return 0 on failure
581 }
582
583 // ================================================ editing
584
585
586
587 int Edits::copy(int64_t start, int64_t end, FileXML *file, const char *output_path)
588 {
589         Edit *current_edit;
590
591         file->tag.set_title("EDITS");
592         file->append_tag();
593         file->append_newline();
594
595         for(current_edit = first; current_edit; current_edit = current_edit->next)
596         {
597                 current_edit->copy(start, end, file, output_path);
598         }
599
600         file->tag.set_title("/EDITS");
601         file->append_tag();
602         file->append_newline();
603         return 0;
604 }
605
606
607
608 void Edits::clear(int64_t start, int64_t end)
609 {
610         if( start >= end ) return;
611
612         Edit* edit1 = editof(start, PLAY_FORWARD, 0);
613         Edit* edit2 = editof(end, PLAY_FORWARD, 0);
614         Edit* current_edit;
615
616         if(end == start) return;        // nothing selected
617         if(!edit1 && !edit2) return;       // nothing selected
618
619
620         if(!edit2) {                // edit2 beyond end of track
621                 edit2 = last;
622                 end = this->length();
623         }
624
625         if( edit1 && edit2 && edit1 != edit2)
626         {
627 // in different edits
628
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;
634
635 // delete
636                 for(current_edit = edit1->next; current_edit && current_edit != edit2;) {
637                         Edit* next = current_edit->next;
638                         remove(current_edit);
639                         current_edit = next;
640                 }
641 // shift
642                 for(current_edit = edit2; current_edit; current_edit = current_edit->next) {
643                         current_edit->startproject -= end - start;
644                 }
645         }
646         else {
647 // in same edit. paste_edit depends on this
648 // create a new edit
649                 current_edit = split_edit(start);
650                 if( current_edit ) {
651                         current_edit->length -= end - start;
652                         current_edit->startsource += end - start;
653 // shift
654                         while( (current_edit=current_edit->next) != 0 ) {
655                                 current_edit->startproject -= end - start;
656                         }
657                 }
658         }
659
660         optimize();
661 }
662
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,
667         Edits *trim_edits)
668 {
669 //printf("Edits::clear_recursive 1\n");
670         track->clear(start, end,
671                 edit_edits, edit_labels, edit_plugins, edit_autos, trim_edits);
672 }
673
674
675 int Edits::clear_handle(double start, double end,
676         int edit_plugins, int edit_autos, double &distance)
677 {
678         Edit *current_edit;
679
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) {
684
685
686
687                 if(current_edit->asset && current_edit->next->asset) {
688
689                         if(current_edit->asset->equivalent(*current_edit->next->asset, 0, 0, edl)) {
690
691 // Got two consecutive edits in same source
692                                 if(edl->equivalent(track->from_units(current_edit->next->startproject),
693                                         start)) {
694 // handle selected
695                                         int length = -current_edit->length;
696                                         current_edit->length = current_edit->next->startsource - current_edit->startsource;
697                                         length += current_edit->length;
698
699 // Lengthen automation
700                                         if(edit_autos)
701                                                 track->automation->paste_silence(current_edit->next->startproject,
702                                                         current_edit->next->startproject + length);
703
704 // Lengthen effects
705                                         if(edit_plugins)
706                                                 track->shift_effects(current_edit->next->startproject,
707                                                                 length, edit_autos, 0);
708
709                                         for(current_edit = current_edit->next; current_edit; current_edit = current_edit->next)
710                                         {
711                                                 current_edit->startproject += length;
712                                         }
713
714                                         distance = track->from_units(length);
715                                         optimize();
716                                         break;
717                                 }
718                         }
719                 }
720         }
721
722         return 0;
723 }
724
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)
728 {
729         int result = 0;
730         Edit *current_edit;
731         Edit *left = 0, *right = 0;
732         if( group_id > 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; }
740                 }
741         }
742
743 //printf("Edits::modify_handles 1 %d %f %f\n", currentend, newposition, oldposition);
744         if(currentend == 0) {
745 // left handle
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),
749                                 oldposition) ) {
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);
758                                 result = 1;
759                         }
760
761                         if(!result) current_edit = current_edit->next;
762                 }
763         }
764         else {
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;
774                                 result = 1;
775
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);
779                         }
780
781                         if(!result) current_edit = current_edit->next;
782 //printf("Edits::modify_handle 8\n");
783                 }
784         }
785
786         optimize();
787         return 0;
788 }
789
790 void Edits::paste_silence(int64_t start, int64_t end)
791 {
792         Edit *new_edit = editof(start, PLAY_FORWARD, 0);
793         if (!new_edit) return;
794
795         if( !new_edit->silence() || new_edit->hard_right ) {
796                 new_edit = insert_new_edit(start);
797                 new_edit->length = end - start;
798         }
799         else
800                 new_edit->length += end - start;
801
802         for(Edit *current = new_edit->next; current; current = NEXT) {
803                 current->startproject += end - start;
804         }
805
806         return;
807 }
808
809 Edit *Edits::create_silence(int64_t start, int64_t end)
810 {
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;
815         }
816         return new_edit;
817 }
818
819 Edit* Edits::shift(int64_t position, int64_t difference)
820 {
821         Edit *new_edit = split_edit(position);
822
823         for(Edit *current = first; current; current = NEXT) {
824                 if(current->startproject >= position) {
825                         current->shift(difference);
826                 }
827         }
828         return new_edit;
829 }
830
831
832 void Edits::shift_keyframes_recursive(int64_t position, int64_t length)
833 {
834         track->shift_keyframes(position, length);
835 }
836
837 void Edits::shift_effects_recursive(int64_t position, int64_t length, int edit_autos)
838 {
839         track->shift_effects(position, length, edit_autos, 0);
840 }
841
842 double Edits::early_timecode()
843 {
844         double result = -1;
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;
855         }
856         return result;
857 }
858
859 void Edits::align_timecodes(double offset)
860 {
861         for( Edit *edit=first, *next=0; edit; edit=next ) {
862                 next = edit->next;
863                 if( edit->silence() ) delete edit;
864         }
865         for( Edit *edit=first, *next=0; edit; edit=next ) {
866                 next = 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;
871         }
872         int result = 1;
873         while( result ) {
874                 result = 0;
875                 for( Edit *edit=first, *next=0; edit; edit=next ) {
876                         next = edit->next;
877                         if( !next || next->startproject >= edit->startproject ) continue;
878                         swap(next, edit);
879                         next = edit;
880                         result = 1;
881                 }
882         }
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;
888                 }
889                 int64_t length = edit->startproject - startproject;
890                 if( length > 0 ) {
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;
896                 }
897                 startproject += edit->length;
898         }
899 }
900
901 void Edits::update_idxbl_length(int id, int64_t du)
902 {
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;
907                 edit->length += du;
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;
913                 }
914                 if( edit->length < 0 ) edit->length = 0;
915         }
916 }
917