码率控制程序_第1页
码率控制程序_第2页
码率控制程序_第3页
码率控制程序_第4页
码率控制程序_第5页
已阅读5页,还剩44页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#i nclude #in clude #in elude #i nclude global.h#in clude ratectl.hcon st double THETA=1.3636;/yyn注:在帧层码率控制前编码阶段计算P帧复杂度时用的常数。const int Switch=0;int Ip rev_bits=O;int Pp rev_bits=0;/* rate con trol variables */int Xp, Xb; /分别为P图像和B图像的图像复杂度(定义为图像编码比特数与图像所 有基本单元平均量化参数之积)? ?static int R,T_field;static i

2、nt Np, Nb, bits_to pfield, Q;long T,T1;/HRD con siderationlong UpperBoun d1, UpperBoun d2, LowerB ound;double In itialDelayOffset;con st double OMEGA=0.9;/yyn注:HRD的上限计算式中的常数匸,JVT-H017式double Wp,Wb;/分别为P图像和B图像的平均图像复杂度(定义为图像编码比特数与图像所有基本单元平均量化参数之积)int Total PF rame;int DuantQp;int P Dua ntQp;FILE *BitR

3、ate;double DeltaP; /yyn注:基本单元层码率控制量化参数调整时使用的,N m bunit bit_rate;/yyn 注: bit_rate 信道带宽frame_rate = (float)(img-framerate *(i npu t-successive_Bframe + 1) / (float) (input-jumpd +1);PreviousBit_Rate=bit_rate;计算一帧中MB总数/yyn注:图像中当前帧的/*co mpute the total nu mber of MBs in a frame*/img-Frame_Total_Number_M

4、B=img-height*img-width/256;宏块总数=图像的高x图像的宽/256/yyn注:如果输入文件的基if(inpu t-basic un itimg-Frame_Total_Number_MB)inp ut-basic un it=img-Frame_Total_Number_MB;本单元数 帧中宏块总数,则令基本单元数=帧中宏块总数if(inpu t-basic un itFrame_Total_Number_MB)TotalNumberofBasicUnit=img-Frame_Total_Number_MB/input-basicunit;/yyn 注:女0.N m b

5、picN unit = NN m bunit果输入文件的基本单元数 basicunitFrame_Total_Number_MB) MINV ALUE=8.0;MINV ALUE=4.0;/* MINV ALUE 是什么意思? ?/*i nitialize the parameters of fluid flow traffic model*/流量往返模型的初始化参数BufferSize=bit_rate* 2.56;/G012 里为 2 /yyn 注:缓冲器容量=比特率 *2.56CurrentBufferFullness=0;/yyn注:当前缓冲器充盈度 =0。此时初始化GOPTarget

6、BufferLevel=CurrentBufferFullness;/yyn 注:GOP 目标缓冲级=当前缓冲器充盈度。/*HRD con sideratio n*/Ini tialDelayOffset=BufferSize*0.8;/yyn注:初始化延迟偏移=缓冲容量*80%/* yyn注:窗口尺寸用于在帧层码率控制后/* yyn注:GOP层剩余比特数。不知道这样/*i nitialize the p revious wi ndow size*/编码阶段时更新 MAD的系数和R-D参数。m_win dowSize=0;MADm_wi ndowSize=0;img-NumberofCoded

