fix possible segf in median filter

refactor fx init / deinit
This commit is contained in:
niels
2012-06-19 06:42:06 +02:00
parent f1d60dbbd4
commit 2c6efc4b51
10 changed files with 177 additions and 140 deletions

View File

@@ -1190,6 +1190,7 @@ void livido_plug_deinit( void *instance )
}
livido_port_recursive_free( instance );
instance = NULL;
}
//get plugin defaults

View File

@@ -646,6 +646,7 @@ void plug_deactivate( void *instance )
{
if( instance )
deinstantiate_plugin( instance );
}
static int linear_len( char **items )

View File

@@ -1918,7 +1918,7 @@ int sample_chain_free(int s1)
e_id = sample->effect_chain[i]->effect_id;
if(e_id!=-1)
{
if(vj_effect_initialized(e_id) || sample->effect_chain[i]->fx_instance != NULL)
if(vj_effect_initialized(e_id, sample->effect_chain[i]->fx_instance))
{
vj_effect_deactivate(e_id, sample->effect_chain[i]->fx_instance);
sample->effect_chain[i]->fx_instance = NULL;
@@ -2007,9 +2007,9 @@ int sample_chain_add(int s1, int c, int effect_nr)
int effect_params = 0, i;
sample_info *sample = sample_get(s1);
if (!sample)
return -1;
return 0;
if (c < 0 || c >= SAMPLE_MAX_EFFECTS)
return -1;
return 0;
/* if ( effect_nr < VJ_IMAGE_EFFECT_MIN ) return -1;
@@ -2020,30 +2020,32 @@ int sample_chain_add(int s1, int c, int effect_nr)
if( sample->effect_chain[c]->effect_id != -1 && sample->effect_chain[c]->effect_id != effect_nr )
{
//verify if the effect should be discarded
if(vj_effect_initialized( sample->effect_chain[c]->effect_id ))
if(vj_effect_initialized( sample->effect_chain[c]->effect_id, sample->effect_chain[c]->fx_instance ) )
{
// it is using some memory, see if we can free it ...
int i;
int ok = 1;
for(i=(c+1); i < SAMPLE_MAX_EFFECTS; i++)
{
if( sample->effect_chain[i]->effect_id == sample->effect_chain[c]->effect_id) ok = 0;
}
// ok, lets get rid of it.
if( ok ) {
if(!vj_effect_is_plugin( sample->effect_chain[c]->effect_id ) ) {
int i = 0;
int frm = 1;
for( i = 0; i < SAMPLE_MAX_EFFECTS ; i ++ ) {
if( i == c )
continue;
if( sample->effect_chain[i]->effect_id == effect_nr )
frm = 0;
}
if( frm == 1 ) {
vj_effect_deactivate( sample->effect_chain[c]->effect_id, sample->effect_chain[c]->fx_instance );
sample->effect_chain[c]->fx_instance = NULL;
}
} else {
vj_effect_deactivate( sample->effect_chain[c]->effect_id, sample->effect_chain[c]->fx_instance );
sample->effect_chain[c]->fx_instance = NULL;
}
}
else if( sample->effect_chain[c]->fx_instance != NULL && vj_effect_is_plugin( sample->effect_chain[c]->effect_id ) ) {
vj_effect_deactivate( sample->effect_chain[c]->effect_id, sample->effect_chain[c]->fx_instance );
sample->effect_chain[c]->fx_instance = NULL;
}
}
if(!vj_effect_initialized(effect_nr) || vj_effect_is_plugin( effect_nr ) )
if(!vj_effect_initialized(effect_nr, sample->effect_chain[c]->fx_instance) )
{
veejay_msg(VEEJAY_MSG_DEBUG, "Effect %s must be initialized now",
vj_effect_get_description(effect_nr));
@@ -2051,8 +2053,16 @@ int sample_chain_add(int s1, int c, int effect_nr)
sample->effect_chain[c]->fx_instance = vj_effect_activate( effect_nr, &res );
if(!res)
{
veejay_msg(VEEJAY_MSG_ERROR, "Cant activate %d", effect_nr);
return -1;
veejay_msg(VEEJAY_MSG_ERROR, "Cannot activate %d", effect_nr);
//@ clear
sample->effect_chain[c]->effect_id = -1;
sample->effect_chain[c]->e_flag = 1;
int i;
for( i = 0; i < SAMPLE_MAX_PARAMETERS; i ++ )
sample->effect_chain[c]->arg[i] = 0;
sample->effect_chain[c]->frame_trimmer = 0;
return 0;
}
}
@@ -2087,7 +2097,7 @@ int sample_chain_add(int s1, int c, int effect_nr)
}
sample_update(sample,s1);
return c; /* return position on which it was added */
return 1; /* return position on which it was added */
}
int sample_reset_offset(int s1)
@@ -2175,7 +2185,7 @@ int sample_chain_clear(int s1)
for (i = 0; i < SAMPLE_MAX_EFFECTS; i++) {
if(sample->effect_chain[i]->effect_id != -1)
{
if(vj_effect_initialized( sample->effect_chain[i]->effect_id ) || sample->effect_chain[i]->fx_instance != NULL ) {
if(vj_effect_initialized( sample->effect_chain[i]->effect_id, sample->effect_chain[i]->fx_instance )) {
vj_effect_deactivate( sample->effect_chain[i]->effect_id, sample->effect_chain[i]->fx_instance );
sample->effect_chain[i]->fx_instance = NULL;
}
@@ -2260,16 +2270,23 @@ int sample_chain_get_free_entry(int s1)
*
****************************************************************************************************/
static int _sample_can_free(sample_info *sample, int reserved, int effect_id)
{
int i;
if( vj_effect_is_plugin(effect_id ) )
return 1;
for(i=0; i < SAMPLE_MAX_EFFECTS; i++)
{
if(i != reserved && effect_id == sample->effect_chain[i]->effect_id) return 0;
if(i != reserved && effect_id == sample->effect_chain[i]->effect_id)
return 0;
}
return 1;
}
int sample_chain_remove(int s1, int position)
{
int j;
@@ -2281,8 +2298,7 @@ int sample_chain_remove(int s1, int position)
return -1;
if(sample->effect_chain[position]->effect_id != -1)
{
if(vj_effect_initialized( sample->effect_chain[position]->effect_id) &&
_sample_can_free(sample,position, sample->effect_chain[position]->effect_id) || sample->effect_chain[position]->fx_instance != NULL) {
if(vj_effect_initialized( sample->effect_chain[position]->effect_id, sample->effect_chain[position]->fx_instance ) && _sample_can_free( sample, position, sample->effect_chain[position]->effect_id) ) {
vj_effect_deactivate( sample->effect_chain[position]->effect_id, sample->effect_chain[position]->fx_instance);
sample->effect_chain[position]->fx_instance = NULL;
}
@@ -2698,7 +2714,7 @@ void ParseEffect(xmlDocPtr doc, xmlNodePtr cur, int dst_sample, int start_at)
if (effect_id != -1) {
int j;
if (sample_chain_add(dst_sample, chain_index, effect_id) == -1) {
if (!sample_chain_add(dst_sample, chain_index, effect_id)) {
veejay_msg(VEEJAY_MSG_ERROR, "Error parsing effect %d (pos %d)\n",
effect_id, chain_index);
}

View File

@@ -1391,8 +1391,7 @@ int vj_tag_del(int id)
break;
case VJ_TAG_TYPE_GENERATOR:
if( tag->generator ) {
//@ leak it FIXME
// plug_deactivate( tag->generator );
plug_deactivate( tag->generator );
}
tag->generator = NULL;
break;
@@ -2135,18 +2134,17 @@ int vj_tag_chain_malloc(int t1)
for(i=0; i < SAMPLE_MAX_EFFECTS; i++)
{
e_id = tag->effect_chain[i]->effect_id;
if(e_id!=-1)
{
if(!vj_effect_initialized(e_id) || vj_effect_is_plugin( e_id ) )
e_id = tag->effect_chain[i]->effect_id;
if(e_id)
{
sum ++;
int res = 0;
tag->effect_chain[i]->fx_instance = vj_effect_activate(e_id, &res);
if( res )
sum ++;
}
}
}
return sum;
veejay_msg(VEEJAY_MSG_DEBUG, "Allocated %d effects",sum);
return sum;
}
int vj_tag_chain_free(int t1)
@@ -2161,16 +2159,16 @@ int vj_tag_chain_free(int t1)
for(i=0; i < SAMPLE_MAX_EFFECTS; i++)
{
e_id = tag->effect_chain[i]->effect_id;
if(e_id!=-1)
{
if(vj_effect_initialized(e_id) || ( vj_effect_is_plugin(e_id) && tag->effect_chain[i]->fx_instance != NULL ) )
e_id = tag->effect_chain[i]->effect_id;
if(e_id!=-1)
{
vj_effect_deactivate(e_id, tag->effect_chain[i]->fx_instance);
tag->effect_chain[i]->fx_instance = NULL;
sum++;
}
}
if(vj_effect_initialized(e_id, tag->effect_chain[i]->fx_instance) )
{
vj_effect_deactivate(e_id, tag->effect_chain[i]->fx_instance);
tag->effect_chain[i]->fx_instance = NULL;
sum++;
}
}
}
return sum;
}
@@ -2268,39 +2266,53 @@ int vj_tag_set_effect(int t1, int position, int effect_id)
vj_tag *tag = vj_tag_get(t1);
if (!tag)
return -1;
return 0;
if (position < 0 || position >= SAMPLE_MAX_EFFECTS)
return -1;
return 0;
if( tag->effect_chain[position]->effect_id != -1 && tag->effect_chain[position]->effect_id != effect_id )
{
//verify if the effect should be discarded
if(vj_effect_initialized( tag->effect_chain[position]->effect_id ))
if(vj_effect_initialized( tag->effect_chain[position]->effect_id, tag->effect_chain[position]->fx_instance ))
{
// it is using some memory, see if we can free it ...
int ok = 1;
for(i=(position+1); i < SAMPLE_MAX_EFFECTS; i++)
{
if( tag->effect_chain[i]->effect_id == tag->effect_chain[position]->effect_id) ok = 0;
if(!vj_effect_is_plugin( tag->effect_chain[position]->effect_id )) {
int i = 0;
int frm = 1;
for( i =0; i < SAMPLE_MAX_EFFECTS; i ++ ) {
if( i == position )
continue;
if( tag->effect_chain[i]->effect_id == effect_id )
frm = 0;
}
if( frm == 1 ) {
vj_effect_deactivate( tag->effect_chain[position]->effect_id, tag->effect_chain[position]->fx_instance );
tag->effect_chain[position]->fx_instance = NULL;
}
} else {
vj_effect_deactivate( tag->effect_chain[position]->effect_id, tag->effect_chain[position]->fx_instance );
tag->effect_chain[position]->fx_instance = NULL;
}
// ok, lets get rid of it.
if( ok ) {
vj_effect_deactivate( tag->effect_chain[position]->effect_id, tag->effect_chain[position]->fx_instance );
tag->effect_chain[position]->fx_instance = NULL;
}
}
else if ( vj_effect_is_plugin( tag->effect_chain[position]->effect_id ) && tag->effect_chain[position]->fx_instance != NULL ) {
vj_effect_deactivate( tag->effect_chain[position]->effect_id, tag->effect_chain[position]->fx_instance );
tag->effect_chain[position]->fx_instance = NULL;
}
}
if (!vj_effect_initialized(effect_id)||vj_effect_is_plugin( effect_id ))
if (!vj_effect_initialized(effect_id, tag->effect_chain[position]->fx_instance ))
{
int res = 0;
tag->effect_chain[position]->fx_instance = vj_effect_activate( effect_id, &res );
if( res == 0 )
return -1;
veejay_msg(VEEJAY_MSG_DEBUG, "Effect %s must be initialized now",
vj_effect_get_description(effect_id));
int res = 0;
tag->effect_chain[position]->fx_instance = vj_effect_activate( effect_id, &res );
if(!res) {
veejay_msg(VEEJAY_MSG_ERROR, "Cannot activate FX %d", effect_id );
tag->effect_chain[position]->effect_id = -1;
tag->effect_chain[position]->e_flag = 1;
int i;
for( i = 0; i < SAMPLE_MAX_PARAMETERS; i ++ )
tag->effect_chain[position]->arg[i] = 0;
tag->effect_chain[position]->frame_trimmer = 0;
return 0;
}
}
tag->effect_chain[position]->effect_id = effect_id;
@@ -2321,8 +2333,8 @@ int vj_tag_set_effect(int t1, int position, int effect_id)
}
if (!vj_tag_update(tag,t1))
return -1;
return position;
return 0;
return 1;
}
int vj_tag_has_cali_fx( int t1 ) {
@@ -2844,47 +2856,54 @@ int vj_tag_set_selected_entry(int t1, int position)
return (vj_tag_update(tag,t1));
}
static int vj_tag_chain_can_delete(vj_tag *tag, int s_pos, int e_id)
static int vj_tag_chain_can_delete(vj_tag *tag, int reserved, int effect_id)
{
int i;
for(i=0; i < SAMPLE_MAX_EFFECTS;i++)
if( vj_effect_is_plugin(effect_id ) )
return 1;
for(i=0; i < SAMPLE_MAX_EFFECTS; i++)
{
// effect is on chain > 1
if(e_id == tag->effect_chain[i]->effect_id && i != s_pos)
{
if(i != reserved && effect_id == tag->effect_chain[i]->effect_id)
return 0;
}
}
return 1;
}
int vj_tag_chain_remove(int t1, int index)
{
int i;
vj_tag *tag = vj_tag_get(t1);
if (!tag)
return -1;
return -1;
if( tag->effect_chain[index]->effect_id != -1)
{
if( vj_effect_initialized( tag->effect_chain[index]->effect_id ) &&
vj_tag_chain_can_delete(tag, index, tag->effect_chain[index]->effect_id) || vj_effect_is_plugin( tag->effect_chain[index]->effect_id) )
if( vj_effect_initialized( tag->effect_chain[index]->effect_id, tag->effect_chain[index]->fx_instance ) && vj_tag_chain_can_delete( tag, index, tag->effect_chain[index]->effect_id ) )
{
vj_effect_deactivate( tag->effect_chain[index]->effect_id, tag->effect_chain[index]->fx_instance );
tag->effect_chain[index]->fx_instance = NULL;
}
if( tag->effect_chain[index]->kf )
vpf(tag->effect_chain[index]->kf );
tag->effect_chain[index]->kf = vpn(VEVO_ANONYMOUS_PORT);
}
if (tag->effect_chain[index]->effect_id != -1) {
tag->effect_chain[index]->effect_id = -1;
tag->effect_chain[index]->e_flag = 0;
for (i = 0; i < SAMPLE_MAX_PARAMETERS; i++) {
vj_tag_set_effect_arg(t1, index, i, 0);
}
}
tag->effect_chain[index]->effect_id = -1;
tag->effect_chain[index]->e_flag = 0;
if( tag->effect_chain[index]->kf )
vpf(tag->effect_chain[index]->kf );
tag->effect_chain[index]->kf = vpn(VEVO_ANONYMOUS_PORT);
tag->effect_chain[index]->source_type = 0;
tag->effect_chain[index]->channel = 0;
int j;
for (j = 0; j < SAMPLE_MAX_PARAMETERS; j++)
tag->effect_chain[index]->arg[j] = 0;
if (!vj_tag_update(tag,t1))
return -1;
return -1;
return 1;
}

View File

@@ -32,7 +32,7 @@ vj_effect *medianfilter_init(int w, int h)
ve->limits[0] = (int *) vj_calloc(sizeof(int) * ve->num_params); /* min */
ve->limits[1] = (int *) vj_calloc(sizeof(int) * ve->num_params); /* max */
ve->limits[0][0] = 0;
ve->limits[1][0] = 127;
ve->limits[1][0] = (h / 4) - 1;
ve->defaults[0] = 3;// 255;
ve->description = "Constant Time Median Filter";
ve->sub_format = 0;

View File

@@ -192,6 +192,8 @@ void set_pixel_range(uint8_t Yhi,uint8_t Uhi, uint8_t Ylo, uint8_t Ulo)
pixel_Y_lo_ = Ulo;
}
static void *__plugin_ref = NULL;
static struct
{
int (*mem_init)(int width, int height);
@@ -324,16 +326,33 @@ int vj_effect_is_plugin( int effect_id )
return 0;
}
int vj_effect_initialized(int effect_id)
int vj_effect_initialized(int effect_id, void *instance_ptr )
{
int seq = vj_effect_real_to_sequence(effect_id);
#ifdef STRICT_CHECKNIG
if( seq < 0 ) {
veejay_msg(0, "FX %d, ptr %p has seq %d", effect_id, instance_ptr , seq);
assert( seq >= 0 );
}
#endif
if( seq < 0 )
return 0;
if( _no_mem_required(effect_id) || vj_effect_ready[seq] == 1 )
if( seq >= MAX_EFFECTS && seq < (MAX_EFFECTS + n_ext_plugs_)) {
//@ is plugin
if( instance_ptr == NULL ) {
return 0;
}
return 1;
} else if( seq < MAX_EFFECTS ) { //@ veejay internal FX
if( _no_mem_required(effect_id) || vj_effect_ready[seq] == 1 ) {
return 1;
}
}
#ifdef STRICT_CHECKING
assert( vj_effect_ready[seq] == 0 );
#endif
return 0;
}
@@ -349,7 +368,6 @@ static void *vj_effect_activate_ext( int fx_id, int *result )
void *plug = plug_activate( fx_id - MAX_EFFECTS );
if(plug)
{
// vj_effect_ready[fx_id] = 1;
*result = 1;
return plug;
}
@@ -449,44 +467,32 @@ int vj_effect_deactivate(int effect_id, void *ptr)
// if( vj_effect_ready[seq] == 1 )
// {
if( seq >= MAX_EFFECTS && seq < (n_ext_plugs_ + MAX_EFFECTS))
{
if(ptr)
{
plug_deactivate( ptr );
ptr = NULL;
return 1;
}
return 0;
}
if( vj_effect_ready[seq] == 1 ) {
int index = _get_simple_effect(effect_id);
if(index==-1)
{
index = _get_complex_effect(effect_id);
if(index == -1)
{
return 0;
} else if ( seq < MAX_EFFECTS ) {
if( vj_effect_ready[seq] == 1 ) {
int index = _get_simple_effect(effect_id);
if(index==-1) {
index = _get_complex_effect(effect_id);
if(index == -1)
{
return 0;
}
complex_effect_index[index].free( vj_effects[seq]->user_data );
vj_effect_ready[seq] = 0;
veejay_msg(VEEJAY_MSG_DEBUG, "Deactivated complex effect %s", vj_effects[seq]->description);
return 1;
}
complex_effect_index[index].free( vj_effects[seq]->user_data );
simple_effect_index[index].free();
vj_effect_ready[seq] = 0;
veejay_msg(VEEJAY_MSG_DEBUG, "Deactivated complex effect %s",
vj_effects[seq]->description);
veejay_msg(VEEJAY_MSG_DEBUG, "Deactivated simple effect %s", vj_effects[seq]->description);
return 1;
}
simple_effect_index[index].free( );
vj_effect_ready[seq] = 0;
veejay_msg(VEEJAY_MSG_DEBUG, "Deactivated simple effect %s", vj_effects[seq]->description);
return 1;
}
if( vj_effect_ready[seq] == 0 )
{
return 1;
}
return 0;

View File

@@ -705,15 +705,11 @@ int vj_effect_apply( VJFrame **frames, VJFrameInfo *frameinfo, vjp_kf *kf, int s
if( !frames || !frames[0] ) return VJE_NO_FRAMES;
if( !vj_effect_initialized( selector ) && !vj_effect_is_plugin(selector) ) {
if( !vj_effect_initialized( selector, ptr ) ) {
veejay_msg(0, "FX REQUIRES INTIALIZATION DURING PROCESS RUN");
return VJE_NEED_INIT;
}
// if(!vj_effect_initialized(selector) && !vj_effect_is_plugin( selector ))
// {
// return VJE_NEED_INIT;
// }
if( selector >= 500 ) {
vj_effman_apply_plug_effect( frames, frameinfo, kf, arguments,n_a, entry, selector, ptr );
}

View File

@@ -115,7 +115,7 @@ extern int vj_effect_get_summary(int entry, char *dst);
extern int vj_effect_get_summary_len(int entry);
extern void *vj_effect_activate(int e, int *retcode);
extern int vj_effect_deactivate(int e, void *ptr);
extern int vj_effect_initialized(int e);
extern int vj_effect_initialized(int e, void *ptr);
extern int vj_effect_get_min_i();
extern int vj_effect_get_max_i();
extern int vj_effect_get_min_v();

View File

@@ -3026,7 +3026,7 @@ veejay_t *veejay_malloc()
if(!info->seq)
return NULL;
info->seq->samples = (int*) vj_calloc(sizeof(int) * MAX_SEQUENCES );
info->seq->samples = (int*) vj_calloc(sizeof(int) * (MAX_SEQUENCES+1) ); //@ SL contains 100 sequence items
info->audio = AUDIO_PLAY;
info->continuous = 1;

View File

@@ -5538,7 +5538,7 @@ void vj_event_chain_entry_set(void *ptr, const char format[], va_list ap)
if(sample_exists(args[0]))
{
//int real_id = vj_effect_real_to_sequence(args[2]);
if(sample_chain_add(args[0],args[1],args[2]) != -1)
if(sample_chain_add(args[0],args[1],args[2]))
{
veejay_msg(VEEJAY_MSG_DEBUG, "Sample %d chain entry %d has effect %s",
args[0],args[1],vj_effect_get_description(args[2]));
@@ -5564,10 +5564,9 @@ void vj_event_chain_entry_set(void *ptr, const char format[], va_list ap)
if(vj_tag_exists(args[0]))
{
if(vj_tag_set_effect(args[0],args[1], args[2]) != -1)
if(vj_tag_set_effect(args[0],args[1], args[2]))
{
// veejay_msg(VEEJAY_MSG_INFO, "Stream %d chain entry %d has effect %s",
// args[0],args[1],vj_effect_get_description(real_id));
veejay_msg(VEEJAY_MSG_INFO, "Stream %d chain entry %d has effect %s",args[0],args[1],args[2]);
v->uc->chain_changed = 1;
}
else
@@ -5697,7 +5696,7 @@ void vj_event_chain_entry_preset(void *ptr,const char format[], va_list ap)
int i;
num_p = vj_effect_get_num_params(real_id);
if(sample_chain_add( args[0],args[1],args[2])!=-1)
if(sample_chain_add( args[0],args[1],args[2]))
{
int args_offset = 3;
@@ -5752,13 +5751,13 @@ void vj_event_chain_entry_preset(void *ptr,const char format[], va_list ap)
int num_p = vj_effect_get_num_params(real_id);
int i;
if(vj_tag_set_effect(args[0],args[1], args[2]) != -1)
if(vj_tag_set_effect(args[0],args[1], args[2]) )
{
for(i=0; i < num_p; i++)
{
if(vj_effect_valid_value(real_id, i, args[i+3]) )
{
if(vj_tag_set_effect_arg(args[0],args[1],i,args[i+3]) == -1)
if(vj_tag_set_effect_arg(args[0],args[1],i,args[i+3]))
{
veejay_msg(VEEJAY_MSG_ERROR, "setting argument %d value %d for %s",
i,
@@ -7893,7 +7892,7 @@ void vj_event_effect_add(void *ptr, const char format[], va_list ap)
{
int c = sample_get_selected_entry(v->uc->sample_id);
if ( sample_chain_add( v->uc->sample_id, c,
vj_effect_get_real_id(v->uc->key_effect)) != 1)
vj_effect_get_real_id(v->uc->key_effect)))
{
int real_id = vj_effect_get_real_id(v->uc->key_effect);
veejay_msg(VEEJAY_MSG_INFO,"Added Effect %s on chain entry %d",
@@ -7913,8 +7912,7 @@ void vj_event_effect_add(void *ptr, const char format[], va_list ap)
if(STREAM_PLAYING(v))
{
int c = vj_tag_get_selected_entry(v->uc->sample_id);
if ( vj_tag_set_effect( v->uc->sample_id, c,
vj_effect_get_real_id( v->uc->key_effect) ) != -1)
if ( vj_tag_set_effect( v->uc->sample_id, c, vj_effect_get_real_id( v->uc->key_effect) ))
{
int real_id = vj_effect_get_real_id(v->uc->key_effect);
veejay_msg(VEEJAY_MSG_INFO,"Added Effect %s on chain entry %d",