add missing GPL information in guicast program files
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / proxy.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2015 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 "assets.h"
23 #include "audiodevice.h"
24 #include "bcsignals.h"
25 #include "cache.h"
26 #include "clip.h"
27 #include "cstrdup.h"
28 #include "edl.h"
29 #include "edlsession.h"
30 #include "errorbox.h"
31 #include "file.h"
32 #include "filesystem.h"
33 #include "formattools.h"
34 #include "language.h"
35 #include "mainerror.h"
36 #include "mainindexes.h"
37 #include "mainprogress.h"
38 #include "mainundo.h"
39 #include "mutex.h"
40 #include "mwindow.h"
41 #include "mwindowgui.h"
42 #include "overlayframe.h"
43 #include "preferences.h"
44 #include "proxy.h"
45 #include "renderengine.h"
46 #include "theme.h"
47 #include "transportque.h"
48 #include "vrender.h"
49
50 #define WIDTH xS(400)
51 #define HEIGHT yS(400)
52 #define MAX_SCALE 16
53
54 ProxyMenuItem::ProxyMenuItem(MWindow *mwindow)
55  : BC_MenuItem(_("Proxy settings..."),  _("Alt-r"), 'r')
56 {
57         this->mwindow = mwindow;
58         set_alt();
59         dialog = 0;
60 }
61 ProxyMenuItem::~ProxyMenuItem()
62 {
63         delete dialog;
64 }
65
66 void ProxyMenuItem::create_objects()
67 {
68         dialog = new ProxyDialog(mwindow);
69 }
70
71 int ProxyMenuItem::handle_event()
72 {
73         mwindow->gui->unlock_window();
74         dialog->start();
75         mwindow->gui->lock_window("ProxyMenuItem::handle_event");
76
77         return 1;
78 }
79
80 ProxyDialog::ProxyDialog(MWindow *mwindow)
81 {
82         this->mwindow = mwindow;
83         gui = 0;
84         asset = new Asset;
85
86 // quicker than some, not as good as others
87         asset->format = FILE_FFMPEG;
88         strcpy(asset->fformat, "mpeg");
89         strcpy(asset->vcodec, "mpeg.mpeg");
90         asset->ff_video_bitrate = 2000000;
91         asset->video_data = 1;
92
93         bzero(size_text, sizeof(char*) * MAX_SIZES);
94         bzero(size_factors, sizeof(int) * MAX_SIZES);
95         size_text[0] = cstrdup(_("off"));
96         size_text[1] = cstrdup(_("1"));
97         size_factors[0] = 0;
98         size_factors[1] = 1;
99         total_sizes = 2;
100 }
101
102 ProxyDialog::~ProxyDialog()
103 {
104         close_window();
105         for( int i=0; i<MAX_SIZES; ++i ) delete [] size_text[i];
106         asset->remove_user();
107 }
108
109 BC_Window* ProxyDialog::new_gui()
110 {
111         asset->format = FILE_FFMPEG;
112         asset->load_defaults(mwindow->defaults, "PROXY_", 1, 1, 0, 0, 0);
113         mwindow->gui->lock_window("ProxyDialog::new_gui");
114         int cx, cy;
115         mwindow->gui->get_abs_cursor(cx, cy);
116         gui = new ProxyWindow(mwindow, this, cx - WIDTH/2, cy - HEIGHT/2);
117         gui->create_objects();
118         mwindow->gui->unlock_window();
119         return gui;
120 }
121
122 void ProxyDialog::scale_to_text(char *string, int scale)
123 {
124         strcpy(string, size_text[0]);
125         for( int i = 0; i < total_sizes; i++ ) {
126                 if( scale == size_factors[i] ) {
127                         strcpy(string, size_text[i]);
128                         break;
129                 }
130         }
131 }
132
133
134 void ProxyDialog::calculate_sizes()
135 {
136         for( int i=2; i<total_sizes; ++i ) {
137                 delete [] size_text[i];
138                 size_text[i] = 0;
139                 size_factors[i] = 0;
140         }
141         total_sizes = 2;
142
143         if( !use_scaler ) {
144 // w,h should stay even for yuv
145                 int ow = orig_w, oh = orig_h;
146                 if( BC_CModels::is_yuv(mwindow->edl->session->color_model) ) {
147                         ow /= 2;  oh /= 2;
148                 }
149                 for( int i=2; i<MAX_SCALE; ++i ) {
150                         if( (ow % i) != 0 ) continue;
151                         if( (oh % i) != 0 ) continue;
152                         size_factors[total_sizes++] = i;
153                 }
154         }
155         else {
156                 size_factors[total_sizes++] = 2;   size_factors[total_sizes++] = 3;
157                 size_factors[total_sizes++] = 8;   size_factors[total_sizes++] = 12;
158                 size_factors[total_sizes++] = 16;  size_factors[total_sizes++] = 24;
159                 size_factors[total_sizes++] = 32;
160         }
161         for( int i=2; i<total_sizes; ++i ) {
162                 char string[BCTEXTLEN];
163                 sprintf(string, "1/%d", size_factors[i]);
164                 size_text[i] = cstrdup(string);
165         }
166 }
167
168 void ProxyDialog::handle_close_event(int result)
169 {
170         gui = 0;
171         if( result ) return;
172         if( !File::renders_video(asset) ) {
173                 eprintf(_("Specified format does not render video"));
174                 return;
175         }
176         mwindow->edl->session->proxy_auto_scale = auto_scale;
177         mwindow->edl->session->proxy_beep = beeper_volume;
178         asset->save_defaults(mwindow->defaults, "PROXY_", 1, 1, 0, 0, 0); 
179         result = mwindow->to_proxy(asset, new_scale, use_scaler);
180         if( result >= 0 && beeper_on && beeper_volume > 0 && new_scale >= 1 ) {
181                 if( !result ) {
182                         mwindow->beep(4000., 0.5, beeper_volume);
183                         usleep(250000);
184                         mwindow->beep(1000., 0.5, beeper_volume);
185                         usleep(250000);
186                         mwindow->beep(4000., 0.5, beeper_volume);
187                 }
188                 else
189                         mwindow->beep(2000., 2.0, beeper_volume);
190         }
191         mwindow->edl->session->proxy_disabled_scale = 1;
192         mwindow->gui->lock_window("ProxyDialog::handle_close_event");
193         mwindow->update_project(LOADMODE_REPLACE);
194         mwindow->gui->unlock_window();
195 }
196
197 void ProxyRender::to_proxy_path(char *new_path, Indexable *indexable, int scale)
198 {
199 // path is already a proxy
200         if( strstr(indexable->path, ".proxy") ) return;
201         if( !indexable->is_asset ) {
202                 char *ifn = indexable->path, *cp = strrchr(ifn, '/');
203                 if( cp ) ifn = cp+1;
204                 char proxy_path[BCTEXTLEN];
205                 File::getenv_path(proxy_path,
206                         mwindow->preferences->nested_proxy_path);
207                 sprintf(new_path, "%s/%s", proxy_path, ifn);
208         }
209         else
210                 strcpy(new_path, indexable->path);
211         char prxy[BCSTRLEN];
212         int n = sprintf(prxy, ".proxy%d", scale);
213 // insert proxy, path.sfx => path.proxy#-sfx.ext
214         char *ep = new_path + strlen(new_path);
215         char *sfx = strrchr(new_path, '.');
216         if( sfx ) {
217                 char *bp = ep, *cp = (ep += n);
218                 while( --bp > sfx ) *--cp = *bp;
219                 *--cp = '-';
220         }
221         else {
222                 sfx = ep;  ep += n;
223         }
224         for( char *cp=prxy; --n>=0; ++cp ) *sfx++ = *cp;
225         *ep++ = '.';
226         const char *ext = indexable->get_video_frames() < 0 ?  "png" :
227                 format_asset->format == FILE_FFMPEG ?
228                         format_asset->fformat :
229                         File::get_tag(format_asset->format);
230         while( *ext ) *ep++ = *ext++;
231         *ep = 0;
232 //printf("ProxyRender::to_proxy_path %d %s %s\n", __LINE__, new_path), asset->path);
233 }
234
235 int ProxyRender::from_proxy_path(char *new_path, Asset *asset, int scale)
236 {
237         char prxy[BCTEXTLEN];
238         int n = sprintf(prxy, ".proxy%d", scale);
239         strcpy(new_path, asset->path);
240         char *ptr = strstr(new_path, prxy);
241         if( !ptr || (ptr[n] != '-' && ptr[n] != '.') ) return 1;
242 // remove proxy, path.proxy#-sfx.ext => path.sfx
243         char *ext = strrchr(ptr, '.');
244         if( !ext ) ext = ptr + strlen(ptr);
245         char *cp = ptr + n;
246         for( *cp='.'; cp<ext; ++cp ) *ptr++ = *cp;
247         *ptr = 0;
248         if( asset->proxy_edl ) {
249                 if( (cp = strrchr(new_path, '/')) != 0 ) {
250                         for( ptr=new_path; *++cp; ) *ptr++ = *cp;
251                         *ptr = 0;
252                 }
253         }
254         return 0;
255 }
256
257 ProxyRender::ProxyRender(MWindow *mwindow, Asset *format_asset, int asset_scale)
258 {
259         this->mwindow = mwindow;
260         this->format_asset = format_asset;
261         this->asset_scale = asset_scale;
262         progress = 0;
263         counter_lock = new Mutex("ProxyDialog::counter_lock");
264         total_rendered = 0;
265         failed = 0;  canceled = 0;
266 }
267
268 ProxyRender::~ProxyRender()
269 {
270         delete progress;
271         delete counter_lock;
272
273         for( int i=0,n=orig_idxbls.size(); i<n; ++i ) orig_idxbls[i]->remove_user();
274         for( int i=0,n=orig_proxies.size(); i<n; ++i ) orig_proxies[i]->remove_user();
275         for( int i=0,n=needed_idxbls.size(); i<n; ++i ) needed_idxbls[i]->remove_user();
276         for( int i=0,n=needed_proxies.size(); i<n; ++i ) needed_proxies[i]->remove_user();
277 }
278
279 Asset *ProxyRender::add_original(Indexable *idxbl, int new_scale)
280 {
281         if( !idxbl->have_video() ) return 0;
282 // don't proxy proxies
283         if( strstr(idxbl->path,".proxy") ) return 0;
284         char new_path[BCTEXTLEN];
285         to_proxy_path(new_path, idxbl, new_scale);
286 // don't proxy if not readable, or proxy_path not writable
287         if( idxbl->is_asset && access(idxbl->path, R_OK) ) return 0;
288         int ret = access(new_path, W_OK);
289         if( ret ) {
290                 int fd = ::open(new_path,O_WRONLY);
291                 if( fd < 0 ) fd = open(new_path,O_WRONLY+O_CREAT,0666);
292                 if( fd >= 0 ) { close(fd);  ret = 0; }
293         }
294         if( ret ) {
295                 eprintf(_("bad proxy path: %s\n"), new_path);
296                 return 0;
297         }
298 // add to orig_idxbls & orig_proxies if it isn't already there.
299         int got_it = 0;
300         for( int i = 0; !got_it && i<orig_proxies.size(); ++i )
301                 got_it = !strcmp(orig_proxies[i]->path, new_path);
302         if( got_it ) return 0;
303         Assets *edl_assets = mwindow->edl->assets;
304         Asset *proxy = edl_assets->get_asset(new_path);
305         if( !proxy ) {
306                 proxy = new Asset(new_path);
307 // new compression parameters
308                 int64_t video_frames = idxbl->get_video_frames();
309                 if( video_frames < 0 ) {
310                         proxy->format = FILE_PNG;
311                         proxy->png_use_alpha = 1;
312                         proxy->video_length = -1;
313                 }
314                 else {
315                         proxy->copy_format(format_asset, 0);
316                         proxy->video_length = video_frames;
317                 }
318                 proxy->folder_no = AW_PROXY_FOLDER;
319                 proxy->proxy_edl = !idxbl->is_asset ? 1 : 0;
320                 proxy->audio_data = 0;
321                 proxy->video_data = 1;
322                 proxy->layers = 1;
323                 proxy->width = idxbl->get_w() / new_scale;
324                 if( proxy->width & 1 ) ++proxy->width;
325                 proxy->actual_width = proxy->width;
326                 proxy->height = idxbl->get_h() / new_scale;
327                 if( proxy->height & 1 ) ++proxy->height;
328                 proxy->actual_height = proxy->height;
329                 proxy->frame_rate = idxbl->get_frame_rate();
330         }
331         orig_proxies.append(proxy);
332         idxbl->add_user();
333         orig_idxbls.append(idxbl);
334         return proxy;
335 }
336
337 void ProxyRender::add_needed(Indexable *idxbl, Asset *proxy)
338 {
339         needed_idxbls.append(idxbl);
340         idxbl->add_user();
341         needed_proxies.append(proxy);
342         proxy->add_user();
343 }
344
345 void ProxyRender::update_progress()
346 {
347         counter_lock->lock();
348         ++total_rendered;
349         counter_lock->unlock();
350         progress->update(total_rendered);
351 }
352
353 int ProxyRender::is_canceled()
354 {
355         return progress->is_cancelled();
356 }
357
358 int ProxyRender::create_needed_proxies(int new_scale)
359 {
360         if( !needed_proxies.size() ) return 0;
361         total_rendered = 0;
362         failed = 0;  canceled = 0;
363
364 // create proxy assets which don't already exist
365         int64_t total_len = 0;
366         for( int i = 0; i < needed_idxbls.size(); i++ ) {
367                 total_len += needed_idxbls[i]->get_video_frames();
368         }
369
370 // start progress bar.  MWindow is locked inside this
371         mwindow->gui->lock_window("ProxyRender::create_needed_proxies");
372         progress = mwindow->mainprogress->
373                 start_progress(_("Creating proxy files..."), total_len);
374         mwindow->gui->unlock_window();
375
376         ProxyFarm engine(mwindow, this, &needed_idxbls, &needed_proxies);
377         engine.process_packages();
378 printf("proxy: failed=%d canceled=%d\n", failed, progress->is_cancelled());
379
380 // stop progress bar
381         canceled = progress->is_cancelled();
382         progress->stop_progress();
383         delete progress;  progress = 0;
384
385         if( failed && !canceled ) {
386                 eprintf(_("Error making proxy."));
387         }
388         return !failed && !canceled ? 0 : 1;
389 }
390
391
392 ProxyWindow::ProxyWindow(MWindow *mwindow, ProxyDialog *dialog, int x, int y)
393  : BC_Window(_(PROGRAM_NAME ": Proxy settings"), x, y, WIDTH, HEIGHT,
394                 -1, -1, 0, 0, 1)
395 {
396         this->mwindow = mwindow;
397         this->dialog = dialog;
398         format_tools = 0;
399 // *** CONTEXT_HELP ***
400         context_help_set_keyword("Proxy");
401 }
402
403 ProxyWindow::~ProxyWindow()
404 {
405         lock_window("ProxyWindow::~ProxyWindow");
406         delete format_tools;
407         unlock_window();
408 }
409
410
411 void ProxyWindow::create_objects()
412 {
413         lock_window("ProxyWindow::create_objects");
414         int margin = mwindow->theme->widget_border;
415         int xs10 = xS(10), x1 = xS(50);
416         int lmargin = margin + xs10;
417         int x = lmargin, y = margin+yS(10);
418
419         dialog->use_scaler = mwindow->edl->session->proxy_use_scaler;
420         dialog->orig_scale = mwindow->edl->session->proxy_scale;
421         dialog->auto_scale = mwindow->edl->session->proxy_auto_scale;
422         dialog->beeper_on = mwindow->edl->session->proxy_beep > 0 ? 1 : 0;
423         dialog->beeper_volume = mwindow->edl->session->proxy_beep;
424         dialog->new_scale = mwindow->edl->session->proxy_state != PROXY_INACTIVE ?
425                 dialog->orig_scale : 0;
426         dialog->orig_w = mwindow->edl->session->output_w;
427         dialog->orig_h = mwindow->edl->session->output_h;
428         if( !dialog->use_scaler ) {
429                 dialog->orig_w *= dialog->orig_scale;
430                 dialog->orig_h *= dialog->orig_scale;
431         }
432
433         add_subwindow(title_bar1 = new BC_TitleBar(xs10, y, get_w()-xS(30), xS(20), xs10,
434                 _("Scaling options")));
435         y += title_bar1->get_h() + 3*margin;
436
437         BC_Title *text;
438         x = lmargin;
439         add_subwindow(text = new BC_Title(x, y, _("Scale factor:")));
440         int popupmenu_w = BC_PopupMenu::calculate_w(get_text_width(MEDIUMFONT, dialog->size_text[0])+xS(25));
441         x += text->get_w() + 2*margin;
442         add_subwindow(scale_factor = new ProxyMenu(mwindow, this, x, y, popupmenu_w, ""));
443         scale_factor->update_sizes();
444         x += popupmenu_w + margin;
445         ProxyTumbler *tumbler;
446         add_subwindow(tumbler = new ProxyTumbler(mwindow, this, x, y));
447         y += tumbler->get_h() + margin;
448
449         x = x1;
450         add_subwindow(text = new BC_Title(x, y, _("Media size: ")));
451         x += text->get_w() + margin;
452         add_subwindow(new_dimensions = new BC_Title(x, y, ""));
453         y += new_dimensions->get_h() + margin;
454
455         x = x1;
456         add_subwindow(text = new BC_Title(x, y, _("Active Scale: ")));
457         x += text->get_w() + margin;
458         add_subwindow(active_scale = new BC_Title(x, y, ""));
459         x += xS(64);
460         add_subwindow(text = new BC_Title(x, y, _("State: ")));
461         x += text->get_w() + margin;
462         add_subwindow(active_state = new BC_Title(x, y, ""));
463         y += active_scale->get_h() + margin;
464
465         add_subwindow(use_scaler = new ProxyUseScaler(this, x1, y));
466         y += use_scaler->get_h() + margin;
467         add_subwindow(auto_scale = new ProxyAutoScale(this, x1, y));
468         y += auto_scale->get_h() + 2*margin;
469
470         x = lmargin;  y += yS(15);
471         format_tools = new ProxyFormatTools(mwindow, this, dialog->asset);
472         format_tools->create_objects(x, y, 0, 1, 0, 0, 0, 1, 0, 1, // skip the path
473                 0, 0);
474
475         y += margin;
476         add_subwindow(title_bar2 = new BC_TitleBar(xs10, y, get_w()-xS(30), xS(20), xs10,
477                 _("Beep on Done")));
478         y += title_bar2->get_h() + 3*margin;
479
480         x = lmargin;
481         add_subwindow(text = new BC_Title(x, y, _("Volume:")));
482         x += text->get_w() + 2*margin;
483         add_subwindow(beep_on_done = new ProxyBeepOnDone(this, x, y));
484         x += beep_on_done->get_w() + margin + xS(10);
485         add_subwindow(beep_volume = new ProxyBeepVolume(this, x, y));
486
487         update();
488
489         add_subwindow(new BC_OKButton(this));
490         add_subwindow(new BC_CancelButton(this));
491         show_window(1);
492         unlock_window();
493 }
494
495 ProxyFormatTools::ProxyFormatTools(MWindow *mwindow, ProxyWindow *pwindow, Asset *asset)
496  : FormatTools(mwindow, pwindow, asset)
497 {
498         this->pwindow = pwindow;
499 }
500
501 void ProxyFormatTools::update_format()
502 {
503         asset->save_defaults(mwindow->defaults, "PROXY_", 1, 1, 0, 0, 0);
504         FormatTools::update_format();
505         pwindow->use_scaler->update();
506 }
507
508 void ProxyWindow::update()
509 {
510         char string[BCSTRLEN];
511         dialog->scale_to_text(string, dialog->new_scale);
512         scale_factor->set_text(string);
513         int new_scale = dialog->new_scale;
514         if( new_scale < 1 ) new_scale = 1;
515         int new_w = dialog->orig_w / new_scale;
516         if( new_w & 1 ) ++new_w;
517         int new_h = dialog->orig_h / new_scale;
518         if( new_h & 1 ) ++new_h;
519         sprintf(string, "%dx%d", new_w, new_h);
520         new_dimensions->update(string);
521         use_scaler->update();
522         auto_scale->update();
523         int scale = mwindow->edl->session->proxy_state == PROXY_ACTIVE ?
524                         mwindow->edl->session->proxy_scale :
525                 mwindow->edl->session->proxy_state == PROXY_DISABLED ?
526                         mwindow->edl->session->proxy_disabled_scale : 1;
527         sprintf(string, scale>1 ? "1/%d" : "%d", scale);
528         active_scale->update(string);
529         const char *state = "";
530         switch( mwindow->edl->session->proxy_state ) {
531         case PROXY_INACTIVE: state = _("Off");  break;
532         case PROXY_ACTIVE:   state = _("Active");    break;
533         case PROXY_DISABLED: state = _("Disabled");  break;
534         }
535         active_state->update(state);
536         beep_on_done->update(dialog->beeper_on);
537         beep_volume->update(dialog->beeper_volume*100.f);
538 }
539
540
541 ProxyUseScaler::ProxyUseScaler(ProxyWindow *pwindow, int x, int y)
542  : BC_CheckBox(x, y, pwindow->dialog->use_scaler, _("Rescaled to project size (FFMPEG only)"))
543 {
544         this->pwindow = pwindow;
545 }
546
547 void ProxyUseScaler::update()
548 {
549         ProxyDialog *dialog = pwindow->dialog;
550         if( dialog->asset->format != FILE_FFMPEG ) dialog->use_scaler = 0;
551         BC_CheckBox::update(dialog->use_scaler);
552         int scaler_avail = dialog->asset->format == FILE_FFMPEG ? 1 : 0;
553         if( !scaler_avail &&  enabled ) disable();
554         if( scaler_avail  && !enabled ) enable();
555 }
556
557 int ProxyUseScaler::handle_event()
558 {
559         pwindow->dialog->new_scale = 1;
560         pwindow->dialog->use_scaler = get_value();
561         pwindow->scale_factor->update_sizes();
562         pwindow->update();
563         return 1;
564 }
565
566 ProxyAutoScale::ProxyAutoScale(ProxyWindow *pwindow, int x, int y)
567  : BC_CheckBox(x, y, pwindow->dialog->auto_scale, _("Creation of proxy on media loading"))
568 {
569         this->pwindow = pwindow;
570 }
571
572 void ProxyAutoScale::update()
573 {
574         ProxyDialog *dialog = pwindow->dialog;
575         int can_auto_scale = dialog->new_scale >= 1 ? 1 : 0;
576         if( !can_auto_scale ) dialog->auto_scale = 0;
577         BC_CheckBox::update(dialog->auto_scale);
578         if( !can_auto_scale &&  enabled ) disable();
579         if( can_auto_scale  && !enabled ) enable();
580 }
581
582 int ProxyAutoScale::handle_event()
583 {
584         pwindow->dialog->auto_scale = get_value();
585         pwindow->update();
586         return 1;
587 }
588
589 ProxyBeepOnDone::ProxyBeepOnDone(ProxyWindow *pwindow, int x, int y)
590  : BC_CheckBox(x, y, pwindow->dialog->beeper_on)
591 {
592         this->pwindow = pwindow;
593 }
594
595 int ProxyBeepOnDone::handle_event()
596 {
597         pwindow->dialog->beeper_on = get_value();
598         return 1;
599 }
600
601 ProxyBeepVolume::ProxyBeepVolume(ProxyWindow *pwindow, int x, int y)
602  : BC_FSlider(x, y, 0, xS(160), xS(160), 0.f, 100.f,
603                 pwindow->dialog->beeper_volume*100.f, 0)
604 {
605         this->pwindow = pwindow;
606 }
607
608 int ProxyBeepVolume::handle_event()
609 {
610         pwindow->dialog->beeper_volume = get_value()/100.f;
611         pwindow->dialog->beeper_on = pwindow->dialog->beeper_volume>0 ? 1 : 0;
612         pwindow->update();
613         return 1;
614 }
615
616
617 ProxyMenu::ProxyMenu(MWindow *mwindow, ProxyWindow *pwindow,
618                 int x, int y, int w, const char *text)
619  : BC_PopupMenu(x, y, w, text, 1)
620 {
621         this->mwindow = mwindow;
622         this->pwindow = pwindow;
623 }
624
625 void ProxyMenu::update_sizes()
626 {
627         while( total_items() > 0 ) del_item(0);
628         ProxyDialog *dialog = pwindow->dialog;
629         dialog->calculate_sizes();
630         for( int i=0; i < dialog->total_sizes; i++ )
631                 add_item(new BC_MenuItem(dialog->size_text[i]));
632 }
633
634 int ProxyMenu::handle_event()
635 {
636         ProxyDialog *dialog = pwindow->dialog;
637         for( int i = 0; i < dialog->total_sizes; i++ ) {
638                 if( !strcmp(get_text(), pwindow->dialog->size_text[i]) ) {
639                         dialog->new_scale = pwindow->dialog->size_factors[i];
640                         pwindow->update();
641                         break;
642                 }
643         }
644         return 1;
645 }
646
647
648 ProxyTumbler::ProxyTumbler(MWindow *mwindow, ProxyWindow *pwindow, int x, int y)
649  : BC_Tumbler(x, y, 0)
650 {
651         this->mwindow = mwindow;
652         this->pwindow = pwindow;
653 }
654
655 int ProxyTumbler::handle_up_event()
656 {
657         if( pwindow->dialog->new_scale > 1 ) {
658                 int i;
659                 for( i = 0; i < pwindow->dialog->total_sizes; i++ ) {
660                         if( pwindow->dialog->new_scale == pwindow->dialog->size_factors[i] ) {
661                                 i--;
662                                 pwindow->dialog->new_scale = pwindow->dialog->size_factors[i];
663                                 pwindow->update();
664                                 return 1;
665                         }
666                 }               
667         }
668
669         return 0;
670 }
671
672 int ProxyTumbler::handle_down_event()
673 {
674         int i;
675         for( i = 0; i < pwindow->dialog->total_sizes - 1; i++ ) {
676                 if( pwindow->dialog->new_scale == pwindow->dialog->size_factors[i] ) {
677                         i++;
678                         pwindow->dialog->new_scale = pwindow->dialog->size_factors[i];
679                         pwindow->update();
680                         return 1;
681                 }
682         }
683
684         return 0;
685 }
686
687
688
689 ProxyPackage::ProxyPackage()
690  : LoadPackage()
691 {
692 }
693
694 ProxyClient::ProxyClient(MWindow *mwindow,
695                 ProxyRender *proxy_render, ProxyFarm *server)
696  : LoadClient(server)
697 {
698         this->mwindow = mwindow;
699         this->proxy_render = proxy_render;
700         render_engine = 0;
701         video_cache = 0;
702         src_file = 0;
703 }
704 ProxyClient::~ProxyClient()
705 {
706         delete render_engine;
707         if( video_cache )
708                 video_cache->remove_user();
709         delete src_file;
710 }
711
712 void ProxyClient::process_package(LoadPackage *ptr)
713 {
714         if( proxy_render->failed ) return;
715         if( proxy_render->is_canceled() ) return;
716
717         EDL *edl = mwindow->edl;
718         Preferences *preferences = mwindow->preferences;
719         ProxyPackage *package = (ProxyPackage*)ptr;
720         Indexable *orig = package->orig_idxbl;
721         Asset *proxy = package->proxy_asset;
722 //printf("%s %s\n", orig->path, proxy->path);
723         VRender *vrender = 0;
724         int jobs = proxy_render->needed_proxies.size();
725         int processors = preferences->project_smp / jobs + 1, result = 0;
726
727 // each cpu should process at least about 1 MB, or it thrashes
728         int size = edl->session->output_w * edl->session->output_h * 4;
729         int cpus = size / 0x100000 + 1;
730         if( processors > cpus ) processors = cpus;
731
732         if( orig->is_asset ) {
733                 src_file = new File;
734                 src_file->set_processors(processors);
735                 src_file->set_preload(edl->session->playback_preload);
736                 src_file->set_subtitle(edl->session->decode_subtitles ? 
737                         edl->session->subtitle_number : -1);
738                 src_file->set_interpolate_raw(edl->session->interpolate_raw);
739                 src_file->set_white_balance_raw(edl->session->white_balance_raw);
740                 if( src_file->open_file(preferences, (Asset*)orig, 1, 0) != FILE_OK )
741                         result = 1;
742         }
743         else {
744                 TransportCommand command(preferences);
745                 command.command = CURRENT_FRAME;
746                 command.get_edl()->copy_all((EDL *)orig);
747                 command.change_type = CHANGE_ALL;
748                 command.realtime = 0;
749                 render_engine = new RenderEngine(0, preferences, 0, 0);
750                 render_engine->set_vcache(video_cache = new CICache(preferences));
751                 render_engine->arm_command(&command);
752                 if( !(vrender = render_engine->vrender) )
753                         result = 1;
754         }
755         if( result ) {
756 // go to the next asset if the reader fails
757 //              proxy_render->failed = 1;
758                 return;
759         }
760
761         File dst_file;
762         dst_file.set_processors(processors);
763         result = dst_file.open_file(preferences, proxy, 0, 1);
764         if( result ) {
765                 proxy_render->failed = 1;
766                 ::remove(proxy->path);
767                 return;
768         }
769
770         dst_file.start_video_thread(1, edl->session->color_model,
771                         processors > 1 ? 2 : 1, 0);
772
773         int src_w = orig->get_w(), src_h = orig->get_h();
774         VFrame src_frame(src_w,src_h, edl->session->color_model);
775
776         OverlayFrame scaler(processors);
777         int64_t video_length = orig->get_video_frames();
778         if( video_length < 0 ) video_length = 1;
779
780         for( int64_t i=0; i<video_length &&
781              !proxy_render->failed && !proxy_render->is_canceled(); ++i ) {
782                 if( orig->is_asset ) {
783                         src_file->set_video_position(i, 0);
784                         result = src_file->read_frame(&src_frame);
785                 }
786                 else
787                         result = vrender->process_buffer(&src_frame, i, 0);
788 //printf("result=%d\n", result);
789
790                 if( result ) {
791 // go to the next asset if the reader fails
792 //                      proxy_render->failed = 1;
793                         break;
794                 }
795
796 // have to write after getting the video buffer or it locks up
797                 VFrame ***dst_frames = dst_file.get_video_buffer();
798                 VFrame *dst_frame = dst_frames[0][0];
799                 int dst_w = dst_frame->get_w(), dst_h = dst_frame->get_h();
800                 scaler.overlay(dst_frame, &src_frame,
801                         0,0, src_w,src_h, 0,0, dst_w,dst_h,
802                         1.0, TRANSFER_REPLACE, NEAREST_NEIGHBOR);
803                 result = dst_file.write_video_buffer(1);
804                 if( result ) {
805 // only fail if the writer fails
806                         proxy_render->failed = 1;
807                         break;
808                 }
809                 proxy_render->update_progress();
810         }
811         if( !proxy_render->failed && !proxy_render->is_canceled() ) {
812                 Asset *asset = edl->assets->update(proxy);
813                 asset->proxy_scale = proxy_render->asset_scale;
814                 int scale = asset->proxy_scale;
815                 if( !scale ) scale = 1;
816                 asset->width = asset->actual_width * scale;
817                 asset->height = asset->actual_height * scale;
818                 mwindow->mainindexes->add_indexable(asset);
819                 mwindow->mainindexes->start_build();
820         }
821         else
822                 ::remove(proxy->path);
823 }
824
825
826 ProxyFarm::ProxyFarm(MWindow *mwindow, ProxyRender *proxy_render,
827                 ArrayList<Indexable*> *orig_idxbls,
828                 ArrayList<Asset*> *proxy_assets)
829  : LoadServer(MIN(mwindow->preferences->processors, proxy_assets->size()), 
830         proxy_assets->size())
831 {
832         this->mwindow = mwindow;
833         this->proxy_render = proxy_render;
834         this->orig_idxbls = orig_idxbls;
835         this->proxy_assets = proxy_assets;
836 }
837
838 void ProxyFarm::init_packages()
839 {
840         for( int i = 0; i < get_total_packages(); i++ ) {
841                 ProxyPackage *package = (ProxyPackage*)get_package(i);
842                 package->proxy_asset = proxy_assets->get(i);
843                 package->orig_idxbl = orig_idxbls->get(i);
844         }
845 }
846
847 LoadClient* ProxyFarm::new_client()
848 {
849         return new ProxyClient(mwindow, proxy_render, this);
850 }
851
852 LoadPackage* ProxyFarm::new_package()
853 {
854         return new ProxyPackage;
855 }
856