HEVC Test Model (HM)  HM-16.18
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TEncRateCtrl.cpp
Go to the documentation of this file.
1 /* The copyright in this software is being made available under the BSD
2  * License, included below. This software may be subject to other third party
3  * and contributor rights, including patent rights, and no such rights are
4  * granted under this license.
5  *
6  * Copyright (c) 2010-2017, ITU/ISO/IEC
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18  * be used to endorse or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
37 #include "TEncRateCtrl.h"
38 #include "../TLibCommon/TComPic.h"
39 #include "../TLibCommon/TComChromaFormat.h"
40 
41 #include <cmath>
42 
43 using namespace std;
44 
45 //sequence level
47 {
48  m_totalFrames = 0;
49  m_targetRate = 0;
50  m_frameRate = 0;
51  m_targetBits = 0;
52  m_GOPSize = 0;
53  m_picWidth = 0;
54  m_picHeight = 0;
55  m_LCUWidth = 0;
56  m_LCUHeight = 0;
57  m_numberOfLevel = 0;
58  m_numberOfLCU = 0;
59  m_averageBits = 0;
60  m_bitsRatio = NULL;
61  m_GOPID2Level = NULL;
62  m_picPara = NULL;
63  m_LCUPara = NULL;
64  m_numberOfPixel = 0;
65  m_framesLeft = 0;
66  m_bitsLeft = 0;
67  m_useLCUSeparateModel = false;
68  m_adaptiveBit = 0;
69  m_lastLambda = 0.0;
70 }
71 
73 {
74  destroy();
75 }
76 
77 Void TEncRCSeq::create( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int numberOfLevel, Bool useLCUSeparateModel, Int adaptiveBit )
78 {
79  destroy();
80  m_totalFrames = totalFrames;
81  m_targetRate = targetBitrate;
82  m_frameRate = frameRate;
83  m_GOPSize = GOPSize;
84  m_picWidth = picWidth;
85  m_picHeight = picHeight;
86  m_LCUWidth = LCUWidth;
87  m_LCUHeight = LCUHeight;
88  m_numberOfLevel = numberOfLevel;
89  m_useLCUSeparateModel = useLCUSeparateModel;
90 
91  m_numberOfPixel = m_picWidth * m_picHeight;
92  m_targetBits = (Int64)m_totalFrames * (Int64)m_targetRate / (Int64)m_frameRate;
93  m_seqTargetBpp = (Double)m_targetRate / (Double)m_frameRate / (Double)m_numberOfPixel;
94  if ( m_seqTargetBpp < 0.03 )
95  {
96  m_alphaUpdate = 0.01;
97  m_betaUpdate = 0.005;
98  }
99  else if ( m_seqTargetBpp < 0.08 )
100  {
101  m_alphaUpdate = 0.05;
102  m_betaUpdate = 0.025;
103  }
104  else if ( m_seqTargetBpp < 0.2 )
105  {
106  m_alphaUpdate = 0.1;
107  m_betaUpdate = 0.05;
108  }
109  else if ( m_seqTargetBpp < 0.5 )
110  {
111  m_alphaUpdate = 0.2;
112  m_betaUpdate = 0.1;
113  }
114  else
115  {
116  m_alphaUpdate = 0.4;
117  m_betaUpdate = 0.2;
118  }
119 
120  m_averageBits = (Int)(m_targetBits / totalFrames);
121  Int picWidthInBU = ( m_picWidth % m_LCUWidth ) == 0 ? m_picWidth / m_LCUWidth : m_picWidth / m_LCUWidth + 1;
122  Int picHeightInBU = ( m_picHeight % m_LCUHeight ) == 0 ? m_picHeight / m_LCUHeight : m_picHeight / m_LCUHeight + 1;
123  m_numberOfLCU = picWidthInBU * picHeightInBU;
124 
125  m_bitsRatio = new Int[m_GOPSize];
126  for ( Int i=0; i<m_GOPSize; i++ )
127  {
128  m_bitsRatio[i] = 1;
129  }
130 
131  m_GOPID2Level = new Int[m_GOPSize];
132  for ( Int i=0; i<m_GOPSize; i++ )
133  {
134  m_GOPID2Level[i] = 1;
135  }
136 
137  m_picPara = new TRCParameter[m_numberOfLevel];
138  for ( Int i=0; i<m_numberOfLevel; i++ )
139  {
140  m_picPara[i].m_alpha = 0.0;
141  m_picPara[i].m_beta = 0.0;
142  }
143 
144  if ( m_useLCUSeparateModel )
145  {
146  m_LCUPara = new TRCParameter*[m_numberOfLevel];
147  for ( Int i=0; i<m_numberOfLevel; i++ )
148  {
149  m_LCUPara[i] = new TRCParameter[m_numberOfLCU];
150  for ( Int j=0; j<m_numberOfLCU; j++)
151  {
152  m_LCUPara[i][j].m_alpha = 0.0;
153  m_LCUPara[i][j].m_beta = 0.0;
154  }
155  }
156  }
157 
158  m_framesLeft = m_totalFrames;
159  m_bitsLeft = m_targetBits;
160  m_adaptiveBit = adaptiveBit;
161  m_lastLambda = 0.0;
162 }
163 
165 {
166  if (m_bitsRatio != NULL)
167  {
168  delete[] m_bitsRatio;
169  m_bitsRatio = NULL;
170  }
171 
172  if ( m_GOPID2Level != NULL )
173  {
174  delete[] m_GOPID2Level;
175  m_GOPID2Level = NULL;
176  }
177 
178  if ( m_picPara != NULL )
179  {
180  delete[] m_picPara;
181  m_picPara = NULL;
182  }
183 
184  if ( m_LCUPara != NULL )
185  {
186  for ( Int i=0; i<m_numberOfLevel; i++ )
187  {
188  delete[] m_LCUPara[i];
189  }
190  delete[] m_LCUPara;
191  m_LCUPara = NULL;
192  }
193 }
194 
196 {
197  for (Int i=0; i<m_GOPSize; i++)
198  {
199  m_bitsRatio[i] = bitsRatio[i];
200  }
201 }
202 
204 {
205  for ( Int i=0; i<m_GOPSize; i++ )
206  {
207  m_GOPID2Level[i] = GOPID2Level[i];
208  }
209 }
210 
212 {
213  assert( m_picPara != NULL );
214 
215  if ( picPara == NULL )
216  {
217  for ( Int i=0; i<m_numberOfLevel; i++ )
218  {
219  if (i>0)
220  {
221  m_picPara[i].m_alpha = 3.2003;
222  m_picPara[i].m_beta = -1.367;
223  }
224  else
225  {
226  m_picPara[i].m_alpha = ALPHA;
227  m_picPara[i].m_beta = BETA2;
228  }
229  }
230  }
231  else
232  {
233  for ( Int i=0; i<m_numberOfLevel; i++ )
234  {
235  m_picPara[i] = picPara[i];
236  }
237  }
238 }
239 
241 {
242  if ( m_LCUPara == NULL )
243  {
244  return;
245  }
246  if ( LCUPara == NULL )
247  {
248  for ( Int i=0; i<m_numberOfLevel; i++ )
249  {
250  for ( Int j=0; j<m_numberOfLCU; j++)
251  {
252  m_LCUPara[i][j].m_alpha = m_picPara[i].m_alpha;
253  m_LCUPara[i][j].m_beta = m_picPara[i].m_beta;
254  }
255  }
256  }
257  else
258  {
259  for ( Int i=0; i<m_numberOfLevel; i++ )
260  {
261  for ( Int j=0; j<m_numberOfLCU; j++)
262  {
263  m_LCUPara[i][j] = LCUPara[i][j];
264  }
265  }
266  }
267 }
268 
270 {
271  m_bitsLeft -= bits;
272  m_framesLeft--;
273 }
274 
275 Void TEncRCSeq::setAllBitRatio( Double basicLambda, Double* equaCoeffA, Double* equaCoeffB )
276 {
277  Int* bitsRatio = new Int[m_GOPSize];
278  for ( Int i=0; i<m_GOPSize; i++ )
279  {
280  bitsRatio[i] = (Int)( equaCoeffA[i] * pow( basicLambda, equaCoeffB[i] ) * m_numberOfPixel );
281  }
282  initBitsRatio( bitsRatio );
283  delete[] bitsRatio;
284 }
285 
286 //GOP level
288 {
289  m_encRCSeq = NULL;
290  m_picTargetBitInGOP = NULL;
291  m_numPic = 0;
292  m_targetBits = 0;
293  m_picLeft = 0;
294  m_bitsLeft = 0;
295 }
296 
298 {
299  destroy();
300 }
301 
302 Void TEncRCGOP::create( TEncRCSeq* encRCSeq, Int numPic )
303 {
304  destroy();
305  Int targetBits = xEstGOPTargetBits( encRCSeq, numPic );
306 
307  if ( encRCSeq->getAdaptiveBits() > 0 && encRCSeq->getLastLambda() > 0.1 )
308  {
309  Double targetBpp = (Double)targetBits / encRCSeq->getNumPixel();
310  Double basicLambda = 0.0;
311  Double* lambdaRatio = new Double[encRCSeq->getGOPSize()];
312  Double* equaCoeffA = new Double[encRCSeq->getGOPSize()];
313  Double* equaCoeffB = new Double[encRCSeq->getGOPSize()];
314 
315  if ( encRCSeq->getAdaptiveBits() == 1 ) // for GOP size =4, low delay case
316  {
317  if ( encRCSeq->getLastLambda() < 120.0 )
318  {
319  lambdaRatio[1] = 0.725 * log( encRCSeq->getLastLambda() ) + 0.5793;
320  lambdaRatio[0] = 1.3 * lambdaRatio[1];
321  lambdaRatio[2] = 1.3 * lambdaRatio[1];
322  lambdaRatio[3] = 1.0;
323  }
324  else
325  {
326  lambdaRatio[0] = 5.0;
327  lambdaRatio[1] = 4.0;
328  lambdaRatio[2] = 5.0;
329  lambdaRatio[3] = 1.0;
330  }
331  }
332  else if ( encRCSeq->getAdaptiveBits() == 2 ) // for GOP size = 8, random access case
333  {
334  if ( encRCSeq->getLastLambda() < 90.0 )
335  {
336  lambdaRatio[0] = 1.0;
337  lambdaRatio[1] = 0.725 * log( encRCSeq->getLastLambda() ) + 0.7963;
338  lambdaRatio[2] = 1.3 * lambdaRatio[1];
339  lambdaRatio[3] = 3.25 * lambdaRatio[1];
340  lambdaRatio[4] = 3.25 * lambdaRatio[1];
341  lambdaRatio[5] = 1.3 * lambdaRatio[1];
342  lambdaRatio[6] = 3.25 * lambdaRatio[1];
343  lambdaRatio[7] = 3.25 * lambdaRatio[1];
344  }
345  else
346  {
347  lambdaRatio[0] = 1.0;
348  lambdaRatio[1] = 4.0;
349  lambdaRatio[2] = 5.0;
350  lambdaRatio[3] = 12.3;
351  lambdaRatio[4] = 12.3;
352  lambdaRatio[5] = 5.0;
353  lambdaRatio[6] = 12.3;
354  lambdaRatio[7] = 12.3;
355  }
356  }
357 
358  xCalEquaCoeff( encRCSeq, lambdaRatio, equaCoeffA, equaCoeffB, encRCSeq->getGOPSize() );
359  basicLambda = xSolveEqua( targetBpp, equaCoeffA, equaCoeffB, encRCSeq->getGOPSize() );
360  encRCSeq->setAllBitRatio( basicLambda, equaCoeffA, equaCoeffB );
361 
362  delete []lambdaRatio;
363  delete []equaCoeffA;
364  delete []equaCoeffB;
365  }
366 
367  m_picTargetBitInGOP = new Int[numPic];
368  Int i;
369  Int totalPicRatio = 0;
370  Int currPicRatio = 0;
371  for ( i=0; i<numPic; i++ )
372  {
373  totalPicRatio += encRCSeq->getBitRatio( i );
374  }
375  for ( i=0; i<numPic; i++ )
376  {
377  currPicRatio = encRCSeq->getBitRatio( i );
378  m_picTargetBitInGOP[i] = (Int)( ((Double)targetBits) * currPicRatio / totalPicRatio );
379  }
380 
381  m_encRCSeq = encRCSeq;
382  m_numPic = numPic;
383  m_targetBits = targetBits;
384  m_picLeft = m_numPic;
385  m_bitsLeft = m_targetBits;
386 }
387 
388 Void TEncRCGOP::xCalEquaCoeff( TEncRCSeq* encRCSeq, Double* lambdaRatio, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize )
389 {
390  for ( Int i=0; i<GOPSize; i++ )
391  {
392  Int frameLevel = encRCSeq->getGOPID2Level(i);
393  Double alpha = encRCSeq->getPicPara(frameLevel).m_alpha;
394  Double beta = encRCSeq->getPicPara(frameLevel).m_beta;
395  equaCoeffA[i] = pow( 1.0/alpha, 1.0/beta ) * pow( lambdaRatio[i], 1.0/beta );
396  equaCoeffB[i] = 1.0/beta;
397  }
398 }
399 
400 Double TEncRCGOP::xSolveEqua( Double targetBpp, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize )
401 {
402  Double solution = 100.0;
403  Double minNumber = 0.1;
404  Double maxNumber = 10000.0;
405  for ( Int i=0; i<g_RCIterationNum; i++ )
406  {
407  Double fx = 0.0;
408  for ( Int j=0; j<GOPSize; j++ )
409  {
410  fx += equaCoeffA[j] * pow( solution, equaCoeffB[j] );
411  }
412 
413  if ( fabs( fx - targetBpp ) < 0.000001 )
414  {
415  break;
416  }
417 
418  if ( fx > targetBpp )
419  {
420  minNumber = solution;
421  solution = ( solution + maxNumber ) / 2.0;
422  }
423  else
424  {
425  maxNumber = solution;
426  solution = ( solution + minNumber ) / 2.0;
427  }
428  }
429 
430  solution = Clip3( 0.1, 10000.0, solution );
431  return solution;
432 }
433 
435 {
436  m_encRCSeq = NULL;
437  if ( m_picTargetBitInGOP != NULL )
438  {
439  delete[] m_picTargetBitInGOP;
440  m_picTargetBitInGOP = NULL;
441  }
442 }
443 
445 {
446  m_bitsLeft -= bitsCost;
447  m_picLeft--;
448 }
449 
451 {
452  Int realInfluencePicture = min( g_RCSmoothWindowSize, encRCSeq->getFramesLeft() );
453  Int averageTargetBitsPerPic = (Int)( encRCSeq->getTargetBits() / encRCSeq->getTotalFrames() );
454  Int currentTargetBitsPerPic = (Int)( ( encRCSeq->getBitsLeft() - averageTargetBitsPerPic * (encRCSeq->getFramesLeft() - realInfluencePicture) ) / realInfluencePicture );
455  Int targetBits = currentTargetBitsPerPic * GOPSize;
456 
457  if ( targetBits < 200 )
458  {
459  targetBits = 200; // at least allocate 200 bits for one GOP
460  }
461 
462  return targetBits;
463 }
464 
465 //picture level
467 {
468  m_encRCSeq = NULL;
469  m_encRCGOP = NULL;
470 
471  m_frameLevel = 0;
472  m_numberOfPixel = 0;
473  m_numberOfLCU = 0;
474  m_targetBits = 0;
475  m_estHeaderBits = 0;
476  m_estPicQP = 0;
477  m_estPicLambda = 0.0;
478 
479  m_LCULeft = 0;
480  m_bitsLeft = 0;
481  m_pixelsLeft = 0;
482 
483  m_LCUs = NULL;
484  m_picActualHeaderBits = 0;
485  m_picActualBits = 0;
486  m_picQP = 0;
487  m_picLambda = 0.0;
488 }
489 
491 {
492  destroy();
493 }
494 
496 {
497  Int targetBits = 0;
498  Int GOPbitsLeft = encRCGOP->getBitsLeft();
499 
500  Int i;
501  Int currPicPosition = encRCGOP->getNumPic()-encRCGOP->getPicLeft();
502  Int currPicRatio = encRCSeq->getBitRatio( currPicPosition );
503  Int totalPicRatio = 0;
504  for ( i=currPicPosition; i<encRCGOP->getNumPic(); i++ )
505  {
506  totalPicRatio += encRCSeq->getBitRatio( i );
507  }
508 
509  targetBits = Int( ((Double)GOPbitsLeft) * currPicRatio / totalPicRatio );
510 
511  if ( targetBits < 100 )
512  {
513  targetBits = 100; // at least allocate 100 bits for one picture
514  }
515 
516  if ( m_encRCSeq->getFramesLeft() > 16 )
517  {
518  targetBits = Int( g_RCWeightPicRargetBitInBuffer * targetBits + g_RCWeightPicTargetBitInGOP * m_encRCGOP->getTargetBitInGOP( currPicPosition ) );
519  }
520 
521  return targetBits;
522 }
523 
524 Int TEncRCPic::xEstPicHeaderBits( list<TEncRCPic*>& listPreviousPictures, Int frameLevel )
525 {
526  Int numPreviousPics = 0;
527  Int totalPreviousBits = 0;
528 
529  list<TEncRCPic*>::iterator it;
530  for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ )
531  {
532  if ( (*it)->getFrameLevel() == frameLevel )
533  {
534  totalPreviousBits += (*it)->getPicActualHeaderBits();
535  numPreviousPics++;
536  }
537  }
538 
539  Int estHeaderBits = 0;
540  if ( numPreviousPics > 0 )
541  {
542  estHeaderBits = totalPreviousBits / numPreviousPics;
543  }
544 
545  return estHeaderBits;
546 }
547 
549 {
550  Int lowerBound = 0;
551  Int GOPbitsLeft = encRCGOP->getBitsLeft();
552 
553  const Int nextPicPosition = (encRCGOP->getNumPic() - encRCGOP->getPicLeft() + 1) % encRCGOP->getNumPic();
554  const Int nextPicRatio = encRCSeq->getBitRatio(nextPicPosition);
555 
556  Int totalPicRatio = 0;
557  for (Int i = nextPicPosition; i < encRCGOP->getNumPic(); i++)
558  {
559  totalPicRatio += encRCSeq->getBitRatio(i);
560  }
561 
562  if (nextPicPosition == 0)
563  {
564  GOPbitsLeft = encRCGOP->getTargetBits();
565  }
566  else
567  {
568  GOPbitsLeft -= m_targetBits;
569  }
570 
571  lowerBound = Int(((Double)GOPbitsLeft) * nextPicRatio / totalPicRatio);
572 
573  if (lowerBound < 100)
574  {
575  lowerBound = 100; // at least allocate 100 bits for one picture
576  }
577 
578  if (m_encRCSeq->getFramesLeft() > 16)
579  {
580  lowerBound = Int(g_RCWeightPicRargetBitInBuffer * lowerBound + g_RCWeightPicTargetBitInGOP * m_encRCGOP->getTargetBitInGOP(nextPicPosition));
581  }
582 
583  return lowerBound;
584 }
585 
586 Void TEncRCPic::addToPictureLsit( list<TEncRCPic*>& listPreviousPictures )
587 {
588  if ( listPreviousPictures.size() > g_RCMaxPicListSize )
589  {
590  TEncRCPic* p = listPreviousPictures.front();
591  listPreviousPictures.pop_front();
592  p->destroy();
593  delete p;
594  }
595 
596  listPreviousPictures.push_back( this );
597 }
598 
599 Void TEncRCPic::create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures )
600 {
601  destroy();
602  m_encRCSeq = encRCSeq;
603  m_encRCGOP = encRCGOP;
604 
605  Int targetBits = xEstPicTargetBits( encRCSeq, encRCGOP );
606  Int estHeaderBits = xEstPicHeaderBits( listPreviousPictures, frameLevel );
607 
608  if ( targetBits < estHeaderBits + 100 )
609  {
610  targetBits = estHeaderBits + 100; // at least allocate 100 bits for picture data
611  }
612 
613  m_frameLevel = frameLevel;
614  m_numberOfPixel = encRCSeq->getNumPixel();
615  m_numberOfLCU = encRCSeq->getNumberOfLCU();
616  m_estPicLambda = 100.0;
617  m_targetBits = targetBits;
618  m_estHeaderBits = estHeaderBits;
619  m_bitsLeft = m_targetBits;
620  Int picWidth = encRCSeq->getPicWidth();
621  Int picHeight = encRCSeq->getPicHeight();
622  Int LCUWidth = encRCSeq->getLCUWidth();
623  Int LCUHeight = encRCSeq->getLCUHeight();
624  Int picWidthInLCU = ( picWidth % LCUWidth ) == 0 ? picWidth / LCUWidth : picWidth / LCUWidth + 1;
625  Int picHeightInLCU = ( picHeight % LCUHeight ) == 0 ? picHeight / LCUHeight : picHeight / LCUHeight + 1;
626  m_lowerBound = xEstPicLowerBound( encRCSeq, encRCGOP );
627 
628  m_LCULeft = m_numberOfLCU;
629  m_bitsLeft -= m_estHeaderBits;
630  m_pixelsLeft = m_numberOfPixel;
631 
632  m_LCUs = new TRCLCU[m_numberOfLCU];
633  Int i, j;
634  Int LCUIdx;
635  for ( i=0; i<picWidthInLCU; i++ )
636  {
637  for ( j=0; j<picHeightInLCU; j++ )
638  {
639  LCUIdx = j*picWidthInLCU + i;
640  m_LCUs[LCUIdx].m_actualBits = 0;
641  m_LCUs[LCUIdx].m_QP = 0;
642  m_LCUs[LCUIdx].m_lambda = 0.0;
643  m_LCUs[LCUIdx].m_targetBits = 0;
644  m_LCUs[LCUIdx].m_bitWeight = 1.0;
645  Int currWidth = ( (i == picWidthInLCU -1) ? picWidth - LCUWidth *(picWidthInLCU -1) : LCUWidth );
646  Int currHeight = ( (j == picHeightInLCU-1) ? picHeight - LCUHeight*(picHeightInLCU-1) : LCUHeight );
647  m_LCUs[LCUIdx].m_numberOfPixel = currWidth * currHeight;
648  }
649  }
650  m_picActualHeaderBits = 0;
651  m_picActualBits = 0;
652  m_picQP = 0;
653  m_picLambda = 0.0;
654 }
655 
657 {
658  if( m_LCUs != NULL )
659  {
660  delete[] m_LCUs;
661  m_LCUs = NULL;
662  }
663  m_encRCSeq = NULL;
664  m_encRCGOP = NULL;
665 }
666 
667 
668 Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures, SliceType eSliceType)
669 {
670  Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
671  Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
672  Double bpp = (Double)m_targetBits/(Double)m_numberOfPixel;
673  Double estLambda;
674  if (eSliceType == I_SLICE)
675  {
676  estLambda = calculateLambdaIntra(alpha, beta, pow(m_totalCostIntra/(Double)m_numberOfPixel, BETA1), bpp);
677  }
678  else
679  {
680  estLambda = alpha * pow( bpp, beta );
681  }
682 
683  Double lastLevelLambda = -1.0;
684  Double lastPicLambda = -1.0;
685  Double lastValidLambda = -1.0;
686  list<TEncRCPic*>::iterator it;
687  for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ )
688  {
689  if ( (*it)->getFrameLevel() == m_frameLevel )
690  {
691  lastLevelLambda = (*it)->getPicActualLambda();
692  }
693  lastPicLambda = (*it)->getPicActualLambda();
694 
695  if ( lastPicLambda > 0.0 )
696  {
697  lastValidLambda = lastPicLambda;
698  }
699  }
700 
701  if ( lastLevelLambda > 0.0 )
702  {
703  lastLevelLambda = Clip3( 0.1, 10000.0, lastLevelLambda );
704  estLambda = Clip3( lastLevelLambda * pow( 2.0, -3.0/3.0 ), lastLevelLambda * pow( 2.0, 3.0/3.0 ), estLambda );
705  }
706 
707  if ( lastPicLambda > 0.0 )
708  {
709  lastPicLambda = Clip3( 0.1, 2000.0, lastPicLambda );
710  estLambda = Clip3( lastPicLambda * pow( 2.0, -10.0/3.0 ), lastPicLambda * pow( 2.0, 10.0/3.0 ), estLambda );
711  }
712  else if ( lastValidLambda > 0.0 )
713  {
714  lastValidLambda = Clip3( 0.1, 2000.0, lastValidLambda );
715  estLambda = Clip3( lastValidLambda * pow(2.0, -10.0/3.0), lastValidLambda * pow(2.0, 10.0/3.0), estLambda );
716  }
717  else
718  {
719  estLambda = Clip3( 0.1, 10000.0, estLambda );
720  }
721 
722  if ( estLambda < 0.1 )
723  {
724  estLambda = 0.1;
725  }
726 
727  m_estPicLambda = estLambda;
728 
729  Double totalWeight = 0.0;
730  // initial BU bit allocation weight
731  for ( Int i=0; i<m_numberOfLCU; i++ )
732  {
733  Double alphaLCU, betaLCU;
734  if ( m_encRCSeq->getUseLCUSeparateModel() )
735  {
736  alphaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_alpha;
737  betaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_beta;
738  }
739  else
740  {
741  alphaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
742  betaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
743  }
744 
745  m_LCUs[i].m_bitWeight = m_LCUs[i].m_numberOfPixel * pow( estLambda/alphaLCU, 1.0/betaLCU );
746 
747  if ( m_LCUs[i].m_bitWeight < 0.01 )
748  {
749  m_LCUs[i].m_bitWeight = 0.01;
750  }
751  totalWeight += m_LCUs[i].m_bitWeight;
752  }
753  for ( Int i=0; i<m_numberOfLCU; i++ )
754  {
755  Double BUTargetBits = m_targetBits * m_LCUs[i].m_bitWeight / totalWeight;
756  m_LCUs[i].m_bitWeight = BUTargetBits;
757  }
758 
759  return estLambda;
760 }
761 
762 Int TEncRCPic::estimatePicQP( Double lambda, list<TEncRCPic*>& listPreviousPictures )
763 {
764  Int QP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 );
765 
766  Int lastLevelQP = g_RCInvalidQPValue;
767  Int lastPicQP = g_RCInvalidQPValue;
768  Int lastValidQP = g_RCInvalidQPValue;
769  list<TEncRCPic*>::iterator it;
770  for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ )
771  {
772  if ( (*it)->getFrameLevel() == m_frameLevel )
773  {
774  lastLevelQP = (*it)->getPicActualQP();
775  }
776  lastPicQP = (*it)->getPicActualQP();
777  if ( lastPicQP > g_RCInvalidQPValue )
778  {
779  lastValidQP = lastPicQP;
780  }
781  }
782 
783  if ( lastLevelQP > g_RCInvalidQPValue )
784  {
785  QP = Clip3( lastLevelQP - 3, lastLevelQP + 3, QP );
786  }
787 
788  if( lastPicQP > g_RCInvalidQPValue )
789  {
790  QP = Clip3( lastPicQP - 10, lastPicQP + 10, QP );
791  }
792  else if( lastValidQP > g_RCInvalidQPValue )
793  {
794  QP = Clip3( lastValidQP - 10, lastValidQP + 10, QP );
795  }
796 
797  return QP;
798 }
799 
801 {
802  Int LCUIdx = getLCUCoded();
803  Double bpp = -1.0;
804  Int avgBits = 0;
805 
806  if (eSliceType == I_SLICE)
807  {
808  Int noOfLCUsLeft = m_numberOfLCU - LCUIdx + 1;
809  Int bitrateWindow = min(4,noOfLCUsLeft);
810  Double MAD = getLCU(LCUIdx).m_costIntra;
811 
812  if (m_remainingCostIntra > 0.1 )
813  {
814  Double weightedBitsLeft = (m_bitsLeft*bitrateWindow+(m_bitsLeft-getLCU(LCUIdx).m_targetBitsLeft)*noOfLCUsLeft)/(Double)bitrateWindow;
815  avgBits = Int( MAD*weightedBitsLeft/m_remainingCostIntra );
816  }
817  else
818  {
819  avgBits = Int( m_bitsLeft / m_LCULeft );
820  }
821  m_remainingCostIntra -= MAD;
822  }
823  else
824  {
825  Double totalWeight = 0;
826  for ( Int i=LCUIdx; i<m_numberOfLCU; i++ )
827  {
828  totalWeight += m_LCUs[i].m_bitWeight;
829  }
830  Int realInfluenceLCU = min( g_RCLCUSmoothWindowSize, getLCULeft() );
831  avgBits = (Int)( m_LCUs[LCUIdx].m_bitWeight - ( totalWeight - m_bitsLeft ) / realInfluenceLCU + 0.5 );
832  }
833 
834  if ( avgBits < 1 )
835  {
836  avgBits = 1;
837  }
838 
839  bpp = ( Double )avgBits/( Double )m_LCUs[ LCUIdx ].m_numberOfPixel;
840  m_LCUs[ LCUIdx ].m_targetBits = avgBits;
841 
842  return bpp;
843 }
844 
846 {
847  Int LCUIdx = getLCUCoded();
848  Double alpha;
849  Double beta;
850  if ( m_encRCSeq->getUseLCUSeparateModel() )
851  {
852  alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha;
853  beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta;
854  }
855  else
856  {
857  alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
858  beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
859  }
860 
861  Double estLambda = alpha * pow( bpp, beta );
862  //for Lambda clip, picture level clip
863  Double clipPicLambda = m_estPicLambda;
864 
865  //for Lambda clip, LCU level clip
866  Double clipNeighbourLambda = -1.0;
867  for ( Int i=LCUIdx - 1; i>=0; i-- )
868  {
869  if ( m_LCUs[i].m_lambda > 0 )
870  {
871  clipNeighbourLambda = m_LCUs[i].m_lambda;
872  break;
873  }
874  }
875 
876  if ( clipNeighbourLambda > 0.0 )
877  {
878  estLambda = Clip3( clipNeighbourLambda * pow( 2.0, -1.0/3.0 ), clipNeighbourLambda * pow( 2.0, 1.0/3.0 ), estLambda );
879  }
880 
881  if ( clipPicLambda > 0.0 )
882  {
883  estLambda = Clip3( clipPicLambda * pow( 2.0, -2.0/3.0 ), clipPicLambda * pow( 2.0, 2.0/3.0 ), estLambda );
884  }
885  else
886  {
887  estLambda = Clip3( 10.0, 1000.0, estLambda );
888  }
889 
890  if ( estLambda < 0.1 )
891  {
892  estLambda = 0.1;
893  }
894 
895  return estLambda;
896 }
897 
898 Int TEncRCPic::getLCUEstQP( Double lambda, Int clipPicQP )
899 {
900  Int LCUIdx = getLCUCoded();
901  Int estQP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 );
902 
903  //for Lambda clip, LCU level clip
904  Int clipNeighbourQP = g_RCInvalidQPValue;
905  for ( Int i=LCUIdx - 1; i>=0; i-- )
906  {
907  if ( (getLCU(i)).m_QP > g_RCInvalidQPValue )
908  {
909  clipNeighbourQP = getLCU(i).m_QP;
910  break;
911  }
912  }
913 
914  if ( clipNeighbourQP > g_RCInvalidQPValue )
915  {
916  estQP = Clip3( clipNeighbourQP - 1, clipNeighbourQP + 1, estQP );
917  }
918 
919  estQP = Clip3( clipPicQP - 2, clipPicQP + 2, estQP );
920 
921  return estQP;
922 }
923 
924 Void TEncRCPic::updateAfterCTU( Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter )
925 {
926  m_LCUs[LCUIdx].m_actualBits = bits;
927  m_LCUs[LCUIdx].m_QP = QP;
928  m_LCUs[LCUIdx].m_lambda = lambda;
929 
930  m_LCULeft--;
931  m_bitsLeft -= bits;
932  m_pixelsLeft -= m_LCUs[LCUIdx].m_numberOfPixel;
933 
934  if ( !updateLCUParameter )
935  {
936  return;
937  }
938 
939  if ( !m_encRCSeq->getUseLCUSeparateModel() )
940  {
941  return;
942  }
943 
944  Double alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha;
945  Double beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta;
946 
947  Int LCUActualBits = m_LCUs[LCUIdx].m_actualBits;
948  Int LCUTotalPixels = m_LCUs[LCUIdx].m_numberOfPixel;
949  Double bpp = ( Double )LCUActualBits/( Double )LCUTotalPixels;
950  Double calLambda = alpha * pow( bpp, beta );
951  Double inputLambda = m_LCUs[LCUIdx].m_lambda;
952 
953  if( inputLambda < 0.01 || calLambda < 0.01 || bpp < 0.0001 )
954  {
955  alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 );
956  beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 );
957 
958  alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
959  beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta );
960 
961  TRCParameter rcPara;
962  rcPara.m_alpha = alpha;
963  rcPara.m_beta = beta;
964  m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara );
965 
966  return;
967  }
968 
969  calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda );
970  alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha;
971  Double lnbpp = log( bpp );
972  lnbpp = Clip3( -5.0, -0.1, lnbpp );
973  beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp;
974 
975  alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
976  beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta );
977 
978  TRCParameter rcPara;
979  rcPara.m_alpha = alpha;
980  rcPara.m_beta = beta;
981  m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara );
982 
983 }
984 
986 {
987  Int totalQPs = 0;
988  Int numTotalLCUs = 0;
989 
990  Int i;
991  for ( i=0; i<m_numberOfLCU; i++ )
992  {
993  if ( m_LCUs[i].m_QP > 0 )
994  {
995  totalQPs += m_LCUs[i].m_QP;
996  numTotalLCUs++;
997  }
998  }
999 
1000  Double avgQP = 0.0;
1001  if ( numTotalLCUs == 0 )
1002  {
1003  avgQP = g_RCInvalidQPValue;
1004  }
1005  else
1006  {
1007  avgQP = ((Double)totalQPs) / ((Double)numTotalLCUs);
1008  }
1009  return avgQP;
1010 }
1011 
1013 {
1014  Double totalLambdas = 0.0;
1015  Int numTotalLCUs = 0;
1016 
1017  Int i;
1018  for ( i=0; i<m_numberOfLCU; i++ )
1019  {
1020  if ( m_LCUs[i].m_lambda > 0.01 )
1021  {
1022  totalLambdas += log( m_LCUs[i].m_lambda );
1023  numTotalLCUs++;
1024  }
1025  }
1026 
1027  Double avgLambda;
1028  if( numTotalLCUs == 0 )
1029  {
1030  avgLambda = -1.0;
1031  }
1032  else
1033  {
1034  avgLambda = pow( 2.7183, totalLambdas / numTotalLCUs );
1035  }
1036  return avgLambda;
1037 }
1038 
1039 
1040 Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType)
1041 {
1042  m_picActualHeaderBits = actualHeaderBits;
1043  m_picActualBits = actualTotalBits;
1044  if ( averageQP > 0.0 )
1045  {
1046  m_picQP = Int( averageQP + 0.5 );
1047  }
1048  else
1049  {
1050  m_picQP = g_RCInvalidQPValue;
1051  }
1052  m_picLambda = averageLambda;
1053 
1054  Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
1055  Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
1056 
1057  if (eSliceType == I_SLICE)
1058  {
1059  updateAlphaBetaIntra(&alpha, &beta);
1060  }
1061  else
1062  {
1063  // update parameters
1064  Double picActualBits = ( Double )m_picActualBits;
1065  Double picActualBpp = picActualBits/(Double)m_numberOfPixel;
1066  Double calLambda = alpha * pow( picActualBpp, beta );
1067  Double inputLambda = m_picLambda;
1068 
1069  if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 )
1070  {
1071  alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 );
1072  beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 );
1073 
1074  alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
1075  beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta );
1076 
1077  TRCParameter rcPara;
1078  rcPara.m_alpha = alpha;
1079  rcPara.m_beta = beta;
1080  m_encRCSeq->setPicPara( m_frameLevel, rcPara );
1081 
1082  return;
1083  }
1084 
1085  calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda );
1086  alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha;
1087  Double lnbpp = log( picActualBpp );
1088  lnbpp = Clip3( -5.0, -0.1, lnbpp );
1089 
1090  beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp;
1091 
1092  alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
1093  beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta );
1094  }
1095 
1096  TRCParameter rcPara;
1097  rcPara.m_alpha = alpha;
1098  rcPara.m_beta = beta;
1099 
1100  m_encRCSeq->setPicPara( m_frameLevel, rcPara );
1101 
1102  if ( m_frameLevel == 1 )
1103  {
1104  Double currLambda = Clip3( 0.1, 10000.0, m_picLambda );
1105  Double updateLastLambda = g_RCWeightHistoryLambda * m_encRCSeq->getLastLambda() + g_RCWeightCurrentLambda * currLambda;
1106  m_encRCSeq->setLastLambda( updateLastLambda );
1107  }
1108 }
1109 
1111 {
1112  Double alpha=0.25, beta=0.5582;
1113  Int iIntraBits;
1114 
1115  if (orgBits*40 < m_numberOfPixel)
1116  {
1117  alpha=0.25;
1118  }
1119  else
1120  {
1121  alpha=0.30;
1122  }
1123 
1124  iIntraBits = (Int)(alpha* pow(m_totalCostIntra*4.0/(Double)orgBits, beta)*(Double)orgBits+0.5);
1125 
1126  return iIntraBits;
1127 }
1128 
1129 Double TEncRCPic::calculateLambdaIntra(Double alpha, Double beta, Double MADPerPixel, Double bitsPerPixel)
1130 {
1131  return ( (alpha/256.0) * pow( MADPerPixel/bitsPerPixel, beta ) );
1132 }
1133 
1135 {
1136  Double lnbpp = log(pow(m_totalCostIntra / (Double)m_numberOfPixel, BETA1));
1137  Double diffLambda = (*beta)*(log((Double)m_picActualBits)-log((Double)m_targetBits));
1138 
1139  diffLambda = Clip3(-0.125, 0.125, 0.25*diffLambda);
1140  *alpha = (*alpha) * exp(diffLambda);
1141  *beta = (*beta) + diffLambda / lnbpp;
1142 }
1143 
1144 
1146 {
1147  Int iAvgBits = 0;
1148 
1149  m_remainingCostIntra = m_totalCostIntra;
1150  for (Int i=m_numberOfLCU-1; i>=0; i--)
1151  {
1152  iAvgBits += Int(m_targetBits * getLCU(i).m_costIntra/m_totalCostIntra);
1153  getLCU(i).m_targetBitsLeft = iAvgBits;
1154  }
1155 }
1156 
1157 
1159 {
1160  Int LCUIdx = getLCUCoded();
1161 
1162  Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
1163  Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
1164 
1165  Double costPerPixel = getLCU(LCUIdx).m_costIntra/(Double)getLCU(LCUIdx).m_numberOfPixel;
1166  costPerPixel = pow(costPerPixel, BETA1);
1167  Double estLambda = calculateLambdaIntra(alpha, beta, costPerPixel, bpp);
1168 
1169  Int clipNeighbourQP = g_RCInvalidQPValue;
1170  for (Int i=LCUIdx-1; i>=0; i--)
1171  {
1172  if ((getLCU(i)).m_QP > g_RCInvalidQPValue)
1173  {
1174  clipNeighbourQP = getLCU(i).m_QP;
1175  break;
1176  }
1177  }
1178 
1179  Int minQP = clipPicQP - 2;
1180  Int maxQP = clipPicQP + 2;
1181 
1182  if ( clipNeighbourQP > g_RCInvalidQPValue )
1183  {
1184  maxQP = min(clipNeighbourQP + 1, maxQP);
1185  minQP = max(clipNeighbourQP - 1, minQP);
1186  }
1187 
1188  Double maxLambda=exp(((Double)(maxQP+0.49)-13.7122)/4.2005);
1189  Double minLambda=exp(((Double)(minQP-0.49)-13.7122)/4.2005);
1190 
1191  estLambda = Clip3(minLambda, maxLambda, estLambda);
1192 
1193  *estQP = Int( 4.2005 * log(estLambda) + 13.7122 + 0.5 );
1194  *estQP = Clip3(minQP, maxQP, *estQP);
1195 
1196  return estLambda;
1197 }
1198 
1200 {
1201  m_encRCSeq = NULL;
1202  m_encRCGOP = NULL;
1203  m_encRCPic = NULL;
1204 }
1205 
1207 {
1208  destroy();
1209 }
1210 
1212 {
1213  if ( m_encRCSeq != NULL )
1214  {
1215  delete m_encRCSeq;
1216  m_encRCSeq = NULL;
1217  }
1218  if ( m_encRCGOP != NULL )
1219  {
1220  delete m_encRCGOP;
1221  m_encRCGOP = NULL;
1222  }
1223  while ( m_listRCPictures.size() > 0 )
1224  {
1225  TEncRCPic* p = m_listRCPictures.front();
1226  m_listRCPictures.pop_front();
1227  delete p;
1228  }
1229 }
1230 
1231 Void TEncRateCtrl::init( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int keepHierBits, Bool useLCUSeparateModel, GOPEntry GOPList[MAX_GOP] )
1232 {
1233  destroy();
1234 
1235  Bool isLowdelay = true;
1236  for ( Int i=0; i<GOPSize-1; i++ )
1237  {
1238  if ( GOPList[i].m_POC > GOPList[i+1].m_POC )
1239  {
1240  isLowdelay = false;
1241  break;
1242  }
1243  }
1244 
1245  Int numberOfLevel = 1;
1246  Int adaptiveBit = 0;
1247  if ( keepHierBits > 0 )
1248  {
1249  numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1;
1250  }
1251  if ( !isLowdelay && GOPSize == 8 )
1252  {
1253  numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1;
1254  }
1255  numberOfLevel++; // intra picture
1256  numberOfLevel++; // non-reference picture
1257 
1258 
1259  Int* bitsRatio;
1260  bitsRatio = new Int[ GOPSize ];
1261  for ( Int i=0; i<GOPSize; i++ )
1262  {
1263  bitsRatio[i] = 10;
1264  if ( !GOPList[i].m_refPic )
1265  {
1266  bitsRatio[i] = 2;
1267  }
1268  }
1269 
1270  if ( keepHierBits > 0 )
1271  {
1272  Double bpp = (Double)( targetBitrate / (Double)( frameRate*picWidth*picHeight ) );
1273  if ( GOPSize == 4 && isLowdelay )
1274  {
1275  if ( bpp > 0.2 )
1276  {
1277  bitsRatio[0] = 2;
1278  bitsRatio[1] = 3;
1279  bitsRatio[2] = 2;
1280  bitsRatio[3] = 6;
1281  }
1282  else if( bpp > 0.1 )
1283  {
1284  bitsRatio[0] = 2;
1285  bitsRatio[1] = 3;
1286  bitsRatio[2] = 2;
1287  bitsRatio[3] = 10;
1288  }
1289  else if ( bpp > 0.05 )
1290  {
1291  bitsRatio[0] = 2;
1292  bitsRatio[1] = 3;
1293  bitsRatio[2] = 2;
1294  bitsRatio[3] = 12;
1295  }
1296  else
1297  {
1298  bitsRatio[0] = 2;
1299  bitsRatio[1] = 3;
1300  bitsRatio[2] = 2;
1301  bitsRatio[3] = 14;
1302  }
1303 
1304  if ( keepHierBits == 2 )
1305  {
1306  adaptiveBit = 1;
1307  }
1308  }
1309  else if ( GOPSize == 8 && !isLowdelay )
1310  {
1311  if ( bpp > 0.2 )
1312  {
1313  bitsRatio[0] = 15;
1314  bitsRatio[1] = 5;
1315  bitsRatio[2] = 4;
1316  bitsRatio[3] = 1;
1317  bitsRatio[4] = 1;
1318  bitsRatio[5] = 4;
1319  bitsRatio[6] = 1;
1320  bitsRatio[7] = 1;
1321  }
1322  else if ( bpp > 0.1 )
1323  {
1324  bitsRatio[0] = 20;
1325  bitsRatio[1] = 6;
1326  bitsRatio[2] = 4;
1327  bitsRatio[3] = 1;
1328  bitsRatio[4] = 1;
1329  bitsRatio[5] = 4;
1330  bitsRatio[6] = 1;
1331  bitsRatio[7] = 1;
1332  }
1333  else if ( bpp > 0.05 )
1334  {
1335  bitsRatio[0] = 25;
1336  bitsRatio[1] = 7;
1337  bitsRatio[2] = 4;
1338  bitsRatio[3] = 1;
1339  bitsRatio[4] = 1;
1340  bitsRatio[5] = 4;
1341  bitsRatio[6] = 1;
1342  bitsRatio[7] = 1;
1343  }
1344  else
1345  {
1346  bitsRatio[0] = 30;
1347  bitsRatio[1] = 8;
1348  bitsRatio[2] = 4;
1349  bitsRatio[3] = 1;
1350  bitsRatio[4] = 1;
1351  bitsRatio[5] = 4;
1352  bitsRatio[6] = 1;
1353  bitsRatio[7] = 1;
1354  }
1355 
1356  if ( keepHierBits == 2 )
1357  {
1358  adaptiveBit = 2;
1359  }
1360  }
1361  else
1362  {
1363  printf( "\n hierarchical bit allocation is not support for the specified coding structure currently.\n" );
1364  }
1365  }
1366 
1367  Int* GOPID2Level = new Int[ GOPSize ];
1368  for ( Int i=0; i<GOPSize; i++ )
1369  {
1370  GOPID2Level[i] = 1;
1371  if ( !GOPList[i].m_refPic )
1372  {
1373  GOPID2Level[i] = 2;
1374  }
1375  }
1376 
1377  if ( keepHierBits > 0 )
1378  {
1379  if ( GOPSize == 4 && isLowdelay )
1380  {
1381  GOPID2Level[0] = 3;
1382  GOPID2Level[1] = 2;
1383  GOPID2Level[2] = 3;
1384  GOPID2Level[3] = 1;
1385  }
1386  else if ( GOPSize == 8 && !isLowdelay )
1387  {
1388  GOPID2Level[0] = 1;
1389  GOPID2Level[1] = 2;
1390  GOPID2Level[2] = 3;
1391  GOPID2Level[3] = 4;
1392  GOPID2Level[4] = 4;
1393  GOPID2Level[5] = 3;
1394  GOPID2Level[6] = 4;
1395  GOPID2Level[7] = 4;
1396  }
1397  }
1398 
1399  if ( !isLowdelay && GOPSize == 8 )
1400  {
1401  GOPID2Level[0] = 1;
1402  GOPID2Level[1] = 2;
1403  GOPID2Level[2] = 3;
1404  GOPID2Level[3] = 4;
1405  GOPID2Level[4] = 4;
1406  GOPID2Level[5] = 3;
1407  GOPID2Level[6] = 4;
1408  GOPID2Level[7] = 4;
1409  }
1410 
1411  m_encRCSeq = new TEncRCSeq;
1412  m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel, adaptiveBit );
1413  m_encRCSeq->initBitsRatio( bitsRatio );
1414  m_encRCSeq->initGOPID2Level( GOPID2Level );
1415  m_encRCSeq->initPicPara();
1416  if ( useLCUSeparateModel )
1417  {
1418  m_encRCSeq->initLCUPara();
1419  }
1420  m_CpbSaturationEnabled = false;
1421  m_cpbSize = targetBitrate;
1422  m_cpbState = (UInt)(m_cpbSize*0.5f);
1423  m_bufferingRate = (Int)(targetBitrate / frameRate);
1424 
1425  delete[] bitsRatio;
1426  delete[] GOPID2Level;
1427 }
1428 
1430 {
1431  m_encRCPic = new TEncRCPic;
1432  m_encRCPic->create( m_encRCSeq, m_encRCGOP, frameLevel, m_listRCPictures );
1433 }
1434 
1435 Void TEncRateCtrl::initRCGOP( Int numberOfPictures )
1436 {
1437  m_encRCGOP = new TEncRCGOP;
1438  m_encRCGOP->create( m_encRCSeq, numberOfPictures );
1439 }
1440 
1442 {
1443  Int cpbState = 1;
1444 
1445  m_cpbState -= actualBits;
1446  if (m_cpbState < 0)
1447  {
1448  cpbState = -1;
1449  }
1450 
1451  m_cpbState += m_bufferingRate;
1452  if (m_cpbState > m_cpbSize)
1453  {
1454  cpbState = 0;
1455  }
1456 
1457  return cpbState;
1458 }
1459 
1460 Void TEncRateCtrl::initHrdParam(const TComHRD* pcHrd, Int iFrameRate, Double fInitialCpbFullness)
1461 {
1462  m_CpbSaturationEnabled = true;
1463  m_cpbSize = (pcHrd->getCpbSizeValueMinus1(0, 0, 0) + 1) << (4 + pcHrd->getCpbSizeScale());
1464  m_cpbState = (UInt)(m_cpbSize*fInitialCpbFullness);
1465  m_bufferingRate = (UInt)(((pcHrd->getBitRateValueMinus1(0, 0, 0) + 1) << (6 + pcHrd->getBitRateScale())) / iFrameRate);
1466  printf("\nHRD - [Initial CPB state %6d] [CPB Size %6d] [Buffering Rate %6d]\n", m_cpbState, m_cpbSize, m_bufferingRate);
1467 }
1468 
1470 {
1471  delete m_encRCGOP;
1472  m_encRCGOP = NULL;
1473 }
const Double g_RCBetaMaxValue
Definition: TEncRateCtrl.h:73
SliceType
supported slice type
Definition: TypeDef.h:283
Double m_beta
Definition: TEncRateCtrl.h:94
Int getGOPSize()
Definition: TEncRateCtrl.h:117
UInt getCpbSizeValueMinus1(Int layer, Int cpbcnt, Int nalOrVcl) const
Definition: TComSlice.h:380
Void initLCUPara(TRCParameter **LCUPara=0)
void Void
Definition: TypeDef.h:203
Int getPicLeft()
Definition: TEncRateCtrl.h:204
Void destroy()
#define NULL
Definition: CommonDef.h:107
Int getLCUHeight()
Definition: TEncRateCtrl.h:121
Void getLCUInitTargetBits()
const Double g_RCAlphaMaxValue
Definition: TEncRateCtrl.h:71
const Double g_RCAlphaMinValue
Definition: TEncRateCtrl.h:70
unsigned int UInt
Definition: TypeDef.h:212
Double calculateLambdaIntra(Double alpha, Double beta, Double MADPerPixel, Double bitsPerPixel)
#define BETA2
Definition: TEncRateCtrl.h:77
Int getPicWidth()
Definition: TEncRateCtrl.h:118
Int * getBitRatio()
Definition: TEncRateCtrl.h:130
#define ALPHA
Definition: TEncRateCtrl.h:75
Int xEstGOPTargetBits(TEncRCSeq *encRCSeq, Int GOPSize)
Int updateCpbState(Int actualBits)
Int64 getBitsLeft()
Definition: TEncRateCtrl.h:143
Definition: TEncCfg.h:49
Double calAverageQP()
const Int g_RCInvalidQPValue
Definition: TEncRateCtrl.h:61
Int estimatePicQP(Double lambda, list< TEncRCPic * > &listPreviousPictures)
Double estimatePicLambda(list< TEncRCPic * > &listPreviousPictures, SliceType eSliceType)
#define BETA1
Definition: TEncRateCtrl.h:76
Int getTargetBits()
Definition: TEncRateCtrl.h:203
Int getNumberOfLCU()
Definition: TEncRateCtrl.h:129
Double xSolveEqua(Double targetBpp, Double *equaCoeffA, Double *equaCoeffB, Int GOPSize)
Int getFramesLeft()
Definition: TEncRateCtrl.h:142
const Double g_RCWeightPicTargetBitInGOP
Definition: TEncRateCtrl.h:64
Int getNumPic()
Definition: TEncRateCtrl.h:202
Void create(TEncRCSeq *encRCSeq, Int numPic)
Int * getGOPID2Level()
Definition: TEncRateCtrl.h:132
static const Int MAX_GOP
max. value of hierarchical GOP size
Definition: CommonDef.h:123
Int getBitsLeft()
Definition: TEncRateCtrl.h:205
const Int g_RCMaxPicListSize
Definition: TEncRateCtrl.h:63
TRCParameter * getPicPara()
Definition: TEncRateCtrl.h:134
Void create(Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int numberOfLevel, Bool useLCUSeparateModel, Int adaptiveBit)
bool Bool
Definition: TypeDef.h:204
long long Int64
Definition: TypeDef.h:232
const Int g_RCIterationNum
Definition: TEncRateCtrl.h:66
Void initRCGOP(Int numberOfPictures)
Void updateAfterPicture(Int bitsCost)
Int getRefineBitsForIntra(Int orgBits)
Int getNumPixel()
Definition: TEncRateCtrl.h:127
UInt getCpbSizeScale() const
Definition: TComSlice.h:348
Int getPicHeight()
Definition: TEncRateCtrl.h:119
T Clip3(const T minVal, const T maxVal, const T a)
general min/max clip
Definition: CommonDef.h:252
Int xEstPicTargetBits(TEncRCSeq *encRCSeq, TEncRCGOP *encRCGOP)
Double getLCUTargetBpp(SliceType eSliceType)
Int getLCUWidth()
Definition: TEncRateCtrl.h:120
const Double g_RCWeightPicRargetBitInBuffer
Definition: TEncRateCtrl.h:65
Void initGOPID2Level(Int GOPID2Level[])
Void initHrdParam(const TComHRD *pcHrd, Int iFrameRate, Double fInitialCpbFullness)
const Int g_RCSmoothWindowSize
Definition: TEncRateCtrl.h:62
Double getLastLambda()
Definition: TEncRateCtrl.h:150
Int getLCUEstQP(Double lambda, Int clipPicQP)
Void updateAfterCTU(Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter=true)
Double calAverageLambda()
Void updateAfterPicture(Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType)
Void addToPictureLsit(list< TEncRCPic * > &listPreviousPictures)
const Double g_RCWeightHistoryLambda
Definition: TEncRateCtrl.h:67
Double getLCUEstLambdaAndQP(Double bpp, Int clipPicQP, Int *estQP)
const Double g_RCWeightCurrentLambda
Definition: TEncRateCtrl.h:68
Int xEstPicLowerBound(TEncRCSeq *encRCSeq, TEncRCGOP *encRCGOP)
Void create(TEncRCSeq *encRCSeq, TEncRCGOP *encRCGOP, Int frameLevel, list< TEncRCPic * > &listPreviousPictures)
Void initRCPic(Int frameLevel)
Int m_actualBits
Definition: TEncRateCtrl.h:81
UInt getBitRateValueMinus1(Int layer, Int cpbcnt, Int nalOrVcl) const
Definition: TComSlice.h:377
Int64 getTargetBits()
Definition: TEncRateCtrl.h:128
int Int
Definition: TypeDef.h:211
Int getAdaptiveBits()
Definition: TEncRateCtrl.h:149
Void initBitsRatio(Int bitsRatio[])
Rate control manager class.
double Double
Definition: TypeDef.h:213
Void updateAlphaBetaIntra(Double *alpha, Double *beta)
Void setAllBitRatio(Double basicLambda, Double *equaCoeffA, Double *equaCoeffB)
const Int g_RCLCUSmoothWindowSize
Definition: TEncRateCtrl.h:69
Void destroy()
Int xEstPicHeaderBits(list< TEncRCPic * > &listPreviousPictures, Int frameLevel)
Void initPicPara(TRCParameter *picPara=0)
Double m_alpha
Definition: TEncRateCtrl.h:93
Void destroy()
Int getTotalFrames()
Definition: TEncRateCtrl.h:114
const Double g_RCBetaMinValue
Definition: TEncRateCtrl.h:72
Void updateAfterPic(Int bits)
Void init(Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int keepHierBits, Bool useLCUSeparateModel, GOPEntry GOPList[MAX_GOP])
Double getLCUEstLambda(Double bpp)
UInt getBitRateScale() const
Definition: TComSlice.h:345
Void xCalEquaCoeff(TEncRCSeq *encRCSeq, Double *lambdaRatio, Double *equaCoeffA, Double *equaCoeffB, Int GOPSize)