‘ D = M S E = ∑ k = 1 K ∫ y k − 1 y k ( x − x k ) 2 p ( x ) d x = ∑ k = 1 K ∫ x k − Δ k / 2 x k + Δ k / 2 ( x − x k ) 2 p ( x ) d x = ∑ k = 1 K p k Δ k ∫ x k − Δ k / 2 x k + Δ k / 2 ( x − x k ) 2 d x = ∑ k = 1 K p k Δ k ∫ − Δ k / 2 Δ k / 2 x 2 d x = 1 12 ∑ k = 1 K p k Δ k 2 = Δ 2 12 , 对于均匀量化器, Δ k = y k − y k − 1 = Δ , 1 < = k < = K ‘ ` \begin{aligned} D=MSE&=\displaystyle \sum^{K}_{k=1}\int_{y_{k-1}}^{y_k}(x-x_k)^2p(x)dx \\ &=\displaystyle \sum^{K}_{k=1}\int_{x_k-\Delta_k/2}^{x_k+\Delta_k/2}(x-x_k)^2p(x)dx \\ &=\displaystyle \sum^{K}_{k=1}\frac{p_k}{\Delta_k}\int_{x_k-\Delta_k/2}^{x_k+\Delta_k/2}(x-x_k)^2dx \\ &= \displaystyle \sum^{K}_{k=1}\frac{p_k}{\Delta_k}\int_{-\Delta_k/2}^{\Delta_k/2}x^2dx \\ &=\frac{1}{12}\sum^{K}_{k=1} p_k {\Delta_k}^2 \\ &=\frac{\Delta^2}{12},对于均匀量化器,\Delta_k=y_k-y_{k-1}=\Delta,1<=k<=K \end{aligned} ` ‘D=MSE=k=1∑K∫yk−1yk(x−xk)2p(x)dx=k=1∑K∫xk−Δk/2xk+Δk/2(x−xk)2p(x)dx=k=1∑KΔkpk∫xk−Δk/2xk+Δk/2(x−xk)2dx=k=1∑KΔkpk∫−Δk/2Δk/2x2dx=121k=1∑KpkΔk2=12Δ2,对于均匀量化器,Δk=yk−yk−1=Δ,1<=k<=K‘
‘ p k = P r ( X ‾ = x k ) = P r ( X ∈ ( y k − 1 , y k ] ) = ∫ y k − 1 y k p ( x ) d x p k = p ( x ) Δ k H ( X ‾ ) = − ∑ k = 1 K p k log 2 p k = − ∑ k = 1 K ∫ y k − 1 y k p ( x ) ( log 2 p ( x ) + log 2 Δ k ) d x = − ∑ k = 1 K ∫ y k − 1 y k p ( x ) log 2 p ( x ) d x − ∑ k = 1 K ∫ y k − 1 y k p ( x ) log 2 Δ k d x = − ∑ k = 1 K ∫ y k − 1 y k p ( x ) log 2 p ( x ) d x − ∑ k = 1 K p k log 2 Δ k = H d ( X ) − 1 2 ∑ k = 1 K p k log 2 Δ k 2 因为 log 2 是凹函数 , 在高分辨率量化假设下 1 2 ∑ k = 1 K p k log 2 Δ k 2 < = 1 2 log 2 ∑ k = 1 K p k Δ k 2 = 1 2 log 2 12 D 所以 H ( X ‾ ) > = H d ( X ) − 1 2 log 2 ( 12 D ) , 当前仅当所有 Δ k 相等时,不等式相等 针对高分辨率量化器,最小平均比特 R = H ( X ‾ ) = H d ( X ) − 1 2 log 2 12 D , 可以得到 D = 2 2 H d ( X ) 12 ∗ 2 − 2 R = b ∗ 2 − R / a , 其中 b = 2 2 H d ( X ) 12 , a = 1 2 ‘ ` \begin{aligned} p_k&=Pr(\overline X=x_k)=Pr(X\in(y_{k-1},y_k]) =\int_{y_{k-1}}^{y_k} p(x)dx \\ p_k&=p(x)\Delta_k \\ H(\overline X) &=-\displaystyle \sum^{K}_{k=1}p_k \log_{2}{p_k} \\ &= -\displaystyle \sum^{K}_{k=1} \int_{y_{k-1}}^{y_k} p(x)(\log_2{p(x)+\log_2{\Delta_k}})dx \\ &= -\displaystyle \sum^{K}_{k=1} \int_{y_{k-1}}^{y_k} p(x)\log_2{p(x})dx - \displaystyle \sum^{K}_{k=1} \int_{y_{k-1}}^{y_k} p(x)\log_2{\Delta_k}dx \\ &= -\displaystyle \sum^{K}_{k=1} \int_{y_{k-1}}^{y_k} p(x)\log_2{p(x})dx -\displaystyle \sum^{K}_{k=1} p_k \log_2{\Delta_k} \\ &= H_d(X) - \frac{1}{2}\displaystyle \sum^{K}_{k=1} p_k \log_2{\Delta_k}^2 \\ & 因为\log_2是凹函数,在高分辨率量化假设下 \\ &\frac{1}{2}\displaystyle \sum^{K}_{k=1} p_k \log_2{\Delta_k}^2 <=\frac{1}{2} \log_2{\displaystyle \sum^{K}_{k=1} p_k\Delta_k^2}=\frac{1}{2} \log_2{12D} \\ & 所以 \\ & H(\overline X) >= H_d(X) - \frac{1}{2} \log_2(12D), 当前仅当所有\Delta_k相等时,不等式相等 \\ & 针对高分辨率量化器,最小平均比特R=H(\overline X)=H_d(X)-\frac{1}{2}\log_2{12D},可以得到 \\ D&=\frac{2^{2H_d(X)}}{12}* 2^{-2R}=b*2^{-R/a},其中b=\frac{2^{2H_d(X)}}{12}, a = \frac{1}{2} \end{aligned} ` ‘pkpkH(X)D=Pr(X=xk)=Pr(X∈(yk−1,yk])=∫yk−1ykp(x)dx=p(x)Δk=−k=1∑Kpklog2pk=−k=1∑K∫yk−1ykp(x)(log2p(x)+log2Δk)dx=−k=1∑K∫yk−1ykp(x)log2p(x)dx−k=1∑K∫yk−1ykp(x)log2Δkdx=−k=1∑K∫yk−1ykp(x)log2p(x)dx−k=1∑Kpklog2Δk=Hd(X)−21k=1∑Kpklog2Δk2因为log2是凹函数,在高分辨率量化假设下21k=1∑Kpklog2Δk2<=21log2k=1∑KpkΔk2=21log212D所以H(X)>=Hd(X)−21log2(12D),当前仅当所有Δk相等时,不等式相等针对高分辨率量化器,最小平均比特R=H(X)=Hd(X)−21log212D,可以得到=1222Hd(X)∗2−2R=b∗2−R/a,其中b=1222Hd(X),a=21‘
《信号处理的小波导引》第11.2节
《Principles of Digital Communication》chapter 3: https://ocw.mit.edu/courses/6-450-principles-of-digital-communications-i-fall-2006/926689aaa62a0315473fa9b982de1b07_book_3.pdf
hm lambda2
推导‘ D = b ∗ 2 ( − R / a ) R = a l o g 2 ( b D ) D = Q s t e p 2 12 ,为了体现 Q s t e p 和 Q P 的关系,这里用 Q s t e p 替换 Δ Q s t e p = 2 ( Q P − 4 ) / 6 D = 2 ( Q P − 4 ) / 3 12 d D d Q P = d ( 2 ( Q P − 4 ) / 3 ) 12 d Q P = 1 12 ⋅ 2 ( Q P − 4 ) / 3 ⋅ l n 2 = l n 2 ⋅ 1 3 ⋅ 1 4 ⋅ 2 ( Q P − 4 ) / 3 = l n 2 3 ⋅ 2 ( Q P − 4 ) / 3 − 2 = l n 2 3 2 ( Q P − 10 ) / 3 = l n 2 24 Q s t e p 2 d R d Q P = d ( a l o g 2 ( b D ) ) d Q P = a ( d l o g 2 ( b D ) d D d D d Q P ) = a 2 ( Q P − 4 ) / 3 12 b 1 l n 2 d ( b D ) d D d D d Q P = a 2 ( Q P − 4 ) / 3 12 b 1 l n 2 ⋅ ( − b D − 2 ) ) d D d Q P = a 2 ( Q P − 4 ) / 3 12 b − b l n 2 12 ⋅ 12 2 ( Q P − 4 ) / 3 ⋅ 2 ( Q P − 4 ) / 3 d D d Q P = a 2 ( Q P − 4 ) / 3 12 b − b l n 2 12 ⋅ 12 2 ( Q P − 4 ) / 3 ⋅ 2 ( Q P − 4 ) / 3 ⋅ 1 12 ⋅ 2 ( Q P − 4 ) / 3 ⋅ l n 2 = − a 3 J = D + λ R d J d R = d D d R + λ = 0 λ = − d D d Q P d R d Q P = − l n 2 24 Q s t e p 2 − a 3 = l n 2 8 a Q s t e p 2 l n 2 ≈ 2 − 2 / 3 λ = − d D d Q P d R d Q P = − l n 2 3 2 ( Q P − 10 ) / 3 − a 3 = l n 2 ⋅ 2 ( Q P − 10 ) / 3 a = c ⋅ 2 ( Q P − 12 ) / 3 ‘ ‘ ` \begin{aligned} & D=b*2^{(-R/a)} \\ & R=alog_2(\frac{b}{D}) \\ & D=\frac{Qstep^2}{12},为了体现Qstep和QP的关系,这里用Qstep替换\Delta \\ & Qstep=2^{(QP-4)/6} \\ & D=\frac{2^{(QP-4)/3}}{12} \\ & \frac{dD}{dQP}=\frac{\frac{d(2^{(QP-4)/3})}{12}}{dQP}=\frac{1}{12}\cdot2^{(QP-4)/3}\cdot ln2=ln2\cdot\frac{1}{3}\cdot\frac{1}{4}\cdot2^{(QP-4)/3}=\frac{ln2}{3}\cdot2^{(QP-4)/3-2}=\frac{ln2}{3}2^{(QP-10)/3}=\frac{ln2}{24}Qstep^2 \\ & \frac{dR}{dQP}=\frac{d(alog_2(\frac{b}{D}))}{dQP}=a(\frac{dlog_2(\frac{b}{D})}{dD}\frac{dD}{dQP})=a\frac{2^{(QP-4)/3}}{12b}\frac{1}{ln2}\frac{d(\frac{b}{D})}{dD}\frac{dD}{dQP} \\ & =a\frac{2^{(QP-4)/3}}{12b}\frac{1}{ln2}\cdot(-bD^{-2}))\frac{dD}{dQP}=a\frac{2^{(QP-4)/3}}{12b}\frac{-b}{ln2}\frac{12\cdot12}{2^{(QP-4)/3}\cdot2^{(QP-4)/3}}\frac{dD}{dQP} \\ & =a\frac{2^{(QP-4)/3}}{12b}\frac{-b}{ln2}\frac{12\cdot12}{2^{(QP-4)/3}\cdot2^{(QP-4)/3}}\cdot \frac{1}{12}\cdot2^{(QP-4)/3}\cdot ln2=-\frac{a}{3} \\ & J=D + \lambda R \\ & \frac{dJ}{dR}=\frac{dD}{dR}+\lambda=0 \\ & \lambda= -\frac{\frac{dD}{dQP}}{\frac{dR}{dQP}}=\frac{-\frac{ln2}{24}Qstep^2}{-\frac{a}{3}}=\frac{ln2}{8 a}Qstep^2 \\ & ln2\approx 2^{-2/3} \\ & \lambda= -\frac{\frac{dD}{dQP}}{\frac{dR}{dQP}}=\frac{-\frac{ln2}{3}2^{(QP-10)/3}}{-\frac{a}{3}}=\frac{ln2\cdot{2^{(QP-10)/3}}}{a}=c \cdot 2^{(QP-12)/3}` \\ \end{aligned} ` ‘D=b∗2(−R/a)R=alog2(Db)D=12Qstep2,为了体现Qstep和QP的关系,这里用Qstep替换ΔQstep=2(QP−4)/6D=122(QP−4)/3dQPdD=dQP12d(2(QP−4)/3)=121⋅2(QP−4)/3⋅ln2=ln2⋅31⋅41⋅2(QP−4)/3=3ln2⋅2(QP−4)/3−2=3ln22(QP−10)/3=24ln2Qstep2dQPdR=dQPd(alog2(Db))=a(dDdlog2(Db)dQPdD)=a12b2(QP−4)/3ln21dDd(Db)dQPdD=a12b2(QP−4)/3ln21⋅(−bD−2))dQPdD=a12b2(QP−4)/3ln2−b2(QP−4)/3⋅2(QP−4)/312⋅12dQPdD=a12b2(QP−4)/3ln2−b2(QP−4)/3⋅2(QP−4)/312⋅12⋅121⋅2(QP−4)/3⋅ln2=−3aJ=D+λRdRdJ=dRdD+λ=0λ=−dQPdRdQPdD=−3a−24ln2Qstep2=8aln2Qstep2ln2≈2−2/3λ=−dQPdRdQPdD=−3a−3ln22(QP−10)/3=aln2⋅2(QP−10)/3=c⋅2(QP−12)/3‘‘
‘ l a m b d a 2 = 2 ( Q P − 12 ) / 3 ‘ `lambda2= 2^{(QP-12)/3}` ‘lambda2=2(QP−12)/3‘
‘ l a m b d a = 2 ( Q P − 12 ) / 6 ‘ `lambda = 2^{(QP-12)/6}` ‘lambda=2(QP−12)/6‘
《Implementing rate-distortion optimization on a resource-limited H.264 encoder》第3.3节
《新一代高效视频编码H.265HEVC原理、标准与实现》第170页
x265 lambda2
推导‘ Q P = 4.2005 × l n ( λ ) + 13.7122 ‘ `QP=4.2005 \times ln(\lambda) + 13.7122` ‘QP=4.2005×ln(λ)+13.7122‘
‘ Q P − 13.7122 4.2005 = l n ( λ ) ‘ `\frac {QP - 13.7122}{4.2005}=ln(\lambda)` ‘4.2005QP−13.7122=ln(λ)‘
‘ λ = e Q P 4.2005 ⋅ e − 13.7122 4.2005 ‘ `\lambda=e^{\frac{QP}{4.2005}} \cdot e^{\frac{-13.7122}{4.2005}}` ‘λ=e4.2005QP⋅e4.2005−13.7122‘
‘ l a m b d a 2 = 0.038 ⋅ e 0.238 ⋅ Q P ‘ `lambda2 = 0.038 \cdot e^{0.238 \cdot QP}` ‘lambda2=0.038⋅e0.238⋅QP‘
‘ l a m b d a = 2 ( Q P − 12 ) / 6 ‘ `lambda = 2^{(QP-12)/6}` ‘lambda=2(QP−12)/6‘
《面向高性能视频编码标准的率失真优化技术研究》,李斌,中科大博士论文,第5章
x264 lambda2
推导‘ l a m b d a 2 = 0.9 ∗ 2 ( Q P − 12 ) / 3 ‘ `lambda2 = 0.9*2^{(QP-12)/3}` ‘lambda2=0.9∗2(QP−12)/3‘
‘ l a m b d a = 2 ( Q P − 12 ) / 6 ‘ `lambda = 2^{(QP-12)/6}` ‘lambda=2(QP−12)/6‘
‘ q s c a l e ∝ X R q s c a l e = α ∗ X R = X R / α = X r a t e F a c t o r Q P = 12 + 6 ∗ log 2 q s c a l e 0.85 ‘ ` \begin{aligned} &qscale\propto\frac{X}{R} \\ &qscale=\alpha*\frac{X}{R}=\frac{X}{R/\alpha}=\frac{X}{rateFactor} \\ &QP=12+6*\log_{2}{\frac{qscale}{0.85}} \end{aligned} ` ‘qscale∝RXqscale=α∗RX=R/αX=rateFactorXQP=12+6∗log20.85qscale‘
从公式上看,qscale等价于lambda
在使用时,只需要算出X、rateFactor就可以计算出编码当前帧的qscale,然后算出QP
X
每一帧的复杂度用SATD表示
为了避免QP波动过大,使用模糊复杂度,也就是使用前面帧的SATD进行平滑
‘ b l u r r e d C o m p l e x i t y = { f p s / 25 , c u t r e e = 1 & & a q = 0 s h o r t T e r m C p l x S u m s h o r t T e r m C p l x C o u n t = ∑ i = 0 n 0. 5 n − i ∗ c u r r e n t S a t d i ∑ i = 0 n 0. 5 n − i , 其他 ‘ ` \begin{aligned} blurredComplexity= \begin{cases} fps/25, cutree=1 \&\& aq=0 \\ \frac{shortTermCplxSum}{shortTermCplxCount}=\frac{\displaystyle \sum^{n}_{i=0}0.5^{n-i}*currentSatd_i}{\displaystyle \sum^{n}_{i=0}0.5^{n-i}}, 其他 \\ \end{cases} \end{aligned} ` ‘blurredComplexity=⎩ ⎨ ⎧fps/25,cutree=1&&aq=0shortTermCplxCountshortTermCplxSum=i=0∑n0.5n−ii=0∑n0.5n−i∗currentSatdi,其他‘
You want the movie to be somewhere approaching constant quality. However, constant quality does not mean constant PSNR nor constant QP.Details are less noticeable in high-complexity or high-motion scenes, so you can get away with somewhat higher QP for the same perceived quality”恒定的质量并不代表恒定的QP, 对于高复杂度的场景,细节丢失比较难以发现,因此可以使用比较高的QP.所以对复杂度进行了非线性压缩
‘ X = b l u r r e d C o m p l e x i t y 1 − r c . q C o m p r e s s ‘ `X=blurredComplexity^{1-rc.qCompress}` ‘X=blurredComplexity1−rc.qCompress‘
经过如上的计算X=rceq
rateFactor
‘ b a s e C p l x = { m n c u ∗ 120 , b f r a m e s > 0 m n c u ∗ 80 , b f r a m e s = 0 m b t r e e _ o f f s e t = { ( 1.0 − r c . q C o m p r e s s ) ∗ 13.5 , r c . c u T r e e = 1 0 , r c . c u T r e e = 0 m _ q C o m p r e s s = { 1 , ! r c . h e v c A q & & r c . c u T r e e r c . q C o m p r e s s , o t h e r s r a t e F a c t o r = X q s c a l e = r a t e F a c t o r C o n s t a n t = b a s e C p l x 1 − m _ q C o m p r e s s x 265 _ q p 2 q S c a l e ( r c . r f C o n s t a n t + m b t r e e _ o f f s e t ) ‘ ` \begin{aligned} &baseCplx= \begin{cases} m_ncu * 120, bframes>0 \\ m_ncu * 80, bframes=0 \\ \end{cases} \\ &mbtree\_offset= \begin{cases} (1.0 - rc.qCompress) * 13.5, rc.cuTree=1 \\ 0, rc.cuTree=0 \\ \end{cases} \\ &m\_qCompress= \begin{cases} 1, !rc.hevcAq \&\&rc.cuTree \\ rc.qCompress, others \end{cases} \\ &rateFactor=\frac{X}{qscale}=rateFactorConstant= \frac{baseCplx^{1 - m\_qCompress}}{ x265\_qp2qScale(rc.rfConstant + mbtree\_offset)} \end{aligned} ` ‘baseCplx={mncu∗120,bframes>0mncu∗80,bframes=0mbtree_offset={(1.0−rc.qCompress)∗13.5,rc.cuTree=10,rc.cuTree=0m_qCompress={1,!rc.hevcAq&&rc.cuTreerc.qCompress,othersrateFactor=qscaleX=rateFactorConstant=x265_qp2qScale(rc.rfConstant+mbtree_offset)baseCplx1−m_qCompress‘
‘ α = q s c a l e ∗ R X ‘ `\alpha=\frac{qscale*R}{X}` ‘α=Xqscale∗R‘
因为 ‘ α ‘ `\alpha` ‘α‘不是一个固定的值,所以,可以通过前面已编码的帧进行迭代更新
‘ α = ∑ i = 0 n − 1 b i t i ∗ q s c a l e i r c e q i ‘ `\alpha=\displaystyle \sum^{n-1}_{i=0}\frac{bit_{i}*qscale_{i}}{rceq_{i}}` ‘α=i=0∑n−1rceqibiti∗qscalei‘
R
‘ r a t e F a c t o r = R α = w a n t e d _ b i t s _ w i n d o w i c p l x r _ s u m i = w a n t e d _ b i t s _ w i n d o w i − 1 + b i t r a t e / f p s c p l x r _ s u m i − 1 + b i t s i ∗ q s c a l e i / r c e q i = ∑ i = 0 n b i t r a t e f p s ∑ i = 0 n − 1 b i t i ∗ q s c a l e i r c e q i ‘ `rateFactor=\frac{R}{\alpha}=\frac{wanted\_bits\_window_i}{cplxr\_sum_i}=\frac{wanted\_bits\_window_{i-1}+bitrate/fps}{cplxr\_sum_{i-1}+bits_i*qscale_i/rceq_i}=\frac{\sum^{n}_{i=0}\frac{bitrate}{fps}}{ \sum^{n-1}_{i=0}\frac{bit_{i}*qscale_{i}}{rceq_{i}}}` ‘rateFactor=αR=cplxr_sumiwanted_bits_windowi=cplxr_sumi−1+bitsi∗qscalei/rceqiwanted_bits_windowi−1+bitrate/fps=∑i=0n−1rceqibiti∗qscalei∑i=0nfpsbitrate‘
QP
公式‘ Q P = 12 + 6 ∗ log 2 α X 0.85 R = 12 + 6 ∗ log 2 b l u r r e d C o m p l e x i t y 0.85 ∗ r a t e F a c t o r ‘ ` \begin{aligned} QP&=12+6*\log_2{\frac{\alpha X}{0.85R}} \\ &=12 + 6 * \log_2{\frac{blurredComplexity}{0.85*rateFactor}} \end{aligned} ` ‘QP=12+6∗log20.85RαX=12+6∗log20.85∗rateFactorblurredComplexity‘
plannedSatd
和plannedType
数组里x265 VBV没有严格按照编码顺序存储信息
plannedSatd
和plannedType
数组里plannedSatd
和plannedType
数组中,同理第1帧编码顺序在第4帧之后,所以,也需要存储在第4帧的plannedSatd
和plannedType
数组中,其他依次类推,第5帧编码顺序在第8、4、1、2、3帧之后,所以相关信息需要进行存储 假设有f个frameencoder,第X帧的buffer状态, ‘ b u f f e r F i l l X = ∑ i = 0 X − f − 1 ( b u f f e r R a t e i − b i t s i ) + ∑ i = X − f X ( b u f f e r R a t e i − m a x ( f r a m e S i z e E s t i m a t e d , f r a m e S i z e P l a n n e d ) ) ‘ `bufferFill_X=\sum^{X-f-1}_{i=0}(bufferRate_i-bits_i)+\sum^{X}_{i=X-f}(bufferRate_i-max(frameSizeEstimated,frameSizePlanned))` ‘bufferFillX=∑i=0X−f−1(bufferRatei−bitsi)+∑i=X−fX(bufferRatei−max(frameSizeEstimated,frameSizePlanned))‘
以单线程为例:使用如下命令,并在函数RateControl::updateVbv
里面打印未经过运算的bufferFillFinal、bufferRate、bits以及经过计算后的bufferFillFinal
,如下图所示:
--input-res 960x540 --no-info --fps 24/1 --input-depth 8 --output-depth 8 --input msub067_960x540_8bit420_24_993.yuv -p medium --keyint 32768 --open-gop --frame-threads 1 --bframes 7 --lookahead-slices 1 --pools 1 --crf 50 --vbv-maxrate 12 --vbv-bufsize 12 msub067_960x540x24x993x420p8_#2#_x265enc.265
‘ 根据上面假设,可以得到: q ∝ v a r b i t s 如果按照一阶线性模型,可以得到 b i t s ∗ q = v a r ∗ c o e f f + o f f s e t 迭代更新: c o e f f i + 1 = b i t s ∗ q − o f f s e t i v a r o f f s e t i + 1 = b i t s ∗ q − c o e f f i + 1 ∗ v a r 为了平滑 c o e f f 和 o f f s e t , c o e f f = ∑ i = 0 n + 1 d e c a y n + 1 − i ∗ c o e f f i o f f s e t = ∑ i = 0 n + 1 d e c a y n + 1 − i ∗ o f f s e t i c o u n t = ∑ i = 0 n + 1 d e c a y n + 1 − i ∗ c o u n t i 最终 b i t s = c o e f f ∗ v a r + o f f s e t q ∗ c o u n t ‘ ` \begin{aligned} &根据上面假设,可以得到:q\propto\frac{var}{bits} \\ &如果按照一阶线性模型,可以得到 bits*q=var*coeff+offset \\ &迭代更新:coeff_{i+1}=\frac{bits*q-offset_{i}}{var}\\ &offset_{i+1}=bits*q-coeff_{i+1}*var\\ & 为了平滑coeff和offset, \\ & coeff=\sum^{n+1}_{i=0}decay^{n+1-i}*coeff_{i}\\ & offset=\sum^{n+1}_{i=0}decay^{n+1-i}*offset_{i}\\ & count=\sum^{n+1}_{i=0}decay^{n+1-i}*count_{i}\\ & 最终bits=\frac{coeff*var+offset}{q*count} \\ \end{aligned} ` ‘根据上面假设,可以得到:q∝bitsvar如果按照一阶线性模型,可以得到bits∗q=var∗coeff+offset迭代更新:coeffi+1=varbits∗q−offsetioffseti+1=bits∗q−coeffi+1∗var为了平滑coeff和offset,coeff=i=0∑n+1decayn+1−i∗coeffioffset=i=0∑n+1decayn+1−i∗offseticount=i=0∑n+1decayn+1−i∗counti最终bits=q∗countcoeff∗var+offset‘
帧级VBV主要通过clipQscale函数进行调整。主要使用三个信息调整qscale
码控计算得到的原始qscale
利用VBV Lookahead中当前帧存储的plannedSatd
和plannedType
信息(最多1秒)
使用上面的预测模型计算后续最多1秒的帧的比特
利用以上信息可以得到当前buffer的剩余预算bufferFillCur,比较bufferFillCur和targetFill的大小,调整qscale的大小
‘ b u f f e r F i l l C u r = b u f f e r F i l l + ∑ i = 0 m i n ( r c l o o k a h e a d , f p s ) ( b u f f e r R a t e i − b i t s i ) t a r g e t F i l l 50 = M I N ( b u f f e r F i l l + t o t a l D u r a t i o n ∗ v b v M a x R a t e ∗ 0.5 , b u f f e r S i z e ∗ ( 1 − m i n B u f f e r F i l l ∗ f i n a l D u r ) ) t a r g e t F i l l 80 = c l i p 3 ( b u f f e r S i z e ∗ ( 1 − m a x B u f f e r F i l l ∗ f i n a l D u r ) , b u f f e r S i z e , b u f f e r F i l l − t o t a l D u r a t i o n ∗ v b v M a x R a t e ∗ 0.5 ) q = { q ∗ 1.01 , b u f f e r F i l l C u r < t a r g e t F i l l 50 q / 1.01 , b u f f e r F i l l C u r > t a r g e t F i l l 80 ‘ ` \begin{aligned} &bufferFillCur=bufferFill+\sum^{min(rclookahead,fps)}_{i=0}(bufferRate_i-bits_i) \\ &targetFill_{50}=MIN(bufferFill + totalDuration * vbvMaxRate * 0.5, bufferSize * (1 - minBufferFill * finalDur)) \\ &targetFill_{80}=clip3(bufferSize * (1 - maxBufferFill * finalDur), bufferSize, bufferFill - totalDuration * vbvMaxRate * 0.5) \\ &q= \begin{cases} q * 1.01, bufferFillCur
根据向后参考帧的平均QP,计算当前帧的QP,然后映射为qscale值,并预估bits
predictRowsSizeSum函数计算当前帧已编码端比特和未编码的预测比特之和
‘ a c c F r a m e B i t s = ∑ r o w = 0 m a x R o w s e n c o d e d B i t s S o F a r r o w + t o t a l S a t d B i t s r o w t o t a l S a t d B i t s r o w = { p r e d _ s + p r e d T o t a l 2 , 当前帧和参考帧都为 P 帧, q s c a l e ≥ r e f Q S c a l e , 当前帧未编码的 s a t d C o s t F o r P e n d i n g C u s 和参考帧未编码的 r e f R o w S a t d C o s t 差异较小 p r e d _ s + p r e d _ i n t r a , 当前帧是 P 帧, q S c a l e ≤ r e f Q S c a l e p r e d _ s , 其他 ( I 帧, B 帧,以及 P 帧其他情况 ) ‘ ` \begin{aligned} & accFrameBits= \sum^{maxRows}_{row=0}encodedBitsSoFar_{row}+totalSatdBits_{row} \\ &totalSatdBits_{row}= \begin{cases} \frac{pred\_s+predTotal}{2},当前帧和参考帧都为P帧,qscale \geq refQScale, 当前帧未编码的satdCostForPendingCus和参考帧未编码的refRowSatdCost差异较小 \\ pred\_s + pred\_intra, 当前帧是P帧,qScale \leq refQScale \\ pred\_s, 其他(I帧,B帧,以及P帧其他情况) \\ \end{cases} \\ \end{aligned} ` ‘accFrameBits=row=0∑maxRowsencodedBitsSoFarrow+totalSatdBitsrowtotalSatdBitsrow=⎩ ⎨ ⎧2pred_s+predTotal,当前帧和参考帧都为P帧,qscale≥refQScale,当前帧未编码的satdCostForPendingCus和参考帧未编码的refRowSatdCost差异较小pred_s+pred_intra,当前帧是P帧,qScale≤refQScalepred_s,其他(I帧,B帧,以及P帧其他情况)‘
编码完成之后更新bufferFillFinal
reference
[1] Asymptotically efficient quantizing.pdf
[2] Lagrange multiplier selection in hybrid video coder control.pdf