-
Notifications
You must be signed in to change notification settings - Fork 2
/
ratectl.c
1691 lines (1533 loc) · 58.1 KB
/
ratectl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
***************************************************************************
* \file ratectl.c
*
* \brief
* Rate Control algorithm
*
* \author
* Main contributors (see contributors.h for copyright, address and affiliation details)
* - Siwei Ma <swma@jdl.ac.cn>
* - Zhengguo LI<ezgli@lit.a-star.edu.sg>
*
* \date
* 16 Jan. 2003
**************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "global.h"
#include "ratectl.h"
const double THETA=1.3636;
const int Switch=0;
int Iprev_bits=0;
int Pprev_bits=0;
/* rate control variables */
int Xp, Xb;
static int R,T_field;
static int Np, Nb, bits_topfield, Q;
long T,T1;
//HRD consideration
long UpperBound1, UpperBound2, LowerBound;
double InitialDelayOffset;
const double OMEGA=0.9;
double Wp,Wb;
int TotalPFrame;
int DuantQp;
int PDuantQp;
FILE *BitRate;
double DeltaP;
// Initiate rate control parameters 初始化码率控制变量
void rc_init_seq()
{
double L1,L2,L3,bpp;
int qp;
int i;
Xp=0;
Xb=0;
bit_rate=input->bit_rate;//目标码率
frame_rate = (float)(img->framerate *(input->successive_Bframe + 1)) / (float) (input->jumpd + 1);//帧率 ??怎么算的
PreviousBit_Rate=bit_rate;//保存数据
/*compute the total number of MBs in a frame*/
img->Frame_Total_Number_MB=img->height*img->width/256;//16*16=256 所有宏块数
if(input->basicunit > img->Frame_Total_Number_MB)
input->basicunit = img->Frame_Total_Number_MB;//每个基本单元的宏块数不可能超过总宏块数
if(input->basicunit < img->Frame_Total_Number_MB)
TotalNumberofBasicUnit = img->Frame_Total_Number_MB/input->basicunit;//基本单元个数
MINVALUE=4.0;
/*initialize the parameters of fluid flow traffic model*/
BufferSize=bit_rate*2.56;//缓冲区大小
CurrentBufferFullness=0;//当前缓冲区的充盈程度清零
GOPTargetBufferLevel=CurrentBufferFullness;//??
/*HRD consideration*/
InitialDelayOffset=BufferSize*0.8;
/*initialize the previous window size*/
m_windowSize=0;
MADm_windowSize=0;
img->NumberofCodedBFrame=0;//已经编码的B帧重新计数
img->NumberofCodedPFrame=0;//已经编码的P帧重新计数
img->NumberofGOP=0;
/*remaining # of bits in GOP */
R = 0;//当前GOP中剩余的比特数
/*control parameter */
if(input->successive_Bframe>0)//有B帧 P帧 PBB..BBP
{
GAMMAP=0.25;//计算参考图像的比特数
BETAP=0.9;
}
else//只有P帧IPPPPI
{
GAMMAP=0.5;
BETAP=0.5;
}
/*quadratic 平方 rate-distortion model*/
PPreHeader=0;
Pm_X1=bit_rate*1.0;//RD模型参数
Pm_X2=0.0;
/* linear 线性 prediction model for P picture*/
PMADPictureC1=1.0;//对D(失真度)的线性预测
PMADPictureC2=0.0;
for(i=0;i<20;i++)
{
Pm_rgQp[i]=0;
Pm_rgRp[i]=0.0;
PPictureMAD[i]=0.0;
}
PPictureMAD[20]=0.0;
//Define the largest variation of quantization parameters
PDuantQp=2;//量化参数最大的变化量
/*basic unit 基本单元 layer rate control*/
PAveHeaderBits1=0;
PAveHeaderBits3=0;
if(TotalNumberofBasicUnit>=9)//当编码图像没有可用bits,这个是重新偏移QP的初始值 书上的判断是8
DDquant=1;
else
DDquant=2;
MBPerRow=input->img_width/16;//每一行的宏块数
/*adaptive field/frame coding*/
img->FieldControl=0;
RC_MAX_QUANT = 51; // clipping 限幅 QP的最大值
RC_MIN_QUANT = 0;//clippin
/*compute thei initial QP*/
//码率除以帧率就是每一幅图像分配的bits 再除以图像像素 得到图像上平均每一个像素分配的bits
bpp = 1.0*bit_rate /(frame_rate*img->width*img->height);
if (img->width == 176) //QCIF图像
{
L1 = 0.1;
L2 = 0.3;
L3 = 0.6;
}else if (img->width == 352)//CIF图像
{
L1 = 0.2;
L2 = 0.6;
L3 = 1.2;
}else //其他图像
{
L1 = 0.6;
L2 = 1.4;
L3 = 2.4;
}
if (input->SeinitialQP==0)//对第一个GOP的第一个IDR帧QP设置
{
if(bpp<= L1)//如果每个像素分配的bits很少 初始化用较大的量化间距 QP
qp = 35;
else
if(bpp<=L2)//bpp越小 分配的QP越小
qp = 25;
else
if(bpp<=L3)
qp = 20;
else
qp =10;
input->SeinitialQP = qp;
}
}
// Initiate one GOP 初始化一个GOP
void rc_init_GOP(int np, int nb)
{
Boolean Overum=FALSE;//默认没有超出预算
int OverBits;
int OverDuantQp;
int AllocatedBits;
int GOPDquant;
/*check if the last GOP over uses its budget. If yes, the initial QP of the I frame in
the coming GOP will be increased.*/
if(R<0)//上一个GOP总的比特超出了预算
Overum=TRUE;
OverBits=-R;//R的相反数 超出的比特数(正表示有超出 负表示没有超出)
/*initialize the lower bound and the upper bound for the target bits of each frame, HRD consideration*/
LowerBound=(long)(R+bit_rate/frame_rate);//下界
UpperBound1=(long)(R+InitialDelayOffset);//上界 InitialDelayOffset=BufferSize*0.8; BufferSize=bit_rate*2.56;
/*compute the total number of bits for the current GOP*/
//(1 + np + nb) I,P,B帧数量 即GOP内帧数量
AllocatedBits = (int) floor((1 + np + nb) * bit_rate / frame_rate + 0.5);//分配的bits
R +=AllocatedBits;//前一个GOP剩余的bits数加上这个GOP预测的bits
Np = np;
Nb = nb;
OverDuantQp=(int)(8*OverBits/AllocatedBits+0.5);//??
GOPOverdue=FALSE;
/*field coding*/
img->IFLAG=1;
/*Compute InitialQp for each GOP*/
TotalPFrame=np;//p帧数量
img->NumberofGOP++;//GOP开始累加
if(img->NumberofGOP==1)//如果是第一个GOP
{
MyInitialQp=input->SeinitialQP;//第一个GOP的IDR图像QP确定
PreviousQp2=MyInitialQp-1; //recent change -0;
QPLastGOP=MyInitialQp;//为计算后面的GOP的IDR图像的QP
}
else//除开第一个GOP
{
/*adaptive field/frame coding*/
if((input->PicInterlace==ADAPTIVE_CODING)\
||(input->MbInterlace))
{
if (img->FieldFrame == 1)//图像是帧?
{
img->TotalQpforPPicture += FrameQPBuffer;//把前一个P帧的QP添加到求和里面
QPLastPFrame = FrameQPBuffer;//为计算后面的GOP的IDR图像的QP
}
else//图像是场?
{
img->TotalQpforPPicture += FieldQPBuffer;
QPLastPFrame = FieldQPBuffer;
}
}
/*compute the average QP of P frames in the previous GOP*/
PAverageQp=(int)(1.0*img->TotalQpforPPicture/img->NumberofPPicture+0.5);//前一个GOP中所有P帧的平均QP
GOPDquant=(int)(0.5+1.0*(np+nb+1)/15);//偏移 p166
if(GOPDquant>2)
GOPDquant=2;//限幅
PAverageQp-=GOPDquant;
//I帧之前肯定是最后一个P帧
if (PAverageQp > (QPLastPFrame - 2))//QPLastPFrame是前一个GOP最后一个P帧图像的QP
PAverageQp--;
PAverageQp = MAX(QPLastGOP-2, PAverageQp);//QPLastGOP是上一个GOP的IDR图像的QP
PAverageQp = MIN(QPLastGOP+2, PAverageQp);
PAverageQp = MIN(RC_MAX_QUANT, PAverageQp);//RC_MAX_QUANT是51
PAverageQp = MAX(RC_MIN_QUANT, PAverageQp);//最低是0
MyInitialQp=PAverageQp;//非第一个GOP中IDR帧初始化的QP
QPLastGOP = MyInitialQp;//保存起来 为求解下一个GOP的IDR帧QP做准备
Pm_Qp=PAverageQp;
PAveFrameQP=PAverageQp;
PreviousQp1=PreviousQp2;
PreviousQp2=MyInitialQp-1;
}
img->TotalQpforPPicture=0;//上面已经使用过该数据 这个地方清零 当前帧的数据求出了后再赋值
img->NumberofPPicture=0;
NumberofBFrames=0;
}
void rc_init_pict(int fieldpic,int topfield,int targetcomputation)
{
int i;
/*compute the total number of basic units in a frame*/
if(input->MbInterlace)
TotalNumberofBasicUnit=img->Frame_Total_Number_MB/img->BasicUnit;//基本单元的个数 基本单元由若干个宏块构成
img->NumberofCodedMacroBlocks=0;
/*Normally, the bandwith for the VBR case is estimated by
a congestion(拥塞) control algorithm. A bandwidth curve can be predefined if we only want to
test the proposed(建议) algorithm*/
if(input->channel_type==1)//时变信道
{
if(img->NumberofCodedPFrame==58)//不是很懂?? 码率突然增大 是在程序里面测试可变码率控制??
bit_rate *=1.5;
else if(img->NumberofCodedPFrame==59)
PreviousBit_Rate=bit_rate;
}
/*predefine a target buffer level for each frame*/
if((fieldpic||topfield)&&targetcomputation)
{
switch (img->type)
{
case P_SLICE: //P片 在码率控制里面算法用得最多的地方
/*Since the available bandwidth may vary at any time, the total number of
bits is updated picture by picture 每帧图像都更新R */
if(PreviousBit_Rate!=bit_rate)//如果不是恒定码率
R +=(int) floor((bit_rate-PreviousBit_Rate)*(Np+Nb)/frame_rate+0.5);//
/* predefine the target buffer level for each picture.
frame layer rate control*/
if(img->BasicUnit==img->Frame_Total_Number_MB)//一个基本单元是所有宏块 相当于图像级码率控制
{
if(img->NumberofPPicture==1)//当前GOP第一个P帧
{
TargetBufferLevel=CurrentBufferFullness;//Si(2)=Vi(2) 缓冲区充盈程度赋值给缓冲区级别
DeltaP=(CurrentBufferFullness-GOPTargetBufferLevel)/(TotalPFrame-1);//书上没有减GOPTargetBufferLevel
TargetBufferLevel -=DeltaP;
}
else if(img->NumberofPPicture>1)//当前GOP非第一个P帧
TargetBufferLevel -=DeltaP;
}
/*basic unit layer rate control*/
else//一个基本单元是几个或一个宏块 基本单元层码率控制
{
if(img->NumberofCodedPFrame>0)
{
/*adaptive frame/filed coding*/
if(((input->PicInterlace==ADAPTIVE_CODING)||(input->MbInterlace))\
&&(img->FieldControl==1))
{
for(i=0;i<TotalNumberofBasicUnit;i++)
FCBUPFMAD[i]=FCBUCFMAD[i];//上一个基本单元同位置的MADS来预测当前的MADS
}
else
{
for(i=0;i<TotalNumberofBasicUnit;i++)
BUPFMAD[i]=BUCFMAD[i];
}
}
//其实GOP的判断可以去掉 和上面一样
if(img->NumberofGOP==1)//第一个GOP
{
if(img->NumberofPPicture==1)//第一个GOP第一个P帧
{
TargetBufferLevel=CurrentBufferFullness;//和图像层码率控制一样 Si(2)=Vi(2)
DeltaP=(CurrentBufferFullness-GOPTargetBufferLevel)/(TotalPFrame-1);
TargetBufferLevel -=DeltaP;
}
else if(img->NumberofPPicture>1)//第一个GOP后面的P帧
TargetBufferLevel -=DeltaP;
}
else if(img->NumberofGOP>1)//非第一个GOP
{
if(img->NumberofPPicture==0)//后面GOP的第一个P帧
{
TargetBufferLevel=CurrentBufferFullness;
DeltaP=(CurrentBufferFullness-GOPTargetBufferLevel)/TotalPFrame;
TargetBufferLevel -=DeltaP;
}
else if(img->NumberofPPicture>0)//后面GOP的非第一个P帧
TargetBufferLevel -=DeltaP;
}
}
if(img->NumberofCodedPFrame==1)//每一个GOP的第一个P帧
AWp=Wp;//Wp是P帧图像的复杂度 AWp是P帧图像的平均复杂度
if((img->NumberofCodedPFrame<8)&&(img->NumberofCodedPFrame>1))//每一个GOP的第二个P帧到第七个P帧
AWp=Wp*(img->NumberofCodedPFrame-1)/img->NumberofCodedPFrame+\
AWp/img->NumberofCodedPFrame;
else if(img->NumberofCodedPFrame>1)//这个判断有点怪??
AWp=Wp/8+7*AWp/8;//书上有公式
//compute the average complexity of B frames
if(input->successive_Bframe>0)//如果存在B帧 IPBP
{
//compute the target buffer level
//TargetBufferLevel与B帧数量L(input->successive_Bframe)有关
TargetBufferLevel +=(AWp*(input->successive_Bframe+1)*bit_rate\
/(frame_rate*(AWp+AWb*input->successive_Bframe))-bit_rate/frame_rate);//这个时候TargetBufferLevel才真正求出来了
}
break;
//在图像层 作为非参考帧 PBB..BP通过相邻的P帧的QP来求中间B帧的QP
//在基本编码单元层 所有图像单元使用相同的QP
case B_SLICE:
/* update the total number of bits if the bandwidth is changed*/
if(PreviousBit_Rate!=bit_rate)//可变码率控制
R +=(int) floor((bit_rate-PreviousBit_Rate)*(Np+Nb)/frame_rate+0.5);//图像层分配的bits
if((img->NumberofCodedPFrame==1)&&(img->NumberofCodedBFrame==1))//当前GOP里面第一个B帧
{
AWp=Wp;//Wp是P帧图像的复杂度 AWp是P帧图像的平均复杂度
AWb=Wb;//Wp是B帧图像的复杂度 AWp是B帧图像的平均复杂度
}
else if(img->NumberofCodedBFrame>1)//当前GOP里面后面的B帧
{
//compute the average weight
if(img->NumberofCodedBFrame<8)
AWb=Wb*(img->NumberofCodedBFrame-1)/img->NumberofCodedBFrame+\
AWb/img->NumberofCodedBFrame;
else
AWb=Wb/8+7*AWb/8;
}
break;
}
/*Compute the target bit for each frame*/
if(img->type==P_SLICE)//计算P帧图像的目标bits
{
/*frame layer rate control*/
if(img->BasicUnit==img->Frame_Total_Number_MB)//一个基本单元所有宏块 相当于图像层编码控制
{
if(img->NumberofCodedPFrame>0)
{
//从当前GOP可用bit的角度来计算
T = (long) floor(Wp*R/(Np*Wp+Nb*Wb) + 0.5);//带有权重的计算平均一帧参考图像需要的bits
//从目标码率 帧率 缓冲区占用情况 目标缓存级别
T1 = (long) floor(bit_rate/frame_rate-GAMMAP*(CurrentBufferFullness-TargetBufferLevel)+0.5);
T1=MAX(0,T1);
T = (long)(floor(BETAP*T+(1.0-BETAP)*T1+0.5));
}
}
/*basic unit layer rate control*/
else//基本层的码率控制
{
if((img->NumberofGOP==1)&&(img->NumberofCodedPFrame>0))//第一个GOP
{
T = (int) floor(Wp*R/(Np*Wp+Nb*Wb) + 0.5);
T1 = (int) floor(bit_rate/frame_rate-GAMMAP*(CurrentBufferFullness-TargetBufferLevel)+0.5);
T1=MAX(0,T1);
T = (int)(floor(BETAP*T+(1.0-BETAP)*T1+0.5));
}
else if(img->NumberofGOP>1)//后面的GOP
{
T = (long) floor(Wp*R/(Np*Wp+Nb*Wb) + 0.5);
T1 = (long) floor(bit_rate/frame_rate-GAMMAP*(CurrentBufferFullness-TargetBufferLevel)+0.5);
T1 = MAX(0,T1);
T = (long)(floor(BETAP*T+(1.0-BETAP)*T1+0.5));
}
}
/*reserve some bits for smoothing*/
T=(long)((1.0-0.0*input->successive_Bframe)*T);//0乘?? 这句话有问题吧?????
/*HRD consideration*/
T = MAX(T, (long) LowerBound);//可变信道的最低带宽
T = MIN(T, (long) UpperBound2);//可变信道的最高带宽
if((topfield)||(fieldpic&&((input->PicInterlace==ADAPTIVE_CODING)\
||(input->MbInterlace))))
T_field=T;
}
}
if(fieldpic||topfield)//顶场
{
/*frame layer rate control*/
img->NumberofHeaderBits=0;
img->NumberofTextureBits=0;
/*basic unit layer rate control*/
if(img->BasicUnit < img->Frame_Total_Number_MB)//一个基本单元有几个宏块 基本单元层编码控制
{
TotalFrameQP=0;
img->NumberofBasicUnitHeaderBits=0;
img->NumberofBasicUnitTextureBits=0;
img->TotalMADBasicUnit=0;
if(img->FieldControl==0)
NumberofBasicUnit=TotalNumberofBasicUnit;
else
NumberofBasicUnit=TotalNumberofBasicUnit/2;
}
}
if((img->type==P_SLICE)&&(img->BasicUnit<img->Frame_Total_Number_MB)\
&&(img->FieldControl==1)) //P帧 场模式 基本单元层编码控制
{
/*top filed at basic unit layer rate control*/
if(topfield)//顶场
{
bits_topfield=0;
T=(long)(T_field*0.6);//顶场占整个帧的所有bits的0.6
}
/*bottom filed at basic unit layer rate control*/
else//底场
{
T=T_field-bits_topfield;//底场占整个帧的所有bits的0.4
img->NumberofBasicUnitHeaderBits=0;
img->NumberofBasicUnitTextureBits=0;
img->TotalMADBasicUnit=0;
NumberofBasicUnit=TotalNumberofBasicUnit/2;
}
}
}
//calculate MAD for the current macroblock
double calc_MAD()
{
int k,l;
int s = 0;
double MAD;
for (k = 0; k < 16; k++)
for (l = 0; l < 16; l++)
s+= abs(diffy[k][l]);//残差矩阵里面每一个元素绝对值求和
MAD=s*1.0/256;//因为是平均绝对误差 所有要除以16*16
return MAD;
}
// update one picture after frame/field encoding
void rc_update_pict(int nbits)// nbits是前一图像编码后的实际比特数
{
//这个地方一幅图像分配的bits才真正算完
R-= nbits; /* remaining # of bits in GOP 当前GOP的剩余bits要减去上一帧实际的产生的bits(根据码率控制计算的QP量化编码后的比特数)
*/
//当前缓冲区的充盈程度 根据漏斗原则 nbits - bit_rate/frame_rate表示净增加的bits
CurrentBufferFullness += nbits - bit_rate/frame_rate;
/*update the lower bound and the upper bound for the target bits of each frame, HRD consideration*/
LowerBound +=(long)(bit_rate/frame_rate-nbits);//(bit_rate/frame_rate-nbits)表示净输出的Bits
UpperBound1 +=(long)(bit_rate/frame_rate-nbits);
UpperBound2 = (long)(OMEGA*UpperBound1);
return;
}
// update after frame encoding 编码接收后对已编码B P帧的计数器累加,获取编码头部信息,更新图像复杂度
void rc_update_pict_frame(int nbits)
{
/*update the
complexity weight of I, P, B frame*/
int Avem_Qc;
int X;
/*frame layer rate control*/
if(img->BasicUnit==img->Frame_Total_Number_MB)//图像层码率控制
X = (int) floor(nbits*m_Qc+ 0.5);//计算复杂度??
/*basic unit layer rate control*/
else//基本编码单元层码率控制
{
if(img->type==P_SLICE)//P帧
{
if(((img->IFLAG==0)&&(img->FieldControl==1))\
||(img->FieldControl==0))
{
Avem_Qc=TotalFrameQP/TotalNumberofBasicUnit;//一幅P图像里面的平均QP,由一幅P图像里面每一个编码单元求平均计算得到
X=(int)floor(nbits*Avem_Qc+0.5);
}
}
else if(img->type==B_SLICE)//B帧
X = (int) floor(nbits*m_Qc+ 0.5);
}
switch (img->type)
{
//P帧
case P_SLICE:
/*filed coding*/
if(((img->IFLAG==0)&&(img->FieldControl==1))\
||(img->FieldControl==0))
{
Xp = X;
Np--;//NP减少 说明NP是表示当前GOP中剩余的P帧数
Wp=Xp;//计算P帧图像复杂度 与图像实际产生的比特数和平均QP有关
Pm_Hp=img->NumberofHeaderBits;//头部bit
img->NumberofCodedPFrame++;
img->NumberofPPicture++;
}
else if((img->IFLAG!=0)&&(img->FieldControl==1))
img->IFLAG=0;
break;
case B_SLICE://B帧
Xb = X;
Nb--;
Wb=Xb/THETA;// 计算B帧图像复杂度 与图像实际产生的比特数和平均QP有关
img->NumberofCodedBFrame++;
NumberofBFrames++;
break;
}
}
// coded bits for top field
void setbitscount(int nbits)
{
bits_topfield = nbits;
}
//compute a quantization parameter for each frame
//计算每一帧的量化参数 QP 得到码率控制的最终结果
int updateQuantizationParameter(int topfield)
{
double dtmp;
int m_Bits;
int BFrameNumber;
int StepSize;
int PAverageQP;
int SumofBasicUnit;
int i;
/*frame layer rate control*/
if(img->BasicUnit==img->Frame_Total_Number_MB)//基于图像层的码率控制
{
/*fixed quantization parameter is used to coded I frame, the first P frame and the first B frame
the quantization parameter is adjusted according the available channel bandwidth and
the type of vide*/
/*top field*/
if((topfield)||(img->FieldControl==0))//顶场
{
if(img->type==I_SLICE)//图像是I帧
{
m_Qc=MyInitialQp;//GOP的第一帧IDR图像QP
return m_Qc;
}
else if(img->type==B_SLICE)//B片
{
if(input->successive_Bframe==1)//一个GOP只有一个B帧 PBP
{
//PBB..BBBP BFrameNumber是当前编码帧结构里面的B帧顺序 12...56
BFrameNumber=(NumberofBFrames+1)%input->successive_Bframe;
if(BFrameNumber==0)
BFrameNumber=input->successive_Bframe;
/*adaptive field/frame coding*/
else if(BFrameNumber==1)//当前一小段(两个P帧间)里面的第一个B帧
{
if((input->PicInterlace==ADAPTIVE_CODING)\
||(input->MbInterlace))//采取交织
{
if(img->FieldControl==0)
{
/*previous choice is frame coding*/
if(img->FieldFrame==1)//帧
{
//PreviousQp2 B FrameQPBuffer
//PreviousQp1 B PreviousQp2 B FrameQPBuffer
// PreviousQp1 B PreviousQp2
PreviousQp1=PreviousQp2;//PBP里面左边的P帧QP
PreviousQp2=FrameQPBuffer;//PBP里面右边的P帧QP
}
/*previous choice is field coding*/
else//场
{
PreviousQp1=PreviousQp2;
PreviousQp2=FieldQPBuffer;
}
}
}
}
if(PreviousQp1==PreviousQp2)//PBP的结构 如果前后P帧的量化参数一样 B帧的量化参数加2
m_Qc=PreviousQp1+2;
else////PBP的结构 如果前后P帧的量化参数不一样 为了保证平滑 按照下面算
m_Qc=(PreviousQp1+PreviousQp2)/2+1;
m_Qc = MIN(m_Qc, RC_MAX_QUANT); // clipping
m_Qc = MAX(RC_MIN_QUANT, m_Qc);//clipping
}
else//一个GOP只有多个B帧 PBB..BP
{
//PBB..BBBP BFrameNumber是当前编码帧结构里面的B帧顺序 12...56
BFrameNumber=(NumberofBFrames+1)%input->successive_Bframe;
if(BFrameNumber==0)
BFrameNumber=input->successive_Bframe;
/*adaptive field/frame coding*/
else if(BFrameNumber==1)//当前一小段(两个P帧间)里面的第一个B帧
{
if((input->PicInterlace==ADAPTIVE_CODING)\
||(input->MbInterlace))
{
if(img->FieldControl==0)
{
/*previous choice is frame coding*/
if(img->FieldFrame==1)
{
PreviousQp1=PreviousQp2;//PBB...BP里面左边的P帧QP
PreviousQp2=FrameQPBuffer;//PBB...BP里面右边的P帧QP
}
/*previous choice is field coding*/
else
{
PreviousQp1=PreviousQp2;
PreviousQp2=FieldQPBuffer;
}
}
}
}
if((PreviousQp2-PreviousQp1)<=(-2*input->successive_Bframe-3))
StepSize=-3;
else if((PreviousQp2-PreviousQp1)==(-2*input->successive_Bframe-2))
StepSize=-2;
else if((PreviousQp2-PreviousQp1)==(-2*input->successive_Bframe-1))
StepSize=-1;
else if((PreviousQp2-PreviousQp1)==(-2*input->successive_Bframe))
StepSize=0;
else if((PreviousQp2-PreviousQp1)==(-2*input->successive_Bframe+1))
StepSize=1;
else
StepSize=2;
m_Qc=PreviousQp1+StepSize;
m_Qc +=MIN(2*(BFrameNumber-1),MAX(-2*(BFrameNumber-1), \
(BFrameNumber-1)*(PreviousQp2-PreviousQp1)/(input->successive_Bframe-1)));//多乘了(BFrameNumber-1)
m_Qc = MIN(m_Qc, RC_MAX_QUANT); // clipping 限幅
m_Qc = MAX(RC_MIN_QUANT, m_Qc);//clipping
}
return m_Qc;
}
else if((img->type==P_SLICE)&&(img->NumberofPPicture==0))//第一个IDR帧(I或P)
{
m_Qc=MyInitialQp;//好像直接用的I帧的QP来用的
if(img->FieldControl==0)
{
if(active_sps->frame_mbs_only_flag)
{
img->TotalQpforPPicture +=m_Qc;//保存当前的QP 累加到所有P帧QP和的变量里面
PreviousQp1=PreviousQp2;
PreviousQp2=m_Qc;
Pm_Qp=m_Qc;
}
/*adaptive field/frame coding*/
else
FrameQPBuffer=m_Qc;
}
return m_Qc;
}
else//后面的P帧
{
/*
第一步:确定每一个参考图像(P)的目标目标bits
第二步:确定参考图像的QP 运行RDO优化
*/
/*adaptive field/frame coding*/
if(((input->PicInterlace==ADAPTIVE_CODING)\
||(input->MbInterlace))\
&&(img->FieldControl==0))
{
/*previous choice is frame coding*/
if(img->FieldFrame==1)
{
img->TotalQpforPPicture +=FrameQPBuffer;
Pm_Qp=FrameQPBuffer;
}
/*previous choice is field coding*/
else
{
img->TotalQpforPPicture +=FieldQPBuffer;
Pm_Qp=FieldQPBuffer;//前一帧P帧的QP
}
}
//Pm_X1=bit_rate*1.0;//RD模型参数
//Pm_X2=0.0;
m_X1=Pm_X1;//RD模型的两个参数
m_X2=Pm_X2;
m_Hp=PPreHeader;
m_Qp=Pm_Qp;//前一帧P帧的QP
DuantQp=PDuantQp;//量化参数最大的变化量 2
MADPictureC1=PMADPictureC1;//PMADPictureC1=1 线性预测的两个参数
MADPictureC2=PMADPictureC2;//PMADPictureC1=0
PreviousPictureMAD=PPictureMAD[0];
/* predict the MAD of current picture*/
CurrentFrameMAD=MADPictureC1*PreviousPictureMAD+MADPictureC2;//线性预测当前帧的MAD
/*compute the number of bits for the texture*/
if(T<0)//当T小于0 直接使用前一个P帧的QP来预测 然后加2 得到当前的QP值
{
m_Qc=m_Qp+DuantQp;//前一帧P帧的QP加2 DuantQp=2
m_Qc = MIN(m_Qc, RC_MAX_QUANT); // clipping
}
else//当T大于0 执行RD优化
{
m_Bits =T-m_Hp;//减去头部bits ??
m_Bits = MAX(m_Bits, (int)(bit_rate/(MINVALUE*frame_rate)));//MINVALUE=4 确定一个最大值上界
//求解m_Bits(Qstep)=X1*D/Qstep+x2*D/(Qstep*Qstep)一元二次方程解
dtmp = CurrentFrameMAD * m_X1 * CurrentFrameMAD * m_X1 \
+ 4 * m_X2 * CurrentFrameMAD * m_Bits;//判别式
if ((m_X2 == 0.0) || (dtmp < 0) || ((sqrt (dtmp) - m_X1 * CurrentFrameMAD) <= 0.0)) // fall back 1st order mode
m_Qstep = (float) (m_X1 * CurrentFrameMAD / (double) m_Bits);
else // 2nd order mode
m_Qstep = (float) ((2 * m_X2 * CurrentFrameMAD) / (sqrt (dtmp) - m_X1 * CurrentFrameMAD));
m_Qc=Qstep2QP(m_Qstep);//0.625到224的Qstep映射到QP 0到51
//m_Qp是前一P帧QP的预测
m_Qc = MIN(m_Qp+DuantQp, m_Qc); // control variation
m_Qc = MIN(m_Qc, RC_MAX_QUANT); // clipping
m_Qc = MAX(m_Qp-DuantQp, m_Qc); // control variation
m_Qc = MAX(RC_MIN_QUANT, m_Qc);// QP平滑和限幅
}
if(img->FieldControl==0)
{
/*frame coding*/
if(active_sps->frame_mbs_only_flag)
{
img->TotalQpforPPicture +=m_Qc;//记录累加现在的QP 求解下一个GOP的IDR帧的QP
PreviousQp1=PreviousQp2;//记录相邻P帧的QP 为了计算B帧QP
PreviousQp2=m_Qc;
Pm_Qp=m_Qc;//当前P帧的QP对下一帧P帧预测
}
/*adaptive field/frame coding*/
else
FrameQPBuffer=m_Qc;
}
return m_Qc;
}
}
/*bottom field*/
else//底场
{
if((img->type==P_SLICE)&&(img->IFLAG==0))
{
/*field coding*/
if(input->PicInterlace==FIELD_CODING)
{
img->TotalQpforPPicture +=m_Qc;//从什么地方算的??
PreviousQp1=PreviousQp2+1;
PreviousQp2=m_Qc;//+0 Recent change 13/1/2003
Pm_Qp=m_Qc;
}
/*adaptive field/frame coding*/
else
FieldQPBuffer=m_Qc;
}
return m_Qc;
}
}
/*basic unit layer rate control*/
else //基本编码单元层的码率控制 I帧和P帧都和前面图像层码率控制一样
{
/*top filed of I frame*/
if(img->type==I_SLICE)//I帧内所有编码单元的宏块QP相同
{
m_Qc=MyInitialQp;
return m_Qc;
}
/*bottom field of I frame*/
else if((img->type==P_SLICE)&&(img->IFLAG==1)&&(img->FieldControl==1))
{
m_Qc=MyInitialQp;
return m_Qc;
}
else if(img->type==B_SLICE)//B帧
{
/*top filed of B frame*/
if((topfield)||(img->FieldControl==0))//B帧顶场
{
if(input->successive_Bframe==1)//PBP 只有一个B
{
BFrameNumber=(NumberofBFrames+1)%input->successive_Bframe;
if(BFrameNumber==0)
BFrameNumber=input->successive_Bframe;
/*adaptive field/frame coding*/
else if(BFrameNumber==1)
{
if((input->PicInterlace==ADAPTIVE_CODING)\
||(input->MbInterlace))
{
if(img->FieldControl==0)
{
/*previous choice is frame coding*/
if(img->FieldFrame==1)
{
PreviousQp1=PreviousQp2;
PreviousQp2=FrameQPBuffer;
}
/*previous choice is field coding*/
else
{
PreviousQp1=PreviousQp2;
PreviousQp2=FieldQPBuffer;
}
}
}
}
if(PreviousQp1==PreviousQp2)
m_Qc=PreviousQp1+2;
else
m_Qc=(PreviousQp1+PreviousQp2)/2+1;
m_Qc = MIN(m_Qc, RC_MAX_QUANT); // clipping
m_Qc = MAX(RC_MIN_QUANT, m_Qc);//clipping
}
else//PBB...BP形式
{
/*
比如PBBBBP input->successive_Bframe=4
NumberofBFrames=0 BFrameNumber=1
NumberofBFrames=1 BFrameNumber=2
NumberofBFrames=2 BFrameNumber=3
NumberofBFrames=3 BFrameNumber=0
BFrameNumber是一个帧结构内部的B帧顺序
*/
BFrameNumber=(NumberofBFrames+1)%input->successive_Bframe;
if(BFrameNumber==0)
BFrameNumber=input->successive_Bframe;
/*adaptive field/frame coding*/
else if(BFrameNumber==1)//PBBBP的第一个B
{
if((input->PicInterlace==ADAPTIVE_CODING)\
||(input->MbInterlace))
{
if(img->FieldControl==0)
{
/*previous choice is frame coding*/
if(img->FieldFrame==1)
{
/*
PreviousQp2 FrameQPBuffer
PreviousQp1 PreviousQp2 FrameQPBuffer
PreviousQp1 PreviousQp2 FrameQPBuffer
*/
PreviousQp1=PreviousQp2;
PreviousQp2=FrameQPBuffer;
}
/*previous choice is field coding*/
else
{
PreviousQp1=PreviousQp2;
PreviousQp2=FieldQPBuffer;
}
}
}
}
if((PreviousQp2-PreviousQp1)<=(-2*input->successive_Bframe-3))
StepSize=-3;
else if((PreviousQp2-PreviousQp1)==(-2*input->successive_Bframe-2))
StepSize=-2;
else if((PreviousQp2-PreviousQp1)==(-2*input->successive_Bframe-1))
StepSize=-1;
else if((PreviousQp2-PreviousQp1)==(-2*input->successive_Bframe))
StepSize=0;//0
else if((PreviousQp2-PreviousQp1)==(-2*input->successive_Bframe+1))
StepSize=1;//1
else
StepSize=2;//2
m_Qc=PreviousQp1+StepSize;
m_Qc +=MIN(2*(BFrameNumber-1),MAX(-2*(BFrameNumber-1), \
(BFrameNumber-1)*(PreviousQp2-PreviousQp1)/(input->successive_Bframe-1)));
m_Qc = MIN(m_Qc, RC_MAX_QUANT); // clipping
m_Qc = MAX(RC_MIN_QUANT, m_Qc);//clipping
}
return m_Qc;
}
/*bottom field of B frame*/
else
return m_Qc;
}
else if(img->type==P_SLICE)//P帧内基本编码单元层的码率控制
{
if((img->NumberofGOP==1)&&(img->NumberofPPicture==0))//第一个GOP的第一个P帧
{
if((img->FieldControl==0)||((img->FieldControl==1)\
&&(img->IFLAG==0)))
{
/*top field of the first P frame*/
m_Qc=MyInitialQp;//和I帧一样 使用fix的方法
img->NumberofBasicUnitHeaderBits=0;
img->NumberofBasicUnitTextureBits=0;
NumberofBasicUnit--;//基本单元数量逐减
/*bottom field of the first P frame*/
if((!topfield)&&(NumberofBasicUnit==0))//底场的第一个基本单元
{
/*frame coding or field coding*/
if((active_sps->frame_mbs_only_flag)||(input->PicInterlace==FIELD_CODING))//帧或者场
{
img->TotalQpforPPicture +=m_Qc;
PreviousQp1=PreviousQp2;
PreviousQp2=m_Qc;
PAveFrameQP=m_Qc;//保存当前的QP ??
PAveHeaderBits3=PAveHeaderBits2;//头部信息bits??
}
/*adaptive frame/field coding*/
else if((input->PicInterlace==ADAPTIVE_CODING)\
||(input->MbInterlace))//交织
{
if(img->FieldControl==0)//顶场
{
FrameQPBuffer=m_Qc;
FrameAveHeaderBits=PAveHeaderBits2;
}
else//底场
{
FieldQPBuffer=m_Qc;
FieldAveHeaderBits=PAveHeaderBits2;
}
}
}
Pm_Qp=m_Qc;//给后面预测
TotalFrameQP +=m_Qc;//当前帧内的QP数累加
return m_Qc;
}
}
else// 非第一帧的P帧 使用RD优化算法
{
m_X1=Pm_X1;//RD优化的两个参数
m_X2=Pm_X2;
m_Hp=PPreHeader;//头部信息
m_Qp=Pm_Qp;//预测QP
DuantQp=PDuantQp;//QP的偏移量2
MADPictureC1=PMADPictureC1;//MAD线性预测的参数
MADPictureC2=PMADPictureC2;
if(img->FieldControl==0)//底场
SumofBasicUnit=TotalNumberofBasicUnit;
else//顶场
SumofBasicUnit=TotalNumberofBasicUnit/2;
/*
TotalNumberofBasicUnit:是总的基本编码单元数
NumberofBasicUnit:说明这个值最大是第一个编码单元 为0是最后一个编码单元