7、BFrame=0;img-NumberofCoded PF rame=0; img-NumberofGO P=0;/*rema ining # of bits in GOP */理解对不对,待定?R = 0;/yyn注:如果连续 B帧0,即有B帧时/*c on trol p arameter */if(inpu t-successive_Bframe0)GAMMAP=0.25;/* yyn注:在帧层码率控制微观控制时,为了减小实际缓冲充盈度与目标缓存等级之间的差值,所使用的一个调整系数,没有B帧时为0.75,有B帧u (n i i)时为0.25。 JVT-G012文档的式(16)f(ni,j)

8、= F ,j + 丫(TbI (ni,j)- Bc(ni,j)F r中的YBETA P=0.9;/yyn注:计算目标比特数时的加权模型的系数。即JVT-G012文档B帧时为0.5,有B的式(19) If(n i,j)=卩? f(n i,j)+ L -卩)? f(n i,j)中的 F ,在没有 帧时为0.9。 else 为了减小实际缓冲B帧时为0.75,有GAMMAP= 0.5;/G012里为0.75 在帧层码率控制微观控制时,充盈度与目标缓存等级之间的差值,所使用的一个调整系数,没有 时为0.25。BETA P=0.5;/yyn注:计算目标比特数时的加权模型的系数。即JVT-G012文档的式(

9、19) If(,)=B? f (n中的卩,在没有B帧时为0.5,帧时为0.9。/*quadratic rate-distorti on Ti=X1xMAD1/Qi+X2xMAD2/Qi 化级别,MADi当前第i帧图像的MAD值。PP reHeader=0;model*/2 Ti用来编码当前第/yyn 注:二次i帧图像的比特数,率失真模型。Qi当前第i帧的量/yyn注:二次率失真模型系数Pm _X1=bit_rate*1.0;Pm _X2=0.0;/* lin ear p redict ion model for P pi cture*/PMADPictureC1=1.0;/yyn注:P图像的M

10、AD线性预测模型系数为1和0a1和a2,初始值PMADP ictureC2=0.0;for(i=0;i=9) DDqua nt=1;elseDDqua nt=2;MBP erRow=i np ut-img_width/16;/yyn注:宏块的列数=图像的宽度/16/*ada ptive field/frame codi ng*/img-FieldControl=0;/与 G012 比少 img-UpdateTimes=0 ;RC_MAX_QUANT = 51;/ cli ppingRC_MIN_QUANT = 0;/cli pping/yyn注:最大的量化参数 =51,最小=0/*comput

11、e the initial QP*/yyn 注:属于 帧层码率控制bpp = 1.0*bit_rate /(frame_rate*img-width*img-height); /* yyn 注:bpp 每像素比特数.1 .0 Xu (n(o,o)bpp= 帧率X宽X高if (img-width = 176)/yyn 注: QCIF 图像L1 = 0.1;L2 = 0.3;L3 = 0.6;else if (img-width = 352) L1 = 0.2;L2 = 0.6;L3 = 1.2;elseL1 = 0.6;L2 = 1.4;L3 = 2.4;/yyn注:CIF图像/yyn 注:其他

12、类型的图像if (input-SeinitialQP=0)if(bpp= L1)qp = 35;else if(bpp=L2) qp = 25;else if(bppSeinitialQP = qp;/yyn 注:序列的初始量化参数。/*/ /*/ / Initiate one GOPvoid rc_init_GOP(int np, int nb) Boolean Overum=FALSE;int OverBits;int OverDuantQp;int AllocatedBits;int GOPDquant;/yyn 注: GOP 量化步长/*check if the last GOP ov

13、er uses its budget. If yes, the in itial QP of the I frame in the coming GOP will be in creased.*/if(R0)/yyn注:剩余比特数IFLAG=1;/*Co mp ute In itialQ p for each GOP*/Tota IPF rame=np;/yyn注:如果是第一个 GOP则进行初始化img-NumberofGO P+;if(img-NumberofG OP=1)MyI nitialQ p=i npu t-Sei ni tialQ P;P reviousQ p2=M yin iti

14、alQ p-1; /rece nt cha nge -0;QP LastGO P=Myl ni tialQ p;else/*ada ptive field/frame codi ng*/if(i np ut- Piel nterlace=ADA PTIVE_CODING) |(i npu t-MbI nterlace)if (img-FieldFrame = 1)img-TotalQ pforPP icture += FrameQ PBuffer; QP Last PF rame = FrameQ PBuffer;elseimg-TotalQ pforPP icture += FieldQ P

