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