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 ) 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)
102 strcpy(string, size_text[0]);
103 for( int i = 0; i < total_sizes; i++ ) {
104 if( scale == size_factors[i] ) {
105 strcpy(string, size_text[i]);
112 void ProxyThread::calculate_sizes()
114 for( int i=1; i<total_sizes; ++i ) {
115 delete [] size_text[i];
121 int orig_w = mwindow->edl->session->output_w * orig_scale;
122 int orig_h = mwindow->edl->session->output_h * orig_scale;
125 // w,h should stay even for yuv
126 int ow = orig_w, oh = orig_h;
127 if( BC_CModels::is_yuv(mwindow->edl->session->color_model) ) {
130 for( int i=2; i<MAX_SCALE; ++i ) {
131 if( (ow % i) != 0 ) continue;
132 if( (oh % i) != 0 ) continue;
133 size_factors[total_sizes++] = i;
137 size_factors[total_sizes++] = 2; size_factors[total_sizes++] = 3;
138 size_factors[total_sizes++] = 8; size_factors[total_sizes++] = 12;
139 size_factors[total_sizes++] = 16; size_factors[total_sizes++] = 24;
140 size_factors[total_sizes++] = 32;
142 for( int i=1; i<total_sizes; ++i ) {
143 char string[BCTEXTLEN];
144 sprintf(string, "1/%d", size_factors[i]);
145 size_text[i] = cstrdup(string);
149 void ProxyThread::handle_close_event(int result)
151 asset->save_defaults(mwindow->defaults, "PROXY_", 1, 1, 0, 0, 0);
157 void ProxyThread::to_proxy()
159 // test for new files
160 ArrayList<char *> confirm_paths;
161 confirm_paths.set_array_delete();
163 ArrayList<Indexable*> proxy_assets;
164 // assets which must be created
165 ArrayList<Asset*> needed_assets;
167 ArrayList<Indexable*> orig_assets;
168 // original assets which match the needed_assets
169 ArrayList<Asset*> needed_orig_assets;
170 Asset *proxy_asset = 0;
171 Asset *orig_asset = 0;
173 mwindow->edl->Garbage::add_user();
174 mwindow->save_backup();
175 mwindow->undo->update_undo_before(_("proxy"), this);
177 EDL *&edl = mwindow->edl;
178 EDLSession *&session = edl->session;
179 Assets *&assets = edl->assets;
181 // revert project to original size from current size
182 // remove all session proxy assets at the at the current proxy_scale
183 if( session->proxy_scale > 1 ) {
184 orig_asset = assets->first;
185 for( ; orig_asset; orig_asset=orig_asset->next ) {
186 char new_path[BCTEXTLEN];
187 to_proxy_path(new_path, orig_asset, session->proxy_scale);
188 proxy_asset = assets->get_asset(new_path);
189 if( !proxy_asset ) continue;
190 // test if proxy asset was already added to proxy_assets
192 for( int i = 0; !got_it && i<proxy_assets.size(); ++i )
193 got_it = !strcmp(proxy_assets[i]->path, new_path);
194 if( got_it ) 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->Garbage::add_user();
199 orig_assets.append(orig_asset);
200 orig_asset->Garbage::add_user();
203 // convert from the proxy assets to the original assets
204 mwindow->set_proxy(0, 1, &proxy_assets, &orig_assets);
206 // remove the proxy assets
207 mwindow->remove_assets_from_project(0, 0, &proxy_assets, NULL);
208 for( int i=0; i<proxy_assets.size(); ++i )
209 proxy_assets.get(i)->Garbage::remove_user();
210 proxy_assets.remove_all();
211 for( int i = 0; i < orig_assets.size(); i++ )
212 orig_assets.get(i)->Garbage::remove_user();
213 orig_assets.remove_all();
216 // convert to new size if not original size
217 if( new_scale != 1 ) {
218 orig_asset = assets->first;
219 for( ; orig_asset; orig_asset=orig_asset->next ) {
220 if( !orig_asset->video_data ) continue;
221 char new_path[BCTEXTLEN];
222 to_proxy_path(new_path, orig_asset, new_scale);
223 // add to proxy_assets & orig_assets if it isn't already there.
225 for( int i = 0; !got_it && i<proxy_assets.size(); ++i )
226 got_it = !strcmp(proxy_assets[i]->path, new_path);
228 proxy_asset = new Asset;
229 // new compression parameters
230 proxy_asset->copy_format(asset, 0);
231 proxy_asset->update_path(new_path);
232 proxy_asset->audio_data = 0;
233 proxy_asset->video_data = 1;
234 proxy_asset->layers = 1;
235 proxy_asset->width = orig_asset->width / new_scale;
236 if( proxy_asset->width & 1 ) ++proxy_asset->width;
237 proxy_asset->actual_width = proxy_asset->width;
238 proxy_asset->height = orig_asset->height / new_scale;
239 if( proxy_asset->height & 1 ) ++proxy_asset->height;
240 proxy_asset->actual_height = proxy_asset->height;
241 proxy_asset->frame_rate = orig_asset->frame_rate;
242 proxy_asset->video_length = orig_asset->video_length;
243 proxy_assets.append(proxy_asset);
244 orig_asset->add_user();
245 orig_assets.append(orig_asset);
248 // test if proxy file exists.
250 FILE *fd = fopen(new_path, "r");
255 // got it if proxy file is newer than original.
256 got_it = fs.get_date(new_path) > fs.get_date(asset->path);
262 if( exists ) // prompt user to overwrite
263 confirm_paths.append(cstrdup(new_path));
265 needed_assets.append(proxy_asset);
266 proxy_asset->add_user();
267 needed_orig_assets.append(orig_asset);
268 orig_asset->add_user();
269 //printf("ProxyThread::handle_close_event %d %s\n", __LINE__, new_path);
273 // test for existing files
275 if( confirm_paths.size() ) {
276 result = ConfirmSave::test_files(mwindow, &confirm_paths);
277 confirm_paths.remove_all_objects();
284 // create proxy assets which don't already exist
285 if( needed_orig_assets.size() > 0 ) {
286 int64_t total_len = 0;
287 for( int i = 0; i < needed_orig_assets.size(); i++ )
288 total_len += needed_orig_assets.get(i)->video_length;
289 // start progress bar. MWindow is locked inside this
290 progress = mwindow->mainprogress->
291 start_progress(_("Creating proxy files..."), total_len);
294 ProxyFarm engine(mwindow, this,
295 &needed_assets, &needed_orig_assets);
296 engine.process_packages();
297 printf("failed=%d canceled=%d\n", failed, progress->is_cancelled());
300 canceled = progress->is_cancelled();
301 progress->stop_progress();
302 delete progress; progress = 0;
304 if( failed && !canceled ) {
306 mwindow->gui->get_abs_cursor_xy(cx, cy, 1);
307 ErrorBox error_box(PROGRAM_NAME ": Error", cx, cy);
308 error_box.create_objects(_("Error making proxy."));
309 error_box.raise_window();
310 error_box.run_window();
315 if( !failed && !canceled ) {
316 mwindow->set_proxy(use_scaler, new_scale, &orig_assets, &proxy_assets);
320 for( int i = 0; i < proxy_assets.size(); i++ )
321 proxy_assets.get(i)->Garbage::remove_user();
322 for( int i = 0; i < orig_assets.size(); i++ )
323 orig_assets.get(i)->Garbage::remove_user();
324 for( int i = 0; i < needed_assets.size(); i++ )
325 needed_assets.get(i)->Garbage::remove_user();
326 for( int i = 0; i < needed_orig_assets.size(); i++ )
327 needed_orig_assets.get(i)->Garbage::remove_user();
330 mwindow->undo->update_undo_after(_("proxy"), LOAD_ALL);
331 mwindow->edl->Garbage::remove_user();
332 mwindow->restart_brender();
334 mwindow->gui->lock_window("ProxyThread::to_proxy");
335 mwindow->update_project(LOAD_ALL);
336 mwindow->gui->unlock_window();
340 void ProxyThread::to_proxy_path(char *new_path, Asset *asset, int scale)
342 strcpy(new_path, asset->path);
343 char prxy[BCTEXTLEN];
344 sprintf(prxy, ".proxy%d", scale);
345 // path is already a proxy
346 if( strstr(new_path, prxy) ) return;
347 int len = strlen(new_path);
349 char *ptr = strrchr(new_path, '.');
351 char *cp = new_path + len;
352 int n = strlen(prxy);
354 for( *bp=0; cp>ptr; ) *--bp = *--cp;
355 for( cp= prxy+n; bp>ptr; ) *--bp = *--cp;
356 //printf("ProxyThread::to_proxy_path %d %s %s\n", __LINE__, new_path), asset->path);
359 strcpy(new_path+len, prxy);
362 void ProxyThread::from_proxy_path(char *new_path, Asset *asset, int scale)
364 char prxy[BCTEXTLEN];
365 sprintf(prxy, ".proxy%d", scale);
366 strcpy(new_path, asset->path);
367 char *ptr = strstr(asset->path, prxy);
369 int n = strlen(prxy);
370 for( char *cp=ptr+n; --n>=0; ++ptr,++cp ) *ptr = *cp;
374 void ProxyThread::update_progress()
376 counter_lock->lock();
378 counter_lock->unlock();
379 progress->update(total_rendered);
382 int ProxyThread::is_canceled()
384 return progress->is_cancelled();
389 ProxyWindow::ProxyWindow(MWindow *mwindow, ProxyThread *thread, int x, int y)
390 : BC_Window(_(PROGRAM_NAME ": Proxy settings"), x, y, WIDTH, HEIGHT,
393 this->mwindow = mwindow;
394 this->thread = thread;
398 ProxyWindow::~ProxyWindow()
400 lock_window("ProxyWindow::~ProxyWindow");
406 void ProxyWindow::create_objects()
408 lock_window("ProxyWindow::create_objects");
410 int margin = mwindow->theme->widget_border;
413 thread->use_scaler = mwindow->edl->session->proxy_use_scaler;
414 thread->orig_scale = mwindow->edl->session->proxy_scale;
415 thread->new_scale = thread->orig_scale;
418 add_subwindow(text = new BC_Title(x, y,
419 _("What size should the project\n"
420 "be scaled to for editing?")));
421 y += text->get_h() * 2 + margin;
423 add_subwindow(text = new BC_Title(x, y, _("Scale factor:")));
424 x += text->get_w() + margin;
426 thread->size_text[0] = cstrdup(_("Original size"));
427 thread->size_factors[0] = 1;
428 thread->total_sizes = 1;
429 int popupmenu_w = BC_PopupMenu::calculate_w(get_text_width(MEDIUMFONT, thread->size_text[0]));
430 add_subwindow(scale_factor = new ProxyMenu(mwindow, this, x, y, popupmenu_w, ""));
431 scale_factor->update_sizes();
432 x += scale_factor->get_w() + margin;
434 ProxyTumbler *tumbler;
435 add_subwindow(tumbler = new ProxyTumbler(mwindow, this, x, y));
436 y += tumbler->get_h() + margin;
438 add_subwindow(use_scaler = new ProxyUseScaler(mwindow, this, x, y));
439 y += use_scaler->get_h() + margin;
443 add_subwindow(text = new BC_Title(x, y, _("New media dimensions: ")));
444 x += text->get_w() + margin;
445 add_subwindow(new_dimensions = new BC_Title(x, y, ""));
448 y += new_dimensions->get_h() * 2 + margin;
451 format_tools = new ProxyFormatTools(mwindow, this, thread->asset);
452 format_tools->create_objects(x, y, 0, 1, 0, 0, 0, 1, 0, 1, // skip the path
457 add_subwindow(new BC_OKButton(this));
458 add_subwindow(new BC_CancelButton(this));
463 ProxyFormatTools::ProxyFormatTools(MWindow *mwindow, ProxyWindow *pwindow, Asset *asset)
464 : FormatTools(mwindow, pwindow, asset)
466 this->pwindow = pwindow;
469 void ProxyFormatTools::update_format()
471 FormatTools::update_format();
472 pwindow->use_scaler->update();
475 void ProxyWindow::update()
477 // preview the new size
478 char string[BCTEXTLEN];
479 //printf("ProxyWindow::update %d %d %d %d %d\n",
480 // __LINE__, mwindow->edl->session->output_w, mwindow->edl->session->output_h,
481 // thread->orig_scale, thread->new_scale);
482 int orig_w = mwindow->edl->session->output_w * thread->orig_scale;
483 int orig_h = mwindow->edl->session->output_h * thread->orig_scale;
484 int new_w = orig_w / thread->new_scale;
485 if( new_w & 1 ) ++new_w;
486 int new_h = orig_h / thread->new_scale;
487 if( new_h & 1 ) ++new_h;
488 sprintf(string, "%dx%d", new_w, new_h);
489 new_dimensions->update(string);
490 thread->scale_to_text(string, thread->new_scale);
491 scale_factor->set_text(string);
492 use_scaler->update();
496 ProxyUseScaler::ProxyUseScaler(MWindow *mwindow, ProxyWindow *pwindow, int x, int y)
497 : BC_CheckBox(x, y, pwindow->thread->use_scaler, _("Use scaler (FFMPEG only)"))
499 this->mwindow = mwindow;
500 this->pwindow = pwindow;
503 void ProxyUseScaler::update()
505 ProxyThread *thread = pwindow->thread;
506 if( thread->asset->format != FILE_FFMPEG ) thread->use_scaler = 0;
507 BC_CheckBox::update(thread->use_scaler);
508 int scaler_avail = thread->asset->format == FILE_FFMPEG ? 1 : 0;
509 if( !scaler_avail && enabled ) disable();
510 if( scaler_avail && !enabled ) enable();
513 int ProxyUseScaler::handle_event()
515 pwindow->thread->new_scale = 1;
516 pwindow->thread->use_scaler = get_value();
517 pwindow->scale_factor->update_sizes();
523 ProxyMenu::ProxyMenu(MWindow *mwindow, ProxyWindow *pwindow,
524 int x, int y, int w, const char *text)
525 : BC_PopupMenu(x, y, w, text, 1)
527 this->mwindow = mwindow;
528 this->pwindow = pwindow;
531 void ProxyMenu::update_sizes()
533 while( total_items() > 0 ) del_item(0);
534 ProxyThread *thread = pwindow->thread;
535 thread->calculate_sizes();
536 for( int i=0; i < thread->total_sizes; i++ )
537 add_item(new BC_MenuItem(thread->size_text[i]));
540 int ProxyMenu::handle_event()
542 for( int i = 0; i < pwindow->thread->total_sizes; i++ ) {
543 if( !strcmp(get_text(), pwindow->thread->size_text[i]) ) {
544 pwindow->thread->new_scale = pwindow->thread->size_factors[i];
545 if( pwindow->thread->new_scale == 1 )
546 pwindow->thread->use_scaler = 0;
555 ProxyTumbler::ProxyTumbler(MWindow *mwindow, ProxyWindow *pwindow, int x, int y)
556 : BC_Tumbler(x, y, 0)
558 this->mwindow = mwindow;
559 this->pwindow = pwindow;
562 int ProxyTumbler::handle_up_event()
564 if( pwindow->thread->new_scale > 1 ) {
566 for( i = 0; i < pwindow->thread->total_sizes; i++ ) {
567 if( pwindow->thread->new_scale == pwindow->thread->size_factors[i] ) {
569 pwindow->thread->new_scale = pwindow->thread->size_factors[i];
579 int ProxyTumbler::handle_down_event()
582 for( i = 0; i < pwindow->thread->total_sizes - 1; i++ ) {
583 if( pwindow->thread->new_scale == pwindow->thread->size_factors[i] ) {
585 pwindow->thread->new_scale = pwindow->thread->size_factors[i];
595 ProxyPackage::ProxyPackage()
600 ProxyClient::ProxyClient(MWindow *mwindow, ProxyThread *thread, ProxyFarm *server)
603 this->mwindow = mwindow;
604 this->thread = thread;
607 void ProxyClient::process_package(LoadPackage *ptr)
609 ProxyPackage *package = (ProxyPackage*)ptr;
610 if( thread->failed ) return;
614 EDL *edl = mwindow->edl;
615 Preferences *preferences = mwindow->preferences;
619 src_file.set_processors(processors);
620 src_file.set_preload(edl->session->playback_preload);
621 src_file.set_subtitle(edl->session->decode_subtitles ?
622 edl->session->subtitle_number : -1);
623 src_file.set_interpolate_raw(edl->session->interpolate_raw);
624 src_file.set_white_balance_raw(edl->session->white_balance_raw);
626 //printf("%s %s\n", package->orig_asset->path, package->proxy_asset->path);
629 result = src_file.open_file(preferences, package->orig_asset, 1, 0);
631 // go to the next asset if the reader fails
632 // thread->failed = 1;
636 dst_file.set_processors(processors);
637 result = dst_file.open_file(preferences, package->proxy_asset, 0, 1);
643 dst_file.start_video_thread(1, edl->session->color_model,
644 processors > 1 ? 2 : 1, 0);
646 VFrame src_frame(0, -1,
647 package->orig_asset->width, package->orig_asset->height,
648 edl->session->color_model, -1);
650 OverlayFrame scaler(processors);
652 for( int64_t i = 0; i < package->orig_asset->video_length &&
653 !thread->failed && !thread->is_canceled(); i++ ) {
654 src_file.set_video_position(i, 0);
655 result = src_file.read_frame(&src_frame);
656 //printf("result=%d\n", result);
659 // go to the next asset if the reader fails
660 // thread->failed = 1;
664 // have to write after getting the video buffer or it locks up
665 VFrame ***dst_frames = dst_file.get_video_buffer();
666 VFrame *dst_frame = dst_frames[0][0];
667 scaler.overlay(dst_frame, &src_frame,
668 0, 0, src_frame.get_w(), src_frame.get_h(),
669 0, 0, dst_frame->get_w(), dst_frame->get_h(),
670 1.0, TRANSFER_REPLACE, NEAREST_NEIGHBOR);
671 result = dst_file.write_video_buffer(1);
673 // only fail if the writer fails
678 thread->update_progress();
684 ProxyFarm::ProxyFarm(MWindow *mwindow, ProxyThread *thread,
685 ArrayList<Asset*> *proxy_assets, ArrayList<Asset*> *orig_assets)
686 : LoadServer(MIN(mwindow->preferences->processors, proxy_assets->size()),
687 proxy_assets->size())
689 this->mwindow = mwindow;
690 this->thread = thread;
691 this->proxy_assets = proxy_assets;
692 this->orig_assets = orig_assets;
695 void ProxyFarm::init_packages()
697 for( int i = 0; i < get_total_packages(); i++ ) {
698 ProxyPackage *package = (ProxyPackage*)get_package(i);
699 package->proxy_asset = proxy_assets->get(i);
700 package->orig_asset = orig_assets->get(i);
704 LoadClient* ProxyFarm::new_client()
706 return new ProxyClient(mwindow, thread, this);
709 LoadPackage* ProxyFarm::new_package()
711 return new ProxyPackage;