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"
25 #include "confirmsave.h"
28 #include "edlsession.h"
31 #include "filesystem.h"
32 #include "formattools.h"
34 #include "mainprogress.h"
38 #include "mwindowgui.h"
39 #include "overlayframe.h"
40 #include "preferences.h"
48 ProxyMenuItem::ProxyMenuItem(MWindow *mwindow)
49 : BC_MenuItem(_("Proxy settings..."), _("Alt-P"), 'p')
51 this->mwindow = mwindow;
55 void ProxyMenuItem::create_objects()
57 thread = new ProxyThread(mwindow);
60 int ProxyMenuItem::handle_event()
62 mwindow->gui->unlock_window();
64 mwindow->gui->lock_window("ProxyMenuItem::handle_event");
69 ProxyThread::ProxyThread(MWindow *mwindow)
71 this->mwindow = mwindow;
75 counter_lock = new Mutex("ProxyThread::counter_lock");
76 bzero(size_text, sizeof(char*) * MAX_SIZES);
77 bzero(size_factors, sizeof(int) * MAX_SIZES);
80 ProxyThread::~ProxyThread()
82 for( int i=0; i<MAX_SIZES; ++i )
83 if( size_text[i] ) delete [] size_text[i];
86 BC_Window* ProxyThread::new_gui()
88 asset->format = FILE_FFMPEG;
89 asset->load_defaults(mwindow->defaults, "PROXY_", 1, 1, 0, 0, 0);
90 mwindow->gui->lock_window("ProxyThread::new_gui");
91 int x = mwindow->gui->get_abs_cursor_x(0) - WIDTH / 2;
92 int y = mwindow->gui->get_abs_cursor_y(0) - HEIGHT / 2;
94 gui = new ProxyWindow(mwindow, this, x, y);
95 gui->create_objects();
96 mwindow->gui->unlock_window();
100 void ProxyThread::scale_to_text(char *string, int scale)
103 strcpy(string, size_text[0]);
104 for( int i = 0; i < total_sizes; i++ ) {
105 if( scale == size_factors[i] ) {
106 strcpy(string, size_text[i]);
113 void ProxyThread::calculate_sizes()
116 for( int i = 0; i < MAX_SIZES; i++ ) {
117 if( size_text[i] ) delete [] size_text[i];
119 bzero(size_text, sizeof(char*) * MAX_SIZES);
120 bzero(size_factors, sizeof(int) * MAX_SIZES);
122 int orig_w = mwindow->edl->session->output_w * orig_scale;
123 int orig_h = mwindow->edl->session->output_h * orig_scale;
126 size_text[0] = cstrdup(_("Original size"));
129 int current_factor = 2;
131 for( current_factor = 2; current_factor < MAX_SCALE; current_factor++ ) {
132 if( current_factor * (orig_w / current_factor) == orig_w &&
133 current_factor * (orig_h / current_factor) == orig_h ) {
134 //printf("ProxyThread::calculate_sizes %d\n", current_factor);
135 char string[BCTEXTLEN];
136 sprintf(string, "1/%d", current_factor);
137 size_text[total_sizes] = cstrdup(string);
138 size_factors[total_sizes] = current_factor;
146 void ProxyThread::handle_close_event(int result)
148 asset->save_defaults(mwindow->defaults, "PROXY_", 1, 1, 0, 0, 0);
155 void ProxyThread::to_proxy()
157 // test for new files
158 ArrayList<char *> confirm_paths;
159 confirm_paths.set_array_delete();
161 ArrayList<Indexable*> proxy_assets;
162 // assets which must be created
163 ArrayList<Asset*> needed_assets;
165 ArrayList<Indexable*> orig_assets;
166 // original assets which match the needed_assets
167 ArrayList<Asset*> needed_orig_assets;
168 Asset *proxy_asset = 0;
169 Asset *orig_asset = 0;
171 mwindow->edl->Garbage::add_user();
172 mwindow->save_backup();
173 mwindow->undo->update_undo_before(_("proxy"), this);
175 // revert project to original size from current size
176 if( mwindow->edl->session->proxy_scale > 1 ) {
177 for( orig_asset = mwindow->edl->assets->first;
179 orig_asset = orig_asset->next ) {
180 char new_path[BCTEXTLEN];
181 to_proxy_path(new_path, orig_asset, mwindow->edl->session->proxy_scale);
183 // test if proxy asset was already added to proxy assets
186 for( int i = 0; i < proxy_assets.size(); i++ ) {
187 if( !strcmp(proxy_assets.get(i)->path, new_path) ) {
193 // add pointer to existing EDL asset if it exists
194 // EDL won't delete it unless it's the same pointer.
196 proxy_asset = mwindow->edl->assets->get_asset(new_path);
199 proxy_assets.append(proxy_asset);
200 proxy_asset->Garbage::add_user();
202 orig_assets.append(orig_asset);
203 orig_asset->Garbage::add_user();
209 // convert from the proxy assets to the original assets
210 mwindow->set_proxy(1, &proxy_assets, &orig_assets);
212 // remove the proxy assets
213 mwindow->remove_assets_from_project(0, 0, &proxy_assets, NULL);
216 for( int i = 0; i < proxy_assets.size(); i++ ) {
217 proxy_assets.get(i)->Garbage::remove_user();
219 proxy_assets.remove_all();
221 for( int i = 0; i < orig_assets.size(); i++ ) {
222 orig_assets.get(i)->Garbage::remove_user();
224 orig_assets.remove_all();
227 // convert to new size if not original size
228 if( new_scale != 1 ) {
229 for( orig_asset = mwindow->edl->assets->first;
231 orig_asset = orig_asset->next ) {
232 if( orig_asset->video_data ) {
233 char new_path[BCTEXTLEN];
234 to_proxy_path(new_path, orig_asset, new_scale);
235 // add to proxy_assets & orig_assets if it isn't already there.
238 for( int i = 0; i < proxy_assets.size(); i++ ) {
239 if( !strcmp(proxy_assets.get(i)->path, new_path) ) {
241 proxy_asset = (Asset*)proxy_assets.get(i);
247 proxy_asset = new Asset;
248 // new compression parameters
249 proxy_asset->copy_format(asset, 0);
250 proxy_asset->update_path(new_path);
251 proxy_asset->audio_data = 0;
252 proxy_asset->video_data = 1;
253 proxy_asset->layers = 1;
254 proxy_asset->width = orig_asset->width / new_scale;
255 proxy_asset->height = orig_asset->height / new_scale;
256 proxy_asset->frame_rate = orig_asset->frame_rate;
257 proxy_asset->video_length = orig_asset->video_length;
259 proxy_assets.append(proxy_asset);
260 orig_asset->add_user();
261 orig_assets.append(orig_asset);
264 // test if proxy file exists.
266 FILE *fd = fopen(new_path, "r");
273 // test if proxy file is newer than original.
274 if( fs.get_date(new_path) < fs.get_date(asset->path) ) {
279 // proxy doesn't exist
284 // prompt user to overwrite
286 confirm_paths.append(cstrdup(new_path));
289 needed_assets.append(proxy_asset);
290 proxy_asset->add_user();
291 needed_orig_assets.append(orig_asset);
292 orig_asset->add_user();
294 //printf("ProxyThread::handle_close_event %d %s\n", __LINE__, new_path);
298 // test for existing files
300 if( confirm_paths.size() ) {
301 result = ConfirmSave::test_files(mwindow, &confirm_paths);
302 confirm_paths.remove_all_objects();
309 // create proxy assets which don't already exist
310 if( needed_orig_assets.size() > 0 ) {
311 int64_t total_len = 0;
312 for( int i = 0; i < needed_orig_assets.size(); i++ )
313 total_len += needed_orig_assets.get(i)->video_length;
314 // start progress bar. MWindow is locked inside this
315 progress = mwindow->mainprogress->
316 start_progress(_("Creating proxy files..."), total_len);
319 ProxyFarm engine(mwindow,
322 &needed_orig_assets);
323 engine.process_packages();
325 printf("failed=%d canceled=%d\n", failed, progress->is_cancelled());
328 canceled = progress->is_cancelled();
329 progress->stop_progress();
333 if( failed && !canceled ) {
334 ErrorBox error_box(PROGRAM_NAME ": Error",
335 mwindow->gui->get_abs_cursor_x(1),
336 mwindow->gui->get_abs_cursor_y(1));
337 error_box.create_objects(_("Error making proxy."));
338 error_box.raise_window();
339 error_box.run_window();
344 if( !failed && !canceled ) {
345 mwindow->set_proxy(new_scale, &orig_assets, &proxy_assets);
349 for( int i = 0; i < proxy_assets.size(); i++ ) {
350 proxy_assets.get(i)->Garbage::remove_user();
353 for( int i = 0; i < orig_assets.size(); i++ ) {
354 orig_assets.get(i)->Garbage::remove_user();
357 for( int i = 0; i < needed_assets.size(); i++ ) {
358 needed_assets.get(i)->Garbage::remove_user();
361 for( int i = 0; i < needed_orig_assets.size(); i++ ) {
362 needed_orig_assets.get(i)->Garbage::remove_user();
366 mwindow->undo->update_undo_after(_("proxy"), LOAD_ALL);
368 mwindow->edl->Garbage::remove_user();
370 mwindow->restart_brender();
372 mwindow->gui->lock_window("ProxyThread::to_proxy");
373 mwindow->update_project(LOAD_ALL);
374 mwindow->gui->unlock_window();
378 void ProxyThread::to_proxy_path(char *new_path, Asset *asset, int scale)
380 strcpy(new_path, asset->path);
381 char prxy[BCTEXTLEN];
382 sprintf(prxy, ".proxy%d", scale);
383 // path is already a proxy
384 if( strstr(new_path, prxy) ) return;
385 int len = strlen(new_path);
387 char *ptr = strrchr(new_path, '.');
389 char *cp = new_path + len;
390 int n = strlen(prxy);
392 for( *bp=0; cp>ptr; ) *--bp = *--cp;
393 for( cp= prxy+n; bp>ptr; ) *--bp = *--cp;
394 //printf("ProxyThread::to_proxy_path %d %s %s\n", __LINE__, new_path), asset->path);
397 strcpy(new_path+len, prxy);
400 void ProxyThread::from_proxy_path(char *new_path, Asset *asset, int scale)
402 char prxy[BCTEXTLEN];
403 sprintf(prxy, ".proxy%d", scale);
404 strcpy(new_path, asset->path);
405 char *ptr = strstr(asset->path, prxy);
407 int n = strlen(prxy);
408 for( char *cp=ptr+n; --n>=0; ++ptr,++cp ) *ptr = *cp;
412 void ProxyThread::update_progress()
414 counter_lock->lock();
416 counter_lock->unlock();
417 progress->update(total_rendered);
420 int ProxyThread::is_canceled()
422 return progress->is_cancelled();
427 ProxyWindow::ProxyWindow(MWindow *mwindow, ProxyThread *thread, int x, int y)
428 : BC_Window(_(PROGRAM_NAME ": Proxy settings"), x, y, WIDTH, HEIGHT,
431 this->mwindow = mwindow;
432 this->thread = thread;
436 ProxyWindow::~ProxyWindow()
438 lock_window("ProxyWindow::~ProxyWindow");
444 void ProxyWindow::create_objects()
446 lock_window("ProxyWindow::create_objects");
448 int margin = mwindow->theme->widget_border;
453 mwindow->edl->session->proxy_scale;
456 add_subwindow(text = new BC_Title(x, y,
457 _("What size should the project\n"
458 "be scaled to for editing?")));
459 y += text->get_h() * 2 + margin;
462 add_subwindow(text = new BC_Title(x, y, _("Scale factor:")));
463 x += text->get_w() + margin;
466 thread->calculate_sizes();
467 int popupmenu_w = BC_PopupMenu::calculate_w(get_text_width(MEDIUMFONT, thread->size_text[0]));
468 add_subwindow(scale_factor = new ProxyMenu(mwindow, this, x, y, popupmenu_w, ""));
469 for( int i = 0; i < thread->total_sizes; i++ ) {
470 scale_factor->add_item(new BC_MenuItem(thread->size_text[i]));
472 x += scale_factor->get_w() + margin;
474 ProxyTumbler *tumbler;
475 add_subwindow(tumbler = new ProxyTumbler(mwindow, this, x, y));
478 y += tumbler->get_h() + margin;
480 add_subwindow(reset = new ProxyReset(mwindow, this, x, y));
482 y += reset->get_h() * 2 + margin;
484 add_subwindow(text = new BC_Title(x, y, _("New project dimensions: ")));
485 x += text->get_w() + margin;
486 add_subwindow(new_dimensions = new BC_Title(x, y, ""));
489 y += new_dimensions->get_h() * 2 + margin;
492 format_tools = new FormatTools(mwindow, this, thread->asset);
493 format_tools->create_objects(x, y, 0, 1, 0, 0, 0, 1, 0, 1, // skip the path
498 add_subwindow(new BC_OKButton(this));
499 add_subwindow(new BC_CancelButton(this));
504 void ProxyWindow::update()
506 // preview the new size
507 char string[BCTEXTLEN];
508 //printf("ProxyWindow::update %d %d %d %d %d\n",
509 // __LINE__, mwindow->edl->session->output_w, mwindow->edl->session->output_h,
510 // thread->orig_scale, thread->new_scale);
511 int orig_w = mwindow->edl->session->output_w * thread->orig_scale;
512 int orig_h = mwindow->edl->session->output_h * thread->orig_scale;
513 sprintf(string, "%dx%d",
514 orig_w / thread->new_scale, orig_h / thread->new_scale);
515 new_dimensions->update(string);
516 thread->scale_to_text(string, thread->new_scale);
517 scale_factor->set_text(string);
521 ProxyReset::ProxyReset(MWindow *mwindow, ProxyWindow *pwindow, int x, int y)
522 : BC_GenericButton(x, y, _("Reset"))
524 this->mwindow = mwindow;
525 this->pwindow = pwindow;
528 int ProxyReset::handle_event()
530 pwindow->thread->new_scale = pwindow->thread->orig_scale;
536 ProxyMenu::ProxyMenu(MWindow *mwindow, ProxyWindow *pwindow,
537 int x, int y, int w, const char *text)
538 : BC_PopupMenu(x, y, w, text, 1)
540 this->mwindow = mwindow;
541 this->pwindow = pwindow;
544 int ProxyMenu::handle_event()
546 for( int i = 0; i < pwindow->thread->total_sizes; i++ ) {
547 if( !strcmp(get_text(), pwindow->thread->size_text[i]) ) {
548 pwindow->thread->new_scale = pwindow->thread->size_factors[i];
557 ProxyTumbler::ProxyTumbler(MWindow *mwindow, ProxyWindow *pwindow, int x, int y)
558 : BC_Tumbler(x, y, 0)
560 this->mwindow = mwindow;
561 this->pwindow = pwindow;
564 int ProxyTumbler::handle_up_event()
566 if( pwindow->thread->new_scale > 1 ) {
568 for( i = 0; i < pwindow->thread->total_sizes; i++ ) {
569 if( pwindow->thread->new_scale == pwindow->thread->size_factors[i] ) {
571 pwindow->thread->new_scale = pwindow->thread->size_factors[i];
581 int ProxyTumbler::handle_down_event()
584 for( i = 0; i < pwindow->thread->total_sizes - 1; i++ ) {
585 if( pwindow->thread->new_scale == pwindow->thread->size_factors[i] ) {
587 pwindow->thread->new_scale = pwindow->thread->size_factors[i];
597 ProxyPackage::ProxyPackage()
602 ProxyClient::ProxyClient(MWindow *mwindow, ProxyThread *thread, ProxyFarm *server)
605 this->mwindow = mwindow;
606 this->thread = thread;
609 void ProxyClient::process_package(LoadPackage *ptr)
611 ProxyPackage *package = (ProxyPackage*)ptr;
612 if( thread->failed ) return;
616 EDL *edl = mwindow->edl;
617 Preferences *preferences = mwindow->preferences;
621 src_file.set_processors(processors);
622 src_file.set_preload(edl->session->playback_preload);
623 src_file.set_subtitle(edl->session->decode_subtitles ?
624 edl->session->subtitle_number : -1);
625 src_file.set_interpolate_raw(edl->session->interpolate_raw);
626 src_file.set_white_balance_raw(edl->session->white_balance_raw);
628 //printf("%s %s\n", package->orig_asset->path, package->proxy_asset->path);
631 result = src_file.open_file(preferences, package->orig_asset, 1, 0);
633 // go to the next asset if the reader fails
634 // thread->failed = 1;
638 dst_file.set_processors(processors);
639 result = dst_file.open_file(preferences, package->proxy_asset, 0, 1);
645 dst_file.start_video_thread(1, edl->session->color_model,
646 processors > 1 ? 2 : 1, 0);
648 VFrame src_frame(0, -1,
649 package->orig_asset->width, package->orig_asset->height,
650 edl->session->color_model, -1);
652 OverlayFrame scaler(processors);
654 for( int64_t i = 0; i < package->orig_asset->video_length &&
655 !thread->failed && !thread->is_canceled(); i++ ) {
656 src_file.set_video_position(i, 0);
657 result = src_file.read_frame(&src_frame);
658 //printf("result=%d\n", result);
661 // go to the next asset if the reader fails
662 // thread->failed = 1;
666 // have to write after getting the video buffer or it locks up
667 VFrame ***dst_frames = dst_file.get_video_buffer();
668 VFrame *dst_frame = dst_frames[0][0];
669 scaler.overlay(dst_frame, &src_frame,
670 0, 0, src_frame.get_w(), src_frame.get_h(),
671 0, 0, dst_frame->get_w(), dst_frame->get_h(),
672 1.0, TRANSFER_REPLACE, NEAREST_NEIGHBOR);
673 result = dst_file.write_video_buffer(1);
675 // only fail if the writer fails
680 thread->update_progress();
686 ProxyFarm::ProxyFarm(MWindow *mwindow, ProxyThread *thread,
687 ArrayList<Asset*> *proxy_assets, ArrayList<Asset*> *orig_assets)
688 : LoadServer(MIN(mwindow->preferences->processors, proxy_assets->size()),
689 proxy_assets->size())
691 this->mwindow = mwindow;
692 this->thread = thread;
693 this->proxy_assets = proxy_assets;
694 this->orig_assets = orig_assets;
697 void ProxyFarm::init_packages()
699 for( int i = 0; i < get_total_packages(); i++ ) {
700 ProxyPackage *package = (ProxyPackage*)get_package(i);
701 package->proxy_asset = proxy_assets->get(i);
702 package->orig_asset = orig_assets->get(i);
706 LoadClient* ProxyFarm::new_client()
708 return new ProxyClient(mwindow, thread, this);
711 LoadPackage* ProxyFarm::new_package()
713 return new ProxyPackage;