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 FloatAuto *previous = 0;
271 FloatAutos *speed_autos = (FloatAutos*)track->automation->autos[AUTOMATION_SPEED];
272 for(int64_t i = edit_startproject; i < direction_position; i++)
274 double speed = speed_autos->get_value(i,
278 speed_position += speed;
280 //printf("VModule::import_frame %d %lld %lld\n", __LINE__, position, (int64_t)speed_position);
281 position = (int64_t)speed_position;
290 if(debug) printf("VModule::import_frame %d\n", __LINE__);
293 // maybe apply speed curve here, so timeline reflects actual playback
297 // if we hit the end of stream, freeze at last frame
298 uint64_t max_position = 0;
301 max_position = Units::to_int64((double)file->get_video_length() *
303 current_edit->asset->frame_rate - 1);
307 max_position = Units::to_int64(nested_edl->tracks->total_playable_length() *
312 if(position > max_position) position = max_position;
314 if(position < 0) position = 0;
316 int use_cache = renderengine &&
317 renderengine->command->single_frame();
318 int use_asynchronous = !use_cache && renderengine &&
319 // Try to make rendering go faster.
320 // But converts some formats to YUV420, which may degrade input format.
321 // renderengine->command->realtime &&
322 renderengine->get_edl()->session->video_asynchronous;
326 if(debug) printf("VModule::import_frame %d\n", __LINE__);
328 file->start_video_decode_thread();
330 file->stop_video_thread();
332 int64_t normalized_position = Units::to_int64(position *
333 current_edit->asset->frame_rate /
335 // printf("VModule::import_frame %d %lld %lld\n",
338 // normalized_position);
339 file->set_layer(current_edit->channel);
340 file->set_video_position(normalized_position,
342 asset_w = current_edit->asset->width;
343 asset_h = current_edit->asset->height;
344 //printf("VModule::import_frame %d normalized_position=%lld\n", __LINE__, normalized_position);
348 if(debug) printf("VModule::import_frame %d\n", __LINE__);
349 asset_w = nested_edl->session->output_w;
350 asset_h = nested_edl->session->output_h;
351 // Get source position in nested frame rate in direction of playback.
352 nested_position = Units::to_int64(position *
353 nested_edl->session->frame_rate /
355 if(direction == PLAY_REVERSE)
360 // Auto scale if required
361 if(output->get_params()->get("AUTOSCALE", 0))
363 float autoscale_w = output->get_params()->get("AUTOSCALE_W", 1024);
364 float autoscale_h = output->get_params()->get("AUTOSCALE_H", 1024);
365 float x_scale = autoscale_w / asset_w;
366 float y_scale = autoscale_h / asset_h;
374 if(x_scale < y_scale)
376 out_w = in_w * x_scale;
377 out_h = in_h * x_scale;
381 out_w = in_w * y_scale;
382 out_h = in_h * y_scale;
385 out_x = track->track_w / 2 - out_w / 2;
386 out_y = track->track_h / 2 - out_h / 2;
391 ((VTrack*)track)->calculate_input_transfer(asset_w,
393 input_position_project,
405 // printf("VModule::import_frame %d %f %d %f %d\n",
412 // printf("VModule::import_frame 1 [ilace] Project: mode (%d) Asset: autofixoption (%d), mode (%d), method (%d)\n",
413 // get_edl()->session->interlace_mode,
414 // current_edit->asset->interlace_autofixoption,
415 // current_edit->asset->interlace_mode,
416 // current_edit->asset->interlace_fixmethod);
418 // Determine the interlacing method to use.
419 int interlace_fixmethod = !current_edit->asset ? ILACE_FIXMETHOD_NONE :
420 ilaceautofixmethod2(get_edl()->session->interlace_mode,
421 current_edit->asset->interlace_autofixoption,
422 current_edit->asset->interlace_mode,
423 current_edit->asset->interlace_fixmethod);
425 // char string[BCTEXTLEN];
426 // ilacefixmethod_to_text(string,interlace_fixmethod);
427 // printf("VModule::import_frame 1 [ilace] Compensating by using: '%s'\n",string);
429 // Compensate for the said interlacing...
430 switch (interlace_fixmethod) {
431 case ILACE_FIXMETHOD_NONE:
434 case ILACE_FIXMETHOD_UPONE:
437 case ILACE_FIXMETHOD_DOWNONE:
441 printf("vmodule::importframe WARNING - unknown fix method for interlacing, no compensation in effect\n");
444 // file -> temp -> output
445 if( !EQUIV(in_x, 0) ||
447 !EQUIV(in_w, track->track_w) ||
448 !EQUIV(in_h, track->track_h) ||
451 !EQUIV(out_w, track->track_w) ||
452 !EQUIV(out_h, track->track_h) ||
453 !EQUIV(in_w, asset_w) ||
454 !EQUIV(in_h, asset_h))
456 if(debug) printf("VModule::import_frame %d file -> temp -> output\n", __LINE__);
461 // Get temporary input buffer
466 VRender *vrender = (VRender*)commonrender;
467 //printf("VModule::import_frame %d vrender->input_temp=%p\n", __LINE__, vrender->input_temp);
468 input = &vrender->input_temp;
478 ((*input)->get_w() != asset_w ||
479 (*input)->get_h() != asset_h))
491 (*input) = new VFrame(0,
495 get_edl()->session->color_model,
501 (*input)->copy_stacks(output);
504 // Cache for single frame only
507 if(debug) printf("VModule::import_frame %d this=%p file=%s\n",
510 current_edit->asset->path);
511 if(use_cache) file->set_cache_frames(1);
512 result = file->read_frame((*input));
513 if(use_cache) file->set_cache_frames(0);
514 (*input)->set_opengl_state(VFrame::RAM);
519 // If the colormodels differ, change input to nested colormodel
520 int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
521 int current_cmodel = output->get_color_model();
522 int output_w = output->get_w();
523 int output_h = output->get_h();
524 VFrame *input2 = (*input);
526 if(nested_cmodel != current_cmodel)
528 // If opengl, input -> input -> output
534 // If software, input2 -> input -> output
535 // Use output as a temporary.
539 if(debug) printf("VModule::import_frame %d this=%p nested_cmodel=%d\n",
544 input2->reallocate(0,
557 if(debug) printf("VModule::import_frame %d this=%p nested_edl=%s input2=%p\n",
563 result = nested_renderengine->vrender->process_buffer(
568 if(debug) printf("VModule::import_frame %d this=%p nested_edl=%s\n",
573 if(nested_cmodel != current_cmodel)
575 if(debug) printf("VModule::import_frame %d\n", __LINE__);
578 // Change colormodel in hardware.
579 if(debug) printf("VModule::import_frame %d\n", __LINE__);
580 x11_device->convert_cmodel(input2,
583 // The converted color model is now in hardware, so return the input2 buffer
584 // to the expected color model.
585 input2->reallocate(0,
597 // Transfer from input2 to input
598 if(debug) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d input2=%p input=%p output=%p\n",
605 BC_CModels::transfer((*input)->get_rows(),
626 //printf("VModule::import_frame %d\n", __LINE__);
628 // input2 was the output buffer, so it must be restored
629 input2->reallocate(0,
638 //printf("VModule::import_frame %d\n", __LINE__);
644 // Find an overlayer object to perform the camera transformation
645 OverlayFrame *overlayer = 0;
647 // OpenGL playback uses hardware
650 //printf("VModule::import_frame %d\n", __LINE__);
656 VRender *vrender = (VRender*)commonrender;
657 overlayer = vrender->overlayer;
663 printf("VModule::import_frame neither plugin_array nor commonrender is defined.\n");
666 overlay_temp = new OverlayFrame(plugin_array->mwindow->preferences->processors);
669 overlayer = overlay_temp;
671 // printf("VModule::import_frame 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
682 // for(int j = 0; j < output->get_w() * 3 * 5; j++)
683 // output->get_rows()[0][j] = 255;
687 x11_device->do_camera(output,
697 if(debug) printf("VModule::import_frame %d %d %d\n",
699 output->get_opengl_state(),
700 (*input)->get_opengl_state());
707 output->clear_frame();
710 // get_cache()->check_in(current_edit->asset);
713 // TRANSFER_REPLACE is the fastest transfer mode but it has the disadvantage
714 // of producing green borders in floating point translation of YUV
715 int mode = TRANSFER_REPLACE;
716 if(get_edl()->session->interpolation_type != NEAREST_NEIGHBOR &&
717 BC_CModels::is_yuv(output->get_color_model()))
718 mode = TRANSFER_NORMAL;
720 if(debug) printf("VModule::import_frame %d temp -> output\n", __LINE__);
721 overlayer->overlay(output,
733 get_edl()->session->interpolation_type);
736 output->copy_stacks((*input));
741 if(debug) printf("VModule::import_frame %d file -> output nested_edl=%p file=%p\n",
747 VFrame **input = &output;
748 // If colormodels differ, reallocate output in nested colormodel.
749 int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
750 int current_cmodel = output->get_color_model();
752 if(debug) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d\n",
757 if(nested_cmodel != current_cmodel)
766 input = &((VRender*)commonrender)->input_temp;
773 if(!(*input)) (*input) = new VFrame;
776 (*input)->reallocate(0,
785 if(debug) printf("VModule::import_frame %d\n",
788 //(*input)->clear_frame();
791 if(debug) printf("VModule::import_frame %d %p %p\n",
793 (*input)->get_rows(),
795 result = nested_renderengine->vrender->process_buffer(
799 if(debug) printf("VModule::import_frame %d\n",
802 // If colormodels differ, change colormodels in opengl if possible.
803 // Swap output for temp if not possible.
804 if(nested_cmodel != current_cmodel)
808 x11_device->convert_cmodel(output,
811 // The color model was changed in place, so return output buffer
812 output->reallocate(0,
824 // Transfer from temporary to output
825 if(debug) printf("VModule::import_frame %d %d %d %d %d %d %d\n",
833 BC_CModels::transfer(output->get_rows(),
834 (*input)->get_rows(),
862 // Cache single frames
863 //memset(output->get_rows()[0], 0xff, 1024);
864 if(use_cache) file->set_cache_frames(1);
865 result = file->read_frame(output);
866 if(use_cache) file->set_cache_frames(0);
867 output->set_opengl_state(VFrame::RAM);
873 get_cache()->check_in(current_edit->asset);
880 if(debug) printf("VModule::import_frame %d\n", __LINE__);
883 x11_device->clear_input(output);
887 output->clear_frame();
895 if(debug) printf("VModule::import_frame %d\n", __LINE__);
898 x11_device->clear_input(output);
902 output->clear_frame();
906 if(debug) printf("VModule::import_frame %d done\n", __LINE__);
913 int VModule::render(VFrame *output,
914 int64_t start_position,
922 double edl_rate = get_edl()->session->frame_rate;
924 //printf("VModule::render %d %ld\n", __LINE__, start_position);
926 if(use_nudge) start_position += Units::to_int64(track->nudge *
927 frame_rate / edl_rate);
929 int64_t start_position_project = Units::to_int64(start_position *
930 edl_rate / frame_rate + 0.5);
932 update_transition(start_position_project,
935 VEdit* current_edit = (VEdit*)track->edits->editof(start_position_project,
938 VEdit* previous_edit = 0;
939 //printf("VModule::render %d %p %ld %d\n", __LINE__, current_edit, start_position_project, direction);
942 printf(" VModule::render %d %d %jd %s transition=%p opengl=%d current_edit=%p output=%p\n",
945 start_position_project,
954 output->clear_frame();
955 // We do not apply mask here, since alpha is 0, and neither substracting nor multypling changes it
956 // Another mask mode - "addition" should be added to be able to create mask from empty frames
957 // in this case we would call masking here too...
964 // Process transition
965 if(transition && transition->on)
968 // Get temporary buffer
969 VFrame **transition_input = 0;
972 VRender *vrender = (VRender*)commonrender;
973 transition_input = &vrender->transition_temp;
977 transition_input = &transition_temp;
980 if((*transition_input) &&
981 ((*transition_input)->get_w() != track->track_w ||
982 (*transition_input)->get_h() != track->track_h))
984 delete (*transition_input);
985 (*transition_input) = 0;
988 // Load incoming frame
989 if(!(*transition_input))
991 (*transition_input) = new VFrame(0,
995 get_edl()->session->color_model,
999 (*transition_input)->copy_stacks(output);
1001 //printf("VModule::render %d\n", __LINE__);
1002 result = import_frame((*transition_input),
1010 // Load transition buffer
1011 previous_edit = (VEdit*)current_edit->previous;
1013 result |= import_frame(output,
1019 //printf("VModule::render %d\n", __LINE__);
1021 // printf("VModule::render %d %p %p %p %p\n",
1023 // (*transition_input),
1024 // (*transition_input)->get_pbuffer(),
1026 // output->get_pbuffer());
1029 // Execute plugin with transition_input and output here
1031 transition_server->set_use_opengl(use_opengl, renderengine->video);
1032 transition_server->process_transition((*transition_input),
1034 (direction == PLAY_FORWARD) ?
1035 (start_position_project - current_edit->startproject) :
1036 (start_position_project - current_edit->startproject - 1),
1037 transition->length);
1041 // Load output buffer
1042 result = import_frame(output,
1051 MaskAutos *keyframe_set =
1052 (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
1053 int64_t mask_position = !renderengine ? start_position :
1054 renderengine->vrender->current_position;
1055 MaskAuto *keyframe =
1056 (MaskAuto*)keyframe_set->get_prev_auto(mask_position, direction, current);
1058 if( keyframe->apply_before_plugins ) {
1059 VDeviceX11 *x11_device = 0;
1060 if(use_opengl && renderengine && renderengine->video) {
1061 x11_device = (VDeviceX11*)renderengine->video->get_output_base();
1062 if( !x11_device->can_mask(mask_position, keyframe_set) )
1065 if( use_opengl && x11_device ) {
1066 x11_device->do_mask(output, mask_position, keyframe_set,
1067 keyframe, keyframe);
1071 int cpus = renderengine ?
1072 renderengine->preferences->processors :
1073 plugin_array->mwindow->preferences->processors;
1074 masker = new MaskEngine(cpus);
1076 masker->do_mask(output, mask_position, keyframe_set, keyframe, keyframe);
1088 void VModule::create_objects()
1090 Module::create_objects();