source: 3DVCSoftware/branches/HTM-15.1-MV-draft-4/source/Lib/TLibEncoder/TEncRateCtrl.cpp @ 1389

Last change on this file since 1389 was 1325, checked in by tech, 10 years ago

Removed 3D-HEVC.

File size: 36.7 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-2015, 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#include "../TLibCommon/TComChromaFormat.h"
40
41#include <cmath>
42
43using namespace std;
44
45//sequence level
46TEncRCSeq::TEncRCSeq()
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
72TEncRCSeq::~TEncRCSeq()
73{
74  destroy();
75}
76
77Void 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
164Void TEncRCSeq::destroy()
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
195Void TEncRCSeq::initBitsRatio( Int bitsRatio[])
196{
197  for (Int i=0; i<m_GOPSize; i++)
198  {
199    m_bitsRatio[i] = bitsRatio[i];
200  }
201}
202
203Void TEncRCSeq::initGOPID2Level( Int GOPID2Level[] )
204{
205  for ( Int i=0; i<m_GOPSize; i++ )
206  {
207    m_GOPID2Level[i] = GOPID2Level[i];
208  }
209}
210
211Void TEncRCSeq::initPicPara( TRCParameter* picPara )
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
240Void TEncRCSeq::initLCUPara( TRCParameter** LCUPara )
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
269Void TEncRCSeq::updateAfterPic ( Int bits )
270{
271  m_bitsLeft -= bits;
272  m_framesLeft--;
273}
274
275Void 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
287TEncRCGOP::TEncRCGOP()
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
297TEncRCGOP::~TEncRCGOP()
298{
299  destroy();
300}
301
302Void 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
388Void 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
400Double 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
434Void TEncRCGOP::destroy()
435{
436  m_encRCSeq = NULL;
437  if ( m_picTargetBitInGOP != NULL )
438  {
439    delete[] m_picTargetBitInGOP;
440    m_picTargetBitInGOP = NULL;
441  }
442}
443
444Void TEncRCGOP::updateAfterPicture( Int bitsCost )
445{
446  m_bitsLeft -= bitsCost;
447  m_picLeft--;
448}
449
450Int TEncRCGOP::xEstGOPTargetBits( TEncRCSeq* encRCSeq, Int GOPSize )
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
466TEncRCPic::TEncRCPic()
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
485  m_picActualHeaderBits = 0;
486  m_picActualBits       = 0;
487  m_picQP               = 0;
488  m_picLambda           = 0.0;
489
490}
491
492TEncRCPic::~TEncRCPic()
493{
494  destroy();
495}
496
497Int TEncRCPic::xEstPicTargetBits( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP )
498{
499  Int targetBits        = 0;
500  Int GOPbitsLeft       = encRCGOP->getBitsLeft();
501
502  Int i;
503  Int currPicPosition = encRCGOP->getNumPic()-encRCGOP->getPicLeft();
504  Int currPicRatio    = encRCSeq->getBitRatio( currPicPosition );
505  Int totalPicRatio   = 0;
506  for ( i=currPicPosition; i<encRCGOP->getNumPic(); i++ )
507  {
508    totalPicRatio += encRCSeq->getBitRatio( i );
509  }
510
511  targetBits  = Int( ((Double)GOPbitsLeft) * currPicRatio / totalPicRatio );
512
513  if ( targetBits < 100 )
514  {
515    targetBits = 100;   // at least allocate 100 bits for one picture
516  }
517
518  if ( m_encRCSeq->getFramesLeft() > 16 )
519  {
520    targetBits = Int( g_RCWeightPicRargetBitInBuffer * targetBits + g_RCWeightPicTargetBitInGOP * m_encRCGOP->getTargetBitInGOP( currPicPosition ) );
521  }
522
523  return targetBits;
524}
525
526Int TEncRCPic::xEstPicHeaderBits( list<TEncRCPic*>& listPreviousPictures, Int frameLevel )
527{
528  Int numPreviousPics   = 0;
529  Int totalPreviousBits = 0;
530
531  list<TEncRCPic*>::iterator it;
532  for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ )
533  {
534    if ( (*it)->getFrameLevel() == frameLevel )
535    {
536      totalPreviousBits += (*it)->getPicActualHeaderBits();
537      numPreviousPics++;
538    }
539  }
540
541  Int estHeaderBits = 0;
542  if ( numPreviousPics > 0 )
543  {
544    estHeaderBits = totalPreviousBits / numPreviousPics;
545  }
546
547  return estHeaderBits;
548}
549
550Void TEncRCPic::addToPictureLsit( list<TEncRCPic*>& listPreviousPictures )
551{
552  if ( listPreviousPictures.size() > g_RCMaxPicListSize )
553  {
554    TEncRCPic* p = listPreviousPictures.front();
555    listPreviousPictures.pop_front();
556    p->destroy();
557    delete p;
558  }
559
560  listPreviousPictures.push_back( this );
561}
562
563
564Void TEncRCPic::create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures )
565{
566  destroy();
567  m_encRCSeq = encRCSeq;
568  m_encRCGOP = encRCGOP;
569
570  Int targetBits    = xEstPicTargetBits( encRCSeq, encRCGOP );
571  Int estHeaderBits = xEstPicHeaderBits( listPreviousPictures, frameLevel );
572
573  if ( targetBits < estHeaderBits + 100 )
574  {
575    targetBits = estHeaderBits + 100;   // at least allocate 100 bits for picture data
576  }
577
578  m_frameLevel       = frameLevel;
579  m_numberOfPixel    = encRCSeq->getNumPixel();
580  m_numberOfLCU      = encRCSeq->getNumberOfLCU();
581  m_estPicLambda     = 100.0;
582  m_targetBits       = targetBits;
583  m_estHeaderBits    = estHeaderBits;
584  m_bitsLeft         = m_targetBits;
585  Int picWidth       = encRCSeq->getPicWidth();
586  Int picHeight      = encRCSeq->getPicHeight();
587  Int LCUWidth       = encRCSeq->getLCUWidth();
588  Int LCUHeight      = encRCSeq->getLCUHeight();
589  Int picWidthInLCU  = ( picWidth  % LCUWidth  ) == 0 ? picWidth  / LCUWidth  : picWidth  / LCUWidth  + 1;
590  Int picHeightInLCU = ( picHeight % LCUHeight ) == 0 ? picHeight / LCUHeight : picHeight / LCUHeight + 1;
591
592  m_LCULeft         = m_numberOfLCU;
593  m_bitsLeft       -= m_estHeaderBits;
594  m_pixelsLeft      = m_numberOfPixel;
595
596  m_LCUs           = new TRCLCU[m_numberOfLCU];
597  Int i, j;
598  Int LCUIdx;
599  for ( i=0; i<picWidthInLCU; i++ )
600  {
601    for ( j=0; j<picHeightInLCU; j++ )
602    {
603      LCUIdx = j*picWidthInLCU + i;
604      m_LCUs[LCUIdx].m_actualBits = 0;
605      m_LCUs[LCUIdx].m_QP         = 0;
606      m_LCUs[LCUIdx].m_lambda     = 0.0;
607      m_LCUs[LCUIdx].m_targetBits = 0;
608      m_LCUs[LCUIdx].m_bitWeight  = 1.0;
609      Int currWidth  = ( (i == picWidthInLCU -1) ? picWidth  - LCUWidth *(picWidthInLCU -1) : LCUWidth  );
610      Int currHeight = ( (j == picHeightInLCU-1) ? picHeight - LCUHeight*(picHeightInLCU-1) : LCUHeight );
611      m_LCUs[LCUIdx].m_numberOfPixel = currWidth * currHeight;
612
613    }
614  }
615  m_picActualHeaderBits = 0;
616  m_picActualBits       = 0;
617  m_picQP               = 0;
618  m_picLambda           = 0.0;
619
620
621
622
623}
624
625Void TEncRCPic::destroy()
626{
627  if( m_LCUs != NULL )
628  {
629    delete[] m_LCUs;
630    m_LCUs = NULL;
631  }
632  m_encRCSeq = NULL;
633  m_encRCGOP = NULL;
634}
635
636
637Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures, SliceType eSliceType)
638{
639  Double alpha         = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
640  Double beta          = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
641  Double bpp       = (Double)m_targetBits/(Double)m_numberOfPixel;
642  Double estLambda;
643  if (eSliceType == I_SLICE)
644  {
645    estLambda = calculateLambdaIntra(alpha, beta, pow(m_totalCostIntra/(Double)m_numberOfPixel, BETA1), bpp);
646  }
647  else
648  {
649    estLambda = alpha * pow( bpp, beta );
650  }
651
652  Double lastLevelLambda = -1.0;
653  Double lastPicLambda   = -1.0;
654  Double lastValidLambda = -1.0;
655  list<TEncRCPic*>::iterator it;
656  for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ )
657  {
658    if ( (*it)->getFrameLevel() == m_frameLevel )
659    {
660      lastLevelLambda = (*it)->getPicActualLambda();
661    }
662    lastPicLambda     = (*it)->getPicActualLambda();
663
664    if ( lastPicLambda > 0.0 )
665    {
666      lastValidLambda = lastPicLambda;
667    }
668  }
669
670  if ( lastLevelLambda > 0.0 )
671  {
672    lastLevelLambda = Clip3( 0.1, 10000.0, lastLevelLambda );
673    estLambda = Clip3( lastLevelLambda * pow( 2.0, -3.0/3.0 ), lastLevelLambda * pow( 2.0, 3.0/3.0 ), estLambda );
674  }
675
676  if ( lastPicLambda > 0.0 )
677  {
678    lastPicLambda = Clip3( 0.1, 2000.0, lastPicLambda );
679    estLambda = Clip3( lastPicLambda * pow( 2.0, -10.0/3.0 ), lastPicLambda * pow( 2.0, 10.0/3.0 ), estLambda );
680  }
681  else if ( lastValidLambda > 0.0 )
682  {
683    lastValidLambda = Clip3( 0.1, 2000.0, lastValidLambda );
684    estLambda = Clip3( lastValidLambda * pow(2.0, -10.0/3.0), lastValidLambda * pow(2.0, 10.0/3.0), estLambda );
685  }
686  else
687  {
688    estLambda = Clip3( 0.1, 10000.0, estLambda );
689  }
690
691  if ( estLambda < 0.1 )
692  {
693    estLambda = 0.1;
694  }
695
696  m_estPicLambda = estLambda;
697
698  Double totalWeight = 0.0;
699  // initial BU bit allocation weight
700  for ( Int i=0; i<m_numberOfLCU; i++ )
701  {
702    Double alphaLCU, betaLCU;
703    if ( m_encRCSeq->getUseLCUSeparateModel() )
704    {
705      alphaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_alpha;
706      betaLCU  = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_beta;
707    }
708    else
709    {
710      alphaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
711      betaLCU  = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
712    }
713
714    m_LCUs[i].m_bitWeight =  m_LCUs[i].m_numberOfPixel * pow( estLambda/alphaLCU, 1.0/betaLCU );
715
716    if ( m_LCUs[i].m_bitWeight < 0.01 )
717    {
718      m_LCUs[i].m_bitWeight = 0.01;
719    }
720    totalWeight += m_LCUs[i].m_bitWeight;
721  }
722  for ( Int i=0; i<m_numberOfLCU; i++ )
723  {
724    Double BUTargetBits = m_targetBits * m_LCUs[i].m_bitWeight / totalWeight;
725    m_LCUs[i].m_bitWeight = BUTargetBits;
726  }
727
728  return estLambda;
729}
730
731Int TEncRCPic::estimatePicQP( Double lambda, list<TEncRCPic*>& listPreviousPictures )
732{
733  Int QP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 );
734
735  Int lastLevelQP = g_RCInvalidQPValue;
736  Int lastPicQP   = g_RCInvalidQPValue;
737  Int lastValidQP = g_RCInvalidQPValue;
738  list<TEncRCPic*>::iterator it;
739  for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ )
740  {
741    if ( (*it)->getFrameLevel() == m_frameLevel )
742    {
743      lastLevelQP = (*it)->getPicActualQP();
744    }
745    lastPicQP = (*it)->getPicActualQP();
746    if ( lastPicQP > g_RCInvalidQPValue )
747    {
748      lastValidQP = lastPicQP;
749    }
750  }
751
752  if ( lastLevelQP > g_RCInvalidQPValue )
753  {
754    QP = Clip3( lastLevelQP - 3, lastLevelQP + 3, QP );
755  }
756
757  if( lastPicQP > g_RCInvalidQPValue )
758  {
759    QP = Clip3( lastPicQP - 10, lastPicQP + 10, QP );
760  }
761  else if( lastValidQP > g_RCInvalidQPValue )
762  {
763    QP = Clip3( lastValidQP - 10, lastValidQP + 10, QP );
764  }
765
766  return QP;
767}
768
769
770
771
772Double TEncRCPic::getLCUTargetBpp(SliceType eSliceType)
773{
774  Int   LCUIdx    = getLCUCoded();
775  Double bpp      = -1.0;
776  Int avgBits     = 0;
777
778  if (eSliceType == I_SLICE)
779  {
780    Int noOfLCUsLeft = m_numberOfLCU - LCUIdx + 1;
781    Int bitrateWindow = min(4,noOfLCUsLeft);
782    Double MAD      = getLCU(LCUIdx).m_costIntra;
783
784    if (m_remainingCostIntra > 0.1 )
785    {
786      Double weightedBitsLeft = (m_bitsLeft*bitrateWindow+(m_bitsLeft-getLCU(LCUIdx).m_targetBitsLeft)*noOfLCUsLeft)/(Double)bitrateWindow;
787      avgBits = Int( MAD*weightedBitsLeft/m_remainingCostIntra );
788    }
789    else
790    {
791      avgBits = Int( m_bitsLeft / m_LCULeft );
792    }
793    m_remainingCostIntra -= MAD;
794  }
795  else
796  {
797    Double totalWeight = 0;
798    for ( Int i=LCUIdx; i<m_numberOfLCU; i++ )
799    {
800      totalWeight += m_LCUs[i].m_bitWeight;
801    }
802    Int realInfluenceLCU = min( g_RCLCUSmoothWindowSize, getLCULeft() );
803    avgBits = (Int)( m_LCUs[LCUIdx].m_bitWeight - ( totalWeight - m_bitsLeft ) / realInfluenceLCU + 0.5 );
804  }
805
806  if ( avgBits < 1 )
807  {
808    avgBits = 1;
809  }
810
811  bpp = ( Double )avgBits/( Double )m_LCUs[ LCUIdx ].m_numberOfPixel;
812  m_LCUs[ LCUIdx ].m_targetBits = avgBits;
813
814  return bpp;
815}
816
817
818
819
820
821
822Double TEncRCPic::getLCUEstLambda( Double bpp )
823{
824  Int   LCUIdx = getLCUCoded();
825  Double alpha;
826  Double beta;
827  if ( m_encRCSeq->getUseLCUSeparateModel() )
828  {
829    alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha;
830    beta  = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta;
831  }
832  else
833  {
834    alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
835    beta  = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
836  }
837
838  Double estLambda = alpha * pow( bpp, beta );
839  //for Lambda clip, picture level clip
840  Double clipPicLambda = m_estPicLambda;
841
842  //for Lambda clip, LCU level clip
843  Double clipNeighbourLambda = -1.0;
844  for ( Int i=LCUIdx - 1; i>=0; i-- )
845  {
846    if ( m_LCUs[i].m_lambda > 0 )
847    {
848      clipNeighbourLambda = m_LCUs[i].m_lambda;
849      break;
850    }
851  }
852
853  if ( clipNeighbourLambda > 0.0 )
854  {
855    estLambda = Clip3( clipNeighbourLambda * pow( 2.0, -1.0/3.0 ), clipNeighbourLambda * pow( 2.0, 1.0/3.0 ), estLambda );
856  }
857
858  if ( clipPicLambda > 0.0 )
859  {
860    estLambda = Clip3( clipPicLambda * pow( 2.0, -2.0/3.0 ), clipPicLambda * pow( 2.0, 2.0/3.0 ), estLambda );
861  }
862  else
863  {
864    estLambda = Clip3( 10.0, 1000.0, estLambda );
865  }
866
867  if ( estLambda < 0.1 )
868  {
869    estLambda = 0.1;
870  }
871
872  return estLambda;
873}
874
875Int TEncRCPic::getLCUEstQP( Double lambda, Int clipPicQP )
876{
877  Int LCUIdx = getLCUCoded();
878  Int estQP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 );
879
880  //for Lambda clip, LCU level clip
881  Int clipNeighbourQP = g_RCInvalidQPValue;
882  for ( Int i=LCUIdx - 1; i>=0; i-- )
883  {
884    if ( (getLCU(i)).m_QP > g_RCInvalidQPValue )
885    {
886      clipNeighbourQP = getLCU(i).m_QP;
887      break;
888    }
889  }
890
891  if ( clipNeighbourQP > g_RCInvalidQPValue )
892  {
893    estQP = Clip3( clipNeighbourQP - 1, clipNeighbourQP + 1, estQP );
894  }
895
896  estQP = Clip3( clipPicQP - 2, clipPicQP + 2, estQP );
897
898  return estQP;
899}
900
901Void TEncRCPic::updateAfterCTU( Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter )
902{
903  m_LCUs[LCUIdx].m_actualBits = bits;
904  m_LCUs[LCUIdx].m_QP         = QP;
905  m_LCUs[LCUIdx].m_lambda     = lambda;
906
907  m_LCULeft--;
908  m_bitsLeft   -= bits;
909  m_pixelsLeft -= m_LCUs[LCUIdx].m_numberOfPixel;
910
911  if ( !updateLCUParameter )
912  {
913    return;
914  }
915
916  if ( !m_encRCSeq->getUseLCUSeparateModel() )
917  {
918    return;
919  }
920
921  Double alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha;
922  Double beta  = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta;
923
924  Int LCUActualBits   = m_LCUs[LCUIdx].m_actualBits;
925  Int LCUTotalPixels  = m_LCUs[LCUIdx].m_numberOfPixel;
926  Double bpp         = ( Double )LCUActualBits/( Double )LCUTotalPixels;
927  Double calLambda   = alpha * pow( bpp, beta );
928  Double inputLambda = m_LCUs[LCUIdx].m_lambda;
929
930  if( inputLambda < 0.01 || calLambda < 0.01 || bpp < 0.0001 )
931  {
932    alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 );
933    beta  *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 );
934
935    alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
936    beta  = Clip3( g_RCBetaMinValue,  g_RCBetaMaxValue,  beta  );
937
938    TRCParameter rcPara;
939    rcPara.m_alpha = alpha;
940    rcPara.m_beta  = beta;
941    m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara );
942
943    return;
944  }
945
946  calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda );
947  alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha;
948  Double lnbpp = log( bpp );
949  lnbpp = Clip3( -5.0, -0.1, lnbpp );
950  beta  += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp;
951
952  alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
953  beta  = Clip3( g_RCBetaMinValue,  g_RCBetaMaxValue,  beta  );
954
955  TRCParameter rcPara;
956  rcPara.m_alpha = alpha;
957  rcPara.m_beta  = beta;
958  m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara );
959
960}
961
962Double TEncRCPic::calAverageQP()
963{
964  Int totalQPs = 0;
965  Int numTotalLCUs = 0;
966
967  Int i;
968  for ( i=0; i<m_numberOfLCU; i++ )
969  {
970    if ( m_LCUs[i].m_QP > 0 )
971    {
972      totalQPs += m_LCUs[i].m_QP;
973      numTotalLCUs++;
974    }
975  }
976
977  Double avgQP = 0.0;
978  if ( numTotalLCUs == 0 )
979  {
980    avgQP = g_RCInvalidQPValue;
981  }
982  else
983  {
984    avgQP = ((Double)totalQPs) / ((Double)numTotalLCUs);
985  }
986  return avgQP;
987}
988
989Double TEncRCPic::calAverageLambda()
990{
991  Double totalLambdas = 0.0;
992  Int numTotalLCUs = 0;
993
994  Int i;
995  for ( i=0; i<m_numberOfLCU; i++ )
996  {
997    if ( m_LCUs[i].m_lambda > 0.01 )
998    {
999      totalLambdas += log( m_LCUs[i].m_lambda );
1000      numTotalLCUs++;
1001    }
1002  }
1003
1004  Double avgLambda;
1005  if( numTotalLCUs == 0 )
1006  {
1007    avgLambda = -1.0;
1008  }
1009  else
1010  {
1011    avgLambda = pow( 2.7183, totalLambdas / numTotalLCUs );
1012  }
1013  return avgLambda;
1014}
1015
1016
1017Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType)
1018{
1019  m_picActualHeaderBits = actualHeaderBits;
1020  m_picActualBits       = actualTotalBits;
1021  if ( averageQP > 0.0 )
1022  {
1023    m_picQP             = Int( averageQP + 0.5 );
1024  }
1025  else
1026  {
1027    m_picQP             = g_RCInvalidQPValue;
1028  }
1029  m_picLambda           = averageLambda;
1030
1031  Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
1032  Double beta  = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
1033
1034  if (eSliceType == I_SLICE)
1035  {
1036    updateAlphaBetaIntra(&alpha, &beta);
1037  }
1038  else
1039  {
1040    // update parameters
1041    Double picActualBits = ( Double )m_picActualBits;
1042    Double picActualBpp  = picActualBits/(Double)m_numberOfPixel;
1043    Double calLambda     = alpha * pow( picActualBpp, beta );
1044    Double inputLambda   = m_picLambda;
1045
1046    if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 )
1047    {
1048      alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 );
1049      beta  *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 );
1050
1051      alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
1052      beta  = Clip3( g_RCBetaMinValue,  g_RCBetaMaxValue,  beta  );
1053
1054      TRCParameter rcPara;
1055      rcPara.m_alpha = alpha;
1056      rcPara.m_beta  = beta;
1057      m_encRCSeq->setPicPara( m_frameLevel, rcPara );
1058
1059      return;
1060    }
1061
1062    calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda );
1063    alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha;
1064    Double lnbpp = log( picActualBpp );
1065    lnbpp = Clip3( -5.0, -0.1, lnbpp );
1066
1067    beta  += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp;
1068
1069    alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
1070    beta  = Clip3( g_RCBetaMinValue,  g_RCBetaMaxValue,  beta  );
1071  }
1072
1073  TRCParameter rcPara;
1074  rcPara.m_alpha = alpha;
1075  rcPara.m_beta  = beta;
1076
1077  m_encRCSeq->setPicPara( m_frameLevel, rcPara );
1078
1079  if ( m_frameLevel == 1 )
1080  {
1081    Double currLambda = Clip3( 0.1, 10000.0, m_picLambda );
1082    Double updateLastLambda = g_RCWeightHistoryLambda * m_encRCSeq->getLastLambda() + g_RCWeightCurrentLambda * currLambda;
1083    m_encRCSeq->setLastLambda( updateLastLambda );
1084  }
1085}
1086
1087Int TEncRCPic::getRefineBitsForIntra( Int orgBits )
1088{
1089  Double alpha=0.25, beta=0.5582;
1090  Int iIntraBits;
1091
1092  if (orgBits*40 < m_numberOfPixel)
1093  {
1094    alpha=0.25;
1095  }
1096  else
1097  {
1098    alpha=0.30;
1099  }
1100
1101  iIntraBits = (Int)(alpha* pow(m_totalCostIntra*4.0/(Double)orgBits, beta)*(Double)orgBits+0.5);
1102
1103  return iIntraBits;
1104}
1105
1106Double TEncRCPic::calculateLambdaIntra(Double alpha, Double beta, Double MADPerPixel, Double bitsPerPixel)
1107{
1108  return ( (alpha/256.0) * pow( MADPerPixel/bitsPerPixel, beta ) );
1109}
1110
1111Void TEncRCPic::updateAlphaBetaIntra(Double *alpha, Double *beta)
1112{
1113  Double lnbpp = log(pow(m_totalCostIntra / (Double)m_numberOfPixel, BETA1));
1114  Double diffLambda = (*beta)*(log((Double)m_picActualBits)-log((Double)m_targetBits));
1115
1116  diffLambda = Clip3(-0.125, 0.125, 0.25*diffLambda);
1117  *alpha    =  (*alpha) * exp(diffLambda);
1118  *beta     =  (*beta) + diffLambda / lnbpp;
1119}
1120
1121
1122Void TEncRCPic::getLCUInitTargetBits()
1123{
1124  Int iAvgBits     = 0;
1125
1126  m_remainingCostIntra = m_totalCostIntra;
1127  for (Int i=m_numberOfLCU-1; i>=0; i--)
1128  {
1129    iAvgBits += Int(m_targetBits * getLCU(i).m_costIntra/m_totalCostIntra);
1130    getLCU(i).m_targetBitsLeft = iAvgBits;
1131  }
1132}
1133
1134
1135Double TEncRCPic::getLCUEstLambdaAndQP(Double bpp, Int clipPicQP, Int *estQP)
1136{
1137  Int   LCUIdx = getLCUCoded();
1138
1139  Double   alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
1140  Double   beta  = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
1141
1142  Double costPerPixel = getLCU(LCUIdx).m_costIntra/(Double)getLCU(LCUIdx).m_numberOfPixel;
1143  costPerPixel = pow(costPerPixel, BETA1);
1144  Double estLambda = calculateLambdaIntra(alpha, beta, costPerPixel, bpp);
1145
1146  Int clipNeighbourQP = g_RCInvalidQPValue;
1147  for (Int i=LCUIdx-1; i>=0; i--)
1148  {
1149    if ((getLCU(i)).m_QP > g_RCInvalidQPValue)
1150    {
1151      clipNeighbourQP = getLCU(i).m_QP;
1152      break;
1153    }
1154  }
1155
1156  Int minQP = clipPicQP - 2;
1157  Int maxQP = clipPicQP + 2;
1158
1159  if ( clipNeighbourQP > g_RCInvalidQPValue )
1160  {
1161    maxQP = min(clipNeighbourQP + 1, maxQP);
1162    minQP = max(clipNeighbourQP - 1, minQP);
1163  }
1164
1165  Double maxLambda=exp(((Double)(maxQP+0.49)-13.7122)/4.2005);
1166  Double minLambda=exp(((Double)(minQP-0.49)-13.7122)/4.2005);
1167
1168  estLambda = Clip3(minLambda, maxLambda, estLambda);
1169
1170  *estQP = Int( 4.2005 * log(estLambda) + 13.7122 + 0.5 );
1171  *estQP = Clip3(minQP, maxQP, *estQP);
1172
1173  return estLambda;
1174}
1175
1176TEncRateCtrl::TEncRateCtrl()
1177{
1178  m_encRCSeq = NULL;
1179  m_encRCGOP = NULL;
1180  m_encRCPic = NULL;
1181}
1182
1183TEncRateCtrl::~TEncRateCtrl()
1184{
1185  destroy();
1186}
1187
1188Void TEncRateCtrl::destroy()
1189{
1190  if ( m_encRCSeq != NULL )
1191  {
1192    delete m_encRCSeq;
1193    m_encRCSeq = NULL;
1194  }
1195  if ( m_encRCGOP != NULL )
1196  {
1197    delete m_encRCGOP;
1198    m_encRCGOP = NULL;
1199  }
1200  while ( m_listRCPictures.size() > 0 )
1201  {
1202    TEncRCPic* p = m_listRCPictures.front();
1203    m_listRCPictures.pop_front();
1204    delete p;
1205  }
1206}
1207
1208Void 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] )
1209{
1210  destroy();
1211
1212  Bool isLowdelay = true;
1213  for ( Int i=0; i<GOPSize-1; i++ )
1214  {
1215    if ( GOPList[i].m_POC > GOPList[i+1].m_POC )
1216    {
1217      isLowdelay = false;
1218      break;
1219    }
1220  }
1221
1222  Int numberOfLevel = 1;
1223  Int adaptiveBit = 0;
1224  if ( keepHierBits > 0 )
1225  {
1226    numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1;
1227  }
1228  if ( !isLowdelay && GOPSize == 8 )
1229  {
1230    numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1;
1231  }
1232  numberOfLevel++;    // intra picture
1233  numberOfLevel++;    // non-reference picture
1234
1235
1236  Int* bitsRatio;
1237  bitsRatio = new Int[ GOPSize ];
1238  for ( Int i=0; i<GOPSize; i++ )
1239  {
1240    bitsRatio[i] = 10;
1241    if ( !GOPList[i].m_refPic )
1242    {
1243      bitsRatio[i] = 2;
1244    }
1245  }
1246
1247  if ( keepHierBits > 0 )
1248  {
1249    Double bpp = (Double)( targetBitrate / (Double)( frameRate*picWidth*picHeight ) );
1250    if ( GOPSize == 4 && isLowdelay )
1251    {
1252      if ( bpp > 0.2 )
1253      {
1254        bitsRatio[0] = 2;
1255        bitsRatio[1] = 3;
1256        bitsRatio[2] = 2;
1257        bitsRatio[3] = 6;
1258      }
1259      else if( bpp > 0.1 )
1260      {
1261        bitsRatio[0] = 2;
1262        bitsRatio[1] = 3;
1263        bitsRatio[2] = 2;
1264        bitsRatio[3] = 10;
1265      }
1266      else if ( bpp > 0.05 )
1267      {
1268        bitsRatio[0] = 2;
1269        bitsRatio[1] = 3;
1270        bitsRatio[2] = 2;
1271        bitsRatio[3] = 12;
1272      }
1273      else
1274      {
1275        bitsRatio[0] = 2;
1276        bitsRatio[1] = 3;
1277        bitsRatio[2] = 2;
1278        bitsRatio[3] = 14;
1279      }
1280
1281      if ( keepHierBits == 2 )
1282      {
1283        adaptiveBit = 1;
1284      }
1285    }
1286    else if ( GOPSize == 8 && !isLowdelay )
1287    {
1288      if ( bpp > 0.2 )
1289      {
1290        bitsRatio[0] = 15;
1291        bitsRatio[1] = 5;
1292        bitsRatio[2] = 4;
1293        bitsRatio[3] = 1;
1294        bitsRatio[4] = 1;
1295        bitsRatio[5] = 4;
1296        bitsRatio[6] = 1;
1297        bitsRatio[7] = 1;
1298      }
1299      else if ( bpp > 0.1 )
1300      {
1301        bitsRatio[0] = 20;
1302        bitsRatio[1] = 6;
1303        bitsRatio[2] = 4;
1304        bitsRatio[3] = 1;
1305        bitsRatio[4] = 1;
1306        bitsRatio[5] = 4;
1307        bitsRatio[6] = 1;
1308        bitsRatio[7] = 1;
1309      }
1310      else if ( bpp > 0.05 )
1311      {
1312        bitsRatio[0] = 25;
1313        bitsRatio[1] = 7;
1314        bitsRatio[2] = 4;
1315        bitsRatio[3] = 1;
1316        bitsRatio[4] = 1;
1317        bitsRatio[5] = 4;
1318        bitsRatio[6] = 1;
1319        bitsRatio[7] = 1;
1320      }
1321      else
1322      {
1323        bitsRatio[0] = 30;
1324        bitsRatio[1] = 8;
1325        bitsRatio[2] = 4;
1326        bitsRatio[3] = 1;
1327        bitsRatio[4] = 1;
1328        bitsRatio[5] = 4;
1329        bitsRatio[6] = 1;
1330        bitsRatio[7] = 1;
1331      }
1332
1333      if ( keepHierBits == 2 )
1334      {
1335        adaptiveBit = 2;
1336      }
1337    }
1338    else
1339    {
1340      printf( "\n hierarchical bit allocation is not support for the specified coding structure currently.\n" );
1341    }
1342  }
1343
1344  Int* GOPID2Level = new Int[ GOPSize ];
1345  for ( Int i=0; i<GOPSize; i++ )
1346  {
1347    GOPID2Level[i] = 1;
1348    if ( !GOPList[i].m_refPic )
1349    {
1350      GOPID2Level[i] = 2;
1351    }
1352  }
1353
1354  if ( keepHierBits > 0 )
1355  {
1356    if ( GOPSize == 4 && isLowdelay )
1357    {
1358      GOPID2Level[0] = 3;
1359      GOPID2Level[1] = 2;
1360      GOPID2Level[2] = 3;
1361      GOPID2Level[3] = 1;
1362    }
1363    else if ( GOPSize == 8 && !isLowdelay )
1364    {
1365      GOPID2Level[0] = 1;
1366      GOPID2Level[1] = 2;
1367      GOPID2Level[2] = 3;
1368      GOPID2Level[3] = 4;
1369      GOPID2Level[4] = 4;
1370      GOPID2Level[5] = 3;
1371      GOPID2Level[6] = 4;
1372      GOPID2Level[7] = 4;
1373    }
1374  }
1375
1376  if ( !isLowdelay && GOPSize == 8 )
1377  {
1378    GOPID2Level[0] = 1;
1379    GOPID2Level[1] = 2;
1380    GOPID2Level[2] = 3;
1381    GOPID2Level[3] = 4;
1382    GOPID2Level[4] = 4;
1383    GOPID2Level[5] = 3;
1384    GOPID2Level[6] = 4;
1385    GOPID2Level[7] = 4;
1386  }
1387
1388  m_encRCSeq = new TEncRCSeq;
1389  m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel, adaptiveBit );
1390  m_encRCSeq->initBitsRatio( bitsRatio );
1391  m_encRCSeq->initGOPID2Level( GOPID2Level );
1392  m_encRCSeq->initPicPara();
1393  if ( useLCUSeparateModel )
1394  {
1395    m_encRCSeq->initLCUPara();
1396  }
1397
1398
1399  delete[] bitsRatio;
1400  delete[] GOPID2Level;
1401}
1402
1403Void TEncRateCtrl::initRCPic( Int frameLevel )
1404{
1405  m_encRCPic = new TEncRCPic;
1406  m_encRCPic->create( m_encRCSeq, m_encRCGOP, frameLevel, m_listRCPictures );
1407}
1408
1409Void TEncRateCtrl::initRCGOP( Int numberOfPictures )
1410{
1411  m_encRCGOP = new TEncRCGOP;
1412  m_encRCGOP->create( m_encRCSeq, numberOfPictures );
1413}
1414
1415Void TEncRateCtrl::destroyRCGOP()
1416{
1417  delete m_encRCGOP;
1418  m_encRCGOP = NULL;
1419}
Note: See TracBrowser for help on using the repository browser.