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"
30 #include "transportque.inc"
37 void write_mask(VFrame *vfrm, const char *fmt, ...)
39 va_list ap; va_start(ap, fmt);
40 char fn[256]; vsnprintf(fn, sizeof(fn), fmt, ap);
42 FILE *fp = !strcmp(fn,"-") ? stdout : fopen(fn,"w");
44 int w = vfrm->get_w(), h = vfrm->get_h();
45 int m = vfrm->get_color_model();
46 fprintf(fp,"P5\n%d %d\n%d\n",w,h,m==BC_A8? 0xff : 0xffff);
47 int bpp = m==BC_A8? 1 : 2;
48 fwrite(vfrm->get_data(),bpp*w,h,fp); fflush(fp);
49 if( fp != stdout ) fclose(fp);
53 MaskPackage::MaskPackage()
57 MaskPackage::~MaskPackage()
61 MaskUnit::MaskUnit(MaskEngine *engine)
64 this->engine = engine;
73 void MaskUnit::draw_line(int v, int ix1, int iy1, int ix2, int iy2)
75 if( iy1 == iy2 ) return;
76 int x1 = iy1 < iy2 ? ix1 : ix2;
77 int y1 = iy1 < iy2 ? iy1 : iy2;
78 int x2 = iy1 < iy2 ? ix2 : ix1;
79 int y2 = iy1 < iy2 ? iy2 : iy1;
80 float slope = (float)(x2-x1) / (y2-y1);
81 int dy = y1 - start_y;
82 int i = dy < 0 ? (y1=start_y, -dy) : 0;
83 if( y2 > end_y ) y2 = end_y;
84 if( y2 < start_y || y1 >= end_y ) return;
86 VFrame *temp = engine->temp;
87 int w1 = temp->get_w()-1;
88 temp_t **rows = (temp_t **)temp->get_rows();
89 for( int y=y1; y<y2; ++i,++y ) {
90 int x = (int)(i*slope + x1);
92 rows[y][x] = rows[y][x] == v ? 0 : v;
96 void MaskUnit::draw_fill(int v)
98 VFrame *temp = engine->temp;
99 int temp_w = temp->get_w();
100 temp_t **rows = (temp_t**)temp->get_rows();
102 for( int y=start_y; y<end_y; ++y ) {
103 temp_t *row = rows[y];
104 int value = 0, total = 0;
105 for( int x=0; x<temp_w; ++x )
106 if( row[x] == v ) ++total;
107 if( total < 2 ) continue;
108 if( total & 0x1 ) --total;
109 for( int x=0; x<temp_w; ++x ) {
110 if( row[x]==v && total>0 ) {
112 value = value ? 0 : v;
120 void MaskUnit::draw_feather(int ix1,int iy1, int ix2,int iy2)
122 int x1 = iy1 < iy2 ? ix1 : ix2;
123 int y1 = iy1 < iy2 ? iy1 : iy2;
124 int x2 = iy1 < iy2 ? ix2 : ix1;
125 int y2 = iy1 < iy2 ? iy2 : iy1;
126 VFrame *temp = engine->temp;
127 int h = temp->get_h();
128 if( y2 < 0 || y1 >= h ) return;
131 int dx = x2-x1, dy = y2-y1;
132 int dx2 = 2*dx, dy2 = 2*dy;
133 if( dx < 0 ) dx = -dx;
134 int m = dx > dy ? dx : dy, n = m;
136 if( dx2 >= 0 ) do { /* +Y, +X */
138 if( (m -= dx2) < 0 ) { m += dy2; ++x; }
140 else do { /* +Y, -X */
142 if( (m += dx2) < 0 ) { m += dy2; --x; }
146 if( dx2 >= 0 ) do { /* +X, +Y */
148 if( (m -= dy2) < 0 ) { m += dx2; ++y; }
150 else do { /* -X, +Y */
152 if( (m -= dy2) < 0 ) { m -= dx2; ++y; }
157 void MaskUnit::draw_spot(int ix, int iy)
159 int rr = r * r, n = abs(r), rv = r * v;
160 if( iy < start_y-n || iy >= end_y+n ) return;
161 VFrame *temp = engine->temp;
162 int w1 = temp->get_w()-1, h1 = temp->get_h()-1;
163 int xs = ix - n; bclamp(xs, 0, w1);
164 int xn = ix + n; bclamp(xn, 0, w1);
165 int ys = iy - n; bclamp(ys, 0, h1);
166 int yn = iy + n; bclamp(yn, 0, h1);
168 temp_t **rows = (temp_t**)temp->get_rows();
169 for( int y=ys ; y<=yn; ++y ) {
170 temp_t *row = rows[y];
171 for( int x=xs; x<=xn; ++x ) {
172 int dx = x-ix, dy = y-iy;
173 int dd = dx*dx + dy*dy;
174 if( dd >= rr ) continue;
175 temp_t *rp = &row[x], a = spot[dd];
176 if( rv*(*rp-a) < 0 ) *rp = a;
181 void MaskUnit::process_package(LoadPackage *package)
183 MaskPackage *ptr = (MaskPackage*)package;
184 start_y = ptr->start_y;
186 if( start_y >= end_y ) return;
187 mask_model = engine->mask->get_color_model();
188 VFrame *temp = engine->temp;
189 if( engine->recalculate && engine->step == DO_MASK ) {
190 // Draw masked region of polygons on temp
191 for( int k=0; k<engine->edges.size(); ++k ) {
192 if( !engine->edges[k] ) continue;
193 MaskEdge &edge = *engine->edges[k];
194 if( edge.size() < 3 ) continue;
196 for( int i=0; i<edge.size(); ++i ) {
197 MaskCoord a = edge[i];
198 MaskCoord b = i<edge.size()-1 ? edge[i+1] : edge[0];
199 draw_line(v, a.x,a.y, b.x,b.y);
203 // map temp to fader alpha
204 int temp_w = temp->get_w();
205 temp_t **rows = (temp_t**)temp->get_rows();
206 temp_t *fade = engine->fade;
207 for( int y=start_y; y<end_y; ++y ) {
208 temp_t *tp = rows[y];
209 for( int i=temp_w; --i>=0; ++tp ) *tp = fade[*tp];
212 if( engine->recalculate && engine->step == DO_FEATHER ) {
214 for( int k=0; k<engine->edges.size(); ++k ) {
215 if( !(v = engine->faders[k]) ) continue;
216 if( !(r = engine->feathers[k]) ) continue;
217 MaskEdge &edge = *engine->edges[k];
218 if( !edge.size() ) continue;
219 float rv = r * v, vv = fabs(v);
220 int fg = 0xffff * (rv >= 0 ? vv : 0);
221 int bg = 0xffff * (rv >= 0 ? 0 : vv);
222 int rr = r*r; double dr = 1./rr;
223 temp_t psf[rr+1]; spot = psf;
224 for( int i=0; i<=rr; ++i ) {
226 psf[i] = (1-d)*fg + d*bg;
229 for( int i=0; i<n; ++i ) {
230 MaskCoord &a = edge[i];
231 MaskCoord &b = i<edge.size()-1 ? edge[i+1] : edge[0];
232 draw_feather(a.x,a.y, b.x,b.y);
236 #define REMAP(cmodel, type, expr) case cmodel: { \
237 type **msk_rows = (type**)engine->mask->get_rows(); \
238 for( int y=start_y; y<end_y; ++y ) { \
239 temp_t *rp = rows[y]; \
240 type *mp = msk_rows[y]; \
241 for( int i=temp_w; --i>=0; ++rp,++mp ) *mp = expr; \
244 const float to_flt = 1/65535.;
245 int temp_w = temp->get_w();
246 temp_t **rows = (temp_t**)temp->get_rows();
247 switch( mask_model ) {
248 REMAP(BC_A8, uint8_t, *rp >> 8);
249 REMAP(BC_A16, uint16_t, *rp);
250 REMAP(BC_A_FLOAT, float, *rp * to_flt);
255 if( engine->step == DO_APPLY ) {
256 int mask_w = engine->mask->get_w();
257 uint8_t **out_rows = engine->output->get_rows();
258 uint8_t **msk_rows = engine->mask->get_rows();
259 #define APPLY_MASK_ALPHA(cmodel, type, max, components, do_yuv) \
261 for( int y=ptr->start_y; y<ptr->end_y; ++y ) { \
262 type *out_row = (type*)out_rows[y]; \
263 type *msk_row = (type*)msk_rows[y]; \
264 type chroma_offset = (int)(max + 1) / 2; \
265 for( int x=0; x<mask_w; ++x ) { \
266 type a = msk_row[x], b = max-a; \
267 if( components == 4 ) { \
268 out_row[x*4 + 3] = out_row[x*4 + 3]*b / max; \
271 out_row[x*3 + 0] = out_row[x*3 + 0]*b / max; \
272 out_row[x*3 + 1] = out_row[x*3 + 1]*b / max; \
273 out_row[x*3 + 2] = out_row[x*3 + 2]*b / max; \
275 out_row[x*3 + 1] += chroma_offset*a / max; \
276 out_row[x*3 + 2] += chroma_offset*a / max; \
282 switch( engine->output->get_color_model() ) { \
283 APPLY_MASK_ALPHA(BC_RGB888, uint8_t, 0xff, 3, 0); \
284 APPLY_MASK_ALPHA(BC_RGB_FLOAT, float, 1.0, 3, 0); \
285 APPLY_MASK_ALPHA(BC_YUV888, uint8_t, 0xff, 3, 1); \
286 APPLY_MASK_ALPHA(BC_RGBA_FLOAT, float, 1.0, 4, 0); \
287 APPLY_MASK_ALPHA(BC_YUVA8888, uint8_t, 0xff, 4, 1); \
288 APPLY_MASK_ALPHA(BC_RGBA8888, uint8_t, 0xff, 4, 0); \
289 APPLY_MASK_ALPHA(BC_RGB161616, uint16_t, 0xffff, 3, 0); \
290 APPLY_MASK_ALPHA(BC_YUV161616, uint16_t, 0xffff, 3, 1); \
291 APPLY_MASK_ALPHA(BC_YUVA16161616, uint16_t, 0xffff, 4, 1); \
292 APPLY_MASK_ALPHA(BC_RGBA16161616, uint16_t, 0xffff, 4, 0); \
298 MaskEngine::MaskEngine(int cpus)
299 : LoadServer(cpus, 2*cpus)
300 // : LoadServer(1, 1)
306 MaskEngine::~MaskEngine()
310 for( int i = 0; i < point_sets.total; i++ )
311 point_sets[i]->remove_all_objects();
312 point_sets.remove_all_objects();
315 int MaskEngine::points_equivalent(MaskPoints *new_points,
318 //printf("MaskEngine::points_equivalent %d %d\n", new_points->total, points->total);
319 if( new_points->total != points->total ) return 0;
321 for( int i = 0; i < new_points->total; i++ ) {
322 if( !(*new_points->get(i) == *points->get(i)) ) return 0;
328 void MaskEngine::do_mask(VFrame *output,
329 int64_t start_position_project,
330 MaskAutos *keyframe_set,
332 MaskAuto *default_auto)
334 this->output = output;
338 switch( output->get_color_model() ) {
341 mask_model = BC_A_FLOAT;
352 case BC_RGBA16161616:
354 case BC_YUVA16161616:
359 // Determine if recalculation is needed
362 int mask_w = output->get_w(), mask_h = output->get_h();
363 if( mask && ( mask->get_color_model() != mask_model ||
364 mask->get_w() != mask_w || mask->get_h() != mask_h ) ) {
365 delete mask; mask = 0;
368 if( temp && ( temp->get_w() != mask_w || temp->get_h() != mask_h ) ) {
369 delete temp; temp = 0;
372 total_submasks = keyframe_set->total_submasks(start_position_project, PLAY_FORWARD);
373 if( total_submasks != point_sets.size() )
376 for( int i=0; i<total_submasks && !recalculate; ++i ) {
377 float new_fader = keyframe_set->get_fader(start_position_project, i, PLAY_FORWARD);
378 if( new_fader != faders[i] ) { recalculate = 1; break; }
379 float new_feather = keyframe_set->get_feather(start_position_project, i, PLAY_FORWARD);
380 if( new_feather != feathers[i] ) { recalculate = 1; break; }
381 MaskPoints new_points;
382 keyframe_set->get_points(&new_points, i,
383 start_position_project, PLAY_FORWARD);
384 if( !points_equivalent(&new_points, point_sets[i]) )
389 for( int i = 0; i < point_sets.total; i++ ) {
390 MaskPoints *points = point_sets[i];
391 points->remove_all_objects();
393 point_sets.remove_all_objects();
394 edges.remove_all_objects();
396 feathers.remove_all();
399 int show_mask = keyframe_set->track->masks;
400 for( int i=0; i<total_submasks; ++i ) {
401 float fader = keyframe_set->get_fader(start_position_project, i, PLAY_FORWARD);
402 float v = fader / 100;
404 temp_t t = fabs(v) * 0xffff;
406 if( fade[0] < t ) fade[0] = t;
410 float feather = keyframe_set->get_feather(start_position_project, i, PLAY_FORWARD);
411 feathers.append(feather);
412 MaskPoints *new_points = new MaskPoints();
413 keyframe_set->get_points(new_points, i, start_position_project, PLAY_FORWARD);
414 point_sets.append(new_points);
415 MaskEdge *edge = edges.append(new MaskEdge());
416 if( !((show_mask>>i) & 1) ) continue;
417 edge->load(*new_points, 0);
420 if( !mask ) mask = new VFrame(mask_w, mask_h, mask_model, 0);
421 if( !temp ) temp = new VFrame(mask_w, mask_h, BC_A16, 0);
436 void MaskEngine::init_packages()
439 //printf("MaskEngine::init_packages 1\n");
440 int x0 = 0, y0 = 0, i = 0, n = get_total_packages();
441 int out_w = output->get_w(), out_h = output->get_h();
444 MaskPackage *ptr = (MaskPackage*)get_package(i++);
445 int x1 = (out_w * i) / n, y1 = (out_h * i) / n;
446 ptr->start_x = x0; ptr->end_x = x1;
447 ptr->start_y = y0; ptr->end_y = y1;
451 //printf("MaskEngine::init_packages 2\n");
454 LoadClient* MaskEngine::new_client()
456 return new MaskUnit(this);
459 LoadPackage* MaskEngine::new_package()
461 return new MaskPackage;