4 * Copyright (C) 2009-2013 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
24 #include "bcpbuffer.h"
25 #include "bcsignals.h"
28 #include "commonrender.h"
31 #include "edlsession.h"
34 #include "floatautos.h"
35 #include "mainerror.h"
37 #include "maskautos.h"
39 #include "overlayframe.h"
41 #include "pluginarray.h"
42 #include "preferences.h"
43 #include "renderengine.h"
44 #include "sharedlocation.h"
46 #include "transition.h"
47 #include "transportque.h"
49 #include "vattachmentpoint.h"
50 #include "vdevicex11.h"
53 #include "videodevice.h"
54 #include "virtualvconsole.h"
60 #include "maskengine.h"
61 #include "automation.h"
63 VModule::VModule(RenderEngine *renderengine,
64 CommonRender *commonrender,
65 PluginArray *plugin_array,
67 : Module(renderengine, commonrender, plugin_array, track)
69 data_type = TRACK_VIDEO;
78 if( overlay_temp ) delete overlay_temp;
79 if( input_temp ) delete input_temp;
80 if( transition_temp ) delete transition_temp;
85 AttachmentPoint* VModule::new_attachment(Plugin *plugin)
87 return new VAttachmentPoint(renderengine, plugin);
90 int VModule::get_buffer_size()
95 CICache* VModule::get_cache()
98 return renderengine->get_vcache();
104 int VModule::import_frame(VFrame *output, VEdit *current_edit,
105 int64_t input_position, double frame_rate, int direction, int use_opengl)
107 int64_t direction_position=0;
108 // Translation of edit
109 float in_x=0.0, in_y=0.0, in_w=0.0, in_h=0.0;
110 float out_x=0.0, out_y=0.0, out_w=0.0, out_h=0.0;
113 double edl_rate = get_edl()->session->frame_rate;
115 int64_t input_position_project = Units::to_int64(input_position *
116 edl_rate / frame_rate + 0.001);
118 if( !output ) printf("VModule::import_frame %d output=%p\n", __LINE__, output);
119 //output->dump_params();
121 if( debug ) printf("VModule::import_frame %d this=%p input_position=%lld direction=%d\n",
122 __LINE__, this, (long long)input_position, direction);
124 // Convert to position corrected for direction
125 direction_position = input_position;
126 if( direction == PLAY_REVERSE ) {
127 if( direction_position > 0 ) direction_position--;
128 if( input_position_project > 0 ) input_position_project--;
130 if( !output ) printf("VModule::import_frame %d output=%p\n", __LINE__, output);
132 VDeviceX11 *x11_device = 0;
134 if( renderengine && renderengine->video ) {
135 x11_device = (VDeviceX11*)renderengine->video->get_output_base();
136 output->set_opengl_state(VFrame::RAM);
137 if( !x11_device ) use_opengl = 0;
141 if( !output ) printf("VModule::import_frame %d output=%p x11_device=%p nested_edl=%p\n",
142 __LINE__, output, x11_device, nested_edl);
144 if( debug ) printf("VModule::import_frame %d current_edit=%p\n",
145 __LINE__, current_edit);
147 // Load frame into output
149 // Create objects for nested EDL
150 if( current_edit && current_edit->nested_edl ) {
152 if( debug ) printf("VModule::import_frame %d nested_edl=%p current_edit->nested_edl=%p\n",
153 __LINE__, nested_edl, current_edit->nested_edl);
155 // Convert requested direction to command
156 if( renderengine->command->command == CURRENT_FRAME ||
157 renderengine->command->command == LAST_FRAME ) {
158 command = renderengine->command->command;
161 if( direction == PLAY_REVERSE ) {
162 if( renderengine->command->single_frame() )
163 command = SINGLE_FRAME_REWIND;
165 command = NORMAL_REWIND;
168 if( renderengine->command->single_frame() )
169 command = SINGLE_FRAME_FWD;
171 command = NORMAL_FWD;
174 if( !nested_edl || nested_edl->id != current_edit->nested_edl->id ) {
175 nested_edl = current_edit->nested_edl;
176 if( nested_renderengine ) {
177 delete nested_renderengine;
178 nested_renderengine = 0;
180 if( !nested_command )
181 nested_command = new TransportCommand(get_preferences());
182 nested_command->command = command;
183 nested_command->get_edl()->copy_all(nested_edl);
184 nested_command->change_type = CHANGE_ALL;
185 nested_command->realtime = renderengine->command->realtime;
186 nested_renderengine = new RenderEngine(0, get_preferences(), 0, 1);
187 nested_renderengine->set_vcache(get_cache());
188 nested_renderengine->arm_command(nested_command);
192 // Update nested command
193 nested_renderengine->command->command = command;
194 nested_command->realtime = renderengine->command->realtime;
197 // Update nested video driver for opengl
198 nested_renderengine->video = renderengine->video;
203 if( debug ) printf("VModule::import_frame %d\n", __LINE__);
207 x11_device->clear_input(output);
209 output->clear_frame();
212 printf("VModule::import_frame %d output=%p\n", __LINE__, output);
215 (current_edit->asset ||
216 (current_edit->nested_edl && nested_renderengine->vrender)) ) {
219 //printf("VModule::import_frame %d cache=%p\n", __LINE__, get_cache());
220 if( current_edit->asset ) {
222 file = get_cache()->check_out(current_edit->asset, get_edl());
223 // get_cache()->dump();
227 if( file || nested_edl ) {
228 int64_t nested_position = 0;
229 // Source position going forward, in edl framerate
230 int64_t pos = Units::to_int64((double)direction_position / frame_rate * edl_rate);
231 int64_t len = pos - current_edit->startproject;
232 FloatAutos *speed_autos = !track->has_speed() ? 0 :
233 (FloatAutos*)track->automation->autos[AUTOMATION_SPEED];
234 if( speed_autos && len > 0 )
235 len = speed_autos->automation_integral(current_edit->startproject, len, PLAY_FORWARD);
236 pos = current_edit->startsource + len;
237 // Make positions based on requested frame rate.
238 int64_t position = Units::to_int64((double)pos * frame_rate / edl_rate);
240 int64_t video_length;
241 int asset_w, asset_h;
243 asset_w = current_edit->asset->width;
244 asset_h = current_edit->asset->height;
245 video_length = file->get_video_length();
248 asset_w = nested_edl->session->output_w;
249 asset_h = nested_edl->session->output_h;
250 video_length = nested_edl->tracks->total_length();
253 VFrame *&input = commonrender ?
254 ((VRender*)commonrender)->input_temp : // Realtime playback
255 input_temp ; // Menu effect
256 VFrame::get_temp(input, asset_w, asset_h, get_edl()->session->color_model);
258 int use_cache = renderengine->command->single_frame() ? 1 :
259 renderengine->command->get_direction() == PLAY_REVERSE ? -1 : 0;
261 // int use_asynchronous = !use_cache &&
263 // Try to make rendering go faster.
264 // But converts some formats to YUV420, which may degrade input format.
265 //// renderengine->command->realtime &&
266 // renderengine->get_edl()->session->video_asynchronous;
269 if( debug ) printf("VModule::import_frame %d\n", __LINE__);
270 // if( use_asynchronous )
271 // file->start_video_decode_thread();
273 file->stop_video_thread();
275 VEdit *vnext = (VEdit *)current_edit->next;
276 pos = Units::to_int64((double)input_position / frame_rate * edl_rate);
277 if( renderengine->preferences->cache_transitions && !use_cache &&
278 // cache transitions, not caching and inside transition
279 vnext && vnext->transition && vnext->transition->on &&
280 file->get_video_length() >= 0 && pos >= vnext->startproject &&
281 pos < vnext->startproject + vnext->transition->length ) {
282 file->set_cache_frames(0);
283 file->set_layer(current_edit->channel);
284 VEdit *vnext = (VEdit *)current_edit->next;
285 Track *track = current_edit->track;
286 FloatAutos *speed_autos = (FloatAutos*)(track->has_speed() ?
287 track->automation->autos[AUTOMATION_SPEED] : 0);
288 int64_t end = vnext->startproject + vnext->transition->length;
290 int count = renderengine->preferences->cache_size /
291 input->get_data_size() / 2; // try to burn only 1/2 of cache
292 while( !result && pos < end && count > 0 ) {
293 int64_t curr_pos = pos - current_edit->startproject;
294 if( curr_pos > 0 && speed_autos )
295 curr_pos = speed_autos->automation_integral(
296 current_edit->startproject, curr_pos, PLAY_FORWARD);
297 curr_pos += current_edit->startsource;
298 int64_t norm_pos = Units::to_int64((double)curr_pos *
299 current_edit->asset->frame_rate / edl_rate);
300 if( norm_pos < 0 || video_length < 0 )
302 else if( norm_pos >= video_length )
303 norm_pos = video_length-1;
305 if( file->get_cache_frame(input, norm_pos) )
306 break; // if inside a cache run
308 file->purge_cache(); // start new run
310 file->set_cache_frames(1);
311 file->set_video_position(norm_pos, 0);
312 result = file->read_frame(input);
318 int64_t normalized_position = Units::to_int64((double)position *
319 current_edit->asset->frame_rate / frame_rate);
320 if( normalized_position < 0 || video_length < 0 )
321 normalized_position = 0;
322 else if( normalized_position >= video_length )
323 normalized_position = video_length-1;
324 //printf("VModule::import_frame %d %lld %lld\n", __LINE__, position, normalized_position);
325 file->set_layer(current_edit->channel);
326 file->set_video_position(normalized_position, 0);
329 if( debug ) printf("VModule::import_frame %d\n", __LINE__);
330 // Get source position in nested frame rate in direction of playback.
331 nested_position = Units::to_int64(position *
332 nested_edl->session->frame_rate /
334 if( direction == PLAY_REVERSE )
339 // Auto scale if required
340 if( output->get_params()->get("AUTOSCALE", 0) ) {
341 float autoscale_w = output->get_params()->get("AUTOSCALE_W", 1024);
342 float autoscale_h = output->get_params()->get("AUTOSCALE_H", 1024);
343 float x_scale = autoscale_w / asset_w;
344 float y_scale = autoscale_h / asset_h;
352 if( x_scale < y_scale ) {
353 out_w = in_w * x_scale;
354 out_h = in_h * x_scale;
357 out_w = in_w * y_scale;
358 out_h = in_h * y_scale;
361 out_x = track->track_w / 2 - out_w / 2;
362 out_y = track->track_h / 2 - out_h / 2;
366 ((VTrack*)track)->calculate_input_transfer(asset_w,
367 asset_h, input_position_project, direction,
368 in_x, in_y, in_w, in_h,
369 out_x, out_y, out_w, out_h);
372 // printf("VModule::import_frame %d %f %d %f %d\n",
373 // __LINE__, in_w, asset_w, in_h, asset_h);
375 // file -> temp -> output
376 if( !EQUIV(in_x, 0) ||
378 !EQUIV(in_w, track->track_w) ||
379 !EQUIV(in_h, track->track_h) ||
382 !EQUIV(out_w, track->track_w) ||
383 !EQUIV(out_h, track->track_h) ||
384 !EQUIV(in_w, asset_w) ||
385 !EQUIV(in_h, asset_h)) {
386 //printf("VModule::import_frame %d file -> temp -> output\n", __LINE__);
387 // Get temporary input buffer
388 VFrame **input = commonrender ? // Realtime playback
389 &((VRender*)commonrender)->input_temp :
390 &input_temp ; // Menu effect
391 VFrame::get_temp(*input, asset_w, asset_h,
392 get_edl()->session->color_model);
393 (*input)->copy_stacks(output);
395 // Cache for single frame, reverse playback
397 if( debug ) printf("VModule::import_frame %d this=%p file=%s\n",
400 current_edit->asset->path);
402 file->set_cache_frames(use_cache);
403 result = file->read_frame((*input));
405 file->set_cache_frames(0);
406 (*input)->set_opengl_state(VFrame::RAM);
410 // If the colormodels differ, change input to nested colormodel
411 int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
412 int current_cmodel = output->get_color_model();
413 int output_w = output->get_w();
414 int output_h = output->get_h();
415 VFrame *input2 = (*input);
417 if( nested_cmodel != current_cmodel ) {
418 // If opengl, input -> input -> output
421 // If software, input2 -> input -> output
422 // Use output as a temporary.
426 if( debug ) printf("VModule::import_frame %d this=%p nested_cmodel=%d\n",
431 input2->reallocate(0, -1, 0, 0, 0,
432 (*input)->get_w(), (*input)->get_h(),
438 if( debug ) printf("VModule::import_frame %d this=%p nested_edl=%s input2=%p\n",
439 __LINE__, this, nested_edl->path, input2);
441 result = nested_renderengine->vrender->process_buffer(
442 input2, nested_position, use_opengl);
444 if( debug ) printf("VModule::import_frame %d this=%p nested_edl=%s\n",
449 if( nested_cmodel != current_cmodel ) {
450 if( debug ) printf("VModule::import_frame %d\n", __LINE__);
452 // Change colormodel in hardware.
453 if( debug ) printf("VModule::import_frame %d\n", __LINE__);
454 x11_device->convert_cmodel(input2,
457 // The converted color model is now in hardware, so return the input2 buffer
458 // to the expected color model.
459 input2->reallocate(0, -1, 0, 0, 0,
460 (*input)->get_w(), (*input)->get_h(),
464 // Transfer from input2 to input
465 if( debug ) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d input2=%p input=%p output=%p\n",
466 __LINE__, nested_cmodel, current_cmodel, input2, (*input), output);
467 BC_CModels::transfer((*input)->get_rows(), input2->get_rows(),
469 0, 0, input2->get_w(), input2->get_h(),
470 0, 0, (*input)->get_w(), (*input)->get_h(),
471 nested_cmodel, current_cmodel, 0,
472 input2->get_w(), (*input)->get_w());
473 //printf("VModule::import_frame %d\n", __LINE__);
475 // input2 was the output buffer, so it must be restored
476 input2->reallocate(0, -1, 0, 0, 0,
477 output_w, output_h, current_cmodel, -1);
478 //printf("VModule::import_frame %d\n", __LINE__);
484 // Find an overlayer object to perform the camera transformation
485 OverlayFrame *overlayer = 0;
487 // OpenGL playback uses hardware
489 //printf("VModule::import_frame %d\n", __LINE__);
491 else if( commonrender ) {
493 VRender *vrender = (VRender*)commonrender;
494 overlayer = vrender->overlayer;
499 printf("VModule::import_frame neither plugin_array nor commonrender is defined.\n");
500 if( !overlay_temp ) {
501 overlay_temp = new OverlayFrame(plugin_array->mwindow->preferences->processors);
504 overlayer = overlay_temp;
506 // printf("VModule::import_frame 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
507 // in_x, in_y, in_w, in_h, out_x, out_y, out_w, out_h);
510 // for( int j = 0; j < output->get_w() * 3 * 5; j++ )
511 // output->get_rows()[0][j] = 255;
514 x11_device->do_camera(output, (*input),
515 in_x, in_y, in_x + in_w, in_y + in_h,
516 out_x, out_y, out_x + out_w, out_y + out_h);
517 if( debug ) printf("VModule::import_frame %d %d %d\n",
518 __LINE__, output->get_opengl_state(), (*input)->get_opengl_state());
521 output->clear_frame();
522 // get_cache()->check_in(current_edit->asset);
525 // TRANSFER_REPLACE is the fastest transfer mode but it has the disadvantage
526 // of producing green borders in floating point translation of YUV
527 int mode = TRANSFER_REPLACE;
528 if( get_edl()->session->interpolation_type != NEAREST_NEIGHBOR &&
529 BC_CModels::is_yuv(output->get_color_model()) )
530 mode = TRANSFER_NORMAL;
532 if( debug ) printf("VModule::import_frame %d temp -> output\n", __LINE__);
533 overlayer->overlay(output, (*input),
534 in_x, in_y, in_x + in_w, in_y + in_h,
535 out_x, out_y, out_x + out_w, out_y + out_h,
536 1, mode, get_edl()->session->interpolation_type);
540 output->copy_stacks((*input));
543 //printf("VModule::import_frame %d\n", __LINE__);
544 //(*input)->dump_params();
545 //output->dump_params();
549 if( debug ) printf("VModule::import_frame %d file -> output nested_edl=%p file=%p\n",
550 __LINE__, nested_edl, file);
552 VFrame **input = &output;
553 // If colormodels differ, reallocate output in nested colormodel.
554 int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
555 int current_cmodel = output->get_color_model();
557 if( debug ) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d\n",
562 if( nested_cmodel != current_cmodel ) {
566 input = &((VRender*)commonrender)->input_temp;
572 if( !(*input) ) (*input) = new VFrame;
575 (*input)->reallocate(0, -1, 0, 0, 0,
576 output->get_w(), output->get_h(),
578 if( debug ) printf("VModule::import_frame %d\n", __LINE__);
580 //(*input)->clear_frame();
583 if( debug ) printf("VModule::import_frame %d %p %p\n",
584 __LINE__, (*input)->get_rows(), (*input));
585 result = nested_renderengine->vrender->process_buffer(
586 (*input), nested_position, use_opengl);
587 if( debug ) printf("VModule::import_frame %d\n",
590 // If colormodels differ, change colormodels in opengl if possible.
591 // Swap output for temp if not possible.
592 if( nested_cmodel != current_cmodel ) {
594 x11_device->convert_cmodel(output,
597 // The color model was changed in place, so return output buffer
598 output->reallocate(0, -1, 0, 0, 0,
599 output->get_w(), output->get_h(),
603 // Transfer from temporary to output
604 if( debug ) printf("VModule::import_frame %d %d %d %d %d %d %d\n",
605 __LINE__, (*input)->get_w(), (*input)->get_h(),
606 output->get_w(), output->get_h(), nested_cmodel, current_cmodel);
607 BC_CModels::transfer(output->get_rows(), (*input)->get_rows(),
609 0, 0, (*input)->get_w(), (*input)->get_h(),
610 0, 0, output->get_w(), output->get_h(),
611 nested_cmodel, current_cmodel, 0,
612 (*input)->get_w(), output->get_w());
618 // Cache single frames
619 //memset(output->get_rows()[0], 0xff, 1024);
621 file->set_cache_frames(use_cache);
622 result = file->read_frame(output);
624 file->set_cache_frames(0);
625 output->set_opengl_state(VFrame::RAM);
630 get_cache()->check_in(current_edit->asset);
639 // printf("VModule::import_frame %d cache=%p\n",
646 if( debug ) printf("VModule::import_frame %d\n", __LINE__);
648 x11_device->clear_input(output);
651 output->clear_frame();
655 if( debug ) printf("VModule::import_frame %d done\n", __LINE__);
661 int VModule::render(VFrame *output,
662 int64_t start_position, int direction, double frame_rate,
663 int use_nudge, int debug_render, int use_opengl)
666 double edl_rate = get_edl()->session->frame_rate;
668 //printf("VModule::render %d %ld\n", __LINE__, start_position);
670 if( use_nudge ) start_position += Units::to_int64(track->nudge *
671 frame_rate / edl_rate);
673 int64_t start_position_project = Units::to_int64(start_position *
674 edl_rate / frame_rate + 0.5);
676 update_transition(start_position_project, direction);
678 VEdit* current_edit = (VEdit*)track->edits->editof(start_position_project,
680 VEdit* previous_edit = 0;
681 //printf("VModule::render %d %p %ld %d\n", __LINE__, current_edit, start_position_project, direction);
683 Plugin *transition = get_edl()->tracks->plugin_exists(transition_id);
685 printf(" VModule::render %d %d %jd %s transition=%p opengl=%d current_edit=%p output=%p\n",
686 __LINE__, use_nudge, start_position_project, track->title,
687 transition, use_opengl, current_edit, output);
689 if( !current_edit ) {
690 output->clear_frame();
691 // We do not apply mask here, since alpha is 0, and neither substracting nor multypling changes it
692 // Another mask mode - "addition" should be added to be able to create mask from empty frames
693 // in this case we would call masking here too...
700 // Process transition
701 if( transition && transition->on ) {
703 // Get temporary buffer
704 VFrame **transition_input = 0;
706 VRender *vrender = (VRender*)commonrender;
707 transition_input = &vrender->transition_temp;
710 transition_input = &transition_temp;
713 if( (*transition_input) &&
714 ((*transition_input)->get_w() != track->track_w ||
715 (*transition_input)->get_h() != track->track_h) ) {
716 delete (*transition_input);
717 (*transition_input) = 0;
720 // Load incoming frame
721 if( !(*transition_input) ) {
722 (*transition_input) =
723 new VFrame(track->track_w, track->track_h,
724 get_edl()->session->color_model);
727 (*transition_input)->copy_stacks(output);
729 //printf("VModule::render %d\n", __LINE__);
730 result = import_frame((*transition_input),
731 current_edit, start_position, frame_rate,
732 direction, use_opengl);
735 // Load transition buffer
736 previous_edit = (VEdit*)current_edit->previous;
738 result |= import_frame(output,
739 previous_edit, start_position, frame_rate,
740 direction, use_opengl);
741 //printf("VModule::render %d %p %p %p %p\n", __LINE__,
742 // (*transition_input), (*transition_input)->get_pbuffer(),
743 // output, output->get_pbuffer());
745 if( transition_server ) {
746 // Execute plugin with transition_input and output here
748 transition_server->set_use_opengl(use_opengl, renderengine->video);
749 transition_server->process_transition((*transition_input), output,
750 (direction == PLAY_FORWARD) ?
751 (start_position_project - current_edit->startproject) :
752 (start_position_project - current_edit->startproject - 1),
756 eprintf("missing transition plugin: %s\n", transition->title);
759 // Load output buffer
760 result = import_frame(output,
761 current_edit, start_position, frame_rate,
762 direction, use_opengl);
766 MaskAutos *keyframe_set =
767 (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
768 int64_t mask_position = !renderengine ? start_position :
769 renderengine->vrender->current_position;
771 (MaskAuto*)keyframe_set->get_prev_auto(mask_position, direction, current);
773 if( keyframe->apply_before_plugins && keyframe->has_active_mask() ) {
774 VDeviceX11 *x11_device = 0;
775 if( use_opengl && renderengine && renderengine->video ) {
776 x11_device = (VDeviceX11*)renderengine->video->get_output_base();
777 if( !x11_device->can_mask(mask_position, keyframe_set) ) {
778 if( output->get_opengl_state() != VFrame::RAM ) {
779 int w = output->get_w(), h = output->get_h();
780 x11_device->do_camera(output, output,
781 0,0,w,h, 0,0,w,h); // copy to ram
786 if( use_opengl && x11_device ) {
787 x11_device->do_mask(output, mask_position, keyframe_set,
792 int cpus = renderengine ?
793 renderengine->preferences->processors :
794 plugin_array->mwindow->preferences->processors;
795 masker = new MaskEngine(cpus);
797 masker->do_mask(output, mask_position, keyframe_set, keyframe, keyframe);
805 void VModule::create_objects()
807 Module::create_objects();