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"
36 #include "keyframes.h"
38 #include "transportque.inc"
41 // chroma interpolated key, crikey
43 REGISTER_PLUGIN(CriKey)
46 void crikey_pgm(const char *fn,VFrame *vfrm)
48 FILE *fp = fopen(fn,"w");
49 int w = vfrm->get_w(), h = vfrm->get_h();
50 fprintf(fp,"P5\n%d %d\n255\n",w,h);
51 fwrite(vfrm->get_data(),w,h,fp);
56 CriKeyPoint::CriKeyPoint(int tag, int e, float x, float y, float t)
58 this->tag = tag; this->e = e;
59 this->x = x; this->y = y;
62 CriKeyPoint::~CriKeyPoint()
66 CriKeyConfig::CriKeyConfig()
69 draw_mode = DRAW_ALPHA;
71 CriKeyConfig::~CriKeyConfig()
75 int CriKeyConfig::equivalent(CriKeyConfig &that)
77 if( !EQUIV(this->threshold, that.threshold) ) return 0;
78 if( this->draw_mode != that.draw_mode ) 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;
96 points.remove_all_objects();
97 for( int i=0,n=that.points.size(); i<n; ++i ) {
98 CriKeyPoint *pt = that.points[i];
99 add_point(pt->tag, pt->e, pt->x, pt->y, pt->t);
103 void CriKeyConfig::interpolate(CriKeyConfig &prev, CriKeyConfig &next,
104 long prev_frame, long next_frame, long current_frame)
106 this->threshold = prev.threshold;
107 this->draw_mode = prev.draw_mode;
109 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
110 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
112 points.remove_all_objects();
113 int prev_sz = prev.points.size(), next_sz = next.points.size();
114 for( int i=0; i<prev_sz; ++i ) {
115 CriKeyPoint *pt = prev.points[i], *nt = 0;
116 float x = pt->x, y = pt->y, t = pt->t;
117 int k = next_sz; // associated by tag id in next
118 while( --k >= 0 && pt->tag != (nt=next.points[k])->tag );
120 x = x * prev_scale + nt->x * next_scale;
121 y = y * prev_scale + nt->y * next_scale;
122 t = t * prev_scale + nt->t * next_scale;
124 add_point(pt->tag, pt->e, x, y, t);
128 void CriKeyConfig::limits()
130 bclamp(threshold, 0.0f, 1.0f);
131 bclamp(draw_mode, 0, DRAW_MODES-1);
134 int CriKeyConfig::add_point(int tag, int e, float x, float y, float t)
136 int k = points.size();
139 for( int i=k; --i>=0; ) {
140 int n = points[i]->tag;
141 if( n >= tag ) tag = n + 1;
144 points.append(new CriKeyPoint(tag, e, x, y, t));
148 void CriKeyConfig::del_point(int i)
150 points.remove_object_number(i);
156 class segment { public: int y, lt, rt; };
157 ArrayList<segment> stack;
159 void push(int y, int lt, int rt) {
160 segment &seg = stack.append();
161 seg.y = y; seg.lt = lt; seg.rt = rt;
163 void pop(int &y, int <, int &rt) {
164 segment &seg = stack.last();
165 y = seg.y; lt = seg.lt; rt = seg.rt;
172 bool edge_pixel(int i) { return edg[i] > 0; }
175 void fill(int x, int y);
178 FillRegion(VFrame *edg, VFrame *msk);
181 FillRegion::FillRegion(VFrame *edg, VFrame *msk)
183 this->w = msk->get_w();
184 this->h = msk->get_h();
185 this->msk = (uint8_t*) msk->get_data();
186 this->edg = (float*) edg->get_data();
189 void FillRegion::fill(int x, int y)
194 void FillRegion::run()
196 while( stack.size() > 0 ) {
200 for( int x=ilt; x<=irt; ++x,++ofs ) {
201 if( !msk[ofs] ) continue;
203 if( edge_pixel(ofs) ) continue;
206 for( int i=lt; --i>=0; ) {
207 if( !msk[--lofs] ) break;
208 msk[lofs] = 0; lt = i;
209 if( edge_pixel(lofs) ) break;
212 for( int i=rt; ++i< w; ) {
213 if( !msk[++rofs] ) break;
214 msk[rofs] = 0; rt = i;
215 if( edge_pixel(rofs) ) break;
217 if( y+1 < h ) push(y+1, lt, rt);
218 if( y-1 >= 0 ) push(y-1, lt, rt);
224 CriKey::CriKey(PluginServer *server)
225 : 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 void CriKey::render_gui(void *data)
266 CriKey *crikey = (CriKey *)data;
268 crikey->selected = selected;
271 int CriKey::is_dragging()
275 send_render_gui(this);
279 int CriKey::new_point()
281 EDLSession *session = get_edl()->session;
282 float x = !session ? 0.f : session->output_w / 2.f;
283 float y = !session ? 0.f : session->output_h / 2.f;
284 return config.add_point(-1, 0, x, y, 0.5f);
287 void CriKeyConfig::save_data(KeyFrame *keyframe)
291 // cause data to be stored directly in text
292 output.set_shared_output(keyframe->xbuf);
294 output.tag.set_title("CRIKEY");
295 output.tag.set_property("THRESHOLD", threshold);
296 output.tag.set_property("DRAW_MODE", draw_mode);
298 output.append_newline();
299 output.tag.set_title("/CRIKEY");
301 output.append_newline();
302 for( int i=0, n = points.size(); i<n; ++i ) {
303 CriKeyPoint *pt = points[i];
304 char point[BCSTRLEN];
305 sprintf(point,"/POINT_%d",pt->tag);
306 output.tag.set_title(point+1);
307 output.tag.set_property("E", pt->e);
308 output.tag.set_property("X", pt->x);
309 output.tag.set_property("Y", pt->y);
310 output.tag.set_property("T", pt->t);
312 output.tag.set_title(point+0);
314 output.append_newline();
316 output.terminate_string();
318 void CriKey::save_data(KeyFrame *keyframe)
320 config.save_data(keyframe);
323 void CriKeyConfig::read_data(KeyFrame *keyframe)
326 input.set_shared_input(keyframe->xbuf);
327 points.remove_all_objects();
330 while( !(result=input.read_tag()) ) {
331 if( input.tag.title_is("CRIKEY") ) {
332 threshold = input.tag.get_property("THRESHOLD", threshold);
333 draw_mode = input.tag.get_property("DRAW_MODE", draw_mode);
336 else if( !strncmp(input.tag.get_title(),"POINT_",6) ) {
337 int tag = atoi(input.tag.get_title() + 6);
338 int e = input.tag.get_property("E", 0);
339 float x = input.tag.get_property("X", 0.f);
340 float y = input.tag.get_property("Y", 0.f);
341 float t = input.tag.get_property("T", .5f);
342 add_point(tag, e, x, y, t);
346 void CriKey::read_data(KeyFrame *keyframe)
348 config.read_data(keyframe);
349 if( !config.points.size() )
353 void CriKey::span_keyframes(KeyFrame *src, int64_t start, int64_t end)
355 CriKeyConfig src_config;
356 src_config.read_data(src);
357 KeyFrames *keyframes = (KeyFrames *)src->autos;
358 KeyFrame *prev = keyframes->get_prev_keyframe(start, PLAY_FORWARD);
359 CriKeyConfig prev_config;
360 prev_config.read_data(prev);
361 // Always update the first one
362 update_parameter(prev_config, src_config, prev);
363 KeyFrame *curr = (KeyFrame*)prev->next;
364 while( curr && curr->position < end ) {
365 update_parameter(prev_config, src_config, curr);
366 curr = (KeyFrame*)curr->next;
370 void CriKeyPoint::update_parameter(CriKeyPoint *prev, CriKeyPoint *src)
372 if( prev->e != src->e ) e = src->e;
373 if( prev->x != src->x ) x = src->x;
374 if( prev->y != src->y ) y = src->y;
375 if( prev->t != src->t ) t = src->t;
378 void CriKey::update_parameter(CriKeyConfig &prev_config, CriKeyConfig &src_config,
381 CriKeyConfig dst_config;
382 dst_config.read_data(keyframe);
383 if( !EQUIV(prev_config.threshold, src_config.threshold) )
384 dst_config.threshold = src_config.threshold;
385 if( prev_config.draw_mode != src_config.draw_mode )
386 dst_config.draw_mode = src_config.draw_mode;
387 int src_points = src_config.points.size();
388 int dst_points = dst_config.points.size();
389 int prev_points = prev_config.points.size();
390 int npoints = bmin(prev_points, bmin(src_points, dst_points));
391 for( int i=0; i<npoints; ++i ) {
392 CriKeyPoint *src_point = src_config.points[i];
393 int tag = src_point->tag, k = prev_points;
394 while( --k >= 0 && tag != prev_config.points[k]->tag );
395 if( k < 0 ) continue;
396 CriKeyPoint *prev_point = prev_config.points[k];
398 while( --k >= 0 && tag != dst_config.points[k]->tag );
399 if( k < 0 ) continue;
400 CriKeyPoint *dst_point = dst_config.points[k];
401 dst_point->update_parameter(prev_point, src_point);
403 dst_config.save_data(keyframe);
406 void CriKey::update_gui()
408 if( !thread ) return;
409 thread->window->lock_window("CriKey::update_gui");
410 CriKeyWindow *window = (CriKeyWindow*)thread->window;
411 if( load_configuration() ) {
412 window->update_gui();
415 thread->window->unlock_window();
418 void CriKey::draw_alpha(VFrame *msk)
420 uint8_t **src_rows = src->get_rows();
421 uint8_t **msk_rows = msk->get_rows();
422 switch( color_model ) {
424 for( int y=0; y<h; ++y ) {
425 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
426 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
428 float *px = (float *)sp;
429 px[0] = px[1] = px[2] = 0;
434 for( int y=0; y<h; ++y ) {
435 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
436 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
438 float *px = (float *)sp;
444 for( int y=0; y<h; ++y ) {
445 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
446 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
449 px[0] = px[1] = px[2] = 0;
454 for( int y=0; y<h; ++y ) {
455 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
456 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
460 px[1] = px[2] = 0x80;
466 for( int y=0; y<h; ++y ) {
467 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
468 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
478 void CriKey::draw_edge(VFrame *edg)
480 uint8_t **src_rows = src->get_rows();
481 float **edg_rows = (float**) edg->get_rows(), gain = 10;
482 switch( color_model ) {
484 for( int y=0; y<h; ++y ) {
485 uint8_t *sp = src_rows[y];
486 float *ap = edg_rows[y];
487 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
488 float *px = (float *)sp, v = *ap * gain;
489 px[0] = px[1] = px[2] = v<1 ? v : 1;
494 for( int y=0; y<h; ++y ) {
495 uint8_t *sp = src_rows[y];
496 float *ap = edg_rows[y];
497 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
498 float *px = (float *)sp, v = *ap * gain;
499 px[0] = px[1] = px[2] = v<1 ? v : 1;
505 for( int y=0; y<h; ++y ) {
506 uint8_t *sp = src_rows[y];
507 float *ap = edg_rows[y];
508 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
509 uint8_t *px = sp; float v = *ap * gain;
510 px[0] = px[1] = px[2] = v<1 ? v*256 : 255;
515 for( int y=0; y<h; ++y ) {
516 uint8_t *sp = src_rows[y];
517 float *ap = edg_rows[y];
518 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
519 uint8_t *px = sp; float v = *ap * gain;
520 px[0] = px[1] = px[2] = v<1 ? v*256 : 255;
526 for( int y=0; y<h; ++y ) {
527 uint8_t *sp = src_rows[y];
528 float *ap = edg_rows[y];
529 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
530 uint8_t *px = sp; float v = *ap * gain;
531 px[0] = *ap<1 ? v*256 : 255;
532 px[1] = px[2] = 0x80;
537 for( int y=0; y<h; ++y ) {
538 uint8_t *sp = src_rows[y];
539 float *ap = edg_rows[y];
540 for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
541 uint8_t *px = sp; float v = *ap * gain;
542 px[0] = *ap<1 ? v*256 : 255;
543 px[1] = px[2] = 0x80;
551 void CriKey::draw_mask(VFrame *msk)
553 uint8_t **src_rows = src->get_rows();
554 uint8_t **msk_rows = msk->get_rows();
555 float scale = 1 / 255.0f;
556 switch( color_model ) {
558 for( int y=0; y<h; ++y ) {
559 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
560 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
561 float a = *mp * scale;
562 float *px = (float *)sp;
563 px[0] = px[1] = px[2] = a;
568 for( int y=0; y<h; ++y ) {
569 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
570 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
571 float a = *mp * scale;
572 float *px = (float *)sp;
573 px[0] = px[1] = px[2] = a;
579 for( int y=0; y<h; ++y ) {
580 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
581 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
582 float a = *mp * scale;
584 px[0] = px[1] = px[2] = a * 255;
589 for( int y=0; y<h; ++y ) {
590 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
591 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
592 float a = *mp * scale;
594 px[0] = px[1] = px[2] = a * 255;
600 for( int y=0; y<h; ++y ) {
601 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
602 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
603 float a = *mp * scale;
606 px[1] = px[2] = 0x80;
611 for( int y=0; y<h; ++y ) {
612 uint8_t *sp = src_rows[y], *mp = msk_rows[y];
613 for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
614 float a = *mp * scale;
617 px[1] = px[2] = 0x80;
626 void CriKey::draw_point(VFrame *src, CriKeyPoint *pt)
628 int d = bmax(w,h) / 200 + 2;
629 int r = d/2+1, x = pt->x, y = pt->y;
630 src->draw_smooth(x-r,y+0, x-r, y-r, x+0,y-r);
631 src->draw_smooth(x+0,y-r, x+r, y-r, x+r,y+0);
632 src->draw_smooth(x+r,y+0, x+r, y+r, x+0,y+r);
633 src->draw_smooth(x+0,y+r, x-r, y+r, x-r,y+0);
635 src->set_pixel_color(RED);
636 src->draw_x(pt->x, pt->y, d);
639 src->set_pixel_color(BLUE);
640 src->draw_t(pt->x, pt->y, d);
645 static void fill_edge(VFrame *vfrm, int w, int h)
647 int w1 = w-1, h1 = h-1;
648 float *dp = (float*) vfrm->get_data();
649 if( w1 > 0 ) for( int y=0; y<h1; ++y,dp+=w ) dp[w1] = dp[w1-1];
650 if( h1 > 0 ) for( int x=0; x<w; ++x ) dp[x] = dp[x-w];
653 int CriKey::process_buffer(VFrame *frame, int64_t start_position, double frame_rate)
655 load_configuration();
657 w = src->get_w(), h = src->get_h();
658 color_model = src->get_color_model();
659 bpp = BC_CModels::calculate_pixelsize(color_model);
660 is_float = BC_CModels::is_float(color_model);
661 is_yuv = BC_CModels::is_yuv(color_model);
662 comp = BC_CModels::components(color_model);
663 if( comp > 3 ) comp = 3;
665 read_frame(src, 0, start_position, frame_rate, 0);
666 VFrame::get_temp(edg, w, h, BC_A_FLOAT);
669 engine = new CriKeyEngine(this,
670 PluginClient::get_project_smp() + 1,
671 PluginClient::get_project_smp() + 1);
673 VFrame::get_temp(msk, w, h, BC_A8);
674 memset(msk->get_data(), 0xff, msk->get_data_size());
676 for( int i=0, n=config.points.size(); i<n; ++i ) {
677 CriKeyPoint *pt = config.points[i];
678 if( !pt->e ) continue;
679 if( set_target(engine->color, pt->x, pt->y) ) continue;
680 engine->threshold = pt->t;
682 engine->process_packages();
683 fill_edge(edg, w, h);
684 FillRegion fill_region(edg, msk);
685 fill_region.fill(pt->x, pt->y);
689 //crikey_pgm("/tmp/msk.pgm",msk);
690 switch( config.draw_mode ) {
691 case DRAW_ALPHA: draw_alpha(msk); break;
692 case DRAW_EDGE: draw_edge(edg); break;
693 case DRAW_MASK: draw_mask(msk); break;
696 if( is_dragging() ) {
697 for( int i=0, n=config.points.size(); i<n; ++i ) {
698 CriKeyPoint *pt = config.points[i];
699 src->set_pixel_color(selected == i ? GREEN : WHITE);
707 void CriKeyEngine::init_packages()
709 int y = 0, h1 = plugin->h-1;
710 for(int i = 0; i < get_total_packages(); ) {
711 CriKeyPackage *pkg = (CriKeyPackage*)get_package(i++);
713 y = h1 * i / LoadServer::get_total_packages();
718 LoadPackage* CriKeyEngine::new_package()
720 return new CriKeyPackage();
723 LoadClient* CriKeyEngine::new_client()
725 return new CriKeyUnit(this);
728 #define EDGE_MACRO(type, components, is_yuv) \
730 uint8_t **src_rows = src->get_rows(); \
731 int comps = MIN(components, 3); \
732 for( int y=y1; y<y2; ++y ) { \
733 uint8_t *row0 = src_rows[y], *row1 = src_rows[y+1]; \
734 float *edgp = edg_rows[y]; \
735 for( int x=x1; x<x2; ++edgp,++x,row0+=bpp,row1+=bpp ) { \
736 type *r0 = (type*)row0, *r1 = (type*)row1; \
737 float a00 = 0, a01 = 0, a10 = 0, a11 = 0; \
738 for( int c=0; c<comps; ++c,++r0,++r1 ) { \
739 float t = target_color[c]; \
740 a00 += fabs(t - r0[0]); \
741 a01 += fabs(t - r0[components]); \
742 a10 += fabs(t - r1[0]); \
743 a11 += fabs(t - r1[components]); \
745 float mx = scale * bmax(bmax(a00, a01), bmax(a10, a11)); \
746 if( mx < threshold ) continue; \
747 float mn = scale * bmin(bmin(a00, a01), bmin(a10, a11)); \
748 if( mn >= threshold ) continue; \
749 *edgp += (mx - mn); \
755 void CriKeyUnit::process_package(LoadPackage *package)
757 int color_model = server->plugin->color_model;
758 int bpp = server->plugin->bpp;
759 VFrame *src = server->plugin->src;
760 VFrame *edg = server->plugin->edg;
761 float **edg_rows = (float**)edg->get_rows();
762 float *target_color = server->color;
763 float threshold = 2.f * server->threshold*server->threshold;
764 float scale = 1./BC_CModels::calculate_max(color_model);
765 CriKeyPackage *pkg = (CriKeyPackage*)package;
766 int x1 = 0, x2 = server->plugin->w-1;
767 int y1 = pkg->y1, y2 = pkg->y2;
769 switch( color_model ) {
770 case BC_RGB_FLOAT: EDGE_MACRO(float, 3, 0);
771 case BC_RGBA_FLOAT: EDGE_MACRO(float, 4, 0);
772 case BC_RGB888: EDGE_MACRO(unsigned char, 3, 0);
773 case BC_YUV888: EDGE_MACRO(unsigned char, 3, 1);
774 case BC_RGBA8888: EDGE_MACRO(unsigned char, 4, 0);
775 case BC_YUVA8888: EDGE_MACRO(unsigned char, 4, 1);