no longer need ffmpeg patch0 which was for Termux
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / proxy.C
index a53dcf6c6bf29aba38d0eae04c26b196658d091d..0012fd1924d1b0fb40ce992b13a60605641fa86f 100644 (file)
@@ -47,8 +47,8 @@
 #include "transportque.h"
 #include "vrender.h"
 
-#define WIDTH 400
-#define HEIGHT 330
+#define WIDTH xS(400)
+#define HEIGHT yS(400)
 #define MAX_SCALE 16
 
 ProxyMenuItem::ProxyMenuItem(MWindow *mwindow)
@@ -88,12 +88,15 @@ ProxyDialog::ProxyDialog(MWindow *mwindow)
        strcpy(asset->fformat, "mpeg");
        strcpy(asset->vcodec, "mpeg.mpeg");
        asset->ff_video_bitrate = 2000000;
+       asset->video_data = 1;
 
        bzero(size_text, sizeof(char*) * MAX_SIZES);
        bzero(size_factors, sizeof(int) * MAX_SIZES);
-       size_text[0] = cstrdup(_("Original size"));
-       size_factors[0] = 1;
-       total_sizes = 1;
+       size_text[0] = cstrdup(_("off"));
+       size_text[1] = cstrdup(_("1"));
+       size_factors[0] = 0;
+       size_factors[1] = 1;
+       total_sizes = 2;
 }
 
 ProxyDialog::~ProxyDialog()
