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"
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
45 // Scene graph for 3D models
46 // Defined by the subclass
51 virtual ~VFrameScene();
58 friend class VFramePng;
59 friend class PngReadFunction;
61 // Create new frame with shared data if *data is nonzero.
62 // Pass 0 to *data & -1 to shmid if private data is desired.
66 long bytes_per_line = -1);
67 VFrame(unsigned char *data,
71 int color_model /* = BC_RGBA8888 */,
72 long bytes_per_line /* = -1 */);
73 VFrame(unsigned char *data, // 0
80 int color_model, /* = BC_RGBA8888 */
81 long bytes_per_line /* = -1 */);
82 VFrame(BC_Bitmap *bitmap,
88 VFrame(VFrame &vframe);
89 // Create new frame for compressed data.
93 // Return 1 if the colormodel and dimensions are the same
95 int equivalent(VFrame *src, int test_stacks = 0);
97 // Reallocate a frame without deleting the class
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
107 long bytes_per_line); // -1 if unused
109 void set_memory(unsigned char *data,
114 void set_memory(BC_Bitmap *bitmap);
116 void set_compressed_memory(unsigned char *data,
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);
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);
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; }
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());
151 int allocate_compressed_data(long bytes);
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; }
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; }
168 // return an array of pointers to rows
169 unsigned char** get_rows() { return rows; }
170 int get_memory_usage();
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; }
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();
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);
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);
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.
227 // Transfer from PBuffer to RAM.
228 // used in Playback3D::overlay_sync, plugin Overlay::handle_opengl
229 void screen_to_ram();
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);
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,
246 // Draw the texture using the frame's size as the input and output coordinates.
247 void draw_texture(int flip_y = 0);
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);
265 // OpenGL image is in RAM
267 // OpenGL image is in texture
269 // OpenGL image is composited in PBuffer or back buffer
274 int get_texture_id();
275 void set_texture_id(int id);
276 // Get window ID the texture is bound to
280 int get_texture_components();
283 // Binds the opengl context to this frame's PBuffer
284 void enable_opengl();
286 // Clears the pbuffer with the right values depending on YUV
287 void clear_pbuffer();
290 BC_PBuffer* get_pbuffer();
292 // Bind the frame's texture to GL_TEXTURE_2D and enable it.
293 void bind_texture(int texture_unit, int nearest=0);
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.
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();
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);
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);
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();
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; }
344 // Compare stacks and params from 2 images and return 1 if equal.
345 int equal_stacks(VFrame *src);
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
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;
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);
375 // Not integrated with shmem because that only affects codecs
376 VFrameScene* get_scene();
387 // Not integrated with shmem because that only affects codecs
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();
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,
410 long bytes_per_line);
412 // Convenience storage
422 // Data pointer is pointing to someone else's buffer.
424 // ID of shared memory if using IPC.
425 // The 1st 1 after reboot is 0.
427 // Local setting for shm usage
429 // If not set by user, is calculated from color_model
434 // Pointers to the start of each row
435 unsigned char **rows;
436 // One of the #defines
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;
447 // Pointer to alpha plane
449 // Dimensions of frame
451 // color used by clear_frame
452 int clear_color, clear_alpha;
453 // Info for reading png images
454 const unsigned char *image;
457 // For writing discontinuous frames in background rendering
458 long sequence_number;
460 // read status of input frame -1/err, 0/noxfr, 1/ok
464 // State of the current texture
466 // State of the current PBuffer
469 // Location of working image if OpenGL playback
472 ArrayList<char*> prev_effects;
473 ArrayList<char*> next_effects;
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);
482 VFramePng(unsigned char *png_data, double s=0);
483 VFramePng(unsigned char *png_data, long image_size, double xs=0, double ys=0);
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);