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