olaf neophyte and de.po updates, valgrind tweaks, delete green lady, inkscape dpi=96
[goodguy/history.git] / cinelerra-5.1 / cinelerra / playback3d.C
index 3dcb00df660075fdbea4983907ae5825aa1d6659..2ca2a4c68e4d43f35f03595d76f23e8a6483d1d4 100644 (file)
 
 #define GL_GLEXT_PROTOTYPES
 
+#include "bccolors.h"
 #include "bcsignals.h"
 #include "bcwindowbase.h"
 #include "canvas.h"
 #include "clip.h"
 #include "condition.h"
+#include "edl.h"
 #include "maskautos.h"
 #include "maskauto.h"
 #include "mutex.h"
+#include "mwindow.h"
 #include "overlayframe.inc"
 #include "overlayframe.h"
 #include "playback3d.h"
 #include "pluginclient.h"
 #include "pluginvclient.h"
+#include "edlsession.h"
 #include "transportque.inc"
 #include "vframe.h"
 
 #ifdef HAVE_GL
 static const char *yuv_to_rgb_frag =
        "uniform sampler2D tex;\n"
+       "uniform mat3 yuv_to_rgb_matrix;\n"
+       "uniform float yminf;\n"
        "void main()\n"
        "{\n"
        "       vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
-       "       yuva.rgb -= vec3(0, 0.5, 0.5);\n"
-       "       const mat3 yuv_to_rgb_matrix = mat3(\n"
-       "               1,       1,        1, \n"
-       "               0,       -0.34414, 1.77200, \n"
-       "               1.40200, -0.71414, 0);\n"
+       "       yuva.rgb -= vec3(yminf, 0.5, 0.5);\n"
        "       gl_FragColor = vec4(yuv_to_rgb_matrix * yuva.rgb, yuva.a);\n"
        "}\n";
 
@@ -86,14 +88,12 @@ static const char *yuva_to_yuv_frag =
 
 static const char *yuva_to_rgb_frag =
        "uniform sampler2D tex;\n"
+       "uniform mat3 yuv_to_rgb_matrix;\n"
+       "uniform float yminf;\n"
        "void main()\n"
        "{\n"
        "       vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
-       "       yuva.rgb -= vec3(0, 0.5, 0.5);\n"
-       "       const mat3 yuv_to_rgb_matrix = mat3(\n"
-       "               1,       1,        1, \n"
-       "               0,       -0.34414, 1.77200, \n"
-       "               1.40200, -0.71414, 0);\n"
+       "       yuva.rgb -= vec3(yminf, 0.5, 0.5);\n"
        "       yuva.rgb = yuv_to_rgb_matrix * yuva.rgb;\n"
        "       yuva.rgb *= yuva.a;\n"
        "       yuva.a = 1.0;\n"
@@ -102,15 +102,13 @@ static const char *yuva_to_rgb_frag =
 
 static const char *rgb_to_yuv_frag =
        "uniform sampler2D tex;\n"
+       "uniform mat3 rgb_to_yuv_matrix;\n"
+       "uniform float yminf;\n"
        "void main()\n"
        "{\n"
        "       vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
-       "       const mat3 rgb_to_yuv_matrix = mat3(\n"
-       "               0.29900, -0.16874, 0.50000, \n"
-       "               0.58700, -0.33126, -0.41869, \n"
-       "               0.11400, 0.50000,  -0.08131);\n"
        "       rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
-       "       rgba.rgb += vec3(0, 0.5, 0.5);\n"
+       "       rgba.rgb += vec3(yminf, 0.5, 0.5);\n"
        "       gl_FragColor = rgba;\n"
        "}\n";
 
@@ -127,25 +125,23 @@ static const char *rgba_to_rgb_frag =
 
 static const char *rgba_to_yuv_frag =
        "uniform sampler2D tex;\n"
+       "uniform mat3 rgb_to_yuv_matrix;\n"
+       "uniform float yminf;\n"
        "void main()\n"
        "{\n"
        "       vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
