yet another proxy ref fix
[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         proxy->add_user();
355         orig_proxies.append(proxy);
356         idxbl->add_user();
357         orig_idxbls.append(idxbl);
358         return proxy;
359 }
360
361 void ProxyRender::add_needed(Indexable *idxbl, Asset *proxy)
362 {
363         needed_idxbls.append(idxbl);
364         idxbl->add_user();
365         needed_proxies.append(proxy);
366         proxy->add_user();
367 }
368
369 void ProxyRender::update_progress()
370 {
371         counter_lock->lock();
372         ++total_rendered;
373         counter_lock->unlock();
374         progress->update(total_rendered);
375 }
376
377 int ProxyRender::is_canceled()
378 {
379         return progress->is_cancelled();
380 }
381
382 int ProxyRender::create_needed_proxies(int new_scale)
383 {
384         if( !needed_proxies.size() ) return 0;
385         total_rendered = 0;
386         failed = 0;  canceled = 0;
387
388 // create proxy assets which don't already exist
389         int64_t total_len = 0;
390         for( int i = 0; i < needed_idxbls.size(); i++ ) {
391                 total_len += needed_idxbls[i]->get_video_frames();
392         }
393
394 // start progress bar.  MWindow is locked inside this
395         progress = mwindow->mainprogress->
396                 start_progress(_("Creating proxy files..."), total_len);
397
398         ProxyFarm engine(mwindow, this, &needed_idxbls, &needed_proxies);
399         engine.process_packages();
400 printf("failed=%d canceled=%d\n", failed, progress->is_cancelled());
401
402 // stop progress bar
403         canceled = progress->is_cancelled();
404         progress->stop_progress();
405         delete progress;  progress = 0;
406
407         if( failed && !canceled ) {
408                 eprintf("Error making proxy.");
409         }
410
411         return !failed && !canceled ? 0 : 1;
412 }
413
414
415 ProxyWindow::ProxyWindow(MWindow *mwindow, ProxyDialog *dialog, int x, int y)
416  : BC_Window(_(PROGRAM_NAME ": Proxy settings"), x, y, WIDTH, HEIGHT,
417                 -1, -1, 0, 0, 1)
418 {
419         this->mwindow = mwindow;
420         this->dialog = dialog;
421         format_tools = 0;
422 }
423
424 ProxyWindow::~ProxyWindow()
425 {
426         lock_window("ProxyWindow::~ProxyWindow");
427         delete format_tools;
428         unlock_window();
429 }
430
431
432 void ProxyWindow::create_objects()
433 {
434         lock_window("ProxyWindow::create_objects");
435         int margin = mwindow->theme->widget_border;
436
437         dialog->use_scaler = mwindow->edl->session->proxy_use_scaler;
438         dialog->orig_scale = mwindow->edl->session->proxy_scale;
439         dialog->auto_scale = mwindow->edl->session->proxy_auto_scale;
440         dialog->new_scale = dialog->orig_scale;
441
442         int x = margin;
443         int y = margin+10;
444         add_subwindow(use_scaler = new ProxyUseScaler(this, x, y));
445         y += use_scaler->get_h() + margin;
446
447         BC_Title *text;
448         add_subwindow(text = new BC_Title(x, y, _("Scale factor:")));
449         x += text->get_w() + margin;
450
451         dialog->size_text[0] = cstrdup(_("Original size"));
452         dialog->size_factors[0] = 1;
453         dialog->total_sizes = 1;
454         int popupmenu_w = BC_PopupMenu::calculate_w(get_text_width(MEDIUMFONT, dialog->size_text[0]));
455         add_subwindow(scale_factor = new ProxyMenu(mwindow, this, x, y, popupmenu_w, ""));
456         scale_factor->update_sizes();
457         x += scale_factor->get_w() + margin;
458
459         ProxyTumbler *tumbler;
460         add_subwindow(tumbler = new ProxyTumbler(mwindow, this, x, y));
461         y += tumbler->get_h() + margin;
462
463         x = margin;
464         add_subwindow(text = new BC_Title(x, y, _("New media dimensions: ")));
465         x += text->get_w() + margin;
466         add_subwindow(new_dimensions = new BC_Title(x, y, ""));
467         y += new_dimensions->get_h() + margin;
468
469         x = margin;  y += 25;
470         format_tools = new ProxyFormatTools(mwindow, this, dialog->asset);
471         format_tools->create_objects(x, y, 0, 1, 0, 0, 0, 1, 0, 1, // skip the path
472                 0, 0);
473
474         x = margin;
475         add_subwindow(auto_scale = new ProxyAutoScale(this, x, y));
476         y += auto_scale->get_h() + margin;
477
478         update();
479
480         add_subwindow(new BC_OKButton(this));
481         add_subwindow(new BC_CancelButton(this));
482         show_window(1);
483         unlock_window();
484 }
485
486 ProxyFormatTools::ProxyFormatTools(MWindow *mwindow, ProxyWindow *pwindow, Asset *asset)
487  : FormatTools(mwindow, pwindow, asset)
488 {
489         this->pwindow = pwindow;
490 }
491
492 void ProxyFormatTools::update_format()
493 {
494         FormatTools::update_format();
495         pwindow->use_scaler->update();
496 }
497
498 void ProxyWindow::update()
499 {
500 // preview the new size
501         char string[BCTEXTLEN];
502 //printf("ProxyWindow::update %d %d %d %d %d\n", 
503 // __LINE__, mwindow->edl->session->output_w, mwindow->edl->session->output_h,
504 // dialog->orig_scale, dialog->new_scale);
505         int orig_w = mwindow->edl->session->output_w * dialog->orig_scale;
506         int orig_h = mwindow->edl->session->output_h * dialog->orig_scale;
507         int new_w = orig_w / dialog->new_scale;
508         if( new_w & 1 ) ++new_w;
509         int new_h = orig_h / dialog->new_scale;
510         if( new_h & 1 ) ++new_h;
511         sprintf(string, "%dx%d", new_w, new_h);
512         new_dimensions->update(string);
513         dialog->scale_to_text(string, dialog->new_scale);
514         scale_factor->set_text(string);
515         use_scaler->update();
516         auto_scale->update();
517 }
518
519
520 ProxyUseScaler::ProxyUseScaler(ProxyWindow *pwindow, int x, int y)
521  : BC_CheckBox(x, y, pwindow->dialog->use_scaler, _("Use scaler   (FFMPEG only)"))
522 {
523         this->pwindow = pwindow;
524 }
525
526 void ProxyUseScaler::update()
527 {
528         ProxyDialog *dialog = pwindow->dialog;
529         if( dialog->asset->format != FILE_FFMPEG ) dialog->use_scaler = 0;
530         BC_CheckBox::update(dialog->use_scaler);
531         int scaler_avail = dialog->asset->format == FILE_FFMPEG ? 1 : 0;
532         if( !scaler_avail &&  enabled ) disable();
533         if( scaler_avail  && !enabled ) enable();
534 }
535
536 int ProxyUseScaler::handle_event()
537 {
538         pwindow->dialog->new_scale = 1;
539         pwindow->dialog->use_scaler = get_value();
540         pwindow->scale_factor->update_sizes();
541         pwindow->update();
542         return 1;
543 }
544
545 ProxyAutoScale::ProxyAutoScale(ProxyWindow *pwindow, int x, int y)
546  : BC_CheckBox(x, y, pwindow->dialog->use_scaler, _("Auto proxy/scale media loads"))
547 {
548         this->pwindow = pwindow;
549 }
550
551 void ProxyAutoScale::update()
552 {
553         ProxyDialog *dialog = pwindow->dialog;
554         if( dialog->new_scale == 1 ) dialog->auto_scale = 0;
555         BC_CheckBox::update(dialog->auto_scale);
556         int can_auto_proxy = dialog->new_scale != 1 ? 1 : 0;
557         if( !can_auto_proxy &&  enabled ) disable();
558         if( can_auto_proxy  && !enabled ) enable();
559 }
560
561 int ProxyAutoScale::handle_event()
562 {
563         pwindow->dialog->auto_scale = get_value();
564         pwindow->update();
565         return 1;
566 }
567
568
569 ProxyMenu::ProxyMenu(MWindow *mwindow, ProxyWindow *pwindow,
570                 int x, int y, int w, const char *text)
571  : BC_PopupMenu(x, y, w, text, 1)
572 {
573         this->mwindow = mwindow;
574         this->pwindow = pwindow;
575 }
576
577 void ProxyMenu::update_sizes()
578 {
579         while( total_items() > 0 ) del_item(0);
580         ProxyDialog *dialog = pwindow->dialog;
581         dialog->calculate_sizes();
582         for( int i=0; i < dialog->total_sizes; i++ )
583                 add_item(new BC_MenuItem(dialog->size_text[i]));
584 }
585
586 int ProxyMenu::handle_event()
587 {
588         ProxyDialog *dialog = pwindow->dialog;
589         for( int i = 0; i < dialog->total_sizes; i++ ) {
590                 if( !strcmp(get_text(), pwindow->dialog->size_text[i]) ) {
591                         dialog->new_scale = pwindow->dialog->size_factors[i];
592                         if( dialog->new_scale == 1 ) dialog->use_scaler = 0;
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         delete video_cache;
661         delete src_file;
662 }
663
664 void ProxyClient::process_package(LoadPackage *ptr)
665 {
666         if( proxy_render->failed ) return;
667         if( proxy_render->is_canceled() ) return;
668
669         EDL *edl = mwindow->edl;
670         Preferences *preferences = mwindow->preferences;
671         ProxyPackage *package = (ProxyPackage*)ptr;
672         Indexable *orig = package->orig_idxbl;
673         Asset *proxy = package->proxy_asset;
674 //printf("%s %s\n", orig->path, proxy->path);
675         VRender *vrender = 0;
676         int processors = 1, result = 0;
677
678         if( orig->is_asset ) {
679                 src_file = new File;
680                 src_file->set_processors(processors);
681                 src_file->set_preload(edl->session->playback_preload);
682                 src_file->set_subtitle(edl->session->decode_subtitles ? 
683                         edl->session->subtitle_number : -1);
684                 src_file->set_interpolate_raw(edl->session->interpolate_raw);
685                 src_file->set_white_balance_raw(edl->session->white_balance_raw);
686                 if( src_file->open_file(preferences, (Asset*)orig, 1, 0) != FILE_OK )
687                         result = 1;
688         }
689         else {
690                 TransportCommand command;
691                 command.command = CURRENT_FRAME;
692                 command.get_edl()->copy_all((EDL *)orig);
693                 command.change_type = CHANGE_ALL;
694                 command.realtime = 0;
695                 render_engine = new RenderEngine(0, preferences, 0, 0);
696                 render_engine->set_vcache(video_cache = new CICache(preferences));
697                 render_engine->arm_command(&command);
698                 if( !(vrender = render_engine->vrender) )
699                         result = 1;
700         }
701         if( result ) {
702 // go to the next asset if the reader fails
703 //              proxy_render->failed = 1;
704                 return;
705         }
706
707         File dst_file;
708         dst_file.set_processors(processors);
709         result = dst_file.open_file(preferences, proxy, 0, 1);
710         if( result ) {
711                 proxy_render->failed = 1;
712                 return;
713         }
714
715         dst_file.start_video_thread(1, edl->session->color_model,
716                         processors > 1 ? 2 : 1, 0);
717
718         int src_w = orig->get_w(), src_h = orig->get_h();
719         VFrame src_frame(src_w,src_h, edl->session->color_model);
720
721         OverlayFrame scaler(processors);
722
723         for( int64_t i=0, length=orig->get_video_frames(); i<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 }
755
756
757 ProxyFarm::ProxyFarm(MWindow *mwindow, ProxyRender *proxy_render,
758                 ArrayList<Indexable*> *orig_idxbls,
759                 ArrayList<Asset*> *proxy_assets)
760  : LoadServer(MIN(mwindow->preferences->processors, proxy_assets->size()), 
761         proxy_assets->size())
762 {
763         this->mwindow = mwindow;
764         this->proxy_render = proxy_render;
765         this->orig_idxbls = orig_idxbls;
766         this->proxy_assets = proxy_assets;
767 }
768
769 void ProxyFarm::init_packages()
770 {
771         for( int i = 0; i < get_total_packages(); i++ ) {
772                 ProxyPackage *package = (ProxyPackage*)get_package(i);
773                 package->proxy_asset = proxy_assets->get(i);
774                 package->orig_idxbl = orig_idxbls->get(i);
775         }
776 }
777
778 LoadClient* ProxyFarm::new_client()
779 {
780         return new ProxyClient(mwindow, proxy_render, this);
781 }
782
783 LoadPackage* ProxyFarm::new_package()
784 {
785         return new ProxyPackage;
786 }
787