bsd lang segv fix, enable bsd lv2, lv2 gui enable fix, proxy/ffmpeg toggle resize...
[goodguy/history.git] / cinelerra-5.1 / cinelerra / proxy.C
index 02eb20fa03c5003e06f375e542c00c6ab1f0ecd4..45feb5951354a36d5e668f73080483ddc6ababba 100644 (file)
  */
 
 #include "assets.h"
+#include "audiodevice.h"
 #include "bcsignals.h"
 #include "cache.h"
 #include "clip.h"
-#include "confirmsave.h"
 #include "cstrdup.h"
 #include "edl.h"
 #include "edlsession.h"
@@ -48,7 +48,7 @@
 #include "vrender.h"
 
 #define WIDTH 400
-#define HEIGHT 285
+#define HEIGHT 330
 #define MAX_SCALE 16
 
 ProxyMenuItem::ProxyMenuItem(MWindow *mwindow)
@@ -160,110 +160,25 @@ void ProxyDialog::calculate_sizes()
 
 void ProxyDialog::handle_close_event(int result)
 {
-       asset->save_defaults(mwindow->defaults, "PROXY_", 1, 1, 0, 0, 0); 
-
-       if( !result )
-               to_proxy();
-}
-
-void ProxyDialog::to_proxy()
-{
-       ArrayList<Indexable*> orig_idxbls;
-       ArrayList<Indexable*> proxy_assets;
-
-       EDL *edl = mwindow->edl;
-       mwindow->edl->Garbage::add_user();
-       mwindow->save_backup();
-       mwindow->undo->update_undo_before(_("proxy"), this);
-       ProxyRender proxy_render(mwindow, asset);
-
-// revert project to original size from current size
-// remove all session proxy assets at the at the current proxy_scale
-       int proxy_scale = edl->session->proxy_scale;
-       if( proxy_scale > 1 ) {
-               Asset *orig_asset = edl->assets->first;
-               for( ; orig_asset; orig_asset=orig_asset->next ) {
-                       char new_path[BCTEXTLEN];
-                       proxy_render.to_proxy_path(new_path, orig_asset, proxy_scale);
-// test if proxy asset was already added to proxy_assets
-                       int got_it = 0;
-                       for( int i = 0; !got_it && i<proxy_assets.size(); ++i )
-                               got_it = !strcmp(proxy_assets[i]->path, new_path);
-                       if( got_it ) continue;
-                       Asset *proxy_asset = edl->assets->get_asset(new_path);
-                       if( !proxy_asset ) continue;
-// add pointer to existing EDL asset if it exists
-// EDL won't delete it unless it's the same pointer.
-                       proxy_assets.append(proxy_asset);
-                       proxy_asset->add_user();
-                       orig_idxbls.append(orig_asset);
-                       orig_asset->add_user();
-               }
-
-// convert from the proxy assets to the original assets
-               int proxy_auto_scale = edl->session->proxy_auto_scale;
-               mwindow->set_proxy(0, 1, proxy_auto_scale, &proxy_assets, &orig_idxbls);
-
-// remove the references
-               for( int i=0; i<proxy_assets.size(); ++i ) {
-                       Asset *proxy = (Asset *) proxy_assets[i];
-                       proxy->width = proxy->actual_width;
-                       proxy->height = proxy->actual_height;
-                       proxy->remove_user();
-                       mwindow->edl->assets->remove_pointer(proxy);
-                       proxy->remove_user();
-               }
-               proxy_assets.remove_all();
-               for( int i = 0; i < orig_idxbls.size(); i++ )
-                       orig_idxbls[i]->remove_user();
-               orig_idxbls.remove_all();
-       }
-
-       ArrayList<char *> confirm_paths;    // test for new files
-       confirm_paths.set_array_delete();
-
-// convert to new size if not original size
-       if( new_scale != 1 ) {
-               FileSystem fs;
-               Asset *orig = mwindow->edl->assets->first;
-               for( ; orig; orig=orig->next ) {
-                       Asset *proxy = proxy_render.add_original(orig, new_scale);
-                       if( !proxy ) continue;
-                       int exists = fs.get_size(proxy->path) > 0 ? 1 : 0;
-                       int got_it = exists && // if proxy exists, and is newer than orig
-                           fs.get_date(proxy->path) > fs.get_date(orig->path) ? 1 : 0;
-                       if( !got_it ) {
-                               if( exists ) // prompt user to overwrite
-                                       confirm_paths.append(cstrdup(proxy->path));
-                               proxy_render.add_needed(orig, proxy);
-                       }
-               }
+       if( result ) return;
+       if( !File::renders_video(asset) ) {
+               eprintf("Specified format does not render video");
+               return;
        }
-
-       int result = 0;
-// test for existing files
-       if( confirm_paths.size() ) {
-               result = ConfirmSave::test_files(mwindow, &confirm_paths);
-               confirm_paths.remove_all_objects();
+       mwindow->edl->session->proxy_use_scaler = use_scaler;
+       mwindow->edl->session->proxy_auto_scale = auto_scale;
+       mwindow->edl->session->proxy_beep = beep;
+       mwindow->edl->session->proxy_disabled_scale = 1;
+       asset->save_defaults(mwindow->defaults, "PROXY_", 1, 1, 0, 0, 0); 
+       result = mwindow->to_proxy(asset, new_scale);
+       if( result >= 0 && beep && new_scale != 1 ) {
+               static struct { double freq, secs, gain; }
+                   tone[2] = { { 2000., 1.5, 0.5 }, { 4000., 0.25, 0.5 } };
+               int i = result > 0 ? 0 : 1;
+               mwindow->beep(tone[i].freq, tone[i].secs, tone[i].gain);
        }
-
-       if( !result )
-               result = proxy_render.create_needed_proxies(new_scale);
-
-       if( !result ) // resize project
-               mwindow->set_proxy(use_scaler, new_scale, auto_scale,
-                       &proxy_render.orig_idxbls, &proxy_render.orig_proxies);
-
-       mwindow->undo->update_undo_after(_("proxy"), LOAD_ALL);
-       mwindow->edl->Garbage::remove_user();
-       mwindow->restart_brender();
-
-       mwindow->gui->lock_window("ProxyDialog::to_proxy");
-       mwindow->update_project(LOAD_ALL);
-       mwindow->gui->unlock_window();
 }
 
-
 void ProxyRender::to_proxy_path(char *new_path, Indexable *indexable, int scale)
 {
 // path is already a proxy
@@ -283,27 +198,30 @@ void ProxyRender::to_proxy_path(char *new_path, Indexable *indexable, int scale)
                sfx = ep;  ep += n;
        }
        for( char *cp=prxy; --n>=0; ++cp ) *sfx++ = *cp;
-       const char *ext = format_asset->format == FILE_FFMPEG ?
-               format_asset->fformat : File::get_tag(format_asset->format);
        *ep++ = '.';
+       const char *ext = indexable->get_video_frames() < 0 ?  "png" :
+               format_asset->format == FILE_FFMPEG ?
+                       format_asset->fformat :
+                       File::get_tag(format_asset->format);
        while( *ext ) *ep++ = *ext++;
        *ep = 0;
 //printf("ProxyRender::to_proxy_path %d %s %s\n", __LINE__, new_path), asset->path);
 }
 
-void ProxyRender::from_proxy_path(char *new_path, Asset *asset, int scale)
+int ProxyRender::from_proxy_path(char *new_path, Indexable *indexable, int scale)
 {
        char prxy[BCTEXTLEN];
        int n = sprintf(prxy, ".proxy%d", scale);
-       strcpy(new_path, asset->path);
+       strcpy(new_path, indexable->path);
        char *ptr = strstr(new_path, prxy);
-       if( !ptr || (ptr[n] != '-' && ptr[n] != '.') ) return;
+       if( !ptr || (ptr[n] != '-' && ptr[n] != '.') ) return 1;
 // remove proxy, path.proxy#-sfx.ext => path.sfx
        char *ext = strrchr(ptr, '.');
        if( !ext ) ext = ptr + strlen(ptr);
        char *cp = ptr + n;
        for( *cp='.'; cp<ext; ++cp ) *ptr++ = *cp;
        *ptr = 0;
+       return 0;
 }
 
 ProxyRender::ProxyRender(MWindow *mwindow, Asset *format_asset)
@@ -330,7 +248,6 @@ ProxyRender::~ProxyRender()
 Asset *ProxyRender::add_original(Indexable *idxbl, int new_scale)
 {
        if( !idxbl->have_video() ) return 0;
-       if( idxbl->get_video_frames() <= 0 ) return 0;
 // don't proxy proxies
        if( strstr(idxbl->path,".proxy") ) return 0;
        char new_path[BCTEXTLEN];
@@ -347,7 +264,16 @@ Asset *ProxyRender::add_original(Indexable *idxbl, int new_scale)
        if( !proxy ) {
                proxy = new Asset(new_path);
 // new compression parameters
-               proxy->copy_format(format_asset, 0);
+               int64_t video_frames = idxbl->get_video_frames();
+               if( video_frames < 0 ) {
+                       proxy->format = FILE_PNG;
+                       proxy->png_use_alpha = 1;
+                       proxy->video_length = -1;
+               }
+               else {
+                       proxy->copy_format(format_asset, 0);
+                       proxy->video_length = video_frames;
+               }
                proxy->awindow_folder = AW_PROXY_FOLDER;
                proxy->audio_data = 0;
                proxy->video_data = 1;
@@ -359,10 +285,7 @@ Asset *ProxyRender::add_original(Indexable *idxbl, int new_scale)
                if( proxy->height & 1 ) ++proxy->height;
                proxy->actual_height = proxy->height;
                proxy->frame_rate = idxbl->get_frame_rate();
-               proxy->video_length = idxbl->get_video_frames();
-               edl_assets->append(proxy);
        }
-       proxy->add_user();
        orig_proxies.append(proxy);
        idxbl->add_user();
        orig_idxbls.append(idxbl);
@@ -418,7 +341,6 @@ printf("failed=%d canceled=%d\n", failed, progress->is_cancelled());
        if( failed && !canceled ) {
                eprintf("Error making proxy.");
        }
-
        return !failed && !canceled ? 0 : 1;
 }
 
@@ -448,6 +370,7 @@ void ProxyWindow::create_objects()
        dialog->use_scaler = mwindow->edl->session->proxy_use_scaler;
        dialog->orig_scale = mwindow->edl->session->proxy_scale;
        dialog->auto_scale = mwindow->edl->session->proxy_auto_scale;
+       dialog->beep = mwindow->edl->session->proxy_beep;
        dialog->new_scale = dialog->orig_scale;
 
        int x = margin;
@@ -474,6 +397,12 @@ void ProxyWindow::create_objects()
        add_subwindow(new_dimensions = new BC_Title(x, y, ""));
        y += new_dimensions->get_h() + margin;
 
+       x = margin;
+       add_subwindow(text = new BC_Title(x, y, _("Active Scale: ")));
+       x += text->get_w() + margin;
+       add_subwindow(active_scale = new BC_Title(x, y, ""));
+       y += active_scale->get_h() + margin;
+
        x = margin;  y += 25;
        format_tools = new ProxyFormatTools(mwindow, this, dialog->asset);
        format_tools->create_objects(x, y, 0, 1, 0, 0, 0, 1, 0, 1, // skip the path
@@ -482,6 +411,8 @@ void ProxyWindow::create_objects()
        x = margin;
        add_subwindow(auto_scale = new ProxyAutoScale(this, x, y));
        y += auto_scale->get_h() + margin;
+       add_subwindow(beep_on_done = new ProxyBeepOnDone(this, x, y));
+       y += beep_on_done->get_h() + margin;
 
        update();
 
@@ -505,11 +436,7 @@ void ProxyFormatTools::update_format()
 
 void ProxyWindow::update()
 {
-// preview the new size
-       char string[BCTEXTLEN];
-//printf("ProxyWindow::update %d %d %d %d %d\n", 
-// __LINE__, mwindow->edl->session->output_w, mwindow->edl->session->output_h,
-// dialog->orig_scale, dialog->new_scale);
+       char string[BCSTRLEN];
        int orig_w = mwindow->edl->session->output_w * dialog->orig_scale;
        int orig_h = mwindow->edl->session->output_h * dialog->orig_scale;
        int new_w = orig_w / dialog->new_scale;
@@ -522,6 +449,10 @@ void ProxyWindow::update()
        scale_factor->set_text(string);
        use_scaler->update();
        auto_scale->update();
+       int scale = mwindow->edl->session->proxy_scale;
+       if( scale == 1 ) scale = mwindow->edl->session->proxy_disabled_scale;
+       sprintf(string, scale>1 ? "1/%d" : "%d", scale);
+       active_scale->update(string);
 }
 
 
@@ -551,7 +482,7 @@ int ProxyUseScaler::handle_event()
 }
 
 ProxyAutoScale::ProxyAutoScale(ProxyWindow *pwindow, int x, int y)
- : BC_CheckBox(x, y, pwindow->dialog->use_scaler, _("Auto proxy/scale media loads"))
+ : BC_CheckBox(x, y, pwindow->dialog->auto_scale, _("Auto proxy/scale media loads"))
 {
        this->pwindow = pwindow;
 }
@@ -573,6 +504,19 @@ int ProxyAutoScale::handle_event()
        return 1;
 }
 
