73e7706d3452125bf03a92165eed4174a5373ae3
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / tracks.C
1 /*
2  * CINELERRA
3  * Copyright (C) 2010 Adam Williams <broadcast at earthling dot net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include "atrack.h"
22 #include "automation.h"
23 #include "clip.h"
24 #include "bchash.h"
25 #include "edit.h"
26 #include "edits.h"
27 #include "edl.h"
28 #include "edlsession.h"
29 #include "file.h"
30 #include "filexml.h"
31 #include "intauto.h"
32 #include "intautos.h"
33 #include "localsession.h"
34 #include "module.h"
35 #include "panauto.h"
36 #include "panautos.h"
37 #include "patchbay.h"
38 #include "plugin.h"
39 #include "mainsession.h"
40 #include "strack.h"
41 #include "theme.h"
42 #include "track.h"
43 #include "trackcanvas.h"
44 #include "tracks.h"
45 #include "transportque.inc"
46 #include "vtrack.h"
47 #include <string.h>
48
49 Tracks::Tracks(EDL *edl)
50  : List<Track>()
51 {
52         this->edl = edl;
53 }
54
55 Tracks::Tracks()
56  : List<Track>()
57 {
58 }
59
60
61 Tracks::~Tracks()
62 {
63         delete_all_tracks();
64 }
65
66
67
68
69
70 void Tracks::equivalent_output(Tracks *tracks, double *result)
71 {
72         if(total_playable_vtracks() != tracks->total_playable_vtracks())
73         {
74                 *result = 0;
75         }
76         else
77         {
78                 Track *current = first;
79                 Track *that_current = tracks->first;
80                 while(current || that_current)
81                 {
82 // Get next video track
83                         while(current && current->data_type != TRACK_VIDEO)
84                                 current = NEXT;
85
86                         while(that_current && that_current->data_type != TRACK_VIDEO)
87                                 that_current = that_current->next;
88
89 // One doesn't exist but the other does
90                         if((!current && that_current) ||
91                                 (current && !that_current))
92                         {
93                                 *result = 0;
94                                 break;
95                         }
96                         else
97 // Both exist
98                         if(current && that_current)
99                         {
100                                 current->equivalent_output(that_current, result);
101                                 current = NEXT;
102                                 that_current = that_current->next;
103                         }
104                 }
105         }
106 }
107
108
109 void Tracks::clear_selected_edits()
110 {
111         for( Track *track=first; track; track=track->next ) {
112                 for( Edit *edit=track->edits->first; edit; edit=edit->next )
113                         edit->is_selected = 0;
114         }
115 }
116
117 void Tracks::get_selected_edits(ArrayList<Edit*> *drag_edits)
118 {
119         drag_edits->remove_all();
120         for( Track *track=first; track; track=track->next ) {
121                 if( !track->is_armed() ) continue;
122                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
123                         if( !edit->is_selected ) continue;
124                         drag_edits->append(edit);
125                 }
126         }
127 }
128
129 void Tracks::select_edits(double start, double end)
130 {
131         for( Track *track=first; track; track=track->next ) {
132                 if( !track->is_armed() ) continue;
133                 int64_t start_pos = track->to_units(start, 0);
134                 int64_t end_pos = track->to_units(end, 0);
135                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
136                         if( start_pos >= edit->startproject+edit->length ) continue;
137                         if( edit->startproject >= end_pos ) continue;
138                         edit->is_selected = 1;
139                 }
140         }
141 }
142
143 void Tracks::get_automation_extents(float *min,
144         float *max,
145         double start,
146         double end,
147         int autogrouptype)
148 {
149         *min = 0;
150         *max = 0;
151         int coords_undefined = 1;
152         for(Track *current = first; current; current = NEXT)
153         {
154                 if(current->is_armed())
155                 {
156                         current->automation->get_extents(min,
157                                 max,
158                                 &coords_undefined,
159                                 current->to_units(start, 0),
160                                 current->to_units(end, 1),
161                                 autogrouptype);
162                 }
163         }
164 }
165
166
167 void Tracks::copy_from(Tracks *tracks)
168 {
169         Track *new_track = 0;
170
171         delete_all_tracks();
172         int solo_track_id = tracks->edl->local_session->solo_track_id;
173
174         for(Track *current = tracks->first; current; current = NEXT)
175         {
176                 switch(current->data_type)
177                 {
178                 case TRACK_AUDIO:
179                         new_track = add_audio_track(0, 0);
180                         break;
181                 case TRACK_VIDEO:
182                         new_track = add_video_track(0, 0);
183                         break;
184                 case TRACK_SUBTITLE:
185                         new_track = add_subttl_track(0, 0);
186                         break;
187                 default:
188                         continue;
189                 }
190                 new_track->copy_from(current);
191
192                 if( current->get_id() == solo_track_id )
193                         edl->local_session->solo_track_id = new_track->get_id();
194         }
195 }
196
197 Tracks& Tracks::operator=(Tracks &tracks)
198 {
199 printf("Tracks::operator= 1\n");
200         copy_from(&tracks);
201         return *this;
202 }
203
204 int Tracks::load(FileXML *xml,
205         int &track_offset,
206         uint32_t load_flags)
207 {
208 // add the appropriate type of track
209         char string[BCTEXTLEN];
210         Track *track = 0;
211         string[0] = 0;
212
213         xml->tag.get_property("TYPE", string);
214
215         if((load_flags & LOAD_ALL) == LOAD_ALL ||
216                 (load_flags & LOAD_EDITS)) {
217                 if(!strcmp(string, "VIDEO")) {
218                         track = add_video_track(0, 0);
219                 }
220                 else if(!strcmp(string, "SUBTTL")) {
221                         track = add_subttl_track(0, 0);
222                 }
223                 else {
224                         track = add_audio_track(0, 0);    // default to audio
225                 }
226         }
227         else {
228                 track = get_item_number(track_offset++);
229         }
230
231 // load it
232         if( track ) track->load(xml, track_offset, load_flags);
233
234         return 0;
235 }
236
237 Track* Tracks::add_audio_track(int above, Track *dst_track)
238 {
239         ATrack* new_track = new ATrack(edl, this);
240         if(!dst_track)
241         {
242                 dst_track = (above ? first : last);
243         }
244
245         if(above)
246         {
247                 insert_before(dst_track, (Track*)new_track);
248         }
249         else
250         {
251                 insert_after(dst_track, (Track*)new_track);
252 // Shift effects referenced below the destination track
253         }
254
255 // Shift effects referenced below the new track
256         for(Track *track = last;
257                 track && track != new_track;
258                 track = track->previous)
259         {
260                 change_modules(number_of(track) - 1, number_of(track), 0);
261         }
262
263
264         new_track->create_objects();
265         new_track->set_default_title();
266
267         int current_pan = 0;
268         for(Track *current = first;
269                 current != (Track*)new_track;
270                 current = NEXT)
271         {
272                 if(current->data_type == TRACK_AUDIO) current_pan++;
273                 if(current_pan >= edl->session->audio_channels) current_pan = 0;
274         }
275
276
277
278         PanAuto* pan_auto =
279                 (PanAuto*)new_track->automation->autos[AUTOMATION_PAN]->default_auto;
280         pan_auto->values[current_pan] = 1.0;
281
282         BC_Pan::calculate_stick_position(edl->session->audio_channels,
283                 edl->session->achannel_positions,
284                 pan_auto->values,
285                 MAX_PAN,
286                 PAN_RADIUS,
287                 pan_auto->handle_x,
288                 pan_auto->handle_y);
289         return new_track;
290 }
291
292 Track* Tracks::add_video_track(int above, Track *dst_track)
293 {
294         VTrack* new_track = new VTrack(edl, this);
295         if(!dst_track)
296                 dst_track = (above ? first : last);
297         if(above)
298                 insert_before(dst_track, (Track*)new_track);
299         else
300                 insert_after(dst_track, (Track*)new_track);
301
302         for(Track *track = last; track && track != new_track; track = track->previous)
303                 change_modules(number_of(track) - 1, number_of(track), 0);
304
305         new_track->create_objects();
306         new_track->set_default_title();
307         return new_track;
308 }
309
310
311 Track* Tracks::add_subttl_track(int above, Track *dst_track)
312 {
313         STrack* new_track = new STrack(edl, this);
314         if(!dst_track)
315                 dst_track = (above ? first : last);
316
317         if(above)
318                 insert_before(dst_track, (Track*)new_track);
319         else
320                 insert_after(dst_track, (Track*)new_track);
321
322         for(Track *track = last; track && track != new_track; track = track->previous)
323                 change_modules(number_of(track) - 1, number_of(track), 0);
324
325         new_track->create_objects();
326         new_track->set_default_title();
327 //      new_track->paste_silence(0,total_length(),0);
328         return new_track;
329 }
330
331
332 int Tracks::delete_track(Track *track, int gang)
333 {
334         if( !track ) return 0;
335         if( gang < 0 )
336                 gang = edl->session->gang_tracks != GANG_NONE ? 1 : 0;
337         Track *nxt = track->next;
338         if( gang ) {
339                 while( track && !track->master && track->previous )
340                         track = track->previous;
341                 while( nxt && !nxt->master )
342                         nxt = nxt->next;
343         }
344         Track *current = track;
345         int old_location = number_of(current);
346         for( Track *next_track=0; current!=nxt; current=next_track ) {
347                 next_track = current->next;
348                 detach_shared_effects(old_location);
349                 for( Track *curr=current; curr; curr=curr->next ) {
350 // Shift effects referencing effects below the deleted track
351                         change_modules(number_of(curr), number_of(curr)-1, 0);
352                 }
353                 delete current;
354         }
355         return 0;
356 }
357
358 int Tracks::detach_shared_effects(int module)
359 {
360         for( Track *current=first; current; current=NEXT ) {
361                 current->detach_shared_effects(module);
362         }
363         return 0;
364
365 int Tracks::detach_ganged_effects(Plugin *plugin)
366 {
367         if( edl->session->gang_tracks == GANG_NONE ) return 1;
368         for( Track *current=first; current; current=NEXT ) {
369                 if( current == plugin->track ) continue;
370                 if( !current->armed_gang(plugin->track) ) continue;
371                 current->detach_ganged_effects(plugin);
372         }
373         return 0;
374 }
375
376 int Tracks::total_of(int type)
377 {
378         int result = 0;
379
380         for(Track *current = first; current; current = NEXT)
381         {
382                 long unit_start = current->to_units(edl->local_session->get_selectionstart(1), 0);
383                 Auto *mute_keyframe = 0;
384                 current->automation->autos[AUTOMATION_MUTE]->
385                         get_prev_auto(unit_start, PLAY_FORWARD, mute_keyframe);
386                 IntAuto *mute_auto = (IntAuto *)mute_keyframe;
387
388                 result +=
389                         (current->play && type == PLAY) ||
390                         (current->is_armed() && type == RECORD) ||
391                         (current->is_ganged() && type == GANG) ||
392                         (current->draw && type == DRAW) ||
393                         (mute_auto->value && type == MUTE) ||
394                         (current->expand_view && type == EXPAND);
395         }
396         return result;
397 }
398
399 int Tracks::recordable_audio_tracks()
400 {
401         int result = 0;
402         for(Track *current = first; current; current = NEXT)
403                 if(current->data_type == TRACK_AUDIO && current->is_armed()) result++;
404         return result;
405 }
406
407 int Tracks::recordable_video_tracks()
408 {
409         int result = 0;
410         for(Track *current = first; current; current = NEXT)
411         {
412                 if(current->data_type == TRACK_VIDEO && current->is_armed()) result++;
413         }
414         return result;
415 }
416
417
418 int Tracks::playable_audio_tracks()
419 {
420         int result = 0;
421
422         for(Track *current = first; current; current = NEXT)
423         {
424                 if(current->data_type == TRACK_AUDIO && current->play)
425                 {
426                         result++;
427                 }
428         }
429
430         return result;
431 }
432
433 int Tracks::playable_video_tracks()
434 {
435         int result = 0;
436
437         for(Track *current = first; current; current = NEXT)
438         {
439                 if(current->data_type == TRACK_VIDEO && current->play)
440                 {
441                         result++;
442                 }
443         }
444         return result;
445 }
446
447 int Tracks::total_audio_tracks()
448 {
449         int result = 0;
450         for(Track *current = first; current; current = NEXT)
451                 if(current->data_type == TRACK_AUDIO) result++;
452         return result;
453 }
454
455 int Tracks::total_video_tracks()
456 {
457         int result = 0;
458         for(Track *current = first; current; current = NEXT)
459                 if(current->data_type == TRACK_VIDEO) result++;
460         return result;
461 }
462
463 double Tracks::total_playable_length()
464 {
465         double total = 0;
466         for(Track *current = first; current; current = NEXT)
467         {
468                 if( current->play )
469                 {
470                         double length = current->get_length();
471                         if(length > total) total = length;
472                 }
473         }
474         return total;
475 }
476
477 double Tracks::total_recordable_length()
478 {
479         double total = -1;
480         for(Track *current = first; current; current = NEXT)
481         {
482                 if(current->is_armed())
483                 {
484                         double length = current->get_length();
485                         if(length > total) total = length;
486                 }
487         }
488         return total;
489 }
490
491 double Tracks::total_length()
492 {
493         double total = 0;
494         for(Track *current = first; current; current = NEXT)
495         {
496                 double length = current->get_length();
497                 if(length > total) total = length;
498         }
499         return total;
500 }
501
502 double Tracks::total_audio_length()
503 {
504         double total = 0;
505         for(Track *current = first; current; current = NEXT)
506         {
507                 if(current->data_type == TRACK_AUDIO &&
508                         current->get_length() > total) total = current->get_length();
509         }
510         return total;
511 }
512
513 double Tracks::total_video_length()
514 {
515         double total = 0;
516         for(Track *current = first; current; current = NEXT)
517         {
518                 if(current->data_type == TRACK_VIDEO &&
519                         current->get_length() > total) total = current->get_length();
520         }
521         return total;
522 }
523
524 double Tracks::total_length_framealigned(double fps)
525 {
526         if (total_audio_tracks() && total_video_tracks())
527                 return MIN(floor(total_audio_length() * fps), floor(total_video_length() * fps)) / fps;
528
529         if (total_audio_tracks())
530                 return floor(total_audio_length() * fps) / fps;
531
532         if (total_video_tracks())
533                 return floor(total_video_length() * fps) / fps;
534
535         return 0;
536 }
537
538 void Tracks::translate_fauto_xy(int fauto, float dx, float dy, int all)
539 {
540         Track *track = first;
541         for( ; track; track=track->next ) {
542                 if( !all && !track->is_armed() ) continue;
543                 if( track->data_type != TRACK_VIDEO ) continue;
544                 ((VTrack*)track)->translate(fauto, dx, dy, all);
545         }
546 }
547
548 void Tracks::translate_projector(float dx, float dy, int all)
549 {
550         translate_fauto_xy(AUTOMATION_PROJECTOR_X, dx, dy, all);
551 }
552
553 void Tracks::translate_camera(float dx, float dy, int all)
554 {
555         translate_fauto_xy(AUTOMATION_CAMERA_X, dx, dy, all);
556 }
557
558 void Tracks::crop_resize(float x, float y, float z)
559 {
560         float ctr_x = edl->session->output_w / 2.;
561         float ctr_y = edl->session->output_h / 2.;
562         Track *track = first;
563         for( ; track; track=track->next ) {
564                 if( !track->is_armed() ) continue;
565                 if( track->data_type != TRACK_VIDEO ) continue;
566                 float px, py, pz;
567                 track->get_projector(px, py, pz);
568                 float old_x = px + ctr_x;
569                 float old_y = py + ctr_y;
570                 float nx = (old_x - x) * z;
571                 float ny = (old_y - y) * z;
572                 track->set_projector(nx, ny, pz * z);
573         }
574 }
575
576 void Tracks::crop_shrink(float x, float y, float z)
577 {
578         float ctr_x = edl->session->output_w / 2.;
579         float ctr_y = edl->session->output_h / 2.;
580         Track *track = first;
581         for( ; track; track=track->next ) {
582                 if( !track->is_armed() ) continue;
583                 if( track->data_type != TRACK_VIDEO ) continue;
584                 float cx, cy, cz, px, py, pz;
585                 track->get_camera(cx, cy, cz);
586                 track->get_projector(px, py, pz);
587                 float dx = x - (px + ctr_x);
588                 float dy = y - (py + ctr_y);
589                 cz *= pz;
590                 cx += dx / cz;  cy += dy / cz;
591                 track->set_camera(cx, cy, cz * z);
592                 px += dx;  py += dy;
593                 track->set_projector(px, py, 1 / z);
594         }
595 }
596
597 void Tracks::update_y_pixels(Theme *theme)
598 {
599 //      int y = -edl->local_session->track_start;
600         int y = 0;
601         for(Track *current = first; current; current = NEXT)
602         {
603 //printf("Tracks::update_y_pixels %d\n", y);
604                 current->y_pixel = y;
605                 if( current->is_hidden() ) continue;
606                 y += current->vertical_span(theme);
607         }
608 }
609
610 int Tracks::dump(FILE *fp)
611 {
612         for(Track* current = first; current; current = NEXT)
613         {
614                 fprintf(fp,"  Track: %p\n", current);
615                 current->dump(fp);
616                 fprintf(fp,"\n");
617         }
618         return 0;
619 }
620
621 void Tracks::select_all(int type,
622                 int value)
623 {
624         for(Track* current = first; current; current = NEXT)
625         {
626                 double position = edl->local_session->get_selectionstart(1);
627
628                 if(type == PLAY) current->play = value;
629                 if(type == RECORD) current->armed = value;
630                 if(type == GANG) current->ganged = value;
631                 if(type == DRAW) current->draw = value;
632
633                 if(type == MUTE)
634                 {
635                         ((IntAuto*)current->automation->autos[AUTOMATION_MUTE]->get_auto_for_editing(position))->value = value;
636                 }
637
638                 if(type == EXPAND) current->expand_view = value;
639         }
640 }
641
642 // ===================================== file operations
643
644 int Tracks::popup_transition(int cursor_x, int cursor_y)
645 {
646         int result = 0;
647         for(Track* current = first; current && !result; current = NEXT)
648         {
649                 result = current->popup_transition(cursor_x, cursor_y);
650         }
651         return result;
652 }
653
654
655 int Tracks::change_channels(int oldchannels, int newchannels)
656 {
657         for(Track *current = first; current; current = NEXT)
658         { current->change_channels(oldchannels, newchannels); }
659         return 0;
660 }
661
662
663
664 int Tracks::totalpixels()
665 {
666         int result = 0;
667         for(Track* current = first; current; current = NEXT)
668         {
669                 result += current->data_h;
670         }
671         return result;
672 }
673
674 int Tracks::number_of(Track *track)
675 {
676         int i = 0;
677         for(Track *current = first; current && current != track; current = NEXT)
678         {
679                 i++;
680         }
681         return i;
682 }
683
684 Track* Tracks::number(int number)
685 {
686         Track *current;
687         int i = 0;
688         for(current = first; current && i < number; current = NEXT)
689         {
690                 i++;
691         }
692         return current;
693 }
694
695 Track* Tracks::get_track_by_id(int id)
696 {
697         Track *track = edl->tracks->first;
698         while( track && track->get_id() != id ) track = track->next;
699         return track;
700 }
701
702 int Tracks::total_playable_vtracks()
703 {
704         int result = 0;
705         for(Track *current = first; current; current = NEXT)
706         {
707                 if(current->data_type == TRACK_VIDEO && current->play) result++;
708         }
709         return result;
710 }
711
712 Plugin *Tracks::plugin_exists(int plugin_id)
713 {
714         if( plugin_id < 0 ) return 0;
715         Plugin *plugin = 0;
716         for( Track *track=first; !plugin && track; track=track->next ) {
717                 plugin = track->plugin_exists(plugin_id);
718         }
719         return plugin;
720 }
721
722 int Tracks::track_exists(Track *track)
723 {
724         for(Track *current = first; current; current = NEXT)
725         {
726                 if(current == track) return 1;
727         }
728         return 0;
729 }
730
731 int Tracks::new_group(int id)
732 {
733         int count = 0;
734         for( Track *track=first; track; track=track->next ) {
735                 if( !track->is_armed() ) continue;
736                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
737                         if( edit->group_id > 0 ) continue;
738                         if( !edit->is_selected ) continue;
739                         edit->group_id = id;
740                         ++count;
741                 }
742         }
743         return count;
744 }
745
746 int Tracks::set_group_selected(int id, int v)
747 {
748         int count = 0;
749         int gang = edl->session->gang_tracks != GANG_NONE ? 1 : 0;
750         for( Track *track=first; track; track=track->next ) {
751                 if( track->is_hidden() ) continue;
752                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
753                         if( edit->group_id != id ) continue;
754                         if( v < 0 ) v = !edit->is_selected ? 1 : 0;
755                         edit->select_affected_edits(v, gang);
756                         ++count;
757                 }
758         }
759         return count;
760 }
761
762 int Tracks::del_group(int id)
763 {
764         int count = 0;
765         for( Track *track=first; track; track=track->next ) {
766                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
767                         if( edit->group_id != id ) continue;
768                         edit->is_selected = 1;
769                         edit->group_id = 0;
770                         ++count;
771                 }
772         }
773         return count;
774 }
775
776 Track *Tracks::get(int idx, int data_type)
777 {
778         for(Track *current = first; current; current = NEXT)
779         {
780                 if( current->data_type != data_type ) continue;
781                 if( --idx < 0 ) return current;
782         }
783         return 0;
784 }
785
786 void Tracks::move_tracks(Track *src, Track *dst, int n)
787 {
788         if( src == dst ) return;
789         while( --n >= 0 && src ) {
790                 Track *nxt = src->next;
791                 change_modules(number_of(src), total(), 0);
792                 for( Track *track=nxt; track; track=track->next )
793                         change_modules(number_of(track), number_of(track)-1, 0);
794                 remove_pointer(src);
795                 int ndst = dst ? number_of(dst) : total();
796                 insert_before(dst, src);
797                 for( Track *track=last; track && track!=src; track=track->previous ) 
798                         change_modules(number_of(track)-1, number_of(track), 0);
799                 change_modules(total(), ndst, 0);
800                 src = nxt;
801         }
802 }
803
804 double Tracks::align_timecodes()
805 {
806         double offset = -1;
807         for( Track *track=first; track; track=track->next ) {
808                 if( !track->is_armed() ) continue;
809                 double early_offset = track->edits->early_timecode();
810                 if( offset < 0 || offset > early_offset )
811                         offset = early_offset;
812         }
813         if( offset >= 0 ) {
814                 for( Track *track=first; track; track=track->next ) {
815                         if( !track->is_armed() ) continue;
816                         track->edits->align_timecodes(offset);
817                 }
818         }
819         return offset;
820 }
821
822 void Tracks::update_idxbl_length(int id, double dt)
823 {
824         for( Track *track=first; track; track=track->next ) {
825                 if( !track->is_armed() ) continue;
826                 int64_t du = track->to_units(dt,0);
827                 track->edits->update_idxbl_length(id, du);
828                 track->optimize();
829         }
830 }
831
832 void Tracks::create_keyframes(double position, int mask, int mode)
833 {
834         for( Track *track=first; track; track=track->next ) {
835                 if( !track->is_armed() ) continue;
836                 track->create_keyframes(position, mask, mode);
837         }
838 }
839