@@ -130,15 +133,12 @@ void ProxyDialog::scale_to_text(char *string, int scale)
 
 void ProxyDialog::calculate_sizes()
 {
-       for( int i=1; i<total_sizes; ++i ) {
+       for( int i=2; i<total_sizes; ++i ) {
                delete [] size_text[i];
                size_text[i] = 0;
                size_factors[i] = 0;
        }
-       total_sizes = 1;
-
-       int orig_w = mwindow->edl->session->output_w * orig_scale;
-       int orig_h = mwindow->edl->session->output_h * orig_scale;
+       total_sizes = 2;
 
        if( !use_scaler ) {
 // w,h should stay even for yuv
@@ -158,7 +158,7 @@ void ProxyDialog::calculate_sizes()
                size_factors[total_sizes++] = 16;  size_factors[total_sizes++] = 24;
                size_factors[total_sizes++] = 32;
        }
-       for( int i=1; i<total_sizes; ++i ) {
+       for( int i=2; i<total_sizes; ++i ) {
                char string[BCTEXTLEN];
                sprintf(string, "1/%d", size_factors[i]);
                size_text[i] = cstrdup(string);
@@ -167,32 +167,47 @@ void ProxyDialog::calculate_sizes()
 
 void ProxyDialog::handle_close_event(int result)
 {
+       gui = 0;
        if( result ) return;
        if( !File::renders_video(asset) ) {
-               eprintf("Specified format does not render video");
+               eprintf(_("Specified format does not render video"));
                return;
        }
        mwindow->edl->session->proxy_auto_scale = auto_scale;
-       mwindow->edl->session->proxy_beep = beep;
-       mwindow->edl->session->proxy_disabled_scale = 1;
-       mwindow->gui->lock_window("ProxyDialog::handle_close_event");
-       mwindow->gui->update_proxy_toggle();
-       mwindow->gui->unlock_window();
+       mwindow->edl->session->proxy_beep = beeper_volume;
        asset->save_defaults(mwindow->defaults, "PROXY_", 1, 1, 0, 0, 0); 
        result = mwindow->to_proxy(asset, new_scale, use_scaler);
-       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 >= 0 && beeper_on && beeper_volume > 0 && new_scale >= 1 ) {
+               if( !result ) {
+                       mwindow->beep(4000., 0.5, beeper_volume);
+                       usleep(250000);
+                       mwindow->beep(1000., 0.5, beeper_volume);
+                       usleep(250000);
+                       mwindow->beep(4000., 0.5, beeper_volume);
+               }
+               else
+                       mwindow->beep(2000., 2.0, beeper_volume);
        }
+       mwindow->edl->session->proxy_disabled_scale = 1;
+       mwindow->gui->lock_window("ProxyDialog::handle_close_event");
+       mwindow->update_project(LOADMODE_REPLACE);
+       mwindow->gui->unlock_window();
 }
 
 void ProxyRender::to_proxy_path(char *new_path, Indexable *indexable, int scale)
 {
 // path is already a proxy
        if( strstr(indexable->path, ".proxy") ) return;
-       strcpy(new_path, indexable->path);
+       if( !indexable->is_asset ) {
+               char *ifn = indexable->path, *cp = strrchr(ifn, '/');
+               if( cp ) ifn = cp+1;
+               char proxy_path[BCTEXTLEN];
+               File::getenv_path(proxy_path,
+                       mwindow->preferences->nested_proxy_path);
+               sprintf(new_path, "%s/%s", proxy_path, ifn);
+       }
+       else
+               strcpy(new_path, indexable->path);
        char prxy[BCSTRLEN];
        int n = sprintf(prxy, ".proxy%d", scale);
 // insert proxy, path.sfx => path.proxy#-sfx.ext
@@ -217,11 +232,11 @@ void ProxyRender::to_proxy_path(char *new_path, Indexable *indexable, int scale)
 //printf("ProxyRender::to_proxy_path %d %s %s\n", __LINE__, new_path), asset->path);
 }
 
-int ProxyRender::from_proxy_path(char *new_path, Indexable *indexable, int scale)
+int ProxyRender::from_proxy_path(char *new_path, Asset *asset, int scale)
 {
        char prxy[BCTEXTLEN];
        int n = sprintf(prxy, ".proxy%d", scale);
-       strcpy(new_path, indexable->path);
+       strcpy(new_path, asset->path);
        char *ptr = strstr(new_path, prxy);
        if( !ptr || (ptr[n] != '-' && ptr[n] != '.') ) return 1;
 // remove proxy, path.proxy#-sfx.ext => path.sfx
@@ -230,13 +245,20 @@ int ProxyRender::from_proxy_path(char *new_path, Indexable *indexable, int scale
        char *cp = ptr + n;
        for( *cp='.'; cp<ext; ++cp ) *ptr++ = *cp;
        *ptr = 0;
+       if( asset->proxy_edl ) {
+               if( (cp = strrchr(new_path, '/')) != 0 ) {
+                       for( ptr=new_path; *++cp; ) *ptr++ = *cp;
+                       *ptr = 0;
+               }
+       }
        return 0;
 }
 
-ProxyRender::ProxyRender(MWindow *mwindow, Asset *format_asset)
+ProxyRender::ProxyRender(MWindow *mwindow, Asset *format_asset, int asset_scale)
 {
        this->mwindow = mwindow;
        this->format_asset = format_asset;
+       this->asset_scale = asset_scale;
        progress = 0;
        counter_lock = new Mutex("ProxyDialog::counter_lock");
        total_rendered = 0;
@@ -261,8 +283,18 @@ Asset *ProxyRender::add_original(Indexable *idxbl, int new_scale)
        if( strstr(idxbl->path,".proxy") ) return 0;
        char new_path[BCTEXTLEN];
        to_proxy_path(new_path, idxbl, new_scale);
-// don't proxy if not readable
+// don't proxy if not readable, or proxy_path not writable
        if( idxbl->is_asset && access(idxbl->path, R_OK) ) return 0;
+       int ret = access(new_path, W_OK);
+       if( ret ) {
+               int fd = ::open(new_path,O_WRONLY);
+               if( fd < 0 ) fd = open(new_path,O_WRONLY+O_CREAT,0666);
+               if( fd >= 0 ) { close(fd);  ret = 0; }
+       }
+       if( ret ) {
+               eprintf(_("bad proxy path: %s\n"), new_path);
+               return 0;
+       }
 // add to orig_idxbls & orig_proxies if it isn't already there.
        int got_it = 0;
        for( int i = 0; !got_it && i<orig_proxies.size(); ++i )
@@ -284,6 +316,7 @@ Asset *ProxyRender::add_original(Indexable *idxbl, int new_scale)
                        proxy->video_length = video_frames;
                }
                proxy->folder_no = AW_PROXY_FOLDER;
+               proxy->proxy_edl = !idxbl->is_asset ? 1 : 0;
                proxy->audio_data = 0;
                proxy->video_data = 1;
                proxy->layers = 1;
@@ -335,12 +368,14 @@ int ProxyRender::create_needed_proxies(int new_scale)
        }
 
 // start progress bar.  MWindow is locked inside this
+       mwindow->gui->lock_window("ProxyRender::create_needed_proxies");
        progress = mwindow->mainprogress->
                start_progress(_("Creating proxy files..."), total_len);
+       mwindow->gui->unlock_window();
 
        ProxyFarm engine(mwindow, this, &needed_idxbls, &needed_proxies);
        engine.process_packages();
-printf("failed=%d canceled=%d\n", failed, progress->is_cancelled());
+printf("proxy: failed=%d canceled=%d\n", failed, progress->is_cancelled());
 
 // stop progress bar
        canceled = progress->is_cancelled();
@@ -348,7 +383,7 @@ printf("failed=%d canceled=%d\n", failed, progress->is_cancelled());
        delete progress;  progress = 0;
 
        if( failed && !canceled ) {
-               eprintf("Error making proxy.");
+               eprintf(_("Error making proxy."));
        }
        return !failed && !canceled ? 0 : 1;
 }
@@ -361,6 +396,8 @@ ProxyWindow::ProxyWindow(MWindow *mwindow, ProxyDialog *dialog, int x, int y)
        this->mwindow = mwindow;
        this->dialog = dialog;
        format_tools = 0;
+// *** CONTEXT_HELP ***
+       context_help_set_keyword("Proxy");
 }
 
 ProxyWindow::~ProxyWindow()
@@ -375,53 +412,77 @@ void ProxyWindow::create_objects()
 {
        lock_window("ProxyWindow::create_objects");
        int margin = mwindow->theme->widget_border;
+       int xs10 = xS(10), x1 = xS(50);
+       int lmargin = margin + xs10;
+       int x = lmargin, y = margin+yS(10);
 
        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;
+       dialog->beeper_on = mwindow->edl->session->proxy_beep > 0 ? 1 : 0;
+       dialog->beeper_volume = mwindow->edl->session->proxy_beep;
+       dialog->new_scale = mwindow->edl->session->proxy_state != PROXY_INACTIVE ?
+               dialog->orig_scale : 0;
+       dialog->orig_w = mwindow->edl->session->output_w;
+       dialog->orig_h = mwindow->edl->session->output_h;
+       if( !dialog->use_scaler ) {
+               dialog->orig_w *= dialog->orig_scale;
+               dialog->orig_h *= dialog->orig_scale;
+       }
 
-       int x = margin;
-       int y = margin+10;
-       add_subwindow(use_scaler = new ProxyUseScaler(this, x, y));
-       y += use_scaler->get_h() + margin;
+       add_subwindow(title_bar1 = new BC_TitleBar(xs10, y, get_w()-xS(30), xS(20), xs10,
+               _("Scaling options")));
+       y += title_bar1->get_h() + 3*margin;
 
        BC_Title *text;
+       x = lmargin;
        add_subwindow(text = new BC_Title(x, y, _("Scale factor:")));
-       x += text->get_w() + margin;
-
-       int popupmenu_w = BC_PopupMenu::calculate_w(get_text_width(MEDIUMFONT, dialog->size_text[0]));
+       int popupmenu_w = BC_PopupMenu::calculate_w(get_text_width(MEDIUMFONT, dialog->size_text[0])+xS(25));
+       x += text->get_w() + 2*margin;
        add_subwindow(scale_factor = new ProxyMenu(mwindow, this, x, y, popupmenu_w, ""));
        scale_factor->update_sizes();
-       x += scale_factor->get_w() + margin;
-
+       x += popupmenu_w + margin;
        ProxyTumbler *tumbler;
        add_subwindow(tumbler = new ProxyTumbler(mwindow, this, x, y));
        y += tumbler->get_h() + margin;
 
-       x = margin;
-       add_subwindow(text = new BC_Title(x, y, _("New media dimensions: ")));
+       x = x1;
+       add_subwindow(text = new BC_Title(x, y, _("Media size: ")));
        x += text->get_w() + margin;
        add_subwindow(new_dimensions = new BC_Title(x, y, ""));
        y += new_dimensions->get_h() + margin;
 
-       x = margin;
+       x = x1;
        add_subwindow(text = new BC_Title(x, y, _("Active Scale: ")));
        x += text->get_w() + margin;
        add_subwindow(active_scale = new BC_Title(x, y, ""));
+       x += xS(64);
+       add_subwindow(text = new BC_Title(x, y, _("State: ")));
+       x += text->get_w() + margin;
+       add_subwindow(active_state = new BC_Title(x, y, ""));
        y += active_scale->get_h() + margin;
 
-       x = margin;  y += 25;
+       add_subwindow(use_scaler = new ProxyUseScaler(this, x1, y));
+       y += use_scaler->get_h() + margin;
+       add_subwindow(auto_scale = new ProxyAutoScale(this, x1, y));
+       y += auto_scale->get_h() + 2*margin;
+
+       x = lmargin;  y += yS(15);
        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
                0, 0);
 
-       x = margin;
-       add_subwindow(auto_scale = new ProxyAutoScale(this, x, y));
-       y += auto_scale->get_h() + margin;
+       y += margin;
+       add_subwindow(title_bar2 = new BC_TitleBar(xs10, y, get_w()-xS(30), xS(20), xs10,
+               _("Beep on Done")));
+       y += title_bar2->get_h() + 3*margin;
+
+       x = lmargin;
+       add_subwindow(text = new BC_Title(x, y, _("Volume:")));
+       x += text->get_w() + 2*margin;
        add_subwindow(beep_on_done = new ProxyBeepOnDone(this, x, y));
-       y += beep_on_done->get_h() + margin;
+       x += beep_on_done->get_w() + margin + xS(10);
+       add_subwindow(beep_volume = new ProxyBeepVolume(this, x, y));
 
        update();
 
@@ -439,6 +500,7 @@ ProxyFormatTools::ProxyFormatTools(MWindow *mwindow, ProxyWindow *pwindow, Asset
 
 void ProxyFormatTools::update_format()
 {
+       asset->save_defaults(mwindow->defaults, "PROXY_", 1, 1, 0, 0, 0);
         FormatTools::update_format();
        pwindow->use_scaler->update();
 }
@@ -446,27 +508,38 @@ void ProxyFormatTools::update_format()
 void ProxyWindow::update()
 {
        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;
+       dialog->scale_to_text(string, dialog->new_scale);
+       scale_factor->set_text(string);
+       int new_scale = dialog->new_scale;
+       if( new_scale < 1 ) new_scale = 1;
+       int new_w = dialog->orig_w / new_scale;
        if( new_w & 1 ) ++new_w;
-       int new_h = orig_h / dialog->new_scale;
+       int new_h = dialog->orig_h / new_scale;
        if( new_h & 1 ) ++new_h;
        sprintf(string, "%dx%d", new_w, new_h);
        new_dimensions->update(string);
-       dialog->scale_to_text(string, dialog->new_scale);
-       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;
+       int scale = mwindow->edl->session->proxy_state == PROXY_ACTIVE ?
+                       mwindow->edl->session->proxy_scale :
+               mwindow->edl->session->proxy_state == PROXY_DISABLED ?
+                       mwindow->edl->session->proxy_disabled_scale : 1;
        sprintf(string, scale>1 ? "1/%d" : "%d", scale);
        active_scale->update(string);
+       const char *state = "";
+       switch( mwindow->edl->session->proxy_state ) {
+       case PROXY_INACTIVE: state = _("Off");  break;
+       case PROXY_ACTIVE:   state = _("Active");    break;
+       case PROXY_DISABLED: state = _("Disabled");  break;
+       }
+       active_state->update(state);
+       beep_on_done->update(dialog->beeper_on);
+       beep_volume->update(dialog->beeper_volume*100.f);
 }
 
 
 ProxyUseScaler::ProxyUseScaler(ProxyWindow *pwindow, int x, int y)
- : BC_CheckBox(x, y, pwindow->dialog->use_scaler, _("Use scaler   (FFMPEG only)"))
+ : BC_CheckBox(x, y, pwindow->dialog->use_scaler, _("Rescaled to project size (FFMPEG only)"))
 {
        this->pwindow = pwindow;
 }
@@ -491,7 +564,7 @@ int ProxyUseScaler::handle_event()
 }
 
 ProxyAutoScale::ProxyAutoScale(ProxyWindow *pwindow, int x, int y)
- : BC_CheckBox(x, y, pwindow->dialog->auto_scale, _("Auto proxy/scale media loads"))
+ : BC_CheckBox(x, y, pwindow->dialog->auto_scale, _("Creation of proxy on media loading"))
 {
        this->pwindow = pwindow;
 }
@@ -499,11 +572,11 @@ ProxyAutoScale::ProxyAutoScale(ProxyWindow *pwindow, int x, int y)
 void ProxyAutoScale::update()
 {
        ProxyDialog *dialog = pwindow->dialog;
-       if( dialog->new_scale == 1 ) dialog->auto_scale = 0;
+       int can_auto_scale = dialog->new_scale >= 1 ? 1 : 0;
+       if( !can_auto_scale ) dialog->auto_scale = 0;
        BC_CheckBox::update(dialog->auto_scale);
-       int can_auto_proxy = dialog->new_scale != 1 ? 1 : 0;
-       if( !can_auto_proxy &&  enabled ) disable();
-       if( can_auto_proxy  && !enabled ) enable();
+       if( !can_auto_scale &&  enabled ) disable();
+       if( can_auto_scale  && !enabled ) enable();
 }
 
 int ProxyAutoScale::handle_event()
@@ -514,14 +587,28 @@ int ProxyAutoScale::handle_event()
 }
 
 ProxyBeepOnDone::ProxyBeepOnDone(ProxyWindow *pwindow, int x, int y)
