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 ||
163 renderengine->command->command == LAST_FRAME )
165 command = renderengine->command->command;
168 if(direction == PLAY_REVERSE)
170 if(renderengine->command->single_frame())
171 command = SINGLE_FRAME_REWIND;
173 command = NORMAL_REWIND;
177 if(renderengine->command->single_frame())
178 command = SINGLE_FRAME_FWD;
180 command = NORMAL_FWD;
183 if(!nested_edl || nested_edl->id != current_edit->nested_edl->id)
185 nested_edl = current_edit->nested_edl;
186 if(nested_renderengine)
188 delete nested_renderengine;
189 nested_renderengine = 0;
194 nested_command = new TransportCommand;
198 if(!nested_renderengine)
200 nested_command->command = command;
201 nested_command->get_edl()->copy_all(nested_edl);
202 nested_command->change_type = CHANGE_ALL;
203 nested_command->realtime = renderengine->command->realtime;
204 nested_renderengine = new RenderEngine(0, get_preferences(), 0, 1);
205 nested_renderengine->set_vcache(get_cache());
206 nested_renderengine->arm_command(nested_command);
212 // Update nested command
213 nested_renderengine->command->command = command;
214 nested_command->realtime = renderengine->command->realtime;
217 // Update nested video driver for opengl
218 nested_renderengine->video = renderengine->video;
224 if(debug) printf("VModule::import_frame %d\n", __LINE__);
228 x11_device->clear_input(output);
230 output->clear_frame();
233 printf("VModule::import_frame %d output=%p\n", __LINE__, output);
236 (current_edit->asset ||
237 (current_edit->nested_edl && nested_renderengine->vrender)))
241 //printf("VModule::import_frame %d cache=%p\n", __LINE__, get_cache());
242 if( current_edit->asset ) {
244 file = get_cache()->check_out(current_edit->asset,
246 // get_cache()->dump();
250 if(file || nested_edl)
252 // Make all positions based on requested frame rate.
253 int64_t edit_startproject = Units::to_int64(current_edit->startproject *
256 int64_t edit_startsource = Units::to_int64(current_edit->startsource *
259 // Source position going forward
260 uint64_t position = direction_position -
263 int64_t nested_position = 0;
269 // apply speed curve to source position so the timeline agrees with the playback
270 if(track->has_speed())
272 // integrate position from start of edit.
273 double speed_position = edit_startsource;
274 FloatAutos *speed_autos = (FloatAutos*)track->automation->autos[AUTOMATION_SPEED];
275 speed_position += speed_autos->automation_integral(edit_startproject,
276 direction_position-edit_startproject, PLAY_FORWARD);
277 //printf("VModule::import_frame %d %lld %lld\n", __LINE__, position, (int64_t)speed_position);
278 position = (int64_t)speed_position;
287 if(debug) printf("VModule::import_frame %d\n", __LINE__);
290 // maybe apply speed curve here, so timeline reflects actual playback
294 // if we hit the end of stream, freeze at last frame
295 uint64_t max_position = 0;
298 max_position = Units::to_int64((double)file->get_video_length() *
300 current_edit->asset->frame_rate - 1);
304 max_position = Units::to_int64(nested_edl->tracks->total_length() *
309 if(position > max_position) position = max_position;
311 if(position < 0) position = 0;
313 int use_cache = renderengine &&
314 renderengine->command->single_frame();
315 // int use_asynchronous = !use_cache &&
317 // Try to make rendering go faster.
318 // But converts some formats to YUV420, which may degrade input format.
319 //// renderengine->command->realtime &&
320 // renderengine->get_edl()->session->video_asynchronous;
324 if(debug) printf("VModule::import_frame %d\n", __LINE__);
325 // if(use_asynchronous)
326 // file->start_video_decode_thread();
328 file->stop_video_thread();
330 int64_t normalized_position = Units::to_int64(position *
331 current_edit->asset->frame_rate /
333 // printf("VModule::import_frame %d %lld %lld\n",
336 // normalized_position);
337 file->set_layer(current_edit->channel);
338 file->set_video_position(normalized_position,
340 asset_w = current_edit->asset->width;
341 asset_h = current_edit->asset->height;
342 //printf("VModule::import_frame %d normalized_position=%lld\n", __LINE__, normalized_position);
346 if(debug) printf("VModule::import_frame %d\n", __LINE__);
347 asset_w = nested_edl->session->output_w;
348 asset_h = nested_edl->session->output_h;
349 // Get source position in nested frame rate in direction of playback.
350 nested_position = Units::to_int64(position *
351 nested_edl->session->frame_rate /
353 if(direction == PLAY_REVERSE)
358 // Auto scale if required
359 if(output->get_params()->get("AUTOSCALE", 0))
361 float autoscale_w = output->get_params()->get("AUTOSCALE_W", 1024);
362 float autoscale_h = output->get_params()->get("AUTOSCALE_H", 1024);
363 float x_scale = autoscale_w / asset_w;
364 float y_scale = autoscale_h / asset_h;
372 if(x_scale < y_scale)
374 out_w = in_w * x_scale;
375 out_h = in_h * x_scale;
379 out_w = in_w * y_scale;
380 out_h = in_h * y_scale;
383 out_x = track->track_w / 2 - out_w / 2;
384 out_y = track->track_h / 2 - out_h / 2;
389 ((VTrack*)track)->calculate_input_transfer(asset_w,
391 input_position_project,
403 // printf("VModule::import_frame %d %f %d %f %d\n",
410 // printf("VModule::import_frame 1 [ilace] Project: mode (%d) Asset: autofixoption (%d), mode (%d), method (%d)\n",
411 // get_edl()->session->interlace_mode,
412 // current_edit->asset->interlace_autofixoption,
413 // current_edit->asset->interlace_mode,
414 // current_edit->asset->interlace_fixmethod);
416 // Determine the interlacing method to use.
417 int interlace_fixmethod = !current_edit->asset ? ILACE_FIXMETHOD_NONE :
418 ilaceautofixmethod2(get_edl()->session->interlace_mode,
419 current_edit->asset->interlace_autofixoption,
420 current_edit->asset->interlace_mode,
421 current_edit->asset->interlace_fixmethod);
423 // char string[BCTEXTLEN];
424 // ilacefixmethod_to_text(string,interlace_fixmethod);
425 // printf("VModule::import_frame 1 [ilace] Compensating by using: '%s'\n",string);
427 // Compensate for the said interlacing...
428 switch (interlace_fixmethod) {
429 case ILACE_FIXMETHOD_NONE:
432 case ILACE_FIXMETHOD_UPONE:
435 case ILACE_FIXMETHOD_DOWNONE:
439 printf("vmodule::importframe WARNING - unknown fix method for interlacing, no compensation in effect\n");
442 // file -> temp -> output
443 if( !EQUIV(in_x, 0) ||
445 !EQUIV(in_w, track->track_w) ||
446 !EQUIV(in_h, track->track_h) ||
449 !EQUIV(out_w, track->track_w) ||
450 !EQUIV(out_h, track->track_h) ||
451 !EQUIV(in_w, asset_w) ||
452 !EQUIV(in_h, asset_h))
454 //printf("VModule::import_frame %d file -> temp -> output\n", __LINE__);
459 // Get temporary input buffer
464 VRender *vrender = (VRender*)commonrender;
465 //printf("VModule::import_frame %d vrender->input_temp=%p\n", __LINE__, vrender->input_temp);
466 input = &vrender->input_temp;
476 ((*input)->get_w() != asset_w ||
477 (*input)->get_h() != asset_h))
490 new VFrame(asset_w, asset_h,
491 get_edl()->session->color_model);
496 (*input)->copy_stacks(output);
499 // Cache for single frame only
502 if(debug) printf("VModule::import_frame %d this=%p file=%s\n",
505 current_edit->asset->path);
506 if(use_cache) file->set_cache_frames(1);
507 result = file->read_frame((*input));
508 if(use_cache) file->set_cache_frames(0);
509 (*input)->set_opengl_state(VFrame::RAM);
514 // If the colormodels differ, change input to nested colormodel
515 int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
516 int current_cmodel = output->get_color_model();
517 int output_w = output->get_w();
518 int output_h = output->get_h();
519 VFrame *input2 = (*input);
521 if(nested_cmodel != current_cmodel)
523 // If opengl, input -> input -> output
529 // If software, input2 -> input -> output
530 // Use output as a temporary.
534 if(debug) printf("VModule::import_frame %d this=%p nested_cmodel=%d\n",
539 input2->reallocate(0,
552 if(debug) printf("VModule::import_frame %d this=%p nested_edl=%s input2=%p\n",
558 result = nested_renderengine->vrender->process_buffer(
563 if(debug) printf("VModule::import_frame %d this=%p nested_edl=%s\n",
568 if(nested_cmodel != current_cmodel)
570 if(debug) printf("VModule::import_frame %d\n", __LINE__);
573 // Change colormodel in hardware.
574 if(debug) printf("VModule::import_frame %d\n", __LINE__);
575 x11_device->convert_cmodel(input2,
578 // The converted color model is now in hardware, so return the input2 buffer
579 // to the expected color model.
580 input2->reallocate(0,
592 // Transfer from input2 to input
593 if(debug) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d input2=%p input=%p output=%p\n",
600 BC_CModels::transfer((*input)->get_rows(),
621 //printf("VModule::import_frame %d\n", __LINE__);
623 // input2 was the output buffer, so it must be restored
624 input2->reallocate(0,
633 //printf("VModule::import_frame %d\n", __LINE__);
639 // Find an overlayer object to perform the camera transformation
640 OverlayFrame *overlayer = 0;
642 // OpenGL playback uses hardware
645 //printf("VModule::import_frame %d\n", __LINE__);
651 VRender *vrender = (VRender*)commonrender;
652 overlayer = vrender->overlayer;
658 printf("VModule::import_frame neither plugin_array nor commonrender is defined.\n");
661 overlay_temp = new OverlayFrame(plugin_array->mwindow->preferences->processors);
664 overlayer = overlay_temp;
666 // printf("VModule::import_frame 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
677 // for(int j = 0; j < output->get_w() * 3 * 5; j++)
678 // output->get_rows()[0][j] = 255;
682 x11_device->do_camera(output,
692 if(debug) printf("VModule::import_frame %d %d %d\n",
694 output->get_opengl_state(),
695 (*input)->get_opengl_state());
702 output->clear_frame();
705 // get_cache()->check_in(current_edit->asset);
708 // TRANSFER_REPLACE is the fastest transfer mode but it has the disadvantage
709 // of producing green borders in floating point translation of YUV
710 int mode = TRANSFER_REPLACE;
711 if(get_edl()->session->interpolation_type != NEAREST_NEIGHBOR &&
712 BC_CModels::is_yuv(output->get_color_model()))
713 mode = TRANSFER_NORMAL;
715 if(debug) printf("VModule::import_frame %d temp -> output\n", __LINE__);
716 overlayer->overlay(output,
728 get_edl()->session->interpolation_type);
732 output->copy_stacks((*input));
735 //printf("VModule::import_frame %d\n", __LINE__);
736 //(*input)->dump_params();
737 //output->dump_params();
742 if(debug) printf("VModule::import_frame %d file -> output nested_edl=%p file=%p\n",
748 VFrame **input = &output;
749 // If colormodels differ, reallocate output in nested colormodel.
750 int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
751 int current_cmodel = output->get_color_model();
753 if(debug) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d\n",
758 if(nested_cmodel != current_cmodel)
767 input = &((VRender*)commonrender)->input_temp;
774 if(!(*input)) (*input) = new VFrame;
777 (*input)->reallocate(0,
786 if(debug) printf("VModule::import_frame %d\n",
789 //(*input)->clear_frame();
792 if(debug) printf("VModule::import_frame %d %p %p\n",
794 (*input)->get_rows(),
796 result = nested_renderengine->vrender->process_buffer(
800 if(debug) printf("VModule::import_frame %d\n",
803 // If colormodels differ, change colormodels in opengl if possible.
804 // Swap output for temp if not possible.
805 if(nested_cmodel != current_cmodel)
809 x11_device->convert_cmodel(output,
812 // The color model was changed in place, so return output buffer
813 output->reallocate(0,
825 // Transfer from temporary to output
826 if(debug) printf("VModule::import_frame %d %d %d %d %d %d %d\n",
834 BC_CModels::transfer(output->get_rows(),
835 (*input)->get_rows(),
863 // Cache single frames
864 //memset(output->get_rows()[0], 0xff, 1024);
865 if(use_cache) file->set_cache_frames(1);
866 result = file->read_frame(output);
867 if(use_cache) file->set_cache_frames(0);
868 output->set_opengl_state(VFrame::RAM);
874 get_cache()->check_in(current_edit->asset);
884 // printf("VModule::import_frame %d cache=%p\n",
892 if(debug) printf("VModule::import_frame %d\n", __LINE__);
895 x11_device->clear_input(output);
899 output->clear_frame();
903 if(debug) printf("VModule::import_frame %d done\n", __LINE__);
910 int VModule::render(VFrame *output,
911 int64_t start_position,
919 double edl_rate = get_edl()->session->frame_rate;
921 //printf("VModule::render %d %ld\n", __LINE__, start_position);
923 if(use_nudge) start_position += Units::to_int64(track->nudge *
924 frame_rate / edl_rate);
926 int64_t start_position_project = Units::to_int64(start_position *
927 edl_rate / frame_rate + 0.5);
929 update_transition(start_position_project,
932 VEdit* current_edit = (VEdit*)track->edits->editof(start_position_project,
935 VEdit* previous_edit = 0;
936 //printf("VModule::render %d %p %ld %d\n", __LINE__, current_edit, start_position_project, direction);
939 printf(" VModule::render %d %d %jd %s transition=%p opengl=%d current_edit=%p output=%p\n",
942 start_position_project,
951 output->clear_frame();
952 // We do not apply mask here, since alpha is 0, and neither substracting nor multypling changes it
953 // Another mask mode - "addition" should be added to be able to create mask from empty frames
954 // in this case we would call masking here too...
961 // Process transition
962 if(transition && transition->on)
965 // Get temporary buffer
966 VFrame **transition_input = 0;
969 VRender *vrender = (VRender*)commonrender;
970 transition_input = &vrender->transition_temp;
974 transition_input = &transition_temp;
977 if((*transition_input) &&
978 ((*transition_input)->get_w() != track->track_w ||
979 (*transition_input)->get_h() != track->track_h))
981 delete (*transition_input);
982 (*transition_input) = 0;
985 // Load incoming frame
986 if(!(*transition_input))
988 (*transition_input) =
989 new VFrame(track->track_w, track->track_h,
990 get_edl()->session->color_model);
993 (*transition_input)->copy_stacks(output);
995 //printf("VModule::render %d\n", __LINE__);
996 result = import_frame((*transition_input),
1004 // Load transition buffer
1005 previous_edit = (VEdit*)current_edit->previous;
1007 result |= import_frame(output,
1013 //printf("VModule::render %d\n", __LINE__);
1015 // printf("VModule::render %d %p %p %p %p\n",
1017 // (*transition_input),
1018 // (*transition_input)->get_pbuffer(),
1020 // output->get_pbuffer());
1023 // Execute plugin with transition_input and output here
1025 transition_server->set_use_opengl(use_opengl, renderengine->video);
1026 transition_server->process_transition((*transition_input),
1028 (direction == PLAY_FORWARD) ?
1029 (start_position_project - current_edit->startproject) :
1030 (start_position_project - current_edit->startproject - 1),
1031 transition->length);
1035 // Load output buffer
1036 result = import_frame(output,
1045 MaskAutos *keyframe_set =
1046 (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
1047 int64_t mask_position = !renderengine ? start_position :
1048 renderengine->vrender->current_position;
1049 MaskAuto *keyframe =
1050 (MaskAuto*)keyframe_set->get_prev_auto(mask_position, direction, current);
1052 if( keyframe->apply_before_plugins ) {
1053 VDeviceX11 *x11_device = 0;
1054 if(use_opengl && renderengine && renderengine->video) {
1055 x11_device = (VDeviceX11*)renderengine->video->get_output_base();
1056 if( !x11_device->can_mask(mask_position, keyframe_set) )
1059 if( use_opengl && x11_device ) {
1060 x11_device->do_mask(output, mask_position, keyframe_set,
1061 keyframe, keyframe);
1065 int cpus = renderengine ?
1066 renderengine->preferences->processors :
1067 plugin_array->mwindow->preferences->processors;
1068 masker = new MaskEngine(cpus);
1070 masker->do_mask(output, mask_position, keyframe_set, keyframe, keyframe);
1082 void VModule::create_objects()
1084 Module::create_objects();