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