titler rework, some code cleanup and fixes
[goodguy/history.git] / cinelerra-5.1 / guicast / vframe.h
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2011 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 VFRAME_H
23 #define VFRAME_H
24
25 #include "arraylist.h"
26 #include "bcbitmap.inc"
27 #include "bchash.inc"
28 #include "bcpbuffer.inc"
29 #include "bctexture.inc"
30 #include "bcwindowbase.inc"
31 #include "bccmodels.h"
32 #include "bccmodels.h"
33 #include "vframe.inc"
34
35 // Maximum number of prev or next effects to be pushed onto the stacks.
36 #define MAX_STACK_ELEMENTS 255
37
38
39 // Scene graph for 3D models
40 // Defined by the subclass
41 class VFrameScene
42 {
43 public:
44         VFrameScene();
45         virtual ~VFrameScene();
46 };
47
48
49
50 class VFrame
51 {
52         friend class VFramePng;
53         friend class PngReadFunction;
54 public:
55 // Create new frame with shared data if *data is nonzero.
56 // Pass 0 to *data & -1 to shmid if private data is desired.
57         VFrame(int w,
58                 int h,
59                 int color_model,
60                 long bytes_per_line = -1);
61         VFrame(unsigned char *data,
62                 int shmid,
63                 int w,
64                 int h,
65                 int color_model /* = BC_RGBA8888 */,
66                 long bytes_per_line /* = -1 */);
67         VFrame(unsigned char *data,  // 0
68                 int shmid, // -1
69                 long y_offset,
70                 long u_offset,
71                 long v_offset,
72                 int w,
73                 int h,
74                 int color_model,  /* = BC_RGBA8888 */
75                 long bytes_per_line /* = -1 */);
76         VFrame(BC_Bitmap *bitmap,
77                 int w,
78                 int h,
79                 int color_model,
80                 long bytes_per_line);
81
82         VFrame(VFrame &vframe);
83 // Create new frame for compressed data.
84         VFrame();
85         ~VFrame();
86
87 // Return 1 if the colormodel and dimensions are the same
88 // Used by FrameCache
89         int equivalent(VFrame *src, int test_stacks = 0);
90
91 // Reallocate a frame without deleting the class
92         int reallocate(
93                 unsigned char *data,   // Data if shared
94                 int shmid,             // shmid if IPC  -1 if not
95                 long y_offset,         // plane offsets if shared YUV
96                 long u_offset,
97                 long v_offset,
98                 int w,
99                 int h,
100                 int color_model,
101                 long bytes_per_line);        // -1 if unused
102
103         void set_memory(unsigned char *data,
104                 int shmid,
105                 long y_offset,
106                 long u_offset,
107                 long v_offset);
108         void set_memory(BC_Bitmap *bitmap);
109
110         void set_compressed_memory(unsigned char *data,
111                 int shmid,
112                 int data_size,
113                 int data_allocated);
114
115 // Write a PNG for debugging
116         int write_png(const char *path);
117
118 // if frame points to the same data as this return 1
119         int equals(VFrame *frame);
120 // Test if frame already matches parameters
121         int params_match(int w, int h, int color_model);
122 // Test if data values in the frame match
123         int data_matches(VFrame *frame);
124
125 //      long set_shm_offset(long offset);
126 //      long get_shm_offset();
127         int get_shmid() { return shmid; }
128         void set_use_shm(int value) { use_shm = value; }
129         int get_use_shm() { return use_shm; }
130
131 // direct copy with no alpha
132         int copy_from(VFrame *frame);
133 // BC_CModels::transfer
134         int transfer_from(VFrame *frame, int bg_color, int in_x, int in_y, int in_w, int in_h);
135         int transfer_from(VFrame *frame, int bg_color=0) {
136                 return transfer_from(frame, bg_color, 0,0, frame->get_w(),frame->get_h());
137         }
138 // Required for YUV
139         int clear_frame();
140         int allocate_compressed_data(long bytes);
141
142 // Sequence number. -1 means invalid.  Passing frames to the encoder is
143 // asynchronous.  The sequence number must be preserved in the image itself
144 // to encode discontinuous frames.
145         long get_number() { return sequence_number; }
146         void set_number(long number) { sequence_number = number; }
147
148         int get_color_model() { return color_model; }
149 // Get the data pointer
150         unsigned char* get_data() { return data; }
151         long get_compressed_allocated() { return compressed_allocated; }
152         long get_compressed_size() { return compressed_size; }
153         void set_compressed_size(long size) { compressed_size = size; }
154         double get_timestamp() { return timestamp; }
155         void set_timestamp(double time) { timestamp = time; }
156
157 // return an array of pointers to rows
158         unsigned char** get_rows() { return rows; }
159         int get_memory_usage();
160 // accessors
161         int get_w() { return w; }
162         int get_h() { return h; }
163         int get_w_fixed() { return w - 1; }
164         int get_h_fixed() { return h - 1; }
165         unsigned char *get_y() { return y; }
166         unsigned char *get_u() { return u; }
167         unsigned char *get_v() { return v; }
168 // return rgba planes
169         unsigned char *get_r() { return y; }
170         unsigned char *get_g() { return u; }
171         unsigned char *get_b() { return v; }
172         unsigned char *get_a() { return a; }
173         void set_a(unsigned char *ap) { a = ap; }
174 // return yuv planes
175         static int get_scale_tables(int *column_table, int *row_table,
176                         int in_x1, int in_y1, int in_x2, int in_y2,
177                         int out_x1, int out_y1, int out_x2, int out_y2);
178         int get_bytes_per_pixel() { return bytes_per_pixel; }
179         long get_bytes_per_line();
180         int get_memory_type();
181
182
183
184         static int calculate_bytes_per_pixel(int colormodel);
185 // Get size + 4 for assembly language
186         static long calculate_data_size(int w, int h,
187                 int bytes_per_line = -1, int color_model = BC_RGB888);
188 // Get size of uncompressed frame buffer without extra 4 bytes
189         long get_data_size();
190
191         void rotate270();
192         void rotate90();
193         void flip_vert();
194         void flip_horiz();
195
196 // Convenience storage.
197 // Returns -1 if not set.
198         int get_field2_offset();
199         int set_field2_offset(int value);
200 // Set keyframe status
201         void set_keyframe(int value);
202         int get_keyframe();
203
204 // If the opengl state is RAM, transfer image from RAM to the texture
205 // referenced by this frame.
206 // If the opengl state is TEXTURE, do nothing.
207 // If the opengl state is SCREEN, switch the current drawable to the pbuffer and
208 // transfer the image to the texture with screen_to_texture.
209 // The opengl state is changed to TEXTURE.
210 // If no textures exist, textures are created.
211 // If the textures already exist, they are reused.
212 // Textures are resized to match the current dimensions.
213 // Must be called from a synchronous opengl thread after enable_opengl.
214         void to_texture();
215
216 // Transfer from PBuffer to RAM.
217 //   used in Playback3D::overlay_sync, plugin Overlay::handle_opengl
218         void screen_to_ram();
219
220 // Transfer contents of current pbuffer to texture,
221 // creating a new texture if necessary.
222 // Coordinates are the coordinates in the drawable to copy.
223         void screen_to_texture(int x = -1, int y = -1, int w = -1, int h = -1);
224
225 // Transfer contents of texture to the current drawable.
226 // Just calls the vertex functions but doesn't initialize.
227 // The coordinates are relative to the VFrame size and flipped to make
228 // the texture upright.
229 // The default coordinates are the size of the VFrame.
230 // flip_y flips the texture in the vertical direction and only used when
231 // writing to the final surface.
232         void draw_texture(float in_x1, float in_y1, float in_x2, float in_y2,
233                 float out_x1, float out_y1, float out_x2, float out_y2,
234                 int flip_y = 0);
235 // Draw the texture using the frame's size as the input and output coordinates.
236         void draw_texture(int flip_y = 0);
237
238
239
240
241
242
243
244 // ================================ OpenGL functions ===========================
245 // Defined in vframe3d.C
246 // Location of working image if OpenGL playback
247         int get_opengl_state();
248         void set_opengl_state(int value);
249 // OpenGL states
250         enum
251         {
252 // Undefined
253                 UNKNOWN,
254 // OpenGL image is in RAM
255                 RAM,
256 // OpenGL image is in texture
257                 TEXTURE,
258 // OpenGL image is composited in PBuffer or back buffer
259                 SCREEN
260         };
261
262 // Texture ID
263         int get_texture_id();
264         void set_texture_id(int id);
265 // Get window ID the texture is bound to
266         int get_window_id();
267         int get_texture_w();
268         int get_texture_h();
269         int get_texture_components();
270
271
272 // Binds the opengl context to this frame's PBuffer
273         void enable_opengl();
274
275 // Clears the pbuffer with the right values depending on YUV
276         void clear_pbuffer();
277
278 // Get the pbuffer
279         BC_PBuffer* get_pbuffer();
280
281 // Bind the frame's texture to GL_TEXTURE_2D and enable it.
282 // If a texture_unit is supplied, the texture unit is made active
283 // and the commands are run in the right sequence to
284 // initialize it to our preferred specifications.
285         void bind_texture(int texture_unit = -1);
286
287
288
289 // Create a frustum with 0,0 in the upper left and w,-h in the bottom right.
290 // Set preferred opengl settings.
291         static void init_screen(int w, int h);
292 // Calls init_screen with the current frame's dimensions.
293         void init_screen();
294
295 // Compiles and links the shaders into a program.
296 // Adds the program with put_shader.
297 // Returns the program handle.
298 // Requires a null terminated argument list of shaders to link together.
299 // At least one shader argument must have a main() function.  make_shader
300 // replaces all the main() functions with unique functions and calls them in
301 // sequence, so multiple independant shaders can be linked.
302 // x is a placeholder for va_arg and should be 0.
303         static unsigned int make_shader(int x, ...);
304         static void dump_shader(int shader_id);
305
306 // Because OpenGL is faster if multiple effects are combined, we need
307 // to provide ways for effects to aggregate.
308 // The prev_effect is the object providing the data to read_frame.
309 // The next_effect is the object which called read_frame.
310 // Push and pop are only called from Cinelerra internals, so
311 // if an object calls read_frame with a temporary, the stack before and after
312 // the temporary is lost.
313         void push_prev_effect(const char *name);
314         void pop_prev_effect();
315         void push_next_effect(const char *name);
316         void pop_next_effect();
317 // These are called by plugins to determine aggregation.
318 // They access any member of the stack based on the number argument.
319 // next effect 0 is the one that called read_frame most recently.
320 // prev effect 0 is the one that filled our call to read_frame.
321         const char* get_next_effect(int number = 0);
322         const char* get_prev_effect(int number = 0);
323
324 // It isn't enough to know the name of the neighboring effects.
325 // Relevant configuration parameters must be passed on.
326         BC_Hash* get_params();
327
328 // get/set read status  -1/err, 0/noxfer, 1/ok
329         int get_status() { return status; }
330         void set_status(int v) { status = v; }
331
332 // Compare stacks and params from 2 images and return 1 if equal.
333         int equal_stacks(VFrame *src);
334
335 // Copy stacks and params from another frame
336 // Replaces the stacks with the src stacks but only updates the params.
337         void copy_stacks(VFrame *src);
338 // Updates the params with values from src
339         void copy_params(VFrame *src);
340
341 // This clears the stacks and the param table
342         void clear_stacks();
343
344         int (VFrame::*draw_point)(int x, int y);
345         int pixel_rgb, pixel_yuv, stipple;
346
347         void set_pixel_color(int rgb);
348         void set_stiple(int mask);
349         int draw_pixel(int x, int y);
350         void draw_line(int x1, int y1, int x2, int y2);
351         void draw_smooth(int x1, int y1, int x2, int y2, int x3, int y3);
352         void smooth_draw(int x1, int y1, int x2, int y2, int x3, int y3);
353         void draw_rect(int x1, int y1, int x2, int y2);
354         void draw_arrow(int x1, int y1, int x2, int y2, int sz=10);
355         void draw_x(int x1, int y1, int sz=2);
356         void draw_t(int x1, int y1, int sz=2);
357
358 // 3D scene graphs
359 // Not integrated with shmem because that only affects codecs
360         VFrameScene* get_scene();
361
362 // Debugging
363         void dump_stacks();
364         void dump();
365
366         void dump_params();
367
368 private:
369
370 // 3D scene graphs
371 // Not integrated with shmem because that only affects codecs
372         VFrameScene *scene;
373
374 // Create a PBuffer matching this frame's dimensions and to be
375 // referenced by this frame.  Does nothing if the pbuffer already exists.
376 // If the frame is resized, the PBuffer is deleted.
377 // Called by enable_opengl.
378 // This allows PBuffers, textures, and bitmaps to travel through the entire
379 // rendering chain without requiring the user to manage a lot of objects.
380 // Must be called from a synchronous opengl thread after enable_opengl.
381         void create_pbuffer();
382
383         int clear_objects(int do_opengl);
384         int reset_parameters(int do_opengl);
385         void create_row_pointers();
386         int allocate_data(unsigned char *data,
387                 int shmid,
388                 long y_offset,
389                 long u_offset,
390                 long v_offset,
391                 int w,
392                 int h,
393                 int color_model,
394                 long bytes_per_line);
395
396 // Convenience storage
397         int field2_offset;
398         int memory_type;
399         enum
400         {
401                 PRIVATE,
402                 SHARED,
403                 SHMGET
404         };
405
406 // Data pointer is pointing to someone else's buffer.
407 //      long shm_offset;
408 // ID of shared memory if using IPC.
409 // The 1st 1 after reboot is 0.
410         int shmid;
411 // Local setting for shm usage
412         int use_shm;
413 // If not set by user, is calculated from color_model
414         long bytes_per_line;
415         int bytes_per_pixel;
416 // Image data
417         unsigned char *data;
418 // Pointers to the start of each row
419         unsigned char **rows;
420 // One of the #defines
421         int color_model;
422 // Allocated space for compressed data
423         long compressed_allocated;
424 // Size of stored compressed image
425         long compressed_size;
426 // Pointers to yuv / rgb planes
427         unsigned char *y, *u, *v;
428         long y_offset;
429         long u_offset;
430         long v_offset;
431 // Pointer to alpha plane
432         unsigned char *a;
433 // Dimensions of frame
434         int w, h;
435 // Info for reading png images
436         const unsigned char *image;
437         long image_offset;
438         long image_size;
439 // For writing discontinuous frames in background rendering
440         long sequence_number;
441         double timestamp;
442 // read status of input frame -1/err, 0/noxfr, 1/ok
443         int status;
444 // OpenGL support
445         int is_keyframe;
446 // State of the current texture
447         BC_Texture *texture;
448 // State of the current PBuffer
449         BC_PBuffer *pbuffer;
450
451 // Location of working image if OpenGL playback
452         int opengl_state;
453
454         ArrayList<char*> prev_effects;
455         ArrayList<char*> next_effects;
456         BC_Hash *params;
457 };
458
459 // Create a frame with the png image
460 class VFramePng : public VFrame {
461 // Read a PNG into the frame with alpha
462         int read_png(const unsigned char *data, long image_size, double xscale, double yscale);
463 public:
464         VFramePng(unsigned char *png_data, double s=0);
465         VFramePng(unsigned char *png_data, long image_size, double xs=0, double ys=0);
466         ~VFramePng();
467         static VFrame *vframe_png(int fd, double xs=1, double ys=1);
468         static VFrame *vframe_png(const char *png_path, double xs=1, double ys=1);
469 };
470
471 #endif