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");
90 Indexable* Edit::get_source()
92 if(asset) return asset;
93 if(nested_edl) return nested_edl;
97 int Edit::copy(int64_t start,
100 const char *output_path)
103 //printf("Edit::copy 1\n");
105 int64_t endproject = startproject + length;
108 if((startproject >= start && startproject <= end) || // startproject in range
109 (endproject <= end && endproject >= start) || // endproject in range
110 (startproject <= start && endproject >= end)) // range in project
113 int64_t startproject_in_selection = startproject; // start of edit in selection in project
114 int64_t startsource_in_selection = startsource; // start of source in selection in source
115 //int64_t endsource_in_selection = startsource + length; // end of source in selection
116 int64_t length_in_selection = length; // length of edit in selection
117 //printf("Edit::copy 2\n");
119 if(startproject < start)
120 { // start is after start of edit in project
121 int64_t length_difference = start - startproject;
123 startsource_in_selection += length_difference;
124 startproject_in_selection += length_difference;
125 length_in_selection -= length_difference;
127 //printf("Edit::copy 3\n");
130 { // end is before end of edit in project
131 length_in_selection = end - startproject_in_selection;
134 //printf("Edit::copy 4\n");
135 if(file) // only if not counting
137 file->tag.set_title("EDIT");
138 file->tag.set_property("STARTSOURCE", startsource_in_selection);
139 file->tag.set_property("CHANNEL", (int64_t)channel);
140 file->tag.set_property("LENGTH", length_in_selection);
141 file->tag.set_property("HARD_LEFT", hard_left);
142 file->tag.set_property("HARD_RIGHT", hard_right);
143 if(user_title[0]) file->tag.set_property("USER_TITLE", user_title);
144 //printf("Edit::copy 5\n");
146 copy_properties_derived(file, length_in_selection);
149 // file->append_newline();
150 //printf("Edit::copy 6\n");
154 file->tag.set_title("NESTED_EDL");
155 file->tag.set_property("SRC", nested_edl->path);
157 file->tag.set_title("/NESTED_EDL");
159 file->append_newline();
164 //printf("Edit::copy 6 %s\n", asset->path);
165 char stored_path[BCTEXTLEN];
166 char asset_directory[BCTEXTLEN];
167 char output_directory[BCTEXTLEN];
170 //printf("Edit::copy %d %s\n", __LINE__, asset->path);
171 fs.extract_dir(asset_directory, asset->path);
172 //printf("Edit::copy %d %s\n", __LINE__, asset->path);
175 fs.extract_dir(output_directory, output_path);
177 output_directory[0] = 0;
178 //printf("Edit::copy %s, %s %s, %s\n", asset->path, asset_directory, output_path, output_directory);
180 if(output_path && !strcmp(asset_directory, output_directory))
181 fs.extract_name(stored_path, asset->path);
183 strcpy(stored_path, asset->path);
185 file->tag.set_title("FILE");
186 file->tag.set_property("SRC", stored_path);
188 file->tag.set_title("/FILE");
194 transition->save_xml(file);
197 //printf("Edit::copy 7\n");
198 file->tag.set_title("/EDIT");
200 file->append_newline();
201 //printf("Edit::copy 8\n");
203 //printf("Edit::copy 9\n");
210 //printf("Edit::copy 10\n");
215 int64_t Edit::get_source_end(int64_t default_)
220 void Edit::insert_transition(char *title)
222 //printf("Edit::insert_transition this=%p title=%p title=%s\n", this, title, title);
224 transition = new Transition(edl, this, title,
225 track->to_units(edl->session->default_transition_length, 1));
228 void Edit::detach_transition()
230 if(transition) delete transition;
236 return asset || nested_edl ? 0 : 1;
240 void Edit::copy_from(Edit *edit)
242 this->nested_edl = edl->nested_edls->get_copy(edit->nested_edl);
243 this->asset = edl->assets->update(edit->asset);
244 this->startsource = edit->startsource;
245 this->startproject = edit->startproject;
246 this->length = edit->length;
247 this->hard_left = edit->hard_left;
248 this->hard_right = edit->hard_right;
249 strcpy (this->user_title, edit->user_title);
253 if(!transition) transition = new Transition(edl,
255 edit->transition->title,
256 edit->transition->length);
257 *this->transition = *edit->transition;
259 this->channel = edit->channel;
262 void Edit::equivalent_output(Edit *edit, int64_t *result)
264 // End of edit changed
265 if(startproject + length != edit->startproject + edit->length)
267 int64_t new_length = MIN(startproject + length,
268 edit->startproject + edit->length);
269 if(*result < 0 || new_length < *result)
270 *result = new_length;
274 // Different nested EDLs
275 (edit->nested_edl && !nested_edl) ||
276 (!edit->nested_edl && nested_edl) ||
278 (edit->asset == 0 && asset != 0) ||
279 (edit->asset != 0 && asset == 0) ||
280 // different transitions
281 (edit->transition == 0 && transition != 0) ||
282 (edit->transition != 0 && transition == 0) ||
284 (startproject != edit->startproject) ||
285 (startsource != edit->startsource) ||
286 // Transition changed
287 (transition && edit->transition &&
288 !transition->identical(edit->transition)) ||
290 (asset && edit->asset &&
291 !asset->equivalent(*edit->asset, 1, 1, edl)) ||
292 // Nested EDL changed
293 (nested_edl && edit->nested_edl &&
294 strcmp(nested_edl->path, edit->nested_edl->path))
297 // Start of edit changed
298 if(*result < 0 || startproject < *result) *result = startproject;
303 Edit& Edit::operator=(Edit& edit)
305 //printf("Edit::operator= called\n");
310 void Edit::synchronize_params(Edit *edit)
316 // Comparison for ResourcePixmap drawing
317 int Edit::identical(Edit &edit)
319 int result = (this->nested_edl == edit.nested_edl &&
320 this->asset == edit.asset &&
321 this->startsource == edit.startsource &&
322 this->startproject == edit.startproject &&
323 this->length == edit.length &&
324 this->hard_left == edit.hard_left &&
325 this->hard_right == edit.hard_right &&
326 this->transition == edit.transition &&
327 this->channel == edit.channel);
331 int Edit::operator==(Edit &edit)
333 return identical(edit);
336 double Edit::frames_per_picon()
338 return Units::round(picon_w()) / frame_w();
341 double Edit::frame_w()
343 return track->from_units(1) *
344 edl->session->sample_rate /
345 edl->local_session->zoom_sample;
348 double Edit::picon_w()
355 else if(nested_edl) {
356 w = nested_edl->session->output_w;
357 h = nested_edl->session->output_h;
359 return w>0 && h>0 ? ((double)edl->local_session->zoom_track*w)/h : 0;
364 return edl->local_session->zoom_track;
368 int Edit::dump(FILE *fp)
370 fprintf(fp," EDIT %p\n", this); fflush(fp);
371 fprintf(fp," nested_edl=%p %s asset=%p %s\n",
373 nested_edl ? nested_edl->path : "",
375 asset ? asset->path : "");
377 fprintf(fp," channel %d\n", channel);
380 fprintf(fp," TRANSITION %p\n", transition);
381 transition->dump(fp);
383 fprintf(fp," startsource %jd startproject %jd hard lt/rt %d/%d length %jd\n",
384 startsource, startproject, hard_left, hard_right, length); fflush(fp);
388 int Edit::load_properties(FileXML *file, int64_t &startproject)
390 startsource = file->tag.get_property("STARTSOURCE", (int64_t)0);
391 length = file->tag.get_property("LENGTH", (int64_t)0);
392 hard_left = file->tag.get_property("HARD_LEFT", (int64_t)0);
393 hard_right = file->tag.get_property("HARD_RIGHT", (int64_t)0);
395 file->tag.get_property("USER_TITLE", user_title);
396 this->startproject = startproject;
397 load_properties_derived(file);
401 void Edit::shift(int64_t difference)
403 //printf("Edit::shift 1 %p %jd %jd\n", this, startproject, difference);
404 startproject += difference;
405 //printf("Edit::shift 2 %jd %jd\n", startproject, difference);
408 int Edit::shift_start_in(int edit_mode,
417 int64_t cut_length = newposition - oldposition;
418 int64_t end_previous_source, end_source;
420 if(edit_mode == MOVE_ALL_EDITS)
422 if(cut_length < length)
424 edits->clear_recursive(oldposition,
434 edits->clear_recursive(oldposition,
435 startproject + length,
444 if(edit_mode == MOVE_ONE_EDIT)
446 // Paste silence and cut
447 //printf("Edit::shift_start_in 1\n");
450 Edit *new_edit = edits->create_edit();
451 new_edit->startproject = this->startproject;
452 new_edit->length = 0;
453 edits->insert_before(this,
456 //printf("Edit::shift_start_in 2 %p\n", previous);
458 end_previous_source = previous->get_source_end(previous->startsource + previous->length + cut_length);
459 if(end_previous_source > 0 &&
460 previous->startsource + previous->length + cut_length > end_previous_source)
461 cut_length = end_previous_source - previous->startsource - previous->length;
463 if(cut_length < length)
465 startproject += cut_length;
466 startsource += cut_length;
467 length -= cut_length;
468 previous->length += cut_length;
469 //printf("Edit::shift_start_in 2\n");
472 { // Clear entire edit
474 previous->length += cut_length;
475 for(Edit* current_edit = this; current_edit; current_edit = current_edit->next)
477 current_edit->startproject += cut_length;
479 edits->clear_recursive(oldposition + cut_length,
480 startproject + cut_length,
487 //printf("Edit::shift_start_in 3\n");
490 if(edit_mode == MOVE_NO_EDITS)
492 end_source = get_source_end(startsource + length + cut_length);
493 if(end_source > 0 && startsource + length + cut_length > end_source)
494 cut_length = end_source - startsource - length;
496 startsource += cut_length;
501 int Edit::shift_start_out(int edit_mode,
510 int64_t cut_length = oldposition - newposition;
513 if(asset || nested_edl)
515 int64_t end_source = get_source_end(1);
517 //printf("Edit::shift_start_out 1 %jd %jd\n", startsource, cut_length);
518 if(end_source > 0 && startsource < cut_length)
520 cut_length = startsource;
524 if(edit_mode == MOVE_ALL_EDITS)
526 //printf("Edit::shift_start_out 10 %jd\n", cut_length);
527 startsource -= cut_length;
528 length += cut_length;
531 edits->shift_keyframes_recursive(startproject,
534 edits->shift_effects_recursive(startproject,
538 for(Edit* current_edit = next; current_edit; current_edit = current_edit->next)
540 current_edit->startproject += cut_length;
544 if(edit_mode == MOVE_ONE_EDIT)
548 if(cut_length < previous->length)
549 { // Cut into previous edit
550 previous->length -= cut_length;
551 startproject -= cut_length;
552 startsource -= cut_length;
553 length += cut_length;
554 printf("Edit::shift_start_out 2\n");
557 { // Clear entire previous edit
558 cut_length = previous->length;
559 previous->length = 0;
560 length += cut_length;
561 startsource -= cut_length;
562 startproject -= cut_length;
567 if(edit_mode == MOVE_NO_EDITS)
569 startsource -= cut_length;
572 // Fix infinite length files
573 if(startsource < 0) startsource = 0;
577 int Edit::shift_end_in(int edit_mode,
586 int64_t cut_length = oldposition - newposition;
588 if(edit_mode == MOVE_ALL_EDITS)
590 //printf("Edit::shift_end_in 1\n");
591 if(newposition > startproject)
592 { // clear partial edit
593 //printf("Edit::shift_end_in %p %p\n", track->edits, edits);
594 edits->clear_recursive(newposition,
603 { // clear entire edit
604 edits->clear_recursive(startproject,
614 if(edit_mode == MOVE_ONE_EDIT)
620 int64_t end_source = next->get_source_end(1);
622 if(end_source > 0 && next->startsource - cut_length < 0)
624 cut_length = next->startsource;
628 if(cut_length < length)
630 length -= cut_length;
631 next->startproject -= cut_length;
632 next->startsource -= cut_length;
633 next->length += cut_length;
634 //printf("Edit::shift_end_in 2 %d\n", cut_length);
639 next->length += cut_length;
640 next->startsource -= cut_length;
641 next->startproject -= cut_length;
642 length -= cut_length;
647 if(cut_length < length)
649 length -= cut_length;
654 edits->clear_recursive(startproject,
665 // Does nothing for plugins
666 if(edit_mode == MOVE_NO_EDITS)
668 //printf("Edit::shift_end_in 3\n");
669 int64_t end_source = get_source_end(1);
670 if(end_source > 0 && startsource < cut_length)
672 cut_length = startsource;
674 startsource -= cut_length;
679 int Edit::shift_end_out(int edit_mode,
688 int64_t cut_length = newposition - oldposition;
689 int64_t endsource = get_source_end(startsource + length + cut_length);
691 // check end of edit against end of source file
692 if(endsource > 0 && startsource + length + cut_length > endsource)
693 cut_length = endsource - startsource - length;
695 //printf("Edit::shift_end_out 1 %jd %d %d %d\n", oldposition, newposition, this->length, cut_length);
696 if(edit_mode == MOVE_ALL_EDITS)
699 this->length += cut_length;
701 // Effects are shifted in length extension
703 edits->shift_effects_recursive(oldposition /* startproject */,
707 edits->shift_keyframes_recursive(oldposition /* startproject */,
710 for(Edit* current_edit = next; current_edit; current_edit = current_edit->next)
712 current_edit->startproject += cut_length;
716 if(edit_mode == MOVE_ONE_EDIT)
720 if(cut_length < next->length)
722 length += cut_length;
723 next->startproject += cut_length;
724 next->startsource += cut_length;
725 next->length -= cut_length;
726 //printf("Edit::shift_end_out %d cut_length=%d\n", __LINE__, cut_length);
730 //printf("Edit::shift_end_out %d cut_length=%d next->length=%d\n", __LINE__, cut_length, next->length);
731 cut_length = next->length;
732 next->startproject += next->length;
733 next->startsource += next->length;
735 length += cut_length;
741 length += cut_length;
745 if(edit_mode == MOVE_NO_EDITS)
747 startsource += cut_length;
780 int Edit::popup_transition(float view_start, float zoom_units, int cursor_x, int cursor_y)
782 int64_t left, right, left_unit, right_unit;
783 if(!transition) return 0;
784 get_handle_parameters(left, right, left_unit, right_unit, view_start, zoom_units);
786 if(cursor_x > left && cursor_x < right)
788 // transition->popup_transition(cursor_x, cursor_y);
794 int Edit::select_handle(float view_start, float zoom_units, int cursor_x, int cursor_y, int64_t &selection)
796 int64_t left, right, left_unit, right_unit;
797 get_handle_parameters(left, right, left_unit, right_unit, view_start, zoom_units);
799 int64_t pixel1, pixel2;
801 pixel2 = pixel1 + 10;
804 // cursor_x is faked in acanvas
805 if(cursor_x >= pixel1 && cursor_x <= pixel2)
807 selection = left_unit;
808 return 1; // left handle
811 //int64_t endproject = startproject + length;
813 pixel1 = pixel2 - 10;
816 if(cursor_x >= pixel1 && cursor_x <= pixel2)
818 selection = right_unit;
819 return 2; // right handle