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