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 "bcsignals.h"
26 #include "confirmsave.h"
29 #include "edlsession.h"
32 #include "filesystem.h"
33 #include "formattools.h"
35 #include "mainerror.h"
36 #include "mainprogress.h"
40 #include "mwindowgui.h"
41 #include "overlayframe.h"
42 #include "preferences.h"
44 #include "renderengine.h"
46 #include "transportque.h"
53 ProxyMenuItem::ProxyMenuItem(MWindow *mwindow)
54 : BC_MenuItem(_("Proxy settings..."), _("Alt-P"), 'p')
56 this->mwindow = mwindow;
60 void ProxyMenuItem::create_objects()
62 dialog = new ProxyDialog(mwindow);
65 int ProxyMenuItem::handle_event()
67 mwindow->gui->unlock_window();
69 mwindow->gui->lock_window("ProxyMenuItem::handle_event");
74 ProxyDialog::ProxyDialog(MWindow *mwindow)
76 this->mwindow = mwindow;
79 bzero(size_text, sizeof(char*) * MAX_SIZES);
80 bzero(size_factors, sizeof(int) * MAX_SIZES);
84 ProxyDialog::~ProxyDialog()
87 for( int i=0; i<MAX_SIZES; ++i ) delete [] size_text[i];
91 BC_Window* ProxyDialog::new_gui()
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");
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();
104 void ProxyDialog::scale_to_text(char *string, int scale)
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]);
116 void ProxyDialog::calculate_sizes()
118 for( int i=1; i<total_sizes; ++i ) {
119 delete [] size_text[i];
125 int orig_w = mwindow->edl->session->output_w * orig_scale;
126 int orig_h = mwindow->edl->session->output_h * orig_scale;
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) ) {
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;
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;
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);
153 void ProxyDialog::handle_close_event(int result)
155 asset->save_defaults(mwindow->defaults, "PROXY_", 1, 1, 0, 0, 0);
161 void ProxyDialog::to_proxy()
163 ArrayList<Indexable*> orig_idxbls;
164 ArrayList<Indexable*> proxy_assets;
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);
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
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();
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);
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();
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();
212 ArrayList<char *> confirm_paths; // test for new files
213 confirm_paths.set_array_delete();
215 // convert to new size if not original size
216 if( new_scale != 1 ) {
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;
226 if( exists ) // prompt user to overwrite
227 confirm_paths.append(cstrdup(proxy->path));
228 proxy_render.add_needed(orig, proxy);
234 // test for existing files
235 if( confirm_paths.size() ) {
236 result = ConfirmSave::test_files(mwindow, &confirm_paths);
237 confirm_paths.remove_all_objects();
241 result = proxy_render.create_needed_proxies(new_scale);
243 if( !result ) // resize project
244 mwindow->set_proxy(use_scaler, new_scale, auto_scale,
245 &proxy_render.orig_idxbls, &proxy_render.orig_proxies);
247 mwindow->undo->update_undo_after(_("proxy"), LOAD_ALL);
248 mwindow->edl->Garbage::remove_user();
249 mwindow->restart_brender();
251 mwindow->gui->lock_window("ProxyDialog::to_proxy");
252 mwindow->update_project(LOAD_ALL);
253 mwindow->gui->unlock_window();
257 void ProxyRender::to_proxy_path(char *new_path, Indexable *indexable, int scale)
259 // path is already a proxy
260 if( strstr(indexable->path, ".proxy") ) return;
261 strcpy(new_path, indexable->path);
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, '.');
268 char *bp = ep, *cp = (ep += n);
269 while( --bp > sfx ) *--cp = *bp;
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);
279 while( *ext ) *ep++ = *ext++;
281 //printf("ProxyRender::to_proxy_path %d %s %s\n", __LINE__, new_path), asset->path);
284 void ProxyRender::from_proxy_path(char *new_path, Asset *asset, int scale)
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);
295 for( *cp='.'; cp<ext; ++cp ) *ptr++ = *cp;
299 ProxyRender::ProxyRender(MWindow *mwindow, Asset *format_asset)
301 this->mwindow = mwindow;
302 this->format_asset = format_asset;
304 counter_lock = new Mutex("ProxyDialog::counter_lock");
306 failed = 0; canceled = 0;
309 ProxyRender::~ProxyRender()
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();
320 Asset *ProxyRender::add_original(Indexable *idxbl, int new_scale)
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.
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);
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;
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);
355 orig_proxies.append(proxy);
357 orig_idxbls.append(idxbl);
361 void ProxyRender::add_needed(Indexable *idxbl, Asset *proxy)
363 needed_idxbls.append(idxbl);
365 needed_proxies.append(proxy);
369 void ProxyRender::update_progress()
371 counter_lock->lock();
373 counter_lock->unlock();
374 progress->update(total_rendered);
377 int ProxyRender::is_canceled()
379 return progress->is_cancelled();
382 int ProxyRender::create_needed_proxies(int new_scale)
384 if( !needed_proxies.size() ) return 0;
386 failed = 0; canceled = 0;
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();
394 // start progress bar. MWindow is locked inside this
395 progress = mwindow->mainprogress->
396 start_progress(_("Creating proxy files..."), total_len);
398 ProxyFarm engine(mwindow, this, &needed_idxbls, &needed_proxies);
399 engine.process_packages();
400 printf("failed=%d canceled=%d\n", failed, progress->is_cancelled());
403 canceled = progress->is_cancelled();
404 progress->stop_progress();
405 delete progress; progress = 0;
407 if( failed && !canceled ) {
408 eprintf("Error making proxy.");
411 return !failed && !canceled ? 0 : 1;
415 ProxyWindow::ProxyWindow(MWindow *mwindow, ProxyDialog *dialog, int x, int y)
416 : BC_Window(_(PROGRAM_NAME ": Proxy settings"), x, y, WIDTH, HEIGHT,
419 this->mwindow = mwindow;
420 this->dialog = dialog;
424 ProxyWindow::~ProxyWindow()
426 lock_window("ProxyWindow::~ProxyWindow");
432 void ProxyWindow::create_objects()
434 lock_window("ProxyWindow::create_objects");
435 int margin = mwindow->theme->widget_border;
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;
444 add_subwindow(use_scaler = new ProxyUseScaler(this, x, y));
445 y += use_scaler->get_h() + margin;
448 add_subwindow(text = new BC_Title(x, y, _("Scale factor:")));
449 x += text->get_w() + margin;
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;
459 ProxyTumbler *tumbler;
460 add_subwindow(tumbler = new ProxyTumbler(mwindow, this, x, y));
461 y += tumbler->get_h() + 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;
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
475 add_subwindow(auto_scale = new ProxyAutoScale(this, x, y));
476 y += auto_scale->get_h() + margin;
480 add_subwindow(new BC_OKButton(this));
481 add_subwindow(new BC_CancelButton(this));
486 ProxyFormatTools::ProxyFormatTools(MWindow *mwindow, ProxyWindow *pwindow, Asset *asset)
487 : FormatTools(mwindow, pwindow, asset)
489 this->pwindow = pwindow;
492 void ProxyFormatTools::update_format()
494 FormatTools::update_format();
495 pwindow->use_scaler->update();
498 void ProxyWindow::update()
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();
520 ProxyUseScaler::ProxyUseScaler(ProxyWindow *pwindow, int x, int y)
521 : BC_CheckBox(x, y, pwindow->dialog->use_scaler, _("Use scaler (FFMPEG only)"))
523 this->pwindow = pwindow;
526 void ProxyUseScaler::update()
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();
536 int ProxyUseScaler::handle_event()
538 pwindow->dialog->new_scale = 1;
539 pwindow->dialog->use_scaler = get_value();
540 pwindow->scale_factor->update_sizes();
545 ProxyAutoScale::ProxyAutoScale(ProxyWindow *pwindow, int x, int y)
546 : BC_CheckBox(x, y, pwindow->dialog->use_scaler, _("Auto proxy/scale media loads"))
548 this->pwindow = pwindow;
551 void ProxyAutoScale::update()
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();
561 int ProxyAutoScale::handle_event()
563 pwindow->dialog->auto_scale = get_value();
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)
573 this->mwindow = mwindow;
574 this->pwindow = pwindow;
577 void ProxyMenu::update_sizes()
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]));
586 int ProxyMenu::handle_event()
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;
601 ProxyTumbler::ProxyTumbler(MWindow *mwindow, ProxyWindow *pwindow, int x, int y)
602 : BC_Tumbler(x, y, 0)
604 this->mwindow = mwindow;
605 this->pwindow = pwindow;
608 int ProxyTumbler::handle_up_event()
610 if( pwindow->dialog->new_scale > 1 ) {
612 for( i = 0; i < pwindow->dialog->total_sizes; i++ ) {
613 if( pwindow->dialog->new_scale == pwindow->dialog->size_factors[i] ) {
615 pwindow->dialog->new_scale = pwindow->dialog->size_factors[i];
625 int ProxyTumbler::handle_down_event()
628 for( i = 0; i < pwindow->dialog->total_sizes - 1; i++ ) {
629 if( pwindow->dialog->new_scale == pwindow->dialog->size_factors[i] ) {
631 pwindow->dialog->new_scale = pwindow->dialog->size_factors[i];
642 ProxyPackage::ProxyPackage()
647 ProxyClient::ProxyClient(MWindow *mwindow,
648 ProxyRender *proxy_render, ProxyFarm *server)
651 this->mwindow = mwindow;
652 this->proxy_render = proxy_render;
657 ProxyClient::~ProxyClient()
659 delete render_engine;
664 void ProxyClient::process_package(LoadPackage *ptr)
666 if( proxy_render->failed ) return;
667 if( proxy_render->is_canceled() ) return;
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;
678 if( orig->is_asset ) {
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 )
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) )
702 // go to the next asset if the reader fails
703 // proxy_render->failed = 1;
708 dst_file.set_processors(processors);
709 result = dst_file.open_file(preferences, proxy, 0, 1);
711 proxy_render->failed = 1;
715 dst_file.start_video_thread(1, edl->session->color_model,
716 processors > 1 ? 2 : 1, 0);
718 int src_w = orig->get_w(), src_h = orig->get_h();
719 VFrame src_frame(src_w,src_h, edl->session->color_model);
721 OverlayFrame scaler(processors);
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);
730 result = vrender->process_buffer(&src_frame, i, 0);
731 //printf("result=%d\n", result);
734 // go to the next asset if the reader fails
735 // proxy_render->failed = 1;
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);
748 // only fail if the writer fails
749 proxy_render->failed = 1;
752 proxy_render->update_progress();
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())
763 this->mwindow = mwindow;
764 this->proxy_render = proxy_render;
765 this->orig_idxbls = orig_idxbls;
766 this->proxy_assets = proxy_assets;
769 void ProxyFarm::init_packages()
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);
778 LoadClient* ProxyFarm::new_client()
780 return new ProxyClient(mwindow, proxy_render, this);
783 LoadPackage* ProxyFarm::new_package()
785 return new ProxyPackage;