4 * Copyright (C) 2008 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
24 #include "bcsignals.h"
29 #include "edlsession.h"
31 #include "filesystem.h"
32 #include "localsession.h"
34 #include "mainsession.h"
35 #include "nestededls.h"
36 #include "trackcanvas.h"
38 #include "transition.h"
47 Edit::Edit(EDL *edl, Track *track)
52 if(track) this->edits = track->edits;
56 Edit::Edit(EDL *edl, Edits *edits)
61 if(edits) this->track = edits->track;
67 //printf("Edit::~Edit 1\n");
68 if(transition) delete transition;
69 //printf("Edit::~Edit 2\n");
88 Indexable* Edit::get_source()
90 if(asset) return asset;
91 if(nested_edl) return nested_edl;
95 int Edit::copy(int64_t start,
98 const char *output_path)
101 //printf("Edit::copy 1\n");
103 int64_t endproject = startproject + length;
106 if((startproject >= start && startproject <= end) || // startproject in range
107 (endproject <= end && endproject >= start) || // endproject in range
108 (startproject <= start && endproject >= end)) // range in project
111 int64_t startproject_in_selection = startproject; // start of edit in selection in project
112 int64_t startsource_in_selection = startsource; // start of source in selection in source
113 //int64_t endsource_in_selection = startsource + length; // end of source in selection
114 int64_t length_in_selection = length; // length of edit in selection
115 //printf("Edit::copy 2\n");
117 if(startproject < start)
118 { // start is after start of edit in project
119 int64_t length_difference = start - startproject;
121 startsource_in_selection += length_difference;
122 startproject_in_selection += length_difference;
123 length_in_selection -= length_difference;
125 //printf("Edit::copy 3\n");
128 { // end is before end of edit in project
129 length_in_selection = end - startproject_in_selection;
132 //printf("Edit::copy 4\n");
133 if(file) // only if not counting
135 file->tag.set_title("EDIT");
136 file->tag.set_property("STARTSOURCE", startsource_in_selection);
137 file->tag.set_property("CHANNEL", (int64_t)channel);
138 file->tag.set_property("LENGTH", length_in_selection);
139 if(user_title[0]) file->tag.set_property("USER_TITLE", user_title);
140 //printf("Edit::copy 5\n");
142 copy_properties_derived(file, length_in_selection);
145 // file->append_newline();
146 //printf("Edit::copy 6\n");
150 file->tag.set_title("NESTED_EDL");
151 file->tag.set_property("SRC", nested_edl->path);
157 //printf("Edit::copy 6 %s\n", asset->path);
158 char stored_path[BCTEXTLEN];
159 char asset_directory[BCTEXTLEN];
160 char output_directory[BCTEXTLEN];
163 //printf("Edit::copy %d %s\n", __LINE__, asset->path);
164 fs.extract_dir(asset_directory, asset->path);
165 //printf("Edit::copy %d %s\n", __LINE__, asset->path);
168 fs.extract_dir(output_directory, output_path);
170 output_directory[0] = 0;
171 //printf("Edit::copy %s, %s %s, %s\n", asset->path, asset_directory, output_path, output_directory);
173 if(output_path && !strcmp(asset_directory, output_directory))
174 fs.extract_name(stored_path, asset->path);
176 strcpy(stored_path, asset->path);
178 file->tag.set_title("FILE");
179 file->tag.set_property("SRC", stored_path);
181 file->tag.set_title("/FILE");
187 transition->save_xml(file);
190 //printf("Edit::copy 7\n");
191 file->tag.set_title("/EDIT");
193 file->append_newline();
194 //printf("Edit::copy 8\n");
196 //printf("Edit::copy 9\n");
203 //printf("Edit::copy 10\n");
208 int64_t Edit::get_source_end(int64_t default_)
213 void Edit::insert_transition(char *title)
215 //printf("Edit::insert_transition this=%p title=%p title=%s\n", this, title, title);
217 transition = new Transition(edl, this, title,
218 track->to_units(edl->session->default_transition_length, 1));
221 void Edit::detach_transition()
223 if(transition) delete transition;
229 return asset || nested_edl ? 0 : 1;
233 void Edit::copy_from(Edit *edit)
235 this->nested_edl = edl->nested_edls->get_copy(edit->nested_edl);
236 this->asset = edl->assets->update(edit->asset);
237 this->startsource = edit->startsource;
238 this->startproject = edit->startproject;
239 this->length = edit->length;
240 strcpy (this->user_title, edit->user_title);
244 if(!transition) transition = new Transition(edl,
246 edit->transition->title,
247 edit->transition->length);
248 *this->transition = *edit->transition;
250 this->channel = edit->channel;
253 void Edit::equivalent_output(Edit *edit, int64_t *result)
255 // End of edit changed
256 if(startproject + length != edit->startproject + edit->length)
258 int64_t new_length = MIN(startproject + length,
259 edit->startproject + edit->length);
260 if(*result < 0 || new_length < *result)
261 *result = new_length;
265 // Different nested EDLs
266 (edit->nested_edl && !nested_edl) ||
267 (!edit->nested_edl && nested_edl) ||
269 (edit->asset == 0 && asset != 0) ||
270 (edit->asset != 0 && asset == 0) ||
271 // different transitions
272 (edit->transition == 0 && transition != 0) ||
273 (edit->transition != 0 && transition == 0) ||
275 (startproject != edit->startproject) ||
276 (startsource != edit->startsource) ||
277 // Transition changed
278 (transition && edit->transition &&
279 !transition->identical(edit->transition)) ||
281 (asset && edit->asset &&
282 !asset->equivalent(*edit->asset, 1, 1)) ||
283 // Nested EDL changed
284 (nested_edl && edit->nested_edl &&
285 strcmp(nested_edl->path, edit->nested_edl->path))
288 // Start of edit changed
289 if(*result < 0 || startproject < *result) *result = startproject;
294 Edit& Edit::operator=(Edit& edit)
296 //printf("Edit::operator= called\n");
301 void Edit::synchronize_params(Edit *edit)
307 // Comparison for ResourcePixmap drawing
308 int Edit::identical(Edit &edit)
310 int result = (this->nested_edl == edit.nested_edl &&
311 this->asset == edit.asset &&
312 this->startsource == edit.startsource &&
313 this->startproject == edit.startproject &&
314 this->length == edit.length &&
315 this->transition == edit.transition &&
316 this->channel == edit.channel);
320 int Edit::operator==(Edit &edit)
322 return identical(edit);
325 double Edit::frames_per_picon()
327 return Units::round(picon_w()) / frame_w();
330 double Edit::frame_w()
332 return track->from_units(1) *
333 edl->session->sample_rate /
334 edl->local_session->zoom_sample;
337 double Edit::picon_w()
344 else if(nested_edl) {
345 w = nested_edl->session->output_w;
346 h = nested_edl->session->output_h;
348 return w>0 && h>0 ? ((double)edl->local_session->zoom_track*w)/h : 0;
353 return edl->local_session->zoom_track;
357 int Edit::dump(FILE *fp)
359 fprintf(fp," EDIT %p\n", this); fflush(fp);
360 fprintf(fp," nested_edl=%p %s asset=%p %s\n",
362 nested_edl ? nested_edl->path : "",
364 asset ? asset->path : "");
366 fprintf(fp," channel %d\n", channel);
369 fprintf(fp," TRANSITION %p\n", transition);
370 transition->dump(fp);
372 fprintf(fp," startsource %jd startproject %jd length %jd\n",
373 startsource, startproject, length); fflush(fp);
377 int Edit::load_properties(FileXML *file, int64_t &startproject)
379 startsource = file->tag.get_property("STARTSOURCE", (int64_t)0);
380 length = file->tag.get_property("LENGTH", (int64_t)0);
382 file->tag.get_property("USER_TITLE", user_title);
383 this->startproject = startproject;
384 load_properties_derived(file);
388 void Edit::shift(int64_t difference)
390 //printf("Edit::shift 1 %p %jd %jd\n", this, startproject, difference);
391 startproject += difference;
392 //printf("Edit::shift 2 %jd %jd\n", startproject, difference);
395 int Edit::shift_start_in(int edit_mode,
404 int64_t cut_length = newposition - oldposition;
405 int64_t end_previous_source, end_source;
407 if(edit_mode == MOVE_ALL_EDITS)
409 if(cut_length < length)
411 edits->clear_recursive(oldposition,
421 edits->clear_recursive(oldposition,
422 startproject + length,
431 if(edit_mode == MOVE_ONE_EDIT)
433 // Paste silence and cut
434 //printf("Edit::shift_start_in 1\n");
437 Edit *new_edit = edits->create_edit();
438 new_edit->startproject = this->startproject;
439 new_edit->length = 0;
440 edits->insert_before(this,
443 //printf("Edit::shift_start_in 2 %p\n", previous);
445 end_previous_source = previous->get_source_end(previous->startsource + previous->length + cut_length);
446 if(end_previous_source > 0 &&
447 previous->startsource + previous->length + cut_length > end_previous_source)
448 cut_length = end_previous_source - previous->startsource - previous->length;
450 if(cut_length < length)
452 startproject += cut_length;
453 startsource += cut_length;
454 length -= cut_length;
455 previous->length += cut_length;
456 //printf("Edit::shift_start_in 2\n");
459 { // Clear entire edit
461 previous->length += cut_length;
462 for(Edit* current_edit = this; current_edit; current_edit = current_edit->next)
464 current_edit->startproject += cut_length;
466 edits->clear_recursive(oldposition + cut_length,
467 startproject + cut_length,
474 //printf("Edit::shift_start_in 3\n");
477 if(edit_mode == MOVE_NO_EDITS)
479 end_source = get_source_end(startsource + length + cut_length);
480 if(end_source > 0 && startsource + length + cut_length > end_source)
481 cut_length = end_source - startsource - length;
483 startsource += cut_length;
488 int Edit::shift_start_out(int edit_mode,
497 int64_t cut_length = oldposition - newposition;
500 if(asset || nested_edl)
502 int64_t end_source = get_source_end(1);
504 //printf("Edit::shift_start_out 1 %jd %jd\n", startsource, cut_length);
505 if(end_source > 0 && startsource < cut_length)
507 cut_length = startsource;
511 if(edit_mode == MOVE_ALL_EDITS)
513 //printf("Edit::shift_start_out 10 %jd\n", cut_length);
514 startsource -= cut_length;
515 length += cut_length;
518 edits->shift_keyframes_recursive(startproject,
521 edits->shift_effects_recursive(startproject,
525 for(Edit* current_edit = next; current_edit; current_edit = current_edit->next)
527 current_edit->startproject += cut_length;
531 if(edit_mode == MOVE_ONE_EDIT)
535 if(cut_length < previous->length)
536 { // Cut into previous edit
537 previous->length -= cut_length;
538 startproject -= cut_length;
539 startsource -= cut_length;
540 length += cut_length;
541 printf("Edit::shift_start_out 2\n");
544 { // Clear entire previous edit
545 cut_length = previous->length;
546 previous->length = 0;
547 length += cut_length;
548 startsource -= cut_length;
549 startproject -= cut_length;
554 if(edit_mode == MOVE_NO_EDITS)
556 startsource -= cut_length;
559 // Fix infinite length files
560 if(startsource < 0) startsource = 0;
564 int Edit::shift_end_in(int edit_mode,
573 int64_t cut_length = oldposition - newposition;
575 if(edit_mode == MOVE_ALL_EDITS)
577 //printf("Edit::shift_end_in 1\n");
578 if(newposition > startproject)
579 { // clear partial edit
580 //printf("Edit::shift_end_in %p %p\n", track->edits, edits);
581 edits->clear_recursive(newposition,
590 { // clear entire edit
591 edits->clear_recursive(startproject,
601 if(edit_mode == MOVE_ONE_EDIT)
607 int64_t end_source = next->get_source_end(1);
609 if(end_source > 0 && next->startsource - cut_length < 0)
611 cut_length = next->startsource;
615 if(cut_length < length)
617 length -= cut_length;
618 next->startproject -= cut_length;
619 next->startsource -= cut_length;
620 next->length += cut_length;
621 //printf("Edit::shift_end_in 2 %d\n", cut_length);
626 next->length += cut_length;
627 next->startsource -= cut_length;
628 next->startproject -= cut_length;
629 length -= cut_length;
634 if(cut_length < length)
636 length -= cut_length;
641 edits->clear_recursive(startproject,
652 // Does nothing for plugins
653 if(edit_mode == MOVE_NO_EDITS)
655 //printf("Edit::shift_end_in 3\n");
656 int64_t end_source = get_source_end(1);
657 if(end_source > 0 && startsource < cut_length)
659 cut_length = startsource;
661 startsource -= cut_length;
666 int Edit::shift_end_out(int edit_mode,
675 int64_t cut_length = newposition - oldposition;
676 int64_t endsource = get_source_end(startsource + length + cut_length);
678 // check end of edit against end of source file
679 if(endsource > 0 && startsource + length + cut_length > endsource)
680 cut_length = endsource - startsource - length;
682 //printf("Edit::shift_end_out 1 %jd %d %d %d\n", oldposition, newposition, this->length, cut_length);
683 if(edit_mode == MOVE_ALL_EDITS)
686 this->length += cut_length;
688 // Effects are shifted in length extension
690 edits->shift_effects_recursive(oldposition /* startproject */,
694 edits->shift_keyframes_recursive(oldposition /* startproject */,
697 for(Edit* current_edit = next; current_edit; current_edit = current_edit->next)
699 current_edit->startproject += cut_length;
703 if(edit_mode == MOVE_ONE_EDIT)
707 if(cut_length < next->length)
709 length += cut_length;
710 next->startproject += cut_length;
711 next->startsource += cut_length;
712 next->length -= cut_length;
713 //printf("Edit::shift_end_out 2 %d\n", cut_length);
717 cut_length = next->length;
719 length += cut_length;
724 length += cut_length;
728 if(edit_mode == MOVE_NO_EDITS)
730 startsource += cut_length;
763 int Edit::popup_transition(float view_start, float zoom_units, int cursor_x, int cursor_y)
765 int64_t left, right, left_unit, right_unit;
766 if(!transition) return 0;
767 get_handle_parameters(left, right, left_unit, right_unit, view_start, zoom_units);
769 if(cursor_x > left && cursor_x < right)
771 // transition->popup_transition(cursor_x, cursor_y);
777 int Edit::select_handle(float view_start, float zoom_units, int cursor_x, int cursor_y, int64_t &selection)
779 int64_t left, right, left_unit, right_unit;
780 get_handle_parameters(left, right, left_unit, right_unit, view_start, zoom_units);
782 int64_t pixel1, pixel2;
784 pixel2 = pixel1 + 10;
787 // cursor_x is faked in acanvas
788 if(cursor_x >= pixel1 && cursor_x <= pixel2)
790 selection = left_unit;
791 return 1; // left handle
794 //int64_t endproject = startproject + length;
796 pixel1 = pixel2 - 10;
799 if(cursor_x >= pixel1 && cursor_x <= pixel2)
801 selection = right_unit;
802 return 2; // right handle