4 * Copyright (C) 2008 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
30 #include "cwindowgui.h"
34 #include "edlsession.h"
37 #include "filesystem.h"
39 #include "indexfile.h"
42 #include "mwindowgui.h"
43 #include "packagerenderer.h"
44 #include "playabletracks.h"
45 #include "playbackconfig.h"
46 #include "pluginserver.h"
47 #include "preferences.h"
49 #include "renderengine.h"
50 #include "renderfarmfsserver.h"
52 #include "sighandler.h"
54 #include "transportque.h"
57 #include "videodevice.h"
66 RenderPackage::RenderPackage()
79 RenderPackage::~RenderPackage()
90 // Used by RenderFarm and in the future, Render, to do packages.
91 PackageRenderer::PackageRenderer()
99 frames_per_second = 0;
102 PackageRenderer::~PackageRenderer()
113 int PackageRenderer::initialize(MWindow *mwindow,
115 Preferences *preferences,
116 Asset *default_asset)
120 this->mwindow = mwindow;
122 this->preferences = preferences;
123 this->default_asset = default_asset;
126 //printf("PackageRenderer::initialize %d\n", preferences->processors);
127 command = new TransportCommand;
128 command->command = NORMAL_FWD;
129 command->get_edl()->copy_all(edl);
130 command->change_type = CHANGE_ALL;
131 command->set_playback_range(edl);
133 default_asset->frame_rate = command->get_edl()->session->frame_rate;
134 default_asset->sample_rate = command->get_edl()->session->sample_rate;
135 default_asset->aspect_ratio = (double)command->get_edl()->session->aspect_w /
136 command->get_edl()->session->aspect_h;
137 result = Render::check_asset(edl, *default_asset);
139 audio_cache = new CICache(preferences);
140 video_cache = new CICache(preferences);
142 //PlaybackConfig *config = command->get_edl()->session->playback_config;
143 aconfig = new AudioOutConfig();
144 vconfig = new VideoOutConfig;
149 void PackageRenderer::create_output()
152 asset = new Asset(*default_asset);
154 if(!get_master() && preferences->renderfarm_vfs && preferences->use_renderfarm)
155 sprintf(asset->path, RENDERFARM_FS_PREFIX "%s", package->path);
157 strcpy(asset->path, package->path);
164 file->set_processors(preferences->processors);
166 //printf("PackageRenderer::create_output %d\n", __LINE__);
167 result = file->open_file(preferences,
171 //printf("PackageRenderer::create_output %d %d\n", __LINE__, result);
173 if(result && mwindow)
176 char string[BCTEXTLEN];
177 sprintf(string, _("Couldn't open %s"), asset->path);
178 ErrorBox error(_(PROGRAM_NAME ": Error"),
179 mwindow->gui->get_abs_cursor_x(1),
180 mwindow->gui->get_abs_cursor_y(1));
181 error.create_objects(string);
187 mwindow->sighandler->push_file(file);
188 IndexFile::delete_index(preferences, asset);
190 //printf("PackageRenderer::create_output %d %d\n", __LINE__, result);
193 void PackageRenderer::create_engine()
195 // Fix audio buffers to 1 second
196 audio_read_length = command->get_edl()->session->sample_rate;
197 command->get_edl()->session->playback_config->aconfig->fragment_size = audio_read_length;
199 aconfig->fragment_size = audio_read_length;
202 render_engine = new RenderEngine(0,
207 render_engine->set_acache(audio_cache);
208 render_engine->set_vcache(video_cache);
209 render_engine->arm_command(command);
211 if(package->use_brender)
213 audio_preroll = Units::to_int64((double)preferences->brender_preroll /
214 default_asset->frame_rate *
215 default_asset->sample_rate);
216 video_preroll = preferences->brender_preroll;
220 audio_preroll = Units::to_int64(preferences->render_preroll *
221 default_asset->sample_rate);
222 video_preroll = Units::to_int64(preferences->render_preroll *
223 default_asset->frame_rate);
225 audio_position = package->audio_start - audio_preroll;
226 if( audio_position < 0 )
228 audio_preroll += audio_position;
231 video_position = package->video_start - video_preroll;
232 if( video_position < 0 )
234 video_preroll += video_position;
241 // Create output buffers
242 if(asset->audio_data)
244 file->start_audio_thread(audio_read_length,
245 preferences->processors > 1 ? 2 : 1);
250 if(asset->video_data)
252 compressed_output = new VFrame;
253 // The write length needs to correlate with the processor count because
254 // it is passed to the file handler which usually processes frames simultaneously.
255 video_write_length = preferences->processors;
256 video_write_position = 0;
257 direct_frame_copying = 0;
260 //printf("PackageRenderer::create_engine %d\n", __LINE__);
261 file->start_video_thread(video_write_length,
262 command->get_edl()->session->color_model,
263 preferences->processors > 1 ? 2 : 1,
265 //printf("PackageRenderer::create_engine %d\n", __LINE__);
270 video_device = new VideoDevice;
271 video_device->open_output(vconfig,
272 command->get_edl()->session->frame_rate,
273 command->get_edl()->session->output_w,
274 command->get_edl()->session->output_h,
275 mwindow->cwindow->gui->canvas,
277 // video_device->start_playback();
282 playable_tracks = new PlayableTracks(render_engine->get_edl(),
293 void PackageRenderer::do_audio()
295 //printf("PackageRenderer::do_audio %d\n", __LINE__);
297 if(asset->audio_data)
299 audio_output = file->get_audio_buffer();
300 // Zero unused channels in output vector
301 for(int i = 0; i < MAX_CHANNELS; i++)
303 audio_output_ptr[i] = (i < asset->channels) ?
310 // Call render engine
312 result = render_engine->arender->process_buffer(audio_output_ptr,
318 // Fix buffers for preroll
319 int64_t output_length = audio_read_length;
320 if(audio_preroll > 0)
322 if(audio_preroll >= output_length)
326 output_length -= audio_preroll;
327 for(int i = 0; i < MAX_CHANNELS; i++)
329 if(audio_output_ptr[i])
331 double *data = audio_output_ptr[i]->get_data();
332 for(int j = 0; j < output_length; j++)
334 data[j] = data[j + audio_read_length - output_length];
339 //printf("PackageRenderer::do_audio 4\n");
341 audio_preroll -= audio_read_length;
344 // Must perform writes even if 0 length so get_audio_buffer doesn't block
345 result |= file->write_audio_buffer(output_length);
348 audio_position += audio_read_length;
349 //printf("PackageRenderer::do_audio %d\n", __LINE__);
353 void PackageRenderer::do_video()
357 if(asset->video_data)
359 // get the absolute video position from the audio position
360 int64_t video_end = video_position + video_read_length;
362 if(video_end > package->video_end)
363 video_end = package->video_end;
365 while(video_position < video_end && !result)
367 // Try to copy the compressed frame directly from the input to output files
368 //printf("PackageRenderer::do_video 2 video_position=%ld\n", video_position);
369 if(direct_frame_copy(command->get_edl(),
374 // Direct frame copy failed.
375 // Switch back to background compression
376 if(direct_frame_copying)
379 file->start_video_thread(video_write_length,
380 command->get_edl()->session->color_model,
381 preferences->processors > 1 ? 2 : 1,
383 //printf("PackageRenderer::do_video %d %d\n", __LINE__, preferences->processors);
384 direct_frame_copying = 0;
387 // Try to use the rendering engine to write the frame.
388 // Get a buffer for background writing.
390 if(video_write_position == 0)
391 video_output = file->get_video_buffer();
393 if(debug) printf("PackageRenderer::do_video %d %p\n", __LINE__, video_output);
394 if(debug) printf("PackageRenderer::do_video %d %p\n", __LINE__, video_output[0]);
399 // Construct layered output buffer
400 video_output_ptr = video_output[0][video_write_position];
403 printf("PackageRenderer::do_video %d %p\n", __LINE__, video_output_ptr);
404 printf("PackageRenderer::do_video %d %d\n", __LINE__, result);
405 video_output_ptr->dump();
409 result = render_engine->vrender->process_buffer(
415 if(debug) printf("PackageRenderer::do_video %d %d\n", __LINE__, result);
419 video_device->output_visible())
421 // Vector for video device
422 VFrame *preview_output;
424 video_device->new_output_buffer(&preview_output,
425 command->get_edl()->session->color_model);
427 preview_output->copy_from(video_output_ptr);
428 video_device->write_buffer(preview_output,
432 if(debug) printf("PackageRenderer::do_video %d %d\n", __LINE__, result);
435 // Don't write to file
436 if(video_preroll && !result)
439 // Keep the write position at 0 until ready to write real frames
440 result = file->write_video_buffer(0);
441 video_write_position = 0;
446 // Set background rendering parameters
447 // Allow us to skip sections of the output file by setting the frame number.
448 // Used by background render and render farm.
449 //printf("PackageRenderer::do_video %d " _LD "\n", __LINE__, video_position);
450 video_output_ptr->set_number(video_position);
451 video_write_position++;
453 if(video_write_position >= video_write_length)
455 result = file->write_video_buffer(video_write_position);
456 //printf("PackageRenderer::do_video %d " _LD "\n", __LINE__, video_position);
457 // Update the brender map after writing the files.
458 if(package->use_brender)
460 //printf("PackageRenderer::do_video 10\n");
461 for(int i = 0; i < video_write_position && !result; i++)
463 result = set_video_map(video_position + 1 - video_write_position + i,
466 //printf("PackageRenderer::do_video 11 %d\n", result);
468 video_write_position = 0;
471 //printf("PackageRenderer::do_video %d " _LD "\n", __LINE__, video_position);
477 if(!result && get_result()) result = 1;
478 if(!result && progress_cancelled()) result = 1;
483 video_position += video_read_length;
488 void PackageRenderer::stop_engine()
490 delete render_engine;
491 delete playable_tracks;
495 void PackageRenderer::stop_output()
498 if(asset->audio_data)
501 file->stop_audio_thread();
504 if(asset->video_data)
506 delete compressed_output;
507 if(video_write_position)
508 file->write_video_buffer(video_write_position);
509 if(package->use_brender)
511 for(int i = 0; i < video_write_position && !error; i++)
513 error = set_video_map(video_position - video_write_position + i,
517 video_write_position = 0;
518 if(!error) file->stop_video_thread();
521 // video_device->stop_playback();
522 video_device->close_all();
529 void PackageRenderer::close_output()
532 mwindow->sighandler->pull_file(file);
535 asset->Garbage::remove_user();
538 // Aborts and returns 1 if an error is encountered.
539 int PackageRenderer::render_package(RenderPackage *package)
543 int samples_rendered = 0;
548 this->package = package;
551 // "PackageRenderer::render_package: audio s=" _LD " l=" _LD " video s=" _LD " l=" _LD "\n",
552 // package->audio_start,
553 // package->audio_end - package->audio_start,
554 // package->video_start,
555 // package->video_end - package->video_start);
557 if(debug) PRINT_TRACE
559 if( package->video_do ) default_asset->video_data = 1;
560 if( package->audio_do ) default_asset->audio_data = 1;
561 Render::check_asset(edl, *default_asset);
564 if(debug) PRINT_TRACE
566 if(!asset->video_data) video_done = 1;
567 if(!asset->audio_data) audio_done = 1;
569 // Create render engine
572 if(debug) PRINT_TRACE
574 if(debug) PRINT_TRACE
579 total_samples_rendered = 0;
580 while((!audio_done || !video_done) && !result)
582 int need_audio = 0, need_video = 0;
587 // Calculate lengths to process. Audio fragment is constant.
590 if(audio_position + audio_read_length >= package->audio_end)
593 audio_read_length = package->audio_end - audio_position;
596 samples_rendered = audio_read_length;
600 //printf("PackageRenderer::render_package 6 %d\n", samples_rendered);
606 // video_read_length = package->video_end - video_position;
607 // // Packetize video length so progress gets updated
608 // video_read_length = (int)MIN(asset->frame_rate, video_read_length);
609 // video_read_length = MAX(video_read_length, 30);
610 video_read_length = 1;
613 // Guide video with audio
615 video_read_length = Units::to_int64(
616 (double)(audio_position + audio_read_length) /
623 if(video_position + video_read_length >= package->video_end)
626 video_read_length = package->video_end - video_position;
629 // Calculate samples rendered for progress bar.
631 samples_rendered = Units::round((double)video_read_length /
637 if(debug) PRINT_TRACE
639 if(debug) printf("PackageRenderer::render_package 1 %d " _LD " " _LD "\n",
640 result, audio_read_length, video_read_length);
641 if(need_video && !result) do_video();
642 if(debug) printf("PackageRenderer::render_package %d %d %d\n",
643 __LINE__, result, samples_rendered);
644 if(need_audio && !result) do_audio();
647 if(debug) PRINT_TRACE
650 // Calculate frames per second for the renderfarm table.
651 total_samples_rendered += samples_rendered;
652 if(!video_done && timer->get_difference() > 30000)
654 frames_per_second = (double)total_samples_rendered *
657 ((double)timer->get_difference() / 1000);
659 set_progress(samples_rendered);
661 if(debug) PRINT_TRACE
667 if(!result && progress_cancelled()) result = 1;
668 if(debug) PRINT_TRACE
670 // printf("PackageRenderer::render_package 10 %d %d %d %d\n",
671 // audio_read_length, video_read_length, samples_rendered, result);
675 result = get_result();
679 frames_per_second = (double)(package->video_end - package->video_start) /
680 ((double)timer->get_difference() / 1000);
715 // Try to copy the compressed frame directly from the input to output files
716 // Return 1 on failure and 0 on success
717 int PackageRenderer::direct_frame_copy(EDL *edl,
718 int64_t &video_position,
722 Track *playable_track = 0;
723 Edit *playable_edit = 0;
725 //printf("Render::direct_frame_copy 1\n");
726 if(direct_copy_possible(edl,
732 // Switch to direct copying
733 if(!direct_frame_copying)
735 if(video_write_position)
737 error |= file->write_video_buffer(video_write_position);
738 video_write_position = 0;
740 file->stop_video_thread();
741 direct_frame_copying = 1;
743 //printf("Render::direct_frame_copy 2\n");
745 if(!package->use_brender)
746 error |= ((VEdit*)playable_edit)->read_frame(compressed_output,
755 if(!error && video_preroll > 0)
762 if(package->use_brender)
764 //printf("PackageRenderer::direct_frame_copy 1\n");
765 error = set_video_map(video_position, BRender::SCANNED);
766 //printf("PackageRenderer::direct_frame_copy 10 %d\n", error);
770 VFrame ***temp_output = new VFrame**[1];
771 temp_output[0] = new VFrame*[1];
772 temp_output[0][0] = compressed_output;
773 error = file->write_frames(temp_output, 1);
774 delete [] temp_output[0];
775 delete [] temp_output;
784 int PackageRenderer::direct_copy_possible(EDL *edl,
785 int64_t current_position,
786 Track* &playable_track, // The one track which is playable
787 Edit* &playable_edit, // The edit which is playing
788 File *file) // Output file
791 int total_playable_tracks = 0;
792 Track* current_track;
794 // Number of playable tracks must equal 1
795 for(current_track = edl->tracks->first;
796 current_track && result;
797 current_track = current_track->next)
799 if(current_track->data_type == TRACK_VIDEO)
801 if(playable_tracks->is_playable(current_track,
806 playable_track = current_track;
807 total_playable_tracks++;
812 //printf("Render::direct_copy_possible 1 %d\n", result);
813 if(total_playable_tracks != 1) result = 0;
814 //printf("Render::direct_copy_possible 2 %d\n", result);
816 // Edit must have a source file
817 // TODO: descend into nested EDL's
820 //printf("Render::direct_copy_possible 3 %d\n", result);
821 playable_edit = playable_track->edits->get_playable_edit(current_position, 1);
822 //printf("Render::direct_copy_possible 4 %d %p\n", result, playable_edit);
827 // Source file must be able to copy to destination file.
828 // Source file must be same size as project output.
831 if(!file->can_copy_from(playable_edit->asset,
832 current_position + playable_track->nudge,
833 edl->session->output_w,
834 edl->session->output_h))
837 //printf("Render::direct_copy_possible 6 %d\n", result);
839 // Test conditions mutual between vrender.C and this.
841 !playable_track->direct_copy_possible(current_position, PLAY_FORWARD, 1))
843 //printf("Render::direct_copy_possible 7 %d\n", result);
856 int PackageRenderer::get_master()
861 // Get result status from server
862 int PackageRenderer::get_result()
867 void PackageRenderer::set_result(int value)
871 void PackageRenderer::set_progress(int64_t value)
875 int PackageRenderer::set_video_map(int64_t position, int value)
880 int PackageRenderer::progress_cancelled()