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;
66 void MaskUnit::draw_line_clamped(VFrame *frame,
67 int x1, int y1, int x2, int y2, unsigned char k)
73 draw_x1 = x2; draw_y1 = y2;
74 draw_x2 = x1; draw_y2 = y1;
77 draw_x1 = x1; draw_y1 = y1;
78 draw_x2 = x2; draw_y2 = y2;
81 unsigned char **rows = (unsigned char**)frame->get_rows();
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();
88 for(float y = draw_y1; y < draw_y2; y++) {
90 int x = (int)((y - draw_y1) * slope + draw_x1);
92 int x_i = CLIP(x, 0, w);
94 if(rows[y_i][x_i] == k)
103 void MaskUnit::blur_strip(double *val_p,
111 double *sp_m = src + size - 1;
113 double *vm = val_m + size - 1;
114 double initial_p = sp_p[0];
115 double initial_m = sp_m[0];
117 //printf("MaskUnit::blur_strip %d\n", size);
118 for(int k = 0; k < size; k++)
120 int terms = (k < 4) ? k : 4;
122 for(l = 0; l <= terms; l++)
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];
130 *vp += (n_p[l] - bd_p[l]) * initial_p;
131 *vm += (n_m[l] - bd_m[l]) * initial_m;
139 for(int i = 0; i < size; i++)
141 double sum = val_p[i] + val_m[i];
147 void MaskUnit::do_feather(VFrame *output,
155 //printf("MaskUnit::do_feather %f\n", feather);
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;
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])) +
175 (constants[5] * sin(constants[2]) -
176 (2 * constants[6] + constants[4]) * cos(constants[2]));
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]);
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] *
195 d_p[1] = -2 * exp(constants[1]) * cos(constants[3]) -
196 2 * exp(constants[0]) * cos(constants[2]);
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]);
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]);
205 d_p[4] = exp(2 * constants[0] + 2 * constants[1]);
207 for(int i = 0; i < 5; i++) d_m[i] = d_p[i];
210 for(int i = 1; i <= 4; i++)
211 n_m[i] = n_p[i] - d_p[i] * n_p[0];
213 double sum_n_p, sum_n_m, sum_d;
219 for(int i = 0; i < 5; i++)
226 a = sum_n_p / (1 + sum_d);
227 b = sum_n_m / (1 + sum_d);
229 for(int i = 0; i < 5; i++)
231 bd_p[i] = d_p[i] * a;
232 bd_m[i] = d_m[i] * b;
256 #define DO_FEATHER(type, max) \
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(); \
269 /* printf("DO_FEATHER 1\n"); */ \
270 if(end_x > start_x) \
272 for(j = start_x; j < end_x; j++) \
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++) \
279 src[k] = (double)in_rows[k][j]; \
282 blur_strip(val_p, val_m, dst, src, frame_h, max); \
284 for(int k = 0; k < frame_h; k++) \
286 out_rows[k][j] = (type)dst[k]; \
291 if(end_y > start_y) \
293 for(j = start_y; j < end_y; j++) \
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++) \
300 src[k] = (double)out_rows[j][k]; \
303 blur_strip(val_p, val_m, dst, src, frame_w, max); \
305 for(int k = 0; k < frame_w; k++) \
307 out_rows[j][k] = (type)dst[k]; \
312 /* printf("DO_FEATHER 3\n"); */ \
318 /* printf("DO_FEATHER 4\n"); */ \
328 //printf("do_feather %d\n", frame->get_color_model());
329 switch(input->get_color_model())
332 DO_FEATHER(unsigned char, 0xff);
336 DO_FEATHER(uint16_t, 0xffff);
340 DO_FEATHER(float, 1);
349 void MaskUnit::process_package(LoadPackage *package)
351 MaskPackage *ptr = (MaskPackage*)package;
353 if(engine->recalculate &&
354 engine->step == DO_MASK)
357 if(engine->feather > 0)
358 mask = engine->temp_mask;
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");
372 (temp->get_w() != oversampled_package_w ||
373 temp->get_h() != oversampled_package_h)) {
374 delete temp; temp = 0;
376 //printf("MaskUnit::process_package 1\n");
380 temp = new VFrame(oversampled_package_w, oversampled_package_h, BC_A8, 0);
385 //printf("MaskUnit::process_package 1 %d\n", engine->point_sets.total);
389 // Draw oversampled region of polygons on temp
390 for(int k = 0; k < engine->point_sets.total; k++)
393 unsigned char max = k + 1;
394 ArrayList<MaskPoint*> *points = engine->point_sets.values[k];
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++)
400 MaskPoint *point1 = points->values[i];
401 MaskPoint *point2 = (i >= points->total - 1) ?
403 points->values[i + 1];
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)
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;
421 for(int j = 0; j <= segments; j++)
423 float t = (float)j / segments;
425 float tpow3 = t * t * t;
427 float invtpow2 = invt * invt;
428 float invtpow3 = invt * invt * invt;
431 + 3 * t * invtpow2 * x1
432 + 3 * tpow2 * invt * x2
435 + 3 * t * invtpow2 * y1
436 + 3 * tpow2 * invt * y2
445 draw_line_clamped(temp, old_x, old_y, (int)x, (int)y, max);
454 //printf("MaskUnit::process_package 1\n");
460 // Fill in the polygon in the horizontal direction
461 for(int i = 0; i < oversampled_package_h; i++)
463 unsigned char *row = (unsigned char*)temp->get_rows()[i];
467 for(int j = 0; j < oversampled_package_w; j++)
468 if(row[j] == max) total++;
472 if(total & 0x1) total--;
473 for(int j = 0; j < oversampled_package_w; j++)
475 if(row[j] == max && total > 0)
485 if(value) row[j] = value;
499 #define DOWNSAMPLE(type, temp_type, value) \
500 for(int i = 0; i < ptr->end_y - ptr->start_y; i++) \
502 type *output_row = (type*)mask->get_rows()[i + ptr->start_y]; \
503 unsigned char **input_rows = (unsigned char**)temp->get_rows() + i * OVERSAMPLE; \
506 for(int j = 0; j < mask_w; j++) \
508 temp_type total = 0; \
510 /* Accumulate pixel */ \
511 for(int k = 0; k < OVERSAMPLE; k++) \
513 unsigned char *input_vector = input_rows[k] + j * OVERSAMPLE; \
514 for(int l = 0; l < OVERSAMPLE; l++) \
516 total += (input_vector[l] ? value : 0); \
521 total /= OVERSAMPLE * OVERSAMPLE; \
523 output_row[j] = total; \
529 // Downsample polygon
530 switch(mask->get_color_model())
535 value = (int)((float)engine->value / 100 * 0xff);
536 DOWNSAMPLE(unsigned char, int64_t, value);
543 value = (int)((float)engine->value / 100 * 0xffff);
544 DOWNSAMPLE(uint16_t, int64_t, value);
551 value = (float)engine->value / 100;
552 DOWNSAMPLE(float, double, value);
562 if(engine->step == DO_X_FEATHER)
565 if(engine->recalculate)
568 if(engine->feather > 0) do_feather(engine->mask,
576 //printf("MaskUnit::process_package 3 %f\n", engine->feather);
579 if(engine->step == DO_Y_FEATHER)
581 if(engine->recalculate)
584 if(engine->feather > 0) do_feather(engine->mask,
594 if(engine->step == DO_APPLY)
597 int mask_w = engine->mask->get_w();
600 #define APPLY_MASK_SUBTRACT_ALPHA(type, max, components, do_yuv) \
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; \
606 for(int j = 0; j < mask_w; j++) \
608 if(components == 4) \
610 output_row[j * 4 + 3] = output_row[j * 4 + 3] * (max - mask_row[j]) / max; \
614 output_row[j * 3] = output_row[j * 3] * (max - mask_row[j]) / max; \
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; \
621 output_row[j * 3 + 1] += chroma_offset * mask_row[j] / max; \
622 output_row[j * 3 + 2] += chroma_offset * mask_row[j] / max; \
628 #define APPLY_MASK_MULTIPLY_ALPHA(type, max, components, do_yuv) \
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; \
634 for(int j = 0; j < mask_w; j++) \
636 if(components == 4) \
638 output_row[j * 4 + 3] = output_row[j * 4 + 3] * mask_row[j] / max; \
642 output_row[j * 3] = output_row[j * 3] * mask_row[j] / max; \
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; \
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; \
659 //printf("MaskUnit::process_package 1 %d\n", engine->mode);
660 for(int i = ptr->start_y; i < ptr->end_y; i++)
664 case MASK_MULTIPLY_ALPHA:
665 switch(engine->output->get_color_model())
668 APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 3, 0);
671 APPLY_MASK_MULTIPLY_ALPHA(float, 1.0, 3, 0);
674 APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 3, 1);
677 APPLY_MASK_MULTIPLY_ALPHA(float, 1.0, 4, 0);
680 APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 4, 1);
683 APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 4, 0);
686 APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 3, 0);
689 APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 3, 1);
691 case BC_YUVA16161616:
692 APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 4, 1);
694 case BC_RGBA16161616:
695 APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 4, 0);
700 case MASK_SUBTRACT_ALPHA:
701 switch(engine->output->get_color_model())
704 APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 3, 0);
707 APPLY_MASK_SUBTRACT_ALPHA(float, 1.0, 3, 0);
710 APPLY_MASK_SUBTRACT_ALPHA(float, 1.0, 4, 0);
713 APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 4, 0);
716 APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 3, 1);
719 APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 4, 1);
722 APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 3, 0);
724 case BC_RGBA16161616:
725 APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 4, 0);
728 APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 3, 1);
730 case BC_YUVA16161616:
731 APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 4, 1);
744 MaskEngine::MaskEngine(int cpus)
745 : LoadServer(cpus, cpus * OVERSAMPLE * 2)
746 // : LoadServer(1, OVERSAMPLE * 2)
751 MaskEngine::~MaskEngine()
759 for(int i = 0; i < point_sets.total; i++)
761 ArrayList<MaskPoint*> *points = point_sets.values[i];
762 points->remove_all_objects();
764 point_sets.remove_all_objects();
767 int MaskEngine::points_equivalent(ArrayList<MaskPoint*> *new_points,
768 ArrayList<MaskPoint*> *points)
770 //printf("MaskEngine::points_equivalent %d %d\n", new_points->total, points->total);
771 if(new_points->total != points->total) return 0;
773 for(int i = 0; i < new_points->total; i++)
775 if(!(*new_points->values[i] == *points->values[i])) return 0;
781 void MaskEngine::do_mask(VFrame *output,
782 int64_t start_position_project,
783 MaskAutos *keyframe_set,
785 MaskAuto *default_auto)
787 int new_color_model = 0;
790 switch(output->get_color_model())
794 new_color_model = BC_A_FLOAT;
801 new_color_model = BC_A8;
805 case BC_RGBA16161616:
807 case BC_YUVA16161616:
808 new_color_model = BC_A16;
812 // Determine if recalculation is needed
816 (mask->get_w() != output->get_w() ||
817 mask->get_h() != output->get_h() ||
818 mask->get_color_model() != new_color_model))
828 if(point_sets.total != keyframe_set->total_submasks(start_position_project,
836 i < keyframe_set->total_submasks(start_position_project,
837 PLAY_FORWARD) && !recalculate;
840 ArrayList<MaskPoint*> *new_points = new ArrayList<MaskPoint*>;
841 keyframe_set->get_points(new_points,
843 start_position_project,
845 if(!points_equivalent(new_points, point_sets.values[i])) recalculate = 1;
846 new_points->remove_all_objects();
851 int new_value = keyframe_set->get_value(start_position_project,
853 float new_feather = keyframe_set->get_feather(start_position_project,
857 !EQUIV(new_feather, feather) ||
858 !EQUIV(new_value, value))
863 mask = new VFrame(output->get_w(), output->get_h(),
865 temp_mask = new VFrame(output->get_w(), output->get_h(),
869 temp_mask->clear_frame();
873 for(int i = 0; i < point_sets.total; i++)
875 ArrayList<MaskPoint*> *points = point_sets.values[i];
876 points->remove_all_objects();
878 point_sets.remove_all_objects();
881 i < keyframe_set->total_submasks(start_position_project,
885 ArrayList<MaskPoint*> *new_points = new ArrayList<MaskPoint*>;
886 keyframe_set->get_points(new_points,
888 start_position_project,
890 point_sets.append(new_points);
896 this->output = output;
897 this->mode = default_auto->mode;
898 this->feather = new_feather;
899 this->value = new_value;
917 void MaskEngine::init_packages()
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();
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;
932 //printf("MaskEngine::init_packages 2\n");
935 LoadClient* MaskEngine::new_client()
937 return new MaskUnit(this);
940 LoadPackage* MaskEngine::new_package()
942 return new MaskPackage;