From f79b366ca5ab7c03a9dbb452099e6cd5a68ec88d Mon Sep 17 00:00:00 2001 From: niels Date: Thu, 12 Nov 2015 22:41:13 +0100 Subject: [PATCH] fix avarage effect #42 --- .../veejay-server/libvje/effects/average.c | 108 ++++++++++++++---- .../veejay-server/libvje/effects/average.h | 6 +- .../veejay-server/libvje/internal.h | 2 +- .../veejay-server/libvje/vj-effect.c | 1 + .../veejay-server/libvje/vj-effman.c | 2 +- 5 files changed, 91 insertions(+), 28 deletions(-) diff --git a/veejay-current/veejay-server/libvje/effects/average.c b/veejay-current/veejay-server/libvje/effects/average.c index 1c6745ca..efff2c43 100644 --- a/veejay-current/veejay-server/libvje/effects/average.c +++ b/veejay-current/veejay-server/libvje/effects/average.c @@ -1,7 +1,7 @@ /* * Linux VeeJay * - * Copyright(C)2002 Niels Elburg + * Copyright(C)2002-2015 Niels Elburg * * 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 #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(){} + diff --git a/veejay-current/veejay-server/libvje/effects/average.h b/veejay-current/veejay-server/libvje/effects/average.h index 9312abeb..599923c3 100644 --- a/veejay-current/veejay-server/libvje/effects/average.h +++ b/veejay-current/veejay-server/libvje/effects/average.h @@ -1,7 +1,7 @@ /* * Linux VeeJay * - * Copyright(C)2002 Niels Elburg + * Copyright(C)2002-2015 Niels Elburg * * 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 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 diff --git a/veejay-current/veejay-server/libvje/internal.h b/veejay-current/veejay-server/libvje/internal.h index dbcd962e..5878909b 100644 --- a/veejay-current/veejay-server/libvje/internal.h +++ b/veejay-current/veejay-server/libvje/internal.h @@ -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); diff --git a/veejay-current/veejay-server/libvje/vj-effect.c b/veejay-current/veejay-server/libvje/vj-effect.c index 5772d3c5..3a182e0a 100644 --- a/veejay-current/veejay-server/libvje/vj-effect.c +++ b/veejay-current/veejay-server/libvje/vj-effect.c @@ -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 }, }; diff --git a/veejay-current/veejay-server/libvje/vj-effman.c b/veejay-current/veejay-server/libvje/vj-effman.c index 73fe4894..181b30bd 100644 --- a/veejay-current/veejay-server/libvje/vj-effman.c +++ b/veejay-current/veejay-server/libvje/vj-effman.c @@ -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]);