首页 > 代码库 > ffmpeg 中av_rescale_rnd 的含义

ffmpeg 中av_rescale_rnd 的含义

http://blog.csdn.net/fireroll/article/details/8485482

一、函数声明: 

int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd); 


直接看代码, 它的作用是计算 "a * b / c" 的值并分五种方式来取整.

用在FFmpeg中,

则是将以 "时钟基c" 表示的 数值a 转换成以 "时钟基b" 来表示。


一共有5种方式:

 
enum AVRounding {
    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  
    AV_ROUND_PASS_MINMAX = 8192, ///< Flag to pass INT64_MIN/MAX through instead of rescaling, this avoids special cases for AV_NOPTS_VALUE
} 


二、函数定义(见于libavutil/mathematics.c):

 1 int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd){
 2     int64_t r=0;
 3     av_assert2(c > 0);
 4     av_assert2(b >=0);
 5     av_assert2((unsigned)(rnd&~AV_ROUND_PASS_MINMAX)<=5 && (rnd&~AV_ROUND_PASS_MINMAX)!=4);
 6 
 7     if (c <= 0 || b < 0 || !((unsigned)(rnd&~AV_ROUND_PASS_MINMAX)<=5 && (rnd&~AV_ROUND_PASS_MINMAX)!=4))
 8         return INT64_MIN;
 9 
10     if (rnd & AV_ROUND_PASS_MINMAX) {
11         if (a == INT64_MIN || a == INT64_MAX)
12             return a;
13         rnd -= AV_ROUND_PASS_MINMAX;
14     }
15 
16     if(a<0 && a != INT64_MIN) return -av_rescale_rnd(-a, b, c, rnd ^ ((rnd>>1)&1));
17 
18     if(rnd==AV_ROUND_NEAR_INF) r= c/2;
19     else if(rnd&1)             r= c-1;
20 
21     if(b<=INT_MAX && c<=INT_MAX){
22         if(a<=INT_MAX)
23             return (a * b + r)/c;
24         else
25             return a/c*b + (a%c*b + r)/c;
26     }else{
27 #if 1
28         uint64_t a0= a&0xFFFFFFFF;
29         uint64_t a1= a>>32;
30         uint64_t b0= b&0xFFFFFFFF;
31         uint64_t b1= b>>32;
32         uint64_t t1= a0*b1 + a1*b0;
33         uint64_t t1a= t1<<32;
34         int i;
35 
36         a0 = a0*b0 + t1a;
37         a1 = a1*b1 + (t1>>32) + (a0<t1a);
38         a0 += r;
39         a1 += a0<r;
40 
41         for(i=63; i>=0; i--){
42 //            int o= a1 & 0x8000000000000000ULL;
43             a1+= a1 + ((a0>>i)&1);
44             t1+=t1;
45             if(/*o || */c <= a1){
46                 a1 -= c;
47                 t1++;
48             }
49         }
50         return t1;
51     }
52 #else
53         AVInteger ai;
54         ai= av_mul_i(av_int2i(a), av_int2i(b));
55         ai= av_add_i(ai, av_int2i(r));
56 
57         return av_i2int(av_div_i(ai, av_int2i(c)));
58     }
59 #endif
60 }

三、实例分析
将以"1MHz时钟基" 表示的 "PTS/DTS值a" 转换成以 "90kHz时钟基" 表示。

 1 av_rescale_q(a=-10949117256, 
 2              bq={num=1, den=1000000}, 
 3              cq={num=1, den=90000))
 4 {
 5   int64_t b= bq.num * (int64_t)cq.den; // = 1 * 90000   = 90000;
 6   int64_t c= cq.num * (int64_t)bq.den; // = 1 * 1000000 = 1000000
 7   return av_rescale_rnd(a, b, c, 5);                   
 8 }
 9 
10 
11 av_rescale_rnd(a=10949117256, b=90000, c=1000000, rnd=5)
12 {
13   if (rnd==5) 
14     r = c / 2;                          // r =500000;
15   
16   if (b<=INT_MAX && c<=INT_MAX)
17   {
18     if (a<=INT_MAX)

19       return (a * b + r)/c;
20     else
21       return a/c*b + (a%c*b + r)/c;    // =  10949117256 / 1000000 * 90000 + 
22                                        //   (10949117256 % 1000000 * 90000 + 500000) / 1000000
23                                        // =  985420553
24   }
25   else
26   {
27     ...
28   }  
29 }