7f7917661e9194ae0094bac728be432f2f0ccf3a
[goodguy/history.git] / 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                 {
375                         delete temp;
376                         temp = 0;
377                 }
378 //printf("MaskUnit::process_package 1\n");
379
380 SET_TRACE
381                 if(!temp)
382                 {
383                         temp = new VFrame(0,
384                                 -1,
385                                 oversampled_package_w,
386                                 oversampled_package_h,
387                                 BC_A8,
388                                 -1);
389                 }
390
391 SET_TRACE
392                 temp->clear_frame();
393 //printf("MaskUnit::process_package 1 %d\n", engine->point_sets.total);
394
395 SET_TRACE
396
397 // Draw oversampled region of polygons on temp
398                 for(int k = 0; k < engine->point_sets.total; k++)
399                 {
400                         int old_x, old_y;
401                         unsigned char max = k + 1;
402                         ArrayList<MaskPoint*> *points = engine->point_sets.values[k];
403
404                         if(points->total < 3) continue;
405 //printf("MaskUnit::process_package 2 %d %d\n", k, points->total);
406                         for(int i = 0; i < points->total; i++)
407                         {
408                                 MaskPoint *point1 = points->values[i];
409                                 MaskPoint *point2 = (i >= points->total - 1) ?
410                                         points->values[0] :
411                                         points->values[i + 1];
412
413                                 float x, y;
414                                 int segments = (int)(sqrt(SQR(point1->x - point2->x) + SQR(point1->y - point2->y)));
415                                 if(point1->control_x2 == 0 &&
416                                         point1->control_y2 == 0 &&
417                                         point2->control_x1 == 0 &&
418                                         point2->control_y1 == 0)
419                                         segments = 1;
420                                 float x0 = point1->x;
421                                 float y0 = point1->y;
422                                 float x1 = point1->x + point1->control_x2;
423                                 float y1 = point1->y + point1->control_y2;
424                                 float x2 = point2->x + point2->control_x1;
425                                 float y2 = point2->y + point2->control_y1;
426                                 float x3 = point2->x;
427                                 float y3 = point2->y;
428
429                                 for(int j = 0; j <= segments; j++)
430                                 {
431                                         float t = (float)j / segments;
432                                         float tpow2 = t * t;
433                                         float tpow3 = t * t * t;
434                                         float invt = 1 - t;
435                                         float invtpow2 = invt * invt;
436                                         float invtpow3 = invt * invt * invt;
437
438                                         x = (        invtpow3 * x0
439                                                 + 3 * t     * invtpow2 * x1
440                                                 + 3 * tpow2 * invt     * x2
441                                                 +     tpow3            * x3);
442                                         y = (        invtpow3 * y0
443                                                 + 3 * t     * invtpow2 * y1
444                                                 + 3 * tpow2 * invt     * y2
445                                                 +     tpow3            * y3);
446
447                                         y -= ptr->start_y;
448                                         x *= OVERSAMPLE;
449                                         y *= OVERSAMPLE;
450
451                                         if(j > 0)
452                                         {
453                                                 draw_line_clamped(temp, old_x, old_y, (int)x, (int)y, max);
454                                         }
455
456                                         old_x = (int)x;
457                                         old_y = (int)y;
458                                 }
459                         }
460
461 SET_TRACE
462 //printf("MaskUnit::process_package 1\n");
463
464
465
466
467
468 // Fill in the polygon in the horizontal direction
469                         for(int i = 0; i < oversampled_package_h; i++)
470                         {
471                                 unsigned char *row = (unsigned char*)temp->get_rows()[i];
472                                 int value = 0x0;
473                                 int total = 0;
474
475                                 for(int j = 0; j < oversampled_package_w; j++)
476                                         if(row[j] == max) total++;
477
478                                 if(total > 1)
479                                 {
480                                         if(total & 0x1) total--;
481                                         for(int j = 0; j < oversampled_package_w; j++)
482                                         {
483                                                 if(row[j] == max && total > 0)
484                                                 {
485                                                         if(value)
486                                                                 value = 0x0;
487                                                         else
488                                                                 value = max;
489                                                         total--;
490                                                 }
491                                                 else
492                                                 {
493                                                         if(value) row[j] = value;
494                                                 }
495                                         }
496                                 }
497                         }
498                 }
499
500
501 SET_TRACE
502
503
504
505
506
507 #define DOWNSAMPLE(type, temp_type, value) \
508 for(int i = 0; i < ptr->end_y - ptr->start_y; i++) \
509 { \
510         type *output_row = (type*)mask->get_rows()[i + ptr->start_y]; \
511         unsigned char **input_rows = (unsigned char**)temp->get_rows() + i * OVERSAMPLE; \
512  \
513  \
514         for(int j = 0; j < mask_w; j++) \
515         { \
516                 temp_type total = 0; \
517  \
518 /* Accumulate pixel */ \
519                 for(int k = 0; k < OVERSAMPLE; k++) \
520                 { \
521                         unsigned char *input_vector = input_rows[k] + j * OVERSAMPLE; \
522                         for(int l = 0; l < OVERSAMPLE; l++) \
523                         { \
524                                 total += (input_vector[l] ? value : 0); \
525                         } \
526                 } \
527  \
528 /* Divide pixel */ \
529                 total /= OVERSAMPLE * OVERSAMPLE; \
530  \
531                 output_row[j] = total; \
532         } \
533 }
534
535 SET_TRACE
536
537 // Downsample polygon
538                 switch(mask->get_color_model())
539                 {
540                         case BC_A8:
541                         {
542                                 unsigned char value;
543                                 value = (int)((float)engine->value / 100 * 0xff);
544                                 DOWNSAMPLE(unsigned char, int64_t, value);
545                                 break;
546                         }
547
548                         case BC_A16:
549                         {
550                                 uint16_t value;
551                                 value = (int)((float)engine->value / 100 * 0xffff);
552                                 DOWNSAMPLE(uint16_t, int64_t, value);
553                                 break;
554                         }
555
556                         case BC_A_FLOAT:
557                         {
558                                 float value;
559                                 value = (float)engine->value / 100;
560                                 DOWNSAMPLE(float, double, value);
561                                 break;
562                         }
563                 }
564         }
565
566 SET_TRACE
567
568 SET_TRACE
569
570         if(engine->step == DO_X_FEATHER)
571         {
572
573                 if(engine->recalculate)
574                 {
575 // Feather polygon
576                         if(engine->feather > 0) do_feather(engine->mask,
577                                 engine->temp_mask,
578                                 engine->feather,
579                                 ptr->start_y,
580                                 ptr->end_y,
581                                 0,
582                                 0);
583                 }
584 //printf("MaskUnit::process_package 3 %f\n", engine->feather);
585         }
586
587         if(engine->step == DO_Y_FEATHER)
588         {
589                 if(engine->recalculate)
590                 {
591 // Feather polygon
592                         if(engine->feather > 0) do_feather(engine->mask,
593                                 engine->temp_mask,
594                                 engine->feather,
595                                 0,
596                                 0,
597                                 ptr->start_x,
598                                 ptr->end_x);
599                 }
600         }
601
602         if(engine->step == DO_APPLY)
603         {
604 // Apply mask
605                 int mask_w = engine->mask->get_w();
606
607
608 #define APPLY_MASK_SUBTRACT_ALPHA(type, max, components, do_yuv) \
609 { \
610         type *output_row = (type*)engine->output->get_rows()[i]; \
611         type *mask_row = (type*)engine->mask->get_rows()[i]; \
612         int chroma_offset = (int)(max + 1) / 2; \
613  \
614         for(int j  = 0; j < mask_w; j++) \
615         { \
616                 if(components == 4) \
617                 { \
618                         output_row[j * 4 + 3] = output_row[j * 4 + 3] * (max - mask_row[j]) / max; \
619                 } \
620                 else \
621                 { \
622                         output_row[j * 3] = output_row[j * 3] * (max - mask_row[j]) / max; \
623  \
624                         output_row[j * 3 + 1] = output_row[j * 3 + 1] * (max - mask_row[j]) / max; \
625                         output_row[j * 3 + 2] = output_row[j * 3 + 2] * (max - mask_row[j]) / max; \
626  \
627                         if(do_yuv) \
628                         { \
629                                 output_row[j * 3 + 1] += chroma_offset * mask_row[j] / max; \
630                                 output_row[j * 3 + 2] += chroma_offset * mask_row[j] / max; \
631                         } \
632                 } \
633         } \
634 }
635
636 #define APPLY_MASK_MULTIPLY_ALPHA(type, max, components, do_yuv) \
637 { \
638         type *output_row = (type*)engine->output->get_rows()[i]; \
639         type *mask_row = (type*)engine->mask->get_rows()[i]; \
640         int chroma_offset = (int)(max + 1) / 2; \
641  \
642         for(int j  = 0; j < mask_w; j++) \
643         { \
644                 if(components == 4) \
645                 { \
646                         output_row[j * 4 + 3] = output_row[j * 4 + 3] * mask_row[j] / max; \
647                 } \
648                 else \
649                 { \
650                         output_row[j * 3] = output_row[j * 3] * mask_row[j] / max; \
651  \
652                         output_row[j * 3 + 1] = output_row[j * 3 + 1] * mask_row[j] / max; \
653                         output_row[j * 3 + 2] = output_row[j * 3 + 2] * mask_row[j] / max; \
654  \
655                         if(do_yuv) \
656                         { \
657                                 output_row[j * 3 + 1] += chroma_offset * (max - mask_row[j]) / max; \
658                                 output_row[j * 3 + 2] += chroma_offset * (max - mask_row[j]) / max; \
659                         } \
660                 } \
661         } \
662 }
663
664
665
666
667 //printf("MaskUnit::process_package 1 %d\n", engine->mode);
668                 for(int i = ptr->start_y; i < ptr->end_y; i++)
669                 {
670                         switch(engine->mode)
671                         {
672                                 case MASK_MULTIPLY_ALPHA:
673                                         switch(engine->output->get_color_model())
674                                         {
675                                                 case BC_RGB888:
676                                                         APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 3, 0);
677                                                         break;
678                                                 case BC_RGB_FLOAT:
679                                                         APPLY_MASK_MULTIPLY_ALPHA(float, 1.0, 3, 0);
680                                                         break;
681                                                 case BC_YUV888:
682                                                         APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 3, 1);
683                                                         break;
684                                                 case BC_RGBA_FLOAT:
685                                                         APPLY_MASK_MULTIPLY_ALPHA(float, 1.0, 4, 0);
686                                                         break;
687                                                 case BC_YUVA8888:
688                                                         APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 4, 1);
689                                                         break;
690                                                 case BC_RGBA8888:
691                                                         APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 4, 0);
692                                                         break;
693                                                 case BC_RGB161616:
694                                                         APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 3, 0);
695                                                         break;
696                                                 case BC_YUV161616:
697                                                         APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 3, 1);
698                                                         break;
699                                                 case BC_YUVA16161616:
700                                                         APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 4, 1);
701                                                         break;
702                                                 case BC_RGBA16161616:
703                                                         APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 4, 0);
704                                                         break;
705                                         }
706                                         break;
707
708                                 case MASK_SUBTRACT_ALPHA:
709                                         switch(engine->output->get_color_model())
710                                         {
711                                                 case BC_RGB888:
712                                                         APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 3, 0);
713                                                         break;
714                                                 case BC_RGB_FLOAT:
715                                                         APPLY_MASK_SUBTRACT_ALPHA(float, 1.0, 3, 0);
716                                                         break;
717                                                 case BC_RGBA_FLOAT:
718                                                         APPLY_MASK_SUBTRACT_ALPHA(float, 1.0, 4, 0);
719                                                         break;
720                                                 case BC_RGBA8888:
721                                                         APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 4, 0);
722                                                         break;
723                                                 case BC_YUV888:
724                                                         APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 3, 1);
725                                                         break;
726                                                 case BC_YUVA8888:
727                                                         APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 4, 1);
728                                                         break;
729                                                 case BC_RGB161616:
730                                                         APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 3, 0);
731                                                         break;
732                                                 case BC_RGBA16161616:
733                                                         APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 4, 0);
734                                                         break;
735                                                 case BC_YUV161616:
736                                                         APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 3, 1);
737                                                         break;
738                                                 case BC_YUVA16161616:
739                                                         APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 4, 1);
740                                                         break;
741                                         }
742                                         break;
743                         }
744                 }
745         }
746 }
747
748
749
750
751
752 MaskEngine::MaskEngine(int cpus)
753  : LoadServer(cpus, cpus * OVERSAMPLE * 2)
754 // : LoadServer(1, OVERSAMPLE * 2)
755 {
756         mask = 0;
757 }
758
759 MaskEngine::~MaskEngine()
760 {
761         if(mask)
762         {
763                 delete mask;
764                 delete temp_mask;
765         }
766
767         for(int i = 0; i < point_sets.total; i++)
768         {
769                 ArrayList<MaskPoint*> *points = point_sets.values[i];
770                 points->remove_all_objects();
771         }
772         point_sets.remove_all_objects();
773 }
774
775 int MaskEngine::points_equivalent(ArrayList<MaskPoint*> *new_points,
776         ArrayList<MaskPoint*> *points)
777 {
778 //printf("MaskEngine::points_equivalent %d %d\n", new_points->total, points->total);
779         if(new_points->total != points->total) return 0;
780
781         for(int i = 0; i < new_points->total; i++)
782         {
783                 if(!(*new_points->values[i] == *points->values[i])) return 0;
784         }
785
786         return 1;
787 }
788
789 void MaskEngine::do_mask(VFrame *output,
790         int64_t start_position_project,
791         MaskAutos *keyframe_set,
792         MaskAuto *keyframe,
793         MaskAuto *default_auto)
794 {
795         int new_color_model = 0;
796         recalculate = 0;
797
798         switch(output->get_color_model())
799         {
800                 case BC_RGB_FLOAT:
801                 case BC_RGBA_FLOAT:
802                         new_color_model = BC_A_FLOAT;
803                         break;
804
805                 case BC_RGB888:
806                 case BC_RGBA8888:
807                 case BC_YUV888:
808                 case BC_YUVA8888:
809                         new_color_model = BC_A8;
810                         break;
811
812                 case BC_RGB161616:
813                 case BC_RGBA16161616:
814                 case BC_YUV161616:
815                 case BC_YUVA16161616:
816                         new_color_model = BC_A16;
817                         break;
818         }
819
820 // Determine if recalculation is needed
821 SET_TRACE
822
823         if(mask &&
824                 (mask->get_w() != output->get_w() ||
825                 mask->get_h() != output->get_h() ||
826                 mask->get_color_model() != new_color_model))
827         {
828                 delete mask;
829                 delete temp_mask;
830                 mask = 0;
831                 recalculate = 1;
832         }
833
834         if(!recalculate)
835         {
836                 if(point_sets.total != keyframe_set->total_submasks(start_position_project,
837                         PLAY_FORWARD))
838                         recalculate = 1;
839         }
840
841         if(!recalculate)
842         {
843                 for(int i = 0;
844                         i < keyframe_set->total_submasks(start_position_project,
845                                 PLAY_FORWARD) && !recalculate;
846                         i++)
847                 {
848                         ArrayList<MaskPoint*> *new_points = new ArrayList<MaskPoint*>;
849                         keyframe_set->get_points(new_points,
850                                 i,
851                                 start_position_project,
852                                 PLAY_FORWARD);
853                         if(!points_equivalent(new_points, point_sets.values[i])) recalculate = 1;
854                         new_points->remove_all_objects();
855                         delete new_points;
856                 }
857         }
858
859         int new_value = keyframe_set->get_value(start_position_project,
860                 PLAY_FORWARD);
861         float new_feather = keyframe_set->get_feather(start_position_project,
862                 PLAY_FORWARD);
863
864         if(recalculate ||
865                 !EQUIV(new_feather, feather) ||
866                 !EQUIV(new_value, value))
867         {
868                 recalculate = 1;
869                 if(!mask)
870                 {
871                         mask = new VFrame(0,
872                                         -1,
873                                         output->get_w(),
874                                         output->get_h(),
875                                         new_color_model,
876                                         -1);
877                         temp_mask = new VFrame(0,
878                                         -1,
879                                         output->get_w(),
880                                         output->get_h(),
881                                         new_color_model,
882                                         -1);
883                 }
884                 if(new_feather > 0)
885                         temp_mask->clear_frame();
886                 else
887                         mask->clear_frame();
888
889                 for(int i = 0; i < point_sets.total; i++)
890                 {
891                         ArrayList<MaskPoint*> *points = point_sets.values[i];
892                         points->remove_all_objects();
893                 }
894                 point_sets.remove_all_objects();
895
896                 for(int i = 0;
897                         i < keyframe_set->total_submasks(start_position_project,
898                                 PLAY_FORWARD);
899                         i++)
900                 {
901                         ArrayList<MaskPoint*> *new_points = new ArrayList<MaskPoint*>;
902                         keyframe_set->get_points(new_points,
903                                 i,
904                                 start_position_project,
905                                 PLAY_FORWARD);
906                         point_sets.append(new_points);
907                 }
908         }
909
910
911
912         this->output = output;
913         this->mode = default_auto->mode;
914         this->feather = new_feather;
915         this->value = new_value;
916
917
918 // Run units
919 SET_TRACE
920         step = DO_MASK;
921         process_packages();
922         step = DO_Y_FEATHER;
923         process_packages();
924         step = DO_X_FEATHER;
925         process_packages();
926         step = DO_APPLY;
927         process_packages();
928 SET_TRACE
929
930
931 }
932
933 void MaskEngine::init_packages()
934 {
935 SET_TRACE
936 //printf("MaskEngine::init_packages 1\n");
937         int division = (int)((float)output->get_h() / (get_total_packages() / 2) + 0.5);
938         if(division < 1) division = 1;
939
940 SET_TRACE
941         for(int i = 0; i < get_total_packages(); i++)
942         {
943                 MaskPackage *ptr = (MaskPackage*)get_package(i);
944
945                 ptr->start_y = output->get_h() * i / get_total_packages();
946                 ptr->end_y = output->get_h() * (i + 1) / get_total_packages();
947
948                 ptr->start_x = output->get_w() * i / get_total_packages();
949                 ptr->end_x = output->get_w() * (i + 1) / get_total_packages();
950         }
951 SET_TRACE
952 //printf("MaskEngine::init_packages 2\n");
953 }
954
955 LoadClient* MaskEngine::new_client()
956 {
957         return new MaskUnit(this);
958 }
959
960 LoadPackage* MaskEngine::new_package()
961 {
962         return new MaskPackage;
963 }
964