15、Buffer; QP Last PF rame = FieldQ PBuffer;/yyn注:否则进行自适应帧/场编码/yyn注:帧/yyn注:场/*compute the average QP of P frames in the previous GOP*/ /yyn 注:以下对应为 GOP 初始量化参数的计算 ,JVT-G012 中的式 (7)CC _ Sum PQP彳 8 T r ( n i- 1, N gop ) N gopst =-Tr(n i,0) 百,其中sum为前面GOP的所有量化参数的总和,Ngo p=np+n b+1P AverageQ p=(i nt)(1.0*img-

16、TotalQ pforPP icture/img-NumberofP Picture+0.5); 式(7)中的第一项GOP Dqua nt=(i nt)(0.5+1.0*( np+n b+1)/15);if(GOP Dqua nt2)GOP Dqua nt=2;/yyn 注:/yyn注:式(7)中的最后一项P AverageQ p-=G OP Dqua nt;if (P AverageQ p (QP Last PF rame - 2)围大于2,则减减,之后调整量化参数P AverageQ p-;P AverageQ p = MAX(Q PLastGO P-2,P AverageQ p);P A

17、verageQ p = MIN(Q PLastGO P+2, P AverageQ p);P AverageQ p = MIN(RC_MAX_QUANT, PAverageQ p);P AverageQ p = MAX(RC_MIN_QUANT, PAverageQ p);/yyn注:如果P帧的平均量化参数变化范MyI ni tialQ p=P AverageQ p; QP LastG OP = Myl nitialQ p;Pm_Qp=P AverageQ p;P AveFrameQ P=P AverageQ p;PreviousQ p1=P reviousQ p2;P reviousQ p2

18、=M yin itialQ p-1;img-TotalQ pforPP icture=0; img-Numberof PP icture=0; NumberofBFrames=0;/*/*/void rc_init_pi ct(i nt field pic,i nt top field,i nt targetco mpu tati on)int i;/*compute the total nu mber of basic un its in a frame*/ if(inpu t-Mb In terlace)TotalNumberofBasic Un it=img-Frame_Total_Nu

19、mber_MB/img-BasicU nit;/yyn注:计算基本单元数N . m b picN u n i= NN m b u nit,img-BasicUnit为一个基本单元中宏块数img-NumberofCodedMacroBlocks=0;/*Normally, the ban dwith for the VBR case is estimated bya con gesti on con trol algorithm. A ban dwidth curve can be p redefi ned if we only want to test the prop osed algori

20、thm*/if(inpu t-cha nn el_t yp e=1)if(img-NumberofCoded PF rame=58)bit_rate *=1.5;/yyn 注:信道带宽else if(img-NumberofCoded PF rame=59)P reviousBit_Rate=bit_rate;/yyn注:VBR信道/*p redefi ne a target buffer level for each frame*/if(field pic|t op field)&targetco mpu tati on)switch (img-t ype)case P_SLICE:/*Si

21、nce the available ban dwidth may vary at any time, the total nu mber of bits is up dated picture by p icture*/if(PreviousBit_Rate!=bit_rate)/yyn 注:可变信道的比特率(带宽)R +=(int) floor(bit_rate-PreviousBit_Rate)*(Np+Nb)/frame_rate+0.5);/yyn 注:剩余比特数更新,(带宽-生成比特数)R = R +X(N p+ N b)帧率/* P redefi ne the target buf

22、fer level for each p icture.frame layer rate con trol*/if(img-BasicUnit=img-Frame_Total_Number_MB)/yyn 注:如果输入的图像的一个基本单元=所有MB数,即一个基本单元为一帧,则进行帧层码率控制if(img-Numberof PPictu re=1)TargetBufferLevel=Curre ntBufferFull ness;Delta P=(Curre ntBufferFull ness-G OP TargetBufferLevel)/(Total PFrame-1);TargetBuff

23、erLevel -=Delta P;else if(img-Numberof PPictu re1)TargetBufferLevel -=Delta P;/*basic unit layer rate control*/yyn注:否则进行基本单元层码率控制else if(img-NumberofCoded PF rame0)/*ada ptive frame/filed codi ng*/if(i np ut- Piel nterlace=ADA PTIVE_CODING)|(i np ut-Mbl nterlace)&(img-FieldCo ntrol=1)for(i=0;iTotalN

24、umberofBasic Un it;i+)FCB UPF MADi=FCBUCFMADi;elsefor(i=0;iNumberofG OP=1)if(img-NumberofP Pictu re=1)TargetBufferLevel=Curre ntBufferFull ness;Delta P=(Curre ntBufferFull ness-G OP TargetBufferLevel)/(Total PF rame-1); TargetBufferLevel -=Delta P;else if(img-NumberofP Pictu re1) TargetBufferLevel -

