4 #include "batchrender.h"
5 #include "bcprogressbox.h"
8 #include "compresspopup.h"
10 #include "confirmsave.h"
11 #include "cwindowgui.h"
16 #include "edlsession.h"
19 #include "filesystem.h"
21 #include "formatcheck.h"
22 #include "formatpopup.h"
23 #include "formattools.h"
27 #include "localsession.h"
28 #include "mainprogress.h"
29 #include "mainsession.h"
33 #include "mwindowgui.h"
35 #include "packagedispatcher.h"
36 #include "packagerenderer.h"
38 #include "playabletracks.h"
39 #include "preferences.h"
40 #include "quicktime.h"
41 #include "renderfarm.h"
43 #include "statusbar.h"
47 #include "transportque.h"
50 #include "videoconfig.h"
58 RenderItem::RenderItem(MWindow *mwindow)
59 : BC_MenuItem(_("Render..."), "Shift+R", 'R')
61 this->mwindow = mwindow;
65 int RenderItem::handle_event()
67 mwindow->render->start_interactive();
80 RenderProgress::RenderProgress(MWindow *mwindow, Render *render)
83 this->mwindow = mwindow;
84 this->render = render;
86 Thread::set_synchronous(1);
89 RenderProgress::~RenderProgress()
96 void RenderProgress::run()
98 Thread::disable_cancel();
101 if(render->total_rendered != last_value)
103 render->progress->update(render->total_rendered);
104 last_value = render->total_rendered;
107 Thread::enable_cancel();
109 Thread::disable_cancel();
122 MainPackageRenderer::MainPackageRenderer(Render *render)
125 this->render = render;
130 MainPackageRenderer::~MainPackageRenderer()
135 int MainPackageRenderer::get_master()
140 int MainPackageRenderer::get_result()
142 return render->result;
145 void MainPackageRenderer::set_result(int value)
148 render->result = value;
151 void MainPackageRenderer::set_progress(int64_t value)
153 render->counter_lock->lock("MainPackageRenderer::set_progress");
154 render->total_rendered += value;
156 // If non interactive, print progress out
157 if(!render->progress)
159 int64_t current_eta = render->progress_timer->get_scaled_difference(1000);
160 if(current_eta - render->last_eta > 1000)
165 if(render->total_rendered)
169 render->progress_max /
170 render->total_rendered -
175 char string[BCTEXTLEN];
176 Units::totext(string,
180 printf("\r%d%% ETA: %s ", (int)(100 *
181 (float)render->total_rendered /
182 render->progress_max),
185 render->last_eta = current_eta;
189 render->counter_lock->unlock();
192 int MainPackageRenderer::progress_cancelled()
194 return (render->progress && render->progress->is_cancelled()) ||
195 render->batch_cancelled;
209 Render::Render(MWindow *mwindow)
212 this->mwindow = mwindow;
213 if(mwindow) plugindb = mwindow->plugindb;
218 package_lock = new Mutex("Render::package_lock");
219 counter_lock = new Mutex("Render::counter_lock");
220 completion = new Condition(0, "Render::completion");
221 progress_timer = new Timer;
229 if(preferences) delete preferences;
230 delete progress_timer;
233 void Render::start_interactive()
235 if(!Thread::running())
237 mode = Render::INTERACTIVE;
245 // raise the window if rendering hasn't started yet
246 if (render_window && ! in_progress) {
247 render_window->raise_window();
250 ErrorBox error_box(PROGRAM_NAME ": Error",
251 mwindow->gui->get_abs_cursor_x(1),
252 mwindow->gui->get_abs_cursor_y(1));
253 error_box.create_objects("Already rendering");
254 error_box.raise_window();
255 error_box.run_window();
260 void Render::start_batches(ArrayList<BatchRenderJob*> *jobs)
263 if(!Thread::running())
265 mode = Render::BATCH;
272 ErrorBox error_box(PROGRAM_NAME ": Error",
273 mwindow->gui->get_abs_cursor_x(1),
274 mwindow->gui->get_abs_cursor_y(1));
275 error_box.create_objects("Already rendering");
276 error_box.raise_window();
277 error_box.run_window();
281 void Render::start_batches(ArrayList<BatchRenderJob*> *jobs,
282 Defaults *boot_defaults,
283 Preferences *preferences,
284 ArrayList<PluginServer*> *plugindb)
286 mode = Render::BATCH;
289 this->preferences = preferences;
290 this->plugindb = plugindb;
294 this->preferences = 0;
297 void Render::stop_operation()
299 if(Thread::running())
302 // Wait for completion
303 completion->lock("Render::stop_operation");
316 if(mode == Render::INTERACTIVE)
318 // Fix the asset for rendering
319 Asset *asset = new Asset;
320 load_defaults(asset);
321 check_asset(mwindow->edl, *asset);
323 // Get format from user
332 render_window = new RenderWindow(mwindow, this, asset);
333 render_window->create_objects();
334 result = render_window->run_window();
336 // add to recentlist only on OK
337 render_window->format_tools->path_recent->add_item(FILE_FORMAT_PREFIX(asset->format), asset->path);
339 delete render_window;
345 // Check the asset format for errors.
346 FormatCheck format_check(asset);
347 format_error = format_check.check_format();
349 }while(format_error && !result);
352 save_defaults(asset);
353 mwindow->save_defaults();
355 if(!result) render(1, asset, mwindow->edl, strategy);
360 if(mode == Render::BATCH)
362 for(int i = 0; i < jobs->total && !result; i++)
364 BatchRenderJob *job = jobs->values[i];
369 mwindow->batch_render->update_active(i);
373 printf("Render::run: %s\n", job->edl_path);
377 FileXML *file = new FileXML;
379 edl->create_objects();
380 file->read_from_file(job->edl_path);
381 if(!plugindb && mwindow)
382 plugindb = mwindow->plugindb;
383 edl->load_xml(plugindb, file, LOAD_ALL);
385 render(0, job->asset, edl, job->strategy);
392 mwindow->batch_render->update_done(i, 1, elapsed_time);
395 char string[BCTEXTLEN];
397 (double)progress_timer->get_scaled_difference(1);
398 Units::totext(string,
401 printf("Render::run: done in %s\n", string);
407 mwindow->batch_render->update_active(-1);
409 printf("Render::run: failed\n");
416 mwindow->batch_render->update_active(-1);
417 mwindow->batch_render->update_done(-1, 0, 0);
424 int Render::check_asset(EDL *edl, Asset &asset)
426 if(asset.video_data &&
427 edl->tracks->playable_video_tracks() &&
428 File::supports_video(asset.format))
430 asset.video_data = 1;
432 asset.width = edl->session->output_w;
433 asset.height = edl->session->output_h;
434 asset.interlace_mode = edl->session->interlace_mode;
435 asset.tcstart = (int64_t) (edl->session->get_frame_offset() +
436 edl->local_session->get_selectionstart() *
437 edl->session->frame_rate);
438 asset.tcend = (int64_t) (edl->session->get_frame_offset() +
439 edl->local_session->get_selectionend() *
440 edl->session->frame_rate);
444 asset.video_data = 0;
450 if(asset.audio_data &&
451 edl->tracks->playable_audio_tracks() &&
452 File::supports_audio(asset.format))
454 asset.audio_data = 1;
455 asset.channels = edl->session->audio_channels;
456 if(asset.format == FILE_MOV) asset.byte_order = 0;
457 asset.tcstart = (int64_t) (edl->session->get_frame_offset() +
458 edl->local_session->get_selectionstart() *
459 edl->session->sample_rate);
460 asset.tcend = (int64_t) (edl->session->get_frame_offset() +
461 edl->local_session->get_selectionend() *
462 edl->session->sample_rate);
466 asset.audio_data = 0;
472 if(!asset.audio_data &&
480 int Render::fix_strategy(int strategy, int use_renderfarm)
484 if(strategy == FILE_PER_LABEL)
485 strategy = FILE_PER_LABEL_FARM;
487 if(strategy == SINGLE_PASS)
488 strategy = SINGLE_PASS_FARM;
492 if(strategy == FILE_PER_LABEL_FARM)
493 strategy = FILE_PER_LABEL;
495 if(strategy == SINGLE_PASS_FARM)
496 strategy = SINGLE_PASS;
501 void Render::start_progress()
503 char filename[BCTEXTLEN];
504 char string[BCTEXTLEN];
507 progress_max = packages->get_progress_max();
509 progress_timer->update();
513 // Generate the progress box
514 fs.extract_name(filename, default_asset->path);
515 sprintf(string, _("Rendering %s..."), filename);
517 // Don't bother with the filename since renderfarm defeats the meaning
518 progress = mwindow->mainprogress->start_progress(_("Rendering..."),
520 render_progress = new RenderProgress(mwindow, this);
521 render_progress->start();
525 void Render::stop_progress()
529 char string[BCTEXTLEN], string2[BCTEXTLEN];
530 delete render_progress;
531 progress->get_time(string);
532 elapsed_time = progress->get_time();
533 progress->stop_progress();
536 sprintf(string2, _("Rendering took %s"), string);
537 mwindow->gui->lock_window("");
538 mwindow->gui->show_message(string2);
539 mwindow->gui->stop_hourglass();
540 mwindow->gui->unlock_window();
547 int Render::render(int test_overwrite,
552 char string[BCTEXTLEN];
553 // Total length in seconds
555 int last_audio_buffer;
556 RenderFarmServer *farm_server = 0;
558 int total_digits; // Total number of digits including padding the user specified.
559 int number_start; // Character in the filename path at which the number begins
560 int current_number; // The number the being injected into the filename.
562 // (VFrame*)(VFrame array [])(Channel [])
563 VFrame ***video_output;
564 // Pointer to output buffers
565 VFrame *video_output_ptr[MAX_CHANNELS];
566 double *audio_output_ptr[MAX_CHANNELS];
571 this->default_asset = asset;
578 preferences = new Preferences;
580 preferences->copy_from(mwindow->preferences);
584 // Create rendering command
585 command = new TransportCommand;
586 command->command = NORMAL_FWD;
587 command->get_edl()->copy_all(edl);
588 command->change_type = CHANGE_ALL;
589 // Get highlighted playback range
590 command->set_playback_range();
591 // Adjust playback range with in/out points
592 command->adjust_playback_range();
593 packages = new PackageDispatcher;
596 // Configure preview monitor
597 VideoOutConfig vconfig;
598 PlaybackConfig *playback_config = new PlaybackConfig;
599 for(int i = 0; i < MAX_CHANNELS; i++)
601 vconfig.do_channel[i] = (i < command->get_edl()->session->video_channels);
602 playback_config->vconfig->do_channel[i] = (i < command->get_edl()->session->video_channels);
603 playback_config->aconfig->do_channel[i] = (i < command->get_edl()->session->audio_channels);
607 audio_cache = new CICache(command->get_edl(), preferences, plugindb);
608 video_cache = new CICache(command->get_edl(), preferences, plugindb);
610 default_asset->frame_rate = command->get_edl()->session->frame_rate;
611 default_asset->sample_rate = command->get_edl()->session->sample_rate;
613 // Conform asset to EDL. Find out if any tracks are playable.
614 result = check_asset(command->get_edl(), *default_asset);
618 // Get total range to render
619 total_start = command->start_position;
620 total_end = command->end_position;
621 total_length = total_end - total_start;
624 if(EQUIV(total_length, 0))
639 // Stop background rendering
640 if(mwindow) mwindow->stop_brender();
642 fs.complete_path(default_asset->path);
643 strategy = Render::fix_strategy(strategy, preferences->use_renderfarm);
645 result = packages->create_packages(mwindow,
666 frames_per_second = 0;
670 // Start dispatching external jobs
673 mwindow->gui->lock_window("Render::render 1");
674 mwindow->gui->show_message(_("Starting render farm"));
675 mwindow->gui->start_hourglass();
676 mwindow->gui->unlock_window();
680 printf("Render::render: starting render farm\n");
683 if(strategy == SINGLE_PASS_FARM || strategy == FILE_PER_LABEL_FARM)
685 farm_server = new RenderFarmServer(plugindb,
695 result = farm_server->start_clients();
701 mwindow->gui->lock_window("Render::render 2");
702 mwindow->gui->show_message(_("Failed to start render farm"),
703 mwindow->theme->message_error);
704 mwindow->gui->stop_hourglass();
705 mwindow->gui->unlock_window();
709 printf("Render::render: Failed to start render farm\n");
718 // Perform local rendering
728 MainPackageRenderer package_renderer(this);
729 result = package_renderer.initialize(mwindow,
730 command->get_edl(), // Copy of master EDL
743 // Get unfinished job
744 RenderPackage *package;
746 if(strategy == SINGLE_PASS_FARM)
748 package = packages->get_package(frames_per_second, -1, 1);
752 package = packages->get_package(0, -1, 1);
767 if(package_renderer.render_package(package))
770 // Result is also set directly by the RenderFarm.
772 frames_per_second = (double)(package->video_end - package->video_start) /
773 (double)(timer.get_difference() / 1000);
780 //printf("Render::run: Session finished.\n");
783 //printf("Render::render 80\n");
787 if(strategy == SINGLE_PASS_FARM || strategy == FILE_PER_LABEL_FARM)
789 farm_server->wait_clients();
790 result |= packages->packages_are_done();
793 //printf("Render::render 90\n");
797 (!progress || !progress->is_cancelled()) &&
802 ErrorBox error_box(PROGRAM_NAME ": Error",
803 mwindow->gui->get_abs_cursor_x(1),
804 mwindow->gui->get_abs_cursor_y(1));
805 error_box.create_objects(_("Error rendering data."));
806 error_box.raise_window();
807 error_box.run_window();
811 printf("Render::render: Error rendering data\n");
815 // Delete the progress box
818 //printf("Render::render 100\n");
826 // Paste all packages into timeline if desired
829 load_mode != LOAD_NOTHING &&
831 mode != Render::BATCH)
833 mwindow->gui->lock_window("Render::render 3");
838 ArrayList<Asset*> *assets = packages->get_asset_list();
839 if(load_mode == LOAD_PASTE)
841 mwindow->load_assets(assets,
846 mwindow->edl->session->labels_follow_edits,
847 mwindow->edl->session->plugins_follow_edits);
848 assets->remove_all_objects();
852 mwindow->save_backup();
853 mwindow->undo->update_undo(_("render"), LOAD_ALL);
854 mwindow->update_plugin_guis();
855 mwindow->gui->update(1,
862 mwindow->sync_parameters(CHANGE_ALL);
863 mwindow->gui->unlock_window();
870 mwindow->gui->lock_window("Render::render 3");
871 mwindow->gui->stop_hourglass();
872 mwindow->gui->unlock_window();
875 //printf("Render::render 110\n");
876 // Need to restart because brender always stops before render.
878 mwindow->restart_brender();
879 if(farm_server) delete farm_server;
881 delete playback_config;
884 // Must delete packages after server
887 completion->unlock();
888 //printf("Render::render 120\n");
894 void Render::create_filename(char *path,
901 int len = strlen(default_path);
902 char printf_string[BCTEXTLEN];
903 int found_number = 0;
905 for(i = 0, j = 0; i < number_start; i++, j++)
907 printf_string[j] = default_path[i];
911 sprintf(&printf_string[j], "%%0%dd", total_digits);
912 j = strlen(printf_string);
915 // Copy remainder of string
916 for( ; i < len; i++, j++)
918 printf_string[j] = default_path[i];
920 printf_string[j] = 0;
921 // Print the printf argument to the path
922 sprintf(path, printf_string, current_number);
925 void Render::get_starting_number(char *path,
932 int len = strlen(path);
933 char number_text[BCTEXTLEN];
941 ptr2 = strrchr(path, '/');
943 // Search for first 0 after last /.
945 ptr = strchr(ptr2, '0');
947 if(ptr && isdigit(*ptr))
949 number_start = ptr - path;
951 // Store the first number
952 char *ptr2 = number_text;
956 current_number = atol(number_text);
957 total_digits = strlen(number_text);
961 // No number found or number not long enough
962 if(total_digits < min_digits)
966 total_digits = min_digits;
976 int Render::load_defaults(Asset *asset)
978 strategy = mwindow->defaults->get("RENDER_STRATEGY", SINGLE_PASS);
979 load_mode = mwindow->defaults->get("RENDER_LOADMODE", LOAD_NEW_TRACKS);
982 asset->load_defaults(mwindow->defaults,
994 int Render::save_defaults(Asset *asset)
996 mwindow->defaults->update("RENDER_STRATEGY", strategy);
997 mwindow->defaults->update("RENDER_LOADMODE", load_mode);
1002 asset->save_defaults(mwindow->defaults,
1020 RenderWindow::RenderWindow(MWindow *mwindow, Render *render, Asset *asset)
1021 : BC_Window(PROGRAM_NAME ": Render",
1022 mwindow->gui->get_root_w(0, 1) / 2 - WIDTH / 2,
1023 mwindow->gui->get_root_h(1) / 2 - HEIGHT / 2,
1032 this->mwindow = mwindow;
1033 this->render = render;
1034 this->asset = asset;
1037 RenderWindow::~RenderWindow()
1039 delete format_tools;
1045 int RenderWindow::create_objects()
1048 add_subwindow(new BC_Title(x,
1050 (char*)((render->strategy == FILE_PER_LABEL ||
1051 render->strategy == FILE_PER_LABEL_FARM) ?
1052 _("Select the first file to render to:") :
1053 _("Select a file to render to:"))));
1056 format_tools = new FormatTools(mwindow,
1059 format_tools->create_objects(x,
1072 loadmode = new LoadMode(mwindow, this, x, y, &render->load_mode, 1);
1073 loadmode->create_objects();
1075 add_subwindow(new BC_OKButton(this));
1076 add_subwindow(new BC_CancelButton(this));