tweak zoom/fullscr to remember cwdw scale after fullscr
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / menueffects.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2010 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 "asset.h"
23 #include "awindow.h"
24 #include "awindowgui.h"
25 #include "clip.h"
26 #include "confirmsave.h"
27 #include "bchash.h"
28 #include "edl.h"
29 #include "edlsession.h"
30 #include "errorbox.h"
31 #include "file.h"
32 #include "formatcheck.h"
33 #include "indexfile.h"
34 #include "keyframe.h"
35 #include "keys.h"
36 #include "labels.h"
37 #include "language.h"
38 #include "loadmode.h"
39 #include "localsession.h"
40 #include "mainmenu.h"
41 #include "mainsession.h"
42 #include "mainundo.h"
43 #include "mwindow.h"
44 #include "mwindowgui.h"
45 #include "menueffects.h"
46 #include "playbackengine.h"
47 #include "pluginarray.h"
48 #include "pluginserver.h"
49 #include "preferences.h"
50 #include "render.h"
51 #include "sighandler.h"
52 #include "theme.h"
53 #include "tracks.h"
54
55
56
57 MenuEffects::MenuEffects(MWindow *mwindow)
58  : BC_MenuItem(_("Render effect..."))
59 {
60         this->mwindow = mwindow;
61 }
62
63 MenuEffects::~MenuEffects()
64 {
65 }
66
67
68 int MenuEffects::handle_event()
69 {
70         thread->set_title("");
71         thread->start();
72         return 1;
73 }
74
75
76
77
78
79 MenuEffectPacket::MenuEffectPacket(char *path, int64_t start, int64_t end)
80 {
81         this->start = start;
82         this->end = end;
83         strcpy(this->path, path);
84 }
85
86 MenuEffectPacket::~MenuEffectPacket()
87 {
88 }
89
90
91
92
93
94
95 MenuEffectThread::MenuEffectThread(MWindow *mwindow, MenuEffects *menu_item)
96 {
97         this->mwindow = mwindow;
98         this->menu_item = menu_item;
99         title[0] = 0;
100         dead_plugins = new ArrayList<PluginServer*>;
101 }
102
103 MenuEffectThread::~MenuEffectThread()
104 {
105         delete dead_plugins;
106 }
107
108
109
110
111
112 int MenuEffectThread::set_title(const char *title)
113 {
114         strcpy(this->title, title);
115         return 0;
116 }
117
118 // for recent effect menu items and running new effects
119 // prompts for an effect if title is blank
120 void MenuEffectThread::run()
121 {
122         for(int i = 0; i < dead_plugins->size(); i++)
123         {
124                 delete dead_plugins->get(i);
125         }
126         dead_plugins->remove_all();
127
128
129
130
131 // get stuff from main window
132         ArrayList<PluginServer*> *plugindb = mwindow->plugindb;
133         BC_Hash *defaults = mwindow->defaults;
134         ArrayList<BC_ListBoxItem*> plugin_list;
135         ArrayList<PluginServer*> local_plugindb;
136         char string[1024];
137         int result = 0;
138 // Default configuration
139         Asset *default_asset = new Asset;
140 // Output
141         ArrayList<Indexable*> assets;
142
143
144 // check for recordable tracks
145         if(!get_recordable_tracks(default_asset))
146         {
147                 sprintf(string, _("No recordable tracks specified."));
148                 ErrorBox error(_(PROGRAM_NAME ": Error"));
149                 error.create_objects(string);
150                 error.run_window();
151                 default_asset->Garbage::remove_user();
152                 return;
153         }
154
155 // check for plugins
156         if(!plugindb->total)
157         {
158                 sprintf(string, _("No plugins available."));
159                 ErrorBox error(_(PROGRAM_NAME ": Error"));
160                 error.create_objects(string);
161                 error.run_window();
162                 default_asset->Garbage::remove_user();
163                 return;
164         }
165
166
167 // get default attributes for output file
168 // used after completion
169         get_derived_attributes(default_asset, defaults);
170 //      to_tracks = defaults->get("RENDER_EFFECT_TO_TRACKS", 1);
171         load_mode = defaults->get("RENDER_EFFECT_LOADMODE", LOADMODE_PASTE);
172         use_labels = defaults->get("RENDER_FILE_PER_LABEL", 0);
173
174 // get plugin information
175         int need_plugin = !strlen(title) ? 1 : 0;
176 // generate a list of plugins for the window
177         if( need_plugin ) {
178                 mwindow->search_plugindb(default_asset->audio_data,
179                         default_asset->video_data, -1, 0, 0, local_plugindb);
180                 for(int i = 0; i < local_plugindb.total; i++) {
181                         plugin_list.append(new BC_ListBoxItem(_(local_plugindb.values[i]->title)));
182                 }
183         }
184
185 // find out which effect to run and get output file
186         int plugin_number, format_error = 0;
187
188         do
189         {
190                 {
191                         MenuEffectWindow window(mwindow,
192                                 this,
193                                 need_plugin ? &plugin_list : 0,
194                                 default_asset);
195                         window.create_objects();
196                         result = window.run_window();
197                         plugin_number = window.result;
198                 }
199
200                 if(!result)
201                 {
202                         FormatCheck format_check(default_asset);
203                         format_error = format_check.check_format();
204                 }
205         }while(format_error && !result);
206
207 // save defaults
208         save_derived_attributes(default_asset, defaults);
209         defaults->update("RENDER_EFFECT_LOADMODE", load_mode);
210         defaults->update("RENDER_EFFECT_FILE_PER_LABEL", use_labels);
211         mwindow->save_defaults();
212
213 // get plugin server to use and delete the plugin list
214         PluginServer *plugin_server = 0;
215         PluginServer *plugin = 0;
216         if(need_plugin)
217         {
218                 plugin_list.remove_all_objects();
219                 if(plugin_number > -1)
220                 {
221                         plugin_server = local_plugindb.values[plugin_number];
222                         strcpy(title, plugin_server->title);
223                 }
224         }
225         else
226         {
227                 for(int i = 0; i < plugindb->total && !plugin_server; i++)
228                 {
229                         if(!strcmp(plugindb->values[i]->title, title))
230                         {
231                                 plugin_server = plugindb->values[i];
232                                 plugin_number = i;
233                         }
234                 }
235         }
236
237 // Update the  most recently used effects and copy the plugin server.
238         if(plugin_server)
239         {
240                 plugin = new PluginServer(*plugin_server);
241                 fix_menu(title);
242         }
243
244         if(!result && !strlen(default_asset->path))
245         {
246                 result = 1;        // no output path given
247                 ErrorBox error(_(PROGRAM_NAME ": Error"));
248                 error.create_objects(_("No output file specified."));
249                 error.run_window();
250         }
251
252         if(!result && plugin_number < 0)
253         {
254                 result = 1;        // no output path given
255                 ErrorBox error(_(PROGRAM_NAME ": Error"));
256                 error.create_objects(_("No effect selected."));
257                 error.run_window();
258         }
259
260 // Configuration for realtime plugins.
261         KeyFrame plugin_data;
262
263 // get selection to render
264 // Range
265         double total_start, total_end;
266
267         total_start = mwindow->edl->local_session->get_selectionstart();
268
269
270         if(mwindow->edl->local_session->get_selectionend() ==
271                 mwindow->edl->local_session->get_selectionstart())
272                 total_end = mwindow->edl->tracks->total_length();
273         else
274                 total_end = mwindow->edl->local_session->get_selectionend();
275
276
277
278 // get native units for range
279         total_start = to_units(total_start, 0);
280         total_end = to_units(total_end, 1);
281
282
283
284 // Trick boundaries in case of a non-realtime synthesis plugin
285         if(plugin &&
286                 !plugin->realtime &&
287                 total_end == total_start) total_end = total_start + 1;
288
289 // Units are now in the track's units.
290         int64_t total_length = (int64_t)total_end - (int64_t)total_start;
291 // length of output file
292
293         if(!result && total_length <= 0)
294         {
295                 result = 1;        // no output path given
296                 ErrorBox error(_(PROGRAM_NAME ": Error"));
297                 error.create_objects(_("No selected range to process."));
298                 error.run_window();
299         }
300
301 // ========================= get keyframe from user
302         if(!result)
303         {
304 // ========================= realtime plugin
305 // no get_parameters
306                 if(plugin->realtime)
307                 {
308 // Open a prompt GUI
309                         MenuEffectPrompt prompt(mwindow);
310                         prompt.create_objects();
311                         char title[BCTEXTLEN];
312                         sprintf(title, _(PROGRAM_NAME ": %s"), plugin->title);
313
314 // Open the plugin GUI
315                         plugin->set_mwindow(mwindow);
316                         plugin->set_keyframe(&plugin_data);
317                         plugin->set_prompt(&prompt);
318                         plugin->open_plugin(0, mwindow->preferences, mwindow->edl, 0);
319 // Must set parameters since there is no plugin object to draw from.
320                         plugin->get_parameters((int64_t)total_start,
321                                 (int64_t)total_end,
322                                 1);
323                         plugin->show_gui();
324
325 // wait for user input
326                         result = prompt.run_window();
327
328 // Close plugin.
329                         plugin->save_data(&plugin_data);
330                         plugin->hide_gui();
331
332 // Can't delete here.
333                         dead_plugins->append(plugin);
334                         default_asset->sample_rate = mwindow->edl->session->sample_rate;
335                         default_asset->frame_rate = mwindow->edl->session->frame_rate;
336                         realtime = 1;
337                 }
338                 else
339 // ============================non realtime plugin
340                 {
341                         plugin->set_mwindow(mwindow);
342                         plugin->open_plugin(0, mwindow->preferences, mwindow->edl, 0);
343                         result = plugin->get_parameters((int64_t)total_start,
344                                 (int64_t)total_end,
345                                 get_recordable_tracks(default_asset));
346 // some plugins can change the sample rate and the frame rate
347
348
349                         if(!result)
350                         {
351                                 default_asset->sample_rate = plugin->get_samplerate();
352                                 default_asset->frame_rate = plugin->get_framerate();
353                         }
354                         delete plugin;
355                         realtime = 0;
356                 }
357
358 // Should take from first recordable track
359                 default_asset->width = mwindow->edl->session->output_w;
360                 default_asset->height = mwindow->edl->session->output_h;
361         }
362
363         int strategy = Render::get_strategy(mwindow->preferences->use_renderfarm, use_labels);
364 // Process the total length in fragments
365         ArrayList<MenuEffectPacket*> packets;
366         if(!result)
367         {
368                 Label *current_label = mwindow->edl->labels->first;
369                 mwindow->stop_brender();
370
371                 int current_number;
372                 int number_start;
373                 int total_digits;
374                 Render::get_starting_number(default_asset->path,
375                         current_number,
376                         number_start,
377                         total_digits);
378
379
380
381 // Construct all packets for single overwrite confirmation
382                 for(int64_t fragment_start = (int64_t)total_start, fragment_end;
383                         fragment_start < (int64_t)total_end;
384                         fragment_start = fragment_end)
385                 {
386 // Get fragment end
387                         if(strategy == FILE_PER_LABEL || strategy == FILE_PER_LABEL_FARM)
388                         {
389                                 while(current_label  &&
390                                         to_units(current_label->position, 0) <= fragment_start)
391                                         current_label = current_label->next;
392                                 if(!current_label)
393                                         fragment_end = (int64_t)total_end;
394                                 else
395                                         fragment_end = to_units(current_label->position, 0);
396                         }
397                         else
398                         {
399                                 fragment_end = (int64_t)total_end;
400                         }
401
402 // Get path
403                         char path[BCTEXTLEN];
404                         if(strategy == FILE_PER_LABEL || strategy == FILE_PER_LABEL_FARM)
405                                 Render::create_filename(path,
406                                         default_asset->path,
407                                         current_number,
408                                         total_digits,
409                                         number_start);
410                         else
411                                 strcpy(path, default_asset->path);
412                         current_number++;
413
414                         MenuEffectPacket *packet = new MenuEffectPacket(path,
415                                 fragment_start,
416                                 fragment_end);
417                         packets.append(packet);
418                 }
419
420
421 // Test existence of files
422                 ArrayList<char*> paths;
423                 for(int i = 0; i < packets.total; i++)
424                 {
425                         paths.append(packets.values[i]->path);
426                 }
427                 result = ConfirmSave::test_files(mwindow, &paths);
428                 paths.remove_all();
429         }
430
431
432
433         for(int current_packet = 0;
434                 current_packet < packets.total && !result;
435                 current_packet++)
436         {
437                 Asset *asset = new Asset(*default_asset);
438                 MenuEffectPacket *packet = packets.values[current_packet];
439                 int64_t fragment_start = packet->start;
440                 int64_t fragment_end = packet->end;
441                 strcpy(asset->path, packet->path);
442
443                 assets.append(asset);
444                 File *file = new File;
445
446 // Open the output file after getting the information because the sample rate
447 // is needed here.
448                 if(!result)
449                 {
450 // open output file in write mode
451                         file->set_processors(mwindow->preferences->processors);
452                         if(file->open_file(mwindow->preferences,
453                                 asset,
454                                 0,
455                                 1))
456                         {
457 // open failed
458                                 sprintf(string, _("Couldn't open %s"), asset->path);
459                                 ErrorBox error(_(PROGRAM_NAME ": Error"));
460                                 error.create_objects(string);
461                                 error.run_window();
462                                 result = 1;
463                         }
464                         else
465                         {
466                                 mwindow->sighandler->push_file(file);
467                                 IndexFile::delete_index(mwindow->preferences,
468                                         asset);
469                         }
470                 }
471
472 // run plugins
473                 if(!result)
474                 {
475 // position file
476                         PluginArray *plugin_array;
477                         plugin_array = create_plugin_array();
478
479                         plugin_array->start_plugins(mwindow,
480                                 mwindow->edl,
481                                 plugin_server,
482                                 &plugin_data,
483                                 fragment_start,
484                                 fragment_end,
485                                 file);
486                         plugin_array->run_plugins();
487
488                         plugin_array->stop_plugins();
489                         mwindow->sighandler->pull_file(file);
490                         file->close_file();
491                         asset->audio_length = file->asset->audio_length;
492                         asset->video_length = file->asset->video_length;
493                         delete plugin_array;
494                 }
495
496                 delete file;
497         }
498
499         packets.remove_all_objects();
500
501 // paste output to tracks
502         if(!result && load_mode != LOADMODE_NOTHING)
503         {
504                 mwindow->gui->lock_window("MenuEffectThread::run");
505
506                 mwindow->undo->update_undo_before("", 0);
507                 if(load_mode == LOADMODE_PASTE)
508                         mwindow->clear(0);
509
510                 mwindow->load_assets(&assets, -1, load_mode, 0, 0,
511                         mwindow->edl->session->labels_follow_edits,
512                         mwindow->edl->session->plugins_follow_edits,
513                         mwindow->edl->session->autos_follow_edits,
514                         0); // overwrite
515
516                 mwindow->save_backup();
517                 mwindow->undo->update_undo_after(title, LOAD_ALL);
518
519                 mwindow->restart_brender();
520                 mwindow->update_plugin_guis();
521                 mwindow->gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
522                 mwindow->sync_parameters(CHANGE_ALL);
523                 mwindow->gui->unlock_window();
524
525                 mwindow->awindow->gui->async_update_assets();
526         }
527
528         for(int i = 0; i < assets.total; i++)
529                 assets.values[i]->Garbage::remove_user();
530         assets.remove_all();
531
532         default_asset->Garbage::remove_user();
533 }
534
535
536
537
538
539
540
541 MenuEffectItem::MenuEffectItem(MenuEffects *menueffect, const char *string)
542  : BC_MenuItem(string)
543 {
544         this->menueffect = menueffect;
545 }
546 int MenuEffectItem::handle_event()
547 {
548         menueffect->thread->set_title(get_text());
549         menueffect->thread->start();
550         return 1;
551 }
552
553
554
555
556
557
558
559
560
561
562
563
564 MenuEffectWindow::MenuEffectWindow(MWindow *mwindow,
565         MenuEffectThread *menueffects,
566         ArrayList<BC_ListBoxItem*> *plugin_list,
567         Asset *asset)
568  : BC_Window(_(PROGRAM_NAME ": Render effect"),
569                 mwindow->gui->get_abs_cursor_x(1),
570                 mwindow->gui->get_abs_cursor_y(1) - mwindow->session->menueffect_h / 2,
571                 mwindow->session->menueffect_w,
572                 mwindow->session->menueffect_h,
573                 xS(580), yS(350), 1, 0, 1)
574 {
575         this->menueffects = menueffects;
576         this->plugin_list = plugin_list;
577         this->asset = asset;
578         this->mwindow = mwindow;
579         file_title = 0;
580         format_tools = 0;
581         loadmode = 0;
582 }
583
584 MenuEffectWindow::~MenuEffectWindow()
585 {
586         lock_window("MenuEffectWindow::~MenuEffectWindow");
587         delete format_tools;
588         unlock_window();
589 }
590
591
592
593 void MenuEffectWindow::create_objects()
594 {
595         int x, y;
596         result = -1;
597         mwindow->theme->get_menueffect_sizes(plugin_list ? 1 : 0);
598
599         lock_window("MenuEffectWindow::create_objects");
600 // only add the list if needed
601         if(plugin_list)
602         {
603                 add_subwindow(list_title = new BC_Title(mwindow->theme->menueffect_list_x,
604                         mwindow->theme->menueffect_list_y,
605                         _("Select an effect")));
606                 int ys5 = yS(5);
607                 add_subwindow(list = new MenuEffectWindowList(this,
608                         mwindow->theme->menueffect_list_x,
609                         mwindow->theme->menueffect_list_y + list_title->get_h() + ys5,
610                         mwindow->theme->menueffect_list_w,
611                         mwindow->theme->menueffect_list_h - list_title->get_h() - ys5,
612                         plugin_list));
613         }
614
615         add_subwindow(file_title = new BC_Title(
616                 mwindow->theme->menueffect_file_x,
617                 mwindow->theme->menueffect_file_y,
618                 (char*)(menueffects->use_labels ?
619                         _("Select the first file to render to:") :
620                         _("Select a file to render to:"))));
621
622         x = mwindow->theme->menueffect_tools_x;
623         y = mwindow->theme->menueffect_tools_y;
624         format_tools = new FormatTools(mwindow,
625                                         this,
626                                         asset);
627         format_tools->create_objects(x, y, asset->audio_data, asset->video_data,
628                 0, 0, 0, 1, 0, 0, &menueffects->use_labels, 0);
629
630         loadmode = new LoadMode(mwindow, this, x, y, &menueffects->load_mode);
631         loadmode->create_objects();
632
633         add_subwindow(new MenuEffectWindowOK(this));
634         add_subwindow(new MenuEffectWindowCancel(this));
635         show_window();
636         unlock_window();
637 }
638
639 int MenuEffectWindow::resize_event(int w, int h)
640 {
641         mwindow->session->menueffect_w = w;
642         mwindow->session->menueffect_h = h;
643         mwindow->theme->get_menueffect_sizes(plugin_list ? 1 : 0);
644
645         if(plugin_list)
646         {
647                 list_title->reposition_window(mwindow->theme->menueffect_list_x,
648                         mwindow->theme->menueffect_list_y);
649                 int ys5 = yS(5);
650                 list->reposition_window(mwindow->theme->menueffect_list_x,
651                         mwindow->theme->menueffect_list_y + list_title->get_h() + ys5,
652                         mwindow->theme->menueffect_list_w,
653                         mwindow->theme->menueffect_list_h - list_title->get_h() - ys5);
654         }
655
656         if(file_title) file_title->reposition_window(mwindow->theme->menueffect_file_x,
657                 mwindow->theme->menueffect_file_y);
658         int x = mwindow->theme->menueffect_tools_x;
659         int y = mwindow->theme->menueffect_tools_y;
660         if(format_tools) format_tools->reposition_window(x, y);
661         if(loadmode) loadmode->reposition_window(x, y);
662         return 0;
663 }
664
665
666
667 MenuEffectWindowOK::MenuEffectWindowOK(MenuEffectWindow *window)
668  : BC_OKButton(window)
669 {
670         this->window = window;
671 }
672
673 int MenuEffectWindowOK::handle_event()
674 {
675         if(window->plugin_list)
676                 window->result = window->list->get_selection_number(0, 0);
677
678         window->set_done(0);
679         return 1;
680 }
681
682 int MenuEffectWindowOK::keypress_event()
683 {
684         if(get_keypress() == RETURN)
685         {
686                 handle_event();
687                 return 1;
688         }
689         return 0;
690 }
691
692 MenuEffectWindowCancel::MenuEffectWindowCancel(MenuEffectWindow *window)
693  : BC_CancelButton(window)
694 {
695         this->window = window;
696 }
697
698 int MenuEffectWindowCancel::handle_event()
699 {
700         window->set_done(1);
701         return 1;
702 }
703
704 int MenuEffectWindowCancel::keypress_event()
705 {
706         if(get_keypress() == ESC)
707         {
708                 handle_event();
709                 return 1;
710         }
711         return 0;
712 }
713
714 MenuEffectWindowList::MenuEffectWindowList(MenuEffectWindow *window,
715         int x,
716         int y,
717         int w,
718         int h,
719         ArrayList<BC_ListBoxItem*> *plugin_list)
720  : BC_ListBox(x, y, w, h, LISTBOX_TEXT, plugin_list)
721 {
722         this->window = window;
723 }
724
725 int MenuEffectWindowList::handle_event()
726 {
727         window->result = get_selection_number(0, 0);
728         window->set_done(0);
729         return 1;
730 }
731
732 #define PROMPT_TEXT _("Set up effect panel and hit \"OK\"")
733 #define MEP_W xS(260)
734 #define MEP_H yS(100)
735
736 MenuEffectPrompt::MenuEffectPrompt(MWindow *mwindow)
737  : BC_Window(_(PROGRAM_NAME ": Effect Prompt"),
738                 mwindow->gui->get_abs_cursor_x(1) - MEP_W/2,
739                 mwindow->gui->get_abs_cursor_y(1) - MEP_H/2,
740                 MenuEffectPrompt::calculate_w(mwindow->gui),
741                 MenuEffectPrompt::calculate_h(mwindow->gui),
742                 MenuEffectPrompt::calculate_w(mwindow->gui),
743                 MenuEffectPrompt::calculate_h(mwindow->gui),
744                 0, 0, 1)
745 {
746 }
747
748 int MenuEffectPrompt::calculate_w(BC_WindowBase *gui)
749 {
750         int w = BC_Title::calculate_w(gui, PROMPT_TEXT) + xS(10);
751         w = MAX(w, BC_OKButton::calculate_w() + BC_CancelButton::calculate_w() + xS(30));
752         return w;
753 }
754
755 int MenuEffectPrompt::calculate_h(BC_WindowBase *gui)
756 {
757         int h = BC_Title::calculate_h(gui, PROMPT_TEXT);
758         h += BC_OKButton::calculate_h() + yS(30);
759         return h;
760 }
761
762
763 void MenuEffectPrompt::create_objects()
764 {
765         lock_window("MenuEffectPrompt::create_objects");
766         int x = xS(10), y = yS(10);
767         BC_Title *title;
768         add_subwindow(title = new BC_Title(x, y, PROMPT_TEXT));
769         add_subwindow(new BC_OKButton(this));
770         add_subwindow(new BC_CancelButton(this));
771         show_window();
772         raise_window();
773         unlock_window();
774 }
775