-       "       const mat3 rgb_to_yuv_matrix = mat3(\n"
-       "               0.29900, -0.16874, 0.50000, \n"
-       "               0.58700, -0.33126, -0.41869, \n"
-       "               0.11400, 0.50000,  -0.08131);\n"
        "       rgba.rgb *= rgba.a;\n"
        "       rgba.a = 1.0;\n"
        "       rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
-       "       rgba.rgb += vec3(0, 0.5, 0.5);\n"
+       "       rgba.rgb += vec3(yminf, 0.5, 0.5);\n"
        "       gl_FragColor = rgba;\n"
        "}\n";
 
-static const char *rgba_to_rgb_flatten =
-       "void main() {\n"
-       "       gl_FragColor.rgb *= gl_FragColor.a;\n"
-       "       gl_FragColor.a = 1.0;\n"
-       "}\n";
+//static const char *rgba_to_rgb_flatten =
+//     "void main() {\n"
+//     "       gl_FragColor.rgb *= gl_FragColor.a;\n"
+//     "       gl_FragColor.a = 1.0;\n"
+//     "}\n";
 
 #define GL_STD_BLEND(FN) \
 static const char *blend_##FN##_frag = \
@@ -505,12 +501,23 @@ void Playback3D::copy_from_sync(Playback3DCommand *command)
                        else
 // Copy to RAM
                        {
-                               command->input->enable_opengl();
-                               glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-                               glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE,
-                                       command->frame->get_rows()[0]);
-                               command->frame->flip_vert();
-                               command->frame->set_opengl_state(VFrame::RAM);
+                               command->input->to_texture();
+                               command->input->bind_texture(0);
+                               command->frame->enable_opengl();
+                               command->frame->init_screen();
+                               unsigned int shader = BC_CModels::is_yuv(command->input->get_color_model()) ?
+                                       VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
+                               if( shader > 0 ) {
+                                       glUseProgram(shader);
+                                       int variable = glGetUniformLocation(shader, "tex");
+                                       glUniform1i(variable, 0);
+                                       BC_GL_YUV_TO_RGB(shader);
+                               }
+                               else
+                                       glUseProgram(0);
+                               command->input->draw_texture(1);
+                               command->frame->screen_to_ram();
+                               glUseProgram(0);
                        }
                }
                else
@@ -641,27 +648,31 @@ void Playback3D::write_buffer_sync(Playback3DCommand *command)
 // Make sure OpenGL is enabled first.
                window->enable_opengl();
 
-
 //printf("Playback3D::write_buffer_sync 1 %d\n", window->get_id());
-               switch(command->frame->get_opengl_state())
-               {
+               int flip_y = 0, frame_state = command->frame->get_opengl_state();
+               switch( frame_state ) {
 // Upload texture and composite to screen
                        case VFrame::RAM:
+                               flip_y = 1;
+                       case VFrame::SCREEN:
                                command->frame->to_texture();
-                               draw_output(command);
-                               break;
+                               window->enable_opengl();
 // Composite texture to screen and swap buffer
                        case VFrame::TEXTURE:
-                               draw_output(command);
-                               break;
-                       case VFrame::SCREEN:
-// swap buffers only
-                               window->flip_opengl();
+                               if( !flip_y ) {
+                                       int fh1 = command->frame->get_h()-1;
+                                       float in_y1 = fh1 - command->in_y1;
+                                       float in_y2 = fh1 - command->in_y2;
+                                       command->in_y1 = in_y2;
+                                       command->in_y2 = in_y1;
+                               }
+                               draw_output(command, flip_y);
                                break;
                        default:
                                printf("Playback3D::write_buffer_sync unknown state\n");
                                break;
                }
+               command->frame->set_opengl_state(frame_state);
                window->unlock_window();
        }
 
@@ -670,7 +681,7 @@ void Playback3D::write_buffer_sync(Playback3DCommand *command)
 
 
 
