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