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];
85 BC_Window* ProxyThread::new_gui()
87 asset->format = FILE_FFMPEG;
88 asset->load_defaults(mwindow->defaults, "PROXY_", 1, 1, 0, 0, 0);
89 mwindow->gui->lock_window("ProxyThread::new_gui");
90 int x = mwindow->gui->get_abs_cursor_x(0) - WIDTH / 2;
91 int y = mwindow->gui->get_abs_cursor_y(0) - HEIGHT / 2;
93 gui = new ProxyWindow(mwindow, this, x, y);
94 gui->create_objects();
95 mwindow->gui->unlock_window();
99 void ProxyThread::scale_to_text(char *string, int scale)
101 strcpy(string, size_text[0]);
102 for( int i = 0; i < total_sizes; i++ ) {
103 if( scale == size_factors[i] ) {
104 strcpy(string, size_text[i]);
111 void ProxyThread::calculate_sizes()
113 for( int i=1; i<total_sizes; ++i ) {
114 delete [] size_text[i];
120 int orig_w = mwindow->edl->session->output_w * orig_scale;
121 int orig_h = mwindow->edl->session->output_h * orig_scale;
124 for( int i=2; i<MAX_SCALE; ++i ) {
125 if( (orig_w % i) != 0 ) continue;
126 if( (orig_h % i) != 0 ) continue;
127 size_factors[total_sizes++] = i;
131 size_factors[total_sizes++] = 2; size_factors[total_sizes++] = 3;
132 size_factors[total_sizes++] = 8; size_factors[total_sizes++] = 12;
133 size_factors[total_sizes++] = 16; size_factors[total_sizes++] = 24;
134 size_factors[total_sizes++] = 32; size_factors[total_sizes++] = 36;
136 for( int i=1; i<total_sizes; ++i ) {
137 char string[BCTEXTLEN];
138 sprintf(string, "1/%d", size_factors[i]);
139 size_text[i] = cstrdup(string);
143 void ProxyThread::handle_close_event(int result)
145 asset->save_defaults(mwindow->defaults, "PROXY_", 1, 1, 0, 0, 0);
151 void ProxyThread::to_proxy()
153 // test for new files
154 ArrayList<char *> confirm_paths;
155 confirm_paths.set_array_delete();
157 ArrayList<Indexable*> proxy_assets;
158 // assets which must be created
159 ArrayList<Asset*> needed_assets;
161 ArrayList<Indexable*> orig_assets;
162 // original assets which match the needed_assets
163 ArrayList<Asset*> needed_orig_assets;
164 Asset *proxy_asset = 0;
165 Asset *orig_asset = 0;
167 mwindow->edl->Garbage::add_user();
168 mwindow->save_backup();
169 mwindow->undo->update_undo_before(_("proxy"), this);
171 EDL *&edl = mwindow->edl;
172 EDLSession *&session = edl->session;
173 Assets *&assets = edl->assets;
175 // revert project to original size from current size
176 // remove all session proxy assets at the at the current proxy_scale
177 if( session->proxy_scale > 1 ) {
178 orig_asset = assets->first;
179 for( ; orig_asset; orig_asset=orig_asset->next ) {
180 char new_path[BCTEXTLEN];
181 to_proxy_path(new_path, orig_asset, session->proxy_scale);
182 proxy_asset = assets->get_asset(new_path);
183 if( !proxy_asset ) continue;
184 // test if proxy asset was already added to proxy_assets
186 for( int i = 0; !got_it && i<proxy_assets.size(); ++i )
187 got_it = !strcmp(proxy_assets[i]->path, new_path);
188 if( got_it ) continue;
189 // add pointer to existing EDL asset if it exists
190 // EDL won't delete it unless it's the same pointer.
191 proxy_assets.append(proxy_asset);
192 proxy_asset->Garbage::add_user();
193 orig_assets.append(orig_asset);
194 orig_asset->Garbage::add_user();
197 // convert from the proxy assets to the original assets
198 mwindow->set_proxy(0, 1, &proxy_assets, &orig_assets);
200 // remove the proxy assets
201 mwindow->remove_assets_from_project(0, 0, &proxy_assets, NULL);
202 for( int i=0; i<proxy_assets.size(); ++i )
203 proxy_assets.get(i)->Garbage::remove_user();
204 proxy_assets.remove_all();
205 for( int i = 0; i < orig_assets.size(); i++ )
206 orig_assets.get(i)->Garbage::remove_user();
207 orig_assets.remove_all();
210 // convert to new size if not original size
211 if( new_scale != 1 ) {
212 orig_asset = assets->first;
213 for( ; orig_asset; orig_asset=orig_asset->next ) {
214 if( !orig_asset->video_data ) continue;
215 char new_path[BCTEXTLEN];
216 to_proxy_path(new_path, orig_asset, new_scale);
217 // add to proxy_assets & orig_assets if it isn't already there.
219 for( int i = 0; !got_it && i<proxy_assets.size(); ++i )
220 got_it = !strcmp(proxy_assets[i]->path, new_path);
222 proxy_asset = new Asset;
223 // new compression parameters
224 proxy_asset->copy_format(asset, 0);
225 proxy_asset->update_path(new_path);
226 proxy_asset->audio_data = 0;
227 proxy_asset->video_data = 1;
228 proxy_asset->layers = 1;
229 proxy_asset->width = orig_asset->width / new_scale;
230 proxy_asset->actual_width = proxy_asset->width;
231 proxy_asset->height = orig_asset->height / new_scale;
232 proxy_asset->actual_height = proxy_asset->height;
233 proxy_asset->frame_rate = orig_asset->frame_rate;
234 proxy_asset->video_length = orig_asset->video_length;
235 proxy_assets.append(proxy_asset);
236 orig_asset->add_user();
237 orig_assets.append(orig_asset);
240 // test if proxy file exists.
242 FILE *fd = fopen(new_path, "r");
247 // got it if proxy file is newer than original.
248 got_it = fs.get_date(new_path) > fs.get_date(asset->path);
254 if( exists ) // prompt user to overwrite
255 confirm_paths.append(cstrdup(new_path));
257 needed_assets.append(proxy_asset);
258 proxy_asset->add_user();
259 needed_orig_assets.append(orig_asset);
260 orig_asset->add_user();
261 //printf("ProxyThread::handle_close_event %d %s\n", __LINE__, new_path);
265 // test for existing files
267 if( confirm_paths.size() ) {
268 result = ConfirmSave::test_files(mwindow, &confirm_paths);
269 confirm_paths.remove_all_objects();
276 // create proxy assets which don't already exist
277 if( needed_orig_assets.size() > 0 ) {
278 int64_t total_len = 0;
279 for( int i = 0; i < needed_orig_assets.size(); i++ )
280 total_len += needed_orig_assets.get(i)->video_length;
281 // start progress bar. MWindow is locked inside this
282 progress = mwindow->mainprogress->
283 start_progress(_("Creating proxy files..."), total_len);
286 ProxyFarm engine(mwindow, this,
287 &needed_assets, &needed_orig_assets);
288 engine.process_packages();
289 printf("failed=%d canceled=%d\n", failed, progress->is_cancelled());
292 canceled = progress->is_cancelled();
293 progress->stop_progress();
294 delete progress; progress = 0;
296 if( failed && !canceled ) {
298 mwindow->gui->get_abs_cursor_xy(cx, cy, 1);
299 ErrorBox error_box(PROGRAM_NAME ": Error", cx, cy);
300 error_box.create_objects(_("Error making proxy."));
301 error_box.raise_window();
302 error_box.run_window();
307 if( !failed && !canceled ) {
308 mwindow->set_proxy(use_scaler, new_scale, &orig_assets, &proxy_assets);
312 for( int i = 0; i < proxy_assets.size(); i++ )
313 proxy_assets.get(i)->Garbage::remove_user();
314 for( int i = 0; i < orig_assets.size(); i++ )
315 orig_assets.get(i)->Garbage::remove_user();
316 for( int i = 0; i < needed_assets.size(); i++ )
317 needed_assets.get(i)->Garbage::remove_user();
318 for( int i = 0; i < needed_orig_assets.size(); i++ )
319 needed_orig_assets.get(i)->Garbage::remove_user();
322 mwindow->undo->update_undo_after(_("proxy"), LOAD_ALL);
323 mwindow->edl->Garbage::remove_user();
324 mwindow->restart_brender();
326 mwindow->gui->lock_window("ProxyThread::to_proxy");
327 mwindow->update_project(LOAD_ALL);
328 mwindow->gui->unlock_window();
332 void ProxyThread::to_proxy_path(char *new_path, Asset *asset, int scale)
334 strcpy(new_path, asset->path);
335 char prxy[BCTEXTLEN];
336 sprintf(prxy, ".proxy%d", scale);
337 // path is already a proxy
338 if( strstr(new_path, prxy) ) return;
339 int len = strlen(new_path);
341 char *ptr = strrchr(new_path, '.');
343 char *cp = new_path + len;
344 int n = strlen(prxy);
346 for( *bp=0; cp>ptr; ) *--bp = *--cp;
347 for( cp= prxy+n; bp>ptr; ) *--bp = *--cp;
348 //printf("ProxyThread::to_proxy_path %d %s %s\n", __LINE__, new_path), asset->path);
351 strcpy(new_path+len, prxy);
354 void ProxyThread::from_proxy_path(char *new_path, Asset *asset, int scale)
356 char prxy[BCTEXTLEN];
357 sprintf(prxy, ".proxy%d", scale);
358 strcpy(new_path, asset->path);
359 char *ptr = strstr(asset->path, prxy);
361 int n = strlen(prxy);
362 for( char *cp=ptr+n; --n>=0; ++ptr,++cp ) *ptr = *cp;
366 void ProxyThread::update_progress()
368 counter_lock->lock();
370 counter_lock->unlock();
371 progress->update(total_rendered);
374 int ProxyThread::is_canceled()
376 return progress->is_cancelled();
381 ProxyWindow::ProxyWindow(MWindow *mwindow, ProxyThread *thread, int x, int y)
382 : BC_Window(_(PROGRAM_NAME ": Proxy settings"), x, y, WIDTH, HEIGHT,
385 this->mwindow = mwindow;
386 this->thread = thread;
390 ProxyWindow::~ProxyWindow()
392 lock_window("ProxyWindow::~ProxyWindow");
398 void ProxyWindow::create_objects()
400 lock_window("ProxyWindow::create_objects");
402 int margin = mwindow->theme->widget_border;
405 thread->use_scaler = mwindow->edl->session->proxy_use_scaler;
406 thread->orig_scale = mwindow->edl->session->proxy_scale;
407 thread->new_scale = thread->orig_scale;
410 add_subwindow(text = new BC_Title(x, y,
411 _("What size should the project\n"
412 "be scaled to for editing?")));
413 y += text->get_h() * 2 + margin;
415 add_subwindow(text = new BC_Title(x, y, _("Scale factor:")));
416 x += text->get_w() + margin;
418 thread->size_text[0] = cstrdup(_("Original size"));
419 thread->size_factors[0] = 1;
420 thread->total_sizes = 1;
421 int popupmenu_w = BC_PopupMenu::calculate_w(get_text_width(MEDIUMFONT, thread->size_text[0]));
422 add_subwindow(scale_factor = new ProxyMenu(mwindow, this, x, y, popupmenu_w, ""));
423 scale_factor->update_sizes();
424 x += scale_factor->get_w() + margin;
426 ProxyTumbler *tumbler;
427 add_subwindow(tumbler = new ProxyTumbler(mwindow, this, x, y));
428 y += tumbler->get_h() + margin;
430 add_subwindow(use_scaler = new ProxyUseScaler(mwindow, this, x, y));
431 y += use_scaler->get_h() + margin;
435 add_subwindow(text = new BC_Title(x, y, _("New media dimensions: ")));
436 x += text->get_w() + margin;
437 add_subwindow(new_dimensions = new BC_Title(x, y, ""));
440 y += new_dimensions->get_h() * 2 + margin;
443 format_tools = new ProxyFormatTools(mwindow, this, thread->asset);
444 format_tools->create_objects(x, y, 0, 1, 0, 0, 0, 1, 0, 1, // skip the path
449 add_subwindow(new BC_OKButton(this));
450 add_subwindow(new BC_CancelButton(this));
455 ProxyFormatTools::ProxyFormatTools(MWindow *mwindow, ProxyWindow *pwindow, Asset *asset)
456 : FormatTools(mwindow, pwindow, asset)
458 this->pwindow = pwindow;
461 void ProxyFormatTools::update_format()
463 FormatTools::update_format();
464 pwindow->use_scaler->update();
467 void ProxyWindow::update()
469 // preview the new size
470 char string[BCTEXTLEN];
471 //printf("ProxyWindow::update %d %d %d %d %d\n",
472 // __LINE__, mwindow->edl->session->output_w, mwindow->edl->session->output_h,
473 // thread->orig_scale, thread->new_scale);
474 int orig_w = mwindow->edl->session->output_w * thread->orig_scale;
475 int orig_h = mwindow->edl->session->output_h * thread->orig_scale;
476 sprintf(string, "%dx%d",
477 orig_w / thread->new_scale, orig_h / thread->new_scale);
478 new_dimensions->update(string);
479 thread->scale_to_text(string, thread->new_scale);
480 scale_factor->set_text(string);
481 use_scaler->update();
485 ProxyUseScaler::ProxyUseScaler(MWindow *mwindow, ProxyWindow *pwindow, int x, int y)
486 : BC_CheckBox(x, y, pwindow->thread->use_scaler, _("Use scaler (FFMPEG only)"))
488 this->mwindow = mwindow;
489 this->pwindow = pwindow;
492 void ProxyUseScaler::update()
494 ProxyThread *thread = pwindow->thread;
495 if( thread->asset->format != FILE_FFMPEG ) thread->use_scaler = 0;
496 BC_CheckBox::update(thread->use_scaler);
497 int scaler_avail = thread->asset->format == FILE_FFMPEG ? 1 : 0;
498 if( !scaler_avail && enabled ) disable();
499 if( scaler_avail && !enabled ) enable();
502 int ProxyUseScaler::handle_event()
504 pwindow->thread->new_scale = 1;
505 pwindow->thread->use_scaler = get_value();
506 pwindow->scale_factor->update_sizes();
512 ProxyMenu::ProxyMenu(MWindow *mwindow, ProxyWindow *pwindow,
513 int x, int y, int w, const char *text)
514 : BC_PopupMenu(x, y, w, text, 1)
516 this->mwindow = mwindow;
517 this->pwindow = pwindow;
520 void ProxyMenu::update_sizes()
522 while( total_items() > 0 ) del_item(0);
523 ProxyThread *thread = pwindow->thread;
524 thread->calculate_sizes();
525 for( int i=0; i < thread->total_sizes; i++ )
526 add_item(new BC_MenuItem(thread->size_text[i]));
529 int ProxyMenu::handle_event()
531 for( int i = 0; i < pwindow->thread->total_sizes; i++ ) {
532 if( !strcmp(get_text(), pwindow->thread->size_text[i]) ) {
533 pwindow->thread->new_scale = pwindow->thread->size_factors[i];
534 if( pwindow->thread->new_scale == 1 )
535 pwindow->thread->use_scaler = 0;
544 ProxyTumbler::ProxyTumbler(MWindow *mwindow, ProxyWindow *pwindow, int x, int y)
545 : BC_Tumbler(x, y, 0)
547 this->mwindow = mwindow;
548 this->pwindow = pwindow;
551 int ProxyTumbler::handle_up_event()
553 if( pwindow->thread->new_scale > 1 ) {
555 for( i = 0; i < pwindow->thread->total_sizes; i++ ) {
556 if( pwindow->thread->new_scale == pwindow->thread->size_factors[i] ) {
558 pwindow->thread->new_scale = pwindow->thread->size_factors[i];
568 int ProxyTumbler::handle_down_event()
571 for( i = 0; i < pwindow->thread->total_sizes - 1; i++ ) {
572 if( pwindow->thread->new_scale == pwindow->thread->size_factors[i] ) {
574 pwindow->thread->new_scale = pwindow->thread->size_factors[i];
584 ProxyPackage::ProxyPackage()
589 ProxyClient::ProxyClient(MWindow *mwindow, ProxyThread *thread, ProxyFarm *server)
592 this->mwindow = mwindow;
593 this->thread = thread;
596 void ProxyClient::process_package(LoadPackage *ptr)
598 ProxyPackage *package = (ProxyPackage*)ptr;
599 if( thread->failed ) return;
603 EDL *edl = mwindow->edl;
604 Preferences *preferences = mwindow->preferences;
608 src_file.set_processors(processors);
609 src_file.set_preload(edl->session->playback_preload);
610 src_file.set_subtitle(edl->session->decode_subtitles ?
611 edl->session->subtitle_number : -1);
612 src_file.set_interpolate_raw(edl->session->interpolate_raw);
613 src_file.set_white_balance_raw(edl->session->white_balance_raw);
615 //printf("%s %s\n", package->orig_asset->path, package->proxy_asset->path);
618 result = src_file.open_file(preferences, package->orig_asset, 1, 0);
620 // go to the next asset if the reader fails
621 // thread->failed = 1;
625 dst_file.set_processors(processors);
626 result = dst_file.open_file(preferences, package->proxy_asset, 0, 1);
632 dst_file.start_video_thread(1, edl->session->color_model,
633 processors > 1 ? 2 : 1, 0);
635 VFrame src_frame(0, -1,
636 package->orig_asset->width, package->orig_asset->height,
637 edl->session->color_model, -1);
639 OverlayFrame scaler(processors);
641 for( int64_t i = 0; i < package->orig_asset->video_length &&
642 !thread->failed && !thread->is_canceled(); i++ ) {
643 src_file.set_video_position(i, 0);
644 result = src_file.read_frame(&src_frame);
645 //printf("result=%d\n", result);
648 // go to the next asset if the reader fails
649 // thread->failed = 1;
653 // have to write after getting the video buffer or it locks up
654 VFrame ***dst_frames = dst_file.get_video_buffer();
655 VFrame *dst_frame = dst_frames[0][0];
656 scaler.overlay(dst_frame, &src_frame,
657 0, 0, src_frame.get_w(), src_frame.get_h(),
658 0, 0, dst_frame->get_w(), dst_frame->get_h(),
659 1.0, TRANSFER_REPLACE, NEAREST_NEIGHBOR);
660 result = dst_file.write_video_buffer(1);
662 // only fail if the writer fails
667 thread->update_progress();
673 ProxyFarm::ProxyFarm(MWindow *mwindow, ProxyThread *thread,
674 ArrayList<Asset*> *proxy_assets, ArrayList<Asset*> *orig_assets)
675 : LoadServer(MIN(mwindow->preferences->processors, proxy_assets->size()),
676 proxy_assets->size())
678 this->mwindow = mwindow;
679 this->thread = thread;
680 this->proxy_assets = proxy_assets;
681 this->orig_assets = orig_assets;
684 void ProxyFarm::init_packages()
686 for( int i = 0; i < get_total_packages(); i++ ) {
687 ProxyPackage *package = (ProxyPackage*)get_package(i);
688 package->proxy_asset = proxy_assets->get(i);
689 package->orig_asset = orig_assets->get(i);
693 LoadClient* ProxyFarm::new_client()
695 return new ProxyClient(mwindow, thread, this);
698 LoadPackage* ProxyFarm::new_package()
700 return new ProxyPackage;