add missing GPL information in guicast program files
[goodguy/cinelerra.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 "mainerror.h"
36 #include "maskauto.h"
37 #include "maskautos.h"
38 #include "mwindow.h"
39 #include "overlayframe.h"
40 #include "patch.h"
41 #include "pluginarray.h"
42 #include "preferences.h"
43 #include "renderengine.h"
44 #include "sharedlocation.h"
45 #include "tracks.h"
46 #include "transition.h"
47 #include "transportque.h"
48 #include "units.h"
49 #include "vattachmentpoint.h"
50 #include "vdevicex11.h"
51 #include "vedit.h"
52 #include "vframe.h"
53 #include "videodevice.h"
54 #include "virtualvconsole.h"
55 #include "vmodule.h"
56 #include "vrender.h"
57 #include "vplugin.h"
58 #include "vtrack.h"
59 #include <string.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 int VModule::import_frame(VFrame *output, VEdit *current_edit,
105         int64_t input_position, double frame_rate, int direction, int use_opengl)
106 {
107         int64_t direction_position=0;
108 // Translation of edit
109         float in_x=0.0, in_y=0.0, in_w=0.0, in_h=0.0;
110         float out_x=0.0, out_y=0.0, out_w=0.0, out_h=0.0;
111         int result = 0;
112         const int debug = 0;
113         double edl_rate = get_edl()->session->frame_rate;
114
115         int64_t input_position_project = Units::to_int64(input_position *
116                 edl_rate / frame_rate + 0.001);
117
118         if( !output ) printf("VModule::import_frame %d output=%p\n", __LINE__, output);
119         //output->dump_params();
120
121         if( debug ) printf("VModule::import_frame %d this=%p input_position=%lld direction=%d\n",
122                 __LINE__, this, (long long)input_position, direction);
123
124 // Convert to position corrected for direction
125         direction_position = input_position;
126         if( direction == PLAY_REVERSE ) {
127                 if( direction_position > 0 ) direction_position--;
128                 if( input_position_project > 0 ) input_position_project--;
129         }
130         if( !output ) printf("VModule::import_frame %d output=%p\n", __LINE__, output);
131
132         VDeviceX11 *x11_device = 0;
133         if( use_opengl ) {
134                 if( renderengine && renderengine->video ) {
135                         x11_device = (VDeviceX11*)renderengine->video->get_output_base();
136                         output->set_opengl_state(VFrame::RAM);
137                         if( !x11_device ) use_opengl = 0;
138                 }
139         }
140
141         if( !output ) printf("VModule::import_frame %d output=%p x11_device=%p nested_edl=%p\n",
142                 __LINE__, output, x11_device, nested_edl);
143
144         if( debug ) printf("VModule::import_frame %d current_edit=%p\n",
145                 __LINE__, current_edit);
146
147 // Load frame into output
148
149 // Create objects for nested EDL
150         if( current_edit && current_edit->nested_edl ) {
151                 int command;
152                 if( debug ) printf("VModule::import_frame %d nested_edl=%p current_edit->nested_edl=%p\n",
153                         __LINE__, nested_edl, current_edit->nested_edl);
154
155 // Convert requested direction to command
156                 if( renderengine->command->command == CURRENT_FRAME ||
157                     renderengine->command->command == LAST_FRAME ) {
158                         command = renderengine->command->command;
159                 }
160                 else
161                 if( direction == PLAY_REVERSE ) {
162                         if( renderengine->command->single_frame() )
163                                 command = SINGLE_FRAME_REWIND;
164                         else
165                                 command = NORMAL_REWIND;
166                 }
167                 else {
168                         if( renderengine->command->single_frame() )
169                                 command = SINGLE_FRAME_FWD;
170                         else
171                                 command = NORMAL_FWD;
172                 }
173
174                 if( !nested_edl || nested_edl->id != current_edit->nested_edl->id ) {
175                         nested_edl = current_edit->nested_edl;
176                         if( nested_renderengine ) {
177                                 delete nested_renderengine;
178                                 nested_renderengine = 0;
179                         }
180                         if( !nested_command )
181                                 nested_command = new TransportCommand(get_preferences());
182                         nested_command->command = command;
183                         nested_command->get_edl()->copy_all(nested_edl);
184                         nested_command->change_type = CHANGE_ALL;
185                         nested_command->realtime = renderengine->command->realtime;
186                         nested_renderengine = new RenderEngine(0, get_preferences(), 0, 1);
187                         nested_renderengine->set_vcache(get_cache());
188                         nested_renderengine->arm_command(nested_command);
189                 }
190                 else {
191
192 // Update nested command
193                         nested_renderengine->command->command = command;
194                         nested_command->realtime = renderengine->command->realtime;
195                 }
196
197 // Update nested video driver for opengl
198                 nested_renderengine->video = renderengine->video;
199         }
200         else {
201                 nested_edl = 0;
202         }
203         if( debug ) printf("VModule::import_frame %d\n", __LINE__);
204
205         if( output ) {
206                 if( use_opengl )
207                         x11_device->clear_input(output);
208                 else
209                         output->clear_frame();
210         }
211         else
212                 printf("VModule::import_frame %d output=%p\n", __LINE__, output);
213
214         if( current_edit &&
215                 (current_edit->asset ||
216                 (current_edit->nested_edl && nested_renderengine->vrender)) ) {
217                 File *file = 0;
218
219 //printf("VModule::import_frame %d cache=%p\n", __LINE__, get_cache());
220                 if( current_edit->asset ) {
221                         get_cache()->age();
222                         file = get_cache()->check_out(current_edit->asset, get_edl());
223 //                      get_cache()->dump();
224                 }
225
226 // File found
227                 if( file || nested_edl ) {
228                         int64_t nested_position = 0;
229 // Source position going forward, in edl framerate
230                         int64_t pos = Units::to_int64((double)direction_position / frame_rate * edl_rate);
231                         int64_t len = pos - current_edit->startproject;
232                         FloatAutos *speed_autos = !track->has_speed() ? 0 :
233                                 (FloatAutos*)track->automation->autos[AUTOMATION_SPEED];
234                         if( speed_autos && len > 0 )
235                                 len = speed_autos->automation_integral(current_edit->startproject, len, PLAY_FORWARD);
236                         pos = current_edit->startsource + len;
237 // Make positions based on requested frame rate.
238                         int64_t position = Units::to_int64((double)pos * frame_rate / edl_rate);
239
240                         int64_t video_length;
241                         int asset_w, asset_h;
242                         if( file ) {
243                                 asset_w = current_edit->asset->width;
244                                 asset_h = current_edit->asset->height;
245                                 video_length = file->get_video_length();
246                         }
247                         else {
248                                 asset_w = nested_edl->session->output_w;
249                                 asset_h = nested_edl->session->output_h;
250                                 video_length = nested_edl->tracks->total_length();
251                         }
252
253                         VFrame *&input = commonrender ?
254                                 ((VRender*)commonrender)->input_temp : // Realtime playback
255                                 input_temp ; // Menu effect
256                         VFrame::get_temp(input, asset_w, asset_h, get_edl()->session->color_model);
257
258                         int use_cache = renderengine->command->single_frame() ? 1 :
259                                 renderengine->command->get_direction() == PLAY_REVERSE ? -1 : 0;
260
261 //                      int use_asynchronous = !use_cache &&
262 //                              renderengine &&
263 // Try to make rendering go faster.
264 // But converts some formats to YUV420, which may degrade input format.
265 ////                            renderengine->command->realtime &&
266 //                              renderengine->get_edl()->session->video_asynchronous;
267
268                         if( file ) {
269                                 if( debug ) printf("VModule::import_frame %d\n", __LINE__);
270 //                              if( use_asynchronous )
271 //                                      file->start_video_decode_thread();
272 //                              else
273                                         file->stop_video_thread();
274
275                                 VEdit *vnext = (VEdit *)current_edit->next;
276                                 pos = Units::to_int64((double)input_position / frame_rate * edl_rate);
277                                 if( renderengine->preferences->cache_transitions && !use_cache &&
278 // cache transitions, not caching and inside transition 
279                                     vnext && vnext->transition && vnext->transition->on &&
280                                     file->get_video_length() >= 0 && pos >= vnext->startproject &&
281                                     pos < vnext->startproject + vnext->transition->length ) {
282                                         file->set_cache_frames(0);
283                                         file->set_layer(current_edit->channel);
284                                         VEdit *vnext = (VEdit *)current_edit->next;
285                                         Track *track = current_edit->track;
286                                         FloatAutos *speed_autos = (FloatAutos*)(track->has_speed() ?
287                                                 track->automation->autos[AUTOMATION_SPEED] : 0);
288                                         int64_t end = vnext->startproject + vnext->transition->length;
289                                         int first_frame = 1;
290                                         int count = renderengine->preferences->cache_size /
291                                                 input->get_data_size() / 2;  // try to burn only 1/2 of cache
292                                         while( !result && pos < end && count > 0 ) {
293                                                 int64_t curr_pos = pos - current_edit->startproject;
294                                                 if( curr_pos > 0 && speed_autos )
295                                                         curr_pos = speed_autos->automation_integral(
296                                                                 current_edit->startproject, curr_pos, PLAY_FORWARD);
297                                                 curr_pos += current_edit->startsource;
298                                                 int64_t norm_pos = Units::to_int64((double)curr_pos *
299                                                         current_edit->asset->frame_rate / edl_rate);
300                                                 if( norm_pos < 0 || video_length < 0 )
301                                                         norm_pos = 0;
302                                                 else if( norm_pos >= video_length )
303                                                         norm_pos = video_length-1;
304                                                 if( first_frame ) {
305                                                         if( file->get_cache_frame(input, norm_pos) )
306                                                                 break;  // if inside a cache run
307                                                         first_frame = 0;
308                                                         file->purge_cache(); // start new run
309                                                 }
310                                                 file->set_cache_frames(1);
311                                                 file->set_video_position(norm_pos, 0);
312                                                 result = file->read_frame(input);
313                                                 ++pos;  --count;
314                                         }
315                                         use_cache = 1;
316                                 }
317
318                                 int64_t normalized_position = Units::to_int64((double)position *
319                                         current_edit->asset->frame_rate / frame_rate);
320                                 if( normalized_position < 0 || video_length < 0 )
321                                         normalized_position = 0;
322                                 else if( normalized_position >= video_length )
323                                         normalized_position = video_length-1;
324 //printf("VModule::import_frame %d %lld %lld\n", __LINE__, position, normalized_position);
325                                 file->set_layer(current_edit->channel);
326                                 file->set_video_position(normalized_position, 0);
327                         }
328                         else {
329                                 if( debug ) printf("VModule::import_frame %d\n", __LINE__);
330 // Get source position in nested frame rate in direction of playback.
331                                 nested_position = Units::to_int64(position *
332                                         nested_edl->session->frame_rate /
333                                         frame_rate);
334                                 if( direction == PLAY_REVERSE )
335                                         nested_position++;
336                         }
337
338
339 // Auto scale if required
340                         if( output->get_params()->get("AUTOSCALE", 0) ) {
341                                 float autoscale_w = output->get_params()->get("AUTOSCALE_W", 1024);
342                                 float autoscale_h = output->get_params()->get("AUTOSCALE_H", 1024);
343                                 float x_scale = autoscale_w / asset_w;
344                                 float y_scale = autoscale_h / asset_h;
345
346 // Overriding camera
347                                 in_x = 0;
348                                 in_y = 0;
349                                 in_w = asset_w;
350                                 in_h = asset_h;
351
352                                 if( x_scale < y_scale ) {
353                                         out_w = in_w * x_scale;
354                                         out_h = in_h * x_scale;
355                                 }
356                                 else {
357                                         out_w = in_w * y_scale;
358                                         out_h = in_h * y_scale;
359                                 }
360
361                                 out_x = track->track_w / 2 - out_w / 2;
362                                 out_y = track->track_h / 2 - out_h / 2;
363                         }
364                         else {
365 // Apply camera
366                                 ((VTrack*)track)->calculate_input_transfer(asset_w,
367                                         asset_h, input_position_project, direction,
368                                         in_x, in_y, in_w, in_h,
369                                         out_x, out_y, out_w, out_h);
370                         }
371
372 // printf("VModule::import_frame %d %f %d %f %d\n",
373 // __LINE__, in_w, asset_w, in_h, asset_h);
374
375 // file -> temp -> output
376                         if( !EQUIV(in_x, 0) ||
377                                 !EQUIV(in_y, 0) ||
378                                 !EQUIV(in_w, track->track_w) ||
379                                 !EQUIV(in_h, track->track_h) ||
380                                 !EQUIV(out_x, 0) ||
381                                 !EQUIV(out_y, 0) ||
382                                 !EQUIV(out_w, track->track_w) ||
383                                 !EQUIV(out_h, track->track_h) ||
384                                 !EQUIV(in_w, asset_w) ||
385                                 !EQUIV(in_h, asset_h)) {
386 //printf("VModule::import_frame %d file -> temp -> output\n", __LINE__);
387 // Get temporary input buffer
388                                 VFrame **input = commonrender ?  // Realtime playback
389                                         &((VRender*)commonrender)->input_temp :
390                                         &input_temp ; // Menu effect
391                                 VFrame::get_temp(*input, asset_w, asset_h,
392                                                 get_edl()->session->color_model);
393                                 (*input)->copy_stacks(output);
394 // file -> temp
395 // Cache for single frame, reverse playback
396                                 if( file ) {
397                                         if( debug ) printf("VModule::import_frame %d this=%p file=%s\n",
398                                                 __LINE__,
399                                                 this,
400                                                 current_edit->asset->path);
401                                         if( use_cache )
402                                                 file->set_cache_frames(use_cache);
403                                         result = file->read_frame((*input));
404                                         if( use_cache )
405                                                 file->set_cache_frames(0);
406                                         (*input)->set_opengl_state(VFrame::RAM);
407                                 }
408                                 else
409                                 if( nested_edl ) {
410 // If the colormodels differ, change input to nested colormodel
411                                         int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
412                                         int current_cmodel = output->get_color_model();
413                                         int output_w = output->get_w();
414                                         int output_h = output->get_h();
415                                         VFrame *input2 = (*input);
416
417                                         if( nested_cmodel != current_cmodel ) {
418 // If opengl, input -> input -> output
419                                                 if( use_opengl ) { }
420                                                 else {
421 // If software, input2 -> input -> output
422 // Use output as a temporary.
423                                                         input2 = output;
424                                                 }
425
426                                                 if( debug ) printf("VModule::import_frame %d this=%p nested_cmodel=%d\n",
427                                                         __LINE__,
428                                                         this,
429                                                         nested_cmodel);
430                                                 input2->dump();
431                                                 input2->reallocate(0, -1, 0, 0, 0,
432                                                         (*input)->get_w(), (*input)->get_h(),
433                                                         nested_cmodel, -1);
434                                                 input2->dump();
435                                         }
436
437
438                                         if( debug ) printf("VModule::import_frame %d this=%p nested_edl=%s input2=%p\n",
439                                                 __LINE__, this, nested_edl->path, input2);
440
441                                         result = nested_renderengine->vrender->process_buffer(
442                                                 input2, nested_position, use_opengl);
443
444                                         if( debug ) printf("VModule::import_frame %d this=%p nested_edl=%s\n",
445                                                 __LINE__,
446                                                 this,
447                                                 nested_edl->path);
448
449                                         if( nested_cmodel != current_cmodel ) {
450                                                 if( debug ) printf("VModule::import_frame %d\n", __LINE__);
451                                                 if( use_opengl ) {
452 // Change colormodel in hardware.
453                                                         if( debug ) printf("VModule::import_frame %d\n", __LINE__);
454                                                         x11_device->convert_cmodel(input2,
455                                                                 current_cmodel);
456
457 // The converted color model is now in hardware, so return the input2 buffer
458 // to the expected color model.
459                                                         input2->reallocate(0, -1, 0, 0, 0,
460                                                                 (*input)->get_w(), (*input)->get_h(),
461                                                                 current_cmodel, -1);
462                                                 }
463                                                 else {
464 // Transfer from input2 to input
465 if( debug ) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d input2=%p input=%p output=%p\n",
466   __LINE__, nested_cmodel, current_cmodel, input2, (*input), output);
467                                                         BC_CModels::transfer((*input)->get_rows(), input2->get_rows(),
468                                                                 0, 0, 0, 0, 0, 0,
469                                                                 0, 0, input2->get_w(), input2->get_h(),
470                                                                 0, 0, (*input)->get_w(), (*input)->get_h(),
471                                                                 nested_cmodel, current_cmodel, 0,
472                                                                 input2->get_w(), (*input)->get_w());
473 //printf("VModule::import_frame %d\n", __LINE__);
474
475 // input2 was the output buffer, so it must be restored
476                                                 input2->reallocate(0, -1, 0, 0, 0,
477                                                         output_w, output_h, current_cmodel, -1);
478 //printf("VModule::import_frame %d\n", __LINE__);
479                                                 }
480                                         }
481
482                                 }
483
484 // Find an overlayer object to perform the camera transformation
485                                 OverlayFrame *overlayer = 0;
486
487 // OpenGL playback uses hardware
488                                 if( use_opengl ) {
489 //printf("VModule::import_frame %d\n", __LINE__);
490                                 }
491                                 else if( commonrender ) {
492 // Realtime playback
493                                         VRender *vrender = (VRender*)commonrender;
494                                         overlayer = vrender->overlayer;
495                                 }
496                                 else {
497 // Menu effect
498                                         if( !plugin_array )
499                                                 printf("VModule::import_frame neither plugin_array nor commonrender is defined.\n");
500                                         if( !overlay_temp ) {
501                                                 overlay_temp = new OverlayFrame(plugin_array->mwindow->preferences->processors);
502                                         }
503
504                                         overlayer = overlay_temp;
505                                 }
506 // printf("VModule::import_frame 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
507 //   in_x, in_y, in_w, in_h, out_x, out_y, out_w, out_h);
508
509 // temp -> output
510 // for( int j = 0; j < output->get_w() * 3 * 5; j++ )
511 //      output->get_rows()[0][j] = 255;
512
513                                 if( use_opengl ) {
514                                         x11_device->do_camera(output, (*input),
515                                                 in_x, in_y, in_x + in_w, in_y + in_h,
516                                                 out_x, out_y, out_x + out_w, out_y + out_h);
517 if( debug ) printf("VModule::import_frame %d %d %d\n",
518  __LINE__, output->get_opengl_state(), (*input)->get_opengl_state());
519                                 }
520                                 else {
521                                         output->clear_frame();
522 // get_cache()->check_in(current_edit->asset);
523 // return;
524
525 // TRANSFER_REPLACE is the fastest transfer mode but it has the disadvantage
526 // of producing green borders in floating point translation of YUV
527                                         int mode = TRANSFER_REPLACE;
528                                         if( get_edl()->session->interpolation_type != NEAREST_NEIGHBOR &&
529                                                 BC_CModels::is_yuv(output->get_color_model()) )
530                                                 mode = TRANSFER_NORMAL;
531
532                                         if( debug ) printf("VModule::import_frame %d temp -> output\n", __LINE__);
533                                         overlayer->overlay(output, (*input),
534                                                 in_x, in_y, in_x + in_w, in_y + in_h,
535                                                 out_x, out_y, out_x + out_w, out_y + out_h,
536                                                 1, mode, get_edl()->session->interpolation_type);
537                                 }
538                                 result = 1;
539                                 
540                                 output->copy_stacks((*input));
541                                 
542                                 
543 //printf("VModule::import_frame %d\n", __LINE__); 
544 //(*input)->dump_params();
545 //output->dump_params();
546                         }
547                         else {
548 // file -> output
549                                 if( debug ) printf("VModule::import_frame %d file -> output nested_edl=%p file=%p\n",
550                                         __LINE__, nested_edl, file);
551                                 if( nested_edl ) {
552                                         VFrame **input = &output;
553 // If colormodels differ, reallocate output in nested colormodel.
554                                         int nested_cmodel = nested_renderengine->get_edl()->session->color_model;
555                                         int current_cmodel = output->get_color_model();
556
557 if( debug ) printf("VModule::import_frame %d nested_cmodel=%d current_cmodel=%d\n",
558 __LINE__,
559 nested_cmodel,
560 current_cmodel);
561
562                                         if( nested_cmodel != current_cmodel ) {
563                                                 if( use_opengl ) { }
564                                                 else {
565                                                         if( commonrender ) {
566                                                                 input = &((VRender*)commonrender)->input_temp;
567                                                         }
568                                                         else {
569                                                                 input = &input_temp;
570                                                         }
571
572                                                         if( !(*input) ) (*input) = new VFrame;
573                                                 }
574
575                                                 (*input)->reallocate(0, -1, 0, 0, 0,
576                                                         output->get_w(), output->get_h(),
577                                                         nested_cmodel, -1);
578 if( debug ) printf("VModule::import_frame %d\n", __LINE__);
579 //(*input)->dump();
580 //(*input)->clear_frame();
581                                         }
582
583 if( debug ) printf("VModule::import_frame %d %p %p\n",
584  __LINE__, (*input)->get_rows(), (*input));
585                                         result = nested_renderengine->vrender->process_buffer(
586                                                 (*input), nested_position, use_opengl);
587 if( debug ) printf("VModule::import_frame %d\n",
588 __LINE__);
589
590 // If colormodels differ, change colormodels in opengl if possible.
591 // Swap output for temp if not possible.
592                                         if( nested_cmodel != current_cmodel ) {
593                                                 if( use_opengl ) {
594                                                         x11_device->convert_cmodel(output,
595                                                                 current_cmodel);
596
597 // The color model was changed in place, so return output buffer
598                                                         output->reallocate(0, -1, 0, 0, 0,
599                                                                 output->get_w(), output->get_h(),
600                                                                 current_cmodel, -1);
601                                                 }
602                                                 else {
603 // Transfer from temporary to output
604 if( debug ) printf("VModule::import_frame %d %d %d %d %d %d %d\n",
605   __LINE__, (*input)->get_w(), (*input)->get_h(),
606   output->get_w(), output->get_h(), nested_cmodel, current_cmodel);
607                                                         BC_CModels::transfer(output->get_rows(), (*input)->get_rows(),
608                                                                 0, 0, 0, 0, 0, 0,
609                                                                 0, 0, (*input)->get_w(), (*input)->get_h(),
610                                                                 0, 0, output->get_w(), output->get_h(),
611                                                                 nested_cmodel, current_cmodel, 0,
612                                                                 (*input)->get_w(), output->get_w());
613                                                 }
614
615                                         }
616                                 }
617                                 else if( file ) {
618 // Cache single frames
619 //memset(output->get_rows()[0], 0xff, 1024);
620                                         if( use_cache )
621                                                 file->set_cache_frames(use_cache);
622                                         result = file->read_frame(output);
623                                         if( use_cache )
624                                                 file->set_cache_frames(0);
625                                         output->set_opengl_state(VFrame::RAM);
626                                 }
627                         }
628
629                         if( file ) {
630                                 get_cache()->check_in(current_edit->asset);
631                                 file = 0;
632                         }
633                 }
634                 else {
635 // Source not found
636                         result = 1;
637                 }
638
639 //              printf("VModule::import_frame %d cache=%p\n", 
640 //                      __LINE__,
641 //                      get_cache());
642
643         }
644         else {
645 // Source is silence
646                 if( debug ) printf("VModule::import_frame %d\n", __LINE__);
647                 if( use_opengl ) {
648                         x11_device->clear_input(output);
649                 }
650                 else {
651                         output->clear_frame();
652                 }
653         }
654
655         if( debug ) printf("VModule::import_frame %d done\n", __LINE__);
656
657         return result;
658 }
659
660
661 int VModule::render(VFrame *output,
662         int64_t start_position, int direction, double frame_rate,
663         int use_nudge, int debug_render, int use_opengl)
664 {
665         int result = 0;
666         double edl_rate = get_edl()->session->frame_rate;
667
668 //printf("VModule::render %d %ld\n", __LINE__, start_position);
669
670         if( use_nudge ) start_position += Units::to_int64(track->nudge *
671                 frame_rate / edl_rate);
672
673         int64_t start_position_project = Units::to_int64(start_position *
674                 edl_rate / frame_rate + 0.5);
675
676         update_transition(start_position_project, direction);
677
678         VEdit* current_edit = (VEdit*)track->edits->editof(start_position_project,
679                 direction, 0);
680         VEdit* previous_edit = 0;
681 //printf("VModule::render %d %p %ld %d\n", __LINE__, current_edit, start_position_project, direction);
682
683         Plugin *transition = get_edl()->tracks->plugin_exists(transition_id);
684         if( debug_render )
685                 printf("    VModule::render %d %d %jd %s transition=%p opengl=%d current_edit=%p output=%p\n",
686                         __LINE__, use_nudge, start_position_project, track->title,
687                         transition, use_opengl, current_edit, output);
688
689         if( !current_edit ) {
690                 output->clear_frame();
691                 // We do not apply mask here, since alpha is 0, and neither substracting nor multypling changes it
692                 // Another mask mode - "addition" should be added to be able to create mask from empty frames
693                 // in this case we would call masking here too...
694                 return 0;
695         }
696
697
698
699
700 // Process transition
701         if( transition && transition->on ) {
702
703 // Get temporary buffer
704                 VFrame **transition_input = 0;
705                 if( commonrender ) {
706                         VRender *vrender = (VRender*)commonrender;
707                         transition_input = &vrender->transition_temp;
708                 }
709                 else {
710                         transition_input = &transition_temp;
711                 }
712
713                 if( (*transition_input) &&
714                         ((*transition_input)->get_w() != track->track_w ||
715                         (*transition_input)->get_h() != track->track_h) ) {
716                         delete (*transition_input);
717                         (*transition_input) = 0;
718                 }
719
720 // Load incoming frame
721                 if( !(*transition_input) ) {
722                         (*transition_input) =
723                                 new VFrame(track->track_w, track->track_h,
724                                         get_edl()->session->color_model);
725                 }
726
727                 (*transition_input)->copy_stacks(output);
728
729 //printf("VModule::render %d\n", __LINE__);
730                 result = import_frame((*transition_input),
731                         current_edit, start_position, frame_rate,
732                         direction, use_opengl);
733
734
735 // Load transition buffer
736                 previous_edit = (VEdit*)current_edit->previous;
737
738                 result |= import_frame(output,
739                         previous_edit, start_position, frame_rate,
740                         direction, use_opengl);
741 //printf("VModule::render %d %p %p %p %p\n", __LINE__,
742 // (*transition_input), (*transition_input)->get_pbuffer(),
743 // output, output->get_pbuffer());
744
745                 if( transition_server ) {
746 // Execute plugin with transition_input and output here
747                         if( renderengine )
748                                 transition_server->set_use_opengl(use_opengl, renderengine->video);
749                         transition_server->process_transition((*transition_input), output,
750                                 (direction == PLAY_FORWARD) ?
751                                         (start_position_project - current_edit->startproject) :
752                                         (start_position_project - current_edit->startproject - 1),
753                                 transition->length);
754                 }
755                 else
756                         eprintf("missing transition plugin: %s\n", transition->title);
757         }
758         else {
759 // Load output buffer
760                 result = import_frame(output,
761                         current_edit, start_position, frame_rate,
762                         direction, use_opengl);
763         }
764
765         Auto *current = 0;
766         MaskAutos *keyframe_set =
767                 (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
768         int64_t mask_position = !renderengine ? start_position :
769                 renderengine->vrender->current_position;
770         MaskAuto *keyframe =
771                 (MaskAuto*)keyframe_set->get_prev_auto(mask_position, direction, current);
772
773         if( keyframe->apply_before_plugins && keyframe->has_active_mask() ) {
774                 VDeviceX11 *x11_device = 0;
775                 if( use_opengl && renderengine && renderengine->video ) {
776                         x11_device = (VDeviceX11*)renderengine->video->get_output_base();
777                         if( !x11_device->can_mask(mask_position, keyframe_set) ) {
778                                 if( output->get_opengl_state() != VFrame::RAM ) {
779                                         int w = output->get_w(), h = output->get_h();
780                                         x11_device->do_camera(output, output,
781                                                 0,0,w,h, 0,0,w,h); // copy to ram
782                                 }
783                                 use_opengl = 0;
784                         }
785                 }
786                 if( use_opengl && x11_device ) {
787                         x11_device->do_mask(output, mask_position, keyframe_set,
788                                         keyframe, keyframe);
789                 }
790                 else {
791                         if( !masker ) {
792                                 int cpus = renderengine ?
793                                         renderengine->preferences->processors :
794                                         plugin_array->mwindow->preferences->processors;
795                                 masker = new MaskEngine(cpus);
796                         }
797                         masker->do_mask(output, mask_position, keyframe_set, keyframe, keyframe);
798                 }
799         }
800
801         return result;
802 }
803
804
805 void VModule::create_objects()
806 {
807         Module::create_objects();
808 }
809