int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd);
用在FFmpeg中,
则是将以 "时钟基c" 表示的 数值a 转换成以 "时钟基b" 来表示。
AV_ROUND_ZERO = 0, // Round toward zero. 趋近于0 AV_ROUND_INF = 1, // Round away from zero. 趋远于0 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. // 四舍五入,小于0.5取值趋向0,大于0.5取值趋远于0
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd) { int64_t r=0; assert(c > 0); assert(b >=0); assert((unsigned)rnd<=5 && rnd!=4); /* 将小于0的整数,转换成大于0的整来计算 */ if (a<0 && a != INT64_MIN) return -av_rescale_rnd(-a, 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) { /* 处理小于32位整数的情况 */ if (a<=INT_MAX) return (a * b + r)/c; else return a/c*b + (a%c*b + r)/c; } else { /* 64位整数的算法 */ 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++; } } } return t1; }
av_rescale_q(a=-10949117256, bq={num=1, den=1000000}, cq={num=1, den=90000)) { int64_t b= bq.num * (int64_t)cq.den; // = 1 * 90000 = 90000; int64_t c= cq.num * (int64_t)bq.den; // = 1 * 1000000 = 1000000 return av_rescale_rnd(a, b, c, 5); } av_rescale_rnd(a=10949117256, b=90000, c=1000000, rnd=5) { if (rnd==5) r = c / 2; // r =500000; if (b<=INT_MAX && c<=INT_MAX) { if (a<=INT_MAX) return (a * b + r)/c; else return a/c*b + (a%c*b + r)/c; // = 10949117256 / 1000000 * 90000 + // (10949117256 % 1000000 * 90000 + 500000) / 1000000 // = 985420553 } else { ... } }