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"
47 void copy_from(YUVConfig &src);
48 int equivalent(YUVConfig &src);
49 void interpolate(YUVConfig &prev,
58 class YUVLevel : public BC_FSlider
61 YUVLevel(YUVEffect *plugin, float *output, int x, int y);
67 class YUVReset : public BC_GenericButton
70 YUVReset(YUVEffect *plugin, YUVWindow *window, int x, int y);
77 class YUVWindow : public PluginClientWindow
80 YUVWindow(YUVEffect *plugin);
81 void create_objects();
90 class YUVEffect : public PluginVClient
93 YUVEffect(PluginServer *server);
97 PLUGIN_CLASS_MEMBERS(YUVConfig)
98 int process_realtime(VFrame *input, VFrame *output);
100 void save_data(KeyFrame *keyframe);
101 void read_data(KeyFrame *keyframe);
109 REGISTER_PLUGIN(YUVEffect)
117 YUVConfig::YUVConfig()
122 void YUVConfig::reset()
129 void YUVConfig::copy_from(YUVConfig &src)
136 int YUVConfig::equivalent(YUVConfig &src)
138 return EQUIV(y, src.y) && EQUIV(u, src.u) && EQUIV(v, src.v);
141 void YUVConfig::interpolate(YUVConfig &prev,
147 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
148 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
150 y = prev.y * prev_scale + next.y * next_scale;
151 u = prev.u * prev_scale + next.u * next_scale;
152 v = prev.v * prev_scale + next.v * next_scale;
162 YUVLevel::YUVLevel(YUVEffect *plugin, float *output, int x, int y)
172 this->plugin = plugin;
173 this->output = output;
176 int YUVLevel::handle_event()
178 *output = get_value();
179 plugin->send_configure_change();
184 YUVReset::YUVReset(YUVEffect *plugin, YUVWindow *window, int x, int y)
185 : BC_GenericButton(x, y, _("Reset"))
187 this->plugin = plugin;
188 this->window = window;
190 YUVReset::~YUVReset()
193 int YUVReset::handle_event()
195 plugin->config.reset();
197 plugin->send_configure_change();
202 YUVWindow::YUVWindow(YUVEffect *plugin)
203 : PluginClientWindow(plugin,
210 this->plugin = plugin;
213 void YUVWindow::create_objects()
215 int x = 10, y = 10, x1 = 40;
216 add_subwindow(new BC_Title(x, y, _("Y:")));
217 add_subwindow(this->y = new YUVLevel(plugin, &plugin->config.y, x1, y));
219 add_subwindow(new BC_Title(x, y, _("U:")));
220 add_subwindow(u = new YUVLevel(plugin, &plugin->config.u, x1, y));
222 add_subwindow(new BC_Title(x, y, _("V:")));
223 add_subwindow(v = new YUVLevel(plugin, &plugin->config.v, x1, y));
225 add_subwindow(reset = new YUVReset(plugin, this, x, y));
233 void YUVWindow::update()
235 this->y->update(plugin->config.y);
236 u->update(plugin->config.u);
237 v->update(plugin->config.v);
245 YUVEffect::YUVEffect(PluginServer *server)
246 : PluginVClient(server)
250 YUVEffect::~YUVEffect()
255 const char* YUVEffect::plugin_title() { return N_("YUV"); }
256 int YUVEffect::is_realtime() { return 1; }
259 NEW_WINDOW_MACRO(YUVEffect, YUVWindow)
260 LOAD_CONFIGURATION_MACRO(YUVEffect, YUVConfig)
262 void YUVEffect::update_gui()
266 thread->window->lock_window();
267 load_configuration();
268 ((YUVWindow*)thread->window)->y->update(config.y);
269 ((YUVWindow*)thread->window)->u->update(config.u);
270 ((YUVWindow*)thread->window)->v->update(config.v);
271 thread->window->unlock_window();
275 void YUVEffect::save_data(KeyFrame *keyframe)
278 output.set_shared_output(keyframe->xbuf);
279 output.tag.set_title("YUV");
280 output.tag.set_property("Y", config.y);
281 output.tag.set_property("U", config.u);
282 output.tag.set_property("V", config.v);
284 output.tag.set_title("/YUV");
286 output.append_newline();
287 output.terminate_string();
290 void YUVEffect::read_data(KeyFrame *keyframe)
293 input.set_shared_input(keyframe->xbuf);
294 while(!input.read_tag())
296 if(input.tag.title_is("YUV"))
298 config.y = input.tag.get_property("Y", config.y);
299 config.u = input.tag.get_property("U", config.u);
300 config.v = input.tag.get_property("V", config.v);
306 #define YUV_MACRO(type, temp_type, max, components, use_yuv) \
308 for(int i = 0; i < input->get_h(); i++) \
310 type *in_row = (type*)input->get_rows()[i]; \
311 type *out_row = (type*)output->get_rows()[i]; \
312 const float round = (sizeof(type) == 4) ? 0.0 : 0.5; \
314 for(int j = 0; j < w; j++) \
318 int y = (int)((float)in_row[0] * y_scale + round); \
319 int u = (int)((float)(in_row[1] - (max / 2 + 1)) * u_scale + round) + (max / 2 + 1); \
320 int v = (int)((float)(in_row[2] - (max / 2 + 1)) * v_scale + round) + (max / 2 + 1); \
321 out_row[0] = CLIP(y, 0, max); \
322 out_row[1] = CLIP(u, 0, max); \
323 out_row[2] = CLIP(v, 0, max); \
327 temp_type y, u, v, r, g, b; \
328 if(sizeof(type) == 4) \
330 YUV::yuv.rgb_to_yuv_f(in_row[0], in_row[1], in_row[2], y, u, v); \
333 if(sizeof(type) == 2) \
335 YUV::yuv.rgb_to_yuv_16(in_row[0], in_row[1], in_row[2], y, u, v); \
339 YUV::yuv.rgb_to_yuv_8(in_row[0], in_row[1], in_row[2], y, u, v); \
342 if(sizeof(type) < 4) \
348 y = temp_type((float)y * y_scale + round); \
349 u = temp_type((float)(u - (max / 2 + 1)) * u_scale + round) + (max / 2 + 1); \
350 v = temp_type((float)(v - (max / 2 + 1)) * v_scale + round) + (max / 2 + 1); \
358 y = temp_type((float)y * y_scale + round); \
359 u = temp_type((float)u * u_scale + round); \
360 v = temp_type((float)v * v_scale + round); \
363 if(sizeof(type) == 4) \
364 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v); \
366 if(sizeof(type) == 2) \
367 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v); \
369 YUV::yuv.yuv_to_rgb_8(r, g, b, y, u, v); \
376 in_row += components; \
377 out_row += components; \
382 int YUVEffect::process_realtime(VFrame *input, VFrame *output)
384 load_configuration();
386 if(EQUIV(config.y, 0) && EQUIV(config.u, 0) && EQUIV(config.v, 0))
388 if(input->get_rows()[0] != output->get_rows()[0])
389 output->copy_from(input);
393 int w = input->get_w();
395 float y_scale = (float)(config.y + MAXVALUE) / MAXVALUE;
396 float u_scale = (float)(config.u + MAXVALUE) / MAXVALUE;
397 float v_scale = (float)(config.v + MAXVALUE) / MAXVALUE;
399 if(u_scale > 1) u_scale = 1 + (u_scale - 1) * 4;
400 if(v_scale > 1) v_scale = 1 + (v_scale - 1) * 4;
402 switch(input->get_color_model())
405 YUV_MACRO(float, float, 1, 3, 0)
409 YUV_MACRO(unsigned char, int, 0xff, 3, 0)
413 YUV_MACRO(unsigned char, int, 0xff, 3, 1)
417 YUV_MACRO(uint16_t, int, 0xffff, 3, 0)
421 YUV_MACRO(uint16_t, int, 0xffff, 3, 1)
425 YUV_MACRO(float, float, 1, 4, 0)
429 YUV_MACRO(unsigned char, int, 0xff, 4, 0)
433 YUV_MACRO(unsigned char, int, 0xff, 4, 1)
436 case BC_RGBA16161616:
437 YUV_MACRO(uint16_t, int, 0xffff, 4, 0)
440 case BC_YUVA16161616:
441 YUV_MACRO(uint16_t, int, 0xffff, 4, 1)