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