FFmpeg源码:av_rescale_rnd、av_rescale_q_rnd、av_rescale_q、av_add_stable函数分析

一、av_rescale_rnd函数

(一)av_rescale_rnd函数的声明

av_rescale_rnd函数声明在FFmpeg源码(本文演示用的FFmpeg源码版本为7.0.1)的头文件libavutil/mathematics.h中:

/**
 * Rounding methods.
 */
enum AVRounding {
    AV_ROUND_ZERO     = 0, ///< Round toward zero.
    AV_ROUND_INF      = 1, ///< Round away from zero.
    AV_ROUND_DOWN     = 2, ///< Round toward -infinity.
    AV_ROUND_UP       = 3, ///< Round toward +infinity.
    AV_ROUND_NEAR_INF = 5, ///< Round to nearest and halfway cases away from zero.
    /**
     * Flag telling rescaling functions to pass `INT64_MIN`/`MAX` through
     * unchanged, avoiding special cases for #AV_NOPTS_VALUE.
     *
     * Unlike other values of the enumeration AVRounding, this value is a
     * bitmask that must be used in conjunction with another value of the
     * enumeration through a bitwise OR, in order to set behavior for normal
     * cases.
     *
     * @code{.c}
     * av_rescale_rnd(3, 1, 2, AV_ROUND_UP | AV_ROUND_PASS_MINMAX);
     * // Rescaling 3:
     * //     Calculating 3 * 1 / 2
     * //     3 / 2 is rounded up to 2
     * //     => 2
     *
     * av_rescale_rnd(AV_NOPTS_VALUE, 1, 2, AV_ROUND_UP | AV_ROUND_PASS_MINMAX);
     * // Rescaling AV_NOPTS_VALUE:
     * //     AV_NOPTS_VALUE == INT64_MIN
     * //     AV_NOPTS_VALUE is passed through
     * //     => AV_NOPTS_VALUE
     * @endcode
     */
    AV_ROUND_PASS_MINMAX = 8192,
};


/**
 * Rescale a 64-bit integer with specified rounding.
 *
 * The operation is mathematically equivalent to `a * b / c`, but writing that
 * directly can overflow, and does not support different rounding methods.
 * If the result is not representable then INT64_MIN is returned.
 *
 * @see av_rescale(), av_rescale_q(), av_rescale_q_rnd()
 */
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd) av_const;

该函数的作用是:计算整形a乘以整形b再除以整形c(a * b / c)的结果,将结果作为返回值返回。 其中:

AV_ROUND_INF和AV_ROUND_UP:将计算结果向上取整。比如计算结果为4096.4,返回值为4097。

AV_ROUND_ZERO和AV_ROUND_DOWN:将计算结果向下取整。比如计算结果为4096.4,返回值为4096。

AV_ROUND_NEAR_INF:将计算结果四舍五入。比如计算结果为0.4,返回值为0;计算结果为0.6,返回值为1。

(二)av_rescale_rnd函数的定义

av_rescale_rnd函数定义在源文件libavutil/mathematics.c中:

int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
{
    int64_t r = 0;
    av_assert2(c > 0);
    av_assert2(b >=0);
    av_assert2((unsigned)(rnd&~AV_ROUND_PASS_MINMAX)<=5 && (rnd&~AV_ROUND_PASS_MINMAX)!=4);

    if (c <= 0 || b < 0 || !((unsigned)(rnd&~AV_ROUND_PASS_MINMAX)<=5 && (rnd&~AV_ROUND_PASS_MINMAX)!=4))
        return INT64_MIN;

    if (rnd & AV_ROUND_PASS_MINMAX) {
        if (a == INT64_MIN || a == INT64_MAX)
            return a;
        rnd -= AV_ROUND_PASS_MINMAX;
    }

    if (a < 0)
        return -(uint64_t)av_rescale_rnd(-FFMAX(a, -INT64_MAX), b, c, rnd ^ ((rnd >> 1) & 1));

    if (rnd == AV_ROUND_NEAR_INF)
        r = c / 2;
    else if (rnd & 1)
        r = c - 1;

    if (b <= INT_MAX && c <= INT_MAX) {
        if (a <= INT_MAX)
            return (a * b + r) / c;
        else {
            int64_t ad = a / c;
            int64_t a2 = (a % c * b + r) / c;
            if (ad >= INT32_MAX && b && ad > (INT64_MAX - a2) / b)
                return INT64_MIN;
            return ad * b + a2;
        }
    } else {
#if 1
        uint64_t a0  = a & 0xFFFFFFFF;
        uint64_t a1  = a >> 32;
        uint64_t b0  = b & 0xFFFFFFFF;
        uint64_t b1  = b >> 32;
        uint64_t t1  = a0 * b1 + a1 * b0;
        uint64_t t1a = t1 << 32;
        int i;

        a0  = a0 * b0 + t1a;
        a1  = a1 * b1 + (t1 >> 32) + (a0 < t1a);
        a0 += r;
        a1 += a0 < r;

        for (i = 63; i >= 0; i--) {
            a1 += a1 + ((a0 >> i) & 1);
            t1 += t1;
            if (c <= a1) {
                a1 -= c;
                t1++;
            }
        }
        if (t1 > INT64_MAX)
            return INT64_MIN;
        return t1;
#else
        /* reference code doing (a*b + r) / c, requires libavutil/integer.h */
        AVInteger ai;
        ai = av_mul_i(av_int2i(a), av_int2i(b));
        ai = av_add_i(ai, av_int2i(r));

        return av_i2int(av_div_i(ai, av_int2i(c)));
#endif
    }
}

二、av_rescale_q_rnd函数

