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