-void Playback3D::draw_output(Playback3DCommand *command)
+void Playback3D::draw_output(Playback3DCommand *command, int flip_y)
 {
 #ifdef HAVE_GL
        int texture_id = command->frame->get_texture_id();
@@ -690,54 +701,42 @@ void Playback3D::draw_output(Playback3DCommand *command)
                canvas_w = window->get_w();
                canvas_h = window->get_h();
                VFrame::init_screen(canvas_w, canvas_h);
+               int color_model = command->frame->get_color_model();
+               int is_yuv = BC_CModels::is_yuv(color_model);
 
                if(!command->is_cleared)
                {
 // If we get here, the virtual console was not used.
-                       init_frame(command);
+                       init_frame(command, 0);
                }
 
 // Texture
 // Undo any previous shader settings
                command->frame->bind_texture(0);
 
-
-
-
 // Convert colormodel
-               unsigned int frag_shader = 0;
-               switch(command->frame->get_color_model())
-               {
-                       case BC_YUV888:
-                       case BC_YUVA8888:
-                               frag_shader = VFrame::make_shader(0,
-                                       yuv_to_rgb_frag,
-                                       0);
-                               break;
-               }
-
-
-               if(frag_shader > 0)
-               {
-                       glUseProgram(frag_shader);
-                       int variable = glGetUniformLocation(frag_shader, "tex");
+               unsigned int shader = is_yuv ? VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
+               if( shader > 0 ) {
+                       glUseProgram(shader);
 // Set texture unit of the texture
+                       int variable = glGetUniformLocation(shader, "tex");
                        glUniform1i(variable, 0);
+                       BC_GL_YUV_TO_RGB(shader);
                }
 
-               if(BC_CModels::components(command->frame->get_color_model()) == 4)
-               {
-                       glEnable(GL_BLEND);
-                       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-               }
+//             if(BC_CModels::components(color_model) == 4)
+//             {
+//                     glEnable(GL_BLEND);
+//                     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+//             }
 
                command->frame->draw_texture(
                        command->in_x1, command->in_y1, command->in_x2, command->in_y2,
                        command->out_x1, command->out_y1, command->out_x2, command->out_y2,
-                       1);
+                       flip_y);
 
 
-// printf("Playback3D::draw_output 2 %f,%f %f,%f -> %f,%f %f,%f\n",
+//printf("Playback3D::draw_output 2 %f,%f %f,%f -> %f,%f %f,%f\n",
 // command->in_x1,
 // command->in_y1,
 // command->in_x2,
@@ -756,13 +755,11 @@ void Playback3D::draw_output(Playback3DCommand *command)
 }
 
 
-void Playback3D::init_frame(Playback3DCommand *command)
+void Playback3D::init_frame(Playback3DCommand *command, int is_yuv)
 {
 #ifdef HAVE_GL
-       canvas_w = command->canvas->get_canvas()->get_w();
-       canvas_h = command->canvas->get_canvas()->get_h();
-
-       glClearColor(0.0, 0.0, 0.0, 0.0);
+       float gbuv = is_yuv ? 0.5 : 0.0;
+       glClearColor(0.0, gbuv, gbuv, 0.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 #endif
 }
@@ -785,15 +782,17 @@ void Playback3D::clear_output_sync(Playback3DCommand *command)
                command->canvas->get_canvas()->lock_window("Playback3D::clear_output_sync");
 // If we get here, the virtual console is being used.
                command->canvas->get_canvas()->enable_opengl();
+               int is_yuv = 0;
 
 // Using pbuffer for refresh frame.
                if(command->frame)
                {
                        command->frame->enable_opengl();
+                       int color_model = command->canvas->mwindow->edl->session->color_model;
+                       is_yuv = BC_CModels::is_yuv(color_model);
                }
 
-
-               init_frame(command);
+               init_frame(command, is_yuv);
                command->canvas->get_canvas()->unlock_window();
        }
        command->canvas->unlock_canvas();
@@ -968,22 +967,9 @@ void Playback3D::overlay_sync(Playback3DCommand *command)
                glColor4f(1, 1, 1, 1);
                glDisable(GL_BLEND);
 
