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 "bcdisplayinfo.h"
25 #include "bcsignals.h"
30 #include "unsharpwindow.h"
36 REGISTER_PLUGIN(UnsharpMain)
40 UnsharpConfig::UnsharpConfig()
47 int UnsharpConfig::equivalent(UnsharpConfig &that)
49 return EQUIV(radius, that.radius) &&
50 EQUIV(amount, that.amount) &&
51 threshold == that.threshold;
54 void UnsharpConfig::copy_from(UnsharpConfig &that)
58 threshold = that.threshold;
61 void UnsharpConfig::interpolate(UnsharpConfig &prev,
65 int64_t current_frame)
67 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
68 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
69 this->radius = prev.radius * prev_scale + next.radius * next_scale;
70 this->amount = prev.amount * prev_scale + next.amount * next_scale;
71 this->threshold = (int)(prev.threshold * prev_scale + next.threshold * next_scale);
92 UnsharpMain::UnsharpMain(PluginServer *server)
93 : PluginVClient(server)
99 UnsharpMain::~UnsharpMain()
105 const char* UnsharpMain::plugin_title() { return _("Unsharp"); }
106 int UnsharpMain::is_realtime() { return 1; }
109 NEW_WINDOW_MACRO(UnsharpMain, UnsharpWindow)
111 LOAD_CONFIGURATION_MACRO(UnsharpMain, UnsharpConfig)
115 void UnsharpMain::update_gui()
119 if(load_configuration())
121 thread->window->lock_window("UnsharpMain::update_gui");
122 ((UnsharpWindow*)thread->window)->update();
123 thread->window->unlock_window();
131 void UnsharpMain::save_data(KeyFrame *keyframe)
135 // cause data to be stored directly in text
136 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
137 output.tag.set_title("UNSHARP");
139 output.tag.set_property("RADIUS", config.radius);
140 output.tag.set_property("AMOUNT", config.amount);
141 output.tag.set_property("THRESHOLD", config.threshold);
143 output.terminate_string();
146 void UnsharpMain::read_data(KeyFrame *keyframe)
150 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
156 result = input.read_tag();
160 if(input.tag.title_is("UNSHARP"))
162 config.radius = input.tag.get_property("RADIUS", config.radius);
163 config.amount = input.tag.get_property("AMOUNT", config.amount);
164 config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
176 int UnsharpMain::process_buffer(VFrame *frame,
177 int64_t start_position,
180 /*int need_reconfigure =*/ load_configuration();
182 if(!engine) engine = new UnsharpEngine(this,
183 get_project_smp() + 1, get_project_smp() + 1);
184 read_frame(frame, 0, get_source_position(), get_framerate(), 0);
185 engine->do_unsharp(frame);
200 UnsharpPackage::UnsharpPackage()
210 UnsharpUnit::UnsharpUnit(UnsharpEngine *server,
214 this->plugin = plugin;
215 this->server = server;
219 UnsharpUnit::~UnsharpUnit()
225 // Derived from the Gimp.
226 // In the original file it says
228 // Copyright (C) 1999 Winston Chang
229 // <winstonc@cs.wisc.edu>
230 // <winston@stdout.org>
232 // Adapted for Cinelerra by Heroine Virtual Ltd.
234 static int calculate_convolution_matrix(double radius, double **cmatrix)
236 radius = fabs(radius) + 1.0;
237 double std_dev = radius;
238 radius = std_dev * 2;
239 int matrix_length = (int)(2 * ceil(radius - 0.5) + 1);
240 matrix_length = MAX(1, matrix_length);
241 // int matrix_midpoint = matrix_length / 2 + 1;
242 (*cmatrix) = new double[matrix_length];
244 // Top right of matrix
245 for(int i = matrix_length / 2 + 1; i < matrix_length; i++)
247 double base_x = i - floor(matrix_length / 2) - 0.5;
249 for(int j = 1; j <= 50; j++)
251 if(base_x + 0.02 * j <= radius)
253 sum += exp(-(base_x + 0.02 * j) *
254 (base_x + 0.02 * j) /
255 (2 * std_dev * std_dev));
258 (*cmatrix)[i] = sum / 50;
261 // Top left of matrix
262 for(int i = 0; i < matrix_length / 2; i++)
264 (*cmatrix)[i] = (*cmatrix)[matrix_length - 1 - i];
269 for(int j = 0; j <= 50; j++)
271 sum += exp(-(0.5 + 0.02 * j) *
273 (2 * std_dev * std_dev));
275 (*cmatrix)[matrix_length / 2] = sum / 51;
279 for(int i = 0; i < matrix_length; i++)
280 sum += (*cmatrix)[i];
281 for(int i = 0; i < matrix_length; i++)
282 (*cmatrix)[i] = (*cmatrix)[i] / sum;
284 return matrix_length;
287 static double get_convolution(double *cmatrix,
291 return cmatrix[index] * input;
294 static void blur_pixels(double *cmatrix,
301 if(cmatrix_length > pixels)
303 for(int pixel = 0; pixel < pixels; pixel++)
306 for(int j = 0; j < pixels; j++)
308 if((j + cmatrix_length / 2 - pixel >= 0) &&
309 (j + cmatrix_length / 2 - pixel < cmatrix_length))
311 scale += cmatrix[j + cmatrix_length / 2 - pixel];
315 for(int i = 0; i < components; i++)
318 for(int j = 0; j < pixels; j++)
320 if((j >= pixel - cmatrix_length / 2) &&
321 (j <= pixel + cmatrix_length / 2))
323 sum += input[j * components + i] * cmatrix[i];
326 output[pixel * components + i] = sum / scale;
332 int cmatrix_middle = cmatrix_length / 2;
334 for(pixel = 0; pixel < cmatrix_middle; pixel++)
337 for(int j = cmatrix_middle - pixel; j < cmatrix_length;j++)
342 for(int i = 0; i < components; i++)
345 for(int j = cmatrix_middle - pixel; j < cmatrix_length; j++)
347 sum += input[(pixel + j - cmatrix_middle) * components + i] *
350 output[pixel * components + i] = sum / scale;
354 float *output_ptr = output + pixel * components;
355 for( ; pixel < pixels - cmatrix_middle; pixel++)
357 float *input_ptr = input + (pixel - cmatrix_middle) * components;
358 for(int i = 0; i < components; i++)
361 float *input_ptr2 = input_ptr;
362 for(int j = cmatrix_length; j > 0; j--)
364 sum += get_convolution(cmatrix,
367 input_ptr2 += components;
374 for( ; pixel < pixels; pixel++)
377 for(int j = 0; j < pixels - pixel + cmatrix_middle; j++)
382 for(int i = 0; i < components; i++)
385 for(int j = 0; j < pixels - pixel + cmatrix_middle; j++)
387 sum += input[(pixel + j - cmatrix_middle) * components + i] *
390 output[pixel * components + i] = sum / scale;
396 #define GET_ROW(type, components) \
398 type *in_row = (type*)src->get_rows()[row]; \
399 int pixels = src->get_w() * components; \
400 for(int i = 0; i < pixels; i++) \
402 dst[i] = in_row[i]; \
406 static void get_row(float *dst, VFrame *src, int row)
408 switch(src->get_color_model())
412 GET_ROW(unsigned char, 3);
419 GET_ROW(unsigned char, 4);
425 GET_ROW(uint16_t, 3);
427 case BC_YUVA16161616:
428 GET_ROW(uint16_t, 4);
433 static void get_column(float *dst, VFrame *src, int column)
435 int components = BC_CModels::components(src->get_color_model());
436 for(int i = 0; i < src->get_h(); i++)
438 float *input_pixel = (float*)src->get_rows()[i] + column * components;
439 memcpy(dst, input_pixel, sizeof(float) * components);
444 static void put_column(float *src, VFrame *dst, int column)
446 int components = BC_CModels::components(dst->get_color_model());
447 for(int i = 0; i < dst->get_h(); i++)
449 float *output_pixel = (float*)dst->get_rows()[i] + column * components;
450 memcpy(output_pixel, src, sizeof(float) * components);
455 void UnsharpUnit::process_package(LoadPackage *package)
457 UnsharpPackage *pkg = (UnsharpPackage*)package;
458 // int w = server->src->get_w();
459 // int h = server->src->get_h();
460 int color_model = server->src->get_color_model();
461 int components = BC_CModels::components(color_model);
463 int cmatrix_length = 0;
464 int padded_y1 = pkg->y1;
465 int padded_y2 = pkg->y2;
467 cmatrix_length = calculate_convolution_matrix(
468 plugin->config.radius,
472 if(padded_y2 < server->src->get_h())
474 padded_y2 += cmatrix_length / 2;
475 padded_y2 = MIN(server->src->get_h(), padded_y2);
479 padded_y1 -= cmatrix_length / 2;
480 padded_y1 = MAX(0, padded_y1);
483 int padded_rows = padded_y2 - padded_y1;
485 if(!temp || temp->get_h() != padded_rows)
494 temp->set_use_shm(0);
500 server->src->get_w(),
502 components == 3 ? BC_RGB_FLOAT : BC_RGBA_FLOAT,
506 float *temp_in = new float[MAX(temp->get_w(), padded_rows) * components];
507 float *temp_out = new float[MAX(temp->get_w(), padded_rows) * components];
510 for(int i = padded_y1; i < padded_y2; i++)
512 get_row(temp_in, server->src, i);
519 // printf("UnsharpUnit::process_package %d %p %p %p %d %d\n",
522 // temp->get_rows()[0],
525 // temp->get_bytes_per_line());
526 memcpy(temp->get_rows()[i - padded_y1],
528 temp->get_bytes_per_line());
531 //Now we're 100% floating point. Blur the columns
532 for(int i = 0; i < temp->get_w(); i++)
534 get_column(temp_in, temp, i);
541 put_column(temp_out, temp, i);
545 //printf("%f %f %d\n", plugin->config.radius,plugin->config.amount, plugin->config.threshold);
548 #define UNSHARPEN(type, components, max) \
550 float threshold = (float)plugin->config.threshold * max / 0xff; \
551 float amount = plugin->config.amount; \
553 for(int i = pkg->y1; i < pkg->y2; i++) \
555 float *blurry_row = (float*)temp->get_rows()[i - padded_y1]; \
556 type *orig_row = (type*)server->src->get_rows()[i]; \
557 for(int j = 0; j < server->src->get_w(); j++) \
559 for(int k = 0; k < components; k++) \
561 float diff = *orig_row - *blurry_row; \
562 if(fabsf(2 * diff) < threshold) \
564 float value = *orig_row + amount * diff; \
565 if(sizeof(type) == 4) \
566 *orig_row = (type)value; \
568 *orig_row = (type)CLIP(value, 0, max); \
576 // Apply unsharpening
581 UNSHARPEN(unsigned char, 3, 0xff);
585 UNSHARPEN(unsigned char, 4, 0xff);
588 UNSHARPEN(float, 3, 1.0);
591 UNSHARPEN(float, 4, 1.0);
594 UNSHARPEN(uint16_t, 3, 0xffff);
596 case BC_YUVA16161616:
597 UNSHARPEN(uint16_t, 4, 0xffff);
614 UnsharpEngine::UnsharpEngine(UnsharpMain *plugin,
619 total_clients, total_packages
622 this->plugin = plugin;
625 UnsharpEngine::~UnsharpEngine()
630 void UnsharpEngine::init_packages()
632 for(int i = 0; i < get_total_packages(); i++)
634 UnsharpPackage *pkg = (UnsharpPackage*)get_package(i);
635 pkg->y1 = src->get_h() * i / get_total_packages();
636 pkg->y2 = src->get_h() * (i + 1) / get_total_packages();
640 LoadClient* UnsharpEngine::new_client()
642 return new UnsharpUnit(this, plugin);
645 LoadPackage* UnsharpEngine::new_package()
647 return new UnsharpPackage;
651 void UnsharpEngine::do_unsharp(VFrame *src)