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,
110 int64_t input_position,
115 int64_t direction_position;
116 // Translation of edit
127 double edl_rate = get_edl()->session->frame_rate;
129 int64_t input_position_project = Units::to_int64(input_position *
140 if(!output) printf("VModule::import_frame %d output=%p\n", __LINE__, output);
141 //output->dump_params();
144 if(debug) printf("VModule::import_frame %d this=%p input_position=%lld direction=%d\n",
147 (long long)input_position,
150 // Convert to position corrected for direction
151 direction_position = input_position;
152 if(direction == PLAY_REVERSE)
154 direction_position--;
155 input_position_project--;
157 if(!output) printf("VModule::import_frame %d output=%p\n", __LINE__, output);
159 VDeviceX11 *x11_device = 0;
162 if(renderengine && renderengine->video)
164 x11_device = (VDeviceX11*)renderengine->video->get_output_base();
165 output->set_opengl_state(VFrame::RAM);
166 if(!x11_device) use_opengl = 0;
170 if(!output) printf("VModule::import_frame %d output=%p x11_device=%p nested_edl=%p\n",
177 if(debug) printf("VModule::import_frame %d current_edit=%p\n",
182 // Load frame into output
184 // Create objects for nested EDL
186 current_edit->nested_edl)
189 if(debug) printf("VModule::import_frame %d nested_edl=%p current_edit->nested_edl=%p\n",
192 current_edit->nested_edl);
194 // Convert requested direction to command
195 if(renderengine->command->command == CURRENT_FRAME)
197 command = CURRENT_FRAME;
200 if(direction == PLAY_REVERSE)
202 if(renderengine->command->single_frame())
203 command = SINGLE_FRAME_REWIND;
205 command = NORMAL_REWIND;
209 if(renderengine->command->single_frame())
210 command = SINGLE_FRAME_FWD;
212 command = NORMAL_FWD;
215 if(!nested_edl || nested_edl->id != current_edit->nested_edl->id)
217 nested_edl = current_edit->nested_edl;
218 if(nested_renderengine)
220 delete nested_renderengine;
221 nested_renderengine = 0;
226 nested_command = new TransportCommand;
230 if(!nested_renderengine)
232 nested_command->command = command;
233 nested_command->get_edl()->copy_all(nested_edl);
234 nested_command->change_type = CHANGE_ALL;
235 nested_command->realtime = renderengine->command->realtime;
236 nested_renderengine = new RenderEngine(0,
239 renderengine ? renderengine->channeldb : 0,
241 nested_renderengine->set_vcache(get_cache());
242 nested_renderengine->arm_command(nested_command);
248 // Update nested command
249 nested_renderengine->command->command = command;
250 nested_command->realtime = renderengine->command->realtime;
253 // Update nested video driver for opengl
254 nested_renderengine->video = renderengine->video;
260 if(debug) printf("VModule::import_frame %d\n", __LINE__);
262 if(!output) printf("VModule::import_frame %d output=%p\n", __LINE__, output);
265 (current_edit->asset ||
266 (current_edit->nested_edl && nested_renderengine->vrender)))
270 if(debug) printf("VModule::import_frame %d cache=%p\n",
273 if(current_edit->asset)
276 file = get_cache()->check_out(current_edit->asset,
278 // get_cache()->dump();
282 if(file || nested_edl)
284 // Make all positions based on requested frame rate.
285 int64_t edit_startproject = Units::to_int64(current_edit->startproject *
288 int64_t edit_startsource = Units::to_int64(current_edit->startsource *
291 // Source position going forward
292 uint64_t position = direction_position -
295 int64_t nested_position = 0;
301 // apply speed curve to source position so the timeline agrees with the playback
302 if(track->has_speed())
304 // integrate position from start of edit.
305 double speed_position = edit_startsource;
306 FloatAuto *previous = 0;
308 FloatAutos *speed_autos = (FloatAutos*)track->automation->autos[AUTOMATION_SPEED];
309 for(int64_t i = edit_startproject; i < direction_position; i++)
311 double speed = speed_autos->get_value(i,
315 speed_position += speed;
317 //printf("VModule::import_frame %d %lld %lld\n", __LINE__, position, (int64_t)speed_position);
318 position = (int64_t)speed_position;
327 if(debug) printf("VModule::import_frame %d\n", __LINE__);
330 // maybe apply speed curve here, so timeline reflects actual playback
334 // if we hit the end of stream, freeze at last frame
335 uint64_t max_position = 0;
338 max_position = Units::to_int64((double)file->get_video_length() *
340 current_edit->asset->frame_rate - 1);
344 max_position = Units::to_int64(nested_edl->tracks->total_playable_length() *
349 if(position > max_position) position = max_position;
351 if(position < 0) position = 0;
353 int use_cache = renderengine &&
354 renderengine->command->single_frame();
355 int use_asynchronous = !use_cache && renderengine &&
356 // Try to make rendering go faster.
357 // But converts some formats to YUV420, which may degrade input format.
358 // renderengine->command->realtime &&
359 renderengine->get_edl()->session->video_asynchronous;
363 if(debug) printf("VModule::import_frame %d\n", __LINE__);
365 file->start_video_decode_thread();
367 file->stop_video_thread();
369 int64_t normalized_position = Units::to_int64(position *
370 current_edit->asset->frame_rate /
372 // printf("VModule::import_frame %d %lld %lld\n",
375 // normalized_position);
376 file->set_layer(current_edit->channel);
377 file->set_video_position(normalized_position,
379 asset_w = current_edit->asset->width;
380 asset_h = current_edit->asset->height;
381 //printf("VModule::import_frame %d normalized_position=%lld\n", __LINE__, normalized_position);
385 if(debug) printf("VModule::import_frame %d\n", __LINE__);
386 asset_w = nested_edl->session->output_w;
387 asset_h = nested_edl->session->output_h;
388 // Get source position in nested frame rate in direction of playback.
389 nested_position = Units::to_int64(position *
390 nested_edl->session->frame_rate /
392 if(direction == PLAY_REVERSE)
397 // Auto scale if required
398 if(output->get_params()->get("AUTOSCALE", 0))
400 float autoscale_w = output->get_params()->get("AUTOSCALE_W", 1024);
401 float autoscale_h = output->get_params()->get("AUTOSCALE_H", 1024);
402 float x_scale = autoscale_w / asset_w;
403 float y_scale = autoscale_h / asset_h;
411 if(x_scale < y_scale)
413 out_w = in_w * x_scale;
414 out_h = in_h * x_scale;
418 out_w = in_w * y_scale;
419 out_h = in_h * y_scale;
422 out_x = track->track_w / 2 - out_w / 2;
423 out_y = track->track_h / 2 - out_h / 2;
428 ((VTrack*)track)->calculate_input_transfer(asset_w,
430 input_position_project,
442 // printf("VModule::import_frame %d %f %d %f %d\n",
449 // printf("VModule::import_frame 1 [ilace] Project: mode (%d) Asset: autofixoption (%d), mode (%d), method (%d)\n",
450 // get_edl()->session->interlace_mode,
451 // current_edit->asset->interlace_autofixoption,
452 // current_edit->asset->interlace_mode,
453 // current_edit->asset->interlace_fixmethod);
455 // Determine the interlacing method to use.
456 int interlace_fixmethod = !current_edit->asset ? BC_ILACE_FIXMETHOD_NONE :
457 ilaceautofixmethod2(get_edl()->session->interlace_mode,
458 current_edit->asset->interlace_autofixoption,
459 current_edit->asset->interlace_mode,
460 current_edit->asset->interlace_fixmethod);
462 // char string[BCTEXTLEN];
463 // ilacefixmethod_to_text(string,interlace_fixmethod);
464 // printf("VModule::import_frame 1 [ilace] Compensating by using: '%s'\n",string);
466 // Compensate for the said interlacing...
467 switch (interlace_fixmethod) {
468 case BC_ILACE_FIXMETHOD_NONE:
471 case BC_ILACE_FIXMETHOD_UPONE:
474 case BC_ILACE_FIXMETHOD_DOWNONE:
478 printf("vmodule::importframe WARNING - unknown fix method for interlacing, no compensation in effect\n");
481 // file -> temp -> output
482 if( !EQUIV(in_x, 0) ||
484 !EQUIV(in_w, track->track_w) ||
485 !EQUIV(in_h, track->track_h) ||
488 !EQUIV(out_w, track->track_w) ||
489 !EQUIV(out_h, track->track_h) ||
490 !EQUIV(in_w, asset_w) ||
491 !EQUIV(in_h, asset_h))
493 if(debug) printf("VModule::import_frame %d file -> temp -> output\n", __LINE__);
498 // Get temporary input buffer
503 VRender *vrender = (VRender*)commonrender;
504 //printf("VModule::import_frame %d vrender->input_temp=%p\n", __LINE__, vrender->input_temp);
505 input = &vrender->input_temp;
515 ((*input)->get_w() != asset_w ||
516 (*input)->get_h() != asset_h))
528 (*input) = new VFrame(0,
532 get_edl()->session->color_model,
538 (*input)->copy_stacks(output);
541 // Cache for single frame only
544 if(debug) printf("VModule::import_frame %d this=%p file=%s\n",
547 current_edit->asset->path);
548 if(use_cache) file->set_cache_frames(1);
549 result = file->read_frame((*input));
550 if(use_cache) file->set_cache_frames(0);
551 (*input)->set_opengl_state(VFrame::RAM);
556 // If the colormodels differ, change input to nested colormodel
557 int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
558 int current_cmodel = output->get_color_model();
559 int output_w = output->get_w();
560 int output_h = output->get_h();
561 VFrame *input2 = (*input);
563 if(nested_cmodel != current_cmodel)
565 // If opengl, input -> input -> output
571 // If software, input2 -> input -> output
572 // Use output as a temporary.
576 if(debug) printf("VModule::import_frame %d this=%p nested_cmodel=%d\n",
581 input2->reallocate(0,
594 if(debug) printf("VModule::import_frame %d this=%p nested_edl=%s input2=%p\n",
600 result = nested_renderengine->vrender->process_buffer(
605 if(debug) printf("VModule::import_frame %d this=%p nested_edl=%s\n",
610 if(nested_cmodel != current_cmodel)
612 if(debug) printf("VModule::import_frame %d\n", __LINE__);
615 // Change colormodel in hardware.
616 if(debug) printf("VModule::import_frame %d\n", __LINE__);
617 x11_device->convert_cmodel(input2,
620 // The converted color model is now in hardware, so return the input2 buffer
621 // to the expected color model.
622 input2->reallocate(0,
634 // Transfer from input2 to input
635 if(debug) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d input2=%p input=%p output=%p\n",
642 BC_CModels::transfer((*input)->get_rows(),
663 //printf("VModule::import_frame %d\n", __LINE__);
665 // input2 was the output buffer, so it must be restored
666 input2->reallocate(0,
675 //printf("VModule::import_frame %d\n", __LINE__);
681 // Find an overlayer object to perform the camera transformation
682 OverlayFrame *overlayer = 0;
684 // OpenGL playback uses hardware
687 //printf("VModule::import_frame %d\n", __LINE__);
693 VRender *vrender = (VRender*)commonrender;
694 overlayer = vrender->overlayer;
700 printf("VModule::import_frame neither plugin_array nor commonrender is defined.\n");
703 overlay_temp = new OverlayFrame(plugin_array->mwindow->preferences->processors);
706 overlayer = overlay_temp;
708 // printf("VModule::import_frame 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
719 // for(int j = 0; j < output->get_w() * 3 * 5; j++)
720 // output->get_rows()[0][j] = 255;
724 x11_device->do_camera(output,
734 if(debug) printf("VModule::import_frame %d %d %d\n",
736 output->get_opengl_state(),
737 (*input)->get_opengl_state());
744 output->clear_frame();
747 // get_cache()->check_in(current_edit->asset);
750 // TRANSFER_REPLACE is the fastest transfer mode but it has the disadvantage
751 // of producing green borders in floating point translation of YUV
752 int mode = TRANSFER_REPLACE;
753 if(get_edl()->session->interpolation_type != NEAREST_NEIGHBOR &&
754 BC_CModels::is_yuv(output->get_color_model()))
755 mode = TRANSFER_NORMAL;
757 if(debug) printf("VModule::import_frame %d temp -> output\n", __LINE__);
758 overlayer->overlay(output,
770 get_edl()->session->interpolation_type);
773 output->copy_stacks((*input));
778 if(debug) printf("VModule::import_frame %d file -> output nested_edl=%p file=%p\n",
784 VFrame **input = &output;
785 // If colormodels differ, reallocate output in nested colormodel.
786 int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
787 int current_cmodel = output->get_color_model();
789 if(debug) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d\n",
794 if(nested_cmodel != current_cmodel)
803 input = &((VRender*)commonrender)->input_temp;
810 if(!(*input)) (*input) = new VFrame;
813 (*input)->reallocate(0,
822 if(debug) printf("VModule::import_frame %d\n",
825 //(*input)->clear_frame();
828 if(debug) printf("VModule::import_frame %d %p %p\n",
830 (*input)->get_rows(),
832 result = nested_renderengine->vrender->process_buffer(
836 if(debug) printf("VModule::import_frame %d\n",
839 // If colormodels differ, change colormodels in opengl if possible.
840 // Swap output for temp if not possible.
841 if(nested_cmodel != current_cmodel)
845 x11_device->convert_cmodel(output,
848 // The color model was changed in place, so return output buffer
849 output->reallocate(0,
861 // Transfer from temporary to output
862 if(debug) printf("VModule::import_frame %d %d %d %d %d %d %d\n",
870 BC_CModels::transfer(output->get_rows(),
871 (*input)->get_rows(),
899 // Cache single frames
900 //memset(output->get_rows()[0], 0xff, 1024);
901 if(use_cache) file->set_cache_frames(1);
902 result = file->read_frame(output);
903 if(use_cache) file->set_cache_frames(0);
904 output->set_opengl_state(VFrame::RAM);
910 get_cache()->check_in(current_edit->asset);
917 if(debug) printf("VModule::import_frame %d\n", __LINE__);
920 x11_device->clear_input(output);
924 output->clear_frame();
932 if(debug) printf("VModule::import_frame %d\n", __LINE__);
935 x11_device->clear_input(output);
939 output->clear_frame();
943 if(debug) printf("VModule::import_frame %d done\n", __LINE__);
950 int VModule::render(VFrame *output,
951 int64_t start_position,
959 double edl_rate = get_edl()->session->frame_rate;
961 //printf("VModule::render %d %ld\n", __LINE__, start_position);
963 if(use_nudge) start_position += Units::to_int64(track->nudge *
964 frame_rate / edl_rate);
966 int64_t start_position_project = Units::to_int64(start_position *
967 edl_rate / frame_rate + 0.5);
969 update_transition(start_position_project,
972 VEdit* current_edit = (VEdit*)track->edits->editof(start_position_project,
975 VEdit* previous_edit = 0;
976 //printf("VModule::render %d %p %ld %d\n", __LINE__, current_edit, start_position_project, direction);
979 printf(" VModule::render %d %d %ld %s transition=%p opengl=%d current_edit=%p output=%p\n",
982 start_position_project,
991 output->clear_frame();
992 // We do not apply mask here, since alpha is 0, and neither substracting nor multypling changes it
993 // Another mask mode - "addition" should be added to be able to create mask from empty frames
994 // in this case we would call masking here too...
1001 // Process transition
1002 if(transition && transition->on)
1005 // Get temporary buffer
1006 VFrame **transition_input = 0;
1009 VRender *vrender = (VRender*)commonrender;
1010 transition_input = &vrender->transition_temp;
1014 transition_input = &transition_temp;
1017 if((*transition_input) &&
1018 ((*transition_input)->get_w() != track->track_w ||
1019 (*transition_input)->get_h() != track->track_h))
1021 delete (*transition_input);
1022 (*transition_input) = 0;
1025 // Load incoming frame
1026 if(!(*transition_input))
1028 (*transition_input) = new VFrame(0,
1032 get_edl()->session->color_model,
1036 (*transition_input)->copy_stacks(output);
1038 //printf("VModule::render %d\n", __LINE__);
1039 result = import_frame((*transition_input),
1047 // Load transition buffer
1048 previous_edit = (VEdit*)current_edit->previous;
1050 result |= import_frame(output,
1056 //printf("VModule::render %d\n", __LINE__);
1058 // printf("VModule::render %d %p %p %p %p\n",
1060 // (*transition_input),
1061 // (*transition_input)->get_pbuffer(),
1063 // output->get_pbuffer());
1066 // Execute plugin with transition_input and output here
1068 transition_server->set_use_opengl(use_opengl, renderengine->video);
1069 transition_server->process_transition((*transition_input),
1071 (direction == PLAY_FORWARD) ?
1072 (start_position_project - current_edit->startproject) :
1073 (start_position_project - current_edit->startproject - 1),
1074 transition->length);
1078 // Load output buffer
1079 result = import_frame(output,
1088 MaskAutos *keyframe_set =
1089 (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
1090 int64_t mask_position = !renderengine ? start_position :
1091 renderengine->vrender->current_position;
1092 MaskAuto *keyframe =
1093 (MaskAuto*)keyframe_set->get_prev_auto(mask_position, direction, current);
1095 if( keyframe->apply_before_plugins ) {
1096 VDeviceX11 *x11_device = 0;
1097 if(use_opengl && renderengine && renderengine->video) {
1098 x11_device = (VDeviceX11*)renderengine->video->get_output_base();
1099 if( !x11_device->can_mask(mask_position, keyframe_set) )
1102 if( use_opengl && x11_device ) {
1103 x11_device->do_mask(output, mask_position, keyframe_set,
1104 keyframe, keyframe);
1108 int cpus = renderengine ?
1109 renderengine->preferences->processors :
1110 plugin_array->mwindow->preferences->processors;
1111 masker = new MaskEngine(cpus);
1113 masker->do_mask(output, mask_position, keyframe_set, keyframe, keyframe);
1125 void VModule::create_objects()
1127 Module::create_objects();