no /. in ffmpeg init_decode segv, build index fix audio wave, stop playback state...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / playback3d.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #define GL_GLEXT_PROTOTYPES
23
24 #include "bccolors.h"
25 #include "bcsignals.h"
26 #include "bcwindowbase.h"
27 #include "canvas.h"
28 #include "clip.h"
29 #include "condition.h"
30 #include "edl.h"
31 #include "maskautos.h"
32 #include "maskauto.h"
33 #include "mutex.h"
34 #include "mwindow.h"
35 #include "overlayframe.inc"
36 #include "overlayframe.h"
37 #include "playback3d.h"
38 #include "pluginclient.h"
39 #include "pluginvclient.h"
40 #include "edlsession.h"
41 #include "transportque.inc"
42 #include "vframe.h"
43
44 #ifdef HAVE_GL
45 #include <GL/gl.h>
46 #include <GL/glext.h>
47 #include <GL/glu.h>
48 #endif
49
50 #include <string.h>
51 #include <unistd.h>
52 #include <fcntl.h>
53
54 #define QQ(q)#q
55 #define SS(s)QQ(s)
56
57
58 // Shaders
59 // These should be passed to VFrame::make_shader to construct shaders.
60 // Can't hard code sampler2D
61
62
63 #ifdef HAVE_GL
64 static const char *yuv_to_rgb_frag =
65         "uniform sampler2D tex;\n"
66         "uniform mat3 yuv_to_rgb_matrix;\n"
67         "uniform float yminf;\n"
68         "void main()\n"
69         "{\n"
70         "       vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
71         "       yuva.rgb -= vec3(yminf, 0.5, 0.5);\n"
72         "       gl_FragColor = vec4(yuv_to_rgb_matrix * yuva.rgb, yuva.a);\n"
73         "}\n";
74
75 static const char *yuva_to_yuv_frag =
76         "uniform sampler2D tex;\n"
77         "void main()\n"
78         "{\n"
79         "       vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
80         "       float a = yuva.a;\n"
81         "       float anti_a = 1.0 - a;\n"
82         "       yuva.r *= a;\n"
83         "       yuva.g = yuva.g * a + 0.5 * anti_a;\n"
84         "       yuva.b = yuva.b * a + 0.5 * anti_a;\n"
85         "       yuva.a = 1.0;\n"
86         "       gl_FragColor = yuva;\n"
87         "}\n";
88
89 static const char *yuva_to_rgb_frag =
90         "uniform sampler2D tex;\n"
91         "uniform mat3 yuv_to_rgb_matrix;\n"
92         "uniform float yminf;\n"
93         "void main()\n"
94         "{\n"
95         "       vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
96         "       yuva.rgb -= vec3(yminf, 0.5, 0.5);\n"
97         "       yuva.rgb = yuv_to_rgb_matrix * yuva.rgb;\n"
98         "       yuva.rgb *= yuva.a;\n"
99         "       yuva.a = 1.0;\n"
100         "       gl_FragColor = yuva;\n"
101         "}\n";
102
103 static const char *rgb_to_yuv_frag =
104         "uniform sampler2D tex;\n"
105         "uniform mat3 rgb_to_yuv_matrix;\n"
106         "uniform float yminf;\n"
107         "void main()\n"
108         "{\n"
109         "       vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
110         "       rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
111         "       rgba.rgb += vec3(yminf, 0.5, 0.5);\n"
112         "       gl_FragColor = rgba;\n"
113         "}\n";
114
115
116 static const char *rgba_to_rgb_frag =
117         "uniform sampler2D tex;\n"
118         "void main()\n"
119         "{\n"
120         "       vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
121         "       rgba.rgb *= rgba.a;\n"
122         "       rgba.a = 1.0;\n"
123         "       gl_FragColor = rgba;\n"
124         "}\n";
125
126 static const char *rgba_to_yuv_frag =
127         "uniform sampler2D tex;\n"
128         "uniform mat3 rgb_to_yuv_matrix;\n"
129         "uniform float yminf;\n"
130         "void main()\n"
131         "{\n"
132         "       vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
133         "       rgba.rgb *= rgba.a;\n"
134         "       rgba.a = 1.0;\n"
135         "       rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
136         "       rgba.rgb += vec3(yminf, 0.5, 0.5);\n"
137         "       gl_FragColor = rgba;\n"
138         "}\n";
139
140 //static const char *rgba_to_rgb_flatten =
141 //      "void main() {\n"
142 //      "       gl_FragColor.rgb *= gl_FragColor.a;\n"
143 //      "       gl_FragColor.a = 1.0;\n"
144 //      "}\n";
145
146 #define GL_STD_BLEND(FN) \
147 static const char *blend_##FN##_frag = \
148         "uniform sampler2D tex2;\n" \
149         "uniform vec2 tex2_dimensions;\n" \
150         "uniform float alpha;\n" \
151         "void main() {\n" \
152         "       vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n" \
153         "       vec4 result;\n" \
154         "       result.rgb = " SS(COLOR_##FN(1.0, gl_FragColor.rgb, gl_FragColor.a, canvas.rgb, canvas.a)) ";\n" \
155         "       result.a = " SS(ALPHA_##FN(1.0, gl_FragColor.a, canvas.a)) ";\n" \
156         "       gl_FragColor = mix(canvas, result, alpha);\n" \
157         "}\n"
158
159 #define GL_VEC_BLEND(FN) \
160 static const char *blend_##FN##_frag = \
161         "uniform sampler2D tex2;\n" \
162         "uniform vec2 tex2_dimensions;\n" \
163         "uniform float alpha;\n" \
164         "void main() {\n" \
165         "       vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n" \
166         "       vec4 result;\n" \
167         "       result.r = " SS(COLOR_##FN(1.0, gl_FragColor.r, gl_FragColor.a, canvas.r, canvas.a)) ";\n" \
168         "       result.g = " SS(COLOR_##FN(1.0, gl_FragColor.g, gl_FragColor.a, canvas.g, canvas.a)) ";\n" \
169         "       result.b = " SS(COLOR_##FN(1.0, gl_FragColor.b, gl_FragColor.a, canvas.b, canvas.a)) ";\n" \
170         "       result.a = " SS(ALPHA_##FN(1.0, gl_FragColor.a, canvas.a)) ";\n" \
171         "       result = clamp(result, 0.0, 1.0);\n" \
172         "       gl_FragColor = mix(canvas, result, alpha);\n" \
173         "}\n"
174
175 #undef mabs
176 #define mabs abs
177 #undef mmin
178 #define mmin min
179 #undef mmax
180 #define mmax max
181
182 #undef ZERO
183 #define ZERO 0.0
184 #undef ONE
185 #define ONE 1.0
186 #undef TWO
187 #define TWO 2.0
188
189 // NORMAL
190 static const char *blend_NORMAL_frag =
191         "uniform sampler2D tex2;\n"
192         "uniform vec2 tex2_dimensions;\n"
193         "uniform float alpha;\n"
194         "void main() {\n"
195         "       vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
196         "       vec4 result = mix(canvas, gl_FragColor, gl_FragColor.a);\n"
197         "       gl_FragColor = mix(canvas, result, alpha);\n"
198         "}\n";
199
200 // REPLACE
201 static const char *blend_REPLACE_frag =
202         "uniform float alpha;\n"
203         "void main() {\n"
204         "}\n";
205
206 // ADDITION
207 static const char *blend_ADDITION_frag =
208         "uniform sampler2D tex2;\n"
209         "uniform vec2 tex2_dimensions;\n"
210         "uniform float alpha;\n"
211         "void main() {\n"
212         "       vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
213         "       vec4 result = clamp(gl_FragColor + canvas, 0.0, 1.0);\n"
214         "       gl_FragColor = mix(canvas, result, alpha);\n"
215         "}\n";
216
217 // SUBTRACT
218 static const char *blend_SUBTRACT_frag =
219         "uniform sampler2D tex2;\n"
220         "uniform vec2 tex2_dimensions;\n"
221         "uniform float alpha;\n"
222         "void main() {\n"
223         "       vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
224         "       vec4 result = clamp(gl_FragColor - canvas, 0.0, 1.0);\n"
225         "       gl_FragColor = mix(canvas, result, alpha);\n"
226         "}\n";
227
228 GL_STD_BLEND(MULTIPLY);
229 GL_VEC_BLEND(DIVIDE);
230 GL_VEC_BLEND(MAX);
231 GL_VEC_BLEND(MIN);
232 GL_VEC_BLEND(DARKEN);
233 GL_VEC_BLEND(LIGHTEN);
234 GL_STD_BLEND(DST);
235 GL_STD_BLEND(DST_ATOP);
236 GL_STD_BLEND(DST_IN);
237 GL_STD_BLEND(DST_OUT);
238 GL_STD_BLEND(DST_OVER);
239 GL_STD_BLEND(SRC);
240 GL_STD_BLEND(SRC_ATOP);
241 GL_STD_BLEND(SRC_IN);
242 GL_STD_BLEND(SRC_OUT);
243 GL_STD_BLEND(SRC_OVER);
244 GL_STD_BLEND(AND);
245 GL_STD_BLEND(OR);
246 GL_STD_BLEND(XOR);
247 GL_VEC_BLEND(OVERLAY);
248 GL_STD_BLEND(SCREEN);
249 GL_VEC_BLEND(BURN);
250 GL_VEC_BLEND(DODGE);
251 GL_VEC_BLEND(HARDLIGHT);
252 GL_VEC_BLEND(SOFTLIGHT);
253 GL_VEC_BLEND(DIFFERENCE);
254
255 static const char *read_texture_frag =
256         "uniform sampler2D tex;\n"
257         "void main()\n"
258         "{\n"
259         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
260         "}\n";
261
262 static const char *multiply_mask4_frag =
263         "uniform sampler2D tex;\n"
264         "uniform sampler2D tex1;\n"
265         "uniform float scale;\n"
266         "void main()\n"
267         "{\n"
268         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
269         "       gl_FragColor.a *= texture2D(tex1, gl_TexCoord[0].st / vec2(scale, scale)).r;\n"
270         "}\n";
271
272 static const char *multiply_mask3_frag =
273         "uniform sampler2D tex;\n"
274         "uniform sampler2D tex1;\n"
275         "uniform float scale;\n"
276         "uniform bool is_yuv;\n"
277         "void main()\n"
278         "{\n"
279         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
280         "       float a = texture2D(tex1, gl_TexCoord[0].st / vec2(scale, scale)).r;\n"
281         "       gl_FragColor.rgb *= vec3(a, a, a);\n"
282         "}\n";
283
284 static const char *multiply_yuvmask3_frag =
285         "uniform sampler2D tex;\n"
286         "uniform sampler2D tex1;\n"
287         "uniform float scale;\n"
288         "void main()\n"
289         "{\n"
290         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
291         "       float a = texture2D(tex1, gl_TexCoord[0].st / vec2(scale, scale)).r;\n"
292         "       gl_FragColor.gb -= vec2(0.5, 0.5);\n"
293         "       gl_FragColor.rgb *= vec3(a, a, a);\n"
294         "       gl_FragColor.gb += vec2(0.5, 0.5);\n"
295         "}\n";
296
297 static const char *fade_rgba_frag =
298         "uniform sampler2D tex;\n"
299         "uniform float alpha;\n"
300         "void main()\n"
301         "{\n"
302         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
303         "       gl_FragColor.a *= alpha;\n"
304         "}\n";
305
306 static const char *fade_yuv_frag =
307         "uniform sampler2D tex;\n"
308         "uniform float alpha;\n"
309         "void main()\n"
310         "{\n"
311         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
312         "       gl_FragColor.r *= alpha;\n"
313         "       gl_FragColor.gb -= vec2(0.5, 0.5);\n"
314         "       gl_FragColor.g *= alpha;\n"
315         "       gl_FragColor.b *= alpha;\n"
316         "       gl_FragColor.gb += vec2(0.5, 0.5);\n"
317         "}\n";
318
319 #endif
320
321
322 Playback3DCommand::Playback3DCommand()
323  : BC_SynchronousCommand()
324 {
325         canvas = 0;
326         is_nested = 0;
327 }
328
329 void Playback3DCommand::copy_from(BC_SynchronousCommand *command)
330 {
331         Playback3DCommand *ptr = (Playback3DCommand*)command;
332         this->canvas = ptr->canvas;
333         this->is_cleared = ptr->is_cleared;
334
335         this->in_x1 = ptr->in_x1;
336         this->in_y1 = ptr->in_y1;
337         this->in_x2 = ptr->in_x2;
338         this->in_y2 = ptr->in_y2;
339         this->out_x1 = ptr->out_x1;
340         this->out_y1 = ptr->out_y1;
341         this->out_x2 = ptr->out_x2;
342         this->out_y2 = ptr->out_y2;
343         this->alpha = ptr->alpha;
344         this->mode = ptr->mode;
345         this->interpolation_type = ptr->interpolation_type;
346
347         this->input = ptr->input;
348         this->start_position_project = ptr->start_position_project;
349         this->keyframe_set = ptr->keyframe_set;
350         this->keyframe = ptr->keyframe;
351         this->default_auto = ptr->default_auto;
352         this->plugin_client = ptr->plugin_client;
353         this->want_texture = ptr->want_texture;
354         this->is_nested = ptr->is_nested;
355         this->dst_cmodel = ptr->dst_cmodel;
356
357         BC_SynchronousCommand::copy_from(command);
358 }
359
360
361 ///static void glDebugCallback(GLenum src, GLenum typ, GLuint id,
362 ///     GLenum svy, GLsizei len, const GLchar* msg, void* dat)
363 //static void glDebugCallback(unsigned int src, unsigned int typ, unsigned int id,
364 //      unsigned int svy, int len, const char* msg, const void* dat)
365 //{
366 //      printf("glDebug: %d:%d; %d/%d %s\n",src,typ,id,svy,msg);
367 //}
368
369
370 Playback3D::Playback3D(MWindow *mwindow)
371  : BC_Synchronous()
372 {
373         this->mwindow = mwindow;
374         temp_texture = 0;
375         //Enabling OpenGL debug output on nVidia drivers
376 //      glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
377 //      glEnable(GL_DEBUG_OUTPUT);
378 //      glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
379 //      glDebugMessageCallback(glDebugCallback, 0);
380 }
381
382 Playback3D::~Playback3D()
383 {
384 }
385
386
387
388
389 BC_SynchronousCommand* Playback3D::new_command()
390 {
391         return new Playback3DCommand;
392 }
393
394
395
396 void Playback3D::handle_command(BC_SynchronousCommand *command)
397 {
398 //printf("Playback3D::handle_command 1 %d\n", command->command);
399         switch(command->command)
400         {
401                 case Playback3DCommand::WRITE_BUFFER:
402                         write_buffer_sync((Playback3DCommand*)command);
403                         break;
404
405                 case Playback3DCommand::FINISH_OUTPUT:
406                         finish_output_sync((Playback3DCommand*)command);
407                         break;
408
409                 case Playback3DCommand::CLEAR_OUTPUT:
410                         clear_output_sync((Playback3DCommand*)command);
411                         break;
412
413                 case Playback3DCommand::CLEAR_INPUT:
414                         clear_input_sync((Playback3DCommand*)command);
415                         break;
416
417                 case Playback3DCommand::DO_CAMERA:
418                         do_camera_sync((Playback3DCommand*)command);
419                         break;
420
421                 case Playback3DCommand::OVERLAY:
422                         overlay_sync((Playback3DCommand*)command);
423                         break;
424
425                 case Playback3DCommand::DO_FADE:
426                         do_fade_sync((Playback3DCommand*)command);
427                         break;
428
429                 case Playback3DCommand::DO_MASK:
430                         do_mask_sync((Playback3DCommand*)command);
431                         break;
432
433                 case Playback3DCommand::PLUGIN:
434                         run_plugin_sync((Playback3DCommand*)command);
435                         break;
436
437                 case Playback3DCommand::COPY_FROM:
438                         copy_from_sync((Playback3DCommand*)command);
439                         break;
440
441                 case Playback3DCommand::CONVERT_CMODEL:
442                         convert_cmodel_sync((Playback3DCommand*)command);
443                         break;
444
445 //              case Playback3DCommand::DRAW_REFRESH:
446 //                      draw_refresh_sync((Playback3DCommand*)command);
447 //                      break;
448         }
449 //printf("Playback3D::handle_command 10\n");
450 }
451
452
453
454
455 void Playback3D::copy_from(Canvas *canvas,
456         VFrame *dst,
457         VFrame *src,
458         int want_texture)
459 {
460         Playback3DCommand command;
461         command.command = Playback3DCommand::COPY_FROM;
462         command.canvas = canvas;
463         command.frame = dst;
464         command.input = src;
465         command.want_texture = want_texture;
466         send_command(&command);
467 }
468
469 void Playback3D::copy_from_sync(Playback3DCommand *command)
470 {
471 #ifdef HAVE_GL
472         command->canvas->lock_canvas("Playback3D::draw_refresh_sync");
473         BC_WindowBase *window = command->canvas->get_canvas();
474         if(window)
475         {
476                 window->lock_window("Playback3D:draw_refresh_sync");
477                 window->enable_opengl();
478                 int w = command->input->get_w();
479                 int h = command->input->get_h();
480
481                 if(command->input->get_opengl_state() == VFrame::SCREEN &&
482                         w == command->frame->get_w() && h == command->frame->get_h())
483                 {
484 // printf("Playback3D::copy_from_sync 1 %d %d %d %d %d\n",
485 // command->input->get_w(),
486 // command->input->get_h(),
487 // command->frame->get_w(),
488 // command->frame->get_h(),
489 // command->frame->get_color_model());
490 // With NVidia at least,
491                         if(w % 4)
492                         {
493                                 printf("Playback3D::copy_from_sync: w=%d not supported because it is not divisible by 4.\n", w);
494                         }
495                         else
496 // Copy to texture
497                         if(command->want_texture)
498                         {
499 //printf("Playback3D::copy_from_sync 1 dst=%p src=%p\n", command->frame, command->input);
500 // Screen_to_texture requires the source pbuffer enabled.
501                                 command->input->enable_opengl();
502                                 command->frame->screen_to_texture();
503                                 command->frame->set_opengl_state(VFrame::TEXTURE);
504                         }
505                         else
506 // Copy to RAM
507                         {
508                                 command->input->to_texture();
509                                 command->input->bind_texture(0);
510                                 command->frame->enable_opengl();
511                                 command->frame->init_screen();
512                                 unsigned int shader = BC_CModels::is_yuv(command->input->get_color_model()) ?
513                                         VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
514                                 if( shader > 0 ) {
515                                         glUseProgram(shader);
516                                         int variable = glGetUniformLocation(shader, "tex");
517                                         glUniform1i(variable, 0);
518                                         BC_GL_YUV_TO_RGB(shader);
519                                 }
520                                 else
521                                         glUseProgram(0);
522                                 command->input->draw_texture(1);
523                                 command->frame->screen_to_ram();
524                                 glUseProgram(0);
525                         }
526                 }
527                 else
528                 {
529                         printf("Playback3D::copy_from_sync: invalid formats opengl_state=%d %dx%d -> %dx%d\n",
530                                 command->input->get_opengl_state(), w, h,
531                                 command->frame->get_w(), command->frame->get_h());
532                 }
533
534                 window->unlock_window();
535         }
536         command->canvas->unlock_canvas();
537 #endif
538 }
539
540
541
542
543 // void Playback3D::draw_refresh(Canvas *canvas,
544 //      VFrame *frame,
545 //      float in_x1,
546 //      float in_y1,
547 //      float in_x2,
548 //      float in_y2,
549 //      float out_x1,
550 //      float out_y1,
551 //      float out_x2,
552 //      float out_y2)
553 // {
554 //      Playback3DCommand command;
555 //      command.command = Playback3DCommand::DRAW_REFRESH;
556 //      command.canvas = canvas;
557 //      command.frame = frame;
558 //      command.in_x1 = in_x1;
559 //      command.in_y1 = in_y1;
560 //      command.in_x2 = in_x2;
561 //      command.in_y2 = in_y2;
562 //      command.out_x1 = out_x1;
563 //      command.out_y1 = out_y1;
564 //      command.out_x2 = out_x2;
565 //      command.out_y2 = out_y2;
566 //      send_command(&command);
567 // }
568 //
569 // void Playback3D::draw_refresh_sync(Playback3DCommand *command)
570 // {
571 //      command->canvas->lock_canvas("Playback3D::draw_refresh_sync");
572 //      BC_WindowBase *window = command->canvas->get_canvas();
573 //      if(window)
574 //      {
575 //              window->lock_window("Playback3D:draw_refresh_sync");
576 //              window->enable_opengl();
577 //
578 // // Read output pbuffer back to RAM in project colormodel
579 // // RGB 8bit is fastest for OpenGL to read back.
580 //              command->frame->reallocate(0,
581 //                      0,
582 //                      0,
583 //                      0,
584 //                      command->frame->get_w(),
585 //                      command->frame->get_h(),
586 //                      BC_RGB888,
587 //                      -1);
588 //              command->frame->screen_to_ram();
589 //
590 //              window->clear_box(0,
591 //                                              0,
592 //                                              window->get_w(),
593 //                                              window->get_h());
594 //              window->draw_vframe(command->frame,
595 //                                                      (int)command->out_x1,
596 //                                                      (int)command->out_y1,
597 //                                                      (int)(command->out_x2 - command->out_x1),
598 //                                                      (int)(command->out_y2 - command->out_y1),
599 //                                                      (int)command->in_x1,
600 //                                                      (int)command->in_y1,
601 //                                                      (int)(command->in_x2 - command->in_x1),
602 //                                                      (int)(command->in_y2 - command->in_y1),
603 //                                                      0);
604 //
605 //              window->unlock_window();
606 //      }
607 //      command->canvas->unlock_canvas();
608 // }
609
610
611
612
613
614 void Playback3D::write_buffer(Canvas *canvas,
615         VFrame *frame,
616         float in_x1,
617         float in_y1,
618         float in_x2,
619         float in_y2,
620         float out_x1,
621         float out_y1,
622         float out_x2,
623         float out_y2,
624         int is_cleared)
625 {
626         Playback3DCommand command;
627         command.command = Playback3DCommand::WRITE_BUFFER;
628         command.canvas = canvas;
629         command.frame = frame;
630         command.in_x1 = in_x1;
631         command.in_y1 = in_y1;
632         command.in_x2 = in_x2;
633         command.in_y2 = in_y2;
634         command.out_x1 = out_x1;
635         command.out_y1 = out_y1;
636         command.out_x2 = out_x2;
637         command.out_y2 = out_y2;
638         command.is_cleared = is_cleared;
639         send_command(&command);
640 }
641
642
643 void Playback3D::write_buffer_sync(Playback3DCommand *command)
644 {
645         command->canvas->lock_canvas("Playback3D::write_buffer_sync");
646         if(command->canvas->get_canvas())
647         {
648                 BC_WindowBase *window = command->canvas->get_canvas();
649                 window->lock_window("Playback3D::write_buffer_sync");
650 // Update hidden cursor
651                 window->update_video_cursor();
652 // Make sure OpenGL is enabled first.
653                 window->enable_opengl();
654
655 //printf("Playback3D::write_buffer_sync 1 %d\n", window->get_id());
656                 int flip_y = 0, frame_state = command->frame->get_opengl_state();
657                 switch( frame_state ) {
658 // Upload texture and composite to screen
659                         case VFrame::RAM:
660                                 flip_y = 1;
661                         case VFrame::SCREEN:
662                                 command->frame->to_texture();
663                                 window->enable_opengl();
664 // Composite texture to screen and swap buffer
665                         case VFrame::TEXTURE:
666                                 if( !flip_y ) {
667                                         int fh1 = command->frame->get_h()-1;
668                                         float in_y1 = fh1 - command->in_y1;
669                                         float in_y2 = fh1 - command->in_y2;
670                                         command->in_y1 = in_y2;
671                                         command->in_y2 = in_y1;
672                                 }
673                                 draw_output(command, flip_y);
674                                 break;
675                         default:
676                                 printf("Playback3D::write_buffer_sync unknown state\n");
677                                 break;
678                 }
679                 command->frame->set_opengl_state(frame_state);
680                 window->unlock_window();
681         }
682
683         command->canvas->unlock_canvas();
684 }
685
686
687
688 void Playback3D::draw_output(Playback3DCommand *command, int flip_y)
689 {
690 #ifdef HAVE_GL
691         int texture_id = command->frame->get_texture_id();
692         BC_WindowBase *window = command->canvas->get_canvas();
693
694 // printf("Playback3D::draw_output 1 texture_id=%d window=%p\n",
695 // texture_id,
696 // command->canvas->get_canvas());
697
698
699
700
701 // If virtual console is being used, everything in this function has
702 // already been done except the page flip.
703         if(texture_id >= 0)
704         {
705                 canvas_w = window->get_w();
706                 canvas_h = window->get_h();
707                 VFrame::init_screen(canvas_w, canvas_h);
708                 int color_model = command->frame->get_color_model();
709                 int is_yuv = BC_CModels::is_yuv(color_model);
710
711                 if(!command->is_cleared)
712                 {
713 // If we get here, the virtual console was not used.
714                         init_frame(command, 0);
715                 }
716
717 // Texture
718 // Undo any previous shader settings
719                 command->frame->bind_texture(0);
720
721 // Convert colormodel
722                 unsigned int shader = is_yuv ? VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
723                 if( shader > 0 ) {
724                         glUseProgram(shader);
725 // Set texture unit of the texture
726                         int variable = glGetUniformLocation(shader, "tex");
727                         glUniform1i(variable, 0);
728                         BC_GL_YUV_TO_RGB(shader);
729                 }
730
731 //              if(BC_CModels::components(color_model) == 4)
732 //              {
733 //                      glEnable(GL_BLEND);
734 //                      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
735 //              }
736
737                 command->frame->draw_texture(
738                         command->in_x1, command->in_y1, command->in_x2, command->in_y2,
739                         command->out_x1, command->out_y1, command->out_x2, command->out_y2,
740                         flip_y);
741
742
743 //printf("Playback3D::draw_output 2 %f,%f %f,%f -> %f,%f %f,%f\n",
744 // command->in_x1,
745 // command->in_y1,
746 // command->in_x2,
747 // command->in_y2,
748 // command->out_x1,
749 // command->out_y1,
750 // command->out_x2,
751 // command->out_y2);
752
753                 glUseProgram(0);
754
755                 command->canvas->get_canvas()->flip_opengl();
756
757         }
758 #endif
759 }
760
761
762 void Playback3D::init_frame(Playback3DCommand *command, int is_yuv)
763 {
764 #ifdef HAVE_GL
765         float gbuv = is_yuv ? 0.5 : 0.0;
766         glClearColor(0.0, gbuv, gbuv, 0.0);
767         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
768 #endif
769 }
770
771
772 void Playback3D::finish_output()
773 {
774         Playback3DCommand command;
775         command.command = Playback3DCommand::FINISH_OUTPUT;
776         send_command(&command);
777 }
778
779 void Playback3D::finish_output_sync(Playback3DCommand *command)
780 {
781         glFinish();
782 }
783
784
785 void Playback3D::clear_output(Canvas *canvas, VFrame *output)
786 {
787         Playback3DCommand command;
788         command.command = Playback3DCommand::CLEAR_OUTPUT;
789         command.canvas = canvas;
790         command.frame = output;
791         send_command(&command);
792 }
793
794 void Playback3D::clear_output_sync(Playback3DCommand *command)
795 {
796         command->canvas->lock_canvas("Playback3D::clear_output_sync");
797         if(command->canvas->get_canvas())
798         {
799                 command->canvas->get_canvas()->lock_window("Playback3D::clear_output_sync");
800 // If we get here, the virtual console is being used.
801                 command->canvas->get_canvas()->enable_opengl();
802                 int is_yuv = 0;
803
804 // Using pbuffer for refresh frame.
805                 if(command->frame)
806                 {
807                         command->frame->enable_opengl();
808                         int color_model = command->canvas->mwindow->edl->session->color_model;
809                         is_yuv = BC_CModels::is_yuv(color_model);
810                 }
811
812                 init_frame(command, is_yuv);
813                 command->canvas->get_canvas()->unlock_window();
814         }
815         command->canvas->unlock_canvas();
816 }
817
818
819 void Playback3D::clear_input(Canvas *canvas, VFrame *frame)
820 {
821         Playback3DCommand command;
822         command.command = Playback3DCommand::CLEAR_INPUT;
823         command.canvas = canvas;
824         command.frame = frame;
825         send_command(&command);
826 }
827
828 void Playback3D::clear_input_sync(Playback3DCommand *command)
829 {
830         command->canvas->lock_canvas("Playback3D::clear_output_sync");
831         if(command->canvas->get_canvas())
832         {
833                 command->canvas->get_canvas()->lock_window("Playback3D::clear_output_sync");
834                 command->canvas->get_canvas()->enable_opengl();
835                 command->frame->enable_opengl();
836                 command->frame->clear_pbuffer();
837                 command->frame->set_opengl_state(VFrame::SCREEN);
838                 command->canvas->get_canvas()->unlock_window();
839         }
840         command->canvas->unlock_canvas();
841 }
842
843 void Playback3D::do_camera(Canvas *canvas,
844         VFrame *output,
845         VFrame *input,
846         float in_x1,
847         float in_y1,
848         float in_x2,
849         float in_y2,
850         float out_x1,
851         float out_y1,
852         float out_x2,
853         float out_y2)
854 {
855         Playback3DCommand command;
856         command.command = Playback3DCommand::DO_CAMERA;
857         command.canvas = canvas;
858         command.input = input;
859         command.frame = output;
860         command.in_x1 = in_x1;
861         command.in_y1 = in_y1;
862         command.in_x2 = in_x2;
863         command.in_y2 = in_y2;
864         command.out_x1 = out_x1;
865         command.out_y1 = out_y1;
866         command.out_x2 = out_x2;
867         command.out_y2 = out_y2;
868         send_command(&command);
869 }
870
871 void Playback3D::do_camera_sync(Playback3DCommand *command)
872 {
873         command->canvas->lock_canvas("Playback3D::do_camera_sync");
874         if(command->canvas->get_canvas())
875         {
876                 command->canvas->get_canvas()->lock_window("Playback3D::clear_output_sync");
877                 command->canvas->get_canvas()->enable_opengl();
878
879                 command->input->to_texture();
880                 command->frame->enable_opengl();
881                 command->frame->init_screen();
882                 command->frame->clear_pbuffer();
883
884                 command->input->bind_texture(0);
885 // Must call draw_texture in input frame to get the texture coordinates right.
886
887 // printf("Playback3D::do_camera_sync 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
888 // command->in_x1,
889 // command->in_y2,
890 // command->in_x2,
891 // command->in_y1,
892 // command->out_x1,
893 // (float)command->input->get_h() - command->out_y1,
894 // command->out_x2,
895 // (float)command->input->get_h() - command->out_y2);
896                 command->input->draw_texture(
897                         command->in_x1, command->in_y2,
898                         command->in_x2, command->in_y1,
899                         command->out_x1,
900                         (float)command->frame->get_h() - command->out_y1,
901                         command->out_x2,
902                         (float)command->frame->get_h() - command->out_y2);
903
904
905                 command->frame->set_opengl_state(VFrame::SCREEN);
906                 command->frame->screen_to_ram();
907                 command->canvas->get_canvas()->unlock_window();
908         }
909         command->canvas->unlock_canvas();
910 }
911
912 void Playback3D::overlay(Canvas *canvas, VFrame *input,
913         float in_x1, float in_y1, float in_x2, float in_y2,
914         float out_x1, float out_y1, float out_x2, float out_y2,
915         float alpha, int mode, int interpolation_type,
916         VFrame *output, int is_nested)
917 {
918         Playback3DCommand command;
919         command.command = Playback3DCommand::OVERLAY;
920         command.canvas = canvas;
921         command.frame = output;
922         command.input = input;
923         command.in_x1 = in_x1;
924         command.in_y1 = in_y1;
925         command.in_x2 = in_x2;
926         command.in_y2 = in_y2;
927         command.out_x1 = out_x1;
928         command.out_y1 = out_y1;
929         command.out_x2 = out_x2;
930         command.out_y2 = out_y2;
931         command.alpha = alpha;
932         command.mode = mode;
933         command.interpolation_type = interpolation_type;
934         command.is_nested = is_nested;
935         send_command(&command);
936 }
937
938 void Playback3D::overlay_sync(Playback3DCommand *command)
939 {
940 #ifdef HAVE_GL
941 // To do these operations, we need to copy the input buffer to a texture
942 // and blend 2 textures in a shader
943         static const char * const overlay_shaders[TRANSFER_TYPES] = {
944                 blend_NORMAL_frag,      // TRANSFER_NORMAL
945                 blend_ADDITION_frag,    // TRANSFER_ADDITION
946                 blend_SUBTRACT_frag,    // TRANSFER_SUBTRACT
947                 blend_MULTIPLY_frag,    // TRANSFER_MULTIPLY
948                 blend_DIVIDE_frag,      // TRANSFER_DIVIDE
949                 blend_REPLACE_frag,     // TRANSFER_REPLACE
950                 blend_MAX_frag,         // TRANSFER_MAX
951                 blend_MIN_frag,         // TRANSFER_MIN
952                 blend_DARKEN_frag,      // TRANSFER_DARKEN
953                 blend_LIGHTEN_frag,     // TRANSFER_LIGHTEN
954                 blend_DST_frag,         // TRANSFER_DST
955                 blend_DST_ATOP_frag,    // TRANSFER_DST_ATOP
956                 blend_DST_IN_frag,      // TRANSFER_DST_IN
957                 blend_DST_OUT_frag,     // TRANSFER_DST_OUT
958                 blend_DST_OVER_frag,    // TRANSFER_DST_OVER
959                 blend_SRC_frag,         // TRANSFER_SRC
960                 blend_SRC_ATOP_frag,    // TRANSFER_SRC_ATOP
961                 blend_SRC_IN_frag,      // TRANSFER_SRC_IN
962                 blend_SRC_OUT_frag,     // TRANSFER_SRC_OUT
963                 blend_SRC_OVER_frag,    // TRANSFER_SRC_OVER
964                 blend_AND_frag,         // TRANSFER_AND
965                 blend_OR_frag,          // TRANSFER_OR
966                 blend_XOR_frag,         // TRANSFER_XOR
967                 blend_OVERLAY_frag,     // TRANSFER_OVERLAY
968                 blend_SCREEN_frag,      // TRANSFER_SCREEN
969                 blend_BURN_frag,        // TRANSFER_BURN
970                 blend_DODGE_frag,       // TRANSFER_DODGE
971                 blend_HARDLIGHT_frag,   // TRANSFER_HARDLIGHT
972                 blend_SOFTLIGHT_frag,   // TRANSFER_SOFTLIGHT
973                 blend_DIFFERENCE_frag,  // TRANSFER_DIFFERENCE
974         };
975
976         command->canvas->lock_canvas("Playback3D::overlay_sync");
977         if(command->canvas->get_canvas()) {
978                 BC_WindowBase *window = command->canvas->get_canvas();
979                 window->lock_window("Playback3D::overlay_sync");
980 // Make sure OpenGL is enabled first.
981                 window->enable_opengl();
982                 window->update_video_cursor();
983
984                 glColor4f(1, 1, 1, 1);
985                 glDisable(GL_BLEND);
986
987
988 //printf("Playback3D::overlay_sync 1 %d\n", command->input->get_opengl_state());
989                 switch( command->input->get_opengl_state() ) {
990 // Upload texture and composite to screen
991                 case VFrame::RAM:
992                         command->input->to_texture();
993                         break;
994 // Just composite texture to screen
995                 case VFrame::TEXTURE:
996                         break;
997 // read from PBuffer to texture, then composite texture to screen
998                 case VFrame::SCREEN:
999                         command->input->enable_opengl();
1000                         command->input->screen_to_texture();
1001                         break;
1002                 default:
1003                         printf("Playback3D::overlay_sync unknown state\n");
1004                         break;
1005                 }
1006
1007                 if(command->frame) {
1008 // Render to PBuffer
1009                         command->frame->enable_opengl();
1010                         command->frame->set_opengl_state(VFrame::SCREEN);
1011                         canvas_w = command->frame->get_w();
1012                         canvas_h = command->frame->get_h();
1013                 }
1014                 else {
1015 // Render to canvas
1016                         window->enable_opengl();
1017                         canvas_w = window->get_w();
1018                         canvas_h = window->get_h();
1019                 }
1020
1021
1022                 const char *shader_stack[16];
1023                 memset(shader_stack,0, sizeof(shader_stack));
1024                 int total_shaders = 0, need_matrix = 0;
1025
1026                 VFrame::init_screen(canvas_w, canvas_h);
1027
1028 // Enable texture
1029                 command->input->bind_texture(0);
1030
1031 // Convert colormodel to RGB if not nested.
1032 // The color model setting in the output frame is ignored.
1033 //              if( command->is_nested <= 0 &&  // not nested
1034 //                  BC_CModels::is_yuv(command->input->get_color_model()) ) {
1035 //                      need_matrix = 1;
1036 //                      shader_stack[total_shaders++] = yuv_to_rgb_frag;
1037 //              }
1038
1039 // get the shaders
1040 #define add_shader(s) \
1041   if(!total_shaders) shader_stack[total_shaders++] = read_texture_frag; \
1042   shader_stack[total_shaders++] = s
1043
1044                 switch(command->mode) {
1045                 case TRANSFER_REPLACE:
1046 // This requires overlaying an alpha multiplied image on a black screen.
1047                         if( command->input->get_texture_components() != 4 ) break;
1048                         add_shader(overlay_shaders[command->mode]);
1049                         break;
1050                 default:
1051                         enable_overlay_texture(command);
1052                         add_shader(overlay_shaders[command->mode]);
1053                         break;
1054                 }
1055
1056 // if to flatten alpha
1057 //              if( command->is_nested < 0 ) {
1058 //                      switch(command->input->get_color_model()) {
1059 //// yuv has already been converted to rgb
1060 //                      case BC_YUVA8888:
1061 //                      case BC_RGBA_FLOAT:
1062 //                      case BC_RGBA8888:
1063 //                              add_shader(rgba_to_rgb_flatten);
1064 //                              break;
1065 //                      }
1066 //              }
1067
1068 // run the shaders
1069                 add_shader(0);
1070                 unsigned int shader = !shader_stack[0] ? 0 :
1071                         VFrame::make_shader(shader_stack);
1072                 if( shader > 0 ) {
1073                         glUseProgram(shader);
1074                         if( need_matrix ) BC_GL_YUV_TO_RGB(shader);
1075 // Set texture unit of the texture
1076                         glUniform1i(glGetUniformLocation(shader, "tex"), 0);
1077 // Set texture unit of the temp texture
1078                         glUniform1i(glGetUniformLocation(shader, "tex2"), 1);
1079 // Set alpha
1080                         int variable = glGetUniformLocation(shader, "alpha");
1081                         glUniform1f(variable, command->alpha);
1082 // Set dimensions of the temp texture
1083                         if(temp_texture)
1084                                 glUniform2f(glGetUniformLocation(shader, "tex2_dimensions"),
1085                                         (float)temp_texture->get_texture_w(),
1086                                         (float)temp_texture->get_texture_h());
1087                 }
1088                 else
1089                         glUseProgram(0);
1090
1091
1092 //printf("Playback3D::overlay_sync %f %f %f %f %f %f %f %f\n",
1093 // command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1094 // command->out_x1, command->out_y1, command->out_x2, command->out_y2);
1095
1096                 command->input->draw_texture(
1097                         command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1098                         command->out_x1, command->out_y1, command->out_x2, command->out_y2,
1099                         !command->is_nested);
1100                 glUseProgram(0);
1101
1102 // Delete temp texture
1103                 if(temp_texture) {
1104                         delete temp_texture;
1105                         temp_texture = 0;
1106                         glActiveTexture(GL_TEXTURE1);
1107                         glDisable(GL_TEXTURE_2D);
1108                 }
1109                 glActiveTexture(GL_TEXTURE0);
1110                 glDisable(GL_TEXTURE_2D);
1111
1112                 window->unlock_window();
1113         }
1114         command->canvas->unlock_canvas();
1115 #endif
1116 }
1117
1118 void Playback3D::enable_overlay_texture(Playback3DCommand *command)
1119 {
1120 #ifdef HAVE_GL
1121         glDisable(GL_BLEND);
1122
1123         glActiveTexture(GL_TEXTURE1);
1124         BC_Texture::new_texture(&temp_texture, canvas_w, canvas_h,
1125                 command->input->get_color_model());
1126         temp_texture->bind(1);
1127
1128 // Read canvas into texture
1129         glReadBuffer(GL_BACK);
1130         glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, canvas_w, canvas_h);
1131 #endif
1132 }
1133
1134
1135 void Playback3D::do_mask(Canvas *canvas,
1136         VFrame *output,
1137         int64_t start_position_project,
1138         MaskAutos *keyframe_set,
1139         MaskAuto *keyframe,
1140         MaskAuto *default_auto)
1141 {
1142         Playback3DCommand command;
1143         command.command = Playback3DCommand::DO_MASK;
1144         command.canvas = canvas;
1145         command.frame = output;
1146         command.start_position_project = start_position_project;
1147         command.keyframe_set = keyframe_set;
1148         command.keyframe = keyframe;
1149         command.default_auto = default_auto;
1150
1151         send_command(&command);
1152 }
1153
1154
1155
1156 #ifdef HAVE_GL
1157 struct Vertex : ListItem<Vertex>
1158 {
1159         GLdouble c[3];
1160 };
1161 // this list is only used from the main thread, no locking needed
1162 // this must be a list so that pointers to allocated entries remain valid
1163 // when new entries are added
1164 static List<Vertex> *vertex_cache = 0;
1165
1166 static void combine_callback(GLdouble coords[3],
1167         GLdouble *vertex_data[4],
1168         GLfloat weight[4],
1169         GLdouble **dataOut)
1170 {
1171 // can't use malloc here; GLU doesn't delete the memory for us!
1172         Vertex* vertex = vertex_cache->append();
1173         vertex->c[0] = coords[0];
1174         vertex->c[1] = coords[1];
1175         vertex->c[2] = coords[2];
1176 // we don't need to interpolate anything
1177
1178         *dataOut = &vertex->c[0];
1179 }
1180 #endif
1181
1182
1183 void Playback3D::do_mask_sync(Playback3DCommand *command)
1184 {
1185 #ifdef HAVE_GL
1186         command->canvas->lock_canvas("Playback3D::do_mask_sync");
1187         if(command->canvas->get_canvas())
1188         {
1189                 BC_WindowBase *window = command->canvas->get_canvas();
1190                 window->lock_window("Playback3D::do_mask_sync");
1191                 window->enable_opengl();
1192
1193                 switch(command->frame->get_opengl_state())
1194                 {
1195                         case VFrame::RAM:
1196 // Time to upload to the texture
1197                                 command->frame->to_texture();
1198                                 break;
1199
1200                         case VFrame::SCREEN:
1201 // Read back from PBuffer
1202 // Bind context to pbuffer
1203                                 command->frame->enable_opengl();
1204                                 command->frame->screen_to_texture();
1205                                 break;
1206                 }
1207
1208
1209
1210 // Create PBuffer and draw the mask on it
1211                 command->frame->enable_opengl();
1212
1213 // Initialize coordinate system
1214                 int w = command->frame->get_w();
1215                 int h = command->frame->get_h();
1216                 command->frame->init_screen();
1217
1218 // Clear screen
1219                 glDisable(GL_TEXTURE_2D);
1220                 if(command->default_auto->mode == MASK_MULTIPLY_ALPHA)
1221                 {
1222                         glClearColor(0.0, 0.0, 0.0, 0.0);
1223                         glColor4f((float)command->keyframe->value / 100,
1224                                 (float)command->keyframe->value / 100,
1225                                 (float)command->keyframe->value / 100,
1226                                 1.0);
1227                 }
1228                 else
1229                 {
1230                         glClearColor(1.0, 1.0, 1.0, 1.0);
1231                         glColor4f((float)1.0 - (float)command->keyframe->value / 100,
1232                                 (float)1.0 - (float)command->keyframe->value / 100,
1233                                 (float)1.0 - (float)command->keyframe->value / 100,
1234                                 1.0);
1235                 }
1236                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1237
1238
1239 // Draw mask with scaling to simulate feathering
1240                 GLUtesselator *tesselator = gluNewTess();
1241                 gluTessProperty(tesselator, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_ODD);
1242                 gluTessCallback(tesselator, GLU_TESS_VERTEX, (GLvoid (*) ( )) &glVertex3dv);
1243                 gluTessCallback(tesselator, GLU_TESS_BEGIN, (GLvoid (*) ( )) &glBegin);
1244                 gluTessCallback(tesselator, GLU_TESS_END, (GLvoid (*) ( )) &glEnd);
1245                 gluTessCallback(tesselator, GLU_TESS_COMBINE, (GLvoid (*) ( ))&combine_callback);
1246
1247                 vertex_cache = new List<Vertex>;
1248
1249
1250 // Draw every submask as a new polygon
1251                 int total_submasks = command->keyframe_set->total_submasks(
1252                         command->start_position_project,
1253                         PLAY_FORWARD);
1254                 float scale = command->keyframe->feather + 1;
1255                 int display_list = glGenLists(1);
1256                 glNewList(display_list, GL_COMPILE);
1257                 for(int k = 0; k < total_submasks; k++)
1258                 {
1259                         gluTessBeginPolygon(tesselator, NULL);
1260                         gluTessBeginContour(tesselator);
1261                         ArrayList<MaskPoint*> *points = new ArrayList<MaskPoint*>;
1262                         command->keyframe_set->get_points(points,
1263                                 k,
1264                                 command->start_position_project,
1265                                 PLAY_FORWARD);
1266
1267                         int first_point = 0;
1268 // Need to tabulate every vertex in persistent memory because
1269 // gluTessVertex doesn't copy them.
1270                         ArrayList<GLdouble*> coords;
1271                         coords.set_array_delete();
1272                         for(int i = 0; i < points->total; i++)
1273                         {
1274                                 MaskPoint *point1 = points->values[i];
1275                                 MaskPoint *point2 = (i >= points->total - 1) ?
1276                                         points->values[0] :
1277                                         points->values[i + 1];
1278
1279                                 float x, y;
1280                                 int segments = 0;
1281                                 if(point1->control_x2 == 0 &&
1282                                         point1->control_y2 == 0 &&
1283                                         point2->control_x1 == 0 &&
1284                                         point2->control_y1 == 0)
1285                                         segments = 1;
1286
1287                                 float x0 = point1->x;
1288                                 float y0 = point1->y;
1289                                 float x1 = point1->x + point1->control_x2;
1290                                 float y1 = point1->y + point1->control_y2;
1291                                 float x2 = point2->x + point2->control_x1;
1292                                 float y2 = point2->y + point2->control_y1;
1293                                 float x3 = point2->x;
1294                                 float y3 = point2->y;
1295
1296                                 // forward differencing bezier curves implementation taken from GPL code at
1297                                 // http://cvs.sourceforge.net/viewcvs.py/guliverkli/guliverkli/src/subtitles/Rasterizer.cpp?rev=1.3
1298
1299                                 float cx3, cx2, cx1, cx0, cy3, cy2, cy1, cy0;
1300
1301                                 // [-1 +3 -3 +1]
1302                                 // [+3 -6 +3  0]
1303                                 // [-3 +3  0  0]
1304                                 // [+1  0  0  0]
1305
1306                                 cx3 = -  x0 + 3*x1 - 3*x2 + x3;
1307                                 cx2 =  3*x0 - 6*x1 + 3*x2;
1308                                 cx1 = -3*x0 + 3*x1;
1309                                 cx0 =    x0;
1310
1311                                 cy3 = -  y0 + 3*y1 - 3*y2 + y3;
1312                                 cy2 =  3*y0 - 6*y1 + 3*y2;
1313                                 cy1 = -3*y0 + 3*y1;
1314                                 cy0 =    y0;
1315
1316                                 // This equation is from Graphics Gems I.
1317                                 //
1318                                 // The idea is that since we're approximating a cubic curve with lines,
1319                                 // any error we incur is due to the curvature of the line, which we can
1320                                 // estimate by calculating the maximum acceleration of the curve.  For
1321                                 // a cubic, the acceleration (second derivative) is a line, meaning that
1322                                 // the absolute maximum acceleration must occur at either the beginning
1323                                 // (|c2|) or the end (|c2+c3|).  Our bounds here are a little more
1324                                 // conservative than that, but that's okay.
1325                                 if (segments == 0)
1326                                 {
1327                                         float maxaccel1 = fabs(2*cy2) + fabs(6*cy3);
1328                                         float maxaccel2 = fabs(2*cx2) + fabs(6*cx3);
1329
1330                                         float maxaccel = maxaccel1 > maxaccel2 ? maxaccel1 : maxaccel2;
1331                                         float h = 1.0;
1332
1333                                         if(maxaccel > 8.0) h = sqrt((8.0) / maxaccel);
1334                                         segments = int(1/h);
1335                                 }
1336
1337                                 for(int j = 0; j <= segments; j++)
1338                                 {
1339                                         float t = (float)j / segments;
1340                                         x = cx0 + t*(cx1 + t*(cx2 + t*cx3));
1341                                         y = cy0 + t*(cy1 + t*(cy2 + t*cy3));
1342
1343                                         if(j > 0 || first_point)
1344                                         {
1345                                                 GLdouble *coord = new GLdouble[3];
1346                                                 coord[0] = x / scale;
1347                                                 coord[1] = -h + y / scale;
1348                                                 coord[2] = 0;
1349                                                 coords.append(coord);
1350                                                 first_point = 0;
1351                                         }
1352                                 }
1353                         }
1354
1355 // Now that we know the total vertices, send them to GLU
1356                         for(int i = 0; i < coords.total; i++)
1357                                 gluTessVertex(tesselator, coords.values[i], coords.values[i]);
1358
1359                         gluTessEndContour(tesselator);
1360                         gluTessEndPolygon(tesselator);
1361                         points->remove_all_objects();
1362                         delete points;
1363                         coords.remove_all_objects();
1364                 }
1365                 glEndList();
1366                 glCallList(display_list);
1367                 glDeleteLists(display_list, 1);
1368                 gluDeleteTess(tesselator);
1369
1370                 delete vertex_cache;
1371                 vertex_cache = 0;
1372
1373                 glColor4f(1, 1, 1, 1);
1374
1375
1376 // Read mask into temporary texture.
1377 // For feathering, just read the part of the screen after the downscaling.
1378
1379
1380                 float w_scaled = w / scale;
1381                 float h_scaled = h / scale;
1382 // Don't vary the texture size according to scaling because that
1383 // would waste memory.
1384 // This enables and binds the temporary texture.
1385                 glActiveTexture(GL_TEXTURE1);
1386                 BC_Texture::new_texture(&temp_texture,
1387                         w,
1388                         h,
1389                         command->frame->get_color_model());
1390                 temp_texture->bind(1);
1391                 glReadBuffer(GL_BACK);
1392
1393 // Need to add extra size to fill in the bottom right
1394                 glCopyTexSubImage2D(GL_TEXTURE_2D,
1395                         0,
1396                         0,
1397                         0,
1398                         0,
1399                         0,
1400                         (int)MIN(w_scaled + 2, w),
1401                         (int)MIN(h_scaled + 2, h));
1402
1403                 command->frame->bind_texture(0);
1404
1405
1406 // For feathered masks, use a shader to multiply.
1407 // For unfeathered masks, we could use a stencil buffer
1408 // for further optimization but we also need a YUV algorithm.
1409                 unsigned int frag_shader = 0;
1410                 switch(temp_texture->get_texture_components()) {
1411                 case 3:
1412                         frag_shader = VFrame::make_shader(0,
1413                                 command->frame->get_color_model() == BC_YUV888 ?
1414                                         multiply_yuvmask3_frag : multiply_mask3_frag,
1415                                 0);
1416                         break;
1417                 case 4:
1418                         frag_shader = VFrame::make_shader(0, multiply_mask4_frag, 0);
1419                         break;
1420                 }
1421
1422                 if( frag_shader ) {
1423                         int variable;
1424                         glUseProgram(frag_shader);
1425                         if((variable = glGetUniformLocation(frag_shader, "tex")) >= 0)
1426                                 glUniform1i(variable, 0);
1427                         if((variable = glGetUniformLocation(frag_shader, "tex1")) >= 0)
1428                                 glUniform1i(variable, 1);
1429                         if((variable = glGetUniformLocation(frag_shader, "scale")) >= 0)
1430                                 glUniform1f(variable, scale);
1431                 }
1432
1433
1434
1435 // Write texture to PBuffer with multiply and scaling for feather.
1436
1437
1438                 command->frame->draw_texture(0, 0, w, h, 0, 0, w, h);
1439                 command->frame->set_opengl_state(VFrame::SCREEN);
1440
1441
1442 // Disable temp texture
1443                 glUseProgram(0);
1444
1445                 glActiveTexture(GL_TEXTURE1);
1446                 glDisable(GL_TEXTURE_2D);
1447                 delete temp_texture;
1448                 temp_texture = 0;
1449
1450                 glActiveTexture(GL_TEXTURE0);
1451                 glDisable(GL_TEXTURE_2D);
1452
1453 // Default drawable
1454                 window->enable_opengl();
1455                 window->unlock_window();
1456         }
1457         command->canvas->unlock_canvas();
1458 #endif
1459 }
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470 void Playback3D::convert_cmodel(Canvas *canvas,
1471         VFrame *output,
1472         int dst_cmodel)
1473 {
1474 // Do nothing if colormodels are equivalent in OpenGL & the image is in hardware.
1475         int src_cmodel = output->get_color_model();
1476         if(
1477                 (output->get_opengl_state() == VFrame::TEXTURE ||
1478                 output->get_opengl_state() == VFrame::SCREEN) &&
1479 // OpenGL has no floating point.
1480                 ( (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGB_FLOAT) ||
1481                   (src_cmodel == BC_RGBA8888 && dst_cmodel == BC_RGBA_FLOAT) ||
1482                   (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGB888) ||
1483                   (src_cmodel == BC_RGBA_FLOAT && dst_cmodel == BC_RGBA8888) ||
1484 // OpenGL sets alpha to 1 on import
1485                   (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGBA8888) ||
1486                   (src_cmodel == BC_YUV888 && dst_cmodel == BC_YUVA8888) ||
1487                   (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGBA_FLOAT) )
1488                 ) return;
1489
1490
1491
1492         Playback3DCommand command;
1493         command.command = Playback3DCommand::CONVERT_CMODEL;
1494         command.canvas = canvas;
1495         command.frame = output;
1496         command.dst_cmodel = dst_cmodel;
1497         send_command(&command);
1498 }
1499
1500 void Playback3D::convert_cmodel_sync(Playback3DCommand *command)
1501 {
1502 #ifdef HAVE_GL
1503         command->canvas->lock_canvas("Playback3D::convert_cmodel_sync");
1504
1505         if( command->canvas->get_canvas() ) {
1506                 BC_WindowBase *window = command->canvas->get_canvas();
1507                 window->lock_window("Playback3D::convert_cmodel_sync");
1508                 window->enable_opengl();
1509
1510 // Import into hardware
1511                 command->frame->enable_opengl();
1512                 command->frame->init_screen();
1513                 command->frame->to_texture();
1514
1515 // Colormodel permutation
1516                 int src_cmodel = command->frame->get_color_model();
1517                 int dst_cmodel = command->dst_cmodel;
1518                 typedef struct {
1519                         int src, dst, typ;
1520                         const char *shader;
1521                 } cmodel_shader_table_t;
1522                 enum { rgb_to_rgb, rgb_to_yuv, yuv_to_rgb, yuv_to_yuv, };
1523                 int type = -1;
1524                 static cmodel_shader_table_t cmodel_shader_table[]  = {
1525                         { BC_RGB888,    BC_YUV888,      rgb_to_yuv, rgb_to_yuv_frag  },
1526                         { BC_RGB888,    BC_YUVA8888,    rgb_to_yuv, rgb_to_yuv_frag  },
1527                         { BC_RGBA8888,  BC_RGB888,      rgb_to_rgb, rgba_to_rgb_frag },
1528                         { BC_RGBA8888,  BC_RGB_FLOAT,   rgb_to_rgb, rgba_to_rgb_frag },
1529                         { BC_RGBA8888,  BC_YUV888,      rgb_to_yuv, rgba_to_yuv_frag },
1530                         { BC_RGBA8888,  BC_YUVA8888,    rgb_to_yuv, rgb_to_yuv_frag  },
1531                         { BC_RGB_FLOAT, BC_YUV888,      rgb_to_yuv, rgb_to_yuv_frag  },
1532                         { BC_RGB_FLOAT, BC_YUVA8888,    rgb_to_yuv, rgb_to_yuv_frag  },
1533                         { BC_RGBA_FLOAT,BC_RGB888,      rgb_to_rgb, rgba_to_rgb_frag },
1534                         { BC_RGBA_FLOAT,BC_RGB_FLOAT,   rgb_to_rgb, rgba_to_rgb_frag },
1535                         { BC_RGBA_FLOAT,BC_YUV888,      rgb_to_yuv, rgba_to_yuv_frag },
1536                         { BC_RGBA_FLOAT,BC_YUVA8888,    rgb_to_yuv, rgb_to_yuv_frag  },
1537                         { BC_YUV888,    BC_RGB888,      yuv_to_rgb, yuv_to_rgb_frag  },
1538                         { BC_YUV888,    BC_RGBA8888,    yuv_to_rgb, yuv_to_rgb_frag  },
1539                         { BC_YUV888,    BC_RGB_FLOAT,   yuv_to_rgb, yuv_to_rgb_frag  },
1540                         { BC_YUV888,    BC_RGBA_FLOAT,  yuv_to_rgb, yuv_to_rgb_frag  },
1541                         { BC_YUVA8888,  BC_RGB888,      yuv_to_rgb, yuva_to_rgb_frag },
1542                         { BC_YUVA8888,  BC_RGBA8888,    yuv_to_rgb, yuv_to_rgb_frag  },
1543                         { BC_YUVA8888,  BC_RGB_FLOAT,   yuv_to_rgb, yuva_to_rgb_frag },
1544                         { BC_YUVA8888,  BC_RGBA_FLOAT,  yuv_to_rgb, yuv_to_rgb_frag  },
1545                         { BC_YUVA8888,  BC_YUV888,      yuv_to_yuv, yuva_to_yuv_frag },
1546                 };
1547
1548                 const char *shader = 0;
1549                 int table_size = sizeof(cmodel_shader_table) / sizeof(cmodel_shader_table_t);
1550                 for( int i=0; i<table_size; ++i ) {
1551                         if( cmodel_shader_table[i].src == src_cmodel &&
1552                             cmodel_shader_table[i].dst == dst_cmodel ) {
1553                                 shader = cmodel_shader_table[i].shader;
1554                                 type = cmodel_shader_table[i].typ;
1555                                 break;
1556                         }
1557                 }
1558
1559 // printf("Playback3D::convert_cmodel_sync %d %d %d shader=\n%s",
1560 // __LINE__,
1561 // command->frame->get_color_model(),
1562 // command->dst_cmodel,
1563 // shader);
1564
1565                 const char *shader_stack[9];
1566                 memset(shader_stack,0, sizeof(shader_stack));
1567                 int current_shader = 0;
1568
1569                 if( shader ) {
1570 //printf("Playback3D::convert_cmodel_sync %d\n", __LINE__);
1571                         shader_stack[current_shader++] = shader;
1572                         shader_stack[current_shader] = 0;
1573                         unsigned int shader_id = VFrame::make_shader(shader_stack);
1574
1575                         command->frame->bind_texture(0);
1576                         glUseProgram(shader_id);
1577
1578                         glUniform1i(glGetUniformLocation(shader_id, "tex"), 0);
1579                         switch( type ) {
1580                         case rgb_to_yuv:
1581                                 BC_GL_RGB_TO_YUV(shader_id);
1582                                 break;
1583                         case yuv_to_rgb:
1584                                 BC_GL_YUV_TO_RGB(shader_id);
1585                                 break;
1586                         }
1587
1588                         command->frame->draw_texture();
1589                         if(shader) glUseProgram(0);
1590                         command->frame->set_opengl_state(VFrame::SCREEN);
1591                 }
1592
1593                 window->unlock_window();
1594         }
1595
1596         command->canvas->unlock_canvas();
1597 #endif // HAVE_GL
1598 }
1599
1600 void Playback3D::do_fade(Canvas *canvas, VFrame *frame, float fade)
1601 {
1602         Playback3DCommand command;
1603         command.command = Playback3DCommand::DO_FADE;
1604         command.canvas = canvas;
1605         command.frame = frame;
1606         command.alpha = fade;
1607         send_command(&command);
1608 }
1609
1610 void Playback3D::do_fade_sync(Playback3DCommand *command)
1611 {
1612 #ifdef HAVE_GL
1613         command->canvas->lock_canvas("Playback3D::do_mask_sync");
1614         if(command->canvas->get_canvas())
1615         {
1616                 BC_WindowBase *window = command->canvas->get_canvas();
1617                 window->lock_window("Playback3D::do_fade_sync");
1618                 window->enable_opengl();
1619
1620                 switch(command->frame->get_opengl_state())
1621                 {
1622                         case VFrame::RAM:
1623                                 command->frame->to_texture();
1624                                 break;
1625
1626                         case VFrame::SCREEN:
1627 // Read back from PBuffer
1628 // Bind context to pbuffer
1629                                 command->frame->enable_opengl();
1630                                 command->frame->screen_to_texture();
1631                                 break;
1632                 }
1633
1634
1635                 command->frame->enable_opengl();
1636                 command->frame->init_screen();
1637                 command->frame->bind_texture(0);
1638
1639 //              glClearColor(0.0, 0.0, 0.0, 0.0);
1640 //              glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1641                 glDisable(GL_BLEND);
1642                 unsigned int frag_shader = 0;
1643                 switch(command->frame->get_color_model())
1644                 {
1645 // For the alpha colormodels, the native function seems to multiply the
1646 // components by the alpha instead of just the alpha.
1647                         case BC_RGBA8888:
1648                         case BC_RGBA_FLOAT:
1649                         case BC_YUVA8888:
1650                                 frag_shader = VFrame::make_shader(0, fade_rgba_frag, 0);
1651                                 break;
1652
1653                         case BC_RGB888:
1654                                 glEnable(GL_BLEND);
1655                                 glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
1656                                 glColor4f(command->alpha, command->alpha, command->alpha, 1);
1657                                 break;
1658
1659
1660                         case BC_YUV888:
1661                                 frag_shader = VFrame::make_shader(0, fade_yuv_frag, 0);
1662                                 break;
1663                 }
1664
1665
1666                 if( frag_shader ) {
1667                         glUseProgram(frag_shader);
1668                         int variable;
1669                         if((variable = glGetUniformLocation(frag_shader, "tex")) >= 0)
1670                                 glUniform1i(variable, 0);
1671                         if((variable = glGetUniformLocation(frag_shader, "alpha")) >= 0)
1672                                 glUniform1f(variable, command->alpha);
1673                 }
1674
1675                 command->frame->draw_texture();
1676                 command->frame->set_opengl_state(VFrame::SCREEN);
1677
1678                 if(frag_shader)
1679                 {
1680                         glUseProgram(0);
1681                 }
1682
1683                 glColor4f(1, 1, 1, 1);
1684                 glDisable(GL_BLEND);
1685
1686                 window->unlock_window();
1687         }
1688         command->canvas->unlock_canvas();
1689 #endif
1690 }
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702 int Playback3D::run_plugin(Canvas *canvas, PluginClient *client)
1703 {
1704         Playback3DCommand command;
1705         command.command = Playback3DCommand::PLUGIN;
1706         command.canvas = canvas;
1707         command.plugin_client = client;
1708         return send_command(&command);
1709 }
1710
1711 void Playback3D::run_plugin_sync(Playback3DCommand *command)
1712 {
1713         command->canvas->lock_canvas("Playback3D::run_plugin_sync");
1714         if(command->canvas->get_canvas())
1715         {
1716                 BC_WindowBase *window = command->canvas->get_canvas();
1717                 window->lock_window("Playback3D::run_plugin_sync");
1718                 window->enable_opengl();
1719
1720                 command->result = ((PluginVClient*)command->plugin_client)->handle_opengl();
1721
1722                 window->unlock_window();
1723         }
1724         command->canvas->unlock_canvas();
1725 }
1726
1727