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 //printf("VModule::import_frame %d cache=%p\n", __LINE__, get_cache());
234 if( current_edit->asset ) {
236 file = get_cache()->check_out(current_edit->asset,
238 // get_cache()->dump();
242 if(file || nested_edl)
244 // Make all positions based on requested frame rate.
245 int64_t edit_startproject = Units::to_int64(current_edit->startproject *
248 int64_t edit_startsource = Units::to_int64(current_edit->startsource *
251 // Source position going forward
252 uint64_t position = direction_position -
255 int64_t nested_position = 0;
261 // apply speed curve to source position so the timeline agrees with the playback
262 if(track->has_speed())
264 // integrate position from start of edit.
265 double speed_position = edit_startsource;
266 FloatAutos *speed_autos = (FloatAutos*)track->automation->autos[AUTOMATION_SPEED];
267 speed_position += speed_autos->automation_integral(edit_startproject,
268 direction_position-edit_startproject, PLAY_FORWARD);
269 //printf("VModule::import_frame %d %lld %lld\n", __LINE__, position, (int64_t)speed_position);
270 position = (int64_t)speed_position;
279 if(debug) printf("VModule::import_frame %d\n", __LINE__);
282 // maybe apply speed curve here, so timeline reflects actual playback
286 // if we hit the end of stream, freeze at last frame
287 uint64_t max_position = 0;
290 max_position = Units::to_int64((double)file->get_video_length() *
292 current_edit->asset->frame_rate - 1);
296 max_position = Units::to_int64(nested_edl->tracks->total_length() *
301 if(position > max_position) position = max_position;
303 if(position < 0) position = 0;
305 int use_cache = renderengine &&
306 renderengine->command->single_frame();
307 // int use_asynchronous = !use_cache &&
309 // Try to make rendering go faster.
310 // But converts some formats to YUV420, which may degrade input format.
311 //// renderengine->command->realtime &&
312 // renderengine->get_edl()->session->video_asynchronous;
316 if(debug) printf("VModule::import_frame %d\n", __LINE__);
317 // if(use_asynchronous)
318 // file->start_video_decode_thread();
320 file->stop_video_thread();
322 int64_t normalized_position = Units::to_int64(position *
323 current_edit->asset->frame_rate /
325 // printf("VModule::import_frame %d %lld %lld\n",
328 // normalized_position);
329 file->set_layer(current_edit->channel);
330 file->set_video_position(normalized_position,
332 asset_w = current_edit->asset->width;
333 asset_h = current_edit->asset->height;
334 //printf("VModule::import_frame %d normalized_position=%lld\n", __LINE__, normalized_position);
338 if(debug) printf("VModule::import_frame %d\n", __LINE__);
339 asset_w = nested_edl->session->output_w;
340 asset_h = nested_edl->session->output_h;
341 // Get source position in nested frame rate in direction of playback.
342 nested_position = Units::to_int64(position *
343 nested_edl->session->frame_rate /
345 if(direction == PLAY_REVERSE)
350 // Auto scale if required
351 if(output->get_params()->get("AUTOSCALE", 0))
353 float autoscale_w = output->get_params()->get("AUTOSCALE_W", 1024);
354 float autoscale_h = output->get_params()->get("AUTOSCALE_H", 1024);
355 float x_scale = autoscale_w / asset_w;
356 float y_scale = autoscale_h / asset_h;
364 if(x_scale < y_scale)
366 out_w = in_w * x_scale;
367 out_h = in_h * x_scale;
371 out_w = in_w * y_scale;
372 out_h = in_h * y_scale;
375 out_x = track->track_w / 2 - out_w / 2;
376 out_y = track->track_h / 2 - out_h / 2;
381 ((VTrack*)track)->calculate_input_transfer(asset_w,
383 input_position_project,
395 // printf("VModule::import_frame %d %f %d %f %d\n",
402 // printf("VModule::import_frame 1 [ilace] Project: mode (%d) Asset: autofixoption (%d), mode (%d), method (%d)\n",
403 // get_edl()->session->interlace_mode,
404 // current_edit->asset->interlace_autofixoption,
405 // current_edit->asset->interlace_mode,
406 // current_edit->asset->interlace_fixmethod);
408 // Determine the interlacing method to use.
409 int interlace_fixmethod = !current_edit->asset ? ILACE_FIXMETHOD_NONE :
410 ilaceautofixmethod2(get_edl()->session->interlace_mode,
411 current_edit->asset->interlace_autofixoption,
412 current_edit->asset->interlace_mode,
413 current_edit->asset->interlace_fixmethod);
415 // char string[BCTEXTLEN];
416 // ilacefixmethod_to_text(string,interlace_fixmethod);
417 // printf("VModule::import_frame 1 [ilace] Compensating by using: '%s'\n",string);
419 // Compensate for the said interlacing...
420 switch (interlace_fixmethod) {
421 case ILACE_FIXMETHOD_NONE:
424 case ILACE_FIXMETHOD_UPONE:
427 case ILACE_FIXMETHOD_DOWNONE:
431 printf("vmodule::importframe WARNING - unknown fix method for interlacing, no compensation in effect\n");
434 // file -> temp -> output
435 if( !EQUIV(in_x, 0) ||
437 !EQUIV(in_w, track->track_w) ||
438 !EQUIV(in_h, track->track_h) ||
441 !EQUIV(out_w, track->track_w) ||
442 !EQUIV(out_h, track->track_h) ||
443 !EQUIV(in_w, asset_w) ||
444 !EQUIV(in_h, asset_h))
446 //printf("VModule::import_frame %d file -> temp -> output\n", __LINE__);
451 // Get temporary input buffer
456 VRender *vrender = (VRender*)commonrender;
457 //printf("VModule::import_frame %d vrender->input_temp=%p\n", __LINE__, vrender->input_temp);
458 input = &vrender->input_temp;
468 ((*input)->get_w() != asset_w ||
469 (*input)->get_h() != asset_h))
482 new VFrame(asset_w, asset_h,
483 get_edl()->session->color_model);
488 (*input)->copy_stacks(output);
491 // Cache for single frame only
494 if(debug) printf("VModule::import_frame %d this=%p file=%s\n",
497 current_edit->asset->path);
498 if(use_cache) file->set_cache_frames(1);
499 result = file->read_frame((*input));
500 if(use_cache) file->set_cache_frames(0);
501 (*input)->set_opengl_state(VFrame::RAM);
506 // If the colormodels differ, change input to nested colormodel
507 int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
508 int current_cmodel = output->get_color_model();
509 int output_w = output->get_w();
510 int output_h = output->get_h();
511 VFrame *input2 = (*input);
513 if(nested_cmodel != current_cmodel)
515 // If opengl, input -> input -> output
521 // If software, input2 -> input -> output
522 // Use output as a temporary.
526 if(debug) printf("VModule::import_frame %d this=%p nested_cmodel=%d\n",
531 input2->reallocate(0,
544 if(debug) printf("VModule::import_frame %d this=%p nested_edl=%s input2=%p\n",
550 result = nested_renderengine->vrender->process_buffer(
555 if(debug) printf("VModule::import_frame %d this=%p nested_edl=%s\n",
560 if(nested_cmodel != current_cmodel)
562 if(debug) printf("VModule::import_frame %d\n", __LINE__);
565 // Change colormodel in hardware.
566 if(debug) printf("VModule::import_frame %d\n", __LINE__);
567 x11_device->convert_cmodel(input2,
570 // The converted color model is now in hardware, so return the input2 buffer
571 // to the expected color model.
572 input2->reallocate(0,
584 // Transfer from input2 to input
585 if(debug) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d input2=%p input=%p output=%p\n",
592 BC_CModels::transfer((*input)->get_rows(),
613 //printf("VModule::import_frame %d\n", __LINE__);
615 // input2 was the output buffer, so it must be restored
616 input2->reallocate(0,
625 //printf("VModule::import_frame %d\n", __LINE__);
631 // Find an overlayer object to perform the camera transformation
632 OverlayFrame *overlayer = 0;
634 // OpenGL playback uses hardware
637 //printf("VModule::import_frame %d\n", __LINE__);
643 VRender *vrender = (VRender*)commonrender;
644 overlayer = vrender->overlayer;
650 printf("VModule::import_frame neither plugin_array nor commonrender is defined.\n");
653 overlay_temp = new OverlayFrame(plugin_array->mwindow->preferences->processors);
656 overlayer = overlay_temp;
658 // printf("VModule::import_frame 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
669 // for(int j = 0; j < output->get_w() * 3 * 5; j++)
670 // output->get_rows()[0][j] = 255;
674 x11_device->do_camera(output,
684 if(debug) printf("VModule::import_frame %d %d %d\n",
686 output->get_opengl_state(),
687 (*input)->get_opengl_state());
694 output->clear_frame();
697 // get_cache()->check_in(current_edit->asset);
700 // TRANSFER_REPLACE is the fastest transfer mode but it has the disadvantage
701 // of producing green borders in floating point translation of YUV
702 int mode = TRANSFER_REPLACE;
703 if(get_edl()->session->interpolation_type != NEAREST_NEIGHBOR &&
704 BC_CModels::is_yuv(output->get_color_model()))
705 mode = TRANSFER_NORMAL;
707 if(debug) printf("VModule::import_frame %d temp -> output\n", __LINE__);
708 overlayer->overlay(output,
720 get_edl()->session->interpolation_type);
724 output->copy_stacks((*input));
727 //printf("VModule::import_frame %d\n", __LINE__);
728 //(*input)->dump_params();
729 //output->dump_params();
734 if(debug) printf("VModule::import_frame %d file -> output nested_edl=%p file=%p\n",
740 VFrame **input = &output;
741 // If colormodels differ, reallocate output in nested colormodel.
742 int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
743 int current_cmodel = output->get_color_model();
745 if(debug) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d\n",
750 if(nested_cmodel != current_cmodel)
759 input = &((VRender*)commonrender)->input_temp;
766 if(!(*input)) (*input) = new VFrame;
769 (*input)->reallocate(0,
778 if(debug) printf("VModule::import_frame %d\n",
781 //(*input)->clear_frame();
784 if(debug) printf("VModule::import_frame %d %p %p\n",
786 (*input)->get_rows(),
788 result = nested_renderengine->vrender->process_buffer(
792 if(debug) printf("VModule::import_frame %d\n",
795 // If colormodels differ, change colormodels in opengl if possible.
796 // Swap output for temp if not possible.
797 if(nested_cmodel != current_cmodel)
801 x11_device->convert_cmodel(output,
804 // The color model was changed in place, so return output buffer
805 output->reallocate(0,
817 // Transfer from temporary to output
818 if(debug) printf("VModule::import_frame %d %d %d %d %d %d %d\n",
826 BC_CModels::transfer(output->get_rows(),
827 (*input)->get_rows(),
855 // Cache single frames
856 //memset(output->get_rows()[0], 0xff, 1024);
857 if(use_cache) file->set_cache_frames(1);
858 result = file->read_frame(output);
859 if(use_cache) file->set_cache_frames(0);
860 output->set_opengl_state(VFrame::RAM);
866 get_cache()->check_in(current_edit->asset);
873 if(debug) printf("VModule::import_frame %d\n", __LINE__);
876 x11_device->clear_input(output);
880 output->clear_frame();
885 // printf("VModule::import_frame %d cache=%p\n",
893 if(debug) printf("VModule::import_frame %d\n", __LINE__);
896 x11_device->clear_input(output);
900 output->clear_frame();
904 if(debug) printf("VModule::import_frame %d done\n", __LINE__);
911 int VModule::render(VFrame *output,
912 int64_t start_position,
920 double edl_rate = get_edl()->session->frame_rate;
922 //printf("VModule::render %d %ld\n", __LINE__, start_position);
924 if(use_nudge) start_position += Units::to_int64(track->nudge *
925 frame_rate / edl_rate);
927 int64_t start_position_project = Units::to_int64(start_position *
928 edl_rate / frame_rate + 0.5);
930 update_transition(start_position_project,
933 VEdit* current_edit = (VEdit*)track->edits->editof(start_position_project,
936 VEdit* previous_edit = 0;
937 //printf("VModule::render %d %p %ld %d\n", __LINE__, current_edit, start_position_project, direction);
940 printf(" VModule::render %d %d %jd %s transition=%p opengl=%d current_edit=%p output=%p\n",
943 start_position_project,
952 output->clear_frame();
953 // We do not apply mask here, since alpha is 0, and neither substracting nor multypling changes it
954 // Another mask mode - "addition" should be added to be able to create mask from empty frames
955 // in this case we would call masking here too...
962 // Process transition
963 if(transition && transition->on)
966 // Get temporary buffer
967 VFrame **transition_input = 0;
970 VRender *vrender = (VRender*)commonrender;
971 transition_input = &vrender->transition_temp;
975 transition_input = &transition_temp;
978 if((*transition_input) &&
979 ((*transition_input)->get_w() != track->track_w ||
980 (*transition_input)->get_h() != track->track_h))
982 delete (*transition_input);
983 (*transition_input) = 0;
986 // Load incoming frame
987 if(!(*transition_input))
989 (*transition_input) =
990 new VFrame(track->track_w, track->track_h,
991 get_edl()->session->color_model);
994 (*transition_input)->copy_stacks(output);
996 //printf("VModule::render %d\n", __LINE__);
997 result = import_frame((*transition_input),
1005 // Load transition buffer
1006 previous_edit = (VEdit*)current_edit->previous;
1008 result |= import_frame(output,
1014 //printf("VModule::render %d\n", __LINE__);
1016 // printf("VModule::render %d %p %p %p %p\n",
1018 // (*transition_input),
1019 // (*transition_input)->get_pbuffer(),
1021 // output->get_pbuffer());
1024 // Execute plugin with transition_input and output here
1026 transition_server->set_use_opengl(use_opengl, renderengine->video);
1027 transition_server->process_transition((*transition_input),
1029 (direction == PLAY_FORWARD) ?
1030 (start_position_project - current_edit->startproject) :
1031 (start_position_project - current_edit->startproject - 1),
1032 transition->length);
1036 // Load output buffer
1037 result = import_frame(output,
1046 MaskAutos *keyframe_set =
1047 (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
1048 int64_t mask_position = !renderengine ? start_position :
1049 renderengine->vrender->current_position;
1050 MaskAuto *keyframe =
1051 (MaskAuto*)keyframe_set->get_prev_auto(mask_position, direction, current);
1053 if( keyframe->apply_before_plugins ) {
1054 VDeviceX11 *x11_device = 0;
1055 if(use_opengl && renderengine && renderengine->video) {
1056 x11_device = (VDeviceX11*)renderengine->video->get_output_base();
1057 if( !x11_device->can_mask(mask_position, keyframe_set) )
1060 if( use_opengl && x11_device ) {
1061 x11_device->do_mask(output, mask_position, keyframe_set,
1062 keyframe, keyframe);
1066 int cpus = renderengine ?
1067 renderengine->preferences->processors :
1068 plugin_array->mwindow->preferences->processors;
1069 masker = new MaskEngine(cpus);
1071 masker->do_mask(output, mask_position, keyframe_set, keyframe, keyframe);
1083 void VModule::create_objects()
1085 Module::create_objects();