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.run_window();
259 void Render::start_batches(ArrayList<BatchRenderJob*> *jobs)
262 if(!Thread::running())
264 mode = Render::BATCH;
271 ErrorBox error_box(PROGRAM_NAME ": Error",
272 mwindow->gui->get_abs_cursor_x(1),
273 mwindow->gui->get_abs_cursor_y(1));
274 error_box.create_objects("Already rendering");
275 error_box.run_window();
279 void Render::start_batches(ArrayList<BatchRenderJob*> *jobs,
280 Defaults *boot_defaults,
281 Preferences *preferences,
282 ArrayList<PluginServer*> *plugindb)
284 mode = Render::BATCH;
287 this->preferences = preferences;
288 this->plugindb = plugindb;
292 this->preferences = 0;
295 void Render::stop_operation()
297 if(Thread::running())
300 // Wait for completion
301 completion->lock("Render::stop_operation");
314 if(mode == Render::INTERACTIVE)
316 // Fix the asset for rendering
317 Asset *asset = new Asset;
318 load_defaults(asset);
319 check_asset(mwindow->edl, *asset);
321 // Get format from user
330 render_window = new RenderWindow(mwindow, this, asset);
331 render_window->create_objects();
332 result = render_window->run_window();
334 // add to recentlist only on OK
335 render_window->format_tools->path_recent->add_item(FILE_FORMAT_PREFIX(asset->format), asset->path);
337 delete render_window;
343 // Check the asset format for errors.
344 FormatCheck format_check(asset);
345 format_error = format_check.check_format();
347 }while(format_error && !result);
350 save_defaults(asset);
351 mwindow->save_defaults();
353 if(!result) render(1, asset, mwindow->edl, strategy);
358 if(mode == Render::BATCH)
360 for(int i = 0; i < jobs->total && !result; i++)
362 BatchRenderJob *job = jobs->values[i];
367 mwindow->batch_render->update_active(i);
371 printf("Render::run: %s\n", job->edl_path);
375 FileXML *file = new FileXML;
377 edl->create_objects();
378 file->read_from_file(job->edl_path);
379 if(!plugindb && mwindow)
380 plugindb = mwindow->plugindb;
381 edl->load_xml(plugindb, file, LOAD_ALL);
383 render(0, job->asset, edl, job->strategy);
390 mwindow->batch_render->update_done(i, 1, elapsed_time);
393 char string[BCTEXTLEN];
395 (double)progress_timer->get_scaled_difference(1);
396 Units::totext(string,
399 printf("Render::run: done in %s\n", string);
405 mwindow->batch_render->update_active(-1);
407 printf("Render::run: failed\n");
414 mwindow->batch_render->update_active(-1);
415 mwindow->batch_render->update_done(-1, 0, 0);
422 int Render::check_asset(EDL *edl, Asset &asset)
424 if(asset.video_data &&
425 edl->tracks->playable_video_tracks() &&
426 File::supports_video(asset.format))
428 asset.video_data = 1;
430 asset.width = edl->session->output_w;
431 asset.height = edl->session->output_h;
432 asset.interlace_mode = edl->session->interlace_mode;
433 asset.tcstart = (int64_t) (edl->session->get_frame_offset() +
434 edl->local_session->get_selectionstart() *
435 edl->session->frame_rate);
436 asset.tcend = (int64_t) (edl->session->get_frame_offset() +
437 edl->local_session->get_selectionend() *
438 edl->session->frame_rate);
442 asset.video_data = 0;
448 if(asset.audio_data &&
449 edl->tracks->playable_audio_tracks() &&
450 File::supports_audio(asset.format))
452 asset.audio_data = 1;
453 asset.channels = edl->session->audio_channels;
454 if(asset.format == FILE_MOV) asset.byte_order = 0;
455 asset.tcstart = (int64_t) (edl->session->get_frame_offset() +
456 edl->local_session->get_selectionstart() *
457 edl->session->sample_rate);
458 asset.tcend = (int64_t) (edl->session->get_frame_offset() +
459 edl->local_session->get_selectionend() *
460 edl->session->sample_rate);
464 asset.audio_data = 0;
470 if(!asset.audio_data &&
478 int Render::fix_strategy(int strategy, int use_renderfarm)
482 if(strategy == FILE_PER_LABEL)
483 strategy = FILE_PER_LABEL_FARM;
485 if(strategy == SINGLE_PASS)
486 strategy = SINGLE_PASS_FARM;
490 if(strategy == FILE_PER_LABEL_FARM)
491 strategy = FILE_PER_LABEL;
493 if(strategy == SINGLE_PASS_FARM)
494 strategy = SINGLE_PASS;
499 void Render::start_progress()
501 char filename[BCTEXTLEN];
502 char string[BCTEXTLEN];
505 progress_max = Units::to_int64(default_asset->sample_rate *
506 (total_end - total_start)) +
507 Units::to_int64(preferences->render_preroll *
508 packages->total_allocated *
509 default_asset->sample_rate);
510 progress_timer->update();
514 // Generate the progress box
515 fs.extract_name(filename, default_asset->path);
516 sprintf(string, _("Rendering %s..."), filename);
518 // Don't bother with the filename since renderfarm defeats the meaning
519 progress = mwindow->mainprogress->start_progress(_("Rendering..."),
521 render_progress = new RenderProgress(mwindow, this);
522 render_progress->start();
526 void Render::stop_progress()
530 char string[BCTEXTLEN], string2[BCTEXTLEN];
531 delete render_progress;
532 progress->get_time(string);
533 elapsed_time = progress->get_time();
534 progress->stop_progress();
537 sprintf(string2, _("Rendering took %s"), string);
538 mwindow->gui->lock_window("");
539 mwindow->gui->show_message(string2);
540 mwindow->gui->stop_hourglass();
541 mwindow->gui->unlock_window();
548 int Render::render(int test_overwrite,
553 char string[BCTEXTLEN];
554 // Total length in seconds
556 int last_audio_buffer;
557 RenderFarmServer *farm_server = 0;
559 int total_digits; // Total number of digits including padding the user specified.
560 int number_start; // Character in the filename path at which the number begins
561 int current_number; // The number the being injected into the filename.
563 // (VFrame*)(VFrame array [])(Channel [])
564 VFrame ***video_output;
565 // Pointer to output buffers
566 VFrame *video_output_ptr[MAX_CHANNELS];
567 double *audio_output_ptr[MAX_CHANNELS];
572 this->default_asset = asset;
579 preferences = new Preferences;
581 preferences->copy_from(mwindow->preferences);
585 // Create rendering command
586 command = new TransportCommand;
587 command->command = NORMAL_FWD;
588 command->get_edl()->copy_all(edl);
589 command->change_type = CHANGE_ALL;
590 // Get highlighted playback range
591 command->set_playback_range();
592 // Adjust playback range with in/out points
593 command->adjust_playback_range();
594 packages = new PackageDispatcher;
597 // Configure preview monitor
598 VideoOutConfig vconfig;
599 PlaybackConfig *playback_config = new PlaybackConfig;
600 for(int i = 0; i < MAX_CHANNELS; i++)
602 vconfig.do_channel[i] = (i < command->get_edl()->session->video_channels);
603 playback_config->vconfig->do_channel[i] = (i < command->get_edl()->session->video_channels);
604 playback_config->aconfig->do_channel[i] = (i < command->get_edl()->session->audio_channels);
608 audio_cache = new CICache(command->get_edl(), preferences, plugindb);
609 video_cache = new CICache(command->get_edl(), preferences, plugindb);
611 default_asset->frame_rate = command->get_edl()->session->frame_rate;
612 default_asset->sample_rate = command->get_edl()->session->sample_rate;
614 // Conform asset to EDL. Find out if any tracks are playable.
615 result = check_asset(command->get_edl(), *default_asset);
619 // Get total range to render
620 total_start = command->start_position;
621 total_end = command->end_position;
622 total_length = total_end - total_start;
625 if(EQUIV(total_length, 0))
640 // Stop background rendering
641 if(mwindow) mwindow->stop_brender();
643 fs.complete_path(default_asset->path);
644 strategy = Render::fix_strategy(strategy, preferences->use_renderfarm);
646 result = packages->create_packages(mwindow,
667 frames_per_second = 0;
671 // Start dispatching external jobs
674 mwindow->gui->lock_window("Render::render 1");
675 mwindow->gui->show_message(_("Starting render farm"));
676 mwindow->gui->start_hourglass();
677 mwindow->gui->unlock_window();
681 printf("Render::render: starting render farm\n");
684 if(strategy == SINGLE_PASS_FARM || strategy == FILE_PER_LABEL_FARM)
686 farm_server = new RenderFarmServer(plugindb,
696 result = farm_server->start_clients();
702 mwindow->gui->lock_window("Render::render 2");
703 mwindow->gui->show_message(_("Failed to start render farm"),
704 mwindow->theme->message_error);
705 mwindow->gui->stop_hourglass();
706 mwindow->gui->unlock_window();
710 printf("Render::render: Failed to start render farm\n");
719 // Perform local rendering
729 MainPackageRenderer package_renderer(this);
730 result = package_renderer.initialize(mwindow,
731 command->get_edl(), // Copy of master EDL
744 // Get unfinished job
745 RenderPackage *package;
747 if(strategy == SINGLE_PASS_FARM)
749 package = packages->get_package(frames_per_second, -1, 1);
753 package = packages->get_package(0, -1, 1);
768 if(package_renderer.render_package(package))
771 // Result is also set directly by the RenderFarm.
773 frames_per_second = (double)(package->video_end - package->video_start) /
774 (double)(timer.get_difference() / 1000);
781 //printf("Render::run: Session finished.\n");
784 //printf("Render::render 80\n");
788 if(strategy == SINGLE_PASS_FARM || strategy == FILE_PER_LABEL_FARM)
790 farm_server->wait_clients();
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.run_window();
810 printf("Render::render: Error rendering data\n");
814 // Delete the progress box
817 //printf("Render::render 100\n");
825 // Paste all packages into timeline if desired
828 load_mode != LOAD_NOTHING &&
830 mode != Render::BATCH)
832 mwindow->gui->lock_window("Render::render 3");
837 ArrayList<Asset*> *assets = packages->get_asset_list();
838 if(load_mode == LOAD_PASTE)
840 mwindow->load_assets(assets,
845 mwindow->edl->session->labels_follow_edits,
846 mwindow->edl->session->plugins_follow_edits);
847 assets->remove_all_objects();
851 mwindow->save_backup();
852 mwindow->undo->update_undo(_("render"), LOAD_ALL);
853 mwindow->update_plugin_guis();
854 mwindow->gui->update(1,
861 mwindow->sync_parameters(CHANGE_ALL);
862 mwindow->gui->unlock_window();
869 mwindow->gui->lock_window("Render::render 3");
870 mwindow->gui->stop_hourglass();
871 mwindow->gui->unlock_window();
874 //printf("Render::render 110\n");
875 // Need to restart because brender always stops before render.
877 mwindow->restart_brender();
878 if(farm_server) delete farm_server;
880 delete playback_config;
883 // Must delete packages after server
886 completion->unlock();
887 //printf("Render::render 120\n");
893 void Render::create_filename(char *path,
900 int len = strlen(default_path);
901 char printf_string[BCTEXTLEN];
902 int found_number = 0;
904 for(i = 0, j = 0; i < number_start; i++, j++)
906 printf_string[j] = default_path[i];
910 sprintf(&printf_string[j], "%%0%dd", total_digits);
911 j = strlen(printf_string);
914 // Copy remainder of string
915 for( ; i < len; i++, j++)
917 printf_string[j] = default_path[i];
919 printf_string[j] = 0;
920 // Print the printf argument to the path
921 sprintf(path, printf_string, current_number);
924 void Render::get_starting_number(char *path,
931 int len = strlen(path);
932 char number_text[BCTEXTLEN];
940 ptr2 = strrchr(path, '/');
942 // Search for first 0 after last /.
944 ptr = strchr(ptr2, '0');
946 if(ptr && isdigit(*ptr))
948 number_start = ptr - path;
950 // Store the first number
951 char *ptr2 = number_text;
955 current_number = atol(number_text);
956 total_digits = strlen(number_text);
960 // No number found or number not long enough
961 if(total_digits < min_digits)
965 total_digits = min_digits;
975 int Render::load_defaults(Asset *asset)
977 strategy = mwindow->defaults->get("RENDER_STRATEGY", SINGLE_PASS);
978 load_mode = mwindow->defaults->get("RENDER_LOADMODE", LOAD_NEW_TRACKS);
981 asset->load_defaults(mwindow->defaults,
993 int Render::save_defaults(Asset *asset)
995 mwindow->defaults->update("RENDER_STRATEGY", strategy);
996 mwindow->defaults->update("RENDER_LOADMODE", load_mode);
1001 asset->save_defaults(mwindow->defaults,
1019 RenderWindow::RenderWindow(MWindow *mwindow, Render *render, Asset *asset)
1020 : BC_Window(PROGRAM_NAME ": Render",
1021 mwindow->gui->get_root_w(0, 1) / 2 - WIDTH / 2,
1022 mwindow->gui->get_root_h(1) / 2 - HEIGHT / 2,
1031 this->mwindow = mwindow;
1032 this->render = render;
1033 this->asset = asset;
1036 RenderWindow::~RenderWindow()
1038 delete format_tools;
1044 int RenderWindow::create_objects()
1047 add_subwindow(new BC_Title(x,
1049 (char*)((render->strategy == FILE_PER_LABEL ||
1050 render->strategy == FILE_PER_LABEL_FARM) ?
1051 _("Select the first file to render to:") :
1052 _("Select a file to render to:"))));
1055 format_tools = new FormatTools(mwindow,
1058 format_tools->create_objects(x,
1071 loadmode = new LoadMode(mwindow, this, x, y, &render->load_mode, 1);
1072 loadmode->create_objects();
1074 add_subwindow(new BC_OKButton(this));
1075 add_subwindow(new BC_CancelButton(this));