r854: Merge 2.1:
[cinelerra_cv/ct.git] / guicast / vframe.h
blob1344faef119d7911b43a1339a2e65a4c1ac1e66e
1 #ifndef VFRAME_H
2 #define VFRAME_H
4 #include "arraylist.h"
5 #include "bcpbuffer.inc"
6 #include "bctexture.inc"
7 #include "colormodels.h"
8 #include "vframe.inc"
10 class PngReadFunction;
13 // Maximum number of prev or next effects to be pushed onto the stacks.
14 #define MAX_STACK_ELEMENTS 255
18 class VFrame
20 public:
21 // Create new frame with shared data if *data is nonzero.
22 // Pass 0 to *data if private data is desired.
23 VFrame(unsigned char *data,
24 int w,
25 int h,
26 int color_model = BC_RGBA8888,
27 long bytes_per_line = -1);
28 VFrame(unsigned char *data,
29 long y_offset,
30 long u_offset,
31 long v_offset,
32 int w,
33 int h,
34 int color_model = BC_RGBA8888,
35 long bytes_per_line = -1);
36 // Create a frame with the png image
37 VFrame(unsigned char *png_data);
38 VFrame(VFrame &vframe);
39 // Create new frame for compressed data.
40 VFrame();
41 ~VFrame();
43 friend class PngReadFunction;
45 // Return 1 if the colormodel and dimensions are the same
46 // Used by FrameCache
47 int equivalent(VFrame *src);
49 // Reallocate a frame without deleting the class
50 int reallocate(unsigned char *data,
51 long y_offset,
52 long u_offset,
53 long v_offset,
54 int w,
55 int h,
56 int color_model,
57 long bytes_per_line);
59 void set_memory(unsigned char *data,
60 long y_offset,
61 long u_offset,
62 long v_offset);
64 void set_compressed_memory(unsigned char *data,
65 int data_size,
66 int data_allocated);
68 // Read a PNG into the frame with alpha
69 int read_png(unsigned char *data);
71 // if frame points to the same data as this return 1
72 int equals(VFrame *frame);
73 // Test if frame already matches parameters
74 int params_match(int w, int h, int color_model);
76 long set_shm_offset(long offset);
77 long get_shm_offset();
79 // direct copy with no alpha
80 int copy_from(VFrame *frame);
81 // Required for YUV
82 int clear_frame();
83 int allocate_compressed_data(long bytes);
85 // Sequence number. -1 means invalid. Passing frames to the encoder is
86 // asynchronous. The sequence number must be preserved in the image itself
87 // to encode discontinuous frames.
88 long get_number();
89 void set_number(long number);
91 long get_compressed_allocated();
92 long get_compressed_size();
93 long set_compressed_size(long size);
94 int get_color_model();
95 // Get the data pointer
96 unsigned char* get_data();
97 // return an array of pointers to rows
98 unsigned char** get_rows();
99 // return yuv planes
100 unsigned char* get_y();
101 unsigned char* get_u();
102 unsigned char* get_v();
103 int get_w();
104 int get_h();
105 int get_w_fixed();
106 int get_h_fixed();
107 static int get_scale_tables(int *column_table, int *row_table,
108 int in_x1, int in_y1, int in_x2, int in_y2,
109 int out_x1, int out_y1, int out_x2, int out_y2);
110 int get_bytes_per_pixel();
111 long get_bytes_per_line();
112 static int calculate_bytes_per_pixel(int colormodel);
113 static long calculate_data_size(int w,
114 int h,
115 int bytes_per_line = -1,
116 int color_model = BC_RGB888);
117 // Get size of uncompressed frame buffer
118 long get_data_size();
119 void rotate270();
120 void rotate90();
121 void flip_vert();
123 // Convenience storage.
124 // Returns -1 if not set.
125 int get_field2_offset();
126 int set_field2_offset(int value);
127 // Set keyframe status
128 void set_keyframe(int value);
129 int get_keyframe();
130 // Overlay src onto this with blending and translation of input.
131 // Source and this must have alpha
132 void overlay(VFrame *src,
133 int out_x1,
134 int out_y1);
136 // If the opengl state is RAM, transfer image from RAM to the texture
137 // referenced by this frame.
138 // If the opengl state is TEXTURE, do nothing.
139 // If the opengl state is SCREEN, switch the current drawable to the pbuffer and
140 // transfer the image to the texture with screen_to_texture.
141 // The opengl state is changed to TEXTURE.
142 // If no textures exist, textures are created.
143 // If the textures already exist, they are reused.
144 // Textures are resized to match the current dimensions.
145 // Must be called from a synchronous opengl thread after enable_opengl.
146 void to_texture();
149 // Transfer contents of current pbuffer to texture,
150 // creating a new texture if necessary.
151 // Coordinates are the coordinates in the drawable to copy.
152 void screen_to_texture(int x = -1,
153 int y = -1,
154 int w = -1,
155 int h = -1);
157 // Transfer contents of texture to the current drawable.
158 // Just calls the vertex functions but doesn't initialize.
159 // The coordinates are relative to the VFrame size and flipped to make
160 // the texture upright.
161 // The default coordinates are the size of the VFrame.
162 // flip_y flips the texture in the vertical direction and only used when
163 // writing to the final surface.
164 void draw_texture(float in_x1,
165 float in_y1,
166 float in_x2,
167 float in_y2,
168 float out_x1,
169 float out_y1,
170 float out_x2,
171 float out_y2,
172 int flip_y = 0);
173 // Draw the texture using the frame's size as the input and output coordinates.
174 void draw_texture(int flip_y = 0);
178 // ================================ OpenGL functions ===========================
179 // Location of working image if OpenGL playback
180 int get_opengl_state();
181 void set_opengl_state(int value);
182 // OpenGL states
183 enum
185 // Undefined
186 UNKNOWN,
187 // OpenGL image is in RAM
188 RAM,
189 // OpenGL image is in texture
190 TEXTURE,
191 // OpenGL image is composited in PBuffer or back buffer
192 SCREEN
195 // Texture ID
196 int get_texture_id();
197 int get_texture_w();
198 int get_texture_h();
199 int get_texture_components();
202 // Binds the opengl context to this frame's PBuffer
203 void enable_opengl();
205 // Clears the pbuffer with the right values depending on YUV
206 void clear_pbuffer();
209 // Bind the frame's texture to GL_TEXTURE_2D and enable it.
210 // If a texture_unit is supplied, the texture unit is made active
211 // and the commands are run in the right sequence to
212 // initialize it to our preferred specifications.
213 void bind_texture(int texture_unit = -1);
217 // Create a frustum with 0,0 in the upper left and w,-h in the bottom right.
218 // Set preferred opengl settings.
219 static void init_screen(int w, int h);
220 // Calls init_screen with the current frame's dimensions.
221 void init_screen();
223 // Compiles and links the shaders into a program.
224 // Adds the program with put_shader.
225 // Returns the program handle.
226 // Requires a null terminated argument list of shaders to link together.
227 // At least one shader argument must have a main() function. make_shader
228 // replaces all the main() functions with unique functions and calls them in
229 // sequence, so multiple independant shaders can be linked.
230 // x is a placeholder for va_arg and should be 0.
231 static unsigned int make_shader(int x, ...);
232 static void dump_shader(int shader_id);
234 // Because OpenGL is faster if multiple effects are combined, we need
235 // to provide ways for effects to aggregate.
236 // The prev_effect is the object providing the data to read_frame.
237 // The next_effect is the object which called read_frame.
238 // Push and pop are only called from Cinelerra internals, so
239 // if an object calls read_frame with a temporary, the stack before and after
240 // the temporary is lost.
241 void push_prev_effect(char *name);
242 void pop_prev_effect();
243 void push_next_effect(char *name);
244 void pop_next_effect();
247 // Copy stacks and params from another frame
248 // Replaces the stacks with the src stacks but only updates the params.
249 void copy_stacks(VFrame *src);
251 // This clears the stacks and the param table
252 void clear_stacks();
255 private:
257 // Create a PBuffer matching this frame's dimensions and to be
258 // referenced by this frame. Does nothing if the pbuffer already exists.
259 // If the frame is resized, the PBuffer is deleted.
260 // Called by enable_opengl.
261 // This allows PBuffers, textures, and bitmaps to travel through the entire
262 // rendering chain without requiring the user to manage a lot of objects.
263 // Must be called from a synchronous opengl thread after enable_opengl.
264 void create_pbuffer();
268 int clear_objects(int do_opengl);
269 int reset_parameters(int do_opengl);
270 void create_row_pointers();
271 int allocate_data(unsigned char *data,
272 long y_offset,
273 long u_offset,
274 long v_offset,
275 int w,
276 int h,
277 int color_model,
278 long bytes_per_line);
280 // Convenience storage
281 int field2_offset;
282 // Data is pointing to someone else's buffer.
283 int shared;
284 long shm_offset;
285 // If not set by user, is calculated from color_model
286 long bytes_per_line;
287 int bytes_per_pixel;
288 // Image data
289 unsigned char *data;
290 // Pointers to the start of each row
291 unsigned char **rows;
292 // One of the #defines
293 int color_model;
294 // Allocated space for compressed data
295 long compressed_allocated;
296 // Size of stored compressed image
297 long compressed_size;
298 // Pointers to yuv planes
299 unsigned char *y, *u, *v;
300 long y_offset;
301 long u_offset;
302 long v_offset;
303 // Dimensions of frame
304 int w, h;
305 // Info for reading png images
306 unsigned char *image;
307 long image_offset;
308 long image_size;
309 // For writing discontinuous frames in background rendering
310 long sequence_number;
312 // OpenGL support
313 int is_keyframe;
314 // State of the current texture
315 BC_Texture *texture;
316 // State of the current PBuffer
317 BC_PBuffer *pbuffer;
319 // Location of working image if OpenGL playback
320 int opengl_state;
322 ArrayList<char*> prev_effects;
323 ArrayList<char*> next_effects;
327 #endif