4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
23 #include "automation.h"
24 #include "bcsignals.h"
28 #include "edlsession.h"
29 #include "fadeengine.h"
30 #include "floatauto.h"
31 #include "floatautos.h"
35 #include "maskautos.h"
36 #include "maskengine.h"
39 #include "overlayframe.h"
40 #include "playabletracks.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"
49 #include "videodevice.h"
50 #include "virtualvconsole.h"
51 #include "virtualvnode.h"
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,
66 //VRender *vrender = ((VirtualVConsole*)vconsole)->vrender;
67 fader = new FadeEngine(renderengine->preferences->processors);
68 masker = new MaskEngine(renderengine->preferences->processors);
72 VirtualVNode::~VirtualVNode()
78 VirtualNode* VirtualVNode::create_module(Plugin *real_plugin,
82 return new VirtualVNode(renderengine, vconsole,
83 real_module, 0, track, this);
87 VirtualNode* VirtualVNode::create_plugin(Plugin *real_plugin)
89 return new VirtualVNode(renderengine, vconsole,
90 0, real_plugin, track, this);
93 int VirtualVNode::read_data(VFrame *output_temp,
94 int64_t start_position, double frame_rate, int use_opengl)
96 VirtualNode *previous_plugin = 0;
100 printf("VirtualVNode::read_data output_temp=%p\n", output_temp);
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);
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)
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);
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)))
124 result = ((VirtualVNode*)previous_plugin)->render(output_temp,
125 start_position, frame_rate, use_opengl);
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))
134 result = ((VirtualVNode*)parent_node)->read_data(output_temp,
135 start_position, frame_rate, use_opengl);
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);
150 int VirtualVNode::render(VFrame *output_temp,
151 int64_t start_position,
155 VRender *vrender = ((VirtualVConsole*)vconsole)->vrender;
158 render_as_module(vrender->video_out, output_temp,
159 start_position, frame_rate, use_opengl);
164 render_as_plugin(output_temp,
165 start_position, frame_rate, use_opengl);
170 void VirtualVNode::render_as_plugin(VFrame *output_temp,
171 int64_t start_position,
175 if(!attachment || !real_plugin || !real_plugin->on) return;
178 if(vconsole->debug_tree)
179 printf(" VirtualVNode::render_as_plugin title=%s use_opengl=%d\n",
180 track->title, use_opengl);
182 ((VAttachmentPoint*)attachment)->render(
183 output_temp, plugin_buffer_number, start_position,
184 frame_rate, vconsole->debug_tree, use_opengl);
188 int VirtualVNode::render_as_module(VFrame *video_out,
190 int64_t start_position,
195 int direction = renderengine->command->get_direction();
196 double edl_rate = renderengine->get_edl()->session->frame_rate;
202 // Get position relative to project, compensated for direction
203 int64_t start_position_project = (int64_t)(start_position *
206 if(direction == PLAY_REVERSE) start_position_project--;
210 if(track->has_speed())
212 // integrate position from start of track. 1/speed is duration of each frame
216 if(vconsole->debug_tree)
217 printf(" VirtualVNode::render_as_module title=%s use_opengl=%d video_out=%p output_temp=%p\n",
218 track->title, use_opengl, video_out, output_temp);
220 output_temp->push_next_effect("VirtualVNode::render_as_module");
222 // Process last subnode. This propogates up the chain of subnodes and finishes
226 VirtualVNode *node = (VirtualVNode*)subnodes.values[subnodes.total - 1];
227 node->render(output_temp, start_position, frame_rate, use_opengl);
230 // Read data from previous entity
232 read_data(output_temp, start_position, frame_rate, use_opengl);
235 output_temp->pop_next_effect();
237 render_fade(output_temp, start_position,
238 frame_rate, track->automation->autos[AUTOMATION_FADE],
239 direction, use_opengl);
241 render_mask(output_temp, start_position_project, frame_rate, use_opengl);
244 // overlay on the final output
247 int mute_fragment = 1;
251 get_mute_fragment(start_position, mute_constant, mute_fragment,
252 (Autos*)((VTrack*)track)->automation->autos[AUTOMATION_MUTE],
258 render_projector(output_temp, video_out, start_position,
259 frame_rate, use_opengl);
262 output_temp->push_prev_effect("VirtualVNode::render_as_module");
263 //printf("VirtualVNode::render_as_module\n");
264 //output_temp->dump_stacks();
267 if(renderengine->show_tc)
268 renderengine->vrender->insert_timecode(edit,
276 int VirtualVNode::render_fade(VFrame *output,
277 // start of input fragment in project if forward / end of input fragment if reverse
278 // relative to requested frame rate
279 int64_t start_position, double frame_rate,
280 Autos *autos, int direction, int use_opengl)
282 FloatAuto *previous = 0;
284 double edl_rate = renderengine->get_edl()->session->frame_rate;
285 int64_t start_position_project =
286 (int64_t)(start_position * edl_rate/frame_rate + EPSILON);
288 if(vconsole->debug_tree)
289 printf(" VirtualVNode::render_fade title=%s\n", track->title);
291 double intercept = ((FloatAutos*)autos)->get_value(start_position_project,
292 direction, previous, next);
294 CLAMP(intercept, 0, 100);
298 // Can't use overlay here because overlayer blends the frame with itself.
299 // The fade engine can compensate for lack of alpha channels by multiplying the
300 // color components by alpha.
301 if(!EQUIV(intercept / 100, 1))
304 ((VDeviceX11*)((VirtualVConsole*)vconsole)->get_vdriver())->do_fade(
308 fader->do_fade(output, output, intercept / 100);
311 alpha = intercept / 100;
319 void VirtualVNode::render_mask(VFrame *output_temp,
320 int64_t start_position_project,
324 MaskAutos *keyframe_set =
325 (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
328 // MaskAuto *default_auto = (MaskAuto*)keyframe_set->default_auto;
329 MaskAuto *keyframe = (MaskAuto*)keyframe_set->
330 get_prev_auto(start_position_project, PLAY_FORWARD, current);
331 if( keyframe->apply_before_plugins ) return;
333 int total_points = 0;
334 for(int i = 0; i < keyframe->masks.total; i++)
336 SubMask *mask = keyframe->get_submask(i);
337 int submask_points = mask->points.total;
338 if(submask_points > 1) total_points += submask_points;
341 //printf("VirtualVNode::render_mask 1 %d %d\n", total_points, keyframe->value);
342 // Ignore certain masks
343 if(total_points <= 2 ||
344 (keyframe->value == 0 && keyframe->mode == MASK_SUBTRACT_ALPHA))
349 // Fake certain masks
350 if(keyframe->value == 0 && keyframe->mode == MASK_MULTIPLY_ALPHA)
352 output_temp->clear_frame();
357 if( !((VDeviceX11*)((VirtualVConsole*)vconsole)->get_vdriver())->can_mask(
358 start_position_project, keyframe_set) )
363 ((VDeviceX11*)((VirtualVConsole*)vconsole)->get_vdriver())->do_mask(
364 output_temp, start_position_project, keyframe_set,
368 // Revert to software
369 masker->do_mask(output_temp, start_position_project,
370 keyframe_set, keyframe, keyframe);
375 // Start of input fragment in project if forward.
376 // End of input fragment if reverse.
377 int VirtualVNode::render_projector(VFrame *input, VFrame *output,
378 int64_t start_position, double frame_rate,
381 float in_x1, in_y1, in_x2, in_y2;
382 float out_x1, out_y1, out_x2, out_y2;
383 double edl_rate = renderengine->get_edl()->session->frame_rate;
384 int64_t start_position_project = (int64_t)(start_position *
387 VRender *vrender = ((VirtualVConsole*)vconsole)->vrender;
388 if(vconsole->debug_tree)
389 printf(" VirtualVNode::render_projector input=%p output=%p cmodel=%d title=%s\n",
390 input, output, output->get_color_model(), track->title);
394 int direction = renderengine->command->get_direction();
395 ((VTrack*)track)->calculate_output_transfer(
396 start_position_project, direction,
397 in_x1, in_y1, in_x2, in_y2,
398 out_x1, out_y1, out_x2, out_y2);
400 in_x2 += in_x1; in_y2 += in_y1;
401 out_x2 += out_x1; out_y2 += out_y1;
403 //for(int j = 0; j < input->get_w() * 3 * 5; j++)
404 // input->get_rows()[0][j] = 255;
406 if(out_x2 > out_x1 && out_y2 > out_y1 &&
407 in_x2 > in_x1 && in_y2 > in_y1)
409 Auto *auto_keyframe = 0;
410 IntAuto *mode_keyframe =
411 (IntAuto*)track->automation->autos[AUTOMATION_MODE]->get_prev_auto(
412 start_position_project, PLAY_FORWARD, auto_keyframe);
414 int mode = mode_keyframe->value;
416 // Fade is performed in render_fade so as to allow this module
417 // to be chained in another module, thus only 4 component colormodels
418 // can do dissolves, although a blend equation is still required for 3 component
419 // colormodels since fractional translation requires blending.
421 // If this is the first playable video track and the mode_keyframe is "src"
422 if(mode == TRANSFER_NORMAL &&
423 vconsole->current_exit_node == vconsole->total_exit_nodes - 1)
428 // Nested EDL's overlay on a PBuffer instead of a screen
429 // is_nested < 0 ? flatten alpha channel, last draw before driver render
430 int is_nested = renderengine->is_nested ? 1 :
431 vconsole->current_exit_node == 0 ? -1 : 0;
432 ((VDeviceX11*)((VirtualVConsole*)vconsole)->get_vdriver())->overlay(
434 in_x1, in_y1, in_x2, in_y2,
435 out_x1, out_y1, out_x2, out_y2,
437 renderengine->get_edl(),
442 vrender->overlayer->overlay(output, input,
443 in_x1, in_y1, in_x2, in_y2,
444 out_x1, out_y1, out_x2, out_y2,
446 renderengine->get_edl()->session->interpolation_type);