no longer need ffmpeg patch0 which was for Termux
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / awindowgui.C
index dc884e55896a2d0729c7d07a58f15d7ab97dae87..dfe0163a25c488e5d37df1684902c68bc30a82f3 100644 (file)
@@ -2,6 +2,7 @@
 /*
  * CINELERRA
  * Copyright (C) 1997-2012 Adam Williams <broadcast at earthling dot net>
+ * Copyright (C) 2003-2016 Cinelerra CV contributors
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include "awindow.h"
 #include "awindowgui.h"
 #include "bccmodels.h"
-#include "bcsignals.h"
+#include "bcdisplayinfo.h"
 #include "bchash.h"
+#include "bcsignals.h"
+#include "bctimer.h"
 #include "binfolder.h"
 #include "cache.h"
 #include "cstrdup.h"
@@ -109,20 +112,24 @@ AssetVIcon::AssetVIcon(AssetPicon *picon, int w, int h, double framerate, int64_
        this->picon = picon;
        this->length = length;
        temp = 0;
+       broken = 0;
 }
 
 AssetVIcon::~AssetVIcon()
 {
+       if( picon->gui->vicon_thread->solo == this )
+               picon->gui->vicon_thread->solo = 0;
        picon->gui->vicon_thread->del_vicon(this);
        delete temp;
 }
 
 VFrame *AssetVIcon::frame()
 {
+       if( broken ) return 0;
        AssetVIconThread *avt = picon->gui->vicon_thread;
        Indexable *idxbl = picon->indexable;
        Asset *asset = idxbl && idxbl->is_asset ? (Asset *)idxbl : 0;
-       if( !asset ) return vframes()>0 ? (VFrame*)*images[0] : 0;
+       if( !asset ) return vframes()>0 ? images[0]->vfrm : 0;
        if( !asset->video_data && audio_data && audio_size && length > 0 ) {
                if( !temp ) temp = new VFrame(0, -1, w, h, BC_RGB888, -1);
                temp->clear_frame();
@@ -160,27 +167,36 @@ VFrame *AssetVIcon::frame()
        }
        if( seq_no >= images.size() ) {
                MWindow *mwindow = picon->mwindow;
-               File *file = mwindow->video_cache->check_out(asset, mwindow->edl, 1);
-               if( !file ) return 0;
                if( temp && (temp->get_w() != asset->width || temp->get_h() != asset->height) ) {
                        delete temp;  temp = 0;
                }
                if( !temp )
                        temp = new VFrame(0, -1, asset->width, asset->height, BC_RGB888, -1);
-               while( seq_no >= images.size() ) {
-                       mwindow->video_cache->check_in(asset);
-                       Thread::yield();
-                       file = mwindow->video_cache->check_out(asset, mwindow->edl, 0);
-                       if( !file ) { usleep(1000);  continue; }
-                       file->set_layer(0);
+               File *file = mwindow->video_cache->check_out(asset, mwindow->edl, 1);
+               if( !file ) { broken = 1;  return 0; }
+               Timer timer;
+               while( file && seq_no >= images.size() ) {
                        int64_t pos = images.size() / picon->gui->vicon_thread->refresh_rate * frame_rate;
                        file->set_video_position(pos,0);
+                       file->set_layer(0);
                        if( file->read_frame(temp) ) temp->clear_frame();
                        add_image(temp, vw, vh, vicon_cmodel);
+                       if( seq_no < images.size() ) break;
+                       mwindow->video_cache->check_in(asset);
+                       if( timer.get_difference() > 500 ) return 0;
+                       Thread::yield();
+                       if( avt->interrupted ) return 0;
+                       file = mwindow->video_cache->check_out(asset, mwindow->edl, 0);
+                       for( int retries=10; !file && --retries>=0; usleep(1000) ) {
+                               if( avt->interrupted ) return 0;
+                               file = mwindow->video_cache->check_out(asset, mwindow->edl, 0);
+                       }
+                       if( !file ) return 0;
                }
                mwindow->video_cache->check_in(asset);
        }
-       return *images[seq_no];
+       if( seq_no >= images.size() ) return 0;
+       return images[seq_no]->vfrm;
 }
 
 int64_t AssetVIcon::set_seq_no(int64_t no)
@@ -529,7 +545,7 @@ void AssetViewPopup::draw_vframe(VFrame *vframe)
        double total_length = edl->tracks->total_length();
        if( !total_length ) total_length = 1;
        for( Track *track=edl->tracks->first; track!=0; track=track->next ) {
-               if( !track->record ) continue;
+               if( !track->is_armed() ) continue;
                for( Edit *edit=track->edits->first; edit!=0; edit=edit->next ) {
                        Indexable *indexable = (Indexable *)edit->asset;
                        if( !indexable ) indexable = (Indexable *)edit->nested_edl;
@@ -600,22 +616,10 @@ AssetVIconThread::AssetVIconThread(AWindowGUI *gui, Preferences *preferences)
        case VICON_COLOR_MODE_HIGH:  vicon_cmodel = BC_RGB888;  break;
        }
        this->vicon_cmodel = vicon_cmodel;
-       this->draw_lock = new Mutex("AssetVIconThread::draw_lock");
 }
 
 AssetVIconThread::~AssetVIconThread()
 {
-       delete draw_lock;
-}
-
-void AssetVIconThread::drawing_started()
-{
-       draw_lock->lock("AssetVIconThread::drawing_started");
-}
-
-void AssetVIconThread::drawing_stopped()
-{
-       draw_lock->unlock();
 }
 
 void AssetVIconThread::set_view_popup(AssetVIcon *v, int draw_mode)
@@ -676,11 +680,11 @@ ViewPopup *AssetVIconThread::new_view_window(ViewPopup *vpopup)
        return av_popup;
 }
 
-void AssetVIconThread::close_view_popup()
+void AssetVIconThread::stop_vicon_drawing(int wait)
 {
        stop_drawing();
-       drawing_started(); // waits for draw lock
-       drawing_stopped();
+       gui->lock_window("AssetVIconThread::stop_vicon_drawing");
+       gui->unlock_window();
 }
 
 
@@ -879,7 +883,7 @@ void AssetPicon::reset()
 
 void AssetPicon::open_render_engine(EDL *edl, int is_audio)
 {
-       TransportCommand command;
+       TransportCommand command(mwindow->preferences);
        command.command = is_audio ? NORMAL_FWD : CURRENT_FRAME;
        command.get_edl()->copy_all(edl);
        command.change_type = CHANGE_ALL;
@@ -929,7 +933,7 @@ void AssetPicon::create_objects()
                else {
                        edl = (EDL *)indexable;
                        cp += sprintf(cp, "edl:");
-                       set_color(get_color() ^ 0xCC9955);
+//                     set_color(get_color() ^ 0xCC9955);
                }
                fs.extract_name(cp, indexable->path);
                set_text(name);
@@ -945,6 +949,11 @@ void AssetPicon::create_objects()
                if( asset->video_data ) {
                        if( mwindow->preferences->use_thumbnails ) {
                                gui->unlock_window();
+                               char string[BCTEXTLEN];
+                               sprintf(string, _("Reading %s"), name);
+                               mwindow->gui->lock_window("AssetPicon::create_objects");
+                               mwindow->gui->show_message(string);
+                               mwindow->gui->unlock_window();
                                File *file = mwindow->video_cache->check_out(asset,
                                        mwindow->edl,
                                        1);
@@ -968,11 +977,6 @@ void AssetPicon::create_objects()
                                                        asset->width, asset->height,
                                                        BC_RGB888, -1);
                                        }
-                                       { char string[BCTEXTLEN];
-                                       sprintf(string, _("Reading %s"), name);
-                                       mwindow->gui->lock_window("AssetPicon::create_objects");
-                                       mwindow->gui->show_message(string);
-                                       mwindow->gui->unlock_window(); }
                                        file->read_frame(gui->temp_picon);
                                        mwindow->video_cache->check_in(asset);
 
@@ -1319,6 +1323,8 @@ AWindowGUI::AWindowGUI(MWindow *mwindow, AWindow *awindow)
        new_folder_thread = 0;
        modify_folder_thread = 0;
        folder_lock = new Mutex("AWindowGUI::folder_lock");
+// *** CONTEXT_HELP ***
+       context_help_set_keyword("Resources Window");
 }
 
 AWindowGUI::~AWindowGUI()
@@ -1690,17 +1696,12 @@ int AWindowGUI::start_vicon_drawing()
        return 1;
 }
 
-int AWindowGUI::stop_vicon_drawing()
+int AWindowGUI::stop_vicon_drawing(int wait)
 {
-       if( !vicon_thread->interrupted )
-               vicon_thread->stop_drawing();
+       vicon_thread->stop_vicon_drawing(wait);
        return 0;
 }
 
-void AWindowGUI::close_view_popup()
-{
-       vicon_thread->close_view_popup();
-}
 
 VFrame *AssetPicon::get_vicon_frame()
 {
@@ -1761,6 +1762,8 @@ AWindowRemovePluginGUI(AWindow *awindow, AWindowRemovePlugin *thread,
        VFrame *vframe = plugin->get_picon();
        icon = vframe ? create_pixmap(vframe) : 0;
        plugin_list.append(new BC_ListBoxItem(plugin->title, icon));
+// *** CONTEXT_HELP ***
+       context_help_set_keyword("Delete Plugins to save Resources Space");
 }
 
 AWindowRemovePluginGUI::
@@ -1874,6 +1877,8 @@ BC_Window* AWindowRemovePlugin::new_gui()
 
 int AWindowGUI::keypress_event()
 {
+       char title[BCTEXTLEN];
+       PluginServer* plugin = 0;
        switch( get_keypress() ) {
        case 'w': case 'W':
                if( ctrl_down() ) {
@@ -1895,7 +1900,7 @@ int AWindowGUI::keypress_event()
                return cycle_assetlist_format();
        case DELETE:
                if( shift_down() && ctrl_down() ) {
-                       PluginServer* plugin = selected_plugin();
+                       plugin = selected_plugin();
                        if( !plugin ) break;
                        remove_plugin = new AWindowRemovePlugin(awindow, plugin);
                        unlock_window();
@@ -1924,7 +1929,30 @@ int AWindowGUI::keypress_event()
                }
                break;
        }
-       return 0;
+// *** CONTEXT_HELP ***
+       if( get_keypress() != 'h' || ! alt_down() )         return 0;
+       if( ! is_tooltip_event_win() || ! cursor_inside() ) return 0;
+       // If some plugin is selected, show its help
+       // Otherwise show general help
+       plugin = selected_plugin();
+       if( plugin ) {
+               strcpy(title, plugin->title);
+               if( ! strcmp(title, "Overlay") ) {
+                       // "Overlay" plugin title is ambiguous
+                       if( plugin->audio ) strcat(title, " \\(Audio\\)");
+                       if( plugin->video ) strcat(title, " \\(Video\\)");
+               }
+               if( plugin->is_ffmpeg() ) {
+                       // FFmpeg plugins can be audio or video
+                       if( plugin->audio )
+                               strcpy(title, "FFmpeg Audio Plugins");
+                       if( plugin->video )
+                               strcpy(title, "FFmpeg Video Plugins");
+               }
+               context_help_show(title);
+       }
+       else context_help_show("Resources Window");
+       return 1;
 }
 
 
@@ -2078,6 +2106,21 @@ void AWindowGUI::update_asset_list()
                        AssetPicon *picon = new AssetPicon(mwindow,
                                this, current);
                        new_assets.append(picon);
+                       if( current->width > ASSET_MAX_WIDTH || current->height > ASSET_MAX_HEIGHT ) {
+                               eprintf(_("Warning: %s\n"
+                                       " dimensions %dx%d exceed asset maximum limits %dx%d\n"),
+                                       current->path, current->width, current->height,
+                                               ASSET_MAX_WIDTH, ASSET_MAX_HEIGHT);
+                       }
+                       else if( mwindow->edl->session->playback_config->vconfig->driver == PLAYBACK_X11_GL ) {
+                               int texture_limit = BC_DisplayInfo::get_gl_max_texture_size();
+                               if( texture_limit >= 0 &&
+                                   (current->width >= texture_limit || current->height >= texture_limit) ) {
+                                       eprintf(_("Warning: %s\n"
+                                               " dimensions %dx%d exceed OpenGL texture limit %d\n"),
+                                               current->path, current->width, current->height, texture_limit);
+                               }
+                       }
                }
        }
 
@@ -2202,14 +2245,16 @@ void AWindowGUI::sort_folders()
        update_assets();
 }
 
-EDL *AWindowGUI::collect_proxy(Indexable *indexable)
+EDL *AWindowGUI::collect_proxy(Asset *proxy_asset)
 {
-       Asset *proxy_asset = (Asset *)indexable;
        char path[BCTEXTLEN];
        int proxy_scale = mwindow->edl->session->proxy_scale;
        ProxyRender::from_proxy_path(path, proxy_asset, proxy_scale);
-       Asset *unproxy_asset = mwindow->edl->assets->get_asset(path);
-       if( !unproxy_asset || !unproxy_asset->layers ) return 0;
+       Indexable *unproxy_idxbl =
+               proxy_asset->proxy_edl ?
+                       (Indexable *) mwindow->edl->get_nested_edl(path) :
+                       (Indexable *) mwindow->edl->assets->get_asset(path);
+       if( !unproxy_idxbl || !unproxy_idxbl->get_video_layers() ) return 0;
 // make a clip from proxy video tracks and unproxy audio tracks
        EDL *proxy_edl = new EDL(mwindow->edl);
        proxy_edl->create_objects();
@@ -2218,7 +2263,7 @@ EDL *AWindowGUI::collect_proxy(Indexable *indexable)
        strcpy(proxy_edl->local_session->clip_title, path);
        strcpy(proxy_edl->local_session->clip_notes, _("Proxy clip"));
        proxy_edl->session->video_tracks = proxy_asset->layers;
-       proxy_edl->session->audio_tracks = unproxy_asset->channels;
+       proxy_edl->session->audio_tracks = unproxy_idxbl->get_audio_channels();
        proxy_edl->create_default_tracks();
        double length = proxy_asset->frame_rate > 0 ?
                ( proxy_asset->video_length >= 0 ?
@@ -2232,11 +2277,15 @@ EDL *AWindowGUI::collect_proxy(Indexable *indexable)
                if( current->data_type != TRACK_VIDEO ) continue;
                current->insert_asset(proxy_asset, 0, length, 0, vtrack++);
        }
-       length = (double)unproxy_asset->audio_length / unproxy_asset->sample_rate;
+       int64_t samples = unproxy_idxbl->get_audio_samples();
+       int sample_rate = unproxy_idxbl->get_sample_rate();
+       length = sample_rate > 0 ? (double)samples / sample_rate : 0;
        current = proxy_edl->tracks->first;
        for( int atrack=0; current; current=NEXT ) {
                if( current->data_type != TRACK_AUDIO ) continue;
-               current->insert_asset(unproxy_asset, 0, length, 0, atrack++);
+               Asset *asset = unproxy_idxbl->is_asset ? (Asset *)unproxy_idxbl : 0;
+               EDL *nested_edl = unproxy_idxbl->is_asset ? 0 : (EDL *)unproxy_idxbl;
+               current->insert_asset(asset, nested_edl, length, 0, atrack++);
        }
        proxy_edl->folder_no = AW_PROXY_FOLDER;
        return proxy_edl;
@@ -2252,7 +2301,7 @@ void AWindowGUI::collect_assets(int proxy)
                Indexable *indexable = result->indexable;
                if( proxy && indexable && indexable->is_asset &&
                    indexable->folder_no == AW_PROXY_FOLDER ) {
-                       EDL *drag_edl = collect_proxy(indexable);
+                       EDL *drag_edl = collect_proxy((Asset*)indexable);
                        if( drag_edl ) mwindow->session->drag_clips->append(drag_edl);
                        continue;
                }
@@ -2648,12 +2697,18 @@ int AWindowFolders::load_expanders()
        char expanders_path[BCTEXTLEN];
        mwindow->create_defaults_path(expanders_path, EXPANDERS_FILE);
        FILE *fp = fopen(expanders_path, "r");
+       if( !fp ) {
+               snprintf(expanders_path, sizeof(expanders_path), "%s/%s",
+                       File::get_cindat_path(), EXPANDERS_FILE);
+               char *cp = strrchr(expanders_path,'.');
+               if( cp ) strcpy(cp+1, mwindow->cin_lang);
+               fp = fopen(expanders_path, "r");
+       }
        if( !fp ) {
                snprintf(expanders_path, sizeof(expanders_path), "%s/%s",
                        File::get_cindat_path(), EXPANDERS_FILE);
                fp = fopen(expanders_path, "r");
        }
-
        if( !fp ) return 1;
        const char tab = '\t';
        char line[BCTEXTLEN];   line[0] = 0;
@@ -2808,8 +2863,8 @@ int AWindowAssets::handle_event()
                break;
        }
        if( !vwindow || !vwindow->is_running() ) return 1;
-       if( proxy && picon_idxbl ) {
-               picon_edl = gui->collect_proxy(picon_idxbl);
+       if( proxy && picon_idxbl && picon_idxbl->is_asset ) {
+               picon_edl = gui->collect_proxy((Asset*)picon_idxbl);
                picon_idxbl = 0;
        }
 
@@ -2856,6 +2911,18 @@ int AWindowAssets::selection_changed()
 
                deactivate_selection();
        }
+       else if( get_button_down() && get_buttonpress() == LEFT_BUTTON &&
+                get_double_click() ) {
+               item = (AssetPicon*)get_selection(0, 0);
+               if( item ) {
+                       switch( folder ) {
+                       case AW_LABEL_FOLDER:
+                               if( !item->label ) break;
+                               mwindow->set_position(item->label->position);
+                               break;
+                       }
+               }
+       }
        else if( get_button_down() && !gui->play_off &&
                 mwindow->edl->session->assetlist_format != ASSETS_TEXT ) {
                item = (AssetPicon*)get_selection(0, 0);
@@ -3160,6 +3227,102 @@ void AWindowAssets::hide_tip_info()
        info_tip = -1;
 }
 
+// *** CONTEXT_HELP ***
+int AWindowAssets::keypress_event()
+{
+       int item;
+       char title[BCTEXTLEN];
+       AssetPicon *picon = 0;
+       PluginServer *plugin = 0;
+
+//     printf("AWindowAssets::keypress_event: %d\n", get_keypress());
+
+       // If not our context help keystroke, redispatch it
+       // to the event handler of the base class
+       if (get_keypress() != 'h' || ! alt_down() ||
+           ! is_tooltip_event_win() || ! cursor_inside())
+               return BC_ListBox::keypress_event();
+
+       switch (mwindow->edl->session->awindow_folder) {
+
+       case AW_AEFFECT_FOLDER:
+       case AW_VEFFECT_FOLDER:
+       case AW_ATRANSITION_FOLDER:
+       case AW_VTRANSITION_FOLDER:
+               // If plugin tips activated, show help for plugin under mouse
+               // Otherwise show help for the selected plugin
+               if (gui->tip_info) {
+                       item = BC_ListBox::get_highlighted_item();
+                       if (item >= 0 && item < gui->displayed_assets[0].size()) {
+                               picon = (AssetPicon *) gui->displayed_assets[0][item];
+                               if (picon) plugin = picon->plugin;
+                       }
+               }
+               else plugin = gui->selected_plugin();
+               // If some plugin is highlighted or selected, show its help
+               // Otherwise show more general help
+               if (plugin) {
+                       strcpy(title, plugin->title);
+                       if (! strcmp(title, "Overlay")) {
+                               // "Overlay" plugin title is ambiguous
+                               if (plugin->audio)
+                                       strcat(title, " \\(Audio\\)");
+                               if (plugin->video)
+                                       strcat(title, " \\(Video\\)");
+                       }
+                       if (plugin->is_ffmpeg()) {
+                               // FFmpeg plugins can be audio or video
+                               if (plugin->audio)
+                                       strcpy(title, "FFmpeg Audio Plugins");
+                               if (plugin->video)
+                                       strcpy(title, "FFmpeg Video Plugins");
+                       }
+                       context_help_show(title);
+                       return 1;
+               }
+               else {
+                       switch (mwindow->edl->session->awindow_folder) {
+                       case AW_AEFFECT_FOLDER:
+                               context_help_show("Audio Effects");
+                               return 1;
+                       case AW_VEFFECT_FOLDER:
+                               context_help_show("Video Effects");
+                               return 1;
+                       case AW_ATRANSITION_FOLDER:
+                               context_help_show("Audio Transitions");
+                               return 1;
+                       case AW_VTRANSITION_FOLDER:
+                               context_help_show("Video Transitions");
+                               return 1;
+                       default:
+                               context_help_show("Resources Window");
+                               return 1;
+                       }
+                       context_help_show("Resources Window");
+                       return 1;
+               }
+
+       case AW_LABEL_FOLDER:
+               context_help_show("Labels");
+               return 1;
+
+       case AW_CLIP_FOLDER:
+               context_help_show("Nested Clips");
+               return 1;
+
+       case AW_PROXY_FOLDER:
+               context_help_show("Proxy");
+               return 1;
+
+       default:
+               context_help_show("Resources Window");
+               return 1;
+       }
+
+       context_help_show("Resources Window");
+       return 1;
+}
+
 
 AWindowSearchTextBox::AWindowSearchTextBox(AWindowSearchText *search_text, int x, int y, int w)
  : BC_TextBox(x, y, w, 1, "")