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
22 #include "packagingengine.h"
23 #include "preferences.h"
24 #include "edlsession.h"
30 // Default packaging engine implementation, simply split the range up, and consider client's speed
32 PackagingEngineDefault::PackagingEngineDefault()
37 PackagingEngineDefault::~PackagingEngineDefault()
41 for(int i = 0; i < total_packages; i++)
48 int PackagingEngineDefault::create_packages_single_farm(EDL *edl,
49 Preferences *preferences, Asset *default_asset,
50 double total_start, double total_end)
52 this->total_start = total_start;
53 this->total_end = total_end;
55 this->preferences = preferences;
56 this->default_asset = default_asset;
57 audio_position = Units::to_int64(total_start * default_asset->sample_rate);
58 video_position = Units::to_int64(total_start * default_asset->frame_rate);
59 audio_end = Units::to_int64(total_end * default_asset->sample_rate);
60 video_end = Units::to_int64(total_end * default_asset->frame_rate);
64 double total_len = total_end - total_start;
65 total_packages = preferences->renderfarm_job_count;
66 total_allocated = total_packages + preferences->get_enabled_nodes();
67 packages = new RenderPackage*[total_allocated];
68 package_len = total_len / total_packages;
69 min_package_len = 2.0 / edl->session->frame_rate;
71 int current_number; // The number being injected into the filename.
72 int number_start; // Character in the filename path at which the number begins
73 int total_digits; // Total number of digits including padding the user specified.
75 Render::get_starting_number(default_asset->path,
76 current_number, number_start, total_digits, 3);
78 for( int i=0; i<total_allocated; ++i ) {
79 RenderPackage *package = packages[i] = new RenderPackage;
81 // Create file number differently if image file sequence
82 Render::create_filename(package->path, default_asset->path,
83 current_number, total_digits, number_start);
89 RenderPackage* PackagingEngineDefault::get_package_single_farm(double frames_per_second,
90 int client_number, int use_local_rate)
92 RenderPackage *result = 0;
93 float avg_frames_per_second = preferences->get_avg_rate(use_local_rate);
94 double length = package_len;
95 int scaled_length = 0;
97 if( (default_asset->audio_data &&
98 (audio_position < audio_end && !EQUIV(audio_position, audio_end))) ||
99 (default_asset->video_data &&
100 (video_position < video_end && !EQUIV(video_position, video_end))) ) {
102 result = packages[current_package];
103 result->audio_start = audio_position;
104 result->video_start = video_position;
105 result->video_do = default_asset->video_data;
106 result->audio_do = default_asset->audio_data;
108 if( current_package >= total_allocated-1 ) {
109 result->audio_end = audio_end;
110 result->video_end = video_end;
111 audio_position = result->audio_end;
112 video_position = result->video_end;
115 if( frames_per_second > 0 &&
116 !EQUIV(frames_per_second, 0) && !EQUIV(avg_frames_per_second, 0) ) {
117 // package size to fit the requestor.
118 length *= frames_per_second / avg_frames_per_second;
121 if( length < min_package_len )
122 length = min_package_len;
123 double end_position = current_position + length;
125 if( result->video_do ) {
126 int64_t video_end = end_position * default_asset->frame_rate;
127 result->video_end = MIN(this->video_end, video_end);
128 end_position = video_end / default_asset->frame_rate;
130 if( result->audio_do ) {
131 int64_t audio_end = end_position * default_asset->sample_rate;
132 result->audio_end = MIN(this->audio_end, audio_end);
134 audio_position = result->audio_end;
135 video_position = result->video_end;
136 current_position = end_position;
138 // Package size is no longer touched between total_packages and total_allocated
139 if( scaled_length && current_package < total_packages-1 ) {
141 result->audio_do ? (double)(audio_end - audio_position) /
142 default_asset->sample_rate :
143 result->video_do ? (double)(video_end - video_position) /
144 default_asset->frame_rate : 0;
145 if( remaining > 0 ) {
146 int jobs = total_packages - current_package;
147 package_len = remaining / jobs;
153 //printf("Dispatcher::get_package 50 %lld %lld %lld %lld\n",
154 // result->audio_start, result->video_start, result->audio_end, result->video_end);
159 void PackagingEngineDefault::get_package_paths(ArrayList<char*> *path_list)
161 for( int i=0; i<total_allocated; ++i ) {
162 path_list->append(strdup(packages[i]->path));
164 path_list->set_free();
167 int PackagingEngineDefault::get_asset_list(ArrayList<Indexable *> &idxbls)
169 for( int i=0; i<current_package; ++i ) {
170 Asset *asset = new Asset;
171 asset->copy_from(default_asset, 1);
172 strcpy(asset->path, packages[i]->path);
173 asset->video_length = packages[i]->video_end - packages[i]->video_start;
174 asset->audio_length = packages[i]->audio_end - packages[i]->audio_start;
175 idxbls.append(asset);
177 return current_package;
180 int64_t PackagingEngineDefault::get_progress_max()
182 return Units::to_int64(default_asset->sample_rate * (total_end - total_start)) +
183 Units::to_int64(preferences->render_preroll * 2 * default_asset->sample_rate);
186 int PackagingEngineDefault::packages_are_done()