Add back 2 patches for histogram and overlayframe that are working correctly and...
[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::get_automation_extents(float *min,
130         float *max,
131         double start,
132         double end,
133         int autogrouptype)
134 {
135         *min = 0;
136         *max = 0;
137         int coords_undefined = 1;
138         for(Track *current = first; current; current = NEXT)
139         {
140                 if(current->is_armed())
141                 {
142                         current->automation->get_extents(min,
143                                 max,
144                                 &coords_undefined,
145                                 current->to_units(start, 0),
146                                 current->to_units(end, 1),
147                                 autogrouptype);
148                 }
149         }
150 }
151
152
153 void Tracks::copy_from(Tracks *tracks)
154 {
155         Track *new_track = 0;
156
157         delete_all_tracks();
158         int solo_track_id = tracks->edl->local_session->solo_track_id;
159
160         for(Track *current = tracks->first; current; current = NEXT)
161         {
162                 switch(current->data_type)
163                 {
164                 case TRACK_AUDIO:
165                         new_track = add_audio_track(0, 0);
166                         break;
167                 case TRACK_VIDEO:
168                         new_track = add_video_track(0, 0);
169                         break;
170                 case TRACK_SUBTITLE:
171                         new_track = add_subttl_track(0, 0);
172                         break;
173                 default:
174                         continue;
175                 }
176                 new_track->copy_from(current);
177
178                 if( current->get_id() == solo_track_id )
179                         edl->local_session->solo_track_id = new_track->get_id();
180         }
181 }
182
183 Tracks& Tracks::operator=(Tracks &tracks)
184 {
185 printf("Tracks::operator= 1\n");
186         copy_from(&tracks);
187         return *this;
188 }
189
190 int Tracks::load(FileXML *xml,
191         int &track_offset,
192         uint32_t load_flags)
193 {
194 // add the appropriate type of track
195         char string[BCTEXTLEN];
196         Track *track = 0;
197         string[0] = 0;
198
199         xml->tag.get_property("TYPE", string);
200
201         if((load_flags & LOAD_ALL) == LOAD_ALL ||
202                 (load_flags & LOAD_EDITS)) {
203                 if(!strcmp(string, "VIDEO")) {
204                         track = add_video_track(0, 0);
205                 }
206                 else if(!strcmp(string, "SUBTTL")) {
207                         track = add_subttl_track(0, 0);
208                 }
209                 else {
210                         track = add_audio_track(0, 0);    // default to audio
211                 }
212         }
213         else {
214                 track = get_item_number(track_offset++);
215         }
216
217 // load it
218         if( track ) track->load(xml, track_offset, load_flags);
219
220         return 0;
221 }
222
223 Track* Tracks::add_audio_track(int above, Track *dst_track)
224 {
225         ATrack* new_track = new ATrack(edl, this);
226         if(!dst_track)
227         {
228                 dst_track = (above ? first : last);
229         }
230
231         if(above)
232         {
233                 insert_before(dst_track, (Track*)new_track);
234         }
235         else
236         {
237                 insert_after(dst_track, (Track*)new_track);
238 // Shift effects referenced below the destination track
239         }
240
241 // Shift effects referenced below the new track
242         for(Track *track = last;
243                 track && track != new_track;
244                 track = track->previous)
245         {
246                 change_modules(number_of(track) - 1, number_of(track), 0);
247         }
248
249
250         new_track->create_objects();
251         new_track->set_default_title();
252
253         int current_pan = 0;
254         for(Track *current = first;
255                 current != (Track*)new_track;
256                 current = NEXT)
257         {
258                 if(current->data_type == TRACK_AUDIO) current_pan++;
259                 if(current_pan >= edl->session->audio_channels) current_pan = 0;
260         }
261
262
263
264         PanAuto* pan_auto =
265                 (PanAuto*)new_track->automation->autos[AUTOMATION_PAN]->default_auto;
266         pan_auto->values[current_pan] = 1.0;
267
268         BC_Pan::calculate_stick_position(edl->session->audio_channels,
269                 edl->session->achannel_positions,
270                 pan_auto->values,
271                 MAX_PAN,
272                 PAN_RADIUS,
273                 pan_auto->handle_x,
274                 pan_auto->handle_y);
275         return new_track;
276 }
277
278 Track* Tracks::add_video_track(int above, Track *dst_track)
279 {
280         VTrack* new_track = new VTrack(edl, this);
281         if(!dst_track)
282                 dst_track = (above ? first : last);
283         if(above)
284                 insert_before(dst_track, (Track*)new_track);
285         else
286                 insert_after(dst_track, (Track*)new_track);
287
288         for(Track *track = last; track && track != new_track; track = track->previous)
289                 change_modules(number_of(track) - 1, number_of(track), 0);
290
291         new_track->create_objects();
292         new_track->set_default_title();
293         return new_track;
294 }
295
296
297 Track* Tracks::add_subttl_track(int above, Track *dst_track)
298 {
299         STrack* new_track = new STrack(edl, this);
300         if(!dst_track)
301                 dst_track = (above ? first : last);
302
303         if(above)
304                 insert_before(dst_track, (Track*)new_track);
305         else
306                 insert_after(dst_track, (Track*)new_track);
307
308         for(Track *track = last; track && track != new_track; track = track->previous)
309                 change_modules(number_of(track) - 1, number_of(track), 0);
310
311         new_track->create_objects();
312         new_track->set_default_title();
313 //      new_track->paste_silence(0,total_length(),0);
314         return new_track;
315 }
316
317
318 int Tracks::delete_track(Track *track, int gang)
319 {
320         if( !track ) return 0;
321         if( gang < 0 )
322                 gang = edl->session->gang_tracks != GANG_NONE ? 1 : 0;
323         Track *nxt = track->next;
324         if( gang ) {
325                 while( track && !track->master && track->previous )
326                         track = track->previous;
327                 while( nxt && !nxt->master )
328                         nxt = nxt->next;
329         }
330         Track *current = track;
331         int old_location = number_of(current);
332         for( Track *next_track=0; current!=nxt; current=next_track ) {
333                 next_track = current->next;
334                 detach_shared_effects(old_location);
335                 for( Track *curr=current; curr; curr=curr->next ) {
336 // Shift effects referencing effects below the deleted track
337                         change_modules(number_of(curr), number_of(curr)-1, 0);
338                 }
339                 delete current;
340         }
341         return 0;
342 }
343
344 int Tracks::detach_shared_effects(int module)
345 {
346         for( Track *current=first; current; current=NEXT ) {
347                 current->detach_shared_effects(module);
348         }
349         return 0;
350
351 int Tracks::detach_ganged_effects(Plugin *plugin)
352 {
353         if( edl->session->gang_tracks == GANG_NONE ) return 1;
354         for( Track *current=first; current; current=NEXT ) {
355                 if( current == plugin->track ) continue;
356                 if( !current->armed_gang(plugin->track) ) continue;
357                 current->detach_ganged_effects(plugin);
358         }
359         return 0;
360 }
361
362 int Tracks::total_of(int type)
363 {
364         int result = 0;
365
366         for(Track *current = first; current; current = NEXT)
367         {
368                 long unit_start = current->to_units(edl->local_session->get_selectionstart(1), 0);
369                 Auto *mute_keyframe = 0;
370                 current->automation->autos[AUTOMATION_MUTE]->
371                         get_prev_auto(unit_start, PLAY_FORWARD, mute_keyframe);
372                 IntAuto *mute_auto = (IntAuto *)mute_keyframe;
373
374                 result +=
375                         (current->play && type == PLAY) ||
376                         (current->is_armed() && type == RECORD) ||
377                         (current->is_ganged() && type == GANG) ||
378                         (current->draw && type == DRAW) ||
379                         (mute_auto->value && type == MUTE) ||
380                         (current->expand_view && type == EXPAND);
381         }
382         return result;
383 }
384
385 int Tracks::recordable_audio_tracks()
386 {
387         int result = 0;
388         for(Track *current = first; current; current = NEXT)
389                 if(current->data_type == TRACK_AUDIO && current->is_armed()) result++;
390         return result;
391 }
392
393 int Tracks::recordable_video_tracks()
394 {
395         int result = 0;
396         for(Track *current = first; current; current = NEXT)
397         {
398                 if(current->data_type == TRACK_VIDEO && current->is_armed()) result++;
399         }
400         return result;
401 }
402
403
404 int Tracks::playable_audio_tracks()
405 {
406         int result = 0;
407
408         for(Track *current = first; current; current = NEXT)
409         {
410                 if(current->data_type == TRACK_AUDIO && current->play)
411                 {
412                         result++;
413                 }
414         }
415
416         return result;
417 }
418
419 int Tracks::playable_video_tracks()
420 {
421         int result = 0;
422
423         for(Track *current = first; current; current = NEXT)
424         {
425                 if(current->data_type == TRACK_VIDEO && current->play)
426                 {
427                         result++;
428                 }
429         }
430         return result;
431 }
432
433 int Tracks::total_audio_tracks()
434 {
435         int result = 0;
436         for(Track *current = first; current; current = NEXT)
437                 if(current->data_type == TRACK_AUDIO) result++;
438         return result;
439 }
440
441 int Tracks::total_video_tracks()
442 {
443         int result = 0;
444         for(Track *current = first; current; current = NEXT)
445                 if(current->data_type == TRACK_VIDEO) result++;
446         return result;
447 }
448
449 double Tracks::total_playable_length()
450 {
451         double total = 0;
452         for(Track *current = first; current; current = NEXT)
453         {
454                 if( current->play )
455                 {
456                         double length = current->get_length();
457                         if(length > total) total = length;
458                 }
459         }
460         return total;
461 }
462
463 double Tracks::total_recordable_length()
464 {
465         double total = -1;
466         for(Track *current = first; current; current = NEXT)
467         {
468                 if(current->is_armed())
469                 {
470                         double length = current->get_length();
471                         if(length > total) total = length;
472                 }
473         }
474         return total;
475 }
476
477 double Tracks::total_length()
478 {
479         double total = 0;
480         for(Track *current = first; current; current = NEXT)
481         {
482                 double length = current->get_length();
483                 if(length > total) total = length;
484         }
485         return total;
486 }
487
488 double Tracks::total_audio_length()
489 {
490         double total = 0;
491         for(Track *current = first; current; current = NEXT)
492         {
493                 if(current->data_type == TRACK_AUDIO &&
494                         current->get_length() > total) total = current->get_length();
495         }
496         return total;
497 }
498
499 double Tracks::total_video_length()
500 {
501         double total = 0;
502         for(Track *current = first; current; current = NEXT)
503         {
504                 if(current->data_type == TRACK_VIDEO &&
505                         current->get_length() > total) total = current->get_length();
506         }
507         return total;
508 }
509
510 double Tracks::total_length_framealigned(double fps)
511 {
512         if (total_audio_tracks() && total_video_tracks())
513                 return MIN(floor(total_audio_length() * fps), floor(total_video_length() * fps)) / fps;
514
515         if (total_audio_tracks())
516                 return floor(total_audio_length() * fps) / fps;
517
518         if (total_video_tracks())
519                 return floor(total_video_length() * fps) / fps;
520
521         return 0;
522 }
523
524 void Tracks::translate_fauto_xy(int fauto, float dx, float dy, int all)
525 {
526         Track *track = first;
527         for( ; track; track=track->next ) {
528                 if( !all && !track->is_armed() ) continue;
529                 if( track->data_type != TRACK_VIDEO ) continue;
530                 ((VTrack*)track)->translate(fauto, dx, dy, all);
531         }
532 }
533
534 void Tracks::translate_projector(float dx, float dy, int all)
535 {
536         translate_fauto_xy(AUTOMATION_PROJECTOR_X, dx, dy, all);
537 }
538
539 void Tracks::translate_camera(float dx, float dy, int all)
540 {
541         translate_fauto_xy(AUTOMATION_CAMERA_X, dx, dy, all);
542 }
543
544 void Tracks::crop_resize(float x, float y, float z)
545 {
546         float ctr_x = edl->session->output_w / 2.;
547         float ctr_y = edl->session->output_h / 2.;
548         Track *track = first;
549         for( ; track; track=track->next ) {
550                 if( !track->is_armed() ) continue;
551                 if( track->data_type != TRACK_VIDEO ) continue;
552                 float px, py, pz;
553                 track->get_projector(px, py, pz);
554                 float old_x = px + ctr_x;
555                 float old_y = py + ctr_y;
556                 float nx = (old_x - x) * z;
557                 float ny = (old_y - y) * z;
558                 track->set_projector(nx, ny, pz * z);
559         }
560 }
561
562 void Tracks::crop_shrink(float x, float y, float z)
563 {
564         float ctr_x = edl->session->output_w / 2.;
565         float ctr_y = edl->session->output_h / 2.;
566         Track *track = first;
567         for( ; track; track=track->next ) {
568                 if( !track->is_armed() ) continue;
569                 if( track->data_type != TRACK_VIDEO ) continue;
570                 float cx, cy, cz, px, py, pz;
571                 track->get_camera(cx, cy, cz);
572                 track->get_projector(px, py, pz);
573                 float dx = x - (px + ctr_x);
574                 float dy = y - (py + ctr_y);
575                 cz *= pz;
576                 cx += dx / cz;  cy += dy / cz;
577                 track->set_camera(cx, cy, cz * z);
578                 px += dx;  py += dy;
579                 track->set_projector(px, py, 1 / z);
580         }
581 }
582
583 void Tracks::update_y_pixels(Theme *theme)
584 {
585 //      int y = -edl->local_session->track_start;
586         int y = 0;
587         for(Track *current = first; current; current = NEXT)
588         {
589 //printf("Tracks::update_y_pixels %d\n", y);
590                 current->y_pixel = y;
591                 if( current->is_hidden() ) continue;
592                 y += current->vertical_span(theme);
593         }
594 }
595
596 int Tracks::dump(FILE *fp)
597 {
598         for(Track* current = first; current; current = NEXT)
599         {
600                 fprintf(fp,"  Track: %p\n", current);
601                 current->dump(fp);
602                 fprintf(fp,"\n");
603         }
604         return 0;
605 }
606
607 void Tracks::select_all(int type,
608                 int value)
609 {
610         for(Track* current = first; current; current = NEXT)
611         {
612                 double position = edl->local_session->get_selectionstart(1);
613
614                 if(type == PLAY) current->play = value;
615                 if(type == RECORD) current->armed = value;
616                 if(type == GANG) current->ganged = value;
617                 if(type == DRAW) current->draw = value;
618
619                 if(type == MUTE)
620                 {
621                         ((IntAuto*)current->automation->autos[AUTOMATION_MUTE]->get_auto_for_editing(position))->value = value;
622                 }
623
624                 if(type == EXPAND) current->expand_view = value;
625         }
626 }
627
628 // ===================================== file operations
629
630 int Tracks::popup_transition(int cursor_x, int cursor_y)
631 {
632         int result = 0;
633         for(Track* current = first; current && !result; current = NEXT)
634         {
635                 result = current->popup_transition(cursor_x, cursor_y);
636         }
637         return result;
638 }
639
640
641 int Tracks::change_channels(int oldchannels, int newchannels)
642 {
643         for(Track *current = first; current; current = NEXT)
644         { current->change_channels(oldchannels, newchannels); }
645         return 0;
646 }
647
648
649
650 int Tracks::totalpixels()
651 {
652         int result = 0;
653         for(Track* current = first; current; current = NEXT)
654         {
655                 result += current->data_h;
656         }
657         return result;
658 }
659
660 int Tracks::number_of(Track *track)
661 {
662         int i = 0;
663         for(Track *current = first; current && current != track; current = NEXT)
664         {
665                 i++;
666         }
667         return i;
668 }
669
670 Track* Tracks::number(int number)
671 {
672         Track *current;
673         int i = 0;
674         for(current = first; current && i < number; current = NEXT)
675         {
676                 i++;
677         }
678         return current;
679 }
680
681 Track* Tracks::get_track_by_id(int id)
682 {
683         Track *track = edl->tracks->first;
684         while( track && track->get_id() != id ) track = track->next;
685         return track;
686 }
687
688 int Tracks::total_playable_vtracks()
689 {
690         int result = 0;
691         for(Track *current = first; current; current = NEXT)
692         {
693                 if(current->data_type == TRACK_VIDEO && current->play) result++;
694         }
695         return result;
696 }
697
698 Plugin *Tracks::plugin_exists(int plugin_id)
699 {
700         if( plugin_id < 0 ) return 0;
701         Plugin *plugin = 0;
702         for( Track *track=first; !plugin && track; track=track->next ) {
703                 plugin = track->plugin_exists(plugin_id);
704         }
705         return plugin;
706 }
707
708 int Tracks::track_exists(Track *track)
709 {
710         for(Track *current = first; current; current = NEXT)
711         {
712                 if(current == track) return 1;
713         }
714         return 0;
715 }
716
717 int Tracks::new_group(int id)
718 {
719         int count = 0;
720         for( Track *track=first; track; track=track->next ) {
721                 if( !track->is_armed() ) continue;
722                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
723                         if( edit->group_id > 0 ) continue;
724                         if( !edit->is_selected ) continue;
725                         edit->group_id = id;
726                         ++count;
727                 }
728         }
729         return count;
730 }
731
732 int Tracks::set_group_selected(int id, int v)
733 {
734         int count = 0;
735         int gang = edl->session->gang_tracks != GANG_NONE ? 1 : 0;
736         for( Track *track=first; track; track=track->next ) {
737                 if( track->is_hidden() ) continue;
738                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
739                         if( edit->group_id != id ) continue;
740                         if( v < 0 ) v = !edit->is_selected ? 1 : 0;
741                         edit->select_affected_edits(v, gang);
742                         ++count;
743                 }
744         }
745         return count;
746 }
747
748 int Tracks::del_group(int id)
749 {
750         int count = 0;
751         for( Track *track=first; track; track=track->next ) {
752                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
753                         if( edit->group_id != id ) continue;
754                         edit->is_selected = 1;
755                         edit->group_id = 0;
756                         ++count;
757                 }
758         }
759         return count;
760 }
761
762 Track *Tracks::get(int idx, int data_type)
763 {
764         for(Track *current = first; current; current = NEXT)
765         {
766                 if( current->data_type != data_type ) continue;
767                 if( --idx < 0 ) return current;
768         }
769         return 0;
770 }
771
772 void Tracks::move_tracks(Track *src, Track *dst, int n)
773 {
774         if( src == dst ) return;
775         while( --n >= 0 && src ) {
776                 Track *nxt = src->next;
777                 change_modules(number_of(src), total(), 0);
778                 for( Track *track=nxt; track; track=track->next )
779                         change_modules(number_of(track), number_of(track)-1, 0);
780                 remove_pointer(src);
781                 int ndst = dst ? number_of(dst) : total();
782                 insert_before(dst, src);
783                 for( Track *track=last; track && track!=src; track=track->previous ) 
784                         change_modules(number_of(track)-1, number_of(track), 0);
785                 change_modules(total(), ndst, 0);
786                 src = nxt;
787         }
788 }
789
790 double Tracks::align_timecodes()
791 {
792         double offset = -1;
793         for( Track *track=first; track; track=track->next ) {
794                 if( !track->is_armed() ) continue;
795                 double early_offset = track->edits->early_timecode();
796                 if( offset < 0 || offset > early_offset )
797                         offset = early_offset;
798         }
799         if( offset >= 0 ) {
800                 for( Track *track=first; track; track=track->next ) {
801                         if( !track->is_armed() ) continue;
802                         track->edits->align_timecodes(offset);
803                 }
804         }
805         return offset;
806 }
807
808 void Tracks::update_idxbl_length(int id, double dt)
809 {
810         for( Track *track=first; track; track=track->next ) {
811                 if( !track->is_armed() ) continue;
812                 int64_t du = track->to_units(dt,0);
813                 track->edits->update_idxbl_length(id, du);
814                 track->optimize();
815         }
816 }
817