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))
378 //printf("MaskUnit::process_package 1\n");
385 oversampled_package_w,
386 oversampled_package_h,
393 //printf("MaskUnit::process_package 1 %d\n", engine->point_sets.total);
397 // Draw oversampled region of polygons on temp
398 for(int k = 0; k < engine->point_sets.total; k++)
401 unsigned char max = k + 1;
402 ArrayList<MaskPoint*> *points = engine->point_sets.values[k];
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++)
408 MaskPoint *point1 = points->values[i];
409 MaskPoint *point2 = (i >= points->total - 1) ?
411 points->values[i + 1];
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)
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;
429 for(int j = 0; j <= segments; j++)
431 float t = (float)j / segments;
433 float tpow3 = t * t * t;
435 float invtpow2 = invt * invt;
436 float invtpow3 = invt * invt * invt;
439 + 3 * t * invtpow2 * x1
440 + 3 * tpow2 * invt * x2
443 + 3 * t * invtpow2 * y1
444 + 3 * tpow2 * invt * y2
453 draw_line_clamped(temp, old_x, old_y, (int)x, (int)y, max);
462 //printf("MaskUnit::process_package 1\n");
468 // Fill in the polygon in the horizontal direction
469 for(int i = 0; i < oversampled_package_h; i++)
471 unsigned char *row = (unsigned char*)temp->get_rows()[i];
475 for(int j = 0; j < oversampled_package_w; j++)
476 if(row[j] == max) total++;
480 if(total & 0x1) total--;
481 for(int j = 0; j < oversampled_package_w; j++)
483 if(row[j] == max && total > 0)
493 if(value) row[j] = value;
507 #define DOWNSAMPLE(type, temp_type, value) \
508 for(int i = 0; i < ptr->end_y - ptr->start_y; i++) \
510 type *output_row = (type*)mask->get_rows()[i + ptr->start_y]; \
511 unsigned char **input_rows = (unsigned char**)temp->get_rows() + i * OVERSAMPLE; \
514 for(int j = 0; j < mask_w; j++) \
516 temp_type total = 0; \
518 /* Accumulate pixel */ \
519 for(int k = 0; k < OVERSAMPLE; k++) \
521 unsigned char *input_vector = input_rows[k] + j * OVERSAMPLE; \
522 for(int l = 0; l < OVERSAMPLE; l++) \
524 total += (input_vector[l] ? value : 0); \
529 total /= OVERSAMPLE * OVERSAMPLE; \
531 output_row[j] = total; \
537 // Downsample polygon
538 switch(mask->get_color_model())
543 value = (int)((float)engine->value / 100 * 0xff);
544 DOWNSAMPLE(unsigned char, int64_t, value);
551 value = (int)((float)engine->value / 100 * 0xffff);
552 DOWNSAMPLE(uint16_t, int64_t, value);
559 value = (float)engine->value / 100;
560 DOWNSAMPLE(float, double, value);
570 if(engine->step == DO_X_FEATHER)
573 if(engine->recalculate)
576 if(engine->feather > 0) do_feather(engine->mask,
584 //printf("MaskUnit::process_package 3 %f\n", engine->feather);
587 if(engine->step == DO_Y_FEATHER)
589 if(engine->recalculate)
592 if(engine->feather > 0) do_feather(engine->mask,
602 if(engine->step == DO_APPLY)
605 int mask_w = engine->mask->get_w();
608 #define APPLY_MASK_SUBTRACT_ALPHA(type, max, components, do_yuv) \
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; \
614 for(int j = 0; j < mask_w; j++) \
616 if(components == 4) \
618 output_row[j * 4 + 3] = output_row[j * 4 + 3] * (max - mask_row[j]) / max; \
622 output_row[j * 3] = output_row[j * 3] * (max - mask_row[j]) / max; \
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; \
629 output_row[j * 3 + 1] += chroma_offset * mask_row[j] / max; \
630 output_row[j * 3 + 2] += chroma_offset * mask_row[j] / max; \
636 #define APPLY_MASK_MULTIPLY_ALPHA(type, max, components, do_yuv) \
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; \
642 for(int j = 0; j < mask_w; j++) \
644 if(components == 4) \
646 output_row[j * 4 + 3] = output_row[j * 4 + 3] * mask_row[j] / max; \
650 output_row[j * 3] = output_row[j * 3] * mask_row[j] / max; \
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; \
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; \
667 //printf("MaskUnit::process_package 1 %d\n", engine->mode);
668 for(int i = ptr->start_y; i < ptr->end_y; i++)
672 case MASK_MULTIPLY_ALPHA:
673 switch(engine->output->get_color_model())
676 APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 3, 0);
679 APPLY_MASK_MULTIPLY_ALPHA(float, 1.0, 3, 0);
682 APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 3, 1);
685 APPLY_MASK_MULTIPLY_ALPHA(float, 1.0, 4, 0);
688 APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 4, 1);
691 APPLY_MASK_MULTIPLY_ALPHA(unsigned char, 0xff, 4, 0);
694 APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 3, 0);
697 APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 3, 1);
699 case BC_YUVA16161616:
700 APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 4, 1);
702 case BC_RGBA16161616:
703 APPLY_MASK_MULTIPLY_ALPHA(uint16_t, 0xffff, 4, 0);
708 case MASK_SUBTRACT_ALPHA:
709 switch(engine->output->get_color_model())
712 APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 3, 0);
715 APPLY_MASK_SUBTRACT_ALPHA(float, 1.0, 3, 0);
718 APPLY_MASK_SUBTRACT_ALPHA(float, 1.0, 4, 0);
721 APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 4, 0);
724 APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 3, 1);
727 APPLY_MASK_SUBTRACT_ALPHA(unsigned char, 0xff, 4, 1);
730 APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 3, 0);
732 case BC_RGBA16161616:
733 APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 4, 0);
736 APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 3, 1);
738 case BC_YUVA16161616:
739 APPLY_MASK_SUBTRACT_ALPHA(uint16_t, 0xffff, 4, 1);
752 MaskEngine::MaskEngine(int cpus)
753 : LoadServer(cpus, cpus * OVERSAMPLE * 2)
754 // : LoadServer(1, OVERSAMPLE * 2)
759 MaskEngine::~MaskEngine()
767 for(int i = 0; i < point_sets.total; i++)
769 ArrayList<MaskPoint*> *points = point_sets.values[i];
770 points->remove_all_objects();
772 point_sets.remove_all_objects();
775 int MaskEngine::points_equivalent(ArrayList<MaskPoint*> *new_points,
776 ArrayList<MaskPoint*> *points)
778 //printf("MaskEngine::points_equivalent %d %d\n", new_points->total, points->total);
779 if(new_points->total != points->total) return 0;
781 for(int i = 0; i < new_points->total; i++)
783 if(!(*new_points->values[i] == *points->values[i])) return 0;
789 void MaskEngine::do_mask(VFrame *output,
790 int64_t start_position_project,
791 MaskAutos *keyframe_set,
793 MaskAuto *default_auto)
795 int new_color_model = 0;
798 switch(output->get_color_model())
802 new_color_model = BC_A_FLOAT;
809 new_color_model = BC_A8;
813 case BC_RGBA16161616:
815 case BC_YUVA16161616:
816 new_color_model = BC_A16;
820 // Determine if recalculation is needed
824 (mask->get_w() != output->get_w() ||
825 mask->get_h() != output->get_h() ||
826 mask->get_color_model() != new_color_model))
836 if(point_sets.total != keyframe_set->total_submasks(start_position_project,
844 i < keyframe_set->total_submasks(start_position_project,
845 PLAY_FORWARD) && !recalculate;
848 ArrayList<MaskPoint*> *new_points = new ArrayList<MaskPoint*>;
849 keyframe_set->get_points(new_points,
851 start_position_project,
853 if(!points_equivalent(new_points, point_sets.values[i])) recalculate = 1;
854 new_points->remove_all_objects();
859 int new_value = keyframe_set->get_value(start_position_project,
861 float new_feather = keyframe_set->get_feather(start_position_project,
865 !EQUIV(new_feather, feather) ||
866 !EQUIV(new_value, value))
877 temp_mask = new VFrame(0,
885 temp_mask->clear_frame();
889 for(int i = 0; i < point_sets.total; i++)
891 ArrayList<MaskPoint*> *points = point_sets.values[i];
892 points->remove_all_objects();
894 point_sets.remove_all_objects();
897 i < keyframe_set->total_submasks(start_position_project,
901 ArrayList<MaskPoint*> *new_points = new ArrayList<MaskPoint*>;
902 keyframe_set->get_points(new_points,
904 start_position_project,
906 point_sets.append(new_points);
912 this->output = output;
913 this->mode = default_auto->mode;
914 this->feather = new_feather;
915 this->value = new_value;
933 void MaskEngine::init_packages()
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;
941 for(int i = 0; i < get_total_packages(); i++)
943 MaskPackage *ptr = (MaskPackage*)get_package(i);
945 ptr->start_y = output->get_h() * i / get_total_packages();
946 ptr->end_y = output->get_h() * (i + 1) / get_total_packages();
948 ptr->start_x = output->get_w() * i / get_total_packages();
949 ptr->end_x = output->get_w() * (i + 1) / get_total_packages();
952 //printf("MaskEngine::init_packages 2\n");
955 LoadClient* MaskEngine::new_client()
957 return new MaskUnit(this);
960 LoadPackage* MaskEngine::new_package()
962 return new MaskPackage;