Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / cinelerra / packagingengine.C
blob42de5ce8643fd903c9967cadc46c636b683d7805
1 #include "packagingengine.h"
2 #include "preferences.h"
3 #include "edlsession.h"
4 #include "asset.h"
5 #include "render.h"
6 #include "clip.h"
9 // Default packaging engine implementation, simply split the range up, and consider client's speed
11 PackagingEngineDefault::PackagingEngineDefault()
13         packages = 0;
16 PackagingEngineDefault::~PackagingEngineDefault()
18         if(packages)
19         {
20                 for(int i = 0; i < total_packages; i++)
21                         delete packages[i];
22                 delete [] packages;
23         }
27 int PackagingEngineDefault::create_packages_single_farm(
28                 EDL *edl,
29                 Preferences *preferences,
30                 Asset_GC default_asset, 
31                 double total_start, 
32                 double total_end)
34         this->total_start = total_start;
35         this->total_end = total_end;
37         this->preferences = preferences;
38         this->default_asset = default_asset;
39         audio_position = Units::to_int64(total_start * default_asset->sample_rate);
40         video_position = Units::to_int64(total_start * default_asset->frame_rate);
41         audio_end = Units::to_int64(total_end * default_asset->sample_rate);
42         video_end = Units::to_int64(total_end * default_asset->frame_rate);
43         current_package = 0;
45         double total_len = total_end - total_start;
46         total_packages = preferences->renderfarm_job_count;
47         total_allocated = total_packages + preferences->get_enabled_nodes();
48         packages = new RenderPackage*[total_allocated];
49         package_len = total_len / total_packages;
50         min_package_len = 2.0 / edl->session->frame_rate;
53 //printf("PackageDispatcher::create_packages: %f / %d = %f\n", total_len, total_packages, package_len);
54         int current_number;    // The number being injected into the filename.
55         int number_start;      // Character in the filename path at which the number begins
56         int total_digits;      // Total number of digits including padding the user specified.
58         Render::get_starting_number(default_asset->path, 
59                 current_number,
60                 number_start, 
61                 total_digits,
62                 3);
64         for(int i = 0; i < total_allocated; i++)
65         {
66                 RenderPackage *package = packages[i] = new RenderPackage;
68 // Create file number differently if image file sequence
69                 Render::create_filename(package->path, 
70                         default_asset->path, 
71                         current_number,
72                         total_digits,
73                         number_start);
74                 current_number++;
75         }
79 RenderPackage* PackagingEngineDefault::get_package_single_farm(double frames_per_second, 
80                 int client_number,
81                 int use_local_rate)
84 //printf("PackageDispatcher::get_package %ld %ld %ld %ld\n", audio_position, video_position, audio_end, video_end);
86                 RenderPackage *result = 0;
87                 float avg_frames_per_second = preferences->get_avg_rate(use_local_rate);
89                 if(audio_position < audio_end ||
90                         video_position < video_end)
91                 {
92 // Last package
93                         double scaled_len;
94                         result = packages[current_package];
95                         result->audio_start = audio_position;
96                         result->video_start = video_position;
97                         result->video_do = default_asset->video_data;
98                         result->audio_do = default_asset->audio_data;
100                         if(current_package >= total_allocated - 1)
101                         {
102                                 result->audio_end = audio_end;
103                                 result->video_end = video_end;
104                                 audio_position = result->audio_end;
105                                 video_position = result->video_end;
106                         }
107                         else
108 // No useful speed data.  May get infinity for real fast jobs.
109                         if(frames_per_second > 0x7fffff || frames_per_second < 0 ||
110                                 EQUIV(frames_per_second, 0) || 
111                                 EQUIV(avg_frames_per_second, 0))
112                         {
113                                 scaled_len = MAX(package_len, min_package_len);
115                                 result->audio_end = audio_position + 
116                                         Units::round(scaled_len * default_asset->sample_rate);
117                                 result->video_end = video_position + 
118                                         Units::round(scaled_len * default_asset->frame_rate);
120 // If we get here without any useful speed data render the whole thing.
121                                 if(current_package >= total_packages - 1)
122                                 {
123                                         result->audio_end = audio_end;
124                                         result->video_end = video_end;
125                                 }
126                                 else
127                                 {
128                                         result->audio_end = MIN(audio_end, result->audio_end);
129                                         result->video_end = MIN(video_end, result->video_end);
130                                 }
132                                 audio_position = result->audio_end;
133                                 video_position = result->video_end;
134                         }
135                         else
136 // Useful speed data and future packages exist.  Scale the 
137 // package size to fit the requestor.
138                         {
139                                 scaled_len = package_len * 
140                                         frames_per_second / 
141                                         avg_frames_per_second;
142                                 scaled_len = MAX(scaled_len, min_package_len);
144                                 result->audio_end = result->audio_start + 
145                                         Units::to_int64(scaled_len * default_asset->sample_rate);
146                                 result->video_end = result->video_start +
147                                         Units::to_int64(scaled_len * default_asset->frame_rate);
149                                 result->audio_end = MIN(audio_end, result->audio_end);
150                                 result->video_end = MIN(video_end, result->video_end);
152                                 audio_position = result->audio_end;
153                                 video_position = result->video_end;
155 // Package size is no longer touched between total_packages and total_allocated
156                                 if(current_package < total_packages - 1)
157                                 {
158                                         package_len = (double)(audio_end - audio_position) / 
159                                                 (double)default_asset->sample_rate /
160                                                 (double)(total_packages - current_package);
161                                 }
163                         }
165                         current_package++;
166 //printf("Dispatcher::get_package 50 %lld %lld %lld %lld\n", 
167 //result->audio_start, 
168 //result->video_start, 
169 //result->audio_end, 
170 //result->video_end);
171                 }
172                 return result;
176 void PackagingEngineDefault::get_package_paths(ArrayList<char*> *path_list)
178         for(int i = 0; i < total_allocated; i++)
179         {
180                 path_list->append(strdup(packages[i]->path));
181         }
182         path_list->set_free();
185 int64_t PackagingEngineDefault::get_progress_max()
187         return Units::to_int64(default_asset->sample_rate * 
188                         (total_end - total_start)) +
189                 Units::to_int64(preferences->render_preroll * 
190                         2 * 
191                         default_asset->sample_rate);
194 int PackagingEngineDefault::packages_are_done()
196         return 0;
203 //      Local Variables:
204 //      mode: C++
205 //      c-file-style: "linux"
206 //      End: