3 * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
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.
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.
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
27 #include "apatchgui.h"
28 #include "arraylist.h"
31 #include "audiodevice.inc"
33 #include "awindow.inc"
34 #include "batchrender.inc"
35 #include "bcwindowbase.inc"
36 #include "bdcreate.inc"
37 #include "brender.inc"
39 #include "channel.inc"
40 #include "channeldb.inc"
41 #include "commercials.inc"
42 #include "convert.inc"
43 #include "cwindow.inc"
45 #include "devicedvbinput.inc"
46 #include "devicempeginput.inc"
47 #include "dvdcreate.inc"
50 #include "edlsession.inc"
51 #include "exportedl.inc"
52 #include "filesystem.inc"
53 #include "filexml.inc"
54 #include "framecache.inc"
55 #include "gwindow.inc"
56 #include "indexable.inc"
57 #include "keyframegui.inc"
58 #include "levelwindow.inc"
59 #include "loadmode.inc"
60 #include "mainerror.inc"
61 #include "mainindexes.inc"
62 #include "mainprogress.inc"
63 #include "mainsession.inc"
64 #include "mainundo.inc"
65 #include "maxchannels.h"
66 #include "mixersalign.inc"
68 #include "mwindow.inc"
69 #include "mwindowgui.inc"
71 #include "patchbay.inc"
72 #include "playback3d.inc"
73 #include "playbackengine.inc"
75 #include "pluginclient.inc"
76 #include "pluginfclient.inc"
77 #include "pluginserver.inc"
78 #include "pluginset.inc"
79 #include "preferences.inc"
80 #include "preferencesthread.inc"
83 #include "recordlabel.inc"
85 #include "sharedlocation.inc"
86 #include "sighandler.inc"
87 #include "splashgui.inc"
88 #include "shuttle.inc"
91 #include "threadloader.inc"
92 #include "timebar.inc"
93 #include "tipwindow.inc"
95 #include "tracking.inc"
97 #include "transition.inc"
98 #include "transportque.inc"
99 #include "videowindow.inc"
100 #include "vpatchgui.h"
101 #include "vwindow.inc"
102 #include "zwindow.inc"
103 #include "wwindow.inc"
104 #include "wavecache.inc"
107 #define FONT_SEARCHPATH "fonts"
118 class Stack : public ArrayList<StackItem>
124 #define BEEP_SAMPLE_RATE 48000
126 class Beeper : public Thread
129 typedef int16_t audio_data_t;
130 Beeper(MWindow *mwindow);
136 void tone(double freq, double secs, double gain);
139 double freq, secs, gain;
141 int playing_audio, interrupted;
146 class MWindow : public Thread
152 // ======================================== initialization commands
153 void create_objects(int want_gui,
162 int run_script(FileXML *script);
164 int delete_project(int flash = 1);
166 int restart() { return restart_status; }
170 int set_filename(const char *filename);
171 // Total vertical pixels in timeline
172 int get_tracks_height();
173 // Total horizontal pixels in timeline
174 int get_tracks_width();
176 void stack_push(EDL *edl, Indexable *idxbl);
178 void clip_to_media();
179 void media_to_clip();
180 int create_ref(Asset *asset, EDL *ref);
188 void restore_windows();
189 void load_layout(const char *layout);
190 void save_layout(const char *layout);
191 void delete_layout(const char *layout);
192 int tile_windows(int window_config);
193 char *get_cwindow_display();
194 void set_screens(int value);
195 int asset_to_edl(EDL *new_edl,
197 RecordLabels *labels = 0);
199 // Entry point to insert assets and insert edls. Called by TrackCanvas
200 // and AssetPopup when assets are dragged in from AWindow.
201 // Takes the drag vectors from MainSession and
202 // pastes either assets or clips depending on which is full.
203 // Returns 1 if the vectors were full
204 int paste_assets(double position, Track *dest_track, int overwrite);
206 // Insert the assets at a point in the EDL. Called by menueffects,
207 // render, and CWindow drop but recording calls paste_edls directly for
209 void load_assets(ArrayList<Indexable*> *new_assets,
212 Track *first_track /* = 0 */,
213 RecordLabels *labels /* = 0 */,
218 int paste_edls(ArrayList<EDL*> *new_edls,
220 Track *first_track /* = 0 */,
221 double current_position /* = -1 */,
226 // Reset everything for a load
227 void update_project(int load_mode);
228 void update_preferences(Preferences *prefs);
229 void update_vwindow();
230 // Fit selected time to horizontal display range
231 void fit_selection();
232 // Fit selected autos to the vertical display range
233 void fit_autos(int all);
234 void change_currentautorange(int autogrouptype, int increment, int changemax);
235 void expand_autos(int changeall, int domin, int domax);
236 void shrink_autos(int changeall, int domin, int domax);
237 // move the window to include the cursor
239 // Search plugindb and put results in argument
240 static void search_plugindb(int do_audio,
245 ArrayList<PluginServer*> &results);
246 // Find the plugin whose title matches title and return it
247 static PluginServer* scan_plugindb(char *title,
249 static void fix_plugin_title(char *title);
250 static int plugin_exists(const char *plugin_path, ArrayList<PluginServer*> &plugins);
251 static int plugin_exists(char *plugin_path);
252 void dump_plugindb(FILE *fp);
253 void stop_playback(int wait);
254 void stop_transport();
255 void undo_before(const char *description = "", void *creator = 0);
256 void undo_after(const char *description, uint32_t load_flags, int changes_made = 1);
258 void handle_mixers(EDL *edl, int command, int wait_tracking,
259 int use_inout, int toggle_audio, int loop_play, float speed);
260 ZWindow *create_mixer(Indexable *indexable, double position);
261 void create_mixers(double position = 0);
262 void refresh_mixers(int dir=1);
264 void close_mixers(int result=1);
266 ZWindow *get_mixer(Mixer *&mixer);
267 void del_mixer(ZWindow *zwindow);
268 int mixer_track_active(Track *track);
269 void update_mixer_tracks();
271 int select_zwindow(ZWindow *zwindow);
273 int load_filenames(ArrayList<char*> *filenames,
274 int load_mode = LOADMODE_REPLACE,
275 int edl_mode = LOADMODE_EDL_CLIP,
276 // Cause the project filename on the top of the window to be updated.
277 // Not wanted for loading backups.
278 int update_filename = 1);
280 // Print out plugins which are referenced in the EDL but not loaded.
281 void test_plugins(EDL *new_edl, const char *path);
283 int interrupt_indexes(); // Stop index building
285 int redraw_time_dependancies(); // after reconfiguring the time format, sample rate, frame rate
287 // =========================================== movement
289 void next_time_format();
290 void prev_time_format();
291 void time_format_common();
292 int reposition_timebar(int new_pixel, int new_height);
294 int zoom_in_sample();
295 int zoom_sample(int64_t zoom_sample);
296 void zoom_autos(float min, float max);
297 void zoom_amp(int64_t zoom_amp);
298 void zoom_track(int64_t zoom_track);
300 int move_left(int64_t distance = 0);
301 int move_right(int64_t distance = 0);
302 void move_up(int64_t distance = 0);
303 void move_down(int64_t distance = 0);
304 int find_selection(double position, int scroll_display = 0);
305 void toggle_camera_xyz();
306 void toggle_projector_xyz();
307 double get_position();
308 void set_position(double position);
311 // shift_down must be passed by the caller because different windows call
313 int next_label(int shift_down);
314 int prev_label(int shift_down);
315 // seek to edit handles
316 int next_edit_handle(int shift_down);
317 int prev_edit_handle(int shift_down);
319 int nearest_plugin_keyframe(int shift_down, int dir);
320 // offset is pixels to add to track_start
321 void trackmovement(int offset, int pane_number);
322 // view_start is pixels
323 int samplemovement(int64_t view_start, int pane_number);
327 int goto_position(double position);
334 void crop_video(int mode);
335 void update_plugins();
336 void get_backup_path(char *path, int len);
337 // Call after every edit operation
340 void show_plugin(Plugin *plugin);
341 void hide_plugin(Plugin *plugin, int lock);
343 void delete_plugin(PluginServer *plugin);
344 // Update plugins with configuration changes.
345 // Called by TrackCanvas::cursor_motion_event.
346 void update_plugin_guis(int do_keyframe_guis = 1);
347 void update_plugin_states();
348 void update_plugin_titles();
349 // Called by Attachmentpoint during playback.
350 // Searches for matching plugin and renders data in it.
351 void render_plugin_gui(void *data, Plugin *plugin);
352 void render_plugin_gui(void *data, int size, Plugin *plugin);
353 void reset_plugin_gui_frames(Plugin *plugin);
354 void render_plugin_gui_frames(PluginClientFrames *frames, Plugin *plugin);
355 double get_tracking_position();
356 int get_tracking_direction();
358 // Called from PluginVClient::process_buffer
359 // Returns 1 if a GUI for the plugin is open so OpenGL routines can determine if
361 int plugin_gui_open(Plugin *plugin);
362 void stop_plugin_guis();
364 void show_keyframe_gui(Plugin *plugin);
365 void hide_keyframe_guis();
366 void hide_keyframe_gui(Plugin *plugin);
367 void update_keyframe_guis();
368 int get_hash_color(Edit *edit);
369 int get_hash_color(int v);
370 int get_group_color(int v);
371 int get_title_color(Edit *edit);
373 // ============================= editing commands ========================
375 // Map each recordable audio track to the desired pattern
376 void map_audio(int pattern);
377 void remap_audio(int pattern);
383 void add_audio_track_entry(int above, Track *dst);
384 int add_audio_track(int above, Track *dst);
385 void add_clip_to_edl(EDL *edl);
386 void add_video_track_entry(Track *dst = 0);
387 int add_video_track(int above, Track *dst);
388 void add_subttl_track_entry(Track *dst = 0);
389 int add_subttl_track(int above, Track *dst);
392 void asset_to_size();
393 void asset_to_rate();
394 // Entry point for clear operations.
396 // Clears active region in EDL.
397 // If clear_handle, edit boundaries are cleared if the range is 0.
398 // Called by paste, record, menueffects, render, and CWindow drop.
399 void clear(int clear_handle);
401 int clear_labels(double start, double end);
402 void clear_hard_edges();
403 int clear_hard_edges(double start, double end);
405 void concatenate_tracks();
406 int copy_flags(int copy_flags=COPY_CLIPBOARD);
408 int copy(double start, double end);
410 void blade(double position);
411 void cut(double start, double end, double new_position=-1);
412 // cut edit from current position to handle/label
413 void cut_left_edit();
414 void cut_right_edit();
415 void cut_left_label();
416 void cut_right_label();
418 // Calculate aspect ratio from pixel counts
419 static int create_aspect_ratio(float &w, float &h, int width, int height);
420 // Calculate defaults path
421 static void create_defaults_path(char *string, const char *config_file);
423 void delete_track(Track *track);
424 void delete_tracks();
425 int feather_edits(int64_t feather_samples, int audio, int video);
426 int64_t get_feather(int audio, int video);
427 float get_aspect_ratio();
428 void insert(double position, FileXML *file,
429 int edit_labels, int edit_plugins, int edit_autos,
430 EDL *parent_edl, Track *first_track, int overwrite);
432 // TrackCanvas calls this to insert multiple effects from the drag_pluginservers
433 // into pluginset_highlighted.
434 void insert_effects_canvas(double start, double length);
436 // CWindow calls this to insert multiple effects from
437 // the drag_pluginservers array.
438 void insert_effects_cwindow(Track *dest_track);
440 // Attach new effect to all recordable tracks
441 // single_standalone - attach 1 standalone on the first track and share it with
443 void insert_effect(char *title,
444 SharedLocation *shared_location,
445 int data_type, int plugin_type, int single_standalone);
447 // This is called multiple times by the above functions.
448 // It can't sync parameters.
449 void insert_effect(char *title,
450 SharedLocation *shared_location,
451 Track *track, PluginSet *plugin_set,
452 double start, double length, int plugin_type);
454 void match_output_size(Track *track);
455 void delete_edit(Edit *edit, const char *msg, int collapse=0);
456 void delete_edits(ArrayList<Edit*> *edits, const char *msg, int collapse=0);
457 void delete_edits(int collapse=0);
458 void cut_selected_edits(int collapse, int packed);
459 // Move edit to new position
460 void move_edits(ArrayList<Edit*> *edits, Track *track, double position,
461 int mode); // mode: 0 - mute and overwrite, 1 - cut and paste
462 void selected_edits_to_clipboard(int packed);
463 void paste_clipboard(Track *first_track, double position, int overwrite,
464 int edit_edits, int edit_labels, int edit_autos, int edit_plugins);
465 void move_group(EDL *group, Track *first_track, double position, int overwrite);
466 // Move effect to position
467 void move_effect(Plugin *plugin, Track *track, int64_t position);
468 void move_effect(Plugin *plugin, PluginSet *plugin_set, int64_t position);
469 void move_plugins_up(PluginSet *plugin_set);
470 void move_plugins_down(PluginSet *plugin_set);
471 void move_track_down(Track *track);
472 void move_tracks_down();
473 void move_track_up(Track *track);
474 void move_tracks_up();
475 void mute_selection();
476 void new_folder(const char *new_folder, int is_clips);
477 void delete_folder(char *folder);
478 // For clipboard commands
480 void paste(double start, Track *first_track, int clear_selection, int overwrite);
481 // For splice and overwrite
482 void overwrite(EDL *source, int all);
483 void splice(EDL *source, int all);
484 int paste(double start, double end, FileXML *file,
485 int edit_labels, int edit_plugins, int edit_autos,
486 Track *first_track, int overwrite);
487 int paste_output(int64_t startproject, int64_t endproject,
488 int64_t startsource_sample, int64_t endsource_sample,
489 int64_t startsource_frame, int64_t endsource_frame,
490 Asset *asset, RecordLabels *new_labels);
491 void paste_silence();
493 // Detach single transition
494 void detach_transition(Transition *transition);
495 // Detach all transitions in selection
496 void detach_transitions();
497 // Attach dragged transition
498 void paste_transition();
499 // Attach transition to all edits in selection
500 void paste_transitions(int track_type, char *title);
501 // Attach transition dragged onto CWindow
502 void paste_transition_cwindow(Track *dest_track);
503 // Attach default transition to single edit
504 void paste_audio_transition();
505 void paste_video_transition();
506 void shuffle_edits();
507 void reverse_edits();
509 void set_edit_length(double length);
510 // Set length of single transition
511 void set_transition_length(Transition *transition, double length);
512 // Set length in seconds of all transitions in active range
513 void set_transition_length(double length);
515 void remove_indexfile(Indexable *indexable);
516 void rebuild_indices();
517 // Asset removal from caches
519 void remove_from_caches(Indexable *idxbl);
520 void remove_assets_from_project(int push_undo, int redraw, int delete_indexes,
521 ArrayList<Indexable*> *drag_assets /* mwindow->session->drag_assets */,
522 ArrayList<EDL*> *drag_clips /* mwindow->session->drag_clips */);
523 void remove_assets_from_disk();
524 void resize_track(Track *track, int w, int h);
526 void set_automation_mode(int mode);
527 void set_keyframe_type(int mode);
528 void set_auto_keyframes(int value);
529 void set_span_keyframes(int value);
530 void set_auto_visibility(Autos *autos, int value);
531 void set_labels_follow_edits(int value);
533 // Update the editing mode
534 int set_editing_mode(int new_editing_mode);
535 void toggle_editing_mode();
538 void unset_inoutpoint();
539 void toggle_loop_playback();
540 void trim_selection();
541 // Synchronize EDL settings with all playback engines depending on current
542 // operation. Doesn't redraw anything.
543 void sync_parameters(int change_type = CHANGE_PARAMS);
544 void save_clip(EDL *new_edl, const char *txt);
545 void to_clip(EDL *edl, const char *txt, int all);
547 void undo_entry(BC_WindowBase *calling_window_gui);
548 void redo_entry(BC_WindowBase *calling_window_gui);
549 void save_undo_data();
550 void load_undo_data();
551 int copy_target(const char *path, const char *target);
552 int link_target(const char *real_path, const char *link_path, int relative);
553 void save_project(const char *dir, int save_mode, int overwrite, int reload);
555 int cut_automation();
556 int copy_automation();
557 int paste_automation();
558 void clear_automation();
559 int cut_default_keyframe();
560 int copy_default_keyframe();
561 // Use paste_automation to paste the default keyframe in other position.
562 // Use paste_default_keyframe to replace the default keyframe with whatever is
564 int paste_default_keyframe();
565 int clear_default_keyframe();
567 FloatAuto* get_float_auto(PatchGUI *patch,int idx);
568 IntAuto* get_int_auto(PatchGUI *patch,int idx);
569 PanAuto* get_pan_auto(PatchGUI *patch);
570 PatchGUI *get_patchgui(Track *track);
572 int modify_edithandles();
573 int modify_pluginhandles();
574 void finish_modify_handles();
575 void rescale_proxy(EDL *clip, int orig_scale, int new_scale);
576 void add_proxy(ArrayList<Indexable*> *orig_assets,
577 ArrayList<Indexable*> *proxy_assets);
578 int render_proxy(ArrayList<Indexable *> &new_idxbls);
579 void beep(double freq, double secs, double gain);
582 int to_proxy(Asset *asset, int new_scale, int new_use_scaler);
585 void dump_plugins(FILE *fp=stdout);
586 void dump_edl(FILE *fp=stdout);
587 void dump_undo(FILE *fp=stdout);
588 void dump_exe(FILE *fp=stdout);
589 static void trap_hook(FILE *fp, void *vp);
591 void reset_android_remote();
593 // Send new EDL to caches
595 int optimize_assets(); // delete unused assets from the cache and assets
596 // render edl assets to specified format, then replace in edl
597 void start_convert(Asset *format_asset, const char *suffix,
598 float beep, int remove_originals);
599 void finish_convert(int remove_originals);
600 ConvertRender *convert_render;
602 void select_point(double position);
603 int set_loop_boundaries(); // toggle loop playback and set boundaries for loop playback
606 Playback3D *playback_3d;
607 SplashGUI *splash_window;
617 // CICaches for drawing timeline only
618 CICache *audio_cache, *video_cache;
619 // Frame cache for drawing timeline only.
620 // Cache drawing doesn't wait for file decoding.
621 FrameCache *frame_cache;
622 WaveCache *wave_cache;
623 Preferences *preferences;
624 PreferencesThread *preferences_thread;
625 MainSession *session;
627 MainIndexes *mainindexes;
628 MainProgress *mainprogress;
632 const char *default_standard;
633 static Commercials *commercials;
634 int commercial_active;
635 int has_commercials();
636 // copy of edl created in speed_before, used in speed_after to normalize_speed
640 ArrayList<ColormodelItem*> colormodels;
641 ArrayList<InterlacemodeItem*> interlace_project_modes;
642 ArrayList<InterlacemodeItem*> interlace_asset_modes;
647 int select_asset(Asset *asset, int vstream, int astream, int delete_tracks);
648 int select_asset(int vtrack, int delete_tracks);
650 // Channel DB for playback only. Record channel DB's are in record.C
651 ChannelDB *channeldb_buz;
652 ChannelDB *channeldb_v4l2jpeg;
654 // ====================================== plugins ==============================
656 // Contains file descriptors for all the dlopens
657 static ArrayList<PluginServer*> *plugindb;
658 // Currently visible plugins
659 int64_t plugin_visibility;
660 PluginGUIs *plugin_guis;
661 // GUI Plugins to delete
662 ArrayList<PluginServer*> *dead_plugins;
664 ArrayList<KeyFrameThread*> *keyframe_threads;
666 // Adjust sample position to line up with frames.
667 int fix_timing(int64_t &samples_out,
672 CreateBD_Thread *create_bd;
673 CreateDVD_Thread *create_dvd;
674 BatchRenderThread *batch_render;
677 ExportEDL *exportedl;
687 Mutex *vwindows_lock;
688 ArrayList<VWindow*> vwindows;
690 Mutex *zwindows_lock;
691 ArrayList<ZWindow*> zwindows;
692 MixersAlign *mixers_align;
702 void show_warning(int *do_warning, const char *text);
705 LevelWindow *lwindow;
707 // Lock during creation and destruction of GUI
708 Mutex *plugin_gui_lock;
709 Mutex *dead_plugin_lock;
710 Mutex *keyframe_gui_lock;
711 // Lock during creation and destruction of brender so playback doesn't use it.
713 // Initialize shared memory
714 void init_shm(const char *pfn, int64_t min);
715 // Initialize channel DB's for playback
716 void init_channeldb();
718 void init_exportedl();
719 // These three happen synchronously with each other
720 // Make sure this is called after synchronizing EDL's.
722 // Restart brender after testing its existence
723 void restart_brender();
724 // Stops brender after testing its existence
726 // This one happens asynchronously of the others. Used by playback to
727 // see what frame is background rendered.
728 int brender_available(int position);
729 void set_brender_active(int v, int update=1);
730 int put_commercial();
731 void activate_commercial() { commercial_active = 1; }
732 void commit_commercial();
733 void undo_commercial();
734 void cut_commercials();
735 void update_gui(int changed_edl);
736 int paste_subtitle_text(char *text, double start, double end);
740 static void init_defaults(BC_Hash* &defaults, char *config_path);
741 void check_language();
742 const char *default_std();
743 void fill_preset_defaults(const char *preset, EDLSession *session);
744 const char *get_preset_name(int index);
748 void init_tipwindow();
749 // Used by MWindow and RenderFarmClient
750 static void get_plugin_path(char *path, const char *plug_dir, const char *fs_path);
751 static int init_plugins(MWindow *mwindow, Preferences *preferences);
752 static int init_ladspa_plugins(MWindow *mwindow, Preferences *preferences);
753 static void init_plugin_tips(ArrayList<PluginServer*> &plugins, const char *lang);
754 static int check_plugin_index(ArrayList<PluginServer*> &plugins,
755 const char *plug_dir, const char *plug_path);
756 static void init_plugin_index(MWindow *mwindow, Preferences *preferences,
757 FILE *fp, const char *plugin_dir);
758 static int init_ladspa_index(MWindow *mwindow, Preferences *preferences,
759 FILE *fp, const char *plugin_dir);
760 static void scan_plugin_index(MWindow *mwindow, Preferences *preferences,
761 FILE *fp, const char *plug_dir, const char *plug_path, int &idx);
762 static void init_ffmpeg();
763 static void init_ffmpeg_index(MWindow *mwindow, Preferences *preferences, FILE *fp);
764 static int load_plugin_index(MWindow *mwindow, FILE *fp, const char *plugin_dir);
765 static PluginServer *new_ffmpeg_server(MWindow *mwindow, const char *name);
766 static int init_lv2_index(MWindow *mwindow, Preferences *preferences, FILE *fp);
767 static PluginServer *new_lv2_server(MWindow *mwindow, const char *name);
768 static void remove_plugin_index();
770 void init_preferences();
775 void init_compositor();
776 void init_levelwindow();
777 // Called when creating a new viewer to view footage
778 VWindow* get_viewer(int start_it, int idx=-1);
784 void init_playbackcursor();
785 void init_commercials();
786 static void add_plugins(ArrayList<PluginServer*> &plugins);
787 static void delete_plugins();
789 int speed_after(int done);
790 int normalize_speed(EDL *old_edl, EDL *new_edl);
792 void clean_indexes();
793 // TimeBomb timebomb;
794 SigHandler *sighandler;