+ProxyBeepOnDone::ProxyBeepOnDone(ProxyWindow *pwindow, int x, int y)
+ : BC_CheckBox(x, y, pwindow->dialog->beep, _("Beep on done"))
+{
+       this->pwindow = pwindow;
+}
+
+int ProxyBeepOnDone::handle_event()
+{
+       pwindow->dialog->beep = get_value();
+       pwindow->update();
+       return 1;
+}
+
 
 ProxyMenu::ProxyMenu(MWindow *mwindow, ProxyWindow *pwindow,
                int x, int y, int w, const char *text)
@@ -718,6 +662,7 @@ void ProxyClient::process_package(LoadPackage *ptr)
        result = dst_file.open_file(preferences, proxy, 0, 1);
        if( result ) {
                proxy_render->failed = 1;
+               ::remove(proxy->path);
                return;
        }
 
@@ -728,8 +673,10 @@ void ProxyClient::process_package(LoadPackage *ptr)
        VFrame src_frame(src_w,src_h, edl->session->color_model);
 
        OverlayFrame scaler(processors);
+       int64_t video_length = orig->get_video_frames();
+       if( video_length < 0 ) video_length = 1;
 
-       for( int64_t i=0, length=orig->get_video_frames(); i<length &&
+       for( int64_t i=0; i<video_length &&
             !proxy_render->failed && !proxy_render->is_canceled(); ++i ) {
                if( orig->is_asset ) {
                        src_file->set_video_position(i, 0);
@@ -765,6 +712,8 @@ void ProxyClient::process_package(LoadPackage *ptr)
                mwindow->mainindexes->add_next_asset(0, asset);
                mwindow->mainindexes->start_build();
        }
+       else
+               ::remove(proxy->path);
 }
 
 
@@ -799,3 +748,85 @@ LoadPackage* ProxyFarm::new_package()
        return new ProxyPackage;
 }
 
