[FFmpeg-devel] [PATCH] avfilter/vsrc_mandelbrot: avoid sqrt for epsilon calculation

Ganesh Ajjanagadde gajjanag at mit.edu
Tue Nov 24 15:23:04 CET 2015


On Mon, Nov 23, 2015 at 9:46 PM, Michael Niedermayer <michaelni at gmx.at> wrote:
> On Mon, Nov 23, 2015 at 05:19:52PM -0500, Ganesh Ajjanagadde wrote:
>> This rewrites into the mathematically equivalent expression avoiding sqrt,
>> and results in a very minor speedup.
>>
>> Tested on x86-64, Haswell, GNU/Linux.
>> Command:
>> ffmpeg -v error -f lavfi -i mandelbrot -f null -
>>
>> old (draw_mandelbrot):
>> 3982389425 decicycles in draw_mandelbrot,     256 runs,      0 skips
>> 7634221782 decicycles in draw_mandelbrot,     512 runs,      0 skips
>> 20576449397 decicycles in draw_mandelbrot,    1024 runs,      0 skips
>> 12949998655 decicycles in draw_mandelbrot,    2048 runs,      0 skips
>>
>> new (draw_mandelbrot):
>> 3966406060 decicycles in draw_mandelbrot,     256 runs,      0 skips
>> 7553322112 decicycles in draw_mandelbrot,     512 runs,      0 skips
>> 20454169970 decicycles in draw_mandelbrot,    1024 runs,      0 skips
>> 12822228615 decicycles in draw_mandelbrot,    2048 runs,      0 skips
>>
>> Signed-off-by: Ganesh Ajjanagadde <gajjanagadde at gmail.com>
>> ---
>>  libavfilter/vsrc_mandelbrot.c | 6 +++---
>>  1 file changed, 3 insertions(+), 3 deletions(-)
>>
>> diff --git a/libavfilter/vsrc_mandelbrot.c b/libavfilter/vsrc_mandelbrot.c
>> index 950c5c8..20da8ae 100644
>> --- a/libavfilter/vsrc_mandelbrot.c
>> +++ b/libavfilter/vsrc_mandelbrot.c
>> @@ -291,7 +291,7 @@ static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize,
>>
>>              use_zyklus= (x==0 || s->inner!=BLACK ||color[x-1 + y*linesize] == 0xFF000000);
>>              if(use_zyklus)
>> -                epsilon= scale*1*sqrt(SQR(x-s->w/2) + SQR(y-s->h/2))/s->w;
>> +                epsilon= SQR(scale/s->w)*(SQR(x-s->w/2) + SQR(y-s->h/2));
>
> if the sqrt is a speed problem, (i had originally thougt it would
> not be)
> you can probably replace this by an approximation like:
> epsilon= scale*CONSTANT*(FFABS(x-s->w/2) + FFABS(y-s->h/2))/s->w;

This is mathematically justifiable in the sense that \forall x, y \ge
0, \sqrt{\frac{1}{2}}(x+y) \le \sqrt{x^2 + y^2} \le 1(x+y), i.e
replacing by a constant is ok as the constant is guaranteed to lie
within bounds not too far apart (~[0.7, 1]). Furthermore, anyway the
"10*epsilon*epsilon" is a heuristic, and so the 10 can absorb any
minor change here.
Will change, and replace by the constant 1. Note that this will avoid
the cost of sqrt as well as squaring, so hopefully it is more readily
measurable.

>
> [...]
> --
> Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
>
> Why not whip the teacher when the pupil misbehaves? -- Diogenes of Sinope
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>


More information about the ffmpeg-devel mailing list