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"
31 #include "edlsession.h"
34 #include "crikeywindow.h"
38 // chroma interpolated key, crikey
40 REGISTER_PLUGIN(CriKey)
43 void crikey_pgm(const char *fn,VFrame *vfrm)
45 FILE *fp = fopen(fn,"w");
46 int w = vfrm->get_w(), h = vfrm->get_h();
47 fprintf(fp,"P5\n%d %d\n255\n",w,h);
48 fwrite(vfrm->get_data(),w,h,fp);
53 CriKeyPoint::CriKeyPoint(int tag, int e, float x, float y, float t)
55 this->tag = tag; this->e = e;
56 this->x = x; this->y = y;
59 CriKeyPoint::~CriKeyPoint()
63 CriKeyConfig::CriKeyConfig()
66 draw_mode = DRAW_ALPHA;
70 CriKeyConfig::~CriKeyConfig()
74 int CriKeyConfig::equivalent(CriKeyConfig &that)
76 if( !EQUIV(this->threshold, that.threshold) ) return 0;
77 if( this->draw_mode != that.draw_mode ) return 0;
78 if( this->drag != that.drag ) return 0;
79 if( this->points.size() != that.points.size() ) return 0;
80 for( int i=0, n=points.size(); i<n; ++i ) {
81 CriKeyPoint *ap = this->points[i], *bp = that.points[i];
82 if( ap->tag != bp->tag ) return 0;
83 if( ap->e != bp->e ) return 0;
84 if( !EQUIV(ap->x, bp->x) ) return 0;
85 if( !EQUIV(ap->y, bp->y) ) return 0;
86 if( !EQUIV(ap->t, bp->t) ) return 0;
91 void CriKeyConfig::copy_from(CriKeyConfig &that)
93 this->threshold = that.threshold;
94 this->draw_mode = that.draw_mode;
95 this->drag = that.drag;
96 this->selected = that.selected;
98 points.remove_all_objects();
99 for( int i=0,n=that.points.size(); i<n; ++i ) {
100 CriKeyPoint *pt = that.points[i];
101 add_point(pt->tag, pt->e, pt->x, pt->y, pt->t);
105 void CriKeyConfig::interpolate(CriKeyConfig &prev, CriKeyConfig &next,
106 long prev_frame, long next_frame, long current_frame)
108 this->threshold = prev.threshold;
109 this->draw_mode = prev.draw_mode;
110 this->drag = prev.drag;
111 this->selected = prev.selected;
113 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
114 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
116 points.remove_all_objects();
117 int prev_sz = prev.points.size(), next_sz = next.points.size();
118 for( int i=0; i<prev_sz; ++i ) {
119 CriKeyPoint *pt = prev.points[i], *nt = 0;
120 float x = pt->x, y = pt->y, t = pt->t;
121 int k = next_sz; // associated by tag id in next
122 while( --k >= 0 && pt->tag != (nt=next.points[k])->tag );
124 x = x * prev_scale + nt->x * next_scale;
125 y = y * prev_scale + nt->y * next_scale;
126 t = t * prev_scale + nt->t * next_scale;
128 add_point(pt->tag, pt->e, x, y, t);
132 void CriKeyConfig::limits()
134 bclamp(threshold, 0.0f, 1.0f);
135 bclamp(draw_mode, 0, DRAW_MODES-1);
138 int CriKeyConfig::add_point(int tag, int e, float x, float y, float t)
140 int k = points.size();
143 for( int i=k; --i>=0; ) {
144 int n = points[i]->tag;
145 if( n >= tag ) tag = n + 1;
148 points.append(new CriKeyPoint(tag, e, x, y, t));
152 void CriKeyConfig::del_point(int i)
154 points.remove_object_number(i);
160 class segment { public: int y, lt, rt; };
161 ArrayList<segment> stack;
163 void push(int y, int lt, int rt) {
164 segment &seg = stack.append();
165 seg.y = y; seg.lt = lt; seg.rt = rt;
167 void pop(int &y, int <, int &rt) {
168 segment &seg = stack.last();
169 y = seg.y; lt = seg.lt; rt = seg.rt;
176 bool edge_pixel(int i) { return edg[i] > 0; }
179 void fill(int x, int y);
182 FillRegion(VFrame *edg, VFrame *msk);
185 FillRegion::FillRegion(VFrame *edg, VFrame *msk)
187 this->w = msk->get_w();
188 this->h = msk->get_h();
189 this->msk = (uint8_t*) msk->get_data();
190 this->edg = (float*) edg->get_data();
193 void FillRegion::fill(int x, int y)
198 void FillRegion::run()
200 while( stack.size() > 0 ) {
204 for( int x=ilt; x<=irt; ++x,++ofs ) {
205 if( !msk[ofs] ) continue;
207 if( edge_pixel(ofs) ) continue;
210 for( int i=lt; --i>=0; ) {
211 if( !msk[--lofs] ) break;
212 msk[lofs] = 0; lt = i;
213 if( edge_pixel(lofs) ) break;
216 for( int i=rt; ++i< w; ) {
217 if( !msk[++rofs] ) break;
218 msk[rofs] = 0; rt = i;
219 if( edge_pixel(rofs) ) break;
221 if( y+1 < h ) push(y+1, lt, rt);
222 if( y-1 >= 0 ) push(y-1, lt, rt);
228 CriKey::CriKey(PluginServer *server)
229 : PluginVClient(server)
243 int CriKey::set_target(float *color, int x, int y)
245 if( x < 0 || x >= w ) return 1;
246 if( y < 0 || y >= h ) return 1;
247 uint8_t **src_rows = src->get_rows();
249 float *fp = (float*)(src_rows[y] + x*bpp);
250 for( int i=0; i<comp; ++i,++fp ) color[i] = *fp;
253 uint8_t *sp = src_rows[y] + x*bpp;
254 for( int i=0; i<comp; ++i,++sp ) color[i] = *sp;
259 const char* CriKey::plugin_title() { return N_("CriKey"); }
260 int CriKey::is_realtime() { return 1; }
262 NEW_WINDOW_MACRO(CriKey, CriKeyWindow);
263 LOAD_CONFIGURATION_MACRO(CriKey, CriKeyConfig)
265 int CriKey::new_point()
267 EDLSession *session = get_edl()->session;
268 float x = !session ? 0.f : session->output_w / 2.f;
269 float y = !session ? 0.f : session->output_h / 2.f;
270 return config.add_point(-1, 0, x, y, 0.5f);
273 void CriKey::save_data(KeyFrame *keyframe)
277 // cause data to be stored directly in text
278 output.set_shared_output(keyframe->xbuf);
280 output.tag.set_title("CRIKEY");
281 output.tag.set_property("THRESHOLD", config.threshold);
282 output.tag.set_property("DRAW_MODE", config.draw_mode);
283 output.tag.set_property("DRAG", config.drag);
284 output.tag.set_property("SELECTED", config.selected);
286 output.append_newline();
287 output.tag.set_title("/CRIKEY");
289 output.append_newline();
290 for( int i=0, n = config.points.size(); i<n; ++i ) {
291 CriKeyPoint *pt = config.points[i];
292 char point[BCSTRLEN];
293 sprintf(point,"/POINT_%d",pt->tag);
294 output.tag.set_title(point+1);
295 output.tag.set_property("E", pt->e);
296 output.tag.set_property("X", pt->x);
297 output.tag.set_property("Y", pt->y);
298 output.tag.set_property("T", pt->t);
300 output.tag.set_title(point+0);
302 output.append_newline();
304 output.terminate_string();
307 void CriKey::read_data(KeyFrame *keyframe)
310 input.set_shared_input(keyframe->xbuf);
311 config.points.remove_all_objects();
314 while( !(result=input.read_tag()) ) {
315 if( input.tag.title_is("CRIKEY") ) {
316 config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
317 config.draw_mode = input.tag.get_property("DRAW_MODE", config.draw_mode);
318 config.drag = input.tag.get_property("DRAG", config.drag);
319 config.selected = input.tag.get_property("SELECTED", 0);
322 else if( !strncmp(input.tag.get_title(),"POINT_",6) ) {
323 int tag = atoi(input.tag.get_title() + 6);
324 int e = input.tag.get_property("E", 0);
325 float x = input.tag.get_property("X", 0.f);
326 float y = input.tag.get_property("Y", 0.f);
327 float t = input.tag.get_property("T", .5f);
328 config.add_point(tag, e, x, y, t);
332 if( !config.points.size() ) new_point();
335 void CriKey::update_gui()
337 if( !thread ) return;
338 thread->window->lock_window("CriKey::update_gui");
339 CriKeyWindow *window = (CriKeyWindow*)thread->window;
340 if( load_configuration() ) {
341 window->update_gui();
344 thread->window->unlock_window();
347 void CriKey::draw_alpha(VFrame *msk)
349 uint8_t **src_rows = src->get_rows();
350 uint8_t **msk_rows = msk->get_rows();
351 switch( color_model ) {
353 for( int y=0; y<h; ++y ) {
354 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
355 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
357 float *px = (float *)sp;
358 px[0] = px[1] = px[2] = 0;
363 for( int y=0; y<h; ++y ) {
364 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
365 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
367 float *px = (float *)sp;
373 for( int y=0; y<h; ++y ) {
374 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
375 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
378 px[0] = px[1] = px[2] = 0;
383 for( int y=0; y<h; ++y ) {
384 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
385 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
389 px[1] = px[2] = 0x80;
395 for( int y=0; y<h; ++y ) {
396 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
397 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
407 void CriKey::draw_edge(VFrame *edg)
409 uint8_t **src_rows = src->get_rows();
410 float **edg_rows = (float**) edg->get_rows(), gain = 10;
411 switch( color_model ) {
413 for( int y=0; y<h; ++y ) {
414 uint8_t *sp = src_rows[y];
415 float *ap = edg_rows[y];
416 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
417 float *px = (float *)sp, v = *ap * gain;
418 px[0] = px[1] = px[2] = v<1 ? v : 1;
423 for( int y=0; y<h; ++y ) {
424 uint8_t *sp = src_rows[y];
425 float *ap = edg_rows[y];
426 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
427 float *px = (float *)sp, v = *ap * gain;
428 px[0] = px[1] = px[2] = v<1 ? v : 1;
434 for( int y=0; y<h; ++y ) {
435 uint8_t *sp = src_rows[y];
436 float *ap = edg_rows[y];
437 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
438 uint8_t *px = sp; float v = *ap * gain;
439 px[0] = px[1] = px[2] = v<1 ? v*256 : 255;
444 for( int y=0; y<h; ++y ) {
445 uint8_t *sp = src_rows[y];
446 float *ap = edg_rows[y];
447 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
448 uint8_t *px = sp; float v = *ap * gain;
449 px[0] = px[1] = px[2] = v<1 ? v*256 : 255;
455 for( int y=0; y<h; ++y ) {
456 uint8_t *sp = src_rows[y];
457 float *ap = edg_rows[y];
458 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
459 uint8_t *px = sp; float v = *ap * gain;
460 px[0] = *ap<1 ? v*256 : 255;
461 px[1] = px[2] = 0x80;
466 for( int y=0; y<h; ++y ) {
467 uint8_t *sp = src_rows[y];
468 float *ap = edg_rows[y];
469 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
470 uint8_t *px = sp; float v = *ap * gain;
471 px[0] = *ap<1 ? v*256 : 255;
472 px[1] = px[2] = 0x80;
480 void CriKey::draw_mask(VFrame *msk)
482 uint8_t **src_rows = src->get_rows();
483 uint8_t **msk_rows = msk->get_rows();
484 float scale = 1 / 255.0f;
485 switch( color_model ) {
487 for( int y=0; y<h; ++y ) {
488 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
489 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
490 float a = *mp * scale;
491 float *px = (float *)sp;
492 px[0] = px[1] = px[2] = a;
497 for( int y=0; y<h; ++y ) {
498 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
499 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
500 float a = *mp * scale;
501 float *px = (float *)sp;
502 px[0] = px[1] = px[2] = a;
508 for( int y=0; y<h; ++y ) {
509 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
510 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
511 float a = *mp * scale;
513 px[0] = px[1] = px[2] = a * 255;
518 for( int y=0; y<h; ++y ) {
519 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
520 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
521 float a = *mp * scale;
523 px[0] = px[1] = px[2] = a * 255;
529 for( int y=0; y<h; ++y ) {
530 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
531 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
532 float a = *mp * scale;
535 px[1] = px[2] = 0x80;
540 for( int y=0; y<h; ++y ) {
541 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
542 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
543 float a = *mp * scale;
546 px[1] = px[2] = 0x80;
555 void CriKey::draw_point(VFrame *src, CriKeyPoint *pt)
557 int d = bmax(w,h) / 200 + 2;
558 int r = d/2+1, x = pt->x, y = pt->y;
559 src->draw_smooth(x-r,y+0, x-r, y-r, x+0,y-r);
560 src->draw_smooth(x+0,y-r, x+r, y-r, x+r,y+0);
561 src->draw_smooth(x+r,y+0, x+r, y+r, x+0,y+r);
562 src->draw_smooth(x+0,y+r, x-r, y+r, x-r,y+0);
564 src->set_pixel_color(RED);
565 src->draw_x(pt->x, pt->y, d);
568 src->set_pixel_color(BLUE);
569 src->draw_t(pt->x, pt->y, d);
574 static void fill_edge(VFrame *vfrm, int w, int h)
576 int w1 = w-1, h1 = h-1;
577 float *dp = (float*) vfrm->get_data();
578 if( w1 > 0 ) for( int y=0; y<h1; ++y,dp+=w ) dp[w1] = dp[w1-1];
579 if( h1 > 0 ) for( int x=0; x<w; ++x ) dp[x] = dp[x-w];
582 int CriKey::process_buffer(VFrame *frame, int64_t start_position, double frame_rate)
584 load_configuration();
586 w = src->get_w(), h = src->get_h();
587 color_model = src->get_color_model();
588 bpp = BC_CModels::calculate_pixelsize(color_model);
589 is_float = BC_CModels::is_float(color_model);
590 is_yuv = BC_CModels::is_yuv(color_model);
591 comp = BC_CModels::components(color_model);
592 if( comp > 3 ) comp = 3;
594 read_frame(src, 0, start_position, frame_rate, 0);
595 VFrame::get_temp(edg, w, h, BC_A_FLOAT);
598 engine = new CriKeyEngine(this,
599 PluginClient::get_project_smp() + 1,
600 PluginClient::get_project_smp() + 1);
602 VFrame::get_temp(msk, w, h, BC_A8);
603 memset(msk->get_data(), 0xff, msk->get_data_size());
605 for( int i=0, n=config.points.size(); i<n; ++i ) {
606 CriKeyPoint *pt = config.points[i];
607 if( !pt->e ) continue;
608 if( set_target(engine->color, pt->x, pt->y) ) continue;
609 engine->threshold = pt->t;
611 engine->process_packages();
612 fill_edge(edg, w, h);
613 FillRegion fill_region(edg, msk);
614 fill_region.fill(pt->x, pt->y);
618 //crikey_pgm("/tmp/msk.pgm",msk);
619 switch( config.draw_mode ) {
620 case DRAW_ALPHA: draw_alpha(msk); break;
621 case DRAW_EDGE: draw_edge(edg); break;
622 case DRAW_MASK: draw_mask(msk); break;
626 for( int i=0, n=config.points.size(); i<n; ++i ) {
627 CriKeyPoint *pt = config.points[i];
628 src->set_pixel_color(config.selected == i ? GREEN : WHITE);
636 void CriKeyEngine::init_packages()
638 int y = 0, h1 = plugin->h-1;
639 for(int i = 0; i < get_total_packages(); ) {
640 CriKeyPackage *pkg = (CriKeyPackage*)get_package(i++);
642 y = h1 * i / LoadServer::get_total_packages();
647 LoadPackage* CriKeyEngine::new_package()
649 return new CriKeyPackage();
652 LoadClient* CriKeyEngine::new_client()
654 return new CriKeyUnit(this);
657 #define EDGE_MACRO(type, components, is_yuv) \
659 uint8_t **src_rows = src->get_rows(); \
660 int comps = MIN(components, 3); \
661 for( int y=y1; y<y2; ++y ) { \
662 uint8_t *row0 = src_rows[y], *row1 = src_rows[y+1]; \
663 float *edgp = edg_rows[y]; \
664 for( int x=x1; x<x2; ++edgp,++x,row0+=bpp,row1+=bpp ) { \
665 type *r0 = (type*)row0, *r1 = (type*)row1; \
666 float a00 = 0, a01 = 0, a10 = 0, a11 = 0; \
667 for( int c=0; c<comps; ++c,++r0,++r1 ) { \
668 float t = target_color[c]; \
669 a00 += fabs(t - r0[0]); \
670 a01 += fabs(t - r0[components]); \
671 a10 += fabs(t - r1[0]); \
672 a11 += fabs(t - r1[components]); \
674 float mx = scale * bmax(bmax(a00, a01), bmax(a10, a11)); \
675 if( mx < threshold ) continue; \
676 float mn = scale * bmin(bmin(a00, a01), bmin(a10, a11)); \
677 if( mn >= threshold ) continue; \
678 *edgp += (mx - mn); \
684 void CriKeyUnit::process_package(LoadPackage *package)
686 int color_model = server->plugin->color_model;
687 int bpp = server->plugin->bpp;
688 VFrame *src = server->plugin->src;
689 VFrame *edg = server->plugin->edg;
690 float **edg_rows = (float**)edg->get_rows();
691 float *target_color = server->color;
692 float threshold = 2.f * server->threshold*server->threshold;
693 float scale = 1./BC_CModels::calculate_max(color_model);
694 CriKeyPackage *pkg = (CriKeyPackage*)package;
695 int x1 = 0, x2 = server->plugin->w-1;
696 int y1 = pkg->y1, y2 = pkg->y2;
698 switch( color_model ) {
699 case BC_RGB_FLOAT: EDGE_MACRO(float, 3, 0);
700 case BC_RGBA_FLOAT: EDGE_MACRO(float, 4, 0);
701 case BC_RGB888: EDGE_MACRO(unsigned char, 3, 0);
702 case BC_YUV888: EDGE_MACRO(unsigned char, 3, 1);
703 case BC_RGBA8888: EDGE_MACRO(unsigned char, 4, 0);
704 case BC_YUVA8888: EDGE_MACRO(unsigned char, 4, 1);