- : BC_CheckBox(x, y, pwindow->dialog->beep, _("Beep on done"))
+ : BC_CheckBox(x, y, pwindow->dialog->beeper_on)
 {
        this->pwindow = pwindow;
 }
 
 int ProxyBeepOnDone::handle_event()
 {
-       pwindow->dialog->beep = get_value();
+       pwindow->dialog->beeper_on = get_value();
+       return 1;
+}
+
+ProxyBeepVolume::ProxyBeepVolume(ProxyWindow *pwindow, int x, int y)
+ : BC_FSlider(x, y, 0, xS(160), xS(160), 0.f, 100.f,
+               pwindow->dialog->beeper_volume*100.f, 0)
+{
+       this->pwindow = pwindow;
+}
+
+int ProxyBeepVolume::handle_event()
+{
+       pwindow->dialog->beeper_volume = get_value()/100.f;
+       pwindow->dialog->beeper_on = pwindow->dialog->beeper_volume>0 ? 1 : 0;
        pwindow->update();
        return 1;
 }
@@ -550,7 +637,6 @@ int ProxyMenu::handle_event()
        for( int i = 0; i < dialog->total_sizes; i++ ) {
                if( !strcmp(get_text(), pwindow->dialog->size_text[i]) ) {
                        dialog->new_scale = pwindow->dialog->size_factors[i];
-                       if( dialog->new_scale == 1 ) dialog->use_scaler = 0;
                        pwindow->update();
                        break;
                }
@@ -618,7 +704,8 @@ ProxyClient::ProxyClient(MWindow *mwindow,
 ProxyClient::~ProxyClient()
 {
        delete render_engine;
-       delete video_cache;
+       if( video_cache )
+               video_cache->remove_user();
        delete src_file;
 }
 
@@ -654,7 +741,7 @@ void ProxyClient::process_package(LoadPackage *ptr)
                        result = 1;
        }
        else {
-               TransportCommand command;
+               TransportCommand command(preferences);
                command.command = CURRENT_FRAME;
                command.get_edl()->copy_all((EDL *)orig);
                command.change_type = CHANGE_ALL;
@@ -722,8 +809,13 @@ void ProxyClient::process_package(LoadPackage *ptr)
                proxy_render->update_progress();
        }
        if( !proxy_render->failed && !proxy_render->is_canceled() ) {
-               Asset *asset = mwindow->edl->assets->update(proxy);
-               mwindow->mainindexes->add_next_asset(0, asset);
+               Asset *asset = edl->assets->update(proxy);
+               asset->proxy_scale = proxy_render->asset_scale;
+               int scale = asset->proxy_scale;
+               if( !scale ) scale = 1;
+               asset->width = asset->actual_width * scale;
+               asset->height = asset->actual_height * scale;
+               mwindow->mainindexes->add_indexable(asset);
                mwindow->mainindexes->start_build();
        }
        else
@@ -762,85 +854,3 @@ 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();
-}
-