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