source: 3DVCSoftware/branches/HTM-13.0-MV-draft-2/source/Lib/TLibEncoder/TEncRateCtrl.cpp @ 1129

Last change on this file since 1129 was 1128, checked in by tech, 10 years ago

Removed 3D-HEVC related code.

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