Sequencer: Add gaussian blur effect

Currently this gaussian blur implementation accumulates values in the
square kernel rather that doing X direction and then Y direction because
of the lack of using multiple-staged filters.

Once we can we'll implement a way to apply filter as multiple stages we
can optimize hell of a lot in here.

Another thing we can do is to use SSE2 instructions here.
This commit is contained in:
Sergey Sharybin 2014-07-19 22:16:10 +06:00
parent 00b29156e0
commit 3b2f6dbf98
9 changed files with 357 additions and 34 deletions

View File

@ -284,6 +284,7 @@ class SEQUENCER_MT_add_effect(Menu):
layout.operator("sequencer.effect_strip_add", text="Alpha Under").type = 'ALPHA_UNDER'
layout.operator("sequencer.effect_strip_add", text="Cross").type = 'CROSS'
layout.operator("sequencer.effect_strip_add", text="Gamma Cross").type = 'GAMMA_CROSS'
layout.operator("sequencer.effect_strip_add", text="Gaussian Blur").type = 'GAUSSIAN_BLUR'
layout.operator("sequencer.effect_strip_add", text="Multiply").type = 'MULTIPLY'
layout.operator("sequencer.effect_strip_add", text="Over Drop").type = 'OVER_DROP'
layout.operator("sequencer.effect_strip_add", text="Wipe").type = 'WIPE'
@ -490,7 +491,7 @@ class SEQUENCER_PT_effect(SequencerButtonsPanel, Panel):
return strip.type in {'ADD', 'SUBTRACT', 'ALPHA_OVER', 'ALPHA_UNDER',
'CROSS', 'GAMMA_CROSS', 'MULTIPLY', 'OVER_DROP',
'WIPE', 'GLOW', 'TRANSFORM', 'COLOR', 'SPEED',
'MULTICAM'}
'MULTICAM', 'GAUSSIAN_BLUR'}
def draw(self, context):
layout = self.layout
@ -588,6 +589,9 @@ class SEQUENCER_PT_effect(SequencerButtonsPanel, Panel):
col.prop(strip, "use_default_fade", "Default fade")
if not strip.use_default_fade:
col.prop(strip, "effect_fader", text="Effect fader")
elif strip.type == 'GAUSSIAN_BLUR':
col.prop(strip, "size_x")
col.prop(strip, "size_y")
class SEQUENCER_PT_input(SequencerButtonsPanel, Panel):

View File

