4 * Copyright (C) 2015 Adam Williams <broadcast at earthling dot net>
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.
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.
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
23 #include "audiodevice.h"
24 #include "bcsignals.h"
29 #include "edlsession.h"
32 #include "filesystem.h"
33 #include "formattools.h"
35 #include "mainerror.h"
36 #include "mainindexes.h"
37 #include "mainprogress.h"
41 #include "mwindowgui.h"
42 #include "overlayframe.h"
43 #include "preferences.h"
45 #include "renderengine.h"
47 #include "transportque.h"
54 ProxyMenuItem::ProxyMenuItem(MWindow *mwindow)
55 : BC_MenuItem(_("Proxy settings..."), _("Alt-r"), 'r')
57 this->mwindow = mwindow;
61 ProxyMenuItem::~ProxyMenuItem()
66 void ProxyMenuItem::create_objects()
68 dialog = new ProxyDialog(mwindow);
71 int ProxyMenuItem::handle_event()
73 mwindow->gui->unlock_window();
75 mwindow->gui->lock_window("ProxyMenuItem::handle_event");
80 ProxyDialog::ProxyDialog(MWindow *mwindow)
82 this->mwindow = mwindow;
85 bzero(size_text, sizeof(char*) * MAX_SIZES);
86 bzero(size_factors, sizeof(int) * MAX_SIZES);
87 size_text[0] = cstrdup(_("Original size"));
92 ProxyDialog::~ProxyDialog()
95 for( int i=0; i<MAX_SIZES; ++i ) delete [] size_text[i];
99 BC_Window* ProxyDialog::new_gui()
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");
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();
112 void ProxyDialog::scale_to_text(char *string, int scale)
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]);
124 void ProxyDialog::calculate_sizes()
126 for( int i=1; i<total_sizes; ++i ) {
127 delete [] size_text[i];
133 int orig_w = mwindow->edl->session->output_w * orig_scale;
134 int orig_h = mwindow->edl->session->output_h * orig_scale;
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) ) {
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;
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;
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);
161 void ProxyDialog::handle_close_event(int result)
164 if( !File::renders_video(asset) ) {
165 eprintf("Specified format does not render video");
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);
182 void ProxyRender::to_proxy_path(char *new_path, Indexable *indexable, int scale)
184 // path is already a proxy
185 if( strstr(indexable->path, ".proxy") ) return;
186 strcpy(new_path, indexable->path);
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, '.');
193 char *bp = ep, *cp = (ep += n);
194 while( --bp > sfx ) *--cp = *bp;
200 for( char *cp=prxy; --n>=0; ++cp ) *sfx++ = *cp;
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++;
208 //printf("ProxyRender::to_proxy_path %d %s %s\n", __LINE__, new_path), asset->path);
211 int ProxyRender::from_proxy_path(char *new_path, Indexable *indexable, int scale)
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);
222 for( *cp='.'; cp<ext; ++cp ) *ptr++ = *cp;
227 ProxyRender::ProxyRender(MWindow *mwindow, Asset *format_asset)
229 this->mwindow = mwindow;
230 this->format_asset = format_asset;
232 counter_lock = new Mutex("ProxyDialog::counter_lock");
234 failed = 0; canceled = 0;
237 ProxyRender::~ProxyRender()
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();
248 Asset *ProxyRender::add_original(Indexable *idxbl, int new_scale)
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.
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);
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;
274 proxy->copy_format(format_asset, 0);
275 proxy->video_length = video_frames;
277 proxy->awindow_folder = AW_PROXY_FOLDER;
278 proxy->audio_data = 0;
279 proxy->video_data = 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();
289 orig_proxies.append(proxy);
291 orig_idxbls.append(idxbl);
295 void ProxyRender::add_needed(Indexable *idxbl, Asset *proxy)
297 needed_idxbls.append(idxbl);
299 needed_proxies.append(proxy);
303 void ProxyRender::update_progress()
305 counter_lock->lock();
307 counter_lock->unlock();
308 progress->update(total_rendered);
311 int ProxyRender::is_canceled()
313 return progress->is_cancelled();
316 int ProxyRender::create_needed_proxies(int new_scale)
318 if( !needed_proxies.size() ) return 0;
320 failed = 0; canceled = 0;
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();
328 // start progress bar. MWindow is locked inside this
329 progress = mwindow->mainprogress->
330 start_progress(_("Creating proxy files..."), total_len);
332 ProxyFarm engine(mwindow, this, &needed_idxbls, &needed_proxies);
333 engine.process_packages();
334 printf("failed=%d canceled=%d\n", failed, progress->is_cancelled());
337 canceled = progress->is_cancelled();
338 progress->stop_progress();
339 delete progress; progress = 0;
341 if( failed && !canceled ) {
342 eprintf("Error making proxy.");
344 return !failed && !canceled ? 0 : 1;
348 ProxyWindow::ProxyWindow(MWindow *mwindow, ProxyDialog *dialog, int x, int y)
349 : BC_Window(_(PROGRAM_NAME ": Proxy settings"), x, y, WIDTH, HEIGHT,
352 this->mwindow = mwindow;
353 this->dialog = dialog;
357 ProxyWindow::~ProxyWindow()
359 lock_window("ProxyWindow::~ProxyWindow");
365 void ProxyWindow::create_objects()
367 lock_window("ProxyWindow::create_objects");
368 int margin = mwindow->theme->widget_border;
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;
378 add_subwindow(use_scaler = new ProxyUseScaler(this, x, y));
379 y += use_scaler->get_h() + margin;
382 add_subwindow(text = new BC_Title(x, y, _("Scale factor:")));
383 x += text->get_w() + margin;
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;
390 ProxyTumbler *tumbler;
391 add_subwindow(tumbler = new ProxyTumbler(mwindow, this, x, y));
392 y += tumbler->get_h() + 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;
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;
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
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;
419 add_subwindow(new BC_OKButton(this));
420 add_subwindow(new BC_CancelButton(this));
425 ProxyFormatTools::ProxyFormatTools(MWindow *mwindow, ProxyWindow *pwindow, Asset *asset)
426 : FormatTools(mwindow, pwindow, asset)
428 this->pwindow = pwindow;
431 void ProxyFormatTools::update_format()
433 FormatTools::update_format();
434 pwindow->use_scaler->update();
437 void ProxyWindow::update()
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);
459 ProxyUseScaler::ProxyUseScaler(ProxyWindow *pwindow, int x, int y)
460 : BC_CheckBox(x, y, pwindow->dialog->use_scaler, _("Use scaler (FFMPEG only)"))
462 this->pwindow = pwindow;
465 void ProxyUseScaler::update()
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();
475 int ProxyUseScaler::handle_event()
477 pwindow->dialog->new_scale = 1;
478 pwindow->dialog->use_scaler = get_value();
479 pwindow->scale_factor->update_sizes();
484 ProxyAutoScale::ProxyAutoScale(ProxyWindow *pwindow, int x, int y)
485 : BC_CheckBox(x, y, pwindow->dialog->auto_scale, _("Auto proxy/scale media loads"))
487 this->pwindow = pwindow;
490 void ProxyAutoScale::update()
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();
500 int ProxyAutoScale::handle_event()
502 pwindow->dialog->auto_scale = get_value();
507 ProxyBeepOnDone::ProxyBeepOnDone(ProxyWindow *pwindow, int x, int y)
508 : BC_CheckBox(x, y, pwindow->dialog->beep, _("Beep on done"))
510 this->pwindow = pwindow;
513 int ProxyBeepOnDone::handle_event()
515 pwindow->dialog->beep = get_value();
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)
525 this->mwindow = mwindow;
526 this->pwindow = pwindow;
529 void ProxyMenu::update_sizes()
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]));
538 int ProxyMenu::handle_event()
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;
553 ProxyTumbler::ProxyTumbler(MWindow *mwindow, ProxyWindow *pwindow, int x, int y)
554 : BC_Tumbler(x, y, 0)
556 this->mwindow = mwindow;
557 this->pwindow = pwindow;
560 int ProxyTumbler::handle_up_event()
562 if( pwindow->dialog->new_scale > 1 ) {
564 for( i = 0; i < pwindow->dialog->total_sizes; i++ ) {
565 if( pwindow->dialog->new_scale == pwindow->dialog->size_factors[i] ) {
567 pwindow->dialog->new_scale = pwindow->dialog->size_factors[i];
577 int ProxyTumbler::handle_down_event()
580 for( i = 0; i < pwindow->dialog->total_sizes - 1; i++ ) {
581 if( pwindow->dialog->new_scale == pwindow->dialog->size_factors[i] ) {
583 pwindow->dialog->new_scale = pwindow->dialog->size_factors[i];
594 ProxyPackage::ProxyPackage()
599 ProxyClient::ProxyClient(MWindow *mwindow,
600 ProxyRender *proxy_render, ProxyFarm *server)
603 this->mwindow = mwindow;
604 this->proxy_render = proxy_render;
609 ProxyClient::~ProxyClient()
611 delete render_engine;
616 void ProxyClient::process_package(LoadPackage *ptr)
618 if( proxy_render->failed ) return;
619 if( proxy_render->is_canceled() ) return;
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;
631 if( orig->is_asset ) {
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 )
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) )
655 // go to the next asset if the reader fails
656 // proxy_render->failed = 1;
661 dst_file.set_processors(processors);
662 result = dst_file.open_file(preferences, proxy, 0, 1);
664 proxy_render->failed = 1;
665 ::remove(proxy->path);
669 dst_file.start_video_thread(1, edl->session->color_model,
670 processors > 1 ? 2 : 1, 0);
672 int src_w = orig->get_w(), src_h = orig->get_h();
673 VFrame src_frame(src_w,src_h, edl->session->color_model);
675 OverlayFrame scaler(processors);
676 int64_t video_length = orig->get_video_frames();
677 if( video_length < 0 ) video_length = 1;
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);
686 result = vrender->process_buffer(&src_frame, i, 0);
687 //printf("result=%d\n", result);
690 // go to the next asset if the reader fails
691 // proxy_render->failed = 1;
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);
704 // only fail if the writer fails
705 proxy_render->failed = 1;
708 proxy_render->update_progress();
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();
716 ::remove(proxy->path);
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())
726 this->mwindow = mwindow;
727 this->proxy_render = proxy_render;
728 this->orig_idxbls = orig_idxbls;
729 this->proxy_assets = proxy_assets;
732 void ProxyFarm::init_packages()
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);
741 LoadClient* ProxyFarm::new_client()
743 return new ProxyClient(mwindow, proxy_render, this);
746 LoadPackage* ProxyFarm::new_package()
748 return new ProxyPackage;
752 ProxyBeep::ProxyBeep(MWindow *mwindow)
755 this->mwindow = mwindow;
756 audio = new AudioDevice(mwindow);
761 ProxyBeep::~ProxyBeep()
767 void ProxyBeep::run()
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();
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;
785 while( !interrupted ) {
786 int len = audio_len - audio_pos;
787 if( len <= 0 ) break;
788 if( len > bfrsz ) len = bfrsz;
790 for( int i=0; i<len; ++i,++k,th+=dt ) {
791 double t = th - two_pi;
793 out0[i] = out1[i] = sin(th) * gain;
795 audio->write_buffer(out, channels, len);
800 audio->set_last_buffer();
801 audio->stop_audio(interrupted ? 0 : 1);
807 void ProxyBeep::start()
809 if( running() ) return;
815 void ProxyBeep::stop(int wait)
817 if( running() && !interrupted ) {
819 audio->stop_audio(wait);
824 void ProxyBeep::tone(double freq, double secs, double gain)