batchrender cleanup, bd/dvd create upgrades, remote ctrl booby fix
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / packagingengine.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 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 "packagingengine.h"
23 #include "preferences.h"
24 #include "edlsession.h"
25 #include "asset.h"
26 #include "render.h"
27 #include "clip.h"
28
29
30 // Default packaging engine implementation, simply split the range up, and consider client's speed
31
32 PackagingEngineDefault::PackagingEngineDefault()
33 {
34         packages = 0;
35 }
36
37 PackagingEngineDefault::~PackagingEngineDefault()
38 {
39         if(packages)
40         {
41                 for(int i = 0; i < total_packages; i++)
42                         delete packages[i];
43                 delete [] packages;
44         }
45 }
46
47
48 int PackagingEngineDefault::create_packages_single_farm(EDL *edl,
49                 Preferences *preferences, Asset *default_asset,
50                 double total_start, double total_end)
51 {
52         this->total_start = total_start;
53         this->total_end = total_end;
54
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);
61         current_package = 0;
62         current_position = 0;
63
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;
70
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.
74
75         Render::get_starting_number(default_asset->path,
76                 current_number, number_start, total_digits, 3);
77
78         for( int i=0; i<total_allocated; ++i ) {
79                 RenderPackage *package = packages[i] = new RenderPackage;
80
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);
84                 current_number++;
85         }
86         return 0;
87 }
88
89 RenderPackage* PackagingEngineDefault::get_package_single_farm(double frames_per_second,
90                 int client_number, int use_local_rate)
91 {
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;
96
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))) ) {
101 // Last package
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;
107
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;
113                 }
114                 else {
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;
119                                 scaled_length = 1;
120                         }
121                         if( length < min_package_len )
122                                 length = min_package_len;
123                         double end_position = current_position + length;
124
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;
129                         }
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);
133                         }
134                         audio_position = result->audio_end;
135                         video_position = result->video_end;
136                         current_position = end_position;
137
138 // Package size is no longer touched between total_packages and total_allocated
139                         if( scaled_length && current_package < total_packages-1 ) {
140                                 double remaining =
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;
148                                 }
149                         }
150                 }
151
152                 current_package++;
153 //printf("Dispatcher::get_package 50 %lld %lld %lld %lld\n",
154 // result->audio_start, result->video_start, result->audio_end, result->video_end);
155         }
156         return result;
157 }
158
159 void PackagingEngineDefault::get_package_paths(ArrayList<char*> *path_list)
160 {
161         for( int i=0; i<total_allocated; ++i ) {
162                 path_list->append(strdup(packages[i]->path));
163         }
164         path_list->set_free();
165 }
166
167 int PackagingEngineDefault::get_asset_list(ArrayList<Indexable *> &idxbls)
168 {
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);
176         }
177         return current_package;
178 }
179
180 int64_t PackagingEngineDefault::get_progress_max()
181 {
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);
184 }
185
186 int PackagingEngineDefault::packages_are_done()
187 {
188         return 0;
189 }
190