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);
153 file->tag.set_title("/NESTED_EDL");
155 file->append_newline();
160 //printf("Edit::copy 6 %s\n", asset->path);
161 char stored_path[BCTEXTLEN];
162 char asset_directory[BCTEXTLEN];
163 char output_directory[BCTEXTLEN];
166 //printf("Edit::copy %d %s\n", __LINE__, asset->path);
167 fs.extract_dir(asset_directory, asset->path);
168 //printf("Edit::copy %d %s\n", __LINE__, asset->path);
171 fs.extract_dir(output_directory, output_path);
173 output_directory[0] = 0;
174 //printf("Edit::copy %s, %s %s, %s\n", asset->path, asset_directory, output_path, output_directory);
176 if(output_path && !strcmp(asset_directory, output_directory))
177 fs.extract_name(stored_path, asset->path);
179 strcpy(stored_path, asset->path);
181 file->tag.set_title("FILE");
182 file->tag.set_property("SRC", stored_path);
184 file->tag.set_title("/FILE");
190 transition->save_xml(file);
193 //printf("Edit::copy 7\n");
194 file->tag.set_title("/EDIT");
196 file->append_newline();
197 //printf("Edit::copy 8\n");
199 //printf("Edit::copy 9\n");
206 //printf("Edit::copy 10\n");
211 int64_t Edit::get_source_end(int64_t default_)
216 void Edit::insert_transition(char *title)
218 //printf("Edit::insert_transition this=%p title=%p title=%s\n", this, title, title);
220 transition = new Transition(edl, this, title,
221 track->to_units(edl->session->default_transition_length, 1));
224 void Edit::detach_transition()
226 if(transition) delete transition;
232 return asset || nested_edl ? 0 : 1;
236 void Edit::copy_from(Edit *edit)
238 this->nested_edl = edl->nested_edls->get_copy(edit->nested_edl);
239 this->asset = edl->assets->update(edit->asset);
240 this->startsource = edit->startsource;
241 this->startproject = edit->startproject;
242 this->length = edit->length;
243 strcpy (this->user_title, edit->user_title);
247 if(!transition) transition = new Transition(edl,
249 edit->transition->title,
250 edit->transition->length);
251 *this->transition = *edit->transition;
253 this->channel = edit->channel;
256 void Edit::equivalent_output(Edit *edit, int64_t *result)
258 // End of edit changed
259 if(startproject + length != edit->startproject + edit->length)
261 int64_t new_length = MIN(startproject + length,
262 edit->startproject + edit->length);
263 if(*result < 0 || new_length < *result)
264 *result = new_length;
268 // Different nested EDLs
269 (edit->nested_edl && !nested_edl) ||
270 (!edit->nested_edl && nested_edl) ||
272 (edit->asset == 0 && asset != 0) ||
273 (edit->asset != 0 && asset == 0) ||
274 // different transitions
275 (edit->transition == 0 && transition != 0) ||
276 (edit->transition != 0 && transition == 0) ||
278 (startproject != edit->startproject) ||
279 (startsource != edit->startsource) ||
280 // Transition changed
281 (transition && edit->transition &&
282 !transition->identical(edit->transition)) ||
284 (asset && edit->asset &&
285 !asset->equivalent(*edit->asset, 1, 1)) ||
286 // Nested EDL changed
287 (nested_edl && edit->nested_edl &&
288 strcmp(nested_edl->path, edit->nested_edl->path))
291 // Start of edit changed
292 if(*result < 0 || startproject < *result) *result = startproject;
297 Edit& Edit::operator=(Edit& edit)
299 //printf("Edit::operator= called\n");
304 void Edit::synchronize_params(Edit *edit)
310 // Comparison for ResourcePixmap drawing
311 int Edit::identical(Edit &edit)
313 int result = (this->nested_edl == edit.nested_edl &&
314 this->asset == edit.asset &&
315 this->startsource == edit.startsource &&
316 this->startproject == edit.startproject &&
317 this->length == edit.length &&
318 this->transition == edit.transition &&
319 this->channel == edit.channel);
323 int Edit::operator==(Edit &edit)
325 return identical(edit);
328 double Edit::frames_per_picon()
330 return Units::round(picon_w()) / frame_w();
333 double Edit::frame_w()
335 return track->from_units(1) *
336 edl->session->sample_rate /
337 edl->local_session->zoom_sample;
340 double Edit::picon_w()
347 else if(nested_edl) {
348 w = nested_edl->session->output_w;
349 h = nested_edl->session->output_h;
351 return w>0 && h>0 ? ((double)edl->local_session->zoom_track*w)/h : 0;
356 return edl->local_session->zoom_track;
360 int Edit::dump(FILE *fp)
362 fprintf(fp," EDIT %p\n", this); fflush(fp);
363 fprintf(fp," nested_edl=%p %s asset=%p %s\n",
365 nested_edl ? nested_edl->path : "",
367 asset ? asset->path : "");
369 fprintf(fp," channel %d\n", channel);
372 fprintf(fp," TRANSITION %p\n", transition);
373 transition->dump(fp);
375 fprintf(fp," startsource %jd startproject %jd length %jd\n",
376 startsource, startproject, length); fflush(fp);
380 int Edit::load_properties(FileXML *file, int64_t &startproject)
382 startsource = file->tag.get_property("STARTSOURCE", (int64_t)0);
383 length = file->tag.get_property("LENGTH", (int64_t)0);
385 file->tag.get_property("USER_TITLE", user_title);
386 this->startproject = startproject;
387 load_properties_derived(file);
391 void Edit::shift(int64_t difference)
393 //printf("Edit::shift 1 %p %jd %jd\n", this, startproject, difference);
394 startproject += difference;
395 //printf("Edit::shift 2 %jd %jd\n", startproject, difference);
398 int Edit::shift_start_in(int edit_mode,
407 int64_t cut_length = newposition - oldposition;
408 int64_t end_previous_source, end_source;
410 if(edit_mode == MOVE_ALL_EDITS)
412 if(cut_length < length)
414 edits->clear_recursive(oldposition,
424 edits->clear_recursive(oldposition,
425 startproject + length,
434 if(edit_mode == MOVE_ONE_EDIT)
436 // Paste silence and cut
437 //printf("Edit::shift_start_in 1\n");
440 Edit *new_edit = edits->create_edit();
441 new_edit->startproject = this->startproject;
442 new_edit->length = 0;
443 edits->insert_before(this,
446 //printf("Edit::shift_start_in 2 %p\n", previous);
448 end_previous_source = previous->get_source_end(previous->startsource + previous->length + cut_length);
449 if(end_previous_source > 0 &&
450 previous->startsource + previous->length + cut_length > end_previous_source)
451 cut_length = end_previous_source - previous->startsource - previous->length;
453 if(cut_length < length)
455 startproject += cut_length;
456 startsource += cut_length;
457 length -= cut_length;
458 previous->length += cut_length;
459 //printf("Edit::shift_start_in 2\n");
462 { // Clear entire edit
464 previous->length += cut_length;
465 for(Edit* current_edit = this; current_edit; current_edit = current_edit->next)
467 current_edit->startproject += cut_length;
469 edits->clear_recursive(oldposition + cut_length,
470 startproject + cut_length,
477 //printf("Edit::shift_start_in 3\n");
480 if(edit_mode == MOVE_NO_EDITS)
482 end_source = get_source_end(startsource + length + cut_length);
483 if(end_source > 0 && startsource + length + cut_length > end_source)
484 cut_length = end_source - startsource - length;
486 startsource += cut_length;
491 int Edit::shift_start_out(int edit_mode,
500 int64_t cut_length = oldposition - newposition;
503 if(asset || nested_edl)
505 int64_t end_source = get_source_end(1);
507 //printf("Edit::shift_start_out 1 %jd %jd\n", startsource, cut_length);
508 if(end_source > 0 && startsource < cut_length)
510 cut_length = startsource;
514 if(edit_mode == MOVE_ALL_EDITS)
516 //printf("Edit::shift_start_out 10 %jd\n", cut_length);
517 startsource -= cut_length;
518 length += cut_length;
521 edits->shift_keyframes_recursive(startproject,
524 edits->shift_effects_recursive(startproject,
528 for(Edit* current_edit = next; current_edit; current_edit = current_edit->next)
530 current_edit->startproject += cut_length;
534 if(edit_mode == MOVE_ONE_EDIT)
538 if(cut_length < previous->length)
539 { // Cut into previous edit
540 previous->length -= cut_length;
541 startproject -= cut_length;
542 startsource -= cut_length;
543 length += cut_length;
544 printf("Edit::shift_start_out 2\n");
547 { // Clear entire previous edit
548 cut_length = previous->length;
549 previous->length = 0;
550 length += cut_length;
551 startsource -= cut_length;
552 startproject -= cut_length;
557 if(edit_mode == MOVE_NO_EDITS)
559 startsource -= cut_length;
562 // Fix infinite length files
563 if(startsource < 0) startsource = 0;
567 int Edit::shift_end_in(int edit_mode,
576 int64_t cut_length = oldposition - newposition;
578 if(edit_mode == MOVE_ALL_EDITS)
580 //printf("Edit::shift_end_in 1\n");
581 if(newposition > startproject)
582 { // clear partial edit
583 //printf("Edit::shift_end_in %p %p\n", track->edits, edits);
584 edits->clear_recursive(newposition,
593 { // clear entire edit
594 edits->clear_recursive(startproject,
604 if(edit_mode == MOVE_ONE_EDIT)
610 int64_t end_source = next->get_source_end(1);
612 if(end_source > 0 && next->startsource - cut_length < 0)
614 cut_length = next->startsource;
618 if(cut_length < length)
620 length -= cut_length;
621 next->startproject -= cut_length;
622 next->startsource -= cut_length;
623 next->length += cut_length;
624 //printf("Edit::shift_end_in 2 %d\n", cut_length);
629 next->length += cut_length;
630 next->startsource -= cut_length;
631 next->startproject -= cut_length;
632 length -= cut_length;
637 if(cut_length < length)
639 length -= cut_length;
644 edits->clear_recursive(startproject,
655 // Does nothing for plugins
656 if(edit_mode == MOVE_NO_EDITS)
658 //printf("Edit::shift_end_in 3\n");
659 int64_t end_source = get_source_end(1);
660 if(end_source > 0 && startsource < cut_length)
662 cut_length = startsource;
664 startsource -= cut_length;
669 int Edit::shift_end_out(int edit_mode,
678 int64_t cut_length = newposition - oldposition;
679 int64_t endsource = get_source_end(startsource + length + cut_length);
681 // check end of edit against end of source file
682 if(endsource > 0 && startsource + length + cut_length > endsource)
683 cut_length = endsource - startsource - length;
685 //printf("Edit::shift_end_out 1 %jd %d %d %d\n", oldposition, newposition, this->length, cut_length);
686 if(edit_mode == MOVE_ALL_EDITS)
689 this->length += cut_length;
691 // Effects are shifted in length extension
693 edits->shift_effects_recursive(oldposition /* startproject */,
697 edits->shift_keyframes_recursive(oldposition /* startproject */,
700 for(Edit* current_edit = next; current_edit; current_edit = current_edit->next)
702 current_edit->startproject += cut_length;
706 if(edit_mode == MOVE_ONE_EDIT)
710 if(cut_length < next->length)
712 length += cut_length;
713 next->startproject += cut_length;
714 next->startsource += cut_length;
715 next->length -= cut_length;
716 //printf("Edit::shift_end_out 2 %d\n", cut_length);
720 cut_length = next->length;
722 length += cut_length;
727 length += cut_length;
731 if(edit_mode == MOVE_NO_EDITS)
733 startsource += cut_length;
766 int Edit::popup_transition(float view_start, float zoom_units, int cursor_x, int cursor_y)
768 int64_t left, right, left_unit, right_unit;
769 if(!transition) return 0;
770 get_handle_parameters(left, right, left_unit, right_unit, view_start, zoom_units);
772 if(cursor_x > left && cursor_x < right)
774 // transition->popup_transition(cursor_x, cursor_y);
780 int Edit::select_handle(float view_start, float zoom_units, int cursor_x, int cursor_y, int64_t &selection)
782 int64_t left, right, left_unit, right_unit;
783 get_handle_parameters(left, right, left_unit, right_unit, view_start, zoom_units);
785 int64_t pixel1, pixel2;
787 pixel2 = pixel1 + 10;
790 // cursor_x is faked in acanvas
791 if(cursor_x >= pixel1 && cursor_x <= pixel2)
793 selection = left_unit;
794 return 1; // left handle
797 //int64_t endproject = startproject + length;
799 pixel1 = pixel2 - 10;
802 if(cursor_x >= pixel1 && cursor_x <= pixel2)
804 selection = right_unit;
805 return 2; // right handle