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