4 * Copyright (C) 2011 Adam Williams <broadcast at earthling dot net>
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.
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.
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
25 #include "arraylist.h"
26 #include "bcbitmap.inc"
28 #include "bcpbuffer.inc"
29 #include "bctexture.inc"
30 #include "bcwindowbase.inc"
31 #include "bccmodels.h"
32 #include "bccmodels.h"
35 // Maximum number of prev or next effects to be pushed onto the stacks.
36 #define MAX_STACK_ELEMENTS 255
37 #define SHM_MIN_SIZE 2048
39 // Scene graph for 3D models
40 // Defined by the subclass
45 virtual ~VFrameScene();
52 friend class VFramePng;
53 friend class PngReadFunction;
55 // Create new frame with shared data if *data is nonzero.
56 // Pass 0 to *data & -1 to shmid if private data is desired.
60 long bytes_per_line = -1);
61 VFrame(unsigned char *data,
65 int color_model /* = BC_RGBA8888 */,
66 long bytes_per_line /* = -1 */);
67 VFrame(unsigned char *data, // 0
74 int color_model, /* = BC_RGBA8888 */
75 long bytes_per_line /* = -1 */);
76 VFrame(BC_Bitmap *bitmap,
82 VFrame(VFrame &vframe);
83 // Create new frame for compressed data.
87 // Return 1 if the colormodel and dimensions are the same
89 int equivalent(VFrame *src, int test_stacks = 0);
91 // Reallocate a frame without deleting the class
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
101 long bytes_per_line); // -1 if unused
103 void set_memory(unsigned char *data,
108 void set_memory(BC_Bitmap *bitmap);
110 void set_compressed_memory(unsigned char *data,
115 // Write a PNG for debugging
116 int write_png(const char *path);
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);
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; }
131 // direct copy with no alpha
132 int copy_from(VFrame *frame);
133 int copy_vframe(VFrame *src);
134 // BC_CModels::transfer
135 int transfer_from(VFrame *frame, int bg_color, int in_x, int in_y, int in_w, int in_h);
136 int transfer_from(VFrame *frame, int bg_color=0) {
137 return transfer_from(frame, bg_color, 0,0, frame->get_w(),frame->get_h());
141 int allocate_compressed_data(long bytes);
143 // Sequence number. -1 means invalid. Passing frames to the encoder is
144 // asynchronous. The sequence number must be preserved in the image itself
145 // to encode discontinuous frames.
146 long get_number() { return sequence_number; }
147 void set_number(long number) { sequence_number = number; }
149 int get_color_model() { return color_model; }
150 // Get the data pointer
151 unsigned char* get_data() { return data; }
152 long get_compressed_allocated() { return compressed_allocated; }
153 long get_compressed_size() { return compressed_size; }
154 void set_compressed_size(long size) { compressed_size = size; }
155 double get_timestamp() { return timestamp; }
156 void set_timestamp(double time) { timestamp = time; }
158 // return an array of pointers to rows
159 unsigned char** get_rows() { return rows; }
160 int get_memory_usage();
162 int get_w() { return w; }
163 int get_h() { return h; }
164 int get_w_fixed() { return w - 1; }
165 int get_h_fixed() { return h - 1; }
166 unsigned char *get_y() { return y; }
167 unsigned char *get_u() { return u; }
168 unsigned char *get_v() { return v; }
169 // return rgba planes
170 unsigned char *get_r() { return y; }
171 unsigned char *get_g() { return u; }
172 unsigned char *get_b() { return v; }
173 unsigned char *get_a() { return a; }
174 void set_a(unsigned char *ap) { a = ap; }
176 static int get_scale_tables(int *column_table, int *row_table,
177 int in_x1, int in_y1, int in_x2, int in_y2,
178 int out_x1, int out_y1, int out_x2, int out_y2);
179 int get_bytes_per_pixel() { return bytes_per_pixel; }
180 long get_bytes_per_line();
181 int get_memory_type();
183 static int calculate_bytes_per_pixel(int colormodel);
184 // Get size + 4 for assembly language
185 static long calculate_data_size(int w, int h,
186 int bytes_per_line = -1, int color_model = BC_RGB888);
187 // Get size of uncompressed frame buffer without extra 4 bytes
188 long get_data_size();
189 // alloc/reset temp vframe to spec
190 static void get_temp(VFrame *&vfrm, int w, int h, int color_model);
197 // Convenience storage.
198 // Returns -1 if not set.
199 int get_field2_offset();
200 int set_field2_offset(int value);
201 // Set keyframe status
202 void set_keyframe(int value);
205 // If the opengl state is RAM, transfer image from RAM to the texture
206 // referenced by this frame.
207 // If the opengl state is TEXTURE, do nothing.
208 // If the opengl state is SCREEN, switch the current drawable to the pbuffer and
209 // transfer the image to the texture with screen_to_texture.
210 // The opengl state is changed to TEXTURE.
211 // If no textures exist, textures are created.
212 // If the textures already exist, they are reused.
213 // Textures are resized to match the current dimensions.
214 // Must be called from a synchronous opengl thread after enable_opengl.
217 // Transfer from PBuffer to RAM.
218 // used in Playback3D::overlay_sync, plugin Overlay::handle_opengl
219 void screen_to_ram();
221 // Transfer contents of current pbuffer to texture,
222 // creating a new texture if necessary.
223 // Coordinates are the coordinates in the drawable to copy.
224 void screen_to_texture(int x = -1, int y = -1, int w = -1, int h = -1);
226 // Transfer contents of texture to the current drawable.
227 // Just calls the vertex functions but doesn't initialize.
228 // The coordinates are relative to the VFrame size and flipped to make
229 // the texture upright.
230 // The default coordinates are the size of the VFrame.
231 // flip_y flips the texture in the vertical direction and only used when
232 // writing to the final surface.
233 void draw_texture(float in_x1, float in_y1, float in_x2, float in_y2,
234 float out_x1, float out_y1, float out_x2, float out_y2,
236 // Draw the texture using the frame's size as the input and output coordinates.
237 void draw_texture(int flip_y = 0);
245 // ================================ OpenGL functions ===========================
246 // Defined in vframe3d.C
247 // Location of working image if OpenGL playback
248 int get_opengl_state();
249 void set_opengl_state(int value);
255 // OpenGL image is in RAM
257 // OpenGL image is in texture
259 // OpenGL image is composited in PBuffer or back buffer
264 int get_texture_id();
265 void set_texture_id(int id);
266 // Get window ID the texture is bound to
270 int get_texture_components();
273 // Binds the opengl context to this frame's PBuffer
274 void enable_opengl();
276 // Clears the pbuffer with the right values depending on YUV
277 void clear_pbuffer();
280 BC_PBuffer* get_pbuffer();
282 // Bind the frame's texture to GL_TEXTURE_2D and enable it.
283 // If a texture_unit is supplied, the texture unit is made active
284 // and the commands are run in the right sequence to
285 // initialize it to our preferred specifications.
286 void bind_texture(int texture_unit = -1);
290 // Create a frustum with 0,0 in the upper left and w,-h in the bottom right.
291 // Set preferred opengl settings.
292 static void init_screen(int w, int h);
293 // Calls init_screen with the current frame's dimensions.
296 // Compiles and links the shaders into a program.
297 // Adds the program with put_shader.
298 // Returns the program handle.
299 // Requires a null terminated argument list of shaders to link together.
300 // if fragments is not NULL, it is a a zero terminated list of frags
301 // if fragments is NULL, then a zero terminated list of va_args frags
302 // At least one shader argument must have a main() function. make_shader
303 // replaces all the main() functions with unique functions and calls them in
304 // sequence, so multiple independant shaders can be linked.
305 static unsigned int make_shader(const char **fragments, ...);
306 static void dump_shader(int shader_id);
308 // Because OpenGL is faster if multiple effects are combined, we need
309 // to provide ways for effects to aggregate.
310 // The prev_effect is the object providing the data to read_frame.
311 // The next_effect is the object which called read_frame.
312 // Push and pop are only called from Cinelerra internals, so
313 // if an object calls read_frame with a temporary, the stack before and after
314 // the temporary is lost.
315 void push_prev_effect(const char *name);
316 void pop_prev_effect();
317 void push_next_effect(const char *name);
318 void pop_next_effect();
319 // These are called by plugins to determine aggregation.
320 // They access any member of the stack based on the number argument.
321 // next effect 0 is the one that called read_frame most recently.
322 // prev effect 0 is the one that filled our call to read_frame.
323 const char* get_next_effect(int number = 0);
324 const char* get_prev_effect(int number = 0);
326 // It isn't enough to know the name of the neighboring effects.
327 // Relevant configuration parameters must be passed on.
328 BC_Hash* get_params();
330 // get/set read status -1/err, 0/noxfer, 1/ok
331 int get_status() { return status; }
332 void set_status(int v) { status = v; }
334 // Compare stacks and params from 2 images and return 1 if equal.
335 int equal_stacks(VFrame *src);
337 // Copy stacks and params from another frame
338 // Replaces the stacks with the src stacks but only updates the params.
339 void copy_stacks(VFrame *src);
340 // Updates the params with values from src
341 void copy_params(VFrame *src);
343 // This clears the stacks and the param table
346 virtual int draw_pixel(int x, int y);
347 int pixel_rgb, pixel_yuv, stipple;
349 void set_pixel_color(int rgb);
350 void set_pixel_color(int rgb, int a) { set_pixel_color((rgb&0xffffff)|((~a&0xff)<<24)); }
351 void set_stiple(int mask);
352 void draw_line(int x1, int y1, int x2, int y2);
353 void draw_smooth(int x1, int y1, int x2, int y2, int x3, int y3);
354 void smooth_draw(int x1, int y1, int x2, int y2, int x3, int y3);
355 void draw_rect(int x1, int y1, int x2, int y2);
356 void draw_arrow(int x1, int y1, int x2, int y2, int sz=10);
357 void draw_x(int x1, int y1, int sz=2);
358 void draw_t(int x1, int y1, int sz=2);
359 void draw_oval(int x1, int y1, int x2, int y2);
362 // Not integrated with shmem because that only affects codecs
363 VFrameScene* get_scene();
374 // Not integrated with shmem because that only affects codecs
377 // Create a PBuffer matching this frame's dimensions and to be
378 // referenced by this frame. Does nothing if the pbuffer already exists.
379 // If the frame is resized, the PBuffer is deleted.
380 // Called by enable_opengl.
381 // This allows PBuffers, textures, and bitmaps to travel through the entire
382 // rendering chain without requiring the user to manage a lot of objects.
383 // Must be called from a synchronous opengl thread after enable_opengl.
384 void create_pbuffer();
386 int clear_objects(int do_opengl);
387 int reset_parameters(int do_opengl);
388 void create_row_pointers();
389 int allocate_data(unsigned char *data,
397 long bytes_per_line);
399 // Convenience storage
409 // Data pointer is pointing to someone else's buffer.
411 // ID of shared memory if using IPC.
412 // The 1st 1 after reboot is 0.
414 // Local setting for shm usage
416 // If not set by user, is calculated from color_model
421 // Pointers to the start of each row
422 unsigned char **rows;
423 // One of the #defines
425 // Allocated space for compressed data
426 long compressed_allocated;
427 // Size of stored compressed image
428 long compressed_size;
429 // Pointers to yuv / rgb planes
430 unsigned char *y, *u, *v;
434 // Pointer to alpha plane
436 // Dimensions of frame
438 // Info for reading png images
439 const unsigned char *image;
442 // For writing discontinuous frames in background rendering
443 long sequence_number;
445 // read status of input frame -1/err, 0/noxfr, 1/ok
449 // State of the current texture
451 // State of the current PBuffer
454 // Location of working image if OpenGL playback
457 ArrayList<char*> prev_effects;
458 ArrayList<char*> next_effects;
462 // Create a frame with the png image
463 class VFramePng : public VFrame {
464 // Read a PNG into the frame with alpha
465 int read_png(const unsigned char *data, long image_size, double xscale, double yscale);
467 VFramePng(unsigned char *png_data, double s=0);
468 VFramePng(unsigned char *png_data, long image_size, double xs=0, double ys=0);
470 static VFrame *vframe_png(int fd, double xs=1, double ys=1);
471 static VFrame *vframe_png(const char *png_path, double xs=1, double ys=1);