Reported by Fedora team for gcc-13 and Andrew created patch here
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / packagedispatcher.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "asset.h"
23 #include "clip.h"
24 #include "confirmsave.h"
25 #include "edl.h"
26 #include "edlsession.h"
27 #include "labels.h"
28 #include "mainerror.h"
29 #include "mutex.h"
30 #include "mwindow.h"
31 #include "packagedispatcher.h"
32 #include "packagerenderer.h"
33 #include "preferences.h"
34 #include "render.h"
35 #include "file.h"
36
37
38
39 PackageDispatcher::PackageDispatcher()
40 {
41         packages = 0;
42         package_lock = new Mutex("PackageDispatcher::package_lock");
43         packaging_engine = 0;
44 }
45
46 PackageDispatcher::~PackageDispatcher()
47 {
48         if(packages)
49         {
50                 for(int i = 0; i < total_packages; i++)
51                         delete packages[i];
52                 delete [] packages;
53         }
54         if (packaging_engine)
55                 delete packaging_engine;
56         delete package_lock;
57 }
58
59 int PackageDispatcher::create_packages(MWindow *mwindow, EDL *edl,
60         Preferences *preferences, int strategy, Asset *default_asset,
61         double total_start, double total_end, int test_overwrite)
62 {
63         Label *label;
64         int result = 0;
65
66         this->mwindow = mwindow;
67         this->edl = edl;
68         this->preferences = preferences;
69         this->strategy = strategy;
70         this->default_asset = default_asset;
71         this->total_start = total_start;
72         this->total_end = total_end;
73
74         nodes = preferences->get_enabled_nodes();
75         audio_position = Units::to_int64(total_start * default_asset->sample_rate);
76         video_position = Units::to_int64(total_start * default_asset->frame_rate);
77         audio_end = Units::to_int64(total_end * default_asset->sample_rate);
78         video_end = Units::to_int64(total_end * default_asset->frame_rate);
79         current_package = 0;
80
81 // sleep(1);
82 // printf("PackageDispatcher::create_packages 1 %d %f %f\n",
83 // video_end,
84 // total_end,
85 // default_asset->frame_rate);
86
87         switch( strategy ) {
88         case SINGLE_PASS:
89                 total_len = this->total_end - this->total_start;
90                 package_len = total_len;
91                 min_package_len = total_len;
92                 total_packages = 1;
93                 total_allocated = 1;
94                 packages = new RenderPackage*[total_allocated];
95                 packages[0] = new RenderPackage;
96                 packages[0]->audio_start = audio_position;
97                 packages[0]->audio_end = audio_end;
98                 packages[0]->video_start = video_position;
99                 packages[0]->video_end = video_end;
100                 packages[0]->audio_do = default_asset->audio_data;
101                 packages[0]->video_do = default_asset->video_data;
102                 strcpy(packages[0]->path, default_asset->path);
103                 break;
104         case SINGLE_PASS_FARM:
105                 packaging_engine = (PackagingEngine*)new PackagingEngineDefault();
106                 packaging_engine->create_packages_single_farm(edl, preferences,
107                                 default_asset, total_start, total_end);
108                 break;
109         case FILE_PER_LABEL:
110         case FILE_PER_LABEL_FARM:
111                 label = edl->labels->first;
112                 total_packages = 0;
113                 packages = new RenderPackage*[edl->labels->total() + 2];
114                 if( !label ) {
115                         eprintf(_("Render file per label and no labels\n"));
116                         result = 1;
117                         break;
118                 }
119
120                 Render::get_starting_number(default_asset->path,
121                         current_number, number_start, total_digits, 3);
122
123                 while( audio_position < audio_end ) {
124                         RenderPackage *package = new RenderPackage;
125                         packages[total_packages++] = package;
126                         package->audio_start = audio_position;
127                         package->video_start = video_position;
128                         package->audio_do = default_asset->audio_data;
129                         package->video_do = default_asset->video_data;
130
131                         while( label &&
132                                 (label->position < (double)audio_position / default_asset->sample_rate ||
133                                 EQUIV(label->position, (double)audio_position / default_asset->sample_rate)) ) {
134                                 label = label->next;
135                         }
136
137                         if( !label ) {
138                                 package->audio_end = Units::to_int64(total_end * default_asset->sample_rate);
139                                 package->video_end = Units::to_int64(total_end * default_asset->frame_rate);
140                         }
141                         else {
142                                 package->audio_end = Units::to_int64(label->position * default_asset->sample_rate);
143                                 package->video_end = Units::to_int64(label->position * default_asset->frame_rate);
144                         }
145
146                         if( package->audio_end > audio_end ) {
147                                 package->audio_end = audio_end;
148                         }
149
150                         if( package->video_end > video_end ) {
151                                 package->video_end = video_end;
152                         }
153
154                         audio_position = package->audio_end;
155                         video_position = package->video_end;
156
157 // Create file number differently if image file sequence
158                         Render::create_filename(package->path, default_asset->path,
159                                 current_number++, total_digits, number_start);
160                 }
161
162                 total_allocated = total_packages;
163                 break;
164         case BRENDER_FARM:
165                 total_len = this->total_end - this->total_start;
166
167 // Create packages as they're requested.
168                 total_packages = 0;
169                 total_allocated = 0;
170                 packages = 0;
171
172                 Render::get_starting_number(default_asset->path,
173                         current_number, number_start, total_digits, 6);
174
175 // Master node only
176                 if( preferences->renderfarm_nodes.total == 1 ) {
177                         package_len = total_len;
178                         min_package_len = total_len;
179                 }
180                 else {
181                         package_len = preferences->brender_fragment /
182                                 edl->session->frame_rate;
183                         min_package_len = 1.0 / edl->session->frame_rate;
184                 }
185                 break;
186         }
187
188 // Test existence of every output file.
189 // Only if this isn't a background render or non interactive.
190         if( strategy != BRENDER_FARM && test_overwrite && mwindow ) {
191                 ArrayList<char*> paths;
192                 paths.set_array_delete();
193                 get_package_paths(&paths);
194                 result = ConfirmSave::test_files(mwindow, &paths);
195                 paths.remove_all_objects();
196         }
197
198         return result;
199 }
200
201 void PackageDispatcher::get_package_paths(ArrayList<char*> *path_list)
202 {
203                 if (strategy == SINGLE_PASS_FARM)
204                         packaging_engine->get_package_paths(path_list);
205                 else {
206                         for( int i=0; i<total_allocated; ++i )
207                                 path_list->append(cstrdup(packages[i]->path));
208                 }
209
210 }
211
212 RenderPackage* PackageDispatcher::get_package(double frames_per_second,
213                 int client_number, int use_local_rate)
214 {
215         package_lock->lock("PackageDispatcher::get_package");
216
217 // Store new frames per second for the node
218         if( !EQUIV(frames_per_second, 0) ) {
219                 preferences->set_rate(frames_per_second, client_number);
220                 if(mwindow) mwindow->preferences->copy_rates_from(preferences);
221         }
222         else {
223 // Use previous frames per second
224                 frames_per_second = preferences->get_rate(client_number);
225         }
226
227         float avg_frames_per_second = preferences->get_avg_rate(use_local_rate);
228
229         RenderPackage *result = 0;
230         switch( strategy ) {
231 //printf("PackageDispatcher::get_package 1 %d\n", strategy);
232         case SINGLE_PASS:
233         case FILE_PER_LABEL:
234         case FILE_PER_LABEL_FARM:
235                 if( current_package < total_packages )
236                         result = packages[current_package++];
237                 break;
238         case SINGLE_PASS_FARM:
239                 result = packaging_engine->get_package_single_farm(frames_per_second,
240                                         client_number, use_local_rate);
241                 break;
242         case BRENDER_FARM:
243 //printf("Dispatcher::get_package 1 %d %d\n", video_position, video_end);
244                 if( video_position < video_end ) {
245 // Allocate new packages
246                         if( total_packages == 0 ) {
247                                 total_allocated = 256;
248                                 packages = new RenderPackage*[total_allocated];
249                         }
250                         else if( total_packages >= total_allocated ) {
251                                 RenderPackage **old_packages = packages;
252                                 total_allocated *= 2;
253                                 packages = new RenderPackage*[total_allocated];
254                                 memcpy(packages,
255                                         old_packages,
256                                         total_packages * sizeof(RenderPackage*));
257                                 delete [] old_packages;
258                         }
259
260 // Calculate package.
261                         result = packages[total_packages] = new RenderPackage;
262                         double scaled_len;
263
264 // No load balancing data exists
265                         if( EQUIV(frames_per_second, 0) ||
266                              EQUIV(avg_frames_per_second, 0)) {
267                                 scaled_len = package_len;
268                         }
269                         else {
270 // Load balancing data exists
271                                 scaled_len = package_len *
272                                         frames_per_second / avg_frames_per_second;
273                         }
274
275                         scaled_len = MAX(scaled_len, min_package_len);
276
277 // Always an image file sequence
278                         result->audio_start = audio_position;
279                         result->video_start = video_position;
280                         result->audio_end = result->audio_start +
281                                 Units::to_int64(scaled_len * default_asset->sample_rate);
282                         if( result->audio_end > audio_end ) result->audio_end = audio_end;
283                         result->video_end = result->video_start +
284                                 Units::to_int64(scaled_len * default_asset->frame_rate);
285                         if( result->video_end > video_end ) result->video_end = video_end;
286                         if( result->video_end == result->video_start ) result->video_end++;
287                         audio_position = result->audio_end;
288                         video_position = result->video_end;
289                         result->audio_do = default_asset->audio_data;
290                         result->video_do = default_asset->video_data;
291
292
293 // The frame numbers are read from the vframe objects themselves.
294                         Render::create_filename(result->path, default_asset->path,
295                                         0, total_digits, number_start);
296 //printf("PackageDispatcher::get_package 2 %s\n", result->path);
297                         ++current_number;
298                         ++total_packages;
299                         ++current_package;
300                 }
301                 break;
302         }
303
304         package_lock->unlock();
305         return result;
306 }
307
308
309 int PackageDispatcher::get_asset_list(ArrayList<Indexable*> &idxbls)
310 {
311         if( strategy == SINGLE_PASS_FARM )
312                 return packaging_engine->get_asset_list(idxbls);
313         for( int i=0; i<current_package; ++i ) {
314                 Asset *asset = new Asset;
315                 asset->copy_from(default_asset, 1);
316                 strcpy(asset->path, packages[i]->path);
317                 asset->video_length = packages[i]->video_end - packages[i]->video_start;
318                 asset->audio_length = packages[i]->audio_end - packages[i]->audio_start;
319                 idxbls.append(asset);
320         }
321         return current_package;
322 }
323
324 int64_t PackageDispatcher::get_progress_max()
325 {
326         return strategy == SINGLE_PASS_FARM ?
327                 packaging_engine->get_progress_max() :
328                 Units::to_int64(default_asset->sample_rate * (total_end - total_start)) +
329                         Units::to_int64(preferences->render_preroll *
330                                 total_allocated * default_asset->sample_rate);
331 }
332
333 RenderPackage *PackageDispatcher::get_package(int number)
334 {
335         return packages[number];
336 }
337
338 int PackageDispatcher::get_total_packages()
339 {
340         return total_allocated;
341 }
342
343 int PackageDispatcher::packages_are_done()
344 {
345         return packaging_engine ? packaging_engine->packages_are_done() : 0;
346 }
347