Credit Andrew - improve in-tree documentation
[goodguy/cinelerra.git] / cinelerra / virtualvnode.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 "asset.h"
23 #include "automation.h"
24 #include "bcsignals.h"
25 #include "clip.h"
26 #include "edits.h"
27 #include "edl.h"
28 #include "edlsession.h"
29 #include "fadeengine.h"
30 #include "floatauto.h"
31 #include "floatautos.h"
32 #include "intauto.h"
33 #include "intautos.h"
34 #include "maskauto.h"
35 #include "maskautos.h"
36 #include "maskengine.h"
37 #include "mwindow.h"
38 #include "module.h"
39 #include "overlayframe.h"
40 #include "playabletracks.h"
41 #include "plugin.h"
42 #include "preferences.h"
43 #include "renderengine.h"
44 #include "transition.h"
45 #include "transportque.h"
46 #include "vattachmentpoint.h"
47 #include "vdevicex11.h"
48 #include "vframe.h"
49 #include "videodevice.h"
50 #include "virtualvconsole.h"
51 #include "virtualvnode.h"
52 #include "vmodule.h"
53 #include "vrender.h"
54 #include "vtrack.h"
55
56 #include <string.h>
57
58
59 VirtualVNode::VirtualVNode(RenderEngine *renderengine,
60                 VirtualConsole *vconsole, Module *real_module,
61                 Plugin *real_plugin, Track *track,
62                 VirtualNode *parent_node)
63  : VirtualNode(renderengine, vconsole, real_module, real_plugin,
64                 track, parent_node)
65 {
66         //VRender *vrender = ((VirtualVConsole*)vconsole)->vrender;
67         fader = new FadeEngine(renderengine->preferences->processors);
68         masker = new MaskEngine(renderengine->preferences->processors);
69         alpha = 1;
70 }
71
72 VirtualVNode::~VirtualVNode()
73 {
74         delete fader;
75         delete masker;
76 }
77
78 VirtualNode* VirtualVNode::create_module(Plugin *real_plugin,
79                                                         Module *real_module,
80                                                         Track *track)
81 {
82         return new VirtualVNode(renderengine, vconsole,
83                 real_module, 0, track, this);
84 }
85
86
87 VirtualNode* VirtualVNode::create_plugin(Plugin *real_plugin)
88 {
89         return new VirtualVNode(renderengine, vconsole,
90                 0, real_plugin, track, this);
91 }
92
93 int VirtualVNode::read_data(VFrame *output_temp,
94         int64_t start_position, double frame_rate, int use_opengl)
95 {
96         VirtualNode *previous_plugin = 0;
97         int result = 0;
98
99         if(!output_temp)
100                 printf("VirtualVNode::read_data output_temp=%p\n", output_temp);
101
102         if(vconsole->debug_tree)
103                 printf("  VirtualVNode::read_data position=%jd rate=%f title=%s opengl=%d\n",
104                         start_position, frame_rate, track->title, use_opengl);
105
106 // If there is a parent module but the parent module has no data source,
107 // use our own data source.
108 // Current edit in parent track
109         VEdit *parent_edit = 0;
110         if(parent_node && parent_node->track && renderengine)
111         {
112                 double edl_rate = renderengine->get_edl()->session->frame_rate;
113                 int64_t start_position_project = (int64_t)(start_position *
114                         edl_rate / frame_rate + 0.5);
115                 parent_edit = (VEdit*)parent_node->track->edits->editof(start_position_project,
116                         renderengine->command->get_direction(), 0);
117         }
118
119
120 // This is a plugin on parent module with a preceeding effect.
121 // Get data from preceeding effect on parent module.
122         if(parent_node && (previous_plugin = parent_node->get_previous_plugin(this)))
123         {
124                 result = ((VirtualVNode*)previous_plugin)->render(output_temp,
125                         start_position, frame_rate, use_opengl);
126         }
127         else
128 // The current node is the first plugin on parent module.
129 // The parent module has an edit to read from or the current node
130 // has no source to read from.
131 // Read data from parent module
132         if(parent_node && (parent_edit || !real_module))
133         {
134                 result = ((VirtualVNode*)parent_node)->read_data(output_temp,
135                         start_position, frame_rate, use_opengl);
136         }
137         else
138         if(real_module)
139         {
140 // This is the first node in the tree
141                 result = ((VModule*)real_module)->render(output_temp,
142                         start_position, renderengine->command->get_direction(),
143                         frame_rate, 0, vconsole->debug_tree, use_opengl);
144         }
145
146         return result;
147 }
148
149
150 int VirtualVNode::render(VFrame *output_temp,
151         int64_t start_position,
152         double frame_rate,
153         int use_opengl)
154 {
155         VRender *vrender = ((VirtualVConsole*)vconsole)->vrender;
156         if(real_module)
157         {
158                 render_as_module(vrender->video_out, output_temp,
159                         start_position, frame_rate, use_opengl);
160         }
161         else
162         if(real_plugin)
163         {
164                 render_as_plugin(output_temp,
165                         start_position, frame_rate, use_opengl);
166         }
167         return 0;
168 }
169
170 void VirtualVNode::render_as_plugin(VFrame *output_temp,
171         int64_t start_position,
172         double frame_rate,
173         int use_opengl)
174 {
175         if(!attachment || !real_plugin || !real_plugin->on) return;
176
177
178         if(vconsole->debug_tree)
179                 printf("  VirtualVNode::render_as_plugin title=%s use_opengl=%d\n",
180                         track->title, use_opengl);
181
182         ((VAttachmentPoint*)attachment)->render(
183                 output_temp, plugin_buffer_number, start_position,
184                 frame_rate, vconsole->debug_tree, use_opengl);
185 }
186
187
188 int VirtualVNode::render_as_module(VFrame *video_out,
189         VFrame *output_temp,
190         int64_t start_position,
191         double frame_rate,
192         int use_opengl)
193 {
194
195         int direction = renderengine->command->get_direction();
196         double edl_rate = renderengine->get_edl()->session->frame_rate;
197
198
199
200
201
202 // Get position relative to project, compensated for direction
203         int64_t start_position_project = (int64_t)(start_position *
204                 edl_rate /
205                 frame_rate);
206         if(direction == PLAY_REVERSE && start_position_project > 0 )
207                 start_position_project--;
208
209
210 // speed curve
211         if(track->has_speed())
212         {
213 // integrate position from start of track.  1/speed is duration of each frame
214
215         }
216
217         if(vconsole->debug_tree)
218                 printf("  VirtualVNode::render_as_module title=%s use_opengl=%d video_out=%p output_temp=%p\n",
219                         track->title, use_opengl, video_out, output_temp);
220
221         output_temp->push_next_effect("VirtualVNode::render_as_module");
222
223 // Process last subnode.  This propogates up the chain of subnodes and finishes
224 // the chain.
225         if(subnodes.total)
226         {
227                 VirtualVNode *node = (VirtualVNode*)subnodes.values[subnodes.total - 1];
228                 node->render(output_temp, start_position, frame_rate, use_opengl);
229         }
230         else
231 // Read data from previous entity
232         {
233                 read_data(output_temp, start_position, frame_rate, use_opengl);
234         }
235
236         output_temp->pop_next_effect();
237
238         render_fade(output_temp, start_position,
239                 frame_rate, track->automation->autos[AUTOMATION_FADE],
240                 direction, use_opengl);
241
242         render_mask(output_temp, start_position_project, frame_rate, use_opengl);
243
244
245 // overlay on the final output
246 // Get mute status
247         int mute_constant;
248         int mute_fragment = 1;
249
250
251 // Is frame muted?
252         get_mute_fragment(start_position, mute_constant, mute_fragment,
253                         (Autos*)((VTrack*)track)->automation->autos[AUTOMATION_MUTE],
254                         direction, 0);
255
256         if(!mute_constant)
257         {
258 // Frame is playable
259                 render_projector(output_temp, video_out, start_position,
260                         frame_rate, use_opengl);
261         }
262
263         output_temp->push_prev_effect("VirtualVNode::render_as_module");
264 //printf("VirtualVNode::render_as_module\n");
265 //output_temp->dump_stacks();
266
267         return 0;
268 }
269
270 #define EPSILON 1e-6
271 int VirtualVNode::render_fade(VFrame *output,
272 // start of input fragment in project if forward / end of input fragment if reverse
273 // relative to requested frame rate
274                         int64_t start_position, double frame_rate,
275                         Autos *autos, int direction, int use_opengl)
276 {
277         FloatAuto *previous = 0;
278         FloatAuto *next = 0;
279         double edl_rate = renderengine->get_edl()->session->frame_rate;
280         int64_t start_position_project =
281                 (int64_t)(start_position * edl_rate/frame_rate + EPSILON);
282
283         if(vconsole->debug_tree)
284                 printf("  VirtualVNode::render_fade title=%s\n", track->title);
285
286         double intercept = ((FloatAutos*)autos)->get_value(start_position_project,
287                 direction, previous, next);
288
289         CLAMP(intercept, 0, 100);
290
291
292 #if 0
293 // Can't use overlay here because overlayer blends the frame with itself.
294 // The fade engine can compensate for lack of alpha channels by multiplying the
295 // color components by alpha.
296         if(!EQUIV(intercept / 100, 1))
297         {
298                 if(use_opengl)
299                         ((VDeviceX11*)((VirtualVConsole*)vconsole)->get_vdriver())->do_fade(
300                                 output,
301                                 intercept / 100);
302                 else
303                         fader->do_fade(output, output, intercept / 100);
304         }
305 #else
306         alpha = intercept / 100;
307 #endif
308
309         return 0;
310 }
311
312
313
314 void VirtualVNode::render_mask(VFrame *output_temp,
315         int64_t start_position_project,
316         double frame_rate,
317         int use_opengl)
318 {
319         MaskAutos *keyframe_set =
320                 (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
321
322         Auto *current = 0;
323 //      MaskAuto *default_auto = (MaskAuto*)keyframe_set->default_auto;
324         MaskAuto *keyframe = (MaskAuto*)keyframe_set->
325                 get_prev_auto(start_position_project, PLAY_FORWARD, current);
326         if( keyframe->apply_before_plugins ) return;
327
328         int total_points = 0;
329         for(int i = 0; i < keyframe->masks.total; i++)
330         {
331                 SubMask *mask = keyframe->get_submask(i);
332                 int submask_points = mask->points.total;
333                 if(submask_points > 1) total_points += submask_points;
334         }
335
336 //printf("VirtualVNode::render_mask 1 %d %d\n", total_points, keyframe->value);
337 // Ignore certain masks
338         if(total_points <= 2 ||
339                 (keyframe->value == 0 && keyframe->mode == MASK_SUBTRACT_ALPHA))
340         {
341                 return;
342         }
343
344 // Fake certain masks
345         if(keyframe->value == 0 && keyframe->mode == MASK_MULTIPLY_ALPHA)
346         {
347                 output_temp->clear_frame();
348                 return;
349         }
350
351         if(use_opengl) {
352                 if( !((VDeviceX11*)((VirtualVConsole*)vconsole)->get_vdriver())->can_mask(
353                                 start_position_project, keyframe_set) )
354                         use_opengl = 0;
355
356         }
357         if(use_opengl) {
358                 ((VDeviceX11*)((VirtualVConsole*)vconsole)->get_vdriver())->do_mask(
359                                 output_temp, start_position_project, keyframe_set,
360                                 keyframe, keyframe);
361         }
362         else {
363 // Revert to software
364                 masker->do_mask(output_temp, start_position_project,
365                                 keyframe_set, keyframe, keyframe);
366         }
367 }
368
369
370 // Start of input fragment in project if forward.
371 // End of input fragment if reverse.
372 int VirtualVNode::render_projector(VFrame *input, VFrame *output,
373                         int64_t start_position, double frame_rate,
374                         int use_opengl)
375 {
376         float in_x1, in_y1, in_x2, in_y2;
377         float out_x1, out_y1, out_x2, out_y2;
378         double edl_rate = renderengine->get_edl()->session->frame_rate;
379         int64_t start_position_project = (int64_t)(start_position *
380                 edl_rate /
381                 frame_rate);
382         VRender *vrender = ((VirtualVConsole*)vconsole)->vrender;
383         if(vconsole->debug_tree)
384                 printf("  VirtualVNode::render_projector input=%p output=%p cmodel=%d title=%s\n",
385                         input, output, output->get_color_model(), track->title);
386
387         if(output)
388         {
389                 int direction = renderengine->command->get_direction();
390                 ((VTrack*)track)->calculate_output_transfer(
391                         start_position_project, direction,
392                         in_x1, in_y1, in_x2, in_y2,
393                         out_x1, out_y1, out_x2, out_y2);
394
395                 in_x2 += in_x1;   in_y2 += in_y1;
396                 out_x2 += out_x1; out_y2 += out_y1;
397
398 //for(int j = 0; j < input->get_w() * 3 * 5; j++)
399 //      input->get_rows()[0][j] = 255;
400 //
401                 if(out_x2 > out_x1 && out_y2 > out_y1 &&
402                         in_x2 > in_x1 && in_y2 > in_y1)
403                 {
404                         Auto *auto_keyframe = 0;
405                         IntAuto *mode_keyframe =
406                                 (IntAuto*)track->automation->autos[AUTOMATION_MODE]->get_prev_auto(
407                                         start_position_project, PLAY_FORWARD, auto_keyframe);
408
409                         int mode = mode_keyframe->value;
410
411 // Fade is performed in render_fade so as to allow this module
412 // to be chained in another module, thus only 4 component colormodels
413 // can do dissolves, although a blend equation is still required for 3 component
414 // colormodels since fractional translation requires blending.
415
416 // If this is the first playable video track and the mode_keyframe is "src"
417                         if(mode == TRANSFER_NORMAL &&
418                                 vconsole->current_exit_node == vconsole->total_exit_nodes - 1)
419                                 mode = TRANSFER_SRC;
420
421                         if(use_opengl)
422                         {
423                                 ((VDeviceX11*)((VirtualVConsole*)vconsole)->get_vdriver())->overlay(
424                                         output, input,
425                                         in_x1, in_y1, in_x2, in_y2,
426                                         out_x1, out_y1, out_x2, out_y2,
427                                         alpha, mode, renderengine->get_edl(),
428                                         renderengine->is_nested);
429                         }
430                         else
431                         {
432                                 vrender->overlayer->overlay(output, input,
433                                         in_x1, in_y1, in_x2, in_y2,
434                                         out_x1, out_y1, out_x2, out_y2,
435                                         alpha, mode,
436                                         renderengine->get_edl()->session->interpolation_type);
437                         }
438                 }
439         }
440         return 0;
441 }
442