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