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, get_preferences(), 0, 1);
237 nested_renderengine->set_vcache(get_cache());
238 nested_renderengine->arm_command(nested_command);
244 // Update nested command
245 nested_renderengine->command->command = command;
246 nested_command->realtime = renderengine->command->realtime;
249 // Update nested video driver for opengl
250 nested_renderengine->video = renderengine->video;
256 if(debug) printf("VModule::import_frame %d\n", __LINE__);
258 if(!output) printf("VModule::import_frame %d output=%p\n", __LINE__, output);
261 (current_edit->asset ||
262 (current_edit->nested_edl && nested_renderengine->vrender)))
266 if(debug) printf("VModule::import_frame %d cache=%p\n",
269 if(current_edit->asset)
272 file = get_cache()->check_out(current_edit->asset,
274 // get_cache()->dump();
278 if(file || nested_edl)
280 // Make all positions based on requested frame rate.
281 int64_t edit_startproject = Units::to_int64(current_edit->startproject *
284 int64_t edit_startsource = Units::to_int64(current_edit->startsource *
287 // Source position going forward
288 uint64_t position = direction_position -
291 int64_t nested_position = 0;
297 // apply speed curve to source position so the timeline agrees with the playback
298 if(track->has_speed())
300 // integrate position from start of edit.
301 double speed_position = edit_startsource;
302 FloatAuto *previous = 0;
304 FloatAutos *speed_autos = (FloatAutos*)track->automation->autos[AUTOMATION_SPEED];
305 for(int64_t i = edit_startproject; i < direction_position; i++)
307 double speed = speed_autos->get_value(i,
311 speed_position += speed;
313 //printf("VModule::import_frame %d %lld %lld\n", __LINE__, position, (int64_t)speed_position);
314 position = (int64_t)speed_position;
323 if(debug) printf("VModule::import_frame %d\n", __LINE__);
326 // maybe apply speed curve here, so timeline reflects actual playback
330 // if we hit the end of stream, freeze at last frame
331 uint64_t max_position = 0;
334 max_position = Units::to_int64((double)file->get_video_length() *
336 current_edit->asset->frame_rate - 1);
340 max_position = Units::to_int64(nested_edl->tracks->total_playable_length() *
345 if(position > max_position) position = max_position;
347 if(position < 0) position = 0;
349 int use_cache = renderengine &&
350 renderengine->command->single_frame();
351 int use_asynchronous = !use_cache && renderengine &&
352 // Try to make rendering go faster.
353 // But converts some formats to YUV420, which may degrade input format.
354 // renderengine->command->realtime &&
355 renderengine->get_edl()->session->video_asynchronous;
359 if(debug) printf("VModule::import_frame %d\n", __LINE__);
361 file->start_video_decode_thread();
363 file->stop_video_thread();
365 int64_t normalized_position = Units::to_int64(position *
366 current_edit->asset->frame_rate /
368 // printf("VModule::import_frame %d %lld %lld\n",
371 // normalized_position);
372 file->set_layer(current_edit->channel);
373 file->set_video_position(normalized_position,
375 asset_w = current_edit->asset->width;
376 asset_h = current_edit->asset->height;
377 //printf("VModule::import_frame %d normalized_position=%lld\n", __LINE__, normalized_position);
381 if(debug) printf("VModule::import_frame %d\n", __LINE__);
382 asset_w = nested_edl->session->output_w;
383 asset_h = nested_edl->session->output_h;
384 // Get source position in nested frame rate in direction of playback.
385 nested_position = Units::to_int64(position *
386 nested_edl->session->frame_rate /
388 if(direction == PLAY_REVERSE)
393 // Auto scale if required
394 if(output->get_params()->get("AUTOSCALE", 0))
396 float autoscale_w = output->get_params()->get("AUTOSCALE_W", 1024);
397 float autoscale_h = output->get_params()->get("AUTOSCALE_H", 1024);
398 float x_scale = autoscale_w / asset_w;
399 float y_scale = autoscale_h / asset_h;
407 if(x_scale < y_scale)
409 out_w = in_w * x_scale;
410 out_h = in_h * x_scale;
414 out_w = in_w * y_scale;
415 out_h = in_h * y_scale;
418 out_x = track->track_w / 2 - out_w / 2;
419 out_y = track->track_h / 2 - out_h / 2;
424 ((VTrack*)track)->calculate_input_transfer(asset_w,
426 input_position_project,
438 // printf("VModule::import_frame %d %f %d %f %d\n",
445 // printf("VModule::import_frame 1 [ilace] Project: mode (%d) Asset: autofixoption (%d), mode (%d), method (%d)\n",
446 // get_edl()->session->interlace_mode,
447 // current_edit->asset->interlace_autofixoption,
448 // current_edit->asset->interlace_mode,
449 // current_edit->asset->interlace_fixmethod);
451 // Determine the interlacing method to use.
452 int interlace_fixmethod = !current_edit->asset ? ILACE_FIXMETHOD_NONE :
453 ilaceautofixmethod2(get_edl()->session->interlace_mode,
454 current_edit->asset->interlace_autofixoption,
455 current_edit->asset->interlace_mode,
456 current_edit->asset->interlace_fixmethod);
458 // char string[BCTEXTLEN];
459 // ilacefixmethod_to_text(string,interlace_fixmethod);
460 // printf("VModule::import_frame 1 [ilace] Compensating by using: '%s'\n",string);
462 // Compensate for the said interlacing...
463 switch (interlace_fixmethod) {
464 case ILACE_FIXMETHOD_NONE:
467 case ILACE_FIXMETHOD_UPONE:
470 case ILACE_FIXMETHOD_DOWNONE:
474 printf("vmodule::importframe WARNING - unknown fix method for interlacing, no compensation in effect\n");
477 // file -> temp -> output
478 if( !EQUIV(in_x, 0) ||
480 !EQUIV(in_w, track->track_w) ||
481 !EQUIV(in_h, track->track_h) ||
484 !EQUIV(out_w, track->track_w) ||
485 !EQUIV(out_h, track->track_h) ||
486 !EQUIV(in_w, asset_w) ||
487 !EQUIV(in_h, asset_h))
489 if(debug) printf("VModule::import_frame %d file -> temp -> output\n", __LINE__);
494 // Get temporary input buffer
499 VRender *vrender = (VRender*)commonrender;
500 //printf("VModule::import_frame %d vrender->input_temp=%p\n", __LINE__, vrender->input_temp);
501 input = &vrender->input_temp;
511 ((*input)->get_w() != asset_w ||
512 (*input)->get_h() != asset_h))
524 (*input) = new VFrame(0,
528 get_edl()->session->color_model,
534 (*input)->copy_stacks(output);
537 // Cache for single frame only
540 if(debug) printf("VModule::import_frame %d this=%p file=%s\n",
543 current_edit->asset->path);
544 if(use_cache) file->set_cache_frames(1);
545 result = file->read_frame((*input));
546 if(use_cache) file->set_cache_frames(0);
547 (*input)->set_opengl_state(VFrame::RAM);
552 // If the colormodels differ, change input to nested colormodel
553 int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
554 int current_cmodel = output->get_color_model();
555 int output_w = output->get_w();
556 int output_h = output->get_h();
557 VFrame *input2 = (*input);
559 if(nested_cmodel != current_cmodel)
561 // If opengl, input -> input -> output
567 // If software, input2 -> input -> output
568 // Use output as a temporary.
572 if(debug) printf("VModule::import_frame %d this=%p nested_cmodel=%d\n",
577 input2->reallocate(0,
590 if(debug) printf("VModule::import_frame %d this=%p nested_edl=%s input2=%p\n",
596 result = nested_renderengine->vrender->process_buffer(
601 if(debug) printf("VModule::import_frame %d this=%p nested_edl=%s\n",
606 if(nested_cmodel != current_cmodel)
608 if(debug) printf("VModule::import_frame %d\n", __LINE__);
611 // Change colormodel in hardware.
612 if(debug) printf("VModule::import_frame %d\n", __LINE__);
613 x11_device->convert_cmodel(input2,
616 // The converted color model is now in hardware, so return the input2 buffer
617 // to the expected color model.
618 input2->reallocate(0,
630 // Transfer from input2 to input
631 if(debug) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d input2=%p input=%p output=%p\n",
638 BC_CModels::transfer((*input)->get_rows(),
659 //printf("VModule::import_frame %d\n", __LINE__);
661 // input2 was the output buffer, so it must be restored
662 input2->reallocate(0,
671 //printf("VModule::import_frame %d\n", __LINE__);
677 // Find an overlayer object to perform the camera transformation
678 OverlayFrame *overlayer = 0;
680 // OpenGL playback uses hardware
683 //printf("VModule::import_frame %d\n", __LINE__);
689 VRender *vrender = (VRender*)commonrender;
690 overlayer = vrender->overlayer;
696 printf("VModule::import_frame neither plugin_array nor commonrender is defined.\n");
699 overlay_temp = new OverlayFrame(plugin_array->mwindow->preferences->processors);
702 overlayer = overlay_temp;
704 // printf("VModule::import_frame 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
715 // for(int j = 0; j < output->get_w() * 3 * 5; j++)
716 // output->get_rows()[0][j] = 255;
720 x11_device->do_camera(output,
730 if(debug) printf("VModule::import_frame %d %d %d\n",
732 output->get_opengl_state(),
733 (*input)->get_opengl_state());
740 output->clear_frame();
743 // get_cache()->check_in(current_edit->asset);
746 // TRANSFER_REPLACE is the fastest transfer mode but it has the disadvantage
747 // of producing green borders in floating point translation of YUV
748 int mode = TRANSFER_REPLACE;
749 if(get_edl()->session->interpolation_type != NEAREST_NEIGHBOR &&
750 BC_CModels::is_yuv(output->get_color_model()))
751 mode = TRANSFER_NORMAL;
753 if(debug) printf("VModule::import_frame %d temp -> output\n", __LINE__);
754 overlayer->overlay(output,
766 get_edl()->session->interpolation_type);
769 output->copy_stacks((*input));
774 if(debug) printf("VModule::import_frame %d file -> output nested_edl=%p file=%p\n",
780 VFrame **input = &output;
781 // If colormodels differ, reallocate output in nested colormodel.
782 int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
783 int current_cmodel = output->get_color_model();
785 if(debug) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d\n",
790 if(nested_cmodel != current_cmodel)
799 input = &((VRender*)commonrender)->input_temp;
806 if(!(*input)) (*input) = new VFrame;
809 (*input)->reallocate(0,
818 if(debug) printf("VModule::import_frame %d\n",
821 //(*input)->clear_frame();
824 if(debug) printf("VModule::import_frame %d %p %p\n",
826 (*input)->get_rows(),
828 result = nested_renderengine->vrender->process_buffer(
832 if(debug) printf("VModule::import_frame %d\n",
835 // If colormodels differ, change colormodels in opengl if possible.
836 // Swap output for temp if not possible.
837 if(nested_cmodel != current_cmodel)
841 x11_device->convert_cmodel(output,
844 // The color model was changed in place, so return output buffer
845 output->reallocate(0,
857 // Transfer from temporary to output
858 if(debug) printf("VModule::import_frame %d %d %d %d %d %d %d\n",
866 BC_CModels::transfer(output->get_rows(),
867 (*input)->get_rows(),
895 // Cache single frames
896 //memset(output->get_rows()[0], 0xff, 1024);
897 if(use_cache) file->set_cache_frames(1);
898 result = file->read_frame(output);
899 if(use_cache) file->set_cache_frames(0);
900 output->set_opengl_state(VFrame::RAM);
906 get_cache()->check_in(current_edit->asset);
913 if(debug) printf("VModule::import_frame %d\n", __LINE__);
916 x11_device->clear_input(output);
920 output->clear_frame();
928 if(debug) printf("VModule::import_frame %d\n", __LINE__);
931 x11_device->clear_input(output);
935 output->clear_frame();
939 if(debug) printf("VModule::import_frame %d done\n", __LINE__);
946 int VModule::render(VFrame *output,
947 int64_t start_position,
955 double edl_rate = get_edl()->session->frame_rate;
957 //printf("VModule::render %d %ld\n", __LINE__, start_position);
959 if(use_nudge) start_position += Units::to_int64(track->nudge *
960 frame_rate / edl_rate);
962 int64_t start_position_project = Units::to_int64(start_position *
963 edl_rate / frame_rate + 0.5);
965 update_transition(start_position_project,
968 VEdit* current_edit = (VEdit*)track->edits->editof(start_position_project,
971 VEdit* previous_edit = 0;
972 //printf("VModule::render %d %p %ld %d\n", __LINE__, current_edit, start_position_project, direction);
975 printf(" VModule::render %d %d %jd %s transition=%p opengl=%d current_edit=%p output=%p\n",
978 start_position_project,
987 output->clear_frame();
988 // We do not apply mask here, since alpha is 0, and neither substracting nor multypling changes it
989 // Another mask mode - "addition" should be added to be able to create mask from empty frames
990 // in this case we would call masking here too...
997 // Process transition
998 if(transition && transition->on)
1001 // Get temporary buffer
1002 VFrame **transition_input = 0;
1005 VRender *vrender = (VRender*)commonrender;
1006 transition_input = &vrender->transition_temp;
1010 transition_input = &transition_temp;
1013 if((*transition_input) &&
1014 ((*transition_input)->get_w() != track->track_w ||
1015 (*transition_input)->get_h() != track->track_h))
1017 delete (*transition_input);
1018 (*transition_input) = 0;
1021 // Load incoming frame
1022 if(!(*transition_input))
1024 (*transition_input) = new VFrame(0,
1028 get_edl()->session->color_model,
1032 (*transition_input)->copy_stacks(output);
1034 //printf("VModule::render %d\n", __LINE__);
1035 result = import_frame((*transition_input),
1043 // Load transition buffer
1044 previous_edit = (VEdit*)current_edit->previous;
1046 result |= import_frame(output,
1052 //printf("VModule::render %d\n", __LINE__);
1054 // printf("VModule::render %d %p %p %p %p\n",
1056 // (*transition_input),
1057 // (*transition_input)->get_pbuffer(),
1059 // output->get_pbuffer());
1062 // Execute plugin with transition_input and output here
1064 transition_server->set_use_opengl(use_opengl, renderengine->video);
1065 transition_server->process_transition((*transition_input),
1067 (direction == PLAY_FORWARD) ?
1068 (start_position_project - current_edit->startproject) :
1069 (start_position_project - current_edit->startproject - 1),
1070 transition->length);
1074 // Load output buffer
1075 result = import_frame(output,
1084 MaskAutos *keyframe_set =
1085 (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
1086 int64_t mask_position = !renderengine ? start_position :
1087 renderengine->vrender->current_position;
1088 MaskAuto *keyframe =
1089 (MaskAuto*)keyframe_set->get_prev_auto(mask_position, direction, current);
1091 if( keyframe->apply_before_plugins ) {
1092 VDeviceX11 *x11_device = 0;
1093 if(use_opengl && renderengine && renderengine->video) {
1094 x11_device = (VDeviceX11*)renderengine->video->get_output_base();
1095 if( !x11_device->can_mask(mask_position, keyframe_set) )
1098 if( use_opengl && x11_device ) {
1099 x11_device->do_mask(output, mask_position, keyframe_set,
1100 keyframe, keyframe);
1104 int cpus = renderengine ?
1105 renderengine->preferences->processors :
1106 plugin_array->mwindow->preferences->processors;
1107 masker = new MaskEngine(cpus);
1109 masker->do_mask(output, mask_position, keyframe_set, keyframe, keyframe);
1121 void VModule::create_objects()
1123 Module::create_objects();