(一)av_rescale_q_rnd函数的声明

av_rescale_q_rnd函数声明在头文件libavutil/mathematics.h中:

/**
 * Rescale a 64-bit integer by 2 rational numbers with specified rounding.
 *
 * The operation is mathematically equivalent to `a * bq / cq`.
 *
 * @see av_rescale(), av_rescale_rnd(), av_rescale_q()
 */
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq,
                         enum AVRounding rnd) av_const;

该函数的作用是:计算整形a乘以有理数bq再除以有理数cq(a * bq / cq)的结果,将结果作为返回值返回。 其中:

AV_ROUND_INF和AV_ROUND_UP:将计算结果向上取整。比如计算结果为4096.4,返回值为4097。

AV_ROUND_ZERO和AV_ROUND_DOWN:将计算结果向下取整。比如计算结果为4096.4,返回值为4096。

AV_ROUND_NEAR_INF:将计算结果四舍五入。比如计算结果为0.4,返回值为0;计算结果为0.6,返回值为1。

关于AVRational类型可以参考:《FFmpeg有理数相关的源码:AVRational结构体和其相关的函数分析》

(二)av_rescale_q_rnd函数的定义

av_rescale_q_rnd函数定义在源文件libavutil/mathematics.c中:

int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq,
                         enum AVRounding rnd)
{
    int64_t b = bq.num * (int64_t)cq.den;
    int64_t c = cq.num * (int64_t)bq.den;
    return av_rescale_rnd(a, b, c, rnd);
}

可以看到av_rescale_q_rnd函数内部调用了av_rescale_rnd函数。

三、av_rescale_q函数

(一)av_rescale_q函数的声明

av_rescale_q函数声明在头文件libavutil/mathematics.h中:

/**
 * Rescale a 64-bit integer by 2 rational numbers.
 *
 * The operation is mathematically equivalent to `a * bq / cq`.
 *
 * This function is equivalent to av_rescale_q_rnd() with #AV_ROUND_NEAR_INF.
 *
 * @see av_rescale(), av_rescale_rnd(), av_rescale_q_rnd()
 */
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const;

该函数的作用是:计算整形a乘以有理数bq再除以有理数cq(a * bq / cq)的结果,将结果作为返回值返回。计算结果四舍五入,比如结果为0.4,返回值为0;结果为0.6,返回值为1。

(二)av_rescale_q函数的定义

av_rescale_q函数定义在源文件libavutil/mathematics.c中:

int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
{
    return av_rescale_q_rnd(a, bq, cq, AV_ROUND_NEAR_INF);
}

可以看到av_rescale_q函数内部调用了av_rescale_q_rnd函数。

四、av_add_stable函数

(一)av_add_stable函数的声明

av_add_stable函数声明在头文件libavutil/mathematics.h中:

/**
 * Add a value to a timestamp.
 *
 * This function guarantees that when the same value is repeatly added that
 * no accumulation of rounding errors occurs.
 *
 * @param[in] ts     Input timestamp
 * @param[in] ts_tb  Input timestamp time base
 * @param[in] inc    Value to be added
 * @param[in] inc_tb Time base of `inc`
 */
int64_t av_add_stable(AVRational ts_tb, int64_t ts, AVRational inc_tb, int64_t inc);

该函数的作用是:计算ts + (inc * inc_tb / ts_tb)的结果,将结果作为返回值返回。

(二)av_add_stable函数的定义

av_add_stable函数定义在源文件libavutil/mathematics.c中:

int64_t av_add_stable(AVRational ts_tb, int64_t ts, AVRational inc_tb, int64_t inc)
{
    int64_t m, d;

    if (inc != 1)
        inc_tb = av_mul_q(inc_tb, (AVRational) {inc, 1});

    m = inc_tb.num * (int64_t)ts_tb.den;
    d = inc_tb.den * (int64_t)ts_tb.num;

    if (m % d == 0 && ts <= INT64_MAX - m / d)
        return ts + m / d;
    if (m < d)
        return ts;

    {
        int64_t old = av_rescale_q(ts, ts_tb, inc_tb);
        int64_t old_ts = av_rescale_q(old, inc_tb, ts_tb);

        if (old == INT64_MAX || old == AV_NOPTS_VALUE || old_ts == AV_NOPTS_VALUE)
            return ts;

        return av_sat_add64(av_rescale_q(old + 1, inc_tb, ts_tb), ts - old_ts);
    }
}

大部分情况下,av_add_stable函数可以化简为:

int64_t av_add_stable(AVRational ts_tb, int64_t ts, AVRational inc_tb, int64_t inc)
{
    int64_t m, d;
    inc_tb = av_mul_q(inc_tb, (AVRational) {inc, 1});

    m = inc_tb.num * (int64_t)ts_tb.den;
    d = inc_tb.den * (int64_t)ts_tb.num;

    return ts + m / d;
}

关于av_mul_q函数可以参考:《FFmpeg有理数相关的源码:AVRational结构体和其相关的函数分析》,下面语句相当于执行了inc_tb = inc * inc_tb:

inc_tb = av_mul_q(inc_tb, (AVRational) {inc, 1});

下面语句相当于执行了inc_tb / ts_tb:

m = inc_tb.num * (int64_t)ts_tb.den;
d = inc_tb.den * (int64_t)ts_tb.num;

所以语句“return ts + m / d” 等价于返回:ts + (inc * inc_tb / ts_tb) 。

五、参考

《ffmpeg 中av_rescale_rnd 的含义》

《av_rescale_rnd计算原理》

你可能感兴趣的:(FFmpeg源码分析,ffmpeg)