r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / cinelerra / commonrender.C
blob734f13b5784a47e949f383c530ade245c2186be2
1 #include "auto.h"
2 #include "cache.h"
3 #include "commonrender.h"
4 #include "edl.h"
5 #include "edlsession.h"
6 #include "intautos.h"
7 #include "localsession.h"
8 #include "mainsession.h"
9 #include "module.h"
10 #include "mwindow.h"
11 #include "patchbay.h"
12 #include "patch.h"
13 #include "playabletracks.h"
14 #include "preferences.h"
15 #include "renderengine.h"
16 #include "track.h"
17 #include "tracks.h"
18 #include "transportque.h"
19 #include "virtualconsole.h"
21 CommonRender::CommonRender(RenderEngine *renderengine)
22  : Thread()
24         this->renderengine = renderengine;
25         reset_parameters();
28 CommonRender::~CommonRender()
30         delete_vconsole();
31         if(modules)
32         {
33                 for(int i = 0; i < total_modules; i++)
34                         delete modules[i];
35                 delete [] modules;
36         }
39 void CommonRender::reset_parameters()
41         total_modules = 0;
42         set_synchronous(1);
43         modules = 0;
44         vconsole = 0;
45         done = 0;
46         interrupt = 0;
47         last_playback = 0;
48         asynchronous = 0;
49         restart_plugins = 0;
52 void CommonRender::arm_command()
54         int64_t temp_length = 1;
56 //printf("CommonRender::arm_command 1 %f\n", renderengine->command->playbackstart);
57         current_position = tounits(renderengine->command->playbackstart, 0);
58 //printf("CommonRender::arm_command 2 %d\n", renderengine->command->playbackstart);
60         init_output_buffers();
62 //printf("CommonRender::arm_command 3 %d\n", renderengine->command->playbackstart);
63         last_playback = 0;
64         if(test_reconfigure(current_position, temp_length))
65         {
66 //printf("CommonRender::arm_command 3.1 %d\n", renderengine->command->playbackstart);
67                 restart_playback();
68         }
69         else
70         {
71                 vconsole->start_playback();
72         }
73 //printf("CommonRender::arm_command 4 %d\n", renderengine->command->playbackstart);
75         done = 0;
76         interrupt = 0;
77         last_playback = 0;
78         restart_plugins = 0;
80 //printf("CommonRender::arm_command 5\n");
85 void CommonRender::create_modules()
87 //printf("CommonRender::create_modules 1\n");
88 // Create a module for every track, playable or not
89         Track *current = renderengine->edl->tracks->first;
90         int module = 0;
92 //printf("CommonRender::create_modules 1\n");
93         if(!modules)
94         {
95                 total_modules = get_total_tracks();
96                 modules = new Module*[total_modules];
97 //printf("CommonRender::create_modules 1 %d\n", total_modules);
99                 for(module = 0; module < total_modules && current; current = NEXT)
100                 {
101 //printf("CommonRender::create_modules 2 %d %p\n", total_modules, current);
102                         if(current->data_type == data_type)
103                         {
104                                 modules[module] = new_module(current);
105                                 modules[module]->create_objects();
106                                 module++;
107                         }
108                 }
109         }
110         else
111 // Update changes in plugins for existing modules
112         {
113 //printf("CommonRender::create_modules 3 %d\n", total_modules);
114                 for(module = 0; module < total_modules; module++)
115                 {
116                         modules[module]->create_objects();
117                 }
118 //printf("CommonRender::create_modules 4\n");
119         }
122 void CommonRender::start_plugins()
124 //printf("CommonRender::start_plugins 1\n");
125 // Only start if virtual console was created
126         if(restart_plugins)
127         {
128                 for(int i = 0; i < total_modules; i++)
129                 {
130                         modules[i]->render_init();
131                 }
132         }
133 //printf("CommonRender::start_plugins 2\n");
136 int CommonRender::test_reconfigure(int64_t position, int64_t &length)
138         if(!vconsole) return 1;
139         if(!modules) return 1;
140         
141         return vconsole->test_reconfigure(position, length, last_playback);
145 void CommonRender::build_virtual_console()
147 // Create new virtual console object
148 //printf("CommonRender::build_virtual_console 1 %p\n", vconsole);
149         if(!vconsole)
150         {
151                 vconsole = new_vconsole_object();
152 //printf("CommonRender::build_virtual_console 2\n");
153         }
155 // Create nodes
156         vconsole->create_objects();
157 //printf("CommonRender::build_virtual_console 3\n");
160 void CommonRender::start_command()
162         if(renderengine->command->realtime)
163         {
164                 start_lock.lock();
165                 Thread::start();
166                 start_lock.lock();
167                 start_lock.unlock();
168         }
171 int CommonRender::restart_playback()
173         delete_vconsole();
174         create_modules();
175         build_virtual_console();
176         vconsole->start_playback();
177         start_plugins();
179         done = 0;
180         interrupt = 0;
181         last_playback = 0;
182         restart_plugins = 0;
183         return 0;
186 void CommonRender::delete_vconsole()
188         if(vconsole) delete vconsole;
189         vconsole = 0;
192 int CommonRender::get_boundaries(int64_t &current_render_length)
194         int64_t loop_end = tounits(renderengine->edl->local_session->loop_end, 1);
195         int64_t loop_start = tounits(renderengine->edl->local_session->loop_start, 0);
196         int64_t start_position = tounits(renderengine->command->start_position, 0);
197         int64_t end_position = tounits(renderengine->command->end_position, 1);
200 //printf("CommonRender::get_boundaries 1 %d %d %d %d\n", 
201 //current_render_length, 
202 //current_position, 
203 //start_position,
204 //end_position);
205 // test absolute boundaries if no loop and not infinite
206         if(renderengine->command->single_frame() || 
207                 (!renderengine->edl->local_session->loop_playback && 
208                 !renderengine->command->infinite))
209         {
210 //printf("CommonRender::get_boundaries 1.1 %d\n", current_render_length);
211                 if(renderengine->command->get_direction() == PLAY_FORWARD)
212                 {
213 //printf("CommonRender::get_boundaries 1.2 %d\n", current_render_length);
214                         if(current_position + current_render_length >= end_position)
215                         {
216                                 last_playback = 1;
217                                 current_render_length = end_position - current_position;
218 //printf("CommonRender::get_boundaries 1.3 %d\n", current_render_length);
219                         }
220                 }
221 // reverse playback
222                 else               
223                 {
224 //printf("CommonRender::get_boundaries 1.4 %d\n", current_render_length);
225                         if(current_position - current_render_length <= start_position)
226                         {
227                                 last_playback = 1;
228                                 current_render_length = current_position - start_position;
229 //printf("CommonRender::get_boundaries 1.5 %d\n", current_render_length);
230                         }
231                 }
232         }
233 //printf("CommonRender::get_boundaries 2 %d\n", current_render_length);
235 // test against loop boundaries
236         if(!renderengine->command->single_frame() &&
237                 renderengine->edl->local_session->loop_playback && 
238                 !renderengine->command->infinite)
239         {
241                 if(renderengine->command->get_direction() == PLAY_FORWARD)
242                 {
243                         int64_t segment_end = current_position + current_render_length;
244                         if(segment_end > loop_end)
245                         {
246                                 current_render_length = loop_end - current_position;
247                         }
248                 }
249                 else
250                 {
251                         int64_t segment_end = current_position - current_render_length;
252                         if(segment_end < loop_start)
253                         {
254                                 current_render_length = current_position - loop_start;
255                         }
256                 }
257         }
258         
259 //printf("CommonRender::get_boundaries 3 %d\n", current_render_length);
260         if(renderengine->command->single_frame())
261                 current_render_length = 1;
263 //printf("CommonRender::get_boundaries 4 %d\n", current_render_length);
264 //sleep(1);
265         return 0;
268 void CommonRender::run()
270         start_lock.unlock();
293 CommonRender::CommonRender(MWindow *mwindow, RenderEngine *renderengine)
294  : Thread()
296         this->mwindow = mwindow;
297         this->renderengine = renderengine;
298         current_position = 0;
299         interrupt = 0;
300         done = 0;
301         last_playback = 0;
302         vconsole = 0;
303         asynchronous = 1;
307 int CommonRender::wait_for_completion()
309         join();
316 int CommonRender::advance_position(int64_t current_render_length)
318         int64_t loop_end = tounits(renderengine->edl->local_session->loop_end, 1);
319         int64_t loop_start = tounits(renderengine->edl->local_session->loop_start, 0);
321 // advance the playback position
322         if(renderengine->command->get_direction() == PLAY_REVERSE)
323                 current_position -= current_render_length;
324         else
325                 current_position += current_render_length;
327 //printf("CommonRender::advance_position 1 %d\n", current_render_length);
328 // test loop again
329         if(renderengine->edl->local_session->loop_playback && 
330                 !renderengine->command->infinite)
331         {
332                 if(renderengine->command->get_direction() == PLAY_REVERSE)
333                 {
334                         if(current_position <= loop_start)
335                                 current_position = loop_end;
336                 }
337                 else
338                 {
339                         if(current_position >= loop_end)
340                                 current_position = loop_start + (current_position - loop_end);
341                 }
342         }
343         return 0;
346 int64_t CommonRender::tounits(double position, int round)
348         return (int64_t)position;
351 double CommonRender::fromunits(int64_t position)
353         return (double)position;