4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "bcsignals.h"
23 #include "condition.h"
26 #include "maskautos.h"
27 #include "maskengine.h"
29 #include "transportque.inc"
36 MaskPackage::MaskPackage()
40 MaskPackage::~MaskPackage()
50 MaskUnit::MaskUnit(MaskEngine *engine)
53 this->engine = engine;
68 void MaskUnit::draw_line_clamped(VFrame *frame,
69 int x1,int y1, int x2,int y2, unsigned char k)
71 int w = frame->get_w(), h = frame->get_h();
72 unsigned char **rows = (unsigned char**)frame->get_rows();
73 int dx = x2-x1, dy = y2-y1;
74 //printf("MaskUnit::draw_line_clamped(%d,%d -> %d,%d, 0x%02x\n", x1,y1, x2,y2, k);
76 int ax = 2*abs(dx), ay = 2*abs(dy);
78 if( ax > ay ) { /* x dominant */
81 int xx = x2; x2 = x1; x1 = xx;
82 int yy = y2; y2 = y1; y1 = yy;
84 if( x1 >= w || x2 < 0 ) return;
85 if( dx < 0 ) { dx = -dx; dy = -dy; }
86 int x = x1, y = y1, d = dx;
87 int sy = dy < 0 ? -1 : 1;
89 double py = -(double)dy/dx * x1 + y1 + 0.5;
96 if( y != y0 && y >= 0 && y < h ) {
98 unsigned char *bp = rows[y] + x;
99 *bp = *bp == k ? 0 : k;
101 if( x == x2 ) return;
102 if( d < 0 ) { d += ax; y += sy; }
106 else { /* y dominant */
107 if( dy == 0 ) return;
109 int xx = x2; x2 = x1; x1 = xx;
110 int yy = y2; y2 = y1; y1 = yy;
112 if( y1 >= h || y2 < 0 ) return;
113 if( dy < 0 ) { dx = -dx; dy = -dy; }
114 int x = x1, y = y1, d = dy;
115 int sx = dx < 0 ? -1 : 1;
117 double px = -(double)dx/dy * y1 + x1 + 0.5;
124 if( x >= 0 && x < w ) {
125 unsigned char *bp = rows[y] + x;
126 *bp = *bp == k ? 0 : k;
128 if( y == y2 ) return;
129 if( d < 0 ) { d += ay; x += sx; }
137 void MaskUnit::draw_line_clamped(VFrame *frame,
138 int x1, int y1, int x2, int y2, unsigned char k)
140 int draw_x1, draw_y1;
141 int draw_x2, draw_y2;
144 draw_x1 = x2; draw_y1 = y2;
145 draw_x2 = x1; draw_y2 = y1;
148 draw_x1 = x1; draw_y1 = y1;
149 draw_x2 = x2; draw_y2 = y2;
152 unsigned char **rows = (unsigned char**)frame->get_rows();
154 if(draw_y2 != draw_y1) {
155 float slope = ((float)draw_x2 - draw_x1) / ((float)draw_y2 - draw_y1);
156 int w = frame->get_w() - 1;
157 int h = frame->get_h();
159 for(float y = draw_y1; y < draw_y2; y++) {
160 if(y >= 0 && y < h) {
161 int x = (int)((y - draw_y1) * slope + draw_x1);
163 int x_i = CLIP(x, 0, w);
165 if(rows[y_i][x_i] == k)
176 void MaskUnit::blur_strip(double *val_p,
184 double *sp_m = src + size - 1;
186 double *vm = val_m + size - 1;
187 double initial_p = sp_p[0];
188 double initial_m = sp_m[0];
190 //printf("MaskUnit::blur_strip %d\n", size);
191 for(int k = 0; k < size; k++)
193 int terms = (k < 4) ? k : 4;
195 for(l = 0; l <= terms; l++)
197 *vp += n_p[l] * sp_p[-l] - d_p[l] * vp[-l];
198 *vm += n_m[l] * sp_m[l] - d_m[l] * vm[l];
203 *vp += (n_p[l] - bd_p[l]) * initial_p;
204 *vm += (n_m[l] - bd_m[l]) * initial_m;
212 for(int i = 0; i < size; i++)
214 double sum = val_p[i] + val_m[i];
220 void MaskUnit::do_feather(VFrame *output,
228 //printf("MaskUnit::do_feather %f\n", feather);
232 double std_dev = sqrt(-(double)(feather * feather) / (2 * log(1.0 / 255.0)));
233 div = sqrt(2 * M_PI) * std_dev;
234 constants[0] = -1.783 / std_dev;
235 constants[1] = -1.723 / std_dev;
236 constants[2] = 0.6318 / std_dev;
237 constants[3] = 1.997 / std_dev;
238 constants[4] = 1.6803 / div;
239 constants[5] = 3.735 / div;
240 constants[6] = -0.6803 / div;
241 constants[7] = -0.2598 / div;
243 n_p[0] = constants[4] + constants[6];
244 n_p[1] = exp(constants[1]) *
245 (constants[7] * sin(constants[3]) -
246 (constants[6] + 2 * constants[4]) * cos(constants[3])) +
248 (constants[5] * sin(constants[2]) -
249 (2 * constants[6] + constants[4]) * cos(constants[2]));
251 n_p[2] = 2 * exp(constants[0] + constants[1]) *
252 ((constants[4] + constants[6]) * cos(constants[3]) *
253 cos(constants[2]) - constants[5] *
254 cos(constants[3]) * sin(constants[2]) -
255 constants[7] * cos(constants[2]) * sin(constants[3])) +
256 constants[6] * exp(2 * constants[0]) +
257 constants[4] * exp(2 * constants[1]);
259 n_p[3] = exp(constants[1] + 2 * constants[0]) *
260 (constants[7] * sin(constants[3]) -
261 constants[6] * cos(constants[3])) +
262 exp(constants[0] + 2 * constants[1]) *
263 (constants[5] * sin(constants[2]) - constants[4] *
268 d_p[1] = -2 * exp(constants[1]) * cos(constants[3]) -
269 2 * exp(constants[0]) * cos(constants[2]);
271 d_p[2] = 4 * cos(constants[3]) * cos(constants[2]) *
272 exp(constants[0] + constants[1]) +
273 exp(2 * constants[1]) + exp (2 * constants[0]);
275 d_p[3] = -2 * cos(constants[2]) * exp(constants[0] + 2 * constants[1]) -
276 2 * cos(constants[3]) * exp(constants[1] + 2 * constants[0]);
278 d_p[4] = exp(2 * constants[0] + 2 * constants[1]);
280 for(int i = 0; i < 5; i++) d_m[i] = d_p[i];
283 for(int i = 1; i <= 4; i++)
284 n_m[i] = n_p[i] - d_p[i] * n_p[0];
286 double sum_n_p, sum_n_m, sum_d;
292 for(int i = 0; i < 5; i++)
299 a = sum_n_p / (1 + sum_d);
300 b = sum_n_m / (1 + sum_d);
302 for(int i = 0; i < 5; i++)
304 bd_p[i] = d_p[i] * a;
305 bd_m[i] = d_m[i] * b;
329 #define DO_FEATHER(type, max) \
331 int frame_w = input->get_w(); \
332 int frame_h = input->get_h(); \
333 int size = MAX(frame_w, frame_h); \
334 double *src = new double[size]; \
335 double *dst = new double[size]; \
336 double *val_p = new double[size]; \
337 double *val_m = new double[size]; \
338 type **in_rows = (type**)input->get_rows(); \
339 type **out_rows = (type**)output->get_rows(); \
342 /* printf("DO_FEATHER 1\n"); */ \
343 if(end_x > start_x) \
345 for(j = start_x; j < end_x; j++) \
347 /* printf("DO_FEATHER 1.1 %d\n", j); */ \
348 bzero(val_p, sizeof(double) * frame_h); \
349 bzero(val_m, sizeof(double) * frame_h); \
350 for(int k = 0; k < frame_h; k++) \
352 src[k] = (double)in_rows[k][j]; \
355 blur_strip(val_p, val_m, dst, src, frame_h, max); \
357 for(int k = 0; k < frame_h; k++) \
359 out_rows[k][j] = (type)dst[k]; \
364 if(end_y > start_y) \
366 for(j = start_y; j < end_y; j++) \
368 /* printf("DO_FEATHER 2 %d\n", j); */ \
369 bzero(val_p, sizeof(double) * frame_w); \
370 bzero(val_m, sizeof(double) * frame_w); \
371 for(int k = 0; k < frame_w; k++) \
373 src[k] = (double)out_rows[j][k]; \
376 blur_strip(val_p, val_m, dst, src, frame_w, max); \
378 for(int k = 0; k < frame_w; k++) \
380 out_rows[j][k] = (type)dst[k]; \
385 /* printf("DO_FEATHER 3\n"); */ \
391 /* printf("DO_FEATHER 4\n"); */ \
401 //printf("do_feather %d\n", frame->get_color_model());
402 switch(input->get_color_model())
405 DO_FEATHER(unsigned char, 0xff);
409 DO_FEATHER(uint16_t, 0xffff);
413 DO_FEATHER(float, 1);
422 void MaskUnit::process_package(LoadPackage *package)
424 MaskPackage *ptr = (MaskPackage*)package;
426 if(engine->recalculate &&
427 engine->step == DO_MASK)
430 if(engine->feather > 0)
431 mask = engine->temp_mask;
436 // Generated oversampling frame
437 int mask_w = mask->get_w();
438 //int mask_h = mask->get_h();
439 int oversampled_package_w = mask_w * OVERSAMPLE;
440 int oversampled_package_h = (ptr->end_y - ptr->start_y) * OVERSAMPLE;
441 //printf("MaskUnit::process_package 1\n");
445 (temp->get_w() != oversampled_package_w ||
446 temp->get_h() != oversampled_package_h))
451 //printf("MaskUnit::process_package 1\n");
458 oversampled_package_w,
459 oversampled_package_h,
466 //printf("MaskUnit::process_package 1 %d\n", engine->point_sets.total);
470 // Draw oversampled region of polygons on temp
471 for(int k = 0; k < engine->point_sets.total; k++)
474 unsigned char max = k + 1;
475 ArrayList<MaskPoint*> *points = engine->point_sets.values[k];
477 if(points->total < 3) continue;
478 //printf("MaskUnit::process_package 2 %d %d\n", k, points->total);
479 for(int i = 0; i < points->total; i++)
481 MaskPoint *point1 = points->values[i];
482 MaskPoint *point2 = (i >= points->total - 1) ?
484 points->values[i + 1];
487 int segments = (int)(sqrt(SQR(point1->x - point2->x) + SQR(point1->y - point2->y)));
488 if(point1->control_x2 == 0 &&
489 point1->control_y2 == 0 &&
490 point2->control_x1 == 0 &&
491 point2->control_y1 == 0)
493 float x0 = point1->x;
494 float y0 = point1->y;
495 float x1 = point1->x + point1->control_x2;
496 float y1 = point1->y + point1->control_y2;
497 float x2 = point2->x + point2->control_x1;
498 float y2 = point2->y + point2->control_y1;
499 float x3 = point2->x;
500 float y3 = point2->y;
502 for(int j = 0; j <= segments; j++)
504 float t = (float)j / segments;
506 float tpow3 = t * t * t;
508 float invtpow2 = invt * invt;
509 float invtpow3 = invt * invt * invt;
512 + 3 * t * invtpow2 * x1
513 + 3 * tpow2 * invt * x2
516 + 3 * t * invtpow2 * y1
517 + 3 * tpow2 * invt * y2
526 draw_line_clamped(temp, old_x, old_y, (int)x, (int)y, max);
535 //printf("MaskUnit::process_package 1\n");
541 // Fill in the polygon in the horizontal direction
542 for(int i = 0; i < oversampled_package_h; i++)
544 unsigned char *row = (unsigned char*)temp->get_rows()[i];
548 for(int j = 0; j < oversampled_package_w; j++)
549 if(row[j] == max) total++;
553 if(total & 0x1) total--;
554 for(int j = 0; j < oversampled_package_w; j++)
556 if(row[j] == max && total > 0)
566 if(value) row[j] = value;
580 #define DOWNSAMPLE(type, temp_type, value) \
581 for(int i = 0; i < ptr->end_y - ptr->start_y; i++) \
583 type *output_row = (type*)mask->get_rows()[i + ptr->start_y]; \
584 unsigned char **input_rows = (unsigned char**)temp->get_rows() + i * OVERSAMPLE; \
587 for(int j = 0; j < mask_w; j++) \
589 temp_type total = 0; \
591 /* Accumulate pixel */ \
592 for(int k = 0; k < OVERSAMPLE; k++) \
594 unsigned char *input_vector = input_rows[k] + j * OVERSAMPLE; \
595 for(int l = 0; l < OVERSAMPLE; l++) \
597 total += (input_vector[l] ? value : 0); \
602 total /= OVERSAMPLE * OVERSAMPLE; \
604 output_row[j] = total; \
610 // Downsample polygon
611 switch(mask->get_color_model())
616 value = (int)((float)engine->value / 100 * 0xff);
617 DOWNSAMPLE(unsigned char, int64_t, value);
624 value = (int)((float)engine->value / 100 * 0xffff);
625 DOWNSAMPLE(uint16_t, int64_t, value);
632 value = (float)engine->value / 100;
633 DOWNSAMPLE(float, double, value);
643 if(engine->step == DO_X_FEATHER)
646 if(engine->recalculate)
649 if(engine->feather > 0) do_feather(engine->mask,
657 //printf("MaskUnit::process_package 3 %f\n", engine->feather);
660 if(engine->step == DO_Y_FEATHER)
662 if(engine->recalculate)
665 if(engine->feather > 0) do_feather(engine->mask,
675 if(engine->step == DO_APPLY)
678 int mask_w = engine->mask->get_w();
681 #define APPLY_MASK_SUBTRACT_ALPHA(type, max, components, do_yuv) \
683 type *output_row = (type*)engine->output->get_rows()[i]; \
684 type *mask_row = (type*)engine->mask->get_rows()[i]; \
685 int chroma_offset = (int)(max + 1) / 2; \
687 for(int j = 0; j < mask_w; j++) \
689 if(components == 4) \
691 output_row[j * 4 + 3] = output_row[j * 4 + 3] * (max - mask_row[j]) / max; \
695 output_row[j * 3] = output_row[j * 3] * (max - mask_row[j]) / max; \
697 output_row[j * 3 + 1] = output_row[j * 3 + 1] * (max - mask_row[j]) / max; \
698 output_row[j * 3 + 2] = output_row[j * 3 + 2] * (max - mask_row[j]) / max; \
702 output_row[j * 3 + 1] += chroma_offset * mask_row[j] / max; \
703 output_row[j * 3 + 2] += chroma_offset * mask_row[j] / max; \
709 #define APPLY_MASK_MULTIPLY_ALPHA(type, max, components, do_yuv) \
711 type *output_row = (type*)engine->output->get_rows()[i]; \
712 type *mask_row = (type*)engine->mask->get_rows()[i]; \
713 int chroma_offset = (int)(max + 1) / 2; \
715 for(int j = 0; j < mask_w; j++) \
717 if(components == 4) \
719 output_row[j * 4 + 3] = output_row[j * 4 + 3] * mask_row[j] / max; \
723 output_row[j * 3] = output_row[j * 3] * mask_row[j] / max; \
725 output_row[j * 3 + 1] = output_row[j * 3 + 1] * mask_row[j] / max; \
726 output_row[j * 3 + 2] = output_row[j * 3 + 2] * mask_row[j] / max; \
730 output_row[j * 3 + 1] += chroma_offset * (max - mask_row[j]) / max; \
731 output_row[j * 3 + 2] += chroma_offset * (max - mask_row[j]) / max; \
740 //printf("MaskUnit::process_package 1 %d\n", engine->mode);
741 for(int i = ptr->start_y; i < ptr->end_y; i++)
745 case MASK_MULTIPLY_ALPHA:
746 switch(engine->output->get_color_model())
749 APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 3, 0);
752 APPLY_MASK_MULTIPLY_ALPHA(float, 1.0, 3, 0);
755 APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 3, 1);
758 APPLY_MASK_MULTIPLY_ALPHA(float, 1.0, 4, 0);
761 APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 4, 1);
764 APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 4, 0);
767 APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 3, 0);
770 APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 3, 1);
772 case BC_YUVA16161616:
773 APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 4, 1);
775 case BC_RGBA16161616:
776 APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 4, 0);
781 case MASK_SUBTRACT_ALPHA:
782 switch(engine->output->get_color_model())
785 APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 3, 0);
788 APPLY_MASK_SUBTRACT_ALPHA(float, 1.0, 3, 0);
791 APPLY_MASK_SUBTRACT_ALPHA(float, 1.0, 4, 0);
794 APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 4, 0);
797 APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 3, 1);
800 APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 4, 1);
803 APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 3, 0);
805 case BC_RGBA16161616:
806 APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 4, 0);
809 APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 3, 1);
811 case BC_YUVA16161616:
812 APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 4, 1);
825 MaskEngine::MaskEngine(int cpus)
826 : LoadServer(cpus, cpus * OVERSAMPLE * 2)
827 // : LoadServer(1, OVERSAMPLE * 2)
832 MaskEngine::~MaskEngine()
840 for(int i = 0; i < point_sets.total; i++)
842 ArrayList<MaskPoint*> *points = point_sets.values[i];
843 points->remove_all_objects();
845 point_sets.remove_all_objects();
848 int MaskEngine::points_equivalent(ArrayList<MaskPoint*> *new_points,
849 ArrayList<MaskPoint*> *points)
851 //printf("MaskEngine::points_equivalent %d %d\n", new_points->total, points->total);
852 if(new_points->total != points->total) return 0;
854 for(int i = 0; i < new_points->total; i++)
856 if(!(*new_points->values[i] == *points->values[i])) return 0;
862 void MaskEngine::do_mask(VFrame *output,
863 int64_t start_position_project,
864 MaskAutos *keyframe_set,
866 MaskAuto *default_auto)
868 int new_color_model = 0;
871 switch(output->get_color_model())
875 new_color_model = BC_A_FLOAT;
882 new_color_model = BC_A8;
886 case BC_RGBA16161616:
888 case BC_YUVA16161616:
889 new_color_model = BC_A16;
893 // Determine if recalculation is needed
897 (mask->get_w() != output->get_w() ||
898 mask->get_h() != output->get_h() ||
899 mask->get_color_model() != new_color_model))
909 if(point_sets.total != keyframe_set->total_submasks(start_position_project,
917 i < keyframe_set->total_submasks(start_position_project,
918 PLAY_FORWARD) && !recalculate;
921 ArrayList<MaskPoint*> *new_points = new ArrayList<MaskPoint*>;
922 keyframe_set->get_points(new_points,
924 start_position_project,
926 if(!points_equivalent(new_points, point_sets.values[i])) recalculate = 1;
927 new_points->remove_all_objects();
932 int new_value = keyframe_set->get_value(start_position_project,
934 float new_feather = keyframe_set->get_feather(start_position_project,
938 !EQUIV(new_feather, feather) ||
939 !EQUIV(new_value, value))
950 temp_mask = new VFrame(0,
958 temp_mask->clear_frame();
962 for(int i = 0; i < point_sets.total; i++)
964 ArrayList<MaskPoint*> *points = point_sets.values[i];
965 points->remove_all_objects();
967 point_sets.remove_all_objects();
970 i < keyframe_set->total_submasks(start_position_project,
974 ArrayList<MaskPoint*> *new_points = new ArrayList<MaskPoint*>;
975 keyframe_set->get_points(new_points,
977 start_position_project,
979 point_sets.append(new_points);
985 this->output = output;
986 this->mode = default_auto->mode;
987 this->feather = new_feather;
988 this->value = new_value;
1006 void MaskEngine::init_packages()
1009 //printf("MaskEngine::init_packages 1\n");
1010 int division = (int)((float)output->get_h() / (get_total_packages() / 2) + 0.5);
1011 if(division < 1) division = 1;
1014 for(int i = 0; i < get_total_packages(); i++)
1016 MaskPackage *ptr = (MaskPackage*)get_package(i);
1018 ptr->start_y = output->get_h() * i / get_total_packages();
1019 ptr->end_y = output->get_h() * (i + 1) / get_total_packages();
1021 ptr->start_x = output->get_w() * i / get_total_packages();
1022 ptr->end_x = output->get_w() * (i + 1) / get_total_packages();
1025 //printf("MaskEngine::init_packages 2\n");
1028 LoadClient* MaskEngine::new_client()
1030 return new MaskUnit(this);
1033 LoadPackage* MaskEngine::new_package()
1035 return new MaskPackage;