-               if(command->frame) {
-// Render to PBuffer
-                       command->frame->enable_opengl();
-                       command->frame->set_opengl_state(VFrame::SCREEN);
-                       canvas_w = command->frame->get_w();
-                       canvas_h = command->frame->get_h();
-               }
-               else {
-// Render to canvas
-                       canvas_w = window->get_w();
-                       canvas_h = window->get_h();
-               }
-
 
 //printf("Playback3D::overlay_sync 1 %d\n", command->input->get_opengl_state());
-               switch(command->input->get_opengl_state()) {
+               switch( command->input->get_opengl_state() ) {
 // Upload texture and composite to screen
                case VFrame::RAM:
                        command->input->to_texture();
@@ -995,19 +981,30 @@ void Playback3D::overlay_sync(Playback3DCommand *command)
                case VFrame::SCREEN:
                        command->input->enable_opengl();
                        command->input->screen_to_texture();
-                       if(command->frame)
-                               command->frame->enable_opengl();
-                       else
-                               window->enable_opengl();
                        break;
                default:
                        printf("Playback3D::overlay_sync unknown state\n");
                        break;
                }
 
+               if(command->frame) {
+// Render to PBuffer
+                       command->frame->enable_opengl();
+                       command->frame->set_opengl_state(VFrame::SCREEN);
+                       canvas_w = command->frame->get_w();
+                       canvas_h = command->frame->get_h();
+               }
+               else {
+// Render to canvas
+                       window->enable_opengl();
+                       canvas_w = window->get_w();
+                       canvas_h = window->get_h();
+               }
+
 
-               const char *shader_stack[4] = { 0, 0, 0, 0, };
-               int total_shaders = 0;
+               const char *shader_stack[16];
+               memset(shader_stack,0, sizeof(shader_stack));
+               int total_shaders = 0, need_matrix = 0;
 
                VFrame::init_screen(canvas_w, canvas_h);
 
@@ -1016,14 +1013,11 @@ void Playback3D::overlay_sync(Playback3DCommand *command)
 
 // Convert colormodel to RGB if not nested.
 // The color model setting in the output frame is ignored.
-               if( command->is_nested <= 0 ) {  // not nested
-                       switch(command->input->get_color_model()) {
-                       case BC_YUV888:
-                       case BC_YUVA8888:
-                               shader_stack[total_shaders++] = yuv_to_rgb_frag;
-                               break;
-                       }
-               }
+//             if( command->is_nested <= 0 &&  // not nested
+//                 BC_CModels::is_yuv(command->input->get_color_model()) ) {
+//                     need_matrix = 1;
+//                     shader_stack[total_shaders++] = yuv_to_rgb_frag;
+//             }
 
 // get the shaders
 #define add_shader(s) \
@@ -1043,36 +1037,34 @@ void Playback3D::overlay_sync(Playback3DCommand *command)
                }
 
 // if to flatten alpha
-               if( command->is_nested < 0 ) {
-                       switch(command->input->get_color_model()) {
-// yuv has already been converted to rgb
-                       case BC_YUVA8888:
-                       case BC_RGBA_FLOAT:
-                       case BC_RGBA8888:
-                               add_shader(rgba_to_rgb_flatten);
-                               break;
-                       }
-               }
+//             if( command->is_nested < 0 ) {
+//                     switch(command->input->get_color_model()) {
+//// yuv has already been converted to rgb
+//                     case BC_YUVA8888:
+//                     case BC_RGBA_FLOAT:
+//                     case BC_RGBA8888:
+//                             add_shader(rgba_to_rgb_flatten);
+//                             break;
+//                     }
+//             }
 
 // run the shaders
