8cbf92cec4c927bc3a7732d7b8547e858f5bb85a
[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, int v)
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 = v > 1 ? 1 : v < 0 ? 0 : !edit->is_selected ;
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                 track = track->gang_master();
340                 while( nxt && !nxt->master )
341                         nxt = nxt->next;
342         }
343         Track *current = track;
344         int old_location = number_of(current);
345         for( Track *next_track=0; current!=nxt; current=next_track ) {
346                 next_track = current->next;
347                 detach_shared_effects(old_location);
348                 for( Track *curr=current; curr; curr=curr->next ) {
349 // Shift effects referencing effects below the deleted track
350                         change_modules(number_of(curr), number_of(curr)-1, 0);
351                 }
352                 delete current;
353         }
354         return 0;
355 }
356
357 int Tracks::detach_shared_effects(int module)
358 {
359         for( Track *current=first; current; current=NEXT ) {
360                 current->detach_shared_effects(module);
361         }
362         return 0;
363
364 int Tracks::detach_ganged_effects(Plugin *plugin)
365 {
366         if( edl->session->gang_tracks == GANG_NONE ) return 1;
367         for( Track *current=first; current; current=NEXT ) {
368                 if( current == plugin->track ) continue;
369                 if( !current->armed_gang(plugin->track) ) continue;
370                 current->detach_ganged_effects(plugin);
371         }
372         return 0;
373 }
374
375 int Tracks::total_of(int type)
376 {
377         int result = 0;
378
379         for(Track *current = first; current; current = NEXT)
380         {
381                 long unit_start = current->to_units(edl->local_session->get_selectionstart(1), 0);
382                 Auto *mute_keyframe = 0;
383                 current->automation->autos[AUTOMATION_MUTE]->
384                         get_prev_auto(unit_start, PLAY_FORWARD, mute_keyframe);
385                 IntAuto *mute_auto = (IntAuto *)mute_keyframe;
386
387                 result +=
388                         (current->play && type == PLAY) ||
389                         (current->is_armed() && type == RECORD) ||
390                         (current->is_ganged() && type == GANG) ||
391                         (current->draw && type == DRAW) ||
392                         (mute_auto->value && type == MUTE) ||
393                         (current->expand_view && type == EXPAND);
394         }
395         return result;
396 }
397
398 int Tracks::recordable_audio_tracks()
399 {
400         int result = 0;
401         for(Track *current = first; current; current = NEXT)
402                 if(current->data_type == TRACK_AUDIO && current->is_armed()) result++;
403         return result;
404 }
405
406 int Tracks::recordable_video_tracks()
407 {
408         int result = 0;
409         for(Track *current = first; current; current = NEXT)
410         {
411                 if(current->data_type == TRACK_VIDEO && current->is_armed()) result++;
412         }
413         return result;
414 }
415
416
417 int Tracks::playable_audio_tracks()
418 {
419         int result = 0;
420
421         for(Track *current = first; current; current = NEXT)
422         {
423                 if(current->data_type == TRACK_AUDIO && current->play)
424                 {
425                         result++;
426                 }
427         }
428
429         return result;
430 }
431
432 int Tracks::playable_video_tracks()
433 {
434         int result = 0;
435
436         for(Track *current = first; current; current = NEXT)
437         {
438                 if(current->data_type == TRACK_VIDEO && current->play)
439                 {
440                         result++;
441                 }
442         }
443         return result;
444 }
445
446 int Tracks::total_audio_tracks()
447 {
448         int result = 0;
449         for(Track *current = first; current; current = NEXT)
450                 if(current->data_type == TRACK_AUDIO) result++;
451         return result;
452 }
453
454 int Tracks::total_video_tracks()
455 {
456         int result = 0;
457         for(Track *current = first; current; current = NEXT)
458                 if(current->data_type == TRACK_VIDEO) result++;
459         return result;
460 }
461
462 double Tracks::total_playable_length()
463 {
464         double total = 0;
465         for(Track *current = first; current; current = NEXT)
466         {
467                 if( current->play )
468                 {
469                         double length = current->get_length();
470                         if(length > total) total = length;
471                 }
472         }
473         return total;
474 }
475
476 double Tracks::total_recordable_length()
477 {
478         double total = -1;
479         for(Track *current = first; current; current = NEXT)
480         {
481                 if(current->is_armed())
482                 {
483                         double length = current->get_length();
484                         if(length > total) total = length;
485                 }
486         }
487         return total;
488 }
489
490 double Tracks::total_length()
491 {
492         double total = 0;
493         for(Track *current = first; current; current = NEXT)
494         {
495                 double length = current->get_length();
496                 if(length > total) total = length;
497         }
498         return total;
499 }
500
501 double Tracks::total_audio_length()
502 {
503         double total = 0;
504         for(Track *current = first; current; current = NEXT)
505         {
506                 if(current->data_type == TRACK_AUDIO &&
507                         current->get_length() > total) total = current->get_length();
508         }
509         return total;
510 }
511
512 double Tracks::total_video_length()
513 {
514         double total = 0;
515         for(Track *current = first; current; current = NEXT)
516         {
517                 if(current->data_type == TRACK_VIDEO &&
518                         current->get_length() > total) total = current->get_length();
519         }
520         return total;
521 }
522
523 double Tracks::total_length_framealigned(double fps)
524 {
525         if (total_audio_tracks() && total_video_tracks())
526                 return MIN(floor(total_audio_length() * fps), floor(total_video_length() * fps)) / fps;
527
528         if (total_audio_tracks())
529                 return floor(total_audio_length() * fps) / fps;
530
531         if (total_video_tracks())
532                 return floor(total_video_length() * fps) / fps;
533
534         return 0;
535 }
536
537 void Tracks::translate_fauto_xy(int fauto, float dx, float dy, int all)
538 {
539         Track *track = first;
540         for( ; track; track=track->next ) {
541                 if( !all && !track->is_armed() ) continue;
542                 if( track->data_type != TRACK_VIDEO ) continue;
543                 ((VTrack*)track)->translate(fauto, dx, dy, all);
544         }
545 }
546
547 void Tracks::translate_projector(float dx, float dy, int all)
548 {
549         translate_fauto_xy(AUTOMATION_PROJECTOR_X, dx, dy, all);
550 }
551
552 void Tracks::translate_camera(float dx, float dy, int all)
553 {
554         translate_fauto_xy(AUTOMATION_CAMERA_X, dx, dy, all);
555 }
556
557 void Tracks::crop_resize(float x, float y, float z)
558 {
559         float ctr_x = edl->session->output_w / 2.;
560         float ctr_y = edl->session->output_h / 2.;
561         Track *track = first;
562         for( ; track; track=track->next ) {
563                 if( !track->is_armed() ) continue;
564                 if( track->data_type != TRACK_VIDEO ) continue;
565                 float px, py, pz;
566                 track->get_projector(px, py, pz);
567                 float old_x = px + ctr_x;
568                 float old_y = py + ctr_y;
569                 float nx = (old_x - x) * z;
570                 float ny = (old_y - y) * z;
571                 track->set_projector(nx, ny, pz * z);
572         }
573 }
574
575 void Tracks::crop_shrink(float x, float y, float z)
576 {
577         float ctr_x = edl->session->output_w / 2.;
578         float ctr_y = edl->session->output_h / 2.;
579         Track *track = first;
580         for( ; track; track=track->next ) {
581                 if( !track->is_armed() ) continue;
582                 if( track->data_type != TRACK_VIDEO ) continue;
583                 float cx, cy, cz, px, py, pz;
584                 track->get_camera(cx, cy, cz);
585                 track->get_projector(px, py, pz);
586                 float dx = x - (px + ctr_x);
587                 float dy = y - (py + ctr_y);
588                 cz *= pz;
589                 cx += dx / cz;  cy += dy / cz;
590                 track->set_camera(cx, cy, cz * z);
591                 px += dx;  py += dy;
592                 track->set_projector(px, py, 1 / z);
593         }
594 }
595
596 void Tracks::update_y_pixels(Theme *theme)
597 {
598 //      int y = -edl->local_session->track_start;
599         int y = 0;
600         for(Track *current = first; current; current = NEXT)
601         {
602 //printf("Tracks::update_y_pixels %d\n", y);
603                 current->y_pixel = y;
604                 if( current->is_hidden() ) continue;
605                 y += current->vertical_span(theme);
606         }
607 }
608
609 int Tracks::dump(FILE *fp)
610 {
611         for(Track* current = first; current; current = NEXT)
612         {
613                 fprintf(fp,"  Track: %p\n", current);
614                 current->dump(fp);
615                 fprintf(fp,"\n");
616         }
617         return 0;
618 }
619
620 void Tracks::select_all(int type,
621                 int value)
622 {
623         for(Track* current = first; current; current = NEXT)
624         {
625                 double position = edl->local_session->get_selectionstart(1);
626
627                 if(type == PLAY) current->play = value;
628                 if(type == RECORD) current->armed = value;
629                 if(type == GANG) current->ganged = value;
630                 if(type == DRAW) current->draw = value;
631
632                 if(type == MUTE)
633                 {
634                         ((IntAuto*)current->automation->autos[AUTOMATION_MUTE]->get_auto_for_editing(position))->value = value;
635                 }
636
637                 if(type == EXPAND) current->expand_view = value;
638         }
639 }
640
641 // ===================================== file operations
642
643 int Tracks::popup_transition(int cursor_x, int cursor_y)
644 {
645         int result = 0;
646         for(Track* current = first; current && !result; current = NEXT)
647         {
648                 result = current->popup_transition(cursor_x, cursor_y);
649         }
650         return result;
651 }
652
653
654 int Tracks::change_channels(int oldchannels, int newchannels)
655 {
656         for(Track *current = first; current; current = NEXT)
657         { current->change_channels(oldchannels, newchannels); }
658         return 0;
659 }
660
661
662
663 int Tracks::totalpixels()
664 {
665         int result = 0;
666         for(Track* current = first; current; current = NEXT)
667         {
668                 result += current->data_h;
669         }
670         return result;
671 }
672
673 int Tracks::number_of(Track *track)
674 {
675         int i = 0;
676         for(Track *current = first; current && current != track; current = NEXT)
677         {
678                 i++;
679         }
680         return i;
681 }
682
683 Track* Tracks::number(int number)
684 {
685         Track *current;
686         int i = 0;
687         for(current = first; current && i < number; current = NEXT)
688         {
689                 i++;
690         }
691         return current;
692 }
693
694 Track* Tracks::get_track_by_id(int id)
695 {
696         Track *track = edl->tracks->first;
697         while( track && track->get_id() != id ) track = track->next;
698         return track;
699 }
700
701 int Tracks::total_playable_vtracks()
702 {
703         int result = 0;
704         for(Track *current = first; current; current = NEXT)
705         {
706                 if(current->data_type == TRACK_VIDEO && current->play) result++;
707         }
708         return result;
709 }
710
711 Plugin *Tracks::plugin_exists(int plugin_id)
712 {
713         if( plugin_id < 0 ) return 0;
714         Plugin *plugin = 0;
715         for( Track *track=first; !plugin && track; track=track->next ) {
716                 plugin = track->plugin_exists(plugin_id);
717         }
718         return plugin;
719 }
720
721 int Tracks::track_exists(Track *track)
722 {
723         for(Track *current = first; current; current = NEXT)
724         {
725                 if(current == track) return 1;
726         }
727         return 0;
728 }
729
730 int Tracks::new_group(int id)
731 {
732         int count = 0;
733         for( Track *track=first; track; track=track->next ) {
734                 if( !track->is_armed() ) continue;
735                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
736                         if( edit->group_id > 0 ) continue;
737                         if( !edit->is_selected ) continue;
738                         edit->group_id = id;
739                         ++count;
740                 }
741         }
742         return count;
743 }
744
745 int Tracks::set_group_selected(int id, int v)
746 {
747         int count = 0;
748         int gang = edl->session->gang_tracks != GANG_NONE ? 1 : 0;
749         for( Track *track=first; track; track=track->next ) {
750                 if( track->is_hidden() ) continue;
751                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
752                         if( edit->group_id != id ) continue;
753                         if( v < 0 ) v = !edit->is_selected ? 1 : 0;
754                         edit->select_affected_edits(v, gang);
755                         ++count;
756                 }
757         }
758         return count;
759 }
760
761 int Tracks::del_group(int id)
762 {
763         int count = 0;
764         for( Track *track=first; track; track=track->next ) {
765                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
766                         if( edit->group_id != id ) continue;
767                         edit->is_selected = 1;
768                         edit->group_id = 0;
769                         ++count;
770                 }
771         }
772         return count;
773 }
774
775 Track *Tracks::get(int idx, int data_type)
776 {
777         for(Track *current = first; current; current = NEXT)
778         {
779                 if( current->data_type != data_type ) continue;
780                 if( --idx < 0 ) return current;
781         }
782         return 0;
783 }
784
785 void Tracks::roll_tracks(Track *src, Track *dst, int n)
786 {
787         if( src == dst ) return;
788         while( --n >= 0 && src ) {
789                 Track *nxt = src->next;
790                 change_modules(number_of(src), total(), 0);
791                 for( Track *track=nxt; track; track=track->next )
792                         change_modules(number_of(track), number_of(track)-1, 0);
793                 remove_pointer(src);
794                 int ndst = dst ? number_of(dst) : total();
795                 insert_before(dst, src);
796                 for( Track *track=last; track && track!=src; track=track->previous ) 
797                         change_modules(number_of(track)-1, number_of(track), 0);
798                 change_modules(total(), ndst, 0);
799                 src = nxt;
800         }
801 }
802
803 double Tracks::align_timecodes()
804 {
805         double offset = -1;
806         for( Track *track=first; track; track=track->next ) {
807                 if( !track->is_armed() ) continue;
808                 double early_offset = track->edits->early_timecode();
809                 if( offset < 0 || offset > early_offset )
810                         offset = early_offset;
811         }
812         if( offset >= 0 ) {
813                 for( Track *track=first; track; track=track->next ) {
814                         if( !track->is_armed() ) continue;
815                         track->edits->align_timecodes(offset);
816                 }
817         }
818         return offset;
819 }
820
821 void Tracks::update_idxbl_length(int id, double dt)
822 {
823         for( Track *track=first; track; track=track->next ) {
824                 if( !track->is_armed() ) continue;
825                 int64_t du = track->to_units(dt,0);
826                 track->edits->update_idxbl_length(id, du);
827                 track->optimize();
828         }
829 }
830
831 void Tracks::create_keyframes(double position, int mask, int mode)
832 {
833         for( Track *track=first; track; track=track->next ) {
834                 if( !track->is_armed() ) continue;
835                 track->create_keyframes(position, mask, mode);
836         }
837 }
838