[FFmpeg-devel] [PATCH v2] avfilter: add nonlinearstretch filter.

Mathias Rasmussen mathiasvr at gmail.com
Fri Jul 10 18:39:47 EEST 2020


> On 7 Jul 2020, at 16.23, Nicolas George <george at nsup.org> wrote:
> 
> Mathias Rasmussen (12020-06-25):
>> 
>> +// non-linear mapping of target frame pixel
>> +// x-coordinate to position in the source frame.
>> +#define SIGN(x) (x < 0 ? -1 : 1)
> 
>> +static double stretch_x(int target_x, int target_width, int src_width, double exp)
>> +{
>> +    double x = 2.0 * target_x / target_width - 1;
>> +
>> +    double step = 0.5 + 0.5 * pow(fabs(x), exp) * SIGN(x);
>> +
>> +    double src_x = target_x - step * (target_width - src_width);
>> +
>> +    // large exponent and high stretch ratio
>> +    // can cause over- and underflow of the frame width
>> +    return av_clipd(src_x, 0, src_width - 1);
>> +}
> 
> Could this be done with only integer arithmetic? Possibly using some
> kind of limited power series to approximate the exponent?
> 
> Integer arithmetic makes the code bit-exact across architectures, which
> makes testing much easier.

I’ve looked into this and come up with a couple of methods to perform the approximation,
however I’m not sure it’s possible to avoid floating point completely,
since the filter depends on the target_width / src_width ratio in some way or another.

The approximation might be useful for allowing dynamically adjusting the stretch factor,
however for constant stretching the current caching approach should be preferable.

More specifically I question if approximation of the step variable could provide any benefit, or must the function only rely integers?
Does the scale filter somehow achieve this? I will try to look into this.


More information about the ffmpeg-devel mailing list