edl plugin names eng, fix segv for opengl brender, renderfarm rework strategy, perf...
[goodguy/history.git] / cinelerra-5.1 / plugins / diffkey / diffkey.C
index 8cf242622927e393265705f54f3a3ff0ac130bec..ff8502f0cb8d3a4ad9c5caa386e8dd7d53e96988 100644 (file)
@@ -2,26 +2,27 @@
 /*
  * CINELERRA
  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
- * 
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- * 
+ *
  */
 
 #ifndef DIFFKEY_H
 #define DIFFKEY_H
 
+#include "bccolors.h"
 #include "bcdisplayinfo.h"
 #include "clip.h"
 #include "bchash.h"
@@ -29,9 +30,9 @@
 #include "guicast.h"
 #include "language.h"
 #include "loadbalance.h"
-#include "cicolors.h"
+#include "bccolors.h"
 #include "pluginvclient.h"
-
+#include "playback3d.h"
 
 #include <string.h>
 
@@ -48,10 +49,10 @@ public:
        DiffKeyConfig();
        void copy_from(DiffKeyConfig &src);
        int equivalent(DiffKeyConfig &src);
-       void interpolate(DiffKeyConfig &prev, 
-               DiffKeyConfig &next, 
-               int64_t prev_frame, 
-               int64_t next_frame, 
+       void interpolate(DiffKeyConfig &prev,
+               DiffKeyConfig &next,
+               int64_t prev_frame,
+               int64_t next_frame,
                int64_t current_frame);
 
        float threshold;
@@ -193,10 +194,10 @@ int DiffKeyConfig::equivalent(DiffKeyConfig &src)
                do_value == src.do_value;
 }
 