+
+ProxyBeep::ProxyBeep(MWindow *mwindow)
+ : Thread(1, 0, 0)
+{
+       this->mwindow = mwindow;
+       audio = new AudioDevice(mwindow);
+       playing_audio = 0;
+       interrupted = -1;
+}
+
+ProxyBeep::~ProxyBeep()
+{
+       stop(0);
+       delete audio;
+}
+
+void ProxyBeep::run()
+{
+       int channels = 2;
+       int64_t bfrsz = BEEP_SAMPLE_RATE;
+       EDL *edl = mwindow->edl;
+       EDLSession *session = edl->session;
+       AudioOutConfig *aconfig = session->playback_config->aconfig;
+       audio->open_output(aconfig, BEEP_SAMPLE_RATE, bfrsz, channels, 0);
+       audio->start_playback();
+
+       double out0[bfrsz], out1[bfrsz], *out[2] = { out0, out1 };
+       const double two_pi = 2*M_PI;
+       int64_t audio_len = BEEP_SAMPLE_RATE * secs;
+       const double dt = two_pi * freq/BEEP_SAMPLE_RATE;
+       double th = 0;
+
+       audio_pos = 0;
+       playing_audio = 1;
+       while( !interrupted ) {
+               int len = audio_len - audio_pos;
+               if( len <= 0 ) break;
+               if( len > bfrsz ) len = bfrsz;
+               int k = audio_pos;
+               for( int i=0; i<len; ++i,++k,th+=dt ) {
+                       double t = th - two_pi;
+                       if( t >= 0 ) th = t;
+                       out0[i] = out1[i] = sin(th) * gain;
+               }
+               audio->write_buffer(out, channels, len);
+               audio_pos = k;
+       }
+
+       if( !interrupted )
+               audio->set_last_buffer();
+       audio->stop_audio(interrupted ? 0 : 1);
+       playing_audio = 0;
+
+       audio->close_all();
+}
+
+void ProxyBeep::start()
+{
+       if( running() ) return;
+       audio_pos = -1;
+       interrupted = 0;
+       Thread::start();
+}
+
+void ProxyBeep::stop(int wait)
+{
+       if( running() && !interrupted ) {
+               interrupted = 1;
+               audio->stop_audio(wait);
+       }
+       Thread::join();
+}
+
+void ProxyBeep::tone(double freq, double secs, double gain)
+{
+       stop(0);
+       this->freq = freq;
+       this->secs = secs;
+       this->gain = gain;
+       start();
+}
+