@ -52,6 +52,11 @@
#include "RNA_access.h"
/* TODO(sergey): Could be considered a bad level call, but
* need this for gaussian table.
*/
#include "RE_pipeline.h"
static void slice_get_byte_buffers(const SeqRenderData *context, const ImBuf *ibuf1, const ImBuf *ibuf2,
const ImBuf *ibuf3, const ImBuf *out, int start_line, unsigned char **rect1,
unsigned char **rect2, unsigned char **rect3, unsigned char **rect_out)
@ -2579,6 +2584,275 @@ static void do_overdrop_effect(const SeqRenderData *context, Sequence *UNUSED(se
}
}
/*********************** Gaussian Blur *************************/
/* NOTE: This gaussian blur implementation accumulates values in the square
* kernel rather that doing X direction and then Y direction because of the
* lack of using multiple-staged filters.
*
* Once we can we'll implement a way to apply filter as multiple stages we
* can optimize hell of a lot in here.
*/
static void init_gaussian_blur_effect(Sequence *seq)
{
if (seq->effectdata)
MEM_freeN(seq->effectdata);
seq->effectdata = MEM_callocN(sizeof(WipeVars), "wipevars");
}
static int num_inputs_gaussian_blur(void)
{
return 1;
}
static void free_gaussian_blur_effect(Sequence *seq)
{
if (seq->effectdata)
MEM_freeN(seq->effectdata);
seq->effectdata = NULL;
}
static void copy_gaussian_blur_effect(Sequence *dst, Sequence *src)
{
dst->effectdata = MEM_dupallocN(src->effectdata);
}
static int early_out_gaussian_blur(Sequence *UNUSED(seq), float UNUSED(facf0), float UNUSED(facf1))
{
return EARLY_DO_EFFECT;
}
/* TODO(sergey): De-duplicate with compositor. */
static float *make_gaussian_blur_kernel(float rad, int size)
{
float *gausstab, sum, val;
float fac;
int i, n;
n = 2 * size + 1;
gausstab = (float *)MEM_mallocN(sizeof(float) * n, __func__);
sum = 0.0f;
fac = (rad > 0.0f ? 1.0f / rad : 0.0f);
for (i = -size; i <= size; i++) {
val = RE_filter_value(R_FILTER_GAUSS, (float)i * fac);
sum += val;
gausstab[i + size] = val;
}
sum = 1.0f / sum;
for (i = 0; i < n; i++)
gausstab[i] *= sum;
return gausstab;
}
static void do_gaussian_blur_effect_byte(Sequence *seq,
int start_line,
int x, int y,
int frame_width, int frame_height,
unsigned char *rect,
unsigned char *out)
{
#define INDEX(_x, _y) (((_y) * (x) + (_x)) * 4)
GaussianBlurVars *data = seq->effectdata;
const int size_x = (int) (data->size_x + 0.5f),
size_y = (int) (data->size_y + 0.5f);
int i, j;
/* Make gaussian weight tabke. */
float *gausstab_x, *gausstab_y;
gausstab_x = make_gaussian_blur_kernel(data->size_x, size_x);
if (data->size_x == data->size_y) {
gausstab_y = gausstab_x;
}
else {
gausstab_y = make_gaussian_blur_kernel(data->size_y, size_y);
}
for (i = 0; i < y; ++i) {
for (j = 0; j < x; ++j) {
int out_index = INDEX(j, i);
int current_x, current_y;
float accum[4] = {0.0f, 0.0f, 0.0f, 0.0f};
float accum_weight = 0.0f;
for (current_y = i - size_y;
current_y < i + size_y;
++current_y)
{
if (current_y < -start_line ||
current_y + start_line >= frame_height)
{
/* Out of bounds. */
continue;
}
for (current_x = j - size_x;
current_x < j + size_x;
++current_x)
{
float weight;
int index = INDEX(current_x, current_y + start_line);
if (current_x < 0 || current_x >= frame_width) {
/* Out of bounds. */
continue;
}
BLI_assert(index >= 0);
BLI_assert(index < frame_width * frame_height * 4);
weight = gausstab_x[current_x - j + size_x] *
gausstab_y[current_y - i + size_y];
accum[0] += rect[index] * weight;
accum[1] += rect[index + 1] * weight;
accum[2] += rect[index + 2] * weight;
accum[3] += rect[index + 3] * weight;
accum_weight += weight;
}
}
out[out_index + 0] = accum[0] / accum_weight;
out[out_index + 1] = accum[1] / accum_weight;
out[out_index + 2] = accum[2] / accum_weight;
out[out_index + 3] = accum[3] / accum_weight;
}
}
MEM_freeN(gausstab_x);
if (gausstab_x != gausstab_y) {
MEM_freeN(gausstab_y);
}
#undef INDEX
}
static void do_gaussian_blur_effect_float(Sequence *seq,
int start_line,
int x, int y,
int frame_width, int frame_height,
float *rect,
float *out)
{
#define INDEX(_x, _y) (((_y) * (x) + (_x)) * 4)
GaussianBlurVars *data = seq->effectdata;
const int size_x = (int) (data->size_x + 0.5f),
size_y = (int) (data->size_y + 0.5f);
int i, j;
/* Make gaussian weight tabke. */
float *gausstab_x, *gausstab_y;
gausstab_x = make_gaussian_blur_kernel(data->size_x, size_x);
if (data->size_x == data->size_y) {
gausstab_y = gausstab_x;
}
else {
gausstab_y = make_gaussian_blur_kernel(data->size_y, size_y);
}
for (i = 0; i < y; ++i) {
for (j = 0; j < x; ++j) {
int out_index = INDEX(j, i);
int current_x, current_y;
float accum[4] = {0.0f, 0.0f, 0.0f, 0.0f};
float accum_weight = 0.0f;
for (current_y = i - size_y;
current_y < i + size_y;
++current_y)
{
float weight;
if (current_y < -start_line ||
current_y + start_line >= frame_height)
{
/* Out of bounds. */
continue;
}
for (current_x = j - size_x;
current_x < j + size_x;
++current_x)
{
int index = INDEX(current_x, current_y + start_line);
if (current_x < 0 || current_x >= frame_width) {
/* Out of bounds. */
continue;
}
weight = gausstab_x[current_x - j + size_x] *
gausstab_y[current_y - i + size_y];
madd_v4_v4fl(accum, &rect[index], weight);
accum_weight += weight;
}
}
mul_v4_v4fl(&out[out_index], accum, 1.0f / accum_weight);
}
}
MEM_freeN(gausstab_x);
if (gausstab_x != gausstab_y) {
MEM_freeN(gausstab_y);
}
#undef INDEX
}
static void do_gaussian_blur_effect(const SeqRenderData *context,
Sequence *seq,
float UNUSED(cfra),
float UNUSED(facf0),
float UNUSED(facf1),
ImBuf *ibuf1,
ImBuf *ibuf2,
ImBuf *UNUSED(ibuf3),
int start_line,
int total_lines,
ImBuf *out)
{
if (out->rect_float) {
float *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
slice_get_float_buffers(context,
ibuf1, ibuf2,
NULL,
out,
start_line,
&rect1,
&rect2,
NULL,
&rect_out);
do_gaussian_blur_effect_float(seq,
start_line,
context->rectx,
total_lines,
context->rectx,
context->recty,
ibuf1->rect_float,
rect_out);
}
else {
unsigned char *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
slice_get_byte_buffers(context,
ibuf1, ibuf2,
NULL,
out,
start_line,
&rect1,
&rect2,
NULL,
&rect_out);
do_gaussian_blur_effect_byte(seq,
start_line,
context->rectx,
total_lines,
context->rectx,
context->recty,
(unsigned char *) ibuf1->rect,
rect_out);
}
}
/*********************** sequence effect factory *************************/
static void init_noop(Sequence *UNUSED(seq))
@ -2767,6 +3041,15 @@ static struct SeqEffectHandle get_sequence_effect_impl(int seq_type)
rval.early_out = early_out_adjustment;
rval.execute = do_adjustment;
break;
case SEQ_TYPE_GAUSSIAN_BLUR:
rval.multithreaded = true;
rval.init = init_gaussian_blur_effect;
rval.num_inputs = num_inputs_gaussian_blur;
rval.free = free_gaussian_blur_effect;
rval.copy = copy_gaussian_blur_effect;
rval.early_out = early_out_gaussian_blur;
rval.execute_slice = do_gaussian_blur_effect;
break;
}
return rval;

