2 * Cinelerra :: Blue Banana - color modification plugin for Cinelerra-CV
3 * Copyright (C) 2012-2013 Monty <monty@xiph.org>
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 "bcdisplayinfo.h"
27 #include "bcsignals.h"
31 #include "bluebanana.h"
32 #include "bluebananaconfig.h"
33 #include "bluebananawindow.h"
36 #include "loadbalance.h"
37 #include "playback3d.h"
40 #include "workarounds.h"
43 class BluebananaEngine;
44 class BluebananaWindow;
46 REGISTER_PLUGIN(BluebananaMain)
48 BluebananaMain::BluebananaMain(PluginServer *server)
49 : PluginVClient(server)
53 /* be sure a lookup update triggers */
54 lookup_cache.Hsel_lo=-999999;
55 lookup_cache.Ssel_lo=-999999;
56 lookup_cache.Vsel_lo=-999999;
57 lookup_cache.Fsel_lo=-999999;
59 lookup_cache.Sadj_lo=-999999;
60 lookup_cache.Vadj_lo=-999999;
61 lookup_cache.Radj_lo=-999999;
62 lookup_cache.Gadj_lo=-999999;
63 lookup_cache.Badj_lo=-999999;
69 memset(red_histogram,0,sizeof(red_histogram));
70 memset(green_histogram,0,sizeof(green_histogram));
71 memset(blue_histogram,0,sizeof(blue_histogram));
72 memset(hue_histogram,0,sizeof(hue_histogram));
73 memset(sat_histogram,0,sizeof(sat_histogram));
74 memset(value_histogram,0,sizeof(value_histogram));
77 BluebananaMain::~BluebananaMain() {
79 // if ants are running, run one more pane update to hide them (gui
80 // is already marked as closed)
81 if(server && server->mwindow && ants_counter>0)
82 server->mwindow->sync_parameters();
87 const char* BluebananaMain::plugin_title() { return _("Blue Banana"); }
88 int BluebananaMain::is_realtime() { return 1; }
90 NEW_WINDOW_MACRO(BluebananaMain, BluebananaWindow)
91 LOAD_CONFIGURATION_MACRO(BluebananaMain, BluebananaConfig)
93 void BluebananaMain::render_gui(void *data){
95 BluebananaMain *that = (BluebananaMain *)data; // that is server-side
96 BluebananaWindow *window = (BluebananaWindow *)thread->window;
97 window->lock_window("BluebananaMain::render_gui");
99 /* push histogram data to gui */
100 window->update_histograms(that);
102 /* push any colormodel update to gui */
103 if(that->frame && colormodel != that->frame->get_color_model()){
104 colormodel = that->frame->get_color_model();
108 window->unlock_window();
112 void BluebananaMain::update_gui(){
114 BluebananaWindow *window = (BluebananaWindow *)thread->window;
115 window->lock_window("BluebananaMain::update_gui");
116 window->flush_config_change();
117 int reconfigure = load_configuration();
121 window->unlock_window();
125 void BluebananaMain::save_data(KeyFrame *keyframe){
128 // cause data to be stored directly in text
129 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
130 output.tag.set_title("BLUEBANANA");
132 output.tag.set_property("ACTIVE", config.active);
133 output.tag.set_property("INVERT_SELECTION", config.invert_selection);
134 output.tag.set_property("USE_MASK", config.use_mask);
135 output.tag.set_property("CAPTURE_MASK", config.capture_mask);
137 output.tag.set_property("HUE_ACTIVE", config.Hsel_active);
138 output.tag.set_property("HUE_LO", config.Hsel_lo);
139 output.tag.set_property("HUE_HI", config.Hsel_hi);
140 output.tag.set_property("HUE_OVERLAP", config.Hsel_over);
142 output.tag.set_property("SATURATION_ACTIVE", config.Ssel_active);
143 output.tag.set_property("SATURATION_LO", config.Ssel_lo);
144 output.tag.set_property("SATURATION_HI", config.Ssel_hi);
145 output.tag.set_property("SATURATION_OVERLAP", config.Ssel_over);
147 output.tag.set_property("VALUE_ACTIVE", config.Vsel_active);
148 output.tag.set_property("VALUE_LO", config.Vsel_lo);
149 output.tag.set_property("VALUE_HI", config.Vsel_hi);
150 output.tag.set_property("VALUE_OVERLAP", config.Vsel_over);
152 output.tag.set_property("FILL_ACTIVE", config.Fsel_active);
153 output.tag.set_property("FILL_ERODE", config.Fsel_erode);
154 output.tag.set_property("FILL_LO", config.Fsel_lo);
155 output.tag.set_property("FILL_MID", config.Fsel_mid);
156 output.tag.set_property("FILL_HI", config.Fsel_hi);
157 output.tag.set_property("FILL_FEATHER", config.Fsel_over);
159 output.tag.set_property("HUE_ADJUST_ACTIVE", config.Hadj_active);
160 output.tag.set_property("HUE_ADJUST", config.Hadj_val);
162 output.tag.set_property("SATURATION_ADJUST_ACTIVE", config.Sadj_active);
163 output.tag.set_property("SATURATION_ADJUST_GAMMA", config.Sadj_gamma);
164 output.tag.set_property("SATURATION_ADJUST_LO", config.Sadj_lo);
165 output.tag.set_property("SATURATION_ADJUST_HI", config.Sadj_hi);
167 output.tag.set_property("VALUE_ADJUST_ACTIVE", config.Vadj_active);
168 output.tag.set_property("VALUE_ADJUST_GAMMA", config.Vadj_gamma);
169 output.tag.set_property("VALUE_ADJUST_LO", config.Vadj_lo);
170 output.tag.set_property("VALUE_ADJUST_HI", config.Vadj_hi);
172 output.tag.set_property("RED_ADJUST_ACTIVE", config.Radj_active);
173 output.tag.set_property("RED_ADJUST_GAMMA", config.Radj_gamma);
174 output.tag.set_property("RED_ADJUST_LO", config.Radj_lo);
175 output.tag.set_property("RED_ADJUST_HI", config.Radj_hi);
177 output.tag.set_property("GREEN_ADJUST_ACTIVE", config.Gadj_active);
178 output.tag.set_property("GREEN_ADJUST_GAMMA", config.Gadj_gamma);
179 output.tag.set_property("GREEN_ADJUST_LO", config.Gadj_lo);
180 output.tag.set_property("GREEN_ADJUST_HI", config.Gadj_hi);
182 output.tag.set_property("BLUE_ADJUST_ACTIVE", config.Badj_active);
183 output.tag.set_property("BLUE_ADJUST_GAMMA", config.Badj_gamma);
184 output.tag.set_property("BLUE_ADJUST_LO", config.Badj_lo);
185 output.tag.set_property("BLUE_ADJUST_HI", config.Badj_hi);
187 output.tag.set_property("OPACITY_ADJUST_ACTIVE", config.Oadj_active);
188 output.tag.set_property("OPACITY_ADJUST", config.Oadj_val);
191 output.append_newline();
193 output.tag.set_title("/BLUEBANANA");
195 output.append_newline();
197 if(keyframe->position==0){
198 /* this will otherwise overwrite the nonauto information as well */
199 output_nonauto(&output);
202 output.terminate_string();
205 // save non-auto data to the default keyframe at zero and does it
206 // without alerting the keyframing mechanism
207 void BluebananaMain::save_nonauto(){
209 KeyFrame *default_keyframe=get_prev_keyframe(0);
210 if(default_keyframe){
215 input.read_from_string(default_keyframe->get_data());
216 output.set_shared_output(default_keyframe->get_data(), MESSAGESIZE);
219 result = input.read_tag();
222 !input.tag.title_is("BLUEBANANA_NONAUTO") &&
223 !input.tag.title_is("/BLUEBANANA_NONAUTO")){
224 input.tag.write_tag(&output);
225 output.append_newline();
229 output_nonauto(&output);
233 void BluebananaMain::output_nonauto(FileXML *output){
234 output->tag.set_title("BLUEBANANA_NONAUTO");
235 output->tag.set_property("MARK", config.mark);
236 output->append_tag();
237 output->tag.set_title("/BLUEBANANA_NONAUTO");
238 output->append_tag();
239 output->append_newline();
240 output->terminate_string();
243 void BluebananaMain::load_nonauto(){
244 /* nonauto data stored in the default keyframe at position 0 */
245 KeyFrame *default_keyframe=get_prev_keyframe(0);
246 if(default_keyframe){
249 input.set_shared_input(default_keyframe->get_data(), strlen(default_keyframe->get_data()));
252 result = input.read_tag();
254 if(!result && input.tag.title_is("BLUEBANANA_NONAUTO")){
255 config.mark = input.tag.get_property("MARK", config.mark);
262 void BluebananaMain::read_data(KeyFrame *keyframe){
266 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
269 result = input.read_tag();
271 if(!result && input.tag.title_is("BLUEBANANA")){
272 config.active = input.tag.get_property("ACTIVE", config.active);
273 config.invert_selection = input.tag.get_property("INVERT_SELECTION", config.invert_selection);
274 config.use_mask = input.tag.get_property("USE_MASK", config.use_mask);
275 config.capture_mask = input.tag.get_property("CAPTURE_MASK", config.capture_mask);
277 config.Hsel_active = input.tag.get_property("HUE_ACTIVE", config.Hsel_active);
278 config.Hsel_lo = input.tag.get_property("HUE_LO", config.Hsel_lo);
279 config.Hsel_hi = input.tag.get_property("HUE_HI", config.Hsel_hi);
280 config.Hsel_over = input.tag.get_property("HUE_OVERLAP", config.Hsel_over);
282 config.Ssel_active = input.tag.get_property("SATURATION_ACTIVE", config.Ssel_active);
283 config.Ssel_lo = input.tag.get_property("SATURATION_LO", config.Ssel_lo);
284 config.Ssel_hi = input.tag.get_property("SATURATION_HI", config.Ssel_hi);
285 config.Ssel_over = input.tag.get_property("SATURATION_OVERLAP", config.Ssel_over);
287 config.Vsel_active = input.tag.get_property("VALUE_ACTIVE", config.Vsel_active);
288 config.Vsel_lo = input.tag.get_property("VALUE_LO", config.Vsel_lo);
289 config.Vsel_hi = input.tag.get_property("VALUE_HI", config.Vsel_hi);
290 config.Vsel_over = input.tag.get_property("VALUE_OVERLAP", config.Vsel_over);
292 config.Fsel_active = input.tag.get_property("FILL_ACTIVE", config.Fsel_active);
293 config.Fsel_erode = input.tag.get_property("FILL_ERODE", config.Fsel_erode);
294 config.Fsel_lo = input.tag.get_property("FILL_LO", config.Fsel_lo);
295 config.Fsel_mid = input.tag.get_property("FILL_MID", config.Fsel_mid);
296 config.Fsel_hi = input.tag.get_property("FILL_HI", config.Fsel_hi);
297 config.Fsel_over = input.tag.get_property("FILL_FEATHER", config.Fsel_over);
299 config.Hadj_active = input.tag.get_property("HUE_ADJUST_ACTIVE", config.Hadj_active);
300 config.Hadj_val = input.tag.get_property("HUE_ADJUST", config.Hadj_val);
302 config.Sadj_active = input.tag.get_property("SATURATION_ADJUST_ACTIVE", config.Sadj_active);
303 config.Sadj_gamma = input.tag.get_property("SATURATION_ADJUST_GAMMA", config.Sadj_gamma);
304 config.Sadj_lo = input.tag.get_property("SATURATION_ADJUST_LO", config.Sadj_lo);
305 config.Sadj_hi = input.tag.get_property("SATURATION_ADJUST_HI", config.Sadj_hi);
307 config.Vadj_active = input.tag.get_property("VALUE_ADJUST_ACTIVE", config.Vadj_active);
308 config.Vadj_gamma = input.tag.get_property("VALUE_ADJUST_GAMMA", config.Vadj_gamma);
309 config.Vadj_lo = input.tag.get_property("VALUE_ADJUST_LO", config.Vadj_lo);
310 config.Vadj_hi = input.tag.get_property("VALUE_ADJUST_HI", config.Vadj_hi);
312 config.Radj_active = input.tag.get_property("RED_ADJUST_ACTIVE", config.Radj_active);
313 config.Radj_gamma = input.tag.get_property("RED_ADJUST_GAMMA", config.Radj_gamma);
314 config.Radj_lo = input.tag.get_property("RED_ADJUST_LO", config.Radj_lo);
315 config.Radj_hi = input.tag.get_property("RED_ADJUST_HI", config.Radj_hi);
317 config.Gadj_active = input.tag.get_property("GREEN_ADJUST_ACTIVE", config.Gadj_active);
318 config.Gadj_gamma = input.tag.get_property("GREEN_ADJUST_GAMMA", config.Gadj_gamma);
319 config.Gadj_lo = input.tag.get_property("GREEN_ADJUST_LO", config.Gadj_lo);
320 config.Gadj_hi = input.tag.get_property("GREEN_ADJUST_HI", config.Gadj_hi);
322 config.Badj_active = input.tag.get_property("BLUE_ADJUST_ACTIVE", config.Badj_active);
323 config.Badj_gamma = input.tag.get_property("BLUE_ADJUST_GAMMA", config.Badj_gamma);
324 config.Badj_lo = input.tag.get_property("BLUE_ADJUST_LO", config.Badj_lo);
325 config.Badj_hi = input.tag.get_property("BLUE_ADJUST_HI", config.Badj_hi);
327 config.Oadj_active = input.tag.get_property("OPACITY_ADJUST_ACTIVE", config.Oadj_active);
328 config.Oadj_val = input.tag.get_property("OPACITY_ADJUST", config.Oadj_val);
336 static void select_grow_h(float *hrow, float *vrow, int width){
340 for(i=0;i<width-1;i++){
341 if(hrow[i]<hrow[i+1])hrow[i]=hrow[i+1];
342 if(vrow[i]<hrow[i])vrow[i]=hrow[i];
345 for(i=width-1;i>0;i--){
346 if(hrow[i]<hrow[i-1])hrow[i]=hrow[i-1];
347 if(vrow[i]<hrow[i])vrow[i]=hrow[i];
351 static void select_shrink_h(float *hrow, float *vrow, int width){
355 for(i=0;i<width-1;i++){
356 if(hrow[i]>hrow[i+1])hrow[i]=hrow[i+1];
357 if(vrow[i]>hrow[i])vrow[i]=hrow[i];
360 for(i=width-1;i>0;i--){
361 if(hrow[i]>hrow[i-1])hrow[i]=hrow[i-1];
362 if(vrow[i]>hrow[i])vrow[i]=hrow[i];
366 static void select_grow_v(float *row0, float *row1, int width){
371 if(row0[i]<row1[i])row0[i]=row1[i];
374 static void select_shrink_v(float *row0, float *row1, int width){
377 /* spread out of 0 */
379 if(row0[i]>row1[i])row0[i]=row1[i];
382 static void threaded_horizontal(float *in, float *work,
383 int width, int height,
384 BluebananaEngine *e, int tasks, int passes,
385 void(*func)(float *, float *, int)){
387 /* these are individually fast operations; here we do in fact make
388 cache region collisions as impossible as we can. Live with the
389 overhead of the join. */
391 e->set_task(tasks,"H_even");
394 int row = (j*2)*height/(tasks*2);
395 int end = (j*2+1)*height/(tasks*2);
397 for(i=0;i<passes;i++)
398 func(in+row*width,work+row*width,width);
402 e->set_task(0,"H_odd");
404 int row = (j*2+1)*height/(tasks*2);
405 int end = (j*2+2)*height/(tasks*2);
407 for(i=0;i<passes;i++)
408 func(in+row*width,work+row*width,width);
413 static void threaded_vertical(float *work, float *temp,
414 int width, int height,
415 BluebananaEngine *e, int tasks,
416 void(*func)(float *, float *, int)){
418 /* regions overlap, so this becomes a bit more complex. */
420 /* rather than on-demand task allocation, we use the task engine to
421 grab a slot, then reuse this same slot through several joins */
423 e->set_task(tasks,"up_odd");
424 int region = e->next_task(); /* grab one slot */
425 int start_row = (region*2)*height/(tasks*2);
426 int mid_row = (region*2+1)*height/(tasks*2);
427 int end_row = (region*2+2)*height/(tasks*2);
430 /* spread up, starting at middle row, moving down */
431 /* first task is to save a copy of the un-transformed middle row, as
432 we'll need it for the even pass */
433 memcpy(temp,work+mid_row*width,sizeof(*temp)*width);
436 for(row=mid_row;row<end_row-1;row++)
437 func(work+row*width,work+(row+1)*width,width);
438 if(end_row<height && row<end_row)
439 func(work+row*width,work+(row+1)*width,width);
442 /* even interleave */
443 e->set_task(0,"up_even");
444 for(row=start_row;row<mid_row-1;row++)
445 func(work+row*width,work+(row+1)*width,width);
447 func(work+row*width,temp,width);
450 /* spread down, starting at mid row and moving up */
451 /* once again, grab a temp vector for the second pass overlap */
452 memcpy(temp,work+mid_row*width,sizeof(*temp)*width);
454 /* even interleave */
455 e->set_task(0,"down_even");
456 for(row=mid_row;row>start_row;row--)
457 func(work+row*width,work+(row-1)*width,width);
459 func(work+row*width,work+(row-1)*width,width);
463 e->set_task(0,"down_odd");
464 for(row=end_row-1;row>mid_row+1;row--)
465 func(work+row*width,work+(row-1)*width,width);
467 func(work+row*width,temp,width);
473 static float *fill_one(float *in, float *work,
474 int width, int height,
475 BluebananaEngine *e, // NULL if we're not threading
479 int tasks = e?e->get_total_packages():0;
486 /* multiple memcpys running at once is occasionally a total
487 cache disaster on Sandy Bridge. So only one thread gets to
488 copy, the others wait. */
489 e->set_task(1,"fill_memcpy");
490 while((j = e->next_task())>=0){
491 memcpy(work,in,width*height*sizeof(*work));
495 memcpy(work,in,width*height*sizeof(*work));
501 case 'H': /* grow horizontal */
503 threaded_horizontal(in, work, width, height, e, tasks, 1, select_grow_h);
505 for(j=0;j<height;j++)
506 select_grow_h(in+j*width,work+j*width,width);
510 case 'V': /* grow vertical */
512 threaded_vertical(work, temp, width, height, e, tasks, select_grow_v);
514 for(j=0;j<height-1;j++)
515 select_grow_v(work+j*width,work+(j+1)*width,width);
516 for(j=height-1;j>0;j--)
517 select_grow_v(work+j*width,work+(j-1)*width,width);
521 case 'h': /* shrink horizontal */
523 threaded_horizontal(in, work, width, height, e, tasks, 1, select_shrink_h);
525 for(j=0;j<height;j++)
526 select_shrink_h(in+j*width,work+j*width,width);
530 default: /* shrink vertical */
533 threaded_vertical(work, temp, width, height, e, tasks, select_shrink_v);
535 for(j=0;j<height-1;j++)
536 select_shrink_v(work+j*width,work+(j+1)*width,width);
537 for(j=height-1;j>0;j--)
538 select_shrink_v(work+j*width,work+(j-1)*width,width);
549 static void select_feather_h(float *p, float *dummy, int w){
550 for(int x=0;x<w-1;x++)
551 p[x] = (p[x]+p[x+1])*.5;
552 for(int x=w-1;x>0;x--)
553 p[x] = (p[x]+p[x-1])*.5;
556 static void select_feather_v(float *p0, float *p1, int w){
557 for(int x=0; x<w; x++)
558 p0[x] = (p0[x]+p1[x])*.5;
561 static void feather_one(float *in,
562 int width, int height,
563 BluebananaEngine *e, // NULL if we're not threading
566 int tasks = e?e->get_total_packages():0;
570 threaded_horizontal(in, 0, width, height, e, tasks, n, select_feather_h);
572 threaded_vertical(in, temp, width, height, e, tasks, select_feather_v);
574 for(j=0;j<height;j++)
576 select_feather_h(in+j*width,0,width);
579 for(j=0;j<height-1;j++)
580 select_feather_v(in+j*width,in+(j+1)*width,width);
581 for(j=height-1;j>0;j--)
582 select_feather_v(in+j*width,in+(j-1)*width,width);
587 /* here and not in the engine as the GUI also uses it (without
589 float *BluebananaMain::fill_selection(float *in, float *work,
590 int width, int height,
591 BluebananaEngine *e){
596 C=fill_one(A,B,width,height,e,select_one,select_one_n);
597 C=fill_one(C,(C==A?B:A),width,height,e,select_two,select_two_n);
598 C=fill_one(C,(C==A?B:A),width,height,e,select_three,select_three_n);
600 feather_one(C,width,height,e,config.Fsel_over);
606 int BluebananaMain::process_buffer(VFrame *frame,
607 int64_t start_position,
612 load_configuration();
619 read_frame(frame, 0, start_position, frame_rate, 0);
622 engine = new BluebananaEngine(this, get_project_smp() + 1,
623 get_project_smp() + 1);
625 engine->process_packages(frame);
627 // push final histograms to UI if it's up
629 send_render_gui(this);