drag edithandle rework, lib x264/x265 update, dvb fixes, batchrender boot_defaults...
[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 "mainsession.h"
39 #include "strack.h"
40 #include "theme.h"
41 #include "track.h"
42 #include "trackcanvas.h"
43 #include "tracks.h"
44 #include "transportque.inc"
45 #include "vtrack.h"
46 #include <string.h>
47
48 Tracks::Tracks(EDL *edl)
49  : List<Track>()
50 {
51         this->edl = edl;
52 }
53
54 Tracks::Tracks()
55  : List<Track>()
56 {
57 }
58
59
60 Tracks::~Tracks()
61 {
62         delete_all_tracks();
63 }
64
65
66
67
68
69 void Tracks::equivalent_output(Tracks *tracks, double *result)
70 {
71         if(total_playable_vtracks() != tracks->total_playable_vtracks())
72         {
73                 *result = 0;
74         }
75         else
76         {
77                 Track *current = first;
78                 Track *that_current = tracks->first;
79                 while(current || that_current)
80                 {
81 // Get next video track
82                         while(current && current->data_type != TRACK_VIDEO)
83                                 current = NEXT;
84
85                         while(that_current && that_current->data_type != TRACK_VIDEO)
86                                 that_current = that_current->next;
87
88 // One doesn't exist but the other does
89                         if((!current && that_current) ||
90                                 (current && !that_current))
91                         {
92                                 *result = 0;
93                                 break;
94                         }
95                         else
96 // Both exist
97                         if(current && that_current)
98                         {
99                                 current->equivalent_output(that_current, result);
100                                 current = NEXT;
101                                 that_current = that_current->next;
102                         }
103                 }
104         }
105 }
106
107
108 void Tracks::clear_selected_edits()
109 {
110         for( Track *track=first; track; track=track->next ) {
111                 for( Edit *edit=track->edits->first; edit; edit=edit->next )
112                         edit->is_selected = 0;
113         }
114 }
115
116 void Tracks::select_affected_edits(double position, Track *start_track, int sense)
117 {
118         for( Track *track=start_track; track; track=track->next ) {
119                 if( !track->record ) continue;
120                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
121                         if( edit->silence() ) continue;
122                         double startproject = track->from_units(edit->startproject);
123                         if( edl->equivalent(startproject, position) ) {
124                                 edit->is_selected = sense >= 0 ? sense :
125                                         edit->is_selected ? 0 : 1;
126                                 break;
127                         }
128                 }
129         }
130 }
131
132 void Tracks::get_selected_edits(ArrayList<Edit*> *drag_edits)
133 {
134         drag_edits->remove_all();
135         for( Track *track=first; track; track=track->next ) {
136                 if( !track->record ) continue;
137                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
138                         if( !edit->is_selected ) continue;
139                         drag_edits->append(edit);
140                 }
141         }
142 }
143
144 void Tracks::get_automation_extents(float *min,
145         float *max,
146         double start,
147         double end,
148         int autogrouptype)
149 {
150         *min = 0;
151         *max = 0;
152         int coords_undefined = 1;
153         for(Track *current = first; current; current = NEXT)
154         {
155                 if(current->record)
156                 {
157                         current->automation->get_extents(min,
158                                 max,
159                                 &coords_undefined,
160                                 current->to_units(start, 0),
161                                 current->to_units(end, 1),
162                                 autogrouptype);
163                 }
164         }
165 }
166
167
168 void Tracks::copy_from(Tracks *tracks)
169 {
170         Track *new_track = 0;
171
172         delete_all_tracks();
173         for(Track *current = tracks->first; current; current = NEXT)
174         {
175                 switch(current->data_type)
176                 {
177                 case TRACK_AUDIO:
178                         new_track = add_audio_track(0, 0);
179                         break;
180                 case TRACK_VIDEO:
181                         new_track = add_video_track(0, 0);
182                         break;
183                 case TRACK_SUBTITLE:
184                         new_track = add_subttl_track(0, 0);
185                         break;
186                 default:
187                         continue;
188                 }
189                 new_track->copy_from(current);
190         }
191 }
192
193 Tracks& Tracks::operator=(Tracks &tracks)
194 {
195 printf("Tracks::operator= 1\n");
196         copy_from(&tracks);
197         return *this;
198 }
199
200 int Tracks::load(FileXML *xml,
201         int &track_offset,
202         uint32_t load_flags)
203 {
204 // add the appropriate type of track
205         char string[BCTEXTLEN];
206         Track *track = 0;
207         string[0] = 0;
208
209         xml->tag.get_property("TYPE", string);
210
211         if((load_flags & LOAD_ALL) == LOAD_ALL ||
212                 (load_flags & LOAD_EDITS)) {
213                 if(!strcmp(string, "VIDEO")) {
214                         track = add_video_track(0, 0);
215                 }
216                 else if(!strcmp(string, "SUBTTL")) {
217                         track = add_subttl_track(0, 0);
218                 }
219                 else {
220                         track = add_audio_track(0, 0);    // default to audio
221                 }
222         }
223         else {
224                 track = get_item_number(track_offset++);
225         }
226
227 // load it
228         if( track ) track->load(xml, track_offset, load_flags);
229
230         return 0;
231 }
232
233 Track* Tracks::add_audio_track(int above, Track *dst_track)
234 {
235         ATrack* new_track = new ATrack(edl, this);
236         if(!dst_track)
237         {
238                 dst_track = (above ? first : last);
239         }
240
241         if(above)
242         {
243                 insert_before(dst_track, (Track*)new_track);
244         }
245         else
246         {
247                 insert_after(dst_track, (Track*)new_track);
248 // Shift effects referenced below the destination track
249         }
250
251 // Shift effects referenced below the new track
252         for(Track *track = last;
253                 track && track != new_track;
254                 track = track->previous)
255         {
256                 change_modules(number_of(track) - 1, number_of(track), 0);
257         }
258
259
260         new_track->create_objects();
261         new_track->set_default_title();
262
263         int current_pan = 0;
264         for(Track *current = first;
265                 current != (Track*)new_track;
266                 current = NEXT)
267         {
268                 if(current->data_type == TRACK_AUDIO) current_pan++;
269                 if(current_pan >= edl->session->audio_channels) current_pan = 0;
270         }
271
272
273
274         PanAuto* pan_auto =
275                 (PanAuto*)new_track->automation->autos[AUTOMATION_PAN]->default_auto;
276         pan_auto->values[current_pan] = 1.0;
277
278         BC_Pan::calculate_stick_position(edl->session->audio_channels,
279                 edl->session->achannel_positions,
280                 pan_auto->values,
281                 MAX_PAN,
282                 PAN_RADIUS,
283                 pan_auto->handle_x,
284                 pan_auto->handle_y);
285         return new_track;
286 }
287
288 Track* Tracks::add_video_track(int above, Track *dst_track)
289 {
290         VTrack* new_track = new VTrack(edl, this);
291         if(!dst_track)
292                 dst_track = (above ? first : last);
293         if(above)
294                 insert_before(dst_track, (Track*)new_track);
295         else
296                 insert_after(dst_track, (Track*)new_track);
297
298         for(Track *track = last; track && track != new_track; track = track->previous)
299                 change_modules(number_of(track) - 1, number_of(track), 0);
300
301         new_track->create_objects();
302         new_track->set_default_title();
303         return new_track;
304 }
305
306
307 Track* Tracks::add_subttl_track(int above, Track *dst_track)
308 {
309         STrack* new_track = new STrack(edl, this);
310         if(!dst_track)
311                 dst_track = (above ? first : last);
312
313         if(above)
314                 insert_before(dst_track, (Track*)new_track);
315         else
316                 insert_after(dst_track, (Track*)new_track);
317
318         for(Track *track = last; track && track != new_track; track = track->previous)
319                 change_modules(number_of(track) - 1, number_of(track), 0);
320
321         new_track->create_objects();
322         new_track->set_default_title();
323 //      new_track->paste_silence(0,total_length(),0);
324         return new_track;
325 }
326
327
328 int Tracks::delete_track(Track *track)
329 {
330         if (!track)
331                 return 0;
332
333         int old_location = number_of(track);
334         detach_shared_effects(old_location);
335
336 // Shift effects referencing effects below the deleted track
337         for(Track *current = track;
338                 current;
339                 current = NEXT)
340         {
341                 change_modules(number_of(current), number_of(current) - 1, 0);
342         }
343         if(track) delete track;
344
345         return 0;
346 }
347
348 int Tracks::detach_shared_effects(int module)
349 {
350         for(Track *current = first; current; current = NEXT)
351         {
352                 current->detach_shared_effects(module);
353         }
354
355         return 0;
356  }
357
358 int Tracks::total_of(int type)
359 {
360         int result = 0;
361
362         for(Track *current = first; current; current = NEXT)
363         {
364                 long unit_start = current->to_units(edl->local_session->get_selectionstart(1), 0);
365                 Auto *mute_keyframe = 0;
366                 current->automation->autos[AUTOMATION_MUTE]->
367                         get_prev_auto(unit_start, PLAY_FORWARD, mute_keyframe);
368                 IntAuto *mute_auto = (IntAuto *)mute_keyframe;
369
370                 result +=
371                         (current->play && type == PLAY) ||
372                         (current->record && type == RECORD) ||
373                         (current->gang && type == GANG) ||
374                         (current->draw && type == DRAW) ||
375                         (mute_auto->value && type == MUTE) ||
376                         (current->expand_view && type == EXPAND);
377         }
378         return result;
379 }
380
381 int Tracks::recordable_audio_tracks()
382 {
383         int result = 0;
384         for(Track *current = first; current; current = NEXT)
385                 if(current->data_type == TRACK_AUDIO && current->record) result++;
386         return result;
387 }
388
389 int Tracks::recordable_video_tracks()
390 {
391         int result = 0;
392         for(Track *current = first; current; current = NEXT)
393         {
394                 if(current->data_type == TRACK_VIDEO && current->record) result++;
395         }
396         return result;
397 }
398
399
400 int Tracks::playable_audio_tracks()
401 {
402         int result = 0;
403
404         for(Track *current = first; current; current = NEXT)
405         {
406                 if(current->data_type == TRACK_AUDIO && current->play)
407                 {
408                         result++;
409                 }
410         }
411
412         return result;
413 }
414
415 int Tracks::playable_video_tracks()
416 {
417         int result = 0;
418
419         for(Track *current = first; current; current = NEXT)
420         {
421                 if(current->data_type == TRACK_VIDEO && current->play)
422                 {
423                         result++;
424                 }
425         }
426         return result;
427 }
428
429 int Tracks::total_audio_tracks()
430 {
431         int result = 0;
432         for(Track *current = first; current; current = NEXT)
433                 if(current->data_type == TRACK_AUDIO) result++;
434         return result;
435 }
436
437 int Tracks::total_video_tracks()
438 {
439         int result = 0;
440         for(Track *current = first; current; current = NEXT)
441                 if(current->data_type == TRACK_VIDEO) result++;
442         return result;
443 }
444
445 double Tracks::total_playable_length()
446 {
447         double total = 0;
448         for(Track *current = first; current; current = NEXT)
449         {
450                 if( current->play )
451                 {
452                         double length = current->get_length();
453                         if(length > total) total = length;
454                 }
455         }
456         return total;
457 }
458
459 double Tracks::total_recordable_length()
460 {
461         double total = -1;
462         for(Track *current = first; current; current = NEXT)
463         {
464                 if(current->record)
465                 {
466                         double length = current->get_length();
467                         if(length > total) total = length;
468                 }
469         }
470         return total;
471 }
472
473 double Tracks::total_length()
474 {
475         double total = 0;
476         for(Track *current = first; current; current = NEXT)
477         {
478                 double length = current->get_length();
479                 if(length > total) total = length;
480         }
481         return total;
482 }
483
484 double Tracks::total_audio_length()
485 {
486         double total = 0;
487         for(Track *current = first; current; current = NEXT)
488         {
489                 if(current->data_type == TRACK_AUDIO &&
490                         current->get_length() > total) total = current->get_length();
491         }
492         return total;
493 }
494
495 double Tracks::total_video_length()
496 {
497         double total = 0;
498         for(Track *current = first; current; current = NEXT)
499         {
500                 if(current->data_type == TRACK_VIDEO &&
501                         current->get_length() > total) total = current->get_length();
502         }
503         return total;
504 }
505
506 double Tracks::total_length_framealigned(double fps)
507 {
508         if (total_audio_tracks() && total_video_tracks())
509                 return MIN(floor(total_audio_length() * fps), floor(total_video_length() * fps)) / fps;
510
511         if (total_audio_tracks())
512                 return floor(total_audio_length() * fps) / fps;
513
514         if (total_video_tracks())
515                 return floor(total_video_length() * fps) / fps;
516
517         return 0;
518 }
519
520 void Tracks::translate_projector(float offset_x, float offset_y)
521 {
522         for(Track *current = first; current; current = NEXT)
523         {
524                 if(current->data_type == TRACK_VIDEO)
525                 {
526                         ((VTrack*)current)->translate(offset_x, offset_y, 0);
527                 }
528         }
529 }
530
531 void Tracks::update_y_pixels(Theme *theme)
532 {
533 //      int y = -edl->local_session->track_start;
534         int y = 0;
535         for(Track *current = first; current; current = NEXT)
536         {
537 //printf("Tracks::update_y_pixels %d\n", y);
538                 current->y_pixel = y;
539                 y += current->vertical_span(theme);
540         }
541 }
542
543 int Tracks::dump(FILE *fp)
544 {
545         for(Track* current = first; current; current = NEXT)
546         {
547                 fprintf(fp,"  Track: %p\n", current);
548                 current->dump(fp);
549                 fprintf(fp,"\n");
550         }
551         return 0;
552 }
553
554 void Tracks::select_all(int type,
555                 int value)
556 {
557         for(Track* current = first; current; current = NEXT)
558         {
559                 double position = edl->local_session->get_selectionstart(1);
560
561                 if(type == PLAY) current->play = value;
562                 if(type == RECORD) current->record = value;
563                 if(type == GANG) current->gang = value;
564                 if(type == DRAW) current->draw = value;
565
566                 if(type == MUTE)
567                 {
568                         ((IntAuto*)current->automation->autos[AUTOMATION_MUTE]->get_auto_for_editing(position))->value = value;
569                 }
570
571                 if(type == EXPAND) current->expand_view = value;
572         }
573 }
574
575 // ===================================== file operations
576
577 int Tracks::popup_transition(int cursor_x, int cursor_y)
578 {
579         int result = 0;
580         for(Track* current = first; current && !result; current = NEXT)
581         {
582                 result = current->popup_transition(cursor_x, cursor_y);
583         }
584         return result;
585 }
586
587
588 int Tracks::change_channels(int oldchannels, int newchannels)
589 {
590         for(Track *current = first; current; current = NEXT)
591         { current->change_channels(oldchannels, newchannels); }
592         return 0;
593 }
594
595
596
597 int Tracks::totalpixels()
598 {
599         int result = 0;
600         for(Track* current = first; current; current = NEXT)
601         {
602                 result += edl->local_session->zoom_track;
603         }
604         return result;
605 }
606
607 int Tracks::number_of(Track *track)
608 {
609         int i = 0;
610         for(Track *current = first; current && current != track; current = NEXT)
611         {
612                 i++;
613         }
614         return i;
615 }
616
617 Track* Tracks::number(int number)
618 {
619         Track *current;
620         int i = 0;
621         for(current = first; current && i < number; current = NEXT)
622         {
623                 i++;
624         }
625         return current;
626 }
627
628
629 int Tracks::total_playable_vtracks()
630 {
631         int result = 0;
632         for(Track *current = first; current; current = NEXT)
633         {
634                 if(current->data_type == TRACK_VIDEO && current->play) result++;
635         }
636         return result;
637 }
638
639 int Tracks::plugin_exists(Plugin *plugin)
640 {
641         for(Track *track = first; track; track = track->next)
642         {
643                 if(track->plugin_exists(plugin)) return 1;
644         }
645         return 0;
646 }
647
648 int Tracks::track_exists(Track *track)
649 {
650         for(Track *current = first; current; current = NEXT)
651         {
652                 if(current == track) return 1;
653         }
654         return 0;
655 }
656
657 int Tracks::new_group(int id)
658 {
659         int count = 0;
660         for( Track *track=first; track; track=track->next ) {
661                 if( !track->record ) continue;
662                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
663                         if( edit->group_id > 0 ) continue;
664                         if( !edit->is_selected ) continue;
665                         edit->group_id = id;
666                         ++count;
667                 }
668         }
669         return count;
670 }
671
672 int Tracks::set_group_selected(int id, int v)
673 {
674         int count = 0;
675         for( Track *track=first; track; track=track->next ) {
676                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
677                         if( edit->group_id != id ) continue;
678                         edit->is_selected = v >= 0 ? v : !edit->is_selected ? 1 : 0;
679                         ++count;
680                 }
681         }
682         return count;
683 }
684
685 int Tracks::del_group(int id)
686 {
687         int count = 0;
688         for( Track *track=first; track; track=track->next ) {
689                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
690                         if( edit->group_id != id ) continue;
691                         edit->is_selected = 1;
692                         edit->group_id = 0;
693                         ++count;
694                 }
695         }
696         return count;
697 }
698
699 Track *Tracks::get(int idx, int data_type)
700 {
701         for(Track *current = first; current; current = NEXT)
702         {
703                 if( current->data_type != data_type ) continue;
704                 if( --idx < 0 ) return current;
705         }
706         return 0;
707 }
708