25、=Delta P;else if(img-NumberofG OP 1)if(img-NumberofP Picture=0)TargetBufferLevel=Curre ntBufferFull ness;Delta P=(Curre ntBufferFull ness-G OP TargetBufferLevel)/Tota IPF rame;TargetBufferLevel -=Delta P;else if(img-NumberofP Picture。)TargetBufferLevel -=Delta P;if(img-NumberofCodedPFrame=1) /yyn 注:

26、计算 P 帧平均复杂度 AWp=Wp; /yyn注:初始化:P帧的平均复杂度=P帧复杂度 if(img-NumberofCoded PF rameNumberofCoded PF rame1)/yyn注:如果已编码 P帧数大于1且小于8AWp=Wp*(img-NumberofCoded PFrame-1)/img-NumberofCoded PF rame+ AWp/img-NumberofCoded PF rame;else if(img-NumberofCoded PF rame1) AWp=Wp/8+7*AWp/8;P帧平均为 JVT-G012 中的(式 14 )/yyn注:帧层码率控制

27、计算 P帧量化参数过程中,计算目标缓存等级时式子里的 复杂度,复杂度的计算公式对应W p(n i j)7 XW p( n i j JW p(ni,j)=p(8 i,j)+ p(i,j-1)计算B帧的平均复杂度注:非参考帧数大于 0,即有非参考帧/compute the average compi exity of B frames if(inpu t-successive_Bframe0)/yyn时,则计算目标缓冲等级/co mpute the target buffer level上 式 为 JVT-G012 中 的 (式 13)TargetBufferLevel +=(AWp*(i npu

28、 t-successive_Bframe+1)*bit_rate /(frame_rate*(AWp+AWb*i np ut-successive_Bframe)-bit_rate/frame_rate); /yyn 注Tbl(ni”=TblTN+F:X:;n:L;【;:昭rTbl (n i 2)其中 Tbl (n i,j+1)-Tbl (n i,j)- N 1, successive_Bframe 对应为 JVT-G012 (式 13)中的Lbreak;case B SLICE:/* up date the total nu mber of bits if the ban dwidth is

29、 cha nged*/if(PreviousBit_Rate!=bit_rate)/yyn 注:如果是可变信道R +=(i nt) floor(bit_rate-PreviousBit_Rate)*(N p+Nb)/frame_rate+0.5);if(img-NumberofCoded PF rame=1) &(img-NumberofCodedBFrame=1)AWp=Wp;AWb=Wb;else if(img-NumberofCodedBFrame1)/compute the average weight if(img-NumberofCodedBFrameNumberofCodedBF

30、rame-1)/img-NumberofCodedBFrame+ AWb/img-NumberofCodedBFrame;elseAWb=Wb/8+7*AWb/8;/yyn注:平均加权复杂度,对应为JVT-G012中的(式14)W b(n i,j)7 ; W b(n i,j- 1 )W b(n i, j)= 8+break;/*Compute the target bit for each frame*/if(img-type=P_SLICE)/yyn注:如果输入的图像类型为/*frame layer rate con trol*/if(img-Basic Un it=img-Frame_To

31、tal_Number_MB) 宏块总数,则进行帧层码率控制if(img-NumberofCoded PF rame0)/yyn注:如果基本单元数T = (lo ng) floor(Wp*R/(N p*Wp+Nb*Wb) + 0.5);/yyn注:对应为JVT-G012 中的(式 18)W p( n i i- 1 ) T r (n i i)T( n ij)=并 W p(n i,j- 1)N p ,r(j- 1 )+ W b(n i,j- JN (j-1 )+ ( 1T (门 j考虑剩余比特数时的目标比特数。T即为等号左边项。T1=(long)floor(bit_rate/frame_rate-G

32、AMMA P*(Curre ntBuferFull ness-TargetBuferLevel)+0.5);T1=MAX(0,T1);/yyn注:对应为JVT-G012中的(式16)u ( n i j)f (ni,j)= + 丫(TbI (ni,j)- Bc(ni,j)F r变量GAMMAP=卩,初始值上面已经定义。 T1即为等号左边项。T = (lo ng)(floor(BETA P*T+(1.0-BETA P)*T1+0.5);/yyn注:对应为JVT-G012中的(式19)f ( n i , j )= R? T( n i , j)+( 1 - R )? T( n i , j)I 苴中BE

33、TAP= R,初始值上面已经定义。T即为等号左边项,即目标比特数。P帧量化参数过程中的目标比特分配。/yyn注:以上为帧层码率控制时计算/*basic un it layer rate con trol*/else /yyn注:否则进行基本单元层码率控制,以下同上面类似,计算目标比特 数的过程。if(img-NumberofGO P=1) &(img-NumberofCoded PF rame0)T = (int) floor(Wp*R/(N p*Wp+Nb*Wb) + 0.5);(int)T1=floor(bit_rate/frame_rate-GAMMA P*(Curre ntBuferF

34、ull ness-TargetBuferLevel)+0.5); T1=MAX(0,T1);T = (i nt)(floor(BETA P*T+(1.0-BETA P)*T1+0.5);else if(img-NumberofGO P1)T = (lo ng) floor(Wp*R/(N p*Wp+Nb*Wb) + 0.5);(long)T1=floor(bit_rate/frame_rate-GAMMA P*(Curre ntBuferFull ness-TargetBuferLevel)+0.5);T1 = MAX(0,T1);T = (lo ng)(floor(BETA P*T+(1.0

35、-BETA P)*T1+0.5);Preserve some bits for smoothi ng*/ /yyn 注:为了图像平滑预留的比特数。 /yynT=(long)(1.0-0.0*input-successive_Bframe)*T);/*HRD consideration*/ /yyn 注:虚拟参考解码器调整。 T = MAX(T, (long) LowerBound);T = MIN(T, (long) UpperBound2);if(topfield)|(fieldpic&(input-PicInterlace=ADAPTIVE_CODING)|(input-MbInterla

36、ce)T_field=T;注: ADAPTIVE_CODING 为自适应编码。 if(fieldpic|topfield)/*frame layer rate control*/ img-NumberofHeaderBits=0; img-NumberofTextureBits=0;/*basic unit layer rate control*/ if(img-BasicUnitFrame_Total_Number_MB) TotalFrameQP=0; img-NumberofBasicUnitHeaderBits=0; img-NumberofBasicUnitTextureBits=0

37、; img-TotalMADBasicUnit=0; if(img-FieldControl=0)NumberofBasicUnit=TotalNumberofBasicUnit; elseNumberofBasicUnit=TotalNumberofBasicUnit/2;if(img-type=P_SLICE)&(img-BasicUnitFrame_Total_Number_MB) &(img-FieldControl=1)/*top filed at basic unit layer rate control*/if(topfield)bits_topfield=0;T=(long)(

38、T_field*0.6);/*bottom filed at basic unit layer rate control*/else/yyn 注:基本单元层码控顶场/yyn 注:基本单元层码控底场T=T_field-bits_topfield;img-NumberofBasicUnitHeaderBits=0;img-NumberofBasicUnitTextureBits=0;img-TotalMADBasicUnit=0;NumberofBasicUnit=TotalNumberofBasicUnit/2;/*/calculate MAD for the current macrobloc

39、k double calc_MAD()int k,l;int s = 0;double MAD;/*/yyn 注:为当前宏块计算 MADfor (k = 0; k 16; k+)for (l = 0; l BasicU nit=img-Frame_Total_Number_MB)X = (int) floor(nbits*m_Qc+ 0.5);/yyn 注:floor()为取整函数。X 复杂度,等于生成的比特数X量化参数/*basic unit layer rate con trol*/ elseif(img-ty pe=P_SLICE)if(img-IFLAG=0) &(img-FieldC

40、o ntrol=1)|(img-FieldCo ntrol=0)Avem_Qc=TotalFrameQ P/TotalNumberofBasic Unit;X=(i nt)floor( nbits*Avem_Qc+0.5);else if(img-ty pe=B_SLICE)X = (int) floor( nbits*m_Qc+ 0.5);switch (img-t ype)case P_SLICE:/*filed codi ng*/if(img-IFLAG=0) &(img-FieldCo ntrol=1) |(img-FieldCo ntrol=0)Xp = X;Np-;Wp=X p;Pm_Hp=img-NumberofHeaderBits; img-NumberofCoded PF rame+; img-NumberofP Pi cture+;else if(img-IFLAG!=0) &(img-FieldCo ntrol=1) img-IFLAG=0;break;case

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论