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"
37 #define RESET_Y_SLIDER 1
38 #define RESET_U_SLIDER 2
39 #define RESET_V_SLIDER 3
51 void reset(int clear);
53 void copy_from(YUVConfig &src);
54 int equivalent(YUVConfig &src);
55 void interpolate(YUVConfig &prev,
64 class YUVLevel : public BC_FSlider
67 YUVLevel(YUVEffect *plugin, float *output, int x, int y);
73 class YUVReset : public BC_GenericButton
76 YUVReset(YUVEffect *plugin, YUVWindow *window, int x, int y);
83 class YUVSliderClr : public BC_Button
86 YUVSliderClr(YUVEffect *plugin, YUVWindow *window, int x, int y, int w, int clear);
94 class YUVWindow : public PluginClientWindow
97 YUVWindow(YUVEffect *plugin);
98 void create_objects();
99 void update_gui(int clear);
103 YUVSliderClr *yClr, *uClr, *vClr;
108 class YUVEffect : public PluginVClient
111 YUVEffect(PluginServer *server);
115 PLUGIN_CLASS_MEMBERS(YUVConfig)
116 int process_realtime(VFrame *input, VFrame *output);
118 void save_data(KeyFrame *keyframe);
119 void read_data(KeyFrame *keyframe);
127 REGISTER_PLUGIN(YUVEffect)
135 YUVConfig::YUVConfig()
140 void YUVConfig::reset(int clear)
143 case RESET_Y_SLIDER : y = 0;
145 case RESET_U_SLIDER : u = 0;
147 case RESET_V_SLIDER : v = 0;
156 void YUVConfig::copy_from(YUVConfig &src)
163 int YUVConfig::equivalent(YUVConfig &src)
165 return EQUIV(y, src.y) && EQUIV(u, src.u) && EQUIV(v, src.v);
168 void YUVConfig::interpolate(YUVConfig &prev,
174 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
175 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
177 y = prev.y * prev_scale + next.y * next_scale;
178 u = prev.u * prev_scale + next.u * next_scale;
179 v = prev.v * prev_scale + next.v * next_scale;
189 YUVLevel::YUVLevel(YUVEffect *plugin, float *output, int x, int y)
199 this->plugin = plugin;
200 this->output = output;
203 int YUVLevel::handle_event()
205 *output = get_value();
206 plugin->send_configure_change();
211 YUVReset::YUVReset(YUVEffect *plugin, YUVWindow *window, int x, int y)
212 : BC_GenericButton(x, y, _("Reset"))
214 this->plugin = plugin;
215 this->window = window;
217 YUVReset::~YUVReset()
220 int YUVReset::handle_event()
222 plugin->config.reset(RESET_ALL);
223 window->update_gui(RESET_ALL);
224 plugin->send_configure_change();
229 YUVSliderClr::YUVSliderClr(YUVEffect *plugin, YUVWindow *window, int x, int y, int w, int clear)
230 : BC_Button(x, y, w, plugin->get_theme()->get_image_set("reset_button"))
232 this->plugin = plugin;
233 this->window = window;
236 YUVSliderClr::~YUVSliderClr()
239 int YUVSliderClr::handle_event()
241 // clear==1 ==> Y slider
242 // clear==2 ==> U slider
243 // clear==3 ==> V slider
244 plugin->config.reset(clear);
245 window->update_gui(clear);
246 plugin->send_configure_change();
251 YUVWindow::YUVWindow(YUVEffect *plugin)
252 : PluginClientWindow(plugin, 310, 135, 310, 135, 0)
254 this->plugin = plugin;
257 void YUVWindow::create_objects()
259 int x = 10, y = 10, x1 = 40;
260 int x2 = 0; int clrBtn_w = 50;
262 add_subwindow(new BC_Title(x, y, _("Y:")));
263 add_subwindow(this->y = new YUVLevel(plugin, &plugin->config.y, x1, y));
264 x2 = x1 + this->y->get_w() + 10;
265 add_subwindow(yClr = new YUVSliderClr(plugin, this, x2, y, clrBtn_w, RESET_Y_SLIDER));
268 add_subwindow(new BC_Title(x, y, _("U:")));
269 add_subwindow(u = new YUVLevel(plugin, &plugin->config.u, x1, y));
270 add_subwindow(uClr = new YUVSliderClr(plugin, this, x2, y, clrBtn_w, RESET_U_SLIDER));
273 add_subwindow(new BC_Title(x, y, _("V:")));
274 add_subwindow(v = new YUVLevel(plugin, &plugin->config.v, x1, y));
275 add_subwindow(vClr = new YUVSliderClr(plugin, this, x2, y, clrBtn_w, RESET_V_SLIDER));
278 add_subwindow(reset = new YUVReset(plugin, this, x, y));
286 void YUVWindow::update_gui(int clear)
289 case RESET_Y_SLIDER : this->y->update(plugin->config.y);
291 case RESET_U_SLIDER : u->update(plugin->config.u);
293 case RESET_V_SLIDER : v->update(plugin->config.v);
297 this->y->update(plugin->config.y);
298 u->update(plugin->config.u);
299 v->update(plugin->config.v);
309 YUVEffect::YUVEffect(PluginServer *server)
310 : PluginVClient(server)
314 YUVEffect::~YUVEffect()
319 const char* YUVEffect::plugin_title() { return N_("YUV"); }
320 int YUVEffect::is_realtime() { return 1; }
323 NEW_WINDOW_MACRO(YUVEffect, YUVWindow)
324 LOAD_CONFIGURATION_MACRO(YUVEffect, YUVConfig)
326 void YUVEffect::update_gui()
330 thread->window->lock_window();
331 load_configuration();
332 ((YUVWindow*)thread->window)->y->update(config.y);
333 ((YUVWindow*)thread->window)->u->update(config.u);
334 ((YUVWindow*)thread->window)->v->update(config.v);
335 thread->window->unlock_window();
339 void YUVEffect::save_data(KeyFrame *keyframe)
342 output.set_shared_output(keyframe->xbuf);
343 output.tag.set_title("YUV");
344 output.tag.set_property("Y", config.y);
345 output.tag.set_property("U", config.u);
346 output.tag.set_property("V", config.v);
348 output.tag.set_title("/YUV");
350 output.append_newline();
351 output.terminate_string();
354 void YUVEffect::read_data(KeyFrame *keyframe)
357 input.set_shared_input(keyframe->xbuf);
358 while(!input.read_tag())
360 if(input.tag.title_is("YUV"))
362 config.y = input.tag.get_property("Y", config.y);
363 config.u = input.tag.get_property("U", config.u);
364 config.v = input.tag.get_property("V", config.v);
370 #define YUV_MACRO(type, temp_type, max, components, use_yuv) \
372 for(int i = 0; i < input->get_h(); i++) \
374 type *in_row = (type*)input->get_rows()[i]; \
375 type *out_row = (type*)output->get_rows()[i]; \
376 const float round = (sizeof(type) == 4) ? 0.0 : 0.5; \
378 for(int j = 0; j < w; j++) \
382 int y = (int)((float)in_row[0] * y_scale + round); \
383 int u = (int)((float)(in_row[1] - (max / 2 + 1)) * u_scale + round) + (max / 2 + 1); \
384 int v = (int)((float)(in_row[2] - (max / 2 + 1)) * v_scale + round) + (max / 2 + 1); \
385 out_row[0] = CLIP(y, 0, max); \
386 out_row[1] = CLIP(u, 0, max); \
387 out_row[2] = CLIP(v, 0, max); \
391 temp_type y, u, v, r, g, b; \
392 if(sizeof(type) == 4) \
394 YUV::yuv.rgb_to_yuv_f(in_row[0], in_row[1], in_row[2], y, u, v); \
397 if(sizeof(type) == 2) \
399 YUV::yuv.rgb_to_yuv_16(in_row[0], in_row[1], in_row[2], y, u, v); \
403 YUV::yuv.rgb_to_yuv_8(in_row[0], in_row[1], in_row[2], y, u, v); \
406 if(sizeof(type) < 4) \
412 y = temp_type((float)y * y_scale + round); \
413 u = temp_type((float)(u - (max / 2 + 1)) * u_scale + round) + (max / 2 + 1); \
414 v = temp_type((float)(v - (max / 2 + 1)) * v_scale + round) + (max / 2 + 1); \
422 y = temp_type((float)y * y_scale + round); \
423 u = temp_type((float)u * u_scale + round); \
424 v = temp_type((float)v * v_scale + round); \
427 if(sizeof(type) == 4) \
428 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v); \
430 if(sizeof(type) == 2) \
431 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v); \
433 YUV::yuv.yuv_to_rgb_8(r, g, b, y, u, v); \
440 in_row += components; \
441 out_row += components; \
446 int YUVEffect::process_realtime(VFrame *input, VFrame *output)
448 load_configuration();
450 if(EQUIV(config.y, 0) && EQUIV(config.u, 0) && EQUIV(config.v, 0))
452 if(input->get_rows()[0] != output->get_rows()[0])
453 output->copy_from(input);
457 int w = input->get_w();
459 float y_scale = (float)(config.y + MAXVALUE) / MAXVALUE;
460 float u_scale = (float)(config.u + MAXVALUE) / MAXVALUE;
461 float v_scale = (float)(config.v + MAXVALUE) / MAXVALUE;
463 if(u_scale > 1) u_scale = 1 + (u_scale - 1) * 4;
464 if(v_scale > 1) v_scale = 1 + (v_scale - 1) * 4;
466 switch(input->get_color_model())
469 YUV_MACRO(float, float, 1, 3, 0)
473 YUV_MACRO(unsigned char, int, 0xff, 3, 0)
477 YUV_MACRO(unsigned char, int, 0xff, 3, 1)
481 YUV_MACRO(uint16_t, int, 0xffff, 3, 0)
485 YUV_MACRO(uint16_t, int, 0xffff, 3, 1)
489 YUV_MACRO(float, float, 1, 4, 0)
493 YUV_MACRO(unsigned char, int, 0xff, 4, 0)
497 YUV_MACRO(unsigned char, int, 0xff, 4, 1)
500 case BC_RGBA16161616:
501 YUV_MACRO(uint16_t, int, 0xffff, 4, 0)
504 case BC_YUVA16161616:
505 YUV_MACRO(uint16_t, int, 0xffff, 4, 1)