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 VFrameJpeg;
60 friend class PngReadFunction;
62 // Create new frame with shared data if *data is nonzero.
63 // Pass 0 to *data & -1 to shmid if private data is desired.
67 long bytes_per_line = -1);
68 VFrame(unsigned char *data,
72 int color_model /* = BC_RGBA8888 */,
73 long bytes_per_line /* = -1 */);
74 VFrame(unsigned char *data, // 0
81 int color_model, /* = BC_RGBA8888 */
82 long bytes_per_line /* = -1 */);
83 VFrame(BC_Bitmap *bitmap,
89 VFrame(VFrame &vframe);
90 // Create new frame for compressed data.
94 // Return 1 if the colormodel and dimensions are the same
96 int equivalent(VFrame *src, int test_stacks = 0);
98 // Reallocate a frame without deleting the class
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
108 long bytes_per_line); // -1 if unused
110 void set_memory(unsigned char *data,
115 void set_memory(BC_Bitmap *bitmap);
117 void set_compressed_memory(unsigned char *data,
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);
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);
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; }
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());
152 int allocate_compressed_data(long bytes);
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; }
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; }
169 // return an array of pointers to rows
170 unsigned char** get_rows() { return rows; }
171 int get_memory_usage();
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; }
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();
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);
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);
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.
228 // Transfer from PBuffer to RAM.
229 // used in Playback3D::overlay_sync, plugin Overlay::handle_opengl
230 void screen_to_ram();
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);
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,
247 // Draw the texture using the frame's size as the input and output coordinates.
248 void draw_texture(int flip_y = 0);
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);
266 // OpenGL image is in RAM
268 // OpenGL image is in texture
270 // OpenGL image is composited in PBuffer or back buffer
275 int get_texture_id();
276 void set_texture_id(int id);
277 // Get window ID the texture is bound to
281 int get_texture_components();
284 // Binds the opengl context to this frame's PBuffer
285 void enable_opengl();
287 // Clears the pbuffer with the right values depending on YUV
288 void clear_pbuffer();
291 BC_PBuffer* get_pbuffer();
293 // Bind the frame's texture to GL_TEXTURE_2D and enable it.
294 void bind_texture(int texture_unit, int nearest=0);
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.
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();
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);
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);
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();
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; }
345 // Compare stacks and params from 2 images and return 1 if equal.
346 int equal_stacks(VFrame *src);
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
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;
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);
376 // Not integrated with shmem because that only affects codecs
377 VFrameScene* get_scene();
388 // Not integrated with shmem because that only affects codecs
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();
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,
411 long bytes_per_line);
413 // Convenience storage
423 // Data pointer is pointing to someone else's buffer.
425 // ID of shared memory if using IPC.
426 // The 1st 1 after reboot is 0.
428 // Local setting for shm usage
430 // If not set by user, is calculated from color_model
435 // Pointers to the start of each row
436 unsigned char **rows;
437 // One of the #defines
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;
448 // Pointer to alpha plane
450 // Dimensions of frame
452 // color used by clear_frame
453 int clear_color, clear_alpha;
454 // Info for reading png images
455 const unsigned char *image;
458 // For writing discontinuous frames in background rendering
459 long sequence_number;
461 // read status of input frame -1/err, 0/noxfr, 1/ok
465 // State of the current texture
467 // State of the current PBuffer
470 // Location of working image if OpenGL playback
473 ArrayList<char*> prev_effects;
474 ArrayList<char*> next_effects;
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);
483 VFramePng(unsigned char *png_data, double s=0);
484 VFramePng(unsigned char *png_data, long image_size, double xs=0, double ys=0);
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);
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
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);
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);