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