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 > 0 )
207 start_position_project--;
211 if(track->has_speed())
213 // integrate position from start of track. 1/speed is duration of each frame
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);
221 output_temp->push_next_effect("VirtualVNode::render_as_module");
223 // Process last subnode. This propogates up the chain of subnodes and finishes
227 VirtualVNode *node = (VirtualVNode*)subnodes.values[subnodes.total - 1];
228 node->render(output_temp, start_position, frame_rate, use_opengl);
231 // Read data from previous entity
233 read_data(output_temp, start_position, frame_rate, use_opengl);
236 output_temp->pop_next_effect();
238 render_fade(output_temp, start_position,
239 frame_rate, track->automation->autos[AUTOMATION_FADE],
240 direction, use_opengl);
242 render_mask(output_temp, start_position_project, frame_rate, use_opengl);
245 // overlay on the final output
248 int mute_fragment = 1;
252 get_mute_fragment(start_position, mute_constant, mute_fragment,
253 (Autos*)((VTrack*)track)->automation->autos[AUTOMATION_MUTE],
259 render_projector(output_temp, video_out, start_position,
260 frame_rate, use_opengl);
263 output_temp->push_prev_effect("VirtualVNode::render_as_module");
264 //printf("VirtualVNode::render_as_module\n");
265 //output_temp->dump_stacks();
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)
277 FloatAuto *previous = 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);
283 if(vconsole->debug_tree)
284 printf(" VirtualVNode::render_fade title=%s\n", track->title);
286 double intercept = ((FloatAutos*)autos)->get_value(start_position_project,
287 direction, previous, next);
289 CLAMP(intercept, 0, 100);
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))
299 ((VDeviceX11*)((VirtualVConsole*)vconsole)->get_vdriver())->do_fade(
303 fader->do_fade(output, output, intercept / 100);
306 alpha = intercept / 100;
314 void VirtualVNode::render_mask(VFrame *output_temp,
315 int64_t start_position_project,
319 MaskAutos *keyframe_set =
320 (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
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;
328 int total_points = 0;
329 for(int i = 0; i < keyframe->masks.total; i++)
331 SubMask *mask = keyframe->get_submask(i);
332 int submask_points = mask->points.total;
333 if(submask_points > 1) total_points += submask_points;
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))
344 // Fake certain masks
345 if(keyframe->value == 0 && keyframe->mode == MASK_MULTIPLY_ALPHA)
347 output_temp->clear_frame();
352 if( !((VDeviceX11*)((VirtualVConsole*)vconsole)->get_vdriver())->can_mask(
353 start_position_project, keyframe_set) )
358 ((VDeviceX11*)((VirtualVConsole*)vconsole)->get_vdriver())->do_mask(
359 output_temp, start_position_project, keyframe_set,
363 // Revert to software
364 masker->do_mask(output_temp, start_position_project,
365 keyframe_set, keyframe, keyframe);
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,
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 *
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);
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);
395 in_x2 += in_x1; in_y2 += in_y1;
396 out_x2 += out_x1; out_y2 += out_y1;
398 //for(int j = 0; j < input->get_w() * 3 * 5; j++)
399 // input->get_rows()[0][j] = 255;
401 if(out_x2 > out_x1 && out_y2 > out_y1 &&
402 in_x2 > in_x1 && in_y2 > in_y1)
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);
409 int mode = mode_keyframe->value;
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.
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)
423 ((VDeviceX11*)((VirtualVConsole*)vconsole)->get_vdriver())->overlay(
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);
432 vrender->overlayer->overlay(output, input,
433 in_x1, in_y1, in_x2, in_y2,
434 out_x1, out_y1, out_x2, out_y2,
436 renderengine->get_edl()->session->interpolation_type);