-               unsigned int frag_shader = 0;
-               if(shader_stack[0]) {
-                       frag_shader = VFrame::make_shader(0,
-                               shader_stack[0], shader_stack[1],
-                               shader_stack[2], shader_stack[3], 0);
-
-                       glUseProgram(frag_shader);
-
+               add_shader(0);
+               unsigned int shader = !shader_stack[0] ? 0 :
+                       VFrame::make_shader(shader_stack);
+               if( shader > 0 ) {
+                       glUseProgram(shader);
+                       if( need_matrix ) BC_GL_YUV_TO_RGB(shader);
 // Set texture unit of the texture
-                       glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
+                       glUniform1i(glGetUniformLocation(shader, "tex"), 0);
 // Set texture unit of the temp texture
-                       glUniform1i(glGetUniformLocation(frag_shader, "tex2"), 1);
+                       glUniform1i(glGetUniformLocation(shader, "tex2"), 1);
 // Set alpha
-                       int variable = glGetUniformLocation(frag_shader, "alpha");
+                       int variable = glGetUniformLocation(shader, "alpha");
                        glUniform1f(variable, command->alpha);
 // Set dimensions of the temp texture
                        if(temp_texture)
-                               glUniform2f(glGetUniformLocation(frag_shader, "tex2_dimensions"),
+                               glUniform2f(glGetUniformLocation(shader, "tex2_dimensions"),
                                        (float)temp_texture->get_texture_w(),
                                        (float)temp_texture->get_texture_h());
                }
@@ -1080,15 +1072,14 @@ void Playback3D::overlay_sync(Playback3DCommand *command)
                        glUseProgram(0);
 
 
-// printf("Playback3D::overlay_sync %f %f %f %f %f %f %f %f\n",
+//printf("Playback3D::overlay_sync %f %f %f %f %f %f %f %f\n",
 // command->in_x1, command->in_y1, command->in_x2, command->in_y2,
 // command->out_x1, command->out_y1, command->out_x2, command->out_y2);
 
                command->input->draw_texture(
                        command->in_x1, command->in_y1, command->in_x2, command->in_y2,
                        command->out_x1, command->out_y1, command->out_x2, command->out_y2,
-// Don't flip vertical if nested
-                       command->is_nested > 0 ? 0 : 1);
+                       !command->is_nested);
                glUseProgram(0);
 
 // Delete temp texture
@@ -1260,6 +1251,7 @@ void Playback3D::do_mask_sync(Playback3DCommand *command)
 // Need to tabulate every vertex in persistent memory because
 // gluTessVertex doesn't copy them.
                        ArrayList<GLdouble*> coords;
+                       coords.set_array_delete();
                        for(int i = 0; i < points->total; i++)
                        {
                                MaskPoint *point1 = points->values[i];
@@ -1398,27 +1390,19 @@ void Playback3D::do_mask_sync(Playback3DCommand *command)
 // For unfeathered masks, we could use a stencil buffer
 // for further optimization but we also need a YUV algorithm.
                unsigned int frag_shader = 0;
-               switch(temp_texture->get_texture_components())
-               {
-                       case 3:
-                               if(command->frame->get_color_model() == BC_YUV888)
-                                       frag_shader = VFrame::make_shader(0,
-                                               multiply_yuvmask3_frag,
-                                               0);
-                               else
-                                       frag_shader = VFrame::make_shader(0,
-                                               multiply_mask3_frag,
-                                               0);
-                               break;
-                       case 4:
-                               frag_shader = VFrame::make_shader(0,
-                                       multiply_mask4_frag,
-                                       0);
-                               break;
+               switch(temp_texture->get_texture_components()) {
+               case 3:
+                       frag_shader = VFrame::make_shader(0,
+                               command->frame->get_color_model() == BC_YUV888 ?
+                                       multiply_yuvmask3_frag : multiply_mask3_frag,
+                               0);
+                       break;
+               case 4:
+                       frag_shader = VFrame::make_shader(0, multiply_mask4_frag, 0);
+                       break;
                }
 
-               if(frag_shader)
-               {
+               if( frag_shader ) {
                        int variable;
                        glUseProgram(frag_shader);
                        if((variable = glGetUniformLocation(frag_shader, "tex")) >= 0)
@@ -1501,8 +1485,7 @@ void Playback3D::convert_cmodel_sync(Playback3DCommand *command)
 #ifdef HAVE_GL
        command->canvas->lock_canvas("Playback3D::convert_cmodel_sync");
 
-       if(command->canvas->get_canvas())
-       {
+       if( command->canvas->get_canvas() ) {
                BC_WindowBase *window = command->canvas->get_canvas();
                window->lock_window("Playback3D::convert_cmodel_sync");
                window->enable_opengl();
@@ -1513,47 +1496,45 @@ void Playback3D::convert_cmodel_sync(Playback3DCommand *command)
                command->frame->to_texture();
 
 // Colormodel permutation
-               const char *shader = 0;
                int src_cmodel = command->frame->get_color_model();
                int dst_cmodel = command->dst_cmodel;
-               typedef struct
-               {
-                       int src;
-                       int dst;
+               typedef struct {
+                       int src, dst, typ;
                        const char *shader;
                } cmodel_shader_table_t;
-               static cmodel_shader_table_t cmodel_shader_table[]  =
-               {
-                       { BC_RGB888, BC_YUV888, rgb_to_yuv_frag },
-                       { BC_RGB888, BC_YUVA8888, rgb_to_yuv_frag },
-                       { BC_RGBA8888, BC_RGB888, rgba_to_rgb_frag },
-                       { BC_RGBA8888, BC_RGB_FLOAT, rgba_to_rgb_frag },
-                       { BC_RGBA8888, BC_YUV888, rgba_to_yuv_frag },
-                       { BC_RGBA8888, BC_YUVA8888, rgb_to_yuv_frag },
-                       { BC_RGB_FLOAT, BC_YUV888, rgb_to_yuv_frag },
-                       { BC_RGB_FLOAT, BC_YUVA8888, rgb_to_yuv_frag },
-                       { BC_RGBA_FLOAT, BC_RGB888, rgba_to_rgb_frag },
-                       { BC_RGBA_FLOAT, BC_RGB_FLOAT, rgba_to_rgb_frag },
-                       { BC_RGBA_FLOAT, BC_YUV888, rgba_to_yuv_frag },
-                       { BC_RGBA_FLOAT, BC_YUVA8888, rgb_to_yuv_frag },
-                       { BC_YUV888, BC_RGB888, yuv_to_rgb_frag },
-                       { BC_YUV888, BC_RGBA8888, yuv_to_rgb_frag },
-                       { BC_YUV888, BC_RGB_FLOAT, yuv_to_rgb_frag },
-                       { BC_YUV888, BC_RGBA_FLOAT, yuv_to_rgb_frag },
-                       { BC_YUVA8888, BC_RGB888, yuva_to_rgb_frag },
-                       { BC_YUVA8888, BC_RGBA8888, yuv_to_rgb_frag },
-                       { BC_YUVA8888, BC_RGB_FLOAT, yuva_to_rgb_frag },
-                       { BC_YUVA8888, BC_RGBA_FLOAT, yuv_to_rgb_frag },
-                       { BC_YUVA8888, BC_YUV888, yuva_to_yuv_frag },
+               enum { rgb_to_rgb, rgb_to_yuv, yuv_to_rgb, yuv_to_yuv, };
+               int type = -1;
+               static cmodel_shader_table_t cmodel_shader_table[]  = {
+                       { BC_RGB888,    BC_YUV888,      rgb_to_yuv, rgb_to_yuv_frag  },
+                       { BC_RGB888,    BC_YUVA8888,    rgb_to_yuv, rgb_to_yuv_frag  },
+                       { BC_RGBA8888,  BC_RGB888,      rgb_to_rgb, rgba_to_rgb_frag },
+                       { BC_RGBA8888,  BC_RGB_FLOAT,   rgb_to_rgb, rgba_to_rgb_frag },
+                       { BC_RGBA8888,  BC_YUV888,      rgb_to_yuv, rgba_to_yuv_frag },
+                       { BC_RGBA8888,  BC_YUVA8888,    rgb_to_yuv, rgb_to_yuv_frag  },
+                       { BC_RGB_FLOAT, BC_YUV888,      rgb_to_yuv, rgb_to_yuv_frag  },
+                       { BC_RGB_FLOAT, BC_YUVA8888,    rgb_to_yuv, rgb_to_yuv_frag  },
+                       { BC_RGBA_FLOAT,BC_RGB888,      rgb_to_rgb, rgba_to_rgb_frag },
+                       { BC_RGBA_FLOAT,BC_RGB_FLOAT,   rgb_to_rgb, rgba_to_rgb_frag },
+                       { BC_RGBA_FLOAT,BC_YUV888,      rgb_to_yuv, rgba_to_yuv_frag },
+                       { BC_RGBA_FLOAT,BC_YUVA8888,    rgb_to_yuv, rgb_to_yuv_frag  },
+                       { BC_YUV888,    BC_RGB888,      yuv_to_rgb, yuv_to_rgb_frag  },
+                       { BC_YUV888,    BC_RGBA8888,    yuv_to_rgb, yuv_to_rgb_frag  },
+                       { BC_YUV888,    BC_RGB_FLOAT,   yuv_to_rgb, yuv_to_rgb_frag  },
+                       { BC_YUV888,    BC_RGBA_FLOAT,  yuv_to_rgb, yuv_to_rgb_frag  },
+                       { BC_YUVA8888,  BC_RGB888,      yuv_to_rgb, yuva_to_rgb_frag },
+                       { BC_YUVA8888,  BC_RGBA8888,    yuv_to_rgb, yuv_to_rgb_frag  },
+                       { BC_YUVA8888,  BC_RGB_FLOAT,   yuv_to_rgb, yuva_to_rgb_frag },
+                       { BC_YUVA8888,  BC_RGBA_FLOAT,  yuv_to_rgb, yuv_to_rgb_frag  },
+                       { BC_YUVA8888,  BC_YUV888,      yuv_to_yuv, yuva_to_yuv_frag },
                };
 
+               const char *shader = 0;
                int table_size = sizeof(cmodel_shader_table) / sizeof(cmodel_shader_table_t);
-               for(int i = 0; i < table_size; i++)
-               {
-                       if(cmodel_shader_table[i].src == src_cmodel &&
-                               cmodel_shader_table[i].dst == dst_cmodel)
-                       {
+               for( int i=0; i<table_size; ++i ) {
+                       if( cmodel_shader_table[i].src == src_cmodel &&
+                           cmodel_shader_table[i].dst == dst_cmodel ) {
                                shader = cmodel_shader_table[i].shader;
+                               type = cmodel_shader_table[i].typ;
                                break;
                        }
                }
@@ -1564,24 +1545,31 @@ void Playback3D::convert_cmodel_sync(Playback3DCommand *command)
 // command->dst_cmodel,
 // shader);
 
-               if(shader)
-               {
+               const char *shader_stack[9];
+               memset(shader_stack,0, sizeof(shader_stack));
+               int current_shader = 0;
+
+               if( shader ) {
 //printf("Playback3D::convert_cmodel_sync %d\n", __LINE__);
+                       shader_stack[current_shader++] = shader;
+                       shader_stack[current_shader] = 0;
+                       unsigned int shader_id = VFrame::make_shader(shader_stack);
+
                        command->frame->bind_texture(0);
-                       unsigned int shader_id = -1;
-                       if(shader)
-                       {
-                               shader_id = VFrame::make_shader(0,
-                                       shader,
-                                       0);
-                               glUseProgram(shader_id);
-                               glUniform1i(glGetUniformLocation(shader_id, "tex"), 0);
+                       glUseProgram(shader_id);
+
+                       glUniform1i(glGetUniformLocation(shader_id, "tex"), 0);
+                       switch( type ) {
+                       case rgb_to_yuv:
+                               BC_GL_RGB_TO_YUV(shader_id);
+                               break;
+                       case yuv_to_rgb:
+                               BC_GL_YUV_TO_RGB(shader_id);
+                               break;
                        }
 
                        command->frame->draw_texture();
-
                        if(shader) glUseProgram(0);
-
                        command->frame->set_opengl_state(VFrame::SCREEN);
                }
 
@@ -1658,8 +1646,7 @@ void Playback3D::do_fade_sync(Playback3DCommand *command)
                }
 
 
-               if(frag_shader)
-               {
+               if( frag_shader ) {
                        glUseProgram(frag_shader);
                        int variable;
                        if((variable = glGetUniformLocation(frag_shader, "tex")) >= 0)