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"
36 #include "maskautos.h"
38 #include "overlayframe.h"
40 #include "pluginarray.h"
41 #include "preferences.h"
42 #include "renderengine.h"
43 #include "sharedlocation.h"
45 #include "transition.h"
46 #include "transportque.h"
48 #include "vattachmentpoint.h"
49 #include "vdevicex11.h"
52 #include "videodevice.h"
53 #include "virtualvconsole.h"
59 #include "interlacemodes.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();
108 int VModule::import_frame(VFrame *output, VEdit *current_edit,
109 int64_t input_position, double frame_rate, int direction, int use_opengl)
111 int64_t direction_position;
112 // Translation of edit
113 float in_x, in_y, in_w, in_h;
114 float out_x, out_y, out_w, out_h;
117 double edl_rate = get_edl()->session->frame_rate;
119 int64_t input_position_project = Units::to_int64(input_position *
120 edl_rate / frame_rate + 0.001);
122 if(!output) printf("VModule::import_frame %d output=%p\n", __LINE__, output);
123 //output->dump_params();
125 if(debug) printf("VModule::import_frame %d this=%p input_position=%lld direction=%d\n",
126 __LINE__, this, (long long)input_position, direction);
128 // Convert to position corrected for direction
129 direction_position = input_position;
130 if(direction == PLAY_REVERSE) {
131 if( direction_position > 0 ) direction_position--;
132 if( input_position_project > 0 ) input_position_project--;
134 if(!output) printf("VModule::import_frame %d output=%p\n", __LINE__, output);
136 VDeviceX11 *x11_device = 0;
139 if(renderengine && renderengine->video)
141 x11_device = (VDeviceX11*)renderengine->video->get_output_base();
142 output->set_opengl_state(VFrame::RAM);
143 if(!x11_device) use_opengl = 0;
147 if(!output) printf("VModule::import_frame %d output=%p x11_device=%p nested_edl=%p\n",
148 __LINE__, output, x11_device, nested_edl);
150 if(debug) printf("VModule::import_frame %d current_edit=%p\n",
151 __LINE__, current_edit);
153 // Load frame into output
155 // Create objects for nested EDL
156 if(current_edit && current_edit->nested_edl) {
158 if(debug) printf("VModule::import_frame %d nested_edl=%p current_edit->nested_edl=%p\n",
159 __LINE__, nested_edl, current_edit->nested_edl);
161 // Convert requested direction to command
162 if(renderengine->command->command == CURRENT_FRAME)
164 command = CURRENT_FRAME;
167 if(direction == PLAY_REVERSE)
169 if(renderengine->command->single_frame())
170 command = SINGLE_FRAME_REWIND;
172 command = NORMAL_REWIND;
176 if(renderengine->command->single_frame())
177 command = SINGLE_FRAME_FWD;
179 command = NORMAL_FWD;
182 if(!nested_edl || nested_edl->id != current_edit->nested_edl->id)
184 nested_edl = current_edit->nested_edl;
185 if(nested_renderengine)
187 delete nested_renderengine;
188 nested_renderengine = 0;
193 nested_command = new TransportCommand;
197 if(!nested_renderengine)
199 nested_command->command = command;
200 nested_command->get_edl()->copy_all(nested_edl);
201 nested_command->change_type = CHANGE_ALL;
202 nested_command->realtime = renderengine->command->realtime;
203 nested_renderengine = new RenderEngine(0, get_preferences(), 0, 1);
204 nested_renderengine->set_vcache(get_cache());
205 nested_renderengine->arm_command(nested_command);
211 // Update nested command
212 nested_renderengine->command->command = command;
213 nested_command->realtime = renderengine->command->realtime;
216 // Update nested video driver for opengl
217 nested_renderengine->video = renderengine->video;
223 if(debug) printf("VModule::import_frame %d\n", __LINE__);
225 if(!output) printf("VModule::import_frame %d output=%p\n", __LINE__, output);
228 (current_edit->asset ||
229 (current_edit->nested_edl && nested_renderengine->vrender)))
233 if(debug) printf("VModule::import_frame %d cache=%p\n",
236 if(current_edit->asset)
239 file = get_cache()->check_out(current_edit->asset,
241 // get_cache()->dump();
245 if(file || nested_edl)
247 // Make all positions based on requested frame rate.
248 int64_t edit_startproject = Units::to_int64(current_edit->startproject *
251 int64_t edit_startsource = Units::to_int64(current_edit->startsource *
254 // Source position going forward
255 uint64_t position = direction_position -
258 int64_t nested_position = 0;
264 // apply speed curve to source position so the timeline agrees with the playback
265 if(track->has_speed())
267 // integrate position from start of edit.
268 double speed_position = edit_startsource;
269 FloatAutos *speed_autos = (FloatAutos*)track->automation->autos[AUTOMATION_SPEED];
270 speed_position += speed_autos->automation_integral(edit_startproject,
271 direction_position-edit_startproject, PLAY_FORWARD);
272 //printf("VModule::import_frame %d %lld %lld\n", __LINE__, position, (int64_t)speed_position);
273 position = (int64_t)speed_position;
282 if(debug) printf("VModule::import_frame %d\n", __LINE__);
285 // maybe apply speed curve here, so timeline reflects actual playback
289 // if we hit the end of stream, freeze at last frame
290 uint64_t max_position = 0;
293 max_position = Units::to_int64((double)file->get_video_length() *
295 current_edit->asset->frame_rate - 1);
299 max_position = Units::to_int64(nested_edl->tracks->total_playable_length() *
304 if(position > max_position) position = max_position;
306 if(position < 0) position = 0;
308 int use_cache = renderengine &&
309 renderengine->command->single_frame();
310 int use_asynchronous = !use_cache && renderengine &&
311 // Try to make rendering go faster.
312 // But converts some formats to YUV420, which may degrade input format.
313 // renderengine->command->realtime &&
314 renderengine->get_edl()->session->video_asynchronous;
318 if(debug) printf("VModule::import_frame %d\n", __LINE__);
320 file->start_video_decode_thread();
322 file->stop_video_thread();
324 int64_t normalized_position = Units::to_int64(position *
325 current_edit->asset->frame_rate /
327 // printf("VModule::import_frame %d %lld %lld\n",
330 // normalized_position);
331 file->set_layer(current_edit->channel);
332 file->set_video_position(normalized_position,
334 asset_w = current_edit->asset->width;
335 asset_h = current_edit->asset->height;
336 //printf("VModule::import_frame %d normalized_position=%lld\n", __LINE__, normalized_position);
340 if(debug) printf("VModule::import_frame %d\n", __LINE__);
341 asset_w = nested_edl->session->output_w;
342 asset_h = nested_edl->session->output_h;
343 // Get source position in nested frame rate in direction of playback.
344 nested_position = Units::to_int64(position *
345 nested_edl->session->frame_rate /
347 if(direction == PLAY_REVERSE)
352 // Auto scale if required
353 if(output->get_params()->get("AUTOSCALE", 0))
355 float autoscale_w = output->get_params()->get("AUTOSCALE_W", 1024);
356 float autoscale_h = output->get_params()->get("AUTOSCALE_H", 1024);
357 float x_scale = autoscale_w / asset_w;
358 float y_scale = autoscale_h / asset_h;
366 if(x_scale < y_scale)
368 out_w = in_w * x_scale;
369 out_h = in_h * x_scale;
373 out_w = in_w * y_scale;
374 out_h = in_h * y_scale;
377 out_x = track->track_w / 2 - out_w / 2;
378 out_y = track->track_h / 2 - out_h / 2;
383 ((VTrack*)track)->calculate_input_transfer(asset_w,
385 input_position_project,
397 // printf("VModule::import_frame %d %f %d %f %d\n",
404 // printf("VModule::import_frame 1 [ilace] Project: mode (%d) Asset: autofixoption (%d), mode (%d), method (%d)\n",
405 // get_edl()->session->interlace_mode,
406 // current_edit->asset->interlace_autofixoption,
407 // current_edit->asset->interlace_mode,
408 // current_edit->asset->interlace_fixmethod);
410 // Determine the interlacing method to use.
411 int interlace_fixmethod = !current_edit->asset ? ILACE_FIXMETHOD_NONE :
412 ilaceautofixmethod2(get_edl()->session->interlace_mode,
413 current_edit->asset->interlace_autofixoption,
414 current_edit->asset->interlace_mode,
415 current_edit->asset->interlace_fixmethod);
417 // char string[BCTEXTLEN];
418 // ilacefixmethod_to_text(string,interlace_fixmethod);
419 // printf("VModule::import_frame 1 [ilace] Compensating by using: '%s'\n",string);
421 // Compensate for the said interlacing...
422 switch (interlace_fixmethod) {
423 case ILACE_FIXMETHOD_NONE:
426 case ILACE_FIXMETHOD_UPONE:
429 case ILACE_FIXMETHOD_DOWNONE:
433 printf("vmodule::importframe WARNING - unknown fix method for interlacing, no compensation in effect\n");
436 // file -> temp -> output
437 if( !EQUIV(in_x, 0) ||
439 !EQUIV(in_w, track->track_w) ||
440 !EQUIV(in_h, track->track_h) ||
443 !EQUIV(out_w, track->track_w) ||
444 !EQUIV(out_h, track->track_h) ||
445 !EQUIV(in_w, asset_w) ||
446 !EQUIV(in_h, asset_h))
448 if(debug) printf("VModule::import_frame %d file -> temp -> output\n", __LINE__);
453 // Get temporary input buffer
458 VRender *vrender = (VRender*)commonrender;
459 //printf("VModule::import_frame %d vrender->input_temp=%p\n", __LINE__, vrender->input_temp);
460 input = &vrender->input_temp;
470 ((*input)->get_w() != asset_w ||
471 (*input)->get_h() != asset_h))
483 (*input) = new VFrame(0,
487 get_edl()->session->color_model,
493 (*input)->copy_stacks(output);
496 // Cache for single frame only
499 if(debug) printf("VModule::import_frame %d this=%p file=%s\n",
502 current_edit->asset->path);
503 if(use_cache) file->set_cache_frames(1);
504 result = file->read_frame((*input));
505 if(use_cache) file->set_cache_frames(0);
506 (*input)->set_opengl_state(VFrame::RAM);
511 // If the colormodels differ, change input to nested colormodel
512 int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
513 int current_cmodel = output->get_color_model();
514 int output_w = output->get_w();
515 int output_h = output->get_h();
516 VFrame *input2 = (*input);
518 if(nested_cmodel != current_cmodel)
520 // If opengl, input -> input -> output
526 // If software, input2 -> input -> output
527 // Use output as a temporary.
531 if(debug) printf("VModule::import_frame %d this=%p nested_cmodel=%d\n",
536 input2->reallocate(0,
549 if(debug) printf("VModule::import_frame %d this=%p nested_edl=%s input2=%p\n",
555 result = nested_renderengine->vrender->process_buffer(
560 if(debug) printf("VModule::import_frame %d this=%p nested_edl=%s\n",
565 if(nested_cmodel != current_cmodel)
567 if(debug) printf("VModule::import_frame %d\n", __LINE__);
570 // Change colormodel in hardware.
571 if(debug) printf("VModule::import_frame %d\n", __LINE__);
572 x11_device->convert_cmodel(input2,
575 // The converted color model is now in hardware, so return the input2 buffer
576 // to the expected color model.
577 input2->reallocate(0,
589 // Transfer from input2 to input
590 if(debug) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d input2=%p input=%p output=%p\n",
597 BC_CModels::transfer((*input)->get_rows(),
618 //printf("VModule::import_frame %d\n", __LINE__);
620 // input2 was the output buffer, so it must be restored
621 input2->reallocate(0,
630 //printf("VModule::import_frame %d\n", __LINE__);
636 // Find an overlayer object to perform the camera transformation
637 OverlayFrame *overlayer = 0;
639 // OpenGL playback uses hardware
642 //printf("VModule::import_frame %d\n", __LINE__);
648 VRender *vrender = (VRender*)commonrender;
649 overlayer = vrender->overlayer;
655 printf("VModule::import_frame neither plugin_array nor commonrender is defined.\n");
658 overlay_temp = new OverlayFrame(plugin_array->mwindow->preferences->processors);
661 overlayer = overlay_temp;
663 // printf("VModule::import_frame 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
674 // for(int j = 0; j < output->get_w() * 3 * 5; j++)
675 // output->get_rows()[0][j] = 255;
679 x11_device->do_camera(output,
689 if(debug) printf("VModule::import_frame %d %d %d\n",
691 output->get_opengl_state(),
692 (*input)->get_opengl_state());
699 output->clear_frame();
702 // get_cache()->check_in(current_edit->asset);
705 // TRANSFER_REPLACE is the fastest transfer mode but it has the disadvantage
706 // of producing green borders in floating point translation of YUV
707 int mode = TRANSFER_REPLACE;
708 if(get_edl()->session->interpolation_type != NEAREST_NEIGHBOR &&
709 BC_CModels::is_yuv(output->get_color_model()))
710 mode = TRANSFER_NORMAL;
712 if(debug) printf("VModule::import_frame %d temp -> output\n", __LINE__);
713 overlayer->overlay(output,
725 get_edl()->session->interpolation_type);
728 output->copy_stacks((*input));
733 if(debug) printf("VModule::import_frame %d file -> output nested_edl=%p file=%p\n",
739 VFrame **input = &output;
740 // If colormodels differ, reallocate output in nested colormodel.
741 int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
742 int current_cmodel = output->get_color_model();
744 if(debug) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d\n",
749 if(nested_cmodel != current_cmodel)
758 input = &((VRender*)commonrender)->input_temp;
765 if(!(*input)) (*input) = new VFrame;
768 (*input)->reallocate(0,
777 if(debug) printf("VModule::import_frame %d\n",
780 //(*input)->clear_frame();
783 if(debug) printf("VModule::import_frame %d %p %p\n",
785 (*input)->get_rows(),
787 result = nested_renderengine->vrender->process_buffer(
791 if(debug) printf("VModule::import_frame %d\n",
794 // If colormodels differ, change colormodels in opengl if possible.
795 // Swap output for temp if not possible.
796 if(nested_cmodel != current_cmodel)
800 x11_device->convert_cmodel(output,
803 // The color model was changed in place, so return output buffer
804 output->reallocate(0,
816 // Transfer from temporary to output
817 if(debug) printf("VModule::import_frame %d %d %d %d %d %d %d\n",
825 BC_CModels::transfer(output->get_rows(),
826 (*input)->get_rows(),
854 // Cache single frames
855 //memset(output->get_rows()[0], 0xff, 1024);
856 if(use_cache) file->set_cache_frames(1);
857 result = file->read_frame(output);
858 if(use_cache) file->set_cache_frames(0);
859 output->set_opengl_state(VFrame::RAM);
865 get_cache()->check_in(current_edit->asset);
872 if(debug) printf("VModule::import_frame %d\n", __LINE__);
875 x11_device->clear_input(output);
879 output->clear_frame();
887 if(debug) printf("VModule::import_frame %d\n", __LINE__);
890 x11_device->clear_input(output);
894 output->clear_frame();
898 if(debug) printf("VModule::import_frame %d done\n", __LINE__);
905 int VModule::render(VFrame *output,
906 int64_t start_position,
914 double edl_rate = get_edl()->session->frame_rate;
916 //printf("VModule::render %d %ld\n", __LINE__, start_position);
918 if(use_nudge) start_position += Units::to_int64(track->nudge *
919 frame_rate / edl_rate);
921 int64_t start_position_project = Units::to_int64(start_position *
922 edl_rate / frame_rate + 0.5);
924 update_transition(start_position_project,
927 VEdit* current_edit = (VEdit*)track->edits->editof(start_position_project,
930 VEdit* previous_edit = 0;
931 //printf("VModule::render %d %p %ld %d\n", __LINE__, current_edit, start_position_project, direction);
934 printf(" VModule::render %d %d %jd %s transition=%p opengl=%d current_edit=%p output=%p\n",
937 start_position_project,
946 output->clear_frame();
947 // We do not apply mask here, since alpha is 0, and neither substracting nor multypling changes it
948 // Another mask mode - "addition" should be added to be able to create mask from empty frames
949 // in this case we would call masking here too...
956 // Process transition
957 if(transition && transition->on)
960 // Get temporary buffer
961 VFrame **transition_input = 0;
964 VRender *vrender = (VRender*)commonrender;
965 transition_input = &vrender->transition_temp;
969 transition_input = &transition_temp;
972 if((*transition_input) &&
973 ((*transition_input)->get_w() != track->track_w ||
974 (*transition_input)->get_h() != track->track_h))
976 delete (*transition_input);
977 (*transition_input) = 0;
980 // Load incoming frame
981 if(!(*transition_input))
983 (*transition_input) = new VFrame(0,
987 get_edl()->session->color_model,
991 (*transition_input)->copy_stacks(output);
993 //printf("VModule::render %d\n", __LINE__);
994 result = import_frame((*transition_input),
1002 // Load transition buffer
1003 previous_edit = (VEdit*)current_edit->previous;
1005 result |= import_frame(output,
1011 //printf("VModule::render %d\n", __LINE__);
1013 // printf("VModule::render %d %p %p %p %p\n",
1015 // (*transition_input),
1016 // (*transition_input)->get_pbuffer(),
1018 // output->get_pbuffer());
1021 // Execute plugin with transition_input and output here
1023 transition_server->set_use_opengl(use_opengl, renderengine->video);
1024 transition_server->process_transition((*transition_input),
1026 (direction == PLAY_FORWARD) ?
1027 (start_position_project - current_edit->startproject) :
1028 (start_position_project - current_edit->startproject - 1),
1029 transition->length);
1033 // Load output buffer
1034 result = import_frame(output,
1043 MaskAutos *keyframe_set =
1044 (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
1045 int64_t mask_position = !renderengine ? start_position :
1046 renderengine->vrender->current_position;
1047 MaskAuto *keyframe =
1048 (MaskAuto*)keyframe_set->get_prev_auto(mask_position, direction, current);
1050 if( keyframe->apply_before_plugins ) {
1051 VDeviceX11 *x11_device = 0;
1052 if(use_opengl && renderengine && renderengine->video) {
1053 x11_device = (VDeviceX11*)renderengine->video->get_output_base();
1054 if( !x11_device->can_mask(mask_position, keyframe_set) )
1057 if( use_opengl && x11_device ) {
1058 x11_device->do_mask(output, mask_position, keyframe_set,
1059 keyframe, keyframe);
1063 int cpus = renderengine ?
1064 renderengine->preferences->processors :
1065 plugin_array->mwindow->preferences->processors;
1066 masker = new MaskEngine(cpus);
1068 masker->do_mask(output, mask_position, keyframe_set, keyframe, keyframe);
1080 void VModule::create_objects()
1082 Module::create_objects();