1 // effects-weed.h
2 // LiVES (lives-exe)
3 // (c) G. Finch 2005 - 2016 <salsaman@gmail.com>
4 // Released under the GPL 3 or later
5 // see file ../COPYING for licensing details
6 
7 #ifndef HAS_LIVES_EFFECTS_WEED_H
8 #define HAS_LIVES_EFFECTS_WEED_H
9 
10 #define MAX_WEED_STRLEN 65535 // soft limit for LiVES
11 #define MAX_WEED_ELEMENTS 65535 // soft limit for LiVES
12 
13 /// filter apply errors
14 typedef enum {
15   FILTER_SUCCESS = 0,
16   FILTER_ERROR_MISSING_LAYER,
17   FILTER_ERROR_BLANK_FRAME,
18   FILTER_ERROR_MISSING_FRAME,
19   FILTER_ERROR_INVALID_PALETTE_CONVERSION,
20   FILTER_ERROR_UNABLE_TO_RESIZE,
21   FILTER_ERROR_COULD_NOT_REINIT,
22   FILTER_ERROR_INVALID_PLUGIN,
23   FILTER_ERROR_NEEDS_REINIT,   // TODO
24   FILTER_ERROR_NO_IN_CHANNELS,
25   FILTER_ERROR_INVALID_TRACK,
26   FILTER_ERROR_INTERPOLATION_FAILED,
27   FILTER_ERROR_INVALID_INSTANCE,
28   FILTER_ERROR_INVALID_FILTER,
29   FILTER_ERROR_INVALID_INIT_EVENT,
30   FILTER_ERROR_IS_AUDIO,
31   FILTER_ERROR_IS_SCRAP_FILE,
32   FILTER_ERROR_MISSING_CHANNEL,
33   FILTER_ERROR_TEMPLATE_MISMATCH,
34   FILTER_ERROR_MEMORY_ERROR,
35   FILTER_ERROR_DONT_THREAD,
36   FILTER_ERROR_COPYING_FAILED,
37   FILTER_ERROR_BUSY,
38 
39   /// values >= 512 are info
40   FILTER_INFO_REINITED = 512,
41   FILTER_INFO_REDRAWN
42 } lives_filter_error_t;
43 
44 typedef enum {
45   FX_LIST_NAME, // just name
46   FX_LIST_EXTENDED_NAME, // name + author (if dupe) + subcat + observations
47   FX_LIST_HASHNAME, // hashnames - (packagefilterauthor) author and not extra_authors
48 } lives_fx_list_t;
49 
50 // set some custom Weed values
51 
52 #define WEED_FLAG_HOST_READONLY (1 << 16)
53 
54 // plugin specific values
55 #define WEED_LEAF_HOST_SUSPICIOUS "host_suspicious" // plugin is badly behaved
56 
57 // internal values
58 #define WEED_LEAF_HOST_AUDIO_PLAYER "host_audio_player" // exported to plugins
59 
60 #define WEED_LEAF_HOST_ORIG_PDATA "host_orig_pdata" // set if we "steal" an alpha channel to chain
61 #define WEED_LEAF_HOST_MENU_HIDE "host_menu_hide" // hide from menus
62 #define WEED_LEAF_HOST_DEFAULT "host_default" // user set default
63 #define WEED_LEAF_HOST_WIDTH "host_width" // user set width
64 #define WEED_LEAF_HOST_HEIGHT "host_height" // user set height
65 #define WEED_LEAF_HOST_FPS "host_fps" // user set fps
66 #define WEED_LEAF_HOST_TAG "host_tag" // internal key mapping (for higher keys)
67 #define WEED_LEAF_HOST_KEY "host_key" // internal key mapping
68 #define WEED_LEAF_HOST_MODE "host_mode" // internal mode mapping
69 #define WEED_LEAF_HOST_INPLACE "host_inplace" // inplace effect
70 #define WEED_LEAF_HOST_DISABLED "host_disabled" // channel is disabled
71 #define WEED_LEAF_HOST_TEMP_DISABLED "host_temp_dis" // channel is temp disabled
72 #define WEED_LEAF_HOST_REFS "host_refs" // host ref counting
73 #define WEED_LEAF_HOST_REPEATS "host_repeats" // host channel repeats
74 #define WEED_LEAF_HOST_INITED "host_inited" // inited or not
75 #define WEED_LEAF_HOST_PLUGIN_PATH "host_plugin_path" // plugin path
76 #define WEED_LEAF_HOST_HANDLE "host_handle" // dll handle
77 #define WEED_LEAF_HOST_FILTER_LIST "host_filter_list" // host usable filters
78 #define WEED_LEAF_HOST_NORECORD "host_norecord" // do not record parameter changes for this instance
79 #define WEED_LEAF_DUPLICATE "host_duplicate"
80 #define WEED_LEAF_HOST_REINITING "host_reiniting"
81 #define WEED_LEAF_HOST_UNUSED "host_unused"
82 
83 #define WEED_LEAF_HOST_INSTANCE "host_instance" // special value for text widgets
84 #define WEED_LEAF_HOST_IDX "host_idx" // special value for text widgets
85 
86 #define WEED_LEAF_FREED_PLANTS "host_freed_plants" // list of freed pointers to avoid freeing dupes during unload
87 
88 #define WEED_LEAF_HOST_SCRAP_FILE_OFFSET "scrap_offset" // special value for scrap_file
89 
90 #define WEED_LEAF_HOST_IDENTIFIER "host_unique_id"
91 
92 #define WEED_LEAF_HOST_EASING_END "host_easing_end"
93 #define WEED_LEAF_HOST_EASE_OUT_COUNT "host_ease_out_count"
94 #define WEED_LEAF_AUTO_EASING "host_auto_easing"
95 
96 #define WEED_LEAF_RFX_STRINGS "layout_rfx_strings"
97 #define WEED_LEAF_RFX_DELIM "layout_rfx_delim"
98 
99 #define WEED_LEAF_HOST_PLUGIN_NAME "host_plugin_name"
100 
101 // compound plugins
102 #define WEED_LEAF_HOST_INTERNAL_CONNECTION "host_internal_connection" // for chain plugins
103 #define WEED_LEAF_HOST_INTERNAL_CONNECTION_AUTOSCALE "host_internal_connection_autoscale" // for chain plugins
104 #define WEED_LEAF_HOST_NEXT_INSTANCE "host_next_instance" // for chain plugins
105 #define WEED_LEAF_HOST_COMPOUND_CLASS "host_compound_class" // for chain plugins
106 #define WEED_LEAF_HOST_CHANNEL_CONNECTION "host_channel_connection" // special value for text widgets
107 
108 // custom leaf flags, may evolve
109 #define LIVES_FLAG_MAINTAIN_VALUE (1 << 16) ///< soft flag, like immutable / deletable for host
110 
111 weed_plant_t *get_weed_filter(int filter_idx); // TODO: make const
112 char *weed_filter_idx_get_package_name(int filter_idx) WARN_UNUSED;
113 char *weed_filter_idx_get_name(int idx, boolean add_subcats, boolean add_notes) WARN_UNUSED;
114 char *weed_instance_get_filter_name(weed_plant_t *inst, boolean get_compound_parent) WARN_UNUSED;
115 char *make_weed_hashname(int filter_idx, boolean fullname,
116                          boolean use_extra_authors, char sep, boolean spc_to_underscore) WARN_UNUSED;  ///< fullname includes author and version
117 int weed_get_idx_for_hashname(const char *hashname, boolean fullname) GNU_CONST;  ///< fullname includes author and version
118 int *weed_get_indices_from_template(const char *package_name, const char *filter_name, const char *author, int version);
119 int weed_filter_highest_version(const char *pkg, const char *fxname, const char *auth, int *return_version);
120 int enabled_in_channels(weed_plant_t *plant, boolean count_repeats);
121 int enabled_out_channels(weed_plant_t *plant, boolean count_repeats);
122 weed_plant_t *get_enabled_channel(weed_plant_t *inst, int which, boolean is_in);  ///< for FILTER_INST
123 weed_plant_t *get_enabled_audio_channel(weed_plant_t *inst, int which, boolean is_in);  ///< for FILTER_INST
124 weed_plant_t *get_mandatory_channel(weed_plant_t *filter, int which, boolean is_in);  ///< for FILTER_CLASS
125 boolean weed_instance_is_resizer(weed_plant_t *filt);
126 weed_plant_t *weed_instance_get_filter(weed_plant_t *inst, boolean get_compound_parent);
127 
128 #define PLUGIN_COMPOUND_EFFECTS_BUILTIN "effects/compound/"
129 #define PLUGIN_COMPOUND_EFFECTS_CUSTOM "plugins/effects/compound/"
130 
131 int num_compound_fx(weed_plant_t
132                     *plant); ///< return number of filters in a compound fx (1 if it is not compound) - works for filter or inst
133 
134 boolean has_non_alpha_palette(weed_plant_t *ctmpl, weed_plant_t *filter);
135 boolean has_alpha_palette(weed_plant_t *ctmpl, weed_plant_t *filter);
136 
137 boolean is_audio_channel_in(weed_plant_t *inst, int chnum);
138 boolean has_video_chans_in(weed_plant_t *filter, boolean count_opt);
139 boolean has_audio_chans_in(weed_plant_t *filter, boolean count_opt);
140 boolean is_audio_channel_out(weed_plant_t *inst, int chnum);
141 boolean has_video_chans_out(weed_plant_t *filter, boolean count_opt);
142 boolean has_audio_chans_out(weed_plant_t *filter, boolean count_opt);
143 boolean is_pure_audio(weed_plant_t *filter_or_instance, boolean count_opt); ///< TRUE if audio in or out and no vid in/out
144 
145 boolean has_video_filters(boolean analysers_only);
146 
147 #ifdef HAS_LIVES_EFFECTS_H
148 lives_fx_cat_t weed_filter_categorise(weed_plant_t *pl, int in_channels, int out_channels);
149 lives_fx_cat_t weed_filter_subcategorise(weed_plant_t *pl, lives_fx_cat_t category, boolean count_opt);
150 boolean has_audio_filters(lives_af_t af_type);
151 #endif
152 
153 boolean has_usable_palette(weed_plant_t *chantmpl);
154 int best_palette_match(int *palete_list, int num_palettes, int palette);
155 
156 // instances
157 weed_error_t weed_call_init_func(weed_plant_t *instance);
158 weed_error_t weed_call_deinit_func(weed_plant_t *instance);
159 lives_filter_error_t run_process_func(weed_plant_t *instance, weed_timecode_t tc, int key);
160 
161 char *cd_to_plugin_dir(weed_plant_t *filter);
162 boolean weed_init_effect(int hotkey); ///< hotkey starts at 1
163 boolean  weed_deinit_effect(int hotkey); ///< hotkey starts at 1
164 weed_plant_t *weed_instance_from_filter(weed_plant_t *filter);
165 int _wood_instance_ref(weed_plant_t *inst);
166 int _wood_instance_unref(weed_plant_t *inst);
167 weed_plant_t *_wood_instance_obtain(int line, char *file, int key, int mode);
168 void weed_in_parameters_free(weed_plant_t *inst);
169 void weed_in_params_free(weed_plant_t **parameters, int num_parameters);
170 void add_param_connections(weed_plant_t *inst);
171 lives_filter_error_t weed_reinit_effect(weed_plant_t *inst, boolean reinit_compound);
172 void weed_reinit_all(void);
173 
174 int weed_flagset_array_count(weed_plant_t **array, boolean set_readonly);
175 
176 int num_alpha_channels(weed_plant_t *filter, boolean out);
177 
178 int num_in_params(weed_plant_t *, boolean skip_hidden, boolean skip_internal);
179 int num_out_params(weed_plant_t *);
180 weed_plant_t *weed_inst_in_param(weed_plant_t *inst, int param_num, boolean skip_hidden, boolean skip_internal);
181 weed_plant_t *weed_inst_out_param(weed_plant_t *inst, int param_num);
182 weed_plant_t *weed_filter_in_paramtmpl(weed_plant_t *filter, int param_num, boolean skip_internal);
183 weed_plant_t *weed_filter_out_paramtmpl(weed_plant_t *filter, int param_num);
184 boolean is_hidden_param(weed_plant_t *, int i);
185 int get_nth_simple_param(weed_plant_t *, int pnum);
186 int count_simple_params(weed_plant_t *);
187 weed_plant_t **weed_params_create(weed_plant_t *filter, boolean in);
188 int get_transition_param(weed_plant_t *filter, boolean skip_internal);
189 int get_master_vol_param(weed_plant_t *filter, boolean skip_internal);
190 boolean is_perchannel_multiw(weed_plant_t *param);
191 boolean has_perchannel_multiw(weed_plant_t *filter);
192 boolean weed_parameter_has_variable_elements_strict(weed_plant_t *inst, weed_plant_t *ptmpl);
193 
194 /// parameter interpolation
195 boolean interpolate_param(weed_plant_t *param, void *pchain, ticks_t tc);
196 boolean interpolate_params(weed_plant_t *inst, void **pchains, ticks_t tc);
197 
198 int filter_mutex_lock(int key);  // 0 based key
199 int filter_mutex_trylock(int key);  // 0 based key
200 int filter_mutex_unlock(int key); // 0 based key
201 
202 size_t weed_plant_serialise(int fd, weed_plant_t *plant, unsigned char **mem);
203 weed_plant_t *weed_plant_deserialise(int fd, unsigned char **mem, weed_plant_t *plant);
204 
205 /// record a parameter value change in our event_list
206 void rec_param_change(weed_plant_t *inst, int pnum);
207 
208 // copy values for "copy_value_to" params
209 int set_copy_to(weed_plant_t *inst, int pnum, lives_rfx_t *rfx, boolean update);
210 
211 weed_plant_t *get_textparm();
212 
213 void weed_set_blend_factor(int hotkey);  // 0 based key
214 int weed_get_blend_factor(int hotkey); // 0 based key
215 
216 void weed_functions_init(void); ///< call weed_init() to set our weed core functions
217 
218 void weed_load_all(void);  ///< load effects
219 void weed_unload_all(void); ///< unload all effects
220 int get_next_free_key(void); ///< next free "key" for the multitrack system
221 
222 void weed_deinit_all(boolean shutdown); ///< deinit all active effects
223 
224 weed_plant_t *weed_apply_effects(weed_plant_t **layers, weed_plant_t *filter_map, ticks_t tc, int opwidth, int opheight,
225                                  void ***pchains);
226 lives_filter_error_t weed_apply_instance(weed_plant_t *inst, weed_plant_t *init_event, weed_plant_t **layers,
227     int opwidth, int opheight, ticks_t tc);
228 void weed_apply_audio_effects(weed_plant_t *filter_map, weed_layer_t **, int nbtracks, int nchans, int64_t nsamps, double arate,
229                               ticks_t tc, double *vis);
230 void weed_apply_audio_effects_rt(weed_layer_t *alayer, ticks_t tc, boolean analysers_only, boolean is_audio_thread);
231 
232 lives_filter_error_t weed_apply_audio_instance(weed_plant_t *init_event, weed_layer_t **layers, int nbtracks, int nchans,
233     int64_t nsamps,
234     double arate, ticks_t tc, double *vis);
235 
236 int weed_generator_start(weed_plant_t *inst, int key);  // 0 based key
237 void weed_generator_end(weed_plant_t *inst);
238 boolean weed_playback_gen_start(void);
239 void weed_bg_generator_end(weed_plant_t *inst);
240 void wge_inner(weed_plant_t *inst); ///< deinit and instance(s) for generator, reset instance mapping
241 
242 // layers
243 weed_error_t weed_leaf_copy_or_delete(weed_layer_t *dlayer, const char *key, weed_layer_t *slayer);
244 weed_plant_t *weed_layer_create_from_generator(weed_plant_t *inst, ticks_t tc, int clipno);
245 
246 /// for multitrack
247 void backup_weed_instances(void);
248 void restore_weed_instances(void);
249 
250 //////////////////////////////////////////////////////////
251 // WARNING !! "key" here starts at 1, "mode" starts at 0
252 
253 boolean rte_key_valid(int key, boolean is_userkey);  ///< returns TRUE if there is a filter bound to active mode of hotkey
254 boolean rte_keymode_valid(int key, int mode,
255                           boolean is_userkey);  ///< returns TRUE if a filter_class is bound to key/mode, is_userkey should be
256 ///< set to TRUE
257 int rte_keymode_get_filter_idx(int key, int mode); ///< returns filter_class index of key/mode (or -1 if no filter bound)
258 char *rte_keymode_get_filter_name(int key, int mode,
259                                   boolean add_notes) WARN_UNUSED;  ///< returns name of filter_class bound to key/mode (or "")
260 char *rte_keymode_get_plugin_name(int key,
261                                   int mode) WARN_UNUSED; ///< returns name of plugin package containing filter_class (or "")
262 char *rte_keymode_get_type(int key, int mode) WARN_UNUSED;  ///< returns a string filter/instance type (or "")
263 
264 #ifdef HAS_LIVES_EFFECTS_H
265 lives_fx_cat_t rte_keymode_get_category(int key, int mode);
266 #endif
267 
268 weed_plant_t *rte_keymode_get_instance(int key, int mode); ///< returns refcounted filter_instance bound to key/mode (or NULL)
269 weed_plant_t *rte_keymode_get_filter(int key, int mode); ///< returns filter_class bound to key/mode (or NULL)
270 
271 boolean weed_delete_effectkey(int key, int mode);  ///< unbinds a filter_class from a key/mode
272 int weed_add_effectkey(int key, const char *hashname,
273                        boolean fullname);  ///< bind a filter_class to key/mode using its hashname
274 
275 int weed_add_effectkey_by_idx(int key, int idx);  ///< see description
276 
277 int rte_key_getmode(int key);  ///< returns current active mode for a key (or -1)
278 int rte_key_getmaxmode(int key); ///< returns highest mode which is set
279 
280 weed_plant_t *get_new_inst_for_keymode(int key, int mode); ///< get new refcounted inst (during recording playback)
281 
282 boolean rte_key_setmode(int key, int newmode);  ///< set mode for a given key; if key==0 then the active key is used
283 
284 ///< returns -1 if the filter is not found; it will match the first name found - returns -2 if you try to switch a generator/non-generator
285 int rte_switch_keymode(int key, int mode, const char *hashname);
286 
287 /////////////////////////////////////////////////////////////
288 
289 LiVESList *weed_get_all_names(lives_fx_list_t list_type);
290 int rte_get_numfilters(void);
291 int weed_get_sorted_filter(int i);
292 
293 /////////////////////////////////////////////////////////
294 // key starts at 0
295 
296 void free_key_defaults(int key, int mode);
297 void apply_key_defaults(weed_plant_t *inst, int key, int mode);
298 void write_key_defaults(int fd, int key, int mode);
299 boolean read_key_defaults(int fd, int nparams, int key, int mode, int version);
300 void set_key_defaults(weed_plant_t *inst, int key, int mode);
301 boolean has_key_defaults(void);
302 
303 //////////////////////////////////////////////////////
304 // 0 based keys
305 void rte_swap_fg_bg(void);
306 
307 int rte_bg_gen_key(void) GNU_PURE;
308 
309 int rte_fg_gen_key(void) GNU_PURE;
310 
311 int rte_bg_gen_mode(void) GNU_PURE;
312 int rte_fg_gen_mode(void) GNU_PURE;
313 
314 ////////////////////////////////////////////////////////////////////////
315 
316 char *get_weed_display_string(weed_plant_t *inst, int pnum);
317 weed_plant_t *add_filter_deinit_events(weed_plant_t *event_list);
318 weed_plant_t *add_filter_init_events(weed_plant_t *event_list, ticks_t tc);
319 void deinit_render_effects(void);
320 void deinit_easing_effects(void);
321 
322 boolean write_filter_defaults(int fd, int idx);
323 boolean read_filter_defaults(int fd);
324 
325 boolean write_generator_sizes(int fd, int idx);
326 boolean read_generator_sizes(int fd);
327 
328 //void set_param_gui_readwrite(weed_plant_t *inst);
329 //void set_param_gui_readonly(weed_plant_t *inst);
330 
331 void update_all_host_info(void);
332 
333 /// add default filler values to a parameter or pchange.
334 void fill_param_vals_to(weed_plant_t *param, weed_plant_t *ptmpl, int fill_slot);
335 
336 //#define DEBUG_FILTER_MUTEXES
337 #ifdef DEBUG_FILTER_MUTEXES
338 #define filter_mutex_lock(key) {g_print ("lock %d at line %d in file %s\n",key,__LINE__,__FILE__); \
339     if (key >= 0 && key < FX_KEYS_MAX) pthread_mutex_lock(&mainw->fx_mutex[key]); g_print("done\n");}
340 #define filter_mutex_unlock(key) {g_print ("unlock %d at line %d in file %s\n\n",key,__LINE__,__FILE__); \
341     if (key >= 0 && key < FX_KEYS_MAX) pthread_mutex_unlock(&mainw->fx_mutex[key]); g_print("done\n");}
342 #endif
343 
344 //#define DEBUG_REFCOUNT
345 #ifdef DEBUG_REFCOUNT
346 #define weed_instance_ref(a) {g_print ("ref %p at line %d in file %s\n",a,__LINE__,__FILE__); _weed_instance_ref(a);}
347 #define weed_instance_unref(a) {g_print ("unref %p at line %d in file %s\n",a,__LINE__,__FILE__); _weed_instance_unref(a);}
348 #define weed_instance_obtain(a,b) _weed_instance_obtain(__LINE__, __FILE__, a, b)
349 #endif
350 
351 int _weed_instance_ref(weed_plant_t *inst);
352 int _weed_instance_unref(weed_plant_t *inst);
353 weed_plant_t *_weed_instance_obtain(int line, char *file, int key, int mode);
354 
355 #ifndef DEBUG_REFCOUNT
356 int weed_instance_ref(weed_plant_t *inst);
357 int weed_instance_unref(weed_plant_t *inst);
358 weed_plant_t *weed_instance_obtain(int key, int mode);
359 #endif
360 
361 #define WEED_ERROR_NOSUCH_PLANT 65536
362 
363 weed_plant_t *host_info_cb(weed_plant_t *xhost_info, void *data);
364 
365 weed_error_t weed_leaf_set_host(weed_plant_t *plant, const char *key, uint32_t seed_type, weed_size_t num_elems, void *value);
366 weed_error_t weed_leaf_delete_host(weed_plant_t *plant, const char *key);
367 weed_error_t weed_plant_free_host(weed_plant_t *plant);
368 weed_plant_t *weed_plant_new_host(int type);
369 //weed_error_t weed_leaf_get_monitor(weed_plant_t *plant, const char *key, int32_t idx, void *value);
370 
371 void show_weed_stats(weed_plant_t *statsplant);
372 
373 #endif
374