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"
29 #include "pluginvclient.h"
36 #define RESET_Y_SLIDER 1
37 #define RESET_U_SLIDER 2
38 #define RESET_V_SLIDER 3
50 void reset(int clear);
52 void copy_from(YUVConfig &src);
53 int equivalent(YUVConfig &src);
54 void interpolate(YUVConfig &prev,
63 class YUVLevel : public BC_FSlider
66 YUVLevel(YUVEffect *plugin, float *output, int x, int y);
72 class YUVReset : public BC_GenericButton
75 YUVReset(YUVEffect *plugin, YUVWindow *window, int x, int y);
82 class YUVSliderClr : public BC_GenericButton
85 YUVSliderClr(YUVEffect *plugin, YUVWindow *window, int x, int y, int w, int clear);
93 class YUVWindow : public PluginClientWindow
96 YUVWindow(YUVEffect *plugin);
97 void create_objects();
98 void update_gui(int clear);
102 YUVSliderClr *yClr, *uClr, *vClr;
107 class YUVEffect : public PluginVClient
110 YUVEffect(PluginServer *server);
114 PLUGIN_CLASS_MEMBERS(YUVConfig)
115 int process_realtime(VFrame *input, VFrame *output);
117 void save_data(KeyFrame *keyframe);
118 void read_data(KeyFrame *keyframe);
126 REGISTER_PLUGIN(YUVEffect)
134 YUVConfig::YUVConfig()
139 void YUVConfig::reset(int clear)
142 case RESET_Y_SLIDER : y = 0;
144 case RESET_U_SLIDER : u = 0;
146 case RESET_V_SLIDER : v = 0;
155 void YUVConfig::copy_from(YUVConfig &src)
162 int YUVConfig::equivalent(YUVConfig &src)
164 return EQUIV(y, src.y) && EQUIV(u, src.u) && EQUIV(v, src.v);
167 void YUVConfig::interpolate(YUVConfig &prev,
173 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
174 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
176 y = prev.y * prev_scale + next.y * next_scale;
177 u = prev.u * prev_scale + next.u * next_scale;
178 v = prev.v * prev_scale + next.v * next_scale;
188 YUVLevel::YUVLevel(YUVEffect *plugin, float *output, int x, int y)
198 this->plugin = plugin;
199 this->output = output;
202 int YUVLevel::handle_event()
204 *output = get_value();
205 plugin->send_configure_change();
210 YUVReset::YUVReset(YUVEffect *plugin, YUVWindow *window, int x, int y)
211 : BC_GenericButton(x, y, _("Reset"))
213 this->plugin = plugin;
214 this->window = window;
216 YUVReset::~YUVReset()
219 int YUVReset::handle_event()
221 plugin->config.reset(RESET_ALL);
222 window->update_gui(RESET_ALL);
223 plugin->send_configure_change();
228 YUVSliderClr::YUVSliderClr(YUVEffect *plugin, YUVWindow *window, int x, int y, int w, int clear)
229 : BC_GenericButton(x, y, w, _("⌂"))
231 this->plugin = plugin;
232 this->window = window;
235 YUVSliderClr::~YUVSliderClr()
238 int YUVSliderClr::handle_event()
240 // clear==1 ==> Y slider
241 // clear==2 ==> U slider
242 // clear==3 ==> V slider
243 plugin->config.reset(clear);
244 window->update_gui(clear);
245 plugin->send_configure_change();
250 YUVWindow::YUVWindow(YUVEffect *plugin)
251 : PluginClientWindow(plugin, 310, 135, 310, 135, 0)
253 this->plugin = plugin;
256 void YUVWindow::create_objects()
258 int x = 10, y = 10, x1 = 40;
259 int x2 = 0; int clrBtn_w = 50;
261 add_subwindow(new BC_Title(x, y, _("Y:")));
262 add_subwindow(this->y = new YUVLevel(plugin, &plugin->config.y, x1, y));
263 x2 = x1 + this->y->get_w() + 10;
264 add_subwindow(yClr = new YUVSliderClr(plugin, this, x2, y, clrBtn_w, RESET_Y_SLIDER));
267 add_subwindow(new BC_Title(x, y, _("U:")));
268 add_subwindow(u = new YUVLevel(plugin, &plugin->config.u, x1, y));
269 add_subwindow(uClr = new YUVSliderClr(plugin, this, x2, y, clrBtn_w, RESET_U_SLIDER));
272 add_subwindow(new BC_Title(x, y, _("V:")));
273 add_subwindow(v = new YUVLevel(plugin, &plugin->config.v, x1, y));
274 add_subwindow(vClr = new YUVSliderClr(plugin, this, x2, y, clrBtn_w, RESET_V_SLIDER));
277 add_subwindow(reset = new YUVReset(plugin, this, x, y));
285 void YUVWindow::update_gui(int clear)
288 case RESET_Y_SLIDER : this->y->update(plugin->config.y);
290 case RESET_U_SLIDER : u->update(plugin->config.u);
292 case RESET_V_SLIDER : v->update(plugin->config.v);
296 this->y->update(plugin->config.y);
297 u->update(plugin->config.u);
298 v->update(plugin->config.v);
308 YUVEffect::YUVEffect(PluginServer *server)
309 : PluginVClient(server)
313 YUVEffect::~YUVEffect()
318 const char* YUVEffect::plugin_title() { return N_("YUV"); }
319 int YUVEffect::is_realtime() { return 1; }
322 NEW_WINDOW_MACRO(YUVEffect, YUVWindow)
323 LOAD_CONFIGURATION_MACRO(YUVEffect, YUVConfig)
325 void YUVEffect::update_gui()
329 thread->window->lock_window();
330 load_configuration();
331 ((YUVWindow*)thread->window)->y->update(config.y);
332 ((YUVWindow*)thread->window)->u->update(config.u);
333 ((YUVWindow*)thread->window)->v->update(config.v);
334 thread->window->unlock_window();
338 void YUVEffect::save_data(KeyFrame *keyframe)
341 output.set_shared_output(keyframe->xbuf);
342 output.tag.set_title("YUV");
343 output.tag.set_property("Y", config.y);
344 output.tag.set_property("U", config.u);
345 output.tag.set_property("V", config.v);
347 output.tag.set_title("/YUV");
349 output.append_newline();
350 output.terminate_string();
353 void YUVEffect::read_data(KeyFrame *keyframe)
356 input.set_shared_input(keyframe->xbuf);
357 while(!input.read_tag())
359 if(input.tag.title_is("YUV"))
361 config.y = input.tag.get_property("Y", config.y);
362 config.u = input.tag.get_property("U", config.u);
363 config.v = input.tag.get_property("V", config.v);
369 #define YUV_MACRO(type, temp_type, max, components, use_yuv) \
371 for(int i = 0; i < input->get_h(); i++) \
373 type *in_row = (type*)input->get_rows()[i]; \
374 type *out_row = (type*)output->get_rows()[i]; \
375 const float round = (sizeof(type) == 4) ? 0.0 : 0.5; \
377 for(int j = 0; j < w; j++) \
381 int y = (int)((float)in_row[0] * y_scale + round); \
382 int u = (int)((float)(in_row[1] - (max / 2 + 1)) * u_scale + round) + (max / 2 + 1); \
383 int v = (int)((float)(in_row[2] - (max / 2 + 1)) * v_scale + round) + (max / 2 + 1); \
384 out_row[0] = CLIP(y, 0, max); \
385 out_row[1] = CLIP(u, 0, max); \
386 out_row[2] = CLIP(v, 0, max); \
390 temp_type y, u, v, r, g, b; \
391 if(sizeof(type) == 4) \
393 YUV::yuv.rgb_to_yuv_f(in_row[0], in_row[1], in_row[2], y, u, v); \
396 if(sizeof(type) == 2) \
398 YUV::yuv.rgb_to_yuv_16(in_row[0], in_row[1], in_row[2], y, u, v); \
402 YUV::yuv.rgb_to_yuv_8(in_row[0], in_row[1], in_row[2], y, u, v); \
405 if(sizeof(type) < 4) \
411 y = temp_type((float)y * y_scale + round); \
412 u = temp_type((float)(u - (max / 2 + 1)) * u_scale + round) + (max / 2 + 1); \
413 v = temp_type((float)(v - (max / 2 + 1)) * v_scale + round) + (max / 2 + 1); \
421 y = temp_type((float)y * y_scale + round); \
422 u = temp_type((float)u * u_scale + round); \
423 v = temp_type((float)v * v_scale + round); \
426 if(sizeof(type) == 4) \
427 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v); \
429 if(sizeof(type) == 2) \
430 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v); \
432 YUV::yuv.yuv_to_rgb_8(r, g, b, y, u, v); \
439 in_row += components; \
440 out_row += components; \
445 int YUVEffect::process_realtime(VFrame *input, VFrame *output)
447 load_configuration();
449 if(EQUIV(config.y, 0) && EQUIV(config.u, 0) && EQUIV(config.v, 0))
451 if(input->get_rows()[0] != output->get_rows()[0])
452 output->copy_from(input);
456 int w = input->get_w();
458 float y_scale = (float)(config.y + MAXVALUE) / MAXVALUE;
459 float u_scale = (float)(config.u + MAXVALUE) / MAXVALUE;
460 float v_scale = (float)(config.v + MAXVALUE) / MAXVALUE;
462 if(u_scale > 1) u_scale = 1 + (u_scale - 1) * 4;
463 if(v_scale > 1) v_scale = 1 + (v_scale - 1) * 4;
465 switch(input->get_color_model())
468 YUV_MACRO(float, float, 1, 3, 0)
472 YUV_MACRO(unsigned char, int, 0xff, 3, 0)
476 YUV_MACRO(unsigned char, int, 0xff, 3, 1)
480 YUV_MACRO(uint16_t, int, 0xffff, 3, 0)
484 YUV_MACRO(uint16_t, int, 0xffff, 3, 1)
488 YUV_MACRO(float, float, 1, 4, 0)
492 YUV_MACRO(unsigned char, int, 0xff, 4, 0)
496 YUV_MACRO(unsigned char, int, 0xff, 4, 1)
499 case BC_RGBA16161616:
500 YUV_MACRO(uint16_t, int, 0xffff, 4, 0)
503 case BC_YUVA16161616:
504 YUV_MACRO(uint16_t, int, 0xffff, 4, 1)