-void DiffKeyConfig::interpolate(DiffKeyConfig &prev, 
-       DiffKeyConfig &next, 
-       int64_t prev_frame, 
-       int64_t next_frame, 
+void DiffKeyConfig::interpolate(DiffKeyConfig &prev,
+       DiffKeyConfig &next,
+       int64_t prev_frame,
+       int64_t next_frame,
        int64_t current_frame)
 {
        double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
@@ -313,20 +314,20 @@ void DiffKeyGUI::create_objects()
 DiffKey::DiffKey(PluginServer *server)
  : PluginVClient(server)
 {
-       
+
        engine = 0;
 }
 
 DiffKey::~DiffKey()
 {
-       
+
        delete engine;
 }
 
 NEW_WINDOW_MACRO(DiffKey, DiffKeyGUI)
 LOAD_CONFIGURATION_MACRO(DiffKey, DiffKeyConfig)
 
-const char* DiffKey::plugin_title() { return _("Difference key"); }
+const char* DiffKey::plugin_title() { return N_("Difference key"); }
 int DiffKey::is_realtime() { return 1; }
 int DiffKey::is_multichannel() { return 1; }
 
@@ -385,25 +386,25 @@ int DiffKey::process_buffer(VFrame **frame,
        load_configuration();
 
 // Don't process if only 1 layer.
-       if(get_total_buffers() < 2) 
+       if(get_total_buffers() < 2)
        {
-               read_frame(frame[0], 
-                       0, 
-                       start_position, 
+               read_frame(frame[0],
+                       0,
+                       start_position,
                        frame_rate,
                        get_use_opengl());
                return 0;
        }
 
 // Read frames from 2 layers
-       read_frame(frame[0], 
-               0, 
-               start_position, 
+       read_frame(frame[0],
+               0,
+               start_position,
                frame_rate,
                get_use_opengl());
-       read_frame(frame[1], 
-               1, 
-               start_position, 
+       read_frame(frame[1],
+               1,
+               start_position,
                frame_rate,
                get_use_opengl());
 
@@ -432,7 +433,7 @@ int DiffKey::process_buffer(VFrame **frame,
 int DiffKey::handle_opengl()
 {
 #ifdef HAVE_GL
-       static const char *diffkey_head = 
+       static const char *diffkey_head =
                "uniform sampler2D tex_bg;\n"
                "uniform sampler2D tex_fg;\n"
                "uniform float threshold;\n"
@@ -443,17 +444,18 @@ int DiffKey::handle_opengl()
                "       vec4 foreground = texture2D(tex_fg, gl_TexCoord[0].st);\n"
                "       vec4 background = texture2D(tex_bg, gl_TexCoord[0].st);\n";
 
-       static const char *colorcube = 
+       static const char *colorcube =
                "       float difference = length(foreground.rgb - background.rgb);\n";
 
-       static const char *yuv_value = 
+       static const char *yuv_value =
                "       float difference = abs(foreground.r - background.r);\n";
 
-       static const char *rgb_value = 
-               "       float difference = abs(dot(foreground.rgb, vec3(0.29900, 0.58700, 0.11400)) - \n"
-               "                                               dot(background.rgb, vec3(0.29900, 0.58700, 0.11400)));\n";
+       static const char *rgb_value =
+               "       float difference = abs("
+               "               dot(foreground.rgb, rgb_to_y_vector) - "
+               "               dot(background.rgb, rgb_to_y_vector));\n";
 
-       static const char *diffkey_tail = 
+       static const char *diffkey_tail =
                "       vec4 result;\n"
                "       if(difference < threshold)\n"
                "               result.a = 0.0;\n"
@@ -466,59 +468,42 @@ int DiffKey::handle_opengl()
                "       gl_FragColor = result;\n"
                "}\n";
 
-
-
-
-
-       top_frame->enable_opengl();
-       top_frame->init_screen();
-
        top_frame->to_texture();
        bottom_frame->to_texture();
 
        top_frame->enable_opengl();
        top_frame->init_screen();
 
-       unsigned int shader_id = 0;
-       if(config.do_value)
-       {
-               if(BC_CModels::is_yuv(top_frame->get_color_model()))
-                       shader_id = VFrame::make_shader(0, 
-                               diffkey_head,
-                               yuv_value,
-                               diffkey_tail,
-                               0);
-               else
-                       shader_id = VFrame::make_shader(0, 
-                               diffkey_head,
-                               rgb_value,
-                               diffkey_tail,
-                               0);
-       }
-       else
-       {
-                       shader_id = VFrame::make_shader(0, 
-                               diffkey_head,
-                               colorcube,
-                               diffkey_tail,
-                               0);
-       }
-
-
-
+       const char *shader_stack[16];
+       memset(shader_stack,0, sizeof(shader_stack));
+       int current_shader = 0;
+
+       int need_rgb_to_y = 0;
+       const char *shader_frag = !config.do_value ? colorcube :
+               BC_CModels::is_yuv(top_frame->get_color_model()) ?
+                       yuv_value : (need_rgb_to_y = 1, rgb_value);
+       if( need_rgb_to_y )
+               shader_stack[current_shader++] = bc_gl_rgb_to_y;
+       shader_stack[current_shader++] = diffkey_head;
+       shader_stack[current_shader++] = shader_frag;
+       shader_stack[current_shader++] = diffkey_tail;
+
+       shader_stack[current_shader] = 0;
+       unsigned int shader = VFrame::make_shader(shader_stack);
        DIFFKEY_VARS(this)
 
        bottom_frame->bind_texture(1);
        top_frame->bind_texture(0);
 
-       if(shader_id > 0)
-       {
-               glUseProgram(shader_id);
-               glUniform1i(glGetUniformLocation(shader_id, "tex_fg"), 0);
-               glUniform1i(glGetUniformLocation(shader_id, "tex_bg"), 1);
-               glUniform1f(glGetUniformLocation(shader_id, "threshold"), threshold);
-               glUniform1f(glGetUniformLocation(shader_id, "pad"), pad);
-               glUniform1f(glGetUniformLocation(shader_id, "threshold_pad"), threshold_pad);
+       if( shader > 0 ) {
+               glUseProgram(shader);
+               glUniform1i(glGetUniformLocation(shader, "tex_fg"), 0);
+               glUniform1i(glGetUniformLocation(shader, "tex_bg"), 1);
+               glUniform1f(glGetUniformLocation(shader, "threshold"), threshold);
+               glUniform1f(glGetUniformLocation(shader, "pad"), pad);
+               glUniform1f(glGetUniformLocation(shader, "threshold_pad"), threshold_pad);
+               if( need_rgb_to_y )
+                       BC_GL_MATRIX(shader, rgb_to_y_vector);
        }
 
        if(BC_CModels::components(get_output()->get_color_model()) == 3)
@@ -531,11 +516,12 @@ int DiffKey::handle_opengl()
        top_frame->draw_texture();
        glUseProgram(0);
        top_frame->set_opengl_state(VFrame::SCREEN);
-// Fastest way to discard output
-       bottom_frame->set_opengl_state(VFrame::TEXTURE);
        glDisable(GL_BLEND);
+// Fastest way to discard output
+       bottom_frame->set_opengl_state(VFrame::UNKNOWN);
 
-
+// kludge ahead
+       top_frame->screen_to_ram();
 #endif
        return 0;
 }
@@ -543,7 +529,7 @@ int DiffKey::handle_opengl()
 
 
 
-DiffKeyEngine::DiffKeyEngine(DiffKey *plugin) 
+DiffKeyEngine::DiffKeyEngine(DiffKey *plugin)
  : LoadServer(plugin->get_project_smp() + 1, plugin->get_project_smp() + 1)
 {
        this->plugin = plugin;
@@ -557,7 +543,7 @@ void DiffKeyEngine::init_packages()
        {
                DiffKeyPackage *pkg = (DiffKeyPackage*)get_package(i);
                pkg->row1 = y;
-               pkg->row2 = MIN(y + increment, plugin->top_frame->get_h()); 
+               pkg->row2 = MIN(y + increment, plugin->top_frame->get_h());
                y  += increment;
        }
 }
@@ -598,8 +584,7 @@ void DiffKeyClient::process_package(LoadPackage *ptr)
        DiffKey *plugin = engine->plugin;
        int w = plugin->top_frame->get_w();
 
-#define RGB_TO_VALUE(r, g, b) \
-((r) * R_TO_Y + (g) * G_TO_Y + (b) * B_TO_Y)
+#define RGB_TO_VALUE(r, g, b) YUV::yuv.rgb_to_y_f((r),(g),(b))
 
 
 #define DIFFKEY_MACRO(type, components, max, chroma_offset) \