rework set default transtion, nested proxy edl fixes, doubleclick proxy media fix...
[goodguy/history.git] / cinelerra-5.1 / cinelerra / maskengine.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 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 "bcsignals.h"
23 #include "condition.h"
24 #include "clip.h"
25 #include "maskauto.h"
26 #include "maskautos.h"
27 #include "maskengine.h"
28 #include "mutex.h"
29 #include "transportque.inc"
30 #include "vframe.h"
31
32 #include <math.h>
33 #include <stdint.h>
34 #include <string.h>
35
36 MaskPackage::MaskPackage()
37 {
38 }
39
40 MaskPackage::~MaskPackage()
41 {
42 }
43
44
45
46
47
48
49
50 MaskUnit::MaskUnit(MaskEngine *engine)
51  : LoadClient(engine)
52 {
53         this->engine = engine;
54         this->temp = 0;
55 }
56
57
58 MaskUnit::~MaskUnit()
59 {
60         if(temp) delete temp;
61 }
62
63
64 #define OVERSAMPLE 8
65
66 void MaskUnit::draw_line_clamped(VFrame *frame,
67         int x1, int y1, int x2, int y2, unsigned char k)
68 {
69         int draw_x1, draw_y1;
70         int draw_x2, draw_y2;
71
72         if(y2 < y1) {
73                 draw_x1 = x2;  draw_y1 = y2;
74                 draw_x2 = x1;  draw_y2 = y1;
75         }
76         else {
77                 draw_x1 = x1;  draw_y1 = y1;
78                 draw_x2 = x2;  draw_y2 = y2;
79         }
80
81         unsigned char **rows = (unsigned char**)frame->get_rows();
82
83         if(draw_y2 != draw_y1) {
84                 float slope = ((float)draw_x2 - draw_x1) / ((float)draw_y2 - draw_y1);
85                 int w = frame->get_w() - 1;
86                 int h = frame->get_h();
87
88                 for(float y = draw_y1; y < draw_y2; y++) {
89                         if(y >= 0 && y < h) {
90                                 int x = (int)((y - draw_y1) * slope + draw_x1);
91                                 int y_i = (int)y;
92                                 int x_i = CLIP(x, 0, w);
93
94                                 if(rows[y_i][x_i] == k)
95                                         rows[y_i][x_i] = 0;
96                                 else
97                                         rows[y_i][x_i] = k;
98                         }
99                 }
100         }
101 }
102
103 void MaskUnit::blur_strip(double *val_p,
104         double *val_m,
105         double *dst,
106         double *src,
107         int size,
108         int max)
109 {
110         double *sp_p = src;
111         double *sp_m = src + size - 1;
112         double *vp = val_p;
113         double *vm = val_m + size - 1;
114         double initial_p = sp_p[0];
115         double initial_m = sp_m[0];
116
117 //printf("MaskUnit::blur_strip %d\n", size);
118         for(int k = 0; k < size; k++)
119         {
120                 int terms = (k < 4) ? k : 4;
121                 int l;
122                 for(l = 0; l <= terms; l++)
123                 {
124                         *vp += n_p[l] * sp_p[-l] - d_p[l] * vp[-l];
125                         *vm += n_m[l] * sp_m[l] - d_m[l] * vm[l];
126                 }
127
128                 for( ; l <= 4; l++)
129                 {
130                         *vp += (n_p[l] - bd_p[l]) * initial_p;
131                         *vm += (n_m[l] - bd_m[l]) * initial_m;
132                 }
133                 sp_p++;
134                 sp_m--;
135                 vp++;
136                 vm--;
137         }
138
139         for(int i = 0; i < size; i++)
140         {
141                 double sum = val_p[i] + val_m[i];
142                 CLAMP(sum, 0, max);
143                 dst[i] = sum;
144         }
145 }
146
147 void MaskUnit::do_feather(VFrame *output,
148         VFrame *input,
149         double feather,
150         int start_y,
151         int end_y,
152         int start_x,
153         int end_x)
154 {
155 //printf("MaskUnit::do_feather %f\n", feather);
156 // Get constants
157         double constants[8];
158         double div;
159         double std_dev = sqrt(-(double)(feather * feather) / (2 * log(1.0 / 255.0)));
160         div = sqrt(2 * M_PI) * std_dev;
161         constants[0] = -1.783 / std_dev;
162         constants[1] = -1.723 / std_dev;
163         constants[2] = 0.6318 / std_dev;
164         constants[3] = 1.997  / std_dev;
165         constants[4] = 1.6803 / div;
166         constants[5] = 3.735 / div;
167         constants[6] = -0.6803 / div;
168         constants[7] = -0.2598 / div;
169
170         n_p[0] = constants[4] + constants[6];
171         n_p[1] = exp(constants[1]) *
172                                 (constants[7] * sin(constants[3]) -
173                                 (constants[6] + 2 * constants[4]) * cos(constants[3])) +
174                                 exp(constants[0]) *
175                                 (constants[5] * sin(constants[2]) -
176                                 (2 * constants[6] + constants[4]) * cos(constants[2]));
177
178         n_p[2] = 2 * exp(constants[0] + constants[1]) *
179                                 ((constants[4] + constants[6]) * cos(constants[3]) *
180                                 cos(constants[2]) - constants[5] *
181                                 cos(constants[3]) * sin(constants[2]) -
182                                 constants[7] * cos(constants[2]) * sin(constants[3])) +
183                                 constants[6] * exp(2 * constants[0]) +
184                                 constants[4] * exp(2 * constants[1]);
185
186         n_p[3] = exp(constants[1] + 2 * constants[0]) *
187                                 (constants[7] * sin(constants[3]) -
188                                 constants[6] * cos(constants[3])) +
189                                 exp(constants[0] + 2 * constants[1]) *
190                                 (constants[5] * sin(constants[2]) - constants[4] *
191                                 cos(constants[2]));
192         n_p[4] = 0.0;
193
194         d_p[0] = 0.0;
195         d_p[1] = -2 * exp(constants[1]) * cos(constants[3]) -
196                                 2 * exp(constants[0]) * cos(constants[2]);
197
198         d_p[2] = 4 * cos(constants[3]) * cos(constants[2]) *
199                                 exp(constants[0] + constants[1]) +
200                                 exp(2 * constants[1]) + exp (2 * constants[0]);
201
202         d_p[3] = -2 * cos(constants[2]) * exp(constants[0] + 2 * constants[1]) -
203                                 2 * cos(constants[3]) * exp(constants[1] + 2 * constants[0]);
204
205         d_p[4] = exp(2 * constants[0] + 2 * constants[1]);
206
207         for(int i = 0; i < 5; i++) d_m[i] = d_p[i];
208
209         n_m[0] = 0.0;
210         for(int i = 1; i <= 4; i++)
211                 n_m[i] = n_p[i] - d_p[i] * n_p[0];
212
213         double sum_n_p, sum_n_m, sum_d;
214         double a, b;
215
216         sum_n_p = 0.0;
217         sum_n_m = 0.0;
218         sum_d = 0.0;
219         for(int i = 0; i < 5; i++)
220         {
221                 sum_n_p += n_p[i];
222                 sum_n_m += n_m[i];
223                 sum_d += d_p[i];
224         }
225
226         a = sum_n_p / (1 + sum_d);
227         b = sum_n_m / (1 + sum_d);
228
229         for(int i = 0; i < 5; i++)
230         {
231                 bd_p[i] = d_p[i] * a;
232                 bd_m[i] = d_m[i] * b;
233         }
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256 #define DO_FEATHER(type, max) \
257 { \
258         int frame_w = input->get_w(); \
259         int frame_h = input->get_h(); \
260         int size = MAX(frame_w, frame_h); \
261         double *src = new double[size]; \
262         double *dst = new double[size]; \
263         double *val_p = new double[size]; \
264         double *val_m = new double[size]; \
265         type **in_rows = (type**)input->get_rows(); \
266         type **out_rows = (type**)output->get_rows(); \
267         int j; \
268  \
269 /* printf("DO_FEATHER 1\n"); */ \
270         if(end_x > start_x) \
271         { \
272                 for(j = start_x; j < end_x; j++) \
273                 { \
274         /* printf("DO_FEATHER 1.1 %d\n", j); */ \
275                         bzero(val_p, sizeof(double) * frame_h); \
276                         bzero(val_m, sizeof(double) * frame_h); \
277                         for(int k = 0; k < frame_h; k++) \
278                         { \
279                                 src[k] = (double)in_rows[k][j]; \
280                         } \
281          \
282                         blur_strip(val_p, val_m, dst, src, frame_h, max); \
283          \
284                         for(int k = 0; k < frame_h; k++) \
285                         { \
286                                 out_rows[k][j] = (type)dst[k]; \
287                         } \
288                 } \
289         } \
290  \
291         if(end_y > start_y) \
292         { \
293                 for(j = start_y; j < end_y; j++) \
294                 { \
295         /* printf("DO_FEATHER 2 %d\n", j); */ \
296                         bzero(val_p, sizeof(double) * frame_w); \
297                         bzero(val_m, sizeof(double) * frame_w); \
298                         for(int k = 0; k < frame_w; k++) \
299                         { \
300                                 src[k] = (double)out_rows[j][k]; \
301                         } \
302          \
303                         blur_strip(val_p, val_m, dst, src, frame_w, max); \
304          \
305                         for(int k = 0; k < frame_w; k++) \
306                         { \
307                                 out_rows[j][k] = (type)dst[k]; \
308                         } \
309                 } \
310         } \
311  \
312 /* printf("DO_FEATHER 3\n"); */ \
313  \
314         delete [] src; \
315         delete [] dst; \
316         delete [] val_p; \
317         delete [] val_m; \
318 /* printf("DO_FEATHER 4\n"); */ \
319 }
320
321
322
323
324
325
326
327
328 //printf("do_feather %d\n", frame->get_color_model());
329         switch(input->get_color_model())
330         {
331                 case BC_A8:
332                         DO_FEATHER(unsigned char, 0xff);
333                         break;
334
335                 case BC_A16:
336                         DO_FEATHER(uint16_t, 0xffff);
337                         break;
338
339                 case BC_A_FLOAT:
340                         DO_FEATHER(float, 1);
341                         break;
342         }
343
344
345
346
347 }
348
349 void MaskUnit::process_package(LoadPackage *package)
350 {
351         MaskPackage *ptr = (MaskPackage*)package;
352
353         if(engine->recalculate &&
354                 engine->step == DO_MASK)
355         {
356                 VFrame *mask;
357                 if(engine->feather > 0)
358                         mask = engine->temp_mask;
359                 else
360                         mask = engine->mask;
361
362 SET_TRACE
363 // Generated oversampling frame
364                 int mask_w = mask->get_w();
365                 //int mask_h = mask->get_h();
366                 int oversampled_package_w = mask_w * OVERSAMPLE;
367                 int oversampled_package_h = (ptr->end_y - ptr->start_y) * OVERSAMPLE;
368 //printf("MaskUnit::process_package 1\n");
369
370 SET_TRACE
371                 if(temp &&
372                         (temp->get_w() != oversampled_package_w ||
373                         temp->get_h() != oversampled_package_h)) {
374                         delete temp;  temp = 0;
375                 }
376 //printf("MaskUnit::process_package 1\n");
377
378 SET_TRACE
379                 if(!temp) {
380                         temp = new VFrame(oversampled_package_w, oversampled_package_h, BC_A8, 0);
381                 }
382
383 SET_TRACE
384                 temp->clear_frame();
385 //printf("MaskUnit::process_package 1 %d\n", engine->point_sets.total);
386
387 SET_TRACE
388
389 // Draw oversampled region of polygons on temp
390                 for(int k = 0; k < engine->point_sets.total; k++)
391                 {
392                         int old_x, old_y;
393                         unsigned char max = k + 1;
394                         ArrayList<MaskPoint*> *points = engine->point_sets.values[k];
395
396                         if(points->total < 3) continue;
397 //printf("MaskUnit::process_package 2 %d %d\n", k, points->total);
398                         for(int i = 0; i < points->total; i++)
399                         {
400                                 MaskPoint *point1 = points->values[i];
401                                 MaskPoint *point2 = (i >= points->total - 1) ?
402                                         points->values[0] :
403                                         points->values[i + 1];
404
405                                 float x, y;
406                                 int segments = (int)(sqrt(SQR(point1->x - point2->x) + SQR(point1->y - point2->y)));
407                                 if(point1->control_x2 == 0 &&
408                                         point1->control_y2 == 0 &&
409                                         point2->control_x1 == 0 &&
410                                         point2->control_y1 == 0)
411                                         segments = 1;
412                                 float x0 = point1->x;
413                                 float y0 = point1->y;
414                                 float x1 = point1->x + point1->control_x2;
415                                 float y1 = point1->y + point1->control_y2;
416                                 float x2 = point2->x + point2->control_x1;
417                                 float y2 = point2->y + point2->control_y1;
418                                 float x3 = point2->x;
419                                 float y3 = point2->y;
420
421                                 for(int j = 0; j <= segments; j++)
422                                 {
423                                         float t = (float)j / segments;
424                                         float tpow2 = t * t;
425                                         float tpow3 = t * t * t;
426                                         float invt = 1 - t;
427                                         float invtpow2 = invt * invt;
428                                         float invtpow3 = invt * invt * invt;
429
430                                         x = (        invtpow3 * x0
431                                                 + 3 * t     * invtpow2 * x1
432                                                 + 3 * tpow2 * invt     * x2
433                                                 +     tpow3            * x3);
434                                         y = (        invtpow3 * y0
435                                                 + 3 * t     * invtpow2 * y1
436                                                 + 3 * tpow2 * invt     * y2
437                                                 +     tpow3            * y3);
438
439                                         y -= ptr->start_y;
440                                         x *= OVERSAMPLE;
441                                         y *= OVERSAMPLE;
442
443                                         if(j > 0)
444                                         {
445                                                 draw_line_clamped(temp, old_x, old_y, (int)x, (int)y, max);
446                                         }
447
448                                         old_x = (int)x;
449                                         old_y = (int)y;
450                                 }
451                         }
452
453 SET_TRACE
454 //printf("MaskUnit::process_package 1\n");
455
456
457
458
459
460 // Fill in the polygon in the horizontal direction
461                         for(int i = 0; i < oversampled_package_h; i++)
462                         {
463                                 unsigned char *row = (unsigned char*)temp->get_rows()[i];
464                                 int value = 0x0;
465                                 int total = 0;
466
467                                 for(int j = 0; j < oversampled_package_w; j++)
468                                         if(row[j] == max) total++;
469
470                                 if(total > 1)
471                                 {
472                                         if(total & 0x1) total--;
473                                         for(int j = 0; j < oversampled_package_w; j++)
474                                         {
475                                                 if(row[j] == max && total > 0)
476                                                 {
477                                                         if(value)
478                                                                 value = 0x0;
479                                                         else
480                                                                 value = max;
481                                                         total--;
482                                                 }
483                                                 else
484                                                 {
485                                                         if(value) row[j] = value;
486                                                 }
487                                         }
488                                 }
489                         }
490                 }
491
492
493 SET_TRACE
494
495
496
497
498
499 #define DOWNSAMPLE(type, temp_type, value) \
500 for(int i = 0; i < ptr->end_y - ptr->start_y; i++) \
501 { \
502         type *output_row = (type*)mask->get_rows()[i + ptr->start_y]; \
503         unsigned char **input_rows = (unsigned char**)temp->get_rows() + i * OVERSAMPLE; \
504  \
505  \
506         for(int j = 0; j < mask_w; j++) \
507         { \
508                 temp_type total = 0; \
509  \
510 /* Accumulate pixel */ \
511                 for(int k = 0; k < OVERSAMPLE; k++) \
512                 { \
513                         unsigned char *input_vector = input_rows[k] + j * OVERSAMPLE; \
514                         for(int l = 0; l < OVERSAMPLE; l++) \
515                         { \
516                                 total += (input_vector[l] ? value : 0); \
517                         } \
518                 } \
519  \
520 /* Divide pixel */ \
521                 total /= OVERSAMPLE * OVERSAMPLE; \
522  \
523                 output_row[j] = total; \
524         } \
525 }
526
527 SET_TRACE
528
529 // Downsample polygon
530                 switch(mask->get_color_model())
531                 {
532                         case BC_A8:
533                         {
534                                 unsigned char value;
535                                 value = (int)((float)engine->value / 100 * 0xff);
536                                 DOWNSAMPLE(unsigned char, int64_t, value);
537                                 break;
538                         }
539
540                         case BC_A16:
541                         {
542                                 uint16_t value;
543                                 value = (int)((float)engine->value / 100 * 0xffff);
544                                 DOWNSAMPLE(uint16_t, int64_t, value);
545                                 break;
546                         }
547
548                         case BC_A_FLOAT:
549                         {
550                                 float value;
551                                 value = (float)engine->value / 100;
552                                 DOWNSAMPLE(float, double, value);
553                                 break;
554                         }
555                 }
556         }
557
558 SET_TRACE
559
560 SET_TRACE
561
562         if(engine->step == DO_X_FEATHER)
563         {
564
565                 if(engine->recalculate)
566                 {
567 // Feather polygon
568                         if(engine->feather > 0) do_feather(engine->mask,
569                                 engine->temp_mask,
570                                 engine->feather,
571                                 ptr->start_y,
572                                 ptr->end_y,
573                                 0,
574                                 0);
575                 }
576 //printf("MaskUnit::process_package 3 %f\n", engine->feather);
577         }
578
579         if(engine->step == DO_Y_FEATHER)
580         {
581                 if(engine->recalculate)
582                 {
583 // Feather polygon
584                         if(engine->feather > 0) do_feather(engine->mask,
585                                 engine->temp_mask,
586                                 engine->feather,
587                                 0,
588                                 0,
589                                 ptr->start_x,
590                                 ptr->end_x);
591                 }
592         }
593
594         if(engine->step == DO_APPLY)
595         {
596 // Apply mask
597                 int mask_w = engine->mask->get_w();
598
599
600 #define APPLY_MASK_SUBTRACT_ALPHA(type, max, components, do_yuv) \
601 { \
602         type *output_row = (type*)engine->output->get_rows()[i]; \
603         type *mask_row = (type*)engine->mask->get_rows()[i]; \
604         int chroma_offset = (int)(max + 1) / 2; \
605  \
606         for(int j  = 0; j < mask_w; j++) \
607         { \
608                 if(components == 4) \
609                 { \
610                         output_row[j * 4 + 3] = output_row[j * 4 + 3] * (max - mask_row[j]) / max; \
611                 } \
612                 else \
613                 { \
614                         output_row[j * 3] = output_row[j * 3] * (max - mask_row[j]) / max; \
615  \
616                         output_row[j * 3 + 1] = output_row[j * 3 + 1] * (max - mask_row[j]) / max; \
617                         output_row[j * 3 + 2] = output_row[j * 3 + 2] * (max - mask_row[j]) / max; \
618  \
619                         if(do_yuv) \
620                         { \
621                                 output_row[j * 3 + 1] += chroma_offset * mask_row[j] / max; \
622                                 output_row[j * 3 + 2] += chroma_offset * mask_row[j] / max; \
623                         } \
624                 } \
625         } \
626 }
627
628 #define APPLY_MASK_MULTIPLY_ALPHA(type, max, components, do_yuv) \
629 { \
630         type *output_row = (type*)engine->output->get_rows()[i]; \
631         type *mask_row = (type*)engine->mask->get_rows()[i]; \
632         int chroma_offset = (int)(max + 1) / 2; \
633  \
634         for(int j  = 0; j < mask_w; j++) \
635         { \
636                 if(components == 4) \
637                 { \
638                         output_row[j * 4 + 3] = output_row[j * 4 + 3] * mask_row[j] / max; \
639                 } \
640                 else \
641                 { \
642                         output_row[j * 3] = output_row[j * 3] * mask_row[j] / max; \
643  \
644                         output_row[j * 3 + 1] = output_row[j * 3 + 1] * mask_row[j] / max; \
645                         output_row[j * 3 + 2] = output_row[j * 3 + 2] * mask_row[j] / max; \
646  \
647                         if(do_yuv) \
648                         { \
649                                 output_row[j * 3 + 1] += chroma_offset * (max - mask_row[j]) / max; \
650                                 output_row[j * 3 + 2] += chroma_offset * (max - mask_row[j]) / max; \
651                         } \
652                 } \
653         } \
654 }
655
656
657
658
659 //printf("MaskUnit::process_package 1 %d\n", engine->mode);
660                 for(int i = ptr->start_y; i < ptr->end_y; i++)
661                 {
662                         switch(engine->mode)
663                         {
664                                 case MASK_MULTIPLY_ALPHA:
665                                         switch(engine->output->get_color_model())
666                                         {
667                                                 case BC_RGB888:
668                                                         APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 3, 0);
669                                                         break;
670                                                 case BC_RGB_FLOAT:
671                                                         APPLY_MASK_MULTIPLY_ALPHA(float, 1.0, 3, 0);
672                                                         break;
673                                                 case BC_YUV888:
674                                                         APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 3, 1);
675                                                         break;
676                                                 case BC_RGBA_FLOAT:
677                                                         APPLY_MASK_MULTIPLY_ALPHA(float, 1.0, 4, 0);
678                                                         break;
679                                                 case BC_YUVA8888:
680                                                         APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 4, 1);
681                                                         break;
682                                                 case BC_RGBA8888:
683                                                         APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 4, 0);
684                                                         break;
685                                                 case BC_RGB161616:
686                                                         APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 3, 0);
687                                                         break;
688                                                 case BC_YUV161616:
689                                                         APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 3, 1);
690                                                         break;
691                                                 case BC_YUVA16161616:
692                                                         APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 4, 1);
693                                                         break;
694                                                 case BC_RGBA16161616:
695                                                         APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 4, 0);
696                                                         break;
697                                         }
698                                         break;
699
700                                 case MASK_SUBTRACT_ALPHA:
701                                         switch(engine->output->get_color_model())
702                                         {
703                                                 case BC_RGB888:
704                                                         APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 3, 0);
705                                                         break;
706                                                 case BC_RGB_FLOAT:
707                                                         APPLY_MASK_SUBTRACT_ALPHA(float, 1.0, 3, 0);
708                                                         break;
709                                                 case BC_RGBA_FLOAT:
710                                                         APPLY_MASK_SUBTRACT_ALPHA(float, 1.0, 4, 0);
711                                                         break;
712                                                 case BC_RGBA8888:
713                                                         APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 4, 0);
714                                                         break;
715                                                 case BC_YUV888:
716                                                         APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 3, 1);
717                                                         break;
718                                                 case BC_YUVA8888:
719                                                         APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 4, 1);
720                                                         break;
721                                                 case BC_RGB161616:
722                                                         APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 3, 0);
723                                                         break;
724                                                 case BC_RGBA16161616:
725                                                         APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 4, 0);
726                                                         break;
727                                                 case BC_YUV161616:
728                                                         APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 3, 1);
729                                                         break;
730                                                 case BC_YUVA16161616:
731                                                         APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 4, 1);
732                                                         break;
733                                         }
734                                         break;
735                         }
736                 }
737         }
738 }
739
740
741
742
743
744 MaskEngine::MaskEngine(int cpus)
745  : LoadServer(cpus, cpus * OVERSAMPLE * 2)
746 // : LoadServer(1, OVERSAMPLE * 2)
747 {
748         mask = 0;
749 }
750
751 MaskEngine::~MaskEngine()
752 {
753         if(mask)
754         {
755                 delete mask;
756                 delete temp_mask;
757         }
758
759         for(int i = 0; i < point_sets.total; i++)
760         {
761                 ArrayList<MaskPoint*> *points = point_sets.values[i];
762                 points->remove_all_objects();
763         }
764         point_sets.remove_all_objects();
765 }
766
767 int MaskEngine::points_equivalent(ArrayList<MaskPoint*> *new_points,
768         ArrayList<MaskPoint*> *points)
769 {
770 //printf("MaskEngine::points_equivalent %d %d\n", new_points->total, points->total);
771         if(new_points->total != points->total) return 0;
772
773         for(int i = 0; i < new_points->total; i++)
774         {
775                 if(!(*new_points->values[i] == *points->values[i])) return 0;
776         }
777
778         return 1;
779 }
780
781 void MaskEngine::do_mask(VFrame *output,
782         int64_t start_position_project,
783         MaskAutos *keyframe_set,
784         MaskAuto *keyframe,
785         MaskAuto *default_auto)
786 {
787         int new_color_model = 0;
788         recalculate = 0;
789
790         switch(output->get_color_model())
791         {
792                 case BC_RGB_FLOAT:
793                 case BC_RGBA_FLOAT:
794                         new_color_model = BC_A_FLOAT;
795                         break;
796
797                 case BC_RGB888:
798                 case BC_RGBA8888:
799                 case BC_YUV888:
800                 case BC_YUVA8888:
801                         new_color_model = BC_A8;
802                         break;
803
804                 case BC_RGB161616:
805                 case BC_RGBA16161616:
806                 case BC_YUV161616:
807                 case BC_YUVA16161616:
808                         new_color_model = BC_A16;
809                         break;
810         }
811
812 // Determine if recalculation is needed
813 SET_TRACE
814
815         if(mask &&
816                 (mask->get_w() != output->get_w() ||
817                 mask->get_h() != output->get_h() ||
818                 mask->get_color_model() != new_color_model))
819         {
820                 delete mask;
821                 delete temp_mask;
822                 mask = 0;
823                 recalculate = 1;
824         }
825
826         if(!recalculate)
827         {
828                 if(point_sets.total != keyframe_set->total_submasks(start_position_project,
829                         PLAY_FORWARD))
830                         recalculate = 1;
831         }
832
833         if(!recalculate)
834         {
835                 for(int i = 0;
836                         i < keyframe_set->total_submasks(start_position_project,
837                                 PLAY_FORWARD) && !recalculate;
838                         i++)
839                 {
840                         ArrayList<MaskPoint*> *new_points = new ArrayList<MaskPoint*>;
841                         keyframe_set->get_points(new_points,
842                                 i,
843                                 start_position_project,
844                                 PLAY_FORWARD);
845                         if(!points_equivalent(new_points, point_sets.values[i])) recalculate = 1;
846                         new_points->remove_all_objects();
847                         delete new_points;
848                 }
849         }
850
851         int new_value = keyframe_set->get_value(start_position_project,
852                 PLAY_FORWARD);
853         float new_feather = keyframe_set->get_feather(start_position_project,
854                 PLAY_FORWARD);
855
856         if(recalculate ||
857                 !EQUIV(new_feather, feather) ||
858                 !EQUIV(new_value, value))
859         {
860                 recalculate = 1;
861                 if(!mask)
862                 {
863                         mask = new VFrame(output->get_w(), output->get_h(),
864                                         new_color_model, 0);
865                         temp_mask = new VFrame(output->get_w(), output->get_h(),
866                                         new_color_model, 0);
867                 }
868                 if(new_feather > 0)
869                         temp_mask->clear_frame();
870                 else
871                         mask->clear_frame();
872
873                 for(int i = 0; i < point_sets.total; i++)
874                 {
875                         ArrayList<MaskPoint*> *points = point_sets.values[i];
876                         points->remove_all_objects();
877                 }
878                 point_sets.remove_all_objects();
879
880                 for(int i = 0;
881                         i < keyframe_set->total_submasks(start_position_project,
882                                 PLAY_FORWARD);
883                         i++)
884                 {
885                         ArrayList<MaskPoint*> *new_points = new ArrayList<MaskPoint*>;
886                         keyframe_set->get_points(new_points,
887                                 i,
888                                 start_position_project,
889                                 PLAY_FORWARD);
890                         point_sets.append(new_points);
891                 }
892         }
893
894
895
896         this->output = output;
897         this->mode = default_auto->mode;
898         this->feather = new_feather;
899         this->value = new_value;
900
901
902 // Run units
903 SET_TRACE
904         step = DO_MASK;
905         process_packages();
906         step = DO_Y_FEATHER;
907         process_packages();
908         step = DO_X_FEATHER;
909         process_packages();
910         step = DO_APPLY;
911         process_packages();
912 SET_TRACE
913
914
915 }
916
917 void MaskEngine::init_packages()
918 {
919 SET_TRACE
920 //printf("MaskEngine::init_packages 1\n");
921         int x0 = 0, y0 = 0, i = 0, n = get_total_packages();
922         int out_w = output->get_w(), out_h = output->get_h();
923 SET_TRACE
924         while( i < n ) {
925                 MaskPackage *ptr = (MaskPackage*)get_package(i++);
926                 int x1 = (out_w * i) / n, y1 = (out_h * i) / n;
927                 ptr->start_x = x0;  ptr->end_x = x1;
928                 ptr->start_y = y0;  ptr->end_y = y1;
929                 x0 = x1;  y0 = y1;
930         }
931 SET_TRACE
932 //printf("MaskEngine::init_packages 2\n");
933 }
934
935 LoadClient* MaskEngine::new_client()
936 {
937         return new MaskUnit(this);
938 }
939
940 LoadPackage* MaskEngine::new_package()
941 {
942         return new MaskPackage;
943 }
944