View File

@ -994,28 +994,29 @@ void BKE_sequence_base_unique_name_recursive(ListBase *seqbasep, Sequence *seq)
static const char *give_seqname_by_type(int type)
{
switch (type) {
case SEQ_TYPE_META: return "Meta";
case SEQ_TYPE_IMAGE: return "Image";
case SEQ_TYPE_SCENE: return "Scene";
case SEQ_TYPE_MOVIE: return "Movie";
case SEQ_TYPE_MOVIECLIP: return "Clip";
case SEQ_TYPE_MASK: return "Mask";
case SEQ_TYPE_SOUND_RAM: return "Audio";
case SEQ_TYPE_CROSS: return "Cross";
case SEQ_TYPE_GAMCROSS: return "Gamma Cross";
case SEQ_TYPE_ADD: return "Add";
case SEQ_TYPE_SUB: return "Sub";
case SEQ_TYPE_MUL: return "Mul";
case SEQ_TYPE_ALPHAOVER: return "Alpha Over";
case SEQ_TYPE_ALPHAUNDER: return "Alpha Under";
case SEQ_TYPE_OVERDROP: return "Over Drop";
case SEQ_TYPE_WIPE: return "Wipe";
case SEQ_TYPE_GLOW: return "Glow";
case SEQ_TYPE_TRANSFORM: return "Transform";
case SEQ_TYPE_COLOR: return "Color";
case SEQ_TYPE_MULTICAM: return "Multicam";
case SEQ_TYPE_ADJUSTMENT: return "Adjustment";
case SEQ_TYPE_SPEED: return "Speed";
case SEQ_TYPE_META: return "Meta";
case SEQ_TYPE_IMAGE: return "Image";
case SEQ_TYPE_SCENE: return "Scene";
case SEQ_TYPE_MOVIE: return "Movie";
case SEQ_TYPE_MOVIECLIP: return "Clip";
case SEQ_TYPE_MASK: return "Mask";
case SEQ_TYPE_SOUND_RAM: return "Audio";
case SEQ_TYPE_CROSS: return "Cross";
case SEQ_TYPE_GAMCROSS: return "Gamma Cross";
case SEQ_TYPE_ADD: return "Add";
case SEQ_TYPE_SUB: return "Sub";
case SEQ_TYPE_MUL: return "Mul";
case SEQ_TYPE_ALPHAOVER: return "Alpha Over";
case SEQ_TYPE_ALPHAUNDER: return "Alpha Under";
case SEQ_TYPE_OVERDROP: return "Over Drop";
case SEQ_TYPE_WIPE: return "Wipe";
case SEQ_TYPE_GLOW: return "Glow";
case SEQ_TYPE_TRANSFORM: return "Transform";
case SEQ_TYPE_COLOR: return "Color";
case SEQ_TYPE_MULTICAM: return "Multicam";
case SEQ_TYPE_ADJUSTMENT: return "Adjustment";
case SEQ_TYPE_SPEED: return "Speed";
case SEQ_TYPE_GAUSSIAN_BLUR: return "Gaussian Blur";
default:
return NULL;
}

View File

@ -2256,6 +2256,9 @@ static void write_scenes(WriteData *wd, ListBase *scebase)
case SEQ_TYPE_TRANSFORM:
writestruct(wd, DATA, "TransformVars", 1, seq->effectdata);
break;
case SEQ_TYPE_GAUSSIAN_BLUR:
writestruct(wd, DATA, "GaussianBlurVars", 1, seq->effectdata);
break;
}
}

