fix avarage effect #42

This commit is contained in:
niels
2015-11-12 22:41:13 +01:00
parent 575c0a37d7
commit f79b366ca5
5 changed files with 91 additions and 28 deletions

View File

@@ -1,7 +1,7 @@
/*
* Linux VeeJay
*
* Copyright(C)2002 Niels Elburg <elburg@hio.hen.nl>
* Copyright(C)2002-2015 Niels Elburg <nwelburg@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -23,49 +23,109 @@
#include <libvjmem/vjmem.h>
#include "average.h"
#include "common.h"
static double *running_sum[4] = { NULL, NULL, NULL, NULL };
static int last_params[2] = { 0,0 };
static int frame_count = -1;
vj_effect *average_init(int w, int h)
{
vj_effect *ve = (vj_effect *) vj_calloc(sizeof(vj_effect));
ve->num_params = 1;
ve->num_params = 2;
ve->defaults = (int *) vj_calloc(sizeof(int) * ve->num_params); /* default values */
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] = 1;
ve->limits[1][0] = 100;
ve->limits[1][0] = 1000;
ve->limits[0][1] = 0;
ve->limits[1][1] = 1;
ve->defaults[0] = 1;
ve->parallel = 1;
ve->defaults[1] = 0;
ve->parallel = 0; //@ cannot run in parallel
ve->description = "Average";
ve->sub_format = 0;
ve->sub_format = 1;
ve->extra_frame = 0;
ve->has_user = 0;
ve->param_description = vje_build_param_list( ve->num_params, "Value");
ve->param_description = vje_build_param_list( ve->num_params, "Number of frames to average", "Mode");
return ve;
}
void average_apply(VJFrame *frame, int width, int height, int val)
int average_malloc(int width, int height)
{
running_sum[0] = (double*) vj_calloc( sizeof(double) * RUP8(width * height * 3 ));
if(!running_sum[0])
return 0;
running_sum[1] = running_sum[0] + RUP8(width*height);
running_sum[2] = running_sum[1] + RUP8(width*height);
return 1;
}
void average_free()
{
if(running_sum[0]) {
free(running_sum[0]);
running_sum[0] = NULL;
}
last_params[0] = 0;
last_params[1] = 0;
frame_count = -1;
}
void average_apply(VJFrame *frame, int width, int height, int max_sum, int mode)
{
unsigned int i;
const unsigned int len = frame->len;
const unsigned int uv_len = frame->uv_len;
uint8_t *Y = frame->data[0];
uint8_t *Cb = frame->data[1];
uint8_t *Cr = frame->data[2];
int a,b;
for (i = 0; i < len; i++) {
a = Y[i];
b = ((val-1) * a + a)/val;
Y[i] = CLAMP_Y(b);
}
if( last_params[0] != max_sum || last_params[1] != mode || ( mode == 0 && frame_count == max_sum) )
{
veejay_memset( running_sum[0], 0, sizeof(double) * (len + len + len) );
last_params[0] = max_sum;
last_params[1] = mode;
frame_count = 1;
}
for (i = 0; i < uv_len; i++) {
a = Cb[i];
b = ((val-1) * a + a)/val;
Cb[i] = CLAMP_UV(b);
a = Cr[i];
b = ((val-1) * a + a )/val;
Cr[i] = CLAMP_UV(b);
}
if( mode == 0 )
{
if( frame_count <= max_sum ) {
for (i = 0; i < len; i++) {
running_sum[0][i] += Y[i];
running_sum[1][i] += Cb[i];
running_sum[2][i] += Cr[i];
}
}
if( frame_count > 2 ) {
for (i = 0; i < len; i++) {
Y[i] = (uint8_t)(running_sum[0][i] / frame_count );
Cb[i] = (uint8_t)(running_sum[1][i] / frame_count );
Cr[i] = (uint8_t)(running_sum[2][i] / frame_count );
}
}
if( frame_count <= max_sum )
frame_count ++;
}
else
{
for (i = 0; i < len; i++) {
running_sum[0][i] += Y[i];
running_sum[1][i] += Cb[i];
running_sum[2][i] += Cr[i];
}
if( frame_count > 2 )
{
for (i = 0; i < len; i++) {
Y[i] = (running_sum[0][i] / frame_count );
Cb[i] = (running_sum[1][i] / frame_count );
Cr[i] = (running_sum[2][i] / frame_count );
}
}
frame_count ++;
}
}
void average_free(){}

View File

@@ -1,7 +1,7 @@
/*
* Linux VeeJay
*
* Copyright(C)2002 Niels Elburg <elburg@hio.hen.nl>
* Copyright(C)2002-2015 Niels Elburg <nwelburg@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -25,6 +25,8 @@
#include <stdint.h>
vj_effect *average_init();
void average_apply(VJFrame *src, int width, int height, int val);
void average_apply(VJFrame *src, int width, int height, int sum, int mode);
void average_free();
int average_malloc(int width, int height);
void average_free();
#endif

View File

@@ -494,7 +494,7 @@ extern void bar_apply(VJFrame *frame, VJFrame *frame2,
extern void vbar_apply(VJFrame *frame, VJFrame *frame2,
int w, int h, int d, int x1, int x2, int t1, int t2);
extern void average_apply(VJFrame *frame, int w, int h, int val);
extern void average_apply(VJFrame *frame, int w, int h, int val, int mode);
extern void ripple_apply(VJFrame *frame, int width, int height, int waves, int ampli,int atten);

View File

@@ -275,6 +275,7 @@ static struct
{ slicer_malloc, slicer_free, VJ_VIDEO_EFFECT_SLICER },
{ perspective_malloc, perspective_free, VJ_IMAGE_EFFECT_PERSPECTIVE },
{ feathermask_malloc, feathermask_free, VJ_IMAGE_EFFECT_ALPHAFEATHERMASK },
{ average_malloc, average_free, VJ_IMAGE_EFFECT_AVERAGE },
{ NULL , NULL ,0 },
};

View File

@@ -368,7 +368,7 @@ void vj_effman_apply_image_effect(
sinoids_apply(frames[0], frames[0]->width, frames[0]->height,arg[0],arg[1]);
break;
case VJ_IMAGE_EFFECT_AVERAGE:
average_apply(frames[0],frames[0]->width,frames[0]->height,arg[0]);
average_apply(frames[0],frames[0]->width,frames[0]->height,arg[0],arg[1]);
break;
case VJ_IMAGE_EFFECT_RIPPLE:
ripple_apply(frames[0],frames[0]->width,frames[0]->height,arg[0],arg[1],arg[2]);