batchrender cleanup, bd/dvd create upgrades, remote ctrl booby fix
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / render.C
index 0398ccf..ff21962 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * CINELERRA
  * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
@@ -96,15 +95,6 @@ int RenderItem::handle_event()
        return 1;
 }
 
-
-
-
-
-
-
-
-
-
 RenderProgress::RenderProgress(MWindow *mwindow, Render *render)
  : Thread(1, 0, 0)
 {
@@ -124,14 +114,12 @@ RenderProgress::~RenderProgress()
 void RenderProgress::run()
 {
        Thread::disable_cancel();
-       while(1)
-       {
-               if(render->total_rendered != last_value)
-               {
+       for( ;; ) {
+               if( render->total_rendered != last_value ) {
                        render->progress->update(render->total_rendered);
                        last_value = render->total_rendered;
 
-                       if(mwindow) mwindow->preferences_thread->update_rates();
+                       if( mwindow ) mwindow->preferences_thread->update_rates();
                }
 
                Thread::enable_cancel();
@@ -167,11 +155,8 @@ int MainPackageRenderer::get_result()
 
 void MainPackageRenderer::set_result(int value)
 {
-       if(value)
+       if( value )
                render->result = value;
-
-
-
 }
 
 void MainPackageRenderer::set_progress(int64_t value)
@@ -186,7 +171,7 @@ void MainPackageRenderer::set_progress(int64_t value)
 //printf("MainPackageRenderer::set_progress %d %ld %f\n", __LINE__, (long)value, frames_per_second);
 
 // If non interactive, print progress out
-       if(!render->progress)
+       if( !render->progress )
                render->show_progress();
 
        render->counter_lock->unlock();
@@ -201,6 +186,18 @@ int MainPackageRenderer::progress_cancelled()
                render->batch_cancelled;
 }
 
+void RenderAssets::clear()
+{
+       for( int i=size(); --i>=0; get(i)->remove_user() );
+       remove_all();
+}
+RenderAssets::RenderAssets()
+{
+}
+RenderAssets::~RenderAssets()
+{
+        clear();
+}
 
 Render::Render(MWindow *mwindow)
  : BC_DialogThread()
@@ -256,8 +253,7 @@ void Render::start_interactive()
 
 void Render::start_batches(ArrayList<BatchRenderJob*> *jobs)
 {
-       if(!thread->running())
-       {
+       if( !thread->running() ) {
                mode = Render::BATCH;
                batch_cancelled = 0;
                this->jobs = jobs;
@@ -297,9 +293,9 @@ BC_Window* Render::new_gui()
        batch_cancelled = 0;
        result = 0;
 
-       if(mode == Render::INTERACTIVE) {
+       if( mode == Render::INTERACTIVE ) {
 // Fix the asset for rendering
-               if(!asset) asset = new Asset;
+               if( !asset ) asset = new Asset;
                load_defaults(asset);
                check_asset(mwindow->edl, *asset);
                int px = mwindow->gui->get_pop_cursor_x(1);
@@ -314,7 +310,7 @@ BC_Window* Render::new_gui()
 
 void Render::handle_done_event(int result)
 {
-       if(!result) {
+       if( !result ) {
                mwindow->edl->session->render_beep = beep;
                // add to recentlist only on OK
                render_window->render_format->path_recent->
@@ -370,7 +366,7 @@ void Render::handle_close_event(int result)
                error_box.run_window();
        }
 
-       if(!result) {
+       if( !result ) {
 // Check the asset format for errors.
                FormatCheck format_check(asset);
                if( format_check.check_format() )
@@ -385,9 +381,9 @@ void Render::handle_close_event(int result)
 //PRINT_TRACE
 
        if( !result ) {
-               if(debug) printf("Render::handle_close_event %d\n", __LINE__);
-               if(!result) start_render();
-               if(debug) printf("Render::handle_close_event %d\n", __LINE__);
+               if( debug ) printf("Render::handle_close_event %d\n", __LINE__);
+               if( !result ) start_render();
+               if( debug ) printf("Render::handle_close_event %d\n", __LINE__);
        }
 //PRINT_TRACE
 }
@@ -396,8 +392,7 @@ void Render::handle_close_event(int result)
 
 void Render::stop_operation()
 {
-       if(thread->Thread::running())
-       {
+       if( thread->Thread::running() ) {
                batch_cancelled = 1;
 // Wait for completion
                completion->lock("Render::stop_operation");
@@ -407,38 +402,33 @@ void Render::stop_operation()
 
 int Render::check_asset(EDL *edl, Asset &asset)
 {
-       if(asset.video_data &&
+       if( asset.video_data &&
                edl->tracks->playable_video_tracks() &&
-               File::renders_video(&asset))
-       {
+               File::renders_video(&asset) ) {
                asset.video_data = 1;
                asset.layers = 1;
                asset.width = edl->session->output_w;
                asset.height = edl->session->output_h;
                asset.interlace_mode = edl->session->interlace_mode;
        }
-       else
-       {
+       else {
                asset.video_data = 0;
                asset.layers = 0;
        }
 
-       if(asset.audio_data &&
+       if( asset.audio_data &&
                edl->tracks->playable_audio_tracks() &&
-               File::renders_audio(&asset))
-       {
+               File::renders_audio(&asset) ) {
                asset.audio_data = 1;
                asset.channels = edl->session->audio_channels;
        }
-       else
-       {
+       else {
                asset.audio_data = 0;
                asset.channels = 0;
        }
 
-       if(!asset.audio_data &&
-               !asset.video_data)
-       {
+       if( !asset.audio_data &&
+               !asset.video_data ) {
                return 1;
        }
        return 0;
@@ -459,15 +449,13 @@ void Render::start_progress()
 {
        char filename[BCTEXTLEN];
        char string[BCTEXTLEN];
-       FileSystem fs;
-
        progress_max = packages->get_progress_max();
 
        progress_timer->update();
        last_eta = 0;
-       if(mwindow)
-       {
+       if( mwindow ) {
 // Generate the progress box
+               FileSystem fs;
                fs.extract_name(filename, default_asset->path);
                sprintf(string, _("Rendering %s..."), filename);
 
@@ -481,8 +469,7 @@ void Render::start_progress()
 
 void Render::stop_progress()
 {
-       if(progress)
-       {
+       if( progress ) {
                char string[BCTEXTLEN], string2[BCTEXTLEN];
                delete render_progress;
                progress->get_time(string);
@@ -503,7 +490,7 @@ void Render::stop_progress()
 void Render::show_progress()
 {
        int64_t current_eta = progress_timer->get_scaled_difference(1000);
-       if (current_eta - last_eta < 1000 ) return;
+       if( current_eta - last_eta < 1000  ) return;
        double eta = !total_rendered ? 0 :
                current_eta / 1000. * (progress_max / (double)total_rendered - 1.);
        char string[BCTEXTLEN];  Units::totext(string, eta, TIME_HMS2);
@@ -535,8 +522,7 @@ void Render::create_filename(char *path,
        int len = strlen(default_path);
        char printf_string[BCTEXTLEN];
 
-       for(i = 0, j = 0; i < number_start; i++, j++)
-       {
+       for( i=0, j=0; i<number_start; ++i, ++j ) {
                printf_string[j] = default_path[i];
        }
 
@@ -546,8 +532,7 @@ void Render::create_filename(char *path,
        i += total_digits;
 
 // Copy remainder of string
-       for( ; i < len; i++, j++)
-       {
+       for( ; i<len; ++i, ++j ) {
                printf_string[j] = default_path[i];
        }
        printf_string[j] = 0;
@@ -573,17 +558,15 @@ void Render::get_starting_number(char *path,
        ptr2 = strrchr(path, '/');
 
 // Search for first 0 after last /.
-       if(ptr2)
+       if( ptr2 )
                ptr = strchr(ptr2, '0');
 
-       if(ptr && isdigit(*ptr))
-       {
+       if( ptr && isdigit(*ptr) ) {
                number_start = ptr - path;
 
 // Store the first number
                char *ptr2 = number_text;
-               while(isdigit(*ptr))
-                       *ptr2++ = *ptr++;
+               while( isdigit(*ptr) ) *ptr2++ = *ptr++;
                *ptr2++ = 0;
                current_number = atol(number_text);
                total_digits = strlen(number_text);
@@ -591,8 +574,7 @@ void Render::get_starting_number(char *path,
 
 
 // No number found or number not long enough
-       if(total_digits < min_digits)
-       {
+       if( total_digits < min_digits ) {
                current_number = 1;
                number_start = len;
                total_digits = min_digits;
@@ -647,6 +629,11 @@ int Render::save_defaults(Asset *asset)
        return 0;
 }
 
+void Render::update_assets()
+{
+       if( packages )
+               packages->get_asset_list(assets);
+}
 
 static void run_script(const char *script, const char *arg)
 {
@@ -675,12 +662,8 @@ void RenderThread::render_single(int test_overwrite, Asset *asset, EDL *edl,
 // Total length in seconds
        double total_length = 0;
        RenderFarmServer *farm_server = 0;
-       FileSystem fs;
-       const int debug = 0;
 
        render->in_progress = 1;
-
-
        render->default_asset = asset;
        render->progress = 0;
        render->result = 0;
@@ -725,82 +708,62 @@ void RenderThread::render_single(int test_overwrite, Asset *asset, EDL *edl,
        render->result = render->check_asset(command->get_edl(),
                *render->default_asset);
 
-       if(!render->result)
-       {
+       if( !render->result ) {
 // Get total range to render
                render->total_start = command->start_position;
                render->total_end = command->end_position;
                total_length = render->total_end - render->total_start;
 
 // Nothing to render
-               if(EQUIV(total_length, 0))
-               {
+               if( EQUIV(total_length, 0) ) {
                        render->result = 1;
                }
        }
        render_frames = render->default_asset->frame_rate * total_length;
 
 // Generate packages
-       if(!render->result)
-       {
+       if( !render->result ) {
 // Stop background rendering
-               if(mwindow) mwindow->stop_brender();
+               if( mwindow ) mwindow->stop_brender();
 
+               FileSystem fs;
                fs.complete_path(render->default_asset->path);
 
-               render->result = render->packages->create_packages(mwindow,
-                       command->get_edl(),
-                       render->preferences,
-                       strategy,
-                       render->default_asset,
-                       render->total_start,
-                       render->total_end,
-                       test_overwrite);
+               render->result = render->packages->create_packages(mwindow, command->get_edl(),
+                       render->preferences, strategy, render->default_asset,
+                       render->total_start, render->total_end, test_overwrite);
        }
 
        render->total_rendered = 0;
 
-       if(!render->result)
-       {
+       if( !render->result ) {
 // Start dispatching external jobs
-               if(mwindow)
-               {
+               if( mwindow ) {
                        mwindow->gui->lock_window("Render::render 1");
                        mwindow->gui->show_message(_("Starting render farm"));
                        mwindow->gui->start_hourglass();
                        mwindow->gui->unlock_window();
                }
-               else
-               {
+               else {
                        printf("Render::render: starting render farm\n");
                }
 
-               if(strategy == SINGLE_PASS_FARM || strategy == FILE_PER_LABEL_FARM)
-               {
-                       farm_server = new RenderFarmServer(mwindow,
-                               render->packages,
-                               render->preferences,
-                               1,
-                               &render->result,
-                               &render->total_rendered,
-                               render->counter_lock,
-                               render->default_asset,
-                               command->get_edl(),
-                               0);
+               if( strategy == SINGLE_PASS_FARM || strategy == FILE_PER_LABEL_FARM ) {
+                       farm_server = new RenderFarmServer(mwindow, render->packages,
+                               render->preferences, 1, &render->result,
+                               &render->total_rendered, render->counter_lock,
+                               render->default_asset, command->get_edl(), 0);
                        render->result = farm_server->start_clients();
 
-                       if(render->result)
-                       {
-                               if(mwindow)
-                               {
+                       if( render->result ) {
+                               if( mwindow ) {
                                        mwindow->gui->lock_window("Render::render 2");
                                        mwindow->gui->show_message(_("Failed to start render farm"),
                                                mwindow->theme->message_error);
                                        mwindow->gui->stop_hourglass();
                                        mwindow->gui->unlock_window();
                                }
-                               else
-                               {
+                               else {
                                        printf("Render::render: Failed to start render farm\n");
                                }
                        }
@@ -809,8 +772,8 @@ void RenderThread::render_single(int test_overwrite, Asset *asset, EDL *edl,
 
 // Perform local rendering
 
-       if(!render->result)
-       {
+       render->assets.clear();
+       if( !render->result ) {
                render->start_progress();
 
                MainPackageRenderer package_renderer(render);
@@ -819,33 +782,16 @@ void RenderThread::render_single(int test_overwrite, Asset *asset, EDL *edl,
                                render->preferences,
                                render->default_asset);
 
-               while(!render->result)
-               {
+               while( !render->result ) {
+                       int fps = strategy == SINGLE_PASS_FARM ?
+                               package_renderer.frames_per_second : 0;
 // Get unfinished job
-                       RenderPackage *package;
-
-                       if(strategy == SINGLE_PASS_FARM)
-                       {
-                               package = render->packages->get_package(
-                                       package_renderer.frames_per_second,
-                                       -1,
-                                       1);
-                       }
-                       else
-                       {
-                               package = render->packages->get_package(0, -1, 1);
-                       }
-
+                       RenderPackage *package = render->packages->get_package(fps, -1, 1);
 // Exit point
-                       if(!package)
-                       {
-                               break;
-                       }
+                       if( !package ) break;
 
-                       if(package_renderer.render_package(package))
+                       if( package_renderer.render_package(package) )
                                render->result = 1;
-
-
                } // file_number
 
 printf("Render::render_single: Session finished.\n");
@@ -862,93 +808,55 @@ printf("Render::render_single: Session finished.\n");
                        render->result |= render->packages->packages_are_done();
                }
 
-if(debug) printf("Render::render %d\n", __LINE__);
-
 // Notify of error
-               if(render->result &&
-                       (!render->progress || !render->progress->is_cancelled()) &&
-                       !render->batch_cancelled)
-               {
-if(debug) printf("Render::render %d\n", __LINE__);
-                       if(mwindow)
-                       {
-if(debug) printf("Render::render %d\n", __LINE__);
+               if( render->result && !render->batch_cancelled &&
+                   (!render->progress || !render->progress->is_cancelled()) ) {
+                       if( mwindow ) {
                                int cx, cy;
                                mwindow->gui->get_abs_cursor(cx, cy, 1);
                                ErrorBox error_box(_(PROGRAM_NAME ": Error"), cx, cy);
                                error_box.create_objects(_("Error rendering data."));
                                error_box.raise_window();
                                error_box.run_window();
-if(debug) printf("Render::render %d\n", __LINE__);
                        }
-                       else
-                       {
+                       else {
                                printf("Render::render: Error rendering data\n");
                        }
                }
-if(debug) printf("Render::render %d\n", __LINE__);
 
 // Delete the progress box
                render->stop_progress();
-if(debug) printf("Render::render %d\n", __LINE__);
+               render->update_assets();
        }
 
 // Paste all packages into timeline if desired
 
-       if(!render->result &&
-               render->load_mode != LOADMODE_NOTHING &&
-               mwindow &&
-               render->mode != Render::BATCH)
-       {
-if(debug) printf("Render::render %d\n", __LINE__);
+       if( !render->result && mwindow &&
+           render->load_mode != LOADMODE_NOTHING &&
+           render->mode != Render::BATCH ) {
                mwindow->gui->lock_window("Render::render 3");
-if(debug) printf("Render::render %d\n", __LINE__);
 
                mwindow->undo->update_undo_before();
-
-if(debug) printf("Render::render %d\n", __LINE__);
-
-
-               ArrayList<Indexable*> *assets = render->packages->get_asset_list();
-if(debug) printf("Render::render %d\n", __LINE__);
-               if(render->load_mode == LOADMODE_PASTE)
+               if( render->load_mode == LOADMODE_PASTE )
                        mwindow->clear(0);
-if(debug) printf("Render::render %d\n", __LINE__);
-               mwindow->load_assets(assets, -1, render->load_mode, 0, 0,
+               mwindow->load_assets(&render->assets,
+                       -1, render->load_mode, 0, 0,
                        mwindow->edl->session->labels_follow_edits,
                        mwindow->edl->session->plugins_follow_edits,
                        mwindow->edl->session->autos_follow_edits,
                        0); // overwrite
-if(debug) printf("Render::render %d\n", __LINE__);
-               for(int i = 0; i < assets->size(); i++)
-                       assets->get(i)->Garbage::remove_user();
-               delete assets;
-if(debug) printf("Render::render %d\n", __LINE__);
-
-
                mwindow->save_backup();
-if(debug) printf("Render::render %d\n", __LINE__);
                mwindow->undo->update_undo_after(_("render"), LOAD_ALL);
-if(debug) printf("Render::render %d\n", __LINE__);
                mwindow->update_plugin_guis();
-if(debug) printf("Render::render %d\n", __LINE__);
                mwindow->gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
-if(debug) printf("Render::render %d\n", __LINE__);
                mwindow->sync_parameters(CHANGE_ALL);
-if(debug) printf("Render::render %d\n", __LINE__);
                mwindow->gui->unlock_window();
 
-
                mwindow->awindow->gui->async_update_assets();
-
-if(debug) printf("Render::render %d\n", __LINE__);
        }
 
-if(debug) printf("Render::render %d\n", __LINE__);
-
 // Disable hourglass
-       if(mwindow)
-       {
+       if( mwindow ) {
                mwindow->gui->lock_window("Render::render 3");
                mwindow->gui->stop_hourglass();
                mwindow->gui->unlock_window();
@@ -956,9 +864,9 @@ if(debug) printf("Render::render %d\n", __LINE__);
 
 //printf("Render::render 110\n");
 // Need to restart because brender always stops before render.
-       if(mwindow)
+       if( mwindow )
                mwindow->restart_brender();
-       if(farm_server) delete farm_server;
+       if( farm_server ) delete farm_server;
        delete command;
        delete audio_cache;
        delete video_cache;
@@ -967,103 +875,80 @@ if(debug) printf("Render::render %d\n", __LINE__);
 
        render->packages = 0;
        render->in_progress = 0;
-if(debug) printf("Render::render %d\n", __LINE__);
 }
 
 void RenderThread::run()
 {
+       char *script = 0;
        Timer render_timer;
 
        if( mwindow )
                render->preferences->copy_from(mwindow->preferences);
 
-       if(render->mode == Render::INTERACTIVE)
-       {
+       if( render->mode == Render::INTERACTIVE ) {
                render_single(1, render->asset, mwindow->edl,
                        render->get_strategy(), render->range_type);
        }
        else
-       if(render->mode == Render::BATCH)
-       {
-// PRINT_TRACE
-// printf("RenderThread::run %d %d %d\n",
-// __LINE__,
-// render->jobs->total,
-// render->result);
-               for(int i = 0; i < render->jobs->total && !render->result; i++)
-               {
-//PRINT_TRACE
+       if( render->mode == Render::BATCH ) {
+               for( int i=0; i<render->jobs->total && !render->result; ++i ) {
                        BatchRenderJob *job = render->jobs->values[i];
-//PRINT_TRACE
-                       if(job->enabled)
-                       {
-                               if( *job->edl_path == '@' )
-                               {
-                                       run_script(job->edl_path+1, job->asset->path);
-                               }
-
-                               if(mwindow)
-                               {
-                                       mwindow->batch_render->update_active(i);
-                               }
-                               else
-                               {
-                                       printf("Render::run: %s\n", job->edl_path);
-                               }
-
-//PRINT_TRACE
-
-                               FileXML *file = new FileXML;
-                               EDL *edl = new EDL;
-                               edl->create_objects();
-                               file->read_from_file(job->edl_path);
-                               edl->load_xml(file, LOAD_ALL);
-//PRINT_TRACE
-                               render_single(0, job->asset, edl, job->get_strategy(), RANGE_BACKCOMPAT);
-
-//PRINT_TRACE
-                               edl->Garbage::remove_user();
-                               delete file;
-                               if(!render->result)
-                               {
-                                       if(mwindow)
-                                               mwindow->batch_render->update_done(i, 1, render->elapsed_time);
-                                       else
-                                       {
-                                               char string[BCTEXTLEN];
-                                               render->elapsed_time =
-                                                       (double)render->progress_timer->get_scaled_difference(1);
-                                               Units::totext(string,
-                                                       render->elapsed_time,
-                                                       TIME_HMS2);
-                                               printf("Render::run: done in %s\n", string);
-                                       }
+                       if( !job->enabled ) continue;
+                       if( mwindow )
+                               mwindow->batch_render->update_active(i);
+                       else
+                               printf("Render::run: %s\n", job->edl_path);
+
+                       FileXML *file = new FileXML;
+                       EDL *edl = new EDL;
+                       edl->create_objects();
+                       file->read_from_file(job->edl_path);
+                       edl->load_xml(file, LOAD_ALL);
+                       delete file;
+
+                       render_single(0, job->asset, edl, job->get_strategy(), RANGE_BACKCOMPAT);
+                       if( !render->result ) {
+                               if( !i )
+                                       script = job->create_script(edl, &render->assets);
+                               if( mwindow )
+                                       mwindow->batch_render->update_done(i, 1, render->elapsed_time);
+                               else {
+                                       char string[BCTEXTLEN];
+                                       render->elapsed_time =
+                                               (double)render->progress_timer->get_scaled_difference(1);
+                                       Units::totext(string, render->elapsed_time, TIME_HMS2);
+                                       printf("Render::run: done in %s\n", string);
                                }
+                       }
+                       else {
+                               if( mwindow )
+                                       mwindow->batch_render->update_active(-1);
                                else
-                               {
-                                       if(mwindow)
-                                               mwindow->batch_render->update_active(-1);
-                                       else
-                                               printf("Render::run: failed\n");
-                               }
+                                       printf("Render::run: failed\n");
                        }
-//PRINT_TRACE
                }
-
-               if(mwindow)
-               {
+               if( mwindow ) {
                        mwindow->batch_render->update_active(-1);
                        mwindow->batch_render->update_done(-1, 0, 0);
                }
        }
        render->completion->unlock();
-       double render_time = render_timer.get_difference() / 1000.0;
-       double render_rate = render_time > 0 ? render_frames / render_time : 0;
-       printf("** rendered %jd frames in %0.3f secs, %0.3f fps\n",
-               render_frames, render_time, render_rate);
+
+       if( !render->result ) {
+               double render_time = render_timer.get_difference() / 1000.0;
+               double render_rate = render_time > 0 ? render_frames / render_time : 0;
+               printf("** rendered %jd frames in %0.3f secs, %0.3f fps\n",
+                       render_frames, render_time, render_rate);
+       }
 
        if( render->mode == Render::INTERACTIVE && render->beep )
                mwindow->beep(3000., 1.5, 0.5);
+
+       if( script ) {
+               if( !render->result )
+                       run_script(script, 0);
+               delete [] script;
+       }
 }