View File

@ -142,19 +142,21 @@ static void get_seq_color3ubv(Scene *curscene, Sequence *seq, unsigned char col[
case SEQ_TYPE_GLOW:
case SEQ_TYPE_MULTICAM:
case SEQ_TYPE_ADJUSTMENT:
case SEQ_TYPE_GAUSSIAN_BLUR:
UI_GetThemeColor3ubv(TH_SEQ_EFFECT, col);
/* slightly offset hue to distinguish different effects */
if (seq->type == SEQ_TYPE_ADD) rgb_byte_set_hue_float_offset(col, 0.04);
else if (seq->type == SEQ_TYPE_SUB) rgb_byte_set_hue_float_offset(col, 0.08);
else if (seq->type == SEQ_TYPE_MUL) rgb_byte_set_hue_float_offset(col, 0.12);
else if (seq->type == SEQ_TYPE_ALPHAOVER) rgb_byte_set_hue_float_offset(col, 0.16);
else if (seq->type == SEQ_TYPE_ALPHAUNDER) rgb_byte_set_hue_float_offset(col, 0.20);
else if (seq->type == SEQ_TYPE_OVERDROP) rgb_byte_set_hue_float_offset(col, 0.24);
else if (seq->type == SEQ_TYPE_GLOW) rgb_byte_set_hue_float_offset(col, 0.28);
else if (seq->type == SEQ_TYPE_TRANSFORM) rgb_byte_set_hue_float_offset(col, 0.36);
else if (seq->type == SEQ_TYPE_MULTICAM) rgb_byte_set_hue_float_offset(col, 0.32);
else if (seq->type == SEQ_TYPE_ADJUSTMENT) rgb_byte_set_hue_float_offset(col, 0.40);
if (seq->type == SEQ_TYPE_ADD) rgb_byte_set_hue_float_offset(col, 0.04);
else if (seq->type == SEQ_TYPE_SUB) rgb_byte_set_hue_float_offset(col, 0.08);
else if (seq->type == SEQ_TYPE_MUL) rgb_byte_set_hue_float_offset(col, 0.12);
else if (seq->type == SEQ_TYPE_ALPHAOVER) rgb_byte_set_hue_float_offset(col, 0.16);
else if (seq->type == SEQ_TYPE_ALPHAUNDER) rgb_byte_set_hue_float_offset(col, 0.20);
else if (seq->type == SEQ_TYPE_OVERDROP) rgb_byte_set_hue_float_offset(col, 0.24);
else if (seq->type == SEQ_TYPE_GLOW) rgb_byte_set_hue_float_offset(col, 0.28);
else if (seq->type == SEQ_TYPE_TRANSFORM) rgb_byte_set_hue_float_offset(col, 0.36);
else if (seq->type == SEQ_TYPE_MULTICAM) rgb_byte_set_hue_float_offset(col, 0.32);
else if (seq->type == SEQ_TYPE_ADJUSTMENT) rgb_byte_set_hue_float_offset(col, 0.40);
else if (seq->type == SEQ_TYPE_GAUSSIAN_BLUR) rgb_byte_set_hue_float_offset(col, 0.42);
break;
case SEQ_TYPE_COLOR:

View File

@ -86,6 +86,7 @@ EnumPropertyItem sequencer_prop_effect_types[] = {
{SEQ_TYPE_SPEED, "SPEED", 0, "Speed", "Color effect strip type"},
{SEQ_TYPE_MULTICAM, "MULTICAM", 0, "Multicam Selector", ""},
{SEQ_TYPE_ADJUSTMENT, "ADJUSTMENT", 0, "Adjustment Layer", ""},
{SEQ_TYPE_GAUSSIAN_BLUR, "GAUSSIAN_BLUR", 0, "Gaussian Blur", ""},
{0, NULL, 0, NULL, NULL}
};

View File

@ -249,6 +249,11 @@ typedef struct SpeedControlVars {
int lastValidFrame;
} SpeedControlVars;
typedef struct GaussianBlurVars {
float size_x;
float size_y;
} GaussianBlurVars;
/* ***************** Sequence modifiers ****************** */
typedef struct SequenceModifierData {
@ -421,7 +426,8 @@ enum {
SEQ_TYPE_SPEED = 29,
SEQ_TYPE_MULTICAM = 30,
SEQ_TYPE_ADJUSTMENT = 31,
SEQ_TYPE_EFFECT_MAX = 31
SEQ_TYPE_GAUSSIAN_BLUR = 40,
SEQ_TYPE_EFFECT_MAX = 40
};
#define SEQ_MOVIECLIP_RENDER_UNDISTORTED (1 << 0)

View File

@ -539,6 +539,8 @@ static StructRNA *rna_Sequence_refine(struct PointerRNA *ptr)
return &RNA_ColorSequence;
case SEQ_TYPE_SPEED:
return &RNA_SpeedControlSequence;
case SEQ_TYPE_GAUSSIAN_BLUR:
return &RNA_GaussianBlurSequence;
default:
return &RNA_Sequence;
}
@ -1374,6 +1376,7 @@ static void rna_def_sequence(BlenderRNA *brna)
{SEQ_TYPE_SPEED, "SPEED", 0, "Speed", ""},
{SEQ_TYPE_MULTICAM, "MULTICAM", 0, "Multicam Selector", ""},
{SEQ_TYPE_ADJUSTMENT, "ADJUSTMENT", 0, "Adjustment Layer", ""},
{SEQ_TYPE_GAUSSIAN_BLUR, "GAUSSIAN_BLUR", 0, "Gaussian Blur", ""},
{0, NULL, 0, NULL, NULL}
};
@ -2203,6 +2206,23 @@ static void rna_def_speed_control(StructRNA *srna)
RNA_def_property_boolean_sdna(prop, NULL, "flags", SEQ_SPEED_COMPRESS_IPO_Y);
RNA_def_property_ui_text(prop, "Scale to length", "Scale values from 0.0 to 1.0 to target sequence length");
RNA_def_property_update(prop, NC_SCENE | ND_SEQUENCER, "rna_Sequence_update");
}
static void rna_def_gaussian_blur(StructRNA *srna)
{
PropertyRNA *prop;
RNA_def_struct_sdna_from(srna, "GaussianBlurVars", "effectdata");
prop = RNA_def_property(srna, "size_x", PROP_FLOAT, PROP_UNSIGNED);
RNA_def_property_ui_text(prop, "Size X", "Size of the blur along X axis");
RNA_def_property_ui_range(prop, 0.0f, FLT_MAX, 1, -1);
RNA_def_property_update(prop, NC_SCENE | ND_SEQUENCER, "rna_Sequence_update");
prop = RNA_def_property(srna, "size_y", PROP_FLOAT, PROP_UNSIGNED);
RNA_def_property_ui_text(prop, "Size Y", "Size of the blur along Y axis");
RNA_def_property_ui_range(prop, 0.0f, FLT_MAX, 1, -1);
RNA_def_property_update(prop, NC_SCENE | ND_SEQUENCER, "rna_Sequence_update");
}
static EffectInfo def_effects[] = {
@ -2227,6 +2247,8 @@ static EffectInfo def_effects[] = {
"Sequence strip applying affine transformations to other strips", rna_def_transform, 1},
{"WipeSequence", "Wipe Sequence", "Sequence strip creating a wipe transition",
rna_def_wipe, 1},
{"GaussianBlurSequence", "Gaussian Blur Sequence", "Sequence strip creating a gaussian blur",
rna_def_gaussian_blur, 1},
{"", "", "", NULL, 0}
};

View File

@ -468,6 +468,7 @@ void RNA_api_sequences(BlenderRNA *brna, PropertyRNA *cprop)
{SEQ_TYPE_SPEED, "SPEED", 0, "Speed", ""},
{SEQ_TYPE_MULTICAM, "MULTICAM", 0, "Multicam Selector", ""},
{SEQ_TYPE_ADJUSTMENT, "ADJUSTMENT", 0, "Adjustment Layer", ""},
{SEQ_TYPE_GAUSSIAN_BLUR, "GAUSSIAN_BLUR", 0, "Gaussian Blur", ""},
{0, NULL, 0, NULL, NULL}
};