no longer need ffmpeg patch0 which was for Termux
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / localsession.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * Copyright (C) 2003-2016 Cinelerra CV contributors
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 #include "automation.h"
24 #include "awindowgui.h"
25 #include "clip.h"
26 #include "bchash.h"
27 #include "edl.h"
28 #include "edlsession.h"
29 #include "filesystem.h"
30 #include "filexml.h"
31 #include "floatauto.h"
32 #include "language.h"
33 #include "localsession.h"
34
35
36 static const char *xml_autogrouptypes_titlesmax[] =
37 {
38         "AUTOGROUPTYPE_AUDIO_FADE_MAX",
39         "AUTOGROUPTYPE_VIDEO_FADE_MAX",
40         "AUTOGROUPTYPE_ZOOM_MAX",
41         "AUTOGROUPTYPE_SPEED_MAX",
42         "AUTOGROUPTYPE_X_MAX",
43         "AUTOGROUPTYPE_Y_MAX",
44         "AUTOGROUPTYPE_INT255_MAX"
45 };
46
47 static const char *xml_autogrouptypes_titlesmin[] =
48 {
49         "AUTOGROUPTYPE_AUDIO_FADE_MIN",
50         "AUTOGROUPTYPE_VIDEO_FADE_MIN",
51         "AUTOGROUPTYPE_ZOOM_MIN",
52         "AUTOGROUPTYPE_SPEED_MIN",
53         "AUTOGROUPTYPE_X_MIN",
54         "AUTOGROUPTYPE_Y_MIN",
55         "AUTOGROUPTYPE_INT255_MIN"
56 };
57
58
59 LocalSession::LocalSession(EDL *edl)
60 {
61         this->edl = edl;
62
63         selectionstart = selectionend = 0;
64         in_point = out_point = -1;
65         sprintf(clip_title, _("Program"));
66         strcpy(clip_notes, _("Hello world"));
67         strcpy(clip_icon, "");
68         clipboard_length = 0;
69         asset2edl = 0;
70         loop_playback = 0;
71         loop_start = loop_end = 0;
72         playback_start = -1;
73         playback_end = 0;
74         preview_start = 0;  preview_end = -1;
75         zoom_sample = DEFAULT_ZOOM_TIME;
76         zoom_y = 0;
77         zoom_atrack = 64;
78         zoom_vtrack = 64;
79         x_pane = y_pane = -1;
80         gang_tracks = GANG_NONE;
81
82         for(int i = 0; i < TOTAL_PANES; i++) {
83                 view_start[i] = 0;
84                 track_start[i] = 0;
85         }
86
87         reset_view_limits();
88         automation_mins[AUTOGROUPTYPE_AUDIO_FADE] = -80;
89         automation_maxs[AUTOGROUPTYPE_AUDIO_FADE] = 6;
90         automation_mins[AUTOGROUPTYPE_VIDEO_FADE] = 0;
91         automation_maxs[AUTOGROUPTYPE_VIDEO_FADE] = 100;
92         automation_mins[AUTOGROUPTYPE_SPEED] = SPEED_MIN;
93         automation_maxs[AUTOGROUPTYPE_SPEED] = 5.000;
94         automation_mins[AUTOGROUPTYPE_INT255] = 0;
95         automation_maxs[AUTOGROUPTYPE_INT255] = 255;
96
97         zoombar_showautotype = AUTOGROUPTYPE_AUDIO_FADE;
98         zoombar_showautocolor = -1;
99
100         floatauto_type = FloatAuto::SMOOTH;
101
102         red = green = blue = 0;
103         red_max = green_max = blue_max = 0;
104         use_max = 0;
105         solo_track_id = -1;
106         gang_tracks = GANG_NONE;
107 }
108
109 LocalSession::~LocalSession()
110 {
111 }
112
113 void LocalSession::copy_from(LocalSession *that)
114 {
115         strcpy(clip_title, that->clip_title);
116         strcpy(clip_notes, that->clip_notes);
117         strcpy(clip_icon, that->clip_icon);
118         in_point = that->in_point;
119         loop_playback = that->loop_playback;
120         loop_start = that->loop_start;
121         loop_end = that->loop_end;
122         out_point = that->out_point;
123         selectionend = that->selectionend;
124         selectionstart = that->selectionstart;
125         x_pane = that->x_pane;
126         y_pane = that->y_pane;
127
128         for(int i = 0; i < TOTAL_PANES; i++)
129         {
130                 view_start[i] = that->view_start[i];
131                 track_start[i] = that->track_start[i];
132         }
133
134         zoom_sample = that->zoom_sample;
135         zoom_y = that->zoom_y;
136         zoom_atrack = that->zoom_atrack;
137         zoom_vtrack = that->zoom_vtrack;
138         preview_start = that->preview_start;
139         preview_end = that->preview_end;
140         red = that->red;
141         green = that->green;
142         blue = that->blue;
143         red_max = that->red_max;
144         green_max = that->green_max;
145         blue_max = that->blue_max;
146         use_max = that->use_max;
147         solo_track_id = that->solo_track_id;
148         gang_tracks = that->gang_tracks;
149
150         for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
151                 automation_mins[i] = that->automation_mins[i];
152                 automation_maxs[i] = that->automation_maxs[i];
153         }
154         floatauto_type = that->floatauto_type;
155 }
156
157 void LocalSession::save_xml(FileXML *file, double start)
158 {
159         file->tag.set_title("LOCALSESSION");
160
161         file->tag.set_property("IN_POINT", in_point - start);
162         file->tag.set_property("LOOP_PLAYBACK", loop_playback);
163         file->tag.set_property("LOOP_START", loop_start - start);
164         file->tag.set_property("LOOP_END", loop_end - start);
165         file->tag.set_property("OUT_POINT", out_point - start);
166         file->tag.set_property("SELECTION_START", selectionstart - start);
167         file->tag.set_property("SELECTION_END", selectionend - start);
168         file->tag.set_property("CLIP_TITLE", clip_title);
169         file->tag.set_property("CLIP_ICON", clip_icon);
170         file->tag.set_property("X_PANE", x_pane);
171         file->tag.set_property("Y_PANE", y_pane);
172
173         char string[BCTEXTLEN];
174         for(int i = 0; i < TOTAL_PANES; i++)
175         {
176                 sprintf(string, "TRACK_START%d", i);
177                 file->tag.set_property(string, track_start[i]);
178                 sprintf(string, "VIEW_START%d", i);
179                 file->tag.set_property(string, view_start[i]);
180         }
181
182         file->tag.set_property("ZOOM_SAMPLE", zoom_sample);
183 //printf("EDLSession::save_session 1\n");
184         file->tag.set_property("ZOOMY", zoom_y);
185         file->tag.set_property("ZOOM_ATRACK", zoom_atrack);
186         file->tag.set_property("ZOOM_VTRACK", zoom_vtrack);
187
188         double preview_start = this->preview_start - start;
189         if( preview_start < 0 ) preview_start = 0;
190         double preview_end = this->preview_end - start;
191         if( preview_end < preview_start ) preview_end = -1;
192         file->tag.set_property("PREVIEW_START", preview_start);
193         file->tag.set_property("PREVIEW_END", preview_end);
194         file->tag.set_property("FLOATAUTO_TYPE", floatauto_type);
195
196         file->tag.set_property("RED", red);
197         file->tag.set_property("GREEN", green);
198         file->tag.set_property("BLUE", blue);
199         file->tag.set_property("RED_MAX", red_max);
200         file->tag.set_property("GREEN_MAX", green_max);
201         file->tag.set_property("BLUE_MAX", blue_max);
202         file->tag.set_property("USE_MAX", use_max);
203         file->tag.set_property("GANG_TRACKS", gang_tracks);
204
205
206         for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
207                 if (!Automation::autogrouptypes_fixedrange[i]) {
208                         file->tag.set_property(xml_autogrouptypes_titlesmin[i],automation_mins[i]);
209                         file->tag.set_property(xml_autogrouptypes_titlesmax[i],automation_maxs[i]);
210                 }
211         }
212         file->append_tag();
213         file->append_newline();
214
215 //this used to be a property, now used as tag member
216 //      file->tag.set_property("CLIP_NOTES", clip_notes);
217         file->tag.set_title("CLIP_NOTES");   file->append_tag();
218         file->append_text(clip_notes);
219         file->tag.set_title("/CLIP_NOTES");  file->append_tag();
220         file->append_newline();
221
222         file->tag.set_title("/LOCALSESSION");
223         file->append_tag();
224         file->append_newline();
225         file->append_newline();
226 }
227
228 void LocalSession::synchronize_params(LocalSession *that)
229 {
230         loop_playback = that->loop_playback;
231         loop_start = that->loop_start;
232         loop_end = that->loop_end;
233         preview_start = that->preview_start;
234         preview_end = that->preview_end;
235         red = that->red;
236         green = that->green;
237         blue = that->blue;
238         red_max = that->red_max;
239         green_max = that->green_max;
240         blue_max = that->blue_max;
241         if( solo_track_id < 0 || that->solo_track_id < 0 )
242                 solo_track_id = that->solo_track_id;
243         gang_tracks = that->gang_tracks;
244 }
245
246
247 void LocalSession::load_xml(FileXML *file, unsigned long load_flags)
248 {
249         if(load_flags & LOAD_SESSION)
250         {
251 // moved to EDL::load_xml for paste to fill silence.
252 //              clipboard_length = 0;
253 // Overwritten by MWindow::load_filenames
254                 file->tag.get_property("CLIP_TITLE", clip_title);
255                 clip_notes[0] = 0;
256                 file->tag.get_property("CLIP_NOTES", clip_notes);
257                 clip_icon[0] = 0;
258                 file->tag.get_property("CLIP_ICON", clip_icon);
259 // kludge if possible
260                 if( !clip_icon[0] ) {
261                         char *cp = clip_notes;
262                         int year, mon, mday, hour, min, sec;
263                         while( *cp && *cp++ != ':' );
264                         if( *cp && sscanf(cp, "%d/%02d/%02d %02d:%02d:%02d,",
265                                         &year, &mon, &mday, &hour, &min, &sec) == 6 ) {
266                                 sprintf(clip_icon, "clip_%02d%02d%02d-%02d%02d%02d.png",
267                                         year, mon, mday, hour, min, sec);
268                         }
269                 }
270                 loop_playback = file->tag.get_property("LOOP_PLAYBACK", 0);
271                 loop_start = file->tag.get_property("LOOP_START", (double)0);
272                 loop_end = file->tag.get_property("LOOP_END", (double)0);
273                 selectionstart = file->tag.get_property("SELECTION_START", (double)0);
274                 selectionend = file->tag.get_property("SELECTION_END", (double)0);
275                 x_pane = file->tag.get_property("X_PANE", -1);
276                 y_pane = file->tag.get_property("Y_PANE", -1);
277
278
279                 char string[BCTEXTLEN];
280                 for(int i = 0; i < TOTAL_PANES; i++)
281                 {
282                         sprintf(string, "TRACK_START%d", i);
283                         track_start[i] = file->tag.get_property(string, track_start[i]);
284                         sprintf(string, "VIEW_START%d", i);
285                         view_start[i] = file->tag.get_property(string, view_start[i]);
286                 }
287
288                 zoom_sample = file->tag.get_property("ZOOM_SAMPLE", zoom_sample);
289                 zoom_y = file->tag.get_property("ZOOMY", zoom_y);
290                 int64_t zoom_track = file->tag.get_property("ZOOM_TRACK", 0);
291                 if( zoom_track > 0 ) zoom_atrack = zoom_vtrack = zoom_track;
292                 zoom_atrack = file->tag.get_property("ZOOM_ATRACK", zoom_atrack);
293                 zoom_vtrack = file->tag.get_property("ZOOM_VTRACK", zoom_vtrack);
294                 preview_start = file->tag.get_property("PREVIEW_START", preview_start);
295                 preview_end = file->tag.get_property("PREVIEW_END", preview_end);
296                 red = file->tag.get_property("RED", red);
297                 green = file->tag.get_property("GREEN", green);
298                 blue = file->tag.get_property("BLUE", blue);
299                 red_max = file->tag.get_property("RED_MAX", red_max);
300                 green_max = file->tag.get_property("GREEN_MAX", green_max);
301                 blue_max = file->tag.get_property("BLUE_MAX", blue_max);
302                 use_max = file->tag.get_property("USE_MAX", use_max);
303                 gang_tracks = file->tag.get_property("GANG_TRACKS", gang_tracks);
304                 for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
305                         if (!Automation::autogrouptypes_fixedrange[i]) {
306                                 automation_mins[i] = file->tag.get_property(xml_autogrouptypes_titlesmin[i],automation_mins[i]);
307                                 AUTOMATIONCLAMPS(automation_mins[i], i);
308                                 automation_maxs[i] = file->tag.get_property(xml_autogrouptypes_titlesmax[i],automation_maxs[i]);
309                                 AUTOMATIONCLAMPS(automation_maxs[i], i);
310                         }
311                 }
312                 floatauto_type = file->tag.get_property("FLOATAUTO_TYPE", floatauto_type);
313         }
314
315
316 // on operations like cut, paste, slice, clear... we should also undo the cursor position as users
317 // expect - this is additionally important in keyboard-only editing in viewer window
318         if(load_flags & LOAD_SESSION || load_flags & LOAD_TIMEBAR)
319         {
320                 selectionstart = file->tag.get_property("SELECTION_START", (double)0);
321                 selectionend = file->tag.get_property("SELECTION_END", (double)0);
322         }
323
324
325
326         if(load_flags & LOAD_TIMEBAR)
327         {
328                 in_point = file->tag.get_property("IN_POINT", (double)-1);
329                 out_point = file->tag.get_property("OUT_POINT", (double)-1);
330         }
331
332         while( !file->read_tag() ) {
333                 if( file->tag.title_is("/LOCALSESSION") ) break;
334                 if( file->tag.title_is("CLIP_NOTES") ) {
335                         XMLBuffer notes;
336                         file->read_text_until("/CLIP_NOTES", &notes, 1);
337                         memset(clip_notes, 0, sizeof(clip_notes));
338                         strncpy(clip_notes, notes.cstr(), sizeof(clip_notes)-1);
339                 }
340         }
341 }
342
343 void LocalSession::boundaries()
344 {
345         zoom_sample = MAX(1, zoom_sample);
346 }
347
348 int LocalSession::load_defaults(BC_Hash *defaults)
349 {
350         loop_playback = defaults->get("LOOP_PLAYBACK", 0);
351         loop_start = defaults->get("LOOP_START", (double)0);
352         loop_end = defaults->get("LOOP_END", (double)0);
353         selectionstart = defaults->get("SELECTIONSTART", selectionstart);
354         selectionend = defaults->get("SELECTIONEND", selectionend);
355 //      track_start = defaults->get("TRACK_START", 0);
356 //      view_start = defaults->get("VIEW_START", 0);
357         zoom_sample = defaults->get("ZOOM_SAMPLE", DEFAULT_ZOOM_TIME);
358         zoom_y = defaults->get("ZOOMY", DEFAULT_ZOOM_TRACK);
359         int64_t zoom_track = defaults->get("ZOOM_TRACK", 0);
360         if( zoom_track == 0 ) zoom_track = DEFAULT_ZOOM_TRACK;
361         zoom_atrack = defaults->get("ZOOM_ATRACK", zoom_track);
362         zoom_vtrack = defaults->get("ZOOM_VTRACK", zoom_track);
363         red = defaults->get("RED", 0.0);
364         green = defaults->get("GREEN", 0.0);
365         blue = defaults->get("BLUE", 0.0);
366         red_max = defaults->get("RED_MAX", 0.0);
367         green_max = defaults->get("GREEN_MAX", 0.0);
368         blue_max = defaults->get("BLUE_MAX", 0.0);
369         use_max = defaults->get("USE_MAX", 0);
370         gang_tracks = defaults->get("GANG_TRACKS", GANG_NONE);
371
372         for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
373                 if (!Automation::autogrouptypes_fixedrange[i]) {
374                         automation_mins[i] = defaults->get(xml_autogrouptypes_titlesmin[i], automation_mins[i]);
375                         AUTOMATIONCLAMPS(automation_mins[i], i);
376                         automation_maxs[i] = defaults->get(xml_autogrouptypes_titlesmax[i], automation_maxs[i]);
377                         AUTOMATIONCLAMPS(automation_maxs[i], i);
378                 }
379         }
380
381         floatauto_type = defaults->get("FLOATAUTO_TYPE", floatauto_type);
382         x_pane = defaults->get("X_PANE", x_pane);
383         y_pane = defaults->get("Y_PANE", y_pane);
384
385         return 0;
386 }
387
388 int LocalSession::save_defaults(BC_Hash *defaults)
389 {
390         defaults->update("LOOP_PLAYBACK", loop_playback);
391         defaults->update("LOOP_START", loop_start);
392         defaults->update("LOOP_END", loop_end);
393         defaults->update("SELECTIONSTART", selectionstart);
394         defaults->update("SELECTIONEND", selectionend);
395 //      defaults->update("TRACK_START", track_start);
396 //      defaults->update("VIEW_START", view_start);
397         defaults->update("ZOOM_SAMPLE", zoom_sample);
398         defaults->update("ZOOMY", zoom_y);
399         defaults->update("ZOOM_ATRACK", zoom_atrack);
400         defaults->update("ZOOM_VTRACK", zoom_vtrack);
401         defaults->update("RED", red);
402         defaults->update("GREEN", green);
403         defaults->update("BLUE", blue);
404         defaults->update("RED_MAX", red_max);
405         defaults->update("GREEN_MAX", green_max);
406         defaults->update("BLUE_MAX", blue_max);
407         defaults->update("USE_MAX", use_max);
408         defaults->update("GANG_TRACKS", gang_tracks);
409
410         for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
411                 if (!Automation::autogrouptypes_fixedrange[i]) {
412                         defaults->update(xml_autogrouptypes_titlesmin[i], automation_mins[i]);
413                         defaults->update(xml_autogrouptypes_titlesmax[i], automation_maxs[i]);
414                 }
415         }
416
417         defaults->update("FLOATAUTO_TYPE", floatauto_type);
418         defaults->update("X_PANE", x_pane);
419         defaults->update("Y_PANE", y_pane);
420         return 0;
421 }
422
423 void LocalSession::set_selectionstart(double value)
424 {
425         this->selectionstart = value;
426 }
427
428 void LocalSession::set_selectionend(double value)
429 {
430         this->selectionend = value;
431 }
432
433 void LocalSession::set_inpoint(double value)
434 {
435         in_point = value;
436 }
437
438 void LocalSession::set_outpoint(double value)
439 {
440         out_point = value;
441 }
442
443 void LocalSession::unset_inpoint()
444 {
445         in_point = -1;
446 }
447
448 void LocalSession::unset_outpoint()
449 {
450         out_point = -1;
451 }
452
453 void LocalSession::set_playback_start(double value)
454 {
455         if( playback_end < 0 ) return;
456         playback_start = value;
457         playback_end = -1;
458 }
459
460 void LocalSession::set_playback_end(double value)
461 {
462         if( value < playback_start ) {
463                 if( playback_end < 0 )
464                         playback_end = playback_start;
465                 playback_start = value;
466         }
467         else if( playback_end < 0 || value > playback_end )
468                 playback_end = value;
469 }
470
471
472 double LocalSession::get_selectionstart(int highlight_only)
473 {
474         if(highlight_only || !EQUIV(selectionstart, selectionend))
475                 return selectionstart;
476
477         if(in_point >= 0)
478                 return in_point;
479         else
480         if(out_point >= 0)
481                 return out_point;
482         else
483                 return selectionstart;
484 }
485
486 double LocalSession::get_selectionend(int highlight_only)
487 {
488         if(highlight_only || !EQUIV(selectionstart, selectionend))
489                 return selectionend;
490
491         if(out_point >= 0)
492                 return out_point;
493         else
494         if(in_point >= 0)
495                 return in_point;
496         else
497                 return selectionend;
498 }
499
500 double LocalSession::get_inpoint()
501 {
502         return in_point;
503 }
504
505 double LocalSession::get_outpoint()
506 {
507         return out_point;
508 }
509
510 int LocalSession::inpoint_valid()
511 {
512         return in_point >= 0;
513 }
514
515 int LocalSession::outpoint_valid()
516 {
517         return out_point >= 0;
518 }
519
520 void LocalSession::reset_view_limits()
521 {
522         automation_mins[AUTOGROUPTYPE_ZOOM] = 0.005;
523         automation_maxs[AUTOGROUPTYPE_ZOOM] = 5.000;
524         int out_w = edl->session->output_w;
525         automation_mins[AUTOGROUPTYPE_X] = -out_w;
526         automation_maxs[AUTOGROUPTYPE_X] = out_w;
527         int out_h = edl->session->output_h;
528         automation_mins[AUTOGROUPTYPE_Y] = -out_h;
529         automation_maxs[AUTOGROUPTYPE_Y] = out_h;
530
531 }
532