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