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"
44 void copy_from(YUVConfig &src);
45 int equivalent(YUVConfig &src);
46 void interpolate(YUVConfig &prev,
55 class YUVLevel : public BC_FSlider
58 YUVLevel(YUVEffect *plugin, float *output, int x, int y);
64 class YUVWindow : public PluginClientWindow
67 YUVWindow(YUVEffect *plugin);
68 void create_objects();
75 class YUVEffect : public PluginVClient
78 YUVEffect(PluginServer *server);
82 PLUGIN_CLASS_MEMBERS(YUVConfig)
83 int process_realtime(VFrame *input, VFrame *output);
85 void save_data(KeyFrame *keyframe);
86 void read_data(KeyFrame *keyframe);
94 REGISTER_PLUGIN(YUVEffect)
102 YUVConfig::YUVConfig()
109 void YUVConfig::copy_from(YUVConfig &src)
116 int YUVConfig::equivalent(YUVConfig &src)
118 return EQUIV(y, src.y) && EQUIV(u, src.u) && EQUIV(v, src.v);
121 void YUVConfig::interpolate(YUVConfig &prev,
127 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
128 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
130 y = prev.y * prev_scale + next.y * next_scale;
131 u = prev.u * prev_scale + next.u * next_scale;
132 v = prev.v * prev_scale + next.v * next_scale;
142 YUVLevel::YUVLevel(YUVEffect *plugin, float *output, int x, int y)
152 this->plugin = plugin;
153 this->output = output;
156 int YUVLevel::handle_event()
158 *output = get_value();
159 plugin->send_configure_change();
164 YUVWindow::YUVWindow(YUVEffect *plugin)
165 : PluginClientWindow(plugin,
172 this->plugin = plugin;
175 void YUVWindow::create_objects()
177 int x = 10, y = 10, x1 = 50;
178 add_subwindow(new BC_Title(x, y, _("Y:")));
179 add_subwindow(this->y = new YUVLevel(plugin, &plugin->config.y, x1, y));
181 add_subwindow(new BC_Title(x, y, _("U:")));
182 add_subwindow(u = new YUVLevel(plugin, &plugin->config.u, x1, y));
184 add_subwindow(new BC_Title(x, y, _("V:")));
185 add_subwindow(v = new YUVLevel(plugin, &plugin->config.v, x1, y));
199 YUVEffect::YUVEffect(PluginServer *server)
200 : PluginVClient(server)
204 YUVEffect::~YUVEffect()
209 const char* YUVEffect::plugin_title() { return _("YUV"); }
210 int YUVEffect::is_realtime() { return 1; }
213 NEW_WINDOW_MACRO(YUVEffect, YUVWindow)
214 LOAD_CONFIGURATION_MACRO(YUVEffect, YUVConfig)
216 void YUVEffect::update_gui()
220 thread->window->lock_window();
221 load_configuration();
222 ((YUVWindow*)thread->window)->y->update(config.y);
223 ((YUVWindow*)thread->window)->u->update(config.u);
224 ((YUVWindow*)thread->window)->v->update(config.v);
225 thread->window->unlock_window();
229 void YUVEffect::save_data(KeyFrame *keyframe)
232 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
233 output.tag.set_title("YUV");
234 output.tag.set_property("Y", config.y);
235 output.tag.set_property("U", config.u);
236 output.tag.set_property("V", config.v);
238 output.tag.set_title("/YUV");
240 output.append_newline();
241 output.terminate_string();
244 void YUVEffect::read_data(KeyFrame *keyframe)
247 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
248 while(!input.read_tag())
250 if(input.tag.title_is("YUV"))
252 config.y = input.tag.get_property("Y", config.y);
253 config.u = input.tag.get_property("U", config.u);
254 config.v = input.tag.get_property("V", config.v);
260 static YUV yuv_static;
262 #define YUV_MACRO(type, temp_type, max, components, use_yuv) \
264 for(int i = 0; i < input->get_h(); i++) \
266 type *in_row = (type*)input->get_rows()[i]; \
267 type *out_row = (type*)output->get_rows()[i]; \
268 const float round = (sizeof(type) == 4) ? 0.0 : 0.5; \
270 for(int j = 0; j < w; j++) \
274 int y = (int)((float)in_row[0] * y_scale + round); \
275 int u = (int)((float)(in_row[1] - (max / 2 + 1)) * u_scale + round) + (max / 2 + 1); \
276 int v = (int)((float)(in_row[2] - (max / 2 + 1)) * v_scale + round) + (max / 2 + 1); \
277 out_row[0] = CLIP(y, 0, max); \
278 out_row[1] = CLIP(u, 0, max); \
279 out_row[2] = CLIP(v, 0, max); \
283 temp_type y, u, v, r, g, b; \
284 if(sizeof(type) == 4) \
286 yuv_static.rgb_to_yuv_f(in_row[0], in_row[1], in_row[2], y, u, v); \
289 if(sizeof(type) == 2) \
291 yuv_static.rgb_to_yuv_16(in_row[0], in_row[1], in_row[2], y, u, v); \
295 yuv_static.rgb_to_yuv_8(in_row[0], in_row[1], in_row[2], y, u, v); \
298 if(sizeof(type) < 4) \
304 y = temp_type((float)y * y_scale + round); \
305 u = temp_type((float)(u - (max / 2 + 1)) * u_scale + round) + (max / 2 + 1); \
306 v = temp_type((float)(v - (max / 2 + 1)) * v_scale + round) + (max / 2 + 1); \
310 y = temp_type((float)y * y_scale + round); \
311 u = temp_type((float)u * u_scale + round); \
312 v = temp_type((float)v * v_scale + round); \
315 if(sizeof(type) == 4) \
316 yuv_static.yuv_to_rgb_f(r, g, b, y, u, v); \
318 if(sizeof(type) == 2) \
319 yuv_static.yuv_to_rgb_16(r, g, b, y, u, v); \
321 yuv_static.yuv_to_rgb_8(r, g, b, y, u, v); \
328 in_row += components; \
329 out_row += components; \
334 int YUVEffect::process_realtime(VFrame *input, VFrame *output)
336 load_configuration();
338 if(EQUIV(config.y, 0) && EQUIV(config.u, 0) && EQUIV(config.v, 0))
340 if(input->get_rows()[0] != output->get_rows()[0])
341 output->copy_from(input);
345 int w = input->get_w();
347 float y_scale = (float)(config.y + MAXVALUE) / MAXVALUE;
348 float u_scale = (float)(config.u + MAXVALUE) / MAXVALUE;
349 float v_scale = (float)(config.v + MAXVALUE) / MAXVALUE;
351 if(u_scale > 1) u_scale = 1 + (u_scale - 1) * 4;
352 if(v_scale > 1) v_scale = 1 + (v_scale - 1) * 4;
354 switch(input->get_color_model())
357 YUV_MACRO(float, float, 1, 3, 0)
361 YUV_MACRO(unsigned char, int, 0xff, 3, 0)
365 YUV_MACRO(unsigned char, int, 0xff, 3, 1)
369 YUV_MACRO(uint16_t, int, 0xffff, 3, 0)
373 YUV_MACRO(uint16_t, int, 0xffff, 3, 1)
377 YUV_MACRO(float, float, 1, 4, 0)
381 YUV_MACRO(unsigned char, int, 0xff, 4, 0)
385 YUV_MACRO(unsigned char, int, 0xff, 4, 1)
388 case BC_RGBA16161616:
389 YUV_MACRO(uint16_t, int, 0xffff, 4, 0)
392 case BC_YUVA16161616:
393 YUV_MACRO(uint16_t, int, 0xffff, 4, 1)