3 * Copyright (C) 1997-2015 Adam Williams <broadcast at earthling dot net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "arraylist.h"
27 #include "bccmodels.h"
30 #include "edlsession.h"
33 #include "crikeywindow.h"
37 // chroma interpolated key, crikey
39 REGISTER_PLUGIN(CriKey)
42 void crikey_pgm(const char *fn,VFrame *vfrm)
44 FILE *fp = fopen(fn,"w");
45 int w = vfrm->get_w(), h = vfrm->get_h();
46 fprintf(fp,"P5\n%d %d\n255\n",w,h);
47 fwrite(vfrm->get_data(),w,h,fp);
52 CriKeyPoint::CriKeyPoint(int tag, int e, float x, float y, float t)
54 this->tag = tag; this->e = e;
55 this->x = x; this->y = y;
58 CriKeyPoint::~CriKeyPoint()
62 CriKeyConfig::CriKeyConfig()
65 draw_mode = DRAW_ALPHA;
69 CriKeyConfig::~CriKeyConfig()
73 int CriKeyConfig::equivalent(CriKeyConfig &that)
75 if( !EQUIV(this->threshold, that.threshold) ) return 0;
76 if( this->draw_mode != that.draw_mode ) return 0;
77 if( this->drag != that.drag ) return 0;
78 if( this->points.size() != that.points.size() ) return 0;
79 for( int i=0, n=points.size(); i<n; ++i ) {
80 CriKeyPoint *ap = this->points[i], *bp = that.points[i];
81 if( ap->tag != bp->tag ) return 0;
82 if( ap->e != bp->e ) return 0;
83 if( !EQUIV(ap->x, bp->x) ) return 0;
84 if( !EQUIV(ap->y, bp->y) ) return 0;
85 if( !EQUIV(ap->t, bp->t) ) return 0;
90 void CriKeyConfig::copy_from(CriKeyConfig &that)
92 this->threshold = that.threshold;
93 this->draw_mode = that.draw_mode;
94 this->drag = that.drag;
95 this->selected = that.selected;
97 points.remove_all_objects();
98 for( int i=0,n=that.points.size(); i<n; ++i ) {
99 CriKeyPoint *pt = that.points[i];
100 add_point(pt->tag, pt->e, pt->x, pt->y, pt->t);
104 void CriKeyConfig::interpolate(CriKeyConfig &prev, CriKeyConfig &next,
105 long prev_frame, long next_frame, long current_frame)
107 this->threshold = prev.threshold;
108 this->draw_mode = prev.draw_mode;
109 this->drag = prev.drag;
110 this->selected = prev.selected;
112 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
113 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
115 points.remove_all_objects();
116 int prev_sz = prev.points.size(), next_sz = next.points.size();
117 for( int i=0; i<prev_sz; ++i ) {
118 CriKeyPoint *pt = prev.points[i], *nt = 0;
119 float x = pt->x, y = pt->y, t = pt->t;
120 int k = next_sz; // associated by tag id in next
121 while( --k >= 0 && pt->tag != (nt=next.points[k])->tag );
123 x = x * prev_scale + nt->x * next_scale;
124 y = y * prev_scale + nt->y * next_scale;
125 t = t * prev_scale + nt->t * next_scale;
127 add_point(pt->tag, pt->e, x, y, t);
131 void CriKeyConfig::limits()
133 bclamp(threshold, 0.0f, 1.0f);
134 bclamp(draw_mode, 0, DRAW_MODES-1);
137 int CriKeyConfig::add_point(int tag, int e, float x, float y, float t)
139 int k = points.size();
142 for( int i=k; --i>=0; ) {
143 int n = points[i]->tag;
144 if( n >= tag ) tag = n + 1;
147 points.append(new CriKeyPoint(tag, e, x, y, t));
151 void CriKeyConfig::del_point(int i)
153 points.remove_object_number(i);
159 class segment { public: int y, lt, rt; };
160 ArrayList<segment> stack;
162 void push(int y, int lt, int rt) {
163 segment &seg = stack.append();
164 seg.y = y; seg.lt = lt; seg.rt = rt;
166 void pop(int &y, int <, int &rt) {
167 segment &seg = stack.last();
168 y = seg.y; lt = seg.lt; rt = seg.rt;
175 bool edge_pixel(int i) { return edg[i] > 0; }
178 void fill(int x, int y);
181 FillRegion(VFrame *edg, VFrame *msk);
184 FillRegion::FillRegion(VFrame *edg, VFrame *msk)
186 this->w = msk->get_w();
187 this->h = msk->get_h();
188 this->msk = (uint8_t*) msk->get_data();
189 this->edg = (float*) edg->get_data();
192 void FillRegion::fill(int x, int y)
197 void FillRegion::run()
199 while( stack.size() > 0 ) {
203 for( int x=ilt; x<=irt; ++x,++ofs ) {
204 if( !msk[ofs] ) continue;
206 if( edge_pixel(ofs) ) continue;
209 for( int i=lt; --i>=0; ) {
210 if( !msk[--lofs] ) break;
211 msk[lofs] = 0; lt = i;
212 if( edge_pixel(lofs) ) break;
215 for( int i=rt; ++i< w; ) {
216 if( !msk[++rofs] ) break;
217 msk[rofs] = 0; rt = i;
218 if( edge_pixel(rofs) ) break;
220 if( y+1 < h ) push(y+1, lt, rt);
221 if( y-1 >= 0 ) push(y-1, lt, rt);
227 CriKey::CriKey(PluginServer *server)
228 : PluginVClient(server)
242 int CriKey::set_target(float *color, int x, int y)
244 if( x < 0 || x >= w ) return 1;
245 if( y < 0 || y >= h ) return 1;
246 uint8_t **src_rows = src->get_rows();
248 float *fp = (float*)(src_rows[y] + x*bpp);
249 for( int i=0; i<comp; ++i,++fp ) color[i] = *fp;
252 uint8_t *sp = src_rows[y] + x*bpp;
253 for( int i=0; i<comp; ++i,++sp ) color[i] = *sp;
258 const char* CriKey::plugin_title() { return N_("CriKey"); }
259 int CriKey::is_realtime() { return 1; }
261 NEW_WINDOW_MACRO(CriKey, CriKeyWindow);
262 LOAD_CONFIGURATION_MACRO(CriKey, CriKeyConfig)
264 int CriKey::new_point()
266 EDLSession *session = get_edlsession();
267 float x = !session ? 0.f : session->output_w / 2.f;
268 float y = !session ? 0.f : session->output_h / 2.f;
269 return config.add_point(-1, 0, x, y, 0.5f);
272 void CriKey::save_data(KeyFrame *keyframe)
276 // cause data to be stored directly in text
277 output.set_shared_output(keyframe->xbuf);
279 output.tag.set_title("CRIKEY");
280 output.tag.set_property("THRESHOLD", config.threshold);
281 output.tag.set_property("DRAW_MODE", config.draw_mode);
282 output.tag.set_property("DRAG", config.drag);
283 output.tag.set_property("SELECTED", config.selected);
285 output.append_newline();
286 output.tag.set_title("/CRIKEY");
288 output.append_newline();
289 for( int i=0, n = config.points.size(); i<n; ++i ) {
290 CriKeyPoint *pt = config.points[i];
291 char point[BCSTRLEN];
292 sprintf(point,"/POINT_%d",pt->tag);
293 output.tag.set_title(point+1);
294 output.tag.set_property("E", pt->e);
295 output.tag.set_property("X", pt->x);
296 output.tag.set_property("Y", pt->y);
297 output.tag.set_property("T", pt->t);
299 output.tag.set_title(point+0);
301 output.append_newline();
303 output.terminate_string();
306 void CriKey::read_data(KeyFrame *keyframe)
309 input.set_shared_input(keyframe->xbuf);
310 config.points.remove_all_objects();
313 while( !(result=input.read_tag()) ) {
314 if( input.tag.title_is("CRIKEY") ) {
315 config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
316 config.draw_mode = input.tag.get_property("DRAW_MODE", config.draw_mode);
317 config.drag = input.tag.get_property("DRAG", config.drag);
318 config.selected = input.tag.get_property("SELECTED", 0);
321 else if( !strncmp(input.tag.get_title(),"POINT_",6) ) {
322 int tag = atoi(input.tag.get_title() + 6);
323 int e = input.tag.get_property("E", 0);
324 float x = input.tag.get_property("X", 0.f);
325 float y = input.tag.get_property("Y", 0.f);
326 float t = input.tag.get_property("T", .5f);
327 config.add_point(tag, e, x, y, t);
331 if( !config.points.size() ) new_point();
334 void CriKey::update_gui()
336 if( !thread ) return;
337 thread->window->lock_window("CriKey::update_gui");
338 CriKeyWindow *window = (CriKeyWindow*)thread->window;
339 if( load_configuration() ) {
340 window->update_gui();
343 thread->window->unlock_window();
346 void CriKey::draw_alpha(VFrame *msk)
348 uint8_t **src_rows = src->get_rows();
349 uint8_t **msk_rows = msk->get_rows();
350 switch( color_model ) {
352 for( int y=0; y<h; ++y ) {
353 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
354 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
356 float *px = (float *)sp;
357 px[0] = px[1] = px[2] = 0;
362 for( int y=0; y<h; ++y ) {
363 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
364 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
366 float *px = (float *)sp;
372 for( int y=0; y<h; ++y ) {
373 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
374 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
377 px[0] = px[1] = px[2] = 0;
382 for( int y=0; y<h; ++y ) {
383 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
384 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
388 px[1] = px[2] = 0x80;
394 for( int y=0; y<h; ++y ) {
395 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
396 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
406 void CriKey::draw_edge(VFrame *edg)
408 uint8_t **src_rows = src->get_rows();
409 float **edg_rows = (float**) edg->get_rows(), gain = 10;
410 switch( color_model ) {
412 for( int y=0; y<h; ++y ) {
413 uint8_t *sp = src_rows[y];
414 float *ap = edg_rows[y];
415 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
416 float *px = (float *)sp, v = *ap * gain;
417 px[0] = px[1] = px[2] = v<1 ? v : 1;
422 for( int y=0; y<h; ++y ) {
423 uint8_t *sp = src_rows[y];
424 float *ap = edg_rows[y];
425 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
426 float *px = (float *)sp, v = *ap * gain;
427 px[0] = px[1] = px[2] = v<1 ? v : 1;
433 for( int y=0; y<h; ++y ) {
434 uint8_t *sp = src_rows[y];
435 float *ap = edg_rows[y];
436 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
437 uint8_t *px = sp; float v = *ap * gain;
438 px[0] = px[1] = px[2] = v<1 ? v*256 : 255;
443 for( int y=0; y<h; ++y ) {
444 uint8_t *sp = src_rows[y];
445 float *ap = edg_rows[y];
446 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
447 uint8_t *px = sp; float v = *ap * gain;
448 px[0] = px[1] = px[2] = v<1 ? v*256 : 255;
454 for( int y=0; y<h; ++y ) {
455 uint8_t *sp = src_rows[y];
456 float *ap = edg_rows[y];
457 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
458 uint8_t *px = sp; float v = *ap * gain;
459 px[0] = *ap<1 ? v*256 : 255;
460 px[1] = px[2] = 0x80;
465 for( int y=0; y<h; ++y ) {
466 uint8_t *sp = src_rows[y];
467 float *ap = edg_rows[y];
468 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
469 uint8_t *px = sp; float v = *ap * gain;
470 px[0] = *ap<1 ? v*256 : 255;
471 px[1] = px[2] = 0x80;
479 void CriKey::draw_mask(VFrame *msk)
481 uint8_t **src_rows = src->get_rows();
482 uint8_t **msk_rows = msk->get_rows();
483 float scale = 1 / 255.0f;
484 switch( color_model ) {
486 for( int y=0; y<h; ++y ) {
487 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
488 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
489 float a = *mp * scale;
490 float *px = (float *)sp;
491 px[0] = px[1] = px[2] = a;
496 for( int y=0; y<h; ++y ) {
497 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
498 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
499 float a = *mp * scale;
500 float *px = (float *)sp;
501 px[0] = px[1] = px[2] = a;
507 for( int y=0; y<h; ++y ) {
508 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
509 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
510 float a = *mp * scale;
512 px[0] = px[1] = px[2] = a * 255;
517 for( int y=0; y<h; ++y ) {
518 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
519 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
520 float a = *mp * scale;
522 px[0] = px[1] = px[2] = a * 255;
528 for( int y=0; y<h; ++y ) {
529 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
530 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
531 float a = *mp * scale;
534 px[1] = px[2] = 0x80;
539 for( int y=0; y<h; ++y ) {
540 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
541 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
542 float a = *mp * scale;
545 px[1] = px[2] = 0x80;
554 void CriKey::draw_point(VFrame *src, CriKeyPoint *pt)
556 int d = bmax(w,h) / 200 + 2;
557 int r = d/2+1, x = pt->x, y = pt->y;
558 src->draw_smooth(x-r,y+0, x-r, y-r, x+0,y-r);
559 src->draw_smooth(x+0,y-r, x+r, y-r, x+r,y+0);
560 src->draw_smooth(x+r,y+0, x+r, y+r, x+0,y+r);
561 src->draw_smooth(x+0,y+r, x-r, y+r, x-r,y+0);
563 src->set_pixel_color(RED);
564 src->draw_x(pt->x, pt->y, d);
567 src->set_pixel_color(BLUE);
568 src->draw_t(pt->x, pt->y, d);
573 static void fill_edge(VFrame *vfrm, int w, int h)
575 int w1 = w-1, h1 = h-1;
576 float *dp = (float*) vfrm->get_data();
577 if( w1 > 0 ) for( int y=0; y<h1; ++y,dp+=w ) dp[w1] = dp[w1-1];
578 if( h1 > 0 ) for( int x=0; x<w; ++x ) dp[x] = dp[x-w];
581 int CriKey::process_buffer(VFrame *frame, int64_t start_position, double frame_rate)
583 load_configuration();
585 w = src->get_w(), h = src->get_h();
586 color_model = src->get_color_model();
587 bpp = BC_CModels::calculate_pixelsize(color_model);
588 is_float = BC_CModels::is_float(color_model);
589 is_yuv = BC_CModels::is_yuv(color_model);
590 comp = BC_CModels::components(color_model);
591 if( comp > 3 ) comp = 3;
593 read_frame(src, 0, start_position, frame_rate, 0);
594 VFrame::get_temp(edg, w, h, BC_A_FLOAT);
597 engine = new CriKeyEngine(this,
598 PluginClient::get_project_smp() + 1,
599 PluginClient::get_project_smp() + 1);
601 VFrame::get_temp(msk, w, h, BC_A8);
602 memset(msk->get_data(), 0xff, msk->get_data_size());
604 for( int i=0, n=config.points.size(); i<n; ++i ) {
605 CriKeyPoint *pt = config.points[i];
606 if( !pt->e ) continue;
607 if( set_target(engine->color, pt->x, pt->y) ) continue;
608 engine->threshold = pt->t;
610 engine->process_packages();
611 fill_edge(edg, w, h);
612 FillRegion fill_region(edg, msk);
613 fill_region.fill(pt->x, pt->y);
617 //crikey_pgm("/tmp/msk.pgm",msk);
618 switch( config.draw_mode ) {
619 case DRAW_ALPHA: draw_alpha(msk); break;
620 case DRAW_EDGE: draw_edge(edg); break;
621 case DRAW_MASK: draw_mask(msk); break;
625 for( int i=0, n=config.points.size(); i<n; ++i ) {
626 CriKeyPoint *pt = config.points[i];
627 src->set_pixel_color(config.selected == i ? GREEN : WHITE);
635 void CriKeyEngine::init_packages()
637 int y = 0, h1 = plugin->h-1;
638 for(int i = 0; i < get_total_packages(); ) {
639 CriKeyPackage *pkg = (CriKeyPackage*)get_package(i++);
641 y = h1 * i / LoadServer::get_total_packages();
646 LoadPackage* CriKeyEngine::new_package()
648 return new CriKeyPackage();
651 LoadClient* CriKeyEngine::new_client()
653 return new CriKeyUnit(this);
656 #define EDGE_MACRO(type, components, is_yuv) \
658 uint8_t **src_rows = src->get_rows(); \
659 int comps = MIN(components, 3); \
660 for( int y=y1; y<y2; ++y ) { \
661 uint8_t *row0 = src_rows[y], *row1 = src_rows[y+1]; \
662 float *edgp = edg_rows[y]; \
663 for( int x=x1; x<x2; ++edgp,++x,row0+=bpp,row1+=bpp ) { \
664 type *r0 = (type*)row0, *r1 = (type*)row1; \
665 float a00 = 0, a01 = 0, a10 = 0, a11 = 0; \
666 for( int c=0; c<comps; ++c,++r0,++r1 ) { \
667 float t = target_color[c]; \
668 a00 += fabs(t - r0[0]); \
669 a01 += fabs(t - r0[components]); \
670 a10 += fabs(t - r1[0]); \
671 a11 += fabs(t - r1[components]); \
673 float mx = scale * bmax(bmax(a00, a01), bmax(a10, a11)); \
674 if( mx < threshold ) continue; \
675 float mn = scale * bmin(bmin(a00, a01), bmin(a10, a11)); \
676 if( mn >= threshold ) continue; \
677 *edgp += (mx - mn); \
683 void CriKeyUnit::process_package(LoadPackage *package)
685 int color_model = server->plugin->color_model;
686 int bpp = server->plugin->bpp;
687 VFrame *src = server->plugin->src;
688 VFrame *edg = server->plugin->edg;
689 float **edg_rows = (float**)edg->get_rows();
690 float *target_color = server->color;
691 float threshold = 2.f * server->threshold*server->threshold;
692 float scale = 1./BC_CModels::calculate_max(color_model);
693 CriKeyPackage *pkg = (CriKeyPackage*)package;
694 int x1 = 0, x2 = server->plugin->w-1;
695 int y1 = pkg->y1, y2 = pkg->y2;
697 switch( color_model ) {
698 case BC_RGB_FLOAT: EDGE_MACRO(float, 3, 0);
699 case BC_RGBA_FLOAT: EDGE_MACRO(float, 4, 0);
700 case BC_RGB888: EDGE_MACRO(unsigned char, 3, 0);
701 case BC_YUV888: EDGE_MACRO(unsigned char, 3, 1);
702 case BC_RGBA8888: EDGE_MACRO(unsigned char, 4, 0);
703 case BC_YUVA8888: EDGE_MACRO(unsigned char, 4, 1);