一、函数声明:
|
|
二、函数定义(见于libavutil/mathematics.c):
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) { /* 处理b和c 小于32位整数的情况 */ if (a<=INT_MAX) return (a * b + r)/c; else return a/c*b + (a%c*b + r)/c; } else { /* 处理b或c 大于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 { ... } }