direct integrals for floatautos, clipboard fix
[goodguy/history.git] / cinelerra-5.1 / cinelerra / vmodule.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009-2013 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "asset.h"
23 #include "bchash.h"
24 #include "bcpbuffer.h"
25 #include "bcsignals.h"
26 #include "cache.h"
27 #include "clip.h"
28 #include "commonrender.h"
29 #include "edits.h"
30 #include "edl.h"
31 #include "edlsession.h"
32 #include "file.h"
33 #include "filexml.h"
34 #include "floatautos.h"
35 #include "maskauto.h"
36 #include "maskautos.h"
37 #include "mwindow.h"
38 #include "overlayframe.h"
39 #include "patch.h"
40 #include "pluginarray.h"
41 #include "preferences.h"
42 #include "renderengine.h"
43 #include "sharedlocation.h"
44 #include "tracks.h"
45 #include "transition.h"
46 #include "transportque.h"
47 #include "units.h"
48 #include "vattachmentpoint.h"
49 #include "vdevicex11.h"
50 #include "vedit.h"
51 #include "vframe.h"
52 #include "videodevice.h"
53 #include "virtualvconsole.h"
54 #include "vmodule.h"
55 #include "vrender.h"
56 #include "vplugin.h"
57 #include "vtrack.h"
58 #include <string.h>
59 #include "interlacemodes.h"
60 #include "maskengine.h"
61 #include "automation.h"
62
63 VModule::VModule(RenderEngine *renderengine,
64         CommonRender *commonrender,
65         PluginArray *plugin_array,
66         Track *track)
67  : Module(renderengine, commonrender, plugin_array, track)
68 {
69         data_type = TRACK_VIDEO;
70         overlay_temp = 0;
71         input_temp = 0;
72         transition_temp = 0;
73         masker = 0;
74 }
75
76 VModule::~VModule()
77 {
78         if(overlay_temp) delete overlay_temp;
79         if(input_temp) delete input_temp;
80         if(transition_temp) delete transition_temp;
81         delete masker;
82 }
83
84
85 AttachmentPoint* VModule::new_attachment(Plugin *plugin)
86 {
87         return new VAttachmentPoint(renderengine, plugin);
88 }
89
90 int VModule::get_buffer_size()
91 {
92         return 1;
93 }
94
95 CICache* VModule::get_cache()
96 {
97         if(renderengine)
98                 return renderengine->get_vcache();
99         else
100                 return cache;
101 }
102
103
104
105
106
107
108 int VModule::import_frame(VFrame *output, VEdit *current_edit,
109         int64_t input_position, double frame_rate, int direction, int use_opengl)
110 {
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;
115         int result = 0;
116         const int debug = 0;
117         double edl_rate = get_edl()->session->frame_rate;
118
119         int64_t input_position_project = Units::to_int64(input_position *
120                 edl_rate / frame_rate + 0.001);
121
122         if(!output) printf("VModule::import_frame %d output=%p\n", __LINE__, output);
123         //output->dump_params();
124
125         if(debug) printf("VModule::import_frame %d this=%p input_position=%lld direction=%d\n",
126                 __LINE__, this, (long long)input_position, direction);
127
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--;
133         }
134         if(!output) printf("VModule::import_frame %d output=%p\n", __LINE__, output);
135
136         VDeviceX11 *x11_device = 0;
137         if(use_opengl)
138         {
139                 if(renderengine && renderengine->video)
140                 {
141                         x11_device = (VDeviceX11*)renderengine->video->get_output_base();
142                         output->set_opengl_state(VFrame::RAM);
143                         if(!x11_device) use_opengl = 0;
144                 }
145         }
146
147         if(!output) printf("VModule::import_frame %d output=%p x11_device=%p nested_edl=%p\n",
148                 __LINE__, output, x11_device, nested_edl);
149
150         if(debug) printf("VModule::import_frame %d current_edit=%p\n",
151                 __LINE__, current_edit);
152
153 // Load frame into output
154
155 // Create objects for nested EDL
156         if(current_edit && current_edit->nested_edl) {
157                 int command;
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);
160
161 // Convert requested direction to command
162                 if(renderengine->command->command == CURRENT_FRAME)
163                 {
164                         command = CURRENT_FRAME;
165                 }
166                 else
167                 if(direction == PLAY_REVERSE)
168                 {
169                         if(renderengine->command->single_frame())
170                                 command = SINGLE_FRAME_REWIND;
171                         else
172                                 command = NORMAL_REWIND;
173                 }
174                 else
175                 {
176                         if(renderengine->command->single_frame())
177                                 command = SINGLE_FRAME_FWD;
178                         else
179                                 command = NORMAL_FWD;
180                 }
181
182                 if(!nested_edl || nested_edl->id != current_edit->nested_edl->id)
183                 {
184                         nested_edl = current_edit->nested_edl;
185                         if(nested_renderengine)
186                         {
187                                 delete nested_renderengine;
188                                 nested_renderengine = 0;
189                         }
190
191                         if(!nested_command)
192                         {
193                                 nested_command = new TransportCommand;
194                         }
195
196
197                         if(!nested_renderengine)
198                         {
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);
206                         }
207                 }
208                 else
209                 {
210
211 // Update nested command
212                         nested_renderengine->command->command = command;
213                         nested_command->realtime = renderengine->command->realtime;
214                 }
215
216 // Update nested video driver for opengl
217                 nested_renderengine->video = renderengine->video;
218         }
219         else
220         {
221                 nested_edl = 0;
222         }
223         if(debug) printf("VModule::import_frame %d\n", __LINE__);
224
225         if(!output) printf("VModule::import_frame %d output=%p\n", __LINE__, output);
226
227         if(current_edit &&
228                 (current_edit->asset ||
229                 (current_edit->nested_edl && nested_renderengine->vrender)))
230         {
231                 File *file = 0;
232
233                 if(debug) printf("VModule::import_frame %d cache=%p\n",
234                         __LINE__,
235                         get_cache());
236                 if(current_edit->asset)
237                 {
238                         get_cache()->age();
239                         file = get_cache()->check_out(current_edit->asset,
240                                 get_edl());
241 //                      get_cache()->dump();
242                 }
243
244 // File found
245                 if(file || nested_edl)
246                 {
247 // Make all positions based on requested frame rate.
248                         int64_t edit_startproject = Units::to_int64(current_edit->startproject *
249                                 frame_rate /
250                                 edl_rate);
251                         int64_t edit_startsource = Units::to_int64(current_edit->startsource *
252                                 frame_rate /
253                                 edl_rate);
254 // Source position going forward
255                         uint64_t position = direction_position -
256                                 edit_startproject +
257                                 edit_startsource;
258                         int64_t nested_position = 0;
259
260
261
262
263
264 // apply speed curve to source position so the timeline agrees with the playback
265                         if(track->has_speed())
266                         {
267 // integrate position from start of edit.
268                                 double speed_position = edit_startsource;
269                                 FloatAutos *speed_autos = (FloatAutos*)track->automation->autos[AUTOMATION_SPEED];
270                                 speed_position += speed_autos->automation_intergal(edit_startproject,
271                                                 direction_position-edit_startproject, PLAY_FORWARD);
272 //printf("VModule::import_frame %d %lld %lld\n", __LINE__, position, (int64_t)speed_position);
273                                 position = (int64_t)speed_position;
274                         }
275
276
277
278
279
280                         int asset_w;
281                         int asset_h;
282                         if(debug) printf("VModule::import_frame %d\n", __LINE__);
283
284
285 // maybe apply speed curve here, so timeline reflects actual playback
286
287
288
289 // if we hit the end of stream, freeze at last frame
290                         uint64_t max_position = 0;
291                         if(file)
292                         {
293                                 max_position = Units::to_int64((double)file->get_video_length() *
294                                         frame_rate /
295                                         current_edit->asset->frame_rate - 1);
296                         }
297                         else
298                         {
299                                 max_position = Units::to_int64(nested_edl->tracks->total_playable_length() *
300                                         frame_rate - 1);
301                         }
302
303
304                         if(position > max_position) position = max_position;
305                         else
306                         if(position < 0) position = 0;
307
308                         int use_cache = renderengine &&
309                                 renderengine->command->single_frame();
310                         int use_asynchronous = !use_cache && renderengine &&
311 // Try to make rendering go faster.
312 // But converts some formats to YUV420, which may degrade input format.
313 //                              renderengine->command->realtime &&
314                                 renderengine->get_edl()->session->video_asynchronous;
315
316                         if(file)
317                         {
318                                 if(debug) printf("VModule::import_frame %d\n", __LINE__);
319                                 if(use_asynchronous)
320                                         file->start_video_decode_thread();
321                                 else
322                                         file->stop_video_thread();
323
324                                 int64_t normalized_position = Units::to_int64(position *
325                                         current_edit->asset->frame_rate /
326                                         frame_rate);
327 // printf("VModule::import_frame %d %lld %lld\n",
328 // __LINE__,
329 // position,
330 // normalized_position);
331                                 file->set_layer(current_edit->channel);
332                                 file->set_video_position(normalized_position,
333                                         0);
334                                 asset_w = current_edit->asset->width;
335                                 asset_h = current_edit->asset->height;
336 //printf("VModule::import_frame %d normalized_position=%lld\n", __LINE__, normalized_position);
337                         }
338                         else
339                         {
340                                 if(debug) printf("VModule::import_frame %d\n", __LINE__);
341                                 asset_w = nested_edl->session->output_w;
342                                 asset_h = nested_edl->session->output_h;
343 // Get source position in nested frame rate in direction of playback.
344                                 nested_position = Units::to_int64(position *
345                                         nested_edl->session->frame_rate /
346                                         frame_rate);
347                                 if(direction == PLAY_REVERSE)
348                                         nested_position++;
349                         }
350
351
352 // Auto scale if required
353                         if(output->get_params()->get("AUTOSCALE", 0))
354                         {
355                                 float autoscale_w = output->get_params()->get("AUTOSCALE_W", 1024);
356                                 float autoscale_h = output->get_params()->get("AUTOSCALE_H", 1024);
357                                 float x_scale = autoscale_w / asset_w;
358                                 float y_scale = autoscale_h / asset_h;
359
360 // Overriding camera
361                                 in_x = 0;
362                                 in_y = 0;
363                                 in_w = asset_w;
364                                 in_h = asset_h;
365
366                                 if(x_scale < y_scale)
367                                 {
368                                         out_w = in_w * x_scale;
369                                         out_h = in_h * x_scale;
370                                 }
371                                 else
372                                 {
373                                         out_w = in_w * y_scale;
374                                         out_h = in_h * y_scale;
375                                 }
376
377                                 out_x = track->track_w / 2 - out_w / 2;
378                                 out_y = track->track_h / 2 - out_h / 2;
379                         }
380                         else
381 // Apply camera
382                         {
383                                 ((VTrack*)track)->calculate_input_transfer(asset_w,
384                                         asset_h,
385                                         input_position_project,
386                                         direction,
387                                         in_x,
388                                         in_y,
389                                         in_w,
390                                         in_h,
391                                         out_x,
392                                         out_y,
393                                         out_w,
394                                         out_h);
395                         }
396
397 // printf("VModule::import_frame %d %f %d %f %d\n",
398 // __LINE__,
399 // in_w,
400 // asset_w,
401 // in_h,
402 // asset_h);
403 //
404 //                      printf("VModule::import_frame 1 [ilace] Project: mode (%d) Asset: autofixoption (%d), mode (%d), method (%d)\n",
405 //                      get_edl()->session->interlace_mode,
406 //                      current_edit->asset->interlace_autofixoption,
407 //                      current_edit->asset->interlace_mode,
408 //                      current_edit->asset->interlace_fixmethod);
409
410                         // Determine the interlacing method to use.
411                         int interlace_fixmethod = !current_edit->asset ? ILACE_FIXMETHOD_NONE :
412                                  ilaceautofixmethod2(get_edl()->session->interlace_mode,
413                                         current_edit->asset->interlace_autofixoption,
414                                         current_edit->asset->interlace_mode,
415                                         current_edit->asset->interlace_fixmethod);
416 //
417 //                      char string[BCTEXTLEN];
418 //                      ilacefixmethod_to_text(string,interlace_fixmethod);
419 //                      printf("VModule::import_frame 1 [ilace] Compensating by using: '%s'\n",string);
420
421                         // Compensate for the said interlacing...
422                         switch (interlace_fixmethod) {
423                                 case ILACE_FIXMETHOD_NONE:
424
425                                 break;
426                                 case ILACE_FIXMETHOD_UPONE:
427                                         out_y--;
428                                 break;
429                                 case ILACE_FIXMETHOD_DOWNONE:
430                                         out_y++;
431                                 break;
432                                 default:
433                                         printf("vmodule::importframe WARNING - unknown fix method for interlacing, no compensation in effect\n");
434                         }
435
436 // file -> temp -> output
437                         if( !EQUIV(in_x, 0) ||
438                                 !EQUIV(in_y, 0) ||
439                                 !EQUIV(in_w, track->track_w) ||
440                                 !EQUIV(in_h, track->track_h) ||
441                                 !EQUIV(out_x, 0) ||
442                                 !EQUIV(out_y, 0) ||
443                                 !EQUIV(out_w, track->track_w) ||
444                                 !EQUIV(out_h, track->track_h) ||
445                                 !EQUIV(in_w, asset_w) ||
446                                 !EQUIV(in_h, asset_h))
447                         {
448                                 if(debug) printf("VModule::import_frame %d file -> temp -> output\n", __LINE__);
449
450
451
452
453 // Get temporary input buffer
454                                 VFrame **input = 0;
455 // Realtime playback
456                                 if(commonrender)
457                                 {
458                                         VRender *vrender = (VRender*)commonrender;
459 //printf("VModule::import_frame %d vrender->input_temp=%p\n", __LINE__, vrender->input_temp);
460                                         input = &vrender->input_temp;
461                                 }
462                                 else
463 // Menu effect
464                                 {
465                                         input = &input_temp;
466                                 }
467
468
469                                 if((*input) &&
470                                         ((*input)->get_w() != asset_w ||
471                                         (*input)->get_h() != asset_h))
472                                 {
473                                         delete (*input);
474                                         (*input) = 0;
475                                 }
476
477
478
479
480
481                                 if(!(*input))
482                                 {
483                                         (*input) = new VFrame(0,
484                                                 -1,
485                                                 asset_w,
486                                                 asset_h,
487                                                 get_edl()->session->color_model,
488                                                 -1);
489                                 }
490
491
492
493                                 (*input)->copy_stacks(output);
494
495 // file -> temp
496 // Cache for single frame only
497                                 if(file)
498                                 {
499                                         if(debug) printf("VModule::import_frame %d this=%p file=%s\n",
500                                                 __LINE__,
501                                                 this,
502                                                 current_edit->asset->path);
503                                         if(use_cache) file->set_cache_frames(1);
504                                         result = file->read_frame((*input));
505                                         if(use_cache) file->set_cache_frames(0);
506                                         (*input)->set_opengl_state(VFrame::RAM);
507                                 }
508                                 else
509                                 if(nested_edl)
510                                 {
511 // If the colormodels differ, change input to nested colormodel
512                                         int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
513                                         int current_cmodel = output->get_color_model();
514                                         int output_w = output->get_w();
515                                         int output_h = output->get_h();
516                                         VFrame *input2 = (*input);
517
518                                         if(nested_cmodel != current_cmodel)
519                                         {
520 // If opengl, input -> input -> output
521                                                 if(use_opengl)
522                                                 {
523                                                 }
524                                                 else
525                                                 {
526 // If software, input2 -> input -> output
527 // Use output as a temporary.
528                                                         input2 = output;
529                                                 }
530
531                                                 if(debug) printf("VModule::import_frame %d this=%p nested_cmodel=%d\n",
532                                                         __LINE__,
533                                                         this,
534                                                         nested_cmodel);
535                                                 input2->dump();
536                                                 input2->reallocate(0,
537                                                         -1,
538                                                         0,
539                                                         0,
540                                                         0,
541                                                         (*input)->get_w(),
542                                                         (*input)->get_h(),
543                                                         nested_cmodel,
544                                                         -1);
545                                                 input2->dump();
546                                         }
547
548
549                                         if(debug) printf("VModule::import_frame %d this=%p nested_edl=%s input2=%p\n",
550                                                 __LINE__,
551                                                 this,
552                                                 nested_edl->path,
553                                                 input2);
554
555                                         result = nested_renderengine->vrender->process_buffer(
556                                                 input2,
557                                                 nested_position,
558                                                 use_opengl);
559
560                                         if(debug) printf("VModule::import_frame %d this=%p nested_edl=%s\n",
561                                                 __LINE__,
562                                                 this,
563                                                 nested_edl->path);
564
565                                         if(nested_cmodel != current_cmodel)
566                                         {
567                                                 if(debug) printf("VModule::import_frame %d\n", __LINE__);
568                                                 if(use_opengl)
569                                                 {
570 // Change colormodel in hardware.
571                                                         if(debug) printf("VModule::import_frame %d\n", __LINE__);
572                                                         x11_device->convert_cmodel(input2,
573                                                                 current_cmodel);
574
575 // The converted color model is now in hardware, so return the input2 buffer
576 // to the expected color model.
577                                                         input2->reallocate(0,
578                                                                 -1,
579                                                                 0,
580                                                                 0,
581                                                                 0,
582                                                                 (*input)->get_w(),
583                                                                 (*input)->get_h(),
584                                                                 current_cmodel,
585                                                                 -1);
586                                                 }
587                                                 else
588                                                 {
589 // Transfer from input2 to input
590 if(debug) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d input2=%p input=%p output=%p\n",
591 __LINE__,
592 nested_cmodel,
593 current_cmodel,
594 input2,
595 (*input),
596 output);
597                                                         BC_CModels::transfer((*input)->get_rows(),
598                                                                 input2->get_rows(),
599                                                                 0,
600                                                                 0,
601                                                                 0,
602                                                                 0,
603                                                                 0,
604                                                                 0,
605                                                                 0,
606                                                                 0,
607                                                             input2->get_w(),
608                                                             input2->get_h(),
609                                                                 0,
610                                                                 0,
611                                                                 (*input)->get_w(),
612                                                                 (*input)->get_h(),
613                                                                 nested_cmodel,
614                                                                 current_cmodel,
615                                                                 0,
616                                                                 input2->get_w(),
617                                                                 (*input)->get_w());
618 //printf("VModule::import_frame %d\n", __LINE__);
619
620 // input2 was the output buffer, so it must be restored
621                                                 input2->reallocate(0,
622                                                         -1,
623                                                         0,
624                                                         0,
625                                                         0,
626                                                         output_w,
627                                                         output_h,
628                                                         current_cmodel,
629                                                         -1);
630 //printf("VModule::import_frame %d\n", __LINE__);
631                                                 }
632                                         }
633
634                                 }
635
636 // Find an overlayer object to perform the camera transformation
637                                 OverlayFrame *overlayer = 0;
638
639 // OpenGL playback uses hardware
640                                 if(use_opengl)
641                                 {
642 //printf("VModule::import_frame %d\n", __LINE__);
643                                 }
644                                 else
645 // Realtime playback
646                                 if(commonrender)
647                                 {
648                                         VRender *vrender = (VRender*)commonrender;
649                                         overlayer = vrender->overlayer;
650                                 }
651                                 else
652 // Menu effect
653                                 {
654                                         if(!plugin_array)
655                                                 printf("VModule::import_frame neither plugin_array nor commonrender is defined.\n");
656                                         if(!overlay_temp)
657                                         {
658                                                 overlay_temp = new OverlayFrame(plugin_array->mwindow->preferences->processors);
659                                         }
660
661                                         overlayer = overlay_temp;
662                                 }
663 // printf("VModule::import_frame 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
664 //      in_x,
665 //      in_y,
666 //      in_w,
667 //      in_h,
668 //      out_x,
669 //      out_y,
670 //      out_w,
671 //      out_h);
672
673 // temp -> output
674 // for(int j = 0; j < output->get_w() * 3 * 5; j++)
675 //      output->get_rows()[0][j] = 255;
676
677                                 if(use_opengl)
678                                 {
679                                         x11_device->do_camera(output,
680                                                 (*input),
681                                                 in_x,
682                                                 in_y,
683                                                 in_x + in_w,
684                                                 in_y + in_h,
685                                                 out_x,
686                                                 out_y,
687                                                 out_x + out_w,
688                                                 out_y + out_h);
689 if(debug) printf("VModule::import_frame %d %d %d\n",
690 __LINE__,
691 output->get_opengl_state(),
692 (*input)->get_opengl_state());
693                                 }
694                                 else
695                                 {
696
697
698
699                                         output->clear_frame();
700
701
702 // get_cache()->check_in(current_edit->asset);
703 // return;
704
705 // TRANSFER_REPLACE is the fastest transfer mode but it has the disadvantage
706 // of producing green borders in floating point translation of YUV
707                                         int mode = TRANSFER_REPLACE;
708                                         if(get_edl()->session->interpolation_type != NEAREST_NEIGHBOR &&
709                                                 BC_CModels::is_yuv(output->get_color_model()))
710                                                 mode = TRANSFER_NORMAL;
711
712                                         if(debug) printf("VModule::import_frame %d temp -> output\n", __LINE__);
713                                         overlayer->overlay(output,
714                                                 (*input),
715                                                 in_x,
716                                                 in_y,
717                                                 in_x + in_w,
718                                                 in_y + in_h,
719                                                 out_x,
720                                                 out_y,
721                                                 out_x + out_w,
722                                                 out_y + out_h,
723                                                 1,
724                                                 mode,
725                                                 get_edl()->session->interpolation_type);
726                                 }
727                                 result = 1;
728                                 output->copy_stacks((*input));
729                         }
730                         else
731 // file -> output
732                         {
733                                 if(debug) printf("VModule::import_frame %d file -> output nested_edl=%p file=%p\n",
734                                         __LINE__,
735                                         nested_edl,
736                                         file);
737                                 if(nested_edl)
738                                 {
739                                         VFrame **input = &output;
740 // If colormodels differ, reallocate output in nested colormodel.
741                                         int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
742                                         int current_cmodel = output->get_color_model();
743
744 if(debug) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d\n",
745 __LINE__,
746 nested_cmodel,
747 current_cmodel);
748
749                                         if(nested_cmodel != current_cmodel)
750                                         {
751                                                 if(use_opengl)
752                                                 {
753                                                 }
754                                                 else
755                                                 {
756                                                         if(commonrender)
757                                                         {
758                                                                 input = &((VRender*)commonrender)->input_temp;
759                                                         }
760                                                         else
761                                                         {
762                                                                 input = &input_temp;
763                                                         }
764
765                                                         if(!(*input)) (*input) = new VFrame;
766                                                 }
767
768                                                 (*input)->reallocate(0,
769                                                         -1,
770                                                         0,
771                                                         0,
772                                                         0,
773                                                         output->get_w(),
774                                                         output->get_h(),
775                                                         nested_cmodel,
776                                                         -1);
777 if(debug) printf("VModule::import_frame %d\n",
778 __LINE__);
779 //(*input)->dump();
780 //(*input)->clear_frame();
781                                         }
782
783 if(debug) printf("VModule::import_frame %d %p %p\n",
784 __LINE__,
785 (*input)->get_rows(),
786 (*input));
787                                         result = nested_renderengine->vrender->process_buffer(
788                                                 (*input),
789                                                 nested_position,
790                                                 use_opengl);
791 if(debug) printf("VModule::import_frame %d\n",
792 __LINE__);
793
794 // If colormodels differ, change colormodels in opengl if possible.
795 // Swap output for temp if not possible.
796                                         if(nested_cmodel != current_cmodel)
797                                         {
798                                                 if(use_opengl)
799                                                 {
800                                                         x11_device->convert_cmodel(output,
801                                                                 current_cmodel);
802
803 // The color model was changed in place, so return output buffer
804                                                         output->reallocate(0,
805                                                                 -1,
806                                                                 0,
807                                                                 0,
808                                                                 0,
809                                                                 output->get_w(),
810                                                                 output->get_h(),
811                                                                 current_cmodel,
812                                                                 -1);
813                                                 }
814                                                 else
815                                                 {
816 // Transfer from temporary to output
817 if(debug) printf("VModule::import_frame %d %d %d %d %d %d %d\n",
818 __LINE__,
819 (*input)->get_w(),
820 (*input)->get_h(),
821 output->get_w(),
822 output->get_h(),
823 nested_cmodel,
824 current_cmodel);
825                                                         BC_CModels::transfer(output->get_rows(),
826                                                                 (*input)->get_rows(),
827                                                                 0,
828                                                                 0,
829                                                                 0,
830                                                                 0,
831                                                                 0,
832                                                                 0,
833                                                                 0,
834                                                                 0,
835                                                                 (*input)->get_w(),
836                                                                 (*input)->get_h(),
837                                                                 0,
838                                                                 0,
839                                                                 output->get_w(),
840                                                                 output->get_h(),
841                                                                 nested_cmodel,
842                                                                 current_cmodel,
843                                                                 0,
844                                                                 (*input)->get_w(),
845                                                                 output->get_w());
846                                                 }
847
848                                         }
849
850                                 }
851                                 else
852                                 if(file)
853                                 {
854 // Cache single frames
855 //memset(output->get_rows()[0], 0xff, 1024);
856                                         if(use_cache) file->set_cache_frames(1);
857                                         result = file->read_frame(output);
858                                         if(use_cache) file->set_cache_frames(0);
859                                         output->set_opengl_state(VFrame::RAM);
860                                 }
861                         }
862
863                         if(file)
864                         {
865                                 get_cache()->check_in(current_edit->asset);
866                                 file = 0;
867                         }
868                 }
869                 else
870 // Source not found
871                 {
872                         if(debug) printf("VModule::import_frame %d\n", __LINE__);
873                         if(use_opengl)
874                         {
875                                 x11_device->clear_input(output);
876                         }
877                         else
878                         {
879                                 output->clear_frame();
880                         }
881                         result = 1;
882                 }
883         }
884         else
885 // Source is silence
886         {
887                 if(debug) printf("VModule::import_frame %d\n", __LINE__);
888                 if(use_opengl)
889                 {
890                         x11_device->clear_input(output);
891                 }
892                 else
893                 {
894                         output->clear_frame();
895                 }
896         }
897
898         if(debug) printf("VModule::import_frame %d done\n", __LINE__);
899
900         return result;
901 }
902
903
904
905 int VModule::render(VFrame *output,
906         int64_t start_position,
907         int direction,
908         double frame_rate,
909         int use_nudge,
910         int debug_render,
911         int use_opengl)
912 {
913         int result = 0;
914         double edl_rate = get_edl()->session->frame_rate;
915
916 //printf("VModule::render %d %ld\n", __LINE__, start_position);
917
918         if(use_nudge) start_position += Units::to_int64(track->nudge *
919                 frame_rate / edl_rate);
920
921         int64_t start_position_project = Units::to_int64(start_position *
922                 edl_rate / frame_rate + 0.5);
923
924         update_transition(start_position_project,
925                 direction);
926
927         VEdit* current_edit = (VEdit*)track->edits->editof(start_position_project,
928                 direction,
929                 0);
930         VEdit* previous_edit = 0;
931 //printf("VModule::render %d %p %ld %d\n", __LINE__, current_edit, start_position_project, direction);
932
933         if(debug_render)
934                 printf("    VModule::render %d %d %jd %s transition=%p opengl=%d current_edit=%p output=%p\n",
935                         __LINE__,
936                         use_nudge,
937                         start_position_project,
938                         track->title,
939                         transition,
940                         use_opengl,
941                         current_edit,
942                         output);
943
944         if(!current_edit)
945         {
946                 output->clear_frame();
947                 // We do not apply mask here, since alpha is 0, and neither substracting nor multypling changes it
948                 // Another mask mode - "addition" should be added to be able to create mask from empty frames
949                 // in this case we would call masking here too...
950                 return 0;
951         }
952
953
954
955
956 // Process transition
957         if(transition && transition->on)
958         {
959
960 // Get temporary buffer
961                 VFrame **transition_input = 0;
962                 if(commonrender)
963                 {
964                         VRender *vrender = (VRender*)commonrender;
965                         transition_input = &vrender->transition_temp;
966                 }
967                 else
968                 {
969                         transition_input = &transition_temp;
970                 }
971
972                 if((*transition_input) &&
973                         ((*transition_input)->get_w() != track->track_w ||
974                         (*transition_input)->get_h() != track->track_h))
975                 {
976                         delete (*transition_input);
977                         (*transition_input) = 0;
978                 }
979
980 // Load incoming frame
981                 if(!(*transition_input))
982                 {
983                         (*transition_input) = new VFrame(0,
984                                 -1,
985                                 track->track_w,
986                                 track->track_h,
987                                 get_edl()->session->color_model,
988                                 -1);
989                 }
990
991                 (*transition_input)->copy_stacks(output);
992
993 //printf("VModule::render %d\n", __LINE__);
994                 result = import_frame((*transition_input),
995                         current_edit,
996                         start_position,
997                         frame_rate,
998                         direction,
999                         use_opengl);
1000
1001
1002 // Load transition buffer
1003                 previous_edit = (VEdit*)current_edit->previous;
1004
1005                 result |= import_frame(output,
1006                         previous_edit,
1007                         start_position,
1008                         frame_rate,
1009                         direction,
1010                         use_opengl);
1011 //printf("VModule::render %d\n", __LINE__);
1012
1013 // printf("VModule::render %d %p %p %p %p\n",
1014 // __LINE__,
1015 // (*transition_input),
1016 // (*transition_input)->get_pbuffer(),
1017 // output,
1018 // output->get_pbuffer());
1019
1020
1021 // Execute plugin with transition_input and output here
1022                 if(renderengine)
1023                         transition_server->set_use_opengl(use_opengl, renderengine->video);
1024                 transition_server->process_transition((*transition_input),
1025                         output,
1026                         (direction == PLAY_FORWARD) ?
1027                                 (start_position_project - current_edit->startproject) :
1028                                 (start_position_project - current_edit->startproject - 1),
1029                         transition->length);
1030         }
1031         else
1032         {
1033 // Load output buffer
1034                 result = import_frame(output,
1035                         current_edit,
1036                         start_position,
1037                         frame_rate,
1038                         direction,
1039                         use_opengl);
1040         }
1041
1042         Auto *current = 0;
1043         MaskAutos *keyframe_set =
1044                 (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
1045         int64_t mask_position = !renderengine ? start_position :
1046                 renderengine->vrender->current_position;
1047         MaskAuto *keyframe =
1048                 (MaskAuto*)keyframe_set->get_prev_auto(mask_position, direction, current);
1049
1050         if( keyframe->apply_before_plugins ) {
1051                 VDeviceX11 *x11_device = 0;
1052                 if(use_opengl && renderengine && renderengine->video) {
1053                         x11_device = (VDeviceX11*)renderengine->video->get_output_base();
1054                         if( !x11_device->can_mask(mask_position, keyframe_set) )
1055                                 use_opengl = 0;
1056                 }
1057                 if( use_opengl && x11_device ) {
1058                         x11_device->do_mask(output, mask_position, keyframe_set,
1059                                         keyframe, keyframe);
1060                 }
1061                 else {
1062                         if( !masker ) {
1063                                 int cpus = renderengine ?
1064                                         renderengine->preferences->processors :
1065                                         plugin_array->mwindow->preferences->processors;
1066                                 masker = new MaskEngine(cpus);
1067                         }
1068                         masker->do_mask(output, mask_position, keyframe_set, keyframe, keyframe);
1069                 }
1070         }
1071
1072         return result;
1073 }
1074
1075
1076
1077
1078
1079
1080 void VModule::create_objects()
1081 {
1082         Module::create_objects();
1083 }
1084
1085
1086
1087
1088
1089