update SVT-AV1 to latest version of 2.3.0 from 2.2.1
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / playback3d.h
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 #ifndef PLAYBACK3D_H
23 #define PLAYBACK3D_H
24
25 #include "arraylist.h"
26 #include "bccolors.h"
27 #include "bcpixmap.inc"
28 #include "bcsynchronous.h"
29 #include "bcwindowbase.inc"
30 #include "canvas.inc"
31 #include "condition.inc"
32 #include "maskauto.h"
33 #include "maskautos.inc"
34 #include "mutex.inc"
35 #include "mwindow.inc"
36 #include "pluginclient.inc"
37 #include "thread.h"
38 #include "vframe.inc"
39
40
41
42
43 // use static presets YUV in bccolors.h
44 #define BC_GL_MATRIX(shader, mat) \
45         glUniformMatrix3fv(glGetUniformLocation(shader, #mat), 1, 1, YUV::mat)
46 #define BC_GL_VECTOR(shader, vec) \
47         glUniform3fv(glGetUniformLocation(shader, #vec), 1, YUV::vec)
48
49 #define BC_GL_YMINF(shader) \
50         glUniform1f(glGetUniformLocation(shader, "yminf"), YUV::yuv.get_yminf())
51
52 #define BC_GL_RGB_TO_YUV(shader) do { \
53         BC_GL_MATRIX(shader, rgb_to_yuv_matrix); \
54         BC_GL_YMINF(shader); \
55 } while(0)
56
57 #define BC_GL_RGB_TO_Y(shader) do { \
58         BC_GL_VECTOR(shader, rgb_to_y_vector); \
59         BC_GL_YMINF(shader); \
60 } while(0)
61
62 #define BC_GL_YUV_TO_RGB(shader) do { \
63         BC_GL_MATRIX(shader, yuv_to_rgb_matrix); \
64         BC_GL_YMINF(shader); \
65 } while(0)
66
67 #define BC_GL_COLORS(shader) do { \
68         BC_GL_MATRIX(shader, yuv_to_rgb_matrix); \
69         BC_GL_MATRIX(shader, rgb_to_yuv_matrix); \
70         BC_GL_YMINF(shader); \
71 } while(0)
72
73
74 #define bc_gl_yuv_to_rgb "uniform mat3 yuv_to_rgb_matrix;\n"
75 #define bc_gl_rgb_to_yuv "uniform mat3 rgb_to_yuv_matrix;\n"
76 #define bc_gl_rgb_to_y   "uniform vec3 rgb_to_y_vector;\n"
77 #define bc_gl_yminf      "uniform float yminf;\n"
78 #define bc_gl_colors bc_gl_yuv_to_rgb bc_gl_rgb_to_yuv bc_gl_yminf
79
80 // Macros for useful fragment shaders
81 #define YUV_TO_RGB_FRAG(PIXEL) \
82         PIXEL ".rgb -= vec3(yminf, 0.5, 0.5);\n" \
83         PIXEL ".rgb = yuv_to_rgb_matrix * " PIXEL ".rgb;\n"
84
85 #define RGB_TO_YUV_FRAG(PIXEL) \
86         PIXEL ".rgb = rgb_to_yuv_matrix * " PIXEL ".rgb;\n" \
87         PIXEL ".rgb += vec3(yminf, 0.5, 0.5);\n"
88
89 #define RGB_TO_HSV_FRAG(PIXEL) \
90         "{\n" \
91         "float r, g, b;\n" \
92         "float h, s, v;\n" \
93         "float min, max, delta;\n" \
94         "float f, p, q, t;\n" \
95         "r = " PIXEL ".r;\n" \
96         "g = " PIXEL ".g;\n" \
97         "b = " PIXEL ".b;\n" \
98         "min = ((r < g) ? r : g) < b ? ((r < g) ? r : g) : b;\n" \
99         "max = ((r > g) ? r : g) > b ? ((r > g) ? r : g) : b;\n" \
100         "v = max;\n" \
101         "delta = max - min;\n" \
102         "if(max != 0.0 && delta != 0.0)\n" \
103     "{\n" \
104         "    s = delta / max;\n" \
105         "       if(r == max)\n" \
106     "           h = (g - b) / delta;\n" \
107         "       else \n" \
108         "       if(g == max)\n" \
109     "           h = 2.0 + (b - r) / delta;\n" \
110         "       else\n" \
111     "           h = 4.0 + (r - g) / delta;\n" \
112         "\n" \
113         "       h *= 60.0;\n" \
114         "       if(h < 0.0)\n" \
115     "           h += 360.0;\n" \
116         "}\n" \
117         "else\n"  \
118         "{\n" \
119     "    s = 0.0;\n" \
120     "    h = -1.0;\n" \
121         "}\n" \
122         "" PIXEL ".r = h;\n" \
123         "" PIXEL ".g = s;\n" \
124         "" PIXEL ".b = v;\n" \
125         "}\n"
126
127 #define HSV_TO_RGB_FRAG(PIXEL) \
128         "{\n" \
129     "int i;\n" \
130         "float r, g, b;\n" \
131         "float h, s, v;\n" \
132         "float min, max, delta;\n" \
133         "float f, p, q, t;\n" \
134         "h = " PIXEL ".r;\n" \
135         "s = " PIXEL ".g;\n" \
136         "v = " PIXEL ".b;\n" \
137     "if(s == 0.0) \n" \
138         "{\n" \
139     "    r = g = b = v;\n" \
140     "}\n" \
141         "else\n" \
142         "{\n" \
143     "   h /= 60.0;\n" \
144     "   i = int(h);\n" \
145     "   f = h - float(i);\n" \
146     "   p = v * (1.0 - s);\n" \
147     "   q = v * (1.0 - s * f);\n" \
148     "   t = v * (1.0 - s * (1.0 - f));\n" \
149         "\n" \
150     "   if(i == 0)\n" \
151         "       {\n" \
152     "           r = v;\n" \
153     "           g = t;\n" \
154     "           b = p;\n" \
155     "    }\n" \
156         "       else\n" \
157         "       if(i == 1)\n" \
158         "       {\n" \
159     "           r = q;\n" \
160     "           g = v;\n" \
161     "           b = p;\n" \
162     "    }\n" \
163         "       else\n" \
164         "       if(i == 2)\n" \
165         "       {\n" \
166     "           r = p;\n" \
167     "           g = v;\n" \
168     "           b = t;\n" \
169     "   }\n" \
170         "       else\n" \
171         "       if(i == 3)\n" \
172         "       {\n" \
173     "           r = p;\n" \
174     "           g = q;\n" \
175     "           b = v;\n" \
176     "   }\n" \
177         "       else\n" \
178         "       if(i == 4)\n" \
179         "       {\n" \
180     "           r = t;\n" \
181     "           g = p;\n" \
182     "           b = v;\n" \
183     "    }\n" \
184         "       else\n" \
185         "       if(i == 5)\n" \
186         "       {\n" \
187     "           r = v;\n" \
188     "           g = p;\n" \
189     "           b = q;\n" \
190     "   }\n" \
191         "}\n" \
192         "" PIXEL ".r = r;\n" \
193         "" PIXEL ".g = g;\n" \
194         "" PIXEL ".b = b;\n" \
195         "}\n"
196
197 class Playback3DCommand : public BC_SynchronousCommand
198 {
199 public:
200         Playback3DCommand();
201         void copy_from(BC_SynchronousCommand *command);
202
203 // Extra commands
204         enum
205         {
206 // 5
207                 WRITE_BUFFER = LAST_COMMAND,
208                 CLEAR_OUTPUT,
209                 OVERLAY,
210                 DO_FADE,
211                 DO_MASK,
212                 PLUGIN,
213                 CLEAR_INPUT,
214                 DO_CAMERA,
215                 COPY_FROM,
216                 CONVERT_CMODEL
217         };
218
219         Canvas *canvas;
220         int is_cleared;
221
222 // Parameters for overlay command
223         float in_x1;
224         float in_y1;
225         float in_x2;
226         float in_y2;
227         float out_x1;
228         float out_y1;
229         float out_x2;
230         float out_y2;
231 // 0 - 1
232         float alpha;
233         int mode;
234         int interpolation_type;
235         VFrame *input;
236         int want_texture;
237         int is_nested;
238
239         int dst_cmodel;
240         int64_t start_position_project;
241         MaskAutos *keyframe_set;
242         MaskAuto *keyframe;
243         MaskAuto *default_auto;
244         PluginClient *plugin_client;
245 };
246
247
248 class Playback3D : public BC_Synchronous
249 {
250 public:
251         Playback3D(MWindow *mwindow);
252         ~Playback3D();
253
254         BC_SynchronousCommand* new_command();
255         void handle_command(BC_SynchronousCommand *command);
256
257 // Called by VDeviceX11::write_buffer during video playback
258         void write_buffer(Canvas *canvas,
259                 VFrame *frame,
260                 float in_x1,
261                 float in_y1,
262                 float in_x2,
263                 float in_y2,
264                 float out_x1,
265                 float out_y1,
266                 float out_x2,
267                 float out_y2,
268                 int is_cleared);
269
270 // Reads from pbuffer to either RAM or texture and updates the dst state
271 // want_texture - causes read into texture if 1
272         void copy_from(Canvas *canvas,
273                 VFrame *dst,
274                 VFrame *src,
275                 int want_texture = 0);
276
277 // Clear framebuffer before composing virtual console
278 // output - passed when rendering refresh frame.  If 0, the canvas is cleared.
279         void clear_output(Canvas *canvas, VFrame *output);
280
281         void do_fade(Canvas *canvas, VFrame *frame, float fade);
282         void convert_cmodel(Canvas *canvas, VFrame *output, int dst_cmodel);
283
284         void draw_spots(MaskSpots &spots, int ix1,int iy1, int ix2,int iy2);
285         void do_mask(Canvas *canvas, VFrame *output, int64_t start_position_project,
286                 MaskAutos *keyframe_set, MaskAuto *keyframe, MaskAuto *default_auto);
287
288 // Overlay a virtual node on the framebuffer
289         void overlay(Canvas *canvas,
290                 VFrame *input,
291                 float in_x1,
292                 float in_y1,
293                 float in_x2,
294                 float in_y2,
295                 float out_x1,
296                 float out_y1,
297                 float out_x2,
298                 float out_y2,
299                 float alpha,        // 0 - 1
300                 int mode,
301                 int interpolation_type,
302 // supplied if rendering single frame to PBuffer.
303                 VFrame *output = 0,
304                 int is_nested = 0);
305
306
307         int run_plugin(Canvas *canvas, PluginClient *client);
308
309         void clear_input(Canvas *canvas, VFrame *frame);
310         void do_camera(Canvas *canvas,
311                 VFrame *output,
312                 VFrame *input,
313                 float in_x1,
314                 float in_y1,
315                 float in_x2,
316                 float in_y2,
317                 float out_x1,
318                 float out_y1,
319                 float out_x2,
320                 float out_y2);
321
322 private:
323 // Called by write_buffer and clear_frame to initialize OpenGL flags
324         void color_frame(Playback3DCommand *command,
325                 float r, float g, float b, float a);
326         void write_buffer_sync(Playback3DCommand *command);
327         void draw_output(Playback3DCommand *command, int flip_y);
328         void clear_output_sync(Playback3DCommand *command);
329         void clear_input_sync(Playback3DCommand *command);
330         void overlay_sync(Playback3DCommand *command);
331 // Read frame buffer back into texture for overlay operation
332         void enable_overlay_texture(Playback3DCommand *command);
333         void do_fade_sync(Playback3DCommand *command);
334         void do_mask_sync(Playback3DCommand *command);
335         void run_plugin_sync(Playback3DCommand *command);
336         void do_camera_sync(Playback3DCommand *command);
337 //      void draw_refresh_sync(Playback3DCommand *command);
338         void copy_from_sync(Playback3DCommand *command);
339         void convert_cmodel_sync(Playback3DCommand *command);
340
341 // Print errors from shader compilation
342         void print_error(unsigned int object, int is_program);
343
344 // This quits the program when it's 1.
345         MWindow *mwindow;
346 // Temporaries for render to texture
347         BC_Texture *temp_texture;
348 // This is set by clear_output and used in compositing directly
349 // to the output framebuffer.
350         int canvas_w;
351         int canvas_h;
352 };
353
354
355
356
357 #endif