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
23 #include "bccmodels.h"
27 #include "rgb601window.h"
33 REGISTER_PLUGIN(RGB601Main)
38 RGB601Config::RGB601Config()
43 RGB601Main::RGB601Main(PluginServer *server)
44 : PluginVClient(server)
49 RGB601Main::~RGB601Main()
54 const char* RGB601Main::plugin_title() { return _("RGB - 601"); }
55 int RGB601Main::is_realtime() { return 1; }
58 NEW_WINDOW_MACRO(RGB601Main, RGB601Window)
61 void RGB601Main::update_gui()
66 thread->window->lock_window();
67 ((RGB601Window*)thread->window)->forward->update(config.direction == 1);
68 ((RGB601Window*)thread->window)->reverse->update(config.direction == 2);
69 thread->window->unlock_window();
74 int RGB601Main::load_configuration()
76 KeyFrame *prev_keyframe;
78 prev_keyframe = get_prev_keyframe(get_source_position());
79 // Must also switch between interpolation between keyframes and using first keyframe
80 read_data(prev_keyframe);
85 void RGB601Main::save_data(KeyFrame *keyframe)
89 // cause data to be stored directly in text
90 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
91 output.tag.set_title("RGB601");
92 output.tag.set_property("DIRECTION", config.direction);
94 output.tag.set_title("/RGB601");
96 output.append_newline();
97 output.terminate_string();
100 void RGB601Main::read_data(KeyFrame *keyframe)
104 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
110 result = input.read_tag();
114 if(input.tag.title_is("RGB601"))
116 config.direction = input.tag.get_property("DIRECTION", config.direction);
123 ((RGB601Window*)thread->window)->update();
128 #define CREATE_TABLE(max) \
130 for(int i = 0; i < max; i++) \
132 int forward_output = (int)((double)0.8588 * i + max * 0.0627 + 0.5); \
133 int reverse_output = (int)((double)1.1644 * i - max * 0.0627 + 0.5); \
134 forward_table[i] = CLIP(forward_output, 0, max - 1); \
135 reverse_table[i] = CLIP(reverse_output, 0, max - 1); \
139 void RGB601Main::create_table(VFrame *input_ptr)
141 switch(input_ptr->get_color_model())
152 case BC_RGBA16161616:
153 case BC_YUVA16161616:
154 CREATE_TABLE(0x10000);
159 #define PROCESS(table, type, components, yuv) \
161 for(int i = 0; i < h; i++) \
163 type *in_row = (type*)input_ptr->get_rows()[i]; \
164 type *out_row = (type*)output_ptr->get_rows()[i]; \
169 for(int j = 0; j < w; j++) \
171 out_row[j * components] = table[(int)in_row[j * components]]; \
172 out_row[j * components + 1] = in_row[j * components + 1]; \
173 out_row[j * components + 2] = in_row[j * components + 2]; \
177 if(sizeof(type) == 4) \
179 for(int j = 0; j < w; j++) \
181 if(table == forward_table) \
183 out_row[j * components] = (type)(in_row[j * components] * 0.8588 + 0.0627); \
184 out_row[j * components + 1] = (type)(in_row[j * components + 1] * 0.8588 + 0.0627); \
185 out_row[j * components + 2] = (type)(in_row[j * components + 2] * 0.8588 + 0.0627); \
189 out_row[j * components] = (type)(in_row[j * components] * 1.1644 - 0.0627); \
190 out_row[j * components + 1] = (type)(in_row[j * components + 1] * 1.1644 - 0.0627); \
191 out_row[j * components + 2] = (type)(in_row[j * components + 2] * 1.1644 - 0.0627); \
197 for(int j = 0; j < w; j++) \
199 out_row[j * components] = table[(int)in_row[j * components]]; \
200 out_row[j * components + 1] = table[(int)in_row[j * components + 1]]; \
201 out_row[j * components + 2] = table[(int)in_row[j * components + 2]]; \
207 void RGB601Main::process(int *table, VFrame *input_ptr, VFrame *output_ptr)
209 int w = input_ptr->get_w();
210 int h = input_ptr->get_h();
212 if(config.direction == 1)
213 switch(input_ptr->get_color_model())
216 PROCESS(forward_table, unsigned char, 3, 1);
219 PROCESS(forward_table, unsigned char, 4, 1);
222 PROCESS(forward_table, u_int16_t, 3, 1);
224 case BC_YUVA16161616:
225 PROCESS(forward_table, u_int16_t, 4, 1);
228 PROCESS(forward_table, unsigned char, 3, 0);
231 PROCESS(forward_table, unsigned char, 4, 0);
234 PROCESS(forward_table, float, 3, 0);
237 PROCESS(forward_table, float, 4, 0);
240 PROCESS(forward_table, u_int16_t, 3, 0);
242 case BC_RGBA16161616:
243 PROCESS(forward_table, u_int16_t, 4, 0);
247 if(config.direction == 2)
248 switch(input_ptr->get_color_model())
251 PROCESS(reverse_table, unsigned char, 3, 1);
254 PROCESS(reverse_table, unsigned char, 4, 1);
257 PROCESS(reverse_table, u_int16_t, 3, 1);
259 case BC_YUVA16161616:
260 PROCESS(reverse_table, u_int16_t, 4, 1);
263 PROCESS(reverse_table, unsigned char, 3, 0);
266 PROCESS(reverse_table, unsigned char, 4, 0);
269 PROCESS(reverse_table, float, 3, 0);
272 PROCESS(reverse_table, float, 4, 0);
275 PROCESS(reverse_table, u_int16_t, 3, 0);
277 case BC_RGBA16161616:
278 PROCESS(reverse_table, u_int16_t, 4, 0);
283 int RGB601Main::process_buffer(VFrame *frame,
284 int64_t start_position,
287 load_configuration();
289 // Set parameters for aggregation with previous or next effect.
290 frame->get_params()->update("RGB601_DIRECTION", config.direction);
298 // Deinterlace effects may aggregate this one,
299 if(get_use_opengl() &&
300 (prev_effect_is(_("Frames to fields")) ||
301 next_effect_is(_("Frames to fields"))))
306 if(get_use_opengl() && config.direction)
315 if(config.direction == 1)
316 process(forward_table, frame, frame);
318 if(config.direction == 2)
319 process(reverse_table, frame, frame);
324 int RGB601Main::handle_opengl()
327 static const char *yuv_fwd_frag =
328 "uniform sampler2D tex;\n"
331 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
332 " gl_FragColor.r = gl_FragColor.r * 0.8588 + 0.0627;\n"
334 static const char *yuv_rev_frag =
335 "uniform sampler2D tex;\n"
338 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
339 " gl_FragColor.r = gl_FragColor.r * 1.1644 - 0.0627;\n"
341 static const char *rgb_fwd_frag =
342 "uniform sampler2D tex;\n"
345 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
346 " gl_FragColor.rgb = gl_FragColor.rgb * vec3(0.8588, 0.8588, 0.8588) + vec3(0.0627, 0.0627, 0.0627);\n"
348 static const char *rgb_rev_frag =
349 "uniform sampler2D tex;\n"
352 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
353 " gl_FragColor.rgb = gl_FragColor.rgb * vec3(1.1644, 1.1644, 1.1644) - vec3(0.0627, 0.0627, 0.0627);\n"
357 get_output()->to_texture();
358 get_output()->enable_opengl();
359 get_output()->bind_texture(0);
361 unsigned int frag_shader = 0;
362 switch(get_output()->get_color_model())
366 frag_shader = VFrame::make_shader(0,
367 config.direction == 1 ? yuv_fwd_frag : yuv_rev_frag,
372 frag_shader = VFrame::make_shader(0,
373 config.direction == 1 ? rgb_fwd_frag : rgb_rev_frag,
380 glUseProgram(frag_shader);
381 glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
383 VFrame::init_screen(get_output()->get_w(), get_output()->get_h());
384 get_output()->draw_texture();
386 get_output()->set_opengl_state(VFrame::SCREEN);