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;
327 if( !keyframe->has_active_mask() ) return;
329 // Fake certain masks
330 if(keyframe->value == 0 && keyframe->mode == MASK_MULTIPLY_ALPHA)
332 output_temp->clear_frame();
336 VDeviceX11 *x11_device = 0;
338 x11_device = (VDeviceX11*)((VirtualVConsole*)vconsole)->get_vdriver();
339 if( !x11_device->can_mask(start_position_project, keyframe_set) ) {
340 if( output_temp->get_opengl_state() != VFrame::RAM ) {
341 int w = output_temp->get_w(), h = output_temp->get_h();
342 x11_device->do_camera(output_temp, output_temp,
343 0,0,w,h, 0,0,w,h); // copy to ram
349 x11_device->do_mask(output_temp, start_position_project, keyframe_set,
352 // Revert to software
353 masker->do_mask(output_temp, start_position_project,
354 keyframe_set, keyframe, keyframe);
359 // Start of input fragment in project if forward.
360 // End of input fragment if reverse.
361 int VirtualVNode::render_projector(VFrame *input, VFrame *output,
362 int64_t start_position, double frame_rate,
365 float in_x1, in_y1, in_x2, in_y2;
366 float out_x1, out_y1, out_x2, out_y2;
367 double edl_rate = renderengine->get_edl()->session->frame_rate;
368 int64_t start_position_project = (int64_t)(start_position *
371 VRender *vrender = ((VirtualVConsole*)vconsole)->vrender;
372 if(vconsole->debug_tree)
373 printf(" VirtualVNode::render_projector input=%p output=%p cmodel=%d title=%s\n",
374 input, output, output->get_color_model(), track->title);
378 int direction = renderengine->command->get_direction();
379 ((VTrack*)track)->calculate_output_transfer(
380 start_position_project, direction,
381 in_x1, in_y1, in_x2, in_y2,
382 out_x1, out_y1, out_x2, out_y2);
384 in_x2 += in_x1; in_y2 += in_y1;
385 out_x2 += out_x1; out_y2 += out_y1;
387 //for(int j = 0; j < input->get_w() * 3 * 5; j++)
388 // input->get_rows()[0][j] = 255;
390 if(out_x2 > out_x1 && out_y2 > out_y1 &&
391 in_x2 > in_x1 && in_y2 > in_y1)
393 Auto *auto_keyframe = 0;
394 IntAuto *mode_keyframe =
395 (IntAuto*)track->automation->autos[AUTOMATION_MODE]->get_prev_auto(
396 start_position_project, PLAY_FORWARD, auto_keyframe);
398 int mode = mode_keyframe->value;
400 // Fade is performed in render_fade so as to allow this module
401 // to be chained in another module, thus only 4 component colormodels
402 // can do dissolves, although a blend equation is still required for 3 component
403 // colormodels since fractional translation requires blending.
405 // If this is the first playable video track and the mode_keyframe is "src"
406 if(mode == TRANSFER_NORMAL &&
407 vconsole->current_exit_node == vconsole->total_exit_nodes - 1)
412 ((VDeviceX11*)((VirtualVConsole*)vconsole)->get_vdriver())->overlay(
414 in_x1, in_y1, in_x2, in_y2,
415 out_x1, out_y1, out_x2, out_y2,
416 alpha, mode, renderengine->get_edl(),
417 renderengine->is_nested);
421 vrender->overlayer->overlay(output, input,
422 in_x1, in_y1, in_x2, in_y2,
423 out_x1, out_y1, out_x2, out_y2,
425 renderengine->get_edl()->session->interpolation_type);